]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
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>
20 #define GNUPGPREFIX "[GNUPG:]"
21 #define GNUPGBADSIG "[GNUPG:] BADSIG"
22 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
23 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
24 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
25 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
26 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
27 #define GNUPGNODATA "[GNUPG:] NODATA"
29 class GPGVMethod
: public pkgAcqMethod
32 string
VerifyGetSigners(const char *file
, const char *outfile
,
33 vector
<string
> &GoodSigners
,
34 vector
<string
> &BadSigners
,
35 vector
<string
> &WorthlessSigners
,
36 vector
<string
> &NoPubKeySigners
);
39 virtual bool Fetch(FetchItem
*Itm
);
43 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
46 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
47 vector
<string
> &GoodSigners
,
48 vector
<string
> &BadSigners
,
49 vector
<string
> &WorthlessSigners
,
50 vector
<string
> &NoPubKeySigners
)
52 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
53 // setup a (empty) stringstream for formating the return value
54 std::stringstream ret
;
58 std::clog
<< "inside VerifyGetSigners" << std::endl
;
63 return "Couldn't create pipe";
67 return string("Couldn't spawn new process") + strerror(errno
);
70 _error
->PushToStack();
71 bool const success
= SigVerify::RunGPGV(outfile
, file
, 3, fd
);
75 _error
->PopMessage(errmsg
);
76 _error
->RevertToStack();
79 _error
->RevertToStack();
84 FILE *pipein
= fdopen(fd
[0], "r");
86 // Loop over the output of gpgv, and check the signatures.
87 size_t buffersize
= 64;
88 char *buffer
= (char *) malloc(buffersize
);
95 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
97 if (bufferoff
== buffersize
)
98 buffer
= (char *) realloc(buffer
, buffersize
*= 2);
99 *(buffer
+bufferoff
) = c
;
102 if (bufferoff
== 0 && c
== EOF
)
104 *(buffer
+bufferoff
) = '\0';
107 std::clog
<< "Read: " << buffer
<< std::endl
;
109 // Push the data into three separate vectors, which
110 // we later concatenate. They're kept separate so
111 // if we improve the apt method communication stuff later
112 // it will be better.
113 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
116 std::clog
<< "Got BADSIG! " << std::endl
;
117 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
120 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
123 std::clog
<< "Got NO_PUBKEY " << std::endl
;
124 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
126 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
129 std::clog
<< "Got NODATA! " << std::endl
;
130 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
132 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
135 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
136 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
138 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
141 std::clog
<< "Got REVKEYSIG! " << std::endl
;
142 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
144 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
146 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
147 char *p
= sig
+ sizeof("GOODSIG");
148 while (*p
&& isxdigit(*p
))
152 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
153 GoodSigners
.push_back(string(sig
));
159 waitpid(pid
, &status
, 0);
162 std::clog
<< "gpgv exited\n";
165 if (WEXITSTATUS(status
) == 0)
167 if (GoodSigners
.empty())
168 return _("Internal error: Good signature, but could not determine key fingerprint?!");
171 else if (WEXITSTATUS(status
) == 1)
173 return _("At least one invalid signature was encountered.");
175 else if (WEXITSTATUS(status
) == 111)
177 ioprintf(ret
, _("Could not execute 'gpgv' to verify signature (is gpgv installed?)"));
182 return _("Unknown error executing gpgv");
186 bool GPGVMethod::Fetch(FetchItem
*Itm
)
189 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
191 vector
<string
> GoodSigners
;
192 vector
<string
> BadSigners
;
193 // a worthless signature is a expired or revoked one
194 vector
<string
> WorthlessSigners
;
195 vector
<string
> NoPubKeySigners
;
198 Res
.Filename
= Itm
->DestFile
;
201 // Run gpgv on file, extract contents and get the key ID of the signer
202 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
203 GoodSigners
, BadSigners
, WorthlessSigners
,
205 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
208 // In this case, something bad probably happened, so we just go
209 // with what the other method gave us for an error message.
210 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
214 if (!BadSigners
.empty())
216 errmsg
+= _("The following signatures were invalid:\n");
217 for (vector
<string
>::iterator I
= BadSigners
.begin();
218 I
!= BadSigners
.end(); ++I
)
219 errmsg
+= (*I
+ "\n");
221 if (!WorthlessSigners
.empty())
223 errmsg
+= _("The following signatures were invalid:\n");
224 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
225 I
!= WorthlessSigners
.end(); ++I
)
226 errmsg
+= (*I
+ "\n");
228 if (!NoPubKeySigners
.empty())
230 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
231 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
232 I
!= NoPubKeySigners
.end(); ++I
)
233 errmsg
+= (*I
+ "\n");
236 // this is only fatal if we have no good sigs or if we have at
237 // least one bad signature. good signatures and NoPubKey signatures
238 // happen easily when a file is signed with multiple signatures
239 if(GoodSigners
.empty() or !BadSigners
.empty())
240 return _error
->Error("%s", errmsg
.c_str());
243 // Just pass the raw output up, because passing it as a real data
244 // structure is too difficult with the method stuff. We keep it
245 // as three separate vectors for future extensibility.
246 Res
.GPGVOutput
= GoodSigners
;
247 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
248 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
251 if (_config
->FindB("Debug::Acquire::gpgv", false))
253 std::clog
<< "gpgv succeeded\n";
262 setlocale(LC_ALL
, "");