Don't make pwmd_nb_status_t deprecated. Fixes g++ compilation.
[libpwmd.git] / libpwmd.c
blobe393c421798d7f3593d69a69604ccc4d4961b8e8
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 void pwmd_close(pwm_t *pwm)
163 if (!pwm)
164 return;
166 if (pwm->ctx)
167 assuan_disconnect(pwm->ctx);
169 if (pwm->password)
170 xfree(pwm->password);
172 if (pwm->title)
173 xfree(pwm->title);
175 if (pwm->desc)
176 xfree(pwm->desc);
178 if (pwm->prompt)
179 xfree(pwm->prompt);
181 if (pwm->pinentry_tty)
182 xfree(pwm->pinentry_tty);
184 if (pwm->pinentry_display)
185 xfree(pwm->pinentry_display);
187 if (pwm->pinentry_term)
188 xfree(pwm->pinentry_term);
190 if (pwm->filename)
191 xfree(pwm->filename);
193 xfree(pwm);
196 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
198 membuf_t *mem = (membuf_t *)data;
199 void *p;
201 if (!buffer)
202 return 0;
204 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
205 return 1;
207 mem->buf = p;
208 memcpy((char *)mem->buf + mem->len, buffer, len);
209 mem->len += len;
210 return 0;
213 void pwmd_free_result(void *data)
215 xfree(data);
218 static int _inquire_cb(void *data, const char *keyword)
220 pwm_t *pwm = (pwm_t *)data;
221 gpg_error_t rc = 0;
223 /* Shouldn't get this far without a callback. */
224 if (!pwm->inquire_func)
225 return GPG_ERR_INV_ARG;
227 for (;;) {
228 char *result = NULL;
229 size_t len;
230 gpg_error_t arc;
232 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
233 rc = gpg_err_code(rc);
235 if (rc == GPG_ERR_EOF || !rc) {
236 if (len <= 0 || !result || !*result) {
237 rc = 0;
238 break;
241 arc = assuan_send_data(pwm->ctx, result, len);
243 if (rc == GPG_ERR_EOF) {
244 rc = arc;
245 break;
248 rc = arc;
250 else if (rc)
251 break;
254 return rc;
257 gpg_error_t pwmd_finalize(pwm_t *pwm)
259 if (!pwm || pwm->fd < 0)
260 return GPG_ERR_INV_ARG;
262 pwm->state = ASYNC_INIT;
263 return 0;
266 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
268 char *buf;
269 gpg_error_t rc;
270 size_t len = strlen(cmd) + 2;
272 len += arg ? strlen(arg) : 0;
274 if (pwm->state != ASYNC_INIT)
275 return GPG_ERR_UNEXPECTED;
277 buf = (char *)xmalloc(len);
279 if (!buf) {
280 rc = gpg_error_from_errno(ENOMEM);
281 goto fail;
284 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
285 rc = assuan_write_line(pwm->ctx, buf);
286 xfree(buf);
288 if (!rc)
289 pwm->state = ASYNC_PROCESS;
291 fail:
292 return rc;
295 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
297 if (!pwm || !filename)
298 return GPG_ERR_INV_ARG;
300 return do_nb_command(pwm, "OPEN", filename);
303 gpg_error_t pwmd_save_async(pwm_t *pwm)
305 if (!pwm)
306 return GPG_ERR_INV_ARG;
308 return do_nb_command(pwm, "SAVE", NULL);
311 static gpg_error_t parse_assuan_line(pwm_t *pwm)
313 gpg_error_t rc;
314 char *line;
315 size_t len;
317 again:
318 rc = assuan_read_line(pwm->ctx, &line, &len);
320 if (!rc) {
321 if (line[0] == 'O' && line[1] == 'K' &&
322 (line[2] == 0 || line[2] == ' ')) {
323 pwm->state = ASYNC_DONE;
325 else if (line[0] == '#') {
327 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
328 if (pwm->status_func) {
329 pwm->status_func(pwm->status_data,
330 line[1] == 0 ? line+1 : line+2);
333 goto again;
335 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
336 (line[3] == 0 || line[3] == ' ')) {
337 line += 4;
338 rc = atoi(line);
339 pwm->state = ASYNC_DONE;
343 return rc;
346 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
348 fd_set rfds;
349 int n;
350 struct timeval tv = {0, 0};
352 *rc = 0;
354 if (!pwm || pwm->fd < 0) {
355 *rc = GPG_ERR_INV_ARG;
356 return ASYNC_DONE;
358 else if (pwm->state == ASYNC_DONE)
359 return pwm->state;
360 else if (pwm->state == ASYNC_INIT) {
361 *rc = GPG_ERR_UNEXPECTED;
362 return ASYNC_DONE;
365 FD_ZERO(&rfds);
366 FD_SET(pwm->fd, &rfds);
367 n = select(pwm->fd+1, &rfds, NULL, NULL, &tv);
369 if (n > 0) {
370 if (FD_ISSET(pwm->fd, &rfds))
371 *rc = parse_assuan_line(pwm);
374 return pwm->state;
377 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
378 char **result, const char *cmd)
380 membuf_t data;
381 gpg_error_t rc;
383 data.len = 0;
384 data.buf = NULL;
386 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
387 pwm->status_func, pwm->status_data);
389 if (rc) {
390 if (data.buf) {
391 xfree(data.buf);
392 data.buf = NULL;
395 else {
396 if (data.buf) {
397 mem_realloc_cb(&data, "", 1);
398 *result = (char *)data.buf;
402 return gpg_err_code(rc);
405 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
406 void *data)
408 if (!pwm || !cmd || !fn)
409 return GPG_ERR_INV_ARG;
411 pwm->inquire_func = fn;
412 pwm->inquire_data = data;
413 return assuan_command(pwm, pwm->ctx, NULL, cmd);
416 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
418 #ifndef USE_PINENTRY
419 return GPG_ERR_NOT_IMPLEMENTED;
420 #else
421 if (!pwm || pwm->pid == -1)
422 return GPG_ERR_INV_ARG;
424 if (kill(pwm->pid, 0) == 0) {
425 if (kill(pwm->pid, SIGTERM) == -1) {
426 if (kill(pwm->pid, SIGKILL) == -1)
427 return gpg_error_from_errno(errno);
430 pwm->pin_error = GPG_ERR_TIMEOUT;
432 else
433 return gpg_error_from_errno(errno);
435 return 0;
436 #endif
439 #ifdef USE_PINENTRY
440 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
442 char *buf;
443 char tmp[ASSUAN_LINELENGTH];
444 gpg_error_t error;
446 if (!pwm->title)
447 pwm->title = xstrdup(N_("LibPWMD"));
449 if (!pwm->prompt)
450 pwm->prompt = xstrdup(N_("Password:"));
452 if (!pwm->desc && !which)
453 pwm->desc = xstrdup(N_("Enter a password."));
455 if (which == 1) {
456 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
457 buf = xstrdup(tmp);
459 else if (which == 2) {
460 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
461 buf = xstrdup(tmp);
463 else {
464 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
465 sprintf(buf, "SETERROR %s", pwm->desc);
468 error = pinentry_command(pwm, NULL, buf);
469 xfree(buf);
471 if (error)
472 return error;
474 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
475 sprintf(buf, "SETPROMPT %s", pwm->prompt);
476 error = pinentry_command(pwm, NULL, buf);
477 xfree(buf);
479 if (error)
480 return error;
482 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
483 sprintf(buf, "SETDESC %s", pwm->title);
484 error = pinentry_command(pwm, NULL, buf);
485 xfree(buf);
486 return error;
489 static void update_pinentry_settings(pwm_t *pwm)
491 FILE *fp;
492 char buf[LINE_MAX];
493 struct passwd *pw = getpwuid(getuid());
494 char *p;
496 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
498 if ((fp = fopen(buf, "r")) == NULL)
499 return;
501 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
502 char name[32], val[256];
504 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
505 continue;
507 if (strcasecmp(name, "TTYNAME") == 0) {
508 xfree(pwm->pinentry_tty);
509 pwm->pinentry_tty = xstrdup(val);
511 else if (strcasecmp(name, "TTYTYPE") == 0) {
512 xfree(pwm->pinentry_term);
513 pwm->pinentry_term = xstrdup(val);
515 else if (strcasecmp(name, "DISPLAY") == 0) {
516 xfree(pwm->pinentry_display);
517 pwm->pinentry_display = xstrdup(val);
519 else if (strcasecmp(name, "PATH") == 0) {
520 xfree(pwm->pinentry_path);
521 pwm->pinentry_path = xstrdup(val);
525 fclose(fp);
528 static gpg_error_t launch_pinentry(pwm_t *pwm)
530 int rc;
531 assuan_context_t ctx;
532 int child_list[] = {-1};
533 char *display = getenv("DISPLAY");
534 const char *argv[6];
535 int have_display = 0;
536 char *tty = NULL;
538 update_pinentry_settings(pwm);
540 if (pwm->pinentry_display || display)
541 have_display = 1;
542 else {
543 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
545 if (!tty)
546 return gpg_error_from_errno(errno);
549 if (!have_display && !tty)
550 return GPG_ERR_ENOTTY;
552 argv[0] = "pinentry";
553 argv[1] = have_display ? "--display" : "--ttyname";
554 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
555 argv[3] = NULL;
557 if (!have_display) {
558 argv[3] = "--ttytype";
559 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
560 argv[5] = NULL;
563 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
565 if (rc)
566 return rc;
568 pwm->pid = assuan_get_pid(ctx);
569 pwm->pctx = ctx;
570 return set_pinentry_strings(pwm, 0);
573 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
575 gpg_error_t n;
577 if (!pwm->pctx) {
578 n = launch_pinentry(pwm);
580 if (n)
581 return n;
584 return assuan_command(pwm, pwm->pctx, result, cmd);
587 static void pinentry_disconnect(pwm_t *pwm)
589 if (pwm->pctx)
590 assuan_disconnect(pwm->pctx);
592 pwm->pctx = NULL;
593 pwm->pid = -1;
597 * Only called from a child process.
599 static void catchsig(int sig)
601 switch (sig) {
602 case SIGALRM:
603 if (gelapsed++ >= gtimeout) {
604 global_error = pwmd_terminate_pinentry(gpwm);
606 if (!global_error)
607 global_error = GPG_ERR_TIMEOUT;
609 break;
612 alarm(1);
613 break;
614 default:
615 break;
619 static char *percent_escape(const char *atext)
621 const unsigned char *s;
622 int len = strlen(atext) * 3 + 1;
623 char *buf = (char *)xmalloc(len), *p = buf;
625 if (!buf)
626 return NULL;
628 for (s=(const unsigned char *)atext; *s; s++) {
629 if (*s < ' ') {
630 sprintf (p, "%%%02X", *s);
631 p += 3;
633 else
634 *p++ = *s;
637 *p = 0;
638 return buf;
640 #endif
642 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
644 if (!cmd)
645 return GPG_ERR_INV_ARG;
647 return assuan_command(pwm, pwm->ctx, result, cmd);
651 * Avoid sending the BYE command here. libassuan will close the file
652 * descriptor and release the assuan context. Use pwmd_close() instead.
654 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
656 va_list ap;
657 char *buf;
658 size_t len;
659 gpg_error_t error;
661 if (!pwm || !cmd)
662 return GPG_ERR_INV_ARG;
664 *result = NULL;
665 va_start(ap, cmd);
667 * C99
669 len = vsnprintf(NULL, 0, cmd, ap);
670 buf = (char *)xmalloc(len + 1);
671 len = vsnprintf(buf, len + 1, cmd, ap);
672 va_end(ap);
673 error = send_command(pwm, result, buf);
674 xfree(buf);
675 return error;
678 #ifdef USE_PINENTRY
679 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
681 if (gtimeout) {
682 signal(SIGALRM, catchsig);
683 alarm(1);
686 *result = NULL;
687 return pinentry_command(pwm, result, "GETPIN");
690 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
692 int pin_try = *try_n;
693 gpg_error_t error;
695 getpin_again:
696 *try_n = pin_try;
698 if (pin_try == -1) {
699 error = set_pinentry_strings(pwm, which);
701 if (error) {
702 pinentry_disconnect(pwm);
703 return error;
706 else {
707 if (pwm->pinentry_tries-1 != pin_try) {
708 error = set_pinentry_strings(pwm, 1);
710 if (error) {
711 pinentry_disconnect(pwm);
712 return error;
717 error = do_getpin(pwm, result);
720 * Since there was input cancel any timeout.
722 alarm(0);
724 if (error) {
725 if (error == GPG_ERR_ASS_CANCELED || error == ASSUAN_Canceled)
726 return GPG_ERR_ASS_CANCELED;
728 if (pin_try != -1 && pin_try--)
729 goto getpin_again;
731 if (pwm->pctx)
732 pinentry_disconnect(pwm);
734 *try_n = pin_try;
735 return error;
738 return 0;
740 #endif
742 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
744 char *result;
745 gpg_error_t error;
747 #ifndef USE_PINENTRY
748 return GPG_ERR_NOT_IMPLEMENTED;
749 #endif
751 if (!pwm || !pw || !pw->filename[0])
752 return GPG_ERR_INV_ARG;
754 close(pw->fd);
756 if (pw->error) {
757 error = pw->error;
758 goto fail;
761 error = pwmd_command(pwm, &result, "ISCACHED %s", pw->filename);
763 if (error)
764 goto fail;
766 if (pwm->filename)
767 xfree(pwm->filename);
769 pwm->filename = xstrdup(pw->filename);
770 memset(pw, 0, sizeof(pwmd_nb_status_t));
771 return 0;
773 fail:
774 memset(pw, 0, sizeof(pwmd_nb_status_t));
775 return error;
778 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
780 char buf[ASSUAN_LINELENGTH];
781 gpg_error_t error;
782 char *result = NULL;
784 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
785 error = send_command(pwm, &result, buf);
786 memset(buf, 0, sizeof(buf));
788 if (error && result)
789 xfree(result);
791 return error;
794 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
795 int nb, int timeout)
797 char *result = NULL;
798 char *password = NULL;
799 char path[PATH_MAX];
800 #ifdef USE_PINENTRY
801 int pin_try;
802 #endif
804 if (!pwm || !filename || !*filename) {
805 *error = GPG_ERR_INV_ARG;
806 return nb ? -1 : 1;
809 #ifdef USE_PINENTRY
810 pin_try = pwm->pinentry_tries - 1;
811 #endif
814 * Avoid calling pinentry if the password is cached on the server or if
815 * this is a new file.
817 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
819 if (*error)
820 return nb ? -1 : 1;
822 snprintf(path, sizeof(path), "%s/%s", result, filename);
823 pwmd_free_result(result);
825 if (access(path, R_OK) == -1) {
826 if (errno == ENOENT)
827 goto gotpassword;
829 *error = gpg_error_from_errno(errno);
830 return nb ? -1 : 1;
833 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
835 if (*error == EPWMD_CACHE_NOT_FOUND) {
836 if (pwm->passfunc) {
837 password = pwm->passfunc(pwm, pwm->passdata);
838 goto gotpassword;
841 if (*error == EPWMD_CACHE_NOT_FOUND) {
842 #ifdef USE_PINENTRY
844 * Get the password from pinentry.
846 if (pwm->use_pinentry) {
848 * Nonblocking is wanted. fork() then return a file descriptor
849 * that the client can use to read() from.
851 if (nb) {
852 int p[2];
853 pid_t pid;
854 pwmd_nb_status_t pw;
856 if (pipe(p) == -1) {
857 *error = gpg_error_from_syserror();
858 return -1;
861 pid = fork();
863 switch (pid) {
864 case 0:
865 close(p[0]);
866 strncpy(pw.filename, filename, sizeof(pw.filename));
867 pw.filename[sizeof(pw.filename)-1] = 0;
868 pw.fd = p[0];
870 if (timeout > 0) {
871 gpwm = pwm;
872 gtimeout = timeout;
873 gelapsed = 0;
876 getpin_nb_again:
877 *error = getpin(pwm, &password, &pin_try, 0);
879 if (*error) {
880 getpin_nb_fail:
881 if (pwm->pctx)
882 pinentry_disconnect(pwm);
884 if (gtimeout && gelapsed >= gtimeout)
885 *error = GPG_ERR_TIMEOUT;
887 pw.error = *error;
888 write(p[1], &pw, sizeof(pw));
889 close(p[1]);
890 _exit(1);
894 * Don't count the time it takes to open the file
895 * which may have many iterations.
897 signal(SIGALRM, SIG_DFL);
898 *error = do_open_command(pwm, filename, password);
900 if (timeout)
901 signal(SIGALRM, catchsig);
903 if (pwm->pctx && *error == EPWMD_BADKEY) {
904 if (pin_try-- > 0)
905 goto getpin_nb_again;
907 goto getpin_nb_fail;
910 pinentry_disconnect(pwm);
911 pw.error = 0;
912 write(p[1], &pw, sizeof(pw));
913 close(p[1]);
914 _exit(0);
915 break;
916 case -1:
917 *error = gpg_error_from_syserror();
918 close(p[0]);
919 close(p[1]);
920 return -1;
921 default:
922 break;
925 close(p[1]);
926 return p[0];
929 getpin_again:
930 *error = getpin(pwm, &password, &pin_try, 1);
932 if (*error) {
933 if (pwm->pctx)
934 pinentry_disconnect(pwm);
936 if (pwm->pin_error) {
937 *error = pwm->pin_error;
938 pwm->pin_error = 0;
941 return 1;
944 else {
945 #endif
947 * Not using pinentry and the file was not found
948 * in the cache.
950 #if 0
951 if (pwm->password == NULL) {
952 *error = EPWMD_KEY;
953 return 1;
955 #endif
957 password = pwm->password;
958 #ifdef USE_PINENTRY
960 #endif
963 else if (*error)
964 return nb ? -1 : 1;
966 gotpassword:
967 *error = do_open_command(pwm, filename, password);
970 * Keep the user defined password set with pwmd_setopt(). The password may
971 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
973 if (password && password != pwm->password)
974 xfree(password);
976 #ifdef USE_PINENTRY
977 if (pwm->pctx && *error == EPWMD_BADKEY) {
978 if (pin_try-- > 0)
979 goto getpin_again;
981 pinentry_disconnect(pwm);
982 return nb ? -1 : 1;
984 else if (pwm->pctx)
985 pinentry_disconnect(pwm);
986 #endif
988 if (!*error) {
989 if (pwm->filename)
990 xfree(pwm->filename);
992 pwm->filename = xstrdup(filename);
996 * The file is cached or the file is a new file.
998 if (nb)
999 return *error ? -1 : -2;
1001 return *error ? 1 : 0;
1004 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1006 gpg_error_t error;
1008 do_pwmd_open(pwm, &error, filename, 0, 0);
1009 return error;
1012 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1013 int timeout)
1015 #ifndef USE_PINENTRY
1016 *error = GPG_ERR_NOT_IMPLEMENTED;
1017 return -1;
1018 #else
1019 return do_pwmd_open(pwm, error, filename, 1, timeout);
1020 #endif
1023 #ifdef USE_PINENTRY
1024 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1026 int confirm = 0;
1027 gpg_error_t error;
1028 char *result = NULL;
1029 int pin_try = -1;
1031 again:
1032 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1034 if (error) {
1035 if (pwm->pctx)
1036 pinentry_disconnect(pwm);
1038 if (*password)
1039 xfree(*password);
1041 return error;
1044 if (!confirm++) {
1045 *password = result;
1046 goto again;
1049 if (strcmp(*password, result)) {
1050 xfree(*password);
1051 xfree(result);
1052 pinentry_disconnect(pwm);
1053 error = EPWMD_BADKEY;
1054 return error;
1057 xfree(result);
1058 pinentry_disconnect(pwm);
1059 return 0;
1061 #endif
1063 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1065 char buf[ASSUAN_LINELENGTH];
1066 gpg_error_t error;
1067 char *result = NULL;
1069 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1070 error = send_command(pwm, &result, buf);
1071 memset(&buf, 0, sizeof(buf));
1073 if (error && result)
1074 xfree(result);
1076 return error;
1079 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1081 gpg_error_t error;
1083 #ifndef USE_PINENTRY
1084 return GPG_ERR_NOT_IMPLEMENTED;
1085 #endif
1087 if (!pwm || !pw || !pw->filename[0])
1088 return GPG_ERR_INV_ARG;
1090 close(pw->fd);
1092 if (pw->error) {
1093 error = pw->error;
1094 memset(pw, 0, sizeof(pwmd_nb_status_t));
1095 return error;
1098 memset(pw, 0, sizeof(pwmd_nb_status_t));
1099 return 0;
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;