2003-06-05 09:14:21 +02:00
|
|
|
/* plaintext.c - process plaintext packets
|
2006-04-28 16:31:29 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
|
|
|
* 2006 Free Software Foundation, Inc.
|
2003-06-05 09:14:21 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
2006-04-19 13:26:11 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2006-04-19 13:26:11 +02:00
|
|
|
#include <sys/types.h>
|
2003-06-05 09:14:21 +02:00
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
|
|
|
#include <fcntl.h> /* for setmode() */
|
|
|
|
#endif
|
|
|
|
|
2003-06-18 21:56:13 +02:00
|
|
|
#include "gpg.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "ttyio.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "status.h"
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
|
2006-04-19 13:26:11 +02:00
|
|
|
int nooutput, int clearsig )
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
|
|
|
char *fname = NULL;
|
|
|
|
FILE *fp = NULL;
|
2006-04-19 13:26:11 +02:00
|
|
|
static off_t count=0;
|
2003-06-05 09:14:21 +02:00
|
|
|
int rc = 0;
|
|
|
|
int c;
|
2006-04-19 13:26:11 +02:00
|
|
|
int convert = (pt->mode == 't' || pt->mode == 'u');
|
2003-06-05 09:14:21 +02:00
|
|
|
#ifdef __riscos__
|
|
|
|
int filetype = 0xfff;
|
|
|
|
#endif
|
2003-06-18 21:56:13 +02:00
|
|
|
|
2006-04-19 13:26:11 +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())
|
|
|
|
{
|
|
|
|
char status[50];
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
/* create the filename as C string */
|
|
|
|
if( nooutput )
|
|
|
|
;
|
|
|
|
else if( opt.outfile ) {
|
2006-04-19 13:26:11 +02:00
|
|
|
fname = xmalloc( strlen( opt.outfile ) + 1);
|
2003-06-05 09:14:21 +02:00
|
|
|
strcpy(fname, opt.outfile );
|
|
|
|
}
|
|
|
|
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-28 16:31:29 +02:00
|
|
|
else if( !opt.flags.use_embedded_filename ) {
|
2003-06-05 09:14:21 +02:00
|
|
|
fname = make_outfile_name( iobuf_get_real_fname(pt->buf) );
|
|
|
|
if( !fname )
|
|
|
|
fname = ask_outfile_name( pt->name, pt->namelen );
|
|
|
|
if( !fname ) {
|
2006-04-19 15:24:36 +02:00
|
|
|
rc = gpg_error (GPG_ERR_GENERAL); /* Can't create file. */
|
|
|
|
goto leave;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
2006-04-28 16:31:29 +02:00
|
|
|
else
|
|
|
|
fname=utf8_to_native(pt->name,pt->namelen,0);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if( nooutput )
|
|
|
|
;
|
2006-04-19 13:26:11 +02:00
|
|
|
else if ( iobuf_is_pipe_filename (fname) || !*fname)
|
|
|
|
{
|
|
|
|
/* No filename or "-" given; write to stdout. */
|
2003-06-05 09:14:21 +02:00
|
|
|
fp = stdout;
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
|
|
|
setmode ( fileno(fp) , O_BINARY );
|
|
|
|
#endif
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
else {
|
|
|
|
while( !overwrite_filep (fname) ) {
|
|
|
|
char *tmp = ask_outfile_name (NULL, 0);
|
|
|
|
if ( !tmp || !*tmp ) {
|
2003-06-18 21:56:13 +02:00
|
|
|
xfree (tmp);
|
2006-04-19 15:24:36 +02:00
|
|
|
rc = gpg_error (GPG_ERR_GENERAL); /* G10ERR_CREATE_FILE*/
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
2003-06-18 21:56:13 +02:00
|
|
|
xfree (fname);
|
2003-06-05 09:14:21 +02:00
|
|
|
fname = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef __riscos__
|
|
|
|
if( fp || nooutput )
|
|
|
|
;
|
2006-04-19 13:26:11 +02:00
|
|
|
else if (is_secured_filename (fname))
|
|
|
|
{
|
|
|
|
errno = EPERM;
|
2006-04-21 14:56:40 +02:00
|
|
|
rc = gpg_error_from_errno (errno);
|
2006-04-19 13:26:11 +02:00
|
|
|
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
|
|
|
|
goto leave;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
else if( !(fp = fopen(fname,"wb")) ) {
|
2006-04-21 14:56:40 +02:00
|
|
|
rc = gpg_error_from_errno (errno);
|
2003-06-05 09:14:21 +02:00
|
|
|
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
#else /* __riscos__ */
|
2006-04-19 13:26:11 +02: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] = '/';
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if( fp || nooutput )
|
|
|
|
;
|
|
|
|
else {
|
|
|
|
fp = fopen(fname,"wb");
|
|
|
|
if( !fp ) {
|
|
|
|
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
|
2006-04-19 13:26:11 +02:00
|
|
|
rc = G10ERR_CREATE_FILE;
|
2003-06-05 09:14:21 +02:00
|
|
|
if (errno == 106)
|
|
|
|
log_info("Do output file and input file have the same name?\n");
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
#endif /* __riscos__ */
|
|
|
|
|
|
|
|
if( !pt->is_partial ) {
|
2006-04-19 13:26:11 +02: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;
|
|
|
|
}
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
if( convert ) { /* text mode */
|
|
|
|
for( ; pt->len; pt->len-- ) {
|
|
|
|
if( (c = iobuf_get(pt->buf)) == -1 ) {
|
2006-04-19 13:26:11 +02:00
|
|
|
rc = gpg_error_from_errno (errno);
|
|
|
|
log_error ("problem reading source (%u bytes remaining)\n",
|
|
|
|
(unsigned)pt->len);
|
|
|
|
goto leave;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
if( mfx->md )
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_md_putc (mfx->md, c );
|
2003-06-05 09:14:21 +02:00
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
|
|
|
if( c == '\r' ) /* convert to native line ending */
|
|
|
|
continue; /* fixme: this hack might be too simple */
|
|
|
|
#endif
|
2006-04-19 13:26:11 +02:00
|
|
|
if( fp )
|
|
|
|
{
|
|
|
|
if(opt.max_output && (++count)>opt.max_output)
|
|
|
|
{
|
2006-04-19 15:24:36 +02:00
|
|
|
log_error ("error writing to `%s': %s\n",
|
|
|
|
fname,"exceeded --max-output limit\n");
|
2006-04-21 14:56:40 +02:00
|
|
|
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
2006-04-19 13:26:11 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
else if( putc( c, fp ) == EOF )
|
|
|
|
{
|
2006-04-19 15:24:36 +02:00
|
|
|
if (ferror (fp))
|
|
|
|
rc = gpg_error_from_errno (errno);
|
|
|
|
else
|
|
|
|
rc = gpg_error (GPG_ERR_EOF);
|
|
|
|
log_error ("error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* binary mode */
|
2006-04-19 13:26:11 +02:00
|
|
|
byte *buffer = xmalloc( 32768 );
|
2003-06-05 09:14:21 +02:00
|
|
|
while( pt->len ) {
|
|
|
|
int len = pt->len > 32768 ? 32768 : pt->len;
|
|
|
|
len = iobuf_read( pt->buf, buffer, len );
|
|
|
|
if( len == -1 ) {
|
2006-04-19 15:24:36 +02:00
|
|
|
rc = gpg_error_from_errno (errno);
|
|
|
|
log_error ("problem reading source (%u bytes remaining)\n",
|
|
|
|
(unsigned)pt->len);
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree( buffer );
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if( mfx->md )
|
2006-04-19 13:26:11 +02:00
|
|
|
gcry_md_write ( mfx->md, buffer, len );
|
|
|
|
if( fp )
|
|
|
|
{
|
|
|
|
if(opt.max_output && (count+=len)>opt.max_output)
|
|
|
|
{
|
2006-04-21 14:56:40 +02:00
|
|
|
log_error ("error writing to `%s': %s\n",
|
|
|
|
fname,"exceeded --max-output limit\n");
|
|
|
|
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree( buffer );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
else if( fwrite( buffer, 1, len, fp ) != len )
|
|
|
|
{
|
2006-04-21 14:56:40 +02:00
|
|
|
rc = gpg_error_from_errno (errno);
|
|
|
|
log_error ("error writing to `%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree( buffer );
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
pt->len -= len;
|
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree( buffer );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( !clearsig ) {
|
|
|
|
if( convert ) { /* text mode */
|
|
|
|
while( (c = iobuf_get(pt->buf)) != -1 ) {
|
|
|
|
if( mfx->md )
|
2006-04-19 15:24:36 +02:00
|
|
|
gcry_md_putc (mfx->md, c );
|
2003-06-05 09:14:21 +02:00
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
|
|
|
if( convert && c == '\r' )
|
|
|
|
continue; /* fixme: this hack might be too simple */
|
|
|
|
#endif
|
2006-04-19 13:26:11 +02:00
|
|
|
if( fp )
|
|
|
|
{
|
|
|
|
if(opt.max_output && (++count)>opt.max_output)
|
|
|
|
{
|
|
|
|
log_error("Error writing to `%s': %s\n",
|
|
|
|
fname,"exceeded --max-output limit\n");
|
2006-04-21 14:56:40 +02:00
|
|
|
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
2006-04-19 13:26:11 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
else if( putc( c, fp ) == EOF )
|
|
|
|
{
|
2006-04-21 14:56:40 +02:00
|
|
|
if ( ferror (fp ) )
|
|
|
|
rc = gpg_error_from_errno (errno);
|
|
|
|
else
|
|
|
|
rc = gpg_error (GPG_ERR_EOF);
|
2006-05-23 18:19:43 +02:00
|
|
|
log_error("error writing to `%s': %s\n",
|
2003-06-05 09:14:21 +02:00
|
|
|
fname, strerror(errno) );
|
|
|
|
goto leave;
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* binary mode */
|
2006-04-19 13:26:11 +02:00
|
|
|
byte *buffer = xmalloc( 32768 );
|
2003-06-05 09:14:21 +02:00
|
|
|
int eof;
|
|
|
|
for( eof=0; !eof; ) {
|
|
|
|
/* Why do we check for len < 32768:
|
|
|
|
* If we won't, we would practically read 2 EOFs but
|
|
|
|
* the first one has already popped the block_filter
|
|
|
|
* off and therefore we don't catch the boundary.
|
|
|
|
* So, always assume EOF if iobuf_read returns less bytes
|
|
|
|
* then requested */
|
|
|
|
int len = iobuf_read( pt->buf, buffer, 32768 );
|
|
|
|
if( len == -1 )
|
|
|
|
break;
|
|
|
|
if( len < 32768 )
|
|
|
|
eof = 1;
|
|
|
|
if( mfx->md )
|
2006-04-19 15:24:36 +02:00
|
|
|
gcry_md_write ( mfx->md, buffer, len );
|
2006-04-19 13:26:11 +02:00
|
|
|
if( fp )
|
|
|
|
{
|
|
|
|
if(opt.max_output && (count+=len)>opt.max_output)
|
|
|
|
{
|
2006-05-23 18:19:43 +02:00
|
|
|
log_error("error writing to `%s': %s\n",
|
2006-04-19 13:26:11 +02:00
|
|
|
fname,"exceeded --max-output limit\n");
|
2006-04-21 14:56:40 +02:00
|
|
|
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree( buffer );
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
|
|
|
else if( fwrite( buffer, 1, len, fp ) != len ) {
|
2006-04-28 16:31:29 +02:00
|
|
|
rc = (errno? gpg_error_from_errno (errno)
|
|
|
|
: gpg_error (GPG_ERR_INTERNAL));
|
2006-05-23 18:19:43 +02:00
|
|
|
log_error ("error writing to `%s': %s\n",
|
2006-04-19 13:26:11 +02:00
|
|
|
fname, strerror(errno) );
|
|
|
|
xfree( buffer );
|
|
|
|
goto leave;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree( buffer );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
pt->buf = NULL;
|
|
|
|
}
|
|
|
|
else { /* clear text signature - don't hash the last cr,lf */
|
|
|
|
int state = 0;
|
|
|
|
|
|
|
|
while( (c = iobuf_get(pt->buf)) != -1 ) {
|
2006-04-19 13:26:11 +02:00
|
|
|
if( fp )
|
|
|
|
{
|
|
|
|
if(opt.max_output && (++count)>opt.max_output)
|
|
|
|
{
|
2006-05-23 18:19:43 +02:00
|
|
|
log_error ("error writing to `%s': %s\n",
|
2006-04-19 13:26:11 +02:00
|
|
|
fname,"exceeded --max-output limit\n");
|
2006-04-21 14:56:40 +02:00
|
|
|
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
|
|
|
else if( putc( c, fp ) == EOF )
|
|
|
|
{
|
2006-05-23 18:19:43 +02:00
|
|
|
rc = (errno? gpg_error_from_errno (errno)
|
|
|
|
: gpg_error (GPG_ERR_INTERNAL));
|
|
|
|
log_error ("error writing to `%s': %s\n",
|
2006-04-19 13:26:11 +02:00
|
|
|
fname, strerror(errno) );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
if( !mfx->md )
|
|
|
|
continue;
|
|
|
|
if( state == 2 ) {
|
2006-04-21 14:56:40 +02:00
|
|
|
gcry_md_putc (mfx->md, '\r' );
|
|
|
|
gcry_md_putc (mfx->md, '\n' );
|
2003-06-05 09:14:21 +02:00
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
if( !state ) {
|
|
|
|
if( c == '\r' )
|
|
|
|
state = 1;
|
|
|
|
else if( c == '\n' )
|
|
|
|
state = 2;
|
|
|
|
else
|
2006-04-21 14:56:40 +02:00
|
|
|
gcry_md_putc(mfx->md, c );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else if( state == 1 ) {
|
|
|
|
if( c == '\n' )
|
|
|
|
state = 2;
|
|
|
|
else {
|
2006-04-21 14:56:40 +02:00
|
|
|
gcry_md_putc(mfx->md, '\r' );
|
2003-06-05 09:14:21 +02:00
|
|
|
if( c == '\r' )
|
|
|
|
state = 1;
|
|
|
|
else {
|
|
|
|
state = 0;
|
2006-04-21 14:56:40 +02:00
|
|
|
gcry_md_putc(mfx->md, c );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pt->buf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fp && fp != stdout && fclose(fp) ) {
|
2006-05-23 18:19:43 +02:00
|
|
|
rc = (errno? gpg_error_from_errno (errno)
|
|
|
|
: gpg_error (GPG_ERR_INTERNAL));
|
|
|
|
log_error ("error closing `%s': %s\n", fname, strerror(errno) );
|
2003-06-05 09:14:21 +02:00
|
|
|
fp = NULL;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
fp = NULL;
|
|
|
|
|
|
|
|
leave:
|
|
|
|
if( fp && fp != stdout )
|
|
|
|
fclose(fp);
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(fname);
|
2003-06-05 09:14:21 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-04-19 13:26:11 +02:00
|
|
|
do_hash( gcry_md_hd_t md, gcry_md_hd_t md2, IOBUF fp, int textmode )
|
2003-06-05 09:14:21 +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' )
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_md_putc (md2, c);
|
2003-06-05 09:14:21 +02:00
|
|
|
else if( c == '\n' ) {
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_md_putc (md2, '\r');
|
|
|
|
gcry_md_putc (md2, c);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else if( c != '\n' && lc == '\r' ) {
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_md_putc (md2, '\n');
|
|
|
|
gcry_md_putc (md2, c);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_md_putc (md2, c);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if( md )
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_md_putc (md, c );
|
2003-06-05 09:14:21 +02:00
|
|
|
lc = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while( (c = iobuf_get(fp)) != -1 ) {
|
|
|
|
if( md )
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_md_putc (md, c );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Ask for the detached datafile and calculate the digest from it.
|
|
|
|
* INFILE is the name of the input file.
|
|
|
|
*/
|
|
|
|
int
|
2006-04-19 13:26:11 +02:00
|
|
|
ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2,
|
2003-06-05 09:14:21 +02:00
|
|
|
const char *inname, int textmode )
|
|
|
|
{
|
|
|
|
progress_filter_context_t pfx;
|
|
|
|
char *answer = NULL;
|
2006-04-19 13:26:11 +02:00
|
|
|
IOBUF fp;
|
2003-06-05 09:14:21 +02:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
fp = open_sigfile( inname, &pfx ); /* open default file */
|
|
|
|
|
|
|
|
if( !fp && !opt.batch ) {
|
|
|
|
int any=0;
|
|
|
|
tty_printf(_("Detached signature.\n"));
|
|
|
|
do {
|
2006-04-19 13:26:11 +02:00
|
|
|
char *name;
|
|
|
|
xfree(answer);
|
|
|
|
tty_enable_completion(NULL);
|
|
|
|
name = cpr_get("detached_signature.filename",
|
2003-06-05 09:14:21 +02:00
|
|
|
_("Please enter name of data file: "));
|
2006-04-19 13:26:11 +02:00
|
|
|
tty_disable_completion();
|
2003-06-05 09:14:21 +02:00
|
|
|
cpr_kill_prompt();
|
2006-04-19 13:26:11 +02:00
|
|
|
answer=make_filename(name,(void *)NULL);
|
|
|
|
xfree(name);
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
if( any && !*answer ) {
|
2006-04-19 13:26:11 +02:00
|
|
|
rc = gpg_error (GPG_ERR_GENERAL); /*G10ERR_READ_FILE*/
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
fp = iobuf_open(answer);
|
2006-04-19 13:26:11 +02:00
|
|
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
|
|
|
{
|
|
|
|
iobuf_close (fp);
|
|
|
|
fp = NULL;
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
if( !fp && errno == ENOENT ) {
|
|
|
|
tty_printf("No such file, try again or hit enter to quit.\n");
|
|
|
|
any++;
|
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
else if( !fp )
|
|
|
|
{
|
2006-04-19 15:24:36 +02:00
|
|
|
rc = gpg_error_from_errno (errno);
|
2006-04-19 13:26:11 +02:00
|
|
|
log_error(_("can't open `%s': %s\n"), answer, strerror(errno));
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
} while( !fp );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !fp ) {
|
|
|
|
if( opt.verbose )
|
|
|
|
log_info(_("reading stdin ...\n"));
|
|
|
|
fp = iobuf_open( NULL );
|
|
|
|
assert(fp);
|
|
|
|
}
|
|
|
|
do_hash( md, md2, fp, textmode );
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
|
|
|
leave:
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(answer);
|
2003-06-05 09:14:21 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Hash the given files and append the hash to hash context md.
|
|
|
|
* If FILES is NULL, hash stdin.
|
|
|
|
*/
|
|
|
|
int
|
2006-04-19 13:26:11 +02:00
|
|
|
hash_datafiles( gcry_md_hd_t md, gcry_md_hd_t md2, STRLIST files,
|
2003-06-05 09:14:21 +02:00
|
|
|
const char *sigfilename, int textmode )
|
|
|
|
{
|
|
|
|
progress_filter_context_t pfx;
|
2006-04-19 13:26:11 +02:00
|
|
|
IOBUF fp;
|
2003-06-05 09:14:21 +02:00
|
|
|
STRLIST sl;
|
|
|
|
|
|
|
|
if( !files ) {
|
|
|
|
/* check whether we can open the signed material */
|
|
|
|
fp = open_sigfile( sigfilename, &pfx );
|
|
|
|
if( fp ) {
|
|
|
|
do_hash( md, md2, fp, textmode );
|
|
|
|
iobuf_close(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
log_error (_("no signed data\n"));
|
2006-04-19 13:26:11 +02:00
|
|
|
return gpg_error (GPG_ERR_NO_DATA);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (sl=files; sl; sl = sl->next ) {
|
|
|
|
fp = iobuf_open( sl->d );
|
2006-04-19 13:26:11 +02:00
|
|
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
|
|
|
{
|
|
|
|
iobuf_close (fp);
|
|
|
|
fp = NULL;
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
if( !fp ) {
|
2006-04-19 15:24:36 +02:00
|
|
|
int rc = gpg_error_from_errno (errno);
|
2003-06-05 09:14:21 +02:00
|
|
|
log_error(_("can't open signed data `%s'\n"),
|
|
|
|
print_fname_stdin(sl->d));
|
2006-04-19 15:24:36 +02:00
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
handle_progress (&pfx, fp, sl->d);
|
|
|
|
do_hash( md, md2, fp, textmode );
|
|
|
|
iobuf_close(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-04-28 16:31:29 +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;
|
|
|
|
}
|