diff options
Diffstat (limited to 'g10/status.c')
-rw-r--r-- | g10/status.c | 348 |
1 files changed, 258 insertions, 90 deletions
diff --git a/g10/status.c b/g10/status.c index d336ae3b0..e0b126b78 100644 --- a/g10/status.c +++ b/g10/status.c @@ -1,5 +1,5 @@ /* status.c - * Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. + * Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. * * This file is part of GnuPG. * @@ -24,11 +24,13 @@ #include <string.h> #include <errno.h> #include <unistd.h> +#include <signal.h> #ifdef USE_SHM_COPROCESSING #ifdef USE_CAPABILITIES #include <sys/capability.h> #endif #ifdef HAVE_SYS_IPC_H + #include <sys/types.h> #include <sys/ipc.h> #endif #ifdef HAVE_SYS_SHM_H @@ -38,16 +40,20 @@ #include <sys/mman.h> #endif #endif - -#include <gcrypt.h> #include "util.h" #include "status.h" #include "ttyio.h" #include "options.h" #include "main.h" #include "i18n.h" +#include "cipher.h" /* for progress functions */ + +#define CONTROL_D ('D' - 'A' + 1) + + + +static FILE *statusfp; -static int fd = -1; #ifdef USE_SHM_COPROCESSING static int shm_id = -1; static volatile char *shm_area; @@ -68,26 +74,118 @@ progress_cb ( void *ctx, int c ) write_status_text ( STATUS_PROGRESS, buf ); } +static const char * +get_status_string ( int no ) +{ + const char *s; + + switch( no ) { + 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_KEYEXPIRED: s = "KEYEXPIRED"; 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; + case STATUS_BEGIN_STREAM : s = "BEGIN_STREAM"; break; + case STATUS_END_STREAM : s = "END_STREAM"; break; + case STATUS_KEY_CREATED : s = "KEY_CREATED"; break; + case STATUS_USERID_HINT : s = "USERID_HINT"; break; + case STATUS_UNEXPECTED : s = "UNEXPECTED"; break; + case STATUS_INV_RECP : s = "INV_RECP"; break; + case STATUS_NO_RECP : s = "NO_RECP"; break; + case STATUS_ALREADY_SIGNED : s = "ALREADY_SIGNED"; break; + case STATUS_SIGEXPIRED : s = "SIGEXPIRED deprecated-use-keyexpired-instead"; break; + case STATUS_EXPSIG : s = "EXPSIG"; break; + case STATUS_EXPKEYSIG : s = "EXPKEYSIG"; break; + case STATUS_ATTRIBUTE : s = "ATTRIBUTE"; break; + default: s = "?"; break; + } + return s; +} void -set_status_fd ( int newfd ) +set_status_fd ( int fd ) { - fd = newfd; - if ( fd != -1 ) { - #warning fixme - progress functions - /* Has to be fixed in libgcrypt */ - #if 0 - register_primegen_progress ( progress_cb, "primegen" ); - register_pk_dsa_progress ( progress_cb, "pk_dsa" ); - register_pk_elg_progress ( progress_cb, "pk_elg" ); - #endif + 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 @@ -99,77 +197,97 @@ write_status ( int no ) void write_status_text ( int no, const char *text) { - const char *s; - - if( fd == -1 ) + if( !statusfp ) return; /* not enabled */ - 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; - default: s = "?\n"; break; + fputs ( "[GNUPG:] ", statusfp ); + fputs ( get_status_string (no), statusfp ); + if( text ) { + putc ( ' ', statusfp ); + for (; *text; text++) { + if (*text == '\n') + fputs ( "\\n", statusfp ); + else if (*text == '\r') + fputs ( "\\r", statusfp ); + else + putc ( *(const byte *)text, statusfp ); + } } + putc ('\n',statusfp); + fflush (statusfp); +} - write( fd, "[GNUPG:] ", 9 ); - if( text ) { - write( fd, s, strlen(s)-1 ); - write( fd, " ", 1 ); - write( fd, text, strlen(text) ); - write( fd, "\n", 1 ); + +/* + * Write a status line with a buffer using %XX escapes. If WRAP is > + * 0 wrap the line after this length. If STRING is not NULL it will + * be prepended to the buffer, no escaping is done for string. + * A wrap of -1 forces spaces not to be encoded as %20. + */ +void +write_status_text_and_buffer ( int no, const char *string, + const char *buffer, size_t len, int wrap ) +{ + const char *s, *text; + int esc, first; + int lower_limit = ' '; + size_t n, count, dowrap; + + if( !statusfp ) + return; /* not enabled */ + + if (wrap == -1) { + lower_limit--; + wrap = 0; } - else - write( fd, s, strlen(s) ); + + text = get_status_string (no); + count = dowrap = first = 1; + do { + if (dowrap) { + fprintf (statusfp, "[GNUPG:] %s ", text ); + count = dowrap = 0; + if (first && string) { + fputs (string, statusfp); + count += strlen (string); + } + first = 0; + } + for (esc=0, s=buffer, n=len; n && !esc; s++, n-- ) { + if ( *s == '%' || *(const byte*)s <= lower_limit + || *(const byte*)s == 127 ) + esc = 1; + if ( wrap && ++count > wrap ) { + dowrap=1; + break; + } + } + if (esc) { + s--; n++; + } + if (s != buffer) + fwrite (buffer, s-buffer, 1, statusfp ); + if ( esc ) { + fprintf (statusfp, "%%%02X", *(const byte*)s ); + s++; n--; + } + buffer = s; + len = n; + if ( dowrap && len ) + putc ( '\n', statusfp ); + } while ( len ); + + putc ('\n',statusfp); + fflush (statusfp); } +void +write_status_buffer ( int no, const char *buffer, size_t len, int wrap ) +{ + write_status_text_and_buffer (no, NULL, buffer, len, wrap); +} + + #ifdef USE_SHM_COPROCESSING @@ -333,7 +451,7 @@ do_shm_get( const char *keyword, int hidden, int bool ) if( bool ) return p[0]? "" : NULL; - string = hidden? gcry_xmalloc_secure( n+1 ) : gcry_xmalloc( n+1 ); + string = hidden? m_alloc_secure( n+1 ) : m_alloc( n+1 ); memcpy(string, p, n ); string[n] = 0; /* make sure it is a string */ if( hidden ) /* invalidate the memory */ @@ -344,6 +462,32 @@ do_shm_get( const char *keyword, int hidden, int bool ) #endif /* USE_SHM_COPROCESSING */ +static int +myread(int fd, void *buf, size_t count) +{ + int rc; + do { + rc = read( fd, buf, count ); + } while ( rc == -1 && errno == EINTR ); + if ( !rc && count ) { + static int eof_emmited=0; + if ( eof_emmited < 3 ) { + *(char*)buf = CONTROL_D; + rc = 1; + eof_emmited++; + } + else { /* Ctrl-D not caught - do something reasonable */ + #ifdef HAVE_DOSISH_SYSTEM + raise (SIGINT); /* nothing to hangup under DOS */ + #else + raise (SIGHUP); /* no more input data */ + #endif + } + } + return rc; +} + + /**************** * Request a string from the client over the command-fd @@ -362,15 +506,21 @@ do_get_from_fd( const char *keyword, int hidden, int bool ) if( i >= len-1 ) { char *save = string; len += 100; - string = hidden? gcry_xmalloc_secure ( len ) : gcry_malloc ( len ); + string = hidden? m_alloc_secure ( len ) : m_alloc ( len ); if( save ) memcpy(string, save, i ); else i=0; } /* Hmmm: why not use our read_line function here */ - if( read( opt.command_fd, string+i, 1) != 1 || string[i] == '\n' ) - break; + if( myread( opt.command_fd, string+i, 1) != 1 || string[i] == '\n' ) + break; + else if ( string[i] == CONTROL_D ) { + /* found ETX - cancel the line and return a sole ETX */ + string[0] = CONTROL_D; + i=1; + break; + } } string[i] = 0; @@ -397,6 +547,23 @@ cpr_enabled() } char * +cpr_get_no_help( const char *keyword, const char *prompt ) +{ + char *p; + + if( opt.command_fd != -1 ) + return do_get_from_fd ( keyword, 0, 0 ); + #ifdef USE_SHM_COPROCESSING + if( opt.shm_coprocess ) + return do_shm_get( keyword, 0, 0 ); + #endif + for(;;) { + p = tty_get( prompt ); + return p; + } +} + +char * cpr_get( const char *keyword, const char *prompt ) { char *p; @@ -410,7 +577,7 @@ cpr_get( const char *keyword, const char *prompt ) for(;;) { p = tty_get( prompt ); if( *p=='?' && !p[1] && !(keyword && !*keyword)) { - gcry_free(p); + m_free(p); display_online_help( keyword ); } else @@ -418,6 +585,7 @@ cpr_get( const char *keyword, const char *prompt ) } } + char * cpr_get_utf8( const char *keyword, const char *prompt ) { @@ -425,7 +593,7 @@ cpr_get_utf8( const char *keyword, const char *prompt ) p = cpr_get( keyword, prompt ); if( p ) { char *utf8 = native_to_utf8( p ); - gcry_free( p ); + m_free( p ); p = utf8; } return p; @@ -445,7 +613,7 @@ cpr_get_hidden( const char *keyword, const char *prompt ) for(;;) { p = tty_get_hidden( prompt ); if( *p == '?' && !p[1] ) { - gcry_free(p); + m_free(p); display_online_help( keyword ); } else @@ -482,13 +650,13 @@ cpr_get_answer_is_yes( const char *keyword, const char *prompt ) p = tty_get( prompt ); trim_spaces(p); /* it is okay to do this here */ if( *p == '?' && !p[1] ) { - gcry_free(p); + m_free(p); display_online_help( keyword ); } else { tty_kill_prompt(); yes = answer_is_yes(p); - gcry_free(p); + m_free(p); return yes; } } @@ -510,13 +678,13 @@ cpr_get_answer_yes_no_quit( const char *keyword, const char *prompt ) p = tty_get( prompt ); trim_spaces(p); /* it is okay to do this here */ if( *p == '?' && !p[1] ) { - gcry_free(p); + m_free(p); display_online_help( keyword ); } else { tty_kill_prompt(); yes = answer_is_yes_no_quit(p); - gcry_free(p); + m_free(p); return yes; } } |