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