]>
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
= 0;
93 if (getline(&buffer
, &buffersize
, pipein
) == -1)
96 std::clog
<< "Read: " << buffer
<< std::endl
;
98 // Push the data into three separate vectors, which
99 // we later concatenate. They're kept separate so
100 // if we improve the apt method communication stuff later
101 // it will be better.
102 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
105 std::clog
<< "Got BADSIG! " << std::endl
;
106 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
109 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
112 std::clog
<< "Got NO_PUBKEY " << std::endl
;
113 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
115 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
118 std::clog
<< "Got NODATA! " << std::endl
;
119 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
121 if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
124 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
125 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
127 if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
130 std::clog
<< "Got REVKEYSIG! " << std::endl
;
131 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
133 if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
135 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
136 char *p
= sig
+ sizeof("GOODSIG");
137 while (*p
&& isxdigit(*p
))
141 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
142 GoodSigners
.push_back(string(sig
));
149 waitpid(pid
, &status
, 0);
152 ioprintf(std::clog
, "gpgv exited with status %i\n", WEXITSTATUS(status
));
155 if (WEXITSTATUS(status
) == 0)
157 if (GoodSigners
.empty())
158 return _("Internal error: Good signature, but could not determine key fingerprint?!");
161 else if (WEXITSTATUS(status
) == 1)
162 return _("At least one invalid signature was encountered.");
163 else if (WEXITSTATUS(status
) == 111)
164 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
165 else if (WEXITSTATUS(status
) == 112)
167 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
169 //TRANSLATORS: %s is a single techy word like 'NODATA'
170 strprintf(errmsg
, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
174 return _("Unknown error executing apt-key");
177 bool GPGVMethod::Fetch(FetchItem
*Itm
)
180 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
182 vector
<string
> GoodSigners
;
183 vector
<string
> BadSigners
;
184 // a worthless signature is a expired or revoked one
185 vector
<string
> WorthlessSigners
;
186 vector
<string
> NoPubKeySigners
;
189 Res
.Filename
= Itm
->DestFile
;
192 // Run apt-key on file, extract contents and get the key ID of the signer
193 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
194 GoodSigners
, BadSigners
, WorthlessSigners
,
196 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
199 // In this case, something bad probably happened, so we just go
200 // with what the other method gave us for an error message.
201 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
205 if (!BadSigners
.empty())
207 errmsg
+= _("The following signatures were invalid:\n");
208 for (vector
<string
>::iterator I
= BadSigners
.begin();
209 I
!= BadSigners
.end(); ++I
)
210 errmsg
+= (*I
+ "\n");
212 if (!WorthlessSigners
.empty())
214 errmsg
+= _("The following signatures were invalid:\n");
215 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
216 I
!= WorthlessSigners
.end(); ++I
)
217 errmsg
+= (*I
+ "\n");
219 if (!NoPubKeySigners
.empty())
221 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
222 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
223 I
!= NoPubKeySigners
.end(); ++I
)
224 errmsg
+= (*I
+ "\n");
227 // this is only fatal if we have no good sigs or if we have at
228 // least one bad signature. good signatures and NoPubKey signatures
229 // happen easily when a file is signed with multiple signatures
230 if(GoodSigners
.empty() or !BadSigners
.empty())
231 return _error
->Error("%s", errmsg
.c_str());
234 // Just pass the raw output up, because passing it as a real data
235 // structure is too difficult with the method stuff. We keep it
236 // as three separate vectors for future extensibility.
237 Res
.GPGVOutput
= GoodSigners
;
238 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
239 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
242 if (_config
->FindB("Debug::Acquire::gpgv", false))
244 std::clog
<< "apt-key succeeded\n";
253 setlocale(LC_ALL
, "");