1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2007 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 02111-1307 USA
26 #include <sys/socket.h>
35 #include <sys/types.h>
52 #define N_(msgid) dgettext("libpwmd", msgid)
58 #define xrealloc realloc
59 #define xmalloc malloc
60 #define xstrdup strdup
61 #define xcalloc calloc
68 static int gelapsed
, gtimeout
;
69 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
72 static gpg_error_t global_error
;
74 const char *pwmd_strerror(gpg_error_t e
)
76 gpg_err_code_t code
= gpg_err_code(e
);
78 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
82 return N_("Unknown error");
84 return N_("No cache slots available");
86 return N_("Recursion loop");
88 return N_("No file is open");
90 return N_("General LibXML error");
92 return N_("File modified");
96 return gpg_strerror(e
);
99 gpg_error_t
pwmd_init()
102 bindtextdomain("libpwmd", LOCALEDIR
);
105 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
106 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
110 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
113 char *socketpath
= NULL
;
116 assuan_context_t ctx
;
120 pw
= getpwuid(getuid());
121 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
122 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
125 socketpath
= xstrdup(path
);
127 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
135 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
136 *error
= gpg_error_from_errno(errno
);
137 assuan_disconnect(ctx
);
144 pwm
->pinentry_tries
= 3;
152 void pwmd_close(pwm_t
*pwm
)
158 assuan_disconnect(pwm
->ctx
);
161 xfree(pwm
->password
);
172 if (pwm
->pinentry_tty
)
173 xfree(pwm
->pinentry_tty
);
175 if (pwm
->pinentry_display
)
176 xfree(pwm
->pinentry_display
);
178 if (pwm
->pinentry_term
)
179 xfree(pwm
->pinentry_term
);
182 xfree(pwm
->filename
);
187 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
189 membuf_t
*mem
= (membuf_t
*)data
;
195 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
199 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
204 void pwmd_free_result(void *data
)
209 static int _inquire_cb(void *data
, const char *keyword
)
211 pwm_t
*pwm
= (pwm_t
*)data
;
214 if (pwm
->inquire_func
) {
219 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
, &len
);
220 rc
= gpg_err_code(rc
);
222 if (rc
== GPG_ERR_EOF
) {
229 rc
= assuan_send_data(pwm
->ctx
, result
, len
);
238 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
239 char **result
, const char *cmd
)
247 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, _inquire_cb
, pwm
,
248 pwm
->status_func
, pwm
->status_data
);
258 mem_realloc_cb(&data
, "", 1);
259 *result
= (char *)data
.buf
;
263 return gpg_err_code(rc
);
266 gpg_error_t
pwmd_inquire(pwm_t
*pwm
, const char *cmd
)
273 return GPG_ERR_INV_ARG
;
275 error
= assuan_write_line(pwm
->ctx
, cmd
);
280 error
= assuan_read_line(pwm
->ctx
, &line
, &len
);
284 gpg_error_t
pwmd_inquire_process(pwm_t
*pwm
, const void *buf
, size_t len
)
289 return GPG_ERR_INV_ARG
;
291 error
= assuan_send_data(pwm
->ctx
, buf
, len
);
295 gpg_error_t
pwmd_inquire_finish(pwm_t
*pwm
)
302 return GPG_ERR_INV_ARG
;
304 error
= assuan_send_data(pwm
->ctx
, NULL
, 0);
309 error
= assuan_read_line(pwm
->ctx
, &line
, &len
);
313 gpg_error_t
pwmd_terminate_pinentry(pwm_t
*pwm
)
316 return GPG_ERR_NOT_IMPLEMENTED
;
319 if (!pwm
|| pwm
->pid
== -1)
320 return GPG_ERR_INV_ARG
;
322 if (kill(pwm
->pid
, 0) == 0) {
323 if (kill(pwm
->pid
, SIGTERM
) == -1) {
324 if (kill(pwm
->pid
, SIGKILL
) == -1)
325 return gpg_error_from_errno(errno
);
328 global_error
= GPG_ERR_TIMEOUT
;
331 return gpg_error_from_errno(errno
);
337 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
340 char tmp
[ASSUAN_LINELENGTH
];
344 pwm
->title
= xstrdup(N_("LibPWMD"));
347 pwm
->prompt
= xstrdup(N_("Password:"));
349 if (!pwm
->desc
&& !which
)
350 pwm
->desc
= xstrdup(N_("Enter a password."));
353 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Invalid password, please try again."));
356 else if (which
== 2) {
357 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Please type the password again for confirmation."));
361 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
362 sprintf(buf
, "SETERROR %s", pwm
->desc
);
365 error
= pinentry_command(pwm
, NULL
, buf
);
371 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
372 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
373 error
= pinentry_command(pwm
, NULL
, buf
);
379 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
380 sprintf(buf
, "SETDESC %s", pwm
->title
);
381 error
= pinentry_command(pwm
, NULL
, buf
);
386 static void update_pinentry_settings(pwm_t
*pwm
)
390 struct passwd
*pw
= getpwuid(getuid());
393 snprintf(buf
, sizeof(buf
), "%s/.pwmd/env", pw
->pw_dir
);
395 if ((fp
= fopen(buf
, "r")) == NULL
)
398 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
399 char name
[32], val
[256];
401 if (sscanf(p
, "%[a-zA-Z]=%s", name
, val
) != 2)
404 if (strcasecmp(name
, "TTY") == 0) {
405 if (!pwm
->pinentry_tty
) {
406 xfree(pwm
->pinentry_tty
);
407 pwm
->pinentry_tty
= xstrdup(val
);
410 else if (strcasecmp(name
, "TERM") == 0) {
411 if (!pwm
->pinentry_term
) {
412 xfree(pwm
->pinentry_term
);
413 pwm
->pinentry_term
= xstrdup(val
);
416 else if (strcasecmp(name
, "DISPLAY") == 0) {
417 if (!pwm
->pinentry_display
) {
418 xfree(pwm
->pinentry_display
);
419 pwm
->pinentry_display
= xstrdup(val
);
427 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
430 assuan_context_t ctx
;
431 int child_list
[] = {-1};
432 char *display
= getenv("DISPLAY");
434 int have_display
= 0;
437 update_pinentry_settings(pwm
);
439 if (pwm
->pinentry_display
|| display
)
442 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
445 return gpg_error_from_errno(errno
);
448 if (!display
&& !tty
)
449 return GPG_ERR_ENOTTY
;
451 argv
[0] = "pinentry";
452 argv
[1] = have_display
? "--display" : "--ttyname";
453 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
457 argv
[3] = "--ttytype";
458 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
462 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
467 pwm
->pid
= assuan_get_pid(ctx
);
469 return set_pinentry_strings(pwm
, 0);
472 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
477 n
= launch_pinentry(pwm
);
483 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
486 static void pinentry_disconnect(pwm_t
*pwm
)
489 assuan_disconnect(pwm
->pctx
);
496 * Only called from a child process.
498 static void catchsig(int sig
)
502 if (gelapsed
++ >= gtimeout
) {
503 global_error
= pwmd_terminate_pinentry(gpwm
);
506 global_error
= GPG_ERR_TIMEOUT
;
518 static char *percent_escape(const char *atext
)
520 const unsigned char *s
;
521 int len
= strlen(atext
) * 3 + 1;
522 char *buf
= (char *)xmalloc(len
), *p
= buf
;
527 for (s
=(const unsigned char *)atext
; *s
; s
++) {
529 sprintf (p
, "%%%02X", *s
);
541 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
544 return GPG_ERR_INV_ARG
;
546 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
550 * Avoid sending the BYE command here. libassuan will close the file
551 * descriptor and release the assuan context. Use pwmd_close() instead.
553 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
561 return GPG_ERR_INV_ARG
;
568 len
= vsnprintf(NULL
, 0, cmd
, ap
);
569 buf
= (char *)xmalloc(len
+ 1);
570 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
572 error
= send_command(pwm
, result
, buf
);
578 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
583 signal(SIGALRM
, catchsig
);
588 error
= pinentry_command(pwm
, result
, "GETPIN");
590 if (error
== GPG_ERR_ASS_CANCELED
)
599 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
601 int pin_try
= *try_n
;
608 error
= set_pinentry_strings(pwm
, which
);
611 pinentry_disconnect(pwm
);
616 if (pwm
->pinentry_tries
-1 != pin_try
) {
617 error
= set_pinentry_strings(pwm
, 1);
620 pinentry_disconnect(pwm
);
626 error
= do_getpin(pwm
, result
);
629 * Since there was input cancel any timeout.
634 if (pin_try
!= -1 && pin_try
-- && error
== EPWMD_KEY
)
638 pinentry_disconnect(pwm
);
648 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
654 return GPG_ERR_NOT_IMPLEMENTED
;
657 if (!pwm
|| !pw
|| !pw
->filename
[0])
658 return GPG_ERR_INV_ARG
;
667 error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pw
->filename
);
673 xfree(pwm
->filename
);
675 pwm
->filename
= xstrdup(pw
->filename
);
676 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
680 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
684 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
686 char buf
[ASSUAN_LINELENGTH
];
689 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
690 error
= send_command(pwm
, NULL
, buf
);
691 memset(buf
, 0, sizeof(buf
));
695 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
699 char *password
= NULL
;
705 if (!pwm
|| !filename
|| !*filename
) {
706 *error
= GPG_ERR_INV_ARG
;
711 pin_try
= pwm
->pinentry_tries
- 1;
715 * Avoid calling pinentry if the password is cached on the server or if
716 * this is a new file.
718 *error
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
723 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
724 pwmd_free_result(result
);
726 if (access(path
, R_OK
) == -1) {
730 *error
= gpg_error_from_errno(errno
);
734 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
736 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
738 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
740 if (!password
|| !*password
) {
745 password
= xstrdup(password
);
749 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
752 * Get the password from pinentry.
754 if (pwm
->use_pinentry
) {
756 * Nonblocking is wanted. fork() then return a file descriptor
757 * that the client can use to read() from.
765 *error
= gpg_error_from_syserror();
774 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
784 *error
= getpin(pwm
, &password
, &pin_try
, 0);
789 pinentry_disconnect(pwm
);
791 if (gtimeout
&& gelapsed
>= gtimeout
)
792 *error
= GPG_ERR_TIMEOUT
;
795 write(p
[1], &pw
, sizeof(pw
));
801 * Don't count the time it takes to open the file
802 * which may have many iterations.
804 signal(SIGALRM
, SIG_DFL
);
805 *error
= do_open_command(pwm
, filename
, password
);
808 signal(SIGALRM
, catchsig
);
810 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
812 goto getpin_nb_again
;
817 pinentry_disconnect(pwm
);
819 write(p
[1], &pw
, sizeof(pw
));
824 *error
= gpg_error_from_syserror();
837 *error
= getpin(pwm
, &password
, &pin_try
, 1);
841 pinentry_disconnect(pwm
);
844 *error
= global_error
;
854 * Not using pinentry and the file was not found
857 if (pwm
->password
== NULL
) {
862 password
= pwm
->password
;
872 *error
= do_open_command(pwm
, filename
, password
);
875 * Keep the user defined password set with pwmd_setopt(). The password may
876 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
878 if (password
&& password
!= pwm
->password
)
882 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
886 pinentry_disconnect(pwm
);
893 xfree(pwm
->filename
);
895 pwm
->filename
= xstrdup(filename
);
899 * The file is cached or the file is a new file.
902 return *error
? -1 : -2;
904 return *error
? 1 : 0;
907 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
911 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
915 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
919 *error
= GPG_ERR_NOT_IMPLEMENTED
;
922 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
927 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
935 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
939 pinentry_disconnect(pwm
);
952 if (strcmp(*password
, result
)) {
955 pinentry_disconnect(pwm
);
956 error
= EPWMD_BADKEY
;
961 pinentry_disconnect(pwm
);
966 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
968 char buf
[ASSUAN_LINELENGTH
];
971 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
972 error
= send_command(pwm
, NULL
, buf
);
973 memset(&buf
, 0, sizeof(buf
));
977 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
982 return GPG_ERR_NOT_IMPLEMENTED
;
985 if (!pwm
|| !pw
|| !pw
->filename
[0])
986 return GPG_ERR_INV_ARG
;
992 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
996 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
1000 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
1002 char *result
= NULL
;
1003 char *password
= NULL
;
1006 *error
= GPG_ERR_INV_ARG
;
1010 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
1011 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
1013 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
1015 if (pwm
->use_pinentry
) {
1019 pwmd_nb_status_t pw
;
1021 if (pipe(p
) == -1) {
1022 *error
= gpg_error_from_syserror();
1031 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
1036 *error
= do_save_getpin(pwm
, &password
);
1037 } while (*error
== EPWMD_KEY
|| *error
== EPWMD_BADKEY
);
1041 pinentry_disconnect(pwm
);
1044 write(p
[1], &pw
, sizeof(pw
));
1049 *error
= do_save_command(pwm
, password
);
1050 pinentry_disconnect(pwm
);
1052 write(p
[1], &pw
, sizeof(pw
));
1057 *error
= gpg_error_from_syserror();
1069 *error
= do_save_getpin(pwm
, &password
);
1076 if (pwm
->passfunc
) {
1077 char *tmp
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1079 if (!tmp
|| !*tmp
) {
1084 password
= xstrdup(tmp
);
1090 if (!password
|| !*password
) {
1101 password
= pwm
->password
;
1103 *error
= do_save_command(pwm
, password
);
1105 if (password
&& password
!= pwm
->password
)
1109 return *error
? -1 : -2;
1111 return *error
? 1 : 0;
1114 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1116 #ifndef USE_PINENTRY
1117 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1120 return do_pwmd_save(pwm
, error
, 1);
1124 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1128 do_pwmd_save(pwm
, &error
, 0);
1132 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1136 int n
= va_arg(ap
, int);
1141 return GPG_ERR_INV_ARG
;
1146 case PWMD_OPTION_INQUIRE_FUNC
:
1147 pwm
->inquire_func
= va_arg(ap
, pwmd_inquire_fn
);
1149 case PWMD_OPTION_INQUIRE_DATA
:
1150 pwm
->inquire_data
= va_arg(ap
, void *);
1152 case PWMD_OPTION_STATUS_FUNC
:
1153 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1155 case PWMD_OPTION_STATUS_DATA
:
1156 pwm
->status_data
= va_arg(ap
, void *);
1158 case PWMD_OPTION_PASSWORD_FUNC
:
1159 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1161 case PWMD_OPTION_PASSWORD_DATA
:
1162 pwm
->passdata
= va_arg(ap
, void *);
1164 case PWMD_OPTION_PASSWORD
:
1165 arg1
= va_arg(ap
, char *);
1168 xfree(pwm
->password
);
1170 pwm
->password
= xstrdup(arg1
);
1173 case PWMD_OPTION_PINENTRY
:
1174 n
= va_arg(ap
, int);
1176 if (n
!= 0 && n
!= 1) {
1178 return GPG_ERR_INV_VALUE
;
1181 pwm
->use_pinentry
= n
;
1183 case PWMD_OPTION_PINENTRY_TRIES
:
1184 n
= va_arg(ap
, int);
1188 return GPG_ERR_INV_VALUE
;
1191 pwm
->pinentry_tries
= n
;
1193 case PWMD_OPTION_PINENTRY_PATH
:
1194 if (pwm
->pinentry_path
)
1195 xfree(pwm
->pinentry_path
);
1197 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1199 case PWMD_OPTION_PINENTRY_TTY
:
1200 if (pwm
->pinentry_tty
)
1201 xfree(pwm
->pinentry_tty
);
1203 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1205 case PWMD_OPTION_PINENTRY_DISPLAY
:
1206 if (pwm
->pinentry_display
)
1207 xfree(pwm
->pinentry_display
);
1209 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1211 case PWMD_OPTION_PINENTRY_TERM
:
1212 if (pwm
->pinentry_term
)
1213 xfree(pwm
->pinentry_term
);
1215 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1217 case PWMD_OPTION_PINENTRY_TITLE
:
1220 pwm
->title
= percent_escape(va_arg(ap
, char *));
1222 case PWMD_OPTION_PINENTRY_PROMPT
:
1225 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1227 case PWMD_OPTION_PINENTRY_DESC
:
1230 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1233 case PWMD_OPTION_PINENTRY
:
1234 case PWMD_OPTION_PINENTRY_TRIES
:
1235 case PWMD_OPTION_PINENTRY_PATH
:
1236 case PWMD_OPTION_PINENTRY_TTY
:
1237 case PWMD_OPTION_PINENTRY_DISPLAY
:
1238 case PWMD_OPTION_PINENTRY_TERM
:
1239 case PWMD_OPTION_PINENTRY_TITLE
:
1240 case PWMD_OPTION_PINENTRY_PROMPT
:
1241 case PWMD_OPTION_PINENTRY_DESC
:
1242 return GPG_ERR_NOT_IMPLEMENTED
;
1246 return GPG_ERR_NOT_IMPLEMENTED
;