]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/gpgv.cc
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>
27 static char * GenerateTemporaryFileTemplate ( const char * basename
) /*{{{*/
30 std :: string tmpdir
= GetTempDir ();
31 strprintf ( out
, " %s / %s .XXXXXX" , tmpdir
. c_str (), basename
);
32 return strdup ( out
. c_str ());
35 // ExecGPGV - returns the command needed for verify /*{{{*/
36 // ---------------------------------------------------------------------
37 /* Generating the commandline for calling gpg is somehow complicated as
38 we need to add multiple keyrings and user supplied options.
39 Also, as gpg has no options to enforce a certain reduced style of
40 clear-signed files (=the complete content of the file is signed and
41 the content isn't encoded) we do a divide and conquer approach here
42 and split up the clear-signed file in message and signature for gpg.
43 And as a cherry on the cake, we use our apt-key wrapper to do part
44 of the lifting in regards to merging keyrings. Fun for the whole family.
46 void ExecGPGV ( std :: string
const & File
, std :: string
const & FileGPG
,
47 int const & statusfd
, int fd
[ 2 ], std :: string
const & key
)
50 std :: string
const aptkey
= _config
-> Find ( "Dir::Bin::apt-key" , "/usr/bin/apt-key" );
52 bool const Debug
= _config
-> FindB ( "Debug::Acquire::gpgv" , false );
54 std :: vector
< const char *> files
;
55 void operator ()( int code
) APT_NORETURN
{
56 std :: for_each ( files
. begin (), files
. end (), unlink
);
62 std :: vector
< const char *> Args
;
65 Args
. push_back ( aptkey
. c_str ());
66 Args
. push_back ( "--quiet" );
67 Args
. push_back ( "--readonly" );
68 if ( key
. empty () == false )
72 Args
. push_back ( "--keyring" );
73 Args
. push_back ( key
. c_str ());
77 Args
. push_back ( "--keyid" );
78 Args
. push_back ( key
. c_str ());
81 Args
. push_back ( "verify" );
86 Args
. push_back ( "--status-fd" );
87 snprintf ( statusfdstr
, sizeof ( statusfdstr
), " %i " , statusfd
);
88 Args
. push_back ( statusfdstr
);
91 Configuration :: Item
const * Opts
;
92 Opts
= _config
-> Tree ( "Acquire::gpgv::Options" );
96 for (; Opts
!= 0 ; Opts
= Opts
-> Next
)
98 if ( Opts
-> Value
. empty () == true )
100 Args
. push_back ( Opts
-> Value
. c_str ());
104 enum { DETACHED
, CLEARSIGNED
} releaseSignature
= ( FileGPG
!= File
) ? DETACHED
: CLEARSIGNED
;
105 std :: vector
< std :: string
> dataHeader
;
109 if ( releaseSignature
== DETACHED
)
111 Args
. push_back ( FileGPG
. c_str ());
112 Args
. push_back ( File
. c_str ());
114 else // clear-signed file
116 sig
= GenerateTemporaryFileTemplate ( "apt.sig" );
117 data
= GenerateTemporaryFileTemplate ( "apt.data" );
118 if ( sig
== NULL
|| data
== NULL
)
120 ioprintf ( std :: cerr
, "Couldn't create tempfile names for splitting up %s " , File
. c_str ());
121 local_exit ( EINTERNAL
);
124 int const sigFd
= mkstemp ( sig
);
125 int const dataFd
= mkstemp ( data
);
127 local_exit
. files
. push_back ( data
);
129 local_exit
. files
. push_back ( sig
);
130 if ( sigFd
== - 1 || dataFd
== - 1 )
132 ioprintf ( std :: cerr
, "Couldn't create tempfiles for splitting up %s " , File
. c_str ());
133 local_exit ( EINTERNAL
);
137 signature
. OpenDescriptor ( sigFd
, FileFd :: WriteOnly
, true );
139 message
. OpenDescriptor ( dataFd
, FileFd :: WriteOnly
, true );
141 if ( signature
. Failed () == true || message
. Failed () == true ||
142 SplitClearSignedFile ( File
, & message
, & dataHeader
, & signature
) == false )
144 ioprintf ( std :: cerr
, "Splitting up %s into data and signature failed" , File
. c_str ());
148 Args
. push_back ( data
);
151 Args
. push_back ( NULL
);
155 std :: clog
<< "Preparing to exec: " ;
156 for ( std :: vector
< const char *>:: const_iterator a
= Args
. begin (); * a
!= NULL
; ++ a
)
157 std :: clog
<< " " << * a
;
158 std :: clog
<< std :: endl
;
163 int const nullfd
= open ( "/dev/null" , O_WRONLY
);
165 // Redirect output to /dev/null; we read from the status fd
166 if ( statusfd
!= STDOUT_FILENO
)
167 dup2 ( nullfd
, STDOUT_FILENO
);
168 if ( statusfd
!= STDERR_FILENO
)
169 dup2 ( nullfd
, STDERR_FILENO
);
170 // Redirect the pipe to the status fd (3)
171 dup2 ( fd
[ 1 ], statusfd
);
173 putenv (( char *) "LANG=" );
174 putenv (( char *) "LC_ALL=" );
175 putenv (( char *) "LC_MESSAGES=" );
178 if ( releaseSignature
== DETACHED
)
180 execvp ( Args
[ 0 ], ( char **) & Args
[ 0 ]);
181 ioprintf ( std :: cerr
, "Couldn't execute %s to check %s " , Args
[ 0 ], File
. c_str ());
182 local_exit ( EINTERNAL
);
186 // for clear-signed files we have created tempfiles we have to clean up
187 // and we do an additional check, so fork yet another time …
188 pid_t pid
= ExecFork ();
190 ioprintf ( std :: cerr
, "Fork failed for %s to check %s " , Args
[ 0 ], File
. c_str ());
191 local_exit ( EINTERNAL
);
196 dup2 ( fd
[ 1 ], statusfd
);
197 execvp ( Args
[ 0 ], ( char **) & Args
[ 0 ]);
198 ioprintf ( std :: cerr
, "Couldn't execute %s to check %s " , Args
[ 0 ], File
. c_str ());
199 local_exit ( EINTERNAL
);
202 // Wait and collect the error code - taken from WaitPid as we need the exact Status
204 while ( waitpid ( pid
,& Status
, 0 ) != pid
)
208 ioprintf ( std :: cerr
, _ ( "Waited for %s but it wasn't there" ), "apt-key" );
209 local_exit ( EINTERNAL
);
212 // check if it exit'ed normally …
213 if ( WIFEXITED ( Status
) == false )
215 ioprintf ( std :: cerr
, _ ( "Sub-process %s exited unexpectedly" ), "apt-key" );
216 local_exit ( EINTERNAL
);
219 // … and with a good exit code
220 if ( WEXITSTATUS ( Status
) != 0 )
222 ioprintf ( std :: cerr
, _ ( "Sub-process %s returned an error code ( %u )" ), "apt-key" , WEXITSTATUS ( Status
));
223 local_exit ( WEXITSTATUS ( Status
));
229 local_exit ( EINTERNAL
); // unreachable safe-guard
232 // SplitClearSignedFile - split message into data/signature /*{{{*/
233 bool SplitClearSignedFile ( std :: string
const & InFile
, FileFd
* const ContentFile
,
234 std :: vector
< std :: string
> * const ContentHeader
, FileFd
* const SignatureFile
)
236 FILE * in
= fopen ( InFile
. c_str (), "r" );
238 return _error
-> Errno ( "fopen" , "can not open %s " , InFile
. c_str ());
240 bool found_message_start
= false ;
241 bool found_message_end
= false ;
242 bool skip_until_empty_line
= false ;
243 bool found_signature
= false ;
244 bool first_line
= true ;
248 while ( getline (& buf
, & buf_size
, in
) != - 1 )
251 if ( found_message_start
== false )
253 if ( strcmp ( buf
, "-----BEGIN PGP SIGNED MESSAGE-----" ) == 0 )
255 found_message_start
= true ;
256 skip_until_empty_line
= true ;
259 else if ( skip_until_empty_line
== true )
261 if ( strlen ( buf
) == 0 )
262 skip_until_empty_line
= false ;
263 // save "Hash" Armor Headers, others aren't allowed
264 else if ( ContentHeader
!= NULL
&& strncmp ( buf
, "Hash: " , strlen ( "Hash: " )) == 0 )
265 ContentHeader
-> push_back ( buf
);
267 else if ( found_signature
== false )
269 if ( strcmp ( buf
, "-----BEGIN PGP SIGNATURE-----" ) == 0 )
271 found_signature
= true ;
272 found_message_end
= true ;
273 if ( SignatureFile
!= NULL
)
275 SignatureFile
-> Write ( buf
, strlen ( buf
));
276 SignatureFile
-> Write ( " \n " , 1 );
279 else if ( found_message_end
== false ) // we are in the message block
281 // we don't have any fields which need dash-escaped,
282 // but implementations are free to encode all lines …
283 char const * dashfree
= buf
;
284 if ( strncmp ( dashfree
, "- " , 2 ) == 0 )
286 if ( first_line
== true ) // first line does not need a newline
288 else if ( ContentFile
!= NULL
)
289 ContentFile
-> Write ( " \n " , 1 );
292 if ( ContentFile
!= NULL
)
293 ContentFile
-> Write ( dashfree
, strlen ( dashfree
));
296 else if ( found_signature
== true )
298 if ( SignatureFile
!= NULL
)
300 SignatureFile
-> Write ( buf
, strlen ( buf
));
301 SignatureFile
-> Write ( " \n " , 1 );
303 if ( strcmp ( buf
, "-----END PGP SIGNATURE-----" ) == 0 )
304 found_signature
= false ; // look for other signatures
306 // all the rest is whitespace, unsigned garbage or additional message blocks we ignore
312 if ( found_signature
== true )
313 return _error
-> Error ( "Signature in file %s wasn't closed" , InFile
. c_str ());
315 // if we haven't found any of them, this an unsigned file,
316 // so don't generate an error, but splitting was unsuccessful none-the-less
317 if ( first_line
== true && found_message_start
== false && found_message_end
== false )
319 // otherwise one missing indicates a syntax error
320 else if ( first_line
== true || found_message_start
== false || found_message_end
== false )
321 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
);
326 bool OpenMaybeClearSignedFile ( std :: string
const & ClearSignedFileName
, FileFd
& MessageFile
) /*{{{*/
328 char * const message
= GenerateTemporaryFileTemplate ( "fileutl.message" );
329 int const messageFd
= mkstemp ( message
);
333 return _error
-> Errno ( "mkstemp" , "Couldn't create temporary file to work with %s " , ClearSignedFileName
. c_str ());
335 // we have the fd, thats enough for us
339 MessageFile
. OpenDescriptor ( messageFd
, FileFd :: ReadWrite
| FileFd :: BufferedWrite
, true );
340 if ( MessageFile
. Failed () == true )
341 return _error
-> Error ( "Couldn't open temporary file to work with %s " , ClearSignedFileName
. c_str ());
343 _error
-> PushToStack ();
344 bool const splitDone
= SplitClearSignedFile ( ClearSignedFileName
, & MessageFile
, NULL
, NULL
);
345 bool const errorDone
= _error
-> PendingError ();
346 _error
-> MergeWithStack ();
347 if ( splitDone
== false )
351 if ( errorDone
== true )
354 // we deal with an unsigned file
355 MessageFile
. Open ( ClearSignedFileName
, FileFd :: ReadOnly
);
359 if ( MessageFile
. Seek ( 0 ) == false )
360 return _error
-> Errno ( "lseek" , "Unable to seek back in message for file %s " , ClearSignedFileName
. c_str ());
363 return MessageFile
. Failed () == false ;