No need to usleep() in inquire_cb() after _pwmd_process(). Send the data
[libpwmd.git] / src / libpwmd.c
blob3212b1f3fcce3c82c21175c2f2371235bcf70180
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 <libpwmd.h>
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
44 #ifdef HAVE_ASSUAN_H
45 #include <assuan.h>
46 #endif
48 #include "mem.h"
49 #include "misc.h"
50 #include "types.h"
52 #ifdef WITH_PINENTRY
53 #include "pinentry.h"
54 #endif
56 #ifdef WITH_TCP
57 #include "ssh.h"
58 #endif
60 static gpg_error_t send_pinentry_options(pwm_t *pwm);
61 static gpg_error_t _pwmd_process(pwm_t *pwm);
62 static gpg_error_t set_pinentry_retry(pwm_t *pwm);
63 static gpg_error_t get_custom_passphrase(pwm_t *pwm, char **result);
65 static const char *_pwmd_strerror(gpg_error_t e)
67 gpg_err_code_t code = gpg_err_code(e);
69 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
70 switch (code) {
71 case GPG_ERR_USER_1:
72 return N_("No file is open");
73 case GPG_ERR_USER_2:
74 return N_("General LibXML error");
75 case GPG_ERR_USER_3:
76 return N_("File modified");
77 default:
78 return N_("Unknown error");
82 return NULL;
85 const char *pwmd_strerror(gpg_error_t code)
87 const char *p = _pwmd_strerror(code);
89 return p ? p : gpg_strerror(code);
92 int pwmd_strerror_r(gpg_error_t code, char *buf, size_t size)
94 const char *p = _pwmd_strerror(code);
96 if (p) {
97 snprintf(buf, size, "%s", p);
99 if (strlen(p) > size)
100 return ERANGE;
102 return 0;
105 return gpg_strerror_r(code, buf, size);
108 gpg_error_t pwmd_init()
110 static int initialized;
112 if (initialized)
113 return 0;
115 #ifndef MEM_DEBUG
116 _xmem_init();
117 #endif
118 #ifdef ENABLE_NLS
119 bindtextdomain("libpwmd", LOCALEDIR);
120 #endif
121 gpg_err_init();
122 assuan_set_malloc_hooks(pwmd_malloc, pwmd_realloc, pwmd_free);
123 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
124 initialized = 1;
125 return 0;
128 gpg_error_t _connect_finalize(pwm_t *pwm)
130 int active[2];
131 int n = assuan_get_active_fds(pwm->ctx, 0, active, N_ARRAY(active));
132 gpg_error_t rc;
134 if (n <= 0)
135 return GPG_ERR_EBADFD;
137 pwm->fd = active[0];
138 #ifdef WITH_PINENTRY
139 pwm->pid = -1;
140 #endif
141 assuan_set_pointer(pwm->ctx, pwm);
143 #ifdef WITH_TCP
144 // Until X11 forwarding is supported, disable the remote pwmd pinentry.
145 if (pwm->tcp_conn) {
146 pwm->tcp_conn->state = SSH_NONE;
147 rc = pwmd_command(pwm, NULL, "SET ENABLE_PINENTRY=0");
149 if (rc)
150 return rc;
152 #endif
154 if (pwm->name) {
155 rc = pwmd_command(pwm, NULL, "SET NAME=%s", pwm->name);
157 if (rc)
158 return rc;
161 return 0;
164 static gpg_error_t _pwmd_connect_url(pwm_t *pwm, const char *url, int async)
166 char *p = (char *)url;
167 gpg_error_t rc;
169 if (!pwm)
170 return GPG_ERR_INV_ARG;
172 if (!p || !strncmp(p, "file://", 7) || !strncmp(p, "local://", 8)) {
173 if (p) {
174 if (!strncmp(p, "file://", 7))
175 p += 7;
176 else
177 p += 8;
180 goto connect_uds;
182 else if (!strncmp(p, "ssh://", 6) || !strncmp(p, "ssh6://", 7) ||
183 !strncmp(p, "ssh4://", 7)) {
184 #ifndef WITH_TCP
185 return GPG_ERR_NOT_IMPLEMENTED;
186 #else
187 char *host = NULL;
188 int port = -1;
189 char *identity = NULL;
190 char *known_hosts = NULL;
191 char *username = NULL;
193 if (!strncmp(p, "ssh6://", 7)) {
194 rc = pwmd_setopt(pwm, PWMD_OPTION_IP_VERSION, PWMD_IPV6);
195 p += 7;
197 else if (!strncmp(p, "ssh4://", 7)) {
198 rc = pwmd_setopt(pwm, PWMD_OPTION_IP_VERSION, PWMD_IPV4);
199 p += 7;
201 else {
202 rc = pwmd_setopt(pwm, PWMD_OPTION_IP_VERSION, PWMD_IP_ANY);
203 p += 6;
206 if (rc)
207 return rc;
209 rc = _parse_ssh_url(p, &host, &port, &username, &identity,
210 &known_hosts);
212 if (rc)
213 goto fail;
215 if (async)
216 rc = pwmd_ssh_connect_async(pwm, host, port, identity, username,
217 known_hosts);
218 else
219 rc = pwmd_ssh_connect(pwm, host, port, identity, username,
220 known_hosts);
222 fail:
223 if (host)
224 pwmd_free(host);
226 if (username)
227 pwmd_free(username);
229 if (identity)
230 pwmd_free(identity);
232 if (known_hosts)
233 pwmd_free(known_hosts);
235 return rc;
236 #endif
239 connect_uds:
240 rc = pwmd_connect(pwm, p);
241 pwm->state = ASYNC_DONE;
242 return rc;
245 gpg_error_t pwmd_connect_url(pwm_t *pwm, const char *url)
247 return _pwmd_connect_url(pwm, url, 0);
250 gpg_error_t pwmd_connect_url_async(pwm_t *pwm, const char *url)
252 return _pwmd_connect_url(pwm, url, 1);
255 gpg_error_t pwmd_connect(pwm_t *pwm, const char *path)
257 char *socketpath = NULL;
258 assuan_context_t ctx;
259 struct passwd pw;
260 char *pwbuf;
261 gpg_error_t rc;
263 if (!pwm)
264 return GPG_ERR_INV_ARG;
266 pwbuf = _getpwuid(&pw);
268 if (!pwbuf)
269 return gpg_error_from_errno(errno);
271 if (!path || !*path)
272 socketpath = pwmd_strdup_printf("%s/.pwmd/socket", pw.pw_dir);
273 else
274 socketpath = _expand_homedir((char *)path, &pw);
276 pwmd_free(pwbuf);
278 if (!socketpath)
279 return gpg_error_from_errno(ENOMEM);
281 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
282 pwmd_free(socketpath);
284 if (rc)
285 return gpg_err_code(rc);
287 pwm->ctx = ctx;
288 return _connect_finalize(pwm);
291 static void disconnect(pwm_t *pwm)
293 if (!pwm || !pwm->ctx)
294 return;
296 assuan_disconnect(pwm->ctx);
297 pwm->ctx = NULL;
298 pwm->fd = -1;
301 void pwmd_close(pwm_t *pwm)
303 if (!pwm)
304 return;
306 disconnect(pwm);
308 if (pwm->password)
309 pwmd_free(pwm->password);
311 if (pwm->title)
312 pwmd_free(pwm->title);
314 if (pwm->desc)
315 pwmd_free(pwm->desc);
317 if (pwm->prompt)
318 pwmd_free(pwm->prompt);
320 if (pwm->pinentry_tty)
321 pwmd_free(pwm->pinentry_tty);
323 if (pwm->pinentry_display)
324 pwmd_free(pwm->pinentry_display);
326 if (pwm->pinentry_term)
327 pwmd_free(pwm->pinentry_term);
329 if (pwm->lcctype)
330 pwmd_free(pwm->lcctype);
332 if (pwm->lcmessages)
333 pwmd_free(pwm->lcmessages);
335 if (pwm->filename)
336 pwmd_free(pwm->filename);
338 if (pwm->name)
339 pwmd_free(pwm->name);
341 #ifdef WITH_TCP
342 if (pwm->tcp_conn)
343 _free_ssh_conn(pwm->tcp_conn);
344 #endif
346 #ifdef WITH_PINENTRY
347 if (pwm->pctx)
348 _pinentry_disconnect(pwm);
349 #endif
351 pwmd_free(pwm);
354 static gpg_error_t do_async_command(pwm_t *pwm, char **result)
356 pwmd_async_t s;
357 gpg_error_t rc;
359 do {
360 s = pwmd_process(pwm, &rc, result);
362 if (s != ASYNC_DONE) {
363 #ifdef WITH_LIBPTH
364 pth_usleep(50000);
365 #else
366 usleep(50000);
367 #endif
369 } while (s != ASYNC_DONE);
371 return rc;
374 gpg_error_t pwmd_ssh_connect_async(pwm_t *pwm, const char *host, int port,
375 const char *identity, const char *user, const char *known_hosts)
377 #ifndef WITH_TCP
378 return GPG_ERR_NOT_IMPLEMENTED;
379 #else
380 return _do_pwmd_ssh_connect_async(pwm, host, port, identity, user,
381 known_hosts, ASYNC_CMD_CONNECT);
382 #endif
385 gpg_error_t pwmd_ssh_connect(pwm_t *pwm, const char *host, int port,
386 const char *identity, const char *user, const char *known_hosts)
388 #ifndef WITH_TCP
389 return GPG_ERR_NOT_IMPLEMENTED;
390 #else
391 gpg_error_t rc;
393 rc = _do_pwmd_ssh_connect_async(pwm, host, port, identity, user,
394 known_hosts, ASYNC_CMD_CONNECT);
395 return rc || do_async_command(pwm, NULL);
396 #endif
399 gpg_error_t pwmd_get_hostkey(pwm_t *pwm, const char *host, int port,
400 char **result)
402 #ifndef WITH_TCP
403 return GPG_ERR_NOT_IMPLEMENTED;
404 #else
405 gpg_error_t rc;
407 rc = _do_pwmd_ssh_connect_async(pwm, host, port, NULL, NULL, NULL,
408 ASYNC_CMD_HOSTKEY);
410 return rc || do_async_command(pwm, result);
411 #endif
414 gpg_error_t pwmd_get_hostkey_async(pwm_t *pwm, const char *host, int port)
416 #ifndef WITH_TCP
417 return GPG_ERR_NOT_IMPLEMENTED;
418 #else
419 return _do_pwmd_ssh_connect_async(pwm, host, port, NULL, NULL, NULL,
420 ASYNC_CMD_HOSTKEY);
421 #endif
424 static int inquire_realloc_cb(void *data, const void *buffer, size_t len)
426 membuf_t *mem = (membuf_t *)data;
427 void *p;
429 if (!buffer)
430 return 0;
432 if ((p = pwmd_realloc(mem->buf, mem->len + len)) == NULL)
433 return gpg_error_from_errno(ENOMEM);
435 mem->buf = p;
436 memcpy((char *)mem->buf + mem->len, buffer, len);
437 mem->len += len;
438 return 0;
441 static int inquire_cb(void *data, const char *keyword)
443 pwm_t *pwm = (pwm_t *)data;
444 gpg_error_t rc = 0;
446 /* Shouldn't get this far without a callback. */
447 if (!pwm->inquire_func)
448 return GPG_ERR_INV_ARG;
450 for (;;) {
451 char *result = NULL;
452 size_t len;
453 gpg_error_t arc;
455 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
456 rc = gpg_err_code(rc);
458 if (rc == GPG_ERR_EOF || !rc) {
459 if (len <= 0 || !result) {
460 rc = 0;
461 break;
464 arc = assuan_send_data(pwm->ctx, result, len);
465 arc = gpg_err_code(arc);
467 if (rc == GPG_ERR_EOF) {
468 rc = arc;
469 break;
472 rc = arc;
474 else if (rc)
475 break;
477 if (!rc) {
478 pwm->inquire_sent += len;
480 if (pwm->status_func) {
481 char buf[ASSUAN_LINELENGTH];
483 snprintf(buf, sizeof(buf), "XFER %u %u", pwm->inquire_sent,
484 pwm->inquire_total);
485 rc = pwm->status_func(pwm->status_data, buf);
487 if (rc)
488 continue;
491 rc = _pwmd_process(pwm);
493 if (rc == GPG_ERR_EAGAIN)
494 rc = 0;
498 return gpg_err_code(rc);
501 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, ...)
503 char *buf;
504 gpg_error_t rc;
505 va_list ap;
506 int len;
508 if (pwm->state == ASYNC_DONE)
509 pwm->state = ASYNC_INIT;
511 if (pwm->state != ASYNC_INIT)
512 return GPG_ERR_INV_STATE;
514 buf = pwmd_malloc(ASSUAN_LINELENGTH+1);
516 if (!buf)
517 return gpg_error_from_errno(ENOMEM);
519 va_start(ap, cmd);
520 len = vsnprintf(buf, ASSUAN_LINELENGTH+1, cmd, ap);
521 va_end(ap);
523 if (len >= ASSUAN_LINELENGTH+1) {
524 pwmd_free(buf);
525 return GPG_ERR_LINE_TOO_LONG;
528 rc = assuan_write_line(pwm->ctx, buf);
529 pwmd_free(buf);
531 if (!rc)
532 pwm->state = ASYNC_PROCESS;
534 return gpg_err_code(rc);
537 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
539 char *p = NULL;
540 const char *f = NULL;
541 gpg_error_t rc;
543 if (!pwm || !filename)
544 return GPG_ERR_INV_ARG;
546 if (!pwm->ctx)
547 return GPG_ERR_INV_STATE;
549 if (pwm->cmd != ASYNC_CMD_NONE)
550 return GPG_ERR_ASS_NESTED_COMMANDS;
552 if (pwm->lastcmd == ASYNC_CMD_NONE) {
553 pwm->pin_try = 0;
555 if (pwm->filename)
556 pwmd_free(pwm->filename);
558 pwm->filename = pwmd_strdup(filename);
560 if (!pwm->filename)
561 return gpg_error_from_errno(ENOMEM);
563 gpg_error_t rc = send_pinentry_options(pwm);
565 if (rc)
566 return rc;
568 rc = get_custom_passphrase(pwm, &p);
570 if (rc && rc != GPG_ERR_NO_DATA)
571 return rc;
573 f = filename;
575 #ifdef WITH_PINENTRY
576 else if (pwm->lastcmd == ASYNC_CMD_OPEN2) {
577 p = pwm->_password;
578 f = pwm->filename;
580 #endif
581 else if (pwm->lastcmd == ASYNC_CMD_OPEN) {
582 rc = set_pinentry_retry(pwm);
584 if (rc)
585 return rc;
587 p = pwm->password;
588 f = filename;
590 else
591 return GPG_ERR_INV_STATE;
593 pwm->cmd = ASYNC_CMD_OPEN;
594 return do_nb_command(pwm, "OPEN %s %s", f, p ? p : "");
597 gpg_error_t pwmd_save_async(pwm_t *pwm)
599 char *p = NULL;
601 if (!pwm)
602 return GPG_ERR_INV_ARG;
604 if (!pwm->ctx)
605 return GPG_ERR_INV_STATE;
607 if (pwm->cmd != ASYNC_CMD_NONE)
608 return GPG_ERR_ASS_NESTED_COMMANDS;
610 if (pwm->lastcmd != ASYNC_CMD_SAVE2) {
611 gpg_error_t rc = send_pinentry_options(pwm);
613 if (rc)
614 return rc;
616 rc = get_custom_passphrase(pwm, &p);
618 if (rc && rc != GPG_ERR_NO_DATA)
619 return rc;
621 #ifdef WITH_PINENTRY
622 else
623 p = pwm->_password;
624 #endif
626 pwm->cmd = ASYNC_CMD_SAVE;
627 return do_nb_command(pwm, "SAVE %s", p ? p : "");
630 static gpg_error_t parse_assuan_line(pwm_t *pwm)
632 gpg_error_t rc;
633 char *line;
634 size_t len;
636 rc = assuan_read_line(pwm->ctx, &line, &len);
638 if (!rc) {
639 if (line[0] == 'O' && line[1] == 'K' &&
640 (line[2] == 0 || line[2] == ' ')) {
641 pwm->state = ASYNC_DONE;
643 else if (line[0] == '#') {
645 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
646 if (pwm->status_func) {
647 rc = pwm->status_func(pwm->status_data,
648 line[1] == 0 ? line+1 : line+2);
651 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
652 (line[3] == 0 || line[3] == ' ')) {
653 line += 4;
654 rc = atoi(line);
655 pwm->state = ASYNC_DONE;
659 return gpg_err_code(rc);
662 gpg_error_t pwmd_pending_line(pwm_t *pwm)
664 if (!pwm)
665 return GPG_ERR_INV_ARG;
667 if (!pwm->ctx)
668 return GPG_ERR_INV_STATE;
670 return assuan_pending_line(pwm->ctx) ? 0 : GPG_ERR_NO_DATA;
673 static pwmd_async_t reset_async(pwm_t *pwm, int done)
675 pwm->state = ASYNC_INIT;
676 pwm->cmd = pwm->lastcmd = ASYNC_CMD_NONE;
678 #ifdef WITH_PINENTRY
679 if (pwm->nb_fd != -1) {
680 close(pwm->nb_fd);
681 pwm->nb_fd = -1;
684 if (pwm->_password) {
685 pwmd_free(pwm->_password);
686 pwm->_password = NULL;
688 #endif
689 #ifdef WITH_TCP
690 if (pwm->tcp_conn)
691 pwm->tcp_conn->rc = 0;
693 if (done && pwm->tcp_conn) {
694 _free_ssh_conn(pwm->tcp_conn);
695 pwm->tcp_conn = NULL;
697 #endif
699 return ASYNC_DONE;
703 * Used for processing status messages when not in an async command and for
704 * waiting for the result from pwmd_open_async() and pwmd_save_async().
706 static gpg_error_t _pwmd_process(pwm_t *pwm)
708 gpg_error_t rc = 0;
709 fd_set fds;
710 struct timeval tv = {0, 0};
711 int n;
713 FD_ZERO(&fds);
714 FD_SET(pwm->fd, &fds);
715 #ifdef WITH_LIBPTH
716 n = pth_select(pwm->fd+1, &fds, NULL, NULL, &tv);
717 #else
718 n = select(pwm->fd+1, &fds, NULL, NULL, &tv);
719 #endif
721 if (n == -1)
722 return gpg_error_from_syserror();
724 if (n > 0) {
725 if (FD_ISSET(pwm->fd, &fds))
726 rc = parse_assuan_line(pwm);
729 while (!rc && assuan_pending_line(pwm->ctx))
730 rc = parse_assuan_line(pwm);
732 return gpg_err_code(rc);
735 static void reset_handle(pwm_t *h)
737 h->fd = -1;
738 #ifdef WITH_PINENTRY
739 if (h->pctx)
740 _pinentry_disconnect(h);
742 h->nb_fd = -1;
743 #endif
744 h->pin_try = 0;
745 reset_async(h, 0);
748 gpg_error_t pwmd_disconnect(pwm_t *pwm)
750 if (!pwm)
751 return GPG_ERR_INV_ARG;
753 #ifdef WITH_TCP
754 if (pwm->fd == -1 && pwm->tcp_conn && pwm->tcp_conn->fd == -1)
755 #else
756 if (pwm->fd == -1)
757 #endif
758 return GPG_ERR_INV_STATE;
760 if (pwm->fd != 1)
761 disconnect(pwm);
762 #ifdef WITH_TCP
763 else
764 _ssh_disconnect(pwm);
765 #endif
767 reset_handle(pwm);
768 return 0;
771 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc, char **result)
773 #if defined(WITH_PINENTRY) || defined(WITH_TCP)
774 fd_set fds;
775 int n;
776 struct timeval tv = {0, 0};
777 #endif
779 if (result)
780 *result = NULL;
782 if (!rc)
783 return GPG_ERR_INV_ARG;
785 *rc = 0;
787 if (!pwm) {
788 *rc = GPG_ERR_INV_ARG;
789 return ASYNC_DONE;
791 else if (!pwm->ctx) {
792 switch (pwm->cmd) {
793 default:
794 *rc = GPG_ERR_INV_STATE;
795 return ASYNC_DONE;
796 #ifdef WITH_TCP
797 case ASYNC_CMD_DNS:
798 case ASYNC_CMD_CONNECT:
799 case ASYNC_CMD_HOSTKEY:
800 break;
801 #endif
805 /* When not in a command, this will let libassuan process status messages
806 * by calling PWMD_OPTION_STATUS_FUNC. The client can poll the file
807 * descriptor returned by pwmd_get_fd() to determine when this should be
808 * called or call pwmd_pending_line() to determine whether a buffered line
809 * needs to be processed. */
810 if (pwm->cmd == ASYNC_CMD_NONE) {
811 *rc = _pwmd_process(pwm);
812 return ASYNC_DONE;
815 /* Fixes pwmd_open/save_async2() when there is a cached or new file. */
816 if (pwm->state == ASYNC_DONE) {
817 *rc = _pwmd_process(pwm);
818 return reset_async(pwm, 0);
821 if (pwm->state != ASYNC_PROCESS) {
822 *rc = GPG_ERR_INV_STATE;
823 return ASYNC_DONE;
826 #ifdef WITH_TCP
827 if (pwm->cmd == ASYNC_CMD_DNS) {
828 fd_set rfds, wfds;
830 if (pwm->tcp_conn->rc) {
831 *rc = pwm->tcp_conn->rc;
832 return reset_async(pwm, 1);
835 FD_ZERO(&rfds);
836 FD_ZERO(&wfds);
837 n = ares_fds(pwm->tcp_conn->chan, &rfds, &wfds);
839 /* Shouldn't happen. */
840 if (!n)
841 return pwm->state;
843 #ifdef WITH_LIBPTH
844 n = pth_select(n, &rfds, &wfds, NULL, &tv);
845 #else
846 n = select(n, &rfds, &wfds, NULL, &tv);
847 #endif
849 if (n < 0) {
850 *rc = gpg_error_from_syserror();
851 return reset_async(pwm, 1);
854 if (n > 0)
855 ares_process(pwm->tcp_conn->chan, &rfds, &wfds);
857 return pwm->state;
859 else if (pwm->cmd == ASYNC_CMD_CONNECT) {
860 if (pwm->tcp_conn->rc == GPG_ERR_EINPROGRESS) {
861 int ret;
862 socklen_t len = sizeof(int);
864 FD_ZERO(&fds);
865 FD_SET(pwm->tcp_conn->fd, &fds);
866 #ifdef WITH_LIBPTH
867 n = pth_select(pwm->tcp_conn->fd+1, NULL, &fds, NULL, &tv);
868 #else
869 n = select(pwm->tcp_conn->fd+1, NULL, &fds, NULL, &tv);
870 #endif
872 if (!n || !FD_ISSET(pwm->tcp_conn->fd, &fds))
873 return pwm->state;
874 else if (n == -1) {
875 *rc = gpg_error_from_syserror();
876 return reset_async(pwm, 1);
879 ret = getsockopt(pwm->tcp_conn->fd, SOL_SOCKET, SO_ERROR, &n, &len);
881 if (ret || n) {
882 *rc = ret ? gpg_error_from_syserror() : gpg_error_from_errno(n);
883 return reset_async(pwm, 1);
886 pwm->tcp_conn->state = SSH_NONE;
887 pwm->tcp_conn->rc = 0;
888 *rc = _setup_ssh_session(pwm);
890 if (*rc && *rc != GPG_ERR_EAGAIN)
891 return reset_async(pwm, 1);
893 else if (pwm->tcp_conn->rc) {
894 *rc = pwm->tcp_conn->rc;
895 return reset_async(pwm, 1);
898 switch (pwm->tcp_conn->state) {
899 case SSH_INIT:
900 *rc = _setup_ssh_init(pwm);
901 break;
902 case SSH_AUTHLIST:
903 *rc = _setup_ssh_authlist(pwm);
904 break;
905 case SSH_AUTH:
906 *rc = _setup_ssh_auth(pwm);
907 break;
908 case SSH_CHANNEL:
909 *rc = _setup_ssh_channel(pwm);
910 break;
911 case SSH_SHELL:
912 *rc = _setup_ssh_shell(pwm);
913 break;
914 default:
915 break;
918 if (*rc == GPG_ERR_EAGAIN) {
919 *rc = 0;
920 return ASYNC_PROCESS;
923 if (!*rc) {
924 switch (pwm->tcp_conn->cmd) {
925 case ASYNC_CMD_HOSTKEY:
926 *result = pwmd_strdup(pwm->tcp_conn->hostkey);
928 if (!*result)
929 *rc = GPG_ERR_ENOMEM;
930 break;
931 default:
932 break;
936 return reset_async(pwm, *rc ? 1 : 0);
938 #endif
940 #ifdef WITH_PINENTRY
941 if (pwm->cmd == ASYNC_CMD_OPEN2 || pwm->cmd == ASYNC_CMD_SAVE2) {
942 int status;
944 if (pwm->nb_fd == -1) {
945 *rc = GPG_ERR_INV_STATE;
946 return reset_async(pwm, 0);
949 FD_ZERO(&fds);
950 FD_SET(pwm->nb_fd, &fds);
951 FD_SET(pwm->fd, &fds);
952 #ifdef WITH_LIBPTH
953 n = pth_select(pwm->nb_fd+1, &fds, NULL, NULL, &tv);
954 #else
955 n = select(pwm->nb_fd+1, &fds, NULL, NULL, &tv);
956 #endif
957 if (n < 0) {
958 *rc = gpg_error_from_syserror();
959 return reset_async(pwm, 0);
962 if (n > 0 && FD_ISSET(pwm->nb_fd, &fds)) {
963 pwmd_nb_status_t nb;
964 #ifdef WITH_LIBPTH
965 size_t len = pth_read(pwm->nb_fd, &nb, sizeof(nb));
966 #else
967 size_t len = read(pwm->nb_fd, &nb, sizeof(nb));
968 #endif
969 waitpid(pwm->nb_pid, &status, WNOHANG);
971 if (len != sizeof(nb)) {
972 memset(&nb, 0, sizeof(pwmd_nb_status_t));
973 *rc = gpg_error_from_syserror();
974 return reset_async(pwm, 0);
977 *rc = nb.error;
979 if (*rc)
980 return reset_async(pwm, 0);
982 /* Since the non-blocking pinentry returned a success, do a
983 * non-blocking OPEN or SAVE. */
984 pwmd_async_cmd_t c = pwm->cmd;
985 reset_async(pwm, 0);
986 pwm->_password = pwmd_strdup(nb.password);
987 memset(&nb, 0, sizeof(pwmd_nb_status_t));
988 pwm->lastcmd = c;
990 if (!pwm->_password) {
991 *rc = gpg_error_from_errno(ENOMEM);
992 return reset_async(pwm, 0);
995 if (c == ASYNC_CMD_SAVE2)
996 *rc = pwmd_save_async(pwm);
997 else
998 *rc = pwmd_open_async(pwm, pwm->filename);
1000 if (*rc) {
1001 reset_async(pwm, 0);
1002 return ASYNC_DONE;
1005 return ASYNC_PROCESS;
1008 /* Fall through so status messages can be processed during the
1009 * pinentry. */
1011 #endif
1013 if (pwm->fd < 0) {
1014 *rc = GPG_ERR_INV_STATE;
1015 return reset_async(pwm, 0);
1018 /* This is for pwmd_open_async() and pwmd_save_async(). For pinentry
1019 * retries. */
1020 *rc = _pwmd_process(pwm);
1022 if (*rc && *rc != GPG_ERR_INV_PASSPHRASE)
1023 return reset_async(pwm, 0);
1025 if (pwm->cmd == ASYNC_CMD_OPEN &&
1026 *rc == GPG_ERR_INV_PASSPHRASE &&
1027 #ifdef WITH_TCP
1028 (!pwm->tcp_conn || (pwm->tcp_conn && pwm->lastcmd == ASYNC_CMD_OPEN2)) &&
1029 #endif
1030 ++pwm->pin_try < pwm->pinentry_tries) {
1031 if (!get_custom_passphrase(pwm, NULL))
1032 goto done;
1034 #ifdef WITH_PINENTRY
1035 if (pwm->_password) {
1036 reset_async(pwm, 0);
1037 pwm->lastcmd = ASYNC_CMD_OPEN2;
1038 *rc = pwmd_open_async2(pwm, pwm->filename);
1040 else {
1041 #endif
1042 reset_async(pwm, 0);
1043 pwm->lastcmd = ASYNC_CMD_OPEN;
1044 *rc = pwmd_open_async(pwm, pwm->filename);
1045 #ifdef WITH_PINENTRY
1047 #endif
1050 done:
1051 if (*rc || pwm->state == ASYNC_DONE)
1052 return reset_async(pwm, 0);
1054 return pwm->state;
1057 gpg_error_t _assuan_command(pwm_t *pwm, assuan_context_t ctx,
1058 char **result, const char *cmd)
1060 membuf_t data;
1061 gpg_error_t rc;
1063 if (!cmd || !*cmd)
1064 return GPG_ERR_INV_ARG;
1066 if (strlen(cmd) >= ASSUAN_LINELENGTH+1)
1067 return GPG_ERR_LINE_TOO_LONG;
1069 data.len = 0;
1070 data.buf = NULL;
1071 rc = assuan_transact(ctx, cmd, inquire_realloc_cb, &data,
1072 #ifdef WITH_QUALITY
1073 pwm->pctx == ctx ? pwm->_inquire_func : inquire_cb,
1074 pwm->pctx == ctx ? pwm->_inquire_data : pwm,
1075 #else
1076 inquire_cb, pwm,
1077 #endif
1078 pwm->status_func, pwm->status_data);
1080 if (rc) {
1081 if (data.buf) {
1082 pwmd_free(data.buf);
1083 data.buf = NULL;
1086 else {
1087 if (data.buf) {
1088 inquire_realloc_cb(&data, "", 1);
1090 if (!result) {
1091 pwmd_free(data.buf);
1092 rc = GPG_ERR_INV_ARG;
1094 else
1095 *result = (char *)data.buf;
1099 return gpg_err_code(rc);
1102 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_cb_t fn,
1103 void *data)
1105 if (!pwm || !cmd || !fn)
1106 return GPG_ERR_INV_ARG;
1108 if (!pwm->ctx)
1109 return GPG_ERR_INV_STATE;
1111 pwm->inquire_func = fn;
1112 pwm->inquire_data = data;
1113 pwm->inquire_sent = 0;
1114 return _assuan_command(pwm, pwm->ctx, NULL, cmd);
1117 gpg_error_t pwmd_command_ap(pwm_t *pwm, char **result, const char *cmd,
1118 va_list ap)
1120 char *buf;
1121 size_t len;
1122 va_list ap2;
1124 if (!pwm || !cmd)
1125 return GPG_ERR_INV_ARG;
1127 if (!pwm->ctx)
1128 return GPG_ERR_INV_STATE;
1131 * C99 allows the dst pointer to be null which will calculate the length
1132 * of the would-be result and return it.
1134 va_copy(ap2, ap);
1135 len = vsnprintf(NULL, 0, cmd, ap)+1;
1136 buf = (char *)pwmd_malloc(len);
1138 if (!buf) {
1139 va_end(ap2);
1140 return gpg_error_from_errno(ENOMEM);
1143 len = vsnprintf(buf, len, cmd, ap2);
1144 va_end(ap2);
1146 if (buf[strlen(buf)-1] == '\n')
1147 buf[strlen(buf)-1] = 0;
1149 if (buf[strlen(buf)-1] == '\r')
1150 buf[strlen(buf)-1] = 0;
1152 gpg_error_t rc = _assuan_command(pwm, pwm->ctx, result, buf);
1153 pwmd_free(buf);
1154 return rc;
1157 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
1159 va_list ap;
1161 if (!pwm || !cmd)
1162 return GPG_ERR_INV_ARG;
1164 if (!pwm->ctx)
1165 return GPG_ERR_INV_STATE;
1167 if (result)
1168 *result = NULL;
1170 va_start(ap, cmd);
1171 gpg_error_t rc = pwmd_command_ap(pwm, result, cmd, ap);
1172 va_end(ap);
1173 return rc;
1176 static gpg_error_t send_pinentry_options(pwm_t *pwm)
1178 gpg_error_t rc;
1180 if (pwm->pinentry_path) {
1181 rc = pwmd_command(pwm, NULL, "SET PINENTRY_PATH=%s",
1182 pwm->pinentry_path);
1184 if (rc)
1185 return rc;
1188 if (pwm->pinentry_tty) {
1189 rc = pwmd_command(pwm, NULL, "SET TTYNAME=%s", pwm->pinentry_tty);
1191 if (rc)
1192 return rc;
1195 if (pwm->pinentry_term) {
1196 rc = pwmd_command(pwm, NULL, "SET TTYTYPE=%s", pwm->pinentry_term);
1198 if (rc)
1199 return rc;
1202 if (pwm->pinentry_display) {
1203 rc = pwmd_command(pwm, NULL, "SET DISPLAY=%s",
1204 pwm->pinentry_display);
1206 if (rc)
1207 return rc;
1210 if (pwm->title) {
1211 rc = pwmd_command(pwm, NULL, "SET TITLE=%s", pwm->title);
1213 if (rc)
1214 return rc;
1217 if (pwm->desc) {
1218 rc = pwmd_command(pwm, NULL, "SET DESC=%s", pwm->desc);
1220 if (rc)
1221 return rc;
1224 if (pwm->prompt) {
1225 rc = pwmd_command(pwm, NULL, "SET PROMPT=%s", pwm->prompt);
1227 if (rc)
1228 return rc;
1231 if (pwm->lcctype) {
1232 rc = pwmd_command(pwm, NULL, "SET LC_CTYPE=%s", pwm->lcctype);
1234 if (rc)
1235 return rc;
1238 if (pwm->lcmessages) {
1239 rc = pwmd_command(pwm, NULL, "SET LC_MESSAGES=%s", pwm->lcmessages);
1241 if (rc)
1242 return rc;
1245 if (pwm->pinentry_timeout >= 0 && !pwm->pin_try) {
1246 rc = pwmd_command(pwm, NULL, "SET PINENTRY_TIMEOUT=%i",
1247 pwm->pinentry_timeout);
1249 if (rc)
1250 return rc;
1253 return 0;
1256 gpg_error_t pwmd_socket_type(pwm_t *pwm, pwmd_socket_t *result)
1258 if (!pwm || !result)
1259 return GPG_ERR_INV_ARG;
1261 #ifdef WITH_TCP
1262 if ((pwm->fd == -1 && !pwm->tcp_conn) ||
1263 (pwm->fd == -1 && pwm->tcp_conn && pwm->tcp_conn->fd == -1))
1264 #else
1265 if (pwm->fd == -1)
1266 #endif
1267 return GPG_ERR_INV_STATE;
1269 #ifdef WITH_TCP
1270 *result = pwm->tcp_conn ? PWMD_SOCKET_SSH : PWMD_SOCKET_LOCAL;
1271 #else
1272 *result = PWMD_SOCKET_LOCAL;
1273 #endif
1274 return 0;
1277 static gpg_error_t set_pinentry_retry(pwm_t *pwm)
1279 gpg_error_t rc = 0;
1281 if (pwm->pin_try == 1) {
1282 rc = pwmd_command(pwm, NULL, "SET TITLE=%s",
1283 N_("Invalid passphrase, please try again."));
1285 if (rc)
1286 return rc;
1288 rc = pwmd_command(pwm, NULL, "SET PINENTRY_TIMEOUT=0");
1291 return rc;
1294 static gpg_error_t get_custom_passphrase(pwm_t *pwm, char **result)
1296 gpg_error_t rc = GPG_ERR_NO_DATA;
1298 if (result)
1299 *result = NULL;
1300 else {
1301 if (pwm->password || pwm->passfunc)
1302 return 0;
1305 if (pwm->password) {
1306 rc = 0;
1307 *result = pwm->password;
1309 else if (pwm->passfunc)
1310 rc = pwm->passfunc(pwm->passdata, result);
1312 return rc;
1315 static gpg_error_t do_pwmd_open(pwm_t *pwm, const char *filename, int nb,
1316 int local_pinentry)
1318 char *result = NULL;
1319 char *password = NULL;
1320 gpg_error_t rc;
1322 if (pwm->lastcmd != ASYNC_CMD_OPEN2)
1323 pwm->pin_try = 0;
1325 if (!pwm || !filename || !*filename)
1326 return GPG_ERR_INV_ARG;
1328 if (!pwm->ctx)
1329 return GPG_ERR_INV_STATE;
1332 * Avoid calling pinentry if the password is cached on the server or if
1333 * this is a new file.
1335 rc = pwmd_command(pwm, &result, "ISCACHED %s", filename);
1337 if (rc == GPG_ERR_ENOENT)
1338 goto gotpassword;
1340 if (rc && rc != GPG_ERR_NOT_FOUND)
1341 return rc;
1343 if (rc == GPG_ERR_NOT_FOUND) {
1344 rc = get_custom_passphrase(pwm, &password);
1346 if (rc && rc != GPG_ERR_NO_DATA)
1347 return rc;
1348 else if (rc == GPG_ERR_NO_DATA)
1349 rc = GPG_ERR_NOT_FOUND;
1350 else
1351 goto gotpassword;
1354 #ifdef WITH_PINENTRY
1355 if (rc == GPG_ERR_NOT_FOUND && local_pinentry) {
1356 /* Prevent pwmd from using it's pinentry if the passphrase fails. */
1357 if (!pwm->pin_try) {
1358 rc = pwmd_command(pwm, NULL, "SET ENABLE_PINENTRY=0");
1360 if (rc)
1361 return rc;
1364 rc = _pinentry_open(pwm, filename, &password, nb);
1366 /* pwmd_process() should be called if using a non-blocking local
1367 * pinentry. */
1368 if (rc || (!rc && nb))
1369 return rc;
1371 #endif
1373 gotpassword:
1374 reset_async(pwm, 0);
1376 #ifdef WITH_TCP
1377 if (!local_pinentry && !pwm->tcp_conn && !pwm->pin_try) {
1378 #else
1379 if (!local_pinentry && !pwm->pin_try) {
1380 #endif
1381 rc = send_pinentry_options(pwm);
1383 if (rc)
1384 return rc;
1387 rc = pwmd_command(pwm, NULL, "OPEN %s %s", filename,
1388 password ? password : "");
1391 * Keep the user defined password set with pwmd_setopt(). The password may
1392 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1394 if (!pwm->passfunc && password && password != pwm->password)
1395 pwmd_free(password);
1397 if (rc == GPG_ERR_INV_PASSPHRASE) {
1398 if (++pwm->pin_try < pwm->pinentry_tries) {
1399 if (!get_custom_passphrase(pwm, NULL))
1400 goto done;
1402 #ifdef WITH_PINENTRY
1403 #ifdef WITH_TCP
1404 if (pwm->tcp_conn && !local_pinentry)
1405 return rc;
1406 else if (local_pinentry)
1407 rc = _getpin(pwm, &password, PWMD_PINENTRY_OPEN_FAILED);
1408 else
1409 #else
1410 if (local_pinentry)
1411 rc = _getpin(pwm, &password, PWMD_PINENTRY_OPEN_FAILED);
1412 else
1413 #endif
1414 #else
1415 #ifdef WITH_TCP
1416 if (pwm->tcp_conn)
1417 return rc;
1418 else
1419 #endif
1420 #endif
1421 rc = set_pinentry_retry(pwm);
1423 if (rc)
1424 return rc;
1426 goto gotpassword;
1428 #ifdef WITH_PINENTRY
1429 else if (local_pinentry)
1430 _pinentry_disconnect(pwm);
1431 #endif
1433 done:
1434 return rc;
1436 #ifdef WITH_PINENTRY
1437 else if (rc && local_pinentry)
1438 _pinentry_disconnect(pwm);
1439 #endif
1441 if (!rc) {
1442 if (pwm->filename)
1443 pwmd_free(pwm->filename);
1445 pwm->filename = pwmd_strdup(filename);
1448 return rc;
1451 gpg_error_t pwmd_open2(pwm_t *pwm, const char *filename)
1453 #ifndef WITH_PINENTRY
1454 return GPG_ERR_NOT_IMPLEMENTED;
1455 #else
1456 return do_pwmd_open(pwm, filename, 0, 1);
1457 #endif
1460 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1462 return do_pwmd_open(pwm, filename, 0, 0);
1465 gpg_error_t pwmd_open_async2(pwm_t *pwm, const char *filename)
1467 #ifndef WITH_PINENTRY
1468 return GPG_ERR_NOT_IMPLEMENTED;
1469 #else
1470 if (!pwm || !filename)
1471 return GPG_ERR_INV_ARG;
1473 if (!pwm->ctx)
1474 return GPG_ERR_INV_STATE;
1476 if (pwm->cmd != ASYNC_CMD_NONE)
1477 return GPG_ERR_ASS_NESTED_COMMANDS;
1479 /* Initialize a new command since this is not a pinentry retry. */
1480 if (pwm->lastcmd != ASYNC_CMD_OPEN2)
1481 pwm->pin_try = 0;
1483 pwm->cmd = ASYNC_CMD_OPEN2;
1484 pwm->state = ASYNC_PROCESS;
1485 gpg_error_t rc = do_pwmd_open(pwm, filename, 1, 1);
1487 if (rc)
1488 reset_async(pwm, 0);
1490 return rc;
1491 #endif
1494 static gpg_error_t do_pwmd_save(pwm_t *pwm, int nb, int local_pinentry)
1496 char *result = NULL;
1497 char *password = NULL;
1498 gpg_error_t rc;
1500 if (!pwm)
1501 return GPG_ERR_INV_ARG;
1503 if (!pwm->ctx)
1504 return GPG_ERR_INV_STATE;
1506 rc = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1508 if (rc == GPG_ERR_ENOENT)
1509 rc = GPG_ERR_NOT_FOUND;
1511 if (rc && rc != GPG_ERR_NOT_FOUND)
1512 return rc;
1514 if (rc == GPG_ERR_NOT_FOUND) {
1515 rc = get_custom_passphrase(pwm, &password);
1517 if (rc && rc != GPG_ERR_NO_DATA)
1518 return rc;
1519 else if (rc == GPG_ERR_NO_DATA)
1520 rc = GPG_ERR_NOT_FOUND;
1521 else
1522 goto gotpassword;
1525 if (rc == GPG_ERR_NOT_FOUND && local_pinentry) {
1526 #ifdef WITH_PINENTRY
1527 /* Get the password using the LOCAL pinentry. */
1528 if (nb) {
1529 int p[2];
1530 pid_t pid;
1531 pwmd_nb_status_t pw;
1533 if (pipe(p) == -1)
1534 return gpg_error_from_syserror();
1536 #ifdef WITH_LIBPTH
1537 pid = pth_fork();
1538 #else
1539 pid = fork();
1540 #endif
1542 switch (pid) {
1543 case 0:
1544 close(p[0]);
1545 pw.fd = p[0];
1546 password = NULL;
1547 pw.error = _do_save_getpin(pwm, &password);
1549 if (!pw.error) {
1550 snprintf(pw.password, sizeof(pw.password), "%s",
1551 password);
1552 pwmd_free(password);
1554 #ifdef WITH_LIBPTH
1555 pth_write(p[1], &pw, sizeof(pw));
1556 #else
1557 write(p[1], &pw, sizeof(pw));
1558 #endif
1559 memset(&pw, 0, sizeof(pw));
1560 close(p[1]);
1561 _exit(0);
1562 break;
1563 case -1:
1564 rc = gpg_error_from_syserror();
1565 close(p[0]);
1566 close(p[1]);
1567 return rc;
1568 default:
1569 break;
1572 close(p[1]);
1573 pwm->nb_fd = p[0];
1574 pwm->nb_pid = pid;
1575 return 0;
1578 rc = _do_save_getpin(pwm, &password);
1580 if (rc)
1581 return rc;
1582 #endif
1584 else
1585 pwm->state = ASYNC_DONE;
1587 gotpassword:
1588 reset_async(pwm, 0);
1590 #ifdef WITH_TCP
1591 if (!local_pinentry && !pwm->tcp_conn) {
1592 #else
1593 if (!local_pinentry) {
1594 #endif
1595 rc = send_pinentry_options(pwm);
1597 if (rc)
1598 return rc;
1601 rc = pwmd_command(pwm, NULL, "SAVE %s", password ? password : "");
1603 if (!pwm->passfunc && password && password != pwm->password)
1604 pwmd_free(password);
1606 return rc;
1609 gpg_error_t pwmd_save_async2(pwm_t *pwm)
1611 #ifndef WITH_PINENTRY
1612 return GPG_ERR_NOT_IMPLEMENTED;
1613 #else
1614 if (!pwm)
1615 return GPG_ERR_INV_ARG;
1617 if (!pwm->ctx)
1618 return GPG_ERR_INV_STATE;
1620 if (pwm->cmd != ASYNC_CMD_NONE)
1621 return GPG_ERR_ASS_NESTED_COMMANDS;
1623 pwm->cmd = ASYNC_CMD_SAVE2;
1624 pwm->state = ASYNC_PROCESS;
1625 gpg_error_t rc = do_pwmd_save(pwm, 1, 1);
1627 if (rc)
1628 reset_async(pwm, 0);
1630 return rc;
1631 #endif
1634 gpg_error_t pwmd_save2(pwm_t *pwm)
1636 #ifndef WITH_PINENTRY
1637 return GPG_ERR_NOT_IMPLEMENTED;
1638 #else
1639 return do_pwmd_save(pwm, 0, 1);
1640 #endif
1643 gpg_error_t pwmd_save(pwm_t *pwm)
1645 return do_pwmd_save(pwm, 0, 0);
1648 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1650 va_list ap;
1651 int n = va_arg(ap, int);
1652 char *arg1;
1653 gpg_error_t rc = 0;
1655 if (!pwm)
1656 return GPG_ERR_INV_ARG;
1658 va_start(ap, opt);
1660 switch (opt) {
1661 case PWMD_OPTION_INQUIRE_TOTAL:
1662 n = va_arg(ap, size_t);
1663 pwm->inquire_total = n;
1664 break;
1665 case PWMD_OPTION_STATUS_CB:
1666 pwm->status_func = va_arg(ap, pwmd_status_cb_t);
1667 break;
1668 case PWMD_OPTION_STATUS_DATA:
1669 pwm->status_data = va_arg(ap, void *);
1670 break;
1671 case PWMD_OPTION_PASSPHRASE_CB:
1672 pwm->passfunc = va_arg(ap, pwmd_passphrase_cb_t);
1673 break;
1674 case PWMD_OPTION_PASSPHRASE_DATA:
1675 pwm->passdata = va_arg(ap, void *);
1676 break;
1677 case PWMD_OPTION_PASSPHRASE:
1678 arg1 = va_arg(ap, char *);
1680 if (pwm->password)
1681 pwmd_free(pwm->password);
1683 pwm->password = arg1 ? pwmd_strdup(arg1) : NULL;
1684 break;
1685 case PWMD_OPTION_PINENTRY_TRIES:
1686 n = va_arg(ap, int);
1688 if (n <= 0) {
1689 va_end(ap);
1690 rc = GPG_ERR_INV_VALUE;
1692 else
1693 pwm->pinentry_tries = n;
1694 break;
1695 case PWMD_OPTION_PINENTRY_TIMEOUT:
1696 n = va_arg(ap, int);
1698 if (n < 0) {
1699 va_end(ap);
1700 rc = GPG_ERR_INV_VALUE;
1702 else
1703 pwm->pinentry_timeout = n;
1704 break;
1705 case PWMD_OPTION_PINENTRY_PATH:
1706 if (pwm->pinentry_path)
1707 pwmd_free(pwm->pinentry_path);
1709 pwm->pinentry_path = _expand_homedir(va_arg(ap, char *), NULL);
1710 break;
1711 case PWMD_OPTION_PINENTRY_TTY:
1712 arg1 = va_arg(ap, char *);
1714 if (pwm->pinentry_tty)
1715 pwmd_free(pwm->pinentry_tty);
1717 pwm->pinentry_tty = arg1 ? pwmd_strdup(arg1) : NULL;
1718 break;
1719 case PWMD_OPTION_PINENTRY_DISPLAY:
1720 if (pwm->pinentry_display)
1721 pwmd_free(pwm->pinentry_display);
1723 pwm->pinentry_display = pwmd_strdup(va_arg(ap, char *));
1724 break;
1725 case PWMD_OPTION_PINENTRY_TERM:
1726 arg1 = va_arg(ap, char *);
1728 if (pwm->pinentry_term)
1729 pwmd_free(pwm->pinentry_term);
1731 pwm->pinentry_term = arg1 ? pwmd_strdup(arg1) : NULL;
1732 break;
1733 case PWMD_OPTION_PINENTRY_TITLE:
1734 if (pwm->title)
1735 pwmd_free(pwm->title);
1737 pwm->title = _percent_escape(va_arg(ap, char *));
1738 break;
1739 case PWMD_OPTION_PINENTRY_PROMPT:
1740 if (pwm->prompt)
1741 pwmd_free(pwm->prompt);
1743 pwm->prompt = _percent_escape(va_arg(ap, char *));
1744 break;
1745 case PWMD_OPTION_PINENTRY_DESC:
1746 if (pwm->desc)
1747 pwmd_free(pwm->desc);
1749 pwm->desc = _percent_escape(va_arg(ap, char *));
1750 break;
1751 case PWMD_OPTION_PINENTRY_LC_CTYPE:
1752 arg1 = va_arg(ap, char *);
1754 if (pwm->lcctype)
1755 pwmd_free(pwm->lcctype);
1757 pwm->lcctype = arg1 ? pwmd_strdup(arg1) : NULL;
1758 break;
1759 case PWMD_OPTION_PINENTRY_LC_MESSAGES:
1760 arg1 = va_arg(ap, char *);
1762 if (pwm->lcmessages)
1763 pwmd_free(pwm->lcmessages);
1765 pwm->lcmessages = arg1 ? pwmd_strdup(arg1) : NULL;
1766 break;
1767 case PWMD_OPTION_IP_VERSION:
1768 #ifdef WITH_TCP
1769 n = va_arg(ap, int);
1771 switch (n) {
1772 case PWMD_IP_ANY:
1773 case PWMD_IPV4:
1774 case PWMD_IPV6:
1775 pwm->prot = n;
1776 break;
1777 default:
1778 rc = GPG_ERR_INV_VALUE;
1779 break;
1782 va_end(ap);
1783 #else
1784 rc = GPG_ERR_NOT_IMPLEMENTED;
1785 #endif
1786 break;
1787 case PWMD_OPTION_KNOWNHOST_CB:
1788 #ifdef WITH_TCP
1789 pwm->kh_cb = va_arg(ap, pwmd_knownhost_cb_t);
1790 #else
1791 rc = GPG_ERR_NOT_IMPLEMENTED;
1792 #endif
1793 break;
1794 case PWMD_OPTION_KNOWNHOST_DATA:
1795 #ifdef WITH_TCP
1796 pwm->kh_data = va_arg(ap, void *);
1797 #else
1798 rc = GPG_ERR_NOT_IMPLEMENTED;
1799 #endif
1800 break;
1801 default:
1802 rc = GPG_ERR_UNKNOWN_OPTION;
1803 break;
1806 va_end(ap);
1807 return rc;
1810 gpg_error_t pwmd_get_fds(pwm_t *pwm, pwmd_fd_t *fds, int *n_fds)
1812 int in_total;
1813 int fd = 0;
1814 #ifdef WITH_TCP
1815 int afds[ARES_GETSOCK_MAXNUM];
1816 int got_sock = 0;
1817 int n, i;
1818 #endif
1820 if (!pwm || !fds || !n_fds || *n_fds <= 0)
1821 return GPG_ERR_INV_ARG;
1823 in_total = *n_fds;
1824 #ifdef WITH_TCP
1825 memset(afds, 0, sizeof(int)*ARES_GETSOCK_MAXNUM);
1826 #endif
1827 memset(fds, 0, sizeof(pwmd_fd_t)*in_total);
1828 *n_fds = 0;
1830 switch (pwm->cmd) {
1831 default:
1832 case ASYNC_CMD_NONE:
1833 case ASYNC_CMD_OPEN:
1834 case ASYNC_CMD_SAVE:
1835 #ifdef WITH_PINENTRY
1836 async1:
1837 #endif
1838 if (pwm->fd == -1)
1839 return GPG_ERR_INV_STATE;
1841 (*n_fds)++;
1842 fds[fd].fd = pwm->fd;
1843 fds[fd++].flags = PWMD_FD_READABLE;
1844 return 0;
1845 #ifdef WITH_PINENTRY
1846 case ASYNC_CMD_OPEN2:
1847 case ASYNC_CMD_SAVE2:
1848 /* The command has already completed (cached or new). */
1849 if (pwm->state == ASYNC_DONE)
1850 return 0;
1852 if (pwm->nb_fd == -1)
1853 return GPG_ERR_INV_STATE;
1855 (*n_fds)++;
1856 fds[fd].fd = pwm->nb_fd;
1857 fds[fd++].flags = PWMD_FD_READABLE;
1858 goto async1;
1859 #endif
1860 #ifdef WITH_TCP
1861 case ASYNC_CMD_DNS:
1862 if (!pwm->tcp_conn || !pwm->tcp_conn->chan)
1863 return GPG_ERR_INV_STATE;
1865 n = ares_getsock(pwm->tcp_conn->chan, afds, ARES_GETSOCK_MAXNUM);
1867 for (i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
1868 got_sock = 0;
1870 if (fd > in_total) {
1871 *n_fds = fd;
1872 return GPG_ERR_ERANGE;
1875 if (ARES_GETSOCK_READABLE(n, i)) {
1876 got_sock++;
1877 fds[fd].flags |= PWMD_FD_READABLE;
1880 if (ARES_GETSOCK_WRITABLE(n, i)) {
1881 got_sock++;
1882 fds[fd].flags |= PWMD_FD_WRITABLE;
1885 if (got_sock)
1886 fds[fd++].fd = afds[i];
1889 *n_fds = fd;
1890 return 0;
1891 case ASYNC_CMD_CONNECT:
1892 case ASYNC_CMD_HOSTKEY:
1893 if (!pwm->tcp_conn || pwm->tcp_conn->fd == -1)
1894 return GPG_ERR_INV_STATE;
1896 (*n_fds)++;
1897 fds[fd].fd = pwm->tcp_conn->fd;
1898 fds[fd++].flags = PWMD_FD_READABLE;
1899 return 0;
1900 #endif
1903 return GPG_ERR_INV_STATE;
1906 pwm_t *pwmd_new(const char *name)
1908 pwm_t *h = pwmd_calloc(1, sizeof(pwm_t));
1910 if (!h)
1911 return NULL;
1913 if (name) {
1914 h->name = pwmd_strdup(name);
1916 if (!h->name) {
1917 pwmd_free(h);
1918 return NULL;
1922 reset_handle(h);
1923 h->pinentry_timeout = -30;
1924 h->pinentry_tries = 3;
1925 #ifdef WITH_TCP
1926 h->prot = PWMD_IP_ANY;
1927 #endif
1929 if (ttyname(STDOUT_FILENO)) {
1930 char buf[256];
1932 ttyname_r(STDOUT_FILENO, buf, sizeof(buf));
1933 h->pinentry_tty = pwmd_strdup(buf);
1935 if (!h->pinentry_tty)
1936 goto fail;
1939 if (getenv("TERM") && h->pinentry_tty) {
1940 h->pinentry_term = pwmd_strdup(getenv("TERM"));
1942 if (!h->pinentry_term)
1943 goto fail;
1946 if (getenv("DISPLAY")) {
1947 h->pinentry_display = pwmd_strdup(getenv("DISPLAY"));
1949 if (!h->pinentry_display)
1950 goto fail;
1953 return h;
1955 fail:
1956 pwmd_close(h);
1957 return NULL;
1960 void pwmd_free(void *ptr)
1962 _xfree(ptr);
1965 void *pwmd_malloc(size_t size)
1967 return _xmalloc(size);
1970 void *pwmd_calloc(size_t nmemb, size_t size)
1972 return _xcalloc(nmemb, size);
1975 void *pwmd_realloc(void *ptr, size_t size)
1977 return _xrealloc(ptr, size);
1980 char *pwmd_strdup(const char *str)
1982 return _xstrdup(str);
1985 char *pwmd_strdup_printf(const char *fmt, ...)
1987 va_list ap, ap2;
1988 int len;
1989 char *buf;
1991 if (!fmt)
1992 return NULL;
1994 va_start(ap, fmt);
1995 va_copy(ap2, ap);
1996 len = vsnprintf(NULL, 0, fmt, ap);
1997 va_end(ap);
1998 buf = pwmd_malloc(++len);
2000 if (buf)
2001 vsnprintf(buf, len, fmt, ap2);
2003 va_end(ap2);
2004 return buf;
2007 gpg_error_t pwmd_getpin(pwm_t *pwm, const char *filename, char **result,
2008 pwmd_pinentry_t which)
2010 #ifndef WITH_PINENTRY
2011 return GPG_ERR_NOT_IMPLEMENTED;
2012 #else
2013 return _pwmd_getpin(pwm, filename, result, which);
2014 #endif