Make sure the STORE command has following data.
[libpwmd.git] / libpwmd.c
blob588ee13a8f0fd57a117d362502f724bd72531a0e
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 if (pwm->inquire_func) {
215 for (;;) {
216 char *result = NULL;
217 size_t len;
219 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
220 rc = gpg_err_code(rc);
222 if (rc == GPG_ERR_EOF) {
223 rc = 0;
224 break;
226 else if (rc)
227 break;
229 rc = assuan_send_data(pwm->ctx, result, len);
232 return rc;
235 return 0;
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)
268 gpg_error_t error;
269 char *line;
270 size_t len;
272 if (!pwm || !cmd)
273 return GPG_ERR_INV_ARG;
275 error = assuan_write_line(pwm->ctx, cmd);
277 if (error)
278 return error;
280 error = assuan_read_line(pwm->ctx, &line, &len);
281 return error;
284 gpg_error_t pwmd_inquire_process(pwm_t *pwm, const void *buf, size_t len)
286 gpg_error_t error;
288 if (!pwm)
289 return GPG_ERR_INV_ARG;
291 error = assuan_send_data(pwm->ctx, buf, len);
292 return error;
295 gpg_error_t pwmd_inquire_finish(pwm_t *pwm)
297 gpg_error_t error;
298 char *line;
299 size_t len;
301 if (!pwm)
302 return GPG_ERR_INV_ARG;
304 error = assuan_send_data(pwm->ctx, NULL, 0);
306 if (error)
307 return error;
309 error = assuan_read_line(pwm->ctx, &line, &len);
310 return error;
313 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
315 #ifndef USE_PINENTRY
316 return GPG_ERR_NOT_IMPLEMENTED;
317 #endif
319 if (!pwm || pwm->pid == -1)
320 return GPG_ERR_INV_ARG;
322 if (kill(pwm->pid, 0) == 0) {
323 if (kill(pwm->pid, SIGTERM) == -1) {
324 if (kill(pwm->pid, SIGKILL) == -1)
325 return gpg_error_from_errno(errno);
328 global_error = GPG_ERR_TIMEOUT;
330 else
331 return gpg_error_from_errno(errno);
333 return 0;
336 #ifdef USE_PINENTRY
337 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
339 char *buf;
340 char tmp[ASSUAN_LINELENGTH];
341 gpg_error_t error;
343 if (!pwm->title)
344 pwm->title = xstrdup(N_("LibPWMD"));
346 if (!pwm->prompt)
347 pwm->prompt = xstrdup(N_("Password:"));
349 if (!pwm->desc && !which)
350 pwm->desc = xstrdup(N_("Enter a password."));
352 if (which == 1) {
353 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
354 buf = xstrdup(tmp);
356 else if (which == 2) {
357 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
358 buf = xstrdup(tmp);
360 else {
361 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
362 sprintf(buf, "SETERROR %s", pwm->desc);
365 error = pinentry_command(pwm, NULL, buf);
366 xfree(buf);
368 if (error)
369 return error;
371 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
372 sprintf(buf, "SETPROMPT %s", pwm->prompt);
373 error = pinentry_command(pwm, NULL, buf);
374 xfree(buf);
376 if (error)
377 return error;
379 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
380 sprintf(buf, "SETDESC %s", pwm->title);
381 error = pinentry_command(pwm, NULL, buf);
382 xfree(buf);
383 return error;
386 static void update_pinentry_settings(pwm_t *pwm)
388 FILE *fp;
389 char buf[LINE_MAX];
390 struct passwd *pw = getpwuid(getuid());
391 char *p;
393 snprintf(buf, sizeof(buf), "%s/.pwmd/env", pw->pw_dir);
395 if ((fp = fopen(buf, "r")) == NULL)
396 return;
398 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
399 char name[32], val[256];
401 if (sscanf(p, "%[a-zA-Z]=%s", name, val) != 2)
402 continue;
404 if (strcasecmp(name, "TTY") == 0) {
405 if (!pwm->pinentry_tty) {
406 xfree(pwm->pinentry_tty);
407 pwm->pinentry_tty = xstrdup(val);
410 else if (strcasecmp(name, "TERM") == 0) {
411 if (!pwm->pinentry_term) {
412 xfree(pwm->pinentry_term);
413 pwm->pinentry_term = xstrdup(val);
416 else if (strcasecmp(name, "DISPLAY") == 0) {
417 if (!pwm->pinentry_display) {
418 xfree(pwm->pinentry_display);
419 pwm->pinentry_display = xstrdup(val);
424 fclose(fp);
427 static gpg_error_t launch_pinentry(pwm_t *pwm)
429 int rc;
430 assuan_context_t ctx;
431 int child_list[] = {-1};
432 char *display = getenv("DISPLAY");
433 const char *argv[6];
434 int have_display = 0;
435 char *tty = NULL;
437 update_pinentry_settings(pwm);
439 if (pwm->pinentry_display || display)
440 have_display = 1;
441 else {
442 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
444 if (!tty)
445 return gpg_error_from_errno(errno);
448 if (!display && !tty)
449 return GPG_ERR_ENOTTY;
451 argv[0] = "pinentry";
452 argv[1] = have_display ? "--display" : "--ttyname";
453 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
454 argv[3] = NULL;
456 if (!have_display) {
457 argv[3] = "--ttytype";
458 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
459 argv[5] = NULL;
462 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
464 if (rc)
465 return rc;
467 pwm->pid = assuan_get_pid(ctx);
468 pwm->pctx = ctx;
469 return set_pinentry_strings(pwm, 0);
472 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
474 gpg_error_t n;
476 if (!pwm->pctx) {
477 n = launch_pinentry(pwm);
479 if (n)
480 return n;
483 return assuan_command(pwm, pwm->pctx, result, cmd);
486 static void pinentry_disconnect(pwm_t *pwm)
488 if (pwm->pctx)
489 assuan_disconnect(pwm->pctx);
491 pwm->pctx = NULL;
492 pwm->pid = -1;
496 * Only called from a child process.
498 static void catchsig(int sig)
500 switch (sig) {
501 case SIGALRM:
502 if (gelapsed++ >= gtimeout) {
503 global_error = pwmd_terminate_pinentry(gpwm);
505 if (!global_error)
506 global_error = GPG_ERR_TIMEOUT;
508 break;
511 alarm(1);
512 break;
513 default:
514 break;
518 static char *percent_escape(const char *atext)
520 const unsigned char *s;
521 int len = strlen(atext) * 3 + 1;
522 char *buf = (char *)xmalloc(len), *p = buf;
524 if (!buf)
525 return NULL;
527 for (s=(const unsigned char *)atext; *s; s++) {
528 if (*s < ' ') {
529 sprintf (p, "%%%02X", *s);
530 p += 3;
532 else
533 *p++ = *s;
536 *p = 0;
537 return buf;
539 #endif
541 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
543 if (!cmd)
544 return GPG_ERR_INV_ARG;
546 return assuan_command(pwm, pwm->ctx, result, cmd);
550 * Avoid sending the BYE command here. libassuan will close the file
551 * descriptor and release the assuan context. Use pwmd_close() instead.
553 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
555 va_list ap;
556 char *buf;
557 size_t len;
558 gpg_error_t error;
560 if (!pwm || !cmd)
561 return GPG_ERR_INV_ARG;
563 *result = NULL;
564 va_start(ap, cmd);
566 * C99
568 len = vsnprintf(NULL, 0, cmd, ap);
569 buf = (char *)xmalloc(len + 1);
570 len = vsnprintf(buf, len + 1, cmd, ap);
571 va_end(ap);
572 error = send_command(pwm, result, buf);
573 xfree(buf);
574 return error;
577 #ifdef USE_PINENTRY
578 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
580 gpg_error_t error;
582 if (gtimeout) {
583 signal(SIGALRM, catchsig);
584 alarm(1);
587 *result = NULL;
588 error = pinentry_command(pwm, result, "GETPIN");
590 if (error == GPG_ERR_ASS_CANCELED)
591 return error;
593 if (!*result)
594 return EPWMD_KEY;
596 return 0;
599 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
601 int pin_try = *try_n;
602 gpg_error_t error;
604 getpin_again:
605 *try_n = pin_try;
607 if (pin_try == -1) {
608 error = set_pinentry_strings(pwm, which);
610 if (error) {
611 pinentry_disconnect(pwm);
612 return error;
615 else {
616 if (pwm->pinentry_tries-1 != pin_try) {
617 error = set_pinentry_strings(pwm, 1);
619 if (error) {
620 pinentry_disconnect(pwm);
621 return error;
626 error = do_getpin(pwm, result);
629 * Since there was input cancel any timeout.
631 alarm(0);
633 if (error) {
634 if (pin_try != -1 && pin_try-- && error == EPWMD_KEY)
635 goto getpin_again;
637 if (pwm->pctx)
638 pinentry_disconnect(pwm);
640 *try_n = pin_try;
641 return error;
644 return 0;
646 #endif
648 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
650 char *result;
651 gpg_error_t error;
653 #ifndef USE_PINENTRY
654 return GPG_ERR_NOT_IMPLEMENTED;
655 #endif
657 if (!pwm || !pw || !pw->filename[0])
658 return GPG_ERR_INV_ARG;
660 close(pw->fd);
662 if (pw->error) {
663 error = pw->error;
664 goto fail;
667 error = pwmd_command(pwm, &result, "ISCACHED %s", pw->filename);
669 if (error)
670 goto fail;
672 if (pwm->filename)
673 xfree(pwm->filename);
675 pwm->filename = xstrdup(pw->filename);
676 memset(pw, 0, sizeof(pwmd_nb_status_t));
677 return 0;
679 fail:
680 memset(pw, 0, sizeof(pwmd_nb_status_t));
681 return error;
684 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
686 char buf[ASSUAN_LINELENGTH];
687 gpg_error_t error;
689 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
690 error = send_command(pwm, NULL, buf);
691 memset(buf, 0, sizeof(buf));
692 return error;
695 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
696 int nb, int timeout)
698 char *result = NULL;
699 char *password = NULL;
700 char path[PATH_MAX];
701 #ifdef USE_PINENTRY
702 int pin_try;
703 #endif
705 if (!pwm || !filename || !*filename) {
706 *error = GPG_ERR_INV_ARG;
707 return nb ? -1 : 1;
710 #ifdef USE_PINENTRY
711 pin_try = pwm->pinentry_tries - 1;
712 #endif
715 * Avoid calling pinentry if the password is cached on the server or if
716 * this is a new file.
718 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
720 if (*error)
721 return nb ? -1 : 1;
723 snprintf(path, sizeof(path), "%s/%s", result, filename);
724 pwmd_free_result(result);
726 if (access(path, R_OK) == -1) {
727 if (errno == ENOENT)
728 goto gotpassword;
730 *error = gpg_error_from_errno(errno);
731 return nb ? -1 : 1;
734 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
736 if (*error == EPWMD_CACHE_NOT_FOUND) {
737 if (pwm->passfunc) {
738 password = pwm->passfunc(pwm, pwm->passdata);
740 if (!password || !*password) {
741 *error = EPWMD_KEY;
742 return nb ? -1 : 1;
745 password = xstrdup(password);
746 goto gotpassword;
749 if (*error == EPWMD_CACHE_NOT_FOUND) {
750 #ifdef USE_PINENTRY
752 * Get the password from pinentry.
754 if (pwm->use_pinentry) {
756 * Nonblocking is wanted. fork() then return a file descriptor
757 * that the client can use to read() from.
759 if (nb) {
760 int p[2];
761 pid_t pid;
762 pwmd_nb_status_t pw;
764 if (pipe(p) == -1) {
765 *error = gpg_error_from_syserror();
766 return -1;
769 pid = fork();
771 switch (pid) {
772 case 0:
773 close(p[0]);
774 strncpy(pw.filename, filename, sizeof(pw.filename));
775 pw.fd = p[0];
777 if (timeout > 0) {
778 gpwm = pwm;
779 gtimeout = timeout;
780 gelapsed = 0;
783 getpin_nb_again:
784 *error = getpin(pwm, &password, &pin_try, 0);
786 if (*error) {
787 getpin_nb_fail:
788 if (pwm->pctx)
789 pinentry_disconnect(pwm);
791 if (gtimeout && gelapsed >= gtimeout)
792 *error = GPG_ERR_TIMEOUT;
794 pw.error = *error;
795 write(p[1], &pw, sizeof(pw));
796 close(p[1]);
797 _exit(1);
801 * Don't count the time it takes to open the file
802 * which may have many iterations.
804 signal(SIGALRM, SIG_DFL);
805 *error = do_open_command(pwm, filename, password);
807 if (timeout)
808 signal(SIGALRM, catchsig);
810 if (pwm->pctx && *error == EPWMD_BADKEY) {
811 if (pin_try-- > 0)
812 goto getpin_nb_again;
814 goto getpin_nb_fail;
817 pinentry_disconnect(pwm);
818 pw.error = 0;
819 write(p[1], &pw, sizeof(pw));
820 close(p[1]);
821 _exit(0);
822 break;
823 case -1:
824 *error = gpg_error_from_syserror();
825 close(p[0]);
826 close(p[1]);
827 return -1;
828 default:
829 break;
832 close(p[1]);
833 return p[0];
836 getpin_again:
837 *error = getpin(pwm, &password, &pin_try, 1);
839 if (*error) {
840 if (pwm->pctx)
841 pinentry_disconnect(pwm);
843 if (global_error) {
844 *error = global_error;
845 global_error = 0;
848 return 1;
851 else {
852 #endif
854 * Not using pinentry and the file was not found
855 * in the cache.
857 if (pwm->password == NULL) {
858 *error = EPWMD_KEY;
859 return 1;
862 password = pwm->password;
863 #ifdef USE_PINENTRY
865 #endif
868 else if (*error)
869 return nb ? -1 : 1;
871 gotpassword:
872 *error = do_open_command(pwm, filename, password);
875 * Keep the user defined password set with pwmd_setopt(). The password may
876 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
878 if (password && password != pwm->password)
879 xfree(password);
881 #ifdef USE_PINENTRY
882 if (pwm->pctx && *error == EPWMD_BADKEY) {
883 if (pin_try-- > 0)
884 goto getpin_again;
886 pinentry_disconnect(pwm);
887 return nb ? -1 : 1;
889 #endif
891 if (!*error) {
892 if (pwm->filename)
893 xfree(pwm->filename);
895 pwm->filename = xstrdup(filename);
899 * The file is cached or the file is a new file.
901 if (nb)
902 return *error ? -1 : -2;
904 return *error ? 1 : 0;
907 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
909 gpg_error_t error;
911 do_pwmd_open(pwm, &error, filename, 0, 0);
912 return error;
915 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
916 int timeout)
918 #ifndef USE_PINENTRY
919 *error = GPG_ERR_NOT_IMPLEMENTED;
920 return -1;
921 #else
922 return do_pwmd_open(pwm, error, filename, 1, timeout);
923 #endif
926 #ifdef USE_PINENTRY
927 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
929 int confirm = 0;
930 gpg_error_t error;
931 char *result = NULL;
932 int pin_try = -1;
934 again:
935 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
937 if (error) {
938 if (pwm->pctx)
939 pinentry_disconnect(pwm);
941 if (*password)
942 xfree(*password);
944 return error;
947 if (!confirm++) {
948 *password = result;
949 goto again;
952 if (strcmp(*password, result)) {
953 xfree(*password);
954 xfree(result);
955 pinentry_disconnect(pwm);
956 error = EPWMD_BADKEY;
957 return error;
960 xfree(result);
961 pinentry_disconnect(pwm);
962 return 0;
964 #endif
966 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
968 char buf[ASSUAN_LINELENGTH];
969 gpg_error_t error;
971 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
972 error = send_command(pwm, NULL, buf);
973 memset(&buf, 0, sizeof(buf));
974 return error;
977 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
979 gpg_error_t error;
981 #ifndef USE_PINENTRY
982 return GPG_ERR_NOT_IMPLEMENTED;
983 #endif
985 if (!pwm || !pw || !pw->filename[0])
986 return GPG_ERR_INV_ARG;
988 close(pw->fd);
990 if (pw->error) {
991 error = pw->error;
992 memset(pw, 0, sizeof(pwmd_nb_status_t));
993 return error;
996 memset(pw, 0, sizeof(pwmd_nb_status_t));
997 return 0;
1000 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1002 char *result = NULL;
1003 char *password = NULL;
1005 if (!pwm) {
1006 *error = GPG_ERR_INV_ARG;
1007 return nb ? -1 : 1;
1010 if (pwm->use_pinentry || pwm->passfunc) {
1011 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1013 if (*error == EPWMD_CACHE_NOT_FOUND) {
1014 #ifdef USE_PINENTRY
1015 if (pwm->use_pinentry) {
1016 if (nb) {
1017 int p[2];
1018 pid_t pid;
1019 pwmd_nb_status_t pw;
1021 if (pipe(p) == -1) {
1022 *error = gpg_error_from_syserror();
1023 return -1;
1026 pid = fork();
1028 switch (pid) {
1029 case 0:
1030 close(p[0]);
1031 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1032 pw.fd = p[0];
1034 do {
1035 password = NULL;
1036 *error = do_save_getpin(pwm, &password);
1037 } while (*error == EPWMD_KEY || *error == EPWMD_BADKEY);
1039 if (*error) {
1040 if (pwm->pctx)
1041 pinentry_disconnect(pwm);
1043 pw.error = *error;
1044 write(p[1], &pw, sizeof(pw));
1045 close(p[1]);
1046 _exit(1);
1049 *error = do_save_command(pwm, password);
1050 pinentry_disconnect(pwm);
1051 pw.error = *error;
1052 write(p[1], &pw, sizeof(pw));
1053 close(p[1]);
1054 _exit(0);
1055 break;
1056 case -1:
1057 *error = gpg_error_from_syserror();
1058 close(p[0]);
1059 close(p[1]);
1060 return -1;
1061 default:
1062 break;
1065 close(p[1]);
1066 return p[0];
1069 *error = do_save_getpin(pwm, &password);
1071 if (*error)
1072 return 1;
1074 else {
1075 #endif
1076 if (pwm->passfunc) {
1077 char *tmp = (*pwm->passfunc)(pwm, pwm->passdata);
1079 if (!tmp || !*tmp) {
1080 *error = EPWMD_KEY;
1081 return 1;
1084 password = xstrdup(tmp);
1086 #ifdef USE_PINENTRY
1088 #endif
1090 if (!password || !*password) {
1091 *error = EPWMD_KEY;
1092 return 1;
1095 else {
1096 if (*error)
1097 return nb ? -1 : 1;
1100 else
1101 password = pwm->password;
1103 *error = do_save_command(pwm, password);
1105 if (password && password != pwm->password)
1106 xfree(password);
1108 if (nb)
1109 return *error ? -1 : -2;
1111 return *error ? 1 : 0;
1114 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1116 #ifndef USE_PINENTRY
1117 *error = GPG_ERR_NOT_IMPLEMENTED;
1118 return -1;
1119 #else
1120 return do_pwmd_save(pwm, error, 1);
1121 #endif
1124 gpg_error_t pwmd_save(pwm_t *pwm)
1126 gpg_error_t error;
1128 do_pwmd_save(pwm, &error, 0);
1129 return error;
1132 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1134 va_list ap;
1135 #ifdef USE_PINENTRY
1136 int n = va_arg(ap, int);
1137 #endif
1138 char *arg1;
1140 if (!pwm)
1141 return GPG_ERR_INV_ARG;
1143 va_start(ap, opt);
1145 switch (opt) {
1146 case PWMD_OPTION_INQUIRE_FUNC:
1147 pwm->inquire_func = va_arg(ap, pwmd_inquire_fn);
1148 break;
1149 case PWMD_OPTION_INQUIRE_DATA:
1150 pwm->inquire_data = va_arg(ap, void *);
1151 break;
1152 case PWMD_OPTION_STATUS_FUNC:
1153 pwm->status_func = va_arg(ap, pwmd_status_fn);
1154 break;
1155 case PWMD_OPTION_STATUS_DATA:
1156 pwm->status_data = va_arg(ap, void *);
1157 break;
1158 case PWMD_OPTION_PASSWORD_FUNC:
1159 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1160 break;
1161 case PWMD_OPTION_PASSWORD_DATA:
1162 pwm->passdata = va_arg(ap, void *);
1163 break;
1164 case PWMD_OPTION_PASSWORD:
1165 arg1 = va_arg(ap, char *);
1167 if (pwm->password)
1168 xfree(pwm->password);
1170 pwm->password = xstrdup(arg1);
1171 break;
1172 #ifdef USE_PINENTRY
1173 case PWMD_OPTION_PINENTRY:
1174 n = va_arg(ap, int);
1176 if (n != 0 && n != 1) {
1177 va_end(ap);
1178 return GPG_ERR_INV_VALUE;
1181 pwm->use_pinentry = n;
1182 break;
1183 case PWMD_OPTION_PINENTRY_TRIES:
1184 n = va_arg(ap, int);
1186 if (n <= 0) {
1187 va_end(ap);
1188 return GPG_ERR_INV_VALUE;
1191 pwm->pinentry_tries = n;
1192 break;
1193 case PWMD_OPTION_PINENTRY_PATH:
1194 if (pwm->pinentry_path)
1195 xfree(pwm->pinentry_path);
1197 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1198 break;
1199 case PWMD_OPTION_PINENTRY_TTY:
1200 if (pwm->pinentry_tty)
1201 xfree(pwm->pinentry_tty);
1203 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1204 break;
1205 case PWMD_OPTION_PINENTRY_DISPLAY:
1206 if (pwm->pinentry_display)
1207 xfree(pwm->pinentry_display);
1209 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1210 break;
1211 case PWMD_OPTION_PINENTRY_TERM:
1212 if (pwm->pinentry_term)
1213 xfree(pwm->pinentry_term);
1215 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1216 break;
1217 case PWMD_OPTION_PINENTRY_TITLE:
1218 if (pwm->title)
1219 xfree(pwm->title);
1220 pwm->title = percent_escape(va_arg(ap, char *));
1221 break;
1222 case PWMD_OPTION_PINENTRY_PROMPT:
1223 if (pwm->prompt)
1224 xfree(pwm->prompt);
1225 pwm->prompt = percent_escape(va_arg(ap, char *));
1226 break;
1227 case PWMD_OPTION_PINENTRY_DESC:
1228 if (pwm->desc)
1229 xfree(pwm->desc);
1230 pwm->desc = percent_escape(va_arg(ap, char *));
1231 break;
1232 #else
1233 case PWMD_OPTION_PINENTRY:
1234 case PWMD_OPTION_PINENTRY_TRIES:
1235 case PWMD_OPTION_PINENTRY_PATH:
1236 case PWMD_OPTION_PINENTRY_TTY:
1237 case PWMD_OPTION_PINENTRY_DISPLAY:
1238 case PWMD_OPTION_PINENTRY_TERM:
1239 case PWMD_OPTION_PINENTRY_TITLE:
1240 case PWMD_OPTION_PINENTRY_PROMPT:
1241 case PWMD_OPTION_PINENTRY_DESC:
1242 return GPG_ERR_NOT_IMPLEMENTED;
1243 #endif
1244 default:
1245 va_end(ap);
1246 return GPG_ERR_NOT_IMPLEMENTED;
1249 va_end(ap);
1250 return 0;