From bb1bab488f9f57a793f798a8e511f8e9f984c65e Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Wed, 6 Dec 2000 12:06:19 +0000 Subject: [PATCH] Mainly changes to the pipe handling under W32 --- THANKS | 1 + g10/ChangeLog | 19 ++++- g10/cipher.c | 1 - g10/encode.c | 13 ++-- g10/g10.c | 3 +- g10/import.c | 1 + g10/sign.c | 5 +- g10/status.c | 194 +++++++++++++++++++++++++------------------------- util/iobuf.c | 14 +++- 9 files changed, 143 insertions(+), 108 deletions(-) diff --git a/THANKS b/THANKS index a8eb06a44..e6fed9598 100644 --- a/THANKS +++ b/THANKS @@ -111,6 +111,7 @@ N J Doye nic@niss.ac.uk Oliver Haakert haakert@hsp.de Oskari Jääskeläinen f33003a@cc.hut.fi Paul D. Smith psmith@baynetworks.com +Per Cederqvist ceder@lysator.liu.se Philippe Laliberte arsphl@oeil.qc.ca Peter Gutmann pgut001@cs.auckland.ac.nz Peter Marschall Peter.Marschall@gedos.de diff --git a/g10/ChangeLog b/g10/ChangeLog index 5b3587b07..305a4d341 100644 --- a/g10/ChangeLog +++ b/g10/ChangeLog @@ -1,8 +1,25 @@ +2000-12-05 Werner Koch + + * cipher.c (cipher_filter): Moved the end_encryption status ... + * encode.c (encode_simple,encode_crypt): to here + * sign.c (sign_file): and here. + + * status.c (mywrite): Removed. + (get_status_string): Removed the LFs from the strings. + (set_status_fd,is_status_enabed,write_status_text, + write_status_buffer): Replaced all mywrite by stdio calls and use + fdopen to create a strem. This is needed to make things smoother + in the W32 version. + +2000-12-04 Werner Koch + + * import.c (merge_blocks): Increment n_sigs for revocations. + 2000-11-30 Werner Koch * g10.c (main): Use iobuf_translate_file_handle for all options with filehandles as arguments. This is function does some magic - some for the W32 API. + for the W32 API. * verify.c (verify_signatures): Add a comment rant about the detached signature problem. diff --git a/g10/cipher.c b/g10/cipher.c index c45fa1e70..3e7803649 100644 --- a/g10/cipher.c +++ b/g10/cipher.c @@ -141,7 +141,6 @@ cipher_filter( void *opaque, int control, log_error("writing MDC packet failed\n" ); } cipher_close(cfx->cipher_hd); - write_status( STATUS_END_ENCRYPTION ); } else if( control == IOBUFCTRL_DESC ) { *(char**)buf = "cipher_filter"; diff --git a/g10/encode.c b/g10/encode.c index e70901387..7f5371283 100644 --- a/g10/encode.c +++ b/g10/encode.c @@ -36,7 +36,7 @@ #include "filter.h" #include "trustdb.h" #include "i18n.h" - +#include "status.h" static int encode_simple( const char *filename, int mode ); static int write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out ); @@ -221,8 +221,11 @@ encode_simple( const char *filename, int mode ) iobuf_close(inp); if (rc) iobuf_cancel(out); - else + else { iobuf_close(out); /* fixme: check returncode */ + if (mode) + write_status( STATUS_END_ENCRYPTION ); + } if (pt) pt->buf = NULL; free_packet(&pkt); @@ -323,7 +326,7 @@ encode_crypt( const char *filename, STRLIST remusr ) if( !(filesize = iobuf_get_filelength(inp)) ) log_info(_("%s: WARNING: empty file\n"), filename ); /* we can't yet encode the length of very large files, - * so we switch to partial lengthn encoding in this case */ + * so we switch to partial length encoding in this case */ if ( filesize >= IOBUF_FILELENGTH_LIMIT ) filesize = 0; } @@ -381,8 +384,10 @@ encode_crypt( const char *filename, STRLIST remusr ) iobuf_close(inp); if( rc ) iobuf_cancel(out); - else + else { iobuf_close(out); /* fixme: check returncode */ + write_status( STATUS_END_ENCRYPTION ); + } if( pt ) pt->buf = NULL; free_packet(&pkt); diff --git a/g10/g10.c b/g10/g10.c index 955a8d946..3d42c3734 100644 --- a/g10/g10.c +++ b/g10/g10.c @@ -677,7 +677,7 @@ main( int argc, char **argv ) else if ( pargs.r_opt == oStatusFD ) { /* this is needed to ensure that the status-fd filedescriptor is * initialized when init_shm_coprocessing() is called */ - set_status_fd( pargs.r.ret_int ); + set_status_fd( iobuf_translate_file_handle (pargs.r.ret_int, 1) ); } #endif } @@ -1594,7 +1594,6 @@ g10_exit( int rc ) secmem_term(); rc = rc? rc : log_get_errorcount(0)? 2 : g10_errors_seen? 1 : 0; - /*write_status( STATUS_LEAVE );*/ exit(rc ); } diff --git a/g10/import.c b/g10/import.c index 10fdb9961..6bd1582dc 100644 --- a/g10/import.c +++ b/g10/import.c @@ -1057,6 +1057,7 @@ merge_blocks( const char *fname, KBNODE keyblock_orig, KBNODE keyblock, KBNODE n2 = clone_kbnode(node); insert_kbnode( keyblock_orig, n2, 0 ); n2->flag |= 1; + ++*n_sigs; log_info( _("key %08lX: revocation certificate added\n"), (ulong)keyid[1]); } diff --git a/g10/sign.c b/g10/sign.c index 00ef1d710..4706f0bef 100644 --- a/g10/sign.c +++ b/g10/sign.c @@ -562,8 +562,11 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr, leave: if( rc ) iobuf_cancel(out); - else + else { iobuf_close(out); + if (encrypt) + write_status( STATUS_END_ENCRYPTION ); + } iobuf_close(inp); md_close( mfx.md ); release_sk_list( sk_list ); diff --git a/g10/status.c b/g10/status.c index 1c9b8a36a..98575b632 100644 --- a/g10/status.c +++ b/g10/status.c @@ -51,7 +51,8 @@ -static int fd = -1; +static FILE *statusfp; + #ifdef USE_SHM_COPROCESSING static int shm_id = -1; static volatile char *shm_area; @@ -78,81 +79,100 @@ get_status_string ( int no ) const char *s; switch( no ) { - case STATUS_ENTER : s = "ENTER\n"; break; - case STATUS_LEAVE : s = "LEAVE\n"; break; - case STATUS_ABORT : s = "ABORT\n"; break; - case STATUS_GOODSIG: s = "GOODSIG\n"; break; - case STATUS_SIGEXPIRED: s = "SIGEXPIRED\n"; break; - case STATUS_KEYREVOKED: s = "KEYREVOKED\n"; break; - case STATUS_BADSIG : s = "BADSIG\n"; break; - case STATUS_ERRSIG : s = "ERRSIG\n"; break; - case STATUS_BADARMOR : s = "BADARMOR\n"; break; - case STATUS_RSA_OR_IDEA : s= "RSA_OR_IDEA\n"; break; - case STATUS_TRUST_UNDEFINED: s = "TRUST_UNDEFINED\n"; break; - case STATUS_TRUST_NEVER : s = "TRUST_NEVER\n"; break; - case STATUS_TRUST_MARGINAL : s = "TRUST_MARGINAL\n"; break; - case STATUS_TRUST_FULLY : s = "TRUST_FULLY\n"; break; - case STATUS_TRUST_ULTIMATE : s = "TRUST_ULTIMATE\n"; break; - case STATUS_GET_BOOL : s = "GET_BOOL\n"; break; - case STATUS_GET_LINE : s = "GET_LINE\n"; break; - case STATUS_GET_HIDDEN : s = "GET_HIDDEN\n"; break; - case STATUS_GOT_IT : s = "GOT_IT\n"; break; - case STATUS_SHM_INFO : s = "SHM_INFO\n"; break; - case STATUS_SHM_GET : s = "SHM_GET\n"; break; - case STATUS_SHM_GET_BOOL : s = "SHM_GET_BOOL\n"; break; - case STATUS_SHM_GET_HIDDEN : s = "SHM_GET_HIDDEN\n"; break; - case STATUS_NEED_PASSPHRASE: s = "NEED_PASSPHRASE\n"; break; - case STATUS_VALIDSIG : s = "VALIDSIG\n"; break; - case STATUS_SIG_ID : s = "SIG_ID\n"; break; - case STATUS_ENC_TO : s = "ENC_TO\n"; break; - case STATUS_NODATA : s = "NODATA\n"; break; - case STATUS_BAD_PASSPHRASE : s = "BAD_PASSPHRASE\n"; break; - case STATUS_NO_PUBKEY : s = "NO_PUBKEY\n"; break; - case STATUS_NO_SECKEY : s = "NO_SECKEY\n"; break; - case STATUS_NEED_PASSPHRASE_SYM: s = "NEED_PASSPHRASE_SYM\n"; break; - case STATUS_DECRYPTION_FAILED: s = "DECRYPTION_FAILED\n"; break; - case STATUS_DECRYPTION_OKAY: s = "DECRYPTION_OKAY\n"; break; - case STATUS_MISSING_PASSPHRASE: s = "MISSING_PASSPHRASE\n"; break; - case STATUS_GOOD_PASSPHRASE : s = "GOOD_PASSPHRASE\n"; break; - case STATUS_GOODMDC : s = "GOODMDC\n"; break; - case STATUS_BADMDC : s = "BADMDC\n"; break; - case STATUS_ERRMDC : s = "ERRMDC\n"; break; - case STATUS_IMPORTED : s = "IMPORTED\n"; break; - case STATUS_IMPORT_RES : s = "IMPORT_RES\n"; break; - case STATUS_FILE_START : s = "FILE_START\n"; break; - case STATUS_FILE_DONE : s = "FILE_DONE\n"; break; - case STATUS_FILE_ERROR : s = "FILE_ERROR\n"; break; - case STATUS_BEGIN_DECRYPTION:s = "BEGIN_DECRYPTION\n"; break; - case STATUS_END_DECRYPTION : s = "END_DECRYPTION\n"; break; - case STATUS_BEGIN_ENCRYPTION:s = "BEGIN_ENCRYPTION\n"; break; - case STATUS_END_ENCRYPTION : s = "END_ENCRYPTION\n"; break; - case STATUS_DELETE_PROBLEM : s = "DELETE_PROBLEM\n"; break; - case STATUS_PROGRESS : s = "PROGRESS\n"; break; - case STATUS_SIG_CREATED : s = "SIG_CREATED\n"; break; - case STATUS_SESSION_KEY : s = "SESSION_KEY\n"; break; - case STATUS_NOTATION_NAME : s = "NOTATION_NAME\n" ; break; - case STATUS_NOTATION_DATA : s = "NOTATION_DATA\n" ; break; - case STATUS_POLICY_URL : s = "POLICY_URL\n" ; break; - default: s = "?\n"; break; + case STATUS_ENTER : s = "ENTER"; break; + case STATUS_LEAVE : s = "LEAVE"; break; + case STATUS_ABORT : s = "ABORT"; break; + case STATUS_GOODSIG: s = "GOODSIG"; break; + case STATUS_SIGEXPIRED: s = "SIGEXPIRED"; break; + case STATUS_KEYREVOKED: s = "KEYREVOKED"; break; + case STATUS_BADSIG : s = "BADSIG"; break; + case STATUS_ERRSIG : s = "ERRSIG"; break; + case STATUS_BADARMOR : s = "BADARMOR"; break; + case STATUS_RSA_OR_IDEA : s= "RSA_OR_IDEA"; break; + case STATUS_TRUST_UNDEFINED: s = "TRUST_UNDEFINED"; break; + case STATUS_TRUST_NEVER : s = "TRUST_NEVER"; break; + case STATUS_TRUST_MARGINAL : s = "TRUST_MARGINAL"; break; + case STATUS_TRUST_FULLY : s = "TRUST_FULLY"; break; + case STATUS_TRUST_ULTIMATE : s = "TRUST_ULTIMATE"; break; + case STATUS_GET_BOOL : s = "GET_BOOL"; break; + case STATUS_GET_LINE : s = "GET_LINE"; break; + case STATUS_GET_HIDDEN : s = "GET_HIDDEN"; break; + case STATUS_GOT_IT : s = "GOT_IT"; break; + case STATUS_SHM_INFO : s = "SHM_INFO"; break; + case STATUS_SHM_GET : s = "SHM_GET"; break; + case STATUS_SHM_GET_BOOL : s = "SHM_GET_BOOL"; break; + case STATUS_SHM_GET_HIDDEN : s = "SHM_GET_HIDDEN"; break; + case STATUS_NEED_PASSPHRASE: s = "NEED_PASSPHRASE"; break; + case STATUS_VALIDSIG : s = "VALIDSIG"; break; + case STATUS_SIG_ID : s = "SIG_ID"; break; + case STATUS_ENC_TO : s = "ENC_TO"; break; + case STATUS_NODATA : s = "NODATA"; break; + case STATUS_BAD_PASSPHRASE : s = "BAD_PASSPHRASE"; break; + case STATUS_NO_PUBKEY : s = "NO_PUBKEY"; break; + case STATUS_NO_SECKEY : s = "NO_SECKEY"; break; + case STATUS_NEED_PASSPHRASE_SYM: s = "NEED_PASSPHRASE_SYM"; break; + case STATUS_DECRYPTION_FAILED: s = "DECRYPTION_FAILED"; break; + case STATUS_DECRYPTION_OKAY: s = "DECRYPTION_OKAY"; break; + case STATUS_MISSING_PASSPHRASE: s = "MISSING_PASSPHRASE"; break; + case STATUS_GOOD_PASSPHRASE : s = "GOOD_PASSPHRASE"; break; + case STATUS_GOODMDC : s = "GOODMDC"; break; + case STATUS_BADMDC : s = "BADMDC"; break; + case STATUS_ERRMDC : s = "ERRMDC"; break; + case STATUS_IMPORTED : s = "IMPORTED"; break; + case STATUS_IMPORT_RES : s = "IMPORT_RES"; break; + case STATUS_FILE_START : s = "FILE_START"; break; + case STATUS_FILE_DONE : s = "FILE_DONE"; break; + case STATUS_FILE_ERROR : s = "FILE_ERROR"; break; + case STATUS_BEGIN_DECRYPTION:s = "BEGIN_DECRYPTION"; break; + case STATUS_END_DECRYPTION : s = "END_DECRYPTION"; break; + case STATUS_BEGIN_ENCRYPTION:s = "BEGIN_ENCRYPTION"; break; + case STATUS_END_ENCRYPTION : s = "END_ENCRYPTION"; break; + case STATUS_DELETE_PROBLEM : s = "DELETE_PROBLEM"; break; + case STATUS_PROGRESS : s = "PROGRESS"; break; + case STATUS_SIG_CREATED : s = "SIG_CREATED"; break; + case STATUS_SESSION_KEY : s = "SESSION_KEY"; break; + case STATUS_NOTATION_NAME : s = "NOTATION_NAME" ; break; + case STATUS_NOTATION_DATA : s = "NOTATION_DATA" ; break; + case STATUS_POLICY_URL : s = "POLICY_URL" ; break; + default: s = "?"; break; } return s; } void -set_status_fd ( int newfd ) +set_status_fd ( int fd ) { - fd = newfd; - if ( fd != -1 ) { - register_primegen_progress ( progress_cb, "primegen" ); - register_pk_dsa_progress ( progress_cb, "pk_dsa" ); - register_pk_elg_progress ( progress_cb, "pk_elg" ); + static int last_fd = -1; + + if ( fd != -1 && last_fd == fd ) + return; + + if ( statusfp && statusfp != stdout && statusfp != stderr ) + fclose (statusfp); + statusfp = NULL; + if ( fd == -1 ) + return; + + if( fd == 1 ) + statusfp = stdout; + else if( fd == 2 ) + statusfp = stderr; + else + statusfp = fdopen( fd, "w" ); + if( !statusfp ) { + log_fatal("can't open fd %d for status output: %s\n", + fd, strerror(errno)); } + last_fd = fd; + register_primegen_progress ( progress_cb, "primegen" ); + register_pk_dsa_progress ( progress_cb, "pk_dsa" ); + register_pk_elg_progress ( progress_cb, "pk_elg" ); } int is_status_enabled() { - return fd != -1; + return !!statusfp; } void @@ -161,35 +181,20 @@ write_status ( int no ) write_status_text( no, NULL ); } -static void -mywrite ( int fd, const char *buffer, size_t len ) -{ - int nwritten; - - do { - nwritten = write (fd, buffer, len ); - } while (nwritten == -1 && errno == EINTR ); -} - void write_status_text ( int no, const char *text) { - const char *s; - - if( fd == -1 ) + if( !statusfp ) return; /* not enabled */ - s = get_status_string (no); - - mywrite( fd, "[GNUPG:] ", 9 ); + fputs ( "[GNUPG:] ", statusfp ); + fputs ( get_status_string (no), statusfp ); if( text ) { - mywrite( fd, s, strlen(s)-1 ); - mywrite( fd, " ", 1 ); - mywrite( fd, text, strlen(text) ); - mywrite( fd, "\n", 1 ); + putc ( ' ', statusfp ); + fputs ( text, statusfp ); } - else - mywrite( fd, s, strlen(s) ); + putc ('\n',statusfp); + fflush (statusfp); } @@ -204,16 +209,14 @@ write_status_buffer ( int no, const char *buffer, size_t len, int wrap ) int esc; size_t n, count, dowrap; - if( fd == -1 ) + if( !statusfp ) return; /* not enabled */ text = get_status_string (no); count = dowrap = 1; do { if (dowrap) { - mywrite( fd, "[GNUPG:] ", 9 ); - mywrite( fd, text, strlen(text)-1 ); - mywrite( fd, " ", 1 ); + fprintf (statusfp, "[GNUPG:] %s ", text ); count = dowrap = 0; } for (esc=0, s=buffer, n=len; n && !esc; s++, n-- ) { @@ -228,20 +231,19 @@ write_status_buffer ( int no, const char *buffer, size_t len, int wrap ) s--; n++; } if (s != buffer) - mywrite ( fd, buffer, s-buffer ); + fwrite (buffer, s-buffer, 1, statusfp ); if ( esc ) { - char buf[5]; - sprintf (buf, "%%%02X", *(const byte*)s ); - mywrite (fd, buf, 3 ); + fprintf (statusfp, "%%%02X", *(const byte*)s ); s++; n--; } buffer = s; len = n; if ( dowrap && len ) - mywrite( fd, "\n", 1 ); + putc ( '\n', statusfp ); } while ( len ); - mywrite( fd, "\n", 1 ); + putc ('\n',statusfp); + fflush (statusfp); } diff --git a/util/iobuf.c b/util/iobuf.c index 12ca89fc2..f010b7d09 100644 --- a/util/iobuf.c +++ b/util/iobuf.c @@ -107,8 +107,9 @@ file_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len) else { clearerr( fp ); nbytes = fread( buf, 1, size, fp ); - if( feof(fp) && !nbytes ) + if( feof(fp) && !nbytes ) { rc = -1; /* okay: we can return EOF now. */ + } else if( ferror(fp) && errno != EPIPE ) { log_error("%s: read error: %s\n", a->fname, strerror(errno)); @@ -1626,14 +1627,21 @@ iobuf_translate_file_handle ( int fd, int for_write ) { #ifdef __MINGW32__ { - int x = _open_osfhandle ( (void*)fd, for_write? 1:0 ); + int x; + + if ( fd <= 2 ) + return fd; /* do not do this for error, stdin, stdout, stderr */ + + x = _open_osfhandle ( (void*)fd, for_write? 1:0 ); if (x==-1 ) log_error ("failed to translate osfhandle %p\n", (void*)fd ); else { - log_info ("_open_osfhandle %p yields %d\n", (void*)fd, x ); + /*log_info ("_open_osfhandle %p yields %d%s\n", + (void*)fd, x, for_write? " for writing":"" );*/ fd = x; } } #endif return fd; } +