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