Imported Upstream version 2.0.31
[platform/upstream/gpg2.git] / g10 / gpgv.c
1 /* gpgv.c - The GnuPG signature verify utility
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2006,
3  *               2008, 2009, 2012 Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
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
35
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"
52
53
54 enum cmd_and_opt_values {
55   aNull = 0,
56   oQuiet          = 'q',
57   oVerbose        = 'v',
58   oBatch          = 500,
59   oKeyring,
60   oIgnoreTimeConflict,                      
61   oStatusFD,
62   oLoggerFD,
63   oHomedir,
64   aTest
65 };
66
67
68 static ARGPARSE_OPTS opts[] = {
69   ARGPARSE_group (300, N_("@\nOptions:\n ")),
70   
71   ARGPARSE_s_n (oVerbose, "verbose", N_("verbose")),
72   ARGPARSE_s_n (oQuiet,   "quiet",   N_("be somewhat more quiet")),
73   ARGPARSE_s_s (oKeyring, "keyring", 
74                 N_("|FILE|take the keys from the keyring FILE")),
75   ARGPARSE_s_n (oIgnoreTimeConflict, "ignore-time-conflict",
76                 N_("make timestamp conflicts only a warning")),
77   ARGPARSE_s_i (oStatusFD, "status-fd",
78                 N_("|FD|write status info to this FD")),
79   ARGPARSE_s_i (oLoggerFD, "logger-fd", "@"),
80   ARGPARSE_s_s (oHomedir, "homedir", "@"),
81
82   ARGPARSE_end ()
83 };
84
85
86
87 int g10_errors_seen = 0;
88
89
90 static char *
91 make_libversion (const char *libname, const char *(*getfnc)(const char*))
92 {
93   const char *s;
94   char *result;
95   
96   s = getfnc (NULL);
97   result = xmalloc (strlen (libname) + 1 + strlen (s) + 1);
98   strcpy (stpcpy (stpcpy (result, libname), " "), s);
99   return result;
100 }
101
102 static const char *
103 my_strusage( int level )
104 {
105   static char *ver_gcry;
106   const char *p;
107
108   switch (level)
109     {
110     case 11: p = "gpgv (GnuPG)";
111       break;
112     case 13: p = VERSION; break;
113     case 17: p = PRINTABLE_OS_NAME; break;
114     case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
115
116     case 1:
117     case 40: p = _("Usage: gpgv [options] [files] (-h for help)");
118       break;
119     case 41: p = _("Syntax: gpgv [options] [files]\n"
120                    "Check signatures against known trusted keys\n");
121         break;
122
123     case 20:
124       if (!ver_gcry)
125         ver_gcry = make_libversion ("libgcrypt", gcry_check_version);
126       p = ver_gcry;
127       break;
128
129
130     default: p = NULL;
131     }
132   return p;
133 }
134
135
136
137 int
138 main( int argc, char **argv )
139 {
140   ARGPARSE_ARGS pargs;
141   int rc=0;
142   strlist_t sl;
143   strlist_t nrings=NULL;
144   unsigned configlineno;
145
146   set_strusage (my_strusage);
147   log_set_prefix ("gpgv", 1);
148
149   /* Make sure that our subsystems are ready.  */
150   i18n_init();
151   init_common_subsystems ();
152
153   if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )
154     {
155       log_fatal ( _("%s is too old (need %s, have %s)\n"), "libgcrypt",
156                   NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL) );
157     }
158   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
159
160   gnupg_init_signals (0, NULL);
161
162   opt.command_fd = -1; /* no command fd */
163   opt.pgp2_workarounds = 1;
164   opt.keyserver_options.options|=KEYSERVER_AUTO_KEY_RETRIEVE;
165   opt.trust_model = TM_ALWAYS;
166   opt.no_sig_cache = 1;
167   opt.flags.require_cross_cert = 1;
168   opt.batch = 1;
169
170   opt.homedir = default_homedir ();
171
172   tty_no_terminal(1);
173   tty_batchmode(1);
174   disable_dotlock();
175
176   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
177
178   pargs.argc = &argc;
179   pargs.argv = &argv;
180   pargs.flags=  1;  /* do not remove the args */
181   while (optfile_parse( NULL, NULL, &configlineno, &pargs, opts))
182     {
183       switch (pargs.r_opt)
184         {
185         case oQuiet: opt.quiet = 1; break;
186         case oVerbose: 
187           opt.verbose++; 
188           opt.list_sigs=1;
189           gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose);
190           break;
191         case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break;
192         case oStatusFD: set_status_fd( pargs.r.ret_int ); break;
193         case oLoggerFD: 
194           log_set_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
195           break;
196         case oHomedir: opt.homedir = pargs.r.ret_str; break;
197         case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;
198         default : pargs.err = ARGPARSE_PRINT_ERROR; break;
199         }
200     }
201   
202   if (log_get_errorcount (0))
203     g10_exit(2);
204
205   if (opt.verbose > 1)
206     set_packet_list_mode(1);
207
208   /* Note: We open all keyrings in read-only mode (flag value: 8).  */
209   if (!nrings)  /* No keyring given: use default one. */
210     keydb_add_resource ("trustedkeys" EXTSEP_S "gpg", 8, 0);
211   for (sl = nrings; sl; sl = sl->next)
212     keydb_add_resource (sl->d, 8, 0 );
213    
214   FREE_STRLIST (nrings);
215     
216   if ( (rc = verify_signatures( argc, argv ) ))
217     log_error("verify signatures failed: %s\n", g10_errstr(rc) );
218   
219   /* cleanup */
220   g10_exit (0);
221   return 8; /*NOTREACHED*/
222 }
223
224
225 void
226 g10_exit( int rc )
227 {
228   rc = rc? rc : log_get_errorcount(0)? 2 : g10_errors_seen? 1 : 0;
229   exit(rc );
230 }
231
232
233 /* Stub:
234  * We have to override the trustcheck from pkclist.c becuase 
235  * this utility assumes that all keys in the keyring are trustworthy
236  */
237 int
238 check_signatures_trust( PKT_signature *sig )
239 {
240   (void)sig;
241   return 0;
242 }
243
244 void
245 read_trust_options(byte *trust_model, ulong *created, ulong *nextcheck,
246                    byte *marginals, byte *completes, byte *cert_depth,
247                    byte *min_cert_level)
248 {
249   (void)trust_model;
250   (void)created;
251   (void)nextcheck;
252   (void)marginals;
253   (void)completes;
254   (void)cert_depth;
255   (void)min_cert_level;
256 }
257
258 /* Stub: 
259  * We don't have the trustdb , so we have to provide some stub functions
260  * instead
261  */
262
263 int
264 cache_disabled_value(PKT_public_key *pk)
265 {
266   (void)pk;
267   return 0;
268 }
269
270 void
271 check_trustdb_stale(void) 
272 {
273 }
274
275 int
276 get_validity_info (PKT_public_key *pk, PKT_user_id *uid)
277 {
278   (void)pk;
279   (void)uid;
280   return '?';
281 }
282
283 unsigned int
284 get_validity (PKT_public_key *pk, PKT_user_id *uid)
285 {
286   (void)pk;
287   (void)uid;
288   return 0;
289 }
290
291 const char *
292 trust_value_to_string (unsigned int value)
293 {
294   (void)value;
295   return "err";
296 }
297
298 const char *
299 uid_trust_string_fixed (PKT_public_key *key, PKT_user_id *uid)
300 {
301   (void)key;
302   (void)uid;
303   return "err";
304 }
305
306 int
307 get_ownertrust_info (PKT_public_key *pk)
308 {
309   (void)pk;
310   return '?';
311 }
312
313 unsigned int
314 get_ownertrust (PKT_public_key *pk)
315 {
316   (void)pk;
317   return TRUST_UNKNOWN;
318 }
319
320
321 /* Stubs:
322  * Because we only work with trusted keys, it does not make sense to
323  * get them from a keyserver
324  */
325
326 struct keyserver_spec *
327 keyserver_match (struct keyserver_spec *spec)
328 {
329   (void)spec;
330   return NULL; 
331 }
332
333 int
334 keyserver_import_keyid (u32 *keyid, void *dummy)
335 {
336   (void)keyid;
337   (void)dummy;
338   return -1;
339 }
340
341 int
342 keyserver_import_cert (const char *name)
343 {
344   (void)name;
345   return -1; 
346 }
347
348 int
349 keyserver_import_pka (const char *name,unsigned char *fpr)
350 {
351   (void)name;
352   (void)fpr;
353   return -1;
354 }
355
356 int
357 keyserver_import_name (const char *name,struct keyserver_spec *spec)
358 {
359   (void)name;
360   (void)spec;
361   return -1;
362 }
363
364 int
365 keyserver_import_ldap (const char *name)
366 {
367   (void)name;
368   return -1;
369 }
370
371 /* Stub:
372  * No encryption here but mainproc links to these functions.
373  */
374 int
375 get_session_key (PKT_pubkey_enc *k, DEK *dek)
376 {
377   (void)k;
378   (void)dek;
379   return G10ERR_GENERAL;
380 }
381
382 /* Stub: */
383 int
384 get_override_session_key (DEK *dek, const char *string)
385 {
386   (void)dek;
387   (void)string;
388   return G10ERR_GENERAL;
389 }
390
391 /* Stub: */
392 int
393 decrypt_data (void *procctx, PKT_encrypted *ed, DEK *dek)
394 {
395   (void)procctx;
396   (void)ed;
397   (void)dek;
398   return G10ERR_GENERAL;
399 }
400
401
402 /* Stub:
403  * No interactive commands, so we don't need the helptexts
404  */
405 void
406 display_online_help (const char *keyword)
407 {
408   (void)keyword;
409 }
410
411 /* Stub:
412  * We don't use secret keys, but getkey.c links to this
413  */
414 int
415 check_secret_key (PKT_secret_key *sk, int n)
416 {
417   (void)sk;
418   (void)n;
419   return G10ERR_GENERAL;
420 }
421
422 /* Stub:
423  * No secret key, so no passphrase needed 
424  */
425 DEK *
426 passphrase_to_dek (u32 *keyid, int pubkey_algo,
427                    int cipher_algo, STRING2KEY *s2k, int mode,
428                    const char *tmp, int *canceled)
429 {
430   (void)keyid;
431   (void)pubkey_algo;
432   (void)cipher_algo;
433   (void)s2k;
434   (void)mode;
435   (void)tmp;
436
437   if (canceled)
438     *canceled = 0;
439   return NULL;
440 }
441
442 void
443 passphrase_clear_cache (u32 *keyid, const char *cacheid, int algo)
444 {
445   (void)keyid;
446   (void)cacheid;
447   (void)algo;
448 }
449
450 struct keyserver_spec *
451 parse_preferred_keyserver(PKT_signature *sig) 
452 {
453   (void)sig;
454   return NULL;
455 }
456
457 struct keyserver_spec *
458 parse_keyserver_uri (const char *uri, int require_scheme,
459                      const char *configname, unsigned int configlineno)
460 {
461   (void)uri;
462   (void)require_scheme;
463   (void)configname;
464   (void)configlineno;
465   return NULL;
466 }
467
468 void 
469 free_keyserver_spec (struct keyserver_spec *keyserver)
470 {
471   (void)keyserver;
472 }
473
474 /* Stubs to avoid linking to photoid.c */
475 void 
476 show_photos (const struct user_attribute *attrs, int count, PKT_public_key *pk)
477 {
478   (void)attrs;
479   (void)count;
480   (void)pk;
481 }
482
483 int 
484 parse_image_header (const struct user_attribute *attr, byte *type, u32 *len)
485 {
486   (void)attr;
487   (void)type;
488   (void)len;
489   return 0;
490 }
491
492 char *
493 image_type_to_string (byte type, int string)
494 {
495   (void)type;
496   (void)string;
497   return NULL;
498 }
499
500 #ifdef ENABLE_CARD_SUPPORT
501 int 
502 agent_scd_getattr (const char *name, struct agent_card_info_s *info)
503 {
504   (void)name;
505   (void)info;
506   return 0;
507 }
508 #endif /* ENABLE_CARD_SUPPORT */
509
510 /* We do not do any locking, so use these stubs here */
511 void 
512 disable_dotlock (void)
513 {
514 }
515
516 DOTLOCK 
517 create_dotlock (const char *file_to_lock)
518 {
519   (void)file_to_lock;
520   return NULL;
521 }
522
523 void 
524 destroy_dotlock (DOTLOCK h)
525 {
526   (void)h;
527 }
528
529 int
530 make_dotlock (DOTLOCK h, long timeout)
531 {
532   (void)h;
533   (void)timeout;
534   return 0;
535 }
536
537 int
538 release_dotlock (DOTLOCK h)
539 {
540   (void)h;
541   return 0;
542 }
543
544 void 
545 remove_lockfiles (void)
546 {
547 }
548