1 // -*- mode: cpp; mode: fold -*-
2 // Include Files /*{{{*/
5 #include<apt-pkg/configuration.h>
6 #include<apt-pkg/error.h>
7 #include<apt-pkg/strutl.h>
8 #include<apt-pkg/fileutl.h>
9 #include<apt-pkg/gpgv.h>
25 static char * GenerateTemporaryFileTemplate(const char *basename
) /*{{{*/
28 std::string tmpdir
= GetTempDir();
29 strprintf(out
, "%s/%s.XXXXXX", tmpdir
.c_str(), basename
);
30 return strdup(out
.c_str());
33 // ExecGPGV - returns the command needed for verify /*{{{*/
34 // ---------------------------------------------------------------------
35 /* Generating the commandline for calling gpgv is somehow complicated as
36 we need to add multiple keyrings and user supplied options.
37 Also, as gpgv has no options to enforce a certain reduced style of
38 clear-signed files (=the complete content of the file is signed and
39 the content isn't encoded) we do a divide and conquer approach here
40 and split up the clear-signed file in message and signature for gpgv
42 void ExecGPGV(std::string
const &File
, std::string
const &FileGPG
,
43 int const &statusfd
, int fd
[2])
46 std::string
const gpgvpath
= _config
->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
47 // FIXME: remove support for deprecated APT::GPGV setting
48 std::string
const trustedFile
= _config
->Find("APT::GPGV::TrustedKeyring", _config
->FindFile("Dir::Etc::Trusted"));
49 std::string
const trustedPath
= _config
->FindDir("Dir::Etc::TrustedParts");
51 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
55 std::clog
<< "gpgv path: " << gpgvpath
<< std::endl
;
56 std::clog
<< "Keyring file: " << trustedFile
<< std::endl
;
57 std::clog
<< "Keyring path: " << trustedPath
<< std::endl
;
60 std::vector
<std::string
> keyrings
;
61 if (DirectoryExists(trustedPath
))
62 keyrings
= GetListOfFilesInDir(trustedPath
, "gpg", false, true);
63 if (RealFileExists(trustedFile
) == true)
64 keyrings
.push_back(trustedFile
);
66 std::vector
<const char *> Args
;
69 if (keyrings
.empty() == true)
71 // TRANSLATOR: %s is the trusted keyring parts directory
72 ioprintf(std::cerr
, _("No keyring installed in %s."),
73 _config
->FindDir("Dir::Etc::TrustedParts").c_str());
77 Args
.push_back(gpgvpath
.c_str());
78 Args
.push_back("--ignore-time-conflict");
83 Args
.push_back("--status-fd");
84 snprintf(statusfdstr
, sizeof(statusfdstr
), "%i", statusfd
);
85 Args
.push_back(statusfdstr
);
88 for (std::vector
<std::string
>::const_iterator K
= keyrings
.begin();
89 K
!= keyrings
.end(); ++K
)
91 Args
.push_back("--keyring");
92 Args
.push_back(K
->c_str());
95 Configuration::Item
const *Opts
;
96 Opts
= _config
->Tree("Acquire::gpgv::Options");
100 for (; Opts
!= 0; Opts
= Opts
->Next
)
102 if (Opts
->Value
.empty() == true)
104 Args
.push_back(Opts
->Value
.c_str());
108 enum { DETACHED
, CLEARSIGNED
} releaseSignature
= (FileGPG
!= File
) ? DETACHED
: CLEARSIGNED
;
109 std::vector
<std::string
> dataHeader
;
113 if (releaseSignature
== DETACHED
)
115 Args
.push_back(FileGPG
.c_str());
116 Args
.push_back(File
.c_str());
118 else // clear-signed file
120 sig
= GenerateTemporaryFileTemplate("apt.sig");
121 data
= GenerateTemporaryFileTemplate("apt.data");
122 if (sig
== NULL
|| data
== NULL
)
124 ioprintf(std::cerr
, "Couldn't create tempfile names for splitting up %s", File
.c_str());
128 int const sigFd
= mkstemp(sig
);
129 int const dataFd
= mkstemp(data
);
130 if (sigFd
== -1 || dataFd
== -1)
136 ioprintf(std::cerr
, "Couldn't create tempfiles for splitting up %s", File
.c_str());
141 signature
.OpenDescriptor(sigFd
, FileFd::WriteOnly
, true);
143 message
.OpenDescriptor(dataFd
, FileFd::WriteOnly
, true);
145 if (signature
.Failed() == true || message
.Failed() == true ||
146 SplitClearSignedFile(File
, &message
, &dataHeader
, &signature
) == false)
152 ioprintf(std::cerr
, "Splitting up %s into data and signature failed", File
.c_str());
156 Args
.push_back(data
);
159 Args
.push_back(NULL
);
163 std::clog
<< "Preparing to exec: " << gpgvpath
;
164 for (std::vector
<const char *>::const_iterator a
= Args
.begin(); *a
!= NULL
; ++a
)
165 std::clog
<< " " << *a
;
166 std::clog
<< std::endl
;
171 int const nullfd
= open("/dev/null", O_RDONLY
);
173 // Redirect output to /dev/null; we read from the status fd
174 if (statusfd
!= STDOUT_FILENO
)
175 dup2(nullfd
, STDOUT_FILENO
);
176 if (statusfd
!= STDERR_FILENO
)
177 dup2(nullfd
, STDERR_FILENO
);
178 // Redirect the pipe to the status fd (3)
179 dup2(fd
[1], statusfd
);
181 putenv((char *)"LANG=");
182 putenv((char *)"LC_ALL=");
183 putenv((char *)"LC_MESSAGES=");
186 if (releaseSignature
== DETACHED
)
188 execvp(gpgvpath
.c_str(), (char **) &Args
[0]);
189 ioprintf(std::cerr
, "Couldn't execute %s to check %s", Args
[0], File
.c_str());
194 //#define UNLINK_EXIT(X) exit(X)
195 #define UNLINK_EXIT(X) unlink(sig);unlink(data);exit(X)
197 // for clear-signed files we have created tempfiles we have to clean up
198 // and we do an additional check, so fork yet another time …
199 pid_t pid
= ExecFork();
201 ioprintf(std::cerr
, "Fork failed for %s to check %s", Args
[0], File
.c_str());
202 UNLINK_EXIT(EINTERNAL
);
207 dup2(fd
[1], statusfd
);
208 execvp(gpgvpath
.c_str(), (char **) &Args
[0]);
209 ioprintf(std::cerr
, "Couldn't execute %s to check %s", Args
[0], File
.c_str());
210 UNLINK_EXIT(EINTERNAL
);
213 // Wait and collect the error code - taken from WaitPid as we need the exact Status
215 while (waitpid(pid
,&Status
,0) != pid
)
219 ioprintf(std::cerr
, _("Waited for %s but it wasn't there"), "gpgv");
220 UNLINK_EXIT(EINTERNAL
);
223 // we don't need the files any longer
229 // check if it exit'ed normally …
230 if (WIFEXITED(Status
) == false)
232 ioprintf(std::cerr
, _("Sub-process %s exited unexpectedly"), "gpgv");
236 // … and with a good exit code
237 if (WEXITSTATUS(Status
) != 0)
239 ioprintf(std::cerr
, _("Sub-process %s returned an error code (%u)"), "gpgv", WEXITSTATUS(Status
));
240 exit(WEXITSTATUS(Status
));
246 exit(EINTERNAL
); // unreachable safe-guard
249 // SplitClearSignedFile - split message into data/signature /*{{{*/
250 bool SplitClearSignedFile(std::string
const &InFile
, FileFd
* const ContentFile
,
251 std::vector
<std::string
> * const ContentHeader
, FileFd
* const SignatureFile
)
253 FILE *in
= fopen(InFile
.c_str(), "r");
255 return _error
->Errno("fopen", "can not open %s", InFile
.c_str());
257 bool found_message_start
= false;
258 bool found_message_end
= false;
259 bool skip_until_empty_line
= false;
260 bool found_signature
= false;
261 bool first_line
= true;
265 while (getline(&buf
, &buf_size
, in
) != -1)
268 if (found_message_start
== false)
270 if (strcmp(buf
, "-----BEGIN PGP SIGNED MESSAGE-----") == 0)
272 found_message_start
= true;
273 skip_until_empty_line
= true;
276 else if (skip_until_empty_line
== true)
278 if (strlen(buf
) == 0)
279 skip_until_empty_line
= false;
280 // save "Hash" Armor Headers, others aren't allowed
281 else if (ContentHeader
!= NULL
&& strncmp(buf
, "Hash: ", strlen("Hash: ")) == 0)
282 ContentHeader
->push_back(buf
);
284 else if (found_signature
== false)
286 if (strcmp(buf
, "-----BEGIN PGP SIGNATURE-----") == 0)
288 found_signature
= true;
289 found_message_end
= true;
290 if (SignatureFile
!= NULL
)
292 SignatureFile
->Write(buf
, strlen(buf
));
293 SignatureFile
->Write("\n", 1);
296 else if (found_message_end
== false) // we are in the message block
298 // we don't have any fields which need dash-escaped,
299 // but implementations are free to encode all lines …
300 char const * dashfree
= buf
;
301 if (strncmp(dashfree
, "- ", 2) == 0)
303 if(first_line
== true) // first line does not need a newline
305 else if (ContentFile
!= NULL
)
306 ContentFile
->Write("\n", 1);
309 if (ContentFile
!= NULL
)
310 ContentFile
->Write(dashfree
, strlen(dashfree
));
313 else if (found_signature
== true)
315 if (SignatureFile
!= NULL
)
317 SignatureFile
->Write(buf
, strlen(buf
));
318 SignatureFile
->Write("\n", 1);
320 if (strcmp(buf
, "-----END PGP SIGNATURE-----") == 0)
321 found_signature
= false; // look for other signatures
323 // all the rest is whitespace, unsigned garbage or additional message blocks we ignore
327 if (found_signature
== true)
328 return _error
->Error("Signature in file %s wasn't closed", InFile
.c_str());
330 // if we haven't found any of them, this an unsigned file,
331 // so don't generate an error, but splitting was unsuccessful none-the-less
332 if (first_line
== true && found_message_start
== false && found_message_end
== false)
334 // otherwise one missing indicates a syntax error
335 else if (first_line
== true || found_message_start
== false || found_message_end
== false)
336 return _error
->Error("Splitting of file %s failed as it doesn't contain all expected parts %i %i %i", InFile
.c_str(), first_line
, found_message_start
, found_message_end
);
341 bool OpenMaybeClearSignedFile(std::string
const &ClearSignedFileName
, FileFd
&MessageFile
) /*{{{*/
343 char * const message
= GenerateTemporaryFileTemplate("fileutl.message");
344 int const messageFd
= mkstemp(message
);
348 return _error
->Errno("mkstemp", "Couldn't create temporary file to work with %s", ClearSignedFileName
.c_str());
350 // we have the fd, thats enough for us
354 MessageFile
.OpenDescriptor(messageFd
, FileFd::ReadWrite
, true);
355 if (MessageFile
.Failed() == true)
356 return _error
->Error("Couldn't open temporary file to work with %s", ClearSignedFileName
.c_str());
358 _error
->PushToStack();
359 bool const splitDone
= SplitClearSignedFile(ClearSignedFileName
, &MessageFile
, NULL
, NULL
);
360 bool const errorDone
= _error
->PendingError();
361 _error
->MergeWithStack();
362 if (splitDone
== false)
366 if (errorDone
== true)
369 // we deal with an unsigned file
370 MessageFile
.Open(ClearSignedFileName
, FileFd::ReadOnly
);
374 if (MessageFile
.Seek(0) == false)
375 return _error
->Errno("lseek", "Unable to seek back in message for file %s", ClearSignedFileName
.c_str());
378 return MessageFile
.Failed() == false;