Use the new EXISTS protocol command to determine whether a file is new
[libpwmd.git] / libpwmd.c
blobf6659cfa3c656f6f43f547c9855c64d2328e1f80
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 #include "mem.h"
55 #include "types.h"
57 #ifdef USE_PINENTRY
58 #define PINENTRY_PATH "/usr/bin/pinentry"
59 static pwm_t *gpwm;
60 static int gelapsed, gtimeout;
61 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd);
62 #endif
64 static gpg_error_t global_error;
66 const char *pwmd_strerror(gpg_error_t error)
68 gpg_err_code_t code = gpg_err_code(error);
70 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
71 switch (code) {
72 case GPG_ERR_USER_1:
73 default:
74 return N_("Unknown error");
75 case GPG_ERR_USER_2:
76 return N_("No cache slots available");
77 case GPG_ERR_USER_3:
78 return N_("Element not found");
79 case GPG_ERR_USER_4:
80 return N_("Trailing element");
81 case GPG_ERR_USER_5:
82 return N_("Invalid character in element");
83 case GPG_ERR_USER_6:
84 return N_("Empty");
85 case GPG_ERR_USER_7:
86 return N_("Will not overwrite existing account");
87 case GPG_ERR_USER_8:
88 return N_("File not found");
89 case GPG_ERR_USER_9:
90 return N_("No file is open");
91 case GPG_ERR_USER_10:
92 return N_("General LibXML error");
93 case GPG_ERR_USER_11:
94 return N_("File not found in cache");
95 case GPG_ERR_USER_12:
96 return N_("Attribute not found");
97 case GPG_ERR_USER_13:
98 return N_("Invalid filename or link");
99 case GPG_ERR_USER_14:
100 return N_("File modified");
104 return gpg_strerror(error);
107 gpg_error_t pwmd_init()
109 #ifdef ENABLE_NLS
110 bindtextdomain("libpwmd", LOCALEDIR);
111 #endif
112 gpg_err_init();
113 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
114 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
115 return 0;
118 pwm_t *pwmd_connect(const char *path, gpg_error_t *error)
120 pwm_t *pwm = NULL;
121 char *socketpath = NULL;
122 time_t now;
123 struct passwd *pw;
124 assuan_context_t ctx;
125 int rc;
127 if (!path) {
128 pw = getpwuid(getuid());
129 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
130 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
132 else
133 socketpath = xstrdup(path);
135 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
136 xfree(socketpath);
138 if (rc) {
139 *error = rc;
140 return NULL;
143 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
144 *error = gpg_error_from_errno(errno);
145 assuan_disconnect(ctx);
146 return NULL;
149 pwm->ctx = ctx;
150 #ifdef USE_PINENTRY
151 pwm->pid = -1;
152 pwm->pinentry_tries = 3;
153 #endif
154 time(&now);
155 srandom(now);
156 return pwm;
159 void pwmd_close(pwm_t *pwm)
161 if (!pwm)
162 return;
164 if (pwm->ctx)
165 assuan_disconnect(pwm->ctx);
167 if (pwm->password)
168 xfree(pwm->password);
170 if (pwm->title)
171 xfree(pwm->title);
173 if (pwm->desc)
174 xfree(pwm->desc);
176 if (pwm->prompt)
177 xfree(pwm->prompt);
179 if (pwm->pinentry_tty)
180 xfree(pwm->pinentry_tty);
182 if (pwm->pinentry_display)
183 xfree(pwm->pinentry_display);
185 if (pwm->pinentry_term)
186 xfree(pwm->pinentry_term);
188 if (pwm->filename)
189 xfree(pwm->filename);
191 xfree(pwm);
194 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
196 membuf_t *mem = (membuf_t *)data;
197 void *p;
199 if (!buffer)
200 return 0;
202 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
203 return 1;
205 mem->buf = p;
206 memcpy((char *)mem->buf + mem->len, buffer, len);
207 mem->len += len;
208 return 0;
211 static int inquire_cb(void *data, const char *keyword)
213 struct inquire_s *inquire = (struct inquire_s *)data;
215 return assuan_send_data(inquire->ctx, inquire->buf, inquire->len);
218 void pwmd_free_result(void *data)
220 xfree(data);
223 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
224 char **result, const char *cmd)
226 membuf_t data;
227 gpg_error_t rc;
229 data.len = 0;
230 data.buf = NULL;
233 * This is needed because assuan only accepts 1000 byte command strings.
234 * If the line is more than this the command will fail. So we use an
235 * INQUIRE on the server which waits for an END that assuan_transact()
236 * fulfills.
238 * Other commands shouldn't need the INQUIRE. Let me know if you have an
239 * element path that's greater than 1000 bytes and I'll fix it.
241 if (strncasecmp(cmd, "STORE", 5) == 0) {
242 const char *p = cmd + 5;
243 struct inquire_s *inq = (struct inquire_s *)xmalloc(sizeof(struct inquire_s));
245 if (*p == ' ')
246 p++;
248 inq->ctx = ctx;
249 inq->buf = p;
250 inq->len = strlen(p);
251 rc = assuan_transact(ctx, "STORE", mem_realloc_cb, &data, inquire_cb,
252 inq, pwm->status_func, pwm->status_data);
253 xfree(inq);
255 else {
256 #ifdef USE_PINENTRY
258 * Ignore any status callback function for pinentry.
260 if (ctx == pwm->pctx)
261 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, NULL, NULL, NULL, NULL);
262 else
263 #endif
264 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, NULL, NULL,
265 pwm->status_func, pwm->status_data);
268 if (rc) {
269 if (data.buf) {
270 xfree(data.buf);
271 data.buf = NULL;
274 else {
275 if (data.buf) {
276 mem_realloc_cb(&data, "", 1);
277 *result = (char *)data.buf;
281 return gpg_err_code(rc);
284 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
286 #ifndef USE_PINENTRY
287 return GPG_ERR_NOT_IMPLEMENTED;
288 #endif
290 if (!pwm || pwm->pid == -1)
291 return GPG_ERR_INV_ARG;
293 if (kill(pwm->pid, 0) == 0) {
294 if (kill(pwm->pid, SIGTERM) == -1) {
295 if (kill(pwm->pid, SIGKILL) == -1)
296 return gpg_error_from_errno(errno);
299 global_error = GPG_ERR_TIMEOUT;
301 else
302 return gpg_error_from_errno(errno);
304 return 0;
307 #ifdef USE_PINENTRY
308 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
310 char *buf;
311 char tmp[ASSUAN_LINELENGTH];
312 gpg_error_t error;
314 if (!pwm->title)
315 pwm->title = xstrdup(N_("LibPWMD"));
317 if (!pwm->prompt)
318 pwm->prompt = xstrdup(N_("Password:"));
320 if (!pwm->desc && !which)
321 pwm->desc = xstrdup(N_("Enter a password."));
323 if (which == 1) {
324 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
325 buf = xstrdup(tmp);
327 else if (which == 2) {
328 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
329 buf = xstrdup(tmp);
331 else {
332 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
333 sprintf(buf, "SETERROR %s", pwm->desc);
336 error = pinentry_command(pwm, NULL, buf);
337 xfree(buf);
339 if (error)
340 return error;
342 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
343 sprintf(buf, "SETPROMPT %s", pwm->prompt);
344 error = pinentry_command(pwm, NULL, buf);
345 xfree(buf);
347 if (error)
348 return error;
350 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
351 sprintf(buf, "SETDESC %s", pwm->title);
352 error = pinentry_command(pwm, NULL, buf);
353 xfree(buf);
354 return error;
357 static void update_pinentry_settings(pwm_t *pwm)
359 FILE *fp;
360 char buf[LINE_MAX];
361 struct passwd *pw = getpwuid(getuid());
362 char *p;
364 snprintf(buf, sizeof(buf), "%s/.pwmd/env", pw->pw_dir);
366 if ((fp = fopen(buf, "r")) == NULL)
367 return;
369 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
370 char name[32], val[256];
372 if (sscanf(p, "%[a-zA-Z]=%s", name, val) != 2)
373 continue;
375 if (strcasecmp(name, "TTY") == 0) {
376 if (!pwm->pinentry_tty) {
377 xfree(pwm->pinentry_tty);
378 pwm->pinentry_tty = xstrdup(val);
381 else if (strcasecmp(name, "TERM") == 0) {
382 if (!pwm->pinentry_term) {
383 xfree(pwm->pinentry_term);
384 pwm->pinentry_term = xstrdup(val);
387 else if (strcasecmp(name, "DISPLAY") == 0) {
388 if (!pwm->pinentry_display) {
389 xfree(pwm->pinentry_display);
390 pwm->pinentry_display = xstrdup(val);
395 fclose(fp);
398 static gpg_error_t launch_pinentry(pwm_t *pwm)
400 int rc;
401 assuan_context_t ctx;
402 int child_list[] = {-1};
403 char *display = getenv("DISPLAY");
404 const char *argv[6];
405 int have_display = 0;
406 char *tty = NULL;
408 update_pinentry_settings(pwm);
410 if (pwm->pinentry_display || display)
411 have_display = 1;
412 else {
413 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
415 if (!tty)
416 return gpg_error_from_errno(errno);
419 if (!display && !tty)
420 return GPG_ERR_ENOTTY;
422 argv[0] = "pinentry";
423 argv[1] = have_display ? "--display" : "--ttyname";
424 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
425 argv[3] = NULL;
427 if (!have_display) {
428 argv[3] = "--ttytype";
429 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
430 argv[5] = NULL;
433 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
435 if (rc)
436 return rc;
438 pwm->pid = assuan_get_pid(ctx);
439 pwm->pctx = ctx;
440 return set_pinentry_strings(pwm, 0);
443 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
445 gpg_error_t n;
447 if (!pwm->pctx) {
448 n = launch_pinentry(pwm);
450 if (n)
451 return n;
454 return assuan_command(pwm, pwm->pctx, result, cmd);
457 static void pinentry_disconnect(pwm_t *pwm)
459 if (pwm->pctx)
460 assuan_disconnect(pwm->pctx);
462 pwm->pctx = NULL;
463 pwm->pid = -1;
467 * Only called from a child process.
469 static void catchsig(int sig)
471 switch (sig) {
472 case SIGALRM:
473 if (gelapsed++ >= gtimeout) {
474 global_error = pwmd_terminate_pinentry(gpwm);
476 if (!global_error)
477 global_error = GPG_ERR_TIMEOUT;
479 break;
482 alarm(1);
483 break;
484 default:
485 break;
489 static char *percent_escape(const char *atext)
491 const unsigned char *s;
492 int len = strlen(atext) * 3 + 1;
493 char *buf = (char *)xmalloc(len), *p = buf;
495 if (!buf)
496 return NULL;
498 for (s=(const unsigned char *)atext; *s; s++) {
499 if (*s < ' ') {
500 sprintf (p, "%%%02X", *s);
501 p += 3;
503 else
504 *p++ = *s;
507 *p = 0;
508 return buf;
510 #endif
512 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
514 if (!cmd)
515 return GPG_ERR_INV_ARG;
517 return assuan_command(pwm, pwm->ctx, result, cmd);
521 * Avoid sending the BYE command here. libassuan will close the file
522 * descriptor and release the assuan context. Use pwmd_close() instead.
524 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
526 va_list ap;
527 char *buf;
528 size_t len;
529 gpg_error_t error;
531 if (!pwm || !cmd)
532 return GPG_ERR_INV_ARG;
534 *result = NULL;
535 va_start(ap, cmd);
537 * C99
539 len = vsnprintf(NULL, 0, cmd, ap);
540 buf = (char *)xmalloc(len + 1);
541 len = vsnprintf(buf, len + 1, cmd, ap);
542 va_end(ap);
543 error = send_command(pwm, result, buf);
544 xfree(buf);
545 return error;
548 #ifdef USE_PINENTRY
549 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
551 gpg_error_t error;
553 if (gtimeout) {
554 signal(SIGALRM, catchsig);
555 alarm(1);
558 *result = NULL;
559 error = pinentry_command(pwm, result, "GETPIN");
561 if (error == GPG_ERR_ASS_CANCELED)
562 return error;
564 if (!*result)
565 return EPWMD_KEY;
567 return 0;
570 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
572 int pin_try = *try_n;
573 gpg_error_t error;
575 getpin_again:
576 *try_n = pin_try;
578 if (pin_try == -1) {
579 error = set_pinentry_strings(pwm, which);
581 if (error) {
582 pinentry_disconnect(pwm);
583 return error;
586 else {
587 if (pwm->pinentry_tries-1 != pin_try) {
588 error = set_pinentry_strings(pwm, 1);
590 if (error) {
591 pinentry_disconnect(pwm);
592 return error;
597 error = do_getpin(pwm, result);
599 if (error) {
600 if (pin_try != -1 && pin_try-- && error == EPWMD_KEY)
601 goto getpin_again;
603 if (pwm->pctx)
604 pinentry_disconnect(pwm);
606 *try_n = pin_try;
607 return error;
610 return 0;
612 #endif
614 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
616 char *result;
617 gpg_error_t error;
619 #ifndef USE_PINENTRY
620 return GPG_ERR_NOT_IMPLEMENTED;
621 #endif
623 if (!pwm || !pw || !pw->filename[0])
624 return GPG_ERR_INV_ARG;
626 close(pw->fd);
628 if (pw->error) {
629 error = pw->error;
630 goto fail;
633 error = pwmd_command(pwm, &result, "ISCACHED %s", pw->filename);
635 if (error)
636 goto fail;
638 if (pwm->filename)
639 xfree(pwm->filename);
641 pwm->filename = xstrdup(pw->filename);
642 memset(pw, 0, sizeof(pwmd_nb_status_t));
643 return 0;
645 fail:
646 memset(pw, 0, sizeof(pwmd_nb_status_t));
647 return error;
650 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
652 char buf[ASSUAN_LINELENGTH];
653 gpg_error_t error;
655 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
656 error = send_command(pwm, NULL, buf);
657 memset(buf, 0, sizeof(buf));
658 return error;
661 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
662 int nb, int timeout)
664 char *result = NULL;
665 char *password = NULL;
666 #ifdef USE_PINENTRY
667 int pin_try;
668 #endif
670 if (!pwm || !filename || !*filename) {
671 *error = GPG_ERR_INV_ARG;
672 return nb ? -1 : 1;
675 #ifdef USE_PINENTRY
676 pin_try = pwm->pinentry_tries - 1;
677 #endif
680 * Avoid calling pinentry if the password is cached on the server or if
681 * this is a new file.
683 *error = pwmd_command(pwm, &result, "EXISTS %s", filename);
685 if (!*error)
686 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
687 else {
688 if (gpg_err_code(*error) != gpg_err_code_from_errno(ENOENT))
689 return nb ? -1 : 1;
691 goto gotpassword;
694 if (*error == EPWMD_CACHE_NOT_FOUND) {
695 if (pwm->passfunc) {
696 password = pwm->passfunc(pwm, pwm->passdata);
698 if (!password || !*password) {
699 *error = EPWMD_KEY;
700 return nb ? -1 : 1;
703 password = xstrdup(password);
704 goto gotpassword;
707 if (*error == EPWMD_CACHE_NOT_FOUND) {
708 #ifdef USE_PINENTRY
710 * Get the password from pinentry.
712 if (pwm->use_pinentry) {
714 * Nonblocking is wanted. fork() then return a file descriptor
715 * that the client can use to read() from.
717 if (nb) {
718 int p[2];
719 pid_t pid;
720 pwmd_nb_status_t pw;
722 if (pipe(p) == -1) {
723 *error = gpg_error_from_syserror();
724 return -1;
727 pid = fork();
729 switch (pid) {
730 case 0:
731 close(p[0]);
732 strncpy(pw.filename, filename, sizeof(pw.filename));
733 pw.fd = p[0];
735 if (timeout > 0) {
736 gpwm = pwm;
737 gtimeout = timeout;
738 gelapsed = 0;
741 getpin_nb_again:
742 *error = getpin(pwm, &password, &pin_try, 0);
744 if (*error) {
745 getpin_nb_fail:
746 if (pwm->pctx)
747 pinentry_disconnect(pwm);
749 if (gtimeout && gelapsed >= gtimeout)
750 *error = GPG_ERR_TIMEOUT;
752 pw.error = *error;
753 write(p[1], &pw, sizeof(pw));
754 close(p[1]);
755 _exit(1);
759 * Don't count the time it takes to open the file
760 * which may have many iterations.
762 signal(SIGALRM, SIG_DFL);
763 *error = do_open_command(pwm, filename, password);
765 if (timeout)
766 signal(SIGALRM, catchsig);
768 if (pwm->pctx && *error == EPWMD_BADKEY) {
769 if (pin_try-- > 0)
770 goto getpin_nb_again;
772 goto getpin_nb_fail;
775 pinentry_disconnect(pwm);
776 pw.error = 0;
777 write(p[1], &pw, sizeof(pw));
778 close(p[1]);
779 _exit(0);
780 break;
781 case -1:
782 *error = gpg_error_from_syserror();
783 close(p[0]);
784 close(p[1]);
785 return -1;
786 default:
787 break;
790 close(p[1]);
791 return p[0];
794 getpin_again:
795 *error = getpin(pwm, &password, &pin_try, 1);
797 if (*error) {
798 if (pwm->pctx)
799 pinentry_disconnect(pwm);
801 if (global_error) {
802 *error = global_error;
803 global_error = 0;
806 return 1;
809 else {
810 #endif
812 * Not using pinentry and the file was not found
813 * in the cache.
815 if (pwm->password == NULL) {
816 *error = EPWMD_KEY;
817 return 1;
820 password = pwm->password;
821 #ifdef USE_PINENTRY
823 #endif
826 else if (*error)
827 return nb ? -1 : 1;
829 gotpassword:
830 *error = do_open_command(pwm, filename, password);
833 * Keep the user defined password set with pwmd_setopt(). The password may
834 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
836 if (password && password != pwm->password)
837 xfree(password);
839 #ifdef USE_PINENTRY
840 if (pwm->pctx && *error == EPWMD_BADKEY) {
841 if (pin_try-- > 0)
842 goto getpin_again;
844 pinentry_disconnect(pwm);
845 return nb ? -1 : 1;
847 #endif
849 if (!*error) {
850 if (pwm->filename)
851 xfree(pwm->filename);
853 pwm->filename = xstrdup(filename);
857 * The file is cached or the file is a new file.
859 if (nb)
860 return *error ? -1 : -2;
862 return *error ? 1 : 0;
865 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
867 gpg_error_t error;
869 do_pwmd_open(pwm, &error, filename, 0, 0);
870 return error;
873 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
874 int timeout)
876 #ifndef USE_PINENTRY
877 *error = GPG_ERR_NOT_IMPLEMENTED;
878 return -1;
879 #else
880 return do_pwmd_open(pwm, error, filename, 1, timeout);
881 #endif
884 #ifdef USE_PINENTRY
885 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
887 int confirm = 0;
888 gpg_error_t error;
889 char *result = NULL;
890 int pin_try = -1;
892 again:
893 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
895 if (error) {
896 if (pwm->pctx)
897 pinentry_disconnect(pwm);
899 if (*password)
900 xfree(*password);
902 return error;
905 if (!confirm++) {
906 *password = result;
907 goto again;
910 if (strcmp(*password, result)) {
911 xfree(*password);
912 xfree(result);
913 pinentry_disconnect(pwm);
914 error = EPWMD_BADKEY;
915 return error;
918 xfree(result);
919 pinentry_disconnect(pwm);
920 return 0;
922 #endif
924 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
926 char buf[ASSUAN_LINELENGTH];
927 gpg_error_t error;
929 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
930 error = send_command(pwm, NULL, buf);
931 memset(&buf, 0, sizeof(buf));
932 return error;
935 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
937 gpg_error_t error;
939 #ifndef USE_PINENTRY
940 return GPG_ERR_NOT_IMPLEMENTED;
941 #endif
943 if (!pwm || !pw || !pw->filename[0])
944 return GPG_ERR_INV_ARG;
946 close(pw->fd);
948 if (pw->error) {
949 error = pw->error;
950 memset(pw, 0, sizeof(pwmd_nb_status_t));
951 return error;
954 memset(pw, 0, sizeof(pwmd_nb_status_t));
955 return 0;
958 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
960 char *result = NULL;
961 char *password = NULL;
963 if (!pwm) {
964 *error = GPG_ERR_INV_ARG;
965 return nb ? -1 : 1;
968 if (pwm->use_pinentry || pwm->passfunc) {
969 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
971 if (*error == EPWMD_CACHE_NOT_FOUND) {
972 #ifdef USE_PINENTRY
973 if (pwm->use_pinentry) {
974 if (nb) {
975 int p[2];
976 pid_t pid;
977 pwmd_nb_status_t pw;
979 if (pipe(p) == -1) {
980 *error = gpg_error_from_syserror();
981 return -1;
984 pid = fork();
986 switch (pid) {
987 case 0:
988 close(p[0]);
989 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
990 pw.fd = p[0];
992 do {
993 password = NULL;
994 *error = do_save_getpin(pwm, &password);
995 } while (*error == EPWMD_KEY || *error == EPWMD_BADKEY);
997 if (*error) {
998 if (pwm->pctx)
999 pinentry_disconnect(pwm);
1001 pw.error = *error;
1002 write(p[1], &pw, sizeof(pw));
1003 close(p[1]);
1004 _exit(1);
1007 *error = do_save_command(pwm, password);
1008 pinentry_disconnect(pwm);
1009 pw.error = *error;
1010 write(p[1], &pw, sizeof(pw));
1011 close(p[1]);
1012 _exit(0);
1013 break;
1014 case -1:
1015 *error = gpg_error_from_syserror();
1016 close(p[0]);
1017 close(p[1]);
1018 return -1;
1019 default:
1020 break;
1023 close(p[1]);
1024 return p[0];
1027 *error = do_save_getpin(pwm, &password);
1029 if (*error)
1030 return 1;
1032 else {
1033 #endif
1034 if (pwm->passfunc) {
1035 char *tmp = (*pwm->passfunc)(pwm, pwm->passdata);
1037 if (!tmp || !*tmp) {
1038 *error = EPWMD_KEY;
1039 return 1;
1042 password = xstrdup(tmp);
1044 #ifdef USE_PINENTRY
1046 #endif
1048 if (!password || !*password) {
1049 *error = EPWMD_KEY;
1050 return 1;
1053 else {
1054 if (*error)
1055 return nb ? -1 : 1;
1058 else
1059 password = pwm->password;
1061 *error = do_save_command(pwm, password);
1063 if (password && password != pwm->password)
1064 xfree(password);
1066 if (nb)
1067 return *error ? -1 : -2;
1069 return *error ? 1 : 0;
1072 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1074 #ifndef USE_PINENTRY
1075 *error = GPG_ERR_NOT_IMPLEMENTED;
1076 return -1;
1077 #else
1078 return do_pwmd_save(pwm, error, 1);
1079 #endif
1082 gpg_error_t pwmd_save(pwm_t *pwm)
1084 gpg_error_t error;
1086 do_pwmd_save(pwm, &error, 0);
1087 return error;
1090 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1092 va_list ap;
1093 #ifdef USE_PINENTRY
1094 int n = va_arg(ap, int);
1095 #endif
1096 char *arg1;
1098 if (!pwm)
1099 return GPG_ERR_INV_ARG;
1101 va_start(ap, opt);
1103 switch (opt) {
1104 case PWMD_OPTION_STATUS_FUNC:
1105 pwm->status_func = va_arg(ap, pwmd_status_fn);
1106 break;
1107 case PWMD_OPTION_STATUS_DATA:
1108 pwm->status_data = va_arg(ap, void *);
1109 break;
1110 case PWMD_OPTION_PASSWORD_FUNC:
1111 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1112 break;
1113 case PWMD_OPTION_PASSWORD_DATA:
1114 pwm->passdata = va_arg(ap, void *);
1115 break;
1116 case PWMD_OPTION_PASSWORD:
1117 arg1 = va_arg(ap, char *);
1119 if (pwm->password)
1120 xfree(pwm->password);
1122 pwm->password = xstrdup(arg1);
1123 break;
1124 #ifdef USE_PINENTRY
1125 case PWMD_OPTION_PINENTRY:
1126 n = va_arg(ap, int);
1128 if (n != 0 && n != 1) {
1129 va_end(ap);
1130 return GPG_ERR_INV_VALUE;
1133 pwm->use_pinentry = n;
1134 break;
1135 case PWMD_OPTION_PINENTRY_TRIES:
1136 n = va_arg(ap, int);
1138 if (n <= 0) {
1139 va_end(ap);
1140 return GPG_ERR_INV_VALUE;
1143 pwm->pinentry_tries = n;
1144 break;
1145 case PWMD_OPTION_PINENTRY_PATH:
1146 if (pwm->pinentry_path)
1147 xfree(pwm->pinentry_path);
1149 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1150 break;
1151 case PWMD_OPTION_PINENTRY_TTY:
1152 if (pwm->pinentry_tty)
1153 xfree(pwm->pinentry_tty);
1155 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1156 break;
1157 case PWMD_OPTION_PINENTRY_DISPLAY:
1158 if (pwm->pinentry_display)
1159 xfree(pwm->pinentry_display);
1161 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1162 break;
1163 case PWMD_OPTION_PINENTRY_TERM:
1164 if (pwm->pinentry_term)
1165 xfree(pwm->pinentry_term);
1167 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1168 break;
1169 case PWMD_OPTION_PINENTRY_TITLE:
1170 if (pwm->title)
1171 xfree(pwm->title);
1172 pwm->title = percent_escape(va_arg(ap, char *));
1173 break;
1174 case PWMD_OPTION_PINENTRY_PROMPT:
1175 if (pwm->prompt)
1176 xfree(pwm->prompt);
1177 pwm->prompt = percent_escape(va_arg(ap, char *));
1178 break;
1179 case PWMD_OPTION_PINENTRY_DESC:
1180 if (pwm->desc)
1181 xfree(pwm->desc);
1182 pwm->desc = percent_escape(va_arg(ap, char *));
1183 break;
1184 #else
1185 case PWMD_OPTION_PINENTRY:
1186 case PWMD_OPTION_PINENTRY_TRIES:
1187 case PWMD_OPTION_PINENTRY_PATH:
1188 case PWMD_OPTION_PINENTRY_TTY:
1189 case PWMD_OPTION_PINENTRY_DISPLAY:
1190 case PWMD_OPTION_PINENTRY_TERM:
1191 case PWMD_OPTION_PINENTRY_TITLE:
1192 case PWMD_OPTION_PINENTRY_PROMPT:
1193 case PWMD_OPTION_PINENTRY_DESC:
1194 return GPG_ERR_NOT_IMPLEMENTED;
1195 #endif
1196 default:
1197 va_end(ap);
1198 return GPG_ERR_NOT_IMPLEMENTED;
1201 va_end(ap);
1202 return 0;