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
);
704 gpg_error_t
pwmd_command_ap(pwm_t
*pwm
, char **result
, const char *cmd
,
712 * C99 allows the dst pointer to be null which will calculate the length
713 * of the would-be result and return it.
715 len
= vsnprintf(NULL
, 0, cmd
, ap
)+1;
716 buf
= (char *)xmalloc(len
);
717 len
= vsnprintf(buf
, len
, cmd
, ap
);
718 error
= send_command(pwm
, result
, buf
);
724 * Avoid sending the BYE command here. libassuan will close the file
725 * descriptor and release the assuan context. Use pwmd_close() instead.
727 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
733 return GPG_ERR_INV_ARG
;
737 error
= pwmd_command_ap(pwm
, result
, cmd
, ap
);
743 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
746 signal(SIGALRM
, catchsig
);
751 return pinentry_command(pwm
, result
, "GETPIN");
754 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
756 int pin_try
= *try_n
;
763 error
= set_pinentry_strings(pwm
, which
);
766 pinentry_disconnect(pwm
);
771 if (pwm
->pinentry_tries
-1 != pin_try
) {
772 error
= set_pinentry_strings(pwm
, 1);
775 pinentry_disconnect(pwm
);
781 error
= do_getpin(pwm
, result
);
784 * Since there was input cancel any timeout setting.
789 if (error
== GPG_ERR_CANCELED
)
790 return GPG_ERR_CANCELED
;
792 if (pin_try
!= -1 && pin_try
--)
796 pinentry_disconnect(pwm
);
806 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
811 return GPG_ERR_NOT_IMPLEMENTED
;
814 if (!pwm
|| !pw
|| !pw
->filename
[0])
815 return GPG_ERR_INV_ARG
;
825 xfree(pwm
->filename
);
827 pwm
->filename
= xstrdup(pw
->filename
);
828 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
832 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
836 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
838 char buf
[ASSUAN_LINELENGTH
];
842 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
843 error
= send_command(pwm
, &result
, buf
);
844 memset(buf
, 0, sizeof(buf
));
852 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
856 char *password
= NULL
;
862 if (!pwm
|| !filename
|| !*filename
) {
863 *error
= GPG_ERR_INV_ARG
;
868 pin_try
= pwm
->pinentry_tries
- 1;
872 * Avoid calling pinentry if the password is cached on the server or if
873 * this is a new file.
875 *error
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
880 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
881 pwmd_free_result(result
);
883 if (access(path
, R_OK
) == -1) {
888 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
890 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
892 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
898 * Get the password from pinentry.
900 if (pwm
->use_pinentry
) {
902 * Nonblocking is wanted. fork() then return a file descriptor
903 * that the client can use to read() from.
911 *error
= gpg_error_from_syserror();
924 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
925 pw
.filename
[sizeof(pw
.filename
)-1] = 0;
935 *error
= getpin(pwm
, &password
, &pin_try
, 0);
940 pinentry_disconnect(pwm
);
942 if (gtimeout
&& gelapsed
>= gtimeout
)
943 *error
= GPG_ERR_TIMEOUT
;
947 pth_write(p
[1], &pw
, sizeof(pw
));
949 write(p
[1], &pw
, sizeof(pw
));
956 * Don't count the time it takes to open the file
957 * which may have many iterations.
959 signal(SIGALRM
, SIG_DFL
);
960 *error
= do_open_command(pwm
, filename
, password
);
963 signal(SIGALRM
, catchsig
);
965 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
967 goto getpin_nb_again
;
972 pinentry_disconnect(pwm
);
975 pth_write(p
[1], &pw
, sizeof(pw
));
977 write(p
[1], &pw
, sizeof(pw
));
983 *error
= gpg_error_from_syserror();
999 * Not using pinentry and the file was not found
1002 password
= pwm
->password
;
1011 *error
= do_open_command(pwm
, filename
, password
);
1014 * Keep the user defined password set with pwmd_setopt(). The password may
1015 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1017 if (password
&& password
!= pwm
->password
)
1021 if (*error
== EPWMD_BADKEY
) {
1022 if (pin_try
-- > 0 && !nb
) {
1023 *error
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
1024 N_("Invalid passphrase, please try again."));
1033 pinentry_disconnect(pwm
);
1041 xfree(pwm
->filename
);
1043 pwm
->filename
= xstrdup(filename
);
1047 * The file is cached or the file is a new file.
1050 return *error
? -1 : -2;
1052 return *error
? 1 : 0;
1055 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
1059 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
1063 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
1066 #ifndef USE_PINENTRY
1067 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1070 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
1075 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
1079 char *result
= NULL
;
1083 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
1087 pinentry_disconnect(pwm
);
1100 if (strcmp(*password
, result
)) {
1103 pinentry_disconnect(pwm
);
1104 error
= EPWMD_BADKEY
;
1109 pinentry_disconnect(pwm
);
1114 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
1116 char buf
[ASSUAN_LINELENGTH
];
1118 char *result
= NULL
;
1120 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
1121 error
= send_command(pwm
, &result
, buf
);
1122 memset(&buf
, 0, sizeof(buf
));
1124 if (error
&& result
)
1130 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
1134 #ifndef USE_PINENTRY
1135 return GPG_ERR_NOT_IMPLEMENTED
;
1138 if (!pwm
|| !pw
|| !pw
->filename
[0])
1139 return GPG_ERR_INV_ARG
;
1143 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
1147 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
1149 char *result
= NULL
;
1150 char *password
= NULL
;
1153 *error
= GPG_ERR_INV_ARG
;
1157 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
1158 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
1160 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
1162 if (pwm
->use_pinentry
) {
1166 pwmd_nb_status_t pw
;
1168 if (pipe(p
) == -1) {
1169 *error
= gpg_error_from_syserror();
1182 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
1183 pw
.filename
[sizeof(pw
.filename
)-1] = 0;
1188 *error
= do_save_getpin(pwm
, &password
);
1189 } while (*error
== EPWMD_BADKEY
);
1193 pinentry_disconnect(pwm
);
1197 pth_write(p
[1], &pw
, sizeof(pw
));
1199 write(p
[1], &pw
, sizeof(pw
));
1205 *error
= do_save_command(pwm
, password
);
1206 pinentry_disconnect(pwm
);
1209 pth_write(p
[1], &pw
, sizeof(pw
));
1211 write(p
[1], &pw
, sizeof(pw
));
1217 *error
= gpg_error_from_syserror();
1230 *error
= do_save_getpin(pwm
, &password
);
1238 password
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1249 password
= pwm
->password
;
1251 *error
= do_save_command(pwm
, password
);
1253 if (password
&& password
!= pwm
->password
)
1257 return *error
? -1 : -2;
1259 return *error
? 1 : 0;
1262 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1264 #ifndef USE_PINENTRY
1265 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1268 return do_pwmd_save(pwm
, error
, 1);
1272 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1276 do_pwmd_save(pwm
, &error
, 0);
1280 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1284 int n
= va_arg(ap
, int);
1288 gpg_error_t error
= 0;
1291 return GPG_ERR_INV_ARG
;
1296 case PWMD_OPTION_STATUS_FUNC
:
1297 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1299 case PWMD_OPTION_STATUS_DATA
:
1300 pwm
->status_data
= va_arg(ap
, void *);
1302 case PWMD_OPTION_PASSWORD_FUNC
:
1303 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1305 case PWMD_OPTION_PASSWORD_DATA
:
1306 pwm
->passdata
= va_arg(ap
, void *);
1308 case PWMD_OPTION_PASSWORD
:
1309 arg1
= va_arg(ap
, char *);
1312 xfree(pwm
->password
);
1314 pwm
->password
= xstrdup(arg1
);
1317 case PWMD_OPTION_PINENTRY
:
1318 n
= va_arg(ap
, int);
1320 if (n
!= 0 && n
!= 1) {
1322 error
= GPG_ERR_INV_VALUE
;
1325 pwm
->use_pinentry
= n
;
1326 error
= pwmd_command(pwm
, &result
, "OPTION PINENTRY=%i",
1327 !pwm
->use_pinentry
);
1330 case PWMD_OPTION_PINENTRY_TRIES
:
1331 n
= va_arg(ap
, int);
1335 error
= GPG_ERR_INV_VALUE
;
1338 pwm
->pinentry_tries
= n
;
1340 case PWMD_OPTION_PINENTRY_PATH
:
1341 if (pwm
->pinentry_path
)
1342 xfree(pwm
->pinentry_path
);
1344 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1346 case PWMD_OPTION_PINENTRY_TTY
:
1347 if (pwm
->pinentry_tty
)
1348 xfree(pwm
->pinentry_tty
);
1350 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1352 case PWMD_OPTION_PINENTRY_DISPLAY
:
1353 if (pwm
->pinentry_display
)
1354 xfree(pwm
->pinentry_display
);
1356 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1358 case PWMD_OPTION_PINENTRY_TERM
:
1359 if (pwm
->pinentry_term
)
1360 xfree(pwm
->pinentry_term
);
1362 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1364 case PWMD_OPTION_PINENTRY_TITLE
:
1367 pwm
->title
= percent_escape(va_arg(ap
, char *));
1369 case PWMD_OPTION_PINENTRY_PROMPT
:
1372 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1374 case PWMD_OPTION_PINENTRY_DESC
:
1377 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1380 case PWMD_OPTION_PINENTRY
:
1381 case PWMD_OPTION_PINENTRY_TRIES
:
1382 case PWMD_OPTION_PINENTRY_PATH
:
1383 case PWMD_OPTION_PINENTRY_TTY
:
1384 case PWMD_OPTION_PINENTRY_DISPLAY
:
1385 case PWMD_OPTION_PINENTRY_TERM
:
1386 case PWMD_OPTION_PINENTRY_TITLE
:
1387 case PWMD_OPTION_PINENTRY_PROMPT
:
1388 case PWMD_OPTION_PINENTRY_DESC
:
1389 error
= GPG_ERR_NOT_IMPLEMENTED
;
1393 error
= GPG_ERR_NOT_IMPLEMENTED
;
1402 * Prevent requiring assuan.h when setting ctx. The ctx is really an
1403 * assuan_context_t *.
1405 gpg_error_t
pwmd_assuan_ctx(pwm_t
*pwm
, void *ctx
, int *fd
)
1408 return GPG_ERR_INV_ARG
;