]> granicus.if.org Git - sudo/commitdiff
Add Linux audit support.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Tue, 15 Jun 2010 10:50:37 +0000 (06:50 -0400)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Tue, 15 Jun 2010 10:50:37 +0000 (06:50 -0400)
--HG--
branch : 1.7

Makefile.in
linux_audit.c [new file with mode: 0644]
linux_audit.h [new file with mode: 0644]

index f86c6508f2c6da4f9bbd3930d78a675987ba3202..e948b375dfdfe10dd02c63269d3550ee04146d5e 100644 (file)
@@ -106,13 +106,14 @@ SRCS = aix.c alias.c alloc.c audit.c boottime.c bsm_audit.c check.c \
        closefrom.c def_data.c defaults.c env.c error.c exec.c exec_pty.c \
        fileops.c find_path.c fnmatch.c get_pty.c getcwd.c getprogname.c \
        getspwuid.c gettime.c glob.c goodpath.c gram.c gram.y interfaces.c \
-       iolog.c isblank.c lbuf.c ldap.c list.c logging.c match.c mksiglist.c \
-       mkstemp.c memrchr.c nanosleep.c parse.c parse_args.c pwutil.c \
-       set_perms.c setsid.c sigaction.c snprintf.c strcasecmp.c strerror.c \
-       strlcat.c strlcpy.c strsignal.c sudo.c sudo_noexec.c sudo_edit.c \
-       sudo_nss.c term.c testsudoers.c tgetpass.c toke.c toke.l tsgetgrpw.c \
-       utimes.c vasgroups.c visudo.c zero_bytes.c redblack.c selinux.c sesh.c \
-       sudoreplay.c getdate.c getdate.y getline.c timestr.c $(AUTH_SRCS)
+       iolog.c isblank.c lbuf.c ldap.c linux_audit.c list.c logging.c match.c \
+       mksiglist.c mkstemp.c memrchr.c nanosleep.c parse.c parse_args.c \
+       pwutil.c set_perms.c setsid.c sigaction.c snprintf.c strcasecmp.c \
+       strerror.c strlcat.c strlcpy.c strsignal.c sudo.c sudo_noexec.c \
+       sudo_edit.c sudo_nss.c term.c testsudoers.c tgetpass.c toke.c toke.l \
+       tsgetgrpw.c utimes.c vasgroups.c visudo.c zero_bytes.c redblack.c \
+       selinux.c sesh.c sudoreplay.c getdate.c getdate.y getline.c \
+       timestr.c $(AUTH_SRCS)
 
 AUTH_SRCS = auth/afs.c auth/aix_auth.c auth/bsdauth.c auth/dce.c auth/fwtk.c \
            auth/kerb4.c auth/kerb5.c auth/pam.c auth/passwd.c auth/rfc1938.c \
@@ -121,9 +122,9 @@ AUTH_SRCS = auth/afs.c auth/aix_auth.c auth/bsdauth.c auth/dce.c auth/fwtk.c \
 
 HDRS = alloc.h bsm_audit.h compat.h def_data.h defaults.h error.h ins_2001.h \
        ins_classic.h ins_csops.h ins_goons.h insults.h interfaces.h lbuf.h \
-       list.h logging.h missing.h mksiglist.h nonunix.h redblack.h parse.h \
-       sudo.h sudo_nss.h gram.h auth/sudo_auth.h emul/charclass.h \
-       emul/fnmatch.h emul/glob.h emul/timespec.h emul/utime.h
+       linux_audit.h list.h logging.h missing.h mksiglist.h nonunix.h \
+       redblack.h parse.h sudo.h sudo_nss.h gram.h auth/sudo_auth.h \
+       emul/charclass.h emul/fnmatch.h emul/glob.h emul/timespec.h emul/utime.h
 
 AUTH_OBJS = sudo_auth.o @AUTH_OBJS@
 
