]>
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"
21 class GPGVMethod
: public pkgAcqMethod
24 string
VerifyGetSigners(const char *file
, const char *outfile
,
25 vector
<string
> &GoodSigners
, vector
<string
> &BadSigners
,
26 vector
<string
> &NoPubKeySigners
);
29 virtual bool Fetch(FetchItem
*Itm
);
33 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
36 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
37 vector
<string
> &GoodSigners
,
38 vector
<string
> &BadSigners
,
39 vector
<string
> &NoPubKeySigners
)
41 // setup a (empty) stringstream for formating the return value
42 std::stringstream ret
;
45 if (_config
->FindB("Debug::Acquire::gpgv", false))
47 std::cerr
<< "inside VerifyGetSigners" << std::endl
;
54 string gpgvpath
= _config
->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
55 string pubringpath
= _config
->Find("APT::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg");
56 if (_config
->FindB("Debug::Acquire::gpgv", false))
58 std::cerr
<< "gpgv path: " << gpgvpath
<< std::endl
;
59 std::cerr
<< "Keyring path: " << pubringpath
<< std::endl
;
62 if (stat(pubringpath
.c_str(), &buff
) != 0)
64 ioprintf(ret
, _("Couldn't access keyring: '%s'"), strerror(errno
));
69 return "Couldn't create pipe";
75 return string("Couldn't spawn new process") + strerror(errno
);
79 const char *Args
[400];
82 Args
[i
++] = gpgvpath
.c_str();
83 Args
[i
++] = "--status-fd";
85 Args
[i
++] = "--keyring";
86 Args
[i
++] = pubringpath
.c_str();
88 Configuration::Item
const *Opts
;
89 Opts
= _config
->Tree("Acquire::gpgv::Options");
93 for (; Opts
!= 0; Opts
= Opts
->Next
)
95 if (Opts
->Value
.empty() == true)
97 Args
[i
++] = Opts
->Value
.c_str();
99 std::cerr
<< _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl
;
108 if (_config
->FindB("Debug::Acquire::gpgv", false))
110 std::cerr
<< "Preparing to exec: " << gpgvpath
;
111 for(unsigned int j
=0;Args
[j
] != NULL
; j
++)
112 std::cerr
<< " " << Args
[j
];
113 std::cerr
<< std::endl
;
115 int nullfd
= open("/dev/null", O_RDONLY
);
117 // Redirect output to /dev/null; we read from the status fd
118 dup2(nullfd
, STDOUT_FILENO
);
119 dup2(nullfd
, STDERR_FILENO
);
120 // Redirect the pipe to the status fd (3)
125 putenv("LC_MESSAGES=");
126 execvp(gpgvpath
.c_str(), (char **)Args
);
132 pipein
= fdopen(fd
[0], "r");
134 // Loop over the output of gpgv, and check the signatures.
135 size_t buffersize
= 64;
136 char *buffer
= (char *) malloc(buffersize
);
137 size_t bufferoff
= 0;
142 // Read a line. Sigh.
143 while ((c
= getc(pipein
)) != EOF
&& c
!= '\n')
145 if (bufferoff
== buffersize
)
146 buffer
= (char *) realloc(buffer
, buffersize
*= 2);
147 *(buffer
+bufferoff
) = c
;
150 if (bufferoff
== 0 && c
== EOF
)
152 *(buffer
+bufferoff
) = '\0';
154 if (_config
->FindB("Debug::Acquire::gpgv", false))
155 std::cerr
<< "Read: " << buffer
<< std::endl
;
157 // Push the data into three separate vectors, which
158 // we later concatenate. They're kept separate so
159 // if we improve the apt method communication stuff later
160 // it will be better.
161 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
163 if (_config
->FindB("Debug::Acquire::gpgv", false))
164 std::cerr
<< "Got BADSIG! " << std::endl
;
165 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
168 if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
170 if (_config
->FindB("Debug::Acquire::gpgv", false))
171 std::cerr
<< "Got NO_PUBKEY " << std::endl
;
172 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
175 if (strncmp(buffer
, GNUPGVALIDSIG
, sizeof(GNUPGVALIDSIG
)-1) == 0)
177 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
178 char *p
= sig
+ sizeof("VALIDSIG");
179 while (*p
&& isxdigit(*p
))
182 if (_config
->FindB("Debug::Acquire::gpgv", false))
183 std::cerr
<< "Got VALIDSIG, key ID:" << sig
<< std::endl
;
184 GoodSigners
.push_back(string(sig
));
189 waitpid(pid
, &status
, 0);
190 if (_config
->FindB("Debug::Acquire::gpgv", false))
192 std::cerr
<< "gpgv exited\n";
195 if (WEXITSTATUS(status
) == 0)
197 if (GoodSigners
.empty())
198 return _("Internal error: Good signature, but could not determine key fingerprint?!");
201 else if (WEXITSTATUS(status
) == 1)
203 return _("At least one invalid signature was encountered.");
205 else if (WEXITSTATUS(status
) == 111)
207 ioprintf(ret
, _("Could not execute '%s' to verify signature (is gnupg installed?)"), gpgvpath
.c_str());
212 return _("Unknown error executing gpgv");
216 bool GPGVMethod::Fetch(FetchItem
*Itm
)
219 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
221 vector
<string
> GoodSigners
;
222 vector
<string
> BadSigners
;
223 vector
<string
> NoPubKeySigners
;
226 Res
.Filename
= Itm
->DestFile
;
229 // Run gpgv on file, extract contents and get the key ID of the signer
230 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(),
231 GoodSigners
, BadSigners
, NoPubKeySigners
);
232 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
235 // In this case, something bad probably happened, so we just go
236 // with what the other method gave us for an error message.
237 if (BadSigners
.empty() && NoPubKeySigners
.empty())
241 if (!BadSigners
.empty())
243 errmsg
+= _("The following signatures were invalid:\n");
244 for (vector
<string
>::iterator I
= BadSigners
.begin();
245 I
!= BadSigners
.end(); I
++)
246 errmsg
+= (*I
+ "\n");
248 if (!NoPubKeySigners
.empty())
250 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
251 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
252 I
!= NoPubKeySigners
.end(); I
++)
253 errmsg
+= (*I
+ "\n");
256 // this is only fatal if we have no good sigs or if we have at
257 // least one bad signature. good signatures and NoPubKey signatures
258 // happen easily when a file is signed with multiple signatures
259 if(GoodSigners
.empty() or !BadSigners
.empty())
260 return _error
->Error(errmsg
.c_str());
263 // Transfer the modification times
265 if (stat(Path
.c_str(),&Buf
) != 0)
266 return _error
->Errno("stat",_("Failed to stat %s"), Path
.c_str());
268 struct utimbuf TimeBuf
;
269 TimeBuf
.actime
= Buf
.st_atime
;
270 TimeBuf
.modtime
= Buf
.st_mtime
;
271 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
272 return _error
->Errno("utime",_("Failed to set modification time"));
274 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
275 return _error
->Errno("stat",_("Failed to stat"));
277 // Return a Done response
278 Res
.LastModified
= Buf
.st_mtime
;
279 Res
.Size
= Buf
.st_size
;
280 // Just pass the raw output up, because passing it as a real data
281 // structure is too difficult with the method stuff. We keep it
282 // as three separate vectors for future extensibility.
283 Res
.GPGVOutput
= GoodSigners
;
284 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
285 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
288 if (_config
->FindB("Debug::Acquire::gpgv", false))
290 std::cerr
<< "gpgv succeeded\n";
299 setlocale(LC_ALL
, "");