8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libbsm / common / audit_rexecd.c
blob38246e232cd824513567e2ba08dd315be2c138bf
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 <stdio.h>
27 #include <unistd.h>
28 #include <sys/fcntl.h>
29 #include <bsm/audit.h>
30 #include <bsm/audit_record.h>
31 #include <bsm/audit_uevents.h>
32 #include <bsm/libbsm.h>
33 #include <bsm/audit_private.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <syslog.h>
37 #include <pwd.h>
38 #include <netinet/in.h>
39 #include <tsol/label.h>
40 #include <locale.h>
41 #include "generic.h"
43 #ifdef C2_DEBUG
44 #define dprintf(x) { (void) printf x; }
45 #else
46 #define dprintf(x)
47 #endif
49 static au_event_t event;
50 static int audit_rexecd_status = 0;
52 static int
53 selected(uid, user, event, sf)
54 uid_t uid;
55 char *user;
56 au_event_t event;
57 int sf;
59 int sorf;
60 struct au_mask mask;
62 mask.am_success = mask.am_failure = 0;
63 if (uid > MAXEPHUID) {
64 /* get non-attrib flags */
65 (void) auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask));
66 } else {
67 (void) au_user_mask(user, &mask);
70 if (sf == 0) {
71 sorf = AU_PRS_SUCCESS;
72 } else if (sf == -1) {
73 sorf = AU_PRS_FAILURE;
74 } else {
75 sorf = AU_PRS_BOTH;
78 return (au_preselect(event, &mask, sorf, AU_PRS_REREAD));
81 void
82 audit_rexecd_setup()
84 dprintf(("audit_rexecd_setup()\n"));
86 event = AUE_rexecd;
90 static void
91 audit_rexecd_session_setup(char *name, char *mach, uid_t uid)
93 int rc;
94 au_mask_t mask;
95 struct auditinfo_addr info;
96 uint32_t addr[4], type;
98 info.ai_auid = uid;
99 info.ai_asid = getpid();
101 mask.am_success = 0;
102 mask.am_failure = 0;
104 (void) au_user_mask(name, &mask);
106 info.ai_mask.am_success = mask.am_success;
107 info.ai_mask.am_failure = mask.am_failure;
109 rc = aug_get_machine(mach, addr, &type);
110 if (rc < 0) {
111 perror("get address");
113 info.ai_termid.at_port = aug_get_port();
114 info.ai_termid.at_type = type;
115 info.ai_termid.at_addr[0] = addr[0];
116 info.ai_termid.at_addr[1] = addr[1];
117 info.ai_termid.at_addr[2] = addr[2];
118 info.ai_termid.at_addr[3] = addr[3];
120 rc = setaudit_addr(&info, sizeof (info));
121 if (rc < 0) {
122 perror("setaudit");
126 void
127 audit_rexecd_fail(msg, hostname, user, cmdbuf)
128 char *msg; /* message containing failure information */
129 char *hostname; /* hostname of machine requesting service */
130 char *user; /* username of user requesting service */
131 char *cmdbuf; /* command line to be executed locally */
133 int rd; /* audit record descriptor */
134 char buf[256]; /* temporary buffer */
135 char *tbuf; /* temporary buffer */
136 int tlen;
137 const char *gtxt;
138 uid_t uid;
139 gid_t gid;
140 pid_t pid;
141 au_tid_addr_t tid;
142 struct passwd *pwd;
143 uint32_t addr[4], type;
144 int rc;
146 dprintf(("audit_rexecd_fail()\n"));
149 * check if audit_rexecd_fail() or audit_rexecd_success()
150 * have been called already.
152 if (audit_rexecd_status == 1) {
153 return;
156 if (cannot_audit(0)) {
157 return;
161 * set status to prevent multiple calls
162 * to audit_rexecd_fail() and audit_rexecd_success()
164 audit_rexecd_status = 1;
166 pwd = getpwnam(user);
167 if (pwd == NULL) {
168 uid = (uid_t)-1;
169 gid = (gid_t)-1;
170 } else {
171 uid = pwd->pw_uid;
172 gid = pwd->pw_gid;
175 /* determine if we're preselected */
176 if (!selected(uid, user, event, -1))
177 return;
179 pid = getpid();
180 rc = aug_get_machine(hostname, addr, &type);
181 if (rc < 0) {
182 perror("get address");
185 tid.at_port = aug_get_port();
186 tid.at_addr[0] = addr[0];
187 tid.at_addr[1] = addr[1];
188 tid.at_addr[2] = addr[2];
189 tid.at_addr[3] = addr[3];
190 tid.at_type = type;
192 rd = au_open();
194 /* add subject token */
195 (void) au_write(rd,
196 au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid, &tid));
197 if (is_system_labeled())
198 (void) au_write(rd, au_to_mylabel());
200 /* add reason for failure */
201 (void) au_write(rd, au_to_text(msg));
203 /* add hostname of machine requesting service */
204 (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
205 "Remote execution requested by: %s"), hostname);
206 (void) au_write(rd, au_to_text(buf));
208 /* add username of user requesting service */
209 (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
210 "Username: %s"), user);
211 (void) au_write(rd, au_to_text(buf));
213 /* add command line to be executed locally */
214 gtxt = dgettext(bsm_dom, "Command line: %s");
215 tlen = strlen(gtxt) + strlen(cmdbuf) + 1;
216 if ((tbuf = malloc(tlen)) == NULL) {
217 (void) au_close(rd, 0, 0);
218 return;
220 (void) snprintf(tbuf, tlen, gtxt, cmdbuf);
221 (void) au_write(rd, au_to_text(tbuf));
222 (void) free(tbuf);
224 /* add return token */
225 #ifdef _LP64
226 (void) au_write(rd, au_to_return64(-1, (int64_t)0));
227 #else
228 (void) au_write(rd, au_to_return32(-1, (int32_t)0));
229 #endif
231 /* write audit record */
232 if (au_close(rd, 1, event) < 0) {
233 (void) au_close(rd, 0, 0);
234 return;
238 void
239 audit_rexecd_success(hostname, user, cmdbuf)
240 char *hostname; /* hostname of machine requesting service */
241 char *user; /* username of user requesting service */
242 char *cmdbuf; /* command line to be executed locally */
244 int rd; /* audit record descriptor */
245 char buf[256]; /* temporary buffer */
246 char *tbuf; /* temporary buffer */
247 int tlen;
248 const char *gtxt;
249 uid_t uid;
250 gid_t gid;
251 pid_t pid;
252 au_tid_addr_t tid;
253 struct passwd *pwd;
254 uint32_t addr[4], type;
255 int rc;
257 dprintf(("audit_rexecd_success()\n"));
260 * check if audit_rexecd_fail() or audit_rexecd_success()
261 * have been called already.
263 if (audit_rexecd_status == 1) {
264 return;
267 if (cannot_audit(0)) {
268 return;
272 * set status to prevent multiple calls
273 * to audit_rexecd_fail() and audit_rexecd_success()
275 audit_rexecd_status = 1;
277 pwd = getpwnam(user);
278 if (pwd == NULL) {
279 uid = (uid_t)-1;
280 gid = (gid_t)-1;
281 } else {
282 uid = pwd->pw_uid;
283 gid = pwd->pw_gid;
286 /* determine if we're preselected */
287 if (!selected(uid, user, event, 0))
288 goto rexecd_audit_session;
290 pid = getpid();
291 rc = aug_get_machine(hostname, addr, &type);
292 if (rc < 0) {
293 perror("get address");
296 tid.at_port = aug_get_port();
297 tid.at_addr[0] = addr[0];
298 tid.at_addr[1] = addr[1];
299 tid.at_addr[2] = addr[2];
300 tid.at_addr[3] = addr[3];
301 tid.at_type = type;
303 rd = au_open();
305 /* add subject token */
306 (void) au_write(rd,
307 au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid, &tid));
308 if (is_system_labeled())
309 (void) au_write(rd, au_to_mylabel());
311 /* add hostname of machine requesting service */
312 (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
313 "Remote execution requested by: %s"), hostname);
314 (void) au_write(rd, au_to_text(buf));
316 /* add username at machine requesting service */
317 (void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
318 "Username: %s"), user);
319 (void) au_write(rd, au_to_text(buf));
321 /* add command line to be executed locally */
322 gtxt = dgettext(bsm_dom, "Command line: %s");
323 tlen = strlen(gtxt) + strlen(cmdbuf) + 1;
324 if ((tbuf = malloc(tlen)) == NULL) {
325 (void) au_close(rd, 0, 0);
326 } else {
327 (void) snprintf(tbuf, tlen, gtxt, cmdbuf);
328 (void) au_write(rd, au_to_text(tbuf));
329 (void) free(tbuf);
331 /* add return token */
332 #ifdef _LP64
333 (void) au_write(rd, au_to_return64(0, (int64_t)0));
334 #else
335 (void) au_write(rd, au_to_return32(0, (int32_t)0));
336 #endif
338 /* write audit record */
339 if (au_close(rd, 1, event) < 0) {
340 (void) au_close(rd, 0, 0);
344 rexecd_audit_session:
345 audit_rexecd_session_setup(user, hostname, uid);