summaryrefslogtreecommitdiff
path: root/rdiff-backup/rdiff_backup/eas_acls.py
blob: 70523733377c72035f8da38f449d5d7aaaa2f998 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# Copyright 2003 Ben Escoto
#
# This file is part of rdiff-backup.
#
# rdiff-backup is free software; you can redistribute it and/or modify
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# rdiff-backup is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rdiff-backup; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

"""Store and retrieve extended attributes and access control lists

Not all file systems will have EAs and ACLs, but if they do, store
this information in separate files in the rdiff-backup-data directory,
called extended_attributes.<time>.snapshot and
access_control_lists.<time>.snapshot.

"""

from __future__ import generators
import base64, errno, re
try: import posix1e
except ImportError: pass
import static, Globals, eas_acls, connection, metadata, rorpiter, log, C, \
	   rpath, user_group

# When an ACL gets dropped, put name in dropped_acl_names.  This is
# only used so that only the first dropped ACL for any given name
# triggers a warning.
dropped_acl_names = {}

class ExtendedAttributes:
	"""Hold a file's extended attribute information"""
	def __init__(self, index, attr_dict = None):
		"""Initialize EA object with no attributes"""
		self.index = index
		if attr_dict is None: self.attr_dict = {}
		else: self.attr_dict = attr_dict

	def __eq__(self, ea):
		"""Equal if all attributes are equal"""
		assert isinstance(ea, ExtendedAttributes)
		return ea.attr_dict == self.attr_dict
	def __ne__(self, ea): return not self.__eq__(ea)

	def get_indexpath(self): return self.index and '/'.join(self.index) or '.'

	def read_from_rp(self, rp):
		"""Set the extended attributes from an rpath"""
		try:
			attr_list = rp.conn.xattr.listxattr(rp.path, rp.issym())
		except IOError, exc:
			if exc[0] in (errno.EOPNOTSUPP, errno.EPERM, errno.ETXTBSY):
				return # if not supported, consider empty
			if exc[0] in (errno.EACCES, errno.ENOENT, errno.ELOOP):
				log.Log("Warning: listattr(%s): %s" % (repr(rp.path), exc), 4)
				return
			raise
		for attr in attr_list:
			if attr.startswith('system.'):
				# Do not preserve system extended attributes
				continue
			if not rp.isdir() and attr == 'com.apple.ResourceFork':
				# Resource Fork handled elsewhere, except for directories
				continue
			try:
				self.attr_dict[attr] = \
					rp.conn.xattr.getxattr(rp.path, attr, rp.issym())
			except IOError, exc:
				# File probably modified while reading, just continue
				if exc[0] == errno.ENODATA: continue
				elif exc[0] == errno.ENOENT: break
				# Handle bug in pyxattr < 0.2.2
				elif exc[0] == errno.ERANGE: continue
				else: raise

	def clear_rp(self, rp):
		"""Delete all the extended attributes in rpath"""
		try:
			for name in rp.conn.xattr.listxattr(rp.path, rp.issym()):
				try:
					rp.conn.xattr.removexattr(rp.path, name, rp.issym())
				except IOError, exc:
					# SELinux attributes cannot be removed, and we don't want
					# to bail out or be too noisy at low log levels.
					if exc[0] == errno.EACCES:
						log.Log("Warning: unable to remove xattr %s from %s"
							% (name, repr(rp.path)), 7)
						continue
					else: raise
		except IOError, exc:
			if exc[0] == errno.EOPNOTSUPP or exc[0] == errno.EPERM:
				return # if not supported, consider empty
			elif exc[0] == errno.ENOENT: # path is bad
				log.Log("Warning: unable to clear xattrs on %s: %s" %
						(repr(rp.path), exc), 3)
				return
			else: raise

	def write_to_rp(self, rp):
		"""Write extended attributes to rpath rp"""
		self.clear_rp(rp)
		for (name, value) in self.attr_dict.iteritems():
			try:
				rp.conn.xattr.setxattr(rp.path, name, value, 0, rp.issym())
			except IOError, exc:
				# Mac and Linux attributes have different namespaces, so
				# fail gracefully if can't call setxattr
				if exc[0] in (errno.EOPNOTSUPP, errno.EPERM, errno.EACCES,
						errno.ENOENT, errno.EINVAL):
					log.Log("Warning: unable to write xattr %s to %s"
							% (name, repr(rp.path)), 6)
					continue
				else: raise

	def get(self, name):
		"""Return attribute attached to given name"""
		return self.attr_dict[name]

	def set(self, name, value = ""):
		"""Set given name to given value.  Does not write to disk"""
		self.attr_dict[name] = value

	def delete(self, name):
		"""Delete value associated with given name"""
		del self.attr_dict[name]

	def empty(self):
		"""Return true if no extended attributes are set"""
		return not self.attr_dict

