]> git.saurik.com Git - apt.git/blame - methods/gpgv.cc
Update with files from apt-sarge. Complete French translation.
[apt.git] / methods / gpgv.cc
CommitLineData
7db98ffc
MZ
1#include <apt-pkg/error.h>
2#include <apt-pkg/acquire-method.h>
3#include <apt-pkg/strutl.h>
4
5#include <sys/stat.h>
6#include <unistd.h>
7#include <utime.h>
8#include <stdio.h>
9#include <fcntl.h>
10#include <errno.h>
11#include <sys/wait.h>
12#include <iostream>
13
14#define GNUPGPREFIX "[GNUPG:]"
15#define GNUPGBADSIG "[GNUPG:] BADSIG"
16#define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
17#define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
18
19class GPGVMethod : public pkgAcqMethod
20{
21 private:
22 const char *VerifyGetSigners(const char *file, const char *outfile,
23 vector<string> &GoodSigners, vector<string> &BadSigners,
24 vector<string> &NoPubKeySigners);
25
26 protected:
27 virtual bool Fetch(FetchItem *Itm);
28
29 public:
30
31 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
32};
33
34const char *GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
35 vector<string> &GoodSigners,
36 vector<string> &BadSigners,
37 vector<string> &NoPubKeySigners)
38{
39 if (_config->FindB("Debug::Acquire::gpgv", false))
40 {
41 std::cerr << "inside VerifyGetSigners" << std::endl;
42 }
43 pid_t pid;
44 int fd[2];
45 FILE *pipein;
46 int status;
47 struct stat buff;
48 string gpgvpath = _config->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
49 string pubringpath = _config->Find("Apt::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg");
50 if (_config->FindB("Debug::Acquire::gpgv", false))
51 {
52 std::cerr << "gpgv path: " << gpgvpath << std::endl;
53 std::cerr << "Keyring path: " << pubringpath << std::endl;
54 }
55
56 if (stat(pubringpath.c_str(), &buff) != 0)
57 return (string("Couldn't access keyring: ") + strerror(errno)).c_str();
58
59 if (pipe(fd) < 0)
60 {
61 return "Couldn't create pipe";
62 }
63
64 pid = fork();
65 if (pid < 0)
66 {
67 return (string("Couldn't spawn new process") + strerror(errno)).c_str();
68 }
69 else if (pid == 0)
70 {
71 if (_config->FindB("Debug::Acquire::gpgv", false))
72 {
73 std::cerr << "Preparing to exec: " << gpgvpath
74 << " --status-fd 3 --keyring " << pubringpath
75 << " " << file << " " << outfile << std::endl;
76 }
77 int nullfd = open("/dev/null", O_RDONLY);
78 close(fd[0]);
79 // Redirect output to /dev/null; we read from the status fd
80 dup2(nullfd, STDOUT_FILENO);
81 dup2(nullfd, STDERR_FILENO);
82 // Redirect the pipe to the status fd (3)
83 dup2(fd[1], 3);
84
85 putenv("LANG=");
86 putenv("LC_ALL=");
87 putenv("LC_MESSAGES=");
88 execlp(gpgvpath.c_str(), gpgvpath.c_str(), "--status-fd", "3", "--keyring",
89 pubringpath.c_str(), file, outfile, NULL);
90
91 exit(111);
92 }
93 close(fd[1]);
94
95 pipein = fdopen(fd[0], "r");
96
97 // Loop over the output of gpgv, and check the signatures.
98 size_t buffersize = 64;
99 char *buffer = (char *) malloc(buffersize);
100 size_t bufferoff = 0;
101 while (1)
102 {
103 int c;
104
105 // Read a line. Sigh.
106 while ((c = getc(pipein)) != EOF && c != '\n')
107 {
108 if (bufferoff == buffersize)
109 buffer = (char *) realloc(buffer, buffersize *= 2);
110 *(buffer+bufferoff) = c;
111 bufferoff++;
112 }
113 if (bufferoff == 0 && c == EOF)
114 break;
115 *(buffer+bufferoff) = '\0';
116 bufferoff = 0;
117 if (_config->FindB("Debug::Acquire::gpgv", false))
118 std::cerr << "Read: " << buffer << std::endl;
119
120 // Push the data into three separate vectors, which
121 // we later concatenate. They're kept separate so
122 // if we improve the apt method communication stuff later
123 // it will be better.
124 if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
125 {
126 if (_config->FindB("Debug::Acquire::gpgv", false))
127 std::cerr << "Got BADSIG! " << std::endl;
128 BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
129 }
130
131 if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
132 {
133 if (_config->FindB("Debug::Acquire::gpgv", false))
134 std::cerr << "Got NO_PUBKEY " << std::endl;
135 NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
136 }
137
138 if (strncmp(buffer, GNUPGVALIDSIG, sizeof(GNUPGVALIDSIG)-1) == 0)
139 {
140 char *sig = buffer + sizeof(GNUPGPREFIX);
141 char *p = sig + sizeof("VALIDSIG");
142 while (*p && isxdigit(*p))
143 p++;
144 *p = 0;
145 if (_config->FindB("Debug::Acquire::gpgv", false))
146 std::cerr << "Got VALIDSIG, key ID:" << sig << std::endl;
147 GoodSigners.push_back(string(sig));
148 }
149 }
150 fclose(pipein);
151
152 waitpid(pid, &status, 0);
153 if (_config->FindB("Debug::Acquire::gpgv", false))
154 {
155 std::cerr <<"gpgv exited\n";
156 }
157
158 if (WEXITSTATUS(status) == 0)
159 {
160 if (GoodSigners.empty())
161 return "Internal error: Good signature, but could not determine key fingerprint?!";
162 return NULL;
163 }
164 else if (WEXITSTATUS(status) == 1)
165 {
166 return "At least one invalid signature was encountered.";
167 }
168 else if (WEXITSTATUS(status) == 111)
169 {
170 return (string("Could not execute ") + gpgvpath +
171 string(" to verify signature (is gnupg installed?)")).c_str();
172 }
173 else
174 {
175 return "Unknown error executing gpgv";
176 }
177}
178
179bool GPGVMethod::Fetch(FetchItem *Itm)
180{
181 URI Get = Itm->Uri;
182 string Path = Get.Host + Get.Path; // To account for relative paths
183 string keyID;
184 vector<string> GoodSigners;
185 vector<string> BadSigners;
186 vector<string> NoPubKeySigners;
187
188 FetchResult Res;
189 Res.Filename = Itm->DestFile;
190 URIStart(Res);
191
192 // Run gpgv on file, extract contents and get the key ID of the signer
193 const char *msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
194 GoodSigners, BadSigners, NoPubKeySigners);
195 if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
196 {
197 string errmsg;
198 // In this case, something bad probably happened, so we just go
199 // with what the other method gave us for an error message.
200 if (BadSigners.empty() && NoPubKeySigners.empty())
201 errmsg = msg;
202 else
203 {
204 if (!BadSigners.empty())
205 {
206 errmsg += "The following signatures were invalid:\n";
207 for (vector<string>::iterator I = BadSigners.begin();
208 I != BadSigners.end(); I++)
209 errmsg += (*I + "\n");
210 }
211 if (!NoPubKeySigners.empty())
212 {
213 errmsg += "The following signatures couldn't be verified because the public key is not available:\n";
214 for (vector<string>::iterator I = NoPubKeySigners.begin();
215 I != NoPubKeySigners.end(); I++)
216 errmsg += (*I + "\n");
217 }
218 }
219 return _error->Error(errmsg.c_str());
220 }
221
222 // Transfer the modification times
223 struct stat Buf;
224 if (stat(Path.c_str(),&Buf) != 0)
225 return _error->Errno("stat","Failed to stat %s", Path.c_str());
226
227 struct utimbuf TimeBuf;
228 TimeBuf.actime = Buf.st_atime;
229 TimeBuf.modtime = Buf.st_mtime;
230 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
231 return _error->Errno("utime","Failed to set modification time");
232
233 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
234 return _error->Errno("stat","Failed to stat");
235
236 // Return a Done response
237 Res.LastModified = Buf.st_mtime;
238 Res.Size = Buf.st_size;
239 // Just pass the raw output up, because passing it as a real data
240 // structure is too difficult with the method stuff. We keep it
241 // as three separate vectors for future extensibility.
242 Res.GPGVOutput = GoodSigners;
243 Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end());
244 Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end());
245 URIDone(Res);
246
247 if (_config->FindB("Debug::Acquire::gpgv", false))
248 {
249 std::cerr <<"gpgv suceeded\n";
250 }
251
252 return true;
253}
254
255
256int main()
257{
258 GPGVMethod Mth;
259
260 return Mth.Run();
261}