/src/gnupg/g10/plaintext.c
Line | Count | Source |
1 | | /* plaintext.c - process plaintext packets |
2 | | * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
3 | | * 2006, 2009, 2010 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 <https://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 <sys/types.h> |
27 | | #ifdef HAVE_DOSISH_SYSTEM |
28 | | # include <fcntl.h> /* for setmode() */ |
29 | | #endif |
30 | | |
31 | | #include "gpg.h" |
32 | | #include "../common/util.h" |
33 | | #include "options.h" |
34 | | #include "packet.h" |
35 | | #include "../common/ttyio.h" |
36 | | #include "filter.h" |
37 | | #include "main.h" |
38 | | #include "../common/status.h" |
39 | | #include "../common/i18n.h" |
40 | | |
41 | | |
42 | | /* Get the output filename. On success, the actual filename that is |
43 | | used is set in *FNAMEP and a filepointer is returned in *FP. |
44 | | |
45 | | EMBEDDED_NAME AND EMBEDDED_NAMELEN are normally stored in a |
46 | | plaintext packet. EMBEDDED_NAMELEN should not include any NUL |
47 | | terminator (EMBEDDED_NAME does not need to be NUL terminated). |
48 | | |
49 | | DATA is the iobuf containing the input data. We just use it to get |
50 | | the input file's filename. |
51 | | |
52 | | On success, the caller is responsible for calling xfree on *FNAMEP |
53 | | and calling es_close on *FPP. */ |
54 | | gpg_error_t |
55 | | get_output_file (const byte *embedded_name, int embedded_namelen, |
56 | | iobuf_t data, char **fnamep, estream_t *fpp) |
57 | 0 | { |
58 | 0 | gpg_error_t err = 0; |
59 | 0 | char *fname = NULL; |
60 | 0 | estream_t fp = NULL; |
61 | 0 | int nooutput = 0; |
62 | | |
63 | | /* Create the filename as C string. */ |
64 | 0 | if (opt.outfp) |
65 | 0 | { |
66 | 0 | fname = xtrystrdup ("[FP]"); |
67 | 0 | if (!fname) |
68 | 0 | { |
69 | 0 | err = gpg_error_from_syserror (); |
70 | 0 | goto leave; |
71 | 0 | } |
72 | 0 | } |
73 | 0 | else if (opt.outfile |
74 | 0 | && !(opt.flags.use_embedded_filename && opt.flags.dummy_outfile)) |
75 | 0 | { |
76 | 0 | fname = xtrystrdup (opt.outfile); |
77 | 0 | if (!fname) |
78 | 0 | { |
79 | 0 | err = gpg_error_from_syserror (); |
80 | 0 | goto leave; |
81 | 0 | } |
82 | 0 | } |
83 | 0 | else if (embedded_namelen == 8 && !memcmp (embedded_name, "_CONSOLE", 8)) |
84 | 0 | { |
85 | 0 | log_info (_("data not saved; use option \"--output\" to save it\n")); |
86 | 0 | nooutput = 1; |
87 | 0 | } |
88 | 0 | else if (!opt.flags.use_embedded_filename) |
89 | 0 | { |
90 | 0 | if (data) |
91 | 0 | fname = make_outfile_name (iobuf_get_real_fname (data)); |
92 | 0 | if (!fname) |
93 | 0 | fname = ask_outfile_name (embedded_name, embedded_namelen); |
94 | 0 | if (!fname) |
95 | 0 | { |
96 | 0 | err = gpg_error (GPG_ERR_GENERAL); /* Can't create file. */ |
97 | 0 | goto leave; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | else |
101 | 0 | fname = utf8_to_native (embedded_name, embedded_namelen, 0); |
102 | | |
103 | 0 | if (nooutput) |
104 | 0 | ; |
105 | 0 | else if (opt.outfp) |
106 | 0 | { |
107 | 0 | fp = opt.outfp; |
108 | 0 | es_set_binary (fp); |
109 | 0 | } |
110 | 0 | else if (iobuf_is_pipe_filename (fname) || !*fname) |
111 | 0 | { |
112 | | /* Special file name, no filename, or "-" given; write to the |
113 | | * file descriptor or to stdout. */ |
114 | 0 | gnupg_fd_t fd; |
115 | 0 | char xname[64]; |
116 | |
|
117 | 0 | fd = gnupg_check_special_filename (fname); |
118 | 0 | if (fd == GNUPG_INVALID_FD) |
119 | 0 | { |
120 | | /* Not a special filename, thus we want stdout. */ |
121 | 0 | fp = es_stdout; |
122 | 0 | es_set_binary (fp); |
123 | 0 | } |
124 | 0 | else if (!(fp = open_stream_nc (fd, "wb"))) |
125 | 0 | { |
126 | 0 | err = gpg_error_from_syserror (); |
127 | 0 | snprintf (xname, sizeof xname, "[fd %d]", FD_DBG (fd)); |
128 | 0 | log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err)); |
129 | 0 | goto leave; |
130 | 0 | } |
131 | 0 | } |
132 | 0 | else |
133 | 0 | { |
134 | 0 | while (!overwrite_filep (fname)) |
135 | 0 | { |
136 | 0 | char *tmp = ask_outfile_name (NULL, 0); |
137 | 0 | if (!tmp || !*tmp) |
138 | 0 | { |
139 | 0 | xfree (tmp); |
140 | 0 | err = gpg_error (GPG_ERR_EEXIST); |
141 | 0 | goto leave; |
142 | 0 | } |
143 | 0 | xfree (fname); |
144 | 0 | fname = tmp; |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | 0 | if (fp || nooutput) |
149 | 0 | ; |
150 | 0 | else if (is_secured_filename (fname)) |
151 | 0 | { |
152 | 0 | gpg_err_set_errno (EPERM); |
153 | 0 | err = gpg_error_from_syserror (); |
154 | 0 | log_error (_("error creating '%s': %s\n"), fname, gpg_strerror (err)); |
155 | 0 | goto leave; |
156 | 0 | } |
157 | 0 | else if (!(fp = es_fopen (fname, "wb"))) |
158 | 0 | { |
159 | 0 | err = gpg_error_from_syserror (); |
160 | 0 | log_error (_("error creating '%s': %s\n"), fname, gpg_strerror (err)); |
161 | 0 | goto leave; |
162 | 0 | } |
163 | | |
164 | 0 | leave: |
165 | 0 | if (err) |
166 | 0 | { |
167 | 0 | if (fp && fp != es_stdout && fp != opt.outfp) |
168 | 0 | es_fclose (fp); |
169 | 0 | xfree (fname); |
170 | 0 | return err; |
171 | 0 | } |
172 | | |
173 | 0 | *fnamep = fname; |
174 | 0 | *fpp = fp; |
175 | 0 | return 0; |
176 | 0 | } |
177 | | |
178 | | /* Handle a plaintext packet. If MFX is not NULL, update the MDs |
179 | | * Note: We should have used the filter stuff here, but we have to add |
180 | | * some easy mimic to set a read limit, so we calculate only the bytes |
181 | | * from the plaintext. */ |
182 | | int |
183 | | handle_plaintext (PKT_plaintext * pt, md_filter_context_t * mfx, |
184 | | int nooutput, int clearsig) |
185 | 2.37k | { |
186 | 2.37k | char *fname = NULL; |
187 | 2.37k | estream_t fp = NULL; |
188 | 2.37k | static off_t count = 0; |
189 | 2.37k | int err = 0; |
190 | 2.37k | int c; |
191 | 2.37k | int convert; |
192 | | #ifdef __riscos__ |
193 | | int filetype = 0xfff; |
194 | | #endif |
195 | | |
196 | 2.37k | if (pt->mode == 't' || pt->mode == 'u' || pt->mode == 'm') |
197 | 561 | convert = pt->mode; |
198 | 1.81k | else |
199 | 1.81k | convert = 0; |
200 | | |
201 | | /* Let people know what the plaintext info is. This allows the |
202 | | receiving program to try and do something different based on the |
203 | | format code (say, recode UTF-8 to local). */ |
204 | 2.37k | if (!nooutput && is_status_enabled ()) |
205 | 0 | { |
206 | 0 | char status[50]; |
207 | | |
208 | | /* Better make sure that stdout has been flushed in case the |
209 | | output will be written to it. This is to make sure that no |
210 | | not-yet-flushed stuff will be written after the plaintext |
211 | | status message. */ |
212 | 0 | es_fflush (es_stdout); |
213 | |
|
214 | 0 | snprintf (status, sizeof status, |
215 | 0 | "%X %lu ", (byte) pt->mode, (ulong) pt->timestamp); |
216 | 0 | write_status_text_and_buffer (STATUS_PLAINTEXT, |
217 | 0 | status, pt->name, pt->namelen, 0); |
218 | |
|
219 | 0 | if (!pt->is_partial) |
220 | 0 | { |
221 | 0 | snprintf (status, sizeof status, "%lu", (ulong) pt->len); |
222 | 0 | write_status_text (STATUS_PLAINTEXT_LENGTH, status); |
223 | 0 | } |
224 | 0 | } |
225 | | |
226 | 2.37k | if (! nooutput) |
227 | 0 | { |
228 | 0 | err = get_output_file (pt->name, pt->namelen, pt->buf, &fname, &fp); |
229 | 0 | if (err) |
230 | 0 | goto leave; |
231 | 0 | } |
232 | | |
233 | 2.37k | if (!pt->is_partial) |
234 | 1.31k | { |
235 | | /* We have an actual length (which might be zero). */ |
236 | | |
237 | 1.31k | if (clearsig) |
238 | 0 | { |
239 | 0 | log_error ("clearsig encountered while not expected\n"); |
240 | 0 | err = gpg_error (GPG_ERR_UNEXPECTED); |
241 | 0 | goto leave; |
242 | 0 | } |
243 | | |
244 | 1.31k | if (convert) /* Text mode. */ |
245 | 76 | { |
246 | 183k | for (; pt->len; pt->len--) |
247 | 183k | { |
248 | 183k | if ((c = iobuf_get (pt->buf)) == -1) |
249 | 72 | { |
250 | 72 | err = gpg_error_from_syserror (); |
251 | 72 | log_error ("problem reading source (%u bytes remaining)\n", |
252 | 72 | (unsigned) pt->len); |
253 | 72 | goto leave; |
254 | 72 | } |
255 | 183k | if (mfx->md) |
256 | 183k | gcry_md_putc (mfx->md, c); |
257 | 183k | #ifndef HAVE_DOSISH_SYSTEM |
258 | | /* Convert to native line ending. */ |
259 | | /* fixme: this hack might be too simple */ |
260 | 183k | if (c == '\r' && convert != 'm') |
261 | 1.19k | continue; |
262 | 182k | #endif |
263 | 182k | if (fp) |
264 | 0 | { |
265 | 0 | if (opt.max_output && (++count) > opt.max_output) |
266 | 0 | { |
267 | 0 | log_error ("error writing to '%s': %s\n", |
268 | 0 | fname, "exceeded --max-output limit\n"); |
269 | 0 | err = gpg_error (GPG_ERR_TOO_LARGE); |
270 | 0 | goto leave; |
271 | 0 | } |
272 | 0 | else if (es_putc (c, fp) == EOF) |
273 | 0 | { |
274 | 0 | if (es_ferror (fp)) |
275 | 0 | err = gpg_error_from_syserror (); |
276 | 0 | else |
277 | 0 | err = gpg_error (GPG_ERR_EOF); |
278 | 0 | log_error ("error writing to '%s': %s\n", |
279 | 0 | fname, gpg_strerror (err)); |
280 | 0 | goto leave; |
281 | 0 | } |
282 | 0 | } |
283 | 182k | } |
284 | 76 | } |
285 | 1.23k | else /* Binary mode. */ |
286 | 1.23k | { |
287 | 1.23k | size_t temp_size = iobuf_set_buffer_size(0) * 1024; |
288 | 1.23k | byte *buffer; |
289 | | |
290 | 1.23k | if (fp) |
291 | 0 | { |
292 | | /* Disable buffering in estream as we are passing large |
293 | | * buffers to es_fwrite. */ |
294 | 0 | es_setbuf (fp, NULL); |
295 | 0 | } |
296 | | |
297 | 1.23k | buffer = xmalloc (temp_size); |
298 | 1.23k | if (!buffer) |
299 | 0 | { |
300 | 0 | err = gpg_error_from_syserror (); |
301 | 0 | goto leave; |
302 | 0 | } |
303 | | |
304 | 1.45k | while (pt->len) |
305 | 1.14k | { |
306 | 1.14k | int len = pt->len > temp_size ? temp_size : pt->len; |
307 | 1.14k | len = iobuf_read (pt->buf, buffer, len); |
308 | 1.14k | if (len == -1) |
309 | 924 | { |
310 | 924 | err = gpg_error_from_syserror (); |
311 | 924 | log_error ("problem reading source (%u bytes remaining)\n", |
312 | 924 | (unsigned) pt->len); |
313 | 924 | xfree (buffer); |
314 | 924 | goto leave; |
315 | 924 | } |
316 | 222 | if (mfx->md) |
317 | 222 | gcry_md_write (mfx->md, buffer, len); |
318 | 222 | if (fp) |
319 | 0 | { |
320 | 0 | if (opt.max_output && (count += len) > opt.max_output) |
321 | 0 | { |
322 | 0 | log_error ("error writing to '%s': %s\n", |
323 | 0 | fname, "exceeded --max-output limit\n"); |
324 | 0 | err = gpg_error (GPG_ERR_TOO_LARGE); |
325 | 0 | xfree (buffer); |
326 | 0 | goto leave; |
327 | 0 | } |
328 | 0 | else if (es_fwrite (buffer, 1, len, fp) != len) |
329 | 0 | { |
330 | 0 | err = gpg_error_from_syserror (); |
331 | 0 | log_error ("error writing to '%s': %s\n", |
332 | 0 | fname, gpg_strerror (err)); |
333 | 0 | xfree (buffer); |
334 | 0 | goto leave; |
335 | 0 | } |
336 | 0 | } |
337 | 222 | pt->len -= len; |
338 | 222 | } |
339 | 310 | xfree (buffer); |
340 | 310 | } |
341 | 1.31k | } |
342 | 1.06k | else if (!clearsig) |
343 | 635 | { |
344 | 635 | if (convert) |
345 | 59 | { /* text mode */ |
346 | 334k | while ((c = iobuf_get (pt->buf)) != -1) |
347 | 334k | { |
348 | 334k | if (mfx->md) |
349 | 334k | gcry_md_putc (mfx->md, c); |
350 | 334k | #ifndef HAVE_DOSISH_SYSTEM |
351 | 334k | if (c == '\r' && convert != 'm') |
352 | 1.14k | continue; /* fixme: this hack might be too simple */ |
353 | 333k | #endif |
354 | 333k | if (fp) |
355 | 0 | { |
356 | 0 | if (opt.max_output && (++count) > opt.max_output) |
357 | 0 | { |
358 | 0 | log_error ("Error writing to '%s': %s\n", |
359 | 0 | fname, "exceeded --max-output limit\n"); |
360 | 0 | err = gpg_error (GPG_ERR_TOO_LARGE); |
361 | 0 | goto leave; |
362 | 0 | } |
363 | 0 | else if (es_putc (c, fp) == EOF) |
364 | 0 | { |
365 | 0 | if (es_ferror (fp)) |
366 | 0 | err = gpg_error_from_syserror (); |
367 | 0 | else |
368 | 0 | err = gpg_error (GPG_ERR_EOF); |
369 | 0 | log_error ("error writing to '%s': %s\n", |
370 | 0 | fname, gpg_strerror (err)); |
371 | 0 | goto leave; |
372 | 0 | } |
373 | 0 | } |
374 | 333k | } |
375 | 59 | } |
376 | 576 | else |
377 | 576 | { /* binary mode */ |
378 | 576 | size_t temp_size = iobuf_set_buffer_size(0) * 1024; |
379 | 576 | byte *buffer; |
380 | 576 | int eof_seen = 0; |
381 | | |
382 | 576 | if (fp) |
383 | 0 | { |
384 | | /* Disable buffering in estream as we are passing large |
385 | | * buffers to es_fwrite. */ |
386 | 0 | es_setbuf (fp, NULL); |
387 | 0 | } |
388 | | |
389 | 576 | buffer = xtrymalloc (temp_size); |
390 | 576 | if (!buffer) |
391 | 0 | { |
392 | 0 | err = gpg_error_from_syserror (); |
393 | 0 | goto leave; |
394 | 0 | } |
395 | | |
396 | 918 | while (!eof_seen) |
397 | 639 | { |
398 | | /* Why do we check for len < temp_size: |
399 | | * If we won't, we would practically read 2 EOFs but |
400 | | * the first one has already popped the block_filter |
401 | | * off and therefore we don't catch the boundary. |
402 | | * So, always assume EOF if iobuf_read returns less bytes |
403 | | * then requested */ |
404 | 639 | int len = iobuf_read (pt->buf, buffer, temp_size); |
405 | 639 | if (len == -1) |
406 | 297 | break; |
407 | 342 | if (len < temp_size) |
408 | 279 | eof_seen = 1; |
409 | 342 | if (mfx->md) |
410 | 342 | gcry_md_write (mfx->md, buffer, len); |
411 | 342 | if (fp) |
412 | 0 | { |
413 | 0 | if (opt.max_output && (count += len) > opt.max_output) |
414 | 0 | { |
415 | 0 | log_error ("error writing to '%s': %s\n", |
416 | 0 | fname, "exceeded --max-output limit\n"); |
417 | 0 | err = gpg_error (GPG_ERR_TOO_LARGE); |
418 | 0 | xfree (buffer); |
419 | 0 | goto leave; |
420 | 0 | } |
421 | 0 | else if (es_fwrite (buffer, 1, len, fp) != len) |
422 | 0 | { |
423 | 0 | err = gpg_error_from_syserror (); |
424 | 0 | log_error ("error writing to '%s': %s\n", |
425 | 0 | fname, gpg_strerror (err)); |
426 | 0 | xfree (buffer); |
427 | 0 | goto leave; |
428 | 0 | } |
429 | 0 | } |
430 | 342 | } |
431 | 576 | xfree (buffer); |
432 | 576 | } |
433 | 635 | pt->buf = NULL; |
434 | 635 | } |
435 | 426 | else /* Clear text signature - don't hash the last CR,LF. */ |
436 | 426 | { |
437 | 426 | int state = 0; |
438 | | |
439 | 2.22M | while ((c = iobuf_get (pt->buf)) != -1) |
440 | 2.22M | { |
441 | 2.22M | if (fp) |
442 | 0 | { |
443 | 0 | if (opt.max_output && (++count) > opt.max_output) |
444 | 0 | { |
445 | 0 | log_error ("error writing to '%s': %s\n", |
446 | 0 | fname, "exceeded --max-output limit\n"); |
447 | 0 | err = gpg_error (GPG_ERR_TOO_LARGE); |
448 | 0 | goto leave; |
449 | 0 | } |
450 | 0 | else if (es_putc (c, fp) == EOF) |
451 | 0 | { |
452 | 0 | err = gpg_error_from_syserror (); |
453 | 0 | log_error ("error writing to '%s': %s\n", |
454 | 0 | fname, gpg_strerror (err)); |
455 | 0 | goto leave; |
456 | 0 | } |
457 | 0 | } |
458 | 2.22M | if (!mfx->md) |
459 | 0 | continue; |
460 | 2.22M | if (state == 2) |
461 | 601k | { |
462 | 601k | gcry_md_putc (mfx->md, '\r'); |
463 | 601k | gcry_md_putc (mfx->md, '\n'); |
464 | 601k | state = 0; |
465 | 601k | } |
466 | 2.22M | if (!state) |
467 | 1.82M | { |
468 | 1.82M | if (c == '\r') |
469 | 358k | state = 1; |
470 | 1.46M | else if (c == '\n') |
471 | 599k | state = 2; |
472 | 868k | else |
473 | 1.46M | gcry_md_putc (mfx->md, c); |
474 | 1.82M | } |
475 | 399k | else if (state == 1) |
476 | 399k | { |
477 | 399k | if (c == '\n') |
478 | 2.67k | state = 2; |
479 | 396k | else |
480 | 396k | { |
481 | 396k | gcry_md_putc (mfx->md, '\r'); |
482 | 396k | if (c == '\r') |
483 | 41.3k | state = 1; |
484 | 355k | else |
485 | 355k | { |
486 | 355k | state = 0; |
487 | 355k | gcry_md_putc (mfx->md, c); |
488 | 355k | } |
489 | 396k | } |
490 | 399k | } |
491 | 2.22M | } |
492 | 426 | pt->buf = NULL; |
493 | 426 | } |
494 | | |
495 | 1.37k | if (fp && fp != es_stdout && fp != opt.outfp && es_fclose (fp)) |
496 | 0 | { |
497 | 0 | err = gpg_error_from_syserror (); |
498 | 0 | log_error ("error closing '%s': %s\n", fname, gpg_strerror (err)); |
499 | 0 | fp = NULL; |
500 | 0 | goto leave; |
501 | 0 | } |
502 | 1.37k | fp = NULL; |
503 | | |
504 | 2.37k | leave: |
505 | | /* Make sure that stdout gets flushed after the plaintext has been |
506 | | handled. This is for extra security as we do a flush anyway |
507 | | before checking the signature. */ |
508 | 2.37k | if (es_fflush (es_stdout)) |
509 | 0 | { |
510 | | /* We need to check the return code to detect errors like disk |
511 | | full for short plaintexts. See bug#1207. Checking return |
512 | | values is a good idea in any case. */ |
513 | 0 | if (!err) |
514 | 0 | err = gpg_error_from_syserror (); |
515 | 0 | log_error ("error flushing '%s': %s\n", "[stdout]", |
516 | 0 | gpg_strerror (err)); |
517 | 0 | } |
518 | | |
519 | 2.37k | if (fp && fp != es_stdout && fp != opt.outfp) |
520 | 2.37k | es_fclose (fp); |
521 | 2.37k | xfree (fname); |
522 | 2.37k | return err; |
523 | 1.37k | } |
524 | | |
525 | | |
526 | | static void |
527 | | do_hash (gcry_md_hd_t md, gcry_md_hd_t md2, IOBUF fp, int textmode) |
528 | 0 | { |
529 | 0 | text_filter_context_t tfx; |
530 | 0 | int c; |
531 | |
|
532 | 0 | if (textmode) |
533 | 0 | { |
534 | 0 | memset (&tfx, 0, sizeof tfx); |
535 | 0 | iobuf_push_filter (fp, text_filter, &tfx); |
536 | 0 | } |
537 | 0 | if (md2) |
538 | 0 | { /* work around a strange behaviour in pgp2 */ |
539 | | /* It seems that at least PGP5 converts a single CR to a CR,LF too */ |
540 | 0 | int lc = -1; |
541 | 0 | while ((c = iobuf_get (fp)) != -1) |
542 | 0 | { |
543 | 0 | if (c == '\n' && lc == '\r') |
544 | 0 | gcry_md_putc (md2, c); |
545 | 0 | else if (c == '\n') |
546 | 0 | { |
547 | 0 | gcry_md_putc (md2, '\r'); |
548 | 0 | gcry_md_putc (md2, c); |
549 | 0 | } |
550 | 0 | else if (c != '\n' && lc == '\r') |
551 | 0 | { |
552 | 0 | gcry_md_putc (md2, '\n'); |
553 | 0 | gcry_md_putc (md2, c); |
554 | 0 | } |
555 | 0 | else |
556 | 0 | gcry_md_putc (md2, c); |
557 | |
|
558 | 0 | if (md) |
559 | 0 | gcry_md_putc (md, c); |
560 | 0 | lc = c; |
561 | 0 | } |
562 | 0 | } |
563 | 0 | else |
564 | 0 | { |
565 | 0 | size_t temp_size = iobuf_set_buffer_size(0) * 1024; |
566 | 0 | byte *buffer = xmalloc (temp_size); |
567 | 0 | int ret; |
568 | |
|
569 | 0 | while ((ret = iobuf_read (fp, buffer, temp_size)) != -1) |
570 | 0 | { |
571 | 0 | if (md) |
572 | 0 | gcry_md_write (md, buffer, ret); |
573 | 0 | } |
574 | |
|
575 | 0 | xfree (buffer); |
576 | 0 | } |
577 | 0 | } |
578 | | |
579 | | |
580 | | /**************** |
581 | | * Ask for the detached datafile and calculate the digest from it. |
582 | | * INFILE is the name of the input file. |
583 | | */ |
584 | | int |
585 | | ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2, |
586 | | const char *inname, int textmode) |
587 | 0 | { |
588 | 0 | progress_filter_context_t *pfx; |
589 | 0 | char *answer = NULL; |
590 | 0 | IOBUF fp; |
591 | 0 | int rc = 0; |
592 | |
|
593 | 0 | pfx = new_progress_context (); |
594 | 0 | fp = open_sigfile (inname, pfx); /* Open default file. */ |
595 | |
|
596 | 0 | if (!fp && !opt.batch) |
597 | 0 | { |
598 | 0 | int any = 0; |
599 | 0 | tty_printf (_("Detached signature.\n")); |
600 | 0 | do |
601 | 0 | { |
602 | 0 | char *name; |
603 | |
|
604 | 0 | xfree (answer); |
605 | 0 | tty_enable_completion (NULL); |
606 | 0 | name = cpr_get ("detached_signature.filename", |
607 | 0 | _("Please enter name of data file: ")); |
608 | 0 | tty_disable_completion (); |
609 | 0 | cpr_kill_prompt (); |
610 | 0 | answer = make_filename (name, (void *) NULL); |
611 | 0 | xfree (name); |
612 | |
|
613 | 0 | if (any && !*answer) |
614 | 0 | { |
615 | 0 | rc = gpg_error (GPG_ERR_GENERAL); /*G10ERR_READ_FILE */ |
616 | 0 | goto leave; |
617 | 0 | } |
618 | 0 | fp = iobuf_open (answer); |
619 | 0 | if (fp && is_secured_file (iobuf_get_fd (fp))) |
620 | 0 | { |
621 | 0 | iobuf_close (fp); |
622 | 0 | fp = NULL; |
623 | 0 | gpg_err_set_errno (EPERM); |
624 | 0 | } |
625 | 0 | if (!fp && errno == ENOENT) |
626 | 0 | { |
627 | 0 | tty_printf ("No such file, try again or hit enter to quit.\n"); |
628 | 0 | any++; |
629 | 0 | } |
630 | 0 | else if (!fp) |
631 | 0 | { |
632 | 0 | rc = gpg_error_from_syserror (); |
633 | 0 | log_error (_("can't open '%s': %s\n"), answer, |
634 | 0 | strerror (errno)); |
635 | 0 | goto leave; |
636 | 0 | } |
637 | 0 | } |
638 | 0 | while (!fp); |
639 | 0 | } |
640 | | |
641 | 0 | if (!fp) |
642 | 0 | { |
643 | 0 | if (opt.verbose) |
644 | 0 | log_info (_("reading stdin ...\n")); |
645 | 0 | fp = iobuf_open (NULL); |
646 | 0 | log_assert (fp); |
647 | 0 | } |
648 | 0 | do_hash (md, md2, fp, textmode); |
649 | 0 | iobuf_close (fp); |
650 | |
|
651 | 0 | leave: |
652 | 0 | xfree (answer); |
653 | 0 | release_progress_context (pfx); |
654 | 0 | return rc; |
655 | 0 | } |
656 | | |
657 | | |
658 | | |
659 | | /* Hash the given files and append the hash to hash contexts MD and |
660 | | * MD2. If FILES is NULL, stdin is hashed. */ |
661 | | int |
662 | | hash_datafiles (gcry_md_hd_t md, gcry_md_hd_t md2, strlist_t files, |
663 | | const char *sigfilename, int textmode) |
664 | 268k | { |
665 | 268k | progress_filter_context_t *pfx; |
666 | 268k | IOBUF fp; |
667 | 268k | strlist_t sl; |
668 | | |
669 | 268k | pfx = new_progress_context (); |
670 | | |
671 | 268k | if (!files) |
672 | 268k | { |
673 | | /* Check whether we can open the signed material. We avoid |
674 | | trying to open a file if run in batch mode. This assumed |
675 | | data file for a sig file feature is just a convenience thing |
676 | | for the command line and the user needs to read possible |
677 | | warning messages. */ |
678 | 268k | if (!opt.batch) |
679 | 268k | { |
680 | 268k | fp = open_sigfile (sigfilename, pfx); |
681 | 268k | if (fp) |
682 | 0 | { |
683 | 0 | do_hash (md, md2, fp, textmode); |
684 | 0 | iobuf_close (fp); |
685 | 0 | release_progress_context (pfx); |
686 | 0 | return 0; |
687 | 0 | } |
688 | 268k | } |
689 | 268k | log_error (_("no signed data\n")); |
690 | 268k | release_progress_context (pfx); |
691 | 268k | return gpg_error (GPG_ERR_NO_DATA); |
692 | 268k | } |
693 | | |
694 | | |
695 | 0 | for (sl = files; sl; sl = sl->next) |
696 | 0 | { |
697 | 0 | fp = iobuf_open (sl->d); |
698 | 0 | if (fp && is_secured_file (iobuf_get_fd (fp))) |
699 | 0 | { |
700 | 0 | iobuf_close (fp); |
701 | 0 | fp = NULL; |
702 | 0 | gpg_err_set_errno (EPERM); |
703 | 0 | } |
704 | 0 | if (!fp) |
705 | 0 | { |
706 | 0 | int rc = gpg_error_from_syserror (); |
707 | 0 | log_error (_("can't open signed data '%s'\n"), |
708 | 0 | print_fname_stdin (sl->d)); |
709 | 0 | release_progress_context (pfx); |
710 | 0 | return rc; |
711 | 0 | } |
712 | 0 | handle_progress (pfx, fp, sl->d); |
713 | 0 | do_hash (md, md2, fp, textmode); |
714 | 0 | iobuf_close (fp); |
715 | 0 | } |
716 | | |
717 | 0 | release_progress_context (pfx); |
718 | 0 | return 0; |
719 | 0 | } |
720 | | |
721 | | |
722 | | /* Hash the data from file descriptor DATA_FD and append the hash to hash |
723 | | contexts MD and MD2. */ |
724 | | int |
725 | | hash_datafile_by_fd (gcry_md_hd_t md, gcry_md_hd_t md2, |
726 | | gnupg_fd_t data_fd, int textmode) |
727 | 0 | { |
728 | 0 | progress_filter_context_t *pfx = new_progress_context (); |
729 | 0 | iobuf_t fp; |
730 | |
|
731 | 0 | if (is_secured_file (data_fd)) |
732 | 0 | { |
733 | 0 | fp = NULL; |
734 | 0 | gpg_err_set_errno (EPERM); |
735 | 0 | } |
736 | 0 | else |
737 | 0 | fp = iobuf_fdopen_nc (data_fd, "rb"); |
738 | |
|
739 | 0 | if (!fp) |
740 | 0 | { |
741 | 0 | int rc = gpg_error_from_syserror (); |
742 | 0 | log_error (_("can't open signed data fd=%d: %s\n"), |
743 | 0 | FD_DBG (data_fd), strerror (errno)); |
744 | 0 | release_progress_context (pfx); |
745 | 0 | return rc; |
746 | 0 | } |
747 | | |
748 | 0 | handle_progress (pfx, fp, NULL); |
749 | |
|
750 | 0 | do_hash (md, md2, fp, textmode); |
751 | |
|
752 | 0 | iobuf_close (fp); |
753 | |
|
754 | 0 | release_progress_context (pfx); |
755 | 0 | return 0; |
756 | 0 | } |
757 | | |
758 | | |
759 | | /* Set up a plaintext packet with the appropriate filename. If there |
760 | | is a --set-filename, use it (it's already UTF8). If there is a |
761 | | regular filename, UTF8-ize it if necessary. If there is no |
762 | | filenames at all, set the field empty. */ |
763 | | |
764 | | PKT_plaintext * |
765 | | setup_plaintext_name (const char *filename, IOBUF iobuf) |
766 | 0 | { |
767 | 0 | PKT_plaintext *pt; |
768 | |
|
769 | 0 | if ((filename && !iobuf_is_pipe_filename (filename)) |
770 | 0 | || (opt.set_filename && !iobuf_is_pipe_filename (opt.set_filename))) |
771 | 0 | { |
772 | 0 | char *s; |
773 | |
|
774 | 0 | if (opt.set_filename) |
775 | 0 | s = make_basename (opt.set_filename, iobuf_get_real_fname (iobuf)); |
776 | 0 | else if (filename && !opt.flags.utf8_filename) |
777 | 0 | { |
778 | 0 | char *tmp = native_to_utf8 (filename); |
779 | 0 | s = make_basename (tmp, iobuf_get_real_fname (iobuf)); |
780 | 0 | xfree (tmp); |
781 | 0 | } |
782 | 0 | else |
783 | 0 | s = make_basename (filename, iobuf_get_real_fname (iobuf)); |
784 | |
|
785 | 0 | pt = xmalloc (sizeof *pt + strlen (s) - 1); |
786 | 0 | pt->namelen = strlen (s); |
787 | 0 | memcpy (pt->name, s, pt->namelen); |
788 | 0 | xfree (s); |
789 | 0 | } |
790 | 0 | else |
791 | 0 | { |
792 | | /* no filename */ |
793 | 0 | pt = xmalloc (sizeof *pt - 1); |
794 | 0 | pt->namelen = 0; |
795 | 0 | } |
796 | |
|
797 | 0 | return pt; |
798 | 0 | } |