Added pwmd_inquire(). This removes PWMD_OPTION_INQUIRE_FUNC and
[libpwmd.git] / libpwmd.c
blob1c299db68baab49b4602d92f686642b30dc5f28c
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2007 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 <libpwmd.h>
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
43 #ifdef HAVE_ASSUAN_H
44 #include <assuan.h>
45 #endif
47 #ifdef HAVE_SETLOCALE
48 #include <locale.h>
49 #endif
51 #include "gettext.h"
52 #define N_(msgid) dgettext("libpwmd", msgid)
54 #ifndef MEM_DEBUG
55 #include "mem.h"
56 #else
57 #define xfree free
58 #define xrealloc realloc
59 #define xmalloc malloc
60 #define xstrdup strdup
61 #define xcalloc calloc
62 #endif
64 #include "types.h"
66 #ifdef USE_PINENTRY
67 static pwm_t *gpwm;
68 static int gelapsed, gtimeout;
69 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd);
70 #endif
72 static gpg_error_t global_error;
74 const char *pwmd_strerror(gpg_error_t e)
76 gpg_err_code_t code = gpg_err_code(e);
78 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
79 switch (code) {
80 case GPG_ERR_USER_1:
81 default:
82 return N_("Unknown error");
83 case GPG_ERR_USER_2:
84 return N_("No cache slots available");
85 case GPG_ERR_USER_3:
86 return N_("Recursion loop");
87 case GPG_ERR_USER_4:
88 return N_("No file is open");
89 case GPG_ERR_USER_5:
90 return N_("General LibXML error");
91 case GPG_ERR_USER_6:
92 return N_("File modified");
96 return gpg_strerror(e);
99 gpg_error_t pwmd_init()
101 #ifdef ENABLE_NLS
102 bindtextdomain("libpwmd", LOCALEDIR);
103 #endif
104 gpg_err_init();
105 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
106 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
107 return 0;
110 pwm_t *pwmd_connect(const char *path, gpg_error_t *error)
112 pwm_t *pwm = NULL;
113 char *socketpath = NULL;
114 time_t now;
115 struct passwd *pw;
116 assuan_context_t ctx;
117 int rc;
119 if (!path) {
120 pw = getpwuid(getuid());
121 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
122 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
124 else
125 socketpath = xstrdup(path);
127 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
128 xfree(socketpath);
130 if (rc) {
131 *error = rc;
132 return NULL;
135 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
136 *error = gpg_error_from_errno(errno);
137 assuan_disconnect(ctx);
138 return NULL;
141 pwm->ctx = ctx;
142 #ifdef USE_PINENTRY
143 pwm->pid = -1;
144 pwm->pinentry_tries = 3;
145 #endif
146 time(&now);
147 srandom(now);
148 *error = 0;
149 return pwm;
152 void pwmd_close(pwm_t *pwm)
154 if (!pwm)
155 return;
157 if (pwm->ctx)
158 assuan_disconnect(pwm->ctx);
160 if (pwm->password)
161 xfree(pwm->password);
163 if (pwm->title)
164 xfree(pwm->title);
166 if (pwm->desc)
167 xfree(pwm->desc);
169 if (pwm->prompt)
170 xfree(pwm->prompt);
172 if (pwm->pinentry_tty)
173 xfree(pwm->pinentry_tty);
175 if (pwm->pinentry_display)
176 xfree(pwm->pinentry_display);
178 if (pwm->pinentry_term)
179 xfree(pwm->pinentry_term);
181 if (pwm->filename)
182 xfree(pwm->filename);
184 xfree(pwm);
187 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
189 membuf_t *mem = (membuf_t *)data;
190 void *p;
192 if (!buffer)
193 return 0;
195 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
196 return 1;
198 mem->buf = p;
199 memcpy((char *)mem->buf + mem->len, buffer, len);
200 mem->len += len;
201 return 0;
204 void pwmd_free_result(void *data)
206 xfree(data);
209 static int _inquire_cb(void *data, const char *keyword)
211 pwm_t *pwm = (pwm_t *)data;
212 gpg_error_t rc = 0;
214 /* Shouldn't get this far without a callback. */
215 if (!pwm->inquire_func)
216 return GPG_ERR_INV_ARG;
218 for (;;) {
219 char *result = NULL;
220 size_t len;
222 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
223 rc = gpg_err_code(rc);
225 if (rc == GPG_ERR_EOF) {
226 rc = 0;
227 break;
229 else if (rc)
230 break;
232 rc = assuan_send_data(pwm->ctx, result, len);
235 return rc;
238 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
239 char **result, const char *cmd)
241 membuf_t data;
242 gpg_error_t rc;
244 data.len = 0;
245 data.buf = NULL;
247 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
248 pwm->status_func, pwm->status_data);
250 if (rc) {
251 if (data.buf) {
252 xfree(data.buf);
253 data.buf = NULL;
256 else {
257 if (data.buf) {
258 mem_realloc_cb(&data, "", 1);
259 *result = (char *)data.buf;
263 return gpg_err_code(rc);
266 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
267 void *data)
269 if (!pwm || !cmd || !fn)
270 return GPG_ERR_INV_ARG;
272 pwm->inquire_func = fn;
273 pwm->inquire_data = data;
274 return assuan_command(pwm, pwm->ctx, NULL, cmd);
277 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
279 #ifndef USE_PINENTRY
280 return GPG_ERR_NOT_IMPLEMENTED;
281 #endif
283 if (!pwm || pwm->pid == -1)
284 return GPG_ERR_INV_ARG;
286 if (kill(pwm->pid, 0) == 0) {
287 if (kill(pwm->pid, SIGTERM) == -1) {
288 if (kill(pwm->pid, SIGKILL) == -1)
289 return gpg_error_from_errno(errno);
292 global_error = GPG_ERR_TIMEOUT;
294 else
295 return gpg_error_from_errno(errno);
297 return 0;
300 #ifdef USE_PINENTRY
301 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
303 char *buf;
304 char tmp[ASSUAN_LINELENGTH];
305 gpg_error_t error;
307 if (!pwm->title)
308 pwm->title = xstrdup(N_("LibPWMD"));
310 if (!pwm->prompt)
311 pwm->prompt = xstrdup(N_("Password:"));
313 if (!pwm->desc && !which)
314 pwm->desc = xstrdup(N_("Enter a password."));
316 if (which == 1) {
317 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
318 buf = xstrdup(tmp);
320 else if (which == 2) {
321 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
322 buf = xstrdup(tmp);
324 else {
325 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
326 sprintf(buf, "SETERROR %s", pwm->desc);
329 error = pinentry_command(pwm, NULL, buf);
330 xfree(buf);
332 if (error)
333 return error;
335 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
336 sprintf(buf, "SETPROMPT %s", pwm->prompt);
337 error = pinentry_command(pwm, NULL, buf);
338 xfree(buf);
340 if (error)
341 return error;
343 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
344 sprintf(buf, "SETDESC %s", pwm->title);
345 error = pinentry_command(pwm, NULL, buf);
346 xfree(buf);
347 return error;
350 static void update_pinentry_settings(pwm_t *pwm)
352 FILE *fp;
353 char buf[LINE_MAX];
354 struct passwd *pw = getpwuid(getuid());
355 char *p;
357 snprintf(buf, sizeof(buf), "%s/.pwmd/env", pw->pw_dir);
359 if ((fp = fopen(buf, "r")) == NULL)
360 return;
362 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
363 char name[32], val[256];
365 if (sscanf(p, "%[a-zA-Z]=%s", name, val) != 2)
366 continue;
368 if (strcasecmp(name, "TTY") == 0) {
369 if (!pwm->pinentry_tty) {
370 xfree(pwm->pinentry_tty);
371 pwm->pinentry_tty = xstrdup(val);
374 else if (strcasecmp(name, "TERM") == 0) {
375 if (!pwm->pinentry_term) {
376 xfree(pwm->pinentry_term);
377 pwm->pinentry_term = xstrdup(val);
380 else if (strcasecmp(name, "DISPLAY") == 0) {
381 if (!pwm->pinentry_display) {
382 xfree(pwm->pinentry_display);
383 pwm->pinentry_display = xstrdup(val);
388 fclose(fp);
391 static gpg_error_t launch_pinentry(pwm_t *pwm)
393 int rc;
394 assuan_context_t ctx;
395 int child_list[] = {-1};
396 char *display = getenv("DISPLAY");
397 const char *argv[6];
398 int have_display = 0;
399 char *tty = NULL;
401 update_pinentry_settings(pwm);
403 if (pwm->pinentry_display || display)
404 have_display = 1;
405 else {
406 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
408 if (!tty)
409 return gpg_error_from_errno(errno);
412 if (!display && !tty)
413 return GPG_ERR_ENOTTY;
415 argv[0] = "pinentry";
416 argv[1] = have_display ? "--display" : "--ttyname";
417 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
418 argv[3] = NULL;
420 if (!have_display) {
421 argv[3] = "--ttytype";
422 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
423 argv[5] = NULL;
426 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
428 if (rc)
429 return rc;
431 pwm->pid = assuan_get_pid(ctx);
432 pwm->pctx = ctx;
433 return set_pinentry_strings(pwm, 0);
436 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
438 gpg_error_t n;
440 if (!pwm->pctx) {
441 n = launch_pinentry(pwm);
443 if (n)
444 return n;
447 return assuan_command(pwm, pwm->pctx, result, cmd);
450 static void pinentry_disconnect(pwm_t *pwm)
452 if (pwm->pctx)
453 assuan_disconnect(pwm->pctx);
455 pwm->pctx = NULL;
456 pwm->pid = -1;
460 * Only called from a child process.
462 static void catchsig(int sig)
464 switch (sig) {
465 case SIGALRM:
466 if (gelapsed++ >= gtimeout) {
467 global_error = pwmd_terminate_pinentry(gpwm);
469 if (!global_error)
470 global_error = GPG_ERR_TIMEOUT;
472 break;
475 alarm(1);
476 break;
477 default:
478 break;
482 static char *percent_escape(const char *atext)
484 const unsigned char *s;
485 int len = strlen(atext) * 3 + 1;
486 char *buf = (char *)xmalloc(len), *p = buf;
488 if (!buf)
489 return NULL;
491 for (s=(const unsigned char *)atext; *s; s++) {
492 if (*s < ' ') {
493 sprintf (p, "%%%02X", *s);
494 p += 3;
496 else
497 *p++ = *s;
500 *p = 0;
501 return buf;
503 #endif
505 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
507 if (!cmd)
508 return GPG_ERR_INV_ARG;
510 return assuan_command(pwm, pwm->ctx, result, cmd);
514 * Avoid sending the BYE command here. libassuan will close the file
515 * descriptor and release the assuan context. Use pwmd_close() instead.
517 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
519 va_list ap;
520 char *buf;
521 size_t len;
522 gpg_error_t error;
524 if (!pwm || !cmd)
525 return GPG_ERR_INV_ARG;
527 *result = NULL;
528 va_start(ap, cmd);
530 * C99
532 len = vsnprintf(NULL, 0, cmd, ap);
533 buf = (char *)xmalloc(len + 1);
534 len = vsnprintf(buf, len + 1, cmd, ap);
535 va_end(ap);
536 error = send_command(pwm, result, buf);
537 xfree(buf);
538 return error;
541 #ifdef USE_PINENTRY
542 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
544 gpg_error_t error;
546 if (gtimeout) {
547 signal(SIGALRM, catchsig);
548 alarm(1);
551 *result = NULL;
552 error = pinentry_command(pwm, result, "GETPIN");
554 if (error == GPG_ERR_ASS_CANCELED)
555 return error;
557 if (!*result)
558 return EPWMD_KEY;
560 return 0;
563 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
565 int pin_try = *try_n;
566 gpg_error_t error;
568 getpin_again:
569 *try_n = pin_try;
571 if (pin_try == -1) {
572 error = set_pinentry_strings(pwm, which);
574 if (error) {
575 pinentry_disconnect(pwm);
576 return error;
579 else {
580 if (pwm->pinentry_tries-1 != pin_try) {
581 error = set_pinentry_strings(pwm, 1);
583 if (error) {
584 pinentry_disconnect(pwm);
585 return error;
590 error = do_getpin(pwm, result);
593 * Since there was input cancel any timeout.
595 alarm(0);
597 if (error) {
598 if (pin_try != -1 && pin_try-- && error == EPWMD_KEY)
599 goto getpin_again;
601 if (pwm->pctx)
602 pinentry_disconnect(pwm);
604 *try_n = pin_try;
605 return error;
608 return 0;
610 #endif
612 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
614 char *result;
615 gpg_error_t error;
617 #ifndef USE_PINENTRY
618 return GPG_ERR_NOT_IMPLEMENTED;
619 #endif
621 if (!pwm || !pw || !pw->filename[0])
622 return GPG_ERR_INV_ARG;
624 close(pw->fd);
626 if (pw->error) {
627 error = pw->error;
628 goto fail;
631 error = pwmd_command(pwm, &result, "ISCACHED %s", pw->filename);
633 if (error)
634 goto fail;
636 if (pwm->filename)
637 xfree(pwm->filename);
639 pwm->filename = xstrdup(pw->filename);
640 memset(pw, 0, sizeof(pwmd_nb_status_t));
641 return 0;
643 fail:
644 memset(pw, 0, sizeof(pwmd_nb_status_t));
645 return error;
648 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
650 char buf[ASSUAN_LINELENGTH];
651 gpg_error_t error;
653 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
654 error = send_command(pwm, NULL, buf);
655 memset(buf, 0, sizeof(buf));
656 return error;
659 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
660 int nb, int timeout)
662 char *result = NULL;
663 char *password = NULL;
664 char path[PATH_MAX];
665 #ifdef USE_PINENTRY
666 int pin_try;
667 #endif
669 if (!pwm || !filename || !*filename) {
670 *error = GPG_ERR_INV_ARG;
671 return nb ? -1 : 1;
674 #ifdef USE_PINENTRY
675 pin_try = pwm->pinentry_tries - 1;
676 #endif
679 * Avoid calling pinentry if the password is cached on the server or if
680 * this is a new file.
682 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
684 if (*error)
685 return nb ? -1 : 1;
687 snprintf(path, sizeof(path), "%s/%s", result, filename);
688 pwmd_free_result(result);
690 if (access(path, R_OK) == -1) {
691 if (errno == ENOENT)
692 goto gotpassword;
694 *error = gpg_error_from_errno(errno);
695 return nb ? -1 : 1;
698 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
700 if (*error == EPWMD_CACHE_NOT_FOUND) {
701 if (pwm->passfunc) {
702 password = pwm->passfunc(pwm, pwm->passdata);
704 if (!password || !*password) {
705 *error = EPWMD_KEY;
706 return nb ? -1 : 1;
709 password = xstrdup(password);
710 goto gotpassword;
713 if (*error == EPWMD_CACHE_NOT_FOUND) {
714 #ifdef USE_PINENTRY
716 * Get the password from pinentry.
718 if (pwm->use_pinentry) {
720 * Nonblocking is wanted. fork() then return a file descriptor
721 * that the client can use to read() from.
723 if (nb) {
724 int p[2];
725 pid_t pid;
726 pwmd_nb_status_t pw;
728 if (pipe(p) == -1) {
729 *error = gpg_error_from_syserror();
730 return -1;
733 pid = fork();
735 switch (pid) {
736 case 0:
737 close(p[0]);
738 strncpy(pw.filename, filename, sizeof(pw.filename));
739 pw.fd = p[0];
741 if (timeout > 0) {
742 gpwm = pwm;
743 gtimeout = timeout;
744 gelapsed = 0;
747 getpin_nb_again:
748 *error = getpin(pwm, &password, &pin_try, 0);
750 if (*error) {
751 getpin_nb_fail:
752 if (pwm->pctx)
753 pinentry_disconnect(pwm);
755 if (gtimeout && gelapsed >= gtimeout)
756 *error = GPG_ERR_TIMEOUT;
758 pw.error = *error;
759 write(p[1], &pw, sizeof(pw));
760 close(p[1]);
761 _exit(1);
765 * Don't count the time it takes to open the file
766 * which may have many iterations.
768 signal(SIGALRM, SIG_DFL);
769 *error = do_open_command(pwm, filename, password);
771 if (timeout)
772 signal(SIGALRM, catchsig);
774 if (pwm->pctx && *error == EPWMD_BADKEY) {
775 if (pin_try-- > 0)
776 goto getpin_nb_again;
778 goto getpin_nb_fail;
781 pinentry_disconnect(pwm);
782 pw.error = 0;
783 write(p[1], &pw, sizeof(pw));
784 close(p[1]);
785 _exit(0);
786 break;
787 case -1:
788 *error = gpg_error_from_syserror();
789 close(p[0]);
790 close(p[1]);
791 return -1;
792 default:
793 break;
796 close(p[1]);
797 return p[0];
800 getpin_again:
801 *error = getpin(pwm, &password, &pin_try, 1);
803 if (*error) {
804 if (pwm->pctx)
805 pinentry_disconnect(pwm);
807 if (global_error) {
808 *error = global_error;
809 global_error = 0;
812 return 1;
815 else {
816 #endif
818 * Not using pinentry and the file was not found
819 * in the cache.
821 if (pwm->password == NULL) {
822 *error = EPWMD_KEY;
823 return 1;
826 password = pwm->password;
827 #ifdef USE_PINENTRY
829 #endif
832 else if (*error)
833 return nb ? -1 : 1;
835 gotpassword:
836 *error = do_open_command(pwm, filename, password);
839 * Keep the user defined password set with pwmd_setopt(). The password may
840 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
842 if (password && password != pwm->password)
843 xfree(password);
845 #ifdef USE_PINENTRY
846 if (pwm->pctx && *error == EPWMD_BADKEY) {
847 if (pin_try-- > 0)
848 goto getpin_again;
850 pinentry_disconnect(pwm);
851 return nb ? -1 : 1;
853 #endif
855 if (!*error) {
856 if (pwm->filename)
857 xfree(pwm->filename);
859 pwm->filename = xstrdup(filename);
863 * The file is cached or the file is a new file.
865 if (nb)
866 return *error ? -1 : -2;
868 return *error ? 1 : 0;
871 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
873 gpg_error_t error;
875 do_pwmd_open(pwm, &error, filename, 0, 0);
876 return error;
879 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
880 int timeout)
882 #ifndef USE_PINENTRY
883 *error = GPG_ERR_NOT_IMPLEMENTED;
884 return -1;
885 #else
886 return do_pwmd_open(pwm, error, filename, 1, timeout);
887 #endif
890 #ifdef USE_PINENTRY
891 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
893 int confirm = 0;
894 gpg_error_t error;
895 char *result = NULL;
896 int pin_try = -1;
898 again:
899 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
901 if (error) {
902 if (pwm->pctx)
903 pinentry_disconnect(pwm);
905 if (*password)
906 xfree(*password);
908 return error;
911 if (!confirm++) {
912 *password = result;
913 goto again;
916 if (strcmp(*password, result)) {
917 xfree(*password);
918 xfree(result);
919 pinentry_disconnect(pwm);
920 error = EPWMD_BADKEY;
921 return error;
924 xfree(result);
925 pinentry_disconnect(pwm);
926 return 0;
928 #endif
930 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
932 char buf[ASSUAN_LINELENGTH];
933 gpg_error_t error;
935 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
936 error = send_command(pwm, NULL, buf);
937 memset(&buf, 0, sizeof(buf));
938 return error;
941 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
943 gpg_error_t error;
945 #ifndef USE_PINENTRY
946 return GPG_ERR_NOT_IMPLEMENTED;
947 #endif
949 if (!pwm || !pw || !pw->filename[0])
950 return GPG_ERR_INV_ARG;
952 close(pw->fd);
954 if (pw->error) {
955 error = pw->error;
956 memset(pw, 0, sizeof(pwmd_nb_status_t));
957 return error;
960 memset(pw, 0, sizeof(pwmd_nb_status_t));
961 return 0;
964 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
966 char *result = NULL;
967 char *password = NULL;
969 if (!pwm) {
970 *error = GPG_ERR_INV_ARG;
971 return nb ? -1 : 1;
974 if (pwm->use_pinentry || pwm->passfunc) {
975 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
977 if (*error == EPWMD_CACHE_NOT_FOUND) {
978 #ifdef USE_PINENTRY
979 if (pwm->use_pinentry) {
980 if (nb) {
981 int p[2];
982 pid_t pid;
983 pwmd_nb_status_t pw;
985 if (pipe(p) == -1) {
986 *error = gpg_error_from_syserror();
987 return -1;
990 pid = fork();
992 switch (pid) {
993 case 0:
994 close(p[0]);
995 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
996 pw.fd = p[0];
998 do {
999 password = NULL;
1000 *error = do_save_getpin(pwm, &password);
1001 } while (*error == EPWMD_KEY || *error == EPWMD_BADKEY);
1003 if (*error) {
1004 if (pwm->pctx)
1005 pinentry_disconnect(pwm);
1007 pw.error = *error;
1008 write(p[1], &pw, sizeof(pw));
1009 close(p[1]);
1010 _exit(1);
1013 *error = do_save_command(pwm, password);
1014 pinentry_disconnect(pwm);
1015 pw.error = *error;
1016 write(p[1], &pw, sizeof(pw));
1017 close(p[1]);
1018 _exit(0);
1019 break;
1020 case -1:
1021 *error = gpg_error_from_syserror();
1022 close(p[0]);
1023 close(p[1]);
1024 return -1;
1025 default:
1026 break;
1029 close(p[1]);
1030 return p[0];
1033 *error = do_save_getpin(pwm, &password);
1035 if (*error)
1036 return 1;
1038 else {
1039 #endif
1040 if (pwm->passfunc) {
1041 char *tmp = (*pwm->passfunc)(pwm, pwm->passdata);
1043 if (!tmp || !*tmp) {
1044 *error = EPWMD_KEY;
1045 return 1;
1048 password = xstrdup(tmp);
1050 #ifdef USE_PINENTRY
1052 #endif
1054 if (!password || !*password) {
1055 *error = EPWMD_KEY;
1056 return 1;
1059 else {
1060 if (*error)
1061 return nb ? -1 : 1;
1064 else
1065 password = pwm->password;
1067 *error = do_save_command(pwm, password);
1069 if (password && password != pwm->password)
1070 xfree(password);
1072 if (nb)
1073 return *error ? -1 : -2;
1075 return *error ? 1 : 0;
1078 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1080 #ifndef USE_PINENTRY
1081 *error = GPG_ERR_NOT_IMPLEMENTED;
1082 return -1;
1083 #else
1084 return do_pwmd_save(pwm, error, 1);
1085 #endif
1088 gpg_error_t pwmd_save(pwm_t *pwm)
1090 gpg_error_t error;
1092 do_pwmd_save(pwm, &error, 0);
1093 return error;
1096 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1098 va_list ap;
1099 #ifdef USE_PINENTRY
1100 int n = va_arg(ap, int);
1101 #endif
1102 char *arg1;
1104 if (!pwm)
1105 return GPG_ERR_INV_ARG;
1107 va_start(ap, opt);
1109 switch (opt) {
1110 case PWMD_OPTION_STATUS_FUNC:
1111 pwm->status_func = va_arg(ap, pwmd_status_fn);
1112 break;
1113 case PWMD_OPTION_STATUS_DATA:
1114 pwm->status_data = va_arg(ap, void *);
1115 break;
1116 case PWMD_OPTION_PASSWORD_FUNC:
1117 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1118 break;
1119 case PWMD_OPTION_PASSWORD_DATA:
1120 pwm->passdata = va_arg(ap, void *);
1121 break;
1122 case PWMD_OPTION_PASSWORD:
1123 arg1 = va_arg(ap, char *);
1125 if (pwm->password)
1126 xfree(pwm->password);
1128 pwm->password = xstrdup(arg1);
1129 break;
1130 #ifdef USE_PINENTRY
1131 case PWMD_OPTION_PINENTRY:
1132 n = va_arg(ap, int);
1134 if (n != 0 && n != 1) {
1135 va_end(ap);
1136 return GPG_ERR_INV_VALUE;
1139 pwm->use_pinentry = n;
1140 break;
1141 case PWMD_OPTION_PINENTRY_TRIES:
1142 n = va_arg(ap, int);
1144 if (n <= 0) {
1145 va_end(ap);
1146 return GPG_ERR_INV_VALUE;
1149 pwm->pinentry_tries = n;
1150 break;
1151 case PWMD_OPTION_PINENTRY_PATH:
1152 if (pwm->pinentry_path)
1153 xfree(pwm->pinentry_path);
1155 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1156 break;
1157 case PWMD_OPTION_PINENTRY_TTY:
1158 if (pwm->pinentry_tty)
1159 xfree(pwm->pinentry_tty);
1161 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1162 break;
1163 case PWMD_OPTION_PINENTRY_DISPLAY:
1164 if (pwm->pinentry_display)
1165 xfree(pwm->pinentry_display);
1167 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1168 break;
1169 case PWMD_OPTION_PINENTRY_TERM:
1170 if (pwm->pinentry_term)
1171 xfree(pwm->pinentry_term);
1173 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1174 break;
1175 case PWMD_OPTION_PINENTRY_TITLE:
1176 if (pwm->title)
1177 xfree(pwm->title);
1178 pwm->title = percent_escape(va_arg(ap, char *));
1179 break;
1180 case PWMD_OPTION_PINENTRY_PROMPT:
1181 if (pwm->prompt)
1182 xfree(pwm->prompt);
1183 pwm->prompt = percent_escape(va_arg(ap, char *));
1184 break;
1185 case PWMD_OPTION_PINENTRY_DESC:
1186 if (pwm->desc)
1187 xfree(pwm->desc);
1188 pwm->desc = percent_escape(va_arg(ap, char *));
1189 break;
1190 #else
1191 case PWMD_OPTION_PINENTRY:
1192 case PWMD_OPTION_PINENTRY_TRIES:
1193 case PWMD_OPTION_PINENTRY_PATH:
1194 case PWMD_OPTION_PINENTRY_TTY:
1195 case PWMD_OPTION_PINENTRY_DISPLAY:
1196 case PWMD_OPTION_PINENTRY_TERM:
1197 case PWMD_OPTION_PINENTRY_TITLE:
1198 case PWMD_OPTION_PINENTRY_PROMPT:
1199 case PWMD_OPTION_PINENTRY_DESC:
1200 return GPG_ERR_NOT_IMPLEMENTED;
1201 #endif
1202 default:
1203 va_end(ap);
1204 return GPG_ERR_NOT_IMPLEMENTED;
1207 va_end(ap);
1208 return 0;