Imported Upstream version 1.8.6p8
[debian/sudo] / plugins / sudoers / linux_audit.c
1 /*
2  * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20 #include <stdio.h>
21 #ifdef STDC_HEADERS
22 # include <stdlib.h>
23 # include <stddef.h>
24 #else
25 # ifdef HAVE_STDLIB_H
26 #  include <stdlib.h>
27 # endif
28 #endif /* STDC_HEADERS */
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <libaudit.h>
33
34 #include "missing.h"
35 #include "error.h"
36 #include "alloc.h"
37 #include "gettext.h"
38 #include "sudo_debug.h"
39 #include "linux_audit.h"
40
41 /*
42  * Open audit connection if possible.
43  * Returns audit fd on success and -1 on failure.
44  */
45 int
46 static linux_audit_open(void)
47 {
48     static int au_fd = -1;
49     debug_decl(linux_audit_open, SUDO_DEBUG_AUDIT)
50
51     if (au_fd != -1)
52         debug_return_int(au_fd);
53     au_fd = audit_open();
54     if (au_fd == -1) {
55         /* Kernel may not have audit support. */
56         if (errno != EINVAL && errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
57             error(1, _("unable to open audit system"));
58     } else {
59         (void)fcntl(au_fd, F_SETFD, FD_CLOEXEC);
60     }
61     debug_return_int(au_fd);
62 }
63
64 int
65 linux_audit_command(char *argv[], int result)
66 {
67     int au_fd, rc;
68     char *command, *cp, **av;
69     size_t size, n;
70     debug_decl(linux_audit_command, SUDO_DEBUG_AUDIT)
71
72     if ((au_fd = linux_audit_open()) == -1)
73         debug_return_int(-1);
74
75     /* Convert argv to a flat string. */
76     for (size = 0, av = argv; *av != NULL; av++)
77         size += strlen(*av) + 1;
78     command = cp = emalloc(size);
79     for (av = argv; *av != NULL; av++) {
80         n = strlcpy(cp, *av, size - (cp - command));
81         if (n >= size - (cp - command)) {
82             errorx(1, _("internal error, %s overflow"),
83                 "linux_audit_command()");
84         }
85         cp += n;
86         *cp++ = ' ';
87     }
88     *--cp = '\0';
89
90     /* Log command, ignoring ECONNREFUSED on error. */
91     rc = audit_log_user_command(au_fd, AUDIT_USER_CMD, command, NULL, result);
92     if (rc <= 0 && errno != ECONNREFUSED)
93         warning(_("unable to send audit message"));
94
95     efree(command);
96
97     debug_return_int(rc);
98 }