diff options
author | Werner Koch <[email protected]> | 2024-10-23 08:40:36 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2024-10-23 09:43:08 +0000 |
commit | 18081e2ecf43de2be6ad5a7ca3384e1e2b66914d (patch) | |
tree | 85aa3365b947da5bc7da0cea5610a60da6517eb3 /sm/server.c | |
parent | agent: Fix resource leak for PRIMARY_CTX. (diff) | |
download | gnupg-18081e2ecf43de2be6ad5a7ca3384e1e2b66914d.tar.gz gnupg-18081e2ecf43de2be6ad5a7ca3384e1e2b66914d.zip |
gpgsm: Terminate key listing on output write error.
* sm/keylist.c (list_internal_keys): Detect write errors to the output
stream.
* sm/server.c (any_failure_printed): New var.
(gpgsm_status2): Handle new var. Move statusfp init to ...
(gpgsm_init_statusfp): new function.
(gpgsm_exit_failure_status): New.
* sm/gpgsm.c (main): Explicit statusfp init.
(gpgsm_exit): Print failure status on error.
--
Test by using
gpgsm -k >/dev/full
gpgsm -k --wit-colons >/dev/full
and also by redirecting to a file on a small partition.
GnuPG-bug-id: 6185
Diffstat (limited to 'sm/server.c')
-rw-r--r-- | sm/server.c | 62 |
1 files changed, 47 insertions, 15 deletions
diff --git a/sm/server.c b/sm/server.c index cb3fae24d..345c3167b 100644 --- a/sm/server.c +++ b/sm/server.c @@ -37,6 +37,11 @@ #define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t)) +/* Used to track whether we printed any FAILURE status in non-server + * mode. */ +static int any_failure_printed; + + /* The filepointer for status message used in non-server mode */ static FILE *statusfp; @@ -1515,6 +1520,24 @@ gpgsm_server (certlist_t default_recplist) } +void +gpgsm_init_statusfp (ctrl_t ctrl) +{ + if (statusfp || ctrl->status_fd == -1) + return; + + if (ctrl->status_fd == 1) + statusfp = stdout; + else if (ctrl->status_fd == 2) + statusfp = stderr; + else + statusfp = fdopen (ctrl->status_fd, "w"); + + if (!statusfp) + log_fatal ("can't open fd %d for status output: %s\n", + ctrl->status_fd, strerror(errno)); +} + gpg_error_t gpgsm_status2 (ctrl_t ctrl, int no, ...) @@ -1527,23 +1550,11 @@ gpgsm_status2 (ctrl_t ctrl, int no, ...) if (ctrl->no_server && ctrl->status_fd == -1) ; /* No status wanted. */ + else if (ctrl->no_server && no == STATUS_FAILURE && any_failure_printed) + ; /* Don't emit FAILURE a second time. */ else if (ctrl->no_server) { - if (!statusfp) - { - if (ctrl->status_fd == 1) - statusfp = stdout; - else if (ctrl->status_fd == 2) - statusfp = stderr; - else - statusfp = fdopen (ctrl->status_fd, "w"); - - if (!statusfp) - { - log_fatal ("can't open fd %d for status output: %s\n", - ctrl->status_fd, strerror(errno)); - } - } + gpgsm_init_statusfp (ctrl); fputs ("[GNUPG:] ", statusfp); fputs (get_status_string (no), statusfp); @@ -1562,6 +1573,10 @@ gpgsm_status2 (ctrl_t ctrl, int no, ...) } } putc ('\n', statusfp); + + if (no == STATUS_FAILURE) + any_failure_printed = 1; + if (ferror (statusfp)) err = gpg_error_from_syserror (); else @@ -1614,6 +1629,23 @@ gpgsm_status_with_error (ctrl_t ctrl, int no, const char *text, } +/* Function to print a FAILURE status line on exit. */ +void +gpgsm_exit_failure_status (void) +{ + /* stderr is used as a last but possible wrong resort if no status + * has yet been printed. */ + FILE *fp = statusfp? statusfp : stderr; + + if (any_failure_printed) + return; + + fputs ("[GNUPG:] ", fp); + fputs (get_status_string (STATUS_FAILURE), fp); + fputs (" gpgsm-exit 50331649\n", fp); +} + + /* This callback is used to emit progress status lines. */ gpg_error_t gpgsm_progress_cb (ctrl_t ctrl, uint64_t current, uint64_t total) |