Merge apt--authentication--0
[ntk/apt.git] / cmdline / apt-get.cc
index 75d3754..6dbb828 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: apt-get.cc,v 1.131 2003/07/22 01:49:26 mdz Exp $
+// $Id: apt-get.cc,v 1.156 2004/08/28 01:05:16 mdz Exp $
 /* ######################################################################
    
    apt-get - Cover for dpkg
@@ -46,6 +46,7 @@
 #include "acqprogress.h"
 
 #include <locale.h>
+#include <langinfo.h>
 #include <fstream>
 #include <termios.h>
 #include <sys/ioctl.h>
@@ -110,28 +111,39 @@ class CacheFile : public pkgCacheFile
 // YnPrompt - Yes No Prompt.                                           /*{{{*/
 // ---------------------------------------------------------------------
 /* Returns true on a Yes.*/
-bool YnPrompt()
+bool YnPrompt(bool Default=true)
 {
-   // This needs to be a capital
-   const char *Yes = _("Y");
-                          
    if (_config->FindB("APT::Get::Assume-Yes",false) == true)
    {
-      c1out << Yes << endl;
+      c1out << _("Y") << endl;
       return true;
    }
-   
-   char C = 0;
-   char Jnk = 0;
-   if (read(STDIN_FILENO,&C,1) != 1)
+
+   char response[1024] = "";
+   cin.getline(response, sizeof(response));
+
+   if (!cin)
       return false;
-   while (C != '\n' && Jnk != '\n') 
-      if (read(STDIN_FILENO,&Jnk,1) != 1)
-        return false;
+
+   if (strlen(response) == 0)
+      return Default;
+
+   regex_t Pattern;
+   int Res;
+
+   Res = regcomp(&Pattern, nl_langinfo(YESEXPR),
+                 REG_EXTENDED|REG_ICASE|REG_NOSUB);
+
+   if (Res != 0) {
+      char Error[300];        
+      regerror(Res,&Pattern,Error,sizeof(Error));
+      return _error->Error(_("Regex compilation error - %s"),Error);
+   }
    
-   if (!(toupper(C) == *Yes || C == '\n' || C == '\r'))
-      return false;
-   return true;
+   Res = regexec(&Pattern, response, 0, NULL, 0);
+   if (Res == 0)
+      return true;
+   return false;
 }
                                                                        /*}}}*/
 // AnalPrompt - Annoying Yes No Prompt.                                        /*{{{*/
@@ -258,7 +270,7 @@ void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
         // Compute a single dependency element (glob or)
         pkgCache::DepIterator Start;
         pkgCache::DepIterator End;
-        D.GlobOr(Start,End);
+        D.GlobOr(Start,End); // advances D
 
         if (Cache->IsImportantDep(End) == false)
            continue;
@@ -345,7 +357,7 @@ void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
 /* */
 void ShowNew(ostream &out,CacheFile &Cache)
 {
-   /* Print out a list of packages that are going to be removed extra
+   /* Print out a list of packages that are going to be installed extra
       to what the user asked */
    string List;
    string VersionsList;
@@ -406,7 +418,7 @@ void ShowKept(ostream &out,CacheFile &Cache)
       List += string(I.Name()) + " ";
       VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
    }
-   ShowList(out,_("The following packages have been kept back"),List,VersionsList);
+   ShowList(out,_("The following packages have been kept back:"),List,VersionsList);
 }
                                                                        /*}}}*/
 // ShowUpgraded - Show upgraded packages                               /*{{{*/
@@ -427,7 +439,7 @@ void ShowUpgraded(ostream &out,CacheFile &Cache)
       List += string(I.Name()) + " ";
       VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
    }
-   ShowList(out,_("The following packages will be upgraded"),List,VersionsList);
+   ShowList(out,_("The following packages will be upgraded:"),List,VersionsList);
 }
                                                                        /*}}}*/
 // ShowDowngraded - Show downgraded packages                           /*{{{*/
@@ -448,7 +460,7 @@ bool ShowDowngraded(ostream &out,CacheFile &Cache)
       List += string(I.Name()) + " ";
       VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
    }
-   return ShowList(out,_("The following packages will be DOWNGRADED"),List,VersionsList);
+   return ShowList(out,_("The following packages will be DOWNGRADED:"),List,VersionsList);
 }
                                                                        /*}}}*/
 // ShowHold - Show held but changed packages                           /*{{{*/
