chiark / gitweb /
asshelp.c: add a lot of debug logging
[gnupg2.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 <https://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   oOutput         = 'o',
59   oBatch          = 500,
60   oKeyring,
61   oIgnoreTimeConflict,
62   oStatusFD,
63   oLoggerFD,
64   oHomedir,
65   oWeakDigest,
66   oEnableSpecialFilenames,
67   aTest
68 };
69
70
71 static ARGPARSE_OPTS opts[] = {
72   ARGPARSE_group (300, N_("@\nOptions:\n ")),
73
74   ARGPARSE_s_n (oVerbose, "verbose", N_("verbose")),
75   ARGPARSE_s_n (oQuiet,   "quiet",   N_("be somewhat more quiet")),
76   ARGPARSE_s_s (oKeyring, "keyring",
77                 N_("|FILE|take the keys from the keyring FILE")),
78   ARGPARSE_s_s (oOutput, "output", N_("|FILE|write output to FILE")),
79   ARGPARSE_s_n (oIgnoreTimeConflict, "ignore-time-conflict",
80                 N_("make timestamp conflicts only a warning")),
81   ARGPARSE_s_i (oStatusFD, "status-fd",
82                 N_("|FD|write status info to this FD")),
83   ARGPARSE_s_i (oLoggerFD, "logger-fd", "@"),
84   ARGPARSE_s_s (oHomedir, "homedir", "@"),
85   ARGPARSE_s_s (oWeakDigest, "weak-digest",
86                 N_("|ALGO|reject signatures made with ALGO")),
87   ARGPARSE_s_n (oEnableSpecialFilenames, "enable-special-filenames", "@"),
88
89   ARGPARSE_end ()
90 };
91
92
93
94 int g10_errors_seen = 0;
95
96
97 static char *
98 make_libversion (const char *libname, const char *(*getfnc)(const char*))
99 {
100   const char *s;
101   char *result;
102
103   s = getfnc (NULL);
104   result = xmalloc (strlen (libname) + 1 + strlen (s) + 1);
105   strcpy (stpcpy (stpcpy (result, libname), " "), s);
106   return result;
107 }
108
109 static const char *
110 my_strusage( int level )
111 {
112   static char *ver_gcry;
113   const char *p;
114
115   switch (level)
116     {
117     case 11: p = "@GPG@v (GnuPG)";
118       break;
119     case 13: p = VERSION; break;
120     case 17: p = PRINTABLE_OS_NAME; break;
121     case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
122
123     case 1:
124     case 40: p = _("Usage: gpgv [options] [files] (-h for help)");
125       break;
126     case 41: p = _("Syntax: gpgv [options] [files]\n"
127                    "Check signatures against known trusted keys\n");
128         break;
129
130     case 20:
131       if (!ver_gcry)
132         ver_gcry = make_libversion ("libgcrypt", gcry_check_version);
133       p = ver_gcry;
134       break;
135
136
137     default: p = NULL;
138     }
139   return p;
140 }
141
142
143
144 int
145 main( int argc, char **argv )
146 {
147   ARGPARSE_ARGS pargs;
148   int rc=0;
149   strlist_t sl;
150   strlist_t nrings = NULL;
151   unsigned configlineno;
152   ctrl_t ctrl;
153
154   early_system_init ();
155   set_strusage (my_strusage);
156   log_set_prefix ("gpgv", GPGRT_LOG_WITH_PREFIX);
157
158   /* Make sure that our subsystems are ready.  */
159   i18n_init();
160   init_common_subsystems (&argc, &argv);
161
162   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
163
164   gnupg_init_signals (0, NULL);
165
166   opt.command_fd = -1; /* no command fd */
167   opt.keyserver_options.options |= KEYSERVER_AUTO_KEY_RETRIEVE;
168   opt.trust_model = TM_ALWAYS;
169   opt.no_sig_cache = 1;
170   opt.flags.require_cross_cert = 1;
171   opt.batch = 1;
172   opt.answer_yes = 1;
173
174   opt.weak_digests = NULL;
175
176   tty_no_terminal(1);
177   tty_batchmode(1);
178   dotlock_disable ();
179   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
180   additional_weak_digest("MD5");
181
182   pargs.argc = &argc;
183   pargs.argv = &argv;
184   pargs.flags=  1;  /* do not remove the args */
185   while (optfile_parse( NULL, NULL, &configlineno, &pargs, opts))
186     {
187       switch (pargs.r_opt)
188         {
189         case oQuiet: opt.quiet = 1; break;
190         case oVerbose:
191           opt.verbose++;
192           opt.list_sigs=1;
193           gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose);
194           break;
195         case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break;
196         case oOutput: opt.outfile = pargs.r.ret_str; break;
197         case oStatusFD: set_status_fd( pargs.r.ret_int ); break;
198         case oLoggerFD:
199           log_set_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
200           break;
201         case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break;
202         case oWeakDigest:
203           additional_weak_digest(pargs.r.ret_str);
204           break;
205         case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;
206         case oEnableSpecialFilenames:
207           enable_special_filenames ();
208           break;
209         default : pargs.err = ARGPARSE_PRINT_ERROR; break;
210         }
211     }
212
213   if (log_get_errorcount (0))
214     g10_exit(2);
215
216   if (opt.verbose > 1)
217     set_packet_list_mode(1);
218
219   /* Note: We open all keyrings in read-only mode.  */
220   if (!nrings)  /* No keyring given: use default one. */
221     keydb_add_resource ("trustedkeys" EXTSEP_S "kbx",
222                         (KEYDB_RESOURCE_FLAG_READONLY
223                          |KEYDB_RESOURCE_FLAG_GPGVDEF));
224   for (sl = nrings; sl; sl = sl->next)
225     keydb_add_resource (sl->d, KEYDB_RESOURCE_FLAG_READONLY);
226
227   FREE_STRLIST (nrings);
228
229   ctrl = xcalloc (1, sizeof *ctrl);
230
231   if ((rc = verify_signatures (ctrl, argc, argv)))
232     log_error("verify signatures failed: %s\n", gpg_strerror (rc) );
233
234   xfree (ctrl);
235
236   /* cleanup */
237   g10_exit (0);
238   return 8; /*NOTREACHED*/
239 }
240
241
242 void
243 g10_exit( int rc )
244 {
245   rc = rc? rc : log_get_errorcount(0)? 2 : g10_errors_seen? 1 : 0;
246   exit(rc );
247 }
248
249
250 /* Stub:
251  * We have to override the trustcheck from pkclist.c because
252  * this utility assumes that all keys in the keyring are trustworthy
253  */
254 int
255 check_signatures_trust (ctrl_t ctrl, PKT_signature *sig)
256 {
257   (void)ctrl;
258   (void)sig;
259   return 0;
260 }
261
262 void
263 read_trust_options(byte *trust_model, ulong *created, ulong *nextcheck,
264                    byte *marginals, byte *completes, byte *cert_depth,
265                    byte *min_cert_level)
266 {
267   (void)trust_model;
268   (void)created;
269   (void)nextcheck;
270   (void)marginals;
271   (void)completes;
272   (void)cert_depth;
273   (void)min_cert_level;
274 }
275
276 /* Stub:
277  * We don't have the trustdb , so we have to provide some stub functions
278  * instead
279  */
280
281 int
282 cache_disabled_value(PKT_public_key *pk)
283 {
284   (void)pk;
285   return 0;
286 }
287
288 void
289 check_trustdb_stale (ctrl_t ctrl)
290 {
291   (void)ctrl;
292 }
293
294 int
295 get_validity_info (ctrl_t ctrl, kbnode_t kb, PKT_public_key *pk,
296                    PKT_user_id *uid)
297 {
298   (void)ctrl;
299   (void)kb;
300   (void)pk;
301   (void)uid;
302   return '?';
303 }
304
305 unsigned int
306 get_validity (ctrl_t ctrl, kbnode_t kb, PKT_public_key *pk, PKT_user_id *uid,
307               PKT_signature *sig, int may_ask)
308 {
309   (void)ctrl;
310   (void)kb;
311   (void)pk;
312   (void)uid;
313   (void)sig;
314   (void)may_ask;
315   return 0;
316 }
317
318 const char *
319 trust_value_to_string (unsigned int value)
320 {
321   (void)value;
322   return "err";
323 }
324
325 const char *
326 uid_trust_string_fixed (ctrl_t ctrl, PKT_public_key *key, PKT_user_id *uid)
327 {
328   (void)ctrl;
329   (void)key;
330   (void)uid;
331   return "err";
332 }
333
334 int
335 get_ownertrust_info (PKT_public_key *pk)
336 {
337   (void)pk;
338   return '?';
339 }
340
341 unsigned int
342 get_ownertrust (PKT_public_key *pk)
343 {
344   (void)pk;
345   return TRUST_UNKNOWN;
346 }
347
348
349 /* Stubs:
350  * Because we only work with trusted keys, it does not make sense to
351  * get them from a keyserver
352  */
353
354 struct keyserver_spec *
355 keyserver_match (struct keyserver_spec *spec)
356 {
357   (void)spec;
358   return NULL;
359 }
360
361 int
362 keyserver_any_configured (ctrl_t ctrl)
363 {
364   (void)ctrl;
365   return 0;
366 }
367
368 int
369 keyserver_import_keyid (u32 *keyid, void *dummy, int quick)
370 {
371   (void)keyid;
372   (void)dummy;
373   (void)quick;
374   return -1;
375 }
376
377 int
378 keyserver_import_fprint (ctrl_t ctrl, const byte *fprint,size_t fprint_len,
379                          struct keyserver_spec *keyserver, int quick)
380 {
381   (void)ctrl;
382   (void)fprint;
383   (void)fprint_len;
384   (void)keyserver;
385   (void)quick;
386   return -1;
387 }
388
389 int
390 keyserver_import_cert (const char *name)
391 {
392   (void)name;
393   return -1;
394 }
395
396 int
397 keyserver_import_pka (const char *name,unsigned char *fpr)
398 {
399   (void)name;
400   (void)fpr;
401   return -1;
402 }
403
404 gpg_error_t
405 keyserver_import_wkd (ctrl_t ctrl, const char *name, int quick,
406                       unsigned char **fpr, size_t *fpr_len)
407 {
408   (void)ctrl;
409   (void)name;
410   (void)quick;
411   (void)fpr;
412   (void)fpr_len;
413   return GPG_ERR_BUG;
414 }
415
416 int
417 keyserver_import_name (const char *name,struct keyserver_spec *spec)
418 {
419   (void)name;
420   (void)spec;
421   return -1;
422 }
423
424 int
425 keyserver_import_ldap (const char *name)
426 {
427   (void)name;
428   return -1;
429 }
430
431
432 gpg_error_t
433 read_key_from_file (ctrl_t ctrl, const char *fname, kbnode_t *r_keyblock)
434 {
435   (void)ctrl;
436   (void)fname;
437   (void)r_keyblock;
438   return -1;
439 }
440
441
442 /* Stub:
443  * No encryption here but mainproc links to these functions.
444  */
445 gpg_error_t
446 get_session_key (ctrl_t ctrl, PKT_pubkey_enc *k, DEK *dek)
447 {
448   (void)ctrl;
449   (void)k;
450   (void)dek;
451   return GPG_ERR_GENERAL;
452 }
453
454 /* Stub: */
455 gpg_error_t
456 get_override_session_key (DEK *dek, const char *string)
457 {
458   (void)dek;
459   (void)string;
460   return GPG_ERR_GENERAL;
461 }
462
463 /* Stub: */
464 int
465 decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
466 {
467   (void)ctrl;
468   (void)procctx;
469   (void)ed;
470   (void)dek;
471   return GPG_ERR_GENERAL;
472 }
473
474
475 /* Stub:
476  * No interactive commands, so we don't need the helptexts
477  */
478 void
479 display_online_help (const char *keyword)
480 {
481   (void)keyword;
482 }
483
484 /* Stub:
485  * We don't use secret keys, but getkey.c links to this
486  */
487 int
488 check_secret_key (PKT_public_key *pk, int n)
489 {
490   (void)pk;
491   (void)n;
492   return GPG_ERR_GENERAL;
493 }
494
495 /* Stub:
496  * No secret key, so no passphrase needed
497  */
498 DEK *
499 passphrase_to_dek (int cipher_algo, STRING2KEY *s2k, int create, int nocache,
500                    const char *tmp, int *canceled)
501 {
502   (void)cipher_algo;
503   (void)s2k;
504   (void)create;
505   (void)nocache;
506   (void)tmp;
507
508   if (canceled)
509     *canceled = 0;
510   return NULL;
511 }
512
513 void
514 passphrase_clear_cache (const char *cacheid)
515 {
516   (void)cacheid;
517 }
518
519 struct keyserver_spec *
520 parse_preferred_keyserver(PKT_signature *sig)
521 {
522   (void)sig;
523   return NULL;
524 }
525
526 struct keyserver_spec *
527 parse_keyserver_uri (const char *uri, int require_scheme,
528                      const char *configname, unsigned int configlineno)
529 {
530   (void)uri;
531   (void)require_scheme;
532   (void)configname;
533   (void)configlineno;
534   return NULL;
535 }
536
537 void
538 free_keyserver_spec (struct keyserver_spec *keyserver)
539 {
540   (void)keyserver;
541 }
542
543 /* Stubs to avoid linking to photoid.c */
544 void
545 show_photos (const struct user_attribute *attrs, int count, PKT_public_key *pk)
546 {
547   (void)attrs;
548   (void)count;
549   (void)pk;
550 }
551
552 int
553 parse_image_header (const struct user_attribute *attr, byte *type, u32 *len)
554 {
555   (void)attr;
556   (void)type;
557   (void)len;
558   return 0;
559 }
560
561 char *
562 image_type_to_string (byte type, int string)
563 {
564   (void)type;
565   (void)string;
566   return NULL;
567 }
568
569 #ifdef ENABLE_CARD_SUPPORT
570 int
571 agent_scd_getattr (const char *name, struct agent_card_info_s *info)
572 {
573   (void)name;
574   (void)info;
575   return 0;
576 }
577 #endif /* ENABLE_CARD_SUPPORT */
578
579 /* We do not do any locking, so use these stubs here */
580 void
581 dotlock_disable (void)
582 {
583 }
584
585 dotlock_t
586 dotlock_create (const char *file_to_lock, unsigned int flags)
587 {
588   (void)file_to_lock;
589   (void)flags;
590   return NULL;
591 }
592
593 void
594 dotlock_destroy (dotlock_t h)
595 {
596   (void)h;
597 }
598
599 int
600 dotlock_take (dotlock_t h, long timeout)
601 {
602   (void)h;
603   (void)timeout;
604   return 0;
605 }
606
607 int
608 dotlock_release (dotlock_t h)
609 {
610   (void)h;
611   return 0;
612 }
613
614 void
615 dotlock_remove_lockfiles (void)
616 {
617 }
618
619 gpg_error_t
620 agent_probe_secret_key (ctrl_t ctrl, PKT_public_key *pk)
621 {
622   (void)ctrl;
623   (void)pk;
624   return gpg_error (GPG_ERR_NO_SECKEY);
625 }
626
627 gpg_error_t
628 agent_probe_any_secret_key (ctrl_t ctrl, kbnode_t keyblock)
629 {
630   (void)ctrl;
631   (void)keyblock;
632   return gpg_error (GPG_ERR_NO_SECKEY);
633 }
634
635 gpg_error_t
636 agent_get_keyinfo (ctrl_t ctrl, const char *hexkeygrip,
637                    char **r_serialno, int *r_cleartext)
638 {
639   (void)ctrl;
640   (void)hexkeygrip;
641   (void)r_cleartext;
642   *r_serialno = NULL;
643   return gpg_error (GPG_ERR_NO_SECKEY);
644 }
645
646 gpg_error_t
647 gpg_dirmngr_get_pka (ctrl_t ctrl, const char *userid,
648                      unsigned char **r_fpr, size_t *r_fprlen,
649                      char **r_url)
650 {
651   (void)ctrl;
652   (void)userid;
653   if (r_fpr)
654     *r_fpr = NULL;
655   if (r_fprlen)
656     *r_fprlen = 0;
657   if (r_url)
658     *r_url = NULL;
659   return gpg_error (GPG_ERR_NOT_FOUND);
660 }
661
662 gpg_error_t
663 export_pubkey_buffer (ctrl_t ctrl, const char *keyspec, unsigned int options,
664                       export_stats_t stats,
665                       kbnode_t *r_keyblock, void **r_data, size_t *r_datalen)
666 {
667   (void)ctrl;
668   (void)keyspec;
669   (void)options;
670   (void)stats;
671
672   *r_keyblock = NULL;
673   *r_data = NULL;
674   *r_datalen = 0;
675   return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
676 }
677
678 gpg_error_t
679 tofu_write_tfs_record (ctrl_t ctrl, estream_t fp,
680                        PKT_public_key *pk, const char *user_id)
681 {
682   (void)ctrl;
683   (void)fp;
684   (void)pk;
685   (void)user_id;
686   return gpg_error (GPG_ERR_GENERAL);
687 }
688
689 gpg_error_t
690 tofu_get_policy (ctrl_t ctrl, PKT_public_key *pk, PKT_user_id *user_id,
691                  enum tofu_policy *policy)
692 {
693   (void)ctrl;
694   (void)pk;
695   (void)user_id;
696   (void)policy;
697   return gpg_error (GPG_ERR_GENERAL);
698 }
699
700 const char *
701 tofu_policy_str (enum tofu_policy policy)
702 {
703   (void)policy;
704
705   return "unknown";
706 }
707
708 void
709 tofu_begin_batch_update (ctrl_t ctrl)
710 {
711   (void)ctrl;
712 }
713
714 void
715 tofu_end_batch_update (ctrl_t ctrl)
716 {
717   (void)ctrl;
718 }
719
720 gpg_error_t
721 tofu_notice_key_changed (ctrl_t ctrl, kbnode_t kb)
722 {
723   (void) ctrl;
724   (void) kb;
725
726   return 0;
727 }