Coverage Report

Created: 2026-09-14 07:17

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