From: Todd C. Miller Date: Wed, 19 Oct 2016 17:32:36 +0000 (-0600) Subject: add vsyslog() for systems without it. X-Git-Tag: SUDO_1_8_19^2~100 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=19c96da44d7a335e4baa19b0b70eeae8f2efdc99;p=sudo add vsyslog() for systems without it. --- diff --git a/MANIFEST b/MANIFEST index 0d8e460c8..b84839a38 100644 --- a/MANIFEST +++ b/MANIFEST @@ -111,6 +111,7 @@ lib/util/parseln.c lib/util/progname.c lib/util/pw_dup.c lib/util/reallocarray.c +lib/util/vsyslog.c lib/util/regress/atofoo/atofoo_test.c lib/util/regress/fnmatch/fnm_test.c lib/util/regress/fnmatch/fnm_test.in diff --git a/configure b/configure index c3748d662..4fadbe768 100755 --- a/configure +++ b/configure @@ -2663,7 +2663,6 @@ as_fn_append ac_func_list " pwrite" as_fn_append ac_func_list " openat" as_fn_append ac_func_list " faccessat" as_fn_append ac_func_list " wordexp" -as_fn_append ac_func_list " vsyslog" as_fn_append ac_func_list " seteuid" # Check that the precious variables saved in the cache have kept the same # value. @@ -18096,8 +18095,6 @@ done - - case "$host_os" in @@ -19894,6 +19891,32 @@ esac done fi +for ac_func in vsyslog +do : + ac_fn_c_check_func "$LINENO" "vsyslog" "ac_cv_func_vsyslog" +if test "x$ac_cv_func_vsyslog" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_VSYSLOG 1 +_ACEOF + +else + + case " $LIBOBJS " in + *" vsyslog.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS vsyslog.$ac_objext" + ;; +esac + + + for _sym in sudo_vsyslog; do + COMPAT_EXP="${COMPAT_EXP}${_sym} +" + done + + +fi +done + if test X"$with_noexec" != X"no"; then # Check for non-standard exec functions for ac_func in exect execvP execvpe diff --git a/configure.ac b/configure.ac index 55a2573f7..ba6e1f634 100644 --- a/configure.ac +++ b/configure.ac @@ -2408,7 +2408,7 @@ dnl dnl Function checks dnl AC_FUNC_GETGROUPS -AC_CHECK_FUNCS_ONCE([fexecve killpg nl_langinfo strftime pread pwrite openat faccessat wordexp vsyslog]) +AC_CHECK_FUNCS_ONCE([fexecve killpg nl_langinfo strftime pread pwrite openat faccessat wordexp]) case "$host_os" in hpux*) if test X"$ac_cv_func_pread" = X"yes"; then @@ -2692,6 +2692,10 @@ if test X"$FOUND_SHA2" = X"no"; then AC_LIBOBJ(sha2) SUDO_APPEND_COMPAT_EXP(sudo_SHA224Final sudo_SHA224Init sudo_SHA224Pad sudo_SHA224Transform sudo_SHA224Update sudo_SHA256Final sudo_SHA256Init sudo_SHA256Pad sudo_SHA256Transform sudo_SHA256Update sudo_SHA384Final sudo_SHA384Init sudo_SHA384Pad sudo_SHA384Transform sudo_SHA384Update sudo_SHA512Final sudo_SHA512Init sudo_SHA512Pad sudo_SHA512Transform sudo_SHA512Update) fi +AC_CHECK_FUNCS([vsyslog], [], [ + AC_LIBOBJ(vsyslog) + SUDO_APPEND_COMPAT_EXP(sudo_vsyslog) +]) dnl dnl Function checks for sudo_noexec dnl diff --git a/include/sudo_compat.h b/include/sudo_compat.h index 2e35131ce..c2aa3211d 100644 --- a/include/sudo_compat.h +++ b/include/sudo_compat.h @@ -487,5 +487,10 @@ __dso_public void *sudo_reallocarray(void *ptr, size_t nmemb, size_t size); # undef reallocarray # define reallocarray(_a, _b, _c) sudo_reallocarray((_a), (_b), (_c)) #endif /* HAVE_REALLOCARRAY */ +#ifndef HAVE_VSYSLOG +__dso_public void sudo_vsyslog(int pri, const char *fmt, va_list ap); +# undef vsyslog +# define vsyslog(_a, _b, _c) sudo_vsyslog((_a), (_b), (_c)) +#endif /* HAVE_VSYSLOG */ #endif /* SUDO_COMPAT_H */ diff --git a/lib/util/vsyslog.c b/lib/util/vsyslog.c new file mode 100644 index 000000000..2cc2b0b00 --- /dev/null +++ b/lib/util/vsyslog.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2016 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include +#include +#include +#ifdef HAVE_STRING_H +# include +#endif /* HAVE_STRING_H */ +#ifdef HAVE_STRINGS_H +# include +#endif /* HAVE_STRINGS_H */ +#include + +#include "sudo_compat.h" + +#ifndef HAVE_VSYSLOG +void +sudo_vsyslog(int pri, const char *fmt, va_list ap) +{ + int saved_errno = errno; + char *buf, *cp, *ep, new_fmt[1024]; + size_t len; + + /* Rewrite fmt, replacing %m with an errno string. */ + for (cp = new_fmt, ep = new_fmt + sizeof(new_fmt); *fmt != '\0'; fmt++) { + if (fmt[0] == '%' && fmt[1] == 'm') { + fmt++; + len = strlcpy(cp, strerror(saved_errno), (ep - cp)); + if (len >= (size_t)(ep - cp)) + len = (size_t)(ep - cp) - 1; + cp += len; + break; + } else { + if (fmt[0] == '%' && fmt[1] == '%') { + fmt++; + if (cp < ep - 1) + *cp++ = '%'; + } + if (cp < ep - 1) + *cp++ = *fmt; + } + } + *cp = '\0'; + + /* Format message and log it. */ + if (vasprintf(&buf, new_fmt, ap) != -1) { + syslog(pri, "%s", buf); + free(buf); + } +} +#endif /* HAVE_VSYSLOG */ diff --git a/plugins/sudoers/logging.c b/plugins/sudoers/logging.c index be31f6c2e..78da001f0 100644 --- a/plugins/sudoers/logging.c +++ b/plugins/sudoers/logging.c @@ -66,8 +66,6 @@ static char *new_logline(const char *, int); * We do an openlog(3)/closelog(3) for each message because some * authentication methods (notably PAM) use syslog(3) for their * own nefarious purposes and may call openlog(3) and closelog(3). - * Note that because we don't want to assume that all systems have - * vsyslog(3) (HP-UX doesn't) "%m" will not be expanded. */ static void mysyslog(int pri, const char *fmt, ...) @@ -77,19 +75,7 @@ mysyslog(int pri, const char *fmt, ...) va_start(ap, fmt); openlog("sudo", 0, def_syslog); -#ifdef HAVE_VSYSLOG vsyslog(pri, fmt, ap); -#else - do { - char *buf; - if (vasprintf(&buf, fmt, ap) == -1) { - sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); - } else { - syslog(pri, "%s", buf); - free(buf); - } - } while (0); -#endif va_end(ap); closelog(); debug_return;