Fixed showing the wrong function when an error occurred in pwmc.
[libpwmd.git] / libpwmd.c
blobf1fd62ca5f6a55b7a15b34e5efa507ef7197b18e
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2008 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 02110-1301 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 <sys/select.h>
38 #include <libpwmd.h>
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
44 #ifdef HAVE_ASSUAN_H
45 #include <assuan.h>
46 #endif
48 #ifdef HAVE_SETLOCALE
49 #include <locale.h>
50 #endif
52 #include "gettext.h"
53 #define N_(msgid) dgettext("libpwmd", msgid)
55 #ifndef MEM_DEBUG
56 #include "mem.h"
57 #else
58 #define xfree free
59 #define xrealloc realloc
60 #define xmalloc malloc
61 #define xstrdup strdup
62 #define xcalloc calloc
63 #endif
65 #include "types.h"
67 #ifdef USE_PINENTRY
68 static pwm_t *gpwm;
69 static int gelapsed, gtimeout;
70 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd);
71 static gpg_error_t global_error;
72 #endif
74 const char *pwmd_strerror(gpg_error_t e)
76 gpg_err_code_t code = gpg_err_code(e);
78 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
79 switch (code) {
80 case GPG_ERR_USER_1:
81 default:
82 return N_("Unknown error");
83 case GPG_ERR_USER_2:
84 return N_("No cache slots available");
85 case GPG_ERR_USER_3:
86 return N_("Recursion loop");
87 case GPG_ERR_USER_4:
88 return N_("No file is open");
89 case GPG_ERR_USER_5:
90 return N_("General LibXML error");
91 case GPG_ERR_USER_6:
92 return N_("File modified");
96 return gpg_strerror(e);
99 gpg_error_t pwmd_init()
101 #ifdef ENABLE_NLS
102 bindtextdomain("libpwmd", LOCALEDIR);
103 #endif
104 gpg_err_init();
105 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
106 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
107 return 0;
110 pwm_t *pwmd_connect(const char *path, gpg_error_t *error)
112 pwm_t *pwm = NULL;
113 char *socketpath = NULL;
114 time_t now;
115 struct passwd *pw;
116 assuan_context_t ctx;
117 int rc, n;
118 int active[2];
120 if (!path) {
121 pw = getpwuid(getuid());
122 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
123 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
125 else
126 socketpath = xstrdup(path);
128 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
129 xfree(socketpath);
131 if (rc) {
132 *error = rc;
133 return NULL;
136 n = assuan_get_active_fds(ctx, 0, active, sizeof(active));
138 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
139 *error = gpg_error_from_errno(errno);
140 assuan_disconnect(ctx);
141 return NULL;
144 pwm->fd = n <= 0 ? -1 : dup(active[0]);
146 if (pwm->fd != -1)
147 fcntl(pwm->fd, F_SETFL, O_NONBLOCK);
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 *error = 0;
157 return pwm;
160 gpg_error_t pwmd_pending_line(pwm_t *pwm, char **line, size_t *len)
162 if (!pwm)
163 return GPG_ERR_INV_ARG;
165 if (assuan_pending_line(pwm->ctx))
166 return assuan_read_line(pwm->ctx, line, len);
168 return GPG_ERR_NO_DATA;
171 void pwmd_close(pwm_t *pwm)
173 if (!pwm)
174 return;
176 if (pwm->ctx)
177 assuan_disconnect(pwm->ctx);
179 if (pwm->password)
180 xfree(pwm->password);
182 if (pwm->title)
183 xfree(pwm->title);
185 if (pwm->desc)
186 xfree(pwm->desc);
188 if (pwm->prompt)
189 xfree(pwm->prompt);
191 if (pwm->pinentry_tty)
192 xfree(pwm->pinentry_tty);
194 if (pwm->pinentry_display)
195 xfree(pwm->pinentry_display);
197 if (pwm->pinentry_term)
198 xfree(pwm->pinentry_term);
200 if (pwm->filename)
201 xfree(pwm->filename);
203 xfree(pwm);
206 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
208 membuf_t *mem = (membuf_t *)data;
209 void *p;
211 if (!buffer)
212 return 0;
214 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
215 return 1;
217 mem->buf = p;
218 memcpy((char *)mem->buf + mem->len, buffer, len);
219 mem->len += len;
220 return 0;
223 void pwmd_free_result(void *data)
225 xfree(data);
228 static int _inquire_cb(void *data, const char *keyword)
230 pwm_t *pwm = (pwm_t *)data;
231 gpg_error_t rc = 0;
232 int flags = fcntl(pwm->fd, F_GETFL);
234 /* Shouldn't get this far without a callback. */
235 if (!pwm->inquire_func)
236 return GPG_ERR_INV_ARG;
239 * Since the socket file descriptor is probably set to non-blocking, set to
240 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
241 * asynchronous INQUIRE is supported by either libassuan or a later
242 * libpwmd.
244 fcntl(pwm->fd, F_SETFL, 0);
246 for (;;) {
247 char *result = NULL;
248 size_t len;
249 gpg_error_t arc;
251 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
252 rc = gpg_err_code(rc);
254 if (rc == GPG_ERR_EOF || !rc) {
255 if (len <= 0 || !result || !*result) {
256 rc = 0;
257 break;
260 arc = assuan_send_data(pwm->ctx, result, len);
262 if (rc == GPG_ERR_EOF) {
263 rc = arc;
264 break;
267 rc = arc;
269 else if (rc)
270 break;
273 fcntl(pwm->fd, F_SETFL, flags);
274 return rc;
277 gpg_error_t pwmd_finalize(pwm_t *pwm)
279 if (!pwm || pwm->fd < 0)
280 return GPG_ERR_INV_ARG;
282 pwm->state = ASYNC_INIT;
283 pwm->ntries = 0;
284 pwm->is_open_cmd = 0;
285 return 0;
288 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
290 char *buf;
291 gpg_error_t rc;
292 size_t len = strlen(cmd) + 2;
294 len += arg ? strlen(arg) : 0;
296 if (pwm->state != ASYNC_INIT)
297 return GPG_ERR_UNEXPECTED;
299 buf = (char *)xmalloc(len);
301 if (!buf) {
302 rc = gpg_error_from_errno(ENOMEM);
303 goto fail;
306 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
307 rc = assuan_write_line(pwm->ctx, buf);
308 xfree(buf);
310 if (!rc)
311 pwm->state = ASYNC_PROCESS;
313 fail:
314 return rc;
317 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
319 if (!pwm || !filename)
320 return GPG_ERR_INV_ARG;
322 /* For pinentry retries. */
323 if (!pwm->is_open_cmd) {
324 if (pwm->filename)
325 xfree(pwm->filename);
327 pwm->filename = xstrdup(filename);
330 pwm->is_open_cmd = 1;
331 return do_nb_command(pwm, "OPEN", filename);
334 gpg_error_t pwmd_save_async(pwm_t *pwm)
336 if (!pwm)
337 return GPG_ERR_INV_ARG;
339 return do_nb_command(pwm, "SAVE", NULL);
342 static gpg_error_t parse_assuan_line(pwm_t *pwm)
344 gpg_error_t rc;
345 char *line;
346 size_t len;
348 rc = assuan_read_line(pwm->ctx, &line, &len);
350 if (!rc) {
351 if (line[0] == 'O' && line[1] == 'K' &&
352 (line[2] == 0 || line[2] == ' ')) {
353 pwm->state = ASYNC_DONE;
355 else if (line[0] == '#') {
357 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
358 if (pwm->status_func) {
359 pwm->status_func(pwm->status_data,
360 line[1] == 0 ? line+1 : line+2);
363 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
364 (line[3] == 0 || line[3] == ' ')) {
365 line += 4;
366 rc = atoi(line);
367 pwm->state = ASYNC_DONE;
371 return rc;
374 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
376 fd_set rfds;
377 int n;
378 struct timeval tv = {0, 0};
380 *rc = 0;
382 if (!pwm || pwm->fd < 0) {
383 *rc = GPG_ERR_INV_ARG;
384 return ASYNC_DONE;
386 else if (pwm->state == ASYNC_DONE)
387 return pwm->state;
388 else if (pwm->state == ASYNC_INIT) {
389 *rc = GPG_ERR_UNEXPECTED;
390 return ASYNC_DONE;
393 FD_ZERO(&rfds);
394 FD_SET(pwm->fd, &rfds);
395 n = select(pwm->fd+1, &rfds, NULL, NULL, &tv);
397 if (n > 0) {
398 if (FD_ISSET(pwm->fd, &rfds))
399 *rc = parse_assuan_line(pwm);
402 while (!*rc && assuan_pending_line(pwm->ctx))
403 *rc = parse_assuan_line(pwm);
405 if (pwm->is_open_cmd && gpg_err_code(*rc) == EPWMD_BADKEY &&
406 ++pwm->ntries < pwm->pinentry_tries) {
407 pwm->state = ASYNC_INIT;
408 *rc = pwmd_open_async(pwm, pwm->filename);
411 return pwm->state;
414 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
415 char **result, const char *cmd)
417 membuf_t data;
418 gpg_error_t rc;
420 data.len = 0;
421 data.buf = NULL;
423 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
424 pwm->status_func, pwm->status_data);
426 if (rc) {
427 if (data.buf) {
428 xfree(data.buf);
429 data.buf = NULL;
432 else {
433 if (data.buf) {
434 mem_realloc_cb(&data, "", 1);
435 *result = (char *)data.buf;
439 return gpg_err_code(rc);
442 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
443 void *data)
445 if (!pwm || !cmd || !fn)
446 return GPG_ERR_INV_ARG;
448 pwm->inquire_func = fn;
449 pwm->inquire_data = data;
450 return assuan_command(pwm, pwm->ctx, NULL, cmd);
453 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
455 #ifndef USE_PINENTRY
456 return GPG_ERR_NOT_IMPLEMENTED;
457 #else
458 if (!pwm || pwm->pid == -1)
459 return GPG_ERR_INV_ARG;
461 if (kill(pwm->pid, 0) == 0) {
462 if (kill(pwm->pid, SIGTERM) == -1) {
463 if (kill(pwm->pid, SIGKILL) == -1)
464 return gpg_error_from_errno(errno);
467 pwm->pin_error = GPG_ERR_TIMEOUT;
469 else
470 return gpg_error_from_errno(errno);
472 return 0;
473 #endif
476 #ifdef USE_PINENTRY
477 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
479 char *buf;
480 char tmp[ASSUAN_LINELENGTH];
481 gpg_error_t error;
483 if (!pwm->title)
484 pwm->title = xstrdup(N_("LibPWMD"));
486 if (!pwm->prompt)
487 pwm->prompt = xstrdup(N_("Password:"));
489 if (!pwm->desc && !which)
490 pwm->desc = xstrdup(N_("Enter a password."));
492 if (which == 1) {
493 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid password, please try again."));
494 buf = xstrdup(tmp);
496 else if (which == 2) {
497 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the password again for confirmation."));
498 buf = xstrdup(tmp);
500 else {
501 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
502 sprintf(buf, "SETERROR %s", pwm->desc);
505 error = pinentry_command(pwm, NULL, buf);
506 xfree(buf);
508 if (error)
509 return error;
511 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
512 sprintf(buf, "SETPROMPT %s", pwm->prompt);
513 error = pinentry_command(pwm, NULL, buf);
514 xfree(buf);
516 if (error)
517 return error;
519 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
520 sprintf(buf, "SETDESC %s", pwm->title);
521 error = pinentry_command(pwm, NULL, buf);
522 xfree(buf);
523 return error;
526 static void update_pinentry_settings(pwm_t *pwm)
528 FILE *fp;
529 char buf[LINE_MAX];
530 struct passwd *pw = getpwuid(getuid());
531 char *p;
533 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
535 if ((fp = fopen(buf, "r")) == NULL)
536 return;
538 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
539 char name[32], val[256];
541 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
542 continue;
544 if (strcasecmp(name, "TTYNAME") == 0) {
545 xfree(pwm->pinentry_tty);
546 pwm->pinentry_tty = xstrdup(val);
548 else if (strcasecmp(name, "TTYTYPE") == 0) {
549 xfree(pwm->pinentry_term);
550 pwm->pinentry_term = xstrdup(val);
552 else if (strcasecmp(name, "DISPLAY") == 0) {
553 xfree(pwm->pinentry_display);
554 pwm->pinentry_display = xstrdup(val);
556 else if (strcasecmp(name, "PATH") == 0) {
557 xfree(pwm->pinentry_path);
558 pwm->pinentry_path = xstrdup(val);
562 fclose(fp);
565 static gpg_error_t launch_pinentry(pwm_t *pwm)
567 int rc;
568 assuan_context_t ctx;
569 int child_list[] = {-1};
570 char *display = getenv("DISPLAY");
571 const char *argv[6];
572 int have_display = 0;
573 char *tty = NULL;
575 update_pinentry_settings(pwm);
577 if (pwm->pinentry_display || display)
578 have_display = 1;
579 else {
580 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
582 if (!tty)
583 return gpg_error_from_errno(errno);
586 if (!have_display && !tty)
587 return GPG_ERR_ENOTTY;
589 argv[0] = "pinentry";
590 argv[1] = have_display ? "--display" : "--ttyname";
591 argv[2] = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
592 argv[3] = NULL;
594 if (!have_display) {
595 argv[3] = "--ttytype";
596 argv[4] = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
597 argv[5] = NULL;
600 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
602 if (rc)
603 return rc;
605 pwm->pid = assuan_get_pid(ctx);
606 pwm->pctx = ctx;
607 return set_pinentry_strings(pwm, 0);
610 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
612 gpg_error_t n;
614 if (!pwm->pctx) {
615 n = launch_pinentry(pwm);
617 if (n)
618 return n;
621 return assuan_command(pwm, pwm->pctx, result, cmd);
624 static void pinentry_disconnect(pwm_t *pwm)
626 if (pwm->pctx)
627 assuan_disconnect(pwm->pctx);
629 pwm->pctx = NULL;
630 pwm->pid = -1;
634 * Only called from a child process.
636 static void catchsig(int sig)
638 switch (sig) {
639 case SIGALRM:
640 if (gelapsed++ >= gtimeout) {
641 global_error = pwmd_terminate_pinentry(gpwm);
643 if (!global_error)
644 global_error = GPG_ERR_TIMEOUT;
646 break;
649 alarm(1);
650 break;
651 default:
652 break;
657 * Borrowed from libassuan.
659 static char *percent_escape(const char *atext)
661 const unsigned char *s;
662 int len = strlen(atext) * 3 + 1;
663 char *buf = (char *)xmalloc(len), *p = buf;
665 if (!buf)
666 return NULL;
668 for (s=(const unsigned char *)atext; *s; s++) {
669 if (*s < ' ') {
670 sprintf (p, "%%%02X", *s);
671 p += 3;
673 else
674 *p++ = *s;
677 *p = 0;
678 return buf;
680 #endif
682 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
684 if (!cmd)
685 return GPG_ERR_INV_ARG;
687 return assuan_command(pwm, pwm->ctx, result, cmd);
691 * Avoid sending the BYE command here. libassuan will close the file
692 * descriptor and release the assuan context. Use pwmd_close() instead.
694 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
696 va_list ap;
697 char *buf;
698 size_t len;
699 gpg_error_t error;
701 if (!pwm || !cmd)
702 return GPG_ERR_INV_ARG;
704 *result = NULL;
705 va_start(ap, cmd);
707 * C99 allows the dst pointer to be null which will calculate the length
708 * of the result and return it.
710 len = vsnprintf(NULL, 0, cmd, ap);
711 buf = (char *)xmalloc(len + 1);
712 len = vsnprintf(buf, len + 1, cmd, ap);
713 va_end(ap);
714 error = send_command(pwm, result, buf);
715 xfree(buf);
716 return error;
719 #ifdef USE_PINENTRY
720 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
722 if (gtimeout) {
723 signal(SIGALRM, catchsig);
724 alarm(1);
727 *result = NULL;
728 return pinentry_command(pwm, result, "GETPIN");
731 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
733 int pin_try = *try_n;
734 gpg_error_t error;
736 getpin_again:
737 *try_n = pin_try;
739 if (pin_try == -1) {
740 error = set_pinentry_strings(pwm, which);
742 if (error) {
743 pinentry_disconnect(pwm);
744 return error;
747 else {
748 if (pwm->pinentry_tries-1 != pin_try) {
749 error = set_pinentry_strings(pwm, 1);
751 if (error) {
752 pinentry_disconnect(pwm);
753 return error;
758 error = do_getpin(pwm, result);
761 * Since there was input cancel any timeout setting.
763 alarm(0);
765 if (error) {
766 if (error == GPG_ERR_CANCELED)
767 return GPG_ERR_CANCELED;
769 if (pin_try != -1 && pin_try--)
770 goto getpin_again;
772 if (pwm->pctx)
773 pinentry_disconnect(pwm);
775 *try_n = pin_try;
776 return error;
779 return 0;
781 #endif
783 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
785 gpg_error_t error;
787 #ifndef USE_PINENTRY
788 return GPG_ERR_NOT_IMPLEMENTED;
789 #endif
791 if (!pwm || !pw || !pw->filename[0])
792 return GPG_ERR_INV_ARG;
794 close(pw->fd);
796 if (pw->error) {
797 error = pw->error;
798 goto fail;
801 if (pwm->filename)
802 xfree(pwm->filename);
804 pwm->filename = xstrdup(pw->filename);
805 memset(pw, 0, sizeof(pwmd_nb_status_t));
806 return 0;
808 fail:
809 memset(pw, 0, sizeof(pwmd_nb_status_t));
810 return error;
813 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
815 char buf[ASSUAN_LINELENGTH];
816 gpg_error_t error;
817 char *result = NULL;
819 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
820 error = send_command(pwm, &result, buf);
821 memset(buf, 0, sizeof(buf));
823 if (error && result)
824 xfree(result);
826 return error;
829 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
830 int nb, int timeout)
832 char *result = NULL;
833 char *password = NULL;
834 char path[PATH_MAX];
835 #ifdef USE_PINENTRY
836 int pin_try;
837 #endif
839 if (!pwm || !filename || !*filename) {
840 *error = GPG_ERR_INV_ARG;
841 return nb ? -1 : 1;
844 #ifdef USE_PINENTRY
845 pin_try = pwm->pinentry_tries - 1;
846 #endif
849 * Avoid calling pinentry if the password is cached on the server or if
850 * this is a new file.
852 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
854 if (*error)
855 return nb ? -1 : 1;
857 snprintf(path, sizeof(path), "%s/%s", result, filename);
858 pwmd_free_result(result);
860 if (access(path, R_OK) == -1) {
861 if (errno == ENOENT)
862 goto gotpassword;
865 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
867 if (*error == EPWMD_CACHE_NOT_FOUND) {
868 if (pwm->passfunc) {
869 password = pwm->passfunc(pwm, pwm->passdata);
870 goto gotpassword;
873 #ifdef USE_PINENTRY
875 * Get the password from pinentry.
877 if (pwm->use_pinentry) {
879 * Nonblocking is wanted. fork() then return a file descriptor
880 * that the client can use to read() from.
882 if (nb) {
883 int p[2];
884 pid_t pid;
885 pwmd_nb_status_t pw;
887 if (pipe(p) == -1) {
888 *error = gpg_error_from_syserror();
889 return -1;
892 pid = fork();
894 switch (pid) {
895 case 0:
896 close(p[0]);
897 strncpy(pw.filename, filename, sizeof(pw.filename));
898 pw.filename[sizeof(pw.filename)-1] = 0;
899 pw.fd = p[0];
901 if (timeout > 0) {
902 gpwm = pwm;
903 gtimeout = timeout;
904 gelapsed = 0;
907 getpin_nb_again:
908 *error = getpin(pwm, &password, &pin_try, 0);
910 if (*error) {
911 getpin_nb_fail:
912 if (pwm->pctx)
913 pinentry_disconnect(pwm);
915 if (gtimeout && gelapsed >= gtimeout)
916 *error = GPG_ERR_TIMEOUT;
918 pw.error = *error;
919 write(p[1], &pw, sizeof(pw));
920 close(p[1]);
921 _exit(1);
925 * Don't count the time it takes to open the file
926 * which may have many iterations.
928 signal(SIGALRM, SIG_DFL);
929 *error = do_open_command(pwm, filename, password);
931 if (timeout)
932 signal(SIGALRM, catchsig);
934 if (pwm->pctx && *error == EPWMD_BADKEY) {
935 if (pin_try-- > 0)
936 goto getpin_nb_again;
938 goto getpin_nb_fail;
941 pinentry_disconnect(pwm);
942 pw.error = 0;
943 write(p[1], &pw, sizeof(pw));
944 close(p[1]);
945 _exit(0);
946 break;
947 case -1:
948 *error = gpg_error_from_syserror();
949 close(p[0]);
950 close(p[1]);
951 return -1;
952 default:
953 break;
956 close(p[1]);
957 return p[0];
960 else {
961 #endif
963 * Not using pinentry and the file was not found
964 * in the cache.
966 password = pwm->password;
967 #ifdef USE_PINENTRY
969 #endif
971 else if (*error)
972 return nb ? -1 : 1;
974 gotpassword:
975 *error = do_open_command(pwm, filename, password);
978 * Keep the user defined password set with pwmd_setopt(). The password may
979 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
981 if (password && password != pwm->password)
982 xfree(password);
984 #ifdef USE_PINENTRY
985 if (*error == EPWMD_BADKEY) {
986 if (pin_try-- > 0 && !nb) {
987 *error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
988 N_("Invalid password, please try again."));
990 if (*error)
991 return 1;
993 goto gotpassword;
996 if (nb)
997 pinentry_disconnect(pwm);
999 return nb ? -1 : 1;
1001 #endif
1003 if (!*error) {
1004 if (pwm->filename)
1005 xfree(pwm->filename);
1007 pwm->filename = xstrdup(filename);
1011 * The file is cached or the file is a new file.
1013 if (nb)
1014 return *error ? -1 : -2;
1016 return *error ? 1 : 0;
1019 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1021 gpg_error_t error;
1023 do_pwmd_open(pwm, &error, filename, 0, 0);
1024 return error;
1027 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1028 int timeout)
1030 #ifndef USE_PINENTRY
1031 *error = GPG_ERR_NOT_IMPLEMENTED;
1032 return -1;
1033 #else
1034 return do_pwmd_open(pwm, error, filename, 1, timeout);
1035 #endif
1038 #ifdef USE_PINENTRY
1039 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1041 int confirm = 0;
1042 gpg_error_t error;
1043 char *result = NULL;
1044 int pin_try = -1;
1046 again:
1047 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1049 if (error) {
1050 if (pwm->pctx)
1051 pinentry_disconnect(pwm);
1053 if (*password)
1054 xfree(*password);
1056 return error;
1059 if (!confirm++) {
1060 *password = result;
1061 goto again;
1064 if (strcmp(*password, result)) {
1065 xfree(*password);
1066 xfree(result);
1067 pinentry_disconnect(pwm);
1068 error = EPWMD_BADKEY;
1069 return error;
1072 xfree(result);
1073 pinentry_disconnect(pwm);
1074 return 0;
1076 #endif
1078 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1080 char buf[ASSUAN_LINELENGTH];
1081 gpg_error_t error;
1082 char *result = NULL;
1084 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1085 error = send_command(pwm, &result, buf);
1086 memset(&buf, 0, sizeof(buf));
1088 if (error && result)
1089 xfree(result);
1091 return error;
1094 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1096 gpg_error_t rc;
1098 #ifndef USE_PINENTRY
1099 return GPG_ERR_NOT_IMPLEMENTED;
1100 #endif
1102 if (!pwm || !pw || !pw->filename[0])
1103 return GPG_ERR_INV_ARG;
1105 close(pw->fd);
1106 rc = pw->error;
1107 memset(pw, 0, sizeof(pwmd_nb_status_t));
1108 return rc;
1111 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1113 char *result = NULL;
1114 char *password = NULL;
1116 if (!pwm) {
1117 *error = GPG_ERR_INV_ARG;
1118 return nb ? -1 : 1;
1121 if (pwm->use_pinentry || pwm->passfunc) {
1122 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1124 if (*error == EPWMD_CACHE_NOT_FOUND) {
1125 #ifdef USE_PINENTRY
1126 if (pwm->use_pinentry) {
1127 if (nb) {
1128 int p[2];
1129 pid_t pid;
1130 pwmd_nb_status_t pw;
1132 if (pipe(p) == -1) {
1133 *error = gpg_error_from_syserror();
1134 return -1;
1137 pid = fork();
1139 switch (pid) {
1140 case 0:
1141 close(p[0]);
1142 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1143 pw.filename[sizeof(pw.filename)-1] = 0;
1144 pw.fd = p[0];
1146 do {
1147 password = NULL;
1148 *error = do_save_getpin(pwm, &password);
1149 } while (*error == EPWMD_BADKEY);
1151 if (*error) {
1152 if (pwm->pctx)
1153 pinentry_disconnect(pwm);
1155 pw.error = *error;
1156 write(p[1], &pw, sizeof(pw));
1157 close(p[1]);
1158 _exit(1);
1161 *error = do_save_command(pwm, password);
1162 pinentry_disconnect(pwm);
1163 pw.error = *error;
1164 write(p[1], &pw, sizeof(pw));
1165 close(p[1]);
1166 _exit(0);
1167 break;
1168 case -1:
1169 *error = gpg_error_from_syserror();
1170 close(p[0]);
1171 close(p[1]);
1172 return -1;
1173 default:
1174 break;
1177 close(p[1]);
1178 return p[0];
1181 *error = do_save_getpin(pwm, &password);
1183 if (*error)
1184 return 1;
1186 else {
1187 #endif
1188 if (pwm->passfunc)
1189 password = (*pwm->passfunc)(pwm, pwm->passdata);
1190 #ifdef USE_PINENTRY
1192 #endif
1194 else {
1195 if (*error)
1196 return nb ? -1 : 1;
1199 else
1200 password = pwm->password;
1202 *error = do_save_command(pwm, password);
1204 if (password && password != pwm->password)
1205 xfree(password);
1207 if (nb)
1208 return *error ? -1 : -2;
1210 return *error ? 1 : 0;
1213 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1215 #ifndef USE_PINENTRY
1216 *error = GPG_ERR_NOT_IMPLEMENTED;
1217 return -1;
1218 #else
1219 return do_pwmd_save(pwm, error, 1);
1220 #endif
1223 gpg_error_t pwmd_save(pwm_t *pwm)
1225 gpg_error_t error;
1227 do_pwmd_save(pwm, &error, 0);
1228 return error;
1231 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1233 va_list ap;
1234 #ifdef USE_PINENTRY
1235 int n = va_arg(ap, int);
1236 char *result;
1237 #endif
1238 char *arg1;
1239 gpg_error_t error = 0;
1241 if (!pwm)
1242 return GPG_ERR_INV_ARG;
1244 va_start(ap, opt);
1246 switch (opt) {
1247 case PWMD_OPTION_STATUS_FUNC:
1248 pwm->status_func = va_arg(ap, pwmd_status_fn);
1249 break;
1250 case PWMD_OPTION_STATUS_DATA:
1251 pwm->status_data = va_arg(ap, void *);
1252 break;
1253 case PWMD_OPTION_PASSWORD_FUNC:
1254 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1255 break;
1256 case PWMD_OPTION_PASSWORD_DATA:
1257 pwm->passdata = va_arg(ap, void *);
1258 break;
1259 case PWMD_OPTION_PASSWORD:
1260 arg1 = va_arg(ap, char *);
1262 if (pwm->password)
1263 xfree(pwm->password);
1265 pwm->password = xstrdup(arg1);
1266 break;
1267 #ifdef USE_PINENTRY
1268 case PWMD_OPTION_PINENTRY:
1269 n = va_arg(ap, int);
1271 if (n != 0 && n != 1) {
1272 va_end(ap);
1273 error = GPG_ERR_INV_VALUE;
1275 else {
1276 pwm->use_pinentry = n;
1277 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
1278 !pwm->use_pinentry);
1280 break;
1281 case PWMD_OPTION_PINENTRY_TRIES:
1282 n = va_arg(ap, int);
1284 if (n <= 0) {
1285 va_end(ap);
1286 error = GPG_ERR_INV_VALUE;
1288 else
1289 pwm->pinentry_tries = n;
1290 break;
1291 case PWMD_OPTION_PINENTRY_PATH:
1292 if (pwm->pinentry_path)
1293 xfree(pwm->pinentry_path);
1295 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1296 break;
1297 case PWMD_OPTION_PINENTRY_TTY:
1298 if (pwm->pinentry_tty)
1299 xfree(pwm->pinentry_tty);
1301 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1302 break;
1303 case PWMD_OPTION_PINENTRY_DISPLAY:
1304 if (pwm->pinentry_display)
1305 xfree(pwm->pinentry_display);
1307 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1308 break;
1309 case PWMD_OPTION_PINENTRY_TERM:
1310 if (pwm->pinentry_term)
1311 xfree(pwm->pinentry_term);
1313 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1314 break;
1315 case PWMD_OPTION_PINENTRY_TITLE:
1316 if (pwm->title)
1317 xfree(pwm->title);
1318 pwm->title = percent_escape(va_arg(ap, char *));
1319 break;
1320 case PWMD_OPTION_PINENTRY_PROMPT:
1321 if (pwm->prompt)
1322 xfree(pwm->prompt);
1323 pwm->prompt = percent_escape(va_arg(ap, char *));
1324 break;
1325 case PWMD_OPTION_PINENTRY_DESC:
1326 if (pwm->desc)
1327 xfree(pwm->desc);
1328 pwm->desc = percent_escape(va_arg(ap, char *));
1329 break;
1330 #else
1331 case PWMD_OPTION_PINENTRY:
1332 case PWMD_OPTION_PINENTRY_TRIES:
1333 case PWMD_OPTION_PINENTRY_PATH:
1334 case PWMD_OPTION_PINENTRY_TTY:
1335 case PWMD_OPTION_PINENTRY_DISPLAY:
1336 case PWMD_OPTION_PINENTRY_TERM:
1337 case PWMD_OPTION_PINENTRY_TITLE:
1338 case PWMD_OPTION_PINENTRY_PROMPT:
1339 case PWMD_OPTION_PINENTRY_DESC:
1340 error = GPG_ERR_NOT_IMPLEMENTED;
1341 break;
1342 #endif
1343 default:
1344 error = GPG_ERR_NOT_IMPLEMENTED;
1345 break;
1348 va_end(ap);
1349 return error;
1352 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, assuan_context_t *ctx, int *fd)
1354 if (!pwm)
1355 return GPG_ERR_INV_ARG;
1357 *ctx = pwm->ctx;
1358 *fd = pwm->fd;
1359 return 0;