@@ -253,11 +254,11 @@ alias.o: $(srcdir)/alias.c $(SUDODEP) $(srcdir)/parse.h $(srcdir)/list.h $(srcdi
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/alias.c
 alloc.o: $(srcdir)/alloc.c $(SUDODEP)
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/alloc.c
-audit.o: $(srcdir)/audit.c $(SUDODEP)
+audit.o: $(srcdir)/audit.c $(SUDODEP) $(srcdir)/bsm_audit.h $(srcdir)/linux_audit.h
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/audit.c
 boottime.o: $(srcdir)/boottime.c config.h
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/boottime.c
-bsm_audit.o: $(srcdir)/bsm_audit.c $(SUDODEP) bsm_audit.h
+bsm_audit.o: $(srcdir)/bsm_audit.c $(SUDODEP) $(srcdir)/bsm_audit.h
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/bsm_audit.c
 check.o: $(srcdir)/check.c $(SUDODEP)
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/check.c
@@ -309,6 +310,8 @@ lbuf.o: $(srcdir)/lbuf.c $(SUDODEP)
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/lbuf.c
 ldap.o: $(srcdir)/ldap.c $(SUDODEP) $(srcdir)/parse.h $(srcdir)/list.h
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/ldap.c
+linux_audit.o: $(srcdir)/linux_audit.c $(SUDODEP) $(srcdir)/linux_audit.h
+       $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/linux_audit.c
 list.o: $(srcdir)/list.c $(SUDODEP)
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/list.c
 logging.o: $(srcdir)/logging.c $(SUDODEP)
diff --git a/linux_audit.c b/linux_audit.c
new file mode 100644 (file)
index 0000000..21d4012
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2010 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 <sys/types.h>
+#include <stdio.h>
+#ifdef STDC_HEADERS
+# include <stdlib.h>
+# include <stddef.h>
+#else
+# ifdef HAVE_STDLIB_H
+#  include <stdlib.h>
+# endif
+#endif /* STDC_HEADERS */
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <libaudit.h>
+
+#include "compat.h"
+#include "error.h"
+#include "alloc.h"
+#include "missing.h"
+#include "linux_audit.h"
+
+/*
+ * Open audit connection if possible.
+ * Returns audit fd on success and -1 on failure.
+ */
+static int
+linux_audit_open(void)
+{
+    static int au_fd = -1;
+
+    if (au_fd != -1)
+       return au_fd;
+    au_fd = audit_open();
+    if (au_fd == -1) {
+       /* Kernel may not have audit support. */
+       if (errno != EINVAL && errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
+           error(1, "unable to open audit system");
+    } else {
+       (void)fcntl(au_fd, F_SETFD, FD_CLOEXEC);
+    }
+    return au_fd;
+}
+
+int
+linux_audit_command(char *argv[], int result)
+{
+    int au_fd, rc;
+    char *command, *cp, **av;
+    size_t size, n;
+
+    if ((au_fd = linux_audit_open()) == -1)
+       return -1;
+
+    /* Convert argv to a flat string. */
+    for (size = 0, av = argv; *av != NULL; av++)
+       size += strlen(*av) + 1;
+    command = cp = emalloc(size);
+    for (av = argv; *av != NULL; av++) {
+       n = strlcpy(cp, *av, size - (cp - command));
+       if (n >= size - (cp - command))
+           errorx(1, "internal error, linux_audit_command() overflow");
+       cp += n;
+       *cp++ = ' ';
+    }
+    *--cp = '\0';
+
+    /* Log command, ignoring EPERM on error. */
+    rc = audit_log_user_command(au_fd, AUDIT_USER_CMD, command, NULL, result);
+    if (rc <= 0)
+       warning("unable to send audit message");
+
+    efree(command);
+
+    return -1;
+}
+
+#ifdef HAVE_SELINUX
+int
+linux_audit_role_change(const char *old_context,
+    const char *new_context, const char *ttyn)
+{
+    int au_fd, rc;
+    char *message;
+
+    if ((au_fd = linux_audit_open()) == -1)
+       return -1;
+
+    /* audit role change using the same format as newrole(1) */
+    easprintf(&message, "newrole: old-context=%s new-context=%s",
+       old_context, new_context);
+    rc = audit_log_user_message(au_fd, AUDIT_USER_ROLE_CHANGE,
+       message, NULL, NULL, ttyn, 1);
+    if (rc <= 0)
+       warning("unable to send audit message");
+
+    efree(message);
+
+    return rc;
+}
+#endif /* HAVE_SELINUX */
diff --git a/linux_audit.h b/linux_audit.h
new file mode 100644 (file)
index 0000000..f2574f6
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2010 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.
+ */
+
+#ifndef _SUDO_LINUX_AUDIT_H
+#define        _SUDO_LINUX_AUDIT_H
+
+int    linux_audit_command(char *argv[], int result);
+int    linux_audit_role_change(const char *old_context,
+           const char *new_context, const char *ttyn);
+
+#endif /* _SUDO_LINUX_AUDIT_H */