]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/deb/dpkgpm.cc
pkg from only trusted sources keeps being trusted
[apt.git] / apt-pkg / deb / dpkgpm.cc
index 496daf1df4799a1a27ba29c8e999309ec6aec0ca..3c101376192f6c5ed2a0cd59562ddf08bebe3e04 100644 (file)
@@ -134,6 +134,8 @@ static void dpkgChrootDirectory()
    std::cerr << "Chrooting into " << chrootDir << std::endl;
    if (chroot(chrootDir.c_str()) != 0)
       _exit(100);
+   if (chdir("/") != 0)
+      _exit(100);
 }
                                                                        /*}}}*/
 
@@ -147,11 +149,11 @@ static
 pkgCache::VerIterator FindNowVersion(const pkgCache::PkgIterator &Pkg)
 {
    pkgCache::VerIterator Ver;
-   for (Ver = Pkg.VersionList(); Ver.end() == false; Ver++)
+   for (Ver = Pkg.VersionList(); Ver.end() == false; ++Ver)
    {
       pkgCache::VerFileIterator Vf = Ver.FileList();
       pkgCache::PkgFileIterator F = Vf.File();
-      for (F = Vf.File(); F.end() == false; F++)
+      for (F = Vf.File(); F.end() == false; ++F)
       {
          if (F && F.Archive())
          {
@@ -187,7 +189,7 @@ pkgDPkgPM::~pkgDPkgPM()
 bool pkgDPkgPM::Install(PkgIterator Pkg,string File)
 {
    if (File.empty() == true || Pkg.end() == true)
-      return _error->Error("Internal Error, No file name for %s",Pkg.Name());
+      return _error->Error("Internal Error, No file name for %s",Pkg.FullName().c_str());
 
    // If the filename string begins with DPkg::Chroot-Directory, return the
    // substr that is within the chroot so dpkg can access it.
@@ -238,15 +240,23 @@ bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge)
    return true;
 }
                                                                        /*}}}*/
-// DPkgPM::SendV2Pkgs - Send version 2 package info                    /*{{{*/
+// DPkgPM::SendPkgInfo - Send info for install-pkgs hook               /*{{{*/
 // ---------------------------------------------------------------------
 /* This is part of the helper script communication interface, it sends
    very complete information down to the other end of the pipe.*/
 bool pkgDPkgPM::SendV2Pkgs(FILE *F)
 {
-   fprintf(F,"VERSION 2\n");
-   
-   /* Write out all of the configuration directives by walking the 
+   return SendPkgsInfo(F, 2);
+}
+bool pkgDPkgPM::SendPkgsInfo(FILE * const F, unsigned int const &Version)
+{
+   // This version of APT supports only v3, so don't sent higher versions
+   if (Version <= 3)
+      fprintf(F,"VERSION %u\n", Version);
+   else
+      fprintf(F,"VERSION 3\n");
+
+   /* Write out all of the configuration directives by walking the
       configuration tree */
    const Configuration::Item *Top = _config->Tree(0);
    for (; Top != 0;)
@@ -280,30 +290,51 @@ bool pkgDPkgPM::SendV2Pkgs(FILE *F)
       pkgDepCache::StateCache &S = Cache[I->Pkg];
       
       fprintf(F,"%s ",I->Pkg.Name());
-      // Current version
-      if (I->Pkg->CurrentVer == 0)
-        fprintf(F,"- ");
+
+      // Current version which we are going to replace
+      pkgCache::VerIterator CurVer = I->Pkg.CurrentVer();
+      if (CurVer.end() == true && (I->Op == Item::Remove || I->Op == Item::Purge))
+        CurVer = FindNowVersion(I->Pkg);
+
+      if (CurVer.end() == true)
+      {
+        if (Version <= 2)
+           fprintf(F, "- ");
+        else
+           fprintf(F, "- - none ");
+      }
       else
-        fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr());
-      
-      // Show the compare operator
-      // Target version
+      {
+        fprintf(F, "%s ", CurVer.VerStr());
+        if (Version >= 3)
+           fprintf(F, "%s %s ", CurVer.Arch(), CurVer.MultiArchType());
+      }
+
+      // Show the compare operator between current and install version
       if (S.InstallVer != 0)
       {
+        pkgCache::VerIterator const InstVer = S.InstVerIter(Cache);
         int Comp = 2;
-        if (I->Pkg->CurrentVer != 0)
-           Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer());
+        if (CurVer.end() == false)
+           Comp = InstVer.CompareVer(CurVer);
         if (Comp < 0)
            fprintf(F,"> ");
-        if (Comp == 0)
+        else if (Comp == 0)
            fprintf(F,"= ");
-        if (Comp > 0)
+        else if (Comp > 0)
            fprintf(F,"< ");
-        fprintf(F,"%s ",S.InstVerIter(Cache).VerStr());
+        fprintf(F, "%s ", InstVer.VerStr());
+        if (Version >= 3)
+           fprintf(F, "%s %s ", InstVer.Arch(), InstVer.MultiArchType());
       }
       else
-        fprintf(F,"> - ");
-      
+      {
+        if (Version <= 2)
+           fprintf(F, "> - ");
+        else
+           fprintf(F, "> - - none ");
+      }
+
       // Show the filename/operation
       if (I->Op == Item::Install)
       {
@@ -313,9 +344,9 @@ bool pkgDPkgPM::SendV2Pkgs(FILE *F)
         else
            fprintf(F,"%s\n",I->File.c_str());
       }      
-      if (I->Op == Item::Configure)
+      else if (I->Op == Item::Configure)
         fprintf(F,"**CONFIGURE**\n");
-      if (I->Op == Item::Remove ||
+      else if (I->Op == Item::Remove ||
          I->Op == Item::Purge)
         fprintf(F,"**REMOVE**\n");
       
@@ -351,24 +382,32 @@ bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
       OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos);
       
       unsigned int Version = _config->FindI(OptSec+"::Version",1);
+      unsigned int InfoFD = _config->FindI(OptSec + "::InfoFD", STDIN_FILENO);
       
       // Create the pipes
       int Pipes[2];
       if (pipe(Pipes) != 0)
         return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
-      SetCloseExec(Pipes[0],true);
+      if (InfoFD != (unsigned)Pipes[0])
+        SetCloseExec(Pipes[0],true);
+      else
+        _config->Set("APT::Keep-Fds::", Pipes[0]);
       SetCloseExec(Pipes[1],true);
-      
+
       // Purified Fork for running the script
-      pid_t Process = ExecFork();      
+      pid_t Process = ExecFork();
       if (Process == 0)
       {
         // Setup the FDs
-        dup2(Pipes[0],STDIN_FILENO);
+        dup2(Pipes[0], InfoFD);
         SetCloseExec(STDOUT_FILENO,false);
-        SetCloseExec(STDIN_FILENO,false);      
+        SetCloseExec(STDIN_FILENO,false);
         SetCloseExec(STDERR_FILENO,false);
 
+        string hookfd;
+        strprintf(hookfd, "%d", InfoFD);
+        setenv("APT_HOOK_INFO_FD", hookfd.c_str(), 1);
+
         dpkgChrootDirectory();
         const char *Args[4];
         Args[0] = "/bin/sh";
@@ -378,6 +417,8 @@ bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
         execv(Args[0],(char **)Args);
         _exit(100);
       }
+      if (InfoFD == (unsigned)Pipes[0])
+        _config->Clear("APT::Keep-Fds", Pipes[0]);
       close(Pipes[0]);
       FILE *F = fdopen(Pipes[1],"w");
       if (F == 0)
@@ -404,7 +445,7 @@ bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
         }
       }
       else
