chiark / gitweb /
dirmngr: Fix error handling.
[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:
198           set_status_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
199           break;
200         case oLoggerFD:
201           log_set_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
202           break;
203         case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break;
204         case oWeakDigest:
205           additional_weak_digest(pargs.r.ret_str);
206           break;
207         case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;
208         case oEnableSpecialFilenames:
209           enable_special_filenames ();
210           break;
211         default : pargs.err = ARGPARSE_PRINT_ERROR; break;
212         }
213     }
214
215   if (log_get_errorcount (0))
216     g10_exit(2);
217
218   if (opt.verbose > 1)
219     set_packet_list_mode(1);
220
221   /* Note: We open all keyrings in read-only mode.  */
222   if (!nrings)  /* No keyring given: use default one. */
223     keydb_add_resource ("trustedkeys" EXTSEP_S "kbx",
224                         (KEYDB_RESOURCE_FLAG_READONLY
225                          |KEYDB_RESOURCE_FLAG_GPGVDEF));
226   for (sl = nrings; sl; sl = sl->next)
227     keydb_add_resource (sl->d, KEYDB_RESOURCE_FLAG_READONLY);
228
229   FREE_STRLIST (nrings);
230
231   ctrl = xcalloc (1, sizeof *ctrl);
232
233   if ((rc = verify_signatures (ctrl, argc, argv)))
234     log_error("verify signatures failed: %s\n", gpg_strerror (rc) );
235
236   xfree (ctrl);
237
238   /* cleanup */
239   g10_exit (0);
240   return 8; /*NOTREACHED*/
241 }
242
243
244 void
245 g10_exit( int rc )
246 {
247   rc = rc? rc : log_get_errorcount(0)? 2 : g10_errors_seen? 1 : 0;
248   exit(rc );
249 }
250
251
252 /* Stub:
253  * We have to override the trustcheck from pkclist.c because
254  * this utility assumes that all keys in the keyring are trustworthy
255  */
256 int
257 check_signatures_trust (ctrl_t ctrl, PKT_signature *sig)
258 {
259   (void)ctrl;
260   (void)sig;
261   return 0;
262 }
263
264 void
265 read_trust_options(byte *trust_model, ulong *created, ulong *nextcheck,
266                    byte *marginals, byte *completes, byte *cert_depth,
267                    byte *min_cert_level)
268 {
269   (void)trust_model;
270   (void)created;
271   (void)nextcheck;
272   (void)marginals;
273   (void)completes;
274   (void)cert_depth;
275   (void)min_cert_level;
276 }
277
278 /* Stub:
279  * We don't have the trustdb , so we have to provide some stub functions
280  * instead
281  */
282
283 int
284 cache_disabled_value(PKT_public_key *pk)
285 {
286   (void)pk;
287   return 0;
288 }
289
290 void
291 check_trustdb_stale (ctrl_t ctrl)
292 {
293   (void)ctrl;
294 }
295
296 int
297 get_validity_info (ctrl_t ctrl, kbnode_t kb, PKT_public_key *pk,
298                    PKT_user_id *uid)
299 {
300   (void)ctrl;
301   (void)kb;
302   (void)pk;
303   (void)uid;
304   return '?';
305 }
306
307 unsigned int
308 get_validity (ctrl_t ctrl, kbnode_t kb, PKT_public_key *pk, PKT_user_id *uid,
309               PKT_signature *sig, int may_ask)
310 {
311   (void)ctrl;
312   (void)kb;
313   (void)pk;
314   (void)uid;
315   (void)sig;
316   (void)may_ask;
317   return 0;
318 }
319
320 const char *
321 trust_value_to_string (unsigned int value)
322 {
323   (void)value;
324   return "err";
325 }
326
327 const char *
328 uid_trust_string_fixed (ctrl_t ctrl, PKT_public_key *key, PKT_user_id *uid)
329 {
330   (void)ctrl;
331   (void)key;
332   (void)uid;
333   return "err";
334 }
335
336 int
337 get_ownertrust_info (PKT_public_key *pk)
338 {
339   (void)pk;
340   return '?';
341 }
342
343 unsigned int
344 get_ownertrust (PKT_public_key *pk)
345 {
346   (void)pk;
347   return TRUST_UNKNOWN;
348 }
349
350
351 /* Stubs:
352  * Because we only work with trusted keys, it does not make sense to
353  * get them from a keyserver
354  */
355
356 struct keyserver_spec *
357 keyserver_match (struct keyserver_spec *spec)
358 {
359   (void)spec;
360   return NULL;
361 }
362
363 int
364 keyserver_any_configured (ctrl_t ctrl)
365 {
366   (void)ctrl;
367   return 0;
368 }
369
370 int
371 keyserver_import_keyid (u32 *keyid, void *dummy, int quick)
372 {
373   (void)keyid;
374   (void)dummy;
375   (void)quick;
376   return -1;
377 }
378
379 int
380 keyserver_import_fprint (ctrl_t ctrl, const byte *fprint,size_t fprint_len,
381                          struct keyserver_spec *keyserver, int quick)
382 {
383   (void)ctrl;
384   (void)fprint;
385   (void)fprint_len;
386   (void)keyserver;
387   (void)quick;
388   return -1;
389 }
390
391 int
392 keyserver_import_cert (const char *name)
393 {
394   (void)name;
395   return -1;
396 }
397
398 int
399 keyserver_import_pka (const char *name,unsigned char *fpr)
400 {
401   (void)name;
402   (void)fpr;
403   return -1;
404 }
405
406 gpg_error_t
407 keyserver_import_wkd (ctrl_t ctrl, const char *name, int quick,
408                       unsigned char **fpr, size_t *fpr_len)
409 {
410   (void)ctrl;
411   (void)name;
412   (void)quick;
413   (void)fpr;
414   (void)fpr_len;
415   return GPG_ERR_BUG;
416 }
417
418 int
419 keyserver_import_name (const char *name,struct keyserver_spec *spec)
420 {
421   (void)name;
422   (void)spec;
423   return -1;
424 }
425
426 int
427 keyserver_import_ldap (const char *name)
428 {
429   (void)name;
430   return -1;
431 }
432
433
434 gpg_error_t
435 read_key_from_file (ctrl_t ctrl, const char *fname, kbnode_t *r_keyblock)
436 {
437   (void)ctrl;
438   (void)fname;
439   (void)r_keyblock;
440   return -1;
441 }
442
443
444 /* Stub:
445  * No encryption here but mainproc links to these functions.
446  */
447 gpg_error_t
448 get_session_key (ctrl_t ctrl, PKT_pubkey_enc *k, DEK *dek)
449 {
450   (void)ctrl;
451   (void)k;
452   (void)dek;
453   return GPG_ERR_GENERAL;
454 }
455
456 /* Stub: */
457 gpg_error_t
458 get_override_session_key (DEK *dek, const char *string)
459 {
460   (void)dek;
461   (void)string;
462   return GPG_ERR_GENERAL;
463 }
464
465 /* Stub: */
466 int
467 decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
468 {
469   (void)ctrl;
470   (void)procctx;
471   (void)ed;
472   (void)dek;
473   return GPG_ERR_GENERAL;
474 }
475
476
477 /* Stub:
478  * No interactive commands, so we don't need the helptexts
479  */
480 void
481 display_online_help (const char *keyword)
482 {
483   (void)keyword;
484 }
485
486 /* Stub:
487  * We don't use secret keys, but getkey.c links to this
488  */
489 int
490 check_secret_key (PKT_public_key *pk, int n)
491 {
492   (void)pk;
493   (void)n;
494   return GPG_ERR_GENERAL;
495 }
496
497 /* Stub:
498  * No secret key, so no passphrase needed
499  */
500 DEK *
501 passphrase_to_dek (int cipher_algo, STRING2KEY *s2k, int create, int nocache,
502                    const char *tmp, int *canceled)
503 {
504   (void)cipher_algo;
505   (void)s2k;
506   (void)create;
507   (void)nocache;
508   (void)tmp;
509
510   if (canceled)
511     *canceled = 0;
512   return NULL;
513 }
514
515 void
516 passphrase_clear_cache (const char *cacheid)
517 {
518   (void)cacheid;
519 }
520
521 struct keyserver_spec *
522 parse_preferred_keyserver(PKT_signature *sig)
523 {
524   (void)sig;
525   return NULL;
526 }
527
528 struct keyserver_spec *
529 parse_keyserver_uri (const char *uri, int require_scheme,
530                      const char *configname, unsigned int configlineno)
531 {
532   (void)uri;
533   (void)require_scheme;
534   (void)configname;
535   (void)configlineno;
536   return NULL;
537 }
538
539 void
540 free_keyserver_spec (struct keyserver_spec *keyserver)
541 {
542   (void)keyserver;
543 }
544
545 /* Stubs to avoid linking to photoid.c */
546 void
547 show_photos (const struct user_attribute *attrs, int count, PKT_public_key *pk)
548 {
549   (void)attrs;
550   (void)count;
551   (void)pk;
552 }
553
554 int
555 parse_image_header (const struct user_attribute *attr, byte *type, u32 *len)
556 {
557   (void)attr;
558   (void)type;
559   (void)len;
560   return 0;
561 }
562
563 char *
564 image_type_to_string (byte type, int string)
565 {
566   (void)type;
567   (void)string;
568   return NULL;
569 }
570
571 #ifdef ENABLE_CARD_SUPPORT
572 int
573 agent_scd_getattr (const char *name, struct agent_card_info_s *info)
574 {
575   (void)name;
576   (void)info;
577   return 0;
578 }
579 #endif /* ENABLE_CARD_SUPPORT */
580
581 /* We do not do any locking, so use these stubs here */
582 void
583 dotlock_disable (void)
584 {
585 }
586
587 dotlock_t
588 dotlock_create (const char *file_to_lock, unsigned int flags)
589 {
590   (void)file_to_lock;
591   (void)flags;
592   return NULL;
593 }
594
595 void
596 dotlock_destroy (dotlock_t h)
597 {
598   (void)h;
599 }
600
601 int
602 dotlock_take (dotlock_t h, long timeout)
603 {
604   (void)h;
605   (void)timeout;
606   return 0;
607 }
608
609 int
610 dotlock_release (dotlock_t h)
611 {
612   (void)h;
613   return 0;
614 }
615
616 void
617 dotlock_remove_lockfiles (void)
618 {
619 }
620
621 gpg_error_t
622 agent_probe_secret_key (ctrl_t ctrl, PKT_public_key *pk)
623 {
624   (void)ctrl;
625   (void)pk;
626   return gpg_error (GPG_ERR_NO_SECKEY);
627 }
628
629 gpg_error_t
630 agent_probe_any_secret_key (ctrl_t ctrl, kbnode_t keyblock)
631 {
632   (void)ctrl;
633   (void)keyblock;
634   return gpg_error (GPG_ERR_NO_SECKEY);
635 }
636
637 gpg_error_t
638 agent_get_keyinfo (ctrl_t ctrl, const char *hexkeygrip,
639                    char **r_serialno, int *r_cleartext)
640 {
641   (void)ctrl;
642   (void)hexkeygrip;
643   (void)r_cleartext;
644   *r_serialno = NULL;
645   return gpg_error (GPG_ERR_NO_SECKEY);
646 }
647
648 gpg_error_t
649 gpg_dirmngr_get_pka (ctrl_t ctrl, const char *userid,
650                      unsigned char **r_fpr, size_t *r_fprlen,
651                      char **r_url)
652 {
653   (void)ctrl;
654   (void)userid;
655   if (r_fpr)
656     *r_fpr = NULL;
657   if (r_fprlen)
658     *r_fprlen = 0;
659   if (r_url)
660     *r_url = NULL;
661   return gpg_error (GPG_ERR_NOT_FOUND);
662 }
663
664 gpg_error_t
665 export_pubkey_buffer (ctrl_t ctrl, const char *keyspec, unsigned int options,
666                       export_stats_t stats,
667                       kbnode_t *r_keyblock, void **r_data, size_t *r_datalen)
668 {
669   (void)ctrl;
670   (void)keyspec;
671   (void)options;
672   (void)stats;
673
674   *r_keyblock = NULL;
675   *r_data = NULL;
676   *r_datalen = 0;
677   return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
678 }
679
680 gpg_error_t
681 tofu_write_tfs_record (ctrl_t ctrl, estream_t fp,
682                        PKT_public_key *pk, const char *user_id)
683 {
684   (void)ctrl;
685   (void)fp;
686   (void)pk;
687   (void)user_id;
688   return gpg_error (GPG_ERR_GENERAL);
689 }
690
691 gpg_error_t
692 tofu_get_policy (ctrl_t ctrl, PKT_public_key *pk, PKT_user_id *user_id,
693                  enum tofu_policy *policy)
694 {
695   (void)ctrl;
696   (void)pk;
697   (void)user_id;
698   (void)policy;
699   return gpg_error (GPG_ERR_GENERAL);
700 }
701
702 const char *
703 tofu_policy_str (enum tofu_policy policy)
704 {
705   (void)policy;
706
707   return "unknown";
708 }
709
710 void
711 tofu_begin_batch_update (ctrl_t ctrl)
712 {
713   (void)ctrl;
714 }
715
716 void
717 tofu_end_batch_update (ctrl_t ctrl)
718 {
719   (void)ctrl;
720 }
721
722 gpg_error_t
723 tofu_notice_key_changed (ctrl_t ctrl, kbnode_t kb)
724 {
725   (void) ctrl;
726   (void) kb;
727
728   return 0;
729 }