Revert "Let pwmd_process() work when not inside a command. This is needed so"
[libpwmd.git] / src / libpwmd.c
blob6bd58ecb86f799b2c906dede51dea83f33f870ad
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);
260 pwm->tcp_conn = NULL;
264 * Sets common options from both pwmd_tcp_connect() and
265 * pwmd_tcp_connect_async().
267 static gpg_error_t init_tcp_conn(pwmd_tcp_conn_t **dst, const char *host,
268 int port, const char *identity, const char *user, const char *hosts,
269 int get)
271 pwmd_tcp_conn_t *conn;
272 gpg_error_t rc = 0;
274 if (get) {
275 if (!host)
276 return GPG_ERR_INV_ARG;
278 else {
279 if (!host || !identity || !user || !hosts)
280 return GPG_ERR_INV_ARG;
283 conn = xcalloc(1, sizeof(pwmd_tcp_conn_t));
285 if (!conn)
286 return gpg_error_from_errno(ENOMEM);
288 conn->port = port == -1 ? 22 : port;
289 conn->host = xstrdup(host);
291 if (!conn->host) {
292 rc = gpg_error_from_errno(ENOMEM);
293 goto fail;
296 if (!get) {
297 conn->identity = xstrdup(identity);
299 if (!conn->identity) {
300 rc = gpg_error_from_errno(ENOMEM);
301 goto fail;
304 conn->identity_pub = xmalloc(strlen(conn->identity)+5);
306 if (!conn->identity_pub) {
307 rc = gpg_error_from_errno(ENOMEM);
308 goto fail;
311 sprintf(conn->identity_pub, "%s.pub", conn->identity);
312 conn->username = xstrdup(user);
314 if (!conn->username) {
315 rc = gpg_error_from_errno(ENOMEM);
316 goto fail;
319 conn->known_hosts = xstrdup(hosts);
321 if (!conn->known_hosts) {
322 rc = gpg_error_from_errno(ENOMEM);
323 goto fail;
327 *dst = conn;
328 return 0;
330 fail:
331 free_tcp_conn(conn);
332 return rc;
335 static gpg_error_t do_connect(pwm_t *pwm, int prot, void *addr)
337 struct sockaddr_in their_addr;
339 pwm->tcp_conn->fd = socket(prot, SOCK_STREAM, 0);
341 if (pwm->tcp_conn->fd == -1)
342 return gpg_error_from_syserror();
344 if (pwm->tcp_conn->async)
345 fcntl(pwm->tcp_conn->fd, F_SETFL, O_NONBLOCK);
347 pwm->cmd = ASYNC_CMD_CONNECT;
348 their_addr.sin_family = prot;
349 their_addr.sin_port = htons(pwm->tcp_conn->port);
350 their_addr.sin_addr = *((struct in_addr *)addr);
351 memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
353 #ifdef WITH_LIBPTH
354 if (pth_connect(pwm->tcp_conn->fd, (struct sockaddr *)&their_addr,
355 sizeof(their_addr)) == -1)
356 #else
357 if (connect(pwm->tcp_conn->fd, (struct sockaddr *)&their_addr,
358 sizeof(their_addr)) == -1)
359 #endif
360 return gpg_error_from_syserror();
362 return 0;
365 static gpg_error_t ares_error_to_pwmd(int status)
367 if (status != ARES_SUCCESS)
368 warnx("%s", ares_strerror(status));
370 switch (status) {
371 case ARES_ENODATA:
372 case ARES_EFORMERR:
373 case ARES_ENOTFOUND:
374 return GPG_ERR_UNKNOWN_HOST;
375 case ARES_ESERVFAIL:
376 return GPG_ERR_EHOSTDOWN;
377 case ARES_ETIMEOUT:
378 return GPG_ERR_TIMEOUT;
379 case ARES_ENOMEM:
380 return gpg_error_from_errno(ENOMEM);
381 case ARES_ECONNREFUSED:
382 return GPG_ERR_ECONNREFUSED;
383 default:
384 /* FIXME ??? */
385 return GPG_ERR_EHOSTUNREACH;
388 return ARES_SUCCESS;
391 static void dns_resolve_cb(void *arg, int status, int timeouts,
392 unsigned char *abuf, int alen)
394 pwm_t *pwm = arg;
395 int rc;
396 struct hostent *he;
398 if (status == ARES_EDESTRUCTION)
399 return;
401 if (status != ARES_SUCCESS) {
402 pwm->tcp_conn->rc = ares_error_to_pwmd(status);
403 return;
406 //FIXME localhost. works with ipv4. maybe local system config error
407 /* Check for an IPv6 address first. */
408 rc = ares_parse_a_reply(abuf, alen, &he, NULL, NULL);
410 if (rc != ARES_SUCCESS) {
411 if (rc != ARES_ENODATA) {
412 pwm->tcp_conn->rc = ares_error_to_pwmd(status);
413 return;
416 rc = ares_parse_aaaa_reply(abuf, alen, &he, NULL, NULL);
418 if (rc != ARES_SUCCESS) {
419 pwm->tcp_conn->rc = ares_error_to_pwmd(status);
420 return;
424 pwm->tcp_conn->he = he;
425 pwm->tcp_conn->rc = do_connect(pwm, he->h_addrtype, he->h_addr);
428 pwm_t *_do_pwmd_tcp_connect_async(const char *host, int port,
429 const char *identity, const char *user, const char *known_hosts,
430 gpg_error_t *rc, pwmd_async_cmd_t which)
432 pwmd_tcp_conn_t *conn;
433 pwm_t *pwm;
435 *rc = init_tcp_conn(&conn, host, port, identity, user, known_hosts,
436 which == ASYNC_CMD_HOSTKEY ? 1 : 0);
438 if (*rc)
439 return NULL;
441 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
442 *rc = gpg_error_from_syserror();
443 free_tcp_conn(conn);
444 return NULL;
447 conn->async = 1;
448 pwm->tcp_conn = conn;
449 pwm->tcp_conn->cmd = which;
451 if (pwm->tcp_conn->cmd == ASYNC_CMD_HOSTKEY)
452 pwm->tcp_conn->get_only = 1;
454 pwm->cmd = ASYNC_CMD_DNS;
455 pwm->state = ASYNC_PROCESS;
456 ares_init(&pwm->tcp_conn->chan);
457 ares_query(pwm->tcp_conn->chan, pwm->tcp_conn->host, ns_c_any, ns_t_any,
458 dns_resolve_cb, pwm);
459 return pwm;
462 pwm_t *pwmd_tcp_connect_async(const char *host, int port, const char *identity,
463 const char *user, const char *known_hosts, gpg_error_t *rc)
465 return _do_pwmd_tcp_connect_async(host, port, identity, user, known_hosts,
466 rc, ASYNC_CMD_CONNECT);
469 void *_ssh_malloc(size_t size, void **data)
471 return xmalloc(size);
474 void _ssh_free(void *ptr, void **data)
476 xfree(ptr);
479 void *_ssh_realloc(void *ptr, size_t size, void **data)
481 return xrealloc(ptr, size);
484 static char *to_hex(const char *str, size_t slen)
486 int i;
487 char *buf = xmalloc(slen*2+1);
489 if (!buf)
490 return NULL;
492 for (i = 0, buf[0] = 0; i < slen; i++) {
493 char tmp[3];
495 sprintf(tmp, "%02x", (unsigned char)str[i]);
496 strcat(buf, tmp);
499 return buf;
502 static int verify_host_key(pwm_t *pwm)
504 FILE *fp = fopen(pwm->tcp_conn->known_hosts, "r");
505 char *buf, *p;
507 if (!fp)
508 return 1;
510 buf = xmalloc(LINE_MAX);
512 if (!buf)
513 goto fail;
515 while ((p = fgets(buf, LINE_MAX, fp))) {
516 if (*p == '#' || isspace(*p))
517 continue;
519 if (p[strlen(p)-1] == '\n')
520 p[strlen(p)-1] = 0;
522 if (!strcmp(buf, pwm->tcp_conn->hostkey))
523 goto done;
526 fail:
527 if (buf)
528 xfree(buf);
530 fclose(fp);
531 return 1;
533 done:
534 xfree(buf);
535 fclose(fp);
536 return 0;
539 static gpg_error_t authenticate_ssh(pwm_t *pwm)
541 const char *fp = libssh2_hostkey_hash(pwm->tcp_conn->session,
542 LIBSSH2_HOSTKEY_HASH_SHA1);
543 char *userauth;
545 pwm->tcp_conn->hostkey = to_hex(fp, 20);
547 if (!pwm->tcp_conn->hostkey)
548 return gpg_error_from_errno(ENOMEM);
550 if (pwm->tcp_conn->get_only)
551 return 0;
553 if (!fp || verify_host_key(pwm))
554 return GPG_ERR_CHECKSUM;
556 userauth = libssh2_userauth_list(pwm->tcp_conn->session,
557 pwm->tcp_conn->username, strlen(pwm->tcp_conn->username));
559 if (!userauth || !strstr(userauth, "publickey"))
560 return GPG_ERR_BAD_PIN_METHOD;
562 if (libssh2_userauth_publickey_fromfile(pwm->tcp_conn->session,
563 pwm->tcp_conn->username, pwm->tcp_conn->identity_pub,
564 pwm->tcp_conn->identity, NULL))
565 return GPG_ERR_BAD_SECKEY;
567 return 0;
570 static pwm_t *setup_context(pwm_t *pwm, gpg_error_t *rc)
572 assuan_context_t ctx;
573 struct assuan_io_hooks io_hooks = {read_hook, write_hook};
575 pwm->tcp_conn->session = libssh2_session_init_ex(_ssh_malloc, _ssh_free,
576 _ssh_realloc, NULL);
578 if (!pwm->tcp_conn->session) {
579 *rc = gpg_error_from_errno(ENOMEM);
580 goto fail;
583 if (libssh2_session_startup(pwm->tcp_conn->session, pwm->tcp_conn->fd)) {
584 *rc = GPG_ERR_ASSUAN_SERVER_FAULT;
585 goto fail;
588 *rc = authenticate_ssh(pwm);
590 if (*rc)
591 goto fail;
593 /* pwmd_get_hostkey(). */
594 if (pwm->tcp_conn->get_only) {
595 pwm->result = xstrdup(pwm->tcp_conn->hostkey);
597 if (!pwm->result) {
598 *rc = gpg_error_from_errno(ENOMEM);
599 goto fail;
602 return pwm;
605 pwm->tcp_conn->channel = libssh2_channel_open_session(pwm->tcp_conn->session);
607 if (!pwm->tcp_conn->channel) {
608 *rc = GPG_ERR_ASSUAN_SERVER_FAULT;
609 goto fail;
612 if (libssh2_channel_shell(pwm->tcp_conn->channel)) {
613 *rc = GPG_ERR_ASSUAN_SERVER_FAULT;
614 goto fail;
617 assuan_set_io_hooks(&io_hooks);
618 *rc = assuan_socket_connect_fd(&ctx, pwm->tcp_conn->fd, 0, pwm);
620 if (*rc)
621 goto fail;
623 assuan_set_finish_handler(ctx, _ssh_assuan_deinit);
624 return _socket_connect_finalize(pwm, ctx);
626 fail:
627 if (!pwm->tcp_conn->async)
628 pwmd_close(pwm);
630 return NULL;
633 static pwm_t *_do_pwmd_tcp_connect(const char *host, int port,
634 const char *identity, const char *user, const char *known_hosts,
635 gpg_error_t *rc, int get)
637 pwm_t *pwm = NULL;
638 pwmd_tcp_conn_t *conn;
640 *rc = init_tcp_conn(&conn, host, port, identity, user, known_hosts, get);
642 if (*rc)
643 return NULL;
645 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
646 *rc = gpg_error_from_errno(ENOMEM);
647 goto fail;
650 pwm->tcp_conn = conn;
651 pwm->tcp_conn->get_only = get;
652 pwm->cmd = ASYNC_CMD_DNS;
653 ares_init(&pwm->tcp_conn->chan);
654 ares_query(pwm->tcp_conn->chan, pwm->tcp_conn->host, ns_c_any, ns_t_any,
655 dns_resolve_cb, pwm);
657 /* dns_resolve_cb() may have already been called. */
658 if (pwm->tcp_conn->rc) {
659 *rc = pwm->tcp_conn->rc;
660 goto fail;
664 * Fake a blocking DNS lookup. libcares does a better job than
665 * getaddrinfo().
667 do {
668 fd_set rfds, wfds;
669 int n;
670 struct timeval tv;
672 FD_ZERO(&rfds);
673 FD_ZERO(&wfds);
674 n = ares_fds(pwm->tcp_conn->chan, &rfds, &wfds);
675 ares_timeout(pwm->tcp_conn->chan, NULL, &tv);
676 #ifdef WITH_LIBPTH
677 n = pth_select(n, &rfds, &wfds, NULL, &tv);
678 #else
679 n = select(n, &rfds, &wfds, NULL, &tv);
680 #endif
682 if (n == -1) {
683 *rc = gpg_error_from_syserror();
684 goto fail;
686 else if (n == 0) {
687 *rc = GPG_ERR_TIMEOUT;
688 goto fail;
691 ares_process(pwm->tcp_conn->chan, &rfds, &wfds);
693 if (pwm->tcp_conn->rc)
694 break;
695 } while (pwm->cmd == ASYNC_CMD_DNS);
697 if (pwm->tcp_conn->rc) {
698 *rc = pwm->tcp_conn->rc;
699 goto fail;
702 return setup_context(pwm, rc);
704 fail:
705 pwmd_close(pwm);
706 return NULL;
709 pwm_t *pwmd_tcp_connect(const char *host, int port, const char *identity,
710 const char *user, const char *known_hosts, gpg_error_t *rc)
712 return _do_pwmd_tcp_connect(host, port, identity, user, known_hosts, rc, 0);
715 /* Must free the result with pwmd_free_result(). */
716 char *pwmd_get_hostkey(const char *host, int port, gpg_error_t *rc)
718 char *hostkey;
719 pwm_t *pwm;
721 pwm = _do_pwmd_tcp_connect(host, port, NULL, NULL, NULL, rc, 1);
723 if (!pwm)
724 return NULL;
726 hostkey = xstrdup(pwm->tcp_conn->hostkey);
728 if (!hostkey)
729 *rc = gpg_error_from_errno(ENOMEM);
731 pwmd_close(pwm);
732 return hostkey;
735 pwm_t *pwmd_get_hostkey_async(const char *host, int port, gpg_error_t *rc)
737 return _do_pwmd_tcp_connect_async(host, port, NULL, NULL, NULL, rc,
738 ASYNC_CMD_HOSTKEY);
740 #endif
742 pwm_t *pwmd_connect(const char *path, gpg_error_t *rc)
744 pwm_t *pwm = NULL;
745 char *socketpath = NULL;
746 struct passwd *pw;
747 assuan_context_t ctx;
749 if (!path) {
750 pw = getpwuid(getuid());
751 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
752 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
754 else
755 socketpath = xstrdup(path);
757 *rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
758 xfree(socketpath);
760 if (*rc)
761 return NULL;
763 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
764 *rc = gpg_error_from_syserror();
765 return NULL;
768 return _socket_connect_finalize(pwm, ctx);
771 gpg_error_t pwmd_pending_line(pwm_t *pwm, char **line, size_t *len)
773 if (!pwm)
774 return GPG_ERR_INV_ARG;
776 if (assuan_pending_line(pwm->ctx))
777 return assuan_read_line(pwm->ctx, line, len);
779 return GPG_ERR_NO_DATA;
782 void pwmd_close(pwm_t *pwm)
784 if (!pwm)
785 return;
787 if (pwm->ctx)
788 assuan_disconnect(pwm->ctx);
790 if (pwm->password)
791 xfree(pwm->password);
793 if (pwm->title)
794 xfree(pwm->title);
796 if (pwm->desc)
797 xfree(pwm->desc);
799 if (pwm->prompt)
800 xfree(pwm->prompt);
802 if (pwm->pinentry_tty)
803 xfree(pwm->pinentry_tty);
805 if (pwm->pinentry_display)
806 xfree(pwm->pinentry_display);
808 if (pwm->pinentry_term)
809 xfree(pwm->pinentry_term);
811 if (pwm->lcctype)
812 xfree(pwm->lcctype);
814 if (pwm->lcmessages)
815 xfree(pwm->lcmessages);
817 if (pwm->filename)
818 xfree(pwm->filename);
820 #ifdef WITH_TCP
821 if (pwm->result)
822 xfree(pwm->result);
824 if (pwm->tcp_conn)
825 free_tcp_conn(pwm->tcp_conn);
827 #endif
828 xfree(pwm);
831 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
833 membuf_t *mem = (membuf_t *)data;
834 void *p;
836 if (!buffer)
837 return 0;
839 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
840 return 1;
842 mem->buf = p;
843 memcpy((char *)mem->buf + mem->len, buffer, len);
844 mem->len += len;
845 return 0;
848 void pwmd_free_result(void *data)
850 xfree(data);
853 static int _inquire_cb(void *data, const char *keyword)
855 pwm_t *pwm = (pwm_t *)data;
856 gpg_error_t rc = 0;
857 int flags = fcntl(pwm->fd, F_GETFL);
859 /* Shouldn't get this far without a callback. */
860 if (!pwm->inquire_func)
861 return GPG_ERR_INV_ARG;
864 * Since the socket file descriptor is probably set to non-blocking, set to
865 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
866 * asynchronous INQUIRE is supported by either libassuan or a later
867 * libpwmd.
869 fcntl(pwm->fd, F_SETFL, 0);
871 for (;;) {
872 char *result = NULL;
873 size_t len;
874 gpg_error_t arc;
876 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
877 rc = gpg_err_code(rc);
879 if (rc == GPG_ERR_EOF || !rc) {
880 if (len <= 0 || !result || !*result) {
881 rc = 0;
882 break;
885 arc = assuan_send_data(pwm->ctx, result, len);
887 if (rc == GPG_ERR_EOF) {
888 rc = arc;
889 break;
892 rc = arc;
894 else if (rc)
895 break;
898 fcntl(pwm->fd, F_SETFL, flags);
899 return rc;
902 gpg_error_t pwmd_finalize(pwm_t *pwm)
904 if (!pwm || pwm->cmd == ASYNC_CMD_NONE || pwm->state != ASYNC_DONE)
905 return GPG_ERR_INV_ARG;
907 pwm->state = ASYNC_INIT;
908 pwm->cmd = ASYNC_CMD_NONE;
910 #ifdef WITH_TCP
911 if (pwm->cmd == ASYNC_CMD_CONNECT || pwm->cmd == ASYNC_CMD_DNS) {
912 gpg_error_t rc = pwm->tcp_conn->rc;
914 if (pwm->result)
915 xfree(pwm->result);
917 pwm->result = NULL;
919 /* pwm is no longer a valid handle. */
920 if (rc)
921 pwmd_close(pwm);
923 return 0;
925 #endif
927 if (pwm->fd < 0)
928 return GPG_ERR_INV_ARG;
930 pwm->ntries = 0;
931 #ifdef WITH_PINENTRY
932 pwm->is_open_cmd = 0;
933 #endif
934 return 0;
937 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
939 char *buf;
940 gpg_error_t rc;
941 size_t len = strlen(cmd) + 2;
943 len += arg ? strlen(arg) : 0;
945 if (pwm->state != ASYNC_INIT)
946 return GPG_ERR_INV_STATE;
948 buf = (char *)xmalloc(len);
950 if (!buf) {
951 rc = gpg_error_from_errno(ENOMEM);
952 goto fail;
955 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
956 rc = assuan_write_line(pwm->ctx, buf);
957 xfree(buf);
959 if (!rc)
960 pwm->state = ASYNC_PROCESS;
962 fail:
963 return rc;
966 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
968 if (!pwm || !filename)
969 return GPG_ERR_INV_ARG;
971 /* For pinentry retries. */
972 if (!pwm->is_open_cmd) {
973 if (pwm->filename)
974 xfree(pwm->filename);
976 pwm->filename = xstrdup(filename);
979 pwm->is_open_cmd = 1;
980 return do_nb_command(pwm, "OPEN", filename);
983 gpg_error_t pwmd_save_async(pwm_t *pwm)
985 if (!pwm)
986 return GPG_ERR_INV_ARG;
988 return do_nb_command(pwm, "SAVE", NULL);
991 static gpg_error_t parse_assuan_line(pwm_t *pwm)
993 gpg_error_t rc;
994 char *line;
995 size_t len;
997 rc = assuan_read_line(pwm->ctx, &line, &len);
999 if (!rc) {
1000 if (line[0] == 'O' && line[1] == 'K' &&
1001 (line[2] == 0 || line[2] == ' ')) {
1002 pwm->state = ASYNC_DONE;
1004 else if (line[0] == '#') {
1006 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
1007 if (pwm->status_func) {
1008 pwm->status_func(pwm->status_data,
1009 line[1] == 0 ? line+1 : line+2);
1012 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
1013 (line[3] == 0 || line[3] == ' ')) {
1014 line += 4;
1015 rc = atoi(line);
1016 pwm->state = ASYNC_DONE;
1020 return rc;
1023 gpg_error_t pwmd_get_result(pwm_t *pwm, const char **result)
1025 if (!pwm || !result)
1026 return GPG_ERR_INV_ARG;
1028 if (pwm->state != ASYNC_DONE)
1029 return GPG_ERR_INV_STATE;
1031 if (!pwm->result)
1032 return GPG_ERR_NO_DATA;
1034 *result = pwm->result;
1035 return 0;
1038 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
1040 fd_set fds;
1041 int n;
1042 struct timeval tv = {0, 0};
1044 *rc = 0;
1046 if (!pwm) {
1047 *rc = GPG_ERR_INV_ARG;
1048 return ASYNC_DONE;
1051 #ifdef WITH_TCP
1052 if (pwm->cmd == ASYNC_CMD_DNS) {
1053 fd_set rfds, wfds;
1055 if (pwm->tcp_conn->rc) {
1056 *rc = pwm->tcp_conn->rc;
1057 close(pwm->tcp_conn->fd);
1058 pwm->state = ASYNC_DONE;
1059 return pwm->state;
1062 FD_ZERO(&rfds);
1063 FD_ZERO(&wfds);
1064 n = ares_fds(pwm->tcp_conn->chan, &rfds, &wfds);
1066 /* Shouldn't happen. */
1067 if (!n)
1068 return pwm->state;
1070 n = select(n, &rfds, &wfds, NULL, &tv);
1072 if (n > 0)
1073 ares_process(pwm->tcp_conn->chan, &rfds, &wfds);
1075 return pwm->state;
1077 else if (pwm->cmd == ASYNC_CMD_CONNECT) {
1078 if (pwm->tcp_conn->rc == GPG_ERR_EINPROGRESS) {
1079 int ret;
1080 socklen_t len = sizeof(int);
1082 FD_ZERO(&fds);
1083 FD_SET(pwm->tcp_conn->fd, &fds);
1084 n = select(pwm->tcp_conn->fd+1, NULL, &fds, NULL, &tv);
1086 if (!n || !FD_ISSET(pwm->tcp_conn->fd, &fds))
1087 return pwm->state;
1088 else if (n == -1) {
1089 *rc = gpg_error_from_syserror();
1090 close(pwm->tcp_conn->fd);
1091 pwm->state = ASYNC_DONE;
1092 return pwm->state;
1095 ret = getsockopt(pwm->tcp_conn->fd, SOL_SOCKET, SO_ERROR, &n, &len);
1096 if (ret || n) {
1097 *rc = ret ? gpg_error_from_syserror() : gpg_error_from_errno(n);
1098 close(pwm->tcp_conn->fd);
1099 pwm->state = ASYNC_DONE;
1100 return pwm->state;
1103 else if (pwm->tcp_conn->rc) {
1104 *rc = pwm->tcp_conn->rc;
1105 close(pwm->tcp_conn->fd);
1106 pwm->state = ASYNC_DONE;
1107 return pwm->state;
1110 fcntl(pwm->tcp_conn->fd, F_SETFL, 0);
1111 setup_context(pwm, rc);
1112 pwm->state = ASYNC_DONE;
1113 return pwm->state;
1115 #endif
1117 if (pwm->fd < 0) {
1118 *rc = GPG_ERR_INV_ARG;
1119 return ASYNC_DONE;
1121 else if (pwm->state == ASYNC_DONE)
1122 return pwm->state;
1123 else if (pwm->state == ASYNC_INIT) {
1124 *rc = GPG_ERR_INV_STATE;
1125 return ASYNC_DONE;
1128 FD_ZERO(&fds);
1129 FD_SET(pwm->fd, &fds);
1130 #ifdef WITH_LIBPTH
1131 n = pth_select(pwm->fd+1, &fds, NULL, NULL, &tv);
1132 #else
1133 n = select(pwm->fd+1, &fds, NULL, NULL, &tv);
1134 #endif
1136 if (n > 0) {
1137 if (FD_ISSET(pwm->fd, &fds))
1138 *rc = parse_assuan_line(pwm);
1141 while (!*rc && assuan_pending_line(pwm->ctx))
1142 *rc = parse_assuan_line(pwm);
1144 if (pwm->is_open_cmd && gpg_err_code(*rc) == EPWMD_BADKEY &&
1145 ++pwm->ntries < pwm->pinentry_tries) {
1146 pwm->state = ASYNC_INIT;
1147 *rc = pwmd_open_async(pwm, pwm->filename);
1150 return pwm->state;
1153 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
1154 char **result, const char *cmd)
1156 membuf_t data;
1157 gpg_error_t rc;
1159 data.len = 0;
1160 data.buf = NULL;
1162 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
1163 pwm->status_func, pwm->status_data);
1165 if (rc) {
1166 if (data.buf) {
1167 xfree(data.buf);
1168 data.buf = NULL;
1171 else {
1172 if (data.buf) {
1173 mem_realloc_cb(&data, "", 1);
1174 *result = (char *)data.buf;
1178 return gpg_err_code(rc);
1181 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
1182 void *data)
1184 if (!pwm || !cmd || !fn)
1185 return GPG_ERR_INV_ARG;
1187 pwm->inquire_func = fn;
1188 pwm->inquire_data = data;
1189 return assuan_command(pwm, pwm->ctx, NULL, cmd);
1192 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
1194 #ifndef WITH_PINENTRY
1195 return GPG_ERR_NOT_IMPLEMENTED;
1196 #else
1197 pid_t pid = pwm->pid;
1199 pwm->pid = -1;
1201 if (!pwm || pid == -1)
1202 return GPG_ERR_INV_ARG;
1204 if (kill(pid, 0) == 0) {
1205 if (kill(pid, SIGTERM) == -1) {
1206 if (kill(pid, SIGKILL) == -1)
1207 return gpg_error_from_errno(errno);
1210 pwm->pin_error = GPG_ERR_TIMEOUT;
1212 else
1213 return gpg_error_from_errno(errno);
1215 return 0;
1216 #endif
1219 #ifdef WITH_PINENTRY
1220 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
1222 char *buf;
1223 char tmp[ASSUAN_LINELENGTH];
1224 gpg_error_t error;
1226 if (!pwm->title)
1227 pwm->title = xstrdup(N_("LibPWMD"));
1229 if (!pwm->prompt)
1230 pwm->prompt = xstrdup(N_("Passphrase:"));
1232 if (!pwm->desc && !which)
1233 pwm->desc = xstrdup(N_("Enter a passphrase."));
1235 if (which == 1) {
1236 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid passphrase, please try again."));
1237 buf = xstrdup(tmp);
1239 else if (which == 2) {
1240 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the passphrase again for confirmation."));
1241 buf = xstrdup(tmp);
1243 else {
1244 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
1245 sprintf(buf, "SETERROR %s", pwm->desc);
1248 error = pinentry_command(pwm, NULL, buf);
1249 xfree(buf);
1251 if (error)
1252 return error;
1254 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
1255 sprintf(buf, "SETPROMPT %s", pwm->prompt);
1256 error = pinentry_command(pwm, NULL, buf);
1257 xfree(buf);
1259 if (error)
1260 return error;
1262 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
1263 sprintf(buf, "SETDESC %s", pwm->title);
1264 error = pinentry_command(pwm, NULL, buf);
1265 xfree(buf);
1266 return error;
1269 static void update_pinentry_settings(pwm_t *pwm)
1271 FILE *fp;
1272 char buf[LINE_MAX];
1273 struct passwd *pw = getpwuid(getuid());
1274 char *p;
1276 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
1278 if ((fp = fopen(buf, "r")) == NULL)
1279 return;
1281 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
1282 char name[32], val[256];
1284 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
1285 continue;
1287 if (strcasecmp(name, "TTYNAME") == 0) {
1288 xfree(pwm->pinentry_tty);
1289 pwm->pinentry_tty = xstrdup(val);
1291 else if (strcasecmp(name, "TTYTYPE") == 0) {
1292 xfree(pwm->pinentry_term);
1293 pwm->pinentry_term = xstrdup(val);
1295 else if (strcasecmp(name, "DISPLAY") == 0) {
1296 xfree(pwm->pinentry_display);
1297 pwm->pinentry_display = xstrdup(val);
1299 else if (strcasecmp(name, "PATH") == 0) {
1300 xfree(pwm->pinentry_path);
1301 pwm->pinentry_path = xstrdup(val);
1305 fclose(fp);
1308 static gpg_error_t launch_pinentry(pwm_t *pwm)
1310 int rc;
1311 assuan_context_t ctx;
1312 int child_list[] = {-1};
1313 char *display = getenv("DISPLAY");
1314 const char *argv[10];
1315 const char **p = argv;
1316 int have_display = 0;
1317 char *tty = NULL;
1319 update_pinentry_settings(pwm);
1321 if (pwm->pinentry_display || display)
1322 have_display = 1;
1323 else {
1324 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
1326 if (!tty)
1327 return gpg_error_from_errno(errno);
1330 if (!have_display && !tty)
1331 return GPG_ERR_ENOTTY;
1333 *p++ = "pinentry";
1334 *p++ = have_display ? "--display" : "--ttyname";
1335 *p++ = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
1337 if (pwm->lcctype) {
1338 *p++ = "--lc-ctype";
1339 *p++ = pwm->lcctype;
1342 if (pwm->lcmessages) {
1343 *p++ = "--lc-messages";
1344 *p++ = pwm->lcmessages;
1347 *p = NULL;
1349 if (!have_display) {
1350 *p++ = "--ttytype";
1351 *p++ = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
1352 *p = NULL;
1355 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
1357 if (rc)
1358 return rc;
1360 pwm->pid = assuan_get_pid(ctx);
1361 pwm->pctx = ctx;
1362 return set_pinentry_strings(pwm, 0);
1365 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
1367 gpg_error_t n;
1369 if (!pwm->pctx) {
1370 n = launch_pinentry(pwm);
1372 if (n)
1373 return n;
1376 return assuan_command(pwm, pwm->pctx, result, cmd);
1379 static void pinentry_disconnect(pwm_t *pwm)
1381 if (pwm->pctx)
1382 assuan_disconnect(pwm->pctx);
1384 pwm->pctx = NULL;
1385 pwm->pid = -1;
1389 * Only called from a child process.
1391 static void catchsig(int sig)
1393 switch (sig) {
1394 case SIGALRM:
1395 if (gelapsed++ >= gtimeout) {
1396 global_error = pwmd_terminate_pinentry(gpwm);
1398 if (!global_error)
1399 global_error = GPG_ERR_TIMEOUT;
1401 break;
1404 alarm(1);
1405 break;
1406 default:
1407 break;
1410 #endif
1413 * Borrowed from libassuan.
1415 static char *percent_escape(const char *atext)
1417 const unsigned char *s;
1418 int len = strlen(atext) * 3 + 1;
1419 char *buf = (char *)xmalloc(len), *p = buf;
1421 if (!buf)
1422 return NULL;
1424 for (s=(const unsigned char *)atext; *s; s++) {
1425 if (*s < ' ') {
1426 sprintf (p, "%%%02X", *s);
1427 p += 3;
1429 else
1430 *p++ = *s;
1433 *p = 0;
1434 return buf;
1437 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
1439 if (!cmd)
1440 return GPG_ERR_INV_ARG;
1442 return assuan_command(pwm, pwm->ctx, result, cmd);
1445 gpg_error_t pwmd_command_ap(pwm_t *pwm, char **result, const char *cmd,
1446 va_list ap)
1448 char *buf;
1449 size_t len;
1450 gpg_error_t error;
1452 if (!pwm || !cmd)
1453 return GPG_ERR_INV_ARG;
1456 * C99 allows the dst pointer to be null which will calculate the length
1457 * of the would-be result and return it.
1459 len = vsnprintf(NULL, 0, cmd, ap)+1;
1460 buf = (char *)xmalloc(len);
1461 len = vsnprintf(buf, len, cmd, ap);
1462 error = send_command(pwm, result, buf);
1463 xfree(buf);
1464 return error;
1468 * Avoid sending the BYE command here. libassuan will close the file
1469 * descriptor and release the assuan context. Use pwmd_close() instead.
1471 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
1473 va_list ap;
1474 gpg_error_t error;
1476 if (!pwm || !cmd)
1477 return GPG_ERR_INV_ARG;
1479 *result = NULL;
1480 va_start(ap, cmd);
1481 error = pwmd_command_ap(pwm, result, cmd, ap);
1482 va_end(ap);
1483 return error;
1486 #ifdef WITH_PINENTRY
1487 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
1489 if (gtimeout) {
1490 signal(SIGALRM, catchsig);
1491 alarm(1);
1494 *result = NULL;
1495 return pinentry_command(pwm, result, "GETPIN");
1498 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
1500 int pin_try = *try_n;
1501 gpg_error_t error;
1503 getpin_again:
1504 *try_n = pin_try;
1506 if (pin_try == -1) {
1507 error = set_pinentry_strings(pwm, which);
1509 if (error) {
1510 pinentry_disconnect(pwm);
1511 return error;
1514 else {
1515 if (pwm->pinentry_tries-1 != pin_try) {
1516 error = set_pinentry_strings(pwm, 1);
1518 if (error) {
1519 pinentry_disconnect(pwm);
1520 return error;
1525 error = do_getpin(pwm, result);
1528 * Since there was input cancel any timeout setting.
1530 alarm(0);
1532 if (error) {
1533 if (error == GPG_ERR_CANCELED)
1534 return GPG_ERR_CANCELED;
1536 if (pin_try != -1 && pin_try--)
1537 goto getpin_again;
1539 if (pwm->pctx)
1540 pinentry_disconnect(pwm);
1542 *try_n = pin_try;
1543 return error;
1546 return 0;
1548 #endif
1550 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1552 gpg_error_t error;
1554 #ifndef WITH_PINENTRY
1555 return GPG_ERR_NOT_IMPLEMENTED;
1556 #endif
1558 if (!pwm || !pw || !pw->filename[0])
1559 return GPG_ERR_INV_ARG;
1561 close(pw->fd);
1563 if (pw->error) {
1564 error = pw->error;
1565 goto fail;
1568 if (pwm->filename)
1569 xfree(pwm->filename);
1571 pwm->filename = xstrdup(pw->filename);
1572 memset(pw, 0, sizeof(pwmd_nb_status_t));
1573 return 0;
1575 fail:
1576 memset(pw, 0, sizeof(pwmd_nb_status_t));
1577 return error;
1580 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
1582 char buf[ASSUAN_LINELENGTH];
1583 gpg_error_t error;
1584 char *result = NULL;
1586 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
1587 error = send_command(pwm, &result, buf);
1588 memset(buf, 0, sizeof(buf));
1590 if (error && result)
1591 xfree(result);
1593 return error;
1596 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
1597 int nb, int timeout)
1599 char *result = NULL;
1600 char *password = NULL;
1601 char path[PATH_MAX];
1602 #ifdef WITH_PINENTRY
1603 int pin_try;
1604 #endif
1606 if (!pwm || !filename || !*filename) {
1607 *error = GPG_ERR_INV_ARG;
1608 return nb ? -1 : 1;
1611 #ifdef WITH_PINENTRY
1612 pin_try = pwm->pinentry_tries - 1;
1613 #endif
1616 * Avoid calling pinentry if the password is cached on the server or if
1617 * this is a new file.
1619 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
1621 if (*error)
1622 return nb ? -1 : 1;
1624 snprintf(path, sizeof(path), "%s/%s", result, filename);
1625 pwmd_free_result(result);
1627 if (access(path, R_OK) == -1) {
1628 if (errno == ENOENT)
1629 goto gotpassword;
1632 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
1634 if (*error == EPWMD_CACHE_NOT_FOUND) {
1635 if (pwm->passfunc) {
1636 password = pwm->passfunc(pwm, pwm->passdata);
1637 goto gotpassword;
1640 #ifdef WITH_PINENTRY
1642 * Get the password from pinentry.
1644 if (pwm->use_pinentry) {
1646 * Nonblocking is wanted. fork() then return a file descriptor
1647 * that the client can use to read() from.
1649 if (nb) {
1650 int p[2];
1651 pid_t pid;
1652 pwmd_nb_status_t pw;
1654 if (pipe(p) == -1) {
1655 *error = gpg_error_from_syserror();
1656 return -1;
1659 #ifdef WITH_LIBPTH
1660 pid = pth_fork();
1661 #else
1662 pid = fork();
1663 #endif
1665 switch (pid) {
1666 case 0:
1667 close(p[0]);
1668 strncpy(pw.filename, filename, sizeof(pw.filename));
1669 pw.filename[sizeof(pw.filename)-1] = 0;
1670 pw.fd = p[0];
1672 if (timeout > 0) {
1673 gpwm = pwm;
1674 gtimeout = timeout;
1675 gelapsed = 0;
1678 getpin_nb_again:
1679 *error = getpin(pwm, &password, &pin_try, 0);
1681 if (*error) {
1682 getpin_nb_fail:
1683 if (pwm->pctx)
1684 pinentry_disconnect(pwm);
1686 if (gtimeout && gelapsed >= gtimeout)
1687 *error = GPG_ERR_TIMEOUT;
1689 pw.error = *error;
1690 #ifdef WITH_LIBPTH
1691 pth_write(p[1], &pw, sizeof(pw));
1692 #else
1693 write(p[1], &pw, sizeof(pw));
1694 #endif
1695 close(p[1]);
1696 _exit(1);
1700 * Don't count the time it takes to open the file
1701 * which may have many iterations.
1703 signal(SIGALRM, SIG_DFL);
1704 *error = do_open_command(pwm, filename, password);
1706 if (timeout)
1707 signal(SIGALRM, catchsig);
1709 if (pwm->pctx && *error == EPWMD_BADKEY) {
1710 if (pin_try-- > 0)
1711 goto getpin_nb_again;
1713 goto getpin_nb_fail;
1716 pinentry_disconnect(pwm);
1717 pw.error = 0;
1718 #ifdef WITH_LIBPTH
1719 pth_write(p[1], &pw, sizeof(pw));
1720 #else
1721 write(p[1], &pw, sizeof(pw));
1722 #endif
1723 close(p[1]);
1724 _exit(0);
1725 break;
1726 case -1:
1727 *error = gpg_error_from_syserror();
1728 close(p[0]);
1729 close(p[1]);
1730 return -1;
1731 default:
1732 break;
1735 close(p[1]);
1736 *error = 0;
1737 return p[0];
1740 else {
1741 #endif
1743 * Not using pinentry and the file was not found
1744 * in the cache.
1746 password = pwm->password;
1747 #ifdef WITH_PINENTRY
1749 #endif
1751 else if (*error)
1752 return nb ? -1 : 1;
1754 gotpassword:
1755 *error = do_open_command(pwm, filename, password);
1758 * Keep the user defined password set with pwmd_setopt(). The password may
1759 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1761 if (!pwm->passfunc && password && password != pwm->password)
1762 xfree(password);
1764 #ifdef WITH_PINENTRY
1765 if (*error == EPWMD_BADKEY) {
1766 if (pin_try-- > 0 && !nb) {
1767 *error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
1768 N_("Invalid passphrase, please try again."));
1770 if (*error)
1771 return 1;
1773 goto gotpassword;
1776 if (nb)
1777 pinentry_disconnect(pwm);
1779 return nb ? -1 : 1;
1781 #endif
1783 if (!*error) {
1784 if (pwm->filename)
1785 xfree(pwm->filename);
1787 pwm->filename = xstrdup(filename);
1791 * The file is cached or the file is a new file.
1793 if (nb)
1794 return *error ? -1 : -2;
1796 return *error ? 1 : 0;
1799 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1801 gpg_error_t error;
1803 do_pwmd_open(pwm, &error, filename, 0, 0);
1804 return error;
1807 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1808 int timeout)
1810 #ifndef WITH_PINENTRY
1811 *error = GPG_ERR_NOT_IMPLEMENTED;
1812 return -1;
1813 #else
1814 return do_pwmd_open(pwm, error, filename, 1, timeout);
1815 #endif
1818 #ifdef WITH_PINENTRY
1819 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1821 int confirm = 0;
1822 gpg_error_t error;
1823 char *result = NULL;
1824 int pin_try = -1;
1826 again:
1827 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1829 if (error) {
1830 if (pwm->pctx)
1831 pinentry_disconnect(pwm);
1833 if (*password)
1834 xfree(*password);
1836 return error;
1839 if (!confirm++) {
1840 *password = result;
1841 goto again;
1844 if (strcmp(*password, result)) {
1845 xfree(*password);
1846 xfree(result);
1847 pinentry_disconnect(pwm);
1848 error = EPWMD_BADKEY;
1849 return error;
1852 xfree(result);
1853 pinentry_disconnect(pwm);
1854 return 0;
1856 #endif
1858 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1860 char buf[ASSUAN_LINELENGTH];
1861 gpg_error_t error;
1862 char *result = NULL;
1864 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1865 error = send_command(pwm, &result, buf);
1866 memset(&buf, 0, sizeof(buf));
1868 if (error && result)
1869 xfree(result);
1871 return error;
1874 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1876 gpg_error_t rc;
1878 #ifndef WITH_PINENTRY
1879 return GPG_ERR_NOT_IMPLEMENTED;
1880 #endif
1882 if (!pwm || !pw || !pw->filename[0])
1883 return GPG_ERR_INV_ARG;
1885 close(pw->fd);
1886 rc = pw->error;
1887 memset(pw, 0, sizeof(pwmd_nb_status_t));
1888 return rc;
1891 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1893 char *result = NULL;
1894 char *password = NULL;
1896 if (!pwm) {
1897 *error = GPG_ERR_INV_ARG;
1898 return nb ? -1 : 1;
1901 if (pwm->use_pinentry || pwm->passfunc) {
1902 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1904 if (*error == EPWMD_CACHE_NOT_FOUND) {
1905 if (pwm->passfunc)
1906 password = (*pwm->passfunc)(pwm, pwm->passdata);
1907 #ifdef WITH_PINENTRY
1908 else if (pwm->use_pinentry) {
1909 if (nb) {
1910 int p[2];
1911 pid_t pid;
1912 pwmd_nb_status_t pw;
1914 if (pipe(p) == -1) {
1915 *error = gpg_error_from_syserror();
1916 return -1;
1919 #ifdef WITH_LIBPTH
1920 pid = pth_fork();
1921 #else
1922 pid = fork();
1923 #endif
1925 switch (pid) {
1926 case 0:
1927 close(p[0]);
1928 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1929 pw.filename[sizeof(pw.filename)-1] = 0;
1930 pw.fd = p[0];
1932 do {
1933 password = NULL;
1934 *error = do_save_getpin(pwm, &password);
1935 } while (*error == EPWMD_BADKEY);
1937 if (*error) {
1938 if (pwm->pctx)
1939 pinentry_disconnect(pwm);
1941 pw.error = *error;
1942 #ifdef WITH_LIBPTH
1943 pth_write(p[1], &pw, sizeof(pw));
1944 #else
1945 write(p[1], &pw, sizeof(pw));
1946 #endif
1947 close(p[1]);
1948 _exit(1);
1951 *error = do_save_command(pwm, password);
1952 pinentry_disconnect(pwm);
1953 pw.error = *error;
1954 #ifdef WITH_LIBPTH
1955 pth_write(p[1], &pw, sizeof(pw));
1956 #else
1957 write(p[1], &pw, sizeof(pw));
1958 #endif
1959 close(p[1]);
1960 _exit(0);
1961 break;
1962 case -1:
1963 *error = gpg_error_from_syserror();
1964 close(p[0]);
1965 close(p[1]);
1966 return -1;
1967 default:
1968 break;
1971 close(p[1]);
1972 *error = 0;
1973 return p[0];
1976 *error = do_save_getpin(pwm, &password);
1978 if (*error)
1979 return 1;
1981 #endif
1983 else {
1984 if (*error)
1985 return nb ? -1 : 1;
1988 else
1989 password = pwm->password;
1991 *error = do_save_command(pwm, password);
1993 if (!pwm->passfunc && password && password != pwm->password)
1994 xfree(password);
1996 if (nb)
1997 return *error ? -1 : -2;
1999 return *error ? 1 : 0;
2002 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
2004 #ifndef WITH_PINENTRY
2005 *error = GPG_ERR_NOT_IMPLEMENTED;
2006 return -1;
2007 #else
2008 return do_pwmd_save(pwm, error, 1);
2009 #endif
2012 gpg_error_t pwmd_save(pwm_t *pwm)
2014 gpg_error_t error;
2016 do_pwmd_save(pwm, &error, 0);
2017 return error;
2020 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
2022 va_list ap;
2023 int n = va_arg(ap, int);
2024 char *result;
2025 char *arg1;
2026 gpg_error_t error = 0;
2028 if (!pwm)
2029 return GPG_ERR_INV_ARG;
2031 va_start(ap, opt);
2033 switch (opt) {
2034 case PWMD_OPTION_STATUS_FUNC:
2035 pwm->status_func = va_arg(ap, pwmd_status_fn);
2036 break;
2037 case PWMD_OPTION_STATUS_DATA:
2038 pwm->status_data = va_arg(ap, void *);
2039 break;
2040 case PWMD_OPTION_PASSWORD_FUNC:
2041 pwm->passfunc = va_arg(ap, pwmd_password_fn);
2042 break;
2043 case PWMD_OPTION_PASSWORD_DATA:
2044 pwm->passdata = va_arg(ap, void *);
2045 break;
2046 case PWMD_OPTION_PASSWORD:
2047 arg1 = va_arg(ap, char *);
2049 if (pwm->password)
2050 xfree(pwm->password);
2052 pwm->password = xstrdup(arg1);
2053 break;
2054 case PWMD_OPTION_PINENTRY:
2055 n = va_arg(ap, int);
2057 if (n != 0 && n != 1) {
2058 va_end(ap);
2059 error = GPG_ERR_INV_VALUE;
2061 else {
2062 pwm->use_pinentry = n;
2063 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
2064 !pwm->use_pinentry);
2066 break;
2067 #ifdef WITH_PINENTRY
2068 case PWMD_OPTION_PINENTRY_TRIES:
2069 n = va_arg(ap, int);
2071 if (n <= 0) {
2072 va_end(ap);
2073 error = GPG_ERR_INV_VALUE;
2075 else
2076 pwm->pinentry_tries = n;
2077 break;
2078 #endif
2079 case PWMD_OPTION_PINENTRY_TIMEOUT:
2080 n = va_arg(ap, int);
2082 if (n < 0) {
2083 va_end(ap);
2084 error = GPG_ERR_INV_VALUE;
2086 else
2087 pwm->pinentry_timeout = n;
2089 if (!pwm->use_pinentry)
2090 error = pwmd_command(pwm, &result, "OPTION TIMEOUT=%i",
2091 pwm->pinentry_timeout);
2092 break;
2093 case PWMD_OPTION_PINENTRY_PATH:
2094 if (pwm->pinentry_path)
2095 xfree(pwm->pinentry_path);
2097 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
2099 if (!pwm->use_pinentry)
2100 error = pwmd_command(pwm, &result, "OPTION PATH=%s",
2101 pwm->pinentry_path);
2102 break;
2103 case PWMD_OPTION_PINENTRY_TTY:
2104 if (pwm->pinentry_tty)
2105 xfree(pwm->pinentry_tty);
2107 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
2109 if (!pwm->use_pinentry)
2110 error = pwmd_command(pwm, &result, "OPTION TTY=%s",
2111 pwm->pinentry_tty);
2112 break;
2113 case PWMD_OPTION_PINENTRY_DISPLAY:
2114 if (pwm->pinentry_display)
2115 xfree(pwm->pinentry_display);
2117 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
2119 if (!pwm->use_pinentry)
2120 error = pwmd_command(pwm, &result, "OPTION DISPLAY=%s",
2121 pwm->pinentry_display);
2122 break;
2123 case PWMD_OPTION_PINENTRY_TERM:
2124 if (pwm->pinentry_term)
2125 xfree(pwm->pinentry_term);
2127 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
2129 if (!pwm->use_pinentry)
2130 error = pwmd_command(pwm, &result, "OPTION TTYTYPE=%s",
2131 pwm->pinentry_term);
2132 break;
2133 case PWMD_OPTION_PINENTRY_TITLE:
2134 if (pwm->title)
2135 xfree(pwm->title);
2137 pwm->title = percent_escape(va_arg(ap, char *));
2139 if (!pwm->use_pinentry)
2140 error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
2141 pwm->title);
2142 break;
2143 case PWMD_OPTION_PINENTRY_PROMPT:
2144 if (pwm->prompt)
2145 xfree(pwm->prompt);
2147 pwm->prompt = percent_escape(va_arg(ap, char *));
2149 if (!pwm->use_pinentry)
2150 error = pwmd_command(pwm, &result, "OPTION PROMPT=%s",
2151 pwm->prompt);
2152 break;
2153 case PWMD_OPTION_PINENTRY_DESC:
2154 if (pwm->desc)
2155 xfree(pwm->desc);
2157 pwm->desc = percent_escape(va_arg(ap, char *));
2159 if (!pwm->use_pinentry)
2160 error = pwmd_command(pwm, &result, "OPTION DESC=%s",
2161 pwm->desc);
2162 break;
2163 case PWMD_OPTION_PINENTRY_LC_CTYPE:
2164 if (pwm->lcctype)
2165 xfree(pwm->lcctype);
2167 pwm->lcctype = xstrdup(va_arg(ap, char *));
2169 if (!pwm->use_pinentry)
2170 error = pwmd_command(pwm, &result, "OPTION LC_CTYPE=%s",
2171 pwm->lcctype);
2172 break;
2173 case PWMD_OPTION_PINENTRY_LC_MESSAGES:
2174 if (pwm->lcmessages)
2175 xfree(pwm->lcmessages);
2177 pwm->lcmessages = xstrdup(va_arg(ap, char *));
2179 if (!pwm->use_pinentry)
2180 error = pwmd_command(pwm, &result, "OPTION LC_MESSAGES=%s",
2181 pwm->lcmessages);
2182 break;
2183 default:
2184 error = GPG_ERR_NOT_IMPLEMENTED;
2185 break;
2188 va_end(ap);
2189 return error;
2193 * Prevent requiring assuan.h when setting ctx. The ctx is really an
2194 * assuan_context_t *.
2196 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, void *ctx, int *fd)
2198 if (!pwm)
2199 return GPG_ERR_INV_ARG;
2201 ctx = pwm->ctx;
2202 *fd = pwm->fd;
2203 return 0;