-        SendV2Pkgs(F);
+        SendPkgsInfo(F, Version);
 
       fclose(F);
       
@@ -591,6 +632,9 @@ void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line)
             << ":"  << (PackagesDone/float(PackagesTotal)*100.0) 
             << ":" << s
             << endl;
+      if(_config->FindB("DPkgPM::Progress", false) == true)
+         SendTerminalProgress(PackagesDone/float(PackagesTotal)*100.0);
+
       if(OutStatusFd > 0)
         FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
       if (Debug == true)
@@ -720,13 +764,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)
-         chown(logfile_name.c_str(), pw->pw_uid, gr->gr_gid);
-      chmod(logfile_name.c_str(), 0644);
+      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);
    }
 
@@ -738,6 +784,7 @@ bool pkgDPkgPM::OpenLog()
       d->history_out = fopen(history_name.c_str(),"a");
       if (d->history_out == NULL)
         return _error->WarningE("OpenLog", _("Could not open file '%s'"), history_name.c_str());
+      SetCloseExec(fileno(d->history_out), true);
       chmod(history_name.c_str(), 0644);
       fprintf(d->history_out, "\nStart-Date: %s\n", timestr);
       string remove, purge, install, reinstall, upgrade, downgrade;
@@ -830,6 +877,18 @@ bool pkgDPkgPM::CloseLog()
    return true;
 }
                                                                        /*}}}*/
+// DPkgPM::SendTerminalProgress                                        /*{{{*/
+// ---------------------------------------------------------------------
+/* Send progress info to the terminal
+ */
+void pkgDPkgPM::SendTerminalProgress(float percentage)
+{
+   // FIXME: use colors too
+   std::cout << "\r\n"
+             << "Progress: [" << percentage << "%]"
+             << "\r\n";
+}
+                                                                       /*}}}*/
 /*{{{*/
 // This implements a racy version of pselect for those architectures
 // that don't have a working implementation.
@@ -1130,7 +1189,9 @@ bool pkgDPkgPM::Go(int OutStatusFd)
            if (I->Op == Item::Configure && disappearedPkgs.find(I->Pkg.Name()) != disappearedPkgs.end())
               continue;
            // We keep this here to allow "smooth" transitions from e.g. multiarch dpkg/ubuntu to dpkg/debian
