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