* exec.c (make_tempdir) [_WIN32]: Modified to properly handle
[gnupg.git] / g10 / decrypt.c
blob3482e3915741f2cac140a18790032d5600464d16
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 "options.h"
31 #include "packet.h"
32 #include "errors.h"
33 #include "iobuf.h"
34 #include "keydb.h"
35 #include "memory.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 log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
70 return G10ERR_OPEN_FILE;
73 handle_progress (&pfx, fp, filename);
75 if( !opt.no_armor ) {
76 if( use_armor_filter( fp ) ) {
77 memset( &afx, 0, sizeof afx);
78 iobuf_push_filter( fp, armor_filter, &afx );
82 if( !opt.outfile ) {
83 no_out = 1;
84 opt.outfile = "-";
86 rc = proc_encryption_packets( NULL, fp );
87 if( no_out )
88 opt.outfile = NULL;
89 iobuf_close(fp);
90 return rc;
93 void
94 decrypt_messages(int nfiles, char *files[])
96 IOBUF fp;
97 armor_filter_context_t afx;
98 progress_filter_context_t pfx;
99 char *p, *output = NULL;
100 int rc=0,use_stdin=0;
101 unsigned int lno=0;
103 if (opt.outfile)
105 log_error(_("--output doesn't work for this command\n"));
106 return;
110 if(!nfiles)
111 use_stdin=1;
113 for(;;)
115 char line[2048];
116 char *filename=NULL;
118 if(use_stdin)
120 if(fgets(line, DIM(line), stdin))
122 lno++;
123 if (!*line || line[strlen(line)-1] != '\n')
124 log_error("input line %u too long or missing LF\n", lno);
125 else
127 line[strlen(line)-1] = '\0';
128 filename=line;
132 else
134 if(nfiles)
136 filename=*files;
137 nfiles--;
138 files++;
142 if(filename==NULL)
143 break;
145 print_file_status(STATUS_FILE_START, filename, 3);
146 output = make_outfile_name(filename);
147 if (!output)
148 goto next_file;
149 fp = iobuf_open(filename);
150 if (fp)
151 iobuf_ioctl (fp,3,1,NULL); /* disable fd caching */
152 if (fp && is_secured_file (iobuf_get_fd (fp)))
154 iobuf_close (fp);
155 fp = NULL;
156 errno = EPERM;
158 if (!fp)
160 log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
161 goto next_file;
164 handle_progress (&pfx, fp, filename);
166 if (!opt.no_armor)
168 if (use_armor_filter(fp))
170 memset(&afx, 0, sizeof afx);
171 iobuf_push_filter(fp, armor_filter, &afx);
174 rc = proc_packets(NULL, fp);
175 iobuf_close(fp);
176 if (rc)
177 log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
178 g10_errstr(rc));
179 p = get_last_passphrase();
180 set_next_passphrase(p);
181 xfree (p);
183 next_file:
184 /* Note that we emit file_done even after an error. */
185 write_status( STATUS_FILE_DONE );
186 xfree(output);
189 set_next_passphrase(NULL);