1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
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
26 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <sys/select.h>
57 #define N_(msgid) dgettext("libpwmd", msgid)
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
;
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
)) {
77 return N_("Unknown error");
79 return N_("No cache slots available");
81 return N_("Recursion loop");
83 return N_("No file is open");
85 return N_("General LibXML error");
87 return N_("File modified");
91 return gpg_strerror(e
);
94 gpg_error_t
pwmd_init()
96 static int initialized
;
105 bindtextdomain("libpwmd", LOCALEDIR
);
108 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
109 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
;
119 assuan_context_t ctx
;
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
);
129 socketpath
= xstrdup(path
);
131 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
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
);
147 pwm
->fd
= n
<= 0 ? -1 : dup(active
[0]);
150 fcntl(pwm
->fd
, F_SETFL
, O_NONBLOCK
);
155 pwm
->pinentry_tries
= 3;
161 gpg_error_t
pwmd_pending_line(pwm_t
*pwm
, char **line
, size_t *len
)
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
)
178 assuan_disconnect(pwm
->ctx
);
181 xfree(pwm
->password
);
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
);
205 xfree(pwm
->lcmessages
);
208 xfree(pwm
->filename
);
213 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
215 membuf_t
*mem
= (membuf_t
*)data
;
221 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
225 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
230 void pwmd_free_result(void *data
)
235 static int _inquire_cb(void *data
, const char *keyword
)
237 pwm_t
*pwm
= (pwm_t
*)data
;
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
251 fcntl(pwm
->fd
, F_SETFL
, 0);
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
) {
267 arc
= assuan_send_data(pwm
->ctx
, result
, len
);
269 if (rc
== GPG_ERR_EOF
) {
280 fcntl(pwm
->fd
, F_SETFL
, flags
);
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
;
291 pwm
->is_open_cmd
= 0;
295 static gpg_error_t
do_nb_command(pwm_t
*pwm
, const char *cmd
, const char *arg
)
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
);
309 rc
= gpg_error_from_errno(ENOMEM
);
313 snprintf(buf
, len
, "%s %s", cmd
, arg
? arg
: "");
314 rc
= assuan_write_line(pwm
->ctx
, buf
);
318 pwm
->state
= ASYNC_PROCESS
;
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
) {
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
)
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
)
355 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
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] == ' ')) {
374 pwm
->state
= ASYNC_DONE
;
381 pwmd_async_t
pwmd_process(pwm_t
*pwm
, gpg_error_t
*rc
)
385 struct timeval tv
= {0, 0};
389 if (!pwm
|| pwm
->fd
< 0) {
390 *rc
= GPG_ERR_INV_ARG
;
393 else if (pwm
->state
== ASYNC_DONE
)
395 else if (pwm
->state
== ASYNC_INIT
) {
396 *rc
= GPG_ERR_UNEXPECTED
;
401 FD_SET(pwm
->fd
, &rfds
);
403 n
= pth_select(pwm
->fd
+1, &rfds
, NULL
, NULL
, &tv
);
405 n
= select(pwm
->fd
+1, &rfds
, NULL
, NULL
, &tv
);
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
);
425 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
426 char **result
, const char *cmd
)
434 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
435 pwm
->status_func
, pwm
->status_data
);
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
,
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
)
467 return GPG_ERR_NOT_IMPLEMENTED
;
469 pid_t pid
= pwm
->pid
;
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
;
485 return gpg_error_from_errno(errno
);
492 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
495 char tmp
[ASSUAN_LINELENGTH
];
499 pwm
->title
= xstrdup(N_("LibPWMD"));
502 pwm
->prompt
= xstrdup(N_("Passphrase:"));
504 if (!pwm
->desc
&& !which
)
505 pwm
->desc
= xstrdup(N_("Enter a passphrase."));
508 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Invalid passphrase, please try again."));
511 else if (which
== 2) {
512 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Please type the passphrase again for confirmation."));
516 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
517 sprintf(buf
, "SETERROR %s", pwm
->desc
);
520 error
= pinentry_command(pwm
, NULL
, buf
);
526 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
527 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
528 error
= pinentry_command(pwm
, NULL
, buf
);
534 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
535 sprintf(buf
, "SETDESC %s", pwm
->title
);
536 error
= pinentry_command(pwm
, NULL
, buf
);
541 static void update_pinentry_settings(pwm_t
*pwm
)
545 struct passwd
*pw
= getpwuid(getuid());
548 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
->pw_dir
);
550 if ((fp
= fopen(buf
, "r")) == NULL
)
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)
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
);
580 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
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;
591 update_pinentry_settings(pwm
);
593 if (pwm
->pinentry_display
|| display
)
596 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
599 return gpg_error_from_errno(errno
);
602 if (!have_display
&& !tty
)
603 return GPG_ERR_ENOTTY
;
606 *p
++ = have_display
? "--display" : "--ttyname";
607 *p
++ = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
614 if (pwm
->lcmessages
) {
615 *p
++ = "--lc-messages";
616 *p
++ = pwm
->lcmessages
;
623 *p
++ = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
627 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
632 pwm
->pid
= assuan_get_pid(ctx
);
634 return set_pinentry_strings(pwm
, 0);
637 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
642 n
= launch_pinentry(pwm
);
648 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
651 static void pinentry_disconnect(pwm_t
*pwm
)
654 assuan_disconnect(pwm
->pctx
);
661 * Only called from a child process.
663 static void catchsig(int sig
)
667 if (gelapsed
++ >= gtimeout
) {
668 global_error
= pwmd_terminate_pinentry(gpwm
);
671 global_error
= GPG_ERR_TIMEOUT
;
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
;
696 for (s
=(const unsigned char *)atext
; *s
; s
++) {
698 sprintf (p
, "%%%02X", *s
);
709 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *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
,
726 return GPG_ERR_INV_ARG
;
729 * C99 allows the dst pointer to be null which will calculate the length
730 * of the would-be result and return it.
733 len
= vsnprintf(NULL
, 0, cmd
, ap
)+1;
734 buf
= (char *)xmalloc(len
);
738 return gpg_error_from_errno(ENOMEM
);
741 len
= vsnprintf(buf
, len
, cmd
, ap
);
743 error
= send_command(pwm
, result
, buf
);
749 * Avoid sending the BYE command here. libassuan will close the file
750 * descriptor and release the assuan context. Use pwmd_close() instead.
752 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
758 return GPG_ERR_INV_ARG
;
762 error
= pwmd_command_ap(pwm
, result
, cmd
, ap
);
768 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
771 signal(SIGALRM
, catchsig
);
776 return pinentry_command(pwm
, result
, "GETPIN");
779 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
781 int pin_try
= *try_n
;
788 error
= set_pinentry_strings(pwm
, which
);
791 pinentry_disconnect(pwm
);
796 if (pwm
->pinentry_tries
-1 != pin_try
) {
797 error
= set_pinentry_strings(pwm
, 1);
800 pinentry_disconnect(pwm
);
806 error
= do_getpin(pwm
, result
);
809 * Since there was input cancel any timeout setting.
814 if (error
== GPG_ERR_CANCELED
)
815 return GPG_ERR_CANCELED
;
817 if (pin_try
!= -1 && pin_try
--)
821 pinentry_disconnect(pwm
);
831 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
836 return GPG_ERR_NOT_IMPLEMENTED
;
839 if (!pwm
|| !pw
|| !pw
->filename
[0])
840 return GPG_ERR_INV_ARG
;
850 xfree(pwm
->filename
);
852 pwm
->filename
= xstrdup(pw
->filename
);
853 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
857 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
861 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
863 char buf
[ASSUAN_LINELENGTH
];
867 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
868 error
= send_command(pwm
, &result
, buf
);
869 memset(buf
, 0, sizeof(buf
));
877 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
881 char *password
= NULL
;
887 if (!pwm
|| !filename
|| !*filename
) {
888 *error
= GPG_ERR_INV_ARG
;
893 pin_try
= pwm
->pinentry_tries
- 1;
897 * Avoid calling pinentry if the password is cached on the server or if
898 * this is a new file.
900 *error
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
905 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
906 pwmd_free_result(result
);
908 if (access(path
, R_OK
) == -1) {
913 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
915 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
917 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
923 * Get the password from pinentry.
925 if (pwm
->use_pinentry
) {
927 * Nonblocking is wanted. fork() then return a file descriptor
928 * that the client can use to read() from.
936 *error
= gpg_error_from_syserror();
949 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
950 pw
.filename
[sizeof(pw
.filename
)-1] = 0;
960 *error
= getpin(pwm
, &password
, &pin_try
, 0);
965 pinentry_disconnect(pwm
);
967 if (gtimeout
&& gelapsed
>= gtimeout
)
968 *error
= GPG_ERR_TIMEOUT
;
972 pth_write(p
[1], &pw
, sizeof(pw
));
974 write(p
[1], &pw
, sizeof(pw
));
981 * Don't count the time it takes to open the file
982 * which may have many iterations.
984 signal(SIGALRM
, SIG_DFL
);
985 *error
= do_open_command(pwm
, filename
, password
);
988 signal(SIGALRM
, catchsig
);
990 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
992 goto getpin_nb_again
;
997 pinentry_disconnect(pwm
);
1000 pth_write(p
[1], &pw
, sizeof(pw
));
1002 write(p
[1], &pw
, sizeof(pw
));
1008 *error
= gpg_error_from_syserror();
1024 * Not using pinentry and the file was not found
1027 password
= pwm
->password
;
1036 *error
= do_open_command(pwm
, filename
, password
);
1039 * Keep the user defined password set with pwmd_setopt(). The password may
1040 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
1042 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
1046 if (*error
== EPWMD_BADKEY
) {
1047 if (pin_try
-- > 0 && !nb
) {
1048 *error
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
1049 N_("Invalid passphrase, please try again."));
1058 pinentry_disconnect(pwm
);
1066 xfree(pwm
->filename
);
1068 pwm
->filename
= xstrdup(filename
);
1072 * The file is cached or the file is a new file.
1075 return *error
? -1 : -2;
1077 return *error
? 1 : 0;
1080 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
1084 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
1088 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
1091 #ifndef USE_PINENTRY
1092 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1095 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
1100 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
1104 char *result
= NULL
;
1108 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
1112 pinentry_disconnect(pwm
);
1125 if (strcmp(*password
, result
)) {
1128 pinentry_disconnect(pwm
);
1129 error
= EPWMD_BADKEY
;
1134 pinentry_disconnect(pwm
);
1139 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
1141 char buf
[ASSUAN_LINELENGTH
];
1143 char *result
= NULL
;
1145 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
1146 error
= send_command(pwm
, &result
, buf
);
1147 memset(&buf
, 0, sizeof(buf
));
1149 if (error
&& result
)
1155 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
1159 #ifndef USE_PINENTRY
1160 return GPG_ERR_NOT_IMPLEMENTED
;
1163 if (!pwm
|| !pw
|| !pw
->filename
[0])
1164 return GPG_ERR_INV_ARG
;
1168 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
1172 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
1174 char *result
= NULL
;
1175 char *password
= NULL
;
1178 *error
= GPG_ERR_INV_ARG
;
1182 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
1183 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
1185 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
1187 password
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1189 else if (pwm
->use_pinentry
) {
1193 pwmd_nb_status_t pw
;
1195 if (pipe(p
) == -1) {
1196 *error
= gpg_error_from_syserror();
1209 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
1210 pw
.filename
[sizeof(pw
.filename
)-1] = 0;
1215 *error
= do_save_getpin(pwm
, &password
);
1216 } while (*error
== EPWMD_BADKEY
);
1220 pinentry_disconnect(pwm
);
1224 pth_write(p
[1], &pw
, sizeof(pw
));
1226 write(p
[1], &pw
, sizeof(pw
));
1232 *error
= do_save_command(pwm
, password
);
1233 pinentry_disconnect(pwm
);
1236 pth_write(p
[1], &pw
, sizeof(pw
));
1238 write(p
[1], &pw
, sizeof(pw
));
1244 *error
= gpg_error_from_syserror();
1257 *error
= do_save_getpin(pwm
, &password
);
1270 password
= pwm
->password
;
1272 *error
= do_save_command(pwm
, password
);
1274 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
1278 return *error
? -1 : -2;
1280 return *error
? 1 : 0;
1283 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1285 #ifndef USE_PINENTRY
1286 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1289 return do_pwmd_save(pwm
, error
, 1);
1293 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1297 do_pwmd_save(pwm
, &error
, 0);
1301 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1304 int n
= va_arg(ap
, int);
1307 gpg_error_t error
= 0;
1310 return GPG_ERR_INV_ARG
;
1315 case PWMD_OPTION_STATUS_FUNC
:
1316 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1318 case PWMD_OPTION_STATUS_DATA
:
1319 pwm
->status_data
= va_arg(ap
, void *);
1321 case PWMD_OPTION_PASSWORD_FUNC
:
1322 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1324 case PWMD_OPTION_PASSWORD_DATA
:
1325 pwm
->passdata
= va_arg(ap
, void *);
1327 case PWMD_OPTION_PASSWORD
:
1328 arg1
= va_arg(ap
, char *);
1331 xfree(pwm
->password
);
1333 pwm
->password
= xstrdup(arg1
);
1335 case PWMD_OPTION_PINENTRY
:
1336 n
= va_arg(ap
, int);
1338 if (n
!= 0 && n
!= 1) {
1340 error
= GPG_ERR_INV_VALUE
;
1343 pwm
->use_pinentry
= n
;
1344 error
= pwmd_command(pwm
, &result
, "OPTION PINENTRY=%i",
1345 !pwm
->use_pinentry
);
1349 case PWMD_OPTION_PINENTRY_TRIES
:
1350 n
= va_arg(ap
, int);
1354 error
= GPG_ERR_INV_VALUE
;
1357 pwm
->pinentry_tries
= n
;
1360 case PWMD_OPTION_PINENTRY_TIMEOUT
:
1361 n
= va_arg(ap
, int);
1365 error
= GPG_ERR_INV_VALUE
;
1368 pwm
->pinentry_timeout
= n
;
1370 if (!pwm
->use_pinentry
)
1371 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i",
1372 pwm
->pinentry_timeout
);
1374 case PWMD_OPTION_PINENTRY_PATH
:
1375 if (pwm
->pinentry_path
)
1376 xfree(pwm
->pinentry_path
);
1378 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1380 if (!pwm
->use_pinentry
)
1381 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s",
1382 pwm
->pinentry_path
);
1384 case PWMD_OPTION_PINENTRY_TTY
:
1385 if (pwm
->pinentry_tty
)
1386 xfree(pwm
->pinentry_tty
);
1388 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1390 if (!pwm
->use_pinentry
)
1391 error
= pwmd_command(pwm
, &result
, "OPTION TTY=%s",
1394 case PWMD_OPTION_PINENTRY_DISPLAY
:
1395 if (pwm
->pinentry_display
)
1396 xfree(pwm
->pinentry_display
);
1398 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1400 if (!pwm
->use_pinentry
)
1401 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s",
1402 pwm
->pinentry_display
);
1404 case PWMD_OPTION_PINENTRY_TERM
:
1405 if (pwm
->pinentry_term
)
1406 xfree(pwm
->pinentry_term
);
1408 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1410 if (!pwm
->use_pinentry
)
1411 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s",
1412 pwm
->pinentry_term
);
1414 case PWMD_OPTION_PINENTRY_TITLE
:
1418 pwm
->title
= percent_escape(va_arg(ap
, char *));
1420 if (!pwm
->use_pinentry
)
1421 error
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
1424 case PWMD_OPTION_PINENTRY_PROMPT
:
1428 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1430 if (!pwm
->use_pinentry
)
1431 error
= pwmd_command(pwm
, &result
, "OPTION PROMPT=%s",
1434 case PWMD_OPTION_PINENTRY_DESC
:
1438 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1440 if (!pwm
->use_pinentry
)
1441 error
= pwmd_command(pwm
, &result
, "OPTION DESC=%s",
1444 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
1446 xfree(pwm
->lcctype
);
1448 pwm
->lcctype
= xstrdup(va_arg(ap
, char *));
1450 if (!pwm
->use_pinentry
)
1451 error
= pwmd_command(pwm
, &result
, "OPTION LC_CTYPE=%s",
1454 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
1455 if (pwm
->lcmessages
)
1456 xfree(pwm
->lcmessages
);
1458 pwm
->lcmessages
= xstrdup(va_arg(ap
, char *));
1460 if (!pwm
->use_pinentry
)
1461 error
= pwmd_command(pwm
, &result
, "OPTION LC_MESSAGES=%s",
1465 error
= GPG_ERR_NOT_IMPLEMENTED
;
1474 * Prevent requiring assuan.h when setting ctx. The ctx is really an
1475 * assuan_context_t *.
1477 gpg_error_t
pwmd_assuan_ctx(pwm_t
*pwm
, void *ctx
, int *fd
)
1480 return GPG_ERR_INV_ARG
;