def ea_compare_rps(rp1, rp2):
	"""Return true if rp1 and rp2 have same extended attributes"""
	ea1 = ExtendedAttributes(rp1.index)
	ea1.read_from_rp(rp1)
	ea2 = ExtendedAttributes(rp2.index)
	ea2.read_from_rp(rp2)
	return ea1 == ea2


def EA2Record(ea):
	"""Convert ExtendedAttributes object to text record"""
	str_list = ['# file: %s' % C.acl_quote(ea.get_indexpath())]
	for (name, val) in ea.attr_dict.iteritems():
		if not val: str_list.append(name)
		else:
			encoded_val = base64.encodestring(val).replace('\n', '')
			try:
				str_list.append('%s=0s%s' % (C.acl_quote(name), encoded_val))
			except UnicodeEncodeError:
				log.Log("Warning: unable to store Unicode extended attribute %s"
							% repr(name), 3)
	return '\n'.join(str_list)+'\n'

def Record2EA(record):
	"""Convert text record to ExtendedAttributes object"""
	lines = record.split('\n')
	first = lines.pop(0)
	if not first[:8] == "# file: ":
		raise metadata.ParsingError("Bad record beginning: " + first[:8])
	filename = first[8:]
	if filename == '.': index = ()
	else: index = tuple(C.acl_unquote(filename).split('/'))
	ea = ExtendedAttributes(index)

	for line in lines:
		line = line.strip()
		if not line: continue
		assert line[0] != '#', line
		eq_pos = line.find('=')
		if eq_pos == -1: ea.set(line)
		else:
			name = line[:eq_pos]
			assert line[eq_pos+1:eq_pos+3] == '0s', \
				   "Currently only base64 encoding supported"
			encoded_val = line[eq_pos+3:]
			ea.set(name, base64.decodestring(encoded_val))
	return ea


class EAExtractor(metadata.FlatExtractor):
	"""Iterate ExtendedAttributes objects from the EA information file"""
	record_boundary_regexp = re.compile('(?:\\n|^)(# file: (.*?))\\n')
	record_to_object = staticmethod(Record2EA)
	def filename_to_index(self, filename):
		"""Convert possibly quoted filename to index tuple"""
		if filename == '.': return ()
		else: return tuple(C.acl_unquote(filename).split('/'))

class ExtendedAttributesFile(metadata.FlatFile):
	"""Store/retrieve EAs from extended_attributes file"""
	_prefix = "extended_attributes"
	_extractor = EAExtractor
	_object_to_record = staticmethod(EA2Record)

def join_ea_iter(rorp_iter, ea_iter):
	"""Update a rorp iter by adding the information from ea_iter"""
	for rorp, ea in rorpiter.CollateIterators(rorp_iter, ea_iter):
		assert rorp, "Missing rorp for index %s" % (ea.index,)
		if not ea: ea = ExtendedAttributes(rorp.index)
		rorp.set_ea(ea)
		yield rorp


