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_CHECKSUM
;
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 gpg_error_t
pwmd_connect(pwm_t
*pwm
, const char *path
)
814 char *socketpath
= NULL
;
815 assuan_context_t ctx
;
821 return GPG_ERR_INV_ARG
;
823 pwbuf
= _getpwuid(&pw
);
826 return gpg_error_from_errno(errno
);
829 socketpath
= (char *)pwmd_malloc(strlen(pw
.pw_dir
) + strlen("/.pwmd/socket") + 1);
830 sprintf(socketpath
, "%s/.pwmd/socket", pw
.pw_dir
);
833 socketpath
= pwmd_strdup(path
);
836 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
837 pwmd_free(socketpath
);
843 return _socket_connect_finalize(pwm
);
846 void pwmd_close(pwm_t
*pwm
)
852 assuan_disconnect(pwm
->ctx
);
855 pwmd_free(pwm
->password
);
858 pwmd_free(pwm
->title
);
861 pwmd_free(pwm
->desc
);
864 pwmd_free(pwm
->prompt
);
866 if (pwm
->pinentry_tty
)
867 pwmd_free(pwm
->pinentry_tty
);
869 if (pwm
->pinentry_display
)
870 pwmd_free(pwm
->pinentry_display
);
872 if (pwm
->pinentry_term
)
873 pwmd_free(pwm
->pinentry_term
);
876 pwmd_free(pwm
->lcctype
);
879 pwmd_free(pwm
->lcmessages
);
882 pwmd_free(pwm
->filename
);
885 pwmd_free(pwm
->name
);
889 free_tcp_conn(pwm
->tcp_conn
);
895 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
897 membuf_t
*mem
= (membuf_t
*)data
;
903 if ((p
= pwmd_realloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
907 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
912 static int _inquire_cb(void *data
, const char *keyword
)
914 pwm_t
*pwm
= (pwm_t
*)data
;
916 int flags
= fcntl(pwm
->fd
, F_GETFL
);
918 /* Shouldn't get this far without a callback. */
919 if (!pwm
->inquire_func
)
920 return GPG_ERR_INV_ARG
;
923 * Since the socket file descriptor is probably set to non-blocking, set to
924 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixed when
925 * asynchronous INQUIRE is supported by either libassuan or a later
928 fcntl(pwm
->fd
, F_SETFL
, 0);
935 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
, &len
);
936 rc
= gpg_err_code(rc
);
938 if (rc
== GPG_ERR_EOF
|| !rc
) {
939 if (len
<= 0 || !result
) {
944 arc
= assuan_send_data(pwm
->ctx
, result
, len
);
946 if (rc
== GPG_ERR_EOF
) {
957 fcntl(pwm
->fd
, F_SETFL
, flags
);
961 static gpg_error_t
do_nb_command(pwm_t
*pwm
, const char *cmd
, ...)
967 if (pwm
->state
== ASYNC_DONE
)
968 pwm
->state
= ASYNC_INIT
;
970 if (pwm
->state
!= ASYNC_INIT
)
971 return GPG_ERR_INV_STATE
;
973 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
976 return gpg_error_from_errno(ENOMEM
);
979 vsnprintf(buf
, ASSUAN_LINELENGTH
, cmd
, ap
);
981 rc
= assuan_write_line(pwm
->ctx
, buf
);
985 pwm
->state
= ASYNC_PROCESS
;
990 gpg_error_t
pwmd_open_async(pwm_t
*pwm
, const char *filename
)
992 if (!pwm
|| !filename
)
993 return GPG_ERR_INV_ARG
;
996 return GPG_ERR_INV_STATE
;
998 if (pwm
->cmd
!= ASYNC_CMD_OPEN
) {
1004 pwmd_free(pwm
->filename
);
1006 pwm
->filename
= pwmd_strdup(filename
);
1008 rc
= send_pinentry_options(pwm
);
1014 pwm
->cmd
= ASYNC_CMD_OPEN
;
1015 return do_nb_command(pwm
, "OPEN %s %s", filename
,
1016 pwm
->password
? pwm
->password
: "");
1019 gpg_error_t
pwmd_save_async(pwm_t
*pwm
)
1024 return GPG_ERR_INV_ARG
;
1027 return GPG_ERR_INV_STATE
;
1029 rc
= send_pinentry_options(pwm
);
1034 pwm
->cmd
= ASYNC_CMD_SAVE
;
1035 return do_nb_command(pwm
, "SAVE %s", pwm
->password
? pwm
->password
: "");
1038 static gpg_error_t
parse_assuan_line(pwm_t
*pwm
)
1044 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
1047 if (line
[0] == 'O' && line
[1] == 'K' &&
1048 (line
[2] == 0 || line
[2] == ' ')) {
1049 pwm
->state
= ASYNC_DONE
;
1051 else if (line
[0] == '#') {
1053 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' ')) {
1054 if (pwm
->status_func
) {
1055 pwm
->status_func(pwm
->status_data
,
1056 line
[1] == 0 ? line
+1 : line
+2);
1059 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
1060 (line
[3] == 0 || line
[3] == ' ')) {
1063 pwm
->state
= ASYNC_DONE
;
1070 gpg_error_t
pwmd_pending_line(pwm_t
*pwm
)
1073 return GPG_ERR_INV_ARG
;
1076 return GPG_ERR_INV_STATE
;
1078 return assuan_pending_line(pwm
->ctx
) ? 0 : GPG_ERR_NO_DATA
;
1081 static pwmd_async_t
reset_async(pwm_t
*pwm
, int done
)
1083 pwm
->state
= ASYNC_INIT
;
1084 pwm
->cmd
= ASYNC_CMD_NONE
;
1086 #ifdef WITH_PINENTRY
1087 if (pwm
->nb_fd
!= -1) {
1093 if (done
&& pwm
->tcp_conn
&& pwm
->tcp_conn
->fd
!= -1) {
1094 close(pwm
->tcp_conn
->fd
);
1095 pwm
->tcp_conn
->fd
= -1;
1102 pwmd_async_t
pwmd_process(pwm_t
*pwm
, gpg_error_t
*rc
, char **result
)
1106 struct timeval tv
= {0, 0};
1112 return GPG_ERR_INV_ARG
;
1117 *rc
= GPG_ERR_INV_ARG
;
1120 else if (!pwm
->ctx
) {
1123 *rc
= GPG_ERR_INV_STATE
;
1127 case ASYNC_CMD_CONNECT
:
1128 case ASYNC_CMD_HOSTKEY
:
1134 /* When not in a command, this will let libassuan process status messages
1135 * by calling PWMD_OPTION_STATUS_FUNC. The client can poll the file
1136 * descriptor returned by pwmd_get_fd() to determine when this should be
1137 * called or call pwmd_pending_line() to determine whether a buffered line
1138 * needs to be processed. */
1139 if (pwm
->cmd
== ASYNC_CMD_NONE
) {
1140 *rc
= assuan_command(pwm
, pwm
->ctx
, NULL
, "NOP");
1144 /* Fixes pwmd_open/save_async2() when there is a cached or new file. */
1145 if (pwm
->state
== ASYNC_DONE
) {
1146 reset_async(pwm
, 0);
1150 if (pwm
->state
!= ASYNC_PROCESS
) {
1151 *rc
= GPG_ERR_INV_STATE
;
1156 if (pwm
->cmd
== ASYNC_CMD_DNS
) {
1159 if (pwm
->tcp_conn
->rc
) {
1160 *rc
= pwm
->tcp_conn
->rc
;
1161 reset_async(pwm
, 1);
1167 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1169 /* Shouldn't happen. */
1174 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
1176 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
1180 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1184 else if (pwm
->cmd
== ASYNC_CMD_CONNECT
) {
1185 if (pwm
->tcp_conn
->rc
== GPG_ERR_EINPROGRESS
) {
1187 socklen_t len
= sizeof(int);
1190 FD_SET(pwm
->tcp_conn
->fd
, &fds
);
1192 n
= pth_select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1194 n
= select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1197 if (!n
|| !FD_ISSET(pwm
->tcp_conn
->fd
, &fds
))
1200 *rc
= gpg_error_from_syserror();
1201 reset_async(pwm
, 1);
1205 ret
= getsockopt(pwm
->tcp_conn
->fd
, SOL_SOCKET
, SO_ERROR
, &n
, &len
);
1208 *rc
= ret
? gpg_error_from_syserror() : gpg_error_from_errno(n
);
1209 reset_async(pwm
, 1);
1213 else if (pwm
->tcp_conn
->rc
) {
1214 *rc
= pwm
->tcp_conn
->rc
;
1215 reset_async(pwm
, 1);
1219 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, 0);
1220 *rc
= setup_tcp_session(pwm
);
1223 switch (pwm
->tcp_conn
->cmd
) {
1224 case ASYNC_CMD_HOSTKEY
:
1226 *result
= pwm
->result
;
1233 return reset_async(pwm
, *rc
? 1 : 0);
1237 #ifdef WITH_PINENTRY
1238 if (pwm
->cmd
== ASYNC_CMD_OPEN2
|| pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1241 if (pwm
->nb_fd
== -1) {
1242 *rc
= GPG_ERR_INV_STATE
;
1243 return reset_async(pwm
, 0);
1247 FD_SET(pwm
->nb_fd
, &fds
);
1248 FD_SET(pwm
->fd
, &fds
);
1250 n
= pth_select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1252 n
= select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1255 *rc
= gpg_error_from_syserror();
1256 return reset_async(pwm
, 0);
1259 if (n
> 0 && FD_ISSET(pwm
->nb_fd
, &fds
)) {
1260 pwmd_nb_status_t nb
;
1262 size_t len
= pth_read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1264 size_t len
= read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1266 waitpid(pwm
->nb_pid
, &status
, WNOHANG
);
1268 if (len
!= sizeof(nb
)) {
1269 *rc
= gpg_error_from_syserror();
1270 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1275 if (*rc
== GPG_ERR_INV_PASSPHRASE
&& pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1276 reset_async(pwm
, 0);
1277 *rc
= pwmd_save_async2(pwm
);
1278 return ASYNC_PROCESS
;
1281 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1283 if (pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1284 *rc
= do_save_command(pwm
, nb
.password
);
1285 memset(&nb
, 0, sizeof(pwmd_nb_status_t
));
1286 return reset_async(pwm
, 0);
1289 if (pwm
->cmd
== ASYNC_CMD_OPEN2
) {
1290 *rc
= do_open_command(pwm
, pwm
->filename
, nb
.password
);
1291 memset(&nb
, 0, sizeof(pwmd_nb_status_t
));
1293 if (*rc
== GPG_ERR_INV_PASSPHRASE
) {
1294 if (++pwm
->pin_try
< pwm
->pinentry_tries
) {
1295 int n
= pwm
->pin_try
;
1297 reset_async(pwm
, 0);
1299 pwm
->cmd
= ASYNC_CMD_OPEN2
;
1300 *rc
= pwmd_open_async2(pwm
, pwm
->filename
);
1303 return reset_async(pwm
, 1);
1309 return reset_async(pwm
, *rc
? 1 : 0);
1313 /* Fall through so status messages can be processed during the
1319 *rc
= GPG_ERR_INV_STATE
;
1320 return reset_async(pwm
, 0);
1323 /* This is for the non-blocking OPEN and SAVE commands. */
1325 FD_SET(pwm
->fd
, &fds
);
1327 n
= pth_select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1329 n
= select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1333 *rc
= gpg_error_from_syserror();
1334 return reset_async(pwm
, 0);
1338 if (FD_ISSET(pwm
->fd
, &fds
))
1339 *rc
= parse_assuan_line(pwm
);
1342 while (!*rc
&& assuan_pending_line(pwm
->ctx
))
1343 *rc
= parse_assuan_line(pwm
);
1345 /* For pinentry retries. */
1346 if (pwm
->cmd
== ASYNC_CMD_OPEN
&&
1347 gpg_err_code(*rc
) == GPG_ERR_INV_PASSPHRASE
&&
1348 ++pwm
->pin_try
< pwm
->pinentry_tries
) {
1349 pwm
->state
= ASYNC_INIT
;
1350 *rc
= pwmd_open_async(pwm
, pwm
->filename
);
1354 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN
? 1 : 0);
1356 if (pwm
->state
== ASYNC_DONE
) {
1357 reset_async(pwm
, 0);
1364 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
1365 char **result
, const char *cmd
)
1373 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
1374 pwm
->status_func
, pwm
->status_data
);
1378 pwmd_free(data
.buf
);
1384 mem_realloc_cb(&data
, "", 1);
1387 pwmd_free(data
.buf
);
1388 rc
= GPG_ERR_INV_ARG
;
1391 *result
= (char *)data
.buf
;
1395 return gpg_err_code(rc
);
1398 gpg_error_t
pwmd_inquire(pwm_t
*pwm
, const char *cmd
, pwmd_inquire_cb_t fn
,
1401 if (!pwm
|| !cmd
|| !fn
)
1402 return GPG_ERR_INV_ARG
;
1405 return GPG_ERR_INV_STATE
;
1407 pwm
->inquire_func
= fn
;
1408 pwm
->inquire_data
= data
;
1409 return assuan_command(pwm
, pwm
->ctx
, NULL
, cmd
);
1412 #ifdef WITH_PINENTRY
1413 static gpg_error_t
terminate_pinentry(pwm_t
*pwm
)
1415 pid_t pid
= pwm
->pid
;
1419 if (!pwm
|| pid
== -1)
1420 return GPG_ERR_INV_ARG
;
1422 if (kill(pid
, 0) == 0) {
1423 if (kill(pid
, SIGTERM
) == -1) {
1424 if (kill(pid
, SIGKILL
) == -1)
1425 return gpg_error_from_errno(errno
);
1429 return gpg_error_from_errno(errno
);
1434 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
1439 tmp
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1442 return gpg_error_from_errno(ENOMEM
);
1445 pwm
->title
= pwmd_strdup(N_("Password Manager Daemon"));
1451 pwm
->prompt
= pwmd_strdup(N_("Passphrase:"));
1456 if (!pwm
->desc
&& (which
== PINENTRY_OPEN
|| which
== PINENTRY_SAVE
)) {
1457 if (which
== PINENTRY_OPEN
)
1458 desc
= pwmd_strdup_printf(N_("A passphrase is required to open the file \"%s\". Please%%0Aenter the passphrase below."), pwm
->filename
);
1460 desc
= pwmd_strdup_printf(N_("A passphrase is required to save to the file \"%s\". Please%%0Aenter the passphrase below."), pwm
->filename
);
1472 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s", desc
);
1474 if (pwm
->desc
!= desc
)
1477 case PINENTRY_OPEN_FAILED
:
1478 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1479 N_("Invalid passphrase, please try again."));
1481 case PINENTRY_SAVE_CONFIRM
:
1482 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1483 N_("Please type the passphrase again for confirmation."));
1487 error
= pinentry_command(pwm
, NULL
, tmp
);
1494 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETPROMPT %s", pwm
->prompt
);
1495 error
= pinentry_command(pwm
, NULL
, tmp
);
1502 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETDESC %s", pwm
->title
);
1503 error
= pinentry_command(pwm
, NULL
, tmp
);
1509 return gpg_error_from_errno(ENOMEM
);
1512 static void update_pinentry_settings(pwm_t
*pwm
)
1518 char *pwbuf
= _getpwuid(&pw
);
1523 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
.pw_dir
);
1526 if ((fp
= fopen(buf
, "r")) == NULL
)
1529 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
1530 char name
[32], val
[256];
1532 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
1535 if (strcasecmp(name
, "TTYNAME") == 0) {
1536 pwmd_free(pwm
->pinentry_tty
);
1537 pwm
->pinentry_tty
= pwmd_strdup(val
);
1539 else if (strcasecmp(name
, "TTYTYPE") == 0) {
1540 pwmd_free(pwm
->pinentry_term
);
1541 pwm
->pinentry_term
= pwmd_strdup(val
);
1543 else if (strcasecmp(name
, "DISPLAY") == 0) {
1544 pwmd_free(pwm
->pinentry_display
);
1545 pwm
->pinentry_display
= pwmd_strdup(val
);
1547 else if (strcasecmp(name
, "PATH") == 0) {
1548 pwmd_free(pwm
->pinentry_path
);
1549 pwm
->pinentry_path
= pwmd_strdup(val
);
1556 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
1559 assuan_context_t ctx
;
1560 int child_list
[] = {-1};
1561 char *display
= getenv("DISPLAY");
1562 const char *argv
[10];
1563 const char **p
= argv
;
1564 int have_display
= 0;
1566 char *ttybuf
= NULL
;
1568 update_pinentry_settings(pwm
);
1570 if (pwm
->pinentry_display
|| display
)
1573 if (!pwm
->pinentry_tty
) {
1574 ttybuf
= pwmd_malloc(255);
1577 return gpg_error_from_errno(ENOMEM
);
1579 rc
= ttyname_r(STDOUT_FILENO
, ttybuf
, 255);
1583 return gpg_error_from_errno(rc
);
1589 tty
= pwm
->pinentry_tty
;
1592 if (!have_display
&& !tty
)
1593 return GPG_ERR_ENOTTY
;
1596 *p
++ = have_display
? "--display" : "--ttyname";
1597 *p
++ = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
1600 *p
++ = "--lc-ctype";
1601 *p
++ = pwm
->lcctype
;
1604 if (pwm
->lcmessages
) {
1605 *p
++ = "--lc-messages";
1606 *p
++ = pwm
->lcmessages
;
1611 if (!have_display
) {
1613 *p
++ = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
1617 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
1625 pwm
->pid
= assuan_get_pid(ctx
);
1627 return set_pinentry_strings(pwm
, 0);
1630 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1635 n
= launch_pinentry(pwm
);
1641 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
1644 static void pinentry_disconnect(pwm_t
*pwm
)
1647 assuan_disconnect(pwm
->pctx
);
1654 * Only called from a child process.
1656 static void catchsig(int sig
)
1660 if (gelapsed
++ >= gtimeout
) {
1661 terminate_pinentry(gpwm
);
1662 gerror
= GPG_ERR_TIMEOUT
;
1674 * Borrowed from libassuan.
1676 static char *percent_escape(const char *atext
)
1678 const unsigned char *s
;
1679 int len
= strlen(atext
) * 3 + 1;
1680 char *buf
= (char *)pwmd_malloc(len
), *p
= buf
;
1685 for (s
=(const unsigned char *)atext
; *s
; s
++) {
1687 sprintf (p
, "%%%02X", *s
);
1698 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1701 return GPG_ERR_INV_ARG
;
1703 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
1706 gpg_error_t
pwmd_command_ap(pwm_t
*pwm
, char **result
, const char *cmd
,
1715 return GPG_ERR_INV_ARG
;
1718 return GPG_ERR_INV_STATE
;
1721 * C99 allows the dst pointer to be null which will calculate the length
1722 * of the would-be result and return it.
1725 len
= vsnprintf(NULL
, 0, cmd
, ap
)+1;
1726 buf
= (char *)pwmd_malloc(len
);
1730 return gpg_error_from_errno(ENOMEM
);
1733 len
= vsnprintf(buf
, len
, cmd
, ap2
);
1736 if (buf
[strlen(buf
)-1] == '\n')
1737 buf
[strlen(buf
)-1] = 0;
1739 if (buf
[strlen(buf
)-1] == '\r')
1740 buf
[strlen(buf
)-1] = 0;
1742 error
= send_command(pwm
, result
, buf
);
1747 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
1753 return GPG_ERR_INV_ARG
;
1756 return GPG_ERR_INV_STATE
;
1762 error
= pwmd_command_ap(pwm
, result
, cmd
, ap
);
1767 #ifdef WITH_PINENTRY
1768 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
1771 signal(SIGALRM
, catchsig
);
1776 return pinentry_command(pwm
, result
, "GETPIN");
1779 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int which
)
1784 rc
= set_pinentry_strings(pwm
, which
);
1787 pinentry_disconnect(pwm
);
1791 rc
= do_getpin(pwm
, result
);
1794 * Since there was input cancel any timeout setting.
1797 signal(SIGALRM
, SIG_DFL
);
1801 pinentry_disconnect(pwm
);
1803 /* This lets pwmd_open2() with PWMD_OPTION_PINENTRY_TIMEOUT work. */
1804 if (rc
== GPG_ERR_EOF
&& gerror
== GPG_ERR_TIMEOUT
)
1814 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
1818 char *result
= NULL
;
1820 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1823 return gpg_error_from_errno(ENOMEM
);
1825 snprintf(buf
, ASSUAN_LINELENGTH
, "OPEN %s %s", filename
,
1826 password
? password
: "");
1827 error
= send_command(pwm
, &result
, buf
);
1830 if (error
&& result
)
1836 static gpg_error_t
send_pinentry_options(pwm_t
*pwm
)
1840 if (pwm
->pinentry_path
) {
1841 rc
= pwmd_command(pwm
, NULL
, "OPTION PATH=%s", pwm
->pinentry_path
);
1847 if (pwm
->pinentry_tty
) {
1848 rc
= pwmd_command(pwm
, NULL
, "OPTION TTYNAME=%s", pwm
->pinentry_tty
);
1854 if (pwm
->pinentry_term
) {
1855 rc
= pwmd_command(pwm
, NULL
, "OPTION TTYTYPE=%s", pwm
->pinentry_term
);
1861 if (pwm
->pinentry_display
) {
1862 rc
= pwmd_command(pwm
, NULL
, "OPTION TITLE=%s", pwm
->pinentry_display
);
1869 rc
= pwmd_command(pwm
, NULL
, "OPTION TITLE=%s", pwm
->title
);
1876 rc
= pwmd_command(pwm
, NULL
, "OPTION DESC=%s", pwm
->desc
);
1883 rc
= pwmd_command(pwm
, NULL
, "OPTION PROMPT=%s", pwm
->prompt
);
1890 rc
= pwmd_command(pwm
, NULL
, "OPTION LC_CTYPE=%s", pwm
->lcctype
);
1896 if (pwm
->lcmessages
) {
1897 rc
= pwmd_command(pwm
, NULL
, "OPTION LC_MESSAGES=%s", pwm
->lcmessages
);
1903 if (pwm
->pinentry_timeout
>= 0) {
1904 rc
= pwmd_command(pwm
, NULL
, "OPTION TIMEOUT=%i", pwm
->pinentry_timeout
);
1913 static gpg_error_t
do_pwmd_open(pwm_t
*pwm
, const char *filename
, int nb
,
1916 char *result
= NULL
;
1917 char *password
= NULL
;
1923 if (!pwm
|| !filename
|| !*filename
)
1924 return GPG_ERR_INV_ARG
;
1927 return GPG_ERR_INV_STATE
;
1929 pin_try
= pwm
->pinentry_tries
- 1;
1932 * Avoid calling pinentry if the password is cached on the server or if
1933 * this is a new file. pwmd version 2 adds a VERSION command which is
1934 * determined in _socket_connect_finalize(). If the server is version 2,
1935 * ISCACHED can determine if a file exists.
1938 if (!pwm
->tcp_conn
&& pwm
->version
== PWMD_V1
) {
1940 if (pwm
->version
== PWMD_V1
) {
1942 rc
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
1947 len
= strlen(result
)+strlen(filename
)+2;
1948 path
= pwmd_malloc(len
);
1952 return gpg_error_from_errno(ENOMEM
);
1955 snprintf(path
, len
, "%s/%s", result
, filename
);
1958 if (access(path
, R_OK
) == -1) {
1959 if (errno
== ENOENT
) {
1968 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
1970 if (gpg_err_code(rc
) == GPG_ERR_ENOENT
)
1973 if (rc
&& rc
!= GPG_ERR_NOT_FOUND
)
1976 if (!nb
&& rc
== GPG_ERR_NOT_FOUND
) {
1977 if (pwm
->password
) {
1978 password
= pwm
->password
;
1982 if (pwm
->passfunc
) {
1983 rc
= pwm
->passfunc(pwm
->passdata
, &password
);
1992 #ifdef WITH_PINENTRY
1993 if (rc
== GPG_ERR_NOT_FOUND
&& local_pinentry
) {
1994 rc
= pwmd_command(pwm
, NULL
, "OPTION PINENTRY=0");
2000 pwm
->filename
= pwmd_strdup(filename
);
2003 return gpg_error_from_errno(ENOMEM
);
2005 /* Get the passphrase using the LOCAL pinentry. */
2009 pwmd_nb_status_t pw
;
2012 return gpg_error_from_syserror();
2025 if (pwm
->pinentry_timeout
!= 0) {
2027 gtimeout
= abs(pwm
->pinentry_timeout
);
2031 pw
.error
= getpin(pwm
, &password
, PINENTRY_OPEN
);
2033 if (gtimeout
&& gelapsed
>= gtimeout
)
2034 pw
.error
= GPG_ERR_TIMEOUT
;
2037 snprintf(pw
.password
, sizeof(pw
.password
), "%s",
2040 pinentry_disconnect(pwm
);
2042 pth_write(p
[1], &pw
, sizeof(pw
));
2044 write(p
[1], &pw
, sizeof(pw
));
2046 memset(&pw
, 0, sizeof(pw
));
2051 rc
= gpg_error_from_syserror();
2065 if (pwm
->pinentry_timeout
!= 0) {
2067 gtimeout
= abs(pwm
->pinentry_timeout
);
2071 rc
= getpin(pwm
, &password
, PINENTRY_OPEN
);
2073 /* Don't timeout when an invalid passphrase was entered. */
2082 pwm
->state
= ASYNC_DONE
;
2084 if (!local_pinentry
) {
2085 rc
= send_pinentry_options(pwm
);
2091 rc
= do_open_command(pwm
, filename
, password
);
2094 * Keep the user defined password set with pwmd_setopt(). The password may
2095 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
2097 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
2098 pwmd_free(password
);
2100 if (rc
== GPG_ERR_INV_PASSPHRASE
) {
2101 if (pin_try
-- > 0 && !nb
) {
2103 #ifdef WITH_PINENTRY
2105 rc
= getpin(pwm
, &password
, PINENTRY_OPEN_FAILED
);
2108 rc
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
2109 N_("Invalid passphrase, please try again."));
2117 #ifdef WITH_PINENTRY
2119 pinentry_disconnect(pwm
);
2127 pwmd_free(pwm
->filename
);
2129 pwm
->filename
= pwmd_strdup(filename
);
2135 gpg_error_t
pwmd_open2(pwm_t
*pwm
, const char *filename
)
2137 #ifndef WITH_PINENTRY
2138 return GPG_ERR_NOT_IMPLEMENTED
;
2140 return do_pwmd_open(pwm
, filename
, 0, 1);
2144 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
2146 return do_pwmd_open(pwm
, filename
, 0, 0);
2149 gpg_error_t
pwmd_open_async2(pwm_t
*pwm
, const char *filename
)
2151 #ifndef WITH_PINENTRY
2152 return GPG_ERR_NOT_IMPLEMENTED
;
2156 if (!pwm
|| !filename
)
2157 return GPG_ERR_INV_ARG
;
2160 return GPG_ERR_INV_STATE
;
2162 if (pwm
->cmd
!= ASYNC_CMD_OPEN2
)
2165 pwm
->cmd
= ASYNC_CMD_OPEN2
;
2166 pwm
->state
= ASYNC_PROCESS
;
2167 rc
= do_pwmd_open(pwm
, filename
, 1, 1);
2170 reset_async(pwm
, 1);
2176 #ifdef WITH_PINENTRY
2177 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
2181 char *result
= NULL
;
2184 error
= getpin(pwm
, &result
, confirm
? PINENTRY_SAVE_CONFIRM
: PINENTRY_SAVE
);
2188 pinentry_disconnect(pwm
);
2191 pwmd_free(*password
);
2201 if (strcmp(*password
, result
)) {
2202 pwmd_free(*password
);
2210 pinentry_disconnect(pwm
);
2215 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
2219 char *result
= NULL
;
2221 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
2224 return gpg_error_from_errno(ENOMEM
);
2226 snprintf(buf
, ASSUAN_LINELENGTH
, "SAVE %s", password
? password
: "");
2227 error
= send_command(pwm
, &result
, buf
);
2230 if (error
&& result
)
2236 static gpg_error_t
do_pwmd_save(pwm_t
*pwm
, int nb
, int local_pinentry
)
2238 char *result
= NULL
;
2239 char *password
= NULL
;
2243 return GPG_ERR_INV_ARG
;
2246 return GPG_ERR_INV_STATE
;
2248 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
2250 if (rc
&& rc
!= GPG_ERR_NOT_FOUND
)
2253 if (!nb
&& rc
== GPG_ERR_NOT_FOUND
) {
2254 if (pwm
->password
) {
2255 password
= pwm
->password
;
2259 if (pwm
->passfunc
) {
2260 rc
= pwm
->passfunc(pwm
->passdata
, &password
);
2269 if (rc
== GPG_ERR_NOT_FOUND
&& local_pinentry
) {
2270 #ifdef WITH_PINENTRY
2271 /* Get the password using the LOCAL pinentry. */
2275 pwmd_nb_status_t pw
;
2278 return gpg_error_from_syserror();
2291 pw
.error
= do_save_getpin(pwm
, &password
);
2292 pinentry_disconnect(pwm
);
2293 snprintf(pw
.password
, sizeof(pw
.password
), "%s",
2296 pth_write(p
[1], &pw
, sizeof(pw
));
2298 write(p
[1], &pw
, sizeof(pw
));
2300 memset(&pw
, 0, sizeof(pw
));
2305 rc
= gpg_error_from_syserror();
2319 rc
= do_save_getpin(pwm
, &password
);
2326 pwm
->state
= ASYNC_DONE
;
2329 if (!local_pinentry
) {
2330 rc
= send_pinentry_options(pwm
);
2336 rc
= do_save_command(pwm
, password
);
2338 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
2339 pwmd_free(password
);
2344 gpg_error_t
pwmd_save_async2(pwm_t
*pwm
)
2346 #ifndef WITH_PINENTRY
2347 return GPG_ERR_NOT_IMPLEMENTED
;
2352 return GPG_ERR_INV_ARG
;
2355 return GPG_ERR_INV_STATE
;
2357 pwm
->cmd
= ASYNC_CMD_SAVE2
;
2358 pwm
->state
= ASYNC_PROCESS
;
2359 rc
= do_pwmd_save(pwm
, 1, 1);
2362 reset_async(pwm
, 0);
2368 gpg_error_t
pwmd_save2(pwm_t
*pwm
)
2370 #ifndef WITH_PINENTRY
2371 return GPG_ERR_NOT_IMPLEMENTED
;
2373 return do_pwmd_save(pwm
, 0, 1);
2377 gpg_error_t
pwmd_save(pwm_t
*pwm
)
2379 return do_pwmd_save(pwm
, 0, 0);
2382 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
2385 int n
= va_arg(ap
, int);
2387 gpg_error_t error
= 0;
2390 return GPG_ERR_INV_ARG
;
2395 case PWMD_OPTION_STATUS_CB
:
2396 pwm
->status_func
= va_arg(ap
, pwmd_status_cb_t
);
2398 case PWMD_OPTION_STATUS_DATA
:
2399 pwm
->status_data
= va_arg(ap
, void *);
2401 case PWMD_OPTION_PASSPHRASE_CB
:
2402 pwm
->passfunc
= va_arg(ap
, pwmd_passphrase_cb_t
);
2404 case PWMD_OPTION_PASSPHRASE_DATA
:
2405 pwm
->passdata
= va_arg(ap
, void *);
2407 case PWMD_OPTION_PASSPHRASE
:
2408 arg1
= va_arg(ap
, char *);
2411 pwmd_free(pwm
->password
);
2413 pwm
->password
= pwmd_strdup(arg1
);
2415 case PWMD_OPTION_PINENTRY_TRIES
:
2416 n
= va_arg(ap
, int);
2420 error
= GPG_ERR_INV_VALUE
;
2423 pwm
->pinentry_tries
= n
;
2425 case PWMD_OPTION_PINENTRY_TIMEOUT
:
2426 n
= va_arg(ap
, int);
2430 error
= GPG_ERR_INV_VALUE
;
2433 pwm
->pinentry_timeout
= n
;
2435 case PWMD_OPTION_PINENTRY_PATH
:
2436 if (pwm
->pinentry_path
)
2437 pwmd_free(pwm
->pinentry_path
);
2439 pwm
->pinentry_path
= pwmd_strdup(va_arg(ap
, char *));
2441 case PWMD_OPTION_PINENTRY_TTY
:
2442 if (pwm
->pinentry_tty
)
2443 pwmd_free(pwm
->pinentry_tty
);
2445 pwm
->pinentry_tty
= pwmd_strdup(va_arg(ap
, char *));
2447 case PWMD_OPTION_PINENTRY_DISPLAY
:
2448 if (pwm
->pinentry_display
)
2449 pwmd_free(pwm
->pinentry_display
);
2451 pwm
->pinentry_display
= pwmd_strdup(va_arg(ap
, char *));
2453 case PWMD_OPTION_PINENTRY_TERM
:
2454 if (pwm
->pinentry_term
)
2455 pwmd_free(pwm
->pinentry_term
);
2457 pwm
->pinentry_term
= pwmd_strdup(va_arg(ap
, char *));
2459 case PWMD_OPTION_PINENTRY_TITLE
:
2461 pwmd_free(pwm
->title
);
2463 pwm
->title
= percent_escape(va_arg(ap
, char *));
2465 case PWMD_OPTION_PINENTRY_PROMPT
:
2467 pwmd_free(pwm
->prompt
);
2469 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
2471 case PWMD_OPTION_PINENTRY_DESC
:
2473 pwmd_free(pwm
->desc
);
2475 pwm
->desc
= percent_escape(va_arg(ap
, char *));
2477 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
2479 pwmd_free(pwm
->lcctype
);
2481 pwm
->lcctype
= pwmd_strdup(va_arg(ap
, char *));
2483 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
2484 if (pwm
->lcmessages
)
2485 pwmd_free(pwm
->lcmessages
);
2487 pwm
->lcmessages
= pwmd_strdup(va_arg(ap
, char *));
2490 case PWMD_OPTION_IP_VERSION
:
2491 n
= va_arg(ap
, int);
2500 error
= GPG_ERR_INV_VALUE
;
2508 error
= GPG_ERR_NOT_IMPLEMENTED
;
2516 gpg_error_t
pwmd_get_fds(pwm_t
*pwm
, pwmd_fd_t
*fds
, int *n_fds
)
2521 int afds
[ARES_GETSOCK_MAXNUM
];
2526 if (!pwm
|| !fds
|| !n_fds
|| *n_fds
<= 0)
2527 return GPG_ERR_INV_ARG
;
2531 memset(afds
, 0, sizeof(int)*ARES_GETSOCK_MAXNUM
);
2533 memset(fds
, 0, sizeof(pwmd_fd_t
)*in_total
);
2538 case ASYNC_CMD_NONE
:
2539 case ASYNC_CMD_OPEN
:
2540 case ASYNC_CMD_SAVE
:
2541 #ifdef WITH_PINENTRY
2545 return GPG_ERR_INV_STATE
;
2548 fds
[fd
].fd
= pwm
->fd
;
2549 fds
[fd
++].flags
= PWMD_FD_READABLE
;
2551 #ifdef WITH_PINENTRY
2552 case ASYNC_CMD_OPEN2
:
2553 case ASYNC_CMD_SAVE2
:
2554 if (pwm
->nb_fd
== -1)
2555 return GPG_ERR_INV_STATE
;
2558 fds
[fd
].fd
= pwm
->nb_fd
;
2559 fds
[fd
++].flags
= PWMD_FD_READABLE
;
2564 if (!pwm
->tcp_conn
|| !pwm
->tcp_conn
->chan
)
2565 return GPG_ERR_INV_STATE
;
2567 n
= ares_getsock(pwm
->tcp_conn
->chan
, afds
, ARES_GETSOCK_MAXNUM
);
2569 for (i
= 0; i
< ARES_GETSOCK_MAXNUM
; i
++) {
2572 if (fd
> in_total
) {
2574 return GPG_ERR_ERANGE
;
2577 if (ARES_GETSOCK_READABLE(n
, i
)) {
2579 fds
[fd
].flags
|= PWMD_FD_READABLE
;
2582 if (ARES_GETSOCK_WRITABLE(n
, i
)) {
2584 fds
[fd
].flags
|= PWMD_FD_WRITABLE
;
2588 fds
[fd
++].fd
= afds
[i
];
2593 case ASYNC_CMD_CONNECT
:
2594 case ASYNC_CMD_HOSTKEY
:
2595 if (!pwm
->tcp_conn
|| pwm
->tcp_conn
->fd
== -1)
2596 return GPG_ERR_INV_STATE
;
2599 fds
[fd
].fd
= pwm
->tcp_conn
->fd
;
2600 fds
[fd
++].flags
= PWMD_FD_READABLE
;
2605 return GPG_ERR_INV_STATE
;
2608 pwm_t
*pwmd_new(const char *name
)
2610 pwm_t
*h
= pwmd_calloc(1, sizeof(pwm_t
));
2616 h
->name
= pwmd_strdup(name
);
2625 #ifdef WITH_PINENTRY
2628 h
->pinentry_timeout
= -30;
2629 h
->pinentry_tries
= 3;
2631 h
->prot
= PWMD_IP_ANY
;
2636 void pwmd_free(void *ptr
)
2641 void *pwmd_malloc(size_t size
)
2643 return xmalloc(size
);
2646 void *pwmd_calloc(size_t nmemb
, size_t size
)
2648 return xcalloc(nmemb
, size
);
2651 void *pwmd_realloc(void *ptr
, size_t size
)
2653 return xrealloc(ptr
, size
);
2656 char *pwmd_strdup(const char *str
)
2658 return xstrdup(str
);
2661 char *pwmd_strdup_printf(const char *fmt
, ...)
2671 len
= vsnprintf(NULL
, 0, fmt
, ap
);
2672 buf
= pwmd_malloc(++len
);
2679 vsnprintf(buf
, len
, fmt
, ap
);