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