1 /* status.c - Status message and command-fd interface
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3 * 2004, 2005, 2006 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
38 #include "cipher.h" /* for progress functions */
40 #define CONTROL_D ('D' - 'A' + 1)
44 static FILE *statusfp
;
48 progress_cb ( void *ctx
, int c
)
53 sprintf ( buf
, "%.20s X 100 100", (char*)ctx
);
55 sprintf ( buf
, "%.20s %c 0 0", (char*)ctx
, c
);
56 write_status_text ( STATUS_PROGRESS
, buf
);
60 get_status_string ( int no
)
66 case STATUS_ENTER
: s
= "ENTER"; break;
67 case STATUS_LEAVE
: s
= "LEAVE"; break;
68 case STATUS_ABORT
: s
= "ABORT"; break;
69 case STATUS_NEWSIG
: s
= "NEWSIG"; break;
70 case STATUS_GOODSIG
: s
= "GOODSIG"; break;
71 case STATUS_KEYEXPIRED
: s
= "KEYEXPIRED"; break;
72 case STATUS_KEYREVOKED
: s
= "KEYREVOKED"; break;
73 case STATUS_BADSIG
: s
= "BADSIG"; break;
74 case STATUS_ERRSIG
: s
= "ERRSIG"; break;
75 case STATUS_BADARMOR
: s
= "BADARMOR"; break;
76 case STATUS_RSA_OR_IDEA
: s
= "RSA_OR_IDEA"; break;
77 case STATUS_TRUST_UNDEFINED
: s
= "TRUST_UNDEFINED"; break;
78 case STATUS_TRUST_NEVER
: s
= "TRUST_NEVER"; break;
79 case STATUS_TRUST_MARGINAL
: s
= "TRUST_MARGINAL"; break;
80 case STATUS_TRUST_FULLY
: s
= "TRUST_FULLY"; break;
81 case STATUS_TRUST_ULTIMATE
: s
= "TRUST_ULTIMATE"; break;
82 case STATUS_GET_BOOL
: s
= "GET_BOOL"; break;
83 case STATUS_GET_LINE
: s
= "GET_LINE"; break;
84 case STATUS_GET_HIDDEN
: s
= "GET_HIDDEN"; break;
85 case STATUS_GOT_IT
: s
= "GOT_IT"; break;
86 case STATUS_SHM_INFO
: s
= "SHM_INFO"; break;
87 case STATUS_SHM_GET
: s
= "SHM_GET"; break;
88 case STATUS_SHM_GET_BOOL
: s
= "SHM_GET_BOOL"; break;
89 case STATUS_SHM_GET_HIDDEN
: s
= "SHM_GET_HIDDEN"; break;
90 case STATUS_NEED_PASSPHRASE
: s
= "NEED_PASSPHRASE"; break;
91 case STATUS_VALIDSIG
: s
= "VALIDSIG"; break;
92 case STATUS_SIG_ID
: s
= "SIG_ID"; break;
93 case STATUS_ENC_TO
: s
= "ENC_TO"; break;
94 case STATUS_NODATA
: s
= "NODATA"; break;
95 case STATUS_BAD_PASSPHRASE
: s
= "BAD_PASSPHRASE"; break;
96 case STATUS_NO_PUBKEY
: s
= "NO_PUBKEY"; break;
97 case STATUS_NO_SECKEY
: s
= "NO_SECKEY"; break;
98 case STATUS_NEED_PASSPHRASE_SYM
: s
= "NEED_PASSPHRASE_SYM"; break;
99 case STATUS_NEED_PASSPHRASE_PIN
: s
= "NEED_PASSPHRASE_PIN"; break;
100 case STATUS_DECRYPTION_FAILED
: s
= "DECRYPTION_FAILED"; break;
101 case STATUS_DECRYPTION_OKAY
: s
= "DECRYPTION_OKAY"; break;
102 case STATUS_MISSING_PASSPHRASE
: s
= "MISSING_PASSPHRASE"; break;
103 case STATUS_GOOD_PASSPHRASE
: s
= "GOOD_PASSPHRASE"; break;
104 case STATUS_GOODMDC
: s
= "GOODMDC"; break;
105 case STATUS_BADMDC
: s
= "BADMDC"; break;
106 case STATUS_ERRMDC
: s
= "ERRMDC"; break;
107 case STATUS_IMPORTED
: s
= "IMPORTED"; break;
108 case STATUS_IMPORT_OK
: s
= "IMPORT_OK"; break;
109 case STATUS_IMPORT_CHECK
: s
= "IMPORT_CHECK"; break;
110 case STATUS_IMPORT_RES
: s
= "IMPORT_RES"; break;
111 case STATUS_FILE_START
: s
= "FILE_START"; break;
112 case STATUS_FILE_DONE
: s
= "FILE_DONE"; break;
113 case STATUS_FILE_ERROR
: s
= "FILE_ERROR"; break;
114 case STATUS_BEGIN_DECRYPTION
:s
= "BEGIN_DECRYPTION"; break;
115 case STATUS_END_DECRYPTION
: s
= "END_DECRYPTION"; break;
116 case STATUS_BEGIN_ENCRYPTION
:s
= "BEGIN_ENCRYPTION"; break;
117 case STATUS_END_ENCRYPTION
: s
= "END_ENCRYPTION"; break;
118 case STATUS_DELETE_PROBLEM
: s
= "DELETE_PROBLEM"; break;
119 case STATUS_PROGRESS
: s
= "PROGRESS"; break;
120 case STATUS_SIG_CREATED
: s
= "SIG_CREATED"; break;
121 case STATUS_SESSION_KEY
: s
= "SESSION_KEY"; break;
122 case STATUS_NOTATION_NAME
: s
= "NOTATION_NAME" ; break;
123 case STATUS_NOTATION_DATA
: s
= "NOTATION_DATA" ; break;
124 case STATUS_POLICY_URL
: s
= "POLICY_URL" ; break;
125 case STATUS_BEGIN_STREAM
: s
= "BEGIN_STREAM"; break;
126 case STATUS_END_STREAM
: s
= "END_STREAM"; break;
127 case STATUS_KEY_CREATED
: s
= "KEY_CREATED"; break;
128 case STATUS_KEY_NOT_CREATED
: s
= "KEY_NOT_CREATED"; break;
129 case STATUS_USERID_HINT
: s
= "USERID_HINT"; break;
130 case STATUS_UNEXPECTED
: s
= "UNEXPECTED"; break;
131 case STATUS_INV_RECP
: s
= "INV_RECP"; break;
132 case STATUS_NO_RECP
: s
= "NO_RECP"; break;
133 case STATUS_ALREADY_SIGNED
: s
= "ALREADY_SIGNED"; break;
134 case STATUS_SIGEXPIRED
: s
= "SIGEXPIRED deprecated-use-keyexpired-instead"; break;
135 case STATUS_EXPSIG
: s
= "EXPSIG"; break;
136 case STATUS_EXPKEYSIG
: s
= "EXPKEYSIG"; break;
137 case STATUS_REVKEYSIG
: s
= "REVKEYSIG"; break;
138 case STATUS_ATTRIBUTE
: s
= "ATTRIBUTE"; break;
139 case STATUS_CARDCTRL
: s
= "CARDCTRL"; break;
140 case STATUS_PLAINTEXT
: s
= "PLAINTEXT"; break;
141 case STATUS_PLAINTEXT_LENGTH
:s
= "PLAINTEXT_LENGTH"; break;
142 case STATUS_SIG_SUBPACKET
: s
= "SIG_SUBPACKET"; break;
143 case STATUS_SC_OP_SUCCESS
: s
= "SC_OP_SUCCESS"; break;
144 case STATUS_SC_OP_FAILURE
: s
= "SC_OP_FAILURE"; break;
145 case STATUS_BACKUP_KEY_CREATED
:s
="BACKUP_KEY_CREATED"; break;
146 case STATUS_PKA_TRUST_BAD
: s
= "PKA_TRUST_BAD"; break;
147 case STATUS_PKA_TRUST_GOOD
: s
= "PKA_TRUST_GOOD"; break;
148 case STATUS_BEGIN_SIGNING
: s
= "BEGIN_SIGNING"; break;
149 default: s
= "?"; break;
155 /* Return true if the status message NO may currently be issued. We
156 need this to avoid syncronisation problem while auto retrieving a
157 key. There it may happen that a status NODATA is issued for a non
158 available key and the user may falsely interpret this has a missing
161 status_currently_allowed (int no
)
163 if (!glo_ctrl
.in_auto_key_retrieve
)
166 /* We allow some statis anyway, so that import statistics are
167 correct and to avoid problems if the retriebval subsystem will
171 case STATUS_GET_BOOL
:
172 case STATUS_GET_LINE
:
173 case STATUS_GET_HIDDEN
:
175 case STATUS_IMPORTED
:
176 case STATUS_IMPORT_OK
:
177 case STATUS_IMPORT_CHECK
:
178 case STATUS_IMPORT_RES
:
188 set_status_fd ( int fd
)
190 static int last_fd
= -1;
192 if ( fd
!= -1 && last_fd
== fd
)
195 if ( statusfp
&& statusfp
!= stdout
&& statusfp
!= stderr
)
206 statusfp
= fdopen( fd
, "w" );
208 log_fatal("can't open fd %d for status output: %s\n",
209 fd
, strerror(errno
));
212 #warning Use libgrypt calls for progress indicators
213 /* register_primegen_progress ( progress_cb, "primegen" ); */
214 /* register_pk_dsa_progress ( progress_cb, "pk_dsa" ); */
215 /* register_pk_elg_progress ( progress_cb, "pk_elg" ); */
225 write_status ( int no
)
227 write_status_text( no
, NULL
);
231 write_status_text ( int no
, const char *text
)
233 if( !statusfp
|| !status_currently_allowed (no
) )
234 return; /* Not enabled or allowed. */
236 fputs ( "[GNUPG:] ", statusfp
);
237 fputs ( get_status_string (no
), statusfp
);
239 putc ( ' ', statusfp
);
240 for (; *text
; text
++) {
242 fputs ( "\\n", statusfp
);
243 else if (*text
== '\r')
244 fputs ( "\\r", statusfp
);
246 putc ( *(const byte
*)text
, statusfp
);
249 putc ('\n',statusfp
);
250 if ( fflush (statusfp
) && opt
.exit_on_status_write_error
)
256 * Write a status line with a buffer using %XX escapes. If WRAP is >
257 * 0 wrap the line after this length. If STRING is not NULL it will
258 * be prepended to the buffer, no escaping is done for string.
259 * A wrap of -1 forces spaces not to be encoded as %20.
262 write_status_text_and_buffer ( int no
, const char *string
,
263 const char *buffer
, size_t len
, int wrap
)
265 const char *s
, *text
;
267 int lower_limit
= ' ';
268 size_t n
, count
, dowrap
;
270 if( !statusfp
|| !status_currently_allowed (no
) )
271 return; /* Not enabled or allowed. */
278 text
= get_status_string (no
);
279 count
= dowrap
= first
= 1;
282 fprintf (statusfp
, "[GNUPG:] %s ", text
);
284 if (first
&& string
) {
285 fputs (string
, statusfp
);
286 count
+= strlen (string
);
290 for (esc
=0, s
=buffer
, n
=len
; n
&& !esc
; s
++, n
-- ) {
291 if ( *s
== '%' || *(const byte
*)s
<= lower_limit
292 || *(const byte
*)s
== 127 )
294 if ( wrap
&& ++count
> wrap
) {
303 fwrite (buffer
, s
-buffer
, 1, statusfp
);
305 fprintf (statusfp
, "%%%02X", *(const byte
*)s
);
311 putc ( '\n', statusfp
);
314 putc ('\n',statusfp
);
315 if ( fflush (statusfp
) && opt
.exit_on_status_write_error
)
320 write_status_buffer ( int no
, const char *buffer
, size_t len
, int wrap
)
322 write_status_text_and_buffer (no
, NULL
, buffer
, len
, wrap
);
328 myread(int fd
, void *buf
, size_t count
)
332 rc
= read( fd
, buf
, count
);
333 } while ( rc
== -1 && errno
== EINTR
);
334 if ( !rc
&& count
) {
335 static int eof_emmited
=0;
336 if ( eof_emmited
< 3 ) {
337 *(char*)buf
= CONTROL_D
;
341 else { /* Ctrl-D not caught - do something reasonable */
342 #ifdef HAVE_DOSISH_SYSTEM
343 raise (SIGINT
); /* nothing to hangup under DOS */
345 raise (SIGHUP
); /* no more input data */
355 * Request a string from the client over the command-fd
356 * If bool, returns static string on true (do not free) or NULL for false
359 do_get_from_fd( const char *keyword
, int hidden
, int bool )
367 write_status_text( bool? STATUS_GET_BOOL
:
368 hidden
? STATUS_GET_HIDDEN
: STATUS_GET_LINE
, keyword
);
370 for( string
= NULL
, i
= len
= 200; ; i
++ ) {
374 string
= hidden
? xmalloc_secure ( len
) : xmalloc ( len
);
376 memcpy(string
, save
, i
);
380 /* Hmmm: why not use our read_line function here */
381 if( myread( opt
.command_fd
, string
+i
, 1) != 1 || string
[i
] == '\n' )
383 else if ( string
[i
] == CONTROL_D
) {
384 /* found ETX - cancel the line and return a sole ETX */
385 string
[0] = CONTROL_D
;
392 write_status( STATUS_GOT_IT
);
394 if( bool ) /* Fixme: is this correct??? */
395 return (string
[0] == 'Y' || string
[0] == 'y') ? "" : NULL
;
405 if( opt
.command_fd
!= -1 )
407 #ifdef USE_SHM_COPROCESSING
408 if( opt
.shm_coprocess
)
415 cpr_get_no_help( const char *keyword
, const char *prompt
)
419 if( opt
.command_fd
!= -1 )
420 return do_get_from_fd ( keyword
, 0, 0 );
421 #ifdef USE_SHM_COPROCESSING
422 if( opt
.shm_coprocess
)
423 return do_shm_get( keyword
, 0, 0 );
426 p
= tty_get( prompt
);
432 cpr_get( const char *keyword
, const char *prompt
)
436 if( opt
.command_fd
!= -1 )
437 return do_get_from_fd ( keyword
, 0, 0 );
438 #ifdef USE_SHM_COPROCESSING
439 if( opt
.shm_coprocess
)
440 return do_shm_get( keyword
, 0, 0 );
443 p
= tty_get( prompt
);
444 if( *p
=='?' && !p
[1] && !(keyword
&& !*keyword
)) {
446 display_online_help( keyword
);
455 cpr_get_utf8( const char *keyword
, const char *prompt
)
458 p
= cpr_get( keyword
, prompt
);
460 char *utf8
= native_to_utf8( p
);
468 cpr_get_hidden( const char *keyword
, const char *prompt
)
472 if( opt
.command_fd
!= -1 )
473 return do_get_from_fd ( keyword
, 1, 0 );
474 #ifdef USE_SHM_COPROCESSING
475 if( opt
.shm_coprocess
)
476 return do_shm_get( keyword
, 1, 0 );
479 p
= tty_get_hidden( prompt
);
480 if( *p
== '?' && !p
[1] ) {
482 display_online_help( keyword
);
490 cpr_kill_prompt(void)
492 if( opt
.command_fd
!= -1 )
494 #ifdef USE_SHM_COPROCESSING
495 if( opt
.shm_coprocess
)
503 cpr_get_answer_is_yes( const char *keyword
, const char *prompt
)
508 if( opt
.command_fd
!= -1 )
509 return !!do_get_from_fd ( keyword
, 0, 1 );
510 #ifdef USE_SHM_COPROCESSING
511 if( opt
.shm_coprocess
)
512 return !!do_shm_get( keyword
, 0, 1 );
515 p
= tty_get( prompt
);
516 trim_spaces(p
); /* it is okay to do this here */
517 if( *p
== '?' && !p
[1] ) {
519 display_online_help( keyword
);
523 yes
= answer_is_yes(p
);
531 cpr_get_answer_yes_no_quit( const char *keyword
, const char *prompt
)
536 if( opt
.command_fd
!= -1 )
537 return !!do_get_from_fd ( keyword
, 0, 1 );
538 #ifdef USE_SHM_COPROCESSING
539 if( opt
.shm_coprocess
)
540 return !!do_shm_get( keyword
, 0, 1 );
543 p
= tty_get( prompt
);
544 trim_spaces(p
); /* it is okay to do this here */
545 if( *p
== '?' && !p
[1] ) {
547 display_online_help( keyword
);
551 yes
= answer_is_yes_no_quit(p
);
560 cpr_get_answer_okay_cancel (const char *keyword
,
568 if( opt
.command_fd
!= -1 )
569 answer
= do_get_from_fd ( keyword
, 0, 0 );
570 #ifdef USE_SHM_COPROCESSING
571 else if( opt
.shm_coprocess
)
572 answer
= do_shm_get( keyword
, 0, 0 );
577 yes
= answer_is_okay_cancel (answer
, def_answer
);
584 p
= tty_get( prompt
);
585 trim_spaces(p
); /* it is okay to do this here */
586 if (*p
== '?' && !p
[1])
589 display_online_help (keyword
);
594 yes
= answer_is_okay_cancel (p
, def_answer
);