1 /* plaintext.c - process plaintext packets
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 * 2006 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/>.
27 #include <sys/types.h>
28 #ifdef HAVE_DOSISH_SYSTEM
29 #include <fcntl.h> /* for setmode() */
44 * Handle a plaintext packet. If MFX is not NULL, update the MDs
45 * Note: we should use the filter stuff here, but we have to add some
46 * easy mimic to set a read limit, so we calculate only the
47 * bytes from the plaintext.
50 handle_plaintext( PKT_plaintext
*pt
, md_filter_context_t
*mfx
,
51 int nooutput
, int clearsig
)
58 int convert
= (pt
->mode
== 't' || pt
->mode
== 'u');
63 /* Let people know what the plaintext info is. This allows the
64 receiving program to try and do something different based on
65 the format code (say, recode UTF-8 to local). */
66 if(!nooutput
&& is_status_enabled())
70 /* Better make sure that stdout has been flushed in case the
71 output will be written to it. This is to make sure that no
72 not-yet-flushed stuff will be written after the plaintext
76 sprintf(status
,"%X %lu ",(byte
)pt
->mode
,(ulong
)pt
->timestamp
);
77 write_status_text_and_buffer(STATUS_PLAINTEXT
,
78 status
,pt
->name
,pt
->namelen
,0);
82 sprintf(status
,"%lu",(ulong
)pt
->len
);
83 write_status_text(STATUS_PLAINTEXT_LENGTH
,status
);
87 /* create the filename as C string */
90 else if( opt
.outfile
) {
91 fname
= xmalloc( strlen( opt
.outfile
) + 1);
92 strcpy(fname
, opt
.outfile
);
94 else if( pt
->namelen
== 8 && !memcmp( pt
->name
, "_CONSOLE", 8 ) ) {
95 log_info(_("data not saved; use option \"--output\" to save it\n"));
98 else if( !opt
.flags
.use_embedded_filename
) {
99 fname
= make_outfile_name( iobuf_get_real_fname(pt
->buf
) );
101 fname
= ask_outfile_name( pt
->name
, pt
->namelen
);
103 rc
= gpg_error (GPG_ERR_GENERAL
); /* Can't create file. */
108 fname
=utf8_to_native(pt
->name
,pt
->namelen
,0);
112 else if ( iobuf_is_pipe_filename (fname
) || !*fname
)
114 /* No filename or "-" given; write to stdout. */
116 #ifdef HAVE_DOSISH_SYSTEM
117 setmode ( fileno(fp
) , O_BINARY
);
121 while( !overwrite_filep (fname
) ) {
122 char *tmp
= ask_outfile_name (NULL
, 0);
123 if ( !tmp
|| !*tmp
) {
125 rc
= gpg_error (GPG_ERR_GENERAL
); /* G10ERR_CREATE_FILE*/
136 else if (is_secured_filename (fname
))
139 rc
= gpg_error_from_syserror ();
140 log_error(_("error creating `%s': %s\n"), fname
, strerror(errno
) );
143 else if( !(fp
= fopen(fname
,"wb")) ) {
144 rc
= gpg_error_from_syserror ();
145 log_error(_("error creating `%s': %s\n"), fname
, strerror(errno
) );
148 #else /* __riscos__ */
149 /* If no output filename was given, i.e. we constructed it,
150 convert all '.' in fname to '/' but not vice versa as
151 we don't create directories! */
153 for( c
=0; fname
[c
]; ++c
)
154 if( fname
[c
] == '.' )
160 fp
= fopen(fname
,"wb");
162 log_error(_("error creating `%s': %s\n"), fname
, strerror(errno
) );
163 rc
= G10ERR_CREATE_FILE
;
165 log_info("Do output file and input file have the same name?\n");
169 /* If there's a ,xxx extension in the embedded filename,
170 use that, else check whether the user input (in fname)
171 has a ,xxx appended, then use that in preference */
172 if( (c
= riscos_get_filetype_from_string( pt
->name
,
173 pt
->namelen
)) != -1 )
175 if( (c
= riscos_get_filetype_from_string( fname
,
176 strlen(fname
) )) != -1 )
178 riscos_set_filetype_by_number(fname
, filetype
);
180 #endif /* __riscos__ */
182 if( !pt
->is_partial
) {
183 /* We have an actual length (which might be zero). */
186 log_error ("clearsig encountered while not expected\n");
187 rc
= G10ERR_UNEXPECTED
;
191 if( convert
) { /* text mode */
192 for( ; pt
->len
; pt
->len
-- ) {
193 if( (c
= iobuf_get(pt
->buf
)) == -1 ) {
194 rc
= gpg_error_from_syserror ();
195 log_error ("problem reading source (%u bytes remaining)\n",
200 gcry_md_putc (mfx
->md
, c
);
201 #ifndef HAVE_DOSISH_SYSTEM
202 if( c
== '\r' ) /* convert to native line ending */
203 continue; /* fixme: this hack might be too simple */
207 if(opt
.max_output
&& (++count
)>opt
.max_output
)
209 log_error ("error writing to `%s': %s\n",
210 fname
,"exceeded --max-output limit\n");
211 rc
= gpg_error (GPG_ERR_TOO_LARGE
);
214 else if( putc( c
, fp
) == EOF
)
217 rc
= gpg_error_from_syserror ();
219 rc
= gpg_error (GPG_ERR_EOF
);
220 log_error ("error writing to `%s': %s\n",
221 fname
, strerror(errno
) );
227 else { /* binary mode */
228 byte
*buffer
= xmalloc( 32768 );
230 int len
= pt
->len
> 32768 ? 32768 : pt
->len
;
231 len
= iobuf_read( pt
->buf
, buffer
, len
);
233 rc
= gpg_error_from_syserror ();
234 log_error ("problem reading source (%u bytes remaining)\n",
240 gcry_md_write ( mfx
->md
, buffer
, len
);
243 if(opt
.max_output
&& (count
+=len
)>opt
.max_output
)
245 log_error ("error writing to `%s': %s\n",
246 fname
,"exceeded --max-output limit\n");
247 rc
= gpg_error (GPG_ERR_TOO_LARGE
);
251 else if( fwrite( buffer
, 1, len
, fp
) != len
)
253 rc
= gpg_error_from_syserror ();
254 log_error ("error writing to `%s': %s\n",
255 fname
, strerror(errno
) );
265 else if( !clearsig
) {
266 if( convert
) { /* text mode */
267 while( (c
= iobuf_get(pt
->buf
)) != -1 ) {
269 gcry_md_putc (mfx
->md
, c
);
270 #ifndef HAVE_DOSISH_SYSTEM
271 if( convert
&& c
== '\r' )
272 continue; /* fixme: this hack might be too simple */
276 if(opt
.max_output
&& (++count
)>opt
.max_output
)
278 log_error("Error writing to `%s': %s\n",
279 fname
,"exceeded --max-output limit\n");
280 rc
= gpg_error (GPG_ERR_TOO_LARGE
);
283 else if( putc( c
, fp
) == EOF
)
286 rc
= gpg_error_from_syserror ();
288 rc
= gpg_error (GPG_ERR_EOF
);
289 log_error("error writing to `%s': %s\n",
290 fname
, strerror(errno
) );
296 else { /* binary mode */
297 byte
*buffer
= xmalloc( 32768 );
300 while ( !eof_seen
) {
301 /* Why do we check for len < 32768:
302 * If we won't, we would practically read 2 EOFs but
303 * the first one has already popped the block_filter
304 * off and therefore we don't catch the boundary.
305 * So, always assume EOF if iobuf_read returns less bytes
307 int len
= iobuf_read( pt
->buf
, buffer
, 32768 );
313 gcry_md_write ( mfx
->md
, buffer
, len
);
316 if(opt
.max_output
&& (count
+=len
)>opt
.max_output
)
318 log_error("error writing to `%s': %s\n",
319 fname
,"exceeded --max-output limit\n");
320 rc
= gpg_error (GPG_ERR_TOO_LARGE
);
324 else if( fwrite( buffer
, 1, len
, fp
) != len
) {
325 rc
= (errno
? gpg_error_from_syserror ()
326 : gpg_error (GPG_ERR_INTERNAL
));
327 log_error ("error writing to `%s': %s\n",
328 fname
, strerror(errno
) );
338 else { /* clear text signature - don't hash the last cr,lf */
341 while( (c
= iobuf_get(pt
->buf
)) != -1 ) {
344 if(opt
.max_output
&& (++count
)>opt
.max_output
)
346 log_error ("error writing to `%s': %s\n",
347 fname
,"exceeded --max-output limit\n");
348 rc
= gpg_error (GPG_ERR_TOO_LARGE
);
351 else if( putc( c
, fp
) == EOF
)
353 rc
= (errno
? gpg_error_from_syserror ()
354 : gpg_error (GPG_ERR_INTERNAL
));
355 log_error ("error writing to `%s': %s\n",
356 fname
, strerror(errno
) );
363 gcry_md_putc (mfx
->md
, '\r' );
364 gcry_md_putc (mfx
->md
, '\n' );
373 gcry_md_putc(mfx
->md
, c
);
375 else if( state
== 1 ) {
379 gcry_md_putc(mfx
->md
, '\r' );
384 gcry_md_putc(mfx
->md
, c
);
392 if( fp
&& fp
!= stdout
&& fclose(fp
) ) {
393 rc
= (errno
? gpg_error_from_syserror ()
394 : gpg_error (GPG_ERR_INTERNAL
));
395 log_error ("error closing `%s': %s\n", fname
, strerror(errno
) );
402 /* Make sure that stdout gets flushed after the plaintext has
403 been handled. This is for extra security as we do a
404 flush anyway before checking the signature. */
407 if( fp
&& fp
!= stdout
)
414 do_hash( gcry_md_hd_t md
, gcry_md_hd_t md2
, IOBUF fp
, int textmode
)
416 text_filter_context_t tfx
;
420 memset( &tfx
, 0, sizeof tfx
);
421 iobuf_push_filter( fp
, text_filter
, &tfx
);
423 if( md2
) { /* work around a strange behaviour in pgp2 */
424 /* It seems that at least PGP5 converts a single CR to a CR,LF too */
426 while( (c
= iobuf_get(fp
)) != -1 ) {
427 if( c
== '\n' && lc
== '\r' )
428 gcry_md_putc (md2
, c
);
429 else if( c
== '\n' ) {
430 gcry_md_putc (md2
, '\r');
431 gcry_md_putc (md2
, c
);
433 else if( c
!= '\n' && lc
== '\r' ) {
434 gcry_md_putc (md2
, '\n');
435 gcry_md_putc (md2
, c
);
438 gcry_md_putc (md2
, c
);
441 gcry_md_putc (md
, c
);
446 while( (c
= iobuf_get(fp
)) != -1 ) {
448 gcry_md_putc (md
, c
);
455 * Ask for the detached datafile and calculate the digest from it.
456 * INFILE is the name of the input file.
459 ask_for_detached_datafile (gcry_md_hd_t md
, gcry_md_hd_t md2
,
460 const char *inname
, int textmode
)
462 progress_filter_context_t
*pfx
;
467 pfx
= new_progress_context ();
468 fp
= open_sigfile ( inname
, pfx
); /* Open default file. */
470 if( !fp
&& !opt
.batch
) {
472 tty_printf(_("Detached signature.\n"));
477 tty_enable_completion(NULL
);
478 name
= cpr_get("detached_signature.filename",
479 _("Please enter name of data file: "));
480 tty_disable_completion();
482 answer
=make_filename(name
,(void *)NULL
);
485 if( any
&& !*answer
) {
486 rc
= gpg_error (GPG_ERR_GENERAL
); /*G10ERR_READ_FILE*/
489 fp
= iobuf_open(answer
);
490 if (fp
&& is_secured_file (iobuf_get_fd (fp
)))
496 if( !fp
&& errno
== ENOENT
) {
497 tty_printf("No such file, try again or hit enter to quit.\n");
502 rc
= gpg_error_from_syserror ();
503 log_error(_("can't open `%s': %s\n"), answer
, strerror(errno
));
511 log_info(_("reading stdin ...\n"));
512 fp
= iobuf_open( NULL
);
515 do_hash( md
, md2
, fp
, textmode
);
520 release_progress_context (pfx
);
527 * Hash the given files and append the hash to hash context md.
528 * If FILES is NULL, hash stdin.
531 hash_datafiles( gcry_md_hd_t md
, gcry_md_hd_t md2
, strlist_t files
,
532 const char *sigfilename
, int textmode
)
534 progress_filter_context_t
*pfx
;
538 pfx
= new_progress_context ();
541 /* check whether we can open the signed material */
542 fp
= open_sigfile( sigfilename
, pfx
);
544 do_hash( md
, md2
, fp
, textmode
);
546 release_progress_context (pfx
);
549 log_error (_("no signed data\n"));
550 release_progress_context (pfx
);
551 return gpg_error (GPG_ERR_NO_DATA
);
555 for (sl
=files
; sl
; sl
= sl
->next
) {
556 fp
= iobuf_open( sl
->d
);
557 if (fp
&& is_secured_file (iobuf_get_fd (fp
)))
564 int rc
= gpg_error_from_syserror ();
565 log_error(_("can't open signed data `%s'\n"),
566 print_fname_stdin(sl
->d
));
567 release_progress_context (pfx
);
570 handle_progress (pfx
, fp
, sl
->d
);
571 do_hash( md
, md2
, fp
, textmode
);
575 release_progress_context (pfx
);
580 /* Hash the data from file descriptor DATA_FD and append the hash to hash
581 contexts MD and MD2. */
583 hash_datafile_by_fd ( gcry_md_hd_t md
, gcry_md_hd_t md2
, int data_fd
,
586 progress_filter_context_t
*pfx
= new_progress_context ();
589 fp
= iobuf_fdopen (data_fd
, "rb");
590 if (fp
&& is_secured_file (data_fd
))
598 int rc
= gpg_error_from_syserror ();
599 log_error ( _("can't open signed data fd=%d: %s\n"),
600 data_fd
, strerror (errno
));
601 release_progress_context (pfx
);
605 handle_progress (pfx
, fp
, NULL
);
607 do_hash ( md
, md2
, fp
, textmode
);
611 release_progress_context (pfx
);
616 /* Set up a plaintext packet with the appropriate filename. If there
617 is a --set-filename, use it (it's already UTF8). If there is a
618 regular filename, UTF8-ize it if necessary. If there is no
619 filenames at all, set the field empty. */
622 setup_plaintext_name(const char *filename
,IOBUF iobuf
)
626 if(filename
|| opt
.set_filename
)
631 s
=make_basename(opt
.set_filename
,iobuf_get_real_fname(iobuf
));
632 else if(filename
&& !opt
.flags
.utf8_filename
)
634 char *tmp
=native_to_utf8(filename
);
635 s
=make_basename(tmp
,iobuf_get_real_fname(iobuf
));
639 s
=make_basename(filename
,iobuf_get_real_fname(iobuf
));
641 pt
= xmalloc (sizeof *pt
+ strlen(s
) - 1);
642 pt
->namelen
= strlen (s
);
643 memcpy (pt
->name
, s
, pt
->namelen
);
649 pt
= xmalloc (sizeof *pt
- 1);