]>
Commit | Line | Data |
---|---|---|
b3d44315 MV |
1 | #include <apt-pkg/error.h> |
2 | #include <apt-pkg/acquire-method.h> | |
3 | #include <apt-pkg/strutl.h> | |
339690e4 | 4 | #include <apti18n.h> |
b3d44315 MV |
5 | |
6 | #include <sys/stat.h> | |
7 | #include <unistd.h> | |
8 | #include <utime.h> | |
9 | #include <stdio.h> | |
10 | #include <fcntl.h> | |
11 | #include <errno.h> | |
12 | #include <sys/wait.h> | |
13 | #include <iostream> | |
da9ed163 | 14 | #include <sstream> |
b3d44315 MV |
15 | |
16 | #define GNUPGPREFIX "[GNUPG:]" | |
17 | #define GNUPGBADSIG "[GNUPG:] BADSIG" | |
18 | #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY" | |
19 | #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG" | |
2abb68b7 | 20 | #define GNUPGNODATA "[GNUPG:] NODATA" |
b3d44315 MV |
21 | |
22 | class GPGVMethod : public pkgAcqMethod | |
23 | { | |
24 | private: | |
da9ed163 | 25 | string VerifyGetSigners(const char *file, const char *outfile, |
b3d44315 MV |
26 | vector<string> &GoodSigners, vector<string> &BadSigners, |
27 | vector<string> &NoPubKeySigners); | |
28 | ||
29 | protected: | |
30 | virtual bool Fetch(FetchItem *Itm); | |
31 | ||
32 | public: | |
33 | ||
34 | GPGVMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {}; | |
35 | }; | |
36 | ||
da9ed163 | 37 | string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile, |
b3d44315 MV |
38 | vector<string> &GoodSigners, |
39 | vector<string> &BadSigners, | |
40 | vector<string> &NoPubKeySigners) | |
41 | { | |
da9ed163 MV |
42 | // setup a (empty) stringstream for formating the return value |
43 | std::stringstream ret; | |
b2c22075 | 44 | ret.str(""); |
da9ed163 | 45 | |
b3d44315 MV |
46 | if (_config->FindB("Debug::Acquire::gpgv", false)) |
47 | { | |
48 | std::cerr << "inside VerifyGetSigners" << std::endl; | |
49 | } | |
50 | pid_t pid; | |
51 | int fd[2]; | |
52 | FILE *pipein; | |
53 | int status; | |
54 | struct stat buff; | |
55 | string gpgvpath = _config->Find("Dir::Bin::gpg", "/usr/bin/gpgv"); | |
8a3642bd | 56 | string pubringpath = _config->Find("APT::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg"); |
b3d44315 MV |
57 | if (_config->FindB("Debug::Acquire::gpgv", false)) |
58 | { | |
59 | std::cerr << "gpgv path: " << gpgvpath << std::endl; | |
60 | std::cerr << "Keyring path: " << pubringpath << std::endl; | |
61 | } | |
62 | ||
da9ed163 MV |
63 | if (stat(pubringpath.c_str(), &buff) != 0) |
64 | { | |
65 | ioprintf(ret, _("Couldn't access keyring: '%s'"), strerror(errno)); | |
66 | return ret.str(); | |
67 | } | |
b3d44315 MV |
68 | if (pipe(fd) < 0) |
69 | { | |
70 | return "Couldn't create pipe"; | |
71 | } | |
72 | ||
73 | pid = fork(); | |
74 | if (pid < 0) | |
75 | { | |
da9ed163 | 76 | return string("Couldn't spawn new process") + strerror(errno); |
b3d44315 MV |
77 | } |
78 | else if (pid == 0) | |
79 | { | |
ad6a45d8 MV |
80 | const char *Args[400]; |
81 | unsigned int i = 0; | |
82 | ||
83 | Args[i++] = gpgvpath.c_str(); | |
84 | Args[i++] = "--status-fd"; | |
85 | Args[i++] = "3"; | |
86 | Args[i++] = "--keyring"; | |
87 | Args[i++] = pubringpath.c_str(); | |
88 | ||
89 | Configuration::Item const *Opts; | |
90 | Opts = _config->Tree("Acquire::gpgv::Options"); | |
91 | if (Opts != 0) | |
92 | { | |
93 | Opts = Opts->Child; | |
94 | for (; Opts != 0; Opts = Opts->Next) | |
95 | { | |
96 | if (Opts->Value.empty() == true) | |
97 | continue; | |
98 | Args[i++] = Opts->Value.c_str(); | |
99 | if(i >= 395) { | |
339690e4 | 100 | std::cerr << _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl; |
ad6a45d8 MV |
101 | exit(111); |
102 | } | |
103 | } | |
104 | } | |
105 | Args[i++] = file; | |
106 | Args[i++] = outfile; | |
107 | Args[i++] = NULL; | |
108 | ||
b3d44315 MV |
109 | if (_config->FindB("Debug::Acquire::gpgv", false)) |
110 | { | |
ad6a45d8 | 111 | std::cerr << "Preparing to exec: " << gpgvpath; |
ec048f63 | 112 | for(unsigned int j=0;Args[j] != NULL; j++) |
af45f087 | 113 | std::cerr << " " << Args[j]; |
ad6a45d8 | 114 | std::cerr << std::endl; |
b3d44315 MV |
115 | } |
116 | int nullfd = open("/dev/null", O_RDONLY); | |
117 | close(fd[0]); | |
118 | // Redirect output to /dev/null; we read from the status fd | |
119 | dup2(nullfd, STDOUT_FILENO); | |
120 | dup2(nullfd, STDERR_FILENO); | |
121 | // Redirect the pipe to the status fd (3) | |
122 | dup2(fd[1], 3); | |
123 | ||
31c64df3 OS |
124 | putenv((char *)"LANG="); |
125 | putenv((char *)"LC_ALL="); | |
126 | putenv((char *)"LC_MESSAGES="); | |
ad6a45d8 | 127 | execvp(gpgvpath.c_str(), (char **)Args); |
b3d44315 MV |
128 | |
129 | exit(111); | |
130 | } | |
131 | close(fd[1]); | |
132 | ||
133 | pipein = fdopen(fd[0], "r"); | |
134 | ||
135 | // Loop over the output of gpgv, and check the signatures. | |
136 | size_t buffersize = 64; | |
137 | char *buffer = (char *) malloc(buffersize); | |
138 | size_t bufferoff = 0; | |
139 | while (1) | |
140 | { | |
141 | int c; | |
142 | ||
143 | // Read a line. Sigh. | |
144 | while ((c = getc(pipein)) != EOF && c != '\n') | |
145 | { | |
146 | if (bufferoff == buffersize) | |
147 | buffer = (char *) realloc(buffer, buffersize *= 2); | |
148 | *(buffer+bufferoff) = c; | |
149 | bufferoff++; | |
150 | } | |
151 | if (bufferoff == 0 && c == EOF) | |
152 | break; | |
153 | *(buffer+bufferoff) = '\0'; | |
154 | bufferoff = 0; | |
155 | if (_config->FindB("Debug::Acquire::gpgv", false)) | |
156 | std::cerr << "Read: " << buffer << std::endl; | |
157 | ||
158 | // Push the data into three separate vectors, which | |
159 | // we later concatenate. They're kept separate so | |
160 | // if we improve the apt method communication stuff later | |
161 | // it will be better. | |
162 | if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0) | |
163 | { | |
164 | if (_config->FindB("Debug::Acquire::gpgv", false)) | |
165 | std::cerr << "Got BADSIG! " << std::endl; | |
166 | BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); | |
167 | } | |
168 | ||
169 | if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0) | |
170 | { | |
171 | if (_config->FindB("Debug::Acquire::gpgv", false)) | |
172 | std::cerr << "Got NO_PUBKEY " << std::endl; | |
173 | NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); | |
174 | } | |
2abb68b7 MV |
175 | if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0) |
176 | { | |
177 | if (_config->FindB("Debug::Acquire::gpgv", false)) | |
178 | std::cerr << "Got NODATA! " << std::endl; | |
179 | BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); | |
180 | } | |
b3d44315 MV |
181 | if (strncmp(buffer, GNUPGVALIDSIG, sizeof(GNUPGVALIDSIG)-1) == 0) |
182 | { | |
183 | char *sig = buffer + sizeof(GNUPGPREFIX); | |
184 | char *p = sig + sizeof("VALIDSIG"); | |
185 | while (*p && isxdigit(*p)) | |
186 | p++; | |
187 | *p = 0; | |
188 | if (_config->FindB("Debug::Acquire::gpgv", false)) | |
189 | std::cerr << "Got VALIDSIG, key ID:" << sig << std::endl; | |
190 | GoodSigners.push_back(string(sig)); | |
191 | } | |
192 | } | |
193 | fclose(pipein); | |
194 | ||
195 | waitpid(pid, &status, 0); | |
196 | if (_config->FindB("Debug::Acquire::gpgv", false)) | |
197 | { | |
339690e4 | 198 | std::cerr << "gpgv exited\n"; |
b3d44315 MV |
199 | } |
200 | ||
201 | if (WEXITSTATUS(status) == 0) | |
202 | { | |
203 | if (GoodSigners.empty()) | |
339690e4 | 204 | return _("Internal error: Good signature, but could not determine key fingerprint?!"); |
da9ed163 | 205 | return ""; |
b3d44315 MV |
206 | } |
207 | else if (WEXITSTATUS(status) == 1) | |
208 | { | |
339690e4 | 209 | return _("At least one invalid signature was encountered."); |
b3d44315 MV |
210 | } |
211 | else if (WEXITSTATUS(status) == 111) | |
212 | { | |
da9ed163 MV |
213 | ioprintf(ret, _("Could not execute '%s' to verify signature (is gnupg installed?)"), gpgvpath.c_str()); |
214 | return ret.str(); | |
b3d44315 MV |
215 | } |
216 | else | |
217 | { | |
339690e4 | 218 | return _("Unknown error executing gpgv"); |
b3d44315 MV |
219 | } |
220 | } | |
221 | ||
222 | bool GPGVMethod::Fetch(FetchItem *Itm) | |
223 | { | |
224 | URI Get = Itm->Uri; | |
225 | string Path = Get.Host + Get.Path; // To account for relative paths | |
226 | string keyID; | |
227 | vector<string> GoodSigners; | |
228 | vector<string> BadSigners; | |
229 | vector<string> NoPubKeySigners; | |
230 | ||
231 | FetchResult Res; | |
232 | Res.Filename = Itm->DestFile; | |
233 | URIStart(Res); | |
234 | ||
235 | // Run gpgv on file, extract contents and get the key ID of the signer | |
da9ed163 MV |
236 | string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(), |
237 | GoodSigners, BadSigners, NoPubKeySigners); | |
b3d44315 MV |
238 | if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty()) |
239 | { | |
240 | string errmsg; | |
241 | // In this case, something bad probably happened, so we just go | |
242 | // with what the other method gave us for an error message. | |
243 | if (BadSigners.empty() && NoPubKeySigners.empty()) | |
244 | errmsg = msg; | |
245 | else | |
246 | { | |
247 | if (!BadSigners.empty()) | |
248 | { | |
339690e4 | 249 | errmsg += _("The following signatures were invalid:\n"); |
b3d44315 MV |
250 | for (vector<string>::iterator I = BadSigners.begin(); |
251 | I != BadSigners.end(); I++) | |
252 | errmsg += (*I + "\n"); | |
253 | } | |
254 | if (!NoPubKeySigners.empty()) | |
255 | { | |
339690e4 | 256 | errmsg += _("The following signatures couldn't be verified because the public key is not available:\n"); |
b3d44315 MV |
257 | for (vector<string>::iterator I = NoPubKeySigners.begin(); |
258 | I != NoPubKeySigners.end(); I++) | |
259 | errmsg += (*I + "\n"); | |
260 | } | |
261 | } | |
ce424cd4 MV |
262 | // this is only fatal if we have no good sigs or if we have at |
263 | // least one bad signature. good signatures and NoPubKey signatures | |
264 | // happen easily when a file is signed with multiple signatures | |
265 | if(GoodSigners.empty() or !BadSigners.empty()) | |
266 | return _error->Error(errmsg.c_str()); | |
b3d44315 MV |
267 | } |
268 | ||
269 | // Transfer the modification times | |
270 | struct stat Buf; | |
271 | if (stat(Path.c_str(),&Buf) != 0) | |
339690e4 | 272 | return _error->Errno("stat",_("Failed to stat %s"), Path.c_str()); |
b3d44315 MV |
273 | |
274 | struct utimbuf TimeBuf; | |
275 | TimeBuf.actime = Buf.st_atime; | |
276 | TimeBuf.modtime = Buf.st_mtime; | |
277 | if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0) | |
339690e4 | 278 | return _error->Errno("utime",_("Failed to set modification time")); |
b3d44315 MV |
279 | |
280 | if (stat(Itm->DestFile.c_str(),&Buf) != 0) | |
339690e4 | 281 | return _error->Errno("stat",_("Failed to stat")); |
b3d44315 MV |
282 | |
283 | // Return a Done response | |
284 | Res.LastModified = Buf.st_mtime; | |
285 | Res.Size = Buf.st_size; | |
286 | // Just pass the raw output up, because passing it as a real data | |
287 | // structure is too difficult with the method stuff. We keep it | |
288 | // as three separate vectors for future extensibility. | |
289 | Res.GPGVOutput = GoodSigners; | |
290 | Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end()); | |
291 | Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end()); | |
292 | URIDone(Res); | |
293 | ||
294 | if (_config->FindB("Debug::Acquire::gpgv", false)) | |
295 | { | |
339690e4 | 296 | std::cerr << "gpgv succeeded\n"; |
b3d44315 MV |
297 | } |
298 | ||
299 | return true; | |
300 | } | |
301 | ||
302 | ||
303 | int main() | |
304 | { | |
339690e4 MV |
305 | setlocale(LC_ALL, ""); |
306 | ||
b3d44315 MV |
307 | GPGVMethod Mth; |
308 | ||
309 | return Mth.Run(); | |
310 | } |