Added pwmc command line option -t. When opening a file and a password
[libpwmd.git] / libpwmd.c
blob9d4d21be58704f261555c2b1d1795dca849d6c1f
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 <libpwmd.h>
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
42 #ifdef HAVE_ASSUAN_H
43 #include <assuan.h>
44 #endif
46 #ifdef HAVE_SETLOCALE
47 #include <locale.h>
48 #endif
50 #include "mem.h"
51 #include "types.h"
53 #ifdef USE_PINENTRY
54 #define PINENTRY_PATH "/usr/bin/pinentry"
55 static pwm_t *gpwm;
56 static int gelapsed, gtimeout;
57 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd);
58 #endif
60 const char *pwmd_strerror(gpg_error_t error)
62 gpg_err_code_t code = gpg_err_code(error);
64 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
65 switch (code) {
66 case GPG_ERR_USER_1:
67 default:
68 return "Unknown error";
69 case GPG_ERR_USER_2:
70 return "No cache slots available";
71 case GPG_ERR_USER_3:
72 return "Element not found";
73 case GPG_ERR_USER_4:
74 return "Trailing element";
75 case GPG_ERR_USER_5:
76 return "Invalid character in element";
77 case GPG_ERR_USER_6:
78 return "Empty";
79 case GPG_ERR_USER_7:
80 return "Will not overwrite existing account";
81 case GPG_ERR_USER_8:
82 return "File not found";
83 case GPG_ERR_USER_9:
84 return "No file is open";
85 case GPG_ERR_USER_10:
86 return "General LibXML error";
87 case GPG_ERR_USER_11:
88 return "File not found in cache";
89 case GPG_ERR_USER_12:
90 return "Attribute not found";
91 case GPG_ERR_USER_13:
92 return "Invalid filename or link";
93 case GPG_ERR_USER_14:
94 return "File modified";
98 return gpg_strerror(error);
101 gpg_error_t pwmd_init()
103 gpg_err_init();
104 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
105 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
106 return 0;
109 pwm_t *pwmd_connect(const char *path, gpg_error_t *error)
111 pwm_t *pwm = NULL;
112 char *socketpath = NULL;
113 time_t now;
114 struct passwd *pw;
115 assuan_context_t ctx;
116 int rc;
118 if (!path) {
119 pw = getpwuid(getuid());
120 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
121 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
123 else
124 socketpath = xstrdup(path);
126 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
127 xfree(socketpath);
129 if (rc) {
130 *error = rc;
131 return NULL;
134 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
135 *error = gpg_error_from_errno(errno);
136 assuan_disconnect(ctx);
137 return NULL;
140 pwm->ctx = ctx;
141 #ifdef USE_PINENTRY
142 pwm->pid = -1;
143 pwm->pinentry_tries = 3;
144 #endif
145 time(&now);
146 srandom(now);
147 return pwm;
150 void pwmd_close(pwm_t *pwm)
152 if (!pwm)
153 return;
155 if (pwm->ctx)
156 assuan_disconnect(pwm->ctx);
158 if (pwm->password)
159 xfree(pwm->password);
161 if (pwm->title)
162 xfree(pwm->title);
164 if (pwm->desc)
165 xfree(pwm->desc);
167 if (pwm->prompt)
168 xfree(pwm->prompt);
170 if (pwm->pinentry_tty)
171 xfree(pwm->pinentry_tty);
173 if (pwm->pinentry_display)
174 xfree(pwm->pinentry_display);
176 if (pwm->pinentry_term)
177 xfree(pwm->pinentry_term);
179 if (pwm->filename)
180 xfree(pwm->filename);
182 xfree(pwm);
185 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
187 membuf_t *mem = (membuf_t *)data;
188 void *p;
190 if (!buffer)
191 return 0;
193 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
194 return 1;
196 mem->buf = p;
197 memcpy((char *)mem->buf + mem->len, buffer, len);
198 mem->len += len;
199 return 0;
202 static int inquire_cb(void *data, const char *keyword)
204 struct inquire_s *inquire = (struct inquire_s *)data;
206 return assuan_send_data(inquire->ctx, inquire->buf, inquire->len);
209 void pwmd_free_result(void *data)
211 xfree(data);
214 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
215 char **result, const char *cmd)
217 membuf_t data;
218 gpg_error_t rc;
220 data.len = 0;
221 data.buf = NULL;
224 * This is needed because assuan only accepts 1000 byte command strings.
225 * If the line is more than this the command will fail. So we use an
226 * INQUIRE on the server which waits for an END that assuan_transact()
227 * fulfills.
229 * Other commands shouldn't need the INQUIRE. Let me know if you have an
230 * element path that's greater than 1000 bytes and I'll fix it.
232 if (strncasecmp(cmd, "STORE", 5) == 0) {
233 const char *p = cmd + 5;
234 struct inquire_s *inq = (struct inquire_s *)xmalloc(sizeof(struct inquire_s));
236 if (*p == ' ')
237 p++;
239 inq->ctx = ctx;
240 inq->buf = p;
241 inq->len = strlen(p);
242 rc = assuan_transact(ctx, "STORE", mem_realloc_cb, &data, inquire_cb,
243 inq, pwm->status_func, pwm->status_data);
244 xfree(inq);
246 else {
247 #ifdef USE_PINENTRY
249 * Ignore any status callback function for pinentry.
251 if (ctx == pwm->pctx)
252 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, NULL, NULL, NULL, NULL);
253 else
254 #endif
255 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, NULL, NULL,
256 pwm->status_func, pwm->status_data);
259 if (rc) {
260 if (data.buf) {
261 xfree(data.buf);
262 data.buf = NULL;
265 else {
266 if (data.buf) {
267 mem_realloc_cb(&data, "", 1);
268 *result = (char *)data.buf;
272 return gpg_err_code(rc);
275 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
277 #ifndef USE_PINENTRY
278 return GPG_ERR_NOT_IMPLEMENTED;
279 #endif
281 if (!pwm || pwm->pid == -1)
282 return GPG_ERR_INV_ARG;
284 if (kill(pwm->pid, 0) == 0) {
285 if (kill(pwm->pid, SIGTERM) == -1) {
286 if (kill(pwm->pid, SIGKILL) == -1)
287 return gpg_error_from_errno(errno);
290 else
291 return gpg_error_from_errno(errno);
293 return 0;
296 #ifdef USE_PINENTRY
297 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
299 char *buf;
300 gpg_error_t error;
302 if (!pwm->title)
303 pwm->title = xstrdup("LibPWMD");
305 if (!pwm->prompt)
306 pwm->prompt = xstrdup("Password:");
308 if (!pwm->desc && !which)
309 pwm->desc = xstrdup("Enter a password.");
311 if (which == 1)
312 buf = xstrdup("SETERROR Invalid password, please try again.");
313 else if (which == 2)
314 buf = xstrdup("SETERROR Please type the password again for confirmation.");
315 else {
316 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
317 sprintf(buf, "SETERROR %s", pwm->desc);
320 error = pinentry_command(pwm, NULL, buf);
321 xfree(buf);
323 if (error)
324 return error;
326 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
327 sprintf(buf, "SETPROMPT %s", pwm->prompt);
328 error = pinentry_command(pwm, NULL, buf);
329 xfree(buf);
331 if (error)
332 return error;
334 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
335 sprintf(buf, "SETDESC %s", pwm->title);
336 error = pinentry_command(pwm, NULL, buf);
337 xfree(buf);
338 return error;
341 static void update_pinentry_settings(pwm_t *pwm)
343 FILE *fp;
344 char buf[LINE_MAX];
345 struct passwd *pw = getpwuid(getuid());
346 char *p;
348 snprintf(buf, sizeof(buf), "%s/.pwmd/env", pw->pw_dir);
350 if ((fp = fopen(buf, "r")) == NULL)
351 return;
353 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
354 char name[32], val[256];
356 if (sscanf(p, "%[a-zA-Z]=%s", name, val) != 2)
357 continue;
359 if (strcasecmp(name, "TTY") == 0) {
360 if (!pwm->pinentry_tty) {
361 xfree(pwm->pinentry_tty);
362 pwm->pinentry_tty = xstrdup(val);
365 else if (strcasecmp(name, "TERM") == 0) {
366 if (!pwm->pinentry_term) {
367 xfree(pwm->pinentry_term);
368 pwm->pinentry_term = xstrdup(val);
371 else if (strcasecmp(name, "DISPLAY") == 0) {
372 if (!pwm->pinentry_display) {
373 xfree(pwm->pinentry_display);
374 pwm->pinentry_display = xstrdup(val);
379 fclose(fp);
382 static gpg_error_t launch_pinentry(pwm_t *pwm)
384 int rc;
385 assuan_context_t ctx;
386 int child_list[] = {-1};
387 char *display = getenv("DISPLAY");
388 const char *argv[6];
389 int have_display = 0;
390 char *tty = NULL;
392 update_pinentry_settings(pwm);
394 if (pwm->pinentry_display || display)
395 have_display = 1;
396 else {
397 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
399 if (!tty)
400 return gpg_error_from_errno(errno);
403 if (!display && !tty)
404 return GPG_ERR_ENOTTY;
406 argv[0] = "pinentry";
407 argv[1] = have_display ? "--display" : "--ttyname";
408 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
409 argv[3] = NULL;
411 if (!have_display) {
412 argv[3] = "--ttytype";
413 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
414 argv[5] = NULL;
417 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
419 if (rc)
420 return rc;
422 pwm->pid = assuan_get_pid(ctx);
423 pwm->pctx = ctx;
424 return set_pinentry_strings(pwm, 0);
427 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
429 gpg_error_t n;
431 if (!pwm->pctx) {
432 n = launch_pinentry(pwm);
434 if (n)
435 return n;
438 return assuan_command(pwm, pwm->pctx, result, cmd);
441 static void pinentry_disconnect(pwm_t *pwm)
443 if (pwm->pctx)
444 assuan_disconnect(pwm->pctx);
446 pwm->pctx = NULL;
447 pwm->pid = -1;
451 * Only called from a child process.
453 static void catchsig(int sig)
455 switch (sig) {
456 case SIGALRM:
457 if (gelapsed++ >= gtimeout) {
458 pwmd_terminate_pinentry(gpwm);
459 break;
462 alarm(1);
463 break;
464 default:
465 break;
469 static char *percent_escape(const char *atext)
471 const unsigned char *s;
472 int len = strlen(atext) * 3 + 1;
473 char *buf = (char *)xmalloc(len), *p = buf;
475 if (!buf)
476 return NULL;
478 for (s=(const unsigned char *)atext; *s; s++) {
479 if (*s < ' ') {
480 sprintf (p, "%%%02X", *s);
481 p += 3;
483 else
484 *p++ = *s;
487 *p = 0;
488 return buf;
490 #endif
492 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
494 if (!cmd)
495 return GPG_ERR_INV_ARG;
497 return assuan_command(pwm, pwm->ctx, result, cmd);
501 * Avoid sending the BYE command here. libassuan will close the file
502 * descriptor and release the assuan context. Use pwmd_close() instead.
504 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
506 va_list ap;
507 char *buf;
508 size_t len;
509 gpg_error_t error;
511 if (!pwm || !cmd)
512 return GPG_ERR_INV_ARG;
514 *result = NULL;
515 va_start(ap, cmd);
517 * C99
519 len = vsnprintf(NULL, 0, cmd, ap);
520 buf = (char *)xmalloc(len + 1);
521 len = vsnprintf(buf, len + 1, cmd, ap);
522 va_end(ap);
523 error = send_command(pwm, result, buf);
524 xfree(buf);
525 return error;
528 #ifdef USE_PINENTRY
529 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
531 gpg_error_t error;
533 if (gtimeout) {
534 signal(SIGALRM, catchsig);
535 alarm(1);
538 *result = NULL;
539 error = pinentry_command(pwm, result, "GETPIN");
541 if (error == GPG_ERR_ASS_CANCELED)
542 return error;
544 if (!*result)
545 return EPWMD_KEY;
547 return 0;
550 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
552 int pin_try = *try_n;
553 gpg_error_t error;
555 getpin_again:
556 *try_n = pin_try;
558 if (pin_try == -1) {
559 error = set_pinentry_strings(pwm, which);
561 if (error) {
562 pinentry_disconnect(pwm);
563 return error;
566 else {
567 if (pwm->pinentry_tries-1 != pin_try) {
568 error = set_pinentry_strings(pwm, 1);
570 if (error) {
571 pinentry_disconnect(pwm);
572 return error;
577 error = do_getpin(pwm, result);
579 if (error) {
580 if (pin_try != -1 && pin_try-- && error == EPWMD_KEY)
581 goto getpin_again;
583 if (pwm->pctx)
584 pinentry_disconnect(pwm);
586 *try_n = pin_try;
587 return error;
590 return 0;
592 #endif
594 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
596 char *result;
597 gpg_error_t error;
599 #ifndef USE_PINENTRY
600 return GPG_ERR_NOT_IMPLEMENTED;
601 #endif
603 if (!pwm || !pw || !pw->filename[0])
604 return GPG_ERR_INV_ARG;
606 close(pw->fd);
608 if (pw->error) {
609 error = pw->error;
610 goto fail;
613 error = pwmd_command(pwm, &result, "ISCACHED %s", pw->filename);
615 if (error)
616 goto fail;
618 if (pwm->filename)
619 xfree(pwm->filename);
621 pwm->filename = xstrdup(pw->filename);
622 memset(pw, 0, sizeof(pwmd_nb_status_t));
623 return 0;
625 fail:
626 memset(pw, 0, sizeof(pwmd_nb_status_t));
627 return error;
630 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
632 char buf[1000];
633 gpg_error_t error;
635 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
636 error = send_command(pwm, NULL, buf);
637 memset(&buf, 0, sizeof(buf));
638 xfree(password);
639 return error;
642 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
643 int nb, int timeout)
645 char *result = NULL;
646 char *password = NULL;
647 #ifdef USE_PINENTRY
648 int pin_try = pwm->pinentry_tries - 1;
649 #endif
651 if (!pwm || !filename || !*filename) {
652 *error = GPG_ERR_INV_ARG;
653 return nb ? -1 : 1;
657 * Avoid calling pinentry if the password is cached on the server.
659 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
661 if (*error == EPWMD_CACHE_NOT_FOUND) {
662 if (pwm->passfunc) {
663 password = pwm->passfunc(pwm, pwm->passdata);
665 if (!password || !*password) {
666 *error = EPWMD_KEY;
667 return nb ? -1 : 1;
670 password = xstrdup(password);
671 goto gotpassword;
674 if (*error == EPWMD_CACHE_NOT_FOUND) {
675 #ifdef USE_PINENTRY
677 * Get the password from pinentry.
679 if (pwm->use_pinentry) {
681 * Nonblocking is wanted. fork() then return a file descriptor
682 * that the client can use to read() from.
684 if (nb) {
685 int p[2];
686 pid_t pid;
687 pwmd_nb_status_t pw;
689 if (pipe(p) == -1) {
690 *error = gpg_error_from_syserror();
691 return -1;
694 pid = fork();
696 switch (pid) {
697 case 0:
698 close(p[0]);
699 strncpy(pw.filename, filename, sizeof(pw.filename));
700 pw.fd = p[0];
702 if (timeout > 0) {
703 gpwm = pwm;
704 gtimeout = timeout;
705 gelapsed = 0;
708 getpin_nb_again:
709 *error = getpin(pwm, &password, &pin_try, 0);
711 if (*error) {
712 getpin_nb_fail:
713 if (pwm->pctx)
714 pinentry_disconnect(pwm);
716 if (gtimeout && gelapsed >= gtimeout)
717 *error = GPG_ERR_TIMEOUT;
719 pw.error = *error;
720 write(p[1], &pw, sizeof(pw));
721 close(p[1]);
722 _exit(1);
726 * Don't count the time it takes to open the file
727 * which may have many iterations.
729 signal(SIGALRM, SIG_DFL);
730 *error = do_open_command(pwm, filename, password);
732 if (timeout)
733 signal(SIGALRM, catchsig);
735 if (pwm->pctx && *error == EPWMD_BADKEY) {
736 if (pin_try-- > 0)
737 goto getpin_nb_again;
739 goto getpin_nb_fail;
742 pinentry_disconnect(pwm);
743 pw.error = 0;
744 write(p[1], &pw, sizeof(pw));
745 close(p[1]);
746 _exit(0);
747 break;
748 case -1:
749 *error = gpg_error_from_syserror();
750 close(p[0]);
751 close(p[1]);
752 return -1;
753 default:
754 break;
757 close(p[1]);
758 return p[0];
761 getpin_again:
762 *error = getpin(pwm, &password, &pin_try, 1);
764 if (*error) {
765 if (pwm->pctx)
766 pinentry_disconnect(pwm);
768 return 1;
771 else {
772 #endif
774 * Not using pinentry and the file was not found
775 * in the cache.
777 if (pwm->password == NULL) {
778 *error = EPWMD_KEY;
779 return 1;
782 password = pwm->password;
783 #ifdef USE_PINENTRY
785 #endif
788 else if (*error && *error != EPWMD_FILE_NOT_FOUND)
789 return 1;
791 gotpassword:
792 *error = do_open_command(pwm, filename, password);
794 #ifdef USE_PINENTRY
795 if (pwm->pctx && *error == EPWMD_BADKEY) {
796 if (pin_try-- > 0)
797 goto getpin_again;
799 pinentry_disconnect(pwm);
800 return 1;
802 #endif
804 if (!pwm->use_pinentry && pwm->password)
805 pwm->password = NULL;
807 if (!*error) {
808 if (pwm->filename)
809 xfree(pwm->filename);
811 pwm->filename = xstrdup(filename);
815 * The file is cached or the file is a new file.
817 if (nb)
818 return *error ? -1 : -2;
820 return *error ? 1 : 0;
823 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
825 gpg_error_t error;
827 do_pwmd_open(pwm, &error, filename, 0, 0);
828 return error;
831 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
832 int timeout)
834 #ifndef USE_PINENTRY
835 *error = GPG_ERR_NOT_IMPLEMENTED;
836 return -1;
837 #else
838 return do_pwmd_open(pwm, error, filename, 1, timeout);
839 #endif
842 #ifdef USE_PINENTRY
843 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
845 int confirm = 0;
846 gpg_error_t error;
847 char *result = NULL;
848 int pin_try = -1;
850 again:
851 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
853 if (error) {
854 if (pwm->pctx)
855 pinentry_disconnect(pwm);
857 if (*password)
858 xfree(*password);
860 return error;
863 if (!confirm++) {
864 *password = result;
865 goto again;
868 if (strcmp(*password, result)) {
869 xfree(*password);
870 xfree(result);
871 pinentry_disconnect(pwm);
872 error = EPWMD_BADKEY;
873 return error;
876 xfree(result);
877 pinentry_disconnect(pwm);
878 return 0;
880 #endif
882 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
884 char buf[ASSUAN_LINELENGTH];
885 gpg_error_t error;
887 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
888 error = send_command(pwm, NULL, buf);
889 memset(&buf, 0, sizeof(buf));
890 xfree(password);
891 pwm->password = NULL;
892 return error;
895 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
897 gpg_error_t error;
899 #ifndef USE_PINENTRY
900 return GPG_ERR_NOT_IMPLEMENTED;
901 #endif
903 if (!pwm || !pw || !pw->filename[0])
904 return GPG_ERR_INV_ARG;
906 close(pw->fd);
908 if (pw->error) {
909 error = pw->error;
910 memset(pw, 0, sizeof(pwmd_nb_status_t));
911 return error;
914 memset(pw, 0, sizeof(pwmd_nb_status_t));
915 return 0;
918 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
920 char *result = NULL;
921 char *password = NULL;
923 if (!pwm) {
924 *error = GPG_ERR_INV_ARG;
925 return 1;
928 if (pwm->use_pinentry || pwm->passfunc) {
929 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
931 if (*error == EPWMD_CACHE_NOT_FOUND) {
932 #ifdef USE_PINENTRY
933 if (pwm->use_pinentry) {
934 if (nb) {
935 int p[2];
936 pid_t pid;
937 pwmd_nb_status_t pw;
939 if (pipe(p) == -1) {
940 *error = gpg_error_from_syserror();
941 return -1;
944 pid = fork();
946 switch (pid) {
947 case 0:
948 close(p[0]);
949 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
950 pw.fd = p[0];
952 do {
953 password = NULL;
954 *error = do_save_getpin(pwm, &password);
955 } while (*error == EPWMD_KEY || *error == EPWMD_BADKEY);
957 if (*error) {
958 if (pwm->pctx)
959 pinentry_disconnect(pwm);
961 pw.error = *error;
962 write(p[1], &pw, sizeof(pw));
963 close(p[1]);
964 _exit(1);
967 *error = do_save_command(pwm, password);
968 pinentry_disconnect(pwm);
969 pw.error = *error;
970 write(p[1], &pw, sizeof(pw));
971 close(p[1]);
972 _exit(0);
973 break;
974 case -1:
975 *error = gpg_error_from_syserror();
976 close(p[0]);
977 close(p[1]);
978 return -1;
979 default:
980 break;
983 close(p[1]);
984 return p[0];
987 *error = do_save_getpin(pwm, &password);
989 if (*error)
990 return 1;
992 else {
993 #endif
994 if (pwm->passfunc) {
995 char *tmp = (*pwm->passfunc)(pwm, pwm->passdata);
997 if (!tmp || !*tmp) {
998 *error = EPWMD_KEY;
999 return 1;
1002 password = xstrdup(tmp);
1004 #ifdef USE_PINENTRY
1006 #endif
1008 if (!password || !*password) {
1009 *error = EPWMD_KEY;
1010 return 1;
1013 else {
1014 if (*error && *error != EPWMD_FILE_NOT_FOUND)
1015 return 1;
1018 else
1019 password = pwm->password;
1021 *error = do_save_command(pwm, password);
1023 if (nb)
1024 return *error ? -1 : -2;
1026 return *error ? 1 : 0;
1029 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1031 #ifndef USE_PINENTRY
1032 *error = GPG_ERR_NOT_IMPLEMENTED;
1033 return -1;
1034 #else
1035 return do_pwmd_save(pwm, error, 1);
1036 #endif
1039 gpg_error_t pwmd_save(pwm_t *pwm)
1041 gpg_error_t error;
1043 do_pwmd_save(pwm, &error, 0);
1044 return error;
1047 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1049 va_list ap;
1050 #ifdef USE_PINENTRY
1051 int n = va_arg(ap, int);
1052 #endif
1053 char *arg1;
1055 if (!pwm)
1056 return GPG_ERR_INV_ARG;
1058 va_start(ap, opt);
1060 switch (opt) {
1061 case PWMD_OPTION_STATUS_FUNC:
1062 pwm->status_func = va_arg(ap, pwmd_status_fn);
1063 break;
1064 case PWMD_OPTION_STATUS_DATA:
1065 pwm->status_data = va_arg(ap, void *);
1066 break;
1067 case PWMD_OPTION_PASSWORD_FUNC:
1068 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1069 break;
1070 case PWMD_OPTION_PASSWORD_DATA:
1071 pwm->passdata = va_arg(ap, void *);
1072 break;
1073 case PWMD_OPTION_PASSWORD:
1074 arg1 = va_arg(ap, char *);
1076 if (pwm->password)
1077 xfree(pwm->password);
1079 pwm->password = xstrdup(arg1);
1080 break;
1081 #ifdef USE_PINENTRY
1082 case PWMD_OPTION_PINENTRY:
1083 n = va_arg(ap, int);
1085 if (n != 0 && n != 1) {
1086 va_end(ap);
1087 return GPG_ERR_INV_VALUE;
1090 pwm->use_pinentry = n;
1091 break;
1092 case PWMD_OPTION_PINENTRY_TRIES:
1093 n = va_arg(ap, int);
1095 if (n <= 0) {
1096 va_end(ap);
1097 return GPG_ERR_INV_VALUE;
1100 pwm->pinentry_tries = n;
1101 break;
1102 case PWMD_OPTION_PINENTRY_PATH:
1103 if (pwm->pinentry_path)
1104 xfree(pwm->pinentry_path);
1106 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1107 break;
1108 case PWMD_OPTION_PINENTRY_TTY:
1109 if (pwm->pinentry_tty)
1110 xfree(pwm->pinentry_tty);
1112 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1113 break;
1114 case PWMD_OPTION_PINENTRY_DISPLAY:
1115 if (pwm->pinentry_display)
1116 xfree(pwm->pinentry_display);
1118 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1119 break;
1120 case PWMD_OPTION_PINENTRY_TERM:
1121 if (pwm->pinentry_term)
1122 xfree(pwm->pinentry_term);
1124 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1125 break;
1126 case PWMD_OPTION_PINENTRY_TITLE:
1127 if (pwm->title)
1128 xfree(pwm->title);
1129 pwm->title = percent_escape(va_arg(ap, char *));
1130 break;
1131 case PWMD_OPTION_PINENTRY_PROMPT:
1132 if (pwm->prompt)
1133 xfree(pwm->prompt);
1134 pwm->prompt = percent_escape(va_arg(ap, char *));
1135 break;
1136 case PWMD_OPTION_PINENTRY_DESC:
1137 if (pwm->desc)
1138 xfree(pwm->desc);
1139 pwm->desc = percent_escape(va_arg(ap, char *));
1140 break;
1141 #else
1142 case PWMD_OPTION_PINENTRY:
1143 case PWMD_OPTION_PINENTRY_TRIES:
1144 case PWMD_OPTION_PINENTRY_PATH:
1145 case PWMD_OPTION_PINENTRY_TTY:
1146 case PWMD_OPTION_PINENTRY_DISPLAY:
1147 case PWMD_OPTION_PINENTRY_TERM:
1148 case PWMD_OPTION_PINENTRY_TITLE:
1149 case PWMD_OPTION_PINENTRY_PROMPT:
1150 case PWMD_OPTION_PINENTRY_DESC:
1151 return GPG_ERR_NOT_IMPLEMENTED;
1152 #endif
1153 default:
1154 va_end(ap);
1155 return GPG_ERR_NOT_IMPLEMENTED;
1158 va_end(ap);
1159 return 0;