Re-indentation
[gnupg.git] / g10 / decrypt.c
blobc76c295c88ffc3aff168b4f38fb21b0b3d592b53
1 /* decrypt.c - decrypt and verify data
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 * 2007, 2009 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 3 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, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <assert.h>
28 #include "gpg.h"
29 #include "options.h"
30 #include "packet.h"
31 #include "status.h"
32 #include "iobuf.h"
33 #include "keydb.h"
34 #include "util.h"
35 #include "main.h"
36 #include "status.h"
37 #include "i18n.h"
39 /* Assume that the input is an encrypted message and decrypt
40 * (and if signed, verify the signature on) it.
41 * This command differs from the default operation, as it never
42 * writes to the filename which is included in the file and it
43 * rejects files which don't begin with an encrypted message.
45 int
46 decrypt_message (const char *filename)
48 IOBUF fp;
49 armor_filter_context_t *afx = NULL;
50 progress_filter_context_t *pfx;
51 int rc;
52 int no_out = 0;
54 pfx = new_progress_context ();
56 /* Open the message file. */
57 fp = iobuf_open (filename);
58 if (fp && is_secured_file (iobuf_get_fd (fp)))
60 iobuf_close (fp);
61 fp = NULL;
62 errno = EPERM;
64 if ( !fp )
66 rc = gpg_error_from_syserror ();
67 log_error (_("can't open `%s': %s\n"), print_fname_stdin(filename),
68 gpg_strerror (rc));
69 release_progress_context (pfx);
70 return rc;
73 handle_progress (pfx, fp, filename);
75 if ( !opt.no_armor )
77 if ( use_armor_filter( fp ) )
79 afx = new_armor_context ();
80 push_armor_filter ( afx, fp );
84 if (!opt.outfile)
86 no_out = 1;
87 opt.outfile = "-";
89 rc = proc_encryption_packets ( NULL, fp );
90 if (no_out)
91 opt.outfile = NULL;
93 iobuf_close (fp);
94 release_armor_context (afx);
95 release_progress_context (pfx);
96 return rc;
100 void
101 decrypt_messages (int nfiles, char *files[])
103 IOBUF fp;
104 armor_filter_context_t *afx = NULL;
105 progress_filter_context_t *pfx;
106 char *p, *output = NULL;
107 int rc=0,use_stdin=0;
108 unsigned int lno=0;
110 if (opt.outfile)
112 log_error(_("--output doesn't work for this command\n"));
113 return;
116 pfx = new_progress_context ();
118 if(!nfiles)
119 use_stdin=1;
121 for(;;)
123 char line[2048];
124 char *filename=NULL;
126 if(use_stdin)
128 if(fgets(line, DIM(line), stdin))
130 lno++;
131 if (!*line || line[strlen(line)-1] != '\n')
132 log_error("input line %u too long or missing LF\n", lno);
133 else
135 line[strlen(line)-1] = '\0';
136 filename=line;
140 else
142 if(nfiles)
144 filename=*files;
145 nfiles--;
146 files++;
150 if(filename==NULL)
151 break;
153 print_file_status(STATUS_FILE_START, filename, 3);
154 output = make_outfile_name(filename);
155 if (!output)
156 goto next_file;
157 fp = iobuf_open(filename);
158 if (fp)
159 iobuf_ioctl (fp,3,1,NULL); /* disable fd caching */
160 if (fp && is_secured_file (iobuf_get_fd (fp)))
162 iobuf_close (fp);
163 fp = NULL;
164 errno = EPERM;
166 if (!fp)
168 log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
169 goto next_file;
172 handle_progress (pfx, fp, filename);
174 if (!opt.no_armor)
176 if (use_armor_filter(fp))
178 afx = new_armor_context ();
179 push_armor_filter ( afx, fp );
182 rc = proc_packets(NULL, fp);
183 iobuf_close(fp);
184 if (rc)
185 log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
186 g10_errstr(rc));
187 p = get_last_passphrase();
188 set_next_passphrase(p);
189 xfree (p);
191 next_file:
192 /* Note that we emit file_done even after an error. */
193 write_status( STATUS_FILE_DONE );
194 xfree(output);
195 reset_literals_seen();
198 set_next_passphrase(NULL);
199 release_armor_context (afx);
200 release_progress_context (pfx);