]>
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 vector
<string
> &GoodSigners
,
41 vector
<string
> &BadSigners
,
42 vector
<string
> &WorthlessSigners
,
43 vector
<string
> &NoPubKeySigners
);
46 virtual bool Fetch(FetchItem
*Itm
);
47 virtual bool Configuration(string Message
);
50 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
53 bool GPGVMethod::Configuration(string Message
)
55 if (pkgAcqMethod::Configuration(Message
) == false)
63 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
64 vector
<string
> &GoodSigners
,
65 vector
<string
> &BadSigners
,
66 vector
<string
> &WorthlessSigners
,
67 vector
<string
> &NoPubKeySigners
)
69 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
72 std::clog
<< "inside VerifyGetSigners" << std::endl
;
77 return "Couldn't create pipe";
81 return string("Couldn't spawn new process") + strerror(errno
);
83 ExecGPGV(outfile
, file
, 3, fd
);
86 FILE *pipein
= fdopen(fd
[0], "r");
88 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
89 size_t buffersize
= 64;
90 char *buffer
= (char *) malloc(buffersize
);
97 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
99 if (bufferoff
== buffersize
)
101 char* newBuffer
= (char *) realloc(buffer
, buffersize
*= 2);
102 if (newBuffer
== NULL
)
105 return "Couldn't allocate a buffer big enough for reading";
109 *(buffer
+bufferoff
) = c
;
112 if (bufferoff
== 0 && c
== EOF
)
114 *(buffer
+bufferoff
) = '\0';
117 std::clog
<< "Read: " << buffer
<< std::endl
;
119 // Push the data into three separate vectors, which
120 // we later concatenate. They're kept separate so
121 // if we improve the apt method communication stuff later
122 // it will be better.
123 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
126 std::clog
<< "Got BADSIG! " << std::endl
;
127 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
130 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
133 std::clog
<< "Got NO_PUBKEY " << std::endl
;
134 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
136 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
139 std::clog
<< "Got NODATA! " << std::endl
;
140 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
142 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
145 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
146 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
148 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
151 std::clog
<< "Got REVKEYSIG! " << std::endl
;
152 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
154 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
156 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
157 char *p
= sig
+ sizeof("GOODSIG");
158 while (*p
&& isxdigit(*p
))
162 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
163 GoodSigners
.push_back(string(sig
));
170 waitpid(pid
, &status
, 0);
173 ioprintf(std::clog
, "gpgv exited with status %i\n", WEXITSTATUS(status
));
176 if (WEXITSTATUS(status
) == 0)
178 if (GoodSigners
.empty())
179 return _("Internal error: Good signature, but could not determine key fingerprint?!");
182 else if (WEXITSTATUS(status
) == 1)
183 return _("At least one invalid signature was encountered.");
184 else if (WEXITSTATUS(status
) == 111)
185 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
186 else if (WEXITSTATUS(status
) == 112)
188 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
190 //TRANSLATORS: %s is a single techy word like 'NODATA'
191 strprintf(errmsg
, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
195 return _("Unknown error executing apt-key");
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 apt-key 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
<< "apt-key succeeded\n";
274 setlocale(LC_ALL
, "");