]> arthur.barton.de Git - bup.git/commitdiff
Don't try to restore owner unless root; handle nonexistent owner/group.
authorRob Browning <rlb@defaultvalue.org>
Sat, 9 Oct 2010 19:42:12 +0000 (14:42 -0500)
committerRob Browning <rlb@defaultvalue.org>
Sat, 9 Oct 2010 19:47:24 +0000 (14:47 -0500)
Don't try to restore a path's owner unless running as root, and even
if asked, don't try to restore the owner or group if it doesn't exist
in the system db.  Just log a warning for now.

lib/bup/metadata.py
lib/bup/t/tmetadata.py

index 6cd744805e50c9e8ee98ec5b2eb5209cb4373030..b41b1f94953633d1ac1431987ad81269bb0c77a2 100644 (file)
@@ -268,11 +268,27 @@ class Metadata:
             elif not stat.S_ISLNK(self.mode):
                 os.chmod(path, 0)
 
+            # Don't try to restore owner unless we're root, and even
+            # if asked, don't try to restore the owner or group if
+            # it doesn't exist in the system db.
             uid = self.uid
             gid = self.gid
             if not restore_numeric_ids:
-                uid = pwd.getpwnam(self.owner)[2]
-                gid = grp.getgrnam(self.group)[2]
+                if os.geteuid() == 0:
+                    try:
+                        uid = pwd.getpwnam(self.owner)[2]
+                    except KeyError:
+                        uid = -1
+                        log('bup: ignoring unknown owner %s for "%s"\n'
+                            % (self.owner, path))
+                else:
+                    uid = -1 # Not root; assume we can't change owner.
+                try:
+                    gid = grp.getgrnam(self.group)[2]
+                except KeyError:
+                    gid = -1
+                    log('bup: ignoring unknown group %s for "%s"\n'
+                        % (self.group, path))
             os.lchown(path, uid, gid)
 
             if _have_lchmod:
index 5e5be5845100136528511aa2ec63fc927d901c26..dba6990ee9adbc6bd093c88a96b67619fb473176 100644 (file)
@@ -1,5 +1,7 @@
-import tempfile
+import grp
+import pwd
 import subprocess
+import tempfile
 from bup import metadata
 from bup.helpers import detect_fakeroot
 from wvtest import *
@@ -146,3 +148,19 @@ def test_restore_restricted_user_group():
                  m.apply_to_path, path, restore_numeric_ids=True)
     finally:
         subprocess.call(['rm', '-rf', tmpdir])
+
+
+@wvtest
+def test_restore_nonexistent_user_group():
+    tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
+    try:
+        path = tmpdir + '/foo'
+        subprocess.call(['mkdir', path])
+        m = metadata.from_path(path, archive_path=path, save_symlinks=True)
+        WVPASSEQ(m.path, path)
+        m.owner = max([x.pw_name for x in pwd.getpwall()], key=len) + 'x'
+        m.group = max([x.gr_name for x in grp.getgrall()], key=len) + 'x'
+        WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=True), None)
+        WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=False), None)
+    finally:
+        subprocess.call(['rm', '-rf', tmpdir])