xref: /titanic_52/usr/src/cmd/smserverd/myaudit.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2001-2002 Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
26*7c478bd9Sstevel@tonic-gate  */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <netdb.h>
31*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
32*7c478bd9Sstevel@tonic-gate #include <pwd.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <unistd.h>
41*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/smedia.h>
43*7c478bd9Sstevel@tonic-gate #include "smserver.h"
44*7c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
45*7c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
46*7c478bd9Sstevel@tonic-gate #include <bsm/audit_uevents.h>
47*7c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate /* Private Functions */
50*7c478bd9Sstevel@tonic-gate static int selected(au_event_t, au_mask_t *, int);
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate static int audit_selected(door_data_t *);
53*7c478bd9Sstevel@tonic-gate static int audit_na_selected(door_data_t *);
54*7c478bd9Sstevel@tonic-gate static int audit_save_namask(door_data_t *door_dp);
55*7c478bd9Sstevel@tonic-gate static int audit_save_policy(door_data_t *door_dp);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /*
58*7c478bd9Sstevel@tonic-gate  * can_audit:
59*7c478bd9Sstevel@tonic-gate  *	Return 1 if audit module is loaded.
60*7c478bd9Sstevel@tonic-gate  *	Return 0 otherwise.
61*7c478bd9Sstevel@tonic-gate  *
62*7c478bd9Sstevel@tonic-gate  */
63*7c478bd9Sstevel@tonic-gate int
64*7c478bd9Sstevel@tonic-gate can_audit(void)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	static int auc = AUC_UNSET;
67*7c478bd9Sstevel@tonic-gate 	int cond = 0;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	if (auditon(A_GETCOND, (caddr_t)&cond, sizeof (cond))) {
70*7c478bd9Sstevel@tonic-gate 		auc = AUC_DISABLED;
71*7c478bd9Sstevel@tonic-gate 	} else {
72*7c478bd9Sstevel@tonic-gate 		auc = cond;
73*7c478bd9Sstevel@tonic-gate 	}
74*7c478bd9Sstevel@tonic-gate 	if (auc == AUC_DISABLED)
75*7c478bd9Sstevel@tonic-gate 		return (0);
76*7c478bd9Sstevel@tonic-gate 	else return (1);
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate static int
80*7c478bd9Sstevel@tonic-gate audit_save_policy(door_data_t *door_dp)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	int policy;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if (auditon(A_GETPOLICY, (caddr_t)&policy, sizeof (policy))) {
85*7c478bd9Sstevel@tonic-gate 		return (-1);
86*7c478bd9Sstevel@tonic-gate 	}
87*7c478bd9Sstevel@tonic-gate 	door_dp->audit_policy = policy;
88*7c478bd9Sstevel@tonic-gate 	return (0);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate /*
92*7c478bd9Sstevel@tonic-gate  * audit_init():
93*7c478bd9Sstevel@tonic-gate  *	Initialize variables.
94*7c478bd9Sstevel@tonic-gate  */
95*7c478bd9Sstevel@tonic-gate void
96*7c478bd9Sstevel@tonic-gate audit_init(door_data_t *door_dp)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	door_dp->audit_auid = -1;
99*7c478bd9Sstevel@tonic-gate 	door_dp->audit_uid = -1;
100*7c478bd9Sstevel@tonic-gate 	door_dp->audit_euid = -1;
101*7c478bd9Sstevel@tonic-gate 	door_dp->audit_gid = -1;
102*7c478bd9Sstevel@tonic-gate 	door_dp->audit_egid = -1;
103*7c478bd9Sstevel@tonic-gate 	door_dp->audit_pid = -1;
104*7c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_port = 0;
105*7c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_type = 0;
106*7c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[0] = 0;
107*7c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[1] = 0;
108*7c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[2] = 0;
109*7c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[3] = 0;
110*7c478bd9Sstevel@tonic-gate 	door_dp->audit_namask.am_success = (int)-1;
111*7c478bd9Sstevel@tonic-gate 	door_dp->audit_namask.am_failure = (int)-1;
112*7c478bd9Sstevel@tonic-gate 	door_dp->audit_event = 0;
113*7c478bd9Sstevel@tonic-gate 	door_dp->audit_sorf = -2;
114*7c478bd9Sstevel@tonic-gate 	door_dp->audit_user = NULL;
115*7c478bd9Sstevel@tonic-gate 	door_dp->audit_text[0] = NULL;
116*7c478bd9Sstevel@tonic-gate 	door_dp->audit_text1[0] = NULL;
117*7c478bd9Sstevel@tonic-gate 	door_dp->audit_na = 0;
118*7c478bd9Sstevel@tonic-gate 	door_dp->audit_asid = -1;
119*7c478bd9Sstevel@tonic-gate 	door_dp->audit_path = NULL;
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate int
123*7c478bd9Sstevel@tonic-gate audit_save_me(door_data_t	*door_dp)
124*7c478bd9Sstevel@tonic-gate {
125*7c478bd9Sstevel@tonic-gate 	door_cred_t	client_cred;
126*7c478bd9Sstevel@tonic-gate 	int		ret_val;
127*7c478bd9Sstevel@tonic-gate 	int		i;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	ret_val = door_cred(&client_cred);
130*7c478bd9Sstevel@tonic-gate 	if (ret_val == -1)
131*7c478bd9Sstevel@tonic-gate 		return (ret_val);
132*7c478bd9Sstevel@tonic-gate 	door_dp->audit_ap.ap_pid = client_cred.dc_pid;
133*7c478bd9Sstevel@tonic-gate 	ret_val = auditon(A_GETPINFO_ADDR, (caddr_t)&door_dp->audit_ap,
134*7c478bd9Sstevel@tonic-gate 		sizeof (door_dp->audit_ap));
135*7c478bd9Sstevel@tonic-gate 	if (ret_val == -1)
136*7c478bd9Sstevel@tonic-gate 		return (ret_val);
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	door_dp->audit_auid = door_dp->audit_ap.ap_auid;
139*7c478bd9Sstevel@tonic-gate 	door_dp->audit_euid = client_cred.dc_euid;
140*7c478bd9Sstevel@tonic-gate 	door_dp->audit_egid = client_cred.dc_egid;
141*7c478bd9Sstevel@tonic-gate 	door_dp->audit_uid = client_cred.dc_ruid;
142*7c478bd9Sstevel@tonic-gate 	door_dp->audit_gid = client_cred.dc_rgid;
143*7c478bd9Sstevel@tonic-gate 	door_dp->audit_pid = client_cred.dc_pid;
144*7c478bd9Sstevel@tonic-gate 	door_dp->audit_asid = door_dp->audit_ap.ap_asid;
145*7c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_port = door_dp->audit_ap.ap_termid.at_port;
146*7c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_type = door_dp->audit_ap.ap_termid.at_type;
147*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < (door_dp->audit_ap.ap_termid.at_type/4); i++)
148*7c478bd9Sstevel@tonic-gate 		door_dp->audit_tid.at_addr[i] =
149*7c478bd9Sstevel@tonic-gate 			door_dp->audit_ap.ap_termid.at_addr[i];
150*7c478bd9Sstevel@tonic-gate 	(void) audit_save_policy(door_dp);
151*7c478bd9Sstevel@tonic-gate 	return (0);
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /*
155*7c478bd9Sstevel@tonic-gate  * audit_save_namask():
156*7c478bd9Sstevel@tonic-gate  *	Save the namask using the naflags entry in the audit_control file.
157*7c478bd9Sstevel@tonic-gate  *	Return 0 if successful.
158*7c478bd9Sstevel@tonic-gate  *	Return -1, and don't change the namask, if failed.
159*7c478bd9Sstevel@tonic-gate  *	Side Effect: Sets audit_na to -1 if error, 1 if successful.
160*7c478bd9Sstevel@tonic-gate  */
161*7c478bd9Sstevel@tonic-gate static int
162*7c478bd9Sstevel@tonic-gate audit_save_namask(door_data_t *door_dp)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	au_mask_t mask;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	door_dp->audit_na = -1;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	/*
169*7c478bd9Sstevel@tonic-gate 	 * get non-attributable system event mask from kernel.
170*7c478bd9Sstevel@tonic-gate 	 */
171*7c478bd9Sstevel@tonic-gate 	if (auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask)) != 0) {
172*7c478bd9Sstevel@tonic-gate 		return (-1);
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	door_dp->audit_namask.am_success = mask.am_success;
176*7c478bd9Sstevel@tonic-gate 	door_dp->audit_namask.am_failure = mask.am_failure;
177*7c478bd9Sstevel@tonic-gate 	door_dp->audit_na = 1;
178*7c478bd9Sstevel@tonic-gate 	return (0);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate /*
182*7c478bd9Sstevel@tonic-gate  * audit_audit:
183*7c478bd9Sstevel@tonic-gate  *	Cut and audit record if it is selected.
184*7c478bd9Sstevel@tonic-gate  *	Return 0, if successfully written.
185*7c478bd9Sstevel@tonic-gate  *	Return 0, if not written, and not expected to write.
186*7c478bd9Sstevel@tonic-gate  *	Return -1, if not written because of unexpected error.
187*7c478bd9Sstevel@tonic-gate  */
188*7c478bd9Sstevel@tonic-gate int
189*7c478bd9Sstevel@tonic-gate audit_audit(door_data_t *door_dp)
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	int ad;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	if (can_audit() == 0) {
194*7c478bd9Sstevel@tonic-gate 		return (0);
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	if (door_dp->audit_na) {
198*7c478bd9Sstevel@tonic-gate 		if (!audit_na_selected(door_dp)) {
199*7c478bd9Sstevel@tonic-gate 			return (0);
200*7c478bd9Sstevel@tonic-gate 		}
201*7c478bd9Sstevel@tonic-gate 	} else if (!audit_selected(door_dp)) {
202*7c478bd9Sstevel@tonic-gate 		return (0);
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	if ((ad = au_open()) == -1) {
206*7c478bd9Sstevel@tonic-gate 		return (-1);
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	(void) au_write(ad, au_to_subject_ex(door_dp->audit_auid,
210*7c478bd9Sstevel@tonic-gate 		door_dp->audit_euid,
211*7c478bd9Sstevel@tonic-gate 		door_dp->audit_egid,
212*7c478bd9Sstevel@tonic-gate 		door_dp->audit_uid, door_dp->audit_gid, door_dp->audit_pid,
213*7c478bd9Sstevel@tonic-gate 		door_dp->audit_asid, &door_dp->audit_tid));
214*7c478bd9Sstevel@tonic-gate 	if (door_dp->audit_policy & AUDIT_GROUP) {
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 		int ng;
217*7c478bd9Sstevel@tonic-gate 		gid_t grplst[NGROUPS_MAX];
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 		(void) memset(grplst, 0, sizeof (grplst));
220*7c478bd9Sstevel@tonic-gate 		if ((ng = getgroups(NGROUPS_UMAX, grplst))) {
221*7c478bd9Sstevel@tonic-gate 			(void) au_write(ad, au_to_newgroups(ng, grplst));
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate 	}
224*7c478bd9Sstevel@tonic-gate 	if (strlen(door_dp->audit_text) != 0) {
225*7c478bd9Sstevel@tonic-gate 		(void) au_write(ad, au_to_text(door_dp->audit_text));
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 	if (strlen(door_dp->audit_text1) != 0) {
228*7c478bd9Sstevel@tonic-gate 		(void) au_write(ad, au_to_text(door_dp->audit_text1));
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 	if (door_dp->audit_path != NULL) {
231*7c478bd9Sstevel@tonic-gate 		(void) au_write(ad, au_to_path(door_dp->audit_path));
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate #ifdef _LP64
234*7c478bd9Sstevel@tonic-gate 	(void) au_write(ad, au_to_return64((door_dp->audit_sorf == 0) ? 0 : -1,
235*7c478bd9Sstevel@tonic-gate 		(int64_t)door_dp->audit_sorf));
236*7c478bd9Sstevel@tonic-gate #else
237*7c478bd9Sstevel@tonic-gate 	(void) au_write(ad, au_to_return32((door_dp->audit_sorf == 0) ? 0 : -1,
238*7c478bd9Sstevel@tonic-gate 		(int32_t)door_dp->audit_sorf));
239*7c478bd9Sstevel@tonic-gate #endif
240*7c478bd9Sstevel@tonic-gate 	if (au_close(ad, 1, door_dp->audit_event) < 0) {
241*7c478bd9Sstevel@tonic-gate 		(void) au_close(ad, 0, 0);
242*7c478bd9Sstevel@tonic-gate 		return (-1);
243*7c478bd9Sstevel@tonic-gate 	}
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	return (0);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate static int
249*7c478bd9Sstevel@tonic-gate audit_na_selected(door_data_t *door_dp)
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	if (door_dp->audit_na == -1) {
252*7c478bd9Sstevel@tonic-gate 		return (-1);
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	return (selected(door_dp->audit_event,
256*7c478bd9Sstevel@tonic-gate 		&door_dp->audit_namask, door_dp->audit_sorf));
257*7c478bd9Sstevel@tonic-gate }
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate static int
260*7c478bd9Sstevel@tonic-gate audit_selected(door_data_t *door_dp)
261*7c478bd9Sstevel@tonic-gate {
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	if (door_dp->audit_uid < 0) {
264*7c478bd9Sstevel@tonic-gate 		(void) audit_save_namask(door_dp);
265*7c478bd9Sstevel@tonic-gate 		return (audit_na_selected(door_dp));
266*7c478bd9Sstevel@tonic-gate 	}
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	return (selected(door_dp->audit_event,
269*7c478bd9Sstevel@tonic-gate 		&door_dp->audit_ap.ap_mask, door_dp->audit_sorf));
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate static int
273*7c478bd9Sstevel@tonic-gate selected(au_event_t e, au_mask_t *m, int sorf)
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate 	int prs_sorf;
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	if (sorf == 0) {
278*7c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_SUCCESS;
279*7c478bd9Sstevel@tonic-gate 	} else if (sorf == -1) {
280*7c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_FAILURE;
281*7c478bd9Sstevel@tonic-gate 	} else {
282*7c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_BOTH;
283*7c478bd9Sstevel@tonic-gate 	}
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 	return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD));
286*7c478bd9Sstevel@tonic-gate }
287