Call assuan_pending_line() even if the socket was ready for a read in
[libpwmd.git] / libpwmd.c
blob0329b0cf21e8f142e61ba5169649bbf490317b0a
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
75 const char *pwmd_strerror(gpg_error_t e)
77 gpg_err_code_t code = gpg_err_code(e);
79 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
80 switch (code) {
81 case GPG_ERR_USER_1:
82 default:
83 return N_("Unknown error");
84 case GPG_ERR_USER_2:
85 return N_("No cache slots available");
86 case GPG_ERR_USER_3:
87 return N_("Recursion loop");
88 case GPG_ERR_USER_4:
89 return N_("No file is open");
90 case GPG_ERR_USER_5:
91 return N_("General LibXML error");
92 case GPG_ERR_USER_6:
93 return N_("File modified");
97 return gpg_strerror(e);
100 gpg_error_t pwmd_init()
102 #ifdef ENABLE_NLS
103 bindtextdomain("libpwmd", LOCALEDIR);
104 #endif
105 gpg_err_init();
106 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
107 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
108 return 0;
111 pwm_t *pwmd_connect(const char *path, gpg_error_t *error)
113 pwm_t *pwm = NULL;
114 char *socketpath = NULL;
115 time_t now;
116 struct passwd *pw;
117 assuan_context_t ctx;
118 int rc, n;
119 int active[2];
121 if (!path) {
122 pw = getpwuid(getuid());
123 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
124 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
126 else
127 socketpath = xstrdup(path);
129 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
130 xfree(socketpath);
132 if (rc) {
133 *error = rc;
134 return NULL;
137 n = assuan_get_active_fds(ctx, 0, active, sizeof(active));
139 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
140 *error = gpg_error_from_errno(errno);
141 assuan_disconnect(ctx);
142 return NULL;
145 pwm->fd = n <= 0 ? -1 : dup(active[0]);
147 if (pwm->fd != -1)
148 fcntl(pwm->fd, F_SETFL, O_NONBLOCK);
150 pwm->ctx = ctx;
151 #ifdef USE_PINENTRY
152 pwm->pid = -1;
153 pwm->pinentry_tries = 3;
154 #endif
155 time(&now);
156 srandom(now);
157 *error = 0;
158 return pwm;
161 gpg_error_t pwmd_pending_line(pwm_t *pwm, char **line, size_t *len)
163 if (!pwm)
164 return GPG_ERR_INV_ARG;
166 if (assuan_pending_line(pwm->ctx))
167 return assuan_read_line(pwm->ctx, line, len);
169 return GPG_ERR_NO_DATA;
172 void pwmd_close(pwm_t *pwm)
174 if (!pwm)
175 return;
177 if (pwm->ctx)
178 assuan_disconnect(pwm->ctx);
180 if (pwm->password)
181 xfree(pwm->password);
183 if (pwm->title)
184 xfree(pwm->title);
186 if (pwm->desc)
187 xfree(pwm->desc);
189 if (pwm->prompt)
190 xfree(pwm->prompt);
192 if (pwm->pinentry_tty)
193 xfree(pwm->pinentry_tty);
195 if (pwm->pinentry_display)
196 xfree(pwm->pinentry_display);
198 if (pwm->pinentry_term)
199 xfree(pwm->pinentry_term);
201 if (pwm->filename)
202 xfree(pwm->filename);
204 xfree(pwm);
207 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
209 membuf_t *mem = (membuf_t *)data;
210 void *p;
212 if (!buffer)
213 return 0;
215 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
216 return 1;
218 mem->buf = p;
219 memcpy((char *)mem->buf + mem->len, buffer, len);
220 mem->len += len;
221 return 0;
224 void pwmd_free_result(void *data)
226 xfree(data);
229 static int _inquire_cb(void *data, const char *keyword)
231 pwm_t *pwm = (pwm_t *)data;
232 gpg_error_t rc = 0;
233 int flags = fcntl(pwm->fd, F_GETFL);
235 /* Shouldn't get this far without a callback. */
236 if (!pwm->inquire_func)
237 return GPG_ERR_INV_ARG;
240 * Since the socket file descriptor is probably set to non-blocking, set to
241 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
242 * asynchronous INQUIRE is supported by either libassuan or a later
243 * libpwmd.
245 fcntl(pwm->fd, F_SETFL, 0);
247 for (;;) {
248 char *result = NULL;
249 size_t len;
250 gpg_error_t arc;
252 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
253 rc = gpg_err_code(rc);
255 if (rc == GPG_ERR_EOF || !rc) {
256 if (len <= 0 || !result || !*result) {
257 rc = 0;
258 break;
261 arc = assuan_send_data(pwm->ctx, result, len);
263 if (rc == GPG_ERR_EOF) {
264 rc = arc;
265 break;
268 rc = arc;
270 else if (rc)
271 break;
274 fcntl(pwm->fd, F_SETFL, flags);
275 return rc;
278 gpg_error_t pwmd_finalize(pwm_t *pwm)
280 if (!pwm || pwm->fd < 0)
281 return GPG_ERR_INV_ARG;
283 pwm->state = ASYNC_INIT;
284 return 0;
287 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
289 char *buf;
290 gpg_error_t rc;
291 size_t len = strlen(cmd) + 2;
293 len += arg ? strlen(arg) : 0;
295 if (pwm->state != ASYNC_INIT)
296 return GPG_ERR_UNEXPECTED;
298 buf = (char *)xmalloc(len);
300 if (!buf) {
301 rc = gpg_error_from_errno(ENOMEM);
302 goto fail;
305 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
306 rc = assuan_write_line(pwm->ctx, buf);
307 xfree(buf);
309 if (!rc)
310 pwm->state = ASYNC_PROCESS;
312 fail:
313 return rc;
316 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
318 if (!pwm || !filename)
319 return GPG_ERR_INV_ARG;
321 return do_nb_command(pwm, "OPEN", filename);
324 gpg_error_t pwmd_save_async(pwm_t *pwm)
326 if (!pwm)
327 return GPG_ERR_INV_ARG;
329 return do_nb_command(pwm, "SAVE", NULL);
332 static gpg_error_t parse_assuan_line(pwm_t *pwm)
334 gpg_error_t rc;
335 char *line;
336 size_t len;
338 rc = assuan_read_line(pwm->ctx, &line, &len);
340 if (!rc) {
341 if (line[0] == 'O' && line[1] == 'K' &&
342 (line[2] == 0 || line[2] == ' ')) {
343 pwm->state = ASYNC_DONE;
345 else if (line[0] == '#') {
347 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
348 if (pwm->status_func) {
349 pwm->status_func(pwm->status_data,
350 line[1] == 0 ? line+1 : line+2);
353 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
354 (line[3] == 0 || line[3] == ' ')) {
355 line += 4;
356 rc = atoi(line);
357 pwm->state = ASYNC_DONE;
361 return rc;
364 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
366 fd_set rfds;
367 int n;
368 struct timeval tv = {0, 0};
370 *rc = 0;
372 if (!pwm || pwm->fd < 0) {
373 *rc = GPG_ERR_INV_ARG;
374 return ASYNC_DONE;
376 else if (pwm->state == ASYNC_DONE)
377 return pwm->state;
378 else if (pwm->state == ASYNC_INIT) {
379 *rc = GPG_ERR_UNEXPECTED;
380 return ASYNC_DONE;
383 FD_ZERO(&rfds);
384 FD_SET(pwm->fd, &rfds);
385 n = select(pwm->fd+1, &rfds, NULL, NULL, &tv);
387 if (n > 0) {
388 if (FD_ISSET(pwm->fd, &rfds))
389 *rc = parse_assuan_line(pwm);
392 if (!*rc && assuan_pending_line(pwm->ctx))
393 *rc = parse_assuan_line(pwm);
395 return pwm->state;
398 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
399 char **result, const char *cmd)
401 membuf_t data;
402 gpg_error_t rc;
404 data.len = 0;
405 data.buf = NULL;
407 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
408 pwm->status_func, pwm->status_data);
410 if (rc) {
411 if (data.buf) {
412 xfree(data.buf);
413 data.buf = NULL;
416 else {
417 if (data.buf) {
418 mem_realloc_cb(&data, "", 1);
419 *result = (char *)data.buf;
423 return gpg_err_code(rc);
426 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
427 void *data)
429 if (!pwm || !cmd || !fn)
430 return GPG_ERR_INV_ARG;
432 pwm->inquire_func = fn;
433 pwm->inquire_data = data;
434 return assuan_command(pwm, pwm->ctx, NULL, cmd);
437 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
439 #ifndef USE_PINENTRY
440 return GPG_ERR_NOT_IMPLEMENTED;
441 #else
442 if (!pwm || pwm->pid == -1)
443 return GPG_ERR_INV_ARG;
445 if (kill(pwm->pid, 0) == 0) {
446 if (kill(pwm->pid, SIGTERM) == -1) {
447 if (kill(pwm->pid, SIGKILL) == -1)
448 return gpg_error_from_errno(errno);
451 pwm->pin_error = GPG_ERR_TIMEOUT;
453 else
454 return gpg_error_from_errno(errno);
456 return 0;
457 #endif
460 #ifdef USE_PINENTRY
461 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
463 char *buf;
464 char tmp[ASSUAN_LINELENGTH];
465 gpg_error_t error;
467 if (!pwm->title)
468 pwm->title = xstrdup(N_("LibPWMD"));
470 if (!pwm->prompt)
471 pwm->prompt = xstrdup(N_("Password:"));
473 if (!pwm->desc && !which)
474 pwm->desc = xstrdup(N_("Enter a password."));
476 if (which == 1) {
477 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
478 buf = xstrdup(tmp);
480 else if (which == 2) {
481 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
482 buf = xstrdup(tmp);
484 else {
485 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
486 sprintf(buf, "SETERROR %s", pwm->desc);
489 error = pinentry_command(pwm, NULL, buf);
490 xfree(buf);
492 if (error)
493 return error;
495 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
496 sprintf(buf, "SETPROMPT %s", pwm->prompt);
497 error = pinentry_command(pwm, NULL, buf);
498 xfree(buf);
500 if (error)
501 return error;
503 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
504 sprintf(buf, "SETDESC %s", pwm->title);
505 error = pinentry_command(pwm, NULL, buf);
506 xfree(buf);
507 return error;
510 static void update_pinentry_settings(pwm_t *pwm)
512 FILE *fp;
513 char buf[LINE_MAX];
514 struct passwd *pw = getpwuid(getuid());
515 char *p;
517 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
519 if ((fp = fopen(buf, "r")) == NULL)
520 return;
522 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
523 char name[32], val[256];
525 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
526 continue;
528 if (strcasecmp(name, "TTYNAME") == 0) {
529 xfree(pwm->pinentry_tty);
530 pwm->pinentry_tty = xstrdup(val);
532 else if (strcasecmp(name, "TTYTYPE") == 0) {
533 xfree(pwm->pinentry_term);
534 pwm->pinentry_term = xstrdup(val);
536 else if (strcasecmp(name, "DISPLAY") == 0) {
537 xfree(pwm->pinentry_display);
538 pwm->pinentry_display = xstrdup(val);
540 else if (strcasecmp(name, "PATH") == 0) {
541 xfree(pwm->pinentry_path);
542 pwm->pinentry_path = xstrdup(val);
546 fclose(fp);
549 static gpg_error_t launch_pinentry(pwm_t *pwm)
551 int rc;
552 assuan_context_t ctx;
553 int child_list[] = {-1};
554 char *display = getenv("DISPLAY");
555 const char *argv[6];
556 int have_display = 0;
557 char *tty = NULL;
559 update_pinentry_settings(pwm);
561 if (pwm->pinentry_display || display)
562 have_display = 1;
563 else {
564 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
566 if (!tty)
567 return gpg_error_from_errno(errno);
570 if (!have_display && !tty)
571 return GPG_ERR_ENOTTY;
573 argv[0] = "pinentry";
574 argv[1] = have_display ? "--display" : "--ttyname";
575 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
576 argv[3] = NULL;
578 if (!have_display) {
579 argv[3] = "--ttytype";
580 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
581 argv[5] = NULL;
584 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
586 if (rc)
587 return rc;
589 pwm->pid = assuan_get_pid(ctx);
590 pwm->pctx = ctx;
591 return set_pinentry_strings(pwm, 0);
594 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
596 gpg_error_t n;
598 if (!pwm->pctx) {
599 n = launch_pinentry(pwm);
601 if (n)
602 return n;
605 return assuan_command(pwm, pwm->pctx, result, cmd);
608 static void pinentry_disconnect(pwm_t *pwm)
610 if (pwm->pctx)
611 assuan_disconnect(pwm->pctx);
613 pwm->pctx = NULL;
614 pwm->pid = -1;
618 * Only called from a child process.
620 static void catchsig(int sig)
622 switch (sig) {
623 case SIGALRM:
624 if (gelapsed++ >= gtimeout) {
625 global_error = pwmd_terminate_pinentry(gpwm);
627 if (!global_error)
628 global_error = GPG_ERR_TIMEOUT;
630 break;
633 alarm(1);
634 break;
635 default:
636 break;
640 static char *percent_escape(const char *atext)
642 const unsigned char *s;
643 int len = strlen(atext) * 3 + 1;
644 char *buf = (char *)xmalloc(len), *p = buf;
646 if (!buf)
647 return NULL;
649 for (s=(const unsigned char *)atext; *s; s++) {
650 if (*s < ' ') {
651 sprintf (p, "%%%02X", *s);
652 p += 3;
654 else
655 *p++ = *s;
658 *p = 0;
659 return buf;
661 #endif
663 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
665 if (!cmd)
666 return GPG_ERR_INV_ARG;
668 return assuan_command(pwm, pwm->ctx, result, cmd);
672 * Avoid sending the BYE command here. libassuan will close the file
673 * descriptor and release the assuan context. Use pwmd_close() instead.
675 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
677 va_list ap;
678 char *buf;
679 size_t len;
680 gpg_error_t error;
682 if (!pwm || !cmd)
683 return GPG_ERR_INV_ARG;
685 *result = NULL;
686 va_start(ap, cmd);
688 * C99
690 len = vsnprintf(NULL, 0, cmd, ap);
691 buf = (char *)xmalloc(len + 1);
692 len = vsnprintf(buf, len + 1, cmd, ap);
693 va_end(ap);
694 error = send_command(pwm, result, buf);
695 xfree(buf);
696 return error;
699 #ifdef USE_PINENTRY
700 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
702 if (gtimeout) {
703 signal(SIGALRM, catchsig);
704 alarm(1);
707 *result = NULL;
708 return pinentry_command(pwm, result, "GETPIN");
711 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
713 int pin_try = *try_n;
714 gpg_error_t error;
716 getpin_again:
717 *try_n = pin_try;
719 if (pin_try == -1) {
720 error = set_pinentry_strings(pwm, which);
722 if (error) {
723 pinentry_disconnect(pwm);
724 return error;
727 else {
728 if (pwm->pinentry_tries-1 != pin_try) {
729 error = set_pinentry_strings(pwm, 1);
731 if (error) {
732 pinentry_disconnect(pwm);
733 return error;
738 error = do_getpin(pwm, result);
741 * Since there was input cancel any timeout.
743 alarm(0);
745 if (error) {
746 if (error == GPG_ERR_ASS_CANCELED || error == ASSUAN_Canceled)
747 return GPG_ERR_ASS_CANCELED;
749 if (pin_try != -1 && pin_try--)
750 goto getpin_again;
752 if (pwm->pctx)
753 pinentry_disconnect(pwm);
755 *try_n = pin_try;
756 return error;
759 return 0;
761 #endif
763 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
765 char *result;
766 gpg_error_t error;
768 #ifndef USE_PINENTRY
769 return GPG_ERR_NOT_IMPLEMENTED;
770 #endif
772 if (!pwm || !pw || !pw->filename[0])
773 return GPG_ERR_INV_ARG;
775 close(pw->fd);
777 if (pw->error) {
778 error = pw->error;
779 goto fail;
782 error = pwmd_command(pwm, &result, "ISCACHED %s", pw->filename);
784 if (error)
785 goto fail;
787 if (pwm->filename)
788 xfree(pwm->filename);
790 pwm->filename = xstrdup(pw->filename);
791 memset(pw, 0, sizeof(pwmd_nb_status_t));
792 return 0;
794 fail:
795 memset(pw, 0, sizeof(pwmd_nb_status_t));
796 return error;
799 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
801 char buf[ASSUAN_LINELENGTH];
802 gpg_error_t error;
803 char *result = NULL;
805 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
806 error = send_command(pwm, &result, buf);
807 memset(buf, 0, sizeof(buf));
809 if (error && result)
810 xfree(result);
812 return error;
815 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
816 int nb, int timeout)
818 char *result = NULL;
819 char *password = NULL;
820 char path[PATH_MAX];
821 #ifdef USE_PINENTRY
822 int pin_try;
823 #endif
825 if (!pwm || !filename || !*filename) {
826 *error = GPG_ERR_INV_ARG;
827 return nb ? -1 : 1;
830 #ifdef USE_PINENTRY
831 pin_try = pwm->pinentry_tries - 1;
832 #endif
835 * Avoid calling pinentry if the password is cached on the server or if
836 * this is a new file.
838 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
840 if (*error)
841 return nb ? -1 : 1;
843 snprintf(path, sizeof(path), "%s/%s", result, filename);
844 pwmd_free_result(result);
846 if (access(path, R_OK) == -1) {
847 if (errno == ENOENT)
848 goto gotpassword;
851 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
853 if (*error == EPWMD_CACHE_NOT_FOUND) {
854 if (pwm->passfunc) {
855 password = pwm->passfunc(pwm, pwm->passdata);
856 goto gotpassword;
859 if (*error == EPWMD_CACHE_NOT_FOUND) {
860 #ifdef USE_PINENTRY
862 * Get the password from pinentry.
864 if (pwm->use_pinentry) {
866 * Nonblocking is wanted. fork() then return a file descriptor
867 * that the client can use to read() from.
869 if (nb) {
870 int p[2];
871 pid_t pid;
872 pwmd_nb_status_t pw;
874 if (pipe(p) == -1) {
875 *error = gpg_error_from_syserror();
876 return -1;
879 pid = fork();
881 switch (pid) {
882 case 0:
883 close(p[0]);
884 strncpy(pw.filename, filename, sizeof(pw.filename));
885 pw.filename[sizeof(pw.filename)-1] = 0;
886 pw.fd = p[0];
888 if (timeout > 0) {
889 gpwm = pwm;
890 gtimeout = timeout;
891 gelapsed = 0;
894 getpin_nb_again:
895 *error = getpin(pwm, &password, &pin_try, 0);
897 if (*error) {
898 getpin_nb_fail:
899 if (pwm->pctx)
900 pinentry_disconnect(pwm);
902 if (gtimeout && gelapsed >= gtimeout)
903 *error = GPG_ERR_TIMEOUT;
905 pw.error = *error;
906 write(p[1], &pw, sizeof(pw));
907 close(p[1]);
908 _exit(1);
912 * Don't count the time it takes to open the file
913 * which may have many iterations.
915 signal(SIGALRM, SIG_DFL);
916 *error = do_open_command(pwm, filename, password);
918 if (timeout)
919 signal(SIGALRM, catchsig);
921 if (pwm->pctx && *error == EPWMD_BADKEY) {
922 if (pin_try-- > 0)
923 goto getpin_nb_again;
925 goto getpin_nb_fail;
928 pinentry_disconnect(pwm);
929 pw.error = 0;
930 write(p[1], &pw, sizeof(pw));
931 close(p[1]);
932 _exit(0);
933 break;
934 case -1:
935 *error = gpg_error_from_syserror();
936 close(p[0]);
937 close(p[1]);
938 return -1;
939 default:
940 break;
943 close(p[1]);
944 return p[0];
947 getpin_again:
948 *error = getpin(pwm, &password, &pin_try, 1);
950 if (*error) {
951 if (pwm->pctx)
952 pinentry_disconnect(pwm);
954 if (pwm->pin_error) {
955 *error = pwm->pin_error;
956 pwm->pin_error = 0;
959 return 1;
962 else {
963 #endif
965 * Not using pinentry and the file was not found
966 * in the cache.
968 #if 0
969 if (pwm->password == NULL) {
970 *error = EPWMD_KEY;
971 return 1;
973 #endif
975 password = pwm->password;
976 #ifdef USE_PINENTRY
978 #endif
981 else if (*error)
982 return nb ? -1 : 1;
984 gotpassword:
985 *error = do_open_command(pwm, filename, password);
988 * Keep the user defined password set with pwmd_setopt(). The password may
989 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
991 if (password && password != pwm->password)
992 xfree(password);
994 #ifdef USE_PINENTRY
995 if (pwm->pctx && *error == EPWMD_BADKEY) {
996 if (pin_try-- > 0)
997 goto getpin_again;
999 pinentry_disconnect(pwm);
1000 return nb ? -1 : 1;
1002 else if (pwm->pctx)
1003 pinentry_disconnect(pwm);
1004 #endif
1006 if (!*error) {
1007 if (pwm->filename)
1008 xfree(pwm->filename);
1010 pwm->filename = xstrdup(filename);
1014 * The file is cached or the file is a new file.
1016 if (nb)
1017 return *error ? -1 : -2;
1019 return *error ? 1 : 0;
1022 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1024 gpg_error_t error;
1026 do_pwmd_open(pwm, &error, filename, 0, 0);
1027 return error;
1030 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1031 int timeout)
1033 #ifndef USE_PINENTRY
1034 *error = GPG_ERR_NOT_IMPLEMENTED;
1035 return -1;
1036 #else
1037 return do_pwmd_open(pwm, error, filename, 1, timeout);
1038 #endif
1041 #ifdef USE_PINENTRY
1042 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1044 int confirm = 0;
1045 gpg_error_t error;
1046 char *result = NULL;
1047 int pin_try = -1;
1049 again:
1050 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1052 if (error) {
1053 if (pwm->pctx)
1054 pinentry_disconnect(pwm);
1056 if (*password)
1057 xfree(*password);
1059 return error;
1062 if (!confirm++) {
1063 *password = result;
1064 goto again;
1067 if (strcmp(*password, result)) {
1068 xfree(*password);
1069 xfree(result);
1070 pinentry_disconnect(pwm);
1071 error = EPWMD_BADKEY;
1072 return error;
1075 xfree(result);
1076 pinentry_disconnect(pwm);
1077 return 0;
1079 #endif
1081 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1083 char buf[ASSUAN_LINELENGTH];
1084 gpg_error_t error;
1085 char *result = NULL;
1087 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1088 error = send_command(pwm, &result, buf);
1089 memset(&buf, 0, sizeof(buf));
1091 if (error && result)
1092 xfree(result);
1094 return error;
1097 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1099 gpg_error_t error;
1101 #ifndef USE_PINENTRY
1102 return GPG_ERR_NOT_IMPLEMENTED;
1103 #endif
1105 if (!pwm || !pw || !pw->filename[0])
1106 return GPG_ERR_INV_ARG;
1108 close(pw->fd);
1110 if (pw->error) {
1111 error = pw->error;
1112 memset(pw, 0, sizeof(pwmd_nb_status_t));
1113 return error;
1116 memset(pw, 0, sizeof(pwmd_nb_status_t));
1117 return 0;
1120 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1122 char *result = NULL;
1123 char *password = NULL;
1125 if (!pwm) {
1126 *error = GPG_ERR_INV_ARG;
1127 return nb ? -1 : 1;
1130 if (pwm->use_pinentry || pwm->passfunc) {
1131 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1133 if (*error == EPWMD_CACHE_NOT_FOUND) {
1134 #ifdef USE_PINENTRY
1135 if (pwm->use_pinentry) {
1136 if (nb) {
1137 int p[2];
1138 pid_t pid;
1139 pwmd_nb_status_t pw;
1141 if (pipe(p) == -1) {
1142 *error = gpg_error_from_syserror();
1143 return -1;
1146 pid = fork();
1148 switch (pid) {
1149 case 0:
1150 close(p[0]);
1151 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1152 pw.filename[sizeof(pw.filename)-1] = 0;
1153 pw.fd = p[0];
1155 do {
1156 password = NULL;
1157 *error = do_save_getpin(pwm, &password);
1158 } while (*error == EPWMD_BADKEY);
1160 if (*error) {
1161 if (pwm->pctx)
1162 pinentry_disconnect(pwm);
1164 pw.error = *error;
1165 write(p[1], &pw, sizeof(pw));
1166 close(p[1]);
1167 _exit(1);
1170 *error = do_save_command(pwm, password);
1171 pinentry_disconnect(pwm);
1172 pw.error = *error;
1173 write(p[1], &pw, sizeof(pw));
1174 close(p[1]);
1175 _exit(0);
1176 break;
1177 case -1:
1178 *error = gpg_error_from_syserror();
1179 close(p[0]);
1180 close(p[1]);
1181 return -1;
1182 default:
1183 break;
1186 close(p[1]);
1187 return p[0];
1190 *error = do_save_getpin(pwm, &password);
1192 if (*error)
1193 return 1;
1195 else {
1196 #endif
1197 if (pwm->passfunc)
1198 password = (*pwm->passfunc)(pwm, pwm->passdata);
1199 #ifdef USE_PINENTRY
1201 #endif
1203 else {
1204 if (*error)
1205 return nb ? -1 : 1;
1208 else
1209 password = pwm->password;
1211 *error = do_save_command(pwm, password);
1213 if (password && password != pwm->password)
1214 xfree(password);
1216 if (nb)
1217 return *error ? -1 : -2;
1219 return *error ? 1 : 0;
1222 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1224 #ifndef USE_PINENTRY
1225 *error = GPG_ERR_NOT_IMPLEMENTED;
1226 return -1;
1227 #else
1228 return do_pwmd_save(pwm, error, 1);
1229 #endif
1232 gpg_error_t pwmd_save(pwm_t *pwm)
1234 gpg_error_t error;
1236 do_pwmd_save(pwm, &error, 0);
1237 return error;
1240 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1242 va_list ap;
1243 #ifdef USE_PINENTRY
1244 int n = va_arg(ap, int);
1245 char *result;
1246 #endif
1247 char *arg1;
1248 gpg_error_t error = 0;
1250 if (!pwm)
1251 return GPG_ERR_INV_ARG;
1253 va_start(ap, opt);
1255 switch (opt) {
1256 case PWMD_OPTION_STATUS_FUNC:
1257 pwm->status_func = va_arg(ap, pwmd_status_fn);
1258 break;
1259 case PWMD_OPTION_STATUS_DATA:
1260 pwm->status_data = va_arg(ap, void *);
1261 break;
1262 case PWMD_OPTION_PASSWORD_FUNC:
1263 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1264 break;
1265 case PWMD_OPTION_PASSWORD_DATA:
1266 pwm->passdata = va_arg(ap, void *);
1267 break;
1268 case PWMD_OPTION_PASSWORD:
1269 arg1 = va_arg(ap, char *);
1271 if (pwm->password)
1272 xfree(pwm->password);
1274 pwm->password = xstrdup(arg1);
1275 break;
1276 #ifdef USE_PINENTRY
1277 case PWMD_OPTION_PINENTRY:
1278 n = va_arg(ap, int);
1280 if (n != 0 && n != 1) {
1281 va_end(ap);
1282 error = GPG_ERR_INV_VALUE;
1284 else {
1285 pwm->use_pinentry = n;
1286 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
1287 !pwm->use_pinentry);
1289 break;
1290 case PWMD_OPTION_PINENTRY_TRIES:
1291 n = va_arg(ap, int);
1293 if (n <= 0) {
1294 va_end(ap);
1295 error = GPG_ERR_INV_VALUE;
1297 else
1298 pwm->pinentry_tries = n;
1299 break;
1300 case PWMD_OPTION_PINENTRY_PATH:
1301 if (pwm->pinentry_path)
1302 xfree(pwm->pinentry_path);
1304 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1305 break;
1306 case PWMD_OPTION_PINENTRY_TTY:
1307 if (pwm->pinentry_tty)
1308 xfree(pwm->pinentry_tty);
1310 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1311 break;
1312 case PWMD_OPTION_PINENTRY_DISPLAY:
1313 if (pwm->pinentry_display)
1314 xfree(pwm->pinentry_display);
1316 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1317 break;
1318 case PWMD_OPTION_PINENTRY_TERM:
1319 if (pwm->pinentry_term)
1320 xfree(pwm->pinentry_term);
1322 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1323 break;
1324 case PWMD_OPTION_PINENTRY_TITLE:
1325 if (pwm->title)
1326 xfree(pwm->title);
1327 pwm->title = percent_escape(va_arg(ap, char *));
1328 break;
1329 case PWMD_OPTION_PINENTRY_PROMPT:
1330 if (pwm->prompt)
1331 xfree(pwm->prompt);
1332 pwm->prompt = percent_escape(va_arg(ap, char *));
1333 break;
1334 case PWMD_OPTION_PINENTRY_DESC:
1335 if (pwm->desc)
1336 xfree(pwm->desc);
1337 pwm->desc = percent_escape(va_arg(ap, char *));
1338 break;
1339 #else
1340 case PWMD_OPTION_PINENTRY:
1341 case PWMD_OPTION_PINENTRY_TRIES:
1342 case PWMD_OPTION_PINENTRY_PATH:
1343 case PWMD_OPTION_PINENTRY_TTY:
1344 case PWMD_OPTION_PINENTRY_DISPLAY:
1345 case PWMD_OPTION_PINENTRY_TERM:
1346 case PWMD_OPTION_PINENTRY_TITLE:
1347 case PWMD_OPTION_PINENTRY_PROMPT:
1348 case PWMD_OPTION_PINENTRY_DESC:
1349 error = GPG_ERR_NOT_IMPLEMENTED;
1350 break;
1351 #endif
1352 default:
1353 error = GPG_ERR_NOT_IMPLEMENTED;
1354 break;
1357 va_end(ap);
1358 return error;
1361 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, assuan_context_t *ctx, int *fd)
1363 if (!pwm)
1364 return GPG_ERR_INV_ARG;
1366 *ctx = pwm->ctx;
1367 *fd = pwm->fd;
1368 return 0;