1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2009 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
26 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <sys/select.h>
39 #include <netinet/in.h>
40 #include <sys/socket.h>
52 #define DNS_USE_GETTIMEOFDAY_FOR_ID 1
54 #include <arpa/nameser.h>
66 #define N_(msgid) dgettext("libpwmd", msgid)
73 static int gelapsed
, gtimeout
;
74 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
77 static char *_getpwuid(struct passwd
*pwd
)
79 size_t size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
80 struct passwd
*result
;
87 buf
= pwmd_malloc(size
);
92 n
= getpwuid_r(getuid(), pwd
, buf
, size
, &result
);
109 const char *_pwmd_strerror(gpg_error_t e
)
111 gpg_err_code_t code
= gpg_err_code(e
);
113 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
118 return N_("Unknown error");
120 return N_("No cache slots available");
122 return N_("Recursion loop");
124 return N_("No file is open");
126 return N_("General LibXML error");
128 return N_("File modified");
130 return N_("Access denied");
137 const char *pwmd_strerror(gpg_error_t e
)
139 const char *p
= _pwmd_strerror(e
);
141 return p
? p
: gpg_strerror(e
);
144 int pwmd_strerror_r(gpg_error_t e
, char *buf
, size_t size
)
146 const char *p
= _pwmd_strerror(e
);
149 snprintf(buf
, size
, "%s", p
);
151 if (strlen(p
) > size
)
157 return gpg_strerror_r(e
, buf
, size
);
160 gpg_error_t
pwmd_init()
162 static int initialized
;
171 bindtextdomain("libpwmd", LOCALEDIR
);
174 assuan_set_malloc_hooks(pwmd_malloc
, pwmd_realloc
, pwmd_free
);
175 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
180 static gpg_error_t
_socket_connect_finalize(pwm_t
*pwm
)
183 int n
= assuan_get_active_fds(pwm
->ctx
, 0, active
, N_ARRAY(active
));
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
);
207 if (!pwm
|| !pwm
->tcp_conn
)
209 *ret
= pth_read((int)fd
, data
, len
);
211 *ret
= read((int)fd
, data
, len
);
215 *ret
= libssh2_channel_read(pwm
->tcp_conn
->channel
, data
, len
);
216 } while (*ret
== LIBSSH2_ERROR_EAGAIN
);
219 return *ret
<= 0 ? 0 : 1;
222 static int write_hook(assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
223 size_t len
, ssize_t
*ret
)
225 pwm_t
*pwm
= assuan_get_pointer(ctx
);
227 if (!pwm
|| !pwm
->tcp_conn
)
229 *ret
= pth_write((int)fd
, data
, len
);
231 *ret
= write((int)fd
, data
, len
);
235 *ret
= libssh2_channel_write(pwm
->tcp_conn
->channel
, data
, len
);
236 } while (*ret
== LIBSSH2_ERROR_EAGAIN
);
239 return *ret
<= 0 ? 0 : 1;
242 static void _ssh_deinit(pwmd_tcp_conn_t
*conn
);
243 static void free_tcp_conn(pwmd_tcp_conn_t
*conn
)
248 if (conn
->username
) {
249 pwmd_free(conn
->username
);
250 conn
->username
= NULL
;
253 if (conn
->known_hosts
) {
254 pwmd_free(conn
->known_hosts
);
255 conn
->known_hosts
= NULL
;
258 if (conn
->identity
) {
259 pwmd_free(conn
->identity
);
260 conn
->identity
= NULL
;
263 if (conn
->identity_pub
) {
264 pwmd_free(conn
->identity_pub
);
265 conn
->identity_pub
= NULL
;
269 pwmd_free(conn
->host
);
274 pwmd_free(conn
->hostkey
);
275 conn
->hostkey
= NULL
;
279 ares_destroy(conn
->chan
);
284 ares_free_hostent(conn
->he
);
299 static void _ssh_deinit(pwmd_tcp_conn_t
*conn
)
305 libssh2_channel_free(conn
->channel
);
308 libssh2_session_disconnect(conn
->session
, "Bye!");
309 libssh2_session_free(conn
->session
);
312 conn
->session
= NULL
;
313 conn
->channel
= NULL
;
317 static void _ssh_assuan_deinit(assuan_context_t ctx
)
319 pwm_t
*pwm
= assuan_get_pointer(ctx
);
321 pwm
->tcp_conn
->fd
= -1;
322 _ssh_deinit(pwm
->tcp_conn
);
323 pwm
->tcp_conn
= NULL
;
327 * Sets common options from both pwmd_tcp_connect() and
328 * pwmd_tcp_connect_async().
330 static gpg_error_t
init_tcp_conn(pwmd_tcp_conn_t
**dst
, const char *host
,
331 int port
, const char *identity
, const char *user
, const char *hosts
,
334 pwmd_tcp_conn_t
*conn
;
339 return GPG_ERR_INV_ARG
;
342 if (!host
|| !identity
|| !hosts
)
343 return GPG_ERR_INV_ARG
;
346 conn
= pwmd_calloc(1, sizeof(pwmd_tcp_conn_t
));
349 return gpg_error_from_errno(ENOMEM
);
351 conn
->port
= port
== -1 ? 22 : port
;
352 conn
->host
= pwmd_strdup(host
);
355 rc
= gpg_error_from_errno(ENOMEM
);
361 char *pwbuf
= _getpwuid(&pw
);
364 rc
= gpg_error_from_errno(errno
);
368 conn
->username
= pwmd_strdup(user
? user
: pw
.pw_name
);
371 if (!conn
->username
) {
372 rc
= gpg_error_from_errno(ENOMEM
);
376 conn
->identity
= pwmd_strdup(identity
);
378 if (!conn
->identity
) {
379 rc
= gpg_error_from_errno(ENOMEM
);
383 conn
->identity_pub
= pwmd_malloc(strlen(conn
->identity
)+5);
385 if (!conn
->identity_pub
) {
386 rc
= gpg_error_from_errno(ENOMEM
);
390 sprintf(conn
->identity_pub
, "%s.pub", conn
->identity
);
391 conn
->known_hosts
= pwmd_strdup(hosts
);
393 if (!conn
->known_hosts
) {
394 rc
= gpg_error_from_errno(ENOMEM
);
407 static gpg_error_t
do_connect(pwm_t
*pwm
, int prot
, void *addr
)
409 struct sockaddr_in their_addr
;
411 pwm
->tcp_conn
->fd
= socket(prot
, SOCK_STREAM
, 0);
413 if (pwm
->tcp_conn
->fd
== -1)
414 return gpg_error_from_syserror();
416 if (pwm
->tcp_conn
->async
)
417 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, O_NONBLOCK
);
419 pwm
->cmd
= ASYNC_CMD_CONNECT
;
420 their_addr
.sin_family
= prot
;
421 their_addr
.sin_port
= htons(pwm
->tcp_conn
->port
);
422 their_addr
.sin_addr
= *((struct in_addr
*)addr
);
423 memset(their_addr
.sin_zero
, '\0', sizeof their_addr
.sin_zero
);
426 if (pth_connect(pwm
->tcp_conn
->fd
, (struct sockaddr
*)&their_addr
,
427 sizeof(their_addr
)) == -1)
429 if (connect(pwm
->tcp_conn
->fd
, (struct sockaddr
*)&their_addr
,
430 sizeof(their_addr
)) == -1)
432 return gpg_error_from_syserror();
437 static gpg_error_t
ares_error_to_pwmd(int status
)
439 if (status
!= ARES_SUCCESS
)
440 warnx("%s", ares_strerror(status
));
446 return GPG_ERR_UNKNOWN_HOST
;
448 return GPG_ERR_EHOSTDOWN
;
450 return GPG_ERR_TIMEOUT
;
452 return gpg_error_from_errno(ENOMEM
);
453 case ARES_ECONNREFUSED
:
454 return GPG_ERR_ECONNREFUSED
;
457 return GPG_ERR_EHOSTUNREACH
;
463 static void dns_resolve_cb(void *arg
, int status
, int timeouts
,
464 unsigned char *abuf
, int alen
)
470 if (status
== ARES_EDESTRUCTION
)
473 if (status
!= ARES_SUCCESS
) {
474 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
478 //FIXME localhost. works with ipv4. maybe local system config error
479 /* Check for an IPv6 address first. */
480 rc
= ares_parse_a_reply(abuf
, alen
, &he
, NULL
, NULL
);
482 if (rc
!= ARES_SUCCESS
) {
483 if (rc
!= ARES_ENODATA
) {
484 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
488 rc
= ares_parse_aaaa_reply(abuf
, alen
, &he
, NULL
, NULL
);
490 if (rc
!= ARES_SUCCESS
) {
491 pwm
->tcp_conn
->rc
= ares_error_to_pwmd(status
);
496 pwm
->tcp_conn
->he
= he
;
497 pwm
->tcp_conn
->rc
= do_connect(pwm
, he
->h_addrtype
, he
->h_addr
);
500 static gpg_error_t
_do_pwmd_tcp_connect_async(pwm_t
*pwm
, const char *host
,
501 int port
, const char *identity
, const char *user
,
502 const char *known_hosts
, pwmd_async_cmd_t which
)
504 pwmd_tcp_conn_t
*conn
;
507 rc
= init_tcp_conn(&conn
, host
, port
, identity
, user
, known_hosts
,
508 which
== ASYNC_CMD_HOSTKEY
? 1 : 0);
514 pwm
->tcp_conn
= conn
;
515 pwm
->tcp_conn
->cmd
= which
;
517 if (pwm
->tcp_conn
->cmd
== ASYNC_CMD_HOSTKEY
)
518 pwm
->tcp_conn
->get_only
= 1;
520 pwm
->cmd
= ASYNC_CMD_DNS
;
521 pwm
->state
= ASYNC_PROCESS
;
522 ares_init(&pwm
->tcp_conn
->chan
);
523 ares_query(pwm
->tcp_conn
->chan
, pwm
->tcp_conn
->host
, ns_c_any
, ns_t_any
,
524 dns_resolve_cb
, pwm
);
528 gpg_error_t
pwmd_ssh_connect_async(pwm_t
*pwm
, const char *host
, int port
,
529 const char *identity
, const char *user
, const char *known_hosts
)
531 return _do_pwmd_tcp_connect_async(pwm
, host
, port
, identity
, user
,
532 known_hosts
, ASYNC_CMD_CONNECT
);
535 static void *_ssh_malloc(size_t size
, void **data
)
537 return pwmd_malloc(size
);
540 static void _ssh_free(void *ptr
, void **data
)
545 static void *_ssh_realloc(void *ptr
, size_t size
, void **data
)
547 return pwmd_realloc(ptr
, size
);
550 static char *to_hex(const char *str
, size_t slen
)
553 char *buf
= pwmd_malloc(slen
*2+1);
558 for (i
= 0, buf
[0] = 0; i
< slen
; i
++) {
561 sprintf(tmp
, "%02x", (unsigned char)str
[i
]);
568 static int verify_host_key(pwm_t
*pwm
)
570 FILE *fp
= fopen(pwm
->tcp_conn
->known_hosts
, "r");
576 buf
= pwmd_malloc(LINE_MAX
);
581 while ((p
= fgets(buf
, LINE_MAX
, fp
))) {
582 if (*p
== '#' || isspace(*p
))
585 if (p
[strlen(p
)-1] == '\n')
588 if (!strcmp(buf
, pwm
->tcp_conn
->hostkey
))
605 static gpg_error_t
authenticate_ssh(pwm_t
*pwm
)
607 const char *fp
= libssh2_hostkey_hash(pwm
->tcp_conn
->session
,
608 LIBSSH2_HOSTKEY_HASH_SHA1
);
611 pwm
->tcp_conn
->hostkey
= to_hex(fp
, 20);
613 if (!pwm
->tcp_conn
->hostkey
)
614 return gpg_error_from_errno(ENOMEM
);
616 if (pwm
->tcp_conn
->get_only
)
619 if (!fp
|| verify_host_key(pwm
))
620 return GPG_ERR_CHECKSUM
;
622 userauth
= libssh2_userauth_list(pwm
->tcp_conn
->session
,
623 pwm
->tcp_conn
->username
, strlen(pwm
->tcp_conn
->username
));
625 if (!userauth
|| !strstr(userauth
, "publickey"))
626 return GPG_ERR_BAD_PIN_METHOD
;
628 if (libssh2_userauth_publickey_fromfile(pwm
->tcp_conn
->session
,
629 pwm
->tcp_conn
->username
, pwm
->tcp_conn
->identity_pub
,
630 pwm
->tcp_conn
->identity
, NULL
))
631 return GPG_ERR_BAD_SECKEY
;
636 static gpg_error_t
setup_tcp_session(pwm_t
*pwm
)
638 assuan_context_t ctx
;
639 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
642 pwm
->tcp_conn
->session
= libssh2_session_init_ex(_ssh_malloc
, _ssh_free
,
645 if (!pwm
->tcp_conn
->session
) {
646 rc
= gpg_error_from_errno(ENOMEM
);
650 if (libssh2_session_startup(pwm
->tcp_conn
->session
, pwm
->tcp_conn
->fd
)) {
651 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
655 rc
= authenticate_ssh(pwm
);
660 /* pwmd_get_hostkey(). */
661 if (pwm
->tcp_conn
->get_only
) {
662 pwm
->result
= pwmd_strdup(pwm
->tcp_conn
->hostkey
);
665 rc
= gpg_error_from_errno(ENOMEM
);
672 pwm
->tcp_conn
->channel
= libssh2_channel_open_session(pwm
->tcp_conn
->session
);
674 if (!pwm
->tcp_conn
->channel
) {
675 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
679 if (libssh2_channel_shell(pwm
->tcp_conn
->channel
)) {
680 rc
= GPG_ERR_ASSUAN_SERVER_FAULT
;
684 assuan_set_io_hooks(&io_hooks
);
685 rc
= assuan_socket_connect_fd(&ctx
, pwm
->tcp_conn
->fd
, 0, pwm
);
690 assuan_set_finish_handler(ctx
, _ssh_assuan_deinit
);
692 return _socket_connect_finalize(pwm
);
695 free_tcp_conn(pwm
->tcp_conn
);
696 pwm
->tcp_conn
= NULL
;
700 static gpg_error_t
_do_pwmd_tcp_connect(pwm_t
*pwm
, const char *host
, int port
,
701 const char *identity
, const char *user
, const char *known_hosts
, int get
)
703 pwmd_tcp_conn_t
*conn
;
706 rc
= init_tcp_conn(&conn
, host
, port
, identity
, user
, known_hosts
, get
);
711 pwm
->tcp_conn
= conn
;
712 pwm
->tcp_conn
->get_only
= get
;
713 pwm
->cmd
= ASYNC_CMD_DNS
;
714 ares_init(&pwm
->tcp_conn
->chan
);
715 ares_query(pwm
->tcp_conn
->chan
, pwm
->tcp_conn
->host
, ns_c_any
, ns_t_any
,
716 dns_resolve_cb
, pwm
);
718 /* dns_resolve_cb() may have already been called. */
719 if (pwm
->tcp_conn
->rc
) {
720 rc
= pwm
->tcp_conn
->rc
;
725 * Fake a blocking DNS lookup. libcares does a better job than
735 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
736 ares_timeout(pwm
->tcp_conn
->chan
, NULL
, &tv
);
738 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
740 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
744 rc
= gpg_error_from_syserror();
748 rc
= GPG_ERR_TIMEOUT
;
752 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
754 if (pwm
->tcp_conn
->rc
)
756 } while (pwm
->cmd
== ASYNC_CMD_DNS
);
758 if (pwm
->tcp_conn
->rc
) {
759 rc
= pwm
->tcp_conn
->rc
;
763 return setup_tcp_session(pwm
);
769 gpg_error_t
pwmd_ssh_connect(pwm_t
*pwm
, const char *host
, int port
,
770 const char *identity
, const char *user
, const char *known_hosts
)
772 return _do_pwmd_tcp_connect(pwm
, host
, port
, identity
, user
, known_hosts
, 0);
775 /* Must free the result with pwmd_free(). */
776 gpg_error_t
pwmd_get_hostkey(const char *host
, int port
, char **result
)
779 pwm_t
*pwm
= pwmd_new(NULL
);
783 return gpg_error_from_errno(ENOMEM
);
785 rc
= _do_pwmd_tcp_connect(pwm
, host
, port
, NULL
, NULL
, NULL
, 1);
792 hostkey
= pwmd_strdup(pwm
->tcp_conn
->hostkey
);
795 rc
= gpg_error_from_errno(ENOMEM
);
802 gpg_error_t
pwmd_get_hostkey_async(pwm_t
*pwm
, const char *host
, int port
)
804 return _do_pwmd_tcp_connect_async(pwm
, host
, port
, NULL
, NULL
, NULL
,
809 gpg_error_t
pwmd_connect(pwm_t
*pwm
, const char *path
)
811 char *socketpath
= NULL
;
812 assuan_context_t ctx
;
814 char *pwbuf
= _getpwuid(&pw
);
818 return gpg_error_from_errno(errno
);
821 socketpath
= (char *)pwmd_malloc(strlen(pw
.pw_dir
) + strlen("/.pwmd/socket") + 1);
822 sprintf(socketpath
, "%s/.pwmd/socket", pw
.pw_dir
);
825 socketpath
= pwmd_strdup(path
);
828 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
829 pwmd_free(socketpath
);
835 return _socket_connect_finalize(pwm
);
838 void pwmd_close(pwm_t
*pwm
)
844 assuan_disconnect(pwm
->ctx
);
847 pwmd_free(pwm
->password
);
850 pwmd_free(pwm
->title
);
853 pwmd_free(pwm
->desc
);
856 pwmd_free(pwm
->prompt
);
858 if (pwm
->pinentry_tty
)
859 pwmd_free(pwm
->pinentry_tty
);
861 if (pwm
->pinentry_display
)
862 pwmd_free(pwm
->pinentry_display
);
864 if (pwm
->pinentry_term
)
865 pwmd_free(pwm
->pinentry_term
);
868 pwmd_free(pwm
->lcctype
);
871 pwmd_free(pwm
->lcmessages
);
874 pwmd_free(pwm
->filename
);
877 pwmd_free(pwm
->name
);
881 free_tcp_conn(pwm
->tcp_conn
);
887 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
889 membuf_t
*mem
= (membuf_t
*)data
;
895 if ((p
= pwmd_realloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
899 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
904 static int _inquire_cb(void *data
, const char *keyword
)
906 pwm_t
*pwm
= (pwm_t
*)data
;
908 int flags
= fcntl(pwm
->fd
, F_GETFL
);
910 /* Shouldn't get this far without a callback. */
911 if (!pwm
->inquire_func
)
912 return GPG_ERR_INV_ARG
;
915 * Since the socket file descriptor is probably set to non-blocking, set to
916 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
917 * asynchronous INQUIRE is supported by either libassuan or a later
920 fcntl(pwm
->fd
, F_SETFL
, 0);
927 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
, &len
);
928 rc
= gpg_err_code(rc
);
930 if (rc
== GPG_ERR_EOF
|| !rc
) {
931 if (len
<= 0 || !result
|| !*result
) {
936 arc
= assuan_send_data(pwm
->ctx
, result
, len
);
938 if (rc
== GPG_ERR_EOF
) {
949 fcntl(pwm
->fd
, F_SETFL
, flags
);
953 static gpg_error_t
do_nb_command(pwm_t
*pwm
, const char *cmd
, ...)
959 if (pwm
->state
== ASYNC_DONE
)
960 pwm
->state
= ASYNC_INIT
;
962 if (pwm
->state
!= ASYNC_INIT
)
963 return GPG_ERR_INV_STATE
;
965 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
968 return gpg_error_from_errno(ENOMEM
);
971 vsnprintf(buf
, ASSUAN_LINELENGTH
, cmd
, ap
);
973 rc
= assuan_write_line(pwm
->ctx
, buf
);
977 pwm
->state
= ASYNC_PROCESS
;
983 gpg_error_t
pwmd_open_async(pwm_t
*pwm
, const char *filename
)
985 if (!pwm
|| !filename
)
986 return GPG_ERR_INV_ARG
;
988 /* For pinentry retries. */
989 if (!pwm
->is_open_cmd
) {
991 pwmd_free(pwm
->filename
);
993 pwm
->filename
= pwmd_strdup(filename
);
996 pwm
->is_open_cmd
= 1;
997 pwm
->cmd
= ASYNC_CMD_OPEN
;
998 return do_nb_command(pwm
, "OPEN %s %s", filename
,
999 pwm
->password
? pwm
->password
: "");
1002 gpg_error_t
pwmd_save_async(pwm_t
*pwm
)
1005 return GPG_ERR_INV_ARG
;
1007 pwm
->cmd
= ASYNC_CMD_SAVE
;
1008 return do_nb_command(pwm
, "SAVE %s", pwm
->password
? pwm
->password
: "");
1011 static gpg_error_t
parse_assuan_line(pwm_t
*pwm
)
1017 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
1020 if (line
[0] == 'O' && line
[1] == 'K' &&
1021 (line
[2] == 0 || line
[2] == ' ')) {
1022 pwm
->state
= ASYNC_DONE
;
1024 else if (line
[0] == '#') {
1026 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' ')) {
1027 if (pwm
->status_func
) {
1028 pwm
->status_func(pwm
->status_data
,
1029 line
[1] == 0 ? line
+1 : line
+2);
1032 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
1033 (line
[3] == 0 || line
[3] == ' ')) {
1036 pwm
->state
= ASYNC_DONE
;
1043 gpg_error_t
pwmd_pending_line(pwm_t
*pwm
)
1046 return GPG_ERR_INV_ARG
;
1049 return GPG_ERR_INV_STATE
;
1051 return assuan_pending_line(pwm
->ctx
) ? 0 : GPG_ERR_NO_DATA
;
1054 static pwmd_async_t
reset_async(pwm_t
*pwm
, int done
)
1056 pwm
->state
= ASYNC_INIT
;
1057 pwm
->cmd
= ASYNC_CMD_NONE
;
1059 #ifdef WITH_PINENTRY
1060 pwm
->is_open_cmd
= 0;
1062 if (pwm
->nb_fd
!= -1) {
1068 if (done
&& pwm
->tcp_conn
&& pwm
->tcp_conn
->fd
!= -1) {
1069 close(pwm
->tcp_conn
->fd
);
1070 pwm
->tcp_conn
->fd
= -1;
1077 pwmd_async_t
pwmd_process(pwm_t
*pwm
, gpg_error_t
*rc
, char **result
)
1081 struct timeval tv
= {0, 0};
1086 *rc
= GPG_ERR_INV_ARG
;
1090 /* When not in a command, this will let libassuan process status messages
1091 * by calling PWMD_OPTION_STATUS_FUNC. The client can poll the file
1092 * descriptor returned by pwmd_get_fd() to determine when this should be
1093 * called or call pwmd_pending_line() to determine whether a buffered line
1094 * needs to be processed. */
1095 if (pwm
->cmd
== ASYNC_CMD_NONE
) {
1096 *rc
= assuan_command(pwm
, pwm
->ctx
, NULL
, "NOP");
1100 /* Fixes pwmd_open/save_async2() when there is a cached or new file. */
1101 if (pwm
->state
== ASYNC_DONE
) {
1102 reset_async(pwm
, 0);
1106 if (pwm
->state
!= ASYNC_PROCESS
) {
1107 *rc
= GPG_ERR_INV_STATE
;
1112 if (pwm
->cmd
== ASYNC_CMD_DNS
) {
1115 if (pwm
->tcp_conn
->rc
) {
1116 *rc
= pwm
->tcp_conn
->rc
;
1117 reset_async(pwm
, 1);
1123 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1125 /* Shouldn't happen. */
1130 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
1132 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
1136 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1140 else if (pwm
->cmd
== ASYNC_CMD_CONNECT
) {
1141 if (pwm
->tcp_conn
->rc
== GPG_ERR_EINPROGRESS
) {
1143 socklen_t len
= sizeof(int);
1146 FD_SET(pwm
->tcp_conn
->fd
, &fds
);
1148 n
= pth_select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1150 n
= select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1153 if (!n
|| !FD_ISSET(pwm
->tcp_conn
->fd
, &fds
))
1156 *rc
= gpg_error_from_syserror();
1157 reset_async(pwm
, 1);
1161 ret
= getsockopt(pwm
->tcp_conn
->fd
, SOL_SOCKET
, SO_ERROR
, &n
, &len
);
1164 *rc
= ret
? gpg_error_from_syserror() : gpg_error_from_errno(n
);
1165 reset_async(pwm
, 1);
1169 else if (pwm
->tcp_conn
->rc
) {
1170 *rc
= pwm
->tcp_conn
->rc
;
1171 reset_async(pwm
, 1);
1175 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, 0);
1176 *rc
= setup_tcp_session(pwm
);
1179 switch (pwm
->tcp_conn
->cmd
) {
1180 case ASYNC_CMD_HOSTKEY
:
1182 *result
= pwm
->result
;
1189 return reset_async(pwm
, *rc
? 1 : 0);
1193 #ifdef WITH_PINENTRY
1194 if (pwm
->cmd
== ASYNC_CMD_OPEN2
|| pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1195 if (pwm
->nb_fd
== -1) {
1196 *rc
= GPG_ERR_INV_STATE
;
1197 return reset_async(pwm
, 0);
1201 FD_SET(pwm
->nb_fd
, &fds
);
1203 n
= pth_select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1205 n
= select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1208 *rc
= gpg_error_from_syserror();
1209 return reset_async(pwm
, 0);
1213 pwmd_nb_status_t nb
;
1214 size_t len
= read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1217 return reset_async(pwm
, 0);
1225 *rc
= GPG_ERR_INV_STATE
;
1226 return reset_async(pwm
, 0);
1229 /* This is for the non-blocking OPEN and SAVE commands. */
1231 FD_SET(pwm
->fd
, &fds
);
1233 n
= pth_select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1235 n
= select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1239 *rc
= gpg_error_from_syserror();
1240 return reset_async(pwm
, 0);
1244 if (FD_ISSET(pwm
->fd
, &fds
))
1245 *rc
= parse_assuan_line(pwm
);
1248 while (!*rc
&& assuan_pending_line(pwm
->ctx
))
1249 *rc
= parse_assuan_line(pwm
);
1251 /* For pinentry retries. */
1252 if (pwm
->is_open_cmd
&& gpg_err_code(*rc
) == EPWMD_BADKEY
&&
1253 ++pwm
->ntries
< pwm
->pinentry_tries
) {
1254 pwm
->state
= ASYNC_INIT
;
1255 *rc
= pwmd_open_async(pwm
, pwm
->filename
);
1259 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN
? 1 : 0);
1261 if (pwm
->state
== ASYNC_DONE
) {
1262 reset_async(pwm
, 0);
1269 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
1270 char **result
, const char *cmd
)
1278 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
1279 pwm
->status_func
, pwm
->status_data
);
1283 pwmd_free(data
.buf
);
1289 mem_realloc_cb(&data
, "", 1);
1292 pwmd_free(data
.buf
);
1293 rc
= GPG_ERR_INV_ARG
;
1296 *result
= (char *)data
.buf
;
1300 return gpg_err_code(rc
);
1303 gpg_error_t
pwmd_inquire(pwm_t
*pwm
, const char *cmd
, pwmd_inquire_fn fn
,
1306 if (!pwm
|| !cmd
|| !fn
)
1307 return GPG_ERR_INV_ARG
;
1309 pwm
->inquire_func
= fn
;
1310 pwm
->inquire_data
= data
;
1311 return assuan_command(pwm
, pwm
->ctx
, NULL
, cmd
);
1314 static gpg_error_t
terminate_pinentry(pwm_t
*pwm
)
1316 #ifndef WITH_PINENTRY
1317 return GPG_ERR_NOT_IMPLEMENTED
;
1319 pid_t pid
= pwm
->pid
;
1323 if (!pwm
|| pid
== -1)
1324 return GPG_ERR_INV_ARG
;
1326 if (kill(pid
, 0) == 0) {
1327 if (kill(pid
, SIGTERM
) == -1) {
1328 if (kill(pid
, SIGKILL
) == -1)
1329 return gpg_error_from_errno(errno
);
1333 return gpg_error_from_errno(errno
);
1339 #ifdef WITH_PINENTRY
1340 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
1345 tmp
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1348 return gpg_error_from_errno(ENOMEM
);
1351 pwm
->title
= pwmd_strdup(N_("LibPWMD"));
1357 pwm
->prompt
= pwmd_strdup(N_("Passphrase:"));
1362 if (!pwm
->desc
&& !which
) {
1363 pwm
->desc
= pwmd_strdup(N_("Enter a passphrase."));
1370 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1371 N_("Invalid passphrase, please try again."));
1372 else if (which
== 2)
1373 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1374 N_("Please type the passphrase again for confirmation."));
1376 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s", pwm
->desc
);
1378 error
= pinentry_command(pwm
, NULL
, tmp
);
1385 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETPROMPT %s", pwm
->prompt
);
1386 error
= pinentry_command(pwm
, NULL
, tmp
);
1393 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETDESC %s", pwm
->title
);
1394 error
= pinentry_command(pwm
, NULL
, tmp
);
1400 return gpg_error_from_errno(ENOMEM
);
1403 static void update_pinentry_settings(pwm_t
*pwm
)
1409 char *pwbuf
= _getpwuid(&pw
);
1414 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
.pw_dir
);
1417 if ((fp
= fopen(buf
, "r")) == NULL
)
1420 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
1421 char name
[32], val
[256];
1423 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
1426 if (strcasecmp(name
, "TTYNAME") == 0) {
1427 pwmd_free(pwm
->pinentry_tty
);
1428 pwm
->pinentry_tty
= pwmd_strdup(val
);
1430 else if (strcasecmp(name
, "TTYTYPE") == 0) {
1431 pwmd_free(pwm
->pinentry_term
);
1432 pwm
->pinentry_term
= pwmd_strdup(val
);
1434 else if (strcasecmp(name
, "DISPLAY") == 0) {
1435 pwmd_free(pwm
->pinentry_display
);
1436 pwm
->pinentry_display
= pwmd_strdup(val
);
1438 else if (strcasecmp(name
, "PATH") == 0) {
1439 pwmd_free(pwm
->pinentry_path
);
1440 pwm
->pinentry_path
= pwmd_strdup(val
);
1447 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
1450 assuan_context_t ctx
;
1451 int child_list
[] = {-1};
1452 char *display
= getenv("DISPLAY");
1453 const char *argv
[10];
1454 const char **p
= argv
;
1455 int have_display
= 0;
1457 char *ttybuf
= NULL
;
1459 update_pinentry_settings(pwm
);
1461 if (pwm
->pinentry_display
|| display
)
1464 if (!pwm
->pinentry_tty
) {
1465 ttybuf
= pwmd_malloc(255);
1468 return gpg_error_from_errno(ENOMEM
);
1470 rc
= ttyname_r(STDOUT_FILENO
, ttybuf
, 255);
1474 return gpg_error_from_errno(rc
);
1480 tty
= pwm
->pinentry_tty
;
1483 if (!have_display
&& !tty
)
1484 return GPG_ERR_ENOTTY
;
1487 *p
++ = have_display
? "--display" : "--ttyname";
1488 *p
++ = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
1491 *p
++ = "--lc-ctype";
1492 *p
++ = pwm
->lcctype
;
1495 if (pwm
->lcmessages
) {
1496 *p
++ = "--lc-messages";
1497 *p
++ = pwm
->lcmessages
;
1502 if (!have_display
) {
1504 *p
++ = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
1508 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
1516 pwm
->pid
= assuan_get_pid(ctx
);
1518 return set_pinentry_strings(pwm
, 0);
1521 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1526 n
= launch_pinentry(pwm
);
1532 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
1535 static void pinentry_disconnect(pwm_t
*pwm
)
1538 assuan_disconnect(pwm
->pctx
);
1545 * Only called from a child process.
1547 static void catchsig(int sig
)
1551 if (gelapsed
++ >= gtimeout
)
1552 terminate_pinentry(gpwm
);
1563 * Borrowed from libassuan.
1565 static char *percent_escape(const char *atext
)
1567 const unsigned char *s
;
1568 int len
= strlen(atext
) * 3 + 1;
1569 char *buf
= (char *)pwmd_malloc(len
), *p
= buf
;
1574 for (s
=(const unsigned char *)atext
; *s
; s
++) {
1576 sprintf (p
, "%%%02X", *s
);
1587 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1590 return GPG_ERR_INV_ARG
;
1592 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
1595 gpg_error_t
pwmd_command_ap(pwm_t
*pwm
, char **result
, const char *cmd
,
1603 return GPG_ERR_INV_ARG
;
1606 * C99 allows the dst pointer to be null which will calculate the length
1607 * of the would-be result and return it.
1609 len
= vsnprintf(NULL
, 0, cmd
, ap
)+1;
1610 buf
= (char *)pwmd_malloc(len
);
1611 len
= vsnprintf(buf
, len
, cmd
, ap
);
1612 error
= send_command(pwm
, result
, buf
);
1618 * Avoid sending the BYE command here. libassuan will close the file
1619 * descriptor and release the assuan context. Use pwmd_close() instead.
1621 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
1627 return GPG_ERR_INV_ARG
;
1633 error
= pwmd_command_ap(pwm
, result
, cmd
, ap
);
1638 #ifdef WITH_PINENTRY
1639 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
1642 signal(SIGALRM
, catchsig
);
1647 return pinentry_command(pwm
, result
, "GETPIN");
1650 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
1652 int pin_try
= *try_n
;
1658 if (pin_try
== -1) {
1659 error
= set_pinentry_strings(pwm
, which
);
1662 pinentry_disconnect(pwm
);
1667 if (pwm
->pinentry_tries
-1 != pin_try
) {
1668 error
= set_pinentry_strings(pwm
, 1);
1671 pinentry_disconnect(pwm
);
1677 error
= do_getpin(pwm
, result
);
1680 * Since there was input cancel any timeout setting.
1685 if (error
== GPG_ERR_CANCELED
)
1686 return GPG_ERR_CANCELED
;
1688 if (pin_try
!= -1 && pin_try
--)
1692 pinentry_disconnect(pwm
);
1702 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
1706 char *result
= NULL
;
1708 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1711 return gpg_error_from_errno(ENOMEM
);
1713 snprintf(buf
, ASSUAN_LINELENGTH
, "OPEN %s %s", filename
,
1714 password
? password
: "");
1715 error
= send_command(pwm
, &result
, buf
);
1718 if (error
&& result
)
1724 static gpg_error_t
do_pwmd_open(pwm_t
*pwm
, const char *filename
, int nb
)
1726 char *result
= NULL
;
1727 char *password
= NULL
;
1728 char path
[PATH_MAX
];
1729 #ifdef WITH_PINENTRY
1734 if (!pwm
|| !filename
|| !*filename
)
1735 return GPG_ERR_INV_ARG
;
1737 #ifdef WITH_PINENTRY
1738 pin_try
= pwm
->pinentry_tries
- 1;
1742 * Avoid calling pinentry if the password is cached on the server or if
1743 * this is a new file.
1745 rc
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
1750 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
1753 if (access(path
, R_OK
) == -1) {
1754 if (errno
== ENOENT
)
1758 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
1760 if (rc
== EPWMD_CACHE_NOT_FOUND
) {
1761 if (pwm
->passfunc
) {
1762 password
= (char *)pwm
->passfunc(pwm
->passdata
);
1766 #ifdef WITH_PINENTRY
1768 * Get the password from pinentry.
1770 if (pwm
->use_pinentry
) {
1772 * Nonblocking is wanted. fork() then return a file descriptor
1773 * that the client can use to read() from.
1778 pwmd_nb_status_t pw
;
1781 return gpg_error_from_syserror();
1794 if (pwm
->pinentry_timeout
> 0) {
1796 gtimeout
= pwm
->pinentry_timeout
;
1801 rc
= getpin(pwm
, &password
, &pin_try
, 0);
1806 pinentry_disconnect(pwm
);
1808 if (gtimeout
&& gelapsed
>= gtimeout
)
1809 rc
= GPG_ERR_TIMEOUT
;
1813 pth_write(p
[1], &pw
, sizeof(pw
));
1815 write(p
[1], &pw
, sizeof(pw
));
1822 * Don't count the time it takes to open the file
1823 * which may have many iterations.
1825 signal(SIGALRM
, SIG_DFL
);
1826 rc
= do_open_command(pwm
, filename
, password
);
1828 if (pwm
->pinentry_timeout
)
1829 signal(SIGALRM
, catchsig
);
1831 if (pwm
->pctx
&& rc
== EPWMD_BADKEY
) {
1833 goto getpin_nb_again
;
1835 goto getpin_nb_fail
;
1838 pinentry_disconnect(pwm
);
1841 pth_write(p
[1], &pw
, sizeof(pw
));
1843 write(p
[1], &pw
, sizeof(pw
));
1849 rc
= gpg_error_from_syserror();
1865 * Not using pinentry and the file was not found
1868 password
= pwm
->password
;
1869 #ifdef WITH_PINENTRY
1877 pwm
->state
= ASYNC_DONE
;
1878 rc
= do_open_command(pwm
, filename
, password
);
1881 * Keep the user defined password set with pwmd_setopt(). The password may
1882 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1884 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
1885 pwmd_free(password
);
1887 #ifdef WITH_PINENTRY
1888 if (rc
== EPWMD_BADKEY
) {
1889 if (pin_try
-- > 0 && !nb
) {
1890 rc
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
1891 N_("Invalid passphrase, please try again."));
1900 pinentry_disconnect(pwm
);
1908 pwmd_free(pwm
->filename
);
1910 pwm
->filename
= pwmd_strdup(filename
);
1916 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
1918 return do_pwmd_open(pwm
, filename
, 0);
1921 gpg_error_t
pwmd_open_async2(pwm_t
*pwm
, const char *filename
)
1923 #ifndef WITH_PINENTRY
1924 return GPG_ERR_NOT_IMPLEMENTED
;
1928 pwm
->cmd
= ASYNC_CMD_OPEN2
;
1929 pwm
->state
= ASYNC_PROCESS
;
1930 rc
= do_pwmd_open(pwm
, filename
, 1);
1933 reset_async(pwm
, 1);
1939 #ifdef WITH_PINENTRY
1940 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
1944 char *result
= NULL
;
1948 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
1952 pinentry_disconnect(pwm
);
1955 pwmd_free(*password
);
1965 if (strcmp(*password
, result
)) {
1966 pwmd_free(*password
);
1968 pinentry_disconnect(pwm
);
1969 error
= EPWMD_BADKEY
;
1974 pinentry_disconnect(pwm
);
1979 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
1983 char *result
= NULL
;
1985 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1988 return gpg_error_from_errno(ENOMEM
);
1990 snprintf(buf
, ASSUAN_LINELENGTH
, "SAVE %s", password
? password
: "");
1991 error
= send_command(pwm
, &result
, buf
);
1994 if (error
&& result
)
2000 static gpg_error_t
do_pwmd_save(pwm_t
*pwm
, int nb
)
2002 char *result
= NULL
;
2003 char *password
= NULL
;
2007 return GPG_ERR_INV_ARG
;
2009 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
2010 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
2012 if (rc
== EPWMD_CACHE_NOT_FOUND
) {
2014 password
= (char *)(*pwm
->passfunc
)(pwm
->passdata
);
2015 #ifdef WITH_PINENTRY
2016 else if (pwm
->use_pinentry
) {
2020 pwmd_nb_status_t pw
;
2023 return gpg_error_from_syserror();
2038 rc
= do_save_getpin(pwm
, &password
);
2039 } while (rc
== EPWMD_BADKEY
);
2043 pinentry_disconnect(pwm
);
2047 pth_write(p
[1], &pw
, sizeof(pw
));
2049 write(p
[1], &pw
, sizeof(pw
));
2055 rc
= do_save_command(pwm
, password
);
2056 pinentry_disconnect(pwm
);
2059 pth_write(p
[1], &pw
, sizeof(pw
));
2061 write(p
[1], &pw
, sizeof(pw
));
2067 rc
= gpg_error_from_syserror();
2080 rc
= do_save_getpin(pwm
, &password
);
2091 pwm
->state
= ASYNC_DONE
;
2095 password
= pwm
->password
;
2097 rc
= do_save_command(pwm
, password
);
2099 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
2100 pwmd_free(password
);
2105 gpg_error_t
pwmd_save_async2(pwm_t
*pwm
)
2107 #ifndef WITH_PINENTRY
2108 return GPG_ERR_NOT_IMPLEMENTED
;
2112 pwm
->cmd
= ASYNC_CMD_SAVE2
;
2113 pwm
->state
= ASYNC_PROCESS
;
2114 rc
= do_pwmd_save(pwm
, 1);
2117 reset_async(pwm
, 0);
2123 gpg_error_t
pwmd_save(pwm_t
*pwm
)
2125 return do_pwmd_save(pwm
, 0);
2128 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
2131 int n
= va_arg(ap
, int);
2134 gpg_error_t error
= 0;
2137 return GPG_ERR_INV_ARG
;
2142 case PWMD_OPTION_STATUS_FUNC
:
2143 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
2145 case PWMD_OPTION_STATUS_DATA
:
2146 pwm
->status_data
= va_arg(ap
, void *);
2148 case PWMD_OPTION_PASSWORD_FUNC
:
2149 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
2151 case PWMD_OPTION_PASSWORD_DATA
:
2152 pwm
->passdata
= va_arg(ap
, void *);
2154 case PWMD_OPTION_PASSWORD
:
2155 arg1
= va_arg(ap
, char *);
2158 pwmd_free(pwm
->password
);
2160 pwm
->password
= pwmd_strdup(arg1
);
2162 case PWMD_OPTION_PINENTRY
:
2163 n
= va_arg(ap
, int);
2165 if (n
!= 0 && n
!= 1) {
2167 error
= GPG_ERR_INV_VALUE
;
2170 pwm
->use_pinentry
= n
;
2171 error
= pwmd_command(pwm
, &result
, "OPTION PINENTRY=%i",
2172 !pwm
->use_pinentry
);
2175 #ifdef WITH_PINENTRY
2176 case PWMD_OPTION_PINENTRY_TRIES
:
2177 n
= va_arg(ap
, int);
2181 error
= GPG_ERR_INV_VALUE
;
2184 pwm
->pinentry_tries
= n
;
2187 case PWMD_OPTION_PINENTRY_TIMEOUT
:
2188 n
= va_arg(ap
, int);
2192 error
= GPG_ERR_INV_VALUE
;
2195 pwm
->pinentry_timeout
= n
;
2197 if (!pwm
->use_pinentry
)
2198 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i",
2199 pwm
->pinentry_timeout
);
2201 case PWMD_OPTION_PINENTRY_PATH
:
2202 if (pwm
->pinentry_path
)
2203 pwmd_free(pwm
->pinentry_path
);
2205 pwm
->pinentry_path
= pwmd_strdup(va_arg(ap
, char *));
2207 if (!pwm
->use_pinentry
)
2208 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s",
2209 pwm
->pinentry_path
);
2211 case PWMD_OPTION_PINENTRY_TTY
:
2212 if (pwm
->pinentry_tty
)
2213 pwmd_free(pwm
->pinentry_tty
);
2215 pwm
->pinentry_tty
= pwmd_strdup(va_arg(ap
, char *));
2217 if (!pwm
->use_pinentry
)
2218 error
= pwmd_command(pwm
, &result
, "OPTION TTY=%s",
2221 case PWMD_OPTION_PINENTRY_DISPLAY
:
2222 if (pwm
->pinentry_display
)
2223 pwmd_free(pwm
->pinentry_display
);
2225 pwm
->pinentry_display
= pwmd_strdup(va_arg(ap
, char *));
2227 if (!pwm
->use_pinentry
)
2228 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s",
2229 pwm
->pinentry_display
);
2231 case PWMD_OPTION_PINENTRY_TERM
:
2232 if (pwm
->pinentry_term
)
2233 pwmd_free(pwm
->pinentry_term
);
2235 pwm
->pinentry_term
= pwmd_strdup(va_arg(ap
, char *));
2237 if (!pwm
->use_pinentry
)
2238 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s",
2239 pwm
->pinentry_term
);
2241 case PWMD_OPTION_PINENTRY_TITLE
:
2243 pwmd_free(pwm
->title
);
2245 pwm
->title
= percent_escape(va_arg(ap
, char *));
2247 if (!pwm
->use_pinentry
)
2248 error
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
2251 case PWMD_OPTION_PINENTRY_PROMPT
:
2253 pwmd_free(pwm
->prompt
);
2255 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
2257 if (!pwm
->use_pinentry
)
2258 error
= pwmd_command(pwm
, &result
, "OPTION PROMPT=%s",
2261 case PWMD_OPTION_PINENTRY_DESC
:
2263 pwmd_free(pwm
->desc
);
2265 pwm
->desc
= percent_escape(va_arg(ap
, char *));
2267 if (!pwm
->use_pinentry
)
2268 error
= pwmd_command(pwm
, &result
, "OPTION DESC=%s",
2271 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
2273 pwmd_free(pwm
->lcctype
);
2275 pwm
->lcctype
= pwmd_strdup(va_arg(ap
, char *));
2277 if (!pwm
->use_pinentry
)
2278 error
= pwmd_command(pwm
, &result
, "OPTION LC_CTYPE=%s",
2281 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
2282 if (pwm
->lcmessages
)
2283 pwmd_free(pwm
->lcmessages
);
2285 pwm
->lcmessages
= pwmd_strdup(va_arg(ap
, char *));
2287 if (!pwm
->use_pinentry
)
2288 error
= pwmd_command(pwm
, &result
, "OPTION LC_MESSAGES=%s",
2292 error
= GPG_ERR_NOT_IMPLEMENTED
;
2300 gpg_error_t
pwmd_get_fd(pwm_t
*pwm
, int *fd
)
2303 return GPG_ERR_INV_ARG
;
2306 return GPG_ERR_INV_STATE
;
2312 gpg_error_t
pwmd_get_fd2(pwm_t
*pwm
, int *fd
)
2314 #ifndef WITH_PINENTRY
2315 return GPG_ERR_NOT_IMPLEMENTED
;
2318 return GPG_ERR_INV_ARG
;
2320 if (pwm
->nb_fd
== -1)
2321 return GPG_ERR_INV_STATE
;
2328 pwm_t
*pwmd_new(const char *name
)
2330 pwm_t
*h
= pwmd_calloc(1, sizeof(pwm_t
));
2336 h
->name
= pwmd_strdup(name
);
2345 #ifdef WITH_PINENTRY
2351 void pwmd_free(void *ptr
)
2356 void *pwmd_malloc(size_t size
)
2358 return xmalloc(size
);
2361 void *pwmd_calloc(size_t nmemb
, size_t size
)
2363 return xcalloc(nmemb
, size
);
2366 void *pwmd_realloc(void *ptr
, size_t size
)
2368 return xrealloc(ptr
, size
);
2371 char *pwmd_strdup(const char *str
)
2373 return xstrdup(str
);