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