]>
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
);
50 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
53 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
54 vector
<string
> &GoodSigners
,
55 vector
<string
> &BadSigners
,
56 vector
<string
> &WorthlessSigners
,
57 vector
<string
> &NoPubKeySigners
)
59 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
62 std::clog
<< "inside VerifyGetSigners" << std::endl
;
67 return "Couldn't create pipe";
71 return string("Couldn't spawn new process") + strerror(errno
);
73 ExecGPGV(outfile
, file
, 3, fd
);
76 FILE *pipein
= fdopen(fd
[0], "r");
78 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
79 size_t buffersize
= 64;
80 char *buffer
= (char *) malloc(buffersize
);
87 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
89 if (bufferoff
== buffersize
)
91 char* newBuffer
= (char *) realloc(buffer
, buffersize
*= 2);
92 if (newBuffer
== NULL
)
95 return "Couldn't allocate a buffer big enough for reading";
99 *(buffer
+bufferoff
) = c
;
102 if (bufferoff
== 0 && c
== EOF
)
104 *(buffer
+bufferoff
) = '\0';
107 std::clog
<< "Read: " << buffer
<< std::endl
;
109 // Push the data into three separate vectors, which
110 // we later concatenate. They're kept separate so
111 // if we improve the apt method communication stuff later
112 // it will be better.
113 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
116 std::clog
<< "Got BADSIG! " << std::endl
;
117 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
120 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
123 std::clog
<< "Got NO_PUBKEY " << std::endl
;
124 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
126 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
129 std::clog
<< "Got NODATA! " << std::endl
;
130 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
132 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
135 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
136 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
138 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
141 std::clog
<< "Got REVKEYSIG! " << std::endl
;
142 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
144 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
146 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
147 char *p
= sig
+ sizeof("GOODSIG");
148 while (*p
&& isxdigit(*p
))
152 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
153 GoodSigners
.push_back(string(sig
));
160 waitpid(pid
, &status
, 0);
163 ioprintf(std::clog
, "gpgv exited with status %i\n", WEXITSTATUS(status
));
166 if (WEXITSTATUS(status
) == 0)
168 if (GoodSigners
.empty())
169 return _("Internal error: Good signature, but could not determine key fingerprint?!");
172 else if (WEXITSTATUS(status
) == 1)
173 return _("At least one invalid signature was encountered.");
174 else if (WEXITSTATUS(status
) == 111)
175 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
176 else if (WEXITSTATUS(status
) == 112)
178 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
180 //TRANSLATORS: %s is a single techy word like 'NODATA'
181 strprintf(errmsg
, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
185 return _("Unknown error executing apt-key");
188 bool GPGVMethod::Fetch(FetchItem
*Itm
)
191 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
193 vector
<string
> GoodSigners
;
194 vector
<string
> BadSigners
;
195 // a worthless signature is a expired or revoked one
196 vector
<string
> WorthlessSigners
;
197 vector
<string
> NoPubKeySigners
;
200 Res
.Filename
= Itm
->DestFile
;
203 // Run apt-key on file, extract contents and get the key ID of the signer
204 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
205 GoodSigners
, BadSigners
, WorthlessSigners
,
207 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
210 // In this case, something bad probably happened, so we just go
211 // with what the other method gave us for an error message.
212 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
216 if (!BadSigners
.empty())
218 errmsg
+= _("The following signatures were invalid:\n");
219 for (vector
<string
>::iterator I
= BadSigners
.begin();
220 I
!= BadSigners
.end(); ++I
)
221 errmsg
+= (*I
+ "\n");
223 if (!WorthlessSigners
.empty())
225 errmsg
+= _("The following signatures were invalid:\n");
226 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
227 I
!= WorthlessSigners
.end(); ++I
)
228 errmsg
+= (*I
+ "\n");
230 if (!NoPubKeySigners
.empty())
232 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
233 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
234 I
!= NoPubKeySigners
.end(); ++I
)
235 errmsg
+= (*I
+ "\n");
238 // this is only fatal if we have no good sigs or if we have at
239 // least one bad signature. good signatures and NoPubKey signatures
240 // happen easily when a file is signed with multiple signatures
241 if(GoodSigners
.empty() or !BadSigners
.empty())
242 return _error
->Error("%s", errmsg
.c_str());
245 // Just pass the raw output up, because passing it as a real data
246 // structure is too difficult with the method stuff. We keep it
247 // as three separate vectors for future extensibility.
248 Res
.GPGVOutput
= GoodSigners
;
249 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
250 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
253 if (_config
->FindB("Debug::Acquire::gpgv", false))
255 std::clog
<< "apt-key succeeded\n";
264 setlocale(LC_ALL
, "");
268 Mth
.DropPrivsOrDie();