From 321aa05b3b4744f6f66f4bd278e38901174c7aa8 Mon Sep 17 00:00:00 2001 From: oshogbo Date: Sun, 12 Nov 2017 08:34:25 +0000 Subject: [PATCH] Introduce syslog service for Casper. syslog in libc secretly reconnects to the daemon. Another issue is that we don't have any information from openlog(3) if we succeeded to open log or not so we don't know if we are ready to enter cabability mode. Because all of that we decided we need a syslog service for Caspser. Reviewed by: bapt@ Differential Revision: https://reviews.freebsd.org/D12824 --- lib/libcasper/services/Makefile | 1 + lib/libcasper/services/cap_syslog/Makefile | 24 +++ .../services/cap_syslog/cap_syslog.c | 199 ++++++++++++++++++ .../services/cap_syslog/cap_syslog.h | 54 +++++ share/mk/bsd.libnames.mk | 1 + share/mk/src.libnames.mk | 3 + 6 files changed, 282 insertions(+) create mode 100644 lib/libcasper/services/cap_syslog/Makefile create mode 100644 lib/libcasper/services/cap_syslog/cap_syslog.c create mode 100644 lib/libcasper/services/cap_syslog/cap_syslog.h diff --git a/lib/libcasper/services/Makefile b/lib/libcasper/services/Makefile index d14f34d5a3c3..c735063d4050 100644 --- a/lib/libcasper/services/Makefile +++ b/lib/libcasper/services/Makefile @@ -7,6 +7,7 @@ SUBDIR+= cap_grp SUBDIR+= cap_pwd SUBDIR+= cap_random SUBDIR+= cap_sysctl +SUBDIR+= cap_syslog SUBDIR.${MK_TESTS}+= tests diff --git a/lib/libcasper/services/cap_syslog/Makefile b/lib/libcasper/services/cap_syslog/Makefile new file mode 100644 index 000000000000..70eb65734ad8 --- /dev/null +++ b/lib/libcasper/services/cap_syslog/Makefile @@ -0,0 +1,24 @@ +# $FreeBSD$ + +SHLIBDIR?= /lib/casper + +.include + +PACKAGE=libcasper + +SHLIB_MAJOR= 0 +INCSDIR?= ${INCLUDEDIR}/casper + +.if ${MK_CASPER} != "no" +SHLIB= cap_syslog + +SRCS= cap_syslog.c +.endif + +INCS= cap_syslog.h + +LIBADD= nv + +CFLAGS+=-I${.CURDIR} + +.include diff --git a/lib/libcasper/services/cap_syslog/cap_syslog.c b/lib/libcasper/services/cap_syslog/cap_syslog.c new file mode 100644 index 000000000000..3bd9ce5b0c35 --- /dev/null +++ b/lib/libcasper/services/cap_syslog/cap_syslog.c @@ -0,0 +1,199 @@ +/*- + * Copyright (c) 2017 Mariusz Zaborski + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "cap_syslog.h" + +#define CAP_SYSLOG_LIMIT 2048 + +void +cap_syslog(cap_channel_t *chan, int pri, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + cap_vsyslog(chan, pri, fmt, ap); + va_end(ap); +} + +void +cap_vsyslog(cap_channel_t *chan, int priority, const char *fmt, va_list ap) +{ + nvlist_t *nvl; + char message[CAP_SYSLOG_LIMIT]; + + (void)vsnprintf(message, sizeof(message), fmt, ap); + + nvl = nvlist_create(0); + nvlist_add_string(nvl, "cmd", "vsyslog"); + nvlist_add_number(nvl, "priority", priority); + nvlist_add_string(nvl, "message", message); + nvl = cap_xfer_nvlist(chan, nvl, 0); + if (nvl == NULL) { + return; + } + nvlist_destroy(nvl); +} + +void +cap_openlog(cap_channel_t *chan, const char *ident, int logopt, int facility) +{ + nvlist_t *nvl; + + nvl = nvlist_create(0); + nvlist_add_string(nvl, "cmd", "openlog"); + if (ident != NULL) { + nvlist_add_string(nvl, "ident", ident); + } + nvlist_add_number(nvl, "logopt", logopt); + nvlist_add_number(nvl, "facility", facility); + nvl = cap_xfer_nvlist(chan, nvl, 0); + if (nvl == NULL) { + return; + } + nvlist_destroy(nvl); +} + +void +cap_closelog(cap_channel_t *chan) +{ + nvlist_t *nvl; + + nvl = nvlist_create(0); + nvlist_add_string(nvl, "cmd", "closelog"); + nvl = cap_xfer_nvlist(chan, nvl, 0); + if (nvl == NULL) { + return; + } + nvlist_destroy(nvl); +} + +int +cap_setlogmask(cap_channel_t *chan, int maskpri) +{ + nvlist_t *nvl; + int omask; + + nvl = nvlist_create(0); + nvlist_add_string(nvl, "cmd", "setlogmask"); + nvlist_add_number(nvl, "maskpri", maskpri); + nvl = cap_xfer_nvlist(chan, nvl, 0); + omask = nvlist_get_number(nvl, "omask"); + + nvlist_destroy(nvl); + + return (omask); +} + +/* + * Service functions. + */ + +static char *LogTag; + +static void +slog_vsyslog(const nvlist_t *limits __unused, const nvlist_t *nvlin, + nvlist_t *nvlout __unused) +{ + + syslog(nvlist_get_number(nvlin, "priority"), "%s", + nvlist_get_string(nvlin, "message")); +} + +static void +slog_openlog(const nvlist_t *limits __unused, const nvlist_t *nvlin, + nvlist_t *nvlout __unused) +{ + const char *ident; + + ident = dnvlist_get_string(nvlin, "ident", NULL); + if (ident != NULL) { + free(LogTag); + LogTag = strdup(ident); + } + + openlog(LogTag, nvlist_get_number(nvlin, "logopt"), + nvlist_get_number(nvlin, "facility")); +} + +static void +slog_closelog(const nvlist_t *limits __unused, const nvlist_t *nvlin __unused, + nvlist_t *nvlout __unused) +{ + + closelog(); + + free(LogTag); + LogTag = NULL; +} + +static void +slog_setlogmask(const nvlist_t *limits __unused, const nvlist_t *nvlin, + nvlist_t *nvlout) +{ + int omask; + + omask = setlogmask(nvlist_get_number(nvlin, "maskpri")); + nvlist_add_number(nvlout, "omask", omask); +} + +static int +syslog_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin, + nvlist_t *nvlout) +{ + + if (strcmp(cmd, "vsyslog") == 0) { + slog_vsyslog(limits, nvlin, nvlout); + } else if (strcmp(cmd, "openlog") == 0) { + slog_openlog(limits, nvlin, nvlout); + } else if (strcmp(cmd, "closelog") == 0) { + slog_closelog(limits, nvlin, nvlout); + } else if (strcmp(cmd, "setlogmask") == 0) { + slog_setlogmask(limits, nvlin, nvlout); + } else { + return (EINVAL); + } + + return (0); +} + +CREATE_SERVICE("system.syslog", NULL, syslog_command, 0); diff --git a/lib/libcasper/services/cap_syslog/cap_syslog.h b/lib/libcasper/services/cap_syslog/cap_syslog.h new file mode 100644 index 000000000000..55ca4b355e16 --- /dev/null +++ b/lib/libcasper/services/cap_syslog/cap_syslog.h @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 2017 Mariusz Zaborski + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _CAP_SYSLOG_H_ +#define _CAP_SYSLOG_H_ + +#ifdef WITH_CASPER +void cap_syslog(cap_channel_t *chan, int pri, + const char *fmt, ...) __printflike(3, 4); +void cap_vsyslog(cap_channel_t *chan, int priority, const char *fmt, + va_list ap) __printflike(3, 0); + +void cap_openlog(cap_channel_t *chan, const char *ident, int logopt, + int facility); +void cap_closelog(cap_channel_t *chan); + +int cap_setlogmask(cap_channel_t *chan, int maskpri); +#else +#define cap_syslog(chan, pri, ...) syslog(pri, __VA_ARGS__) +#define cap_vsyslog(chan, pri, fmt, ap) vsyslog(pri, fmt, ap) + +#define cap_openlog(chan, ident, logopt, facility) \ + openlog(ident, logopt, facility) +#define cap_closelog(chan) closelog() + +#define cap_setlogmask(chan, maskpri) setlogmask(maskpri) +#endif /* !WITH_CASPER */ + +#endif /* !_CAP_SYSLOG_H_ */ diff --git a/share/mk/bsd.libnames.mk b/share/mk/bsd.libnames.mk index b95ea0937697..cf2cdf0baaee 100644 --- a/share/mk/bsd.libnames.mk +++ b/share/mk/bsd.libnames.mk @@ -38,6 +38,7 @@ LIBCAP_GRP?= ${LIBDESTDIR}${LIBDIR_BASE}/libcap_grp.a LIBCAP_PWD?= ${LIBDESTDIR}${LIBDIR_BASE}/libcap_pwd.a LIBCAP_RANDOM?= ${LIBDESTDIR}${LIBDIR_BASE}/libcap_random.a LIBCAP_SYSCTL?= ${LIBDESTDIR}${LIBDIR_BASE}/libcap_sysctl.a +LIBCAP_SYSLOG?= ${LIBDESTDIR}${LIBDIR_BASE}/libcap_syslog.a LIBCASPER?= ${LIBDESTDIR}${LIBDIR_BASE}/libcasper.a LIBCOMPAT?= ${LIBDESTDIR}${LIBDIR_BASE}/libcompat.a LIBCOMPILER_RT?=${LIBDESTDIR}${LIBDIR_BASE}/libcompiler_rt.a diff --git a/share/mk/src.libnames.mk b/share/mk/src.libnames.mk index 703adb03cc56..fa3107380db3 100644 --- a/share/mk/src.libnames.mk +++ b/share/mk/src.libnames.mk @@ -78,6 +78,7 @@ _LIBRARIES= \ cap_pwd \ cap_random \ cap_sysctl \ + cap_syslog \ com_err \ compiler_rt \ crypt \ @@ -239,6 +240,7 @@ _DP_cap_grp= nv _DP_cap_pwd= nv _DP_cap_random= nv _DP_cap_sysctl= nv +_DP_cap_syslog= nv _DP_pjdlog= util _DP_opie= md _DP_usb= pthread @@ -530,6 +532,7 @@ LIBCAP_GRPDIR= ${OBJTOP}/lib/libcasper/services/cap_grp LIBCAP_PWDDIR= ${OBJTOP}/lib/libcasper/services/cap_pwd LIBCAP_RANDOMDIR= ${OBJTOP}/lib/libcasper/services/cap_random LIBCAP_SYSCTLDIR= ${OBJTOP}/lib/libcasper/services/cap_sysctl +LIBCAP_SYSLOGDIR= ${OBJTOP}/lib/libcasper/services/cap_syslog LIBBSDXMLDIR= ${OBJTOP}/lib/libexpat LIBKVMDIR= ${OBJTOP}/lib/libkvm LIBPTHREADDIR= ${OBJTOP}/lib/libthr