pwmd_init() now returns immediately if called more than once.
[libpwmd.git] / libpwmd.c
blob2a63c3eaf2101c57c8a3aa2c8bf8d2e5a57a2c16
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 <libpwmd.h>
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
44 #ifdef WITH_LIBPTH
45 #include <pth.h>
46 #endif
48 #ifdef HAVE_ASSUAN_H
49 #include <assuan.h>
50 #endif
52 #ifdef HAVE_SETLOCALE
53 #include <locale.h>
54 #endif
56 #include "gettext.h"
57 #define N_(msgid) dgettext("libpwmd", msgid)
59 #ifndef MEM_DEBUG
60 #include "mem.h"
61 #else
62 #define xfree free
63 #define xrealloc realloc
64 #define xmalloc malloc
65 #define xstrdup strdup
66 #define xcalloc calloc
67 #endif
69 #include "types.h"
71 #ifdef USE_PINENTRY
72 static pwm_t *gpwm;
73 static int gelapsed, gtimeout;
74 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd);
75 static gpg_error_t global_error;
76 #endif
78 const char *pwmd_strerror(gpg_error_t e)
80 gpg_err_code_t code = gpg_err_code(e);
82 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
83 switch (code) {
84 case GPG_ERR_USER_1:
85 default:
86 return N_("Unknown error");
87 case GPG_ERR_USER_2:
88 return N_("No cache slots available");
89 case GPG_ERR_USER_3:
90 return N_("Recursion loop");
91 case GPG_ERR_USER_4:
92 return N_("No file is open");
93 case GPG_ERR_USER_5:
94 return N_("General LibXML error");
95 case GPG_ERR_USER_6:
96 return N_("File modified");
100 return gpg_strerror(e);
103 gpg_error_t pwmd_init()
105 static int initialized;
107 if (initialized)
108 return 0;
110 #ifdef ENABLE_NLS
111 bindtextdomain("libpwmd", LOCALEDIR);
112 #endif
113 gpg_err_init();
114 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
115 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
116 initialized = 1;
117 return 0;
120 pwm_t *pwmd_connect(const char *path, gpg_error_t *error)
122 pwm_t *pwm = NULL;
123 char *socketpath = NULL;
124 time_t now;
125 struct passwd *pw;
126 assuan_context_t ctx;
127 int rc, n;
128 int active[2];
130 if (!path) {
131 pw = getpwuid(getuid());
132 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
133 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
135 else
136 socketpath = xstrdup(path);
138 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
139 xfree(socketpath);
141 if (rc) {
142 *error = rc;
143 return NULL;
146 n = assuan_get_active_fds(ctx, 0, active, sizeof(active));
148 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
149 *error = gpg_error_from_errno(errno);
150 assuan_disconnect(ctx);
151 return NULL;
154 pwm->fd = n <= 0 ? -1 : dup(active[0]);
156 if (pwm->fd != -1)
157 fcntl(pwm->fd, F_SETFL, O_NONBLOCK);
159 pwm->ctx = ctx;
160 #ifdef USE_PINENTRY
161 pwm->pid = -1;
162 pwm->pinentry_tries = 3;
163 #endif
164 time(&now);
165 srandom(now);
166 *error = 0;
167 return pwm;
170 gpg_error_t pwmd_pending_line(pwm_t *pwm, char **line, size_t *len)
172 if (!pwm)
173 return GPG_ERR_INV_ARG;
175 if (assuan_pending_line(pwm->ctx))
176 return assuan_read_line(pwm->ctx, line, len);
178 return GPG_ERR_NO_DATA;
181 void pwmd_close(pwm_t *pwm)
183 if (!pwm)
184 return;
186 if (pwm->ctx)
187 assuan_disconnect(pwm->ctx);
189 if (pwm->password)
190 xfree(pwm->password);
192 if (pwm->title)
193 xfree(pwm->title);
195 if (pwm->desc)
196 xfree(pwm->desc);
198 if (pwm->prompt)
199 xfree(pwm->prompt);
201 if (pwm->pinentry_tty)
202 xfree(pwm->pinentry_tty);
204 if (pwm->pinentry_display)
205 xfree(pwm->pinentry_display);
207 if (pwm->pinentry_term)
208 xfree(pwm->pinentry_term);
210 if (pwm->filename)
211 xfree(pwm->filename);
213 xfree(pwm);
216 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
218 membuf_t *mem = (membuf_t *)data;
219 void *p;
221 if (!buffer)
222 return 0;
224 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
225 return 1;
227 mem->buf = p;
228 memcpy((char *)mem->buf + mem->len, buffer, len);
229 mem->len += len;
230 return 0;
233 void pwmd_free_result(void *data)
235 xfree(data);
238 static int _inquire_cb(void *data, const char *keyword)
240 pwm_t *pwm = (pwm_t *)data;
241 gpg_error_t rc = 0;
242 int flags = fcntl(pwm->fd, F_GETFL);
244 /* Shouldn't get this far without a callback. */
245 if (!pwm->inquire_func)
246 return GPG_ERR_INV_ARG;
249 * Since the socket file descriptor is probably set to non-blocking, set to
250 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
251 * asynchronous INQUIRE is supported by either libassuan or a later
252 * libpwmd.
254 fcntl(pwm->fd, F_SETFL, 0);
256 for (;;) {
257 char *result = NULL;
258 size_t len;
259 gpg_error_t arc;
261 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
262 rc = gpg_err_code(rc);
264 if (rc == GPG_ERR_EOF || !rc) {
265 if (len <= 0 || !result || !*result) {
266 rc = 0;
267 break;
270 arc = assuan_send_data(pwm->ctx, result, len);
272 if (rc == GPG_ERR_EOF) {
273 rc = arc;
274 break;
277 rc = arc;
279 else if (rc)
280 break;
283 fcntl(pwm->fd, F_SETFL, flags);
284 return rc;
287 gpg_error_t pwmd_finalize(pwm_t *pwm)
289 if (!pwm || pwm->fd < 0)
290 return GPG_ERR_INV_ARG;
292 pwm->state = ASYNC_INIT;
293 pwm->ntries = 0;
294 pwm->is_open_cmd = 0;
295 return 0;
298 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
300 char *buf;
301 gpg_error_t rc;
302 size_t len = strlen(cmd) + 2;
304 len += arg ? strlen(arg) : 0;
306 if (pwm->state != ASYNC_INIT)
307 return GPG_ERR_UNEXPECTED;
309 buf = (char *)xmalloc(len);
311 if (!buf) {
312 rc = gpg_error_from_errno(ENOMEM);
313 goto fail;
316 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
317 rc = assuan_write_line(pwm->ctx, buf);
318 xfree(buf);
320 if (!rc)
321 pwm->state = ASYNC_PROCESS;
323 fail:
324 return rc;
327 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
329 if (!pwm || !filename)
330 return GPG_ERR_INV_ARG;
332 /* For pinentry retries. */
333 if (!pwm->is_open_cmd) {
334 if (pwm->filename)
335 xfree(pwm->filename);
337 pwm->filename = xstrdup(filename);
340 pwm->is_open_cmd = 1;
341 return do_nb_command(pwm, "OPEN", filename);
344 gpg_error_t pwmd_save_async(pwm_t *pwm)
346 if (!pwm)
347 return GPG_ERR_INV_ARG;
349 return do_nb_command(pwm, "SAVE", NULL);
352 static gpg_error_t parse_assuan_line(pwm_t *pwm)
354 gpg_error_t rc;
355 char *line;
356 size_t len;
358 rc = assuan_read_line(pwm->ctx, &line, &len);
360 if (!rc) {
361 if (line[0] == 'O' && line[1] == 'K' &&
362 (line[2] == 0 || line[2] == ' ')) {
363 pwm->state = ASYNC_DONE;
365 else if (line[0] == '#') {
367 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
368 if (pwm->status_func) {
369 pwm->status_func(pwm->status_data,
370 line[1] == 0 ? line+1 : line+2);
373 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
374 (line[3] == 0 || line[3] == ' ')) {
375 line += 4;
376 rc = atoi(line);
377 pwm->state = ASYNC_DONE;
381 return rc;
384 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
386 fd_set rfds;
387 int n;
388 struct timeval tv = {0, 0};
390 *rc = 0;
392 if (!pwm || pwm->fd < 0) {
393 *rc = GPG_ERR_INV_ARG;
394 return ASYNC_DONE;
396 else if (pwm->state == ASYNC_DONE)
397 return pwm->state;
398 else if (pwm->state == ASYNC_INIT) {
399 *rc = GPG_ERR_UNEXPECTED;
400 return ASYNC_DONE;
403 FD_ZERO(&rfds);
404 FD_SET(pwm->fd, &rfds);
405 #ifdef WITH_LIBPTH
406 n = pth_select(pwm->fd+1, &rfds, NULL, NULL, &tv);
407 #else
408 n = select(pwm->fd+1, &rfds, NULL, NULL, &tv);
409 #endif
411 if (n > 0) {
412 if (FD_ISSET(pwm->fd, &rfds))
413 *rc = parse_assuan_line(pwm);
416 while (!*rc && assuan_pending_line(pwm->ctx))
417 *rc = parse_assuan_line(pwm);
419 if (pwm->is_open_cmd && gpg_err_code(*rc) == EPWMD_BADKEY &&
420 ++pwm->ntries < pwm->pinentry_tries) {
421 pwm->state = ASYNC_INIT;
422 *rc = pwmd_open_async(pwm, pwm->filename);
425 return pwm->state;
428 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
429 char **result, const char *cmd)
431 membuf_t data;
432 gpg_error_t rc;
434 data.len = 0;
435 data.buf = NULL;
437 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
438 pwm->status_func, pwm->status_data);
440 if (rc) {
441 if (data.buf) {
442 xfree(data.buf);
443 data.buf = NULL;
446 else {
447 if (data.buf) {
448 mem_realloc_cb(&data, "", 1);
449 *result = (char *)data.buf;
453 return gpg_err_code(rc);
456 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
457 void *data)
459 if (!pwm || !cmd || !fn)
460 return GPG_ERR_INV_ARG;
462 pwm->inquire_func = fn;
463 pwm->inquire_data = data;
464 return assuan_command(pwm, pwm->ctx, NULL, cmd);
467 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
469 #ifndef USE_PINENTRY
470 return GPG_ERR_NOT_IMPLEMENTED;
471 #else
472 if (!pwm || pwm->pid == -1)
473 return GPG_ERR_INV_ARG;
475 if (kill(pwm->pid, 0) == 0) {
476 if (kill(pwm->pid, SIGTERM) == -1) {
477 if (kill(pwm->pid, SIGKILL) == -1)
478 return gpg_error_from_errno(errno);
481 pwm->pin_error = GPG_ERR_TIMEOUT;
483 else
484 return gpg_error_from_errno(errno);
486 return 0;
487 #endif
490 #ifdef USE_PINENTRY
491 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
493 char *buf;
494 char tmp[ASSUAN_LINELENGTH];
495 gpg_error_t error;
497 if (!pwm->title)
498 pwm->title = xstrdup(N_("LibPWMD"));
500 if (!pwm->prompt)
501 pwm->prompt = xstrdup(N_("Password:"));
503 if (!pwm->desc && !which)
504 pwm->desc = xstrdup(N_("Enter a password."));
506 if (which == 1) {
507 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
508 buf = xstrdup(tmp);
510 else if (which == 2) {
511 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
512 buf = xstrdup(tmp);
514 else {
515 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
516 sprintf(buf, "SETERROR %s", pwm->desc);
519 error = pinentry_command(pwm, NULL, buf);
520 xfree(buf);
522 if (error)
523 return error;
525 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
526 sprintf(buf, "SETPROMPT %s", pwm->prompt);
527 error = pinentry_command(pwm, NULL, buf);
528 xfree(buf);
530 if (error)
531 return error;
533 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
534 sprintf(buf, "SETDESC %s", pwm->title);
535 error = pinentry_command(pwm, NULL, buf);
536 xfree(buf);
537 return error;
540 static void update_pinentry_settings(pwm_t *pwm)
542 FILE *fp;
543 char buf[LINE_MAX];
544 struct passwd *pw = getpwuid(getuid());
545 char *p;
547 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
549 if ((fp = fopen(buf, "r")) == NULL)
550 return;
552 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
553 char name[32], val[256];
555 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
556 continue;
558 if (strcasecmp(name, "TTYNAME") == 0) {
559 xfree(pwm->pinentry_tty);
560 pwm->pinentry_tty = xstrdup(val);
562 else if (strcasecmp(name, "TTYTYPE") == 0) {
563 xfree(pwm->pinentry_term);
564 pwm->pinentry_term = xstrdup(val);
566 else if (strcasecmp(name, "DISPLAY") == 0) {
567 xfree(pwm->pinentry_display);
568 pwm->pinentry_display = xstrdup(val);
570 else if (strcasecmp(name, "PATH") == 0) {
571 xfree(pwm->pinentry_path);
572 pwm->pinentry_path = xstrdup(val);
576 fclose(fp);
579 static gpg_error_t launch_pinentry(pwm_t *pwm)
581 int rc;
582 assuan_context_t ctx;
583 int child_list[] = {-1};
584 char *display = getenv("DISPLAY");
585 const char *argv[6];
586 int have_display = 0;
587 char *tty = NULL;
589 update_pinentry_settings(pwm);
591 if (pwm->pinentry_display || display)
592 have_display = 1;
593 else {
594 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
596 if (!tty)
597 return gpg_error_from_errno(errno);
600 if (!have_display && !tty)
601 return GPG_ERR_ENOTTY;
603 argv[0] = "pinentry";
604 argv[1] = have_display ? "--display" : "--ttyname";
605 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
606 argv[3] = NULL;
608 if (!have_display) {
609 argv[3] = "--ttytype";
610 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
611 argv[5] = NULL;
614 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
616 if (rc)
617 return rc;
619 pwm->pid = assuan_get_pid(ctx);
620 pwm->pctx = ctx;
621 return set_pinentry_strings(pwm, 0);
624 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
626 gpg_error_t n;
628 if (!pwm->pctx) {
629 n = launch_pinentry(pwm);
631 if (n)
632 return n;
635 return assuan_command(pwm, pwm->pctx, result, cmd);
638 static void pinentry_disconnect(pwm_t *pwm)
640 if (pwm->pctx)
641 assuan_disconnect(pwm->pctx);
643 pwm->pctx = NULL;
644 pwm->pid = -1;
648 * Only called from a child process.
650 static void catchsig(int sig)
652 switch (sig) {
653 case SIGALRM:
654 if (gelapsed++ >= gtimeout) {
655 global_error = pwmd_terminate_pinentry(gpwm);
657 if (!global_error)
658 global_error = GPG_ERR_TIMEOUT;
660 break;
663 alarm(1);
664 break;
665 default:
666 break;
671 * Borrowed from libassuan.
673 static char *percent_escape(const char *atext)
675 const unsigned char *s;
676 int len = strlen(atext) * 3 + 1;
677 char *buf = (char *)xmalloc(len), *p = buf;
679 if (!buf)
680 return NULL;
682 for (s=(const unsigned char *)atext; *s; s++) {
683 if (*s < ' ') {
684 sprintf (p, "%%%02X", *s);
685 p += 3;
687 else
688 *p++ = *s;
691 *p = 0;
692 return buf;
694 #endif
696 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
698 if (!cmd)
699 return GPG_ERR_INV_ARG;
701 return assuan_command(pwm, pwm->ctx, result, cmd);
705 * Avoid sending the BYE command here. libassuan will close the file
706 * descriptor and release the assuan context. Use pwmd_close() instead.
708 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
710 va_list ap;
711 char *buf;
712 size_t len;
713 gpg_error_t error;
715 if (!pwm || !cmd)
716 return GPG_ERR_INV_ARG;
718 *result = NULL;
719 va_start(ap, cmd);
721 * C99 allows the dst pointer to be null which will calculate the length
722 * of the result and return it.
724 len = vsnprintf(NULL, 0, cmd, ap);
725 buf = (char *)xmalloc(len + 1);
726 len = vsnprintf(buf, len + 1, cmd, ap);
727 va_end(ap);
728 error = send_command(pwm, result, buf);
729 xfree(buf);
730 return error;
733 #ifdef USE_PINENTRY
734 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
736 if (gtimeout) {
737 signal(SIGALRM, catchsig);
738 alarm(1);
741 *result = NULL;
742 return pinentry_command(pwm, result, "GETPIN");
745 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
747 int pin_try = *try_n;
748 gpg_error_t error;
750 getpin_again:
751 *try_n = pin_try;
753 if (pin_try == -1) {
754 error = set_pinentry_strings(pwm, which);
756 if (error) {
757 pinentry_disconnect(pwm);
758 return error;
761 else {
762 if (pwm->pinentry_tries-1 != pin_try) {
763 error = set_pinentry_strings(pwm, 1);
765 if (error) {
766 pinentry_disconnect(pwm);
767 return error;
772 error = do_getpin(pwm, result);
775 * Since there was input cancel any timeout setting.
777 alarm(0);
779 if (error) {
780 if (error == GPG_ERR_CANCELED)
781 return GPG_ERR_CANCELED;
783 if (pin_try != -1 && pin_try--)
784 goto getpin_again;
786 if (pwm->pctx)
787 pinentry_disconnect(pwm);
789 *try_n = pin_try;
790 return error;
793 return 0;
795 #endif
797 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
799 gpg_error_t error;
801 #ifndef USE_PINENTRY
802 return GPG_ERR_NOT_IMPLEMENTED;
803 #endif
805 if (!pwm || !pw || !pw->filename[0])
806 return GPG_ERR_INV_ARG;
808 close(pw->fd);
810 if (pw->error) {
811 error = pw->error;
812 goto fail;
815 if (pwm->filename)
816 xfree(pwm->filename);
818 pwm->filename = xstrdup(pw->filename);
819 memset(pw, 0, sizeof(pwmd_nb_status_t));
820 return 0;
822 fail:
823 memset(pw, 0, sizeof(pwmd_nb_status_t));
824 return error;
827 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
829 char buf[ASSUAN_LINELENGTH];
830 gpg_error_t error;
831 char *result = NULL;
833 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
834 error = send_command(pwm, &result, buf);
835 memset(buf, 0, sizeof(buf));
837 if (error && result)
838 xfree(result);
840 return error;
843 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
844 int nb, int timeout)
846 char *result = NULL;
847 char *password = NULL;
848 char path[PATH_MAX];
849 #ifdef USE_PINENTRY
850 int pin_try;
851 #endif
853 if (!pwm || !filename || !*filename) {
854 *error = GPG_ERR_INV_ARG;
855 return nb ? -1 : 1;
858 #ifdef USE_PINENTRY
859 pin_try = pwm->pinentry_tries - 1;
860 #endif
863 * Avoid calling pinentry if the password is cached on the server or if
864 * this is a new file.
866 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
868 if (*error)
869 return nb ? -1 : 1;
871 snprintf(path, sizeof(path), "%s/%s", result, filename);
872 pwmd_free_result(result);
874 if (access(path, R_OK) == -1) {
875 if (errno == ENOENT)
876 goto gotpassword;
879 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
881 if (*error == EPWMD_CACHE_NOT_FOUND) {
882 if (pwm->passfunc) {
883 password = pwm->passfunc(pwm, pwm->passdata);
884 goto gotpassword;
887 #ifdef USE_PINENTRY
889 * Get the password from pinentry.
891 if (pwm->use_pinentry) {
893 * Nonblocking is wanted. fork() then return a file descriptor
894 * that the client can use to read() from.
896 if (nb) {
897 int p[2];
898 pid_t pid;
899 pwmd_nb_status_t pw;
901 if (pipe(p) == -1) {
902 *error = gpg_error_from_syserror();
903 return -1;
906 #ifdef WITH_LIBPTH
907 pid = pth_fork();
908 #else
909 pid = fork();
910 #endif
912 switch (pid) {
913 case 0:
914 close(p[0]);
915 strncpy(pw.filename, filename, sizeof(pw.filename));
916 pw.filename[sizeof(pw.filename)-1] = 0;
917 pw.fd = p[0];
919 if (timeout > 0) {
920 gpwm = pwm;
921 gtimeout = timeout;
922 gelapsed = 0;
925 getpin_nb_again:
926 *error = getpin(pwm, &password, &pin_try, 0);
928 if (*error) {
929 getpin_nb_fail:
930 if (pwm->pctx)
931 pinentry_disconnect(pwm);
933 if (gtimeout && gelapsed >= gtimeout)
934 *error = GPG_ERR_TIMEOUT;
936 pw.error = *error;
937 #ifdef WITH_LIBPTH
938 pth_write(p[1], &pw, sizeof(pw));
939 #else
940 write(p[1], &pw, sizeof(pw));
941 #endif
942 close(p[1]);
943 _exit(1);
947 * Don't count the time it takes to open the file
948 * which may have many iterations.
950 signal(SIGALRM, SIG_DFL);
951 *error = do_open_command(pwm, filename, password);
953 if (timeout)
954 signal(SIGALRM, catchsig);
956 if (pwm->pctx && *error == EPWMD_BADKEY) {
957 if (pin_try-- > 0)
958 goto getpin_nb_again;
960 goto getpin_nb_fail;
963 pinentry_disconnect(pwm);
964 pw.error = 0;
965 #ifdef WITH_LIBPTH
966 pth_write(p[1], &pw, sizeof(pw));
967 #else
968 write(p[1], &pw, sizeof(pw));
969 #endif
970 close(p[1]);
971 _exit(0);
972 break;
973 case -1:
974 *error = gpg_error_from_syserror();
975 close(p[0]);
976 close(p[1]);
977 return -1;
978 default:
979 break;
982 close(p[1]);
983 *error = 0;
984 return p[0];
987 else {
988 #endif
990 * Not using pinentry and the file was not found
991 * in the cache.
993 password = pwm->password;
994 #ifdef USE_PINENTRY
996 #endif
998 else if (*error)
999 return nb ? -1 : 1;
1001 gotpassword:
1002 *error = do_open_command(pwm, filename, password);
1005 * Keep the user defined password set with pwmd_setopt(). The password may
1006 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1008 if (password && password != pwm->password)
1009 xfree(password);
1011 #ifdef USE_PINENTRY
1012 if (*error == EPWMD_BADKEY) {
1013 if (pin_try-- > 0 && !nb) {
1014 *error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
1015 N_("Invalid password, please try again."));
1017 if (*error)
1018 return 1;
1020 goto gotpassword;
1023 if (nb)
1024 pinentry_disconnect(pwm);
1026 return nb ? -1 : 1;
1028 #endif
1030 if (!*error) {
1031 if (pwm->filename)
1032 xfree(pwm->filename);
1034 pwm->filename = xstrdup(filename);
1038 * The file is cached or the file is a new file.
1040 if (nb)
1041 return *error ? -1 : -2;
1043 return *error ? 1 : 0;
1046 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1048 gpg_error_t error;
1050 do_pwmd_open(pwm, &error, filename, 0, 0);
1051 return error;
1054 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1055 int timeout)
1057 #ifndef USE_PINENTRY
1058 *error = GPG_ERR_NOT_IMPLEMENTED;
1059 return -1;
1060 #else
1061 return do_pwmd_open(pwm, error, filename, 1, timeout);
1062 #endif
1065 #ifdef USE_PINENTRY
1066 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1068 int confirm = 0;
1069 gpg_error_t error;
1070 char *result = NULL;
1071 int pin_try = -1;
1073 again:
1074 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1076 if (error) {
1077 if (pwm->pctx)
1078 pinentry_disconnect(pwm);
1080 if (*password)
1081 xfree(*password);
1083 return error;
1086 if (!confirm++) {
1087 *password = result;
1088 goto again;
1091 if (strcmp(*password, result)) {
1092 xfree(*password);
1093 xfree(result);
1094 pinentry_disconnect(pwm);
1095 error = EPWMD_BADKEY;
1096 return error;
1099 xfree(result);
1100 pinentry_disconnect(pwm);
1101 return 0;
1103 #endif
1105 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1107 char buf[ASSUAN_LINELENGTH];
1108 gpg_error_t error;
1109 char *result = NULL;
1111 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1112 error = send_command(pwm, &result, buf);
1113 memset(&buf, 0, sizeof(buf));
1115 if (error && result)
1116 xfree(result);
1118 return error;
1121 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1123 gpg_error_t rc;
1125 #ifndef USE_PINENTRY
1126 return GPG_ERR_NOT_IMPLEMENTED;
1127 #endif
1129 if (!pwm || !pw || !pw->filename[0])
1130 return GPG_ERR_INV_ARG;
1132 close(pw->fd);
1133 rc = pw->error;
1134 memset(pw, 0, sizeof(pwmd_nb_status_t));
1135 return rc;
1138 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1140 char *result = NULL;
1141 char *password = NULL;
1143 if (!pwm) {
1144 *error = GPG_ERR_INV_ARG;
1145 return nb ? -1 : 1;
1148 if (pwm->use_pinentry || pwm->passfunc) {
1149 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1151 if (*error == EPWMD_CACHE_NOT_FOUND) {
1152 #ifdef USE_PINENTRY
1153 if (pwm->use_pinentry) {
1154 if (nb) {
1155 int p[2];
1156 pid_t pid;
1157 pwmd_nb_status_t pw;
1159 if (pipe(p) == -1) {
1160 *error = gpg_error_from_syserror();
1161 return -1;
1164 #ifdef WITH_LIBPTH
1165 pid = pth_fork();
1166 #else
1167 pid = fork();
1168 #endif
1170 switch (pid) {
1171 case 0:
1172 close(p[0]);
1173 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1174 pw.filename[sizeof(pw.filename)-1] = 0;
1175 pw.fd = p[0];
1177 do {
1178 password = NULL;
1179 *error = do_save_getpin(pwm, &password);
1180 } while (*error == EPWMD_BADKEY);
1182 if (*error) {
1183 if (pwm->pctx)
1184 pinentry_disconnect(pwm);
1186 pw.error = *error;
1187 #ifdef WITH_LIBPTH
1188 pth_write(p[1], &pw, sizeof(pw));
1189 #else
1190 write(p[1], &pw, sizeof(pw));
1191 #endif
1192 close(p[1]);
1193 _exit(1);
1196 *error = do_save_command(pwm, password);
1197 pinentry_disconnect(pwm);
1198 pw.error = *error;
1199 #ifdef WITH_LIBPTH
1200 pth_write(p[1], &pw, sizeof(pw));
1201 #else
1202 write(p[1], &pw, sizeof(pw));
1203 #endif
1204 close(p[1]);
1205 _exit(0);
1206 break;
1207 case -1:
1208 *error = gpg_error_from_syserror();
1209 close(p[0]);
1210 close(p[1]);
1211 return -1;
1212 default:
1213 break;
1216 close(p[1]);
1217 *error = 0;
1218 return p[0];
1221 *error = do_save_getpin(pwm, &password);
1223 if (*error)
1224 return 1;
1226 else {
1227 #endif
1228 if (pwm->passfunc)
1229 password = (*pwm->passfunc)(pwm, pwm->passdata);
1230 #ifdef USE_PINENTRY
1232 #endif
1234 else {
1235 if (*error)
1236 return nb ? -1 : 1;
1239 else
1240 password = pwm->password;
1242 *error = do_save_command(pwm, password);
1244 if (password && password != pwm->password)
1245 xfree(password);
1247 if (nb)
1248 return *error ? -1 : -2;
1250 return *error ? 1 : 0;
1253 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1255 #ifndef USE_PINENTRY
1256 *error = GPG_ERR_NOT_IMPLEMENTED;
1257 return -1;
1258 #else
1259 return do_pwmd_save(pwm, error, 1);
1260 #endif
1263 gpg_error_t pwmd_save(pwm_t *pwm)
1265 gpg_error_t error;
1267 do_pwmd_save(pwm, &error, 0);
1268 return error;
1271 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1273 va_list ap;
1274 #ifdef USE_PINENTRY
1275 int n = va_arg(ap, int);
1276 char *result;
1277 #endif
1278 char *arg1;
1279 gpg_error_t error = 0;
1281 if (!pwm)
1282 return GPG_ERR_INV_ARG;
1284 va_start(ap, opt);
1286 switch (opt) {
1287 case PWMD_OPTION_STATUS_FUNC:
1288 pwm->status_func = va_arg(ap, pwmd_status_fn);
1289 break;
1290 case PWMD_OPTION_STATUS_DATA:
1291 pwm->status_data = va_arg(ap, void *);
1292 break;
1293 case PWMD_OPTION_PASSWORD_FUNC:
1294 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1295 break;
1296 case PWMD_OPTION_PASSWORD_DATA:
1297 pwm->passdata = va_arg(ap, void *);
1298 break;
1299 case PWMD_OPTION_PASSWORD:
1300 arg1 = va_arg(ap, char *);
1302 if (pwm->password)
1303 xfree(pwm->password);
1305 pwm->password = xstrdup(arg1);
1306 break;
1307 #ifdef USE_PINENTRY
1308 case PWMD_OPTION_PINENTRY:
1309 n = va_arg(ap, int);
1311 if (n != 0 && n != 1) {
1312 va_end(ap);
1313 error = GPG_ERR_INV_VALUE;
1315 else {
1316 pwm->use_pinentry = n;
1317 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
1318 !pwm->use_pinentry);
1320 break;
1321 case PWMD_OPTION_PINENTRY_TRIES:
1322 n = va_arg(ap, int);
1324 if (n <= 0) {
1325 va_end(ap);
1326 error = GPG_ERR_INV_VALUE;
1328 else
1329 pwm->pinentry_tries = n;
1330 break;
1331 case PWMD_OPTION_PINENTRY_PATH:
1332 if (pwm->pinentry_path)
1333 xfree(pwm->pinentry_path);
1335 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1336 break;
1337 case PWMD_OPTION_PINENTRY_TTY:
1338 if (pwm->pinentry_tty)
1339 xfree(pwm->pinentry_tty);
1341 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1342 break;
1343 case PWMD_OPTION_PINENTRY_DISPLAY:
1344 if (pwm->pinentry_display)
1345 xfree(pwm->pinentry_display);
1347 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1348 break;
1349 case PWMD_OPTION_PINENTRY_TERM:
1350 if (pwm->pinentry_term)
1351 xfree(pwm->pinentry_term);
1353 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1354 break;
1355 case PWMD_OPTION_PINENTRY_TITLE:
1356 if (pwm->title)
1357 xfree(pwm->title);
1358 pwm->title = percent_escape(va_arg(ap, char *));
1359 break;
1360 case PWMD_OPTION_PINENTRY_PROMPT:
1361 if (pwm->prompt)
1362 xfree(pwm->prompt);
1363 pwm->prompt = percent_escape(va_arg(ap, char *));
1364 break;
1365 case PWMD_OPTION_PINENTRY_DESC:
1366 if (pwm->desc)
1367 xfree(pwm->desc);
1368 pwm->desc = percent_escape(va_arg(ap, char *));
1369 break;
1370 #else
1371 case PWMD_OPTION_PINENTRY:
1372 case PWMD_OPTION_PINENTRY_TRIES:
1373 case PWMD_OPTION_PINENTRY_PATH:
1374 case PWMD_OPTION_PINENTRY_TTY:
1375 case PWMD_OPTION_PINENTRY_DISPLAY:
1376 case PWMD_OPTION_PINENTRY_TERM:
1377 case PWMD_OPTION_PINENTRY_TITLE:
1378 case PWMD_OPTION_PINENTRY_PROMPT:
1379 case PWMD_OPTION_PINENTRY_DESC:
1380 error = GPG_ERR_NOT_IMPLEMENTED;
1381 break;
1382 #endif
1383 default:
1384 error = GPG_ERR_NOT_IMPLEMENTED;
1385 break;
1388 va_end(ap);
1389 return error;
1392 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, assuan_context_t *ctx, int *fd)
1394 if (!pwm)
1395 return GPG_ERR_INV_ARG;
1397 *ctx = pwm->ctx;
1398 *fd = pwm->fd;
1399 return 0;