@@ -532,6 +544,7 @@ bool ShowEssential(ostream &out,CacheFile &Cache)
    return ShowList(out,_("WARNING: The following essential packages will be removed\n"
                         "This should NOT be done unless you know exactly what you are doing!"),List,VersionsList);
 }
+
                                                                        /*}}}*/
 // Stats - Show some statistics                                                /*{{{*/
 // ---------------------------------------------------------------------
@@ -559,7 +572,7 @@ void Stats(ostream &out,pkgDepCache &Dep)
         ReInstall++;
    }   
 
-   ioprintf(out,_("%lu packages upgraded, %lu newly installed, "),
+   ioprintf(out,_("%lu upgraded, %lu newly installed, "),
            Upgrade,Install);
    
    if (ReInstall != 0)
@@ -571,7 +584,7 @@ void Stats(ostream &out,pkgDepCache &Dep)
            Dep.DelCount(),Dep.KeepCount());
    
    if (Dep.BadCount() != 0)
-      ioprintf(out,_("%lu packages not fully installed or removed.\n"),
+      ioprintf(out,_("%lu not fully installed or removed.\n"),
               Dep.BadCount());
 }
                                                                        /*}}}*/
@@ -654,6 +667,49 @@ bool CacheFile::CheckDeps(bool AllowBroken)
       
    return true;
 }
+
+static bool CheckAuth(pkgAcquire& Fetcher)
+{
+   string UntrustedList;
+   for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
+   {
+      if (!(*I)->IsTrusted())
+      {
+         UntrustedList += string((*I)->ShortDesc()) + " ";
+      }
+   }
+
+   if (UntrustedList == "")
+   {
+      return true;
+   }
+        
+   ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"),UntrustedList,"");
+
+   if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
+   {
+      c2out << "Authentication warning overridden.\n";
+      return true;
+   }
+
+   if (_config->FindI("quiet",0) < 2
+       && _config->FindB("APT::Get::Assume-Yes",false) == false)
+   {
+      c2out << _("Install these packages without verification? [y/N] ") << flush;
+      if (!YnPrompt(false))
+         return _error->Error(_("Some packages could not be authenticated"));
+
+      return true;
+   }
+   else if (_config->FindB("APT::Get::Force-Yes",false) == true)
+   {
+      return true;
+   }
+
+   return _error->Error(_("There are problems and -y was used without --force-yes"));
+}
+
+
                                                                        /*}}}*/
 
 // InstallPackages - Actually download and install the packages                /*{{{*/
@@ -661,7 +717,7 @@ bool CacheFile::CheckDeps(bool AllowBroken)
 /* This displays the informative messages describing what is going to 
    happen and then calls the download routines */
 bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
