~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to g10/verify.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* verify.c - verify signed data
2
 
 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
 
1
/* verify.c - Verify signed data
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006,
 
3
 *               2007 Free Software Foundation, Inc.
3
4
 *
4
5
 * This file is part of GnuPG.
5
6
 *
6
7
 * GnuPG is free software; you can redistribute it and/or modify
7
8
 * 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
 * the Free Software Foundation; either version 3 of the License, or
9
10
 * (at your option) any later version.
10
11
 *
11
12
 * GnuPG is distributed in the hope that it will be useful,
14
15
 * GNU General Public License for more details.
15
16
 *
16
17
 * 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
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
19
 */
20
20
 
21
21
#include <config.h>
24
24
#include <string.h>
25
25
#include <errno.h>
26
26
#include <assert.h>
27
 
#include <unistd.h> /* for isatty() */
28
27
 
 
28
#include "gpg.h"
29
29
#include "options.h"
30
30
#include "packet.h"
31
 
#include "errors.h"
 
31
#include "status.h"
32
32
#include "iobuf.h"
33
33
#include "keydb.h"
34
 
#include "memory.h"
35
34
#include "util.h"
36
35
#include "main.h"
37
36
#include "status.h"
54
53
int
55
54
verify_signatures( int nfiles, char **files )
56
55
{
57
 
    iobuf_t fp;
58
 
    armor_filter_context_t afx;
59
 
    progress_filter_context_t pfx;
 
56
    IOBUF fp;
 
57
    armor_filter_context_t *afx = NULL;
 
58
    progress_filter_context_t *pfx = new_progress_context ();
60
59
    const char *sigfile;
61
60
    int i, rc;
62
 
    STRLIST sl;
 
61
    strlist_t sl;
63
62
 
64
 
    memset( &afx, 0, sizeof afx);
65
 
    /* decide whether we should handle a detached or a normal signature,
 
63
    /* Decide whether we should handle a detached or a normal signature,
66
64
     * which is needed so that the code later can hash the correct data and
67
65
     * not have a normal signature act as detached signature and ignoring the
68
66
     * indended signed material from the 2nd file or stdin.
71
69
     * 3. gpg file <file2  - detached
72
70
     * 4. gpg file file2   - detached
73
71
     * 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
 
72
     * we can do it is by reading one byte from stdin and then unget
75
73
     * it; the problem here is that we may be reading from the
76
74
     * terminal (which could be detected using isatty() but won't work
77
75
     * when under contol of a pty using program (e.g. expect)) and
86
84
     * that all quite easily in mainproc.c 
87
85
     */
88
86
     
89
 
 
90
87
    sigfile = nfiles? *files : NULL;
91
88
 
92
89
    /* open the signature file */
93
90
    fp = iobuf_open(sigfile);
 
91
    if (fp && is_secured_file (iobuf_get_fd (fp)))
 
92
      {
 
93
        iobuf_close (fp);
 
94
        fp = NULL;
 
95
        errno = EPERM;
 
96
      }
94
97
    if( !fp ) {
95
 
        rc = gpg_error_from_errno (errno);
 
98
        rc = gpg_error_from_syserror ();
96
99
        log_error(_("can't open `%s': %s\n"),
97
100
                  print_fname_stdin(sigfile), strerror (errno));
98
 
        return rc;
 
101
        goto leave;
99
102
    }
100
 
    handle_progress (&pfx, fp, sigfile);
 
103
    handle_progress (pfx, fp, sigfile);
101
104
 
102
 
    if( !opt.no_armor && use_armor_filter( fp ) )
103
 
        iobuf_push_filter( fp, armor_filter, &afx );
 
105
    if ( !opt.no_armor && use_armor_filter( fp ) )
 
106
      {
 
107
        afx = new_armor_context ();
 
108
        push_armor_filter (afx, fp);
 
109
      }
104
110
 
105
111
    sl = NULL;
106
 
    for(i=1 ; i < nfiles; i++ )
 
112
    for(i=nfiles-1 ; i > 0 ; i-- )
107
113
        add_to_strlist( &sl, files[i] );
108
114
    rc = proc_signature_packets( NULL, fp, sl, sigfile );
109
115
    free_strlist(sl);
110
116
    iobuf_close(fp);
111
 
    if( afx.no_openpgp_data && rc == -1 ) {
 
117
    if( (afx && afx->no_openpgp_data && rc == -1) || rc == G10ERR_NO_DATA ) {
112
118
        log_error(_("the signature could not be verified.\n"
113
119
                   "Please remember that the signature file (.sig or .asc)\n"
114
120
                   "should be the first file given on the command line.\n") );
115
121
        rc = 0;
116
122
    }
117
123
 
 
124
 leave:
 
125
    release_armor_context (afx);
 
126
    release_progress_context (pfx);
118
127
    return rc;
119
128
}
120
129
 
121
130
 
 
131
 
122
132
void
123
133
print_file_status( int status, const char *name, int what )
124
134
{
125
 
    char *p = xmalloc (strlen(name)+10);
 
135
    char *p = xmalloc(strlen(name)+10);
126
136
    sprintf(p, "%d %s", what, name );
127
137
    write_status_text( status, p );
128
 
    xfree (p);
 
138
    xfree(p);
129
139
}
130
140
 
131
141
 
132
142
static int
133
143
verify_one_file( const char *name )
134
144
{
135
 
    iobuf_t fp;
136
 
    armor_filter_context_t afx;
137
 
    progress_filter_context_t pfx;
 
145
    IOBUF fp;
 
146
    armor_filter_context_t *afx = NULL;
 
147
    progress_filter_context_t *pfx = new_progress_context ();
138
148
    int rc;
139
149
 
140
150
    print_file_status( STATUS_FILE_START, name, 1 );
141
151
    fp = iobuf_open(name);
 
152
    if (fp)
 
153
      iobuf_ioctl (fp,3,1,NULL); /* disable fd caching */
 
154
    if (fp && is_secured_file (iobuf_get_fd (fp)))
 
155
      {
 
156
        iobuf_close (fp);
 
157
        fp = NULL;
 
158
        errno = EPERM;
 
159
      }
142
160
    if( !fp ) {
143
 
        rc = gpg_error_from_errno (errno);
 
161
        rc = gpg_error_from_syserror ();
144
162
        log_error(_("can't open `%s': %s\n"),
145
163
                  print_fname_stdin(name), strerror (errno));
146
164
        print_file_status( STATUS_FILE_ERROR, name, 1 );
147
 
        return rc;
 
165
        goto leave;
148
166
    }
149
 
    handle_progress (&pfx, fp, name);
 
167
    handle_progress (pfx, fp, name);
150
168
 
151
169
    if( !opt.no_armor ) {
152
170
        if( use_armor_filter( fp ) ) {
153
 
            memset( &afx, 0, sizeof afx);
154
 
            iobuf_push_filter( fp, armor_filter, &afx );
 
171
            afx = new_armor_context ();
 
172
            push_armor_filter (afx, fp);
155
173
        }
156
174
    }
157
175
 
158
176
    rc = proc_signature_packets( NULL, fp, NULL, name );
159
177
    iobuf_close(fp);
160
178
    write_status( STATUS_FILE_DONE );
 
179
 
 
180
    reset_literals_seen();
 
181
 
 
182
 leave:
 
183
    release_armor_context (afx);
 
184
    release_progress_context (pfx);
161
185
    return rc;
162
186
}
163
187
 
179
203
            lno++;
180
204
            if( !*line || line[strlen(line)-1] != '\n' ) {
181
205
                log_error(_("input line %u too long or missing LF\n"), lno );
182
 
                return GPG_ERR_GENERAL;
 
206
                return G10ERR_GENERAL;
183
207
            }
184
208
            /* This code does not work on MSDOS but how cares there are
185
209
             * also no script languages available.  We don't strip any
195
219
    }
196
220
    return 0;
197
221
}
 
222
 
 
223
 
 
224
 
 
225
 
 
226
/* Perform a verify operation.  To verify detached signatures, DATA_FD
 
227
   shall be the descriptor of the signed data; for regular signatures
 
228
   it needs to be -1.  If OUT_FP is not NULL and DATA_FD is not -1 the
 
229
   the signed material gets written that stream. 
 
230
 
 
231
   FIXME: OUTFP is not yet implemented.
 
232
*/
 
233
int
 
234
gpg_verify (ctrl_t ctrl, int sig_fd, int data_fd, FILE *out_fp)
 
235
{
 
236
  int rc;
 
237
  iobuf_t fp;
 
238
  armor_filter_context_t *afx = NULL;
 
239
  progress_filter_context_t *pfx = new_progress_context ();
 
240
 
 
241
  fp = iobuf_fdopen (sig_fd, "rb");
 
242
  if (fp && is_secured_file (sig_fd))
 
243
    {
 
244
      fp = NULL;
 
245
      errno = EPERM;
 
246
    }
 
247
  if ( !fp )
 
248
    {
 
249
      rc = gpg_error_from_syserror ();
 
250
      log_error (_("can't open fd %d: %s\n"), sig_fd, strerror (errno));
 
251
      goto leave;
 
252
    }
 
253
 
 
254
  handle_progress (pfx, fp, NULL);
 
255
 
 
256
  if ( !opt.no_armor && use_armor_filter (fp) )
 
257
    {
 
258
      afx = new_armor_context ();
 
259
      push_armor_filter (afx, fp);
 
260
    }
 
261
 
 
262
  rc = proc_signature_packets_by_fd ( NULL, fp, data_fd );
 
263
 
 
264
  if ( afx && afx->no_openpgp_data
 
265
       && (rc == -1 || gpg_err_code (rc) == GPG_ERR_EOF) )
 
266
    rc = gpg_error (GPG_ERR_NO_DATA);
 
267
 
 
268
 leave:  
 
269
  if (fp)
 
270
    iobuf_close (fp);
 
271
  release_progress_context (pfx);
 
272
  release_armor_context (afx);
 
273
  return rc;
 
274
}
 
275