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)
71 static char *_getpwuid(struct passwd
*pwd
)
73 size_t size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
74 struct passwd
*result
;
81 buf
= pwmd_malloc(size
);
86 n
= getpwuid_r(getuid(), pwd
, buf
, size
, &result
);
103 static const char *_pwmd_strerror(gpg_error_t e
)
105 gpg_err_code_t code
= gpg_err_code(e
);
107 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
112 return N_("Unknown error");
114 return N_("No cache slots available");
116 return N_("Recursion loop");
118 return N_("No file is open");
120 return N_("General LibXML error");
122 return N_("File modified");
129 const char *pwmd_strerror(gpg_error_t code
)
131 const char *p
= _pwmd_strerror(code
);
133 return p
? p
: gpg_strerror(code
);
136 int pwmd_strerror_r(gpg_error_t code
, char *buf
, size_t size
)
138 const char *p
= _pwmd_strerror(code
);
141 snprintf(buf
, size
, "%s", p
);
143 if (strlen(p
) > size
)
149 return gpg_strerror_r(code
, buf
, size
);
152 gpg_error_t
pwmd_init()
154 static int initialized
;
163 bindtextdomain("libpwmd", LOCALEDIR
);
166 assuan_set_malloc_hooks(pwmd_malloc
, pwmd_realloc
, pwmd_free
);
167 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
172 static gpg_error_t
_socket_connect_finalize(pwm_t
*pwm
)
175 int n
= assuan_get_active_fds(pwm
->ctx
, 0, active
, N_ARRAY(active
));
180 return GPG_ERR_EBADFD
;
186 assuan_set_pointer(pwm
->ctx
, pwm
);
189 rc
= pwmd_command(pwm
, NULL
, "OPTION CLIENT NAME=%s", pwm
->name
);
195 rc
= pwmd_command(pwm
, &result
, "VERSION");
197 if (rc
&& rc
!= GPG_ERR_ASS_UNKNOWN_CMD
)
201 pwm
->version
= PWMD_V1
;
203 pwm
->version
= PWMD_V2
;
210 static int read_hook(assuan_context_t ctx
, assuan_fd_t fd
, void *data
,
211 size_t len
, ssize_t
*ret
)
213 pwm_t
*pwm
= assuan_get_pointer(ctx
);
215 if (!pwm
|| !pwm
->tcp_conn
)
217 *ret
= pth_read((int)fd
, data
, len
);
219 *ret
= read((int)fd
, data
, len
);
222 *ret
= libssh2_channel_read(pwm
->tcp_conn
->channel
, data
, len
);
224 return *ret
>= 0 ? 1 : 0;
227 static int write_hook(assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
228 size_t len
, ssize_t
*ret
)
230 pwm_t
*pwm
= assuan_get_pointer(ctx
);
232 if (!pwm
|| !pwm
->tcp_conn
)
234 *ret
= pth_write((int)fd
, data
, len
);
236 *ret
= write((int)fd
, data
, len
);
239 *ret
= libssh2_channel_write(pwm
->tcp_conn
->channel
, data
, len
);
241 return *ret
>= 0 ? 1 : 0;
244 static void _ssh_deinit(pwmd_tcp_conn_t
*conn
);
245 static void free_tcp_conn(pwmd_tcp_conn_t
*conn
)
250 if (conn
->username
) {
251 pwmd_free(conn
->username
);
252 conn
->username
= NULL
;
255 if (conn
->known_hosts
) {
256 pwmd_free(conn
->known_hosts
);
257 conn
->known_hosts
= NULL
;
260 if (conn
->identity
) {
261 pwmd_free(conn
->identity
);
262 conn
->identity
= NULL
;
265 if (conn
->identity_pub
) {
266 pwmd_free(conn
->identity_pub
);
267 conn
->identity_pub
= NULL
;
271 pwmd_free(conn
->host
);
276 pwmd_free(conn
->hostkey
);
277 conn
->hostkey
= NULL
;
281 ares_destroy(conn
->chan
);
286 ares_free_hostent(conn
->he
);
301 static void _ssh_deinit(pwmd_tcp_conn_t
*conn
)
307 libssh2_channel_free(conn
->channel
);
310 libssh2_session_disconnect(conn
->session
, "Bye!");
311 libssh2_session_free(conn
->session
);
314 conn
->session
= NULL
;
315 conn
->channel
= NULL
;
319 static void _ssh_assuan_deinit(assuan_context_t ctx
)
321 pwm_t
*pwm
= assuan_get_pointer(ctx
);
323 pwm
->tcp_conn
->fd
= -1;
324 _ssh_deinit(pwm
->tcp_conn
);
325 pwm
->tcp_conn
= NULL
;
329 * Sets common options from both pwmd_ssh_connect() and
330 * pwmd_ssh_connect_async().
332 static gpg_error_t
init_tcp_conn(pwmd_tcp_conn_t
**dst
, const char *host
,
333 int port
, const char *identity
, const char *user
, const char *hosts
,
336 pwmd_tcp_conn_t
*conn
;
341 return GPG_ERR_INV_ARG
;
344 if (!host
|| !identity
|| !hosts
)
345 return GPG_ERR_INV_ARG
;
348 conn
= pwmd_calloc(1, sizeof(pwmd_tcp_conn_t
));
351 return gpg_error_from_errno(ENOMEM
);
353 conn
->port
= port
== -1 ? 22 : port
;
354 conn
->host
= pwmd_strdup(host
);
357 rc
= gpg_error_from_errno(ENOMEM
);
363 char *pwbuf
= _getpwuid(&pw
);
366 rc
= gpg_error_from_errno(errno
);
370 conn
->username
= pwmd_strdup(user
? user
: pw
.pw_name
);
373 if (!conn
->username
) {
374 rc
= gpg_error_from_errno(ENOMEM
);
378 conn
->identity
= pwmd_strdup(identity
);
380 if (!conn
->identity
) {
381 rc
= gpg_error_from_errno(ENOMEM
);
385 conn
->identity_pub
= pwmd_malloc(strlen(conn
->identity
)+5);
387 if (!conn
->identity_pub
) {
388 rc
= gpg_error_from_errno(ENOMEM
);
392 sprintf(conn
->identity_pub
, "%s.pub", conn
->identity
);
393 conn
->known_hosts
= pwmd_strdup(hosts
);
395 if (!conn
->known_hosts
) {
396 rc
= gpg_error_from_errno(ENOMEM
);
409 static gpg_error_t
do_connect(pwm_t
*pwm
, int prot
, void *addr
)
411 struct sockaddr_in their_addr
;
413 pwm
->tcp_conn
->fd
= socket(prot
, SOCK_STREAM
, 0);
415 if (pwm
->tcp_conn
->fd
== -1)
416 return gpg_error_from_syserror();
418 if (pwm
->tcp_conn
->async
)
419 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, O_NONBLOCK
);
421 pwm
->cmd
= ASYNC_CMD_CONNECT
;
422 their_addr
.sin_family
= prot
;
423 their_addr
.sin_port
= htons(pwm
->tcp_conn
->port
);
424 their_addr
.sin_addr
= *((struct in_addr
*)addr
);
425 memset(their_addr
.sin_zero
, '\0', sizeof their_addr
.sin_zero
);
428 if (pth_connect(pwm
->tcp_conn
->fd
, (struct sockaddr
*)&their_addr
,
429 sizeof(their_addr
)) == -1)
431 if (connect(pwm
->tcp_conn
->fd
, (struct sockaddr
*)&their_addr
,
432 sizeof(their_addr
)) == -1)
434 return gpg_error_from_syserror();
439 static gpg_error_t
ares_error_to_pwmd(int status
)
441 if (status
!= ARES_SUCCESS
)
442 warnx("%s", ares_strerror(status
));
448 return GPG_ERR_UNKNOWN_HOST
;
450 return GPG_ERR_EHOSTDOWN
;
452 return GPG_ERR_TIMEOUT
;
454 return gpg_error_from_errno(ENOMEM
);
455 case ARES_ECONNREFUSED
:
456 return GPG_ERR_ECONNREFUSED
;
459 return GPG_ERR_EHOSTUNREACH
;
465 static void dns_resolve_cb(void *arg
, int status
, int timeouts
,
466 unsigned char *abuf
, int alen
)
472 if (status
== ARES_EDESTRUCTION
)
475 if (status
!= ARES_SUCCESS
) {
476 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
480 /* Check for an IPv6 address first. */
481 if (pwm
->prot
== PWMD_IP_ANY
|| pwm
->prot
== PWMD_IPV6
)
482 rc
= ares_parse_aaaa_reply(abuf
, alen
, &he
, NULL
, NULL
);
484 rc
= ares_parse_a_reply(abuf
, alen
, &he
, NULL
, NULL
);
486 if (rc
!= ARES_SUCCESS
) {
487 if (pwm
->prot
!= PWMD_IP_ANY
|| rc
!= ARES_ENODATA
) {
488 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
492 rc
= ares_parse_a_reply(abuf
, alen
, &he
, NULL
, NULL
);
494 if (rc
!= ARES_SUCCESS
) {
495 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
500 pwm
->tcp_conn
->he
= he
;
501 pwm
->tcp_conn
->rc
= do_connect(pwm
, he
->h_addrtype
, he
->h_addr
);
504 static gpg_error_t
_do_pwmd_tcp_connect_async(pwm_t
*pwm
, const char *host
,
505 int port
, const char *identity
, const char *user
,
506 const char *known_hosts
, pwmd_async_cmd_t which
)
508 pwmd_tcp_conn_t
*conn
;
512 return GPG_ERR_INV_ARG
;
514 rc
= init_tcp_conn(&conn
, host
, port
, identity
, user
, known_hosts
,
515 which
== ASYNC_CMD_HOSTKEY
? 1 : 0);
521 pwm
->tcp_conn
= conn
;
522 pwm
->tcp_conn
->cmd
= which
;
524 if (pwm
->tcp_conn
->cmd
== ASYNC_CMD_HOSTKEY
)
525 pwm
->tcp_conn
->get_only
= 1;
527 pwm
->cmd
= ASYNC_CMD_DNS
;
528 pwm
->state
= ASYNC_PROCESS
;
529 ares_init(&pwm
->tcp_conn
->chan
);
530 ares_query(pwm
->tcp_conn
->chan
, pwm
->tcp_conn
->host
, ns_c_any
, ns_t_any
,
531 dns_resolve_cb
, pwm
);
535 gpg_error_t
pwmd_ssh_connect_async(pwm_t
*pwm
, const char *host
, int port
,
536 const char *identity
, const char *user
, const char *known_hosts
)
538 return _do_pwmd_tcp_connect_async(pwm
, host
, port
, identity
, user
,
539 known_hosts
, ASYNC_CMD_CONNECT
);
542 static void *_ssh_malloc(size_t size
, void **data
)
544 return pwmd_malloc(size
);
547 static void _ssh_free(void *ptr
, void **data
)
552 static void *_ssh_realloc(void *ptr
, size_t size
, void **data
)
554 return pwmd_realloc(ptr
, size
);
557 static char *to_hex(const char *str
, size_t slen
)
560 char *buf
= pwmd_malloc(slen
*2+1);
565 for (i
= 0, buf
[0] = 0; i
< slen
; i
++) {
568 sprintf(tmp
, "%02x", (unsigned char)str
[i
]);
575 static int verify_host_key(pwm_t
*pwm
)
577 FILE *fp
= fopen(pwm
->tcp_conn
->known_hosts
, "r");
583 buf
= pwmd_malloc(LINE_MAX
);
588 while ((p
= fgets(buf
, LINE_MAX
, fp
))) {
589 if (*p
== '#' || isspace(*p
))
592 if (p
[strlen(p
)-1] == '\n')
595 if (!strcmp(buf
, pwm
->tcp_conn
->hostkey
))
612 static gpg_error_t
authenticate_ssh(pwm_t
*pwm
)
614 const char *fp
= libssh2_hostkey_hash(pwm
->tcp_conn
->session
,
615 LIBSSH2_HOSTKEY_HASH_SHA1
);
618 pwm
->tcp_conn
->hostkey
= to_hex(fp
, 20);
620 if (!pwm
->tcp_conn
->hostkey
)
621 return gpg_error_from_errno(ENOMEM
);
623 if (pwm
->tcp_conn
->get_only
)
626 if (!fp
|| verify_host_key(pwm
))
627 return GPG_ERR_BAD_CERT
;
629 userauth
= libssh2_userauth_list(pwm
->tcp_conn
->session
,
630 pwm
->tcp_conn
->username
, strlen(pwm
->tcp_conn
->username
));
632 if (!userauth
|| !strstr(userauth
, "publickey"))
633 return GPG_ERR_BAD_PIN_METHOD
;
635 if (libssh2_userauth_publickey_fromfile(pwm
->tcp_conn
->session
,
636 pwm
->tcp_conn
->username
, pwm
->tcp_conn
->identity_pub
,
637 pwm
->tcp_conn
->identity
, NULL
))
638 return GPG_ERR_BAD_SECKEY
;
643 static gpg_error_t
setup_tcp_session(pwm_t
*pwm
)
645 assuan_context_t ctx
;
646 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
649 pwm
->tcp_conn
->session
= libssh2_session_init_ex(_ssh_malloc
, _ssh_free
,
652 if (!pwm
->tcp_conn
->session
) {
653 rc
= gpg_error_from_errno(ENOMEM
);
657 if (libssh2_session_startup(pwm
->tcp_conn
->session
, pwm
->tcp_conn
->fd
)) {
658 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
662 rc
= authenticate_ssh(pwm
);
667 /* pwmd_get_hostkey(). */
668 if (pwm
->tcp_conn
->get_only
) {
669 pwm
->result
= pwmd_strdup(pwm
->tcp_conn
->hostkey
);
672 rc
= gpg_error_from_errno(ENOMEM
);
679 pwm
->tcp_conn
->channel
= libssh2_channel_open_session(pwm
->tcp_conn
->session
);
681 if (!pwm
->tcp_conn
->channel
) {
682 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
686 if (libssh2_channel_shell(pwm
->tcp_conn
->channel
)) {
687 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
691 assuan_set_io_hooks(&io_hooks
);
692 rc
= assuan_socket_connect_fd(&ctx
, pwm
->tcp_conn
->fd
, 0, pwm
);
697 assuan_set_finish_handler(ctx
, _ssh_assuan_deinit
);
699 return _socket_connect_finalize(pwm
);
702 free_tcp_conn(pwm
->tcp_conn
);
703 pwm
->tcp_conn
= NULL
;
707 static gpg_error_t
_do_pwmd_tcp_connect(pwm_t
*pwm
, const char *host
, int port
,
708 const char *identity
, const char *user
, const char *known_hosts
, int get
)
710 pwmd_tcp_conn_t
*conn
;
714 return GPG_ERR_INV_ARG
;
716 rc
= init_tcp_conn(&conn
, host
, port
, identity
, user
, known_hosts
, get
);
721 pwm
->tcp_conn
= conn
;
722 pwm
->tcp_conn
->get_only
= get
;
723 pwm
->cmd
= ASYNC_CMD_DNS
;
724 ares_init(&pwm
->tcp_conn
->chan
);
725 ares_query(pwm
->tcp_conn
->chan
, pwm
->tcp_conn
->host
, ns_c_any
, ns_t_any
,
726 dns_resolve_cb
, pwm
);
728 /* dns_resolve_cb() may have already been called. */
729 if (pwm
->tcp_conn
->rc
) {
730 rc
= pwm
->tcp_conn
->rc
;
735 * Fake a blocking DNS lookup. libcares does a better job than
745 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
746 ares_timeout(pwm
->tcp_conn
->chan
, NULL
, &tv
);
748 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
750 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
754 rc
= gpg_error_from_syserror();
758 rc
= GPG_ERR_TIMEOUT
;
762 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
764 if (pwm
->tcp_conn
->rc
)
766 } while (pwm
->cmd
== ASYNC_CMD_DNS
);
768 if (pwm
->tcp_conn
->rc
) {
769 rc
= pwm
->tcp_conn
->rc
;
773 return setup_tcp_session(pwm
);
779 gpg_error_t
pwmd_ssh_connect(pwm_t
*pwm
, const char *host
, int port
,
780 const char *identity
, const char *user
, const char *known_hosts
)
782 return _do_pwmd_tcp_connect(pwm
, host
, port
, identity
, user
, known_hosts
, 0);
785 gpg_error_t
pwmd_get_hostkey(pwm_t
*pwm
, const char *host
, int port
,
791 rc
= _do_pwmd_tcp_connect(pwm
, host
, port
, NULL
, NULL
, NULL
, 1);
796 hostkey
= pwmd_strdup(pwm
->tcp_conn
->hostkey
);
799 rc
= gpg_error_from_errno(ENOMEM
);
805 gpg_error_t
pwmd_get_hostkey_async(pwm_t
*pwm
, const char *host
, int port
)
807 return _do_pwmd_tcp_connect_async(pwm
, host
, port
, NULL
, NULL
, NULL
,
812 * ssh://[username@]hostname[:port],identity,known_hosts
814 * Any missing parameters are checked for in init_tcp_conn().
816 static int parse_ssh_url(char *str
, char **host
, int *port
, char **user
,
817 char **identity
, char **known_hosts
)
823 *host
= *user
= *identity
= *known_hosts
= NULL
;
825 p
= strrchr(str
, '@');
828 len
= strlen(str
)-strlen(p
)+1;
829 *user
= pwmd_malloc(len
);
830 snprintf(*user
, len
, "%s", str
);
839 len
= strlen(p
)-strlen(t
)+1;
840 *host
= pwmd_malloc(len
);
841 snprintf(*host
, len
, "%s", p
);
845 while (*t
&& isdigit(*t
))
857 len
= strlen(p
)-strlen(t
)+1;
858 *host
= pwmd_malloc(len
);
859 snprintf(*host
, len
, "%s", p
);
866 len
= strlen(t
)-strlen(t2
)+1;
870 *identity
= pwmd_malloc(len
);
871 snprintf(*identity
, len
, "%s", t
);
877 *known_hosts
= pwmd_malloc(len
);
878 snprintf(*known_hosts
, len
, "%s", t2
);
884 *host
= pwmd_malloc(len
);
885 snprintf(*host
, len
, "%s", p
);
893 static gpg_error_t
_pwmd_connect_url(pwm_t
*pwm
, const char *url
, int async
)
895 char *p
= (char *)url
;
899 return GPG_ERR_INV_ARG
;
901 if (!strncmp(p
, "socket://", 9)) {
903 rc
= pwmd_connect(pwm
, p
);
904 pwm
->state
= ASYNC_DONE
;
907 else if (!strncmp(p
, "ssh://", 6) || !strncmp(p
, "ssh6://", 7) ||
908 !strncmp(p
, "ssh4://", 7)) {
910 return GPG_ERR_NOT_IMPLEMENTED
;
914 char *identity
= NULL
;
915 char *known_hosts
= NULL
;
916 char *username
= NULL
;
918 if (!strncmp(p
, "ssh6://", 7)) {
919 rc
= pwmd_setopt(pwm
, PWMD_OPTION_IP_VERSION
, PWMD_IPV6
);
922 else if (!strncmp(p
, "ssh4://", 7)) {
923 rc
= pwmd_setopt(pwm
, PWMD_OPTION_IP_VERSION
, PWMD_IPV4
);
927 rc
= pwmd_setopt(pwm
, PWMD_OPTION_IP_VERSION
, PWMD_IP_ANY
);
934 rc
= parse_ssh_url(p
, &host
, &port
, &username
, &identity
,
941 rc
= pwmd_ssh_connect_async(pwm
, host
, port
, identity
, username
,
944 rc
= pwmd_ssh_connect(pwm
, host
, port
, identity
, username
,
957 pwmd_free(known_hosts
);
963 return GPG_ERR_UNSUPPORTED_PROTOCOL
;
966 gpg_error_t
pwmd_connect_url(pwm_t
*pwm
, const char *url
)
968 return _pwmd_connect_url(pwm
, url
, 0);
971 gpg_error_t
pwmd_connect_url_async(pwm_t
*pwm
, const char *url
)
973 return _pwmd_connect_url(pwm
, url
, 1);
976 gpg_error_t
pwmd_connect(pwm_t
*pwm
, const char *path
)
978 char *socketpath
= NULL
;
979 assuan_context_t ctx
;
985 return GPG_ERR_INV_ARG
;
987 pwbuf
= _getpwuid(&pw
);
990 return gpg_error_from_errno(errno
);
992 if (!path
|| !*path
) {
993 socketpath
= (char *)pwmd_malloc(strlen(pw
.pw_dir
) + strlen("/.pwmd/socket") + 1);
994 sprintf(socketpath
, "%s/.pwmd/socket", pw
.pw_dir
);
997 socketpath
= pwmd_strdup(path
);
1000 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
1001 pwmd_free(socketpath
);
1007 return _socket_connect_finalize(pwm
);
1010 void pwmd_close(pwm_t
*pwm
)
1016 assuan_disconnect(pwm
->ctx
);
1019 pwmd_free(pwm
->password
);
1022 pwmd_free(pwm
->title
);
1025 pwmd_free(pwm
->desc
);
1028 pwmd_free(pwm
->prompt
);
1030 if (pwm
->pinentry_tty
)
1031 pwmd_free(pwm
->pinentry_tty
);
1033 if (pwm
->pinentry_display
)
1034 pwmd_free(pwm
->pinentry_display
);
1036 if (pwm
->pinentry_term
)
1037 pwmd_free(pwm
->pinentry_term
);
1040 pwmd_free(pwm
->lcctype
);
1042 if (pwm
->lcmessages
)
1043 pwmd_free(pwm
->lcmessages
);
1046 pwmd_free(pwm
->filename
);
1049 pwmd_free(pwm
->name
);
1053 free_tcp_conn(pwm
->tcp_conn
);
1059 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
1061 membuf_t
*mem
= (membuf_t
*)data
;
1067 if ((p
= pwmd_realloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
1071 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
1076 static int _inquire_cb(void *data
, const char *keyword
)
1078 pwm_t
*pwm
= (pwm_t
*)data
;
1080 int flags
= fcntl(pwm
->fd
, F_GETFL
);
1082 /* Shouldn't get this far without a callback. */
1083 if (!pwm
->inquire_func
)
1084 return GPG_ERR_INV_ARG
;
1087 * Since the socket file descriptor is probably set to non-blocking, set to
1088 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixed when
1089 * asynchronous INQUIRE is supported by either libassuan or a later
1092 fcntl(pwm
->fd
, F_SETFL
, 0);
1095 char *result
= NULL
;
1099 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
, &len
);
1100 rc
= gpg_err_code(rc
);
1102 if (rc
== GPG_ERR_EOF
|| !rc
) {
1103 if (len
<= 0 || !result
) {
1108 arc
= assuan_send_data(pwm
->ctx
, result
, len
);
1110 if (rc
== GPG_ERR_EOF
) {
1121 fcntl(pwm
->fd
, F_SETFL
, flags
);
1125 static gpg_error_t
do_nb_command(pwm_t
*pwm
, const char *cmd
, ...)
1131 if (pwm
->state
== ASYNC_DONE
)
1132 pwm
->state
= ASYNC_INIT
;
1134 if (pwm
->state
!= ASYNC_INIT
)
1135 return GPG_ERR_INV_STATE
;
1137 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1140 return gpg_error_from_errno(ENOMEM
);
1143 vsnprintf(buf
, ASSUAN_LINELENGTH
, cmd
, ap
);
1145 rc
= assuan_write_line(pwm
->ctx
, buf
);
1149 pwm
->state
= ASYNC_PROCESS
;
1154 gpg_error_t
pwmd_open_async(pwm_t
*pwm
, const char *filename
)
1156 if (!pwm
|| !filename
)
1157 return GPG_ERR_INV_ARG
;
1160 return GPG_ERR_INV_STATE
;
1162 if (pwm
->cmd
!= ASYNC_CMD_OPEN
) {
1168 pwmd_free(pwm
->filename
);
1170 pwm
->filename
= pwmd_strdup(filename
);
1172 rc
= send_pinentry_options(pwm
);
1178 pwm
->cmd
= ASYNC_CMD_OPEN
;
1179 return do_nb_command(pwm
, "OPEN %s %s", filename
,
1180 pwm
->password
? pwm
->password
: "");
1183 gpg_error_t
pwmd_save_async(pwm_t
*pwm
)
1188 return GPG_ERR_INV_ARG
;
1191 return GPG_ERR_INV_STATE
;
1193 rc
= send_pinentry_options(pwm
);
1198 pwm
->cmd
= ASYNC_CMD_SAVE
;
1199 return do_nb_command(pwm
, "SAVE %s", pwm
->password
? pwm
->password
: "");
1202 static gpg_error_t
parse_assuan_line(pwm_t
*pwm
)
1208 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
1211 if (line
[0] == 'O' && line
[1] == 'K' &&
1212 (line
[2] == 0 || line
[2] == ' ')) {
1213 pwm
->state
= ASYNC_DONE
;
1215 else if (line
[0] == '#') {
1217 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' ')) {
1218 if (pwm
->status_func
) {
1219 pwm
->status_func(pwm
->status_data
,
1220 line
[1] == 0 ? line
+1 : line
+2);
1223 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
1224 (line
[3] == 0 || line
[3] == ' ')) {
1227 pwm
->state
= ASYNC_DONE
;
1234 gpg_error_t
pwmd_pending_line(pwm_t
*pwm
)
1237 return GPG_ERR_INV_ARG
;
1240 return GPG_ERR_INV_STATE
;
1242 return assuan_pending_line(pwm
->ctx
) ? 0 : GPG_ERR_NO_DATA
;
1245 static pwmd_async_t
reset_async(pwm_t
*pwm
, int done
)
1247 pwm
->state
= ASYNC_INIT
;
1248 pwm
->cmd
= ASYNC_CMD_NONE
;
1250 #ifdef WITH_PINENTRY
1251 if (pwm
->nb_fd
!= -1) {
1257 if (done
&& pwm
->tcp_conn
&& pwm
->tcp_conn
->fd
!= -1) {
1258 close(pwm
->tcp_conn
->fd
);
1259 pwm
->tcp_conn
->fd
= -1;
1266 pwmd_async_t
pwmd_process(pwm_t
*pwm
, gpg_error_t
*rc
, char **result
)
1270 struct timeval tv
= {0, 0};
1276 return GPG_ERR_INV_ARG
;
1281 *rc
= GPG_ERR_INV_ARG
;
1284 else if (!pwm
->ctx
) {
1287 *rc
= GPG_ERR_INV_STATE
;
1291 case ASYNC_CMD_CONNECT
:
1292 case ASYNC_CMD_HOSTKEY
:
1298 /* When not in a command, this will let libassuan process status messages
1299 * by calling PWMD_OPTION_STATUS_FUNC. The client can poll the file
1300 * descriptor returned by pwmd_get_fd() to determine when this should be
1301 * called or call pwmd_pending_line() to determine whether a buffered line
1302 * needs to be processed. */
1303 if (pwm
->cmd
== ASYNC_CMD_NONE
) {
1304 *rc
= assuan_command(pwm
, pwm
->ctx
, NULL
, "NOP");
1308 /* Fixes pwmd_open/save_async2() when there is a cached or new file. */
1309 if (pwm
->state
== ASYNC_DONE
) {
1310 reset_async(pwm
, 0);
1314 if (pwm
->state
!= ASYNC_PROCESS
) {
1315 *rc
= GPG_ERR_INV_STATE
;
1320 if (pwm
->cmd
== ASYNC_CMD_DNS
) {
1323 if (pwm
->tcp_conn
->rc
) {
1324 *rc
= pwm
->tcp_conn
->rc
;
1325 reset_async(pwm
, 1);
1331 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1333 /* Shouldn't happen. */
1338 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
1340 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
1344 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1348 else if (pwm
->cmd
== ASYNC_CMD_CONNECT
) {
1349 if (pwm
->tcp_conn
->rc
== GPG_ERR_EINPROGRESS
) {
1351 socklen_t len
= sizeof(int);
1354 FD_SET(pwm
->tcp_conn
->fd
, &fds
);
1356 n
= pth_select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1358 n
= select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1361 if (!n
|| !FD_ISSET(pwm
->tcp_conn
->fd
, &fds
))
1364 *rc
= gpg_error_from_syserror();
1365 reset_async(pwm
, 1);
1369 ret
= getsockopt(pwm
->tcp_conn
->fd
, SOL_SOCKET
, SO_ERROR
, &n
, &len
);
1372 *rc
= ret
? gpg_error_from_syserror() : gpg_error_from_errno(n
);
1373 reset_async(pwm
, 1);
1377 else if (pwm
->tcp_conn
->rc
) {
1378 *rc
= pwm
->tcp_conn
->rc
;
1379 reset_async(pwm
, 1);
1383 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, 0);
1384 *rc
= setup_tcp_session(pwm
);
1387 switch (pwm
->tcp_conn
->cmd
) {
1388 case ASYNC_CMD_HOSTKEY
:
1390 *result
= pwm
->result
;
1397 return reset_async(pwm
, *rc
? 1 : 0);
1401 #ifdef WITH_PINENTRY
1402 if (pwm
->cmd
== ASYNC_CMD_OPEN2
|| pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1405 if (pwm
->nb_fd
== -1) {
1406 *rc
= GPG_ERR_INV_STATE
;
1407 return reset_async(pwm
, 0);
1411 FD_SET(pwm
->nb_fd
, &fds
);
1412 FD_SET(pwm
->fd
, &fds
);
1414 n
= pth_select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1416 n
= select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1419 *rc
= gpg_error_from_syserror();
1420 return reset_async(pwm
, 0);
1423 if (n
> 0 && FD_ISSET(pwm
->nb_fd
, &fds
)) {
1424 pwmd_nb_status_t nb
;
1426 size_t len
= pth_read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1428 size_t len
= read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1430 waitpid(pwm
->nb_pid
, &status
, WNOHANG
);
1432 if (len
!= sizeof(nb
)) {
1433 *rc
= gpg_error_from_syserror();
1434 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1439 if (*rc
== GPG_ERR_INV_PASSPHRASE
&& pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1440 reset_async(pwm
, 0);
1441 *rc
= pwmd_save_async2(pwm
);
1442 return ASYNC_PROCESS
;
1445 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1447 if (pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1448 *rc
= do_save_command(pwm
, nb
.password
);
1449 memset(&nb
, 0, sizeof(pwmd_nb_status_t
));
1450 return reset_async(pwm
, 0);
1453 if (pwm
->cmd
== ASYNC_CMD_OPEN2
) {
1454 *rc
= do_open_command(pwm
, pwm
->filename
, nb
.password
);
1455 memset(&nb
, 0, sizeof(pwmd_nb_status_t
));
1457 if (*rc
== GPG_ERR_INV_PASSPHRASE
) {
1458 if (++pwm
->pin_try
< pwm
->pinentry_tries
) {
1459 int n
= pwm
->pin_try
;
1461 reset_async(pwm
, 0);
1463 pwm
->cmd
= ASYNC_CMD_OPEN2
;
1464 *rc
= pwmd_open_async2(pwm
, pwm
->filename
);
1467 return reset_async(pwm
, 1);
1473 return reset_async(pwm
, *rc
? 1 : 0);
1477 /* Fall through so status messages can be processed during the
1483 *rc
= GPG_ERR_INV_STATE
;
1484 return reset_async(pwm
, 0);
1487 /* This is for the non-blocking OPEN and SAVE commands. */
1489 FD_SET(pwm
->fd
, &fds
);
1491 n
= pth_select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1493 n
= select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1497 *rc
= gpg_error_from_syserror();
1498 return reset_async(pwm
, 0);
1502 if (FD_ISSET(pwm
->fd
, &fds
))
1503 *rc
= parse_assuan_line(pwm
);
1506 while (!*rc
&& assuan_pending_line(pwm
->ctx
))
1507 *rc
= parse_assuan_line(pwm
);
1509 /* For pinentry retries. */
1510 if (pwm
->cmd
== ASYNC_CMD_OPEN
&&
1511 gpg_err_code(*rc
) == GPG_ERR_INV_PASSPHRASE
&&
1512 ++pwm
->pin_try
< pwm
->pinentry_tries
) {
1513 pwm
->state
= ASYNC_INIT
;
1514 *rc
= pwmd_open_async(pwm
, pwm
->filename
);
1518 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN
? 1 : 0);
1520 if (pwm
->state
== ASYNC_DONE
) {
1521 reset_async(pwm
, 0);
1528 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
1529 char **result
, const char *cmd
)
1537 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
1538 pwm
->status_func
, pwm
->status_data
);
1542 pwmd_free(data
.buf
);
1548 mem_realloc_cb(&data
, "", 1);
1551 pwmd_free(data
.buf
);
1552 rc
= GPG_ERR_INV_ARG
;
1555 *result
= (char *)data
.buf
;
1559 return gpg_err_code(rc
);
1562 gpg_error_t
pwmd_inquire(pwm_t
*pwm
, const char *cmd
, pwmd_inquire_cb_t fn
,
1565 if (!pwm
|| !cmd
|| !fn
)
1566 return GPG_ERR_INV_ARG
;
1569 return GPG_ERR_INV_STATE
;
1571 pwm
->inquire_func
= fn
;
1572 pwm
->inquire_data
= data
;
1573 return assuan_command(pwm
, pwm
->ctx
, NULL
, cmd
);
1576 #ifdef WITH_PINENTRY
1577 static gpg_error_t
terminate_pinentry(pwm_t
*pwm
)
1579 pid_t pid
= pwm
->pid
;
1583 if (!pwm
|| pid
== -1)
1584 return GPG_ERR_INV_ARG
;
1586 if (kill(pid
, 0) == 0) {
1587 if (kill(pid
, SIGTERM
) == -1) {
1588 if (kill(pid
, SIGKILL
) == -1)
1589 return gpg_error_from_errno(errno
);
1593 return gpg_error_from_errno(errno
);
1598 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
1603 tmp
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1606 return gpg_error_from_errno(ENOMEM
);
1609 pwm
->title
= pwmd_strdup(N_("Password Manager Daemon"));
1615 pwm
->prompt
= pwmd_strdup(N_("Passphrase:"));
1620 if (!pwm
->desc
&& (which
== PINENTRY_OPEN
|| which
== PINENTRY_SAVE
)) {
1621 if (which
== PINENTRY_OPEN
)
1622 desc
= pwmd_strdup_printf(N_("A passphrase is required to open the file \"%s\". Please%%0Aenter the passphrase below."), pwm
->filename
);
1624 desc
= pwmd_strdup_printf(N_("A passphrase is required to save to the file \"%s\". Please%%0Aenter the passphrase below."), pwm
->filename
);
1636 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s", desc
);
1638 if (pwm
->desc
!= desc
)
1641 case PINENTRY_OPEN_FAILED
:
1642 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1643 N_("Invalid passphrase, please try again."));
1645 case PINENTRY_SAVE_CONFIRM
:
1646 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1647 N_("Please type the passphrase again for confirmation."));
1651 error
= pinentry_command(pwm
, NULL
, tmp
);
1658 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETPROMPT %s", pwm
->prompt
);
1659 error
= pinentry_command(pwm
, NULL
, tmp
);
1666 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETDESC %s", pwm
->title
);
1667 error
= pinentry_command(pwm
, NULL
, tmp
);
1673 return gpg_error_from_errno(ENOMEM
);
1676 static void update_pinentry_settings(pwm_t
*pwm
)
1682 char *pwbuf
= _getpwuid(&pw
);
1687 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
.pw_dir
);
1690 if ((fp
= fopen(buf
, "r")) == NULL
)
1693 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
1694 char name
[32], val
[256];
1696 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
1699 if (strcasecmp(name
, "TTYNAME") == 0) {
1700 pwmd_free(pwm
->pinentry_tty
);
1701 pwm
->pinentry_tty
= pwmd_strdup(val
);
1703 else if (strcasecmp(name
, "TTYTYPE") == 0) {
1704 pwmd_free(pwm
->pinentry_term
);
1705 pwm
->pinentry_term
= pwmd_strdup(val
);
1707 else if (strcasecmp(name
, "DISPLAY") == 0) {
1708 pwmd_free(pwm
->pinentry_display
);
1709 pwm
->pinentry_display
= pwmd_strdup(val
);
1711 else if (strcasecmp(name
, "PATH") == 0) {
1712 pwmd_free(pwm
->pinentry_path
);
1713 pwm
->pinentry_path
= pwmd_strdup(val
);
1720 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
1723 assuan_context_t ctx
;
1724 int child_list
[] = {-1};
1725 char *display
= getenv("DISPLAY");
1726 const char *argv
[10];
1727 const char **p
= argv
;
1728 int have_display
= 0;
1730 char *ttybuf
= NULL
;
1732 update_pinentry_settings(pwm
);
1734 if (pwm
->pinentry_display
|| display
)
1737 if (!pwm
->pinentry_tty
) {
1738 ttybuf
= pwmd_malloc(255);
1741 return gpg_error_from_errno(ENOMEM
);
1743 rc
= ttyname_r(STDOUT_FILENO
, ttybuf
, 255);
1747 return gpg_error_from_errno(rc
);
1753 tty
= pwm
->pinentry_tty
;
1756 if (!have_display
&& !tty
)
1757 return GPG_ERR_ENOTTY
;
1760 *p
++ = have_display
? "--display" : "--ttyname";
1761 *p
++ = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
1764 *p
++ = "--lc-ctype";
1765 *p
++ = pwm
->lcctype
;
1768 if (pwm
->lcmessages
) {
1769 *p
++ = "--lc-messages";
1770 *p
++ = pwm
->lcmessages
;
1775 if (!have_display
) {
1777 *p
++ = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
1781 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
1789 pwm
->pid
= assuan_get_pid(ctx
);
1791 return set_pinentry_strings(pwm
, 0);
1794 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1799 n
= launch_pinentry(pwm
);
1805 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
1808 static void pinentry_disconnect(pwm_t
*pwm
)
1811 assuan_disconnect(pwm
->pctx
);
1818 * Only called from a child process.
1820 static void catchsig(int sig
)
1824 if (gelapsed
++ >= gtimeout
) {
1825 terminate_pinentry(gpwm
);
1826 gerror
= GPG_ERR_TIMEOUT
;
1838 * Borrowed from libassuan.
1840 static char *percent_escape(const char *atext
)
1842 const unsigned char *s
;
1843 int len
= strlen(atext
) * 3 + 1;
1844 char *buf
= (char *)pwmd_malloc(len
), *p
= buf
;
1849 for (s
=(const unsigned char *)atext
; *s
; s
++) {
1851 sprintf (p
, "%%%02X", *s
);
1862 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1865 return GPG_ERR_INV_ARG
;
1867 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
1870 gpg_error_t
pwmd_command_ap(pwm_t
*pwm
, char **result
, const char *cmd
,
1879 return GPG_ERR_INV_ARG
;
1882 return GPG_ERR_INV_STATE
;
1885 * C99 allows the dst pointer to be null which will calculate the length
1886 * of the would-be result and return it.
1889 len
= vsnprintf(NULL
, 0, cmd
, ap
)+1;
1890 buf
= (char *)pwmd_malloc(len
);
1894 return gpg_error_from_errno(ENOMEM
);
1897 len
= vsnprintf(buf
, len
, cmd
, ap2
);
1900 if (buf
[strlen(buf
)-1] == '\n')
1901 buf
[strlen(buf
)-1] = 0;
1903 if (buf
[strlen(buf
)-1] == '\r')
1904 buf
[strlen(buf
)-1] = 0;
1906 error
= send_command(pwm
, result
, buf
);
1911 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
1917 return GPG_ERR_INV_ARG
;
1920 return GPG_ERR_INV_STATE
;
1926 error
= pwmd_command_ap(pwm
, result
, cmd
, ap
);
1931 #ifdef WITH_PINENTRY
1932 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
1935 signal(SIGALRM
, catchsig
);
1940 return pinentry_command(pwm
, result
, "GETPIN");
1943 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int which
)
1948 rc
= set_pinentry_strings(pwm
, which
);
1951 pinentry_disconnect(pwm
);
1955 rc
= do_getpin(pwm
, result
);
1958 * Since there was input cancel any timeout setting.
1961 signal(SIGALRM
, SIG_DFL
);
1965 pinentry_disconnect(pwm
);
1967 /* This lets pwmd_open2() with PWMD_OPTION_PINENTRY_TIMEOUT work. */
1968 if (rc
== GPG_ERR_EOF
&& gerror
== GPG_ERR_TIMEOUT
)
1978 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
1982 char *result
= NULL
;
1984 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1987 return gpg_error_from_errno(ENOMEM
);
1989 snprintf(buf
, ASSUAN_LINELENGTH
, "OPEN %s %s", filename
,
1990 password
? password
: "");
1991 error
= send_command(pwm
, &result
, buf
);
1994 if (error
&& result
)
2000 static gpg_error_t
send_pinentry_options(pwm_t
*pwm
)
2004 if (pwm
->pinentry_path
) {
2005 rc
= pwmd_command(pwm
, NULL
, "OPTION PATH=%s", pwm
->pinentry_path
);
2011 if (pwm
->pinentry_tty
) {
2012 rc
= pwmd_command(pwm
, NULL
, "OPTION TTYNAME=%s", pwm
->pinentry_tty
);
2018 if (pwm
->pinentry_term
) {
2019 rc
= pwmd_command(pwm
, NULL
, "OPTION TTYTYPE=%s", pwm
->pinentry_term
);
2025 if (pwm
->pinentry_display
) {
2026 rc
= pwmd_command(pwm
, NULL
, "OPTION TITLE=%s", pwm
->pinentry_display
);
2033 rc
= pwmd_command(pwm
, NULL
, "OPTION TITLE=%s", pwm
->title
);
2040 rc
= pwmd_command(pwm
, NULL
, "OPTION DESC=%s", pwm
->desc
);
2047 rc
= pwmd_command(pwm
, NULL
, "OPTION PROMPT=%s", pwm
->prompt
);
2054 rc
= pwmd_command(pwm
, NULL
, "OPTION LC_CTYPE=%s", pwm
->lcctype
);
2060 if (pwm
->lcmessages
) {
2061 rc
= pwmd_command(pwm
, NULL
, "OPTION LC_MESSAGES=%s", pwm
->lcmessages
);
2067 if (pwm
->pinentry_timeout
>= 0) {
2068 rc
= pwmd_command(pwm
, NULL
, "OPTION TIMEOUT=%i", pwm
->pinentry_timeout
);
2077 static gpg_error_t
do_pwmd_open(pwm_t
*pwm
, const char *filename
, int nb
,
2080 char *result
= NULL
;
2081 char *password
= NULL
;
2087 if (!pwm
|| !filename
|| !*filename
)
2088 return GPG_ERR_INV_ARG
;
2091 return GPG_ERR_INV_STATE
;
2093 pin_try
= pwm
->pinentry_tries
- 1;
2096 * Avoid calling pinentry if the password is cached on the server or if
2097 * this is a new file. pwmd version 2 adds a VERSION command which is
2098 * determined in _socket_connect_finalize(). If the server is version 2,
2099 * ISCACHED can determine if a file exists.
2102 if (!pwm
->tcp_conn
&& pwm
->version
== PWMD_V1
) {
2104 if (pwm
->version
== PWMD_V1
) {
2106 rc
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
2111 len
= strlen(result
)+strlen(filename
)+2;
2112 path
= pwmd_malloc(len
);
2116 return gpg_error_from_errno(ENOMEM
);
2119 snprintf(path
, len
, "%s/%s", result
, filename
);
2122 if (access(path
, R_OK
) == -1) {
2123 if (errno
== ENOENT
) {
2132 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
2134 if (gpg_err_code(rc
) == GPG_ERR_ENOENT
)
2137 if (rc
&& rc
!= GPG_ERR_NOT_FOUND
)
2140 if (rc
== GPG_ERR_NOT_FOUND
) {
2141 if (pwm
->password
) {
2142 password
= pwm
->password
;
2146 if (pwm
->passfunc
) {
2147 rc
= pwm
->passfunc(pwm
->passdata
, &password
);
2156 #ifdef WITH_PINENTRY
2157 if (rc
== GPG_ERR_NOT_FOUND
&& local_pinentry
) {
2158 rc
= pwmd_command(pwm
, NULL
, "OPTION PINENTRY=0");
2164 pwm
->filename
= pwmd_strdup(filename
);
2167 return gpg_error_from_errno(ENOMEM
);
2169 /* Get the passphrase using the LOCAL pinentry. */
2173 pwmd_nb_status_t pw
;
2176 return gpg_error_from_syserror();
2189 if (pwm
->pinentry_timeout
!= 0) {
2191 gtimeout
= abs(pwm
->pinentry_timeout
);
2195 pw
.error
= getpin(pwm
, &password
, PINENTRY_OPEN
);
2197 if (gtimeout
&& gelapsed
>= gtimeout
)
2198 pw
.error
= GPG_ERR_TIMEOUT
;
2201 snprintf(pw
.password
, sizeof(pw
.password
), "%s",
2204 pinentry_disconnect(pwm
);
2206 pth_write(p
[1], &pw
, sizeof(pw
));
2208 write(p
[1], &pw
, sizeof(pw
));
2210 memset(&pw
, 0, sizeof(pw
));
2215 rc
= gpg_error_from_syserror();
2229 if (pwm
->pinentry_timeout
!= 0) {
2231 gtimeout
= abs(pwm
->pinentry_timeout
);
2235 rc
= getpin(pwm
, &password
, PINENTRY_OPEN
);
2237 /* Don't timeout when an invalid passphrase was entered. */
2246 pwm
->state
= ASYNC_DONE
;
2248 if (!local_pinentry
) {
2249 rc
= send_pinentry_options(pwm
);
2255 rc
= do_open_command(pwm
, filename
, password
);
2258 * Keep the user defined password set with pwmd_setopt(). The password may
2259 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
2261 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
2262 pwmd_free(password
);
2264 if (rc
== GPG_ERR_INV_PASSPHRASE
) {
2265 if (pin_try
-- > 0 && !nb
) {
2267 #ifdef WITH_PINENTRY
2269 rc
= getpin(pwm
, &password
, PINENTRY_OPEN_FAILED
);
2272 rc
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
2273 N_("Invalid passphrase, please try again."));
2281 #ifdef WITH_PINENTRY
2283 pinentry_disconnect(pwm
);
2291 pwmd_free(pwm
->filename
);
2293 pwm
->filename
= pwmd_strdup(filename
);
2299 gpg_error_t
pwmd_open2(pwm_t
*pwm
, const char *filename
)
2301 #ifndef WITH_PINENTRY
2302 return GPG_ERR_NOT_IMPLEMENTED
;
2304 return do_pwmd_open(pwm
, filename
, 0, 1);
2308 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
2310 return do_pwmd_open(pwm
, filename
, 0, 0);
2313 gpg_error_t
pwmd_open_async2(pwm_t
*pwm
, const char *filename
)
2315 #ifndef WITH_PINENTRY
2316 return GPG_ERR_NOT_IMPLEMENTED
;
2320 if (!pwm
|| !filename
)
2321 return GPG_ERR_INV_ARG
;
2324 return GPG_ERR_INV_STATE
;
2326 if (pwm
->cmd
!= ASYNC_CMD_OPEN2
)
2329 pwm
->cmd
= ASYNC_CMD_OPEN2
;
2330 pwm
->state
= ASYNC_PROCESS
;
2331 rc
= do_pwmd_open(pwm
, filename
, 1, 1);
2334 reset_async(pwm
, 1);
2340 #ifdef WITH_PINENTRY
2341 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
2345 char *result
= NULL
;
2348 error
= getpin(pwm
, &result
, confirm
? PINENTRY_SAVE_CONFIRM
: PINENTRY_SAVE
);
2352 pinentry_disconnect(pwm
);
2355 pwmd_free(*password
);
2365 if (strcmp(*password
, result
)) {
2366 pwmd_free(*password
);
2374 pinentry_disconnect(pwm
);
2379 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
2383 char *result
= NULL
;
2385 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
2388 return gpg_error_from_errno(ENOMEM
);
2390 snprintf(buf
, ASSUAN_LINELENGTH
, "SAVE %s", password
? password
: "");
2391 error
= send_command(pwm
, &result
, buf
);
2394 if (error
&& result
)
2400 static gpg_error_t
do_pwmd_save(pwm_t
*pwm
, int nb
, int local_pinentry
)
2402 char *result
= NULL
;
2403 char *password
= NULL
;
2407 return GPG_ERR_INV_ARG
;
2410 return GPG_ERR_INV_STATE
;
2412 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
2414 if (rc
&& rc
!= GPG_ERR_NOT_FOUND
)
2417 if (rc
== GPG_ERR_NOT_FOUND
) {
2418 if (pwm
->password
) {
2419 password
= pwm
->password
;
2423 if (pwm
->passfunc
) {
2424 rc
= pwm
->passfunc(pwm
->passdata
, &password
);
2433 if (rc
== GPG_ERR_NOT_FOUND
&& local_pinentry
) {
2434 #ifdef WITH_PINENTRY
2435 /* Get the password using the LOCAL pinentry. */
2439 pwmd_nb_status_t pw
;
2442 return gpg_error_from_syserror();
2455 pw
.error
= do_save_getpin(pwm
, &password
);
2456 pinentry_disconnect(pwm
);
2457 snprintf(pw
.password
, sizeof(pw
.password
), "%s",
2460 pth_write(p
[1], &pw
, sizeof(pw
));
2462 write(p
[1], &pw
, sizeof(pw
));
2464 memset(&pw
, 0, sizeof(pw
));
2469 rc
= gpg_error_from_syserror();
2483 rc
= do_save_getpin(pwm
, &password
);
2490 pwm
->state
= ASYNC_DONE
;
2493 if (!local_pinentry
) {
2494 rc
= send_pinentry_options(pwm
);
2500 rc
= do_save_command(pwm
, password
);
2502 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
2503 pwmd_free(password
);
2508 gpg_error_t
pwmd_save_async2(pwm_t
*pwm
)
2510 #ifndef WITH_PINENTRY
2511 return GPG_ERR_NOT_IMPLEMENTED
;
2516 return GPG_ERR_INV_ARG
;
2519 return GPG_ERR_INV_STATE
;
2521 pwm
->cmd
= ASYNC_CMD_SAVE2
;
2522 pwm
->state
= ASYNC_PROCESS
;
2523 rc
= do_pwmd_save(pwm
, 1, 1);
2526 reset_async(pwm
, 0);
2532 gpg_error_t
pwmd_save2(pwm_t
*pwm
)
2534 #ifndef WITH_PINENTRY
2535 return GPG_ERR_NOT_IMPLEMENTED
;
2537 return do_pwmd_save(pwm
, 0, 1);
2541 gpg_error_t
pwmd_save(pwm_t
*pwm
)
2543 return do_pwmd_save(pwm
, 0, 0);
2546 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
2549 int n
= va_arg(ap
, int);
2551 gpg_error_t error
= 0;
2554 return GPG_ERR_INV_ARG
;
2559 case PWMD_OPTION_STATUS_CB
:
2560 pwm
->status_func
= va_arg(ap
, pwmd_status_cb_t
);
2562 case PWMD_OPTION_STATUS_DATA
:
2563 pwm
->status_data
= va_arg(ap
, void *);
2565 case PWMD_OPTION_PASSPHRASE_CB
:
2566 pwm
->passfunc
= va_arg(ap
, pwmd_passphrase_cb_t
);
2568 case PWMD_OPTION_PASSPHRASE_DATA
:
2569 pwm
->passdata
= va_arg(ap
, void *);
2571 case PWMD_OPTION_PASSPHRASE
:
2572 arg1
= va_arg(ap
, char *);
2575 pwmd_free(pwm
->password
);
2577 pwm
->password
= pwmd_strdup(arg1
);
2579 case PWMD_OPTION_PINENTRY_TRIES
:
2580 n
= va_arg(ap
, int);
2584 error
= GPG_ERR_INV_VALUE
;
2587 pwm
->pinentry_tries
= n
;
2589 case PWMD_OPTION_PINENTRY_TIMEOUT
:
2590 n
= va_arg(ap
, int);
2594 error
= GPG_ERR_INV_VALUE
;
2597 pwm
->pinentry_timeout
= n
;
2599 case PWMD_OPTION_PINENTRY_PATH
:
2600 if (pwm
->pinentry_path
)
2601 pwmd_free(pwm
->pinentry_path
);
2603 pwm
->pinentry_path
= pwmd_strdup(va_arg(ap
, char *));
2605 case PWMD_OPTION_PINENTRY_TTY
:
2606 if (pwm
->pinentry_tty
)
2607 pwmd_free(pwm
->pinentry_tty
);
2609 pwm
->pinentry_tty
= pwmd_strdup(va_arg(ap
, char *));
2611 case PWMD_OPTION_PINENTRY_DISPLAY
:
2612 if (pwm
->pinentry_display
)
2613 pwmd_free(pwm
->pinentry_display
);
2615 pwm
->pinentry_display
= pwmd_strdup(va_arg(ap
, char *));
2617 case PWMD_OPTION_PINENTRY_TERM
:
2618 if (pwm
->pinentry_term
)
2619 pwmd_free(pwm
->pinentry_term
);
2621 pwm
->pinentry_term
= pwmd_strdup(va_arg(ap
, char *));
2623 case PWMD_OPTION_PINENTRY_TITLE
:
2625 pwmd_free(pwm
->title
);
2627 pwm
->title
= percent_escape(va_arg(ap
, char *));
2629 case PWMD_OPTION_PINENTRY_PROMPT
:
2631 pwmd_free(pwm
->prompt
);
2633 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
2635 case PWMD_OPTION_PINENTRY_DESC
:
2637 pwmd_free(pwm
->desc
);
2639 pwm
->desc
= percent_escape(va_arg(ap
, char *));
2641 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
2643 pwmd_free(pwm
->lcctype
);
2645 pwm
->lcctype
= pwmd_strdup(va_arg(ap
, char *));
2647 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
2648 if (pwm
->lcmessages
)
2649 pwmd_free(pwm
->lcmessages
);
2651 pwm
->lcmessages
= pwmd_strdup(va_arg(ap
, char *));
2654 case PWMD_OPTION_IP_VERSION
:
2655 n
= va_arg(ap
, int);
2664 error
= GPG_ERR_INV_VALUE
;
2672 error
= GPG_ERR_NOT_IMPLEMENTED
;
2680 gpg_error_t
pwmd_get_fds(pwm_t
*pwm
, pwmd_fd_t
*fds
, int *n_fds
)
2685 int afds
[ARES_GETSOCK_MAXNUM
];
2690 if (!pwm
|| !fds
|| !n_fds
|| *n_fds
<= 0)
2691 return GPG_ERR_INV_ARG
;
2695 memset(afds
, 0, sizeof(int)*ARES_GETSOCK_MAXNUM
);
2697 memset(fds
, 0, sizeof(pwmd_fd_t
)*in_total
);
2702 case ASYNC_CMD_NONE
:
2703 case ASYNC_CMD_OPEN
:
2704 case ASYNC_CMD_SAVE
:
2705 #ifdef WITH_PINENTRY
2709 return GPG_ERR_INV_STATE
;
2712 fds
[fd
].fd
= pwm
->fd
;
2713 fds
[fd
++].flags
= PWMD_FD_READABLE
;
2715 #ifdef WITH_PINENTRY
2716 case ASYNC_CMD_OPEN2
:
2717 case ASYNC_CMD_SAVE2
:
2718 /* The command has already completed (cached or new). */
2719 if (pwm
->state
== ASYNC_DONE
)
2722 if (pwm
->nb_fd
== -1)
2723 return GPG_ERR_INV_STATE
;
2726 fds
[fd
].fd
= pwm
->nb_fd
;
2727 fds
[fd
++].flags
= PWMD_FD_READABLE
;
2732 if (!pwm
->tcp_conn
|| !pwm
->tcp_conn
->chan
)
2733 return GPG_ERR_INV_STATE
;
2735 n
= ares_getsock(pwm
->tcp_conn
->chan
, afds
, ARES_GETSOCK_MAXNUM
);
2737 for (i
= 0; i
< ARES_GETSOCK_MAXNUM
; i
++) {
2740 if (fd
> in_total
) {
2742 return GPG_ERR_ERANGE
;
2745 if (ARES_GETSOCK_READABLE(n
, i
)) {
2747 fds
[fd
].flags
|= PWMD_FD_READABLE
;
2750 if (ARES_GETSOCK_WRITABLE(n
, i
)) {
2752 fds
[fd
].flags
|= PWMD_FD_WRITABLE
;
2756 fds
[fd
++].fd
= afds
[i
];
2761 case ASYNC_CMD_CONNECT
:
2762 case ASYNC_CMD_HOSTKEY
:
2763 if (!pwm
->tcp_conn
|| pwm
->tcp_conn
->fd
== -1)
2764 return GPG_ERR_INV_STATE
;
2767 fds
[fd
].fd
= pwm
->tcp_conn
->fd
;
2768 fds
[fd
++].flags
= PWMD_FD_READABLE
;
2773 return GPG_ERR_INV_STATE
;
2776 pwm_t
*pwmd_new(const char *name
)
2778 pwm_t
*h
= pwmd_calloc(1, sizeof(pwm_t
));
2784 h
->name
= pwmd_strdup(name
);
2793 #ifdef WITH_PINENTRY
2796 h
->pinentry_timeout
= -30;
2797 h
->pinentry_tries
= 3;
2799 h
->prot
= PWMD_IP_ANY
;
2804 void pwmd_free(void *ptr
)
2809 void *pwmd_malloc(size_t size
)
2811 return xmalloc(size
);
2814 void *pwmd_calloc(size_t nmemb
, size_t size
)
2816 return xcalloc(nmemb
, size
);
2819 void *pwmd_realloc(void *ptr
, size_t size
)
2821 return xrealloc(ptr
, size
);
2824 char *pwmd_strdup(const char *str
)
2826 return xstrdup(str
);
2829 char *pwmd_strdup_printf(const char *fmt
, ...)
2840 len
= vsnprintf(NULL
, 0, fmt
, ap
);
2842 buf
= pwmd_malloc(++len
);
2845 vsnprintf(buf
, len
, fmt
, ap2
);