From d37710b0f645aefd56dca855b4e0d22bc889d06f Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Thu, 22 Sep 2022 11:34:00 -0600 Subject: [PATCH] Use mkdtempat_np() and mkostempsat_np() on macOS --- config.h.in | 10 +++++----- configure | 25 ++++++++++++++++++++++++- configure.ac | 10 +++++++++- include/sudo_compat.h | 31 +++++++++++++++++++------------ lib/util/mktemp.c | 5 +++-- 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/config.h.in b/config.h.in index 32dbb9879..ba4d00056 100644 --- a/config.h.in +++ b/config.h.in @@ -602,17 +602,17 @@ /* Define to 1 if you have the `mkdirat' function. */ #undef HAVE_MKDIRAT -/* Define to 1 if you have the `mkdtemp' function. */ -#undef HAVE_MKDTEMP - /* Define to 1 if you have the `mkdtempat' function. */ #undef HAVE_MKDTEMPAT +/* Define to 1 if you have the `mkdtempat_np' function. */ +#undef HAVE_MKDTEMPAT_NP + /* Define to 1 if you have the `mkostempsat' function. */ #undef HAVE_MKOSTEMPSAT -/* Define to 1 if you have the `mkstemps' function. */ -#undef HAVE_MKSTEMPS +/* Define to 1 if you have the `mkostempsat_np' function. */ +#undef HAVE_MKOSTEMPSAT_NP /* Define to 1 if you have the header file. */ #undef HAVE_MPS_LDAP_SSL_H diff --git a/configure b/configure index f565225fb..efdf3f1f2 100755 --- a/configure +++ b/configure @@ -23338,8 +23338,11 @@ fi done sudo_mktemp=no +case "$host_os" in + darwin*) + # macOS has these but uses a _np (non-portable) suffix - for ac_func in mkdtempat mkostempsat mkstemps mkdtemp + for ac_func in mkdtempat_np mkostempsat_np do : as_ac_var=`printf "%s\n" "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" @@ -23354,6 +23357,26 @@ else $as_nop fi done + ;; + *) + + for ac_func in mkdtempat mkostempsat +do : + as_ac_var=`printf "%s\n" "ac_cv_func_$ac_func" | $as_tr_sh` +ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" +if eval test \"x\$"$as_ac_var"\" = x"yes" +then : + cat >>confdefs.h <<_ACEOF +#define `printf "%s\n" "HAVE_$ac_func" | $as_tr_cpp` 1 +_ACEOF + +else $as_nop + sudo_mktemp=yes; break +fi + +done + ;; +esac # If any of the mktemp family are missing we use our own. if test X"$sudo_mktemp" = X"yes"; then diff --git a/configure.ac b/configure.ac index 54f2c5e5a..103bc4fad 100644 --- a/configure.ac +++ b/configure.ac @@ -2962,7 +2962,15 @@ AC_CHECK_FUNCS([closefrom], [], [AC_LIBOBJ(closefrom) COMPAT_TEST_PROGS="${COMPAT_TEST_PROGS}${COMPAT_TEST_PROGS+ }closefrom_test" ]) sudo_mktemp=no -AC_CHECK_FUNCS([mkdtempat mkostempsat mkstemps mkdtemp], [], [sudo_mktemp=yes; break]) +case "$host_os" in + darwin*) + # macOS has these but uses a _np (non-portable) suffix + AC_CHECK_FUNCS([mkdtempat_np mkostempsat_np], [], [sudo_mktemp=yes; break]) + ;; + *) + AC_CHECK_FUNCS([mkdtempat mkostempsat], [], [sudo_mktemp=yes; break]) + ;; +esac # If any of the mktemp family are missing we use our own. if test X"$sudo_mktemp" = X"yes"; then AC_CHECK_FUNCS([arc4random random lrand48], [break]) diff --git a/include/sudo_compat.h b/include/sudo_compat.h index 87602154e..63dd9916c 100644 --- a/include/sudo_compat.h +++ b/include/sudo_compat.h @@ -499,23 +499,30 @@ sudo_dso_public int sudo_mkdirat(int dfd, const char *path, mode_t mode); # undef mkdirat # define mkdirat(_a, _b, _c) sudo_mkdirat((_a), (_b), (_c)) #endif /* HAVE_MKDIRAT */ -#if !defined(HAVE_MKDTEMP) || !defined(HAVE_MKDTEMPAT) || !defined(HAVE_MKOSTEMPSAT) || !defined(HAVE_MKSTEMPS) +#if !defined(HAVE_MKDTEMPAT) || !defined(HAVE_MKOSTEMPSAT) +# if defined(HAVE_MKDTEMPAT_NP) && defined(HAVE_MKOSTEMPSAT_NP) +# undef mkdtempat +# define mkdtempat mkdtempat_np +# undef mkostempsat +# define mkostempsat mkostempsat_np +# else sudo_dso_public char *sudo_mkdtemp(char *path); -# undef mkdtemp -# define mkdtemp(_a) sudo_mkdtemp((_a)) +# undef mkdtemp +# define mkdtemp(_a) sudo_mkdtemp((_a)) sudo_dso_public char *sudo_mkdtempat(int dfd, char *path); -# undef mkdtempat -# define mkdtempat(_a, _b) sudo_mkdtempat((_a), (_b)) +# undef mkdtempat +# define mkdtempat(_a, _b) sudo_mkdtempat((_a), (_b)) sudo_dso_public int sudo_mkostempsat(int dfd, char *path, int slen, int flags); -# undef mkostempsat -# define mkostempsat(_a, _b, _c, _d) sudo_mkostempsat((_a), (_b), (_c), (_d)) +# undef mkostempsat +# define mkostempsat(_a, _b, _c, _d) sudo_mkostempsat((_a), (_b), (_c), (_d)) sudo_dso_public int sudo_mkstemp(char *path); -# undef mkstemp -# define mkstemp(_a) sudo_mkstemp((_a)) +# undef mkstemp +# define mkstemp(_a) sudo_mkstemp((_a)) sudo_dso_public int sudo_mkstemps(char *path, int slen); -# undef mkstemps -# define mkstemps(_a, _b) sudo_mkstemps((_a), (_b)) -#endif /* !HAVE_MKDTEMP || !HAVE_MKDTEMPAT || !HAVE_MKOSTEMPSAT || !HAVE_MKSTEMPS */ +# undef mkstemps +# define mkstemps(_a, _b) sudo_mkstemps((_a), (_b)) +# endif /* HAVE_MKDTEMPAT_NP || HAVE_MKOSTEMPSAT_NP */ +#endif /* !HAVE_MKDTEMPAT || !HAVE_MKOSTEMPSAT */ #ifndef HAVE_NANOSLEEP sudo_dso_public int sudo_nanosleep(const struct timespec *timeout, struct timespec *remainder); #undef nanosleep diff --git a/lib/util/mktemp.c b/lib/util/mktemp.c index 395dcf501..1540e511e 100644 --- a/lib/util/mktemp.c +++ b/lib/util/mktemp.c @@ -24,7 +24,8 @@ #include -#if !defined(HAVE_MKDTEMP) || !defined(HAVE_MKDTEMPAT) || !defined(HAVE_MKOSTEMPSAT) || !defined(HAVE_MKSTEMPS) +#if (!defined(HAVE_MKDTEMPAT) && !defined(HAVE_MKDTEMPAT_NP)) || \ + (!defined(HAVE_MKOSTEMPSAT) && !defined(HAVE_MKOSTEMPSAT_NP)) #include @@ -168,4 +169,4 @@ sudo_mkstemps(char *path, int slen) { return mktemp_internal(AT_FDCWD, path, slen, MKTEMP_FILE, 0); } -#endif /* !HAVE_MKDTEMP || !HAVE_MKDTEMPAT || !HAVE_MKOSTEMPSAT || !HAVE_MKSTEMPS */ +#endif /* !HAVE_MKDTEMPAT || !HAVE_MKOSTEMPSAT */