]>
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>
16 #define GNUPGPREFIX "[GNUPG:]"
17 #define GNUPGBADSIG "[GNUPG:] BADSIG"
18 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
19 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
20 #define GNUPGNODATA "[GNUPG:] NODATA"
22 class GPGVMethod
: public pkgAcqMethod
25 string
VerifyGetSigners(const char *file
, const char *outfile
,
26 vector
<string
> &GoodSigners
, vector
<string
> &BadSigners
,
27 vector
<string
> &NoPubKeySigners
);
30 virtual bool Fetch(FetchItem
*Itm
);
34 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
37 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
38 vector
<string
> &GoodSigners
,
39 vector
<string
> &BadSigners
,
40 vector
<string
> &NoPubKeySigners
)
42 // setup a (empty) stringstream for formating the return value
43 std::stringstream ret
;
46 if (_config
->FindB("Debug::Acquire::gpgv", false))
48 std::cerr
<< "inside VerifyGetSigners" << std::endl
;
55 string gpgvpath
= _config
->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
56 string pubringpath
= _config
->Find("APT::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg");
57 if (_config
->FindB("Debug::Acquire::gpgv", false))
59 std::cerr
<< "gpgv path: " << gpgvpath
<< std::endl
;
60 std::cerr
<< "Keyring path: " << pubringpath
<< std::endl
;
63 if (stat(pubringpath
.c_str(), &buff
) != 0)
65 ioprintf(ret
, _("Couldn't access keyring: '%s'"), strerror(errno
));
70 return "Couldn't create pipe";
76 return string("Couldn't spawn new process") + strerror(errno
);
80 const char *Args
[400];
83 Args
[i
++] = gpgvpath
.c_str();
84 Args
[i
++] = "--status-fd";
86 Args
[i
++] = "--ignore-time-conflict";
87 Args
[i
++] = "--keyring";
88 Args
[i
++] = pubringpath
.c_str();
90 Configuration::Item
const *Opts
;
91 Opts
= _config
->Tree("Acquire::gpgv::Options");
95 for (; Opts
!= 0; Opts
= Opts
->Next
)
97 if (Opts
->Value
.empty() == true)
99 Args
[i
++] = Opts
->Value
.c_str();
101 std::cerr
<< _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl
;
110 if (_config
->FindB("Debug::Acquire::gpgv", false))
112 std::cerr
<< "Preparing to exec: " << gpgvpath
;
113 for(unsigned int j
=0;Args
[j
] != NULL
; j
++)
114 std::cerr
<< " " << Args
[j
];
115 std::cerr
<< std::endl
;
117 int nullfd
= open("/dev/null", O_RDONLY
);
119 // Redirect output to /dev/null; we read from the status fd
120 dup2(nullfd
, STDOUT_FILENO
);
121 dup2(nullfd
, STDERR_FILENO
);
122 // Redirect the pipe to the status fd (3)
125 putenv((char *)"LANG=");
126 putenv((char *)"LC_ALL=");
127 putenv((char *)"LC_MESSAGES=");
128 execvp(gpgvpath
.c_str(), (char **)Args
);
134 pipein
= fdopen(fd
[0], "r");
136 // Loop over the output of gpgv, and check the signatures.
137 size_t buffersize
= 64;
138 char *buffer
= (char *) malloc(buffersize
);
139 size_t bufferoff
= 0;
144 // Read a line. Sigh.
145 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
147 if (bufferoff
== buffersize
)
148 buffer
= (char *) realloc(buffer
, buffersize
*= 2);
149 *(buffer
+bufferoff
) = c
;
152 if (bufferoff
== 0 && c
== EOF
)
154 *(buffer
+bufferoff
) = '\0';
156 if (_config
->FindB("Debug::Acquire::gpgv", false))
157 std::cerr
<< "Read: " << buffer
<< std::endl
;
159 // Push the data into three separate vectors, which
160 // we later concatenate. They're kept separate so
161 // if we improve the apt method communication stuff later
162 // it will be better.
163 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
165 if (_config
->FindB("Debug::Acquire::gpgv", false))
166 std::cerr
<< "Got BADSIG! " << std::endl
;
167 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
170 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
172 if (_config
->FindB("Debug::Acquire::gpgv", false))
173 std::cerr
<< "Got NO_PUBKEY " << std::endl
;
174 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
176 if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
178 if (_config
->FindB("Debug::Acquire::gpgv", false))
179 std::cerr
<< "Got NODATA! " << std::endl
;
180 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
182 if (strncmp(buffer
, GNUPGVALIDSIG
, sizeof(GNUPGVALIDSIG
)-1) == 0)
184 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
185 char *p
= sig
+ sizeof("VALIDSIG");
186 while (*p
&& isxdigit(*p
))
189 if (_config
->FindB("Debug::Acquire::gpgv", false))
190 std::cerr
<< "Got VALIDSIG, key ID:" << sig
<< std::endl
;
191 GoodSigners
.push_back(string(sig
));
196 waitpid(pid
, &status
, 0);
197 if (_config
->FindB("Debug::Acquire::gpgv", false))
199 std::cerr
<< "gpgv exited\n";
202 if (WEXITSTATUS(status
) == 0)
204 if (GoodSigners
.empty())
205 return _("Internal error: Good signature, but could not determine key fingerprint?!");
208 else if (WEXITSTATUS(status
) == 1)
210 return _("At least one invalid signature was encountered.");
212 else if (WEXITSTATUS(status
) == 111)
214 ioprintf(ret
, _("Could not execute '%s' to verify signature (is gnupg installed?)"), gpgvpath
.c_str());
219 return _("Unknown error executing gpgv");
223 bool GPGVMethod::Fetch(FetchItem
*Itm
)
226 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
228 vector
<string
> GoodSigners
;
229 vector
<string
> BadSigners
;
230 vector
<string
> NoPubKeySigners
;
233 Res
.Filename
= Itm
->DestFile
;
236 // Run gpgv on file, extract contents and get the key ID of the signer
237 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
238 GoodSigners
, BadSigners
, NoPubKeySigners
);
239 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
242 // In this case, something bad probably happened, so we just go
243 // with what the other method gave us for an error message.
244 if (BadSigners
.empty() && NoPubKeySigners
.empty())
248 if (!BadSigners
.empty())
250 errmsg
+= _("The following signatures were invalid:\n");
251 for (vector
<string
>::iterator I
= BadSigners
.begin();
252 I
!= BadSigners
.end(); I
++)
253 errmsg
+= (*I
+ "\n");
255 if (!NoPubKeySigners
.empty())
257 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
258 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
259 I
!= NoPubKeySigners
.end(); I
++)
260 errmsg
+= (*I
+ "\n");
263 // this is only fatal if we have no good sigs or if we have at
264 // least one bad signature. good signatures and NoPubKey signatures
265 // happen easily when a file is signed with multiple signatures
266 if(GoodSigners
.empty() or !BadSigners
.empty())
267 return _error
->Error(errmsg
.c_str());
270 // Just pass the raw output up, because passing it as a real data
271 // structure is too difficult with the method stuff. We keep it
272 // as three separate vectors for future extensibility.
273 Res
.GPGVOutput
= GoodSigners
;
274 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
275 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
278 if (_config
->FindB("Debug::Acquire::gpgv", false))
280 std::cerr
<< "gpgv succeeded\n";
289 setlocale(LC_ALL
, "");