1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
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
26 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <sys/select.h>
57 #define N_(msgid) dgettext("libpwmd", msgid)
63 #define xrealloc realloc
64 #define xmalloc malloc
65 #define xstrdup strdup
66 #define xcalloc calloc
73 static int gelapsed
, gtimeout
;
74 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
75 static gpg_error_t global_error
;
78 const char *pwmd_strerror(gpg_error_t e
)
80 gpg_err_code_t code
= gpg_err_code(e
);
82 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
86 return N_("Unknown error");
88 return N_("No cache slots available");
90 return N_("Recursion loop");
92 return N_("No file is open");
94 return N_("General LibXML error");
96 return N_("File modified");
100 return gpg_strerror(e
);
103 gpg_error_t
pwmd_init()
105 static int initialized
;
111 bindtextdomain("libpwmd", LOCALEDIR
);
114 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
115 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
120 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
123 char *socketpath
= NULL
;
126 assuan_context_t ctx
;
131 pw
= getpwuid(getuid());
132 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
133 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
136 socketpath
= xstrdup(path
);
138 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
146 n
= assuan_get_active_fds(ctx
, 0, active
, sizeof(active
));
148 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
149 *error
= gpg_error_from_errno(errno
);
150 assuan_disconnect(ctx
);
154 pwm
->fd
= n
<= 0 ? -1 : dup(active
[0]);
157 fcntl(pwm
->fd
, F_SETFL
, O_NONBLOCK
);
162 pwm
->pinentry_tries
= 3;
170 gpg_error_t
pwmd_pending_line(pwm_t
*pwm
, char **line
, size_t *len
)
173 return GPG_ERR_INV_ARG
;
175 if (assuan_pending_line(pwm
->ctx
))
176 return assuan_read_line(pwm
->ctx
, line
, len
);
178 return GPG_ERR_NO_DATA
;
181 void pwmd_close(pwm_t
*pwm
)
187 assuan_disconnect(pwm
->ctx
);
190 xfree(pwm
->password
);
201 if (pwm
->pinentry_tty
)
202 xfree(pwm
->pinentry_tty
);
204 if (pwm
->pinentry_display
)
205 xfree(pwm
->pinentry_display
);
207 if (pwm
->pinentry_term
)
208 xfree(pwm
->pinentry_term
);
211 xfree(pwm
->filename
);
216 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
218 membuf_t
*mem
= (membuf_t
*)data
;
224 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
228 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
233 void pwmd_free_result(void *data
)
238 static int _inquire_cb(void *data
, const char *keyword
)
240 pwm_t
*pwm
= (pwm_t
*)data
;
242 int flags
= fcntl(pwm
->fd
, F_GETFL
);
244 /* Shouldn't get this far without a callback. */
245 if (!pwm
->inquire_func
)
246 return GPG_ERR_INV_ARG
;
249 * Since the socket file descriptor is probably set to non-blocking, set to
250 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
251 * asynchronous INQUIRE is supported by either libassuan or a later
254 fcntl(pwm
->fd
, F_SETFL
, 0);
261 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
, &len
);
262 rc
= gpg_err_code(rc
);
264 if (rc
== GPG_ERR_EOF
|| !rc
) {
265 if (len
<= 0 || !result
|| !*result
) {
270 arc
= assuan_send_data(pwm
->ctx
, result
, len
);
272 if (rc
== GPG_ERR_EOF
) {
283 fcntl(pwm
->fd
, F_SETFL
, flags
);
287 gpg_error_t
pwmd_finalize(pwm_t
*pwm
)
289 if (!pwm
|| pwm
->fd
< 0)
290 return GPG_ERR_INV_ARG
;
292 pwm
->state
= ASYNC_INIT
;
294 pwm
->is_open_cmd
= 0;
298 static gpg_error_t
do_nb_command(pwm_t
*pwm
, const char *cmd
, const char *arg
)
302 size_t len
= strlen(cmd
) + 2;
304 len
+= arg
? strlen(arg
) : 0;
306 if (pwm
->state
!= ASYNC_INIT
)
307 return GPG_ERR_UNEXPECTED
;
309 buf
= (char *)xmalloc(len
);
312 rc
= gpg_error_from_errno(ENOMEM
);
316 snprintf(buf
, len
, "%s %s", cmd
, arg
? arg
: "");
317 rc
= assuan_write_line(pwm
->ctx
, buf
);
321 pwm
->state
= ASYNC_PROCESS
;
327 gpg_error_t
pwmd_open_async(pwm_t
*pwm
, const char *filename
)
329 if (!pwm
|| !filename
)
330 return GPG_ERR_INV_ARG
;
332 /* For pinentry retries. */
333 if (!pwm
->is_open_cmd
) {
335 xfree(pwm
->filename
);
337 pwm
->filename
= xstrdup(filename
);
340 pwm
->is_open_cmd
= 1;
341 return do_nb_command(pwm
, "OPEN", filename
);
344 gpg_error_t
pwmd_save_async(pwm_t
*pwm
)
347 return GPG_ERR_INV_ARG
;
349 return do_nb_command(pwm
, "SAVE", NULL
);
352 static gpg_error_t
parse_assuan_line(pwm_t
*pwm
)
358 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
361 if (line
[0] == 'O' && line
[1] == 'K' &&
362 (line
[2] == 0 || line
[2] == ' ')) {
363 pwm
->state
= ASYNC_DONE
;
365 else if (line
[0] == '#') {
367 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' ')) {
368 if (pwm
->status_func
) {
369 pwm
->status_func(pwm
->status_data
,
370 line
[1] == 0 ? line
+1 : line
+2);
373 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
374 (line
[3] == 0 || line
[3] == ' ')) {
377 pwm
->state
= ASYNC_DONE
;
384 pwmd_async_t
pwmd_process(pwm_t
*pwm
, gpg_error_t
*rc
)
388 struct timeval tv
= {0, 0};
392 if (!pwm
|| pwm
->fd
< 0) {
393 *rc
= GPG_ERR_INV_ARG
;
396 else if (pwm
->state
== ASYNC_DONE
)
398 else if (pwm
->state
== ASYNC_INIT
) {
399 *rc
= GPG_ERR_UNEXPECTED
;
404 FD_SET(pwm
->fd
, &rfds
);
406 n
= pth_select(pwm
->fd
+1, &rfds
, NULL
, NULL
, &tv
);
408 n
= select(pwm
->fd
+1, &rfds
, NULL
, NULL
, &tv
);
412 if (FD_ISSET(pwm
->fd
, &rfds
))
413 *rc
= parse_assuan_line(pwm
);
416 while (!*rc
&& assuan_pending_line(pwm
->ctx
))
417 *rc
= parse_assuan_line(pwm
);
419 if (pwm
->is_open_cmd
&& gpg_err_code(*rc
) == EPWMD_BADKEY
&&
420 ++pwm
->ntries
< pwm
->pinentry_tries
) {
421 pwm
->state
= ASYNC_INIT
;
422 *rc
= pwmd_open_async(pwm
, pwm
->filename
);
428 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
429 char **result
, const char *cmd
)
437 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
438 pwm
->status_func
, pwm
->status_data
);
448 mem_realloc_cb(&data
, "", 1);
449 *result
= (char *)data
.buf
;
453 return gpg_err_code(rc
);
456 gpg_error_t
pwmd_inquire(pwm_t
*pwm
, const char *cmd
, pwmd_inquire_fn fn
,
459 if (!pwm
|| !cmd
|| !fn
)
460 return GPG_ERR_INV_ARG
;
462 pwm
->inquire_func
= fn
;
463 pwm
->inquire_data
= data
;
464 return assuan_command(pwm
, pwm
->ctx
, NULL
, cmd
);
467 gpg_error_t
pwmd_terminate_pinentry(pwm_t
*pwm
)
470 return GPG_ERR_NOT_IMPLEMENTED
;
472 if (!pwm
|| pwm
->pid
== -1)
473 return GPG_ERR_INV_ARG
;
475 if (kill(pwm
->pid
, 0) == 0) {
476 if (kill(pwm
->pid
, SIGTERM
) == -1) {
477 if (kill(pwm
->pid
, SIGKILL
) == -1)
478 return gpg_error_from_errno(errno
);
481 pwm
->pin_error
= GPG_ERR_TIMEOUT
;
484 return gpg_error_from_errno(errno
);
491 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
494 char tmp
[ASSUAN_LINELENGTH
];
498 pwm
->title
= xstrdup(N_("LibPWMD"));
501 pwm
->prompt
= xstrdup(N_("Passphrase:"));
503 if (!pwm
->desc
&& !which
)
504 pwm
->desc
= xstrdup(N_("Enter a passphrase."));
507 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Invalid passphrase, please try again."));
510 else if (which
== 2) {
511 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Please type the passphrase again for confirmation."));
515 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
516 sprintf(buf
, "SETERROR %s", pwm
->desc
);
519 error
= pinentry_command(pwm
, NULL
, buf
);
525 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
526 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
527 error
= pinentry_command(pwm
, NULL
, buf
);
533 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
534 sprintf(buf
, "SETDESC %s", pwm
->title
);
535 error
= pinentry_command(pwm
, NULL
, buf
);
540 static void update_pinentry_settings(pwm_t
*pwm
)
544 struct passwd
*pw
= getpwuid(getuid());
547 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
->pw_dir
);
549 if ((fp
= fopen(buf
, "r")) == NULL
)
552 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
553 char name
[32], val
[256];
555 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
558 if (strcasecmp(name
, "TTYNAME") == 0) {
559 xfree(pwm
->pinentry_tty
);
560 pwm
->pinentry_tty
= xstrdup(val
);
562 else if (strcasecmp(name
, "TTYTYPE") == 0) {
563 xfree(pwm
->pinentry_term
);
564 pwm
->pinentry_term
= xstrdup(val
);
566 else if (strcasecmp(name
, "DISPLAY") == 0) {
567 xfree(pwm
->pinentry_display
);
568 pwm
->pinentry_display
= xstrdup(val
);
570 else if (strcasecmp(name
, "PATH") == 0) {
571 xfree(pwm
->pinentry_path
);
572 pwm
->pinentry_path
= xstrdup(val
);
579 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
582 assuan_context_t ctx
;
583 int child_list
[] = {-1};
584 char *display
= getenv("DISPLAY");
586 int have_display
= 0;
589 update_pinentry_settings(pwm
);
591 if (pwm
->pinentry_display
|| display
)
594 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
597 return gpg_error_from_errno(errno
);
600 if (!have_display
&& !tty
)
601 return GPG_ERR_ENOTTY
;
603 argv
[0] = "pinentry";
604 argv
[1] = have_display
? "--display" : "--ttyname";
605 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
609 argv
[3] = "--ttytype";
610 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
614 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
619 pwm
->pid
= assuan_get_pid(ctx
);
621 return set_pinentry_strings(pwm
, 0);
624 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
629 n
= launch_pinentry(pwm
);
635 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
638 static void pinentry_disconnect(pwm_t
*pwm
)
641 assuan_disconnect(pwm
->pctx
);
648 * Only called from a child process.
650 static void catchsig(int sig
)
654 if (gelapsed
++ >= gtimeout
) {
655 global_error
= pwmd_terminate_pinentry(gpwm
);
658 global_error
= GPG_ERR_TIMEOUT
;
671 * Borrowed from libassuan.
673 static char *percent_escape(const char *atext
)
675 const unsigned char *s
;
676 int len
= strlen(atext
) * 3 + 1;
677 char *buf
= (char *)xmalloc(len
), *p
= buf
;
682 for (s
=(const unsigned char *)atext
; *s
; s
++) {
684 sprintf (p
, "%%%02X", *s
);
696 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
699 return GPG_ERR_INV_ARG
;
701 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
705 * Avoid sending the BYE command here. libassuan will close the file
706 * descriptor and release the assuan context. Use pwmd_close() instead.
708 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
716 return GPG_ERR_INV_ARG
;
721 * C99 allows the dst pointer to be null which will calculate the length
722 * of the result and return it.
724 len
= vsnprintf(NULL
, 0, cmd
, ap
);
725 buf
= (char *)xmalloc(len
+ 1);
726 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
728 error
= send_command(pwm
, result
, buf
);
734 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
737 signal(SIGALRM
, catchsig
);
742 return pinentry_command(pwm
, result
, "GETPIN");
745 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
747 int pin_try
= *try_n
;
754 error
= set_pinentry_strings(pwm
, which
);
757 pinentry_disconnect(pwm
);
762 if (pwm
->pinentry_tries
-1 != pin_try
) {
763 error
= set_pinentry_strings(pwm
, 1);
766 pinentry_disconnect(pwm
);
772 error
= do_getpin(pwm
, result
);
775 * Since there was input cancel any timeout setting.
780 if (error
== GPG_ERR_CANCELED
)
781 return GPG_ERR_CANCELED
;
783 if (pin_try
!= -1 && pin_try
--)
787 pinentry_disconnect(pwm
);
797 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
802 return GPG_ERR_NOT_IMPLEMENTED
;
805 if (!pwm
|| !pw
|| !pw
->filename
[0])
806 return GPG_ERR_INV_ARG
;
816 xfree(pwm
->filename
);
818 pwm
->filename
= xstrdup(pw
->filename
);
819 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
823 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
827 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
829 char buf
[ASSUAN_LINELENGTH
];
833 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
834 error
= send_command(pwm
, &result
, buf
);
835 memset(buf
, 0, sizeof(buf
));
843 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
847 char *password
= NULL
;
853 if (!pwm
|| !filename
|| !*filename
) {
854 *error
= GPG_ERR_INV_ARG
;
859 pin_try
= pwm
->pinentry_tries
- 1;
863 * Avoid calling pinentry if the password is cached on the server or if
864 * this is a new file.
866 *error
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
871 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
872 pwmd_free_result(result
);
874 if (access(path
, R_OK
) == -1) {
879 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
881 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
883 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
889 * Get the password from pinentry.
891 if (pwm
->use_pinentry
) {
893 * Nonblocking is wanted. fork() then return a file descriptor
894 * that the client can use to read() from.
902 *error
= gpg_error_from_syserror();
915 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
916 pw
.filename
[sizeof(pw
.filename
)-1] = 0;
926 *error
= getpin(pwm
, &password
, &pin_try
, 0);
931 pinentry_disconnect(pwm
);
933 if (gtimeout
&& gelapsed
>= gtimeout
)
934 *error
= GPG_ERR_TIMEOUT
;
938 pth_write(p
[1], &pw
, sizeof(pw
));
940 write(p
[1], &pw
, sizeof(pw
));
947 * Don't count the time it takes to open the file
948 * which may have many iterations.
950 signal(SIGALRM
, SIG_DFL
);
951 *error
= do_open_command(pwm
, filename
, password
);
954 signal(SIGALRM
, catchsig
);
956 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
958 goto getpin_nb_again
;
963 pinentry_disconnect(pwm
);
966 pth_write(p
[1], &pw
, sizeof(pw
));
968 write(p
[1], &pw
, sizeof(pw
));
974 *error
= gpg_error_from_syserror();
990 * Not using pinentry and the file was not found
993 password
= pwm
->password
;
1002 *error
= do_open_command(pwm
, filename
, password
);
1005 * Keep the user defined password set with pwmd_setopt(). The password may
1006 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1008 if (password
&& password
!= pwm
->password
)
1012 if (*error
== EPWMD_BADKEY
) {
1013 if (pin_try
-- > 0 && !nb
) {
1014 *error
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
1015 N_("Invalid passphrase, please try again."));
1024 pinentry_disconnect(pwm
);
1032 xfree(pwm
->filename
);
1034 pwm
->filename
= xstrdup(filename
);
1038 * The file is cached or the file is a new file.
1041 return *error
? -1 : -2;
1043 return *error
? 1 : 0;
1046 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
1050 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
1054 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
1057 #ifndef USE_PINENTRY
1058 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1061 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
1066 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
1070 char *result
= NULL
;
1074 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
1078 pinentry_disconnect(pwm
);
1091 if (strcmp(*password
, result
)) {
1094 pinentry_disconnect(pwm
);
1095 error
= EPWMD_BADKEY
;
1100 pinentry_disconnect(pwm
);
1105 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
1107 char buf
[ASSUAN_LINELENGTH
];
1109 char *result
= NULL
;
1111 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
1112 error
= send_command(pwm
, &result
, buf
);
1113 memset(&buf
, 0, sizeof(buf
));
1115 if (error
&& result
)
1121 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
1125 #ifndef USE_PINENTRY
1126 return GPG_ERR_NOT_IMPLEMENTED
;
1129 if (!pwm
|| !pw
|| !pw
->filename
[0])
1130 return GPG_ERR_INV_ARG
;
1134 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
1138 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
1140 char *result
= NULL
;
1141 char *password
= NULL
;
1144 *error
= GPG_ERR_INV_ARG
;
1148 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
1149 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
1151 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
1153 if (pwm
->use_pinentry
) {
1157 pwmd_nb_status_t pw
;
1159 if (pipe(p
) == -1) {
1160 *error
= gpg_error_from_syserror();
1173 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
1174 pw
.filename
[sizeof(pw
.filename
)-1] = 0;
1179 *error
= do_save_getpin(pwm
, &password
);
1180 } while (*error
== EPWMD_BADKEY
);
1184 pinentry_disconnect(pwm
);
1188 pth_write(p
[1], &pw
, sizeof(pw
));
1190 write(p
[1], &pw
, sizeof(pw
));
1196 *error
= do_save_command(pwm
, password
);
1197 pinentry_disconnect(pwm
);
1200 pth_write(p
[1], &pw
, sizeof(pw
));
1202 write(p
[1], &pw
, sizeof(pw
));
1208 *error
= gpg_error_from_syserror();
1221 *error
= do_save_getpin(pwm
, &password
);
1229 password
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1240 password
= pwm
->password
;
1242 *error
= do_save_command(pwm
, password
);
1244 if (password
&& password
!= pwm
->password
)
1248 return *error
? -1 : -2;
1250 return *error
? 1 : 0;
1253 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1255 #ifndef USE_PINENTRY
1256 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1259 return do_pwmd_save(pwm
, error
, 1);
1263 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1267 do_pwmd_save(pwm
, &error
, 0);
1271 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1275 int n
= va_arg(ap
, int);
1279 gpg_error_t error
= 0;
1282 return GPG_ERR_INV_ARG
;
1287 case PWMD_OPTION_STATUS_FUNC
:
1288 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1290 case PWMD_OPTION_STATUS_DATA
:
1291 pwm
->status_data
= va_arg(ap
, void *);
1293 case PWMD_OPTION_PASSWORD_FUNC
:
1294 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1296 case PWMD_OPTION_PASSWORD_DATA
:
1297 pwm
->passdata
= va_arg(ap
, void *);
1299 case PWMD_OPTION_PASSWORD
:
1300 arg1
= va_arg(ap
, char *);
1303 xfree(pwm
->password
);
1305 pwm
->password
= xstrdup(arg1
);
1308 case PWMD_OPTION_PINENTRY
:
1309 n
= va_arg(ap
, int);
1311 if (n
!= 0 && n
!= 1) {
1313 error
= GPG_ERR_INV_VALUE
;
1316 pwm
->use_pinentry
= n
;
1317 error
= pwmd_command(pwm
, &result
, "OPTION PINENTRY=%i",
1318 !pwm
->use_pinentry
);
1321 case PWMD_OPTION_PINENTRY_TRIES
:
1322 n
= va_arg(ap
, int);
1326 error
= GPG_ERR_INV_VALUE
;
1329 pwm
->pinentry_tries
= n
;
1331 case PWMD_OPTION_PINENTRY_PATH
:
1332 if (pwm
->pinentry_path
)
1333 xfree(pwm
->pinentry_path
);
1335 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1337 case PWMD_OPTION_PINENTRY_TTY
:
1338 if (pwm
->pinentry_tty
)
1339 xfree(pwm
->pinentry_tty
);
1341 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1343 case PWMD_OPTION_PINENTRY_DISPLAY
:
1344 if (pwm
->pinentry_display
)
1345 xfree(pwm
->pinentry_display
);
1347 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1349 case PWMD_OPTION_PINENTRY_TERM
:
1350 if (pwm
->pinentry_term
)
1351 xfree(pwm
->pinentry_term
);
1353 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1355 case PWMD_OPTION_PINENTRY_TITLE
:
1358 pwm
->title
= percent_escape(va_arg(ap
, char *));
1360 case PWMD_OPTION_PINENTRY_PROMPT
:
1363 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1365 case PWMD_OPTION_PINENTRY_DESC
:
1368 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1371 case PWMD_OPTION_PINENTRY
:
1372 case PWMD_OPTION_PINENTRY_TRIES
:
1373 case PWMD_OPTION_PINENTRY_PATH
:
1374 case PWMD_OPTION_PINENTRY_TTY
:
1375 case PWMD_OPTION_PINENTRY_DISPLAY
:
1376 case PWMD_OPTION_PINENTRY_TERM
:
1377 case PWMD_OPTION_PINENTRY_TITLE
:
1378 case PWMD_OPTION_PINENTRY_PROMPT
:
1379 case PWMD_OPTION_PINENTRY_DESC
:
1380 error
= GPG_ERR_NOT_IMPLEMENTED
;
1384 error
= GPG_ERR_NOT_IMPLEMENTED
;
1392 gpg_error_t
pwmd_assuan_ctx(pwm_t
*pwm
, assuan_context_t
*ctx
, int *fd
)
1395 return GPG_ERR_INV_ARG
;