2007-07-05 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / gpgv.c
bloba2343ae85439869f1ee120b15cf358ceca502af7
1 /* gpgv.c - The GnuPG signature verify utility
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005,
3 * 2006 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 3 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, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <unistd.h>
28 #ifdef HAVE_DOSISH_SYSTEM
29 #include <fcntl.h> /* for setmode() */
30 #endif
31 #ifdef HAVE_LIBREADLINE
32 #define GNUPG_LIBREADLINE_H_INCLUDED
33 #include <readline/readline.h>
34 #endif
36 #define INCLUDED_BY_MAIN_MODULE 1
37 #include "gpg.h"
38 #include "packet.h"
39 #include "iobuf.h"
40 #include "util.h"
41 #include "main.h"
42 #include "options.h"
43 #include "keydb.h"
44 #include "trustdb.h"
45 #include "cipher.h"
46 #include "filter.h"
47 #include "ttyio.h"
48 #include "i18n.h"
49 #include "sysutils.h"
50 #include "status.h"
51 #include "call-agent.h"
54 enum cmd_and_opt_values { aNull = 0,
55 oQuiet = 'q',
56 oVerbose = 'v',
57 oBatch = 500,
58 oKeyring,
59 oIgnoreTimeConflict,
60 oStatusFD,
61 oLoggerFD,
62 oHomedir,
63 aTest };
66 static ARGPARSE_OPTS opts[] = {
68 { 301, NULL, 0, N_("@\nOptions:\n ") },
70 { oVerbose, "verbose", 0, N_("verbose") },
71 { oQuiet, "quiet", 0, N_("be somewhat more quiet") },
72 { oKeyring, "keyring" ,2, N_("take the keys from this keyring")},
73 { oIgnoreTimeConflict, "ignore-time-conflict", 0,
74 N_("make timestamp conflicts only a warning") },
75 { oStatusFD, "status-fd" ,1, N_("|FD|write status info to this FD") },
76 { oLoggerFD, "logger-fd",1, "@" },
77 { oHomedir, "homedir", 2, "@" }, /* defaults to "~/.gnupg" */
79 {0} };
83 int g10_errors_seen = 0;
85 static const char *
86 my_strusage( int level )
88 const char *p;
89 switch( level ) {
90 case 11: p = "gpgv (GnuPG)";
91 break;
92 case 13: p = VERSION; break;
93 case 17: p = PRINTABLE_OS_NAME; break;
94 case 19: p =
95 _("Please report bugs to <gnupg-bugs@gnu.org>.\n");
96 break;
97 case 1:
98 case 40: p =
99 _("Usage: gpgv [options] [files] (-h for help)");
100 break;
101 case 41: p =
102 _("Syntax: gpg [options] [files]\n"
103 "Check signatures against known trusted keys\n");
104 break;
106 default: p = NULL;
108 return p;
114 main( int argc, char **argv )
116 ARGPARSE_ARGS pargs;
117 int rc=0;
118 strlist_t sl;
119 strlist_t nrings=NULL;
120 unsigned configlineno;
122 set_strusage (my_strusage);
123 log_set_prefix ("gpgv", 1);
125 /* Make sure that our subsystems are ready. */
126 init_common_subsystems ();
128 gnupg_init_signals (0, NULL);
129 i18n_init();
131 opt.command_fd = -1; /* no command fd */
132 opt.pgp2_workarounds = 1;
133 opt.keyserver_options.options|=KEYSERVER_AUTO_KEY_RETRIEVE;
134 opt.trust_model = TM_ALWAYS;
135 opt.batch = 1;
137 opt.homedir = default_homedir ();
139 tty_no_terminal(1);
140 tty_batchmode(1);
141 disable_dotlock();
143 pargs.argc = &argc;
144 pargs.argv = &argv;
145 pargs.flags= 1; /* do not remove the args */
146 while( optfile_parse( NULL, NULL, &configlineno, &pargs, opts) ) {
147 switch( pargs.r_opt ) {
148 case oQuiet: opt.quiet = 1; break;
149 case oVerbose:
150 opt.verbose++;
151 opt.list_sigs=1;
152 gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose);
153 break;
154 case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break;
155 case oStatusFD: set_status_fd( pargs.r.ret_int ); break;
156 case oLoggerFD:
157 log_set_fd (translate_sys2libc_fd (pargs.r.ret_int, 1));
158 break;
159 case oHomedir: opt.homedir = pargs.r.ret_str; break;
160 case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;
161 default : pargs.err = 2; break;
165 if( log_get_errorcount(0) )
166 g10_exit(2);
168 if( opt.verbose > 1 )
169 set_packet_list_mode(1);
171 if( !nrings ) /* no keyring given: use default one */
172 keydb_add_resource ("trustedkeys" EXTSEP_S "gpg", 0, 0);
173 for(sl = nrings; sl; sl = sl->next )
174 keydb_add_resource (sl->d, 0, 0 );
176 FREE_STRLIST (nrings);
178 if( (rc = verify_signatures( argc, argv ) ))
179 log_error("verify signatures failed: %s\n", g10_errstr(rc) );
181 /* cleanup */
182 g10_exit(0);
183 return 8; /*NEVER REACHED*/
187 void
188 g10_exit( int rc )
190 rc = rc? rc : log_get_errorcount(0)? 2 :
191 g10_errors_seen? 1 : 0;
192 exit(rc );
196 /* Stub:
197 * We have to override the trustcheck from pkclist.c becuase
198 * this utility assumes that all keys in the keyring are trustworthy
201 check_signatures_trust( PKT_signature *sig )
203 return 0;
206 void
207 read_trust_options(byte *trust_model,ulong *created,ulong *nextcheck,
208 byte *marginals,byte *completes,byte *cert_depth) {}
210 /* Stub:
211 * We don't have the trustdb , so we have to provide some stub functions
212 * instead
216 cache_disabled_value(PKT_public_key *pk)
218 return 0;
221 void
222 check_trustdb_stale(void) {}
225 get_validity_info (PKT_public_key *pk, PKT_user_id *uid)
227 return '?';
230 unsigned int
231 get_validity (PKT_public_key *pk, PKT_user_id *uid)
233 return 0;
236 const char *
237 trust_value_to_string (unsigned int value)
239 return "err";
242 const char *
243 uid_trust_string_fixed(PKT_public_key *key,PKT_user_id *uid)
245 return "err";
249 get_ownertrust_info (PKT_public_key *pk)
251 return '?';
254 unsigned int
255 get_ownertrust (PKT_public_key *pk)
257 return TRUST_UNKNOWN;
261 /* Stubs:
262 * Because we only work with trusted keys, it does not make sense to
263 * get them from a keyserver
266 struct keyserver_spec *
267 keyserver_match(struct keyserver_spec *spec) { return NULL; }
270 keyserver_import_keyid( u32 *keyid, void *dummy )
272 return -1;
276 keyserver_import_cert(const char *name) { return -1; }
279 keyserver_import_pka(const char *name,unsigned char *fpr) { return -1; }
282 keyserver_import_name(const char *name,struct keyserver_spec *spec)
284 return -1;
288 keyserver_import_ldap(const char *name) { return -1; }
290 /* Stub:
291 * No encryption here but mainproc links to these functions.
294 get_session_key( PKT_pubkey_enc *k, DEK *dek )
296 return G10ERR_GENERAL;
298 /* Stub: */
300 get_override_session_key( DEK *dek, const char *string )
302 return G10ERR_GENERAL;
304 /* Stub: */
306 decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
308 return G10ERR_GENERAL;
312 /* Stub:
313 * No interactive commnds, so we don't need the helptexts
315 void
316 display_online_help( const char *keyword )
320 /* Stub:
321 * We don't use secret keys, but getkey.c links to this
324 check_secret_key( PKT_secret_key *sk, int n )
326 return G10ERR_GENERAL;
329 /* Stub:
330 * No secret key, so no passphrase needed
332 DEK *
333 passphrase_to_dek( u32 *keyid, int pubkey_algo,
334 int cipher_algo, STRING2KEY *s2k, int mode,
335 const char *tmp, int *canceled)
337 if (canceled)
338 *canceled = 0;
339 return NULL;
342 struct keyserver_spec *parse_preferred_keyserver(PKT_signature *sig) {return NULL;}
343 struct keyserver_spec *parse_keyserver_uri(const char *uri,int require_scheme,
344 const char *configname,
345 unsigned int configlineno)
347 return NULL;
350 void free_keyserver_spec(struct keyserver_spec *keyserver) {}
352 /* Stubs to avoid linking to photoid.c */
353 void show_photos(const struct user_attribute *attrs,int count,PKT_public_key *pk) {}
354 int parse_image_header(const struct user_attribute *attr,byte *type,u32 *len) {return 0;}
355 char *image_type_to_string(byte type,int string) {return NULL;}
357 #ifdef ENABLE_CARD_SUPPORT
358 int agent_scd_getattr (const char *name, struct agent_card_info_s *info) {return 0;}
359 #endif /* ENABLE_CARD_SUPPORT */
361 /* Stubs to void linking to ../cipher/cipher.c */
362 const char *cipher_algo_to_string( int algo ) { return "?";}
363 void disable_cipher_algo( int algo ) {}
364 int check_cipher_algo( int algo ) { return -1;}
365 unsigned int cipher_get_keylen( int algo ) { return 0; }
366 unsigned int cipher_get_blocksize( int algo ) {return 0;}
367 gcry_cipher_hd_t cipher_open( int algo, int mode, int secure ) { return NULL;}
368 void cipher_close( gcry_cipher_hd_t c ) {}
369 int cipher_setkey( gcry_cipher_hd_t c, byte *key, unsigned keylen ) { return -1;}
370 void cipher_setiv( gcry_cipher_hd_t c, const byte *iv, unsigned ivlen ){}
371 void cipher_encrypt( gcry_cipher_hd_t c, byte *outbuf,
372 byte *inbuf, unsigned nbytes ) {}
373 void cipher_decrypt( gcry_cipher_hd_t c, byte *outbuf,
374 byte *inbuf, unsigned nbytes ) {}
375 void cipher_sync( gcry_cipher_hd_t c ) {}
379 /* We do not do any locking, so use these stubs here */
380 void disable_dotlock(void) {}
381 DOTLOCK create_dotlock( const char *file_to_lock ) { return NULL; }
382 void destroy_dotlock (DOTLOCK h) {}
383 int make_dotlock( DOTLOCK h, long timeout ) { return 0;}
384 int release_dotlock( DOTLOCK h ) {return 0;}
385 void remove_lockfiles(void) {}