Fixed pwmc to use HAVE_LIBGNUTLS rather than WITH_GNUTLS.
[libpwmd.git] / libpwmd.c
blob6615bacaf26d30c9d42da1ca5c1d511ce0961673
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 HAVE_LIBGNUTLS
48 #include <gnutls/gnutls.h>
49 #include <gnutls/x509.h>
50 #define DNS_USE_GETTIMEOFDAY_FOR_ID 1
51 #include <event.h>
52 #include <evdns.h>
53 #endif
55 #ifdef HAVE_ASSUAN_H
56 #include <assuan.h>
57 #endif
59 #ifdef HAVE_SETLOCALE
60 #include <locale.h>
61 #endif
63 #include "gettext.h"
64 #define N_(msgid) dgettext("libpwmd", msgid)
66 #ifndef MEM_DEBUG
67 #include "mem.h"
68 #else
69 #define xfree free
70 #define xrealloc realloc
71 #define xmalloc malloc
72 #define xstrdup strdup
73 #define xcalloc calloc
74 #endif
76 #include "types.h"
78 #ifdef WITH_PINENTRY
79 static pwm_t *gpwm;
80 static int gelapsed, gtimeout;
81 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd);
82 static gpg_error_t global_error;
83 #endif
85 const char *pwmd_strerror(gpg_error_t e)
87 gpg_err_code_t code = gpg_err_code(e);
89 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
90 switch (code) {
91 case GPG_ERR_USER_1:
92 default:
93 return N_("Unknown error");
94 case GPG_ERR_USER_2:
95 return N_("No cache slots available");
96 case GPG_ERR_USER_3:
97 return N_("Recursion loop");
98 case GPG_ERR_USER_4:
99 return N_("No file is open");
100 case GPG_ERR_USER_5:
101 return N_("General LibXML error");
102 case GPG_ERR_USER_6:
103 return N_("File modified");
107 return gpg_strerror(e);
110 gpg_error_t pwmd_init()
112 #ifdef HAVE_LIBGNUTLS
113 gnutls_global_set_mem_functions(xmalloc, xmalloc, NULL, xrealloc, xfree);
114 gnutls_global_init ();
115 event_init();
116 evdns_init();
117 #endif
118 #ifdef ENABLE_NLS
119 bindtextdomain("libpwmd", LOCALEDIR);
120 #endif
121 gpg_err_init();
122 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
123 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
124 return 0;
127 static pwm_t *_socket_connect_finalize(pwm_t *pwm, assuan_context_t ctx)
129 int active[2];
130 int n = assuan_get_active_fds(ctx, 0, active, sizeof(active));
132 pwm->fd = n <= 0 ? -1 : dup(active[0]);
133 pwm->ctx = ctx;
134 #ifdef WITH_PINENTRY
135 pwm->pid = -1;
136 pwm->pinentry_tries = 3;
137 #endif
138 assuan_set_pointer(ctx, pwm);
139 return pwm;
142 #ifdef HAVE_LIBGNUTLS
143 static int read_hook(assuan_context_t ctx, assuan_fd_t fd, void *data,
144 size_t len, ssize_t *ret)
146 pwm_t *pwm = assuan_get_pointer(ctx);
148 if (!pwm || !pwm->session)
149 *ret = read((int)fd, data, len);
150 else {
151 do {
152 *ret = gnutls_record_recv(pwm->session, data, len);
154 if (*ret == GNUTLS_E_REHANDSHAKE) {
155 *ret = gnutls_rehandshake(pwm->session);
157 if (*ret == GNUTLS_E_WARNING_ALERT_RECEIVED ||
158 *ret == GNUTLS_A_NO_RENEGOTIATION) {
159 fprintf(stderr, "%s", gnutls_strerror(*ret));
160 continue;
163 if (*ret != GNUTLS_E_SUCCESS) {
164 fprintf(stderr, "%s", gnutls_strerror(*ret));
165 *ret = 0;
166 break;
169 *ret = gnutls_handshake(pwm->session);
171 if (*ret != GNUTLS_E_SUCCESS) {
172 fprintf(stderr, "%s", gnutls_strerror(*ret));
173 *ret = 0;
174 break;
177 continue;
179 } while (*ret == GNUTLS_E_INTERRUPTED || *ret == GNUTLS_E_AGAIN);
182 return *ret <= 0 ? 0 : 1;
185 static int write_hook(assuan_context_t ctx, assuan_fd_t fd, const void *data,
186 size_t len, ssize_t *ret)
188 pwm_t *pwm = assuan_get_pointer(ctx);
190 if (!pwm || !pwm->session)
191 *ret = write((int)fd, data, len);
192 else {
193 do {
194 *ret = gnutls_record_send(pwm->session, data, len);
195 } while (*ret == GNUTLS_E_INTERRUPTED || *ret == GNUTLS_E_AGAIN);
198 return *ret <= 0 ? 0 : 1;
201 static void _tls_deinit(pwm_t *pwm)
203 if (pwm->session) {
204 //FIXME hangs when missing cert
205 gnutls_bye(pwm->session, GNUTLS_SHUT_RDWR);
206 gnutls_deinit(pwm->session);
209 if (pwm->x509)
210 gnutls_certificate_free_credentials(pwm->x509);
213 static gpg_error_t verify_certificate(pwm_t *pwm)
215 unsigned int status;
216 const gnutls_datum_t *cert_list;
217 unsigned int cert_list_size;
218 int i;
219 gnutls_x509_crt_t cert;
220 gpg_error_t rc;
222 rc = gnutls_certificate_verify_peers2(pwm->session, &status);
224 if (rc < 0)
225 return GPG_ERR_UNKNOWN_ERRNO;
227 if (status & GNUTLS_CERT_INVALID)
228 return GPG_ERR_MISSING_CERT;
230 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
231 return GPG_ERR_BAD_SIGNATURE;
233 if (status & GNUTLS_CERT_REVOKED)
234 return GPG_ERR_CERT_REVOKED;
236 if (gnutls_certificate_type_get(pwm->session) != GNUTLS_CRT_X509)
237 return GPG_ERR_UNSUPPORTED_CERT;
239 if (gnutls_x509_crt_init(&cert) < 0)
240 return gpg_error_from_errno(ENOMEM);
242 cert_list = gnutls_certificate_get_peers (pwm->session, &cert_list_size);
244 if (!cert_list) {
245 rc = GPG_ERR_MISSING_CERT;
246 goto done;
249 for (i = 0; i < cert_list_size; i++) {
250 if (gnutls_x509_crt_import(cert, &cert_list[i], GNUTLS_X509_FMT_DER) < 0) {
251 rc = GPG_ERR_BAD_CERT_CHAIN;
252 goto done;
256 /* Beware here we do not check for errors.
258 if (gnutls_x509_crt_get_expiration_time(cert) < time (0)) {
259 rc = GPG_ERR_CERT_EXPIRED;
260 goto done;
263 if (gnutls_x509_crt_get_activation_time(cert) > time (0)) {
264 rc = GPG_ERR_CERT_TOO_YOUNG;
265 goto done;
268 #if 0
269 if (!gnutls_x509_crt_check_hostname (cert, hostname))
271 printf ("The certificate's owner does not match hostname '%s'\n",
272 hostname);
273 return 1;
275 #endif
277 done:
278 gnutls_x509_crt_deinit(cert);
279 return rc;
282 //FIXME return codes
283 static gpg_error_t _tls_init(pwm_t *pwm)
285 gpg_error_t rc;
286 const char *errstr;
288 rc = gnutls_certificate_allocate_credentials(&pwm->x509);
290 if (rc)
291 return gpg_error_from_errno(ENOMEM);
293 /* The client certificate must be signed by the CA of the pwmd server
294 * certificate in order for the client to authenticate successfully. Man in
295 * the middle attacks are still possible if the attacker is running a pwmd
296 * that doesn't require client certificate authentication. So require the
297 * client to verify the server certificate.
299 gnutls_certificate_set_x509_trust_file (pwm->x509, pwm->tcp_conn->ca,
300 GNUTLS_X509_FMT_PEM);
302 rc = gnutls_certificate_set_x509_key_file(pwm->x509, pwm->tcp_conn->cert,
303 pwm->tcp_conn->key, GNUTLS_X509_FMT_PEM);
304 rc = gnutls_init(&pwm->session, GNUTLS_CLIENT);
305 rc = gnutls_priority_set_direct(pwm->session, "SECURE256", &errstr);
306 rc = gnutls_credentials_set(pwm->session, GNUTLS_CRD_CERTIFICATE, pwm->x509);
307 gnutls_transport_set_ptr(pwm->session,
308 (gnutls_transport_ptr_t)pwm->tcp_conn->fd);
309 rc = gnutls_handshake(pwm->session);
311 if (rc < 0) {
312 gnutls_perror (rc);
313 rc = GPG_ERR_INV_CIPHER_MODE;
314 _tls_deinit(pwm);
315 return rc;
318 rc = verify_certificate(pwm);
319 if (rc)
320 _tls_deinit(pwm);
321 return rc;
324 static void _tls_assuan_deinit(assuan_context_t ctx)
326 pwm_t *pwm = assuan_get_pointer(ctx);
328 _tls_deinit(pwm);
331 static void free_tcp_conn(pwmd_tcp_conn_t *conn)
333 if (!conn)
334 return;
336 if (conn->cert)
337 xfree(conn->cert);
339 if (conn->key)
340 xfree(conn->key);
342 if (conn->ca)
343 xfree(conn->ca);
345 if (conn->host)
346 xfree(conn->host);
348 xfree(conn);
352 * Sets common options from both pwmd_tcp_connect() and
353 * pwmd_tcp_connect_async().
355 static gpg_error_t init_tcp_conn(pwmd_tcp_conn_t **dst, char *host, int port,
356 const char *cert, const char *key, const char *ca)
358 char buf[PATH_MAX];
359 struct passwd *pw = getpwuid(getuid());
360 pwmd_tcp_conn_t *conn;
361 gpg_error_t rc = 0;
363 if (!host)
364 return GPG_ERR_INV_ARG;
366 conn = xcalloc(1, sizeof(pwmd_tcp_conn_t));
367 if (!conn)
368 return gpg_error_from_errno(ENOMEM);
370 conn->port = port == -1 ? 6466 : port;
371 conn->host = xstrdup(host);
373 if (!conn->host) {
374 rc = gpg_error_from_syserror();
375 goto fail;
378 if (!cert) {
379 snprintf(buf, sizeof(buf), "%s/.pwmd/client-cert.pem", pw->pw_dir);
380 conn->cert = xstrdup(buf);
382 else
383 conn->cert = xstrdup(cert);
385 if (!conn->cert) {
386 rc = gpg_error_from_syserror();
387 goto fail;
390 if (!key) {
391 snprintf(buf, sizeof(buf), "%s/.pwmd/client-key.pem", pw->pw_dir);
392 conn->key = xstrdup(buf);
394 else
395 conn->key = xstrdup(key);
397 if (!conn->key) {
398 rc = gpg_error_from_syserror();
399 goto fail;
402 if (!ca) {
403 snprintf(buf, sizeof(buf), "%s/.pwmd/ca-cert.pem", pw->pw_dir);
404 conn->ca = xstrdup(buf);
406 else
407 conn->ca = xstrdup(ca);
409 if (!conn->ca) {
410 rc = gpg_error_from_syserror();
411 goto fail;
414 conn->fd = socket(PF_INET, SOCK_STREAM, 0);
416 if (conn->fd == -1) {
417 rc = gpg_error_from_syserror();
418 goto fail;
421 *dst = conn;
422 return 0;
424 fail:
425 free_tcp_conn(conn);
426 return rc;
429 static gpg_error_t do_connect(pwm_t *pwm, void *addr)
431 struct sockaddr_in their_addr;
433 pwm->cmd = ASYNC_CMD_CONNECT;
434 their_addr.sin_family = AF_INET;
435 their_addr.sin_port = htons(pwm->tcp_conn->port);
436 their_addr.sin_addr = *((struct in_addr *)addr);
437 memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
439 if (connect(pwm->tcp_conn->fd, (struct sockaddr *)&their_addr,
440 sizeof(their_addr)) == -1)
441 return gpg_error_from_syserror();
443 return 0;
446 static void dns_resolve_cb(int result, char type, int count, int ttl,
447 void *addrs, void *arg)
449 pwm_t *pwm = arg;
451 if (result != DNS_ERR_NONE) {
452 switch (result) {
453 case DNS_ERR_NOTEXIST:
454 pwm->tcp_conn->rc = GPG_ERR_UNKNOWN_HOST;
455 break;
456 case DNS_ERR_SERVERFAILED:
457 pwm->tcp_conn->rc = GPG_ERR_EHOSTDOWN;
458 break;
459 case DNS_ERR_TIMEOUT:
460 pwm->tcp_conn->rc = GPG_ERR_TIMEOUT;
461 break;
462 default:
463 //FIXME
464 pwm->tcp_conn->rc = GPG_ERR_EHOSTUNREACH;
465 break;
468 return;
471 fcntl(pwm->tcp_conn->fd, F_SETFL, O_NONBLOCK);
472 pwm->tcp_conn->rc = do_connect(pwm, &addrs[0]);
475 pwm_t *pwmd_tcp_connect_async(char *host, int port, gpg_error_t *rc,
476 const char *cert, const char *key, const char *ca)
478 #ifndef HAVE_LIBGNUTLS
479 *rc = GPG_ERR_NOT_IMPLEMENTED;
480 return NULL;
481 #else
482 pwmd_tcp_conn_t *conn;
483 pwm_t *pwm;
485 *rc = init_tcp_conn(&conn, host, port, cert, key, ca);
487 if (*rc)
488 return NULL;
490 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
491 *rc = gpg_error_from_syserror();
492 free_tcp_conn(conn);
493 return NULL;
496 pwm->tcp_conn = conn;
497 pwm->cmd = ASYNC_CMD_DNS;
498 pwm->state = ASYNC_PROCESS;
499 evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
500 evdns_resolve_ipv4(pwm->tcp_conn->host, 0, dns_resolve_cb, pwm);
501 return pwm;
502 #endif
505 static pwm_t *setup_context(pwm_t *pwm, gpg_error_t *rc)
507 assuan_context_t ctx;
508 struct assuan_io_hooks io_hooks = {read_hook, write_hook};
509 int fd = pwm->tcp_conn->fd;
511 *rc = _tls_init(pwm);
512 free_tcp_conn(pwm->tcp_conn);
513 pwm->tcp_conn = NULL;
515 if (*rc)
516 goto fail;
518 assuan_set_io_hooks(&io_hooks);
519 *rc = assuan_socket_connect_fd(&ctx, fd, 0, pwm);
521 if (*rc)
522 goto fail;
524 assuan_set_finish_handler(ctx, _tls_assuan_deinit);
525 return _socket_connect_finalize(pwm, ctx);
527 fail:
528 _tls_deinit(pwm);
529 xfree(pwm);
530 return NULL;
533 pwm_t *pwmd_tcp_connect(char *host, int port, gpg_error_t *rc, const char *cert,
534 const char *key, const char *ca)
536 struct hostent *he;
537 pwm_t *pwm = NULL;
538 pwmd_tcp_conn_t *conn;
540 *rc = init_tcp_conn(&conn, host, port, cert, key, ca);
542 if (*rc)
543 return NULL;
545 if ((he = gethostbyname(conn->host)) == NULL) {
546 // FIXME
547 *rc = gpg_error_from_syserror();
548 free_tcp_conn(conn);
549 return NULL;
552 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
553 *rc = gpg_error_from_syserror();
554 goto fail;
557 pwm->tcp_conn = conn;
558 *rc = do_connect(pwm, he->h_addr);
560 if (*rc)
561 goto fail;
563 return setup_context(pwm, rc);
565 fail:
566 xfree(pwm);
568 if (conn->fd >= 0)
569 close(conn->fd);
571 free_tcp_conn(conn);
572 return NULL;
574 #endif
576 pwm_t *pwmd_connect(const char *path, gpg_error_t *rc)
578 pwm_t *pwm = NULL;
579 char *socketpath = NULL;
580 struct passwd *pw;
581 assuan_context_t ctx;
583 if (!path) {
584 pw = getpwuid(getuid());
585 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
586 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
588 else
589 socketpath = xstrdup(path);
591 *rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
592 xfree(socketpath);
594 if (*rc)
595 return NULL;
597 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
598 *rc = gpg_error_from_syserror();
599 return NULL;
602 return _socket_connect_finalize(pwm, ctx);
605 gpg_error_t pwmd_pending_line(pwm_t *pwm, char **line, size_t *len)
607 if (!pwm)
608 return GPG_ERR_INV_ARG;
610 if (assuan_pending_line(pwm->ctx))
611 return assuan_read_line(pwm->ctx, line, len);
613 return GPG_ERR_NO_DATA;
616 void pwmd_close(pwm_t *pwm)
618 if (!pwm)
619 return;
621 if (pwm->ctx)
622 assuan_disconnect(pwm->ctx);
624 if (pwm->password)
625 xfree(pwm->password);
627 if (pwm->title)
628 xfree(pwm->title);
630 if (pwm->desc)
631 xfree(pwm->desc);
633 if (pwm->prompt)
634 xfree(pwm->prompt);
636 if (pwm->pinentry_tty)
637 xfree(pwm->pinentry_tty);
639 if (pwm->pinentry_display)
640 xfree(pwm->pinentry_display);
642 if (pwm->pinentry_term)
643 xfree(pwm->pinentry_term);
645 if (pwm->filename)
646 xfree(pwm->filename);
648 xfree(pwm);
651 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
653 membuf_t *mem = (membuf_t *)data;
654 void *p;
656 if (!buffer)
657 return 0;
659 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
660 return 1;
662 mem->buf = p;
663 memcpy((char *)mem->buf + mem->len, buffer, len);
664 mem->len += len;
665 return 0;
668 void pwmd_free_result(void *data)
670 xfree(data);
673 static int _inquire_cb(void *data, const char *keyword)
675 pwm_t *pwm = (pwm_t *)data;
676 gpg_error_t rc = 0;
677 int flags = fcntl(pwm->fd, F_GETFL);
679 /* Shouldn't get this far without a callback. */
680 if (!pwm->inquire_func)
681 return GPG_ERR_INV_ARG;
684 * Since the socket file descriptor is probably set to non-blocking, set to
685 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
686 * asynchronous INQUIRE is supported by either libassuan or a later
687 * libpwmd.
689 fcntl(pwm->fd, F_SETFL, 0);
691 for (;;) {
692 char *result = NULL;
693 size_t len;
694 gpg_error_t arc;
696 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
697 rc = gpg_err_code(rc);
699 if (rc == GPG_ERR_EOF || !rc) {
700 if (len <= 0 || !result || !*result) {
701 rc = 0;
702 break;
705 arc = assuan_send_data(pwm->ctx, result, len);
707 if (rc == GPG_ERR_EOF) {
708 rc = arc;
709 break;
712 rc = arc;
714 else if (rc)
715 break;
718 fcntl(pwm->fd, F_SETFL, flags);
719 return rc;
722 gpg_error_t pwmd_finalize(pwm_t *pwm)
724 if (!pwm || pwm->cmd == ASYNC_CMD_NONE || pwm->state != ASYNC_DONE)
725 return GPG_ERR_INV_ARG;
727 pwm->state = ASYNC_INIT;
728 pwm->cmd = ASYNC_CMD_NONE;
730 #ifdef HAVE_LIBGNUTLS
731 if (pwm->cmd == ASYNC_CMD_CONNECT || pwm->cmd == ASYNC_CMD_DNS) {
732 /* pwm is no longer a valid handle. */
733 if (pwm->tcp_conn->rc) {
734 free_tcp_conn(pwm->tcp_conn);
735 xfree(pwm);
738 free_tcp_conn(pwm->tcp_conn);
739 pwm->tcp_conn = NULL;
740 return 0;
742 #endif
744 if (pwm->fd < 0)
745 return GPG_ERR_INV_ARG;
747 pwm->state = ASYNC_INIT;
748 pwm->ntries = 0;
749 #ifdef WITH_PINENTRY
750 pwm->is_open_cmd = 0;
751 #endif
752 return 0;
755 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
757 char *buf;
758 gpg_error_t rc;
759 size_t len = strlen(cmd) + 2;
761 len += arg ? strlen(arg) : 0;
763 if (pwm->state != ASYNC_INIT)
764 return GPG_ERR_UNEXPECTED;
766 buf = (char *)xmalloc(len);
768 if (!buf) {
769 rc = gpg_error_from_errno(ENOMEM);
770 goto fail;
773 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
774 rc = assuan_write_line(pwm->ctx, buf);
775 xfree(buf);
777 if (!rc)
778 pwm->state = ASYNC_PROCESS;
780 fail:
781 return rc;
784 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
786 if (!pwm || !filename)
787 return GPG_ERR_INV_ARG;
789 /* For pinentry retries. */
790 if (!pwm->is_open_cmd) {
791 if (pwm->filename)
792 xfree(pwm->filename);
794 pwm->filename = xstrdup(filename);
797 pwm->is_open_cmd = 1;
798 return do_nb_command(pwm, "OPEN", filename);
801 gpg_error_t pwmd_save_async(pwm_t *pwm)
803 if (!pwm)
804 return GPG_ERR_INV_ARG;
806 return do_nb_command(pwm, "SAVE", NULL);
809 static gpg_error_t parse_assuan_line(pwm_t *pwm)
811 gpg_error_t rc;
812 char *line;
813 size_t len;
815 rc = assuan_read_line(pwm->ctx, &line, &len);
817 if (!rc) {
818 if (line[0] == 'O' && line[1] == 'K' &&
819 (line[2] == 0 || line[2] == ' ')) {
820 pwm->state = ASYNC_DONE;
822 else if (line[0] == '#') {
824 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
825 if (pwm->status_func) {
826 pwm->status_func(pwm->status_data,
827 line[1] == 0 ? line+1 : line+2);
830 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
831 (line[3] == 0 || line[3] == ' ')) {
832 line += 4;
833 rc = atoi(line);
834 pwm->state = ASYNC_DONE;
838 return rc;
841 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
843 fd_set fds;
844 int n;
845 struct timeval tv = {0, 0};
847 *rc = 0;
849 if (!pwm) {
850 *rc = GPG_ERR_INV_ARG;
851 return ASYNC_DONE;
854 #ifdef WITH_LIBGNUTLS
855 if (pwm->cmd == ASYNC_CMD_DNS) {
856 if (pwm->tcp_conn->rc) {
857 *rc = pwm->tcp_conn->rc;
858 close(pwm->tcp_conn->fd);
859 pwm->state = ASYNC_DONE;
862 event_loop(EVLOOP_NONBLOCK);
863 return pwm->state;
865 else if (pwm->cmd == ASYNC_CMD_CONNECT) {
866 if (pwm->tcp_conn->rc == GPG_ERR_EINPROGRESS) {
867 int ret;
868 socklen_t len = sizeof(int);
870 FD_ZERO(&fds);
871 FD_SET(pwm->tcp_conn->fd, &fds);
872 n = select(pwm->tcp_conn->fd+1, NULL, &fds, NULL, &tv);
874 if (!n || !FD_ISSET(pwm->tcp_conn->fd, &fds))
875 return pwm->state;
876 else if (n == -1) {
877 *rc = gpg_error_from_syserror();
878 close(pwm->tcp_conn->fd);
879 pwm->state = ASYNC_DONE;
880 return pwm->state;
883 ret = getsockopt(pwm->tcp_conn->fd, SOL_SOCKET, SO_ERROR, &n, &len);
884 if (ret || n) {
885 *rc = ret ? gpg_error_from_syserror() : gpg_error_from_errno(n);
886 close(pwm->tcp_conn->fd);
887 pwm->state = ASYNC_DONE;
888 return pwm->state;
891 else if (pwm->tcp_conn->rc) {
892 *rc = pwm->tcp_conn->rc;
893 close(pwm->tcp_conn->fd);
894 pwm->state = ASYNC_DONE;
895 return pwm->state;
898 fcntl(pwm->tcp_conn->fd, F_SETFL, 0);
899 setup_context(pwm, rc);
900 pwm->state = ASYNC_DONE;
901 return pwm->state;
903 #endif
905 if (pwm->fd < 0) {
906 *rc = GPG_ERR_INV_ARG;
907 return ASYNC_DONE;
909 else if (pwm->state == ASYNC_DONE)
910 return pwm->state;
911 else if (pwm->state == ASYNC_INIT) {
912 *rc = GPG_ERR_UNEXPECTED;
913 return ASYNC_DONE;
916 FD_ZERO(&fds);
917 FD_SET(pwm->fd, &fds);
918 n = select(pwm->fd+1, &fds, NULL, NULL, &tv);
920 if (n > 0) {
921 if (FD_ISSET(pwm->fd, &fds))
922 *rc = parse_assuan_line(pwm);
925 while (!*rc && assuan_pending_line(pwm->ctx))
926 *rc = parse_assuan_line(pwm);
928 if (pwm->is_open_cmd && gpg_err_code(*rc) == EPWMD_BADKEY &&
929 ++pwm->ntries < pwm->pinentry_tries) {
930 pwm->state = ASYNC_INIT;
931 *rc = pwmd_open_async(pwm, pwm->filename);
934 return pwm->state;
937 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
938 char **result, const char *cmd)
940 membuf_t data;
941 gpg_error_t rc;
943 data.len = 0;
944 data.buf = NULL;
946 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
947 pwm->status_func, pwm->status_data);
949 if (rc) {
950 if (data.buf) {
951 xfree(data.buf);
952 data.buf = NULL;
955 else {
956 if (data.buf) {
957 mem_realloc_cb(&data, "", 1);
958 *result = (char *)data.buf;
962 return gpg_err_code(rc);
965 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
966 void *data)
968 if (!pwm || !cmd || !fn)
969 return GPG_ERR_INV_ARG;
971 pwm->inquire_func = fn;
972 pwm->inquire_data = data;
973 return assuan_command(pwm, pwm->ctx, NULL, cmd);
976 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
978 #ifndef WITH_PINENTRY
979 return GPG_ERR_NOT_IMPLEMENTED;
980 #else
981 if (!pwm || pwm->pid == -1)
982 return GPG_ERR_INV_ARG;
984 if (kill(pwm->pid, 0) == 0) {
985 if (kill(pwm->pid, SIGTERM) == -1) {
986 if (kill(pwm->pid, SIGKILL) == -1)
987 return gpg_error_from_errno(errno);
990 pwm->pin_error = GPG_ERR_TIMEOUT;
992 else
993 return gpg_error_from_errno(errno);
995 return 0;
996 #endif
999 #ifdef WITH_PINENTRY
1000 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
1002 char *buf;
1003 char tmp[ASSUAN_LINELENGTH];
1004 gpg_error_t error;
1006 if (!pwm->title)
1007 pwm->title = xstrdup(N_("LibPWMD"));
1009 if (!pwm->prompt)
1010 pwm->prompt = xstrdup(N_("Password:"));
1012 if (!pwm->desc && !which)
1013 pwm->desc = xstrdup(N_("Enter a password."));
1015 if (which == 1) {
1016 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
1017 buf = xstrdup(tmp);
1019 else if (which == 2) {
1020 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
1021 buf = xstrdup(tmp);
1023 else {
1024 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
1025 sprintf(buf, "SETERROR %s", pwm->desc);
1028 error = pinentry_command(pwm, NULL, buf);
1029 xfree(buf);
1031 if (error)
1032 return error;
1034 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
1035 sprintf(buf, "SETPROMPT %s", pwm->prompt);
1036 error = pinentry_command(pwm, NULL, buf);
1037 xfree(buf);
1039 if (error)
1040 return error;
1042 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
1043 sprintf(buf, "SETDESC %s", pwm->title);
1044 error = pinentry_command(pwm, NULL, buf);
1045 xfree(buf);
1046 return error;
1049 static void update_pinentry_settings(pwm_t *pwm)
1051 FILE *fp;
1052 char buf[LINE_MAX];
1053 struct passwd *pw = getpwuid(getuid());
1054 char *p;
1056 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
1058 if ((fp = fopen(buf, "r")) == NULL)
1059 return;
1061 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
1062 char name[32], val[256];
1064 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
1065 continue;
1067 if (strcasecmp(name, "TTYNAME") == 0) {
1068 xfree(pwm->pinentry_tty);
1069 pwm->pinentry_tty = xstrdup(val);
1071 else if (strcasecmp(name, "TTYTYPE") == 0) {
1072 xfree(pwm->pinentry_term);
1073 pwm->pinentry_term = xstrdup(val);
1075 else if (strcasecmp(name, "DISPLAY") == 0) {
1076 xfree(pwm->pinentry_display);
1077 pwm->pinentry_display = xstrdup(val);
1079 else if (strcasecmp(name, "PATH") == 0) {
1080 xfree(pwm->pinentry_path);
1081 pwm->pinentry_path = xstrdup(val);
1085 fclose(fp);
1088 static gpg_error_t launch_pinentry(pwm_t *pwm)
1090 int rc;
1091 assuan_context_t ctx;
1092 int child_list[] = {-1};
1093 char *display = getenv("DISPLAY");
1094 const char *argv[6];
1095 int have_display = 0;
1096 char *tty = NULL;
1098 update_pinentry_settings(pwm);
1100 if (pwm->pinentry_display || display)
1101 have_display = 1;
1102 else {
1103 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
1105 if (!tty)
1106 return gpg_error_from_errno(errno);
1109 if (!have_display && !tty)
1110 return GPG_ERR_ENOTTY;
1112 argv[0] = "pinentry";
1113 argv[1] = have_display ? "--display" : "--ttyname";
1114 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
1115 argv[3] = NULL;
1117 if (!have_display) {
1118 argv[3] = "--ttytype";
1119 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
1120 argv[5] = NULL;
1123 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
1125 if (rc)
1126 return rc;
1128 pwm->pid = assuan_get_pid(ctx);
1129 pwm->pctx = ctx;
1130 return set_pinentry_strings(pwm, 0);
1133 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
1135 gpg_error_t n;
1137 if (!pwm->pctx) {
1138 n = launch_pinentry(pwm);
1140 if (n)
1141 return n;
1144 return assuan_command(pwm, pwm->pctx, result, cmd);
1147 static void pinentry_disconnect(pwm_t *pwm)
1149 if (pwm->pctx)
1150 assuan_disconnect(pwm->pctx);
1152 pwm->pctx = NULL;
1153 pwm->pid = -1;
1157 * Only called from a child process.
1159 static void catchsig(int sig)
1161 switch (sig) {
1162 case SIGALRM:
1163 if (gelapsed++ >= gtimeout) {
1164 global_error = pwmd_terminate_pinentry(gpwm);
1166 if (!global_error)
1167 global_error = GPG_ERR_TIMEOUT;
1169 break;
1172 alarm(1);
1173 break;
1174 default:
1175 break;
1180 * Borrowed from libassuan.
1182 static char *percent_escape(const char *atext)
1184 const unsigned char *s;
1185 int len = strlen(atext) * 3 + 1;
1186 char *buf = (char *)xmalloc(len), *p = buf;
1188 if (!buf)
1189 return NULL;
1191 for (s=(const unsigned char *)atext; *s; s++) {
1192 if (*s < ' ') {
1193 sprintf (p, "%%%02X", *s);
1194 p += 3;
1196 else
1197 *p++ = *s;
1200 *p = 0;
1201 return buf;
1203 #endif
1205 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
1207 if (!cmd)
1208 return GPG_ERR_INV_ARG;
1210 return assuan_command(pwm, pwm->ctx, result, cmd);
1214 * Avoid sending the BYE command here. libassuan will close the file
1215 * descriptor and release the assuan context. Use pwmd_close() instead.
1217 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
1219 va_list ap;
1220 char *buf;
1221 size_t len;
1222 gpg_error_t error;
1224 if (!pwm || !cmd)
1225 return GPG_ERR_INV_ARG;
1227 *result = NULL;
1228 va_start(ap, cmd);
1230 * C99 allows the dst pointer to be null which will calculate the length
1231 * of the result and return it.
1233 len = vsnprintf(NULL, 0, cmd, ap);
1234 buf = (char *)xmalloc(len + 1);
1235 len = vsnprintf(buf, len + 1, cmd, ap);
1236 va_end(ap);
1237 error = send_command(pwm, result, buf);
1238 xfree(buf);
1239 return error;
1242 #ifdef WITH_PINENTRY
1243 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
1245 if (gtimeout) {
1246 signal(SIGALRM, catchsig);
1247 alarm(1);
1250 *result = NULL;
1251 return pinentry_command(pwm, result, "GETPIN");
1254 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
1256 int pin_try = *try_n;
1257 gpg_error_t error;
1259 getpin_again:
1260 *try_n = pin_try;
1262 if (pin_try == -1) {
1263 error = set_pinentry_strings(pwm, which);
1265 if (error) {
1266 pinentry_disconnect(pwm);
1267 return error;
1270 else {
1271 if (pwm->pinentry_tries-1 != pin_try) {
1272 error = set_pinentry_strings(pwm, 1);
1274 if (error) {
1275 pinentry_disconnect(pwm);
1276 return error;
1281 error = do_getpin(pwm, result);
1284 * Since there was input cancel any timeout setting.
1286 alarm(0);
1288 if (error) {
1289 if (error == GPG_ERR_CANCELED)
1290 return GPG_ERR_CANCELED;
1292 if (pin_try != -1 && pin_try--)
1293 goto getpin_again;
1295 if (pwm->pctx)
1296 pinentry_disconnect(pwm);
1298 *try_n = pin_try;
1299 return error;
1302 return 0;
1304 #endif
1306 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1308 gpg_error_t error;
1310 #ifndef WITH_PINENTRY
1311 return GPG_ERR_NOT_IMPLEMENTED;
1312 #endif
1314 if (!pwm || !pw || !pw->filename[0])
1315 return GPG_ERR_INV_ARG;
1317 close(pw->fd);
1319 if (pw->error) {
1320 error = pw->error;
1321 goto fail;
1324 if (pwm->filename)
1325 xfree(pwm->filename);
1327 pwm->filename = xstrdup(pw->filename);
1328 memset(pw, 0, sizeof(pwmd_nb_status_t));
1329 return 0;
1331 fail:
1332 memset(pw, 0, sizeof(pwmd_nb_status_t));
1333 return error;
1336 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
1338 char buf[ASSUAN_LINELENGTH];
1339 gpg_error_t error;
1340 char *result = NULL;
1342 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
1343 error = send_command(pwm, &result, buf);
1344 memset(buf, 0, sizeof(buf));
1346 if (error && result)
1347 xfree(result);
1349 return error;
1352 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
1353 int nb, int timeout)
1355 char *result = NULL;
1356 char *password = NULL;
1357 char path[PATH_MAX];
1358 #ifdef WITH_PINENTRY
1359 int pin_try;
1360 #endif
1362 if (!pwm || !filename || !*filename) {
1363 *error = GPG_ERR_INV_ARG;
1364 return nb ? -1 : 1;
1367 #ifdef WITH_PINENTRY
1368 pin_try = pwm->pinentry_tries - 1;
1369 #endif
1372 * Avoid calling pinentry if the password is cached on the server or if
1373 * this is a new file.
1375 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
1377 if (*error)
1378 return nb ? -1 : 1;
1380 snprintf(path, sizeof(path), "%s/%s", result, filename);
1381 pwmd_free_result(result);
1383 if (access(path, R_OK) == -1) {
1384 if (errno == ENOENT)
1385 goto gotpassword;
1388 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
1390 if (*error == EPWMD_CACHE_NOT_FOUND) {
1391 if (pwm->passfunc) {
1392 password = pwm->passfunc(pwm, pwm->passdata);
1393 goto gotpassword;
1396 #ifdef WITH_PINENTRY
1398 * Get the password from pinentry.
1400 if (pwm->use_pinentry) {
1402 * Nonblocking is wanted. fork() then return a file descriptor
1403 * that the client can use to read() from.
1405 if (nb) {
1406 int p[2];
1407 pid_t pid;
1408 pwmd_nb_status_t pw;
1410 if (pipe(p) == -1) {
1411 *error = gpg_error_from_syserror();
1412 return -1;
1415 pid = fork();
1417 switch (pid) {
1418 case 0:
1419 close(p[0]);
1420 strncpy(pw.filename, filename, sizeof(pw.filename));
1421 pw.filename[sizeof(pw.filename)-1] = 0;
1422 pw.fd = p[0];
1424 if (timeout > 0) {
1425 gpwm = pwm;
1426 gtimeout = timeout;
1427 gelapsed = 0;
1430 getpin_nb_again:
1431 *error = getpin(pwm, &password, &pin_try, 0);
1433 if (*error) {
1434 getpin_nb_fail:
1435 if (pwm->pctx)
1436 pinentry_disconnect(pwm);
1438 if (gtimeout && gelapsed >= gtimeout)
1439 *error = GPG_ERR_TIMEOUT;
1441 pw.error = *error;
1442 write(p[1], &pw, sizeof(pw));
1443 close(p[1]);
1444 _exit(1);
1448 * Don't count the time it takes to open the file
1449 * which may have many iterations.
1451 signal(SIGALRM, SIG_DFL);
1452 *error = do_open_command(pwm, filename, password);
1454 if (timeout)
1455 signal(SIGALRM, catchsig);
1457 if (pwm->pctx && *error == EPWMD_BADKEY) {
1458 if (pin_try-- > 0)
1459 goto getpin_nb_again;
1461 goto getpin_nb_fail;
1464 pinentry_disconnect(pwm);
1465 pw.error = 0;
1466 write(p[1], &pw, sizeof(pw));
1467 close(p[1]);
1468 _exit(0);
1469 break;
1470 case -1:
1471 *error = gpg_error_from_syserror();
1472 close(p[0]);
1473 close(p[1]);
1474 return -1;
1475 default:
1476 break;
1479 close(p[1]);
1480 return p[0];
1483 else {
1484 #endif
1486 * Not using pinentry and the file was not found
1487 * in the cache.
1489 password = pwm->password;
1490 #ifdef WITH_PINENTRY
1492 #endif
1494 else if (*error)
1495 return nb ? -1 : 1;
1497 gotpassword:
1498 *error = do_open_command(pwm, filename, password);
1501 * Keep the user defined password set with pwmd_setopt(). The password may
1502 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1504 if (password && password != pwm->password)
1505 xfree(password);
1507 #ifdef WITH_PINENTRY
1508 if (*error == EPWMD_BADKEY) {
1509 if (pin_try-- > 0 && !nb) {
1510 *error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
1511 N_("Invalid password, please try again."));
1513 if (*error)
1514 return 1;
1516 goto gotpassword;
1519 if (nb)
1520 pinentry_disconnect(pwm);
1522 return nb ? -1 : 1;
1524 #endif
1526 if (!*error) {
1527 if (pwm->filename)
1528 xfree(pwm->filename);
1530 pwm->filename = xstrdup(filename);
1534 * The file is cached or the file is a new file.
1536 if (nb)
1537 return *error ? -1 : -2;
1539 return *error ? 1 : 0;
1542 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1544 gpg_error_t error;
1546 do_pwmd_open(pwm, &error, filename, 0, 0);
1547 return error;
1550 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1551 int timeout)
1553 #ifndef WITH_PINENTRY
1554 *error = GPG_ERR_NOT_IMPLEMENTED;
1555 return -1;
1556 #else
1557 return do_pwmd_open(pwm, error, filename, 1, timeout);
1558 #endif
1561 #ifdef WITH_PINENTRY
1562 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1564 int confirm = 0;
1565 gpg_error_t error;
1566 char *result = NULL;
1567 int pin_try = -1;
1569 again:
1570 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1572 if (error) {
1573 if (pwm->pctx)
1574 pinentry_disconnect(pwm);
1576 if (*password)
1577 xfree(*password);
1579 return error;
1582 if (!confirm++) {
1583 *password = result;
1584 goto again;
1587 if (strcmp(*password, result)) {
1588 xfree(*password);
1589 xfree(result);
1590 pinentry_disconnect(pwm);
1591 error = EPWMD_BADKEY;
1592 return error;
1595 xfree(result);
1596 pinentry_disconnect(pwm);
1597 return 0;
1599 #endif
1601 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1603 char buf[ASSUAN_LINELENGTH];
1604 gpg_error_t error;
1605 char *result = NULL;
1607 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1608 error = send_command(pwm, &result, buf);
1609 memset(&buf, 0, sizeof(buf));
1611 if (error && result)
1612 xfree(result);
1614 return error;
1617 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1619 gpg_error_t rc;
1621 #ifndef WITH_PINENTRY
1622 return GPG_ERR_NOT_IMPLEMENTED;
1623 #endif
1625 if (!pwm || !pw || !pw->filename[0])
1626 return GPG_ERR_INV_ARG;
1628 close(pw->fd);
1629 rc = pw->error;
1630 memset(pw, 0, sizeof(pwmd_nb_status_t));
1631 return rc;
1634 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1636 char *result = NULL;
1637 char *password = NULL;
1639 if (!pwm) {
1640 *error = GPG_ERR_INV_ARG;
1641 return nb ? -1 : 1;
1644 if (pwm->use_pinentry || pwm->passfunc) {
1645 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1647 if (*error == EPWMD_CACHE_NOT_FOUND) {
1648 #ifdef WITH_PINENTRY
1649 if (pwm->use_pinentry) {
1650 if (nb) {
1651 int p[2];
1652 pid_t pid;
1653 pwmd_nb_status_t pw;
1655 if (pipe(p) == -1) {
1656 *error = gpg_error_from_syserror();
1657 return -1;
1660 pid = fork();
1662 switch (pid) {
1663 case 0:
1664 close(p[0]);
1665 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1666 pw.filename[sizeof(pw.filename)-1] = 0;
1667 pw.fd = p[0];
1669 do {
1670 password = NULL;
1671 *error = do_save_getpin(pwm, &password);
1672 } while (*error == EPWMD_BADKEY);
1674 if (*error) {
1675 if (pwm->pctx)
1676 pinentry_disconnect(pwm);
1678 pw.error = *error;
1679 write(p[1], &pw, sizeof(pw));
1680 close(p[1]);
1681 _exit(1);
1684 *error = do_save_command(pwm, password);
1685 pinentry_disconnect(pwm);
1686 pw.error = *error;
1687 write(p[1], &pw, sizeof(pw));
1688 close(p[1]);
1689 _exit(0);
1690 break;
1691 case -1:
1692 *error = gpg_error_from_syserror();
1693 close(p[0]);
1694 close(p[1]);
1695 return -1;
1696 default:
1697 break;
1700 close(p[1]);
1701 return p[0];
1704 *error = do_save_getpin(pwm, &password);
1706 if (*error)
1707 return 1;
1709 else {
1710 #endif
1711 if (pwm->passfunc)
1712 password = (*pwm->passfunc)(pwm, pwm->passdata);
1713 #ifdef WITH_PINENTRY
1715 #endif
1717 else {
1718 if (*error)
1719 return nb ? -1 : 1;
1722 else
1723 password = pwm->password;
1725 *error = do_save_command(pwm, password);
1727 if (password && password != pwm->password)
1728 xfree(password);
1730 if (nb)
1731 return *error ? -1 : -2;
1733 return *error ? 1 : 0;
1736 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1738 #ifndef WITH_PINENTRY
1739 *error = GPG_ERR_NOT_IMPLEMENTED;
1740 return -1;
1741 #else
1742 return do_pwmd_save(pwm, error, 1);
1743 #endif
1746 gpg_error_t pwmd_save(pwm_t *pwm)
1748 gpg_error_t error;
1750 do_pwmd_save(pwm, &error, 0);
1751 return error;
1754 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1756 va_list ap;
1757 #ifdef WITH_PINENTRY
1758 int n = va_arg(ap, int);
1759 char *result;
1760 #endif
1761 char *arg1;
1762 gpg_error_t error = 0;
1764 if (!pwm)
1765 return GPG_ERR_INV_ARG;
1767 va_start(ap, opt);
1769 switch (opt) {
1770 case PWMD_OPTION_STATUS_FUNC:
1771 pwm->status_func = va_arg(ap, pwmd_status_fn);
1772 break;
1773 case PWMD_OPTION_STATUS_DATA:
1774 pwm->status_data = va_arg(ap, void *);
1775 break;
1776 case PWMD_OPTION_PASSWORD_FUNC:
1777 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1778 break;
1779 case PWMD_OPTION_PASSWORD_DATA:
1780 pwm->passdata = va_arg(ap, void *);
1781 break;
1782 case PWMD_OPTION_PASSWORD:
1783 arg1 = va_arg(ap, char *);
1785 if (pwm->password)
1786 xfree(pwm->password);
1788 pwm->password = xstrdup(arg1);
1789 break;
1790 #ifdef WITH_PINENTRY
1791 case PWMD_OPTION_PINENTRY:
1792 n = va_arg(ap, int);
1794 if (n != 0 && n != 1) {
1795 va_end(ap);
1796 error = GPG_ERR_INV_VALUE;
1798 else {
1799 pwm->use_pinentry = n;
1800 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
1801 !pwm->use_pinentry);
1803 break;
1804 case PWMD_OPTION_PINENTRY_TRIES:
1805 n = va_arg(ap, int);
1807 if (n <= 0) {
1808 va_end(ap);
1809 error = GPG_ERR_INV_VALUE;
1811 else
1812 pwm->pinentry_tries = n;
1813 break;
1814 case PWMD_OPTION_PINENTRY_PATH:
1815 if (pwm->pinentry_path)
1816 xfree(pwm->pinentry_path);
1818 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1819 break;
1820 case PWMD_OPTION_PINENTRY_TTY:
1821 if (pwm->pinentry_tty)
1822 xfree(pwm->pinentry_tty);
1824 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1825 break;
1826 case PWMD_OPTION_PINENTRY_DISPLAY:
1827 if (pwm->pinentry_display)
1828 xfree(pwm->pinentry_display);
1830 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1831 break;
1832 case PWMD_OPTION_PINENTRY_TERM:
1833 if (pwm->pinentry_term)
1834 xfree(pwm->pinentry_term);
1836 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1837 break;
1838 case PWMD_OPTION_PINENTRY_TITLE:
1839 if (pwm->title)
1840 xfree(pwm->title);
1841 pwm->title = percent_escape(va_arg(ap, char *));
1842 break;
1843 case PWMD_OPTION_PINENTRY_PROMPT:
1844 if (pwm->prompt)
1845 xfree(pwm->prompt);
1846 pwm->prompt = percent_escape(va_arg(ap, char *));
1847 break;
1848 case PWMD_OPTION_PINENTRY_DESC:
1849 if (pwm->desc)
1850 xfree(pwm->desc);
1851 pwm->desc = percent_escape(va_arg(ap, char *));
1852 break;
1853 #else
1854 case PWMD_OPTION_PINENTRY:
1855 case PWMD_OPTION_PINENTRY_TRIES:
1856 case PWMD_OPTION_PINENTRY_PATH:
1857 case PWMD_OPTION_PINENTRY_TTY:
1858 case PWMD_OPTION_PINENTRY_DISPLAY:
1859 case PWMD_OPTION_PINENTRY_TERM:
1860 case PWMD_OPTION_PINENTRY_TITLE:
1861 case PWMD_OPTION_PINENTRY_PROMPT:
1862 case PWMD_OPTION_PINENTRY_DESC:
1863 error = GPG_ERR_NOT_IMPLEMENTED;
1864 break;
1865 #endif
1866 default:
1867 error = GPG_ERR_NOT_IMPLEMENTED;
1868 break;
1871 va_end(ap);
1872 return error;
1875 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, assuan_context_t *ctx, int *fd)
1877 if (!pwm)
1878 return GPG_ERR_INV_ARG;
1880 *ctx = pwm->ctx;
1881 *fd = pwm->fd;
1882 return 0;