1 /* status.c - Status message and command-fd interface
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3 * 2004, 2005, 2006, 2010 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 3 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, see <https://www.gnu.org/licenses/>.
39 #define CONTROL_D ('D' - 'A' + 1)
42 /* The stream to output the status information. Output is disabled if
44 static estream_t statusfp;
48 progress_cb (void *ctx, const char *what, int printchar,
49 int current, int total)
55 if ( printchar == '\n' && !strcmp (what, "primegen") )
56 snprintf (buf, sizeof buf, "%.20s X 100 100", what );
58 snprintf (buf, sizeof buf, "%.20s %c %d %d",
59 what, printchar=='\n'?'X':printchar, current, total );
60 write_status_text (STATUS_PROGRESS, buf);
64 /* Return true if the status message NO may currently be issued. We
65 need this to avoid syncronisation problem while auto retrieving a
66 key. There it may happen that a status NODATA is issued for a non
67 available key and the user may falsely interpret this has a missing
70 status_currently_allowed (int no)
72 if (!glo_ctrl.in_auto_key_retrieve)
75 /* We allow some statis anyway, so that import statistics are
76 correct and to avoid problems if the retriebval subsystem will
82 case STATUS_GET_HIDDEN:
85 case STATUS_IMPORT_OK:
86 case STATUS_IMPORT_CHECK:
87 case STATUS_IMPORT_RES:
97 set_status_fd (int fd)
99 static int last_fd = -1;
101 if (fd != -1 && last_fd == fd)
104 if (statusfp && statusfp != es_stdout && statusfp != es_stderr )
105 es_fclose (statusfp);
110 if (! gnupg_fd_valid (fd))
111 log_fatal ("status-fd is invalid: %s\n", strerror (errno));
114 statusfp = es_stdout;
116 statusfp = es_stderr;
118 statusfp = es_fdopen (fd, "w");
121 log_fatal ("can't open fd %d for status output: %s\n",
122 fd, strerror (errno));
126 gcry_set_progress_handler (progress_cb, NULL);
138 write_status ( int no )
140 write_status_text( no, NULL );
144 /* Write a status line with code NO followed by the string TEXT and
145 directly followed by the remaining strings up to a NULL. */
147 write_status_strings (int no, const char *text, ...)
152 if (!statusfp || !status_currently_allowed (no) )
153 return; /* Not enabled or allowed. */
155 es_fputs ("[GNUPG:] ", statusfp);
156 es_fputs (get_status_string (no), statusfp);
159 es_putc ( ' ', statusfp);
160 va_start (arg_ptr, text);
167 es_fputs ("\\n", statusfp);
169 es_fputs ("\\r", statusfp);
171 es_fputc (*(const byte *)s, statusfp);
174 while ((s = va_arg (arg_ptr, const char*)));
177 es_putc ('\n', statusfp);
178 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
184 write_status_text (int no, const char *text)
186 write_status_strings (no, text, NULL);
190 /* Write a status line with code NO followed by the outout of the
191 * printf style FORMAT. The caller needs to make sure that LFs and
192 * CRs are not printed. */
194 write_status_printf (int no, const char *format, ...)
198 if (!statusfp || !status_currently_allowed (no) )
199 return; /* Not enabled or allowed. */
201 es_fputs ("[GNUPG:] ", statusfp);
202 es_fputs (get_status_string (no), statusfp);
205 es_putc ( ' ', statusfp);
206 va_start (arg_ptr, format);
207 es_vfprintf (statusfp, format, arg_ptr);
210 es_putc ('\n', statusfp);
211 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
216 /* Write an ERROR status line using a full gpg-error error value. */
218 write_status_error (const char *where, gpg_error_t err)
220 if (!statusfp || !status_currently_allowed (STATUS_ERROR))
221 return; /* Not enabled or allowed. */
223 es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
224 get_status_string (STATUS_ERROR), where, err);
225 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
230 /* Same as above but outputs the error code only. */
232 write_status_errcode (const char *where, int errcode)
234 if (!statusfp || !status_currently_allowed (STATUS_ERROR))
235 return; /* Not enabled or allowed. */
237 es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
238 get_status_string (STATUS_ERROR), where, gpg_err_code (errcode));
239 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
244 /* Write a FAILURE status line. */
246 write_status_failure (const char *where, gpg_error_t err)
248 if (!statusfp || !status_currently_allowed (STATUS_FAILURE))
249 return; /* Not enabled or allowed. */
251 es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
252 get_status_string (STATUS_FAILURE), where, err);
253 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
259 * Write a status line with a buffer using %XX escapes. If WRAP is >
260 * 0 wrap the line after this length. If STRING is not NULL it will
261 * be prepended to the buffer, no escaping is done for string.
262 * A wrap of -1 forces spaces not to be encoded as %20.
265 write_status_text_and_buffer (int no, const char *string,
266 const char *buffer, size_t len, int wrap)
268 const char *s, *text;
270 int lower_limit = ' ';
271 size_t n, count, dowrap;
273 if (!statusfp || !status_currently_allowed (no))
274 return; /* Not enabled or allowed. */
282 text = get_status_string (no);
283 count = dowrap = first = 1;
288 es_fprintf (statusfp, "[GNUPG:] %s ", text);
292 es_fputs (string, statusfp);
293 count += strlen (string);
294 /* Make sure that there is a space after the string. */
295 if (*string && string[strlen (string)-1] != ' ')
297 es_putc (' ', statusfp);
303 for (esc=0, s=buffer, n=len; n && !esc; s++, n--)
305 if (*s == '%' || *(const byte*)s <= lower_limit
306 || *(const byte*)s == 127 )
308 if (wrap && ++count > wrap)
319 es_fwrite (buffer, s-buffer, 1, statusfp);
322 es_fprintf (statusfp, "%%%02X", *(const byte*)s );
328 es_putc ('\n', statusfp);
332 es_putc ('\n',statusfp);
333 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
339 write_status_buffer (int no, const char *buffer, size_t len, int wrap)
341 write_status_text_and_buffer (no, NULL, buffer, len, wrap);
345 /* Print the BEGIN_SIGNING status message. If MD is not NULL it is
346 used to retrieve the hash algorithms used for the message. */
348 write_status_begin_signing (gcry_md_hd_t md)
357 for (i=1; i <= 110; i++)
359 ga = map_md_openpgp_to_gcry (i);
360 if (ga && gcry_md_is_enabled (md, ga) && buflen+10 < DIM(buf))
362 snprintf (buf+buflen, DIM(buf) - buflen,
363 "%sH%d", buflen? " ":"",i);
364 buflen += strlen (buf+buflen);
367 write_status_text (STATUS_BEGIN_SIGNING, buf);
370 write_status ( STATUS_BEGIN_SIGNING );
375 myread(int fd, void *buf, size_t count)
380 rc = read( fd, buf, count );
382 while (rc == -1 && errno == EINTR);
386 static int eof_emmited=0;
387 if ( eof_emmited < 3 )
389 *(char*)buf = CONTROL_D;
393 else /* Ctrl-D not caught - do something reasonable */
395 #ifdef HAVE_DOSISH_SYSTEM
396 #ifndef HAVE_W32CE_SYSTEM
397 raise (SIGINT); /* Nothing to hangup under DOS. */
400 raise (SIGHUP); /* No more input data. */
409 /* Request a string from the client over the command-fd. If GETBOOL
410 is set the function returns a static string (do not free) if the
411 entered value was true or NULL if the entered value was false. */
413 do_get_from_fd ( const char *keyword, int hidden, int getbool )
418 if (statusfp != es_stdout)
419 es_fflush (es_stdout);
421 write_status_text (getbool? STATUS_GET_BOOL :
422 hidden? STATUS_GET_HIDDEN : STATUS_GET_LINE, keyword);
424 for (string = NULL, i = len = 200; ; i++ )
430 string = hidden? xmalloc_secure ( len ) : xmalloc ( len );
432 memcpy (string, save, i );
436 /* Fixme: why not use our read_line function here? */
437 if ( myread( opt.command_fd, string+i, 1) != 1 || string[i] == '\n' )
439 else if ( string[i] == CONTROL_D )
441 /* Found ETX - Cancel the line and return a sole ETX. */
442 string[0] = CONTROL_D;
449 write_status (STATUS_GOT_IT);
451 if (getbool) /* Fixme: is this correct??? */
452 return (string[0] == 'Y' || string[0] == 'y') ? "" : NULL;
462 if( opt.command_fd != -1 )
468 cpr_get_no_help( const char *keyword, const char *prompt )
472 if( opt.command_fd != -1 )
473 return do_get_from_fd ( keyword, 0, 0 );
475 p = tty_get( prompt );
481 cpr_get( const char *keyword, const char *prompt )
485 if( opt.command_fd != -1 )
486 return do_get_from_fd ( keyword, 0, 0 );
488 p = tty_get( prompt );
489 if( *p=='?' && !p[1] && !(keyword && !*keyword)) {
491 display_online_help( keyword );
500 cpr_get_utf8( const char *keyword, const char *prompt )
503 p = cpr_get( keyword, prompt );
505 char *utf8 = native_to_utf8( p );
513 cpr_get_hidden( const char *keyword, const char *prompt )
517 if( opt.command_fd != -1 )
518 return do_get_from_fd ( keyword, 1, 0 );
520 p = tty_get_hidden( prompt );
521 if( *p == '?' && !p[1] ) {
523 display_online_help( keyword );
531 cpr_kill_prompt(void)
533 if( opt.command_fd != -1 )
540 cpr_get_answer_is_yes_def (const char *keyword, const char *prompt, int def_yes)
545 if( opt.command_fd != -1 )
546 return !!do_get_from_fd ( keyword, 0, 1 );
548 p = tty_get( prompt );
549 trim_spaces(p); /* it is okay to do this here */
550 if( *p == '?' && !p[1] ) {
552 display_online_help( keyword );
556 yes = answer_is_yes_no_default (p, def_yes);
564 cpr_get_answer_is_yes (const char *keyword, const char *prompt)
566 return cpr_get_answer_is_yes_def (keyword, prompt, 0);
570 cpr_get_answer_yes_no_quit( const char *keyword, const char *prompt )
575 if( opt.command_fd != -1 )
576 return !!do_get_from_fd ( keyword, 0, 1 );
578 p = tty_get( prompt );
579 trim_spaces(p); /* it is okay to do this here */
580 if( *p == '?' && !p[1] ) {
582 display_online_help( keyword );
586 yes = answer_is_yes_no_quit(p);
595 cpr_get_answer_okay_cancel (const char *keyword,
603 if( opt.command_fd != -1 )
604 answer = do_get_from_fd ( keyword, 0, 0 );
608 yes = answer_is_okay_cancel (answer, def_answer);
615 p = tty_get( prompt );
616 trim_spaces(p); /* it is okay to do this here */
617 if (*p == '?' && !p[1])
620 display_online_help (keyword);
625 yes = answer_is_okay_cancel (p, def_answer);