class AccessControlLists:
	"""Hold a file's access control list information

	Since posix1e.ACL objects cannot be pickled, and because they lack
	user/group name information, store everything in self.entry_list
	and self.default_entry_list.

	"""
	def __init__(self, index, acl_text = None):
		"""Initialize object with index and possibly acl_text"""
		self.index = index
		if acl_text: self.set_from_text(acl_text)
		else: self.entry_list = self.default_entry_list = None

	def set_from_text(self, text):
		"""Set self.entry_list and self.default_entry_list from text"""
		self.entry_list, self.default_entry_list = [], []
		for line in text.split('\n'):
			comment_pos = line.find('#')
			if comment_pos >= 0: line = line[:comment_pos]
			line = line.strip()
			if not line: continue

			if line.startswith('default:'):
				entrytuple = self.text_to_entrytuple(line[8:])
				self.default_entry_list.append(entrytuple)
			else: self.entry_list.append(self.text_to_entrytuple(line))

	def __str__(self):
		"""Return text version of acls"""
		if not self.entry_list: return ""
		slist = map(self.entrytuple_to_text, self.entry_list)
		if self.default_entry_list:
			slist.extend(map(lambda e: "default:" + self.entrytuple_to_text(e),
							 self.default_entry_list))
		return "\n".join(slist)

	def entrytuple_to_text(self, entrytuple):
		"""Return text version of entrytuple, as in getfacl"""
		tagchar, name_pair, perms = entrytuple
		if tagchar == "U": text = 'user::'
		elif tagchar == "u":
			uid, uname = name_pair
			text = 'user:%s:' % (uname or uid)
		elif tagchar == "G":
			text = 'group::'
		elif tagchar == "g":
			gid, gname = name_pair
			text = 'group:%s:' % (gname or gid)
		elif tagchar == "M": text = 'mask::'
		else:
			assert tagchar == "O", tagchar
			text = 'other::'

		permstring = '%s%s%s' % (perms & 4 and 'r' or '-',
								 perms & 2 and 'w' or '-',
								 perms & 1 and 'x' or '-')
		return text+permstring

	def text_to_entrytuple(self, text):
		"""Return entrytuple given text like 'user:foo:r--'

		See the acl_to_list function for entrytuple documentation.

		"""
		typetext, qualifier, permtext = text.split(':')
		if qualifier:
			try: uid = int(qualifier)
			except ValueError: namepair = (None, qualifier)
			else: namepair = (uid, None)

			if typetext == 'user': typechar = "u"
			else:
				assert typetext == 'group', (typetext, text)
				typechar = "g"
		else:
			namepair = None
			if typetext == 'user': typechar = "U"
			elif typetext == 'group': typechar = "G"
			elif typetext == 'mask': typechar = "M"
			else:
				assert typetext == 'other', (typetext, text)
				typechar = "O"

		assert len(permtext) == 3, (permtext, text)
		read, write, execute = permtext
		perms = ((read == 'r') << 2 |
				 (write == 'w') << 1 |
				 (execute == 'x'))
		return (typechar, namepair, perms)

	def cmp_entry_list(self, l1, l2):
		"""True if the lists have same entries.  Assume preordered"""
		if not l1: return not l2
		if not l2 or len(l1) != len(l2): return 0
		for i in range(len(l1)):
			type1, namepair1, perms1 = l1[i]
			type2, namepair2, perms2 = l2[i]
			if type1 != type2 or perms1 != perms2: return 0
			if namepair1 == namepair2: continue
			if not namepair1 or not namepair2: return 0
			(id1, name1), (id2, name2) = namepair1, namepair2
			if name1:
				if name1 == name2: continue
				else: return 0
			if name2: return 0
			if id1 != id2: return 0
		return 1

	def __eq__(self, acl):
		"""Compare self and other access control list

		Basic acl permissions are considered equal to an empty acl
		object.

		"""
		assert isinstance(acl, self.__class__)
		if self.is_basic(): return acl.is_basic()
		return (self.cmp_entry_list(self.entry_list, acl.entry_list) and
				self.cmp_entry_list(self.default_entry_list,
									acl.default_entry_list))
	
	def __ne__(self, acl): return not self.__eq__(acl)
	
	def eq_verbose(self, acl):
		"""Returns same as __eq__ but print explanation if not equal"""
		if not self.cmp_entry_list(self.entry_list, acl.entry_list):
			print "ACL entries for %s compare differently" % (self.index,)
			return 0
		if not self.cmp_entry_list(self.default_entry_list,
								   acl.default_entry_list):
			print "Default ACL entries for %s do not compare" % (self.index,)
			return 0
		return 1

	def get_indexpath(self): return self.index and '/'.join(self.index) or '.'

	def is_basic(self):
		"""True if acl can be reduced to standard unix permissions

		Assume that if they are only three entries, they correspond to
		user, group, and other, and thus don't use any special ACL
		features.

		"""
		if not self.entry_list and not self.default_entry_list: return 1
		assert len(self.entry_list) >= 3, self.entry_list
		return len(self.entry_list) == 3 and not self.default_entry_list

	def read_from_rp(self, rp):
		"""Set self.ACL from an rpath, or None if not supported"""
		self.entry_list, self.default_entry_list = \
						 rp.conn.eas_acls.get_acl_lists_from_rp(rp)

	def write_to_rp(self, rp, map_names = 1):
		"""Write current access control list to RPath rp"""
		rp.conn.eas_acls.set_rp_acl(rp, self.entry_list,
									self.default_entry_list, map_names)


