]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
3 #include <apt-pkg/error.h>
4 #include <apt-pkg/acquire-method.h>
5 #include <apt-pkg/strutl.h>
6 #include <apt-pkg/fileutl.h>
7 #include <apt-pkg/indexcopy.h>
8 #include <apt-pkg/configuration.h>
9 #include <apt-pkg/gpgv.h>
24 #define GNUPGPREFIX "[GNUPG:]"
25 #define GNUPGBADSIG "[GNUPG:] BADSIG"
26 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
27 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
28 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
29 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
30 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
31 #define GNUPGNODATA "[GNUPG:] NODATA"
33 class GPGVMethod
: public pkgAcqMethod
36 string
VerifyGetSigners(const char *file
, const char *outfile
,
37 vector
<string
> &GoodSigners
,
38 vector
<string
> &BadSigners
,
39 vector
<string
> &WorthlessSigners
,
40 vector
<string
> &NoPubKeySigners
);
43 virtual bool Fetch(FetchItem
*Itm
);
47 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
50 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
51 vector
<string
> &GoodSigners
,
52 vector
<string
> &BadSigners
,
53 vector
<string
> &WorthlessSigners
,
54 vector
<string
> &NoPubKeySigners
)
56 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
59 std::clog
<< "inside VerifyGetSigners" << std::endl
;
64 return "Couldn't create pipe";
68 return string("Couldn't spawn new process") + strerror(errno
);
70 ExecGPGV(outfile
, file
, 3, fd
);
73 FILE *pipein
= fdopen(fd
[0], "r");
75 // Loop over the output of gpgv, and check the signatures.
76 size_t buffersize
= 64;
77 char *buffer
= (char *) malloc(buffersize
);
84 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
86 if (bufferoff
== buffersize
)
88 char* newBuffer
= (char *) realloc(buffer
, buffersize
*= 2);
89 if (newBuffer
== NULL
)
92 return "Couldn't allocate a buffer big enough for reading";
96 *(buffer
+bufferoff
) = c
;
99 if (bufferoff
== 0 && c
== EOF
)
101 *(buffer
+bufferoff
) = '\0';
104 std::clog
<< "Read: " << buffer
<< std::endl
;
106 // Push the data into three separate vectors, which
107 // we later concatenate. They're kept separate so
108 // if we improve the apt method communication stuff later
109 // it will be better.
110 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
113 std::clog
<< "Got BADSIG! " << std::endl
;
114 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
117 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
120 std::clog
<< "Got NO_PUBKEY " << std::endl
;
121 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
123 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
126 std::clog
<< "Got NODATA! " << std::endl
;
127 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
129 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
132 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
133 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
135 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
138 std::clog
<< "Got REVKEYSIG! " << std::endl
;
139 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
141 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
143 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
144 char *p
= sig
+ sizeof("GOODSIG");
145 while (*p
&& isxdigit(*p
))
149 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
150 GoodSigners
.push_back(string(sig
));
157 waitpid(pid
, &status
, 0);
160 std::clog
<< "gpgv exited\n";
163 if (WEXITSTATUS(status
) == 0)
165 if (GoodSigners
.empty())
166 return _("Internal error: Good signature, but could not determine key fingerprint?!");
169 else if (WEXITSTATUS(status
) == 1)
170 return _("At least one invalid signature was encountered.");
171 else if (WEXITSTATUS(status
) == 111)
172 return _("Could not execute 'gpgv' to verify signature (is gpgv installed?)");
173 else if (WEXITSTATUS(status
) == 112)
175 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
177 //TRANSLATORS: %s is a single techy word like 'NODATA'
178 strprintf(errmsg
, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
182 return _("Unknown error executing gpgv");
185 bool GPGVMethod::Fetch(FetchItem
*Itm
)
188 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
190 vector
<string
> GoodSigners
;
191 vector
<string
> BadSigners
;
192 // a worthless signature is a expired or revoked one
193 vector
<string
> WorthlessSigners
;
194 vector
<string
> NoPubKeySigners
;
197 Res
.Filename
= Itm
->DestFile
;
200 // Run gpgv on file, extract contents and get the key ID of the signer
201 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
202 GoodSigners
, BadSigners
, WorthlessSigners
,
204 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
207 // In this case, something bad probably happened, so we just go
208 // with what the other method gave us for an error message.
209 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
213 if (!BadSigners
.empty())
215 errmsg
+= _("The following signatures were invalid:\n");
216 for (vector
<string
>::iterator I
= BadSigners
.begin();
217 I
!= BadSigners
.end(); ++I
)
218 errmsg
+= (*I
+ "\n");
220 if (!WorthlessSigners
.empty())
222 errmsg
+= _("The following signatures were invalid:\n");
223 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
224 I
!= WorthlessSigners
.end(); ++I
)
225 errmsg
+= (*I
+ "\n");
227 if (!NoPubKeySigners
.empty())
229 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
230 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
231 I
!= NoPubKeySigners
.end(); ++I
)
232 errmsg
+= (*I
+ "\n");
235 // this is only fatal if we have no good sigs or if we have at
236 // least one bad signature. good signatures and NoPubKey signatures
237 // happen easily when a file is signed with multiple signatures
238 if(GoodSigners
.empty() or !BadSigners
.empty())
239 return _error
->Error("%s", errmsg
.c_str());
242 // Just pass the raw output up, because passing it as a real data
243 // structure is too difficult with the method stuff. We keep it
244 // as three separate vectors for future extensibility.
245 Res
.GPGVOutput
= GoodSigners
;
246 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
247 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
250 if (_config
->FindB("Debug::Acquire::gpgv", false))
252 std::clog
<< "gpgv succeeded\n";
261 setlocale(LC_ALL
, "");