1997-11-18 14:06:00 +00:00
|
|
|
/* plaintext.c - process an plaintext packet
|
1998-02-24 18:50:46 +00:00
|
|
|
* Copyright (C) 1998 Free Software Foundation, Inc.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-02-24 18:50:46 +00:00
|
|
|
* This file is part of GNUPG.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-02-24 18:50:46 +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-02-24 18:50:46 +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>
|
|
|
|
#include "util.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#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
|
|
|
|
|
|
|
|
1998-07-08 09:29:43 +00:00
|
|
|
/****************
|
|
|
|
* Defer the last CR,LF
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
special_md_putc( MD_HANDLE md, int c, int *state )
|
|
|
|
{
|
|
|
|
if( c == -1 ) { /* flush */
|
|
|
|
if( *state == 1 ) {
|
|
|
|
md_putc(md, '\r');
|
|
|
|
}
|
|
|
|
*state = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
again:
|
|
|
|
switch( *state ) {
|
|
|
|
case 0:
|
|
|
|
if( c == '\r' )
|
|
|
|
*state = 1;
|
|
|
|
else
|
|
|
|
md_putc(md, c );
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if( c == '\n' )
|
|
|
|
*state = 2;
|
|
|
|
else {
|
|
|
|
md_putc(md, '\r');
|
|
|
|
*state = 0;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
md_putc(md, '\r');
|
|
|
|
md_putc(md, '\n');
|
|
|
|
*state = 0;
|
|
|
|
goto again;
|
|
|
|
default: BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
int rc = 0;
|
|
|
|
int c;
|
1998-02-11 23:22:09 +00:00
|
|
|
int convert = pt->mode == 't';
|
1998-07-08 09:29:43 +00:00
|
|
|
int special_state = 0;
|
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 ) {
|
1997-11-18 14:06:00 +00:00
|
|
|
fname = m_alloc( strlen( opt.outfile ) + 1);
|
|
|
|
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;
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
else {
|
|
|
|
fname = m_alloc( pt->namelen +1 );
|
|
|
|
memcpy( fname, pt->name, pt->namelen );
|
|
|
|
fname[pt->namelen] = 0;
|
|
|
|
}
|
|
|
|
|
1998-06-25 10:19:08 +00:00
|
|
|
if( nooutput )
|
|
|
|
;
|
|
|
|
else if( !*fname || (*fname=='-' && !fname[1])) {
|
1998-03-09 21:44:06 +00:00
|
|
|
/* no filename or "-" given; write to stdout */
|
1997-12-20 17:23:29 +00:00
|
|
|
fp = stdout;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
1998-08-08 19:27:00 +00:00
|
|
|
else if( !overwrite_filep( fname ) ) {
|
|
|
|
rc = G10ERR_CREATE_FILE;
|
1997-11-18 14:06:00 +00:00
|
|
|
goto leave;
|
1998-07-14 17:10:28 +00:00
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1998-06-25 10:19:08 +00:00
|
|
|
if( fp || nooutput )
|
1997-11-18 14:06:00 +00:00
|
|
|
;
|
|
|
|
else if( !(fp = fopen(fname,"wb")) ) {
|
|
|
|
log_error("Error creating '%s': %s\n", fname, strerror(errno) );
|
1998-07-14 17:10:28 +00:00
|
|
|
rc = G10ERR_CREATE_FILE;
|
1997-11-18 14:06:00 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pt->len ) {
|
|
|
|
for( ; pt->len; pt->len-- ) {
|
|
|
|
if( (c = iobuf_get(pt->buf)) == -1 ) {
|
1998-05-26 13:38:00 +00:00
|
|
|
log_error("Problem reading source (%u bytes remaining)\n",
|
|
|
|
(unsigned)pt->len);
|
1997-11-18 14:06:00 +00:00
|
|
|
rc = G10ERR_READ_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-07-08 09:29:43 +00:00
|
|
|
if( mfx->md ) {
|
|
|
|
if( convert && clearsig )
|
|
|
|
special_md_putc(mfx->md, c, &special_state );
|
|
|
|
else
|
|
|
|
md_putc(mfx->md, c );
|
|
|
|
}
|
1998-09-14 15:49:56 +00:00
|
|
|
if( convert && !clearsig && c == '\r' )
|
1998-07-08 09:29:43 +00:00
|
|
|
continue; /* fixme: this hack might be too simple */
|
1998-06-25 10:19:08 +00:00
|
|
|
if( fp ) {
|
|
|
|
if( putc( c, fp ) == EOF ) {
|
|
|
|
log_error("Error writing to '%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while( (c = iobuf_get(pt->buf)) != -1 ) {
|
1998-07-08 09:29:43 +00:00
|
|
|
if( mfx->md ) {
|
|
|
|
if( convert && clearsig )
|
|
|
|
special_md_putc(mfx->md, c, &special_state );
|
|
|
|
else
|
|
|
|
md_putc(mfx->md, c );
|
|
|
|
}
|
1998-09-14 15:49:56 +00:00
|
|
|
if( convert && !clearsig && c == '\r' )
|
1998-07-08 09:29:43 +00:00
|
|
|
continue; /* fixme: this hack might be too simple */
|
1998-06-25 10:19:08 +00:00
|
|
|
if( fp ) {
|
|
|
|
if( putc( c, fp ) == EOF ) {
|
|
|
|
log_error("Error writing to '%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
iobuf_clear_eof(pt->buf);
|
|
|
|
}
|
1998-07-08 09:29:43 +00:00
|
|
|
if( mfx->md && convert && clearsig )
|
|
|
|
special_md_putc(mfx->md, -1, &special_state ); /* flush */
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
if( fp && fp != stdout && fclose(fp) ) {
|
|
|
|
log_error("Error closing '%s': %s\n", fname, strerror(errno) );
|
|
|
|
fp = NULL;
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
fp = NULL;
|
|
|
|
|
|
|
|
leave:
|
|
|
|
if( fp && fp != stdout )
|
|
|
|
fclose(fp);
|
|
|
|
m_free(fname);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
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
|
1997-12-23 17:30:18 +00:00
|
|
|
ask_for_detached_datafile( md_filter_context_t *mfx, const char *inname )
|
1997-12-03 10:20:03 +00:00
|
|
|
{
|
1997-12-23 17:30:18 +00:00
|
|
|
char *answer = NULL;
|
|
|
|
IOBUF fp;
|
1997-12-03 10:20:03 +00:00
|
|
|
int rc = 0;
|
|
|
|
int c;
|
|
|
|
|
1997-12-23 17:30:18 +00:00
|
|
|
fp = open_sigfile( inname ); /* 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;
|
|
|
|
tty_printf("Detached signature.\n");
|
|
|
|
do {
|
|
|
|
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 ) {
|
|
|
|
rc = G10ERR_READ_FILE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
fp = iobuf_open(answer);
|
|
|
|
if( !fp && errno == ENOENT ) {
|
|
|
|
tty_printf("No such file, try again or hit enter to quit.\n");
|
|
|
|
any++;
|
|
|
|
}
|
|
|
|
else if( !fp ) {
|
|
|
|
log_error("can't open '%s': %s\n", answer, strerror(errno) );
|
|
|
|
rc = G10ERR_READ_FILE;
|
|
|
|
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"));
|
1998-02-11 23:22:09 +00:00
|
|
|
while( (c = getchar()) != EOF ) {
|
|
|
|
if( mfx->md )
|
|
|
|
md_putc(mfx->md, c );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while( (c = iobuf_get(fp)) != -1 ) {
|
|
|
|
if( mfx->md )
|
|
|
|
md_putc(mfx->md, c );
|
|
|
|
}
|
|
|
|
iobuf_close(fp);
|
1997-12-03 10:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
|
|
|
m_free(answer);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-07-14 17:10:28 +00:00
|
|
|
static void
|
|
|
|
do_hash( MD_HANDLE md, IOBUF fp, int textmode )
|
|
|
|
{
|
|
|
|
text_filter_context_t tfx;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if( textmode ) {
|
|
|
|
memset( &tfx, 0, sizeof tfx);
|
|
|
|
iobuf_push_filter( fp, text_filter, &tfx );
|
|
|
|
}
|
|
|
|
while( (c = iobuf_get(fp)) != -1 )
|
|
|
|
md_putc(md, c );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
1998-07-14 17:10:28 +00:00
|
|
|
hash_datafiles( MD_HANDLE md, STRLIST files,
|
|
|
|
const char *sigfilename, int textmode )
|
1998-03-09 21:44:06 +00:00
|
|
|
{
|
|
|
|
IOBUF fp;
|
|
|
|
STRLIST sl=NULL;
|
|
|
|
|
1998-07-14 17:10:28 +00:00
|
|
|
if( !files ) {
|
|
|
|
/* check whether we can opne the signed material */
|
|
|
|
fp = open_sigfile( sigfilename );
|
|
|
|
if( fp ) {
|
|
|
|
do_hash( md, fp, textmode );
|
|
|
|
iobuf_close(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* no we can't (no sigfile) - read signed stuff from stdin */
|
1998-03-09 21:44:06 +00:00
|
|
|
add_to_strlist( &sl, "-");
|
1998-07-14 17:10:28 +00:00
|
|
|
}
|
1998-03-09 21:44:06 +00:00
|
|
|
else
|
|
|
|
sl = files;
|
|
|
|
|
|
|
|
for( ; sl; sl = sl->next ) {
|
|
|
|
fp = iobuf_open( sl->d );
|
|
|
|
if( !fp ) {
|
|
|
|
log_error(_("can't open signed data '%s'\n"),
|
|
|
|
print_fname_stdin(sl->d));
|
|
|
|
if( !files )
|
|
|
|
free_strlist(sl);
|
|
|
|
return G10ERR_OPEN_FILE;
|
|
|
|
}
|
1998-07-14 17:10:28 +00:00
|
|
|
do_hash( md, fp, textmode );
|
1998-03-09 21:44:06 +00:00
|
|
|
iobuf_close(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !files )
|
|
|
|
free_strlist(sl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|