2005-04-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / gpgv.c
blob0a97d56b9d6fbc93d4ab55fc713225be83d61815
1 /* gpgv.c - The GnuPG signature verify utility
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2003 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 #include <config.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <unistd.h>
29 #ifdef HAVE_DOSISH_SYSTEM
30 #include <fcntl.h> /* for setmode() */
31 #endif
33 #define INCLUDED_BY_MAIN_MODULE 1
34 #include "gpg.h"
35 #include "packet.h"
36 #include "iobuf.h"
37 #include "memory.h"
38 #include "util.h"
39 #include "main.h"
40 #include "options.h"
41 #include "keydb.h"
42 #include "trustdb.h"
43 #include "mpi.h"
44 #include "cipher.h"
45 #include "filter.h"
46 #include "ttyio.h"
47 #include "i18n.h"
48 #include "status.h"
51 enum cmd_and_opt_values { aNull = 0,
52 oQuiet = 'q',
53 oVerbose = 'v',
54 oBatch = 500,
55 oKeyring,
56 oIgnoreTimeConflict,
57 oStatusFD,
58 oLoggerFD,
59 oHomedir,
60 aTest };
63 static ARGPARSE_OPTS opts[] = {
65 { 301, NULL, 0, N_("@\nOptions:\n ") },
67 { oVerbose, "verbose", 0, N_("verbose") },
68 { oQuiet, "quiet", 0, N_("be somewhat more quiet") },
69 { oKeyring, "keyring" ,2, N_("take the keys from this keyring")},
70 { oIgnoreTimeConflict, "ignore-time-conflict", 0,
71 N_("make timestamp conflicts only a warning") },
72 { oStatusFD, "status-fd" ,1, N_("|FD|write status info to this FD") },
73 { oLoggerFD, "logger-fd",1, "@" },
74 { oHomedir, "homedir", 2, "@" }, /* defaults to "~/.gnupg" */
76 {0} };
80 int g10_errors_seen = 0;
82 #ifdef __riscos__
83 RISCOS_GLOBAL_STATICS("GnuPG (gpgv) Heap")
84 #endif /* __riscos__ */
86 static const char *
87 my_strusage( int level )
89 const char *p;
90 switch( level ) {
91 case 11: p = "gpgv (GnuPG)";
92 break;
93 case 13: p = VERSION; break;
94 case 17: p = PRINTABLE_OS_NAME; break;
95 case 19: p =
96 _("Please report bugs to <gnupg-bugs@gnu.org>.\n");
97 break;
98 case 1:
99 case 40: p =
100 _("Usage: gpgv [options] [files] (-h for help)");
101 break;
102 case 41: p =
103 _("Syntax: gpg [options] [files]\n"
104 "Check signatures against known trusted keys\n");
105 break;
107 default: p = NULL;
109 return p;
115 static void
116 i18n_init(void)
118 #ifdef USE_SIMPLE_GETTEXT
119 set_gettext_file( PACKAGE_GT );
120 #else
121 #ifdef ENABLE_NLS
122 setlocale( LC_ALL, "" );
123 bindtextdomain( PACKAGE_GT, LOCALEDIR );
124 textdomain( PACKAGE_GT );
125 #endif
126 #endif
131 main( int argc, char **argv )
133 ARGPARSE_ARGS pargs;
134 int rc=0;
135 STRLIST sl;
136 STRLIST nrings=NULL;
137 unsigned configlineno;
139 #ifdef __riscos__
140 riscos_global_defaults();
141 #endif /* __riscos__ */
143 set_strusage (my_strusage);
144 log_set_prefix ("gpgv", 1);
145 gnupg_init_signals(0, NULL);
146 i18n_init();
147 opt.command_fd = -1; /* no command fd */
148 opt.pgp2_workarounds = 1;
149 opt.keyserver_options.auto_key_retrieve = 1;
150 opt.trust_model = TM_ALWAYS;
151 opt.batch = 1;
153 opt.homedir = default_homedir ();
155 tty_no_terminal(1);
156 tty_batchmode(1);
157 disable_dotlock();
159 set_native_charset (NULL); /* Try to auto set the character set */
161 pargs.argc = &argc;
162 pargs.argv = &argv;
163 pargs.flags= 1; /* do not remove the args */
164 while( optfile_parse( NULL, NULL, &configlineno, &pargs, opts) ) {
165 switch( pargs.r_opt ) {
166 case oQuiet: opt.quiet = 1; break;
167 case oVerbose: g10_opt_verbose++;
168 opt.verbose++; opt.list_sigs=1; break;
169 case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break;
170 case oStatusFD: set_status_fd( pargs.r.ret_int ); break;
171 case oLoggerFD:
172 log_set_fd (iobuf_translate_file_handle (pargs.r.ret_int, 1));
173 break;
174 case oHomedir: opt.homedir = pargs.r.ret_str; break;
175 case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;
176 default : pargs.err = 2; break;
180 if( log_get_errorcount(0) )
181 g10_exit(2);
183 g10_opt_homedir = opt.homedir;
185 if( opt.verbose > 1 )
186 set_packet_list_mode(1);
188 if( !nrings ) /* no keyring given: use default one */
189 keydb_add_resource ("trustedkeys" EXTSEP_S "gpg", 0, 0);
190 for(sl = nrings; sl; sl = sl->next )
191 keydb_add_resource (sl->d, 0, 0 );
193 FREE_STRLIST(nrings);
195 if( (rc = verify_signatures( argc, argv ) ))
196 log_error("verify signatures failed: %s\n", gpg_strerror (rc) );
198 /* cleanup */
199 g10_exit(0);
200 return 8; /*NEVER REACHED*/
204 void
205 g10_exit( int rc )
207 rc = rc? rc : log_get_errorcount(0)? 2 :
208 g10_errors_seen? 1 : 0;
209 exit(rc );
214 void
215 read_trust_options (byte *trust_model,ulong *created,ulong *nextcheck,
216 byte *marginals,byte *completes,byte *cert_depth)
221 /* Stub:
222 * We have to override the trustcheck from pkclist.c becuase
223 * this utility assumes that all keys in the keyring are trustworthy
226 check_signatures_trust( PKT_signature *sig )
228 return 0;
232 /* Stub:
233 * We don't have the trustdb , so we have to provide some stub functions
234 * instead
238 cache_disabled_value(PKT_public_key *pk)
240 return 0;
244 get_validity_info (PKT_public_key *pk, PKT_user_id *uid)
246 return '?';
249 unsigned int
250 get_validity (PKT_public_key *pk, PKT_user_id *uid)
252 return 0;
255 const char *
256 trust_value_to_string (unsigned int value)
258 return "err";
261 /* Stub: */
263 get_ownertrust_info (PKT_public_key *pk)
265 return '?';
268 unsigned int
269 get_ownertrust (PKT_public_key *pk)
271 return TRUST_UNKNOWN;
275 /* Stub:
276 * Because we only work with trusted keys, it does not make sense to
277 * get them from a keyserver
280 keyserver_import_keyid( u32 *keyid, void *dummy )
282 return -1;
285 /* Stub:
286 * No encryption here but mainproc links to these functions.
289 get_session_key( PKT_pubkey_enc *k, DEK *dek )
291 return GPG_ERR_GENERAL;
293 /* Stub: */
295 get_override_session_key( DEK *dek, const char *string )
297 return GPG_ERR_GENERAL;
299 /* Stub: */
301 decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
303 return GPG_ERR_GENERAL;
307 /* Stub:
308 * No interactive commnds, so we don't need the helptexts
310 void
311 display_online_help( const char *keyword )
315 /* Stub:
316 * We don't use secret keys, but getkey.c links to this
319 check_secret_key( PKT_secret_key *sk, int n )
321 return GPG_ERR_GENERAL;
324 /* Stub:
325 * No secret key, so no passphrase needed
327 DEK *
328 passphrase_to_dek( u32 *keyid, int pubkey_algo,
329 int cipher_algo, STRING2KEY *s2k, int mode,
330 const char *tmp, int *canceled)
332 if (canceled)
333 *canceled = 0;
334 return NULL;
337 /* Stubs to avoid linking to photoid.c */
338 void show_photos(const struct user_attribute *attrs,int count,PKT_public_key *pk) {}
339 int parse_image_header(const struct user_attribute *attr,byte *type,u32 *len) {return 0;}
340 char *image_type_to_string(byte type,int string) {return NULL;}
342 /* Stubs to void linking to ../cipher/cipher.c */
343 int string_to_cipher_algo( const char *string ) { return 0; }
344 const char *cipher_algo_to_string( int algo ) { return "?";}
345 void disable_cipher_algo( int algo ) {}
346 int check_cipher_algo( int algo ) { return -1;}
347 unsigned int cipher_get_keylen( int algo ) { return 0; }
348 unsigned int cipher_get_blocksize( int algo ) {return 0;}
349 CIPHER_HANDLE cipher_open( int algo, int mode, int secure ) { return NULL;}
350 void cipher_close( CIPHER_HANDLE c ) {}
351 int cipher_setkey( CIPHER_HANDLE c, byte *key, unsigned keylen ) { return -1;}
352 void cipher_setiv( CIPHER_HANDLE c, const byte *iv, unsigned ivlen ){}
353 void cipher_encrypt( CIPHER_HANDLE c, byte *outbuf,
354 byte *inbuf, unsigned nbytes ) {}
355 void cipher_decrypt( CIPHER_HANDLE c, byte *outbuf,
356 byte *inbuf, unsigned nbytes ) {}
357 void cipher_sync( CIPHER_HANDLE c ) {}
360 /* Stubs to avoid linking to ../util/ttyio.c */
361 int tty_batchmode( int onoff ) { return 0; }
362 void tty_printf( const char *fmt, ... ) { }
363 void tty_print_string( const byte *p, size_t n ) { }
364 void tty_print_utf8_string( const byte *p, size_t n ) {}
365 void tty_print_utf8_string2( const byte *p, size_t n, size_t max_n ) {}
366 char *tty_get( const char *prompt ) { return NULL;}
367 char *tty_get_hidden( const char *prompt ) {return NULL; }
368 void tty_kill_prompt(void) {}
369 int tty_get_answer_is_yes( const char *prompt ) {return 0;}
370 int tty_no_terminal(int onoff) {return 0;}
372 /* We do not do any locking, so use these stubs here */
373 void disable_dotlock(void) {}
374 DOTLOCK create_dotlock( const char *file_to_lock ) { return NULL; }
375 int make_dotlock( DOTLOCK h, long timeout ) { return 0;}
376 int release_dotlock( DOTLOCK h ) {return 0;}
377 void dotlock_remove_lockfiles(void) {}