2006-06-09 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / decrypt.c
blob9a37283c198045e2efd220bb83f80c17ece13abe
1 /* decrypt.c - verify signed data
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 * 2004 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
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 2 of the License, or
10 * (at your option) any later version.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <assert.h>
30 #include "gpg.h"
31 #include "options.h"
32 #include "packet.h"
33 #include "errors.h"
34 #include "iobuf.h"
35 #include "keydb.h"
36 #include "util.h"
37 #include "main.h"
38 #include "status.h"
39 #include "i18n.h"
43 /****************
44 * Assume that the input is an encrypted message and decrypt
45 * (and if signed, verify the signature on) it.
46 * This command differs from the default operation, as it never
47 * writes to the filename which is included in the file and it
48 * rejects files which don't begin with an encrypted message.
51 int
52 decrypt_message( const char *filename )
54 IOBUF fp;
55 armor_filter_context_t afx;
56 progress_filter_context_t pfx;
57 int rc;
58 int no_out=0;
60 /* Open the message file. */
61 fp = iobuf_open(filename);
62 if (fp && is_secured_file (iobuf_get_fd (fp)))
64 iobuf_close (fp);
65 fp = NULL;
66 errno = EPERM;
68 if( !fp ) {
69 rc = gpg_error_from_errno (errno);
70 log_error (_("can't open `%s': %s\n"), print_fname_stdin(filename),
71 gpg_strerror (rc));
72 return rc;
75 handle_progress (&pfx, fp, filename);
77 if( !opt.no_armor ) {
78 if( use_armor_filter( fp ) ) {
79 memset( &afx, 0, sizeof afx);
80 iobuf_push_filter( fp, armor_filter, &afx );
84 if( !opt.outfile ) {
85 no_out = 1;
86 opt.outfile = "-";
88 rc = proc_encryption_packets( NULL, fp );
89 if( no_out )
90 opt.outfile = NULL;
91 iobuf_close(fp);
92 return rc;
95 void
96 decrypt_messages(int nfiles, char *files[])
98 IOBUF fp;
99 armor_filter_context_t afx;
100 progress_filter_context_t pfx;
101 char *p, *output = NULL;
102 int rc=0,use_stdin=0;
103 unsigned int lno=0;
105 if (opt.outfile)
107 log_error(_("--output doesn't work for this command\n"));
108 return;
112 if(!nfiles)
113 use_stdin=1;
115 for(;;)
117 char line[2048];
118 char *filename=NULL;
120 if(use_stdin)
122 if(fgets(line, DIM(line), stdin))
124 lno++;
125 if (!*line || line[strlen(line)-1] != '\n')
126 log_error("input line %u too long or missing LF\n", lno);
127 else
129 line[strlen(line)-1] = '\0';
130 filename=line;
134 else
136 if(nfiles)
138 filename=*files;
139 nfiles--;
140 files++;
144 if(filename==NULL)
145 break;
147 print_file_status(STATUS_FILE_START, filename, 3);
148 output = make_outfile_name(filename);
149 if (!output)
150 goto next_file;
151 fp = iobuf_open(filename);
152 if (fp)
153 iobuf_ioctl (fp,3,1,NULL); /* disable fd caching */
154 if (fp && is_secured_file (iobuf_get_fd (fp)))
156 iobuf_close (fp);
157 fp = NULL;
158 errno = EPERM;
160 if (!fp)
162 log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
163 goto next_file;
166 handle_progress (&pfx, fp, filename);
168 if (!opt.no_armor)
170 if (use_armor_filter(fp))
172 memset(&afx, 0, sizeof afx);
173 iobuf_push_filter(fp, armor_filter, &afx);
176 rc = proc_packets(NULL, fp);
177 iobuf_close(fp);
178 if (rc)
179 log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
180 g10_errstr(rc));
181 p = get_last_passphrase();
182 set_next_passphrase(p);
183 xfree (p);
185 next_file:
186 /* Note that we emit file_done even after an error. */
187 write_status( STATUS_FILE_DONE );
188 xfree(output);
191 set_next_passphrase(NULL);