xref: /titanic_51/usr/src/lib/libbsm/common/generic.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 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include <netdb.h>
29*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
30*7c478bd9Sstevel@tonic-gate #include <pwd.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
37*7c478bd9Sstevel@tonic-gate #include <string.h>
38*7c478bd9Sstevel@tonic-gate #include <unistd.h>
39*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
40*7c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
41*7c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
42*7c478bd9Sstevel@tonic-gate #include <bsm/audit_uevents.h>
43*7c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	AUC_NEVER	-2	/* audit module not loaded */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /* Private Functions */
48*7c478bd9Sstevel@tonic-gate static int selected(au_event_t, au_mask_t *, int);
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate int aug_selected();
51*7c478bd9Sstevel@tonic-gate int aug_na_selected();
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate /* Global Variables */
54*7c478bd9Sstevel@tonic-gate static au_id_t		aug_auid;	/* auid of user writing audit record */
55*7c478bd9Sstevel@tonic-gate static uid_t		aug_uid;	/* uid of user writing audit record */
56*7c478bd9Sstevel@tonic-gate static uid_t		aug_euid;	/* euid of user writing audit record */
57*7c478bd9Sstevel@tonic-gate static gid_t		aug_gid;	/* gid of user writing audit record */
58*7c478bd9Sstevel@tonic-gate static gid_t		aug_egid;	/* euid of user writing audit record */
59*7c478bd9Sstevel@tonic-gate static pid_t		aug_pid;	/* pid of user writing audit record */
60*7c478bd9Sstevel@tonic-gate static au_tid_addr_t	aug_tid;	/* tid of user writing audit record */
61*7c478bd9Sstevel@tonic-gate static int		aug_na;		/* 0 if event is attributable */
62*7c478bd9Sstevel@tonic-gate static au_mask_t	aug_namask;	/* not attributable flags */
63*7c478bd9Sstevel@tonic-gate static au_event_t	aug_event;	/* id of event being audited */
64*7c478bd9Sstevel@tonic-gate static int 		aug_sorf;	/* success or failure of aug_event */
65*7c478bd9Sstevel@tonic-gate static char		*aug_text;	/* misc text to be written to trail */
66*7c478bd9Sstevel@tonic-gate static char		*aug_text1;	/* misc text to be written to trail */
67*7c478bd9Sstevel@tonic-gate static char		*aug_text2;	/* misc text to be written to trail */
68*7c478bd9Sstevel@tonic-gate static au_asid_t	aug_asid;	/* asid of process writing record */
69*7c478bd9Sstevel@tonic-gate static int 		(*aug_afunc)();	/* write additional tokens if needed */
70*7c478bd9Sstevel@tonic-gate static char		*aug_path;	/* path token */
71*7c478bd9Sstevel@tonic-gate static int		aug_policy;	/* kernel audit policy */
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate  * cannot_audit:
75*7c478bd9Sstevel@tonic-gate  *	Return 1 if audit module not loaded.
76*7c478bd9Sstevel@tonic-gate  *	Return 0 otherwise.
77*7c478bd9Sstevel@tonic-gate  *
78*7c478bd9Sstevel@tonic-gate  * The argument, force, should be set to 1 for long-lived processes
79*7c478bd9Sstevel@tonic-gate  * like some daemons.  Force should be set to 0 for most programs.
80*7c478bd9Sstevel@tonic-gate  */
81*7c478bd9Sstevel@tonic-gate int
82*7c478bd9Sstevel@tonic-gate cannot_audit(force)
83*7c478bd9Sstevel@tonic-gate 	int force;
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	static int auc = AUC_UNSET;
86*7c478bd9Sstevel@tonic-gate 	int cond = 0;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	if (auc == AUC_UNSET || force) {
89*7c478bd9Sstevel@tonic-gate 		if (auditon(A_GETCOND, (caddr_t)&cond, sizeof (cond))) {
90*7c478bd9Sstevel@tonic-gate 			auc = AUC_NEVER;
91*7c478bd9Sstevel@tonic-gate 		} else {
92*7c478bd9Sstevel@tonic-gate 			auc = cond;
93*7c478bd9Sstevel@tonic-gate 		}
94*7c478bd9Sstevel@tonic-gate 	}
95*7c478bd9Sstevel@tonic-gate 	return (auc == AUC_NEVER);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /*
99*7c478bd9Sstevel@tonic-gate  * aug_init():
100*7c478bd9Sstevel@tonic-gate  *	Initialize global variables.
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate void
103*7c478bd9Sstevel@tonic-gate aug_init()
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	aug_auid = -1;
106*7c478bd9Sstevel@tonic-gate 	aug_uid = -1;
107*7c478bd9Sstevel@tonic-gate 	aug_euid = -1;
108*7c478bd9Sstevel@tonic-gate 	aug_gid = -1;
109*7c478bd9Sstevel@tonic-gate 	aug_egid = -1;
110*7c478bd9Sstevel@tonic-gate 	aug_pid = -1;
111*7c478bd9Sstevel@tonic-gate 	aug_tid.at_port = 0;
112*7c478bd9Sstevel@tonic-gate 	aug_tid.at_type = AU_IPv4;
113*7c478bd9Sstevel@tonic-gate 	aug_tid.at_addr[0] = 0;
114*7c478bd9Sstevel@tonic-gate 	aug_tid.at_addr[1] = 0;
115*7c478bd9Sstevel@tonic-gate 	aug_tid.at_addr[2] = 0;
116*7c478bd9Sstevel@tonic-gate 	aug_tid.at_addr[3] = 0;
117*7c478bd9Sstevel@tonic-gate 	aug_namask.am_success = AU_MASK_ALL;
118*7c478bd9Sstevel@tonic-gate 	aug_namask.am_failure = AU_MASK_ALL;
119*7c478bd9Sstevel@tonic-gate 	aug_event = 0;
120*7c478bd9Sstevel@tonic-gate 	aug_sorf = -2;
121*7c478bd9Sstevel@tonic-gate 	aug_text = NULL;
122*7c478bd9Sstevel@tonic-gate 	aug_text1 = NULL;
123*7c478bd9Sstevel@tonic-gate 	aug_text2 = NULL;
124*7c478bd9Sstevel@tonic-gate 	aug_na = 0;
125*7c478bd9Sstevel@tonic-gate 	aug_asid = -1;
126*7c478bd9Sstevel@tonic-gate 	aug_afunc = NULL;
127*7c478bd9Sstevel@tonic-gate 	aug_path = NULL;
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate /*
131*7c478bd9Sstevel@tonic-gate  * aug_get_port:
132*7c478bd9Sstevel@tonic-gate  *	Return the raw device number of the port to which the
133*7c478bd9Sstevel@tonic-gate  *	current process is attached (assumed to be attached
134*7c478bd9Sstevel@tonic-gate  *	through file descriptor 0) or 0 if can't stat the port.
135*7c478bd9Sstevel@tonic-gate  */
136*7c478bd9Sstevel@tonic-gate dev_t
137*7c478bd9Sstevel@tonic-gate aug_get_port()
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate 	int	rc;
140*7c478bd9Sstevel@tonic-gate 	char	*ttyn;
141*7c478bd9Sstevel@tonic-gate 	struct stat sb;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	ttyn = ttyname(0);
144*7c478bd9Sstevel@tonic-gate 	if (ttyn == 0 || *ttyn == '\0') {
145*7c478bd9Sstevel@tonic-gate 		return (0);
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	rc = stat(ttyn, &sb);
149*7c478bd9Sstevel@tonic-gate 	if (rc < 0) {
150*7c478bd9Sstevel@tonic-gate 		perror("stat");
151*7c478bd9Sstevel@tonic-gate 		return (0);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	return ((dev_t)sb.st_rdev);
155*7c478bd9Sstevel@tonic-gate }
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate /*
158*7c478bd9Sstevel@tonic-gate  * aug_get_machine:
159*7c478bd9Sstevel@tonic-gate  *	Return internet address of host hostname,
160*7c478bd9Sstevel@tonic-gate  *	or 0 if can't do lookup.
161*7c478bd9Sstevel@tonic-gate  */
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate int
164*7c478bd9Sstevel@tonic-gate aug_get_machine(const char *hostname, uint32_t *buf, uint32_t *type)
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate 	struct addrinfo *ai;
167*7c478bd9Sstevel@tonic-gate 	int err;
168*7c478bd9Sstevel@tonic-gate 	void *p;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	err = getaddrinfo(hostname, NULL, NULL, &ai);
171*7c478bd9Sstevel@tonic-gate 	if (err != 0)
172*7c478bd9Sstevel@tonic-gate 		return (0);
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	switch (ai->ai_family) {
175*7c478bd9Sstevel@tonic-gate 	case AF_INET:
176*7c478bd9Sstevel@tonic-gate 		/* LINTED */
177*7c478bd9Sstevel@tonic-gate 		p = &((struct sockaddr_in *)ai->ai_addr)->sin_addr,
178*7c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, p,
179*7c478bd9Sstevel@tonic-gate 		    sizeof (((struct sockaddr_in *)0)->sin_addr));
180*7c478bd9Sstevel@tonic-gate 		*type = AU_IPv4;
181*7c478bd9Sstevel@tonic-gate 		break;
182*7c478bd9Sstevel@tonic-gate 	case AF_INET6:
183*7c478bd9Sstevel@tonic-gate 		/* LINTED */
184*7c478bd9Sstevel@tonic-gate 		p = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
185*7c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, p,
186*7c478bd9Sstevel@tonic-gate 		    sizeof (((struct sockaddr_in6 *)0)->sin6_addr));
187*7c478bd9Sstevel@tonic-gate 		*type = AU_IPv6;
188*7c478bd9Sstevel@tonic-gate 		break;
189*7c478bd9Sstevel@tonic-gate 	default:
190*7c478bd9Sstevel@tonic-gate 		return (0);
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	freeaddrinfo(ai);
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	return (1);
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate void
199*7c478bd9Sstevel@tonic-gate aug_save_auid(au_id_t id)
200*7c478bd9Sstevel@tonic-gate {
201*7c478bd9Sstevel@tonic-gate 	aug_auid = id;
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate void
205*7c478bd9Sstevel@tonic-gate aug_save_uid(uid_t id)
206*7c478bd9Sstevel@tonic-gate {
207*7c478bd9Sstevel@tonic-gate 	aug_uid = id;
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate void
211*7c478bd9Sstevel@tonic-gate aug_save_euid(uid_t id)
212*7c478bd9Sstevel@tonic-gate {
213*7c478bd9Sstevel@tonic-gate 	aug_euid = id;
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate void
217*7c478bd9Sstevel@tonic-gate aug_save_gid(gid_t id)
218*7c478bd9Sstevel@tonic-gate {
219*7c478bd9Sstevel@tonic-gate 	aug_gid = id;
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate void
223*7c478bd9Sstevel@tonic-gate aug_save_egid(gid_t id)
224*7c478bd9Sstevel@tonic-gate {
225*7c478bd9Sstevel@tonic-gate 	aug_egid = id;
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate void
229*7c478bd9Sstevel@tonic-gate aug_save_pid(pid_t id)
230*7c478bd9Sstevel@tonic-gate {
231*7c478bd9Sstevel@tonic-gate 	aug_pid = id;
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate void
235*7c478bd9Sstevel@tonic-gate aug_save_asid(au_asid_t id)
236*7c478bd9Sstevel@tonic-gate {
237*7c478bd9Sstevel@tonic-gate 	aug_asid = id;
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate void
241*7c478bd9Sstevel@tonic-gate aug_save_afunc(int (*afunc)())
242*7c478bd9Sstevel@tonic-gate {
243*7c478bd9Sstevel@tonic-gate 	aug_afunc = afunc;
244*7c478bd9Sstevel@tonic-gate }
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate void
247*7c478bd9Sstevel@tonic-gate aug_save_tid(dev_t port, int machine)
248*7c478bd9Sstevel@tonic-gate {
249*7c478bd9Sstevel@tonic-gate 	aug_tid.at_port = port;
250*7c478bd9Sstevel@tonic-gate 	aug_tid.at_type = AU_IPv4;
251*7c478bd9Sstevel@tonic-gate 	aug_tid.at_addr[0] = machine;
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate void
255*7c478bd9Sstevel@tonic-gate aug_save_tid_ex(dev_t port, uint32_t *machine, uint32_t type)
256*7c478bd9Sstevel@tonic-gate {
257*7c478bd9Sstevel@tonic-gate 	int i;
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	aug_tid.at_port = port;
260*7c478bd9Sstevel@tonic-gate 	if ((type != AU_IPv4) && (type != AU_IPv6))
261*7c478bd9Sstevel@tonic-gate 		type = AU_IPv4;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	aug_tid.at_type = type;
264*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < (type/4); i++)
265*7c478bd9Sstevel@tonic-gate 		aug_tid.at_addr[i] = machine[i];
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate int
269*7c478bd9Sstevel@tonic-gate aug_save_me(void)
270*7c478bd9Sstevel@tonic-gate {
271*7c478bd9Sstevel@tonic-gate 	auditinfo_addr_t ai;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	if (getaudit_addr(&ai, sizeof (ai)))
274*7c478bd9Sstevel@tonic-gate 		return (-1);
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 	aug_save_auid(ai.ai_auid);
277*7c478bd9Sstevel@tonic-gate 	aug_save_euid(geteuid());
278*7c478bd9Sstevel@tonic-gate 	aug_save_egid(getegid());
279*7c478bd9Sstevel@tonic-gate 	aug_save_uid(getuid());
280*7c478bd9Sstevel@tonic-gate 	aug_save_gid(getgid());
281*7c478bd9Sstevel@tonic-gate 	aug_save_pid(getpid());
282*7c478bd9Sstevel@tonic-gate 	aug_save_asid(ai.ai_asid);
283*7c478bd9Sstevel@tonic-gate 	aug_save_tid_ex(ai.ai_termid.at_port,
284*7c478bd9Sstevel@tonic-gate 		ai.ai_termid.at_addr,
285*7c478bd9Sstevel@tonic-gate 		ai.ai_termid.at_type);
286*7c478bd9Sstevel@tonic-gate 	return (0);
287*7c478bd9Sstevel@tonic-gate }
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate /*
290*7c478bd9Sstevel@tonic-gate  * aug_save_namask():
291*7c478bd9Sstevel@tonic-gate  *	Save the namask using the naflags entry in the audit_control file.
292*7c478bd9Sstevel@tonic-gate  *	Return 0 if successful.
293*7c478bd9Sstevel@tonic-gate  *	Return -1, and don't change the namask, if failed.
294*7c478bd9Sstevel@tonic-gate  *	Side Effect: Sets aug_na to -1 if error, 1 if successful.
295*7c478bd9Sstevel@tonic-gate  */
296*7c478bd9Sstevel@tonic-gate int
297*7c478bd9Sstevel@tonic-gate aug_save_namask()
298*7c478bd9Sstevel@tonic-gate {
299*7c478bd9Sstevel@tonic-gate 	au_mask_t mask;
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	aug_na = -1;
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	/*
304*7c478bd9Sstevel@tonic-gate 	 * get non-attributable system event mask from kernel.
305*7c478bd9Sstevel@tonic-gate 	 */
306*7c478bd9Sstevel@tonic-gate 	if (auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask)) != 0) {
307*7c478bd9Sstevel@tonic-gate 		return (-1);
308*7c478bd9Sstevel@tonic-gate 	}
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	aug_namask.am_success = mask.am_success;
311*7c478bd9Sstevel@tonic-gate 	aug_namask.am_failure = mask.am_failure;
312*7c478bd9Sstevel@tonic-gate 	aug_na = 1;
313*7c478bd9Sstevel@tonic-gate 	return (0);
314*7c478bd9Sstevel@tonic-gate }
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate void
317*7c478bd9Sstevel@tonic-gate aug_save_event(au_event_t id)
318*7c478bd9Sstevel@tonic-gate {
319*7c478bd9Sstevel@tonic-gate 	aug_event = id;
320*7c478bd9Sstevel@tonic-gate }
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate void
323*7c478bd9Sstevel@tonic-gate aug_save_sorf(int sorf)
324*7c478bd9Sstevel@tonic-gate {
325*7c478bd9Sstevel@tonic-gate 	aug_sorf = sorf;
326*7c478bd9Sstevel@tonic-gate }
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate void
329*7c478bd9Sstevel@tonic-gate aug_save_text(char *s)
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate 	if (aug_text != NULL)
332*7c478bd9Sstevel@tonic-gate 		free(aug_text);
333*7c478bd9Sstevel@tonic-gate 	if (s == NULL)
334*7c478bd9Sstevel@tonic-gate 		aug_text = NULL;
335*7c478bd9Sstevel@tonic-gate 	else
336*7c478bd9Sstevel@tonic-gate 		aug_text = strdup(s);
337*7c478bd9Sstevel@tonic-gate }
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate void
340*7c478bd9Sstevel@tonic-gate aug_save_text1(char *s)
341*7c478bd9Sstevel@tonic-gate {
342*7c478bd9Sstevel@tonic-gate 	if (aug_text1 != NULL)
343*7c478bd9Sstevel@tonic-gate 		free(aug_text1);
344*7c478bd9Sstevel@tonic-gate 	if (s == NULL)
345*7c478bd9Sstevel@tonic-gate 		aug_text1 = NULL;
346*7c478bd9Sstevel@tonic-gate 	else
347*7c478bd9Sstevel@tonic-gate 		aug_text1 = strdup(s);
348*7c478bd9Sstevel@tonic-gate }
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate void
351*7c478bd9Sstevel@tonic-gate aug_save_text2(char *s)
352*7c478bd9Sstevel@tonic-gate {
353*7c478bd9Sstevel@tonic-gate 	if (aug_text2 != NULL)
354*7c478bd9Sstevel@tonic-gate 		free(aug_text2);
355*7c478bd9Sstevel@tonic-gate 	if (s == NULL)
356*7c478bd9Sstevel@tonic-gate 		aug_text2 = NULL;
357*7c478bd9Sstevel@tonic-gate 	else
358*7c478bd9Sstevel@tonic-gate 		aug_text2 = strdup(s);
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate void
362*7c478bd9Sstevel@tonic-gate aug_save_na(int flag)
363*7c478bd9Sstevel@tonic-gate {
364*7c478bd9Sstevel@tonic-gate 	aug_na = flag;
365*7c478bd9Sstevel@tonic-gate }
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate void
368*7c478bd9Sstevel@tonic-gate aug_save_path(char *s)
369*7c478bd9Sstevel@tonic-gate {
370*7c478bd9Sstevel@tonic-gate 	if (aug_path != NULL)
371*7c478bd9Sstevel@tonic-gate 		free(aug_path);
372*7c478bd9Sstevel@tonic-gate 	if (s == NULL)
373*7c478bd9Sstevel@tonic-gate 		aug_path = NULL;
374*7c478bd9Sstevel@tonic-gate 	aug_path = strdup(s);
375*7c478bd9Sstevel@tonic-gate }
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate int
378*7c478bd9Sstevel@tonic-gate aug_save_policy()
379*7c478bd9Sstevel@tonic-gate {
380*7c478bd9Sstevel@tonic-gate 	int policy;
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	if (auditon(A_GETPOLICY, (caddr_t)&policy, sizeof (policy))) {
383*7c478bd9Sstevel@tonic-gate 		return (-1);
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 	aug_policy = policy;
386*7c478bd9Sstevel@tonic-gate 	return (0);
387*7c478bd9Sstevel@tonic-gate }
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate /*
390*7c478bd9Sstevel@tonic-gate  * aug_audit:
391*7c478bd9Sstevel@tonic-gate  *	Cut and audit record if it is selected.
392*7c478bd9Sstevel@tonic-gate  *	Return 0, if successfully written.
393*7c478bd9Sstevel@tonic-gate  *	Return 0, if not written, and not expected to write.
394*7c478bd9Sstevel@tonic-gate  *	Return -1, if not written because of unexpected error.
395*7c478bd9Sstevel@tonic-gate  */
396*7c478bd9Sstevel@tonic-gate int
397*7c478bd9Sstevel@tonic-gate aug_audit(void)
398*7c478bd9Sstevel@tonic-gate {
399*7c478bd9Sstevel@tonic-gate 	int ad;
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
402*7c478bd9Sstevel@tonic-gate 		return (0);
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	if (aug_na) {
406*7c478bd9Sstevel@tonic-gate 		if (!aug_na_selected()) {
407*7c478bd9Sstevel@tonic-gate 			return (0);
408*7c478bd9Sstevel@tonic-gate 		}
409*7c478bd9Sstevel@tonic-gate 	} else if (!aug_selected()) {
410*7c478bd9Sstevel@tonic-gate 		return (0);
411*7c478bd9Sstevel@tonic-gate 	}
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	if ((ad = au_open()) == -1) {
414*7c478bd9Sstevel@tonic-gate 		return (-1);
415*7c478bd9Sstevel@tonic-gate 	}
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	(void) au_write(ad, au_to_subject_ex(aug_auid, aug_euid, aug_egid,
418*7c478bd9Sstevel@tonic-gate 		aug_uid, aug_gid, aug_pid, aug_asid, &aug_tid));
419*7c478bd9Sstevel@tonic-gate 	if (aug_policy & AUDIT_GROUP) {
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 		int ng;
422*7c478bd9Sstevel@tonic-gate 		gid_t grplst[NGROUPS_MAX];
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 		(void) memset(grplst, 0, sizeof (grplst));
425*7c478bd9Sstevel@tonic-gate 		if ((ng = getgroups(NGROUPS_UMAX, grplst))) {
426*7c478bd9Sstevel@tonic-gate 			(void) au_write(ad, au_to_newgroups(ng, grplst));
427*7c478bd9Sstevel@tonic-gate 		}
428*7c478bd9Sstevel@tonic-gate 	}
429*7c478bd9Sstevel@tonic-gate 	if (aug_text != NULL) {
430*7c478bd9Sstevel@tonic-gate 		(void) au_write(ad, au_to_text(aug_text));
431*7c478bd9Sstevel@tonic-gate 	}
432*7c478bd9Sstevel@tonic-gate 	if (aug_text1 != NULL) {
433*7c478bd9Sstevel@tonic-gate 		(void) au_write(ad, au_to_text(aug_text1));
434*7c478bd9Sstevel@tonic-gate 	}
435*7c478bd9Sstevel@tonic-gate 	if (aug_text2 != NULL) {
436*7c478bd9Sstevel@tonic-gate 		(void) au_write(ad, au_to_text(aug_text2));
437*7c478bd9Sstevel@tonic-gate 	}
438*7c478bd9Sstevel@tonic-gate 	if (aug_path != NULL) {
439*7c478bd9Sstevel@tonic-gate 		(void) au_write(ad, au_to_path(aug_path));
440*7c478bd9Sstevel@tonic-gate 	}
441*7c478bd9Sstevel@tonic-gate 	if (aug_afunc != NULL) {
442*7c478bd9Sstevel@tonic-gate 		(*aug_afunc)(ad);
443*7c478bd9Sstevel@tonic-gate 	}
444*7c478bd9Sstevel@tonic-gate #ifdef _LP64
445*7c478bd9Sstevel@tonic-gate 	(void) au_write(ad, au_to_return64((aug_sorf == 0) ? 0 : -1,
446*7c478bd9Sstevel@tonic-gate 						(int64_t)aug_sorf));
447*7c478bd9Sstevel@tonic-gate #else
448*7c478bd9Sstevel@tonic-gate 	(void) au_write(ad, au_to_return32((aug_sorf == 0) ? 0 : -1,
449*7c478bd9Sstevel@tonic-gate 						(int32_t)aug_sorf));
450*7c478bd9Sstevel@tonic-gate #endif
451*7c478bd9Sstevel@tonic-gate 	if (au_close(ad, 1, aug_event) < 0) {
452*7c478bd9Sstevel@tonic-gate 		(void) au_close(ad, 0, 0);
453*7c478bd9Sstevel@tonic-gate 		return (-1);
454*7c478bd9Sstevel@tonic-gate 	}
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	return (0);
457*7c478bd9Sstevel@tonic-gate }
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate int
460*7c478bd9Sstevel@tonic-gate aug_na_selected()
461*7c478bd9Sstevel@tonic-gate {
462*7c478bd9Sstevel@tonic-gate 	if (aug_na == -1) {
463*7c478bd9Sstevel@tonic-gate 		return (-1);
464*7c478bd9Sstevel@tonic-gate 	}
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 	return (selected(aug_event, &aug_namask, aug_sorf));
467*7c478bd9Sstevel@tonic-gate }
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate int
470*7c478bd9Sstevel@tonic-gate aug_selected()
471*7c478bd9Sstevel@tonic-gate {
472*7c478bd9Sstevel@tonic-gate 	auditinfo_addr_t mask;
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	if (aug_uid < 0) {
475*7c478bd9Sstevel@tonic-gate 		(void) aug_save_namask();
476*7c478bd9Sstevel@tonic-gate 		return (aug_na_selected());
477*7c478bd9Sstevel@tonic-gate 	}
478*7c478bd9Sstevel@tonic-gate 	if (getaudit_addr(&mask, sizeof (mask))) {
479*7c478bd9Sstevel@tonic-gate 		return (-1);
480*7c478bd9Sstevel@tonic-gate 	}
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 	return (selected(aug_event, &mask.ai_mask, aug_sorf));
483*7c478bd9Sstevel@tonic-gate }
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate static int
486*7c478bd9Sstevel@tonic-gate selected(au_event_t e, au_mask_t *m, int sorf)
487*7c478bd9Sstevel@tonic-gate {
488*7c478bd9Sstevel@tonic-gate 	int prs_sorf;
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 	if (sorf == 0) {
491*7c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_SUCCESS;
492*7c478bd9Sstevel@tonic-gate 	} else if (sorf == -1) {
493*7c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_FAILURE;
494*7c478bd9Sstevel@tonic-gate 	} else {
495*7c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_BOTH;
496*7c478bd9Sstevel@tonic-gate 	}
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate 	return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD));
499*7c478bd9Sstevel@tonic-gate }
500