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