Imported Upstream version 2.1.9
[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 "util.h"
39 #include "packet.h"
40 #include "iobuf.h"
41 #include "main.h"
42 #include "options.h"
43 #include "keydb.h"
44 #include "trustdb.h"
45 #include "filter.h"
46 #include "ttyio.h"
47 #include "i18n.h"
48 #include "sysutils.h"
49 #include "status.h"
50 #include "call-agent.h"
51 #include "../common/init.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 = "@GPG@v (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   ctrl_t ctrl;
146
147   early_system_init ();
148   set_strusage (my_strusage);
149   log_set_prefix ("gpgv", 1);
150
151   /* Make sure that our subsystems are ready.  */
152   i18n_init();
153   init_common_subsystems (&argc, &argv);
154
155   if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )
156     {
157       log_fatal ( _("%s is too old (need %s, have %s)\n"), "libgcrypt",
158                   NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL) );
159     }
160   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
161
162   gnupg_init_signals (0, NULL);
163
164   opt.command_fd = -1; /* no command fd */
165   opt.keyserver_options.options |= KEYSERVER_AUTO_KEY_RETRIEVE;
166   opt.trust_model = TM_ALWAYS;
167   opt.batch = 1;
168
169   opt.homedir = default_homedir ();
170
171   tty_no_terminal(1);
172   tty_batchmode(1);
173   dotlock_disable ();
174   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
175
176   pargs.argc = &argc;
177   pargs.argv = &argv;
178   pargs.flags=  1;  /* do not remove the args */
179   while (optfile_parse( NULL, NULL, &configlineno, &pargs, opts))
180     {
181       switch (pargs.r_opt)
182         {
183         case oQuiet: opt.quiet = 1; break;
184         case oVerbose:
185           opt.verbose++;
186           opt.list_sigs=1;
187           gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose);
188           break;
189         case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break;
190         case oStatusFD: set_status_fd( pargs.r.ret_int ); break;
191         case oLoggerFD:
192           log_set_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
193           break;
194         case oHomedir: opt.homedir = pargs.r.ret_str; break;
195         case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;
196         default : pargs.err = ARGPARSE_PRINT_ERROR; break;
197         }
198     }
199
200   if (log_get_errorcount (0))
201     g10_exit(2);
202
203   if (opt.verbose > 1)
204     set_packet_list_mode(1);
205
206   /* Note: We open all keyrings in read-only mode.  */
207   if (!nrings)  /* No keyring given: use default one. */
208     keydb_add_resource ("trustedkeys" EXTSEP_S "kbx",
209                         (KEYDB_RESOURCE_FLAG_READONLY
210                          |KEYDB_RESOURCE_FLAG_GPGVDEF));
211   for (sl = nrings; sl; sl = sl->next)
212     keydb_add_resource (sl->d, KEYDB_RESOURCE_FLAG_READONLY);
213
214   FREE_STRLIST (nrings);
215
216   ctrl = xcalloc (1, sizeof *ctrl);
217
218   if ((rc = verify_signatures (ctrl, argc, argv)))
219     log_error("verify signatures failed: %s\n", gpg_strerror (rc) );
220
221   xfree (ctrl);
222
223   /* cleanup */
224   g10_exit (0);
225   return 8; /*NOTREACHED*/
226 }
227
228
229 void
230 g10_exit( int rc )
231 {
232   rc = rc? rc : log_get_errorcount(0)? 2 : g10_errors_seen? 1 : 0;
233   exit(rc );
234 }
235
236
237 /* Stub:
238  * We have to override the trustcheck from pkclist.c becuase
239  * this utility assumes that all keys in the keyring are trustworthy
240  */
241 int
242 check_signatures_trust( PKT_signature *sig )
243 {
244   (void)sig;
245   return 0;
246 }
247
248 void
249 read_trust_options(byte *trust_model, ulong *created, ulong *nextcheck,
250                    byte *marginals, byte *completes, byte *cert_depth,
251                    byte *min_cert_level)
252 {
253   (void)trust_model;
254   (void)created;
255   (void)nextcheck;
256   (void)marginals;
257   (void)completes;
258   (void)cert_depth;
259   (void)min_cert_level;
260 }
261
262 /* Stub:
263  * We don't have the trustdb , so we have to provide some stub functions
264  * instead
265  */
266
267 int
268 cache_disabled_value(PKT_public_key *pk)
269 {
270   (void)pk;
271   return 0;
272 }
273
274 void
275 check_trustdb_stale(void)
276 {
277 }
278
279 int
280 get_validity_info (PKT_public_key *pk, PKT_user_id *uid)
281 {
282   (void)pk;
283   (void)uid;
284   return '?';
285 }
286
287 unsigned int
288 get_validity (PKT_public_key *pk, PKT_user_id *uid)
289 {
290   (void)pk;
291   (void)uid;
292   return 0;
293 }
294
295 const char *
296 trust_value_to_string (unsigned int value)
297 {
298   (void)value;
299   return "err";
300 }
301
302 const char *
303 uid_trust_string_fixed (PKT_public_key *key, PKT_user_id *uid)
304 {
305   (void)key;
306   (void)uid;
307   return "err";
308 }
309
310 int
311 get_ownertrust_info (PKT_public_key *pk)
312 {
313   (void)pk;
314   return '?';
315 }
316
317 unsigned int
318 get_ownertrust (PKT_public_key *pk)
319 {
320   (void)pk;
321   return TRUST_UNKNOWN;
322 }
323
324
325 /* Stubs:
326  * Because we only work with trusted keys, it does not make sense to
327  * get them from a keyserver
328  */
329
330 struct keyserver_spec *
331 keyserver_match (struct keyserver_spec *spec)
332 {
333   (void)spec;
334   return NULL;
335 }
336
337 int
338 keyserver_import_keyid (u32 *keyid, void *dummy)
339 {
340   (void)keyid;
341   (void)dummy;
342   return -1;
343 }
344
345 int
346 keyserver_import_cert (const char *name)
347 {
348   (void)name;
349   return -1;
350 }
351
352 int
353 keyserver_import_pka (const char *name,unsigned char *fpr)
354 {
355   (void)name;
356   (void)fpr;
357   return -1;
358 }
359
360 int
361 keyserver_import_name (const char *name,struct keyserver_spec *spec)
362 {
363   (void)name;
364   (void)spec;
365   return -1;
366 }
367
368 int
369 keyserver_import_ldap (const char *name)
370 {
371   (void)name;
372   return -1;
373 }
374
375 /* Stub:
376  * No encryption here but mainproc links to these functions.
377  */
378 gpg_error_t
379 get_session_key (PKT_pubkey_enc *k, DEK *dek)
380 {
381   (void)k;
382   (void)dek;
383   return GPG_ERR_GENERAL;
384 }
385
386 /* Stub: */
387 gpg_error_t
388 get_override_session_key (DEK *dek, const char *string)
389 {
390   (void)dek;
391   (void)string;
392   return GPG_ERR_GENERAL;
393 }
394
395 /* Stub: */
396 int
397 decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
398 {
399   (void)ctrl;
400   (void)procctx;
401   (void)ed;
402   (void)dek;
403   return GPG_ERR_GENERAL;
404 }
405
406
407 /* Stub:
408  * No interactive commands, so we don't need the helptexts
409  */
410 void
411 display_online_help (const char *keyword)
412 {
413   (void)keyword;
414 }
415
416 /* Stub:
417  * We don't use secret keys, but getkey.c links to this
418  */
419 int
420 check_secret_key (PKT_public_key *pk, int n)
421 {
422   (void)pk;
423   (void)n;
424   return GPG_ERR_GENERAL;
425 }
426
427 /* Stub:
428  * No secret key, so no passphrase needed
429  */
430 DEK *
431 passphrase_to_dek (u32 *keyid, int pubkey_algo,
432                    int cipher_algo, STRING2KEY *s2k, int mode,
433                    const char *tmp, int *canceled)
434 {
435   (void)keyid;
436   (void)pubkey_algo;
437   (void)cipher_algo;
438   (void)s2k;
439   (void)mode;
440   (void)tmp;
441
442   if (canceled)
443     *canceled = 0;
444   return NULL;
445 }
446
447 void
448 passphrase_clear_cache (u32 *keyid, const char *cacheid, int algo)
449 {
450   (void)keyid;
451   (void)cacheid;
452   (void)algo;
453 }
454
455 struct keyserver_spec *
456 parse_preferred_keyserver(PKT_signature *sig)
457 {
458   (void)sig;
459   return NULL;
460 }
461
462 struct keyserver_spec *
463 parse_keyserver_uri (const char *uri, int require_scheme,
464                      const char *configname, unsigned int configlineno)
465 {
466   (void)uri;
467   (void)require_scheme;
468   (void)configname;
469   (void)configlineno;
470   return NULL;
471 }
472
473 void
474 free_keyserver_spec (struct keyserver_spec *keyserver)
475 {
476   (void)keyserver;
477 }
478
479 /* Stubs to avoid linking to photoid.c */
480 void
481 show_photos (const struct user_attribute *attrs, int count, PKT_public_key *pk)
482 {
483   (void)attrs;
484   (void)count;
485   (void)pk;
486 }
487
488 int
489 parse_image_header (const struct user_attribute *attr, byte *type, u32 *len)
490 {
491   (void)attr;
492   (void)type;
493   (void)len;
494   return 0;
495 }
496
497 char *
498 image_type_to_string (byte type, int string)
499 {
500   (void)type;
501   (void)string;
502   return NULL;
503 }
504
505 #ifdef ENABLE_CARD_SUPPORT
506 int
507 agent_scd_getattr (const char *name, struct agent_card_info_s *info)
508 {
509   (void)name;
510   (void)info;
511   return 0;
512 }
513 #endif /* ENABLE_CARD_SUPPORT */
514
515 /* We do not do any locking, so use these stubs here */
516 void
517 dotlock_disable (void)
518 {
519 }
520
521 dotlock_t
522 dotlock_create (const char *file_to_lock, unsigned int flags)
523 {
524   (void)file_to_lock;
525   (void)flags;
526   return NULL;
527 }
528
529 void
530 dotlock_destroy (dotlock_t h)
531 {
532   (void)h;
533 }
534
535 int
536 dotlock_take (dotlock_t h, long timeout)
537 {
538   (void)h;
539   (void)timeout;
540   return 0;
541 }
542
543 int
544 dotlock_release (dotlock_t h)
545 {
546   (void)h;
547   return 0;
548 }
549
550 void
551 dotlock_remove_lockfiles (void)
552 {
553 }
554
555 gpg_error_t
556 agent_probe_secret_key (ctrl_t ctrl, PKT_public_key *pk)
557 {
558   (void)ctrl;
559   (void)pk;
560   return gpg_error (GPG_ERR_NO_SECKEY);
561 }
562
563 gpg_error_t
564 agent_probe_any_secret_key (ctrl_t ctrl, kbnode_t keyblock)
565 {
566   (void)ctrl;
567   (void)keyblock;
568   return gpg_error (GPG_ERR_NO_SECKEY);
569 }
570
571 gpg_error_t
572 agent_get_keyinfo (ctrl_t ctrl, const char *hexkeygrip, char **r_serialno)
573 {
574   (void)ctrl;
575   (void)hexkeygrip;
576   *r_serialno = NULL;
577   return gpg_error (GPG_ERR_NO_SECKEY);
578 }
579
580 gpg_error_t
581 gpg_dirmngr_get_pka (ctrl_t ctrl, const char *userid,
582                      unsigned char **r_fpr, size_t *r_fprlen,
583                      char **r_url)
584 {
585   (void)ctrl;
586   (void)userid;
587   if (r_fpr)
588     *r_fpr = NULL;
589   if (r_fprlen)
590     *r_fprlen = 0;
591   if (r_url)
592     *r_url = NULL;
593   return gpg_error (GPG_ERR_NOT_FOUND);
594 }
595
596 gpg_error_t
597 export_pubkey_buffer (ctrl_t ctrl, const char *keyspec, unsigned int options,
598                       kbnode_t *r_keyblock, void **r_data, size_t *r_datalen)
599 {
600   (void)ctrl;
601   (void)keyspec;
602   (void)options;
603
604   *r_keyblock = NULL;
605   *r_data = NULL;
606   *r_datalen = 0;
607   return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
608 }