-           if (dpkgMultiArch == false && (I->Pkg.Arch() == nativeArch || !strcmp(I->Pkg.Arch(), "all")))
+           if (dpkgMultiArch == false && (I->Pkg.Arch() == nativeArch ||
+                                          strcmp(I->Pkg.Arch(), "all") == 0 ||
+                                          strcmp(I->Pkg.Arch(), "none") == 0))
            {
               char const * const name = I->Pkg.Name();
               ADDARG(name);
@@ -1147,7 +1208,9 @@ bool pkgDPkgPM::Go(int OutStatusFd)
                }
               else
                  PkgVer = Cache[I->Pkg].InstVerIter(Cache);
-               if (PkgVer.end() == false)
+              if (strcmp(I->Pkg.Arch(), "none") == 0)
+                 ; // never arch-qualify a package without an arch
+              else if (PkgVer.end() == false)
                   name.append(":").append(PkgVer.Arch());
                else
                   _error->Warning("Can not find PkgVer for '%s'", name.c_str());
@@ -1200,16 +1263,13 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 
       // if tcgetattr does not return zero there was a error
       // and we do not do any pty magic
-      if (tcgetattr(0, &tt) == 0)
+      _error->PushToStack();
+      if (tcgetattr(STDOUT_FILENO, &tt) == 0)
       {
         ioctl(0, TIOCGWINSZ, (char *)&win);
-        if (openpty(&master, &slave, NULL, &tt, &win) < 0) 
+        if (openpty(&master, &slave, NULL, &tt, &win) < 0)
         {
-           const char *s = _("Can not write log, openpty() "
-                             "failed (/dev/pts not mounted?)\n");
-           fprintf(stderr, "%s",s);
-            if(d->term_out)
-              fprintf(d->term_out, "%s",s);
+           _error->Errno("openpty", _("Can not write log (%s)"), _("Is /dev/pts mounted?"));
            master = slave = -1;
         }  else {
            struct termios rtt;
@@ -1227,6 +1287,15 @@ bool pkgDPkgPM::Go(int OutStatusFd)
            sigprocmask(SIG_SETMASK, &original_sigmask, 0);
         }
       }
+      // complain only if stdout is either a terminal (but still failed) or is an invalid
+      // descriptor otherwise we would complain about redirection to e.g. /dev/null as well.
+      else if (isatty(STDOUT_FILENO) == 1 || errno == EBADF)
+         _error->Errno("tcgetattr", _("Can not write log (%s)"), _("Is stdout a terminal?"));
+
+      if (_error->PendingError() == true)
+        _error->DumpErrors(std::cerr);
+      _error->RevertToStack();
+
        // Fork dpkg
       pid_t Child;
       _config->Set("APT::Keep-Fds::",fd[1]);
@@ -1240,7 +1309,7 @@ bool pkgDPkgPM::Go(int OutStatusFd)
         FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
       }
       Child = ExecFork();
-            
+      
       // This is the child
       if (Child == 0)
       {
@@ -1580,12 +1649,12 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg)
    if (!logfile_name.empty())
    {
       FILE *log = NULL;
-      char buf[1024];
 
       fprintf(report, "DpkgTerminalLog:\n");
       log = fopen(logfile_name.c_str(),"r");
       if(log != NULL)
       {
+        char buf[1024];
         while( fgets(buf, sizeof(buf), log) != NULL)
            fprintf(report, " %s", buf);
         fclose(log);
@@ -1596,18 +1665,19 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg)
    const char *ops_str[] = {"Install", "Configure","Remove","Purge"};
    fprintf(report, "AptOrdering:\n");
    for (vector<Item>::iterator I = List.begin(); I != List.end(); ++I)
-      fprintf(report, " %s: %s\n", (*I).Pkg.Name(), ops_str[(*I).Op]);
+      if ((*I).Pkg != NULL)
+         fprintf(report, " %s: %s\n", (*I).Pkg.Name(), ops_str[(*I).Op]);
+      else
+         fprintf(report, " %s: %s\n", "NULL", ops_str[(*I).Op]);
 
    // attach dmesg log (to learn about segfaults)
    if (FileExists("/bin/dmesg"))
    {
-      FILE *log = NULL;
-      char buf[1024];
-
       fprintf(report, "Dmesg:\n");
-      log = popen("/bin/dmesg","r");
+      FILE *log = popen("/bin/dmesg","r");
       if(log != NULL)
       {
+        char buf[1024];
         while( fgets(buf, sizeof(buf), log) != NULL)
            fprintf(report, " %s", buf);
         pclose(log);
@@ -1617,13 +1687,12 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg)
    // attach df -l log (to learn about filesystem status)
    if (FileExists("/bin/df"))
    {
-      FILE *log = NULL;
-      char buf[1024];
 
       fprintf(report, "Df:\n");
-      log = popen("/bin/df -l","r");
+      FILE *log = popen("/bin/df -l","r");
       if(log != NULL)
       {
+        char buf[1024];
         while( fgets(buf, sizeof(buf), log) != NULL)
            fprintf(report, " %s", buf);
         pclose(log);