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.
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.
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]
23 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
27 #include <bsm/adt_event.h>
29 #include <bsm/audit.h>
30 #include <bsm/audit_record.h>
31 #include <bsm/libbsm.h>
36 #include <sys/mkdev.h>
38 #include <nss_dbdefs.h>
45 #include <sys/systeminfo.h>
49 #include <adt_xlate.h>
50 #include <adt_ucred.h>
51 #include <arpa/inet.h>
53 #include <libinetutil.h>
55 static int adt_selected(struct adt_event_state
*, au_event_t
, int);
56 static int adt_init(adt_internal_state_t
*, int);
57 static int adt_import(adt_internal_state_t
*, const adt_export_data_t
*);
58 static void adt_setto_unaudited(adt_internal_state_t
*);
59 static int adt_get_local_address(int, struct ifaddrlist
*);
62 #define DPRINTF(x) { (void) printf x; }
63 #define DFLUSH (void) fflush(stdout);
70 * Local audit states are a bit mask
72 * The global audit states are
74 * AUC_UNSET 0 - on/off hasn't been decided
75 * AUC_ENABLED 1 - loaded and enabled
77 * The local Zone states are
79 * AUC_AUDITING 0x1 - audit daemon is active
80 * AUC_NOAUDIT 0x2 - audit daemon is not active
81 * AUC_INIT_AUDIT 0x4 - audit is ready but auditd has not run
82 * AUC_NOSPACE 0x8 - audit enabled, no space for audit records
84 * The only values returned by auditon(A_GETCOND) are:
85 * AUC_INIT_AUDIT, AUC_AUDITING, AUC_NOAUDIT, AUC_NOSPACE
87 * The pseudo audit state used when the c2audit module is excluded is
89 * AUC_DISABLED 0x100 - c2audit module is excluded
92 static int auditstate
= AUC_DISABLED
; /* default state */
97 * errors that are not the user's fault (bugs or whatever in
98 * the underlying audit code are noted in syslog.)
100 * Avoid calling adt_write_syslog for things that can happen
103 * syslog's open (openlog) and close (closelog) are interesting;
104 * openlog *may* create a file descriptor and is optional. closelog
105 * *will* close any open file descriptors and is also optional.
107 * Since syslog may also be used by the calling application, the
108 * choice is to avoid openlog, which sets some otherwise useful
109 * parameters, and to embed "Solaris_audit" in the log message.
113 adt_write_syslog(const char *message
, int err
)
115 int save_errno
= errno
;
118 DPRINTF(("syslog called: %s\n", message
));
120 mask_priority
= setlogmask(LOG_MASK(LOG_ALERT
));
122 syslog(LOG_ALERT
, "Solaris_audit %s: %m", message
);
123 (void) setlogmask(mask_priority
);
128 * return true if c2audit is not excluded.
130 * For purpose of this API, anything but AUC_DISABLED
131 * is enabled; however one never actually sees
132 * AUC_DISABLED since auditon returns ENOTSUP in that case. Any
133 * auditon error is considered the same as ENOTSUP for our
134 * purpose. auditstate is not changed by auditon if an error
139 * XXX this should probably be eliminated and adt_audit_state() replace it.
140 * All the legitimate uses are to not fork a waiting process for
141 * process exit processing, as in su, login, dtlogin. Other bogus
142 * users are zoneadmd and init.
143 * All but dtlogin are in ON, so we can do this without cross gate
146 * No longer used in adt.c.
150 adt_audit_enabled(void)
153 (void) auditon(A_GETCOND
, (caddr_t
)&auditstate
, sizeof (auditstate
));
155 return (auditstate
!= AUC_DISABLED
);
159 * See adt_audit_enabled() for state discussions.
160 * The state parameter is a hedge until all the uses become clear.
161 * Likely if adt_audit_enabled is brought internal to this file,
162 * it could be modified to take one or more parameters to describe the
167 adt_audit_state(int states
)
170 (void) auditon(A_GETCOND
, (caddr_t
)&auditstate
, sizeof (auditstate
));
172 return ((auditstate
& states
) ? B_TRUE
: B_FALSE
);
176 * Get user_specific/non-attributable audit mask. This may be called even when
181 adt_get_mask_from_user(uid_t uid
, au_mask_t
*mask
)
186 struct passwd
*result
;
189 if (auditstate
& AUC_DISABLED
) {
190 /* c2audit excluded */
191 mask
->am_success
= 0;
192 mask
->am_failure
= 0;
193 } else if (uid
<= MAXUID
) {
194 if ((buff_sz
= sysconf(_SC_GETPW_R_SIZE_MAX
)) == -1) {
195 adt_write_syslog("couldn't determine maximum size of "
196 "password buffer", errno
);
199 if ((pwd_buff
= calloc(1, (size_t)++buff_sz
)) == NULL
) {
202 getpwuid_r(uid
, &pwd
, pwd_buff
, (int)buff_sz
, &result
);
204 errno
= EINVAL
; /* user doesn't exist */
208 if (au_user_mask(pwd
.pw_name
, mask
)) {
210 errno
= EFAULT
; /* undetermined failure */
214 } else if (auditon(A_GETKMASK
, (caddr_t
)mask
, sizeof (*mask
)) == -1) {
222 * adt_get_unique_id -- generate a hopefully unique 32 bit value
224 * there will be a follow up to replace this with the use of /dev/random
226 * An MD5 hash is taken on a buffer of
227 * hostname . audit id . unix time . pid . count
229 * "count = noise++;" is subject to a race condition but I don't
230 * see a need to put a lock around it.
234 adt_get_unique_id(au_id_t uid
)
236 char hostname
[MAXHOSTNAMELEN
];
239 unsigned char obuff
[128/8];
243 static int noise
= 0;
246 time_t timebits
= time(NULL
);
247 pid_t pidbits
= getpid();
248 au_asid_t retval
= 0;
250 if (gethostname(hostname
, MAXHOSTNAMELEN
)) {
251 adt_write_syslog("gethostname call failed", errno
);
252 (void) strncpy(hostname
, "invalidHostName", MAXHOSTNAMELEN
);
255 while (retval
== 0) { /* 0 is the only invalid result */
258 MD5Update(&context
, (unsigned char *)hostname
,
259 (unsigned int) strlen((const char *)hostname
));
261 MD5Update(&context
, (unsigned char *) &uid
, sizeof (uid_t
));
264 (unsigned char *) &timebits
, sizeof (time_t));
266 MD5Update(&context
, (unsigned char *) &pidbits
,
269 MD5Update(&context
, (unsigned char *) &(count
), sizeof (int));
270 MD5Final(output
.obuff
, &context
);
272 retval
= output
.v
[count
% 4];
278 * the following "port" function deals with the following issues:
280 * 1 the kernel and ucred deal with a dev_t as a 64 bit value made
281 * up from a 32 bit major and 32 bit minor.
282 * 2 User space deals with a dev_t as either the above 64 bit value
283 * or a 32 bit value made from a 14 bit major and an 18 bit minor.
284 * 3 The various audit interfaces (except ucred) pass the 32 or
285 * 64 bit version depending the architecture of the userspace
286 * application. If you get a port value from ucred and pass it
287 * to the kernel via auditon(), it must be squeezed into a 32
288 * bit value because the kernel knows the userspace app's bit
291 * The internal state structure for adt (adt_internal_state_t) uses
292 * dev_t, so adt converts data from ucred to fit. The import/export
293 * functions, however, can't know if they are importing/exporting
294 * from 64 or 32 bit applications, so they always send 64 bits and
295 * the 32 bit end(s) are responsible to convert 32 -> 64 -> 32 as
300 * adt_cpy_tid() -- if lib is 64 bit, just copy it (dev_t and port are
301 * both 64 bits). If lib is 32 bits, squeeze the two-int port into
302 * a 32 bit dev_t. A port fits in the "minor" part of au_port_t,
303 * so it isn't broken up into pieces. (When it goes to the kernel
304 * and back, however, it will have been split into major/minor
309 adt_cpy_tid(au_tid_addr_t
*dest
, const au_tid64_addr_t
*src
)
312 (void) memcpy(dest
, src
, sizeof (au_tid_addr_t
));
314 dest
->at_type
= src
->at_type
;
316 dest
->at_port
= src
->at_port
.at_minor
& MAXMIN32
;
317 dest
->at_port
|= (src
->at_port
.at_major
& MAXMAJ32
) <<
320 (void) memcpy(dest
->at_addr
, src
->at_addr
, 4 * sizeof (uint32_t));
325 * adt_start_session -- create interface handle, create context
327 * The imported_state input is normally NULL, if not, it represents
328 * a continued session; its values obviate the need for a subsequent
329 * call to adt_set_user().
331 * The flag is used to decide how to set the initial state of the session.
332 * If 0, the session is "no audit" until a call to adt_set_user; if
333 * ADT_USE_PROC_DATA, the session is built from the process audit
334 * characteristics obtained from the kernel. If imported_state is
335 * not NULL, the resulting audit mask is an OR of the current process
336 * audit mask and that passed in.
338 * The basic model is that the caller can use the pointer returned
339 * by adt_start_session whether or not auditing is enabled or an
340 * error was returned. The functions that take the session handle
341 * as input generally return without doing anything if auditing is
346 adt_start_session(adt_session_data_t
**new_session
,
347 const adt_export_data_t
*imported_state
, adt_session_flags_t flags
)
349 adt_internal_state_t
*state
;
350 adt_session_flags_t flgmask
= ADT_FLAGS_ALL
;
352 /* test and set auditstate */
353 if (adt_audit_state(AUC_DISABLED
)) {
354 /* c2audit excluded */
359 if ((flags
& ~flgmask
) != 0) {
364 if ((state
= calloc(1, sizeof (adt_internal_state_t
))) == NULL
) {
368 if (adt_init(state
, flags
& ADT_USE_PROC_DATA
) != 0) {
369 goto return_err_free
; /* errno from adt_init() */
373 * The imported state overwrites the initial state if the
374 * imported state represents a valid audit trail
377 if (imported_state
!= NULL
) {
378 if (adt_import(state
, imported_state
) != 0) {
379 goto return_err_free
;
381 } else if (flags
& ADT_USE_PROC_DATA
) {
382 state
->as_session_model
= ADT_PROCESS_MODEL
;
384 state
->as_flags
= flags
;
385 DPRINTF(("(%lld) Starting session id = %08X\n",
386 (long long) getpid(), state
->as_info
.ai_asid
));
388 *new_session
= (adt_session_data_t
*)state
;
395 adt_write_syslog("audit session create failed", errno
);
402 * loads the event translation table into the audit session.
406 adt_load_table(const adt_session_data_t
*session_data
,
407 adt_translation_t
**xlate
, void (*preload
)(au_event_t
, adt_event_data_t
*))
409 adt_internal_state_t
*state
= (adt_internal_state_t
*)session_data
;
412 assert(state
->as_check
== ADT_VALID
);
413 state
->as_xlate
= xlate
;
414 state
->as_preload
= preload
;
419 * adt_get_asid() and adt_set_asid()
421 * if you use this interface, you are responsible to insure that the
422 * rest of the session data is populated correctly before calling
423 * adt_proccess_attr()
425 * neither of these are intended for general use and will likely
426 * remain private interfaces for a long time. Forever is a long
427 * time. In the case of adt_set_asid(), you should have a very,
428 * very good reason for setting your own session id. The process
429 * audit characteristics are not changed by put, use adt_set_proc().
431 * These are "volatile" (more changable than "evolving") and will
432 * probably change in the S10 period.
436 adt_get_asid(const adt_session_data_t
*session_data
, au_asid_t
*asid
)
439 if (session_data
== NULL
) {
442 assert(((adt_internal_state_t
*)session_data
)->as_check
==
445 *asid
= ((adt_internal_state_t
*)session_data
)->as_info
.ai_asid
;
450 adt_set_asid(const adt_session_data_t
*session_data
, const au_asid_t session_id
)
453 if (session_data
!= NULL
) {
454 assert(((adt_internal_state_t
*)session_data
)->as_check
==
457 ((adt_internal_state_t
*)session_data
)->as_have_user_data
|=
459 ((adt_internal_state_t
*)session_data
)->as_info
.ai_asid
=
465 * adt_get_auid() and adt_set_auid()
467 * neither of these are intended for general use and will likely
468 * remain private interfaces for a long time. Forever is a long
469 * time. In the case of adt_set_auid(), you should have a very,
470 * very good reason for setting your own audit id. The process
471 * audit characteristics are not changed by put, use adt_set_proc().
475 adt_get_auid(const adt_session_data_t
*session_data
, au_id_t
*auid
)
478 if (session_data
== NULL
) {
479 *auid
= AU_NOAUDITID
;
481 assert(((adt_internal_state_t
*)session_data
)->as_check
==
484 *auid
= ((adt_internal_state_t
*)session_data
)->as_info
.ai_auid
;
489 adt_set_auid(const adt_session_data_t
*session_data
, const au_id_t audit_id
)
492 if (session_data
!= NULL
) {
493 assert(((adt_internal_state_t
*)session_data
)->as_check
==
496 ((adt_internal_state_t
*)session_data
)->as_have_user_data
|=
498 ((adt_internal_state_t
*)session_data
)->as_info
.ai_auid
=
504 * adt_get_termid(), adt_set_termid()
506 * if you use this interface, you are responsible to insure that the
507 * rest of the session data is populated correctly before calling
508 * adt_proccess_attr()
510 * The process audit characteristics are not changed by put, use
515 adt_get_termid(const adt_session_data_t
*session_data
, au_tid_addr_t
*termid
)
518 if (session_data
== NULL
) {
519 (void) memset(termid
, 0, sizeof (au_tid_addr_t
));
520 termid
->at_type
= AU_IPv4
;
522 assert(((adt_internal_state_t
*)session_data
)->as_check
==
526 ((adt_internal_state_t
*)session_data
)->as_info
.ai_termid
;
531 adt_set_termid(const adt_session_data_t
*session_data
,
532 const au_tid_addr_t
*termid
)
535 if (session_data
!= NULL
) {
536 assert(((adt_internal_state_t
*)session_data
)->as_check
==
539 ((adt_internal_state_t
*)session_data
)->as_info
.ai_termid
=
542 ((adt_internal_state_t
*)session_data
)->as_have_user_data
|=
548 * adt_get_mask(), adt_set_mask()
550 * if you use this interface, you are responsible to insure that the
551 * rest of the session data is populated correctly before calling
552 * adt_proccess_attr()
554 * The process audit characteristics are not changed by put, use
559 adt_get_mask(const adt_session_data_t
*session_data
, au_mask_t
*mask
)
562 if (session_data
== NULL
) {
563 mask
->am_success
= 0;
564 mask
->am_failure
= 0;
566 assert(((adt_internal_state_t
*)session_data
)->as_check
==
569 *mask
= ((adt_internal_state_t
*)session_data
)->as_info
.ai_mask
;
574 adt_set_mask(const adt_session_data_t
*session_data
, const au_mask_t
*mask
)
577 if (session_data
!= NULL
) {
578 assert(((adt_internal_state_t
*)session_data
)->as_check
==
581 ((adt_internal_state_t
*)session_data
)->as_info
.ai_mask
= *mask
;
583 ((adt_internal_state_t
*)session_data
)->as_have_user_data
|=
589 * helpers for adt_load_termid
593 adt_do_ipv6_address(struct sockaddr_in6
*peer
, struct sockaddr_in6
*sock
,
594 au_tid_addr_t
*termid
)
597 termid
->at_port
= ((peer
->sin6_port
<<16) | (sock
->sin6_port
));
598 termid
->at_type
= AU_IPv6
;
599 (void) memcpy(termid
->at_addr
, &peer
->sin6_addr
, 4 * sizeof (uint_t
));
603 adt_do_ipv4_address(struct sockaddr_in
*peer
, struct sockaddr_in
*sock
,
604 au_tid_addr_t
*termid
)
607 termid
->at_port
= ((peer
->sin_port
<<16) | (sock
->sin_port
));
609 termid
->at_type
= AU_IPv4
;
610 termid
->at_addr
[0] = (uint32_t)peer
->sin_addr
.s_addr
;
611 (void) memset(&(termid
->at_addr
[1]), 0, 3 * sizeof (uint_t
));
615 * adt_load_termid: convenience function; inputs file handle and
616 * outputs an au_tid_addr struct.
618 * This code was stolen from audit_settid.c; it differs from audit_settid()
619 * in that it does not write the terminal id to the process.
623 adt_load_termid(int fd
, adt_termid_t
**termid
)
625 au_tid_addr_t
*p_term
;
626 struct sockaddr_in6 peer
;
627 struct sockaddr_in6 sock
;
628 int peerlen
= sizeof (peer
);
629 int socklen
= sizeof (sock
);
631 /* get peer name if its a socket, else assume local terminal */
633 if (getpeername(fd
, (struct sockaddr
*)&peer
, (socklen_t
*)&peerlen
)
635 if (errno
== ENOTSOCK
) {
636 return (adt_load_hostname(NULL
, termid
));
641 if ((p_term
= calloc(1, sizeof (au_tid_addr_t
))) == NULL
) {
646 if (getsockname(fd
, (struct sockaddr
*)&sock
,
647 (socklen_t
*)&socklen
) < 0) {
648 goto return_err_free
;
651 if (peer
.sin6_family
== AF_INET6
) {
652 adt_do_ipv6_address(&peer
, &sock
, p_term
);
654 adt_do_ipv4_address((struct sockaddr_in
*)&peer
,
655 (struct sockaddr_in
*)&sock
, p_term
);
657 *termid
= (adt_termid_t
*)p_term
;
669 adt_have_termid(au_tid_addr_t
*dest
)
671 struct auditinfo_addr audit_data
;
673 if (getaudit_addr(&audit_data
, sizeof (audit_data
)) < 0) {
674 adt_write_syslog("getaudit failed", errno
);
678 if ((audit_data
.ai_termid
.at_type
== 0) ||
679 (audit_data
.ai_termid
.at_addr
[0] |
680 audit_data
.ai_termid
.at_addr
[1] |
681 audit_data
.ai_termid
.at_addr
[2] |
682 audit_data
.ai_termid
.at_addr
[3]) == 0)
685 (void) memcpy(dest
, &(audit_data
.ai_termid
),
686 sizeof (au_tid_addr_t
));
692 * adt_get_hostIP - construct a terminal id from a hostname
694 * Returns 0 = success
695 * -1 = failure and errno = ENETDOWN with the address
696 * defaulted to IPv4 loopback.
700 adt_get_hostIP(const char *hostname
, au_tid_addr_t
*p_term
)
702 struct addrinfo
*ai
= NULL
;
707 while ((tries
-- > 0) &&
708 ((eai_err
= getaddrinfo(hostname
, NULL
, NULL
, &ai
)) != 0)) {
710 * getaddrinfo returns its own set of errors.
711 * Log them here, so any subsequent syslogs will
712 * have a context. adt_get_hostIP callers can only
713 * return errno, so subsequent syslogs may be lacking
714 * that getaddrinfo failed.
716 (void) snprintf(msg
, sizeof (msg
), "getaddrinfo(%s) "
717 "failed[%s]", hostname
, gai_strerror(eai_err
));
718 adt_write_syslog(msg
, 0);
720 if (eai_err
!= EAI_AGAIN
) {
724 /* see if resolution becomes available */
728 if (ai
->ai_family
== AF_INET
) {
729 p_term
->at_type
= AU_IPv4
;
730 (void) memcpy(p_term
->at_addr
,
732 &((struct sockaddr_in
*)ai
->ai_addr
)->sin_addr
,
735 p_term
->at_type
= AU_IPv6
;
736 (void) memcpy(p_term
->at_addr
,
738 &((struct sockaddr_in6
*)ai
->ai_addr
)->sin6_addr
,
743 } else if (auditstate
& (AUC_AUDITING
| AUC_NOSPACE
)) {
744 auditinfo_addr_t audit_info
;
747 * auditd is running so there should be a
748 * kernel audit context
750 if (auditon(A_GETKAUDIT
, (caddr_t
)&audit_info
,
751 sizeof (audit_info
)) < 0) {
752 adt_write_syslog("unable to get kernel audit context",
756 adt_write_syslog("setting Audit IP address to kernel", 0);
757 *p_term
= audit_info
.ai_termid
;
762 struct ifaddrlist al
;
764 char ntop
[INET6_ADDRSTRLEN
];
767 * getaddrinfo has failed to map the hostname
768 * to an IP address, try to get an IP address
769 * from a local interface. If none up, default
773 if (adt_get_local_address(family
, &al
) != 0) {
776 if (adt_get_local_address(family
, &al
) != 0) {
777 adt_write_syslog("adt_get_local_address "
778 "failed, no Audit IP address available, "
779 "faking loopback and error",
781 IN_SET_LOOPBACK_ADDR(
782 (struct sockaddr_in
*)&(al
.addr
.addr
));
783 (void) memcpy(p_term
->at_addr
, &al
.addr
.addr
,
785 p_term
->at_type
= AU_IPv4
;
789 if (family
== AF_INET
) {
790 p_term
->at_type
= AU_IPv4
;
791 (void) memcpy(p_term
->at_addr
, &al
.addr
.addr
, AU_IPv4
);
793 p_term
->at_type
= AU_IPv6
;
794 (void) memcpy(p_term
->at_addr
, &al
.addr
.addr6
, AU_IPv6
);
797 (void) snprintf(msg
, sizeof (msg
), "mapping %s to %s",
798 hostname
, inet_ntop(family
, &(al
.addr
), ntop
,
800 adt_write_syslog(msg
, 0);
806 * adt_load_hostname() is called when the caller does not have a file
807 * handle that gives access to the socket info or any other way to
808 * pass in both port and ip address. The hostname input is ignored if
809 * the terminal id has already been set; instead it returns the
810 * existing terminal id.
812 * If c2audit is excluded, success is returned.
813 * If the hostname lookup fails, the loopback address is assumed,
814 * errno is set to ENETDOWN, this allows the caller to interpret
815 * whether failure is fatal, and if not to have a address for the
817 * Otherwise the caller would need to be aware of the audit state.
819 * Other errors are ignored if not auditing.
823 adt_load_hostname(const char *hostname
, adt_termid_t
**termid
)
825 char localhost
[MAXHOSTNAMELEN
+ 1];
826 au_tid_addr_t
*p_term
;
828 if (adt_audit_state(AUC_DISABLED
)) {
829 /* c2audit excluded */
834 if ((p_term
= calloc(1, sizeof (au_tid_addr_t
))) == NULL
) {
838 if (adt_have_termid(p_term
)) {
839 *termid
= (adt_termid_t
*)p_term
;
844 if (hostname
== NULL
|| *hostname
== '\0') {
845 (void) sysinfo(SI_HOSTNAME
, localhost
, MAXHOSTNAMELEN
);
846 hostname
= localhost
;
848 if (adt_get_hostIP(hostname
, p_term
) == 0) {
849 *termid
= (adt_termid_t
*)p_term
;
852 *termid
= (adt_termid_t
*)p_term
;
858 if (auditstate
& AUC_NOAUDIT
) {
866 * adt_load_ttyname() is called when the caller does not have a file
867 * handle that gives access to the local terminal or any other way
868 * of determining the device id. The ttyname input is ignored if
869 * the terminal id has already been set; instead it returns the
870 * existing terminal id.
872 * If c2audit is excluded, success is returned.
873 * The local hostname is used for the local IP address.
874 * If that hostname lookup fails, the loopback address is assumed,
875 * errno is set to ENETDOWN, this allows the caller to interpret
876 * whether failure is fatal, and if not to have a address for the
878 * Otherwise the caller would need to be aware of the audit state.
880 * Other errors are ignored if not auditing.
884 adt_load_ttyname(const char *ttyname
, adt_termid_t
**termid
)
886 char localhost
[MAXHOSTNAMELEN
+ 1];
887 au_tid_addr_t
*p_term
;
888 struct stat stat_buf
;
890 if (adt_audit_state(AUC_DISABLED
)) {
891 /* c2audit excluded */
896 if ((p_term
= calloc(1, sizeof (au_tid_addr_t
))) == NULL
) {
900 if (adt_have_termid(p_term
)) {
901 *termid
= (adt_termid_t
*)p_term
;
907 if (sysinfo(SI_HOSTNAME
, localhost
, MAXHOSTNAMELEN
) < 0) {
908 goto return_err_free
; /* errno from sysinfo */
911 if (ttyname
!= NULL
&& *ttyname
!= '\0') {
912 if (stat(ttyname
, &stat_buf
) < 0) {
913 goto return_err_free
;
916 p_term
->at_port
= stat_buf
.st_rdev
;
919 if (adt_get_hostIP(localhost
, p_term
) == 0) {
920 *termid
= (adt_termid_t
*)p_term
;
923 *termid
= (adt_termid_t
*)p_term
;
932 if (auditstate
& AUC_NOAUDIT
) {
940 * adt_get_session_id returns a stringified representation of
941 * the audit session id. See also adt_get_asid() for how to
942 * get the unexpurgated version. No guarantees as to how long
943 * the returned string will be or its general form; hex for now.
945 * An empty string is returned if auditing is off; length = 1
946 * and the pointer is valid.
948 * returns strlen + 1 if buffer is valid; else 0 and errno.
952 adt_get_session_id(const adt_session_data_t
*session_data
, char **buff
)
954 au_asid_t session_id
;
957 * output is 0x followed by
958 * two characters per byte
960 * except leading 0's are suppressed, so a few bytes may
963 length
= 2 + (2 * sizeof (session_id
)) + 1;
964 *buff
= malloc(length
);
969 if (session_data
== NULL
) { /* NULL is not an error */
973 adt_get_asid(session_data
, &session_id
);
975 length
= snprintf(*buff
, length
, "0x%X", (int)session_id
);
977 /* length < 1 is a bug: the session data type may have changed */
984 * adt_end_session -- close handle, clear context
986 * if as_check is invalid, no harm, no foul, EXCEPT that this could
987 * be an attempt to free data already free'd, so output to syslog
988 * to help explain why the process cored dumped.
992 adt_end_session(adt_session_data_t
*session_data
)
994 adt_internal_state_t
*state
;
996 if (session_data
!= NULL
) {
997 state
= (adt_internal_state_t
*)session_data
;
998 if (state
->as_check
!= ADT_VALID
) {
999 adt_write_syslog("freeing invalid data", EINVAL
);
1001 state
->as_check
= 0;
1005 /* no errors yet defined */
1010 * adt_dup_session -- copy the session data
1014 adt_dup_session(const adt_session_data_t
*source
, adt_session_data_t
**dest
)
1016 adt_internal_state_t
*source_state
;
1017 adt_internal_state_t
*dest_state
= NULL
;
1020 if (source
!= NULL
) {
1021 source_state
= (adt_internal_state_t
*)source
;
1022 assert(source_state
->as_check
== ADT_VALID
);
1024 dest_state
= malloc(sizeof (adt_internal_state_t
));
1025 if (dest_state
== NULL
) {
1029 (void) memcpy(dest_state
, source
,
1030 sizeof (struct adt_internal_state
));
1033 *dest
= (adt_session_data_t
*)dest_state
;
1038 * from_export_format()
1039 * read from a network order buffer into struct adt_session_data
1043 adt_from_export_format(adt_internal_state_t
*internal
,
1044 const adt_export_data_t
*external
)
1046 struct export_header head
;
1047 struct export_link link
;
1052 char *p
= (char *)external
;
1054 adrm_start(&context
, (char *)external
);
1055 adrm_int32(&context
, (int *)&head
, 4);
1057 if ((internal
->as_check
= head
.ax_check
) != ADT_VALID
) {
1061 offset
= head
.ax_link
.ax_offset
;
1062 version
= head
.ax_link
.ax_version
;
1063 length
= head
.ax_buffer_length
;
1066 * Skip newer versions.
1068 while (version
> PROTOCOL_VERSION_2
) {
1070 return (0); /* failed to match version */
1072 p
+= offset
; /* point to next version # */
1074 if (p
> (char *)external
+ length
) {
1077 adrm_start(&context
, p
);
1078 adrm_int32(&context
, (int *)&link
, 2);
1079 offset
= link
.ax_offset
;
1080 version
= link
.ax_version
;
1081 assert(version
!= 0);
1084 * Adjust buffer pointer to the first data item (euid).
1086 if (p
== (char *)external
) {
1087 adrm_start(&context
, (char *)(p
+ sizeof (head
)));
1089 adrm_start(&context
, (char *)(p
+ sizeof (link
)));
1092 * if down rev version, pid is not included
1094 if (version
== PROTOCOL_VERSION_1
) {
1095 adrm_int32(&context
, (int *)&(internal
->as_euid
), 1);
1096 adrm_int32(&context
, (int *)&(internal
->as_ruid
), 1);
1097 adrm_int32(&context
, (int *)&(internal
->as_egid
), 1);
1098 adrm_int32(&context
, (int *)&(internal
->as_rgid
), 1);
1099 adrm_int32(&context
, (int *)&(internal
->as_info
.ai_auid
), 1);
1100 adrm_int32(&context
,
1101 (int *)&(internal
->as_info
.ai_mask
.am_success
), 2);
1102 adrm_int32(&context
,
1103 (int *)&(internal
->as_info
.ai_termid
.at_port
), 1);
1104 adrm_int32(&context
,
1105 (int *)&(internal
->as_info
.ai_termid
.at_type
), 1);
1106 adrm_int32(&context
,
1107 (int *)&(internal
->as_info
.ai_termid
.at_addr
[0]), 4);
1108 adrm_int32(&context
, (int *)&(internal
->as_info
.ai_asid
), 1);
1109 adrm_int32(&context
, (int *)&(internal
->as_audit_state
), 1);
1110 internal
->as_pid
= (pid_t
)-1;
1111 } else if (version
== PROTOCOL_VERSION_2
) {
1112 adrm_int32(&context
, (int *)&(internal
->as_euid
), 1);
1113 adrm_int32(&context
, (int *)&(internal
->as_ruid
), 1);
1114 adrm_int32(&context
, (int *)&(internal
->as_egid
), 1);
1115 adrm_int32(&context
, (int *)&(internal
->as_rgid
), 1);
1116 adrm_int32(&context
, (int *)&(internal
->as_info
.ai_auid
), 1);
1117 adrm_int32(&context
,
1118 (int *)&(internal
->as_info
.ai_mask
.am_success
), 2);
1119 adrm_int32(&context
,
1120 (int *)&(internal
->as_info
.ai_termid
.at_port
), 1);
1121 adrm_int32(&context
,
1122 (int *)&(internal
->as_info
.ai_termid
.at_type
), 1);
1123 adrm_int32(&context
,
1124 (int *)&(internal
->as_info
.ai_termid
.at_addr
[0]), 4);
1125 adrm_int32(&context
, (int *)&(internal
->as_info
.ai_asid
), 1);
1126 adrm_int32(&context
, (int *)&(internal
->as_audit_state
), 1);
1127 adrm_int32(&context
, (int *)&(internal
->as_pid
), 1);
1134 * adt_to_export_format
1135 * read from struct adt_session_data into a network order buffer.
1137 * (network order 'cause this data may be shared with a remote host.)
1141 adt_to_export_format(adt_export_data_t
*external
,
1142 adt_internal_state_t
*internal
)
1144 struct export_header head
;
1145 struct export_link tail
;
1148 adrm_start(&context
, (char *)external
);
1150 head
.ax_check
= ADT_VALID
;
1151 head
.ax_buffer_length
= sizeof (struct adt_export_data
);
1153 /* version 2 first */
1155 head
.ax_link
.ax_version
= PROTOCOL_VERSION_2
;
1156 head
.ax_link
.ax_offset
= sizeof (struct export_header
) +
1157 sizeof (struct adt_export_v2
);
1159 adrm_putint32(&context
, (int *)&head
, 4);
1161 adrm_putint32(&context
, (int *)&(internal
->as_euid
), 1);
1162 adrm_putint32(&context
, (int *)&(internal
->as_ruid
), 1);
1163 adrm_putint32(&context
, (int *)&(internal
->as_egid
), 1);
1164 adrm_putint32(&context
, (int *)&(internal
->as_rgid
), 1);
1165 adrm_putint32(&context
, (int *)&(internal
->as_info
.ai_auid
), 1);
1166 adrm_putint32(&context
,
1167 (int *)&(internal
->as_info
.ai_mask
.am_success
), 2);
1168 adrm_putint32(&context
,
1169 (int *)&(internal
->as_info
.ai_termid
.at_port
), 1);
1170 adrm_putint32(&context
,
1171 (int *)&(internal
->as_info
.ai_termid
.at_type
), 1);
1172 adrm_putint32(&context
,
1173 (int *)&(internal
->as_info
.ai_termid
.at_addr
[0]), 4);
1174 adrm_putint32(&context
, (int *)&(internal
->as_info
.ai_asid
), 1);
1175 adrm_putint32(&context
, (int *)&(internal
->as_audit_state
), 1);
1176 adrm_putint32(&context
, (int *)&(internal
->as_pid
), 1);
1180 tail
.ax_version
= PROTOCOL_VERSION_1
;
1183 adrm_putint32(&context
, (int *)&tail
, 2);
1185 adrm_putint32(&context
, (int *)&(internal
->as_euid
), 1);
1186 adrm_putint32(&context
, (int *)&(internal
->as_ruid
), 1);
1187 adrm_putint32(&context
, (int *)&(internal
->as_egid
), 1);
1188 adrm_putint32(&context
, (int *)&(internal
->as_rgid
), 1);
1189 adrm_putint32(&context
, (int *)&(internal
->as_info
.ai_auid
), 1);
1190 adrm_putint32(&context
,
1191 (int *)&(internal
->as_info
.ai_mask
.am_success
), 2);
1192 adrm_putint32(&context
,
1193 (int *)&(internal
->as_info
.ai_termid
.at_port
), 1);
1194 adrm_putint32(&context
,
1195 (int *)&(internal
->as_info
.ai_termid
.at_type
), 1);
1196 adrm_putint32(&context
,
1197 (int *)&(internal
->as_info
.ai_termid
.at_addr
[0]), 4);
1198 adrm_putint32(&context
, (int *)&(internal
->as_info
.ai_asid
), 1);
1199 adrm_putint32(&context
, (int *)&(internal
->as_audit_state
), 1);
1201 /* finally terminator */
1203 tail
.ax_version
= 0; /* invalid version number */
1206 adrm_putint32(&context
, (int *)&tail
, 2);
1208 return (head
.ax_buffer_length
);
1212 * adt_import() -- convert from network order to machine-specific order
1216 adt_import(adt_internal_state_t
*internal
, const adt_export_data_t
*external
)
1220 /* save local audit state */
1221 int local_audit_state
= internal
->as_audit_state
;
1223 if (adt_from_export_format(internal
, external
) < 1)
1224 return (-1); /* errno from adt_from_export_format */
1227 * If audit isn't enabled on the remote, they were unable
1228 * to generate the audit mask, so generate it based on
1229 * local configuration. If the user id has changed, the
1230 * resulting mask may miss some subtleties that occurred
1231 * on the remote system.
1233 * If the remote failed to generate a terminal id, it is not
1237 if (!(internal
->as_audit_state
& AUC_DISABLED
)) {
1238 if (adt_get_mask_from_user(internal
->as_info
.ai_auid
,
1239 &(internal
->as_info
.ai_mask
)))
1241 if (internal
->as_info
.ai_auid
!= internal
->as_ruid
) {
1242 if (adt_get_mask_from_user(internal
->as_info
.ai_auid
,
1245 internal
->as_info
.ai_mask
.am_success
|=
1247 internal
->as_info
.ai_mask
.am_failure
|=
1251 internal
->as_audit_state
= local_audit_state
;
1253 DPRINTF(("(%lld)imported asid = %X %u\n", (long long) getpid(),
1254 internal
->as_info
.ai_asid
,
1255 internal
->as_info
.ai_asid
));
1257 internal
->as_have_user_data
= ADT_HAVE_ALL
;
1263 * adt_export_session_data()
1264 * copies a adt_session_data struct into a network order buffer
1266 * In a misconfigured network, the local host may have auditing
1267 * off while the destination may have auditing on, so if there
1268 * is sufficient memory, a buffer will be returned even in the
1273 adt_export_session_data(const adt_session_data_t
*internal
,
1274 adt_export_data_t
**external
)
1276 size32_t length
= 0;
1278 *external
= malloc(sizeof (adt_export_data_t
));
1280 if (*external
== NULL
)
1283 if (internal
== NULL
) {
1284 adt_internal_state_t
*dummy
;
1286 dummy
= malloc(sizeof (adt_internal_state_t
));
1288 goto return_length_free
;
1290 if (adt_init(dummy
, 0)) { /* 0 == don't copy from proc */
1292 goto return_length_free
;
1294 length
= adt_to_export_format(*external
, dummy
);
1297 length
= adt_to_export_format(*external
,
1298 (adt_internal_state_t
*)internal
);
1309 adt_setto_unaudited(adt_internal_state_t
*state
)
1311 if (state
->as_audit_state
& AUC_DISABLED
) {
1312 state
->as_ruid
= AU_NOAUDITID
;
1313 state
->as_euid
= AU_NOAUDITID
;
1314 state
->as_rgid
= AU_NOAUDITID
;
1315 state
->as_egid
= AU_NOAUDITID
;
1316 state
->as_pid
= (pid_t
)-1;
1318 state
->as_info
.ai_asid
= 0;
1319 state
->as_info
.ai_auid
= AU_NOAUDITID
;
1321 (void) memset(&(state
->as_info
.ai_termid
), 0,
1322 sizeof (au_tid_addr_t
));
1323 state
->as_info
.ai_termid
.at_type
= AU_IPv4
;
1325 (void) memset(&(state
->as_info
.ai_mask
), 0,
1326 sizeof (au_mask_t
));
1327 state
->as_have_user_data
= 0;
1332 * adt_init -- set session context by copying the audit characteristics
1333 * from the proc and picking up current uid/tid information.
1335 * By default, an audit session is based on the process; the default
1336 * is overriden by adt_set_user()
1340 adt_init(adt_internal_state_t
*state
, int use_proc_data
)
1342 /* ensure auditstate is set */
1344 (void) adt_audit_state(0);
1345 state
->as_audit_state
= auditstate
;
1347 if (use_proc_data
) {
1348 state
->as_ruid
= getuid();
1349 state
->as_euid
= geteuid();
1350 state
->as_rgid
= getgid();
1351 state
->as_egid
= getegid();
1352 state
->as_pid
= getpid();
1354 if (!(state
->as_audit_state
& AUC_DISABLED
)) {
1355 const au_tid64_addr_t
*tid
;
1356 const au_mask_t
*mask
;
1357 ucred_t
*ucred
= ucred_get(P_MYID
);
1360 * Even if the ucred is NULL, the underlying
1361 * credential may have a valid terminal id; if the
1362 * terminal id is set, then that's good enough. An
1363 * example of where this matters is failed login,
1364 * where rlogin/telnet sets the terminal id before
1365 * calling login; login does not load the credential
1366 * since auth failed.
1368 if (ucred
== NULL
) {
1369 if (!adt_have_termid(
1370 &(state
->as_info
.ai_termid
)))
1373 mask
= ucred_getamask(ucred
);
1375 state
->as_info
.ai_mask
= *mask
;
1380 tid
= ucred_getatid(ucred
);
1382 adt_cpy_tid(&(state
->as_info
.ai_termid
),
1388 state
->as_info
.ai_asid
= ucred_getasid(ucred
);
1389 state
->as_info
.ai_auid
= ucred_getauid(ucred
);
1392 state
->as_have_user_data
= ADT_HAVE_ALL
;
1395 adt_setto_unaudited(state
);
1397 state
->as_session_model
= ADT_SESSION_MODEL
; /* default */
1399 if ((state
->as_audit_state
& (AUC_AUDITING
| AUC_NOSPACE
)) &&
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 */
1404 state
->as_check
= ADT_VALID
;
1405 adt_load_table((adt_session_data_t
*)state
, &adt_xlate_table
[0],
1413 * Copy the current session state to the process. If this function
1414 * is called, the model becomes a process model rather than a
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.
1424 adt_set_proc(const adt_session_data_t
*session_data
)
1426 adt_internal_state_t
*state
;
1428 if (session_data
== NULL
) {
1432 state
= (adt_internal_state_t
*)session_data
;
1434 assert(state
->as_check
== ADT_VALID
);
1436 if ((state
->as_have_user_data
& (ADT_HAVE_ALL
& ~ADT_HAVE_IDS
)) !=
1437 (ADT_HAVE_ALL
& ~ADT_HAVE_IDS
)) {
1442 if (setaudit_addr((auditinfo_addr_t
*)&(state
->as_info
),
1443 sizeof (auditinfo_addr_t
)) < 0) {
1444 goto return_err
; /* errno set by setaudit_addr() */
1447 state
->as_session_model
= ADT_PROCESS_MODEL
;
1452 adt_write_syslog("failed to set process audit characteristics", errno
);
1457 adt_newuser(adt_internal_state_t
*state
, uid_t ruid
, au_tid_addr_t
*termid
)
1459 au_tid_addr_t no_tid
= {0, AU_IPv4
, 0, 0, 0, 0};
1460 au_mask_t no_mask
= {0, 0};
1462 if (ruid
== ADT_NO_AUDIT
) {
1463 state
->as_info
.ai_auid
= AU_NOAUDITID
;
1464 state
->as_info
.ai_asid
= 0;
1465 state
->as_info
.ai_termid
= no_tid
;
1466 state
->as_info
.ai_mask
= no_mask
;
1469 state
->as_info
.ai_auid
= ruid
;
1470 state
->as_info
.ai_asid
= adt_get_unique_id(ruid
);
1472 state
->as_info
.ai_termid
= *termid
;
1474 if (adt_get_mask_from_user(ruid
, &(state
->as_info
.ai_mask
)))
1477 /* Assume intending to audit as this process */
1479 if (state
->as_pid
== (pid_t
)-1)
1480 state
->as_pid
= getpid();
1486 adt_changeuser(adt_internal_state_t
*state
, uid_t ruid
)
1490 if (!(state
->as_have_user_data
& ADT_HAVE_AUID
))
1491 state
->as_info
.ai_auid
= ruid
;
1492 if (!(state
->as_have_user_data
& ADT_HAVE_ASID
))
1493 state
->as_info
.ai_asid
= adt_get_unique_id(ruid
);
1495 if (ruid
<= MAXEPHUID
) {
1496 if (adt_get_mask_from_user(ruid
, &mask
))
1499 state
->as_info
.ai_mask
.am_success
|= mask
.am_success
;
1500 state
->as_info
.ai_mask
.am_failure
|= mask
.am_failure
;
1502 DPRINTF(("changed mask to %08X/%08X for ruid=%d\n",
1503 state
->as_info
.ai_mask
.am_success
,
1504 state
->as_info
.ai_mask
.am_failure
,
1510 * adt_set_user -- see also adt_set_from_ucred()
1512 * ADT_NO_ATTRIB is a valid uid/gid meaning "not known" or
1513 * "unattributed." If ruid, change the model to session.
1515 * ADT_NO_CHANGE is a valid uid/gid meaning "do not change this value"
1516 * only valid with ADT_UPDATE.
1518 * ADT_NO_AUDIT is the external equivalent to AU_NOAUDITID -- there
1519 * isn't a good reason to call adt_set_user() with it unless you don't
1520 * have a good value yet and intend to replace it later; auid will be
1523 * adt_set_user should be called even if auditing is not enabled
1524 * so that adt_export_session_data() will have useful stuff to
1527 * See the note preceding adt_set_proc() about the use of ADT_HAVE_TID
1532 adt_set_user(const adt_session_data_t
*session_data
, uid_t euid
, gid_t egid
,
1533 uid_t ruid
, gid_t rgid
, const adt_termid_t
*termid
,
1534 enum adt_user_context user_context
)
1536 adt_internal_state_t
*state
;
1539 if (session_data
== NULL
) /* no session exists to audit */
1542 state
= (adt_internal_state_t
*)session_data
;
1543 assert(state
->as_check
== ADT_VALID
);
1545 switch (user_context
) {
1547 if (ruid
== ADT_NO_CHANGE
|| euid
== ADT_NO_CHANGE
||
1548 rgid
== ADT_NO_CHANGE
|| egid
== ADT_NO_CHANGE
) {
1552 if ((rc
= adt_newuser(state
, ruid
,
1553 (au_tid_addr_t
*)termid
)) != 0)
1556 state
->as_have_user_data
= ADT_HAVE_ALL
;
1559 if (state
->as_have_user_data
!= ADT_HAVE_ALL
) {
1564 if (ruid
!= ADT_NO_CHANGE
)
1565 if ((rc
= adt_changeuser(state
, ruid
)) != 0)
1569 if (state
->as_have_user_data
!= ADT_HAVE_ALL
) {
1575 assert(termid
!= NULL
);
1576 state
->as_info
.ai_termid
= *((au_tid_addr_t
*)termid
);
1577 /* avoid fooling pam_setcred()... */
1578 state
->as_info
.ai_auid
= AU_NOAUDITID
;
1579 state
->as_info
.ai_asid
= 0;
1580 state
->as_info
.ai_mask
.am_failure
= 0;
1581 state
->as_info
.ai_mask
.am_success
= 0;
1582 state
->as_have_user_data
= ADT_HAVE_TID
|
1583 ADT_HAVE_AUID
| ADT_HAVE_ASID
| ADT_HAVE_MASK
;
1590 if (ruid
== ADT_NO_AUDIT
) {
1591 state
->as_ruid
= AU_NOAUDITID
;
1592 state
->as_euid
= AU_NOAUDITID
;
1593 state
->as_rgid
= AU_NOAUDITID
;
1594 state
->as_egid
= AU_NOAUDITID
;
1596 if (ruid
!= ADT_NO_CHANGE
)
1597 state
->as_ruid
= ruid
;
1598 if (euid
!= ADT_NO_CHANGE
)
1599 state
->as_euid
= euid
;
1600 if (rgid
!= ADT_NO_CHANGE
)
1601 state
->as_rgid
= rgid
;
1602 if (egid
!= ADT_NO_CHANGE
)
1603 state
->as_egid
= egid
;
1606 if (ruid
== ADT_NO_ATTRIB
) {
1607 state
->as_session_model
= ADT_SESSION_MODEL
;
1614 * adt_set_from_ucred()
1616 * an alternate to adt_set_user that fills the same role but uses
1617 * a pointer to a ucred rather than a list of id's. If the ucred
1618 * pointer is NULL, use the credential from the this process.
1620 * A key difference is that for ADT_NEW, adt_set_from_ucred() does
1621 * not overwrite the asid and auid unless auid has not been set.
1622 * ADT_NEW differs from ADT_UPDATE in that it does not OR together
1623 * the incoming audit mask with the one that already exists.
1625 * adt_set_from_ucred should be called even if auditing is not enabled
1626 * so that adt_export_session_data() will have useful stuff to
1631 adt_set_from_ucred(const adt_session_data_t
*session_data
, const ucred_t
*uc
,
1632 enum adt_user_context user_context
)
1634 adt_internal_state_t
*state
;
1636 const au_tid64_addr_t
*tid64
;
1637 au_tid_addr_t termid
, *tid
;
1638 ucred_t
*ucred
= (ucred_t
*)uc
;
1639 boolean_t local_uc
= B_FALSE
;
1641 if (session_data
== NULL
) /* no session exists to audit */
1644 state
= (adt_internal_state_t
*)session_data
;
1645 assert(state
->as_check
== ADT_VALID
);
1647 if (ucred
== NULL
) {
1648 ucred
= ucred_get(P_MYID
);
1655 switch (user_context
) {
1657 tid64
= ucred_getatid(ucred
);
1658 if (tid64
!= NULL
) {
1659 adt_cpy_tid(&termid
, tid64
);
1664 if (ucred_getauid(ucred
) == AU_NOAUDITID
) {
1665 adt_setto_unaudited(state
);
1666 state
->as_have_user_data
= ADT_HAVE_ALL
;
1670 state
->as_info
.ai_auid
= ucred_getauid(ucred
);
1671 state
->as_info
.ai_asid
= ucred_getasid(ucred
);
1672 state
->as_info
.ai_mask
= *ucred_getamask(ucred
);
1673 state
->as_info
.ai_termid
= *tid
;
1675 state
->as_have_user_data
= ADT_HAVE_ALL
;
1678 if (state
->as_have_user_data
!= ADT_HAVE_ALL
) {
1683 if ((rc
= adt_changeuser(state
, ucred_getruid(ucred
))) != 0)
1687 if (state
->as_have_user_data
!= ADT_HAVE_ALL
) {
1698 state
->as_ruid
= ucred_getruid(ucred
);
1699 state
->as_euid
= ucred_geteuid(ucred
);
1700 state
->as_rgid
= ucred_getrgid(ucred
);
1701 state
->as_egid
= ucred_getegid(ucred
);
1702 state
->as_pid
= ucred_getpid(ucred
);
1712 * adt_alloc_event() returns a pointer to allocated memory
1717 *adt_alloc_event(const adt_session_data_t
*session_data
, au_event_t event_id
)
1719 struct adt_event_state
*event_state
;
1720 adt_internal_state_t
*session_state
;
1721 adt_event_data_t
*return_event
= NULL
;
1723 * need to return a valid event pointer even if audit is
1724 * off, else the caller will end up either (1) keeping its
1725 * own flags for on/off or (2) writing to a NULL pointer.
1726 * If auditing is on, the session data must be valid; otherwise
1729 if (session_data
!= NULL
) {
1730 session_state
= (adt_internal_state_t
*)session_data
;
1731 assert(session_state
->as_check
== ADT_VALID
);
1733 event_state
= calloc(1, sizeof (struct adt_event_state
));
1734 if (event_state
== NULL
)
1737 event_state
->ae_check
= ADT_VALID
;
1739 event_state
->ae_event_id
= event_id
;
1740 event_state
->ae_session
= (struct adt_internal_state
*)session_data
;
1742 return_event
= (adt_event_data_t
*)&(event_state
->ae_event_data
);
1745 * preload data so the adt_au_*() functions can detect un-supplied
1746 * values (0 and NULL are free via calloc()).
1748 if (session_data
!= NULL
) {
1749 session_state
->as_preload(event_id
, return_event
);
1753 return (return_event
);
1757 * adt_getXlateTable -- look up translation table address for event id
1760 static adt_translation_t
*
1761 adt_getXlateTable(adt_translation_t
**xlate
, au_event_t event_id
)
1763 /* xlate_table is global in adt_xlate.c */
1764 adt_translation_t
**p_xlate
= xlate
;
1765 adt_translation_t
*p_event
;
1767 while (*p_xlate
!= NULL
) {
1769 if (event_id
== p_event
->tx_external_event
)
1779 * the call to this function is surrounded by a mutex.
1781 * i walks down the table picking up next_token. j walks again to
1782 * calculate the offset to the input data. k points to the next
1783 * token's row. Finally, l, is used to sum the values in the
1786 * What's going on? The entry array is in the order of the input
1787 * fields but the processing of array entries is in the order of
1788 * the output (see next_token). Calculating the offset to the
1789 * "next" input can't be done in the outer loop (i) since i doesn't
1790 * point to the current entry and it can't be done with the k index
1791 * because it doesn't represent the order of input fields.
1793 * While the resulting algorithm is n**2, it is only done once per
1798 * adt_calcOffsets is only called once per event type, but it uses
1799 * the address alignment of memory allocated for that event as if it
1800 * were the same for all subsequently allocated memory. This is
1801 * guaranteed by calloc/malloc. Arrays take special handling since
1802 * what matters for figuring out the correct alignment is the size
1803 * of the array element.
1807 adt_calcOffsets(struct entry
*p_entry
, int tablesize
, void *p_data
)
1810 size_t this_size
, prev_size
;
1811 void *struct_start
= p_data
;
1813 for (i
= 0; i
< tablesize
; i
++) {
1814 if (p_entry
[i
].en_type_def
== NULL
) {
1815 p_entry
[i
].en_offset
= 0;
1819 p_entry
[i
].en_offset
= (char *)p_data
- (char *)struct_start
;
1821 for (j
= 0; j
< p_entry
[i
].en_count_types
; j
++) {
1822 if (p_entry
[i
].en_type_def
[j
].dd_datatype
== ADT_MSG
)
1823 this_size
= sizeof (enum adt_generic
);
1826 p_entry
[i
].en_type_def
[j
].dd_input_size
;
1828 /* adj for first entry */
1830 prev_size
= this_size
;
1832 if (p_entry
[i
].en_type_def
[j
].dd_datatype
==
1834 p_data
= (char *)adt_adjust_address(p_data
,
1835 prev_size
, sizeof (uint32_t)) +
1836 this_size
- sizeof (uint32_t);
1838 prev_size
= sizeof (uint32_t);
1840 p_data
= adt_adjust_address(p_data
, prev_size
,
1842 prev_size
= this_size
;
1849 * adt_generate_event
1850 * generate event record from external struct. The order is based on
1851 * the output tokens, allowing for the possibility that the input data
1852 * is in a different order.
1857 adt_generate_event(const adt_event_data_t
*p_extdata
,
1858 struct adt_event_state
*p_event
,
1859 adt_translation_t
*p_xlate
)
1861 struct entry
*p_entry
;
1862 static mutex_t lock
= DEFAULTMUTEX
;
1864 p_entry
= p_xlate
->tx_first_entry
;
1865 assert(p_entry
!= NULL
);
1867 p_event
->ae_internal_id
= p_xlate
->tx_internal_event
;
1868 adt_token_open(p_event
);
1871 * offsets are not pre-calculated; the initial offsets are all
1872 * 0; valid offsets are >= 0. Offsets for no-input tokens such
1873 * as subject are set to -1 by adt_calcOffset()
1875 if (p_xlate
->tx_offsetsCalculated
== 0) {
1876 (void) mutex_lock(&lock
);
1877 p_xlate
->tx_offsetsCalculated
= 1;
1879 adt_calcOffsets(p_xlate
->tx_top_entry
, p_xlate
->tx_entries
,
1881 (void) mutex_unlock(&lock
);
1883 while (p_entry
!= NULL
) {
1884 adt_generate_token(p_entry
, (char *)p_extdata
, p_event
);
1886 p_entry
= p_entry
->en_next_token
;
1888 return (adt_token_close(p_event
));
1892 * adt_put_event -- main event generation function.
1893 * The input "event" is the address of the struct containing
1894 * event-specific data.
1896 * However if auditing is off or the session handle
1897 * is NULL, no attempt to write a record is made.
1901 adt_put_event(const adt_event_data_t
*event
, int status
, int return_val
)
1903 struct adt_event_state
*event_state
;
1904 adt_translation_t
*xlate
;
1906 if (event
== NULL
) {
1910 event_state
= (struct adt_event_state
*)event
;
1912 /* if this is a broken session or not auditing, exit */
1913 if ((event_state
->ae_session
== NULL
) ||
1914 !(event_state
->ae_session
->as_audit_state
&
1915 (AUC_AUDITING
| AUC_NOSPACE
))) {
1919 assert(event_state
->ae_check
== ADT_VALID
);
1921 event_state
->ae_rc
= status
;
1922 event_state
->ae_type
= return_val
;
1924 /* look up the event */
1926 xlate
= adt_getXlateTable(event_state
->ae_session
->as_xlate
,
1927 event_state
->ae_event_id
);
1929 if (xlate
== NULL
) {
1933 DPRINTF(("got event %d\n", xlate
->tx_internal_event
));
1935 if (adt_selected(event_state
, xlate
->tx_internal_event
, status
)) {
1936 return (adt_generate_event(event
, event_state
, xlate
));
1943 * adt_free_event -- invalidate and free
1947 adt_free_event(adt_event_data_t
*event
)
1949 struct adt_event_state
*event_state
;
1954 event_state
= (struct adt_event_state
*)event
;
1956 assert(event_state
->ae_check
== ADT_VALID
);
1958 event_state
->ae_check
= 0;
1964 * adt_is_selected -- helper to adt_selected(), below.
1966 * "sorf" is "success or fail" status; au_preselect compares
1967 * that with success, fail, or both.
1971 adt_is_selected(au_event_t e
, au_mask_t
*m
, int sorf
)
1976 prs_sorf
= AU_PRS_SUCCESS
;
1978 prs_sorf
= AU_PRS_FAILURE
;
1980 return (au_preselect(e
, m
, prs_sorf
, AU_PRS_REREAD
));
1984 * selected -- see if this event is preselected.
1986 * if errors are encountered trying to check a preselection mask
1987 * or look up a user name, the event is selected. Otherwise, the
1988 * preselection mask is used for the job.
1992 adt_selected(struct adt_event_state
*event
, au_event_t actual_id
, int status
)
1994 adt_internal_state_t
*sp
;
1997 sp
= event
->ae_session
;
1999 if ((sp
->as_have_user_data
& ADT_HAVE_IDS
) == 0) {
2000 adt_write_syslog("No user data available", EINVAL
);
2001 return (1); /* default is "selected" */
2004 /* non-attributable? */
2005 if ((sp
->as_info
.ai_auid
== AU_NOAUDITID
) ||
2006 (sp
->as_info
.ai_auid
== ADT_NO_AUDIT
)) {
2007 if (auditon(A_GETKMASK
, (caddr_t
)&namask
,
2008 sizeof (namask
)) != 0) {
2009 adt_write_syslog("auditon failure", errno
);
2012 return (adt_is_selected(actual_id
, &namask
, status
));
2014 return (adt_is_selected(actual_id
, &(sp
->as_info
.ai_mask
),
2020 * Can't map the host name to an IP address in
2021 * adt_get_hostIP. Get something off an interface
2022 * to act as the hosts IP address for auditing.
2026 adt_get_local_address(int family
, struct ifaddrlist
*al
)
2028 struct ifaddrlist
*ifal
;
2029 char errbuf
[ERRBUFSIZE
] = "empty list";
2030 char msg
[ERRBUFSIZE
+ 512];
2034 if ((ifal_count
= ifaddrlist(&ifal
, family
, 0, errbuf
)) < 0) {
2037 (void) snprintf(msg
, sizeof (msg
), "adt_get_local_address "
2038 "couldn't get %d addrlist %s", family
, errbuf
);
2039 adt_write_syslog(msg
, serrno
);
2044 for (i
= 0; i
< ifal_count
; i
++) {
2046 * loopback always defined,
2047 * even if there is no real address
2049 if ((ifal
[i
].flags
& (IFF_UP
| IFF_LOOPBACK
)) == IFF_UP
) {
2053 if (i
>= ifal_count
) {
2056 * Callers of adt_get_hostIP() can only return
2057 * errno to their callers and eventually the application.
2058 * Picked one that seemed least worse for saying no
2059 * usable address for Audit terminal ID.