]> git.saurik.com Git - apt.git/commitdiff
do not try to chown if not run as root
authorDavid Kalnischkies <kalnischkies@gmail.com>
Sat, 13 Jul 2013 21:05:55 +0000 (23:05 +0200)
committerDavid Kalnischkies <kalnischkies@gmail.com>
Mon, 12 Aug 2013 16:01:37 +0000 (18:01 +0200)
If this code is run as non-root we are in a special situation (e.g. in
our testcases) where it is obvious that we can't enforce user/group on
any file, so skip this code altogether instead of bugging users with
an error message – which we also switch to a warning as a failure to
open the file is "just" a warning, so the 'wrong' owner shouldn't be
that much of an issue.

The file is still handled with chmod, so all the security we can enforce
is still enforced of course, which also gets a warning if it fails.

Git-Dch: Ignore

apt-pkg/deb/dpkgpm.cc

index 34ae4e593252fd486c0b6859fe6c57605b889f50..5b2a8251eff804bc35dea6e6ed1501cbb990cba4 100644 (file)
@@ -751,14 +751,15 @@ bool pkgDPkgPM::OpenLog()
         return _error->WarningE("OpenLog", _("Could not open file '%s'"), logfile_name.c_str());
       setvbuf(d->term_out, NULL, _IONBF, 0);
       SetCloseExec(fileno(d->term_out), true);
-      struct passwd *pw;
-      struct group *gr;
-      pw = getpwnam("root");
-      gr = getgrnam("adm");
-      if (pw != NULL && gr != NULL)
-         if(chown(logfile_name.c_str(), pw->pw_uid, gr->gr_gid) != 0)
-            _error->Errno("OpenLog", "chown failed");
-      chmod(logfile_name.c_str(), 0640);
+      if (getuid() == 0) // if we aren't root, we can't chown a file, so don't try it
+      {
+        struct passwd *pw = getpwnam("root");
+        struct group *gr = getgrnam("adm");
+        if (pw != NULL && gr != NULL && chown(logfile_name.c_str(), pw->pw_uid, gr->gr_gid) != 0)
+           _error->WarningE("OpenLog", "chown to root:adm of file %s failed", logfile_name.c_str());
+      }
+      if (chmod(logfile_name.c_str(), 0640) != 0)
+        _error->WarningE("OpenLog", "chmod 0640 of file %s failed", logfile_name.c_str());
       fprintf(d->term_out, "\nLog started: %s\n", timestr);
    }