]> git.saurik.com Git - apt.git/blame - methods/gpgv.cc
setup LINES in apt-pkg/install-progress.cc
[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>
2f5b6151 9#include <apt-pkg/gpgv.h>
b3d44315 10
b3d44315
MV
11#include <utime.h>
12#include <stdio.h>
13#include <fcntl.h>
14#include <errno.h>
15#include <sys/wait.h>
16#include <iostream>
da9ed163 17#include <sstream>
f5a3d009
DK
18#include <vector>
19
ea542140
DK
20#include <apti18n.h>
21
8f3ba4e8
DK
22using std::string;
23using std::vector;
24
b3d44315
MV
25#define GNUPGPREFIX "[GNUPG:]"
26#define GNUPGBADSIG "[GNUPG:] BADSIG"
27#define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
28#define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
c5d8878d
MV
29#define GNUPGGOODSIG "[GNUPG:] GOODSIG"
30#define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
31#define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
2abb68b7 32#define GNUPGNODATA "[GNUPG:] NODATA"
b3d44315
MV
33
34class GPGVMethod : public pkgAcqMethod
35{
36 private:
da9ed163 37 string VerifyGetSigners(const char *file, const char *outfile,
c5d8878d
MV
38 vector<string> &GoodSigners,
39 vector<string> &BadSigners,
40 vector<string> &WorthlessSigners,
b3d44315
MV
41 vector<string> &NoPubKeySigners);
42
43 protected:
44 virtual bool Fetch(FetchItem *Itm);
45
46 public:
47
48 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
49};
50
da9ed163 51string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
b3d44315
MV
52 vector<string> &GoodSigners,
53 vector<string> &BadSigners,
c5d8878d 54 vector<string> &WorthlessSigners,
b3d44315
MV
55 vector<string> &NoPubKeySigners)
56{
46e39c8e 57 bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
da9ed163 58
46e39c8e
MV
59 if (Debug == true)
60 std::clog << "inside VerifyGetSigners" << std::endl;
61
b3d44315 62 int fd[2];
46e39c8e 63
b3d44315 64 if (pipe(fd) < 0)
b3d44315 65 return "Couldn't create pipe";
b3d44315 66
cf440fac 67 pid_t pid = fork();
b3d44315 68 if (pid < 0)
da9ed163 69 return string("Couldn't spawn new process") + strerror(errno);
b3d44315 70 else if (pid == 0)
99ed26d3 71 ExecGPGV(outfile, file, 3, fd);
b3d44315
MV
72 close(fd[1]);
73
cf440fac
DK
74 FILE *pipein = fdopen(fd[0], "r");
75
b3d44315
MV
76 // Loop over the output of gpgv, and check the signatures.
77 size_t buffersize = 64;
78 char *buffer = (char *) malloc(buffersize);
79 size_t bufferoff = 0;
80 while (1)
81 {
82 int c;
83
84 // Read a line. Sigh.
85 while ((c = getc(pipein)) != EOF && c != '\n')
86 {
aaab1007
DK
87 if (bufferoff == buffersize)
88 {
89 char* newBuffer = (char *) realloc(buffer, buffersize *= 2);
90 if (newBuffer == NULL)
91 {
92 free(buffer);
93 return "Couldn't allocate a buffer big enough for reading";
94 }
95 buffer = newBuffer;
96 }
b3d44315
MV
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);
1b7bf822 155 free(buffer);
b3d44315 156
cf440fac 157 int status;
b3d44315 158 waitpid(pid, &status, 0);
46e39c8e 159 if (Debug == true)
b3d44315 160 {
46e39c8e 161 std::clog << "gpgv exited\n";
b3d44315
MV
162 }
163
164 if (WEXITSTATUS(status) == 0)
165 {
166 if (GoodSigners.empty())
339690e4 167 return _("Internal error: Good signature, but could not determine key fingerprint?!");
da9ed163 168 return "";
b3d44315
MV
169 }
170 else if (WEXITSTATUS(status) == 1)
339690e4 171 return _("At least one invalid signature was encountered.");
b3d44315 172 else if (WEXITSTATUS(status) == 111)
ae99ce2e
DK
173 return _("Could not execute 'gpgv' to verify signature (is gpgv installed?)");
174 else if (WEXITSTATUS(status) == 112)
b3d44315 175 {
ae99ce2e
DK
176 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
177 std::string errmsg;
178 //TRANSLATORS: %s is a single techy word like 'NODATA'
179 strprintf(errmsg, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
180 return errmsg;
b3d44315
MV
181 }
182 else
339690e4 183 return _("Unknown error executing gpgv");
b3d44315
MV
184}
185
186bool GPGVMethod::Fetch(FetchItem *Itm)
187{
188 URI Get = Itm->Uri;
189 string Path = Get.Host + Get.Path; // To account for relative paths
190 string keyID;
191 vector<string> GoodSigners;
192 vector<string> BadSigners;
c5d8878d
MV
193 // a worthless signature is a expired or revoked one
194 vector<string> WorthlessSigners;
b3d44315
MV
195 vector<string> NoPubKeySigners;
196
197 FetchResult Res;
198 Res.Filename = Itm->DestFile;
199 URIStart(Res);
200
201 // Run gpgv on file, extract contents and get the key ID of the signer
da9ed163 202 string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
c5d8878d
MV
203 GoodSigners, BadSigners, WorthlessSigners,
204 NoPubKeySigners);
b3d44315
MV
205 if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
206 {
207 string errmsg;
208 // In this case, something bad probably happened, so we just go
209 // with what the other method gave us for an error message.
c5d8878d 210 if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
b3d44315
MV
211 errmsg = msg;
212 else
213 {
214 if (!BadSigners.empty())
215 {
339690e4 216 errmsg += _("The following signatures were invalid:\n");
b3d44315 217 for (vector<string>::iterator I = BadSigners.begin();
f7f0d6c7 218 I != BadSigners.end(); ++I)
b3d44315
MV
219 errmsg += (*I + "\n");
220 }
c5d8878d
MV
221 if (!WorthlessSigners.empty())
222 {
223 errmsg += _("The following signatures were invalid:\n");
224 for (vector<string>::iterator I = WorthlessSigners.begin();
f7f0d6c7 225 I != WorthlessSigners.end(); ++I)
c5d8878d
MV
226 errmsg += (*I + "\n");
227 }
b3d44315
MV
228 if (!NoPubKeySigners.empty())
229 {
339690e4 230 errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
b3d44315 231 for (vector<string>::iterator I = NoPubKeySigners.begin();
f7f0d6c7 232 I != NoPubKeySigners.end(); ++I)
b3d44315
MV
233 errmsg += (*I + "\n");
234 }
235 }
ce424cd4
MV
236 // this is only fatal if we have no good sigs or if we have at
237 // least one bad signature. good signatures and NoPubKey signatures
238 // happen easily when a file is signed with multiple signatures
239 if(GoodSigners.empty() or !BadSigners.empty())
f23153d0 240 return _error->Error("%s", errmsg.c_str());
b3d44315
MV
241 }
242
b3d44315
MV
243 // Just pass the raw output up, because passing it as a real data
244 // structure is too difficult with the method stuff. We keep it
245 // as three separate vectors for future extensibility.
246 Res.GPGVOutput = GoodSigners;
247 Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end());
248 Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end());
249 URIDone(Res);
250
251 if (_config->FindB("Debug::Acquire::gpgv", false))
252 {
46e39c8e 253 std::clog << "gpgv succeeded\n";
b3d44315
MV
254 }
255
256 return true;
257}
258
259
260int main()
261{
339690e4
MV
262 setlocale(LC_ALL, "");
263
b3d44315
MV
264 GPGVMethod Mth;
265
266 return Mth.Run();
267}