Added pwmd_command_ap(). This makes pwmd_command() a wrapper around this
[libpwmd.git] / libpwmd.c
blob72ea360aecba67eac54a0fc61d90e56aa800d3ab
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_("Passphrase:"));
503 if (!pwm->desc && !which)
504 pwm->desc = xstrdup(N_("Enter a passphrase."));
506 if (which == 1) {
507 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid passphrase, please try again."));
508 buf = xstrdup(tmp);
510 else if (which == 2) {
511 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the passphrase 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);
704 gpg_error_t pwmd_command_ap(pwm_t *pwm, char **result, const char *cmd,
705 va_list ap)
707 char *buf;
708 size_t len;
709 gpg_error_t error;
712 * C99 allows the dst pointer to be null which will calculate the length
713 * of the would-be result and return it.
715 len = vsnprintf(NULL, 0, cmd, ap)+1;
716 buf = (char *)xmalloc(len);
717 len = vsnprintf(buf, len, cmd, ap);
718 error = send_command(pwm, result, buf);
719 xfree(buf);
720 return error;
724 * Avoid sending the BYE command here. libassuan will close the file
725 * descriptor and release the assuan context. Use pwmd_close() instead.
727 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
729 va_list ap;
730 gpg_error_t error;
732 if (!pwm || !cmd)
733 return GPG_ERR_INV_ARG;
735 *result = NULL;
736 va_start(ap, cmd);
737 error = pwmd_command_ap(pwm, result, cmd, ap);
738 va_end(ap);
739 return error;
742 #ifdef USE_PINENTRY
743 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
745 if (gtimeout) {
746 signal(SIGALRM, catchsig);
747 alarm(1);
750 *result = NULL;
751 return pinentry_command(pwm, result, "GETPIN");
754 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
756 int pin_try = *try_n;
757 gpg_error_t error;
759 getpin_again:
760 *try_n = pin_try;
762 if (pin_try == -1) {
763 error = set_pinentry_strings(pwm, which);
765 if (error) {
766 pinentry_disconnect(pwm);
767 return error;
770 else {
771 if (pwm->pinentry_tries-1 != pin_try) {
772 error = set_pinentry_strings(pwm, 1);
774 if (error) {
775 pinentry_disconnect(pwm);
776 return error;
781 error = do_getpin(pwm, result);
784 * Since there was input cancel any timeout setting.
786 alarm(0);
788 if (error) {
789 if (error == GPG_ERR_CANCELED)
790 return GPG_ERR_CANCELED;
792 if (pin_try != -1 && pin_try--)
793 goto getpin_again;
795 if (pwm->pctx)
796 pinentry_disconnect(pwm);
798 *try_n = pin_try;
799 return error;
802 return 0;
804 #endif
806 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
808 gpg_error_t error;
810 #ifndef USE_PINENTRY
811 return GPG_ERR_NOT_IMPLEMENTED;
812 #endif
814 if (!pwm || !pw || !pw->filename[0])
815 return GPG_ERR_INV_ARG;
817 close(pw->fd);
819 if (pw->error) {
820 error = pw->error;
821 goto fail;
824 if (pwm->filename)
825 xfree(pwm->filename);
827 pwm->filename = xstrdup(pw->filename);
828 memset(pw, 0, sizeof(pwmd_nb_status_t));
829 return 0;
831 fail:
832 memset(pw, 0, sizeof(pwmd_nb_status_t));
833 return error;
836 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
838 char buf[ASSUAN_LINELENGTH];
839 gpg_error_t error;
840 char *result = NULL;
842 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
843 error = send_command(pwm, &result, buf);
844 memset(buf, 0, sizeof(buf));
846 if (error && result)
847 xfree(result);
849 return error;
852 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
853 int nb, int timeout)
855 char *result = NULL;
856 char *password = NULL;
857 char path[PATH_MAX];
858 #ifdef USE_PINENTRY
859 int pin_try;
860 #endif
862 if (!pwm || !filename || !*filename) {
863 *error = GPG_ERR_INV_ARG;
864 return nb ? -1 : 1;
867 #ifdef USE_PINENTRY
868 pin_try = pwm->pinentry_tries - 1;
869 #endif
872 * Avoid calling pinentry if the password is cached on the server or if
873 * this is a new file.
875 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
877 if (*error)
878 return nb ? -1 : 1;
880 snprintf(path, sizeof(path), "%s/%s", result, filename);
881 pwmd_free_result(result);
883 if (access(path, R_OK) == -1) {
884 if (errno == ENOENT)
885 goto gotpassword;
888 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
890 if (*error == EPWMD_CACHE_NOT_FOUND) {
891 if (pwm->passfunc) {
892 password = pwm->passfunc(pwm, pwm->passdata);
893 goto gotpassword;
896 #ifdef USE_PINENTRY
898 * Get the password from pinentry.
900 if (pwm->use_pinentry) {
902 * Nonblocking is wanted. fork() then return a file descriptor
903 * that the client can use to read() from.
905 if (nb) {
906 int p[2];
907 pid_t pid;
908 pwmd_nb_status_t pw;
910 if (pipe(p) == -1) {
911 *error = gpg_error_from_syserror();
912 return -1;
915 #ifdef WITH_LIBPTH
916 pid = pth_fork();
917 #else
918 pid = fork();
919 #endif
921 switch (pid) {
922 case 0:
923 close(p[0]);
924 strncpy(pw.filename, filename, sizeof(pw.filename));
925 pw.filename[sizeof(pw.filename)-1] = 0;
926 pw.fd = p[0];
928 if (timeout > 0) {
929 gpwm = pwm;
930 gtimeout = timeout;
931 gelapsed = 0;
934 getpin_nb_again:
935 *error = getpin(pwm, &password, &pin_try, 0);
937 if (*error) {
938 getpin_nb_fail:
939 if (pwm->pctx)
940 pinentry_disconnect(pwm);
942 if (gtimeout && gelapsed >= gtimeout)
943 *error = GPG_ERR_TIMEOUT;
945 pw.error = *error;
946 #ifdef WITH_LIBPTH
947 pth_write(p[1], &pw, sizeof(pw));
948 #else
949 write(p[1], &pw, sizeof(pw));
950 #endif
951 close(p[1]);
952 _exit(1);
956 * Don't count the time it takes to open the file
957 * which may have many iterations.
959 signal(SIGALRM, SIG_DFL);
960 *error = do_open_command(pwm, filename, password);
962 if (timeout)
963 signal(SIGALRM, catchsig);
965 if (pwm->pctx && *error == EPWMD_BADKEY) {
966 if (pin_try-- > 0)
967 goto getpin_nb_again;
969 goto getpin_nb_fail;
972 pinentry_disconnect(pwm);
973 pw.error = 0;
974 #ifdef WITH_LIBPTH
975 pth_write(p[1], &pw, sizeof(pw));
976 #else
977 write(p[1], &pw, sizeof(pw));
978 #endif
979 close(p[1]);
980 _exit(0);
981 break;
982 case -1:
983 *error = gpg_error_from_syserror();
984 close(p[0]);
985 close(p[1]);
986 return -1;
987 default:
988 break;
991 close(p[1]);
992 *error = 0;
993 return p[0];
996 else {
997 #endif
999 * Not using pinentry and the file was not found
1000 * in the cache.
1002 password = pwm->password;
1003 #ifdef USE_PINENTRY
1005 #endif
1007 else if (*error)
1008 return nb ? -1 : 1;
1010 gotpassword:
1011 *error = do_open_command(pwm, filename, password);
1014 * Keep the user defined password set with pwmd_setopt(). The password may
1015 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1017 if (password && password != pwm->password)
1018 xfree(password);
1020 #ifdef USE_PINENTRY
1021 if (*error == EPWMD_BADKEY) {
1022 if (pin_try-- > 0 && !nb) {
1023 *error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
1024 N_("Invalid passphrase, please try again."));
1026 if (*error)
1027 return 1;
1029 goto gotpassword;
1032 if (nb)
1033 pinentry_disconnect(pwm);
1035 return nb ? -1 : 1;
1037 #endif
1039 if (!*error) {
1040 if (pwm->filename)
1041 xfree(pwm->filename);
1043 pwm->filename = xstrdup(filename);
1047 * The file is cached or the file is a new file.
1049 if (nb)
1050 return *error ? -1 : -2;
1052 return *error ? 1 : 0;
1055 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1057 gpg_error_t error;
1059 do_pwmd_open(pwm, &error, filename, 0, 0);
1060 return error;
1063 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1064 int timeout)
1066 #ifndef USE_PINENTRY
1067 *error = GPG_ERR_NOT_IMPLEMENTED;
1068 return -1;
1069 #else
1070 return do_pwmd_open(pwm, error, filename, 1, timeout);
1071 #endif
1074 #ifdef USE_PINENTRY
1075 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1077 int confirm = 0;
1078 gpg_error_t error;
1079 char *result = NULL;
1080 int pin_try = -1;
1082 again:
1083 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1085 if (error) {
1086 if (pwm->pctx)
1087 pinentry_disconnect(pwm);
1089 if (*password)
1090 xfree(*password);
1092 return error;
1095 if (!confirm++) {
1096 *password = result;
1097 goto again;
1100 if (strcmp(*password, result)) {
1101 xfree(*password);
1102 xfree(result);
1103 pinentry_disconnect(pwm);
1104 error = EPWMD_BADKEY;
1105 return error;
1108 xfree(result);
1109 pinentry_disconnect(pwm);
1110 return 0;
1112 #endif
1114 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1116 char buf[ASSUAN_LINELENGTH];
1117 gpg_error_t error;
1118 char *result = NULL;
1120 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1121 error = send_command(pwm, &result, buf);
1122 memset(&buf, 0, sizeof(buf));
1124 if (error && result)
1125 xfree(result);
1127 return error;
1130 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1132 gpg_error_t rc;
1134 #ifndef USE_PINENTRY
1135 return GPG_ERR_NOT_IMPLEMENTED;
1136 #endif
1138 if (!pwm || !pw || !pw->filename[0])
1139 return GPG_ERR_INV_ARG;
1141 close(pw->fd);
1142 rc = pw->error;
1143 memset(pw, 0, sizeof(pwmd_nb_status_t));
1144 return rc;
1147 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1149 char *result = NULL;
1150 char *password = NULL;
1152 if (!pwm) {
1153 *error = GPG_ERR_INV_ARG;
1154 return nb ? -1 : 1;
1157 if (pwm->use_pinentry || pwm->passfunc) {
1158 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1160 if (*error == EPWMD_CACHE_NOT_FOUND) {
1161 #ifdef USE_PINENTRY
1162 if (pwm->use_pinentry) {
1163 if (nb) {
1164 int p[2];
1165 pid_t pid;
1166 pwmd_nb_status_t pw;
1168 if (pipe(p) == -1) {
1169 *error = gpg_error_from_syserror();
1170 return -1;
1173 #ifdef WITH_LIBPTH
1174 pid = pth_fork();
1175 #else
1176 pid = fork();
1177 #endif
1179 switch (pid) {
1180 case 0:
1181 close(p[0]);
1182 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1183 pw.filename[sizeof(pw.filename)-1] = 0;
1184 pw.fd = p[0];
1186 do {
1187 password = NULL;
1188 *error = do_save_getpin(pwm, &password);
1189 } while (*error == EPWMD_BADKEY);
1191 if (*error) {
1192 if (pwm->pctx)
1193 pinentry_disconnect(pwm);
1195 pw.error = *error;
1196 #ifdef WITH_LIBPTH
1197 pth_write(p[1], &pw, sizeof(pw));
1198 #else
1199 write(p[1], &pw, sizeof(pw));
1200 #endif
1201 close(p[1]);
1202 _exit(1);
1205 *error = do_save_command(pwm, password);
1206 pinentry_disconnect(pwm);
1207 pw.error = *error;
1208 #ifdef WITH_LIBPTH
1209 pth_write(p[1], &pw, sizeof(pw));
1210 #else
1211 write(p[1], &pw, sizeof(pw));
1212 #endif
1213 close(p[1]);
1214 _exit(0);
1215 break;
1216 case -1:
1217 *error = gpg_error_from_syserror();
1218 close(p[0]);
1219 close(p[1]);
1220 return -1;
1221 default:
1222 break;
1225 close(p[1]);
1226 *error = 0;
1227 return p[0];
1230 *error = do_save_getpin(pwm, &password);
1232 if (*error)
1233 return 1;
1235 else {
1236 #endif
1237 if (pwm->passfunc)
1238 password = (*pwm->passfunc)(pwm, pwm->passdata);
1239 #ifdef USE_PINENTRY
1241 #endif
1243 else {
1244 if (*error)
1245 return nb ? -1 : 1;
1248 else
1249 password = pwm->password;
1251 *error = do_save_command(pwm, password);
1253 if (password && password != pwm->password)
1254 xfree(password);
1256 if (nb)
1257 return *error ? -1 : -2;
1259 return *error ? 1 : 0;
1262 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1264 #ifndef USE_PINENTRY
1265 *error = GPG_ERR_NOT_IMPLEMENTED;
1266 return -1;
1267 #else
1268 return do_pwmd_save(pwm, error, 1);
1269 #endif
1272 gpg_error_t pwmd_save(pwm_t *pwm)
1274 gpg_error_t error;
1276 do_pwmd_save(pwm, &error, 0);
1277 return error;
1280 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1282 va_list ap;
1283 #ifdef USE_PINENTRY
1284 int n = va_arg(ap, int);
1285 char *result;
1286 #endif
1287 char *arg1;
1288 gpg_error_t error = 0;
1290 if (!pwm)
1291 return GPG_ERR_INV_ARG;
1293 va_start(ap, opt);
1295 switch (opt) {
1296 case PWMD_OPTION_STATUS_FUNC:
1297 pwm->status_func = va_arg(ap, pwmd_status_fn);
1298 break;
1299 case PWMD_OPTION_STATUS_DATA:
1300 pwm->status_data = va_arg(ap, void *);
1301 break;
1302 case PWMD_OPTION_PASSWORD_FUNC:
1303 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1304 break;
1305 case PWMD_OPTION_PASSWORD_DATA:
1306 pwm->passdata = va_arg(ap, void *);
1307 break;
1308 case PWMD_OPTION_PASSWORD:
1309 arg1 = va_arg(ap, char *);
1311 if (pwm->password)
1312 xfree(pwm->password);
1314 pwm->password = xstrdup(arg1);
1315 break;
1316 #ifdef USE_PINENTRY
1317 case PWMD_OPTION_PINENTRY:
1318 n = va_arg(ap, int);
1320 if (n != 0 && n != 1) {
1321 va_end(ap);
1322 error = GPG_ERR_INV_VALUE;
1324 else {
1325 pwm->use_pinentry = n;
1326 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
1327 !pwm->use_pinentry);
1329 break;
1330 case PWMD_OPTION_PINENTRY_TRIES:
1331 n = va_arg(ap, int);
1333 if (n <= 0) {
1334 va_end(ap);
1335 error = GPG_ERR_INV_VALUE;
1337 else
1338 pwm->pinentry_tries = n;
1339 break;
1340 case PWMD_OPTION_PINENTRY_PATH:
1341 if (pwm->pinentry_path)
1342 xfree(pwm->pinentry_path);
1344 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1345 break;
1346 case PWMD_OPTION_PINENTRY_TTY:
1347 if (pwm->pinentry_tty)
1348 xfree(pwm->pinentry_tty);
1350 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1351 break;
1352 case PWMD_OPTION_PINENTRY_DISPLAY:
1353 if (pwm->pinentry_display)
1354 xfree(pwm->pinentry_display);
1356 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1357 break;
1358 case PWMD_OPTION_PINENTRY_TERM:
1359 if (pwm->pinentry_term)
1360 xfree(pwm->pinentry_term);
1362 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1363 break;
1364 case PWMD_OPTION_PINENTRY_TITLE:
1365 if (pwm->title)
1366 xfree(pwm->title);
1367 pwm->title = percent_escape(va_arg(ap, char *));
1368 break;
1369 case PWMD_OPTION_PINENTRY_PROMPT:
1370 if (pwm->prompt)
1371 xfree(pwm->prompt);
1372 pwm->prompt = percent_escape(va_arg(ap, char *));
1373 break;
1374 case PWMD_OPTION_PINENTRY_DESC:
1375 if (pwm->desc)
1376 xfree(pwm->desc);
1377 pwm->desc = percent_escape(va_arg(ap, char *));
1378 break;
1379 #else
1380 case PWMD_OPTION_PINENTRY:
1381 case PWMD_OPTION_PINENTRY_TRIES:
1382 case PWMD_OPTION_PINENTRY_PATH:
1383 case PWMD_OPTION_PINENTRY_TTY:
1384 case PWMD_OPTION_PINENTRY_DISPLAY:
1385 case PWMD_OPTION_PINENTRY_TERM:
1386 case PWMD_OPTION_PINENTRY_TITLE:
1387 case PWMD_OPTION_PINENTRY_PROMPT:
1388 case PWMD_OPTION_PINENTRY_DESC:
1389 error = GPG_ERR_NOT_IMPLEMENTED;
1390 break;
1391 #endif
1392 default:
1393 error = GPG_ERR_NOT_IMPLEMENTED;
1394 break;
1397 va_end(ap);
1398 return error;
1402 * Prevent requiring assuan.h when setting ctx. The ctx is really an
1403 * assuan_context_t *.
1405 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, void *ctx, int *fd)
1407 if (!pwm)
1408 return GPG_ERR_INV_ARG;
1410 ctx = pwm->ctx;
1411 *fd = pwm->fd;
1412 return 0;