xref: /illumos-gate/usr/src/lib/libbsm/common/adt.c (revision 0d8e9319)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58c3c55e7Spaulson  * Common Development and Distribution License (the "License").
68c3c55e7Spaulson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
2391b2cbb3Sgww  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
24435a78ccSAndy Fiddaman  * Copyright 2017 OmniOS Community Edition (OmniOSce) Association.
25cd918266Skcrowenex  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
297c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h>
307c478bd9Sstevel@tonic-gate #include <assert.h>
317c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
327c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
337c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
347c478bd9Sstevel@tonic-gate #include <door.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <generic.h>
377c478bd9Sstevel@tonic-gate #include <md5.h>
387c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
397c478bd9Sstevel@tonic-gate #include <netdb.h>
407c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
417c478bd9Sstevel@tonic-gate #include <pwd.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <time.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include <synch.h>
477c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
487c478bd9Sstevel@tonic-gate #include <syslog.h>
497c478bd9Sstevel@tonic-gate #include <thread.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <adt_xlate.h>
527c478bd9Sstevel@tonic-gate #include <adt_ucred.h>
539697ae98Sgww #include <arpa/inet.h>
549697ae98Sgww #include <net/if.h>
559697ae98Sgww #include <libinetutil.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static int adt_selected(struct adt_event_state *, au_event_t, int);
587c478bd9Sstevel@tonic-gate static int adt_init(adt_internal_state_t *, int);
597c478bd9Sstevel@tonic-gate static int adt_import(adt_internal_state_t *, const adt_export_data_t *);
60c529a23fSgww static m_label_t *adt_ucred_label(ucred_t *);
61b3baaabfSgww static void adt_setto_unaudited(adt_internal_state_t *);
629697ae98Sgww static int adt_get_local_address(int, struct ifaddrlist *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #ifdef C2_DEBUG
65dfc7be02SJan Friedel #define	DPRINTF(x) { (void) printf x; }
66dfc7be02SJan Friedel #define	DFLUSH (void) fflush(stdout);
677c478bd9Sstevel@tonic-gate #else
687c478bd9Sstevel@tonic-gate #define	DPRINTF(x)
697c478bd9Sstevel@tonic-gate #define	DFLUSH
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 
7291b2cbb3Sgww /*
7391b2cbb3Sgww  * Local audit states are a bit mask
7491b2cbb3Sgww  *
7591b2cbb3Sgww  * The global audit states are
7691b2cbb3Sgww  *
7791b2cbb3Sgww  * AUC_UNSET             0      - on/off hasn't been decided
7891b2cbb3Sgww  * AUC_ENABLED           1      - loaded and enabled
7991b2cbb3Sgww  *
8091b2cbb3Sgww  * The local Zone states are
8191b2cbb3Sgww  *
8291b2cbb3Sgww  * AUC_AUDITING         0x1     - audit daemon is active
8391b2cbb3Sgww  * AUC_NOAUDIT          0x2     - audit daemon is not active
8491b2cbb3Sgww  * AUC_INIT_AUDIT       0x4     - audit is ready but auditd has not run
8591b2cbb3Sgww  * AUC_NOSPACE          0x8     - audit enabled, no space for audit records
8691b2cbb3Sgww  *
8791b2cbb3Sgww  * The only values returned by auditon(A_GETCOND) are:
8891b2cbb3Sgww  * AUC_INIT_AUDIT, AUC_AUDITING, AUC_NOAUDIT, AUC_NOSPACE
8991b2cbb3Sgww  *
9091b2cbb3Sgww  * The pseudo audit state used when the c2audit module is excluded is
9191b2cbb3Sgww  *
9291b2cbb3Sgww  * AUC_DISABLED		0x100	- c2audit module is excluded
9391b2cbb3Sgww  */
9491b2cbb3Sgww 
957c478bd9Sstevel@tonic-gate static int auditstate = AUC_DISABLED;	/* default state */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * adt_write_syslog
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  * errors that are not the user's fault (bugs or whatever in
1017c478bd9Sstevel@tonic-gate  * the underlying audit code are noted in syslog.)
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * Avoid calling adt_write_syslog for things that can happen
1047c478bd9Sstevel@tonic-gate  * at high volume.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * syslog's open (openlog) and close (closelog) are interesting;
1077c478bd9Sstevel@tonic-gate  * openlog *may* create a file descriptor and is optional.  closelog
1087c478bd9Sstevel@tonic-gate  * *will* close any open file descriptors and is also optional.
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * Since syslog may also be used by the calling application, the
1117c478bd9Sstevel@tonic-gate  * choice is to avoid openlog, which sets some otherwise useful
1127c478bd9Sstevel@tonic-gate  * parameters, and to embed "Solaris_audit" in the log message.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate void
adt_write_syslog(const char * message,int err)1167c478bd9Sstevel@tonic-gate adt_write_syslog(const char *message, int err)
1177c478bd9Sstevel@tonic-gate {
1189697ae98Sgww 	int	save_errno = errno;
1197c478bd9Sstevel@tonic-gate 	int	mask_priority;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	DPRINTF(("syslog called: %s\n", message));
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	mask_priority = setlogmask(LOG_MASK(LOG_ALERT));
1249697ae98Sgww 	errno = err;
1259697ae98Sgww 	syslog(LOG_ALERT, "Solaris_audit %s: %m", message);
1267c478bd9Sstevel@tonic-gate 	(void) setlogmask(mask_priority);
1277c478bd9Sstevel@tonic-gate 	errno = save_errno;
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
13191b2cbb3Sgww  * return true if c2audit is not excluded.
13291b2cbb3Sgww  *
13391b2cbb3Sgww  *	For purpose of this API, anything but AUC_DISABLED
13491b2cbb3Sgww  *	is enabled; however one never actually sees
13591b2cbb3Sgww  *	AUC_DISABLED since auditon returns ENOTSUP in that case.  Any
13691b2cbb3Sgww  *	auditon error is considered the same as ENOTSUP for our
1377c478bd9Sstevel@tonic-gate  *	purpose.  auditstate is not changed by auditon if an error
1387c478bd9Sstevel@tonic-gate  *	is returned.
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate 
141b08d8a12Sgww /*
142b08d8a12Sgww  * XXX	this should probably be eliminated and adt_audit_state() replace it.
143b08d8a12Sgww  *	All the legitimate uses	are to not fork a waiting process for
144b08d8a12Sgww  *	process exit processing, as in su, login, dtlogin.  Other bogus
145b08d8a12Sgww  *	users are zoneadmd and init.
146b08d8a12Sgww  *	All but dtlogin are in ON, so we can do this without cross gate
147b08d8a12Sgww  *	synchronization.
14891b2cbb3Sgww  *
14991b2cbb3Sgww  *	No longer used in adt.c.
150b08d8a12Sgww  */
151b08d8a12Sgww 
1527c478bd9Sstevel@tonic-gate boolean_t
adt_audit_enabled(void)153b08d8a12Sgww adt_audit_enabled(void)
154b08d8a12Sgww {
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	(void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate));
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	return (auditstate != AUC_DISABLED);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
161b08d8a12Sgww /*
162b08d8a12Sgww  *	See adt_audit_enabled() for state discussions.
163b08d8a12Sgww  *	The state parameter is a hedge until all the uses become clear.
164b08d8a12Sgww  *	Likely if adt_audit_enabled is brought internal to this file,
165f8994074SJan Friedel  *	it could be modified to take one or more parameters to describe the
166f8994074SJan Friedel  *	state.
167b08d8a12Sgww  */
168b08d8a12Sgww 
169b08d8a12Sgww boolean_t
adt_audit_state(int states)170f8994074SJan Friedel adt_audit_state(int states)
171b08d8a12Sgww {
172b08d8a12Sgww 
173b08d8a12Sgww 	(void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate));
174b08d8a12Sgww 
175f8994074SJan Friedel 	return ((auditstate & states) ? B_TRUE : B_FALSE);
176b08d8a12Sgww }
177b08d8a12Sgww 
1787c478bd9Sstevel@tonic-gate /*
179f8994074SJan Friedel  * Get user_specific/non-attributable audit mask. This may be called even when
180f8994074SJan Friedel  * auditing is off.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static int
adt_get_mask_from_user(uid_t uid,au_mask_t * mask)1847c478bd9Sstevel@tonic-gate adt_get_mask_from_user(uid_t uid, au_mask_t *mask)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	struct passwd	pwd;
187f8994074SJan Friedel 	long		buff_sz;
188f8994074SJan Friedel 	char		*pwd_buff;
189f8994074SJan Friedel 
1907c478bd9Sstevel@tonic-gate 
19191b2cbb3Sgww 	if (auditstate & AUC_DISABLED) {
19291b2cbb3Sgww 		/* c2audit excluded */
1937c478bd9Sstevel@tonic-gate 		mask->am_success = 0;
1947c478bd9Sstevel@tonic-gate 		mask->am_failure = 0;
195cd918266Skcrowenex 		return (0);
196cd918266Skcrowenex 	}
197cd918266Skcrowenex 
198cd918266Skcrowenex 	/*
199cd918266Skcrowenex 	 * This function applies the 'attributable' mask, modified by
200cd918266Skcrowenex 	 * any per-user flags, to any user whose UID can be mapped to
201cd918266Skcrowenex 	 * a name via name services.
202cd918266Skcrowenex 	 * Others, such as users with Ephemeral UIDs, or NFS clients
203cd918266Skcrowenex 	 * using AUTH_SYS, get the 'non-attributable mask'.
204cd918266Skcrowenex 	 * This is true even if some _other_ system or service could
205cd918266Skcrowenex 	 * map the ID to a name, or if it could be inferred from
206cd918266Skcrowenex 	 * other records.
207cd918266Skcrowenex 	 * Note that it is possible for records to contain _only_
208cd918266Skcrowenex 	 * an ephemeral ID, which can't be mapped back to a name
209cd918266Skcrowenex 	 * once it becomes invalid (e.g. server reboot).
210cd918266Skcrowenex 	 */
211cd918266Skcrowenex 	if (uid <= MAXUID) {
212f8994074SJan Friedel 		if ((buff_sz = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) {
213f8994074SJan Friedel 			adt_write_syslog("couldn't determine maximum size of "
214f8994074SJan Friedel 			    "password buffer", errno);
215f8994074SJan Friedel 			return (-1);
216f8994074SJan Friedel 		}
217f8994074SJan Friedel 		if ((pwd_buff = calloc(1, (size_t)++buff_sz)) == NULL) {
218f8994074SJan Friedel 			return (-1);
219f8994074SJan Friedel 		}
220cd918266Skcrowenex 		/*
221cd918266Skcrowenex 		 * Ephemeral id's and id's that exist in a name service we
222cd918266Skcrowenex 		 * don't have configured (LDAP, NIS) can't be looked up,
223cd918266Skcrowenex 		 * but either way it's not an error.
224cd918266Skcrowenex 		 */
225cd918266Skcrowenex 		if (getpwuid_r(uid, &pwd, pwd_buff, (int)buff_sz) != NULL) {
226cd918266Skcrowenex 			if (au_user_mask(pwd.pw_name, mask)) {
227cd918266Skcrowenex 				free(pwd_buff);
228cd918266Skcrowenex 				errno = EFAULT; /* undetermined failure */
229cd918266Skcrowenex 				return (-1);
230cd918266Skcrowenex 			}
2314aed303fSChris Love 			free(pwd_buff);
232cd918266Skcrowenex 			return (0);
2337c478bd9Sstevel@tonic-gate 		}
2344aed303fSChris Love 		free(pwd_buff);
235cd918266Skcrowenex 	}
236cd918266Skcrowenex 
237cd918266Skcrowenex 	if (auditon(A_GETKMASK, (caddr_t)mask, sizeof (*mask)) == -1) {
2387c478bd9Sstevel@tonic-gate 			return (-1);
2397c478bd9Sstevel@tonic-gate 	}
240f8994074SJan Friedel 
2417c478bd9Sstevel@tonic-gate 	return (0);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate  * adt_get_unique_id -- generate a hopefully unique 32 bit value
2467c478bd9Sstevel@tonic-gate  *
2477c478bd9Sstevel@tonic-gate  * there will be a follow up to replace this with the use of /dev/random
2487c478bd9Sstevel@tonic-gate  *
2497c478bd9Sstevel@tonic-gate  * An MD5 hash is taken on a buffer of
2507c478bd9Sstevel@tonic-gate  *     hostname . audit id . unix time . pid . count
2517c478bd9Sstevel@tonic-gate  *
2527c478bd9Sstevel@tonic-gate  * "count = noise++;" is subject to a race condition but I don't
2537c478bd9Sstevel@tonic-gate  * see a need to put a lock around it.
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate 
25685e8d33eSgww au_asid_t
adt_get_unique_id(au_id_t uid)2575b7f77adStw adt_get_unique_id(au_id_t uid)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	char		hostname[MAXHOSTNAMELEN];
2607c478bd9Sstevel@tonic-gate 	union {
2617c478bd9Sstevel@tonic-gate 		au_id_t		v[4];
2627c478bd9Sstevel@tonic-gate 		unsigned char	obuff[128/8];
2637c478bd9Sstevel@tonic-gate 	} output;
2647c478bd9Sstevel@tonic-gate 	MD5_CTX	context;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	static int	noise = 0;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	int		count = noise++;
2697c478bd9Sstevel@tonic-gate 	time_t		timebits = time(NULL);
2707c478bd9Sstevel@tonic-gate 	pid_t		pidbits = getpid();
27185e8d33eSgww 	au_asid_t	retval = 0;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (gethostname(hostname, MAXHOSTNAMELEN)) {
2747c478bd9Sstevel@tonic-gate 		adt_write_syslog("gethostname call failed", errno);
2757c478bd9Sstevel@tonic-gate 		(void) strncpy(hostname, "invalidHostName", MAXHOSTNAMELEN);
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	while (retval == 0) {  /* 0 is the only invalid result */
2797c478bd9Sstevel@tonic-gate 		MD5Init(&context);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *)hostname,
2827c478bd9Sstevel@tonic-gate 		    (unsigned int) strlen((const char *)hostname));
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &uid, sizeof (uid_t));
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		MD5Update(&context,
2877c478bd9Sstevel@tonic-gate 		    (unsigned char *) &timebits, sizeof (time_t));
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &pidbits,
2907c478bd9Sstevel@tonic-gate 		    sizeof (pid_t));
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &(count), sizeof (int));
2937c478bd9Sstevel@tonic-gate 		MD5Final(output.obuff, &context);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		retval = output.v[count % 4];
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 	return (retval);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate  * the following "port" function deals with the following issues:
3027c478bd9Sstevel@tonic-gate  *
3037c478bd9Sstevel@tonic-gate  * 1    the kernel and ucred deal with a dev_t as a 64 bit value made
3047c478bd9Sstevel@tonic-gate  *      up from a 32 bit major and 32 bit minor.
3057c478bd9Sstevel@tonic-gate  * 2    User space deals with a dev_t as either the above 64 bit value
3067c478bd9Sstevel@tonic-gate  *      or a 32 bit value made from a 14 bit major and an 18 bit minor.
3077c478bd9Sstevel@tonic-gate  * 3    The various audit interfaces (except ucred) pass the 32 or
3087c478bd9Sstevel@tonic-gate  *      64 bit version depending the architecture of the userspace
3097c478bd9Sstevel@tonic-gate  *      application.  If you get a port value from ucred and pass it
3107c478bd9Sstevel@tonic-gate  *      to the kernel via auditon(), it must be squeezed into a 32
3117c478bd9Sstevel@tonic-gate  *      bit value because the kernel knows the userspace app's bit
3127c478bd9Sstevel@tonic-gate  *      size.
3137c478bd9Sstevel@tonic-gate  *
3147c478bd9Sstevel@tonic-gate  * The internal state structure for adt (adt_internal_state_t) uses
3157c478bd9Sstevel@tonic-gate  * dev_t, so adt converts data from ucred to fit.  The import/export
3167c478bd9Sstevel@tonic-gate  * functions, however, can't know if they are importing/exporting
3177c478bd9Sstevel@tonic-gate  * from 64 or 32 bit applications, so they always send 64 bits and
3187c478bd9Sstevel@tonic-gate  * the 32 bit end(s) are responsible to convert 32 -> 64 -> 32 as
3197c478bd9Sstevel@tonic-gate  * appropriate.
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * adt_cpy_tid() -- if lib is 64 bit, just copy it (dev_t and port are
3247c478bd9Sstevel@tonic-gate  * both 64 bits).  If lib is 32 bits, squeeze the two-int port into
3257c478bd9Sstevel@tonic-gate  * a 32 bit dev_t.  A port fits in the "minor" part of au_port_t,
3267c478bd9Sstevel@tonic-gate  * so it isn't broken up into pieces.  (When it goes to the kernel
3277c478bd9Sstevel@tonic-gate  * and back, however, it will have been split into major/minor
3287c478bd9Sstevel@tonic-gate  * pieces.)
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate static void
adt_cpy_tid(au_tid_addr_t * dest,const au_tid64_addr_t * src)3327c478bd9Sstevel@tonic-gate adt_cpy_tid(au_tid_addr_t *dest, const au_tid64_addr_t *src)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate #ifdef _LP64
3357c478bd9Sstevel@tonic-gate 	(void) memcpy(dest, src, sizeof (au_tid_addr_t));
3369697ae98Sgww #else	/* _LP64 */
3377c478bd9Sstevel@tonic-gate 	dest->at_type = src->at_type;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	dest->at_port  = src->at_port.at_minor & MAXMIN32;
3407c478bd9Sstevel@tonic-gate 	dest->at_port |= (src->at_port.at_major & MAXMAJ32) <<
3417c478bd9Sstevel@tonic-gate 	    NBITSMINOR32;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	(void) memcpy(dest->at_addr, src->at_addr, 4 * sizeof (uint32_t));
3449697ae98Sgww #endif	/* _LP64 */
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * adt_start_session -- create interface handle, create context
3497c478bd9Sstevel@tonic-gate  *
3507c478bd9Sstevel@tonic-gate  * The imported_state input is normally NULL, if not, it represents
3517c478bd9Sstevel@tonic-gate  * a continued session; its values obviate the need for a subsequent
3527c478bd9Sstevel@tonic-gate  * call to adt_set_user().
3537c478bd9Sstevel@tonic-gate  *
35426fba2a6Sgww  * The flag is used to decide how to set the initial state of the session.
35526fba2a6Sgww  * If 0, the session is "no audit" until a call to adt_set_user; if
35626fba2a6Sgww  * ADT_USE_PROC_DATA, the session is built from the process audit
3577c478bd9Sstevel@tonic-gate  * characteristics obtained from the kernel.  If imported_state is
3587c478bd9Sstevel@tonic-gate  * not NULL, the resulting audit mask is an OR of the current process
3597c478bd9Sstevel@tonic-gate  * audit mask and that passed in.
3607c478bd9Sstevel@tonic-gate  *
3617c478bd9Sstevel@tonic-gate  * The basic model is that the caller can use the pointer returned
3627c478bd9Sstevel@tonic-gate  * by adt_start_session whether or not auditing is enabled or an
3637c478bd9Sstevel@tonic-gate  * error was returned.  The functions that take the session handle
3647c478bd9Sstevel@tonic-gate  * as input generally return without doing anything if auditing is
3657c478bd9Sstevel@tonic-gate  * disabled.
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate int
adt_start_session(adt_session_data_t ** new_session,const adt_export_data_t * imported_state,adt_session_flags_t flags)3697c478bd9Sstevel@tonic-gate adt_start_session(adt_session_data_t **new_session,
37026fba2a6Sgww     const adt_export_data_t *imported_state, adt_session_flags_t flags)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
3737c478bd9Sstevel@tonic-gate 	adt_session_flags_t	flgmask = ADT_FLAGS_ALL;
3747c478bd9Sstevel@tonic-gate 
37591b2cbb3Sgww 	/* test and set auditstate */
37691b2cbb3Sgww 	if (adt_audit_state(AUC_DISABLED)) {
37791b2cbb3Sgww 		/* c2audit excluded */
37891b2cbb3Sgww 		*new_session = NULL;
37991b2cbb3Sgww 		return (0);
38091b2cbb3Sgww 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if ((flags & ~flgmask) != 0) {
3837c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3847c478bd9Sstevel@tonic-gate 		goto return_err;
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
38791b2cbb3Sgww 	if ((state = calloc(1, sizeof (adt_internal_state_t))) == NULL) {
3887c478bd9Sstevel@tonic-gate 		goto return_err;
38991b2cbb3Sgww 	}
3907c478bd9Sstevel@tonic-gate 
39191b2cbb3Sgww 	if (adt_init(state, flags & ADT_USE_PROC_DATA) != 0) {
3927c478bd9Sstevel@tonic-gate 		goto return_err_free;    /* errno from adt_init() */
39391b2cbb3Sgww 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	/*
3967c478bd9Sstevel@tonic-gate 	 * The imported state overwrites the initial state if the
3977c478bd9Sstevel@tonic-gate 	 * imported state represents a valid audit trail
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (imported_state != NULL) {
401c529a23fSgww 		if (adt_import(state, imported_state) != 0) {
4027c478bd9Sstevel@tonic-gate 			goto return_err_free;
403c529a23fSgww 		}
40426fba2a6Sgww 	} else if (flags & ADT_USE_PROC_DATA) {
40526fba2a6Sgww 		state->as_session_model = ADT_PROCESS_MODEL;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	state->as_flags = flags;
408dfc7be02SJan Friedel 	DPRINTF(("(%lld) Starting session id = %08X\n",
409dfc7be02SJan Friedel 	    (long long) getpid(), state->as_info.ai_asid));
4107c478bd9Sstevel@tonic-gate 
41191b2cbb3Sgww 	*new_session = (adt_session_data_t *)state;
4127c478bd9Sstevel@tonic-gate 	return (0);
41391b2cbb3Sgww 
4147c478bd9Sstevel@tonic-gate return_err_free:
4157c478bd9Sstevel@tonic-gate 	free(state);
4167c478bd9Sstevel@tonic-gate return_err:
41791b2cbb3Sgww 	*new_session = NULL;
4187c478bd9Sstevel@tonic-gate 	adt_write_syslog("audit session create failed", errno);
4197c478bd9Sstevel@tonic-gate 	return (-1);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4220ad2061eSgww /*
4230ad2061eSgww  * adt_load_table()
4240ad2061eSgww  *
4250ad2061eSgww  * loads the event translation table into the audit session.
4260ad2061eSgww  */
4270ad2061eSgww 
4280ad2061eSgww void
adt_load_table(const adt_session_data_t * session_data,adt_translation_t ** xlate,void (* preload)(au_event_t,adt_event_data_t *))4290ad2061eSgww adt_load_table(const adt_session_data_t *session_data,
4300ad2061eSgww     adt_translation_t **xlate, void (*preload)(au_event_t, adt_event_data_t *))
4310ad2061eSgww {
4320ad2061eSgww 	adt_internal_state_t	*state = (adt_internal_state_t *)session_data;
4330ad2061eSgww 
4340ad2061eSgww 	if (state != NULL) {
4350ad2061eSgww 		assert(state->as_check == ADT_VALID);
4360ad2061eSgww 		state->as_xlate = xlate;
4370ad2061eSgww 		state->as_preload = preload;
4380ad2061eSgww 	}
4390ad2061eSgww }
4400ad2061eSgww 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * adt_get_asid() and adt_set_asid()
4437c478bd9Sstevel@tonic-gate  *
4447c478bd9Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
4457c478bd9Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
4467c478bd9Sstevel@tonic-gate  * adt_proccess_attr()
4477c478bd9Sstevel@tonic-gate  *
4487c478bd9Sstevel@tonic-gate  * neither of these are intended for general use and will likely
4497c478bd9Sstevel@tonic-gate  * remain private interfaces for a long time.  Forever is a long
4507c478bd9Sstevel@tonic-gate  * time.  In the case of adt_set_asid(), you should have a very,
4517c478bd9Sstevel@tonic-gate  * very good reason for setting your own session id.  The process
4527c478bd9Sstevel@tonic-gate  * audit characteristics are not changed by put, use adt_set_proc().
4537c478bd9Sstevel@tonic-gate  *
4547c478bd9Sstevel@tonic-gate  * These are "volatile" (more changable than "evolving") and will
4557c478bd9Sstevel@tonic-gate  * probably change in the S10 period.
4567c478bd9Sstevel@tonic-gate  */
45726fba2a6Sgww 
4587c478bd9Sstevel@tonic-gate void
adt_get_asid(const adt_session_data_t * session_data,au_asid_t * asid)4597c478bd9Sstevel@tonic-gate adt_get_asid(const adt_session_data_t *session_data, au_asid_t *asid)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (session_data == NULL) {
4637c478bd9Sstevel@tonic-gate 		*asid = 0;
4647c478bd9Sstevel@tonic-gate 	} else {
4657c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
4667c478bd9Sstevel@tonic-gate 		    ADT_VALID);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		*asid = ((adt_internal_state_t *)session_data)->as_info.ai_asid;
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate void
adt_set_asid(const adt_session_data_t * session_data,const au_asid_t session_id)47326fba2a6Sgww adt_set_asid(const adt_session_data_t *session_data, const au_asid_t session_id)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
4777c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
4787c478bd9Sstevel@tonic-gate 		    ADT_VALID);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
4817c478bd9Sstevel@tonic-gate 		    ADT_HAVE_ASID;
4827c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_asid =
4837c478bd9Sstevel@tonic-gate 		    session_id;
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * adt_get_auid() and adt_set_auid()
4897c478bd9Sstevel@tonic-gate  *
4907c478bd9Sstevel@tonic-gate  * neither of these are intended for general use and will likely
4917c478bd9Sstevel@tonic-gate  * remain private interfaces for a long time.  Forever is a long
4927c478bd9Sstevel@tonic-gate  * time.  In the case of adt_set_auid(), you should have a very,
4937c478bd9Sstevel@tonic-gate  * very good reason for setting your own audit id.  The process
4947c478bd9Sstevel@tonic-gate  * audit characteristics are not changed by put, use adt_set_proc().
4957c478bd9Sstevel@tonic-gate  */
49626fba2a6Sgww 
4977c478bd9Sstevel@tonic-gate void
adt_get_auid(const adt_session_data_t * session_data,au_id_t * auid)4987c478bd9Sstevel@tonic-gate adt_get_auid(const adt_session_data_t *session_data, au_id_t *auid)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (session_data == NULL) {
5027c478bd9Sstevel@tonic-gate 		*auid = AU_NOAUDITID;
5037c478bd9Sstevel@tonic-gate 	} else {
5047c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5057c478bd9Sstevel@tonic-gate 		    ADT_VALID);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		*auid = ((adt_internal_state_t *)session_data)->as_info.ai_auid;
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate void
adt_set_auid(const adt_session_data_t * session_data,const au_id_t audit_id)5127c478bd9Sstevel@tonic-gate adt_set_auid(const adt_session_data_t *session_data, const au_id_t audit_id)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
5167c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5177c478bd9Sstevel@tonic-gate 		    ADT_VALID);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
5207c478bd9Sstevel@tonic-gate 		    ADT_HAVE_AUID;
5217c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_auid =
5227c478bd9Sstevel@tonic-gate 		    audit_id;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate  * adt_get_termid(), adt_set_termid()
5287c478bd9Sstevel@tonic-gate  *
5297c478bd9Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
5307c478bd9Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
5317c478bd9Sstevel@tonic-gate  * adt_proccess_attr()
5327c478bd9Sstevel@tonic-gate  *
5337c478bd9Sstevel@tonic-gate  * The process  audit characteristics are not changed by put, use
5347c478bd9Sstevel@tonic-gate  * adt_set_proc().
5357c478bd9Sstevel@tonic-gate  */
53626fba2a6Sgww 
5377c478bd9Sstevel@tonic-gate void
adt_get_termid(const adt_session_data_t * session_data,au_tid_addr_t * termid)5387c478bd9Sstevel@tonic-gate adt_get_termid(const adt_session_data_t *session_data, au_tid_addr_t *termid)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (session_data == NULL) {
5427c478bd9Sstevel@tonic-gate 		(void) memset(termid, 0, sizeof (au_tid_addr_t));
5437c478bd9Sstevel@tonic-gate 		termid->at_type = AU_IPv4;
5447c478bd9Sstevel@tonic-gate 	} else {
5457c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5467c478bd9Sstevel@tonic-gate 		    ADT_VALID);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		*termid =
5497c478bd9Sstevel@tonic-gate 		    ((adt_internal_state_t *)session_data)->as_info.ai_termid;
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate void
adt_set_termid(const adt_session_data_t * session_data,const au_tid_addr_t * termid)5547c478bd9Sstevel@tonic-gate adt_set_termid(const adt_session_data_t *session_data,
5557c478bd9Sstevel@tonic-gate     const au_tid_addr_t *termid)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
5597c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5607c478bd9Sstevel@tonic-gate 		    ADT_VALID);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_termid =
5635b7f77adStw 		    *termid;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
5665b7f77adStw 		    ADT_HAVE_TID;
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * adt_get_mask(), adt_set_mask()
5727c478bd9Sstevel@tonic-gate  *
5737c478bd9Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
5747c478bd9Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
5757c478bd9Sstevel@tonic-gate  * adt_proccess_attr()
5767c478bd9Sstevel@tonic-gate  *
5777c478bd9Sstevel@tonic-gate  * The process  audit characteristics are not changed by put, use
5787c478bd9Sstevel@tonic-gate  * adt_set_proc().
5797c478bd9Sstevel@tonic-gate  */
58026fba2a6Sgww 
5817c478bd9Sstevel@tonic-gate void
adt_get_mask(const adt_session_data_t * session_data,au_mask_t * mask)5827c478bd9Sstevel@tonic-gate adt_get_mask(const adt_session_data_t *session_data, au_mask_t *mask)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (session_data == NULL) {
5867c478bd9Sstevel@tonic-gate 		mask->am_success = 0;
5877c478bd9Sstevel@tonic-gate 		mask->am_failure = 0;
5887c478bd9Sstevel@tonic-gate 	} else {
5897c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5907c478bd9Sstevel@tonic-gate 		    ADT_VALID);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		*mask = ((adt_internal_state_t *)session_data)->as_info.ai_mask;
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate void
adt_set_mask(const adt_session_data_t * session_data,const au_mask_t * mask)5977c478bd9Sstevel@tonic-gate adt_set_mask(const adt_session_data_t *session_data, const au_mask_t *mask)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
6017c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
6027c478bd9Sstevel@tonic-gate 		    ADT_VALID);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_mask = *mask;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
6077c478bd9Sstevel@tonic-gate 		    ADT_HAVE_MASK;
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate  * helpers for adt_load_termid
6137c478bd9Sstevel@tonic-gate  */
61426fba2a6Sgww 
615435a78ccSAndy Fiddaman static dev_t
adt_ports_to_at_port(in_port_t remote,in_port_t local)616435a78ccSAndy Fiddaman adt_ports_to_at_port(in_port_t remote, in_port_t local)
617435a78ccSAndy Fiddaman {
618435a78ccSAndy Fiddaman 	dev_t port;
619435a78ccSAndy Fiddaman 
620435a78ccSAndy Fiddaman #ifdef _LP64
621435a78ccSAndy Fiddaman 	dev_t tmp;
622435a78ccSAndy Fiddaman 
623435a78ccSAndy Fiddaman 	/*
624435a78ccSAndy Fiddaman 	 * In 64-bit, at_port is a 64-bit value encoding major/minor
625435a78ccSAndy Fiddaman 	 * device numbers as 32-bits each. However when a 32-bit application
626435a78ccSAndy Fiddaman 	 * subsequently requests the audit address via getaudit_addr(), this
627435a78ccSAndy Fiddaman 	 * value must be capable of being compressed down to a 14-bit major and
628435a78ccSAndy Fiddaman 	 * 18-bit minor number or the call will fail.
629435a78ccSAndy Fiddaman 	 *
630435a78ccSAndy Fiddaman 	 * In order to construct a 32-bit compatible value, the top 14-bits of
631435a78ccSAndy Fiddaman 	 * the remote port are used for the major number and the remaining
632435a78ccSAndy Fiddaman 	 * 2-bits + local port are used for the minor.
633435a78ccSAndy Fiddaman 	 */
634435a78ccSAndy Fiddaman 
635435a78ccSAndy Fiddaman 	tmp = ((remote<<16) | (local));
636435a78ccSAndy Fiddaman 	port = (tmp & MAXMIN32);
637435a78ccSAndy Fiddaman 	port |= (((tmp >> NBITSMINOR32) & MAXMAJ32) << NBITSMINOR64);
638435a78ccSAndy Fiddaman #else
639435a78ccSAndy Fiddaman 	port = ((remote<<16) | (local));
640435a78ccSAndy Fiddaman #endif
641435a78ccSAndy Fiddaman 
642435a78ccSAndy Fiddaman 	return (port);
643435a78ccSAndy Fiddaman }
644435a78ccSAndy Fiddaman 
6457c478bd9Sstevel@tonic-gate static void
adt_do_ipv6_address(struct sockaddr_in6 * peer,struct sockaddr_in6 * sock,au_tid_addr_t * termid)6467c478bd9Sstevel@tonic-gate adt_do_ipv6_address(struct sockaddr_in6 *peer, struct sockaddr_in6 *sock,
6477c478bd9Sstevel@tonic-gate     au_tid_addr_t *termid)
6487c478bd9Sstevel@tonic-gate {
649435a78ccSAndy Fiddaman 	termid->at_port =
650435a78ccSAndy Fiddaman 	    adt_ports_to_at_port(peer->sin6_port, sock->sin6_port);
6517c478bd9Sstevel@tonic-gate 	termid->at_type = AU_IPv6;
6527c478bd9Sstevel@tonic-gate 	(void) memcpy(termid->at_addr, &peer->sin6_addr, 4 * sizeof (uint_t));
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate static void
adt_do_ipv4_address(struct sockaddr_in * peer,struct sockaddr_in * sock,au_tid_addr_t * termid)6567c478bd9Sstevel@tonic-gate adt_do_ipv4_address(struct sockaddr_in *peer, struct sockaddr_in *sock,
6577c478bd9Sstevel@tonic-gate     au_tid_addr_t *termid)
6587c478bd9Sstevel@tonic-gate {
659435a78ccSAndy Fiddaman 	termid->at_port = adt_ports_to_at_port(peer->sin_port, sock->sin_port);
6607c478bd9Sstevel@tonic-gate 	termid->at_type = AU_IPv4;
6617c478bd9Sstevel@tonic-gate 	termid->at_addr[0] = (uint32_t)peer->sin_addr.s_addr;
6627c478bd9Sstevel@tonic-gate 	(void) memset(&(termid->at_addr[1]), 0, 3 * sizeof (uint_t));
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * adt_load_termid:  convenience function; inputs file handle and
6677c478bd9Sstevel@tonic-gate  * outputs an au_tid_addr struct.
6687c478bd9Sstevel@tonic-gate  *
6697c478bd9Sstevel@tonic-gate  * This code was stolen from audit_settid.c; it differs from audit_settid()
6707c478bd9Sstevel@tonic-gate  * in that it does not write the terminal id to the process.
6717c478bd9Sstevel@tonic-gate  */
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate int
adt_load_termid(int fd,adt_termid_t ** termid)6747c478bd9Sstevel@tonic-gate adt_load_termid(int fd, adt_termid_t **termid)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	au_tid_addr_t		*p_term;
6777c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	peer;
6787c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	sock;
6797c478bd9Sstevel@tonic-gate 	int			peerlen = sizeof (peer);
6807c478bd9Sstevel@tonic-gate 	int			socklen = sizeof (sock);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/* get peer name if its a socket, else assume local terminal */
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (getpeername(fd, (struct sockaddr *)&peer, (socklen_t *)&peerlen)
6857c478bd9Sstevel@tonic-gate 	    < 0) {
68691b2cbb3Sgww 		if (errno == ENOTSOCK) {
6877c478bd9Sstevel@tonic-gate 			return (adt_load_hostname(NULL, termid));
68891b2cbb3Sgww 		}
6897c478bd9Sstevel@tonic-gate 		goto return_err;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
69291b2cbb3Sgww 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
6937c478bd9Sstevel@tonic-gate 		goto return_err;
69491b2cbb3Sgww 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	/* get sock name */
6977c478bd9Sstevel@tonic-gate 	if (getsockname(fd, (struct sockaddr *)&sock,
69891b2cbb3Sgww 	    (socklen_t *)&socklen) < 0) {
6997c478bd9Sstevel@tonic-gate 		goto return_err_free;
70091b2cbb3Sgww 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (peer.sin6_family == AF_INET6) {
7037c478bd9Sstevel@tonic-gate 		adt_do_ipv6_address(&peer, &sock, p_term);
7047c478bd9Sstevel@tonic-gate 	} else {
7057c478bd9Sstevel@tonic-gate 		adt_do_ipv4_address((struct sockaddr_in *)&peer,
7067c478bd9Sstevel@tonic-gate 		    (struct sockaddr_in *)&sock, p_term);
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 	*termid = (adt_termid_t *)p_term;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	return (0);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate return_err_free:
7137c478bd9Sstevel@tonic-gate 	free(p_term);
7147c478bd9Sstevel@tonic-gate return_err:
71591b2cbb3Sgww 	*termid = NULL;
7167c478bd9Sstevel@tonic-gate 	return (-1);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate static boolean_t
adt_have_termid(au_tid_addr_t * dest)7207c478bd9Sstevel@tonic-gate adt_have_termid(au_tid_addr_t *dest)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	struct auditinfo_addr	audit_data;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (getaudit_addr(&audit_data, sizeof (audit_data)) < 0) {
7257c478bd9Sstevel@tonic-gate 		adt_write_syslog("getaudit failed", errno);
7267c478bd9Sstevel@tonic-gate 		return (B_FALSE);
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if ((audit_data.ai_termid.at_type == 0) ||
7307c478bd9Sstevel@tonic-gate 	    (audit_data.ai_termid.at_addr[0] |
7317c478bd9Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[1]  |
7327c478bd9Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[2]  |
7337c478bd9Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[3]) == 0)
7347c478bd9Sstevel@tonic-gate 		return (B_FALSE);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	(void) memcpy(dest, &(audit_data.ai_termid),
7377c478bd9Sstevel@tonic-gate 	    sizeof (au_tid_addr_t));
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	return (B_TRUE);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
74291b2cbb3Sgww /*
74391b2cbb3Sgww  * adt_get_hostIP - construct a terminal id from a hostname
74491b2cbb3Sgww  *
74591b2cbb3Sgww  *	Returns	 0 = success
74691b2cbb3Sgww  *		-1 = failure and errno = ENETDOWN with the address
74791b2cbb3Sgww  *		     defaulted to IPv4 loopback.
74891b2cbb3Sgww  */
74991b2cbb3Sgww 
7507c478bd9Sstevel@tonic-gate static int
adt_get_hostIP(const char * hostname,au_tid_addr_t * p_term)7517c478bd9Sstevel@tonic-gate adt_get_hostIP(const char *hostname, au_tid_addr_t *p_term)
7527c478bd9Sstevel@tonic-gate {
7539697ae98Sgww 	struct addrinfo	*ai = NULL;
7549697ae98Sgww 	int	tries = 3;
7559697ae98Sgww 	int	eai_err;
7569697ae98Sgww 
7579697ae98Sgww 	while ((tries-- > 0) &&
7589697ae98Sgww 	    ((eai_err = getaddrinfo(hostname, NULL, NULL, &ai)) != 0)) {
759*0d8e9319SJoshua M. Clulow 		DPRINTF(("getaddrinfo(%s) failed[%s]", hostname,
760*0d8e9319SJoshua M. Clulow 		    gai_strerror(eai_err)));
7619697ae98Sgww 
7629697ae98Sgww 		if (eai_err != EAI_AGAIN) {
7637c478bd9Sstevel@tonic-gate 			break;
7649697ae98Sgww 		}
7659697ae98Sgww 		/* see if resolution becomes available */
7669697ae98Sgww 		(void) sleep(1);
7677c478bd9Sstevel@tonic-gate 	}
7689697ae98Sgww 	if (ai != NULL) {
7699697ae98Sgww 		if (ai->ai_family == AF_INET) {
7709697ae98Sgww 			p_term->at_type = AU_IPv4;
7719697ae98Sgww 			(void) memcpy(p_term->at_addr,
7729697ae98Sgww 			    /* LINTED */
7739697ae98Sgww 			    &((struct sockaddr_in *)ai->ai_addr)->sin_addr,
7749697ae98Sgww 			    AU_IPv4);
7759697ae98Sgww 		} else {
7769697ae98Sgww 			p_term->at_type = AU_IPv6;
7779697ae98Sgww 			(void) memcpy(p_term->at_addr,
7789697ae98Sgww 			    /* LINTED */
7799697ae98Sgww 			    &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
7809697ae98Sgww 			    AU_IPv6);
7819697ae98Sgww 		}
7829697ae98Sgww 		freeaddrinfo(ai);
7839697ae98Sgww 		return (0);
78491b2cbb3Sgww 	} else if (auditstate & (AUC_AUDITING | AUC_NOSPACE)) {
78591b2cbb3Sgww 		auditinfo_addr_t  audit_info;
78691b2cbb3Sgww 
78791b2cbb3Sgww 		/*
78891b2cbb3Sgww 		 * auditd is running so there should be a
78991b2cbb3Sgww 		 * kernel audit context
79091b2cbb3Sgww 		 */
79191b2cbb3Sgww 		if (auditon(A_GETKAUDIT, (caddr_t)&audit_info,
79291b2cbb3Sgww 		    sizeof (audit_info)) < 0) {
79391b2cbb3Sgww 			adt_write_syslog("unable to get kernel audit context",
79491b2cbb3Sgww 			    errno);
79591b2cbb3Sgww 			goto try_interface;
79691b2cbb3Sgww 		}
797*0d8e9319SJoshua M. Clulow 		DPRINTF(("setting Audit IP address to kernel"));
79891b2cbb3Sgww 		*p_term = audit_info.ai_termid;
79991b2cbb3Sgww 		return (0);
80091b2cbb3Sgww 	}
80191b2cbb3Sgww try_interface:
80291b2cbb3Sgww 	{
8039697ae98Sgww 		struct ifaddrlist al;
8049697ae98Sgww 		int	family;
805*0d8e9319SJoshua M. Clulow #ifdef C2_DEBUG
8069697ae98Sgww 		char	ntop[INET6_ADDRSTRLEN];
807*0d8e9319SJoshua M. Clulow #endif
8089697ae98Sgww 
8099697ae98Sgww 		/*
8109697ae98Sgww 		 * getaddrinfo has failed to map the hostname
8119697ae98Sgww 		 * to an IP address, try to get an IP address
81291b2cbb3Sgww 		 * from a local interface.  If none up, default
81391b2cbb3Sgww 		 * to loopback.
8149697ae98Sgww 		 */
8159697ae98Sgww 		family = AF_INET6;
8169697ae98Sgww 		if (adt_get_local_address(family, &al) != 0) {
8179697ae98Sgww 			family = AF_INET;
8189697ae98Sgww 
8199697ae98Sgww 			if (adt_get_local_address(family, &al) != 0) {
8209697ae98Sgww 				adt_write_syslog("adt_get_local_address "
82191b2cbb3Sgww 				    "failed, no Audit IP address available, "
82291b2cbb3Sgww 				    "faking loopback and error",
8239697ae98Sgww 				    errno);
82491b2cbb3Sgww 				IN_SET_LOOPBACK_ADDR(
82591b2cbb3Sgww 				    (struct sockaddr_in *)&(al.addr.addr));
82691b2cbb3Sgww 				(void) memcpy(p_term->at_addr, &al.addr.addr,
82791b2cbb3Sgww 				    AU_IPv4);
82891b2cbb3Sgww 				p_term->at_type = AU_IPv4;
8299697ae98Sgww 				return (-1);
8309697ae98Sgww 			}
8319697ae98Sgww 		}
8329697ae98Sgww 		if (family == AF_INET) {
8339697ae98Sgww 			p_term->at_type = AU_IPv4;
8349697ae98Sgww 			(void) memcpy(p_term->at_addr, &al.addr.addr, AU_IPv4);
8359697ae98Sgww 		} else {
8369697ae98Sgww 			p_term->at_type = AU_IPv6;
8379697ae98Sgww 			(void) memcpy(p_term->at_addr, &al.addr.addr6, AU_IPv6);
8389697ae98Sgww 		}
8397c478bd9Sstevel@tonic-gate 
840*0d8e9319SJoshua M. Clulow 		DPRINTF(("mapping %s to %s", hostname,
841*0d8e9319SJoshua M. Clulow 		    inet_ntop(family, &(al.addr), ntop, sizeof (ntop))));
8429697ae98Sgww 		return (0);
8439697ae98Sgww 	}
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate  * adt_load_hostname() is called when the caller does not have a file
8487c478bd9Sstevel@tonic-gate  * handle that gives access to the socket info or any other way to
8497c478bd9Sstevel@tonic-gate  * pass in both port and ip address.  The hostname input is ignored if
8507c478bd9Sstevel@tonic-gate  * the terminal id has already been set; instead it returns the
8517c478bd9Sstevel@tonic-gate  * existing terminal id.
8527c478bd9Sstevel@tonic-gate  *
85391b2cbb3Sgww  * If c2audit is excluded, success is returned.
85491b2cbb3Sgww  * If the hostname lookup fails, the loopback address is assumed,
85591b2cbb3Sgww  * errno is set to ENETDOWN, this allows the caller to interpret
85691b2cbb3Sgww  * whether failure is fatal, and if not to have a address for the
85791b2cbb3Sgww  * hostname.
85891b2cbb3Sgww  * Otherwise the caller would need to be aware of the audit state.
85991b2cbb3Sgww  *
86091b2cbb3Sgww  * Other errors are ignored if not auditing.
8617c478bd9Sstevel@tonic-gate  */
86226fba2a6Sgww 
8637c478bd9Sstevel@tonic-gate int
adt_load_hostname(const char * hostname,adt_termid_t ** termid)8647c478bd9Sstevel@tonic-gate adt_load_hostname(const char *hostname, adt_termid_t **termid)
8657c478bd9Sstevel@tonic-gate {
8669697ae98Sgww 	char		localhost[MAXHOSTNAMELEN + 1];
8677c478bd9Sstevel@tonic-gate 	au_tid_addr_t	*p_term;
8687c478bd9Sstevel@tonic-gate 
86991b2cbb3Sgww 	if (adt_audit_state(AUC_DISABLED)) {
87091b2cbb3Sgww 		/* c2audit excluded */
87191b2cbb3Sgww 		*termid = NULL;
8727c478bd9Sstevel@tonic-gate 		return (0);
87391b2cbb3Sgww 	}
8747c478bd9Sstevel@tonic-gate 
87591b2cbb3Sgww 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
8767c478bd9Sstevel@tonic-gate 		goto return_err;
87791b2cbb3Sgww 	}
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	if (adt_have_termid(p_term)) {
8807c478bd9Sstevel@tonic-gate 		*termid = (adt_termid_t *)p_term;
8817c478bd9Sstevel@tonic-gate 		return (0);
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 	p_term->at_port = 0;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	if (hostname == NULL || *hostname == '\0') {
8869697ae98Sgww 		(void) sysinfo(SI_HOSTNAME, localhost, MAXHOSTNAMELEN);
8877c478bd9Sstevel@tonic-gate 		hostname = localhost;
8887c478bd9Sstevel@tonic-gate 	}
88991b2cbb3Sgww 	if (adt_get_hostIP(hostname, p_term) == 0) {
89091b2cbb3Sgww 		*termid = (adt_termid_t *)p_term;
89191b2cbb3Sgww 		return (0);
89291b2cbb3Sgww 	} else {
89391b2cbb3Sgww 		*termid = (adt_termid_t *)p_term;
89491b2cbb3Sgww 		return (-1);
89591b2cbb3Sgww 	}
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate return_err:
89891b2cbb3Sgww 	*termid = NULL;
89991b2cbb3Sgww 	if (auditstate & AUC_NOAUDIT) {
9007c478bd9Sstevel@tonic-gate 		return (0);
90191b2cbb3Sgww 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	return (-1);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate  * adt_load_ttyname() is called when the caller does not have a file
9087c478bd9Sstevel@tonic-gate  * handle that gives access to the local terminal or any other way
9097c478bd9Sstevel@tonic-gate  * of determining the device id.  The ttyname input is ignored if
9107c478bd9Sstevel@tonic-gate  * the terminal id has already been set; instead it returns the
9117c478bd9Sstevel@tonic-gate  * existing terminal id.
9127c478bd9Sstevel@tonic-gate  *
91391b2cbb3Sgww  * If c2audit is excluded, success is returned.
91491b2cbb3Sgww  * The local hostname is used for the local IP address.
91591b2cbb3Sgww  * If that hostname lookup fails, the loopback address is assumed,
91691b2cbb3Sgww  * errno is set to ENETDOWN, this allows the caller to interpret
91791b2cbb3Sgww  * whether failure is fatal, and if not to have a address for the
91891b2cbb3Sgww  * hostname.
91991b2cbb3Sgww  * Otherwise the caller would need to be aware of the audit state.
92091b2cbb3Sgww  *
92191b2cbb3Sgww  * Other errors are ignored if not auditing.
9227c478bd9Sstevel@tonic-gate  */
92326fba2a6Sgww 
9247c478bd9Sstevel@tonic-gate int
adt_load_ttyname(const char * ttyname,adt_termid_t ** termid)9257c478bd9Sstevel@tonic-gate adt_load_ttyname(const char *ttyname, adt_termid_t **termid)
9267c478bd9Sstevel@tonic-gate {
9279697ae98Sgww 	char		localhost[MAXHOSTNAMELEN + 1];
9287c478bd9Sstevel@tonic-gate 	au_tid_addr_t	*p_term;
9297c478bd9Sstevel@tonic-gate 	struct stat	stat_buf;
9307c478bd9Sstevel@tonic-gate 
93191b2cbb3Sgww 	if (adt_audit_state(AUC_DISABLED)) {
93291b2cbb3Sgww 		/* c2audit excluded */
93391b2cbb3Sgww 		*termid = NULL;
9347c478bd9Sstevel@tonic-gate 		return (0);
93591b2cbb3Sgww 	}
9367c478bd9Sstevel@tonic-gate 
93791b2cbb3Sgww 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
9387c478bd9Sstevel@tonic-gate 		goto return_err;
93991b2cbb3Sgww 	}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (adt_have_termid(p_term)) {
9427c478bd9Sstevel@tonic-gate 		*termid = (adt_termid_t *)p_term;
9437c478bd9Sstevel@tonic-gate 		return (0);
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	p_term->at_port = 0;
9477c478bd9Sstevel@tonic-gate 
94891b2cbb3Sgww 	if (sysinfo(SI_HOSTNAME, localhost, MAXHOSTNAMELEN) < 0) {
9497c478bd9Sstevel@tonic-gate 		goto return_err_free; /* errno from sysinfo */
95091b2cbb3Sgww 	}
9517c478bd9Sstevel@tonic-gate 
9527554000cSJan Friedel 	if (ttyname != NULL && *ttyname != '\0') {
95391b2cbb3Sgww 		if (stat(ttyname, &stat_buf) < 0) {
9547c478bd9Sstevel@tonic-gate 			goto return_err_free;
95591b2cbb3Sgww 		}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 		p_term->at_port = stat_buf.st_rdev;
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
96091b2cbb3Sgww 	if (adt_get_hostIP(localhost, p_term) == 0) {
96191b2cbb3Sgww 		*termid = (adt_termid_t *)p_term;
96291b2cbb3Sgww 		return (0);
96391b2cbb3Sgww 	} else {
96491b2cbb3Sgww 		*termid = (adt_termid_t *)p_term;
96591b2cbb3Sgww 		return (-1);
96691b2cbb3Sgww 	}
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate return_err_free:
9697c478bd9Sstevel@tonic-gate 	free(p_term);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate return_err:
97291b2cbb3Sgww 	*termid = NULL;
97391b2cbb3Sgww 	if (auditstate & AUC_NOAUDIT) {
9747c478bd9Sstevel@tonic-gate 		return (0);
97591b2cbb3Sgww 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	return (-1);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate  * adt_get_session_id returns a stringified representation of
9827c478bd9Sstevel@tonic-gate  * the audit session id.  See also adt_get_asid() for how to
9837c478bd9Sstevel@tonic-gate  * get the unexpurgated version.  No guarantees as to how long
9847c478bd9Sstevel@tonic-gate  * the returned string will be or its general form; hex for now.
9857c478bd9Sstevel@tonic-gate  *
9867c478bd9Sstevel@tonic-gate  * An empty string is returned if auditing is off; length = 1
9877c478bd9Sstevel@tonic-gate  * and the pointer is valid.
9887c478bd9Sstevel@tonic-gate  *
9897c478bd9Sstevel@tonic-gate  * returns strlen + 1 if buffer is valid; else 0 and errno.
9907c478bd9Sstevel@tonic-gate  */
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate size_t
adt_get_session_id(const adt_session_data_t * session_data,char ** buff)9937c478bd9Sstevel@tonic-gate adt_get_session_id(const adt_session_data_t *session_data, char **buff)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	au_asid_t	session_id;
9967c478bd9Sstevel@tonic-gate 	size_t		length;
9977c478bd9Sstevel@tonic-gate 	/*
9987c478bd9Sstevel@tonic-gate 	 * output is 0x followed by
9997c478bd9Sstevel@tonic-gate 	 * two characters per byte
10007c478bd9Sstevel@tonic-gate 	 * plus terminator,
10017c478bd9Sstevel@tonic-gate 	 * except leading 0's are suppressed, so a few bytes may
10027c478bd9Sstevel@tonic-gate 	 * be unused.
10037c478bd9Sstevel@tonic-gate 	 */
10047c478bd9Sstevel@tonic-gate 	length = 2 + (2 * sizeof (session_id)) + 1;
10057c478bd9Sstevel@tonic-gate 	*buff = malloc(length);
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	if (*buff == NULL) {
1008c529a23fSgww 		return (0);
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 	if (session_data == NULL) { /* NULL is not an error */
10117c478bd9Sstevel@tonic-gate 		**buff = '\0';
1012c529a23fSgww 		return (1);
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 	adt_get_asid(session_data, &session_id);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	length = snprintf(*buff, length, "0x%X", (int)session_id);
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	/* length < 1 is a bug: the session data type may have changed */
10197c478bd9Sstevel@tonic-gate 	assert(length > 0);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	return (length);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate /*
10257c478bd9Sstevel@tonic-gate  * adt_end_session -- close handle, clear context
10267c478bd9Sstevel@tonic-gate  *
10277c478bd9Sstevel@tonic-gate  * if as_check is invalid, no harm, no foul, EXCEPT that this could
10287c478bd9Sstevel@tonic-gate  * be an attempt to free data already free'd, so output to syslog
10297c478bd9Sstevel@tonic-gate  * to help explain why the process cored dumped.
10307c478bd9Sstevel@tonic-gate  */
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate int
adt_end_session(adt_session_data_t * session_data)10337c478bd9Sstevel@tonic-gate adt_end_session(adt_session_data_t *session_data)
10347c478bd9Sstevel@tonic-gate {
10357c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
10387c478bd9Sstevel@tonic-gate 		state = (adt_internal_state_t *)session_data;
1039c529a23fSgww 		if (state->as_check != ADT_VALID) {
10407c478bd9Sstevel@tonic-gate 			adt_write_syslog("freeing invalid data", EINVAL);
1041c529a23fSgww 		} else {
10427c478bd9Sstevel@tonic-gate 			state->as_check = 0;
1043c529a23fSgww 			m_label_free(state->as_label);
10447c478bd9Sstevel@tonic-gate 			free(session_data);
10457c478bd9Sstevel@tonic-gate 		}
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 	/* no errors yet defined */
10487c478bd9Sstevel@tonic-gate 	return (0);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate /*
10527c478bd9Sstevel@tonic-gate  * adt_dup_session -- copy the session data
10537c478bd9Sstevel@tonic-gate  */
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate int
adt_dup_session(const adt_session_data_t * source,adt_session_data_t ** dest)10567c478bd9Sstevel@tonic-gate adt_dup_session(const adt_session_data_t *source, adt_session_data_t **dest)
10577c478bd9Sstevel@tonic-gate {
10587c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*source_state;
1059c529a23fSgww 	adt_internal_state_t	*dest_state = NULL;
10607c478bd9Sstevel@tonic-gate 	int			rc = 0;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	if (source != NULL) {
10637c478bd9Sstevel@tonic-gate 		source_state = (adt_internal_state_t *)source;
10647c478bd9Sstevel@tonic-gate 		assert(source_state->as_check == ADT_VALID);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 		dest_state = malloc(sizeof (adt_internal_state_t));
10677c478bd9Sstevel@tonic-gate 		if (dest_state == NULL) {
10687c478bd9Sstevel@tonic-gate 			rc = -1;
10697c478bd9Sstevel@tonic-gate 			goto return_rc;
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 		(void) memcpy(dest_state, source,
10727c478bd9Sstevel@tonic-gate 		    sizeof (struct adt_internal_state));
1073c529a23fSgww 
1074c529a23fSgww 		if (source_state->as_label != NULL) {
1075c529a23fSgww 			dest_state->as_label = NULL;
1076c529a23fSgww 			if ((rc = m_label_dup(&dest_state->as_label,
1077c529a23fSgww 			    source_state->as_label)) != 0) {
1078c529a23fSgww 				free(dest_state);
1079c529a23fSgww 				dest_state = NULL;
1080c529a23fSgww 			}
1081c529a23fSgww 		}
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate return_rc:
1084c529a23fSgww 	*dest = (adt_session_data_t *)dest_state;
10857c478bd9Sstevel@tonic-gate 	return (rc);
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate /*
10897c478bd9Sstevel@tonic-gate  * from_export_format()
10907c478bd9Sstevel@tonic-gate  * read from a network order buffer into struct adt_session_data
10917c478bd9Sstevel@tonic-gate  */
109226fba2a6Sgww 
10937c478bd9Sstevel@tonic-gate static size_t
adt_from_export_format(adt_internal_state_t * internal,const adt_export_data_t * external)10947c478bd9Sstevel@tonic-gate adt_from_export_format(adt_internal_state_t *internal,
10957c478bd9Sstevel@tonic-gate     const adt_export_data_t *external)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate 	struct export_header	head;
10987c478bd9Sstevel@tonic-gate 	struct export_link	link;
10997c478bd9Sstevel@tonic-gate 	adr_t			context;
1100cd918266Skcrowenex 	int32_t			offset;
1101cd918266Skcrowenex 	int32_t			length;
1102cd918266Skcrowenex 	int32_t			version;
1103c529a23fSgww 	size_t			label_len;
11047c478bd9Sstevel@tonic-gate 	char			*p = (char *)external;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	adrm_start(&context, (char *)external);
11077c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&head, 4);
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	if ((internal->as_check = head.ax_check) != ADT_VALID) {
11107c478bd9Sstevel@tonic-gate 		errno = EINVAL;
11117c478bd9Sstevel@tonic-gate 		return (0);
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 	offset = head.ax_link.ax_offset;
11147c478bd9Sstevel@tonic-gate 	version = head.ax_link.ax_version;
11157c478bd9Sstevel@tonic-gate 	length = head.ax_buffer_length;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	/*
1118c529a23fSgww 	 * Skip newer versions.
11197c478bd9Sstevel@tonic-gate 	 */
1120c529a23fSgww 	while (version > PROTOCOL_VERSION_2) {
1121c529a23fSgww 		if (offset < 1) {
11227c478bd9Sstevel@tonic-gate 			return (0);	/* failed to match version */
1123c529a23fSgww 		}
1124c529a23fSgww 		p += offset;		/* point to next version # */
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 		if (p > (char *)external + length) {
11277c478bd9Sstevel@tonic-gate 			return (0);
11287c478bd9Sstevel@tonic-gate 		}
11297c478bd9Sstevel@tonic-gate 		adrm_start(&context, p);
11307c478bd9Sstevel@tonic-gate 		adrm_int32(&context, (int *)&link, 2);
11317c478bd9Sstevel@tonic-gate 		offset = link.ax_offset;
11327c478bd9Sstevel@tonic-gate 		version = link.ax_version;
11337c478bd9Sstevel@tonic-gate 		assert(version != 0);
11347c478bd9Sstevel@tonic-gate 	}
1135c529a23fSgww 	/*
1136c529a23fSgww 	 * Adjust buffer pointer to the first data item (euid).
1137c529a23fSgww 	 */
1138c529a23fSgww 	if (p == (char *)external) {
11397c478bd9Sstevel@tonic-gate 		adrm_start(&context, (char *)(p + sizeof (head)));
1140c529a23fSgww 	} else {
11417c478bd9Sstevel@tonic-gate 		adrm_start(&context, (char *)(p + sizeof (link)));
1142c529a23fSgww 	}
1143c529a23fSgww 	/*
1144c529a23fSgww 	 * if down rev version, neither pid nor label are included
1145c529a23fSgww 	 * in v1 ax_size_of_tsol_data intentionally ignored
1146c529a23fSgww 	 */
1147c529a23fSgww 	if (version == PROTOCOL_VERSION_1) {
1148c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_euid), 1);
1149c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_ruid), 1);
1150c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_egid), 1);
1151c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_rgid), 1);
1152c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
1153c529a23fSgww 		adrm_int32(&context,
1154c529a23fSgww 		    (int *)&(internal->as_info.ai_mask.am_success), 2);
1155c529a23fSgww 		adrm_int32(&context,
1156c529a23fSgww 		    (int *)&(internal->as_info.ai_termid.at_port), 1);
1157c529a23fSgww 		adrm_int32(&context,
1158c529a23fSgww 		    (int *)&(internal->as_info.ai_termid.at_type), 1);
1159c529a23fSgww 		adrm_int32(&context,
1160c529a23fSgww 		    (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
1161c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
116291b2cbb3Sgww 		adrm_int32(&context, (int *)&(internal->as_audit_state), 1);
1163c529a23fSgww 		internal->as_pid = (pid_t)-1;
1164c529a23fSgww 		internal->as_label = NULL;
1165c529a23fSgww 	} else if (version == PROTOCOL_VERSION_2) {
1166c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_euid), 1);
1167c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_ruid), 1);
1168c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_egid), 1);
1169c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_rgid), 1);
1170c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
1171c529a23fSgww 		adrm_int32(&context,
1172c529a23fSgww 		    (int *)&(internal->as_info.ai_mask.am_success), 2);
1173c529a23fSgww 		adrm_int32(&context,
1174c529a23fSgww 		    (int *)&(internal->as_info.ai_termid.at_port), 1);
1175c529a23fSgww 		adrm_int32(&context,
1176c529a23fSgww 		    (int *)&(internal->as_info.ai_termid.at_type), 1);
1177c529a23fSgww 		adrm_int32(&context,
1178c529a23fSgww 		    (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
1179c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
118091b2cbb3Sgww 		adrm_int32(&context, (int *)&(internal->as_audit_state), 1);
1181c529a23fSgww 		adrm_int32(&context, (int *)&(internal->as_pid), 1);
1182c529a23fSgww 		adrm_int32(&context, (int *)&label_len, 1);
1183c529a23fSgww 		if (label_len > 0) {
1184c529a23fSgww 			/* read in and deal with different sized labels. */
118542096647STony Nguyen 			size32_t my_label_len = blabel_size();
1186c529a23fSgww 
1187c529a23fSgww 			if ((internal->as_label =
1188c529a23fSgww 			    m_label_alloc(MAC_LABEL)) == NULL) {
1189c529a23fSgww 				return (0);
1190c529a23fSgww 			}
1191c529a23fSgww 			if (label_len > my_label_len) {
1192c529a23fSgww 				errno = EINVAL;
1193c529a23fSgww 				m_label_free(internal->as_label);
1194c529a23fSgww 				return (0);
1195c529a23fSgww 			}
1196c529a23fSgww 			(void) memset(internal->as_label, 0, my_label_len);
1197c529a23fSgww 			adrm_int32(&context, (int *)(internal->as_label),
1198c529a23fSgww 			    label_len / sizeof (int32_t));
1199c529a23fSgww 		} else {
1200c529a23fSgww 			internal->as_label = NULL;
1201c529a23fSgww 		}
1202c529a23fSgww 	}
12037c478bd9Sstevel@tonic-gate 
1204c529a23fSgww 	return (length);
12057c478bd9Sstevel@tonic-gate }
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate /*
1208c529a23fSgww  * adt_to_export_format
12097c478bd9Sstevel@tonic-gate  * read from struct adt_session_data into a network order buffer.
12107c478bd9Sstevel@tonic-gate  *
12117c478bd9Sstevel@tonic-gate  * (network order 'cause this data may be shared with a remote host.)
12127c478bd9Sstevel@tonic-gate  */
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate static size_t
adt_to_export_format(adt_export_data_t * external,adt_internal_state_t * internal)12157c478bd9Sstevel@tonic-gate adt_to_export_format(adt_export_data_t *external,
12167c478bd9Sstevel@tonic-gate     adt_internal_state_t *internal)
12177c478bd9Sstevel@tonic-gate {
12187c478bd9Sstevel@tonic-gate 	struct export_header	head;
12197c478bd9Sstevel@tonic-gate 	struct export_link	tail;
12207c478bd9Sstevel@tonic-gate 	adr_t			context;
122142096647STony Nguyen 	size32_t		label_len = 0;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	adrm_start(&context, (char *)external);
12247c478bd9Sstevel@tonic-gate 
1225c529a23fSgww 	if (internal->as_label != NULL) {
1226c529a23fSgww 		label_len = blabel_size();
1227c529a23fSgww 	}
1228c529a23fSgww 
12297c478bd9Sstevel@tonic-gate 	head.ax_check = ADT_VALID;
1230c529a23fSgww 	head.ax_buffer_length = sizeof (struct adt_export_data) + label_len;
1231c529a23fSgww 
1232c529a23fSgww 	/* version 2 first */
1233c529a23fSgww 
1234c529a23fSgww 	head.ax_link.ax_version = PROTOCOL_VERSION_2;
1235c529a23fSgww 	head.ax_link.ax_offset = sizeof (struct export_header) +
1236c529a23fSgww 	    sizeof (struct adt_export_v2) + label_len;
1237c529a23fSgww 
12387c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&head, 4);
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_euid), 1);
12417c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
12427c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_egid), 1);
12437c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
12447c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
12457c478bd9Sstevel@tonic-gate 	adrm_putint32(&context,
12467c478bd9Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_mask.am_success), 2);
12477c478bd9Sstevel@tonic-gate 	adrm_putint32(&context,
12487c478bd9Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_port), 1);
12497c478bd9Sstevel@tonic-gate 	adrm_putint32(&context,
12507c478bd9Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_type), 1);
12517c478bd9Sstevel@tonic-gate 	adrm_putint32(&context,
12527c478bd9Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
12537c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
125491b2cbb3Sgww 	adrm_putint32(&context, (int *)&(internal->as_audit_state), 1);
1255c529a23fSgww 	adrm_putint32(&context, (int *)&(internal->as_pid), 1);
1256c529a23fSgww 	adrm_putint32(&context, (int *)&label_len, 1);
1257c529a23fSgww 	if (internal->as_label != NULL) {
1258c529a23fSgww 		/* serialize the label */
1259c529a23fSgww 		adrm_putint32(&context, (int *)(internal->as_label),
1260c529a23fSgww 		    (label_len / sizeof (int32_t)));
1261c529a23fSgww 	}
1262c529a23fSgww 
1263c529a23fSgww 	/* now version 1 */
1264c529a23fSgww 
1265c529a23fSgww 	tail.ax_version = PROTOCOL_VERSION_1;
1266c529a23fSgww 	tail.ax_offset = 0;
1267c529a23fSgww 
1268c529a23fSgww 	adrm_putint32(&context, (int *)&tail, 2);
1269c529a23fSgww 
1270c529a23fSgww 	adrm_putint32(&context, (int *)&(internal->as_euid), 1);
1271c529a23fSgww 	adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
1272c529a23fSgww 	adrm_putint32(&context, (int *)&(internal->as_egid), 1);
1273c529a23fSgww 	adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
1274c529a23fSgww 	adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
1275c529a23fSgww 	adrm_putint32(&context,
1276c529a23fSgww 	    (int *)&(internal->as_info.ai_mask.am_success), 2);
1277c529a23fSgww 	adrm_putint32(&context,
1278c529a23fSgww 	    (int *)&(internal->as_info.ai_termid.at_port), 1);
1279c529a23fSgww 	adrm_putint32(&context,
1280c529a23fSgww 	    (int *)&(internal->as_info.ai_termid.at_type), 1);
1281c529a23fSgww 	adrm_putint32(&context,
1282c529a23fSgww 	    (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
1283c529a23fSgww 	adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
128491b2cbb3Sgww 	adrm_putint32(&context, (int *)&(internal->as_audit_state), 1);
1285c529a23fSgww 	/* ignored in v1 */
1286c529a23fSgww 	adrm_putint32(&context, (int *)&label_len, 1);
1287c529a23fSgww 
1288c529a23fSgww 	/* finally terminator */
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	tail.ax_version = 0; /* invalid version number */
12917c478bd9Sstevel@tonic-gate 	tail.ax_offset = 0;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&tail, 2);
12947c478bd9Sstevel@tonic-gate 
1295c529a23fSgww 	return (head.ax_buffer_length);
12967c478bd9Sstevel@tonic-gate }
12977c478bd9Sstevel@tonic-gate 
1298c529a23fSgww /*
1299c529a23fSgww  * adt_ucred_label() -- if label is available, duplicate it.
1300c529a23fSgww  */
1301c529a23fSgww 
1302c529a23fSgww static m_label_t *
adt_ucred_label(ucred_t * uc)1303c529a23fSgww adt_ucred_label(ucred_t *uc)
1304c529a23fSgww {
1305c529a23fSgww 	m_label_t	*ul = NULL;
1306c529a23fSgww 
1307c529a23fSgww 	if (ucred_getlabel(uc) != NULL) {
1308c529a23fSgww 		(void) m_label_dup(&ul, ucred_getlabel(uc));
1309c529a23fSgww 	}
1310c529a23fSgww 
1311c529a23fSgww 	return (ul);
1312c529a23fSgww }
1313c529a23fSgww 
13147c478bd9Sstevel@tonic-gate /*
13157c478bd9Sstevel@tonic-gate  * adt_import() -- convert from network order to machine-specific order
13167c478bd9Sstevel@tonic-gate  */
131726fba2a6Sgww 
13187c478bd9Sstevel@tonic-gate static int
adt_import(adt_internal_state_t * internal,const adt_export_data_t * external)13197c478bd9Sstevel@tonic-gate adt_import(adt_internal_state_t *internal, const adt_export_data_t *external)
13207c478bd9Sstevel@tonic-gate {
13217c478bd9Sstevel@tonic-gate 	au_mask_t mask;
13227c478bd9Sstevel@tonic-gate 
132391b2cbb3Sgww 	/* save local audit state */
132491b2cbb3Sgww 	int	local_audit_state = internal->as_audit_state;
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	if (adt_from_export_format(internal, external) < 1)
13277c478bd9Sstevel@tonic-gate 		return (-1); /* errno from adt_from_export_format */
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	/*
13307c478bd9Sstevel@tonic-gate 	 * If audit isn't enabled on the remote, they were unable
13317c478bd9Sstevel@tonic-gate 	 * to generate the audit mask, so generate it based on
13327c478bd9Sstevel@tonic-gate 	 * local configuration.  If the user id has changed, the
13337c478bd9Sstevel@tonic-gate 	 * resulting mask may miss some subtleties that occurred
13347c478bd9Sstevel@tonic-gate 	 * on the remote system.
13357c478bd9Sstevel@tonic-gate 	 *
13367c478bd9Sstevel@tonic-gate 	 * If the remote failed to generate a terminal id, it is not
13377c478bd9Sstevel@tonic-gate 	 * recoverable.
13387c478bd9Sstevel@tonic-gate 	 */
13397c478bd9Sstevel@tonic-gate 
134091b2cbb3Sgww 	if (!(internal->as_audit_state & AUC_DISABLED)) {
13417c478bd9Sstevel@tonic-gate 		if (adt_get_mask_from_user(internal->as_info.ai_auid,
13427c478bd9Sstevel@tonic-gate 		    &(internal->as_info.ai_mask)))
13437c478bd9Sstevel@tonic-gate 			return (-1);
13447c478bd9Sstevel@tonic-gate 		if (internal->as_info.ai_auid != internal->as_ruid) {
13457c478bd9Sstevel@tonic-gate 			if (adt_get_mask_from_user(internal->as_info.ai_auid,
13467c478bd9Sstevel@tonic-gate 			    &mask))
13477c478bd9Sstevel@tonic-gate 				return (-1);
13487c478bd9Sstevel@tonic-gate 			internal->as_info.ai_mask.am_success |=
13497c478bd9Sstevel@tonic-gate 			    mask.am_success;
13507c478bd9Sstevel@tonic-gate 			internal->as_info.ai_mask.am_failure |=
13517c478bd9Sstevel@tonic-gate 			    mask.am_failure;
13527c478bd9Sstevel@tonic-gate 		}
13537c478bd9Sstevel@tonic-gate 	}
135491b2cbb3Sgww 	internal->as_audit_state = local_audit_state;
13557c478bd9Sstevel@tonic-gate 
1356dfc7be02SJan Friedel 	DPRINTF(("(%lld)imported asid = %X %u\n", (long long) getpid(),
13577c478bd9Sstevel@tonic-gate 	    internal->as_info.ai_asid,
13587c478bd9Sstevel@tonic-gate 	    internal->as_info.ai_asid));
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	internal->as_have_user_data = ADT_HAVE_ALL;
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	return (0);
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate /*
13667c478bd9Sstevel@tonic-gate  * adt_export_session_data()
13677c478bd9Sstevel@tonic-gate  * copies a adt_session_data struct into a network order buffer
13687c478bd9Sstevel@tonic-gate  *
13697c478bd9Sstevel@tonic-gate  * In a misconfigured network, the local host may have auditing
13707c478bd9Sstevel@tonic-gate  * off while the destination may have auditing on, so if there
13717c478bd9Sstevel@tonic-gate  * is sufficient memory, a buffer will be returned even in the
13727c478bd9Sstevel@tonic-gate  * audit off case.
13737c478bd9Sstevel@tonic-gate  */
137426fba2a6Sgww 
13757c478bd9Sstevel@tonic-gate size_t
adt_export_session_data(const adt_session_data_t * internal,adt_export_data_t ** external)13767c478bd9Sstevel@tonic-gate adt_export_session_data(const adt_session_data_t *internal,
137726fba2a6Sgww     adt_export_data_t **external)
13787c478bd9Sstevel@tonic-gate {
137942096647STony Nguyen 	size32_t		length = 0;
13807c478bd9Sstevel@tonic-gate 
1381dd6707eeSgww 	if ((internal != NULL) &&
1382dd6707eeSgww 	    ((adt_internal_state_t *)internal)->as_label != NULL) {
1383c529a23fSgww 		length = blabel_size();
1384c529a23fSgww 	}
1385c529a23fSgww 
1386c529a23fSgww 	*external = malloc(sizeof (adt_export_data_t) + length);
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	if (*external == NULL)
1389c529a23fSgww 		return (0);
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	if (internal == NULL) {
1392c529a23fSgww 		adt_internal_state_t	*dummy;
1393c529a23fSgww 
13947c478bd9Sstevel@tonic-gate 		dummy = malloc(sizeof (adt_internal_state_t));
13957c478bd9Sstevel@tonic-gate 		if (dummy == NULL)
13967c478bd9Sstevel@tonic-gate 			goto return_length_free;
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 		if (adt_init(dummy, 0)) { /* 0 == don't copy from proc */
13997c478bd9Sstevel@tonic-gate 			free(dummy);
14007c478bd9Sstevel@tonic-gate 			goto return_length_free;
14017c478bd9Sstevel@tonic-gate 		}
14027c478bd9Sstevel@tonic-gate 		length = adt_to_export_format(*external, dummy);
14037c478bd9Sstevel@tonic-gate 		free(dummy);
14047c478bd9Sstevel@tonic-gate 	} else {
14057c478bd9Sstevel@tonic-gate 		length = adt_to_export_format(*external,
14067c478bd9Sstevel@tonic-gate 		    (adt_internal_state_t *)internal);
14077c478bd9Sstevel@tonic-gate 	}
14087c478bd9Sstevel@tonic-gate 	return (length);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate return_length_free:
14117c478bd9Sstevel@tonic-gate 	free(*external);
14127c478bd9Sstevel@tonic-gate 	*external = NULL;
1413c529a23fSgww 	return (0);
14147c478bd9Sstevel@tonic-gate }
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate static void
adt_setto_unaudited(adt_internal_state_t * state)14177c478bd9Sstevel@tonic-gate adt_setto_unaudited(adt_internal_state_t *state)
14187c478bd9Sstevel@tonic-gate {
141991b2cbb3Sgww 	if (state->as_audit_state & AUC_DISABLED) {
142091b2cbb3Sgww 		state->as_ruid = AU_NOAUDITID;
142191b2cbb3Sgww 		state->as_euid = AU_NOAUDITID;
142291b2cbb3Sgww 		state->as_rgid = AU_NOAUDITID;
142391b2cbb3Sgww 		state->as_egid = AU_NOAUDITID;
142491b2cbb3Sgww 		state->as_pid = (pid_t)-1;
142591b2cbb3Sgww 		state->as_label = NULL;
142691b2cbb3Sgww 	} else {
14277c478bd9Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
14287c478bd9Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 		(void) memset((void *)&(state->as_info.ai_termid), 0,
14317c478bd9Sstevel@tonic-gate 		    sizeof (au_tid_addr_t));
14327c478bd9Sstevel@tonic-gate 		state->as_info.ai_termid.at_type = AU_IPv4;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 		(void) memset((void *)&(state->as_info.ai_mask), 0,
14357c478bd9Sstevel@tonic-gate 		    sizeof (au_mask_t));
14367c478bd9Sstevel@tonic-gate 		state->as_have_user_data = 0;
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate }
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate /*
14417c478bd9Sstevel@tonic-gate  * adt_init -- set session context by copying the audit characteristics
14427c478bd9Sstevel@tonic-gate  * from the proc and picking up current uid/tid information.
14437c478bd9Sstevel@tonic-gate  *
14447c478bd9Sstevel@tonic-gate  * By default, an audit session is based on the process; the default
14457c478bd9Sstevel@tonic-gate  * is overriden by adt_set_user()
14467c478bd9Sstevel@tonic-gate  */
144726fba2a6Sgww 
14487c478bd9Sstevel@tonic-gate static int
adt_init(adt_internal_state_t * state,int use_proc_data)14497c478bd9Sstevel@tonic-gate adt_init(adt_internal_state_t *state, int use_proc_data)
14507c478bd9Sstevel@tonic-gate {
145191b2cbb3Sgww 	/* ensure auditstate is set */
14527c478bd9Sstevel@tonic-gate 
145391b2cbb3Sgww 	(void) adt_audit_state(0);
145491b2cbb3Sgww 	state->as_audit_state = auditstate;
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	if (use_proc_data) {
14577c478bd9Sstevel@tonic-gate 		state->as_ruid = getuid();
14587c478bd9Sstevel@tonic-gate 		state->as_euid = geteuid();
14597c478bd9Sstevel@tonic-gate 		state->as_rgid = getgid();
14607c478bd9Sstevel@tonic-gate 		state->as_egid = getegid();
1461c529a23fSgww 		state->as_pid = getpid();
14627c478bd9Sstevel@tonic-gate 
146391b2cbb3Sgww 		if (!(state->as_audit_state & AUC_DISABLED)) {
14647c478bd9Sstevel@tonic-gate 			const au_tid64_addr_t	*tid;
14657c478bd9Sstevel@tonic-gate 			const au_mask_t		*mask;
146626fba2a6Sgww 			ucred_t			*ucred = ucred_get(P_MYID);
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 			/*
14697c478bd9Sstevel@tonic-gate 			 * Even if the ucred is NULL, the underlying
14707c478bd9Sstevel@tonic-gate 			 * credential may have a valid terminal id; if the
14717c478bd9Sstevel@tonic-gate 			 * terminal id is set, then that's good enough.  An
14727c478bd9Sstevel@tonic-gate 			 * example of where this matters is failed login,
14737c478bd9Sstevel@tonic-gate 			 * where rlogin/telnet sets the terminal id before
14747c478bd9Sstevel@tonic-gate 			 * calling login; login does not load the credential
14757c478bd9Sstevel@tonic-gate 			 * since auth failed.
14767c478bd9Sstevel@tonic-gate 			 */
14777c478bd9Sstevel@tonic-gate 			if (ucred == NULL) {
14787c478bd9Sstevel@tonic-gate 				if (!adt_have_termid(
14797c478bd9Sstevel@tonic-gate 				    &(state->as_info.ai_termid)))
14807c478bd9Sstevel@tonic-gate 					return (-1);
14817c478bd9Sstevel@tonic-gate 			} else {
14827c478bd9Sstevel@tonic-gate 				mask = ucred_getamask(ucred);
14837c478bd9Sstevel@tonic-gate 				if (mask != NULL) {
14847c478bd9Sstevel@tonic-gate 					state->as_info.ai_mask = *mask;
14857c478bd9Sstevel@tonic-gate 				} else {
14867c478bd9Sstevel@tonic-gate 					ucred_free(ucred);
14877c478bd9Sstevel@tonic-gate 					return (-1);
14887c478bd9Sstevel@tonic-gate 				}
14897c478bd9Sstevel@tonic-gate 				tid = ucred_getatid(ucred);
14907c478bd9Sstevel@tonic-gate 				if (tid != NULL) {
14917c478bd9Sstevel@tonic-gate 					adt_cpy_tid(&(state->as_info.ai_termid),
14927c478bd9Sstevel@tonic-gate 					    tid);
14937c478bd9Sstevel@tonic-gate 				} else {
14947c478bd9Sstevel@tonic-gate 					ucred_free(ucred);
14957c478bd9Sstevel@tonic-gate 					return (-1);
14967c478bd9Sstevel@tonic-gate 				}
14977c478bd9Sstevel@tonic-gate 				state->as_info.ai_asid = ucred_getasid(ucred);
14987c478bd9Sstevel@tonic-gate 				state->as_info.ai_auid = ucred_getauid(ucred);
1499c529a23fSgww 				state->as_label = adt_ucred_label(ucred);
15007c478bd9Sstevel@tonic-gate 				ucred_free(ucred);
15017c478bd9Sstevel@tonic-gate 			}
15027c478bd9Sstevel@tonic-gate 			state->as_have_user_data = ADT_HAVE_ALL;
15037c478bd9Sstevel@tonic-gate 		}
15047c478bd9Sstevel@tonic-gate 	} else {
15057c478bd9Sstevel@tonic-gate 		adt_setto_unaudited(state);
15067c478bd9Sstevel@tonic-gate 	}
15077c478bd9Sstevel@tonic-gate 	state->as_session_model = ADT_SESSION_MODEL;	/* default */
15087c478bd9Sstevel@tonic-gate 
150991b2cbb3Sgww 	if ((state->as_audit_state & (AUC_AUDITING | AUC_NOSPACE)) &&
15107c478bd9Sstevel@tonic-gate 	    auditon(A_GETPOLICY, (caddr_t)&(state->as_kernel_audit_policy),
15117c478bd9Sstevel@tonic-gate 	    sizeof (state->as_kernel_audit_policy))) {
15127c478bd9Sstevel@tonic-gate 		return (-1);  /* errno set by auditon */
15137c478bd9Sstevel@tonic-gate 	}
15147c478bd9Sstevel@tonic-gate 	state->as_check = ADT_VALID;
15150ad2061eSgww 	adt_load_table((adt_session_data_t *)state, &adt_xlate_table[0],
15160ad2061eSgww 	    &adt_preload);
15177c478bd9Sstevel@tonic-gate 	return (0);
15187c478bd9Sstevel@tonic-gate }
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate /*
15217c478bd9Sstevel@tonic-gate  * adt_set_proc
15227c478bd9Sstevel@tonic-gate  *
15237c478bd9Sstevel@tonic-gate  * Copy the current session state to the process.  If this function
15247c478bd9Sstevel@tonic-gate  * is called, the model becomes a process model rather than a
15257c478bd9Sstevel@tonic-gate  * session model.
15267c478bd9Sstevel@tonic-gate  *
15277c478bd9Sstevel@tonic-gate  * In the current implementation, the value state->as_have_user_data
15288c3c55e7Spaulson  * must contain all of: ADT_HAVE_{AUID,MASK,TID,ASID}.  These are all set
15298c3c55e7Spaulson  * by adt_set_user() when the ADT_SETTID or ADT_NEW flag is passed in.
15307c478bd9Sstevel@tonic-gate  *
15317c478bd9Sstevel@tonic-gate  */
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate int
adt_set_proc(const adt_session_data_t * session_data)15347c478bd9Sstevel@tonic-gate adt_set_proc(const adt_session_data_t *session_data)
15357c478bd9Sstevel@tonic-gate {
15367c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
15377c478bd9Sstevel@tonic-gate 
153891b2cbb3Sgww 	if (session_data == NULL) {
15397c478bd9Sstevel@tonic-gate 		return (0);
154091b2cbb3Sgww 	}
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
15457c478bd9Sstevel@tonic-gate 
15468c3c55e7Spaulson 	if ((state->as_have_user_data & (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) !=
15478c3c55e7Spaulson 	    (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) {
15487c478bd9Sstevel@tonic-gate 		errno = EINVAL;
15497c478bd9Sstevel@tonic-gate 		goto return_err;
15507c478bd9Sstevel@tonic-gate 	}
15517c478bd9Sstevel@tonic-gate 
15520ad2061eSgww 	if (setaudit_addr((auditinfo_addr_t *)&(state->as_info),
15530ad2061eSgww 	    sizeof (auditinfo_addr_t)) < 0) {
15547c478bd9Sstevel@tonic-gate 		goto return_err;	/* errno set by setaudit_addr() */
15550ad2061eSgww 	}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	state->as_session_model = ADT_PROCESS_MODEL;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	return (0);
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate return_err:
15627c478bd9Sstevel@tonic-gate 	adt_write_syslog("failed to set process audit characteristics", errno);
15637c478bd9Sstevel@tonic-gate 	return (-1);
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate static int
adt_newuser(adt_internal_state_t * state,uid_t ruid,au_tid_addr_t * termid)15677c478bd9Sstevel@tonic-gate adt_newuser(adt_internal_state_t *state, uid_t ruid, au_tid_addr_t *termid)
15687c478bd9Sstevel@tonic-gate {
15697c478bd9Sstevel@tonic-gate 	au_tid_addr_t	no_tid = {0, AU_IPv4, 0, 0, 0, 0};
15707c478bd9Sstevel@tonic-gate 	au_mask_t	no_mask = {0, 0};
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	if (ruid == ADT_NO_AUDIT) {
15737c478bd9Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
15747c478bd9Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
15757c478bd9Sstevel@tonic-gate 		state->as_info.ai_termid = no_tid;
15767c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask = no_mask;
15777c478bd9Sstevel@tonic-gate 		return (0);
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 	state->as_info.ai_auid = ruid;
15807c478bd9Sstevel@tonic-gate 	state->as_info.ai_asid = adt_get_unique_id(ruid);
15817c478bd9Sstevel@tonic-gate 	if (termid != NULL)
15827c478bd9Sstevel@tonic-gate 		state->as_info.ai_termid = *termid;
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask)))
15857c478bd9Sstevel@tonic-gate 		return (-1);
15867c478bd9Sstevel@tonic-gate 
15872e74cda7Sgww 	/* Assume intending to audit as this process */
15882e74cda7Sgww 
15892e74cda7Sgww 	if (state->as_pid == (pid_t)-1)
15902e74cda7Sgww 		state->as_pid = getpid();
15912e74cda7Sgww 
15922e74cda7Sgww 	if (is_system_labeled() && state->as_label == NULL) {
15932e74cda7Sgww 		ucred_t	*ucred = ucred_get(P_MYID);
15942e74cda7Sgww 
15952e74cda7Sgww 		state->as_label = adt_ucred_label(ucred);
15962e74cda7Sgww 		ucred_free(ucred);
15972e74cda7Sgww 	}
15982e74cda7Sgww 
15997c478bd9Sstevel@tonic-gate 	return (0);
16007c478bd9Sstevel@tonic-gate }
160126fba2a6Sgww 
16027c478bd9Sstevel@tonic-gate static int
adt_changeuser(adt_internal_state_t * state,uid_t ruid)16037c478bd9Sstevel@tonic-gate adt_changeuser(adt_internal_state_t *state, uid_t ruid)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate 	au_mask_t		mask;
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	if (!(state->as_have_user_data & ADT_HAVE_AUID))
16087c478bd9Sstevel@tonic-gate 		state->as_info.ai_auid = ruid;
16097c478bd9Sstevel@tonic-gate 	if (!(state->as_have_user_data & ADT_HAVE_ASID))
16107c478bd9Sstevel@tonic-gate 		state->as_info.ai_asid = adt_get_unique_id(ruid);
16117c478bd9Sstevel@tonic-gate 
1612f48205beScasper 	if (ruid <= MAXEPHUID) {
16137c478bd9Sstevel@tonic-gate 		if (adt_get_mask_from_user(ruid, &mask))
16147c478bd9Sstevel@tonic-gate 			return (-1);
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask.am_success |= mask.am_success;
16177c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask.am_failure |= mask.am_failure;
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 	DPRINTF(("changed mask to %08X/%08X for ruid=%d\n",
16205b7f77adStw 	    state->as_info.ai_mask.am_success,
16215b7f77adStw 	    state->as_info.ai_mask.am_failure,
16225b7f77adStw 	    ruid));
16237c478bd9Sstevel@tonic-gate 	return (0);
16247c478bd9Sstevel@tonic-gate }
162526fba2a6Sgww 
16267c478bd9Sstevel@tonic-gate /*
16277c478bd9Sstevel@tonic-gate  * adt_set_user -- see also adt_set_from_ucred()
16287c478bd9Sstevel@tonic-gate  *
16297c478bd9Sstevel@tonic-gate  * ADT_NO_ATTRIB is a valid uid/gid meaning "not known" or
163026fba2a6Sgww  * "unattributed."  If ruid, change the model to session.
16317c478bd9Sstevel@tonic-gate  *
16327c478bd9Sstevel@tonic-gate  * ADT_NO_CHANGE is a valid uid/gid meaning "do not change this value"
16337c478bd9Sstevel@tonic-gate  * only valid with ADT_UPDATE.
16347c478bd9Sstevel@tonic-gate  *
163526fba2a6Sgww  * ADT_NO_AUDIT is the external equivalent to AU_NOAUDITID -- there
16367c478bd9Sstevel@tonic-gate  * isn't a good reason to call adt_set_user() with it unless you don't
16377c478bd9Sstevel@tonic-gate  * have a good value yet and intend to replace it later; auid will be
16387c478bd9Sstevel@tonic-gate  * AU_NOAUDITID.
16397c478bd9Sstevel@tonic-gate  *
16407c478bd9Sstevel@tonic-gate  * adt_set_user should be called even if auditing is not enabled
16417c478bd9Sstevel@tonic-gate  * so that adt_export_session_data() will have useful stuff to
16427c478bd9Sstevel@tonic-gate  * work with.
16437c478bd9Sstevel@tonic-gate  *
16447c478bd9Sstevel@tonic-gate  * See the note preceding adt_set_proc() about the use of ADT_HAVE_TID
16457c478bd9Sstevel@tonic-gate  * and ADT_HAVE_ALL.
16467c478bd9Sstevel@tonic-gate  */
164726fba2a6Sgww 
16487c478bd9Sstevel@tonic-gate int
adt_set_user(const adt_session_data_t * session_data,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,const adt_termid_t * termid,enum adt_user_context user_context)164926fba2a6Sgww adt_set_user(const adt_session_data_t *session_data, uid_t euid, gid_t egid,
165026fba2a6Sgww     uid_t ruid, gid_t rgid, const adt_termid_t *termid,
16517c478bd9Sstevel@tonic-gate     enum adt_user_context user_context)
16527c478bd9Sstevel@tonic-gate {
16537c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
16547c478bd9Sstevel@tonic-gate 	int			rc;
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	if (session_data == NULL) /* no session exists to audit */
16577c478bd9Sstevel@tonic-gate 		return (0);
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
16607c478bd9Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	switch (user_context) {
16637c478bd9Sstevel@tonic-gate 	case ADT_NEW:
16647c478bd9Sstevel@tonic-gate 		if (ruid == ADT_NO_CHANGE || euid == ADT_NO_CHANGE ||
16657c478bd9Sstevel@tonic-gate 		    rgid == ADT_NO_CHANGE || egid == ADT_NO_CHANGE) {
16667c478bd9Sstevel@tonic-gate 			errno = EINVAL;
16677c478bd9Sstevel@tonic-gate 			return (-1);
16687c478bd9Sstevel@tonic-gate 		}
16697c478bd9Sstevel@tonic-gate 		if ((rc = adt_newuser(state, ruid,
16707c478bd9Sstevel@tonic-gate 		    (au_tid_addr_t *)termid)) != 0)
16717c478bd9Sstevel@tonic-gate 			return (rc);
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_ALL;
16747c478bd9Sstevel@tonic-gate 		break;
16757c478bd9Sstevel@tonic-gate 	case ADT_UPDATE:
16767c478bd9Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
16777c478bd9Sstevel@tonic-gate 			errno = EINVAL;
16787c478bd9Sstevel@tonic-gate 			return (-1);
16797c478bd9Sstevel@tonic-gate 		}
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 		if (ruid != ADT_NO_CHANGE)
16827c478bd9Sstevel@tonic-gate 			if ((rc = adt_changeuser(state, ruid)) != 0)
16837c478bd9Sstevel@tonic-gate 				return (rc);
16847c478bd9Sstevel@tonic-gate 		break;
16857c478bd9Sstevel@tonic-gate 	case ADT_USER:
16867c478bd9Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
16877c478bd9Sstevel@tonic-gate 			errno = EINVAL;
16887c478bd9Sstevel@tonic-gate 			return (-1);
16897c478bd9Sstevel@tonic-gate 		}
16907c478bd9Sstevel@tonic-gate 		break;
16917c478bd9Sstevel@tonic-gate 	case ADT_SETTID:
16927c478bd9Sstevel@tonic-gate 		assert(termid != NULL);
16937c478bd9Sstevel@tonic-gate 		state->as_info.ai_termid = *((au_tid_addr_t *)termid);
16947c478bd9Sstevel@tonic-gate 		/* avoid fooling pam_setcred()... */
16957c478bd9Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
16967c478bd9Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
16977c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask.am_failure = 0;
16987c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask.am_success = 0;
16997c478bd9Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_TID |
17007c478bd9Sstevel@tonic-gate 		    ADT_HAVE_AUID | ADT_HAVE_ASID | ADT_HAVE_MASK;
17017c478bd9Sstevel@tonic-gate 		return (0);
17027c478bd9Sstevel@tonic-gate 	default:
17037c478bd9Sstevel@tonic-gate 		errno = EINVAL;
17047c478bd9Sstevel@tonic-gate 		return (-1);
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	if (ruid == ADT_NO_AUDIT) {
17087c478bd9Sstevel@tonic-gate 		state->as_ruid = AU_NOAUDITID;
17097c478bd9Sstevel@tonic-gate 		state->as_euid = AU_NOAUDITID;
17107c478bd9Sstevel@tonic-gate 		state->as_rgid = AU_NOAUDITID;
17117c478bd9Sstevel@tonic-gate 		state->as_egid = AU_NOAUDITID;
17127c478bd9Sstevel@tonic-gate 	} else {
17137c478bd9Sstevel@tonic-gate 		if (ruid != ADT_NO_CHANGE)
17147c478bd9Sstevel@tonic-gate 			state->as_ruid = ruid;
17157c478bd9Sstevel@tonic-gate 		if (euid != ADT_NO_CHANGE)
17167c478bd9Sstevel@tonic-gate 			state->as_euid = euid;
17177c478bd9Sstevel@tonic-gate 		if (rgid != ADT_NO_CHANGE)
17187c478bd9Sstevel@tonic-gate 			state->as_rgid = rgid;
17197c478bd9Sstevel@tonic-gate 		if (egid != ADT_NO_CHANGE)
17207c478bd9Sstevel@tonic-gate 			state->as_egid = egid;
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 
172326fba2a6Sgww 	if (ruid == ADT_NO_ATTRIB) {
172426fba2a6Sgww 		state->as_session_model = ADT_SESSION_MODEL;
172526fba2a6Sgww 	}
172626fba2a6Sgww 
17277c478bd9Sstevel@tonic-gate 	return (0);
17287c478bd9Sstevel@tonic-gate }
172926fba2a6Sgww 
17307c478bd9Sstevel@tonic-gate /*
17317c478bd9Sstevel@tonic-gate  * adt_set_from_ucred()
17327c478bd9Sstevel@tonic-gate  *
17337c478bd9Sstevel@tonic-gate  * an alternate to adt_set_user that fills the same role but uses
17347c478bd9Sstevel@tonic-gate  * a pointer to a ucred rather than a list of id's.  If the ucred
17357c478bd9Sstevel@tonic-gate  * pointer is NULL, use the credential from the this process.
17367c478bd9Sstevel@tonic-gate  *
17377c478bd9Sstevel@tonic-gate  * A key difference is that for ADT_NEW, adt_set_from_ucred() does
17387c478bd9Sstevel@tonic-gate  * not overwrite the asid and auid unless auid has not been set.
17397c478bd9Sstevel@tonic-gate  * ADT_NEW differs from ADT_UPDATE in that it does not OR together
17407c478bd9Sstevel@tonic-gate  * the incoming audit mask with the one that already exists.
17417c478bd9Sstevel@tonic-gate  *
17427c478bd9Sstevel@tonic-gate  * adt_set_from_ucred should be called even if auditing is not enabled
17437c478bd9Sstevel@tonic-gate  * so that adt_export_session_data() will have useful stuff to
17447c478bd9Sstevel@tonic-gate  * work with.
17457c478bd9Sstevel@tonic-gate  */
174626fba2a6Sgww 
17477c478bd9Sstevel@tonic-gate int
adt_set_from_ucred(const adt_session_data_t * session_data,const ucred_t * uc,enum adt_user_context user_context)174826fba2a6Sgww adt_set_from_ucred(const adt_session_data_t *session_data, const ucred_t *uc,
174926fba2a6Sgww     enum adt_user_context user_context)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
17527c478bd9Sstevel@tonic-gate 	int			rc = -1;
17537c478bd9Sstevel@tonic-gate 	const au_tid64_addr_t		*tid64;
17547c478bd9Sstevel@tonic-gate 	au_tid_addr_t		termid, *tid;
175526706799Sme 	ucred_t	*ucred = (ucred_t *)uc;
175626706799Sme 	boolean_t	local_uc = B_FALSE;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	if (session_data == NULL) /* no session exists to audit */
17597c478bd9Sstevel@tonic-gate 		return (0);
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
17627c478bd9Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	if (ucred == NULL) {
1765b3baaabfSgww 		ucred = ucred_get(P_MYID);
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 		if (ucred == NULL)
17687c478bd9Sstevel@tonic-gate 			goto return_rc;
176926706799Sme 		local_uc = B_TRUE;
17707c478bd9Sstevel@tonic-gate 	}
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	switch (user_context) {
17737c478bd9Sstevel@tonic-gate 	case ADT_NEW:
17747c478bd9Sstevel@tonic-gate 		tid64 = ucred_getatid(ucred);
17757c478bd9Sstevel@tonic-gate 		if (tid64 != NULL) {
17767c478bd9Sstevel@tonic-gate 			adt_cpy_tid(&termid, tid64);
17777c478bd9Sstevel@tonic-gate 			tid = &termid;
17787c478bd9Sstevel@tonic-gate 		} else {
17797c478bd9Sstevel@tonic-gate 			tid = NULL;
17807c478bd9Sstevel@tonic-gate 		}
17817c478bd9Sstevel@tonic-gate 		if (ucred_getauid(ucred) == AU_NOAUDITID) {
1782b3baaabfSgww 			adt_setto_unaudited(state);
1783b3baaabfSgww 			state->as_have_user_data = ADT_HAVE_ALL;
1784b3baaabfSgww 			rc = 0;
1785b3baaabfSgww 			goto return_rc;
17867c478bd9Sstevel@tonic-gate 		} else {
17877c478bd9Sstevel@tonic-gate 			state->as_info.ai_auid = ucred_getauid(ucred);
17887c478bd9Sstevel@tonic-gate 			state->as_info.ai_asid = ucred_getasid(ucred);
17897c478bd9Sstevel@tonic-gate 			state->as_info.ai_mask = *ucred_getamask(ucred);
17907c478bd9Sstevel@tonic-gate 			state->as_info.ai_termid = *tid;
17917c478bd9Sstevel@tonic-gate 		}
17927c478bd9Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_ALL;
17937c478bd9Sstevel@tonic-gate 		break;
17947c478bd9Sstevel@tonic-gate 	case ADT_UPDATE:
17957c478bd9Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
17967c478bd9Sstevel@tonic-gate 			errno = EINVAL;
17977c478bd9Sstevel@tonic-gate 			goto return_rc;
17987c478bd9Sstevel@tonic-gate 		}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 		if ((rc = adt_changeuser(state, ucred_getruid(ucred))) != 0)
18017c478bd9Sstevel@tonic-gate 			goto return_rc;
18027c478bd9Sstevel@tonic-gate 		break;
18037c478bd9Sstevel@tonic-gate 	case ADT_USER:
18047c478bd9Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
18057c478bd9Sstevel@tonic-gate 			errno = EINVAL;
18067c478bd9Sstevel@tonic-gate 			goto return_rc;
18077c478bd9Sstevel@tonic-gate 		}
18087c478bd9Sstevel@tonic-gate 		break;
18097c478bd9Sstevel@tonic-gate 	default:
18107c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18117c478bd9Sstevel@tonic-gate 		goto return_rc;
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate 	rc = 0;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	state->as_ruid = ucred_getruid(ucred);
18167c478bd9Sstevel@tonic-gate 	state->as_euid = ucred_geteuid(ucred);
18177c478bd9Sstevel@tonic-gate 	state->as_rgid = ucred_getrgid(ucred);
18187c478bd9Sstevel@tonic-gate 	state->as_egid = ucred_getegid(ucred);
1819c529a23fSgww 	state->as_pid = ucred_getpid(ucred);
1820c529a23fSgww 	state->as_label = adt_ucred_label(ucred);
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate return_rc:
182326706799Sme 	if (local_uc) {
182426706799Sme 		ucred_free(ucred);
182526706799Sme 	}
18267c478bd9Sstevel@tonic-gate 	return (rc);
18277c478bd9Sstevel@tonic-gate }
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate /*
18307c478bd9Sstevel@tonic-gate  * adt_alloc_event() returns a pointer to allocated memory
18317c478bd9Sstevel@tonic-gate  *
18327c478bd9Sstevel@tonic-gate  */
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate adt_event_data_t
adt_alloc_event(const adt_session_data_t * session_data,au_event_t event_id)18357c478bd9Sstevel@tonic-gate *adt_alloc_event(const adt_session_data_t *session_data, au_event_t event_id)
18367c478bd9Sstevel@tonic-gate {
18377c478bd9Sstevel@tonic-gate 	struct adt_event_state	*event_state;
18387c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*session_state;
18397c478bd9Sstevel@tonic-gate 	adt_event_data_t	*return_event = NULL;
18407c478bd9Sstevel@tonic-gate 	/*
18417c478bd9Sstevel@tonic-gate 	 * need to return a valid event pointer even if audit is
18427c478bd9Sstevel@tonic-gate 	 * off, else the caller will end up either (1) keeping its
18437c478bd9Sstevel@tonic-gate 	 * own flags for on/off or (2) writing to a NULL pointer.
18447c478bd9Sstevel@tonic-gate 	 * If auditing is on, the session data must be valid; otherwise
18457c478bd9Sstevel@tonic-gate 	 * we don't care.
18467c478bd9Sstevel@tonic-gate 	 */
18477c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
18487c478bd9Sstevel@tonic-gate 		session_state = (adt_internal_state_t *)session_data;
18497c478bd9Sstevel@tonic-gate 		assert(session_state->as_check == ADT_VALID);
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 	event_state = calloc(1, sizeof (struct adt_event_state));
18527c478bd9Sstevel@tonic-gate 	if (event_state == NULL)
18537c478bd9Sstevel@tonic-gate 		goto return_ptr;
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	event_state->ae_check = ADT_VALID;
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	event_state->ae_event_id = event_id;
18587c478bd9Sstevel@tonic-gate 	event_state->ae_session = (struct adt_internal_state *)session_data;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	return_event = (adt_event_data_t *)&(event_state->ae_event_data);
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	/*
18637c478bd9Sstevel@tonic-gate 	 * preload data so the adt_au_*() functions can detect un-supplied
18647c478bd9Sstevel@tonic-gate 	 * values (0 and NULL are free via calloc()).
18657c478bd9Sstevel@tonic-gate 	 */
18666193acb9Sgww 	if (session_data != NULL) {
18676193acb9Sgww 		session_state->as_preload(event_id, return_event);
18686193acb9Sgww 	}
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate return_ptr:
18717c478bd9Sstevel@tonic-gate 	return (return_event);
18727c478bd9Sstevel@tonic-gate }
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate /*
18757c478bd9Sstevel@tonic-gate  * adt_getXlateTable -- look up translation table address for event id
18767c478bd9Sstevel@tonic-gate  */
18777c478bd9Sstevel@tonic-gate 
18780ad2061eSgww static adt_translation_t *
adt_getXlateTable(adt_translation_t ** xlate,au_event_t event_id)18790ad2061eSgww adt_getXlateTable(adt_translation_t **xlate, au_event_t event_id)
18807c478bd9Sstevel@tonic-gate {
18817c478bd9Sstevel@tonic-gate 	/* xlate_table is global in adt_xlate.c */
18820ad2061eSgww 	adt_translation_t	**p_xlate = xlate;
18830ad2061eSgww 	adt_translation_t	*p_event;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	while (*p_xlate != NULL) {
18867c478bd9Sstevel@tonic-gate 		p_event = *p_xlate;
18877c478bd9Sstevel@tonic-gate 		if (event_id == p_event->tx_external_event)
18887c478bd9Sstevel@tonic-gate 			return (p_event);
18897c478bd9Sstevel@tonic-gate 		p_xlate++;
18907c478bd9Sstevel@tonic-gate 	}
18917c478bd9Sstevel@tonic-gate 	return (NULL);
18927c478bd9Sstevel@tonic-gate }
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate /*
18957c478bd9Sstevel@tonic-gate  * adt_calcOffsets
18967c478bd9Sstevel@tonic-gate  *
18977c478bd9Sstevel@tonic-gate  * the call to this function is surrounded by a mutex.
18987c478bd9Sstevel@tonic-gate  *
18997c478bd9Sstevel@tonic-gate  * i walks down the table picking up next_token.  j walks again to
19007c478bd9Sstevel@tonic-gate  * calculate the offset to the input data.  k points to the next
19017c478bd9Sstevel@tonic-gate  * token's row.  Finally, l, is used to sum the values in the
19027c478bd9Sstevel@tonic-gate  * datadef array.
19037c478bd9Sstevel@tonic-gate  *
19047c478bd9Sstevel@tonic-gate  * What's going on?  The entry array is in the order of the input
19057c478bd9Sstevel@tonic-gate  * fields but the processing of array entries is in the order of
19067c478bd9Sstevel@tonic-gate  * the output (see next_token).  Calculating the offset to the
19077c478bd9Sstevel@tonic-gate  * "next" input can't be done in the outer loop (i) since i doesn't
19087c478bd9Sstevel@tonic-gate  * point to the current entry and it can't be done with the k index
19097c478bd9Sstevel@tonic-gate  * because it doesn't represent the order of input fields.
19107c478bd9Sstevel@tonic-gate  *
19117c478bd9Sstevel@tonic-gate  * While the resulting algorithm is n**2, it is only done once per
19127c478bd9Sstevel@tonic-gate  * event type.
19137c478bd9Sstevel@tonic-gate  */
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate /*
19167c478bd9Sstevel@tonic-gate  * adt_calcOffsets is only called once per event type, but it uses
19177c478bd9Sstevel@tonic-gate  * the address alignment of memory allocated for that event as if it
19187c478bd9Sstevel@tonic-gate  * were the same for all subsequently allocated memory.  This is
19197c478bd9Sstevel@tonic-gate  * guaranteed by calloc/malloc.  Arrays take special handling since
19207c478bd9Sstevel@tonic-gate  * what matters for figuring out the correct alignment is the size
19217c478bd9Sstevel@tonic-gate  * of the array element.
19227c478bd9Sstevel@tonic-gate  */
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate static void
adt_calcOffsets(struct entry * p_entry,int tablesize,void * p_data)19257c478bd9Sstevel@tonic-gate adt_calcOffsets(struct entry *p_entry, int tablesize, void *p_data)
19267c478bd9Sstevel@tonic-gate {
19277c478bd9Sstevel@tonic-gate 	int		i, j;
19287c478bd9Sstevel@tonic-gate 	size_t		this_size, prev_size;
19297c478bd9Sstevel@tonic-gate 	void		*struct_start = p_data;
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	for (i = 0; i < tablesize; i++) {
19327c478bd9Sstevel@tonic-gate 		if (p_entry[i].en_type_def == NULL) {
19337c478bd9Sstevel@tonic-gate 			p_entry[i].en_offset = 0;
19347c478bd9Sstevel@tonic-gate 			continue;
19357c478bd9Sstevel@tonic-gate 		}
19367c478bd9Sstevel@tonic-gate 		prev_size = 0;
19377c478bd9Sstevel@tonic-gate 		p_entry[i].en_offset = (char *)p_data - (char *)struct_start;
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 		for (j = 0; j < p_entry[i].en_count_types; j++) {
19407c478bd9Sstevel@tonic-gate 			if (p_entry[i].en_type_def[j].dd_datatype == ADT_MSG)
19417c478bd9Sstevel@tonic-gate 				this_size = sizeof (enum adt_generic);
19427c478bd9Sstevel@tonic-gate 			else
19437c478bd9Sstevel@tonic-gate 				this_size =
19447c478bd9Sstevel@tonic-gate 				    p_entry[i].en_type_def[j].dd_input_size;
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 			/* adj for first entry */
19477c478bd9Sstevel@tonic-gate 			if (prev_size == 0)
19487c478bd9Sstevel@tonic-gate 				prev_size = this_size;
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 			if (p_entry[i].en_type_def[j].dd_datatype ==
19517c478bd9Sstevel@tonic-gate 			    ADT_UINT32ARRAY) {
19527c478bd9Sstevel@tonic-gate 				p_data = (char *)adt_adjust_address(p_data,
19537c478bd9Sstevel@tonic-gate 				    prev_size, sizeof (uint32_t)) +
19547c478bd9Sstevel@tonic-gate 				    this_size - sizeof (uint32_t);
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 				prev_size = sizeof (uint32_t);
19577c478bd9Sstevel@tonic-gate 			} else {
19587c478bd9Sstevel@tonic-gate 				p_data = adt_adjust_address(p_data, prev_size,
19597c478bd9Sstevel@tonic-gate 				    this_size);
19607c478bd9Sstevel@tonic-gate 				prev_size = this_size;
19617c478bd9Sstevel@tonic-gate 			}
19627c478bd9Sstevel@tonic-gate 		}
19637c478bd9Sstevel@tonic-gate 	}
19647c478bd9Sstevel@tonic-gate }
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate /*
19677c478bd9Sstevel@tonic-gate  * adt_generate_event
19687c478bd9Sstevel@tonic-gate  * generate event record from external struct.  The order is based on
19697c478bd9Sstevel@tonic-gate  * the output tokens, allowing for the possibility that the input data
19707c478bd9Sstevel@tonic-gate  * is in a different order.
19717c478bd9Sstevel@tonic-gate  *
19727c478bd9Sstevel@tonic-gate  */
19737c478bd9Sstevel@tonic-gate 
19740ad2061eSgww static int
adt_generate_event(const adt_event_data_t * p_extdata,struct adt_event_state * p_event,adt_translation_t * p_xlate)19757c478bd9Sstevel@tonic-gate adt_generate_event(const adt_event_data_t *p_extdata,
19767c478bd9Sstevel@tonic-gate     struct adt_event_state *p_event,
19770ad2061eSgww     adt_translation_t *p_xlate)
19787c478bd9Sstevel@tonic-gate {
19797c478bd9Sstevel@tonic-gate 	struct entry		*p_entry;
19807c478bd9Sstevel@tonic-gate 	static mutex_t	lock = DEFAULTMUTEX;
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	p_entry = p_xlate->tx_first_entry;
19837c478bd9Sstevel@tonic-gate 	assert(p_entry != NULL);
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	p_event->ae_internal_id = p_xlate->tx_internal_event;
19867c478bd9Sstevel@tonic-gate 	adt_token_open(p_event);
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	/*
19897c478bd9Sstevel@tonic-gate 	 * offsets are not pre-calculated; the initial offsets are all
19907c478bd9Sstevel@tonic-gate 	 * 0; valid offsets are >= 0.  Offsets for no-input tokens such
19917c478bd9Sstevel@tonic-gate 	 * as subject are set to -1 by adt_calcOffset()
19927c478bd9Sstevel@tonic-gate 	 */
19937c478bd9Sstevel@tonic-gate 	if (p_xlate->tx_offsetsCalculated == 0) {
19947257d1b4Sraf 		(void) mutex_lock(&lock);
19957c478bd9Sstevel@tonic-gate 		p_xlate->tx_offsetsCalculated = 1;
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 		adt_calcOffsets(p_xlate->tx_top_entry, p_xlate->tx_entries,
19987c478bd9Sstevel@tonic-gate 		    (void *)p_extdata);
19997257d1b4Sraf 		(void) mutex_unlock(&lock);
20007c478bd9Sstevel@tonic-gate 	}
20017c478bd9Sstevel@tonic-gate 	while (p_entry != NULL) {
20020ad2061eSgww 		adt_generate_token(p_entry, (char *)p_extdata, p_event);
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 		p_entry = p_entry->en_next_token;
20057c478bd9Sstevel@tonic-gate 	}
20060ad2061eSgww 	return (adt_token_close(p_event));
20077c478bd9Sstevel@tonic-gate }
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate /*
20107c478bd9Sstevel@tonic-gate  * adt_put_event -- main event generation function.
20117c478bd9Sstevel@tonic-gate  * The input "event" is the address of the struct containing
20127c478bd9Sstevel@tonic-gate  * event-specific data.
20137c478bd9Sstevel@tonic-gate  *
20147c478bd9Sstevel@tonic-gate  * However if auditing is off or the session handle
20157c478bd9Sstevel@tonic-gate  * is NULL, no attempt to write a record is made.
20167c478bd9Sstevel@tonic-gate  */
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate int
adt_put_event(const adt_event_data_t * event,int status,int return_val)201926fba2a6Sgww adt_put_event(const adt_event_data_t *event, int status, int return_val)
20207c478bd9Sstevel@tonic-gate {
20217c478bd9Sstevel@tonic-gate 	struct adt_event_state	*event_state;
20220ad2061eSgww 	adt_translation_t	*xlate;
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	if (event == NULL) {
20257c478bd9Sstevel@tonic-gate 		errno = EINVAL;
20260ad2061eSgww 		return (-1);
20277c478bd9Sstevel@tonic-gate 	}
20287c478bd9Sstevel@tonic-gate 	event_state = (struct adt_event_state *)event;
20297c478bd9Sstevel@tonic-gate 
203091b2cbb3Sgww 	/* if this is a broken session or not auditing, exit */
203191b2cbb3Sgww 	if ((event_state->ae_session == NULL) ||
203291b2cbb3Sgww 	    !(event_state->ae_session->as_audit_state &
203391b2cbb3Sgww 	    (AUC_AUDITING | AUC_NOSPACE))) {
20340ad2061eSgww 		return (0);
20350ad2061eSgww 	}
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	assert(event_state->ae_check == ADT_VALID);
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	event_state->ae_rc = status;
20407c478bd9Sstevel@tonic-gate 	event_state->ae_type = return_val;
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	/* look up the event */
20437c478bd9Sstevel@tonic-gate 
20440ad2061eSgww 	xlate = adt_getXlateTable(event_state->ae_session->as_xlate,
20450ad2061eSgww 	    event_state->ae_event_id);
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	if (xlate == NULL) {
20487c478bd9Sstevel@tonic-gate 		errno = EINVAL;
20490ad2061eSgww 		return (-1);
20507c478bd9Sstevel@tonic-gate 	}
20517c478bd9Sstevel@tonic-gate 	DPRINTF(("got event %d\n", xlate->tx_internal_event));
20527c478bd9Sstevel@tonic-gate 
20530ad2061eSgww 	if (adt_selected(event_state, xlate->tx_internal_event, status)) {
20540ad2061eSgww 		return (adt_generate_event(event, event_state, xlate));
20550ad2061eSgww 	}
20567c478bd9Sstevel@tonic-gate 
20570ad2061eSgww 	return (0);
20587c478bd9Sstevel@tonic-gate }
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate /*
20617c478bd9Sstevel@tonic-gate  * adt_free_event -- invalidate and free
20627c478bd9Sstevel@tonic-gate  */
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate void
adt_free_event(adt_event_data_t * event)20657c478bd9Sstevel@tonic-gate adt_free_event(adt_event_data_t *event)
20667c478bd9Sstevel@tonic-gate {
20677c478bd9Sstevel@tonic-gate 	struct adt_event_state	*event_state;
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	if (event == NULL)
20707c478bd9Sstevel@tonic-gate 		return;
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	event_state = (struct adt_event_state *)event;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	assert(event_state->ae_check == ADT_VALID);
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 	event_state->ae_check = 0;
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	free(event_state);
20797c478bd9Sstevel@tonic-gate }
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate /*
20827c478bd9Sstevel@tonic-gate  * adt_is_selected -- helper to adt_selected(), below.
20837c478bd9Sstevel@tonic-gate  *
20847c478bd9Sstevel@tonic-gate  * "sorf" is "success or fail" status; au_preselect compares
20857c478bd9Sstevel@tonic-gate  * that with success, fail, or both.
20867c478bd9Sstevel@tonic-gate  */
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate static int
adt_is_selected(au_event_t e,au_mask_t * m,int sorf)20897c478bd9Sstevel@tonic-gate adt_is_selected(au_event_t e, au_mask_t *m, int sorf)
20907c478bd9Sstevel@tonic-gate {
20917c478bd9Sstevel@tonic-gate 	int prs_sorf;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if (sorf == 0)
20947c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_SUCCESS;
20957c478bd9Sstevel@tonic-gate 	else
20967c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_FAILURE;
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD));
20997c478bd9Sstevel@tonic-gate }
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate /*
21027c478bd9Sstevel@tonic-gate  * selected -- see if this event is preselected.
21037c478bd9Sstevel@tonic-gate  *
21047c478bd9Sstevel@tonic-gate  * if errors are encountered trying to check a preselection mask
21057c478bd9Sstevel@tonic-gate  * or look up a user name, the event is selected.  Otherwise, the
21067c478bd9Sstevel@tonic-gate  * preselection mask is used for the job.
21077c478bd9Sstevel@tonic-gate  */
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate static int
adt_selected(struct adt_event_state * event,au_event_t actual_id,int status)21107c478bd9Sstevel@tonic-gate adt_selected(struct adt_event_state *event, au_event_t actual_id, int status)
21117c478bd9Sstevel@tonic-gate {
21127c478bd9Sstevel@tonic-gate 	adt_internal_state_t *sp;
21137c478bd9Sstevel@tonic-gate 	au_mask_t	namask;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	sp = event->ae_session;
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	if ((sp->as_have_user_data & ADT_HAVE_IDS) == 0) {
21187c478bd9Sstevel@tonic-gate 		adt_write_syslog("No user data available", EINVAL);
21197c478bd9Sstevel@tonic-gate 		return (1);	/* default is "selected" */
21207c478bd9Sstevel@tonic-gate 	}
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	/* non-attributable? */
21237c478bd9Sstevel@tonic-gate 	if ((sp->as_info.ai_auid == AU_NOAUDITID) ||
21247c478bd9Sstevel@tonic-gate 	    (sp->as_info.ai_auid == ADT_NO_AUDIT)) {
21257c478bd9Sstevel@tonic-gate 		if (auditon(A_GETKMASK, (caddr_t)&namask,
21267c478bd9Sstevel@tonic-gate 		    sizeof (namask)) != 0) {
21277c478bd9Sstevel@tonic-gate 			adt_write_syslog("auditon failure", errno);
21287c478bd9Sstevel@tonic-gate 			return (1);
21297c478bd9Sstevel@tonic-gate 		}
21307c478bd9Sstevel@tonic-gate 		return (adt_is_selected(actual_id, &namask, status));
21317c478bd9Sstevel@tonic-gate 	} else {
21327c478bd9Sstevel@tonic-gate 		return (adt_is_selected(actual_id, &(sp->as_info.ai_mask),
21337c478bd9Sstevel@tonic-gate 		    status));
21347c478bd9Sstevel@tonic-gate 	}
21357c478bd9Sstevel@tonic-gate }
21369697ae98Sgww 
21379697ae98Sgww /*
21389697ae98Sgww  * Can't map the host name to an IP address in
21399697ae98Sgww  * adt_get_hostIP.  Get something off an interface
21409697ae98Sgww  * to act as the hosts IP address for auditing.
21419697ae98Sgww  */
21429697ae98Sgww 
214391b2cbb3Sgww static int
adt_get_local_address(int family,struct ifaddrlist * al)21449697ae98Sgww adt_get_local_address(int family, struct ifaddrlist *al)
21459697ae98Sgww {
21469697ae98Sgww 	struct ifaddrlist	*ifal;
21479697ae98Sgww 	char	errbuf[ERRBUFSIZE] = "empty list";
21489697ae98Sgww 	char	msg[ERRBUFSIZE + 512];
21499697ae98Sgww 	int	ifal_count;
21509697ae98Sgww 	int	i;
21519697ae98Sgww 
2152413d88ffSgww 	if ((ifal_count = ifaddrlist(&ifal, family, 0, errbuf)) < 0) {
21539697ae98Sgww 		int serrno = errno;
21549697ae98Sgww 
21559697ae98Sgww 		(void) snprintf(msg, sizeof (msg), "adt_get_local_address "
21569697ae98Sgww 		    "couldn't get %d addrlist %s", family, errbuf);
21579697ae98Sgww 		adt_write_syslog(msg, serrno);
21589697ae98Sgww 		errno = serrno;
21599697ae98Sgww 		return (-1);
21609697ae98Sgww 	}
21619697ae98Sgww 
21629697ae98Sgww 	for (i = 0; i < ifal_count; i++) {
21639697ae98Sgww 		/*
21649697ae98Sgww 		 * loopback always defined,
21659697ae98Sgww 		 * even if there is no real address
21669697ae98Sgww 		 */
21679697ae98Sgww 		if ((ifal[i].flags & (IFF_UP | IFF_LOOPBACK)) == IFF_UP) {
21689697ae98Sgww 			break;
21699697ae98Sgww 		}
21709697ae98Sgww 	}
21719697ae98Sgww 	if (i >= ifal_count) {
21729697ae98Sgww 		free(ifal);
21739697ae98Sgww 		/*
21749697ae98Sgww 		 * Callers of adt_get_hostIP() can only return
21759697ae98Sgww 		 * errno to their callers and eventually the application.
21769697ae98Sgww 		 * Picked one that seemed least worse for saying no
21779697ae98Sgww 		 * usable address for Audit terminal ID.
21789697ae98Sgww 		 */
21799697ae98Sgww 		errno = ENETDOWN;
21809697ae98Sgww 		return (-1);
21819697ae98Sgww 	}
21829697ae98Sgww 
21839697ae98Sgww 	*al = ifal[i];
21849697ae98Sgww 	free(ifal);
21859697ae98Sgww 	return (0);
21869697ae98Sgww }
2187