2002-06-29 15:46:34 +02:00
|
|
|
/* plaintext.c - process plaintext packets
|
2010-03-26 17:57:09 +01:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
|
|
|
|
* 2007, 2010 Free Software Foundation, Inc.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-18 15:06:00 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-10-23 12:48:09 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-18 15:06:00 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-18 15:06:00 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-10-23 12:48:09 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
1997-11-18 15:06:00 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
1999-01-07 18:05:48 +01:00
|
|
|
#include <assert.h>
|
2004-02-19 00:09:27 +01:00
|
|
|
#include <sys/types.h>
|
2000-07-28 18:19:07 +02:00
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
* armor.c, g10.c, kbnode.c, misc.c, pkclist.c, sign.c, build-packet.c,
getkey.c, keydb.c, openfile.c, plaintext.c, status.c, gpgv.c, keygen.c,
options.h, sig-check.c, tdbio.h, encode.c, mainproc.c, parse-packet.c,
signal.c, textfilter.c: Edit all preprocessor instructions to remove
whitespace before the '#'. This is not required by C89, but there are some
compilers out there that don't like it.
2003-05-24 23:50:33 +02:00
|
|
|
#include <fcntl.h> /* for setmode() */
|
2000-07-28 18:19:07 +02:00
|
|
|
#endif
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
#include "util.h"
|
2002-06-29 15:46:34 +02:00
|
|
|
#include "memory.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
#include "options.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "ttyio.h"
|
1997-11-24 12:04:11 +01:00
|
|
|
#include "filter.h"
|
1997-12-23 18:30:18 +01:00
|
|
|
#include "main.h"
|
1998-08-08 21:27:00 +02:00
|
|
|
#include "status.h"
|
1998-03-09 22:44:06 +01:00
|
|
|
#include "i18n.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2010-09-28 12:07:30 +02:00
|
|
|
#ifdef __VMS
|
|
|
|
# include "vms.h" /* Not part of the standard GnuPG tarball. See
|
|
|
|
http://antinode.info/dec/sw/gnupg.html */
|
|
|
|
# define fopen fopen_vms
|
|
|
|
#endif /* def __VMS */
|
|
|
|
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/****************
|
1997-11-24 12:04:11 +01:00
|
|
|
* Handle a plaintext packet. If MFX is not NULL, update the MDs
|
|
|
|
* Note: we should use the filter stuff here, but we have to add some
|
|
|
|
* easy mimic to set a read limit, so we calculate only the
|
|
|
|
* bytes from the plaintext.
|
1997-11-18 15:06:00 +01:00
|
|
|
*/
|
|
|
|
int
|
1998-07-08 11:29:43 +02:00
|
|
|
handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
|
|
|
|
int nooutput, int clearsig )
|
1997-11-18 15:06:00 +01:00
|
|
|
{
|
1998-06-25 12:19:08 +02:00
|
|
|
char *fname = NULL;
|
1997-11-18 15:06:00 +01:00
|
|
|
FILE *fp = NULL;
|
2004-02-26 03:03:27 +01:00
|
|
|
static off_t count=0;
|
1997-11-18 15:06:00 +01:00
|
|
|
int rc = 0;
|
|
|
|
int c;
|
2004-04-16 18:31:19 +02:00
|
|
|
int convert = (pt->mode == 't' || pt->mode == 'u');
|
2002-10-28 14:26:44 +01:00
|
|
|
#ifdef __riscos__
|
|
|
|
int filetype = 0xfff;
|
|
|
|
#endif
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2004-07-15 23:00:35 +02:00
|
|
|
/* Let people know what the plaintext info is. This allows the
|
|
|
|
receiving program to try and do something different based on
|
|
|
|
the format code (say, recode UTF-8 to local). */
|
|
|
|
if(!nooutput && is_status_enabled())
|
|
|
|
{
|
2004-08-08 15:28:04 +02:00
|
|
|
char status[50];
|
2004-07-15 23:00:35 +02:00
|
|
|
|
2007-03-08 13:13:15 +01:00
|
|
|
/* Better make sure that stdout has been flushed in case the
|
|
|
|
output will be written to it. This is to make sure that no
|
|
|
|
not-yet-flushed stuff will be written after the plaintext
|
|
|
|
status message. */
|
|
|
|
fflush (stdout);
|
|
|
|
|
2004-07-15 23:00:35 +02:00
|
|
|
sprintf(status,"%X %lu ",(byte)pt->mode,(ulong)pt->timestamp);
|
|
|
|
write_status_text_and_buffer(STATUS_PLAINTEXT,
|
|
|
|
status,pt->name,pt->namelen,0);
|
|
|
|
|
|
|
|
if(!pt->is_partial)
|
|
|
|
{
|
|
|
|
sprintf(status,"%lu",(ulong)pt->len);
|
|
|
|
write_status_text(STATUS_PLAINTEXT_LENGTH,status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
/* create the filename as C string */
|
1998-06-25 12:19:08 +02:00
|
|
|
if( nooutput )
|
|
|
|
;
|
|
|
|
else if( opt.outfile ) {
|
2005-07-27 20:10:56 +02:00
|
|
|
fname = xmalloc( strlen( opt.outfile ) + 1);
|
1997-11-18 15:06:00 +01:00
|
|
|
strcpy(fname, opt.outfile );
|
|
|
|
}
|
1998-09-29 18:15:15 +02:00
|
|
|
else if( pt->namelen == 8 && !memcmp( pt->name, "_CONSOLE", 8 ) ) {
|
|
|
|
log_info(_("data not saved; use option \"--output\" to save it\n"));
|
|
|
|
nooutput = 1;
|
|
|
|
}
|
2006-04-20 04:36:05 +02:00
|
|
|
else if( !opt.flags.use_embedded_filename ) {
|
1999-06-01 16:08:57 +02:00
|
|
|
fname = make_outfile_name( iobuf_get_real_fname(pt->buf) );
|
1999-07-01 12:53:35 +02:00
|
|
|
if( !fname )
|
|
|
|
fname = ask_outfile_name( pt->name, pt->namelen );
|
1999-06-01 16:08:57 +02:00
|
|
|
if( !fname ) {
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_CREATE_FILE;
|
1999-06-01 16:08:57 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2006-04-20 04:36:05 +02:00
|
|
|
else
|
|
|
|
fname=utf8_to_native(pt->name,pt->namelen,0);
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1998-06-25 12:19:08 +02:00
|
|
|
if( nooutput )
|
|
|
|
;
|
2004-11-04 23:28:39 +01:00
|
|
|
else if ( iobuf_is_pipe_filename (fname) || !*fname)
|
|
|
|
{
|
2004-10-13 11:59:46 +02:00
|
|
|
/* No filename or "-" given; write to stdout. */
|
1997-12-20 18:23:29 +01:00
|
|
|
fp = stdout;
|
2002-10-28 14:26:44 +01:00
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
2000-07-28 18:19:07 +02:00
|
|
|
setmode ( fileno(fp) , O_BINARY );
|
2002-10-28 14:26:44 +01:00
|
|
|
#endif
|
2004-11-04 23:28:39 +01:00
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
else {
|
|
|
|
while( !overwrite_filep (fname) ) {
|
|
|
|
char *tmp = ask_outfile_name (NULL, 0);
|
|
|
|
if ( !tmp || !*tmp ) {
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree (tmp);
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_CREATE_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree (fname);
|
2002-06-29 15:46:34 +02:00
|
|
|
fname = tmp;
|
|
|
|
}
|
1998-07-14 19:10:28 +02:00
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2002-10-29 15:37:12 +01:00
|
|
|
#ifndef __riscos__
|
|
|
|
if( fp || nooutput )
|
|
|
|
;
|
2004-10-14 09:11:57 +02:00
|
|
|
else if (is_secured_filename (fname))
|
|
|
|
{
|
|
|
|
errno = EPERM;
|
|
|
|
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
|
|
|
|
rc = G10ERR_CREATE_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
2002-10-29 15:37:12 +01:00
|
|
|
else if( !(fp = fopen(fname,"wb")) ) {
|
|
|
|
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
|
|
|
|
rc = G10ERR_CREATE_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
#else /* __riscos__ */
|
2003-12-28 15:12:16 +01:00
|
|
|
/* If no output filename was given, i.e. we constructed it,
|
|
|
|
convert all '.' in fname to '/' but not vice versa as
|
|
|
|
we don't create directories! */
|
|
|
|
if( !opt.outfile )
|
|
|
|
for( c=0; fname[c]; ++c )
|
|
|
|
if( fname[c] == '.' )
|
|
|
|
fname[c] = '/';
|
2002-10-29 15:37:12 +01:00
|
|
|
|
1998-06-25 12:19:08 +02:00
|
|
|
if( fp || nooutput )
|
1997-11-18 15:06:00 +01:00
|
|
|
;
|
2002-11-25 14:30:34 +01:00
|
|
|
else {
|
|
|
|
fp = fopen(fname,"wb");
|
|
|
|
if( !fp ) {
|
|
|
|
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
|
|
|
|
rc = G10ERR_CREATE_FILE;
|
|
|
|
if (errno == 106)
|
|
|
|
log_info("Do output file and input file have the same name?\n");
|
|
|
|
goto leave;
|
|
|
|
}
|
2002-10-29 15:37:12 +01:00
|
|
|
|
2002-11-25 14:30:34 +01:00
|
|
|
/* If there's a ,xxx extension in the embedded filename,
|
|
|
|
use that, else check whether the user input (in fname)
|
|
|
|
has a ,xxx appended, then use that in preference */
|
|
|
|
if( (c = riscos_get_filetype_from_string( pt->name,
|
|
|
|
pt->namelen )) != -1 )
|
|
|
|
filetype = c;
|
|
|
|
if( (c = riscos_get_filetype_from_string( fname,
|
|
|
|
strlen(fname) )) != -1 )
|
|
|
|
filetype = c;
|
|
|
|
riscos_set_filetype_by_number(fname, filetype);
|
|
|
|
}
|
2002-10-29 15:37:12 +01:00
|
|
|
#endif /* __riscos__ */
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
if( !pt->is_partial ) {
|
2006-03-05 16:13:18 +01:00
|
|
|
/* We have an actual length (which might be zero). */
|
|
|
|
|
|
|
|
if (clearsig) {
|
|
|
|
log_error ("clearsig encountered while not expected\n");
|
|
|
|
rc = G10ERR_UNEXPECTED;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
1999-06-01 16:08:57 +02:00
|
|
|
if( convert ) { /* text mode */
|
1999-05-31 19:49:37 +02:00
|
|
|
for( ; pt->len; pt->len-- ) {
|
|
|
|
if( (c = iobuf_get(pt->buf)) == -1 ) {
|
|
|
|
log_error("Problem reading source (%u bytes remaining)\n",
|
|
|
|
(unsigned)pt->len);
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_READ_FILE;
|
1999-05-31 19:49:37 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if( mfx->md )
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(mfx->md, c );
|
2002-10-28 14:26:44 +01:00
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
2000-07-14 19:34:53 +02:00
|
|
|
if( c == '\r' ) /* convert to native line ending */
|
|
|
|
continue; /* fixme: this hack might be too simple */
|
2002-10-28 14:26:44 +01:00
|
|
|
#endif
|
2004-02-19 00:09:27 +01:00
|
|
|
if( fp )
|
|
|
|
{
|
2004-02-26 03:03:27 +01:00
|
|
|
if(opt.max_output && (++count)>opt.max_output)
|
2004-02-19 00:09:27 +01:00
|
|
|
{
|
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname,"exceeded --max-output limit\n");
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
else if( putc( c, fp ) == EOF )
|
|
|
|
{
|
1999-05-31 19:49:37 +02:00
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
1999-05-31 19:49:37 +02:00
|
|
|
goto leave;
|
2004-02-19 00:09:27 +01:00
|
|
|
}
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
1999-05-31 19:49:37 +02:00
|
|
|
}
|
1999-06-01 16:08:57 +02:00
|
|
|
else { /* binary mode */
|
2005-07-27 20:10:56 +02:00
|
|
|
byte *buffer = xmalloc( 32768 );
|
1999-05-31 19:49:37 +02:00
|
|
|
while( pt->len ) {
|
|
|
|
int len = pt->len > 32768 ? 32768 : pt->len;
|
|
|
|
len = iobuf_read( pt->buf, buffer, len );
|
|
|
|
if( len == -1 ) {
|
|
|
|
log_error("Problem reading source (%u bytes remaining)\n",
|
|
|
|
(unsigned)pt->len);
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_READ_FILE;
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree( buffer );
|
1998-06-25 12:19:08 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
1999-05-31 19:49:37 +02:00
|
|
|
if( mfx->md )
|
2002-06-29 15:46:34 +02:00
|
|
|
md_write( mfx->md, buffer, len );
|
2004-02-19 00:09:27 +01:00
|
|
|
if( fp )
|
|
|
|
{
|
2004-02-22 05:16:31 +01:00
|
|
|
if(opt.max_output && (count+=len)>opt.max_output)
|
2004-02-19 00:09:27 +01:00
|
|
|
{
|
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname,"exceeded --max-output limit\n");
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree( buffer );
|
2004-02-19 00:09:27 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
else if( fwrite( buffer, 1, len, fp ) != len )
|
|
|
|
{
|
1999-05-31 19:49:37 +02:00
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree( buffer );
|
1999-05-31 19:49:37 +02:00
|
|
|
goto leave;
|
2004-02-19 00:09:27 +01:00
|
|
|
}
|
|
|
|
}
|
1999-05-31 19:49:37 +02:00
|
|
|
pt->len -= len;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree( buffer );
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
1999-01-20 19:10:35 +01:00
|
|
|
else if( !clearsig ) {
|
1999-06-01 16:08:57 +02:00
|
|
|
if( convert ) { /* text mode */
|
1999-05-31 19:49:37 +02:00
|
|
|
while( (c = iobuf_get(pt->buf)) != -1 ) {
|
|
|
|
if( mfx->md )
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(mfx->md, c );
|
2002-10-28 14:26:44 +01:00
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
1999-05-31 19:49:37 +02:00
|
|
|
if( convert && c == '\r' )
|
|
|
|
continue; /* fixme: this hack might be too simple */
|
2002-10-28 14:26:44 +01:00
|
|
|
#endif
|
2004-02-19 00:09:27 +01:00
|
|
|
if( fp )
|
|
|
|
{
|
2004-02-26 03:03:27 +01:00
|
|
|
if(opt.max_output && (++count)>opt.max_output)
|
2004-02-19 00:09:27 +01:00
|
|
|
{
|
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname,"exceeded --max-output limit\n");
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
else if( putc( c, fp ) == EOF )
|
|
|
|
{
|
1999-05-31 19:49:37 +02:00
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
1999-05-31 19:49:37 +02:00
|
|
|
goto leave;
|
2004-02-19 00:09:27 +01:00
|
|
|
}
|
|
|
|
}
|
1999-05-31 19:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
1999-06-01 16:08:57 +02:00
|
|
|
else { /* binary mode */
|
2005-07-27 20:10:56 +02:00
|
|
|
byte *buffer = xmalloc( 32768 );
|
1999-09-03 10:15:32 +02:00
|
|
|
int eof;
|
|
|
|
for( eof=0; !eof; ) {
|
|
|
|
/* Why do we check for len < 32768:
|
2000-07-14 19:34:53 +02:00
|
|
|
* If we won't, we would practically read 2 EOFs but
|
1999-09-03 10:15:32 +02:00
|
|
|
* the first one has already popped the block_filter
|
|
|
|
* off and therefore we don't catch the boundary.
|
2000-07-14 19:34:53 +02:00
|
|
|
* So, always assume EOF if iobuf_read returns less bytes
|
1999-09-03 10:15:32 +02:00
|
|
|
* then requested */
|
1999-05-31 19:49:37 +02:00
|
|
|
int len = iobuf_read( pt->buf, buffer, 32768 );
|
|
|
|
if( len == -1 )
|
|
|
|
break;
|
1999-09-03 10:15:32 +02:00
|
|
|
if( len < 32768 )
|
|
|
|
eof = 1;
|
1999-05-31 19:49:37 +02:00
|
|
|
if( mfx->md )
|
2002-06-29 15:46:34 +02:00
|
|
|
md_write( mfx->md, buffer, len );
|
2004-02-19 00:09:27 +01:00
|
|
|
if( fp )
|
|
|
|
{
|
2004-02-22 05:16:31 +01:00
|
|
|
if(opt.max_output && (count+=len)>opt.max_output)
|
2004-02-19 00:09:27 +01:00
|
|
|
{
|
1999-05-31 19:49:37 +02:00
|
|
|
log_error("Error writing to `%s': %s\n",
|
2004-02-19 00:09:27 +01:00
|
|
|
fname,"exceeded --max-output limit\n");
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree( buffer );
|
1999-05-31 19:49:37 +02:00
|
|
|
goto leave;
|
2004-02-19 00:09:27 +01:00
|
|
|
}
|
|
|
|
else if( fwrite( buffer, 1, len, fp ) != len ) {
|
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree( buffer );
|
2004-02-19 00:09:27 +01:00
|
|
|
goto leave;
|
1999-05-31 19:49:37 +02:00
|
|
|
}
|
2004-02-19 00:09:27 +01:00
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree( buffer );
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
1999-01-07 18:05:48 +01:00
|
|
|
pt->buf = NULL;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
1999-01-20 19:10:35 +01:00
|
|
|
else { /* clear text signature - don't hash the last cr,lf */
|
|
|
|
int state = 0;
|
|
|
|
|
|
|
|
while( (c = iobuf_get(pt->buf)) != -1 ) {
|
2004-02-19 00:09:27 +01:00
|
|
|
if( fp )
|
|
|
|
{
|
2004-02-26 03:03:27 +01:00
|
|
|
if(opt.max_output && (++count)>opt.max_output)
|
2004-02-19 00:09:27 +01:00
|
|
|
{
|
1999-01-20 19:10:35 +01:00
|
|
|
log_error("Error writing to `%s': %s\n",
|
2004-02-19 00:09:27 +01:00
|
|
|
fname,"exceeded --max-output limit\n");
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
1999-01-20 19:10:35 +01:00
|
|
|
goto leave;
|
2004-02-19 00:09:27 +01:00
|
|
|
}
|
|
|
|
else if( putc( c, fp ) == EOF )
|
|
|
|
{
|
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
1999-01-20 19:10:35 +01:00
|
|
|
if( !mfx->md )
|
|
|
|
continue;
|
|
|
|
if( state == 2 ) {
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(mfx->md, '\r' );
|
|
|
|
md_putc(mfx->md, '\n' );
|
1999-01-20 19:10:35 +01:00
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
if( !state ) {
|
|
|
|
if( c == '\r' )
|
|
|
|
state = 1;
|
2000-07-14 19:34:53 +02:00
|
|
|
else if( c == '\n' )
|
|
|
|
state = 2;
|
1999-01-20 19:10:35 +01:00
|
|
|
else
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(mfx->md, c );
|
1999-01-20 19:10:35 +01:00
|
|
|
}
|
|
|
|
else if( state == 1 ) {
|
|
|
|
if( c == '\n' )
|
|
|
|
state = 2;
|
|
|
|
else {
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(mfx->md, '\r' );
|
1999-01-20 19:10:35 +01:00
|
|
|
if( c == '\r' )
|
|
|
|
state = 1;
|
|
|
|
else {
|
|
|
|
state = 0;
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(mfx->md, c );
|
1999-01-20 19:10:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pt->buf = NULL;
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
if( fp && fp != stdout && fclose(fp) ) {
|
1998-12-29 14:47:31 +01:00
|
|
|
log_error("Error closing `%s': %s\n", fname, strerror(errno) );
|
1997-11-18 15:06:00 +01:00
|
|
|
fp = NULL;
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
1997-11-18 15:06:00 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
fp = NULL;
|
|
|
|
|
|
|
|
leave:
|
2007-03-08 13:13:15 +01:00
|
|
|
/* Make sure that stdout gets flushed after the plaintext has
|
|
|
|
been handled. This is for extra security as we do a
|
|
|
|
flush anyway before checking the signature. */
|
2010-03-26 17:57:09 +01:00
|
|
|
if(fflush(stdout))
|
|
|
|
{
|
|
|
|
/* We check whether this fflush succeeded since fp might have
|
|
|
|
been stdout piping to a file. The fflush could fail if the
|
|
|
|
file cannot be written (disk full, etc). See bug 1207 for
|
|
|
|
more. */
|
|
|
|
log_error("Error flushing plaintext: %s\n",strerror(errno));
|
|
|
|
rc=G10ERR_WRITE_FILE;
|
|
|
|
}
|
2007-03-08 13:13:15 +01:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
if( fp && fp != stdout )
|
|
|
|
fclose(fp);
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(fname);
|
1997-11-18 15:06:00 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-09-03 10:15:32 +02:00
|
|
|
static void
|
2002-06-29 15:46:34 +02:00
|
|
|
do_hash( MD_HANDLE md, MD_HANDLE md2, IOBUF fp, int textmode )
|
1999-09-03 10:15:32 +02:00
|
|
|
{
|
|
|
|
text_filter_context_t tfx;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if( textmode ) {
|
|
|
|
memset( &tfx, 0, sizeof tfx);
|
|
|
|
iobuf_push_filter( fp, text_filter, &tfx );
|
|
|
|
}
|
|
|
|
if( md2 ) { /* work around a strange behaviour in pgp2 */
|
|
|
|
/* It seems that at least PGP5 converts a single CR to a CR,LF too */
|
|
|
|
int lc = -1;
|
|
|
|
while( (c = iobuf_get(fp)) != -1 ) {
|
|
|
|
if( c == '\n' && lc == '\r' )
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(md2, c);
|
1999-09-03 10:15:32 +02:00
|
|
|
else if( c == '\n' ) {
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(md2, '\r');
|
|
|
|
md_putc(md2, c);
|
1999-09-03 10:15:32 +02:00
|
|
|
}
|
|
|
|
else if( c != '\n' && lc == '\r' ) {
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(md2, '\n');
|
|
|
|
md_putc(md2, c);
|
1999-09-03 10:15:32 +02:00
|
|
|
}
|
|
|
|
else
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(md2, c);
|
1999-09-03 10:15:32 +02:00
|
|
|
|
|
|
|
if( md )
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(md, c );
|
1999-09-03 10:15:32 +02:00
|
|
|
lc = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while( (c = iobuf_get(fp)) != -1 ) {
|
|
|
|
if( md )
|
2002-06-29 15:46:34 +02:00
|
|
|
md_putc(md, c );
|
1999-09-03 10:15:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-12-03 11:20:03 +01:00
|
|
|
|
|
|
|
/****************
|
|
|
|
* Ask for the detached datafile and calculate the digest from it.
|
1997-12-23 18:30:18 +01:00
|
|
|
* INFILE is the name of the input file.
|
1997-12-03 11:20:03 +01:00
|
|
|
*/
|
|
|
|
int
|
2002-06-29 15:46:34 +02:00
|
|
|
ask_for_detached_datafile( MD_HANDLE md, MD_HANDLE md2,
|
1999-08-31 17:30:12 +02:00
|
|
|
const char *inname, int textmode )
|
1997-12-03 11:20:03 +01:00
|
|
|
{
|
2003-04-15 17:46:13 +02:00
|
|
|
progress_filter_context_t pfx;
|
1997-12-23 18:30:18 +01:00
|
|
|
char *answer = NULL;
|
|
|
|
IOBUF fp;
|
1997-12-03 11:20:03 +01:00
|
|
|
int rc = 0;
|
|
|
|
|
2003-04-15 17:46:13 +02:00
|
|
|
fp = open_sigfile( inname, &pfx ); /* open default file */
|
|
|
|
|
1998-02-12 00:22:09 +01:00
|
|
|
if( !fp && !opt.batch ) {
|
1997-12-23 18:30:18 +01:00
|
|
|
int any=0;
|
1999-10-26 14:14:37 +02:00
|
|
|
tty_printf(_("Detached signature.\n"));
|
1997-12-23 18:30:18 +01:00
|
|
|
do {
|
2005-09-20 05:34:32 +02:00
|
|
|
char *name;
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(answer);
|
2005-03-21 21:47:28 +01:00
|
|
|
tty_enable_completion(NULL);
|
2005-09-20 05:34:32 +02:00
|
|
|
name = cpr_get("detached_signature.filename",
|
1998-11-27 12:42:49 +01:00
|
|
|
_("Please enter name of data file: "));
|
2005-03-21 21:47:28 +01:00
|
|
|
tty_disable_completion();
|
1998-08-08 21:27:00 +02:00
|
|
|
cpr_kill_prompt();
|
2005-09-20 05:34:32 +02:00
|
|
|
answer=make_filename(name,(void *)NULL);
|
|
|
|
xfree(name);
|
|
|
|
|
1997-12-23 18:30:18 +01:00
|
|
|
if( any && !*answer ) {
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_READ_FILE;
|
1997-12-23 18:30:18 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
fp = iobuf_open(answer);
|
2004-10-13 20:10:06 +02:00
|
|
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
|
|
|
{
|
|
|
|
iobuf_close (fp);
|
|
|
|
fp = NULL;
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
1997-12-23 18:30:18 +01:00
|
|
|
if( !fp && errno == ENOENT ) {
|
|
|
|
tty_printf("No such file, try again or hit enter to quit.\n");
|
|
|
|
any++;
|
|
|
|
}
|
2004-10-28 05:57:30 +02:00
|
|
|
else if( !fp )
|
|
|
|
{
|
|
|
|
log_error(_("can't open `%s': %s\n"), answer, strerror(errno));
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_READ_FILE;
|
1997-12-23 18:30:18 +01:00
|
|
|
goto leave;
|
2004-10-28 05:57:30 +02:00
|
|
|
}
|
1997-12-23 18:30:18 +01:00
|
|
|
} while( !fp );
|
1997-12-03 11:20:03 +01:00
|
|
|
}
|
|
|
|
|
1998-02-12 00:22:09 +01:00
|
|
|
if( !fp ) {
|
1998-02-26 17:56:31 +01:00
|
|
|
if( opt.verbose )
|
1998-11-10 13:59:59 +01:00
|
|
|
log_info(_("reading stdin ...\n"));
|
1999-08-31 17:30:12 +02:00
|
|
|
fp = iobuf_open( NULL );
|
|
|
|
assert(fp);
|
1997-12-03 11:20:03 +01:00
|
|
|
}
|
1999-08-31 17:30:12 +02:00
|
|
|
do_hash( md, md2, fp, textmode );
|
|
|
|
iobuf_close(fp);
|
1997-12-03 11:20:03 +01:00
|
|
|
|
|
|
|
leave:
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(answer);
|
1997-12-03 11:20:03 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-07-14 19:10:28 +02:00
|
|
|
|
1998-03-09 22:44:06 +01:00
|
|
|
/****************
|
|
|
|
* Hash the given files and append the hash to hash context md.
|
|
|
|
* If FILES is NULL, hash stdin.
|
|
|
|
*/
|
|
|
|
int
|
2002-06-29 15:46:34 +02:00
|
|
|
hash_datafiles( MD_HANDLE md, MD_HANDLE md2, STRLIST files,
|
1998-07-14 19:10:28 +02:00
|
|
|
const char *sigfilename, int textmode )
|
1998-03-09 22:44:06 +01:00
|
|
|
{
|
2003-04-15 17:46:13 +02:00
|
|
|
progress_filter_context_t pfx;
|
1998-03-09 22:44:06 +01:00
|
|
|
IOBUF fp;
|
2002-06-29 15:46:34 +02:00
|
|
|
STRLIST sl;
|
1998-03-09 22:44:06 +01:00
|
|
|
|
1998-07-14 19:10:28 +02:00
|
|
|
if( !files ) {
|
2014-11-14 09:36:19 +01:00
|
|
|
/* Check whether we can open the signed material. We avoid
|
|
|
|
trying to open a file if run in batch mode. This assumed
|
|
|
|
data file for a sig file feature is just a convenience thing
|
|
|
|
for the command line and the user needs to read possible
|
|
|
|
warning messages. */
|
|
|
|
if (!opt.batch) {
|
|
|
|
fp = open_sigfile( sigfilename, &pfx );
|
|
|
|
if( fp ) {
|
|
|
|
do_hash( md, md2, fp, textmode );
|
|
|
|
iobuf_close(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
log_error (_("no signed data\n"));
|
|
|
|
return G10ERR_OPEN_FILE;
|
1998-07-14 19:10:28 +02:00
|
|
|
}
|
1998-03-09 22:44:06 +01:00
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
for (sl=files; sl; sl = sl->next ) {
|
1998-03-09 22:44:06 +01:00
|
|
|
fp = iobuf_open( sl->d );
|
2004-10-13 20:10:06 +02:00
|
|
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
|
|
|
{
|
|
|
|
iobuf_close (fp);
|
|
|
|
fp = NULL;
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
1998-03-09 22:44:06 +01:00
|
|
|
if( !fp ) {
|
1998-12-29 14:47:31 +01:00
|
|
|
log_error(_("can't open signed data `%s'\n"),
|
1998-03-09 22:44:06 +01:00
|
|
|
print_fname_stdin(sl->d));
|
2002-06-29 15:46:34 +02:00
|
|
|
return G10ERR_OPEN_FILE;
|
1998-03-09 22:44:06 +01:00
|
|
|
}
|
2003-04-15 17:46:13 +02:00
|
|
|
handle_progress (&pfx, fp, sl->d);
|
1999-05-19 16:12:26 +02:00
|
|
|
do_hash( md, md2, fp, textmode );
|
1998-03-09 22:44:06 +01:00
|
|
|
iobuf_close(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-04-20 04:36:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Set up a plaintext packet with the appropriate filename. If there
|
|
|
|
is a --set-filename, use it (it's already UTF8). If there is a
|
|
|
|
regular filename, UTF8-ize it if necessary. If there is no
|
|
|
|
filenames at all, set the field empty. */
|
|
|
|
|
|
|
|
PKT_plaintext *
|
|
|
|
setup_plaintext_name(const char *filename,IOBUF iobuf)
|
|
|
|
{
|
|
|
|
PKT_plaintext *pt;
|
|
|
|
|
|
|
|
if(filename || opt.set_filename)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if(opt.set_filename)
|
|
|
|
s=make_basename(opt.set_filename,iobuf_get_real_fname(iobuf));
|
|
|
|
else if(filename && !opt.flags.utf8_filename)
|
|
|
|
{
|
|
|
|
char *tmp=native_to_utf8(filename);
|
|
|
|
s=make_basename(tmp,iobuf_get_real_fname(iobuf));
|
|
|
|
xfree(tmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s=make_basename(filename,iobuf_get_real_fname(iobuf));
|
|
|
|
|
|
|
|
pt = xmalloc (sizeof *pt + strlen(s) - 1);
|
|
|
|
pt->namelen = strlen (s);
|
|
|
|
memcpy (pt->name, s, pt->namelen);
|
|
|
|
xfree (s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no filename */
|
|
|
|
pt = xmalloc (sizeof *pt - 1);
|
|
|
|
pt->namelen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pt;
|
|
|
|
}
|