private-download.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/acquire.h>
  4. #include <apt-pkg/acquire-item.h>
  5. #include <apt-pkg/configuration.h>
  6. #include <apt-pkg/error.h>
  7. #include <apt-pkg/fileutl.h>
  8. #include <apt-pkg/strutl.h>
  9. #include <apt-private/private-output.h>
  10. #include <apt-private/private-download.h>
  11. #include <fstream>
  12. #include <string>
  13. #include <vector>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <pwd.h>
  17. #include <fcntl.h>
  18. #include <sys/vfs.h>
  19. #include <sys/statvfs.h>
  20. #include <errno.h>
  21. #include <apti18n.h>
  22. /*}}}*/
  23. bool CheckDropPrivsMustBeDisabled(pkgAcquire &Fetcher) /*{{{*/
  24. {
  25. // no need/possibility to drop privs
  26. if(getuid() != 0)
  27. return true;
  28. // the user does not want to drop privs
  29. std::string SandboxUser = _config->Find("APT::Sandbox::User");
  30. if (SandboxUser.empty())
  31. return true;
  32. struct passwd const * const pw = getpwnam(SandboxUser.c_str());
  33. if (pw == NULL)
  34. return true;
  35. if (seteuid(pw->pw_uid) != 0)
  36. return _error->Errno("seteuid", "seteuid %u failed", pw->pw_uid);
  37. bool res = true;
  38. // check if we can write to destfile
  39. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  40. I != Fetcher.ItemsEnd() && res == true; ++I)
  41. {
  42. if ((*I)->DestFile.empty())
  43. continue;
  44. // we assume that an existing (partial) file means that we have sufficient rights
  45. if (RealFileExists((*I)->DestFile))
  46. continue;
  47. int fd = open((*I)->DestFile.c_str(), O_CREAT | O_EXCL | O_RDWR, 0600);
  48. if (fd < 0)
  49. {
  50. res = false;
  51. std::string msg;
  52. strprintf(msg, _("Can't drop privileges for downloading as file '%s' couldn't be accessed by user '%s'."),
  53. (*I)->DestFile.c_str(), SandboxUser.c_str());
  54. std::cerr << "W: " << msg << std::endl;
  55. _config->Set("APT::Sandbox::User", "");
  56. break;
  57. }
  58. unlink((*I)->DestFile.c_str());
  59. close(fd);
  60. }
  61. if (seteuid(0) != 0)
  62. return _error->Errno("seteuid", "seteuid %u failed", 0);
  63. return res;
  64. }
  65. /*}}}*/
  66. // CheckAuth - check if each download comes form a trusted source /*{{{*/
  67. bool CheckAuth(pkgAcquire& Fetcher, bool const PromptUser)
  68. {
  69. std::string UntrustedList;
  70. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
  71. if (!(*I)->IsTrusted())
  72. UntrustedList += std::string((*I)->ShortDesc()) + " ";
  73. if (UntrustedList == "")
  74. return true;
  75. return AuthPrompt(UntrustedList, PromptUser);
  76. }
  77. bool AuthPrompt(std::string const &UntrustedList, bool const PromptUser)
  78. {
  79. ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"),UntrustedList,"");
  80. if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
  81. {
  82. c2out << _("Authentication warning overridden.\n");
  83. return true;
  84. }
  85. if (PromptUser == false)
  86. return _error->Error(_("Some packages could not be authenticated"));
  87. if (_config->FindI("quiet",0) < 2
  88. && _config->FindB("APT::Get::Assume-Yes",false) == false)
  89. {
  90. c2out << _("Install these packages without verification?") << std::flush;
  91. if (!YnPrompt(false))
  92. return _error->Error(_("Some packages could not be authenticated"));
  93. return true;
  94. }
  95. else if (_config->FindB("APT::Get::Force-Yes",false) == true)
  96. return true;
  97. return _error->Error(_("There are problems and -y was used without --force-yes"));
  98. }
  99. /*}}}*/
  100. bool AcquireRun(pkgAcquire &Fetcher, int const PulseInterval, bool * const Failure, bool * const TransientNetworkFailure)/*{{{*/
  101. {
  102. pkgAcquire::RunResult res;
  103. if(PulseInterval > 0)
  104. res = Fetcher.Run(PulseInterval);
  105. else
  106. res = Fetcher.Run();
  107. if (res == pkgAcquire::Failed)
  108. return false;
  109. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  110. I != Fetcher.ItemsEnd(); ++I)
  111. {
  112. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  113. (*I)->Complete == true)
  114. continue;
  115. if (TransientNetworkFailure != NULL && (*I)->Status == pkgAcquire::Item::StatIdle)
  116. {
  117. *TransientNetworkFailure = true;
  118. continue;
  119. }
  120. ::URI uri((*I)->DescURI());
  121. uri.User.clear();
  122. uri.Password.clear();
  123. std::string descUri = std::string(uri);
  124. _error->Error(_("Failed to fetch %s %s\n"), descUri.c_str(),
  125. (*I)->ErrorText.c_str());
  126. if (Failure != NULL)
  127. *Failure = true;
  128. }
  129. return true;
  130. }
  131. /*}}}*/
  132. bool CheckFreeSpaceBeforeDownload(std::string const &Dir, unsigned long long FetchBytes)/*{{{*/
  133. {
  134. uint32_t const RAMFS_MAGIC = 0x858458f6;
  135. /* Check for enough free space, but only if we are actually going to
  136. download */
  137. if (_config->FindB("APT::Get::Print-URIs", false) == true ||
  138. _config->FindB("APT::Get::Download", true) == false)
  139. return true;
  140. struct statvfs Buf;
  141. if (statvfs(Dir.c_str(),&Buf) != 0) {
  142. if (errno == EOVERFLOW)
  143. return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
  144. Dir.c_str());
  145. else
  146. return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
  147. Dir.c_str());
  148. }
  149. else
  150. {
  151. unsigned long long const FreeBlocks = _config->Find("APT::Sandbox::User").empty() ? Buf.f_bfree : Buf.f_bavail;
  152. if (FreeBlocks < (FetchBytes / Buf.f_bsize))
  153. {
  154. struct statfs Stat;
  155. if (statfs(Dir.c_str(),&Stat) != 0
  156. #if HAVE_STRUCT_STATFS_F_TYPE
  157. || Stat.f_type != RAMFS_MAGIC
  158. #endif
  159. )
  160. return _error->Error(_("You don't have enough free space in %s."),
  161. Dir.c_str());
  162. }
  163. }
  164. return true;
  165. }
  166. /*}}}*/