1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <bsm/adt.h>
30*0Sstevel@tonic-gate #include <bsm/adt_event.h>
31*0Sstevel@tonic-gate #include <assert.h>
32*0Sstevel@tonic-gate #include <bsm/audit.h>
33*0Sstevel@tonic-gate #include <bsm/audit_record.h>
34*0Sstevel@tonic-gate #include <bsm/libbsm.h>
35*0Sstevel@tonic-gate #include <door.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <generic.h>
38*0Sstevel@tonic-gate #include <md5.h>
39*0Sstevel@tonic-gate #include <sys/mkdev.h>
40*0Sstevel@tonic-gate #include <netdb.h>
41*0Sstevel@tonic-gate #include <nss_dbdefs.h>
42*0Sstevel@tonic-gate #include <pwd.h>
43*0Sstevel@tonic-gate #include <sys/stat.h>
44*0Sstevel@tonic-gate #include <time.h>
45*0Sstevel@tonic-gate #include <stdlib.h>
46*0Sstevel@tonic-gate #include <string.h>
47*0Sstevel@tonic-gate #include <synch.h>
48*0Sstevel@tonic-gate #include <sys/systeminfo.h>
49*0Sstevel@tonic-gate #include <syslog.h>
50*0Sstevel@tonic-gate #include <thread.h>
51*0Sstevel@tonic-gate #include <unistd.h>
52*0Sstevel@tonic-gate #include <adt_xlate.h>
53*0Sstevel@tonic-gate #include <adt_ucred.h>
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static int adt_selected(struct adt_event_state *, au_event_t, int);
56*0Sstevel@tonic-gate static int adt_init(adt_internal_state_t *, int);
57*0Sstevel@tonic-gate static int adt_import(adt_internal_state_t *, const adt_export_data_t *);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #ifdef C2_DEBUG
60*0Sstevel@tonic-gate #define	DPRINTF(x) {printf x; }
61*0Sstevel@tonic-gate #define	DFLUSH fflush(stdout);
62*0Sstevel@tonic-gate #else
63*0Sstevel@tonic-gate #define	DPRINTF(x)
64*0Sstevel@tonic-gate #define	DFLUSH
65*0Sstevel@tonic-gate #endif
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate extern int _mutex_lock(mutex_t *);
68*0Sstevel@tonic-gate extern int _mutex_unlock(mutex_t *);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static int auditstate = AUC_DISABLED;	/* default state */
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate  * adt_write_syslog
74*0Sstevel@tonic-gate  *
75*0Sstevel@tonic-gate  * errors that are not the user's fault (bugs or whatever in
76*0Sstevel@tonic-gate  * the underlying audit code are noted in syslog.)
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  * Avoid calling adt_write_syslog for things that can happen
79*0Sstevel@tonic-gate  * at high volume.
80*0Sstevel@tonic-gate  *
81*0Sstevel@tonic-gate  * syslog's open (openlog) and close (closelog) are interesting;
82*0Sstevel@tonic-gate  * openlog *may* create a file descriptor and is optional.  closelog
83*0Sstevel@tonic-gate  * *will* close any open file descriptors and is also optional.
84*0Sstevel@tonic-gate  *
85*0Sstevel@tonic-gate  * Since syslog may also be used by the calling application, the
86*0Sstevel@tonic-gate  * choice is to avoid openlog, which sets some otherwise useful
87*0Sstevel@tonic-gate  * parameters, and to embed "Solaris_audit" in the log message.
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate void
91*0Sstevel@tonic-gate adt_write_syslog(const char *message, int err)
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	int	save_errno;
94*0Sstevel@tonic-gate 	int	mask_priority;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	save_errno = errno;
97*0Sstevel@tonic-gate 	errno = err;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	DPRINTF(("syslog called: %s\n", message));
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	mask_priority = setlogmask(LOG_MASK(LOG_ALERT));
102*0Sstevel@tonic-gate 	syslog(LOG_ALERT, "Solaris_audit %s: %m", message, err);
103*0Sstevel@tonic-gate 	(void) setlogmask(mask_priority);
104*0Sstevel@tonic-gate 	errno = save_errno;
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  * return true if audit is enabled.  "Enabled" is any state
109*0Sstevel@tonic-gate  * other than AUC_DISABLED.
110*0Sstevel@tonic-gate  *
111*0Sstevel@tonic-gate  * states are
112*0Sstevel@tonic-gate  *		AUC_INIT_AUDIT	-- c2audit queuing enabled.
113*0Sstevel@tonic-gate  *		AUC_AUDITING	-- up and running
114*0Sstevel@tonic-gate  *		AUC_DISABLED	-- no audit subsystem loaded
115*0Sstevel@tonic-gate  *		AUC_UNSET	-- early boot state
116*0Sstevel@tonic-gate  *		AUC_NOAUDIT	-- subsystem loaded, turned off via
117*0Sstevel@tonic-gate  *				   auditon(A_SETCOND...)
118*0Sstevel@tonic-gate  *		AUC_NOSPACE	-- up and running, but log partitions are full
119*0Sstevel@tonic-gate  *
120*0Sstevel@tonic-gate  *	For purpose of this API, anything but AUC_DISABLED or
121*0Sstevel@tonic-gate  *	AUC_UNSET is enabled; however one never actually sees
122*0Sstevel@tonic-gate  *	AUC_DISABLED since auditon returns EINVAL in that case.  Any
123*0Sstevel@tonic-gate  *	auditon error is considered the same as EINVAL for our
124*0Sstevel@tonic-gate  *	purpose.  auditstate is not changed by auditon if an error
125*0Sstevel@tonic-gate  *	is returned.
126*0Sstevel@tonic-gate  */
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate boolean_t
129*0Sstevel@tonic-gate adt_audit_enabled(void) {
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	(void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate));
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	return (auditstate != AUC_DISABLED);
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate /*
137*0Sstevel@tonic-gate  * The man page for getpwuid_r says the buffer must be big enough
138*0Sstevel@tonic-gate  * or ERANGE will be returned, but offers no guidance for how big
139*0Sstevel@tonic-gate  * the buffer should be or a way to calculate it.  If you get
140*0Sstevel@tonic-gate  * ERANGE, double pwd_buff's size.
141*0Sstevel@tonic-gate  *
142*0Sstevel@tonic-gate  * This may be called even when auditing is off.
143*0Sstevel@tonic-gate  */
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate #define	NAFLAG_LEN 512
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate static int
148*0Sstevel@tonic-gate adt_get_mask_from_user(uid_t uid, au_mask_t *mask)
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate 	struct passwd	pwd;
151*0Sstevel@tonic-gate 	char		pwd_buff[NSS_BUFSIZ];
152*0Sstevel@tonic-gate 	char		naflag_buf[NAFLAG_LEN];
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	if (auditstate == AUC_DISABLED) {
155*0Sstevel@tonic-gate 		mask->am_success = 0;
156*0Sstevel@tonic-gate 		mask->am_failure = 0;
157*0Sstevel@tonic-gate 	} else if (uid >= 0) {
158*0Sstevel@tonic-gate 		if (getpwuid_r(uid, &pwd, pwd_buff, NSS_BUFSIZ) == NULL) {
159*0Sstevel@tonic-gate 			/*
160*0Sstevel@tonic-gate 			 * getpwuid_r returns NULL without setting
161*0Sstevel@tonic-gate 			 * errno if the user does not exist; only
162*0Sstevel@tonic-gate 			 * if the input is the wrong length does it
163*0Sstevel@tonic-gate 			 * set errno.
164*0Sstevel@tonic-gate 			 */
165*0Sstevel@tonic-gate 			if (errno != ERANGE)
166*0Sstevel@tonic-gate 				errno = EINVAL;
167*0Sstevel@tonic-gate 			return (-1);
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 		if (au_user_mask(pwd.pw_name, mask)) {
170*0Sstevel@tonic-gate 			errno = EFAULT; /* undetermined failure */
171*0Sstevel@tonic-gate 			return (-1);
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 	} else if (getacna(naflag_buf, NAFLAG_LEN - 1) == 0) {
174*0Sstevel@tonic-gate 		if (getauditflagsbin(naflag_buf, mask))
175*0Sstevel@tonic-gate 			return (-1);
176*0Sstevel@tonic-gate 	} else {
177*0Sstevel@tonic-gate 		return (-1);
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate 	return (0);
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate /*
183*0Sstevel@tonic-gate  * adt_get_unique_id -- generate a hopefully unique 32 bit value
184*0Sstevel@tonic-gate  *
185*0Sstevel@tonic-gate  * there will be a follow up to replace this with the use of /dev/random
186*0Sstevel@tonic-gate  *
187*0Sstevel@tonic-gate  * An MD5 hash is taken on a buffer of
188*0Sstevel@tonic-gate  *     hostname . audit id . unix time . pid . count
189*0Sstevel@tonic-gate  *
190*0Sstevel@tonic-gate  * "count = noise++;" is subject to a race condition but I don't
191*0Sstevel@tonic-gate  * see a need to put a lock around it.
192*0Sstevel@tonic-gate  */
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate static au_id_t
195*0Sstevel@tonic-gate adt_get_unique_id(uid_t uid)
196*0Sstevel@tonic-gate {
197*0Sstevel@tonic-gate 	char		hostname[MAXHOSTNAMELEN];
198*0Sstevel@tonic-gate 	union {
199*0Sstevel@tonic-gate 		au_id_t		v[4];
200*0Sstevel@tonic-gate 		unsigned char	obuff[128/8];
201*0Sstevel@tonic-gate 	} output;
202*0Sstevel@tonic-gate 	MD5_CTX	context;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	static int	noise = 0;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	int		count = noise++;
207*0Sstevel@tonic-gate 	time_t		timebits = time(NULL);
208*0Sstevel@tonic-gate 	pid_t		pidbits = getpid();
209*0Sstevel@tonic-gate 	au_id_t		retval = 0;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	if (gethostname(hostname, MAXHOSTNAMELEN)) {
212*0Sstevel@tonic-gate 		adt_write_syslog("gethostname call failed", errno);
213*0Sstevel@tonic-gate 		(void) strncpy(hostname, "invalidHostName", MAXHOSTNAMELEN);
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	while (retval == 0) {  /* 0 is the only invalid result */
217*0Sstevel@tonic-gate 		MD5Init(&context);
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *)hostname,
220*0Sstevel@tonic-gate 		    (unsigned int) strlen((const char *)hostname));
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &uid, sizeof (uid_t));
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 		MD5Update(&context,
225*0Sstevel@tonic-gate 		    (unsigned char *) &timebits, sizeof (time_t));
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &pidbits,
228*0Sstevel@tonic-gate 		    sizeof (pid_t));
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &(count), sizeof (int));
231*0Sstevel@tonic-gate 		MD5Final(output.obuff, &context);
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 		retval = output.v[count % 4];
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 	return (retval);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate  * the following "port" function deals with the following issues:
240*0Sstevel@tonic-gate  *
241*0Sstevel@tonic-gate  * 1    the kernel and ucred deal with a dev_t as a 64 bit value made
242*0Sstevel@tonic-gate  *      up from a 32 bit major and 32 bit minor.
243*0Sstevel@tonic-gate  * 2    User space deals with a dev_t as either the above 64 bit value
244*0Sstevel@tonic-gate  *      or a 32 bit value made from a 14 bit major and an 18 bit minor.
245*0Sstevel@tonic-gate  * 3    The various audit interfaces (except ucred) pass the 32 or
246*0Sstevel@tonic-gate  *      64 bit version depending the architecture of the userspace
247*0Sstevel@tonic-gate  *      application.  If you get a port value from ucred and pass it
248*0Sstevel@tonic-gate  *      to the kernel via auditon(), it must be squeezed into a 32
249*0Sstevel@tonic-gate  *      bit value because the kernel knows the userspace app's bit
250*0Sstevel@tonic-gate  *      size.
251*0Sstevel@tonic-gate  *
252*0Sstevel@tonic-gate  * The internal state structure for adt (adt_internal_state_t) uses
253*0Sstevel@tonic-gate  * dev_t, so adt converts data from ucred to fit.  The import/export
254*0Sstevel@tonic-gate  * functions, however, can't know if they are importing/exporting
255*0Sstevel@tonic-gate  * from 64 or 32 bit applications, so they always send 64 bits and
256*0Sstevel@tonic-gate  * the 32 bit end(s) are responsible to convert 32 -> 64 -> 32 as
257*0Sstevel@tonic-gate  * appropriate.
258*0Sstevel@tonic-gate  */
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate /*
261*0Sstevel@tonic-gate  * adt_cpy_tid() -- if lib is 64 bit, just copy it (dev_t and port are
262*0Sstevel@tonic-gate  * both 64 bits).  If lib is 32 bits, squeeze the two-int port into
263*0Sstevel@tonic-gate  * a 32 bit dev_t.  A port fits in the "minor" part of au_port_t,
264*0Sstevel@tonic-gate  * so it isn't broken up into pieces.  (When it goes to the kernel
265*0Sstevel@tonic-gate  * and back, however, it will have been split into major/minor
266*0Sstevel@tonic-gate  * pieces.)
267*0Sstevel@tonic-gate  */
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate static void
270*0Sstevel@tonic-gate adt_cpy_tid(au_tid_addr_t *dest, const au_tid64_addr_t *src)
271*0Sstevel@tonic-gate {
272*0Sstevel@tonic-gate #ifdef _LP64
273*0Sstevel@tonic-gate 	(void) memcpy(dest, src, sizeof (au_tid_addr_t));
274*0Sstevel@tonic-gate #else
275*0Sstevel@tonic-gate 	dest->at_type = src->at_type;
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	dest->at_port  = src->at_port.at_minor & MAXMIN32;
278*0Sstevel@tonic-gate 	dest->at_port |= (src->at_port.at_major & MAXMAJ32) <<
279*0Sstevel@tonic-gate 	    NBITSMINOR32;
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	(void) memcpy(dest->at_addr, src->at_addr, 4 * sizeof (uint32_t));
282*0Sstevel@tonic-gate #endif
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate /*
286*0Sstevel@tonic-gate  * adt_start_session -- create interface handle, create context
287*0Sstevel@tonic-gate  *
288*0Sstevel@tonic-gate  * The imported_state input is normally NULL, if not, it represents
289*0Sstevel@tonic-gate  * a continued session; its values obviate the need for a subsequent
290*0Sstevel@tonic-gate  * call to adt_set_user().
291*0Sstevel@tonic-gate  *
292*0Sstevel@tonic-gate  * The flag ADT_USE_PROC_DATA is used to decide how to set the initial
293*0Sstevel@tonic-gate  * state of the session.  If 0, the session is "no audit" until a call
294*0Sstevel@tonic-gate  * to adt_set_user; if 1, the session is built from the process audit
295*0Sstevel@tonic-gate  * characteristics obtained from the kernel.  If imported_state is
296*0Sstevel@tonic-gate  * not NULL, the resulting audit mask is an OR of the current process
297*0Sstevel@tonic-gate  * audit mask and that passed in.
298*0Sstevel@tonic-gate  *
299*0Sstevel@tonic-gate  * The basic model is that the caller can use the pointer returned
300*0Sstevel@tonic-gate  * by adt_start_session whether or not auditing is enabled or an
301*0Sstevel@tonic-gate  * error was returned.  The functions that take the session handle
302*0Sstevel@tonic-gate  * as input generally return without doing anything if auditing is
303*0Sstevel@tonic-gate  * disabled.
304*0Sstevel@tonic-gate  */
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate int
307*0Sstevel@tonic-gate adt_start_session(adt_session_data_t **new_session,
308*0Sstevel@tonic-gate     const adt_export_data_t *imported_state,
309*0Sstevel@tonic-gate     adt_session_flags_t flags)
310*0Sstevel@tonic-gate {
311*0Sstevel@tonic-gate 	adt_internal_state_t	*state;
312*0Sstevel@tonic-gate 	adt_session_flags_t	flgmask = ADT_FLAGS_ALL;
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	*new_session = NULL;	/* assume failure */
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	/* ensure that auditstate is set */
317*0Sstevel@tonic-gate 	(void) adt_audit_enabled();
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	if ((flags & ~flgmask) != 0) {
320*0Sstevel@tonic-gate 		errno = EINVAL;
321*0Sstevel@tonic-gate 		goto return_err;
322*0Sstevel@tonic-gate 	}
323*0Sstevel@tonic-gate 	state = calloc(1, sizeof (adt_internal_state_t));
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	if (state == NULL)
326*0Sstevel@tonic-gate 		goto return_err;
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	if (adt_init(state, flags & ADT_USE_PROC_DATA) != 0)
329*0Sstevel@tonic-gate 		goto return_err_free;    /* errno from adt_init() */
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	/*
332*0Sstevel@tonic-gate 	 * The imported state overwrites the initial state if the
333*0Sstevel@tonic-gate 	 * imported state represents a valid audit trail
334*0Sstevel@tonic-gate 	 */
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	if (imported_state != NULL) {
337*0Sstevel@tonic-gate 		if (adt_import(state, imported_state) != 0)
338*0Sstevel@tonic-gate 			goto return_err_free;
339*0Sstevel@tonic-gate 	}
340*0Sstevel@tonic-gate 	state->as_flags = flags;
341*0Sstevel@tonic-gate 	DPRINTF(("(%d) Starting session id = %08X\n",
342*0Sstevel@tonic-gate 	    getpid(), state->as_info.ai_asid));
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	if (state->as_audit_enabled)
345*0Sstevel@tonic-gate 		*new_session = (adt_session_data_t *)state;
346*0Sstevel@tonic-gate 	else
347*0Sstevel@tonic-gate 		free(state);
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	return (0);
350*0Sstevel@tonic-gate return_err_free:
351*0Sstevel@tonic-gate 	free(state);
352*0Sstevel@tonic-gate return_err:
353*0Sstevel@tonic-gate 	adt_write_syslog("audit session create failed", errno);
354*0Sstevel@tonic-gate 	return (-1);
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate /*
358*0Sstevel@tonic-gate  * adt_get_asid() and adt_set_asid()
359*0Sstevel@tonic-gate  *
360*0Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
361*0Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
362*0Sstevel@tonic-gate  * adt_proccess_attr()
363*0Sstevel@tonic-gate  *
364*0Sstevel@tonic-gate  * neither of these are intended for general use and will likely
365*0Sstevel@tonic-gate  * remain private interfaces for a long time.  Forever is a long
366*0Sstevel@tonic-gate  * time.  In the case of adt_set_asid(), you should have a very,
367*0Sstevel@tonic-gate  * very good reason for setting your own session id.  The process
368*0Sstevel@tonic-gate  * audit characteristics are not changed by put, use adt_set_proc().
369*0Sstevel@tonic-gate  *
370*0Sstevel@tonic-gate  * These are "volatile" (more changable than "evolving") and will
371*0Sstevel@tonic-gate  * probably change in the S10 period.
372*0Sstevel@tonic-gate  */
373*0Sstevel@tonic-gate void
374*0Sstevel@tonic-gate adt_get_asid(const adt_session_data_t *session_data, au_asid_t *asid)
375*0Sstevel@tonic-gate {
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	if (session_data == NULL) {
378*0Sstevel@tonic-gate 		*asid = 0;
379*0Sstevel@tonic-gate 	} else {
380*0Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
381*0Sstevel@tonic-gate 		    ADT_VALID);
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 		*asid = ((adt_internal_state_t *)session_data)->as_info.ai_asid;
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate }
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate void
388*0Sstevel@tonic-gate adt_set_asid(const adt_session_data_t *session_data,
389*0Sstevel@tonic-gate     const au_asid_t session_id)
390*0Sstevel@tonic-gate {
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	if (session_data != NULL) {
393*0Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
394*0Sstevel@tonic-gate 		    ADT_VALID);
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
397*0Sstevel@tonic-gate 		    ADT_HAVE_ASID;
398*0Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_asid =
399*0Sstevel@tonic-gate 		    session_id;
400*0Sstevel@tonic-gate 	}
401*0Sstevel@tonic-gate }
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate /*
404*0Sstevel@tonic-gate  * adt_get_auid() and adt_set_auid()
405*0Sstevel@tonic-gate  *
406*0Sstevel@tonic-gate  * neither of these are intended for general use and will likely
407*0Sstevel@tonic-gate  * remain private interfaces for a long time.  Forever is a long
408*0Sstevel@tonic-gate  * time.  In the case of adt_set_auid(), you should have a very,
409*0Sstevel@tonic-gate  * very good reason for setting your own audit id.  The process
410*0Sstevel@tonic-gate  * audit characteristics are not changed by put, use adt_set_proc().
411*0Sstevel@tonic-gate  */
412*0Sstevel@tonic-gate void
413*0Sstevel@tonic-gate adt_get_auid(const adt_session_data_t *session_data, au_id_t *auid)
414*0Sstevel@tonic-gate {
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	if (session_data == NULL) {
417*0Sstevel@tonic-gate 		*auid = AU_NOAUDITID;
418*0Sstevel@tonic-gate 	} else {
419*0Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
420*0Sstevel@tonic-gate 		    ADT_VALID);
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 		*auid = ((adt_internal_state_t *)session_data)->as_info.ai_auid;
423*0Sstevel@tonic-gate 	}
424*0Sstevel@tonic-gate }
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate void
427*0Sstevel@tonic-gate adt_set_auid(const adt_session_data_t *session_data, const au_id_t audit_id)
428*0Sstevel@tonic-gate {
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	if (session_data != NULL) {
431*0Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
432*0Sstevel@tonic-gate 		    ADT_VALID);
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
435*0Sstevel@tonic-gate 		    ADT_HAVE_AUID;
436*0Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_auid =
437*0Sstevel@tonic-gate 		    audit_id;
438*0Sstevel@tonic-gate 	}
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate /*
442*0Sstevel@tonic-gate  * adt_get_termid(), adt_set_termid()
443*0Sstevel@tonic-gate  *
444*0Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
445*0Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
446*0Sstevel@tonic-gate  * adt_proccess_attr()
447*0Sstevel@tonic-gate  *
448*0Sstevel@tonic-gate  * The process  audit characteristics are not changed by put, use
449*0Sstevel@tonic-gate  * adt_set_proc().
450*0Sstevel@tonic-gate  */
451*0Sstevel@tonic-gate void
452*0Sstevel@tonic-gate adt_get_termid(const adt_session_data_t *session_data, au_tid_addr_t *termid)
453*0Sstevel@tonic-gate {
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	if (session_data == NULL) {
456*0Sstevel@tonic-gate 		(void) memset(termid, 0, sizeof (au_tid_addr_t));
457*0Sstevel@tonic-gate 		termid->at_type = AU_IPv4;
458*0Sstevel@tonic-gate 	} else {
459*0Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
460*0Sstevel@tonic-gate 		    ADT_VALID);
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 		*termid =
463*0Sstevel@tonic-gate 		    ((adt_internal_state_t *)session_data)->as_info.ai_termid;
464*0Sstevel@tonic-gate 	}
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate void
468*0Sstevel@tonic-gate adt_set_termid(const adt_session_data_t *session_data,
469*0Sstevel@tonic-gate     const au_tid_addr_t *termid)
470*0Sstevel@tonic-gate {
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 	if (session_data != NULL) {
473*0Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
474*0Sstevel@tonic-gate 		    ADT_VALID);
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_termid =
477*0Sstevel@tonic-gate 			*termid;
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
480*0Sstevel@tonic-gate 			ADT_HAVE_TID;
481*0Sstevel@tonic-gate 	}
482*0Sstevel@tonic-gate }
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate /*
485*0Sstevel@tonic-gate  * adt_get_mask(), adt_set_mask()
486*0Sstevel@tonic-gate  *
487*0Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
488*0Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
489*0Sstevel@tonic-gate  * adt_proccess_attr()
490*0Sstevel@tonic-gate  *
491*0Sstevel@tonic-gate  * The process  audit characteristics are not changed by put, use
492*0Sstevel@tonic-gate  * adt_set_proc().
493*0Sstevel@tonic-gate  */
494*0Sstevel@tonic-gate void
495*0Sstevel@tonic-gate adt_get_mask(const adt_session_data_t *session_data, au_mask_t *mask)
496*0Sstevel@tonic-gate {
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	if (session_data == NULL) {
499*0Sstevel@tonic-gate 		mask->am_success = 0;
500*0Sstevel@tonic-gate 		mask->am_failure = 0;
501*0Sstevel@tonic-gate 	} else {
502*0Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
503*0Sstevel@tonic-gate 		    ADT_VALID);
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 		*mask = ((adt_internal_state_t *)session_data)->as_info.ai_mask;
506*0Sstevel@tonic-gate 	}
507*0Sstevel@tonic-gate }
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate void
510*0Sstevel@tonic-gate adt_set_mask(const adt_session_data_t *session_data, const au_mask_t *mask)
511*0Sstevel@tonic-gate {
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	if (session_data != NULL) {
514*0Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
515*0Sstevel@tonic-gate 		    ADT_VALID);
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_mask = *mask;
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
520*0Sstevel@tonic-gate 		    ADT_HAVE_MASK;
521*0Sstevel@tonic-gate 	}
522*0Sstevel@tonic-gate }
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate /*
525*0Sstevel@tonic-gate  * helpers for adt_load_termid
526*0Sstevel@tonic-gate  */
527*0Sstevel@tonic-gate static void
528*0Sstevel@tonic-gate adt_do_ipv6_address(struct sockaddr_in6 *peer, struct sockaddr_in6 *sock,
529*0Sstevel@tonic-gate     au_tid_addr_t *termid)
530*0Sstevel@tonic-gate {
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	termid->at_port = ((peer->sin6_port<<16) | (sock->sin6_port));
533*0Sstevel@tonic-gate 	termid->at_type = AU_IPv6;
534*0Sstevel@tonic-gate 	(void) memcpy(termid->at_addr, &peer->sin6_addr, 4 * sizeof (uint_t));
535*0Sstevel@tonic-gate }
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate static void
538*0Sstevel@tonic-gate adt_do_ipv4_address(struct sockaddr_in *peer, struct sockaddr_in *sock,
539*0Sstevel@tonic-gate     au_tid_addr_t *termid)
540*0Sstevel@tonic-gate {
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	termid->at_port = ((peer->sin_port<<16) | (sock->sin_port));
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	termid->at_type = AU_IPv4;
545*0Sstevel@tonic-gate 	termid->at_addr[0] = (uint32_t)peer->sin_addr.s_addr;
546*0Sstevel@tonic-gate 	(void) memset(&(termid->at_addr[1]), 0, 3 * sizeof (uint_t));
547*0Sstevel@tonic-gate }
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate /*
550*0Sstevel@tonic-gate  * adt_load_termid:  convenience function; inputs file handle and
551*0Sstevel@tonic-gate  * outputs an au_tid_addr struct.
552*0Sstevel@tonic-gate  *
553*0Sstevel@tonic-gate  * This code was stolen from audit_settid.c; it differs from audit_settid()
554*0Sstevel@tonic-gate  * in that it does not write the terminal id to the process.
555*0Sstevel@tonic-gate  */
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate int
558*0Sstevel@tonic-gate adt_load_termid(int fd, adt_termid_t **termid)
559*0Sstevel@tonic-gate {
560*0Sstevel@tonic-gate 	au_tid_addr_t		*p_term;
561*0Sstevel@tonic-gate 	struct sockaddr_in6	peer;
562*0Sstevel@tonic-gate 	struct sockaddr_in6	sock;
563*0Sstevel@tonic-gate 	int			peerlen = sizeof (peer);
564*0Sstevel@tonic-gate 	int			socklen = sizeof (sock);
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	*termid = NULL;
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	/* get peer name if its a socket, else assume local terminal */
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	if (getpeername(fd, (struct sockaddr *)&peer, (socklen_t *)&peerlen)
571*0Sstevel@tonic-gate 	    < 0) {
572*0Sstevel@tonic-gate 		if (errno == ENOTSOCK)
573*0Sstevel@tonic-gate 			return (adt_load_hostname(NULL, termid));
574*0Sstevel@tonic-gate 		goto return_err;
575*0Sstevel@tonic-gate 	}
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL)
578*0Sstevel@tonic-gate 		goto return_err;
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	/* get sock name */
581*0Sstevel@tonic-gate 	if (getsockname(fd, (struct sockaddr *)&sock,
582*0Sstevel@tonic-gate 	    (socklen_t *)&socklen) < 0)
583*0Sstevel@tonic-gate 		goto return_err_free;
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	if (peer.sin6_family == AF_INET6) {
586*0Sstevel@tonic-gate 		adt_do_ipv6_address(&peer, &sock, p_term);
587*0Sstevel@tonic-gate 	} else {
588*0Sstevel@tonic-gate 		adt_do_ipv4_address((struct sockaddr_in *)&peer,
589*0Sstevel@tonic-gate 		    (struct sockaddr_in *)&sock, p_term);
590*0Sstevel@tonic-gate 	}
591*0Sstevel@tonic-gate 	*termid = (adt_termid_t *)p_term;
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 	return (0);
594*0Sstevel@tonic-gate 
595*0Sstevel@tonic-gate return_err_free:
596*0Sstevel@tonic-gate 	free(p_term);
597*0Sstevel@tonic-gate return_err:
598*0Sstevel@tonic-gate 	return (-1);
599*0Sstevel@tonic-gate }
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate static boolean_t
602*0Sstevel@tonic-gate adt_have_termid(au_tid_addr_t *dest)
603*0Sstevel@tonic-gate {
604*0Sstevel@tonic-gate 	struct auditinfo_addr	audit_data;
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	if (getaudit_addr(&audit_data, sizeof (audit_data)) < 0) {
607*0Sstevel@tonic-gate 		adt_write_syslog("getaudit failed", errno);
608*0Sstevel@tonic-gate 		return (B_FALSE);
609*0Sstevel@tonic-gate 	}
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 	if ((audit_data.ai_termid.at_type == 0) ||
612*0Sstevel@tonic-gate 	    (audit_data.ai_termid.at_addr[0] |
613*0Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[1]  |
614*0Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[2]  |
615*0Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[3]) == 0)
616*0Sstevel@tonic-gate 		return (B_FALSE);
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	(void) memcpy(dest, &(audit_data.ai_termid),
619*0Sstevel@tonic-gate 	    sizeof (au_tid_addr_t));
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	return (B_TRUE);
622*0Sstevel@tonic-gate }
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate static int
625*0Sstevel@tonic-gate adt_get_hostIP(const char *hostname, au_tid_addr_t *p_term)
626*0Sstevel@tonic-gate {
627*0Sstevel@tonic-gate 	struct addrinfo	*ai;
628*0Sstevel@tonic-gate 	void		*p;
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 	if (getaddrinfo(hostname, NULL, NULL, &ai) != 0)
631*0Sstevel@tonic-gate 		return (-1);
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate 	switch (ai->ai_family) {
634*0Sstevel@tonic-gate 		case AF_INET:
635*0Sstevel@tonic-gate 			/* LINTED */
636*0Sstevel@tonic-gate 			p = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
637*0Sstevel@tonic-gate 			(void) memcpy(p_term->at_addr, p,
638*0Sstevel@tonic-gate 			    sizeof (((struct sockaddr_in *)NULL)->sin_addr));
639*0Sstevel@tonic-gate 			p_term->at_type = AU_IPv4;
640*0Sstevel@tonic-gate 			break;
641*0Sstevel@tonic-gate 		case AF_INET6:
642*0Sstevel@tonic-gate 			/* LINTED */
643*0Sstevel@tonic-gate 			p = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
644*0Sstevel@tonic-gate 			(void) memcpy(p_term->at_addr, p,
645*0Sstevel@tonic-gate 			    sizeof (((struct sockaddr_in6 *)NULL)->sin6_addr));
646*0Sstevel@tonic-gate 			p_term->at_type = AU_IPv6;
647*0Sstevel@tonic-gate 			break;
648*0Sstevel@tonic-gate 		default:
649*0Sstevel@tonic-gate 			return (-1);
650*0Sstevel@tonic-gate 	}
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	freeaddrinfo(ai);
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 	return (0);
655*0Sstevel@tonic-gate }
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate /*
658*0Sstevel@tonic-gate  * adt_load_hostname() is called when the caller does not have a file
659*0Sstevel@tonic-gate  * handle that gives access to the socket info or any other way to
660*0Sstevel@tonic-gate  * pass in both port and ip address.  The hostname input is ignored if
661*0Sstevel@tonic-gate  * the terminal id has already been set; instead it returns the
662*0Sstevel@tonic-gate  * existing terminal id.
663*0Sstevel@tonic-gate  *
664*0Sstevel@tonic-gate  * If audit is off and the hostname lookup fails, no error is
665*0Sstevel@tonic-gate  * returned, since an error may be interpreted by the caller
666*0Sstevel@tonic-gate  * as grounds for denying a login.  Otherwise the caller would
667*0Sstevel@tonic-gate  * need to be aware of the audit state.
668*0Sstevel@tonic-gate  */
669*0Sstevel@tonic-gate int
670*0Sstevel@tonic-gate adt_load_hostname(const char *hostname, adt_termid_t **termid)
671*0Sstevel@tonic-gate {
672*0Sstevel@tonic-gate 	char		localhost[ADT_STRING_MAX + 1];
673*0Sstevel@tonic-gate 	au_tid_addr_t	*p_term;
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 	*termid = NULL;
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 	if (!adt_audit_enabled())
678*0Sstevel@tonic-gate 		return (0);
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL)
681*0Sstevel@tonic-gate 		goto return_err;
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate 	if (adt_have_termid(p_term)) {
684*0Sstevel@tonic-gate 		*termid = (adt_termid_t *)p_term;
685*0Sstevel@tonic-gate 		return (0);
686*0Sstevel@tonic-gate 	}
687*0Sstevel@tonic-gate 	p_term->at_port = 0;
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 	if (hostname == NULL || *hostname == '\0') {
690*0Sstevel@tonic-gate 		(void) sysinfo(SI_HOSTNAME, localhost, ADT_STRING_MAX);
691*0Sstevel@tonic-gate 		hostname = localhost;
692*0Sstevel@tonic-gate 	}
693*0Sstevel@tonic-gate 	if (adt_get_hostIP(hostname, p_term))
694*0Sstevel@tonic-gate 		goto return_err_free;
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate 	*termid = (adt_termid_t *)p_term;
697*0Sstevel@tonic-gate 	return (0);
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate return_err_free:
700*0Sstevel@tonic-gate 	free(p_term);
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate return_err:
703*0Sstevel@tonic-gate 	if ((auditstate == AUC_DISABLED) ||
704*0Sstevel@tonic-gate 	    (auditstate == AUC_NOAUDIT))
705*0Sstevel@tonic-gate 		return (0);
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	return (-1);
708*0Sstevel@tonic-gate }
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate /*
711*0Sstevel@tonic-gate  * adt_load_ttyname() is called when the caller does not have a file
712*0Sstevel@tonic-gate  * handle that gives access to the local terminal or any other way
713*0Sstevel@tonic-gate  * of determining the device id.  The ttyname input is ignored if
714*0Sstevel@tonic-gate  * the terminal id has already been set; instead it returns the
715*0Sstevel@tonic-gate  * existing terminal id.
716*0Sstevel@tonic-gate  *
717*0Sstevel@tonic-gate  * If audit is off and the ttyname lookup fails, no error is
718*0Sstevel@tonic-gate  * returned, since an error may be interpreted by the caller
719*0Sstevel@tonic-gate  * as grounds for denying a login.  Otherwise the caller would
720*0Sstevel@tonic-gate  * need to be aware of the audit state.
721*0Sstevel@tonic-gate  */
722*0Sstevel@tonic-gate int
723*0Sstevel@tonic-gate adt_load_ttyname(const char *ttyname, adt_termid_t **termid)
724*0Sstevel@tonic-gate {
725*0Sstevel@tonic-gate 	char		localhost[ADT_STRING_MAX + 1];
726*0Sstevel@tonic-gate 	au_tid_addr_t	*p_term;
727*0Sstevel@tonic-gate 	struct stat	stat_buf;
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 	*termid = NULL;
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 	if (!adt_audit_enabled())
732*0Sstevel@tonic-gate 		return (0);
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL)
735*0Sstevel@tonic-gate 		goto return_err;
736*0Sstevel@tonic-gate 
737*0Sstevel@tonic-gate 	if (adt_have_termid(p_term)) {
738*0Sstevel@tonic-gate 		*termid = (adt_termid_t *)p_term;
739*0Sstevel@tonic-gate 		return (0);
740*0Sstevel@tonic-gate 	}
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	p_term->at_port = 0;
743*0Sstevel@tonic-gate 
744*0Sstevel@tonic-gate 	if (sysinfo(SI_HOSTNAME, localhost, ADT_STRING_MAX) < 0)
745*0Sstevel@tonic-gate 		goto return_err_free; /* errno from sysinfo */
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate 	if (ttyname != NULL) {
748*0Sstevel@tonic-gate 		if (stat(ttyname, &stat_buf) < 0)
749*0Sstevel@tonic-gate 			goto return_err_free;
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 		p_term->at_port = stat_buf.st_rdev;
752*0Sstevel@tonic-gate 	}
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 	if (adt_get_hostIP(localhost, p_term))
755*0Sstevel@tonic-gate 		goto return_err_free;
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	*termid = (adt_termid_t *)p_term;
758*0Sstevel@tonic-gate 	return (0);
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate return_err_free:
761*0Sstevel@tonic-gate 	free(p_term);
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate return_err:
764*0Sstevel@tonic-gate 	if ((auditstate == AUC_DISABLED) ||
765*0Sstevel@tonic-gate 	    (auditstate == AUC_NOAUDIT))
766*0Sstevel@tonic-gate 		return (0);
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	return (-1);
769*0Sstevel@tonic-gate }
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate /*
772*0Sstevel@tonic-gate  * adt_get_session_id returns a stringified representation of
773*0Sstevel@tonic-gate  * the audit session id.  See also adt_get_asid() for how to
774*0Sstevel@tonic-gate  * get the unexpurgated version.  No guarantees as to how long
775*0Sstevel@tonic-gate  * the returned string will be or its general form; hex for now.
776*0Sstevel@tonic-gate  *
777*0Sstevel@tonic-gate  * An empty string is returned if auditing is off; length = 1
778*0Sstevel@tonic-gate  * and the pointer is valid.
779*0Sstevel@tonic-gate  *
780*0Sstevel@tonic-gate  * returns strlen + 1 if buffer is valid; else 0 and errno.
781*0Sstevel@tonic-gate  */
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate size_t
784*0Sstevel@tonic-gate adt_get_session_id(const adt_session_data_t *session_data, char **buff)
785*0Sstevel@tonic-gate {
786*0Sstevel@tonic-gate 	au_asid_t	session_id;
787*0Sstevel@tonic-gate 	size_t		length;
788*0Sstevel@tonic-gate 	/*
789*0Sstevel@tonic-gate 	 * output is 0x followed by
790*0Sstevel@tonic-gate 	 * two characters per byte
791*0Sstevel@tonic-gate 	 * plus terminator,
792*0Sstevel@tonic-gate 	 * except leading 0's are suppressed, so a few bytes may
793*0Sstevel@tonic-gate 	 * be unused.
794*0Sstevel@tonic-gate 	 */
795*0Sstevel@tonic-gate 	length = 2 + (2 * sizeof (session_id)) + 1;
796*0Sstevel@tonic-gate 	*buff = malloc(length);
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	if (*buff == NULL) {
799*0Sstevel@tonic-gate 		length = 0;
800*0Sstevel@tonic-gate 		goto return_length;
801*0Sstevel@tonic-gate 	}
802*0Sstevel@tonic-gate 	if (session_data == NULL) { /* NULL is not an error */
803*0Sstevel@tonic-gate 		**buff = '\0';
804*0Sstevel@tonic-gate 		length = 1;
805*0Sstevel@tonic-gate 		goto return_length; /* empty string */
806*0Sstevel@tonic-gate 	}
807*0Sstevel@tonic-gate 	adt_get_asid(session_data, &session_id);
808*0Sstevel@tonic-gate 
809*0Sstevel@tonic-gate 	length = snprintf(*buff, length, "0x%X", (int)session_id);
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 	/* length < 1 is a bug: the session data type may have changed */
812*0Sstevel@tonic-gate 	assert(length > 0);
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate return_length:
815*0Sstevel@tonic-gate 	return (length);
816*0Sstevel@tonic-gate }
817*0Sstevel@tonic-gate 
818*0Sstevel@tonic-gate /*
819*0Sstevel@tonic-gate  * adt_end_session -- close handle, clear context
820*0Sstevel@tonic-gate  *
821*0Sstevel@tonic-gate  * if as_check is invalid, no harm, no foul, EXCEPT that this could
822*0Sstevel@tonic-gate  * be an attempt to free data already free'd, so output to syslog
823*0Sstevel@tonic-gate  * to help explain why the process cored dumped.
824*0Sstevel@tonic-gate  */
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate int
827*0Sstevel@tonic-gate adt_end_session(adt_session_data_t *session_data)
828*0Sstevel@tonic-gate {
829*0Sstevel@tonic-gate 	adt_internal_state_t	*state;
830*0Sstevel@tonic-gate 
831*0Sstevel@tonic-gate 	if (session_data != NULL) {
832*0Sstevel@tonic-gate 		state = (adt_internal_state_t *)session_data;
833*0Sstevel@tonic-gate 		if (state->as_check != ADT_VALID)
834*0Sstevel@tonic-gate 			adt_write_syslog("freeing invalid data", EINVAL);
835*0Sstevel@tonic-gate 		else {
836*0Sstevel@tonic-gate 			state->as_check = 0;
837*0Sstevel@tonic-gate 			free(session_data);
838*0Sstevel@tonic-gate 		}
839*0Sstevel@tonic-gate 	}
840*0Sstevel@tonic-gate 	/* no errors yet defined */
841*0Sstevel@tonic-gate 	return (0);
842*0Sstevel@tonic-gate }
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate /*
845*0Sstevel@tonic-gate  * adt_dup_session -- copy the session data
846*0Sstevel@tonic-gate  */
847*0Sstevel@tonic-gate 
848*0Sstevel@tonic-gate int
849*0Sstevel@tonic-gate adt_dup_session(const adt_session_data_t *source, adt_session_data_t **dest)
850*0Sstevel@tonic-gate {
851*0Sstevel@tonic-gate 	adt_internal_state_t	*source_state;
852*0Sstevel@tonic-gate 	adt_session_data_t	*dest_state = NULL;
853*0Sstevel@tonic-gate 	int			rc = 0;
854*0Sstevel@tonic-gate 
855*0Sstevel@tonic-gate 	if (source != NULL) {
856*0Sstevel@tonic-gate 		source_state = (adt_internal_state_t *)source;
857*0Sstevel@tonic-gate 		assert(source_state->as_check == ADT_VALID);
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 		dest_state = malloc(sizeof (adt_internal_state_t));
860*0Sstevel@tonic-gate 		if (dest_state == NULL) {
861*0Sstevel@tonic-gate 			rc = -1;
862*0Sstevel@tonic-gate 			goto return_rc;
863*0Sstevel@tonic-gate 		}
864*0Sstevel@tonic-gate 		(void) memcpy(dest_state, source,
865*0Sstevel@tonic-gate 		    sizeof (struct adt_internal_state));
866*0Sstevel@tonic-gate 	}
867*0Sstevel@tonic-gate return_rc:
868*0Sstevel@tonic-gate 	*dest = dest_state;
869*0Sstevel@tonic-gate 	return (rc);
870*0Sstevel@tonic-gate }
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate /*
873*0Sstevel@tonic-gate  * from_export_format()
874*0Sstevel@tonic-gate  * read from a network order buffer into struct adt_session_data
875*0Sstevel@tonic-gate  */
876*0Sstevel@tonic-gate static size_t
877*0Sstevel@tonic-gate adt_from_export_format(adt_internal_state_t *internal,
878*0Sstevel@tonic-gate     const adt_export_data_t *external)
879*0Sstevel@tonic-gate {
880*0Sstevel@tonic-gate 	struct export_header	head;
881*0Sstevel@tonic-gate 	struct export_link	link;
882*0Sstevel@tonic-gate 	adr_t			context;
883*0Sstevel@tonic-gate 	int32_t 		offset;
884*0Sstevel@tonic-gate 	int32_t 		length;
885*0Sstevel@tonic-gate 	int32_t 		version;
886*0Sstevel@tonic-gate 	char			*p = (char *)external;
887*0Sstevel@tonic-gate 
888*0Sstevel@tonic-gate 	adrm_start(&context, (char *)external);
889*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&head, 4);
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 	if ((internal->as_check = head.ax_check) != ADT_VALID) {
892*0Sstevel@tonic-gate 		errno = EINVAL;
893*0Sstevel@tonic-gate 		return (0);
894*0Sstevel@tonic-gate 	}
895*0Sstevel@tonic-gate 	offset = head.ax_link.ax_offset;
896*0Sstevel@tonic-gate 	version = head.ax_link.ax_version;
897*0Sstevel@tonic-gate 	length = head.ax_buffer_length;
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate 	/*
900*0Sstevel@tonic-gate 	 * adjust buffer pointer to the first data item (euid)
901*0Sstevel@tonic-gate 	 * if versions mismatch; otherwise it's ok as is.
902*0Sstevel@tonic-gate 	 */
903*0Sstevel@tonic-gate 	while (version != PROTOCOL_VERSION) {
904*0Sstevel@tonic-gate 		if (offset < 1)
905*0Sstevel@tonic-gate 			return (0);	/* failed to match version */
906*0Sstevel@tonic-gate 		p += offset;	/* point to next version # */
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 		if (p > (char *)external + length) {
909*0Sstevel@tonic-gate 			return (0);
910*0Sstevel@tonic-gate 		}
911*0Sstevel@tonic-gate 		adrm_start(&context, p);
912*0Sstevel@tonic-gate 		adrm_int32(&context, (int *)&link, 2);
913*0Sstevel@tonic-gate 		offset = link.ax_offset;
914*0Sstevel@tonic-gate 		version = link.ax_version;
915*0Sstevel@tonic-gate 		assert(version != 0);
916*0Sstevel@tonic-gate 	}
917*0Sstevel@tonic-gate 	if (p == (char *)external)
918*0Sstevel@tonic-gate 		adrm_start(&context, (char *)(p + sizeof (head)));
919*0Sstevel@tonic-gate 	else
920*0Sstevel@tonic-gate 		adrm_start(&context, (char *)(p + sizeof (link)));
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_euid), 1);
923*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_ruid), 1);
924*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_egid), 1);
925*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_rgid), 1);
926*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
927*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_mask.am_success), 2);
928*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_termid.at_port), 1);
929*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_termid.at_type), 1);
930*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_termid.at_addr[0]),
931*0Sstevel@tonic-gate 	    4);
932*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
933*0Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_audit_enabled), 1);
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate 	/* ax_size_of_tsol_data intentionally ignored */
936*0Sstevel@tonic-gate 
937*0Sstevel@tonic-gate 	return (sizeof (struct adt_export_data));
938*0Sstevel@tonic-gate }
939*0Sstevel@tonic-gate 
940*0Sstevel@tonic-gate /*
941*0Sstevel@tonic-gate  * to_export_format
942*0Sstevel@tonic-gate  * read from struct adt_session_data into a network order buffer.
943*0Sstevel@tonic-gate  *
944*0Sstevel@tonic-gate  * (network order 'cause this data may be shared with a remote host.)
945*0Sstevel@tonic-gate  */
946*0Sstevel@tonic-gate 
947*0Sstevel@tonic-gate static size_t
948*0Sstevel@tonic-gate adt_to_export_format(adt_export_data_t *external,
949*0Sstevel@tonic-gate     adt_internal_state_t *internal)
950*0Sstevel@tonic-gate {
951*0Sstevel@tonic-gate 	struct export_header	head;
952*0Sstevel@tonic-gate 	struct export_link	tail;
953*0Sstevel@tonic-gate 	adr_t			context;
954*0Sstevel@tonic-gate 	size_t			tsol_size = 0;
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate 	adrm_start(&context, (char *)external);
957*0Sstevel@tonic-gate 
958*0Sstevel@tonic-gate 	head.ax_check = ADT_VALID;
959*0Sstevel@tonic-gate 	head.ax_buffer_length = sizeof (struct adt_export_data);
960*0Sstevel@tonic-gate 	head.ax_link.ax_version = PROTOCOL_VERSION;
961*0Sstevel@tonic-gate 	head.ax_link.ax_offset = 0;
962*0Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&head, 4);
963*0Sstevel@tonic-gate 
964*0Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_euid), 1);
965*0Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
966*0Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_egid), 1);
967*0Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
968*0Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
969*0Sstevel@tonic-gate 	adrm_putint32(&context,
970*0Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_mask.am_success), 2);
971*0Sstevel@tonic-gate 	adrm_putint32(&context,
972*0Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_port), 1);
973*0Sstevel@tonic-gate 	adrm_putint32(&context,
974*0Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_type), 1);
975*0Sstevel@tonic-gate 	adrm_putint32(&context,
976*0Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
977*0Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
978*0Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_audit_enabled), 1);
979*0Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&tsol_size, 1);
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate 	/* terminator */
982*0Sstevel@tonic-gate 	tail.ax_version = 0; /* invalid version number */
983*0Sstevel@tonic-gate 	tail.ax_offset = 0;
984*0Sstevel@tonic-gate 
985*0Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&tail, 2);
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate 	return (sizeof (struct adt_export_data));
988*0Sstevel@tonic-gate }
989*0Sstevel@tonic-gate 
990*0Sstevel@tonic-gate /*
991*0Sstevel@tonic-gate  * adt_import_proc() is used by a server acting on behalf
992*0Sstevel@tonic-gate  * of a client which has connected via an ipc mechanism such as
993*0Sstevel@tonic-gate  * a door.
994*0Sstevel@tonic-gate  *
995*0Sstevel@tonic-gate  * Since the interface is via ucred, the info.ap_termid.port
996*0Sstevel@tonic-gate  * value is always the 64 bit version.  What is stored depends
997*0Sstevel@tonic-gate  * on how libbsm is compiled.
998*0Sstevel@tonic-gate  */
999*0Sstevel@tonic-gate size_t
1000*0Sstevel@tonic-gate adt_import_proc(pid_t pid, uid_t euid, gid_t egid, uid_t ruid,
1001*0Sstevel@tonic-gate     gid_t rgid, adt_export_data_t **external)
1002*0Sstevel@tonic-gate {
1003*0Sstevel@tonic-gate 	size_t			length = 0;
1004*0Sstevel@tonic-gate 	adt_internal_state_t	*state;
1005*0Sstevel@tonic-gate 	ucred_t			*ucred;
1006*0Sstevel@tonic-gate 	const au_tid64_addr_t	*tid;
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate 	state = calloc(1, sizeof (adt_internal_state_t));
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 	if (state == NULL)
1011*0Sstevel@tonic-gate 		goto return_length;
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 	if (adt_init(state, 0) != 0)
1014*0Sstevel@tonic-gate 		goto return_length_free;    /* errno from adt_init() */
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate 	/*
1017*0Sstevel@tonic-gate 	 * ucred_getauid() returns AU_NOAUDITID if audit is off, which
1018*0Sstevel@tonic-gate 	 * is the right answer for adt_import_proc().
1019*0Sstevel@tonic-gate 	 *
1020*0Sstevel@tonic-gate 	 * Create a local context as near as possible.
1021*0Sstevel@tonic-gate 	 */
1022*0Sstevel@tonic-gate 
1023*0Sstevel@tonic-gate 	ucred = ucred_get(pid);
1024*0Sstevel@tonic-gate 
1025*0Sstevel@tonic-gate 	if (ucred == NULL)
1026*0Sstevel@tonic-gate 		goto return_length_free;
1027*0Sstevel@tonic-gate 
1028*0Sstevel@tonic-gate 	state->as_ruid = ruid != ADT_NO_CHANGE ? ruid : ucred_getruid(ucred);
1029*0Sstevel@tonic-gate 	state->as_euid = euid != ADT_NO_CHANGE ? euid : ucred_geteuid(ucred);
1030*0Sstevel@tonic-gate 	state->as_rgid = rgid != ADT_NO_CHANGE ? rgid : ucred_getrgid(ucred);
1031*0Sstevel@tonic-gate 	state->as_egid = egid != ADT_NO_CHANGE ? egid : ucred_getegid(ucred);
1032*0Sstevel@tonic-gate 
1033*0Sstevel@tonic-gate 	state->as_info.ai_auid = ucred_getauid(ucred);
1034*0Sstevel@tonic-gate 
1035*0Sstevel@tonic-gate 	if (state->as_info.ai_auid == AU_NOAUDITID) {
1036*0Sstevel@tonic-gate 		state->as_info.ai_asid = adt_get_unique_id(ruid);
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate 		if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask)))
1039*0Sstevel@tonic-gate 			goto return_all_free;
1040*0Sstevel@tonic-gate 	} else {
1041*0Sstevel@tonic-gate 		const au_mask_t *mask = ucred_getamask(ucred);
1042*0Sstevel@tonic-gate 
1043*0Sstevel@tonic-gate 		if (mask != NULL)
1044*0Sstevel@tonic-gate 			state->as_info.ai_mask = *mask;
1045*0Sstevel@tonic-gate 		else
1046*0Sstevel@tonic-gate 			goto return_all_free;
1047*0Sstevel@tonic-gate 
1048*0Sstevel@tonic-gate 		state->as_info.ai_asid = ucred_getasid(ucred);
1049*0Sstevel@tonic-gate 	}
1050*0Sstevel@tonic-gate 
1051*0Sstevel@tonic-gate 	tid = ucred_getatid(ucred);
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate 	if (tid != NULL) {
1054*0Sstevel@tonic-gate 		adt_cpy_tid(&(state->as_info.ai_termid), tid);
1055*0Sstevel@tonic-gate 	} else {
1056*0Sstevel@tonic-gate 		(void) memset((void *)&(state->as_info.ai_termid), 0,
1057*0Sstevel@tonic-gate 		    sizeof (au_tid_addr_t));
1058*0Sstevel@tonic-gate 		state->as_info.ai_termid.at_type = AU_IPv4;
1059*0Sstevel@tonic-gate 	}
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate 	DPRINTF(("import_proc/asid = %X %u\n", state->as_info.ai_asid,
1062*0Sstevel@tonic-gate 	    state->as_info.ai_asid));
1063*0Sstevel@tonic-gate 
1064*0Sstevel@tonic-gate 	DPRINTF(("import_proc/masks = %X %X\n",
1065*0Sstevel@tonic-gate 	    state->as_info.ai_mask.am_success,
1066*0Sstevel@tonic-gate 	    state->as_info.ai_mask.am_failure));
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate 	*external = malloc(sizeof (adt_export_data_t));
1069*0Sstevel@tonic-gate 
1070*0Sstevel@tonic-gate 	if (*external == NULL)
1071*0Sstevel@tonic-gate 		goto return_all_free;
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate 	length = adt_to_export_format(*external, state);
1074*0Sstevel@tonic-gate 	/*
1075*0Sstevel@tonic-gate 	 * yes, state is supposed to be free'd for both pass and fail
1076*0Sstevel@tonic-gate 	 */
1077*0Sstevel@tonic-gate return_all_free:
1078*0Sstevel@tonic-gate 	ucred_free(ucred);
1079*0Sstevel@tonic-gate return_length_free:
1080*0Sstevel@tonic-gate 	free(state);
1081*0Sstevel@tonic-gate return_length:
1082*0Sstevel@tonic-gate 	return (length);
1083*0Sstevel@tonic-gate }
1084*0Sstevel@tonic-gate 
1085*0Sstevel@tonic-gate /*
1086*0Sstevel@tonic-gate  * adt_import() -- convert from network order to machine-specific order
1087*0Sstevel@tonic-gate  */
1088*0Sstevel@tonic-gate static int
1089*0Sstevel@tonic-gate adt_import(adt_internal_state_t *internal, const adt_export_data_t *external)
1090*0Sstevel@tonic-gate {
1091*0Sstevel@tonic-gate 	au_mask_t mask;
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 	/* save local audit enabled state */
1094*0Sstevel@tonic-gate 	int	local_audit_enabled = internal->as_audit_enabled;
1095*0Sstevel@tonic-gate 
1096*0Sstevel@tonic-gate 	if (adt_from_export_format(internal, external) < 1)
1097*0Sstevel@tonic-gate 		return (-1); /* errno from adt_from_export_format */
1098*0Sstevel@tonic-gate 
1099*0Sstevel@tonic-gate 	/*
1100*0Sstevel@tonic-gate 	 * If audit isn't enabled on the remote, they were unable
1101*0Sstevel@tonic-gate 	 * to generate the audit mask, so generate it based on
1102*0Sstevel@tonic-gate 	 * local configuration.  If the user id has changed, the
1103*0Sstevel@tonic-gate 	 * resulting mask may miss some subtleties that occurred
1104*0Sstevel@tonic-gate 	 * on the remote system.
1105*0Sstevel@tonic-gate 	 *
1106*0Sstevel@tonic-gate 	 * If the remote failed to generate a terminal id, it is not
1107*0Sstevel@tonic-gate 	 * recoverable.
1108*0Sstevel@tonic-gate 	 */
1109*0Sstevel@tonic-gate 
1110*0Sstevel@tonic-gate 	if (!internal->as_audit_enabled) {
1111*0Sstevel@tonic-gate 		if (adt_get_mask_from_user(internal->as_info.ai_auid,
1112*0Sstevel@tonic-gate 		    &(internal->as_info.ai_mask)))
1113*0Sstevel@tonic-gate 			return (-1);
1114*0Sstevel@tonic-gate 		if (internal->as_info.ai_auid != internal->as_ruid) {
1115*0Sstevel@tonic-gate 			if (adt_get_mask_from_user(internal->as_info.ai_auid,
1116*0Sstevel@tonic-gate 			    &mask))
1117*0Sstevel@tonic-gate 				return (-1);
1118*0Sstevel@tonic-gate 			internal->as_info.ai_mask.am_success |=
1119*0Sstevel@tonic-gate 			    mask.am_success;
1120*0Sstevel@tonic-gate 			internal->as_info.ai_mask.am_failure |=
1121*0Sstevel@tonic-gate 			    mask.am_failure;
1122*0Sstevel@tonic-gate 		}
1123*0Sstevel@tonic-gate 	}
1124*0Sstevel@tonic-gate 	internal->as_audit_enabled = local_audit_enabled;
1125*0Sstevel@tonic-gate 
1126*0Sstevel@tonic-gate 	DPRINTF(("(%d)imported asid = %X %u\n", getpid(),
1127*0Sstevel@tonic-gate 	    internal->as_info.ai_asid,
1128*0Sstevel@tonic-gate 	    internal->as_info.ai_asid));
1129*0Sstevel@tonic-gate 
1130*0Sstevel@tonic-gate 	internal->as_have_user_data = ADT_HAVE_ALL;
1131*0Sstevel@tonic-gate 
1132*0Sstevel@tonic-gate 	return (0);
1133*0Sstevel@tonic-gate }
1134*0Sstevel@tonic-gate 
1135*0Sstevel@tonic-gate /*
1136*0Sstevel@tonic-gate  * adt_export_session_data()
1137*0Sstevel@tonic-gate  * copies a adt_session_data struct into a network order buffer
1138*0Sstevel@tonic-gate  *
1139*0Sstevel@tonic-gate  * In a misconfigured network, the local host may have auditing
1140*0Sstevel@tonic-gate  * off while the destination may have auditing on, so if there
1141*0Sstevel@tonic-gate  * is sufficient memory, a buffer will be returned even in the
1142*0Sstevel@tonic-gate  * audit off case.
1143*0Sstevel@tonic-gate  */
1144*0Sstevel@tonic-gate size_t
1145*0Sstevel@tonic-gate adt_export_session_data(const adt_session_data_t *internal,
1146*0Sstevel@tonic-gate 			adt_export_data_t **external)
1147*0Sstevel@tonic-gate {
1148*0Sstevel@tonic-gate 	adt_internal_state_t	*dummy;
1149*0Sstevel@tonic-gate 	size_t			length = 0;
1150*0Sstevel@tonic-gate 
1151*0Sstevel@tonic-gate 	*external = malloc(sizeof (adt_export_data_t));
1152*0Sstevel@tonic-gate 
1153*0Sstevel@tonic-gate 	if (*external == NULL)
1154*0Sstevel@tonic-gate 		goto return_length;
1155*0Sstevel@tonic-gate 
1156*0Sstevel@tonic-gate 	if (internal == NULL) {
1157*0Sstevel@tonic-gate 		dummy = malloc(sizeof (adt_internal_state_t));
1158*0Sstevel@tonic-gate 		if (dummy == NULL)
1159*0Sstevel@tonic-gate 			goto return_length_free;
1160*0Sstevel@tonic-gate 
1161*0Sstevel@tonic-gate 		if (adt_init(dummy, 0)) { /* 0 == don't copy from proc */
1162*0Sstevel@tonic-gate 			free(dummy);
1163*0Sstevel@tonic-gate 			goto return_length_free;
1164*0Sstevel@tonic-gate 		}
1165*0Sstevel@tonic-gate 		length = adt_to_export_format(*external, dummy);
1166*0Sstevel@tonic-gate 		free(dummy);
1167*0Sstevel@tonic-gate 	} else {
1168*0Sstevel@tonic-gate 		length = adt_to_export_format(*external,
1169*0Sstevel@tonic-gate 		    (adt_internal_state_t *)internal);
1170*0Sstevel@tonic-gate 	}
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate return_length:
1173*0Sstevel@tonic-gate 	return (length);
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate return_length_free:
1176*0Sstevel@tonic-gate 	free(*external);
1177*0Sstevel@tonic-gate 	*external = NULL;
1178*0Sstevel@tonic-gate 	return (length);
1179*0Sstevel@tonic-gate }
1180*0Sstevel@tonic-gate 
1181*0Sstevel@tonic-gate static void
1182*0Sstevel@tonic-gate adt_setto_unaudited(adt_internal_state_t *state)
1183*0Sstevel@tonic-gate {
1184*0Sstevel@tonic-gate 	state->as_ruid = AU_NOAUDITID;
1185*0Sstevel@tonic-gate 	state->as_euid = AU_NOAUDITID;
1186*0Sstevel@tonic-gate 	state->as_rgid = AU_NOAUDITID;
1187*0Sstevel@tonic-gate 	state->as_egid = AU_NOAUDITID;
1188*0Sstevel@tonic-gate 
1189*0Sstevel@tonic-gate 	if (state->as_audit_enabled) {
1190*0Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
1191*0Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
1192*0Sstevel@tonic-gate 
1193*0Sstevel@tonic-gate 		(void) memset((void *)&(state->as_info.ai_termid), 0,
1194*0Sstevel@tonic-gate 		    sizeof (au_tid_addr_t));
1195*0Sstevel@tonic-gate 		state->as_info.ai_termid.at_type = AU_IPv4;
1196*0Sstevel@tonic-gate 
1197*0Sstevel@tonic-gate 		(void) memset((void *)&(state->as_info.ai_mask), 0,
1198*0Sstevel@tonic-gate 		    sizeof (au_mask_t));
1199*0Sstevel@tonic-gate 		state->as_have_user_data = 0;
1200*0Sstevel@tonic-gate 	}
1201*0Sstevel@tonic-gate }
1202*0Sstevel@tonic-gate 
1203*0Sstevel@tonic-gate /*
1204*0Sstevel@tonic-gate  * adt_init -- set session context by copying the audit characteristics
1205*0Sstevel@tonic-gate  * from the proc and picking up current uid/tid information.
1206*0Sstevel@tonic-gate  *
1207*0Sstevel@tonic-gate  * By default, an audit session is based on the process; the default
1208*0Sstevel@tonic-gate  * is overriden by adt_set_user()
1209*0Sstevel@tonic-gate  */
1210*0Sstevel@tonic-gate static int
1211*0Sstevel@tonic-gate adt_init(adt_internal_state_t *state, int use_proc_data)
1212*0Sstevel@tonic-gate {
1213*0Sstevel@tonic-gate 
1214*0Sstevel@tonic-gate 	state->as_audit_enabled = (auditstate == AUC_DISABLED) ? 0 : 1;
1215*0Sstevel@tonic-gate 
1216*0Sstevel@tonic-gate 	if (use_proc_data) {
1217*0Sstevel@tonic-gate 		state->as_ruid = getuid();
1218*0Sstevel@tonic-gate 		state->as_euid = geteuid();
1219*0Sstevel@tonic-gate 		state->as_rgid = getgid();
1220*0Sstevel@tonic-gate 		state->as_egid = getegid();
1221*0Sstevel@tonic-gate 
1222*0Sstevel@tonic-gate 		if (state->as_audit_enabled) {
1223*0Sstevel@tonic-gate 			const au_tid64_addr_t	*tid;
1224*0Sstevel@tonic-gate 			const au_mask_t		*mask;
1225*0Sstevel@tonic-gate 			ucred_t			*ucred = ucred_get(getpid());
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate 			/*
1228*0Sstevel@tonic-gate 			 * Even if the ucred is NULL, the underlying
1229*0Sstevel@tonic-gate 			 * credential may have a valid terminal id; if the
1230*0Sstevel@tonic-gate 			 * terminal id is set, then that's good enough.  An
1231*0Sstevel@tonic-gate 			 * example of where this matters is failed login,
1232*0Sstevel@tonic-gate 			 * where rlogin/telnet sets the terminal id before
1233*0Sstevel@tonic-gate 			 * calling login; login does not load the credential
1234*0Sstevel@tonic-gate 			 * since auth failed.
1235*0Sstevel@tonic-gate 			 */
1236*0Sstevel@tonic-gate 			if (ucred == NULL) {
1237*0Sstevel@tonic-gate 				if (!adt_have_termid(
1238*0Sstevel@tonic-gate 				    &(state->as_info.ai_termid)))
1239*0Sstevel@tonic-gate 					return (-1);
1240*0Sstevel@tonic-gate 			} else {
1241*0Sstevel@tonic-gate 				mask = ucred_getamask(ucred);
1242*0Sstevel@tonic-gate 				if (mask != NULL) {
1243*0Sstevel@tonic-gate 					state->as_info.ai_mask = *mask;
1244*0Sstevel@tonic-gate 				} else {
1245*0Sstevel@tonic-gate 					ucred_free(ucred);
1246*0Sstevel@tonic-gate 					return (-1);
1247*0Sstevel@tonic-gate 				}
1248*0Sstevel@tonic-gate 				tid = ucred_getatid(ucred);
1249*0Sstevel@tonic-gate 				if (tid != NULL) {
1250*0Sstevel@tonic-gate 					adt_cpy_tid(&(state->as_info.ai_termid),
1251*0Sstevel@tonic-gate 					    tid);
1252*0Sstevel@tonic-gate 				} else {
1253*0Sstevel@tonic-gate 					ucred_free(ucred);
1254*0Sstevel@tonic-gate 					return (-1);
1255*0Sstevel@tonic-gate 				}
1256*0Sstevel@tonic-gate 				state->as_info.ai_asid = ucred_getasid(ucred);
1257*0Sstevel@tonic-gate 				state->as_info.ai_auid = ucred_getauid(ucred);
1258*0Sstevel@tonic-gate 				ucred_free(ucred);
1259*0Sstevel@tonic-gate 			}
1260*0Sstevel@tonic-gate 			state->as_have_user_data = ADT_HAVE_ALL;
1261*0Sstevel@tonic-gate 		}
1262*0Sstevel@tonic-gate 	} else {
1263*0Sstevel@tonic-gate 		adt_setto_unaudited(state);
1264*0Sstevel@tonic-gate 	}
1265*0Sstevel@tonic-gate 	state->as_session_model = ADT_SESSION_MODEL;	/* default */
1266*0Sstevel@tonic-gate 
1267*0Sstevel@tonic-gate 	if (state->as_audit_enabled &&
1268*0Sstevel@tonic-gate 	    auditon(A_GETPOLICY, (caddr_t)&(state->as_kernel_audit_policy),
1269*0Sstevel@tonic-gate 	    sizeof (state->as_kernel_audit_policy))) {
1270*0Sstevel@tonic-gate 		return (-1);  /* errno set by auditon */
1271*0Sstevel@tonic-gate 	}
1272*0Sstevel@tonic-gate 	state->as_check = ADT_VALID;
1273*0Sstevel@tonic-gate 	return (0);
1274*0Sstevel@tonic-gate }
1275*0Sstevel@tonic-gate 
1276*0Sstevel@tonic-gate /*
1277*0Sstevel@tonic-gate  * adt_set_proc
1278*0Sstevel@tonic-gate  *
1279*0Sstevel@tonic-gate  * Copy the current session state to the process.  If this function
1280*0Sstevel@tonic-gate  * is called, the model becomes a process model rather than a
1281*0Sstevel@tonic-gate  * session model.
1282*0Sstevel@tonic-gate  *
1283*0Sstevel@tonic-gate  * In the current implementation, the value state->as_have_user_data
1284*0Sstevel@tonic-gate  * must be either ADT_HAVE_ALL or ADT_HAVE_TID.  The latter value
1285*0Sstevel@tonic-gate  * is to support the adt_set_user() flag of ADT_SETTID.  If another
1286*0Sstevel@tonic-gate  * need for setting single audit data is discovered, consider changing
1287*0Sstevel@tonic-gate  * the logic to test each individual flag setting rather than "ALL."
1288*0Sstevel@tonic-gate  *
1289*0Sstevel@tonic-gate  * Future consideration:  Is ADT_HAVE_ALL too strong a test?  There
1290*0Sstevel@tonic-gate  * doesn't seem to be a good reason for requiring that ADT_HAVE_IDS
1291*0Sstevel@tonic-gate  * be set.
1292*0Sstevel@tonic-gate  */
1293*0Sstevel@tonic-gate 
1294*0Sstevel@tonic-gate int
1295*0Sstevel@tonic-gate adt_set_proc(const adt_session_data_t *session_data)
1296*0Sstevel@tonic-gate {
1297*0Sstevel@tonic-gate 	struct auditinfo_addr	info;
1298*0Sstevel@tonic-gate 	int			rc;
1299*0Sstevel@tonic-gate 	adt_internal_state_t	*state;
1300*0Sstevel@tonic-gate 
1301*0Sstevel@tonic-gate 	if (auditstate == AUC_DISABLED || (session_data == NULL))
1302*0Sstevel@tonic-gate 		return (0);
1303*0Sstevel@tonic-gate 
1304*0Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
1305*0Sstevel@tonic-gate 
1306*0Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
1307*0Sstevel@tonic-gate 
1308*0Sstevel@tonic-gate 	if ((state->as_have_user_data != ADT_HAVE_ALL) &&
1309*0Sstevel@tonic-gate 	    ((state->as_have_user_data & ADT_HAVE_TID) == 0)) {
1310*0Sstevel@tonic-gate 		errno = EINVAL;
1311*0Sstevel@tonic-gate 		goto return_err;
1312*0Sstevel@tonic-gate 	}
1313*0Sstevel@tonic-gate 	/*
1314*0Sstevel@tonic-gate 	 * leave tid alone if session copy is not set
1315*0Sstevel@tonic-gate 	 * Since info contains tid, you need to get tid from kernel
1316*0Sstevel@tonic-gate 	 * so it can be written back.
1317*0Sstevel@tonic-gate 	 * All info other than tid is overwritten from session before
1318*0Sstevel@tonic-gate 	 * it is written back.
1319*0Sstevel@tonic-gate 	 *
1320*0Sstevel@tonic-gate 	 * See the note above the function header; if flag by flag
1321*0Sstevel@tonic-gate 	 * tests are done, testing for an empty tid should just
1322*0Sstevel@tonic-gate 	 * be a test for ADT_HAVE_TID -- and the functions that
1323*0Sstevel@tonic-gate 	 * load the terminal id may have to be modified to make
1324*0Sstevel@tonic-gate 	 * this appropriate.
1325*0Sstevel@tonic-gate 	 */
1326*0Sstevel@tonic-gate 	if (state->as_info.ai_termid.at_port ||
1327*0Sstevel@tonic-gate 	    state->as_info.ai_termid.at_addr[0] ||
1328*0Sstevel@tonic-gate 	    state->as_info.ai_termid.at_addr[1] ||
1329*0Sstevel@tonic-gate 	    state->as_info.ai_termid.at_addr[2] ||
1330*0Sstevel@tonic-gate 	    state->as_info.ai_termid.at_addr[3]) {
1331*0Sstevel@tonic-gate 		info.ai_termid = state->as_info.ai_termid;
1332*0Sstevel@tonic-gate 	} else {
1333*0Sstevel@tonic-gate 		rc = getaudit_addr(&info, sizeof (info));
1334*0Sstevel@tonic-gate 		if (rc < 0)
1335*0Sstevel@tonic-gate 			goto return_err;   /* errno set by getaudit_addr() */
1336*0Sstevel@tonic-gate 	}
1337*0Sstevel@tonic-gate 	info.ai_auid = state->as_info.ai_auid;
1338*0Sstevel@tonic-gate 	info.ai_asid = state->as_info.ai_asid;
1339*0Sstevel@tonic-gate 	info.ai_mask = state->as_info.ai_mask;
1340*0Sstevel@tonic-gate 
1341*0Sstevel@tonic-gate 	rc = setaudit_addr(&info, sizeof (info));
1342*0Sstevel@tonic-gate 
1343*0Sstevel@tonic-gate 	if (rc < 0)
1344*0Sstevel@tonic-gate 		goto return_err;	/* errno set by setaudit_addr() */
1345*0Sstevel@tonic-gate 
1346*0Sstevel@tonic-gate 	state->as_session_model = ADT_PROCESS_MODEL;
1347*0Sstevel@tonic-gate 
1348*0Sstevel@tonic-gate 	return (0);
1349*0Sstevel@tonic-gate 
1350*0Sstevel@tonic-gate return_err:
1351*0Sstevel@tonic-gate 	adt_write_syslog("failed to set process audit characteristics", errno);
1352*0Sstevel@tonic-gate 	return (-1);
1353*0Sstevel@tonic-gate }
1354*0Sstevel@tonic-gate 
1355*0Sstevel@tonic-gate static int
1356*0Sstevel@tonic-gate adt_newuser(adt_internal_state_t *state, uid_t ruid, au_tid_addr_t *termid)
1357*0Sstevel@tonic-gate {
1358*0Sstevel@tonic-gate 	au_tid_addr_t	no_tid = {0, AU_IPv4, 0, 0, 0, 0};
1359*0Sstevel@tonic-gate 	au_mask_t	no_mask = {0, 0};
1360*0Sstevel@tonic-gate 
1361*0Sstevel@tonic-gate 	if (ruid == ADT_NO_AUDIT) {
1362*0Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
1363*0Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
1364*0Sstevel@tonic-gate 		state->as_info.ai_termid = no_tid;
1365*0Sstevel@tonic-gate 		state->as_info.ai_mask = no_mask;
1366*0Sstevel@tonic-gate 		return (0);
1367*0Sstevel@tonic-gate 	}
1368*0Sstevel@tonic-gate 	state->as_info.ai_auid = ruid;
1369*0Sstevel@tonic-gate 	state->as_info.ai_asid = adt_get_unique_id(ruid);
1370*0Sstevel@tonic-gate 	if (termid != NULL)
1371*0Sstevel@tonic-gate 		state->as_info.ai_termid = *termid;
1372*0Sstevel@tonic-gate 
1373*0Sstevel@tonic-gate 	if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask)))
1374*0Sstevel@tonic-gate 		return (-1);
1375*0Sstevel@tonic-gate 
1376*0Sstevel@tonic-gate 	return (0);
1377*0Sstevel@tonic-gate }
1378*0Sstevel@tonic-gate static int
1379*0Sstevel@tonic-gate adt_changeuser(adt_internal_state_t *state, uid_t ruid)
1380*0Sstevel@tonic-gate {
1381*0Sstevel@tonic-gate 	au_mask_t		mask;
1382*0Sstevel@tonic-gate 
1383*0Sstevel@tonic-gate 	if (!(state->as_have_user_data & ADT_HAVE_AUID))
1384*0Sstevel@tonic-gate 		state->as_info.ai_auid = ruid;
1385*0Sstevel@tonic-gate 	if (!(state->as_have_user_data & ADT_HAVE_ASID))
1386*0Sstevel@tonic-gate 		state->as_info.ai_asid = adt_get_unique_id(ruid);
1387*0Sstevel@tonic-gate 
1388*0Sstevel@tonic-gate 	if (ruid >= 0) {
1389*0Sstevel@tonic-gate 		if (adt_get_mask_from_user(ruid, &mask))
1390*0Sstevel@tonic-gate 			return (-1);
1391*0Sstevel@tonic-gate 
1392*0Sstevel@tonic-gate 		state->as_info.ai_mask.am_success |= mask.am_success;
1393*0Sstevel@tonic-gate 		state->as_info.ai_mask.am_failure |= mask.am_failure;
1394*0Sstevel@tonic-gate 	}
1395*0Sstevel@tonic-gate 	DPRINTF(("changed mask to %08X/%08X for ruid=%d\n",
1396*0Sstevel@tonic-gate 		state->as_info.ai_mask.am_success,
1397*0Sstevel@tonic-gate 		state->as_info.ai_mask.am_failure,
1398*0Sstevel@tonic-gate 		ruid));
1399*0Sstevel@tonic-gate 	return (0);
1400*0Sstevel@tonic-gate }
1401*0Sstevel@tonic-gate /*
1402*0Sstevel@tonic-gate  * adt_set_user -- see also adt_set_from_ucred()
1403*0Sstevel@tonic-gate  *
1404*0Sstevel@tonic-gate  * ADT_NO_ATTRIB is a valid uid/gid meaning "not known" or
1405*0Sstevel@tonic-gate  * "unattributed."
1406*0Sstevel@tonic-gate  *
1407*0Sstevel@tonic-gate  * ADT_NO_CHANGE is a valid uid/gid meaning "do not change this value"
1408*0Sstevel@tonic-gate  * only valid with ADT_UPDATE.
1409*0Sstevel@tonic-gate  *
1410*0Sstevel@tonic-gate  * ADT_NO_AUDIT is the external equivalent to AU_NOAUDITIT -- there
1411*0Sstevel@tonic-gate  * isn't a good reason to call adt_set_user() with it unless you don't
1412*0Sstevel@tonic-gate  * have a good value yet and intend to replace it later; auid will be
1413*0Sstevel@tonic-gate  * AU_NOAUDITID.
1414*0Sstevel@tonic-gate  *
1415*0Sstevel@tonic-gate  * adt_set_user should be called even if auditing is not enabled
1416*0Sstevel@tonic-gate  * so that adt_export_session_data() will have useful stuff to
1417*0Sstevel@tonic-gate  * work with.
1418*0Sstevel@tonic-gate  *
1419*0Sstevel@tonic-gate  * See the note preceding adt_set_proc() about the use of ADT_HAVE_TID
1420*0Sstevel@tonic-gate  * and ADT_HAVE_ALL.
1421*0Sstevel@tonic-gate  */
1422*0Sstevel@tonic-gate int
1423*0Sstevel@tonic-gate adt_set_user(const adt_session_data_t *session_data,
1424*0Sstevel@tonic-gate     uid_t euid,
1425*0Sstevel@tonic-gate     gid_t egid,
1426*0Sstevel@tonic-gate     uid_t ruid,
1427*0Sstevel@tonic-gate     gid_t rgid,
1428*0Sstevel@tonic-gate     const adt_termid_t *termid,
1429*0Sstevel@tonic-gate     enum adt_user_context user_context)
1430*0Sstevel@tonic-gate {
1431*0Sstevel@tonic-gate 	adt_internal_state_t	*state;
1432*0Sstevel@tonic-gate 	int			rc;
1433*0Sstevel@tonic-gate 
1434*0Sstevel@tonic-gate 	if (session_data == NULL) /* no session exists to audit */
1435*0Sstevel@tonic-gate 		return (0);
1436*0Sstevel@tonic-gate 
1437*0Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
1438*0Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
1439*0Sstevel@tonic-gate 
1440*0Sstevel@tonic-gate 	switch (user_context) {
1441*0Sstevel@tonic-gate 	case ADT_NEW:
1442*0Sstevel@tonic-gate 		if (ruid == ADT_NO_CHANGE || euid == ADT_NO_CHANGE ||
1443*0Sstevel@tonic-gate 		    rgid == ADT_NO_CHANGE || egid == ADT_NO_CHANGE) {
1444*0Sstevel@tonic-gate 			errno = EINVAL;
1445*0Sstevel@tonic-gate 			return (-1);
1446*0Sstevel@tonic-gate 		}
1447*0Sstevel@tonic-gate 		if ((rc = adt_newuser(state, ruid,
1448*0Sstevel@tonic-gate 		    (au_tid_addr_t *)termid)) != 0)
1449*0Sstevel@tonic-gate 			return (rc);
1450*0Sstevel@tonic-gate 
1451*0Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_ALL;
1452*0Sstevel@tonic-gate 		break;
1453*0Sstevel@tonic-gate 	case ADT_UPDATE:
1454*0Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
1455*0Sstevel@tonic-gate 			errno = EINVAL;
1456*0Sstevel@tonic-gate 			return (-1);
1457*0Sstevel@tonic-gate 		}
1458*0Sstevel@tonic-gate 
1459*0Sstevel@tonic-gate 		if (ruid != ADT_NO_CHANGE)
1460*0Sstevel@tonic-gate 			if ((rc = adt_changeuser(state, ruid)) != 0)
1461*0Sstevel@tonic-gate 				return (rc);
1462*0Sstevel@tonic-gate 		break;
1463*0Sstevel@tonic-gate 	case ADT_USER:
1464*0Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
1465*0Sstevel@tonic-gate 			errno = EINVAL;
1466*0Sstevel@tonic-gate 			return (-1);
1467*0Sstevel@tonic-gate 		}
1468*0Sstevel@tonic-gate 		break;
1469*0Sstevel@tonic-gate 	case ADT_SETTID:
1470*0Sstevel@tonic-gate 		assert(termid != NULL);
1471*0Sstevel@tonic-gate 		state->as_info.ai_termid = *((au_tid_addr_t *)termid);
1472*0Sstevel@tonic-gate 		/* avoid fooling pam_setcred()... */
1473*0Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
1474*0Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
1475*0Sstevel@tonic-gate 		state->as_info.ai_mask.am_failure = 0;
1476*0Sstevel@tonic-gate 		state->as_info.ai_mask.am_success = 0;
1477*0Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_TID |
1478*0Sstevel@tonic-gate 		    ADT_HAVE_AUID | ADT_HAVE_ASID | ADT_HAVE_MASK;
1479*0Sstevel@tonic-gate 		return (0);
1480*0Sstevel@tonic-gate 		break;
1481*0Sstevel@tonic-gate 
1482*0Sstevel@tonic-gate 	default:
1483*0Sstevel@tonic-gate 		errno = EINVAL;
1484*0Sstevel@tonic-gate 		return (-1);
1485*0Sstevel@tonic-gate 	}
1486*0Sstevel@tonic-gate 
1487*0Sstevel@tonic-gate 	if (ruid == ADT_NO_AUDIT) {
1488*0Sstevel@tonic-gate 		state->as_ruid = AU_NOAUDITID;
1489*0Sstevel@tonic-gate 		state->as_euid = AU_NOAUDITID;
1490*0Sstevel@tonic-gate 		state->as_rgid = AU_NOAUDITID;
1491*0Sstevel@tonic-gate 		state->as_egid = AU_NOAUDITID;
1492*0Sstevel@tonic-gate 	} else {
1493*0Sstevel@tonic-gate 		if (ruid != ADT_NO_CHANGE)
1494*0Sstevel@tonic-gate 			state->as_ruid = ruid;
1495*0Sstevel@tonic-gate 		if (euid != ADT_NO_CHANGE)
1496*0Sstevel@tonic-gate 			state->as_euid = euid;
1497*0Sstevel@tonic-gate 		if (rgid != ADT_NO_CHANGE)
1498*0Sstevel@tonic-gate 			state->as_rgid = rgid;
1499*0Sstevel@tonic-gate 		if (egid != ADT_NO_CHANGE)
1500*0Sstevel@tonic-gate 			state->as_egid = egid;
1501*0Sstevel@tonic-gate 	}
1502*0Sstevel@tonic-gate 
1503*0Sstevel@tonic-gate 	return (0);
1504*0Sstevel@tonic-gate }
1505*0Sstevel@tonic-gate /*
1506*0Sstevel@tonic-gate  * adt_set_from_ucred()
1507*0Sstevel@tonic-gate  *
1508*0Sstevel@tonic-gate  * an alternate to adt_set_user that fills the same role but uses
1509*0Sstevel@tonic-gate  * a pointer to a ucred rather than a list of id's.  If the ucred
1510*0Sstevel@tonic-gate  * pointer is NULL, use the credential from the this process.
1511*0Sstevel@tonic-gate  *
1512*0Sstevel@tonic-gate  * A key difference is that for ADT_NEW, adt_set_from_ucred() does
1513*0Sstevel@tonic-gate  * not overwrite the asid and auid unless auid has not been set.
1514*0Sstevel@tonic-gate  * ADT_NEW differs from ADT_UPDATE in that it does not OR together
1515*0Sstevel@tonic-gate  * the incoming audit mask with the one that already exists.
1516*0Sstevel@tonic-gate  *
1517*0Sstevel@tonic-gate  * adt_set_from_ucred should be called even if auditing is not enabled
1518*0Sstevel@tonic-gate  * so that adt_export_session_data() will have useful stuff to
1519*0Sstevel@tonic-gate  * work with.
1520*0Sstevel@tonic-gate  */
1521*0Sstevel@tonic-gate int
1522*0Sstevel@tonic-gate adt_set_from_ucred(const adt_session_data_t *session_data,
1523*0Sstevel@tonic-gate     const ucred_t *ucred,
1524*0Sstevel@tonic-gate     enum adt_user_context user_context)
1525*0Sstevel@tonic-gate {
1526*0Sstevel@tonic-gate 	adt_internal_state_t	*state;
1527*0Sstevel@tonic-gate 	int			rc = -1;
1528*0Sstevel@tonic-gate 	const au_tid64_addr_t		*tid64;
1529*0Sstevel@tonic-gate 	au_tid_addr_t		termid, *tid;
1530*0Sstevel@tonic-gate 
1531*0Sstevel@tonic-gate 	if (session_data == NULL) /* no session exists to audit */
1532*0Sstevel@tonic-gate 		return (0);
1533*0Sstevel@tonic-gate 
1534*0Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
1535*0Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
1536*0Sstevel@tonic-gate 
1537*0Sstevel@tonic-gate 	if (ucred == NULL) {
1538*0Sstevel@tonic-gate 		ucred = ucred_get(getpid());
1539*0Sstevel@tonic-gate 
1540*0Sstevel@tonic-gate 		if (ucred == NULL)
1541*0Sstevel@tonic-gate 			goto return_rc;
1542*0Sstevel@tonic-gate 	}
1543*0Sstevel@tonic-gate 
1544*0Sstevel@tonic-gate 	switch (user_context) {
1545*0Sstevel@tonic-gate 	case ADT_NEW:
1546*0Sstevel@tonic-gate 		tid64 = ucred_getatid(ucred);
1547*0Sstevel@tonic-gate 		if (tid64 != NULL) {
1548*0Sstevel@tonic-gate 			adt_cpy_tid(&termid, tid64);
1549*0Sstevel@tonic-gate 			tid = &termid;
1550*0Sstevel@tonic-gate 		} else {
1551*0Sstevel@tonic-gate 			tid = NULL;
1552*0Sstevel@tonic-gate 		}
1553*0Sstevel@tonic-gate 		/* if unaudited, adt_newuser cleans up */
1554*0Sstevel@tonic-gate 		if (ucred_getauid(ucred) == AU_NOAUDITID) {
1555*0Sstevel@tonic-gate 			if ((rc = adt_newuser(state, ucred_getruid(ucred),
1556*0Sstevel@tonic-gate 			    tid)) != 0)
1557*0Sstevel@tonic-gate 				goto return_rc;
1558*0Sstevel@tonic-gate 		} else {
1559*0Sstevel@tonic-gate 			state->as_info.ai_auid = ucred_getauid(ucred);
1560*0Sstevel@tonic-gate 			state->as_info.ai_asid = ucred_getasid(ucred);
1561*0Sstevel@tonic-gate 			state->as_info.ai_mask = *ucred_getamask(ucred);
1562*0Sstevel@tonic-gate 			state->as_info.ai_termid = *tid;
1563*0Sstevel@tonic-gate 		}
1564*0Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_ALL;
1565*0Sstevel@tonic-gate 		break;
1566*0Sstevel@tonic-gate 	case ADT_UPDATE:
1567*0Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
1568*0Sstevel@tonic-gate 			errno = EINVAL;
1569*0Sstevel@tonic-gate 			goto return_rc;
1570*0Sstevel@tonic-gate 		}
1571*0Sstevel@tonic-gate 
1572*0Sstevel@tonic-gate 		if ((rc = adt_changeuser(state, ucred_getruid(ucred))) != 0)
1573*0Sstevel@tonic-gate 			goto return_rc;
1574*0Sstevel@tonic-gate 		break;
1575*0Sstevel@tonic-gate 	case ADT_USER:
1576*0Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
1577*0Sstevel@tonic-gate 			errno = EINVAL;
1578*0Sstevel@tonic-gate 			goto return_rc;
1579*0Sstevel@tonic-gate 		}
1580*0Sstevel@tonic-gate 		break;
1581*0Sstevel@tonic-gate 	default:
1582*0Sstevel@tonic-gate 		errno = EINVAL;
1583*0Sstevel@tonic-gate 		goto return_rc;
1584*0Sstevel@tonic-gate 	}
1585*0Sstevel@tonic-gate 	rc = 0;
1586*0Sstevel@tonic-gate 
1587*0Sstevel@tonic-gate 	state->as_ruid = ucred_getruid(ucred);
1588*0Sstevel@tonic-gate 	state->as_euid = ucred_geteuid(ucred);
1589*0Sstevel@tonic-gate 	state->as_rgid = ucred_getrgid(ucred);
1590*0Sstevel@tonic-gate 	state->as_egid = ucred_getegid(ucred);
1591*0Sstevel@tonic-gate 
1592*0Sstevel@tonic-gate return_rc:
1593*0Sstevel@tonic-gate 	return (rc);
1594*0Sstevel@tonic-gate }
1595*0Sstevel@tonic-gate 
1596*0Sstevel@tonic-gate /*
1597*0Sstevel@tonic-gate  * adt_alloc_event() returns a pointer to allocated memory
1598*0Sstevel@tonic-gate  *
1599*0Sstevel@tonic-gate  */
1600*0Sstevel@tonic-gate 
1601*0Sstevel@tonic-gate adt_event_data_t
1602*0Sstevel@tonic-gate *adt_alloc_event(const adt_session_data_t *session_data, au_event_t event_id)
1603*0Sstevel@tonic-gate {
1604*0Sstevel@tonic-gate 	struct adt_event_state	*event_state;
1605*0Sstevel@tonic-gate 	adt_internal_state_t	*session_state;
1606*0Sstevel@tonic-gate 	adt_event_data_t	*return_event = NULL;
1607*0Sstevel@tonic-gate 	/*
1608*0Sstevel@tonic-gate 	 * need to return a valid event pointer even if audit is
1609*0Sstevel@tonic-gate 	 * off, else the caller will end up either (1) keeping its
1610*0Sstevel@tonic-gate 	 * own flags for on/off or (2) writing to a NULL pointer.
1611*0Sstevel@tonic-gate 	 * If auditing is on, the session data must be valid; otherwise
1612*0Sstevel@tonic-gate 	 * we don't care.
1613*0Sstevel@tonic-gate 	 */
1614*0Sstevel@tonic-gate 	if (session_data != NULL) {
1615*0Sstevel@tonic-gate 		session_state = (adt_internal_state_t *)session_data;
1616*0Sstevel@tonic-gate 		assert(session_state->as_check == ADT_VALID);
1617*0Sstevel@tonic-gate 	}
1618*0Sstevel@tonic-gate 	event_state = calloc(1, sizeof (struct adt_event_state));
1619*0Sstevel@tonic-gate 	if (event_state == NULL)
1620*0Sstevel@tonic-gate 		goto return_ptr;
1621*0Sstevel@tonic-gate 
1622*0Sstevel@tonic-gate 	event_state->ae_check = ADT_VALID;
1623*0Sstevel@tonic-gate 
1624*0Sstevel@tonic-gate 	event_state->ae_event_id = event_id;
1625*0Sstevel@tonic-gate 	event_state->ae_session = (struct adt_internal_state *)session_data;
1626*0Sstevel@tonic-gate 
1627*0Sstevel@tonic-gate 	return_event = (adt_event_data_t *)&(event_state->ae_event_data);
1628*0Sstevel@tonic-gate 
1629*0Sstevel@tonic-gate 	/*
1630*0Sstevel@tonic-gate 	 * preload data so the adt_au_*() functions can detect un-supplied
1631*0Sstevel@tonic-gate 	 * values (0 and NULL are free via calloc()).
1632*0Sstevel@tonic-gate 	 */
1633*0Sstevel@tonic-gate 	adt_preload(event_id, return_event);
1634*0Sstevel@tonic-gate 
1635*0Sstevel@tonic-gate return_ptr:
1636*0Sstevel@tonic-gate 	return (return_event);
1637*0Sstevel@tonic-gate }
1638*0Sstevel@tonic-gate 
1639*0Sstevel@tonic-gate /*
1640*0Sstevel@tonic-gate  * adt_getXlateTable -- look up translation table address for event id
1641*0Sstevel@tonic-gate  */
1642*0Sstevel@tonic-gate 
1643*0Sstevel@tonic-gate static struct translation *
1644*0Sstevel@tonic-gate adt_getXlateTable(au_event_t event_id)
1645*0Sstevel@tonic-gate {
1646*0Sstevel@tonic-gate 	/* xlate_table is global in adt_xlate.c */
1647*0Sstevel@tonic-gate 	struct translation	**p_xlate = &xlate_table[0];
1648*0Sstevel@tonic-gate 	struct translation	*p_event;
1649*0Sstevel@tonic-gate 
1650*0Sstevel@tonic-gate 	while (*p_xlate != NULL) {
1651*0Sstevel@tonic-gate 		p_event = *p_xlate;
1652*0Sstevel@tonic-gate 		if (event_id == p_event->tx_external_event)
1653*0Sstevel@tonic-gate 			return (p_event);
1654*0Sstevel@tonic-gate 		p_xlate++;
1655*0Sstevel@tonic-gate 	}
1656*0Sstevel@tonic-gate 	return (NULL);
1657*0Sstevel@tonic-gate }
1658*0Sstevel@tonic-gate 
1659*0Sstevel@tonic-gate /*
1660*0Sstevel@tonic-gate  * adt_calcOffsets
1661*0Sstevel@tonic-gate  *
1662*0Sstevel@tonic-gate  * the call to this function is surrounded by a mutex.
1663*0Sstevel@tonic-gate  *
1664*0Sstevel@tonic-gate  * i walks down the table picking up next_token.  j walks again to
1665*0Sstevel@tonic-gate  * calculate the offset to the input data.  k points to the next
1666*0Sstevel@tonic-gate  * token's row.  Finally, l, is used to sum the values in the
1667*0Sstevel@tonic-gate  * datadef array.
1668*0Sstevel@tonic-gate  *
1669*0Sstevel@tonic-gate  * What's going on?  The entry array is in the order of the input
1670*0Sstevel@tonic-gate  * fields but the processing of array entries is in the order of
1671*0Sstevel@tonic-gate  * the output (see next_token).  Calculating the offset to the
1672*0Sstevel@tonic-gate  * "next" input can't be done in the outer loop (i) since i doesn't
1673*0Sstevel@tonic-gate  * point to the current entry and it can't be done with the k index
1674*0Sstevel@tonic-gate  * because it doesn't represent the order of input fields.
1675*0Sstevel@tonic-gate  *
1676*0Sstevel@tonic-gate  * While the resulting algorithm is n**2, it is only done once per
1677*0Sstevel@tonic-gate  * event type.
1678*0Sstevel@tonic-gate  */
1679*0Sstevel@tonic-gate 
1680*0Sstevel@tonic-gate /*
1681*0Sstevel@tonic-gate  * adt_calcOffsets is only called once per event type, but it uses
1682*0Sstevel@tonic-gate  * the address alignment of memory allocated for that event as if it
1683*0Sstevel@tonic-gate  * were the same for all subsequently allocated memory.  This is
1684*0Sstevel@tonic-gate  * guaranteed by calloc/malloc.  Arrays take special handling since
1685*0Sstevel@tonic-gate  * what matters for figuring out the correct alignment is the size
1686*0Sstevel@tonic-gate  * of the array element.
1687*0Sstevel@tonic-gate  */
1688*0Sstevel@tonic-gate 
1689*0Sstevel@tonic-gate static void
1690*0Sstevel@tonic-gate adt_calcOffsets(struct entry *p_entry, int tablesize, void *p_data)
1691*0Sstevel@tonic-gate {
1692*0Sstevel@tonic-gate 	int		i, j;
1693*0Sstevel@tonic-gate 	size_t		this_size, prev_size;
1694*0Sstevel@tonic-gate 	void		*struct_start = p_data;
1695*0Sstevel@tonic-gate 
1696*0Sstevel@tonic-gate 	for (i = 0; i < tablesize; i++) {
1697*0Sstevel@tonic-gate 		if (p_entry[i].en_type_def == NULL) {
1698*0Sstevel@tonic-gate 			p_entry[i].en_offset = 0;
1699*0Sstevel@tonic-gate 			continue;
1700*0Sstevel@tonic-gate 		}
1701*0Sstevel@tonic-gate 		prev_size = 0;
1702*0Sstevel@tonic-gate 		p_entry[i].en_offset = (char *)p_data - (char *)struct_start;
1703*0Sstevel@tonic-gate 
1704*0Sstevel@tonic-gate 		for (j = 0; j < p_entry[i].en_count_types; j++) {
1705*0Sstevel@tonic-gate 			if (p_entry[i].en_type_def[j].dd_datatype == ADT_MSG)
1706*0Sstevel@tonic-gate 				this_size = sizeof (enum adt_generic);
1707*0Sstevel@tonic-gate 			else
1708*0Sstevel@tonic-gate 				this_size =
1709*0Sstevel@tonic-gate 				    p_entry[i].en_type_def[j].dd_input_size;
1710*0Sstevel@tonic-gate 
1711*0Sstevel@tonic-gate 			/* adj for first entry */
1712*0Sstevel@tonic-gate 			if (prev_size == 0)
1713*0Sstevel@tonic-gate 				prev_size = this_size;
1714*0Sstevel@tonic-gate 
1715*0Sstevel@tonic-gate 			if (p_entry[i].en_type_def[j].dd_datatype ==
1716*0Sstevel@tonic-gate 			    ADT_UINT32ARRAY) {
1717*0Sstevel@tonic-gate 				p_data = (char *)adt_adjust_address(p_data,
1718*0Sstevel@tonic-gate 				    prev_size, sizeof (uint32_t)) +
1719*0Sstevel@tonic-gate 				    this_size - sizeof (uint32_t);
1720*0Sstevel@tonic-gate 
1721*0Sstevel@tonic-gate 				prev_size = sizeof (uint32_t);
1722*0Sstevel@tonic-gate 			} else {
1723*0Sstevel@tonic-gate 				p_data = adt_adjust_address(p_data, prev_size,
1724*0Sstevel@tonic-gate 				    this_size);
1725*0Sstevel@tonic-gate 				prev_size = this_size;
1726*0Sstevel@tonic-gate 			}
1727*0Sstevel@tonic-gate 		}
1728*0Sstevel@tonic-gate 	}
1729*0Sstevel@tonic-gate }
1730*0Sstevel@tonic-gate 
1731*0Sstevel@tonic-gate /*
1732*0Sstevel@tonic-gate  * adt_generate_event
1733*0Sstevel@tonic-gate  * generate event record from external struct.  The order is based on
1734*0Sstevel@tonic-gate  * the output tokens, allowing for the possibility that the input data
1735*0Sstevel@tonic-gate  * is in a different order.
1736*0Sstevel@tonic-gate  *
1737*0Sstevel@tonic-gate  */
1738*0Sstevel@tonic-gate 
1739*0Sstevel@tonic-gate static void
1740*0Sstevel@tonic-gate adt_generate_event(const adt_event_data_t *p_extdata,
1741*0Sstevel@tonic-gate     struct adt_event_state *p_event,
1742*0Sstevel@tonic-gate     struct translation *p_xlate)
1743*0Sstevel@tonic-gate {
1744*0Sstevel@tonic-gate 	struct entry		*p_entry;
1745*0Sstevel@tonic-gate 	static mutex_t	lock = DEFAULTMUTEX;
1746*0Sstevel@tonic-gate 
1747*0Sstevel@tonic-gate 	p_entry = p_xlate->tx_first_entry;
1748*0Sstevel@tonic-gate 	assert(p_entry != NULL);
1749*0Sstevel@tonic-gate 
1750*0Sstevel@tonic-gate 	p_event->ae_internal_id = p_xlate->tx_internal_event;
1751*0Sstevel@tonic-gate 	adt_token_open(p_event);
1752*0Sstevel@tonic-gate 
1753*0Sstevel@tonic-gate 	/*
1754*0Sstevel@tonic-gate 	 * offsets are not pre-calculated; the initial offsets are all
1755*0Sstevel@tonic-gate 	 * 0; valid offsets are >= 0.  Offsets for no-input tokens such
1756*0Sstevel@tonic-gate 	 * as subject are set to -1 by adt_calcOffset()
1757*0Sstevel@tonic-gate 	 */
1758*0Sstevel@tonic-gate 	if (p_xlate->tx_offsetsCalculated == 0) {
1759*0Sstevel@tonic-gate 		(void) _mutex_lock(&lock);
1760*0Sstevel@tonic-gate 		p_xlate->tx_offsetsCalculated = 1;
1761*0Sstevel@tonic-gate 
1762*0Sstevel@tonic-gate 		adt_calcOffsets(p_xlate->tx_top_entry, p_xlate->tx_entries,
1763*0Sstevel@tonic-gate 		    (void *)p_extdata);
1764*0Sstevel@tonic-gate 		(void) _mutex_unlock(&lock);
1765*0Sstevel@tonic-gate 	}
1766*0Sstevel@tonic-gate 	while (p_entry != NULL) {
1767*0Sstevel@tonic-gate 		adt_generate_token(p_entry, (char *)p_extdata,
1768*0Sstevel@tonic-gate 		    p_event);
1769*0Sstevel@tonic-gate 
1770*0Sstevel@tonic-gate 		p_entry = p_entry->en_next_token;
1771*0Sstevel@tonic-gate 	}
1772*0Sstevel@tonic-gate 	adt_token_close(p_event);
1773*0Sstevel@tonic-gate }
1774*0Sstevel@tonic-gate 
1775*0Sstevel@tonic-gate /*
1776*0Sstevel@tonic-gate  * adt_put_event -- main event generation function.
1777*0Sstevel@tonic-gate  * The input "event" is the address of the struct containing
1778*0Sstevel@tonic-gate  * event-specific data.
1779*0Sstevel@tonic-gate  *
1780*0Sstevel@tonic-gate  * However if auditing is off or the session handle
1781*0Sstevel@tonic-gate  * is NULL, no attempt to write a record is made.
1782*0Sstevel@tonic-gate  */
1783*0Sstevel@tonic-gate 
1784*0Sstevel@tonic-gate int
1785*0Sstevel@tonic-gate adt_put_event(const adt_event_data_t *event, int status,
1786*0Sstevel@tonic-gate     int return_val)
1787*0Sstevel@tonic-gate {
1788*0Sstevel@tonic-gate 	struct adt_event_state	*event_state;
1789*0Sstevel@tonic-gate 	struct translation	*xlate;
1790*0Sstevel@tonic-gate 	int			rc = 0;
1791*0Sstevel@tonic-gate 
1792*0Sstevel@tonic-gate 	if (event == NULL) {
1793*0Sstevel@tonic-gate 		errno = EINVAL;
1794*0Sstevel@tonic-gate 		rc = -1;
1795*0Sstevel@tonic-gate 		goto return_rc;
1796*0Sstevel@tonic-gate 	}
1797*0Sstevel@tonic-gate 	event_state = (struct adt_event_state *)event;
1798*0Sstevel@tonic-gate 
1799*0Sstevel@tonic-gate 	/* if audit off or this is a broken session, exit */
1800*0Sstevel@tonic-gate 	if (auditstate == AUC_DISABLED || (event_state->ae_session == NULL))
1801*0Sstevel@tonic-gate 		goto return_rc;
1802*0Sstevel@tonic-gate 
1803*0Sstevel@tonic-gate 	assert(event_state->ae_check == ADT_VALID);
1804*0Sstevel@tonic-gate 
1805*0Sstevel@tonic-gate 	event_state->ae_rc = status;
1806*0Sstevel@tonic-gate 	event_state->ae_type = return_val;
1807*0Sstevel@tonic-gate 
1808*0Sstevel@tonic-gate 	/* look up the event */
1809*0Sstevel@tonic-gate 
1810*0Sstevel@tonic-gate 	xlate = adt_getXlateTable(event_state->ae_event_id);
1811*0Sstevel@tonic-gate 
1812*0Sstevel@tonic-gate 	if (xlate == NULL) {
1813*0Sstevel@tonic-gate 		errno = EINVAL;
1814*0Sstevel@tonic-gate 		rc = -1;
1815*0Sstevel@tonic-gate 		goto return_rc;
1816*0Sstevel@tonic-gate 	}
1817*0Sstevel@tonic-gate 	DPRINTF(("got event %d\n", xlate->tx_internal_event));
1818*0Sstevel@tonic-gate 
1819*0Sstevel@tonic-gate 	if (adt_selected(event_state, xlate->tx_internal_event, status))
1820*0Sstevel@tonic-gate 		adt_generate_event(event, event_state, xlate);
1821*0Sstevel@tonic-gate 
1822*0Sstevel@tonic-gate return_rc:
1823*0Sstevel@tonic-gate 	return (rc);
1824*0Sstevel@tonic-gate }
1825*0Sstevel@tonic-gate 
1826*0Sstevel@tonic-gate /*
1827*0Sstevel@tonic-gate  * adt_free_event -- invalidate and free
1828*0Sstevel@tonic-gate  */
1829*0Sstevel@tonic-gate 
1830*0Sstevel@tonic-gate void
1831*0Sstevel@tonic-gate adt_free_event(adt_event_data_t *event)
1832*0Sstevel@tonic-gate {
1833*0Sstevel@tonic-gate 	struct adt_event_state	*event_state;
1834*0Sstevel@tonic-gate 
1835*0Sstevel@tonic-gate 	if (event == NULL)
1836*0Sstevel@tonic-gate 		return;
1837*0Sstevel@tonic-gate 
1838*0Sstevel@tonic-gate 	event_state = (struct adt_event_state *)event;
1839*0Sstevel@tonic-gate 
1840*0Sstevel@tonic-gate 	assert(event_state->ae_check == ADT_VALID);
1841*0Sstevel@tonic-gate 
1842*0Sstevel@tonic-gate 	event_state->ae_check = 0;
1843*0Sstevel@tonic-gate 
1844*0Sstevel@tonic-gate 	free(event_state);
1845*0Sstevel@tonic-gate }
1846*0Sstevel@tonic-gate 
1847*0Sstevel@tonic-gate /*
1848*0Sstevel@tonic-gate  * adt_is_selected -- helper to adt_selected(), below.
1849*0Sstevel@tonic-gate  *
1850*0Sstevel@tonic-gate  * "sorf" is "success or fail" status; au_preselect compares
1851*0Sstevel@tonic-gate  * that with success, fail, or both.
1852*0Sstevel@tonic-gate  */
1853*0Sstevel@tonic-gate 
1854*0Sstevel@tonic-gate static int
1855*0Sstevel@tonic-gate adt_is_selected(au_event_t e, au_mask_t *m, int sorf)
1856*0Sstevel@tonic-gate {
1857*0Sstevel@tonic-gate 	int prs_sorf;
1858*0Sstevel@tonic-gate 
1859*0Sstevel@tonic-gate 	if (sorf == 0)
1860*0Sstevel@tonic-gate 		prs_sorf = AU_PRS_SUCCESS;
1861*0Sstevel@tonic-gate 	else
1862*0Sstevel@tonic-gate 		prs_sorf = AU_PRS_FAILURE;
1863*0Sstevel@tonic-gate 
1864*0Sstevel@tonic-gate 	return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD));
1865*0Sstevel@tonic-gate }
1866*0Sstevel@tonic-gate 
1867*0Sstevel@tonic-gate /*
1868*0Sstevel@tonic-gate  * selected -- see if this event is preselected.
1869*0Sstevel@tonic-gate  *
1870*0Sstevel@tonic-gate  * if errors are encountered trying to check a preselection mask
1871*0Sstevel@tonic-gate  * or look up a user name, the event is selected.  Otherwise, the
1872*0Sstevel@tonic-gate  * preselection mask is used for the job.
1873*0Sstevel@tonic-gate  */
1874*0Sstevel@tonic-gate 
1875*0Sstevel@tonic-gate static int
1876*0Sstevel@tonic-gate adt_selected(struct adt_event_state *event, au_event_t actual_id, int status)
1877*0Sstevel@tonic-gate {
1878*0Sstevel@tonic-gate 	adt_internal_state_t *sp;
1879*0Sstevel@tonic-gate 	au_mask_t	namask;
1880*0Sstevel@tonic-gate 
1881*0Sstevel@tonic-gate 	sp = event->ae_session;
1882*0Sstevel@tonic-gate 
1883*0Sstevel@tonic-gate 	if ((sp->as_have_user_data & ADT_HAVE_IDS) == 0) {
1884*0Sstevel@tonic-gate 		adt_write_syslog("No user data available", EINVAL);
1885*0Sstevel@tonic-gate 		return (1);	/* default is "selected" */
1886*0Sstevel@tonic-gate 	}
1887*0Sstevel@tonic-gate 
1888*0Sstevel@tonic-gate 	/* non-attributable? */
1889*0Sstevel@tonic-gate 	if ((sp->as_info.ai_auid == AU_NOAUDITID) ||
1890*0Sstevel@tonic-gate 	    (sp->as_info.ai_auid == ADT_NO_AUDIT)) {
1891*0Sstevel@tonic-gate 		if (auditon(A_GETKMASK, (caddr_t)&namask,
1892*0Sstevel@tonic-gate 		    sizeof (namask)) != 0) {
1893*0Sstevel@tonic-gate 			adt_write_syslog("auditon failure", errno);
1894*0Sstevel@tonic-gate 			return (1);
1895*0Sstevel@tonic-gate 		}
1896*0Sstevel@tonic-gate 		return (adt_is_selected(actual_id, &namask, status));
1897*0Sstevel@tonic-gate 	} else {
1898*0Sstevel@tonic-gate 		return (adt_is_selected(actual_id, &(sp->as_info.ai_mask),
1899*0Sstevel@tonic-gate 		    status));
1900*0Sstevel@tonic-gate 	}
1901*0Sstevel@tonic-gate }
1902