]> git.saurik.com Git - apt.git/blob - methods/gpgv.cc
debian/apt.postinst: chown _apt:root /etc/apt/auth.conf
[apt.git] / methods / gpgv.cc
1 #include <config.h>
2
3 #include <apt-pkg/acquire-method.h>
4 #include <apt-pkg/configuration.h>
5 #include <apt-pkg/error.h>
6 #include <apt-pkg/gpgv.h>
7 #include <apt-pkg/strutl.h>
8 #include <apt-pkg/fileutl.h>
9
10 #include <ctype.h>
11 #include <errno.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/wait.h>
17 #include <unistd.h>
18 #include <iostream>
19 #include <string>
20 #include <vector>
21
22 #include <apti18n.h>
23
24 using std::string;
25 using std::vector;
26
27 #define GNUPGPREFIX "[GNUPG:]"
28 #define GNUPGBADSIG "[GNUPG:] BADSIG"
29 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
30 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
31 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
32 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
33 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
34 #define GNUPGNODATA "[GNUPG:] NODATA"
35
36 class GPGVMethod : public pkgAcqMethod
37 {
38 private:
39 string VerifyGetSigners(const char *file, const char *outfile,
40 vector<string> &GoodSigners,
41 vector<string> &BadSigners,
42 vector<string> &WorthlessSigners,
43 vector<string> &NoPubKeySigners);
44
45 protected:
46 virtual bool Fetch(FetchItem *Itm);
47 virtual bool Configuration(string Message);
48 public:
49
50 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
51 };
52
53 bool GPGVMethod::Configuration(string Message)
54 {
55 if (pkgAcqMethod::Configuration(Message) == false)
56 return false;
57
58 DropPrivsOrDie();
59
60 return true;
61 }
62
63 string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
64 vector<string> &GoodSigners,
65 vector<string> &BadSigners,
66 vector<string> &WorthlessSigners,
67 vector<string> &NoPubKeySigners)
68 {
69 bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
70
71 if (Debug == true)
72 std::clog << "inside VerifyGetSigners" << std::endl;
73
74 int fd[2];
75
76 if (pipe(fd) < 0)
77 return "Couldn't create pipe";
78
79 pid_t pid = fork();
80 if (pid < 0)
81 return string("Couldn't spawn new process") + strerror(errno);
82 else if (pid == 0)
83 ExecGPGV(outfile, file, 3, fd);
84 close(fd[1]);
85
86 FILE *pipein = fdopen(fd[0], "r");
87
88 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
89 size_t buffersize = 64;
90 char *buffer = (char *) malloc(buffersize);
91 size_t bufferoff = 0;
92 while (1)
93 {
94 int c;
95
96 // Read a line. Sigh.
97 while ((c = getc(pipein)) != EOF && c != '\n')
98 {
99 if (bufferoff == buffersize)
100 {
101 char* newBuffer = (char *) realloc(buffer, buffersize *= 2);
102 if (newBuffer == NULL)
103 {
104 free(buffer);
105 return "Couldn't allocate a buffer big enough for reading";
106 }
107 buffer = newBuffer;
108 }
109 *(buffer+bufferoff) = c;
110 bufferoff++;
111 }
112 if (bufferoff == 0 && c == EOF)
113 break;
114 *(buffer+bufferoff) = '\0';
115 bufferoff = 0;
116 if (Debug == true)
117 std::clog << "Read: " << buffer << std::endl;
118
119 // Push the data into three separate vectors, which
120 // we later concatenate. They're kept separate so
121 // if we improve the apt method communication stuff later
122 // it will be better.
123 if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
124 {
125 if (Debug == true)
126 std::clog << "Got BADSIG! " << std::endl;
127 BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
128 }
129
130 if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
131 {
132 if (Debug == true)
133 std::clog << "Got NO_PUBKEY " << std::endl;
134 NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
135 }
136 if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0)
137 {
138 if (Debug == true)
139 std::clog << "Got NODATA! " << std::endl;
140 BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
141 }
142 if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0)
143 {
144 if (Debug == true)
145 std::clog << "Got KEYEXPIRED! " << std::endl;
146 WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
147 }
148 if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
149 {
150 if (Debug == true)
151 std::clog << "Got REVKEYSIG! " << std::endl;
152 WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
153 }
154 if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
155 {
156 char *sig = buffer + sizeof(GNUPGPREFIX);
157 char *p = sig + sizeof("GOODSIG");
158 while (*p && isxdigit(*p))
159 p++;
160 *p = 0;
161 if (Debug == true)
162 std::clog << "Got GOODSIG, key ID:" << sig << std::endl;
163 GoodSigners.push_back(string(sig));
164 }
165 }
166 fclose(pipein);
167 free(buffer);
168
169 int status;
170 waitpid(pid, &status, 0);
171 if (Debug == true)
172 {
173 ioprintf(std::clog, "gpgv exited with status %i\n", WEXITSTATUS(status));
174 }
175
176 if (WEXITSTATUS(status) == 0)
177 {
178 if (GoodSigners.empty())
179 return _("Internal error: Good signature, but could not determine key fingerprint?!");
180 return "";
181 }
182 else if (WEXITSTATUS(status) == 1)
183 return _("At least one invalid signature was encountered.");
184 else if (WEXITSTATUS(status) == 111)
185 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
186 else if (WEXITSTATUS(status) == 112)
187 {
188 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
189 std::string errmsg;
190 //TRANSLATORS: %s is a single techy word like 'NODATA'
191 strprintf(errmsg, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
192 return errmsg;
193 }
194 else
195 return _("Unknown error executing apt-key");
196 }
197
198 bool 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;
205 // a worthless signature is a expired or revoked one
206 vector<string> WorthlessSigners;
207 vector<string> NoPubKeySigners;
208
209 FetchResult Res;
210 Res.Filename = Itm->DestFile;
211 URIStart(Res);
212
213 // Run apt-key on file, extract contents and get the key ID of the signer
214 string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
215 GoodSigners, BadSigners, WorthlessSigners,
216 NoPubKeySigners);
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.
222 if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
223 errmsg = msg;
224 else
225 {
226 if (!BadSigners.empty())
227 {
228 errmsg += _("The following signatures were invalid:\n");
229 for (vector<string>::iterator I = BadSigners.begin();
230 I != BadSigners.end(); ++I)
231 errmsg += (*I + "\n");
232 }
233 if (!WorthlessSigners.empty())
234 {
235 errmsg += _("The following signatures were invalid:\n");
236 for (vector<string>::iterator I = WorthlessSigners.begin();
237 I != WorthlessSigners.end(); ++I)
238 errmsg += (*I + "\n");
239 }
240 if (!NoPubKeySigners.empty())
241 {
242 errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
243 for (vector<string>::iterator I = NoPubKeySigners.begin();
244 I != NoPubKeySigners.end(); ++I)
245 errmsg += (*I + "\n");
246 }
247 }
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())
252 return _error->Error("%s", errmsg.c_str());
253 }
254
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 {
265 std::clog << "apt-key succeeded\n";
266 }
267
268 return true;
269 }
270
271
272 int main()
273 {
274 setlocale(LC_ALL, "");
275
276 GPGVMethod Mth;
277
278 return Mth.Run();
279 }