1 /* plaintext.c - process plaintext packets
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 * 2005 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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
29 #include <sys/types.h>
30 #ifdef HAVE_DOSISH_SYSTEM
31 #include <fcntl.h> /* for setmode() */
46 * Handle a plaintext packet. If MFX is not NULL, update the MDs
47 * Note: we should use the filter stuff here, but we have to add some
48 * easy mimic to set a read limit, so we calculate only the
49 * bytes from the plaintext.
52 handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
53 int nooutput, int clearsig )
60 int convert = (pt->mode == 't' || pt->mode == 'u');
65 /* Let people know what the plaintext info is. This allows the
66 receiving program to try and do something different based on
67 the format code (say, recode UTF-8 to local). */
68 if(!nooutput && is_status_enabled())
72 sprintf(status,"%X %lu ",(byte)pt->mode,(ulong)pt->timestamp);
73 write_status_text_and_buffer(STATUS_PLAINTEXT,
74 status,pt->name,pt->namelen,0);
78 sprintf(status,"%lu",(ulong)pt->len);
79 write_status_text(STATUS_PLAINTEXT_LENGTH,status);
83 /* create the filename as C string */
86 else if( opt.outfile ) {
87 fname = xmalloc( strlen( opt.outfile ) + 1);
88 strcpy(fname, opt.outfile );
90 else if( pt->namelen == 8 && !memcmp( pt->name, "_CONSOLE", 8 ) ) {
91 log_info(_("data not saved; use option \"--output\" to save it\n"));
94 else if( !opt.use_embedded_filename ) {
95 fname = make_outfile_name( iobuf_get_real_fname(pt->buf) );
97 fname = ask_outfile_name( pt->name, pt->namelen );
99 rc = G10ERR_CREATE_FILE;
104 fname = make_printable_string( pt->name, pt->namelen, 0 );
109 else if ( iobuf_is_pipe_filename (fname) || !*fname)
111 /* No filename or "-" given; write to stdout. */
113 #ifdef HAVE_DOSISH_SYSTEM
114 setmode ( fileno(fp) , O_BINARY );
118 while( !overwrite_filep (fname) ) {
119 char *tmp = ask_outfile_name (NULL, 0);
120 if ( !tmp || !*tmp ) {
122 rc = G10ERR_CREATE_FILE;
133 else if (is_secured_filename (fname))
136 log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
137 rc = G10ERR_CREATE_FILE;
140 else if( !(fp = fopen(fname,"wb")) ) {
141 log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
142 rc = G10ERR_CREATE_FILE;
145 #else /* __riscos__ */
146 /* If no output filename was given, i.e. we constructed it,
147 convert all '.' in fname to '/' but not vice versa as
148 we don't create directories! */
150 for( c=0; fname[c]; ++c )
151 if( fname[c] == '.' )
157 fp = fopen(fname,"wb");
159 log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
160 rc = G10ERR_CREATE_FILE;
162 log_info("Do output file and input file have the same name?\n");
166 /* If there's a ,xxx extension in the embedded filename,
167 use that, else check whether the user input (in fname)
168 has a ,xxx appended, then use that in preference */
169 if( (c = riscos_get_filetype_from_string( pt->name,
170 pt->namelen )) != -1 )
172 if( (c = riscos_get_filetype_from_string( fname,
173 strlen(fname) )) != -1 )
175 riscos_set_filetype_by_number(fname, filetype);
177 #endif /* __riscos__ */
179 if( !pt->is_partial ) {
180 /* we have an actual length (which might be zero). */
182 if( convert ) { /* text mode */
183 for( ; pt->len; pt->len-- ) {
184 if( (c = iobuf_get(pt->buf)) == -1 ) {
185 log_error("Problem reading source (%u bytes remaining)\n",
187 rc = G10ERR_READ_FILE;
191 md_putc(mfx->md, c );
192 #ifndef HAVE_DOSISH_SYSTEM
193 if( c == '\r' ) /* convert to native line ending */
194 continue; /* fixme: this hack might be too simple */
198 if(opt.max_output && (++count)>opt.max_output)
200 log_error("Error writing to `%s': %s\n",
201 fname,"exceeded --max-output limit\n");
202 rc = G10ERR_WRITE_FILE;
205 else if( putc( c, fp ) == EOF )
207 log_error("Error writing to `%s': %s\n",
208 fname, strerror(errno) );
209 rc = G10ERR_WRITE_FILE;
215 else { /* binary mode */
216 byte *buffer = xmalloc( 32768 );
218 int len = pt->len > 32768 ? 32768 : pt->len;
219 len = iobuf_read( pt->buf, buffer, len );
221 log_error("Problem reading source (%u bytes remaining)\n",
223 rc = G10ERR_READ_FILE;
228 md_write( mfx->md, buffer, len );
231 if(opt.max_output && (count+=len)>opt.max_output)
233 log_error("Error writing to `%s': %s\n",
234 fname,"exceeded --max-output limit\n");
235 rc = G10ERR_WRITE_FILE;
239 else if( fwrite( buffer, 1, len, fp ) != len )
241 log_error("Error writing to `%s': %s\n",
242 fname, strerror(errno) );
243 rc = G10ERR_WRITE_FILE;
253 else if( !clearsig ) {
254 if( convert ) { /* text mode */
255 while( (c = iobuf_get(pt->buf)) != -1 ) {
257 md_putc(mfx->md, c );
258 #ifndef HAVE_DOSISH_SYSTEM
259 if( convert && c == '\r' )
260 continue; /* fixme: this hack might be too simple */
264 if(opt.max_output && (++count)>opt.max_output)
266 log_error("Error writing to `%s': %s\n",
267 fname,"exceeded --max-output limit\n");
268 rc = G10ERR_WRITE_FILE;
271 else if( putc( c, fp ) == EOF )
273 log_error("Error writing to `%s': %s\n",
274 fname, strerror(errno) );
275 rc = G10ERR_WRITE_FILE;
281 else { /* binary mode */
282 byte *buffer = xmalloc( 32768 );
284 for( eof=0; !eof; ) {
285 /* Why do we check for len < 32768:
286 * If we won't, we would practically read 2 EOFs but
287 * the first one has already popped the block_filter
288 * off and therefore we don't catch the boundary.
289 * So, always assume EOF if iobuf_read returns less bytes
291 int len = iobuf_read( pt->buf, buffer, 32768 );
297 md_write( mfx->md, buffer, len );
300 if(opt.max_output && (count+=len)>opt.max_output)
302 log_error("Error writing to `%s': %s\n",
303 fname,"exceeded --max-output limit\n");
304 rc = G10ERR_WRITE_FILE;
308 else if( fwrite( buffer, 1, len, fp ) != len ) {
309 log_error("Error writing to `%s': %s\n",
310 fname, strerror(errno) );
311 rc = G10ERR_WRITE_FILE;
321 else { /* clear text signature - don't hash the last cr,lf */
324 while( (c = iobuf_get(pt->buf)) != -1 ) {
327 if(opt.max_output && (++count)>opt.max_output)
329 log_error("Error writing to `%s': %s\n",
330 fname,"exceeded --max-output limit\n");
331 rc = G10ERR_WRITE_FILE;
334 else if( putc( c, fp ) == EOF )
336 log_error("Error writing to `%s': %s\n",
337 fname, strerror(errno) );
338 rc = G10ERR_WRITE_FILE;
345 md_putc(mfx->md, '\r' );
346 md_putc(mfx->md, '\n' );
355 md_putc(mfx->md, c );
357 else if( state == 1 ) {
361 md_putc(mfx->md, '\r' );
366 md_putc(mfx->md, c );
374 if( fp && fp != stdout && fclose(fp) ) {
375 log_error("Error closing `%s': %s\n", fname, strerror(errno) );
377 rc = G10ERR_WRITE_FILE;
383 if( fp && fp != stdout )
390 do_hash( MD_HANDLE md, MD_HANDLE md2, IOBUF fp, int textmode )
392 text_filter_context_t tfx;
396 memset( &tfx, 0, sizeof tfx);
397 iobuf_push_filter( fp, text_filter, &tfx );
399 if( md2 ) { /* work around a strange behaviour in pgp2 */
400 /* It seems that at least PGP5 converts a single CR to a CR,LF too */
402 while( (c = iobuf_get(fp)) != -1 ) {
403 if( c == '\n' && lc == '\r' )
405 else if( c == '\n' ) {
409 else if( c != '\n' && lc == '\r' ) {
422 while( (c = iobuf_get(fp)) != -1 ) {
431 * Ask for the detached datafile and calculate the digest from it.
432 * INFILE is the name of the input file.
435 ask_for_detached_datafile( MD_HANDLE md, MD_HANDLE md2,
436 const char *inname, int textmode )
438 progress_filter_context_t pfx;
443 fp = open_sigfile( inname, &pfx ); /* open default file */
445 if( !fp && !opt.batch ) {
447 tty_printf(_("Detached signature.\n"));
450 tty_enable_completion(NULL);
451 answer = cpr_get("detached_signature.filename",
452 _("Please enter name of data file: "));
453 tty_disable_completion();
455 if( any && !*answer ) {
456 rc = G10ERR_READ_FILE;
459 fp = iobuf_open(answer);
460 if (fp && is_secured_file (iobuf_get_fd (fp)))
466 if( !fp && errno == ENOENT ) {
467 tty_printf("No such file, try again or hit enter to quit.\n");
472 log_error(_("can't open `%s': %s\n"), answer, strerror(errno));
473 rc = G10ERR_READ_FILE;
481 log_info(_("reading stdin ...\n"));
482 fp = iobuf_open( NULL );
485 do_hash( md, md2, fp, textmode );
496 * Hash the given files and append the hash to hash context md.
497 * If FILES is NULL, hash stdin.
500 hash_datafiles( MD_HANDLE md, MD_HANDLE md2, STRLIST files,
501 const char *sigfilename, int textmode )
503 progress_filter_context_t pfx;
508 /* check whether we can open the signed material */
509 fp = open_sigfile( sigfilename, &pfx );
511 do_hash( md, md2, fp, textmode );
515 log_error (_("no signed data\n"));
516 return G10ERR_OPEN_FILE;
520 for (sl=files; sl; sl = sl->next ) {
521 fp = iobuf_open( sl->d );
522 if (fp && is_secured_file (iobuf_get_fd (fp)))
529 log_error(_("can't open signed data `%s'\n"),
530 print_fname_stdin(sl->d));
531 return G10ERR_OPEN_FILE;
533 handle_progress (&pfx, fp, sl->d);
534 do_hash( md, md2, fp, textmode );