]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/gpgv.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Include Files /*{{{*/
11 #include <sys/types.h>
14 #include<apt-pkg/configuration.h>
15 #include<apt-pkg/error.h>
16 #include<apt-pkg/strutl.h>
17 #include<apt-pkg/fileutl.h>
18 #include<apt-pkg/gpgv.h>
22 static char * GenerateTemporaryFileTemplate(const char *basename
) /*{{{*/
24 const char *tmpdir
= getenv("TMPDIR");
33 strprintf(out
, "%s/%s.XXXXXX", tmpdir
, basename
);
34 return strdup(out
.c_str());
37 // ExecGPGV - returns the command needed for verify /*{{{*/
38 // ---------------------------------------------------------------------
39 /* Generating the commandline for calling gpgv is somehow complicated as
40 we need to add multiple keyrings and user supplied options.
41 Also, as gpgv has no options to enforce a certain reduced style of
42 clear-signed files (=the complete content of the file is signed and
43 the content isn't encoded) we do a divide and conquer approach here
44 and split up the clear-signed file in message and signature for gpgv
46 void ExecGPGV(std::string
const &File
, std::string
const &FileGPG
,
47 int const &statusfd
, int fd
[2])
50 std::string
const gpgvpath
= _config
->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
51 // FIXME: remove support for deprecated APT::GPGV setting
52 std::string
const trustedFile
= _config
->Find("APT::GPGV::TrustedKeyring", _config
->FindFile("Dir::Etc::Trusted"));
53 std::string
const trustedPath
= _config
->FindDir("Dir::Etc::TrustedParts");
55 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
59 std::clog
<< "gpgv path: " << gpgvpath
<< std::endl
;
60 std::clog
<< "Keyring file: " << trustedFile
<< std::endl
;
61 std::clog
<< "Keyring path: " << trustedPath
<< std::endl
;
64 std::vector
<std::string
> keyrings
;
65 if (DirectoryExists(trustedPath
))
66 keyrings
= GetListOfFilesInDir(trustedPath
, "gpg", false, true);
67 if (RealFileExists(trustedFile
) == true)
68 keyrings
.push_back(trustedFile
);
70 std::vector
<const char *> Args
;
73 if (keyrings
.empty() == true)
75 // TRANSLATOR: %s is the trusted keyring parts directory
76 ioprintf(std::cerr
, _("No keyring installed in %s."),
77 _config
->FindDir("Dir::Etc::TrustedParts").c_str());
81 Args
.push_back(gpgvpath
.c_str());
82 Args
.push_back("--ignore-time-conflict");
87 Args
.push_back("--status-fd");
88 snprintf(statusfdstr
, sizeof(statusfdstr
), "%i", statusfd
);
89 Args
.push_back(statusfdstr
);
92 for (std::vector
<std::string
>::const_iterator K
= keyrings
.begin();
93 K
!= keyrings
.end(); ++K
)
95 Args
.push_back("--keyring");
96 Args
.push_back(K
->c_str());
99 Configuration::Item
const *Opts
;
100 Opts
= _config
->Tree("Acquire::gpgv::Options");
104 for (; Opts
!= 0; Opts
= Opts
->Next
)
106 if (Opts
->Value
.empty() == true)
108 Args
.push_back(Opts
->Value
.c_str());
112 std::vector
<std::string
> dataHeader
;
116 // file with detached signature
119 Args
.push_back(FileGPG
.c_str());
120 Args
.push_back(File
.c_str());
122 else // clear-signed file
124 sig
= GenerateTemporaryFileTemplate("apt.sig");
125 data
= GenerateTemporaryFileTemplate("apt.data");
126 if (sig
== NULL
|| data
== NULL
)
128 ioprintf(std::cerr
, "Couldn't create tempfile names for splitting up %s", File
.c_str());
132 int const sigFd
= mkstemp(sig
);
133 int const dataFd
= mkstemp(data
);
134 if (sigFd
== -1 || dataFd
== -1)
140 ioprintf(std::cerr
, "Couldn't create tempfiles for splitting up %s", File
.c_str());
145 signature
.OpenDescriptor(sigFd
, FileFd::WriteOnly
, true);
147 message
.OpenDescriptor(dataFd
, FileFd::WriteOnly
, true);
149 if (signature
.Failed() == true || message
.Failed() == true ||
150 SplitClearSignedFile(File
, &message
, &dataHeader
, &signature
) == false)
156 ioprintf(std::cerr
, "Splitting up %s into data and signature failed", File
.c_str());
160 Args
.push_back(data
);
163 Args
.push_back(NULL
);
167 std::clog
<< "Preparing to exec: " << gpgvpath
;
168 for (std::vector
<const char *>::const_iterator a
= Args
.begin(); *a
!= NULL
; ++a
)
169 std::clog
<< " " << *a
;
170 std::clog
<< std::endl
;
175 int const nullfd
= open("/dev/null", O_RDONLY
);
177 // Redirect output to /dev/null; we read from the status fd
178 if (statusfd
!= STDOUT_FILENO
)
179 dup2(nullfd
, STDOUT_FILENO
);
180 if (statusfd
!= STDERR_FILENO
)
181 dup2(nullfd
, STDERR_FILENO
);
182 // Redirect the pipe to the status fd (3)
183 dup2(fd
[1], statusfd
);
185 putenv((char *)"LANG=");
186 putenv((char *)"LC_ALL=");
187 putenv((char *)"LC_MESSAGES=");
192 execvp(gpgvpath
.c_str(), (char **) &Args
[0]);
193 ioprintf(std::cerr
, "Couldn't execute %s to check %s", Args
[0], File
.c_str());
198 //#define UNLINK_EXIT(X) exit(X)
199 #define UNLINK_EXIT(X) unlink(sig);unlink(data);exit(X)
201 // for clear-signed files we have created tempfiles we have to clean up
202 // and we do an additional check, so fork yet another time …
203 pid_t pid
= ExecFork();
205 ioprintf(std::cerr
, "Fork failed for %s to check %s", Args
[0], File
.c_str());
206 UNLINK_EXIT(EINTERNAL
);
211 dup2(fd
[1], statusfd
);
212 execvp(gpgvpath
.c_str(), (char **) &Args
[0]);
213 ioprintf(std::cerr
, "Couldn't execute %s to check %s", Args
[0], File
.c_str());
214 UNLINK_EXIT(EINTERNAL
);
217 // Wait and collect the error code - taken from WaitPid as we need the exact Status
219 while (waitpid(pid
,&Status
,0) != pid
)
223 ioprintf(std::cerr
, _("Waited for %s but it wasn't there"), "gpgv");
224 UNLINK_EXIT(EINTERNAL
);
227 // we don't need the files any longer
233 // check if it exit'ed normally …
234 if (WIFEXITED(Status
) == false)
236 ioprintf(std::cerr
, _("Sub-process %s exited unexpectedly"), "gpgv");
240 // … and with a good exit code
241 if (WEXITSTATUS(Status
) != 0)
243 ioprintf(std::cerr
, _("Sub-process %s returned an error code (%u)"), "gpgv", WEXITSTATUS(Status
));
244 exit(WEXITSTATUS(Status
));
250 exit(EINTERNAL
); // unreachable safe-guard
253 // SplitClearSignedFile - split message into data/signature /*{{{*/
254 bool SplitClearSignedFile(std::string
const &InFile
, FileFd
* const ContentFile
,
255 std::vector
<std::string
> * const ContentHeader
, FileFd
* const SignatureFile
)
257 FILE *in
= fopen(InFile
.c_str(), "r");
259 return _error
->Errno("fopen", "can not open %s", InFile
.c_str());
261 bool found_message_start
= false;
262 bool found_message_end
= false;
263 bool skip_until_empty_line
= false;
264 bool found_signature
= false;
265 bool first_line
= true;
269 ssize_t line_len
= 0;
270 while ((line_len
= getline(&buf
, &buf_size
, in
)) != -1)
273 if (found_message_start
== false)
275 if (strcmp(buf
, "-----BEGIN PGP SIGNED MESSAGE-----") == 0)
277 found_message_start
= true;
278 skip_until_empty_line
= true;
281 else if (skip_until_empty_line
== true)
283 if (strlen(buf
) == 0)
284 skip_until_empty_line
= false;
285 // save "Hash" Armor Headers, others aren't allowed
286 else if (ContentHeader
!= NULL
&& strncmp(buf
, "Hash: ", strlen("Hash: ")) == 0)
287 ContentHeader
->push_back(buf
);
289 else if (found_signature
== false)
291 if (strcmp(buf
, "-----BEGIN PGP SIGNATURE-----") == 0)
293 found_signature
= true;
294 found_message_end
= true;
295 if (SignatureFile
!= NULL
)
297 SignatureFile
->Write(buf
, strlen(buf
));
298 SignatureFile
->Write("\n", 1);
301 else if (found_message_end
== false)
303 // we are in the message block
304 if(first_line
== true) // first line does not need a newline
306 if (ContentFile
!= NULL
)
307 ContentFile
->Write(buf
, strlen(buf
));
310 else if (ContentFile
!= NULL
)
312 ContentFile
->Write("\n", 1);
313 ContentFile
->Write(buf
, strlen(buf
));
317 else if (found_signature
== true)
319 if (SignatureFile
!= NULL
)
321 SignatureFile
->Write(buf
, strlen(buf
));
322 SignatureFile
->Write("\n", 1);
324 if (strcmp(buf
, "-----END PGP SIGNATURE-----") == 0)
325 found_signature
= false; // look for other signatures
327 // all the rest is whitespace, unsigned garbage or additional message blocks we ignore
331 if (found_signature
== true)
332 return _error
->Error("Signature in file %s wasn't closed", InFile
.c_str());
334 // if we haven't found any of them, this an unsigned file,
335 // so don't generate an error, but splitting was unsuccessful none-the-less
336 if (found_message_start
== false && found_message_end
== false)
338 // otherwise one missing indicates a syntax error
339 else if (found_message_start
== false || found_message_end
== false)
340 return _error
->Error("Splitting of file %s failed as it doesn't contain all expected parts", InFile
.c_str());
345 bool OpenMaybeClearSignedFile(std::string
const &ClearSignedFileName
, FileFd
&MessageFile
) /*{{{*/
347 char * const message
= GenerateTemporaryFileTemplate("fileutl.message");
348 int const messageFd
= mkstemp(message
);
352 return _error
->Errno("mkstemp", "Couldn't create temporary file to work with %s", ClearSignedFileName
.c_str());
354 // we have the fd, thats enough for us
358 MessageFile
.OpenDescriptor(messageFd
, FileFd::ReadWrite
, true);
359 if (MessageFile
.Failed() == true)
360 return _error
->Error("Couldn't open temporary file to work with %s", ClearSignedFileName
.c_str());
362 _error
->PushToStack();
363 bool const splitDone
= SplitClearSignedFile(ClearSignedFileName
.c_str(), &MessageFile
, NULL
, NULL
);
364 bool const errorDone
= _error
->PendingError();
365 _error
->MergeWithStack();
366 if (splitDone
== false)
370 if (errorDone
== true)
373 // we deal with an unsigned file
374 MessageFile
.Open(ClearSignedFileName
, FileFd::ReadOnly
);
378 if (MessageFile
.Seek(0) == false)
379 return _error
->Errno("lseek", "Unable to seek back in message for file %s", ClearSignedFileName
.c_str());
382 return MessageFile
.Failed() == false;