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