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