Line data Source code
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.
4 : *
5 : * This file is part of GnuPG.
6 : *
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.
11 : *
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.
16 : *
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/>.
19 : */
20 :
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 :
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"
38 :
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.
44 : */
45 : int
46 0 : decrypt_message (ctrl_t ctrl, const char *filename)
47 : {
48 : IOBUF fp;
49 0 : armor_filter_context_t *afx = NULL;
50 : progress_filter_context_t *pfx;
51 : int rc;
52 0 : int no_out = 0;
53 :
54 0 : pfx = new_progress_context ();
55 :
56 : /* Open the message file. */
57 0 : fp = iobuf_open (filename);
58 0 : if (fp && is_secured_file (iobuf_get_fd (fp)))
59 : {
60 0 : iobuf_close (fp);
61 0 : fp = NULL;
62 0 : gpg_err_set_errno (EPERM);
63 : }
64 0 : if ( !fp )
65 : {
66 0 : rc = gpg_error_from_syserror ();
67 0 : log_error (_("can't open '%s': %s\n"), print_fname_stdin(filename),
68 : gpg_strerror (rc));
69 0 : release_progress_context (pfx);
70 0 : return rc;
71 : }
72 :
73 0 : handle_progress (pfx, fp, filename);
74 :
75 0 : if ( !opt.no_armor )
76 : {
77 0 : if ( use_armor_filter( fp ) )
78 : {
79 0 : afx = new_armor_context ();
80 0 : push_armor_filter ( afx, fp );
81 : }
82 : }
83 :
84 0 : if (!opt.outfile)
85 : {
86 0 : no_out = 1;
87 0 : opt.outfile = "-";
88 : }
89 0 : rc = proc_encryption_packets (ctrl, NULL, fp );
90 0 : if (no_out)
91 0 : opt.outfile = NULL;
92 :
93 0 : iobuf_close (fp);
94 0 : release_armor_context (afx);
95 0 : release_progress_context (pfx);
96 0 : return rc;
97 : }
98 :
99 :
100 : /* Same as decrypt_message but takes a file descriptor for input and
101 : output. */
102 : gpg_error_t
103 0 : decrypt_message_fd (ctrl_t ctrl, int input_fd, int output_fd)
104 : {
105 : #ifdef HAVE_W32_SYSTEM
106 : /* No server mode yet. */
107 : (void)ctrl;
108 : (void)input_fd;
109 : (void)output_fd;
110 : return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
111 : #else
112 : gpg_error_t err;
113 : IOBUF fp;
114 0 : armor_filter_context_t *afx = NULL;
115 : progress_filter_context_t *pfx;
116 :
117 0 : if (opt.outfp)
118 0 : return gpg_error (GPG_ERR_BUG);
119 :
120 0 : pfx = new_progress_context ();
121 :
122 : /* Open the message file. */
123 0 : fp = iobuf_fdopen_nc (FD2INT(input_fd), "rb");
124 0 : if (fp && is_secured_file (iobuf_get_fd (fp)))
125 : {
126 0 : iobuf_close (fp);
127 0 : fp = NULL;
128 0 : gpg_err_set_errno (EPERM);
129 : }
130 0 : if (!fp)
131 : {
132 : char xname[64];
133 :
134 0 : err = gpg_error_from_syserror ();
135 0 : snprintf (xname, sizeof xname, "[fd %d]", input_fd);
136 0 : log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
137 0 : release_progress_context (pfx);
138 0 : return err;
139 : }
140 :
141 : #ifdef HAVE_W32CE_SYSTEM
142 : #warning Need to fix this if we want to use g13
143 : opt.outfp = NULL;
144 : #else
145 0 : opt.outfp = es_fdopen_nc (output_fd, "wb");
146 : #endif
147 0 : if (!opt.outfp)
148 : {
149 : char xname[64];
150 :
151 0 : err = gpg_error_from_syserror ();
152 0 : snprintf (xname, sizeof xname, "[fd %d]", output_fd);
153 0 : log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
154 0 : iobuf_close (fp);
155 0 : release_progress_context (pfx);
156 0 : return err;
157 : }
158 :
159 0 : if (!opt.no_armor)
160 : {
161 0 : if (use_armor_filter (fp))
162 : {
163 0 : afx = new_armor_context ();
164 0 : push_armor_filter ( afx, fp );
165 : }
166 : }
167 :
168 0 : err = proc_encryption_packets (ctrl, NULL, fp );
169 :
170 0 : iobuf_close (fp);
171 0 : es_fclose (opt.outfp);
172 0 : opt.outfp = NULL;
173 0 : release_armor_context (afx);
174 0 : release_progress_context (pfx);
175 0 : return err;
176 : #endif
177 : }
178 :
179 :
180 : void
181 0 : decrypt_messages (ctrl_t ctrl, int nfiles, char *files[])
182 : {
183 : IOBUF fp;
184 0 : armor_filter_context_t *afx = NULL;
185 : progress_filter_context_t *pfx;
186 0 : char *p, *output = NULL;
187 0 : int rc=0,use_stdin=0;
188 0 : unsigned int lno=0;
189 :
190 0 : if (opt.outfile)
191 : {
192 0 : log_error(_("--output doesn't work for this command\n"));
193 0 : return;
194 : }
195 :
196 0 : pfx = new_progress_context ();
197 :
198 0 : if(!nfiles)
199 0 : use_stdin=1;
200 :
201 : for(;;)
202 : {
203 : char line[2048];
204 0 : char *filename=NULL;
205 :
206 0 : if(use_stdin)
207 : {
208 0 : if(fgets(line, DIM(line), stdin))
209 : {
210 0 : lno++;
211 0 : if (!*line || line[strlen(line)-1] != '\n')
212 0 : log_error("input line %u too long or missing LF\n", lno);
213 : else
214 : {
215 0 : line[strlen(line)-1] = '\0';
216 0 : filename=line;
217 : }
218 : }
219 : }
220 : else
221 : {
222 0 : if(nfiles)
223 : {
224 0 : filename=*files;
225 0 : nfiles--;
226 0 : files++;
227 : }
228 : }
229 :
230 0 : if(filename==NULL)
231 0 : break;
232 :
233 0 : print_file_status(STATUS_FILE_START, filename, 3);
234 0 : output = make_outfile_name(filename);
235 0 : if (!output)
236 0 : goto next_file;
237 0 : fp = iobuf_open(filename);
238 0 : if (fp)
239 0 : iobuf_ioctl (fp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
240 0 : if (fp && is_secured_file (iobuf_get_fd (fp)))
241 : {
242 0 : iobuf_close (fp);
243 0 : fp = NULL;
244 0 : gpg_err_set_errno (EPERM);
245 : }
246 0 : if (!fp)
247 : {
248 0 : log_error(_("can't open '%s'\n"), print_fname_stdin(filename));
249 0 : goto next_file;
250 : }
251 :
252 0 : handle_progress (pfx, fp, filename);
253 :
254 0 : if (!opt.no_armor)
255 : {
256 0 : if (use_armor_filter(fp))
257 : {
258 0 : afx = new_armor_context ();
259 0 : push_armor_filter ( afx, fp );
260 : }
261 : }
262 0 : rc = proc_packets (ctrl,NULL, fp);
263 0 : iobuf_close(fp);
264 0 : if (rc)
265 0 : log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
266 : gpg_strerror (rc));
267 0 : p = get_last_passphrase();
268 0 : set_next_passphrase(p);
269 0 : xfree (p);
270 :
271 : next_file:
272 : /* Note that we emit file_done even after an error. */
273 0 : write_status( STATUS_FILE_DONE );
274 0 : xfree(output);
275 0 : reset_literals_seen();
276 0 : }
277 :
278 0 : set_next_passphrase(NULL);
279 0 : release_armor_context (afx);
280 0 : release_progress_context (pfx);
281 : }
|