def set_rp_acl(rp, entry_list = None, default_entry_list = None,
			   map_names = 1):
	"""Set given rp with ACL that acl_text defines.  rp should be local"""
	assert rp.conn is Globals.local_connection
	if entry_list: acl = list_to_acl(entry_list, map_names)
	else: acl = posix1e.ACL()
	acl.applyto(rp.path)

	if rp.isdir():
		if default_entry_list:
			def_acl = list_to_acl(default_entry_list, map_names)
		else: def_acl = posix1e.ACL()
		def_acl.applyto(rp.path, posix1e.ACL_TYPE_DEFAULT)

def get_acl_lists_from_rp(rp):
	"""Returns (acl_list, def_acl_list) from an rpath.  Call locally"""
	assert rp.conn is Globals.local_connection
	try: acl = posix1e.ACL(file=rp.path)
	except IOError, exc:
		if exc[0] == errno.EOPNOTSUPP:
			acl = None
		elif exc[0] == errno.ENOENT:
			log.Log("Warning: unable to read ACL from %s: %s"
					% (repr(rp.path), exc), 3)
			acl = None
		else: raise
	if rp.isdir():
		try: def_acl = posix1e.ACL(filedef=rp.path)
		except IOError, exc:
			if exc[0] == errno.EOPNOTSUPP:
				def_acl = None
			elif exc[0] == errno.ENOENT:
				log.Log("Warning: unable to read default ACL from %s: %s"
					% (repr(rp.path), exc), 3)
				def_acl = None
			else: raise
	else: def_acl = None
	return (acl and acl_to_list(acl), def_acl and acl_to_list(def_acl))

def acl_to_list(acl):
	"""Return list representation of posix1e.ACL object

	ACL objects cannot be pickled, so this representation keeps
	the structure while adding that option.  Also we insert the
	username along with the id, because that information will be
	lost when moved to another system.

	The result will be a list of tuples.  Each tuple will have the
	form (acltype, (uid or gid, uname or gname) or None, permissions
	as an int).  acltype is encoded as a single character:

	U - ACL_USER_OBJ
	u - ACL_USER
	G - ACL_GROUP_OBJ
	g - ACL_GROUP
	M - ACL_MASK
	O - ACL_OTHER

	"""
	def acltag_to_char(tag):
		if tag == posix1e.ACL_USER_OBJ: return "U"
		elif tag == posix1e.ACL_USER: return "u"
		elif tag == posix1e.ACL_GROUP_OBJ: return "G"
		elif tag == posix1e.ACL_GROUP: return "g"
		elif tag == posix1e.ACL_MASK: return "M"
		else:
			assert tag == posix1e.ACL_OTHER, tag
			return "O"

	def entry_to_tuple(entry):
		tagchar = acltag_to_char(entry.tag_type)
		if tagchar == "u":
			uid = entry.qualifier
			owner_pair = (uid, user_group.uid2uname(uid))
		elif tagchar == "g":
			gid = entry.qualifier
			owner_pair = (gid, user_group.gid2gname(gid))
		else: owner_pair = None

		perms = (entry.permset.read << 2 | 
				 entry.permset.write << 1 |
				 entry.permset.execute)
		return (tagchar, owner_pair, perms)
	return map(entry_to_tuple, acl)

