2004-02-26 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / verify.c
blobcfa373637e20b9c0bd669c3c417e5e6c74f82998
1 /* verify.c - verify signed data
2 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include <unistd.h> /* for isatty() */
29 #include "options.h"
30 #include "packet.h"
31 #include "errors.h"
32 #include "iobuf.h"
33 #include "keydb.h"
34 #include "memory.h"
35 #include "util.h"
36 #include "main.h"
37 #include "status.h"
38 #include "filter.h"
39 #include "ttyio.h"
40 #include "i18n.h"
44 /****************
45 * Assume that the input is a signature and verify it without
46 * generating any output. With no arguments, the signature packet
47 * is read from stdin (it may be a detached signature when not
48 * used in batch mode). If only a sigfile is given, it may be a complete
49 * signature or a detached signature in which case the signed stuff
50 * is expected from stdin. With more than 1 argument, the first should
51 * be a detached signature and the remaining files are the signed stuff.
54 int
55 verify_signatures( int nfiles, char **files )
57 iobuf_t fp;
58 armor_filter_context_t afx;
59 progress_filter_context_t pfx;
60 const char *sigfile;
61 int i, rc;
62 STRLIST sl;
64 memset( &afx, 0, sizeof afx);
65 /* decide whether we should handle a detached or a normal signature,
66 * which is needed so that the code later can hash the correct data and
67 * not have a normal signature act as detached signature and ignoring the
68 * indended signed material from the 2nd file or stdin.
69 * 1. gpg <file - normal
70 * 2. gpg file - normal (or detached)
71 * 3. gpg file <file2 - detached
72 * 4. gpg file file2 - detached
73 * The question is how decide between case 2 and 3? The only way
74 * we can do it is by reading one byte from stdin and the unget
75 * it; the problem here is that we may be reading from the
76 * terminal (which could be detected using isatty() but won't work
77 * when under contol of a pty using program (e.g. expect)) and
78 * might get us in trouble when stdin is used for another purpose
79 * (--passphrase-fd 0). So we have to break with the behaviour
80 * prior to gpg 1.0.4 by assuming that case 3 is a normal
81 * signature (where file2 is ignored and require for a detached
82 * signature to indicate signed material comes from stdin by using
83 * case 4 with a file2 of "-".
85 * Actually we don't have to change anything here but can handle
86 * that all quite easily in mainproc.c
90 sigfile = nfiles? *files : NULL;
92 /* open the signature file */
93 fp = iobuf_open(sigfile);
94 if( !fp ) {
95 rc = gpg_error_from_errno (errno);
96 log_error(_("can't open `%s': %s\n"),
97 print_fname_stdin(sigfile), strerror (errno));
98 return rc;
100 handle_progress (&pfx, fp, sigfile);
102 if( !opt.no_armor && use_armor_filter( fp ) )
103 iobuf_push_filter( fp, armor_filter, &afx );
105 sl = NULL;
106 for(i=1 ; i < nfiles; i++ )
107 add_to_strlist( &sl, files[i] );
108 rc = proc_signature_packets( NULL, fp, sl, sigfile );
109 free_strlist(sl);
110 iobuf_close(fp);
111 if( afx.no_openpgp_data && rc == -1 ) {
112 log_error(_("the signature could not be verified.\n"
113 "Please remember that the signature file (.sig or .asc)\n"
114 "should be the first file given on the command line.\n") );
115 rc = 0;
118 return rc;
122 void
123 print_file_status( int status, const char *name, int what )
125 char *p = xmalloc (strlen(name)+10);
126 sprintf(p, "%d %s", what, name );
127 write_status_text( status, p );
128 xfree (p);
132 static int
133 verify_one_file( const char *name )
135 iobuf_t fp;
136 armor_filter_context_t afx;
137 progress_filter_context_t pfx;
138 int rc;
140 print_file_status( STATUS_FILE_START, name, 1 );
141 fp = iobuf_open(name);
142 if( !fp ) {
143 rc = gpg_error_from_errno (errno);
144 log_error(_("can't open `%s': %s\n"),
145 print_fname_stdin(name), strerror (errno));
146 print_file_status( STATUS_FILE_ERROR, name, 1 );
147 return rc;
149 handle_progress (&pfx, fp, name);
151 if( !opt.no_armor ) {
152 if( use_armor_filter( fp ) ) {
153 memset( &afx, 0, sizeof afx);
154 iobuf_push_filter( fp, armor_filter, &afx );
158 rc = proc_signature_packets( NULL, fp, NULL, name );
159 iobuf_close(fp);
160 write_status( STATUS_FILE_DONE );
161 return rc;
164 /****************
165 * Verify each file given in the files array or read the names of the
166 * files from stdin.
167 * Note: This function can not handle detached signatures.
170 verify_files( int nfiles, char **files )
172 int i;
174 if( !nfiles ) { /* read the filenames from stdin */
175 char line[2048];
176 unsigned int lno = 0;
178 while( fgets(line, DIM(line), stdin) ) {
179 lno++;
180 if( !*line || line[strlen(line)-1] != '\n' ) {
181 log_error(_("input line %u too long or missing LF\n"), lno );
182 return GPG_ERR_GENERAL;
184 /* This code does not work on MSDOS but how cares there are
185 * also no script languages available. We don't strip any
186 * spaces, so that we can process nearly all filenames */
187 line[strlen(line)-1] = 0;
188 verify_one_file( line );
192 else { /* take filenames from the array */
193 for(i=0; i < nfiles; i++ )
194 verify_one_file( files[i] );
196 return 0;