]>
git.saurik.com Git - apt.git/blob - apt-private/private-download.cc
1 // Include Files /*{{{*/
4 #include <apt-pkg/acquire.h>
5 #include <apt-pkg/acquire-item.h>
6 #include <apt-pkg/configuration.h>
7 #include <apt-pkg/error.h>
8 #include <apt-pkg/fileutl.h>
9 #include <apt-pkg/strutl.h>
11 #include <apt-private/private-output.h>
12 #include <apt-private/private-download.h>
19 #include <sys/types.h>
23 #include <sys/statvfs.h>
29 bool CheckDropPrivsMustBeDisabled(pkgAcquire
&Fetcher
) /*{{{*/
31 // no need/possibility to drop privs
35 // the user does not want to drop privs
36 std::string SandboxUser
= _config
->Find("APT::Sandbox::User");
37 if (SandboxUser
.empty())
40 struct passwd
const * const pw
= getpwnam(SandboxUser
.c_str());
44 if (seteuid(pw
->pw_uid
) != 0)
45 return _error
->Errno("seteuid", "seteuid %u failed", pw
->pw_uid
);
48 // check if we can write to destfile
49 for (pkgAcquire::ItemIterator I
= Fetcher
.ItemsBegin();
50 I
!= Fetcher
.ItemsEnd() && res
== true; ++I
)
52 if ((*I
)->DestFile
.empty())
54 // we assume that an existing (partial) file means that we have sufficient rights
55 if (RealFileExists((*I
)->DestFile
))
57 int fd
= open((*I
)->DestFile
.c_str(), O_CREAT
| O_EXCL
| O_RDWR
, 0600);
62 strprintf(msg
, _("Can't drop privileges for downloading as file '%s' couldn't be accessed by user '%s'."),
63 (*I
)->DestFile
.c_str(), SandboxUser
.c_str());
64 std::cerr
<< "W: " << msg
<< std::endl
;
65 _config
->Set("APT::Sandbox::User", "");
68 unlink((*I
)->DestFile
.c_str());
73 return _error
->Errno("seteuid", "seteuid %u failed", 0);
78 // CheckAuth - check if each download comes form a trusted source /*{{{*/
79 bool CheckAuth(pkgAcquire
& Fetcher
, bool const PromptUser
)
81 std::string UntrustedList
;
82 for (pkgAcquire::ItemIterator I
= Fetcher
.ItemsBegin(); I
< Fetcher
.ItemsEnd(); ++I
)
83 if (!(*I
)->IsTrusted())
84 UntrustedList
+= std::string((*I
)->ShortDesc()) + " ";
86 if (UntrustedList
== "")
89 return AuthPrompt(UntrustedList
, PromptUser
);
92 bool AuthPrompt(std::string
const &UntrustedList
, bool const PromptUser
)
94 ShowList(c2out
,_("WARNING: The following packages cannot be authenticated!"),UntrustedList
,"");
96 if (_config
->FindB("APT::Get::AllowUnauthenticated",false) == true)
98 c2out
<< _("Authentication warning overridden.\n");
102 if (PromptUser
== false)
103 return _error
->Error(_("Some packages could not be authenticated"));
105 if (_config
->FindI("quiet",0) < 2
106 && _config
->FindB("APT::Get::Assume-Yes",false) == false)
108 c2out
<< _("Install these packages without verification?") << std::flush
;
109 if (!YnPrompt(false))
110 return _error
->Error(_("Some packages could not be authenticated"));
114 else if (_config
->FindB("APT::Get::Force-Yes",false) == true)
117 return _error
->Error(_("There are problems and -y was used without --force-yes"));
120 bool AcquireRun(pkgAcquire
&Fetcher
, int const PulseInterval
, bool * const Failure
, bool * const TransientNetworkFailure
)/*{{{*/
122 pkgAcquire::RunResult res
;
123 if(PulseInterval
> 0)
124 res
= Fetcher
.Run(PulseInterval
);
128 if (res
== pkgAcquire::Failed
)
131 for (pkgAcquire::ItemIterator I
= Fetcher
.ItemsBegin();
132 I
!= Fetcher
.ItemsEnd(); ++I
)
135 if ((*I
)->Status
== pkgAcquire::Item::StatDone
&&
136 (*I
)->Complete
== true)
139 if (TransientNetworkFailure
!= NULL
&& (*I
)->Status
== pkgAcquire::Item::StatIdle
)
141 *TransientNetworkFailure
= true;
145 ::URI
uri((*I
)->DescURI());
147 uri
.Password
.clear();
148 std::string descUri
= std::string(uri
);
149 _error
->Error(_("Failed to fetch %s %s\n"), descUri
.c_str(),
150 (*I
)->ErrorText
.c_str());
159 bool CheckFreeSpaceBeforeDownload(std::string
const &Dir
, unsigned long long FetchBytes
)/*{{{*/
161 uint32_t const RAMFS_MAGIC
= 0x858458f6;
162 /* Check for enough free space, but only if we are actually going to
164 if (_config
->FindB("APT::Get::Print-URIs", false) == true ||
165 _config
->FindB("APT::Get::Download", true) == false)
169 if (statvfs(Dir
.c_str(),&Buf
) != 0) {
170 if (errno
== EOVERFLOW
)
171 return _error
->WarningE("statvfs",_("Couldn't determine free space in %s"),
174 return _error
->Errno("statvfs",_("Couldn't determine free space in %s"),
179 unsigned long long const FreeBlocks
= _config
->Find("APT::Sandbox::User").empty() ? Buf
.f_bfree
: Buf
.f_bavail
;
180 if (FreeBlocks
< (FetchBytes
/ Buf
.f_bsize
))
183 if (statfs(Dir
.c_str(),&Stat
) != 0
184 #if HAVE_STRUCT_STATFS_F_TYPE
185 || Stat
.f_type
!= RAMFS_MAGIC
188 return _error
->Error(_("You don't have enough free space in %s."),