2002-06-29 13:46:34 +00:00
|
|
|
/* plaintext.c - process plaintext packets
|
2004-02-24 23:37:18 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
|
|
|
|
* 2004 Free Software Foundation, Inc.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-18 14:06:00 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-18 14:06:00 +00: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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
1999-01-07 17:05:48 +00:00
|
|
|
#include <assert.h>
|
2004-02-18 23:09:27 +00:00
|
|
|
#include <sys/types.h>
|
2000-07-28 16:19:07 +00: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 21:50:33 +00:00
|
|
|
#include <fcntl.h> /* for setmode() */
|
2000-07-28 16:19:07 +00:00
|
|
|
#endif
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
#include "util.h"
|
2002-06-29 13:46:34 +00:00
|
|
|
#include "memory.h"
|
1997-11-18 14:06:00 +00:00
|
|
|
#include "options.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "ttyio.h"
|
1997-11-24 11:04:11 +00:00
|
|
|
#include "filter.h"
|
1997-12-23 17:30:18 +00:00
|
|
|
#include "main.h"
|
1998-08-08 19:27:00 +00:00
|
|
|
#include "status.h"
|
1998-03-09 21:44:06 +00:00
|
|
|
#include "i18n.h"
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/****************
|
1997-11-24 11:04:11 +00: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 14:06:00 +00:00
|
|
|
*/
|
|
|
|
int
|
1998-07-08 09:29:43 +00:00
|
|
|
handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
|
|
|
|
int nooutput, int clearsig )
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
1998-06-25 10:19:08 +00:00
|
|
|
char *fname = NULL;
|
1997-11-18 14:06:00 +00:00
|
|
|
FILE *fp = NULL;
|
2004-02-26 02:03:27 +00:00
|
|
|
static off_t count=0;
|
1997-11-18 14:06:00 +00:00
|
|
|
int rc = 0;
|
|
|
|
int c;
|
2004-04-16 16:31:19 +00:00
|
|
|
int convert = (pt->mode == 't' || pt->mode == 'u');
|
2002-10-28 13:26:44 +00:00
|
|
|
#ifdef __riscos__
|
|
|
|
int filetype = 0xfff;
|
|
|
|
#endif
|
1997-11-18 14:06:00 +00:00
|
|
|
|
2004-07-15 21:00:35 +00: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 13:28:04 +00:00
|
|
|
char status[50];
|
2004-07-15 21:00:35 +00: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 14:06:00 +00:00
|
|
|
/* create the filename as C string */
|
1998-06-25 10:19:08 +00:00
|
|
|
if( nooutput )
|
|
|
|
;
|
|
|
|
else if( opt.outfile ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
fname = m_alloc( strlen( opt.outfile ) + 1);
|
1997-11-18 14:06:00 +00:00
|
|
|
strcpy(fname, opt.outfile );
|
|
|
|
}
|
1998-09-29 16:15:15 +00: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;
|
|
|
|
}
|
1999-06-01 14:08:57 +00:00
|
|
|
else if( !opt.use_embedded_filename ) {
|
|
|
|
fname = make_outfile_name( iobuf_get_real_fname(pt->buf) );
|
1999-07-01 10:53:35 +00:00
|
|
|
if( !fname )
|
|
|
|
fname = ask_outfile_name( pt->name, pt->namelen );
|
1999-06-01 14:08:57 +00:00
|
|
|
if( !fname ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_CREATE_FILE;
|
1999-06-01 14:08:57 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
else {
|
1999-07-01 10:53:35 +00:00
|
|
|
fname = make_printable_string( pt->name, pt->namelen, 0 );
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
1998-06-25 10:19:08 +00:00
|
|
|
if( nooutput )
|
|
|
|
;
|
2004-10-13 09:59:46 +00:00
|
|
|
else if ( iobuf_is_pipe_filename (fname) ) {
|
|
|
|
/* No filename or "-" given; write to stdout. */
|
1997-12-20 17:23:29 +00:00
|
|
|
fp = stdout;
|
2002-10-28 13:26:44 +00:00
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
2000-07-28 16:19:07 +00:00
|
|
|
setmode ( fileno(fp) , O_BINARY );
|
2002-10-28 13:26:44 +00:00
|
|
|
#endif
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
else {
|
|
|
|
while( !overwrite_filep (fname) ) {
|
|
|
|
char *tmp = ask_outfile_name (NULL, 0);
|
|
|
|
if ( !tmp || !*tmp ) {
|
|
|
|
m_free (tmp);
|
|
|
|
rc = G10ERR_CREATE_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
m_free (fname);
|
|
|
|
fname = tmp;
|
|
|
|
}
|
1998-07-14 17:10:28 +00:00
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
|
2002-10-29 14:37:12 +00:00
|
|
|
#ifndef __riscos__
|
|
|
|
if( fp || nooutput )
|
|
|
|
;
|
|
|
|
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 14:12:16 +00: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 14:37:12 +00:00
|
|
|
|
1998-06-25 10:19:08 +00:00
|
|
|
if( fp || nooutput )
|
1997-11-18 14:06:00 +00:00
|
|
|
;
|
2002-11-25 13:30:34 +00: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 14:37:12 +00:00
|
|
|
|
2002-11-25 13:30:34 +00: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 14:37:12 +00:00
|
|
|
#endif /* __riscos__ */
|
1997-11-18 14:06:00 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
if( !pt->is_partial ) {
|
|
|
|
/* we have an actual length (which might be zero). */
|
1999-01-20 18:10:35 +00:00
|
|
|
assert( !clearsig );
|
1999-06-01 14:08:57 +00:00
|
|
|
if( convert ) { /* text mode */
|
1999-05-31 17:49:37 +00: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 13:46:34 +00:00
|
|
|
rc = G10ERR_READ_FILE;
|
1999-05-31 17:49:37 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if( mfx->md )
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(mfx->md, c );
|
2002-10-28 13:26:44 +00:00
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
2000-07-14 17:34:53 +00:00
|
|
|
if( c == '\r' ) /* convert to native line ending */
|
|
|
|
continue; /* fixme: this hack might be too simple */
|
2002-10-28 13:26:44 +00:00
|
|
|
#endif
|
2004-02-18 23:09:27 +00:00
|
|
|
if( fp )
|
|
|
|
{
|
2004-02-26 02:03:27 +00:00
|
|
|
if(opt.max_output && (++count)>opt.max_output)
|
2004-02-18 23:09:27 +00: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 17:49:37 +00:00
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
1999-05-31 17:49:37 +00:00
|
|
|
goto leave;
|
2004-02-18 23:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
1999-05-31 17:49:37 +00:00
|
|
|
}
|
1999-06-01 14:08:57 +00:00
|
|
|
else { /* binary mode */
|
2002-06-29 13:46:34 +00:00
|
|
|
byte *buffer = m_alloc( 32768 );
|
1999-05-31 17:49:37 +00: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 13:46:34 +00:00
|
|
|
rc = G10ERR_READ_FILE;
|
|
|
|
m_free( buffer );
|
1998-06-25 10:19:08 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
1999-05-31 17:49:37 +00:00
|
|
|
if( mfx->md )
|
2002-06-29 13:46:34 +00:00
|
|
|
md_write( mfx->md, buffer, len );
|
2004-02-18 23:09:27 +00:00
|
|
|
if( fp )
|
|
|
|
{
|
2004-02-22 04:16:31 +00:00
|
|
|
if(opt.max_output && (count+=len)>opt.max_output)
|
2004-02-18 23:09:27 +00:00
|
|
|
{
|
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname,"exceeded --max-output limit\n");
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
m_free( buffer );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
else if( fwrite( buffer, 1, len, fp ) != len )
|
|
|
|
{
|
1999-05-31 17:49:37 +00:00
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
m_free( buffer );
|
1999-05-31 17:49:37 +00:00
|
|
|
goto leave;
|
2004-02-18 23:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
1999-05-31 17:49:37 +00:00
|
|
|
pt->len -= len;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free( buffer );
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
1999-01-20 18:10:35 +00:00
|
|
|
else if( !clearsig ) {
|
1999-06-01 14:08:57 +00:00
|
|
|
if( convert ) { /* text mode */
|
1999-05-31 17:49:37 +00:00
|
|
|
while( (c = iobuf_get(pt->buf)) != -1 ) {
|
|
|
|
if( mfx->md )
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(mfx->md, c );
|
2002-10-28 13:26:44 +00:00
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
1999-05-31 17:49:37 +00:00
|
|
|
if( convert && c == '\r' )
|
|
|
|
continue; /* fixme: this hack might be too simple */
|
2002-10-28 13:26:44 +00:00
|
|
|
#endif
|
2004-02-18 23:09:27 +00:00
|
|
|
if( fp )
|
|
|
|
{
|
2004-02-26 02:03:27 +00:00
|
|
|
if(opt.max_output && (++count)>opt.max_output)
|
2004-02-18 23:09:27 +00: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 17:49:37 +00:00
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
1999-05-31 17:49:37 +00:00
|
|
|
goto leave;
|
2004-02-18 23:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
1999-05-31 17:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
1999-06-01 14:08:57 +00:00
|
|
|
else { /* binary mode */
|
2002-06-29 13:46:34 +00:00
|
|
|
byte *buffer = m_alloc( 32768 );
|
1999-09-03 08:15:32 +00:00
|
|
|
int eof;
|
|
|
|
for( eof=0; !eof; ) {
|
|
|
|
/* Why do we check for len < 32768:
|
2000-07-14 17:34:53 +00:00
|
|
|
* If we won't, we would practically read 2 EOFs but
|
1999-09-03 08:15:32 +00:00
|
|
|
* the first one has already popped the block_filter
|
|
|
|
* off and therefore we don't catch the boundary.
|
2000-07-14 17:34:53 +00:00
|
|
|
* So, always assume EOF if iobuf_read returns less bytes
|
1999-09-03 08:15:32 +00:00
|
|
|
* then requested */
|
1999-05-31 17:49:37 +00:00
|
|
|
int len = iobuf_read( pt->buf, buffer, 32768 );
|
|
|
|
if( len == -1 )
|
|
|
|
break;
|
1999-09-03 08:15:32 +00:00
|
|
|
if( len < 32768 )
|
|
|
|
eof = 1;
|
1999-05-31 17:49:37 +00:00
|
|
|
if( mfx->md )
|
2002-06-29 13:46:34 +00:00
|
|
|
md_write( mfx->md, buffer, len );
|
2004-02-18 23:09:27 +00:00
|
|
|
if( fp )
|
|
|
|
{
|
2004-02-22 04:16:31 +00:00
|
|
|
if(opt.max_output && (count+=len)>opt.max_output)
|
2004-02-18 23:09:27 +00:00
|
|
|
{
|
1999-05-31 17:49:37 +00:00
|
|
|
log_error("Error writing to `%s': %s\n",
|
2004-02-18 23:09:27 +00:00
|
|
|
fname,"exceeded --max-output limit\n");
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
m_free( buffer );
|
1999-05-31 17:49:37 +00:00
|
|
|
goto leave;
|
2004-02-18 23:09:27 +00:00
|
|
|
}
|
|
|
|
else if( fwrite( buffer, 1, len, fp ) != len ) {
|
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
m_free( buffer );
|
|
|
|
goto leave;
|
1999-05-31 17:49:37 +00:00
|
|
|
}
|
2004-02-18 23:09:27 +00:00
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free( buffer );
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
1999-01-07 17:05:48 +00:00
|
|
|
pt->buf = NULL;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
1999-01-20 18:10:35 +00:00
|
|
|
else { /* clear text signature - don't hash the last cr,lf */
|
|
|
|
int state = 0;
|
|
|
|
|
|
|
|
while( (c = iobuf_get(pt->buf)) != -1 ) {
|
2004-02-18 23:09:27 +00:00
|
|
|
if( fp )
|
|
|
|
{
|
2004-02-26 02:03:27 +00:00
|
|
|
if(opt.max_output && (++count)>opt.max_output)
|
2004-02-18 23:09:27 +00:00
|
|
|
{
|
1999-01-20 18:10:35 +00:00
|
|
|
log_error("Error writing to `%s': %s\n",
|
2004-02-18 23:09:27 +00:00
|
|
|
fname,"exceeded --max-output limit\n");
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
1999-01-20 18:10:35 +00:00
|
|
|
goto leave;
|
2004-02-18 23:09:27 +00: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 18:10:35 +00:00
|
|
|
if( !mfx->md )
|
|
|
|
continue;
|
|
|
|
if( state == 2 ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(mfx->md, '\r' );
|
|
|
|
md_putc(mfx->md, '\n' );
|
1999-01-20 18:10:35 +00:00
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
if( !state ) {
|
|
|
|
if( c == '\r' )
|
|
|
|
state = 1;
|
2000-07-14 17:34:53 +00:00
|
|
|
else if( c == '\n' )
|
|
|
|
state = 2;
|
1999-01-20 18:10:35 +00:00
|
|
|
else
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(mfx->md, c );
|
1999-01-20 18:10:35 +00:00
|
|
|
}
|
|
|
|
else if( state == 1 ) {
|
|
|
|
if( c == '\n' )
|
|
|
|
state = 2;
|
|
|
|
else {
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(mfx->md, '\r' );
|
1999-01-20 18:10:35 +00:00
|
|
|
if( c == '\r' )
|
|
|
|
state = 1;
|
|
|
|
else {
|
|
|
|
state = 0;
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(mfx->md, c );
|
1999-01-20 18:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pt->buf = NULL;
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
if( fp && fp != stdout && fclose(fp) ) {
|
1998-12-29 13:47:31 +00:00
|
|
|
log_error("Error closing `%s': %s\n", fname, strerror(errno) );
|
1997-11-18 14:06:00 +00:00
|
|
|
fp = NULL;
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
1997-11-18 14:06:00 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
fp = NULL;
|
|
|
|
|
|
|
|
leave:
|
|
|
|
if( fp && fp != stdout )
|
|
|
|
fclose(fp);
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free(fname);
|
1997-11-18 14:06:00 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-09-03 08:15:32 +00:00
|
|
|
static void
|
2002-06-29 13:46:34 +00:00
|
|
|
do_hash( MD_HANDLE md, MD_HANDLE md2, IOBUF fp, int textmode )
|
1999-09-03 08:15:32 +00: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 13:46:34 +00:00
|
|
|
md_putc(md2, c);
|
1999-09-03 08:15:32 +00:00
|
|
|
else if( c == '\n' ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(md2, '\r');
|
|
|
|
md_putc(md2, c);
|
1999-09-03 08:15:32 +00:00
|
|
|
}
|
|
|
|
else if( c != '\n' && lc == '\r' ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(md2, '\n');
|
|
|
|
md_putc(md2, c);
|
1999-09-03 08:15:32 +00:00
|
|
|
}
|
|
|
|
else
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(md2, c);
|
1999-09-03 08:15:32 +00:00
|
|
|
|
|
|
|
if( md )
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(md, c );
|
1999-09-03 08:15:32 +00:00
|
|
|
lc = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while( (c = iobuf_get(fp)) != -1 ) {
|
|
|
|
if( md )
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(md, c );
|
1999-09-03 08:15:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-12-03 10:20:03 +00:00
|
|
|
|
|
|
|
/****************
|
|
|
|
* Ask for the detached datafile and calculate the digest from it.
|
1997-12-23 17:30:18 +00:00
|
|
|
* INFILE is the name of the input file.
|
1997-12-03 10:20:03 +00:00
|
|
|
*/
|
|
|
|
int
|
2002-06-29 13:46:34 +00:00
|
|
|
ask_for_detached_datafile( MD_HANDLE md, MD_HANDLE md2,
|
1999-08-31 15:30:12 +00:00
|
|
|
const char *inname, int textmode )
|
1997-12-03 10:20:03 +00:00
|
|
|
{
|
2003-04-15 15:46:13 +00:00
|
|
|
progress_filter_context_t pfx;
|
1997-12-23 17:30:18 +00:00
|
|
|
char *answer = NULL;
|
|
|
|
IOBUF fp;
|
1997-12-03 10:20:03 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
2003-04-15 15:46:13 +00:00
|
|
|
fp = open_sigfile( inname, &pfx ); /* open default file */
|
|
|
|
|
1998-02-11 23:22:09 +00:00
|
|
|
if( !fp && !opt.batch ) {
|
1997-12-23 17:30:18 +00:00
|
|
|
int any=0;
|
1999-10-26 12:14:37 +00:00
|
|
|
tty_printf(_("Detached signature.\n"));
|
1997-12-23 17:30:18 +00:00
|
|
|
do {
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free(answer);
|
1998-11-27 11:42:49 +00:00
|
|
|
answer = cpr_get("detached_signature.filename",
|
|
|
|
_("Please enter name of data file: "));
|
1998-08-08 19:27:00 +00:00
|
|
|
cpr_kill_prompt();
|
1997-12-23 17:30:18 +00:00
|
|
|
if( any && !*answer ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_READ_FILE;
|
1997-12-23 17:30:18 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
fp = iobuf_open(answer);
|
2004-10-13 18:10:06 +00:00
|
|
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
|
|
|
{
|
|
|
|
iobuf_close (fp);
|
|
|
|
fp = NULL;
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
1997-12-23 17:30:18 +00:00
|
|
|
if( !fp && errno == ENOENT ) {
|
|
|
|
tty_printf("No such file, try again or hit enter to quit.\n");
|
|
|
|
any++;
|
|
|
|
}
|
|
|
|
else if( !fp ) {
|
1998-12-29 13:47:31 +00:00
|
|
|
log_error("can't open `%s': %s\n", answer, strerror(errno) );
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_READ_FILE;
|
1997-12-23 17:30:18 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
} while( !fp );
|
1997-12-03 10:20:03 +00:00
|
|
|
}
|
|
|
|
|
1998-02-11 23:22:09 +00:00
|
|
|
if( !fp ) {
|
1998-02-26 16:56:31 +00:00
|
|
|
if( opt.verbose )
|
1998-11-10 12:59:59 +00:00
|
|
|
log_info(_("reading stdin ...\n"));
|
1999-08-31 15:30:12 +00:00
|
|
|
fp = iobuf_open( NULL );
|
|
|
|
assert(fp);
|
1997-12-03 10:20:03 +00:00
|
|
|
}
|
1999-08-31 15:30:12 +00:00
|
|
|
do_hash( md, md2, fp, textmode );
|
|
|
|
iobuf_close(fp);
|
1997-12-03 10:20:03 +00:00
|
|
|
|
|
|
|
leave:
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free(answer);
|
1997-12-03 10:20:03 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-07-14 17:10:28 +00:00
|
|
|
|
1998-03-09 21:44:06 +00:00
|
|
|
/****************
|
|
|
|
* Hash the given files and append the hash to hash context md.
|
|
|
|
* If FILES is NULL, hash stdin.
|
|
|
|
*/
|
|
|
|
int
|
2002-06-29 13:46:34 +00:00
|
|
|
hash_datafiles( MD_HANDLE md, MD_HANDLE md2, STRLIST files,
|
1998-07-14 17:10:28 +00:00
|
|
|
const char *sigfilename, int textmode )
|
1998-03-09 21:44:06 +00:00
|
|
|
{
|
2003-04-15 15:46:13 +00:00
|
|
|
progress_filter_context_t pfx;
|
1998-03-09 21:44:06 +00:00
|
|
|
IOBUF fp;
|
2002-06-29 13:46:34 +00:00
|
|
|
STRLIST sl;
|
1998-03-09 21:44:06 +00:00
|
|
|
|
1998-07-14 17:10:28 +00:00
|
|
|
if( !files ) {
|
1999-01-16 08:29:29 +00:00
|
|
|
/* check whether we can open the signed material */
|
2003-04-15 15:46:13 +00:00
|
|
|
fp = open_sigfile( sigfilename, &pfx );
|
1998-07-14 17:10:28 +00:00
|
|
|
if( fp ) {
|
1999-05-19 14:12:26 +00:00
|
|
|
do_hash( md, md2, fp, textmode );
|
1998-07-14 17:10:28 +00:00
|
|
|
iobuf_close(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
log_error (_("no signed data\n"));
|
|
|
|
return G10ERR_OPEN_FILE;
|
1998-07-14 17:10:28 +00:00
|
|
|
}
|
1998-03-09 21:44:06 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
for (sl=files; sl; sl = sl->next ) {
|
1998-03-09 21:44:06 +00:00
|
|
|
fp = iobuf_open( sl->d );
|
2004-10-13 18:10:06 +00:00
|
|
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
|
|
|
{
|
|
|
|
iobuf_close (fp);
|
|
|
|
fp = NULL;
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
1998-03-09 21:44:06 +00:00
|
|
|
if( !fp ) {
|
1998-12-29 13:47:31 +00:00
|
|
|
log_error(_("can't open signed data `%s'\n"),
|
1998-03-09 21:44:06 +00:00
|
|
|
print_fname_stdin(sl->d));
|
2002-06-29 13:46:34 +00:00
|
|
|
return G10ERR_OPEN_FILE;
|
1998-03-09 21:44:06 +00:00
|
|
|
}
|
2003-04-15 15:46:13 +00:00
|
|
|
handle_progress (&pfx, fp, sl->d);
|
1999-05-19 14:12:26 +00:00
|
|
|
do_hash( md, md2, fp, textmode );
|
1998-03-09 21:44:06 +00:00
|
|
|
iobuf_close(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|