17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
58c3c55e7Spaulson * Common Development and Distribution License (the "License").
68c3c55e7Spaulson * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
2391b2cbb3Sgww * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
277c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h>
287c478bd9Sstevel@tonic-gate #include <assert.h>
297c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
307c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
317c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
327c478bd9Sstevel@tonic-gate #include <door.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <generic.h>
357c478bd9Sstevel@tonic-gate #include <md5.h>
367c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
377c478bd9Sstevel@tonic-gate #include <netdb.h>
387c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
397c478bd9Sstevel@tonic-gate #include <pwd.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <time.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <synch.h>
457c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
467c478bd9Sstevel@tonic-gate #include <syslog.h>
477c478bd9Sstevel@tonic-gate #include <thread.h>
487c478bd9Sstevel@tonic-gate #include <unistd.h>
497c478bd9Sstevel@tonic-gate #include <adt_xlate.h>
507c478bd9Sstevel@tonic-gate #include <adt_ucred.h>
519697ae98Sgww #include <arpa/inet.h>
529697ae98Sgww #include <net/if.h>
539697ae98Sgww #include <libinetutil.h>
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static int adt_selected(struct adt_event_state *, au_event_t, int);
567c478bd9Sstevel@tonic-gate static int adt_init(adt_internal_state_t *, int);
577c478bd9Sstevel@tonic-gate static int adt_import(adt_internal_state_t *, const adt_export_data_t *);
58c529a23fSgww static m_label_t *adt_ucred_label(ucred_t *);
59b3baaabfSgww static void adt_setto_unaudited(adt_internal_state_t *);
609697ae98Sgww static int adt_get_local_address(int, struct ifaddrlist *);
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate #ifdef C2_DEBUG
63dfc7be02SJan Friedel #define DPRINTF(x) { (void) printf x; }
64dfc7be02SJan Friedel #define DFLUSH (void) fflush(stdout);
657c478bd9Sstevel@tonic-gate #else
667c478bd9Sstevel@tonic-gate #define DPRINTF(x)
677c478bd9Sstevel@tonic-gate #define DFLUSH
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate
7091b2cbb3Sgww /*
7191b2cbb3Sgww * Local audit states are a bit mask
7291b2cbb3Sgww *
7391b2cbb3Sgww * The global audit states are
7491b2cbb3Sgww *
7591b2cbb3Sgww * AUC_UNSET 0 - on/off hasn't been decided
7691b2cbb3Sgww * AUC_ENABLED 1 - loaded and enabled
7791b2cbb3Sgww *
7891b2cbb3Sgww * The local Zone states are
7991b2cbb3Sgww *
8091b2cbb3Sgww * AUC_AUDITING 0x1 - audit daemon is active
8191b2cbb3Sgww * AUC_NOAUDIT 0x2 - audit daemon is not active
8291b2cbb3Sgww * AUC_INIT_AUDIT 0x4 - audit is ready but auditd has not run
8391b2cbb3Sgww * AUC_NOSPACE 0x8 - audit enabled, no space for audit records
8491b2cbb3Sgww *
8591b2cbb3Sgww * The only values returned by auditon(A_GETCOND) are:
8691b2cbb3Sgww * AUC_INIT_AUDIT, AUC_AUDITING, AUC_NOAUDIT, AUC_NOSPACE
8791b2cbb3Sgww *
8891b2cbb3Sgww * The pseudo audit state used when the c2audit module is excluded is
8991b2cbb3Sgww *
9091b2cbb3Sgww * AUC_DISABLED 0x100 - c2audit module is excluded
9191b2cbb3Sgww */
9291b2cbb3Sgww
937c478bd9Sstevel@tonic-gate static int auditstate = AUC_DISABLED; /* default state */
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * adt_write_syslog
977c478bd9Sstevel@tonic-gate *
987c478bd9Sstevel@tonic-gate * errors that are not the user's fault (bugs or whatever in
997c478bd9Sstevel@tonic-gate * the underlying audit code are noted in syslog.)
1007c478bd9Sstevel@tonic-gate *
1017c478bd9Sstevel@tonic-gate * Avoid calling adt_write_syslog for things that can happen
1027c478bd9Sstevel@tonic-gate * at high volume.
1037c478bd9Sstevel@tonic-gate *
1047c478bd9Sstevel@tonic-gate * syslog's open (openlog) and close (closelog) are interesting;
1057c478bd9Sstevel@tonic-gate * openlog *may* create a file descriptor and is optional. closelog
1067c478bd9Sstevel@tonic-gate * *will* close any open file descriptors and is also optional.
1077c478bd9Sstevel@tonic-gate *
1087c478bd9Sstevel@tonic-gate * Since syslog may also be used by the calling application, the
1097c478bd9Sstevel@tonic-gate * choice is to avoid openlog, which sets some otherwise useful
1107c478bd9Sstevel@tonic-gate * parameters, and to embed "Solaris_audit" in the log message.
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate void
adt_write_syslog(const char * message,int err)1147c478bd9Sstevel@tonic-gate adt_write_syslog(const char *message, int err)
1157c478bd9Sstevel@tonic-gate {
1169697ae98Sgww int save_errno = errno;
1177c478bd9Sstevel@tonic-gate int mask_priority;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate DPRINTF(("syslog called: %s\n", message));
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate mask_priority = setlogmask(LOG_MASK(LOG_ALERT));
1229697ae98Sgww errno = err;
1239697ae98Sgww syslog(LOG_ALERT, "Solaris_audit %s: %m", message);
1247c478bd9Sstevel@tonic-gate (void) setlogmask(mask_priority);
1257c478bd9Sstevel@tonic-gate errno = save_errno;
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /*
12991b2cbb3Sgww * return true if c2audit is not excluded.
1307c478bd9Sstevel@tonic-gate *
13191b2cbb3Sgww * For purpose of this API, anything but AUC_DISABLED
13291b2cbb3Sgww * is enabled; however one never actually sees
13391b2cbb3Sgww * AUC_DISABLED since auditon returns ENOTSUP in that case. Any
13491b2cbb3Sgww * auditon error is considered the same as ENOTSUP for our
1357c478bd9Sstevel@tonic-gate * purpose. auditstate is not changed by auditon if an error
1367c478bd9Sstevel@tonic-gate * is returned.
1377c478bd9Sstevel@tonic-gate */
1387c478bd9Sstevel@tonic-gate
139b08d8a12Sgww /*
140b08d8a12Sgww * XXX this should probably be eliminated and adt_audit_state() replace it.
141b08d8a12Sgww * All the legitimate uses are to not fork a waiting process for
142b08d8a12Sgww * process exit processing, as in su, login, dtlogin. Other bogus
143b08d8a12Sgww * users are zoneadmd and init.
144b08d8a12Sgww * All but dtlogin are in ON, so we can do this without cross gate
145b08d8a12Sgww * synchronization.
14691b2cbb3Sgww *
14791b2cbb3Sgww * No longer used in adt.c.
148b08d8a12Sgww */
149b08d8a12Sgww
1507c478bd9Sstevel@tonic-gate boolean_t
adt_audit_enabled(void)151b08d8a12Sgww adt_audit_enabled(void)
152b08d8a12Sgww {
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate (void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate));
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate return (auditstate != AUC_DISABLED);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate /*
160b08d8a12Sgww * See adt_audit_enabled() for state discussions.
161b08d8a12Sgww * The state parameter is a hedge until all the uses become clear.
162b08d8a12Sgww * Likely if adt_audit_enabled is brought internal to this file,
163f8994074SJan Friedel * it could be modified to take one or more parameters to describe the
164f8994074SJan Friedel * state.
165b08d8a12Sgww */
166b08d8a12Sgww
167b08d8a12Sgww boolean_t
adt_audit_state(int states)168f8994074SJan Friedel adt_audit_state(int states)
169b08d8a12Sgww {
170b08d8a12Sgww
171b08d8a12Sgww (void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate));
172b08d8a12Sgww
173f8994074SJan Friedel return ((auditstate & states) ? B_TRUE : B_FALSE);
174b08d8a12Sgww }
175b08d8a12Sgww
176b08d8a12Sgww /*
177f8994074SJan Friedel * Get user_specific/non-attributable audit mask. This may be called even when
178f8994074SJan Friedel * auditing is off.
1797c478bd9Sstevel@tonic-gate */
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate static int
adt_get_mask_from_user(uid_t uid,au_mask_t * mask)1827c478bd9Sstevel@tonic-gate adt_get_mask_from_user(uid_t uid, au_mask_t *mask)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate struct passwd pwd;
185f8994074SJan Friedel long buff_sz;
186f8994074SJan Friedel char *pwd_buff;
187f8994074SJan Friedel
1887c478bd9Sstevel@tonic-gate
18991b2cbb3Sgww if (auditstate & AUC_DISABLED) {
19091b2cbb3Sgww /* c2audit excluded */
1917c478bd9Sstevel@tonic-gate mask->am_success = 0;
1927c478bd9Sstevel@tonic-gate mask->am_failure = 0;
193f48205beScasper } else if (uid <= MAXUID) {
194f8994074SJan Friedel if ((buff_sz = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) {
195f8994074SJan Friedel adt_write_syslog("couldn't determine maximum size of "
196f8994074SJan Friedel "password buffer", errno);
1977c478bd9Sstevel@tonic-gate return (-1);
1987c478bd9Sstevel@tonic-gate }
199f8994074SJan Friedel if ((pwd_buff = calloc(1, (size_t)++buff_sz)) == NULL) {
200f8994074SJan Friedel return (-1);
201f8994074SJan Friedel }
202f8994074SJan Friedel if (getpwuid_r(uid, &pwd, pwd_buff, (int)buff_sz) == NULL) {
203f8994074SJan Friedel errno = EINVAL; /* user doesn't exist */
204f8994074SJan Friedel free(pwd_buff);
205f8994074SJan Friedel return (-1);
206f8994074SJan Friedel }
2077c478bd9Sstevel@tonic-gate if (au_user_mask(pwd.pw_name, mask)) {
208*4aed303fSChris Love free(pwd_buff);
2097c478bd9Sstevel@tonic-gate errno = EFAULT; /* undetermined failure */
2107c478bd9Sstevel@tonic-gate return (-1);
2117c478bd9Sstevel@tonic-gate }
212*4aed303fSChris Love free(pwd_buff);
213f8994074SJan Friedel } else if (auditon(A_GETKMASK, (caddr_t)mask, sizeof (*mask)) == -1) {
2147c478bd9Sstevel@tonic-gate return (-1);
2157c478bd9Sstevel@tonic-gate }
216f8994074SJan Friedel
2177c478bd9Sstevel@tonic-gate return (0);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate * adt_get_unique_id -- generate a hopefully unique 32 bit value
2227c478bd9Sstevel@tonic-gate *
2237c478bd9Sstevel@tonic-gate * there will be a follow up to replace this with the use of /dev/random
2247c478bd9Sstevel@tonic-gate *
2257c478bd9Sstevel@tonic-gate * An MD5 hash is taken on a buffer of
2267c478bd9Sstevel@tonic-gate * hostname . audit id . unix time . pid . count
2277c478bd9Sstevel@tonic-gate *
2287c478bd9Sstevel@tonic-gate * "count = noise++;" is subject to a race condition but I don't
2297c478bd9Sstevel@tonic-gate * see a need to put a lock around it.
2307c478bd9Sstevel@tonic-gate */
2317c478bd9Sstevel@tonic-gate
23285e8d33eSgww au_asid_t
adt_get_unique_id(au_id_t uid)2335b7f77adStw21770 adt_get_unique_id(au_id_t uid)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN];
2367c478bd9Sstevel@tonic-gate union {
2377c478bd9Sstevel@tonic-gate au_id_t v[4];
2387c478bd9Sstevel@tonic-gate unsigned char obuff[128/8];
2397c478bd9Sstevel@tonic-gate } output;
2407c478bd9Sstevel@tonic-gate MD5_CTX context;
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate static int noise = 0;
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate int count = noise++;
2457c478bd9Sstevel@tonic-gate time_t timebits = time(NULL);
2467c478bd9Sstevel@tonic-gate pid_t pidbits = getpid();
24785e8d33eSgww au_asid_t retval = 0;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate if (gethostname(hostname, MAXHOSTNAMELEN)) {
2507c478bd9Sstevel@tonic-gate adt_write_syslog("gethostname call failed", errno);
2517c478bd9Sstevel@tonic-gate (void) strncpy(hostname, "invalidHostName", MAXHOSTNAMELEN);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate while (retval == 0) { /* 0 is the only invalid result */
2557c478bd9Sstevel@tonic-gate MD5Init(&context);
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate MD5Update(&context, (unsigned char *)hostname,
2587c478bd9Sstevel@tonic-gate (unsigned int) strlen((const char *)hostname));
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate MD5Update(&context, (unsigned char *) &uid, sizeof (uid_t));
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate MD5Update(&context,
2637c478bd9Sstevel@tonic-gate (unsigned char *) &timebits, sizeof (time_t));
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate MD5Update(&context, (unsigned char *) &pidbits,
2667c478bd9Sstevel@tonic-gate sizeof (pid_t));
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate MD5Update(&context, (unsigned char *) &(count), sizeof (int));
2697c478bd9Sstevel@tonic-gate MD5Final(output.obuff, &context);
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate retval = output.v[count % 4];
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate return (retval);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate * the following "port" function deals with the following issues:
2787c478bd9Sstevel@tonic-gate *
2797c478bd9Sstevel@tonic-gate * 1 the kernel and ucred deal with a dev_t as a 64 bit value made
2807c478bd9Sstevel@tonic-gate * up from a 32 bit major and 32 bit minor.
2817c478bd9Sstevel@tonic-gate * 2 User space deals with a dev_t as either the above 64 bit value
2827c478bd9Sstevel@tonic-gate * or a 32 bit value made from a 14 bit major and an 18 bit minor.
2837c478bd9Sstevel@tonic-gate * 3 The various audit interfaces (except ucred) pass the 32 or
2847c478bd9Sstevel@tonic-gate * 64 bit version depending the architecture of the userspace
2857c478bd9Sstevel@tonic-gate * application. If you get a port value from ucred and pass it
2867c478bd9Sstevel@tonic-gate * to the kernel via auditon(), it must be squeezed into a 32
2877c478bd9Sstevel@tonic-gate * bit value because the kernel knows the userspace app's bit
2887c478bd9Sstevel@tonic-gate * size.
2897c478bd9Sstevel@tonic-gate *
2907c478bd9Sstevel@tonic-gate * The internal state structure for adt (adt_internal_state_t) uses
2917c478bd9Sstevel@tonic-gate * dev_t, so adt converts data from ucred to fit. The import/export
2927c478bd9Sstevel@tonic-gate * functions, however, can't know if they are importing/exporting
2937c478bd9Sstevel@tonic-gate * from 64 or 32 bit applications, so they always send 64 bits and
2947c478bd9Sstevel@tonic-gate * the 32 bit end(s) are responsible to convert 32 -> 64 -> 32 as
2957c478bd9Sstevel@tonic-gate * appropriate.
2967c478bd9Sstevel@tonic-gate */
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate * adt_cpy_tid() -- if lib is 64 bit, just copy it (dev_t and port are
3007c478bd9Sstevel@tonic-gate * both 64 bits). If lib is 32 bits, squeeze the two-int port into
3017c478bd9Sstevel@tonic-gate * a 32 bit dev_t. A port fits in the "minor" part of au_port_t,
3027c478bd9Sstevel@tonic-gate * so it isn't broken up into pieces. (When it goes to the kernel
3037c478bd9Sstevel@tonic-gate * and back, however, it will have been split into major/minor
3047c478bd9Sstevel@tonic-gate * pieces.)
3057c478bd9Sstevel@tonic-gate */
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate static void
adt_cpy_tid(au_tid_addr_t * dest,const au_tid64_addr_t * src)3087c478bd9Sstevel@tonic-gate adt_cpy_tid(au_tid_addr_t *dest, const au_tid64_addr_t *src)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate #ifdef _LP64
3117c478bd9Sstevel@tonic-gate (void) memcpy(dest, src, sizeof (au_tid_addr_t));
3129697ae98Sgww #else /* _LP64 */
3137c478bd9Sstevel@tonic-gate dest->at_type = src->at_type;
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate dest->at_port = src->at_port.at_minor & MAXMIN32;
3167c478bd9Sstevel@tonic-gate dest->at_port |= (src->at_port.at_major & MAXMAJ32) <<
3177c478bd9Sstevel@tonic-gate NBITSMINOR32;
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate (void) memcpy(dest->at_addr, src->at_addr, 4 * sizeof (uint32_t));
3209697ae98Sgww #endif /* _LP64 */
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate * adt_start_session -- create interface handle, create context
3257c478bd9Sstevel@tonic-gate *
3267c478bd9Sstevel@tonic-gate * The imported_state input is normally NULL, if not, it represents
3277c478bd9Sstevel@tonic-gate * a continued session; its values obviate the need for a subsequent
3287c478bd9Sstevel@tonic-gate * call to adt_set_user().
3297c478bd9Sstevel@tonic-gate *
33026fba2a6Sgww * The flag is used to decide how to set the initial state of the session.
33126fba2a6Sgww * If 0, the session is "no audit" until a call to adt_set_user; if
33226fba2a6Sgww * ADT_USE_PROC_DATA, the session is built from the process audit
3337c478bd9Sstevel@tonic-gate * characteristics obtained from the kernel. If imported_state is
3347c478bd9Sstevel@tonic-gate * not NULL, the resulting audit mask is an OR of the current process
3357c478bd9Sstevel@tonic-gate * audit mask and that passed in.
3367c478bd9Sstevel@tonic-gate *
3377c478bd9Sstevel@tonic-gate * The basic model is that the caller can use the pointer returned
3387c478bd9Sstevel@tonic-gate * by adt_start_session whether or not auditing is enabled or an
3397c478bd9Sstevel@tonic-gate * error was returned. The functions that take the session handle
3407c478bd9Sstevel@tonic-gate * as input generally return without doing anything if auditing is
3417c478bd9Sstevel@tonic-gate * disabled.
3427c478bd9Sstevel@tonic-gate */
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate int
adt_start_session(adt_session_data_t ** new_session,const adt_export_data_t * imported_state,adt_session_flags_t flags)3457c478bd9Sstevel@tonic-gate adt_start_session(adt_session_data_t **new_session,
34626fba2a6Sgww const adt_export_data_t *imported_state, adt_session_flags_t flags)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate adt_internal_state_t *state;
3497c478bd9Sstevel@tonic-gate adt_session_flags_t flgmask = ADT_FLAGS_ALL;
3507c478bd9Sstevel@tonic-gate
35191b2cbb3Sgww /* test and set auditstate */
35291b2cbb3Sgww if (adt_audit_state(AUC_DISABLED)) {
35391b2cbb3Sgww /* c2audit excluded */
35491b2cbb3Sgww *new_session = NULL;
35591b2cbb3Sgww return (0);
35691b2cbb3Sgww }
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate if ((flags & ~flgmask) != 0) {
3597c478bd9Sstevel@tonic-gate errno = EINVAL;
3607c478bd9Sstevel@tonic-gate goto return_err;
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate
36391b2cbb3Sgww if ((state = calloc(1, sizeof (adt_internal_state_t))) == NULL) {
3647c478bd9Sstevel@tonic-gate goto return_err;
36591b2cbb3Sgww }
3667c478bd9Sstevel@tonic-gate
36791b2cbb3Sgww if (adt_init(state, flags & ADT_USE_PROC_DATA) != 0) {
3687c478bd9Sstevel@tonic-gate goto return_err_free; /* errno from adt_init() */
36991b2cbb3Sgww }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate * The imported state overwrites the initial state if the
3737c478bd9Sstevel@tonic-gate * imported state represents a valid audit trail
3747c478bd9Sstevel@tonic-gate */
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate if (imported_state != NULL) {
377c529a23fSgww if (adt_import(state, imported_state) != 0) {
3787c478bd9Sstevel@tonic-gate goto return_err_free;
379c529a23fSgww }
38026fba2a6Sgww } else if (flags & ADT_USE_PROC_DATA) {
38126fba2a6Sgww state->as_session_model = ADT_PROCESS_MODEL;
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate state->as_flags = flags;
384dfc7be02SJan Friedel DPRINTF(("(%lld) Starting session id = %08X\n",
385dfc7be02SJan Friedel (long long) getpid(), state->as_info.ai_asid));
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate *new_session = (adt_session_data_t *)state;
3887c478bd9Sstevel@tonic-gate return (0);
38991b2cbb3Sgww
3907c478bd9Sstevel@tonic-gate return_err_free:
3917c478bd9Sstevel@tonic-gate free(state);
3927c478bd9Sstevel@tonic-gate return_err:
39391b2cbb3Sgww *new_session = NULL;
3947c478bd9Sstevel@tonic-gate adt_write_syslog("audit session create failed", errno);
3957c478bd9Sstevel@tonic-gate return (-1);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate /*
3990ad2061eSgww * adt_load_table()
4000ad2061eSgww *
4010ad2061eSgww * loads the event translation table into the audit session.
4020ad2061eSgww */
4030ad2061eSgww
4040ad2061eSgww void
adt_load_table(const adt_session_data_t * session_data,adt_translation_t ** xlate,void (* preload)(au_event_t,adt_event_data_t *))4050ad2061eSgww adt_load_table(const adt_session_data_t *session_data,
4060ad2061eSgww adt_translation_t **xlate, void (*preload)(au_event_t, adt_event_data_t *))
4070ad2061eSgww {
4080ad2061eSgww adt_internal_state_t *state = (adt_internal_state_t *)session_data;
4090ad2061eSgww
4100ad2061eSgww if (state != NULL) {
4110ad2061eSgww assert(state->as_check == ADT_VALID);
4120ad2061eSgww state->as_xlate = xlate;
4130ad2061eSgww state->as_preload = preload;
4140ad2061eSgww }
4150ad2061eSgww }
4160ad2061eSgww
4170ad2061eSgww /*
4187c478bd9Sstevel@tonic-gate * adt_get_asid() and adt_set_asid()
4197c478bd9Sstevel@tonic-gate *
4207c478bd9Sstevel@tonic-gate * if you use this interface, you are responsible to insure that the
4217c478bd9Sstevel@tonic-gate * rest of the session data is populated correctly before calling
4227c478bd9Sstevel@tonic-gate * adt_proccess_attr()
4237c478bd9Sstevel@tonic-gate *
4247c478bd9Sstevel@tonic-gate * neither of these are intended for general use and will likely
4257c478bd9Sstevel@tonic-gate * remain private interfaces for a long time. Forever is a long
4267c478bd9Sstevel@tonic-gate * time. In the case of adt_set_asid(), you should have a very,
4277c478bd9Sstevel@tonic-gate * very good reason for setting your own session id. The process
4287c478bd9Sstevel@tonic-gate * audit characteristics are not changed by put, use adt_set_proc().
4297c478bd9Sstevel@tonic-gate *
4307c478bd9Sstevel@tonic-gate * These are "volatile" (more changable than "evolving") and will
4317c478bd9Sstevel@tonic-gate * probably change in the S10 period.
4327c478bd9Sstevel@tonic-gate */
43326fba2a6Sgww
4347c478bd9Sstevel@tonic-gate void
adt_get_asid(const adt_session_data_t * session_data,au_asid_t * asid)4357c478bd9Sstevel@tonic-gate adt_get_asid(const adt_session_data_t *session_data, au_asid_t *asid)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate if (session_data == NULL) {
4397c478bd9Sstevel@tonic-gate *asid = 0;
4407c478bd9Sstevel@tonic-gate } else {
4417c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check ==
4427c478bd9Sstevel@tonic-gate ADT_VALID);
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate *asid = ((adt_internal_state_t *)session_data)->as_info.ai_asid;
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate void
adt_set_asid(const adt_session_data_t * session_data,const au_asid_t session_id)44926fba2a6Sgww adt_set_asid(const adt_session_data_t *session_data, const au_asid_t session_id)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate if (session_data != NULL) {
4537c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check ==
4547c478bd9Sstevel@tonic-gate ADT_VALID);
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_have_user_data |=
4577c478bd9Sstevel@tonic-gate ADT_HAVE_ASID;
4587c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_info.ai_asid =
4597c478bd9Sstevel@tonic-gate session_id;
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate * adt_get_auid() and adt_set_auid()
4657c478bd9Sstevel@tonic-gate *
4667c478bd9Sstevel@tonic-gate * neither of these are intended for general use and will likely
4677c478bd9Sstevel@tonic-gate * remain private interfaces for a long time. Forever is a long
4687c478bd9Sstevel@tonic-gate * time. In the case of adt_set_auid(), you should have a very,
4697c478bd9Sstevel@tonic-gate * very good reason for setting your own audit id. The process
4707c478bd9Sstevel@tonic-gate * audit characteristics are not changed by put, use adt_set_proc().
4717c478bd9Sstevel@tonic-gate */
47226fba2a6Sgww
4737c478bd9Sstevel@tonic-gate void
adt_get_auid(const adt_session_data_t * session_data,au_id_t * auid)4747c478bd9Sstevel@tonic-gate adt_get_auid(const adt_session_data_t *session_data, au_id_t *auid)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate if (session_data == NULL) {
4787c478bd9Sstevel@tonic-gate *auid = AU_NOAUDITID;
4797c478bd9Sstevel@tonic-gate } else {
4807c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check ==
4817c478bd9Sstevel@tonic-gate ADT_VALID);
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate *auid = ((adt_internal_state_t *)session_data)->as_info.ai_auid;
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate void
adt_set_auid(const adt_session_data_t * session_data,const au_id_t audit_id)4887c478bd9Sstevel@tonic-gate adt_set_auid(const adt_session_data_t *session_data, const au_id_t audit_id)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate if (session_data != NULL) {
4927c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check ==
4937c478bd9Sstevel@tonic-gate ADT_VALID);
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_have_user_data |=
4967c478bd9Sstevel@tonic-gate ADT_HAVE_AUID;
4977c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_info.ai_auid =
4987c478bd9Sstevel@tonic-gate audit_id;
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate * adt_get_termid(), adt_set_termid()
5047c478bd9Sstevel@tonic-gate *
5057c478bd9Sstevel@tonic-gate * if you use this interface, you are responsible to insure that the
5067c478bd9Sstevel@tonic-gate * rest of the session data is populated correctly before calling
5077c478bd9Sstevel@tonic-gate * adt_proccess_attr()
5087c478bd9Sstevel@tonic-gate *
5097c478bd9Sstevel@tonic-gate * The process audit characteristics are not changed by put, use
5107c478bd9Sstevel@tonic-gate * adt_set_proc().
5117c478bd9Sstevel@tonic-gate */
51226fba2a6Sgww
5137c478bd9Sstevel@tonic-gate void
adt_get_termid(const adt_session_data_t * session_data,au_tid_addr_t * termid)5147c478bd9Sstevel@tonic-gate adt_get_termid(const adt_session_data_t *session_data, au_tid_addr_t *termid)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate if (session_data == NULL) {
5187c478bd9Sstevel@tonic-gate (void) memset(termid, 0, sizeof (au_tid_addr_t));
5197c478bd9Sstevel@tonic-gate termid->at_type = AU_IPv4;
5207c478bd9Sstevel@tonic-gate } else {
5217c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check ==
5227c478bd9Sstevel@tonic-gate ADT_VALID);
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate *termid =
5257c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_info.ai_termid;
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate
5297c478bd9Sstevel@tonic-gate void
adt_set_termid(const adt_session_data_t * session_data,const au_tid_addr_t * termid)5307c478bd9Sstevel@tonic-gate adt_set_termid(const adt_session_data_t *session_data,
5317c478bd9Sstevel@tonic-gate const au_tid_addr_t *termid)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate if (session_data != NULL) {
5357c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check ==
5367c478bd9Sstevel@tonic-gate ADT_VALID);
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_info.ai_termid =
5397c478bd9Sstevel@tonic-gate *termid;
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_have_user_data |=
5427c478bd9Sstevel@tonic-gate ADT_HAVE_TID;
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate * adt_get_mask(), adt_set_mask()
5487c478bd9Sstevel@tonic-gate *
5497c478bd9Sstevel@tonic-gate * if you use this interface, you are responsible to insure that the
5507c478bd9Sstevel@tonic-gate * rest of the session data is populated correctly before calling
5517c478bd9Sstevel@tonic-gate * adt_proccess_attr()
5527c478bd9Sstevel@tonic-gate *
5537c478bd9Sstevel@tonic-gate * The process audit characteristics are not changed by put, use
5547c478bd9Sstevel@tonic-gate * adt_set_proc().
5557c478bd9Sstevel@tonic-gate */
55626fba2a6Sgww
5577c478bd9Sstevel@tonic-gate void
adt_get_mask(const adt_session_data_t * session_data,au_mask_t * mask)5587c478bd9Sstevel@tonic-gate adt_get_mask(const adt_session_data_t *session_data, au_mask_t *mask)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate if (session_data == NULL) {
5627c478bd9Sstevel@tonic-gate mask->am_success = 0;
5637c478bd9Sstevel@tonic-gate mask->am_failure = 0;
5647c478bd9Sstevel@tonic-gate } else {
5657c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check ==
5667c478bd9Sstevel@tonic-gate ADT_VALID);
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate *mask = ((adt_internal_state_t *)session_data)->as_info.ai_mask;
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate void
adt_set_mask(const adt_session_data_t * session_data,const au_mask_t * mask)5737c478bd9Sstevel@tonic-gate adt_set_mask(const adt_session_data_t *session_data, const au_mask_t *mask)
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate
5767c478bd9Sstevel@tonic-gate if (session_data != NULL) {
5777c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check ==
5787c478bd9Sstevel@tonic-gate ADT_VALID);
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_info.ai_mask = *mask;
5817c478bd9Sstevel@tonic-gate
5827c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_have_user_data |=
5837c478bd9Sstevel@tonic-gate ADT_HAVE_MASK;
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate * helpers for adt_load_termid
5897c478bd9Sstevel@tonic-gate */
59026fba2a6Sgww
5917c478bd9Sstevel@tonic-gate static void
adt_do_ipv6_address(struct sockaddr_in6 * peer,struct sockaddr_in6 * sock,au_tid_addr_t * termid)5927c478bd9Sstevel@tonic-gate adt_do_ipv6_address(struct sockaddr_in6 *peer, struct sockaddr_in6 *sock,
5937c478bd9Sstevel@tonic-gate au_tid_addr_t *termid)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate
5967c478bd9Sstevel@tonic-gate termid->at_port = ((peer->sin6_port<<16) | (sock->sin6_port));
5977c478bd9Sstevel@tonic-gate termid->at_type = AU_IPv6;
5987c478bd9Sstevel@tonic-gate (void) memcpy(termid->at_addr, &peer->sin6_addr, 4 * sizeof (uint_t));
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate static void
adt_do_ipv4_address(struct sockaddr_in * peer,struct sockaddr_in * sock,au_tid_addr_t * termid)6027c478bd9Sstevel@tonic-gate adt_do_ipv4_address(struct sockaddr_in *peer, struct sockaddr_in *sock,
6037c478bd9Sstevel@tonic-gate au_tid_addr_t *termid)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate
6067c478bd9Sstevel@tonic-gate termid->at_port = ((peer->sin_port<<16) | (sock->sin_port));
6077c478bd9Sstevel@tonic-gate
6087c478bd9Sstevel@tonic-gate termid->at_type = AU_IPv4;
6097c478bd9Sstevel@tonic-gate termid->at_addr[0] = (uint32_t)peer->sin_addr.s_addr;
6107c478bd9Sstevel@tonic-gate (void) memset(&(termid->at_addr[1]), 0, 3 * sizeof (uint_t));
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate * adt_load_termid: convenience function; inputs file handle and
6157c478bd9Sstevel@tonic-gate * outputs an au_tid_addr struct.
6167c478bd9Sstevel@tonic-gate *
6177c478bd9Sstevel@tonic-gate * This code was stolen from audit_settid.c; it differs from audit_settid()
6187c478bd9Sstevel@tonic-gate * in that it does not write the terminal id to the process.
6197c478bd9Sstevel@tonic-gate */
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate int
adt_load_termid(int fd,adt_termid_t ** termid)6227c478bd9Sstevel@tonic-gate adt_load_termid(int fd, adt_termid_t **termid)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate au_tid_addr_t *p_term;
6257c478bd9Sstevel@tonic-gate struct sockaddr_in6 peer;
6267c478bd9Sstevel@tonic-gate struct sockaddr_in6 sock;
6277c478bd9Sstevel@tonic-gate int peerlen = sizeof (peer);
6287c478bd9Sstevel@tonic-gate int socklen = sizeof (sock);
6297c478bd9Sstevel@tonic-gate
6307c478bd9Sstevel@tonic-gate /* get peer name if its a socket, else assume local terminal */
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate if (getpeername(fd, (struct sockaddr *)&peer, (socklen_t *)&peerlen)
6337c478bd9Sstevel@tonic-gate < 0) {
63491b2cbb3Sgww if (errno == ENOTSOCK) {
6357c478bd9Sstevel@tonic-gate return (adt_load_hostname(NULL, termid));
63691b2cbb3Sgww }
6377c478bd9Sstevel@tonic-gate goto return_err;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate
64091b2cbb3Sgww if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
6417c478bd9Sstevel@tonic-gate goto return_err;
64291b2cbb3Sgww }
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate /* get sock name */
6457c478bd9Sstevel@tonic-gate if (getsockname(fd, (struct sockaddr *)&sock,
64691b2cbb3Sgww (socklen_t *)&socklen) < 0) {
6477c478bd9Sstevel@tonic-gate goto return_err_free;
64891b2cbb3Sgww }
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate if (peer.sin6_family == AF_INET6) {
6517c478bd9Sstevel@tonic-gate adt_do_ipv6_address(&peer, &sock, p_term);
6527c478bd9Sstevel@tonic-gate } else {
6537c478bd9Sstevel@tonic-gate adt_do_ipv4_address((struct sockaddr_in *)&peer,
6547c478bd9Sstevel@tonic-gate (struct sockaddr_in *)&sock, p_term);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate *termid = (adt_termid_t *)p_term;
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate return (0);
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate return_err_free:
6617c478bd9Sstevel@tonic-gate free(p_term);
6627c478bd9Sstevel@tonic-gate return_err:
66391b2cbb3Sgww *termid = NULL;
6647c478bd9Sstevel@tonic-gate return (-1);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate static boolean_t
adt_have_termid(au_tid_addr_t * dest)6687c478bd9Sstevel@tonic-gate adt_have_termid(au_tid_addr_t *dest)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate struct auditinfo_addr audit_data;
6717c478bd9Sstevel@tonic-gate
6727c478bd9Sstevel@tonic-gate if (getaudit_addr(&audit_data, sizeof (audit_data)) < 0) {
6737c478bd9Sstevel@tonic-gate adt_write_syslog("getaudit failed", errno);
6747c478bd9Sstevel@tonic-gate return (B_FALSE);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate
6777c478bd9Sstevel@tonic-gate if ((audit_data.ai_termid.at_type == 0) ||
6787c478bd9Sstevel@tonic-gate (audit_data.ai_termid.at_addr[0] |
6797c478bd9Sstevel@tonic-gate audit_data.ai_termid.at_addr[1] |
6807c478bd9Sstevel@tonic-gate audit_data.ai_termid.at_addr[2] |
6817c478bd9Sstevel@tonic-gate audit_data.ai_termid.at_addr[3]) == 0)
6827c478bd9Sstevel@tonic-gate return (B_FALSE);
6837c478bd9Sstevel@tonic-gate
6847c478bd9Sstevel@tonic-gate (void) memcpy(dest, &(audit_data.ai_termid),
6857c478bd9Sstevel@tonic-gate sizeof (au_tid_addr_t));
6867c478bd9Sstevel@tonic-gate
6877c478bd9Sstevel@tonic-gate return (B_TRUE);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate
69091b2cbb3Sgww /*
69191b2cbb3Sgww * adt_get_hostIP - construct a terminal id from a hostname
69291b2cbb3Sgww *
69391b2cbb3Sgww * Returns 0 = success
69491b2cbb3Sgww * -1 = failure and errno = ENETDOWN with the address
69591b2cbb3Sgww * defaulted to IPv4 loopback.
69691b2cbb3Sgww */
69791b2cbb3Sgww
6987c478bd9Sstevel@tonic-gate static int
adt_get_hostIP(const char * hostname,au_tid_addr_t * p_term)6997c478bd9Sstevel@tonic-gate adt_get_hostIP(const char *hostname, au_tid_addr_t *p_term)
7007c478bd9Sstevel@tonic-gate {
7019697ae98Sgww struct addrinfo *ai = NULL;
7029697ae98Sgww int tries = 3;
7039697ae98Sgww char msg[512];
7049697ae98Sgww int eai_err;
7057c478bd9Sstevel@tonic-gate
7069697ae98Sgww while ((tries-- > 0) &&
7079697ae98Sgww ((eai_err = getaddrinfo(hostname, NULL, NULL, &ai)) != 0)) {
7089697ae98Sgww /*
7099697ae98Sgww * getaddrinfo returns its own set of errors.
7109697ae98Sgww * Log them here, so any subsequent syslogs will
7119697ae98Sgww * have a context. adt_get_hostIP callers can only
7129697ae98Sgww * return errno, so subsequent syslogs may be lacking
7139697ae98Sgww * that getaddrinfo failed.
7149697ae98Sgww */
7159697ae98Sgww (void) snprintf(msg, sizeof (msg), "getaddrinfo(%s) "
7169697ae98Sgww "failed[%s]", hostname, gai_strerror(eai_err));
7179697ae98Sgww adt_write_syslog(msg, 0);
7187c478bd9Sstevel@tonic-gate
7199697ae98Sgww if (eai_err != EAI_AGAIN) {
7209697ae98Sgww
7219697ae98Sgww break;
7229697ae98Sgww }
7239697ae98Sgww /* see if resolution becomes available */
7249697ae98Sgww (void) sleep(1);
7259697ae98Sgww }
7269697ae98Sgww if (ai != NULL) {
7279697ae98Sgww if (ai->ai_family == AF_INET) {
7287c478bd9Sstevel@tonic-gate p_term->at_type = AU_IPv4;
7299697ae98Sgww (void) memcpy(p_term->at_addr,
7307c478bd9Sstevel@tonic-gate /* LINTED */
7319697ae98Sgww &((struct sockaddr_in *)ai->ai_addr)->sin_addr,
7329697ae98Sgww AU_IPv4);
7339697ae98Sgww } else {
7347c478bd9Sstevel@tonic-gate p_term->at_type = AU_IPv6;
7359697ae98Sgww (void) memcpy(p_term->at_addr,
7369697ae98Sgww /* LINTED */
7379697ae98Sgww &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
7389697ae98Sgww AU_IPv6);
7399697ae98Sgww }
7409697ae98Sgww freeaddrinfo(ai);
7419697ae98Sgww return (0);
74291b2cbb3Sgww } else if (auditstate & (AUC_AUDITING | AUC_NOSPACE)) {
74391b2cbb3Sgww auditinfo_addr_t audit_info;
74491b2cbb3Sgww
74591b2cbb3Sgww /*
74691b2cbb3Sgww * auditd is running so there should be a
74791b2cbb3Sgww * kernel audit context
74891b2cbb3Sgww */
74991b2cbb3Sgww if (auditon(A_GETKAUDIT, (caddr_t)&audit_info,
75091b2cbb3Sgww sizeof (audit_info)) < 0) {
75191b2cbb3Sgww adt_write_syslog("unable to get kernel audit context",
75291b2cbb3Sgww errno);
75391b2cbb3Sgww goto try_interface;
75491b2cbb3Sgww }
75591b2cbb3Sgww adt_write_syslog("setting Audit IP address to kernel", 0);
75691b2cbb3Sgww *p_term = audit_info.ai_termid;
75791b2cbb3Sgww return (0);
75891b2cbb3Sgww }
75991b2cbb3Sgww try_interface:
76091b2cbb3Sgww {
7619697ae98Sgww struct ifaddrlist al;
7629697ae98Sgww int family;
7639697ae98Sgww char ntop[INET6_ADDRSTRLEN];
7649697ae98Sgww
7659697ae98Sgww /*
7669697ae98Sgww * getaddrinfo has failed to map the hostname
7679697ae98Sgww * to an IP address, try to get an IP address
76891b2cbb3Sgww * from a local interface. If none up, default
76991b2cbb3Sgww * to loopback.
7709697ae98Sgww */
7719697ae98Sgww family = AF_INET6;
7729697ae98Sgww if (adt_get_local_address(family, &al) != 0) {
7739697ae98Sgww family = AF_INET;
7749697ae98Sgww
7759697ae98Sgww if (adt_get_local_address(family, &al) != 0) {
7769697ae98Sgww adt_write_syslog("adt_get_local_address "
77791b2cbb3Sgww "failed, no Audit IP address available, "
77891b2cbb3Sgww "faking loopback and error",
7799697ae98Sgww errno);
78091b2cbb3Sgww IN_SET_LOOPBACK_ADDR(
78191b2cbb3Sgww (struct sockaddr_in *)&(al.addr.addr));
78291b2cbb3Sgww (void) memcpy(p_term->at_addr, &al.addr.addr,
78391b2cbb3Sgww AU_IPv4);
78491b2cbb3Sgww p_term->at_type = AU_IPv4;
7857c478bd9Sstevel@tonic-gate return (-1);
7867c478bd9Sstevel@tonic-gate }
7879697ae98Sgww }
7889697ae98Sgww if (family == AF_INET) {
7899697ae98Sgww p_term->at_type = AU_IPv4;
7909697ae98Sgww (void) memcpy(p_term->at_addr, &al.addr.addr, AU_IPv4);
7919697ae98Sgww } else {
7929697ae98Sgww p_term->at_type = AU_IPv6;
7939697ae98Sgww (void) memcpy(p_term->at_addr, &al.addr.addr6, AU_IPv6);
7949697ae98Sgww }
7957c478bd9Sstevel@tonic-gate
7969697ae98Sgww (void) snprintf(msg, sizeof (msg), "mapping %s to %s",
7979697ae98Sgww hostname, inet_ntop(family, &(al.addr), ntop,
7989697ae98Sgww sizeof (ntop)));
7999697ae98Sgww adt_write_syslog(msg, 0);
8007c478bd9Sstevel@tonic-gate return (0);
8017c478bd9Sstevel@tonic-gate }
8029697ae98Sgww }
8037c478bd9Sstevel@tonic-gate
8047c478bd9Sstevel@tonic-gate /*
8057c478bd9Sstevel@tonic-gate * adt_load_hostname() is called when the caller does not have a file
8067c478bd9Sstevel@tonic-gate * handle that gives access to the socket info or any other way to
8077c478bd9Sstevel@tonic-gate * pass in both port and ip address. The hostname input is ignored if
8087c478bd9Sstevel@tonic-gate * the terminal id has already been set; instead it returns the
8097c478bd9Sstevel@tonic-gate * existing terminal id.
8107c478bd9Sstevel@tonic-gate *
81191b2cbb3Sgww * If c2audit is excluded, success is returned.
81291b2cbb3Sgww * If the hostname lookup fails, the loopback address is assumed,
81391b2cbb3Sgww * errno is set to ENETDOWN, this allows the caller to interpret
81491b2cbb3Sgww * whether failure is fatal, and if not to have a address for the
81591b2cbb3Sgww * hostname.
81691b2cbb3Sgww * Otherwise the caller would need to be aware of the audit state.
81791b2cbb3Sgww *
81891b2cbb3Sgww * Other errors are ignored if not auditing.
8197c478bd9Sstevel@tonic-gate */
82026fba2a6Sgww
8217c478bd9Sstevel@tonic-gate int
adt_load_hostname(const char * hostname,adt_termid_t ** termid)8227c478bd9Sstevel@tonic-gate adt_load_hostname(const char *hostname, adt_termid_t **termid)
8237c478bd9Sstevel@tonic-gate {
8249697ae98Sgww char localhost[MAXHOSTNAMELEN + 1];
8257c478bd9Sstevel@tonic-gate au_tid_addr_t *p_term;
8267c478bd9Sstevel@tonic-gate
82791b2cbb3Sgww if (adt_audit_state(AUC_DISABLED)) {
82891b2cbb3Sgww /* c2audit excluded */
8297c478bd9Sstevel@tonic-gate *termid = NULL;
8307c478bd9Sstevel@tonic-gate return (0);
83191b2cbb3Sgww }
8327c478bd9Sstevel@tonic-gate
83391b2cbb3Sgww if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
8347c478bd9Sstevel@tonic-gate goto return_err;
83591b2cbb3Sgww }
8367c478bd9Sstevel@tonic-gate
8377c478bd9Sstevel@tonic-gate if (adt_have_termid(p_term)) {
8387c478bd9Sstevel@tonic-gate *termid = (adt_termid_t *)p_term;
8397c478bd9Sstevel@tonic-gate return (0);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate p_term->at_port = 0;
8427c478bd9Sstevel@tonic-gate
8437c478bd9Sstevel@tonic-gate if (hostname == NULL || *hostname == '\0') {
8449697ae98Sgww (void) sysinfo(SI_HOSTNAME, localhost, MAXHOSTNAMELEN);
8457c478bd9Sstevel@tonic-gate hostname = localhost;
8467c478bd9Sstevel@tonic-gate }
84791b2cbb3Sgww if (adt_get_hostIP(hostname, p_term) == 0) {
8487c478bd9Sstevel@tonic-gate *termid = (adt_termid_t *)p_term;
8497c478bd9Sstevel@tonic-gate return (0);
85091b2cbb3Sgww } else {
85191b2cbb3Sgww *termid = (adt_termid_t *)p_term;
85291b2cbb3Sgww return (-1);
85391b2cbb3Sgww }
8547c478bd9Sstevel@tonic-gate
8557c478bd9Sstevel@tonic-gate return_err:
85691b2cbb3Sgww *termid = NULL;
85791b2cbb3Sgww if (auditstate & AUC_NOAUDIT) {
8587c478bd9Sstevel@tonic-gate return (0);
85991b2cbb3Sgww }
8607c478bd9Sstevel@tonic-gate
8617c478bd9Sstevel@tonic-gate return (-1);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate
8647c478bd9Sstevel@tonic-gate /*
8657c478bd9Sstevel@tonic-gate * adt_load_ttyname() is called when the caller does not have a file
8667c478bd9Sstevel@tonic-gate * handle that gives access to the local terminal or any other way
8677c478bd9Sstevel@tonic-gate * of determining the device id. The ttyname input is ignored if
8687c478bd9Sstevel@tonic-gate * the terminal id has already been set; instead it returns the
8697c478bd9Sstevel@tonic-gate * existing terminal id.
8707c478bd9Sstevel@tonic-gate *
87191b2cbb3Sgww * If c2audit is excluded, success is returned.
87291b2cbb3Sgww * The local hostname is used for the local IP address.
87391b2cbb3Sgww * If that hostname lookup fails, the loopback address is assumed,
87491b2cbb3Sgww * errno is set to ENETDOWN, this allows the caller to interpret
87591b2cbb3Sgww * whether failure is fatal, and if not to have a address for the
87691b2cbb3Sgww * hostname.
87791b2cbb3Sgww * Otherwise the caller would need to be aware of the audit state.
87891b2cbb3Sgww *
87991b2cbb3Sgww * Other errors are ignored if not auditing.
8807c478bd9Sstevel@tonic-gate */
88126fba2a6Sgww
8827c478bd9Sstevel@tonic-gate int
adt_load_ttyname(const char * ttyname,adt_termid_t ** termid)8837c478bd9Sstevel@tonic-gate adt_load_ttyname(const char *ttyname, adt_termid_t **termid)
8847c478bd9Sstevel@tonic-gate {
8859697ae98Sgww char localhost[MAXHOSTNAMELEN + 1];
8867c478bd9Sstevel@tonic-gate au_tid_addr_t *p_term;
8877c478bd9Sstevel@tonic-gate struct stat stat_buf;
8887c478bd9Sstevel@tonic-gate
88991b2cbb3Sgww if (adt_audit_state(AUC_DISABLED)) {
89091b2cbb3Sgww /* c2audit excluded */
8917c478bd9Sstevel@tonic-gate *termid = NULL;
8927c478bd9Sstevel@tonic-gate return (0);
89391b2cbb3Sgww }
8947c478bd9Sstevel@tonic-gate
89591b2cbb3Sgww if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
8967c478bd9Sstevel@tonic-gate goto return_err;
89791b2cbb3Sgww }
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate if (adt_have_termid(p_term)) {
9007c478bd9Sstevel@tonic-gate *termid = (adt_termid_t *)p_term;
9017c478bd9Sstevel@tonic-gate return (0);
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate
9047c478bd9Sstevel@tonic-gate p_term->at_port = 0;
9057c478bd9Sstevel@tonic-gate
90691b2cbb3Sgww if (sysinfo(SI_HOSTNAME, localhost, MAXHOSTNAMELEN) < 0) {
9077c478bd9Sstevel@tonic-gate goto return_err_free; /* errno from sysinfo */
90891b2cbb3Sgww }
9097c478bd9Sstevel@tonic-gate
9107554000cSJan Friedel if (ttyname != NULL && *ttyname != '\0') {
91191b2cbb3Sgww if (stat(ttyname, &stat_buf) < 0) {
9127c478bd9Sstevel@tonic-gate goto return_err_free;
91391b2cbb3Sgww }
9147c478bd9Sstevel@tonic-gate
9157c478bd9Sstevel@tonic-gate p_term->at_port = stat_buf.st_rdev;
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate
91891b2cbb3Sgww if (adt_get_hostIP(localhost, p_term) == 0) {
9197c478bd9Sstevel@tonic-gate *termid = (adt_termid_t *)p_term;
9207c478bd9Sstevel@tonic-gate return (0);
92191b2cbb3Sgww } else {
92291b2cbb3Sgww *termid = (adt_termid_t *)p_term;
92391b2cbb3Sgww return (-1);
92491b2cbb3Sgww }
9257c478bd9Sstevel@tonic-gate
9267c478bd9Sstevel@tonic-gate return_err_free:
9277c478bd9Sstevel@tonic-gate free(p_term);
9287c478bd9Sstevel@tonic-gate
9297c478bd9Sstevel@tonic-gate return_err:
93091b2cbb3Sgww *termid = NULL;
93191b2cbb3Sgww if (auditstate & AUC_NOAUDIT) {
9327c478bd9Sstevel@tonic-gate return (0);
93391b2cbb3Sgww }
9347c478bd9Sstevel@tonic-gate
9357c478bd9Sstevel@tonic-gate return (-1);
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate
9387c478bd9Sstevel@tonic-gate /*
9397c478bd9Sstevel@tonic-gate * adt_get_session_id returns a stringified representation of
9407c478bd9Sstevel@tonic-gate * the audit session id. See also adt_get_asid() for how to
9417c478bd9Sstevel@tonic-gate * get the unexpurgated version. No guarantees as to how long
9427c478bd9Sstevel@tonic-gate * the returned string will be or its general form; hex for now.
9437c478bd9Sstevel@tonic-gate *
9447c478bd9Sstevel@tonic-gate * An empty string is returned if auditing is off; length = 1
9457c478bd9Sstevel@tonic-gate * and the pointer is valid.
9467c478bd9Sstevel@tonic-gate *
9477c478bd9Sstevel@tonic-gate * returns strlen + 1 if buffer is valid; else 0 and errno.
9487c478bd9Sstevel@tonic-gate */
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate size_t
adt_get_session_id(const adt_session_data_t * session_data,char ** buff)9517c478bd9Sstevel@tonic-gate adt_get_session_id(const adt_session_data_t *session_data, char **buff)
9527c478bd9Sstevel@tonic-gate {
9537c478bd9Sstevel@tonic-gate au_asid_t session_id;
9547c478bd9Sstevel@tonic-gate size_t length;
9557c478bd9Sstevel@tonic-gate /*
9567c478bd9Sstevel@tonic-gate * output is 0x followed by
9577c478bd9Sstevel@tonic-gate * two characters per byte
9587c478bd9Sstevel@tonic-gate * plus terminator,
9597c478bd9Sstevel@tonic-gate * except leading 0's are suppressed, so a few bytes may
9607c478bd9Sstevel@tonic-gate * be unused.
9617c478bd9Sstevel@tonic-gate */
9627c478bd9Sstevel@tonic-gate length = 2 + (2 * sizeof (session_id)) + 1;
9637c478bd9Sstevel@tonic-gate *buff = malloc(length);
9647c478bd9Sstevel@tonic-gate
9657c478bd9Sstevel@tonic-gate if (*buff == NULL) {
966c529a23fSgww return (0);
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate if (session_data == NULL) { /* NULL is not an error */
9697c478bd9Sstevel@tonic-gate **buff = '\0';
970c529a23fSgww return (1);
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate adt_get_asid(session_data, &session_id);
9737c478bd9Sstevel@tonic-gate
9747c478bd9Sstevel@tonic-gate length = snprintf(*buff, length, "0x%X", (int)session_id);
9757c478bd9Sstevel@tonic-gate
9767c478bd9Sstevel@tonic-gate /* length < 1 is a bug: the session data type may have changed */
9777c478bd9Sstevel@tonic-gate assert(length > 0);
9787c478bd9Sstevel@tonic-gate
9797c478bd9Sstevel@tonic-gate return (length);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate
9827c478bd9Sstevel@tonic-gate /*
9837c478bd9Sstevel@tonic-gate * adt_end_session -- close handle, clear context
9847c478bd9Sstevel@tonic-gate *
9857c478bd9Sstevel@tonic-gate * if as_check is invalid, no harm, no foul, EXCEPT that this could
9867c478bd9Sstevel@tonic-gate * be an attempt to free data already free'd, so output to syslog
9877c478bd9Sstevel@tonic-gate * to help explain why the process cored dumped.
9887c478bd9Sstevel@tonic-gate */
9897c478bd9Sstevel@tonic-gate
9907c478bd9Sstevel@tonic-gate int
adt_end_session(adt_session_data_t * session_data)9917c478bd9Sstevel@tonic-gate adt_end_session(adt_session_data_t *session_data)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate adt_internal_state_t *state;
9947c478bd9Sstevel@tonic-gate
9957c478bd9Sstevel@tonic-gate if (session_data != NULL) {
9967c478bd9Sstevel@tonic-gate state = (adt_internal_state_t *)session_data;
997c529a23fSgww if (state->as_check != ADT_VALID) {
9987c478bd9Sstevel@tonic-gate adt_write_syslog("freeing invalid data", EINVAL);
999c529a23fSgww } else {
10007c478bd9Sstevel@tonic-gate state->as_check = 0;
1001c529a23fSgww m_label_free(state->as_label);
10027c478bd9Sstevel@tonic-gate free(session_data);
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate /* no errors yet defined */
10067c478bd9Sstevel@tonic-gate return (0);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate
10097c478bd9Sstevel@tonic-gate /*
10107c478bd9Sstevel@tonic-gate * adt_dup_session -- copy the session data
10117c478bd9Sstevel@tonic-gate */
10127c478bd9Sstevel@tonic-gate
10137c478bd9Sstevel@tonic-gate int
adt_dup_session(const adt_session_data_t * source,adt_session_data_t ** dest)10147c478bd9Sstevel@tonic-gate adt_dup_session(const adt_session_data_t *source, adt_session_data_t **dest)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate adt_internal_state_t *source_state;
1017c529a23fSgww adt_internal_state_t *dest_state = NULL;
10187c478bd9Sstevel@tonic-gate int rc = 0;
10197c478bd9Sstevel@tonic-gate
10207c478bd9Sstevel@tonic-gate if (source != NULL) {
10217c478bd9Sstevel@tonic-gate source_state = (adt_internal_state_t *)source;
10227c478bd9Sstevel@tonic-gate assert(source_state->as_check == ADT_VALID);
10237c478bd9Sstevel@tonic-gate
10247c478bd9Sstevel@tonic-gate dest_state = malloc(sizeof (adt_internal_state_t));
10257c478bd9Sstevel@tonic-gate if (dest_state == NULL) {
10267c478bd9Sstevel@tonic-gate rc = -1;
10277c478bd9Sstevel@tonic-gate goto return_rc;
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate (void) memcpy(dest_state, source,
10307c478bd9Sstevel@tonic-gate sizeof (struct adt_internal_state));
1031c529a23fSgww
1032c529a23fSgww if (source_state->as_label != NULL) {
1033c529a23fSgww dest_state->as_label = NULL;
1034c529a23fSgww if ((rc = m_label_dup(&dest_state->as_label,
1035c529a23fSgww source_state->as_label)) != 0) {
1036c529a23fSgww free(dest_state);
1037c529a23fSgww dest_state = NULL;
1038c529a23fSgww }
1039c529a23fSgww }
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate return_rc:
1042c529a23fSgww *dest = (adt_session_data_t *)dest_state;
10437c478bd9Sstevel@tonic-gate return (rc);
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate * from_export_format()
10487c478bd9Sstevel@tonic-gate * read from a network order buffer into struct adt_session_data
10497c478bd9Sstevel@tonic-gate */
105026fba2a6Sgww
10517c478bd9Sstevel@tonic-gate static size_t
adt_from_export_format(adt_internal_state_t * internal,const adt_export_data_t * external)10527c478bd9Sstevel@tonic-gate adt_from_export_format(adt_internal_state_t *internal,
10537c478bd9Sstevel@tonic-gate const adt_export_data_t *external)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate struct export_header head;
10567c478bd9Sstevel@tonic-gate struct export_link link;
10577c478bd9Sstevel@tonic-gate adr_t context;
10587c478bd9Sstevel@tonic-gate int32_t offset;
10597c478bd9Sstevel@tonic-gate int32_t length;
10607c478bd9Sstevel@tonic-gate int32_t version;
1061c529a23fSgww size_t label_len;
10627c478bd9Sstevel@tonic-gate char *p = (char *)external;
10637c478bd9Sstevel@tonic-gate
10647c478bd9Sstevel@tonic-gate adrm_start(&context, (char *)external);
10657c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&head, 4);
10667c478bd9Sstevel@tonic-gate
10677c478bd9Sstevel@tonic-gate if ((internal->as_check = head.ax_check) != ADT_VALID) {
10687c478bd9Sstevel@tonic-gate errno = EINVAL;
10697c478bd9Sstevel@tonic-gate return (0);
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate offset = head.ax_link.ax_offset;
10727c478bd9Sstevel@tonic-gate version = head.ax_link.ax_version;
10737c478bd9Sstevel@tonic-gate length = head.ax_buffer_length;
10747c478bd9Sstevel@tonic-gate
10757c478bd9Sstevel@tonic-gate /*
1076c529a23fSgww * Skip newer versions.
10777c478bd9Sstevel@tonic-gate */
1078c529a23fSgww while (version > PROTOCOL_VERSION_2) {
1079c529a23fSgww if (offset < 1) {
10807c478bd9Sstevel@tonic-gate return (0); /* failed to match version */
1081c529a23fSgww }
10827c478bd9Sstevel@tonic-gate p += offset; /* point to next version # */
10837c478bd9Sstevel@tonic-gate
10847c478bd9Sstevel@tonic-gate if (p > (char *)external + length) {
10857c478bd9Sstevel@tonic-gate return (0);
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate adrm_start(&context, p);
10887c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&link, 2);
10897c478bd9Sstevel@tonic-gate offset = link.ax_offset;
10907c478bd9Sstevel@tonic-gate version = link.ax_version;
10917c478bd9Sstevel@tonic-gate assert(version != 0);
10927c478bd9Sstevel@tonic-gate }
1093c529a23fSgww /*
1094c529a23fSgww * Adjust buffer pointer to the first data item (euid).
1095c529a23fSgww */
1096c529a23fSgww if (p == (char *)external) {
10977c478bd9Sstevel@tonic-gate adrm_start(&context, (char *)(p + sizeof (head)));
1098c529a23fSgww } else {
10997c478bd9Sstevel@tonic-gate adrm_start(&context, (char *)(p + sizeof (link)));
1100c529a23fSgww }
1101c529a23fSgww /*
1102c529a23fSgww * if down rev version, neither pid nor label are included
1103c529a23fSgww * in v1 ax_size_of_tsol_data intentionally ignored
1104c529a23fSgww */
1105c529a23fSgww if (version == PROTOCOL_VERSION_1) {
11067c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_euid), 1);
11077c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_ruid), 1);
11087c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_egid), 1);
11097c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_rgid), 1);
11107c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
1111c529a23fSgww adrm_int32(&context,
1112c529a23fSgww (int *)&(internal->as_info.ai_mask.am_success), 2);
1113c529a23fSgww adrm_int32(&context,
1114c529a23fSgww (int *)&(internal->as_info.ai_termid.at_port), 1);
1115c529a23fSgww adrm_int32(&context,
1116c529a23fSgww (int *)&(internal->as_info.ai_termid.at_type), 1);
1117c529a23fSgww adrm_int32(&context,
1118c529a23fSgww (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
11197c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
112091b2cbb3Sgww adrm_int32(&context, (int *)&(internal->as_audit_state), 1);
1121c529a23fSgww internal->as_pid = (pid_t)-1;
1122c529a23fSgww internal->as_label = NULL;
1123c529a23fSgww } else if (version == PROTOCOL_VERSION_2) {
1124c529a23fSgww adrm_int32(&context, (int *)&(internal->as_euid), 1);
1125c529a23fSgww adrm_int32(&context, (int *)&(internal->as_ruid), 1);
1126c529a23fSgww adrm_int32(&context, (int *)&(internal->as_egid), 1);
1127c529a23fSgww adrm_int32(&context, (int *)&(internal->as_rgid), 1);
1128c529a23fSgww adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
1129c529a23fSgww adrm_int32(&context,
1130c529a23fSgww (int *)&(internal->as_info.ai_mask.am_success), 2);
1131c529a23fSgww adrm_int32(&context,
1132c529a23fSgww (int *)&(internal->as_info.ai_termid.at_port), 1);
1133c529a23fSgww adrm_int32(&context,
1134c529a23fSgww (int *)&(internal->as_info.ai_termid.at_type), 1);
1135c529a23fSgww adrm_int32(&context,
1136c529a23fSgww (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
1137c529a23fSgww adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
113891b2cbb3Sgww adrm_int32(&context, (int *)&(internal->as_audit_state), 1);
1139c529a23fSgww adrm_int32(&context, (int *)&(internal->as_pid), 1);
1140c529a23fSgww adrm_int32(&context, (int *)&label_len, 1);
1141c529a23fSgww if (label_len > 0) {
1142c529a23fSgww /* read in and deal with different sized labels. */
114342096647STony Nguyen size32_t my_label_len = blabel_size();
11447c478bd9Sstevel@tonic-gate
1145c529a23fSgww if ((internal->as_label =
1146c529a23fSgww m_label_alloc(MAC_LABEL)) == NULL) {
1147c529a23fSgww return (0);
1148c529a23fSgww }
1149c529a23fSgww if (label_len > my_label_len) {
1150c529a23fSgww errno = EINVAL;
1151c529a23fSgww m_label_free(internal->as_label);
1152c529a23fSgww return (0);
1153c529a23fSgww }
1154c529a23fSgww (void) memset(internal->as_label, 0, my_label_len);
1155c529a23fSgww adrm_int32(&context, (int *)(internal->as_label),
1156c529a23fSgww label_len / sizeof (int32_t));
1157c529a23fSgww } else {
1158c529a23fSgww internal->as_label = NULL;
1159c529a23fSgww }
1160c529a23fSgww }
11617c478bd9Sstevel@tonic-gate
1162c529a23fSgww return (length);
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate
11657c478bd9Sstevel@tonic-gate /*
1166c529a23fSgww * adt_to_export_format
11677c478bd9Sstevel@tonic-gate * read from struct adt_session_data into a network order buffer.
11687c478bd9Sstevel@tonic-gate *
11697c478bd9Sstevel@tonic-gate * (network order 'cause this data may be shared with a remote host.)
11707c478bd9Sstevel@tonic-gate */
11717c478bd9Sstevel@tonic-gate
11727c478bd9Sstevel@tonic-gate static size_t
adt_to_export_format(adt_export_data_t * external,adt_internal_state_t * internal)11737c478bd9Sstevel@tonic-gate adt_to_export_format(adt_export_data_t *external,
11747c478bd9Sstevel@tonic-gate adt_internal_state_t *internal)
11757c478bd9Sstevel@tonic-gate {
11767c478bd9Sstevel@tonic-gate struct export_header head;
11777c478bd9Sstevel@tonic-gate struct export_link tail;
11787c478bd9Sstevel@tonic-gate adr_t context;
117942096647STony Nguyen size32_t label_len = 0;
11807c478bd9Sstevel@tonic-gate
11817c478bd9Sstevel@tonic-gate adrm_start(&context, (char *)external);
11827c478bd9Sstevel@tonic-gate
1183c529a23fSgww if (internal->as_label != NULL) {
1184c529a23fSgww label_len = blabel_size();
1185c529a23fSgww }
1186c529a23fSgww
11877c478bd9Sstevel@tonic-gate head.ax_check = ADT_VALID;
1188c529a23fSgww head.ax_buffer_length = sizeof (struct adt_export_data) + label_len;
1189c529a23fSgww
1190c529a23fSgww /* version 2 first */
1191c529a23fSgww
1192c529a23fSgww head.ax_link.ax_version = PROTOCOL_VERSION_2;
1193c529a23fSgww head.ax_link.ax_offset = sizeof (struct export_header) +
1194c529a23fSgww sizeof (struct adt_export_v2) + label_len;
1195c529a23fSgww
11967c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&head, 4);
11977c478bd9Sstevel@tonic-gate
11987c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_euid), 1);
11997c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
12007c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_egid), 1);
12017c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
12027c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
12037c478bd9Sstevel@tonic-gate adrm_putint32(&context,
12047c478bd9Sstevel@tonic-gate (int *)&(internal->as_info.ai_mask.am_success), 2);
12057c478bd9Sstevel@tonic-gate adrm_putint32(&context,
12067c478bd9Sstevel@tonic-gate (int *)&(internal->as_info.ai_termid.at_port), 1);
12077c478bd9Sstevel@tonic-gate adrm_putint32(&context,
12087c478bd9Sstevel@tonic-gate (int *)&(internal->as_info.ai_termid.at_type), 1);
12097c478bd9Sstevel@tonic-gate adrm_putint32(&context,
12107c478bd9Sstevel@tonic-gate (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
12117c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
121291b2cbb3Sgww adrm_putint32(&context, (int *)&(internal->as_audit_state), 1);
1213c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_pid), 1);
1214c529a23fSgww adrm_putint32(&context, (int *)&label_len, 1);
1215c529a23fSgww if (internal->as_label != NULL) {
1216c529a23fSgww /* serialize the label */
1217c529a23fSgww adrm_putint32(&context, (int *)(internal->as_label),
1218c529a23fSgww (label_len / sizeof (int32_t)));
1219c529a23fSgww }
12207c478bd9Sstevel@tonic-gate
1221c529a23fSgww /* now version 1 */
1222c529a23fSgww
1223c529a23fSgww tail.ax_version = PROTOCOL_VERSION_1;
1224c529a23fSgww tail.ax_offset = 0;
1225c529a23fSgww
1226c529a23fSgww adrm_putint32(&context, (int *)&tail, 2);
1227c529a23fSgww
1228c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_euid), 1);
1229c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
1230c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_egid), 1);
1231c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
1232c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
1233c529a23fSgww adrm_putint32(&context,
1234c529a23fSgww (int *)&(internal->as_info.ai_mask.am_success), 2);
1235c529a23fSgww adrm_putint32(&context,
1236c529a23fSgww (int *)&(internal->as_info.ai_termid.at_port), 1);
1237c529a23fSgww adrm_putint32(&context,
1238c529a23fSgww (int *)&(internal->as_info.ai_termid.at_type), 1);
1239c529a23fSgww adrm_putint32(&context,
1240c529a23fSgww (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
1241c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
124291b2cbb3Sgww adrm_putint32(&context, (int *)&(internal->as_audit_state), 1);
1243c529a23fSgww /* ignored in v1 */
1244c529a23fSgww adrm_putint32(&context, (int *)&label_len, 1);
1245c529a23fSgww
1246c529a23fSgww /* finally terminator */
1247c529a23fSgww
12487c478bd9Sstevel@tonic-gate tail.ax_version = 0; /* invalid version number */
12497c478bd9Sstevel@tonic-gate tail.ax_offset = 0;
12507c478bd9Sstevel@tonic-gate
12517c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&tail, 2);
12527c478bd9Sstevel@tonic-gate
1253c529a23fSgww return (head.ax_buffer_length);
12547c478bd9Sstevel@tonic-gate }
12557c478bd9Sstevel@tonic-gate
12567c478bd9Sstevel@tonic-gate /*
1257c529a23fSgww * adt_ucred_label() -- if label is available, duplicate it.
1258c529a23fSgww */
1259c529a23fSgww
1260c529a23fSgww static m_label_t *
adt_ucred_label(ucred_t * uc)1261c529a23fSgww adt_ucred_label(ucred_t *uc)
1262c529a23fSgww {
1263c529a23fSgww m_label_t *ul = NULL;
1264c529a23fSgww
1265c529a23fSgww if (ucred_getlabel(uc) != NULL) {
1266c529a23fSgww (void) m_label_dup(&ul, ucred_getlabel(uc));
1267c529a23fSgww }
1268c529a23fSgww
1269c529a23fSgww return (ul);
1270c529a23fSgww }
1271c529a23fSgww
1272c529a23fSgww /*
12737c478bd9Sstevel@tonic-gate * adt_import() -- convert from network order to machine-specific order
12747c478bd9Sstevel@tonic-gate */
127526fba2a6Sgww
12767c478bd9Sstevel@tonic-gate static int
adt_import(adt_internal_state_t * internal,const adt_export_data_t * external)12777c478bd9Sstevel@tonic-gate adt_import(adt_internal_state_t *internal, const adt_export_data_t *external)
12787c478bd9Sstevel@tonic-gate {
12797c478bd9Sstevel@tonic-gate au_mask_t mask;
12807c478bd9Sstevel@tonic-gate
128191b2cbb3Sgww /* save local audit state */
128291b2cbb3Sgww int local_audit_state = internal->as_audit_state;
12837c478bd9Sstevel@tonic-gate
12847c478bd9Sstevel@tonic-gate if (adt_from_export_format(internal, external) < 1)
12857c478bd9Sstevel@tonic-gate return (-1); /* errno from adt_from_export_format */
12867c478bd9Sstevel@tonic-gate
12877c478bd9Sstevel@tonic-gate /*
12887c478bd9Sstevel@tonic-gate * If audit isn't enabled on the remote, they were unable
12897c478bd9Sstevel@tonic-gate * to generate the audit mask, so generate it based on
12907c478bd9Sstevel@tonic-gate * local configuration. If the user id has changed, the
12917c478bd9Sstevel@tonic-gate * resulting mask may miss some subtleties that occurred
12927c478bd9Sstevel@tonic-gate * on the remote system.
12937c478bd9Sstevel@tonic-gate *
12947c478bd9Sstevel@tonic-gate * If the remote failed to generate a terminal id, it is not
12957c478bd9Sstevel@tonic-gate * recoverable.
12967c478bd9Sstevel@tonic-gate */
12977c478bd9Sstevel@tonic-gate
129891b2cbb3Sgww if (!(internal->as_audit_state & AUC_DISABLED)) {
12997c478bd9Sstevel@tonic-gate if (adt_get_mask_from_user(internal->as_info.ai_auid,
13007c478bd9Sstevel@tonic-gate &(internal->as_info.ai_mask)))
13017c478bd9Sstevel@tonic-gate return (-1);
13027c478bd9Sstevel@tonic-gate if (internal->as_info.ai_auid != internal->as_ruid) {
13037c478bd9Sstevel@tonic-gate if (adt_get_mask_from_user(internal->as_info.ai_auid,
13047c478bd9Sstevel@tonic-gate &mask))
13057c478bd9Sstevel@tonic-gate return (-1);
13067c478bd9Sstevel@tonic-gate internal->as_info.ai_mask.am_success |=
13077c478bd9Sstevel@tonic-gate mask.am_success;
13087c478bd9Sstevel@tonic-gate internal->as_info.ai_mask.am_failure |=
13097c478bd9Sstevel@tonic-gate mask.am_failure;
13107c478bd9Sstevel@tonic-gate }
13117c478bd9Sstevel@tonic-gate }
131291b2cbb3Sgww internal->as_audit_state = local_audit_state;
13137c478bd9Sstevel@tonic-gate
1314dfc7be02SJan Friedel DPRINTF(("(%lld)imported asid = %X %u\n", (long long) getpid(),
13157c478bd9Sstevel@tonic-gate internal->as_info.ai_asid,
13167c478bd9Sstevel@tonic-gate internal->as_info.ai_asid));
13177c478bd9Sstevel@tonic-gate
13187c478bd9Sstevel@tonic-gate internal->as_have_user_data = ADT_HAVE_ALL;
13197c478bd9Sstevel@tonic-gate
13207c478bd9Sstevel@tonic-gate return (0);
13217c478bd9Sstevel@tonic-gate }
13227c478bd9Sstevel@tonic-gate
13237c478bd9Sstevel@tonic-gate /*
13247c478bd9Sstevel@tonic-gate * adt_export_session_data()
13257c478bd9Sstevel@tonic-gate * copies a adt_session_data struct into a network order buffer
13267c478bd9Sstevel@tonic-gate *
13277c478bd9Sstevel@tonic-gate * In a misconfigured network, the local host may have auditing
13287c478bd9Sstevel@tonic-gate * off while the destination may have auditing on, so if there
13297c478bd9Sstevel@tonic-gate * is sufficient memory, a buffer will be returned even in the
13307c478bd9Sstevel@tonic-gate * audit off case.
13317c478bd9Sstevel@tonic-gate */
133226fba2a6Sgww
13337c478bd9Sstevel@tonic-gate size_t
adt_export_session_data(const adt_session_data_t * internal,adt_export_data_t ** external)13347c478bd9Sstevel@tonic-gate adt_export_session_data(const adt_session_data_t *internal,
13357c478bd9Sstevel@tonic-gate adt_export_data_t **external)
13367c478bd9Sstevel@tonic-gate {
133742096647STony Nguyen size32_t length = 0;
13387c478bd9Sstevel@tonic-gate
1339dd6707eeSgww if ((internal != NULL) &&
1340dd6707eeSgww ((adt_internal_state_t *)internal)->as_label != NULL) {
1341c529a23fSgww length = blabel_size();
1342c529a23fSgww }
1343c529a23fSgww
1344c529a23fSgww *external = malloc(sizeof (adt_export_data_t) + length);
13457c478bd9Sstevel@tonic-gate
13467c478bd9Sstevel@tonic-gate if (*external == NULL)
1347c529a23fSgww return (0);
13487c478bd9Sstevel@tonic-gate
13497c478bd9Sstevel@tonic-gate if (internal == NULL) {
1350c529a23fSgww adt_internal_state_t *dummy;
1351c529a23fSgww
13527c478bd9Sstevel@tonic-gate dummy = malloc(sizeof (adt_internal_state_t));
13537c478bd9Sstevel@tonic-gate if (dummy == NULL)
13547c478bd9Sstevel@tonic-gate goto return_length_free;
13557c478bd9Sstevel@tonic-gate
13567c478bd9Sstevel@tonic-gate if (adt_init(dummy, 0)) { /* 0 == don't copy from proc */
13577c478bd9Sstevel@tonic-gate free(dummy);
13587c478bd9Sstevel@tonic-gate goto return_length_free;
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate length = adt_to_export_format(*external, dummy);
13617c478bd9Sstevel@tonic-gate free(dummy);
13627c478bd9Sstevel@tonic-gate } else {
13637c478bd9Sstevel@tonic-gate length = adt_to_export_format(*external,
13647c478bd9Sstevel@tonic-gate (adt_internal_state_t *)internal);
13657c478bd9Sstevel@tonic-gate }
13667c478bd9Sstevel@tonic-gate return (length);
13677c478bd9Sstevel@tonic-gate
13687c478bd9Sstevel@tonic-gate return_length_free:
13697c478bd9Sstevel@tonic-gate free(*external);
13707c478bd9Sstevel@tonic-gate *external = NULL;
1371c529a23fSgww return (0);
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate
13747c478bd9Sstevel@tonic-gate static void
adt_setto_unaudited(adt_internal_state_t * state)13757c478bd9Sstevel@tonic-gate adt_setto_unaudited(adt_internal_state_t *state)
13767c478bd9Sstevel@tonic-gate {
137791b2cbb3Sgww if (state->as_audit_state & AUC_DISABLED) {
13787c478bd9Sstevel@tonic-gate state->as_ruid = AU_NOAUDITID;
13797c478bd9Sstevel@tonic-gate state->as_euid = AU_NOAUDITID;
13807c478bd9Sstevel@tonic-gate state->as_rgid = AU_NOAUDITID;
13817c478bd9Sstevel@tonic-gate state->as_egid = AU_NOAUDITID;
1382c529a23fSgww state->as_pid = (pid_t)-1;
1383c529a23fSgww state->as_label = NULL;
138491b2cbb3Sgww } else {
13857c478bd9Sstevel@tonic-gate state->as_info.ai_asid = 0;
13867c478bd9Sstevel@tonic-gate state->as_info.ai_auid = AU_NOAUDITID;
13877c478bd9Sstevel@tonic-gate
13887c478bd9Sstevel@tonic-gate (void) memset((void *)&(state->as_info.ai_termid), 0,
13897c478bd9Sstevel@tonic-gate sizeof (au_tid_addr_t));
13907c478bd9Sstevel@tonic-gate state->as_info.ai_termid.at_type = AU_IPv4;
13917c478bd9Sstevel@tonic-gate
13927c478bd9Sstevel@tonic-gate (void) memset((void *)&(state->as_info.ai_mask), 0,
13937c478bd9Sstevel@tonic-gate sizeof (au_mask_t));
13947c478bd9Sstevel@tonic-gate state->as_have_user_data = 0;
13957c478bd9Sstevel@tonic-gate }
13967c478bd9Sstevel@tonic-gate }
13977c478bd9Sstevel@tonic-gate
13987c478bd9Sstevel@tonic-gate /*
13997c478bd9Sstevel@tonic-gate * adt_init -- set session context by copying the audit characteristics
14007c478bd9Sstevel@tonic-gate * from the proc and picking up current uid/tid information.
14017c478bd9Sstevel@tonic-gate *
14027c478bd9Sstevel@tonic-gate * By default, an audit session is based on the process; the default
14037c478bd9Sstevel@tonic-gate * is overriden by adt_set_user()
14047c478bd9Sstevel@tonic-gate */
140526fba2a6Sgww
14067c478bd9Sstevel@tonic-gate static int
adt_init(adt_internal_state_t * state,int use_proc_data)14077c478bd9Sstevel@tonic-gate adt_init(adt_internal_state_t *state, int use_proc_data)
14087c478bd9Sstevel@tonic-gate {
140991b2cbb3Sgww /* ensure auditstate is set */
14107c478bd9Sstevel@tonic-gate
141191b2cbb3Sgww (void) adt_audit_state(0);
141291b2cbb3Sgww state->as_audit_state = auditstate;
14137c478bd9Sstevel@tonic-gate
14147c478bd9Sstevel@tonic-gate if (use_proc_data) {
14157c478bd9Sstevel@tonic-gate state->as_ruid = getuid();
14167c478bd9Sstevel@tonic-gate state->as_euid = geteuid();
14177c478bd9Sstevel@tonic-gate state->as_rgid = getgid();
14187c478bd9Sstevel@tonic-gate state->as_egid = getegid();
1419c529a23fSgww state->as_pid = getpid();
14207c478bd9Sstevel@tonic-gate
142191b2cbb3Sgww if (!(state->as_audit_state & AUC_DISABLED)) {
14227c478bd9Sstevel@tonic-gate const au_tid64_addr_t *tid;
14237c478bd9Sstevel@tonic-gate const au_mask_t *mask;
142426fba2a6Sgww ucred_t *ucred = ucred_get(P_MYID);
14257c478bd9Sstevel@tonic-gate
14267c478bd9Sstevel@tonic-gate /*
14277c478bd9Sstevel@tonic-gate * Even if the ucred is NULL, the underlying
14287c478bd9Sstevel@tonic-gate * credential may have a valid terminal id; if the
14297c478bd9Sstevel@tonic-gate * terminal id is set, then that's good enough. An
14307c478bd9Sstevel@tonic-gate * example of where this matters is failed login,
14317c478bd9Sstevel@tonic-gate * where rlogin/telnet sets the terminal id before
14327c478bd9Sstevel@tonic-gate * calling login; login does not load the credential
14337c478bd9Sstevel@tonic-gate * since auth failed.
14347c478bd9Sstevel@tonic-gate */
14357c478bd9Sstevel@tonic-gate if (ucred == NULL) {
14367c478bd9Sstevel@tonic-gate if (!adt_have_termid(
14377c478bd9Sstevel@tonic-gate &(state->as_info.ai_termid)))
14387c478bd9Sstevel@tonic-gate return (-1);
14397c478bd9Sstevel@tonic-gate } else {
14407c478bd9Sstevel@tonic-gate mask = ucred_getamask(ucred);
14417c478bd9Sstevel@tonic-gate if (mask != NULL) {
14427c478bd9Sstevel@tonic-gate state->as_info.ai_mask = *mask;
14437c478bd9Sstevel@tonic-gate } else {
14447c478bd9Sstevel@tonic-gate ucred_free(ucred);
14457c478bd9Sstevel@tonic-gate return (-1);
14467c478bd9Sstevel@tonic-gate }
14477c478bd9Sstevel@tonic-gate tid = ucred_getatid(ucred);
14487c478bd9Sstevel@tonic-gate if (tid != NULL) {
14497c478bd9Sstevel@tonic-gate adt_cpy_tid(&(state->as_info.ai_termid),
14507c478bd9Sstevel@tonic-gate tid);
14517c478bd9Sstevel@tonic-gate } else {
14527c478bd9Sstevel@tonic-gate ucred_free(ucred);
14537c478bd9Sstevel@tonic-gate return (-1);
14547c478bd9Sstevel@tonic-gate }
14557c478bd9Sstevel@tonic-gate state->as_info.ai_asid = ucred_getasid(ucred);
14567c478bd9Sstevel@tonic-gate state->as_info.ai_auid = ucred_getauid(ucred);
1457c529a23fSgww state->as_label = adt_ucred_label(ucred);
14587c478bd9Sstevel@tonic-gate ucred_free(ucred);
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate state->as_have_user_data = ADT_HAVE_ALL;
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate } else {
14637c478bd9Sstevel@tonic-gate adt_setto_unaudited(state);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate state->as_session_model = ADT_SESSION_MODEL; /* default */
14667c478bd9Sstevel@tonic-gate
146791b2cbb3Sgww if ((state->as_audit_state & (AUC_AUDITING | AUC_NOSPACE)) &&
14687c478bd9Sstevel@tonic-gate auditon(A_GETPOLICY, (caddr_t)&(state->as_kernel_audit_policy),
14697c478bd9Sstevel@tonic-gate sizeof (state->as_kernel_audit_policy))) {
14707c478bd9Sstevel@tonic-gate return (-1); /* errno set by auditon */
14717c478bd9Sstevel@tonic-gate }
14727c478bd9Sstevel@tonic-gate state->as_check = ADT_VALID;
14730ad2061eSgww adt_load_table((adt_session_data_t *)state, &adt_xlate_table[0],
14740ad2061eSgww &adt_preload);
14757c478bd9Sstevel@tonic-gate return (0);
14767c478bd9Sstevel@tonic-gate }
14777c478bd9Sstevel@tonic-gate
14787c478bd9Sstevel@tonic-gate /*
14797c478bd9Sstevel@tonic-gate * adt_set_proc
14807c478bd9Sstevel@tonic-gate *
14817c478bd9Sstevel@tonic-gate * Copy the current session state to the process. If this function
14827c478bd9Sstevel@tonic-gate * is called, the model becomes a process model rather than a
14837c478bd9Sstevel@tonic-gate * session model.
14847c478bd9Sstevel@tonic-gate *
14857c478bd9Sstevel@tonic-gate * In the current implementation, the value state->as_have_user_data
14868c3c55e7Spaulson * must contain all of: ADT_HAVE_{AUID,MASK,TID,ASID}. These are all set
14878c3c55e7Spaulson * by adt_set_user() when the ADT_SETTID or ADT_NEW flag is passed in.
14887c478bd9Sstevel@tonic-gate *
14897c478bd9Sstevel@tonic-gate */
14907c478bd9Sstevel@tonic-gate
14917c478bd9Sstevel@tonic-gate int
adt_set_proc(const adt_session_data_t * session_data)14927c478bd9Sstevel@tonic-gate adt_set_proc(const adt_session_data_t *session_data)
14937c478bd9Sstevel@tonic-gate {
14947c478bd9Sstevel@tonic-gate adt_internal_state_t *state;
14957c478bd9Sstevel@tonic-gate
149691b2cbb3Sgww if (session_data == NULL) {
14977c478bd9Sstevel@tonic-gate return (0);
149891b2cbb3Sgww }
14997c478bd9Sstevel@tonic-gate
15007c478bd9Sstevel@tonic-gate state = (adt_internal_state_t *)session_data;
15017c478bd9Sstevel@tonic-gate
15027c478bd9Sstevel@tonic-gate assert(state->as_check == ADT_VALID);
15037c478bd9Sstevel@tonic-gate
15048c3c55e7Spaulson if ((state->as_have_user_data & (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) !=
15058c3c55e7Spaulson (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) {
15067c478bd9Sstevel@tonic-gate errno = EINVAL;
15077c478bd9Sstevel@tonic-gate goto return_err;
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate
15100ad2061eSgww if (setaudit_addr((auditinfo_addr_t *)&(state->as_info),
15110ad2061eSgww sizeof (auditinfo_addr_t)) < 0) {
15127c478bd9Sstevel@tonic-gate goto return_err; /* errno set by setaudit_addr() */
15130ad2061eSgww }
15147c478bd9Sstevel@tonic-gate
15157c478bd9Sstevel@tonic-gate state->as_session_model = ADT_PROCESS_MODEL;
15167c478bd9Sstevel@tonic-gate
15177c478bd9Sstevel@tonic-gate return (0);
15187c478bd9Sstevel@tonic-gate
15197c478bd9Sstevel@tonic-gate return_err:
15207c478bd9Sstevel@tonic-gate adt_write_syslog("failed to set process audit characteristics", errno);
15217c478bd9Sstevel@tonic-gate return (-1);
15227c478bd9Sstevel@tonic-gate }
15237c478bd9Sstevel@tonic-gate
15247c478bd9Sstevel@tonic-gate static int
adt_newuser(adt_internal_state_t * state,uid_t ruid,au_tid_addr_t * termid)15257c478bd9Sstevel@tonic-gate adt_newuser(adt_internal_state_t *state, uid_t ruid, au_tid_addr_t *termid)
15267c478bd9Sstevel@tonic-gate {
15277c478bd9Sstevel@tonic-gate au_tid_addr_t no_tid = {0, AU_IPv4, 0, 0, 0, 0};
15287c478bd9Sstevel@tonic-gate au_mask_t no_mask = {0, 0};
15297c478bd9Sstevel@tonic-gate
15307c478bd9Sstevel@tonic-gate if (ruid == ADT_NO_AUDIT) {
15317c478bd9Sstevel@tonic-gate state->as_info.ai_auid = AU_NOAUDITID;
15327c478bd9Sstevel@tonic-gate state->as_info.ai_asid = 0;
15337c478bd9Sstevel@tonic-gate state->as_info.ai_termid = no_tid;
15347c478bd9Sstevel@tonic-gate state->as_info.ai_mask = no_mask;
15357c478bd9Sstevel@tonic-gate return (0);
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate state->as_info.ai_auid = ruid;
15387c478bd9Sstevel@tonic-gate state->as_info.ai_asid = adt_get_unique_id(ruid);
15397c478bd9Sstevel@tonic-gate if (termid != NULL)
15407c478bd9Sstevel@tonic-gate state->as_info.ai_termid = *termid;
15417c478bd9Sstevel@tonic-gate
15427c478bd9Sstevel@tonic-gate if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask)))
15437c478bd9Sstevel@tonic-gate return (-1);
15447c478bd9Sstevel@tonic-gate
15452e74cda7Sgww /* Assume intending to audit as this process */
15462e74cda7Sgww
15472e74cda7Sgww if (state->as_pid == (pid_t)-1)
15482e74cda7Sgww state->as_pid = getpid();
15492e74cda7Sgww
15502e74cda7Sgww if (is_system_labeled() && state->as_label == NULL) {
15512e74cda7Sgww ucred_t *ucred = ucred_get(P_MYID);
15522e74cda7Sgww
15532e74cda7Sgww state->as_label = adt_ucred_label(ucred);
15542e74cda7Sgww ucred_free(ucred);
15552e74cda7Sgww }
15562e74cda7Sgww
15577c478bd9Sstevel@tonic-gate return (0);
15587c478bd9Sstevel@tonic-gate }
155926fba2a6Sgww
15607c478bd9Sstevel@tonic-gate static int
adt_changeuser(adt_internal_state_t * state,uid_t ruid)15617c478bd9Sstevel@tonic-gate adt_changeuser(adt_internal_state_t *state, uid_t ruid)
15627c478bd9Sstevel@tonic-gate {
15637c478bd9Sstevel@tonic-gate au_mask_t mask;
15647c478bd9Sstevel@tonic-gate
15657c478bd9Sstevel@tonic-gate if (!(state->as_have_user_data & ADT_HAVE_AUID))
15667c478bd9Sstevel@tonic-gate state->as_info.ai_auid = ruid;
15677c478bd9Sstevel@tonic-gate if (!(state->as_have_user_data & ADT_HAVE_ASID))
15687c478bd9Sstevel@tonic-gate state->as_info.ai_asid = adt_get_unique_id(ruid);
15697c478bd9Sstevel@tonic-gate
1570f48205beScasper if (ruid <= MAXEPHUID) {
15717c478bd9Sstevel@tonic-gate if (adt_get_mask_from_user(ruid, &mask))
15727c478bd9Sstevel@tonic-gate return (-1);
15737c478bd9Sstevel@tonic-gate
15747c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_success |= mask.am_success;
15757c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_failure |= mask.am_failure;
15767c478bd9Sstevel@tonic-gate }
15777c478bd9Sstevel@tonic-gate DPRINTF(("changed mask to %08X/%08X for ruid=%d\n",
15787c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_success,
15797c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_failure,
15807c478bd9Sstevel@tonic-gate ruid));
15817c478bd9Sstevel@tonic-gate return (0);
15827c478bd9Sstevel@tonic-gate }
158326fba2a6Sgww
15847c478bd9Sstevel@tonic-gate /*
15857c478bd9Sstevel@tonic-gate * adt_set_user -- see also adt_set_from_ucred()
15867c478bd9Sstevel@tonic-gate *
15877c478bd9Sstevel@tonic-gate * ADT_NO_ATTRIB is a valid uid/gid meaning "not known" or
158826fba2a6Sgww * "unattributed." If ruid, change the model to session.
15897c478bd9Sstevel@tonic-gate *
15907c478bd9Sstevel@tonic-gate * ADT_NO_CHANGE is a valid uid/gid meaning "do not change this value"
15917c478bd9Sstevel@tonic-gate * only valid with ADT_UPDATE.
15927c478bd9Sstevel@tonic-gate *
159326fba2a6Sgww * ADT_NO_AUDIT is the external equivalent to AU_NOAUDITID -- there
15947c478bd9Sstevel@tonic-gate * isn't a good reason to call adt_set_user() with it unless you don't
15957c478bd9Sstevel@tonic-gate * have a good value yet and intend to replace it later; auid will be
15967c478bd9Sstevel@tonic-gate * AU_NOAUDITID.
15977c478bd9Sstevel@tonic-gate *
15987c478bd9Sstevel@tonic-gate * adt_set_user should be called even if auditing is not enabled
15997c478bd9Sstevel@tonic-gate * so that adt_export_session_data() will have useful stuff to
16007c478bd9Sstevel@tonic-gate * work with.
16017c478bd9Sstevel@tonic-gate *
16027c478bd9Sstevel@tonic-gate * See the note preceding adt_set_proc() about the use of ADT_HAVE_TID
16037c478bd9Sstevel@tonic-gate * and ADT_HAVE_ALL.
16047c478bd9Sstevel@tonic-gate */
160526fba2a6Sgww
16067c478bd9Sstevel@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)160726fba2a6Sgww adt_set_user(const adt_session_data_t *session_data, uid_t euid, gid_t egid,
160826fba2a6Sgww uid_t ruid, gid_t rgid, const adt_termid_t *termid,
16097c478bd9Sstevel@tonic-gate enum adt_user_context user_context)
16107c478bd9Sstevel@tonic-gate {
16117c478bd9Sstevel@tonic-gate adt_internal_state_t *state;
16127c478bd9Sstevel@tonic-gate int rc;
16137c478bd9Sstevel@tonic-gate
16147c478bd9Sstevel@tonic-gate if (session_data == NULL) /* no session exists to audit */
16157c478bd9Sstevel@tonic-gate return (0);
16167c478bd9Sstevel@tonic-gate
16177c478bd9Sstevel@tonic-gate state = (adt_internal_state_t *)session_data;
16187c478bd9Sstevel@tonic-gate assert(state->as_check == ADT_VALID);
16197c478bd9Sstevel@tonic-gate
16207c478bd9Sstevel@tonic-gate switch (user_context) {
16217c478bd9Sstevel@tonic-gate case ADT_NEW:
16227c478bd9Sstevel@tonic-gate if (ruid == ADT_NO_CHANGE || euid == ADT_NO_CHANGE ||
16237c478bd9Sstevel@tonic-gate rgid == ADT_NO_CHANGE || egid == ADT_NO_CHANGE) {
16247c478bd9Sstevel@tonic-gate errno = EINVAL;
16257c478bd9Sstevel@tonic-gate return (-1);
16267c478bd9Sstevel@tonic-gate }
16277c478bd9Sstevel@tonic-gate if ((rc = adt_newuser(state, ruid,
16287c478bd9Sstevel@tonic-gate (au_tid_addr_t *)termid)) != 0)
16297c478bd9Sstevel@tonic-gate return (rc);
16307c478bd9Sstevel@tonic-gate
16317c478bd9Sstevel@tonic-gate state->as_have_user_data = ADT_HAVE_ALL;
16327c478bd9Sstevel@tonic-gate break;
16337c478bd9Sstevel@tonic-gate case ADT_UPDATE:
16347c478bd9Sstevel@tonic-gate if (state->as_have_user_data != ADT_HAVE_ALL) {
16357c478bd9Sstevel@tonic-gate errno = EINVAL;
16367c478bd9Sstevel@tonic-gate return (-1);
16377c478bd9Sstevel@tonic-gate }
16387c478bd9Sstevel@tonic-gate
16397c478bd9Sstevel@tonic-gate if (ruid != ADT_NO_CHANGE)
16407c478bd9Sstevel@tonic-gate if ((rc = adt_changeuser(state, ruid)) != 0)
16417c478bd9Sstevel@tonic-gate return (rc);
16427c478bd9Sstevel@tonic-gate break;
16437c478bd9Sstevel@tonic-gate case ADT_USER:
16447c478bd9Sstevel@tonic-gate if (state->as_have_user_data != ADT_HAVE_ALL) {
16457c478bd9Sstevel@tonic-gate errno = EINVAL;
16467c478bd9Sstevel@tonic-gate return (-1);
16477c478bd9Sstevel@tonic-gate }
16487c478bd9Sstevel@tonic-gate break;
16497c478bd9Sstevel@tonic-gate case ADT_SETTID:
16507c478bd9Sstevel@tonic-gate assert(termid != NULL);
16517c478bd9Sstevel@tonic-gate state->as_info.ai_termid = *((au_tid_addr_t *)termid);
16527c478bd9Sstevel@tonic-gate /* avoid fooling pam_setcred()... */
16537c478bd9Sstevel@tonic-gate state->as_info.ai_auid = AU_NOAUDITID;
16547c478bd9Sstevel@tonic-gate state->as_info.ai_asid = 0;
16557c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_failure = 0;
16567c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_success = 0;
16577c478bd9Sstevel@tonic-gate state->as_have_user_data = ADT_HAVE_TID |
16587c478bd9Sstevel@tonic-gate ADT_HAVE_AUID | ADT_HAVE_ASID | ADT_HAVE_MASK;
16597c478bd9Sstevel@tonic-gate return (0);
16607c478bd9Sstevel@tonic-gate default:
16617c478bd9Sstevel@tonic-gate errno = EINVAL;
16627c478bd9Sstevel@tonic-gate return (-1);
16637c478bd9Sstevel@tonic-gate }
16647c478bd9Sstevel@tonic-gate
16657c478bd9Sstevel@tonic-gate if (ruid == ADT_NO_AUDIT) {
16667c478bd9Sstevel@tonic-gate state->as_ruid = AU_NOAUDITID;
16677c478bd9Sstevel@tonic-gate state->as_euid = AU_NOAUDITID;
16687c478bd9Sstevel@tonic-gate state->as_rgid = AU_NOAUDITID;
16697c478bd9Sstevel@tonic-gate state->as_egid = AU_NOAUDITID;
16707c478bd9Sstevel@tonic-gate } else {
16717c478bd9Sstevel@tonic-gate if (ruid != ADT_NO_CHANGE)
16727c478bd9Sstevel@tonic-gate state->as_ruid = ruid;
16737c478bd9Sstevel@tonic-gate if (euid != ADT_NO_CHANGE)
16747c478bd9Sstevel@tonic-gate state->as_euid = euid;
16757c478bd9Sstevel@tonic-gate if (rgid != ADT_NO_CHANGE)
16767c478bd9Sstevel@tonic-gate state->as_rgid = rgid;
16777c478bd9Sstevel@tonic-gate if (egid != ADT_NO_CHANGE)
16787c478bd9Sstevel@tonic-gate state->as_egid = egid;
16797c478bd9Sstevel@tonic-gate }
16807c478bd9Sstevel@tonic-gate
168126fba2a6Sgww if (ruid == ADT_NO_ATTRIB) {
168226fba2a6Sgww state->as_session_model = ADT_SESSION_MODEL;
168326fba2a6Sgww }
168426fba2a6Sgww
16857c478bd9Sstevel@tonic-gate return (0);
16867c478bd9Sstevel@tonic-gate }
168726fba2a6Sgww
16887c478bd9Sstevel@tonic-gate /*
16897c478bd9Sstevel@tonic-gate * adt_set_from_ucred()
16907c478bd9Sstevel@tonic-gate *
16917c478bd9Sstevel@tonic-gate * an alternate to adt_set_user that fills the same role but uses
16927c478bd9Sstevel@tonic-gate * a pointer to a ucred rather than a list of id's. If the ucred
16937c478bd9Sstevel@tonic-gate * pointer is NULL, use the credential from the this process.
16947c478bd9Sstevel@tonic-gate *
16957c478bd9Sstevel@tonic-gate * A key difference is that for ADT_NEW, adt_set_from_ucred() does
16967c478bd9Sstevel@tonic-gate * not overwrite the asid and auid unless auid has not been set.
16977c478bd9Sstevel@tonic-gate * ADT_NEW differs from ADT_UPDATE in that it does not OR together
16987c478bd9Sstevel@tonic-gate * the incoming audit mask with the one that already exists.
16997c478bd9Sstevel@tonic-gate *
17007c478bd9Sstevel@tonic-gate * adt_set_from_ucred should be called even if auditing is not enabled
17017c478bd9Sstevel@tonic-gate * so that adt_export_session_data() will have useful stuff to
17027c478bd9Sstevel@tonic-gate * work with.
17037c478bd9Sstevel@tonic-gate */
170426fba2a6Sgww
17057c478bd9Sstevel@tonic-gate int
adt_set_from_ucred(const adt_session_data_t * session_data,const ucred_t * uc,enum adt_user_context user_context)170626fba2a6Sgww adt_set_from_ucred(const adt_session_data_t *session_data, const ucred_t *uc,
170726fba2a6Sgww enum adt_user_context user_context)
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate adt_internal_state_t *state;
17107c478bd9Sstevel@tonic-gate int rc = -1;
17117c478bd9Sstevel@tonic-gate const au_tid64_addr_t *tid64;
17127c478bd9Sstevel@tonic-gate au_tid_addr_t termid, *tid;
171326706799Sme23304 ucred_t *ucred = (ucred_t *)uc;
171426706799Sme23304 boolean_t local_uc = B_FALSE;
17157c478bd9Sstevel@tonic-gate
17167c478bd9Sstevel@tonic-gate if (session_data == NULL) /* no session exists to audit */
17177c478bd9Sstevel@tonic-gate return (0);
17187c478bd9Sstevel@tonic-gate
17197c478bd9Sstevel@tonic-gate state = (adt_internal_state_t *)session_data;
17207c478bd9Sstevel@tonic-gate assert(state->as_check == ADT_VALID);
17217c478bd9Sstevel@tonic-gate
17227c478bd9Sstevel@tonic-gate if (ucred == NULL) {
1723b3baaabfSgww ucred = ucred_get(P_MYID);
17247c478bd9Sstevel@tonic-gate
17257c478bd9Sstevel@tonic-gate if (ucred == NULL)
17267c478bd9Sstevel@tonic-gate goto return_rc;
172726706799Sme23304 local_uc = B_TRUE;
17287c478bd9Sstevel@tonic-gate }
17297c478bd9Sstevel@tonic-gate
17307c478bd9Sstevel@tonic-gate switch (user_context) {
17317c478bd9Sstevel@tonic-gate case ADT_NEW:
17327c478bd9Sstevel@tonic-gate tid64 = ucred_getatid(ucred);
17337c478bd9Sstevel@tonic-gate if (tid64 != NULL) {
17347c478bd9Sstevel@tonic-gate adt_cpy_tid(&termid, tid64);
17357c478bd9Sstevel@tonic-gate tid = &termid;
17367c478bd9Sstevel@tonic-gate } else {
17377c478bd9Sstevel@tonic-gate tid = NULL;
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate if (ucred_getauid(ucred) == AU_NOAUDITID) {
1740b3baaabfSgww adt_setto_unaudited(state);
1741b3baaabfSgww state->as_have_user_data = ADT_HAVE_ALL;
1742b3baaabfSgww rc = 0;
17437c478bd9Sstevel@tonic-gate goto return_rc;
17447c478bd9Sstevel@tonic-gate } else {
17457c478bd9Sstevel@tonic-gate state->as_info.ai_auid = ucred_getauid(ucred);
17467c478bd9Sstevel@tonic-gate state->as_info.ai_asid = ucred_getasid(ucred);
17477c478bd9Sstevel@tonic-gate state->as_info.ai_mask = *ucred_getamask(ucred);
17487c478bd9Sstevel@tonic-gate state->as_info.ai_termid = *tid;
17497c478bd9Sstevel@tonic-gate }
17507c478bd9Sstevel@tonic-gate state->as_have_user_data = ADT_HAVE_ALL;
17517c478bd9Sstevel@tonic-gate break;
17527c478bd9Sstevel@tonic-gate case ADT_UPDATE:
17537c478bd9Sstevel@tonic-gate if (state->as_have_user_data != ADT_HAVE_ALL) {
17547c478bd9Sstevel@tonic-gate errno = EINVAL;
17557c478bd9Sstevel@tonic-gate goto return_rc;
17567c478bd9Sstevel@tonic-gate }
17577c478bd9Sstevel@tonic-gate
17587c478bd9Sstevel@tonic-gate if ((rc = adt_changeuser(state, ucred_getruid(ucred))) != 0)
17597c478bd9Sstevel@tonic-gate goto return_rc;
17607c478bd9Sstevel@tonic-gate break;
17617c478bd9Sstevel@tonic-gate case ADT_USER:
17627c478bd9Sstevel@tonic-gate if (state->as_have_user_data != ADT_HAVE_ALL) {
17637c478bd9Sstevel@tonic-gate errno = EINVAL;
17647c478bd9Sstevel@tonic-gate goto return_rc;
17657c478bd9Sstevel@tonic-gate }
17667c478bd9Sstevel@tonic-gate break;
17677c478bd9Sstevel@tonic-gate default:
17687c478bd9Sstevel@tonic-gate errno = EINVAL;
17697c478bd9Sstevel@tonic-gate goto return_rc;
17707c478bd9Sstevel@tonic-gate }
17717c478bd9Sstevel@tonic-gate rc = 0;
17727c478bd9Sstevel@tonic-gate
17737c478bd9Sstevel@tonic-gate state->as_ruid = ucred_getruid(ucred);
17747c478bd9Sstevel@tonic-gate state->as_euid = ucred_geteuid(ucred);
17757c478bd9Sstevel@tonic-gate state->as_rgid = ucred_getrgid(ucred);
17767c478bd9Sstevel@tonic-gate state->as_egid = ucred_getegid(ucred);
1777c529a23fSgww state->as_pid = ucred_getpid(ucred);
1778c529a23fSgww state->as_label = adt_ucred_label(ucred);
17797c478bd9Sstevel@tonic-gate
17807c478bd9Sstevel@tonic-gate return_rc:
178126706799Sme23304 if (local_uc) {
178226706799Sme23304 ucred_free(ucred);
178326706799Sme23304 }
17847c478bd9Sstevel@tonic-gate return (rc);
17857c478bd9Sstevel@tonic-gate }
17867c478bd9Sstevel@tonic-gate
17877c478bd9Sstevel@tonic-gate /*
17887c478bd9Sstevel@tonic-gate * adt_alloc_event() returns a pointer to allocated memory
17897c478bd9Sstevel@tonic-gate *
17907c478bd9Sstevel@tonic-gate */
17917c478bd9Sstevel@tonic-gate
17927c478bd9Sstevel@tonic-gate adt_event_data_t
adt_alloc_event(const adt_session_data_t * session_data,au_event_t event_id)17937c478bd9Sstevel@tonic-gate *adt_alloc_event(const adt_session_data_t *session_data, au_event_t event_id)
17947c478bd9Sstevel@tonic-gate {
17957c478bd9Sstevel@tonic-gate struct adt_event_state *event_state;
17967c478bd9Sstevel@tonic-gate adt_internal_state_t *session_state;
17977c478bd9Sstevel@tonic-gate adt_event_data_t *return_event = NULL;
17987c478bd9Sstevel@tonic-gate /*
17997c478bd9Sstevel@tonic-gate * need to return a valid event pointer even if audit is
18007c478bd9Sstevel@tonic-gate * off, else the caller will end up either (1) keeping its
18017c478bd9Sstevel@tonic-gate * own flags for on/off or (2) writing to a NULL pointer.
18027c478bd9Sstevel@tonic-gate * If auditing is on, the session data must be valid; otherwise
18037c478bd9Sstevel@tonic-gate * we don't care.
18047c478bd9Sstevel@tonic-gate */
18057c478bd9Sstevel@tonic-gate if (session_data != NULL) {
18067c478bd9Sstevel@tonic-gate session_state = (adt_internal_state_t *)session_data;
18077c478bd9Sstevel@tonic-gate assert(session_state->as_check == ADT_VALID);
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate event_state = calloc(1, sizeof (struct adt_event_state));
18107c478bd9Sstevel@tonic-gate if (event_state == NULL)
18117c478bd9Sstevel@tonic-gate goto return_ptr;
18127c478bd9Sstevel@tonic-gate
18137c478bd9Sstevel@tonic-gate event_state->ae_check = ADT_VALID;
18147c478bd9Sstevel@tonic-gate
18157c478bd9Sstevel@tonic-gate event_state->ae_event_id = event_id;
18167c478bd9Sstevel@tonic-gate event_state->ae_session = (struct adt_internal_state *)session_data;
18177c478bd9Sstevel@tonic-gate
18187c478bd9Sstevel@tonic-gate return_event = (adt_event_data_t *)&(event_state->ae_event_data);
18197c478bd9Sstevel@tonic-gate
18207c478bd9Sstevel@tonic-gate /*
18217c478bd9Sstevel@tonic-gate * preload data so the adt_au_*() functions can detect un-supplied
18227c478bd9Sstevel@tonic-gate * values (0 and NULL are free via calloc()).
18237c478bd9Sstevel@tonic-gate */
18246193acb9Sgww if (session_data != NULL) {
18250ad2061eSgww session_state->as_preload(event_id, return_event);
18266193acb9Sgww }
18277c478bd9Sstevel@tonic-gate
18287c478bd9Sstevel@tonic-gate return_ptr:
18297c478bd9Sstevel@tonic-gate return (return_event);
18307c478bd9Sstevel@tonic-gate }
18317c478bd9Sstevel@tonic-gate
18327c478bd9Sstevel@tonic-gate /*
18337c478bd9Sstevel@tonic-gate * adt_getXlateTable -- look up translation table address for event id
18347c478bd9Sstevel@tonic-gate */
18357c478bd9Sstevel@tonic-gate
18360ad2061eSgww static adt_translation_t *
adt_getXlateTable(adt_translation_t ** xlate,au_event_t event_id)18370ad2061eSgww adt_getXlateTable(adt_translation_t **xlate, au_event_t event_id)
18387c478bd9Sstevel@tonic-gate {
18397c478bd9Sstevel@tonic-gate /* xlate_table is global in adt_xlate.c */
18400ad2061eSgww adt_translation_t **p_xlate = xlate;
18410ad2061eSgww adt_translation_t *p_event;
18427c478bd9Sstevel@tonic-gate
18437c478bd9Sstevel@tonic-gate while (*p_xlate != NULL) {
18447c478bd9Sstevel@tonic-gate p_event = *p_xlate;
18457c478bd9Sstevel@tonic-gate if (event_id == p_event->tx_external_event)
18467c478bd9Sstevel@tonic-gate return (p_event);
18477c478bd9Sstevel@tonic-gate p_xlate++;
18487c478bd9Sstevel@tonic-gate }
18497c478bd9Sstevel@tonic-gate return (NULL);
18507c478bd9Sstevel@tonic-gate }
18517c478bd9Sstevel@tonic-gate
18527c478bd9Sstevel@tonic-gate /*
18537c478bd9Sstevel@tonic-gate * adt_calcOffsets
18547c478bd9Sstevel@tonic-gate *
18557c478bd9Sstevel@tonic-gate * the call to this function is surrounded by a mutex.
18567c478bd9Sstevel@tonic-gate *
18577c478bd9Sstevel@tonic-gate * i walks down the table picking up next_token. j walks again to
18587c478bd9Sstevel@tonic-gate * calculate the offset to the input data. k points to the next
18597c478bd9Sstevel@tonic-gate * token's row. Finally, l, is used to sum the values in the
18607c478bd9Sstevel@tonic-gate * datadef array.
18617c478bd9Sstevel@tonic-gate *
18627c478bd9Sstevel@tonic-gate * What's going on? The entry array is in the order of the input
18637c478bd9Sstevel@tonic-gate * fields but the processing of array entries is in the order of
18647c478bd9Sstevel@tonic-gate * the output (see next_token). Calculating the offset to the
18657c478bd9Sstevel@tonic-gate * "next" input can't be done in the outer loop (i) since i doesn't
18667c478bd9Sstevel@tonic-gate * point to the current entry and it can't be done with the k index
18677c478bd9Sstevel@tonic-gate * because it doesn't represent the order of input fields.
18687c478bd9Sstevel@tonic-gate *
18697c478bd9Sstevel@tonic-gate * While the resulting algorithm is n**2, it is only done once per
18707c478bd9Sstevel@tonic-gate * event type.
18717c478bd9Sstevel@tonic-gate */
18727c478bd9Sstevel@tonic-gate
18737c478bd9Sstevel@tonic-gate /*
18747c478bd9Sstevel@tonic-gate * adt_calcOffsets is only called once per event type, but it uses
18757c478bd9Sstevel@tonic-gate * the address alignment of memory allocated for that event as if it
18767c478bd9Sstevel@tonic-gate * were the same for all subsequently allocated memory. This is
18777c478bd9Sstevel@tonic-gate * guaranteed by calloc/malloc. Arrays take special handling since
18787c478bd9Sstevel@tonic-gate * what matters for figuring out the correct alignment is the size
18797c478bd9Sstevel@tonic-gate * of the array element.
18807c478bd9Sstevel@tonic-gate */
18817c478bd9Sstevel@tonic-gate
18827c478bd9Sstevel@tonic-gate static void
adt_calcOffsets(struct entry * p_entry,int tablesize,void * p_data)18837c478bd9Sstevel@tonic-gate adt_calcOffsets(struct entry *p_entry, int tablesize, void *p_data)
18847c478bd9Sstevel@tonic-gate {
18857c478bd9Sstevel@tonic-gate int i, j;
18867c478bd9Sstevel@tonic-gate size_t this_size, prev_size;
18877c478bd9Sstevel@tonic-gate void *struct_start = p_data;
18887c478bd9Sstevel@tonic-gate
18897c478bd9Sstevel@tonic-gate for (i = 0; i < tablesize; i++) {
18907c478bd9Sstevel@tonic-gate if (p_entry[i].en_type_def == NULL) {
18917c478bd9Sstevel@tonic-gate p_entry[i].en_offset = 0;
18927c478bd9Sstevel@tonic-gate continue;
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate prev_size = 0;
18957c478bd9Sstevel@tonic-gate p_entry[i].en_offset = (char *)p_data - (char *)struct_start;
18967c478bd9Sstevel@tonic-gate
18977c478bd9Sstevel@tonic-gate for (j = 0; j < p_entry[i].en_count_types; j++) {
18987c478bd9Sstevel@tonic-gate if (p_entry[i].en_type_def[j].dd_datatype == ADT_MSG)
18997c478bd9Sstevel@tonic-gate this_size = sizeof (enum adt_generic);
19007c478bd9Sstevel@tonic-gate else
19017c478bd9Sstevel@tonic-gate this_size =
19027c478bd9Sstevel@tonic-gate p_entry[i].en_type_def[j].dd_input_size;
19037c478bd9Sstevel@tonic-gate
19047c478bd9Sstevel@tonic-gate /* adj for first entry */
19057c478bd9Sstevel@tonic-gate if (prev_size == 0)
19067c478bd9Sstevel@tonic-gate prev_size = this_size;
19077c478bd9Sstevel@tonic-gate
19087c478bd9Sstevel@tonic-gate if (p_entry[i].en_type_def[j].dd_datatype ==
19097c478bd9Sstevel@tonic-gate ADT_UINT32ARRAY) {
19107c478bd9Sstevel@tonic-gate p_data = (char *)adt_adjust_address(p_data,
19117c478bd9Sstevel@tonic-gate prev_size, sizeof (uint32_t)) +
19127c478bd9Sstevel@tonic-gate this_size - sizeof (uint32_t);
19137c478bd9Sstevel@tonic-gate
19147c478bd9Sstevel@tonic-gate prev_size = sizeof (uint32_t);
19157c478bd9Sstevel@tonic-gate } else {
19167c478bd9Sstevel@tonic-gate p_data = adt_adjust_address(p_data, prev_size,
19177c478bd9Sstevel@tonic-gate this_size);
19187c478bd9Sstevel@tonic-gate prev_size = this_size;
19197c478bd9Sstevel@tonic-gate }
19207c478bd9Sstevel@tonic-gate }
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate }
19237c478bd9Sstevel@tonic-gate
19247c478bd9Sstevel@tonic-gate /*
19257c478bd9Sstevel@tonic-gate * adt_generate_event
19267c478bd9Sstevel@tonic-gate * generate event record from external struct. The order is based on
19277c478bd9Sstevel@tonic-gate * the output tokens, allowing for the possibility that the input data
19287c478bd9Sstevel@tonic-gate * is in a different order.
19297c478bd9Sstevel@tonic-gate *
19307c478bd9Sstevel@tonic-gate */
19317c478bd9Sstevel@tonic-gate
19320ad2061eSgww static int
adt_generate_event(const adt_event_data_t * p_extdata,struct adt_event_state * p_event,adt_translation_t * p_xlate)19337c478bd9Sstevel@tonic-gate adt_generate_event(const adt_event_data_t *p_extdata,
19347c478bd9Sstevel@tonic-gate struct adt_event_state *p_event,
19350ad2061eSgww adt_translation_t *p_xlate)
19367c478bd9Sstevel@tonic-gate {
19377c478bd9Sstevel@tonic-gate struct entry *p_entry;
19387c478bd9Sstevel@tonic-gate static mutex_t lock = DEFAULTMUTEX;
19397c478bd9Sstevel@tonic-gate
19407c478bd9Sstevel@tonic-gate p_entry = p_xlate->tx_first_entry;
19417c478bd9Sstevel@tonic-gate assert(p_entry != NULL);
19427c478bd9Sstevel@tonic-gate
19437c478bd9Sstevel@tonic-gate p_event->ae_internal_id = p_xlate->tx_internal_event;
19447c478bd9Sstevel@tonic-gate adt_token_open(p_event);
19457c478bd9Sstevel@tonic-gate
19467c478bd9Sstevel@tonic-gate /*
19477c478bd9Sstevel@tonic-gate * offsets are not pre-calculated; the initial offsets are all
19487c478bd9Sstevel@tonic-gate * 0; valid offsets are >= 0. Offsets for no-input tokens such
19497c478bd9Sstevel@tonic-gate * as subject are set to -1 by adt_calcOffset()
19507c478bd9Sstevel@tonic-gate */
19517c478bd9Sstevel@tonic-gate if (p_xlate->tx_offsetsCalculated == 0) {
19527257d1b4Sraf (void) mutex_lock(&lock);
19537c478bd9Sstevel@tonic-gate p_xlate->tx_offsetsCalculated = 1;
19547c478bd9Sstevel@tonic-gate
19557c478bd9Sstevel@tonic-gate adt_calcOffsets(p_xlate->tx_top_entry, p_xlate->tx_entries,
19567c478bd9Sstevel@tonic-gate (void *)p_extdata);
19577257d1b4Sraf (void) mutex_unlock(&lock);
19587c478bd9Sstevel@tonic-gate }
19597c478bd9Sstevel@tonic-gate while (p_entry != NULL) {
19600ad2061eSgww adt_generate_token(p_entry, (char *)p_extdata, p_event);
19617c478bd9Sstevel@tonic-gate
19627c478bd9Sstevel@tonic-gate p_entry = p_entry->en_next_token;
19637c478bd9Sstevel@tonic-gate }
19640ad2061eSgww return (adt_token_close(p_event));
19657c478bd9Sstevel@tonic-gate }
19667c478bd9Sstevel@tonic-gate
19677c478bd9Sstevel@tonic-gate /*
19687c478bd9Sstevel@tonic-gate * adt_put_event -- main event generation function.
19697c478bd9Sstevel@tonic-gate * The input "event" is the address of the struct containing
19707c478bd9Sstevel@tonic-gate * event-specific data.
19717c478bd9Sstevel@tonic-gate *
19727c478bd9Sstevel@tonic-gate * However if auditing is off or the session handle
19737c478bd9Sstevel@tonic-gate * is NULL, no attempt to write a record is made.
19747c478bd9Sstevel@tonic-gate */
19757c478bd9Sstevel@tonic-gate
19767c478bd9Sstevel@tonic-gate int
adt_put_event(const adt_event_data_t * event,int status,int return_val)197726fba2a6Sgww adt_put_event(const adt_event_data_t *event, int status, int return_val)
19787c478bd9Sstevel@tonic-gate {
19797c478bd9Sstevel@tonic-gate struct adt_event_state *event_state;
19800ad2061eSgww adt_translation_t *xlate;
19817c478bd9Sstevel@tonic-gate
19827c478bd9Sstevel@tonic-gate if (event == NULL) {
19837c478bd9Sstevel@tonic-gate errno = EINVAL;
19840ad2061eSgww return (-1);
19857c478bd9Sstevel@tonic-gate }
19867c478bd9Sstevel@tonic-gate event_state = (struct adt_event_state *)event;
19877c478bd9Sstevel@tonic-gate
198891b2cbb3Sgww /* if this is a broken session or not auditing, exit */
198991b2cbb3Sgww if ((event_state->ae_session == NULL) ||
199091b2cbb3Sgww !(event_state->ae_session->as_audit_state &
199191b2cbb3Sgww (AUC_AUDITING | AUC_NOSPACE))) {
19920ad2061eSgww return (0);
19930ad2061eSgww }
19947c478bd9Sstevel@tonic-gate
19957c478bd9Sstevel@tonic-gate assert(event_state->ae_check == ADT_VALID);
19967c478bd9Sstevel@tonic-gate
19977c478bd9Sstevel@tonic-gate event_state->ae_rc = status;
19987c478bd9Sstevel@tonic-gate event_state->ae_type = return_val;
19997c478bd9Sstevel@tonic-gate
20007c478bd9Sstevel@tonic-gate /* look up the event */
20017c478bd9Sstevel@tonic-gate
20020ad2061eSgww xlate = adt_getXlateTable(event_state->ae_session->as_xlate,
20030ad2061eSgww event_state->ae_event_id);
20047c478bd9Sstevel@tonic-gate
20057c478bd9Sstevel@tonic-gate if (xlate == NULL) {
20067c478bd9Sstevel@tonic-gate errno = EINVAL;
20070ad2061eSgww return (-1);
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate DPRINTF(("got event %d\n", xlate->tx_internal_event));
20107c478bd9Sstevel@tonic-gate
20110ad2061eSgww if (adt_selected(event_state, xlate->tx_internal_event, status)) {
20120ad2061eSgww return (adt_generate_event(event, event_state, xlate));
20130ad2061eSgww }
20147c478bd9Sstevel@tonic-gate
20150ad2061eSgww return (0);
20167c478bd9Sstevel@tonic-gate }
20177c478bd9Sstevel@tonic-gate
20187c478bd9Sstevel@tonic-gate /*
20197c478bd9Sstevel@tonic-gate * adt_free_event -- invalidate and free
20207c478bd9Sstevel@tonic-gate */
20217c478bd9Sstevel@tonic-gate
20227c478bd9Sstevel@tonic-gate void
adt_free_event(adt_event_data_t * event)20237c478bd9Sstevel@tonic-gate adt_free_event(adt_event_data_t *event)
20247c478bd9Sstevel@tonic-gate {
20257c478bd9Sstevel@tonic-gate struct adt_event_state *event_state;
20267c478bd9Sstevel@tonic-gate
20277c478bd9Sstevel@tonic-gate if (event == NULL)
20287c478bd9Sstevel@tonic-gate return;
20297c478bd9Sstevel@tonic-gate
20307c478bd9Sstevel@tonic-gate event_state = (struct adt_event_state *)event;
20317c478bd9Sstevel@tonic-gate
20327c478bd9Sstevel@tonic-gate assert(event_state->ae_check == ADT_VALID);
20337c478bd9Sstevel@tonic-gate
20347c478bd9Sstevel@tonic-gate event_state->ae_check = 0;
20357c478bd9Sstevel@tonic-gate
20367c478bd9Sstevel@tonic-gate free(event_state);
20377c478bd9Sstevel@tonic-gate }
20387c478bd9Sstevel@tonic-gate
20397c478bd9Sstevel@tonic-gate /*
20407c478bd9Sstevel@tonic-gate * adt_is_selected -- helper to adt_selected(), below.
20417c478bd9Sstevel@tonic-gate *
20427c478bd9Sstevel@tonic-gate * "sorf" is "success or fail" status; au_preselect compares
20437c478bd9Sstevel@tonic-gate * that with success, fail, or both.
20447c478bd9Sstevel@tonic-gate */
20457c478bd9Sstevel@tonic-gate
20467c478bd9Sstevel@tonic-gate static int
adt_is_selected(au_event_t e,au_mask_t * m,int sorf)20477c478bd9Sstevel@tonic-gate adt_is_selected(au_event_t e, au_mask_t *m, int sorf)
20487c478bd9Sstevel@tonic-gate {
20497c478bd9Sstevel@tonic-gate int prs_sorf;
20507c478bd9Sstevel@tonic-gate
20517c478bd9Sstevel@tonic-gate if (sorf == 0)
20527c478bd9Sstevel@tonic-gate prs_sorf = AU_PRS_SUCCESS;
20537c478bd9Sstevel@tonic-gate else
20547c478bd9Sstevel@tonic-gate prs_sorf = AU_PRS_FAILURE;
20557c478bd9Sstevel@tonic-gate
20567c478bd9Sstevel@tonic-gate return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD));
20577c478bd9Sstevel@tonic-gate }
20587c478bd9Sstevel@tonic-gate
20597c478bd9Sstevel@tonic-gate /*
20607c478bd9Sstevel@tonic-gate * selected -- see if this event is preselected.
20617c478bd9Sstevel@tonic-gate *
20627c478bd9Sstevel@tonic-gate * if errors are encountered trying to check a preselection mask
20637c478bd9Sstevel@tonic-gate * or look up a user name, the event is selected. Otherwise, the
20647c478bd9Sstevel@tonic-gate * preselection mask is used for the job.
20657c478bd9Sstevel@tonic-gate */
20667c478bd9Sstevel@tonic-gate
20677c478bd9Sstevel@tonic-gate static int
adt_selected(struct adt_event_state * event,au_event_t actual_id,int status)20687c478bd9Sstevel@tonic-gate adt_selected(struct adt_event_state *event, au_event_t actual_id, int status)
20697c478bd9Sstevel@tonic-gate {
20707c478bd9Sstevel@tonic-gate adt_internal_state_t *sp;
20717c478bd9Sstevel@tonic-gate au_mask_t namask;
20727c478bd9Sstevel@tonic-gate
20737c478bd9Sstevel@tonic-gate sp = event->ae_session;
20747c478bd9Sstevel@tonic-gate
20757c478bd9Sstevel@tonic-gate if ((sp->as_have_user_data & ADT_HAVE_IDS) == 0) {
20767c478bd9Sstevel@tonic-gate adt_write_syslog("No user data available", EINVAL);
20777c478bd9Sstevel@tonic-gate return (1); /* default is "selected" */
20787c478bd9Sstevel@tonic-gate }
20797c478bd9Sstevel@tonic-gate
20807c478bd9Sstevel@tonic-gate /* non-attributable? */
20817c478bd9Sstevel@tonic-gate if ((sp->as_info.ai_auid == AU_NOAUDITID) ||
20827c478bd9Sstevel@tonic-gate (sp->as_info.ai_auid == ADT_NO_AUDIT)) {
20837c478bd9Sstevel@tonic-gate if (auditon(A_GETKMASK, (caddr_t)&namask,
20847c478bd9Sstevel@tonic-gate sizeof (namask)) != 0) {
20857c478bd9Sstevel@tonic-gate adt_write_syslog("auditon failure", errno);
20867c478bd9Sstevel@tonic-gate return (1);
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate return (adt_is_selected(actual_id, &namask, status));
20897c478bd9Sstevel@tonic-gate } else {
20907c478bd9Sstevel@tonic-gate return (adt_is_selected(actual_id, &(sp->as_info.ai_mask),
20917c478bd9Sstevel@tonic-gate status));
20927c478bd9Sstevel@tonic-gate }
20937c478bd9Sstevel@tonic-gate }
20949697ae98Sgww
20959697ae98Sgww /*
20969697ae98Sgww * Can't map the host name to an IP address in
20979697ae98Sgww * adt_get_hostIP. Get something off an interface
20989697ae98Sgww * to act as the hosts IP address for auditing.
20999697ae98Sgww */
21009697ae98Sgww
210191b2cbb3Sgww static int
adt_get_local_address(int family,struct ifaddrlist * al)21029697ae98Sgww adt_get_local_address(int family, struct ifaddrlist *al)
21039697ae98Sgww {
21049697ae98Sgww struct ifaddrlist *ifal;
21059697ae98Sgww char errbuf[ERRBUFSIZE] = "empty list";
21069697ae98Sgww char msg[ERRBUFSIZE + 512];
21079697ae98Sgww int ifal_count;
21089697ae98Sgww int i;
21099697ae98Sgww
2110413d88ffSgww if ((ifal_count = ifaddrlist(&ifal, family, 0, errbuf)) < 0) {
21119697ae98Sgww int serrno = errno;
21129697ae98Sgww
21139697ae98Sgww (void) snprintf(msg, sizeof (msg), "adt_get_local_address "
21149697ae98Sgww "couldn't get %d addrlist %s", family, errbuf);
21159697ae98Sgww adt_write_syslog(msg, serrno);
21169697ae98Sgww errno = serrno;
21179697ae98Sgww return (-1);
21189697ae98Sgww }
21199697ae98Sgww
21209697ae98Sgww for (i = 0; i < ifal_count; i++) {
21219697ae98Sgww /*
21229697ae98Sgww * loopback always defined,
21239697ae98Sgww * even if there is no real address
21249697ae98Sgww */
21259697ae98Sgww if ((ifal[i].flags & (IFF_UP | IFF_LOOPBACK)) == IFF_UP) {
21269697ae98Sgww break;
21279697ae98Sgww }
21289697ae98Sgww }
21299697ae98Sgww if (i >= ifal_count) {
21309697ae98Sgww free(ifal);
21319697ae98Sgww /*
21329697ae98Sgww * Callers of adt_get_hostIP() can only return
21339697ae98Sgww * errno to their callers and eventually the application.
21349697ae98Sgww * Picked one that seemed least worse for saying no
21359697ae98Sgww * usable address for Audit terminal ID.
21369697ae98Sgww */
21379697ae98Sgww errno = ENETDOWN;
21389697ae98Sgww return (-1);
21399697ae98Sgww }
21409697ae98Sgww
21419697ae98Sgww *al = ifal[i];
21429697ae98Sgww free(ifal);
21439697ae98Sgww return (0);
21449697ae98Sgww }
2145