Compile-time fix for FreeBSD when trying to link with libintl.
[libpwmd.git] / libpwmd.c
blob6526da9ce9c49cbe128b5974c07f8508b0acbaf5
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2009 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 WITH_LIBPTH
45 #include <pth.h>
46 #endif
48 #ifdef HAVE_ASSUAN_H
49 #include <assuan.h>
50 #endif
52 #ifdef HAVE_SETLOCALE
53 #include <locale.h>
54 #endif
56 #include "gettext.h"
57 #define N_(msgid) dgettext("libpwmd", msgid)
59 #include "mem.h"
60 #include "types.h"
62 #ifdef USE_PINENTRY
63 static pwm_t *gpwm;
64 static int gelapsed, gtimeout;
65 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd);
66 static gpg_error_t global_error;
67 #endif
69 const char *pwmd_strerror(gpg_error_t e)
71 gpg_err_code_t code = gpg_err_code(e);
73 if (code >= GPG_ERR_USER_1 && code < gpg_err_code(EPWMD_MAX)) {
74 switch (code) {
75 case GPG_ERR_USER_1:
76 default:
77 return N_("Unknown error");
78 case GPG_ERR_USER_2:
79 return N_("No cache slots available");
80 case GPG_ERR_USER_3:
81 return N_("Recursion loop");
82 case GPG_ERR_USER_4:
83 return N_("No file is open");
84 case GPG_ERR_USER_5:
85 return N_("General LibXML error");
86 case GPG_ERR_USER_6:
87 return N_("File modified");
91 return gpg_strerror(e);
94 gpg_error_t pwmd_init()
96 static int initialized;
98 if (initialized)
99 return 0;
101 #ifndef MEM_DEBUG
102 xmem_init();
103 #endif
104 #ifdef ENABLE_NLS
105 bindtextdomain("libpwmd", LOCALEDIR);
106 #endif
107 gpg_err_init();
108 assuan_set_malloc_hooks(xmalloc, xrealloc, xfree);
109 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT);
110 initialized = 1;
111 return 0;
114 pwm_t *pwmd_connect(const char *path, gpg_error_t *error)
116 pwm_t *pwm = NULL;
117 char *socketpath = NULL;
118 struct passwd *pw;
119 assuan_context_t ctx;
120 int rc, n;
121 int active[2];
123 if (!path) {
124 pw = getpwuid(getuid());
125 socketpath = (char *)xmalloc(strlen(pw->pw_dir) + strlen("/.pwmd/socket") + 1);
126 sprintf(socketpath, "%s/.pwmd/socket", pw->pw_dir);
128 else
129 socketpath = xstrdup(path);
131 rc = assuan_socket_connect_ext(&ctx, socketpath, -1, 0);
132 xfree(socketpath);
134 if (rc) {
135 *error = rc;
136 return NULL;
139 n = assuan_get_active_fds(ctx, 0, active, sizeof(active));
141 if ((pwm = (pwm_t *)xcalloc(1, sizeof(pwm_t))) == NULL) {
142 *error = gpg_error_from_errno(errno);
143 assuan_disconnect(ctx);
144 return NULL;
147 pwm->fd = n <= 0 ? -1 : dup(active[0]);
149 if (pwm->fd != -1)
150 fcntl(pwm->fd, F_SETFL, O_NONBLOCK);
152 pwm->ctx = ctx;
153 #ifdef USE_PINENTRY
154 pwm->pid = -1;
155 pwm->pinentry_tries = 3;
156 #endif
157 *error = 0;
158 return pwm;
161 gpg_error_t pwmd_pending_line(pwm_t *pwm, char **line, size_t *len)
163 if (!pwm)
164 return GPG_ERR_INV_ARG;
166 if (assuan_pending_line(pwm->ctx))
167 return assuan_read_line(pwm->ctx, line, len);
169 return GPG_ERR_NO_DATA;
172 void pwmd_close(pwm_t *pwm)
174 if (!pwm)
175 return;
177 if (pwm->ctx)
178 assuan_disconnect(pwm->ctx);
180 if (pwm->password)
181 xfree(pwm->password);
183 if (pwm->title)
184 xfree(pwm->title);
186 if (pwm->desc)
187 xfree(pwm->desc);
189 if (pwm->prompt)
190 xfree(pwm->prompt);
192 if (pwm->pinentry_tty)
193 xfree(pwm->pinentry_tty);
195 if (pwm->pinentry_display)
196 xfree(pwm->pinentry_display);
198 if (pwm->pinentry_term)
199 xfree(pwm->pinentry_term);
201 if (pwm->lcctype)
202 xfree(pwm->lcctype);
204 if (pwm->lcmessages)
205 xfree(pwm->lcmessages);
207 if (pwm->filename)
208 xfree(pwm->filename);
210 xfree(pwm);
213 static int mem_realloc_cb(void *data, const void *buffer, size_t len)
215 membuf_t *mem = (membuf_t *)data;
216 void *p;
218 if (!buffer)
219 return 0;
221 if ((p = xrealloc(mem->buf, mem->len + len)) == NULL)
222 return 1;
224 mem->buf = p;
225 memcpy((char *)mem->buf + mem->len, buffer, len);
226 mem->len += len;
227 return 0;
230 void pwmd_free_result(void *data)
232 xfree(data);
235 static int _inquire_cb(void *data, const char *keyword)
237 pwm_t *pwm = (pwm_t *)data;
238 gpg_error_t rc = 0;
239 int flags = fcntl(pwm->fd, F_GETFL);
241 /* Shouldn't get this far without a callback. */
242 if (!pwm->inquire_func)
243 return GPG_ERR_INV_ARG;
246 * Since the socket file descriptor is probably set to non-blocking, set to
247 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
248 * asynchronous INQUIRE is supported by either libassuan or a later
249 * libpwmd.
251 fcntl(pwm->fd, F_SETFL, 0);
253 for (;;) {
254 char *result = NULL;
255 size_t len;
256 gpg_error_t arc;
258 rc = pwm->inquire_func(pwm->inquire_data, keyword, rc, &result, &len);
259 rc = gpg_err_code(rc);
261 if (rc == GPG_ERR_EOF || !rc) {
262 if (len <= 0 || !result || !*result) {
263 rc = 0;
264 break;
267 arc = assuan_send_data(pwm->ctx, result, len);
269 if (rc == GPG_ERR_EOF) {
270 rc = arc;
271 break;
274 rc = arc;
276 else if (rc)
277 break;
280 fcntl(pwm->fd, F_SETFL, flags);
281 return rc;
284 gpg_error_t pwmd_finalize(pwm_t *pwm)
286 if (!pwm || pwm->fd < 0)
287 return GPG_ERR_INV_ARG;
289 pwm->state = ASYNC_INIT;
290 pwm->ntries = 0;
291 pwm->is_open_cmd = 0;
292 return 0;
295 static gpg_error_t do_nb_command(pwm_t *pwm, const char *cmd, const char *arg)
297 char *buf;
298 gpg_error_t rc;
299 size_t len = strlen(cmd) + 2;
301 len += arg ? strlen(arg) : 0;
303 if (pwm->state != ASYNC_INIT)
304 return GPG_ERR_UNEXPECTED;
306 buf = (char *)xmalloc(len);
308 if (!buf) {
309 rc = gpg_error_from_errno(ENOMEM);
310 goto fail;
313 snprintf(buf, len, "%s %s", cmd, arg ? arg : "");
314 rc = assuan_write_line(pwm->ctx, buf);
315 xfree(buf);
317 if (!rc)
318 pwm->state = ASYNC_PROCESS;
320 fail:
321 return rc;
324 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
326 if (!pwm || !filename)
327 return GPG_ERR_INV_ARG;
329 /* For pinentry retries. */
330 if (!pwm->is_open_cmd) {
331 if (pwm->filename)
332 xfree(pwm->filename);
334 pwm->filename = xstrdup(filename);
337 pwm->is_open_cmd = 1;
338 return do_nb_command(pwm, "OPEN", filename);
341 gpg_error_t pwmd_save_async(pwm_t *pwm)
343 if (!pwm)
344 return GPG_ERR_INV_ARG;
346 return do_nb_command(pwm, "SAVE", NULL);
349 static gpg_error_t parse_assuan_line(pwm_t *pwm)
351 gpg_error_t rc;
352 char *line;
353 size_t len;
355 rc = assuan_read_line(pwm->ctx, &line, &len);
357 if (!rc) {
358 if (line[0] == 'O' && line[1] == 'K' &&
359 (line[2] == 0 || line[2] == ' ')) {
360 pwm->state = ASYNC_DONE;
362 else if (line[0] == '#') {
364 else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) {
365 if (pwm->status_func) {
366 pwm->status_func(pwm->status_data,
367 line[1] == 0 ? line+1 : line+2);
370 else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' &&
371 (line[3] == 0 || line[3] == ' ')) {
372 line += 4;
373 rc = atoi(line);
374 pwm->state = ASYNC_DONE;
378 return rc;
381 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc)
383 fd_set rfds;
384 int n;
385 struct timeval tv = {0, 0};
387 *rc = 0;
389 if (!pwm || pwm->fd < 0) {
390 *rc = GPG_ERR_INV_ARG;
391 return ASYNC_DONE;
393 else if (pwm->state == ASYNC_DONE)
394 return pwm->state;
395 else if (pwm->state == ASYNC_INIT) {
396 *rc = GPG_ERR_UNEXPECTED;
397 return ASYNC_DONE;
400 FD_ZERO(&rfds);
401 FD_SET(pwm->fd, &rfds);
402 #ifdef WITH_LIBPTH
403 n = pth_select(pwm->fd+1, &rfds, NULL, NULL, &tv);
404 #else
405 n = select(pwm->fd+1, &rfds, NULL, NULL, &tv);
406 #endif
408 if (n > 0) {
409 if (FD_ISSET(pwm->fd, &rfds))
410 *rc = parse_assuan_line(pwm);
413 while (!*rc && assuan_pending_line(pwm->ctx))
414 *rc = parse_assuan_line(pwm);
416 if (pwm->is_open_cmd && gpg_err_code(*rc) == EPWMD_BADKEY &&
417 ++pwm->ntries < pwm->pinentry_tries) {
418 pwm->state = ASYNC_INIT;
419 *rc = pwmd_open_async(pwm, pwm->filename);
422 return pwm->state;
425 static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx,
426 char **result, const char *cmd)
428 membuf_t data;
429 gpg_error_t rc;
431 data.len = 0;
432 data.buf = NULL;
434 rc = assuan_transact(ctx, cmd, mem_realloc_cb, &data, _inquire_cb, pwm,
435 pwm->status_func, pwm->status_data);
437 if (rc) {
438 if (data.buf) {
439 xfree(data.buf);
440 data.buf = NULL;
443 else {
444 if (data.buf) {
445 mem_realloc_cb(&data, "", 1);
446 *result = (char *)data.buf;
450 return gpg_err_code(rc);
453 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_fn fn,
454 void *data)
456 if (!pwm || !cmd || !fn)
457 return GPG_ERR_INV_ARG;
459 pwm->inquire_func = fn;
460 pwm->inquire_data = data;
461 return assuan_command(pwm, pwm->ctx, NULL, cmd);
464 gpg_error_t pwmd_terminate_pinentry(pwm_t *pwm)
466 #ifndef USE_PINENTRY
467 return GPG_ERR_NOT_IMPLEMENTED;
468 #else
469 pid_t pid = pwm->pid;
471 pwm->pid = -1;
473 if (!pwm || pid == -1)
474 return GPG_ERR_INV_ARG;
476 if (kill(pid, 0) == 0) {
477 if (kill(pid, SIGTERM) == -1) {
478 if (kill(pid, SIGKILL) == -1)
479 return gpg_error_from_errno(errno);
482 pwm->pin_error = GPG_ERR_TIMEOUT;
484 else
485 return gpg_error_from_errno(errno);
487 return 0;
488 #endif
491 #ifdef USE_PINENTRY
492 static gpg_error_t set_pinentry_strings(pwm_t *pwm, int which)
494 char *buf;
495 char tmp[ASSUAN_LINELENGTH];
496 gpg_error_t error;
498 if (!pwm->title)
499 pwm->title = xstrdup(N_("LibPWMD"));
501 if (!pwm->prompt)
502 pwm->prompt = xstrdup(N_("Passphrase:"));
504 if (!pwm->desc && !which)
505 pwm->desc = xstrdup(N_("Enter a passphrase."));
507 if (which == 1) {
508 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Invalid passphrase, please try again."));
509 buf = xstrdup(tmp);
511 else if (which == 2) {
512 snprintf(tmp, sizeof(tmp), "SETERROR %s", N_("Please type the passphrase again for confirmation."));
513 buf = xstrdup(tmp);
515 else {
516 buf = (char *)xmalloc(strlen("SETERROR ") + strlen(pwm->desc) + 1);
517 sprintf(buf, "SETERROR %s", pwm->desc);
520 error = pinentry_command(pwm, NULL, buf);
521 xfree(buf);
523 if (error)
524 return error;
526 buf = (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm->prompt) + 1);
527 sprintf(buf, "SETPROMPT %s", pwm->prompt);
528 error = pinentry_command(pwm, NULL, buf);
529 xfree(buf);
531 if (error)
532 return error;
534 buf = (char *)xmalloc(strlen("SETDESC ") + strlen(pwm->title) + 1);
535 sprintf(buf, "SETDESC %s", pwm->title);
536 error = pinentry_command(pwm, NULL, buf);
537 xfree(buf);
538 return error;
541 static void update_pinentry_settings(pwm_t *pwm)
543 FILE *fp;
544 char buf[LINE_MAX];
545 struct passwd *pw = getpwuid(getuid());
546 char *p;
548 snprintf(buf, sizeof(buf), "%s/.pwmd/pinentry.conf", pw->pw_dir);
550 if ((fp = fopen(buf, "r")) == NULL)
551 return;
553 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
554 char name[32], val[256];
556 if (sscanf(p, " %31[a-zA-Z] = %255s", name, val) != 2)
557 continue;
559 if (strcasecmp(name, "TTYNAME") == 0) {
560 xfree(pwm->pinentry_tty);
561 pwm->pinentry_tty = xstrdup(val);
563 else if (strcasecmp(name, "TTYTYPE") == 0) {
564 xfree(pwm->pinentry_term);
565 pwm->pinentry_term = xstrdup(val);
567 else if (strcasecmp(name, "DISPLAY") == 0) {
568 xfree(pwm->pinentry_display);
569 pwm->pinentry_display = xstrdup(val);
571 else if (strcasecmp(name, "PATH") == 0) {
572 xfree(pwm->pinentry_path);
573 pwm->pinentry_path = xstrdup(val);
577 fclose(fp);
580 static gpg_error_t launch_pinentry(pwm_t *pwm)
582 int rc;
583 assuan_context_t ctx;
584 int child_list[] = {-1};
585 char *display = getenv("DISPLAY");
586 const char *argv[10];
587 const char **p = argv;
588 int have_display = 0;
589 char *tty = NULL;
591 update_pinentry_settings(pwm);
593 if (pwm->pinentry_display || display)
594 have_display = 1;
595 else {
596 tty = pwm->pinentry_tty ? pwm->pinentry_tty : ttyname(STDOUT_FILENO);
598 if (!tty)
599 return gpg_error_from_errno(errno);
602 if (!have_display && !tty)
603 return GPG_ERR_ENOTTY;
605 *p++ = "pinentry";
606 *p++ = have_display ? "--display" : "--ttyname";
607 *p++ = have_display ? pwm->pinentry_display ? pwm->pinentry_display : display : tty;
609 if (pwm->lcctype) {
610 *p++ = "--lc-ctype";
611 *p++ = pwm->lcctype;
614 if (pwm->lcmessages) {
615 *p++ = "--lc-messages";
616 *p++ = pwm->lcmessages;
619 *p = NULL;
621 if (!have_display) {
622 *p++ = "--ttytype";
623 *p++ = pwm->pinentry_term ? pwm->pinentry_term : getenv("TERM");
624 *p = NULL;
627 rc = assuan_pipe_connect(&ctx, pwm->pinentry_path ? pwm->pinentry_path : PINENTRY_PATH, argv, child_list);
629 if (rc)
630 return rc;
632 pwm->pid = assuan_get_pid(ctx);
633 pwm->pctx = ctx;
634 return set_pinentry_strings(pwm, 0);
637 static gpg_error_t pinentry_command(pwm_t *pwm, char **result, const char *cmd)
639 gpg_error_t n;
641 if (!pwm->pctx) {
642 n = launch_pinentry(pwm);
644 if (n)
645 return n;
648 return assuan_command(pwm, pwm->pctx, result, cmd);
651 static void pinentry_disconnect(pwm_t *pwm)
653 if (pwm->pctx)
654 assuan_disconnect(pwm->pctx);
656 pwm->pctx = NULL;
657 pwm->pid = -1;
661 * Only called from a child process.
663 static void catchsig(int sig)
665 switch (sig) {
666 case SIGALRM:
667 if (gelapsed++ >= gtimeout) {
668 global_error = pwmd_terminate_pinentry(gpwm);
670 if (!global_error)
671 global_error = GPG_ERR_TIMEOUT;
673 break;
676 alarm(1);
677 break;
678 default:
679 break;
682 #endif
685 * Borrowed from libassuan.
687 static char *percent_escape(const char *atext)
689 const unsigned char *s;
690 int len = strlen(atext) * 3 + 1;
691 char *buf = (char *)xmalloc(len), *p = buf;
693 if (!buf)
694 return NULL;
696 for (s=(const unsigned char *)atext; *s; s++) {
697 if (*s < ' ') {
698 sprintf (p, "%%%02X", *s);
699 p += 3;
701 else
702 *p++ = *s;
705 *p = 0;
706 return buf;
709 static gpg_error_t send_command(pwm_t *pwm, char **result, const char *cmd)
711 if (!cmd)
712 return GPG_ERR_INV_ARG;
714 return assuan_command(pwm, pwm->ctx, result, cmd);
717 gpg_error_t pwmd_command_ap(pwm_t *pwm, char **result, const char *cmd,
718 va_list ap)
720 char *buf;
721 size_t len;
722 gpg_error_t error;
724 if (!pwm || !cmd)
725 return GPG_ERR_INV_ARG;
728 * C99 allows the dst pointer to be null which will calculate the length
729 * of the would-be result and return it.
731 len = vsnprintf(NULL, 0, cmd, ap)+1;
732 buf = (char *)xmalloc(len);
733 len = vsnprintf(buf, len, cmd, ap);
734 error = send_command(pwm, result, buf);
735 xfree(buf);
736 return error;
740 * Avoid sending the BYE command here. libassuan will close the file
741 * descriptor and release the assuan context. Use pwmd_close() instead.
743 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
745 va_list ap;
746 gpg_error_t error;
748 if (!pwm || !cmd)
749 return GPG_ERR_INV_ARG;
751 *result = NULL;
752 va_start(ap, cmd);
753 error = pwmd_command_ap(pwm, result, cmd, ap);
754 va_end(ap);
755 return error;
758 #ifdef USE_PINENTRY
759 static gpg_error_t do_getpin(pwm_t *pwm, char **result)
761 if (gtimeout) {
762 signal(SIGALRM, catchsig);
763 alarm(1);
766 *result = NULL;
767 return pinentry_command(pwm, result, "GETPIN");
770 static gpg_error_t getpin(pwm_t *pwm, char **result, int *try_n, int which)
772 int pin_try = *try_n;
773 gpg_error_t error;
775 getpin_again:
776 *try_n = pin_try;
778 if (pin_try == -1) {
779 error = set_pinentry_strings(pwm, which);
781 if (error) {
782 pinentry_disconnect(pwm);
783 return error;
786 else {
787 if (pwm->pinentry_tries-1 != pin_try) {
788 error = set_pinentry_strings(pwm, 1);
790 if (error) {
791 pinentry_disconnect(pwm);
792 return error;
797 error = do_getpin(pwm, result);
800 * Since there was input cancel any timeout setting.
802 alarm(0);
804 if (error) {
805 if (error == GPG_ERR_CANCELED)
806 return GPG_ERR_CANCELED;
808 if (pin_try != -1 && pin_try--)
809 goto getpin_again;
811 if (pwm->pctx)
812 pinentry_disconnect(pwm);
814 *try_n = pin_try;
815 return error;
818 return 0;
820 #endif
822 gpg_error_t pwmd_open_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
824 gpg_error_t error;
826 #ifndef USE_PINENTRY
827 return GPG_ERR_NOT_IMPLEMENTED;
828 #endif
830 if (!pwm || !pw || !pw->filename[0])
831 return GPG_ERR_INV_ARG;
833 close(pw->fd);
835 if (pw->error) {
836 error = pw->error;
837 goto fail;
840 if (pwm->filename)
841 xfree(pwm->filename);
843 pwm->filename = xstrdup(pw->filename);
844 memset(pw, 0, sizeof(pwmd_nb_status_t));
845 return 0;
847 fail:
848 memset(pw, 0, sizeof(pwmd_nb_status_t));
849 return error;
852 static gpg_error_t do_open_command(pwm_t *pwm, const char *filename, char *password)
854 char buf[ASSUAN_LINELENGTH];
855 gpg_error_t error;
856 char *result = NULL;
858 snprintf(buf, sizeof(buf), "OPEN %s %s", filename, password ? password : "");
859 error = send_command(pwm, &result, buf);
860 memset(buf, 0, sizeof(buf));
862 if (error && result)
863 xfree(result);
865 return error;
868 static int do_pwmd_open(pwm_t *pwm, gpg_error_t *error, const char *filename,
869 int nb, int timeout)
871 char *result = NULL;
872 char *password = NULL;
873 char path[PATH_MAX];
874 #ifdef USE_PINENTRY
875 int pin_try;
876 #endif
878 if (!pwm || !filename || !*filename) {
879 *error = GPG_ERR_INV_ARG;
880 return nb ? -1 : 1;
883 #ifdef USE_PINENTRY
884 pin_try = pwm->pinentry_tries - 1;
885 #endif
888 * Avoid calling pinentry if the password is cached on the server or if
889 * this is a new file.
891 *error = pwmd_command(pwm, &result, "GETCONFIG data_directory");
893 if (*error)
894 return nb ? -1 : 1;
896 snprintf(path, sizeof(path), "%s/%s", result, filename);
897 pwmd_free_result(result);
899 if (access(path, R_OK) == -1) {
900 if (errno == ENOENT)
901 goto gotpassword;
904 *error = pwmd_command(pwm, &result, "ISCACHED %s", filename);
906 if (*error == EPWMD_CACHE_NOT_FOUND) {
907 if (pwm->passfunc) {
908 password = pwm->passfunc(pwm, pwm->passdata);
909 goto gotpassword;
912 #ifdef USE_PINENTRY
914 * Get the password from pinentry.
916 if (pwm->use_pinentry) {
918 * Nonblocking is wanted. fork() then return a file descriptor
919 * that the client can use to read() from.
921 if (nb) {
922 int p[2];
923 pid_t pid;
924 pwmd_nb_status_t pw;
926 if (pipe(p) == -1) {
927 *error = gpg_error_from_syserror();
928 return -1;
931 #ifdef WITH_LIBPTH
932 pid = pth_fork();
933 #else
934 pid = fork();
935 #endif
937 switch (pid) {
938 case 0:
939 close(p[0]);
940 strncpy(pw.filename, filename, sizeof(pw.filename));
941 pw.filename[sizeof(pw.filename)-1] = 0;
942 pw.fd = p[0];
944 if (timeout > 0) {
945 gpwm = pwm;
946 gtimeout = timeout;
947 gelapsed = 0;
950 getpin_nb_again:
951 *error = getpin(pwm, &password, &pin_try, 0);
953 if (*error) {
954 getpin_nb_fail:
955 if (pwm->pctx)
956 pinentry_disconnect(pwm);
958 if (gtimeout && gelapsed >= gtimeout)
959 *error = GPG_ERR_TIMEOUT;
961 pw.error = *error;
962 #ifdef WITH_LIBPTH
963 pth_write(p[1], &pw, sizeof(pw));
964 #else
965 write(p[1], &pw, sizeof(pw));
966 #endif
967 close(p[1]);
968 _exit(1);
972 * Don't count the time it takes to open the file
973 * which may have many iterations.
975 signal(SIGALRM, SIG_DFL);
976 *error = do_open_command(pwm, filename, password);
978 if (timeout)
979 signal(SIGALRM, catchsig);
981 if (pwm->pctx && *error == EPWMD_BADKEY) {
982 if (pin_try-- > 0)
983 goto getpin_nb_again;
985 goto getpin_nb_fail;
988 pinentry_disconnect(pwm);
989 pw.error = 0;
990 #ifdef WITH_LIBPTH
991 pth_write(p[1], &pw, sizeof(pw));
992 #else
993 write(p[1], &pw, sizeof(pw));
994 #endif
995 close(p[1]);
996 _exit(0);
997 break;
998 case -1:
999 *error = gpg_error_from_syserror();
1000 close(p[0]);
1001 close(p[1]);
1002 return -1;
1003 default:
1004 break;
1007 close(p[1]);
1008 *error = 0;
1009 return p[0];
1012 else {
1013 #endif
1015 * Not using pinentry and the file was not found
1016 * in the cache.
1018 password = pwm->password;
1019 #ifdef USE_PINENTRY
1021 #endif
1023 else if (*error)
1024 return nb ? -1 : 1;
1026 gotpassword:
1027 *error = do_open_command(pwm, filename, password);
1030 * Keep the user defined password set with pwmd_setopt(). The password may
1031 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1033 if (!pwm->passfunc && password && password != pwm->password)
1034 xfree(password);
1036 #ifdef USE_PINENTRY
1037 if (*error == EPWMD_BADKEY) {
1038 if (pin_try-- > 0 && !nb) {
1039 *error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
1040 N_("Invalid passphrase, please try again."));
1042 if (*error)
1043 return 1;
1045 goto gotpassword;
1048 if (nb)
1049 pinentry_disconnect(pwm);
1051 return nb ? -1 : 1;
1053 #endif
1055 if (!*error) {
1056 if (pwm->filename)
1057 xfree(pwm->filename);
1059 pwm->filename = xstrdup(filename);
1063 * The file is cached or the file is a new file.
1065 if (nb)
1066 return *error ? -1 : -2;
1068 return *error ? 1 : 0;
1071 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
1073 gpg_error_t error;
1075 do_pwmd_open(pwm, &error, filename, 0, 0);
1076 return error;
1079 int pwmd_open_nb(pwm_t *pwm, gpg_error_t *error, const char *filename,
1080 int timeout)
1082 #ifndef USE_PINENTRY
1083 *error = GPG_ERR_NOT_IMPLEMENTED;
1084 return -1;
1085 #else
1086 return do_pwmd_open(pwm, error, filename, 1, timeout);
1087 #endif
1090 #ifdef USE_PINENTRY
1091 static gpg_error_t do_save_getpin(pwm_t *pwm, char **password)
1093 int confirm = 0;
1094 gpg_error_t error;
1095 char *result = NULL;
1096 int pin_try = -1;
1098 again:
1099 error = getpin(pwm, &result, &pin_try, confirm ? 2 : 0);
1101 if (error) {
1102 if (pwm->pctx)
1103 pinentry_disconnect(pwm);
1105 if (*password)
1106 xfree(*password);
1108 return error;
1111 if (!confirm++) {
1112 *password = result;
1113 goto again;
1116 if (strcmp(*password, result)) {
1117 xfree(*password);
1118 xfree(result);
1119 pinentry_disconnect(pwm);
1120 error = EPWMD_BADKEY;
1121 return error;
1124 xfree(result);
1125 pinentry_disconnect(pwm);
1126 return 0;
1128 #endif
1130 static gpg_error_t do_save_command(pwm_t *pwm, char *password)
1132 char buf[ASSUAN_LINELENGTH];
1133 gpg_error_t error;
1134 char *result = NULL;
1136 snprintf(buf, sizeof(buf), "SAVE %s", password ? password : "");
1137 error = send_command(pwm, &result, buf);
1138 memset(&buf, 0, sizeof(buf));
1140 if (error && result)
1141 xfree(result);
1143 return error;
1146 gpg_error_t pwmd_save_nb_finalize(pwm_t *pwm, pwmd_nb_status_t *pw)
1148 gpg_error_t rc;
1150 #ifndef USE_PINENTRY
1151 return GPG_ERR_NOT_IMPLEMENTED;
1152 #endif
1154 if (!pwm || !pw || !pw->filename[0])
1155 return GPG_ERR_INV_ARG;
1157 close(pw->fd);
1158 rc = pw->error;
1159 memset(pw, 0, sizeof(pwmd_nb_status_t));
1160 return rc;
1163 static int do_pwmd_save(pwm_t *pwm, gpg_error_t *error, int nb)
1165 char *result = NULL;
1166 char *password = NULL;
1168 if (!pwm) {
1169 *error = GPG_ERR_INV_ARG;
1170 return nb ? -1 : 1;
1173 if (pwm->use_pinentry || pwm->passfunc) {
1174 *error = pwmd_command(pwm, &result, "ISCACHED %s", pwm->filename);
1176 if (*error == EPWMD_CACHE_NOT_FOUND) {
1177 if (pwm->passfunc)
1178 password = (*pwm->passfunc)(pwm, pwm->passdata);
1179 #ifdef USE_PINENTRY
1180 else if (pwm->use_pinentry) {
1181 if (nb) {
1182 int p[2];
1183 pid_t pid;
1184 pwmd_nb_status_t pw;
1186 if (pipe(p) == -1) {
1187 *error = gpg_error_from_syserror();
1188 return -1;
1191 #ifdef WITH_LIBPTH
1192 pid = pth_fork();
1193 #else
1194 pid = fork();
1195 #endif
1197 switch (pid) {
1198 case 0:
1199 close(p[0]);
1200 strncpy(pw.filename, pwm->filename, sizeof(pw.filename));
1201 pw.filename[sizeof(pw.filename)-1] = 0;
1202 pw.fd = p[0];
1204 do {
1205 password = NULL;
1206 *error = do_save_getpin(pwm, &password);
1207 } while (*error == EPWMD_BADKEY);
1209 if (*error) {
1210 if (pwm->pctx)
1211 pinentry_disconnect(pwm);
1213 pw.error = *error;
1214 #ifdef WITH_LIBPTH
1215 pth_write(p[1], &pw, sizeof(pw));
1216 #else
1217 write(p[1], &pw, sizeof(pw));
1218 #endif
1219 close(p[1]);
1220 _exit(1);
1223 *error = do_save_command(pwm, password);
1224 pinentry_disconnect(pwm);
1225 pw.error = *error;
1226 #ifdef WITH_LIBPTH
1227 pth_write(p[1], &pw, sizeof(pw));
1228 #else
1229 write(p[1], &pw, sizeof(pw));
1230 #endif
1231 close(p[1]);
1232 _exit(0);
1233 break;
1234 case -1:
1235 *error = gpg_error_from_syserror();
1236 close(p[0]);
1237 close(p[1]);
1238 return -1;
1239 default:
1240 break;
1243 close(p[1]);
1244 *error = 0;
1245 return p[0];
1248 *error = do_save_getpin(pwm, &password);
1250 if (*error)
1251 return 1;
1253 #endif
1255 else {
1256 if (*error)
1257 return nb ? -1 : 1;
1260 else
1261 password = pwm->password;
1263 *error = do_save_command(pwm, password);
1265 if (!pwm->passfunc && password && password != pwm->password)
1266 xfree(password);
1268 if (nb)
1269 return *error ? -1 : -2;
1271 return *error ? 1 : 0;
1274 int pwmd_save_nb(pwm_t *pwm, gpg_error_t *error)
1276 #ifndef USE_PINENTRY
1277 *error = GPG_ERR_NOT_IMPLEMENTED;
1278 return -1;
1279 #else
1280 return do_pwmd_save(pwm, error, 1);
1281 #endif
1284 gpg_error_t pwmd_save(pwm_t *pwm)
1286 gpg_error_t error;
1288 do_pwmd_save(pwm, &error, 0);
1289 return error;
1292 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
1294 va_list ap;
1295 int n = va_arg(ap, int);
1296 char *result;
1297 char *arg1;
1298 gpg_error_t error = 0;
1300 if (!pwm)
1301 return GPG_ERR_INV_ARG;
1303 va_start(ap, opt);
1305 switch (opt) {
1306 case PWMD_OPTION_STATUS_FUNC:
1307 pwm->status_func = va_arg(ap, pwmd_status_fn);
1308 break;
1309 case PWMD_OPTION_STATUS_DATA:
1310 pwm->status_data = va_arg(ap, void *);
1311 break;
1312 case PWMD_OPTION_PASSWORD_FUNC:
1313 pwm->passfunc = va_arg(ap, pwmd_password_fn);
1314 break;
1315 case PWMD_OPTION_PASSWORD_DATA:
1316 pwm->passdata = va_arg(ap, void *);
1317 break;
1318 case PWMD_OPTION_PASSWORD:
1319 arg1 = va_arg(ap, char *);
1321 if (pwm->password)
1322 xfree(pwm->password);
1324 pwm->password = xstrdup(arg1);
1325 break;
1326 case PWMD_OPTION_PINENTRY:
1327 n = va_arg(ap, int);
1329 if (n != 0 && n != 1) {
1330 va_end(ap);
1331 error = GPG_ERR_INV_VALUE;
1333 else {
1334 pwm->use_pinentry = n;
1335 error = pwmd_command(pwm, &result, "OPTION PINENTRY=%i",
1336 !pwm->use_pinentry);
1338 break;
1339 #ifdef USE_PINENTRY
1340 case PWMD_OPTION_PINENTRY_TRIES:
1341 n = va_arg(ap, int);
1343 if (n <= 0) {
1344 va_end(ap);
1345 error = GPG_ERR_INV_VALUE;
1347 else
1348 pwm->pinentry_tries = n;
1349 break;
1350 #endif
1351 case PWMD_OPTION_PINENTRY_TIMEOUT:
1352 n = va_arg(ap, int);
1354 if (n < 0) {
1355 va_end(ap);
1356 error = GPG_ERR_INV_VALUE;
1358 else
1359 pwm->pinentry_timeout = n;
1361 if (!pwm->use_pinentry)
1362 error = pwmd_command(pwm, &result, "OPTION TIMEOUT=%i",
1363 pwm->pinentry_timeout);
1364 break;
1365 case PWMD_OPTION_PINENTRY_PATH:
1366 if (pwm->pinentry_path)
1367 xfree(pwm->pinentry_path);
1369 pwm->pinentry_path = xstrdup(va_arg(ap, char *));
1371 if (!pwm->use_pinentry)
1372 error = pwmd_command(pwm, &result, "OPTION PATH=%s",
1373 pwm->pinentry_path);
1374 break;
1375 case PWMD_OPTION_PINENTRY_TTY:
1376 if (pwm->pinentry_tty)
1377 xfree(pwm->pinentry_tty);
1379 pwm->pinentry_tty = xstrdup(va_arg(ap, char *));
1381 if (!pwm->use_pinentry)
1382 error = pwmd_command(pwm, &result, "OPTION TTY=%s",
1383 pwm->pinentry_tty);
1384 break;
1385 case PWMD_OPTION_PINENTRY_DISPLAY:
1386 if (pwm->pinentry_display)
1387 xfree(pwm->pinentry_display);
1389 pwm->pinentry_display = xstrdup(va_arg(ap, char *));
1391 if (!pwm->use_pinentry)
1392 error = pwmd_command(pwm, &result, "OPTION DISPLAY=%s",
1393 pwm->pinentry_display);
1394 break;
1395 case PWMD_OPTION_PINENTRY_TERM:
1396 if (pwm->pinentry_term)
1397 xfree(pwm->pinentry_term);
1399 pwm->pinentry_term = xstrdup(va_arg(ap, char *));
1401 if (!pwm->use_pinentry)
1402 error = pwmd_command(pwm, &result, "OPTION TTYTYPE=%s",
1403 pwm->pinentry_term);
1404 break;
1405 case PWMD_OPTION_PINENTRY_TITLE:
1406 if (pwm->title)
1407 xfree(pwm->title);
1409 pwm->title = percent_escape(va_arg(ap, char *));
1411 if (!pwm->use_pinentry)
1412 error = pwmd_command(pwm, &result, "OPTION TITLE=%s",
1413 pwm->title);
1414 break;
1415 case PWMD_OPTION_PINENTRY_PROMPT:
1416 if (pwm->prompt)
1417 xfree(pwm->prompt);
1419 pwm->prompt = percent_escape(va_arg(ap, char *));
1421 if (!pwm->use_pinentry)
1422 error = pwmd_command(pwm, &result, "OPTION PROMPT=%s",
1423 pwm->prompt);
1424 break;
1425 case PWMD_OPTION_PINENTRY_DESC:
1426 if (pwm->desc)
1427 xfree(pwm->desc);
1429 pwm->desc = percent_escape(va_arg(ap, char *));
1431 if (!pwm->use_pinentry)
1432 error = pwmd_command(pwm, &result, "OPTION DESC=%s",
1433 pwm->desc);
1434 break;
1435 case PWMD_OPTION_PINENTRY_LC_CTYPE:
1436 if (pwm->lcctype)
1437 xfree(pwm->lcctype);
1439 pwm->lcctype = xstrdup(va_arg(ap, char *));
1441 if (!pwm->use_pinentry)
1442 error = pwmd_command(pwm, &result, "OPTION LC_CTYPE=%s",
1443 pwm->lcctype);
1444 break;
1445 case PWMD_OPTION_PINENTRY_LC_MESSAGES:
1446 if (pwm->lcmessages)
1447 xfree(pwm->lcmessages);
1449 pwm->lcmessages = xstrdup(va_arg(ap, char *));
1451 if (!pwm->use_pinentry)
1452 error = pwmd_command(pwm, &result, "OPTION LC_MESSAGES=%s",
1453 pwm->lcmessages);
1454 break;
1455 default:
1456 error = GPG_ERR_NOT_IMPLEMENTED;
1457 break;
1460 va_end(ap);
1461 return error;
1465 * Prevent requiring assuan.h when setting ctx. The ctx is really an
1466 * assuan_context_t *.
1468 gpg_error_t pwmd_assuan_ctx(pwm_t *pwm, void *ctx, int *fd)
1470 if (!pwm)
1471 return GPG_ERR_INV_ARG;
1473 ctx = pwm->ctx;
1474 *fd = pwm->fd;
1475 return 0;