1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
#ifndef APT_APTMETHOD_H
#define APT_APTMETHOD_H
#include <apt-pkg/acquire-method.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/netrc.h>
#include <algorithm>
#include <locale>
#include <string>
#include <vector>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <apti18n.h>
static bool hasDoubleColon(std::string const &n)
{
return n.find("::") != std::string::npos;
}
class aptMethod : public pkgAcqMethod
{
protected:
std::string const Binary;
public:
virtual bool Configuration(std::string Message) APT_OVERRIDE
{
if (pkgAcqMethod::Configuration(Message) == false)
return false;
std::string const conf = std::string("Binary::") + Binary;
_config->MoveSubTree(conf.c_str(), NULL);
DropPrivsOrDie();
return true;
}
bool CalculateHashes(FetchItem const * const Itm, FetchResult &Res) const APT_NONNULL(2)
{
Hashes Hash(Itm->ExpectedHashes);
FileFd Fd;
if (Fd.Open(Res.Filename, FileFd::ReadOnly) == false || Hash.AddFD(Fd) == false)
return false;
Res.TakeHashes(Hash);
return true;
}
void Warning(const char *Format,...)
{
va_list args;
va_start(args,Format);
PrintStatus("104 Warning", Format, args);
va_end(args);
}
std::vector<std::string> methodNames;
void setPostfixForMethodNames(char const * const postfix) APT_NONNULL(2)
{
methodNames.erase(std::remove_if(methodNames.begin(), methodNames.end(), hasDoubleColon), methodNames.end());
decltype(methodNames) toAdd;
for (auto && name: methodNames)
toAdd.emplace_back(name + "::" + postfix);
std::move(toAdd.begin(), toAdd.end(), std::back_inserter(methodNames));
}
bool DebugEnabled() const
{
if (methodNames.empty())
return false;
auto const sni = std::find_if_not(methodNames.crbegin(), methodNames.crend(), hasDoubleColon);
if (unlikely(sni == methodNames.crend()))
return false;
auto const ln = methodNames[methodNames.size() - 1];
// worst case: all three are the same
std::string confln, confsn, confpn;
strprintf(confln, "Debug::Acquire::%s", ln.c_str());
strprintf(confsn, "Debug::Acquire::%s", sni->c_str());
auto const pni = sni->substr(0, sni->find('+'));
strprintf(confpn, "Debug::Acquire::%s", pni.c_str());
return _config->FindB(confln,_config->FindB(confsn, _config->FindB(confpn, false)));
}
std::string ConfigFind(char const * const postfix, std::string const &defValue) const APT_NONNULL(2)
{
for (auto name = methodNames.rbegin(); name != methodNames.rend(); ++name)
{
std::string conf;
strprintf(conf, "Acquire::%s::%s", name->c_str(), postfix);
auto const value = _config->Find(conf);
if (value.empty() == false)
return value;
}
return defValue;
}
std::string ConfigFind(std::string const &postfix, std::string const &defValue) const
{
return ConfigFind(postfix.c_str(), defValue);
}
bool ConfigFindB(char const * const postfix, bool const defValue) const APT_NONNULL(2)
{
return StringToBool(ConfigFind(postfix, defValue ? "yes" : "no"), defValue);
}
int ConfigFindI(char const * const postfix, int const defValue) const APT_NONNULL(2)
{
char *End;
std::string const value = ConfigFind(postfix, "");
auto const Res = strtol(value.c_str(), &End, 0);
if (value.c_str() == End)
return defValue;
return Res;
}
bool TransferModificationTimes(char const * const From, char const * const To, time_t &LastModified) APT_NONNULL(2, 3)
{
if (strcmp(To, "/dev/null") == 0)
return true;
struct stat Buf2;
if (lstat(To, &Buf2) != 0 || S_ISLNK(Buf2.st_mode))
return true;
struct stat Buf;
if (stat(From, &Buf) != 0)
return _error->Errno("stat",_("Failed to stat"));
// we don't use utimensat here for compatibility reasons: #738567
struct timeval times[2];
times[0].tv_sec = Buf.st_atime;
LastModified = times[1].tv_sec = Buf.st_mtime;
times[0].tv_usec = times[1].tv_usec = 0;
if (utimes(To, times) != 0)
return _error->Errno("utimes",_("Failed to set modification time"));
return true;
}
aptMethod(std::string &&Binary, char const * const Ver, unsigned long const Flags) APT_NONNULL(3) :
pkgAcqMethod(Ver, Flags), Binary(Binary), methodNames({Binary})
{
try {
std::locale::global(std::locale(""));
} catch (...) {
setlocale(LC_ALL, "");
}
}
};
class aptAuthConfMethod : public aptMethod
{
FileFd authconf;
public:
virtual bool Configuration(std::string Message) APT_OVERRIDE
{
if (pkgAcqMethod::Configuration(Message) == false)
return false;
std::string const conf = std::string("Binary::") + Binary;
_config->MoveSubTree(conf.c_str(), NULL);
auto const netrc = _config->FindFile("Dir::Etc::netrc");
if (netrc.empty() == false)
{
// ignore errors with opening the auth file as it doesn't need to exist
_error->PushToStack();
authconf.Open(netrc, FileFd::ReadOnly);
_error->RevertToStack();
}
DropPrivsOrDie();
return true;
}
bool MaybeAddAuthTo(URI &uri)
{
if (uri.User.empty() == false || uri.Password.empty() == false)
return true;
if (authconf.IsOpen() == false)
return true;
if (authconf.Seek(0) == false)
return false;
return MaybeAddAuth(authconf, uri);
}
aptAuthConfMethod(std::string &&Binary, char const * const Ver, unsigned long const Flags) APT_NONNULL(3) :
aptMethod(std::move(Binary), Ver, Flags) {}
};
#endif
|