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()
106 bindtextdomain("libpwmd", LOCALEDIR
);
109 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
110 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
114 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
117 char *socketpath
= NULL
;
120 assuan_context_t ctx
;
125 pw
= getpwuid(getuid());
126 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
127 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
130 socketpath
= xstrdup(path
);
132 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
140 n
= assuan_get_active_fds(ctx
, 0, active
, sizeof(active
));
142 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
143 *error
= gpg_error_from_errno(errno
);
144 assuan_disconnect(ctx
);
148 pwm
->fd
= n
<= 0 ? -1 : dup(active
[0]);
151 fcntl(pwm
->fd
, F_SETFL
, O_NONBLOCK
);
156 pwm
->pinentry_tries
= 3;
164 gpg_error_t
pwmd_pending_line(pwm_t
*pwm
, char **line
, size_t *len
)
167 return GPG_ERR_INV_ARG
;
169 if (assuan_pending_line(pwm
->ctx
))
170 return assuan_read_line(pwm
->ctx
, line
, len
);
172 return GPG_ERR_NO_DATA
;
175 void pwmd_close(pwm_t
*pwm
)
181 assuan_disconnect(pwm
->ctx
);
184 xfree(pwm
->password
);
195 if (pwm
->pinentry_tty
)
196 xfree(pwm
->pinentry_tty
);
198 if (pwm
->pinentry_display
)
199 xfree(pwm
->pinentry_display
);
201 if (pwm
->pinentry_term
)
202 xfree(pwm
->pinentry_term
);
205 xfree(pwm
->filename
);
210 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
212 membuf_t
*mem
= (membuf_t
*)data
;
218 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
222 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
227 void pwmd_free_result(void *data
)
232 static int _inquire_cb(void *data
, const char *keyword
)
234 pwm_t
*pwm
= (pwm_t
*)data
;
236 int flags
= fcntl(pwm
->fd
, F_GETFL
);
238 /* Shouldn't get this far without a callback. */
239 if (!pwm
->inquire_func
)
240 return GPG_ERR_INV_ARG
;
243 * Since the socket file descriptor is probably set to non-blocking, set to
244 * blocking to prevent GPG_ERR_EAGAIN errors. This should be fixes when
245 * asynchronous INQUIRE is supported by either libassuan or a later
248 fcntl(pwm
->fd
, F_SETFL
, 0);
255 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
, &len
);
256 rc
= gpg_err_code(rc
);
258 if (rc
== GPG_ERR_EOF
|| !rc
) {
259 if (len
<= 0 || !result
|| !*result
) {
264 arc
= assuan_send_data(pwm
->ctx
, result
, len
);
266 if (rc
== GPG_ERR_EOF
) {
277 fcntl(pwm
->fd
, F_SETFL
, flags
);
281 gpg_error_t
pwmd_finalize(pwm_t
*pwm
)
283 if (!pwm
|| pwm
->fd
< 0)
284 return GPG_ERR_INV_ARG
;
286 pwm
->state
= ASYNC_INIT
;
288 pwm
->is_open_cmd
= 0;
292 static gpg_error_t
do_nb_command(pwm_t
*pwm
, const char *cmd
, const char *arg
)
296 size_t len
= strlen(cmd
) + 2;
298 len
+= arg
? strlen(arg
) : 0;
300 if (pwm
->state
!= ASYNC_INIT
)
301 return GPG_ERR_UNEXPECTED
;
303 buf
= (char *)xmalloc(len
);
306 rc
= gpg_error_from_errno(ENOMEM
);
310 snprintf(buf
, len
, "%s %s", cmd
, arg
? arg
: "");
311 rc
= assuan_write_line(pwm
->ctx
, buf
);
315 pwm
->state
= ASYNC_PROCESS
;
321 gpg_error_t
pwmd_open_async(pwm_t
*pwm
, const char *filename
)
323 if (!pwm
|| !filename
)
324 return GPG_ERR_INV_ARG
;
326 /* For pinentry retries. */
327 if (!pwm
->is_open_cmd
) {
329 xfree(pwm
->filename
);
331 pwm
->filename
= xstrdup(filename
);
334 pwm
->is_open_cmd
= 1;
335 return do_nb_command(pwm
, "OPEN", filename
);
338 gpg_error_t
pwmd_save_async(pwm_t
*pwm
)
341 return GPG_ERR_INV_ARG
;
343 return do_nb_command(pwm
, "SAVE", NULL
);
346 static gpg_error_t
parse_assuan_line(pwm_t
*pwm
)
352 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
355 if (line
[0] == 'O' && line
[1] == 'K' &&
356 (line
[2] == 0 || line
[2] == ' ')) {
357 pwm
->state
= ASYNC_DONE
;
359 else if (line
[0] == '#') {
361 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' ')) {
362 if (pwm
->status_func
) {
363 pwm
->status_func(pwm
->status_data
,
364 line
[1] == 0 ? line
+1 : line
+2);
367 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
368 (line
[3] == 0 || line
[3] == ' ')) {
371 pwm
->state
= ASYNC_DONE
;
378 pwmd_async_t
pwmd_process(pwm_t
*pwm
, gpg_error_t
*rc
)
382 struct timeval tv
= {0, 0};
386 if (!pwm
|| pwm
->fd
< 0) {
387 *rc
= GPG_ERR_INV_ARG
;
390 else if (pwm
->state
== ASYNC_DONE
)
392 else if (pwm
->state
== ASYNC_INIT
) {
393 *rc
= GPG_ERR_UNEXPECTED
;
398 FD_SET(pwm
->fd
, &rfds
);
400 n
= pth_select(pwm
->fd
+1, &rfds
, NULL
, NULL
, &tv
);
402 n
= select(pwm
->fd
+1, &rfds
, NULL
, NULL
, &tv
);
406 if (FD_ISSET(pwm
->fd
, &rfds
))
407 *rc
= parse_assuan_line(pwm
);
410 while (!*rc
&& assuan_pending_line(pwm
->ctx
))
411 *rc
= parse_assuan_line(pwm
);
413 if (pwm
->is_open_cmd
&& gpg_err_code(*rc
) == EPWMD_BADKEY
&&
414 ++pwm
->ntries
< pwm
->pinentry_tries
) {
415 pwm
->state
= ASYNC_INIT
;
416 *rc
= pwmd_open_async(pwm
, pwm
->filename
);
422 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
423 char **result
, const char *cmd
)
431 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
432 pwm
->status_func
, pwm
->status_data
);
442 mem_realloc_cb(&data
, "", 1);
443 *result
= (char *)data
.buf
;
447 return gpg_err_code(rc
);
450 gpg_error_t
pwmd_inquire(pwm_t
*pwm
, const char *cmd
, pwmd_inquire_fn fn
,
453 if (!pwm
|| !cmd
|| !fn
)
454 return GPG_ERR_INV_ARG
;
456 pwm
->inquire_func
= fn
;
457 pwm
->inquire_data
= data
;
458 return assuan_command(pwm
, pwm
->ctx
, NULL
, cmd
);
461 gpg_error_t
pwmd_terminate_pinentry(pwm_t
*pwm
)
464 return GPG_ERR_NOT_IMPLEMENTED
;
466 if (!pwm
|| pwm
->pid
== -1)
467 return GPG_ERR_INV_ARG
;
469 if (kill(pwm
->pid
, 0) == 0) {
470 if (kill(pwm
->pid
, SIGTERM
) == -1) {
471 if (kill(pwm
->pid
, SIGKILL
) == -1)
472 return gpg_error_from_errno(errno
);
475 pwm
->pin_error
= GPG_ERR_TIMEOUT
;
478 return gpg_error_from_errno(errno
);
485 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
488 char tmp
[ASSUAN_LINELENGTH
];
492 pwm
->title
= xstrdup(N_("LibPWMD"));
495 pwm
->prompt
= xstrdup(N_("Password:"));
497 if (!pwm
->desc
&& !which
)
498 pwm
->desc
= xstrdup(N_("Enter a password."));
501 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Invalid password, please try again."));
504 else if (which
== 2) {
505 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Please type the password again for confirmation."));
509 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
510 sprintf(buf
, "SETERROR %s", pwm
->desc
);
513 error
= pinentry_command(pwm
, NULL
, buf
);
519 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
520 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
521 error
= pinentry_command(pwm
, NULL
, buf
);
527 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
528 sprintf(buf
, "SETDESC %s", pwm
->title
);
529 error
= pinentry_command(pwm
, NULL
, buf
);
534 static void update_pinentry_settings(pwm_t
*pwm
)
538 struct passwd
*pw
= getpwuid(getuid());
541 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
->pw_dir
);
543 if ((fp
= fopen(buf
, "r")) == NULL
)
546 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
547 char name
[32], val
[256];
549 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
552 if (strcasecmp(name
, "TTYNAME") == 0) {
553 xfree(pwm
->pinentry_tty
);
554 pwm
->pinentry_tty
= xstrdup(val
);
556 else if (strcasecmp(name
, "TTYTYPE") == 0) {
557 xfree(pwm
->pinentry_term
);
558 pwm
->pinentry_term
= xstrdup(val
);
560 else if (strcasecmp(name
, "DISPLAY") == 0) {
561 xfree(pwm
->pinentry_display
);
562 pwm
->pinentry_display
= xstrdup(val
);
564 else if (strcasecmp(name
, "PATH") == 0) {
565 xfree(pwm
->pinentry_path
);
566 pwm
->pinentry_path
= xstrdup(val
);
573 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
576 assuan_context_t ctx
;
577 int child_list
[] = {-1};
578 char *display
= getenv("DISPLAY");
580 int have_display
= 0;
583 update_pinentry_settings(pwm
);
585 if (pwm
->pinentry_display
|| display
)
588 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
591 return gpg_error_from_errno(errno
);
594 if (!have_display
&& !tty
)
595 return GPG_ERR_ENOTTY
;
597 argv
[0] = "pinentry";
598 argv
[1] = have_display
? "--display" : "--ttyname";
599 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
603 argv
[3] = "--ttytype";
604 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
608 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
613 pwm
->pid
= assuan_get_pid(ctx
);
615 return set_pinentry_strings(pwm
, 0);
618 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
623 n
= launch_pinentry(pwm
);
629 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
632 static void pinentry_disconnect(pwm_t
*pwm
)
635 assuan_disconnect(pwm
->pctx
);
642 * Only called from a child process.
644 static void catchsig(int sig
)
648 if (gelapsed
++ >= gtimeout
) {
649 global_error
= pwmd_terminate_pinentry(gpwm
);
652 global_error
= GPG_ERR_TIMEOUT
;
665 * Borrowed from libassuan.
667 static char *percent_escape(const char *atext
)
669 const unsigned char *s
;
670 int len
= strlen(atext
) * 3 + 1;
671 char *buf
= (char *)xmalloc(len
), *p
= buf
;
676 for (s
=(const unsigned char *)atext
; *s
; s
++) {
678 sprintf (p
, "%%%02X", *s
);
690 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
693 return GPG_ERR_INV_ARG
;
695 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
699 * Avoid sending the BYE command here. libassuan will close the file
700 * descriptor and release the assuan context. Use pwmd_close() instead.
702 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
710 return GPG_ERR_INV_ARG
;
715 * C99 allows the dst pointer to be null which will calculate the length
716 * of the result and return it.
718 len
= vsnprintf(NULL
, 0, cmd
, ap
);
719 buf
= (char *)xmalloc(len
+ 1);
720 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
722 error
= send_command(pwm
, result
, buf
);
728 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
731 signal(SIGALRM
, catchsig
);
736 return pinentry_command(pwm
, result
, "GETPIN");
739 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
741 int pin_try
= *try_n
;
748 error
= set_pinentry_strings(pwm
, which
);
751 pinentry_disconnect(pwm
);
756 if (pwm
->pinentry_tries
-1 != pin_try
) {
757 error
= set_pinentry_strings(pwm
, 1);
760 pinentry_disconnect(pwm
);
766 error
= do_getpin(pwm
, result
);
769 * Since there was input cancel any timeout setting.
774 if (error
== GPG_ERR_CANCELED
)
775 return GPG_ERR_CANCELED
;
777 if (pin_try
!= -1 && pin_try
--)
781 pinentry_disconnect(pwm
);
791 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
796 return GPG_ERR_NOT_IMPLEMENTED
;
799 if (!pwm
|| !pw
|| !pw
->filename
[0])
800 return GPG_ERR_INV_ARG
;
810 xfree(pwm
->filename
);
812 pwm
->filename
= xstrdup(pw
->filename
);
813 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
817 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
821 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
823 char buf
[ASSUAN_LINELENGTH
];
827 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
828 error
= send_command(pwm
, &result
, buf
);
829 memset(buf
, 0, sizeof(buf
));
837 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
841 char *password
= NULL
;
847 if (!pwm
|| !filename
|| !*filename
) {
848 *error
= GPG_ERR_INV_ARG
;
853 pin_try
= pwm
->pinentry_tries
- 1;
857 * Avoid calling pinentry if the password is cached on the server or if
858 * this is a new file.
860 *error
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
865 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
866 pwmd_free_result(result
);
868 if (access(path
, R_OK
) == -1) {
873 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
875 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
877 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
883 * Get the password from pinentry.
885 if (pwm
->use_pinentry
) {
887 * Nonblocking is wanted. fork() then return a file descriptor
888 * that the client can use to read() from.
896 *error
= gpg_error_from_syserror();
909 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
910 pw
.filename
[sizeof(pw
.filename
)-1] = 0;
920 *error
= getpin(pwm
, &password
, &pin_try
, 0);
925 pinentry_disconnect(pwm
);
927 if (gtimeout
&& gelapsed
>= gtimeout
)
928 *error
= GPG_ERR_TIMEOUT
;
932 pth_write(p
[1], &pw
, sizeof(pw
));
934 write(p
[1], &pw
, sizeof(pw
));
941 * Don't count the time it takes to open the file
942 * which may have many iterations.
944 signal(SIGALRM
, SIG_DFL
);
945 *error
= do_open_command(pwm
, filename
, password
);
948 signal(SIGALRM
, catchsig
);
950 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
952 goto getpin_nb_again
;
957 pinentry_disconnect(pwm
);
960 pth_write(p
[1], &pw
, sizeof(pw
));
962 write(p
[1], &pw
, sizeof(pw
));
968 *error
= gpg_error_from_syserror();
984 * Not using pinentry and the file was not found
987 password
= pwm
->password
;
996 *error
= do_open_command(pwm
, filename
, password
);
999 * Keep the user defined password set with pwmd_setopt(). The password may
1000 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1002 if (password
&& password
!= pwm
->password
)
1006 if (*error
== EPWMD_BADKEY
) {
1007 if (pin_try
-- > 0 && !nb
) {
1008 *error
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
1009 N_("Invalid password, please try again."));
1018 pinentry_disconnect(pwm
);
1026 xfree(pwm
->filename
);
1028 pwm
->filename
= xstrdup(filename
);
1032 * The file is cached or the file is a new file.
1035 return *error
? -1 : -2;
1037 return *error
? 1 : 0;
1040 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
1044 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
1048 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
1051 #ifndef USE_PINENTRY
1052 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1055 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
1060 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
1064 char *result
= NULL
;
1068 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
1072 pinentry_disconnect(pwm
);
1085 if (strcmp(*password
, result
)) {
1088 pinentry_disconnect(pwm
);
1089 error
= EPWMD_BADKEY
;
1094 pinentry_disconnect(pwm
);
1099 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
1101 char buf
[ASSUAN_LINELENGTH
];
1103 char *result
= NULL
;
1105 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
1106 error
= send_command(pwm
, &result
, buf
);
1107 memset(&buf
, 0, sizeof(buf
));
1109 if (error
&& result
)
1115 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
1119 #ifndef USE_PINENTRY
1120 return GPG_ERR_NOT_IMPLEMENTED
;
1123 if (!pwm
|| !pw
|| !pw
->filename
[0])
1124 return GPG_ERR_INV_ARG
;
1128 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
1132 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
1134 char *result
= NULL
;
1135 char *password
= NULL
;
1138 *error
= GPG_ERR_INV_ARG
;
1142 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
1143 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
1145 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
1147 if (pwm
->use_pinentry
) {
1151 pwmd_nb_status_t pw
;
1153 if (pipe(p
) == -1) {
1154 *error
= gpg_error_from_syserror();
1167 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
1168 pw
.filename
[sizeof(pw
.filename
)-1] = 0;
1173 *error
= do_save_getpin(pwm
, &password
);
1174 } while (*error
== EPWMD_BADKEY
);
1178 pinentry_disconnect(pwm
);
1182 pth_write(p
[1], &pw
, sizeof(pw
));
1184 write(p
[1], &pw
, sizeof(pw
));
1190 *error
= do_save_command(pwm
, password
);
1191 pinentry_disconnect(pwm
);
1194 pth_write(p
[1], &pw
, sizeof(pw
));
1196 write(p
[1], &pw
, sizeof(pw
));
1202 *error
= gpg_error_from_syserror();
1215 *error
= do_save_getpin(pwm
, &password
);
1223 password
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1234 password
= pwm
->password
;
1236 *error
= do_save_command(pwm
, password
);
1238 if (password
&& password
!= pwm
->password
)
1242 return *error
? -1 : -2;
1244 return *error
? 1 : 0;
1247 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1249 #ifndef USE_PINENTRY
1250 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1253 return do_pwmd_save(pwm
, error
, 1);
1257 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1261 do_pwmd_save(pwm
, &error
, 0);
1265 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1269 int n
= va_arg(ap
, int);
1273 gpg_error_t error
= 0;
1276 return GPG_ERR_INV_ARG
;
1281 case PWMD_OPTION_STATUS_FUNC
:
1282 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1284 case PWMD_OPTION_STATUS_DATA
:
1285 pwm
->status_data
= va_arg(ap
, void *);
1287 case PWMD_OPTION_PASSWORD_FUNC
:
1288 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1290 case PWMD_OPTION_PASSWORD_DATA
:
1291 pwm
->passdata
= va_arg(ap
, void *);
1293 case PWMD_OPTION_PASSWORD
:
1294 arg1
= va_arg(ap
, char *);
1297 xfree(pwm
->password
);
1299 pwm
->password
= xstrdup(arg1
);
1302 case PWMD_OPTION_PINENTRY
:
1303 n
= va_arg(ap
, int);
1305 if (n
!= 0 && n
!= 1) {
1307 error
= GPG_ERR_INV_VALUE
;
1310 pwm
->use_pinentry
= n
;
1311 error
= pwmd_command(pwm
, &result
, "OPTION PINENTRY=%i",
1312 !pwm
->use_pinentry
);
1315 case PWMD_OPTION_PINENTRY_TRIES
:
1316 n
= va_arg(ap
, int);
1320 error
= GPG_ERR_INV_VALUE
;
1323 pwm
->pinentry_tries
= n
;
1325 case PWMD_OPTION_PINENTRY_PATH
:
1326 if (pwm
->pinentry_path
)
1327 xfree(pwm
->pinentry_path
);
1329 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1331 case PWMD_OPTION_PINENTRY_TTY
:
1332 if (pwm
->pinentry_tty
)
1333 xfree(pwm
->pinentry_tty
);
1335 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1337 case PWMD_OPTION_PINENTRY_DISPLAY
:
1338 if (pwm
->pinentry_display
)
1339 xfree(pwm
->pinentry_display
);
1341 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1343 case PWMD_OPTION_PINENTRY_TERM
:
1344 if (pwm
->pinentry_term
)
1345 xfree(pwm
->pinentry_term
);
1347 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1349 case PWMD_OPTION_PINENTRY_TITLE
:
1352 pwm
->title
= percent_escape(va_arg(ap
, char *));
1354 case PWMD_OPTION_PINENTRY_PROMPT
:
1357 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1359 case PWMD_OPTION_PINENTRY_DESC
:
1362 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1365 case PWMD_OPTION_PINENTRY
:
1366 case PWMD_OPTION_PINENTRY_TRIES
:
1367 case PWMD_OPTION_PINENTRY_PATH
:
1368 case PWMD_OPTION_PINENTRY_TTY
:
1369 case PWMD_OPTION_PINENTRY_DISPLAY
:
1370 case PWMD_OPTION_PINENTRY_TERM
:
1371 case PWMD_OPTION_PINENTRY_TITLE
:
1372 case PWMD_OPTION_PINENTRY_PROMPT
:
1373 case PWMD_OPTION_PINENTRY_DESC
:
1374 error
= GPG_ERR_NOT_IMPLEMENTED
;
1378 error
= GPG_ERR_NOT_IMPLEMENTED
;
1386 gpg_error_t
pwmd_assuan_ctx(pwm_t
*pwm
, assuan_context_t
*ctx
, int *fd
)
1389 return GPG_ERR_INV_ARG
;