1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2009 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
26 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <sys/select.h>
39 #include <netinet/in.h>
40 #include <sys/socket.h>
52 #define DNS_USE_GETTIMEOFDAY_FOR_ID 1
54 #include <arpa/nameser.h>
66 #define N_(msgid) dgettext("libpwmd", msgid)
73 static int gelapsed
, gtimeout
;
74 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
77 static char *_getpwuid(struct passwd
*pwd
)
79 size_t size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
80 struct passwd
*result
;
87 buf
= pwmd_malloc(size
);
92 n
= getpwuid_r(getuid(), pwd
, buf
, size
, &result
);
109 static const char *_pwmd_strerror(gpg_error_t e
)
111 gpg_err_code_t code
= gpg_err_code(e
);
113 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
118 return N_("Unknown error");
120 return N_("No cache slots available");
122 return N_("Recursion loop");
124 return N_("No file is open");
126 return N_("General LibXML error");
128 return N_("File modified");
130 return N_("Access denied");
137 const char *pwmd_strerror(gpg_error_t e
)
139 const char *p
= _pwmd_strerror(e
);
141 return p
? p
: gpg_strerror(e
);
144 int pwmd_strerror_r(gpg_error_t e
, char *buf
, size_t size
)
146 const char *p
= _pwmd_strerror(e
);
149 snprintf(buf
, size
, "%s", p
);
151 if (strlen(p
) > size
)
157 return gpg_strerror_r(e
, buf
, size
);
160 gpg_error_t
pwmd_init()
162 static int initialized
;
171 bindtextdomain("libpwmd", LOCALEDIR
);
174 assuan_set_malloc_hooks(pwmd_malloc
, pwmd_realloc
, pwmd_free
);
175 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
180 static gpg_error_t
_socket_connect_finalize(pwm_t
*pwm
)
183 int n
= assuan_get_active_fds(pwm
->ctx
, 0, active
, N_ARRAY(active
));
186 return GPG_ERR_EBADFD
;
191 pwm
->pinentry_tries
= 3;
193 assuan_set_pointer(pwm
->ctx
, pwm
);
196 return pwmd_command(pwm
, NULL
, "OPTION CLIENT NAME=%s", pwm
->name
);
202 static int read_hook(assuan_context_t ctx
, assuan_fd_t fd
, void *data
,
203 size_t len
, ssize_t
*ret
)
205 pwm_t
*pwm
= assuan_get_pointer(ctx
);
208 if (!pwm
|| !pwm
->tcp_conn
)
210 *ret
= pth_read((int)fd
, data
, len
);
212 *ret
= read((int)fd
, data
, len
);
216 *ret
= libssh2_channel_read(pwm
->tcp_conn
->channel
, data
, len
);
217 n
= libssh2_session_last_errno(pwm
->tcp_conn
->session
);
218 } while (n
== LIBSSH2_ERROR_EAGAIN
);
221 return !n
&& *ret
!= -1 ? 1 : 0;
224 static int write_hook(assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
225 size_t len
, ssize_t
*ret
)
227 pwm_t
*pwm
= assuan_get_pointer(ctx
);
230 if (!pwm
|| !pwm
->tcp_conn
)
232 *ret
= pth_write((int)fd
, data
, len
);
234 *ret
= write((int)fd
, data
, len
);
238 *ret
= libssh2_channel_write(pwm
->tcp_conn
->channel
, data
, len
);
239 n
= libssh2_session_last_errno(pwm
->tcp_conn
->session
);
240 } while (n
== LIBSSH2_ERROR_EAGAIN
);
243 return !n
&& *ret
!= -1 ? 1 : 0;
246 static void _ssh_deinit(pwmd_tcp_conn_t
*conn
);
247 static void free_tcp_conn(pwmd_tcp_conn_t
*conn
)
252 if (conn
->username
) {
253 pwmd_free(conn
->username
);
254 conn
->username
= NULL
;
257 if (conn
->known_hosts
) {
258 pwmd_free(conn
->known_hosts
);
259 conn
->known_hosts
= NULL
;
262 if (conn
->identity
) {
263 pwmd_free(conn
->identity
);
264 conn
->identity
= NULL
;
267 if (conn
->identity_pub
) {
268 pwmd_free(conn
->identity_pub
);
269 conn
->identity_pub
= NULL
;
273 pwmd_free(conn
->host
);
278 pwmd_free(conn
->hostkey
);
279 conn
->hostkey
= NULL
;
283 ares_destroy(conn
->chan
);
288 ares_free_hostent(conn
->he
);
303 static void _ssh_deinit(pwmd_tcp_conn_t
*conn
)
309 libssh2_channel_free(conn
->channel
);
312 libssh2_session_disconnect(conn
->session
, "Bye!");
313 libssh2_session_free(conn
->session
);
316 conn
->session
= NULL
;
317 conn
->channel
= NULL
;
321 static void _ssh_assuan_deinit(assuan_context_t ctx
)
323 pwm_t
*pwm
= assuan_get_pointer(ctx
);
325 pwm
->tcp_conn
->fd
= -1;
326 _ssh_deinit(pwm
->tcp_conn
);
327 pwm
->tcp_conn
= NULL
;
331 * Sets common options from both pwmd_ssh_connect() and
332 * pwmd_ssh_connect_async().
334 static gpg_error_t
init_tcp_conn(pwmd_tcp_conn_t
**dst
, const char *host
,
335 int port
, const char *identity
, const char *user
, const char *hosts
,
338 pwmd_tcp_conn_t
*conn
;
343 return GPG_ERR_INV_ARG
;
346 if (!host
|| !identity
|| !hosts
)
347 return GPG_ERR_INV_ARG
;
350 conn
= pwmd_calloc(1, sizeof(pwmd_tcp_conn_t
));
353 return gpg_error_from_errno(ENOMEM
);
355 conn
->port
= port
== -1 ? 22 : port
;
356 conn
->host
= pwmd_strdup(host
);
359 rc
= gpg_error_from_errno(ENOMEM
);
365 char *pwbuf
= _getpwuid(&pw
);
368 rc
= gpg_error_from_errno(errno
);
372 conn
->username
= pwmd_strdup(user
? user
: pw
.pw_name
);
375 if (!conn
->username
) {
376 rc
= gpg_error_from_errno(ENOMEM
);
380 conn
->identity
= pwmd_strdup(identity
);
382 if (!conn
->identity
) {
383 rc
= gpg_error_from_errno(ENOMEM
);
387 conn
->identity_pub
= pwmd_malloc(strlen(conn
->identity
)+5);
389 if (!conn
->identity_pub
) {
390 rc
= gpg_error_from_errno(ENOMEM
);
394 sprintf(conn
->identity_pub
, "%s.pub", conn
->identity
);
395 conn
->known_hosts
= pwmd_strdup(hosts
);
397 if (!conn
->known_hosts
) {
398 rc
= gpg_error_from_errno(ENOMEM
);
411 static gpg_error_t
do_connect(pwm_t
*pwm
, int prot
, void *addr
)
413 struct sockaddr_in their_addr
;
415 pwm
->tcp_conn
->fd
= socket(prot
, SOCK_STREAM
, 0);
417 if (pwm
->tcp_conn
->fd
== -1)
418 return gpg_error_from_syserror();
420 if (pwm
->tcp_conn
->async
)
421 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, O_NONBLOCK
);
423 pwm
->cmd
= ASYNC_CMD_CONNECT
;
424 their_addr
.sin_family
= prot
;
425 their_addr
.sin_port
= htons(pwm
->tcp_conn
->port
);
426 their_addr
.sin_addr
= *((struct in_addr
*)addr
);
427 memset(their_addr
.sin_zero
, '\0', sizeof their_addr
.sin_zero
);
430 if (pth_connect(pwm
->tcp_conn
->fd
, (struct sockaddr
*)&their_addr
,
431 sizeof(their_addr
)) == -1)
433 if (connect(pwm
->tcp_conn
->fd
, (struct sockaddr
*)&their_addr
,
434 sizeof(their_addr
)) == -1)
436 return gpg_error_from_syserror();
441 static gpg_error_t
ares_error_to_pwmd(int status
)
443 if (status
!= ARES_SUCCESS
)
444 warnx("%s", ares_strerror(status
));
450 return GPG_ERR_UNKNOWN_HOST
;
452 return GPG_ERR_EHOSTDOWN
;
454 return GPG_ERR_TIMEOUT
;
456 return gpg_error_from_errno(ENOMEM
);
457 case ARES_ECONNREFUSED
:
458 return GPG_ERR_ECONNREFUSED
;
461 return GPG_ERR_EHOSTUNREACH
;
467 static void dns_resolve_cb(void *arg
, int status
, int timeouts
,
468 unsigned char *abuf
, int alen
)
474 if (status
== ARES_EDESTRUCTION
)
477 if (status
!= ARES_SUCCESS
) {
478 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
482 //FIXME localhost. works with ipv4. maybe local system config error
483 /* Check for an IPv6 address first. */
484 rc
= ares_parse_a_reply(abuf
, alen
, &he
, NULL
, NULL
);
486 if (rc
!= ARES_SUCCESS
) {
487 if (rc
!= ARES_ENODATA
) {
488 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
492 rc
= ares_parse_aaaa_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
;
511 rc
= init_tcp_conn(&conn
, host
, port
, identity
, user
, known_hosts
,
512 which
== ASYNC_CMD_HOSTKEY
? 1 : 0);
518 pwm
->tcp_conn
= conn
;
519 pwm
->tcp_conn
->cmd
= which
;
521 if (pwm
->tcp_conn
->cmd
== ASYNC_CMD_HOSTKEY
)
522 pwm
->tcp_conn
->get_only
= 1;
524 pwm
->cmd
= ASYNC_CMD_DNS
;
525 pwm
->state
= ASYNC_PROCESS
;
526 ares_init(&pwm
->tcp_conn
->chan
);
527 ares_query(pwm
->tcp_conn
->chan
, pwm
->tcp_conn
->host
, ns_c_any
, ns_t_any
,
528 dns_resolve_cb
, pwm
);
532 gpg_error_t
pwmd_ssh_connect_async(pwm_t
*pwm
, const char *host
, int port
,
533 const char *identity
, const char *user
, const char *known_hosts
)
535 return _do_pwmd_tcp_connect_async(pwm
, host
, port
, identity
, user
,
536 known_hosts
, ASYNC_CMD_CONNECT
);
539 static void *_ssh_malloc(size_t size
, void **data
)
541 return pwmd_malloc(size
);
544 static void _ssh_free(void *ptr
, void **data
)
549 static void *_ssh_realloc(void *ptr
, size_t size
, void **data
)
551 return pwmd_realloc(ptr
, size
);
554 static char *to_hex(const char *str
, size_t slen
)
557 char *buf
= pwmd_malloc(slen
*2+1);
562 for (i
= 0, buf
[0] = 0; i
< slen
; i
++) {
565 sprintf(tmp
, "%02x", (unsigned char)str
[i
]);
572 static int verify_host_key(pwm_t
*pwm
)
574 FILE *fp
= fopen(pwm
->tcp_conn
->known_hosts
, "r");
580 buf
= pwmd_malloc(LINE_MAX
);
585 while ((p
= fgets(buf
, LINE_MAX
, fp
))) {
586 if (*p
== '#' || isspace(*p
))
589 if (p
[strlen(p
)-1] == '\n')
592 if (!strcmp(buf
, pwm
->tcp_conn
->hostkey
))
609 static gpg_error_t
authenticate_ssh(pwm_t
*pwm
)
611 const char *fp
= libssh2_hostkey_hash(pwm
->tcp_conn
->session
,
612 LIBSSH2_HOSTKEY_HASH_SHA1
);
615 pwm
->tcp_conn
->hostkey
= to_hex(fp
, 20);
617 if (!pwm
->tcp_conn
->hostkey
)
618 return gpg_error_from_errno(ENOMEM
);
620 if (pwm
->tcp_conn
->get_only
)
623 if (!fp
|| verify_host_key(pwm
))
624 return GPG_ERR_CHECKSUM
;
626 userauth
= libssh2_userauth_list(pwm
->tcp_conn
->session
,
627 pwm
->tcp_conn
->username
, strlen(pwm
->tcp_conn
->username
));
629 if (!userauth
|| !strstr(userauth
, "publickey"))
630 return GPG_ERR_BAD_PIN_METHOD
;
632 if (libssh2_userauth_publickey_fromfile(pwm
->tcp_conn
->session
,
633 pwm
->tcp_conn
->username
, pwm
->tcp_conn
->identity_pub
,
634 pwm
->tcp_conn
->identity
, NULL
))
635 return GPG_ERR_BAD_SECKEY
;
640 static gpg_error_t
setup_tcp_session(pwm_t
*pwm
)
642 assuan_context_t ctx
;
643 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
646 pwm
->tcp_conn
->session
= libssh2_session_init_ex(_ssh_malloc
, _ssh_free
,
649 if (!pwm
->tcp_conn
->session
) {
650 rc
= gpg_error_from_errno(ENOMEM
);
654 if (libssh2_session_startup(pwm
->tcp_conn
->session
, pwm
->tcp_conn
->fd
)) {
655 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
659 rc
= authenticate_ssh(pwm
);
664 /* pwmd_get_hostkey(). */
665 if (pwm
->tcp_conn
->get_only
) {
666 pwm
->result
= pwmd_strdup(pwm
->tcp_conn
->hostkey
);
669 rc
= gpg_error_from_errno(ENOMEM
);
676 pwm
->tcp_conn
->channel
= libssh2_channel_open_session(pwm
->tcp_conn
->session
);
678 if (!pwm
->tcp_conn
->channel
) {
679 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
683 if (libssh2_channel_shell(pwm
->tcp_conn
->channel
)) {
684 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
688 assuan_set_io_hooks(&io_hooks
);
689 rc
= assuan_socket_connect_fd(&ctx
, pwm
->tcp_conn
->fd
, 0, pwm
);
694 assuan_set_finish_handler(ctx
, _ssh_assuan_deinit
);
696 return _socket_connect_finalize(pwm
);
699 free_tcp_conn(pwm
->tcp_conn
);
700 pwm
->tcp_conn
= NULL
;
704 static gpg_error_t
_do_pwmd_tcp_connect(pwm_t
*pwm
, const char *host
, int port
,
705 const char *identity
, const char *user
, const char *known_hosts
, int get
)
707 pwmd_tcp_conn_t
*conn
;
710 rc
= init_tcp_conn(&conn
, host
, port
, identity
, user
, known_hosts
, get
);
715 pwm
->tcp_conn
= conn
;
716 pwm
->tcp_conn
->get_only
= get
;
717 pwm
->cmd
= ASYNC_CMD_DNS
;
718 ares_init(&pwm
->tcp_conn
->chan
);
719 ares_query(pwm
->tcp_conn
->chan
, pwm
->tcp_conn
->host
, ns_c_any
, ns_t_any
,
720 dns_resolve_cb
, pwm
);
722 /* dns_resolve_cb() may have already been called. */
723 if (pwm
->tcp_conn
->rc
) {
724 rc
= pwm
->tcp_conn
->rc
;
729 * Fake a blocking DNS lookup. libcares does a better job than
739 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
740 ares_timeout(pwm
->tcp_conn
->chan
, NULL
, &tv
);
742 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
744 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
748 rc
= gpg_error_from_syserror();
752 rc
= GPG_ERR_TIMEOUT
;
756 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
758 if (pwm
->tcp_conn
->rc
)
760 } while (pwm
->cmd
== ASYNC_CMD_DNS
);
762 if (pwm
->tcp_conn
->rc
) {
763 rc
= pwm
->tcp_conn
->rc
;
767 return setup_tcp_session(pwm
);
773 gpg_error_t
pwmd_ssh_connect(pwm_t
*pwm
, const char *host
, int port
,
774 const char *identity
, const char *user
, const char *known_hosts
)
776 return _do_pwmd_tcp_connect(pwm
, host
, port
, identity
, user
, known_hosts
, 0);
779 /* Must free the result with pwmd_free(). */
780 gpg_error_t
pwmd_get_hostkey(const char *host
, int port
, char **result
)
783 pwm_t
*pwm
= pwmd_new(NULL
);
787 return gpg_error_from_errno(ENOMEM
);
789 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
);
806 gpg_error_t
pwmd_get_hostkey_async(pwm_t
*pwm
, const char *host
, int port
)
808 return _do_pwmd_tcp_connect_async(pwm
, host
, port
, NULL
, NULL
, NULL
,
813 gpg_error_t
pwmd_connect(pwm_t
*pwm
, const char *path
)
815 char *socketpath
= NULL
;
816 assuan_context_t ctx
;
818 char *pwbuf
= _getpwuid(&pw
);
822 return gpg_error_from_errno(errno
);
825 socketpath
= (char *)pwmd_malloc(strlen(pw
.pw_dir
) + strlen("/.pwmd/socket") + 1);
826 sprintf(socketpath
, "%s/.pwmd/socket", pw
.pw_dir
);
829 socketpath
= pwmd_strdup(path
);
832 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
833 pwmd_free(socketpath
);
839 return _socket_connect_finalize(pwm
);
842 void pwmd_close(pwm_t
*pwm
)
848 assuan_disconnect(pwm
->ctx
);
851 pwmd_free(pwm
->password
);
854 pwmd_free(pwm
->title
);
857 pwmd_free(pwm
->desc
);
860 pwmd_free(pwm
->prompt
);
862 if (pwm
->pinentry_tty
)
863 pwmd_free(pwm
->pinentry_tty
);
865 if (pwm
->pinentry_display
)
866 pwmd_free(pwm
->pinentry_display
);
868 if (pwm
->pinentry_term
)
869 pwmd_free(pwm
->pinentry_term
);
872 pwmd_free(pwm
->lcctype
);
875 pwmd_free(pwm
->lcmessages
);
878 pwmd_free(pwm
->filename
);
881 pwmd_free(pwm
->name
);
885 free_tcp_conn(pwm
->tcp_conn
);
891 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
893 membuf_t
*mem
= (membuf_t
*)data
;
899 if ((p
= pwmd_realloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
903 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
908 static int _inquire_cb(void *data
, const char *keyword
)
910 pwm_t
*pwm
= (pwm_t
*)data
;
912 int flags
= fcntl(pwm
->fd
, F_GETFL
);
914 /* Shouldn't get this far without a callback. */
915 if (!pwm
->inquire_func
)
916 return GPG_ERR_INV_ARG
;
919 * Since the socket file descriptor is probably set to non-blocking, set to
920 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
921 * asynchronous INQUIRE is supported by either libassuan or a later
924 fcntl(pwm
->fd
, F_SETFL
, 0);
931 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
, &len
);
932 rc
= gpg_err_code(rc
);
934 if (rc
== GPG_ERR_EOF
|| !rc
) {
935 if (len
<= 0 || !result
|| !*result
) {
940 arc
= assuan_send_data(pwm
->ctx
, result
, len
);
942 if (rc
== GPG_ERR_EOF
) {
953 fcntl(pwm
->fd
, F_SETFL
, flags
);
957 static gpg_error_t
do_nb_command(pwm_t
*pwm
, const char *cmd
, ...)
963 if (pwm
->state
== ASYNC_DONE
)
964 pwm
->state
= ASYNC_INIT
;
966 if (pwm
->state
!= ASYNC_INIT
)
967 return GPG_ERR_INV_STATE
;
969 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
972 return gpg_error_from_errno(ENOMEM
);
975 vsnprintf(buf
, ASSUAN_LINELENGTH
, cmd
, ap
);
977 rc
= assuan_write_line(pwm
->ctx
, buf
);
981 pwm
->state
= ASYNC_PROCESS
;
986 gpg_error_t
pwmd_open_async(pwm_t
*pwm
, const char *filename
)
988 if (!pwm
|| !filename
)
989 return GPG_ERR_INV_ARG
;
991 if (pwm
->cmd
!= ASYNC_CMD_OPEN
) {
995 pwmd_free(pwm
->filename
);
997 pwm
->filename
= pwmd_strdup(filename
);
1000 pwm
->cmd
= ASYNC_CMD_OPEN
;
1001 return do_nb_command(pwm
, "OPEN %s %s", filename
,
1002 pwm
->password
? pwm
->password
: "");
1005 gpg_error_t
pwmd_save_async(pwm_t
*pwm
)
1008 return GPG_ERR_INV_ARG
;
1010 pwm
->cmd
= ASYNC_CMD_SAVE
;
1011 return do_nb_command(pwm
, "SAVE %s", pwm
->password
? pwm
->password
: "");
1014 static gpg_error_t
parse_assuan_line(pwm_t
*pwm
)
1020 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
1023 if (line
[0] == 'O' && line
[1] == 'K' &&
1024 (line
[2] == 0 || line
[2] == ' ')) {
1025 pwm
->state
= ASYNC_DONE
;
1027 else if (line
[0] == '#') {
1029 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' ')) {
1030 if (pwm
->status_func
) {
1031 pwm
->status_func(pwm
->status_data
,
1032 line
[1] == 0 ? line
+1 : line
+2);
1035 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
1036 (line
[3] == 0 || line
[3] == ' ')) {
1039 pwm
->state
= ASYNC_DONE
;
1046 gpg_error_t
pwmd_pending_line(pwm_t
*pwm
)
1049 return GPG_ERR_INV_ARG
;
1052 return GPG_ERR_INV_STATE
;
1054 return assuan_pending_line(pwm
->ctx
) ? 0 : GPG_ERR_NO_DATA
;
1057 static pwmd_async_t
reset_async(pwm_t
*pwm
, int done
)
1059 pwm
->state
= ASYNC_INIT
;
1060 pwm
->cmd
= ASYNC_CMD_NONE
;
1062 #ifdef WITH_PINENTRY
1063 if (pwm
->nb_fd
!= -1) {
1069 if (done
&& pwm
->tcp_conn
&& pwm
->tcp_conn
->fd
!= -1) {
1070 close(pwm
->tcp_conn
->fd
);
1071 pwm
->tcp_conn
->fd
= -1;
1078 pwmd_async_t
pwmd_process(pwm_t
*pwm
, gpg_error_t
*rc
, char **result
)
1082 struct timeval tv
= {0, 0};
1087 *rc
= GPG_ERR_INV_ARG
;
1091 /* When not in a command, this will let libassuan process status messages
1092 * by calling PWMD_OPTION_STATUS_FUNC. The client can poll the file
1093 * descriptor returned by pwmd_get_fd() to determine when this should be
1094 * called or call pwmd_pending_line() to determine whether a buffered line
1095 * needs to be processed. */
1096 if (pwm
->cmd
== ASYNC_CMD_NONE
) {
1097 *rc
= assuan_command(pwm
, pwm
->ctx
, NULL
, "NOP");
1101 /* Fixes pwmd_open/save_async2() when there is a cached or new file. */
1102 if (pwm
->state
== ASYNC_DONE
) {
1103 reset_async(pwm
, 0);
1107 if (pwm
->state
!= ASYNC_PROCESS
) {
1108 *rc
= GPG_ERR_INV_STATE
;
1113 if (pwm
->cmd
== ASYNC_CMD_DNS
) {
1116 if (pwm
->tcp_conn
->rc
) {
1117 *rc
= pwm
->tcp_conn
->rc
;
1118 reset_async(pwm
, 1);
1124 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1126 /* Shouldn't happen. */
1131 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
1133 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
1137 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1141 else if (pwm
->cmd
== ASYNC_CMD_CONNECT
) {
1142 if (pwm
->tcp_conn
->rc
== GPG_ERR_EINPROGRESS
) {
1144 socklen_t len
= sizeof(int);
1147 FD_SET(pwm
->tcp_conn
->fd
, &fds
);
1149 n
= pth_select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1151 n
= select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1154 if (!n
|| !FD_ISSET(pwm
->tcp_conn
->fd
, &fds
))
1157 *rc
= gpg_error_from_syserror();
1158 reset_async(pwm
, 1);
1162 ret
= getsockopt(pwm
->tcp_conn
->fd
, SOL_SOCKET
, SO_ERROR
, &n
, &len
);
1165 *rc
= ret
? gpg_error_from_syserror() : gpg_error_from_errno(n
);
1166 reset_async(pwm
, 1);
1170 else if (pwm
->tcp_conn
->rc
) {
1171 *rc
= pwm
->tcp_conn
->rc
;
1172 reset_async(pwm
, 1);
1176 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, 0);
1177 *rc
= setup_tcp_session(pwm
);
1180 switch (pwm
->tcp_conn
->cmd
) {
1181 case ASYNC_CMD_HOSTKEY
:
1183 *result
= pwm
->result
;
1190 return reset_async(pwm
, *rc
? 1 : 0);
1194 #ifdef WITH_PINENTRY
1195 if (pwm
->cmd
== ASYNC_CMD_OPEN2
|| pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1198 if (pwm
->nb_fd
== -1) {
1199 *rc
= GPG_ERR_INV_STATE
;
1200 return reset_async(pwm
, 0);
1204 FD_SET(pwm
->nb_fd
, &fds
);
1206 n
= pth_select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1208 n
= select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1211 *rc
= gpg_error_from_syserror();
1212 return reset_async(pwm
, 0);
1216 pwmd_nb_status_t nb
;
1218 size_t len
= pth_read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1220 size_t len
= read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1222 waitpid(pwm
->nb_pid
, &status
, WNOHANG
);
1224 if (len
!= sizeof(nb
)) {
1225 *rc
= gpg_error_from_syserror();
1226 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1231 if (*rc
== EPWMD_BADKEY
&& pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1232 reset_async(pwm
, 0);
1233 *rc
= pwmd_save_async2(pwm
);
1234 return ASYNC_PROCESS
;
1237 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1239 if (pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1240 *rc
= do_save_command(pwm
, nb
.password
);
1241 memset(&nb
, 0, sizeof(pwmd_nb_status_t
));
1242 return reset_async(pwm
, 0);
1245 if (pwm
->cmd
== ASYNC_CMD_OPEN2
) {
1246 *rc
= do_open_command(pwm
, pwm
->filename
, nb
.password
);
1247 memset(&nb
, 0, sizeof(pwmd_nb_status_t
));
1249 if (*rc
== EPWMD_BADKEY
) {
1250 if (++pwm
->pin_try
< pwm
->pinentry_tries
) {
1251 int n
= pwm
->pin_try
;
1253 reset_async(pwm
, 0);
1255 pwm
->cmd
= ASYNC_CMD_OPEN2
;
1256 *rc
= pwmd_open_async2(pwm
, pwm
->filename
);
1259 return reset_async(pwm
, 1);
1265 return reset_async(pwm
, *rc
? 1 : 0);
1274 *rc
= GPG_ERR_INV_STATE
;
1275 return reset_async(pwm
, 0);
1278 /* This is for the non-blocking OPEN and SAVE commands. */
1280 FD_SET(pwm
->fd
, &fds
);
1282 n
= pth_select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1284 n
= select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1288 *rc
= gpg_error_from_syserror();
1289 return reset_async(pwm
, 0);
1293 if (FD_ISSET(pwm
->fd
, &fds
))
1294 *rc
= parse_assuan_line(pwm
);
1297 while (!*rc
&& assuan_pending_line(pwm
->ctx
))
1298 *rc
= parse_assuan_line(pwm
);
1300 /* For pinentry retries. */
1301 if (pwm
->cmd
== ASYNC_CMD_OPEN
&& gpg_err_code(*rc
) == EPWMD_BADKEY
&&
1302 ++pwm
->pin_try
< pwm
->pinentry_tries
) {
1303 pwm
->state
= ASYNC_INIT
;
1304 *rc
= pwmd_open_async(pwm
, pwm
->filename
);
1308 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN
? 1 : 0);
1310 if (pwm
->state
== ASYNC_DONE
) {
1311 reset_async(pwm
, 0);
1318 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
1319 char **result
, const char *cmd
)
1327 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
1328 pwm
->status_func
, pwm
->status_data
);
1332 pwmd_free(data
.buf
);
1338 mem_realloc_cb(&data
, "", 1);
1341 pwmd_free(data
.buf
);
1342 rc
= GPG_ERR_INV_ARG
;
1345 *result
= (char *)data
.buf
;
1349 return gpg_err_code(rc
);
1352 gpg_error_t
pwmd_inquire(pwm_t
*pwm
, const char *cmd
, pwmd_inquire_fn fn
,
1355 if (!pwm
|| !cmd
|| !fn
)
1356 return GPG_ERR_INV_ARG
;
1358 pwm
->inquire_func
= fn
;
1359 pwm
->inquire_data
= data
;
1360 return assuan_command(pwm
, pwm
->ctx
, NULL
, cmd
);
1363 static gpg_error_t
terminate_pinentry(pwm_t
*pwm
)
1365 #ifndef WITH_PINENTRY
1366 return GPG_ERR_NOT_IMPLEMENTED
;
1368 pid_t pid
= pwm
->pid
;
1372 if (!pwm
|| pid
== -1)
1373 return GPG_ERR_INV_ARG
;
1375 if (kill(pid
, 0) == 0) {
1376 if (kill(pid
, SIGTERM
) == -1) {
1377 if (kill(pid
, SIGKILL
) == -1)
1378 return gpg_error_from_errno(errno
);
1382 return gpg_error_from_errno(errno
);
1388 #ifdef WITH_PINENTRY
1389 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
1394 tmp
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1397 return gpg_error_from_errno(ENOMEM
);
1400 pwm
->title
= pwmd_strdup(N_("LibPWMD"));
1406 pwm
->prompt
= pwmd_strdup(N_("Passphrase:"));
1411 if (!pwm
->desc
&& !which
) {
1412 pwm
->desc
= pwmd_strdup(N_("Enter a passphrase."));
1419 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1420 N_("Invalid passphrase, please try again."));
1421 else if (which
== 2)
1422 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1423 N_("Please type the passphrase again for confirmation."));
1425 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s", pwm
->desc
);
1427 error
= pinentry_command(pwm
, NULL
, tmp
);
1434 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETPROMPT %s", pwm
->prompt
);
1435 error
= pinentry_command(pwm
, NULL
, tmp
);
1442 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETDESC %s", pwm
->title
);
1443 error
= pinentry_command(pwm
, NULL
, tmp
);
1449 return gpg_error_from_errno(ENOMEM
);
1452 static void update_pinentry_settings(pwm_t
*pwm
)
1458 char *pwbuf
= _getpwuid(&pw
);
1463 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
.pw_dir
);
1466 if ((fp
= fopen(buf
, "r")) == NULL
)
1469 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
1470 char name
[32], val
[256];
1472 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
1475 if (strcasecmp(name
, "TTYNAME") == 0) {
1476 pwmd_free(pwm
->pinentry_tty
);
1477 pwm
->pinentry_tty
= pwmd_strdup(val
);
1479 else if (strcasecmp(name
, "TTYTYPE") == 0) {
1480 pwmd_free(pwm
->pinentry_term
);
1481 pwm
->pinentry_term
= pwmd_strdup(val
);
1483 else if (strcasecmp(name
, "DISPLAY") == 0) {
1484 pwmd_free(pwm
->pinentry_display
);
1485 pwm
->pinentry_display
= pwmd_strdup(val
);
1487 else if (strcasecmp(name
, "PATH") == 0) {
1488 pwmd_free(pwm
->pinentry_path
);
1489 pwm
->pinentry_path
= pwmd_strdup(val
);
1496 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
1499 assuan_context_t ctx
;
1500 int child_list
[] = {-1};
1501 char *display
= getenv("DISPLAY");
1502 const char *argv
[10];
1503 const char **p
= argv
;
1504 int have_display
= 0;
1506 char *ttybuf
= NULL
;
1508 update_pinentry_settings(pwm
);
1510 if (pwm
->pinentry_display
|| display
)
1513 if (!pwm
->pinentry_tty
) {
1514 ttybuf
= pwmd_malloc(255);
1517 return gpg_error_from_errno(ENOMEM
);
1519 rc
= ttyname_r(STDOUT_FILENO
, ttybuf
, 255);
1523 return gpg_error_from_errno(rc
);
1529 tty
= pwm
->pinentry_tty
;
1532 if (!have_display
&& !tty
)
1533 return GPG_ERR_ENOTTY
;
1536 *p
++ = have_display
? "--display" : "--ttyname";
1537 *p
++ = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
1540 *p
++ = "--lc-ctype";
1541 *p
++ = pwm
->lcctype
;
1544 if (pwm
->lcmessages
) {
1545 *p
++ = "--lc-messages";
1546 *p
++ = pwm
->lcmessages
;
1551 if (!have_display
) {
1553 *p
++ = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
1557 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
1565 pwm
->pid
= assuan_get_pid(ctx
);
1567 return set_pinentry_strings(pwm
, 0);
1570 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1575 n
= launch_pinentry(pwm
);
1581 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
1584 static void pinentry_disconnect(pwm_t
*pwm
)
1587 assuan_disconnect(pwm
->pctx
);
1594 * Only called from a child process.
1596 static void catchsig(int sig
)
1600 if (gelapsed
++ >= gtimeout
)
1601 terminate_pinentry(gpwm
);
1612 * Borrowed from libassuan.
1614 static char *percent_escape(const char *atext
)
1616 const unsigned char *s
;
1617 int len
= strlen(atext
) * 3 + 1;
1618 char *buf
= (char *)pwmd_malloc(len
), *p
= buf
;
1623 for (s
=(const unsigned char *)atext
; *s
; s
++) {
1625 sprintf (p
, "%%%02X", *s
);
1636 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1639 return GPG_ERR_INV_ARG
;
1641 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
1644 gpg_error_t
pwmd_command_ap(pwm_t
*pwm
, char **result
, const char *cmd
,
1652 return GPG_ERR_INV_ARG
;
1655 * C99 allows the dst pointer to be null which will calculate the length
1656 * of the would-be result and return it.
1658 len
= vsnprintf(NULL
, 0, cmd
, ap
)+1;
1659 buf
= (char *)pwmd_malloc(len
);
1662 return gpg_error_from_errno(ENOMEM
);
1664 len
= vsnprintf(buf
, len
, cmd
, ap
);
1666 if (buf
[strlen(buf
)-1] == '\n')
1667 buf
[strlen(buf
)-1] = 0;
1669 if (buf
[strlen(buf
)-1] == '\r')
1670 buf
[strlen(buf
)-1] = 0;
1672 error
= send_command(pwm
, result
, buf
);
1678 * Avoid sending the BYE command here. libassuan will close the file
1679 * descriptor and release the assuan context. Use pwmd_close() instead.
1681 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
1687 return GPG_ERR_INV_ARG
;
1693 error
= pwmd_command_ap(pwm
, result
, cmd
, ap
);
1698 #ifdef WITH_PINENTRY
1699 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
1702 signal(SIGALRM
, catchsig
);
1707 return pinentry_command(pwm
, result
, "GETPIN");
1710 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int which
)
1714 error
= set_pinentry_strings(pwm
, which
);
1717 pinentry_disconnect(pwm
);
1721 error
= do_getpin(pwm
, result
);
1724 * Since there was input cancel any timeout setting.
1729 if (error
== GPG_ERR_CANCELED
)
1730 return GPG_ERR_CANCELED
;
1733 pinentry_disconnect(pwm
);
1742 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
1746 char *result
= NULL
;
1748 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1751 return gpg_error_from_errno(ENOMEM
);
1753 snprintf(buf
, ASSUAN_LINELENGTH
, "OPEN %s %s", filename
,
1754 password
? password
: "");
1755 error
= send_command(pwm
, &result
, buf
);
1758 if (error
&& result
)
1764 static gpg_error_t
do_pwmd_open(pwm_t
*pwm
, const char *filename
, int nb
)
1766 char *result
= NULL
;
1767 char *password
= NULL
;
1768 char path
[PATH_MAX
];
1769 #ifdef WITH_PINENTRY
1774 if (!pwm
|| !filename
|| !*filename
)
1775 return GPG_ERR_INV_ARG
;
1777 #ifdef WITH_PINENTRY
1778 pin_try
= pwm
->pinentry_tries
- 1;
1782 * Avoid calling pinentry if the password is cached on the server or if
1783 * this is a new file.
1785 rc
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
1790 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
1793 if (access(path
, R_OK
) == -1) {
1794 if (errno
== ENOENT
)
1798 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
1800 if (rc
== EPWMD_CACHE_NOT_FOUND
) {
1801 if (pwm
->passfunc
) {
1802 password
= (char *)pwm
->passfunc(pwm
->passdata
);
1806 #ifdef WITH_PINENTRY
1808 * Get the password from pinentry.
1810 if (pwm
->use_pinentry
) {
1812 * Nonblocking is wanted. fork() then return a file descriptor
1813 * that the client can use to read() from.
1818 pwmd_nb_status_t pw
;
1821 return gpg_error_from_syserror();
1824 pwm
->filename
= pwmd_strdup(filename
);
1827 return gpg_error_from_errno(ENOMEM
);
1840 if (pwm
->pinentry_timeout
> 0) {
1842 gtimeout
= pwm
->pinentry_timeout
;
1846 pw
.error
= getpin(pwm
, &password
, 0);
1848 if (gtimeout
&& gelapsed
>= gtimeout
)
1849 pw
.error
= GPG_ERR_TIMEOUT
;
1851 signal(SIGALRM
, SIG_DFL
);
1854 snprintf(pw
.password
, sizeof(pw
.password
), "%s",
1857 pinentry_disconnect(pwm
);
1859 pth_write(p
[1], &pw
, sizeof(pw
));
1861 write(p
[1], &pw
, sizeof(pw
));
1863 memset(&pw
, 0, sizeof(pw
));
1868 rc
= gpg_error_from_syserror();
1885 * Not using pinentry and the file was not found
1888 password
= pwm
->password
;
1889 #ifdef WITH_PINENTRY
1897 pwm
->state
= ASYNC_DONE
;
1898 rc
= do_open_command(pwm
, filename
, password
);
1901 * Keep the user defined password set with pwmd_setopt(). The password may
1902 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1904 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
1905 pwmd_free(password
);
1907 #ifdef WITH_PINENTRY
1908 if (rc
== EPWMD_BADKEY
) {
1909 if (pin_try
-- > 0 && !nb
) {
1910 rc
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
1911 N_("Invalid passphrase, please try again."));
1920 pinentry_disconnect(pwm
);
1928 pwmd_free(pwm
->filename
);
1930 pwm
->filename
= pwmd_strdup(filename
);
1936 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
1938 return do_pwmd_open(pwm
, filename
, 0);
1941 gpg_error_t
pwmd_open_async2(pwm_t
*pwm
, const char *filename
)
1943 #ifndef WITH_PINENTRY
1944 return GPG_ERR_NOT_IMPLEMENTED
;
1948 if (pwm
->cmd
!= ASYNC_CMD_OPEN2
)
1951 pwm
->cmd
= ASYNC_CMD_OPEN2
;
1952 pwm
->state
= ASYNC_PROCESS
;
1953 rc
= do_pwmd_open(pwm
, filename
, 1);
1956 reset_async(pwm
, 1);
1962 #ifdef WITH_PINENTRY
1963 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
1967 char *result
= NULL
;
1970 error
= getpin(pwm
, &result
, confirm
? 2 : 0);
1974 pinentry_disconnect(pwm
);
1977 pwmd_free(*password
);
1987 if (strcmp(*password
, result
)) {
1988 pwmd_free(*password
);
1990 pinentry_disconnect(pwm
);
1991 error
= EPWMD_BADKEY
;
1996 pinentry_disconnect(pwm
);
2001 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
2005 char *result
= NULL
;
2007 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
2010 return gpg_error_from_errno(ENOMEM
);
2012 snprintf(buf
, ASSUAN_LINELENGTH
, "SAVE %s", password
? password
: "");
2013 error
= send_command(pwm
, &result
, buf
);
2016 if (error
&& result
)
2022 static gpg_error_t
do_pwmd_save(pwm_t
*pwm
, int nb
)
2024 char *result
= NULL
;
2025 char *password
= NULL
;
2029 return GPG_ERR_INV_ARG
;
2031 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
2032 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
2034 if (rc
== EPWMD_CACHE_NOT_FOUND
) {
2036 password
= (char *)(*pwm
->passfunc
)(pwm
->passdata
);
2037 #ifdef WITH_PINENTRY
2038 else if (pwm
->use_pinentry
) {
2042 pwmd_nb_status_t pw
;
2045 return gpg_error_from_syserror();
2058 pw
.error
= do_save_getpin(pwm
, &password
);
2059 pinentry_disconnect(pwm
);
2060 snprintf(pw
.password
, sizeof(pw
.password
), "%s",
2063 pth_write(p
[1], &pw
, sizeof(pw
));
2065 write(p
[1], &pw
, sizeof(pw
));
2067 memset(&pw
, 0, sizeof(pw
));
2072 rc
= gpg_error_from_syserror();
2086 rc
= do_save_getpin(pwm
, &password
);
2097 pwm
->state
= ASYNC_DONE
;
2101 password
= pwm
->password
;
2103 rc
= do_save_command(pwm
, password
);
2105 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
2106 pwmd_free(password
);
2111 gpg_error_t
pwmd_save_async2(pwm_t
*pwm
)
2113 #ifndef WITH_PINENTRY
2114 return GPG_ERR_NOT_IMPLEMENTED
;
2118 pwm
->cmd
= ASYNC_CMD_SAVE2
;
2119 pwm
->state
= ASYNC_PROCESS
;
2120 rc
= do_pwmd_save(pwm
, 1);
2123 reset_async(pwm
, 0);
2129 gpg_error_t
pwmd_save(pwm_t
*pwm
)
2131 return do_pwmd_save(pwm
, 0);
2134 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
2137 int n
= va_arg(ap
, int);
2140 gpg_error_t error
= 0;
2143 return GPG_ERR_INV_ARG
;
2148 case PWMD_OPTION_STATUS_FUNC
:
2149 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
2151 case PWMD_OPTION_STATUS_DATA
:
2152 pwm
->status_data
= va_arg(ap
, void *);
2154 case PWMD_OPTION_PASSWORD_FUNC
:
2155 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
2157 case PWMD_OPTION_PASSWORD_DATA
:
2158 pwm
->passdata
= va_arg(ap
, void *);
2160 case PWMD_OPTION_PASSWORD
:
2161 arg1
= va_arg(ap
, char *);
2164 pwmd_free(pwm
->password
);
2166 pwm
->password
= pwmd_strdup(arg1
);
2168 case PWMD_OPTION_PINENTRY
:
2169 n
= va_arg(ap
, int);
2171 if (n
!= 0 && n
!= 1) {
2173 error
= GPG_ERR_INV_VALUE
;
2176 pwm
->use_pinentry
= n
;
2177 error
= pwmd_command(pwm
, &result
, "OPTION PINENTRY=%i",
2178 !pwm
->use_pinentry
);
2181 #ifdef WITH_PINENTRY
2182 case PWMD_OPTION_PINENTRY_TRIES
:
2183 n
= va_arg(ap
, int);
2187 error
= GPG_ERR_INV_VALUE
;
2190 pwm
->pinentry_tries
= n
;
2193 case PWMD_OPTION_PINENTRY_TIMEOUT
:
2194 n
= va_arg(ap
, int);
2198 error
= GPG_ERR_INV_VALUE
;
2201 pwm
->pinentry_timeout
= n
;
2203 if (!pwm
->use_pinentry
)
2204 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i",
2205 pwm
->pinentry_timeout
);
2207 case PWMD_OPTION_PINENTRY_PATH
:
2208 if (pwm
->pinentry_path
)
2209 pwmd_free(pwm
->pinentry_path
);
2211 pwm
->pinentry_path
= pwmd_strdup(va_arg(ap
, char *));
2213 if (!pwm
->use_pinentry
)
2214 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s",
2215 pwm
->pinentry_path
);
2217 case PWMD_OPTION_PINENTRY_TTY
:
2218 if (pwm
->pinentry_tty
)
2219 pwmd_free(pwm
->pinentry_tty
);
2221 pwm
->pinentry_tty
= pwmd_strdup(va_arg(ap
, char *));
2223 if (!pwm
->use_pinentry
)
2224 error
= pwmd_command(pwm
, &result
, "OPTION TTY=%s",
2227 case PWMD_OPTION_PINENTRY_DISPLAY
:
2228 if (pwm
->pinentry_display
)
2229 pwmd_free(pwm
->pinentry_display
);
2231 pwm
->pinentry_display
= pwmd_strdup(va_arg(ap
, char *));
2233 if (!pwm
->use_pinentry
)
2234 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s",
2235 pwm
->pinentry_display
);
2237 case PWMD_OPTION_PINENTRY_TERM
:
2238 if (pwm
->pinentry_term
)
2239 pwmd_free(pwm
->pinentry_term
);
2241 pwm
->pinentry_term
= pwmd_strdup(va_arg(ap
, char *));
2243 if (!pwm
->use_pinentry
)
2244 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s",
2245 pwm
->pinentry_term
);
2247 case PWMD_OPTION_PINENTRY_TITLE
:
2249 pwmd_free(pwm
->title
);
2251 pwm
->title
= percent_escape(va_arg(ap
, char *));
2253 if (!pwm
->use_pinentry
)
2254 error
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
2257 case PWMD_OPTION_PINENTRY_PROMPT
:
2259 pwmd_free(pwm
->prompt
);
2261 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
2263 if (!pwm
->use_pinentry
)
2264 error
= pwmd_command(pwm
, &result
, "OPTION PROMPT=%s",
2267 case PWMD_OPTION_PINENTRY_DESC
:
2269 pwmd_free(pwm
->desc
);
2271 pwm
->desc
= percent_escape(va_arg(ap
, char *));
2273 if (!pwm
->use_pinentry
)
2274 error
= pwmd_command(pwm
, &result
, "OPTION DESC=%s",
2277 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
2279 pwmd_free(pwm
->lcctype
);
2281 pwm
->lcctype
= pwmd_strdup(va_arg(ap
, char *));
2283 if (!pwm
->use_pinentry
)
2284 error
= pwmd_command(pwm
, &result
, "OPTION LC_CTYPE=%s",
2287 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
2288 if (pwm
->lcmessages
)
2289 pwmd_free(pwm
->lcmessages
);
2291 pwm
->lcmessages
= pwmd_strdup(va_arg(ap
, char *));
2293 if (!pwm
->use_pinentry
)
2294 error
= pwmd_command(pwm
, &result
, "OPTION LC_MESSAGES=%s",
2298 error
= GPG_ERR_NOT_IMPLEMENTED
;
2306 gpg_error_t
pwmd_get_fd(pwm_t
*pwm
, int *fd
)
2309 return GPG_ERR_INV_ARG
;
2312 return GPG_ERR_INV_STATE
;
2318 gpg_error_t
pwmd_get_fd2(pwm_t
*pwm
, int *fd
)
2320 #ifndef WITH_PINENTRY
2321 return GPG_ERR_NOT_IMPLEMENTED
;
2324 return GPG_ERR_INV_ARG
;
2326 if (pwm
->nb_fd
== -1)
2327 return GPG_ERR_INV_STATE
;
2334 pwm_t
*pwmd_new(const char *name
)
2336 pwm_t
*h
= pwmd_calloc(1, sizeof(pwm_t
));
2342 h
->name
= pwmd_strdup(name
);
2351 #ifdef WITH_PINENTRY
2357 void pwmd_free(void *ptr
)
2362 void *pwmd_malloc(size_t size
)
2364 return xmalloc(size
);
2367 void *pwmd_calloc(size_t nmemb
, size_t size
)
2369 return xcalloc(nmemb
, size
);
2372 void *pwmd_realloc(void *ptr
, size_t size
)
2374 return xrealloc(ptr
, size
);
2377 char *pwmd_strdup(const char *str
)
2379 return xstrdup(str
);