]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
1 #include <apt-pkg/error.h>
2 #include <apt-pkg/acquire-method.h>
3 #include <apt-pkg/strutl.h>
16 #define GNUPGPREFIX "[GNUPG:]"
17 #define GNUPGBADSIG "[GNUPG:] BADSIG"
18 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
19 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
20 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
21 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
22 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
23 #define GNUPGNODATA "[GNUPG:] NODATA"
25 class GPGVMethod
: public pkgAcqMethod
28 string
VerifyGetSigners(const char *file
, const char *outfile
,
29 vector
<string
> &GoodSigners
,
30 vector
<string
> &BadSigners
,
31 vector
<string
> &WorthlessSigners
,
32 vector
<string
> &NoPubKeySigners
);
35 virtual bool Fetch(FetchItem
*Itm
);
39 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
42 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
43 vector
<string
> &GoodSigners
,
44 vector
<string
> &BadSigners
,
45 vector
<string
> &WorthlessSigners
,
46 vector
<string
> &NoPubKeySigners
)
48 // setup a (empty) stringstream for formating the return value
49 std::stringstream ret
;
52 if (_config
->FindB("Debug::Acquire::gpgv", false))
54 std::cerr
<< "inside VerifyGetSigners" << std::endl
;
61 string gpgvpath
= _config
->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
62 string pubringpath
= _config
->Find("APT::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg");
63 if (_config
->FindB("Debug::Acquire::gpgv", false))
65 std::cerr
<< "gpgv path: " << gpgvpath
<< std::endl
;
66 std::cerr
<< "Keyring path: " << pubringpath
<< std::endl
;
69 if (stat(pubringpath
.c_str(), &buff
) != 0)
71 ioprintf(ret
, _("Couldn't access keyring: '%s'"), strerror(errno
));
76 return "Couldn't create pipe";
82 return string("Couldn't spawn new process") + strerror(errno
);
86 const char *Args
[400];
89 Args
[i
++] = gpgvpath
.c_str();
90 Args
[i
++] = "--status-fd";
92 Args
[i
++] = "--ignore-time-conflict";
93 Args
[i
++] = "--keyring";
94 Args
[i
++] = pubringpath
.c_str();
96 Configuration::Item
const *Opts
;
97 Opts
= _config
->Tree("Acquire::gpgv::Options");
101 for (; Opts
!= 0; Opts
= Opts
->Next
)
103 if (Opts
->Value
.empty() == true)
105 Args
[i
++] = Opts
->Value
.c_str();
107 std::cerr
<< _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl
;
116 if (_config
->FindB("Debug::Acquire::gpgv", false))
118 std::cerr
<< "Preparing to exec: " << gpgvpath
;
119 for(unsigned int j
=0;Args
[j
] != NULL
; j
++)
120 std::cerr
<< " " << Args
[j
];
121 std::cerr
<< std::endl
;
123 int nullfd
= open("/dev/null", O_RDONLY
);
125 // Redirect output to /dev/null; we read from the status fd
126 dup2(nullfd
, STDOUT_FILENO
);
127 dup2(nullfd
, STDERR_FILENO
);
128 // Redirect the pipe to the status fd (3)
131 putenv((char *)"LANG=");
132 putenv((char *)"LC_ALL=");
133 putenv((char *)"LC_MESSAGES=");
134 execvp(gpgvpath
.c_str(), (char **)Args
);
140 pipein
= fdopen(fd
[0], "r");
142 // Loop over the output of gpgv, and check the signatures.
143 size_t buffersize
= 64;
144 char *buffer
= (char *) malloc(buffersize
);
145 size_t bufferoff
= 0;
150 // Read a line. Sigh.
151 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
153 if (bufferoff
== buffersize
)
154 buffer
= (char *) realloc(buffer
, buffersize
*= 2);
155 *(buffer
+bufferoff
) = c
;
158 if (bufferoff
== 0 && c
== EOF
)
160 *(buffer
+bufferoff
) = '\0';
162 if (_config
->FindB("Debug::Acquire::gpgv", false))
163 std::cerr
<< "Read: " << buffer
<< std::endl
;
165 // Push the data into three separate vectors, which
166 // we later concatenate. They're kept separate so
167 // if we improve the apt method communication stuff later
168 // it will be better.
169 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
171 if (_config
->FindB("Debug::Acquire::gpgv", false))
172 std::cerr
<< "Got BADSIG! " << std::endl
;
173 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
176 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
178 if (_config
->FindB("Debug::Acquire::gpgv", false))
179 std::cerr
<< "Got NO_PUBKEY " << std::endl
;
180 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
182 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
184 if (_config
->FindB("Debug::Acquire::gpgv", false))
185 std::cerr
<< "Got NODATA! " << std::endl
;
186 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
188 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
190 if (_config
->FindB("Debug::Acquire::gpgv", false))
191 std::cerr
<< "Got KEYEXPIRED! " << std::endl
;
192 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
194 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
196 if (_config
->FindB("Debug::Acquire::gpgv", false))
197 std::cerr
<< "Got REVKEYSIG! " << std::endl
;
198 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
200 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
202 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
203 char *p
= sig
+ sizeof("GOODSIG");
204 while (*p
&& isxdigit(*p
))
207 if (_config
->FindB("Debug::Acquire::gpgv", false))
208 std::cerr
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
209 GoodSigners
.push_back(string(sig
));
214 waitpid(pid
, &status
, 0);
215 if (_config
->FindB("Debug::Acquire::gpgv", false))
217 std::cerr
<< "gpgv exited\n";
220 if (WEXITSTATUS(status
) == 0)
222 if (GoodSigners
.empty())
223 return _("Internal error: Good signature, but could not determine key fingerprint?!");
226 else if (WEXITSTATUS(status
) == 1)
228 return _("At least one invalid signature was encountered.");
230 else if (WEXITSTATUS(status
) == 111)
232 ioprintf(ret
, _("Could not execute '%s' to verify signature (is gpgv installed?)"), gpgvpath
.c_str());
237 return _("Unknown error executing gpgv");
241 bool GPGVMethod::Fetch(FetchItem
*Itm
)
244 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
246 vector
<string
> GoodSigners
;
247 vector
<string
> BadSigners
;
248 // a worthless signature is a expired or revoked one
249 vector
<string
> WorthlessSigners
;
250 vector
<string
> NoPubKeySigners
;
253 Res
.Filename
= Itm
->DestFile
;
256 // Run gpgv on file, extract contents and get the key ID of the signer
257 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
258 GoodSigners
, BadSigners
, WorthlessSigners
,
260 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
263 // In this case, something bad probably happened, so we just go
264 // with what the other method gave us for an error message.
265 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
269 if (!BadSigners
.empty())
271 errmsg
+= _("The following signatures were invalid:\n");
272 for (vector
<string
>::iterator I
= BadSigners
.begin();
273 I
!= BadSigners
.end(); I
++)
274 errmsg
+= (*I
+ "\n");
276 if (!WorthlessSigners
.empty())
278 errmsg
+= _("The following signatures were invalid:\n");
279 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
280 I
!= WorthlessSigners
.end(); I
++)
281 errmsg
+= (*I
+ "\n");
283 if (!NoPubKeySigners
.empty())
285 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
286 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
287 I
!= NoPubKeySigners
.end(); I
++)
288 errmsg
+= (*I
+ "\n");
291 // this is only fatal if we have no good sigs or if we have at
292 // least one bad signature. good signatures and NoPubKey signatures
293 // happen easily when a file is signed with multiple signatures
294 if(GoodSigners
.empty() or !BadSigners
.empty())
295 return _error
->Error(errmsg
.c_str());
298 // Just pass the raw output up, because passing it as a real data
299 // structure is too difficult with the method stuff. We keep it
300 // as three separate vectors for future extensibility.
301 Res
.GPGVOutput
= GoodSigners
;
302 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
303 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
306 if (_config
->FindB("Debug::Acquire::gpgv", false))
308 std::cerr
<< "gpgv succeeded\n";
317 setlocale(LC_ALL
, "");