]> git.saurik.com Git - apt.git/blame - methods/gpgv.cc
- fix "apt-get source pkg=ver" if binary name != source name
[apt.git] / methods / gpgv.cc
CommitLineData
b3d44315
MV
1#include <apt-pkg/error.h>
2#include <apt-pkg/acquire-method.h>
3#include <apt-pkg/strutl.h>
339690e4 4#include <apti18n.h>
b3d44315
MV
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>
da9ed163 14#include <sstream>
b3d44315
MV
15
16#define GNUPGPREFIX "[GNUPG:]"
17#define GNUPGBADSIG "[GNUPG:] BADSIG"
18#define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
19#define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
2abb68b7 20#define GNUPGNODATA "[GNUPG:] NODATA"
b3d44315
MV
21
22class GPGVMethod : public pkgAcqMethod
23{
24 private:
da9ed163 25 string VerifyGetSigners(const char *file, const char *outfile,
b3d44315
MV
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
da9ed163 37string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
b3d44315
MV
38 vector<string> &GoodSigners,
39 vector<string> &BadSigners,
40 vector<string> &NoPubKeySigners)
41{
da9ed163
MV
42 // setup a (empty) stringstream for formating the return value
43 std::stringstream ret;
b2c22075 44 ret.str("");
da9ed163 45
b3d44315
MV
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");
8a3642bd 56 string pubringpath = _config->Find("APT::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg");
b3d44315
MV
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
da9ed163
MV
63 if (stat(pubringpath.c_str(), &buff) != 0)
64 {
65 ioprintf(ret, _("Couldn't access keyring: '%s'"), strerror(errno));
66 return ret.str();
67 }
b3d44315
MV
68 if (pipe(fd) < 0)
69 {
70 return "Couldn't create pipe";
71 }
72
73 pid = fork();
74 if (pid < 0)
75 {
da9ed163 76 return string("Couldn't spawn new process") + strerror(errno);
b3d44315
MV
77 }
78 else if (pid == 0)
79 {
ad6a45d8
MV
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";
9129f2af 86 Args[i++] = "--ignore-time-conflict";
ad6a45d8
MV
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) {
339690e4 101 std::cerr << _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl;
ad6a45d8
MV
102 exit(111);
103 }
104 }
105 }
106 Args[i++] = file;
107 Args[i++] = outfile;
108 Args[i++] = NULL;
109
b3d44315
MV
110 if (_config->FindB("Debug::Acquire::gpgv", false))
111 {
ad6a45d8 112 std::cerr << "Preparing to exec: " << gpgvpath;
ec048f63 113 for(unsigned int j=0;Args[j] != NULL; j++)
af45f087 114 std::cerr << " " << Args[j];
ad6a45d8 115 std::cerr << std::endl;
b3d44315
MV
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
31c64df3
OS
125 putenv((char *)"LANG=");
126 putenv((char *)"LC_ALL=");
127 putenv((char *)"LC_MESSAGES=");
ad6a45d8 128 execvp(gpgvpath.c_str(), (char **)Args);
b3d44315
MV
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 }
2abb68b7
MV
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 }
b3d44315
MV
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 {
339690e4 199 std::cerr << "gpgv exited\n";
b3d44315
MV
200 }
201
202 if (WEXITSTATUS(status) == 0)
203 {
204 if (GoodSigners.empty())
339690e4 205 return _("Internal error: Good signature, but could not determine key fingerprint?!");
da9ed163 206 return "";
b3d44315
MV
207 }
208 else if (WEXITSTATUS(status) == 1)
209 {
339690e4 210 return _("At least one invalid signature was encountered.");
b3d44315
MV
211 }
212 else if (WEXITSTATUS(status) == 111)
213 {
f83589b5 214 ioprintf(ret, _("Could not execute '%s' to verify signature (is gpgv installed?)"), gpgvpath.c_str());
da9ed163 215 return ret.str();
b3d44315
MV
216 }
217 else
218 {
339690e4 219 return _("Unknown error executing gpgv");
b3d44315
MV
220 }
221}
222
223bool 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
da9ed163
MV
237 string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
238 GoodSigners, BadSigners, NoPubKeySigners);
b3d44315
MV
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 {
339690e4 250 errmsg += _("The following signatures were invalid:\n");
b3d44315
MV
251 for (vector<string>::iterator I = BadSigners.begin();
252 I != BadSigners.end(); I++)
253 errmsg += (*I + "\n");
254 }
255 if (!NoPubKeySigners.empty())
256 {
339690e4 257 errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
b3d44315
MV
258 for (vector<string>::iterator I = NoPubKeySigners.begin();
259 I != NoPubKeySigners.end(); I++)
260 errmsg += (*I + "\n");
261 }
262 }
ce424cd4
MV
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());
b3d44315
MV
268 }
269
b3d44315
MV
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 {
339690e4 280 std::cerr << "gpgv succeeded\n";
b3d44315
MV
281 }
282
283 return true;
284}
285
286
287int main()
288{
339690e4
MV
289 setlocale(LC_ALL, "");
290
b3d44315
MV
291 GPGVMethod Mth;
292
293 return Mth.Run();
294}