platform_linux.pyx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import os
  2. import re
  3. from attic.helpers import posix_acl_use_stored_uid_gid, user2uid, group2gid
  4. API_VERSION = 1
  5. cdef extern from "sys/types.h":
  6. int ACL_TYPE_ACCESS
  7. int ACL_TYPE_DEFAULT
  8. cdef extern from "sys/acl.h":
  9. ctypedef struct _acl_t:
  10. pass
  11. ctypedef _acl_t *acl_t
  12. int acl_free(void *obj)
  13. acl_t acl_get_file(const char *path, int type)
  14. acl_t acl_set_file(const char *path, int type, acl_t acl)
  15. acl_t acl_from_text(const char *buf)
  16. char *acl_to_text(acl_t acl, ssize_t *len)
  17. cdef extern from "acl/libacl.h":
  18. int acl_extended_file_nofollow(const char *path)
  19. _comment_re = re.compile(' *#.*', re.M)
  20. def acl_use_local_uid_gid(acl):
  21. """Replace the user/group field with the local uid/gid if possible
  22. """
  23. entries = []
  24. for entry in acl.decode('ascii').split('\n'):
  25. if entry:
  26. fields = entry.split(':')
  27. if fields[0] == 'user' and fields[1]:
  28. fields[1] = user2uid(fields[1], fields[3])
  29. elif fields[0] == 'group' and fields[1]:
  30. fields[1] = group2gid(fields[1], fields[3])
  31. entries.append(':'.join(entry.split(':')[:3]))
  32. return ('\n'.join(entries)).encode('ascii')
  33. cdef acl_append_numeric_ids(acl):
  34. """Extend the "POSIX 1003.1e draft standard 17" format with an additional uid/gid field
  35. """
  36. entries = []
  37. for entry in _comment_re.sub('', acl.decode('ascii')).split('\n'):
  38. if entry:
  39. type, name, permission = entry.split(':')
  40. if name and type == 'user':
  41. entries.append(':'.join([type, name, permission, str(user2uid(name, name))]))
  42. elif name and type == 'group':
  43. entries.append(':'.join([type, name, permission, str(group2gid(name, name))]))
  44. else:
  45. entries.append(entry)
  46. return ('\n'.join(entries)).encode('ascii')
  47. cdef acl_numeric_ids(acl):
  48. """Replace the "POSIX 1003.1e draft standard 17" user/group field with uid/gid
  49. """
  50. entries = []
  51. for entry in _comment_re.sub('', acl.decode('ascii')).split('\n'):
  52. if entry:
  53. type, name, permission = entry.split(':')
  54. if name and type == 'user':
  55. uid = str(user2uid(name, name))
  56. entries.append(':'.join([type, uid, permission, uid]))
  57. elif name and type == 'group':
  58. gid = str(group2gid(name, name))
  59. entries.append(':'.join([type, gid, permission, gid]))
  60. else:
  61. entries.append(entry)
  62. return ('\n'.join(entries)).encode('ascii')
  63. def acl_get(path, item, numeric_owner=False):
  64. """Saves ACL Entries
  65. If `numeric_owner` is True the user/group field is not preserved only uid/gid
  66. """
  67. cdef acl_t default_acl = NULL
  68. cdef acl_t access_acl = NULL
  69. cdef char *default_text = NULL
  70. cdef char *access_text = NULL
  71. if acl_extended_file_nofollow(<bytes>os.fsencode(path)) <= 0:
  72. return
  73. if numeric_owner:
  74. converter = acl_numeric_ids
  75. else:
  76. converter = acl_append_numeric_ids
  77. try:
  78. access_acl = acl_get_file(<bytes>os.fsencode(path), ACL_TYPE_ACCESS)
  79. if access_acl:
  80. access_text = acl_to_text(access_acl, NULL)
  81. if access_text:
  82. item[b'acl_access'] = converter(access_text)
  83. default_acl = acl_get_file(<bytes>os.fsencode(path), ACL_TYPE_DEFAULT)
  84. if default_acl:
  85. default_text = acl_to_text(default_acl, NULL)
  86. if default_text:
  87. item[b'acl_default'] = converter(default_text)
  88. finally:
  89. acl_free(default_text)
  90. acl_free(default_acl)
  91. acl_free(access_text)
  92. acl_free(access_acl)
  93. def acl_set(path, item, numeric_owner=False):
  94. """Restore ACL Entries
  95. If `numeric_owner` is True the stored uid/gid is used instead
  96. of the user/group names
  97. """
  98. cdef acl_t access_acl = NULL
  99. cdef acl_t default_acl = NULL
  100. if numeric_owner:
  101. converter = posix_acl_use_stored_uid_gid
  102. else:
  103. converter = acl_use_local_uid_gid
  104. access_text = item.get(b'acl_access')
  105. default_text = item.get(b'acl_default')
  106. if access_text:
  107. try:
  108. access_acl = acl_from_text(<bytes>converter(access_text))
  109. if access_acl:
  110. acl_set_file(<bytes>os.fsencode(path), ACL_TYPE_ACCESS, access_acl)
  111. finally:
  112. acl_free(access_acl)
  113. if default_text:
  114. try:
  115. default_acl = acl_from_text(<bytes>converter(default_text))
  116. if default_acl:
  117. acl_set_file(<bytes>os.fsencode(path), ACL_TYPE_DEFAULT, default_acl)
  118. finally:
  119. acl_free(default_acl)