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