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