From 6055b9712b3896861e65d009886ad4d03e5d50d6 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 26 Aug 2019 06:46:09 -0700 Subject: crypt: detect and use glibc's crypt_r. * configure: New test for crypt_r, depositing HAVE_CRYPT_R preprocessor symbol in config.h. * sysif.c: Conditionally include header. (crypt_wrap): Use crypt_r if HAVE_CRYPT_R is nonzero. (sysif_init): Register crypt intrinsic if we HAVE_CRYPT or if we HAVE_CRYPT_R. --- configure | 29 +++++++++++++++++++++++++++++ sysif.c | 12 ++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/configure b/configure index 4e33c2fa..e54eb2b5 100755 --- a/configure +++ b/configure @@ -2619,6 +2619,35 @@ for try_lcrypt in "" "-lcrypt" "no" ; do fi done +printf "Checking for crypt_r ... " + +cat > conftest.c < + +int main(void) +{ + static struct crypt_data cd; + char *c = crypt_r("foo", "bar", &cd); + return 0; +} +! + +for try_lcrypt in "" "-lcrypt" "no" ; do + if [ "$try_lcrypt" = "no" ] ; then + printf "no\n" + break + fi + if conftest EXTRA_LDFLAGS=$try_lcrypt; then + printf "yes\n" + printf "#define HAVE_CRYPT_R 1\n" >> config.h + if [ -n "$try_lcrypt" ] ; then + conf_ldflags="${conf_ldflags:+"$conf_ldflags "}-lcrypt" + fi + break; + fi +done + + printf "Checking for alloca ... " for try_header in stdlib alloca malloc ; do diff --git a/sysif.c b/sysif.c index 1cce32db..f1eb5fa4 100644 --- a/sysif.c +++ b/sysif.c @@ -76,6 +76,9 @@ #if HAVE_DLOPEN #include #endif +#if HAVE_CRYPT_R +#include +#endif #include "alloca.h" #include "lib.h" #include "stream.h" @@ -1446,7 +1449,7 @@ static val getgrnam_wrap(val wname) #endif -#if HAVE_CRYPT +#if HAVE_CRYPT || HAVE_CRYPT_R static val crypt_wrap(val wkey, val wsalt) { @@ -1454,7 +1457,12 @@ static val crypt_wrap(val wkey, val wsalt) const wchar_t *cwsalt = c_str(wsalt); char *key = utf8_dup_to(cwkey); char *salt = utf8_dup_to(cwsalt); +#if HAVE_CRYPT_R + struct crypt_data cd; + char *hash = (cd.initialized = 0, crypt_r(key, salt, &cd)); +#else char *hash = crypt(key, salt); +#endif val whash = string_utf8(hash); free(key); free(salt); @@ -2137,7 +2145,7 @@ void sysif_init(void) reg_fun(intern(lit("getgrnam"), user_package), func_n1(getgrnam_wrap)); #endif -#if HAVE_CRYPT +#if HAVE_CRYPT || HAVE_CRYPT_R reg_fun(intern(lit("crypt"), user_package), func_n2(crypt_wrap)); #endif -- cgit v1.2.3