Forgot to include pth.h. Fixed.
[libpwmd.git] / libpwmd.c
blob501ea4dffe89dbee76d9296e5d37fd90ada2bcb1
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 #ifdef ENABLE_NLS
106 bindtextdomain("libpwmd", LOCALEDIR);
107 #endif
108 gpg_err_init();
109 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
110 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
111 return 0;
114 pwm_t *pwmd_connect(const char *path, gpg_error_t *error)
116 pwm_t *pwm = NULL;
117 char *socketpath = NULL;
118 time_t now;
119 struct passwd *pw;
120 assuan_context_t ctx;
121 int rc, n;
122 int active[2];
124 if (!path) {
125 pw = getpwuid(getuid());
126 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
127 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
129 else
130 socketpath = xstrdup(path);
132 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
133 xfree(socketpath);
135 if (rc) {
136 *error = rc;
137 return NULL;
140 n = assuan_get_active_fds(ctx, 0, active, sizeof(active));
142 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
143 *error = gpg_error_from_errno(errno);
144 assuan_disconnect(ctx);
145 return NULL;
148 pwm->fd = n <= 0 ? -1 : dup(active[0]);
150 if (pwm->fd != -1)
151 fcntl(pwm->fd, F_SETFL, O_NONBLOCK);
153 pwm->ctx = ctx;
154 #ifdef USE_PINENTRY
155 pwm->pid = -1;
156 pwm->pinentry_tries = 3;
157 #endif
158 time(&now);
159 srandom(now);
160 *error = 0;
161 return pwm;
164 gpg_error_t pwmd_pending_line(pwm_t *pwm, char **line, size_t *len)
166 if (!pwm)
167 return GPG_ERR_INV_ARG;
169 if (assuan_pending_line(pwm->ctx))
170 return assuan_read_line(pwm->ctx, line, len);
172 return GPG_ERR_NO_DATA;
175 void pwmd_close(pwm_t *pwm)
177 if (!pwm)
178 return;
180 if (pwm->ctx)
181 assuan_disconnect(pwm->ctx);
183 if (pwm->password)
184 xfree(pwm->password);
186 if (pwm->title)
187 xfree(pwm->title);
189 if (pwm->desc)
190 xfree(pwm->desc);
192 if (pwm->prompt)
193 xfree(pwm->prompt);
195 if (pwm->pinentry_tty)
196 xfree(pwm->pinentry_tty);
198 if (pwm->pinentry_display)
199 xfree(pwm->pinentry_display);
201 if (pwm->pinentry_term)
202 xfree(pwm->pinentry_term);
204 if (pwm->filename)
205 xfree(pwm->filename);
207 xfree(pwm);
210 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
212 membuf_t *mem = (membuf_t *)data;
213 void *p;
215 if (!buffer)
216 return 0;
218 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
219 return 1;
221 mem->buf = p;
222 memcpy((char *)mem->buf + mem->len, buffer, len);
223 mem->len += len;
224 return 0;
227 void pwmd_free_result(void *data)
229 xfree(data);
232 static int _inquire_cb(void *data, const char *keyword)
234 pwm_t *pwm = (pwm_t *)data;
235 gpg_error_t rc = 0;
236 int flags = fcntl(pwm->fd, F_GETFL);
238 /* Shouldn't get this far without a callback. */
239 if (!pwm->inquire_func)
240 return GPG_ERR_INV_ARG;
243 * Since the socket file descriptor is probably set to non-blocking, set to
244 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
245 * asynchronous INQUIRE is supported by either libassuan or a later
246 * libpwmd.
248 fcntl(pwm->fd, F_SETFL, 0);
250 for (;;) {
251 char *result = NULL;
252 size_t len;
253 gpg_error_t arc;
255 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
256 rc = gpg_err_code(rc);
258 if (rc == GPG_ERR_EOF || !rc) {
259 if (len <= 0 || !result || !*result) {
260 rc = 0;
261 break;
264 arc = assuan_send_data(pwm->ctx, result, len);
266 if (rc == GPG_ERR_EOF) {
267 rc = arc;
268 break;
271 rc = arc;
273 else if (rc)
274 break;
277 fcntl(pwm->fd, F_SETFL, flags);
278 return rc;
281 gpg_error_t pwmd_finalize(pwm_t *pwm)
283 if (!pwm || pwm->fd < 0)
284 return GPG_ERR_INV_ARG;
286 pwm->state = ASYNC_INIT;
287 pwm->ntries = 0;
288 pwm->is_open_cmd = 0;
289 return 0;
292 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
294 char *buf;
295 gpg_error_t rc;
296 size_t len = strlen(cmd) + 2;
298 len += arg ? strlen(arg) : 0;
300 if (pwm->state != ASYNC_INIT)
301 return GPG_ERR_UNEXPECTED;
303 buf = (char *)xmalloc(len);
305 if (!buf) {
306 rc = gpg_error_from_errno(ENOMEM);
307 goto fail;
310 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
311 rc = assuan_write_line(pwm->ctx, buf);
312 xfree(buf);
314 if (!rc)
315 pwm->state = ASYNC_PROCESS;
317 fail:
318 return rc;
321 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
323 if (!pwm || !filename)
324 return GPG_ERR_INV_ARG;
326 /* For pinentry retries. */
327 if (!pwm->is_open_cmd) {
328 if (pwm->filename)
329 xfree(pwm->filename);
331 pwm->filename = xstrdup(filename);
334 pwm->is_open_cmd = 1;
335 return do_nb_command(pwm, "OPEN", filename);
338 gpg_error_t pwmd_save_async(pwm_t *pwm)
340 if (!pwm)
341 return GPG_ERR_INV_ARG;
343 return do_nb_command(pwm, "SAVE", NULL);
346 static gpg_error_t parse_assuan_line(pwm_t *pwm)
348 gpg_error_t rc;
349 char *line;
350 size_t len;
352 rc = assuan_read_line(pwm->ctx, &line, &len);
354 if (!rc) {
355 if (line[0] == 'O' && line[1] == 'K' &&
356 (line[2] == 0 || line[2] == ' ')) {
357 pwm->state = ASYNC_DONE;
359 else if (line[0] == '#') {
361 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
362 if (pwm->status_func) {
363 pwm->status_func(pwm->status_data,
364 line[1] == 0 ? line+1 : line+2);
367 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
368 (line[3] == 0 || line[3] == ' ')) {
369 line += 4;
370 rc = atoi(line);
371 pwm->state = ASYNC_DONE;
375 return rc;
378 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
380 fd_set rfds;
381 int n;
382 struct timeval tv = {0, 0};
384 *rc = 0;
386 if (!pwm || pwm->fd < 0) {
387 *rc = GPG_ERR_INV_ARG;
388 return ASYNC_DONE;
390 else if (pwm->state == ASYNC_DONE)
391 return pwm->state;
392 else if (pwm->state == ASYNC_INIT) {
393 *rc = GPG_ERR_UNEXPECTED;
394 return ASYNC_DONE;
397 FD_ZERO(&rfds);
398 FD_SET(pwm->fd, &rfds);
399 #ifdef WITH_LIBPTH
400 n = pth_select(pwm->fd+1, &rfds, NULL, NULL, &tv);
401 #else
402 n = select(pwm->fd+1, &rfds, NULL, NULL, &tv);
403 #endif
405 if (n > 0) {
406 if (FD_ISSET(pwm->fd, &rfds))
407 *rc = parse_assuan_line(pwm);
410 while (!*rc && assuan_pending_line(pwm->ctx))
411 *rc = parse_assuan_line(pwm);
413 if (pwm->is_open_cmd && gpg_err_code(*rc) == EPWMD_BADKEY &&
414 ++pwm->ntries < pwm->pinentry_tries) {
415 pwm->state = ASYNC_INIT;
416 *rc = pwmd_open_async(pwm, pwm->filename);
419 return pwm->state;
422 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
423 char **result, const char *cmd)
425 membuf_t data;
426 gpg_error_t rc;
428 data.len = 0;
429 data.buf = NULL;
431 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
432 pwm->status_func, pwm->status_data);
434 if (rc) {
435 if (data.buf) {
436 xfree(data.buf);
437 data.buf = NULL;
440 else {
441 if (data.buf) {
442 mem_realloc_cb(&data, "", 1);
443 *result = (char *)data.buf;
447 return gpg_err_code(rc);
450 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
451 void *data)
453 if (!pwm || !cmd || !fn)
454 return GPG_ERR_INV_ARG;
456 pwm->inquire_func = fn;
457 pwm->inquire_data = data;
458 return assuan_command(pwm, pwm->ctx, NULL, cmd);
461 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
463 #ifndef USE_PINENTRY
464 return GPG_ERR_NOT_IMPLEMENTED;
465 #else
466 if (!pwm || pwm->pid == -1)
467 return GPG_ERR_INV_ARG;
469 if (kill(pwm->pid, 0) == 0) {
470 if (kill(pwm->pid, SIGTERM) == -1) {
471 if (kill(pwm->pid, SIGKILL) == -1)
472 return gpg_error_from_errno(errno);
475 pwm->pin_error = GPG_ERR_TIMEOUT;
477 else
478 return gpg_error_from_errno(errno);
480 return 0;
481 #endif
484 #ifdef USE_PINENTRY
485 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
487 char *buf;
488 char tmp[ASSUAN_LINELENGTH];
489 gpg_error_t error;
491 if (!pwm->title)
492 pwm->title = xstrdup(N_("LibPWMD"));
494 if (!pwm->prompt)
495 pwm->prompt = xstrdup(N_("Password:"));
497 if (!pwm->desc && !which)
498 pwm->desc = xstrdup(N_("Enter a password."));
500 if (which == 1) {
501 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
502 buf = xstrdup(tmp);
504 else if (which == 2) {
505 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
506 buf = xstrdup(tmp);
508 else {
509 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
510 sprintf(buf, "SETERROR %s", pwm->desc);
513 error = pinentry_command(pwm, NULL, buf);
514 xfree(buf);
516 if (error)
517 return error;
519 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
520 sprintf(buf, "SETPROMPT %s", pwm->prompt);
521 error = pinentry_command(pwm, NULL, buf);
522 xfree(buf);
524 if (error)
525 return error;
527 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
528 sprintf(buf, "SETDESC %s", pwm->title);
529 error = pinentry_command(pwm, NULL, buf);
530 xfree(buf);
531 return error;
534 static void update_pinentry_settings(pwm_t *pwm)
536 FILE *fp;
537 char buf[LINE_MAX];
538 struct passwd *pw = getpwuid(getuid());
539 char *p;
541 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
543 if ((fp = fopen(buf, "r")) == NULL)
544 return;
546 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
547 char name[32], val[256];
549 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
550 continue;
552 if (strcasecmp(name, "TTYNAME") == 0) {
553 xfree(pwm->pinentry_tty);
554 pwm->pinentry_tty = xstrdup(val);
556 else if (strcasecmp(name, "TTYTYPE") == 0) {
557 xfree(pwm->pinentry_term);
558 pwm->pinentry_term = xstrdup(val);
560 else if (strcasecmp(name, "DISPLAY") == 0) {
561 xfree(pwm->pinentry_display);
562 pwm->pinentry_display = xstrdup(val);
564 else if (strcasecmp(name, "PATH") == 0) {
565 xfree(pwm->pinentry_path);
566 pwm->pinentry_path = xstrdup(val);
570 fclose(fp);
573 static gpg_error_t launch_pinentry(pwm_t *pwm)
575 int rc;
576 assuan_context_t ctx;
577 int child_list[] = {-1};
578 char *display = getenv("DISPLAY");
579 const char *argv[6];
580 int have_display = 0;
581 char *tty = NULL;
583 update_pinentry_settings(pwm);
585 if (pwm->pinentry_display || display)
586 have_display = 1;
587 else {
588 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
590 if (!tty)
591 return gpg_error_from_errno(errno);
594 if (!have_display && !tty)
595 return GPG_ERR_ENOTTY;
597 argv[0] = "pinentry";
598 argv[1] = have_display ? "--display" : "--ttyname";
599 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
600 argv[3] = NULL;
602 if (!have_display) {
603 argv[3] = "--ttytype";
604 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
605 argv[5] = NULL;
608 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
610 if (rc)
611 return rc;
613 pwm->pid = assuan_get_pid(ctx);
614 pwm->pctx = ctx;
615 return set_pinentry_strings(pwm, 0);
618 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
620 gpg_error_t n;
622 if (!pwm->pctx) {
623 n = launch_pinentry(pwm);
625 if (n)
626 return n;
629 return assuan_command(pwm, pwm->pctx, result, cmd);
632 static void pinentry_disconnect(pwm_t *pwm)
634 if (pwm->pctx)
635 assuan_disconnect(pwm->pctx);
637 pwm->pctx = NULL;
638 pwm->pid = -1;
642 * Only called from a child process.
644 static void catchsig(int sig)
646 switch (sig) {
647 case SIGALRM:
648 if (gelapsed++ >= gtimeout) {
649 global_error = pwmd_terminate_pinentry(gpwm);
651 if (!global_error)
652 global_error = GPG_ERR_TIMEOUT;
654 break;
657 alarm(1);
658 break;
659 default:
660 break;
665 * Borrowed from libassuan.
667 static char *percent_escape(const char *atext)
669 const unsigned char *s;
670 int len = strlen(atext) * 3 + 1;
671 char *buf = (char *)xmalloc(len), *p = buf;
673 if (!buf)
674 return NULL;
676 for (s=(const unsigned char *)atext; *s; s++) {
677 if (*s < ' ') {
678 sprintf (p, "%%%02X", *s);
679 p += 3;
681 else
682 *p++ = *s;
685 *p = 0;
686 return buf;
688 #endif
690 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
692 if (!cmd)
693 return GPG_ERR_INV_ARG;
695 return assuan_command(pwm, pwm->ctx, result, cmd);
699 * Avoid sending the BYE command here. libassuan will close the file
700 * descriptor and release the assuan context. Use pwmd_close() instead.
702 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
704 va_list ap;
705 char *buf;
706 size_t len;
707 gpg_error_t error;
709 if (!pwm || !cmd)
710 return GPG_ERR_INV_ARG;
712 *result = NULL;
713 va_start(ap, cmd);
715 * C99 allows the dst pointer to be null which will calculate the length
716 * of the result and return it.
718 len = vsnprintf(NULL, 0, cmd, ap);
719 buf = (char *)xmalloc(len + 1);
720 len = vsnprintf(buf, len + 1, cmd, ap);
721 va_end(ap);
722 error = send_command(pwm, result, buf);
723 xfree(buf);
724 return error;
727 #ifdef USE_PINENTRY
728 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
730 if (gtimeout) {
731 signal(SIGALRM, catchsig);
732 alarm(1);
735 *result = NULL;
736 return pinentry_command(pwm, result, "GETPIN");
739 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
741 int pin_try = *try_n;
742 gpg_error_t error;
744 getpin_again:
745 *try_n = pin_try;
747 if (pin_try == -1) {
748 error = set_pinentry_strings(pwm, which);
750 if (error) {
751 pinentry_disconnect(pwm);
752 return error;
755 else {
756 if (pwm->pinentry_tries-1 != pin_try) {
757 error = set_pinentry_strings(pwm, 1);
759 if (error) {
760 pinentry_disconnect(pwm);
761 return error;
766 error = do_getpin(pwm, result);
769 * Since there was input cancel any timeout setting.
771 alarm(0);
773 if (error) {
774 if (error == GPG_ERR_CANCELED)
775 return GPG_ERR_CANCELED;
777 if (pin_try != -1 && pin_try--)
778 goto getpin_again;
780 if (pwm->pctx)
781 pinentry_disconnect(pwm);
783 *try_n = pin_try;
784 return error;
787 return 0;
789 #endif
791 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
793 gpg_error_t error;
795 #ifndef USE_PINENTRY
796 return GPG_ERR_NOT_IMPLEMENTED;
797 #endif
799 if (!pwm || !pw || !pw->filename[0])
800 return GPG_ERR_INV_ARG;
802 close(pw->fd);
804 if (pw->error) {
805 error = pw->error;
806 goto fail;
809 if (pwm->filename)
810 xfree(pwm->filename);
812 pwm->filename = xstrdup(pw->filename);
813 memset(pw, 0, sizeof(pwmd_nb_status_t));
814 return 0;
816 fail:
817 memset(pw, 0, sizeof(pwmd_nb_status_t));
818 return error;
821 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
823 char buf[ASSUAN_LINELENGTH];
824 gpg_error_t error;
825 char *result = NULL;
827 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
828 error = send_command(pwm, &result, buf);
829 memset(buf, 0, sizeof(buf));
831 if (error && result)
832 xfree(result);
834 return error;
837 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
838 int nb, int timeout)
840 char *result = NULL;
841 char *password = NULL;
842 char path[PATH_MAX];
843 #ifdef USE_PINENTRY
844 int pin_try;
845 #endif
847 if (!pwm || !filename || !*filename) {
848 *error = GPG_ERR_INV_ARG;
849 return nb ? -1 : 1;
852 #ifdef USE_PINENTRY
853 pin_try = pwm->pinentry_tries - 1;
854 #endif
857 * Avoid calling pinentry if the password is cached on the server or if
858 * this is a new file.
860 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
862 if (*error)
863 return nb ? -1 : 1;
865 snprintf(path, sizeof(path), "%s/%s", result, filename);
866 pwmd_free_result(result);
868 if (access(path, R_OK) == -1) {
869 if (errno == ENOENT)
870 goto gotpassword;
873 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
875 if (*error == EPWMD_CACHE_NOT_FOUND) {
876 if (pwm->passfunc) {
877 password = pwm->passfunc(pwm, pwm->passdata);
878 goto gotpassword;
881 #ifdef USE_PINENTRY
883 * Get the password from pinentry.
885 if (pwm->use_pinentry) {
887 * Nonblocking is wanted. fork() then return a file descriptor
888 * that the client can use to read() from.
890 if (nb) {
891 int p[2];
892 pid_t pid;
893 pwmd_nb_status_t pw;
895 if (pipe(p) == -1) {
896 *error = gpg_error_from_syserror();
897 return -1;
900 #ifdef WITH_LIBPTH
901 pid = pth_fork();
902 #else
903 pid = fork();
904 #endif
906 switch (pid) {
907 case 0:
908 close(p[0]);
909 strncpy(pw.filename, filename, sizeof(pw.filename));
910 pw.filename[sizeof(pw.filename)-1] = 0;
911 pw.fd = p[0];
913 if (timeout > 0) {
914 gpwm = pwm;
915 gtimeout = timeout;
916 gelapsed = 0;
919 getpin_nb_again:
920 *error = getpin(pwm, &password, &pin_try, 0);
922 if (*error) {
923 getpin_nb_fail:
924 if (pwm->pctx)
925 pinentry_disconnect(pwm);
927 if (gtimeout && gelapsed >= gtimeout)
928 *error = GPG_ERR_TIMEOUT;
930 pw.error = *error;
931 #ifdef WITH_LIBPTH
932 pth_write(p[1], &pw, sizeof(pw));
933 #else
934 write(p[1], &pw, sizeof(pw));
935 #endif
936 close(p[1]);
937 _exit(1);
941 * Don't count the time it takes to open the file
942 * which may have many iterations.
944 signal(SIGALRM, SIG_DFL);
945 *error = do_open_command(pwm, filename, password);
947 if (timeout)
948 signal(SIGALRM, catchsig);
950 if (pwm->pctx && *error == EPWMD_BADKEY) {
951 if (pin_try-- > 0)
952 goto getpin_nb_again;
954 goto getpin_nb_fail;
957 pinentry_disconnect(pwm);
958 pw.error = 0;
959 #ifdef WITH_LIBPTH
960 pth_write(p[1], &pw, sizeof(pw));
961 #else
962 write(p[1], &pw, sizeof(pw));
963 #endif
964 close(p[1]);
965 _exit(0);
966 break;
967 case -1:
968 *error = gpg_error_from_syserror();
969 close(p[0]);
970 close(p[1]);
971 return -1;
972 default:
973 break;
976 close(p[1]);
977 return p[0];
980 else {
981 #endif
983 * Not using pinentry and the file was not found
984 * in the cache.
986 password = pwm->password;
987 #ifdef USE_PINENTRY
989 #endif
991 else if (*error)
992 return nb ? -1 : 1;
994 gotpassword:
995 *error = do_open_command(pwm, filename, password);
998 * Keep the user defined password set with pwmd_setopt(). The password may
999 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1001 if (password && password != pwm->password)
1002 xfree(password);
1004 #ifdef USE_PINENTRY
1005 if (*error == EPWMD_BADKEY) {
1006 if (pin_try-- > 0 && !nb) {
1007 *error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
1008 N_("Invalid password, please try again."));
1010 if (*error)
1011 return 1;
1013 goto gotpassword;
1016 if (nb)
1017 pinentry_disconnect(pwm);
1019 return nb ? -1 : 1;
1021 #endif
1023 if (!*error) {
1024 if (pwm->filename)
1025 xfree(pwm->filename);
1027 pwm->filename = xstrdup(filename);
1031 * The file is cached or the file is a new file.
1033 if (nb)
1034 return *error ? -1 : -2;
1036 return *error ? 1 : 0;
1039 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1041 gpg_error_t error;
1043 do_pwmd_open(pwm, &error, filename, 0, 0);
1044 return error;
1047 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1048 int timeout)
1050 #ifndef USE_PINENTRY
1051 *error = GPG_ERR_NOT_IMPLEMENTED;
1052 return -1;
1053 #else
1054 return do_pwmd_open(pwm, error, filename, 1, timeout);
1055 #endif
1058 #ifdef USE_PINENTRY
1059 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1061 int confirm = 0;
1062 gpg_error_t error;
1063 char *result = NULL;
1064 int pin_try = -1;
1066 again:
1067 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1069 if (error) {
1070 if (pwm->pctx)
1071 pinentry_disconnect(pwm);
1073 if (*password)
1074 xfree(*password);
1076 return error;
1079 if (!confirm++) {
1080 *password = result;
1081 goto again;
1084 if (strcmp(*password, result)) {
1085 xfree(*password);
1086 xfree(result);
1087 pinentry_disconnect(pwm);
1088 error = EPWMD_BADKEY;
1089 return error;
1092 xfree(result);
1093 pinentry_disconnect(pwm);
1094 return 0;
1096 #endif
1098 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1100 char buf[ASSUAN_LINELENGTH];
1101 gpg_error_t error;
1102 char *result = NULL;
1104 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1105 error = send_command(pwm, &result, buf);
1106 memset(&buf, 0, sizeof(buf));
1108 if (error && result)
1109 xfree(result);
1111 return error;
1114 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1116 gpg_error_t rc;
1118 #ifndef USE_PINENTRY
1119 return GPG_ERR_NOT_IMPLEMENTED;
1120 #endif
1122 if (!pwm || !pw || !pw->filename[0])
1123 return GPG_ERR_INV_ARG;
1125 close(pw->fd);
1126 rc = pw->error;
1127 memset(pw, 0, sizeof(pwmd_nb_status_t));
1128 return rc;
1131 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1133 char *result = NULL;
1134 char *password = NULL;
1136 if (!pwm) {
1137 *error = GPG_ERR_INV_ARG;
1138 return nb ? -1 : 1;
1141 if (pwm->use_pinentry || pwm->passfunc) {
1142 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1144 if (*error == EPWMD_CACHE_NOT_FOUND) {
1145 #ifdef USE_PINENTRY
1146 if (pwm->use_pinentry) {
1147 if (nb) {
1148 int p[2];
1149 pid_t pid;
1150 pwmd_nb_status_t pw;
1152 if (pipe(p) == -1) {
1153 *error = gpg_error_from_syserror();
1154 return -1;
1157 #ifdef WITH_LIBPTH
1158 pid = pth_fork();
1159 #else
1160 pid = fork();
1161 #endif
1163 switch (pid) {
1164 case 0:
1165 close(p[0]);
1166 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1167 pw.filename[sizeof(pw.filename)-1] = 0;
1168 pw.fd = p[0];
1170 do {
1171 password = NULL;
1172 *error = do_save_getpin(pwm, &password);
1173 } while (*error == EPWMD_BADKEY);
1175 if (*error) {
1176 if (pwm->pctx)
1177 pinentry_disconnect(pwm);
1179 pw.error = *error;
1180 #ifdef WITH_LIBPTH
1181 pth_write(p[1], &pw, sizeof(pw));
1182 #else
1183 write(p[1], &pw, sizeof(pw));
1184 #endif
1185 close(p[1]);
1186 _exit(1);
1189 *error = do_save_command(pwm, password);
1190 pinentry_disconnect(pwm);
1191 pw.error = *error;
1192 #ifdef WITH_LIBPTH
1193 pth_write(p[1], &pw, sizeof(pw));
1194 #else
1195 write(p[1], &pw, sizeof(pw));
1196 #endif
1197 close(p[1]);
1198 _exit(0);
1199 break;
1200 case -1:
1201 *error = gpg_error_from_syserror();
1202 close(p[0]);
1203 close(p[1]);
1204 return -1;
1205 default:
1206 break;
1209 close(p[1]);
1210 return p[0];
1213 *error = do_save_getpin(pwm, &password);
1215 if (*error)
1216 return 1;
1218 else {
1219 #endif
1220 if (pwm->passfunc)
1221 password = (*pwm->passfunc)(pwm, pwm->passdata);
1222 #ifdef USE_PINENTRY
1224 #endif
1226 else {
1227 if (*error)
1228 return nb ? -1 : 1;
1231 else
1232 password = pwm->password;
1234 *error = do_save_command(pwm, password);
1236 if (password && password != pwm->password)
1237 xfree(password);
1239 if (nb)
1240 return *error ? -1 : -2;
1242 return *error ? 1 : 0;
1245 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1247 #ifndef USE_PINENTRY
1248 *error = GPG_ERR_NOT_IMPLEMENTED;
1249 return -1;
1250 #else
1251 return do_pwmd_save(pwm, error, 1);
1252 #endif
1255 gpg_error_t pwmd_save(pwm_t *pwm)
1257 gpg_error_t error;
1259 do_pwmd_save(pwm, &error, 0);
1260 return error;
1263 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1265 va_list ap;
1266 #ifdef USE_PINENTRY
1267 int n = va_arg(ap, int);
1268 char *result;
1269 #endif
1270 char *arg1;
1271 gpg_error_t error = 0;
1273 if (!pwm)
1274 return GPG_ERR_INV_ARG;
1276 va_start(ap, opt);
1278 switch (opt) {
1279 case PWMD_OPTION_STATUS_FUNC:
1280 pwm->status_func = va_arg(ap, pwmd_status_fn);
1281 break;
1282 case PWMD_OPTION_STATUS_DATA:
1283 pwm->status_data = va_arg(ap, void *);
1284 break;
1285 case PWMD_OPTION_PASSWORD_FUNC:
1286 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1287 break;
1288 case PWMD_OPTION_PASSWORD_DATA:
1289 pwm->passdata = va_arg(ap, void *);
1290 break;
1291 case PWMD_OPTION_PASSWORD:
1292 arg1 = va_arg(ap, char *);
1294 if (pwm->password)
1295 xfree(pwm->password);
1297 pwm->password = xstrdup(arg1);
1298 break;
1299 #ifdef USE_PINENTRY
1300 case PWMD_OPTION_PINENTRY:
1301 n = va_arg(ap, int);
1303 if (n != 0 && n != 1) {
1304 va_end(ap);
1305 error = GPG_ERR_INV_VALUE;
1307 else {
1308 pwm->use_pinentry = n;
1309 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
1310 !pwm->use_pinentry);
1312 break;
1313 case PWMD_OPTION_PINENTRY_TRIES:
1314 n = va_arg(ap, int);
1316 if (n <= 0) {
1317 va_end(ap);
1318 error = GPG_ERR_INV_VALUE;
1320 else
1321 pwm->pinentry_tries = n;
1322 break;
1323 case PWMD_OPTION_PINENTRY_PATH:
1324 if (pwm->pinentry_path)
1325 xfree(pwm->pinentry_path);
1327 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1328 break;
1329 case PWMD_OPTION_PINENTRY_TTY:
1330 if (pwm->pinentry_tty)
1331 xfree(pwm->pinentry_tty);
1333 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1334 break;
1335 case PWMD_OPTION_PINENTRY_DISPLAY:
1336 if (pwm->pinentry_display)
1337 xfree(pwm->pinentry_display);
1339 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1340 break;
1341 case PWMD_OPTION_PINENTRY_TERM:
1342 if (pwm->pinentry_term)
1343 xfree(pwm->pinentry_term);
1345 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1346 break;
1347 case PWMD_OPTION_PINENTRY_TITLE:
1348 if (pwm->title)
1349 xfree(pwm->title);
1350 pwm->title = percent_escape(va_arg(ap, char *));
1351 break;
1352 case PWMD_OPTION_PINENTRY_PROMPT:
1353 if (pwm->prompt)
1354 xfree(pwm->prompt);
1355 pwm->prompt = percent_escape(va_arg(ap, char *));
1356 break;
1357 case PWMD_OPTION_PINENTRY_DESC:
1358 if (pwm->desc)
1359 xfree(pwm->desc);
1360 pwm->desc = percent_escape(va_arg(ap, char *));
1361 break;
1362 #else
1363 case PWMD_OPTION_PINENTRY:
1364 case PWMD_OPTION_PINENTRY_TRIES:
1365 case PWMD_OPTION_PINENTRY_PATH:
1366 case PWMD_OPTION_PINENTRY_TTY:
1367 case PWMD_OPTION_PINENTRY_DISPLAY:
1368 case PWMD_OPTION_PINENTRY_TERM:
1369 case PWMD_OPTION_PINENTRY_TITLE:
1370 case PWMD_OPTION_PINENTRY_PROMPT:
1371 case PWMD_OPTION_PINENTRY_DESC:
1372 error = GPG_ERR_NOT_IMPLEMENTED;
1373 break;
1374 #endif
1375 default:
1376 error = GPG_ERR_NOT_IMPLEMENTED;
1377 break;
1380 va_end(ap);
1381 return error;
1384 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, assuan_context_t *ctx, int *fd)
1386 if (!pwm)
1387 return GPG_ERR_INV_ARG;
1389 *ctx = pwm->ctx;
1390 *fd = pwm->fd;
1391 return 0;