]>
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>
15 #include<apt-pkg/configuration.h>
16 #include<apt-pkg/error.h>
17 #include<apt-pkg/strutl.h>
18 #include<apt-pkg/fileutl.h>
19 #include<apt-pkg/gpgv.h>
23 static char * GenerateTemporaryFileTemplate ( const char * basename
) /*{{{*/
26 std :: string tmpdir
= GetTempDir ();
27 strprintf ( out
, " %s / %s .XXXXXX" , tmpdir
. c_str (), basename
);
28 return strdup ( out
. c_str ());
31 // ExecGPGV - returns the command needed for verify /*{{{*/
32 // ---------------------------------------------------------------------
33 /* Generating the commandline for calling gpgv is somehow complicated as
34 we need to add multiple keyrings and user supplied options.
35 Also, as gpgv has no options to enforce a certain reduced style of
36 clear-signed files (=the complete content of the file is signed and
37 the content isn't encoded) we do a divide and conquer approach here
38 and split up the clear-signed file in message and signature for gpgv
40 void ExecGPGV ( std :: string
const & File
, std :: string
const & FileGPG
,
41 int const & statusfd
, int fd
[ 2 ])
44 std :: string
const gpgvpath
= _config
-> Find ( "Dir::Bin::gpg" , "/usr/bin/gpgv" );
45 // FIXME: remove support for deprecated APT::GPGV setting
46 std :: string
const trustedFile
= _config
-> Find ( "APT::GPGV::TrustedKeyring" , _config
-> FindFile ( "Dir::Etc::Trusted" ));
47 std :: string
const trustedPath
= _config
-> FindDir ( "Dir::Etc::TrustedParts" );
49 bool const Debug
= _config
-> FindB ( "Debug::Acquire::gpgv" , false );
53 std :: clog
<< "gpgv path: " << gpgvpath
<< std :: endl
;
54 std :: clog
<< "Keyring file: " << trustedFile
<< std :: endl
;
55 std :: clog
<< "Keyring path: " << trustedPath
<< std :: endl
;
58 std :: vector
< std :: string
> keyrings
;
59 if ( DirectoryExists ( trustedPath
))
60 keyrings
= GetListOfFilesInDir ( trustedPath
, "gpg" , false , true );
61 if ( RealFileExists ( trustedFile
) == true )
62 keyrings
. push_back ( trustedFile
);
64 std :: vector
< const char *> Args
;
67 if ( keyrings
. empty () == true )
69 // TRANSLATOR: %s is the trusted keyring parts directory
70 ioprintf ( std :: cerr
, _ ( "No keyring installed in %s ." ),
71 _config
-> FindDir ( "Dir::Etc::TrustedParts" ). c_str ());
75 Args
. push_back ( gpgvpath
. c_str ());
76 Args
. push_back ( "--ignore-time-conflict" );
81 Args
. push_back ( "--status-fd" );
82 snprintf ( statusfdstr
, sizeof ( statusfdstr
), " %i " , statusfd
);
83 Args
. push_back ( statusfdstr
);
86 for ( std :: vector
< std :: string
>:: const_iterator K
= keyrings
. begin ();
87 K
!= keyrings
. end (); ++ K
)
89 Args
. push_back ( "--keyring" );
90 Args
. push_back ( K
-> c_str ());
93 Configuration :: Item
const * Opts
;
94 Opts
= _config
-> Tree ( "Acquire::gpgv::Options" );
98 for (; Opts
!= 0 ; Opts
= Opts
-> Next
)
100 if ( Opts
-> Value
. empty () == true )
102 Args
. push_back ( Opts
-> Value
. c_str ());
106 std :: vector
< std :: string
> dataHeader
;
110 // file with detached signature
113 Args
. push_back ( FileGPG
. c_str ());
114 Args
. push_back ( File
. c_str ());
116 else // clear-signed file
118 sig
= GenerateTemporaryFileTemplate ( "apt.sig" );
119 data
= GenerateTemporaryFileTemplate ( "apt.data" );
120 if ( sig
== NULL
|| data
== NULL
)
122 ioprintf ( std :: cerr
, "Couldn't create tempfile names for splitting up %s " , File
. c_str ());
126 int const sigFd
= mkstemp ( sig
);
127 int const dataFd
= mkstemp ( data
);
128 if ( sigFd
== - 1 || dataFd
== - 1 )
134 ioprintf ( std :: cerr
, "Couldn't create tempfiles for splitting up %s " , File
. c_str ());
139 signature
. OpenDescriptor ( sigFd
, FileFd :: WriteOnly
, true );
141 message
. OpenDescriptor ( dataFd
, FileFd :: WriteOnly
, true );
143 if ( signature
. Failed () == true || message
. Failed () == true ||
144 SplitClearSignedFile ( File
, & message
, & dataHeader
, & signature
) == false )
150 ioprintf ( std :: cerr
, "Splitting up %s into data and signature failed" , File
. c_str ());
154 Args
. push_back ( data
);
157 Args
. push_back ( NULL
);
161 std :: clog
<< "Preparing to exec: " << gpgvpath
;
162 for ( std :: vector
< const char *>:: const_iterator a
= Args
. begin (); * a
!= NULL
; ++ a
)
163 std :: clog
<< " " << * a
;
164 std :: clog
<< std :: endl
;
169 int const nullfd
= open ( "/dev/null" , O_RDONLY
);
171 // Redirect output to /dev/null; we read from the status fd
172 if ( statusfd
!= STDOUT_FILENO
)
173 dup2 ( nullfd
, STDOUT_FILENO
);
174 if ( statusfd
!= STDERR_FILENO
)
175 dup2 ( nullfd
, STDERR_FILENO
);
176 // Redirect the pipe to the status fd (3)
177 dup2 ( fd
[ 1 ], statusfd
);
179 putenv (( char *) "LANG=" );
180 putenv (( char *) "LC_ALL=" );
181 putenv (( char *) "LC_MESSAGES=" );
186 execvp ( gpgvpath
. c_str (), ( char **) & Args
[ 0 ]);
187 ioprintf ( std :: cerr
, "Couldn't execute %s to check %s " , Args
[ 0 ], File
. c_str ());
192 //#define UNLINK_EXIT(X) exit(X)
193 #define UNLINK_EXIT(X) unlink(sig);unlink(data);exit(X)
195 // for clear-signed files we have created tempfiles we have to clean up
196 // and we do an additional check, so fork yet another time …
197 pid_t pid
= ExecFork ();
199 ioprintf ( std :: cerr
, "Fork failed for %s to check %s " , Args
[ 0 ], File
. c_str ());
200 UNLINK_EXIT ( EINTERNAL
);
205 dup2 ( fd
[ 1 ], statusfd
);
206 execvp ( gpgvpath
. c_str (), ( char **) & Args
[ 0 ]);
207 ioprintf ( std :: cerr
, "Couldn't execute %s to check %s " , Args
[ 0 ], File
. c_str ());
208 UNLINK_EXIT ( EINTERNAL
);
211 // Wait and collect the error code - taken from WaitPid as we need the exact Status
213 while ( waitpid ( pid
,& Status
, 0 ) != pid
)
217 ioprintf ( std :: cerr
, _ ( "Waited for %s but it wasn't there" ), "gpgv" );
218 UNLINK_EXIT ( EINTERNAL
);
221 // we don't need the files any longer
227 // check if it exit'ed normally …
228 if ( WIFEXITED ( Status
) == false )
230 ioprintf ( std :: cerr
, _ ( "Sub-process %s exited unexpectedly" ), "gpgv" );
234 // … and with a good exit code
235 if ( WEXITSTATUS ( Status
) != 0 )
237 ioprintf ( std :: cerr
, _ ( "Sub-process %s returned an error code ( %u )" ), "gpgv" , WEXITSTATUS ( Status
));
238 exit ( WEXITSTATUS ( Status
));
244 exit ( EINTERNAL
); // unreachable safe-guard
247 // SplitClearSignedFile - split message into data/signature /*{{{*/
248 bool SplitClearSignedFile ( std :: string
const & InFile
, FileFd
* const ContentFile
,
249 std :: vector
< std :: string
> * const ContentHeader
, FileFd
* const SignatureFile
)
251 FILE * in
= fopen ( InFile
. c_str (), "r" );
253 return _error
-> Errno ( "fopen" , "can not open %s " , InFile
. c_str ());
255 bool found_message_start
= false ;
256 bool found_message_end
= false ;
257 bool skip_until_empty_line
= false ;
258 bool found_signature
= false ;
259 bool first_line
= true ;
263 ssize_t line_len
= 0 ;
264 while (( line_len
= getline (& buf
, & buf_size
, in
)) != - 1 )
267 if ( found_message_start
== false )
269 if ( strcmp ( buf
, "-----BEGIN PGP SIGNED MESSAGE-----" ) == 0 )
271 found_message_start
= true ;
272 skip_until_empty_line
= true ;
275 else if ( skip_until_empty_line
== true )
277 if ( strlen ( buf
) == 0 )
278 skip_until_empty_line
= false ;
279 // save "Hash" Armor Headers, others aren't allowed
280 else if ( ContentHeader
!= NULL
&& strncmp ( buf
, "Hash: " , strlen ( "Hash: " )) == 0 )
281 ContentHeader
-> push_back ( buf
);
283 else if ( found_signature
== false )
285 if ( strcmp ( buf
, "-----BEGIN PGP SIGNATURE-----" ) == 0 )
287 found_signature
= true ;
288 found_message_end
= true ;
289 if ( SignatureFile
!= NULL
)
291 SignatureFile
-> Write ( buf
, strlen ( buf
));
292 SignatureFile
-> Write ( " \n " , 1 );
295 else if ( found_message_end
== false ) // we are in the message block
297 // we don't have any fields which need dash-escaped,
298 // but implementations are free to encode all lines …
299 char const * dashfree
= buf
;
300 if ( strncmp ( dashfree
, "- " , 2 ) == 0 )
302 if ( first_line
== true ) // first line does not need a newline
304 else if ( ContentFile
!= NULL
)
305 ContentFile
-> Write ( " \n " , 1 );
308 if ( ContentFile
!= NULL
)
309 ContentFile
-> Write ( dashfree
, strlen ( dashfree
));
312 else if ( found_signature
== true )
314 if ( SignatureFile
!= NULL
)
316 SignatureFile
-> Write ( buf
, strlen ( buf
));
317 SignatureFile
-> Write ( " \n " , 1 );
319 if ( strcmp ( buf
, "-----END PGP SIGNATURE-----" ) == 0 )
320 found_signature
= false ; // look for other signatures
322 // all the rest is whitespace, unsigned garbage or additional message blocks we ignore
326 if ( found_signature
== true )
327 return _error
-> Error ( "Signature in file %s wasn't closed" , InFile
. c_str ());
329 // if we haven't found any of them, this an unsigned file,
330 // so don't generate an error, but splitting was unsuccessful none-the-less
331 if ( first_line
== true && found_message_start
== false && found_message_end
== false )
333 // otherwise one missing indicates a syntax error
334 else if ( first_line
== true || found_message_start
== false || found_message_end
== false )
335 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
);
340 bool OpenMaybeClearSignedFile ( std :: string
const & ClearSignedFileName
, FileFd
& MessageFile
) /*{{{*/
342 char * const message
= GenerateTemporaryFileTemplate ( "fileutl.message" );
343 int const messageFd
= mkstemp ( message
);
347 return _error
-> Errno ( "mkstemp" , "Couldn't create temporary file to work with %s " , ClearSignedFileName
. c_str ());
349 // we have the fd, thats enough for us
353 MessageFile
. OpenDescriptor ( messageFd
, FileFd :: ReadWrite
, true );
354 if ( MessageFile
. Failed () == true )
355 return _error
-> Error ( "Couldn't open temporary file to work with %s " , ClearSignedFileName
. c_str ());
357 _error
-> PushToStack ();
358 bool const splitDone
= SplitClearSignedFile ( ClearSignedFileName
. c_str (), & MessageFile
, NULL
, NULL
);
359 bool const errorDone
= _error
-> PendingError ();
360 _error
-> MergeWithStack ();
361 if ( splitDone
== false )
365 if ( errorDone
== true )
368 // we deal with an unsigned file
369 MessageFile
. Open ( ClearSignedFileName
, FileFd :: ReadOnly
);
373 if ( MessageFile
. Seek ( 0 ) == false )
374 return _error
-> Errno ( "lseek" , "Unable to seek back in message for file %s " , ClearSignedFileName
. c_str ());
377 return MessageFile
. Failed () == false ;