]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
960c06180eede581bdb1ebb0ef687189929ba4b3
1 #include <apt-pkg/error.h>
2 #include <apt-pkg/acquire-method.h>
3 #include <apt-pkg/strutl.h>
4 #include <apt-pkg/fileutl.h>
5 #include <apt-pkg/indexcopy.h>
18 #define GNUPGPREFIX "[GNUPG:]"
19 #define GNUPGBADSIG "[GNUPG:] BADSIG"
20 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
21 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
22 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
23 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
24 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
25 #define GNUPGNODATA "[GNUPG:] NODATA"
27 class GPGVMethod
: public pkgAcqMethod
30 string
VerifyGetSigners(const char *file
, const char *outfile
,
31 vector
<string
> &GoodSigners
,
32 vector
<string
> &BadSigners
,
33 vector
<string
> &WorthlessSigners
,
34 vector
<string
> &NoPubKeySigners
);
37 virtual bool Fetch(FetchItem
*Itm
);
41 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
44 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
45 vector
<string
> &GoodSigners
,
46 vector
<string
> &BadSigners
,
47 vector
<string
> &WorthlessSigners
,
48 vector
<string
> &NoPubKeySigners
)
50 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
51 // setup a (empty) stringstream for formating the return value
52 std::stringstream ret
;
56 std::clog
<< "inside VerifyGetSigners" << std::endl
;
61 return "Couldn't create pipe";
65 return string("Couldn't spawn new process") + strerror(errno
);
68 _error
->PushToStack();
69 bool const success
= SigVerify::RunGPGV(outfile
, file
, 3, fd
);
73 _error
->PopMessage(errmsg
);
74 _error
->RevertToStack();
77 _error
->RevertToStack();
82 FILE *pipein
= fdopen(fd
[0], "r");
84 // Loop over the output of gpgv, and check the signatures.
85 size_t buffersize
= 64;
86 char *buffer
= (char *) malloc(buffersize
);
93 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
95 if (bufferoff
== buffersize
)
96 buffer
= (char *) realloc(buffer
, buffersize
*= 2);
97 *(buffer
+bufferoff
) = c
;
100 if (bufferoff
== 0 && c
== EOF
)
102 *(buffer
+bufferoff
) = '\0';
105 std::clog
<< "Read: " << buffer
<< std::endl
;
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)
114 std::clog
<< "Got BADSIG! " << std::endl
;
115 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
118 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
121 std::clog
<< "Got NO_PUBKEY " << std::endl
;
122 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
124 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
127 std::clog
<< "Got NODATA! " << std::endl
;
128 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
130 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
133 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
134 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
136 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
139 std::clog
<< "Got REVKEYSIG! " << std::endl
;
140 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
142 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
144 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
145 char *p
= sig
+ sizeof("GOODSIG");
146 while (*p
&& isxdigit(*p
))
150 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
151 GoodSigners
.push_back(string(sig
));
157 waitpid(pid
, &status
, 0);
160 std::clog
<< "gpgv exited\n";
163 if (WEXITSTATUS(status
) == 0)
165 if (GoodSigners
.empty())
166 return _("Internal error: Good signature, but could not determine key fingerprint?!");
169 else if (WEXITSTATUS(status
) == 1)
171 return _("At least one invalid signature was encountered.");
173 else if (WEXITSTATUS(status
) == 111)
175 ioprintf(ret
, _("Could not execute 'gpgv' to verify signature (is gpgv installed?)"));
180 return _("Unknown error executing gpgv");
184 bool GPGVMethod::Fetch(FetchItem
*Itm
)
187 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
189 vector
<string
> GoodSigners
;
190 vector
<string
> BadSigners
;
191 // a worthless signature is a expired or revoked one
192 vector
<string
> WorthlessSigners
;
193 vector
<string
> NoPubKeySigners
;
196 Res
.Filename
= Itm
->DestFile
;
199 // Run gpgv on file, extract contents and get the key ID of the signer
200 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
201 GoodSigners
, BadSigners
, WorthlessSigners
,
203 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
206 // In this case, something bad probably happened, so we just go
207 // with what the other method gave us for an error message.
208 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
212 if (!BadSigners
.empty())
214 errmsg
+= _("The following signatures were invalid:\n");
215 for (vector
<string
>::iterator I
= BadSigners
.begin();
216 I
!= BadSigners
.end(); I
++)
217 errmsg
+= (*I
+ "\n");
219 if (!WorthlessSigners
.empty())
221 errmsg
+= _("The following signatures were invalid:\n");
222 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
223 I
!= WorthlessSigners
.end(); I
++)
224 errmsg
+= (*I
+ "\n");
226 if (!NoPubKeySigners
.empty())
228 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
229 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
230 I
!= NoPubKeySigners
.end(); I
++)
231 errmsg
+= (*I
+ "\n");
234 // this is only fatal if we have no good sigs or if we have at
235 // least one bad signature. good signatures and NoPubKey signatures
236 // happen easily when a file is signed with multiple signatures
237 if(GoodSigners
.empty() or !BadSigners
.empty())
238 return _error
->Error("%s", errmsg
.c_str());
241 // Just pass the raw output up, because passing it as a real data
242 // structure is too difficult with the method stuff. We keep it
243 // as three separate vectors for future extensibility.
244 Res
.GPGVOutput
= GoodSigners
;
245 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
246 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
249 if (_config
->FindB("Debug::Acquire::gpgv", false))
251 std::clog
<< "gpgv succeeded\n";
260 setlocale(LC_ALL
, "");