def list_to_acl(entry_list, map_names = 1):
	"""Return posix1e.ACL object from list representation

	If map_names is true, use user_group to update the names for the
	current system, and drop if not available.  Otherwise just use the
	same id.

	See the acl_to_list function for the format of an acllist.

	"""
	def char_to_acltag(typechar):
		"""Given typechar, query posix1e module for appropriate constant"""
		if typechar == "U": return posix1e.ACL_USER_OBJ
		elif typechar == "u": return posix1e.ACL_USER
		elif typechar == "G": return posix1e.ACL_GROUP_OBJ
		elif typechar == "g": return posix1e.ACL_GROUP
		elif typechar == "M": return posix1e.ACL_MASK
		else:
			assert typechar == "O", typechar
			return posix1e.ACL_OTHER

	def warn_drop(name):
		"""Warn about acl with name getting dropped"""
		global dropped_acl_names
		if Globals.never_drop_acls:
			log.Log.FatalError(
"--never-drop-acls specified but cannot map name\n"
"%s occurring inside an ACL." % (name,))
		if dropped_acl_names.has_key(name): return
		log.Log("Warning: name %s not found on system, dropping ACL entry.\n"
				"Further ACL entries dropped with this name will not "
				"trigger further warnings" % (name,), 2)
		dropped_acl_names[name] = name

	acl = posix1e.ACL()
	for typechar, owner_pair, perms in entry_list:
		id = None
		if owner_pair:
			if map_names:
				if typechar == "u": id = user_group.acl_user_map(*owner_pair)
				else:
					assert typechar == "g", (typechar, owner_pair, perms)
					id = user_group.acl_group_map(*owner_pair)
				if id is None:
					warn_drop(owner_pair[1])
					continue
			else:
				assert owner_pair[0] is not None, (typechar, owner_pair, perms)
				id = owner_pair[0]

		entry = posix1e.Entry(acl)
		entry.tag_type = char_to_acltag(typechar)
		if id is not None: entry.qualifier = id
		entry.permset.read = perms >> 2
		entry.permset.write = perms >> 1 & 1
		entry.permset.execute = perms & 1
	return acl

def acl_compare_rps(rp1, rp2):
	"""Return true if rp1 and rp2 have same acl information"""
	acl1 = AccessControlLists(rp1.index)
	acl1.read_from_rp(rp1)
	acl2 = AccessControlLists(rp2.index)
	acl2.read_from_rp(rp2)
	return acl1 == acl2


def ACL2Record(acl):
	"""Convert an AccessControlLists object into a text record"""
	return '# file: %s\n%s\n' % (C.acl_quote(acl.get_indexpath()), str(acl))

def Record2ACL(record):
	"""Convert text record to an AccessControlLists object"""
	newline_pos = record.find('\n')
	first_line = record[:newline_pos]
	if not first_line.startswith('# file: '):
		raise metadata.ParsingError("Bad record beginning: "+ first_line)
	filename = first_line[8:]
	if filename == '.': index = ()
	else: index = tuple(C.acl_unquote(filename).split('/'))
	return AccessControlLists(index, record[newline_pos:])

class ACLExtractor(EAExtractor):
	"""Iterate AccessControlLists objects from the ACL information file

	Except for the record_to_object method, we can reuse everything in
	the EAExtractor class because the file formats are so similar.

	"""
	record_to_object = staticmethod(Record2ACL)

class AccessControlListFile(metadata.FlatFile):
	"""Store/retrieve ACLs from extended attributes file"""
	_prefix = 'access_control_lists'
	_extractor = ACLExtractor
	_object_to_record = staticmethod(ACL2Record)

def join_acl_iter(rorp_iter, acl_iter):
	"""Update a rorp iter by adding the information from acl_iter"""
	for rorp, acl in rorpiter.CollateIterators(rorp_iter, acl_iter):
		assert rorp, "Missing rorp for index %s" % (acl.index,)
		if not acl: acl = AccessControlLists(rorp.index)
		rorp.set_acl(acl)
		yield rorp
	

def rpath_acl_get(rp):
	"""Get acls of given rpath rp.

	This overrides a function in the rpath module.

	"""
	acl = AccessControlLists(rp.index)
	if not rp.issym(): acl.read_from_rp(rp)
	return acl
rpath.acl_get = rpath_acl_get

def rpath_get_blank_acl(index):
	"""Get a blank AccessControlLists object (override rpath function)"""
	return AccessControlLists(index)
rpath.get_blank_acl = rpath_get_blank_acl

def rpath_ea_get(rp):
	"""Get extended attributes of given rpath

	This overrides a function in the rpath module.

	"""
	ea = ExtendedAttributes(rp.index)
	if not rp.issock() and not rp.isfifo():
		ea.read_from_rp(rp)
	return ea
rpath.ea_get = rpath_ea_get

def rpath_get_blank_ea(index):
	"""Get a blank ExtendedAttributes object (override rpath function)"""
	return ExtendedAttributes(index)
rpath.get_blank_ea = rpath_get_blank_ea