2006-09-24 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / status.c
blob2098d6738e6d40d5f6af836949ad6fb4b1abc4a0
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,
20 * USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <signal.h>
31 #include "gpg.h"
32 #include "util.h"
33 #include "status.h"
34 #include "ttyio.h"
35 #include "options.h"
36 #include "main.h"
37 #include "i18n.h"
38 #include "cipher.h" /* for progress functions */
40 #define CONTROL_D ('D' - 'A' + 1)
44 static FILE *statusfp;
47 static void
48 progress_cb ( void *ctx, int c )
50 char buf[50];
52 if ( c == '\n' )
53 sprintf ( buf, "%.20s X 100 100", (char*)ctx );
54 else
55 sprintf ( buf, "%.20s %c 0 0", (char*)ctx, c );
56 write_status_text ( STATUS_PROGRESS, buf );
59 static const char *
60 get_status_string ( int no )
62 const char *s;
64 switch( 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;
151 return s;
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
159 signature. */
160 static int
161 status_currently_allowed (int no)
163 if (!glo_ctrl.in_auto_key_retrieve)
164 return 1; /* Yes. */
166 /* We allow some statis anyway, so that import statistics are
167 correct and to avoid problems if the retriebval subsystem will
168 prompt the user. */
169 switch (no)
171 case STATUS_GET_BOOL:
172 case STATUS_GET_LINE:
173 case STATUS_GET_HIDDEN:
174 case STATUS_GOT_IT:
175 case STATUS_IMPORTED:
176 case STATUS_IMPORT_OK:
177 case STATUS_IMPORT_CHECK:
178 case STATUS_IMPORT_RES:
179 return 1; /* Yes. */
180 default:
181 break;
183 return 0; /* No. */
187 void
188 set_status_fd ( int fd )
190 static int last_fd = -1;
192 if ( fd != -1 && last_fd == fd )
193 return;
195 if ( statusfp && statusfp != stdout && statusfp != stderr )
196 fclose (statusfp);
197 statusfp = NULL;
198 if ( fd == -1 )
199 return;
201 if( fd == 1 )
202 statusfp = stdout;
203 else if( fd == 2 )
204 statusfp = stderr;
205 else
206 statusfp = fdopen( fd, "w" );
207 if( !statusfp ) {
208 log_fatal("can't open fd %d for status output: %s\n",
209 fd, strerror(errno));
211 last_fd = fd;
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" ); */
219 is_status_enabled()
221 return !!statusfp;
224 void
225 write_status ( int no )
227 write_status_text( no, NULL );
230 void
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 );
238 if( text ) {
239 putc ( ' ', statusfp );
240 for (; *text; text++) {
241 if (*text == '\n')
242 fputs ( "\\n", statusfp );
243 else if (*text == '\r')
244 fputs ( "\\r", statusfp );
245 else
246 putc ( *(const byte *)text, statusfp );
249 putc ('\n',statusfp);
250 if ( fflush (statusfp) && opt.exit_on_status_write_error )
251 g10_exit (0);
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.
261 void
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;
266 int esc, first;
267 int lower_limit = ' ';
268 size_t n, count, dowrap;
270 if( !statusfp || !status_currently_allowed (no) )
271 return; /* Not enabled or allowed. */
273 if (wrap == -1) {
274 lower_limit--;
275 wrap = 0;
278 text = get_status_string (no);
279 count = dowrap = first = 1;
280 do {
281 if (dowrap) {
282 fprintf (statusfp, "[GNUPG:] %s ", text );
283 count = dowrap = 0;
284 if (first && string) {
285 fputs (string, statusfp);
286 count += strlen (string);
288 first = 0;
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 )
293 esc = 1;
294 if ( wrap && ++count > wrap ) {
295 dowrap=1;
296 break;
299 if (esc) {
300 s--; n++;
302 if (s != buffer)
303 fwrite (buffer, s-buffer, 1, statusfp );
304 if ( esc ) {
305 fprintf (statusfp, "%%%02X", *(const byte*)s );
306 s++; n--;
308 buffer = s;
309 len = n;
310 if ( dowrap && len )
311 putc ( '\n', statusfp );
312 } while ( len );
314 putc ('\n',statusfp);
315 if ( fflush (statusfp) && opt.exit_on_status_write_error )
316 g10_exit (0);
319 void
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);
327 static int
328 myread(int fd, void *buf, size_t count)
330 int rc;
331 do {
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;
338 rc = 1;
339 eof_emmited++;
341 else { /* Ctrl-D not caught - do something reasonable */
342 #ifdef HAVE_DOSISH_SYSTEM
343 raise (SIGINT); /* nothing to hangup under DOS */
344 #else
345 raise (SIGHUP); /* no more input data */
346 #endif
349 return rc;
354 /****************
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
358 static char *
359 do_get_from_fd( const char *keyword, int hidden, int bool )
361 int i, len;
362 char *string;
364 if(statusfp!=stdout)
365 fflush(stdout);
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++ ) {
371 if( i >= len-1 ) {
372 char *save = string;
373 len += 100;
374 string = hidden? xmalloc_secure ( len ) : xmalloc ( len );
375 if( save )
376 memcpy(string, save, i );
377 else
378 i=0;
380 /* Hmmm: why not use our read_line function here */
381 if( myread( opt.command_fd, string+i, 1) != 1 || string[i] == '\n' )
382 break;
383 else if ( string[i] == CONTROL_D ) {
384 /* found ETX - cancel the line and return a sole ETX */
385 string[0] = CONTROL_D;
386 i=1;
387 break;
390 string[i] = 0;
392 write_status( STATUS_GOT_IT );
394 if( bool ) /* Fixme: is this correct??? */
395 return (string[0] == 'Y' || string[0] == 'y') ? "" : NULL;
397 return string;
403 cpr_enabled()
405 if( opt.command_fd != -1 )
406 return 1;
407 #ifdef USE_SHM_COPROCESSING
408 if( opt.shm_coprocess )
409 return 1;
410 #endif
411 return 0;
414 char *
415 cpr_get_no_help( const char *keyword, const char *prompt )
417 char *p;
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 );
424 #endif
425 for(;;) {
426 p = tty_get( prompt );
427 return p;
431 char *
432 cpr_get( const char *keyword, const char *prompt )
434 char *p;
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 );
441 #endif
442 for(;;) {
443 p = tty_get( prompt );
444 if( *p=='?' && !p[1] && !(keyword && !*keyword)) {
445 xfree(p);
446 display_online_help( keyword );
448 else
449 return p;
454 char *
455 cpr_get_utf8( const char *keyword, const char *prompt )
457 char *p;
458 p = cpr_get( keyword, prompt );
459 if( p ) {
460 char *utf8 = native_to_utf8( p );
461 xfree( p );
462 p = utf8;
464 return p;
467 char *
468 cpr_get_hidden( const char *keyword, const char *prompt )
470 char *p;
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 );
477 #endif
478 for(;;) {
479 p = tty_get_hidden( prompt );
480 if( *p == '?' && !p[1] ) {
481 xfree(p);
482 display_online_help( keyword );
484 else
485 return p;
489 void
490 cpr_kill_prompt(void)
492 if( opt.command_fd != -1 )
493 return;
494 #ifdef USE_SHM_COPROCESSING
495 if( opt.shm_coprocess )
496 return;
497 #endif
498 tty_kill_prompt();
499 return;
503 cpr_get_answer_is_yes( const char *keyword, const char *prompt )
505 int yes;
506 char *p;
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 );
513 #endif
514 for(;;) {
515 p = tty_get( prompt );
516 trim_spaces(p); /* it is okay to do this here */
517 if( *p == '?' && !p[1] ) {
518 xfree(p);
519 display_online_help( keyword );
521 else {
522 tty_kill_prompt();
523 yes = answer_is_yes(p);
524 xfree(p);
525 return yes;
531 cpr_get_answer_yes_no_quit( const char *keyword, const char *prompt )
533 int yes;
534 char *p;
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 );
541 #endif
542 for(;;) {
543 p = tty_get( prompt );
544 trim_spaces(p); /* it is okay to do this here */
545 if( *p == '?' && !p[1] ) {
546 xfree(p);
547 display_online_help( keyword );
549 else {
550 tty_kill_prompt();
551 yes = answer_is_yes_no_quit(p);
552 xfree(p);
553 return yes;
560 cpr_get_answer_okay_cancel (const char *keyword,
561 const char *prompt,
562 int def_answer)
564 int yes;
565 char *answer = NULL;
566 char *p;
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 );
573 #endif
575 if (answer)
577 yes = answer_is_okay_cancel (answer, def_answer);
578 xfree (answer);
579 return yes;
582 for(;;)
584 p = tty_get( prompt );
585 trim_spaces(p); /* it is okay to do this here */
586 if (*p == '?' && !p[1])
588 xfree(p);
589 display_online_help (keyword);
591 else
593 tty_kill_prompt();
594 yes = answer_is_okay_cancel (p, def_answer);
595 xfree(p);
596 return yes;