Make sure pwmd_command_ap() has a handle and command.
[libpwmd.git] / libpwmd.c
blob88d202a52ccef50f85eed071226d42fa7808405f
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
28 #include <signal.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <sys/wait.h>
32 #include <fcntl.h>
33 #include <pwd.h>
34 #include <time.h>
35 #include <sys/types.h>
36 #include <limits.h>
37 #include <sys/select.h>
38 #include <netdb.h>
39 #include <netinet/in.h>
40 #include <sys/socket.h>
41 #include <libpwmd.h>
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
47 #ifdef WITH_LIBPTH
48 #include <pth.h>
49 #endif
51 #ifdef WITH_TCP
52 #define DNS_USE_GETTIMEOFDAY_FOR_ID 1
53 #include <ares.h>
54 #include <arpa/nameser.h>
55 #ifdef WITH_LIBPTH
56 #include <gcrypt.h>
57 GCRY_THREAD_OPTION_PTH_IMPL;
58 #endif
59 #endif
61 #ifdef HAVE_ASSUAN_H
62 #include <assuan.h>
63 #endif
65 #ifdef HAVE_SETLOCALE
66 #include <locale.h>
67 #endif
69 #include "gettext.h"
70 #define N_(msgid) dgettext("libpwmd", msgid)
72 #include "mem.h"
73 #include "types.h"
75 #ifdef WITH_PINENTRY
76 static pwm_t *gpwm;
77 static int gelapsed, gtimeout;
78 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd);
79 static gpg_error_t global_error;
80 #endif
82 const char *pwmd_strerror(gpg_error_t e)
84 gpg_err_code_t code = gpg_err_code(e);
86 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
87 switch (code) {
88 case GPG_ERR_USER_1:
89 default:
90 return N_("Unknown error");
91 case GPG_ERR_USER_2:
92 return N_("No cache slots available");
93 case GPG_ERR_USER_3:
94 return N_("Recursion loop");
95 case GPG_ERR_USER_4:
96 return N_("No file is open");
97 case GPG_ERR_USER_5:
98 return N_("General LibXML error");
99 case GPG_ERR_USER_6:
100 return N_("File modified");
101 case GPG_ERR_USER_7:
102 return N_("Access denied");
106 return gpg_strerror(e);
109 gpg_error_t pwmd_init()
111 static int initialized;
113 if (initialized)
114 return 0;
116 #ifdef WITH_LIBPTH
117 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pth);
118 #endif
119 #ifndef MEM_DEBUG
120 xmem_init();
121 #endif
122 #ifdef ENABLE_NLS
123 bindtextdomain("libpwmd", LOCALEDIR);
124 #endif
125 gpg_err_init();
126 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
127 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
128 initialized = 1;
129 return 0;
132 static pwm_t *_socket_connect_finalize(pwm_t *pwm, assuan_context_t ctx)
134 int active[2];
135 int n = assuan_get_active_fds(ctx, 0, active, sizeof(active));
137 pwm->fd = n <= 0 ? -1 : dup(active[0]);
138 pwm->ctx = ctx;
139 #ifdef WITH_PINENTRY
140 pwm->pid = -1;
141 pwm->pinentry_tries = 3;
142 #endif
143 assuan_set_pointer(ctx, pwm);
144 return pwm;
147 #ifdef WITH_TCP
148 static int read_hook(assuan_context_t ctx, assuan_fd_t fd, void *data,
149 size_t len, ssize_t *ret)
151 pwm_t *pwm = assuan_get_pointer(ctx);
153 if (!pwm || !pwm->tcp_conn)
154 *ret = read((int)fd, data, len);
155 else {
156 do {
157 *ret = libssh2_channel_read(pwm->tcp_conn->channel, data, len);
158 } while (*ret == LIBSSH2_ERROR_EAGAIN);
161 return *ret <= 0 ? 0 : 1;
164 static int write_hook(assuan_context_t ctx, assuan_fd_t fd, const void *data,
165 size_t len, ssize_t *ret)
167 pwm_t *pwm = assuan_get_pointer(ctx);
169 if (!pwm || !pwm->tcp_conn)
170 *ret = write((int)fd, data, len);
171 else {
172 do {
173 *ret = libssh2_channel_write(pwm->tcp_conn->channel, data, len);
174 } while (*ret == LIBSSH2_ERROR_EAGAIN);
177 return *ret <= 0 ? 0 : 1;
180 static void _ssh_deinit(pwmd_tcp_conn_t *conn);
181 static void free_tcp_conn(pwmd_tcp_conn_t *conn)
183 if (!conn)
184 return;
186 if (conn->username) {
187 xfree(conn->username);
188 conn->username = NULL;
191 if (conn->known_hosts) {
192 xfree(conn->known_hosts);
193 conn->known_hosts = NULL;
196 if (conn->identity) {
197 xfree(conn->identity);
198 conn->identity = NULL;
201 if (conn->identity_pub) {
202 xfree(conn->identity_pub);
203 conn->identity_pub = NULL;
206 if (conn->host) {
207 xfree(conn->host);
208 conn->host = NULL;
211 if (conn->hostkey) {
212 xfree(conn->hostkey);
213 conn->hostkey = NULL;
216 if (conn->chan) {
217 ares_destroy(conn->chan);
218 conn->chan = NULL;
221 if (conn->he) {
222 ares_free_hostent(conn->he);
223 conn->he = NULL;
226 if (conn->fd >= 0) {
227 close(conn->fd);
228 conn->fd = -1;
231 if (conn->session)
232 _ssh_deinit(conn);
233 else
234 xfree(conn);
237 static void _ssh_deinit(pwmd_tcp_conn_t *conn)
239 if (!conn)
240 return;
242 if (conn->channel)
243 libssh2_channel_free(conn->channel);
245 if (conn->session) {
246 libssh2_session_disconnect(conn->session, "Bye!");
247 libssh2_session_free(conn->session);
250 conn->session = NULL;
251 conn->channel = NULL;
252 free_tcp_conn(conn);
255 static void _ssh_assuan_deinit(assuan_context_t ctx)
257 pwm_t *pwm = assuan_get_pointer(ctx);
259 _ssh_deinit(pwm->tcp_conn);
263 * Sets common options from both pwmd_tcp_connect() and
264 * pwmd_tcp_connect_async().
266 static gpg_error_t init_tcp_conn(pwmd_tcp_conn_t **dst, const char *host,
267 int port, const char *identity, const char *user, const char *hosts,
268 int get)
270 pwmd_tcp_conn_t *conn;
271 gpg_error_t rc = 0;
273 if (get) {
274 if (!host)
275 return GPG_ERR_INV_ARG;
277 else {
278 if (!host || !identity || !user || !hosts)
279 return GPG_ERR_INV_ARG;
282 conn = xcalloc(1, sizeof(pwmd_tcp_conn_t));
284 if (!conn)
285 return gpg_error_from_errno(ENOMEM);
287 conn->port = port == -1 ? 22 : port;
288 conn->host = xstrdup(host);
290 if (!conn->host) {
291 rc = gpg_error_from_errno(ENOMEM);
292 goto fail;
295 if (!get) {
296 conn->identity = xstrdup(identity);
298 if (!conn->identity) {
299 rc = gpg_error_from_errno(ENOMEM);
300 goto fail;
303 conn->identity_pub = xmalloc(strlen(conn->identity)+5);
305 if (!conn->identity_pub) {
306 rc = gpg_error_from_errno(ENOMEM);
307 goto fail;
310 sprintf(conn->identity_pub, "%s.pub", conn->identity);
311 conn->username = xstrdup(user);
313 if (!conn->username) {
314 rc = gpg_error_from_errno(ENOMEM);
315 goto fail;
318 conn->known_hosts = xstrdup(hosts);
320 if (!conn->known_hosts) {
321 rc = gpg_error_from_errno(ENOMEM);
322 goto fail;
326 *dst = conn;
327 return 0;
329 fail:
330 free_tcp_conn(conn);
331 return rc;
334 static gpg_error_t do_connect(pwm_t *pwm, int prot, void *addr)
336 struct sockaddr_in their_addr;
338 pwm->tcp_conn->fd = socket(prot, SOCK_STREAM, 0);
340 if (pwm->tcp_conn->fd == -1)
341 return gpg_error_from_syserror();
343 if (pwm->tcp_conn->async)
344 fcntl(pwm->tcp_conn->fd, F_SETFL, O_NONBLOCK);
346 pwm->cmd = ASYNC_CMD_CONNECT;
347 their_addr.sin_family = prot;
348 their_addr.sin_port = htons(pwm->tcp_conn->port);
349 their_addr.sin_addr = *((struct in_addr *)addr);
350 memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
352 #ifdef WITH_LIBPTH
353 if (pth_connect(pwm->tcp_conn->fd, (struct sockaddr *)&their_addr,
354 sizeof(their_addr)) == -1)
355 #else
356 if (connect(pwm->tcp_conn->fd, (struct sockaddr *)&their_addr,
357 sizeof(their_addr)) == -1)
358 #endif
359 return gpg_error_from_syserror();
361 return 0;
364 static gpg_error_t ares_error_to_pwmd(int status)
366 if (status != ARES_SUCCESS)
367 warnx("%s", ares_strerror(status));
369 switch (status) {
370 case ARES_ENODATA:
371 case ARES_EFORMERR:
372 case ARES_ENOTFOUND:
373 return GPG_ERR_UNKNOWN_HOST;
374 case ARES_ESERVFAIL:
375 return GPG_ERR_EHOSTDOWN;
376 case ARES_ETIMEOUT:
377 return GPG_ERR_TIMEOUT;
378 case ARES_ENOMEM:
379 return gpg_error_from_errno(ENOMEM);
380 case ARES_ECONNREFUSED:
381 return GPG_ERR_ECONNREFUSED;
382 default:
383 /* FIXME ??? */
384 return GPG_ERR_EHOSTUNREACH;
387 return ARES_SUCCESS;
390 static void dns_resolve_cb(void *arg, int status, int timeouts,
391 unsigned char *abuf, int alen)
393 pwm_t *pwm = arg;
394 int rc;
395 struct hostent *he;
397 if (status == ARES_EDESTRUCTION)
398 return;
400 if (status != ARES_SUCCESS) {
401 pwm->tcp_conn->rc = ares_error_to_pwmd(status);
402 return;
405 //FIXME localhost. works with ipv4. maybe local system config error
406 /* Check for an IPv6 address first. */
407 rc = ares_parse_a_reply(abuf, alen, &he, NULL, NULL);
409 if (rc != ARES_SUCCESS) {
410 if (rc != ARES_ENODATA) {
411 pwm->tcp_conn->rc = ares_error_to_pwmd(status);
412 return;
415 rc = ares_parse_aaaa_reply(abuf, alen, &he, NULL, NULL);
417 if (rc != ARES_SUCCESS) {
418 pwm->tcp_conn->rc = ares_error_to_pwmd(status);
419 return;
423 pwm->tcp_conn->he = he;
424 pwm->tcp_conn->rc = do_connect(pwm, he->h_addrtype, he->h_addr);
427 pwm_t *_do_pwmd_tcp_connect_async(const char *host, int port,
428 const char *identity, const char *user, const char *known_hosts,
429 gpg_error_t *rc, pwmd_async_cmd_t which)
431 pwmd_tcp_conn_t *conn;
432 pwm_t *pwm;
434 *rc = init_tcp_conn(&conn, host, port, identity, user, known_hosts,
435 which == ASYNC_CMD_HOSTKEY ? 1 : 0);
437 if (*rc)
438 return NULL;
440 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
441 *rc = gpg_error_from_syserror();
442 free_tcp_conn(conn);
443 return NULL;
446 conn->async = 1;
447 pwm->tcp_conn = conn;
448 pwm->tcp_conn->cmd = which;
450 if (pwm->tcp_conn->cmd == ASYNC_CMD_HOSTKEY)
451 pwm->tcp_conn->get_only = 1;
453 pwm->cmd = ASYNC_CMD_DNS;
454 pwm->state = ASYNC_PROCESS;
455 ares_init(&pwm->tcp_conn->chan);
456 ares_query(pwm->tcp_conn->chan, pwm->tcp_conn->host, ns_c_any, ns_t_any,
457 dns_resolve_cb, pwm);
458 return pwm;
461 pwm_t *pwmd_tcp_connect_async(const char *host, int port, const char *identity,
462 const char *user, const char *known_hosts, gpg_error_t *rc)
464 return _do_pwmd_tcp_connect_async(host, port, identity, user, known_hosts,
465 rc, ASYNC_CMD_CONNECT);
468 void *_ssh_malloc(size_t size, void **data)
470 return xmalloc(size);
473 void _ssh_free(void *ptr, void **data)
475 xfree(ptr);
478 void *_ssh_realloc(void *ptr, size_t size, void **data)
480 return xrealloc(ptr, size);
483 static char *to_hex(const char *str, size_t slen)
485 int i;
486 char *buf = xmalloc(slen*2+1);
488 if (!buf)
489 return NULL;
491 for (i = 0, buf[0] = 0; i < slen; i++) {
492 char tmp[3];
494 sprintf(tmp, "%02x", (unsigned char)str[i]);
495 strcat(buf, tmp);
498 return buf;
501 static int verify_host_key(pwm_t *pwm)
503 FILE *fp = fopen(pwm->tcp_conn->known_hosts, "r");
504 char *buf, *p;
506 if (!fp)
507 return 1;
509 buf = xmalloc(LINE_MAX);
511 if (!buf)
512 goto fail;
514 while ((p = fgets(buf, LINE_MAX, fp))) {
515 if (*p == '#' || isspace(*p))
516 continue;
518 if (p[strlen(p)-1] == '\n')
519 p[strlen(p)-1] = 0;
521 if (!strcmp(buf, pwm->tcp_conn->hostkey))
522 goto done;
525 fail:
526 if (buf)
527 xfree(buf);
529 fclose(fp);
530 return 1;
532 done:
533 xfree(buf);
534 fclose(fp);
535 return 0;
538 static gpg_error_t authenticate_ssh(pwm_t *pwm)
540 const char *fp = libssh2_hostkey_hash(pwm->tcp_conn->session,
541 LIBSSH2_HOSTKEY_HASH_SHA1);
542 char *userauth;
544 pwm->tcp_conn->hostkey = to_hex(fp, 20);
546 if (!pwm->tcp_conn->hostkey)
547 return gpg_error_from_errno(ENOMEM);
549 if (pwm->tcp_conn->get_only)
550 return 0;
552 if (!fp || verify_host_key(pwm))
553 return GPG_ERR_CHECKSUM;
555 userauth = libssh2_userauth_list(pwm->tcp_conn->session,
556 pwm->tcp_conn->username, strlen(pwm->tcp_conn->username));
558 if (!userauth || !strstr(userauth, "publickey"))
559 return GPG_ERR_BAD_PIN_METHOD;
561 if (libssh2_userauth_publickey_fromfile(pwm->tcp_conn->session,
562 pwm->tcp_conn->username, pwm->tcp_conn->identity_pub,
563 pwm->tcp_conn->identity, NULL))
564 return GPG_ERR_BAD_SECKEY;
566 return 0;
569 static pwm_t *setup_context(pwm_t *pwm, gpg_error_t *rc)
571 assuan_context_t ctx;
572 struct assuan_io_hooks io_hooks = {read_hook, write_hook};
574 pwm->tcp_conn->session = libssh2_session_init_ex(_ssh_malloc, _ssh_free,
575 _ssh_realloc, NULL);
577 if (!pwm->tcp_conn->session) {
578 *rc = gpg_error_from_errno(ENOMEM);
579 goto fail;
582 if (libssh2_session_startup(pwm->tcp_conn->session, pwm->tcp_conn->fd)) {
583 *rc = GPG_ERR_ASSUAN_SERVER_FAULT;
584 goto fail;
587 *rc = authenticate_ssh(pwm);
589 if (*rc)
590 goto fail;
592 /* pwmd_get_hostkey(). */
593 if (pwm->tcp_conn->get_only) {
594 pwm->result = xstrdup(pwm->tcp_conn->hostkey);
596 if (!pwm->result) {
597 *rc = gpg_error_from_errno(ENOMEM);
598 goto fail;
601 return pwm;
604 pwm->tcp_conn->channel = libssh2_channel_open_session(pwm->tcp_conn->session);
606 if (!pwm->tcp_conn->channel) {
607 *rc = GPG_ERR_ASSUAN_SERVER_FAULT;
608 goto fail;
611 if (libssh2_channel_shell(pwm->tcp_conn->channel)) {
612 *rc = GPG_ERR_ASSUAN_SERVER_FAULT;
613 goto fail;
616 assuan_set_io_hooks(&io_hooks);
617 *rc = assuan_socket_connect_fd(&ctx, pwm->tcp_conn->fd, 0, pwm);
619 if (*rc)
620 goto fail;
622 assuan_set_finish_handler(ctx, _ssh_assuan_deinit);
623 return _socket_connect_finalize(pwm, ctx);
625 fail:
626 if (!pwm->tcp_conn->async)
627 pwmd_close(pwm);
629 return NULL;
632 static pwm_t *_do_pwmd_tcp_connect(const char *host, int port,
633 const char *identity, const char *user, const char *known_hosts,
634 gpg_error_t *rc, int get)
636 pwm_t *pwm = NULL;
637 pwmd_tcp_conn_t *conn;
639 *rc = init_tcp_conn(&conn, host, port, identity, user, known_hosts, get);
641 if (*rc)
642 return NULL;
644 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
645 *rc = gpg_error_from_errno(ENOMEM);
646 goto fail;
649 pwm->tcp_conn = conn;
650 pwm->tcp_conn->get_only = get;
651 pwm->cmd = ASYNC_CMD_DNS;
652 ares_init(&pwm->tcp_conn->chan);
653 ares_query(pwm->tcp_conn->chan, pwm->tcp_conn->host, ns_c_any, ns_t_any,
654 dns_resolve_cb, pwm);
656 /* dns_resolve_cb() may have already been called. */
657 if (pwm->tcp_conn->rc) {
658 *rc = pwm->tcp_conn->rc;
659 goto fail;
663 * Fake a blocking DNS lookup. libcares does a better job than
664 * getaddrinfo().
666 do {
667 fd_set rfds, wfds;
668 int n;
669 struct timeval tv;
671 FD_ZERO(&rfds);
672 FD_ZERO(&wfds);
673 n = ares_fds(pwm->tcp_conn->chan, &rfds, &wfds);
674 ares_timeout(pwm->tcp_conn->chan, NULL, &tv);
675 #ifdef WITH_LIBPTH
676 n = pth_select(n, &rfds, &wfds, NULL, &tv);
677 #else
678 n = select(n, &rfds, &wfds, NULL, &tv);
679 #endif
681 if (n == -1) {
682 *rc = gpg_error_from_syserror();
683 goto fail;
685 else if (n == 0) {
686 *rc = GPG_ERR_TIMEOUT;
687 goto fail;
690 ares_process(pwm->tcp_conn->chan, &rfds, &wfds);
692 if (pwm->tcp_conn->rc)
693 break;
694 } while (pwm->cmd == ASYNC_CMD_DNS);
696 if (pwm->tcp_conn->rc) {
697 *rc = pwm->tcp_conn->rc;
698 goto fail;
701 return setup_context(pwm, rc);
703 fail:
704 pwmd_close(pwm);
705 return NULL;
708 pwm_t *pwmd_tcp_connect(const char *host, int port, const char *identity,
709 const char *user, const char *known_hosts, gpg_error_t *rc)
711 return _do_pwmd_tcp_connect(host, port, identity, user, known_hosts, rc, 0);
714 /* Must free the result with pwmd_free_result(). */
715 char *pwmd_get_hostkey(const char *host, int port, gpg_error_t *rc)
717 char *hostkey;
718 pwm_t *pwm;
720 pwm = _do_pwmd_tcp_connect(host, port, NULL, NULL, NULL, rc, 1);
722 if (!pwm)
723 return NULL;
725 hostkey = xstrdup(pwm->tcp_conn->hostkey);
727 if (!hostkey)
728 *rc = gpg_error_from_errno(ENOMEM);
730 pwmd_close(pwm);
731 return hostkey;
734 pwm_t *pwmd_get_hostkey_async(const char *host, int port, gpg_error_t *rc)
736 return _do_pwmd_tcp_connect_async(host, port, NULL, NULL, NULL, rc,
737 ASYNC_CMD_HOSTKEY);
739 #endif
741 pwm_t *pwmd_connect(const char *path, gpg_error_t *rc)
743 pwm_t *pwm = NULL;
744 char *socketpath = NULL;
745 struct passwd *pw;
746 assuan_context_t ctx;
748 if (!path) {
749 pw = getpwuid(getuid());
750 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
751 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
753 else
754 socketpath = xstrdup(path);
756 *rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
757 xfree(socketpath);
759 if (*rc)
760 return NULL;
762 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
763 *rc = gpg_error_from_syserror();
764 return NULL;
767 return _socket_connect_finalize(pwm, ctx);
770 gpg_error_t pwmd_pending_line(pwm_t *pwm, char **line, size_t *len)
772 if (!pwm)
773 return GPG_ERR_INV_ARG;
775 if (assuan_pending_line(pwm->ctx))
776 return assuan_read_line(pwm->ctx, line, len);
778 return GPG_ERR_NO_DATA;
781 void pwmd_close(pwm_t *pwm)
783 if (!pwm)
784 return;
786 if (pwm->ctx)
787 assuan_disconnect(pwm->ctx);
789 if (pwm->password)
790 xfree(pwm->password);
792 if (pwm->title)
793 xfree(pwm->title);
795 if (pwm->desc)
796 xfree(pwm->desc);
798 if (pwm->prompt)
799 xfree(pwm->prompt);
801 if (pwm->pinentry_tty)
802 xfree(pwm->pinentry_tty);
804 if (pwm->pinentry_display)
805 xfree(pwm->pinentry_display);
807 if (pwm->pinentry_term)
808 xfree(pwm->pinentry_term);
810 if (pwm->filename)
811 xfree(pwm->filename);
813 #ifdef WITH_TCP
814 if (pwm->result)
815 xfree(pwm->result);
817 if (pwm->tcp_conn)
818 free_tcp_conn(pwm->tcp_conn);
820 #endif
821 xfree(pwm);
824 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
826 membuf_t *mem = (membuf_t *)data;
827 void *p;
829 if (!buffer)
830 return 0;
832 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
833 return 1;
835 mem->buf = p;
836 memcpy((char *)mem->buf + mem->len, buffer, len);
837 mem->len += len;
838 return 0;
841 void pwmd_free_result(void *data)
843 xfree(data);
846 static int _inquire_cb(void *data, const char *keyword)
848 pwm_t *pwm = (pwm_t *)data;
849 gpg_error_t rc = 0;
850 int flags = fcntl(pwm->fd, F_GETFL);
852 /* Shouldn't get this far without a callback. */
853 if (!pwm->inquire_func)
854 return GPG_ERR_INV_ARG;
857 * Since the socket file descriptor is probably set to non-blocking, set to
858 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
859 * asynchronous INQUIRE is supported by either libassuan or a later
860 * libpwmd.
862 fcntl(pwm->fd, F_SETFL, 0);
864 for (;;) {
865 char *result = NULL;
866 size_t len;
867 gpg_error_t arc;
869 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
870 rc = gpg_err_code(rc);
872 if (rc == GPG_ERR_EOF || !rc) {
873 if (len <= 0 || !result || !*result) {
874 rc = 0;
875 break;
878 arc = assuan_send_data(pwm->ctx, result, len);
880 if (rc == GPG_ERR_EOF) {
881 rc = arc;
882 break;
885 rc = arc;
887 else if (rc)
888 break;
891 fcntl(pwm->fd, F_SETFL, flags);
892 return rc;
895 gpg_error_t pwmd_finalize(pwm_t *pwm)
897 if (!pwm || pwm->cmd == ASYNC_CMD_NONE || pwm->state != ASYNC_DONE)
898 return GPG_ERR_INV_ARG;
900 pwm->state = ASYNC_INIT;
901 pwm->cmd = ASYNC_CMD_NONE;
903 #ifdef WITH_TCP
904 if (pwm->cmd == ASYNC_CMD_CONNECT || pwm->cmd == ASYNC_CMD_DNS) {
905 gpg_error_t rc = pwm->tcp_conn->rc;
907 if (pwm->result)
908 xfree(pwm->result);
910 pwm->result = NULL;
912 /* pwm is no longer a valid handle. */
913 if (rc)
914 pwmd_close(pwm);
916 return 0;
918 #endif
920 if (pwm->fd < 0)
921 return GPG_ERR_INV_ARG;
923 pwm->ntries = 0;
924 #ifdef WITH_PINENTRY
925 pwm->is_open_cmd = 0;
926 #endif
927 return 0;
930 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
932 char *buf;
933 gpg_error_t rc;
934 size_t len = strlen(cmd) + 2;
936 len += arg ? strlen(arg) : 0;
938 if (pwm->state != ASYNC_INIT)
939 return GPG_ERR_INV_STATE;
941 buf = (char *)xmalloc(len);
943 if (!buf) {
944 rc = gpg_error_from_errno(ENOMEM);
945 goto fail;
948 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
949 rc = assuan_write_line(pwm->ctx, buf);
950 xfree(buf);
952 if (!rc)
953 pwm->state = ASYNC_PROCESS;
955 fail:
956 return rc;
959 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
961 if (!pwm || !filename)
962 return GPG_ERR_INV_ARG;
964 /* For pinentry retries. */
965 if (!pwm->is_open_cmd) {
966 if (pwm->filename)
967 xfree(pwm->filename);
969 pwm->filename = xstrdup(filename);
972 pwm->is_open_cmd = 1;
973 return do_nb_command(pwm, "OPEN", filename);
976 gpg_error_t pwmd_save_async(pwm_t *pwm)
978 if (!pwm)
979 return GPG_ERR_INV_ARG;
981 return do_nb_command(pwm, "SAVE", NULL);
984 static gpg_error_t parse_assuan_line(pwm_t *pwm)
986 gpg_error_t rc;
987 char *line;
988 size_t len;
990 rc = assuan_read_line(pwm->ctx, &line, &len);
992 if (!rc) {
993 if (line[0] == 'O' && line[1] == 'K' &&
994 (line[2] == 0 || line[2] == ' ')) {
995 pwm->state = ASYNC_DONE;
997 else if (line[0] == '#') {
999 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
1000 if (pwm->status_func) {
1001 pwm->status_func(pwm->status_data,
1002 line[1] == 0 ? line+1 : line+2);
1005 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
1006 (line[3] == 0 || line[3] == ' ')) {
1007 line += 4;
1008 rc = atoi(line);
1009 pwm->state = ASYNC_DONE;
1013 return rc;
1016 gpg_error_t pwmd_get_result(pwm_t *pwm, const char **result)
1018 if (!pwm || !result)
1019 return GPG_ERR_INV_ARG;
1021 if (pwm->state != ASYNC_DONE)
1022 return GPG_ERR_INV_STATE;
1024 if (!pwm->result)
1025 return GPG_ERR_NO_DATA;
1027 *result = pwm->result;
1028 return 0;
1031 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
1033 fd_set fds;
1034 int n;
1035 struct timeval tv = {0, 0};
1037 *rc = 0;
1039 if (!pwm) {
1040 *rc = GPG_ERR_INV_ARG;
1041 return ASYNC_DONE;
1044 #ifdef WITH_TCP
1045 if (pwm->cmd == ASYNC_CMD_DNS) {
1046 fd_set rfds, wfds;
1048 if (pwm->tcp_conn->rc) {
1049 *rc = pwm->tcp_conn->rc;
1050 close(pwm->tcp_conn->fd);
1051 pwm->state = ASYNC_DONE;
1052 return pwm->state;
1055 FD_ZERO(&rfds);
1056 FD_ZERO(&wfds);
1057 n = ares_fds(pwm->tcp_conn->chan, &rfds, &wfds);
1059 /* Shouldn't happen. */
1060 if (!n)
1061 return pwm->state;
1063 n = select(n, &rfds, &wfds, NULL, &tv);
1065 if (n > 0)
1066 ares_process(pwm->tcp_conn->chan, &rfds, &wfds);
1068 return pwm->state;
1070 else if (pwm->cmd == ASYNC_CMD_CONNECT) {
1071 if (pwm->tcp_conn->rc == GPG_ERR_EINPROGRESS) {
1072 int ret;
1073 socklen_t len = sizeof(int);
1075 FD_ZERO(&fds);
1076 FD_SET(pwm->tcp_conn->fd, &fds);
1077 n = select(pwm->tcp_conn->fd+1, NULL, &fds, NULL, &tv);
1079 if (!n || !FD_ISSET(pwm->tcp_conn->fd, &fds))
1080 return pwm->state;
1081 else if (n == -1) {
1082 *rc = gpg_error_from_syserror();
1083 close(pwm->tcp_conn->fd);
1084 pwm->state = ASYNC_DONE;
1085 return pwm->state;
1088 ret = getsockopt(pwm->tcp_conn->fd, SOL_SOCKET, SO_ERROR, &n, &len);
1089 if (ret || n) {
1090 *rc = ret ? gpg_error_from_syserror() : gpg_error_from_errno(n);
1091 close(pwm->tcp_conn->fd);
1092 pwm->state = ASYNC_DONE;
1093 return pwm->state;
1096 else if (pwm->tcp_conn->rc) {
1097 *rc = pwm->tcp_conn->rc;
1098 close(pwm->tcp_conn->fd);
1099 pwm->state = ASYNC_DONE;
1100 return pwm->state;
1103 fcntl(pwm->tcp_conn->fd, F_SETFL, 0);
1104 setup_context(pwm, rc);
1105 pwm->state = ASYNC_DONE;
1106 return pwm->state;
1108 #endif
1110 if (pwm->fd < 0) {
1111 *rc = GPG_ERR_INV_ARG;
1112 return ASYNC_DONE;
1114 else if (pwm->state == ASYNC_DONE)
1115 return pwm->state;
1116 else if (pwm->state == ASYNC_INIT) {
1117 *rc = GPG_ERR_INV_STATE;
1118 return ASYNC_DONE;
1121 FD_ZERO(&fds);
1122 FD_SET(pwm->fd, &fds);
1123 #ifdef WITH_LIBPTH
1124 n = pth_select(pwm->fd+1, &fds, NULL, NULL, &tv);
1125 #else
1126 n = select(pwm->fd+1, &fds, NULL, NULL, &tv);
1127 #endif
1129 if (n > 0) {
1130 if (FD_ISSET(pwm->fd, &fds))
1131 *rc = parse_assuan_line(pwm);
1134 while (!*rc && assuan_pending_line(pwm->ctx))
1135 *rc = parse_assuan_line(pwm);
1137 if (pwm->is_open_cmd && gpg_err_code(*rc) == EPWMD_BADKEY &&
1138 ++pwm->ntries < pwm->pinentry_tries) {
1139 pwm->state = ASYNC_INIT;
1140 *rc = pwmd_open_async(pwm, pwm->filename);
1143 return pwm->state;
1146 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
1147 char **result, const char *cmd)
1149 membuf_t data;
1150 gpg_error_t rc;
1152 data.len = 0;
1153 data.buf = NULL;
1155 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
1156 pwm->status_func, pwm->status_data);
1158 if (rc) {
1159 if (data.buf) {
1160 xfree(data.buf);
1161 data.buf = NULL;
1164 else {
1165 if (data.buf) {
1166 mem_realloc_cb(&data, "", 1);
1167 *result = (char *)data.buf;
1171 return gpg_err_code(rc);
1174 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
1175 void *data)
1177 if (!pwm || !cmd || !fn)
1178 return GPG_ERR_INV_ARG;
1180 pwm->inquire_func = fn;
1181 pwm->inquire_data = data;
1182 return assuan_command(pwm, pwm->ctx, NULL, cmd);
1185 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
1187 #ifndef WITH_PINENTRY
1188 return GPG_ERR_NOT_IMPLEMENTED;
1189 #else
1190 if (!pwm || pwm->pid == -1)
1191 return GPG_ERR_INV_ARG;
1193 if (kill(pwm->pid, 0) == 0) {
1194 if (kill(pwm->pid, SIGTERM) == -1) {
1195 if (kill(pwm->pid, SIGKILL) == -1)
1196 return gpg_error_from_errno(errno);
1199 pwm->pin_error = GPG_ERR_TIMEOUT;
1201 else
1202 return gpg_error_from_errno(errno);
1204 return 0;
1205 #endif
1208 #ifdef WITH_PINENTRY
1209 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
1211 char *buf;
1212 char tmp[ASSUAN_LINELENGTH];
1213 gpg_error_t error;
1215 if (!pwm->title)
1216 pwm->title = xstrdup(N_("LibPWMD"));
1218 if (!pwm->prompt)
1219 pwm->prompt = xstrdup(N_("Passphrase:"));
1221 if (!pwm->desc && !which)
1222 pwm->desc = xstrdup(N_("Enter a passphrase."));
1224 if (which == 1) {
1225 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid passphrase, please try again."));
1226 buf = xstrdup(tmp);
1228 else if (which == 2) {
1229 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the passphrase again for confirmation."));
1230 buf = xstrdup(tmp);
1232 else {
1233 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
1234 sprintf(buf, "SETERROR %s", pwm->desc);
1237 error = pinentry_command(pwm, NULL, buf);
1238 xfree(buf);
1240 if (error)
1241 return error;
1243 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
1244 sprintf(buf, "SETPROMPT %s", pwm->prompt);
1245 error = pinentry_command(pwm, NULL, buf);
1246 xfree(buf);
1248 if (error)
1249 return error;
1251 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
1252 sprintf(buf, "SETDESC %s", pwm->title);
1253 error = pinentry_command(pwm, NULL, buf);
1254 xfree(buf);
1255 return error;
1258 static void update_pinentry_settings(pwm_t *pwm)
1260 FILE *fp;
1261 char buf[LINE_MAX];
1262 struct passwd *pw = getpwuid(getuid());
1263 char *p;
1265 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
1267 if ((fp = fopen(buf, "r")) == NULL)
1268 return;
1270 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
1271 char name[32], val[256];
1273 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
1274 continue;
1276 if (strcasecmp(name, "TTYNAME") == 0) {
1277 xfree(pwm->pinentry_tty);
1278 pwm->pinentry_tty = xstrdup(val);
1280 else if (strcasecmp(name, "TTYTYPE") == 0) {
1281 xfree(pwm->pinentry_term);
1282 pwm->pinentry_term = xstrdup(val);
1284 else if (strcasecmp(name, "DISPLAY") == 0) {
1285 xfree(pwm->pinentry_display);
1286 pwm->pinentry_display = xstrdup(val);
1288 else if (strcasecmp(name, "PATH") == 0) {
1289 xfree(pwm->pinentry_path);
1290 pwm->pinentry_path = xstrdup(val);
1294 fclose(fp);
1297 static gpg_error_t launch_pinentry(pwm_t *pwm)
1299 int rc;
1300 assuan_context_t ctx;
1301 int child_list[] = {-1};
1302 char *display = getenv("DISPLAY");
1303 const char *argv[6];
1304 int have_display = 0;
1305 char *tty = NULL;
1307 update_pinentry_settings(pwm);
1309 if (pwm->pinentry_display || display)
1310 have_display = 1;
1311 else {
1312 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
1314 if (!tty)
1315 return gpg_error_from_errno(errno);
1318 if (!have_display && !tty)
1319 return GPG_ERR_ENOTTY;
1321 argv[0] = "pinentry";
1322 argv[1] = have_display ? "--display" : "--ttyname";
1323 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
1324 argv[3] = NULL;
1326 if (!have_display) {
1327 argv[3] = "--ttytype";
1328 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
1329 argv[5] = NULL;
1332 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
1334 if (rc)
1335 return rc;
1337 pwm->pid = assuan_get_pid(ctx);
1338 pwm->pctx = ctx;
1339 return set_pinentry_strings(pwm, 0);
1342 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
1344 gpg_error_t n;
1346 if (!pwm->pctx) {
1347 n = launch_pinentry(pwm);
1349 if (n)
1350 return n;
1353 return assuan_command(pwm, pwm->pctx, result, cmd);
1356 static void pinentry_disconnect(pwm_t *pwm)
1358 if (pwm->pctx)
1359 assuan_disconnect(pwm->pctx);
1361 pwm->pctx = NULL;
1362 pwm->pid = -1;
1366 * Only called from a child process.
1368 static void catchsig(int sig)
1370 switch (sig) {
1371 case SIGALRM:
1372 if (gelapsed++ >= gtimeout) {
1373 global_error = pwmd_terminate_pinentry(gpwm);
1375 if (!global_error)
1376 global_error = GPG_ERR_TIMEOUT;
1378 break;
1381 alarm(1);
1382 break;
1383 default:
1384 break;
1389 * Borrowed from libassuan.
1391 static char *percent_escape(const char *atext)
1393 const unsigned char *s;
1394 int len = strlen(atext) * 3 + 1;
1395 char *buf = (char *)xmalloc(len), *p = buf;
1397 if (!buf)
1398 return NULL;
1400 for (s=(const unsigned char *)atext; *s; s++) {
1401 if (*s < ' ') {
1402 sprintf (p, "%%%02X", *s);
1403 p += 3;
1405 else
1406 *p++ = *s;
1409 *p = 0;
1410 return buf;
1412 #endif
1414 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
1416 if (!cmd)
1417 return GPG_ERR_INV_ARG;
1419 return assuan_command(pwm, pwm->ctx, result, cmd);
1422 gpg_error_t pwmd_command_ap(pwm_t *pwm, char **result, const char *cmd,
1423 va_list ap)
1425 char *buf;
1426 size_t len;
1427 gpg_error_t error;
1429 if (!pwm || !cmd)
1430 return GPG_ERR_INV_ARG;
1433 * C99 allows the dst pointer to be null which will calculate the length
1434 * of the would-be result and return it.
1436 len = vsnprintf(NULL, 0, cmd, ap)+1;
1437 buf = (char *)xmalloc(len);
1438 len = vsnprintf(buf, len, cmd, ap);
1439 error = send_command(pwm, result, buf);
1440 xfree(buf);
1441 return error;
1445 * Avoid sending the BYE command here. libassuan will close the file
1446 * descriptor and release the assuan context. Use pwmd_close() instead.
1448 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
1450 va_list ap;
1451 gpg_error_t error;
1453 if (!pwm || !cmd)
1454 return GPG_ERR_INV_ARG;
1456 *result = NULL;
1457 va_start(ap, cmd);
1458 error = pwmd_command_ap(pwm, result, cmd, ap);
1459 va_end(ap);
1460 return error;
1463 #ifdef WITH_PINENTRY
1464 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
1466 if (gtimeout) {
1467 signal(SIGALRM, catchsig);
1468 alarm(1);
1471 *result = NULL;
1472 return pinentry_command(pwm, result, "GETPIN");
1475 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
1477 int pin_try = *try_n;
1478 gpg_error_t error;
1480 getpin_again:
1481 *try_n = pin_try;
1483 if (pin_try == -1) {
1484 error = set_pinentry_strings(pwm, which);
1486 if (error) {
1487 pinentry_disconnect(pwm);
1488 return error;
1491 else {
1492 if (pwm->pinentry_tries-1 != pin_try) {
1493 error = set_pinentry_strings(pwm, 1);
1495 if (error) {
1496 pinentry_disconnect(pwm);
1497 return error;
1502 error = do_getpin(pwm, result);
1505 * Since there was input cancel any timeout setting.
1507 alarm(0);
1509 if (error) {
1510 if (error == GPG_ERR_CANCELED)
1511 return GPG_ERR_CANCELED;
1513 if (pin_try != -1 && pin_try--)
1514 goto getpin_again;
1516 if (pwm->pctx)
1517 pinentry_disconnect(pwm);
1519 *try_n = pin_try;
1520 return error;
1523 return 0;
1525 #endif
1527 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1529 gpg_error_t error;
1531 #ifndef WITH_PINENTRY
1532 return GPG_ERR_NOT_IMPLEMENTED;
1533 #endif
1535 if (!pwm || !pw || !pw->filename[0])
1536 return GPG_ERR_INV_ARG;
1538 close(pw->fd);
1540 if (pw->error) {
1541 error = pw->error;
1542 goto fail;
1545 if (pwm->filename)
1546 xfree(pwm->filename);
1548 pwm->filename = xstrdup(pw->filename);
1549 memset(pw, 0, sizeof(pwmd_nb_status_t));
1550 return 0;
1552 fail:
1553 memset(pw, 0, sizeof(pwmd_nb_status_t));
1554 return error;
1557 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
1559 char buf[ASSUAN_LINELENGTH];
1560 gpg_error_t error;
1561 char *result = NULL;
1563 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
1564 error = send_command(pwm, &result, buf);
1565 memset(buf, 0, sizeof(buf));
1567 if (error && result)
1568 xfree(result);
1570 return error;
1573 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
1574 int nb, int timeout)
1576 char *result = NULL;
1577 char *password = NULL;
1578 char path[PATH_MAX];
1579 #ifdef WITH_PINENTRY
1580 int pin_try;
1581 #endif
1583 if (!pwm || !filename || !*filename) {
1584 *error = GPG_ERR_INV_ARG;
1585 return nb ? -1 : 1;
1588 #ifdef WITH_PINENTRY
1589 pin_try = pwm->pinentry_tries - 1;
1590 #endif
1593 * Avoid calling pinentry if the password is cached on the server or if
1594 * this is a new file.
1596 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
1598 if (*error)
1599 return nb ? -1 : 1;
1601 snprintf(path, sizeof(path), "%s/%s", result, filename);
1602 pwmd_free_result(result);
1604 if (access(path, R_OK) == -1) {
1605 if (errno == ENOENT)
1606 goto gotpassword;
1609 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
1611 if (*error == EPWMD_CACHE_NOT_FOUND) {
1612 if (pwm->passfunc) {
1613 password = pwm->passfunc(pwm, pwm->passdata);
1614 goto gotpassword;
1617 #ifdef WITH_PINENTRY
1619 * Get the password from pinentry.
1621 if (pwm->use_pinentry) {
1623 * Nonblocking is wanted. fork() then return a file descriptor
1624 * that the client can use to read() from.
1626 if (nb) {
1627 int p[2];
1628 pid_t pid;
1629 pwmd_nb_status_t pw;
1631 if (pipe(p) == -1) {
1632 *error = gpg_error_from_syserror();
1633 return -1;
1636 #ifdef WITH_LIBPTH
1637 pid = pth_fork();
1638 #else
1639 pid = fork();
1640 #endif
1642 switch (pid) {
1643 case 0:
1644 close(p[0]);
1645 strncpy(pw.filename, filename, sizeof(pw.filename));
1646 pw.filename[sizeof(pw.filename)-1] = 0;
1647 pw.fd = p[0];
1649 if (timeout > 0) {
1650 gpwm = pwm;
1651 gtimeout = timeout;
1652 gelapsed = 0;
1655 getpin_nb_again:
1656 *error = getpin(pwm, &password, &pin_try, 0);
1658 if (*error) {
1659 getpin_nb_fail:
1660 if (pwm->pctx)
1661 pinentry_disconnect(pwm);
1663 if (gtimeout && gelapsed >= gtimeout)
1664 *error = GPG_ERR_TIMEOUT;
1666 pw.error = *error;
1667 #ifdef WITH_LIBPTH
1668 pth_write(p[1], &pw, sizeof(pw));
1669 #else
1670 write(p[1], &pw, sizeof(pw));
1671 #endif
1672 close(p[1]);
1673 _exit(1);
1677 * Don't count the time it takes to open the file
1678 * which may have many iterations.
1680 signal(SIGALRM, SIG_DFL);
1681 *error = do_open_command(pwm, filename, password);
1683 if (timeout)
1684 signal(SIGALRM, catchsig);
1686 if (pwm->pctx && *error == EPWMD_BADKEY) {
1687 if (pin_try-- > 0)
1688 goto getpin_nb_again;
1690 goto getpin_nb_fail;
1693 pinentry_disconnect(pwm);
1694 pw.error = 0;
1695 #ifdef WITH_LIBPTH
1696 pth_write(p[1], &pw, sizeof(pw));
1697 #else
1698 write(p[1], &pw, sizeof(pw));
1699 #endif
1700 close(p[1]);
1701 _exit(0);
1702 break;
1703 case -1:
1704 *error = gpg_error_from_syserror();
1705 close(p[0]);
1706 close(p[1]);
1707 return -1;
1708 default:
1709 break;
1712 close(p[1]);
1713 *error = 0;
1714 return p[0];
1717 else {
1718 #endif
1720 * Not using pinentry and the file was not found
1721 * in the cache.
1723 password = pwm->password;
1724 #ifdef WITH_PINENTRY
1726 #endif
1728 else if (*error)
1729 return nb ? -1 : 1;
1731 gotpassword:
1732 *error = do_open_command(pwm, filename, password);
1735 * Keep the user defined password set with pwmd_setopt(). The password may
1736 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1738 if (!pwm->passfunc && password && password != pwm->password)
1739 xfree(password);
1741 #ifdef WITH_PINENTRY
1742 if (*error == EPWMD_BADKEY) {
1743 if (pin_try-- > 0 && !nb) {
1744 *error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
1745 N_("Invalid passphrase, please try again."));
1747 if (*error)
1748 return 1;
1750 goto gotpassword;
1753 if (nb)
1754 pinentry_disconnect(pwm);
1756 return nb ? -1 : 1;
1758 #endif
1760 if (!*error) {
1761 if (pwm->filename)
1762 xfree(pwm->filename);
1764 pwm->filename = xstrdup(filename);
1768 * The file is cached or the file is a new file.
1770 if (nb)
1771 return *error ? -1 : -2;
1773 return *error ? 1 : 0;
1776 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1778 gpg_error_t error;
1780 do_pwmd_open(pwm, &error, filename, 0, 0);
1781 return error;
1784 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1785 int timeout)
1787 #ifndef WITH_PINENTRY
1788 *error = GPG_ERR_NOT_IMPLEMENTED;
1789 return -1;
1790 #else
1791 return do_pwmd_open(pwm, error, filename, 1, timeout);
1792 #endif
1795 #ifdef WITH_PINENTRY
1796 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1798 int confirm = 0;
1799 gpg_error_t error;
1800 char *result = NULL;
1801 int pin_try = -1;
1803 again:
1804 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1806 if (error) {
1807 if (pwm->pctx)
1808 pinentry_disconnect(pwm);
1810 if (*password)
1811 xfree(*password);
1813 return error;
1816 if (!confirm++) {
1817 *password = result;
1818 goto again;
1821 if (strcmp(*password, result)) {
1822 xfree(*password);
1823 xfree(result);
1824 pinentry_disconnect(pwm);
1825 error = EPWMD_BADKEY;
1826 return error;
1829 xfree(result);
1830 pinentry_disconnect(pwm);
1831 return 0;
1833 #endif
1835 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1837 char buf[ASSUAN_LINELENGTH];
1838 gpg_error_t error;
1839 char *result = NULL;
1841 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1842 error = send_command(pwm, &result, buf);
1843 memset(&buf, 0, sizeof(buf));
1845 if (error && result)
1846 xfree(result);
1848 return error;
1851 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1853 gpg_error_t rc;
1855 #ifndef WITH_PINENTRY
1856 return GPG_ERR_NOT_IMPLEMENTED;
1857 #endif
1859 if (!pwm || !pw || !pw->filename[0])
1860 return GPG_ERR_INV_ARG;
1862 close(pw->fd);
1863 rc = pw->error;
1864 memset(pw, 0, sizeof(pwmd_nb_status_t));
1865 return rc;
1868 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1870 char *result = NULL;
1871 char *password = NULL;
1873 if (!pwm) {
1874 *error = GPG_ERR_INV_ARG;
1875 return nb ? -1 : 1;
1878 if (pwm->use_pinentry || pwm->passfunc) {
1879 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1881 if (*error == EPWMD_CACHE_NOT_FOUND) {
1882 #ifdef WITH_PINENTRY
1883 if (pwm->use_pinentry) {
1884 if (nb) {
1885 int p[2];
1886 pid_t pid;
1887 pwmd_nb_status_t pw;
1889 if (pipe(p) == -1) {
1890 *error = gpg_error_from_syserror();
1891 return -1;
1894 #ifdef WITH_LIBPTH
1895 pid = pth_fork();
1896 #else
1897 pid = fork();
1898 #endif
1900 switch (pid) {
1901 case 0:
1902 close(p[0]);
1903 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1904 pw.filename[sizeof(pw.filename)-1] = 0;
1905 pw.fd = p[0];
1907 do {
1908 password = NULL;
1909 *error = do_save_getpin(pwm, &password);
1910 } while (*error == EPWMD_BADKEY);
1912 if (*error) {
1913 if (pwm->pctx)
1914 pinentry_disconnect(pwm);
1916 pw.error = *error;
1917 #ifdef WITH_LIBPTH
1918 pth_write(p[1], &pw, sizeof(pw));
1919 #else
1920 write(p[1], &pw, sizeof(pw));
1921 #endif
1922 close(p[1]);
1923 _exit(1);
1926 *error = do_save_command(pwm, password);
1927 pinentry_disconnect(pwm);
1928 pw.error = *error;
1929 #ifdef WITH_LIBPTH
1930 pth_write(p[1], &pw, sizeof(pw));
1931 #else
1932 write(p[1], &pw, sizeof(pw));
1933 #endif
1934 close(p[1]);
1935 _exit(0);
1936 break;
1937 case -1:
1938 *error = gpg_error_from_syserror();
1939 close(p[0]);
1940 close(p[1]);
1941 return -1;
1942 default:
1943 break;
1946 close(p[1]);
1947 *error = 0;
1948 return p[0];
1951 *error = do_save_getpin(pwm, &password);
1953 if (*error)
1954 return 1;
1956 else {
1957 #endif
1958 if (pwm->passfunc)
1959 password = (*pwm->passfunc)(pwm, pwm->passdata);
1960 #ifdef WITH_PINENTRY
1962 #endif
1964 else {
1965 if (*error)
1966 return nb ? -1 : 1;
1969 else
1970 password = pwm->password;
1972 *error = do_save_command(pwm, password);
1974 if (password && password != pwm->password)
1975 xfree(password);
1977 if (nb)
1978 return *error ? -1 : -2;
1980 return *error ? 1 : 0;
1983 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1985 #ifndef WITH_PINENTRY
1986 *error = GPG_ERR_NOT_IMPLEMENTED;
1987 return -1;
1988 #else
1989 return do_pwmd_save(pwm, error, 1);
1990 #endif
1993 gpg_error_t pwmd_save(pwm_t *pwm)
1995 gpg_error_t error;
1997 do_pwmd_save(pwm, &error, 0);
1998 return error;
2001 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
2003 va_list ap;
2004 #ifdef WITH_PINENTRY
2005 int n = va_arg(ap, int);
2006 char *result;
2007 #endif
2008 char *arg1;
2009 gpg_error_t error = 0;
2011 if (!pwm)
2012 return GPG_ERR_INV_ARG;
2014 va_start(ap, opt);
2016 switch (opt) {
2017 case PWMD_OPTION_STATUS_FUNC:
2018 pwm->status_func = va_arg(ap, pwmd_status_fn);
2019 break;
2020 case PWMD_OPTION_STATUS_DATA:
2021 pwm->status_data = va_arg(ap, void *);
2022 break;
2023 case PWMD_OPTION_PASSWORD_FUNC:
2024 pwm->passfunc = va_arg(ap, pwmd_password_fn);
2025 break;
2026 case PWMD_OPTION_PASSWORD_DATA:
2027 pwm->passdata = va_arg(ap, void *);
2028 break;
2029 case PWMD_OPTION_PASSWORD:
2030 arg1 = va_arg(ap, char *);
2032 if (pwm->password)
2033 xfree(pwm->password);
2035 pwm->password = xstrdup(arg1);
2036 break;
2037 #ifdef WITH_PINENTRY
2038 case PWMD_OPTION_PINENTRY:
2039 n = va_arg(ap, int);
2041 if (n != 0 && n != 1) {
2042 va_end(ap);
2043 error = GPG_ERR_INV_VALUE;
2045 else {
2046 pwm->use_pinentry = n;
2047 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
2048 !pwm->use_pinentry);
2050 break;
2051 case PWMD_OPTION_PINENTRY_TRIES:
2052 n = va_arg(ap, int);
2054 if (n <= 0) {
2055 va_end(ap);
2056 error = GPG_ERR_INV_VALUE;
2058 else
2059 pwm->pinentry_tries = n;
2060 break;
2061 case PWMD_OPTION_PINENTRY_PATH:
2062 if (pwm->pinentry_path)
2063 xfree(pwm->pinentry_path);
2065 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
2066 break;
2067 case PWMD_OPTION_PINENTRY_TTY:
2068 if (pwm->pinentry_tty)
2069 xfree(pwm->pinentry_tty);
2071 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
2072 break;
2073 case PWMD_OPTION_PINENTRY_DISPLAY:
2074 if (pwm->pinentry_display)
2075 xfree(pwm->pinentry_display);
2077 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
2078 break;
2079 case PWMD_OPTION_PINENTRY_TERM:
2080 if (pwm->pinentry_term)
2081 xfree(pwm->pinentry_term);
2083 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
2084 break;
2085 case PWMD_OPTION_PINENTRY_TITLE:
2086 if (pwm->title)
2087 xfree(pwm->title);
2088 pwm->title = percent_escape(va_arg(ap, char *));
2089 break;
2090 case PWMD_OPTION_PINENTRY_PROMPT:
2091 if (pwm->prompt)
2092 xfree(pwm->prompt);
2093 pwm->prompt = percent_escape(va_arg(ap, char *));
2094 break;
2095 case PWMD_OPTION_PINENTRY_DESC:
2096 if (pwm->desc)
2097 xfree(pwm->desc);
2098 pwm->desc = percent_escape(va_arg(ap, char *));
2099 break;
2100 #else
2101 case PWMD_OPTION_PINENTRY:
2102 case PWMD_OPTION_PINENTRY_TRIES:
2103 case PWMD_OPTION_PINENTRY_PATH:
2104 case PWMD_OPTION_PINENTRY_TTY:
2105 case PWMD_OPTION_PINENTRY_DISPLAY:
2106 case PWMD_OPTION_PINENTRY_TERM:
2107 case PWMD_OPTION_PINENTRY_TITLE:
2108 case PWMD_OPTION_PINENTRY_PROMPT:
2109 case PWMD_OPTION_PINENTRY_DESC:
2110 error = GPG_ERR_NOT_IMPLEMENTED;
2111 break;
2112 #endif
2113 default:
2114 error = GPG_ERR_NOT_IMPLEMENTED;
2115 break;
2118 va_end(ap);
2119 return error;
2123 * Prevent requiring assuan.h when setting ctx. The ctx is really an
2124 * assuan_context_t *.
2126 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, void *ctx, int *fd)
2128 if (!pwm)
2129 return GPG_ERR_INV_ARG;
2131 ctx = pwm->ctx;
2132 *fd = pwm->fd;
2133 return 0;