]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
1 #include <apt-pkg/error.h>
2 #include <apt-pkg/acquire-method.h>
3 #include <apt-pkg/strutl.h>
4 #include <apt-pkg/fileutl.h>
5 #include <apt-pkg/indexcopy.h>
18 #define GNUPGPREFIX "[GNUPG:]"
19 #define GNUPGBADSIG "[GNUPG:] BADSIG"
20 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
21 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
22 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
23 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
24 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
25 #define GNUPGNODATA "[GNUPG:] NODATA"
27 class GPGVMethod
: public pkgAcqMethod
30 string
VerifyGetSigners(const char *file
, const char *outfile
,
31 vector
<string
> &GoodSigners
,
32 vector
<string
> &BadSigners
,
33 vector
<string
> &WorthlessSigners
,
34 vector
<string
> &NoPubKeySigners
);
37 virtual bool Fetch(FetchItem
*Itm
);
41 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
44 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
45 vector
<string
> &GoodSigners
,
46 vector
<string
> &BadSigners
,
47 vector
<string
> &WorthlessSigners
,
48 vector
<string
> &NoPubKeySigners
)
50 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
51 // setup a (empty) stringstream for formating the return value
52 std::stringstream ret
;
56 std::clog
<< "inside VerifyGetSigners" << std::endl
;
61 return "Couldn't create pipe";
65 return string("Couldn't spawn new process") + strerror(errno
);
68 if (SigVerify::RunGPGV(outfile
, file
, 3, fd
) == false)
70 // TRANSLATOR: %s is the trusted keyring parts directory
71 ioprintf(ret
, _("No keyring installed in %s."),
72 _config
->FindDir("Dir::Etc::TrustedParts", "/etc/apt/trusted.gpg.d").c_str());
79 FILE *pipein
= fdopen(fd
[0], "r");
81 // Loop over the output of gpgv, and check the signatures.
82 size_t buffersize
= 64;
83 char *buffer
= (char *) malloc(buffersize
);
90 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
92 if (bufferoff
== buffersize
)
93 buffer
= (char *) realloc(buffer
, buffersize
*= 2);
94 *(buffer
+bufferoff
) = c
;
97 if (bufferoff
== 0 && c
== EOF
)
99 *(buffer
+bufferoff
) = '\0';
102 std::clog
<< "Read: " << buffer
<< std::endl
;
104 // Push the data into three separate vectors, which
105 // we later concatenate. They're kept separate so
106 // if we improve the apt method communication stuff later
107 // it will be better.
108 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
111 std::clog
<< "Got BADSIG! " << std::endl
;
112 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
115 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
118 std::clog
<< "Got NO_PUBKEY " << std::endl
;
119 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
121 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
124 std::clog
<< "Got NODATA! " << std::endl
;
125 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
127 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
130 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
131 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
133 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
136 std::clog
<< "Got REVKEYSIG! " << std::endl
;
137 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
139 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
141 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
142 char *p
= sig
+ sizeof("GOODSIG");
143 while (*p
&& isxdigit(*p
))
147 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
148 GoodSigners
.push_back(string(sig
));
154 waitpid(pid
, &status
, 0);
157 std::clog
<< "gpgv exited\n";
160 if (WEXITSTATUS(status
) == 0)
162 if (GoodSigners
.empty())
163 return _("Internal error: Good signature, but could not determine key fingerprint?!");
166 else if (WEXITSTATUS(status
) == 1)
168 return _("At least one invalid signature was encountered.");
170 else if (WEXITSTATUS(status
) == 111)
172 ioprintf(ret
, _("Could not execute 'gpgv' to verify signature (is gpgv installed?)"));
177 return _("Unknown error executing gpgv");
181 bool GPGVMethod::Fetch(FetchItem
*Itm
)
184 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
186 vector
<string
> GoodSigners
;
187 vector
<string
> BadSigners
;
188 // a worthless signature is a expired or revoked one
189 vector
<string
> WorthlessSigners
;
190 vector
<string
> NoPubKeySigners
;
193 Res
.Filename
= Itm
->DestFile
;
196 // Run gpgv on file, extract contents and get the key ID of the signer
197 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
198 GoodSigners
, BadSigners
, WorthlessSigners
,
200 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
203 // In this case, something bad probably happened, so we just go
204 // with what the other method gave us for an error message.
205 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
209 if (!BadSigners
.empty())
211 errmsg
+= _("The following signatures were invalid:\n");
212 for (vector
<string
>::iterator I
= BadSigners
.begin();
213 I
!= BadSigners
.end(); I
++)
214 errmsg
+= (*I
+ "\n");
216 if (!WorthlessSigners
.empty())
218 errmsg
+= _("The following signatures were invalid:\n");
219 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
220 I
!= WorthlessSigners
.end(); I
++)
221 errmsg
+= (*I
+ "\n");
223 if (!NoPubKeySigners
.empty())
225 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
226 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
227 I
!= NoPubKeySigners
.end(); I
++)
228 errmsg
+= (*I
+ "\n");
231 // this is only fatal if we have no good sigs or if we have at
232 // least one bad signature. good signatures and NoPubKey signatures
233 // happen easily when a file is signed with multiple signatures
234 if(GoodSigners
.empty() or !BadSigners
.empty())
235 return _error
->Error("%s", errmsg
.c_str());
238 // Just pass the raw output up, because passing it as a real data
239 // structure is too difficult with the method stuff. We keep it
240 // as three separate vectors for future extensibility.
241 Res
.GPGVOutput
= GoodSigners
;
242 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
243 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
246 if (_config
->FindB("Debug::Acquire::gpgv", false))
248 std::clog
<< "gpgv succeeded\n";
257 setlocale(LC_ALL
, "");