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 /* Shouldn't get this far without a callback. */
215 if (!pwm
->inquire_func
)
216 return GPG_ERR_INV_ARG
;
222 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
, &len
);
223 rc
= gpg_err_code(rc
);
225 if (rc
== GPG_ERR_EOF
) {
232 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
, pwmd_inquire_fn fn
,
269 if (!pwm
|| !cmd
|| !fn
)
270 return GPG_ERR_INV_ARG
;
272 pwm
->inquire_func
= fn
;
273 pwm
->inquire_data
= data
;
274 return assuan_command(pwm
, pwm
->ctx
, NULL
, cmd
);
277 gpg_error_t
pwmd_terminate_pinentry(pwm_t
*pwm
)
280 return GPG_ERR_NOT_IMPLEMENTED
;
283 if (!pwm
|| pwm
->pid
== -1)
284 return GPG_ERR_INV_ARG
;
286 if (kill(pwm
->pid
, 0) == 0) {
287 if (kill(pwm
->pid
, SIGTERM
) == -1) {
288 if (kill(pwm
->pid
, SIGKILL
) == -1)
289 return gpg_error_from_errno(errno
);
292 global_error
= GPG_ERR_TIMEOUT
;
295 return gpg_error_from_errno(errno
);
301 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
304 char tmp
[ASSUAN_LINELENGTH
];
308 pwm
->title
= xstrdup(N_("LibPWMD"));
311 pwm
->prompt
= xstrdup(N_("Password:"));
313 if (!pwm
->desc
&& !which
)
314 pwm
->desc
= xstrdup(N_("Enter a password."));
317 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Invalid password, please try again."));
320 else if (which
== 2) {
321 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Please type the password again for confirmation."));
325 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
326 sprintf(buf
, "SETERROR %s", pwm
->desc
);
329 error
= pinentry_command(pwm
, NULL
, buf
);
335 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
336 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
337 error
= pinentry_command(pwm
, NULL
, buf
);
343 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
344 sprintf(buf
, "SETDESC %s", pwm
->title
);
345 error
= pinentry_command(pwm
, NULL
, buf
);
350 static void update_pinentry_settings(pwm_t
*pwm
)
354 struct passwd
*pw
= getpwuid(getuid());
357 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
->pw_dir
);
359 if ((fp
= fopen(buf
, "r")) == NULL
)
362 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
363 char name
[32], val
[256];
365 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
368 if (strcasecmp(name
, "TTYNAME") == 0) {
369 xfree(pwm
->pinentry_tty
);
370 pwm
->pinentry_tty
= xstrdup(val
);
372 else if (strcasecmp(name
, "TTYTYPE") == 0) {
373 xfree(pwm
->pinentry_term
);
374 pwm
->pinentry_term
= xstrdup(val
);
376 else if (strcasecmp(name
, "DISPLAY") == 0) {
377 xfree(pwm
->pinentry_display
);
378 pwm
->pinentry_display
= xstrdup(val
);
380 else if (strcasecmp(name
, "PATH") == 0) {
381 xfree(pwm
->pinentry_path
);
382 pwm
->pinentry_path
= xstrdup(val
);
389 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
392 assuan_context_t ctx
;
393 int child_list
[] = {-1};
394 char *display
= getenv("DISPLAY");
396 int have_display
= 0;
399 update_pinentry_settings(pwm
);
401 if (pwm
->pinentry_display
|| display
)
404 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
407 return gpg_error_from_errno(errno
);
410 if (!display
&& !tty
)
411 return GPG_ERR_ENOTTY
;
413 argv
[0] = "pinentry";
414 argv
[1] = have_display
? "--display" : "--ttyname";
415 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
419 argv
[3] = "--ttytype";
420 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
424 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
429 pwm
->pid
= assuan_get_pid(ctx
);
431 return set_pinentry_strings(pwm
, 0);
434 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
439 n
= launch_pinentry(pwm
);
445 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
448 static void pinentry_disconnect(pwm_t
*pwm
)
451 assuan_disconnect(pwm
->pctx
);
458 * Only called from a child process.
460 static void catchsig(int sig
)
464 if (gelapsed
++ >= gtimeout
) {
465 global_error
= pwmd_terminate_pinentry(gpwm
);
468 global_error
= GPG_ERR_TIMEOUT
;
480 static char *percent_escape(const char *atext
)
482 const unsigned char *s
;
483 int len
= strlen(atext
) * 3 + 1;
484 char *buf
= (char *)xmalloc(len
), *p
= buf
;
489 for (s
=(const unsigned char *)atext
; *s
; s
++) {
491 sprintf (p
, "%%%02X", *s
);
503 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
506 return GPG_ERR_INV_ARG
;
508 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
512 * Avoid sending the BYE command here. libassuan will close the file
513 * descriptor and release the assuan context. Use pwmd_close() instead.
515 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
523 return GPG_ERR_INV_ARG
;
530 len
= vsnprintf(NULL
, 0, cmd
, ap
);
531 buf
= (char *)xmalloc(len
+ 1);
532 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
534 error
= send_command(pwm
, result
, buf
);
540 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
545 signal(SIGALRM
, catchsig
);
550 error
= pinentry_command(pwm
, result
, "GETPIN");
552 if (error
== GPG_ERR_ASS_CANCELED
|| error
== ASSUAN_Canceled
)
553 return GPG_ERR_ASS_CANCELED
;
561 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
563 int pin_try
= *try_n
;
570 error
= set_pinentry_strings(pwm
, which
);
573 pinentry_disconnect(pwm
);
578 if (pwm
->pinentry_tries
-1 != pin_try
) {
579 error
= set_pinentry_strings(pwm
, 1);
582 pinentry_disconnect(pwm
);
588 error
= do_getpin(pwm
, result
);
591 * Since there was input cancel any timeout.
596 if (pin_try
!= -1 && pin_try
-- && error
== EPWMD_KEY
)
600 pinentry_disconnect(pwm
);
610 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
616 return GPG_ERR_NOT_IMPLEMENTED
;
619 if (!pwm
|| !pw
|| !pw
->filename
[0])
620 return GPG_ERR_INV_ARG
;
629 error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pw
->filename
);
635 xfree(pwm
->filename
);
637 pwm
->filename
= xstrdup(pw
->filename
);
638 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
642 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
646 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
648 char buf
[ASSUAN_LINELENGTH
];
651 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
652 error
= send_command(pwm
, NULL
, buf
);
653 memset(buf
, 0, sizeof(buf
));
657 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
661 char *password
= NULL
;
667 if (!pwm
|| !filename
|| !*filename
) {
668 *error
= GPG_ERR_INV_ARG
;
673 pin_try
= pwm
->pinentry_tries
- 1;
677 * Avoid calling pinentry if the password is cached on the server or if
678 * this is a new file.
680 *error
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
685 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
686 pwmd_free_result(result
);
688 if (access(path
, R_OK
) == -1) {
692 *error
= gpg_error_from_errno(errno
);
696 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
698 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
700 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
702 if (!password
|| !*password
) {
707 password
= xstrdup(password
);
711 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
714 * Get the password from pinentry.
716 if (pwm
->use_pinentry
) {
718 * Nonblocking is wanted. fork() then return a file descriptor
719 * that the client can use to read() from.
727 *error
= gpg_error_from_syserror();
736 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
746 *error
= getpin(pwm
, &password
, &pin_try
, 0);
751 pinentry_disconnect(pwm
);
753 if (gtimeout
&& gelapsed
>= gtimeout
)
754 *error
= GPG_ERR_TIMEOUT
;
757 write(p
[1], &pw
, sizeof(pw
));
763 * Don't count the time it takes to open the file
764 * which may have many iterations.
766 signal(SIGALRM
, SIG_DFL
);
767 *error
= do_open_command(pwm
, filename
, password
);
770 signal(SIGALRM
, catchsig
);
772 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
774 goto getpin_nb_again
;
779 pinentry_disconnect(pwm
);
781 write(p
[1], &pw
, sizeof(pw
));
786 *error
= gpg_error_from_syserror();
799 *error
= getpin(pwm
, &password
, &pin_try
, 1);
803 pinentry_disconnect(pwm
);
806 *error
= global_error
;
816 * Not using pinentry and the file was not found
820 if (pwm
->password
== NULL
) {
826 password
= pwm
->password
;
836 *error
= do_open_command(pwm
, filename
, password
);
839 * Keep the user defined password set with pwmd_setopt(). The password may
840 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
842 if (password
&& password
!= pwm
->password
)
846 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
850 pinentry_disconnect(pwm
);
857 xfree(pwm
->filename
);
859 pwm
->filename
= xstrdup(filename
);
863 * The file is cached or the file is a new file.
866 return *error
? -1 : -2;
868 return *error
? 1 : 0;
871 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
875 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
879 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
883 *error
= GPG_ERR_NOT_IMPLEMENTED
;
886 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
891 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
899 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
903 pinentry_disconnect(pwm
);
916 if (strcmp(*password
, result
)) {
919 pinentry_disconnect(pwm
);
920 error
= EPWMD_BADKEY
;
925 pinentry_disconnect(pwm
);
930 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
932 char buf
[ASSUAN_LINELENGTH
];
935 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
936 error
= send_command(pwm
, NULL
, buf
);
937 memset(&buf
, 0, sizeof(buf
));
941 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
946 return GPG_ERR_NOT_IMPLEMENTED
;
949 if (!pwm
|| !pw
|| !pw
->filename
[0])
950 return GPG_ERR_INV_ARG
;
956 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
960 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
964 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
967 char *password
= NULL
;
970 *error
= GPG_ERR_INV_ARG
;
974 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
975 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
977 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
979 if (pwm
->use_pinentry
) {
986 *error
= gpg_error_from_syserror();
995 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
1000 *error
= do_save_getpin(pwm
, &password
);
1001 } while (*error
== EPWMD_KEY
|| *error
== EPWMD_BADKEY
);
1005 pinentry_disconnect(pwm
);
1008 write(p
[1], &pw
, sizeof(pw
));
1013 *error
= do_save_command(pwm
, password
);
1014 pinentry_disconnect(pwm
);
1016 write(p
[1], &pw
, sizeof(pw
));
1021 *error
= gpg_error_from_syserror();
1033 *error
= do_save_getpin(pwm
, &password
);
1040 if (pwm
->passfunc
) {
1041 char *tmp
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1043 if (!tmp
|| !*tmp
) {
1048 password
= xstrdup(tmp
);
1054 if (!password
|| !*password
) {
1065 password
= pwm
->password
;
1067 *error
= do_save_command(pwm
, password
);
1069 if (password
&& password
!= pwm
->password
)
1073 return *error
? -1 : -2;
1075 return *error
? 1 : 0;
1078 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1080 #ifndef USE_PINENTRY
1081 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1084 return do_pwmd_save(pwm
, error
, 1);
1088 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1092 do_pwmd_save(pwm
, &error
, 0);
1096 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1100 int n
= va_arg(ap
, int);
1105 return GPG_ERR_INV_ARG
;
1110 case PWMD_OPTION_STATUS_FUNC
:
1111 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1113 case PWMD_OPTION_STATUS_DATA
:
1114 pwm
->status_data
= va_arg(ap
, void *);
1116 case PWMD_OPTION_PASSWORD_FUNC
:
1117 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1119 case PWMD_OPTION_PASSWORD_DATA
:
1120 pwm
->passdata
= va_arg(ap
, void *);
1122 case PWMD_OPTION_PASSWORD
:
1123 arg1
= va_arg(ap
, char *);
1126 xfree(pwm
->password
);
1128 pwm
->password
= xstrdup(arg1
);
1131 case PWMD_OPTION_PINENTRY
:
1132 n
= va_arg(ap
, int);
1134 if (n
!= 0 && n
!= 1) {
1136 return GPG_ERR_INV_VALUE
;
1139 pwm
->use_pinentry
= n
;
1141 case PWMD_OPTION_PINENTRY_TRIES
:
1142 n
= va_arg(ap
, int);
1146 return GPG_ERR_INV_VALUE
;
1149 pwm
->pinentry_tries
= n
;
1151 case PWMD_OPTION_PINENTRY_PATH
:
1152 if (pwm
->pinentry_path
)
1153 xfree(pwm
->pinentry_path
);
1155 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1157 case PWMD_OPTION_PINENTRY_TTY
:
1158 if (pwm
->pinentry_tty
)
1159 xfree(pwm
->pinentry_tty
);
1161 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1163 case PWMD_OPTION_PINENTRY_DISPLAY
:
1164 if (pwm
->pinentry_display
)
1165 xfree(pwm
->pinentry_display
);
1167 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1169 case PWMD_OPTION_PINENTRY_TERM
:
1170 if (pwm
->pinentry_term
)
1171 xfree(pwm
->pinentry_term
);
1173 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1175 case PWMD_OPTION_PINENTRY_TITLE
:
1178 pwm
->title
= percent_escape(va_arg(ap
, char *));
1180 case PWMD_OPTION_PINENTRY_PROMPT
:
1183 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1185 case PWMD_OPTION_PINENTRY_DESC
:
1188 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1191 case PWMD_OPTION_PINENTRY
:
1192 case PWMD_OPTION_PINENTRY_TRIES
:
1193 case PWMD_OPTION_PINENTRY_PATH
:
1194 case PWMD_OPTION_PINENTRY_TTY
:
1195 case PWMD_OPTION_PINENTRY_DISPLAY
:
1196 case PWMD_OPTION_PINENTRY_TERM
:
1197 case PWMD_OPTION_PINENTRY_TITLE
:
1198 case PWMD_OPTION_PINENTRY_PROMPT
:
1199 case PWMD_OPTION_PINENTRY_DESC
:
1200 return GPG_ERR_NOT_IMPLEMENTED
;
1204 return GPG_ERR_NOT_IMPLEMENTED
;