I messed up the directory tree making it hard to pull changes from
[libpwmd.git] / libpwmd.c
blobbadb5124be2e5af4a13a9adcebfef04ed85cb8e0
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2008 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 #ifndef MEM_DEBUG
73 #include "mem.h"
74 #else
75 #define xfree free
76 #define xrealloc realloc
77 #define xmalloc malloc
78 #define xstrdup strdup
79 #define xcalloc calloc
80 #endif
82 #include "types.h"
84 #ifdef WITH_PINENTRY
85 static pwm_t *gpwm;
86 static int gelapsed, gtimeout;
87 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd);
88 static gpg_error_t global_error;
89 #endif
91 const char *pwmd_strerror(gpg_error_t e)
93 gpg_err_code_t code = gpg_err_code(e);
95 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
96 switch (code) {
97 case GPG_ERR_USER_1:
98 default:
99 return N_("Unknown error");
100 case GPG_ERR_USER_2:
101 return N_("No cache slots available");
102 case GPG_ERR_USER_3:
103 return N_("Recursion loop");
104 case GPG_ERR_USER_4:
105 return N_("No file is open");
106 case GPG_ERR_USER_5:
107 return N_("General LibXML error");
108 case GPG_ERR_USER_6:
109 return N_("File modified");
110 case GPG_ERR_USER_7:
111 return N_("Access denied");
115 return gpg_strerror(e);
118 gpg_error_t pwmd_init()
120 static int initialized;
122 if (initialized)
123 return 0;
125 #ifdef WITH_TCP
126 #ifdef WITH_LIBPTH
127 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pth);
128 #endif
129 #endif
130 #ifdef ENABLE_NLS
131 bindtextdomain("libpwmd", LOCALEDIR);
132 #endif
133 gpg_err_init();
134 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
135 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
136 initialized = 1;
137 return 0;
140 static pwm_t *_socket_connect_finalize(pwm_t *pwm, assuan_context_t ctx)
142 int active[2];
143 int n = assuan_get_active_fds(ctx, 0, active, sizeof(active));
145 pwm->fd = n <= 0 ? -1 : dup(active[0]);
146 pwm->ctx = ctx;
147 #ifdef WITH_PINENTRY
148 pwm->pid = -1;
149 pwm->pinentry_tries = 3;
150 #endif
151 assuan_set_pointer(ctx, pwm);
152 return pwm;
155 #ifdef WITH_TCP
156 static int read_hook(assuan_context_t ctx, assuan_fd_t fd, void *data,
157 size_t len, ssize_t *ret)
159 pwm_t *pwm = assuan_get_pointer(ctx);
161 if (!pwm || !pwm->tcp_conn)
162 *ret = read((int)fd, data, len);
163 else {
164 do {
165 *ret = libssh2_channel_read(pwm->tcp_conn->channel, data, len);
166 } while (*ret == LIBSSH2_ERROR_EAGAIN);
169 return *ret <= 0 ? 0 : 1;
172 static int write_hook(assuan_context_t ctx, assuan_fd_t fd, const void *data,
173 size_t len, ssize_t *ret)
175 pwm_t *pwm = assuan_get_pointer(ctx);
177 if (!pwm || !pwm->tcp_conn)
178 *ret = write((int)fd, data, len);
179 else {
180 do {
181 *ret = libssh2_channel_write(pwm->tcp_conn->channel, data, len);
182 } while (*ret == LIBSSH2_ERROR_EAGAIN);
185 return *ret <= 0 ? 0 : 1;
188 static void _ssh_deinit(pwmd_tcp_conn_t *conn);
189 static void free_tcp_conn(pwmd_tcp_conn_t *conn)
191 if (!conn)
192 return;
194 if (conn->username) {
195 xfree(conn->username);
196 conn->username = NULL;
199 if (conn->known_hosts) {
200 xfree(conn->known_hosts);
201 conn->known_hosts = NULL;
204 if (conn->identity) {
205 xfree(conn->identity);
206 conn->identity = NULL;
209 if (conn->identity_pub) {
210 xfree(conn->identity_pub);
211 conn->identity_pub = NULL;
214 if (conn->host) {
215 xfree(conn->host);
216 conn->host = NULL;
219 if (conn->hostkey) {
220 xfree(conn->hostkey);
221 conn->hostkey = NULL;
224 if (conn->chan) {
225 ares_destroy(conn->chan);
226 conn->chan = NULL;
229 if (conn->he) {
230 ares_free_hostent(conn->he);
231 conn->he = NULL;
234 if (conn->fd >= 0) {
235 close(conn->fd);
236 conn->fd = -1;
239 if (conn->session)
240 _ssh_deinit(conn);
241 else
242 xfree(conn);
245 static void _ssh_deinit(pwmd_tcp_conn_t *conn)
247 if (!conn)
248 return;
250 if (conn->channel)
251 libssh2_channel_free(conn->channel);
253 if (conn->session) {
254 libssh2_session_disconnect(conn->session, "Bye!");
255 libssh2_session_free(conn->session);
258 conn->session = NULL;
259 conn->channel = NULL;
260 free_tcp_conn(conn);
263 static void _ssh_assuan_deinit(assuan_context_t ctx)
265 pwm_t *pwm = assuan_get_pointer(ctx);
267 _ssh_deinit(pwm->tcp_conn);
271 * Sets common options from both pwmd_tcp_connect() and
272 * pwmd_tcp_connect_async().
274 static gpg_error_t init_tcp_conn(pwmd_tcp_conn_t **dst, const char *host,
275 int port, const char *identity, const char *user, const char *hosts,
276 int get)
278 pwmd_tcp_conn_t *conn;
279 gpg_error_t rc = 0;
281 if (get) {
282 if (!host)
283 return GPG_ERR_INV_ARG;
285 else {
286 if (!host || !identity || !user || !hosts)
287 return GPG_ERR_INV_ARG;
290 conn = xcalloc(1, sizeof(pwmd_tcp_conn_t));
292 if (!conn)
293 return gpg_error_from_errno(ENOMEM);
295 conn->port = port == -1 ? 22 : port;
296 conn->host = xstrdup(host);
298 if (!conn->host) {
299 rc = gpg_error_from_errno(ENOMEM);
300 goto fail;
303 if (!get) {
304 conn->identity = xstrdup(identity);
306 if (!conn->identity) {
307 rc = gpg_error_from_errno(ENOMEM);
308 goto fail;
311 conn->identity_pub = xmalloc(strlen(conn->identity)+5);
313 if (!conn->identity_pub) {
314 rc = gpg_error_from_errno(ENOMEM);
315 goto fail;
318 sprintf(conn->identity_pub, "%s.pub", conn->identity);
319 conn->username = xstrdup(user);
321 if (!conn->username) {
322 rc = gpg_error_from_errno(ENOMEM);
323 goto fail;
326 conn->known_hosts = xstrdup(hosts);
328 if (!conn->known_hosts) {
329 rc = gpg_error_from_errno(ENOMEM);
330 goto fail;
334 *dst = conn;
335 return 0;
337 fail:
338 free_tcp_conn(conn);
339 return rc;
342 static gpg_error_t do_connect(pwm_t *pwm, int prot, void *addr)
344 struct sockaddr_in their_addr;
346 pwm->tcp_conn->fd = socket(prot, SOCK_STREAM, 0);
348 if (pwm->tcp_conn->fd == -1)
349 return gpg_error_from_syserror();
351 if (pwm->tcp_conn->async)
352 fcntl(pwm->tcp_conn->fd, F_SETFL, O_NONBLOCK);
354 pwm->cmd = ASYNC_CMD_CONNECT;
355 their_addr.sin_family = prot;
356 their_addr.sin_port = htons(pwm->tcp_conn->port);
357 their_addr.sin_addr = *((struct in_addr *)addr);
358 memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
360 #ifdef WITH_LIBPTH
361 if (pth_connect(pwm->tcp_conn->fd, (struct sockaddr *)&their_addr,
362 sizeof(their_addr)) == -1)
363 #else
364 if (connect(pwm->tcp_conn->fd, (struct sockaddr *)&their_addr,
365 sizeof(their_addr)) == -1)
366 #endif
367 return gpg_error_from_syserror();
369 return 0;
372 static gpg_error_t ares_error_to_pwmd(int status)
374 if (status != ARES_SUCCESS)
375 warnx("%s", ares_strerror(status));
377 switch (status) {
378 case ARES_ENODATA:
379 case ARES_EFORMERR:
380 case ARES_ENOTFOUND:
381 return GPG_ERR_UNKNOWN_HOST;
382 case ARES_ESERVFAIL:
383 return GPG_ERR_EHOSTDOWN;
384 case ARES_ETIMEOUT:
385 return GPG_ERR_TIMEOUT;
386 case ARES_ENOMEM:
387 return gpg_error_from_errno(ENOMEM);
388 case ARES_ECONNREFUSED:
389 return GPG_ERR_ECONNREFUSED;
390 default:
391 /* FIXME ??? */
392 return GPG_ERR_EHOSTUNREACH;
395 return ARES_SUCCESS;
398 static void dns_resolve_cb(void *arg, int status, int timeouts,
399 unsigned char *abuf, int alen)
401 pwm_t *pwm = arg;
402 int rc;
403 struct hostent *he;
405 if (status == ARES_EDESTRUCTION)
406 return;
408 if (status != ARES_SUCCESS) {
409 pwm->tcp_conn->rc = ares_error_to_pwmd(status);
410 return;
413 //FIXME localhost. works with ipv4. maybe local system config error
414 /* Check for an IPv6 address first. */
415 rc = ares_parse_a_reply(abuf, alen, &he, NULL, NULL);
417 if (rc != ARES_SUCCESS) {
418 if (rc != ARES_ENODATA) {
419 pwm->tcp_conn->rc = ares_error_to_pwmd(status);
420 return;
423 rc = ares_parse_aaaa_reply(abuf, alen, &he, NULL, NULL);
425 if (rc != ARES_SUCCESS) {
426 pwm->tcp_conn->rc = ares_error_to_pwmd(status);
427 return;
431 pwm->tcp_conn->he = he;
432 pwm->tcp_conn->rc = do_connect(pwm, he->h_addrtype, he->h_addr);
435 pwm_t *_do_pwmd_tcp_connect_async(const char *host, int port,
436 const char *identity, const char *user, const char *known_hosts,
437 gpg_error_t *rc, pwmd_async_cmd_t which)
439 pwmd_tcp_conn_t *conn;
440 pwm_t *pwm;
442 *rc = init_tcp_conn(&conn, host, port, identity, user, known_hosts,
443 which == ASYNC_CMD_HOSTKEY ? 1 : 0);
445 if (*rc)
446 return NULL;
448 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
449 *rc = gpg_error_from_syserror();
450 free_tcp_conn(conn);
451 return NULL;
454 conn->async = 1;
455 pwm->tcp_conn = conn;
456 pwm->tcp_conn->cmd = which;
458 if (pwm->tcp_conn->cmd == ASYNC_CMD_HOSTKEY)
459 pwm->tcp_conn->get_only = 1;
461 pwm->cmd = ASYNC_CMD_DNS;
462 pwm->state = ASYNC_PROCESS;
463 ares_init(&pwm->tcp_conn->chan);
464 ares_query(pwm->tcp_conn->chan, pwm->tcp_conn->host, ns_c_any, ns_t_any,
465 dns_resolve_cb, pwm);
466 return pwm;
469 pwm_t *pwmd_tcp_connect_async(const char *host, int port, const char *identity,
470 const char *user, const char *known_hosts, gpg_error_t *rc)
472 return _do_pwmd_tcp_connect_async(host, port, identity, user, known_hosts,
473 rc, ASYNC_CMD_CONNECT);
476 void *_ssh_malloc(size_t size, void **data)
478 return xmalloc(size);
481 void _ssh_free(void *ptr, void **data)
483 xfree(ptr);
486 void *_ssh_realloc(void *ptr, size_t size, void **data)
488 return xrealloc(ptr, size);
491 static char *to_hex(const char *str, size_t slen)
493 int i;
494 char *buf = xmalloc(slen*2+1);
496 if (!buf)
497 return NULL;
499 for (i = 0, buf[0] = 0; i < slen; i++) {
500 char tmp[3];
502 sprintf(tmp, "%02x", (unsigned char)str[i]);
503 strcat(buf, tmp);
506 return buf;
509 static int verify_host_key(pwm_t *pwm)
511 FILE *fp = fopen(pwm->tcp_conn->known_hosts, "r");
512 char *buf, *p;
514 if (!fp)
515 return 1;
517 buf = xmalloc(LINE_MAX);
519 if (!buf)
520 goto fail;
522 while ((p = fgets(buf, LINE_MAX, fp))) {
523 if (*p == '#' || isspace(*p))
524 continue;
526 if (p[strlen(p)-1] == '\n')
527 p[strlen(p)-1] = 0;
529 if (!strcmp(buf, pwm->tcp_conn->hostkey))
530 goto done;
533 fail:
534 if (buf)
535 xfree(buf);
537 fclose(fp);
538 return 1;
540 done:
541 xfree(buf);
542 fclose(fp);
543 return 0;
546 static gpg_error_t authenticate_ssh(pwm_t *pwm)
548 const char *fp = libssh2_hostkey_hash(pwm->tcp_conn->session,
549 LIBSSH2_HOSTKEY_HASH_SHA1);
550 char *userauth;
552 pwm->tcp_conn->hostkey = to_hex(fp, 20);
554 if (!pwm->tcp_conn->hostkey)
555 return gpg_error_from_errno(ENOMEM);
557 if (pwm->tcp_conn->get_only)
558 return 0;
560 if (!fp || verify_host_key(pwm))
561 return GPG_ERR_CHECKSUM;
563 userauth = libssh2_userauth_list(pwm->tcp_conn->session,
564 pwm->tcp_conn->username, strlen(pwm->tcp_conn->username));
566 if (!userauth || !strstr(userauth, "publickey"))
567 return GPG_ERR_BAD_PIN_METHOD;
569 if (libssh2_userauth_publickey_fromfile(pwm->tcp_conn->session,
570 pwm->tcp_conn->username, pwm->tcp_conn->identity_pub,
571 pwm->tcp_conn->identity, NULL))
572 return GPG_ERR_BAD_SECKEY;
574 return 0;
577 static pwm_t *setup_context(pwm_t *pwm, gpg_error_t *rc)
579 assuan_context_t ctx;
580 struct assuan_io_hooks io_hooks = {read_hook, write_hook};
582 pwm->tcp_conn->session = libssh2_session_init_ex(_ssh_malloc, _ssh_free,
583 _ssh_realloc, NULL);
585 if (!pwm->tcp_conn->session) {
586 *rc = gpg_error_from_errno(ENOMEM);
587 goto fail;
590 if (libssh2_session_startup(pwm->tcp_conn->session, pwm->tcp_conn->fd)) {
591 *rc = GPG_ERR_ASSUAN_SERVER_FAULT;
592 goto fail;
595 *rc = authenticate_ssh(pwm);
597 if (*rc)
598 goto fail;
600 /* pwmd_get_hostkey(). */
601 if (pwm->tcp_conn->get_only) {
602 pwm->result = xstrdup(pwm->tcp_conn->hostkey);
604 if (!pwm->result) {
605 *rc = gpg_error_from_errno(ENOMEM);
606 goto fail;
609 return pwm;
612 pwm->tcp_conn->channel = libssh2_channel_open_session(pwm->tcp_conn->session);
614 if (!pwm->tcp_conn->channel) {
615 *rc = GPG_ERR_ASSUAN_SERVER_FAULT;
616 goto fail;
619 if (libssh2_channel_shell(pwm->tcp_conn->channel)) {
620 *rc = GPG_ERR_ASSUAN_SERVER_FAULT;
621 goto fail;
624 assuan_set_io_hooks(&io_hooks);
625 *rc = assuan_socket_connect_fd(&ctx, pwm->tcp_conn->fd, 0, pwm);
627 if (*rc)
628 goto fail;
630 assuan_set_finish_handler(ctx, _ssh_assuan_deinit);
631 return _socket_connect_finalize(pwm, ctx);
633 fail:
634 if (!pwm->tcp_conn->async)
635 pwmd_close(pwm);
637 return NULL;
640 static pwm_t *_do_pwmd_tcp_connect(const char *host, int port,
641 const char *identity, const char *user, const char *known_hosts,
642 gpg_error_t *rc, int get)
644 pwm_t *pwm = NULL;
645 pwmd_tcp_conn_t *conn;
647 *rc = init_tcp_conn(&conn, host, port, identity, user, known_hosts, get);
649 if (*rc)
650 return NULL;
652 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
653 *rc = gpg_error_from_errno(ENOMEM);
654 goto fail;
657 pwm->tcp_conn = conn;
658 pwm->tcp_conn->get_only = get;
659 pwm->cmd = ASYNC_CMD_DNS;
660 ares_init(&pwm->tcp_conn->chan);
661 ares_query(pwm->tcp_conn->chan, pwm->tcp_conn->host, ns_c_any, ns_t_any,
662 dns_resolve_cb, pwm);
664 /* dns_resolve_cb() may have already been called. */
665 if (pwm->tcp_conn->rc) {
666 *rc = pwm->tcp_conn->rc;
667 goto fail;
671 * Fake a blocking DNS lookup. libcares does a better job than
672 * getaddrinfo().
674 do {
675 fd_set rfds, wfds;
676 int n;
677 struct timeval tv;
679 FD_ZERO(&rfds);
680 FD_ZERO(&wfds);
681 n = ares_fds(pwm->tcp_conn->chan, &rfds, &wfds);
682 ares_timeout(pwm->tcp_conn->chan, NULL, &tv);
683 #ifdef WITH_LIBPTH
684 n = pth_select(n, &rfds, &wfds, NULL, &tv);
685 #else
686 n = select(n, &rfds, &wfds, NULL, &tv);
687 #endif
689 if (n == -1) {
690 *rc = gpg_error_from_syserror();
691 goto fail;
693 else if (n == 0) {
694 *rc = GPG_ERR_TIMEOUT;
695 goto fail;
698 ares_process(pwm->tcp_conn->chan, &rfds, &wfds);
700 if (pwm->tcp_conn->rc)
701 break;
702 } while (pwm->cmd == ASYNC_CMD_DNS);
704 if (pwm->tcp_conn->rc) {
705 *rc = pwm->tcp_conn->rc;
706 goto fail;
709 return setup_context(pwm, rc);
711 fail:
712 pwmd_close(pwm);
713 return NULL;
716 pwm_t *pwmd_tcp_connect(const char *host, int port, const char *identity,
717 const char *user, const char *known_hosts, gpg_error_t *rc)
719 return _do_pwmd_tcp_connect(host, port, identity, user, known_hosts, rc, 0);
722 /* Must free the result with pwmd_free_result(). */
723 char *pwmd_get_hostkey(const char *host, int port, gpg_error_t *rc)
725 char *hostkey;
726 pwm_t *pwm;
728 pwm = _do_pwmd_tcp_connect(host, port, NULL, NULL, NULL, rc, 1);
730 if (!pwm)
731 return NULL;
733 hostkey = xstrdup(pwm->tcp_conn->hostkey);
735 if (!hostkey)
736 *rc = gpg_error_from_errno(ENOMEM);
738 pwmd_close(pwm);
739 return hostkey;
742 pwm_t *pwmd_get_hostkey_async(const char *host, int port, gpg_error_t *rc)
744 return _do_pwmd_tcp_connect_async(host, port, NULL, NULL, NULL, rc,
745 ASYNC_CMD_HOSTKEY);
747 #endif
749 pwm_t *pwmd_connect(const char *path, gpg_error_t *rc)
751 pwm_t *pwm = NULL;
752 char *socketpath = NULL;
753 struct passwd *pw;
754 assuan_context_t ctx;
756 if (!path) {
757 pw = getpwuid(getuid());
758 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
759 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
761 else
762 socketpath = xstrdup(path);
764 *rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
765 xfree(socketpath);
767 if (*rc)
768 return NULL;
770 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
771 *rc = gpg_error_from_syserror();
772 return NULL;
775 return _socket_connect_finalize(pwm, ctx);
778 gpg_error_t pwmd_pending_line(pwm_t *pwm, char **line, size_t *len)
780 if (!pwm)
781 return GPG_ERR_INV_ARG;
783 if (assuan_pending_line(pwm->ctx))
784 return assuan_read_line(pwm->ctx, line, len);
786 return GPG_ERR_NO_DATA;
789 void pwmd_close(pwm_t *pwm)
791 if (!pwm)
792 return;
794 if (pwm->ctx)
795 assuan_disconnect(pwm->ctx);
797 if (pwm->password)
798 xfree(pwm->password);
800 if (pwm->title)
801 xfree(pwm->title);
803 if (pwm->desc)
804 xfree(pwm->desc);
806 if (pwm->prompt)
807 xfree(pwm->prompt);
809 if (pwm->pinentry_tty)
810 xfree(pwm->pinentry_tty);
812 if (pwm->pinentry_display)
813 xfree(pwm->pinentry_display);
815 if (pwm->pinentry_term)
816 xfree(pwm->pinentry_term);
818 if (pwm->filename)
819 xfree(pwm->filename);
821 #ifdef WITH_TCP
822 if (pwm->result)
823 xfree(pwm->result);
825 if (pwm->tcp_conn)
826 free_tcp_conn(pwm->tcp_conn);
828 #endif
829 xfree(pwm);
832 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
834 membuf_t *mem = (membuf_t *)data;
835 void *p;
837 if (!buffer)
838 return 0;
840 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
841 return 1;
843 mem->buf = p;
844 memcpy((char *)mem->buf + mem->len, buffer, len);
845 mem->len += len;
846 return 0;
849 void pwmd_free_result(void *data)
851 xfree(data);
854 static int _inquire_cb(void *data, const char *keyword)
856 pwm_t *pwm = (pwm_t *)data;
857 gpg_error_t rc = 0;
858 int flags = fcntl(pwm->fd, F_GETFL);
860 /* Shouldn't get this far without a callback. */
861 if (!pwm->inquire_func)
862 return GPG_ERR_INV_ARG;
865 * Since the socket file descriptor is probably set to non-blocking, set to
866 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
867 * asynchronous INQUIRE is supported by either libassuan or a later
868 * libpwmd.
870 fcntl(pwm->fd, F_SETFL, 0);
872 for (;;) {
873 char *result = NULL;
874 size_t len;
875 gpg_error_t arc;
877 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
878 rc = gpg_err_code(rc);
880 if (rc == GPG_ERR_EOF || !rc) {
881 if (len <= 0 || !result || !*result) {
882 rc = 0;
883 break;
886 arc = assuan_send_data(pwm->ctx, result, len);
888 if (rc == GPG_ERR_EOF) {
889 rc = arc;
890 break;
893 rc = arc;
895 else if (rc)
896 break;
899 fcntl(pwm->fd, F_SETFL, flags);
900 return rc;
903 gpg_error_t pwmd_finalize(pwm_t *pwm)
905 if (!pwm || pwm->cmd == ASYNC_CMD_NONE || pwm->state != ASYNC_DONE)
906 return GPG_ERR_INV_ARG;
908 pwm->state = ASYNC_INIT;
909 pwm->cmd = ASYNC_CMD_NONE;
911 #ifdef WITH_TCP
912 if (pwm->cmd == ASYNC_CMD_CONNECT || pwm->cmd == ASYNC_CMD_DNS) {
913 gpg_error_t rc = pwm->tcp_conn->rc;
915 if (pwm->result)
916 xfree(pwm->result);
918 pwm->result = NULL;
920 /* pwm is no longer a valid handle. */
921 if (rc)
922 pwmd_close(pwm);
924 return 0;
926 #endif
928 if (pwm->fd < 0)
929 return GPG_ERR_INV_ARG;
931 pwm->ntries = 0;
932 #ifdef WITH_PINENTRY
933 pwm->is_open_cmd = 0;
934 #endif
935 return 0;
938 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
940 char *buf;
941 gpg_error_t rc;
942 size_t len = strlen(cmd) + 2;
944 len += arg ? strlen(arg) : 0;
946 if (pwm->state != ASYNC_INIT)
947 return GPG_ERR_INV_STATE;
949 buf = (char *)xmalloc(len);
951 if (!buf) {
952 rc = gpg_error_from_errno(ENOMEM);
953 goto fail;
956 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
957 rc = assuan_write_line(pwm->ctx, buf);
958 xfree(buf);
960 if (!rc)
961 pwm->state = ASYNC_PROCESS;
963 fail:
964 return rc;
967 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
969 if (!pwm || !filename)
970 return GPG_ERR_INV_ARG;
972 /* For pinentry retries. */
973 if (!pwm->is_open_cmd) {
974 if (pwm->filename)
975 xfree(pwm->filename);
977 pwm->filename = xstrdup(filename);
980 pwm->is_open_cmd = 1;
981 return do_nb_command(pwm, "OPEN", filename);
984 gpg_error_t pwmd_save_async(pwm_t *pwm)
986 if (!pwm)
987 return GPG_ERR_INV_ARG;
989 return do_nb_command(pwm, "SAVE", NULL);
992 static gpg_error_t parse_assuan_line(pwm_t *pwm)
994 gpg_error_t rc;
995 char *line;
996 size_t len;
998 rc = assuan_read_line(pwm->ctx, &line, &len);
1000 if (!rc) {
1001 if (line[0] == 'O' && line[1] == 'K' &&
1002 (line[2] == 0 || line[2] == ' ')) {
1003 pwm->state = ASYNC_DONE;
1005 else if (line[0] == '#') {
1007 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
1008 if (pwm->status_func) {
1009 pwm->status_func(pwm->status_data,
1010 line[1] == 0 ? line+1 : line+2);
1013 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
1014 (line[3] == 0 || line[3] == ' ')) {
1015 line += 4;
1016 rc = atoi(line);
1017 pwm->state = ASYNC_DONE;
1021 return rc;
1024 gpg_error_t pwmd_get_result(pwm_t *pwm, const char **result)
1026 if (!pwm || !result)
1027 return GPG_ERR_INV_ARG;
1029 if (pwm->state != ASYNC_DONE)
1030 return GPG_ERR_INV_STATE;
1032 if (!pwm->result)
1033 return GPG_ERR_NO_DATA;
1035 *result = pwm->result;
1036 return 0;
1039 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
1041 fd_set fds;
1042 int n;
1043 struct timeval tv = {0, 0};
1045 *rc = 0;
1047 if (!pwm) {
1048 *rc = GPG_ERR_INV_ARG;
1049 return ASYNC_DONE;
1052 #ifdef WITH_TCP
1053 if (pwm->cmd == ASYNC_CMD_DNS) {
1054 fd_set rfds, wfds;
1056 if (pwm->tcp_conn->rc) {
1057 *rc = pwm->tcp_conn->rc;
1058 close(pwm->tcp_conn->fd);
1059 pwm->state = ASYNC_DONE;
1060 return pwm->state;
1063 FD_ZERO(&rfds);
1064 FD_ZERO(&wfds);
1065 n = ares_fds(pwm->tcp_conn->chan, &rfds, &wfds);
1067 /* Shouldn't happen. */
1068 if (!n)
1069 return pwm->state;
1071 n = select(n, &rfds, &wfds, NULL, &tv);
1073 if (n > 0)
1074 ares_process(pwm->tcp_conn->chan, &rfds, &wfds);
1076 return pwm->state;
1078 else if (pwm->cmd == ASYNC_CMD_CONNECT) {
1079 if (pwm->tcp_conn->rc == GPG_ERR_EINPROGRESS) {
1080 int ret;
1081 socklen_t len = sizeof(int);
1083 FD_ZERO(&fds);
1084 FD_SET(pwm->tcp_conn->fd, &fds);
1085 n = select(pwm->tcp_conn->fd+1, NULL, &fds, NULL, &tv);
1087 if (!n || !FD_ISSET(pwm->tcp_conn->fd, &fds))
1088 return pwm->state;
1089 else if (n == -1) {
1090 *rc = gpg_error_from_syserror();
1091 close(pwm->tcp_conn->fd);
1092 pwm->state = ASYNC_DONE;
1093 return pwm->state;
1096 ret = getsockopt(pwm->tcp_conn->fd, SOL_SOCKET, SO_ERROR, &n, &len);
1097 if (ret || n) {
1098 *rc = ret ? gpg_error_from_syserror() : gpg_error_from_errno(n);
1099 close(pwm->tcp_conn->fd);
1100 pwm->state = ASYNC_DONE;
1101 return pwm->state;
1104 else if (pwm->tcp_conn->rc) {
1105 *rc = pwm->tcp_conn->rc;
1106 close(pwm->tcp_conn->fd);
1107 pwm->state = ASYNC_DONE;
1108 return pwm->state;
1111 fcntl(pwm->tcp_conn->fd, F_SETFL, 0);
1112 setup_context(pwm, rc);
1113 pwm->state = ASYNC_DONE;
1114 return pwm->state;
1116 #endif
1118 if (pwm->fd < 0) {
1119 *rc = GPG_ERR_INV_ARG;
1120 return ASYNC_DONE;
1122 else if (pwm->state == ASYNC_DONE)
1123 return pwm->state;
1124 else if (pwm->state == ASYNC_INIT) {
1125 *rc = GPG_ERR_INV_STATE;
1126 return ASYNC_DONE;
1129 FD_ZERO(&fds);
1130 FD_SET(pwm->fd, &fds);
1131 #ifdef WITH_LIBPTH
1132 n = pth_select(pwm->fd+1, &fds, NULL, NULL, &tv);
1133 #else
1134 n = select(pwm->fd+1, &fds, NULL, NULL, &tv);
1135 #endif
1137 if (n > 0) {
1138 if (FD_ISSET(pwm->fd, &fds))
1139 *rc = parse_assuan_line(pwm);
1142 while (!*rc && assuan_pending_line(pwm->ctx))
1143 *rc = parse_assuan_line(pwm);
1145 if (pwm->is_open_cmd && gpg_err_code(*rc) == EPWMD_BADKEY &&
1146 ++pwm->ntries < pwm->pinentry_tries) {
1147 pwm->state = ASYNC_INIT;
1148 *rc = pwmd_open_async(pwm, pwm->filename);
1151 return pwm->state;
1154 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
1155 char **result, const char *cmd)
1157 membuf_t data;
1158 gpg_error_t rc;
1160 data.len = 0;
1161 data.buf = NULL;
1163 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
1164 pwm->status_func, pwm->status_data);
1166 if (rc) {
1167 if (data.buf) {
1168 xfree(data.buf);
1169 data.buf = NULL;
1172 else {
1173 if (data.buf) {
1174 mem_realloc_cb(&data, "", 1);
1175 *result = (char *)data.buf;
1179 return gpg_err_code(rc);
1182 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
1183 void *data)
1185 if (!pwm || !cmd || !fn)
1186 return GPG_ERR_INV_ARG;
1188 pwm->inquire_func = fn;
1189 pwm->inquire_data = data;
1190 return assuan_command(pwm, pwm->ctx, NULL, cmd);
1193 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
1195 #ifndef WITH_PINENTRY
1196 return GPG_ERR_NOT_IMPLEMENTED;
1197 #else
1198 if (!pwm || pwm->pid == -1)
1199 return GPG_ERR_INV_ARG;
1201 if (kill(pwm->pid, 0) == 0) {
1202 if (kill(pwm->pid, SIGTERM) == -1) {
1203 if (kill(pwm->pid, SIGKILL) == -1)
1204 return gpg_error_from_errno(errno);
1207 pwm->pin_error = GPG_ERR_TIMEOUT;
1209 else
1210 return gpg_error_from_errno(errno);
1212 return 0;
1213 #endif
1216 #ifdef WITH_PINENTRY
1217 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
1219 char *buf;
1220 char tmp[ASSUAN_LINELENGTH];
1221 gpg_error_t error;
1223 if (!pwm->title)
1224 pwm->title = xstrdup(N_("LibPWMD"));
1226 if (!pwm->prompt)
1227 pwm->prompt = xstrdup(N_("Passphrase:"));
1229 if (!pwm->desc && !which)
1230 pwm->desc = xstrdup(N_("Enter a passphrase."));
1232 if (which == 1) {
1233 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid passphrase, please try again."));
1234 buf = xstrdup(tmp);
1236 else if (which == 2) {
1237 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the passphrase again for confirmation."));
1238 buf = xstrdup(tmp);
1240 else {
1241 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
1242 sprintf(buf, "SETERROR %s", pwm->desc);
1245 error = pinentry_command(pwm, NULL, buf);
1246 xfree(buf);
1248 if (error)
1249 return error;
1251 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
1252 sprintf(buf, "SETPROMPT %s", pwm->prompt);
1253 error = pinentry_command(pwm, NULL, buf);
1254 xfree(buf);
1256 if (error)
1257 return error;
1259 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
1260 sprintf(buf, "SETDESC %s", pwm->title);
1261 error = pinentry_command(pwm, NULL, buf);
1262 xfree(buf);
1263 return error;
1266 static void update_pinentry_settings(pwm_t *pwm)
1268 FILE *fp;
1269 char buf[LINE_MAX];
1270 struct passwd *pw = getpwuid(getuid());
1271 char *p;
1273 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
1275 if ((fp = fopen(buf, "r")) == NULL)
1276 return;
1278 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
1279 char name[32], val[256];
1281 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
1282 continue;
1284 if (strcasecmp(name, "TTYNAME") == 0) {
1285 xfree(pwm->pinentry_tty);
1286 pwm->pinentry_tty = xstrdup(val);
1288 else if (strcasecmp(name, "TTYTYPE") == 0) {
1289 xfree(pwm->pinentry_term);
1290 pwm->pinentry_term = xstrdup(val);
1292 else if (strcasecmp(name, "DISPLAY") == 0) {
1293 xfree(pwm->pinentry_display);
1294 pwm->pinentry_display = xstrdup(val);
1296 else if (strcasecmp(name, "PATH") == 0) {
1297 xfree(pwm->pinentry_path);
1298 pwm->pinentry_path = xstrdup(val);
1302 fclose(fp);
1305 static gpg_error_t launch_pinentry(pwm_t *pwm)
1307 int rc;
1308 assuan_context_t ctx;
1309 int child_list[] = {-1};
1310 char *display = getenv("DISPLAY");
1311 const char *argv[6];
1312 int have_display = 0;
1313 char *tty = NULL;
1315 update_pinentry_settings(pwm);
1317 if (pwm->pinentry_display || display)
1318 have_display = 1;
1319 else {
1320 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
1322 if (!tty)
1323 return gpg_error_from_errno(errno);
1326 if (!have_display && !tty)
1327 return GPG_ERR_ENOTTY;
1329 argv[0] = "pinentry";
1330 argv[1] = have_display ? "--display" : "--ttyname";
1331 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
1332 argv[3] = NULL;
1334 if (!have_display) {
1335 argv[3] = "--ttytype";
1336 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
1337 argv[5] = NULL;
1340 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
1342 if (rc)
1343 return rc;
1345 pwm->pid = assuan_get_pid(ctx);
1346 pwm->pctx = ctx;
1347 return set_pinentry_strings(pwm, 0);
1350 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
1352 gpg_error_t n;
1354 if (!pwm->pctx) {
1355 n = launch_pinentry(pwm);
1357 if (n)
1358 return n;
1361 return assuan_command(pwm, pwm->pctx, result, cmd);
1364 static void pinentry_disconnect(pwm_t *pwm)
1366 if (pwm->pctx)
1367 assuan_disconnect(pwm->pctx);
1369 pwm->pctx = NULL;
1370 pwm->pid = -1;
1374 * Only called from a child process.
1376 static void catchsig(int sig)
1378 switch (sig) {
1379 case SIGALRM:
1380 if (gelapsed++ >= gtimeout) {
1381 global_error = pwmd_terminate_pinentry(gpwm);
1383 if (!global_error)
1384 global_error = GPG_ERR_TIMEOUT;
1386 break;
1389 alarm(1);
1390 break;
1391 default:
1392 break;
1397 * Borrowed from libassuan.
1399 static char *percent_escape(const char *atext)
1401 const unsigned char *s;
1402 int len = strlen(atext) * 3 + 1;
1403 char *buf = (char *)xmalloc(len), *p = buf;
1405 if (!buf)
1406 return NULL;
1408 for (s=(const unsigned char *)atext; *s; s++) {
1409 if (*s < ' ') {
1410 sprintf (p, "%%%02X", *s);
1411 p += 3;
1413 else
1414 *p++ = *s;
1417 *p = 0;
1418 return buf;
1420 #endif
1422 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
1424 if (!cmd)
1425 return GPG_ERR_INV_ARG;
1427 return assuan_command(pwm, pwm->ctx, result, cmd);
1430 gpg_error_t pwmd_command_ap(pwm_t *pwm, char **result, const char *cmd,
1431 va_list ap)
1433 char *buf;
1434 size_t len;
1435 gpg_error_t error;
1438 * C99 allows the dst pointer to be null which will calculate the length
1439 * of the would-be result and return it.
1441 len = vsnprintf(NULL, 0, cmd, ap)+1;
1442 buf = (char *)xmalloc(len);
1443 len = vsnprintf(buf, len, cmd, ap);
1444 error = send_command(pwm, result, buf);
1445 xfree(buf);
1446 return error;
1450 * Avoid sending the BYE command here. libassuan will close the file
1451 * descriptor and release the assuan context. Use pwmd_close() instead.
1453 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
1455 va_list ap;
1456 gpg_error_t error;
1458 if (!pwm || !cmd)
1459 return GPG_ERR_INV_ARG;
1461 *result = NULL;
1462 va_start(ap, cmd);
1463 error = pwmd_command_ap(pwm, result, cmd, ap);
1464 va_end(ap);
1465 return error;
1468 #ifdef WITH_PINENTRY
1469 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
1471 if (gtimeout) {
1472 signal(SIGALRM, catchsig);
1473 alarm(1);
1476 *result = NULL;
1477 return pinentry_command(pwm, result, "GETPIN");
1480 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
1482 int pin_try = *try_n;
1483 gpg_error_t error;
1485 getpin_again:
1486 *try_n = pin_try;
1488 if (pin_try == -1) {
1489 error = set_pinentry_strings(pwm, which);
1491 if (error) {
1492 pinentry_disconnect(pwm);
1493 return error;
1496 else {
1497 if (pwm->pinentry_tries-1 != pin_try) {
1498 error = set_pinentry_strings(pwm, 1);
1500 if (error) {
1501 pinentry_disconnect(pwm);
1502 return error;
1507 error = do_getpin(pwm, result);
1510 * Since there was input cancel any timeout setting.
1512 alarm(0);
1514 if (error) {
1515 if (error == GPG_ERR_CANCELED)
1516 return GPG_ERR_CANCELED;
1518 if (pin_try != -1 && pin_try--)
1519 goto getpin_again;
1521 if (pwm->pctx)
1522 pinentry_disconnect(pwm);
1524 *try_n = pin_try;
1525 return error;
1528 return 0;
1530 #endif
1532 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1534 gpg_error_t error;
1536 #ifndef WITH_PINENTRY
1537 return GPG_ERR_NOT_IMPLEMENTED;
1538 #endif
1540 if (!pwm || !pw || !pw->filename[0])
1541 return GPG_ERR_INV_ARG;
1543 close(pw->fd);
1545 if (pw->error) {
1546 error = pw->error;
1547 goto fail;
1550 if (pwm->filename)
1551 xfree(pwm->filename);
1553 pwm->filename = xstrdup(pw->filename);
1554 memset(pw, 0, sizeof(pwmd_nb_status_t));
1555 return 0;
1557 fail:
1558 memset(pw, 0, sizeof(pwmd_nb_status_t));
1559 return error;
1562 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
1564 char buf[ASSUAN_LINELENGTH];
1565 gpg_error_t error;
1566 char *result = NULL;
1568 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
1569 error = send_command(pwm, &result, buf);
1570 memset(buf, 0, sizeof(buf));
1572 if (error && result)
1573 xfree(result);
1575 return error;
1578 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
1579 int nb, int timeout)
1581 char *result = NULL;
1582 char *password = NULL;
1583 char path[PATH_MAX];
1584 #ifdef WITH_PINENTRY
1585 int pin_try;
1586 #endif
1588 if (!pwm || !filename || !*filename) {
1589 *error = GPG_ERR_INV_ARG;
1590 return nb ? -1 : 1;
1593 #ifdef WITH_PINENTRY
1594 pin_try = pwm->pinentry_tries - 1;
1595 #endif
1598 * Avoid calling pinentry if the password is cached on the server or if
1599 * this is a new file.
1601 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
1603 if (*error)
1604 return nb ? -1 : 1;
1606 snprintf(path, sizeof(path), "%s/%s", result, filename);
1607 pwmd_free_result(result);
1609 if (access(path, R_OK) == -1) {
1610 if (errno == ENOENT)
1611 goto gotpassword;
1614 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
1616 if (*error == EPWMD_CACHE_NOT_FOUND) {
1617 if (pwm->passfunc) {
1618 password = pwm->passfunc(pwm, pwm->passdata);
1619 goto gotpassword;
1622 #ifdef WITH_PINENTRY
1624 * Get the password from pinentry.
1626 if (pwm->use_pinentry) {
1628 * Nonblocking is wanted. fork() then return a file descriptor
1629 * that the client can use to read() from.
1631 if (nb) {
1632 int p[2];
1633 pid_t pid;
1634 pwmd_nb_status_t pw;
1636 if (pipe(p) == -1) {
1637 *error = gpg_error_from_syserror();
1638 return -1;
1641 #ifdef WITH_LIBPTH
1642 pid = pth_fork();
1643 #else
1644 pid = fork();
1645 #endif
1647 switch (pid) {
1648 case 0:
1649 close(p[0]);
1650 strncpy(pw.filename, filename, sizeof(pw.filename));
1651 pw.filename[sizeof(pw.filename)-1] = 0;
1652 pw.fd = p[0];
1654 if (timeout > 0) {
1655 gpwm = pwm;
1656 gtimeout = timeout;
1657 gelapsed = 0;
1660 getpin_nb_again:
1661 *error = getpin(pwm, &password, &pin_try, 0);
1663 if (*error) {
1664 getpin_nb_fail:
1665 if (pwm->pctx)
1666 pinentry_disconnect(pwm);
1668 if (gtimeout && gelapsed >= gtimeout)
1669 *error = GPG_ERR_TIMEOUT;
1671 pw.error = *error;
1672 #ifdef WITH_LIBPTH
1673 pth_write(p[1], &pw, sizeof(pw));
1674 #else
1675 write(p[1], &pw, sizeof(pw));
1676 #endif
1677 close(p[1]);
1678 _exit(1);
1682 * Don't count the time it takes to open the file
1683 * which may have many iterations.
1685 signal(SIGALRM, SIG_DFL);
1686 *error = do_open_command(pwm, filename, password);
1688 if (timeout)
1689 signal(SIGALRM, catchsig);
1691 if (pwm->pctx && *error == EPWMD_BADKEY) {
1692 if (pin_try-- > 0)
1693 goto getpin_nb_again;
1695 goto getpin_nb_fail;
1698 pinentry_disconnect(pwm);
1699 pw.error = 0;
1700 #ifdef WITH_LIBPTH
1701 pth_write(p[1], &pw, sizeof(pw));
1702 #else
1703 write(p[1], &pw, sizeof(pw));
1704 #endif
1705 close(p[1]);
1706 _exit(0);
1707 break;
1708 case -1:
1709 *error = gpg_error_from_syserror();
1710 close(p[0]);
1711 close(p[1]);
1712 return -1;
1713 default:
1714 break;
1717 close(p[1]);
1718 *error = 0;
1719 return p[0];
1722 else {
1723 #endif
1725 * Not using pinentry and the file was not found
1726 * in the cache.
1728 password = pwm->password;
1729 #ifdef WITH_PINENTRY
1731 #endif
1733 else if (*error)
1734 return nb ? -1 : 1;
1736 gotpassword:
1737 *error = do_open_command(pwm, filename, password);
1740 * Keep the user defined password set with pwmd_setopt(). The password may
1741 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1743 if (password && password != pwm->password)
1744 xfree(password);
1746 #ifdef WITH_PINENTRY
1747 if (*error == EPWMD_BADKEY) {
1748 if (pin_try-- > 0 && !nb) {
1749 *error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
1750 N_("Invalid passphrase, please try again."));
1752 if (*error)
1753 return 1;
1755 goto gotpassword;
1758 if (nb)
1759 pinentry_disconnect(pwm);
1761 return nb ? -1 : 1;
1763 #endif
1765 if (!*error) {
1766 if (pwm->filename)
1767 xfree(pwm->filename);
1769 pwm->filename = xstrdup(filename);
1773 * The file is cached or the file is a new file.
1775 if (nb)
1776 return *error ? -1 : -2;
1778 return *error ? 1 : 0;
1781 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1783 gpg_error_t error;
1785 do_pwmd_open(pwm, &error, filename, 0, 0);
1786 return error;
1789 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1790 int timeout)
1792 #ifndef WITH_PINENTRY
1793 *error = GPG_ERR_NOT_IMPLEMENTED;
1794 return -1;
1795 #else
1796 return do_pwmd_open(pwm, error, filename, 1, timeout);
1797 #endif
1800 #ifdef WITH_PINENTRY
1801 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1803 int confirm = 0;
1804 gpg_error_t error;
1805 char *result = NULL;
1806 int pin_try = -1;
1808 again:
1809 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1811 if (error) {
1812 if (pwm->pctx)
1813 pinentry_disconnect(pwm);
1815 if (*password)
1816 xfree(*password);
1818 return error;
1821 if (!confirm++) {
1822 *password = result;
1823 goto again;
1826 if (strcmp(*password, result)) {
1827 xfree(*password);
1828 xfree(result);
1829 pinentry_disconnect(pwm);
1830 error = EPWMD_BADKEY;
1831 return error;
1834 xfree(result);
1835 pinentry_disconnect(pwm);
1836 return 0;
1838 #endif
1840 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1842 char buf[ASSUAN_LINELENGTH];
1843 gpg_error_t error;
1844 char *result = NULL;
1846 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1847 error = send_command(pwm, &result, buf);
1848 memset(&buf, 0, sizeof(buf));
1850 if (error && result)
1851 xfree(result);
1853 return error;
1856 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1858 gpg_error_t rc;
1860 #ifndef WITH_PINENTRY
1861 return GPG_ERR_NOT_IMPLEMENTED;
1862 #endif
1864 if (!pwm || !pw || !pw->filename[0])
1865 return GPG_ERR_INV_ARG;
1867 close(pw->fd);
1868 rc = pw->error;
1869 memset(pw, 0, sizeof(pwmd_nb_status_t));
1870 return rc;
1873 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1875 char *result = NULL;
1876 char *password = NULL;
1878 if (!pwm) {
1879 *error = GPG_ERR_INV_ARG;
1880 return nb ? -1 : 1;
1883 if (pwm->use_pinentry || pwm->passfunc) {
1884 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1886 if (*error == EPWMD_CACHE_NOT_FOUND) {
1887 #ifdef WITH_PINENTRY
1888 if (pwm->use_pinentry) {
1889 if (nb) {
1890 int p[2];
1891 pid_t pid;
1892 pwmd_nb_status_t pw;
1894 if (pipe(p) == -1) {
1895 *error = gpg_error_from_syserror();
1896 return -1;
1899 #ifdef WITH_LIBPTH
1900 pid = pth_fork();
1901 #else
1902 pid = fork();
1903 #endif
1905 switch (pid) {
1906 case 0:
1907 close(p[0]);
1908 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1909 pw.filename[sizeof(pw.filename)-1] = 0;
1910 pw.fd = p[0];
1912 do {
1913 password = NULL;
1914 *error = do_save_getpin(pwm, &password);
1915 } while (*error == EPWMD_BADKEY);
1917 if (*error) {
1918 if (pwm->pctx)
1919 pinentry_disconnect(pwm);
1921 pw.error = *error;
1922 #ifdef WITH_LIBPTH
1923 pth_write(p[1], &pw, sizeof(pw));
1924 #else
1925 write(p[1], &pw, sizeof(pw));
1926 #endif
1927 close(p[1]);
1928 _exit(1);
1931 *error = do_save_command(pwm, password);
1932 pinentry_disconnect(pwm);
1933 pw.error = *error;
1934 #ifdef WITH_LIBPTH
1935 pth_write(p[1], &pw, sizeof(pw));
1936 #else
1937 write(p[1], &pw, sizeof(pw));
1938 #endif
1939 close(p[1]);
1940 _exit(0);
1941 break;
1942 case -1:
1943 *error = gpg_error_from_syserror();
1944 close(p[0]);
1945 close(p[1]);
1946 return -1;
1947 default:
1948 break;
1951 close(p[1]);
1952 *error = 0;
1953 return p[0];
1956 *error = do_save_getpin(pwm, &password);
1958 if (*error)
1959 return 1;
1961 else {
1962 #endif
1963 if (pwm->passfunc)
1964 password = (*pwm->passfunc)(pwm, pwm->passdata);
1965 #ifdef WITH_PINENTRY
1967 #endif
1969 else {
1970 if (*error)
1971 return nb ? -1 : 1;
1974 else
1975 password = pwm->password;
1977 *error = do_save_command(pwm, password);
1979 if (password && password != pwm->password)
1980 xfree(password);
1982 if (nb)
1983 return *error ? -1 : -2;
1985 return *error ? 1 : 0;
1988 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1990 #ifndef WITH_PINENTRY
1991 *error = GPG_ERR_NOT_IMPLEMENTED;
1992 return -1;
1993 #else
1994 return do_pwmd_save(pwm, error, 1);
1995 #endif
1998 gpg_error_t pwmd_save(pwm_t *pwm)
2000 gpg_error_t error;
2002 do_pwmd_save(pwm, &error, 0);
2003 return error;
2006 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
2008 va_list ap;
2009 #ifdef WITH_PINENTRY
2010 int n = va_arg(ap, int);
2011 char *result;
2012 #endif
2013 char *arg1;
2014 gpg_error_t error = 0;
2016 if (!pwm)
2017 return GPG_ERR_INV_ARG;
2019 va_start(ap, opt);
2021 switch (opt) {
2022 case PWMD_OPTION_STATUS_FUNC:
2023 pwm->status_func = va_arg(ap, pwmd_status_fn);
2024 break;
2025 case PWMD_OPTION_STATUS_DATA:
2026 pwm->status_data = va_arg(ap, void *);
2027 break;
2028 case PWMD_OPTION_PASSWORD_FUNC:
2029 pwm->passfunc = va_arg(ap, pwmd_password_fn);
2030 break;
2031 case PWMD_OPTION_PASSWORD_DATA:
2032 pwm->passdata = va_arg(ap, void *);
2033 break;
2034 case PWMD_OPTION_PASSWORD:
2035 arg1 = va_arg(ap, char *);
2037 if (pwm->password)
2038 xfree(pwm->password);
2040 pwm->password = xstrdup(arg1);
2041 break;
2042 #ifdef WITH_PINENTRY
2043 case PWMD_OPTION_PINENTRY:
2044 n = va_arg(ap, int);
2046 if (n != 0 && n != 1) {
2047 va_end(ap);
2048 error = GPG_ERR_INV_VALUE;
2050 else {
2051 pwm->use_pinentry = n;
2052 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
2053 !pwm->use_pinentry);
2055 break;
2056 case PWMD_OPTION_PINENTRY_TRIES:
2057 n = va_arg(ap, int);
2059 if (n <= 0) {
2060 va_end(ap);
2061 error = GPG_ERR_INV_VALUE;
2063 else
2064 pwm->pinentry_tries = n;
2065 break;
2066 case PWMD_OPTION_PINENTRY_PATH:
2067 if (pwm->pinentry_path)
2068 xfree(pwm->pinentry_path);
2070 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
2071 break;
2072 case PWMD_OPTION_PINENTRY_TTY:
2073 if (pwm->pinentry_tty)
2074 xfree(pwm->pinentry_tty);
2076 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
2077 break;
2078 case PWMD_OPTION_PINENTRY_DISPLAY:
2079 if (pwm->pinentry_display)
2080 xfree(pwm->pinentry_display);
2082 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
2083 break;
2084 case PWMD_OPTION_PINENTRY_TERM:
2085 if (pwm->pinentry_term)
2086 xfree(pwm->pinentry_term);
2088 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
2089 break;
2090 case PWMD_OPTION_PINENTRY_TITLE:
2091 if (pwm->title)
2092 xfree(pwm->title);
2093 pwm->title = percent_escape(va_arg(ap, char *));
2094 break;
2095 case PWMD_OPTION_PINENTRY_PROMPT:
2096 if (pwm->prompt)
2097 xfree(pwm->prompt);
2098 pwm->prompt = percent_escape(va_arg(ap, char *));
2099 break;
2100 case PWMD_OPTION_PINENTRY_DESC:
2101 if (pwm->desc)
2102 xfree(pwm->desc);
2103 pwm->desc = percent_escape(va_arg(ap, char *));
2104 break;
2105 #else
2106 case PWMD_OPTION_PINENTRY:
2107 case PWMD_OPTION_PINENTRY_TRIES:
2108 case PWMD_OPTION_PINENTRY_PATH:
2109 case PWMD_OPTION_PINENTRY_TTY:
2110 case PWMD_OPTION_PINENTRY_DISPLAY:
2111 case PWMD_OPTION_PINENTRY_TERM:
2112 case PWMD_OPTION_PINENTRY_TITLE:
2113 case PWMD_OPTION_PINENTRY_PROMPT:
2114 case PWMD_OPTION_PINENTRY_DESC:
2115 error = GPG_ERR_NOT_IMPLEMENTED;
2116 break;
2117 #endif
2118 default:
2119 error = GPG_ERR_NOT_IMPLEMENTED;
2120 break;
2123 va_end(ap);
2124 return error;
2128 * Prevent requiring assuan.h when setting ctx. The ctx is really an
2129 * assuan_context_t *.
2131 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, void *ctx, int *fd)
2133 if (!pwm)
2134 return GPG_ERR_INV_ARG;
2136 ctx = pwm->ctx;
2137 *fd = pwm->fd;
2138 return 0;