2011-02-04 12:57:53 +01:00
|
|
|
/* status.c - Status message and command-fd interface
|
2006-04-19 11:26:11 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
|
2010-01-08 19:18:49 +00:00
|
|
|
* 2004, 2005, 2006, 2010 Free Software Foundation, Inc.
|
2003-06-05 07:14:21 +00:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 19:49:40 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 07:14:21 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-06-05 07:14:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2010-04-14 14:39:16 +00:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
# include <signal.h>
|
|
|
|
#endif
|
2003-06-18 19:56:13 +00:00
|
|
|
|
|
|
|
#include "gpg.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/util.h"
|
|
|
|
#include "../common/status.h"
|
|
|
|
#include "../common/ttyio.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
#include "options.h"
|
|
|
|
#include "main.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/i18n.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
#define CONTROL_D ('D' - 'A' + 1)
|
|
|
|
|
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
/* The stream to output the status information. Output is disabled if
|
|
|
|
this is NULL. */
|
|
|
|
static estream_t statusfp;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-10-05 11:06:42 +00:00
|
|
|
progress_cb (void *ctx, const char *what, int printchar,
|
|
|
|
int current, int total)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2006-10-05 11:06:42 +00:00
|
|
|
char buf[50];
|
|
|
|
|
2008-10-20 13:53:23 +00:00
|
|
|
(void)ctx;
|
|
|
|
|
2006-10-05 11:06:42 +00:00
|
|
|
if ( printchar == '\n' && !strcmp (what, "primegen") )
|
Fix use cases of snprintf.
* agent/call-pinentry.c, agent/call-scd.c, agent/command.c,
build-aux/speedo/w32/g4wihelp.c, common/get-passphrase.c,
dirmngr/dirmngr.c, g10/call-agent.c, g10/cpr.c, g10/keygen.c,
g10/openfile.c, g10/passphrase.c, scd/app-openpgp.c, scd/scdaemon.c,
sm/call-agent.c, sm/call-dirmngr.c, sm/certreqgen.c: Fix assuming C99.
--
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2016-10-21 12:04:46 +09:00
|
|
|
snprintf (buf, sizeof buf, "%.20s X 100 100", what );
|
2006-10-05 11:06:42 +00:00
|
|
|
else
|
Fix use cases of snprintf.
* agent/call-pinentry.c, agent/call-scd.c, agent/command.c,
build-aux/speedo/w32/g4wihelp.c, common/get-passphrase.c,
dirmngr/dirmngr.c, g10/call-agent.c, g10/cpr.c, g10/keygen.c,
g10/openfile.c, g10/passphrase.c, scd/app-openpgp.c, scd/scdaemon.c,
sm/call-agent.c, sm/call-dirmngr.c, sm/certreqgen.c: Fix assuming C99.
--
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2016-10-21 12:04:46 +09:00
|
|
|
snprintf (buf, sizeof buf, "%.20s %c %d %d",
|
2006-10-05 11:06:42 +00:00
|
|
|
what, printchar=='\n'?'X':printchar, current, total );
|
|
|
|
write_status_text (STATUS_PROGRESS, buf);
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
|
|
|
/* Return true if the status message NO may currently be issued. We
|
2019-06-23 20:21:02 -04:00
|
|
|
need this to avoid synchronization problem while auto retrieving a
|
2006-04-19 11:26:11 +00:00
|
|
|
key. There it may happen that a status NODATA is issued for a non
|
|
|
|
available key and the user may falsely interpret this has a missing
|
|
|
|
signature. */
|
|
|
|
static int
|
|
|
|
status_currently_allowed (int no)
|
|
|
|
{
|
|
|
|
if (!glo_ctrl.in_auto_key_retrieve)
|
|
|
|
return 1; /* Yes. */
|
|
|
|
|
|
|
|
/* We allow some statis anyway, so that import statistics are
|
2017-05-29 15:23:36 +02:00
|
|
|
correct and to avoid problems if the retrieval subsystem will
|
2006-04-19 11:26:11 +00:00
|
|
|
prompt the user. */
|
|
|
|
switch (no)
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
case STATUS_GET_BOOL:
|
|
|
|
case STATUS_GET_LINE:
|
|
|
|
case STATUS_GET_HIDDEN:
|
|
|
|
case STATUS_GOT_IT:
|
2006-04-19 11:26:11 +00:00
|
|
|
case STATUS_IMPORTED:
|
2011-02-04 12:57:53 +01:00
|
|
|
case STATUS_IMPORT_OK:
|
|
|
|
case STATUS_IMPORT_CHECK:
|
2006-04-19 11:26:11 +00:00
|
|
|
case STATUS_IMPORT_RES:
|
|
|
|
return 1; /* Yes. */
|
|
|
|
default:
|
|
|
|
break;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
return 0; /* No. */
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
void
|
2010-06-07 17:04:50 +00:00
|
|
|
set_status_fd (int fd)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2010-06-07 17:04:50 +00:00
|
|
|
static int last_fd = -1;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
if (fd != -1 && last_fd == fd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (statusfp && statusfp != es_stdout && statusfp != es_stderr )
|
|
|
|
es_fclose (statusfp);
|
|
|
|
statusfp = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
if (fd == -1)
|
2010-06-07 17:04:50 +00:00
|
|
|
return;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2017-02-08 13:49:41 +01:00
|
|
|
if (! gnupg_fd_valid (fd))
|
|
|
|
log_fatal ("status-fd is invalid: %s\n", strerror (errno));
|
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
if (fd == 1)
|
|
|
|
statusfp = es_stdout;
|
|
|
|
else if (fd == 2)
|
|
|
|
statusfp = es_stderr;
|
|
|
|
else
|
|
|
|
statusfp = es_fdopen (fd, "w");
|
|
|
|
if (!statusfp)
|
|
|
|
{
|
|
|
|
log_fatal ("can't open fd %d for status output: %s\n",
|
|
|
|
fd, strerror (errno));
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2010-06-07 17:04:50 +00:00
|
|
|
last_fd = fd;
|
2006-10-05 11:06:42 +00:00
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
gcry_set_progress_handler (progress_cb, NULL);
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
int
|
2022-09-13 16:34:00 +09:00
|
|
|
is_status_enabled (void)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2010-06-07 17:04:50 +00:00
|
|
|
return !!statusfp;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
void
|
|
|
|
write_status ( int no )
|
|
|
|
{
|
|
|
|
write_status_text( no, NULL );
|
|
|
|
}
|
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
|
2013-12-11 10:20:15 +01:00
|
|
|
/* Write a status line with code NO followed by the string TEXT and
|
2019-03-15 13:02:44 +01:00
|
|
|
* directly followed by the remaining strings up to a NULL. Embedded
|
|
|
|
* CR and LFs in the strings (but not in TEXT) are C-style escaped.*/
|
2003-06-05 07:14:21 +00:00
|
|
|
void
|
2013-12-11 10:20:15 +01:00
|
|
|
write_status_strings (int no, const char *text, ...)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2013-12-11 10:20:15 +01:00
|
|
|
va_list arg_ptr;
|
|
|
|
const char *s;
|
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
if (!statusfp || !status_currently_allowed (no) )
|
|
|
|
return; /* Not enabled or allowed. */
|
|
|
|
|
|
|
|
es_fputs ("[GNUPG:] ", statusfp);
|
|
|
|
es_fputs (get_status_string (no), statusfp);
|
2011-02-04 12:57:53 +01:00
|
|
|
if ( text )
|
2010-06-07 17:04:50 +00:00
|
|
|
{
|
|
|
|
es_putc ( ' ', statusfp);
|
2013-12-11 10:20:15 +01:00
|
|
|
va_start (arg_ptr, text);
|
|
|
|
s = text;
|
|
|
|
do
|
2010-06-07 17:04:50 +00:00
|
|
|
{
|
2013-12-11 10:20:15 +01:00
|
|
|
for (; *s; s++)
|
|
|
|
{
|
|
|
|
if (*s == '\n')
|
|
|
|
es_fputs ("\\n", statusfp);
|
|
|
|
else if (*s == '\r')
|
|
|
|
es_fputs ("\\r", statusfp);
|
|
|
|
else
|
|
|
|
es_fputc (*(const byte *)s, statusfp);
|
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2013-12-11 10:20:15 +01:00
|
|
|
while ((s = va_arg (arg_ptr, const char*)));
|
|
|
|
va_end (arg_ptr);
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2010-06-07 17:04:50 +00:00
|
|
|
es_putc ('\n', statusfp);
|
|
|
|
if (es_fflush (statusfp) && opt.exit_on_status_write_error)
|
|
|
|
g10_exit (0);
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-01 20:43:57 +02:00
|
|
|
/* Write a status line with code NO followed by the remaining
|
|
|
|
* arguments which must be a list of strings terminated by a NULL.
|
|
|
|
* Embedded CR and LFs in the strings are C-style escaped. All
|
|
|
|
* strings are printed with a space as delimiter. */
|
|
|
|
gpg_error_t
|
|
|
|
write_status_strings2 (ctrl_t dummy, int no, ...)
|
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
(void)dummy;
|
|
|
|
|
|
|
|
if (!statusfp || !status_currently_allowed (no) )
|
|
|
|
return 0; /* Not enabled or allowed. */
|
|
|
|
|
|
|
|
va_start (arg_ptr, no);
|
|
|
|
|
|
|
|
es_fputs ("[GNUPG:] ", statusfp);
|
|
|
|
es_fputs (get_status_string (no), statusfp);
|
|
|
|
while ((s = va_arg (arg_ptr, const char*)))
|
|
|
|
{
|
|
|
|
if (*s)
|
|
|
|
es_putc (' ', statusfp);
|
|
|
|
for (; *s; s++)
|
|
|
|
{
|
|
|
|
if (*s == '\n')
|
|
|
|
es_fputs ("\\n", statusfp);
|
|
|
|
else if (*s == '\r')
|
|
|
|
es_fputs ("\\r", statusfp);
|
|
|
|
else
|
|
|
|
es_fputc (*(const byte *)s, statusfp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
es_putc ('\n', statusfp);
|
|
|
|
|
|
|
|
va_end (arg_ptr);
|
|
|
|
|
|
|
|
if (es_fflush (statusfp) && opt.exit_on_status_write_error)
|
|
|
|
g10_exit (0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-11 10:20:15 +01:00
|
|
|
void
|
|
|
|
write_status_text (int no, const char *text)
|
|
|
|
{
|
|
|
|
write_status_strings (no, text, NULL);
|
|
|
|
}
|
|
|
|
|
2016-09-01 16:33:21 +02:00
|
|
|
|
2018-10-24 15:56:18 -04:00
|
|
|
/* Write a status line with code NO followed by the output of the
|
2019-03-15 13:02:44 +01:00
|
|
|
* printf style FORMAT. Embedded CR and LFs are C-style escaped. */
|
2016-09-01 16:33:21 +02:00
|
|
|
void
|
|
|
|
write_status_printf (int no, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
2019-03-15 13:02:44 +01:00
|
|
|
char *buf;
|
2016-09-01 16:33:21 +02:00
|
|
|
|
|
|
|
if (!statusfp || !status_currently_allowed (no) )
|
|
|
|
return; /* Not enabled or allowed. */
|
|
|
|
|
|
|
|
es_fputs ("[GNUPG:] ", statusfp);
|
|
|
|
es_fputs (get_status_string (no), statusfp);
|
|
|
|
if (format)
|
|
|
|
{
|
|
|
|
es_putc ( ' ', statusfp);
|
|
|
|
va_start (arg_ptr, format);
|
2019-03-15 13:02:44 +01:00
|
|
|
buf = gpgrt_vbsprintf (format, arg_ptr);
|
|
|
|
if (!buf)
|
|
|
|
log_error ("error printing status line: %s\n",
|
|
|
|
gpg_strerror (gpg_err_code_from_syserror ()));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (strpbrk (buf, "\r\n"))
|
|
|
|
{
|
|
|
|
const byte *s;
|
|
|
|
for (s=buf; *s; s++)
|
|
|
|
{
|
|
|
|
if (*s == '\n')
|
|
|
|
es_fputs ("\\n", statusfp);
|
|
|
|
else if (*s == '\r')
|
|
|
|
es_fputs ("\\r", statusfp);
|
|
|
|
else
|
|
|
|
es_fputc (*s, statusfp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
es_fputs (buf, statusfp);
|
|
|
|
gpgrt_free (buf);
|
|
|
|
}
|
|
|
|
|
2016-09-01 16:33:21 +02:00
|
|
|
va_end (arg_ptr);
|
|
|
|
}
|
|
|
|
es_putc ('\n', statusfp);
|
|
|
|
if (es_fflush (statusfp) && opt.exit_on_status_write_error)
|
|
|
|
g10_exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-25 09:03:31 +02:00
|
|
|
/* Write an ERROR status line using a full gpg-error error value. */
|
2008-12-12 12:01:20 +00:00
|
|
|
void
|
2010-01-08 19:18:49 +00:00
|
|
|
write_status_error (const char *where, gpg_error_t err)
|
|
|
|
{
|
|
|
|
if (!statusfp || !status_currently_allowed (STATUS_ERROR))
|
|
|
|
return; /* Not enabled or allowed. */
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
|
2010-06-07 17:04:50 +00:00
|
|
|
get_status_string (STATUS_ERROR), where, err);
|
|
|
|
if (es_fflush (statusfp) && opt.exit_on_status_write_error)
|
2010-01-08 19:18:49 +00:00
|
|
|
g10_exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
/* Same as above but outputs the error code only. */
|
2010-01-08 19:18:49 +00:00
|
|
|
void
|
|
|
|
write_status_errcode (const char *where, int errcode)
|
2008-12-12 12:01:20 +00:00
|
|
|
{
|
|
|
|
if (!statusfp || !status_currently_allowed (STATUS_ERROR))
|
|
|
|
return; /* Not enabled or allowed. */
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
|
2010-06-07 17:04:50 +00:00
|
|
|
get_status_string (STATUS_ERROR), where, gpg_err_code (errcode));
|
|
|
|
if (es_fflush (statusfp) && opt.exit_on_status_write_error)
|
2008-12-12 12:01:20 +00:00
|
|
|
g10_exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-25 09:03:31 +02:00
|
|
|
/* Write a FAILURE status line. */
|
|
|
|
void
|
|
|
|
write_status_failure (const char *where, gpg_error_t err)
|
|
|
|
{
|
2018-04-06 17:32:08 +02:00
|
|
|
static int any_failure_printed;
|
|
|
|
|
2015-08-25 09:03:31 +02:00
|
|
|
if (!statusfp || !status_currently_allowed (STATUS_FAILURE))
|
|
|
|
return; /* Not enabled or allowed. */
|
2018-04-06 17:32:08 +02:00
|
|
|
if (any_failure_printed)
|
|
|
|
return;
|
|
|
|
any_failure_printed = 1;
|
2015-08-25 09:03:31 +02:00
|
|
|
es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
|
|
|
|
get_status_string (STATUS_FAILURE), where, err);
|
|
|
|
if (es_fflush (statusfp) && opt.exit_on_status_write_error)
|
|
|
|
g10_exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2010-06-07 17:04:50 +00:00
|
|
|
write_status_text_and_buffer (int no, const char *string,
|
|
|
|
const char *buffer, size_t len, int wrap)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2010-06-07 17:04:50 +00:00
|
|
|
const char *s, *text;
|
|
|
|
int esc, first;
|
|
|
|
int lower_limit = ' ';
|
|
|
|
size_t n, count, dowrap;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
if (!statusfp || !status_currently_allowed (no))
|
|
|
|
return; /* Not enabled or allowed. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
if (wrap == -1)
|
|
|
|
{
|
|
|
|
lower_limit--;
|
|
|
|
wrap = 0;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
text = get_status_string (no);
|
|
|
|
count = dowrap = first = 1;
|
2011-02-04 12:57:53 +01:00
|
|
|
do
|
2010-06-07 17:04:50 +00:00
|
|
|
{
|
|
|
|
if (dowrap)
|
|
|
|
{
|
|
|
|
es_fprintf (statusfp, "[GNUPG:] %s ", text);
|
|
|
|
count = dowrap = 0;
|
|
|
|
if (first && string)
|
|
|
|
{
|
|
|
|
es_fputs (string, statusfp);
|
|
|
|
count += strlen (string);
|
|
|
|
/* Make sure that there is a space after the string. */
|
|
|
|
if (*string && string[strlen (string)-1] != ' ')
|
|
|
|
{
|
|
|
|
es_putc (' ', statusfp);
|
|
|
|
count++;
|
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2010-06-07 17:04:50 +00:00
|
|
|
first = 0;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2022-06-14 11:33:27 +02:00
|
|
|
for (esc=0, s=buffer, n=len; n; s++, n--)
|
2010-06-07 17:04:50 +00:00
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
if (*s == '%' || *(const byte*)s <= lower_limit
|
|
|
|
|| *(const byte*)s == 127 )
|
2010-06-07 17:04:50 +00:00
|
|
|
esc = 1;
|
|
|
|
if (wrap && ++count > wrap)
|
2022-06-14 11:33:27 +02:00
|
|
|
dowrap=1;
|
|
|
|
if (esc || dowrap)
|
|
|
|
break;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
if (s != buffer)
|
2010-06-07 17:04:50 +00:00
|
|
|
es_fwrite (buffer, s-buffer, 1, statusfp);
|
2011-02-04 12:57:53 +01:00
|
|
|
if ( esc )
|
2010-06-07 17:04:50 +00:00
|
|
|
{
|
|
|
|
es_fprintf (statusfp, "%%%02X", *(const byte*)s );
|
|
|
|
s++; n--;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2010-06-07 17:04:50 +00:00
|
|
|
buffer = s;
|
|
|
|
len = n;
|
|
|
|
if (dowrap && len)
|
|
|
|
es_putc ('\n', statusfp);
|
2011-02-04 12:57:53 +01:00
|
|
|
}
|
2010-06-07 17:04:50 +00:00
|
|
|
while (len);
|
|
|
|
|
|
|
|
es_putc ('\n',statusfp);
|
|
|
|
if (es_fflush (statusfp) && opt.exit_on_status_write_error)
|
|
|
|
g10_exit (0);
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
void
|
2010-06-07 17:04:50 +00:00
|
|
|
write_status_buffer (int no, const char *buffer, size_t len, int wrap)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2010-06-07 17:04:50 +00:00
|
|
|
write_status_text_and_buffer (no, NULL, buffer, len, wrap);
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-30 20:16:28 +00:00
|
|
|
/* Print the BEGIN_SIGNING status message. If MD is not NULL it is
|
2008-12-09 08:58:02 +00:00
|
|
|
used to retrieve the hash algorithms used for the message. */
|
2007-01-30 20:16:28 +00:00
|
|
|
void
|
|
|
|
write_status_begin_signing (gcry_md_hd_t md)
|
|
|
|
{
|
|
|
|
if (md)
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
size_t buflen;
|
2014-01-31 15:33:03 +01:00
|
|
|
int i, ga;
|
|
|
|
|
2007-01-30 20:16:28 +00:00
|
|
|
buflen = 0;
|
2014-01-31 15:33:03 +01:00
|
|
|
for (i=1; i <= 110; i++)
|
|
|
|
{
|
|
|
|
ga = map_md_openpgp_to_gcry (i);
|
|
|
|
if (ga && gcry_md_is_enabled (md, ga) && buflen+10 < DIM(buf))
|
2007-01-30 20:16:28 +00:00
|
|
|
{
|
Fix use cases of snprintf.
* agent/call-pinentry.c, agent/call-scd.c, agent/command.c,
build-aux/speedo/w32/g4wihelp.c, common/get-passphrase.c,
dirmngr/dirmngr.c, g10/call-agent.c, g10/cpr.c, g10/keygen.c,
g10/openfile.c, g10/passphrase.c, scd/app-openpgp.c, scd/scdaemon.c,
sm/call-agent.c, sm/call-dirmngr.c, sm/certreqgen.c: Fix assuming C99.
--
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2016-10-21 12:04:46 +09:00
|
|
|
snprintf (buf+buflen, DIM(buf) - buflen,
|
2007-01-30 20:16:28 +00:00
|
|
|
"%sH%d", buflen? " ":"",i);
|
|
|
|
buflen += strlen (buf+buflen);
|
|
|
|
}
|
2014-01-31 15:33:03 +01:00
|
|
|
}
|
2010-06-07 17:04:50 +00:00
|
|
|
write_status_text (STATUS_BEGIN_SIGNING, buf);
|
2007-01-30 20:16:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
write_status ( STATUS_BEGIN_SIGNING );
|
|
|
|
}
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
myread(int fd, void *buf, size_t count)
|
|
|
|
{
|
2010-06-07 17:04:50 +00:00
|
|
|
int rc;
|
2011-02-04 12:57:53 +01:00
|
|
|
do
|
2010-06-07 17:04:50 +00:00
|
|
|
{
|
|
|
|
rc = read( fd, buf, count );
|
2011-02-04 12:57:53 +01:00
|
|
|
}
|
2010-06-07 17:04:50 +00:00
|
|
|
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++;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2010-06-07 17:04:50 +00:00
|
|
|
else /* Ctrl-D not caught - do something reasonable */
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2003-06-05 07:14:21 +00:00
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
2010-06-07 17:04:50 +00:00
|
|
|
raise (SIGINT); /* Nothing to hangup under DOS. */
|
2003-06-05 07:14:21 +00:00
|
|
|
#else
|
2010-06-07 17:04:50 +00:00
|
|
|
raise (SIGHUP); /* No more input data. */
|
2003-06-05 07:14:21 +00:00
|
|
|
#endif
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
}
|
2010-06-07 17:04:50 +00:00
|
|
|
return rc;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-11-19 16:03:50 +00:00
|
|
|
/* Request a string from the client over the command-fd. If GETBOOL
|
|
|
|
is set the function returns a static string (do not free) if the
|
2016-11-29 14:41:22 +01:00
|
|
|
entered value was true or NULL if the entered value was false. */
|
2003-06-05 07:14:21 +00:00
|
|
|
static char *
|
2007-11-19 16:03:50 +00:00
|
|
|
do_get_from_fd ( const char *keyword, int hidden, int getbool )
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2007-11-19 16:03:50 +00:00
|
|
|
int i, len;
|
|
|
|
char *string;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-06-07 17:04:50 +00:00
|
|
|
if (statusfp != es_stdout)
|
|
|
|
es_fflush (es_stdout);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-11-19 16:03:50 +00:00
|
|
|
write_status_text (getbool? STATUS_GET_BOOL :
|
|
|
|
hidden? STATUS_GET_HIDDEN : STATUS_GET_LINE, keyword);
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
for (string = NULL, i = len = 200; ; i++ )
|
2007-11-19 16:03:50 +00:00
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
if (i >= len-1 )
|
2007-11-19 16:03:50 +00:00
|
|
|
{
|
2017-11-22 20:54:07 +01:00
|
|
|
/* On the first iteration allocate a new buffer. If that
|
|
|
|
* buffer is too short at further iterations do a poor man's
|
|
|
|
* realloc. */
|
2007-11-19 16:03:50 +00:00
|
|
|
char *save = string;
|
|
|
|
len += 100;
|
|
|
|
string = hidden? xmalloc_secure ( len ) : xmalloc ( len );
|
|
|
|
if (save)
|
2017-11-22 20:54:07 +01:00
|
|
|
{
|
|
|
|
memcpy (string, save, i);
|
|
|
|
xfree (save);
|
|
|
|
}
|
2007-11-19 16:03:50 +00:00
|
|
|
else
|
|
|
|
i = 0;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2007-11-19 16:03:50 +00:00
|
|
|
/* Fixme: why not use our read_line function here? */
|
|
|
|
if ( myread( opt.command_fd, string+i, 1) != 1 || string[i] == '\n' )
|
|
|
|
break;
|
2011-02-04 12:57:53 +01:00
|
|
|
else if ( string[i] == CONTROL_D )
|
2007-11-19 16:03:50 +00:00
|
|
|
{
|
|
|
|
/* Found ETX - Cancel the line and return a sole ETX. */
|
|
|
|
string[0] = CONTROL_D;
|
|
|
|
i = 1;
|
|
|
|
break;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-19 16:03:50 +00:00
|
|
|
string[i] = 0;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2007-11-19 16:03:50 +00:00
|
|
|
write_status (STATUS_GOT_IT);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2007-11-19 16:03:50 +00:00
|
|
|
if (getbool) /* Fixme: is this correct??? */
|
2021-05-20 10:13:51 +02:00
|
|
|
{
|
|
|
|
char *rv = (string[0] == 'Y' || string[0] == 'y') ? "" : NULL;
|
|
|
|
xfree (string);
|
|
|
|
return rv;
|
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2007-11-19 16:03:50 +00:00
|
|
|
return string;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2022-09-13 16:34:00 +09:00
|
|
|
cpr_enabled(void)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
|
|
|
if( opt.command_fd != -1 )
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
for(;;) {
|
|
|
|
p = tty_get( prompt );
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
cpr_get( const char *keyword, const char *prompt )
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if( opt.command_fd != -1 )
|
|
|
|
return do_get_from_fd ( keyword, 0, 0 );
|
|
|
|
for(;;) {
|
|
|
|
p = tty_get( prompt );
|
|
|
|
if( *p=='?' && !p[1] && !(keyword && !*keyword)) {
|
2006-04-19 11:26:11 +00:00
|
|
|
xfree(p);
|
2003-06-05 07:14:21 +00:00
|
|
|
display_online_help( keyword );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
cpr_get_utf8( const char *keyword, const char *prompt )
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
p = cpr_get( keyword, prompt );
|
|
|
|
if( p ) {
|
|
|
|
char *utf8 = native_to_utf8( p );
|
2006-04-19 11:26:11 +00:00
|
|
|
xfree( p );
|
2003-06-05 07:14:21 +00:00
|
|
|
p = utf8;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
cpr_get_hidden( const char *keyword, const char *prompt )
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if( opt.command_fd != -1 )
|
|
|
|
return do_get_from_fd ( keyword, 1, 0 );
|
|
|
|
for(;;) {
|
|
|
|
p = tty_get_hidden( prompt );
|
|
|
|
if( *p == '?' && !p[1] ) {
|
2006-04-19 11:26:11 +00:00
|
|
|
xfree(p);
|
2003-06-05 07:14:21 +00:00
|
|
|
display_online_help( keyword );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpr_kill_prompt(void)
|
|
|
|
{
|
|
|
|
if( opt.command_fd != -1 )
|
|
|
|
return;
|
|
|
|
tty_kill_prompt();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-07-23 14:35:22 +02:00
|
|
|
cpr_get_answer_is_yes_def (const char *keyword, const char *prompt, int def_yes)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
|
|
|
int yes;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if( opt.command_fd != -1 )
|
|
|
|
return !!do_get_from_fd ( keyword, 0, 1 );
|
|
|
|
for(;;) {
|
|
|
|
p = tty_get( prompt );
|
|
|
|
trim_spaces(p); /* it is okay to do this here */
|
|
|
|
if( *p == '?' && !p[1] ) {
|
2006-04-19 11:26:11 +00:00
|
|
|
xfree(p);
|
2003-06-05 07:14:21 +00:00
|
|
|
display_online_help( keyword );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tty_kill_prompt();
|
2014-07-23 14:35:22 +02:00
|
|
|
yes = answer_is_yes_no_default (p, def_yes);
|
2006-04-19 11:26:11 +00:00
|
|
|
xfree(p);
|
2003-06-05 07:14:21 +00:00
|
|
|
return yes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 14:35:22 +02:00
|
|
|
int
|
|
|
|
cpr_get_answer_is_yes (const char *keyword, const char *prompt)
|
|
|
|
{
|
|
|
|
return cpr_get_answer_is_yes_def (keyword, prompt, 0);
|
|
|
|
}
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
int
|
|
|
|
cpr_get_answer_yes_no_quit( const char *keyword, const char *prompt )
|
|
|
|
{
|
|
|
|
int yes;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if( opt.command_fd != -1 )
|
|
|
|
return !!do_get_from_fd ( keyword, 0, 1 );
|
|
|
|
for(;;) {
|
|
|
|
p = tty_get( prompt );
|
|
|
|
trim_spaces(p); /* it is okay to do this here */
|
|
|
|
if( *p == '?' && !p[1] ) {
|
2006-04-19 11:26:11 +00:00
|
|
|
xfree(p);
|
2003-06-05 07:14:21 +00:00
|
|
|
display_online_help( keyword );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tty_kill_prompt();
|
|
|
|
yes = answer_is_yes_no_quit(p);
|
2006-04-19 11:26:11 +00:00
|
|
|
xfree(p);
|
2003-06-05 07:14:21 +00:00
|
|
|
return yes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
cpr_get_answer_okay_cancel (const char *keyword,
|
|
|
|
const char *prompt,
|
|
|
|
int def_answer)
|
|
|
|
{
|
|
|
|
int yes;
|
|
|
|
char *answer = NULL;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if( opt.command_fd != -1 )
|
|
|
|
answer = do_get_from_fd ( keyword, 0, 0 );
|
|
|
|
|
|
|
|
if (answer)
|
|
|
|
{
|
|
|
|
yes = answer_is_okay_cancel (answer, def_answer);
|
|
|
|
xfree (answer);
|
|
|
|
return yes;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
p = tty_get( prompt );
|
|
|
|
trim_spaces(p); /* it is okay to do this here */
|
|
|
|
if (*p == '?' && !p[1])
|
|
|
|
{
|
|
|
|
xfree(p);
|
|
|
|
display_online_help (keyword);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tty_kill_prompt();
|
|
|
|
yes = answer_is_okay_cancel (p, def_answer);
|
|
|
|
xfree(p);
|
|
|
|
return yes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|