]>
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>
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 gpg is somehow complicated as
36 we need to add multiple keyrings and user supplied options.
37 Also, as gpg 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 gpg.
41 And as a cherry on the cake, we use our apt-key wrapper to do part
42 of the lifting in regards to merging keyrings. Fun for the whole family.
44 void ExecGPGV ( std :: string
const & File
, std :: string
const & FileGPG
,
45 int const & statusfd
, int fd
[ 2 ])
48 std :: string
const aptkey
= _config
-> FindFile ( "Dir::Bin::apt-key" , "/usr/bin/apt-key" );
50 bool const Debug
= _config
-> FindB ( "Debug::Acquire::gpgv" , false );
52 std :: vector
< const char *> Args
;
55 Args
. push_back ( aptkey
. c_str ());
56 Args
. push_back ( "--quiet" );
57 Args
. push_back ( "--readonly" );
58 Args
. push_back ( "verify" );
63 Args
. push_back ( "--status-fd" );
64 snprintf ( statusfdstr
, sizeof ( statusfdstr
), " %i " , statusfd
);
65 Args
. push_back ( statusfdstr
);
68 Configuration :: Item
const * Opts
;
69 Opts
= _config
-> Tree ( "Acquire::gpgv::Options" );
73 for (; Opts
!= 0 ; Opts
= Opts
-> Next
)
75 if ( Opts
-> Value
. empty () == true )
77 Args
. push_back ( Opts
-> Value
. c_str ());
81 enum { DETACHED
, CLEARSIGNED
} releaseSignature
= ( FileGPG
!= File
) ? DETACHED
: CLEARSIGNED
;
82 std :: vector
< std :: string
> dataHeader
;
86 if ( releaseSignature
== DETACHED
)
88 Args
. push_back ( FileGPG
. c_str ());
89 Args
. push_back ( File
. c_str ());
91 else // clear-signed file
93 sig
= GenerateTemporaryFileTemplate ( "apt.sig" );
94 data
= GenerateTemporaryFileTemplate ( "apt.data" );
95 if ( sig
== NULL
|| data
== NULL
)
97 ioprintf ( std :: cerr
, "Couldn't create tempfile names for splitting up %s " , File
. c_str ());
101 int const sigFd
= mkstemp ( sig
);
102 int const dataFd
= mkstemp ( data
);
103 if ( sigFd
== - 1 || dataFd
== - 1 )
109 ioprintf ( std :: cerr
, "Couldn't create tempfiles for splitting up %s " , File
. c_str ());
114 signature
. OpenDescriptor ( sigFd
, FileFd :: WriteOnly
, true );
116 message
. OpenDescriptor ( dataFd
, FileFd :: WriteOnly
, true );
118 if ( signature
. Failed () == true || message
. Failed () == true ||
119 SplitClearSignedFile ( File
, & message
, & dataHeader
, & signature
) == false )
125 ioprintf ( std :: cerr
, "Splitting up %s into data and signature failed" , File
. c_str ());
129 Args
. push_back ( data
);
132 Args
. push_back ( NULL
);
136 std :: clog
<< "Preparing to exec: " ;
137 for ( std :: vector
< const char *>:: const_iterator a
= Args
. begin (); * a
!= NULL
; ++ a
)
138 std :: clog
<< " " << * a
;
139 std :: clog
<< std :: endl
;
144 int const nullfd
= open ( "/dev/null" , O_WRONLY
);
146 // Redirect output to /dev/null; we read from the status fd
147 if ( statusfd
!= STDOUT_FILENO
)
148 dup2 ( nullfd
, STDOUT_FILENO
);
149 if ( statusfd
!= STDERR_FILENO
)
150 dup2 ( nullfd
, STDERR_FILENO
);
151 // Redirect the pipe to the status fd (3)
152 dup2 ( fd
[ 1 ], statusfd
);
154 putenv (( char *) "LANG=" );
155 putenv (( char *) "LC_ALL=" );
156 putenv (( char *) "LC_MESSAGES=" );
159 if ( releaseSignature
== DETACHED
)
161 execvp ( Args
[ 0 ], ( char **) & Args
[ 0 ]);
162 ioprintf ( std :: cerr
, "Couldn't execute %s to check %s " , Args
[ 0 ], File
. c_str ());
167 //#define UNLINK_EXIT(X) exit(X)
168 #define UNLINK_EXIT(X) unlink(sig);unlink(data);exit(X)
170 // for clear-signed files we have created tempfiles we have to clean up
171 // and we do an additional check, so fork yet another time …
172 pid_t pid
= ExecFork ();
174 ioprintf ( std :: cerr
, "Fork failed for %s to check %s " , Args
[ 0 ], File
. c_str ());
175 UNLINK_EXIT ( EINTERNAL
);
180 dup2 ( fd
[ 1 ], statusfd
);
181 execvp ( Args
[ 0 ], ( char **) & Args
[ 0 ]);
182 ioprintf ( std :: cerr
, "Couldn't execute %s to check %s " , Args
[ 0 ], File
. c_str ());
183 UNLINK_EXIT ( EINTERNAL
);
186 // Wait and collect the error code - taken from WaitPid as we need the exact Status
188 while ( waitpid ( pid
,& Status
, 0 ) != pid
)
192 ioprintf ( std :: cerr
, _ ( "Waited for %s but it wasn't there" ), "apt-key" );
193 UNLINK_EXIT ( EINTERNAL
);
196 // we don't need the files any longer
202 // check if it exit'ed normally …
203 if ( WIFEXITED ( Status
) == false )
205 ioprintf ( std :: cerr
, _ ( "Sub-process %s exited unexpectedly" ), "apt-key" );
209 // … and with a good exit code
210 if ( WEXITSTATUS ( Status
) != 0 )
212 ioprintf ( std :: cerr
, _ ( "Sub-process %s returned an error code ( %u )" ), "apt-key" , WEXITSTATUS ( Status
));
213 exit ( WEXITSTATUS ( Status
));
219 exit ( EINTERNAL
); // unreachable safe-guard
222 // SplitClearSignedFile - split message into data/signature /*{{{*/
223 bool SplitClearSignedFile ( std :: string
const & InFile
, FileFd
* const ContentFile
,
224 std :: vector
< std :: string
> * const ContentHeader
, FileFd
* const SignatureFile
)
226 FILE * in
= fopen ( InFile
. c_str (), "r" );
228 return _error
-> Errno ( "fopen" , "can not open %s " , InFile
. c_str ());
230 bool found_message_start
= false ;
231 bool found_message_end
= false ;
232 bool skip_until_empty_line
= false ;
233 bool found_signature
= false ;
234 bool first_line
= true ;
238 while ( getline (& buf
, & buf_size
, in
) != - 1 )
241 if ( found_message_start
== false )
243 if ( strcmp ( buf
, "-----BEGIN PGP SIGNED MESSAGE-----" ) == 0 )
245 found_message_start
= true ;
246 skip_until_empty_line
= true ;
249 else if ( skip_until_empty_line
== true )
251 if ( strlen ( buf
) == 0 )
252 skip_until_empty_line
= false ;
253 // save "Hash" Armor Headers, others aren't allowed
254 else if ( ContentHeader
!= NULL
&& strncmp ( buf
, "Hash: " , strlen ( "Hash: " )) == 0 )
255 ContentHeader
-> push_back ( buf
);
257 else if ( found_signature
== false )
259 if ( strcmp ( buf
, "-----BEGIN PGP SIGNATURE-----" ) == 0 )
261 found_signature
= true ;
262 found_message_end
= true ;
263 if ( SignatureFile
!= NULL
)
265 SignatureFile
-> Write ( buf
, strlen ( buf
));
266 SignatureFile
-> Write ( " \n " , 1 );
269 else if ( found_message_end
== false ) // we are in the message block
271 // we don't have any fields which need dash-escaped,
272 // but implementations are free to encode all lines …
273 char const * dashfree
= buf
;
274 if ( strncmp ( dashfree
, "- " , 2 ) == 0 )
276 if ( first_line
== true ) // first line does not need a newline
278 else if ( ContentFile
!= NULL
)
279 ContentFile
-> Write ( " \n " , 1 );
282 if ( ContentFile
!= NULL
)
283 ContentFile
-> Write ( dashfree
, strlen ( dashfree
));
286 else if ( found_signature
== true )
288 if ( SignatureFile
!= NULL
)
290 SignatureFile
-> Write ( buf
, strlen ( buf
));
291 SignatureFile
-> Write ( " \n " , 1 );
293 if ( strcmp ( buf
, "-----END PGP SIGNATURE-----" ) == 0 )
294 found_signature
= false ; // look for other signatures
296 // all the rest is whitespace, unsigned garbage or additional message blocks we ignore
300 if ( found_signature
== true )
301 return _error
-> Error ( "Signature in file %s wasn't closed" , InFile
. c_str ());
303 // if we haven't found any of them, this an unsigned file,
304 // so don't generate an error, but splitting was unsuccessful none-the-less
305 if ( first_line
== true && found_message_start
== false && found_message_end
== false )
307 // otherwise one missing indicates a syntax error
308 else if ( first_line
== true || found_message_start
== false || found_message_end
== false )
309 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
);
314 bool OpenMaybeClearSignedFile ( std :: string
const & ClearSignedFileName
, FileFd
& MessageFile
) /*{{{*/
316 char * const message
= GenerateTemporaryFileTemplate ( "fileutl.message" );
317 int const messageFd
= mkstemp ( message
);
321 return _error
-> Errno ( "mkstemp" , "Couldn't create temporary file to work with %s " , ClearSignedFileName
. c_str ());
323 // we have the fd, thats enough for us
327 MessageFile
. OpenDescriptor ( messageFd
, FileFd :: ReadWrite
, true );
328 if ( MessageFile
. Failed () == true )
329 return _error
-> Error ( "Couldn't open temporary file to work with %s " , ClearSignedFileName
. c_str ());
331 _error
-> PushToStack ();
332 bool const splitDone
= SplitClearSignedFile ( ClearSignedFileName
, & MessageFile
, NULL
, NULL
);
333 bool const errorDone
= _error
-> PendingError ();
334 _error
-> MergeWithStack ();
335 if ( splitDone
== false )
339 if ( errorDone
== true )
342 // we deal with an unsigned file
343 MessageFile
. Open ( ClearSignedFileName
, FileFd :: ReadOnly
);
347 if ( MessageFile
. Seek ( 0 ) == false )
348 return _error
-> Errno ( "lseek" , "Unable to seek back in message for file %s " , ClearSignedFileName
. c_str ());
351 return MessageFile
. Failed () == false ;