X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/21248c0f00ee71412dbadc6ebf84011cf974346d..831ea74cddf8568b8648edddb394fdd3d1ba7b8e:/apt-private/private-download.cc diff --git a/apt-private/private-download.cc b/apt-private/private-download.cc index 37fae18e9..6f672635f 100644 --- a/apt-private/private-download.cc +++ b/apt-private/private-download.cc @@ -3,13 +3,19 @@ #include #include +#include +#include +#include #include #include #include #include -#include +#include #include +#include +#include +#include #include #include @@ -21,77 +27,32 @@ #include #include #include +#include #include #include /*}}}*/ -bool CheckDropPrivsMustBeDisabled(pkgAcquire &Fetcher) /*{{{*/ -{ - // no need/possibility to drop privs - if(getuid() != 0) - return true; - - // the user does not want to drop privs - std::string SandboxUser = _config->Find("APT::Sandbox::User"); - if (SandboxUser.empty()) - return true; - - struct passwd const * const pw = getpwnam(SandboxUser.c_str()); - if (pw == NULL) - return true; - - if (seteuid(pw->pw_uid) != 0) - return _error->Errno("seteuid", "seteuid %u failed", pw->pw_uid); - - bool res = true; - // check if we can write to destfile - for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); - I != Fetcher.ItemsEnd() && res == true; ++I) - { - if ((*I)->DestFile.empty()) - continue; - // we assume that an existing (partial) file means that we have sufficient rights - if (RealFileExists((*I)->DestFile)) - continue; - int fd = open((*I)->DestFile.c_str(), O_CREAT | O_EXCL | O_RDWR, 0600); - if (fd < 0) - { - res = false; - std::string msg; - strprintf(msg, _("Can't drop privileges for downloading as file '%s' couldn't be accessed by user '%s'."), - (*I)->DestFile.c_str(), SandboxUser.c_str()); - std::cerr << "W: " << msg << std::endl; - _config->Set("APT::Sandbox::User", ""); - break; - } - unlink((*I)->DestFile.c_str()); - close(fd); - } - - if (seteuid(0) != 0) - return _error->Errno("seteuid", "seteuid %u failed", 0); - - return res; -} - /*}}}*/ // CheckAuth - check if each download comes form a trusted source /*{{{*/ bool CheckAuth(pkgAcquire& Fetcher, bool const PromptUser) { - std::string UntrustedList; + std::vector UntrustedList; for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I) if (!(*I)->IsTrusted()) - UntrustedList += std::string((*I)->ShortDesc()) + " "; + UntrustedList.push_back((*I)->ShortDesc()); - if (UntrustedList == "") + if (UntrustedList.empty()) return true; return AuthPrompt(UntrustedList, PromptUser); } - -bool AuthPrompt(std::string const &UntrustedList, bool const PromptUser) + /*}}}*/ +bool AuthPrompt(std::vector const &UntrustedList, bool const PromptUser)/*{{{*/ { - ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"),UntrustedList,""); + ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"), UntrustedList, + [](std::string const&) { return true; }, + [](std::string const&str) { return str; }, + [](std::string const&) { return ""; }); if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true) { @@ -111,10 +72,12 @@ bool AuthPrompt(std::string const &UntrustedList, bool const PromptUser) return true; } - else if (_config->FindB("APT::Get::Force-Yes",false) == true) + else if (_config->FindB("APT::Get::Force-Yes",false) == true) { + _error->Warning(_("--force-yes is deprecated, use one of the options starting with --allow instead.")); return true; + } - return _error->Error(_("There are problems and -y was used without --force-yes")); + return _error->Error(_("There were unauthenticated packages and -y was used without --allow-unauthenticated")); } /*}}}*/ bool AcquireRun(pkgAcquire &Fetcher, int const PulseInterval, bool * const Failure, bool * const TransientNetworkFailure)/*{{{*/ @@ -192,3 +155,216 @@ bool CheckFreeSpaceBeforeDownload(std::string const &Dir, unsigned long long Fet return true; } /*}}}*/ + +aptAcquireWithTextStatus::aptAcquireWithTextStatus() : pkgAcquire::pkgAcquire(), + Stat(std::cout, ScreenWidth, _config->FindI("quiet",0)) +{ + SetLog(&Stat); +} + +// DoDownload - download a binary /*{{{*/ +bool DoDownload(CommandLine &CmdL) +{ + CacheFile Cache; + if (Cache.ReadOnlyOpen() == false) + return false; + + APT::CacheSetHelper helper; + APT::VersionSet verset = APT::VersionSet::FromCommandLine(Cache, + CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper); + + if (verset.empty() == true) + return false; + + pkgRecords Recs(Cache); + pkgSourceList *SrcList = Cache.GetSourceList(); + + // reuse the usual acquire methods for deb files, but don't drop them into + // the usual directories - keep everything in the current directory + aptAcquireWithTextStatus Fetcher; + std::vector storefile(verset.size()); + std::string const cwd = SafeGetCWD(); + _config->Set("Dir::Cache::Archives", cwd); + int i = 0; + for (APT::VersionSet::const_iterator Ver = verset.begin(); + Ver != verset.end(); ++Ver, ++i) + { + pkgAcquire::Item *I = new pkgAcqArchive(&Fetcher, SrcList, &Recs, *Ver, storefile[i]); + if (storefile[i].empty()) + continue; + std::string const filename = cwd + flNotDir(storefile[i]); + storefile[i].assign(filename); + I->DestFile.assign(filename); + } + + // Just print out the uris and exit if the --print-uris flag was used + if (_config->FindB("APT::Get::Print-URIs") == true) + { + pkgAcquire::UriIterator I = Fetcher.UriBegin(); + for (; I != Fetcher.UriEnd(); ++I) + std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' << + I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl; + return true; + } + + if (_error->PendingError() == true || CheckAuth(Fetcher, false) == false) + return false; + + bool Failed = false; + if (AcquireRun(Fetcher, 0, &Failed, NULL) == false) + return false; + + // copy files in local sources to the current directory + for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I) + { + std::string const filename = cwd + flNotDir((*I)->DestFile); + if ((*I)->Local == true && + filename != (*I)->DestFile && + (*I)->Status == pkgAcquire::Item::StatDone) + { + std::ifstream src((*I)->DestFile.c_str(), std::ios::binary); + std::ofstream dst(filename.c_str(), std::ios::binary); + dst << src.rdbuf(); + chmod(filename.c_str(), 0644); + } + } + return Failed == false; +} + /*}}}*/ +// DoChangelog - Get changelog from the command line /*{{{*/ +bool DoChangelog(CommandLine &CmdL) +{ + CacheFile Cache; + if (Cache.ReadOnlyOpen() == false) + return false; + + APT::CacheSetHelper helper; + APT::VersionList verset = APT::VersionList::FromCommandLine(Cache, + CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper); + if (verset.empty() == true) + return false; + + bool const downOnly = _config->FindB("APT::Get::Download-Only", false); + bool const printOnly = _config->FindB("APT::Get::Print-URIs", false); + + aptAcquireWithTextStatus Fetcher; + for (APT::VersionList::const_iterator Ver = verset.begin(); + Ver != verset.end(); + ++Ver) + { + if (printOnly) + new pkgAcqChangelog(&Fetcher, Ver, "/dev/null"); + else if (downOnly) + new pkgAcqChangelog(&Fetcher, Ver, "."); + else + new pkgAcqChangelog(&Fetcher, Ver); + } + + if (printOnly == false) + { + bool Failed = false; + if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true) + return false; + } + + if (downOnly == false || printOnly == true) + { + bool Failed = false; + for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I) + { + if (printOnly) + { + if ((*I)->ErrorText.empty() == false) + { + Failed = true; + _error->Error("%s", (*I)->ErrorText.c_str()); + } + else + std::cout << '\'' << (*I)->DescURI() << "' " << flNotDir((*I)->DestFile) << std::endl; + } + else + DisplayFileInPager((*I)->DestFile); + } + return Failed == false; + } + + return true; +} + /*}}}*/ + +// DoClean - Remove download archives /*{{{*/ +bool DoClean(CommandLine &) +{ + std::string const archivedir = _config->FindDir("Dir::Cache::archives"); + std::string const listsdir = _config->FindDir("Dir::state::lists"); + + if (_config->FindB("APT::Get::Simulate") == true) + { + std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache"); + std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache"); + std::cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< std::endl + << "Del " << listsdir << "partial/*" << std::endl + << "Del " << pkgcache << " " << srcpkgcache << std::endl; + return true; + } + + pkgAcquire Fetcher; + if (archivedir.empty() == false && FileExists(archivedir) == true && + Fetcher.GetLock(archivedir) == true) + { + Fetcher.Clean(archivedir); + Fetcher.Clean(archivedir + "partial/"); + } + + if (listsdir.empty() == false && FileExists(listsdir) == true && + Fetcher.GetLock(listsdir) == true) + { + Fetcher.Clean(listsdir + "partial/"); + } + + pkgCacheFile::RemoveCaches(); + + return true; +} + /*}}}*/ +// DoAutoClean - Smartly remove downloaded archives /*{{{*/ +// --------------------------------------------------------------------- +/* This is similar to clean but it only purges things that cannot be + downloaded, that is old versions of cached packages. */ + class LogCleaner : public pkgArchiveCleaner +{ + protected: + virtual void Erase(const char *File, std::string Pkg, std::string Ver,struct stat &St) APT_OVERRIDE + { + c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << std::endl; + + if (_config->FindB("APT::Get::Simulate") == false) + RemoveFile("Cleaner::Erase", File); + }; +}; +bool DoAutoClean(CommandLine &) +{ + std::string const archivedir = _config->FindDir("Dir::Cache::Archives"); + if (FileExists(archivedir) == false) + return true; + + // Lock the archive directory + FileFd Lock; + if (_config->FindB("Debug::NoLocking",false) == false) + { + int lock_fd = GetLock(flCombine(archivedir, "lock")); + if (lock_fd < 0) + return _error->Error(_("Unable to lock the download directory")); + Lock.Fd(lock_fd); + } + + CacheFile Cache; + if (Cache.Open() == false) + return false; + + LogCleaner Cleaner; + + return Cleaner.Go(archivedir, *Cache) && + Cleaner.Go(flCombine(archivedir, "partial/"), *Cache); +} + /*}}}*/