xref: /titanic_54/usr/src/lib/libbsm/common/adt.c (revision 26706799998ca499307df9f2515e8be432a95eda)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*26706799Sme23304  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
307c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h>
317c478bd9Sstevel@tonic-gate #include <assert.h>
327c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
337c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
347c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
357c478bd9Sstevel@tonic-gate #include <door.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <generic.h>
387c478bd9Sstevel@tonic-gate #include <md5.h>
397c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
407c478bd9Sstevel@tonic-gate #include <netdb.h>
417c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
427c478bd9Sstevel@tonic-gate #include <pwd.h>
437c478bd9Sstevel@tonic-gate #include <sys/stat.h>
447c478bd9Sstevel@tonic-gate #include <time.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <synch.h>
487c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
497c478bd9Sstevel@tonic-gate #include <syslog.h>
507c478bd9Sstevel@tonic-gate #include <thread.h>
517c478bd9Sstevel@tonic-gate #include <unistd.h>
527c478bd9Sstevel@tonic-gate #include <adt_xlate.h>
537c478bd9Sstevel@tonic-gate #include <adt_ucred.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int adt_selected(struct adt_event_state *, au_event_t, int);
567c478bd9Sstevel@tonic-gate static int adt_init(adt_internal_state_t *, int);
577c478bd9Sstevel@tonic-gate static int adt_import(adt_internal_state_t *, const adt_export_data_t *);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #ifdef C2_DEBUG
607c478bd9Sstevel@tonic-gate #define	DPRINTF(x) {printf x; }
617c478bd9Sstevel@tonic-gate #define	DFLUSH fflush(stdout);
627c478bd9Sstevel@tonic-gate #else
637c478bd9Sstevel@tonic-gate #define	DPRINTF(x)
647c478bd9Sstevel@tonic-gate #define	DFLUSH
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate extern int _mutex_lock(mutex_t *);
687c478bd9Sstevel@tonic-gate extern int _mutex_unlock(mutex_t *);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static int auditstate = AUC_DISABLED;	/* default state */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * adt_write_syslog
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  * errors that are not the user's fault (bugs or whatever in
767c478bd9Sstevel@tonic-gate  * the underlying audit code are noted in syslog.)
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  * Avoid calling adt_write_syslog for things that can happen
797c478bd9Sstevel@tonic-gate  * at high volume.
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  * syslog's open (openlog) and close (closelog) are interesting;
827c478bd9Sstevel@tonic-gate  * openlog *may* create a file descriptor and is optional.  closelog
837c478bd9Sstevel@tonic-gate  * *will* close any open file descriptors and is also optional.
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  * Since syslog may also be used by the calling application, the
867c478bd9Sstevel@tonic-gate  * choice is to avoid openlog, which sets some otherwise useful
877c478bd9Sstevel@tonic-gate  * parameters, and to embed "Solaris_audit" in the log message.
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate void
917c478bd9Sstevel@tonic-gate adt_write_syslog(const char *message, int err)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	int	save_errno;
947c478bd9Sstevel@tonic-gate 	int	mask_priority;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	save_errno = errno;
977c478bd9Sstevel@tonic-gate 	errno = err;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	DPRINTF(("syslog called: %s\n", message));
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	mask_priority = setlogmask(LOG_MASK(LOG_ALERT));
1027c478bd9Sstevel@tonic-gate 	syslog(LOG_ALERT, "Solaris_audit %s: %m", message, err);
1037c478bd9Sstevel@tonic-gate 	(void) setlogmask(mask_priority);
1047c478bd9Sstevel@tonic-gate 	errno = save_errno;
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * return true if audit is enabled.  "Enabled" is any state
1097c478bd9Sstevel@tonic-gate  * other than AUC_DISABLED.
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  * states are
1127c478bd9Sstevel@tonic-gate  *		AUC_INIT_AUDIT	-- c2audit queuing enabled.
1137c478bd9Sstevel@tonic-gate  *		AUC_AUDITING	-- up and running
1147c478bd9Sstevel@tonic-gate  *		AUC_DISABLED	-- no audit subsystem loaded
1157c478bd9Sstevel@tonic-gate  *		AUC_UNSET	-- early boot state
1167c478bd9Sstevel@tonic-gate  *		AUC_NOAUDIT	-- subsystem loaded, turned off via
1177c478bd9Sstevel@tonic-gate  *				   auditon(A_SETCOND...)
1187c478bd9Sstevel@tonic-gate  *		AUC_NOSPACE	-- up and running, but log partitions are full
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  *	For purpose of this API, anything but AUC_DISABLED or
1217c478bd9Sstevel@tonic-gate  *	AUC_UNSET is enabled; however one never actually sees
1227c478bd9Sstevel@tonic-gate  *	AUC_DISABLED since auditon returns EINVAL in that case.  Any
1237c478bd9Sstevel@tonic-gate  *	auditon error is considered the same as EINVAL for our
1247c478bd9Sstevel@tonic-gate  *	purpose.  auditstate is not changed by auditon if an error
1257c478bd9Sstevel@tonic-gate  *	is returned.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate boolean_t
1297c478bd9Sstevel@tonic-gate adt_audit_enabled(void) {
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	(void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate));
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	return (auditstate != AUC_DISABLED);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * The man page for getpwuid_r says the buffer must be big enough
1387c478bd9Sstevel@tonic-gate  * or ERANGE will be returned, but offers no guidance for how big
1397c478bd9Sstevel@tonic-gate  * the buffer should be or a way to calculate it.  If you get
1407c478bd9Sstevel@tonic-gate  * ERANGE, double pwd_buff's size.
1417c478bd9Sstevel@tonic-gate  *
1427c478bd9Sstevel@tonic-gate  * This may be called even when auditing is off.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #define	NAFLAG_LEN 512
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static int
1487c478bd9Sstevel@tonic-gate adt_get_mask_from_user(uid_t uid, au_mask_t *mask)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	struct passwd	pwd;
1517c478bd9Sstevel@tonic-gate 	char		pwd_buff[NSS_BUFSIZ];
1527c478bd9Sstevel@tonic-gate 	char		naflag_buf[NAFLAG_LEN];
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (auditstate == AUC_DISABLED) {
1557c478bd9Sstevel@tonic-gate 		mask->am_success = 0;
1567c478bd9Sstevel@tonic-gate 		mask->am_failure = 0;
1577c478bd9Sstevel@tonic-gate 	} else if (uid >= 0) {
1587c478bd9Sstevel@tonic-gate 		if (getpwuid_r(uid, &pwd, pwd_buff, NSS_BUFSIZ) == NULL) {
1597c478bd9Sstevel@tonic-gate 			/*
1607c478bd9Sstevel@tonic-gate 			 * getpwuid_r returns NULL without setting
1617c478bd9Sstevel@tonic-gate 			 * errno if the user does not exist; only
1627c478bd9Sstevel@tonic-gate 			 * if the input is the wrong length does it
1637c478bd9Sstevel@tonic-gate 			 * set errno.
1647c478bd9Sstevel@tonic-gate 			 */
1657c478bd9Sstevel@tonic-gate 			if (errno != ERANGE)
1667c478bd9Sstevel@tonic-gate 				errno = EINVAL;
1677c478bd9Sstevel@tonic-gate 			return (-1);
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 		if (au_user_mask(pwd.pw_name, mask)) {
1707c478bd9Sstevel@tonic-gate 			errno = EFAULT; /* undetermined failure */
1717c478bd9Sstevel@tonic-gate 			return (-1);
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	} else if (getacna(naflag_buf, NAFLAG_LEN - 1) == 0) {
1747c478bd9Sstevel@tonic-gate 		if (getauditflagsbin(naflag_buf, mask))
1757c478bd9Sstevel@tonic-gate 			return (-1);
1767c478bd9Sstevel@tonic-gate 	} else {
1777c478bd9Sstevel@tonic-gate 		return (-1);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	return (0);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * adt_get_unique_id -- generate a hopefully unique 32 bit value
1847c478bd9Sstevel@tonic-gate  *
1857c478bd9Sstevel@tonic-gate  * there will be a follow up to replace this with the use of /dev/random
1867c478bd9Sstevel@tonic-gate  *
1877c478bd9Sstevel@tonic-gate  * An MD5 hash is taken on a buffer of
1887c478bd9Sstevel@tonic-gate  *     hostname . audit id . unix time . pid . count
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  * "count = noise++;" is subject to a race condition but I don't
1917c478bd9Sstevel@tonic-gate  * see a need to put a lock around it.
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static au_id_t
1957c478bd9Sstevel@tonic-gate adt_get_unique_id(uid_t uid)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	char		hostname[MAXHOSTNAMELEN];
1987c478bd9Sstevel@tonic-gate 	union {
1997c478bd9Sstevel@tonic-gate 		au_id_t		v[4];
2007c478bd9Sstevel@tonic-gate 		unsigned char	obuff[128/8];
2017c478bd9Sstevel@tonic-gate 	} output;
2027c478bd9Sstevel@tonic-gate 	MD5_CTX	context;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	static int	noise = 0;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	int		count = noise++;
2077c478bd9Sstevel@tonic-gate 	time_t		timebits = time(NULL);
2087c478bd9Sstevel@tonic-gate 	pid_t		pidbits = getpid();
2097c478bd9Sstevel@tonic-gate 	au_id_t		retval = 0;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (gethostname(hostname, MAXHOSTNAMELEN)) {
2127c478bd9Sstevel@tonic-gate 		adt_write_syslog("gethostname call failed", errno);
2137c478bd9Sstevel@tonic-gate 		(void) strncpy(hostname, "invalidHostName", MAXHOSTNAMELEN);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	while (retval == 0) {  /* 0 is the only invalid result */
2177c478bd9Sstevel@tonic-gate 		MD5Init(&context);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *)hostname,
2207c478bd9Sstevel@tonic-gate 		    (unsigned int) strlen((const char *)hostname));
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &uid, sizeof (uid_t));
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		MD5Update(&context,
2257c478bd9Sstevel@tonic-gate 		    (unsigned char *) &timebits, sizeof (time_t));
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &pidbits,
2287c478bd9Sstevel@tonic-gate 		    sizeof (pid_t));
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &(count), sizeof (int));
2317c478bd9Sstevel@tonic-gate 		MD5Final(output.obuff, &context);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		retval = output.v[count % 4];
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 	return (retval);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * the following "port" function deals with the following issues:
2407c478bd9Sstevel@tonic-gate  *
2417c478bd9Sstevel@tonic-gate  * 1    the kernel and ucred deal with a dev_t as a 64 bit value made
2427c478bd9Sstevel@tonic-gate  *      up from a 32 bit major and 32 bit minor.
2437c478bd9Sstevel@tonic-gate  * 2    User space deals with a dev_t as either the above 64 bit value
2447c478bd9Sstevel@tonic-gate  *      or a 32 bit value made from a 14 bit major and an 18 bit minor.
2457c478bd9Sstevel@tonic-gate  * 3    The various audit interfaces (except ucred) pass the 32 or
2467c478bd9Sstevel@tonic-gate  *      64 bit version depending the architecture of the userspace
2477c478bd9Sstevel@tonic-gate  *      application.  If you get a port value from ucred and pass it
2487c478bd9Sstevel@tonic-gate  *      to the kernel via auditon(), it must be squeezed into a 32
2497c478bd9Sstevel@tonic-gate  *      bit value because the kernel knows the userspace app's bit
2507c478bd9Sstevel@tonic-gate  *      size.
2517c478bd9Sstevel@tonic-gate  *
2527c478bd9Sstevel@tonic-gate  * The internal state structure for adt (adt_internal_state_t) uses
2537c478bd9Sstevel@tonic-gate  * dev_t, so adt converts data from ucred to fit.  The import/export
2547c478bd9Sstevel@tonic-gate  * functions, however, can't know if they are importing/exporting
2557c478bd9Sstevel@tonic-gate  * from 64 or 32 bit applications, so they always send 64 bits and
2567c478bd9Sstevel@tonic-gate  * the 32 bit end(s) are responsible to convert 32 -> 64 -> 32 as
2577c478bd9Sstevel@tonic-gate  * appropriate.
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * adt_cpy_tid() -- if lib is 64 bit, just copy it (dev_t and port are
2627c478bd9Sstevel@tonic-gate  * both 64 bits).  If lib is 32 bits, squeeze the two-int port into
2637c478bd9Sstevel@tonic-gate  * a 32 bit dev_t.  A port fits in the "minor" part of au_port_t,
2647c478bd9Sstevel@tonic-gate  * so it isn't broken up into pieces.  (When it goes to the kernel
2657c478bd9Sstevel@tonic-gate  * and back, however, it will have been split into major/minor
2667c478bd9Sstevel@tonic-gate  * pieces.)
2677c478bd9Sstevel@tonic-gate  */
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static void
2707c478bd9Sstevel@tonic-gate adt_cpy_tid(au_tid_addr_t *dest, const au_tid64_addr_t *src)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate #ifdef _LP64
2737c478bd9Sstevel@tonic-gate 	(void) memcpy(dest, src, sizeof (au_tid_addr_t));
2747c478bd9Sstevel@tonic-gate #else
2757c478bd9Sstevel@tonic-gate 	dest->at_type = src->at_type;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	dest->at_port  = src->at_port.at_minor & MAXMIN32;
2787c478bd9Sstevel@tonic-gate 	dest->at_port |= (src->at_port.at_major & MAXMAJ32) <<
2797c478bd9Sstevel@tonic-gate 	    NBITSMINOR32;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	(void) memcpy(dest->at_addr, src->at_addr, 4 * sizeof (uint32_t));
2827c478bd9Sstevel@tonic-gate #endif
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * adt_start_session -- create interface handle, create context
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  * The imported_state input is normally NULL, if not, it represents
2897c478bd9Sstevel@tonic-gate  * a continued session; its values obviate the need for a subsequent
2907c478bd9Sstevel@tonic-gate  * call to adt_set_user().
2917c478bd9Sstevel@tonic-gate  *
2927c478bd9Sstevel@tonic-gate  * The flag ADT_USE_PROC_DATA is used to decide how to set the initial
2937c478bd9Sstevel@tonic-gate  * state of the session.  If 0, the session is "no audit" until a call
2947c478bd9Sstevel@tonic-gate  * to adt_set_user; if 1, the session is built from the process audit
2957c478bd9Sstevel@tonic-gate  * characteristics obtained from the kernel.  If imported_state is
2967c478bd9Sstevel@tonic-gate  * not NULL, the resulting audit mask is an OR of the current process
2977c478bd9Sstevel@tonic-gate  * audit mask and that passed in.
2987c478bd9Sstevel@tonic-gate  *
2997c478bd9Sstevel@tonic-gate  * The basic model is that the caller can use the pointer returned
3007c478bd9Sstevel@tonic-gate  * by adt_start_session whether or not auditing is enabled or an
3017c478bd9Sstevel@tonic-gate  * error was returned.  The functions that take the session handle
3027c478bd9Sstevel@tonic-gate  * as input generally return without doing anything if auditing is
3037c478bd9Sstevel@tonic-gate  * disabled.
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate int
3077c478bd9Sstevel@tonic-gate adt_start_session(adt_session_data_t **new_session,
3087c478bd9Sstevel@tonic-gate     const adt_export_data_t *imported_state,
3097c478bd9Sstevel@tonic-gate     adt_session_flags_t flags)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
3127c478bd9Sstevel@tonic-gate 	adt_session_flags_t	flgmask = ADT_FLAGS_ALL;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	*new_session = NULL;	/* assume failure */
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/* ensure that auditstate is set */
3177c478bd9Sstevel@tonic-gate 	(void) adt_audit_enabled();
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if ((flags & ~flgmask) != 0) {
3207c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3217c478bd9Sstevel@tonic-gate 		goto return_err;
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	state = calloc(1, sizeof (adt_internal_state_t));
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (state == NULL)
3267c478bd9Sstevel@tonic-gate 		goto return_err;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (adt_init(state, flags & ADT_USE_PROC_DATA) != 0)
3297c478bd9Sstevel@tonic-gate 		goto return_err_free;    /* errno from adt_init() */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * The imported state overwrites the initial state if the
3337c478bd9Sstevel@tonic-gate 	 * imported state represents a valid audit trail
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (imported_state != NULL) {
3377c478bd9Sstevel@tonic-gate 		if (adt_import(state, imported_state) != 0)
3387c478bd9Sstevel@tonic-gate 			goto return_err_free;
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 	state->as_flags = flags;
3417c478bd9Sstevel@tonic-gate 	DPRINTF(("(%d) Starting session id = %08X\n",
3427c478bd9Sstevel@tonic-gate 	    getpid(), state->as_info.ai_asid));
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (state->as_audit_enabled)
3457c478bd9Sstevel@tonic-gate 		*new_session = (adt_session_data_t *)state;
3467c478bd9Sstevel@tonic-gate 	else
3477c478bd9Sstevel@tonic-gate 		free(state);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	return (0);
3507c478bd9Sstevel@tonic-gate return_err_free:
3517c478bd9Sstevel@tonic-gate 	free(state);
3527c478bd9Sstevel@tonic-gate return_err:
3537c478bd9Sstevel@tonic-gate 	adt_write_syslog("audit session create failed", errno);
3547c478bd9Sstevel@tonic-gate 	return (-1);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * adt_get_asid() and adt_set_asid()
3597c478bd9Sstevel@tonic-gate  *
3607c478bd9Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
3617c478bd9Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
3627c478bd9Sstevel@tonic-gate  * adt_proccess_attr()
3637c478bd9Sstevel@tonic-gate  *
3647c478bd9Sstevel@tonic-gate  * neither of these are intended for general use and will likely
3657c478bd9Sstevel@tonic-gate  * remain private interfaces for a long time.  Forever is a long
3667c478bd9Sstevel@tonic-gate  * time.  In the case of adt_set_asid(), you should have a very,
3677c478bd9Sstevel@tonic-gate  * very good reason for setting your own session id.  The process
3687c478bd9Sstevel@tonic-gate  * audit characteristics are not changed by put, use adt_set_proc().
3697c478bd9Sstevel@tonic-gate  *
3707c478bd9Sstevel@tonic-gate  * These are "volatile" (more changable than "evolving") and will
3717c478bd9Sstevel@tonic-gate  * probably change in the S10 period.
3727c478bd9Sstevel@tonic-gate  */
3737c478bd9Sstevel@tonic-gate void
3747c478bd9Sstevel@tonic-gate adt_get_asid(const adt_session_data_t *session_data, au_asid_t *asid)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (session_data == NULL) {
3787c478bd9Sstevel@tonic-gate 		*asid = 0;
3797c478bd9Sstevel@tonic-gate 	} else {
3807c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
3817c478bd9Sstevel@tonic-gate 		    ADT_VALID);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		*asid = ((adt_internal_state_t *)session_data)->as_info.ai_asid;
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate void
3887c478bd9Sstevel@tonic-gate adt_set_asid(const adt_session_data_t *session_data,
3897c478bd9Sstevel@tonic-gate     const au_asid_t session_id)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
3937c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
3947c478bd9Sstevel@tonic-gate 		    ADT_VALID);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
3977c478bd9Sstevel@tonic-gate 		    ADT_HAVE_ASID;
3987c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_asid =
3997c478bd9Sstevel@tonic-gate 		    session_id;
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate  * adt_get_auid() and adt_set_auid()
4057c478bd9Sstevel@tonic-gate  *
4067c478bd9Sstevel@tonic-gate  * neither of these are intended for general use and will likely
4077c478bd9Sstevel@tonic-gate  * remain private interfaces for a long time.  Forever is a long
4087c478bd9Sstevel@tonic-gate  * time.  In the case of adt_set_auid(), you should have a very,
4097c478bd9Sstevel@tonic-gate  * very good reason for setting your own audit id.  The process
4107c478bd9Sstevel@tonic-gate  * audit characteristics are not changed by put, use adt_set_proc().
4117c478bd9Sstevel@tonic-gate  */
4127c478bd9Sstevel@tonic-gate void
4137c478bd9Sstevel@tonic-gate adt_get_auid(const adt_session_data_t *session_data, au_id_t *auid)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (session_data == NULL) {
4177c478bd9Sstevel@tonic-gate 		*auid = AU_NOAUDITID;
4187c478bd9Sstevel@tonic-gate 	} else {
4197c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
4207c478bd9Sstevel@tonic-gate 		    ADT_VALID);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		*auid = ((adt_internal_state_t *)session_data)->as_info.ai_auid;
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate void
4277c478bd9Sstevel@tonic-gate adt_set_auid(const adt_session_data_t *session_data, const au_id_t audit_id)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
4317c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
4327c478bd9Sstevel@tonic-gate 		    ADT_VALID);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
4357c478bd9Sstevel@tonic-gate 		    ADT_HAVE_AUID;
4367c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_auid =
4377c478bd9Sstevel@tonic-gate 		    audit_id;
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * adt_get_termid(), adt_set_termid()
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  * The process  audit characteristics are not changed by put, use
4497c478bd9Sstevel@tonic-gate  * adt_set_proc().
4507c478bd9Sstevel@tonic-gate  */
4517c478bd9Sstevel@tonic-gate void
4527c478bd9Sstevel@tonic-gate adt_get_termid(const adt_session_data_t *session_data, au_tid_addr_t *termid)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (session_data == NULL) {
4567c478bd9Sstevel@tonic-gate 		(void) memset(termid, 0, sizeof (au_tid_addr_t));
4577c478bd9Sstevel@tonic-gate 		termid->at_type = AU_IPv4;
4587c478bd9Sstevel@tonic-gate 	} else {
4597c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
4607c478bd9Sstevel@tonic-gate 		    ADT_VALID);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		*termid =
4637c478bd9Sstevel@tonic-gate 		    ((adt_internal_state_t *)session_data)->as_info.ai_termid;
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate void
4687c478bd9Sstevel@tonic-gate adt_set_termid(const adt_session_data_t *session_data,
4697c478bd9Sstevel@tonic-gate     const au_tid_addr_t *termid)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
4737c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
4747c478bd9Sstevel@tonic-gate 		    ADT_VALID);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_termid =
4777c478bd9Sstevel@tonic-gate 			*termid;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
4807c478bd9Sstevel@tonic-gate 			ADT_HAVE_TID;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate  * adt_get_mask(), adt_set_mask()
4867c478bd9Sstevel@tonic-gate  *
4877c478bd9Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
4887c478bd9Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
4897c478bd9Sstevel@tonic-gate  * adt_proccess_attr()
4907c478bd9Sstevel@tonic-gate  *
4917c478bd9Sstevel@tonic-gate  * The process  audit characteristics are not changed by put, use
4927c478bd9Sstevel@tonic-gate  * adt_set_proc().
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate void
4957c478bd9Sstevel@tonic-gate adt_get_mask(const adt_session_data_t *session_data, au_mask_t *mask)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if (session_data == NULL) {
4997c478bd9Sstevel@tonic-gate 		mask->am_success = 0;
5007c478bd9Sstevel@tonic-gate 		mask->am_failure = 0;
5017c478bd9Sstevel@tonic-gate 	} else {
5027c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5037c478bd9Sstevel@tonic-gate 		    ADT_VALID);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		*mask = ((adt_internal_state_t *)session_data)->as_info.ai_mask;
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate void
5107c478bd9Sstevel@tonic-gate adt_set_mask(const adt_session_data_t *session_data, const au_mask_t *mask)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
5147c478bd9Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5157c478bd9Sstevel@tonic-gate 		    ADT_VALID);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_mask = *mask;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
5207c478bd9Sstevel@tonic-gate 		    ADT_HAVE_MASK;
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate  * helpers for adt_load_termid
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate static void
5287c478bd9Sstevel@tonic-gate adt_do_ipv6_address(struct sockaddr_in6 *peer, struct sockaddr_in6 *sock,
5297c478bd9Sstevel@tonic-gate     au_tid_addr_t *termid)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	termid->at_port = ((peer->sin6_port<<16) | (sock->sin6_port));
5337c478bd9Sstevel@tonic-gate 	termid->at_type = AU_IPv6;
5347c478bd9Sstevel@tonic-gate 	(void) memcpy(termid->at_addr, &peer->sin6_addr, 4 * sizeof (uint_t));
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static void
5387c478bd9Sstevel@tonic-gate adt_do_ipv4_address(struct sockaddr_in *peer, struct sockaddr_in *sock,
5397c478bd9Sstevel@tonic-gate     au_tid_addr_t *termid)
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	termid->at_port = ((peer->sin_port<<16) | (sock->sin_port));
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	termid->at_type = AU_IPv4;
5457c478bd9Sstevel@tonic-gate 	termid->at_addr[0] = (uint32_t)peer->sin_addr.s_addr;
5467c478bd9Sstevel@tonic-gate 	(void) memset(&(termid->at_addr[1]), 0, 3 * sizeof (uint_t));
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate /*
5507c478bd9Sstevel@tonic-gate  * adt_load_termid:  convenience function; inputs file handle and
5517c478bd9Sstevel@tonic-gate  * outputs an au_tid_addr struct.
5527c478bd9Sstevel@tonic-gate  *
5537c478bd9Sstevel@tonic-gate  * This code was stolen from audit_settid.c; it differs from audit_settid()
5547c478bd9Sstevel@tonic-gate  * in that it does not write the terminal id to the process.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate int
5587c478bd9Sstevel@tonic-gate adt_load_termid(int fd, adt_termid_t **termid)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	au_tid_addr_t		*p_term;
5617c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	peer;
5627c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	sock;
5637c478bd9Sstevel@tonic-gate 	int			peerlen = sizeof (peer);
5647c478bd9Sstevel@tonic-gate 	int			socklen = sizeof (sock);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	*termid = NULL;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	/* get peer name if its a socket, else assume local terminal */
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (getpeername(fd, (struct sockaddr *)&peer, (socklen_t *)&peerlen)
5717c478bd9Sstevel@tonic-gate 	    < 0) {
5727c478bd9Sstevel@tonic-gate 		if (errno == ENOTSOCK)
5737c478bd9Sstevel@tonic-gate 			return (adt_load_hostname(NULL, termid));
5747c478bd9Sstevel@tonic-gate 		goto return_err;
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL)
5787c478bd9Sstevel@tonic-gate 		goto return_err;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/* get sock name */
5817c478bd9Sstevel@tonic-gate 	if (getsockname(fd, (struct sockaddr *)&sock,
5827c478bd9Sstevel@tonic-gate 	    (socklen_t *)&socklen) < 0)
5837c478bd9Sstevel@tonic-gate 		goto return_err_free;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (peer.sin6_family == AF_INET6) {
5867c478bd9Sstevel@tonic-gate 		adt_do_ipv6_address(&peer, &sock, p_term);
5877c478bd9Sstevel@tonic-gate 	} else {
5887c478bd9Sstevel@tonic-gate 		adt_do_ipv4_address((struct sockaddr_in *)&peer,
5897c478bd9Sstevel@tonic-gate 		    (struct sockaddr_in *)&sock, p_term);
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 	*termid = (adt_termid_t *)p_term;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	return (0);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate return_err_free:
5967c478bd9Sstevel@tonic-gate 	free(p_term);
5977c478bd9Sstevel@tonic-gate return_err:
5987c478bd9Sstevel@tonic-gate 	return (-1);
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate static boolean_t
6027c478bd9Sstevel@tonic-gate adt_have_termid(au_tid_addr_t *dest)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	struct auditinfo_addr	audit_data;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (getaudit_addr(&audit_data, sizeof (audit_data)) < 0) {
6077c478bd9Sstevel@tonic-gate 		adt_write_syslog("getaudit failed", errno);
6087c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	if ((audit_data.ai_termid.at_type == 0) ||
6127c478bd9Sstevel@tonic-gate 	    (audit_data.ai_termid.at_addr[0] |
6137c478bd9Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[1]  |
6147c478bd9Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[2]  |
6157c478bd9Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[3]) == 0)
6167c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	(void) memcpy(dest, &(audit_data.ai_termid),
6197c478bd9Sstevel@tonic-gate 	    sizeof (au_tid_addr_t));
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	return (B_TRUE);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate static int
6257c478bd9Sstevel@tonic-gate adt_get_hostIP(const char *hostname, au_tid_addr_t *p_term)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	struct addrinfo	*ai;
6287c478bd9Sstevel@tonic-gate 	void		*p;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if (getaddrinfo(hostname, NULL, NULL, &ai) != 0)
6317c478bd9Sstevel@tonic-gate 		return (-1);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	switch (ai->ai_family) {
6347c478bd9Sstevel@tonic-gate 		case AF_INET:
6357c478bd9Sstevel@tonic-gate 			/* LINTED */
6367c478bd9Sstevel@tonic-gate 			p = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
6377c478bd9Sstevel@tonic-gate 			(void) memcpy(p_term->at_addr, p,
6387c478bd9Sstevel@tonic-gate 			    sizeof (((struct sockaddr_in *)NULL)->sin_addr));
6397c478bd9Sstevel@tonic-gate 			p_term->at_type = AU_IPv4;
6407c478bd9Sstevel@tonic-gate 			break;
6417c478bd9Sstevel@tonic-gate 		case AF_INET6:
6427c478bd9Sstevel@tonic-gate 			/* LINTED */
6437c478bd9Sstevel@tonic-gate 			p = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
6447c478bd9Sstevel@tonic-gate 			(void) memcpy(p_term->at_addr, p,
6457c478bd9Sstevel@tonic-gate 			    sizeof (((struct sockaddr_in6 *)NULL)->sin6_addr));
6467c478bd9Sstevel@tonic-gate 			p_term->at_type = AU_IPv6;
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 		default:
6497c478bd9Sstevel@tonic-gate 			return (-1);
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	freeaddrinfo(ai);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	return (0);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate /*
6587c478bd9Sstevel@tonic-gate  * adt_load_hostname() is called when the caller does not have a file
6597c478bd9Sstevel@tonic-gate  * handle that gives access to the socket info or any other way to
6607c478bd9Sstevel@tonic-gate  * pass in both port and ip address.  The hostname input is ignored if
6617c478bd9Sstevel@tonic-gate  * the terminal id has already been set; instead it returns the
6627c478bd9Sstevel@tonic-gate  * existing terminal id.
6637c478bd9Sstevel@tonic-gate  *
6647c478bd9Sstevel@tonic-gate  * If audit is off and the hostname lookup fails, no error is
6657c478bd9Sstevel@tonic-gate  * returned, since an error may be interpreted by the caller
6667c478bd9Sstevel@tonic-gate  * as grounds for denying a login.  Otherwise the caller would
6677c478bd9Sstevel@tonic-gate  * need to be aware of the audit state.
6687c478bd9Sstevel@tonic-gate  */
6697c478bd9Sstevel@tonic-gate int
6707c478bd9Sstevel@tonic-gate adt_load_hostname(const char *hostname, adt_termid_t **termid)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate 	char		localhost[ADT_STRING_MAX + 1];
6737c478bd9Sstevel@tonic-gate 	au_tid_addr_t	*p_term;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	*termid = NULL;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (!adt_audit_enabled())
6787c478bd9Sstevel@tonic-gate 		return (0);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL)
6817c478bd9Sstevel@tonic-gate 		goto return_err;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if (adt_have_termid(p_term)) {
6847c478bd9Sstevel@tonic-gate 		*termid = (adt_termid_t *)p_term;
6857c478bd9Sstevel@tonic-gate 		return (0);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	p_term->at_port = 0;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	if (hostname == NULL || *hostname == '\0') {
6907c478bd9Sstevel@tonic-gate 		(void) sysinfo(SI_HOSTNAME, localhost, ADT_STRING_MAX);
6917c478bd9Sstevel@tonic-gate 		hostname = localhost;
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 	if (adt_get_hostIP(hostname, p_term))
6947c478bd9Sstevel@tonic-gate 		goto return_err_free;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	*termid = (adt_termid_t *)p_term;
6977c478bd9Sstevel@tonic-gate 	return (0);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate return_err_free:
7007c478bd9Sstevel@tonic-gate 	free(p_term);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate return_err:
7037c478bd9Sstevel@tonic-gate 	if ((auditstate == AUC_DISABLED) ||
7047c478bd9Sstevel@tonic-gate 	    (auditstate == AUC_NOAUDIT))
7057c478bd9Sstevel@tonic-gate 		return (0);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	return (-1);
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate  * adt_load_ttyname() is called when the caller does not have a file
7127c478bd9Sstevel@tonic-gate  * handle that gives access to the local terminal or any other way
7137c478bd9Sstevel@tonic-gate  * of determining the device id.  The ttyname input is ignored if
7147c478bd9Sstevel@tonic-gate  * the terminal id has already been set; instead it returns the
7157c478bd9Sstevel@tonic-gate  * existing terminal id.
7167c478bd9Sstevel@tonic-gate  *
7177c478bd9Sstevel@tonic-gate  * If audit is off and the ttyname lookup fails, no error is
7187c478bd9Sstevel@tonic-gate  * returned, since an error may be interpreted by the caller
7197c478bd9Sstevel@tonic-gate  * as grounds for denying a login.  Otherwise the caller would
7207c478bd9Sstevel@tonic-gate  * need to be aware of the audit state.
7217c478bd9Sstevel@tonic-gate  */
7227c478bd9Sstevel@tonic-gate int
7237c478bd9Sstevel@tonic-gate adt_load_ttyname(const char *ttyname, adt_termid_t **termid)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	char		localhost[ADT_STRING_MAX + 1];
7267c478bd9Sstevel@tonic-gate 	au_tid_addr_t	*p_term;
7277c478bd9Sstevel@tonic-gate 	struct stat	stat_buf;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	*termid = NULL;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (!adt_audit_enabled())
7327c478bd9Sstevel@tonic-gate 		return (0);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL)
7357c478bd9Sstevel@tonic-gate 		goto return_err;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	if (adt_have_termid(p_term)) {
7387c478bd9Sstevel@tonic-gate 		*termid = (adt_termid_t *)p_term;
7397c478bd9Sstevel@tonic-gate 		return (0);
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	p_term->at_port = 0;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_HOSTNAME, localhost, ADT_STRING_MAX) < 0)
7457c478bd9Sstevel@tonic-gate 		goto return_err_free; /* errno from sysinfo */
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if (ttyname != NULL) {
7487c478bd9Sstevel@tonic-gate 		if (stat(ttyname, &stat_buf) < 0)
7497c478bd9Sstevel@tonic-gate 			goto return_err_free;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		p_term->at_port = stat_buf.st_rdev;
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if (adt_get_hostIP(localhost, p_term))
7557c478bd9Sstevel@tonic-gate 		goto return_err_free;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	*termid = (adt_termid_t *)p_term;
7587c478bd9Sstevel@tonic-gate 	return (0);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate return_err_free:
7617c478bd9Sstevel@tonic-gate 	free(p_term);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate return_err:
7647c478bd9Sstevel@tonic-gate 	if ((auditstate == AUC_DISABLED) ||
7657c478bd9Sstevel@tonic-gate 	    (auditstate == AUC_NOAUDIT))
7667c478bd9Sstevel@tonic-gate 		return (0);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	return (-1);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate /*
7727c478bd9Sstevel@tonic-gate  * adt_get_session_id returns a stringified representation of
7737c478bd9Sstevel@tonic-gate  * the audit session id.  See also adt_get_asid() for how to
7747c478bd9Sstevel@tonic-gate  * get the unexpurgated version.  No guarantees as to how long
7757c478bd9Sstevel@tonic-gate  * the returned string will be or its general form; hex for now.
7767c478bd9Sstevel@tonic-gate  *
7777c478bd9Sstevel@tonic-gate  * An empty string is returned if auditing is off; length = 1
7787c478bd9Sstevel@tonic-gate  * and the pointer is valid.
7797c478bd9Sstevel@tonic-gate  *
7807c478bd9Sstevel@tonic-gate  * returns strlen + 1 if buffer is valid; else 0 and errno.
7817c478bd9Sstevel@tonic-gate  */
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate size_t
7847c478bd9Sstevel@tonic-gate adt_get_session_id(const adt_session_data_t *session_data, char **buff)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	au_asid_t	session_id;
7877c478bd9Sstevel@tonic-gate 	size_t		length;
7887c478bd9Sstevel@tonic-gate 	/*
7897c478bd9Sstevel@tonic-gate 	 * output is 0x followed by
7907c478bd9Sstevel@tonic-gate 	 * two characters per byte
7917c478bd9Sstevel@tonic-gate 	 * plus terminator,
7927c478bd9Sstevel@tonic-gate 	 * except leading 0's are suppressed, so a few bytes may
7937c478bd9Sstevel@tonic-gate 	 * be unused.
7947c478bd9Sstevel@tonic-gate 	 */
7957c478bd9Sstevel@tonic-gate 	length = 2 + (2 * sizeof (session_id)) + 1;
7967c478bd9Sstevel@tonic-gate 	*buff = malloc(length);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	if (*buff == NULL) {
7997c478bd9Sstevel@tonic-gate 		length = 0;
8007c478bd9Sstevel@tonic-gate 		goto return_length;
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 	if (session_data == NULL) { /* NULL is not an error */
8037c478bd9Sstevel@tonic-gate 		**buff = '\0';
8047c478bd9Sstevel@tonic-gate 		length = 1;
8057c478bd9Sstevel@tonic-gate 		goto return_length; /* empty string */
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 	adt_get_asid(session_data, &session_id);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	length = snprintf(*buff, length, "0x%X", (int)session_id);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/* length < 1 is a bug: the session data type may have changed */
8127c478bd9Sstevel@tonic-gate 	assert(length > 0);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate return_length:
8157c478bd9Sstevel@tonic-gate 	return (length);
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate  * adt_end_session -- close handle, clear context
8207c478bd9Sstevel@tonic-gate  *
8217c478bd9Sstevel@tonic-gate  * if as_check is invalid, no harm, no foul, EXCEPT that this could
8227c478bd9Sstevel@tonic-gate  * be an attempt to free data already free'd, so output to syslog
8237c478bd9Sstevel@tonic-gate  * to help explain why the process cored dumped.
8247c478bd9Sstevel@tonic-gate  */
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate int
8277c478bd9Sstevel@tonic-gate adt_end_session(adt_session_data_t *session_data)
8287c478bd9Sstevel@tonic-gate {
8297c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
8327c478bd9Sstevel@tonic-gate 		state = (adt_internal_state_t *)session_data;
8337c478bd9Sstevel@tonic-gate 		if (state->as_check != ADT_VALID)
8347c478bd9Sstevel@tonic-gate 			adt_write_syslog("freeing invalid data", EINVAL);
8357c478bd9Sstevel@tonic-gate 		else {
8367c478bd9Sstevel@tonic-gate 			state->as_check = 0;
8377c478bd9Sstevel@tonic-gate 			free(session_data);
8387c478bd9Sstevel@tonic-gate 		}
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 	/* no errors yet defined */
8417c478bd9Sstevel@tonic-gate 	return (0);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate  * adt_dup_session -- copy the session data
8467c478bd9Sstevel@tonic-gate  */
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate int
8497c478bd9Sstevel@tonic-gate adt_dup_session(const adt_session_data_t *source, adt_session_data_t **dest)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*source_state;
8527c478bd9Sstevel@tonic-gate 	adt_session_data_t	*dest_state = NULL;
8537c478bd9Sstevel@tonic-gate 	int			rc = 0;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (source != NULL) {
8567c478bd9Sstevel@tonic-gate 		source_state = (adt_internal_state_t *)source;
8577c478bd9Sstevel@tonic-gate 		assert(source_state->as_check == ADT_VALID);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 		dest_state = malloc(sizeof (adt_internal_state_t));
8607c478bd9Sstevel@tonic-gate 		if (dest_state == NULL) {
8617c478bd9Sstevel@tonic-gate 			rc = -1;
8627c478bd9Sstevel@tonic-gate 			goto return_rc;
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 		(void) memcpy(dest_state, source,
8657c478bd9Sstevel@tonic-gate 		    sizeof (struct adt_internal_state));
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate return_rc:
8687c478bd9Sstevel@tonic-gate 	*dest = dest_state;
8697c478bd9Sstevel@tonic-gate 	return (rc);
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate /*
8737c478bd9Sstevel@tonic-gate  * from_export_format()
8747c478bd9Sstevel@tonic-gate  * read from a network order buffer into struct adt_session_data
8757c478bd9Sstevel@tonic-gate  */
8767c478bd9Sstevel@tonic-gate static size_t
8777c478bd9Sstevel@tonic-gate adt_from_export_format(adt_internal_state_t *internal,
8787c478bd9Sstevel@tonic-gate     const adt_export_data_t *external)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	struct export_header	head;
8817c478bd9Sstevel@tonic-gate 	struct export_link	link;
8827c478bd9Sstevel@tonic-gate 	adr_t			context;
8837c478bd9Sstevel@tonic-gate 	int32_t 		offset;
8847c478bd9Sstevel@tonic-gate 	int32_t 		length;
8857c478bd9Sstevel@tonic-gate 	int32_t 		version;
8867c478bd9Sstevel@tonic-gate 	char			*p = (char *)external;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	adrm_start(&context, (char *)external);
8897c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&head, 4);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	if ((internal->as_check = head.ax_check) != ADT_VALID) {
8927c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8937c478bd9Sstevel@tonic-gate 		return (0);
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 	offset = head.ax_link.ax_offset;
8967c478bd9Sstevel@tonic-gate 	version = head.ax_link.ax_version;
8977c478bd9Sstevel@tonic-gate 	length = head.ax_buffer_length;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	/*
9007c478bd9Sstevel@tonic-gate 	 * adjust buffer pointer to the first data item (euid)
9017c478bd9Sstevel@tonic-gate 	 * if versions mismatch; otherwise it's ok as is.
9027c478bd9Sstevel@tonic-gate 	 */
9037c478bd9Sstevel@tonic-gate 	while (version != PROTOCOL_VERSION) {
9047c478bd9Sstevel@tonic-gate 		if (offset < 1)
9057c478bd9Sstevel@tonic-gate 			return (0);	/* failed to match version */
9067c478bd9Sstevel@tonic-gate 		p += offset;	/* point to next version # */
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		if (p > (char *)external + length) {
9097c478bd9Sstevel@tonic-gate 			return (0);
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 		adrm_start(&context, p);
9127c478bd9Sstevel@tonic-gate 		adrm_int32(&context, (int *)&link, 2);
9137c478bd9Sstevel@tonic-gate 		offset = link.ax_offset;
9147c478bd9Sstevel@tonic-gate 		version = link.ax_version;
9157c478bd9Sstevel@tonic-gate 		assert(version != 0);
9167c478bd9Sstevel@tonic-gate 	}
9177c478bd9Sstevel@tonic-gate 	if (p == (char *)external)
9187c478bd9Sstevel@tonic-gate 		adrm_start(&context, (char *)(p + sizeof (head)));
9197c478bd9Sstevel@tonic-gate 	else
9207c478bd9Sstevel@tonic-gate 		adrm_start(&context, (char *)(p + sizeof (link)));
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_euid), 1);
9237c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_ruid), 1);
9247c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_egid), 1);
9257c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_rgid), 1);
9267c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
9277c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_mask.am_success), 2);
9287c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_termid.at_port), 1);
9297c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_termid.at_type), 1);
9307c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_termid.at_addr[0]),
9317c478bd9Sstevel@tonic-gate 	    4);
9327c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
9337c478bd9Sstevel@tonic-gate 	adrm_int32(&context, (int *)&(internal->as_audit_enabled), 1);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/* ax_size_of_tsol_data intentionally ignored */
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	return (sizeof (struct adt_export_data));
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate /*
9417c478bd9Sstevel@tonic-gate  * to_export_format
9427c478bd9Sstevel@tonic-gate  * read from struct adt_session_data into a network order buffer.
9437c478bd9Sstevel@tonic-gate  *
9447c478bd9Sstevel@tonic-gate  * (network order 'cause this data may be shared with a remote host.)
9457c478bd9Sstevel@tonic-gate  */
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate static size_t
9487c478bd9Sstevel@tonic-gate adt_to_export_format(adt_export_data_t *external,
9497c478bd9Sstevel@tonic-gate     adt_internal_state_t *internal)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	struct export_header	head;
9527c478bd9Sstevel@tonic-gate 	struct export_link	tail;
9537c478bd9Sstevel@tonic-gate 	adr_t			context;
9547c478bd9Sstevel@tonic-gate 	size_t			tsol_size = 0;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	adrm_start(&context, (char *)external);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	head.ax_check = ADT_VALID;
9597c478bd9Sstevel@tonic-gate 	head.ax_buffer_length = sizeof (struct adt_export_data);
9607c478bd9Sstevel@tonic-gate 	head.ax_link.ax_version = PROTOCOL_VERSION;
9617c478bd9Sstevel@tonic-gate 	head.ax_link.ax_offset = 0;
9627c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&head, 4);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_euid), 1);
9657c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
9667c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_egid), 1);
9677c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
9687c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
9697c478bd9Sstevel@tonic-gate 	adrm_putint32(&context,
9707c478bd9Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_mask.am_success), 2);
9717c478bd9Sstevel@tonic-gate 	adrm_putint32(&context,
9727c478bd9Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_port), 1);
9737c478bd9Sstevel@tonic-gate 	adrm_putint32(&context,
9747c478bd9Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_type), 1);
9757c478bd9Sstevel@tonic-gate 	adrm_putint32(&context,
9767c478bd9Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
9777c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
9787c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_audit_enabled), 1);
9797c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&tsol_size, 1);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	/* terminator */
9827c478bd9Sstevel@tonic-gate 	tail.ax_version = 0; /* invalid version number */
9837c478bd9Sstevel@tonic-gate 	tail.ax_offset = 0;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&tail, 2);
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	return (sizeof (struct adt_export_data));
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate /*
9917c478bd9Sstevel@tonic-gate  * adt_import_proc() is used by a server acting on behalf
9927c478bd9Sstevel@tonic-gate  * of a client which has connected via an ipc mechanism such as
9937c478bd9Sstevel@tonic-gate  * a door.
9947c478bd9Sstevel@tonic-gate  *
9957c478bd9Sstevel@tonic-gate  * Since the interface is via ucred, the info.ap_termid.port
9967c478bd9Sstevel@tonic-gate  * value is always the 64 bit version.  What is stored depends
9977c478bd9Sstevel@tonic-gate  * on how libbsm is compiled.
9987c478bd9Sstevel@tonic-gate  */
9997c478bd9Sstevel@tonic-gate size_t
10007c478bd9Sstevel@tonic-gate adt_import_proc(pid_t pid, uid_t euid, gid_t egid, uid_t ruid,
10017c478bd9Sstevel@tonic-gate     gid_t rgid, adt_export_data_t **external)
10027c478bd9Sstevel@tonic-gate {
10037c478bd9Sstevel@tonic-gate 	size_t			length = 0;
10047c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
10057c478bd9Sstevel@tonic-gate 	ucred_t			*ucred;
10067c478bd9Sstevel@tonic-gate 	const au_tid64_addr_t	*tid;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	state = calloc(1, sizeof (adt_internal_state_t));
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (state == NULL)
10117c478bd9Sstevel@tonic-gate 		goto return_length;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	if (adt_init(state, 0) != 0)
10147c478bd9Sstevel@tonic-gate 		goto return_length_free;    /* errno from adt_init() */
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/*
10177c478bd9Sstevel@tonic-gate 	 * ucred_getauid() returns AU_NOAUDITID if audit is off, which
10187c478bd9Sstevel@tonic-gate 	 * is the right answer for adt_import_proc().
10197c478bd9Sstevel@tonic-gate 	 *
10207c478bd9Sstevel@tonic-gate 	 * Create a local context as near as possible.
10217c478bd9Sstevel@tonic-gate 	 */
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	ucred = ucred_get(pid);
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	if (ucred == NULL)
10267c478bd9Sstevel@tonic-gate 		goto return_length_free;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	state->as_ruid = ruid != ADT_NO_CHANGE ? ruid : ucred_getruid(ucred);
10297c478bd9Sstevel@tonic-gate 	state->as_euid = euid != ADT_NO_CHANGE ? euid : ucred_geteuid(ucred);
10307c478bd9Sstevel@tonic-gate 	state->as_rgid = rgid != ADT_NO_CHANGE ? rgid : ucred_getrgid(ucred);
10317c478bd9Sstevel@tonic-gate 	state->as_egid = egid != ADT_NO_CHANGE ? egid : ucred_getegid(ucred);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	state->as_info.ai_auid = ucred_getauid(ucred);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	if (state->as_info.ai_auid == AU_NOAUDITID) {
10367c478bd9Sstevel@tonic-gate 		state->as_info.ai_asid = adt_get_unique_id(ruid);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 		if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask)))
10397c478bd9Sstevel@tonic-gate 			goto return_all_free;
10407c478bd9Sstevel@tonic-gate 	} else {
10417c478bd9Sstevel@tonic-gate 		const au_mask_t *mask = ucred_getamask(ucred);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 		if (mask != NULL)
10447c478bd9Sstevel@tonic-gate 			state->as_info.ai_mask = *mask;
10457c478bd9Sstevel@tonic-gate 		else
10467c478bd9Sstevel@tonic-gate 			goto return_all_free;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 		state->as_info.ai_asid = ucred_getasid(ucred);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	tid = ucred_getatid(ucred);
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	if (tid != NULL) {
10547c478bd9Sstevel@tonic-gate 		adt_cpy_tid(&(state->as_info.ai_termid), tid);
10557c478bd9Sstevel@tonic-gate 	} else {
10567c478bd9Sstevel@tonic-gate 		(void) memset((void *)&(state->as_info.ai_termid), 0,
10577c478bd9Sstevel@tonic-gate 		    sizeof (au_tid_addr_t));
10587c478bd9Sstevel@tonic-gate 		state->as_info.ai_termid.at_type = AU_IPv4;
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	DPRINTF(("import_proc/asid = %X %u\n", state->as_info.ai_asid,
10627c478bd9Sstevel@tonic-gate 	    state->as_info.ai_asid));
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	DPRINTF(("import_proc/masks = %X %X\n",
10657c478bd9Sstevel@tonic-gate 	    state->as_info.ai_mask.am_success,
10667c478bd9Sstevel@tonic-gate 	    state->as_info.ai_mask.am_failure));
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	*external = malloc(sizeof (adt_export_data_t));
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	if (*external == NULL)
10717c478bd9Sstevel@tonic-gate 		goto return_all_free;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	length = adt_to_export_format(*external, state);
10747c478bd9Sstevel@tonic-gate 	/*
10757c478bd9Sstevel@tonic-gate 	 * yes, state is supposed to be free'd for both pass and fail
10767c478bd9Sstevel@tonic-gate 	 */
10777c478bd9Sstevel@tonic-gate return_all_free:
10787c478bd9Sstevel@tonic-gate 	ucred_free(ucred);
10797c478bd9Sstevel@tonic-gate return_length_free:
10807c478bd9Sstevel@tonic-gate 	free(state);
10817c478bd9Sstevel@tonic-gate return_length:
10827c478bd9Sstevel@tonic-gate 	return (length);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate /*
10867c478bd9Sstevel@tonic-gate  * adt_import() -- convert from network order to machine-specific order
10877c478bd9Sstevel@tonic-gate  */
10887c478bd9Sstevel@tonic-gate static int
10897c478bd9Sstevel@tonic-gate adt_import(adt_internal_state_t *internal, const adt_export_data_t *external)
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate 	au_mask_t mask;
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/* save local audit enabled state */
10947c478bd9Sstevel@tonic-gate 	int	local_audit_enabled = internal->as_audit_enabled;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if (adt_from_export_format(internal, external) < 1)
10977c478bd9Sstevel@tonic-gate 		return (-1); /* errno from adt_from_export_format */
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	/*
11007c478bd9Sstevel@tonic-gate 	 * If audit isn't enabled on the remote, they were unable
11017c478bd9Sstevel@tonic-gate 	 * to generate the audit mask, so generate it based on
11027c478bd9Sstevel@tonic-gate 	 * local configuration.  If the user id has changed, the
11037c478bd9Sstevel@tonic-gate 	 * resulting mask may miss some subtleties that occurred
11047c478bd9Sstevel@tonic-gate 	 * on the remote system.
11057c478bd9Sstevel@tonic-gate 	 *
11067c478bd9Sstevel@tonic-gate 	 * If the remote failed to generate a terminal id, it is not
11077c478bd9Sstevel@tonic-gate 	 * recoverable.
11087c478bd9Sstevel@tonic-gate 	 */
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	if (!internal->as_audit_enabled) {
11117c478bd9Sstevel@tonic-gate 		if (adt_get_mask_from_user(internal->as_info.ai_auid,
11127c478bd9Sstevel@tonic-gate 		    &(internal->as_info.ai_mask)))
11137c478bd9Sstevel@tonic-gate 			return (-1);
11147c478bd9Sstevel@tonic-gate 		if (internal->as_info.ai_auid != internal->as_ruid) {
11157c478bd9Sstevel@tonic-gate 			if (adt_get_mask_from_user(internal->as_info.ai_auid,
11167c478bd9Sstevel@tonic-gate 			    &mask))
11177c478bd9Sstevel@tonic-gate 				return (-1);
11187c478bd9Sstevel@tonic-gate 			internal->as_info.ai_mask.am_success |=
11197c478bd9Sstevel@tonic-gate 			    mask.am_success;
11207c478bd9Sstevel@tonic-gate 			internal->as_info.ai_mask.am_failure |=
11217c478bd9Sstevel@tonic-gate 			    mask.am_failure;
11227c478bd9Sstevel@tonic-gate 		}
11237c478bd9Sstevel@tonic-gate 	}
11247c478bd9Sstevel@tonic-gate 	internal->as_audit_enabled = local_audit_enabled;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	DPRINTF(("(%d)imported asid = %X %u\n", getpid(),
11277c478bd9Sstevel@tonic-gate 	    internal->as_info.ai_asid,
11287c478bd9Sstevel@tonic-gate 	    internal->as_info.ai_asid));
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	internal->as_have_user_data = ADT_HAVE_ALL;
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	return (0);
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate /*
11367c478bd9Sstevel@tonic-gate  * adt_export_session_data()
11377c478bd9Sstevel@tonic-gate  * copies a adt_session_data struct into a network order buffer
11387c478bd9Sstevel@tonic-gate  *
11397c478bd9Sstevel@tonic-gate  * In a misconfigured network, the local host may have auditing
11407c478bd9Sstevel@tonic-gate  * off while the destination may have auditing on, so if there
11417c478bd9Sstevel@tonic-gate  * is sufficient memory, a buffer will be returned even in the
11427c478bd9Sstevel@tonic-gate  * audit off case.
11437c478bd9Sstevel@tonic-gate  */
11447c478bd9Sstevel@tonic-gate size_t
11457c478bd9Sstevel@tonic-gate adt_export_session_data(const adt_session_data_t *internal,
11467c478bd9Sstevel@tonic-gate 			adt_export_data_t **external)
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*dummy;
11497c478bd9Sstevel@tonic-gate 	size_t			length = 0;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	*external = malloc(sizeof (adt_export_data_t));
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	if (*external == NULL)
11547c478bd9Sstevel@tonic-gate 		goto return_length;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	if (internal == NULL) {
11577c478bd9Sstevel@tonic-gate 		dummy = malloc(sizeof (adt_internal_state_t));
11587c478bd9Sstevel@tonic-gate 		if (dummy == NULL)
11597c478bd9Sstevel@tonic-gate 			goto return_length_free;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 		if (adt_init(dummy, 0)) { /* 0 == don't copy from proc */
11627c478bd9Sstevel@tonic-gate 			free(dummy);
11637c478bd9Sstevel@tonic-gate 			goto return_length_free;
11647c478bd9Sstevel@tonic-gate 		}
11657c478bd9Sstevel@tonic-gate 		length = adt_to_export_format(*external, dummy);
11667c478bd9Sstevel@tonic-gate 		free(dummy);
11677c478bd9Sstevel@tonic-gate 	} else {
11687c478bd9Sstevel@tonic-gate 		length = adt_to_export_format(*external,
11697c478bd9Sstevel@tonic-gate 		    (adt_internal_state_t *)internal);
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate return_length:
11737c478bd9Sstevel@tonic-gate 	return (length);
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate return_length_free:
11767c478bd9Sstevel@tonic-gate 	free(*external);
11777c478bd9Sstevel@tonic-gate 	*external = NULL;
11787c478bd9Sstevel@tonic-gate 	return (length);
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate static void
11827c478bd9Sstevel@tonic-gate adt_setto_unaudited(adt_internal_state_t *state)
11837c478bd9Sstevel@tonic-gate {
11847c478bd9Sstevel@tonic-gate 	state->as_ruid = AU_NOAUDITID;
11857c478bd9Sstevel@tonic-gate 	state->as_euid = AU_NOAUDITID;
11867c478bd9Sstevel@tonic-gate 	state->as_rgid = AU_NOAUDITID;
11877c478bd9Sstevel@tonic-gate 	state->as_egid = AU_NOAUDITID;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	if (state->as_audit_enabled) {
11907c478bd9Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
11917c478bd9Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 		(void) memset((void *)&(state->as_info.ai_termid), 0,
11947c478bd9Sstevel@tonic-gate 		    sizeof (au_tid_addr_t));
11957c478bd9Sstevel@tonic-gate 		state->as_info.ai_termid.at_type = AU_IPv4;
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 		(void) memset((void *)&(state->as_info.ai_mask), 0,
11987c478bd9Sstevel@tonic-gate 		    sizeof (au_mask_t));
11997c478bd9Sstevel@tonic-gate 		state->as_have_user_data = 0;
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate /*
12047c478bd9Sstevel@tonic-gate  * adt_init -- set session context by copying the audit characteristics
12057c478bd9Sstevel@tonic-gate  * from the proc and picking up current uid/tid information.
12067c478bd9Sstevel@tonic-gate  *
12077c478bd9Sstevel@tonic-gate  * By default, an audit session is based on the process; the default
12087c478bd9Sstevel@tonic-gate  * is overriden by adt_set_user()
12097c478bd9Sstevel@tonic-gate  */
12107c478bd9Sstevel@tonic-gate static int
12117c478bd9Sstevel@tonic-gate adt_init(adt_internal_state_t *state, int use_proc_data)
12127c478bd9Sstevel@tonic-gate {
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	state->as_audit_enabled = (auditstate == AUC_DISABLED) ? 0 : 1;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	if (use_proc_data) {
12177c478bd9Sstevel@tonic-gate 		state->as_ruid = getuid();
12187c478bd9Sstevel@tonic-gate 		state->as_euid = geteuid();
12197c478bd9Sstevel@tonic-gate 		state->as_rgid = getgid();
12207c478bd9Sstevel@tonic-gate 		state->as_egid = getegid();
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		if (state->as_audit_enabled) {
12237c478bd9Sstevel@tonic-gate 			const au_tid64_addr_t	*tid;
12247c478bd9Sstevel@tonic-gate 			const au_mask_t		*mask;
12257c478bd9Sstevel@tonic-gate 			ucred_t			*ucred = ucred_get(getpid());
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 			/*
12287c478bd9Sstevel@tonic-gate 			 * Even if the ucred is NULL, the underlying
12297c478bd9Sstevel@tonic-gate 			 * credential may have a valid terminal id; if the
12307c478bd9Sstevel@tonic-gate 			 * terminal id is set, then that's good enough.  An
12317c478bd9Sstevel@tonic-gate 			 * example of where this matters is failed login,
12327c478bd9Sstevel@tonic-gate 			 * where rlogin/telnet sets the terminal id before
12337c478bd9Sstevel@tonic-gate 			 * calling login; login does not load the credential
12347c478bd9Sstevel@tonic-gate 			 * since auth failed.
12357c478bd9Sstevel@tonic-gate 			 */
12367c478bd9Sstevel@tonic-gate 			if (ucred == NULL) {
12377c478bd9Sstevel@tonic-gate 				if (!adt_have_termid(
12387c478bd9Sstevel@tonic-gate 				    &(state->as_info.ai_termid)))
12397c478bd9Sstevel@tonic-gate 					return (-1);
12407c478bd9Sstevel@tonic-gate 			} else {
12417c478bd9Sstevel@tonic-gate 				mask = ucred_getamask(ucred);
12427c478bd9Sstevel@tonic-gate 				if (mask != NULL) {
12437c478bd9Sstevel@tonic-gate 					state->as_info.ai_mask = *mask;
12447c478bd9Sstevel@tonic-gate 				} else {
12457c478bd9Sstevel@tonic-gate 					ucred_free(ucred);
12467c478bd9Sstevel@tonic-gate 					return (-1);
12477c478bd9Sstevel@tonic-gate 				}
12487c478bd9Sstevel@tonic-gate 				tid = ucred_getatid(ucred);
12497c478bd9Sstevel@tonic-gate 				if (tid != NULL) {
12507c478bd9Sstevel@tonic-gate 					adt_cpy_tid(&(state->as_info.ai_termid),
12517c478bd9Sstevel@tonic-gate 					    tid);
12527c478bd9Sstevel@tonic-gate 				} else {
12537c478bd9Sstevel@tonic-gate 					ucred_free(ucred);
12547c478bd9Sstevel@tonic-gate 					return (-1);
12557c478bd9Sstevel@tonic-gate 				}
12567c478bd9Sstevel@tonic-gate 				state->as_info.ai_asid = ucred_getasid(ucred);
12577c478bd9Sstevel@tonic-gate 				state->as_info.ai_auid = ucred_getauid(ucred);
12587c478bd9Sstevel@tonic-gate 				ucred_free(ucred);
12597c478bd9Sstevel@tonic-gate 			}
12607c478bd9Sstevel@tonic-gate 			state->as_have_user_data = ADT_HAVE_ALL;
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate 	} else {
12637c478bd9Sstevel@tonic-gate 		adt_setto_unaudited(state);
12647c478bd9Sstevel@tonic-gate 	}
12657c478bd9Sstevel@tonic-gate 	state->as_session_model = ADT_SESSION_MODEL;	/* default */
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if (state->as_audit_enabled &&
12687c478bd9Sstevel@tonic-gate 	    auditon(A_GETPOLICY, (caddr_t)&(state->as_kernel_audit_policy),
12697c478bd9Sstevel@tonic-gate 	    sizeof (state->as_kernel_audit_policy))) {
12707c478bd9Sstevel@tonic-gate 		return (-1);  /* errno set by auditon */
12717c478bd9Sstevel@tonic-gate 	}
12727c478bd9Sstevel@tonic-gate 	state->as_check = ADT_VALID;
12737c478bd9Sstevel@tonic-gate 	return (0);
12747c478bd9Sstevel@tonic-gate }
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate /*
12777c478bd9Sstevel@tonic-gate  * adt_set_proc
12787c478bd9Sstevel@tonic-gate  *
12797c478bd9Sstevel@tonic-gate  * Copy the current session state to the process.  If this function
12807c478bd9Sstevel@tonic-gate  * is called, the model becomes a process model rather than a
12817c478bd9Sstevel@tonic-gate  * session model.
12827c478bd9Sstevel@tonic-gate  *
12837c478bd9Sstevel@tonic-gate  * In the current implementation, the value state->as_have_user_data
12847c478bd9Sstevel@tonic-gate  * must be either ADT_HAVE_ALL or ADT_HAVE_TID.  The latter value
12857c478bd9Sstevel@tonic-gate  * is to support the adt_set_user() flag of ADT_SETTID.  If another
12867c478bd9Sstevel@tonic-gate  * need for setting single audit data is discovered, consider changing
12877c478bd9Sstevel@tonic-gate  * the logic to test each individual flag setting rather than "ALL."
12887c478bd9Sstevel@tonic-gate  *
12897c478bd9Sstevel@tonic-gate  * Future consideration:  Is ADT_HAVE_ALL too strong a test?  There
12907c478bd9Sstevel@tonic-gate  * doesn't seem to be a good reason for requiring that ADT_HAVE_IDS
12917c478bd9Sstevel@tonic-gate  * be set.
12927c478bd9Sstevel@tonic-gate  */
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate int
12957c478bd9Sstevel@tonic-gate adt_set_proc(const adt_session_data_t *session_data)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	struct auditinfo_addr	info;
12987c478bd9Sstevel@tonic-gate 	int			rc;
12997c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	if (auditstate == AUC_DISABLED || (session_data == NULL))
13027c478bd9Sstevel@tonic-gate 		return (0);
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	if ((state->as_have_user_data != ADT_HAVE_ALL) &&
13097c478bd9Sstevel@tonic-gate 	    ((state->as_have_user_data & ADT_HAVE_TID) == 0)) {
13107c478bd9Sstevel@tonic-gate 		errno = EINVAL;
13117c478bd9Sstevel@tonic-gate 		goto return_err;
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * leave tid alone if session copy is not set
13157c478bd9Sstevel@tonic-gate 	 * Since info contains tid, you need to get tid from kernel
13167c478bd9Sstevel@tonic-gate 	 * so it can be written back.
13177c478bd9Sstevel@tonic-gate 	 * All info other than tid is overwritten from session before
13187c478bd9Sstevel@tonic-gate 	 * it is written back.
13197c478bd9Sstevel@tonic-gate 	 *
13207c478bd9Sstevel@tonic-gate 	 * See the note above the function header; if flag by flag
13217c478bd9Sstevel@tonic-gate 	 * tests are done, testing for an empty tid should just
13227c478bd9Sstevel@tonic-gate 	 * be a test for ADT_HAVE_TID -- and the functions that
13237c478bd9Sstevel@tonic-gate 	 * load the terminal id may have to be modified to make
13247c478bd9Sstevel@tonic-gate 	 * this appropriate.
13257c478bd9Sstevel@tonic-gate 	 */
13267c478bd9Sstevel@tonic-gate 	if (state->as_info.ai_termid.at_port ||
13277c478bd9Sstevel@tonic-gate 	    state->as_info.ai_termid.at_addr[0] ||
13287c478bd9Sstevel@tonic-gate 	    state->as_info.ai_termid.at_addr[1] ||
13297c478bd9Sstevel@tonic-gate 	    state->as_info.ai_termid.at_addr[2] ||
13307c478bd9Sstevel@tonic-gate 	    state->as_info.ai_termid.at_addr[3]) {
13317c478bd9Sstevel@tonic-gate 		info.ai_termid = state->as_info.ai_termid;
13327c478bd9Sstevel@tonic-gate 	} else {
13337c478bd9Sstevel@tonic-gate 		rc = getaudit_addr(&info, sizeof (info));
13347c478bd9Sstevel@tonic-gate 		if (rc < 0)
13357c478bd9Sstevel@tonic-gate 			goto return_err;   /* errno set by getaudit_addr() */
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 	info.ai_auid = state->as_info.ai_auid;
13387c478bd9Sstevel@tonic-gate 	info.ai_asid = state->as_info.ai_asid;
13397c478bd9Sstevel@tonic-gate 	info.ai_mask = state->as_info.ai_mask;
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	rc = setaudit_addr(&info, sizeof (info));
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	if (rc < 0)
13447c478bd9Sstevel@tonic-gate 		goto return_err;	/* errno set by setaudit_addr() */
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	state->as_session_model = ADT_PROCESS_MODEL;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	return (0);
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate return_err:
13517c478bd9Sstevel@tonic-gate 	adt_write_syslog("failed to set process audit characteristics", errno);
13527c478bd9Sstevel@tonic-gate 	return (-1);
13537c478bd9Sstevel@tonic-gate }
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate static int
13567c478bd9Sstevel@tonic-gate adt_newuser(adt_internal_state_t *state, uid_t ruid, au_tid_addr_t *termid)
13577c478bd9Sstevel@tonic-gate {
13587c478bd9Sstevel@tonic-gate 	au_tid_addr_t	no_tid = {0, AU_IPv4, 0, 0, 0, 0};
13597c478bd9Sstevel@tonic-gate 	au_mask_t	no_mask = {0, 0};
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	if (ruid == ADT_NO_AUDIT) {
13627c478bd9Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
13637c478bd9Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
13647c478bd9Sstevel@tonic-gate 		state->as_info.ai_termid = no_tid;
13657c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask = no_mask;
13667c478bd9Sstevel@tonic-gate 		return (0);
13677c478bd9Sstevel@tonic-gate 	}
13687c478bd9Sstevel@tonic-gate 	state->as_info.ai_auid = ruid;
13697c478bd9Sstevel@tonic-gate 	state->as_info.ai_asid = adt_get_unique_id(ruid);
13707c478bd9Sstevel@tonic-gate 	if (termid != NULL)
13717c478bd9Sstevel@tonic-gate 		state->as_info.ai_termid = *termid;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask)))
13747c478bd9Sstevel@tonic-gate 		return (-1);
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	return (0);
13777c478bd9Sstevel@tonic-gate }
13787c478bd9Sstevel@tonic-gate static int
13797c478bd9Sstevel@tonic-gate adt_changeuser(adt_internal_state_t *state, uid_t ruid)
13807c478bd9Sstevel@tonic-gate {
13817c478bd9Sstevel@tonic-gate 	au_mask_t		mask;
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	if (!(state->as_have_user_data & ADT_HAVE_AUID))
13847c478bd9Sstevel@tonic-gate 		state->as_info.ai_auid = ruid;
13857c478bd9Sstevel@tonic-gate 	if (!(state->as_have_user_data & ADT_HAVE_ASID))
13867c478bd9Sstevel@tonic-gate 		state->as_info.ai_asid = adt_get_unique_id(ruid);
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	if (ruid >= 0) {
13897c478bd9Sstevel@tonic-gate 		if (adt_get_mask_from_user(ruid, &mask))
13907c478bd9Sstevel@tonic-gate 			return (-1);
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask.am_success |= mask.am_success;
13937c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask.am_failure |= mask.am_failure;
13947c478bd9Sstevel@tonic-gate 	}
13957c478bd9Sstevel@tonic-gate 	DPRINTF(("changed mask to %08X/%08X for ruid=%d\n",
13967c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask.am_success,
13977c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask.am_failure,
13987c478bd9Sstevel@tonic-gate 		ruid));
13997c478bd9Sstevel@tonic-gate 	return (0);
14007c478bd9Sstevel@tonic-gate }
14017c478bd9Sstevel@tonic-gate /*
14027c478bd9Sstevel@tonic-gate  * adt_set_user -- see also adt_set_from_ucred()
14037c478bd9Sstevel@tonic-gate  *
14047c478bd9Sstevel@tonic-gate  * ADT_NO_ATTRIB is a valid uid/gid meaning "not known" or
14057c478bd9Sstevel@tonic-gate  * "unattributed."
14067c478bd9Sstevel@tonic-gate  *
14077c478bd9Sstevel@tonic-gate  * ADT_NO_CHANGE is a valid uid/gid meaning "do not change this value"
14087c478bd9Sstevel@tonic-gate  * only valid with ADT_UPDATE.
14097c478bd9Sstevel@tonic-gate  *
14107c478bd9Sstevel@tonic-gate  * ADT_NO_AUDIT is the external equivalent to AU_NOAUDITIT -- there
14117c478bd9Sstevel@tonic-gate  * isn't a good reason to call adt_set_user() with it unless you don't
14127c478bd9Sstevel@tonic-gate  * have a good value yet and intend to replace it later; auid will be
14137c478bd9Sstevel@tonic-gate  * AU_NOAUDITID.
14147c478bd9Sstevel@tonic-gate  *
14157c478bd9Sstevel@tonic-gate  * adt_set_user should be called even if auditing is not enabled
14167c478bd9Sstevel@tonic-gate  * so that adt_export_session_data() will have useful stuff to
14177c478bd9Sstevel@tonic-gate  * work with.
14187c478bd9Sstevel@tonic-gate  *
14197c478bd9Sstevel@tonic-gate  * See the note preceding adt_set_proc() about the use of ADT_HAVE_TID
14207c478bd9Sstevel@tonic-gate  * and ADT_HAVE_ALL.
14217c478bd9Sstevel@tonic-gate  */
14227c478bd9Sstevel@tonic-gate int
14237c478bd9Sstevel@tonic-gate adt_set_user(const adt_session_data_t *session_data,
14247c478bd9Sstevel@tonic-gate     uid_t euid,
14257c478bd9Sstevel@tonic-gate     gid_t egid,
14267c478bd9Sstevel@tonic-gate     uid_t ruid,
14277c478bd9Sstevel@tonic-gate     gid_t rgid,
14287c478bd9Sstevel@tonic-gate     const adt_termid_t *termid,
14297c478bd9Sstevel@tonic-gate     enum adt_user_context user_context)
14307c478bd9Sstevel@tonic-gate {
14317c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
14327c478bd9Sstevel@tonic-gate 	int			rc;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	if (session_data == NULL) /* no session exists to audit */
14357c478bd9Sstevel@tonic-gate 		return (0);
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
14387c478bd9Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	switch (user_context) {
14417c478bd9Sstevel@tonic-gate 	case ADT_NEW:
14427c478bd9Sstevel@tonic-gate 		if (ruid == ADT_NO_CHANGE || euid == ADT_NO_CHANGE ||
14437c478bd9Sstevel@tonic-gate 		    rgid == ADT_NO_CHANGE || egid == ADT_NO_CHANGE) {
14447c478bd9Sstevel@tonic-gate 			errno = EINVAL;
14457c478bd9Sstevel@tonic-gate 			return (-1);
14467c478bd9Sstevel@tonic-gate 		}
14477c478bd9Sstevel@tonic-gate 		if ((rc = adt_newuser(state, ruid,
14487c478bd9Sstevel@tonic-gate 		    (au_tid_addr_t *)termid)) != 0)
14497c478bd9Sstevel@tonic-gate 			return (rc);
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_ALL;
14527c478bd9Sstevel@tonic-gate 		break;
14537c478bd9Sstevel@tonic-gate 	case ADT_UPDATE:
14547c478bd9Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
14557c478bd9Sstevel@tonic-gate 			errno = EINVAL;
14567c478bd9Sstevel@tonic-gate 			return (-1);
14577c478bd9Sstevel@tonic-gate 		}
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 		if (ruid != ADT_NO_CHANGE)
14607c478bd9Sstevel@tonic-gate 			if ((rc = adt_changeuser(state, ruid)) != 0)
14617c478bd9Sstevel@tonic-gate 				return (rc);
14627c478bd9Sstevel@tonic-gate 		break;
14637c478bd9Sstevel@tonic-gate 	case ADT_USER:
14647c478bd9Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
14657c478bd9Sstevel@tonic-gate 			errno = EINVAL;
14667c478bd9Sstevel@tonic-gate 			return (-1);
14677c478bd9Sstevel@tonic-gate 		}
14687c478bd9Sstevel@tonic-gate 		break;
14697c478bd9Sstevel@tonic-gate 	case ADT_SETTID:
14707c478bd9Sstevel@tonic-gate 		assert(termid != NULL);
14717c478bd9Sstevel@tonic-gate 		state->as_info.ai_termid = *((au_tid_addr_t *)termid);
14727c478bd9Sstevel@tonic-gate 		/* avoid fooling pam_setcred()... */
14737c478bd9Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
14747c478bd9Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
14757c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask.am_failure = 0;
14767c478bd9Sstevel@tonic-gate 		state->as_info.ai_mask.am_success = 0;
14777c478bd9Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_TID |
14787c478bd9Sstevel@tonic-gate 		    ADT_HAVE_AUID | ADT_HAVE_ASID | ADT_HAVE_MASK;
14797c478bd9Sstevel@tonic-gate 		return (0);
14807c478bd9Sstevel@tonic-gate 		break;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	default:
14837c478bd9Sstevel@tonic-gate 		errno = EINVAL;
14847c478bd9Sstevel@tonic-gate 		return (-1);
14857c478bd9Sstevel@tonic-gate 	}
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	if (ruid == ADT_NO_AUDIT) {
14887c478bd9Sstevel@tonic-gate 		state->as_ruid = AU_NOAUDITID;
14897c478bd9Sstevel@tonic-gate 		state->as_euid = AU_NOAUDITID;
14907c478bd9Sstevel@tonic-gate 		state->as_rgid = AU_NOAUDITID;
14917c478bd9Sstevel@tonic-gate 		state->as_egid = AU_NOAUDITID;
14927c478bd9Sstevel@tonic-gate 	} else {
14937c478bd9Sstevel@tonic-gate 		if (ruid != ADT_NO_CHANGE)
14947c478bd9Sstevel@tonic-gate 			state->as_ruid = ruid;
14957c478bd9Sstevel@tonic-gate 		if (euid != ADT_NO_CHANGE)
14967c478bd9Sstevel@tonic-gate 			state->as_euid = euid;
14977c478bd9Sstevel@tonic-gate 		if (rgid != ADT_NO_CHANGE)
14987c478bd9Sstevel@tonic-gate 			state->as_rgid = rgid;
14997c478bd9Sstevel@tonic-gate 		if (egid != ADT_NO_CHANGE)
15007c478bd9Sstevel@tonic-gate 			state->as_egid = egid;
15017c478bd9Sstevel@tonic-gate 	}
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	return (0);
15047c478bd9Sstevel@tonic-gate }
15057c478bd9Sstevel@tonic-gate /*
15067c478bd9Sstevel@tonic-gate  * adt_set_from_ucred()
15077c478bd9Sstevel@tonic-gate  *
15087c478bd9Sstevel@tonic-gate  * an alternate to adt_set_user that fills the same role but uses
15097c478bd9Sstevel@tonic-gate  * a pointer to a ucred rather than a list of id's.  If the ucred
15107c478bd9Sstevel@tonic-gate  * pointer is NULL, use the credential from the this process.
15117c478bd9Sstevel@tonic-gate  *
15127c478bd9Sstevel@tonic-gate  * A key difference is that for ADT_NEW, adt_set_from_ucred() does
15137c478bd9Sstevel@tonic-gate  * not overwrite the asid and auid unless auid has not been set.
15147c478bd9Sstevel@tonic-gate  * ADT_NEW differs from ADT_UPDATE in that it does not OR together
15157c478bd9Sstevel@tonic-gate  * the incoming audit mask with the one that already exists.
15167c478bd9Sstevel@tonic-gate  *
15177c478bd9Sstevel@tonic-gate  * adt_set_from_ucred should be called even if auditing is not enabled
15187c478bd9Sstevel@tonic-gate  * so that adt_export_session_data() will have useful stuff to
15197c478bd9Sstevel@tonic-gate  * work with.
15207c478bd9Sstevel@tonic-gate  */
15217c478bd9Sstevel@tonic-gate int
15227c478bd9Sstevel@tonic-gate adt_set_from_ucred(const adt_session_data_t *session_data,
1523*26706799Sme23304     const ucred_t *uc, enum adt_user_context user_context)
15247c478bd9Sstevel@tonic-gate {
15257c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*state;
15267c478bd9Sstevel@tonic-gate 	int			rc = -1;
15277c478bd9Sstevel@tonic-gate 	const au_tid64_addr_t		*tid64;
15287c478bd9Sstevel@tonic-gate 	au_tid_addr_t		termid, *tid;
1529*26706799Sme23304 	ucred_t	*ucred = (ucred_t *)uc;
1530*26706799Sme23304 	boolean_t	local_uc = B_FALSE;
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	if (session_data == NULL) /* no session exists to audit */
15337c478bd9Sstevel@tonic-gate 		return (0);
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
15367c478bd9Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	if (ucred == NULL) {
15397c478bd9Sstevel@tonic-gate 		ucred = ucred_get(getpid());
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 		if (ucred == NULL)
15427c478bd9Sstevel@tonic-gate 			goto return_rc;
1543*26706799Sme23304 		local_uc = B_TRUE;
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	switch (user_context) {
15477c478bd9Sstevel@tonic-gate 	case ADT_NEW:
15487c478bd9Sstevel@tonic-gate 		tid64 = ucred_getatid(ucred);
15497c478bd9Sstevel@tonic-gate 		if (tid64 != NULL) {
15507c478bd9Sstevel@tonic-gate 			adt_cpy_tid(&termid, tid64);
15517c478bd9Sstevel@tonic-gate 			tid = &termid;
15527c478bd9Sstevel@tonic-gate 		} else {
15537c478bd9Sstevel@tonic-gate 			tid = NULL;
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 		/* if unaudited, adt_newuser cleans up */
15567c478bd9Sstevel@tonic-gate 		if (ucred_getauid(ucred) == AU_NOAUDITID) {
15577c478bd9Sstevel@tonic-gate 			if ((rc = adt_newuser(state, ucred_getruid(ucred),
15587c478bd9Sstevel@tonic-gate 			    tid)) != 0)
15597c478bd9Sstevel@tonic-gate 				goto return_rc;
15607c478bd9Sstevel@tonic-gate 		} else {
15617c478bd9Sstevel@tonic-gate 			state->as_info.ai_auid = ucred_getauid(ucred);
15627c478bd9Sstevel@tonic-gate 			state->as_info.ai_asid = ucred_getasid(ucred);
15637c478bd9Sstevel@tonic-gate 			state->as_info.ai_mask = *ucred_getamask(ucred);
15647c478bd9Sstevel@tonic-gate 			state->as_info.ai_termid = *tid;
15657c478bd9Sstevel@tonic-gate 		}
15667c478bd9Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_ALL;
15677c478bd9Sstevel@tonic-gate 		break;
15687c478bd9Sstevel@tonic-gate 	case ADT_UPDATE:
15697c478bd9Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
15707c478bd9Sstevel@tonic-gate 			errno = EINVAL;
15717c478bd9Sstevel@tonic-gate 			goto return_rc;
15727c478bd9Sstevel@tonic-gate 		}
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 		if ((rc = adt_changeuser(state, ucred_getruid(ucred))) != 0)
15757c478bd9Sstevel@tonic-gate 			goto return_rc;
15767c478bd9Sstevel@tonic-gate 		break;
15777c478bd9Sstevel@tonic-gate 	case ADT_USER:
15787c478bd9Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
15797c478bd9Sstevel@tonic-gate 			errno = EINVAL;
15807c478bd9Sstevel@tonic-gate 			goto return_rc;
15817c478bd9Sstevel@tonic-gate 		}
15827c478bd9Sstevel@tonic-gate 		break;
15837c478bd9Sstevel@tonic-gate 	default:
15847c478bd9Sstevel@tonic-gate 		errno = EINVAL;
15857c478bd9Sstevel@tonic-gate 		goto return_rc;
15867c478bd9Sstevel@tonic-gate 	}
15877c478bd9Sstevel@tonic-gate 	rc = 0;
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	state->as_ruid = ucred_getruid(ucred);
15907c478bd9Sstevel@tonic-gate 	state->as_euid = ucred_geteuid(ucred);
15917c478bd9Sstevel@tonic-gate 	state->as_rgid = ucred_getrgid(ucred);
15927c478bd9Sstevel@tonic-gate 	state->as_egid = ucred_getegid(ucred);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate return_rc:
1595*26706799Sme23304 	if (local_uc) {
1596*26706799Sme23304 		ucred_free(ucred);
1597*26706799Sme23304 	}
15987c478bd9Sstevel@tonic-gate 	return (rc);
15997c478bd9Sstevel@tonic-gate }
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate /*
16027c478bd9Sstevel@tonic-gate  * adt_alloc_event() returns a pointer to allocated memory
16037c478bd9Sstevel@tonic-gate  *
16047c478bd9Sstevel@tonic-gate  */
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate adt_event_data_t
16077c478bd9Sstevel@tonic-gate *adt_alloc_event(const adt_session_data_t *session_data, au_event_t event_id)
16087c478bd9Sstevel@tonic-gate {
16097c478bd9Sstevel@tonic-gate 	struct adt_event_state	*event_state;
16107c478bd9Sstevel@tonic-gate 	adt_internal_state_t	*session_state;
16117c478bd9Sstevel@tonic-gate 	adt_event_data_t	*return_event = NULL;
16127c478bd9Sstevel@tonic-gate 	/*
16137c478bd9Sstevel@tonic-gate 	 * need to return a valid event pointer even if audit is
16147c478bd9Sstevel@tonic-gate 	 * off, else the caller will end up either (1) keeping its
16157c478bd9Sstevel@tonic-gate 	 * own flags for on/off or (2) writing to a NULL pointer.
16167c478bd9Sstevel@tonic-gate 	 * If auditing is on, the session data must be valid; otherwise
16177c478bd9Sstevel@tonic-gate 	 * we don't care.
16187c478bd9Sstevel@tonic-gate 	 */
16197c478bd9Sstevel@tonic-gate 	if (session_data != NULL) {
16207c478bd9Sstevel@tonic-gate 		session_state = (adt_internal_state_t *)session_data;
16217c478bd9Sstevel@tonic-gate 		assert(session_state->as_check == ADT_VALID);
16227c478bd9Sstevel@tonic-gate 	}
16237c478bd9Sstevel@tonic-gate 	event_state = calloc(1, sizeof (struct adt_event_state));
16247c478bd9Sstevel@tonic-gate 	if (event_state == NULL)
16257c478bd9Sstevel@tonic-gate 		goto return_ptr;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	event_state->ae_check = ADT_VALID;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	event_state->ae_event_id = event_id;
16307c478bd9Sstevel@tonic-gate 	event_state->ae_session = (struct adt_internal_state *)session_data;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	return_event = (adt_event_data_t *)&(event_state->ae_event_data);
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	/*
16357c478bd9Sstevel@tonic-gate 	 * preload data so the adt_au_*() functions can detect un-supplied
16367c478bd9Sstevel@tonic-gate 	 * values (0 and NULL are free via calloc()).
16377c478bd9Sstevel@tonic-gate 	 */
16387c478bd9Sstevel@tonic-gate 	adt_preload(event_id, return_event);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate return_ptr:
16417c478bd9Sstevel@tonic-gate 	return (return_event);
16427c478bd9Sstevel@tonic-gate }
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate /*
16457c478bd9Sstevel@tonic-gate  * adt_getXlateTable -- look up translation table address for event id
16467c478bd9Sstevel@tonic-gate  */
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate static struct translation *
16497c478bd9Sstevel@tonic-gate adt_getXlateTable(au_event_t event_id)
16507c478bd9Sstevel@tonic-gate {
16517c478bd9Sstevel@tonic-gate 	/* xlate_table is global in adt_xlate.c */
16527c478bd9Sstevel@tonic-gate 	struct translation	**p_xlate = &xlate_table[0];
16537c478bd9Sstevel@tonic-gate 	struct translation	*p_event;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	while (*p_xlate != NULL) {
16567c478bd9Sstevel@tonic-gate 		p_event = *p_xlate;
16577c478bd9Sstevel@tonic-gate 		if (event_id == p_event->tx_external_event)
16587c478bd9Sstevel@tonic-gate 			return (p_event);
16597c478bd9Sstevel@tonic-gate 		p_xlate++;
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 	return (NULL);
16627c478bd9Sstevel@tonic-gate }
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate /*
16657c478bd9Sstevel@tonic-gate  * adt_calcOffsets
16667c478bd9Sstevel@tonic-gate  *
16677c478bd9Sstevel@tonic-gate  * the call to this function is surrounded by a mutex.
16687c478bd9Sstevel@tonic-gate  *
16697c478bd9Sstevel@tonic-gate  * i walks down the table picking up next_token.  j walks again to
16707c478bd9Sstevel@tonic-gate  * calculate the offset to the input data.  k points to the next
16717c478bd9Sstevel@tonic-gate  * token's row.  Finally, l, is used to sum the values in the
16727c478bd9Sstevel@tonic-gate  * datadef array.
16737c478bd9Sstevel@tonic-gate  *
16747c478bd9Sstevel@tonic-gate  * What's going on?  The entry array is in the order of the input
16757c478bd9Sstevel@tonic-gate  * fields but the processing of array entries is in the order of
16767c478bd9Sstevel@tonic-gate  * the output (see next_token).  Calculating the offset to the
16777c478bd9Sstevel@tonic-gate  * "next" input can't be done in the outer loop (i) since i doesn't
16787c478bd9Sstevel@tonic-gate  * point to the current entry and it can't be done with the k index
16797c478bd9Sstevel@tonic-gate  * because it doesn't represent the order of input fields.
16807c478bd9Sstevel@tonic-gate  *
16817c478bd9Sstevel@tonic-gate  * While the resulting algorithm is n**2, it is only done once per
16827c478bd9Sstevel@tonic-gate  * event type.
16837c478bd9Sstevel@tonic-gate  */
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate /*
16867c478bd9Sstevel@tonic-gate  * adt_calcOffsets is only called once per event type, but it uses
16877c478bd9Sstevel@tonic-gate  * the address alignment of memory allocated for that event as if it
16887c478bd9Sstevel@tonic-gate  * were the same for all subsequently allocated memory.  This is
16897c478bd9Sstevel@tonic-gate  * guaranteed by calloc/malloc.  Arrays take special handling since
16907c478bd9Sstevel@tonic-gate  * what matters for figuring out the correct alignment is the size
16917c478bd9Sstevel@tonic-gate  * of the array element.
16927c478bd9Sstevel@tonic-gate  */
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate static void
16957c478bd9Sstevel@tonic-gate adt_calcOffsets(struct entry *p_entry, int tablesize, void *p_data)
16967c478bd9Sstevel@tonic-gate {
16977c478bd9Sstevel@tonic-gate 	int		i, j;
16987c478bd9Sstevel@tonic-gate 	size_t		this_size, prev_size;
16997c478bd9Sstevel@tonic-gate 	void		*struct_start = p_data;
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	for (i = 0; i < tablesize; i++) {
17027c478bd9Sstevel@tonic-gate 		if (p_entry[i].en_type_def == NULL) {
17037c478bd9Sstevel@tonic-gate 			p_entry[i].en_offset = 0;
17047c478bd9Sstevel@tonic-gate 			continue;
17057c478bd9Sstevel@tonic-gate 		}
17067c478bd9Sstevel@tonic-gate 		prev_size = 0;
17077c478bd9Sstevel@tonic-gate 		p_entry[i].en_offset = (char *)p_data - (char *)struct_start;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 		for (j = 0; j < p_entry[i].en_count_types; j++) {
17107c478bd9Sstevel@tonic-gate 			if (p_entry[i].en_type_def[j].dd_datatype == ADT_MSG)
17117c478bd9Sstevel@tonic-gate 				this_size = sizeof (enum adt_generic);
17127c478bd9Sstevel@tonic-gate 			else
17137c478bd9Sstevel@tonic-gate 				this_size =
17147c478bd9Sstevel@tonic-gate 				    p_entry[i].en_type_def[j].dd_input_size;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 			/* adj for first entry */
17177c478bd9Sstevel@tonic-gate 			if (prev_size == 0)
17187c478bd9Sstevel@tonic-gate 				prev_size = this_size;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 			if (p_entry[i].en_type_def[j].dd_datatype ==
17217c478bd9Sstevel@tonic-gate 			    ADT_UINT32ARRAY) {
17227c478bd9Sstevel@tonic-gate 				p_data = (char *)adt_adjust_address(p_data,
17237c478bd9Sstevel@tonic-gate 				    prev_size, sizeof (uint32_t)) +
17247c478bd9Sstevel@tonic-gate 				    this_size - sizeof (uint32_t);
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 				prev_size = sizeof (uint32_t);
17277c478bd9Sstevel@tonic-gate 			} else {
17287c478bd9Sstevel@tonic-gate 				p_data = adt_adjust_address(p_data, prev_size,
17297c478bd9Sstevel@tonic-gate 				    this_size);
17307c478bd9Sstevel@tonic-gate 				prev_size = this_size;
17317c478bd9Sstevel@tonic-gate 			}
17327c478bd9Sstevel@tonic-gate 		}
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate /*
17377c478bd9Sstevel@tonic-gate  * adt_generate_event
17387c478bd9Sstevel@tonic-gate  * generate event record from external struct.  The order is based on
17397c478bd9Sstevel@tonic-gate  * the output tokens, allowing for the possibility that the input data
17407c478bd9Sstevel@tonic-gate  * is in a different order.
17417c478bd9Sstevel@tonic-gate  *
17427c478bd9Sstevel@tonic-gate  */
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate static void
17457c478bd9Sstevel@tonic-gate adt_generate_event(const adt_event_data_t *p_extdata,
17467c478bd9Sstevel@tonic-gate     struct adt_event_state *p_event,
17477c478bd9Sstevel@tonic-gate     struct translation *p_xlate)
17487c478bd9Sstevel@tonic-gate {
17497c478bd9Sstevel@tonic-gate 	struct entry		*p_entry;
17507c478bd9Sstevel@tonic-gate 	static mutex_t	lock = DEFAULTMUTEX;
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	p_entry = p_xlate->tx_first_entry;
17537c478bd9Sstevel@tonic-gate 	assert(p_entry != NULL);
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	p_event->ae_internal_id = p_xlate->tx_internal_event;
17567c478bd9Sstevel@tonic-gate 	adt_token_open(p_event);
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	/*
17597c478bd9Sstevel@tonic-gate 	 * offsets are not pre-calculated; the initial offsets are all
17607c478bd9Sstevel@tonic-gate 	 * 0; valid offsets are >= 0.  Offsets for no-input tokens such
17617c478bd9Sstevel@tonic-gate 	 * as subject are set to -1 by adt_calcOffset()
17627c478bd9Sstevel@tonic-gate 	 */
17637c478bd9Sstevel@tonic-gate 	if (p_xlate->tx_offsetsCalculated == 0) {
17647c478bd9Sstevel@tonic-gate 		(void) _mutex_lock(&lock);
17657c478bd9Sstevel@tonic-gate 		p_xlate->tx_offsetsCalculated = 1;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 		adt_calcOffsets(p_xlate->tx_top_entry, p_xlate->tx_entries,
17687c478bd9Sstevel@tonic-gate 		    (void *)p_extdata);
17697c478bd9Sstevel@tonic-gate 		(void) _mutex_unlock(&lock);
17707c478bd9Sstevel@tonic-gate 	}
17717c478bd9Sstevel@tonic-gate 	while (p_entry != NULL) {
17727c478bd9Sstevel@tonic-gate 		adt_generate_token(p_entry, (char *)p_extdata,
17737c478bd9Sstevel@tonic-gate 		    p_event);
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 		p_entry = p_entry->en_next_token;
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 	adt_token_close(p_event);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate /*
17817c478bd9Sstevel@tonic-gate  * adt_put_event -- main event generation function.
17827c478bd9Sstevel@tonic-gate  * The input "event" is the address of the struct containing
17837c478bd9Sstevel@tonic-gate  * event-specific data.
17847c478bd9Sstevel@tonic-gate  *
17857c478bd9Sstevel@tonic-gate  * However if auditing is off or the session handle
17867c478bd9Sstevel@tonic-gate  * is NULL, no attempt to write a record is made.
17877c478bd9Sstevel@tonic-gate  */
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate int
17907c478bd9Sstevel@tonic-gate adt_put_event(const adt_event_data_t *event, int status,
17917c478bd9Sstevel@tonic-gate     int return_val)
17927c478bd9Sstevel@tonic-gate {
17937c478bd9Sstevel@tonic-gate 	struct adt_event_state	*event_state;
17947c478bd9Sstevel@tonic-gate 	struct translation	*xlate;
17957c478bd9Sstevel@tonic-gate 	int			rc = 0;
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	if (event == NULL) {
17987c478bd9Sstevel@tonic-gate 		errno = EINVAL;
17997c478bd9Sstevel@tonic-gate 		rc = -1;
18007c478bd9Sstevel@tonic-gate 		goto return_rc;
18017c478bd9Sstevel@tonic-gate 	}
18027c478bd9Sstevel@tonic-gate 	event_state = (struct adt_event_state *)event;
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	/* if audit off or this is a broken session, exit */
18057c478bd9Sstevel@tonic-gate 	if (auditstate == AUC_DISABLED || (event_state->ae_session == NULL))
18067c478bd9Sstevel@tonic-gate 		goto return_rc;
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	assert(event_state->ae_check == ADT_VALID);
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	event_state->ae_rc = status;
18117c478bd9Sstevel@tonic-gate 	event_state->ae_type = return_val;
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	/* look up the event */
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	xlate = adt_getXlateTable(event_state->ae_event_id);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	if (xlate == NULL) {
18187c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18197c478bd9Sstevel@tonic-gate 		rc = -1;
18207c478bd9Sstevel@tonic-gate 		goto return_rc;
18217c478bd9Sstevel@tonic-gate 	}
18227c478bd9Sstevel@tonic-gate 	DPRINTF(("got event %d\n", xlate->tx_internal_event));
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	if (adt_selected(event_state, xlate->tx_internal_event, status))
18257c478bd9Sstevel@tonic-gate 		adt_generate_event(event, event_state, xlate);
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate return_rc:
18287c478bd9Sstevel@tonic-gate 	return (rc);
18297c478bd9Sstevel@tonic-gate }
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate /*
18327c478bd9Sstevel@tonic-gate  * adt_free_event -- invalidate and free
18337c478bd9Sstevel@tonic-gate  */
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate void
18367c478bd9Sstevel@tonic-gate adt_free_event(adt_event_data_t *event)
18377c478bd9Sstevel@tonic-gate {
18387c478bd9Sstevel@tonic-gate 	struct adt_event_state	*event_state;
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	if (event == NULL)
18417c478bd9Sstevel@tonic-gate 		return;
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	event_state = (struct adt_event_state *)event;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	assert(event_state->ae_check == ADT_VALID);
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	event_state->ae_check = 0;
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	free(event_state);
18507c478bd9Sstevel@tonic-gate }
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate /*
18537c478bd9Sstevel@tonic-gate  * adt_is_selected -- helper to adt_selected(), below.
18547c478bd9Sstevel@tonic-gate  *
18557c478bd9Sstevel@tonic-gate  * "sorf" is "success or fail" status; au_preselect compares
18567c478bd9Sstevel@tonic-gate  * that with success, fail, or both.
18577c478bd9Sstevel@tonic-gate  */
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate static int
18607c478bd9Sstevel@tonic-gate adt_is_selected(au_event_t e, au_mask_t *m, int sorf)
18617c478bd9Sstevel@tonic-gate {
18627c478bd9Sstevel@tonic-gate 	int prs_sorf;
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	if (sorf == 0)
18657c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_SUCCESS;
18667c478bd9Sstevel@tonic-gate 	else
18677c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_FAILURE;
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD));
18707c478bd9Sstevel@tonic-gate }
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate /*
18737c478bd9Sstevel@tonic-gate  * selected -- see if this event is preselected.
18747c478bd9Sstevel@tonic-gate  *
18757c478bd9Sstevel@tonic-gate  * if errors are encountered trying to check a preselection mask
18767c478bd9Sstevel@tonic-gate  * or look up a user name, the event is selected.  Otherwise, the
18777c478bd9Sstevel@tonic-gate  * preselection mask is used for the job.
18787c478bd9Sstevel@tonic-gate  */
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate static int
18817c478bd9Sstevel@tonic-gate adt_selected(struct adt_event_state *event, au_event_t actual_id, int status)
18827c478bd9Sstevel@tonic-gate {
18837c478bd9Sstevel@tonic-gate 	adt_internal_state_t *sp;
18847c478bd9Sstevel@tonic-gate 	au_mask_t	namask;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	sp = event->ae_session;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	if ((sp->as_have_user_data & ADT_HAVE_IDS) == 0) {
18897c478bd9Sstevel@tonic-gate 		adt_write_syslog("No user data available", EINVAL);
18907c478bd9Sstevel@tonic-gate 		return (1);	/* default is "selected" */
18917c478bd9Sstevel@tonic-gate 	}
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	/* non-attributable? */
18947c478bd9Sstevel@tonic-gate 	if ((sp->as_info.ai_auid == AU_NOAUDITID) ||
18957c478bd9Sstevel@tonic-gate 	    (sp->as_info.ai_auid == ADT_NO_AUDIT)) {
18967c478bd9Sstevel@tonic-gate 		if (auditon(A_GETKMASK, (caddr_t)&namask,
18977c478bd9Sstevel@tonic-gate 		    sizeof (namask)) != 0) {
18987c478bd9Sstevel@tonic-gate 			adt_write_syslog("auditon failure", errno);
18997c478bd9Sstevel@tonic-gate 			return (1);
19007c478bd9Sstevel@tonic-gate 		}
19017c478bd9Sstevel@tonic-gate 		return (adt_is_selected(actual_id, &namask, status));
19027c478bd9Sstevel@tonic-gate 	} else {
19037c478bd9Sstevel@tonic-gate 		return (adt_is_selected(actual_id, &(sp->as_info.ai_mask),
19047c478bd9Sstevel@tonic-gate 		    status));
19057c478bd9Sstevel@tonic-gate 	}
19067c478bd9Sstevel@tonic-gate }
1907