From 87e271dc838fd37f47a2ba60c2742080ed654bb5 Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Mon, 13 Apr 2009 20:26:33 -0400 Subject: [PATCH] Added pwmd_disconnect() to close the connection without freeing the resources. This will also keep any options set for the handle. --- NEWS | 2 +- doc/libpwmd.3 | 42 ++++++++++++++++++++++++++++++++++++------ src/libpwmd.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------ src/libpwmd.h.in | 27 ++++++++++++++++++++++----- src/ssh.c | 6 ++++++ src/ssh.h | 3 +++ 6 files changed, 116 insertions(+), 18 deletions(-) diff --git a/NEWS b/NEWS index e0b68335..b4faa557 100644 --- a/NEWS +++ b/NEWS @@ -16,7 +16,7 @@ how pwmd_process() works. Here are the API changes: pwmd_ssh_connect(), pwmd_ssh_connect_async(), pwmd_get_hostkey(), pwmd_get_hostkey_async(), pwmd_strerror_r(), pwmd_open2(), pwmd_save2(), pwmd_connect_url(), pwmd_connect_url_async(), - pwmd_socket_type() + pwmd_socket_type(), pwmd_disconnect() Options removed: PWMD_OPTION_PINENTRY Options added: PWMD_OPTION_IP_VERSION diff --git a/doc/libpwmd.3 b/doc/libpwmd.3 index efe6570b..d3242c43 100644 --- a/doc/libpwmd.3 +++ b/doc/libpwmd.3 @@ -1,4 +1,4 @@ -.TH "libpwmd.h" 3 "12 Apr 2009" "Version 6.0.0" "libpwmd" \" -*- nroff -*- +.TH "libpwmd.h" 3 "13 Apr 2009" "Version 6.0.0" "libpwmd" \" -*- nroff -*- .ad l .nh .SH NAME @@ -270,6 +270,9 @@ The following example will list the element tree of the data file specified in t .RI "gpg_error_t \fBpwmd_inquire\fP (\fBpwm_t\fP *pwm, const char *cmd, \fBpwmd_inquire_cb_t\fP func, void *user)" .br .ti -1c +.RI "gpg_error_t \fBpwmd_disconnect\fP (\fBpwm_t\fP *pwm)" +.br +.ti -1c .RI "void \fBpwmd_close\fP (\fBpwm_t\fP *pwm)" .br .ti -1c @@ -611,6 +614,11 @@ This will close the connection to a pwmd server and free any resources associate Nothing. .RE .PP +\fBSee also:\fP +.RS 4 +\fBpwmd_disconnect()\fP +.RE +.PP .SS "gpg_error_t pwmd_command (\fBpwm_t\fP * pwm, char ** result, const char * cmd, ...)" .PP @@ -681,7 +689,7 @@ When the first character of a filename parameter is a tilde (~), it will be expa .PP \fBSee also:\fP .RS 4 -\fBpwmd_ssh_connect()\fP, \fBpwmd_ssh_connect_async()\fP +\fBpwmd_ssh_connect()\fP, \fBpwmd_ssh_connect_async()\fP, \fBpwmd_disconnect()\fP .RE .PP @@ -722,7 +730,7 @@ When the first character of a filename parameter is a tilde (~), it will be expa .PP \fBSee also:\fP .RS 4 -\fBpwmd_socket_type()\fP +\fBpwmd_socket_type()\fP, \fBpwmd_disconnect()\fP .RE .PP @@ -765,7 +773,29 @@ When the first character of a filename parameter is a tilde (~), it will be expa .PP \fBSee also:\fP .RS 4 -\fBpwmd_socket_type()\fP +\fBpwmd_socket_type()\fP, \fBpwmd_disconnect()\fP +.RE +.PP + +.SS "gpg_error_t pwmd_disconnect (\fBpwm_t\fP * pwm)" +.PP +Close a connection to the pwmd server. +.PP +This will close the connection but keep any previously set options for the specified handle. +.PP +\fBParameters:\fP +.RS 4 +\fIpwm\fP A handle. +.RE +.PP +\fBReturns:\fP +.RS 4 +0 on success or an error code. +.RE +.PP +\fBSee also:\fP +.RS 4 +\fBpwmd_close()\fP .RE .PP @@ -1327,7 +1357,7 @@ When the first character of a filename parameter is a tilde (~), it will be expa .PP \fBSee also:\fP .RS 4 -\fBpwmd_ssh_connect_async()\fP, \fBPWMD_OPTION_IP_VERSION\fP, \fBSSH Details\fP +\fBpwmd_ssh_connect_async()\fP, \fBPWMD_OPTION_IP_VERSION\fP, \fBpwmd_disconnect()\fP, \fBSSH Details\fP .RE .PP @@ -1366,7 +1396,7 @@ When the first character of a filename parameter is a tilde (~), it will be expa .PP \fBSee also:\fP .RS 4 -\fBpwmd_process()\fP, \fBPWMD_OPTION_IP_VERSION\fP, \fBSSH Details\fP +\fBpwmd_process()\fP, \fBPWMD_OPTION_IP_VERSION\fP, \fBpwmd_disconnect()\fP, \fBSSH Details\fP .RE .PP diff --git a/src/libpwmd.c b/src/libpwmd.c index 2a7fcdc7..629aeac7 100644 --- a/src/libpwmd.c +++ b/src/libpwmd.c @@ -299,13 +299,22 @@ gpg_error_t pwmd_connect(pwm_t *pwm, const char *path) return _connect_finalize(pwm); } +static void disconnect(pwm_t *pwm) +{ + if (!pwm || !pwm->ctx) + return; + + assuan_disconnect(pwm->ctx); + pwm->ctx = NULL; + pwm->fd = -1; +} + void pwmd_close(pwm_t *pwm) { if (!pwm) return; - if (pwm->ctx) - assuan_disconnect(pwm->ctx); + disconnect(pwm); if (pwm->password) pwmd_free(pwm->password); @@ -676,6 +685,42 @@ static gpg_error_t _pwmd_process(pwm_t *pwm) return gpg_err_code(rc); } +static void reset_handle(pwm_t *h) +{ + h->fd = -1; +#ifdef WITH_PINENTRY + if (h->pctx) + _pinentry_disconnect(h); + + h->nb_fd = -1; +#endif + h->pin_try = 0; + reset_async(h, 0); +} + +gpg_error_t pwmd_disconnect(pwm_t *pwm) +{ + if (!pwm) + return GPG_ERR_INV_ARG; + +#ifdef WITH_TCP + if (pwm->fd == -1 && pwm->tcp_conn && pwm->tcp_conn->fd == -1) +#else + if (pwm->fd == -1) +#endif + return GPG_ERR_INV_STATE; + + if (pwm->fd != 1) + disconnect(pwm); +#ifdef WITH_TCP + else + _ssh_disconnect(pwm); +#endif + + reset_handle(pwm); + return 0; +} + pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc, char **result) { #if defined(WITH_PINENTRY) || defined(WITH_TCP) @@ -1738,10 +1783,7 @@ pwm_t *pwmd_new(const char *name) } } - h->fd = -1; -#ifdef WITH_PINENTRY - h->nb_fd = -1; -#endif + reset_handle(h); h->pinentry_timeout = -30; h->pinentry_tries = 3; #ifdef WITH_TCP diff --git a/src/libpwmd.h.in b/src/libpwmd.h.in index b8e11ddc..ba61650f 100644 --- a/src/libpwmd.h.in +++ b/src/libpwmd.h.in @@ -453,7 +453,7 @@ pwm_t *pwmd_new(const char *name) * \a "~/.pwmd/socket" will be used. * \return 0 on success or an error code. * \filepath - * \see pwmd_ssh_connect(), pwmd_ssh_connect_async() + * \see pwmd_ssh_connect(), pwmd_ssh_connect_async(), pwmd_disconnect() */ gpg_error_t pwmd_connect(pwm_t *pwm, const char *path) __attribute__ ((warn_unused_result)); @@ -475,7 +475,8 @@ gpg_error_t pwmd_connect(pwm_t *pwm, const char *path) * format which may be obtained with \ref pwmd_get_hostkey(). * \return 0 on success or an error code. * \filepath - * \see pwmd_ssh_connect_async(), \ref PWMD_OPTION_IP_VERSION, \ref ssh + * \see pwmd_ssh_connect_async(), \ref PWMD_OPTION_IP_VERSION, + * pwmd_disconnect(), \ref ssh */ gpg_error_t pwmd_ssh_connect(pwm_t *pwm, const char *host, int port, const char *identity, const char *user, const char *known_hosts) @@ -501,7 +502,8 @@ gpg_error_t pwmd_ssh_connect(pwm_t *pwm, const char *host, int port, * format which may be obtained with \ref pwmd_get_hostkey(). * \return 0 on success or an error code. * \filepath - * \see pwmd_process(), \ref PWMD_OPTION_IP_VERSION, \ref ssh + * \see pwmd_process(), \ref PWMD_OPTION_IP_VERSION, pwmd_disconnect(), + * \ref ssh */ gpg_error_t pwmd_ssh_connect_async(pwm_t *pwm, const char *host, int port, const char *identity, const char *user, const char *known_hosts) @@ -529,7 +531,7 @@ gpg_error_t pwmd_ssh_connect_async(pwm_t *pwm, const char *host, int port, * \param url The string to parse. * \filepath * \return 0 on success or an error code. - * \see \ref pwmd_socket_type() + * \see \ref pwmd_socket_type(), pwmd_disconnect() */ gpg_error_t pwmd_connect_url(pwm_t *pwm, const char *url) __attribute__ ((warn_unused_result)); @@ -558,7 +560,7 @@ gpg_error_t pwmd_connect_url(pwm_t *pwm, const char *url) * \param url The string to parse. * \filepath * \return 0 on success or an error code. - * \see \ref pwmd_socket_type() + * \see \ref pwmd_socket_type(), pwmd_disconnect() */ gpg_error_t pwmd_connect_url_async(pwm_t *pwm, const char *url) __attribute__ ((warn_unused_result)); @@ -870,6 +872,19 @@ gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_cb_t func, __attribute__ ((warn_unused_result)); +/*! \brief Close a connection to the pwmd server. + * + * This will close the connection but keep any previously set options for the + * specified handle. + * + * \param pwm A handle. + * \return 0 on success or an error code. + * \see \ref pwmd_close() + */ +gpg_error_t pwmd_disconnect(pwm_t *pwm) + __attribute__ ((warn_unused_result)); + + /*! \brief Close a handle. * * This will close the connection to a pwmd server and free any resources @@ -877,9 +892,11 @@ gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_cb_t func, * * \param pwm A handle. * \return Nothing. + * \see \ref pwmd_disconnect() */ void pwmd_close(pwm_t *pwm); + /*! \brief The type of connection a handle has. * * Useful when you want to know what kind of connection a handle has. diff --git a/src/ssh.c b/src/ssh.c index e23afd8e..4665ab18 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -700,3 +700,9 @@ gpg_error_t _parse_ssh_url(char *str, char **host, int *port, char **user, return 0; } + +void _ssh_disconnect(pwm_t *pwm) +{ + ssh_deinit(pwm->tcp_conn); + pwm->tcp_conn = NULL; +} diff --git a/src/ssh.h b/src/ssh.h index 671a41be..1b82c012 100644 --- a/src/ssh.h +++ b/src/ssh.h @@ -41,4 +41,7 @@ gpg_error_t _parse_ssh_url(char *str, char **host, int *port, char **user, char **identity, char **known_hosts) __attribute__ ((visibility ("hidden"))); +void _ssh_disconnect(pwm_t *pwm) + __attribute__ ((visibility ("hidden"))); + #endif -- 2.11.4.GIT