]>
Commit | Line | Data |
---|---|---|
ea542140 DK |
1 | #include <config.h> |
2 | ||
b3d44315 | 3 | #include <apt-pkg/acquire-method.h> |
472ff00e | 4 | #include <apt-pkg/configuration.h> |
453b82a3 | 5 | #include <apt-pkg/error.h> |
2f5b6151 | 6 | #include <apt-pkg/gpgv.h> |
453b82a3 | 7 | #include <apt-pkg/strutl.h> |
3927c6da | 8 | #include <apt-pkg/fileutl.h> |
b3d44315 | 9 | |
453b82a3 | 10 | #include <ctype.h> |
b3d44315 | 11 | #include <errno.h> |
453b82a3 DK |
12 | #include <stddef.h> |
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <string.h> | |
b3d44315 | 16 | #include <sys/wait.h> |
453b82a3 | 17 | #include <unistd.h> |
b3d44315 | 18 | #include <iostream> |
453b82a3 | 19 | #include <string> |
f5a3d009 DK |
20 | #include <vector> |
21 | ||
ea542140 DK |
22 | #include <apti18n.h> |
23 | ||
8f3ba4e8 DK |
24 | using std::string; |
25 | using std::vector; | |
26 | ||
b3d44315 MV |
27 | #define GNUPGPREFIX "[GNUPG:]" |
28 | #define GNUPGBADSIG "[GNUPG:] BADSIG" | |
29 | #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY" | |
30 | #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG" | |
c5d8878d MV |
31 | #define GNUPGGOODSIG "[GNUPG:] GOODSIG" |
32 | #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED" | |
33 | #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG" | |
2abb68b7 | 34 | #define GNUPGNODATA "[GNUPG:] NODATA" |
b3d44315 MV |
35 | |
36 | class GPGVMethod : public pkgAcqMethod | |
37 | { | |
38 | private: | |
da9ed163 | 39 | string VerifyGetSigners(const char *file, const char *outfile, |
c5d8878d MV |
40 | vector<string> &GoodSigners, |
41 | vector<string> &BadSigners, | |
42 | vector<string> &WorthlessSigners, | |
b3d44315 MV |
43 | vector<string> &NoPubKeySigners); |
44 | ||
45 | protected: | |
46 | virtual bool Fetch(FetchItem *Itm); | |
9983999d | 47 | virtual bool Configuration(string Message); |
b3d44315 MV |
48 | public: |
49 | ||
50 | GPGVMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {}; | |
51 | }; | |
52 | ||
9983999d MV |
53 | bool GPGVMethod::Configuration(string Message) |
54 | { | |
55 | if (pkgAcqMethod::Configuration(Message) == false) | |
56 | return false; | |
57 | ||
58 | DropPrivsOrDie(); | |
59 | ||
60 | return true; | |
61 | } | |
62 | ||
da9ed163 | 63 | string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile, |
b3d44315 MV |
64 | vector<string> &GoodSigners, |
65 | vector<string> &BadSigners, | |
c5d8878d | 66 | vector<string> &WorthlessSigners, |
b3d44315 MV |
67 | vector<string> &NoPubKeySigners) |
68 | { | |
46e39c8e | 69 | bool const Debug = _config->FindB("Debug::Acquire::gpgv", false); |
da9ed163 | 70 | |
46e39c8e MV |
71 | if (Debug == true) |
72 | std::clog << "inside VerifyGetSigners" << std::endl; | |
73 | ||
b3d44315 | 74 | int fd[2]; |
46e39c8e | 75 | |
b3d44315 | 76 | if (pipe(fd) < 0) |
b3d44315 | 77 | return "Couldn't create pipe"; |
b3d44315 | 78 | |
cf440fac | 79 | pid_t pid = fork(); |
b3d44315 | 80 | if (pid < 0) |
da9ed163 | 81 | return string("Couldn't spawn new process") + strerror(errno); |
b3d44315 | 82 | else if (pid == 0) |
99ed26d3 | 83 | ExecGPGV(outfile, file, 3, fd); |
b3d44315 MV |
84 | close(fd[1]); |
85 | ||
cf440fac DK |
86 | FILE *pipein = fdopen(fd[0], "r"); |
87 | ||
b39bb552 | 88 | // Loop over the output of apt-key (which really is gnupg), and check the signatures. |
bf6ac7ca DK |
89 | size_t buffersize = 0; |
90 | char *buffer = NULL; | |
b3d44315 MV |
91 | while (1) |
92 | { | |
bf6ac7ca DK |
93 | if (getline(&buffer, &buffersize, pipein) == -1) |
94 | break; | |
46e39c8e MV |
95 | if (Debug == true) |
96 | std::clog << "Read: " << buffer << std::endl; | |
b3d44315 MV |
97 | |
98 | // Push the data into three separate vectors, which | |
99 | // we later concatenate. They're kept separate so | |
100 | // if we improve the apt method communication stuff later | |
101 | // it will be better. | |
102 | if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0) | |
103 | { | |
46e39c8e MV |
104 | if (Debug == true) |
105 | std::clog << "Got BADSIG! " << std::endl; | |
b3d44315 MV |
106 | BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); |
107 | } | |
bf6ac7ca | 108 | |
b3d44315 MV |
109 | if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0) |
110 | { | |
46e39c8e MV |
111 | if (Debug == true) |
112 | std::clog << "Got NO_PUBKEY " << std::endl; | |
b3d44315 MV |
113 | NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); |
114 | } | |
2abb68b7 MV |
115 | if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0) |
116 | { | |
46e39c8e MV |
117 | if (Debug == true) |
118 | std::clog << "Got NODATA! " << std::endl; | |
2abb68b7 MV |
119 | BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); |
120 | } | |
c5d8878d MV |
121 | if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0) |
122 | { | |
46e39c8e MV |
123 | if (Debug == true) |
124 | std::clog << "Got KEYEXPIRED! " << std::endl; | |
c5d8878d MV |
125 | WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); |
126 | } | |
127 | if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0) | |
128 | { | |
46e39c8e MV |
129 | if (Debug == true) |
130 | std::clog << "Got REVKEYSIG! " << std::endl; | |
c5d8878d MV |
131 | WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); |
132 | } | |
133 | if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0) | |
b3d44315 MV |
134 | { |
135 | char *sig = buffer + sizeof(GNUPGPREFIX); | |
c5d8878d | 136 | char *p = sig + sizeof("GOODSIG"); |
b3d44315 MV |
137 | while (*p && isxdigit(*p)) |
138 | p++; | |
139 | *p = 0; | |
46e39c8e MV |
140 | if (Debug == true) |
141 | std::clog << "Got GOODSIG, key ID:" << sig << std::endl; | |
b3d44315 MV |
142 | GoodSigners.push_back(string(sig)); |
143 | } | |
144 | } | |
145 | fclose(pipein); | |
1b7bf822 | 146 | free(buffer); |
b3d44315 | 147 | |
cf440fac | 148 | int status; |
b3d44315 | 149 | waitpid(pid, &status, 0); |
46e39c8e | 150 | if (Debug == true) |
b3d44315 | 151 | { |
2737f28a | 152 | ioprintf(std::clog, "gpgv exited with status %i\n", WEXITSTATUS(status)); |
b3d44315 MV |
153 | } |
154 | ||
155 | if (WEXITSTATUS(status) == 0) | |
156 | { | |
157 | if (GoodSigners.empty()) | |
339690e4 | 158 | return _("Internal error: Good signature, but could not determine key fingerprint?!"); |
da9ed163 | 159 | return ""; |
b3d44315 MV |
160 | } |
161 | else if (WEXITSTATUS(status) == 1) | |
339690e4 | 162 | return _("At least one invalid signature was encountered."); |
b3d44315 | 163 | else if (WEXITSTATUS(status) == 111) |
b39bb552 | 164 | return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)"); |
ae99ce2e | 165 | else if (WEXITSTATUS(status) == 112) |
b3d44315 | 166 | { |
ae99ce2e DK |
167 | // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings) |
168 | std::string errmsg; | |
169 | //TRANSLATORS: %s is a single techy word like 'NODATA' | |
170 | strprintf(errmsg, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA"); | |
171 | return errmsg; | |
b3d44315 MV |
172 | } |
173 | else | |
b39bb552 | 174 | return _("Unknown error executing apt-key"); |
b3d44315 MV |
175 | } |
176 | ||
177 | bool GPGVMethod::Fetch(FetchItem *Itm) | |
178 | { | |
179 | URI Get = Itm->Uri; | |
180 | string Path = Get.Host + Get.Path; // To account for relative paths | |
181 | string keyID; | |
182 | vector<string> GoodSigners; | |
183 | vector<string> BadSigners; | |
c5d8878d MV |
184 | // a worthless signature is a expired or revoked one |
185 | vector<string> WorthlessSigners; | |
b3d44315 MV |
186 | vector<string> NoPubKeySigners; |
187 | ||
188 | FetchResult Res; | |
189 | Res.Filename = Itm->DestFile; | |
190 | URIStart(Res); | |
191 | ||
b39bb552 | 192 | // Run apt-key on file, extract contents and get the key ID of the signer |
da9ed163 | 193 | string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(), |
c5d8878d MV |
194 | GoodSigners, BadSigners, WorthlessSigners, |
195 | NoPubKeySigners); | |
b3d44315 MV |
196 | if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty()) |
197 | { | |
198 | string errmsg; | |
199 | // In this case, something bad probably happened, so we just go | |
200 | // with what the other method gave us for an error message. | |
c5d8878d | 201 | if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty()) |
b3d44315 MV |
202 | errmsg = msg; |
203 | else | |
204 | { | |
205 | if (!BadSigners.empty()) | |
206 | { | |
339690e4 | 207 | errmsg += _("The following signatures were invalid:\n"); |
b3d44315 | 208 | for (vector<string>::iterator I = BadSigners.begin(); |
f7f0d6c7 | 209 | I != BadSigners.end(); ++I) |
b3d44315 MV |
210 | errmsg += (*I + "\n"); |
211 | } | |
c5d8878d MV |
212 | if (!WorthlessSigners.empty()) |
213 | { | |
214 | errmsg += _("The following signatures were invalid:\n"); | |
215 | for (vector<string>::iterator I = WorthlessSigners.begin(); | |
f7f0d6c7 | 216 | I != WorthlessSigners.end(); ++I) |
c5d8878d MV |
217 | errmsg += (*I + "\n"); |
218 | } | |
b3d44315 MV |
219 | if (!NoPubKeySigners.empty()) |
220 | { | |
339690e4 | 221 | errmsg += _("The following signatures couldn't be verified because the public key is not available:\n"); |
b3d44315 | 222 | for (vector<string>::iterator I = NoPubKeySigners.begin(); |
f7f0d6c7 | 223 | I != NoPubKeySigners.end(); ++I) |
b3d44315 MV |
224 | errmsg += (*I + "\n"); |
225 | } | |
226 | } | |
ce424cd4 MV |
227 | // this is only fatal if we have no good sigs or if we have at |
228 | // least one bad signature. good signatures and NoPubKey signatures | |
229 | // happen easily when a file is signed with multiple signatures | |
230 | if(GoodSigners.empty() or !BadSigners.empty()) | |
f23153d0 | 231 | return _error->Error("%s", errmsg.c_str()); |
b3d44315 MV |
232 | } |
233 | ||
b3d44315 MV |
234 | // Just pass the raw output up, because passing it as a real data |
235 | // structure is too difficult with the method stuff. We keep it | |
236 | // as three separate vectors for future extensibility. | |
237 | Res.GPGVOutput = GoodSigners; | |
238 | Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end()); | |
239 | Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end()); | |
240 | URIDone(Res); | |
241 | ||
242 | if (_config->FindB("Debug::Acquire::gpgv", false)) | |
243 | { | |
b39bb552 | 244 | std::clog << "apt-key succeeded\n"; |
b3d44315 MV |
245 | } |
246 | ||
247 | return true; | |
248 | } | |
249 | ||
250 | ||
251 | int main() | |
252 | { | |
339690e4 | 253 | setlocale(LC_ALL, ""); |
3927c6da | 254 | |
b3d44315 MV |
255 | GPGVMethod Mth; |
256 | ||
257 | return Mth.Run(); | |
258 | } |