]>
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>
8 #include <apt-pkg/configuration.h>
24 #define GNUPGPREFIX "[GNUPG:]"
25 #define GNUPGBADSIG "[GNUPG:] BADSIG"
26 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
27 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
28 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
29 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
30 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
31 #define GNUPGNODATA "[GNUPG:] NODATA"
33 class GPGVMethod
: public pkgAcqMethod
36 string
VerifyGetSigners(const char *file
, const char *outfile
,
37 vector
<string
> &GoodSigners
,
38 vector
<string
> &BadSigners
,
39 vector
<string
> &WorthlessSigners
,
40 vector
<string
> &NoPubKeySigners
);
43 virtual bool Fetch(FetchItem
*Itm
);
47 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
50 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
51 vector
<string
> &GoodSigners
,
52 vector
<string
> &BadSigners
,
53 vector
<string
> &WorthlessSigners
,
54 vector
<string
> &NoPubKeySigners
)
56 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
57 // setup a (empty) stringstream for formating the return value
58 std::stringstream ret
;
62 std::clog
<< "inside VerifyGetSigners" << std::endl
;
67 return "Couldn't create pipe";
71 return string("Couldn't spawn new process") + strerror(errno
);
74 _error
->PushToStack();
75 bool const success
= SigVerify::RunGPGV(outfile
, file
, 3, fd
);
79 _error
->PopMessage(errmsg
);
80 _error
->RevertToStack();
83 _error
->RevertToStack();
88 FILE *pipein
= fdopen(fd
[0], "r");
90 // Loop over the output of gpgv, and check the signatures.
91 size_t buffersize
= 64;
92 char *buffer
= (char *) malloc(buffersize
);
99 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
101 if (bufferoff
== buffersize
)
103 char* newBuffer
= (char *) realloc(buffer
, buffersize
*= 2);
104 if (newBuffer
== NULL
)
107 return "Couldn't allocate a buffer big enough for reading";
111 *(buffer
+bufferoff
) = c
;
114 if (bufferoff
== 0 && c
== EOF
)
116 *(buffer
+bufferoff
) = '\0';
119 std::clog
<< "Read: " << buffer
<< std::endl
;
121 // Push the data into three separate vectors, which
122 // we later concatenate. They're kept separate so
123 // if we improve the apt method communication stuff later
124 // it will be better.
125 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
128 std::clog
<< "Got BADSIG! " << std::endl
;
129 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
132 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
135 std::clog
<< "Got NO_PUBKEY " << std::endl
;
136 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
138 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
141 std::clog
<< "Got NODATA! " << std::endl
;
142 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
144 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
147 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
148 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
150 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
153 std::clog
<< "Got REVKEYSIG! " << std::endl
;
154 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
156 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
158 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
159 char *p
= sig
+ sizeof("GOODSIG");
160 while (*p
&& isxdigit(*p
))
164 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
165 GoodSigners
.push_back(string(sig
));
171 waitpid(pid
, &status
, 0);
174 std::clog
<< "gpgv exited\n";
177 if (WEXITSTATUS(status
) == 0)
179 if (GoodSigners
.empty())
180 return _("Internal error: Good signature, but could not determine key fingerprint?!");
183 else if (WEXITSTATUS(status
) == 1)
185 return _("At least one invalid signature was encountered.");
187 else if (WEXITSTATUS(status
) == 111)
189 ioprintf(ret
, _("Could not execute 'gpgv' to verify signature (is gpgv installed?)"));
194 return _("Unknown error executing gpgv");
198 bool GPGVMethod::Fetch(FetchItem
*Itm
)
201 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
203 vector
<string
> GoodSigners
;
204 vector
<string
> BadSigners
;
205 // a worthless signature is a expired or revoked one
206 vector
<string
> WorthlessSigners
;
207 vector
<string
> NoPubKeySigners
;
210 Res
.Filename
= Itm
->DestFile
;
213 // Run gpgv on file, extract contents and get the key ID of the signer
214 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
215 GoodSigners
, BadSigners
, WorthlessSigners
,
217 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
220 // In this case, something bad probably happened, so we just go
221 // with what the other method gave us for an error message.
222 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
226 if (!BadSigners
.empty())
228 errmsg
+= _("The following signatures were invalid:\n");
229 for (vector
<string
>::iterator I
= BadSigners
.begin();
230 I
!= BadSigners
.end(); ++I
)
231 errmsg
+= (*I
+ "\n");
233 if (!WorthlessSigners
.empty())
235 errmsg
+= _("The following signatures were invalid:\n");
236 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
237 I
!= WorthlessSigners
.end(); ++I
)
238 errmsg
+= (*I
+ "\n");
240 if (!NoPubKeySigners
.empty())
242 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
243 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
244 I
!= NoPubKeySigners
.end(); ++I
)
245 errmsg
+= (*I
+ "\n");
248 // this is only fatal if we have no good sigs or if we have at
249 // least one bad signature. good signatures and NoPubKey signatures
250 // happen easily when a file is signed with multiple signatures
251 if(GoodSigners
.empty() or !BadSigners
.empty())
252 return _error
->Error("%s", errmsg
.c_str());
255 // Just pass the raw output up, because passing it as a real data
256 // structure is too difficult with the method stuff. We keep it
257 // as three separate vectors for future extensibility.
258 Res
.GPGVOutput
= GoodSigners
;
259 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
260 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
263 if (_config
->FindB("Debug::Acquire::gpgv", false))
265 std::clog
<< "gpgv succeeded\n";
274 setlocale(LC_ALL
, "");