]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
3 #include <apt-pkg/acquire-method.h>
4 #include <apt-pkg/configuration.h>
5 #include <apt-pkg/error.h>
6 #include <apt-pkg/gpgv.h>
7 #include <apt-pkg/strutl.h>
26 #define GNUPGPREFIX "[GNUPG:]"
27 #define GNUPGBADSIG "[GNUPG:] BADSIG"
28 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
29 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
30 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
31 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
32 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
33 #define GNUPGNODATA "[GNUPG:] NODATA"
35 class GPGVMethod
: public pkgAcqMethod
38 string
VerifyGetSigners(const char *file
, const char *outfile
,
39 vector
<string
> &GoodSigners
,
40 vector
<string
> &BadSigners
,
41 vector
<string
> &WorthlessSigners
,
42 vector
<string
> &NoPubKeySigners
);
45 virtual bool Fetch(FetchItem
*Itm
);
49 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
52 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
53 vector
<string
> &GoodSigners
,
54 vector
<string
> &BadSigners
,
55 vector
<string
> &WorthlessSigners
,
56 vector
<string
> &NoPubKeySigners
)
58 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
61 std::clog
<< "inside VerifyGetSigners" << std::endl
;
66 return "Couldn't create pipe";
70 return string("Couldn't spawn new process") + strerror(errno
);
72 ExecGPGV(outfile
, file
, 3, fd
);
75 FILE *pipein
= fdopen(fd
[0], "r");
77 // Loop over the output of gpgv, and check the signatures.
78 size_t buffersize
= 64;
79 char *buffer
= (char *) malloc(buffersize
);
86 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
88 if (bufferoff
== buffersize
)
90 char* newBuffer
= (char *) realloc(buffer
, buffersize
*= 2);
91 if (newBuffer
== NULL
)
94 return "Couldn't allocate a buffer big enough for reading";
98 *(buffer
+bufferoff
) = c
;
101 if (bufferoff
== 0 && c
== EOF
)
103 *(buffer
+bufferoff
) = '\0';
106 std::clog
<< "Read: " << buffer
<< std::endl
;
108 // Push the data into three separate vectors, which
109 // we later concatenate. They're kept separate so
110 // if we improve the apt method communication stuff later
111 // it will be better.
112 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
115 std::clog
<< "Got BADSIG! " << std::endl
;
116 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
119 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
122 std::clog
<< "Got NO_PUBKEY " << std::endl
;
123 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
125 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
128 std::clog
<< "Got NODATA! " << std::endl
;
129 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
131 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
134 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
135 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
137 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
140 std::clog
<< "Got REVKEYSIG! " << std::endl
;
141 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
143 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
145 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
146 char *p
= sig
+ sizeof("GOODSIG");
147 while (*p
&& isxdigit(*p
))
151 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
152 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)
172 return _("At least one invalid signature was encountered.");
173 else if (WEXITSTATUS(status
) == 111)
174 return _("Could not execute 'gpgv' to verify signature (is gpgv installed?)");
175 else if (WEXITSTATUS(status
) == 112)
177 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
179 //TRANSLATORS: %s is a single techy word like 'NODATA'
180 strprintf(errmsg
, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
184 return _("Unknown error executing gpgv");
187 bool GPGVMethod::Fetch(FetchItem
*Itm
)
190 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
192 vector
<string
> GoodSigners
;
193 vector
<string
> BadSigners
;
194 // a worthless signature is a expired or revoked one
195 vector
<string
> WorthlessSigners
;
196 vector
<string
> NoPubKeySigners
;
199 Res
.Filename
= Itm
->DestFile
;
202 // Run gpgv on file, extract contents and get the key ID of the signer
203 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
204 GoodSigners
, BadSigners
, WorthlessSigners
,
206 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
209 // In this case, something bad probably happened, so we just go
210 // with what the other method gave us for an error message.
211 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
215 if (!BadSigners
.empty())
217 errmsg
+= _("The following signatures were invalid:\n");
218 for (vector
<string
>::iterator I
= BadSigners
.begin();
219 I
!= BadSigners
.end(); ++I
)
220 errmsg
+= (*I
+ "\n");
222 if (!WorthlessSigners
.empty())
224 errmsg
+= _("The following signatures were invalid:\n");
225 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
226 I
!= WorthlessSigners
.end(); ++I
)
227 errmsg
+= (*I
+ "\n");
229 if (!NoPubKeySigners
.empty())
231 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
232 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
233 I
!= NoPubKeySigners
.end(); ++I
)
234 errmsg
+= (*I
+ "\n");
237 // this is only fatal if we have no good sigs or if we have at
238 // least one bad signature. good signatures and NoPubKey signatures
239 // happen easily when a file is signed with multiple signatures
240 if(GoodSigners
.empty() or !BadSigners
.empty())
241 return _error
->Error("%s", errmsg
.c_str());
244 // Just pass the raw output up, because passing it as a real data
245 // structure is too difficult with the method stuff. We keep it
246 // as three separate vectors for future extensibility.
247 Res
.GPGVOutput
= GoodSigners
;
248 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
249 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
252 if (_config
->FindB("Debug::Acquire::gpgv", false))
254 std::clog
<< "gpgv succeeded\n";
263 setlocale(LC_ALL
, "");