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