8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libbsm / common / audit_rexd.c
blob17b6ba7c6ea64e4b46e0f5f061ccde4b4527a43d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <sys/fcntl.h>
30 #include <bsm/audit.h>
31 #include <bsm/audit_record.h>
32 #include <bsm/audit_uevents.h>
33 #include <bsm/libbsm.h>
34 #include <bsm/audit_private.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <syslog.h>
38 #include <pwd.h>
39 #include <netinet/in.h>
40 #include <tsol/label.h>
41 #include <locale.h>
42 #include "generic.h"
44 #ifdef C2_DEBUG
45 #define dprintf(x) { (void) printf x; }
46 #else
47 #define dprintf(x)
48 #endif
50 #define UNKNOWN_CMD "???"
52 static au_event_t event;
53 static int audit_rexd_status = 0;
55 static char *
56 build_cmd(char **cmd)
58 int i, l;
59 char *r;
61 if (cmd == NULL)
62 return (NULL);
63 /* count the total length of command line */
64 for (i = 0, l = 0; cmd[i] != NULL; i++)
65 l += strlen(cmd[i]) + 1;
67 if (l == 0)
68 return (NULL);
69 r = malloc(l);
70 if (r != NULL) {
71 for (i = 0; cmd[i] != NULL; i++) {
72 (void) strcat(r, cmd[i]);
73 if (cmd[i + 1] != NULL)
74 (void) strcat(r, " ");
77 return (r);
80 static int
81 selected(uid, user, event, sf)
82 uid_t uid;
83 char *user;
84 au_event_t event;
85 int sf;
87 int sorf;
88 struct au_mask mask;
90 mask.am_success = mask.am_failure = 0;
91 if (uid > MAXEPHUID) {
92 /* get non-attrib flags */
93 (void) auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask));
94 } else {
95 (void) au_user_mask(user, &mask);
98 if (sf == 0) {
99 sorf = AU_PRS_SUCCESS;
100 } else if (sf == -1) {
101 sorf = AU_PRS_FAILURE;
102 } else {
103 sorf = AU_PRS_BOTH;
106 return (au_preselect(event, &mask, sorf, AU_PRS_REREAD));
109 void
110 audit_rexd_setup()
112 dprintf(("audit_rexd_setup()\n"));
114 event = AUE_rexd;
117 /* ARGSUSED */
118 static void
119 audit_rexd_session_setup(char *name, char *mach, uid_t uid)
121 int rc;
122 au_mask_t mask;
123 struct auditinfo_addr info;
125 if (getaudit_addr(&info, sizeof (info)) < 0) {
126 perror("getaudit_addr");
127 exit(1);
130 info.ai_auid = uid;
131 info.ai_asid = getpid();
133 mask.am_success = 0;
134 mask.am_failure = 0;
136 (void) au_user_mask(name, &mask);
138 info.ai_mask.am_success = mask.am_success;
139 info.ai_mask.am_failure = mask.am_failure;
141 rc = setaudit_addr(&info, sizeof (info));
142 if (rc < 0) {
143 perror("setaudit_addr");
147 void
148 audit_rexd_fail(msg, hostname, user, uid, gid, shell, cmd)
149 char *msg; /* message containing failure information */
150 char *hostname; /* hostname of machine requesting service */
151 char *user; /* username of user requesting service */
152 uid_t uid; /* user id of user requesting service */
153 gid_t gid; /* group of user requesting service */
154 char *shell; /* login shell of user requesting service */
155 char **cmd; /* argv to be executed locally */
157 int rd; /* audit record descriptor */
158 char buf[256]; /* temporary buffer */
159 char *tbuf; /* temporary buffer */
160 int tlen;
161 const char *gtxt; /* gettext return value */
162 pid_t pid;
163 char *cmdbuf;
164 char *audit_cmd[2] = {NULL, NULL};
165 int dont_free = 0;
166 struct auditinfo_addr info;
168 dprintf(("audit_rexd_fail()\n"));
171 * check if audit_rexd_fail() or audit_rexd_success()
172 * have been called already.
174 if (audit_rexd_status == 1) {
175 return;
178 if (cannot_audit(0)) {
179 return;
183 * set status to prevent multiple calls
184 * to audit_rexd_fail() and audit_rexd_success()
186 audit_rexd_status = 1;
188 /* determine if we're preselected */
189 if (!selected(uid, user, event, -1))
190 return;
192 pid = getpid();
194 if (getaudit_addr(&info, sizeof (info)) < 0) {
195 perror("getaudit_addr");
196 exit(1);
199 rd = au_open();
201 /* add subject token */
202 (void) au_write(rd,
203 au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid,
204 &info.ai_termid));
205 if (is_system_labeled())
206 (void) au_write(rd, au_to_mylabel());
208 /* add reason for failure */
209 (void) au_write(rd, au_to_text(msg));
211 /* add hostname of machine requesting service */
212 (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
213 "Remote execution requested by: %s"), hostname);
214 (void) au_write(rd, au_to_text(buf));
216 /* add username of user requesting service */
217 if (user == NULL)
218 user = "???";
219 (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
220 "Username: %s"), user);
221 (void) au_write(rd, au_to_text(buf));
223 (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
224 "User id: %d"), uid);
225 (void) au_write(rd, au_to_text(buf));
227 if (cmd == NULL) {
228 audit_cmd[0] = shell;
229 cmd = audit_cmd;
232 cmdbuf = build_cmd(cmd);
233 if (cmdbuf == NULL) {
234 cmdbuf = UNKNOWN_CMD;
235 dont_free = 1;
238 gtxt = dgettext(bsm_dom, "Command line: %s");
239 /* over estimate of size of buffer needed (%s is replaced) */
240 tlen = strlen(cmdbuf) + strlen(gtxt) + 1;
242 if ((tbuf = malloc(tlen)) == NULL) {
243 (void) au_close(rd, 0, 0);
244 return;
246 (void) snprintf(tbuf, tlen, gtxt, cmdbuf);
247 (void) au_write(rd, au_to_text(tbuf));
248 (void) free(tbuf);
249 if (!dont_free)
250 (void) free(cmdbuf);
252 /* add return token */
253 #ifdef _LP64
254 (void) au_write(rd, au_to_return64(-1, (int64_t)0));
255 #else
256 (void) au_write(rd, au_to_return32(-1, (int32_t)0));
257 #endif
259 /* write audit record */
260 if (au_close(rd, 1, event) < 0) {
261 (void) au_close(rd, 0, 0);
262 return;
266 void
267 audit_rexd_success(hostname, user, uid, gid, shell, cmd)
268 char *hostname; /* hostname of machine requesting service */
269 char *user; /* username of user requesting service, may be NULL */
270 uid_t uid; /* user id of user requesting service */
271 gid_t gid; /* group of user requesting service */
272 char *shell; /* login shell of user requesting service */
273 char **cmd; /* argv to be executed locally, may be NULL */
275 int rd; /* audit record descriptor */
276 char buf[256]; /* temporary buffer */
277 char *tbuf; /* temporary buffer */
278 int tlen;
279 const char *gtxt;
280 pid_t pid;
281 char *cmdbuf;
282 char *audit_cmd[2] = {NULL, NULL};
283 int dont_free = 0;
284 struct auditinfo_addr info;
285 char *empty = "";
287 dprintf(("audit_rexd_success()\n"));
290 * check if audit_rexd_fail() or audit_rexd_success()
291 * have been called already.
293 if (audit_rexd_status == 1) {
294 return;
297 if (cannot_audit(0)) {
298 return;
301 /* a little bullet proofing... */
303 if (hostname == NULL)
304 hostname = empty;
305 if (shell == NULL)
306 shell = empty;
309 * set status to prevent multiple calls
310 * to audit_rexd_fail() and audit_rexd_success()
312 audit_rexd_status = 1;
314 /* determine if we're preselected */
315 if (!selected(uid, user, event, 0))
316 goto rexd_audit_session;
318 pid = getpid();
320 if (getaudit_addr(&info, sizeof (info)) < 0) {
321 perror("getaudit_addr");
322 exit(1);
325 rd = au_open();
327 /* add subject token */
328 (void) au_write(rd,
329 au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid,
330 &info.ai_termid));
331 if (is_system_labeled())
332 (void) au_write(rd, au_to_mylabel());
334 /* add hostname of machine requesting service */
336 (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
337 "Remote execution requested by: %s"), hostname);
338 (void) au_write(rd, au_to_text(buf));
340 /* add username at machine requesting service */
341 (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
342 "Username: %s"), user);
343 (void) au_write(rd, au_to_text(buf));
345 if (cmd == NULL) {
346 audit_cmd[0] = shell;
347 cmd = audit_cmd;
350 cmdbuf = build_cmd(cmd);
351 if (cmdbuf == NULL) {
352 cmdbuf = UNKNOWN_CMD;
353 dont_free = 1;
356 gtxt = dgettext(bsm_dom, "Command line: %s");
357 tlen = strlen(cmdbuf) + strlen(gtxt) + 1;
359 if ((tbuf = malloc(tlen)) == NULL) {
360 (void) au_close(rd, 0, 0);
361 goto rexd_audit_session;
364 (void) snprintf(tbuf, tlen, gtxt, cmdbuf);
365 (void) au_write(rd, au_to_text(tbuf));
366 (void) free(tbuf);
367 if (!dont_free)
368 (void) free(cmdbuf);
370 /* add return token */
371 #ifdef _LP64
372 (void) au_write(rd, au_to_return64(0, (int64_t)0));
373 #else
374 (void) au_write(rd, au_to_return32(0, (int32_t)0));
375 #endif
377 /* write audit record */
378 if (au_close(rd, 1, event) < 0) {
379 (void) au_close(rd, 0, 0);
382 rexd_audit_session:
383 audit_rexd_session_setup(user, hostname, uid);