]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
67cbd36a21e28e21a3786bf6dac493cf5f0d0cc3
3 #include <apt-pkg/error.h>
4 #include <apt-pkg/acquire-method.h>
5 #include <apt-pkg/strutl.h>
6 #include <apt-pkg/fileutl.h>
7 #include <apt-pkg/indexcopy.h>
23 #define GNUPGPREFIX "[GNUPG:]"
24 #define GNUPGBADSIG "[GNUPG:] BADSIG"
25 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
26 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
27 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
28 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
29 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
30 #define GNUPGNODATA "[GNUPG:] NODATA"
32 class GPGVMethod
: public pkgAcqMethod
35 string
VerifyGetSigners(const char *file
, const char *outfile
,
36 vector
<string
> &GoodSigners
,
37 vector
<string
> &BadSigners
,
38 vector
<string
> &WorthlessSigners
,
39 vector
<string
> &NoPubKeySigners
);
42 virtual bool Fetch(FetchItem
*Itm
);
46 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
49 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
50 vector
<string
> &GoodSigners
,
51 vector
<string
> &BadSigners
,
52 vector
<string
> &WorthlessSigners
,
53 vector
<string
> &NoPubKeySigners
)
55 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
56 // setup a (empty) stringstream for formating the return value
57 std::stringstream ret
;
61 std::clog
<< "inside VerifyGetSigners" << std::endl
;
66 return "Couldn't create pipe";
70 return string("Couldn't spawn new process") + strerror(errno
);
73 _error
->PushToStack();
74 bool const success
= SigVerify::RunGPGV(outfile
, file
, 3, fd
);
78 _error
->PopMessage(errmsg
);
79 _error
->RevertToStack();
82 _error
->RevertToStack();
87 FILE *pipein
= fdopen(fd
[0], "r");
89 // Loop over the output of gpgv, and check the signatures.
90 size_t buffersize
= 64;
91 char *buffer
= (char *) malloc(buffersize
);
98 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
100 if (bufferoff
== buffersize
)
101 buffer
= (char *) realloc(buffer
, buffersize
*= 2);
102 *(buffer
+bufferoff
) = c
;
105 if (bufferoff
== 0 && c
== EOF
)
107 *(buffer
+bufferoff
) = '\0';
110 std::clog
<< "Read: " << buffer
<< std::endl
;
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)
119 std::clog
<< "Got BADSIG! " << std::endl
;
120 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
123 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
126 std::clog
<< "Got NO_PUBKEY " << std::endl
;
127 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
129 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
132 std::clog
<< "Got NODATA! " << std::endl
;
133 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
135 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
138 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
139 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
141 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
144 std::clog
<< "Got REVKEYSIG! " << std::endl
;
145 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
147 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
149 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
150 char *p
= sig
+ sizeof("GOODSIG");
151 while (*p
&& isxdigit(*p
))
155 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
156 GoodSigners
.push_back(string(sig
));
162 waitpid(pid
, &status
, 0);
165 std::clog
<< "gpgv exited\n";
168 if (WEXITSTATUS(status
) == 0)
170 if (GoodSigners
.empty())
171 return _("Internal error: Good signature, but could not determine key fingerprint?!");
174 else if (WEXITSTATUS(status
) == 1)
176 return _("At least one invalid signature was encountered.");
178 else if (WEXITSTATUS(status
) == 111)
180 ioprintf(ret
, _("Could not execute 'gpgv' to verify signature (is gpgv installed?)"));
185 return _("Unknown error executing gpgv");
189 bool GPGVMethod::Fetch(FetchItem
*Itm
)
192 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
194 vector
<string
> GoodSigners
;
195 vector
<string
> BadSigners
;
196 // a worthless signature is a expired or revoked one
197 vector
<string
> WorthlessSigners
;
198 vector
<string
> NoPubKeySigners
;
201 Res
.Filename
= Itm
->DestFile
;
204 // Run gpgv on file, extract contents and get the key ID of the signer
205 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
206 GoodSigners
, BadSigners
, WorthlessSigners
,
208 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
211 // In this case, something bad probably happened, so we just go
212 // with what the other method gave us for an error message.
213 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
217 if (!BadSigners
.empty())
219 errmsg
+= _("The following signatures were invalid:\n");
220 for (vector
<string
>::iterator I
= BadSigners
.begin();
221 I
!= BadSigners
.end(); ++I
)
222 errmsg
+= (*I
+ "\n");
224 if (!WorthlessSigners
.empty())
226 errmsg
+= _("The following signatures were invalid:\n");
227 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
228 I
!= WorthlessSigners
.end(); ++I
)
229 errmsg
+= (*I
+ "\n");
231 if (!NoPubKeySigners
.empty())
233 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
234 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
235 I
!= NoPubKeySigners
.end(); ++I
)
236 errmsg
+= (*I
+ "\n");
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())
243 return _error
->Error("%s", errmsg
.c_str());
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());
254 if (_config
->FindB("Debug::Acquire::gpgv", false))
256 std::clog
<< "gpgv succeeded\n";
265 setlocale(LC_ALL
, "");