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