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
);
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_tcp_connect() and
332 * pwmd_tcp_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 /* For pinentry retries. */
992 if (!pwm
->is_open_cmd
) {
994 pwmd_free(pwm
->filename
);
996 pwm
->filename
= pwmd_strdup(filename
);
999 pwm
->is_open_cmd
= 1;
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 pwm
->is_open_cmd
= 0;
1064 #ifdef WITH_PINENTRY
1065 if (pwm
->nb_fd
!= -1) {
1071 if (done
&& pwm
->tcp_conn
&& pwm
->tcp_conn
->fd
!= -1) {
1072 close(pwm
->tcp_conn
->fd
);
1073 pwm
->tcp_conn
->fd
= -1;
1080 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
);
1081 pwmd_async_t
pwmd_process(pwm_t
*pwm
, gpg_error_t
*rc
, char **result
)
1085 struct timeval tv
= {0, 0};
1090 *rc
= GPG_ERR_INV_ARG
;
1094 /* When not in a command, this will let libassuan process status messages
1095 * by calling PWMD_OPTION_STATUS_FUNC. The client can poll the file
1096 * descriptor returned by pwmd_get_fd() to determine when this should be
1097 * called or call pwmd_pending_line() to determine whether a buffered line
1098 * needs to be processed. */
1099 if (pwm
->cmd
== ASYNC_CMD_NONE
) {
1100 *rc
= assuan_command(pwm
, pwm
->ctx
, NULL
, "NOP");
1104 /* Fixes pwmd_open/save_async2() when there is a cached or new file. */
1105 if (pwm
->state
== ASYNC_DONE
) {
1106 reset_async(pwm
, 0);
1110 if (pwm
->state
!= ASYNC_PROCESS
) {
1111 *rc
= GPG_ERR_INV_STATE
;
1116 if (pwm
->cmd
== ASYNC_CMD_DNS
) {
1119 if (pwm
->tcp_conn
->rc
) {
1120 *rc
= pwm
->tcp_conn
->rc
;
1121 reset_async(pwm
, 1);
1127 n
= ares_fds(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1129 /* Shouldn't happen. */
1134 n
= pth_select(n
, &rfds
, &wfds
, NULL
, &tv
);
1136 n
= select(n
, &rfds
, &wfds
, NULL
, &tv
);
1140 ares_process(pwm
->tcp_conn
->chan
, &rfds
, &wfds
);
1144 else if (pwm
->cmd
== ASYNC_CMD_CONNECT
) {
1145 if (pwm
->tcp_conn
->rc
== GPG_ERR_EINPROGRESS
) {
1147 socklen_t len
= sizeof(int);
1150 FD_SET(pwm
->tcp_conn
->fd
, &fds
);
1152 n
= pth_select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1154 n
= select(pwm
->tcp_conn
->fd
+1, NULL
, &fds
, NULL
, &tv
);
1157 if (!n
|| !FD_ISSET(pwm
->tcp_conn
->fd
, &fds
))
1160 *rc
= gpg_error_from_syserror();
1161 reset_async(pwm
, 1);
1165 ret
= getsockopt(pwm
->tcp_conn
->fd
, SOL_SOCKET
, SO_ERROR
, &n
, &len
);
1168 *rc
= ret
? gpg_error_from_syserror() : gpg_error_from_errno(n
);
1169 reset_async(pwm
, 1);
1173 else if (pwm
->tcp_conn
->rc
) {
1174 *rc
= pwm
->tcp_conn
->rc
;
1175 reset_async(pwm
, 1);
1179 fcntl(pwm
->tcp_conn
->fd
, F_SETFL
, 0);
1180 *rc
= setup_tcp_session(pwm
);
1183 switch (pwm
->tcp_conn
->cmd
) {
1184 case ASYNC_CMD_HOSTKEY
:
1186 *result
= pwm
->result
;
1193 return reset_async(pwm
, *rc
? 1 : 0);
1197 #ifdef WITH_PINENTRY
1198 if (pwm
->cmd
== ASYNC_CMD_OPEN2
|| pwm
->cmd
== ASYNC_CMD_SAVE2
) {
1199 if (pwm
->nb_fd
== -1) {
1200 *rc
= GPG_ERR_INV_STATE
;
1201 return reset_async(pwm
, 0);
1205 FD_SET(pwm
->nb_fd
, &fds
);
1207 n
= pth_select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1209 n
= select(pwm
->nb_fd
+1, &fds
, NULL
, NULL
, &tv
);
1212 *rc
= gpg_error_from_syserror();
1213 return reset_async(pwm
, 0);
1217 pwmd_nb_status_t nb
;
1218 size_t len
= read(pwm
->nb_fd
, &nb
, sizeof(nb
));
1220 if (len
!= sizeof(nb
)) {
1221 *rc
= gpg_error_from_syserror();
1222 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1228 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN2
? 1 : 0);
1230 if (pwm
->cmd
== ASYNC_CMD_OPEN2
) {
1231 *rc
= do_open_command(pwm
, pwm
->filename
, nb
.password
);
1232 memset(&nb
, 0, sizeof(pwmd_nb_status_t
));
1234 if (*rc
== EPWMD_BADKEY
) {
1235 if (--pwm
->pin_try
> 0) {
1236 reset_async(pwm
, 0);
1237 pwm
->cmd
= ASYNC_CMD_OPEN2
;
1238 *rc
= pwmd_open_async2(pwm
, pwm
->filename
);
1241 return reset_async(pwm
, 1);
1247 return reset_async(pwm
, *rc
? 1 : 0);
1250 return reset_async(pwm
, 0);
1258 *rc
= GPG_ERR_INV_STATE
;
1259 return reset_async(pwm
, 0);
1262 /* This is for the non-blocking OPEN and SAVE commands. */
1264 FD_SET(pwm
->fd
, &fds
);
1266 n
= pth_select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1268 n
= select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
1272 *rc
= gpg_error_from_syserror();
1273 return reset_async(pwm
, 0);
1277 if (FD_ISSET(pwm
->fd
, &fds
))
1278 *rc
= parse_assuan_line(pwm
);
1281 while (!*rc
&& assuan_pending_line(pwm
->ctx
))
1282 *rc
= parse_assuan_line(pwm
);
1284 /* For pinentry retries. */
1285 if (pwm
->is_open_cmd
&& gpg_err_code(*rc
) == EPWMD_BADKEY
&&
1286 ++pwm
->ntries
< pwm
->pinentry_tries
) {
1287 pwm
->state
= ASYNC_INIT
;
1288 *rc
= pwmd_open_async(pwm
, pwm
->filename
);
1292 return reset_async(pwm
, pwm
->cmd
== ASYNC_CMD_OPEN
? 1 : 0);
1294 if (pwm
->state
== ASYNC_DONE
) {
1295 reset_async(pwm
, 0);
1302 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
1303 char **result
, const char *cmd
)
1311 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
1312 pwm
->status_func
, pwm
->status_data
);
1316 pwmd_free(data
.buf
);
1322 mem_realloc_cb(&data
, "", 1);
1325 pwmd_free(data
.buf
);
1326 rc
= GPG_ERR_INV_ARG
;
1329 *result
= (char *)data
.buf
;
1333 return gpg_err_code(rc
);
1336 gpg_error_t
pwmd_inquire(pwm_t
*pwm
, const char *cmd
, pwmd_inquire_fn fn
,
1339 if (!pwm
|| !cmd
|| !fn
)
1340 return GPG_ERR_INV_ARG
;
1342 pwm
->inquire_func
= fn
;
1343 pwm
->inquire_data
= data
;
1344 return assuan_command(pwm
, pwm
->ctx
, NULL
, cmd
);
1347 static gpg_error_t
terminate_pinentry(pwm_t
*pwm
)
1349 #ifndef WITH_PINENTRY
1350 return GPG_ERR_NOT_IMPLEMENTED
;
1352 pid_t pid
= pwm
->pid
;
1356 if (!pwm
|| pid
== -1)
1357 return GPG_ERR_INV_ARG
;
1359 if (kill(pid
, 0) == 0) {
1360 if (kill(pid
, SIGTERM
) == -1) {
1361 if (kill(pid
, SIGKILL
) == -1)
1362 return gpg_error_from_errno(errno
);
1366 return gpg_error_from_errno(errno
);
1372 #ifdef WITH_PINENTRY
1373 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
1378 tmp
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1381 return gpg_error_from_errno(ENOMEM
);
1384 pwm
->title
= pwmd_strdup(N_("LibPWMD"));
1390 pwm
->prompt
= pwmd_strdup(N_("Passphrase:"));
1395 if (!pwm
->desc
&& !which
) {
1396 pwm
->desc
= pwmd_strdup(N_("Enter a passphrase."));
1403 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1404 N_("Invalid passphrase, please try again."));
1405 else if (which
== 2)
1406 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
1407 N_("Please type the passphrase again for confirmation."));
1409 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETERROR %s", pwm
->desc
);
1411 error
= pinentry_command(pwm
, NULL
, tmp
);
1418 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETPROMPT %s", pwm
->prompt
);
1419 error
= pinentry_command(pwm
, NULL
, tmp
);
1426 snprintf(tmp
, ASSUAN_LINELENGTH
, "SETDESC %s", pwm
->title
);
1427 error
= pinentry_command(pwm
, NULL
, tmp
);
1433 return gpg_error_from_errno(ENOMEM
);
1436 static void update_pinentry_settings(pwm_t
*pwm
)
1442 char *pwbuf
= _getpwuid(&pw
);
1447 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
.pw_dir
);
1450 if ((fp
= fopen(buf
, "r")) == NULL
)
1453 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
1454 char name
[32], val
[256];
1456 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
1459 if (strcasecmp(name
, "TTYNAME") == 0) {
1460 pwmd_free(pwm
->pinentry_tty
);
1461 pwm
->pinentry_tty
= pwmd_strdup(val
);
1463 else if (strcasecmp(name
, "TTYTYPE") == 0) {
1464 pwmd_free(pwm
->pinentry_term
);
1465 pwm
->pinentry_term
= pwmd_strdup(val
);
1467 else if (strcasecmp(name
, "DISPLAY") == 0) {
1468 pwmd_free(pwm
->pinentry_display
);
1469 pwm
->pinentry_display
= pwmd_strdup(val
);
1471 else if (strcasecmp(name
, "PATH") == 0) {
1472 pwmd_free(pwm
->pinentry_path
);
1473 pwm
->pinentry_path
= pwmd_strdup(val
);
1480 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
1483 assuan_context_t ctx
;
1484 int child_list
[] = {-1};
1485 char *display
= getenv("DISPLAY");
1486 const char *argv
[10];
1487 const char **p
= argv
;
1488 int have_display
= 0;
1490 char *ttybuf
= NULL
;
1492 update_pinentry_settings(pwm
);
1494 if (pwm
->pinentry_display
|| display
)
1497 if (!pwm
->pinentry_tty
) {
1498 ttybuf
= pwmd_malloc(255);
1501 return gpg_error_from_errno(ENOMEM
);
1503 rc
= ttyname_r(STDOUT_FILENO
, ttybuf
, 255);
1507 return gpg_error_from_errno(rc
);
1513 tty
= pwm
->pinentry_tty
;
1516 if (!have_display
&& !tty
)
1517 return GPG_ERR_ENOTTY
;
1520 *p
++ = have_display
? "--display" : "--ttyname";
1521 *p
++ = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
1524 *p
++ = "--lc-ctype";
1525 *p
++ = pwm
->lcctype
;
1528 if (pwm
->lcmessages
) {
1529 *p
++ = "--lc-messages";
1530 *p
++ = pwm
->lcmessages
;
1535 if (!have_display
) {
1537 *p
++ = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
1541 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
1549 pwm
->pid
= assuan_get_pid(ctx
);
1551 return set_pinentry_strings(pwm
, 0);
1554 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1559 n
= launch_pinentry(pwm
);
1565 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
1568 static void pinentry_disconnect(pwm_t
*pwm
)
1571 assuan_disconnect(pwm
->pctx
);
1578 * Only called from a child process.
1580 static void catchsig(int sig
)
1584 if (gelapsed
++ >= gtimeout
)
1585 terminate_pinentry(gpwm
);
1596 * Borrowed from libassuan.
1598 static char *percent_escape(const char *atext
)
1600 const unsigned char *s
;
1601 int len
= strlen(atext
) * 3 + 1;
1602 char *buf
= (char *)pwmd_malloc(len
), *p
= buf
;
1607 for (s
=(const unsigned char *)atext
; *s
; s
++) {
1609 sprintf (p
, "%%%02X", *s
);
1620 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
1623 return GPG_ERR_INV_ARG
;
1625 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
1628 gpg_error_t
pwmd_command_ap(pwm_t
*pwm
, char **result
, const char *cmd
,
1636 return GPG_ERR_INV_ARG
;
1639 * C99 allows the dst pointer to be null which will calculate the length
1640 * of the would-be result and return it.
1642 len
= vsnprintf(NULL
, 0, cmd
, ap
)+1;
1643 buf
= (char *)pwmd_malloc(len
);
1646 return gpg_error_from_errno(ENOMEM
);
1648 len
= vsnprintf(buf
, len
, cmd
, ap
);
1650 if (buf
[strlen(buf
)-1] == '\n')
1651 buf
[strlen(buf
)-1] = 0;
1653 if (buf
[strlen(buf
)-1] == '\r')
1654 buf
[strlen(buf
)-1] = 0;
1656 error
= send_command(pwm
, result
, buf
);
1662 * Avoid sending the BYE command here. libassuan will close the file
1663 * descriptor and release the assuan context. Use pwmd_close() instead.
1665 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
1671 return GPG_ERR_INV_ARG
;
1677 error
= pwmd_command_ap(pwm
, result
, cmd
, ap
);
1682 #ifdef WITH_PINENTRY
1683 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
1686 signal(SIGALRM
, catchsig
);
1691 return pinentry_command(pwm
, result
, "GETPIN");
1694 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
1696 int pin_try
= *try_n
;
1702 if (pin_try
== -1) {
1703 error
= set_pinentry_strings(pwm
, which
);
1706 pinentry_disconnect(pwm
);
1711 if (pwm
->pinentry_tries
-1 != pin_try
) {
1712 error
= set_pinentry_strings(pwm
, 1);
1715 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
;
1732 if (pin_try
!= -1 && pin_try
--)
1736 pinentry_disconnect(pwm
);
1746 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
1750 char *result
= NULL
;
1752 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
1755 return gpg_error_from_errno(ENOMEM
);
1757 snprintf(buf
, ASSUAN_LINELENGTH
, "OPEN %s %s", filename
,
1758 password
? password
: "");
1759 error
= send_command(pwm
, &result
, buf
);
1762 if (error
&& result
)
1768 static gpg_error_t
do_pwmd_open(pwm_t
*pwm
, const char *filename
, int nb
)
1770 char *result
= NULL
;
1771 char *password
= NULL
;
1772 char path
[PATH_MAX
];
1773 #ifdef WITH_PINENTRY
1778 if (!pwm
|| !filename
|| !*filename
)
1779 return GPG_ERR_INV_ARG
;
1781 #ifdef WITH_PINENTRY
1782 pin_try
= pwm
->pinentry_tries
- 1;
1786 * Avoid calling pinentry if the password is cached on the server or if
1787 * this is a new file.
1789 rc
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
1794 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
1797 if (access(path
, R_OK
) == -1) {
1798 if (errno
== ENOENT
)
1802 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
1804 if (rc
== EPWMD_CACHE_NOT_FOUND
) {
1805 if (pwm
->passfunc
) {
1806 password
= (char *)pwm
->passfunc(pwm
->passdata
);
1810 #ifdef WITH_PINENTRY
1812 * Get the password from pinentry.
1814 if (pwm
->use_pinentry
) {
1816 * Nonblocking is wanted. fork() then return a file descriptor
1817 * that the client can use to read() from.
1822 pwmd_nb_status_t pw
;
1825 return gpg_error_from_syserror();
1828 pwm
->filename
= pwmd_strdup(filename
);
1831 return gpg_error_from_errno(ENOMEM
);
1844 if (pwm
->pinentry_timeout
> 0) {
1846 gtimeout
= pwm
->pinentry_timeout
;
1850 rc
= getpin(pwm
, &password
, &pin_try
, 0);
1854 pinentry_disconnect(pwm
);
1856 if (gtimeout
&& gelapsed
>= gtimeout
)
1857 rc
= GPG_ERR_TIMEOUT
;
1861 pth_write(p
[1], &pw
, sizeof(pw
));
1863 write(p
[1], &pw
, sizeof(pw
));
1870 * Don't count the time it takes to open the file
1871 * which may have many iterations.
1873 signal(SIGALRM
, SIG_DFL
);
1874 snprintf(pw
.password
, sizeof(pw
.password
), "%s", password
);
1875 pinentry_disconnect(pwm
);
1878 pth_write(p
[1], &pw
, sizeof(pw
));
1880 write(p
[1], &pw
, sizeof(pw
));
1882 memset(&pw
, 0, sizeof(pw
));
1887 rc
= gpg_error_from_syserror();
1903 * Not using pinentry and the file was not found
1906 password
= pwm
->password
;
1907 #ifdef WITH_PINENTRY
1915 pwm
->state
= ASYNC_DONE
;
1916 rc
= do_open_command(pwm
, filename
, password
);
1919 * Keep the user defined password set with pwmd_setopt(). The password may
1920 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1922 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
1923 pwmd_free(password
);
1925 #ifdef WITH_PINENTRY
1926 if (rc
== EPWMD_BADKEY
) {
1927 if (pin_try
-- > 0 && !nb
) {
1928 rc
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
1929 N_("Invalid passphrase, please try again."));
1938 pinentry_disconnect(pwm
);
1946 pwmd_free(pwm
->filename
);
1948 pwm
->filename
= pwmd_strdup(filename
);
1954 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
1956 return do_pwmd_open(pwm
, filename
, 0);
1959 gpg_error_t
pwmd_open_async2(pwm_t
*pwm
, const char *filename
)
1961 #ifndef WITH_PINENTRY
1962 return GPG_ERR_NOT_IMPLEMENTED
;
1966 if (pwm
->cmd
!= ASYNC_CMD_OPEN2
)
1967 pwm
->pin_try
= pwm
->pinentry_tries
;
1969 pwm
->cmd
= ASYNC_CMD_OPEN2
;
1970 pwm
->state
= ASYNC_PROCESS
;
1971 rc
= do_pwmd_open(pwm
, filename
, 1);
1974 reset_async(pwm
, 1);
1980 #ifdef WITH_PINENTRY
1981 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
1985 char *result
= NULL
;
1989 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
1993 pinentry_disconnect(pwm
);
1996 pwmd_free(*password
);
2006 if (strcmp(*password
, result
)) {
2007 pwmd_free(*password
);
2009 pinentry_disconnect(pwm
);
2010 error
= EPWMD_BADKEY
;
2015 pinentry_disconnect(pwm
);
2020 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
2024 char *result
= NULL
;
2026 buf
= pwmd_malloc(ASSUAN_LINELENGTH
+1);
2029 return gpg_error_from_errno(ENOMEM
);
2031 snprintf(buf
, ASSUAN_LINELENGTH
, "SAVE %s", password
? password
: "");
2032 error
= send_command(pwm
, &result
, buf
);
2035 if (error
&& result
)
2041 static gpg_error_t
do_pwmd_save(pwm_t
*pwm
, int nb
)
2043 char *result
= NULL
;
2044 char *password
= NULL
;
2048 return GPG_ERR_INV_ARG
;
2050 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
2051 rc
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
2053 if (rc
== EPWMD_CACHE_NOT_FOUND
) {
2055 password
= (char *)(*pwm
->passfunc
)(pwm
->passdata
);
2056 #ifdef WITH_PINENTRY
2057 else if (pwm
->use_pinentry
) {
2061 pwmd_nb_status_t pw
;
2064 return gpg_error_from_syserror();
2079 rc
= do_save_getpin(pwm
, &password
);
2080 } while (rc
== EPWMD_BADKEY
);
2084 pinentry_disconnect(pwm
);
2088 pth_write(p
[1], &pw
, sizeof(pw
));
2090 write(p
[1], &pw
, sizeof(pw
));
2096 rc
= do_save_command(pwm
, password
);
2097 pinentry_disconnect(pwm
);
2100 pth_write(p
[1], &pw
, sizeof(pw
));
2102 write(p
[1], &pw
, sizeof(pw
));
2108 rc
= gpg_error_from_syserror();
2121 rc
= do_save_getpin(pwm
, &password
);
2132 pwm
->state
= ASYNC_DONE
;
2136 password
= pwm
->password
;
2138 rc
= do_save_command(pwm
, password
);
2140 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
2141 pwmd_free(password
);
2146 gpg_error_t
pwmd_save_async2(pwm_t
*pwm
)
2148 #ifndef WITH_PINENTRY
2149 return GPG_ERR_NOT_IMPLEMENTED
;
2153 pwm
->cmd
= ASYNC_CMD_SAVE2
;
2154 pwm
->state
= ASYNC_PROCESS
;
2155 rc
= do_pwmd_save(pwm
, 1);
2158 reset_async(pwm
, 0);
2164 gpg_error_t
pwmd_save(pwm_t
*pwm
)
2166 return do_pwmd_save(pwm
, 0);
2169 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
2172 int n
= va_arg(ap
, int);
2175 gpg_error_t error
= 0;
2178 return GPG_ERR_INV_ARG
;
2183 case PWMD_OPTION_STATUS_FUNC
:
2184 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
2186 case PWMD_OPTION_STATUS_DATA
:
2187 pwm
->status_data
= va_arg(ap
, void *);
2189 case PWMD_OPTION_PASSWORD_FUNC
:
2190 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
2192 case PWMD_OPTION_PASSWORD_DATA
:
2193 pwm
->passdata
= va_arg(ap
, void *);
2195 case PWMD_OPTION_PASSWORD
:
2196 arg1
= va_arg(ap
, char *);
2199 pwmd_free(pwm
->password
);
2201 pwm
->password
= pwmd_strdup(arg1
);
2203 case PWMD_OPTION_PINENTRY
:
2204 n
= va_arg(ap
, int);
2206 if (n
!= 0 && n
!= 1) {
2208 error
= GPG_ERR_INV_VALUE
;
2211 pwm
->use_pinentry
= n
;
2212 error
= pwmd_command(pwm
, &result
, "OPTION PINENTRY=%i",
2213 !pwm
->use_pinentry
);
2216 #ifdef WITH_PINENTRY
2217 case PWMD_OPTION_PINENTRY_TRIES
:
2218 n
= va_arg(ap
, int);
2222 error
= GPG_ERR_INV_VALUE
;
2225 pwm
->pinentry_tries
= n
;
2228 case PWMD_OPTION_PINENTRY_TIMEOUT
:
2229 n
= va_arg(ap
, int);
2233 error
= GPG_ERR_INV_VALUE
;
2236 pwm
->pinentry_timeout
= n
;
2238 if (!pwm
->use_pinentry
)
2239 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i",
2240 pwm
->pinentry_timeout
);
2242 case PWMD_OPTION_PINENTRY_PATH
:
2243 if (pwm
->pinentry_path
)
2244 pwmd_free(pwm
->pinentry_path
);
2246 pwm
->pinentry_path
= pwmd_strdup(va_arg(ap
, char *));
2248 if (!pwm
->use_pinentry
)
2249 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s",
2250 pwm
->pinentry_path
);
2252 case PWMD_OPTION_PINENTRY_TTY
:
2253 if (pwm
->pinentry_tty
)
2254 pwmd_free(pwm
->pinentry_tty
);
2256 pwm
->pinentry_tty
= pwmd_strdup(va_arg(ap
, char *));
2258 if (!pwm
->use_pinentry
)
2259 error
= pwmd_command(pwm
, &result
, "OPTION TTY=%s",
2262 case PWMD_OPTION_PINENTRY_DISPLAY
:
2263 if (pwm
->pinentry_display
)
2264 pwmd_free(pwm
->pinentry_display
);
2266 pwm
->pinentry_display
= pwmd_strdup(va_arg(ap
, char *));
2268 if (!pwm
->use_pinentry
)
2269 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s",
2270 pwm
->pinentry_display
);
2272 case PWMD_OPTION_PINENTRY_TERM
:
2273 if (pwm
->pinentry_term
)
2274 pwmd_free(pwm
->pinentry_term
);
2276 pwm
->pinentry_term
= pwmd_strdup(va_arg(ap
, char *));
2278 if (!pwm
->use_pinentry
)
2279 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s",
2280 pwm
->pinentry_term
);
2282 case PWMD_OPTION_PINENTRY_TITLE
:
2284 pwmd_free(pwm
->title
);
2286 pwm
->title
= percent_escape(va_arg(ap
, char *));
2288 if (!pwm
->use_pinentry
)
2289 error
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
2292 case PWMD_OPTION_PINENTRY_PROMPT
:
2294 pwmd_free(pwm
->prompt
);
2296 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
2298 if (!pwm
->use_pinentry
)
2299 error
= pwmd_command(pwm
, &result
, "OPTION PROMPT=%s",
2302 case PWMD_OPTION_PINENTRY_DESC
:
2304 pwmd_free(pwm
->desc
);
2306 pwm
->desc
= percent_escape(va_arg(ap
, char *));
2308 if (!pwm
->use_pinentry
)
2309 error
= pwmd_command(pwm
, &result
, "OPTION DESC=%s",
2312 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
2314 pwmd_free(pwm
->lcctype
);
2316 pwm
->lcctype
= pwmd_strdup(va_arg(ap
, char *));
2318 if (!pwm
->use_pinentry
)
2319 error
= pwmd_command(pwm
, &result
, "OPTION LC_CTYPE=%s",
2322 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
2323 if (pwm
->lcmessages
)
2324 pwmd_free(pwm
->lcmessages
);
2326 pwm
->lcmessages
= pwmd_strdup(va_arg(ap
, char *));
2328 if (!pwm
->use_pinentry
)
2329 error
= pwmd_command(pwm
, &result
, "OPTION LC_MESSAGES=%s",
2333 error
= GPG_ERR_NOT_IMPLEMENTED
;
2341 gpg_error_t
pwmd_get_fd(pwm_t
*pwm
, int *fd
)
2344 return GPG_ERR_INV_ARG
;
2347 return GPG_ERR_INV_STATE
;
2353 gpg_error_t
pwmd_get_fd2(pwm_t
*pwm
, int *fd
)
2355 #ifndef WITH_PINENTRY
2356 return GPG_ERR_NOT_IMPLEMENTED
;
2359 return GPG_ERR_INV_ARG
;
2361 if (pwm
->nb_fd
== -1)
2362 return GPG_ERR_INV_STATE
;
2369 pwm_t
*pwmd_new(const char *name
)
2371 pwm_t
*h
= pwmd_calloc(1, sizeof(pwm_t
));
2377 h
->name
= pwmd_strdup(name
);
2386 #ifdef WITH_PINENTRY
2392 void pwmd_free(void *ptr
)
2397 void *pwmd_malloc(size_t size
)
2399 return xmalloc(size
);
2402 void *pwmd_calloc(size_t nmemb
, size_t size
)
2404 return xcalloc(nmemb
, size
);
2407 void *pwmd_realloc(void *ptr
, size_t size
)
2409 return xrealloc(ptr
, size
);
2412 char *pwmd_strdup(const char *str
)
2414 return xstrdup(str
);