Changed the priority from extra to optional.
[libpwmd.git] / libpwmd.c
blob824d742646da3be35bf7e308e33e0a72e2059ed0
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);
608 * Since there was input cancel any timeout.
610 alarm(0);
612 if (error) {
613 if (pin_try != -1 && pin_try-- && error == EPWMD_KEY)
614 goto getpin_again;
616 if (pwm->pctx)
617 pinentry_disconnect(pwm);
619 *try_n = pin_try;
620 return error;
623 return 0;
625 #endif
627 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
629 char *result;
630 gpg_error_t error;
632 #ifndef USE_PINENTRY
633 return GPG_ERR_NOT_IMPLEMENTED;
634 #endif
636 if (!pwm || !pw || !pw->filename[0])
637 return GPG_ERR_INV_ARG;
639 close(pw->fd);
641 if (pw->error) {
642 error = pw->error;
643 goto fail;
646 error = pwmd_command(pwm, &result, "ISCACHED %s", pw->filename);
648 if (error)
649 goto fail;
651 if (pwm->filename)
652 xfree(pwm->filename);
654 pwm->filename = xstrdup(pw->filename);
655 memset(pw, 0, sizeof(pwmd_nb_status_t));
656 return 0;
658 fail:
659 memset(pw, 0, sizeof(pwmd_nb_status_t));
660 return error;
663 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
665 char buf[ASSUAN_LINELENGTH];
666 gpg_error_t error;
668 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
669 error = send_command(pwm, NULL, buf);
670 memset(buf, 0, sizeof(buf));
671 return error;
674 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
675 int nb, int timeout)
677 char *result = NULL;
678 char *password = NULL;
679 char path[PATH_MAX];
680 #ifdef USE_PINENTRY
681 int pin_try;
682 #endif
684 if (!pwm || !filename || !*filename) {
685 *error = GPG_ERR_INV_ARG;
686 return nb ? -1 : 1;
689 #ifdef USE_PINENTRY
690 pin_try = pwm->pinentry_tries - 1;
691 #endif
694 * Avoid calling pinentry if the password is cached on the server or if
695 * this is a new file.
697 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
699 if (*error)
700 return nb ? -1 : 1;
702 snprintf(path, sizeof(path), "%s/%s", result, filename);
703 pwmd_free_result(result);
705 if (access(path, R_OK) == -1) {
706 if (errno == ENOENT)
707 goto gotpassword;
709 *error = gpg_error_from_errno(errno);
710 return nb ? -1 : 1;
713 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
715 if (*error == EPWMD_CACHE_NOT_FOUND) {
716 if (pwm->passfunc) {
717 password = pwm->passfunc(pwm, pwm->passdata);
719 if (!password || !*password) {
720 *error = EPWMD_KEY;
721 return nb ? -1 : 1;
724 password = xstrdup(password);
725 goto gotpassword;
728 if (*error == EPWMD_CACHE_NOT_FOUND) {
729 #ifdef USE_PINENTRY
731 * Get the password from pinentry.
733 if (pwm->use_pinentry) {
735 * Nonblocking is wanted. fork() then return a file descriptor
736 * that the client can use to read() from.
738 if (nb) {
739 int p[2];
740 pid_t pid;
741 pwmd_nb_status_t pw;
743 if (pipe(p) == -1) {
744 *error = gpg_error_from_syserror();
745 return -1;
748 pid = fork();
750 switch (pid) {
751 case 0:
752 close(p[0]);
753 strncpy(pw.filename, filename, sizeof(pw.filename));
754 pw.fd = p[0];
756 if (timeout > 0) {
757 gpwm = pwm;
758 gtimeout = timeout;
759 gelapsed = 0;
762 getpin_nb_again:
763 *error = getpin(pwm, &password, &pin_try, 0);
765 if (*error) {
766 getpin_nb_fail:
767 if (pwm->pctx)
768 pinentry_disconnect(pwm);
770 if (gtimeout && gelapsed >= gtimeout)
771 *error = GPG_ERR_TIMEOUT;
773 pw.error = *error;
774 write(p[1], &pw, sizeof(pw));
775 close(p[1]);
776 _exit(1);
780 * Don't count the time it takes to open the file
781 * which may have many iterations.
783 signal(SIGALRM, SIG_DFL);
784 *error = do_open_command(pwm, filename, password);
786 if (timeout)
787 signal(SIGALRM, catchsig);
789 if (pwm->pctx && *error == EPWMD_BADKEY) {
790 if (pin_try-- > 0)
791 goto getpin_nb_again;
793 goto getpin_nb_fail;
796 pinentry_disconnect(pwm);
797 pw.error = 0;
798 write(p[1], &pw, sizeof(pw));
799 close(p[1]);
800 _exit(0);
801 break;
802 case -1:
803 *error = gpg_error_from_syserror();
804 close(p[0]);
805 close(p[1]);
806 return -1;
807 default:
808 break;
811 close(p[1]);
812 return p[0];
815 getpin_again:
816 *error = getpin(pwm, &password, &pin_try, 1);
818 if (*error) {
819 if (pwm->pctx)
820 pinentry_disconnect(pwm);
822 if (global_error) {
823 *error = global_error;
824 global_error = 0;
827 return 1;
830 else {
831 #endif
833 * Not using pinentry and the file was not found
834 * in the cache.
836 if (pwm->password == NULL) {
837 *error = EPWMD_KEY;
838 return 1;
841 password = pwm->password;
842 #ifdef USE_PINENTRY
844 #endif
847 else if (*error)
848 return nb ? -1 : 1;
850 gotpassword:
851 *error = do_open_command(pwm, filename, password);
854 * Keep the user defined password set with pwmd_setopt(). The password may
855 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
857 if (password && password != pwm->password)
858 xfree(password);
860 #ifdef USE_PINENTRY
861 if (pwm->pctx && *error == EPWMD_BADKEY) {
862 if (pin_try-- > 0)
863 goto getpin_again;
865 pinentry_disconnect(pwm);
866 return nb ? -1 : 1;
868 #endif
870 if (!*error) {
871 if (pwm->filename)
872 xfree(pwm->filename);
874 pwm->filename = xstrdup(filename);
878 * The file is cached or the file is a new file.
880 if (nb)
881 return *error ? -1 : -2;
883 return *error ? 1 : 0;
886 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
888 gpg_error_t error;
890 do_pwmd_open(pwm, &error, filename, 0, 0);
891 return error;
894 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
895 int timeout)
897 #ifndef USE_PINENTRY
898 *error = GPG_ERR_NOT_IMPLEMENTED;
899 return -1;
900 #else
901 return do_pwmd_open(pwm, error, filename, 1, timeout);
902 #endif
905 #ifdef USE_PINENTRY
906 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
908 int confirm = 0;
909 gpg_error_t error;
910 char *result = NULL;
911 int pin_try = -1;
913 again:
914 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
916 if (error) {
917 if (pwm->pctx)
918 pinentry_disconnect(pwm);
920 if (*password)
921 xfree(*password);
923 return error;
926 if (!confirm++) {
927 *password = result;
928 goto again;
931 if (strcmp(*password, result)) {
932 xfree(*password);
933 xfree(result);
934 pinentry_disconnect(pwm);
935 error = EPWMD_BADKEY;
936 return error;
939 xfree(result);
940 pinentry_disconnect(pwm);
941 return 0;
943 #endif
945 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
947 char buf[ASSUAN_LINELENGTH];
948 gpg_error_t error;
950 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
951 error = send_command(pwm, NULL, buf);
952 memset(&buf, 0, sizeof(buf));
953 return error;
956 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
958 gpg_error_t error;
960 #ifndef USE_PINENTRY
961 return GPG_ERR_NOT_IMPLEMENTED;
962 #endif
964 if (!pwm || !pw || !pw->filename[0])
965 return GPG_ERR_INV_ARG;
967 close(pw->fd);
969 if (pw->error) {
970 error = pw->error;
971 memset(pw, 0, sizeof(pwmd_nb_status_t));
972 return error;
975 memset(pw, 0, sizeof(pwmd_nb_status_t));
976 return 0;
979 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
981 char *result = NULL;
982 char *password = NULL;
984 if (!pwm) {
985 *error = GPG_ERR_INV_ARG;
986 return nb ? -1 : 1;
989 if (pwm->use_pinentry || pwm->passfunc) {
990 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
992 if (*error == EPWMD_CACHE_NOT_FOUND) {
993 #ifdef USE_PINENTRY
994 if (pwm->use_pinentry) {
995 if (nb) {
996 int p[2];
997 pid_t pid;
998 pwmd_nb_status_t pw;
1000 if (pipe(p) == -1) {
1001 *error = gpg_error_from_syserror();
1002 return -1;
1005 pid = fork();
1007 switch (pid) {
1008 case 0:
1009 close(p[0]);
1010 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1011 pw.fd = p[0];
1013 do {
1014 password = NULL;
1015 *error = do_save_getpin(pwm, &password);
1016 } while (*error == EPWMD_KEY || *error == EPWMD_BADKEY);
1018 if (*error) {
1019 if (pwm->pctx)
1020 pinentry_disconnect(pwm);
1022 pw.error = *error;
1023 write(p[1], &pw, sizeof(pw));
1024 close(p[1]);
1025 _exit(1);
1028 *error = do_save_command(pwm, password);
1029 pinentry_disconnect(pwm);
1030 pw.error = *error;
1031 write(p[1], &pw, sizeof(pw));
1032 close(p[1]);
1033 _exit(0);
1034 break;
1035 case -1:
1036 *error = gpg_error_from_syserror();
1037 close(p[0]);
1038 close(p[1]);
1039 return -1;
1040 default:
1041 break;
1044 close(p[1]);
1045 return p[0];
1048 *error = do_save_getpin(pwm, &password);
1050 if (*error)
1051 return 1;
1053 else {
1054 #endif
1055 if (pwm->passfunc) {
1056 char *tmp = (*pwm->passfunc)(pwm, pwm->passdata);
1058 if (!tmp || !*tmp) {
1059 *error = EPWMD_KEY;
1060 return 1;
1063 password = xstrdup(tmp);
1065 #ifdef USE_PINENTRY
1067 #endif
1069 if (!password || !*password) {
1070 *error = EPWMD_KEY;
1071 return 1;
1074 else {
1075 if (*error)
1076 return nb ? -1 : 1;
1079 else
1080 password = pwm->password;
1082 *error = do_save_command(pwm, password);
1084 if (password && password != pwm->password)
1085 xfree(password);
1087 if (nb)
1088 return *error ? -1 : -2;
1090 return *error ? 1 : 0;
1093 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1095 #ifndef USE_PINENTRY
1096 *error = GPG_ERR_NOT_IMPLEMENTED;
1097 return -1;
1098 #else
1099 return do_pwmd_save(pwm, error, 1);
1100 #endif
1103 gpg_error_t pwmd_save(pwm_t *pwm)
1105 gpg_error_t error;
1107 do_pwmd_save(pwm, &error, 0);
1108 return error;
1111 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1113 va_list ap;
1114 #ifdef USE_PINENTRY
1115 int n = va_arg(ap, int);
1116 #endif
1117 char *arg1;
1119 if (!pwm)
1120 return GPG_ERR_INV_ARG;
1122 va_start(ap, opt);
1124 switch (opt) {
1125 case PWMD_OPTION_STATUS_FUNC:
1126 pwm->status_func = va_arg(ap, pwmd_status_fn);
1127 break;
1128 case PWMD_OPTION_STATUS_DATA:
1129 pwm->status_data = va_arg(ap, void *);
1130 break;
1131 case PWMD_OPTION_PASSWORD_FUNC:
1132 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1133 break;
1134 case PWMD_OPTION_PASSWORD_DATA:
1135 pwm->passdata = va_arg(ap, void *);
1136 break;
1137 case PWMD_OPTION_PASSWORD:
1138 arg1 = va_arg(ap, char *);
1140 if (pwm->password)
1141 xfree(pwm->password);
1143 pwm->password = xstrdup(arg1);
1144 break;
1145 #ifdef USE_PINENTRY
1146 case PWMD_OPTION_PINENTRY:
1147 n = va_arg(ap, int);
1149 if (n != 0 && n != 1) {
1150 va_end(ap);
1151 return GPG_ERR_INV_VALUE;
1154 pwm->use_pinentry = n;
1155 break;
1156 case PWMD_OPTION_PINENTRY_TRIES:
1157 n = va_arg(ap, int);
1159 if (n <= 0) {
1160 va_end(ap);
1161 return GPG_ERR_INV_VALUE;
1164 pwm->pinentry_tries = n;
1165 break;
1166 case PWMD_OPTION_PINENTRY_PATH:
1167 if (pwm->pinentry_path)
1168 xfree(pwm->pinentry_path);
1170 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1171 break;
1172 case PWMD_OPTION_PINENTRY_TTY:
1173 if (pwm->pinentry_tty)
1174 xfree(pwm->pinentry_tty);
1176 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1177 break;
1178 case PWMD_OPTION_PINENTRY_DISPLAY:
1179 if (pwm->pinentry_display)
1180 xfree(pwm->pinentry_display);
1182 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1183 break;
1184 case PWMD_OPTION_PINENTRY_TERM:
1185 if (pwm->pinentry_term)
1186 xfree(pwm->pinentry_term);
1188 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1189 break;
1190 case PWMD_OPTION_PINENTRY_TITLE:
1191 if (pwm->title)
1192 xfree(pwm->title);
1193 pwm->title = percent_escape(va_arg(ap, char *));
1194 break;
1195 case PWMD_OPTION_PINENTRY_PROMPT:
1196 if (pwm->prompt)
1197 xfree(pwm->prompt);
1198 pwm->prompt = percent_escape(va_arg(ap, char *));
1199 break;
1200 case PWMD_OPTION_PINENTRY_DESC:
1201 if (pwm->desc)
1202 xfree(pwm->desc);
1203 pwm->desc = percent_escape(va_arg(ap, char *));
1204 break;
1205 #else
1206 case PWMD_OPTION_PINENTRY:
1207 case PWMD_OPTION_PINENTRY_TRIES:
1208 case PWMD_OPTION_PINENTRY_PATH:
1209 case PWMD_OPTION_PINENTRY_TTY:
1210 case PWMD_OPTION_PINENTRY_DISPLAY:
1211 case PWMD_OPTION_PINENTRY_TERM:
1212 case PWMD_OPTION_PINENTRY_TITLE:
1213 case PWMD_OPTION_PINENTRY_PROMPT:
1214 case PWMD_OPTION_PINENTRY_DESC:
1215 return GPG_ERR_NOT_IMPLEMENTED;
1216 #endif
1217 default:
1218 va_end(ap);
1219 return GPG_ERR_NOT_IMPLEMENTED;
1222 va_end(ap);
1223 return 0;