]> granicus.if.org Git - sudo/blob - plugins/sudoers/solaris_audit.c
650595ae694ca0c812945d168043cf37285d7164
[sudo] / plugins / sudoers / solaris_audit.c
1 /*
2  * Copyright (c) 2014, Oracle and/or its affiliates.
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 /*
18  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
19  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
20  */
21
22 #include <config.h>
23
24 #ifdef HAVE_SOLARIS_AUDIT
25
26 #include <sys/types.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <bsm/adt.h>
33 #include <bsm/adt_event.h>
34
35 #include "sudoers.h"
36 #include "solaris_audit.h"
37
38 static adt_session_data_t *ah;          /* audit session handle */
39 static adt_event_data_t *event;         /* event to be generated */
40 static char             cwd[PATH_MAX];
41 static char             cmdpath[PATH_MAX];
42
43 static int
44 adt_sudo_common(int argc, char *argv[])
45 {
46         if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
47                 log_warning(SLOG_NO_STDERR, "adt_start_session");
48                 return -1;
49         }
50         if ((event = adt_alloc_event(ah, ADT_sudo)) == NULL) {
51                 log_warning(SLOG_NO_STDERR, "alloc_event");
52                 (void) adt_end_session(ah);
53                 return -1;
54         }
55         if ((event->adt_sudo.cwdpath = getcwd(cwd, sizeof(cwd))) == NULL) {
56                 log_warning(SLOG_NO_STDERR, _("unable to get current working directory"));
57         }
58
59         /* get the real executable name */
60         if (user_cmnd != NULL) {
61                 if (strlcpy(cmdpath, (const char *)user_cmnd,
62                     sizeof(cmdpath)) >= sizeof(cmdpath)) {
63                         log_warningx(SLOG_NO_STDERR,
64                             _("truncated audit path user_cmnd: %s"),
65                             user_cmnd);
66                 }
67         } else {
68                 if (strlcpy(cmdpath, (const char *)argv[0],
69                     sizeof(cmdpath)) >= sizeof(cmdpath)) {
70                         log_warningx(SLOG_NO_STDERR,
71                             _("truncated audit path argv[0]: %s"),
72                             argv[0]);
73                 }
74         }
75
76         event->adt_sudo.cmdpath = cmdpath;
77         event->adt_sudo.argc = argc - 1;
78         event->adt_sudo.argv = &argv[1];
79         event->adt_sudo.envp = env_get();
80
81         return 0;
82 }
83
84
85 /*
86  * Returns 0 on success or -1 on error.
87  */
88 int
89 solaris_audit_success(int argc, char *argv[])
90 {
91         int rc = -1;
92
93         if (adt_sudo_common(argc, argv) != 0) {
94                 return -1;
95         }
96         if (adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS) != 0) {
97                 log_warning(SLOG_NO_STDERR, "adt_put_event(ADT_SUCCESS)");
98         } else {
99                 rc = 0;
100         }
101         adt_free_event(event);
102         (void) adt_end_session(ah);
103
104         return rc;
105 }
106
107 /*
108  * Returns 0 on success or -1 on error.
109  */
110 int
111 solaris_audit_failure(int argc, char *argv[], char const *const fmt, va_list ap)
112 {
113         int rc = -1;
114
115         if (adt_sudo_common(argc, argv) != 0) {
116                 return -1;
117         }
118         if (vasprintf(&event->adt_sudo.errmsg, fmt, ap) == -1) {
119                 log_warning(SLOG_NO_STDERR,
120                     _("audit_failure message too long"));
121         }
122         if (adt_put_event(event, ADT_FAILURE, ADT_FAIL_VALUE_PROGRAM) != 0) {
123                 log_warning(SLOG_NO_STDERR, "adt_put_event(ADT_FAILURE)");
124         } else {
125                 rc = 0;
126         }
127         free(event->adt_sudo.errmsg);
128         adt_free_event(event);
129         (void) adt_end_session(ah);
130
131         return 0;
132 }
133
134 #endif /* HAVE_SOLARIS_AUDIT */