Changed some error codes.
[libpwmd.git] / libpwmd.c
blob7331f74d8ac637d24f84b6a188998928677138ad
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/pinentry.conf", 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, " %31[a-zA-Z] = %255s", name, val) != 2)
366 continue;
368 if (strcasecmp(name, "TTYNAME") == 0) {
369 xfree(pwm->pinentry_tty);
370 pwm->pinentry_tty = xstrdup(val);
372 else if (strcasecmp(name, "TTYTYPE") == 0) {
373 xfree(pwm->pinentry_term);
374 pwm->pinentry_term = xstrdup(val);
376 else if (strcasecmp(name, "DISPLAY") == 0) {
377 xfree(pwm->pinentry_display);
378 pwm->pinentry_display = xstrdup(val);
380 else if (strcasecmp(name, "PATH") == 0) {
381 xfree(pwm->pinentry_path);
382 pwm->pinentry_path = xstrdup(val);
386 fclose(fp);
389 static gpg_error_t launch_pinentry(pwm_t *pwm)
391 int rc;
392 assuan_context_t ctx;
393 int child_list[] = {-1};
394 char *display = getenv("DISPLAY");
395 const char *argv[6];
396 int have_display = 0;
397 char *tty = NULL;
399 update_pinentry_settings(pwm);
401 if (pwm->pinentry_display || display)
402 have_display = 1;
403 else {
404 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
406 if (!tty)
407 return gpg_error_from_errno(errno);
410 if (!have_display && !tty)
411 return GPG_ERR_ENOTTY;
413 argv[0] = "pinentry";
414 argv[1] = have_display ? "--display" : "--ttyname";
415 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
416 argv[3] = NULL;
418 if (!have_display) {
419 argv[3] = "--ttytype";
420 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
421 argv[5] = NULL;
424 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
426 if (rc)
427 return rc;
429 pwm->pid = assuan_get_pid(ctx);
430 pwm->pctx = ctx;
431 return set_pinentry_strings(pwm, 0);
434 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
436 gpg_error_t n;
438 if (!pwm->pctx) {
439 n = launch_pinentry(pwm);
441 if (n)
442 return n;
445 return assuan_command(pwm, pwm->pctx, result, cmd);
448 static void pinentry_disconnect(pwm_t *pwm)
450 if (pwm->pctx)
451 assuan_disconnect(pwm->pctx);
453 pwm->pctx = NULL;
454 pwm->pid = -1;
458 * Only called from a child process.
460 static void catchsig(int sig)
462 switch (sig) {
463 case SIGALRM:
464 if (gelapsed++ >= gtimeout) {
465 global_error = pwmd_terminate_pinentry(gpwm);
467 if (!global_error)
468 global_error = GPG_ERR_TIMEOUT;
470 break;
473 alarm(1);
474 break;
475 default:
476 break;
480 static char *percent_escape(const char *atext)
482 const unsigned char *s;
483 int len = strlen(atext) * 3 + 1;
484 char *buf = (char *)xmalloc(len), *p = buf;
486 if (!buf)
487 return NULL;
489 for (s=(const unsigned char *)atext; *s; s++) {
490 if (*s < ' ') {
491 sprintf (p, "%%%02X", *s);
492 p += 3;
494 else
495 *p++ = *s;
498 *p = 0;
499 return buf;
501 #endif
503 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
505 if (!cmd)
506 return GPG_ERR_INV_ARG;
508 return assuan_command(pwm, pwm->ctx, result, cmd);
512 * Avoid sending the BYE command here. libassuan will close the file
513 * descriptor and release the assuan context. Use pwmd_close() instead.
515 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
517 va_list ap;
518 char *buf;
519 size_t len;
520 gpg_error_t error;
522 if (!pwm || !cmd)
523 return GPG_ERR_INV_ARG;
525 *result = NULL;
526 va_start(ap, cmd);
528 * C99
530 len = vsnprintf(NULL, 0, cmd, ap);
531 buf = (char *)xmalloc(len + 1);
532 len = vsnprintf(buf, len + 1, cmd, ap);
533 va_end(ap);
534 error = send_command(pwm, result, buf);
535 xfree(buf);
536 return error;
539 #ifdef USE_PINENTRY
540 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
542 if (gtimeout) {
543 signal(SIGALRM, catchsig);
544 alarm(1);
547 *result = NULL;
548 return pinentry_command(pwm, result, "GETPIN");
551 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
553 int pin_try = *try_n;
554 gpg_error_t error;
556 getpin_again:
557 *try_n = pin_try;
559 if (pin_try == -1) {
560 error = set_pinentry_strings(pwm, which);
562 if (error) {
563 pinentry_disconnect(pwm);
564 return error;
567 else {
568 if (pwm->pinentry_tries-1 != pin_try) {
569 error = set_pinentry_strings(pwm, 1);
571 if (error) {
572 pinentry_disconnect(pwm);
573 return error;
578 error = do_getpin(pwm, result);
581 * Since there was input cancel any timeout.
583 alarm(0);
585 if (error) {
586 if (error == GPG_ERR_ASS_CANCELED || error == ASSUAN_Canceled)
587 return GPG_ERR_ASS_CANCELED;
589 if (pin_try != -1 && pin_try--)
590 goto getpin_again;
592 if (pwm->pctx)
593 pinentry_disconnect(pwm);
595 *try_n = pin_try;
596 return error;
599 return 0;
601 #endif
603 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
605 char *result;
606 gpg_error_t error;
608 #ifndef USE_PINENTRY
609 return GPG_ERR_NOT_IMPLEMENTED;
610 #endif
612 if (!pwm || !pw || !pw->filename[0])
613 return GPG_ERR_INV_ARG;
615 close(pw->fd);
617 if (pw->error) {
618 error = pw->error;
619 goto fail;
622 error = pwmd_command(pwm, &result, "ISCACHED %s", pw->filename);
624 if (error)
625 goto fail;
627 if (pwm->filename)
628 xfree(pwm->filename);
630 pwm->filename = xstrdup(pw->filename);
631 memset(pw, 0, sizeof(pwmd_nb_status_t));
632 return 0;
634 fail:
635 memset(pw, 0, sizeof(pwmd_nb_status_t));
636 return error;
639 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
641 char buf[ASSUAN_LINELENGTH];
642 gpg_error_t error;
644 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
645 error = send_command(pwm, NULL, buf);
646 memset(buf, 0, sizeof(buf));
647 return error;
650 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
651 int nb, int timeout)
653 char *result = NULL;
654 char *password = NULL;
655 char path[PATH_MAX];
656 #ifdef USE_PINENTRY
657 int pin_try;
658 #endif
660 if (!pwm || !filename || !*filename) {
661 *error = GPG_ERR_INV_ARG;
662 return nb ? -1 : 1;
665 #ifdef USE_PINENTRY
666 pin_try = pwm->pinentry_tries - 1;
667 #endif
670 * Avoid calling pinentry if the password is cached on the server or if
671 * this is a new file.
673 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
675 if (*error)
676 return nb ? -1 : 1;
678 snprintf(path, sizeof(path), "%s/%s", result, filename);
679 pwmd_free_result(result);
681 if (access(path, R_OK) == -1) {
682 if (errno == ENOENT)
683 goto gotpassword;
685 *error = gpg_error_from_errno(errno);
686 return nb ? -1 : 1;
689 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
691 if (*error == EPWMD_CACHE_NOT_FOUND) {
692 if (pwm->passfunc) {
693 password = pwm->passfunc(pwm, pwm->passdata);
694 goto gotpassword;
697 if (*error == EPWMD_CACHE_NOT_FOUND) {
698 #ifdef USE_PINENTRY
700 * Get the password from pinentry.
702 if (pwm->use_pinentry) {
704 * Nonblocking is wanted. fork() then return a file descriptor
705 * that the client can use to read() from.
707 if (nb) {
708 int p[2];
709 pid_t pid;
710 pwmd_nb_status_t pw;
712 if (pipe(p) == -1) {
713 *error = gpg_error_from_syserror();
714 return -1;
717 pid = fork();
719 switch (pid) {
720 case 0:
721 close(p[0]);
722 strncpy(pw.filename, filename, sizeof(pw.filename));
723 pw.fd = p[0];
725 if (timeout > 0) {
726 gpwm = pwm;
727 gtimeout = timeout;
728 gelapsed = 0;
731 getpin_nb_again:
732 *error = getpin(pwm, &password, &pin_try, 0);
734 if (*error) {
735 getpin_nb_fail:
736 if (pwm->pctx)
737 pinentry_disconnect(pwm);
739 if (gtimeout && gelapsed >= gtimeout)
740 *error = GPG_ERR_TIMEOUT;
742 pw.error = *error;
743 write(p[1], &pw, sizeof(pw));
744 close(p[1]);
745 _exit(1);
749 * Don't count the time it takes to open the file
750 * which may have many iterations.
752 signal(SIGALRM, SIG_DFL);
753 *error = do_open_command(pwm, filename, password);
755 if (timeout)
756 signal(SIGALRM, catchsig);
758 if (pwm->pctx && *error == EPWMD_BADKEY) {
759 if (pin_try-- > 0)
760 goto getpin_nb_again;
762 goto getpin_nb_fail;
765 pinentry_disconnect(pwm);
766 pw.error = 0;
767 write(p[1], &pw, sizeof(pw));
768 close(p[1]);
769 _exit(0);
770 break;
771 case -1:
772 *error = gpg_error_from_syserror();
773 close(p[0]);
774 close(p[1]);
775 return -1;
776 default:
777 break;
780 close(p[1]);
781 return p[0];
784 getpin_again:
785 *error = getpin(pwm, &password, &pin_try, 1);
787 if (*error) {
788 if (pwm->pctx)
789 pinentry_disconnect(pwm);
791 if (global_error) {
792 *error = global_error;
793 global_error = 0;
796 return 1;
799 else {
800 #endif
802 * Not using pinentry and the file was not found
803 * in the cache.
805 #if 0
806 if (pwm->password == NULL) {
807 *error = EPWMD_KEY;
808 return 1;
810 #endif
812 password = pwm->password;
813 #ifdef USE_PINENTRY
815 #endif
818 else if (*error)
819 return nb ? -1 : 1;
821 gotpassword:
822 *error = do_open_command(pwm, filename, password);
825 * Keep the user defined password set with pwmd_setopt(). The password may
826 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
828 if (password && password != pwm->password)
829 xfree(password);
831 #ifdef USE_PINENTRY
832 if (pwm->pctx && *error == EPWMD_BADKEY) {
833 if (pin_try-- > 0)
834 goto getpin_again;
836 pinentry_disconnect(pwm);
837 return nb ? -1 : 1;
839 #endif
841 if (!*error) {
842 if (pwm->filename)
843 xfree(pwm->filename);
845 pwm->filename = xstrdup(filename);
849 * The file is cached or the file is a new file.
851 if (nb)
852 return *error ? -1 : -2;
854 return *error ? 1 : 0;
857 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
859 gpg_error_t error;
861 do_pwmd_open(pwm, &error, filename, 0, 0);
862 return error;
865 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
866 int timeout)
868 #ifndef USE_PINENTRY
869 *error = GPG_ERR_NOT_IMPLEMENTED;
870 return -1;
871 #else
872 return do_pwmd_open(pwm, error, filename, 1, timeout);
873 #endif
876 #ifdef USE_PINENTRY
877 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
879 int confirm = 0;
880 gpg_error_t error;
881 char *result = NULL;
882 int pin_try = -1;
884 again:
885 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
887 if (error) {
888 if (pwm->pctx)
889 pinentry_disconnect(pwm);
891 if (*password)
892 xfree(*password);
894 return error;
897 if (!confirm++) {
898 *password = result;
899 goto again;
902 if (strcmp(*password, result)) {
903 xfree(*password);
904 xfree(result);
905 pinentry_disconnect(pwm);
906 error = EPWMD_BADKEY;
907 return error;
910 xfree(result);
911 pinentry_disconnect(pwm);
912 return 0;
914 #endif
916 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
918 char buf[ASSUAN_LINELENGTH];
919 gpg_error_t error;
921 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
922 error = send_command(pwm, NULL, buf);
923 memset(&buf, 0, sizeof(buf));
924 return error;
927 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
929 gpg_error_t error;
931 #ifndef USE_PINENTRY
932 return GPG_ERR_NOT_IMPLEMENTED;
933 #endif
935 if (!pwm || !pw || !pw->filename[0])
936 return GPG_ERR_INV_ARG;
938 close(pw->fd);
940 if (pw->error) {
941 error = pw->error;
942 memset(pw, 0, sizeof(pwmd_nb_status_t));
943 return error;
946 memset(pw, 0, sizeof(pwmd_nb_status_t));
947 return 0;
950 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
952 char *result = NULL;
953 char *password = NULL;
955 if (!pwm) {
956 *error = GPG_ERR_INV_ARG;
957 return nb ? -1 : 1;
960 if (pwm->use_pinentry || pwm->passfunc) {
961 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
963 if (*error == EPWMD_CACHE_NOT_FOUND) {
964 #ifdef USE_PINENTRY
965 if (pwm->use_pinentry) {
966 if (nb) {
967 int p[2];
968 pid_t pid;
969 pwmd_nb_status_t pw;
971 if (pipe(p) == -1) {
972 *error = gpg_error_from_syserror();
973 return -1;
976 pid = fork();
978 switch (pid) {
979 case 0:
980 close(p[0]);
981 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
982 pw.fd = p[0];
984 do {
985 password = NULL;
986 *error = do_save_getpin(pwm, &password);
987 } while (*error == EPWMD_BADKEY);
989 if (*error) {
990 if (pwm->pctx)
991 pinentry_disconnect(pwm);
993 pw.error = *error;
994 write(p[1], &pw, sizeof(pw));
995 close(p[1]);
996 _exit(1);
999 *error = do_save_command(pwm, password);
1000 pinentry_disconnect(pwm);
1001 pw.error = *error;
1002 write(p[1], &pw, sizeof(pw));
1003 close(p[1]);
1004 _exit(0);
1005 break;
1006 case -1:
1007 *error = gpg_error_from_syserror();
1008 close(p[0]);
1009 close(p[1]);
1010 return -1;
1011 default:
1012 break;
1015 close(p[1]);
1016 return p[0];
1019 *error = do_save_getpin(pwm, &password);
1021 if (*error)
1022 return 1;
1024 else {
1025 #endif
1026 if (pwm->passfunc)
1027 password = (*pwm->passfunc)(pwm, pwm->passdata);
1028 #ifdef USE_PINENTRY
1030 #endif
1032 else {
1033 if (*error)
1034 return nb ? -1 : 1;
1037 else
1038 password = pwm->password;
1040 *error = do_save_command(pwm, password);
1042 if (password && password != pwm->password)
1043 xfree(password);
1045 if (nb)
1046 return *error ? -1 : -2;
1048 return *error ? 1 : 0;
1051 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1053 #ifndef USE_PINENTRY
1054 *error = GPG_ERR_NOT_IMPLEMENTED;
1055 return -1;
1056 #else
1057 return do_pwmd_save(pwm, error, 1);
1058 #endif
1061 gpg_error_t pwmd_save(pwm_t *pwm)
1063 gpg_error_t error;
1065 do_pwmd_save(pwm, &error, 0);
1066 return error;
1069 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1071 va_list ap;
1072 #ifdef USE_PINENTRY
1073 int n = va_arg(ap, int);
1074 #endif
1075 char *arg1;
1076 char *result;
1077 gpg_error_t error = 0;
1079 if (!pwm)
1080 return GPG_ERR_INV_ARG;
1082 va_start(ap, opt);
1084 switch (opt) {
1085 case PWMD_OPTION_STATUS_FUNC:
1086 pwm->status_func = va_arg(ap, pwmd_status_fn);
1087 break;
1088 case PWMD_OPTION_STATUS_DATA:
1089 pwm->status_data = va_arg(ap, void *);
1090 break;
1091 case PWMD_OPTION_PASSWORD_FUNC:
1092 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1093 break;
1094 case PWMD_OPTION_PASSWORD_DATA:
1095 pwm->passdata = va_arg(ap, void *);
1096 break;
1097 case PWMD_OPTION_PASSWORD:
1098 arg1 = va_arg(ap, char *);
1100 if (pwm->password)
1101 xfree(pwm->password);
1103 pwm->password = xstrdup(arg1);
1104 break;
1105 #ifdef USE_PINENTRY
1106 case PWMD_OPTION_PINENTRY:
1107 n = va_arg(ap, int);
1109 if (n != 0 && n != 1) {
1110 va_end(ap);
1111 error = GPG_ERR_INV_VALUE;
1113 else {
1114 pwm->use_pinentry = n;
1115 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
1116 !pwm->use_pinentry);
1118 break;
1119 case PWMD_OPTION_PINENTRY_TRIES:
1120 n = va_arg(ap, int);
1122 if (n <= 0) {
1123 va_end(ap);
1124 error = GPG_ERR_INV_VALUE;
1126 else
1127 pwm->pinentry_tries = n;
1128 break;
1129 case PWMD_OPTION_PINENTRY_PATH:
1130 if (pwm->pinentry_path)
1131 xfree(pwm->pinentry_path);
1133 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1134 break;
1135 case PWMD_OPTION_PINENTRY_TTY:
1136 if (pwm->pinentry_tty)
1137 xfree(pwm->pinentry_tty);
1139 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1140 break;
1141 case PWMD_OPTION_PINENTRY_DISPLAY:
1142 if (pwm->pinentry_display)
1143 xfree(pwm->pinentry_display);
1145 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1146 break;
1147 case PWMD_OPTION_PINENTRY_TERM:
1148 if (pwm->pinentry_term)
1149 xfree(pwm->pinentry_term);
1151 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1152 break;
1153 case PWMD_OPTION_PINENTRY_TITLE:
1154 if (pwm->title)
1155 xfree(pwm->title);
1156 pwm->title = percent_escape(va_arg(ap, char *));
1157 break;
1158 case PWMD_OPTION_PINENTRY_PROMPT:
1159 if (pwm->prompt)
1160 xfree(pwm->prompt);
1161 pwm->prompt = percent_escape(va_arg(ap, char *));
1162 break;
1163 case PWMD_OPTION_PINENTRY_DESC:
1164 if (pwm->desc)
1165 xfree(pwm->desc);
1166 pwm->desc = percent_escape(va_arg(ap, char *));
1167 break;
1168 #else
1169 case PWMD_OPTION_PINENTRY:
1170 case PWMD_OPTION_PINENTRY_TRIES:
1171 case PWMD_OPTION_PINENTRY_PATH:
1172 case PWMD_OPTION_PINENTRY_TTY:
1173 case PWMD_OPTION_PINENTRY_DISPLAY:
1174 case PWMD_OPTION_PINENTRY_TERM:
1175 case PWMD_OPTION_PINENTRY_TITLE:
1176 case PWMD_OPTION_PINENTRY_PROMPT:
1177 case PWMD_OPTION_PINENTRY_DESC:
1178 error = GPG_ERR_NOT_IMPLEMENTED;
1179 break;
1180 #endif
1181 default:
1182 error = GPG_ERR_NOT_IMPLEMENTED;
1183 break;
1186 va_end(ap);
1187 return error;