]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/gpgv.cc
8f619fee2ce67d7b54da98742cd4ebeaf203b9a9
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
) /*{{{*/
25 const char * tmpdir
= getenv ( "TMPDIR" );
32 // check that tmpdir is set and exists
34 if (! tmpdir
|| stat ( tmpdir
, & st
) != 0 )
38 strprintf ( out
, " %s / %s .XXXXXX" , tmpdir
, basename
);
39 return strdup ( out
. c_str ());
42 // ExecGPGV - returns the command needed for verify /*{{{*/
43 // ---------------------------------------------------------------------
44 /* Generating the commandline for calling gpgv is somehow complicated as
45 we need to add multiple keyrings and user supplied options.
46 Also, as gpgv has no options to enforce a certain reduced style of
47 clear-signed files (=the complete content of the file is signed and
48 the content isn't encoded) we do a divide and conquer approach here
49 and split up the clear-signed file in message and signature for gpgv
51 void ExecGPGV ( std :: string
const & File
, std :: string
const & FileGPG
,
52 int const & statusfd
, int fd
[ 2 ])
55 std :: string
const gpgvpath
= _config
-> Find ( "Dir::Bin::gpg" , "/usr/bin/gpgv" );
56 // FIXME: remove support for deprecated APT::GPGV setting
57 std :: string
const trustedFile
= _config
-> Find ( "APT::GPGV::TrustedKeyring" , _config
-> FindFile ( "Dir::Etc::Trusted" ));
58 std :: string
const trustedPath
= _config
-> FindDir ( "Dir::Etc::TrustedParts" );
60 bool const Debug
= _config
-> FindB ( "Debug::Acquire::gpgv" , false );
64 std :: clog
<< "gpgv path: " << gpgvpath
<< std :: endl
;
65 std :: clog
<< "Keyring file: " << trustedFile
<< std :: endl
;
66 std :: clog
<< "Keyring path: " << trustedPath
<< std :: endl
;
69 std :: vector
< std :: string
> keyrings
;
70 if ( DirectoryExists ( trustedPath
))
71 keyrings
= GetListOfFilesInDir ( trustedPath
, "gpg" , false , true );
72 if ( RealFileExists ( trustedFile
) == true )
73 keyrings
. push_back ( trustedFile
);
75 std :: vector
< const char *> Args
;
78 if ( keyrings
. empty () == true )
80 // TRANSLATOR: %s is the trusted keyring parts directory
81 ioprintf ( std :: cerr
, _ ( "No keyring installed in %s ." ),
82 _config
-> FindDir ( "Dir::Etc::TrustedParts" ). c_str ());
86 Args
. push_back ( gpgvpath
. c_str ());
87 Args
. push_back ( "--ignore-time-conflict" );
92 Args
. push_back ( "--status-fd" );
93 snprintf ( statusfdstr
, sizeof ( statusfdstr
), " %i " , statusfd
);
94 Args
. push_back ( statusfdstr
);
97 for ( std :: vector
< std :: string
>:: const_iterator K
= keyrings
. begin ();
98 K
!= keyrings
. end (); ++ K
)
100 Args
. push_back ( "--keyring" );
101 Args
. push_back ( K
-> c_str ());
104 Configuration :: Item
const * Opts
;
105 Opts
= _config
-> Tree ( "Acquire::gpgv::Options" );
109 for (; Opts
!= 0 ; Opts
= Opts
-> Next
)
111 if ( Opts
-> Value
. empty () == true )
113 Args
. push_back ( Opts
-> Value
. c_str ());
117 std :: vector
< std :: string
> dataHeader
;
121 // file with detached signature
124 Args
. push_back ( FileGPG
. c_str ());
125 Args
. push_back ( File
. c_str ());
127 else // clear-signed file
129 sig
= GenerateTemporaryFileTemplate ( "apt.sig" );
130 data
= GenerateTemporaryFileTemplate ( "apt.data" );
131 if ( sig
== NULL
|| data
== NULL
)
133 ioprintf ( std :: cerr
, "Couldn't create tempfile names for splitting up %s " , File
. c_str ());
137 int const sigFd
= mkstemp ( sig
);
138 int const dataFd
= mkstemp ( data
);
139 if ( sigFd
== - 1 || dataFd
== - 1 )
145 ioprintf ( std :: cerr
, "Couldn't create tempfiles for splitting up %s " , File
. c_str ());
150 signature
. OpenDescriptor ( sigFd
, FileFd :: WriteOnly
, true );
152 message
. OpenDescriptor ( dataFd
, FileFd :: WriteOnly
, true );
154 if ( signature
. Failed () == true || message
. Failed () == true ||
155 SplitClearSignedFile ( File
, & message
, & dataHeader
, & signature
) == false )
161 ioprintf ( std :: cerr
, "Splitting up %s into data and signature failed" , File
. c_str ());
165 Args
. push_back ( data
);
168 Args
. push_back ( NULL
);
172 std :: clog
<< "Preparing to exec: " << gpgvpath
;
173 for ( std :: vector
< const char *>:: const_iterator a
= Args
. begin (); * a
!= NULL
; ++ a
)
174 std :: clog
<< " " << * a
;
175 std :: clog
<< std :: endl
;
180 int const nullfd
= open ( "/dev/null" , O_RDONLY
);
182 // Redirect output to /dev/null; we read from the status fd
183 if ( statusfd
!= STDOUT_FILENO
)
184 dup2 ( nullfd
, STDOUT_FILENO
);
185 if ( statusfd
!= STDERR_FILENO
)
186 dup2 ( nullfd
, STDERR_FILENO
);
187 // Redirect the pipe to the status fd (3)
188 dup2 ( fd
[ 1 ], statusfd
);
190 putenv (( char *) "LANG=" );
191 putenv (( char *) "LC_ALL=" );
192 putenv (( char *) "LC_MESSAGES=" );
197 execvp ( gpgvpath
. c_str (), ( char **) & Args
[ 0 ]);
198 ioprintf ( std :: cerr
, "Couldn't execute %s to check %s " , Args
[ 0 ], File
. c_str ());
203 //#define UNLINK_EXIT(X) exit(X)
204 #define UNLINK_EXIT(X) unlink(sig);unlink(data);exit(X)
206 // for clear-signed files we have created tempfiles we have to clean up
207 // and we do an additional check, so fork yet another time …
208 pid_t pid
= ExecFork ();
210 ioprintf ( std :: cerr
, "Fork failed for %s to check %s " , Args
[ 0 ], File
. c_str ());
211 UNLINK_EXIT ( EINTERNAL
);
216 dup2 ( fd
[ 1 ], statusfd
);
217 execvp ( gpgvpath
. c_str (), ( char **) & Args
[ 0 ]);
218 ioprintf ( std :: cerr
, "Couldn't execute %s to check %s " , Args
[ 0 ], File
. c_str ());
219 UNLINK_EXIT ( EINTERNAL
);
222 // Wait and collect the error code - taken from WaitPid as we need the exact Status
224 while ( waitpid ( pid
,& Status
, 0 ) != pid
)
228 ioprintf ( std :: cerr
, _ ( "Waited for %s but it wasn't there" ), "gpgv" );
229 UNLINK_EXIT ( EINTERNAL
);
232 // we don't need the files any longer
238 // check if it exit'ed normally …
239 if ( WIFEXITED ( Status
) == false )
241 ioprintf ( std :: cerr
, _ ( "Sub-process %s exited unexpectedly" ), "gpgv" );
245 // … and with a good exit code
246 if ( WEXITSTATUS ( Status
) != 0 )
248 ioprintf ( std :: cerr
, _ ( "Sub-process %s returned an error code ( %u )" ), "gpgv" , WEXITSTATUS ( Status
));
249 exit ( WEXITSTATUS ( Status
));
255 exit ( EINTERNAL
); // unreachable safe-guard
258 // SplitClearSignedFile - split message into data/signature /*{{{*/
259 bool SplitClearSignedFile ( std :: string
const & InFile
, FileFd
* const ContentFile
,
260 std :: vector
< std :: string
> * const ContentHeader
, FileFd
* const SignatureFile
)
262 FILE * in
= fopen ( InFile
. c_str (), "r" );
264 return _error
-> Errno ( "fopen" , "can not open %s " , InFile
. c_str ());
266 bool found_message_start
= false ;
267 bool found_message_end
= false ;
268 bool skip_until_empty_line
= false ;
269 bool found_signature
= false ;
270 bool first_line
= true ;
274 ssize_t line_len
= 0 ;
275 while (( line_len
= getline (& buf
, & buf_size
, in
)) != - 1 )
278 if ( found_message_start
== false )
280 if ( strcmp ( buf
, "-----BEGIN PGP SIGNED MESSAGE-----" ) == 0 )
282 found_message_start
= true ;
283 skip_until_empty_line
= true ;
286 else if ( skip_until_empty_line
== true )
288 if ( strlen ( buf
) == 0 )
289 skip_until_empty_line
= false ;
290 // save "Hash" Armor Headers, others aren't allowed
291 else if ( ContentHeader
!= NULL
&& strncmp ( buf
, "Hash: " , strlen ( "Hash: " )) == 0 )
292 ContentHeader
-> push_back ( buf
);
294 else if ( found_signature
== false )
296 if ( strcmp ( buf
, "-----BEGIN PGP SIGNATURE-----" ) == 0 )
298 found_signature
= true ;
299 found_message_end
= true ;
300 if ( SignatureFile
!= NULL
)
302 SignatureFile
-> Write ( buf
, strlen ( buf
));
303 SignatureFile
-> Write ( " \n " , 1 );
306 else if ( found_message_end
== false ) // we are in the message block
308 // we don't have any fields which need dash-escaped,
309 // but implementations are free to encode all lines …
310 char const * dashfree
= buf
;
311 if ( strncmp ( dashfree
, "- " , 2 ) == 0 )
313 if ( first_line
== true ) // first line does not need a newline
315 else if ( ContentFile
!= NULL
)
316 ContentFile
-> Write ( " \n " , 1 );
319 if ( ContentFile
!= NULL
)
320 ContentFile
-> Write ( dashfree
, strlen ( dashfree
));
323 else if ( found_signature
== true )
325 if ( SignatureFile
!= NULL
)
327 SignatureFile
-> Write ( buf
, strlen ( buf
));
328 SignatureFile
-> Write ( " \n " , 1 );
330 if ( strcmp ( buf
, "-----END PGP SIGNATURE-----" ) == 0 )
331 found_signature
= false ; // look for other signatures
333 // all the rest is whitespace, unsigned garbage or additional message blocks we ignore
337 if ( found_signature
== true )
338 return _error
-> Error ( "Signature in file %s wasn't closed" , InFile
. c_str ());
340 // if we haven't found any of them, this an unsigned file,
341 // so don't generate an error, but splitting was unsuccessful none-the-less
342 if ( first_line
== true && found_message_start
== false && found_message_end
== false )
344 // otherwise one missing indicates a syntax error
345 else if ( first_line
== true || found_message_start
== false || found_message_end
== false )
346 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
);
351 bool OpenMaybeClearSignedFile ( std :: string
const & ClearSignedFileName
, FileFd
& MessageFile
) /*{{{*/
353 char * const message
= GenerateTemporaryFileTemplate ( "fileutl.message" );
354 int const messageFd
= mkstemp ( message
);
358 return _error
-> Errno ( "mkstemp" , "Couldn't create temporary file to work with %s " , ClearSignedFileName
. c_str ());
360 // we have the fd, thats enough for us
364 MessageFile
. OpenDescriptor ( messageFd
, FileFd :: ReadWrite
, true );
365 if ( MessageFile
. Failed () == true )
366 return _error
-> Error ( "Couldn't open temporary file to work with %s " , ClearSignedFileName
. c_str ());
368 _error
-> PushToStack ();
369 bool const splitDone
= SplitClearSignedFile ( ClearSignedFileName
. c_str (), & MessageFile
, NULL
, NULL
);
370 bool const errorDone
= _error
-> PendingError ();
371 _error
-> MergeWithStack ();
372 if ( splitDone
== false )
376 if ( errorDone
== true )
379 // we deal with an unsigned file
380 MessageFile
. Open ( ClearSignedFileName
, FileFd :: ReadOnly
);
384 if ( MessageFile
. Seek ( 0 ) == false )
385 return _error
-> Errno ( "lseek" , "Unable to seek back in message for file %s " , ClearSignedFileName
. c_str ());
388 return MessageFile
. Failed () == false ;