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