]>
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>
8 #include <apt-pkg/fileutl.h>
27 #define GNUPGPREFIX "[GNUPG:]"
28 #define GNUPGBADSIG "[GNUPG:] BADSIG"
29 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
30 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
31 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
32 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
33 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
34 #define GNUPGNODATA "[GNUPG:] NODATA"
36 class GPGVMethod
: public pkgAcqMethod
39 string
VerifyGetSigners(const char *file
, const char *outfile
,
40 std::string
const &key
,
41 vector
<string
> &GoodSigners
,
42 vector
<string
> &BadSigners
,
43 vector
<string
> &WorthlessSigners
,
44 vector
<string
> &NoPubKeySigners
);
47 virtual bool URIAcquire(std::string
const &Message
, FetchItem
*Itm
);
48 virtual bool Configuration(string Message
);
51 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
54 bool GPGVMethod::Configuration(string Message
)
56 if (pkgAcqMethod::Configuration(Message
) == false)
64 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
65 std::string
const &key
,
66 vector
<string
> &GoodSigners
,
67 vector
<string
> &BadSigners
,
68 vector
<string
> &WorthlessSigners
,
69 vector
<string
> &NoPubKeySigners
)
71 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
74 std::clog
<< "inside VerifyGetSigners" << std::endl
;
79 return "Couldn't create pipe";
83 return string("Couldn't spawn new process") + strerror(errno
);
85 ExecGPGV(outfile
, file
, 3, fd
, key
);
88 FILE *pipein
= fdopen(fd
[0], "r");
90 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
91 size_t buffersize
= 0;
95 if (getline(&buffer
, &buffersize
, pipein
) == -1)
98 std::clog
<< "Read: " << buffer
<< std::endl
;
100 // Push the data into three separate vectors, which
101 // we later concatenate. They're kept separate so
102 // if we improve the apt method communication stuff later
103 // it will be better.
104 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
107 std::clog
<< "Got BADSIG! " << std::endl
;
108 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
111 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
114 std::clog
<< "Got NO_PUBKEY " << std::endl
;
115 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
117 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
120 std::clog
<< "Got NODATA! " << std::endl
;
121 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
123 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
126 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
127 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
129 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
132 std::clog
<< "Got REVKEYSIG! " << std::endl
;
133 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
135 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
137 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
138 char *p
= sig
+ sizeof("GOODSIG");
139 while (*p
&& isxdigit(*p
))
143 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
144 GoodSigners
.push_back(string(sig
));
151 waitpid(pid
, &status
, 0);
154 ioprintf(std::clog
, "gpgv exited with status %i\n", WEXITSTATUS(status
));
157 if (WEXITSTATUS(status
) == 0)
159 if (GoodSigners
.empty())
160 return _("Internal error: Good signature, but could not determine key fingerprint?!");
163 else if (WEXITSTATUS(status
) == 1)
164 return _("At least one invalid signature was encountered.");
165 else if (WEXITSTATUS(status
) == 111)
166 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
167 else if (WEXITSTATUS(status
) == 112)
169 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
171 //TRANSLATORS: %s is a single techy word like 'NODATA'
172 strprintf(errmsg
, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
176 return _("Unknown error executing apt-key");
179 bool GPGVMethod::URIAcquire(std::string
const &Message
, FetchItem
*Itm
)
181 URI
const Get
= Itm
->Uri
;
182 string
const Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
183 std::string
const key
= LookupTag(Message
, "Signed-By");
184 vector
<string
> GoodSigners
;
185 vector
<string
> BadSigners
;
186 // a worthless signature is a expired or revoked one
187 vector
<string
> WorthlessSigners
;
188 vector
<string
> NoPubKeySigners
;
191 Res
.Filename
= Itm
->DestFile
;
194 // Run apt-key on file, extract contents and get the key ID of the signer
195 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(), key
,
196 GoodSigners
, BadSigners
, WorthlessSigners
,
198 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
201 // In this case, something bad probably happened, so we just go
202 // with what the other method gave us for an error message.
203 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
207 if (!BadSigners
.empty())
209 errmsg
+= _("The following signatures were invalid:\n");
210 for (vector
<string
>::iterator I
= BadSigners
.begin();
211 I
!= BadSigners
.end(); ++I
)
212 errmsg
+= (*I
+ "\n");
214 if (!WorthlessSigners
.empty())
216 errmsg
+= _("The following signatures were invalid:\n");
217 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
218 I
!= WorthlessSigners
.end(); ++I
)
219 errmsg
+= (*I
+ "\n");
221 if (!NoPubKeySigners
.empty())
223 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
224 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
225 I
!= NoPubKeySigners
.end(); ++I
)
226 errmsg
+= (*I
+ "\n");
229 // this is only fatal if we have no good sigs or if we have at
230 // least one bad signature. good signatures and NoPubKey signatures
231 // happen easily when a file is signed with multiple signatures
232 if(GoodSigners
.empty() or !BadSigners
.empty())
233 return _error
->Error("%s", errmsg
.c_str());
236 // Just pass the raw output up, because passing it as a real data
237 // structure is too difficult with the method stuff. We keep it
238 // as three separate vectors for future extensibility.
239 Res
.GPGVOutput
= GoodSigners
;
240 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
241 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
244 if (_config
->FindB("Debug::Acquire::gpgv", false))
246 std::clog
<< "apt-key succeeded\n";
255 setlocale(LC_ALL
, "");