-                    bool Saftey = true)
+                    bool Safety = true)
 {
    if (_config->FindB("APT::Get::Purge",false) == true)
    {
@@ -682,14 +738,14 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
    if (ShwKept == true)
       ShowKept(c1out,Cache);
    Fail |= !ShowHold(c1out,Cache);
-   if (_config->FindB("APT::Get::Show-Upgraded",false) == true)
+   if (_config->FindB("APT::Get::Show-Upgraded",true) == true)
       ShowUpgraded(c1out,Cache);
    Fail |= !ShowDowngraded(c1out,Cache);
    if (_config->FindB("APT::Get::Download-Only",false) == false)
         Essential = !ShowEssential(c1out,Cache);
    Fail |= Essential;
    Stats(c1out,Cache);
-   
+
    // Sanity check
    if (Cache->BrokenCount() != 0)
    {
@@ -799,7 +855,7 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
         return _error->Error(_("There are problems and -y was used without --force-yes"));
    }         
 
-   if (Essential == true && Saftey == true)
+   if (Essential == true && Safety == true)
    {
       if (_config->FindB("APT::Get::Trivial-Only",false) == true)
         return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
@@ -848,6 +904,9 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
       return true;
    }
 
+   if (!CheckAuth(Fetcher))
+      return false;
+
    /* Unlock the dpkg lock if we are not going to be doing an install
       after. */
    if (_config->FindB("APT::Get::Download-Only",false) == true)
@@ -1025,10 +1084,9 @@ bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
       else
       {
         ioprintf(c1out,
-        _("Package %s has no available version, but exists in the database.\n"
-          "This typically means that the package was mentioned in a dependency and\n"
-          "never uploaded, has been obsoleted or is not available with the contents\n"
-          "of sources.list\n"),Pkg.Name());
+        _("Package %s is not available, but is referred to by another package.\n"
+          "This may mean that the package is missing, has been obsoleted, or\n"
+           "is only available from another source\n"),Pkg.Name());
         
         string List;
         string VersionsList;
@@ -1241,19 +1299,25 @@ bool DoUpdate(CommandLine &CmdL)
    AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
    pkgAcquire Fetcher(&Stat);
 
-   // Populate it with the source selection
-   if (List.GetIndexes(&Fetcher) == false)
-        return false;
    
    // Just print out the uris an exit if the --print-uris flag was used
    if (_config->FindB("APT::Get::Print-URIs") == true)
    {
+      // Populate it with the source selection and get all Indexes 
+      // (GetAll=true)
+      if (List.GetIndexes(&Fetcher,true) == false)
+        return false;
+
       pkgAcquire::UriIterator I = Fetcher.UriBegin();
       for (; I != Fetcher.UriEnd(); I++)
         cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' << 
               I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
       return true;
    }
+
+   // Populate it with the source selection
+   if (List.GetIndexes(&Fetcher) == false)
+        return false;
    
    // Run it
    if (Fetcher.Run() == pkgAcquire::Failed)
@@ -1350,14 +1414,14 @@ bool DoInstall(CommandLine &CmdL)
       while (Cache->FindPkg(S).end() == true)
       {
         // Handle an optional end tag indicating what to do
-        if (S[Length - 1] == '-')
+        if (Length >= 1 && S[Length - 1] == '-')
         {
            Remove = true;
            S[--Length] = 0;
            continue;
         }
         
-        if (S[Length - 1] == '+')
+        if (Length >= 1 && S[Length - 1] == '+')
         {
            Remove = false;
            S[--Length] = 0;
@@ -1521,53 +1585,60 @@ bool DoInstall(CommandLine &CmdL)
           continue;
 
         for (pkgCache::VerIterator V = I.VersionList(); V.end() == false; V++)
-          {
-            for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; D++)
-              {
+         {
+            for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; )
+             {
                 pkgCache::DepIterator Start;
                 pkgCache::DepIterator End;
-                D.GlobOr(Start,End);
-                do
-                  {
-                    if (Start->Type == pkgCache::Dep::Suggests) {
+                D.GlobOr(Start,End); // advances D
+
+                /* 
+                 * If this is a virtual package, we need to check the list of
+                 * packages that provide it and see if any of those are
+                 * installed
+                 */
+                
+                bool providedBySomething = false;
+                for (pkgCache::PrvIterator Prv = Start.TargetPkg().ProvidesList();
+                      Prv.end() != true;
+                      Prv++)
+                   if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
+                    {
+                      providedBySomething = true;
+                      break;
+                   }
+
+                if (providedBySomething) continue;
+            
+                 for(;;)
+                 {
+                     /* Skip if package is  installed already, or is about to be */
+                     string target = string(Start.TargetPkg().Name()) + " ";
 
-                      /* A suggests relations, let's see if we have it 
-                         installed already */
+                     if ((*Start.TargetPkg()).SelectedState == pkgCache::State::Install
+                         || Cache[Start.TargetPkg()].Install())
+                       break;
 
-                      string target = string(Start.TargetPkg().Name()) + " ";
-                      if ((*Start.TargetPkg()).SelectedState == pkgCache::State::Install)
-                        break;
-                      /* Does another package suggest it as well?  If so,
-                         don't print it twice */
-                      if (int(SuggestsList.find(target)) > -1)
-                        break; 
+                     /* Skip if we already saw it */
+                     if (int(SuggestsList.find(target)) != -1 || int(RecommendsList.find(target)) != -1)
+                       break; 
+
+                    if (Start->Type == pkgCache::Dep::Suggests) {
                       SuggestsList += target;
                       SuggestsVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
                     }
                     
                     if (Start->Type == pkgCache::Dep::Recommends) {
-
-                      /* A recommends relation, let's see if we have it
-                         installed already */
-
-                      string target = string(Start.TargetPkg().Name()) + " ";
-                      if ((*Start.TargetPkg()).SelectedState == pkgCache::State::Install)
-                        break;
-                      
-                      /* Does another package recommend it as well?  If so,
-                         don't print it twice */
-
-                      if (int(RecommendsList.find(target)) > -1)
-                        break;
                       RecommendsList += target;
-                      SuggestsVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
+                      RecommendsVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
                     }
-             if (Start == End)
-               break;
-             Start++;
-           } while (1);
-              }
-          }
+
+                     if (Start >= End)
+                        break;
+                     Start++;
+                 }
+             }
+         }
       }
       ShowList(c1out,_("Suggested packages:"),SuggestsList,SuggestsVersions);
       ShowList(c1out,_("Recommended packages:"),RecommendsList,RecommendsVersions);
@@ -2025,7 +2096,7 @@ bool DoBuildDep(CommandLine &CmdL)
         rec.Package = Opts->Value;
         rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
         rec.Op = 0;
-        BuildDeps.insert(BuildDeps.begin(), rec);
+        BuildDeps.push_back(rec);
       }
 
       if (BuildDeps.size() == 0)
@@ -2038,8 +2109,18 @@ bool DoBuildDep(CommandLine &CmdL)
       unsigned int ExpectedInst = 0;
       vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
       pkgProblemResolver Fix(Cache);
+      bool skipAlternatives = false; // skip remaining alternatives in an or group
       for (D = BuildDeps.begin(); D != BuildDeps.end(); D++)
       {
+         bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
+
+         if (skipAlternatives == true)
+         {
+            if (!hasAlternatives)
+               skipAlternatives = false; // end of or group
+            continue;
+         }
+
          if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
             (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
          {
@@ -2061,16 +2142,21 @@ bool DoBuildDep(CommandLine &CmdL)
         else // BuildDep || BuildDepIndep
          {
            pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
+            if (_config->FindB("Debug::BuildDeps",false) == true)
+                 cout << "Looking for " << (*D).Package << "...\n";
+
            if (Pkg.end() == true)
             {
-               // Check if there are any alternatives
-               if (((*D).Op & pkgCache::Dep::Or) != pkgCache::Dep::Or)
-                 return _error->Error(_("%s dependency for %s cannot be satisfied "
-                                         "because the package %s cannot be found"),
-                                        Last->BuildDepType((*D).Type),Src.c_str(),
-                                         (*D).Package.c_str());
-               // Try the next alternative
-               continue;
+               if (_config->FindB("Debug::BuildDeps",false) == true)
+                    cout << " (not found)" << (*D).Package << endl;
+
+               if (hasAlternatives)
+                  continue;
+
+               return _error->Error(_("%s dependency for %s cannot be satisfied "
+                                      "because the package %s cannot be found"),
+                                    Last->BuildDepType((*D).Type),Src.c_str(),
+                                    (*D).Package.c_str());
             }
 
             /*
@@ -2083,9 +2169,6 @@ bool DoBuildDep(CommandLine &CmdL)
              * this would require we do a Resolve cycle for each package we 
              * add to the install list. Ugh
              */
-            while (D != BuildDeps.end() && 
-                   (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or))
-               D++;
                        
            /* 
             * If this is a virtual package, we need to check the list of
@@ -2094,37 +2177,99 @@ bool DoBuildDep(CommandLine &CmdL)
             */
             pkgCache::PrvIterator Prv = Pkg.ProvidesList();
             for (; Prv.end() != true; Prv++)
+           {
+               if (_config->FindB("Debug::BuildDeps",false) == true)
+                    cout << "  Checking provider " << Prv.OwnerPkg().Name() << endl;
+
               if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
                  break;
+            }
             
             // Get installed version and version we are going to install
            pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
-           pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
 
-            for (; CV.end() != true; CV++)
+            if ((*D).Version[0] != '\0') {
+                 // Versioned dependency
+
+                 pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
+
+                 for (; CV.end() != true; CV++)
+                 {
+                      if (Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
+                           break;
+                 }
+                 if (CV.end() == true)
+                  if (hasAlternatives)
+                  {
+                     continue;
+                  }
+                  else
+                  {
+                      return _error->Error(_("%s dependency for %s cannot be satisfied "
+                                             "because no available versions of package %s "
+                                             "can satisfy version requirements"),
+                                           Last->BuildDepType((*D).Type),Src.c_str(),
+                                           (*D).Package.c_str());
+                  }
+            }
+            else
             {
-               if (Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
-                  break;
+               // Only consider virtual packages if there is no versioned dependency
+               if (Prv.end() == false)
+               {
+                  if (_config->FindB("Debug::BuildDeps",false) == true)
+                     cout << "  Is provided by installed package " << Prv.OwnerPkg().Name() << endl;
+                  skipAlternatives = hasAlternatives;
+                  continue;
+               }
             }
-            if (CV.end() == true)
-              return _error->Error(_("%s dependency for %s cannot be satisfied "
-                                      "because no available versions of package %s "
-                                      "can satisfy version requirements"),
-                                     Last->BuildDepType((*D).Type),Src.c_str(),
-                                      (*D).Package.c_str());
 
-            /*
-            * TODO: if we depend on a version lower than what we already have 
-            * installed it is not clear what should be done; in practice
-            * this case should be rare, and right now nothing is 
-             * done about it :-( 
-            */
-           if (Prv.end() == true && // Nothing provides it; and
-                (IV.end() == true || //  It is not installed, or
-                Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == false))
-                                     //  the version installed doesn't 
-                                     //  satisfy constraints 
-              TryToInstall(Pkg,Cache,Fix,false,false,ExpectedInst);
+            if (IV.end() == false)
+            {
+               if (_config->FindB("Debug::BuildDeps",false) == true)
+                  cout << "  Is installed\n";
+
+               if (Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
+               {
+                  skipAlternatives = hasAlternatives;
+                  continue;
+               }
+
+               if (_config->FindB("Debug::BuildDeps",false) == true)
+                  cout << "    ...but the installed version doesn't meet the version requirement\n";
+
+               if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
+               {
+                  return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
+                                       Last->BuildDepType((*D).Type),
+                                       Src.c_str(),
+                                       Pkg.Name());
+               }
+            }
+
+
+            if (_config->FindB("Debug::BuildDeps",false) == true)
+               cout << "  Trying to install " << (*D).Package << endl;
+
+            if (TryToInstall(Pkg,Cache,Fix,false,false,ExpectedInst) == true)
+            {
+               // We successfully installed something; skip remaining alternatives
+               skipAlternatives = hasAlternatives;
+               continue;
+            }
+            else if (hasAlternatives)
+            {
+               if (_config->FindB("Debug::BuildDeps",false) == true)
+                  cout << "  Unsatisfiable, trying alternatives\n";
+               continue;
+            }
+            else
+            {
+               return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
+                                    Last->BuildDepType((*D).Type),
+                                    Src.c_str(),
+                                    (*D).Package.c_str());
+            }
         }             
       }
       
@@ -2134,8 +2279,7 @@ bool DoBuildDep(CommandLine &CmdL)
       
       // Now we check the state of the packages,
       if (Cache->BrokenCount() != 0)
-        return _error->Error(_("Some broken packages were found while trying to process build-dependencies.\n"
-                               "You might want to run `apt-get -f install' to correct these."));
+         return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
    }
   
    if (InstallPackages(Cache, false, true) == false)
@@ -2265,7 +2409,7 @@ void GetInitialize()
    _config->Set("APT::Get::Assume-Yes",false);
    _config->Set("APT::Get::Fix-Broken",false);
    _config->Set("APT::Get::Force-Yes",false);
-   _config->Set("APT::Get::APT::Get::No-List-Cleanup",true);
+   _config->Set("APT::Get::List-Cleanup",true);
 }
                                                                        /*}}}*/
 // SigWinch - Window size change signal handler                                /*{{{*/
@@ -2321,6 +2465,7 @@ int main(int argc,const char *argv[])
       {0,"remove","APT::Get::Remove",0},
       {0,"only-source","APT::Get::Only-Source",0},
       {0,"arch-only","APT::Get::Arch-Only",0},
+      {0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0},
       {'c',"config-file",0,CommandLine::ConfigFile},
       {'o',"option",0,CommandLine::ArbItem},
       {0,0,0,0}};
@@ -2366,7 +2511,7 @@ int main(int argc,const char *argv[])
    }
    
    // Deal with stdout not being a tty
-   if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
+   if (!isatty(STDOUT_FILENO) && _config->FindI("quiet",0) < 1)
       _config->Set("quiet","1");
 
    // Setup the output streams