1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2009 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
26 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <sys/select.h>
39 #include <netinet/in.h>
40 #include <sys/socket.h>
52 #define DNS_USE_GETTIMEOFDAY_FOR_ID 1
54 #include <arpa/nameser.h>
66 #define N_(msgid) dgettext("libpwmd", msgid)
73 static int gelapsed
, gtimeout
;
74 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
77 static char *_getpwuid(struct passwd
*pwd
)
79 size_t size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
80 struct passwd
*result
;
87 buf
= pwmd_malloc(size
);
92 n
= getpwuid_r(getuid(), pwd
, buf
, size
, &result
);
109 const char *_pwmd_strerror(gpg_error_t e
)
111 gpg_err_code_t code
= gpg_err_code(e
);
113 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
118 return N_("Unknown error");
120 return N_("No cache slots available");
122 return N_("Recursion loop");
124 return N_("No file is open");
126 return N_("General LibXML error");
128 return N_("File modified");
130 return N_("Access denied");
137 const char *pwmd_strerror(gpg_error_t e
)
139 const char *p
= _pwmd_strerror(e
);
141 return p
? p
: gpg_strerror(e
);
144 int pwmd_strerror_r(gpg_error_t e
, char *buf
, size_t size
)
146 const char *p
= _pwmd_strerror(e
);
149 snprintf(buf
, size
, "%s", p
);
151 if (strlen(p
) > size
)
157 return gpg_strerror_r(e
, buf
, size
);
160 gpg_error_t
pwmd_init()
162 static int initialized
;
171 bindtextdomain("libpwmd", LOCALEDIR
);
174 assuan_set_malloc_hooks(pwmd_malloc
, pwmd_realloc
, pwmd_free
);
175 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
180 static gpg_error_t
_socket_connect_finalize(pwm_t
*pwm
)
183 int n
= assuan_get_active_fds(pwm
->ctx
, 0, active
, N_ARRAY(active
));
185 pwm
->fd
= n
<= 0 ? -1 : dup(active
[0]);
188 pwm
->pinentry_tries
= 3;
190 assuan_set_pointer(pwm
->ctx
, pwm
);
193 return pwmd_command(pwm
, NULL
, "OPTION CLIENT NAME=%s", pwm
->name
);
199 static int read_hook(assuan_context_t ctx
, assuan_fd_t fd
, void *data
,
200 size_t len
, ssize_t
*ret
)
202 pwm_t
*pwm
= assuan_get_pointer(ctx
);
204 if (!pwm
|| !pwm
->tcp_conn
)
206 *ret
= pth_read((int)fd
, data
, len
);
208 *ret
= read((int)fd
, data
, len
);
212 *ret
= libssh2_channel_read(pwm
->tcp_conn
->channel
, data
, len
);
213 } while (*ret
== LIBSSH2_ERROR_EAGAIN
);
216 return *ret
<= 0 ? 0 : 1;
219 static int write_hook(assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
220 size_t len
, ssize_t
*ret
)
222 pwm_t
*pwm
= assuan_get_pointer(ctx
);
224 if (!pwm
|| !pwm
->tcp_conn
)
226 *ret
= pth_write((int)fd
, data
, len
);
228 *ret
= write((int)fd
, data
, len
);
232 *ret
= libssh2_channel_write(pwm
->tcp_conn
->channel
, data
, len
);
233 } while (*ret
== LIBSSH2_ERROR_EAGAIN
);
236 return *ret
<= 0 ? 0 : 1;
239 static void _ssh_deinit(pwmd_tcp_conn_t
*conn
);
240 static void free_tcp_conn(pwmd_tcp_conn_t
*conn
)
245 if (conn
->username
) {
246 pwmd_free(conn
->username
);
247 conn
->username
= NULL
;
250 if (conn
->known_hosts
) {
251 pwmd_free(conn
->known_hosts
);
252 conn
->known_hosts
= NULL
;
255 if (conn
->identity
) {
256 pwmd_free(conn
->identity
);
257 conn
->identity
= NULL
;
260 if (conn
->identity_pub
) {
261 pwmd_free(conn
->identity_pub
);
262 conn
->identity_pub
= NULL
;
266 pwmd_free(conn
->host
);
271 pwmd_free(conn
->hostkey
);
272 conn
->hostkey
= NULL
;
276 ares_destroy(conn
->chan
);
281 ares_free_hostent(conn
->he
);
296 static void _ssh_deinit(pwmd_tcp_conn_t
*conn
)
302 libssh2_channel_free(conn
->channel
);
305 libssh2_session_disconnect(conn
->session
, "Bye!");
306 libssh2_session_free(conn
->session
);
309 conn
->session
= NULL
;
310 conn
->channel
= NULL
;
314 static void _ssh_assuan_deinit(assuan_context_t ctx
)
316 pwm_t
*pwm
= assuan_get_pointer(ctx
);
318 _ssh_deinit(pwm
->tcp_conn
);
319 pwm
->tcp_conn
= NULL
;
323 * Sets common options from both pwmd_tcp_connect() and
324 * pwmd_tcp_connect_async().
326 static gpg_error_t
init_tcp_conn(pwmd_tcp_conn_t
**dst
, const char *host
,
327 int port
, const char *identity
, const char *user
, const char *hosts
,
330 pwmd_tcp_conn_t
*conn
;
335 return GPG_ERR_INV_ARG
;
338 if (!host
|| !identity
|| !hosts
)
339 return GPG_ERR_INV_ARG
;
342 conn
= pwmd_calloc(1, sizeof(pwmd_tcp_conn_t
));
345 return gpg_error_from_errno(ENOMEM
);
347 conn
->port
= port
== -1 ? 22 : port
;
348 conn
->host
= pwmd_strdup(host
);
351 rc
= gpg_error_from_errno(ENOMEM
);
357 char *pwbuf
= _getpwuid(&pw
);
360 rc
= gpg_error_from_errno(errno
);
364 conn
->username
= pwmd_strdup(user
? user
: pw
.pw_name
);
367 if (!conn
->username
) {
368 rc
= gpg_error_from_errno(ENOMEM
);
372 conn
->identity
= pwmd_strdup(identity
);
374 if (!conn
->identity
) {
375 rc
= gpg_error_from_errno(ENOMEM
);
379 conn
->identity_pub
= pwmd_malloc(strlen(conn
->identity
)+5);
381 if (!conn
->identity_pub
) {
382 rc
= gpg_error_from_errno(ENOMEM
);
386 sprintf(conn
->identity_pub
, "%s.pub", conn
->identity
);
387 conn
->known_hosts
= pwmd_strdup(hosts
);
389 if (!conn
->known_hosts
) {
390 rc
= gpg_error_from_errno(ENOMEM
);
403 static gpg_error_t
do_connect(pwm_t
*pwm
, int prot
, void *addr
)
405 struct sockaddr_in their_addr
;
407 pwm
->tcp_conn
->fd
= socket(prot
, SOCK_STREAM
, 0);
409 if (pwm
->tcp_conn
->fd
== -1)
410 return gpg_error_from_syserror();
412 if (pwm
->tcp_conn
->async
)
413 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, O_NONBLOCK
);
415 pwm
->cmd
= ASYNC_CMD_CONNECT
;
416 their_addr
.sin_family
= prot
;
417 their_addr
.sin_port
= htons(pwm
->tcp_conn
->port
);
418 their_addr
.sin_addr
= *((struct in_addr
*)addr
);
419 memset(their_addr
.sin_zero
, '\0', sizeof their_addr
.sin_zero
);
422 if (pth_connect(pwm
->tcp_conn
->fd
, (struct sockaddr
*)&their_addr
,
423 sizeof(their_addr
)) == -1)
425 if (connect(pwm
->tcp_conn
->fd
, (struct sockaddr
*)&their_addr
,
426 sizeof(their_addr
)) == -1)
428 return gpg_error_from_syserror();
433 static gpg_error_t
ares_error_to_pwmd(int status
)
435 if (status
!= ARES_SUCCESS
)
436 warnx("%s", ares_strerror(status
));
442 return GPG_ERR_UNKNOWN_HOST
;
444 return GPG_ERR_EHOSTDOWN
;
446 return GPG_ERR_TIMEOUT
;
448 return gpg_error_from_errno(ENOMEM
);
449 case ARES_ECONNREFUSED
:
450 return GPG_ERR_ECONNREFUSED
;
453 return GPG_ERR_EHOSTUNREACH
;
459 static void dns_resolve_cb(void *arg
, int status
, int timeouts
,
460 unsigned char *abuf
, int alen
)
466 if (status
== ARES_EDESTRUCTION
)
469 if (status
!= ARES_SUCCESS
) {
470 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
474 //FIXME localhost. works with ipv4. maybe local system config error
475 /* Check for an IPv6 address first. */
476 rc
= ares_parse_a_reply(abuf
, alen
, &he
, NULL
, NULL
);
478 if (rc
!= ARES_SUCCESS
) {
479 if (rc
!= ARES_ENODATA
) {
480 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
484 rc
= ares_parse_aaaa_reply(abuf
, alen
, &he
, NULL
, NULL
);
486 if (rc
!= ARES_SUCCESS
) {
487 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
492 pwm
->tcp_conn
->he
= he
;
493 pwm
->tcp_conn
->rc
= do_connect(pwm
, he
->h_addrtype
, he
->h_addr
);
496 static gpg_error_t
_do_pwmd_tcp_connect_async(pwm_t
*pwm
, const char *host
,
497 int port
, const char *identity
, const char *user
,
498 const char *known_hosts
, pwmd_async_cmd_t which
)
500 pwmd_tcp_conn_t
*conn
;
503 rc
= init_tcp_conn(&conn
, host
, port
, identity
, user
, known_hosts
,
504 which
== ASYNC_CMD_HOSTKEY
? 1 : 0);
510 pwm
->tcp_conn
= conn
;
511 pwm
->tcp_conn
->cmd
= which
;
513 if (pwm
->tcp_conn
->cmd
== ASYNC_CMD_HOSTKEY
)
514 pwm
->tcp_conn
->get_only
= 1;
516 pwm
->cmd
= ASYNC_CMD_DNS
;
517 pwm
->state
= ASYNC_PROCESS
;
518 ares_init(&pwm
->tcp_conn
->chan
);
519 ares_query(pwm
->tcp_conn
->chan
, pwm
->tcp_conn
->host
, ns_c_any
, ns_t_any
,
520 dns_resolve_cb
, pwm
);
524 gpg_error_t
pwmd_tcp_connect_async(pwm_t
*pwm
, const char *host
, int port
,
525 const char *identity
, const char *user
, const char *known_hosts
)
527 return _do_pwmd_tcp_connect_async(pwm
, host
, port
, identity
, user
,
528 known_hosts
, ASYNC_CMD_CONNECT
);
531 static void *_ssh_malloc(size_t size
, void **data
)
533 return pwmd_malloc(size
);
536 static void _ssh_free(void *ptr
, void **data
)
541 static void *_ssh_realloc(void *ptr
, size_t size
, void **data
)
543 return pwmd_realloc(ptr
, size
);
546 static char *to_hex(const char *str
, size_t slen
)
549 char *buf
= pwmd_malloc(slen
*2+1);
554 for (i
= 0, buf
[0] = 0; i
< slen
; i
++) {
557 sprintf(tmp
, "%02x", (unsigned char)str
[i
]);
564 static int verify_host_key(pwm_t
*pwm
)
566 FILE *fp
= fopen(pwm
->tcp_conn
->known_hosts
, "r");
572 buf
= pwmd_malloc(LINE_MAX
);
577 while ((p
= fgets(buf
, LINE_MAX
, fp
))) {
578 if (*p
== '#' || isspace(*p
))
581 if (p
[strlen(p
)-1] == '\n')
584 if (!strcmp(buf
, pwm
->tcp_conn
->hostkey
))
601 static gpg_error_t
authenticate_ssh(pwm_t
*pwm
)
603 const char *fp
= libssh2_hostkey_hash(pwm
->tcp_conn
->session
,
604 LIBSSH2_HOSTKEY_HASH_SHA1
);
607 pwm
->tcp_conn
->hostkey
= to_hex(fp
, 20);
609 if (!pwm
->tcp_conn
->hostkey
)
610 return gpg_error_from_errno(ENOMEM
);
612 if (pwm
->tcp_conn
->get_only
)
615 if (!fp
|| verify_host_key(pwm
))
616 return GPG_ERR_CHECKSUM
;
618 userauth
= libssh2_userauth_list(pwm
->tcp_conn
->session
,
619 pwm
->tcp_conn
->username
, strlen(pwm
->tcp_conn
->username
));
621 if (!userauth
|| !strstr(userauth
, "publickey"))
622 return GPG_ERR_BAD_PIN_METHOD
;
624 if (libssh2_userauth_publickey_fromfile(pwm
->tcp_conn
->session
,
625 pwm
->tcp_conn
->username
, pwm
->tcp_conn
->identity_pub
,
626 pwm
->tcp_conn
->identity
, NULL
))
627 return GPG_ERR_BAD_SECKEY
;
632 static gpg_error_t
setup_tcp_session(pwm_t
*pwm
)
634 assuan_context_t ctx
;
635 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
638 pwm
->tcp_conn
->session
= libssh2_session_init_ex(_ssh_malloc
, _ssh_free
,
641 if (!pwm
->tcp_conn
->session
) {
642 rc
= gpg_error_from_errno(ENOMEM
);
646 if (libssh2_session_startup(pwm
->tcp_conn
->session
, pwm
->tcp_conn
->fd
)) {
647 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
651 rc
= authenticate_ssh(pwm
);
656 /* pwmd_get_hostkey(). */
657 if (pwm
->tcp_conn
->get_only
) {
658 pwm
->result
= pwmd_strdup(pwm
->tcp_conn
->hostkey
);
661 rc
= gpg_error_from_errno(ENOMEM
);
668 pwm
->tcp_conn
->channel
= libssh2_channel_open_session(pwm
->tcp_conn
->session
);
670 if (!pwm
->tcp_conn
->channel
) {
671 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
675 if (libssh2_channel_shell(pwm
->tcp_conn
->channel
)) {
676 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
680 assuan_set_io_hooks(&io_hooks
);
681 rc
= assuan_socket_connect_fd(&ctx
, pwm
->tcp_conn
->fd
, 0, pwm
);
686 assuan_set_finish_handler(ctx
, _ssh_assuan_deinit
);
688 return _socket_connect_finalize(pwm
);
691 free_tcp_conn(pwm
->tcp_conn
);
692 pwm
->tcp_conn
= NULL
;
696 static gpg_error_t
_do_pwmd_tcp_connect(pwm_t
*pwm
, const char *host
, int port
,
697 const char *identity
, const char *user
, const char *known_hosts
, int get
)
699 pwmd_tcp_conn_t
*conn
;
702 rc
= init_tcp_conn(&conn
, host
, port
, identity
, user
, known_hosts
, get
);
707 pwm
->tcp_conn
= conn
;
708 pwm
->tcp_conn
->get_only
= get
;
709 pwm
->cmd
= ASYNC_CMD_DNS
;
710 ares_init(&pwm
->tcp_conn
->chan
);
711 ares_query(pwm
->tcp_conn
->chan
, pwm
->tcp_conn
->host
, ns_c_any
, ns_t_any
,
712 dns_resolve_cb
, pwm
);
714 /* dns_resolve_cb() may have already been called. */
715 if (pwm
->tcp_conn
->rc
) {
716 rc
= pwm
->tcp_conn
->rc
;
721 * Fake a blocking DNS lookup. libcares does a better job than
731 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
732 ares_timeout(pwm
->tcp_conn
->chan
, NULL
, &tv
);
734 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
736 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
740 rc
= gpg_error_from_syserror();
744 rc
= GPG_ERR_TIMEOUT
;
748 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
750 if (pwm
->tcp_conn
->rc
)
752 } while (pwm
->cmd
== ASYNC_CMD_DNS
);
754 if (pwm
->tcp_conn
->rc
) {
755 rc
= pwm
->tcp_conn
->rc
;
759 return setup_tcp_session(pwm
);
765 gpg_error_t
pwmd_tcp_connect(pwm_t
*pwm
, const char *host
, int port
,
766 const char *identity
, const char *user
, const char *known_hosts
)
768 return _do_pwmd_tcp_connect(pwm
, host
, port
, identity
, user
, known_hosts
, 0);
771 /* Must free the result with pwmd_free(). */
772 gpg_error_t
pwmd_get_hostkey(const char *host
, int port
, char **result
)
775 pwm_t
*pwm
= pwmd_new(NULL
);
779 return gpg_error_from_errno(ENOMEM
);
781 rc
= _do_pwmd_tcp_connect(pwm
, host
, port
, NULL
, NULL
, NULL
, 1);
788 hostkey
= pwmd_strdup(pwm
->tcp_conn
->hostkey
);
791 rc
= gpg_error_from_errno(ENOMEM
);
798 gpg_error_t
pwmd_get_hostkey_async(pwm_t
*pwm
, const char *host
, int port
)
800 return _do_pwmd_tcp_connect_async(pwm
, host
, port
, NULL
, NULL
, NULL
,
805 gpg_error_t
pwmd_connect(pwm_t
*pwm
, const char *path
)
807 char *socketpath
= NULL
;
808 assuan_context_t ctx
;
810 char *pwbuf
= _getpwuid(&pw
);
814 return gpg_error_from_errno(errno
);
817 socketpath
= (char *)pwmd_malloc(strlen(pw
.pw_dir
) + strlen("/.pwmd/socket") + 1);
818 sprintf(socketpath
, "%s/.pwmd/socket", pw
.pw_dir
);
821 socketpath
= pwmd_strdup(path
);
824 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
825 pwmd_free(socketpath
);
831 return _socket_connect_finalize(pwm
);
834 void pwmd_close(pwm_t
*pwm
)
840 assuan_disconnect(pwm
->ctx
);
843 pwmd_free(pwm
->password
);
846 pwmd_free(pwm
->title
);
849 pwmd_free(pwm
->desc
);
852 pwmd_free(pwm
->prompt
);
854 if (pwm
->pinentry_tty
)
855 pwmd_free(pwm
->pinentry_tty
);
857 if (pwm
->pinentry_display
)
858 pwmd_free(pwm
->pinentry_display
);
860 if (pwm
->pinentry_term
)
861 pwmd_free(pwm
->pinentry_term
);
864 pwmd_free(pwm
->lcctype
);
867 pwmd_free(pwm
->lcmessages
);
870 pwmd_free(pwm
->filename
);
873 pwmd_free(pwm
->name
);
877 free_tcp_conn(pwm
->tcp_conn
);
883 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
885 membuf_t
*mem
= (membuf_t
*)data
;
891 if ((p
= pwmd_realloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
895 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
900 static int _inquire_cb(void *data
, const char *keyword
)
902 pwm_t
*pwm
= (pwm_t
*)data
;
904 int flags
= fcntl(pwm
->fd
, F_GETFL
);
906 /* Shouldn't get this far without a callback. */
907 if (!pwm
->inquire_func
)
908 return GPG_ERR_INV_ARG
;
911 * Since the socket file descriptor is probably set to non-blocking, set to
912 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
913 * asynchronous INQUIRE is supported by either libassuan or a later
916 fcntl(pwm
->fd
, F_SETFL
, 0);
923 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
, &len
);
924 rc
= gpg_err_code(rc
);
926 if (rc
== GPG_ERR_EOF
|| !rc
) {
927 if (len
<= 0 || !result
|| !*result
) {
932 arc
= assuan_send_data(pwm
->ctx
, result
, len
);
934 if (rc
== GPG_ERR_EOF
) {
945 fcntl(pwm
->fd
, F_SETFL
, flags
);
949 static gpg_error_t
do_nb_command(pwm_t
*pwm
, const char *cmd
, const char *arg
)
953 size_t len
= strlen(cmd
) + 2;
955 len
+= arg
? strlen(arg
) : 0;
957 if (pwm
->state
== ASYNC_DONE
)
958 pwm
->state
= ASYNC_INIT
;
960 if (pwm
->state
!= ASYNC_INIT
)
961 return GPG_ERR_INV_STATE
;
963 buf
= (char *)pwmd_malloc(len
);
966 rc
= gpg_error_from_errno(ENOMEM
);
970 snprintf(buf
, len
, "%s %s", cmd
, arg
? arg
: "");
971 rc
= assuan_write_line(pwm
->ctx
, buf
);
975 pwm
->state
= ASYNC_PROCESS
;
981 gpg_error_t
pwmd_open_async(pwm_t
*pwm
, const char *filename
)
983 if (!pwm
|| !filename
)
984 return GPG_ERR_INV_ARG
;
986 /* For pinentry retries. */
987 if (!pwm
->is_open_cmd
) {
989 pwmd_free(pwm
->filename
);
991 pwm
->filename
= pwmd_strdup(filename
);
994 pwm
->is_open_cmd
= 1;
995 pwm
->cmd
= ASYNC_CMD_OPEN
;
996 return do_nb_command(pwm
, "OPEN", filename
);
999 gpg_error_t
pwmd_save_async(pwm_t
*pwm
)
1002 return GPG_ERR_INV_ARG
;
1004 pwm
->cmd
= ASYNC_CMD_SAVE
;
1005 return do_nb_command(pwm
, "SAVE", NULL
);
1008 static gpg_error_t
parse_assuan_line(pwm_t
*pwm
)
1014 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
1017 if (line
[0] == 'O' && line
[1] == 'K' &&
1018 (line
[2] == 0 || line
[2] == ' ')) {
1019 pwm
->state
= ASYNC_DONE
;
1021 else if (line
[0] == '#') {
1023 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' ')) {
1024 if (pwm
->status_func
) {
1025 pwm
->status_func(pwm
->status_data
,
1026 line
[1] == 0 ? line
+1 : line
+2);
1029 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
1030 (line
[3] == 0 || line
[3] == ' ')) {
1033 pwm
->state
= ASYNC_DONE
;
1040 gpg_error_t
pwmd_pending_line(pwm_t
*pwm
)
1043 return GPG_ERR_INV_ARG
;
1046 return GPG_ERR_INV_STATE
;
1048 return assuan_pending_line(pwm
->ctx
) ? 0 : GPG_ERR_NO_DATA
;
1051 static pwmd_async_t
reset_async(pwm_t
*pwm
, int done
)
1053 pwm
->state
= ASYNC_INIT
;
1054 pwm
->cmd
= ASYNC_CMD_NONE
;
1056 #ifdef WITH_PINENTRY
1057 pwm
->is_open_cmd
= 0;
1059 if (pwm
->nb_fd
!= -1) {
1065 if (done
&& pwm
->tcp_conn
&& pwm
->tcp_conn
->fd
!= -1) {
1066 close(pwm
->tcp_conn
->fd
);
1067 pwm
->tcp_conn
->fd
= -1;
1074 pwmd_async_t
pwmd_process(pwm_t
*pwm
, gpg_error_t
*rc
, char **result
)
1078 struct timeval tv
= {0, 0};
1083 *rc
= GPG_ERR_INV_ARG
;
1087 /* When not in a command, this will let libassuan process status messages
1088 * by calling PWMD_OPTION_STATUS_FUNC. The client can poll the file
1089 * descriptor returned by pwmd_get_fd() to determine when this should be
1090 * called or call pwmd_pending_line() to determine whether a buffered line
1091 * needs to be processed. */
1092 if (pwm
->cmd
== ASYNC_CMD_NONE
) {
1093 *rc
= assuan_command(pwm
, pwm
->ctx
, NULL
, "NOP");
1097 /* Fixes pwmd_open/save_async2() when there is a cached or new file. */
1098 if (pwm
->state
== ASYNC_DONE
) {
1099 reset_async(pwm
, 0);
1103 if (pwm
->state
!= ASYNC_PROCESS
) {
1104 *rc
= GPG_ERR_INV_STATE
;
1109 if (pwm
->cmd
== ASYNC_CMD_DNS
) {
1112 if (pwm
->tcp_conn
->rc
) {
1113 *rc
= pwm
->tcp_conn
->rc
;
1114 reset_async(pwm
, 1);
1120 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1122 /* Shouldn't happen. */
1127 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
1129 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
1133 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1137 else if (pwm
->cmd
== ASYNC_CMD_CONNECT
) {
1138 if (pwm
->tcp_conn
->rc
== GPG_ERR_EINPROGRESS
) {
1140 socklen_t len
= sizeof(int);
1143 FD_SET(pwm
->tcp_conn
->fd
, &fds
);
1145 n
= pth_select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1147 n
= select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1150 if (!n
|| !FD_ISSET(pwm
->tcp_conn
->fd
, &fds
))
1153 *rc
= gpg_error_from_syserror();
1154 reset_async(pwm
, 1);
1158 ret
= getsockopt(pwm
->tcp_conn
->fd
, SOL_SOCKET
, SO_ERROR
, &n
, &len
);
1161 *rc
= ret
? gpg_error_from_syserror() : gpg_error_from_errno(n
);
1162 reset_async(pwm
, 1);
1166 else if (pwm
->tcp_conn
->rc
) {
1167 *rc
= pwm
->tcp_conn
->rc
;
1168 reset_async(pwm
, 1);
1172 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, 0);
1173 *rc
= setup_tcp_session(pwm
);
1176 switch (pwm
->tcp_conn
->cmd
) {
1177 case ASYNC_CMD_HOSTKEY
:
1179 *result
= pwm
->result
;
1186 return reset_async(pwm
, *rc
? 1 : 0);
1190 #ifdef WITH_PINENTRY
1191 if (pwm
->cmd
== ASYNC_CMD_OPEN2
|| pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1192 if (pwm
->nb_fd
== -1) {
1193 *rc
= GPG_ERR_INV_STATE
;
1194 return reset_async(pwm
, 1);
1198 FD_SET(pwm
->nb_fd
, &fds
);
1200 n
= pth_select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1202 n
= select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1205 *rc
= gpg_error_from_syserror();
1206 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1210 pwmd_nb_status_t nb
;
1211 size_t len
= read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1214 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1222 *rc
= GPG_ERR_INV_STATE
;
1223 return reset_async(pwm
, 1);
1226 /* This is for the non-blocking OPEN and SAVE commands. */
1228 FD_SET(pwm
->fd
, &fds
);
1230 n
= pth_select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1232 n
= select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1236 *rc
= gpg_error_from_syserror();
1237 return reset_async(pwm
, 0);
1241 if (FD_ISSET(pwm
->fd
, &fds
)) {
1242 *rc
= parse_assuan_line(pwm
);
1245 return reset_async(pwm
, 0);
1249 while (!*rc
&& assuan_pending_line(pwm
->ctx
))
1250 *rc
= parse_assuan_line(pwm
);
1253 return reset_async(pwm
, 0);
1255 /* For pinentry retries. */
1256 if (pwm
->is_open_cmd
&& gpg_err_code(*rc
) == EPWMD_BADKEY
&&
1257 ++pwm
->ntries
< pwm
->pinentry_tries
) {
1258 pwm
->state
= ASYNC_INIT
;
1259 *rc
= pwmd_open_async(pwm
, pwm
->filename
);
1263 return reset_async(pwm
, 1);
1265 if (pwm
->state
== ASYNC_DONE
) {
1266 reset_async(pwm
, 0);
1273 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
1274 char **result
, const char *cmd
)
1282 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
1283 pwm
->status_func
, pwm
->status_data
);
1287 pwmd_free(data
.buf
);
1293 mem_realloc_cb(&data
, "", 1);
1296 pwmd_free(data
.buf
);
1297 rc
= GPG_ERR_INV_ARG
;
1300 *result
= (char *)data
.buf
;
1304 return gpg_err_code(rc
);
1307 gpg_error_t
pwmd_inquire(pwm_t
*pwm
, const char *cmd
, pwmd_inquire_fn fn
,
1310 if (!pwm
|| !cmd
|| !fn
)
1311 return GPG_ERR_INV_ARG
;
1313 pwm
->inquire_func
= fn
;
1314 pwm
->inquire_data
= data
;
1315 return assuan_command(pwm
, pwm
->ctx
, NULL
, cmd
);
1318 static gpg_error_t
terminate_pinentry(pwm_t
*pwm
)
1320 #ifndef WITH_PINENTRY
1321 return GPG_ERR_NOT_IMPLEMENTED
;
1323 pid_t pid
= pwm
->pid
;
1327 if (!pwm
|| pid
== -1)
1328 return GPG_ERR_INV_ARG
;
1330 if (kill(pid
, 0) == 0) {
1331 if (kill(pid
, SIGTERM
) == -1) {
1332 if (kill(pid
, SIGKILL
) == -1)
1333 return gpg_error_from_errno(errno
);
1337 return gpg_error_from_errno(errno
);
1343 #ifdef WITH_PINENTRY
1344 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
1347 char tmp
[ASSUAN_LINELENGTH
];
1351 pwm
->title
= pwmd_strdup(N_("LibPWMD"));
1354 pwm
->prompt
= pwmd_strdup(N_("Passphrase:"));
1356 if (!pwm
->desc
&& !which
)
1357 pwm
->desc
= pwmd_strdup(N_("Enter a passphrase."));
1360 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Invalid passphrase, please try again."));
1361 buf
= pwmd_strdup(tmp
);
1363 else if (which
== 2) {
1364 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Please type the passphrase again for confirmation."));
1365 buf
= pwmd_strdup(tmp
);
1368 buf
= (char *)pwmd_malloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
1369 sprintf(buf
, "SETERROR %s", pwm
->desc
);
1372 error
= pinentry_command(pwm
, NULL
, buf
);
1378 buf
= (char *)pwmd_malloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
1379 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
1380 error
= pinentry_command(pwm
, NULL
, buf
);
1386 buf
= (char *)pwmd_malloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
1387 sprintf(buf
, "SETDESC %s", pwm
->title
);
1388 error
= pinentry_command(pwm
, NULL
, buf
);
1393 static void update_pinentry_settings(pwm_t
*pwm
)
1399 char *pwbuf
= _getpwuid(&pw
);
1404 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
.pw_dir
);
1407 if ((fp
= fopen(buf
, "r")) == NULL
)
1410 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
1411 char name
[32], val
[256];
1413 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
1416 if (strcasecmp(name
, "TTYNAME") == 0) {
1417 pwmd_free(pwm
->pinentry_tty
);
1418 pwm
->pinentry_tty
= pwmd_strdup(val
);
1420 else if (strcasecmp(name
, "TTYTYPE") == 0) {
1421 pwmd_free(pwm
->pinentry_term
);
1422 pwm
->pinentry_term
= pwmd_strdup(val
);
1424 else if (strcasecmp(name
, "DISPLAY") == 0) {
1425 pwmd_free(pwm
->pinentry_display
);
1426 pwm
->pinentry_display
= pwmd_strdup(val
);
1428 else if (strcasecmp(name
, "PATH") == 0) {
1429 pwmd_free(pwm
->pinentry_path
);
1430 pwm
->pinentry_path
= pwmd_strdup(val
);
1437 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
1440 assuan_context_t ctx
;
1441 int child_list
[] = {-1};
1442 char *display
= getenv("DISPLAY");
1443 const char *argv
[10];
1444 const char **p
= argv
;
1445 int have_display
= 0;
1447 char *ttybuf
= NULL
;
1449 update_pinentry_settings(pwm
);
1451 if (pwm
->pinentry_display
|| display
)
1454 if (!pwm
->pinentry_tty
) {
1455 ttybuf
= pwmd_malloc(255);
1458 return gpg_error_from_errno(ENOMEM
);
1460 rc
= ttyname_r(STDOUT_FILENO
, ttybuf
, 255);
1464 return gpg_error_from_errno(rc
);
1470 tty
= pwm
->pinentry_tty
;
1473 if (!have_display
&& !tty
)
1474 return GPG_ERR_ENOTTY
;
1477 *p
++ = have_display
? "--display" : "--ttyname";
1478 *p
++ = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
1481 *p
++ = "--lc-ctype";
1482 *p
++ = pwm
->lcctype
;
1485 if (pwm
->lcmessages
) {
1486 *p
++ = "--lc-messages";
1487 *p
++ = pwm
->lcmessages
;
1492 if (!have_display
) {
1494 *p
++ = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
1498 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
1506 pwm
->pid
= assuan_get_pid(ctx
);
1508 return set_pinentry_strings(pwm
, 0);
1511 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1516 n
= launch_pinentry(pwm
);
1522 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
1525 static void pinentry_disconnect(pwm_t
*pwm
)
1528 assuan_disconnect(pwm
->pctx
);
1535 * Only called from a child process.
1537 static void catchsig(int sig
)
1541 if (gelapsed
++ >= gtimeout
)
1542 terminate_pinentry(gpwm
);
1553 * Borrowed from libassuan.
1555 static char *percent_escape(const char *atext
)
1557 const unsigned char *s
;
1558 int len
= strlen(atext
) * 3 + 1;
1559 char *buf
= (char *)pwmd_malloc(len
), *p
= buf
;
1564 for (s
=(const unsigned char *)atext
; *s
; s
++) {
1566 sprintf (p
, "%%%02X", *s
);
1577 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1580 return GPG_ERR_INV_ARG
;
1582 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
1585 gpg_error_t
pwmd_command_ap(pwm_t
*pwm
, char **result
, const char *cmd
,
1593 return GPG_ERR_INV_ARG
;
1596 * C99 allows the dst pointer to be null which will calculate the length
1597 * of the would-be result and return it.
1599 len
= vsnprintf(NULL
, 0, cmd
, ap
)+1;
1600 buf
= (char *)pwmd_malloc(len
);
1601 len
= vsnprintf(buf
, len
, cmd
, ap
);
1602 error
= send_command(pwm
, result
, buf
);
1608 * Avoid sending the BYE command here. libassuan will close the file
1609 * descriptor and release the assuan context. Use pwmd_close() instead.
1611 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
1617 return GPG_ERR_INV_ARG
;
1623 error
= pwmd_command_ap(pwm
, result
, cmd
, ap
);
1628 #ifdef WITH_PINENTRY
1629 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
1632 signal(SIGALRM
, catchsig
);
1637 return pinentry_command(pwm
, result
, "GETPIN");
1640 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
1642 int pin_try
= *try_n
;
1648 if (pin_try
== -1) {
1649 error
= set_pinentry_strings(pwm
, which
);
1652 pinentry_disconnect(pwm
);
1657 if (pwm
->pinentry_tries
-1 != pin_try
) {
1658 error
= set_pinentry_strings(pwm
, 1);
1661 pinentry_disconnect(pwm
);
1667 error
= do_getpin(pwm
, result
);
1670 * Since there was input cancel any timeout setting.
1675 if (error
== GPG_ERR_CANCELED
)
1676 return GPG_ERR_CANCELED
;
1678 if (pin_try
!= -1 && pin_try
--)
1682 pinentry_disconnect(pwm
);
1692 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
1694 char buf
[ASSUAN_LINELENGTH
];
1696 char *result
= NULL
;
1698 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
1699 error
= send_command(pwm
, &result
, buf
);
1700 memset(buf
, 0, sizeof(buf
));
1702 if (error
&& result
)
1708 static gpg_error_t
do_pwmd_open(pwm_t
*pwm
, const char *filename
, int nb
)
1710 char *result
= NULL
;
1711 char *password
= NULL
;
1712 char path
[PATH_MAX
];
1713 #ifdef WITH_PINENTRY
1718 if (!pwm
|| !filename
|| !*filename
)
1719 return GPG_ERR_INV_ARG
;
1721 #ifdef WITH_PINENTRY
1722 pin_try
= pwm
->pinentry_tries
- 1;
1726 * Avoid calling pinentry if the password is cached on the server or if
1727 * this is a new file.
1729 rc
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
1734 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
1737 if (access(path
, R_OK
) == -1) {
1738 if (errno
== ENOENT
)
1742 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
1744 if (rc
== EPWMD_CACHE_NOT_FOUND
) {
1745 if (pwm
->passfunc
) {
1746 password
= (char *)pwm
->passfunc(pwm
->passdata
);
1750 #ifdef WITH_PINENTRY
1752 * Get the password from pinentry.
1754 if (pwm
->use_pinentry
) {
1756 * Nonblocking is wanted. fork() then return a file descriptor
1757 * that the client can use to read() from.
1762 pwmd_nb_status_t pw
;
1765 return gpg_error_from_syserror();
1778 if (pwm
->pinentry_timeout
> 0) {
1780 gtimeout
= pwm
->pinentry_timeout
;
1785 rc
= getpin(pwm
, &password
, &pin_try
, 0);
1790 pinentry_disconnect(pwm
);
1792 if (gtimeout
&& gelapsed
>= gtimeout
)
1793 rc
= GPG_ERR_TIMEOUT
;
1797 pth_write(p
[1], &pw
, sizeof(pw
));
1799 write(p
[1], &pw
, sizeof(pw
));
1806 * Don't count the time it takes to open the file
1807 * which may have many iterations.
1809 signal(SIGALRM
, SIG_DFL
);
1810 rc
= do_open_command(pwm
, filename
, password
);
1812 if (pwm
->pinentry_timeout
)
1813 signal(SIGALRM
, catchsig
);
1815 if (pwm
->pctx
&& rc
== EPWMD_BADKEY
) {
1817 goto getpin_nb_again
;
1819 goto getpin_nb_fail
;
1822 pinentry_disconnect(pwm
);
1825 pth_write(p
[1], &pw
, sizeof(pw
));
1827 write(p
[1], &pw
, sizeof(pw
));
1833 rc
= gpg_error_from_syserror();
1849 * Not using pinentry and the file was not found
1852 password
= pwm
->password
;
1853 #ifdef WITH_PINENTRY
1861 rc
= do_open_command(pwm
, filename
, password
);
1864 * Keep the user defined password set with pwmd_setopt(). The password may
1865 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1867 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
1868 pwmd_free(password
);
1870 #ifdef WITH_PINENTRY
1871 if (rc
== EPWMD_BADKEY
) {
1872 if (pin_try
-- > 0 && !nb
) {
1873 rc
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
1874 N_("Invalid passphrase, please try again."));
1883 pinentry_disconnect(pwm
);
1891 pwmd_free(pwm
->filename
);
1893 pwm
->filename
= pwmd_strdup(filename
);
1899 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
1901 return do_pwmd_open(pwm
, filename
, 0);
1904 gpg_error_t
pwmd_open_async2(pwm_t
*pwm
, const char *filename
)
1906 #ifndef WITH_PINENTRY
1907 return GPG_ERR_NOT_IMPLEMENTED
;
1911 pwm
->cmd
= ASYNC_CMD_OPEN2
;
1912 pwm
->state
= ASYNC_PROCESS
;
1913 rc
= do_pwmd_open(pwm
, filename
, 1);
1916 reset_async(pwm
, 1);
1922 #ifdef WITH_PINENTRY
1923 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
1927 char *result
= NULL
;
1931 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
1935 pinentry_disconnect(pwm
);
1938 pwmd_free(*password
);
1948 if (strcmp(*password
, result
)) {
1949 pwmd_free(*password
);
1951 pinentry_disconnect(pwm
);
1952 error
= EPWMD_BADKEY
;
1957 pinentry_disconnect(pwm
);
1962 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
1964 char buf
[ASSUAN_LINELENGTH
];
1966 char *result
= NULL
;
1968 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
1969 error
= send_command(pwm
, &result
, buf
);
1970 memset(&buf
, 0, sizeof(buf
));
1972 if (error
&& result
)
1978 static gpg_error_t
do_pwmd_save(pwm_t
*pwm
, int nb
)
1980 char *result
= NULL
;
1981 char *password
= NULL
;
1985 return GPG_ERR_INV_ARG
;
1987 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
1988 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
1990 if (rc
== EPWMD_CACHE_NOT_FOUND
) {
1992 password
= (char *)(*pwm
->passfunc
)(pwm
->passdata
);
1993 #ifdef WITH_PINENTRY
1994 else if (pwm
->use_pinentry
) {
1998 pwmd_nb_status_t pw
;
2001 return gpg_error_from_syserror();
2016 rc
= do_save_getpin(pwm
, &password
);
2017 } while (rc
== EPWMD_BADKEY
);
2021 pinentry_disconnect(pwm
);
2025 pth_write(p
[1], &pw
, sizeof(pw
));
2027 write(p
[1], &pw
, sizeof(pw
));
2033 rc
= do_save_command(pwm
, password
);
2034 pinentry_disconnect(pwm
);
2037 pth_write(p
[1], &pw
, sizeof(pw
));
2039 write(p
[1], &pw
, sizeof(pw
));
2045 rc
= gpg_error_from_syserror();
2058 rc
= do_save_getpin(pwm
, &password
);
2071 password
= pwm
->password
;
2073 rc
= do_save_command(pwm
, password
);
2075 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
2076 pwmd_free(password
);
2081 gpg_error_t
pwmd_save_async2(pwm_t
*pwm
)
2083 #ifndef WITH_PINENTRY
2084 return GPG_ERR_NOT_IMPLEMENTED
;
2088 pwm
->cmd
= ASYNC_CMD_SAVE2
;
2089 pwm
->state
= ASYNC_PROCESS
;
2090 rc
= do_pwmd_save(pwm
, 1);
2093 reset_async(pwm
, 0);
2099 gpg_error_t
pwmd_save(pwm_t
*pwm
)
2101 return do_pwmd_save(pwm
, 0);
2104 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
2107 int n
= va_arg(ap
, int);
2110 gpg_error_t error
= 0;
2113 return GPG_ERR_INV_ARG
;
2118 case PWMD_OPTION_STATUS_FUNC
:
2119 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
2121 case PWMD_OPTION_STATUS_DATA
:
2122 pwm
->status_data
= va_arg(ap
, void *);
2124 case PWMD_OPTION_PASSWORD_FUNC
:
2125 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
2127 case PWMD_OPTION_PASSWORD_DATA
:
2128 pwm
->passdata
= va_arg(ap
, void *);
2130 case PWMD_OPTION_PASSWORD
:
2131 arg1
= va_arg(ap
, char *);
2134 pwmd_free(pwm
->password
);
2136 pwm
->password
= pwmd_strdup(arg1
);
2138 case PWMD_OPTION_PINENTRY
:
2139 n
= va_arg(ap
, int);
2141 if (n
!= 0 && n
!= 1) {
2143 error
= GPG_ERR_INV_VALUE
;
2146 pwm
->use_pinentry
= n
;
2147 error
= pwmd_command(pwm
, &result
, "OPTION PINENTRY=%i",
2148 !pwm
->use_pinentry
);
2151 #ifdef WITH_PINENTRY
2152 case PWMD_OPTION_PINENTRY_TRIES
:
2153 n
= va_arg(ap
, int);
2157 error
= GPG_ERR_INV_VALUE
;
2160 pwm
->pinentry_tries
= n
;
2163 case PWMD_OPTION_PINENTRY_TIMEOUT
:
2164 n
= va_arg(ap
, int);
2168 error
= GPG_ERR_INV_VALUE
;
2171 pwm
->pinentry_timeout
= n
;
2173 if (!pwm
->use_pinentry
)
2174 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i",
2175 pwm
->pinentry_timeout
);
2177 case PWMD_OPTION_PINENTRY_PATH
:
2178 if (pwm
->pinentry_path
)
2179 pwmd_free(pwm
->pinentry_path
);
2181 pwm
->pinentry_path
= pwmd_strdup(va_arg(ap
, char *));
2183 if (!pwm
->use_pinentry
)
2184 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s",
2185 pwm
->pinentry_path
);
2187 case PWMD_OPTION_PINENTRY_TTY
:
2188 if (pwm
->pinentry_tty
)
2189 pwmd_free(pwm
->pinentry_tty
);
2191 pwm
->pinentry_tty
= pwmd_strdup(va_arg(ap
, char *));
2193 if (!pwm
->use_pinentry
)
2194 error
= pwmd_command(pwm
, &result
, "OPTION TTY=%s",
2197 case PWMD_OPTION_PINENTRY_DISPLAY
:
2198 if (pwm
->pinentry_display
)
2199 pwmd_free(pwm
->pinentry_display
);
2201 pwm
->pinentry_display
= pwmd_strdup(va_arg(ap
, char *));
2203 if (!pwm
->use_pinentry
)
2204 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s",
2205 pwm
->pinentry_display
);
2207 case PWMD_OPTION_PINENTRY_TERM
:
2208 if (pwm
->pinentry_term
)
2209 pwmd_free(pwm
->pinentry_term
);
2211 pwm
->pinentry_term
= pwmd_strdup(va_arg(ap
, char *));
2213 if (!pwm
->use_pinentry
)
2214 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s",
2215 pwm
->pinentry_term
);
2217 case PWMD_OPTION_PINENTRY_TITLE
:
2219 pwmd_free(pwm
->title
);
2221 pwm
->title
= percent_escape(va_arg(ap
, char *));
2223 if (!pwm
->use_pinentry
)
2224 error
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
2227 case PWMD_OPTION_PINENTRY_PROMPT
:
2229 pwmd_free(pwm
->prompt
);
2231 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
2233 if (!pwm
->use_pinentry
)
2234 error
= pwmd_command(pwm
, &result
, "OPTION PROMPT=%s",
2237 case PWMD_OPTION_PINENTRY_DESC
:
2239 pwmd_free(pwm
->desc
);
2241 pwm
->desc
= percent_escape(va_arg(ap
, char *));
2243 if (!pwm
->use_pinentry
)
2244 error
= pwmd_command(pwm
, &result
, "OPTION DESC=%s",
2247 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
2249 pwmd_free(pwm
->lcctype
);
2251 pwm
->lcctype
= pwmd_strdup(va_arg(ap
, char *));
2253 if (!pwm
->use_pinentry
)
2254 error
= pwmd_command(pwm
, &result
, "OPTION LC_CTYPE=%s",
2257 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
2258 if (pwm
->lcmessages
)
2259 pwmd_free(pwm
->lcmessages
);
2261 pwm
->lcmessages
= pwmd_strdup(va_arg(ap
, char *));
2263 if (!pwm
->use_pinentry
)
2264 error
= pwmd_command(pwm
, &result
, "OPTION LC_MESSAGES=%s",
2268 error
= GPG_ERR_NOT_IMPLEMENTED
;
2276 gpg_error_t
pwmd_get_fd(pwm_t
*pwm
, int *fd
)
2279 return GPG_ERR_INV_ARG
;
2282 return GPG_ERR_INV_STATE
;
2288 gpg_error_t
pwmd_get_fd2(pwm_t
*pwm
, int *fd
)
2290 #ifndef WITH_PINENTRY
2291 return GPG_ERR_NOT_IMPLEMENTED
;
2294 return GPG_ERR_INV_ARG
;
2296 if (pwm
->nb_fd
== -1)
2297 return GPG_ERR_INV_STATE
;
2304 pwm_t
*pwmd_new(const char *name
)
2306 pwm_t
*h
= pwmd_calloc(1, sizeof(pwm_t
));
2312 h
->name
= pwmd_strdup(name
);
2321 #ifdef WITH_PINENTRY
2327 void pwmd_free(void *ptr
)
2332 void *pwmd_malloc(size_t size
)
2334 return xmalloc(size
);
2337 void *pwmd_calloc(size_t nmemb
, size_t size
)
2339 return xcalloc(nmemb
, size
);
2342 void *pwmd_realloc(void *ptr
, size_t size
)
2344 return xrealloc(ptr
, size
);
2347 char *pwmd_strdup(const char *str
)
2349 return xstrdup(str
);