add vsyslog() for systems without it.

This commit is contained in:
Todd C. Miller
2016-10-19 11:32:36 -06:00
parent 6fa59b7416
commit 19c96da44d
6 changed files with 105 additions and 18 deletions

View File

@@ -111,6 +111,7 @@ lib/util/parseln.c
lib/util/progname.c lib/util/progname.c
lib/util/pw_dup.c lib/util/pw_dup.c
lib/util/reallocarray.c lib/util/reallocarray.c
lib/util/vsyslog.c
lib/util/regress/atofoo/atofoo_test.c lib/util/regress/atofoo/atofoo_test.c
lib/util/regress/fnmatch/fnm_test.c lib/util/regress/fnmatch/fnm_test.c
lib/util/regress/fnmatch/fnm_test.in lib/util/regress/fnmatch/fnm_test.in

29
configure vendored
View File

@@ -2663,7 +2663,6 @@ as_fn_append ac_func_list " pwrite"
as_fn_append ac_func_list " openat" as_fn_append ac_func_list " openat"
as_fn_append ac_func_list " faccessat" as_fn_append ac_func_list " faccessat"
as_fn_append ac_func_list " wordexp" as_fn_append ac_func_list " wordexp"
as_fn_append ac_func_list " vsyslog"
as_fn_append ac_func_list " seteuid" as_fn_append ac_func_list " seteuid"
# Check that the precious variables saved in the cache have kept the same # Check that the precious variables saved in the cache have kept the same
# value. # value.
@@ -18096,8 +18095,6 @@ done
case "$host_os" in case "$host_os" in
@@ -19894,6 +19891,32 @@ esac
done done
fi 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 if test X"$with_noexec" != X"no"; then
# Check for non-standard exec functions # Check for non-standard exec functions
for ac_func in exect execvP execvpe for ac_func in exect execvP execvpe

View File

@@ -2408,7 +2408,7 @@ dnl
dnl Function checks dnl Function checks
dnl dnl
AC_FUNC_GETGROUPS 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 case "$host_os" in
hpux*) hpux*)
if test X"$ac_cv_func_pread" = X"yes"; then 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) 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) 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 fi
AC_CHECK_FUNCS([vsyslog], [], [
AC_LIBOBJ(vsyslog)
SUDO_APPEND_COMPAT_EXP(sudo_vsyslog)
])
dnl dnl
dnl Function checks for sudo_noexec dnl Function checks for sudo_noexec
dnl dnl

View File

@@ -487,5 +487,10 @@ __dso_public void *sudo_reallocarray(void *ptr, size_t nmemb, size_t size);
# undef reallocarray # undef reallocarray
# define reallocarray(_a, _b, _c) sudo_reallocarray((_a), (_b), (_c)) # define reallocarray(_a, _b, _c) sudo_reallocarray((_a), (_b), (_c))
#endif /* HAVE_REALLOCARRAY */ #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 */ #endif /* SUDO_COMPAT_H */

68
lib/util/vsyslog.c Normal file
View File

@@ -0,0 +1,68 @@
/*
* Copyright (c) 2016 Todd C. Miller <Todd.Miller@courtesan.com>
*
* 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 <config.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <syslog.h>
#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 */

View File

@@ -66,8 +66,6 @@ static char *new_logline(const char *, int);
* We do an openlog(3)/closelog(3) for each message because some * We do an openlog(3)/closelog(3) for each message because some
* authentication methods (notably PAM) use syslog(3) for their * authentication methods (notably PAM) use syslog(3) for their
* own nefarious purposes and may call openlog(3) and closelog(3). * 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 static void
mysyslog(int pri, const char *fmt, ...) mysyslog(int pri, const char *fmt, ...)
@@ -77,19 +75,7 @@ mysyslog(int pri, const char *fmt, ...)
va_start(ap, fmt); va_start(ap, fmt);
openlog("sudo", 0, def_syslog); openlog("sudo", 0, def_syslog);
#ifdef HAVE_VSYSLOG
vsyslog(pri, fmt, ap); 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); va_end(ap);
closelog(); closelog();
debug_return; debug_return;