Added a note that PWMD_OPTION_PINENTRY should not be set when using
[libpwmd.git] / libpwmd.c
blob0e854cd8bbf617c7346b4f87588cd1c370591782
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 02111-1307 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 #ifdef HAVE_SETLOCALE
49 #include <locale.h>
50 #endif
52 #include "gettext.h"
53 #define N_(msgid) dgettext("libpwmd", msgid)
55 #ifndef MEM_DEBUG
56 #include "mem.h"
57 #else
58 #define xfree free
59 #define xrealloc realloc
60 #define xmalloc malloc
61 #define xstrdup strdup
62 #define xcalloc calloc
63 #endif
65 #include "types.h"
67 #ifdef USE_PINENTRY
68 static pwm_t *gpwm;
69 static int gelapsed, gtimeout;
70 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd);
71 static gpg_error_t global_error;
72 #endif
74 const char *pwmd_strerror(gpg_error_t e)
76 gpg_err_code_t code = gpg_err_code(e);
78 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
79 switch (code) {
80 case GPG_ERR_USER_1:
81 default:
82 return N_("Unknown error");
83 case GPG_ERR_USER_2:
84 return N_("No cache slots available");
85 case GPG_ERR_USER_3:
86 return N_("Recursion loop");
87 case GPG_ERR_USER_4:
88 return N_("No file is open");
89 case GPG_ERR_USER_5:
90 return N_("General LibXML error");
91 case GPG_ERR_USER_6:
92 return N_("File modified");
96 return gpg_strerror(e);
99 gpg_error_t pwmd_init()
101 #ifdef ENABLE_NLS
102 bindtextdomain("libpwmd", LOCALEDIR);
103 #endif
104 gpg_err_init();
105 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
106 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
107 return 0;
110 pwm_t *pwmd_connect(const char *path, gpg_error_t *error)
112 pwm_t *pwm = NULL;
113 char *socketpath = NULL;
114 time_t now;
115 struct passwd *pw;
116 assuan_context_t ctx;
117 int rc, n;
118 int active[2];
120 if (!path) {
121 pw = getpwuid(getuid());
122 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
123 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
125 else
126 socketpath = xstrdup(path);
128 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
129 xfree(socketpath);
131 if (rc) {
132 *error = rc;
133 return NULL;
136 n = assuan_get_active_fds(ctx, 0, active, sizeof(active));
138 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
139 *error = gpg_error_from_errno(errno);
140 assuan_disconnect(ctx);
141 return NULL;
144 pwm->fd = n <= 0 ? -1 : dup(active[0]);
146 if (pwm->fd != -1)
147 fcntl(pwm->fd, F_SETFL, O_NONBLOCK);
149 pwm->ctx = ctx;
150 #ifdef USE_PINENTRY
151 pwm->pid = -1;
152 pwm->pinentry_tries = 3;
153 #endif
154 time(&now);
155 srandom(now);
156 *error = 0;
157 return pwm;
160 gpg_error_t pwmd_pending_line(pwm_t *pwm, char **line, size_t *len)
162 if (!pwm)
163 return GPG_ERR_INV_ARG;
165 if (assuan_pending_line(pwm->ctx))
166 return assuan_read_line(pwm->ctx, line, len);
168 return GPG_ERR_NO_DATA;
171 void pwmd_close(pwm_t *pwm)
173 if (!pwm)
174 return;
176 if (pwm->ctx)
177 assuan_disconnect(pwm->ctx);
179 if (pwm->password)
180 xfree(pwm->password);
182 if (pwm->title)
183 xfree(pwm->title);
185 if (pwm->desc)
186 xfree(pwm->desc);
188 if (pwm->prompt)
189 xfree(pwm->prompt);
191 if (pwm->pinentry_tty)
192 xfree(pwm->pinentry_tty);
194 if (pwm->pinentry_display)
195 xfree(pwm->pinentry_display);
197 if (pwm->pinentry_term)
198 xfree(pwm->pinentry_term);
200 if (pwm->filename)
201 xfree(pwm->filename);
203 xfree(pwm);
206 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
208 membuf_t *mem = (membuf_t *)data;
209 void *p;
211 if (!buffer)
212 return 0;
214 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
215 return 1;
217 mem->buf = p;
218 memcpy((char *)mem->buf + mem->len, buffer, len);
219 mem->len += len;
220 return 0;
223 void pwmd_free_result(void *data)
225 xfree(data);
228 static int _inquire_cb(void *data, const char *keyword)
230 pwm_t *pwm = (pwm_t *)data;
231 gpg_error_t rc = 0;
232 int flags = fcntl(pwm->fd, F_GETFL);
234 /* Shouldn't get this far without a callback. */
235 if (!pwm->inquire_func)
236 return GPG_ERR_INV_ARG;
239 * Since the socket file descriptor is probably set to non-blocking, set to
240 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
241 * asynchronous INQUIRE is supported by either libassuan or a later
242 * libpwmd.
244 fcntl(pwm->fd, F_SETFL, 0);
246 for (;;) {
247 char *result = NULL;
248 size_t len;
249 gpg_error_t arc;
251 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
252 rc = gpg_err_code(rc);
254 if (rc == GPG_ERR_EOF || !rc) {
255 if (len <= 0 || !result || !*result) {
256 rc = 0;
257 break;
260 arc = assuan_send_data(pwm->ctx, result, len);
262 if (rc == GPG_ERR_EOF) {
263 rc = arc;
264 break;
267 rc = arc;
269 else if (rc)
270 break;
273 fcntl(pwm->fd, F_SETFL, flags);
274 return rc;
277 gpg_error_t pwmd_finalize(pwm_t *pwm)
279 if (!pwm || pwm->fd < 0)
280 return GPG_ERR_INV_ARG;
282 pwm->state = ASYNC_INIT;
283 return 0;
286 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
288 char *buf;
289 gpg_error_t rc;
290 size_t len = strlen(cmd) + 2;
292 len += arg ? strlen(arg) : 0;
294 if (pwm->state != ASYNC_INIT)
295 return GPG_ERR_UNEXPECTED;
297 buf = (char *)xmalloc(len);
299 if (!buf) {
300 rc = gpg_error_from_errno(ENOMEM);
301 goto fail;
304 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
305 rc = assuan_write_line(pwm->ctx, buf);
306 xfree(buf);
308 if (!rc)
309 pwm->state = ASYNC_PROCESS;
311 fail:
312 return rc;
315 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
317 if (!pwm || !filename)
318 return GPG_ERR_INV_ARG;
320 return do_nb_command(pwm, "OPEN", filename);
323 gpg_error_t pwmd_save_async(pwm_t *pwm)
325 if (!pwm)
326 return GPG_ERR_INV_ARG;
328 return do_nb_command(pwm, "SAVE", NULL);
331 static gpg_error_t parse_assuan_line(pwm_t *pwm)
333 gpg_error_t rc;
334 char *line;
335 size_t len;
337 rc = assuan_read_line(pwm->ctx, &line, &len);
339 if (!rc) {
340 if (line[0] == 'O' && line[1] == 'K' &&
341 (line[2] == 0 || line[2] == ' ')) {
342 pwm->state = ASYNC_DONE;
344 else if (line[0] == '#') {
346 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
347 if (pwm->status_func) {
348 pwm->status_func(pwm->status_data,
349 line[1] == 0 ? line+1 : line+2);
352 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
353 (line[3] == 0 || line[3] == ' ')) {
354 line += 4;
355 rc = atoi(line);
356 pwm->state = ASYNC_DONE;
360 return rc;
363 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
365 fd_set rfds;
366 int n;
367 struct timeval tv = {0, 0};
369 *rc = 0;
371 if (!pwm || pwm->fd < 0) {
372 *rc = GPG_ERR_INV_ARG;
373 return ASYNC_DONE;
375 else if (pwm->state == ASYNC_DONE)
376 return pwm->state;
377 else if (pwm->state == ASYNC_INIT) {
378 *rc = GPG_ERR_UNEXPECTED;
379 return ASYNC_DONE;
382 FD_ZERO(&rfds);
383 FD_SET(pwm->fd, &rfds);
384 n = select(pwm->fd+1, &rfds, NULL, NULL, &tv);
386 if (n > 0) {
387 if (FD_ISSET(pwm->fd, &rfds))
388 *rc = parse_assuan_line(pwm);
391 while (!*rc && assuan_pending_line(pwm->ctx))
392 *rc = parse_assuan_line(pwm);
394 return pwm->state;
397 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
398 char **result, const char *cmd)
400 membuf_t data;
401 gpg_error_t rc;
403 data.len = 0;
404 data.buf = NULL;
406 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
407 pwm->status_func, pwm->status_data);
409 if (rc) {
410 if (data.buf) {
411 xfree(data.buf);
412 data.buf = NULL;
415 else {
416 if (data.buf) {
417 mem_realloc_cb(&data, "", 1);
418 *result = (char *)data.buf;
422 return gpg_err_code(rc);
425 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
426 void *data)
428 if (!pwm || !cmd || !fn)
429 return GPG_ERR_INV_ARG;
431 pwm->inquire_func = fn;
432 pwm->inquire_data = data;
433 return assuan_command(pwm, pwm->ctx, NULL, cmd);
436 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
438 #ifndef USE_PINENTRY
439 return GPG_ERR_NOT_IMPLEMENTED;
440 #else
441 if (!pwm || pwm->pid == -1)
442 return GPG_ERR_INV_ARG;
444 if (kill(pwm->pid, 0) == 0) {
445 if (kill(pwm->pid, SIGTERM) == -1) {
446 if (kill(pwm->pid, SIGKILL) == -1)
447 return gpg_error_from_errno(errno);
450 pwm->pin_error = GPG_ERR_TIMEOUT;
452 else
453 return gpg_error_from_errno(errno);
455 return 0;
456 #endif
459 #ifdef USE_PINENTRY
460 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
462 char *buf;
463 char tmp[ASSUAN_LINELENGTH];
464 gpg_error_t error;
466 if (!pwm->title)
467 pwm->title = xstrdup(N_("LibPWMD"));
469 if (!pwm->prompt)
470 pwm->prompt = xstrdup(N_("Password:"));
472 if (!pwm->desc && !which)
473 pwm->desc = xstrdup(N_("Enter a password."));
475 if (which == 1) {
476 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
477 buf = xstrdup(tmp);
479 else if (which == 2) {
480 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
481 buf = xstrdup(tmp);
483 else {
484 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
485 sprintf(buf, "SETERROR %s", pwm->desc);
488 error = pinentry_command(pwm, NULL, buf);
489 xfree(buf);
491 if (error)
492 return error;
494 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
495 sprintf(buf, "SETPROMPT %s", pwm->prompt);
496 error = pinentry_command(pwm, NULL, buf);
497 xfree(buf);
499 if (error)
500 return error;
502 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
503 sprintf(buf, "SETDESC %s", pwm->title);
504 error = pinentry_command(pwm, NULL, buf);
505 xfree(buf);
506 return error;
509 static void update_pinentry_settings(pwm_t *pwm)
511 FILE *fp;
512 char buf[LINE_MAX];
513 struct passwd *pw = getpwuid(getuid());
514 char *p;
516 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
518 if ((fp = fopen(buf, "r")) == NULL)
519 return;
521 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
522 char name[32], val[256];
524 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
525 continue;
527 if (strcasecmp(name, "TTYNAME") == 0) {
528 xfree(pwm->pinentry_tty);
529 pwm->pinentry_tty = xstrdup(val);
531 else if (strcasecmp(name, "TTYTYPE") == 0) {
532 xfree(pwm->pinentry_term);
533 pwm->pinentry_term = xstrdup(val);
535 else if (strcasecmp(name, "DISPLAY") == 0) {
536 xfree(pwm->pinentry_display);
537 pwm->pinentry_display = xstrdup(val);
539 else if (strcasecmp(name, "PATH") == 0) {
540 xfree(pwm->pinentry_path);
541 pwm->pinentry_path = xstrdup(val);
545 fclose(fp);
548 static gpg_error_t launch_pinentry(pwm_t *pwm)
550 int rc;
551 assuan_context_t ctx;
552 int child_list[] = {-1};
553 char *display = getenv("DISPLAY");
554 const char *argv[6];
555 int have_display = 0;
556 char *tty = NULL;
558 update_pinentry_settings(pwm);
560 if (pwm->pinentry_display || display)
561 have_display = 1;
562 else {
563 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
565 if (!tty)
566 return gpg_error_from_errno(errno);
569 if (!have_display && !tty)
570 return GPG_ERR_ENOTTY;
572 argv[0] = "pinentry";
573 argv[1] = have_display ? "--display" : "--ttyname";
574 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
575 argv[3] = NULL;
577 if (!have_display) {
578 argv[3] = "--ttytype";
579 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
580 argv[5] = NULL;
583 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
585 if (rc)
586 return rc;
588 pwm->pid = assuan_get_pid(ctx);
589 pwm->pctx = ctx;
590 return set_pinentry_strings(pwm, 0);
593 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
595 gpg_error_t n;
597 if (!pwm->pctx) {
598 n = launch_pinentry(pwm);
600 if (n)
601 return n;
604 return assuan_command(pwm, pwm->pctx, result, cmd);
607 static void pinentry_disconnect(pwm_t *pwm)
609 if (pwm->pctx)
610 assuan_disconnect(pwm->pctx);
612 pwm->pctx = NULL;
613 pwm->pid = -1;
617 * Only called from a child process.
619 static void catchsig(int sig)
621 switch (sig) {
622 case SIGALRM:
623 if (gelapsed++ >= gtimeout) {
624 global_error = pwmd_terminate_pinentry(gpwm);
626 if (!global_error)
627 global_error = GPG_ERR_TIMEOUT;
629 break;
632 alarm(1);
633 break;
634 default:
635 break;
640 * Borrowed from libassuan.
642 static char *percent_escape(const char *atext)
644 const unsigned char *s;
645 int len = strlen(atext) * 3 + 1;
646 char *buf = (char *)xmalloc(len), *p = buf;
648 if (!buf)
649 return NULL;
651 for (s=(const unsigned char *)atext; *s; s++) {
652 if (*s < ' ') {
653 sprintf (p, "%%%02X", *s);
654 p += 3;
656 else
657 *p++ = *s;
660 *p = 0;
661 return buf;
663 #endif
665 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
667 if (!cmd)
668 return GPG_ERR_INV_ARG;
670 return assuan_command(pwm, pwm->ctx, result, cmd);
674 * Avoid sending the BYE command here. libassuan will close the file
675 * descriptor and release the assuan context. Use pwmd_close() instead.
677 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
679 va_list ap;
680 char *buf;
681 size_t len;
682 gpg_error_t error;
684 if (!pwm || !cmd)
685 return GPG_ERR_INV_ARG;
687 *result = NULL;
688 va_start(ap, cmd);
690 * C99 allows the dst pointer to be null which will calculate the length
691 * of the result and return it.
693 len = vsnprintf(NULL, 0, cmd, ap);
694 buf = (char *)xmalloc(len + 1);
695 len = vsnprintf(buf, len + 1, cmd, ap);
696 va_end(ap);
697 error = send_command(pwm, result, buf);
698 xfree(buf);
699 return error;
702 #ifdef USE_PINENTRY
703 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
705 if (gtimeout) {
706 signal(SIGALRM, catchsig);
707 alarm(1);
710 *result = NULL;
711 return pinentry_command(pwm, result, "GETPIN");
714 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
716 int pin_try = *try_n;
717 gpg_error_t error;
719 getpin_again:
720 *try_n = pin_try;
722 if (pin_try == -1) {
723 error = set_pinentry_strings(pwm, which);
725 if (error) {
726 pinentry_disconnect(pwm);
727 return error;
730 else {
731 if (pwm->pinentry_tries-1 != pin_try) {
732 error = set_pinentry_strings(pwm, 1);
734 if (error) {
735 pinentry_disconnect(pwm);
736 return error;
741 error = do_getpin(pwm, result);
744 * Since there was input cancel any timeout setting.
746 alarm(0);
748 if (error) {
749 if (error == GPG_ERR_ASS_CANCELED || error == ASSUAN_Canceled)
750 return GPG_ERR_ASS_CANCELED;
752 if (pin_try != -1 && pin_try--)
753 goto getpin_again;
755 if (pwm->pctx)
756 pinentry_disconnect(pwm);
758 *try_n = pin_try;
759 return error;
762 return 0;
764 #endif
766 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
768 gpg_error_t error;
770 #ifndef USE_PINENTRY
771 return GPG_ERR_NOT_IMPLEMENTED;
772 #endif
774 if (!pwm || !pw || !pw->filename[0])
775 return GPG_ERR_INV_ARG;
777 close(pw->fd);
779 if (pw->error) {
780 error = pw->error;
781 goto fail;
784 if (pwm->filename)
785 xfree(pwm->filename);
787 pwm->filename = xstrdup(pw->filename);
788 memset(pw, 0, sizeof(pwmd_nb_status_t));
789 return 0;
791 fail:
792 memset(pw, 0, sizeof(pwmd_nb_status_t));
793 return error;
796 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
798 char buf[ASSUAN_LINELENGTH];
799 gpg_error_t error;
800 char *result = NULL;
802 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
803 error = send_command(pwm, &result, buf);
804 memset(buf, 0, sizeof(buf));
806 if (error && result)
807 xfree(result);
809 return error;
812 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
813 int nb, int timeout)
815 char *result = NULL;
816 char *password = NULL;
817 char path[PATH_MAX];
818 #ifdef USE_PINENTRY
819 int pin_try;
820 #endif
822 if (!pwm || !filename || !*filename) {
823 *error = GPG_ERR_INV_ARG;
824 return nb ? -1 : 1;
827 #ifdef USE_PINENTRY
828 pin_try = pwm->pinentry_tries - 1;
829 #endif
832 * Avoid calling pinentry if the password is cached on the server or if
833 * this is a new file.
835 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
837 if (*error)
838 return nb ? -1 : 1;
840 snprintf(path, sizeof(path), "%s/%s", result, filename);
841 pwmd_free_result(result);
843 if (access(path, R_OK) == -1) {
844 if (errno == ENOENT)
845 goto gotpassword;
848 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
850 if (*error == EPWMD_CACHE_NOT_FOUND) {
851 if (pwm->passfunc) {
852 password = pwm->passfunc(pwm, pwm->passdata);
853 goto gotpassword;
856 #ifdef USE_PINENTRY
858 * Get the password from pinentry.
860 if (pwm->use_pinentry) {
862 * Nonblocking is wanted. fork() then return a file descriptor
863 * that the client can use to read() from.
865 if (nb) {
866 int p[2];
867 pid_t pid;
868 pwmd_nb_status_t pw;
870 if (pipe(p) == -1) {
871 *error = gpg_error_from_syserror();
872 return -1;
875 pid = fork();
877 switch (pid) {
878 case 0:
879 close(p[0]);
880 strncpy(pw.filename, filename, sizeof(pw.filename));
881 pw.filename[sizeof(pw.filename)-1] = 0;
882 pw.fd = p[0];
884 if (timeout > 0) {
885 gpwm = pwm;
886 gtimeout = timeout;
887 gelapsed = 0;
890 getpin_nb_again:
891 *error = getpin(pwm, &password, &pin_try, 0);
893 if (*error) {
894 getpin_nb_fail:
895 if (pwm->pctx)
896 pinentry_disconnect(pwm);
898 if (gtimeout && gelapsed >= gtimeout)
899 *error = GPG_ERR_TIMEOUT;
901 pw.error = *error;
902 write(p[1], &pw, sizeof(pw));
903 close(p[1]);
904 _exit(1);
908 * Don't count the time it takes to open the file
909 * which may have many iterations.
911 signal(SIGALRM, SIG_DFL);
912 *error = do_open_command(pwm, filename, password);
914 if (timeout)
915 signal(SIGALRM, catchsig);
917 if (pwm->pctx && *error == EPWMD_BADKEY) {
918 if (pin_try-- > 0)
919 goto getpin_nb_again;
921 goto getpin_nb_fail;
924 pinentry_disconnect(pwm);
925 pw.error = 0;
926 write(p[1], &pw, sizeof(pw));
927 close(p[1]);
928 _exit(0);
929 break;
930 case -1:
931 *error = gpg_error_from_syserror();
932 close(p[0]);
933 close(p[1]);
934 return -1;
935 default:
936 break;
939 close(p[1]);
940 return p[0];
943 getpin_again:
944 *error = getpin(pwm, &password, &pin_try, 1);
946 if (*error) {
947 if (pwm->pctx)
948 pinentry_disconnect(pwm);
950 if (pwm->pin_error) {
951 *error = pwm->pin_error;
952 pwm->pin_error = 0;
955 return 1;
958 else {
959 #endif
961 * Not using pinentry and the file was not found
962 * in the cache.
964 password = pwm->password;
965 #ifdef USE_PINENTRY
967 #endif
969 else if (*error)
970 return nb ? -1 : 1;
972 gotpassword:
973 *error = do_open_command(pwm, filename, password);
976 * Keep the user defined password set with pwmd_setopt(). The password may
977 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
979 if (password && password != pwm->password)
980 xfree(password);
982 #ifdef USE_PINENTRY
983 if (pwm->pctx && *error == EPWMD_BADKEY) {
984 if (pin_try-- > 0)
985 goto getpin_again;
987 pinentry_disconnect(pwm);
988 return nb ? -1 : 1;
990 else if (pwm->pctx)
991 pinentry_disconnect(pwm);
992 #endif
994 if (!*error) {
995 if (pwm->filename)
996 xfree(pwm->filename);
998 pwm->filename = xstrdup(filename);
1002 * The file is cached or the file is a new file.
1004 if (nb)
1005 return *error ? -1 : -2;
1007 return *error ? 1 : 0;
1010 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1012 gpg_error_t error;
1014 do_pwmd_open(pwm, &error, filename, 0, 0);
1015 return error;
1018 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1019 int timeout)
1021 #ifndef USE_PINENTRY
1022 *error = GPG_ERR_NOT_IMPLEMENTED;
1023 return -1;
1024 #else
1025 return do_pwmd_open(pwm, error, filename, 1, timeout);
1026 #endif
1029 #ifdef USE_PINENTRY
1030 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1032 int confirm = 0;
1033 gpg_error_t error;
1034 char *result = NULL;
1035 int pin_try = -1;
1037 again:
1038 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1040 if (error) {
1041 if (pwm->pctx)
1042 pinentry_disconnect(pwm);
1044 if (*password)
1045 xfree(*password);
1047 return error;
1050 if (!confirm++) {
1051 *password = result;
1052 goto again;
1055 if (strcmp(*password, result)) {
1056 xfree(*password);
1057 xfree(result);
1058 pinentry_disconnect(pwm);
1059 error = EPWMD_BADKEY;
1060 return error;
1063 xfree(result);
1064 pinentry_disconnect(pwm);
1065 return 0;
1067 #endif
1069 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1071 char buf[ASSUAN_LINELENGTH];
1072 gpg_error_t error;
1073 char *result = NULL;
1075 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1076 error = send_command(pwm, &result, buf);
1077 memset(&buf, 0, sizeof(buf));
1079 if (error && result)
1080 xfree(result);
1082 return error;
1085 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1087 gpg_error_t rc;
1089 #ifndef USE_PINENTRY
1090 return GPG_ERR_NOT_IMPLEMENTED;
1091 #endif
1093 if (!pwm || !pw || !pw->filename[0])
1094 return GPG_ERR_INV_ARG;
1096 close(pw->fd);
1097 rc = pw->error;
1098 memset(pw, 0, sizeof(pwmd_nb_status_t));
1099 return rc;
1102 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1104 char *result = NULL;
1105 char *password = NULL;
1107 if (!pwm) {
1108 *error = GPG_ERR_INV_ARG;
1109 return nb ? -1 : 1;
1112 if (pwm->use_pinentry || pwm->passfunc) {
1113 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1115 if (*error == EPWMD_CACHE_NOT_FOUND) {
1116 #ifdef USE_PINENTRY
1117 if (pwm->use_pinentry) {
1118 if (nb) {
1119 int p[2];
1120 pid_t pid;
1121 pwmd_nb_status_t pw;
1123 if (pipe(p) == -1) {
1124 *error = gpg_error_from_syserror();
1125 return -1;
1128 pid = fork();
1130 switch (pid) {
1131 case 0:
1132 close(p[0]);
1133 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1134 pw.filename[sizeof(pw.filename)-1] = 0;
1135 pw.fd = p[0];
1137 do {
1138 password = NULL;
1139 *error = do_save_getpin(pwm, &password);
1140 } while (*error == EPWMD_BADKEY);
1142 if (*error) {
1143 if (pwm->pctx)
1144 pinentry_disconnect(pwm);
1146 pw.error = *error;
1147 write(p[1], &pw, sizeof(pw));
1148 close(p[1]);
1149 _exit(1);
1152 *error = do_save_command(pwm, password);
1153 pinentry_disconnect(pwm);
1154 pw.error = *error;
1155 write(p[1], &pw, sizeof(pw));
1156 close(p[1]);
1157 _exit(0);
1158 break;
1159 case -1:
1160 *error = gpg_error_from_syserror();
1161 close(p[0]);
1162 close(p[1]);
1163 return -1;
1164 default:
1165 break;
1168 close(p[1]);
1169 return p[0];
1172 *error = do_save_getpin(pwm, &password);
1174 if (*error)
1175 return 1;
1177 else {
1178 #endif
1179 if (pwm->passfunc)
1180 password = (*pwm->passfunc)(pwm, pwm->passdata);
1181 #ifdef USE_PINENTRY
1183 #endif
1185 else {
1186 if (*error)
1187 return nb ? -1 : 1;
1190 else
1191 password = pwm->password;
1193 *error = do_save_command(pwm, password);
1195 if (password && password != pwm->password)
1196 xfree(password);
1198 if (nb)
1199 return *error ? -1 : -2;
1201 return *error ? 1 : 0;
1204 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1206 #ifndef USE_PINENTRY
1207 *error = GPG_ERR_NOT_IMPLEMENTED;
1208 return -1;
1209 #else
1210 return do_pwmd_save(pwm, error, 1);
1211 #endif
1214 gpg_error_t pwmd_save(pwm_t *pwm)
1216 gpg_error_t error;
1218 do_pwmd_save(pwm, &error, 0);
1219 return error;
1222 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1224 va_list ap;
1225 #ifdef USE_PINENTRY
1226 int n = va_arg(ap, int);
1227 char *result;
1228 #endif
1229 char *arg1;
1230 gpg_error_t error = 0;
1232 if (!pwm)
1233 return GPG_ERR_INV_ARG;
1235 va_start(ap, opt);
1237 switch (opt) {
1238 case PWMD_OPTION_STATUS_FUNC:
1239 pwm->status_func = va_arg(ap, pwmd_status_fn);
1240 break;
1241 case PWMD_OPTION_STATUS_DATA:
1242 pwm->status_data = va_arg(ap, void *);
1243 break;
1244 case PWMD_OPTION_PASSWORD_FUNC:
1245 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1246 break;
1247 case PWMD_OPTION_PASSWORD_DATA:
1248 pwm->passdata = va_arg(ap, void *);
1249 break;
1250 case PWMD_OPTION_PASSWORD:
1251 arg1 = va_arg(ap, char *);
1253 if (pwm->password)
1254 xfree(pwm->password);
1256 pwm->password = xstrdup(arg1);
1257 break;
1258 #ifdef USE_PINENTRY
1259 case PWMD_OPTION_PINENTRY:
1260 n = va_arg(ap, int);
1262 if (n != 0 && n != 1) {
1263 va_end(ap);
1264 error = GPG_ERR_INV_VALUE;
1266 else {
1267 pwm->use_pinentry = n;
1268 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
1269 !pwm->use_pinentry);
1271 break;
1272 case PWMD_OPTION_PINENTRY_TRIES:
1273 n = va_arg(ap, int);
1275 if (n <= 0) {
1276 va_end(ap);
1277 error = GPG_ERR_INV_VALUE;
1279 else
1280 pwm->pinentry_tries = n;
1281 break;
1282 case PWMD_OPTION_PINENTRY_PATH:
1283 if (pwm->pinentry_path)
1284 xfree(pwm->pinentry_path);
1286 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1287 break;
1288 case PWMD_OPTION_PINENTRY_TTY:
1289 if (pwm->pinentry_tty)
1290 xfree(pwm->pinentry_tty);
1292 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1293 break;
1294 case PWMD_OPTION_PINENTRY_DISPLAY:
1295 if (pwm->pinentry_display)
1296 xfree(pwm->pinentry_display);
1298 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1299 break;
1300 case PWMD_OPTION_PINENTRY_TERM:
1301 if (pwm->pinentry_term)
1302 xfree(pwm->pinentry_term);
1304 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1305 break;
1306 case PWMD_OPTION_PINENTRY_TITLE:
1307 if (pwm->title)
1308 xfree(pwm->title);
1309 pwm->title = percent_escape(va_arg(ap, char *));
1310 break;
1311 case PWMD_OPTION_PINENTRY_PROMPT:
1312 if (pwm->prompt)
1313 xfree(pwm->prompt);
1314 pwm->prompt = percent_escape(va_arg(ap, char *));
1315 break;
1316 case PWMD_OPTION_PINENTRY_DESC:
1317 if (pwm->desc)
1318 xfree(pwm->desc);
1319 pwm->desc = percent_escape(va_arg(ap, char *));
1320 break;
1321 #else
1322 case PWMD_OPTION_PINENTRY:
1323 case PWMD_OPTION_PINENTRY_TRIES:
1324 case PWMD_OPTION_PINENTRY_PATH:
1325 case PWMD_OPTION_PINENTRY_TTY:
1326 case PWMD_OPTION_PINENTRY_DISPLAY:
1327 case PWMD_OPTION_PINENTRY_TERM:
1328 case PWMD_OPTION_PINENTRY_TITLE:
1329 case PWMD_OPTION_PINENTRY_PROMPT:
1330 case PWMD_OPTION_PINENTRY_DESC:
1331 error = GPG_ERR_NOT_IMPLEMENTED;
1332 break;
1333 #endif
1334 default:
1335 error = GPG_ERR_NOT_IMPLEMENTED;
1336 break;
1339 va_end(ap);
1340 return error;
1343 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, assuan_context_t *ctx, int *fd)
1345 if (!pwm)
1346 return GPG_ERR_INV_ARG;
1348 *ctx = pwm->ctx;
1349 *fd = pwm->fd;
1350 return 0;