The default pinentry path can be specified by passing
[libpwmd.git] / libpwmd.c
blobe270bd1a1d31713f5fbda673433a21571cf6ef86
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 error)
76 gpg_err_code_t code = gpg_err_code(error);
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_("Element not found");
87 case GPG_ERR_USER_4:
88 return N_("Trailing element");
89 case GPG_ERR_USER_5:
90 return N_("Invalid character in element");
91 case GPG_ERR_USER_6:
92 return N_("Empty");
93 case GPG_ERR_USER_7:
94 return N_("Will not overwrite existing account");
95 case GPG_ERR_USER_8:
96 return N_("File not found");
97 case GPG_ERR_USER_9:
98 return N_("No file is open");
99 case GPG_ERR_USER_10:
100 return N_("General LibXML error");
101 case GPG_ERR_USER_11:
102 return N_("File not found in cache");
103 case GPG_ERR_USER_12:
104 return N_("Attribute not found");
105 case GPG_ERR_USER_13:
106 return N_("Invalid filename or link");
107 case GPG_ERR_USER_14:
108 return N_("File modified");
112 return gpg_strerror(error);
115 gpg_error_t pwmd_init()
117 #ifdef ENABLE_NLS
118 bindtextdomain("libpwmd", LOCALEDIR);
119 #endif
120 gpg_err_init();
121 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
122 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
123 return 0;
126 pwm_t *pwmd_connect(const char *path, gpg_error_t *error)
128 pwm_t *pwm = NULL;
129 char *socketpath = NULL;
130 time_t now;
131 struct passwd *pw;
132 assuan_context_t ctx;
133 int rc;
135 if (!path) {
136 pw = getpwuid(getuid());
137 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
138 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
140 else
141 socketpath = xstrdup(path);
143 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
144 xfree(socketpath);
146 if (rc) {
147 *error = rc;
148 return NULL;
151 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
152 *error = gpg_error_from_errno(errno);
153 assuan_disconnect(ctx);
154 return NULL;
157 pwm->ctx = ctx;
158 #ifdef USE_PINENTRY
159 pwm->pid = -1;
160 pwm->pinentry_tries = 3;
161 #endif
162 time(&now);
163 srandom(now);
164 return pwm;
167 void pwmd_close(pwm_t *pwm)
169 if (!pwm)
170 return;
172 if (pwm->ctx)
173 assuan_disconnect(pwm->ctx);
175 if (pwm->password)
176 xfree(pwm->password);
178 if (pwm->title)
179 xfree(pwm->title);
181 if (pwm->desc)
182 xfree(pwm->desc);
184 if (pwm->prompt)
185 xfree(pwm->prompt);
187 if (pwm->pinentry_tty)
188 xfree(pwm->pinentry_tty);
190 if (pwm->pinentry_display)
191 xfree(pwm->pinentry_display);
193 if (pwm->pinentry_term)
194 xfree(pwm->pinentry_term);
196 if (pwm->filename)
197 xfree(pwm->filename);
199 xfree(pwm);
202 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
204 membuf_t *mem = (membuf_t *)data;
205 void *p;
207 if (!buffer)
208 return 0;
210 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
211 return 1;
213 mem->buf = p;
214 memcpy((char *)mem->buf + mem->len, buffer, len);
215 mem->len += len;
216 return 0;
219 static int inquire_cb(void *data, const char *keyword)
221 struct inquire_s *inquire = (struct inquire_s *)data;
223 return assuan_send_data(inquire->ctx, inquire->buf, inquire->len);
226 void pwmd_free_result(void *data)
228 xfree(data);
231 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
232 char **result, const char *cmd)
234 membuf_t data;
235 gpg_error_t rc;
237 data.len = 0;
238 data.buf = NULL;
241 * This is needed because assuan only accepts 1000 byte command strings.
242 * If the line is more than this the command will fail. So we use an
243 * INQUIRE on the server which waits for an END that assuan_transact()
244 * fulfills.
246 * Other commands shouldn't need the INQUIRE. Let me know if you have an
247 * element path that's greater than 1000 bytes and I'll fix it.
249 if (strncasecmp(cmd, "STORE", 5) == 0) {
250 const char *p = cmd + 5;
251 struct inquire_s *inq = (struct inquire_s *)xmalloc(sizeof(struct inquire_s));
253 if (*p == ' ')
254 p++;
256 inq->ctx = ctx;
257 inq->buf = p;
258 inq->len = strlen(p);
259 rc = assuan_transact(ctx, "STORE", mem_realloc_cb, &data, inquire_cb,
260 inq, pwm->status_func, pwm->status_data);
261 xfree(inq);
263 else {
264 #ifdef USE_PINENTRY
266 * Ignore any status callback function for pinentry.
268 if (ctx == pwm->pctx)
269 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, NULL, NULL, NULL, NULL);
270 else
271 #endif
272 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, NULL, NULL,
273 pwm->status_func, pwm->status_data);
276 if (rc) {
277 if (data.buf) {
278 xfree(data.buf);
279 data.buf = NULL;
282 else {
283 if (data.buf) {
284 mem_realloc_cb(&data, "", 1);
285 *result = (char *)data.buf;
289 return gpg_err_code(rc);
292 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
294 #ifndef USE_PINENTRY
295 return GPG_ERR_NOT_IMPLEMENTED;
296 #endif
298 if (!pwm || pwm->pid == -1)
299 return GPG_ERR_INV_ARG;
301 if (kill(pwm->pid, 0) == 0) {
302 if (kill(pwm->pid, SIGTERM) == -1) {
303 if (kill(pwm->pid, SIGKILL) == -1)
304 return gpg_error_from_errno(errno);
307 global_error = GPG_ERR_TIMEOUT;
309 else
310 return gpg_error_from_errno(errno);
312 return 0;
315 #ifdef USE_PINENTRY
316 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
318 char *buf;
319 char tmp[ASSUAN_LINELENGTH];
320 gpg_error_t error;
322 if (!pwm->title)
323 pwm->title = xstrdup(N_("LibPWMD"));
325 if (!pwm->prompt)
326 pwm->prompt = xstrdup(N_("Password:"));
328 if (!pwm->desc && !which)
329 pwm->desc = xstrdup(N_("Enter a password."));
331 if (which == 1) {
332 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
333 buf = xstrdup(tmp);
335 else if (which == 2) {
336 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
337 buf = xstrdup(tmp);
339 else {
340 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
341 sprintf(buf, "SETERROR %s", pwm->desc);
344 error = pinentry_command(pwm, NULL, buf);
345 xfree(buf);
347 if (error)
348 return error;
350 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
351 sprintf(buf, "SETPROMPT %s", pwm->prompt);
352 error = pinentry_command(pwm, NULL, buf);
353 xfree(buf);
355 if (error)
356 return error;
358 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
359 sprintf(buf, "SETDESC %s", pwm->title);
360 error = pinentry_command(pwm, NULL, buf);
361 xfree(buf);
362 return error;
365 static void update_pinentry_settings(pwm_t *pwm)
367 FILE *fp;
368 char buf[LINE_MAX];
369 struct passwd *pw = getpwuid(getuid());
370 char *p;
372 snprintf(buf, sizeof(buf), "%s/.pwmd/env", pw->pw_dir);
374 if ((fp = fopen(buf, "r")) == NULL)
375 return;
377 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
378 char name[32], val[256];
380 if (sscanf(p, "%[a-zA-Z]=%s", name, val) != 2)
381 continue;
383 if (strcasecmp(name, "TTY") == 0) {
384 if (!pwm->pinentry_tty) {
385 xfree(pwm->pinentry_tty);
386 pwm->pinentry_tty = xstrdup(val);
389 else if (strcasecmp(name, "TERM") == 0) {
390 if (!pwm->pinentry_term) {
391 xfree(pwm->pinentry_term);
392 pwm->pinentry_term = xstrdup(val);
395 else if (strcasecmp(name, "DISPLAY") == 0) {
396 if (!pwm->pinentry_display) {
397 xfree(pwm->pinentry_display);
398 pwm->pinentry_display = xstrdup(val);
403 fclose(fp);
406 static gpg_error_t launch_pinentry(pwm_t *pwm)
408 int rc;
409 assuan_context_t ctx;
410 int child_list[] = {-1};
411 char *display = getenv("DISPLAY");
412 const char *argv[6];
413 int have_display = 0;
414 char *tty = NULL;
416 update_pinentry_settings(pwm);
418 if (pwm->pinentry_display || display)
419 have_display = 1;
420 else {
421 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
423 if (!tty)
424 return gpg_error_from_errno(errno);
427 if (!display && !tty)
428 return GPG_ERR_ENOTTY;
430 argv[0] = "pinentry";
431 argv[1] = have_display ? "--display" : "--ttyname";
432 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
433 argv[3] = NULL;
435 if (!have_display) {
436 argv[3] = "--ttytype";
437 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
438 argv[5] = NULL;
441 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
443 if (rc)
444 return rc;
446 pwm->pid = assuan_get_pid(ctx);
447 pwm->pctx = ctx;
448 return set_pinentry_strings(pwm, 0);
451 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
453 gpg_error_t n;
455 if (!pwm->pctx) {
456 n = launch_pinentry(pwm);
458 if (n)
459 return n;
462 return assuan_command(pwm, pwm->pctx, result, cmd);
465 static void pinentry_disconnect(pwm_t *pwm)
467 if (pwm->pctx)
468 assuan_disconnect(pwm->pctx);
470 pwm->pctx = NULL;
471 pwm->pid = -1;
475 * Only called from a child process.
477 static void catchsig(int sig)
479 switch (sig) {
480 case SIGALRM:
481 if (gelapsed++ >= gtimeout) {
482 global_error = pwmd_terminate_pinentry(gpwm);
484 if (!global_error)
485 global_error = GPG_ERR_TIMEOUT;
487 break;
490 alarm(1);
491 break;
492 default:
493 break;
497 static char *percent_escape(const char *atext)
499 const unsigned char *s;
500 int len = strlen(atext) * 3 + 1;
501 char *buf = (char *)xmalloc(len), *p = buf;
503 if (!buf)
504 return NULL;
506 for (s=(const unsigned char *)atext; *s; s++) {
507 if (*s < ' ') {
508 sprintf (p, "%%%02X", *s);
509 p += 3;
511 else
512 *p++ = *s;
515 *p = 0;
516 return buf;
518 #endif
520 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
522 if (!cmd)
523 return GPG_ERR_INV_ARG;
525 return assuan_command(pwm, pwm->ctx, result, cmd);
529 * Avoid sending the BYE command here. libassuan will close the file
530 * descriptor and release the assuan context. Use pwmd_close() instead.
532 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
534 va_list ap;
535 char *buf;
536 size_t len;
537 gpg_error_t error;
539 if (!pwm || !cmd)
540 return GPG_ERR_INV_ARG;
542 *result = NULL;
543 va_start(ap, cmd);
545 * C99
547 len = vsnprintf(NULL, 0, cmd, ap);
548 buf = (char *)xmalloc(len + 1);
549 len = vsnprintf(buf, len + 1, cmd, ap);
550 va_end(ap);
551 error = send_command(pwm, result, buf);
552 xfree(buf);
553 return error;
556 #ifdef USE_PINENTRY
557 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
559 gpg_error_t error;
561 if (gtimeout) {
562 signal(SIGALRM, catchsig);
563 alarm(1);
566 *result = NULL;
567 error = pinentry_command(pwm, result, "GETPIN");
569 if (error == GPG_ERR_ASS_CANCELED)
570 return error;
572 if (!*result)
573 return EPWMD_KEY;
575 return 0;
578 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
580 int pin_try = *try_n;
581 gpg_error_t error;
583 getpin_again:
584 *try_n = pin_try;
586 if (pin_try == -1) {
587 error = set_pinentry_strings(pwm, which);
589 if (error) {
590 pinentry_disconnect(pwm);
591 return error;
594 else {
595 if (pwm->pinentry_tries-1 != pin_try) {
596 error = set_pinentry_strings(pwm, 1);
598 if (error) {
599 pinentry_disconnect(pwm);
600 return error;
605 error = do_getpin(pwm, result);
607 if (error) {
608 if (pin_try != -1 && pin_try-- && error == EPWMD_KEY)
609 goto getpin_again;
611 if (pwm->pctx)
612 pinentry_disconnect(pwm);
614 *try_n = pin_try;
615 return error;
618 return 0;
620 #endif
622 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
624 char *result;
625 gpg_error_t error;
627 #ifndef USE_PINENTRY
628 return GPG_ERR_NOT_IMPLEMENTED;
629 #endif
631 if (!pwm || !pw || !pw->filename[0])
632 return GPG_ERR_INV_ARG;
634 close(pw->fd);
636 if (pw->error) {
637 error = pw->error;
638 goto fail;
641 error = pwmd_command(pwm, &result, "ISCACHED %s", pw->filename);
643 if (error)
644 goto fail;
646 if (pwm->filename)
647 xfree(pwm->filename);
649 pwm->filename = xstrdup(pw->filename);
650 memset(pw, 0, sizeof(pwmd_nb_status_t));
651 return 0;
653 fail:
654 memset(pw, 0, sizeof(pwmd_nb_status_t));
655 return error;
658 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
660 char buf[ASSUAN_LINELENGTH];
661 gpg_error_t error;
663 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
664 error = send_command(pwm, NULL, buf);
665 memset(buf, 0, sizeof(buf));
666 return error;
669 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
670 int nb, int timeout)
672 char *result = NULL;
673 char *password = NULL;
674 char path[PATH_MAX];
675 #ifdef USE_PINENTRY
676 int pin_try;
677 #endif
679 if (!pwm || !filename || !*filename) {
680 *error = GPG_ERR_INV_ARG;
681 return nb ? -1 : 1;
684 #ifdef USE_PINENTRY
685 pin_try = pwm->pinentry_tries - 1;
686 #endif
689 * Avoid calling pinentry if the password is cached on the server or if
690 * this is a new file.
692 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
694 if (*error)
695 return nb ? -1 : 1;
697 snprintf(path, sizeof(path), "%s/%s", result, filename);
698 pwmd_free_result(result);
700 if (access(path, R_OK) == -1) {
701 if (errno == ENOENT)
702 goto gotpassword;
704 *error = gpg_error_from_errno(errno);
705 return nb ? -1 : 1;
708 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
710 if (*error == EPWMD_CACHE_NOT_FOUND) {
711 if (pwm->passfunc) {
712 password = pwm->passfunc(pwm, pwm->passdata);
714 if (!password || !*password) {
715 *error = EPWMD_KEY;
716 return nb ? -1 : 1;
719 password = xstrdup(password);
720 goto gotpassword;
723 if (*error == EPWMD_CACHE_NOT_FOUND) {
724 #ifdef USE_PINENTRY
726 * Get the password from pinentry.
728 if (pwm->use_pinentry) {
730 * Nonblocking is wanted. fork() then return a file descriptor
731 * that the client can use to read() from.
733 if (nb) {
734 int p[2];
735 pid_t pid;
736 pwmd_nb_status_t pw;
738 if (pipe(p) == -1) {
739 *error = gpg_error_from_syserror();
740 return -1;
743 pid = fork();
745 switch (pid) {
746 case 0:
747 close(p[0]);
748 strncpy(pw.filename, filename, sizeof(pw.filename));
749 pw.fd = p[0];
751 if (timeout > 0) {
752 gpwm = pwm;
753 gtimeout = timeout;
754 gelapsed = 0;
757 getpin_nb_again:
758 *error = getpin(pwm, &password, &pin_try, 0);
760 if (*error) {
761 getpin_nb_fail:
762 if (pwm->pctx)
763 pinentry_disconnect(pwm);
765 if (gtimeout && gelapsed >= gtimeout)
766 *error = GPG_ERR_TIMEOUT;
768 pw.error = *error;
769 write(p[1], &pw, sizeof(pw));
770 close(p[1]);
771 _exit(1);
775 * Don't count the time it takes to open the file
776 * which may have many iterations.
778 signal(SIGALRM, SIG_DFL);
779 *error = do_open_command(pwm, filename, password);
781 if (timeout)
782 signal(SIGALRM, catchsig);
784 if (pwm->pctx && *error == EPWMD_BADKEY) {
785 if (pin_try-- > 0)
786 goto getpin_nb_again;
788 goto getpin_nb_fail;
791 pinentry_disconnect(pwm);
792 pw.error = 0;
793 write(p[1], &pw, sizeof(pw));
794 close(p[1]);
795 _exit(0);
796 break;
797 case -1:
798 *error = gpg_error_from_syserror();
799 close(p[0]);
800 close(p[1]);
801 return -1;
802 default:
803 break;
806 close(p[1]);
807 return p[0];
810 getpin_again:
811 *error = getpin(pwm, &password, &pin_try, 1);
813 if (*error) {
814 if (pwm->pctx)
815 pinentry_disconnect(pwm);
817 if (global_error) {
818 *error = global_error;
819 global_error = 0;
822 return 1;
825 else {
826 #endif
828 * Not using pinentry and the file was not found
829 * in the cache.
831 if (pwm->password == NULL) {
832 *error = EPWMD_KEY;
833 return 1;
836 password = pwm->password;
837 #ifdef USE_PINENTRY
839 #endif
842 else if (*error)
843 return nb ? -1 : 1;
845 gotpassword:
846 *error = do_open_command(pwm, filename, password);
849 * Keep the user defined password set with pwmd_setopt(). The password may
850 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
852 if (password && password != pwm->password)
853 xfree(password);
855 #ifdef USE_PINENTRY
856 if (pwm->pctx && *error == EPWMD_BADKEY) {
857 if (pin_try-- > 0)
858 goto getpin_again;
860 pinentry_disconnect(pwm);
861 return nb ? -1 : 1;
863 #endif
865 if (!*error) {
866 if (pwm->filename)
867 xfree(pwm->filename);
869 pwm->filename = xstrdup(filename);
873 * The file is cached or the file is a new file.
875 if (nb)
876 return *error ? -1 : -2;
878 return *error ? 1 : 0;
881 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
883 gpg_error_t error;
885 do_pwmd_open(pwm, &error, filename, 0, 0);
886 return error;
889 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
890 int timeout)
892 #ifndef USE_PINENTRY
893 *error = GPG_ERR_NOT_IMPLEMENTED;
894 return -1;
895 #else
896 return do_pwmd_open(pwm, error, filename, 1, timeout);
897 #endif
900 #ifdef USE_PINENTRY
901 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
903 int confirm = 0;
904 gpg_error_t error;
905 char *result = NULL;
906 int pin_try = -1;
908 again:
909 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
911 if (error) {
912 if (pwm->pctx)
913 pinentry_disconnect(pwm);
915 if (*password)
916 xfree(*password);
918 return error;
921 if (!confirm++) {
922 *password = result;
923 goto again;
926 if (strcmp(*password, result)) {
927 xfree(*password);
928 xfree(result);
929 pinentry_disconnect(pwm);
930 error = EPWMD_BADKEY;
931 return error;
934 xfree(result);
935 pinentry_disconnect(pwm);
936 return 0;
938 #endif
940 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
942 char buf[ASSUAN_LINELENGTH];
943 gpg_error_t error;
945 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
946 error = send_command(pwm, NULL, buf);
947 memset(&buf, 0, sizeof(buf));
948 return error;
951 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
953 gpg_error_t error;
955 #ifndef USE_PINENTRY
956 return GPG_ERR_NOT_IMPLEMENTED;
957 #endif
959 if (!pwm || !pw || !pw->filename[0])
960 return GPG_ERR_INV_ARG;
962 close(pw->fd);
964 if (pw->error) {
965 error = pw->error;
966 memset(pw, 0, sizeof(pwmd_nb_status_t));
967 return error;
970 memset(pw, 0, sizeof(pwmd_nb_status_t));
971 return 0;
974 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
976 char *result = NULL;
977 char *password = NULL;
979 if (!pwm) {
980 *error = GPG_ERR_INV_ARG;
981 return nb ? -1 : 1;
984 if (pwm->use_pinentry || pwm->passfunc) {
985 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
987 if (*error == EPWMD_CACHE_NOT_FOUND) {
988 #ifdef USE_PINENTRY
989 if (pwm->use_pinentry) {
990 if (nb) {
991 int p[2];
992 pid_t pid;
993 pwmd_nb_status_t pw;
995 if (pipe(p) == -1) {
996 *error = gpg_error_from_syserror();
997 return -1;
1000 pid = fork();
1002 switch (pid) {
1003 case 0:
1004 close(p[0]);
1005 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1006 pw.fd = p[0];
1008 do {
1009 password = NULL;
1010 *error = do_save_getpin(pwm, &password);
1011 } while (*error == EPWMD_KEY || *error == EPWMD_BADKEY);
1013 if (*error) {
1014 if (pwm->pctx)
1015 pinentry_disconnect(pwm);
1017 pw.error = *error;
1018 write(p[1], &pw, sizeof(pw));
1019 close(p[1]);
1020 _exit(1);
1023 *error = do_save_command(pwm, password);
1024 pinentry_disconnect(pwm);
1025 pw.error = *error;
1026 write(p[1], &pw, sizeof(pw));
1027 close(p[1]);
1028 _exit(0);
1029 break;
1030 case -1:
1031 *error = gpg_error_from_syserror();
1032 close(p[0]);
1033 close(p[1]);
1034 return -1;
1035 default:
1036 break;
1039 close(p[1]);
1040 return p[0];
1043 *error = do_save_getpin(pwm, &password);
1045 if (*error)
1046 return 1;
1048 else {
1049 #endif
1050 if (pwm->passfunc) {
1051 char *tmp = (*pwm->passfunc)(pwm, pwm->passdata);
1053 if (!tmp || !*tmp) {
1054 *error = EPWMD_KEY;
1055 return 1;
1058 password = xstrdup(tmp);
1060 #ifdef USE_PINENTRY
1062 #endif
1064 if (!password || !*password) {
1065 *error = EPWMD_KEY;
1066 return 1;
1069 else {
1070 if (*error)
1071 return nb ? -1 : 1;
1074 else
1075 password = pwm->password;
1077 *error = do_save_command(pwm, password);
1079 if (password && password != pwm->password)
1080 xfree(password);
1082 if (nb)
1083 return *error ? -1 : -2;
1085 return *error ? 1 : 0;
1088 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1090 #ifndef USE_PINENTRY
1091 *error = GPG_ERR_NOT_IMPLEMENTED;
1092 return -1;
1093 #else
1094 return do_pwmd_save(pwm, error, 1);
1095 #endif
1098 gpg_error_t pwmd_save(pwm_t *pwm)
1100 gpg_error_t error;
1102 do_pwmd_save(pwm, &error, 0);
1103 return error;
1106 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1108 va_list ap;
1109 #ifdef USE_PINENTRY
1110 int n = va_arg(ap, int);
1111 #endif
1112 char *arg1;
1114 if (!pwm)
1115 return GPG_ERR_INV_ARG;
1117 va_start(ap, opt);
1119 switch (opt) {
1120 case PWMD_OPTION_STATUS_FUNC:
1121 pwm->status_func = va_arg(ap, pwmd_status_fn);
1122 break;
1123 case PWMD_OPTION_STATUS_DATA:
1124 pwm->status_data = va_arg(ap, void *);
1125 break;
1126 case PWMD_OPTION_PASSWORD_FUNC:
1127 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1128 break;
1129 case PWMD_OPTION_PASSWORD_DATA:
1130 pwm->passdata = va_arg(ap, void *);
1131 break;
1132 case PWMD_OPTION_PASSWORD:
1133 arg1 = va_arg(ap, char *);
1135 if (pwm->password)
1136 xfree(pwm->password);
1138 pwm->password = xstrdup(arg1);
1139 break;
1140 #ifdef USE_PINENTRY
1141 case PWMD_OPTION_PINENTRY:
1142 n = va_arg(ap, int);
1144 if (n != 0 && n != 1) {
1145 va_end(ap);
1146 return GPG_ERR_INV_VALUE;
1149 pwm->use_pinentry = n;
1150 break;
1151 case PWMD_OPTION_PINENTRY_TRIES:
1152 n = va_arg(ap, int);
1154 if (n <= 0) {
1155 va_end(ap);
1156 return GPG_ERR_INV_VALUE;
1159 pwm->pinentry_tries = n;
1160 break;
1161 case PWMD_OPTION_PINENTRY_PATH:
1162 if (pwm->pinentry_path)
1163 xfree(pwm->pinentry_path);
1165 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1166 break;
1167 case PWMD_OPTION_PINENTRY_TTY:
1168 if (pwm->pinentry_tty)
1169 xfree(pwm->pinentry_tty);
1171 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1172 break;
1173 case PWMD_OPTION_PINENTRY_DISPLAY:
1174 if (pwm->pinentry_display)
1175 xfree(pwm->pinentry_display);
1177 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1178 break;
1179 case PWMD_OPTION_PINENTRY_TERM:
1180 if (pwm->pinentry_term)
1181 xfree(pwm->pinentry_term);
1183 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1184 break;
1185 case PWMD_OPTION_PINENTRY_TITLE:
1186 if (pwm->title)
1187 xfree(pwm->title);
1188 pwm->title = percent_escape(va_arg(ap, char *));
1189 break;
1190 case PWMD_OPTION_PINENTRY_PROMPT:
1191 if (pwm->prompt)
1192 xfree(pwm->prompt);
1193 pwm->prompt = percent_escape(va_arg(ap, char *));
1194 break;
1195 case PWMD_OPTION_PINENTRY_DESC:
1196 if (pwm->desc)
1197 xfree(pwm->desc);
1198 pwm->desc = percent_escape(va_arg(ap, char *));
1199 break;
1200 #else
1201 case PWMD_OPTION_PINENTRY:
1202 case PWMD_OPTION_PINENTRY_TRIES:
1203 case PWMD_OPTION_PINENTRY_PATH:
1204 case PWMD_OPTION_PINENTRY_TTY:
1205 case PWMD_OPTION_PINENTRY_DISPLAY:
1206 case PWMD_OPTION_PINENTRY_TERM:
1207 case PWMD_OPTION_PINENTRY_TITLE:
1208 case PWMD_OPTION_PINENTRY_PROMPT:
1209 case PWMD_OPTION_PINENTRY_DESC:
1210 return GPG_ERR_NOT_IMPLEMENTED;
1211 #endif
1212 default:
1213 va_end(ap);
1214 return GPG_ERR_NOT_IMPLEMENTED;
1217 va_end(ap);
1218 return 0;