2006-09-24 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / gpgv.c
bloba9884a31dc64d03062aff1dbecc003ef76bc26ce
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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
23 #include <config.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #ifdef HAVE_DOSISH_SYSTEM
31 #include <fcntl.h> /* for setmode() */
32 #endif
33 #ifdef HAVE_LIBREADLINE
34 #define GNUPG_LIBREADLINE_H_INCLUDED
35 #include <readline/readline.h>
36 #endif
38 #define INCLUDED_BY_MAIN_MODULE 1
39 #include "gpg.h"
40 #include "packet.h"
41 #include "iobuf.h"
42 #include "util.h"
43 #include "main.h"
44 #include "options.h"
45 #include "keydb.h"
46 #include "trustdb.h"
47 #include "cipher.h"
48 #include "filter.h"
49 #include "ttyio.h"
50 #include "i18n.h"
51 #include "status.h"
52 #include "call-agent.h"
55 enum cmd_and_opt_values { aNull = 0,
56 oQuiet = 'q',
57 oVerbose = 'v',
58 oBatch = 500,
59 oKeyring,
60 oIgnoreTimeConflict,
61 oStatusFD,
62 oLoggerFD,
63 oHomedir,
64 aTest };
67 static ARGPARSE_OPTS opts[] = {
69 { 301, NULL, 0, N_("@\nOptions:\n ") },
71 { oVerbose, "verbose", 0, N_("verbose") },
72 { oQuiet, "quiet", 0, N_("be somewhat more quiet") },
73 { oKeyring, "keyring" ,2, N_("take the keys from this keyring")},
74 { oIgnoreTimeConflict, "ignore-time-conflict", 0,
75 N_("make timestamp conflicts only a warning") },
76 { oStatusFD, "status-fd" ,1, N_("|FD|write status info to this FD") },
77 { oLoggerFD, "logger-fd",1, "@" },
78 { oHomedir, "homedir", 2, "@" }, /* defaults to "~/.gnupg" */
80 {0} };
84 int g10_errors_seen = 0;
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;
113 static void
114 i18n_init(void)
116 #ifdef USE_SIMPLE_GETTEXT
117 set_gettext_file (PACKAGE_GT, "Software\\GNU\\GnuPG");
118 #else
119 #ifdef ENABLE_NLS
120 setlocale (LC_ALL, "");
121 bindtextdomain (PACKAGE_GT, LOCALEDIR);
122 textdomain (PACKAGE_GT);
123 #endif
124 #endif
130 main( int argc, char **argv )
132 ARGPARSE_ARGS pargs;
133 int rc=0;
134 STRLIST sl;
135 STRLIST nrings=NULL;
136 unsigned configlineno;
138 set_strusage (my_strusage);
139 log_set_prefix ("gpgv", 1);
140 gnupg_init_signals (0, NULL);
141 i18n_init();
142 opt.command_fd = -1; /* no command fd */
143 opt.pgp2_workarounds = 1;
144 opt.keyserver_options.options|=KEYSERVER_AUTO_KEY_RETRIEVE;
145 opt.trust_model = TM_ALWAYS;
146 opt.batch = 1;
148 opt.homedir = default_homedir ();
150 tty_no_terminal(1);
151 tty_batchmode(1);
152 disable_dotlock();
154 set_native_charset (NULL); /* Try to auto set the character set */
156 pargs.argc = &argc;
157 pargs.argv = &argv;
158 pargs.flags= 1; /* do not remove the args */
159 while( optfile_parse( NULL, NULL, &configlineno, &pargs, opts) ) {
160 switch( pargs.r_opt ) {
161 case oQuiet: opt.quiet = 1; break;
162 case oVerbose:
163 opt.verbose++;
164 opt.list_sigs=1;
165 gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose);
166 break;
167 case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break;
168 case oStatusFD: set_status_fd( pargs.r.ret_int ); break;
169 case oLoggerFD:
170 log_set_fd (iobuf_translate_file_handle (pargs.r.ret_int, 1));
171 break;
172 case oHomedir: opt.homedir = pargs.r.ret_str; break;
173 case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;
174 default : pargs.err = 2; break;
178 if( log_get_errorcount(0) )
179 g10_exit(2);
181 if( opt.verbose > 1 )
182 set_packet_list_mode(1);
184 if( !nrings ) /* no keyring given: use default one */
185 keydb_add_resource ("trustedkeys" EXTSEP_S "gpg", 0, 0);
186 for(sl = nrings; sl; sl = sl->next )
187 keydb_add_resource (sl->d, 0, 0 );
189 FREE_STRLIST(nrings);
191 if( (rc = verify_signatures( argc, argv ) ))
192 log_error("verify signatures failed: %s\n", g10_errstr(rc) );
194 /* cleanup */
195 g10_exit(0);
196 return 8; /*NEVER REACHED*/
200 void
201 g10_exit( int rc )
203 rc = rc? rc : log_get_errorcount(0)? 2 :
204 g10_errors_seen? 1 : 0;
205 exit(rc );
209 /* Stub:
210 * We have to override the trustcheck from pkclist.c becuase
211 * this utility assumes that all keys in the keyring are trustworthy
214 check_signatures_trust( PKT_signature *sig )
216 return 0;
219 void
220 read_trust_options(byte *trust_model,ulong *created,ulong *nextcheck,
221 byte *marginals,byte *completes,byte *cert_depth) {}
223 /* Stub:
224 * We don't have the trustdb , so we have to provide some stub functions
225 * instead
229 cache_disabled_value(PKT_public_key *pk)
231 return 0;
234 void
235 check_trustdb_stale(void) {}
238 get_validity_info (PKT_public_key *pk, PKT_user_id *uid)
240 return '?';
243 unsigned int
244 get_validity (PKT_public_key *pk, PKT_user_id *uid)
246 return 0;
249 const char *
250 trust_value_to_string (unsigned int value)
252 return "err";
255 const char *
256 uid_trust_string_fixed(PKT_public_key *key,PKT_user_id *uid)
258 return "err";
262 get_ownertrust_info (PKT_public_key *pk)
264 return '?';
267 unsigned int
268 get_ownertrust (PKT_public_key *pk)
270 return TRUST_UNKNOWN;
274 /* Stubs:
275 * Because we only work with trusted keys, it does not make sense to
276 * get them from a keyserver
279 struct keyserver_spec *
280 keyserver_match(struct keyserver_spec *spec) { return NULL; }
283 keyserver_import_keyid( u32 *keyid, void *dummy )
285 return -1;
289 keyserver_import_cert(const char *name) { return -1; }
292 keyserver_import_pka(const char *name,unsigned char *fpr) { return -1; }
295 keyserver_import_name(const char *name,struct keyserver_spec *spec)
297 return -1;
301 keyserver_import_ldap(const char *name) { return -1; }
303 /* Stub:
304 * No encryption here but mainproc links to these functions.
307 get_session_key( PKT_pubkey_enc *k, DEK *dek )
309 return G10ERR_GENERAL;
311 /* Stub: */
313 get_override_session_key( DEK *dek, const char *string )
315 return G10ERR_GENERAL;
317 /* Stub: */
319 decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
321 return G10ERR_GENERAL;
325 /* Stub:
326 * No interactive commnds, so we don't need the helptexts
328 void
329 display_online_help( const char *keyword )
333 /* Stub:
334 * We don't use secret keys, but getkey.c links to this
337 check_secret_key( PKT_secret_key *sk, int n )
339 return G10ERR_GENERAL;
342 /* Stub:
343 * No secret key, so no passphrase needed
345 DEK *
346 passphrase_to_dek( u32 *keyid, int pubkey_algo,
347 int cipher_algo, STRING2KEY *s2k, int mode,
348 const char *tmp, int *canceled)
350 if (canceled)
351 *canceled = 0;
352 return NULL;
355 struct keyserver_spec *parse_preferred_keyserver(PKT_signature *sig) {return NULL;}
356 struct keyserver_spec *parse_keyserver_uri(const char *uri,int require_scheme,
357 const char *configname,
358 unsigned int configlineno)
360 return NULL;
363 void free_keyserver_spec(struct keyserver_spec *keyserver) {}
365 /* Stubs to avoid linking to photoid.c */
366 void show_photos(const struct user_attribute *attrs,int count,PKT_public_key *pk) {}
367 int parse_image_header(const struct user_attribute *attr,byte *type,u32 *len) {return 0;}
368 char *image_type_to_string(byte type,int string) {return NULL;}
370 #ifdef ENABLE_CARD_SUPPORT
371 int agent_scd_getattr (const char *name, struct agent_card_info_s *info) {return 0;}
372 #endif /* ENABLE_CARD_SUPPORT */
374 /* Stubs to void linking to ../cipher/cipher.c */
375 const char *cipher_algo_to_string( int algo ) { return "?";}
376 void disable_cipher_algo( int algo ) {}
377 int check_cipher_algo( int algo ) { return -1;}
378 unsigned int cipher_get_keylen( int algo ) { return 0; }
379 unsigned int cipher_get_blocksize( int algo ) {return 0;}
380 gcry_cipher_hd_t cipher_open( int algo, int mode, int secure ) { return NULL;}
381 void cipher_close( gcry_cipher_hd_t c ) {}
382 int cipher_setkey( gcry_cipher_hd_t c, byte *key, unsigned keylen ) { return -1;}
383 void cipher_setiv( gcry_cipher_hd_t c, const byte *iv, unsigned ivlen ){}
384 void cipher_encrypt( gcry_cipher_hd_t c, byte *outbuf,
385 byte *inbuf, unsigned nbytes ) {}
386 void cipher_decrypt( gcry_cipher_hd_t c, byte *outbuf,
387 byte *inbuf, unsigned nbytes ) {}
388 void cipher_sync( gcry_cipher_hd_t c ) {}
391 /* Stubs to avoid linking to ../util/ttyio.c */
392 int tty_batchmode( int onoff ) { return 0; }
393 void tty_printf( const char *fmt, ... ) { }
394 void tty_fprintf (FILE *fp, const char *fmt, ... ) { }
395 void tty_print_string( const byte *p, size_t n ) { }
396 void tty_print_utf8_string( const byte *p, size_t n ) {}
397 void tty_print_utf8_string2( const byte *p, size_t n, size_t max_n ) {}
398 char *tty_get( const char *prompt ) { return NULL;}
399 char *tty_get_hidden( const char *prompt ) {return NULL; }
400 void tty_kill_prompt(void) {}
401 int tty_get_answer_is_yes( const char *prompt ) {return 0;}
402 int tty_no_terminal(int onoff) {return 0;}
403 #ifdef HAVE_LIBREADLINE
404 void tty_enable_completion(rl_completion_func_t *completer) {}
405 void tty_disable_completion(void) {}
406 #endif
408 /* We do not do any locking, so use these stubs here */
409 void disable_dotlock(void) {}
410 DOTLOCK create_dotlock( const char *file_to_lock ) { return NULL; }
411 void destroy_dotlock (DOTLOCK h) {}
412 int make_dotlock( DOTLOCK h, long timeout ) { return 0;}
413 int release_dotlock( DOTLOCK h ) {return 0;}
414 void remove_lockfiles(void) {}