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
,
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
);
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
, ...)
749 return GPG_ERR_INV_ARG
;
753 error
= pwmd_command_ap(pwm
, result
, cmd
, ap
);
759 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
762 signal(SIGALRM
, catchsig
);
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
;
779 error
= set_pinentry_strings(pwm
, which
);
782 pinentry_disconnect(pwm
);
787 if (pwm
->pinentry_tries
-1 != pin_try
) {
788 error
= set_pinentry_strings(pwm
, 1);
791 pinentry_disconnect(pwm
);
797 error
= do_getpin(pwm
, result
);
800 * Since there was input cancel any timeout setting.
805 if (error
== GPG_ERR_CANCELED
)
806 return GPG_ERR_CANCELED
;
808 if (pin_try
!= -1 && pin_try
--)
812 pinentry_disconnect(pwm
);
822 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
827 return GPG_ERR_NOT_IMPLEMENTED
;
830 if (!pwm
|| !pw
|| !pw
->filename
[0])
831 return GPG_ERR_INV_ARG
;
841 xfree(pwm
->filename
);
843 pwm
->filename
= xstrdup(pw
->filename
);
844 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
848 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
852 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
854 char buf
[ASSUAN_LINELENGTH
];
858 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
859 error
= send_command(pwm
, &result
, buf
);
860 memset(buf
, 0, sizeof(buf
));
868 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
872 char *password
= NULL
;
878 if (!pwm
|| !filename
|| !*filename
) {
879 *error
= GPG_ERR_INV_ARG
;
884 pin_try
= pwm
->pinentry_tries
- 1;
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");
896 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
897 pwmd_free_result(result
);
899 if (access(path
, R_OK
) == -1) {
904 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
906 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
908 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
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.
927 *error
= gpg_error_from_syserror();
940 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
941 pw
.filename
[sizeof(pw
.filename
)-1] = 0;
951 *error
= getpin(pwm
, &password
, &pin_try
, 0);
956 pinentry_disconnect(pwm
);
958 if (gtimeout
&& gelapsed
>= gtimeout
)
959 *error
= GPG_ERR_TIMEOUT
;
963 pth_write(p
[1], &pw
, sizeof(pw
));
965 write(p
[1], &pw
, sizeof(pw
));
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
);
979 signal(SIGALRM
, catchsig
);
981 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
983 goto getpin_nb_again
;
988 pinentry_disconnect(pwm
);
991 pth_write(p
[1], &pw
, sizeof(pw
));
993 write(p
[1], &pw
, sizeof(pw
));
999 *error
= gpg_error_from_syserror();
1015 * Not using pinentry and the file was not found
1018 password
= pwm
->password
;
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
)
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."));
1049 pinentry_disconnect(pwm
);
1057 xfree(pwm
->filename
);
1059 pwm
->filename
= xstrdup(filename
);
1063 * The file is cached or the file is a new file.
1066 return *error
? -1 : -2;
1068 return *error
? 1 : 0;
1071 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
1075 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
1079 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
1082 #ifndef USE_PINENTRY
1083 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1086 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
1091 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
1095 char *result
= NULL
;
1099 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
1103 pinentry_disconnect(pwm
);
1116 if (strcmp(*password
, result
)) {
1119 pinentry_disconnect(pwm
);
1120 error
= EPWMD_BADKEY
;
1125 pinentry_disconnect(pwm
);
1130 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
1132 char buf
[ASSUAN_LINELENGTH
];
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
)
1146 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
1150 #ifndef USE_PINENTRY
1151 return GPG_ERR_NOT_IMPLEMENTED
;
1154 if (!pwm
|| !pw
|| !pw
->filename
[0])
1155 return GPG_ERR_INV_ARG
;
1159 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
1163 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
1165 char *result
= NULL
;
1166 char *password
= NULL
;
1169 *error
= GPG_ERR_INV_ARG
;
1173 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
1174 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
1176 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
1178 password
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1180 else if (pwm
->use_pinentry
) {
1184 pwmd_nb_status_t pw
;
1186 if (pipe(p
) == -1) {
1187 *error
= gpg_error_from_syserror();
1200 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
1201 pw
.filename
[sizeof(pw
.filename
)-1] = 0;
1206 *error
= do_save_getpin(pwm
, &password
);
1207 } while (*error
== EPWMD_BADKEY
);
1211 pinentry_disconnect(pwm
);
1215 pth_write(p
[1], &pw
, sizeof(pw
));
1217 write(p
[1], &pw
, sizeof(pw
));
1223 *error
= do_save_command(pwm
, password
);
1224 pinentry_disconnect(pwm
);
1227 pth_write(p
[1], &pw
, sizeof(pw
));
1229 write(p
[1], &pw
, sizeof(pw
));
1235 *error
= gpg_error_from_syserror();
1248 *error
= do_save_getpin(pwm
, &password
);
1261 password
= pwm
->password
;
1263 *error
= do_save_command(pwm
, password
);
1265 if (!pwm
->passfunc
&& password
&& password
!= pwm
->password
)
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
;
1280 return do_pwmd_save(pwm
, error
, 1);
1284 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1288 do_pwmd_save(pwm
, &error
, 0);
1292 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1295 int n
= va_arg(ap
, int);
1298 gpg_error_t error
= 0;
1301 return GPG_ERR_INV_ARG
;
1306 case PWMD_OPTION_STATUS_FUNC
:
1307 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1309 case PWMD_OPTION_STATUS_DATA
:
1310 pwm
->status_data
= va_arg(ap
, void *);
1312 case PWMD_OPTION_PASSWORD_FUNC
:
1313 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1315 case PWMD_OPTION_PASSWORD_DATA
:
1316 pwm
->passdata
= va_arg(ap
, void *);
1318 case PWMD_OPTION_PASSWORD
:
1319 arg1
= va_arg(ap
, char *);
1322 xfree(pwm
->password
);
1324 pwm
->password
= xstrdup(arg1
);
1326 case PWMD_OPTION_PINENTRY
:
1327 n
= va_arg(ap
, int);
1329 if (n
!= 0 && n
!= 1) {
1331 error
= GPG_ERR_INV_VALUE
;
1334 pwm
->use_pinentry
= n
;
1335 error
= pwmd_command(pwm
, &result
, "OPTION PINENTRY=%i",
1336 !pwm
->use_pinentry
);
1340 case PWMD_OPTION_PINENTRY_TRIES
:
1341 n
= va_arg(ap
, int);
1345 error
= GPG_ERR_INV_VALUE
;
1348 pwm
->pinentry_tries
= n
;
1351 case PWMD_OPTION_PINENTRY_TIMEOUT
:
1352 n
= va_arg(ap
, int);
1356 error
= GPG_ERR_INV_VALUE
;
1359 pwm
->pinentry_timeout
= n
;
1361 if (!pwm
->use_pinentry
)
1362 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i",
1363 pwm
->pinentry_timeout
);
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
);
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",
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
);
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
);
1405 case PWMD_OPTION_PINENTRY_TITLE
:
1409 pwm
->title
= percent_escape(va_arg(ap
, char *));
1411 if (!pwm
->use_pinentry
)
1412 error
= pwmd_command(pwm
, &result
, "OPTION TITLE=%s",
1415 case PWMD_OPTION_PINENTRY_PROMPT
:
1419 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1421 if (!pwm
->use_pinentry
)
1422 error
= pwmd_command(pwm
, &result
, "OPTION PROMPT=%s",
1425 case PWMD_OPTION_PINENTRY_DESC
:
1429 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1431 if (!pwm
->use_pinentry
)
1432 error
= pwmd_command(pwm
, &result
, "OPTION DESC=%s",
1435 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
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",
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",
1456 error
= GPG_ERR_NOT_IMPLEMENTED
;
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
)
1471 return GPG_ERR_INV_ARG
;