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