xref: /onnv-gate/usr/src/lib/libbsm/common/adt.c (revision 12948:59a6f9b695ef)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51641Spaulson  * Common Development and Distribution License (the "License").
61641Spaulson  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216812Sraf 
220Sstevel@tonic-gate /*
2312143Sgww@eng.sun.com  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <bsm/adt.h>
270Sstevel@tonic-gate #include <bsm/adt_event.h>
280Sstevel@tonic-gate #include <assert.h>
290Sstevel@tonic-gate #include <bsm/audit.h>
300Sstevel@tonic-gate #include <bsm/audit_record.h>
310Sstevel@tonic-gate #include <bsm/libbsm.h>
320Sstevel@tonic-gate #include <door.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <generic.h>
350Sstevel@tonic-gate #include <md5.h>
360Sstevel@tonic-gate #include <sys/mkdev.h>
370Sstevel@tonic-gate #include <netdb.h>
380Sstevel@tonic-gate #include <nss_dbdefs.h>
390Sstevel@tonic-gate #include <pwd.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include <time.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <synch.h>
450Sstevel@tonic-gate #include <sys/systeminfo.h>
460Sstevel@tonic-gate #include <syslog.h>
470Sstevel@tonic-gate #include <thread.h>
480Sstevel@tonic-gate #include <unistd.h>
490Sstevel@tonic-gate #include <adt_xlate.h>
500Sstevel@tonic-gate #include <adt_ucred.h>
517091Sgww #include <arpa/inet.h>
527091Sgww #include <net/if.h>
537091Sgww #include <libinetutil.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static int adt_selected(struct adt_event_state *, au_event_t, int);
560Sstevel@tonic-gate static int adt_init(adt_internal_state_t *, int);
570Sstevel@tonic-gate static int adt_import(adt_internal_state_t *, const adt_export_data_t *);
582835Sgww static m_label_t *adt_ucred_label(ucred_t *);
593110Sgww static void adt_setto_unaudited(adt_internal_state_t *);
607091Sgww static int adt_get_local_address(int, struct ifaddrlist *);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #ifdef C2_DEBUG
6311706SJan.Friedel@Sun.COM #define	DPRINTF(x) { (void) printf x; }
6411706SJan.Friedel@Sun.COM #define	DFLUSH (void) fflush(stdout);
650Sstevel@tonic-gate #else
660Sstevel@tonic-gate #define	DPRINTF(x)
670Sstevel@tonic-gate #define	DFLUSH
680Sstevel@tonic-gate #endif
690Sstevel@tonic-gate 
7012143Sgww@eng.sun.com /*
7112143Sgww@eng.sun.com  * Local audit states are a bit mask
7212143Sgww@eng.sun.com  *
7312143Sgww@eng.sun.com  * The global audit states are
7412143Sgww@eng.sun.com  *
7512143Sgww@eng.sun.com  * AUC_UNSET             0      - on/off hasn't been decided
7612143Sgww@eng.sun.com  * AUC_ENABLED           1      - loaded and enabled
7712143Sgww@eng.sun.com  *
7812143Sgww@eng.sun.com  * The local Zone states are
7912143Sgww@eng.sun.com  *
8012143Sgww@eng.sun.com  * AUC_AUDITING         0x1     - audit daemon is active
8112143Sgww@eng.sun.com  * AUC_NOAUDIT          0x2     - audit daemon is not active
8212143Sgww@eng.sun.com  * AUC_INIT_AUDIT       0x4     - audit is ready but auditd has not run
8312143Sgww@eng.sun.com  * AUC_NOSPACE          0x8     - audit enabled, no space for audit records
8412143Sgww@eng.sun.com  *
8512143Sgww@eng.sun.com  * The only values returned by auditon(A_GETCOND) are:
8612143Sgww@eng.sun.com  * AUC_INIT_AUDIT, AUC_AUDITING, AUC_NOAUDIT, AUC_NOSPACE
8712143Sgww@eng.sun.com  *
8812143Sgww@eng.sun.com  * The pseudo audit state used when the c2audit module is excluded is
8912143Sgww@eng.sun.com  *
9012143Sgww@eng.sun.com  * AUC_DISABLED		0x100	- c2audit module is excluded
9112143Sgww@eng.sun.com  */
9212143Sgww@eng.sun.com 
930Sstevel@tonic-gate static int auditstate = AUC_DISABLED;	/* default state */
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * adt_write_syslog
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * errors that are not the user's fault (bugs or whatever in
990Sstevel@tonic-gate  * the underlying audit code are noted in syslog.)
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * Avoid calling adt_write_syslog for things that can happen
1020Sstevel@tonic-gate  * at high volume.
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  * syslog's open (openlog) and close (closelog) are interesting;
1050Sstevel@tonic-gate  * openlog *may* create a file descriptor and is optional.  closelog
1060Sstevel@tonic-gate  * *will* close any open file descriptors and is also optional.
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  * Since syslog may also be used by the calling application, the
1090Sstevel@tonic-gate  * choice is to avoid openlog, which sets some otherwise useful
1100Sstevel@tonic-gate  * parameters, and to embed "Solaris_audit" in the log message.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate void
adt_write_syslog(const char * message,int err)1140Sstevel@tonic-gate adt_write_syslog(const char *message, int err)
1150Sstevel@tonic-gate {
1167091Sgww 	int	save_errno = errno;
1170Sstevel@tonic-gate 	int	mask_priority;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	DPRINTF(("syslog called: %s\n", message));
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	mask_priority = setlogmask(LOG_MASK(LOG_ALERT));
1227091Sgww 	errno = err;
1237091Sgww 	syslog(LOG_ALERT, "Solaris_audit %s: %m", message);
1240Sstevel@tonic-gate 	(void) setlogmask(mask_priority);
1250Sstevel@tonic-gate 	errno = save_errno;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
12912143Sgww@eng.sun.com  * return true if c2audit is not excluded.
1300Sstevel@tonic-gate  *
13112143Sgww@eng.sun.com  *	For purpose of this API, anything but AUC_DISABLED
13212143Sgww@eng.sun.com  *	is enabled; however one never actually sees
13312143Sgww@eng.sun.com  *	AUC_DISABLED since auditon returns ENOTSUP in that case.  Any
13412143Sgww@eng.sun.com  *	auditon error is considered the same as ENOTSUP for our
1350Sstevel@tonic-gate  *	purpose.  auditstate is not changed by auditon if an error
1360Sstevel@tonic-gate  *	is returned.
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate 
1396680Sgww /*
1406680Sgww  * XXX	this should probably be eliminated and adt_audit_state() replace it.
1416680Sgww  *	All the legitimate uses	are to not fork a waiting process for
1426680Sgww  *	process exit processing, as in su, login, dtlogin.  Other bogus
1436680Sgww  *	users are zoneadmd and init.
1446680Sgww  *	All but dtlogin are in ON, so we can do this without cross gate
1456680Sgww  *	synchronization.
14612143Sgww@eng.sun.com  *
14712143Sgww@eng.sun.com  *	No longer used in adt.c.
1486680Sgww  */
1496680Sgww 
1500Sstevel@tonic-gate boolean_t
adt_audit_enabled(void)1516680Sgww adt_audit_enabled(void)
1526680Sgww {
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	(void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate));
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	return (auditstate != AUC_DISABLED);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /*
1606680Sgww  *	See adt_audit_enabled() for state discussions.
1616680Sgww  *	The state parameter is a hedge until all the uses become clear.
1626680Sgww  *	Likely if adt_audit_enabled is brought internal to this file,
16312918SJan.Friedel@Sun.COM  *	it could be modified to take one or more parameters to describe the
16412918SJan.Friedel@Sun.COM  *	state.
1656680Sgww  */
1666680Sgww 
1676680Sgww boolean_t
adt_audit_state(int states)16812918SJan.Friedel@Sun.COM adt_audit_state(int states)
1696680Sgww {
1706680Sgww 
1716680Sgww 	(void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate));
1726680Sgww 
17312918SJan.Friedel@Sun.COM 	return ((auditstate & states) ? B_TRUE : B_FALSE);
1746680Sgww }
1756680Sgww 
1766680Sgww /*
17712918SJan.Friedel@Sun.COM  * Get user_specific/non-attributable audit mask. This may be called even when
17812918SJan.Friedel@Sun.COM  * auditing is off.
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate static int
adt_get_mask_from_user(uid_t uid,au_mask_t * mask)1820Sstevel@tonic-gate adt_get_mask_from_user(uid_t uid, au_mask_t *mask)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	struct passwd	pwd;
18512918SJan.Friedel@Sun.COM 	long		buff_sz;
18612918SJan.Friedel@Sun.COM 	char		*pwd_buff;
18712918SJan.Friedel@Sun.COM 
1880Sstevel@tonic-gate 
18912143Sgww@eng.sun.com 	if (auditstate & AUC_DISABLED) {
19012143Sgww@eng.sun.com 		/* c2audit excluded */
1910Sstevel@tonic-gate 		mask->am_success = 0;
1920Sstevel@tonic-gate 		mask->am_failure = 0;
1934321Scasper 	} else if (uid <= MAXUID) {
19412918SJan.Friedel@Sun.COM 		if ((buff_sz = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) {
19512918SJan.Friedel@Sun.COM 			adt_write_syslog("couldn't determine maximum size of "
19612918SJan.Friedel@Sun.COM 			    "password buffer", errno);
1970Sstevel@tonic-gate 			return (-1);
1980Sstevel@tonic-gate 		}
19912918SJan.Friedel@Sun.COM 		if ((pwd_buff = calloc(1, (size_t)++buff_sz)) == NULL) {
20012918SJan.Friedel@Sun.COM 			return (-1);
20112918SJan.Friedel@Sun.COM 		}
20212918SJan.Friedel@Sun.COM 		if (getpwuid_r(uid, &pwd, pwd_buff, (int)buff_sz) == NULL) {
20312918SJan.Friedel@Sun.COM 			errno = EINVAL;	/* user doesn't exist */
20412918SJan.Friedel@Sun.COM 			free(pwd_buff);
20512918SJan.Friedel@Sun.COM 			return (-1);
20612918SJan.Friedel@Sun.COM 		}
20712918SJan.Friedel@Sun.COM 		free(pwd_buff);
2080Sstevel@tonic-gate 		if (au_user_mask(pwd.pw_name, mask)) {
2090Sstevel@tonic-gate 			errno = EFAULT; /* undetermined failure */
2100Sstevel@tonic-gate 			return (-1);
2110Sstevel@tonic-gate 		}
21212918SJan.Friedel@Sun.COM 	} else if (auditon(A_GETKMASK, (caddr_t)mask, sizeof (*mask)) == -1) {
2130Sstevel@tonic-gate 			return (-1);
2140Sstevel@tonic-gate 	}
21512918SJan.Friedel@Sun.COM 
2160Sstevel@tonic-gate 	return (0);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate  * adt_get_unique_id -- generate a hopefully unique 32 bit value
2210Sstevel@tonic-gate  *
2220Sstevel@tonic-gate  * there will be a follow up to replace this with the use of /dev/random
2230Sstevel@tonic-gate  *
2240Sstevel@tonic-gate  * An MD5 hash is taken on a buffer of
2250Sstevel@tonic-gate  *     hostname . audit id . unix time . pid . count
2260Sstevel@tonic-gate  *
2270Sstevel@tonic-gate  * "count = noise++;" is subject to a race condition but I don't
2280Sstevel@tonic-gate  * see a need to put a lock around it.
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate 
2317649Sgww@eng.sun.com au_asid_t
adt_get_unique_id(au_id_t uid)2325777Stw21770 adt_get_unique_id(au_id_t uid)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	char		hostname[MAXHOSTNAMELEN];
2350Sstevel@tonic-gate 	union {
2360Sstevel@tonic-gate 		au_id_t		v[4];
2370Sstevel@tonic-gate 		unsigned char	obuff[128/8];
2380Sstevel@tonic-gate 	} output;
2390Sstevel@tonic-gate 	MD5_CTX	context;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	static int	noise = 0;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	int		count = noise++;
2440Sstevel@tonic-gate 	time_t		timebits = time(NULL);
2450Sstevel@tonic-gate 	pid_t		pidbits = getpid();
2467649Sgww@eng.sun.com 	au_asid_t	retval = 0;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	if (gethostname(hostname, MAXHOSTNAMELEN)) {
2490Sstevel@tonic-gate 		adt_write_syslog("gethostname call failed", errno);
2500Sstevel@tonic-gate 		(void) strncpy(hostname, "invalidHostName", MAXHOSTNAMELEN);
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	while (retval == 0) {  /* 0 is the only invalid result */
2540Sstevel@tonic-gate 		MD5Init(&context);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *)hostname,
2570Sstevel@tonic-gate 		    (unsigned int) strlen((const char *)hostname));
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &uid, sizeof (uid_t));
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		MD5Update(&context,
2620Sstevel@tonic-gate 		    (unsigned char *) &timebits, sizeof (time_t));
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &pidbits,
2650Sstevel@tonic-gate 		    sizeof (pid_t));
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 		MD5Update(&context, (unsigned char *) &(count), sizeof (int));
2680Sstevel@tonic-gate 		MD5Final(output.obuff, &context);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		retval = output.v[count % 4];
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	return (retval);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * the following "port" function deals with the following issues:
2770Sstevel@tonic-gate  *
2780Sstevel@tonic-gate  * 1    the kernel and ucred deal with a dev_t as a 64 bit value made
2790Sstevel@tonic-gate  *      up from a 32 bit major and 32 bit minor.
2800Sstevel@tonic-gate  * 2    User space deals with a dev_t as either the above 64 bit value
2810Sstevel@tonic-gate  *      or a 32 bit value made from a 14 bit major and an 18 bit minor.
2820Sstevel@tonic-gate  * 3    The various audit interfaces (except ucred) pass the 32 or
2830Sstevel@tonic-gate  *      64 bit version depending the architecture of the userspace
2840Sstevel@tonic-gate  *      application.  If you get a port value from ucred and pass it
2850Sstevel@tonic-gate  *      to the kernel via auditon(), it must be squeezed into a 32
2860Sstevel@tonic-gate  *      bit value because the kernel knows the userspace app's bit
2870Sstevel@tonic-gate  *      size.
2880Sstevel@tonic-gate  *
2890Sstevel@tonic-gate  * The internal state structure for adt (adt_internal_state_t) uses
2900Sstevel@tonic-gate  * dev_t, so adt converts data from ucred to fit.  The import/export
2910Sstevel@tonic-gate  * functions, however, can't know if they are importing/exporting
2920Sstevel@tonic-gate  * from 64 or 32 bit applications, so they always send 64 bits and
2930Sstevel@tonic-gate  * the 32 bit end(s) are responsible to convert 32 -> 64 -> 32 as
2940Sstevel@tonic-gate  * appropriate.
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * adt_cpy_tid() -- if lib is 64 bit, just copy it (dev_t and port are
2990Sstevel@tonic-gate  * both 64 bits).  If lib is 32 bits, squeeze the two-int port into
3000Sstevel@tonic-gate  * a 32 bit dev_t.  A port fits in the "minor" part of au_port_t,
3010Sstevel@tonic-gate  * so it isn't broken up into pieces.  (When it goes to the kernel
3020Sstevel@tonic-gate  * and back, however, it will have been split into major/minor
3030Sstevel@tonic-gate  * pieces.)
3040Sstevel@tonic-gate  */
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate static void
adt_cpy_tid(au_tid_addr_t * dest,const au_tid64_addr_t * src)3070Sstevel@tonic-gate adt_cpy_tid(au_tid_addr_t *dest, const au_tid64_addr_t *src)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate #ifdef _LP64
3100Sstevel@tonic-gate 	(void) memcpy(dest, src, sizeof (au_tid_addr_t));
3117091Sgww #else	/* _LP64 */
3120Sstevel@tonic-gate 	dest->at_type = src->at_type;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	dest->at_port  = src->at_port.at_minor & MAXMIN32;
3150Sstevel@tonic-gate 	dest->at_port |= (src->at_port.at_major & MAXMAJ32) <<
3160Sstevel@tonic-gate 	    NBITSMINOR32;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	(void) memcpy(dest->at_addr, src->at_addr, 4 * sizeof (uint32_t));
3197091Sgww #endif	/* _LP64 */
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate  * adt_start_session -- create interface handle, create context
3240Sstevel@tonic-gate  *
3250Sstevel@tonic-gate  * The imported_state input is normally NULL, if not, it represents
3260Sstevel@tonic-gate  * a continued session; its values obviate the need for a subsequent
3270Sstevel@tonic-gate  * call to adt_set_user().
3280Sstevel@tonic-gate  *
3292478Sgww  * The flag is used to decide how to set the initial state of the session.
3302478Sgww  * If 0, the session is "no audit" until a call to adt_set_user; if
3312478Sgww  * ADT_USE_PROC_DATA, the session is built from the process audit
3320Sstevel@tonic-gate  * characteristics obtained from the kernel.  If imported_state is
3330Sstevel@tonic-gate  * not NULL, the resulting audit mask is an OR of the current process
3340Sstevel@tonic-gate  * audit mask and that passed in.
3350Sstevel@tonic-gate  *
3360Sstevel@tonic-gate  * The basic model is that the caller can use the pointer returned
3370Sstevel@tonic-gate  * by adt_start_session whether or not auditing is enabled or an
3380Sstevel@tonic-gate  * error was returned.  The functions that take the session handle
3390Sstevel@tonic-gate  * as input generally return without doing anything if auditing is
3400Sstevel@tonic-gate  * disabled.
3410Sstevel@tonic-gate  */
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate int
adt_start_session(adt_session_data_t ** new_session,const adt_export_data_t * imported_state,adt_session_flags_t flags)3440Sstevel@tonic-gate adt_start_session(adt_session_data_t **new_session,
3452478Sgww     const adt_export_data_t *imported_state, adt_session_flags_t flags)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	adt_internal_state_t	*state;
3480Sstevel@tonic-gate 	adt_session_flags_t	flgmask = ADT_FLAGS_ALL;
3490Sstevel@tonic-gate 
35012143Sgww@eng.sun.com 	/* test and set auditstate */
35112143Sgww@eng.sun.com 	if (adt_audit_state(AUC_DISABLED)) {
35212143Sgww@eng.sun.com 		/* c2audit excluded */
35312143Sgww@eng.sun.com 		*new_session = NULL;
35412143Sgww@eng.sun.com 		return (0);
35512143Sgww@eng.sun.com 	}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	if ((flags & ~flgmask) != 0) {
3580Sstevel@tonic-gate 		errno = EINVAL;
3590Sstevel@tonic-gate 		goto return_err;
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
36212143Sgww@eng.sun.com 	if ((state = calloc(1, sizeof (adt_internal_state_t))) == NULL) {
3630Sstevel@tonic-gate 		goto return_err;
36412143Sgww@eng.sun.com 	}
3650Sstevel@tonic-gate 
36612143Sgww@eng.sun.com 	if (adt_init(state, flags & ADT_USE_PROC_DATA) != 0) {
3670Sstevel@tonic-gate 		goto return_err_free;    /* errno from adt_init() */
36812143Sgww@eng.sun.com 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/*
3710Sstevel@tonic-gate 	 * The imported state overwrites the initial state if the
3720Sstevel@tonic-gate 	 * imported state represents a valid audit trail
3730Sstevel@tonic-gate 	 */
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	if (imported_state != NULL) {
3762835Sgww 		if (adt_import(state, imported_state) != 0) {
3770Sstevel@tonic-gate 			goto return_err_free;
3782835Sgww 		}
3792478Sgww 	} else if (flags & ADT_USE_PROC_DATA) {
3802478Sgww 		state->as_session_model = ADT_PROCESS_MODEL;
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 	state->as_flags = flags;
38311706SJan.Friedel@Sun.COM 	DPRINTF(("(%lld) Starting session id = %08X\n",
38411706SJan.Friedel@Sun.COM 	    (long long) getpid(), state->as_info.ai_asid));
3850Sstevel@tonic-gate 
38612143Sgww@eng.sun.com 	*new_session = (adt_session_data_t *)state;
38712143Sgww@eng.sun.com 	return (0);
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate return_err_free:
3900Sstevel@tonic-gate 	free(state);
3910Sstevel@tonic-gate return_err:
39212143Sgww@eng.sun.com 	*new_session = NULL;
3930Sstevel@tonic-gate 	adt_write_syslog("audit session create failed", errno);
3940Sstevel@tonic-gate 	return (-1);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
3987496Sgww@eng.sun.com  * adt_load_table()
3997496Sgww@eng.sun.com  *
4007496Sgww@eng.sun.com  * loads the event translation table into the audit session.
4017496Sgww@eng.sun.com  */
4027496Sgww@eng.sun.com 
4037496Sgww@eng.sun.com void
adt_load_table(const adt_session_data_t * session_data,adt_translation_t ** xlate,void (* preload)(au_event_t,adt_event_data_t *))4047496Sgww@eng.sun.com adt_load_table(const adt_session_data_t *session_data,
4057496Sgww@eng.sun.com     adt_translation_t **xlate, void (*preload)(au_event_t, adt_event_data_t *))
4067496Sgww@eng.sun.com {
4077496Sgww@eng.sun.com 	adt_internal_state_t	*state = (adt_internal_state_t *)session_data;
4087496Sgww@eng.sun.com 
4097496Sgww@eng.sun.com 	if (state != NULL) {
4107496Sgww@eng.sun.com 		assert(state->as_check == ADT_VALID);
4117496Sgww@eng.sun.com 		state->as_xlate = xlate;
4127496Sgww@eng.sun.com 		state->as_preload = preload;
4137496Sgww@eng.sun.com 	}
4147496Sgww@eng.sun.com }
4157496Sgww@eng.sun.com 
4167496Sgww@eng.sun.com /*
4170Sstevel@tonic-gate  * adt_get_asid() and adt_set_asid()
4180Sstevel@tonic-gate  *
4190Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
4200Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
4210Sstevel@tonic-gate  * adt_proccess_attr()
4220Sstevel@tonic-gate  *
4230Sstevel@tonic-gate  * neither of these are intended for general use and will likely
4240Sstevel@tonic-gate  * remain private interfaces for a long time.  Forever is a long
4250Sstevel@tonic-gate  * time.  In the case of adt_set_asid(), you should have a very,
4260Sstevel@tonic-gate  * very good reason for setting your own session id.  The process
4270Sstevel@tonic-gate  * audit characteristics are not changed by put, use adt_set_proc().
4280Sstevel@tonic-gate  *
4290Sstevel@tonic-gate  * These are "volatile" (more changable than "evolving") and will
4300Sstevel@tonic-gate  * probably change in the S10 period.
4310Sstevel@tonic-gate  */
4322478Sgww 
4330Sstevel@tonic-gate void
adt_get_asid(const adt_session_data_t * session_data,au_asid_t * asid)4340Sstevel@tonic-gate adt_get_asid(const adt_session_data_t *session_data, au_asid_t *asid)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	if (session_data == NULL) {
4380Sstevel@tonic-gate 		*asid = 0;
4390Sstevel@tonic-gate 	} else {
4400Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
4410Sstevel@tonic-gate 		    ADT_VALID);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 		*asid = ((adt_internal_state_t *)session_data)->as_info.ai_asid;
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate void
adt_set_asid(const adt_session_data_t * session_data,const au_asid_t session_id)4482478Sgww adt_set_asid(const adt_session_data_t *session_data, const au_asid_t session_id)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	if (session_data != NULL) {
4520Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
4530Sstevel@tonic-gate 		    ADT_VALID);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
4560Sstevel@tonic-gate 		    ADT_HAVE_ASID;
4570Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_asid =
4580Sstevel@tonic-gate 		    session_id;
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate  * adt_get_auid() and adt_set_auid()
4640Sstevel@tonic-gate  *
4650Sstevel@tonic-gate  * neither of these are intended for general use and will likely
4660Sstevel@tonic-gate  * remain private interfaces for a long time.  Forever is a long
4670Sstevel@tonic-gate  * time.  In the case of adt_set_auid(), you should have a very,
4680Sstevel@tonic-gate  * very good reason for setting your own audit id.  The process
4690Sstevel@tonic-gate  * audit characteristics are not changed by put, use adt_set_proc().
4700Sstevel@tonic-gate  */
4712478Sgww 
4720Sstevel@tonic-gate void
adt_get_auid(const adt_session_data_t * session_data,au_id_t * auid)4730Sstevel@tonic-gate adt_get_auid(const adt_session_data_t *session_data, au_id_t *auid)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	if (session_data == NULL) {
4770Sstevel@tonic-gate 		*auid = AU_NOAUDITID;
4780Sstevel@tonic-gate 	} else {
4790Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
4800Sstevel@tonic-gate 		    ADT_VALID);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 		*auid = ((adt_internal_state_t *)session_data)->as_info.ai_auid;
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate void
adt_set_auid(const adt_session_data_t * session_data,const au_id_t audit_id)4870Sstevel@tonic-gate adt_set_auid(const adt_session_data_t *session_data, const au_id_t audit_id)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if (session_data != NULL) {
4910Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
4920Sstevel@tonic-gate 		    ADT_VALID);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
4950Sstevel@tonic-gate 		    ADT_HAVE_AUID;
4960Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_auid =
4970Sstevel@tonic-gate 		    audit_id;
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /*
5020Sstevel@tonic-gate  * adt_get_termid(), adt_set_termid()
5030Sstevel@tonic-gate  *
5040Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
5050Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
5060Sstevel@tonic-gate  * adt_proccess_attr()
5070Sstevel@tonic-gate  *
5080Sstevel@tonic-gate  * The process  audit characteristics are not changed by put, use
5090Sstevel@tonic-gate  * adt_set_proc().
5100Sstevel@tonic-gate  */
5112478Sgww 
5120Sstevel@tonic-gate void
adt_get_termid(const adt_session_data_t * session_data,au_tid_addr_t * termid)5130Sstevel@tonic-gate adt_get_termid(const adt_session_data_t *session_data, au_tid_addr_t *termid)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (session_data == NULL) {
5170Sstevel@tonic-gate 		(void) memset(termid, 0, sizeof (au_tid_addr_t));
5180Sstevel@tonic-gate 		termid->at_type = AU_IPv4;
5190Sstevel@tonic-gate 	} else {
5200Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5210Sstevel@tonic-gate 		    ADT_VALID);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 		*termid =
5240Sstevel@tonic-gate 		    ((adt_internal_state_t *)session_data)->as_info.ai_termid;
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate void
adt_set_termid(const adt_session_data_t * session_data,const au_tid_addr_t * termid)5290Sstevel@tonic-gate adt_set_termid(const adt_session_data_t *session_data,
5300Sstevel@tonic-gate     const au_tid_addr_t *termid)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	if (session_data != NULL) {
5340Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5350Sstevel@tonic-gate 		    ADT_VALID);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_termid =
5385777Stw21770 		    *termid;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
5415777Stw21770 		    ADT_HAVE_TID;
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate  * adt_get_mask(), adt_set_mask()
5470Sstevel@tonic-gate  *
5480Sstevel@tonic-gate  * if you use this interface, you are responsible to insure that the
5490Sstevel@tonic-gate  * rest of the session data is populated correctly before calling
5500Sstevel@tonic-gate  * adt_proccess_attr()
5510Sstevel@tonic-gate  *
5520Sstevel@tonic-gate  * The process  audit characteristics are not changed by put, use
5530Sstevel@tonic-gate  * adt_set_proc().
5540Sstevel@tonic-gate  */
5552478Sgww 
5560Sstevel@tonic-gate void
adt_get_mask(const adt_session_data_t * session_data,au_mask_t * mask)5570Sstevel@tonic-gate adt_get_mask(const adt_session_data_t *session_data, au_mask_t *mask)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	if (session_data == NULL) {
5610Sstevel@tonic-gate 		mask->am_success = 0;
5620Sstevel@tonic-gate 		mask->am_failure = 0;
5630Sstevel@tonic-gate 	} else {
5640Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5650Sstevel@tonic-gate 		    ADT_VALID);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 		*mask = ((adt_internal_state_t *)session_data)->as_info.ai_mask;
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate void
adt_set_mask(const adt_session_data_t * session_data,const au_mask_t * mask)5720Sstevel@tonic-gate adt_set_mask(const adt_session_data_t *session_data, const au_mask_t *mask)
5730Sstevel@tonic-gate {
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	if (session_data != NULL) {
5760Sstevel@tonic-gate 		assert(((adt_internal_state_t *)session_data)->as_check ==
5770Sstevel@tonic-gate 		    ADT_VALID);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_info.ai_mask = *mask;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		((adt_internal_state_t *)session_data)->as_have_user_data |=
5820Sstevel@tonic-gate 		    ADT_HAVE_MASK;
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate /*
5870Sstevel@tonic-gate  * helpers for adt_load_termid
5880Sstevel@tonic-gate  */
5892478Sgww 
5900Sstevel@tonic-gate static void
adt_do_ipv6_address(struct sockaddr_in6 * peer,struct sockaddr_in6 * sock,au_tid_addr_t * termid)5910Sstevel@tonic-gate adt_do_ipv6_address(struct sockaddr_in6 *peer, struct sockaddr_in6 *sock,
5920Sstevel@tonic-gate     au_tid_addr_t *termid)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	termid->at_port = ((peer->sin6_port<<16) | (sock->sin6_port));
5960Sstevel@tonic-gate 	termid->at_type = AU_IPv6;
5970Sstevel@tonic-gate 	(void) memcpy(termid->at_addr, &peer->sin6_addr, 4 * sizeof (uint_t));
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate static void
adt_do_ipv4_address(struct sockaddr_in * peer,struct sockaddr_in * sock,au_tid_addr_t * termid)6010Sstevel@tonic-gate adt_do_ipv4_address(struct sockaddr_in *peer, struct sockaddr_in *sock,
6020Sstevel@tonic-gate     au_tid_addr_t *termid)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	termid->at_port = ((peer->sin_port<<16) | (sock->sin_port));
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	termid->at_type = AU_IPv4;
6080Sstevel@tonic-gate 	termid->at_addr[0] = (uint32_t)peer->sin_addr.s_addr;
6090Sstevel@tonic-gate 	(void) memset(&(termid->at_addr[1]), 0, 3 * sizeof (uint_t));
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate /*
6130Sstevel@tonic-gate  * adt_load_termid:  convenience function; inputs file handle and
6140Sstevel@tonic-gate  * outputs an au_tid_addr struct.
6150Sstevel@tonic-gate  *
6160Sstevel@tonic-gate  * This code was stolen from audit_settid.c; it differs from audit_settid()
6170Sstevel@tonic-gate  * in that it does not write the terminal id to the process.
6180Sstevel@tonic-gate  */
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate int
adt_load_termid(int fd,adt_termid_t ** termid)6210Sstevel@tonic-gate adt_load_termid(int fd, adt_termid_t **termid)
6220Sstevel@tonic-gate {
6230Sstevel@tonic-gate 	au_tid_addr_t		*p_term;
6240Sstevel@tonic-gate 	struct sockaddr_in6	peer;
6250Sstevel@tonic-gate 	struct sockaddr_in6	sock;
6260Sstevel@tonic-gate 	int			peerlen = sizeof (peer);
6270Sstevel@tonic-gate 	int			socklen = sizeof (sock);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	/* get peer name if its a socket, else assume local terminal */
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	if (getpeername(fd, (struct sockaddr *)&peer, (socklen_t *)&peerlen)
6320Sstevel@tonic-gate 	    < 0) {
63312143Sgww@eng.sun.com 		if (errno == ENOTSOCK) {
6340Sstevel@tonic-gate 			return (adt_load_hostname(NULL, termid));
63512143Sgww@eng.sun.com 		}
6360Sstevel@tonic-gate 		goto return_err;
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
63912143Sgww@eng.sun.com 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
6400Sstevel@tonic-gate 		goto return_err;
64112143Sgww@eng.sun.com 	}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	/* get sock name */
6440Sstevel@tonic-gate 	if (getsockname(fd, (struct sockaddr *)&sock,
64512143Sgww@eng.sun.com 	    (socklen_t *)&socklen) < 0) {
6460Sstevel@tonic-gate 		goto return_err_free;
64712143Sgww@eng.sun.com 	}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	if (peer.sin6_family == AF_INET6) {
6500Sstevel@tonic-gate 		adt_do_ipv6_address(&peer, &sock, p_term);
6510Sstevel@tonic-gate 	} else {
6520Sstevel@tonic-gate 		adt_do_ipv4_address((struct sockaddr_in *)&peer,
6530Sstevel@tonic-gate 		    (struct sockaddr_in *)&sock, p_term);
6540Sstevel@tonic-gate 	}
6550Sstevel@tonic-gate 	*termid = (adt_termid_t *)p_term;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	return (0);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate return_err_free:
6600Sstevel@tonic-gate 	free(p_term);
6610Sstevel@tonic-gate return_err:
66212143Sgww@eng.sun.com 	*termid = NULL;
6630Sstevel@tonic-gate 	return (-1);
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate static boolean_t
adt_have_termid(au_tid_addr_t * dest)6670Sstevel@tonic-gate adt_have_termid(au_tid_addr_t *dest)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	struct auditinfo_addr	audit_data;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if (getaudit_addr(&audit_data, sizeof (audit_data)) < 0) {
6720Sstevel@tonic-gate 		adt_write_syslog("getaudit failed", errno);
6730Sstevel@tonic-gate 		return (B_FALSE);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	if ((audit_data.ai_termid.at_type == 0) ||
6770Sstevel@tonic-gate 	    (audit_data.ai_termid.at_addr[0] |
6780Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[1]  |
6790Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[2]  |
6800Sstevel@tonic-gate 	    audit_data.ai_termid.at_addr[3]) == 0)
6810Sstevel@tonic-gate 		return (B_FALSE);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	(void) memcpy(dest, &(audit_data.ai_termid),
6840Sstevel@tonic-gate 	    sizeof (au_tid_addr_t));
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	return (B_TRUE);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
68912143Sgww@eng.sun.com /*
69012143Sgww@eng.sun.com  * adt_get_hostIP - construct a terminal id from a hostname
69112143Sgww@eng.sun.com  *
69212143Sgww@eng.sun.com  *	Returns	 0 = success
69312143Sgww@eng.sun.com  *		-1 = failure and errno = ENETDOWN with the address
69412143Sgww@eng.sun.com  *		     defaulted to IPv4 loopback.
69512143Sgww@eng.sun.com  */
69612143Sgww@eng.sun.com 
6970Sstevel@tonic-gate static int
adt_get_hostIP(const char * hostname,au_tid_addr_t * p_term)6980Sstevel@tonic-gate adt_get_hostIP(const char *hostname, au_tid_addr_t *p_term)
6990Sstevel@tonic-gate {
7007091Sgww 	struct addrinfo	*ai = NULL;
7017091Sgww 	int	tries = 3;
7027091Sgww 	char	msg[512];
7037091Sgww 	int	eai_err;
7040Sstevel@tonic-gate 
7057091Sgww 	while ((tries-- > 0) &&
7067091Sgww 	    ((eai_err = getaddrinfo(hostname, NULL, NULL, &ai)) != 0)) {
7077091Sgww 		/*
7087091Sgww 		 * getaddrinfo returns its own set of errors.
7097091Sgww 		 * Log them here, so any subsequent syslogs will
7107091Sgww 		 * have a context.  adt_get_hostIP callers can only
7117091Sgww 		 * return errno, so subsequent syslogs may be lacking
7127091Sgww 		 * that getaddrinfo failed.
7137091Sgww 		 */
7147091Sgww 		(void) snprintf(msg, sizeof (msg), "getaddrinfo(%s) "
7157091Sgww 		    "failed[%s]", hostname, gai_strerror(eai_err));
7167091Sgww 		adt_write_syslog(msg, 0);
7177091Sgww 
7187091Sgww 		if (eai_err != EAI_AGAIN) {
7197091Sgww 
7200Sstevel@tonic-gate 			break;
7217091Sgww 		}
7227091Sgww 		/* see if resolution becomes available */
7237091Sgww 		(void) sleep(1);
7247091Sgww 	}
7257091Sgww 	if (ai != NULL) {
7267091Sgww 		if (ai->ai_family == AF_INET) {
7277091Sgww 			p_term->at_type = AU_IPv4;
7287091Sgww 			(void) memcpy(p_term->at_addr,
7297091Sgww 			    /* LINTED */
7307091Sgww 			    &((struct sockaddr_in *)ai->ai_addr)->sin_addr,
7317091Sgww 			    AU_IPv4);
7327091Sgww 		} else {
7330Sstevel@tonic-gate 			p_term->at_type = AU_IPv6;
7347091Sgww 			(void) memcpy(p_term->at_addr,
7357091Sgww 			    /* LINTED */
7367091Sgww 			    &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
7377091Sgww 			    AU_IPv6);
7387091Sgww 		}
7397091Sgww 		freeaddrinfo(ai);
7407091Sgww 		return (0);
74112143Sgww@eng.sun.com 	} else if (auditstate & (AUC_AUDITING | AUC_NOSPACE)) {
74212143Sgww@eng.sun.com 		auditinfo_addr_t  audit_info;
74312143Sgww@eng.sun.com 
74412143Sgww@eng.sun.com 		/*
74512143Sgww@eng.sun.com 		 * auditd is running so there should be a
74612143Sgww@eng.sun.com 		 * kernel audit context
74712143Sgww@eng.sun.com 		 */
74812143Sgww@eng.sun.com 		if (auditon(A_GETKAUDIT, (caddr_t)&audit_info,
74912143Sgww@eng.sun.com 		    sizeof (audit_info)) < 0) {
75012143Sgww@eng.sun.com 			adt_write_syslog("unable to get kernel audit context",
75112143Sgww@eng.sun.com 			    errno);
75212143Sgww@eng.sun.com 			goto try_interface;
75312143Sgww@eng.sun.com 		}
75412143Sgww@eng.sun.com 		adt_write_syslog("setting Audit IP address to kernel", 0);
75512143Sgww@eng.sun.com 		*p_term = audit_info.ai_termid;
75612143Sgww@eng.sun.com 		return (0);
75712143Sgww@eng.sun.com 	}
75812143Sgww@eng.sun.com try_interface:
75912143Sgww@eng.sun.com 	{
7607091Sgww 		struct ifaddrlist al;
7617091Sgww 		int	family;
7627091Sgww 		char	ntop[INET6_ADDRSTRLEN];
7637091Sgww 
7647091Sgww 		/*
7657091Sgww 		 * getaddrinfo has failed to map the hostname
7667091Sgww 		 * to an IP address, try to get an IP address
76712143Sgww@eng.sun.com 		 * from a local interface.  If none up, default
76812143Sgww@eng.sun.com 		 * to loopback.
7697091Sgww 		 */
7707091Sgww 		family = AF_INET6;
7717091Sgww 		if (adt_get_local_address(family, &al) != 0) {
7727091Sgww 			family = AF_INET;
7737091Sgww 
7747091Sgww 			if (adt_get_local_address(family, &al) != 0) {
7757091Sgww 				adt_write_syslog("adt_get_local_address "
77612143Sgww@eng.sun.com 				    "failed, no Audit IP address available, "
77712143Sgww@eng.sun.com 				    "faking loopback and error",
7787091Sgww 				    errno);
77912143Sgww@eng.sun.com 				IN_SET_LOOPBACK_ADDR(
78012143Sgww@eng.sun.com 				    (struct sockaddr_in *)&(al.addr.addr));
78112143Sgww@eng.sun.com 				(void) memcpy(p_term->at_addr, &al.addr.addr,
78212143Sgww@eng.sun.com 				    AU_IPv4);
78312143Sgww@eng.sun.com 				p_term->at_type = AU_IPv4;
7847091Sgww 				return (-1);
7857091Sgww 			}
7867091Sgww 		}
7877091Sgww 		if (family == AF_INET) {
7887091Sgww 			p_term->at_type = AU_IPv4;
7897091Sgww 			(void) memcpy(p_term->at_addr, &al.addr.addr, AU_IPv4);
7907091Sgww 		} else {
7917091Sgww 			p_term->at_type = AU_IPv6;
7927091Sgww 			(void) memcpy(p_term->at_addr, &al.addr.addr6, AU_IPv6);
7937091Sgww 		}
7947091Sgww 
7957091Sgww 		(void) snprintf(msg, sizeof (msg), "mapping %s to %s",
7967091Sgww 		    hostname, inet_ntop(family, &(al.addr), ntop,
7977091Sgww 		    sizeof (ntop)));
7987091Sgww 		adt_write_syslog(msg, 0);
7997091Sgww 		return (0);
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate /*
8040Sstevel@tonic-gate  * adt_load_hostname() is called when the caller does not have a file
8050Sstevel@tonic-gate  * handle that gives access to the socket info or any other way to
8060Sstevel@tonic-gate  * pass in both port and ip address.  The hostname input is ignored if
8070Sstevel@tonic-gate  * the terminal id has already been set; instead it returns the
8080Sstevel@tonic-gate  * existing terminal id.
8090Sstevel@tonic-gate  *
81012143Sgww@eng.sun.com  * If c2audit is excluded, success is returned.
81112143Sgww@eng.sun.com  * If the hostname lookup fails, the loopback address is assumed,
81212143Sgww@eng.sun.com  * errno is set to ENETDOWN, this allows the caller to interpret
81312143Sgww@eng.sun.com  * whether failure is fatal, and if not to have a address for the
81412143Sgww@eng.sun.com  * hostname.
81512143Sgww@eng.sun.com  * Otherwise the caller would need to be aware of the audit state.
81612143Sgww@eng.sun.com  *
81712143Sgww@eng.sun.com  * Other errors are ignored if not auditing.
8180Sstevel@tonic-gate  */
8192478Sgww 
8200Sstevel@tonic-gate int
adt_load_hostname(const char * hostname,adt_termid_t ** termid)8210Sstevel@tonic-gate adt_load_hostname(const char *hostname, adt_termid_t **termid)
8220Sstevel@tonic-gate {
8237091Sgww 	char		localhost[MAXHOSTNAMELEN + 1];
8240Sstevel@tonic-gate 	au_tid_addr_t	*p_term;
8250Sstevel@tonic-gate 
82612143Sgww@eng.sun.com 	if (adt_audit_state(AUC_DISABLED)) {
82712143Sgww@eng.sun.com 		/* c2audit excluded */
82812143Sgww@eng.sun.com 		*termid = NULL;
8290Sstevel@tonic-gate 		return (0);
83012143Sgww@eng.sun.com 	}
8310Sstevel@tonic-gate 
83212143Sgww@eng.sun.com 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
8330Sstevel@tonic-gate 		goto return_err;
83412143Sgww@eng.sun.com 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	if (adt_have_termid(p_term)) {
8370Sstevel@tonic-gate 		*termid = (adt_termid_t *)p_term;
8380Sstevel@tonic-gate 		return (0);
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 	p_term->at_port = 0;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	if (hostname == NULL || *hostname == '\0') {
8437091Sgww 		(void) sysinfo(SI_HOSTNAME, localhost, MAXHOSTNAMELEN);
8440Sstevel@tonic-gate 		hostname = localhost;
8450Sstevel@tonic-gate 	}
84612143Sgww@eng.sun.com 	if (adt_get_hostIP(hostname, p_term) == 0) {
84712143Sgww@eng.sun.com 		*termid = (adt_termid_t *)p_term;
84812143Sgww@eng.sun.com 		return (0);
84912143Sgww@eng.sun.com 	} else {
85012143Sgww@eng.sun.com 		*termid = (adt_termid_t *)p_term;
85112143Sgww@eng.sun.com 		return (-1);
85212143Sgww@eng.sun.com 	}
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate return_err:
85512143Sgww@eng.sun.com 	*termid = NULL;
85612143Sgww@eng.sun.com 	if (auditstate & AUC_NOAUDIT) {
8570Sstevel@tonic-gate 		return (0);
85812143Sgww@eng.sun.com 	}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	return (-1);
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate /*
8640Sstevel@tonic-gate  * adt_load_ttyname() is called when the caller does not have a file
8650Sstevel@tonic-gate  * handle that gives access to the local terminal or any other way
8660Sstevel@tonic-gate  * of determining the device id.  The ttyname input is ignored if
8670Sstevel@tonic-gate  * the terminal id has already been set; instead it returns the
8680Sstevel@tonic-gate  * existing terminal id.
8690Sstevel@tonic-gate  *
87012143Sgww@eng.sun.com  * If c2audit is excluded, success is returned.
87112143Sgww@eng.sun.com  * The local hostname is used for the local IP address.
87212143Sgww@eng.sun.com  * If that hostname lookup fails, the loopback address is assumed,
87312143Sgww@eng.sun.com  * errno is set to ENETDOWN, this allows the caller to interpret
87412143Sgww@eng.sun.com  * whether failure is fatal, and if not to have a address for the
87512143Sgww@eng.sun.com  * hostname.
87612143Sgww@eng.sun.com  * Otherwise the caller would need to be aware of the audit state.
87712143Sgww@eng.sun.com  *
87812143Sgww@eng.sun.com  * Other errors are ignored if not auditing.
8790Sstevel@tonic-gate  */
8802478Sgww 
8810Sstevel@tonic-gate int
adt_load_ttyname(const char * ttyname,adt_termid_t ** termid)8820Sstevel@tonic-gate adt_load_ttyname(const char *ttyname, adt_termid_t **termid)
8830Sstevel@tonic-gate {
8847091Sgww 	char		localhost[MAXHOSTNAMELEN + 1];
8850Sstevel@tonic-gate 	au_tid_addr_t	*p_term;
8860Sstevel@tonic-gate 	struct stat	stat_buf;
8870Sstevel@tonic-gate 
88812143Sgww@eng.sun.com 	if (adt_audit_state(AUC_DISABLED)) {
88912143Sgww@eng.sun.com 		/* c2audit excluded */
89012143Sgww@eng.sun.com 		*termid = NULL;
8910Sstevel@tonic-gate 		return (0);
89212143Sgww@eng.sun.com 	}
8930Sstevel@tonic-gate 
89412143Sgww@eng.sun.com 	if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
8950Sstevel@tonic-gate 		goto return_err;
89612143Sgww@eng.sun.com 	}
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	if (adt_have_termid(p_term)) {
8990Sstevel@tonic-gate 		*termid = (adt_termid_t *)p_term;
9000Sstevel@tonic-gate 		return (0);
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	p_term->at_port = 0;
9040Sstevel@tonic-gate 
90512143Sgww@eng.sun.com 	if (sysinfo(SI_HOSTNAME, localhost, MAXHOSTNAMELEN) < 0) {
9060Sstevel@tonic-gate 		goto return_err_free; /* errno from sysinfo */
90712143Sgww@eng.sun.com 	}
9080Sstevel@tonic-gate 
9099082SJan.Friedel@Sun.COM 	if (ttyname != NULL && *ttyname != '\0') {
91012143Sgww@eng.sun.com 		if (stat(ttyname, &stat_buf) < 0) {
9110Sstevel@tonic-gate 			goto return_err_free;
91212143Sgww@eng.sun.com 		}
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 		p_term->at_port = stat_buf.st_rdev;
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
91712143Sgww@eng.sun.com 	if (adt_get_hostIP(localhost, p_term) == 0) {
91812143Sgww@eng.sun.com 		*termid = (adt_termid_t *)p_term;
91912143Sgww@eng.sun.com 		return (0);
92012143Sgww@eng.sun.com 	} else {
92112143Sgww@eng.sun.com 		*termid = (adt_termid_t *)p_term;
92212143Sgww@eng.sun.com 		return (-1);
92312143Sgww@eng.sun.com 	}
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate return_err_free:
9260Sstevel@tonic-gate 	free(p_term);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate return_err:
92912143Sgww@eng.sun.com 	*termid = NULL;
93012143Sgww@eng.sun.com 	if (auditstate & AUC_NOAUDIT) {
9310Sstevel@tonic-gate 		return (0);
93212143Sgww@eng.sun.com 	}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	return (-1);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate /*
9380Sstevel@tonic-gate  * adt_get_session_id returns a stringified representation of
9390Sstevel@tonic-gate  * the audit session id.  See also adt_get_asid() for how to
9400Sstevel@tonic-gate  * get the unexpurgated version.  No guarantees as to how long
9410Sstevel@tonic-gate  * the returned string will be or its general form; hex for now.
9420Sstevel@tonic-gate  *
9430Sstevel@tonic-gate  * An empty string is returned if auditing is off; length = 1
9440Sstevel@tonic-gate  * and the pointer is valid.
9450Sstevel@tonic-gate  *
9460Sstevel@tonic-gate  * returns strlen + 1 if buffer is valid; else 0 and errno.
9470Sstevel@tonic-gate  */
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate size_t
adt_get_session_id(const adt_session_data_t * session_data,char ** buff)9500Sstevel@tonic-gate adt_get_session_id(const adt_session_data_t *session_data, char **buff)
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate 	au_asid_t	session_id;
9530Sstevel@tonic-gate 	size_t		length;
9540Sstevel@tonic-gate 	/*
9550Sstevel@tonic-gate 	 * output is 0x followed by
9560Sstevel@tonic-gate 	 * two characters per byte
9570Sstevel@tonic-gate 	 * plus terminator,
9580Sstevel@tonic-gate 	 * except leading 0's are suppressed, so a few bytes may
9590Sstevel@tonic-gate 	 * be unused.
9600Sstevel@tonic-gate 	 */
9610Sstevel@tonic-gate 	length = 2 + (2 * sizeof (session_id)) + 1;
9620Sstevel@tonic-gate 	*buff = malloc(length);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	if (*buff == NULL) {
9652835Sgww 		return (0);
9660Sstevel@tonic-gate 	}
9670Sstevel@tonic-gate 	if (session_data == NULL) { /* NULL is not an error */
9680Sstevel@tonic-gate 		**buff = '\0';
9692835Sgww 		return (1);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 	adt_get_asid(session_data, &session_id);
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	length = snprintf(*buff, length, "0x%X", (int)session_id);
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	/* length < 1 is a bug: the session data type may have changed */
9760Sstevel@tonic-gate 	assert(length > 0);
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	return (length);
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate /*
9820Sstevel@tonic-gate  * adt_end_session -- close handle, clear context
9830Sstevel@tonic-gate  *
9840Sstevel@tonic-gate  * if as_check is invalid, no harm, no foul, EXCEPT that this could
9850Sstevel@tonic-gate  * be an attempt to free data already free'd, so output to syslog
9860Sstevel@tonic-gate  * to help explain why the process cored dumped.
9870Sstevel@tonic-gate  */
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate int
adt_end_session(adt_session_data_t * session_data)9900Sstevel@tonic-gate adt_end_session(adt_session_data_t *session_data)
9910Sstevel@tonic-gate {
9920Sstevel@tonic-gate 	adt_internal_state_t	*state;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	if (session_data != NULL) {
9950Sstevel@tonic-gate 		state = (adt_internal_state_t *)session_data;
9962835Sgww 		if (state->as_check != ADT_VALID) {
9970Sstevel@tonic-gate 			adt_write_syslog("freeing invalid data", EINVAL);
9982835Sgww 		} else {
9990Sstevel@tonic-gate 			state->as_check = 0;
10002835Sgww 			m_label_free(state->as_label);
10010Sstevel@tonic-gate 			free(session_data);
10020Sstevel@tonic-gate 		}
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 	/* no errors yet defined */
10050Sstevel@tonic-gate 	return (0);
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate /*
10090Sstevel@tonic-gate  * adt_dup_session -- copy the session data
10100Sstevel@tonic-gate  */
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate int
adt_dup_session(const adt_session_data_t * source,adt_session_data_t ** dest)10130Sstevel@tonic-gate adt_dup_session(const adt_session_data_t *source, adt_session_data_t **dest)
10140Sstevel@tonic-gate {
10150Sstevel@tonic-gate 	adt_internal_state_t	*source_state;
10162835Sgww 	adt_internal_state_t	*dest_state = NULL;
10170Sstevel@tonic-gate 	int			rc = 0;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	if (source != NULL) {
10200Sstevel@tonic-gate 		source_state = (adt_internal_state_t *)source;
10210Sstevel@tonic-gate 		assert(source_state->as_check == ADT_VALID);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 		dest_state = malloc(sizeof (adt_internal_state_t));
10240Sstevel@tonic-gate 		if (dest_state == NULL) {
10250Sstevel@tonic-gate 			rc = -1;
10260Sstevel@tonic-gate 			goto return_rc;
10270Sstevel@tonic-gate 		}
10280Sstevel@tonic-gate 		(void) memcpy(dest_state, source,
10290Sstevel@tonic-gate 		    sizeof (struct adt_internal_state));
10302835Sgww 
10312835Sgww 		if (source_state->as_label != NULL) {
10322835Sgww 			dest_state->as_label = NULL;
10332835Sgww 			if ((rc = m_label_dup(&dest_state->as_label,
10342835Sgww 			    source_state->as_label)) != 0) {
10352835Sgww 				free(dest_state);
10362835Sgww 				dest_state = NULL;
10372835Sgww 			}
10382835Sgww 		}
10390Sstevel@tonic-gate 	}
10400Sstevel@tonic-gate return_rc:
10412835Sgww 	*dest = (adt_session_data_t *)dest_state;
10420Sstevel@tonic-gate 	return (rc);
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate /*
10460Sstevel@tonic-gate  * from_export_format()
10470Sstevel@tonic-gate  * read from a network order buffer into struct adt_session_data
10480Sstevel@tonic-gate  */
10492478Sgww 
10500Sstevel@tonic-gate static size_t
adt_from_export_format(adt_internal_state_t * internal,const adt_export_data_t * external)10510Sstevel@tonic-gate adt_from_export_format(adt_internal_state_t *internal,
10520Sstevel@tonic-gate     const adt_export_data_t *external)
10530Sstevel@tonic-gate {
10540Sstevel@tonic-gate 	struct export_header	head;
10550Sstevel@tonic-gate 	struct export_link	link;
10560Sstevel@tonic-gate 	adr_t			context;
10570Sstevel@tonic-gate 	int32_t 		offset;
10580Sstevel@tonic-gate 	int32_t 		length;
10590Sstevel@tonic-gate 	int32_t 		version;
10602835Sgww 	size_t			label_len;
10610Sstevel@tonic-gate 	char			*p = (char *)external;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	adrm_start(&context, (char *)external);
10640Sstevel@tonic-gate 	adrm_int32(&context, (int *)&head, 4);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	if ((internal->as_check = head.ax_check) != ADT_VALID) {
10670Sstevel@tonic-gate 		errno = EINVAL;
10680Sstevel@tonic-gate 		return (0);
10690Sstevel@tonic-gate 	}
10700Sstevel@tonic-gate 	offset = head.ax_link.ax_offset;
10710Sstevel@tonic-gate 	version = head.ax_link.ax_version;
10720Sstevel@tonic-gate 	length = head.ax_buffer_length;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	/*
10752835Sgww 	 * Skip newer versions.
10760Sstevel@tonic-gate 	 */
10772835Sgww 	while (version > PROTOCOL_VERSION_2) {
10782835Sgww 		if (offset < 1) {
10790Sstevel@tonic-gate 			return (0);	/* failed to match version */
10802835Sgww 		}
10812835Sgww 		p += offset;		/* point to next version # */
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 		if (p > (char *)external + length) {
10840Sstevel@tonic-gate 			return (0);
10850Sstevel@tonic-gate 		}
10860Sstevel@tonic-gate 		adrm_start(&context, p);
10870Sstevel@tonic-gate 		adrm_int32(&context, (int *)&link, 2);
10880Sstevel@tonic-gate 		offset = link.ax_offset;
10890Sstevel@tonic-gate 		version = link.ax_version;
10900Sstevel@tonic-gate 		assert(version != 0);
10910Sstevel@tonic-gate 	}
10922835Sgww 	/*
10932835Sgww 	 * Adjust buffer pointer to the first data item (euid).
10942835Sgww 	 */
10952835Sgww 	if (p == (char *)external) {
10960Sstevel@tonic-gate 		adrm_start(&context, (char *)(p + sizeof (head)));
10972835Sgww 	} else {
10980Sstevel@tonic-gate 		adrm_start(&context, (char *)(p + sizeof (link)));
10992835Sgww 	}
11002835Sgww 	/*
11012835Sgww 	 * if down rev version, neither pid nor label are included
11022835Sgww 	 * in v1 ax_size_of_tsol_data intentionally ignored
11032835Sgww 	 */
11042835Sgww 	if (version == PROTOCOL_VERSION_1) {
11052835Sgww 		adrm_int32(&context, (int *)&(internal->as_euid), 1);
11062835Sgww 		adrm_int32(&context, (int *)&(internal->as_ruid), 1);
11072835Sgww 		adrm_int32(&context, (int *)&(internal->as_egid), 1);
11082835Sgww 		adrm_int32(&context, (int *)&(internal->as_rgid), 1);
11092835Sgww 		adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
11102835Sgww 		adrm_int32(&context,
11112835Sgww 		    (int *)&(internal->as_info.ai_mask.am_success), 2);
11122835Sgww 		adrm_int32(&context,
11132835Sgww 		    (int *)&(internal->as_info.ai_termid.at_port), 1);
11142835Sgww 		adrm_int32(&context,
11152835Sgww 		    (int *)&(internal->as_info.ai_termid.at_type), 1);
11162835Sgww 		adrm_int32(&context,
11172835Sgww 		    (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
11182835Sgww 		adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
111912143Sgww@eng.sun.com 		adrm_int32(&context, (int *)&(internal->as_audit_state), 1);
11202835Sgww 		internal->as_pid = (pid_t)-1;
11212835Sgww 		internal->as_label = NULL;
11222835Sgww 	} else if (version == PROTOCOL_VERSION_2) {
11232835Sgww 		adrm_int32(&context, (int *)&(internal->as_euid), 1);
11242835Sgww 		adrm_int32(&context, (int *)&(internal->as_ruid), 1);
11252835Sgww 		adrm_int32(&context, (int *)&(internal->as_egid), 1);
11262835Sgww 		adrm_int32(&context, (int *)&(internal->as_rgid), 1);
11272835Sgww 		adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
11282835Sgww 		adrm_int32(&context,
11292835Sgww 		    (int *)&(internal->as_info.ai_mask.am_success), 2);
11302835Sgww 		adrm_int32(&context,
11312835Sgww 		    (int *)&(internal->as_info.ai_termid.at_port), 1);
11322835Sgww 		adrm_int32(&context,
11332835Sgww 		    (int *)&(internal->as_info.ai_termid.at_type), 1);
11342835Sgww 		adrm_int32(&context,
11352835Sgww 		    (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
11362835Sgww 		adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
113712143Sgww@eng.sun.com 		adrm_int32(&context, (int *)&(internal->as_audit_state), 1);
11382835Sgww 		adrm_int32(&context, (int *)&(internal->as_pid), 1);
11392835Sgww 		adrm_int32(&context, (int *)&label_len, 1);
11402835Sgww 		if (label_len > 0) {
11412835Sgww 			/* read in and deal with different sized labels. */
11429112STon.Nguyen@Sun.COM 			size32_t my_label_len = blabel_size();
11430Sstevel@tonic-gate 
11442835Sgww 			if ((internal->as_label =
11452835Sgww 			    m_label_alloc(MAC_LABEL)) == NULL) {
11462835Sgww 				return (0);
11472835Sgww 			}
11482835Sgww 			if (label_len > my_label_len) {
11492835Sgww 				errno = EINVAL;
11502835Sgww 				m_label_free(internal->as_label);
11512835Sgww 				return (0);
11522835Sgww 			}
11532835Sgww 			(void) memset(internal->as_label, 0, my_label_len);
11542835Sgww 			adrm_int32(&context, (int *)(internal->as_label),
11552835Sgww 			    label_len / sizeof (int32_t));
11562835Sgww 		} else {
11572835Sgww 			internal->as_label = NULL;
11582835Sgww 		}
11592835Sgww 	}
11600Sstevel@tonic-gate 
11612835Sgww 	return (length);
11620Sstevel@tonic-gate }
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate /*
11652835Sgww  * adt_to_export_format
11660Sstevel@tonic-gate  * read from struct adt_session_data into a network order buffer.
11670Sstevel@tonic-gate  *
11680Sstevel@tonic-gate  * (network order 'cause this data may be shared with a remote host.)
11690Sstevel@tonic-gate  */
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate static size_t
adt_to_export_format(adt_export_data_t * external,adt_internal_state_t * internal)11720Sstevel@tonic-gate adt_to_export_format(adt_export_data_t *external,
11730Sstevel@tonic-gate     adt_internal_state_t *internal)
11740Sstevel@tonic-gate {
11750Sstevel@tonic-gate 	struct export_header	head;
11760Sstevel@tonic-gate 	struct export_link	tail;
11770Sstevel@tonic-gate 	adr_t			context;
11789112STon.Nguyen@Sun.COM 	size32_t		label_len = 0;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	adrm_start(&context, (char *)external);
11810Sstevel@tonic-gate 
11822835Sgww 	if (internal->as_label != NULL) {
11832835Sgww 		label_len = blabel_size();
11842835Sgww 	}
11852835Sgww 
11860Sstevel@tonic-gate 	head.ax_check = ADT_VALID;
11872835Sgww 	head.ax_buffer_length = sizeof (struct adt_export_data) + label_len;
11882835Sgww 
11892835Sgww 	/* version 2 first */
11902835Sgww 
11912835Sgww 	head.ax_link.ax_version = PROTOCOL_VERSION_2;
11922835Sgww 	head.ax_link.ax_offset = sizeof (struct export_header) +
11932835Sgww 	    sizeof (struct adt_export_v2) + label_len;
11942835Sgww 
11950Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&head, 4);
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_euid), 1);
11980Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
11990Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_egid), 1);
12000Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
12010Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
12020Sstevel@tonic-gate 	adrm_putint32(&context,
12030Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_mask.am_success), 2);
12040Sstevel@tonic-gate 	adrm_putint32(&context,
12050Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_port), 1);
12060Sstevel@tonic-gate 	adrm_putint32(&context,
12070Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_type), 1);
12080Sstevel@tonic-gate 	adrm_putint32(&context,
12090Sstevel@tonic-gate 	    (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
12100Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
121112143Sgww@eng.sun.com 	adrm_putint32(&context, (int *)&(internal->as_audit_state), 1);
12122835Sgww 	adrm_putint32(&context, (int *)&(internal->as_pid), 1);
12132835Sgww 	adrm_putint32(&context, (int *)&label_len, 1);
12142835Sgww 	if (internal->as_label != NULL) {
12152835Sgww 		/* serialize the label */
12162835Sgww 		adrm_putint32(&context, (int *)(internal->as_label),
12172835Sgww 		    (label_len / sizeof (int32_t)));
12182835Sgww 	}
12192835Sgww 
12202835Sgww 	/* now version 1 */
12212835Sgww 
12222835Sgww 	tail.ax_version = PROTOCOL_VERSION_1;
12232835Sgww 	tail.ax_offset = 0;
12242835Sgww 
12252835Sgww 	adrm_putint32(&context, (int *)&tail, 2);
12260Sstevel@tonic-gate 
12272835Sgww 	adrm_putint32(&context, (int *)&(internal->as_euid), 1);
12282835Sgww 	adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
12292835Sgww 	adrm_putint32(&context, (int *)&(internal->as_egid), 1);
12302835Sgww 	adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
12312835Sgww 	adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
12322835Sgww 	adrm_putint32(&context,
12332835Sgww 	    (int *)&(internal->as_info.ai_mask.am_success), 2);
12342835Sgww 	adrm_putint32(&context,
12352835Sgww 	    (int *)&(internal->as_info.ai_termid.at_port), 1);
12362835Sgww 	adrm_putint32(&context,
12372835Sgww 	    (int *)&(internal->as_info.ai_termid.at_type), 1);
12382835Sgww 	adrm_putint32(&context,
12392835Sgww 	    (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
12402835Sgww 	adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
124112143Sgww@eng.sun.com 	adrm_putint32(&context, (int *)&(internal->as_audit_state), 1);
12422835Sgww 	/* ignored in v1 */
12432835Sgww 	adrm_putint32(&context, (int *)&label_len, 1);
12442835Sgww 
12452835Sgww 	/* finally terminator */
12462835Sgww 
12470Sstevel@tonic-gate 	tail.ax_version = 0; /* invalid version number */
12480Sstevel@tonic-gate 	tail.ax_offset = 0;
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	adrm_putint32(&context, (int *)&tail, 2);
12510Sstevel@tonic-gate 
12522835Sgww 	return (head.ax_buffer_length);
12530Sstevel@tonic-gate }
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate /*
12562835Sgww  * adt_ucred_label() -- if label is available, duplicate it.
12572835Sgww  */
12582835Sgww 
12592835Sgww static m_label_t *
adt_ucred_label(ucred_t * uc)12602835Sgww adt_ucred_label(ucred_t *uc)
12612835Sgww {
12622835Sgww 	m_label_t	*ul = NULL;
12632835Sgww 
12642835Sgww 	if (ucred_getlabel(uc) != NULL) {
12652835Sgww 		(void) m_label_dup(&ul, ucred_getlabel(uc));
12662835Sgww 	}
12672835Sgww 
12682835Sgww 	return (ul);
12692835Sgww }
12702835Sgww 
12712835Sgww /*
12720Sstevel@tonic-gate  * adt_import() -- convert from network order to machine-specific order
12730Sstevel@tonic-gate  */
12742478Sgww 
12750Sstevel@tonic-gate static int
adt_import(adt_internal_state_t * internal,const adt_export_data_t * external)12760Sstevel@tonic-gate adt_import(adt_internal_state_t *internal, const adt_export_data_t *external)
12770Sstevel@tonic-gate {
12780Sstevel@tonic-gate 	au_mask_t mask;
12790Sstevel@tonic-gate 
128012143Sgww@eng.sun.com 	/* save local audit state */
128112143Sgww@eng.sun.com 	int	local_audit_state = internal->as_audit_state;
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	if (adt_from_export_format(internal, external) < 1)
12840Sstevel@tonic-gate 		return (-1); /* errno from adt_from_export_format */
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	/*
12870Sstevel@tonic-gate 	 * If audit isn't enabled on the remote, they were unable
12880Sstevel@tonic-gate 	 * to generate the audit mask, so generate it based on
12890Sstevel@tonic-gate 	 * local configuration.  If the user id has changed, the
12900Sstevel@tonic-gate 	 * resulting mask may miss some subtleties that occurred
12910Sstevel@tonic-gate 	 * on the remote system.
12920Sstevel@tonic-gate 	 *
12930Sstevel@tonic-gate 	 * If the remote failed to generate a terminal id, it is not
12940Sstevel@tonic-gate 	 * recoverable.
12950Sstevel@tonic-gate 	 */
12960Sstevel@tonic-gate 
129712143Sgww@eng.sun.com 	if (!(internal->as_audit_state & AUC_DISABLED)) {
12980Sstevel@tonic-gate 		if (adt_get_mask_from_user(internal->as_info.ai_auid,
12990Sstevel@tonic-gate 		    &(internal->as_info.ai_mask)))
13000Sstevel@tonic-gate 			return (-1);
13010Sstevel@tonic-gate 		if (internal->as_info.ai_auid != internal->as_ruid) {
13020Sstevel@tonic-gate 			if (adt_get_mask_from_user(internal->as_info.ai_auid,
13030Sstevel@tonic-gate 			    &mask))
13040Sstevel@tonic-gate 				return (-1);
13050Sstevel@tonic-gate 			internal->as_info.ai_mask.am_success |=
13060Sstevel@tonic-gate 			    mask.am_success;
13070Sstevel@tonic-gate 			internal->as_info.ai_mask.am_failure |=
13080Sstevel@tonic-gate 			    mask.am_failure;
13090Sstevel@tonic-gate 		}
13100Sstevel@tonic-gate 	}
131112143Sgww@eng.sun.com 	internal->as_audit_state = local_audit_state;
13120Sstevel@tonic-gate 
131311706SJan.Friedel@Sun.COM 	DPRINTF(("(%lld)imported asid = %X %u\n", (long long) getpid(),
13140Sstevel@tonic-gate 	    internal->as_info.ai_asid,
13150Sstevel@tonic-gate 	    internal->as_info.ai_asid));
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	internal->as_have_user_data = ADT_HAVE_ALL;
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 	return (0);
13200Sstevel@tonic-gate }
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate /*
13230Sstevel@tonic-gate  * adt_export_session_data()
13240Sstevel@tonic-gate  * copies a adt_session_data struct into a network order buffer
13250Sstevel@tonic-gate  *
13260Sstevel@tonic-gate  * In a misconfigured network, the local host may have auditing
13270Sstevel@tonic-gate  * off while the destination may have auditing on, so if there
13280Sstevel@tonic-gate  * is sufficient memory, a buffer will be returned even in the
13290Sstevel@tonic-gate  * audit off case.
13300Sstevel@tonic-gate  */
13312478Sgww 
13320Sstevel@tonic-gate size_t
adt_export_session_data(const adt_session_data_t * internal,adt_export_data_t ** external)13330Sstevel@tonic-gate adt_export_session_data(const adt_session_data_t *internal,
13342478Sgww     adt_export_data_t **external)
13350Sstevel@tonic-gate {
13369112STon.Nguyen@Sun.COM 	size32_t		length = 0;
13370Sstevel@tonic-gate 
13382862Sgww 	if ((internal != NULL) &&
13392862Sgww 	    ((adt_internal_state_t *)internal)->as_label != NULL) {
13402835Sgww 		length = blabel_size();
13412835Sgww 	}
13422835Sgww 
13432835Sgww 	*external = malloc(sizeof (adt_export_data_t) + length);
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	if (*external == NULL)
13462835Sgww 		return (0);
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	if (internal == NULL) {
13492835Sgww 		adt_internal_state_t	*dummy;
13502835Sgww 
13510Sstevel@tonic-gate 		dummy = malloc(sizeof (adt_internal_state_t));
13520Sstevel@tonic-gate 		if (dummy == NULL)
13530Sstevel@tonic-gate 			goto return_length_free;
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 		if (adt_init(dummy, 0)) { /* 0 == don't copy from proc */
13560Sstevel@tonic-gate 			free(dummy);
13570Sstevel@tonic-gate 			goto return_length_free;
13580Sstevel@tonic-gate 		}
13590Sstevel@tonic-gate 		length = adt_to_export_format(*external, dummy);
13600Sstevel@tonic-gate 		free(dummy);
13610Sstevel@tonic-gate 	} else {
13620Sstevel@tonic-gate 		length = adt_to_export_format(*external,
13630Sstevel@tonic-gate 		    (adt_internal_state_t *)internal);
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate 	return (length);
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate return_length_free:
13680Sstevel@tonic-gate 	free(*external);
13690Sstevel@tonic-gate 	*external = NULL;
13702835Sgww 	return (0);
13710Sstevel@tonic-gate }
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate static void
adt_setto_unaudited(adt_internal_state_t * state)13740Sstevel@tonic-gate adt_setto_unaudited(adt_internal_state_t *state)
13750Sstevel@tonic-gate {
137612143Sgww@eng.sun.com 	if (state->as_audit_state & AUC_DISABLED) {
137712143Sgww@eng.sun.com 		state->as_ruid = AU_NOAUDITID;
137812143Sgww@eng.sun.com 		state->as_euid = AU_NOAUDITID;
137912143Sgww@eng.sun.com 		state->as_rgid = AU_NOAUDITID;
138012143Sgww@eng.sun.com 		state->as_egid = AU_NOAUDITID;
138112143Sgww@eng.sun.com 		state->as_pid = (pid_t)-1;
138212143Sgww@eng.sun.com 		state->as_label = NULL;
138312143Sgww@eng.sun.com 	} else {
13840Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
13850Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 		(void) memset((void *)&(state->as_info.ai_termid), 0,
13880Sstevel@tonic-gate 		    sizeof (au_tid_addr_t));
13890Sstevel@tonic-gate 		state->as_info.ai_termid.at_type = AU_IPv4;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 		(void) memset((void *)&(state->as_info.ai_mask), 0,
13920Sstevel@tonic-gate 		    sizeof (au_mask_t));
13930Sstevel@tonic-gate 		state->as_have_user_data = 0;
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate /*
13980Sstevel@tonic-gate  * adt_init -- set session context by copying the audit characteristics
13990Sstevel@tonic-gate  * from the proc and picking up current uid/tid information.
14000Sstevel@tonic-gate  *
14010Sstevel@tonic-gate  * By default, an audit session is based on the process; the default
14020Sstevel@tonic-gate  * is overriden by adt_set_user()
14030Sstevel@tonic-gate  */
14042478Sgww 
14050Sstevel@tonic-gate static int
adt_init(adt_internal_state_t * state,int use_proc_data)14060Sstevel@tonic-gate adt_init(adt_internal_state_t *state, int use_proc_data)
14070Sstevel@tonic-gate {
140812143Sgww@eng.sun.com 	/* ensure auditstate is set */
14090Sstevel@tonic-gate 
141012143Sgww@eng.sun.com 	(void) adt_audit_state(0);
141112143Sgww@eng.sun.com 	state->as_audit_state = auditstate;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	if (use_proc_data) {
14140Sstevel@tonic-gate 		state->as_ruid = getuid();
14150Sstevel@tonic-gate 		state->as_euid = geteuid();
14160Sstevel@tonic-gate 		state->as_rgid = getgid();
14170Sstevel@tonic-gate 		state->as_egid = getegid();
14182835Sgww 		state->as_pid = getpid();
14190Sstevel@tonic-gate 
142012143Sgww@eng.sun.com 		if (!(state->as_audit_state & AUC_DISABLED)) {
14210Sstevel@tonic-gate 			const au_tid64_addr_t	*tid;
14220Sstevel@tonic-gate 			const au_mask_t		*mask;
14232478Sgww 			ucred_t			*ucred = ucred_get(P_MYID);
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 			/*
14260Sstevel@tonic-gate 			 * Even if the ucred is NULL, the underlying
14270Sstevel@tonic-gate 			 * credential may have a valid terminal id; if the
14280Sstevel@tonic-gate 			 * terminal id is set, then that's good enough.  An
14290Sstevel@tonic-gate 			 * example of where this matters is failed login,
14300Sstevel@tonic-gate 			 * where rlogin/telnet sets the terminal id before
14310Sstevel@tonic-gate 			 * calling login; login does not load the credential
14320Sstevel@tonic-gate 			 * since auth failed.
14330Sstevel@tonic-gate 			 */
14340Sstevel@tonic-gate 			if (ucred == NULL) {
14350Sstevel@tonic-gate 				if (!adt_have_termid(
14360Sstevel@tonic-gate 				    &(state->as_info.ai_termid)))
14370Sstevel@tonic-gate 					return (-1);
14380Sstevel@tonic-gate 			} else {
14390Sstevel@tonic-gate 				mask = ucred_getamask(ucred);
14400Sstevel@tonic-gate 				if (mask != NULL) {
14410Sstevel@tonic-gate 					state->as_info.ai_mask = *mask;
14420Sstevel@tonic-gate 				} else {
14430Sstevel@tonic-gate 					ucred_free(ucred);
14440Sstevel@tonic-gate 					return (-1);
14450Sstevel@tonic-gate 				}
14460Sstevel@tonic-gate 				tid = ucred_getatid(ucred);
14470Sstevel@tonic-gate 				if (tid != NULL) {
14480Sstevel@tonic-gate 					adt_cpy_tid(&(state->as_info.ai_termid),
14490Sstevel@tonic-gate 					    tid);
14500Sstevel@tonic-gate 				} else {
14510Sstevel@tonic-gate 					ucred_free(ucred);
14520Sstevel@tonic-gate 					return (-1);
14530Sstevel@tonic-gate 				}
14540Sstevel@tonic-gate 				state->as_info.ai_asid = ucred_getasid(ucred);
14550Sstevel@tonic-gate 				state->as_info.ai_auid = ucred_getauid(ucred);
14562835Sgww 				state->as_label = adt_ucred_label(ucred);
14570Sstevel@tonic-gate 				ucred_free(ucred);
14580Sstevel@tonic-gate 			}
14590Sstevel@tonic-gate 			state->as_have_user_data = ADT_HAVE_ALL;
14600Sstevel@tonic-gate 		}
14610Sstevel@tonic-gate 	} else {
14620Sstevel@tonic-gate 		adt_setto_unaudited(state);
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 	state->as_session_model = ADT_SESSION_MODEL;	/* default */
14650Sstevel@tonic-gate 
146612143Sgww@eng.sun.com 	if ((state->as_audit_state & (AUC_AUDITING | AUC_NOSPACE)) &&
14670Sstevel@tonic-gate 	    auditon(A_GETPOLICY, (caddr_t)&(state->as_kernel_audit_policy),
14680Sstevel@tonic-gate 	    sizeof (state->as_kernel_audit_policy))) {
14690Sstevel@tonic-gate 		return (-1);  /* errno set by auditon */
14700Sstevel@tonic-gate 	}
14710Sstevel@tonic-gate 	state->as_check = ADT_VALID;
14727496Sgww@eng.sun.com 	adt_load_table((adt_session_data_t *)state, &adt_xlate_table[0],
14737496Sgww@eng.sun.com 	    &adt_preload);
14740Sstevel@tonic-gate 	return (0);
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate /*
14780Sstevel@tonic-gate  * adt_set_proc
14790Sstevel@tonic-gate  *
14800Sstevel@tonic-gate  * Copy the current session state to the process.  If this function
14810Sstevel@tonic-gate  * is called, the model becomes a process model rather than a
14820Sstevel@tonic-gate  * session model.
14830Sstevel@tonic-gate  *
14840Sstevel@tonic-gate  * In the current implementation, the value state->as_have_user_data
14851641Spaulson  * must contain all of: ADT_HAVE_{AUID,MASK,TID,ASID}.  These are all set
14861641Spaulson  * by adt_set_user() when the ADT_SETTID or ADT_NEW flag is passed in.
14870Sstevel@tonic-gate  *
14880Sstevel@tonic-gate  */
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate int
adt_set_proc(const adt_session_data_t * session_data)14910Sstevel@tonic-gate adt_set_proc(const adt_session_data_t *session_data)
14920Sstevel@tonic-gate {
14930Sstevel@tonic-gate 	adt_internal_state_t	*state;
14940Sstevel@tonic-gate 
149512143Sgww@eng.sun.com 	if (session_data == NULL) {
14960Sstevel@tonic-gate 		return (0);
149712143Sgww@eng.sun.com 	}
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
15020Sstevel@tonic-gate 
15031641Spaulson 	if ((state->as_have_user_data & (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) !=
15041641Spaulson 	    (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) {
15050Sstevel@tonic-gate 		errno = EINVAL;
15060Sstevel@tonic-gate 		goto return_err;
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 
15097496Sgww@eng.sun.com 	if (setaudit_addr((auditinfo_addr_t *)&(state->as_info),
15107496Sgww@eng.sun.com 	    sizeof (auditinfo_addr_t)) < 0) {
15110Sstevel@tonic-gate 		goto return_err;	/* errno set by setaudit_addr() */
15127496Sgww@eng.sun.com 	}
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	state->as_session_model = ADT_PROCESS_MODEL;
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	return (0);
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate return_err:
15190Sstevel@tonic-gate 	adt_write_syslog("failed to set process audit characteristics", errno);
15200Sstevel@tonic-gate 	return (-1);
15210Sstevel@tonic-gate }
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate static int
adt_newuser(adt_internal_state_t * state,uid_t ruid,au_tid_addr_t * termid)15240Sstevel@tonic-gate adt_newuser(adt_internal_state_t *state, uid_t ruid, au_tid_addr_t *termid)
15250Sstevel@tonic-gate {
15260Sstevel@tonic-gate 	au_tid_addr_t	no_tid = {0, AU_IPv4, 0, 0, 0, 0};
15270Sstevel@tonic-gate 	au_mask_t	no_mask = {0, 0};
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	if (ruid == ADT_NO_AUDIT) {
15300Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
15310Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
15320Sstevel@tonic-gate 		state->as_info.ai_termid = no_tid;
15330Sstevel@tonic-gate 		state->as_info.ai_mask = no_mask;
15340Sstevel@tonic-gate 		return (0);
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 	state->as_info.ai_auid = ruid;
15370Sstevel@tonic-gate 	state->as_info.ai_asid = adt_get_unique_id(ruid);
15380Sstevel@tonic-gate 	if (termid != NULL)
15390Sstevel@tonic-gate 		state->as_info.ai_termid = *termid;
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask)))
15420Sstevel@tonic-gate 		return (-1);
15430Sstevel@tonic-gate 
15443662Sgww 	/* Assume intending to audit as this process */
15453662Sgww 
15463662Sgww 	if (state->as_pid == (pid_t)-1)
15473662Sgww 		state->as_pid = getpid();
15483662Sgww 
15493662Sgww 	if (is_system_labeled() && state->as_label == NULL) {
15503662Sgww 		ucred_t	*ucred = ucred_get(P_MYID);
15513662Sgww 
15523662Sgww 		state->as_label = adt_ucred_label(ucred);
15533662Sgww 		ucred_free(ucred);
15543662Sgww 	}
15553662Sgww 
15560Sstevel@tonic-gate 	return (0);
15570Sstevel@tonic-gate }
15582478Sgww 
15590Sstevel@tonic-gate static int
adt_changeuser(adt_internal_state_t * state,uid_t ruid)15600Sstevel@tonic-gate adt_changeuser(adt_internal_state_t *state, uid_t ruid)
15610Sstevel@tonic-gate {
15620Sstevel@tonic-gate 	au_mask_t		mask;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	if (!(state->as_have_user_data & ADT_HAVE_AUID))
15650Sstevel@tonic-gate 		state->as_info.ai_auid = ruid;
15660Sstevel@tonic-gate 	if (!(state->as_have_user_data & ADT_HAVE_ASID))
15670Sstevel@tonic-gate 		state->as_info.ai_asid = adt_get_unique_id(ruid);
15680Sstevel@tonic-gate 
15694321Scasper 	if (ruid <= MAXEPHUID) {
15700Sstevel@tonic-gate 		if (adt_get_mask_from_user(ruid, &mask))
15710Sstevel@tonic-gate 			return (-1);
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 		state->as_info.ai_mask.am_success |= mask.am_success;
15740Sstevel@tonic-gate 		state->as_info.ai_mask.am_failure |= mask.am_failure;
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 	DPRINTF(("changed mask to %08X/%08X for ruid=%d\n",
15775777Stw21770 	    state->as_info.ai_mask.am_success,
15785777Stw21770 	    state->as_info.ai_mask.am_failure,
15795777Stw21770 	    ruid));
15800Sstevel@tonic-gate 	return (0);
15810Sstevel@tonic-gate }
15822478Sgww 
15830Sstevel@tonic-gate /*
15840Sstevel@tonic-gate  * adt_set_user -- see also adt_set_from_ucred()
15850Sstevel@tonic-gate  *
15860Sstevel@tonic-gate  * ADT_NO_ATTRIB is a valid uid/gid meaning "not known" or
15872478Sgww  * "unattributed."  If ruid, change the model to session.
15880Sstevel@tonic-gate  *
15890Sstevel@tonic-gate  * ADT_NO_CHANGE is a valid uid/gid meaning "do not change this value"
15900Sstevel@tonic-gate  * only valid with ADT_UPDATE.
15910Sstevel@tonic-gate  *
15922478Sgww  * ADT_NO_AUDIT is the external equivalent to AU_NOAUDITID -- there
15930Sstevel@tonic-gate  * isn't a good reason to call adt_set_user() with it unless you don't
15940Sstevel@tonic-gate  * have a good value yet and intend to replace it later; auid will be
15950Sstevel@tonic-gate  * AU_NOAUDITID.
15960Sstevel@tonic-gate  *
15970Sstevel@tonic-gate  * adt_set_user should be called even if auditing is not enabled
15980Sstevel@tonic-gate  * so that adt_export_session_data() will have useful stuff to
15990Sstevel@tonic-gate  * work with.
16000Sstevel@tonic-gate  *
16010Sstevel@tonic-gate  * See the note preceding adt_set_proc() about the use of ADT_HAVE_TID
16020Sstevel@tonic-gate  * and ADT_HAVE_ALL.
16030Sstevel@tonic-gate  */
16042478Sgww 
16050Sstevel@tonic-gate int
adt_set_user(const adt_session_data_t * session_data,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,const adt_termid_t * termid,enum adt_user_context user_context)16062478Sgww adt_set_user(const adt_session_data_t *session_data, uid_t euid, gid_t egid,
16072478Sgww     uid_t ruid, gid_t rgid, const adt_termid_t *termid,
16080Sstevel@tonic-gate     enum adt_user_context user_context)
16090Sstevel@tonic-gate {
16100Sstevel@tonic-gate 	adt_internal_state_t	*state;
16110Sstevel@tonic-gate 	int			rc;
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	if (session_data == NULL) /* no session exists to audit */
16140Sstevel@tonic-gate 		return (0);
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
16170Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	switch (user_context) {
16200Sstevel@tonic-gate 	case ADT_NEW:
16210Sstevel@tonic-gate 		if (ruid == ADT_NO_CHANGE || euid == ADT_NO_CHANGE ||
16220Sstevel@tonic-gate 		    rgid == ADT_NO_CHANGE || egid == ADT_NO_CHANGE) {
16230Sstevel@tonic-gate 			errno = EINVAL;
16240Sstevel@tonic-gate 			return (-1);
16250Sstevel@tonic-gate 		}
16260Sstevel@tonic-gate 		if ((rc = adt_newuser(state, ruid,
16270Sstevel@tonic-gate 		    (au_tid_addr_t *)termid)) != 0)
16280Sstevel@tonic-gate 			return (rc);
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_ALL;
16310Sstevel@tonic-gate 		break;
16320Sstevel@tonic-gate 	case ADT_UPDATE:
16330Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
16340Sstevel@tonic-gate 			errno = EINVAL;
16350Sstevel@tonic-gate 			return (-1);
16360Sstevel@tonic-gate 		}
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 		if (ruid != ADT_NO_CHANGE)
16390Sstevel@tonic-gate 			if ((rc = adt_changeuser(state, ruid)) != 0)
16400Sstevel@tonic-gate 				return (rc);
16410Sstevel@tonic-gate 		break;
16420Sstevel@tonic-gate 	case ADT_USER:
16430Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
16440Sstevel@tonic-gate 			errno = EINVAL;
16450Sstevel@tonic-gate 			return (-1);
16460Sstevel@tonic-gate 		}
16470Sstevel@tonic-gate 		break;
16480Sstevel@tonic-gate 	case ADT_SETTID:
16490Sstevel@tonic-gate 		assert(termid != NULL);
16500Sstevel@tonic-gate 		state->as_info.ai_termid = *((au_tid_addr_t *)termid);
16510Sstevel@tonic-gate 		/* avoid fooling pam_setcred()... */
16520Sstevel@tonic-gate 		state->as_info.ai_auid = AU_NOAUDITID;
16530Sstevel@tonic-gate 		state->as_info.ai_asid = 0;
16540Sstevel@tonic-gate 		state->as_info.ai_mask.am_failure = 0;
16550Sstevel@tonic-gate 		state->as_info.ai_mask.am_success = 0;
16560Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_TID |
16570Sstevel@tonic-gate 		    ADT_HAVE_AUID | ADT_HAVE_ASID | ADT_HAVE_MASK;
16580Sstevel@tonic-gate 		return (0);
16590Sstevel@tonic-gate 	default:
16600Sstevel@tonic-gate 		errno = EINVAL;
16610Sstevel@tonic-gate 		return (-1);
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	if (ruid == ADT_NO_AUDIT) {
16650Sstevel@tonic-gate 		state->as_ruid = AU_NOAUDITID;
16660Sstevel@tonic-gate 		state->as_euid = AU_NOAUDITID;
16670Sstevel@tonic-gate 		state->as_rgid = AU_NOAUDITID;
16680Sstevel@tonic-gate 		state->as_egid = AU_NOAUDITID;
16690Sstevel@tonic-gate 	} else {
16700Sstevel@tonic-gate 		if (ruid != ADT_NO_CHANGE)
16710Sstevel@tonic-gate 			state->as_ruid = ruid;
16720Sstevel@tonic-gate 		if (euid != ADT_NO_CHANGE)
16730Sstevel@tonic-gate 			state->as_euid = euid;
16740Sstevel@tonic-gate 		if (rgid != ADT_NO_CHANGE)
16750Sstevel@tonic-gate 			state->as_rgid = rgid;
16760Sstevel@tonic-gate 		if (egid != ADT_NO_CHANGE)
16770Sstevel@tonic-gate 			state->as_egid = egid;
16780Sstevel@tonic-gate 	}
16790Sstevel@tonic-gate 
16802478Sgww 	if (ruid == ADT_NO_ATTRIB) {
16812478Sgww 		state->as_session_model = ADT_SESSION_MODEL;
16822478Sgww 	}
16832478Sgww 
16840Sstevel@tonic-gate 	return (0);
16850Sstevel@tonic-gate }
16862478Sgww 
16870Sstevel@tonic-gate /*
16880Sstevel@tonic-gate  * adt_set_from_ucred()
16890Sstevel@tonic-gate  *
16900Sstevel@tonic-gate  * an alternate to adt_set_user that fills the same role but uses
16910Sstevel@tonic-gate  * a pointer to a ucred rather than a list of id's.  If the ucred
16920Sstevel@tonic-gate  * pointer is NULL, use the credential from the this process.
16930Sstevel@tonic-gate  *
16940Sstevel@tonic-gate  * A key difference is that for ADT_NEW, adt_set_from_ucred() does
16950Sstevel@tonic-gate  * not overwrite the asid and auid unless auid has not been set.
16960Sstevel@tonic-gate  * ADT_NEW differs from ADT_UPDATE in that it does not OR together
16970Sstevel@tonic-gate  * the incoming audit mask with the one that already exists.
16980Sstevel@tonic-gate  *
16990Sstevel@tonic-gate  * adt_set_from_ucred should be called even if auditing is not enabled
17000Sstevel@tonic-gate  * so that adt_export_session_data() will have useful stuff to
17010Sstevel@tonic-gate  * work with.
17020Sstevel@tonic-gate  */
17032478Sgww 
17040Sstevel@tonic-gate int
adt_set_from_ucred(const adt_session_data_t * session_data,const ucred_t * uc,enum adt_user_context user_context)17052478Sgww adt_set_from_ucred(const adt_session_data_t *session_data, const ucred_t *uc,
17062478Sgww     enum adt_user_context user_context)
17070Sstevel@tonic-gate {
17080Sstevel@tonic-gate 	adt_internal_state_t	*state;
17090Sstevel@tonic-gate 	int			rc = -1;
17100Sstevel@tonic-gate 	const au_tid64_addr_t		*tid64;
17110Sstevel@tonic-gate 	au_tid_addr_t		termid, *tid;
17121405Sme23304 	ucred_t	*ucred = (ucred_t *)uc;
17131405Sme23304 	boolean_t	local_uc = B_FALSE;
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	if (session_data == NULL) /* no session exists to audit */
17160Sstevel@tonic-gate 		return (0);
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	state = (adt_internal_state_t *)session_data;
17190Sstevel@tonic-gate 	assert(state->as_check == ADT_VALID);
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	if (ucred == NULL) {
17223110Sgww 		ucred = ucred_get(P_MYID);
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 		if (ucred == NULL)
17250Sstevel@tonic-gate 			goto return_rc;
17261405Sme23304 		local_uc = B_TRUE;
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	switch (user_context) {
17300Sstevel@tonic-gate 	case ADT_NEW:
17310Sstevel@tonic-gate 		tid64 = ucred_getatid(ucred);
17320Sstevel@tonic-gate 		if (tid64 != NULL) {
17330Sstevel@tonic-gate 			adt_cpy_tid(&termid, tid64);
17340Sstevel@tonic-gate 			tid = &termid;
17350Sstevel@tonic-gate 		} else {
17360Sstevel@tonic-gate 			tid = NULL;
17370Sstevel@tonic-gate 		}
17380Sstevel@tonic-gate 		if (ucred_getauid(ucred) == AU_NOAUDITID) {
17393110Sgww 			adt_setto_unaudited(state);
17403110Sgww 			state->as_have_user_data = ADT_HAVE_ALL;
17413110Sgww 			rc = 0;
17423110Sgww 			goto return_rc;
17430Sstevel@tonic-gate 		} else {
17440Sstevel@tonic-gate 			state->as_info.ai_auid = ucred_getauid(ucred);
17450Sstevel@tonic-gate 			state->as_info.ai_asid = ucred_getasid(ucred);
17460Sstevel@tonic-gate 			state->as_info.ai_mask = *ucred_getamask(ucred);
17470Sstevel@tonic-gate 			state->as_info.ai_termid = *tid;
17480Sstevel@tonic-gate 		}
17490Sstevel@tonic-gate 		state->as_have_user_data = ADT_HAVE_ALL;
17500Sstevel@tonic-gate 		break;
17510Sstevel@tonic-gate 	case ADT_UPDATE:
17520Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
17530Sstevel@tonic-gate 			errno = EINVAL;
17540Sstevel@tonic-gate 			goto return_rc;
17550Sstevel@tonic-gate 		}
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 		if ((rc = adt_changeuser(state, ucred_getruid(ucred))) != 0)
17580Sstevel@tonic-gate 			goto return_rc;
17590Sstevel@tonic-gate 		break;
17600Sstevel@tonic-gate 	case ADT_USER:
17610Sstevel@tonic-gate 		if (state->as_have_user_data != ADT_HAVE_ALL) {
17620Sstevel@tonic-gate 			errno = EINVAL;
17630Sstevel@tonic-gate 			goto return_rc;
17640Sstevel@tonic-gate 		}
17650Sstevel@tonic-gate 		break;
17660Sstevel@tonic-gate 	default:
17670Sstevel@tonic-gate 		errno = EINVAL;
17680Sstevel@tonic-gate 		goto return_rc;
17690Sstevel@tonic-gate 	}
17700Sstevel@tonic-gate 	rc = 0;
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	state->as_ruid = ucred_getruid(ucred);
17730Sstevel@tonic-gate 	state->as_euid = ucred_geteuid(ucred);
17740Sstevel@tonic-gate 	state->as_rgid = ucred_getrgid(ucred);
17750Sstevel@tonic-gate 	state->as_egid = ucred_getegid(ucred);
17762835Sgww 	state->as_pid = ucred_getpid(ucred);
17772835Sgww 	state->as_label = adt_ucred_label(ucred);
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate return_rc:
17801405Sme23304 	if (local_uc) {
17811405Sme23304 		ucred_free(ucred);
17821405Sme23304 	}
17830Sstevel@tonic-gate 	return (rc);
17840Sstevel@tonic-gate }
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate /*
17870Sstevel@tonic-gate  * adt_alloc_event() returns a pointer to allocated memory
17880Sstevel@tonic-gate  *
17890Sstevel@tonic-gate  */
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate adt_event_data_t
adt_alloc_event(const adt_session_data_t * session_data,au_event_t event_id)17920Sstevel@tonic-gate *adt_alloc_event(const adt_session_data_t *session_data, au_event_t event_id)
17930Sstevel@tonic-gate {
17940Sstevel@tonic-gate 	struct adt_event_state	*event_state;
17950Sstevel@tonic-gate 	adt_internal_state_t	*session_state;
17960Sstevel@tonic-gate 	adt_event_data_t	*return_event = NULL;
17970Sstevel@tonic-gate 	/*
17980Sstevel@tonic-gate 	 * need to return a valid event pointer even if audit is
17990Sstevel@tonic-gate 	 * off, else the caller will end up either (1) keeping its
18000Sstevel@tonic-gate 	 * own flags for on/off or (2) writing to a NULL pointer.
18010Sstevel@tonic-gate 	 * If auditing is on, the session data must be valid; otherwise
18020Sstevel@tonic-gate 	 * we don't care.
18030Sstevel@tonic-gate 	 */
18040Sstevel@tonic-gate 	if (session_data != NULL) {
18050Sstevel@tonic-gate 		session_state = (adt_internal_state_t *)session_data;
18060Sstevel@tonic-gate 		assert(session_state->as_check == ADT_VALID);
18070Sstevel@tonic-gate 	}
18080Sstevel@tonic-gate 	event_state = calloc(1, sizeof (struct adt_event_state));
18090Sstevel@tonic-gate 	if (event_state == NULL)
18100Sstevel@tonic-gate 		goto return_ptr;
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 	event_state->ae_check = ADT_VALID;
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	event_state->ae_event_id = event_id;
18150Sstevel@tonic-gate 	event_state->ae_session = (struct adt_internal_state *)session_data;
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	return_event = (adt_event_data_t *)&(event_state->ae_event_data);
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	/*
18200Sstevel@tonic-gate 	 * preload data so the adt_au_*() functions can detect un-supplied
18210Sstevel@tonic-gate 	 * values (0 and NULL are free via calloc()).
18220Sstevel@tonic-gate 	 */
18237503Sgww@eng.sun.com 	if (session_data != NULL) {
18247503Sgww@eng.sun.com 		session_state->as_preload(event_id, return_event);
18257503Sgww@eng.sun.com 	}
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate return_ptr:
18280Sstevel@tonic-gate 	return (return_event);
18290Sstevel@tonic-gate }
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate /*
18320Sstevel@tonic-gate  * adt_getXlateTable -- look up translation table address for event id
18330Sstevel@tonic-gate  */
18340Sstevel@tonic-gate 
18357496Sgww@eng.sun.com static adt_translation_t *
adt_getXlateTable(adt_translation_t ** xlate,au_event_t event_id)18367496Sgww@eng.sun.com adt_getXlateTable(adt_translation_t **xlate, au_event_t event_id)
18370Sstevel@tonic-gate {
18380Sstevel@tonic-gate 	/* xlate_table is global in adt_xlate.c */
18397496Sgww@eng.sun.com 	adt_translation_t	**p_xlate = xlate;
18407496Sgww@eng.sun.com 	adt_translation_t	*p_event;
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	while (*p_xlate != NULL) {
18430Sstevel@tonic-gate 		p_event = *p_xlate;
18440Sstevel@tonic-gate 		if (event_id == p_event->tx_external_event)
18450Sstevel@tonic-gate 			return (p_event);
18460Sstevel@tonic-gate 		p_xlate++;
18470Sstevel@tonic-gate 	}
18480Sstevel@tonic-gate 	return (NULL);
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate /*
18520Sstevel@tonic-gate  * adt_calcOffsets
18530Sstevel@tonic-gate  *
18540Sstevel@tonic-gate  * the call to this function is surrounded by a mutex.
18550Sstevel@tonic-gate  *
18560Sstevel@tonic-gate  * i walks down the table picking up next_token.  j walks again to
18570Sstevel@tonic-gate  * calculate the offset to the input data.  k points to the next
18580Sstevel@tonic-gate  * token's row.  Finally, l, is used to sum the values in the
18590Sstevel@tonic-gate  * datadef array.
18600Sstevel@tonic-gate  *
18610Sstevel@tonic-gate  * What's going on?  The entry array is in the order of the input
18620Sstevel@tonic-gate  * fields but the processing of array entries is in the order of
18630Sstevel@tonic-gate  * the output (see next_token).  Calculating the offset to the
18640Sstevel@tonic-gate  * "next" input can't be done in the outer loop (i) since i doesn't
18650Sstevel@tonic-gate  * point to the current entry and it can't be done with the k index
18660Sstevel@tonic-gate  * because it doesn't represent the order of input fields.
18670Sstevel@tonic-gate  *
18680Sstevel@tonic-gate  * While the resulting algorithm is n**2, it is only done once per
18690Sstevel@tonic-gate  * event type.
18700Sstevel@tonic-gate  */
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate /*
18730Sstevel@tonic-gate  * adt_calcOffsets is only called once per event type, but it uses
18740Sstevel@tonic-gate  * the address alignment of memory allocated for that event as if it
18750Sstevel@tonic-gate  * were the same for all subsequently allocated memory.  This is
18760Sstevel@tonic-gate  * guaranteed by calloc/malloc.  Arrays take special handling since
18770Sstevel@tonic-gate  * what matters for figuring out the correct alignment is the size
18780Sstevel@tonic-gate  * of the array element.
18790Sstevel@tonic-gate  */
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate static void
adt_calcOffsets(struct entry * p_entry,int tablesize,void * p_data)18820Sstevel@tonic-gate adt_calcOffsets(struct entry *p_entry, int tablesize, void *p_data)
18830Sstevel@tonic-gate {
18840Sstevel@tonic-gate 	int		i, j;
18850Sstevel@tonic-gate 	size_t		this_size, prev_size;
18860Sstevel@tonic-gate 	void		*struct_start = p_data;
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	for (i = 0; i < tablesize; i++) {
18890Sstevel@tonic-gate 		if (p_entry[i].en_type_def == NULL) {
18900Sstevel@tonic-gate 			p_entry[i].en_offset = 0;
18910Sstevel@tonic-gate 			continue;
18920Sstevel@tonic-gate 		}
18930Sstevel@tonic-gate 		prev_size = 0;
18940Sstevel@tonic-gate 		p_entry[i].en_offset = (char *)p_data - (char *)struct_start;
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 		for (j = 0; j < p_entry[i].en_count_types; j++) {
18970Sstevel@tonic-gate 			if (p_entry[i].en_type_def[j].dd_datatype == ADT_MSG)
18980Sstevel@tonic-gate 				this_size = sizeof (enum adt_generic);
18990Sstevel@tonic-gate 			else
19000Sstevel@tonic-gate 				this_size =
19010Sstevel@tonic-gate 				    p_entry[i].en_type_def[j].dd_input_size;
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 			/* adj for first entry */
19040Sstevel@tonic-gate 			if (prev_size == 0)
19050Sstevel@tonic-gate 				prev_size = this_size;
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 			if (p_entry[i].en_type_def[j].dd_datatype ==
19080Sstevel@tonic-gate 			    ADT_UINT32ARRAY) {
19090Sstevel@tonic-gate 				p_data = (char *)adt_adjust_address(p_data,
19100Sstevel@tonic-gate 				    prev_size, sizeof (uint32_t)) +
19110Sstevel@tonic-gate 				    this_size - sizeof (uint32_t);
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 				prev_size = sizeof (uint32_t);
19140Sstevel@tonic-gate 			} else {
19150Sstevel@tonic-gate 				p_data = adt_adjust_address(p_data, prev_size,
19160Sstevel@tonic-gate 				    this_size);
19170Sstevel@tonic-gate 				prev_size = this_size;
19180Sstevel@tonic-gate 			}
19190Sstevel@tonic-gate 		}
19200Sstevel@tonic-gate 	}
19210Sstevel@tonic-gate }
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate /*
19240Sstevel@tonic-gate  * adt_generate_event
19250Sstevel@tonic-gate  * generate event record from external struct.  The order is based on
19260Sstevel@tonic-gate  * the output tokens, allowing for the possibility that the input data
19270Sstevel@tonic-gate  * is in a different order.
19280Sstevel@tonic-gate  *
19290Sstevel@tonic-gate  */
19300Sstevel@tonic-gate 
19317496Sgww@eng.sun.com static int
adt_generate_event(const adt_event_data_t * p_extdata,struct adt_event_state * p_event,adt_translation_t * p_xlate)19320Sstevel@tonic-gate adt_generate_event(const adt_event_data_t *p_extdata,
19330Sstevel@tonic-gate     struct adt_event_state *p_event,
19347496Sgww@eng.sun.com     adt_translation_t *p_xlate)
19350Sstevel@tonic-gate {
19360Sstevel@tonic-gate 	struct entry		*p_entry;
19370Sstevel@tonic-gate 	static mutex_t	lock = DEFAULTMUTEX;
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	p_entry = p_xlate->tx_first_entry;
19400Sstevel@tonic-gate 	assert(p_entry != NULL);
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 	p_event->ae_internal_id = p_xlate->tx_internal_event;
19430Sstevel@tonic-gate 	adt_token_open(p_event);
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	/*
19460Sstevel@tonic-gate 	 * offsets are not pre-calculated; the initial offsets are all
19470Sstevel@tonic-gate 	 * 0; valid offsets are >= 0.  Offsets for no-input tokens such
19480Sstevel@tonic-gate 	 * as subject are set to -1 by adt_calcOffset()
19490Sstevel@tonic-gate 	 */
19500Sstevel@tonic-gate 	if (p_xlate->tx_offsetsCalculated == 0) {
19516812Sraf 		(void) mutex_lock(&lock);
19520Sstevel@tonic-gate 		p_xlate->tx_offsetsCalculated = 1;
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 		adt_calcOffsets(p_xlate->tx_top_entry, p_xlate->tx_entries,
19550Sstevel@tonic-gate 		    (void *)p_extdata);
19566812Sraf 		(void) mutex_unlock(&lock);
19570Sstevel@tonic-gate 	}
19580Sstevel@tonic-gate 	while (p_entry != NULL) {
19597496Sgww@eng.sun.com 		adt_generate_token(p_entry, (char *)p_extdata, p_event);
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 		p_entry = p_entry->en_next_token;
19620Sstevel@tonic-gate 	}
19637496Sgww@eng.sun.com 	return (adt_token_close(p_event));
19640Sstevel@tonic-gate }
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate /*
19670Sstevel@tonic-gate  * adt_put_event -- main event generation function.
19680Sstevel@tonic-gate  * The input "event" is the address of the struct containing
19690Sstevel@tonic-gate  * event-specific data.
19700Sstevel@tonic-gate  *
19710Sstevel@tonic-gate  * However if auditing is off or the session handle
19720Sstevel@tonic-gate  * is NULL, no attempt to write a record is made.
19730Sstevel@tonic-gate  */
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate int
adt_put_event(const adt_event_data_t * event,int status,int return_val)19762478Sgww adt_put_event(const adt_event_data_t *event, int status, int return_val)
19770Sstevel@tonic-gate {
19780Sstevel@tonic-gate 	struct adt_event_state	*event_state;
19797496Sgww@eng.sun.com 	adt_translation_t	*xlate;
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 	if (event == NULL) {
19820Sstevel@tonic-gate 		errno = EINVAL;
19837496Sgww@eng.sun.com 		return (-1);
19840Sstevel@tonic-gate 	}
19850Sstevel@tonic-gate 	event_state = (struct adt_event_state *)event;
19860Sstevel@tonic-gate 
198712143Sgww@eng.sun.com 	/* if this is a broken session or not auditing, exit */
198812143Sgww@eng.sun.com 	if ((event_state->ae_session == NULL) ||
198912143Sgww@eng.sun.com 	    !(event_state->ae_session->as_audit_state &
199012143Sgww@eng.sun.com 	    (AUC_AUDITING | AUC_NOSPACE))) {
19917496Sgww@eng.sun.com 		return (0);
19927496Sgww@eng.sun.com 	}
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	assert(event_state->ae_check == ADT_VALID);
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 	event_state->ae_rc = status;
19970Sstevel@tonic-gate 	event_state->ae_type = return_val;
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	/* look up the event */
20000Sstevel@tonic-gate 
20017496Sgww@eng.sun.com 	xlate = adt_getXlateTable(event_state->ae_session->as_xlate,
20027496Sgww@eng.sun.com 	    event_state->ae_event_id);
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	if (xlate == NULL) {
20050Sstevel@tonic-gate 		errno = EINVAL;
20067496Sgww@eng.sun.com 		return (-1);
20070Sstevel@tonic-gate 	}
20080Sstevel@tonic-gate 	DPRINTF(("got event %d\n", xlate->tx_internal_event));
20090Sstevel@tonic-gate 
20107496Sgww@eng.sun.com 	if (adt_selected(event_state, xlate->tx_internal_event, status)) {
20117496Sgww@eng.sun.com 		return (adt_generate_event(event, event_state, xlate));
20127496Sgww@eng.sun.com 	}
20130Sstevel@tonic-gate 
20147496Sgww@eng.sun.com 	return (0);
20150Sstevel@tonic-gate }
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate /*
20180Sstevel@tonic-gate  * adt_free_event -- invalidate and free
20190Sstevel@tonic-gate  */
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate void
adt_free_event(adt_event_data_t * event)20220Sstevel@tonic-gate adt_free_event(adt_event_data_t *event)
20230Sstevel@tonic-gate {
20240Sstevel@tonic-gate 	struct adt_event_state	*event_state;
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	if (event == NULL)
20270Sstevel@tonic-gate 		return;
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	event_state = (struct adt_event_state *)event;
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	assert(event_state->ae_check == ADT_VALID);
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 	event_state->ae_check = 0;
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	free(event_state);
20360Sstevel@tonic-gate }
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate /*
20390Sstevel@tonic-gate  * adt_is_selected -- helper to adt_selected(), below.
20400Sstevel@tonic-gate  *
20410Sstevel@tonic-gate  * "sorf" is "success or fail" status; au_preselect compares
20420Sstevel@tonic-gate  * that with success, fail, or both.
20430Sstevel@tonic-gate  */
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate static int
adt_is_selected(au_event_t e,au_mask_t * m,int sorf)20460Sstevel@tonic-gate adt_is_selected(au_event_t e, au_mask_t *m, int sorf)
20470Sstevel@tonic-gate {
20480Sstevel@tonic-gate 	int prs_sorf;
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 	if (sorf == 0)
20510Sstevel@tonic-gate 		prs_sorf = AU_PRS_SUCCESS;
20520Sstevel@tonic-gate 	else
20530Sstevel@tonic-gate 		prs_sorf = AU_PRS_FAILURE;
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD));
20560Sstevel@tonic-gate }
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate /*
20590Sstevel@tonic-gate  * selected -- see if this event is preselected.
20600Sstevel@tonic-gate  *
20610Sstevel@tonic-gate  * if errors are encountered trying to check a preselection mask
20620Sstevel@tonic-gate  * or look up a user name, the event is selected.  Otherwise, the
20630Sstevel@tonic-gate  * preselection mask is used for the job.
20640Sstevel@tonic-gate  */
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate static int
adt_selected(struct adt_event_state * event,au_event_t actual_id,int status)20670Sstevel@tonic-gate adt_selected(struct adt_event_state *event, au_event_t actual_id, int status)
20680Sstevel@tonic-gate {
20690Sstevel@tonic-gate 	adt_internal_state_t *sp;
20700Sstevel@tonic-gate 	au_mask_t	namask;
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	sp = event->ae_session;
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	if ((sp->as_have_user_data & ADT_HAVE_IDS) == 0) {
20750Sstevel@tonic-gate 		adt_write_syslog("No user data available", EINVAL);
20760Sstevel@tonic-gate 		return (1);	/* default is "selected" */
20770Sstevel@tonic-gate 	}
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 	/* non-attributable? */
20800Sstevel@tonic-gate 	if ((sp->as_info.ai_auid == AU_NOAUDITID) ||
20810Sstevel@tonic-gate 	    (sp->as_info.ai_auid == ADT_NO_AUDIT)) {
20820Sstevel@tonic-gate 		if (auditon(A_GETKMASK, (caddr_t)&namask,
20830Sstevel@tonic-gate 		    sizeof (namask)) != 0) {
20840Sstevel@tonic-gate 			adt_write_syslog("auditon failure", errno);
20850Sstevel@tonic-gate 			return (1);
20860Sstevel@tonic-gate 		}
20870Sstevel@tonic-gate 		return (adt_is_selected(actual_id, &namask, status));
20880Sstevel@tonic-gate 	} else {
20890Sstevel@tonic-gate 		return (adt_is_selected(actual_id, &(sp->as_info.ai_mask),
20900Sstevel@tonic-gate 		    status));
20910Sstevel@tonic-gate 	}
20920Sstevel@tonic-gate }
20937091Sgww 
20947091Sgww /*
20957091Sgww  * Can't map the host name to an IP address in
20967091Sgww  * adt_get_hostIP.  Get something off an interface
20977091Sgww  * to act as the hosts IP address for auditing.
20987091Sgww  */
20997091Sgww 
210012143Sgww@eng.sun.com static int
adt_get_local_address(int family,struct ifaddrlist * al)21017091Sgww adt_get_local_address(int family, struct ifaddrlist *al)
21027091Sgww {
21037091Sgww 	struct ifaddrlist	*ifal;
21047091Sgww 	char	errbuf[ERRBUFSIZE] = "empty list";
21057091Sgww 	char	msg[ERRBUFSIZE + 512];
21067091Sgww 	int	ifal_count;
21077091Sgww 	int	i;
21087091Sgww 
2109*12948Sgary.winiger@oracle.com 	if ((ifal_count = ifaddrlist(&ifal, family, 0, errbuf)) < 0) {
21107091Sgww 		int serrno = errno;
21117091Sgww 
21127091Sgww 		(void) snprintf(msg, sizeof (msg), "adt_get_local_address "
21137091Sgww 		    "couldn't get %d addrlist %s", family, errbuf);
21147091Sgww 		adt_write_syslog(msg, serrno);
21157091Sgww 		errno = serrno;
21167091Sgww 		return (-1);
21177091Sgww 	}
21187091Sgww 
21197091Sgww 	for (i = 0; i < ifal_count; i++) {
21207091Sgww 		/*
21217091Sgww 		 * loopback always defined,
21227091Sgww 		 * even if there is no real address
21237091Sgww 		 */
21247091Sgww 		if ((ifal[i].flags & (IFF_UP | IFF_LOOPBACK)) == IFF_UP) {
21257091Sgww 			break;
21267091Sgww 		}
21277091Sgww 	}
21287091Sgww 	if (i >= ifal_count) {
21297091Sgww 		free(ifal);
21307091Sgww 		/*
21317091Sgww 		 * Callers of adt_get_hostIP() can only return
21327091Sgww 		 * errno to their callers and eventually the application.
21337091Sgww 		 * Picked one that seemed least worse for saying no
21347091Sgww 		 * usable address for Audit terminal ID.
21357091Sgww 		 */
21367091Sgww 		errno = ENETDOWN;
21377091Sgww 		return (-1);
21387091Sgww 	}
21397091Sgww 
21407091Sgww 	*al = ifal[i];
21417091Sgww 	free(ifal);
21427091Sgww 	return (0);
21437091Sgww }
2144