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
);
216 if (!pwm
|| !pwm
->tcp_conn
)
218 *ret
= pth_read((int)fd
, data
, len
);
220 *ret
= read((int)fd
, data
, len
);
224 *ret
= libssh2_channel_read(pwm
->tcp_conn
->channel
, data
, len
);
225 n
= libssh2_session_last_errno(pwm
->tcp_conn
->session
);
226 } while (n
== LIBSSH2_ERROR_EAGAIN
);
229 return !n
&& *ret
!= -1 ? 1 : 0;
232 static int write_hook(assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
233 size_t len
, ssize_t
*ret
)
235 pwm_t
*pwm
= assuan_get_pointer(ctx
);
238 if (!pwm
|| !pwm
->tcp_conn
)
240 *ret
= pth_write((int)fd
, data
, len
);
242 *ret
= write((int)fd
, data
, len
);
246 *ret
= libssh2_channel_write(pwm
->tcp_conn
->channel
, data
, len
);
247 n
= libssh2_session_last_errno(pwm
->tcp_conn
->session
);
248 } while (n
== LIBSSH2_ERROR_EAGAIN
);
251 return !n
&& *ret
!= -1 ? 1 : 0;
254 static void _ssh_deinit(pwmd_tcp_conn_t
*conn
);
255 static void free_tcp_conn(pwmd_tcp_conn_t
*conn
)
260 if (conn
->username
) {
261 pwmd_free(conn
->username
);
262 conn
->username
= NULL
;
265 if (conn
->known_hosts
) {
266 pwmd_free(conn
->known_hosts
);
267 conn
->known_hosts
= NULL
;
270 if (conn
->identity
) {
271 pwmd_free(conn
->identity
);
272 conn
->identity
= NULL
;
275 if (conn
->identity_pub
) {
276 pwmd_free(conn
->identity_pub
);
277 conn
->identity_pub
= NULL
;
281 pwmd_free(conn
->host
);
286 pwmd_free(conn
->hostkey
);
287 conn
->hostkey
= NULL
;
291 ares_destroy(conn
->chan
);
296 ares_free_hostent(conn
->he
);
311 static void _ssh_deinit(pwmd_tcp_conn_t
*conn
)
317 libssh2_channel_free(conn
->channel
);
320 libssh2_session_disconnect(conn
->session
, "Bye!");
321 libssh2_session_free(conn
->session
);
324 conn
->session
= NULL
;
325 conn
->channel
= NULL
;
329 static void _ssh_assuan_deinit(assuan_context_t ctx
)
331 pwm_t
*pwm
= assuan_get_pointer(ctx
);
333 pwm
->tcp_conn
->fd
= -1;
334 _ssh_deinit(pwm
->tcp_conn
);
335 pwm
->tcp_conn
= NULL
;
339 * Sets common options from both pwmd_ssh_connect() and
340 * pwmd_ssh_connect_async().
342 static gpg_error_t
init_tcp_conn(pwmd_tcp_conn_t
**dst
, const char *host
,
343 int port
, const char *identity
, const char *user
, const char *hosts
,
346 pwmd_tcp_conn_t
*conn
;
351 return GPG_ERR_INV_ARG
;
354 if (!host
|| !identity
|| !hosts
)
355 return GPG_ERR_INV_ARG
;
358 conn
= pwmd_calloc(1, sizeof(pwmd_tcp_conn_t
));
361 return gpg_error_from_errno(ENOMEM
);
363 conn
->port
= port
== -1 ? 22 : port
;
364 conn
->host
= pwmd_strdup(host
);
367 rc
= gpg_error_from_errno(ENOMEM
);
373 char *pwbuf
= _getpwuid(&pw
);
376 rc
= gpg_error_from_errno(errno
);
380 conn
->username
= pwmd_strdup(user
? user
: pw
.pw_name
);
383 if (!conn
->username
) {
384 rc
= gpg_error_from_errno(ENOMEM
);
388 conn
->identity
= pwmd_strdup(identity
);
390 if (!conn
->identity
) {
391 rc
= gpg_error_from_errno(ENOMEM
);
395 conn
->identity_pub
= pwmd_malloc(strlen(conn
->identity
)+5);
397 if (!conn
->identity_pub
) {
398 rc
= gpg_error_from_errno(ENOMEM
);
402 sprintf(conn
->identity_pub
, "%s.pub", conn
->identity
);
403 conn
->known_hosts
= pwmd_strdup(hosts
);
405 if (!conn
->known_hosts
) {
406 rc
= gpg_error_from_errno(ENOMEM
);
419 static gpg_error_t
do_connect(pwm_t
*pwm
, int prot
, void *addr
)
421 struct sockaddr_in their_addr
;
423 pwm
->tcp_conn
->fd
= socket(prot
, SOCK_STREAM
, 0);
425 if (pwm
->tcp_conn
->fd
== -1)
426 return gpg_error_from_syserror();
428 if (pwm
->tcp_conn
->async
)
429 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, O_NONBLOCK
);
431 pwm
->cmd
= ASYNC_CMD_CONNECT
;
432 their_addr
.sin_family
= prot
;
433 their_addr
.sin_port
= htons(pwm
->tcp_conn
->port
);
434 their_addr
.sin_addr
= *((struct in_addr
*)addr
);
435 memset(their_addr
.sin_zero
, '\0', sizeof their_addr
.sin_zero
);
438 if (pth_connect(pwm
->tcp_conn
->fd
, (struct sockaddr
*)&their_addr
,
439 sizeof(their_addr
)) == -1)
441 if (connect(pwm
->tcp_conn
->fd
, (struct sockaddr
*)&their_addr
,
442 sizeof(their_addr
)) == -1)
444 return gpg_error_from_syserror();
449 static gpg_error_t
ares_error_to_pwmd(int status
)
451 if (status
!= ARES_SUCCESS
)
452 warnx("%s", ares_strerror(status
));
458 return GPG_ERR_UNKNOWN_HOST
;
460 return GPG_ERR_EHOSTDOWN
;
462 return GPG_ERR_TIMEOUT
;
464 return gpg_error_from_errno(ENOMEM
);
465 case ARES_ECONNREFUSED
:
466 return GPG_ERR_ECONNREFUSED
;
469 return GPG_ERR_EHOSTUNREACH
;
475 static void dns_resolve_cb(void *arg
, int status
, int timeouts
,
476 unsigned char *abuf
, int alen
)
482 if (status
== ARES_EDESTRUCTION
)
485 if (status
!= ARES_SUCCESS
) {
486 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
490 /* Check for an IPv6 address first. */
491 if (pwm
->prot
== PWMD_IP_ANY
|| pwm
->prot
== PWMD_IPV6
)
492 rc
= ares_parse_aaaa_reply(abuf
, alen
, &he
, NULL
, NULL
);
494 rc
= ares_parse_a_reply(abuf
, alen
, &he
, NULL
, NULL
);
496 if (rc
!= ARES_SUCCESS
) {
497 if (pwm
->prot
!= PWMD_IP_ANY
|| rc
!= ARES_ENODATA
) {
498 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
502 rc
= ares_parse_a_reply(abuf
, alen
, &he
, NULL
, NULL
);
504 if (rc
!= ARES_SUCCESS
) {
505 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
510 pwm
->tcp_conn
->he
= he
;
511 pwm
->tcp_conn
->rc
= do_connect(pwm
, he
->h_addrtype
, he
->h_addr
);
514 static gpg_error_t
_do_pwmd_tcp_connect_async(pwm_t
*pwm
, const char *host
,
515 int port
, const char *identity
, const char *user
,
516 const char *known_hosts
, pwmd_async_cmd_t which
)
518 pwmd_tcp_conn_t
*conn
;
522 return GPG_ERR_INV_ARG
;
524 rc
= init_tcp_conn(&conn
, host
, port
, identity
, user
, known_hosts
,
525 which
== ASYNC_CMD_HOSTKEY
? 1 : 0);
531 pwm
->tcp_conn
= conn
;
532 pwm
->tcp_conn
->cmd
= which
;
534 if (pwm
->tcp_conn
->cmd
== ASYNC_CMD_HOSTKEY
)
535 pwm
->tcp_conn
->get_only
= 1;
537 pwm
->cmd
= ASYNC_CMD_DNS
;
538 pwm
->state
= ASYNC_PROCESS
;
539 ares_init(&pwm
->tcp_conn
->chan
);
540 ares_query(pwm
->tcp_conn
->chan
, pwm
->tcp_conn
->host
, ns_c_any
, ns_t_any
,
541 dns_resolve_cb
, pwm
);
545 gpg_error_t
pwmd_ssh_connect_async(pwm_t
*pwm
, const char *host
, int port
,
546 const char *identity
, const char *user
, const char *known_hosts
)
548 return _do_pwmd_tcp_connect_async(pwm
, host
, port
, identity
, user
,
549 known_hosts
, ASYNC_CMD_CONNECT
);
552 static void *_ssh_malloc(size_t size
, void **data
)
554 return pwmd_malloc(size
);
557 static void _ssh_free(void *ptr
, void **data
)
562 static void *_ssh_realloc(void *ptr
, size_t size
, void **data
)
564 return pwmd_realloc(ptr
, size
);
567 static char *to_hex(const char *str
, size_t slen
)
570 char *buf
= pwmd_malloc(slen
*2+1);
575 for (i
= 0, buf
[0] = 0; i
< slen
; i
++) {
578 sprintf(tmp
, "%02x", (unsigned char)str
[i
]);
585 static int verify_host_key(pwm_t
*pwm
)
587 FILE *fp
= fopen(pwm
->tcp_conn
->known_hosts
, "r");
593 buf
= pwmd_malloc(LINE_MAX
);
598 while ((p
= fgets(buf
, LINE_MAX
, fp
))) {
599 if (*p
== '#' || isspace(*p
))
602 if (p
[strlen(p
)-1] == '\n')
605 if (!strcmp(buf
, pwm
->tcp_conn
->hostkey
))
622 static gpg_error_t
authenticate_ssh(pwm_t
*pwm
)
624 const char *fp
= libssh2_hostkey_hash(pwm
->tcp_conn
->session
,
625 LIBSSH2_HOSTKEY_HASH_SHA1
);
628 pwm
->tcp_conn
->hostkey
= to_hex(fp
, 20);
630 if (!pwm
->tcp_conn
->hostkey
)
631 return gpg_error_from_errno(ENOMEM
);
633 if (pwm
->tcp_conn
->get_only
)
636 if (!fp
|| verify_host_key(pwm
))
637 return GPG_ERR_CHECKSUM
;
639 userauth
= libssh2_userauth_list(pwm
->tcp_conn
->session
,
640 pwm
->tcp_conn
->username
, strlen(pwm
->tcp_conn
->username
));
642 if (!userauth
|| !strstr(userauth
, "publickey"))
643 return GPG_ERR_BAD_PIN_METHOD
;
645 if (libssh2_userauth_publickey_fromfile(pwm
->tcp_conn
->session
,
646 pwm
->tcp_conn
->username
, pwm
->tcp_conn
->identity_pub
,
647 pwm
->tcp_conn
->identity
, NULL
))
648 return GPG_ERR_BAD_SECKEY
;
653 static gpg_error_t
setup_tcp_session(pwm_t
*pwm
)
655 assuan_context_t ctx
;
656 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
659 pwm
->tcp_conn
->session
= libssh2_session_init_ex(_ssh_malloc
, _ssh_free
,
662 if (!pwm
->tcp_conn
->session
) {
663 rc
= gpg_error_from_errno(ENOMEM
);
667 if (libssh2_session_startup(pwm
->tcp_conn
->session
, pwm
->tcp_conn
->fd
)) {
668 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
672 rc
= authenticate_ssh(pwm
);
677 /* pwmd_get_hostkey(). */
678 if (pwm
->tcp_conn
->get_only
) {
679 pwm
->result
= pwmd_strdup(pwm
->tcp_conn
->hostkey
);
682 rc
= gpg_error_from_errno(ENOMEM
);
689 pwm
->tcp_conn
->channel
= libssh2_channel_open_session(pwm
->tcp_conn
->session
);
691 if (!pwm
->tcp_conn
->channel
) {
692 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
696 if (libssh2_channel_shell(pwm
->tcp_conn
->channel
)) {
697 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
701 assuan_set_io_hooks(&io_hooks
);
702 rc
= assuan_socket_connect_fd(&ctx
, pwm
->tcp_conn
->fd
, 0, pwm
);
707 assuan_set_finish_handler(ctx
, _ssh_assuan_deinit
);
709 return _socket_connect_finalize(pwm
);
712 free_tcp_conn(pwm
->tcp_conn
);
713 pwm
->tcp_conn
= NULL
;
717 static gpg_error_t
_do_pwmd_tcp_connect(pwm_t
*pwm
, const char *host
, int port
,
718 const char *identity
, const char *user
, const char *known_hosts
, int get
)
720 pwmd_tcp_conn_t
*conn
;
724 return GPG_ERR_INV_ARG
;
726 rc
= init_tcp_conn(&conn
, host
, port
, identity
, user
, known_hosts
, get
);
731 pwm
->tcp_conn
= conn
;
732 pwm
->tcp_conn
->get_only
= get
;
733 pwm
->cmd
= ASYNC_CMD_DNS
;
734 ares_init(&pwm
->tcp_conn
->chan
);
735 ares_query(pwm
->tcp_conn
->chan
, pwm
->tcp_conn
->host
, ns_c_any
, ns_t_any
,
736 dns_resolve_cb
, pwm
);
738 /* dns_resolve_cb() may have already been called. */
739 if (pwm
->tcp_conn
->rc
) {
740 rc
= pwm
->tcp_conn
->rc
;
745 * Fake a blocking DNS lookup. libcares does a better job than
755 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
756 ares_timeout(pwm
->tcp_conn
->chan
, NULL
, &tv
);
758 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
760 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
764 rc
= gpg_error_from_syserror();
768 rc
= GPG_ERR_TIMEOUT
;
772 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
774 if (pwm
->tcp_conn
->rc
)
776 } while (pwm
->cmd
== ASYNC_CMD_DNS
);
778 if (pwm
->tcp_conn
->rc
) {
779 rc
= pwm
->tcp_conn
->rc
;
783 return setup_tcp_session(pwm
);
789 gpg_error_t
pwmd_ssh_connect(pwm_t
*pwm
, const char *host
, int port
,
790 const char *identity
, const char *user
, const char *known_hosts
)
792 return _do_pwmd_tcp_connect(pwm
, host
, port
, identity
, user
, known_hosts
, 0);
795 gpg_error_t
pwmd_get_hostkey(pwm_t
*pwm
, const char *host
, int port
,
801 rc
= _do_pwmd_tcp_connect(pwm
, host
, port
, NULL
, NULL
, NULL
, 1);
806 hostkey
= pwmd_strdup(pwm
->tcp_conn
->hostkey
);
809 rc
= gpg_error_from_errno(ENOMEM
);
815 gpg_error_t
pwmd_get_hostkey_async(pwm_t
*pwm
, const char *host
, int port
)
817 return _do_pwmd_tcp_connect_async(pwm
, host
, port
, NULL
, NULL
, NULL
,
822 gpg_error_t
pwmd_connect(pwm_t
*pwm
, const char *path
)
824 char *socketpath
= NULL
;
825 assuan_context_t ctx
;
831 return GPG_ERR_INV_ARG
;
833 pwbuf
= _getpwuid(&pw
);
836 return gpg_error_from_errno(errno
);
839 socketpath
= (char *)pwmd_malloc(strlen(pw
.pw_dir
) + strlen("/.pwmd/socket") + 1);
840 sprintf(socketpath
, "%s/.pwmd/socket", pw
.pw_dir
);
843 socketpath
= pwmd_strdup(path
);
846 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
847 pwmd_free(socketpath
);
853 return _socket_connect_finalize(pwm
);
856 void pwmd_close(pwm_t
*pwm
)
862 assuan_disconnect(pwm
->ctx
);
865 pwmd_free(pwm
->password
);
868 pwmd_free(pwm
->title
);
871 pwmd_free(pwm
->desc
);
874 pwmd_free(pwm
->prompt
);
876 if (pwm
->pinentry_tty
)
877 pwmd_free(pwm
->pinentry_tty
);
879 if (pwm
->pinentry_display
)
880 pwmd_free(pwm
->pinentry_display
);
882 if (pwm
->pinentry_term
)
883 pwmd_free(pwm
->pinentry_term
);
886 pwmd_free(pwm
->lcctype
);
889 pwmd_free(pwm
->lcmessages
);
892 pwmd_free(pwm
->filename
);
895 pwmd_free(pwm
->name
);
899 free_tcp_conn(pwm
->tcp_conn
);
905 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
907 membuf_t
*mem
= (membuf_t
*)data
;
913 if ((p
= pwmd_realloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
917 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
922 static int _inquire_cb(void *data
, const char *keyword
)
924 pwm_t
*pwm
= (pwm_t
*)data
;
926 int flags
= fcntl(pwm
->fd
, F_GETFL
);
928 /* Shouldn't get this far without a callback. */
929 if (!pwm
->inquire_func
)
930 return GPG_ERR_INV_ARG
;
933 * Since the socket file descriptor is probably set to non-blocking, set to
934 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixed when
935 * asynchronous INQUIRE is supported by either libassuan or a later
938 fcntl(pwm
->fd
, F_SETFL
, 0);
945 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
, &len
);
946 rc
= gpg_err_code(rc
);
948 if (rc
== GPG_ERR_EOF
|| !rc
) {
949 if (len
<= 0 || !result
) {
954 arc
= assuan_send_data(pwm
->ctx
, result
, len
);
956 if (rc
== GPG_ERR_EOF
) {
967 fcntl(pwm
->fd
, F_SETFL
, flags
);
971 static gpg_error_t
do_nb_command(pwm_t
*pwm
, const char *cmd
, ...)
977 if (pwm
->state
== ASYNC_DONE
)
978 pwm
->state
= ASYNC_INIT
;
980 if (pwm
->state
!= ASYNC_INIT
)
981 return GPG_ERR_INV_STATE
;
983 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
986 return gpg_error_from_errno(ENOMEM
);
989 vsnprintf(buf
, ASSUAN_LINELENGTH
, cmd
, ap
);
991 rc
= assuan_write_line(pwm
->ctx
, buf
);
995 pwm
->state
= ASYNC_PROCESS
;
1000 gpg_error_t
pwmd_open_async(pwm_t
*pwm
, const char *filename
)
1002 if (!pwm
|| !filename
)
1003 return GPG_ERR_INV_ARG
;
1006 return GPG_ERR_INV_STATE
;
1008 if (pwm
->cmd
!= ASYNC_CMD_OPEN
) {
1014 pwmd_free(pwm
->filename
);
1016 pwm
->filename
= pwmd_strdup(filename
);
1018 rc
= send_pinentry_options(pwm
);
1024 pwm
->cmd
= ASYNC_CMD_OPEN
;
1025 return do_nb_command(pwm
, "OPEN %s %s", filename
,
1026 pwm
->password
? pwm
->password
: "");
1029 gpg_error_t
pwmd_save_async(pwm_t
*pwm
)
1034 return GPG_ERR_INV_ARG
;
1037 return GPG_ERR_INV_STATE
;
1039 rc
= send_pinentry_options(pwm
);
1044 pwm
->cmd
= ASYNC_CMD_SAVE
;
1045 return do_nb_command(pwm
, "SAVE %s", pwm
->password
? pwm
->password
: "");
1048 static gpg_error_t
parse_assuan_line(pwm_t
*pwm
)
1054 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
1057 if (line
[0] == 'O' && line
[1] == 'K' &&
1058 (line
[2] == 0 || line
[2] == ' ')) {
1059 pwm
->state
= ASYNC_DONE
;
1061 else if (line
[0] == '#') {
1063 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' ')) {
1064 if (pwm
->status_func
) {
1065 pwm
->status_func(pwm
->status_data
,
1066 line
[1] == 0 ? line
+1 : line
+2);
1069 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
1070 (line
[3] == 0 || line
[3] == ' ')) {
1073 pwm
->state
= ASYNC_DONE
;
1080 gpg_error_t
pwmd_pending_line(pwm_t
*pwm
)
1083 return GPG_ERR_INV_ARG
;
1086 return GPG_ERR_INV_STATE
;
1088 return assuan_pending_line(pwm
->ctx
) ? 0 : GPG_ERR_NO_DATA
;
1091 static pwmd_async_t
reset_async(pwm_t
*pwm
, int done
)
1093 pwm
->state
= ASYNC_INIT
;
1094 pwm
->cmd
= ASYNC_CMD_NONE
;
1096 #ifdef WITH_PINENTRY
1097 if (pwm
->nb_fd
!= -1) {
1103 if (done
&& pwm
->tcp_conn
&& pwm
->tcp_conn
->fd
!= -1) {
1104 close(pwm
->tcp_conn
->fd
);
1105 pwm
->tcp_conn
->fd
= -1;
1112 pwmd_async_t
pwmd_process(pwm_t
*pwm
, gpg_error_t
*rc
, char **result
)
1116 struct timeval tv
= {0, 0};
1122 return GPG_ERR_INV_ARG
;
1127 *rc
= GPG_ERR_INV_ARG
;
1130 else if (!pwm
->ctx
) {
1133 *rc
= GPG_ERR_INV_STATE
;
1137 case ASYNC_CMD_CONNECT
:
1138 case ASYNC_CMD_HOSTKEY
:
1144 /* When not in a command, this will let libassuan process status messages
1145 * by calling PWMD_OPTION_STATUS_FUNC. The client can poll the file
1146 * descriptor returned by pwmd_get_fd() to determine when this should be
1147 * called or call pwmd_pending_line() to determine whether a buffered line
1148 * needs to be processed. */
1149 if (pwm
->cmd
== ASYNC_CMD_NONE
) {
1150 *rc
= assuan_command(pwm
, pwm
->ctx
, NULL
, "NOP");
1154 /* Fixes pwmd_open/save_async2() when there is a cached or new file. */
1155 if (pwm
->state
== ASYNC_DONE
) {
1156 reset_async(pwm
, 0);
1160 if (pwm
->state
!= ASYNC_PROCESS
) {
1161 *rc
= GPG_ERR_INV_STATE
;
1166 if (pwm
->cmd
== ASYNC_CMD_DNS
) {
1169 if (pwm
->tcp_conn
->rc
) {
1170 *rc
= pwm
->tcp_conn
->rc
;
1171 reset_async(pwm
, 1);
1177 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1179 /* Shouldn't happen. */
1184 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
1186 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
1190 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1194 else if (pwm
->cmd
== ASYNC_CMD_CONNECT
) {
1195 if (pwm
->tcp_conn
->rc
== GPG_ERR_EINPROGRESS
) {
1197 socklen_t len
= sizeof(int);
1200 FD_SET(pwm
->tcp_conn
->fd
, &fds
);
1202 n
= pth_select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1204 n
= select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1207 if (!n
|| !FD_ISSET(pwm
->tcp_conn
->fd
, &fds
))
1210 *rc
= gpg_error_from_syserror();
1211 reset_async(pwm
, 1);
1215 ret
= getsockopt(pwm
->tcp_conn
->fd
, SOL_SOCKET
, SO_ERROR
, &n
, &len
);
1218 *rc
= ret
? gpg_error_from_syserror() : gpg_error_from_errno(n
);
1219 reset_async(pwm
, 1);
1223 else if (pwm
->tcp_conn
->rc
) {
1224 *rc
= pwm
->tcp_conn
->rc
;
1225 reset_async(pwm
, 1);
1229 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, 0);
1230 *rc
= setup_tcp_session(pwm
);
1233 switch (pwm
->tcp_conn
->cmd
) {
1234 case ASYNC_CMD_HOSTKEY
:
1236 *result
= pwm
->result
;
1243 return reset_async(pwm
, *rc
? 1 : 0);
1247 #ifdef WITH_PINENTRY
1248 if (pwm
->cmd
== ASYNC_CMD_OPEN2
|| pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1251 if (pwm
->nb_fd
== -1) {
1252 *rc
= GPG_ERR_INV_STATE
;
1253 return reset_async(pwm
, 0);
1257 FD_SET(pwm
->nb_fd
, &fds
);
1258 FD_SET(pwm
->fd
, &fds
);
1260 n
= pth_select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1262 n
= select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1265 *rc
= gpg_error_from_syserror();
1266 return reset_async(pwm
, 0);
1269 if (n
> 0 && FD_ISSET(pwm
->nb_fd
, &fds
)) {
1270 pwmd_nb_status_t nb
;
1272 size_t len
= pth_read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1274 size_t len
= read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1276 waitpid(pwm
->nb_pid
, &status
, WNOHANG
);
1278 if (len
!= sizeof(nb
)) {
1279 *rc
= gpg_error_from_syserror();
1280 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1285 if (*rc
== GPG_ERR_INV_PASSPHRASE
&& pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1286 reset_async(pwm
, 0);
1287 *rc
= pwmd_save_async2(pwm
);
1288 return ASYNC_PROCESS
;
1291 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1293 if (pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1294 *rc
= do_save_command(pwm
, nb
.password
);
1295 memset(&nb
, 0, sizeof(pwmd_nb_status_t
));
1296 return reset_async(pwm
, 0);
1299 if (pwm
->cmd
== ASYNC_CMD_OPEN2
) {
1300 *rc
= do_open_command(pwm
, pwm
->filename
, nb
.password
);
1301 memset(&nb
, 0, sizeof(pwmd_nb_status_t
));
1303 if (*rc
== GPG_ERR_INV_PASSPHRASE
) {
1304 if (++pwm
->pin_try
< pwm
->pinentry_tries
) {
1305 int n
= pwm
->pin_try
;
1307 reset_async(pwm
, 0);
1309 pwm
->cmd
= ASYNC_CMD_OPEN2
;
1310 *rc
= pwmd_open_async2(pwm
, pwm
->filename
);
1313 return reset_async(pwm
, 1);
1319 return reset_async(pwm
, *rc
? 1 : 0);
1323 /* Fall through so status messages can be processed during the
1329 *rc
= GPG_ERR_INV_STATE
;
1330 return reset_async(pwm
, 0);
1333 /* This is for the non-blocking OPEN and SAVE commands. */
1335 FD_SET(pwm
->fd
, &fds
);
1337 n
= pth_select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1339 n
= select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1343 *rc
= gpg_error_from_syserror();
1344 return reset_async(pwm
, 0);
1348 if (FD_ISSET(pwm
->fd
, &fds
))
1349 *rc
= parse_assuan_line(pwm
);
1352 while (!*rc
&& assuan_pending_line(pwm
->ctx
))
1353 *rc
= parse_assuan_line(pwm
);
1355 /* For pinentry retries. */
1356 if (pwm
->cmd
== ASYNC_CMD_OPEN
&&
1357 gpg_err_code(*rc
) == GPG_ERR_INV_PASSPHRASE
&&
1358 ++pwm
->pin_try
< pwm
->pinentry_tries
) {
1359 pwm
->state
= ASYNC_INIT
;
1360 *rc
= pwmd_open_async(pwm
, pwm
->filename
);
1364 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN
? 1 : 0);
1366 if (pwm
->state
== ASYNC_DONE
) {
1367 reset_async(pwm
, 0);
1374 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
1375 char **result
, const char *cmd
)
1383 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
1384 pwm
->status_func
, pwm
->status_data
);
1388 pwmd_free(data
.buf
);
1394 mem_realloc_cb(&data
, "", 1);
1397 pwmd_free(data
.buf
);
1398 rc
= GPG_ERR_INV_ARG
;
1401 *result
= (char *)data
.buf
;
1405 return gpg_err_code(rc
);
1408 gpg_error_t
pwmd_inquire(pwm_t
*pwm
, const char *cmd
, pwmd_inquire_cb_t fn
,
1411 if (!pwm
|| !cmd
|| !fn
)
1412 return GPG_ERR_INV_ARG
;
1415 return GPG_ERR_INV_STATE
;
1417 pwm
->inquire_func
= fn
;
1418 pwm
->inquire_data
= data
;
1419 return assuan_command(pwm
, pwm
->ctx
, NULL
, cmd
);
1422 #ifdef WITH_PINENTRY
1423 static gpg_error_t
terminate_pinentry(pwm_t
*pwm
)
1425 pid_t pid
= pwm
->pid
;
1429 if (!pwm
|| pid
== -1)
1430 return GPG_ERR_INV_ARG
;
1432 if (kill(pid
, 0) == 0) {
1433 if (kill(pid
, SIGTERM
) == -1) {
1434 if (kill(pid
, SIGKILL
) == -1)
1435 return gpg_error_from_errno(errno
);
1439 return gpg_error_from_errno(errno
);
1444 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
1449 tmp
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1452 return gpg_error_from_errno(ENOMEM
);
1455 pwm
->title
= pwmd_strdup(N_("Password Manager Daemon"));
1461 pwm
->prompt
= pwmd_strdup(N_("Passphrase:"));
1466 if (!pwm
->desc
&& (which
== PINENTRY_OPEN
|| which
== PINENTRY_SAVE
)) {
1467 if (which
== PINENTRY_OPEN
)
1468 desc
= pwmd_strdup_printf(N_("A passphrase is required to open the file \"%s\". Please%%0Aenter the passphrase below."), pwm
->filename
);
1470 desc
= pwmd_strdup_printf(N_("A passphrase is required to save to the file \"%s\". Please%%0Aenter the passphrase below."), pwm
->filename
);
1482 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s", desc
);
1484 if (pwm
->desc
!= desc
)
1487 case PINENTRY_OPEN_FAILED
:
1488 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1489 N_("Invalid passphrase, please try again."));
1491 case PINENTRY_SAVE_CONFIRM
:
1492 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1493 N_("Please type the passphrase again for confirmation."));
1497 error
= pinentry_command(pwm
, NULL
, tmp
);
1504 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETPROMPT %s", pwm
->prompt
);
1505 error
= pinentry_command(pwm
, NULL
, tmp
);
1512 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETDESC %s", pwm
->title
);
1513 error
= pinentry_command(pwm
, NULL
, tmp
);
1519 return gpg_error_from_errno(ENOMEM
);
1522 static void update_pinentry_settings(pwm_t
*pwm
)
1528 char *pwbuf
= _getpwuid(&pw
);
1533 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
.pw_dir
);
1536 if ((fp
= fopen(buf
, "r")) == NULL
)
1539 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
1540 char name
[32], val
[256];
1542 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
1545 if (strcasecmp(name
, "TTYNAME") == 0) {
1546 pwmd_free(pwm
->pinentry_tty
);
1547 pwm
->pinentry_tty
= pwmd_strdup(val
);
1549 else if (strcasecmp(name
, "TTYTYPE") == 0) {
1550 pwmd_free(pwm
->pinentry_term
);
1551 pwm
->pinentry_term
= pwmd_strdup(val
);
1553 else if (strcasecmp(name
, "DISPLAY") == 0) {
1554 pwmd_free(pwm
->pinentry_display
);
1555 pwm
->pinentry_display
= pwmd_strdup(val
);
1557 else if (strcasecmp(name
, "PATH") == 0) {
1558 pwmd_free(pwm
->pinentry_path
);
1559 pwm
->pinentry_path
= pwmd_strdup(val
);
1566 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
1569 assuan_context_t ctx
;
1570 int child_list
[] = {-1};
1571 char *display
= getenv("DISPLAY");
1572 const char *argv
[10];
1573 const char **p
= argv
;
1574 int have_display
= 0;
1576 char *ttybuf
= NULL
;
1578 update_pinentry_settings(pwm
);
1580 if (pwm
->pinentry_display
|| display
)
1583 if (!pwm
->pinentry_tty
) {
1584 ttybuf
= pwmd_malloc(255);
1587 return gpg_error_from_errno(ENOMEM
);
1589 rc
= ttyname_r(STDOUT_FILENO
, ttybuf
, 255);
1593 return gpg_error_from_errno(rc
);
1599 tty
= pwm
->pinentry_tty
;
1602 if (!have_display
&& !tty
)
1603 return GPG_ERR_ENOTTY
;
1606 *p
++ = have_display
? "--display" : "--ttyname";
1607 *p
++ = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
1610 *p
++ = "--lc-ctype";
1611 *p
++ = pwm
->lcctype
;
1614 if (pwm
->lcmessages
) {
1615 *p
++ = "--lc-messages";
1616 *p
++ = pwm
->lcmessages
;
1621 if (!have_display
) {
1623 *p
++ = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
1627 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
1635 pwm
->pid
= assuan_get_pid(ctx
);
1637 return set_pinentry_strings(pwm
, 0);
1640 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1645 n
= launch_pinentry(pwm
);
1651 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
1654 static void pinentry_disconnect(pwm_t
*pwm
)
1657 assuan_disconnect(pwm
->pctx
);
1664 * Only called from a child process.
1666 static void catchsig(int sig
)
1670 if (gelapsed
++ >= gtimeout
) {
1671 terminate_pinentry(gpwm
);
1672 gerror
= GPG_ERR_TIMEOUT
;
1684 * Borrowed from libassuan.
1686 static char *percent_escape(const char *atext
)
1688 const unsigned char *s
;
1689 int len
= strlen(atext
) * 3 + 1;
1690 char *buf
= (char *)pwmd_malloc(len
), *p
= buf
;
1695 for (s
=(const unsigned char *)atext
; *s
; s
++) {
1697 sprintf (p
, "%%%02X", *s
);
1708 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1711 return GPG_ERR_INV_ARG
;
1713 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
1716 gpg_error_t
pwmd_command_ap(pwm_t
*pwm
, char **result
, const char *cmd
,
1725 return GPG_ERR_INV_ARG
;
1728 return GPG_ERR_INV_STATE
;
1731 * C99 allows the dst pointer to be null which will calculate the length
1732 * of the would-be result and return it.
1735 len
= vsnprintf(NULL
, 0, cmd
, ap
)+1;
1736 buf
= (char *)pwmd_malloc(len
);
1740 return gpg_error_from_errno(ENOMEM
);
1743 len
= vsnprintf(buf
, len
, cmd
, ap2
);
1746 if (buf
[strlen(buf
)-1] == '\n')
1747 buf
[strlen(buf
)-1] = 0;
1749 if (buf
[strlen(buf
)-1] == '\r')
1750 buf
[strlen(buf
)-1] = 0;
1752 error
= send_command(pwm
, result
, buf
);
1757 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
1763 return GPG_ERR_INV_ARG
;
1766 return GPG_ERR_INV_STATE
;
1772 error
= pwmd_command_ap(pwm
, result
, cmd
, ap
);
1777 #ifdef WITH_PINENTRY
1778 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
1781 signal(SIGALRM
, catchsig
);
1786 return pinentry_command(pwm
, result
, "GETPIN");
1789 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int which
)
1794 rc
= set_pinentry_strings(pwm
, which
);
1797 pinentry_disconnect(pwm
);
1801 rc
= do_getpin(pwm
, result
);
1804 * Since there was input cancel any timeout setting.
1807 signal(SIGALRM
, SIG_DFL
);
1811 pinentry_disconnect(pwm
);
1813 /* This lets pwmd_open2() with PWMD_OPTION_PINENTRY_TIMEOUT work. */
1814 if (rc
== GPG_ERR_EOF
&& gerror
== GPG_ERR_TIMEOUT
)
1824 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
1828 char *result
= NULL
;
1830 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1833 return gpg_error_from_errno(ENOMEM
);
1835 snprintf(buf
, ASSUAN_LINELENGTH
, "OPEN %s %s", filename
,
1836 password
? password
: "");
1837 error
= send_command(pwm
, &result
, buf
);
1840 if (error
&& result
)
1846 static gpg_error_t
send_pinentry_options(pwm_t
*pwm
)
1850 if (pwm
->pinentry_path
) {
1851 rc
= pwmd_command(pwm
, NULL
, "OPTION PATH=%s", pwm
->pinentry_path
);
1857 if (pwm
->pinentry_tty
) {
1858 rc
= pwmd_command(pwm
, NULL
, "OPTION TTYNAME=%s", pwm
->pinentry_tty
);
1864 if (pwm
->pinentry_term
) {
1865 rc
= pwmd_command(pwm
, NULL
, "OPTION TTYTYPE=%s", pwm
->pinentry_term
);
1871 if (pwm
->pinentry_display
) {
1872 rc
= pwmd_command(pwm
, NULL
, "OPTION TITLE=%s", pwm
->pinentry_display
);
1879 rc
= pwmd_command(pwm
, NULL
, "OPTION TITLE=%s", pwm
->title
);
1886 rc
= pwmd_command(pwm
, NULL
, "OPTION DESC=%s", pwm
->desc
);
1893 rc
= pwmd_command(pwm
, NULL
, "OPTION PROMPT=%s", pwm
->prompt
);
1900 rc
= pwmd_command(pwm
, NULL
, "OPTION LC_CTYPE=%s", pwm
->lcctype
);
1906 if (pwm
->lcmessages
) {
1907 rc
= pwmd_command(pwm
, NULL
, "OPTION LC_MESSAGES=%s", pwm
->lcmessages
);
1913 if (pwm
->pinentry_timeout
>= 0) {
1914 rc
= pwmd_command(pwm
, NULL
, "OPTION TIMEOUT=%i", pwm
->pinentry_timeout
);
1923 static gpg_error_t
do_pwmd_open(pwm_t
*pwm
, const char *filename
, int nb
,
1926 char *result
= NULL
;
1927 char *password
= NULL
;
1933 if (!pwm
|| !filename
|| !*filename
)
1934 return GPG_ERR_INV_ARG
;
1937 return GPG_ERR_INV_STATE
;
1939 pin_try
= pwm
->pinentry_tries
- 1;
1942 * Avoid calling pinentry if the password is cached on the server or if
1943 * this is a new file. pwmd version 2 adds a VERSION command which is
1944 * determined in _socket_connect_finalize(). If the server is version 2,
1945 * ISCACHED can determine if a file exists.
1948 if (!pwm
->tcp_conn
&& pwm
->version
== PWMD_V1
) {
1950 if (pwm
->version
== PWMD_V1
) {
1952 rc
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
1957 len
= strlen(result
)+strlen(filename
)+2;
1958 path
= pwmd_malloc(len
);
1962 return gpg_error_from_errno(ENOMEM
);
1965 snprintf(path
, len
, "%s/%s", result
, filename
);
1968 if (access(path
, R_OK
) == -1) {
1969 if (errno
== ENOENT
) {
1978 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
1980 if (gpg_err_code(rc
) == GPG_ERR_ENOENT
)
1983 if (rc
&& rc
!= GPG_ERR_NOT_FOUND
)
1986 if (!nb
&& rc
== GPG_ERR_NOT_FOUND
) {
1987 if (pwm
->password
) {
1988 password
= pwm
->password
;
1992 if (pwm
->passfunc
) {
1993 rc
= pwm
->passfunc(pwm
->passdata
, &password
);
2002 #ifdef WITH_PINENTRY
2003 if (rc
== GPG_ERR_NOT_FOUND
&& local_pinentry
) {
2004 rc
= pwmd_command(pwm
, NULL
, "OPTION PINENTRY=0");
2010 pwm
->filename
= pwmd_strdup(filename
);
2013 return gpg_error_from_errno(ENOMEM
);
2015 /* Get the passphrase using the LOCAL pinentry. */
2019 pwmd_nb_status_t pw
;
2022 return gpg_error_from_syserror();
2035 if (pwm
->pinentry_timeout
!= 0) {
2037 gtimeout
= abs(pwm
->pinentry_timeout
);
2041 pw
.error
= getpin(pwm
, &password
, PINENTRY_OPEN
);
2043 if (gtimeout
&& gelapsed
>= gtimeout
)
2044 pw
.error
= GPG_ERR_TIMEOUT
;
2047 snprintf(pw
.password
, sizeof(pw
.password
), "%s",
2050 pinentry_disconnect(pwm
);
2052 pth_write(p
[1], &pw
, sizeof(pw
));
2054 write(p
[1], &pw
, sizeof(pw
));
2056 memset(&pw
, 0, sizeof(pw
));
2061 rc
= gpg_error_from_syserror();
2075 if (pwm
->pinentry_timeout
!= 0) {
2077 gtimeout
= abs(pwm
->pinentry_timeout
);
2081 rc
= getpin(pwm
, &password
, PINENTRY_OPEN
);
2083 /* Don't timeout when an invalid passphrase was entered. */
2092 pwm
->state
= ASYNC_DONE
;
2094 if (!local_pinentry
) {
2095 rc
= send_pinentry_options(pwm
);
2101 rc
= do_open_command(pwm
, filename
, password
);
2104 * Keep the user defined password set with pwmd_setopt(). The password may
2105 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
2107 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
2108 pwmd_free(password
);
2110 if (rc
== GPG_ERR_INV_PASSPHRASE
) {
2111 if (pin_try
-- > 0 && !nb
) {
2113 #ifdef WITH_PINENTRY
2115 rc
= getpin(pwm
, &password
, PINENTRY_OPEN_FAILED
);
2118 rc
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
2119 N_("Invalid passphrase, please try again."));
2127 #ifdef WITH_PINENTRY
2129 pinentry_disconnect(pwm
);
2137 pwmd_free(pwm
->filename
);
2139 pwm
->filename
= pwmd_strdup(filename
);
2145 gpg_error_t
pwmd_open2(pwm_t
*pwm
, const char *filename
)
2147 #ifndef WITH_PINENTRY
2148 return GPG_ERR_NOT_IMPLEMENTED
;
2150 return do_pwmd_open(pwm
, filename
, 0, 1);
2154 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
2156 return do_pwmd_open(pwm
, filename
, 0, 0);
2159 gpg_error_t
pwmd_open_async2(pwm_t
*pwm
, const char *filename
)
2161 #ifndef WITH_PINENTRY
2162 return GPG_ERR_NOT_IMPLEMENTED
;
2166 if (!pwm
|| !filename
)
2167 return GPG_ERR_INV_ARG
;
2170 return GPG_ERR_INV_STATE
;
2172 if (pwm
->cmd
!= ASYNC_CMD_OPEN2
)
2175 pwm
->cmd
= ASYNC_CMD_OPEN2
;
2176 pwm
->state
= ASYNC_PROCESS
;
2177 rc
= do_pwmd_open(pwm
, filename
, 1, 1);
2180 reset_async(pwm
, 1);
2186 #ifdef WITH_PINENTRY
2187 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
2191 char *result
= NULL
;
2194 error
= getpin(pwm
, &result
, confirm
? PINENTRY_SAVE_CONFIRM
: PINENTRY_SAVE
);
2198 pinentry_disconnect(pwm
);
2201 pwmd_free(*password
);
2211 if (strcmp(*password
, result
)) {
2212 pwmd_free(*password
);
2220 pinentry_disconnect(pwm
);
2225 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
2229 char *result
= NULL
;
2231 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
2234 return gpg_error_from_errno(ENOMEM
);
2236 snprintf(buf
, ASSUAN_LINELENGTH
, "SAVE %s", password
? password
: "");
2237 error
= send_command(pwm
, &result
, buf
);
2240 if (error
&& result
)
2246 static gpg_error_t
do_pwmd_save(pwm_t
*pwm
, int nb
, int local_pinentry
)
2248 char *result
= NULL
;
2249 char *password
= NULL
;
2253 return GPG_ERR_INV_ARG
;
2256 return GPG_ERR_INV_STATE
;
2258 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
2260 if (rc
&& rc
!= GPG_ERR_NOT_FOUND
)
2263 if (!nb
&& rc
== GPG_ERR_NOT_FOUND
) {
2264 if (pwm
->password
) {
2265 password
= pwm
->password
;
2269 if (pwm
->passfunc
) {
2270 rc
= pwm
->passfunc(pwm
->passdata
, &password
);
2279 if (rc
== GPG_ERR_NOT_FOUND
&& local_pinentry
) {
2280 #ifdef WITH_PINENTRY
2281 /* Get the password using the LOCAL pinentry. */
2285 pwmd_nb_status_t pw
;
2288 return gpg_error_from_syserror();
2301 pw
.error
= do_save_getpin(pwm
, &password
);
2302 pinentry_disconnect(pwm
);
2303 snprintf(pw
.password
, sizeof(pw
.password
), "%s",
2306 pth_write(p
[1], &pw
, sizeof(pw
));
2308 write(p
[1], &pw
, sizeof(pw
));
2310 memset(&pw
, 0, sizeof(pw
));
2315 rc
= gpg_error_from_syserror();
2329 rc
= do_save_getpin(pwm
, &password
);
2336 pwm
->state
= ASYNC_DONE
;
2339 if (!local_pinentry
) {
2340 rc
= send_pinentry_options(pwm
);
2346 rc
= do_save_command(pwm
, password
);
2348 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
2349 pwmd_free(password
);
2354 gpg_error_t
pwmd_save_async2(pwm_t
*pwm
)
2356 #ifndef WITH_PINENTRY
2357 return GPG_ERR_NOT_IMPLEMENTED
;
2362 return GPG_ERR_INV_ARG
;
2365 return GPG_ERR_INV_STATE
;
2367 pwm
->cmd
= ASYNC_CMD_SAVE2
;
2368 pwm
->state
= ASYNC_PROCESS
;
2369 rc
= do_pwmd_save(pwm
, 1, 1);
2372 reset_async(pwm
, 0);
2378 gpg_error_t
pwmd_save2(pwm_t
*pwm
)
2380 #ifndef WITH_PINENTRY
2381 return GPG_ERR_NOT_IMPLEMENTED
;
2383 return do_pwmd_save(pwm
, 0, 1);
2387 gpg_error_t
pwmd_save(pwm_t
*pwm
)
2389 return do_pwmd_save(pwm
, 0, 0);
2392 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
2395 int n
= va_arg(ap
, int);
2397 gpg_error_t error
= 0;
2400 return GPG_ERR_INV_ARG
;
2405 case PWMD_OPTION_STATUS_CB
:
2406 pwm
->status_func
= va_arg(ap
, pwmd_status_cb_t
);
2408 case PWMD_OPTION_STATUS_DATA
:
2409 pwm
->status_data
= va_arg(ap
, void *);
2411 case PWMD_OPTION_PASSPHRASE_CB
:
2412 pwm
->passfunc
= va_arg(ap
, pwmd_passphrase_cb_t
);
2414 case PWMD_OPTION_PASSPHRASE_DATA
:
2415 pwm
->passdata
= va_arg(ap
, void *);
2417 case PWMD_OPTION_PASSPHRASE
:
2418 arg1
= va_arg(ap
, char *);
2421 pwmd_free(pwm
->password
);
2423 pwm
->password
= pwmd_strdup(arg1
);
2425 case PWMD_OPTION_PINENTRY_TRIES
:
2426 n
= va_arg(ap
, int);
2430 error
= GPG_ERR_INV_VALUE
;
2433 pwm
->pinentry_tries
= n
;
2435 case PWMD_OPTION_PINENTRY_TIMEOUT
:
2436 n
= va_arg(ap
, int);
2440 error
= GPG_ERR_INV_VALUE
;
2443 pwm
->pinentry_timeout
= n
;
2445 case PWMD_OPTION_PINENTRY_PATH
:
2446 if (pwm
->pinentry_path
)
2447 pwmd_free(pwm
->pinentry_path
);
2449 pwm
->pinentry_path
= pwmd_strdup(va_arg(ap
, char *));
2451 case PWMD_OPTION_PINENTRY_TTY
:
2452 if (pwm
->pinentry_tty
)
2453 pwmd_free(pwm
->pinentry_tty
);
2455 pwm
->pinentry_tty
= pwmd_strdup(va_arg(ap
, char *));
2457 case PWMD_OPTION_PINENTRY_DISPLAY
:
2458 if (pwm
->pinentry_display
)
2459 pwmd_free(pwm
->pinentry_display
);
2461 pwm
->pinentry_display
= pwmd_strdup(va_arg(ap
, char *));
2463 case PWMD_OPTION_PINENTRY_TERM
:
2464 if (pwm
->pinentry_term
)
2465 pwmd_free(pwm
->pinentry_term
);
2467 pwm
->pinentry_term
= pwmd_strdup(va_arg(ap
, char *));
2469 case PWMD_OPTION_PINENTRY_TITLE
:
2471 pwmd_free(pwm
->title
);
2473 pwm
->title
= percent_escape(va_arg(ap
, char *));
2475 case PWMD_OPTION_PINENTRY_PROMPT
:
2477 pwmd_free(pwm
->prompt
);
2479 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
2481 case PWMD_OPTION_PINENTRY_DESC
:
2483 pwmd_free(pwm
->desc
);
2485 pwm
->desc
= percent_escape(va_arg(ap
, char *));
2487 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
2489 pwmd_free(pwm
->lcctype
);
2491 pwm
->lcctype
= pwmd_strdup(va_arg(ap
, char *));
2493 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
2494 if (pwm
->lcmessages
)
2495 pwmd_free(pwm
->lcmessages
);
2497 pwm
->lcmessages
= pwmd_strdup(va_arg(ap
, char *));
2500 case PWMD_OPTION_IP_VERSION
:
2501 n
= va_arg(ap
, int);
2510 error
= GPG_ERR_INV_VALUE
;
2518 error
= GPG_ERR_NOT_IMPLEMENTED
;
2526 gpg_error_t
pwmd_get_fds(pwm_t
*pwm
, pwmd_fd_t
*fds
, int *n_fds
)
2531 int afds
[ARES_GETSOCK_MAXNUM
];
2536 if (!pwm
|| !fds
|| !n_fds
|| *n_fds
<= 0)
2537 return GPG_ERR_INV_ARG
;
2541 memset(afds
, 0, sizeof(int)*ARES_GETSOCK_MAXNUM
);
2543 memset(fds
, 0, sizeof(pwmd_fd_t
)*in_total
);
2548 case ASYNC_CMD_NONE
:
2549 case ASYNC_CMD_OPEN
:
2550 case ASYNC_CMD_SAVE
:
2551 #ifdef WITH_PINENTRY
2555 return GPG_ERR_INV_STATE
;
2558 fds
[fd
].fd
= pwm
->fd
;
2559 fds
[fd
++].flags
= PWMD_FD_READABLE
;
2561 #ifdef WITH_PINENTRY
2562 case ASYNC_CMD_OPEN2
:
2563 case ASYNC_CMD_SAVE2
:
2564 if (pwm
->nb_fd
== -1)
2565 return GPG_ERR_INV_STATE
;
2568 fds
[fd
].fd
= pwm
->nb_fd
;
2569 fds
[fd
++].flags
= PWMD_FD_READABLE
;
2574 if (!pwm
->tcp_conn
|| !pwm
->tcp_conn
->chan
)
2575 return GPG_ERR_INV_STATE
;
2577 n
= ares_getsock(pwm
->tcp_conn
->chan
, afds
, ARES_GETSOCK_MAXNUM
);
2579 for (i
= 0; i
< ARES_GETSOCK_MAXNUM
; i
++) {
2582 if (fd
> in_total
) {
2584 return GPG_ERR_ERANGE
;
2587 if (ARES_GETSOCK_READABLE(n
, i
)) {
2589 fds
[fd
].flags
|= PWMD_FD_READABLE
;
2592 if (ARES_GETSOCK_WRITABLE(n
, i
)) {
2594 fds
[fd
].flags
|= PWMD_FD_WRITABLE
;
2598 fds
[fd
++].fd
= afds
[i
];
2603 case ASYNC_CMD_CONNECT
:
2604 case ASYNC_CMD_HOSTKEY
:
2605 if (!pwm
->tcp_conn
|| pwm
->tcp_conn
->fd
== -1)
2606 return GPG_ERR_INV_STATE
;
2609 fds
[fd
].fd
= pwm
->tcp_conn
->fd
;
2610 fds
[fd
++].flags
= PWMD_FD_READABLE
;
2615 return GPG_ERR_INV_STATE
;
2618 pwm_t
*pwmd_new(const char *name
)
2620 pwm_t
*h
= pwmd_calloc(1, sizeof(pwm_t
));
2626 h
->name
= pwmd_strdup(name
);
2635 #ifdef WITH_PINENTRY
2638 h
->pinentry_timeout
= -30;
2639 h
->pinentry_tries
= 3;
2641 h
->prot
= PWMD_IP_ANY
;
2646 void pwmd_free(void *ptr
)
2651 void *pwmd_malloc(size_t size
)
2653 return xmalloc(size
);
2656 void *pwmd_calloc(size_t nmemb
, size_t size
)
2658 return xcalloc(nmemb
, size
);
2661 void *pwmd_realloc(void *ptr
, size_t size
)
2663 return xrealloc(ptr
, size
);
2666 char *pwmd_strdup(const char *str
)
2668 return xstrdup(str
);
2671 char *pwmd_strdup_printf(const char *fmt
, ...)
2681 len
= vsnprintf(NULL
, 0, fmt
, ap
);
2682 buf
= pwmd_malloc(++len
);
2689 vsnprintf(buf
, len
, fmt
, ap
);