]> git.saurik.com Git - apt.git/blame - methods/gpgv.cc
releasing version 0.8.16~exp3
[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>
46e39c8e 4#include <apt-pkg/fileutl.h>
a319c4ee 5#include <apt-pkg/indexcopy.h>
339690e4 6#include <apti18n.h>
b3d44315 7
b3d44315
MV
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 15
f5a3d009
DK
16#include <vector>
17
b3d44315
MV
18#define GNUPGPREFIX "[GNUPG:]"
19#define GNUPGBADSIG "[GNUPG:] BADSIG"
20#define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
21#define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
c5d8878d
MV
22#define GNUPGGOODSIG "[GNUPG:] GOODSIG"
23#define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
24#define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
2abb68b7 25#define GNUPGNODATA "[GNUPG:] NODATA"
b3d44315
MV
26
27class GPGVMethod : public pkgAcqMethod
28{
29 private:
da9ed163 30 string VerifyGetSigners(const char *file, const char *outfile,
c5d8878d
MV
31 vector<string> &GoodSigners,
32 vector<string> &BadSigners,
33 vector<string> &WorthlessSigners,
b3d44315
MV
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
da9ed163 44string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
b3d44315
MV
45 vector<string> &GoodSigners,
46 vector<string> &BadSigners,
c5d8878d 47 vector<string> &WorthlessSigners,
b3d44315
MV
48 vector<string> &NoPubKeySigners)
49{
46e39c8e 50 bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
da9ed163
MV
51 // setup a (empty) stringstream for formating the return value
52 std::stringstream ret;
b2c22075 53 ret.str("");
da9ed163 54
46e39c8e
MV
55 if (Debug == true)
56 std::clog << "inside VerifyGetSigners" << std::endl;
57
b3d44315 58 int fd[2];
46e39c8e 59
b3d44315 60 if (pipe(fd) < 0)
b3d44315 61 return "Couldn't create pipe";
b3d44315 62
cf440fac 63 pid_t pid = fork();
b3d44315 64 if (pid < 0)
da9ed163 65 return string("Couldn't spawn new process") + strerror(errno);
b3d44315
MV
66 else if (pid == 0)
67 {
89c4c588
MV
68 _error->PushToStack();
69 bool const success = SigVerify::RunGPGV(outfile, file, 3, fd);
70 if (success == false)
b3d44315 71 {
89c4c588
MV
72 string errmsg;
73 _error->PopMessage(errmsg);
74 _error->RevertToStack();
75 return errmsg;
b3d44315 76 }
89c4c588 77 _error->RevertToStack();
b3d44315
MV
78 exit(111);
79 }
80 close(fd[1]);
81
cf440fac
DK
82 FILE *pipein = fdopen(fd[0], "r");
83
b3d44315
MV
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;
46e39c8e
MV
104 if (Debug == true)
105 std::clog << "Read: " << buffer << std::endl;
b3d44315
MV
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 {
46e39c8e
MV
113 if (Debug == true)
114 std::clog << "Got BADSIG! " << std::endl;
b3d44315
MV
115 BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
116 }
117
118 if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
119 {
46e39c8e
MV
120 if (Debug == true)
121 std::clog << "Got NO_PUBKEY " << std::endl;
b3d44315
MV
122 NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
123 }
2abb68b7
MV
124 if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0)
125 {
46e39c8e
MV
126 if (Debug == true)
127 std::clog << "Got NODATA! " << std::endl;
2abb68b7
MV
128 BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
129 }
c5d8878d
MV
130 if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0)
131 {
46e39c8e
MV
132 if (Debug == true)
133 std::clog << "Got KEYEXPIRED! " << std::endl;
c5d8878d
MV
134 WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
135 }
136 if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
137 {
46e39c8e
MV
138 if (Debug == true)
139 std::clog << "Got REVKEYSIG! " << std::endl;
c5d8878d
MV
140 WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
141 }
142 if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
b3d44315
MV
143 {
144 char *sig = buffer + sizeof(GNUPGPREFIX);
c5d8878d 145 char *p = sig + sizeof("GOODSIG");
b3d44315
MV
146 while (*p && isxdigit(*p))
147 p++;
148 *p = 0;
46e39c8e
MV
149 if (Debug == true)
150 std::clog << "Got GOODSIG, key ID:" << sig << std::endl;
b3d44315
MV
151 GoodSigners.push_back(string(sig));
152 }
153 }
154 fclose(pipein);
155
cf440fac 156 int status;
b3d44315 157 waitpid(pid, &status, 0);
46e39c8e 158 if (Debug == true)
b3d44315 159 {
46e39c8e 160 std::clog << "gpgv exited\n";
b3d44315
MV
161 }
162
163 if (WEXITSTATUS(status) == 0)
164 {
165 if (GoodSigners.empty())
339690e4 166 return _("Internal error: Good signature, but could not determine key fingerprint?!");
da9ed163 167 return "";
b3d44315
MV
168 }
169 else if (WEXITSTATUS(status) == 1)
170 {
339690e4 171 return _("At least one invalid signature was encountered.");
b3d44315
MV
172 }
173 else if (WEXITSTATUS(status) == 111)
174 {
cf440fac 175 ioprintf(ret, _("Could not execute 'gpgv' to verify signature (is gpgv installed?)"));
da9ed163 176 return ret.str();
b3d44315
MV
177 }
178 else
179 {
339690e4 180 return _("Unknown error executing gpgv");
b3d44315
MV
181 }
182}
183
184bool 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;
c5d8878d
MV
191 // a worthless signature is a expired or revoked one
192 vector<string> WorthlessSigners;
b3d44315
MV
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
da9ed163 200 string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
c5d8878d
MV
201 GoodSigners, BadSigners, WorthlessSigners,
202 NoPubKeySigners);
b3d44315
MV
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.
c5d8878d 208 if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
b3d44315
MV
209 errmsg = msg;
210 else
211 {
212 if (!BadSigners.empty())
213 {
339690e4 214 errmsg += _("The following signatures were invalid:\n");
b3d44315
MV
215 for (vector<string>::iterator I = BadSigners.begin();
216 I != BadSigners.end(); I++)
217 errmsg += (*I + "\n");
218 }
c5d8878d
MV
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 }
b3d44315
MV
226 if (!NoPubKeySigners.empty())
227 {
339690e4 228 errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
b3d44315
MV
229 for (vector<string>::iterator I = NoPubKeySigners.begin();
230 I != NoPubKeySigners.end(); I++)
231 errmsg += (*I + "\n");
232 }
233 }
ce424cd4
MV
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())
f23153d0 238 return _error->Error("%s", errmsg.c_str());
b3d44315
MV
239 }
240
b3d44315
MV
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 {
46e39c8e 251 std::clog << "gpgv succeeded\n";
b3d44315
MV
252 }
253
254 return true;
255}
256
257
258int main()
259{
339690e4
MV
260 setlocale(LC_ALL, "");
261
b3d44315
MV
262 GPGVMethod Mth;
263
264 return Mth.Run();
265}