2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <bsm/adt.h>
2N/A#include <bsm/adt_event.h>
2N/A#include <assert.h>
2N/A#include <bsm/audit.h>
2N/A#include <bsm/audit_record.h>
2N/A#include <bsm/libbsm.h>
2N/A#include <door.h>
2N/A#include <errno.h>
2N/A#include <generic.h>
2N/A#include <md5.h>
2N/A#include <sys/mkdev.h>
2N/A#include <netdb.h>
2N/A#include <nss_dbdefs.h>
2N/A#include <pwd.h>
2N/A#include <sys/stat.h>
2N/A#include <time.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <synch.h>
2N/A#include <sys/systeminfo.h>
2N/A#include <syslog.h>
2N/A#include <thread.h>
2N/A#include <unistd.h>
2N/A#include <adt_xlate.h>
2N/A#include <adt_ucred.h>
2N/A#include <arpa/inet.h>
2N/A#include <net/if.h>
2N/A#include <libinetutil.h>
2N/A#include "audit_plugin.h"
2N/A
2N/Astatic int adt_selected(struct adt_event_state *, au_event_t, int);
2N/Astatic int adt_init(adt_internal_state_t *, int);
2N/Astatic int adt_import(adt_internal_state_t *, const adt_export_data_t *);
2N/Astatic m_label_t *adt_ucred_label(ucred_t *);
2N/Astatic void adt_setto_unaudited(adt_internal_state_t *);
2N/Astatic int adt_get_local_address(int, struct ifaddrlist *);
2N/A
2N/A#ifdef C2_DEBUG
2N/A#define DPRINTF(x) { (void) printf x; }
2N/A#define DFLUSH (void) fflush(stdout);
2N/A#else
2N/A#define DPRINTF(x)
2N/A#define DFLUSH
2N/A#endif
2N/A
2N/A/*
2N/A * Local audit states are a bit mask
2N/A *
2N/A * The global audit states are
2N/A *
2N/A * AUC_UNSET 0 - on/off hasn't been decided
2N/A * AUC_ENABLED 1 - loaded and enabled
2N/A *
2N/A * The local Zone states are
2N/A *
2N/A * AUC_AUDITING 0x1 - audit daemon is active
2N/A * AUC_NOAUDIT 0x2 - audit daemon is not active
2N/A * AUC_INIT_AUDIT 0x4 - audit is ready but auditd has not run
2N/A *
2N/A * The only values returned by auditon(A_GETCOND) are:
2N/A * AUC_INIT_AUDIT, AUC_AUDITING, AUC_NOAUDIT
2N/A *
2N/A * The pseudo audit state used when the c2audit module is excluded is
2N/A *
2N/A * AUC_DISABLED 0x100 - c2audit module is excluded
2N/A */
2N/A
2N/Astatic int auditstate = AUC_DISABLED; /* default state */
2N/A
2N/A/*
2N/A * adt_write_syslog
2N/A *
2N/A * errors that are not the user's fault (bugs or whatever in
2N/A * the underlying audit code are noted in syslog.)
2N/A *
2N/A * Avoid calling adt_write_syslog for things that can happen
2N/A * at high volume.
2N/A *
2N/A * syslog's open (openlog) and close (closelog) are interesting;
2N/A * openlog *may* create a file descriptor and is optional. closelog
2N/A * *will* close any open file descriptors and is also optional.
2N/A *
2N/A * Since syslog may also be used by the calling application, the
2N/A * choice is to avoid openlog, which sets some otherwise useful
2N/A * parameters, and to embed "Solaris_audit" in the log message.
2N/A */
2N/A
2N/Avoid
2N/Aadt_write_syslog(const char *message, int err)
2N/A{
2N/A int save_errno = errno;
2N/A int mask_priority;
2N/A
2N/A DPRINTF(("syslog called: %s\n", message));
2N/A
2N/A mask_priority = setlogmask(LOG_MASK(LOG_ALERT));
2N/A errno = err;
2N/A syslog(LOG_ALERT, "Solaris_audit %s: %m", message);
2N/A (void) setlogmask(mask_priority);
2N/A errno = save_errno;
2N/A}
2N/A
2N/A/*
2N/A * adt_audit_state()
2N/A *
2N/A * returns true if the kernel audit state matches with at least one
2N/A * of audit states set in the argument.
2N/A *
2N/A */
2N/A
2N/Aboolean_t
2N/Aadt_audit_state(int states)
2N/A{
2N/A
2N/A (void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate));
2N/A
2N/A return ((auditstate & states) ? B_TRUE : B_FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Get user_specific/non-attributable audit mask. This may be called even when
2N/A * auditing is off.
2N/A */
2N/A
2N/Astatic int
2N/Aadt_get_mask_from_user(uid_t uid, au_mask_t *mask)
2N/A{
2N/A struct passwd pwd;
2N/A long buff_sz;
2N/A char *pwd_buff;
2N/A
2N/A
2N/A if (auditstate & AUC_DISABLED) {
2N/A /* c2audit excluded */
2N/A mask->am_success = AU_MASK_NONE;
2N/A mask->am_failure = AU_MASK_NONE;
2N/A } else if (uid <= MAXUID) {
2N/A if ((buff_sz = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) {
2N/A adt_write_syslog("couldn't determine maximum size of "
2N/A "password buffer", errno);
2N/A return (-1);
2N/A }
2N/A if ((pwd_buff = calloc(1, (size_t)++buff_sz)) == NULL) {
2N/A return (-1);
2N/A }
2N/A if (getpwuid_r(uid, &pwd, pwd_buff, (int)buff_sz) == NULL) {
2N/A errno = EINVAL; /* user doesn't exist */
2N/A free(pwd_buff);
2N/A return (-1);
2N/A }
2N/A if (au_user_mask(pwd.pw_name, mask)) {
2N/A errno = EFAULT; /* undetermined failure */
2N/A free(pwd_buff);
2N/A return (-1);
2N/A }
2N/A free(pwd_buff);
2N/A } else if (auditon(A_GETKMASK, (caddr_t)mask, sizeof (*mask)) == -1) {
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * adt_get_unique_id -- generate a hopefully unique 32 bit value
2N/A *
2N/A * there will be a follow up to replace this with the use of /dev/random
2N/A *
2N/A * An MD5 hash is taken on a buffer of
2N/A * hostname . audit id . unix time . pid . count
2N/A *
2N/A * "count = noise++;" is subject to a race condition but I don't
2N/A * see a need to put a lock around it.
2N/A */
2N/A
2N/Aau_asid_t
2N/Aadt_get_unique_id(au_id_t uid)
2N/A{
2N/A char hostname[MAXHOSTNAMELEN];
2N/A union {
2N/A au_id_t v[4];
2N/A unsigned char obuff[128/8];
2N/A } output;
2N/A MD5_CTX context;
2N/A
2N/A static int noise = 0;
2N/A
2N/A int count = noise++;
2N/A time_t timebits = time(NULL);
2N/A pid_t pidbits = getpid();
2N/A au_asid_t retval = 0;
2N/A
2N/A if (gethostname(hostname, MAXHOSTNAMELEN)) {
2N/A adt_write_syslog("gethostname call failed", errno);
2N/A (void) strncpy(hostname, "invalidHostName", MAXHOSTNAMELEN);
2N/A }
2N/A
2N/A while (retval == 0) { /* 0 is the only invalid result */
2N/A MD5Init(&context);
2N/A
2N/A MD5Update(&context, (unsigned char *)hostname,
2N/A (unsigned int) strlen((const char *)hostname));
2N/A
2N/A MD5Update(&context, (unsigned char *) &uid, sizeof (uid_t));
2N/A
2N/A MD5Update(&context,
2N/A (unsigned char *) &timebits, sizeof (time_t));
2N/A
2N/A MD5Update(&context, (unsigned char *) &pidbits,
2N/A sizeof (pid_t));
2N/A
2N/A MD5Update(&context, (unsigned char *) &(count), sizeof (int));
2N/A MD5Final(output.obuff, &context);
2N/A
2N/A retval = output.v[count % 4];
2N/A }
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * the following "port" function deals with the following issues:
2N/A *
2N/A * 1 the kernel and ucred deal with a dev_t as a 64 bit value made
2N/A * up from a 32 bit major and 32 bit minor.
2N/A * 2 User space deals with a dev_t as either the above 64 bit value
2N/A * or a 32 bit value made from a 14 bit major and an 18 bit minor.
2N/A * 3 The various audit interfaces (except ucred) pass the 32 or
2N/A * 64 bit version depending the architecture of the userspace
2N/A * application. If you get a port value from ucred and pass it
2N/A * to the kernel via auditon(), it must be squeezed into a 32
2N/A * bit value because the kernel knows the userspace app's bit
2N/A * size.
2N/A *
2N/A * The internal state structure for adt (adt_internal_state_t) uses
2N/A * dev_t, so adt converts data from ucred to fit. The import/export
2N/A * functions, however, can't know if they are importing/exporting
2N/A * from 64 or 32 bit applications, so they always send 64 bits and
2N/A * the 32 bit end(s) are responsible to convert 32 -> 64 -> 32 as
2N/A * appropriate.
2N/A */
2N/A
2N/A/*
2N/A * adt_cpy_tid() -- if lib is 64 bit, just copy it (dev_t and port are
2N/A * both 64 bits). If lib is 32 bits, squeeze the two-int port into
2N/A * a 32 bit dev_t. A port fits in the "minor" part of au_port_t,
2N/A * so it isn't broken up into pieces. (When it goes to the kernel
2N/A * and back, however, it will have been split into major/minor
2N/A * pieces.)
2N/A */
2N/A
2N/Astatic void
2N/Aadt_cpy_tid(au_tid_addr_t *dest, const au_tid64_addr_t *src)
2N/A{
2N/A#ifdef _LP64
2N/A (void) memcpy(dest, src, sizeof (au_tid_addr_t));
2N/A#else /* _LP64 */
2N/A dest->at_type = src->at_type;
2N/A
2N/A dest->at_port = src->at_port.at_minor & MAXMIN32;
2N/A dest->at_port |= (src->at_port.at_major & MAXMAJ32) <<
2N/A NBITSMINOR32;
2N/A
2N/A (void) memcpy(dest->at_addr, src->at_addr, 4 * sizeof (uint32_t));
2N/A#endif /* _LP64 */
2N/A}
2N/A
2N/A/*
2N/A * adt_start_session -- create interface handle, create context
2N/A *
2N/A * The imported_state input is normally NULL, if not, it represents
2N/A * a continued session; its values obviate the need for a subsequent
2N/A * call to adt_set_user().
2N/A *
2N/A * The flag is used to decide how to set the initial state of the session.
2N/A * If 0, the session is "no audit" until a call to adt_set_user; if
2N/A * ADT_USE_PROC_DATA, the session is built from the process audit
2N/A * characteristics obtained from the kernel. If imported_state is
2N/A * not NULL, the resulting audit mask is an OR of the current process
2N/A * audit mask and that passed in.
2N/A *
2N/A * The basic model is that the caller can use the pointer returned
2N/A * by adt_start_session whether or not auditing is enabled or an
2N/A * error was returned. The functions that take the session handle
2N/A * as input generally return without doing anything if auditing is
2N/A * disabled.
2N/A */
2N/A
2N/Aint
2N/Aadt_start_session(adt_session_data_t **new_session,
2N/A const adt_export_data_t *imported_state, adt_session_flags_t flags)
2N/A{
2N/A adt_internal_state_t *state;
2N/A adt_session_flags_t flgmask = ADT_FLAGS_ALL;
2N/A
2N/A /* test and set auditstate */
2N/A if (adt_audit_state(AUC_DISABLED)) {
2N/A /* c2audit excluded */
2N/A *new_session = NULL;
2N/A return (0);
2N/A }
2N/A
2N/A if ((flags & ~flgmask) != 0) {
2N/A errno = EINVAL;
2N/A goto return_err;
2N/A }
2N/A
2N/A if ((state = calloc(1, sizeof (adt_internal_state_t))) == NULL) {
2N/A goto return_err;
2N/A }
2N/A
2N/A if (adt_init(state, flags & ADT_USE_PROC_DATA) != 0) {
2N/A goto return_err_free; /* errno from adt_init() */
2N/A }
2N/A
2N/A /*
2N/A * The imported state overwrites the initial state if the
2N/A * imported state represents a valid audit trail
2N/A */
2N/A
2N/A if (imported_state != NULL) {
2N/A if (adt_import(state, imported_state) != 0) {
2N/A goto return_err_free;
2N/A }
2N/A } else if (flags & ADT_USE_PROC_DATA) {
2N/A state->as_session_model = ADT_PROCESS_MODEL;
2N/A }
2N/A state->as_flags = flags;
2N/A DPRINTF(("(%lld) Starting session id = %08X\n",
2N/A (long long) getpid(), state->as_info.ai_asid));
2N/A
2N/A *new_session = (adt_session_data_t *)state;
2N/A return (0);
2N/A
2N/Areturn_err_free:
2N/A free(state);
2N/Areturn_err:
2N/A *new_session = NULL;
2N/A adt_write_syslog("audit session create failed", errno);
2N/A return (-1);
2N/A}
2N/A
2N/A/*
2N/A * adt_load_table()
2N/A *
2N/A * loads the event translation table into the audit session.
2N/A */
2N/A
2N/Avoid
2N/Aadt_load_table(const adt_session_data_t *session_data,
2N/A adt_translation_t **xlate, void (*preload)(au_event_t, adt_event_data_t *))
2N/A{
2N/A adt_internal_state_t *state = (adt_internal_state_t *)session_data;
2N/A
2N/A if (state != NULL) {
2N/A assert(state->as_check == ADT_VALID);
2N/A state->as_xlate = xlate;
2N/A state->as_preload = preload;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * adt_get_asid() and adt_set_asid()
2N/A *
2N/A * if you use this interface, you are responsible to insure that the
2N/A * rest of the session data is populated correctly before calling
2N/A * adt_proccess_attr()
2N/A *
2N/A * neither of these are intended for general use and will likely
2N/A * remain private interfaces for a long time. Forever is a long
2N/A * time. In the case of adt_set_asid(), you should have a very,
2N/A * very good reason for setting your own session id. The process
2N/A * audit characteristics are not changed by put, use adt_set_proc().
2N/A *
2N/A * These are "volatile" (more changable than "evolving") and will
2N/A * probably change in the S10 period.
2N/A */
2N/A
2N/Avoid
2N/Aadt_get_asid(const adt_session_data_t *session_data, au_asid_t *asid)
2N/A{
2N/A
2N/A if (session_data == NULL) {
2N/A *asid = 0;
2N/A } else {
2N/A assert(((adt_internal_state_t *)session_data)->as_check ==
2N/A ADT_VALID);
2N/A
2N/A *asid = ((adt_internal_state_t *)session_data)->as_info.ai_asid;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Aadt_set_asid(const adt_session_data_t *session_data, const au_asid_t session_id)
2N/A{
2N/A
2N/A if (session_data != NULL) {
2N/A assert(((adt_internal_state_t *)session_data)->as_check ==
2N/A ADT_VALID);
2N/A
2N/A ((adt_internal_state_t *)session_data)->as_have_user_data |=
2N/A ADT_HAVE_ASID;
2N/A ((adt_internal_state_t *)session_data)->as_info.ai_asid =
2N/A session_id;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * adt_get_auid() and adt_set_auid()
2N/A *
2N/A * neither of these are intended for general use and will likely
2N/A * remain private interfaces for a long time. Forever is a long
2N/A * time. In the case of adt_set_auid(), you should have a very,
2N/A * very good reason for setting your own audit id. The process
2N/A * audit characteristics are not changed by put, use adt_set_proc().
2N/A */
2N/A
2N/Avoid
2N/Aadt_get_auid(const adt_session_data_t *session_data, au_id_t *auid)
2N/A{
2N/A
2N/A if (session_data == NULL) {
2N/A *auid = AU_NOAUDITID;
2N/A } else {
2N/A assert(((adt_internal_state_t *)session_data)->as_check ==
2N/A ADT_VALID);
2N/A
2N/A *auid = ((adt_internal_state_t *)session_data)->as_info.ai_auid;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Aadt_set_auid(const adt_session_data_t *session_data, const au_id_t audit_id)
2N/A{
2N/A
2N/A if (session_data != NULL) {
2N/A assert(((adt_internal_state_t *)session_data)->as_check ==
2N/A ADT_VALID);
2N/A
2N/A ((adt_internal_state_t *)session_data)->as_have_user_data |=
2N/A ADT_HAVE_AUID;
2N/A ((adt_internal_state_t *)session_data)->as_info.ai_auid =
2N/A audit_id;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * adt_get_termid(), adt_set_termid()
2N/A *
2N/A * if you use this interface, you are responsible to insure that the
2N/A * rest of the session data is populated correctly before calling
2N/A * adt_proccess_attr()
2N/A *
2N/A * The process audit characteristics are not changed by put, use
2N/A * adt_set_proc().
2N/A */
2N/A
2N/Avoid
2N/Aadt_get_termid(const adt_session_data_t *session_data, au_tid_addr_t *termid)
2N/A{
2N/A
2N/A if (session_data == NULL) {
2N/A (void) memset(termid, 0, sizeof (au_tid_addr_t));
2N/A termid->at_type = AU_IPv4;
2N/A } else {
2N/A assert(((adt_internal_state_t *)session_data)->as_check ==
2N/A ADT_VALID);
2N/A
2N/A *termid =
2N/A ((adt_internal_state_t *)session_data)->as_info.ai_termid;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Aadt_set_termid(const adt_session_data_t *session_data,
2N/A const au_tid_addr_t *termid)
2N/A{
2N/A
2N/A if (session_data != NULL) {
2N/A assert(((adt_internal_state_t *)session_data)->as_check ==
2N/A ADT_VALID);
2N/A
2N/A ((adt_internal_state_t *)session_data)->as_info.ai_termid =
2N/A *termid;
2N/A
2N/A ((adt_internal_state_t *)session_data)->as_have_user_data |=
2N/A ADT_HAVE_TID;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * adt_get_mask(), adt_set_mask()
2N/A *
2N/A * if you use this interface, you are responsible to insure that the
2N/A * rest of the session data is populated correctly before calling
2N/A * adt_proccess_attr()
2N/A *
2N/A * The process audit characteristics are not changed by put, use
2N/A * adt_set_proc().
2N/A */
2N/A
2N/Avoid
2N/Aadt_get_mask(const adt_session_data_t *session_data, au_mask_t *mask)
2N/A{
2N/A
2N/A if (session_data == NULL) {
2N/A mask->am_success = AU_MASK_NONE;
2N/A mask->am_failure = AU_MASK_NONE;
2N/A } else {
2N/A assert(((adt_internal_state_t *)session_data)->as_check ==
2N/A ADT_VALID);
2N/A
2N/A *mask = ((adt_internal_state_t *)session_data)->as_info.ai_mask;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Aadt_set_mask(const adt_session_data_t *session_data, const au_mask_t *mask)
2N/A{
2N/A
2N/A if (session_data != NULL) {
2N/A assert(((adt_internal_state_t *)session_data)->as_check ==
2N/A ADT_VALID);
2N/A
2N/A ((adt_internal_state_t *)session_data)->as_info.ai_mask = *mask;
2N/A
2N/A ((adt_internal_state_t *)session_data)->as_have_user_data |=
2N/A ADT_HAVE_MASK;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * helpers for adt_load_termid
2N/A */
2N/A
2N/Astatic void
2N/Aadt_do_ipv6_address(struct sockaddr_in6 *peer, struct sockaddr_in6 *sock,
2N/A au_tid_addr_t *termid)
2N/A{
2N/A
2N/A termid->at_port = ((peer->sin6_port<<16) | (sock->sin6_port));
2N/A termid->at_type = AU_IPv6;
2N/A (void) memcpy(termid->at_addr, &peer->sin6_addr, 4 * sizeof (uint_t));
2N/A}
2N/A
2N/Astatic void
2N/Aadt_do_ipv4_address(struct sockaddr_in *peer, struct sockaddr_in *sock,
2N/A au_tid_addr_t *termid)
2N/A{
2N/A
2N/A termid->at_port = ((peer->sin_port<<16) | (sock->sin_port));
2N/A
2N/A termid->at_type = AU_IPv4;
2N/A termid->at_addr[0] = (uint32_t)peer->sin_addr.s_addr;
2N/A (void) memset(&(termid->at_addr[1]), 0, 3 * sizeof (uint_t));
2N/A}
2N/A
2N/A/*
2N/A * adt_load_termid: convenience function; inputs file handle and
2N/A * outputs an au_tid_addr struct.
2N/A *
2N/A * This code was stolen from audit_settid.c; it differs from audit_settid()
2N/A * in that it does not write the terminal id to the process.
2N/A */
2N/A
2N/Aint
2N/Aadt_load_termid(int fd, adt_termid_t **termid)
2N/A{
2N/A au_tid_addr_t *p_term;
2N/A struct sockaddr_in6 peer;
2N/A struct sockaddr_in6 sock;
2N/A int peerlen = sizeof (peer);
2N/A int socklen = sizeof (sock);
2N/A
2N/A /* get peer name if its a socket, else assume local terminal */
2N/A
2N/A if (getpeername(fd, (struct sockaddr *)&peer, (socklen_t *)&peerlen)
2N/A < 0) {
2N/A if (errno == ENOTSOCK) {
2N/A return (adt_load_hostname(NULL, termid));
2N/A }
2N/A goto return_err;
2N/A }
2N/A
2N/A if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
2N/A goto return_err;
2N/A }
2N/A
2N/A /* get sock name */
2N/A if (getsockname(fd, (struct sockaddr *)&sock,
2N/A (socklen_t *)&socklen) < 0) {
2N/A goto return_err_free;
2N/A }
2N/A
2N/A if (peer.sin6_family == AF_INET6) {
2N/A adt_do_ipv6_address(&peer, &sock, p_term);
2N/A } else {
2N/A adt_do_ipv4_address((struct sockaddr_in *)&peer,
2N/A (struct sockaddr_in *)&sock, p_term);
2N/A }
2N/A *termid = (adt_termid_t *)p_term;
2N/A
2N/A return (0);
2N/A
2N/Areturn_err_free:
2N/A free(p_term);
2N/Areturn_err:
2N/A *termid = NULL;
2N/A return (-1);
2N/A}
2N/A
2N/Astatic boolean_t
2N/Aadt_have_termid(au_tid_addr_t *dest)
2N/A{
2N/A struct auditinfo_addr audit_data;
2N/A
2N/A if (getaudit_addr(&audit_data, sizeof (audit_data)) < 0) {
2N/A adt_write_syslog("getaudit failed", errno);
2N/A return (B_FALSE);
2N/A }
2N/A
2N/A if ((audit_data.ai_termid.at_type == 0) ||
2N/A (audit_data.ai_termid.at_addr[0] |
2N/A audit_data.ai_termid.at_addr[1] |
2N/A audit_data.ai_termid.at_addr[2] |
2N/A audit_data.ai_termid.at_addr[3]) == 0)
2N/A return (B_FALSE);
2N/A
2N/A (void) memcpy(dest, &(audit_data.ai_termid),
2N/A sizeof (au_tid_addr_t));
2N/A
2N/A return (B_TRUE);
2N/A}
2N/A
2N/A/*
2N/A * adt_get_hostIP - construct a terminal id from a hostname
2N/A *
2N/A * Returns 0 = success
2N/A * -1 = failure and errno = ENETDOWN with the address
2N/A * defaulted to IPv4 loopback.
2N/A */
2N/A
2N/Astatic int
2N/Aadt_get_hostIP(const char *hostname, boolean_t local, au_tid_addr_t *p_term)
2N/A{
2N/A struct addrinfo *ai = NULL;
2N/A auditinfo_addr_t audit_info;
2N/A int tries = 3;
2N/A int eai_err;
2N/A
2N/A /*
2N/A * If local request try to use the current system instance
2N/A * local terminal ID
2N/A */
2N/A if (local) {
2N/A if (auditon(A_GETKAUDIT, (caddr_t)&audit_info,
2N/A sizeof (audit_info)) < 0) {
2N/A adt_write_syslog("unable to get kernel audit context",
2N/A errno);
2N/A return (-1);
2N/A }
2N/A if ((audit_info.ai_termid.at_addr[0] |
2N/A audit_info.ai_termid.at_addr[1] |
2N/A audit_info.ai_termid.at_addr[2] |
2N/A audit_info.ai_termid.at_addr[3]) != 0) {
2N/A *p_term = audit_info.ai_termid;
2N/A return (0);
2N/A }
2N/A }
2N/A
2N/A while ((tries-- > 0) &&
2N/A ((eai_err = getaddrinfo(hostname, NULL, NULL, &ai)) != 0)) {
2N/A /*
2N/A * getaddrinfo returns its own set of errors.
2N/A * Log them here, so any subsequent syslogs will
2N/A * have a context. adt_get_hostIP callers can only
2N/A * return errno, so subsequent syslogs may be lacking
2N/A * that getaddrinfo failed.
2N/A */
2N/A __auditd_debug("getaddrinfo(%s) failed[%s]\n", hostname,
2N/A gai_strerror(eai_err));
2N/A
2N/A if (eai_err != EAI_AGAIN) {
2N/A break;
2N/A }
2N/A /* see if resolution becomes available */
2N/A (void) sleep(1);
2N/A }
2N/A if (ai != NULL) {
2N/A if (ai->ai_family == AF_INET) {
2N/A p_term->at_type = AU_IPv4;
2N/A (void) memcpy(p_term->at_addr,
2N/A /* LINTED */
2N/A &((struct sockaddr_in *)ai->ai_addr)->sin_addr,
2N/A AU_IPv4);
2N/A } else {
2N/A p_term->at_type = AU_IPv6;
2N/A (void) memcpy(p_term->at_addr,
2N/A /* LINTED */
2N/A &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
2N/A AU_IPv6);
2N/A }
2N/A freeaddrinfo(ai);
2N/A return (0);
2N/A } else if (auditstate & AUC_AUDITING) {
2N/A /*
2N/A * auditd is running see if there's an
2N/A * kernel audit context. It could be that
2N/A * auditd is being refreshed from a loopback
2N/A * situation. See __do_sethost().
2N/A */
2N/A if (auditon(A_GETKAUDIT, (caddr_t)&audit_info,
2N/A sizeof (audit_info)) < 0) {
2N/A adt_write_syslog("unable to get kernel audit context",
2N/A errno);
2N/A goto try_interface;
2N/A }
2N/A if ((audit_info.ai_termid.at_addr[0] |
2N/A audit_info.ai_termid.at_addr[1] |
2N/A audit_info.ai_termid.at_addr[2] |
2N/A audit_info.ai_termid.at_addr[3]) == 0) {
2N/A goto try_interface;
2N/A }
2N/A __auditd_debug("setting Audit IP address to kernel for %s\n",
2N/A hostname);
2N/A *p_term = audit_info.ai_termid;
2N/A return (0);
2N/A }
2N/Atry_interface:
2N/A {
2N/A struct ifaddrlist al;
2N/A int family;
2N/A char ntop[INET6_ADDRSTRLEN];
2N/A
2N/A /*
2N/A * getaddrinfo has failed to map the hostname
2N/A * to an IP address, try to get an IP address
2N/A * from a local interface. If none up, default
2N/A * to loopback.
2N/A */
2N/A family = AF_INET6;
2N/A if (adt_get_local_address(family, &al) != 0) {
2N/A family = AF_INET;
2N/A
2N/A if (adt_get_local_address(family, &al) != 0) {
2N/A __auditd_debug("adt_get_local_address failed, "
2N/A "no Audit IP address available, "
2N/A "faking loopback for %s and "
2N/A "error %s\n", hostname,
2N/A strerror(errno));
2N/A al.addr.addr.s_addr = htonl(INADDR_LOOPBACK);
2N/A (void) memcpy(p_term->at_addr, &al.addr.addr,
2N/A AU_IPv4);
2N/A p_term->at_type = AU_IPv4;
2N/A return (-1);
2N/A }
2N/A }
2N/A if (family == AF_INET) {
2N/A p_term->at_type = AU_IPv4;
2N/A (void) memcpy(p_term->at_addr, &al.addr.addr, AU_IPv4);
2N/A } else {
2N/A p_term->at_type = AU_IPv6;
2N/A (void) memcpy(p_term->at_addr, &al.addr.addr6, AU_IPv6);
2N/A }
2N/A
2N/A __auditd_debug("mapping %s to %s\n", hostname,
2N/A inet_ntop(family, &(al.addr), ntop, sizeof (ntop)));
2N/A return (0);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * adt_load_hostname() is called when the caller does not have a file
2N/A * handle that gives access to the socket info or any other way to
2N/A * pass in both port and ip address. The hostname input is ignored if
2N/A * the terminal id has already been set; instead it returns the
2N/A * existing terminal id.
2N/A *
2N/A * If c2audit is excluded, success is returned.
2N/A * If the hostname lookup fails, the loopback address is assumed,
2N/A * errno is set to ENETDOWN, this allows the caller to interpret
2N/A * whether failure is fatal, and if not to have a address for the
2N/A * hostname.
2N/A * Otherwise the caller would need to be aware of the audit state.
2N/A *
2N/A * Other errors are ignored if not auditing.
2N/A */
2N/A
2N/Aint
2N/Aadt_load_hostname(const char *hostname, adt_termid_t **termid)
2N/A{
2N/A char localhost[MAXHOSTNAMELEN + 1];
2N/A au_tid_addr_t *p_term;
2N/A boolean_t local = B_FALSE;
2N/A
2N/A if (adt_audit_state(AUC_DISABLED)) {
2N/A /* c2audit excluded */
2N/A *termid = NULL;
2N/A return (0);
2N/A }
2N/A
2N/A if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
2N/A goto return_err;
2N/A }
2N/A
2N/A if (adt_have_termid(p_term)) {
2N/A *termid = (adt_termid_t *)p_term;
2N/A return (0);
2N/A }
2N/A p_term->at_port = 0;
2N/A
2N/A if (hostname == NULL || *hostname == '\0') {
2N/A (void) sysinfo(SI_HOSTNAME, localhost, MAXHOSTNAMELEN);
2N/A hostname = localhost;
2N/A local = B_TRUE;
2N/A }
2N/A if (adt_get_hostIP(hostname, local, p_term) == 0) {
2N/A *termid = (adt_termid_t *)p_term;
2N/A return (0);
2N/A } else {
2N/A *termid = (adt_termid_t *)p_term;
2N/A return (-1);
2N/A }
2N/A
2N/Areturn_err:
2N/A *termid = NULL;
2N/A if (auditstate & AUC_NOAUDIT) {
2N/A return (0);
2N/A }
2N/A
2N/A return (-1);
2N/A}
2N/A
2N/A/*
2N/A * adt_load_ttyname() is called when the caller does not have a file
2N/A * handle that gives access to the local terminal or any other way
2N/A * of determining the device id. The ttyname input is ignored if
2N/A * the terminal id has already been set; instead it returns the
2N/A * existing terminal id.
2N/A *
2N/A * If c2audit is excluded, success is returned.
2N/A * The local hostname is used for the local IP address.
2N/A * If that hostname lookup fails, the loopback address is assumed,
2N/A * errno is set to ENETDOWN, this allows the caller to interpret
2N/A * whether failure is fatal, and if not to have a address for the
2N/A * hostname.
2N/A * Otherwise the caller would need to be aware of the audit state.
2N/A *
2N/A * Other errors are ignored if not auditing.
2N/A */
2N/A
2N/Aint
2N/Aadt_load_ttyname(const char *ttyname, adt_termid_t **termid)
2N/A{
2N/A char localhost[MAXHOSTNAMELEN + 1];
2N/A au_tid_addr_t *p_term;
2N/A struct stat stat_buf;
2N/A
2N/A if (adt_audit_state(AUC_DISABLED)) {
2N/A /* c2audit excluded */
2N/A *termid = NULL;
2N/A return (0);
2N/A }
2N/A
2N/A if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) {
2N/A goto return_err;
2N/A }
2N/A
2N/A if (adt_have_termid(p_term)) {
2N/A *termid = (adt_termid_t *)p_term;
2N/A return (0);
2N/A }
2N/A
2N/A p_term->at_port = 0;
2N/A
2N/A if (sysinfo(SI_HOSTNAME, localhost, MAXHOSTNAMELEN) < 0) {
2N/A goto return_err_free; /* errno from sysinfo */
2N/A }
2N/A
2N/A if (ttyname != NULL && *ttyname != '\0') {
2N/A if (stat(ttyname, &stat_buf) < 0) {
2N/A goto return_err_free;
2N/A }
2N/A
2N/A p_term->at_port = stat_buf.st_rdev;
2N/A }
2N/A
2N/A if (adt_get_hostIP(localhost, B_TRUE, p_term) == 0) {
2N/A *termid = (adt_termid_t *)p_term;
2N/A return (0);
2N/A } else {
2N/A *termid = (adt_termid_t *)p_term;
2N/A return (-1);
2N/A }
2N/A
2N/Areturn_err_free:
2N/A free(p_term);
2N/A
2N/Areturn_err:
2N/A *termid = NULL;
2N/A if (auditstate & AUC_NOAUDIT) {
2N/A return (0);
2N/A }
2N/A
2N/A return (-1);
2N/A}
2N/A
2N/A/*
2N/A * adt_get_session_id returns a stringified representation of
2N/A * the audit session id. See also adt_get_asid() for how to
2N/A * get the unexpurgated version. No guarantees as to how long
2N/A * the returned string will be or its general form; hex for now.
2N/A *
2N/A * An empty string is returned if auditing is off; length = 1
2N/A * and the pointer is valid.
2N/A *
2N/A * returns strlen + 1 if buffer is valid; else 0 and errno.
2N/A */
2N/A
2N/Asize_t
2N/Aadt_get_session_id(const adt_session_data_t *session_data, char **buff)
2N/A{
2N/A au_asid_t session_id;
2N/A size_t length;
2N/A /*
2N/A * output is 0x followed by
2N/A * two characters per byte
2N/A * plus terminator,
2N/A * except leading 0's are suppressed, so a few bytes may
2N/A * be unused.
2N/A */
2N/A length = 2 + (2 * sizeof (session_id)) + 1;
2N/A *buff = malloc(length);
2N/A
2N/A if (*buff == NULL) {
2N/A return (0);
2N/A }
2N/A if (session_data == NULL) { /* NULL is not an error */
2N/A **buff = '\0';
2N/A return (1);
2N/A }
2N/A adt_get_asid(session_data, &session_id);
2N/A
2N/A length = snprintf(*buff, length, "0x%X", (int)session_id);
2N/A
2N/A /* length < 1 is a bug: the session data type may have changed */
2N/A assert(length > 0);
2N/A
2N/A return (length);
2N/A}
2N/A
2N/A/*
2N/A * adt_end_session -- close handle, clear context
2N/A *
2N/A * if as_check is invalid, no harm, no foul, EXCEPT that this could
2N/A * be an attempt to free data already free'd, so output to syslog
2N/A * to help explain why the process cored dumped.
2N/A */
2N/A
2N/Aint
2N/Aadt_end_session(adt_session_data_t *session_data)
2N/A{
2N/A adt_internal_state_t *state;
2N/A
2N/A if (session_data != NULL) {
2N/A state = (adt_internal_state_t *)session_data;
2N/A if (state->as_check != ADT_VALID) {
2N/A adt_write_syslog("freeing invalid data", EINVAL);
2N/A } else {
2N/A state->as_check = 0;
2N/A m_label_free(state->as_label);
2N/A free(session_data);
2N/A }
2N/A }
2N/A /* no errors yet defined */
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * adt_dup_session -- copy the session data
2N/A */
2N/A
2N/Aint
2N/Aadt_dup_session(const adt_session_data_t *source, adt_session_data_t **dest)
2N/A{
2N/A adt_internal_state_t *source_state;
2N/A adt_internal_state_t *dest_state = NULL;
2N/A int rc = 0;
2N/A
2N/A if (source != NULL) {
2N/A source_state = (adt_internal_state_t *)source;
2N/A assert(source_state->as_check == ADT_VALID);
2N/A
2N/A dest_state = malloc(sizeof (adt_internal_state_t));
2N/A if (dest_state == NULL) {
2N/A rc = -1;
2N/A goto return_rc;
2N/A }
2N/A (void) memcpy(dest_state, source,
2N/A sizeof (struct adt_internal_state));
2N/A
2N/A if (source_state->as_label != NULL) {
2N/A dest_state->as_label = NULL;
2N/A if ((rc = m_label_dup(&dest_state->as_label,
2N/A source_state->as_label)) != 0) {
2N/A free(dest_state);
2N/A dest_state = NULL;
2N/A }
2N/A }
2N/A }
2N/Areturn_rc:
2N/A *dest = (adt_session_data_t *)dest_state;
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * from_export_format()
2N/A * read from a network order buffer into struct adt_session_data
2N/A */
2N/A
2N/Astatic size_t
2N/Aadt_from_export_format(adt_internal_state_t *internal,
2N/A const adt_export_data_t *external)
2N/A{
2N/A struct export_header head;
2N/A struct export_link link;
2N/A adr_t context;
2N/A int32_t offset;
2N/A int32_t length;
2N/A int32_t version;
2N/A size_t label_len;
2N/A char *p = (char *)external;
2N/A
2N/A adrm_start(&context, (char *)external);
2N/A adrm_int32(&context, (int *)&head, 4);
2N/A
2N/A if ((internal->as_check = head.ax_check) != ADT_VALID) {
2N/A errno = EINVAL;
2N/A return (0);
2N/A }
2N/A offset = head.ax_link.ax_offset;
2N/A version = head.ax_link.ax_version;
2N/A length = head.ax_buffer_length;
2N/A
2N/A /*
2N/A * Skip newer versions.
2N/A */
2N/A while (version > PROTOCOL_VERSION_2) {
2N/A if (offset < 1) {
2N/A return (0); /* failed to match version */
2N/A }
2N/A p += offset; /* point to next version # */
2N/A
2N/A if (p > (char *)external + length) {
2N/A return (0);
2N/A }
2N/A adrm_start(&context, p);
2N/A adrm_int32(&context, (int *)&link, 2);
2N/A offset = link.ax_offset;
2N/A version = link.ax_version;
2N/A assert(version != 0);
2N/A }
2N/A /*
2N/A * Adjust buffer pointer to the first data item (euid).
2N/A */
2N/A if (p == (char *)external) {
2N/A adrm_start(&context, (char *)(p + sizeof (head)));
2N/A } else {
2N/A adrm_start(&context, (char *)(p + sizeof (link)));
2N/A }
2N/A /*
2N/A * if down rev version, neither pid nor label are included
2N/A * in v1 ax_size_of_tsol_data intentionally ignored
2N/A */
2N/A if (version == PROTOCOL_VERSION_1) {
2N/A adrm_int32(&context, (int *)&(internal->as_euid), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_ruid), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_egid), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_rgid), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
2N/A adrm_int32(&context,
2N/A (int *)&(internal->as_info.ai_mask.am_success), 1);
2N/A adrm_int32(&context,
2N/A (int *)&(internal->as_info.ai_mask.am_failure), 1);
2N/A adrm_int32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_port), 1);
2N/A adrm_int32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_type), 1);
2N/A adrm_int32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
2N/A adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_audit_state), 1);
2N/A internal->as_pid = (pid_t)-1;
2N/A internal->as_label = NULL;
2N/A } else if (version == PROTOCOL_VERSION_2) {
2N/A adrm_int32(&context, (int *)&(internal->as_euid), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_ruid), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_egid), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_rgid), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
2N/A adrm_int64(&context,
2N/A (int64_t *)&(internal->as_info.ai_mask.am_success), 2);
2N/A adrm_int32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_port), 1);
2N/A adrm_int32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_type), 1);
2N/A adrm_int32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
2N/A adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_audit_state), 1);
2N/A adrm_int32(&context, (int *)&(internal->as_pid), 1);
2N/A adrm_int32(&context, (int *)&label_len, 1);
2N/A if (label_len > 0) {
2N/A /* read in and deal with different sized labels. */
2N/A size32_t my_label_len = blabel_size();
2N/A
2N/A if ((internal->as_label =
2N/A m_label_alloc(MAC_LABEL)) == NULL) {
2N/A return (0);
2N/A }
2N/A if (label_len > my_label_len) {
2N/A errno = EINVAL;
2N/A m_label_free(internal->as_label);
2N/A return (0);
2N/A }
2N/A (void) memset(internal->as_label, 0, my_label_len);
2N/A adrm_int32(&context, (int *)(internal->as_label),
2N/A label_len / sizeof (int32_t));
2N/A } else {
2N/A internal->as_label = NULL;
2N/A }
2N/A }
2N/A
2N/A return (length);
2N/A}
2N/A
2N/A/*
2N/A * adt_to_export_format
2N/A * read from struct adt_session_data into a network order buffer.
2N/A *
2N/A * (network order 'cause this data may be shared with a remote host.)
2N/A */
2N/A
2N/Astatic size_t
2N/Aadt_to_export_format(adt_export_data_t *external,
2N/A adt_internal_state_t *internal)
2N/A{
2N/A struct export_header head;
2N/A struct export_link tail;
2N/A adr_t context;
2N/A size32_t label_len = 0;
2N/A uint32_t mask;
2N/A
2N/A adrm_start(&context, (char *)external);
2N/A
2N/A if (internal->as_label != NULL) {
2N/A label_len = blabel_size();
2N/A }
2N/A
2N/A head.ax_check = ADT_VALID;
2N/A head.ax_buffer_length = sizeof (struct adt_export_data) + label_len;
2N/A
2N/A /* version 2 first */
2N/A
2N/A head.ax_link.ax_version = PROTOCOL_VERSION_2;
2N/A head.ax_link.ax_offset = sizeof (struct export_header) +
2N/A sizeof (struct adt_export_v2) + label_len;
2N/A
2N/A adrm_putint32(&context, (int *)&head, 4);
2N/A
2N/A adrm_putint32(&context, (int *)&(internal->as_euid), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_egid), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
2N/A adrm_putint64(&context,
2N/A (int64_t *)&(internal->as_info.ai_mask.am_success), 2);
2N/A adrm_putint32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_port), 1);
2N/A adrm_putint32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_type), 1);
2N/A adrm_putint32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
2N/A adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_audit_state), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_pid), 1);
2N/A adrm_putint32(&context, (int *)&label_len, 1);
2N/A if (internal->as_label != NULL) {
2N/A /* serialize the label */
2N/A adrm_putint32(&context, (int *)(internal->as_label),
2N/A (label_len / sizeof (int32_t)));
2N/A }
2N/A
2N/A /* now version 1 */
2N/A
2N/A tail.ax_version = PROTOCOL_VERSION_1;
2N/A tail.ax_offset = 0;
2N/A
2N/A adrm_putint32(&context, (int *)&tail, 2);
2N/A
2N/A adrm_putint32(&context, (int *)&(internal->as_euid), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_egid), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
2N/A mask = (uint32_t)(internal->as_info.ai_mask.am_success &
2N/A UINT64_C(0xFFFFFFFF));
2N/A adrm_putint32(&context, (int *)&mask, 1);
2N/A mask = (uint32_t)(internal->as_info.ai_mask.am_failure &
2N/A UINT64_C(0xFFFFFFFF));
2N/A adrm_putint32(&context, (int *)&mask, 1);
2N/A adrm_putint32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_port), 1);
2N/A adrm_putint32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_type), 1);
2N/A adrm_putint32(&context,
2N/A (int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
2N/A adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
2N/A adrm_putint32(&context, (int *)&(internal->as_audit_state), 1);
2N/A /* ignored in v1 */
2N/A adrm_putint32(&context, (int *)&label_len, 1);
2N/A
2N/A /* finally terminator */
2N/A
2N/A tail.ax_version = 0; /* invalid version number */
2N/A tail.ax_offset = 0;
2N/A
2N/A adrm_putint32(&context, (int *)&tail, 2);
2N/A
2N/A return (head.ax_buffer_length);
2N/A}
2N/A
2N/A/*
2N/A * adt_ucred_label() -- if label is available, duplicate it.
2N/A */
2N/A
2N/Astatic m_label_t *
2N/Aadt_ucred_label(ucred_t *uc)
2N/A{
2N/A m_label_t *ul = NULL;
2N/A
2N/A if (ucred_getlabel(uc) != NULL) {
2N/A (void) m_label_dup(&ul, ucred_getlabel(uc));
2N/A }
2N/A
2N/A return (ul);
2N/A}
2N/A
2N/A/*
2N/A * adt_import() -- convert from network order to machine-specific order
2N/A */
2N/A
2N/Astatic int
2N/Aadt_import(adt_internal_state_t *internal, const adt_export_data_t *external)
2N/A{
2N/A au_mask_t mask;
2N/A
2N/A /* save local audit state */
2N/A int local_audit_state = internal->as_audit_state;
2N/A
2N/A if (adt_from_export_format(internal, external) < 1)
2N/A return (-1); /* errno from adt_from_export_format */
2N/A
2N/A /*
2N/A * If audit isn't enabled on the remote, they were unable
2N/A * to generate the audit mask, so generate it based on
2N/A * local configuration. If the user id has changed, the
2N/A * resulting mask may miss some subtleties that occurred
2N/A * on the remote system.
2N/A *
2N/A * If the remote failed to generate a terminal id, it is not
2N/A * recoverable.
2N/A */
2N/A
2N/A if (!(internal->as_audit_state & AUC_DISABLED)) {
2N/A if (adt_get_mask_from_user(internal->as_info.ai_auid,
2N/A &(internal->as_info.ai_mask)))
2N/A return (-1);
2N/A if (internal->as_info.ai_auid != internal->as_ruid) {
2N/A if (adt_get_mask_from_user(internal->as_info.ai_auid,
2N/A &mask))
2N/A return (-1);
2N/A internal->as_info.ai_mask.am_success |=
2N/A mask.am_success;
2N/A internal->as_info.ai_mask.am_failure |=
2N/A mask.am_failure;
2N/A }
2N/A }
2N/A internal->as_audit_state = local_audit_state;
2N/A
2N/A DPRINTF(("(%lld)imported asid = %X %u\n", (long long) getpid(),
2N/A internal->as_info.ai_asid,
2N/A internal->as_info.ai_asid));
2N/A
2N/A internal->as_have_user_data = ADT_HAVE_ALL;
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * adt_export_session_data()
2N/A * copies a adt_session_data struct into a network order buffer
2N/A *
2N/A * In a misconfigured network, the local host may have auditing
2N/A * off while the destination may have auditing on, so if there
2N/A * is sufficient memory, a buffer will be returned even in the
2N/A * audit off case.
2N/A */
2N/A
2N/Asize_t
2N/Aadt_export_session_data(const adt_session_data_t *internal,
2N/A adt_export_data_t **external)
2N/A{
2N/A size32_t length = 0;
2N/A
2N/A if ((internal != NULL) &&
2N/A ((adt_internal_state_t *)internal)->as_label != NULL) {
2N/A length = blabel_size();
2N/A }
2N/A
2N/A *external = malloc(sizeof (adt_export_data_t) + length);
2N/A
2N/A if (*external == NULL)
2N/A return (0);
2N/A
2N/A if (internal == NULL) {
2N/A adt_internal_state_t *dummy;
2N/A
2N/A dummy = malloc(sizeof (adt_internal_state_t));
2N/A if (dummy == NULL)
2N/A goto return_length_free;
2N/A
2N/A if (adt_init(dummy, 0)) { /* 0 == don't copy from proc */
2N/A free(dummy);
2N/A goto return_length_free;
2N/A }
2N/A length = adt_to_export_format(*external, dummy);
2N/A free(dummy);
2N/A } else {
2N/A length = adt_to_export_format(*external,
2N/A (adt_internal_state_t *)internal);
2N/A }
2N/A return (length);
2N/A
2N/Areturn_length_free:
2N/A free(*external);
2N/A *external = NULL;
2N/A return (0);
2N/A}
2N/A
2N/Astatic void
2N/Aadt_setto_unaudited(adt_internal_state_t *state)
2N/A{
2N/A if (state->as_audit_state & AUC_DISABLED) {
2N/A state->as_ruid = AU_NOAUDITID;
2N/A state->as_euid = AU_NOAUDITID;
2N/A state->as_rgid = AU_NOAUDITID;
2N/A state->as_egid = AU_NOAUDITID;
2N/A state->as_pid = (pid_t)-1;
2N/A state->as_label = NULL;
2N/A } else {
2N/A state->as_info.ai_asid = 0;
2N/A state->as_info.ai_auid = AU_NOAUDITID;
2N/A
2N/A (void) memset((void *)&(state->as_info.ai_termid), 0,
2N/A sizeof (au_tid_addr_t));
2N/A state->as_info.ai_termid.at_type = AU_IPv4;
2N/A
2N/A (void) memset((void *)&(state->as_info.ai_mask), 0,
2N/A sizeof (au_mask_t));
2N/A state->as_have_user_data = 0;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * adt_init -- set session context by copying the audit characteristics
2N/A * from the proc and picking up current uid/tid information.
2N/A *
2N/A * By default, an audit session is based on the process; the default
2N/A * is overriden by adt_set_user()
2N/A */
2N/A
2N/Astatic int
2N/Aadt_init(adt_internal_state_t *state, int use_proc_data)
2N/A{
2N/A /* ensure auditstate is set */
2N/A
2N/A (void) adt_audit_state(0);
2N/A state->as_audit_state = auditstate;
2N/A
2N/A if (use_proc_data) {
2N/A state->as_ruid = getuid();
2N/A state->as_euid = geteuid();
2N/A state->as_rgid = getgid();
2N/A state->as_egid = getegid();
2N/A state->as_pid = getpid();
2N/A
2N/A if (!(state->as_audit_state & AUC_DISABLED)) {
2N/A const au_tid64_addr_t *tid;
2N/A const au_mask32_t *mask;
2N/A ucred_t *ucred = ucred_get(P_MYID);
2N/A
2N/A /*
2N/A * Even if the ucred is NULL, the underlying
2N/A * credential may have a valid terminal id; if the
2N/A * terminal id is set, then that's good enough. An
2N/A * example of where this matters is failed login,
2N/A * where rlogin/telnet sets the terminal id before
2N/A * calling login; login does not load the credential
2N/A * since auth failed.
2N/A */
2N/A if (ucred == NULL) {
2N/A if (!adt_have_termid(
2N/A &(state->as_info.ai_termid)))
2N/A return (-1);
2N/A } else {
2N/A mask = ucred_getamask(ucred);
2N/A if (mask != NULL) {
2N/A state->as_info.ai_mask.as_success =
2N/A AU_CLASS_64(mask->am_success_lo,
2N/A mask->am_success_hi);
2N/A state->as_info.ai_mask.as_failure =
2N/A AU_CLASS_64(mask->am_failure_lo,
2N/A mask->am_failure_hi);
2N/A } else {
2N/A ucred_free(ucred);
2N/A return (-1);
2N/A }
2N/A tid = ucred_getatid(ucred);
2N/A if (tid != NULL) {
2N/A adt_cpy_tid(&(state->as_info.ai_termid),
2N/A tid);
2N/A } else {
2N/A ucred_free(ucred);
2N/A return (-1);
2N/A }
2N/A state->as_info.ai_asid = ucred_getasid(ucred);
2N/A state->as_info.ai_auid = ucred_getauid(ucred);
2N/A state->as_label = adt_ucred_label(ucred);
2N/A ucred_free(ucred);
2N/A }
2N/A state->as_have_user_data = ADT_HAVE_ALL;
2N/A }
2N/A } else {
2N/A adt_setto_unaudited(state);
2N/A }
2N/A state->as_session_model = ADT_SESSION_MODEL; /* default */
2N/A
2N/A if ((state->as_audit_state & AUC_AUDITING) &&
2N/A auditon(A_GETPOLICY, (caddr_t)&(state->as_kernel_audit_policy),
2N/A sizeof (state->as_kernel_audit_policy))) {
2N/A return (-1); /* errno set by auditon */
2N/A }
2N/A state->as_check = ADT_VALID;
2N/A adt_load_table((adt_session_data_t *)state, &adt_xlate_table[0],
2N/A &adt_preload);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * adt_set_proc
2N/A *
2N/A * Copy the current session state to the process. If this function
2N/A * is called, the model becomes a process model rather than a
2N/A * session model.
2N/A *
2N/A * In the current implementation, the value state->as_have_user_data
2N/A * must contain all of: ADT_HAVE_{AUID,MASK,TID,ASID}. These are all set
2N/A * by adt_set_user() when the ADT_SETTID or ADT_NEW flag is passed in.
2N/A *
2N/A */
2N/A
2N/Aint
2N/Aadt_set_proc(const adt_session_data_t *session_data)
2N/A{
2N/A adt_internal_state_t *state;
2N/A
2N/A if (session_data == NULL) {
2N/A return (0);
2N/A }
2N/A
2N/A state = (adt_internal_state_t *)session_data;
2N/A
2N/A assert(state->as_check == ADT_VALID);
2N/A
2N/A if ((state->as_have_user_data & (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) !=
2N/A (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) {
2N/A errno = EINVAL;
2N/A goto return_err;
2N/A }
2N/A
2N/A if (setaudit_addr((auditinfo_addr_t *)&(state->as_info),
2N/A sizeof (auditinfo_addr_t)) < 0) {
2N/A goto return_err; /* errno set by setaudit_addr() */
2N/A }
2N/A
2N/A state->as_session_model = ADT_PROCESS_MODEL;
2N/A
2N/A return (0);
2N/A
2N/Areturn_err:
2N/A adt_write_syslog("failed to set process audit characteristics", errno);
2N/A return (-1);
2N/A}
2N/A
2N/Astatic int
2N/Aadt_newuser(adt_internal_state_t *state, uid_t ruid, au_tid_addr_t *termid)
2N/A{
2N/A au_tid_addr_t no_tid = {0, AU_IPv4, 0, 0, 0, 0};
2N/A au_mask_t no_mask = {0, 0};
2N/A
2N/A if (ruid == ADT_NO_AUDIT) {
2N/A state->as_info.ai_auid = AU_NOAUDITID;
2N/A state->as_info.ai_asid = 0;
2N/A state->as_info.ai_termid = no_tid;
2N/A state->as_info.ai_mask = no_mask;
2N/A return (0);
2N/A }
2N/A state->as_info.ai_auid = ruid;
2N/A state->as_info.ai_asid = adt_get_unique_id(ruid);
2N/A if (termid != NULL)
2N/A state->as_info.ai_termid = *termid;
2N/A
2N/A if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask)))
2N/A return (-1);
2N/A
2N/A /* Assume intending to audit as this process */
2N/A
2N/A if (state->as_pid == (pid_t)-1)
2N/A state->as_pid = getpid();
2N/A
2N/A if (is_system_labeled() && state->as_label == NULL) {
2N/A ucred_t *ucred = ucred_get(P_MYID);
2N/A
2N/A state->as_label = adt_ucred_label(ucred);
2N/A ucred_free(ucred);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Aadt_changeuser(adt_internal_state_t *state, uid_t ruid)
2N/A{
2N/A au_mask_t mask;
2N/A
2N/A if (!(state->as_have_user_data & ADT_HAVE_AUID))
2N/A state->as_info.ai_auid = ruid;
2N/A if (!(state->as_have_user_data & ADT_HAVE_ASID))
2N/A state->as_info.ai_asid = adt_get_unique_id(ruid);
2N/A
2N/A if (ruid <= MAXEPHUID) {
2N/A if (adt_get_mask_from_user(ruid, &mask))
2N/A return (-1);
2N/A
2N/A state->as_info.ai_mask.am_success |= mask.am_success;
2N/A state->as_info.ai_mask.am_failure |= mask.am_failure;
2N/A }
2N/A DPRINTF(("changed mask to %08llX/%08llX for ruid=%d\n",
2N/A state->as_info.ai_mask.am_success,
2N/A state->as_info.ai_mask.am_failure,
2N/A ruid));
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * adt_set_user -- see also adt_set_from_ucred()
2N/A *
2N/A * ADT_NO_ATTRIB is a valid uid/gid meaning "not known" or
2N/A * "unattributed." If ruid, change the model to session.
2N/A *
2N/A * ADT_NO_CHANGE is a valid uid/gid meaning "do not change this value"
2N/A * only valid with ADT_UPDATE.
2N/A *
2N/A * ADT_NO_AUDIT is the external equivalent to AU_NOAUDITID -- there
2N/A * isn't a good reason to call adt_set_user() with it unless you don't
2N/A * have a good value yet and intend to replace it later; auid will be
2N/A * AU_NOAUDITID.
2N/A *
2N/A * adt_set_user should be called even if auditing is not enabled
2N/A * so that adt_export_session_data() will have useful stuff to
2N/A * work with.
2N/A *
2N/A * See the note preceding adt_set_proc() about the use of ADT_HAVE_TID
2N/A * and ADT_HAVE_ALL.
2N/A */
2N/A
2N/Aint
2N/Aadt_set_user(const adt_session_data_t *session_data, uid_t euid, gid_t egid,
2N/A uid_t ruid, gid_t rgid, const adt_termid_t *termid,
2N/A enum adt_user_context user_context)
2N/A{
2N/A adt_internal_state_t *state;
2N/A int rc;
2N/A
2N/A if (session_data == NULL) /* no session exists to audit */
2N/A return (0);
2N/A
2N/A state = (adt_internal_state_t *)session_data;
2N/A assert(state->as_check == ADT_VALID);
2N/A
2N/A switch (user_context) {
2N/A case ADT_NEW:
2N/A if (ruid == ADT_NO_CHANGE || euid == ADT_NO_CHANGE ||
2N/A rgid == ADT_NO_CHANGE || egid == ADT_NO_CHANGE) {
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A if ((rc = adt_newuser(state, ruid,
2N/A (au_tid_addr_t *)termid)) != 0)
2N/A return (rc);
2N/A
2N/A state->as_have_user_data = ADT_HAVE_ALL;
2N/A break;
2N/A case ADT_UPDATE:
2N/A if (state->as_have_user_data != ADT_HAVE_ALL) {
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A
2N/A if (ruid != ADT_NO_CHANGE)
2N/A if ((rc = adt_changeuser(state, ruid)) != 0)
2N/A return (rc);
2N/A break;
2N/A case ADT_USER:
2N/A if (state->as_have_user_data != ADT_HAVE_ALL) {
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A break;
2N/A case ADT_SETTID:
2N/A assert(termid != NULL);
2N/A state->as_info.ai_termid = *((au_tid_addr_t *)termid);
2N/A /* avoid fooling pam_setcred()... */
2N/A state->as_info.ai_auid = AU_NOAUDITID;
2N/A state->as_info.ai_asid = 0;
2N/A state->as_info.ai_mask.am_failure = AU_MASK_NONE;
2N/A state->as_info.ai_mask.am_success = AU_MASK_NONE;
2N/A state->as_have_user_data = ADT_HAVE_TID |
2N/A ADT_HAVE_AUID | ADT_HAVE_ASID | ADT_HAVE_MASK;
2N/A return (0);
2N/A default:
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A
2N/A if (ruid == ADT_NO_AUDIT) {
2N/A state->as_ruid = AU_NOAUDITID;
2N/A state->as_euid = AU_NOAUDITID;
2N/A state->as_rgid = AU_NOAUDITID;
2N/A state->as_egid = AU_NOAUDITID;
2N/A } else {
2N/A if (ruid != ADT_NO_CHANGE)
2N/A state->as_ruid = ruid;
2N/A if (euid != ADT_NO_CHANGE)
2N/A state->as_euid = euid;
2N/A if (rgid != ADT_NO_CHANGE)
2N/A state->as_rgid = rgid;
2N/A if (egid != ADT_NO_CHANGE)
2N/A state->as_egid = egid;
2N/A }
2N/A
2N/A if (ruid == ADT_NO_ATTRIB) {
2N/A state->as_session_model = ADT_SESSION_MODEL;
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * adt_set_from_ucred()
2N/A *
2N/A * an alternate to adt_set_user that fills the same role but uses
2N/A * a pointer to a ucred rather than a list of id's. If the ucred
2N/A * pointer is NULL, use the credential from the this process.
2N/A *
2N/A * A key difference is that for ADT_NEW, adt_set_from_ucred() does
2N/A * not overwrite the asid and auid unless auid has not been set.
2N/A * ADT_NEW differs from ADT_UPDATE in that it does not OR together
2N/A * the incoming audit mask with the one that already exists.
2N/A *
2N/A * adt_set_from_ucred should be called even if auditing is not enabled
2N/A * so that adt_export_session_data() will have useful stuff to
2N/A * work with.
2N/A */
2N/A
2N/Aint
2N/Aadt_set_from_ucred(const adt_session_data_t *session_data, const ucred_t *uc,
2N/A enum adt_user_context user_context)
2N/A{
2N/A adt_internal_state_t *state;
2N/A int rc = -1;
2N/A const au_tid64_addr_t *tid64;
2N/A au_tid_addr_t termid, *tid;
2N/A ucred_t *ucred = (ucred_t *)uc;
2N/A boolean_t local_uc = B_FALSE;
2N/A
2N/A if (session_data == NULL) /* no session exists to audit */
2N/A return (0);
2N/A
2N/A state = (adt_internal_state_t *)session_data;
2N/A assert(state->as_check == ADT_VALID);
2N/A
2N/A if (ucred == NULL) {
2N/A ucred = ucred_get(P_MYID);
2N/A
2N/A if (ucred == NULL)
2N/A goto return_rc;
2N/A local_uc = B_TRUE;
2N/A }
2N/A
2N/A switch (user_context) {
2N/A case ADT_NEW:
2N/A tid64 = ucred_getatid(ucred);
2N/A if (tid64 != NULL) {
2N/A adt_cpy_tid(&termid, tid64);
2N/A tid = &termid;
2N/A } else {
2N/A tid = NULL;
2N/A }
2N/A if (ucred_getauid(ucred) == AU_NOAUDITID) {
2N/A adt_setto_unaudited(state);
2N/A state->as_have_user_data = ADT_HAVE_ALL;
2N/A rc = 0;
2N/A goto return_rc;
2N/A } else {
2N/A au_mask32_t m;
2N/A
2N/A state->as_info.ai_auid = ucred_getauid(ucred);
2N/A state->as_info.ai_asid = ucred_getasid(ucred);
2N/A m = *ucred_getamask(ucred);
2N/A state->as_info.ai_mask.as_success =
2N/A AU_CLASS_64(m.am_success_lo, m.am_success_hi);
2N/A state->as_info.ai_mask.as_failure =
2N/A AU_CLASS_64(m.am_failure_lo, m.am_failure_hi);
2N/A state->as_info.ai_termid = *tid;
2N/A }
2N/A state->as_have_user_data = ADT_HAVE_ALL;
2N/A break;
2N/A case ADT_UPDATE:
2N/A if (state->as_have_user_data != ADT_HAVE_ALL) {
2N/A errno = EINVAL;
2N/A goto return_rc;
2N/A }
2N/A
2N/A if ((rc = adt_changeuser(state, ucred_getruid(ucred))) != 0)
2N/A goto return_rc;
2N/A break;
2N/A case ADT_USER:
2N/A if (state->as_have_user_data != ADT_HAVE_ALL) {
2N/A errno = EINVAL;
2N/A goto return_rc;
2N/A }
2N/A break;
2N/A default:
2N/A errno = EINVAL;
2N/A goto return_rc;
2N/A }
2N/A rc = 0;
2N/A
2N/A state->as_ruid = ucred_getruid(ucred);
2N/A state->as_euid = ucred_geteuid(ucred);
2N/A state->as_rgid = ucred_getrgid(ucred);
2N/A state->as_egid = ucred_getegid(ucred);
2N/A state->as_pid = ucred_getpid(ucred);
2N/A state->as_label = adt_ucred_label(ucred);
2N/A
2N/Areturn_rc:
2N/A if (local_uc) {
2N/A ucred_free(ucred);
2N/A }
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * adt_alloc_event() returns a pointer to allocated memory
2N/A *
2N/A */
2N/A
2N/Aadt_event_data_t
2N/A*adt_alloc_event(const adt_session_data_t *session_data, au_event_t event_id)
2N/A{
2N/A struct adt_event_state *event_state;
2N/A adt_internal_state_t *session_state;
2N/A adt_event_data_t *return_event = NULL;
2N/A /*
2N/A * need to return a valid event pointer even if audit is
2N/A * off, else the caller will end up either (1) keeping its
2N/A * own flags for on/off or (2) writing to a NULL pointer.
2N/A * If auditing is on, the session data must be valid; otherwise
2N/A * we don't care.
2N/A */
2N/A if (session_data != NULL) {
2N/A session_state = (adt_internal_state_t *)session_data;
2N/A assert(session_state->as_check == ADT_VALID);
2N/A }
2N/A event_state = calloc(1, sizeof (struct adt_event_state));
2N/A if (event_state == NULL)
2N/A goto return_ptr;
2N/A
2N/A event_state->ae_check = ADT_VALID;
2N/A
2N/A event_state->ae_event_id = event_id;
2N/A event_state->ae_session = (struct adt_internal_state *)session_data;
2N/A
2N/A return_event = (adt_event_data_t *)&(event_state->ae_event_data);
2N/A
2N/A /*
2N/A * preload data so the adt_au_*() functions can detect un-supplied
2N/A * values (0 and NULL are free via calloc()).
2N/A */
2N/A if (session_data != NULL) {
2N/A session_state->as_preload(event_id, return_event);
2N/A }
2N/A
2N/Areturn_ptr:
2N/A return (return_event);
2N/A}
2N/A
2N/A/*
2N/A * adt_getXlateTable -- look up translation table address for event id
2N/A */
2N/A
2N/Astatic adt_translation_t *
2N/Aadt_getXlateTable(adt_translation_t **xlate, au_event_t event_id)
2N/A{
2N/A /* xlate_table is global in adt_xlate.c */
2N/A adt_translation_t **p_xlate = xlate;
2N/A adt_translation_t *p_event;
2N/A
2N/A while (*p_xlate != NULL) {
2N/A p_event = *p_xlate;
2N/A if (event_id == p_event->tx_external_event)
2N/A return (p_event);
2N/A p_xlate++;
2N/A }
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * adt_calcOffsets
2N/A *
2N/A * the call to this function is surrounded by a mutex.
2N/A *
2N/A * i walks down the table picking up next_token. j walks again to
2N/A * calculate the offset to the input data. k points to the next
2N/A * token's row. Finally, l, is used to sum the values in the
2N/A * datadef array.
2N/A *
2N/A * What's going on? The entry array is in the order of the input
2N/A * fields but the processing of array entries is in the order of
2N/A * the output (see next_token). Calculating the offset to the
2N/A * "next" input can't be done in the outer loop (i) since i doesn't
2N/A * point to the current entry and it can't be done with the k index
2N/A * because it doesn't represent the order of input fields.
2N/A *
2N/A * While the resulting algorithm is n**2, it is only done once per
2N/A * event type.
2N/A */
2N/A
2N/A/*
2N/A * adt_calcOffsets is only called once per event type, but it uses
2N/A * the address alignment of memory allocated for that event as if it
2N/A * were the same for all subsequently allocated memory. This is
2N/A * guaranteed by calloc/malloc. Arrays take special handling since
2N/A * what matters for figuring out the correct alignment is the size
2N/A * of the array element.
2N/A */
2N/A
2N/Astatic void
2N/Aadt_calcOffsets(struct entry *p_entry, int tablesize, void *p_data)
2N/A{
2N/A int i, j;
2N/A size_t this_size, prev_size;
2N/A void *struct_start = p_data;
2N/A
2N/A for (i = 0; i < tablesize; i++) {
2N/A if (p_entry[i].en_type_def == NULL) {
2N/A p_entry[i].en_offset = 0;
2N/A continue;
2N/A }
2N/A prev_size = 0;
2N/A p_entry[i].en_offset = (char *)p_data - (char *)struct_start;
2N/A
2N/A for (j = 0; j < p_entry[i].en_count_types; j++) {
2N/A if (p_entry[i].en_type_def[j].dd_datatype == ADT_MSG)
2N/A this_size = sizeof (enum adt_generic);
2N/A else
2N/A this_size =
2N/A p_entry[i].en_type_def[j].dd_input_size;
2N/A
2N/A /* adj for first entry */
2N/A if (prev_size == 0)
2N/A prev_size = this_size;
2N/A
2N/A if (p_entry[i].en_type_def[j].dd_datatype ==
2N/A ADT_UINT32ARRAY) {
2N/A p_data = (char *)adt_adjust_address(p_data,
2N/A prev_size, sizeof (uint32_t)) +
2N/A this_size - sizeof (uint32_t);
2N/A
2N/A prev_size = sizeof (uint32_t);
2N/A } else {
2N/A p_data = adt_adjust_address(p_data, prev_size,
2N/A this_size);
2N/A prev_size = this_size;
2N/A }
2N/A }
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * adt_generate_event
2N/A * generate event record from external struct. The order is based on
2N/A * the output tokens, allowing for the possibility that the input data
2N/A * is in a different order.
2N/A *
2N/A */
2N/A
2N/Astatic int
2N/Aadt_generate_event(const adt_event_data_t *p_extdata,
2N/A struct adt_event_state *p_event,
2N/A adt_translation_t *p_xlate)
2N/A{
2N/A struct entry *p_entry;
2N/A static mutex_t lock = DEFAULTMUTEX;
2N/A
2N/A p_entry = p_xlate->tx_first_entry;
2N/A assert(p_entry != NULL);
2N/A
2N/A p_event->ae_internal_id = p_xlate->tx_internal_event;
2N/A if (adt_token_open(p_event) != 0) {
2N/A /* can't get an audit record descriptor */
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * offsets are not pre-calculated; the initial offsets are all
2N/A * 0; valid offsets are >= 0. Offsets for no-input tokens such
2N/A * as subject are set to -1 by adt_calcOffset()
2N/A */
2N/A if (p_xlate->tx_offsetsCalculated == 0) {
2N/A (void) mutex_lock(&lock);
2N/A p_xlate->tx_offsetsCalculated = 1;
2N/A
2N/A adt_calcOffsets(p_xlate->tx_top_entry, p_xlate->tx_entries,
2N/A (void *)p_extdata);
2N/A (void) mutex_unlock(&lock);
2N/A }
2N/A while (p_entry != NULL) {
2N/A adt_generate_token(p_entry, (char *)p_extdata, p_event);
2N/A
2N/A p_entry = p_entry->en_next_token;
2N/A }
2N/A return (adt_token_close(p_event));
2N/A}
2N/A
2N/A/*
2N/A * adt_put_event -- main event generation function.
2N/A * The input "event" is the address of the struct containing
2N/A * event-specific data.
2N/A *
2N/A * However if auditing is off or the session handle
2N/A * is NULL, no attempt to write a record is made.
2N/A */
2N/A
2N/Aint
2N/Aadt_put_event(const adt_event_data_t *event, int status, int return_val)
2N/A{
2N/A struct adt_event_state *event_state;
2N/A adt_translation_t *xlate;
2N/A
2N/A if (event == NULL) {
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A event_state = (struct adt_event_state *)event;
2N/A
2N/A /* if this is a broken session or not auditing, exit */
2N/A if ((event_state->ae_session == NULL) ||
2N/A !(event_state->ae_session->as_audit_state & AUC_AUDITING)) {
2N/A return (0);
2N/A }
2N/A
2N/A assert(event_state->ae_check == ADT_VALID);
2N/A
2N/A event_state->ae_rc = status;
2N/A event_state->ae_type = return_val;
2N/A event_state->ae_emod |= status == ADT_SUCCESS ? 0 : PAD_FAILURE;
2N/A
2N/A /* look up the event */
2N/A
2N/A xlate = adt_getXlateTable(event_state->ae_session->as_xlate,
2N/A event_state->ae_event_id);
2N/A
2N/A if (xlate == NULL) {
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A DPRINTF(("got event %d\n", xlate->tx_internal_event));
2N/A
2N/A if (adt_selected(event_state, xlate->tx_internal_event, status)) {
2N/A return (adt_generate_event(event, event_state, xlate));
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * adt_free_event -- invalidate and free
2N/A */
2N/A
2N/Avoid
2N/Aadt_free_event(adt_event_data_t *event)
2N/A{
2N/A struct adt_event_state *event_state;
2N/A
2N/A if (event == NULL)
2N/A return;
2N/A
2N/A event_state = (struct adt_event_state *)event;
2N/A
2N/A assert(event_state->ae_check == ADT_VALID);
2N/A
2N/A event_state->ae_check = 0;
2N/A
2N/A free(event_state);
2N/A}
2N/A
2N/A/*
2N/A * adt_is_selected -- helper to adt_selected(), below.
2N/A *
2N/A * "sorf" is "success or fail" status; au_preselect compares
2N/A * that with success, fail, or both.
2N/A */
2N/A
2N/Astatic int
2N/Aadt_is_selected(au_event_t e, au_mask_t *m, int status, int mode)
2N/A{
2N/A return (au_preselect(e, m,
2N/A status == ADT_SUCCESS ? AU_PRS_SUCCESS : AU_PRS_FAILURE, mode));
2N/A}
2N/A
2N/A/*
2N/A * selected -- see if this event is preselected.
2N/A *
2N/A * if errors are encountered trying to check a preselection mask
2N/A * or look up a user name, the event is selected. Otherwise, the
2N/A * preselection mask is used for the job.
2N/A */
2N/A
2N/Astatic int
2N/Aadt_selected(struct adt_event_state *event, au_event_t actual_id, int status)
2N/A{
2N/A adt_internal_state_t *sp;
2N/A au_mask_t namask;
2N/A int mode;
2N/A int rc;
2N/A
2N/A sp = event->ae_session;
2N/A
2N/A if ((sp->as_have_user_data & ADT_HAVE_IDS) == 0) {
2N/A adt_write_syslog("No user data available", EINVAL);
2N/A return (1); /* default is "selected" */
2N/A }
2N/A
2N/A mode = (sp->as_session_model == ADT_SESSION_MODEL) ?
2N/A AU_PRS_USECACHE : AU_PRS_REREAD;
2N/A
2N/A /* non-attributable? */
2N/A if ((sp->as_info.ai_auid == AU_NOAUDITID) ||
2N/A (sp->as_info.ai_auid == ADT_NO_AUDIT) ||
2N/A (sp->as_info.ai_auid == ADT_NO_ATTRIB)) {
2N/A if (auditon(A_GETKMASK, (caddr_t)&namask,
2N/A sizeof (namask)) != 0) {
2N/A adt_write_syslog("auditon failure", errno);
2N/A return (1);
2N/A }
2N/A event->ae_emod |= PAD_NONATTR;
2N/A return (adt_is_selected(actual_id, &namask, status, mode));
2N/A }
2N/A if ((rc = adt_is_selected(actual_id, &(sp->as_info.ai_mask), status,
2N/A mode)) == 2) {
2N/A /* if forced preselection */
2N/A event->ae_emod |= PAD_NONATTR;
2N/A }
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * Can't map the host name to an IP address in
2N/A * adt_get_hostIP. Get something off an interface
2N/A * to act as the hosts IP address for auditing.
2N/A */
2N/A
2N/Astatic int
2N/Aadt_get_local_address(int family, struct ifaddrlist *al)
2N/A{
2N/A struct ifaddrlist *ifal;
2N/A char errbuf[ERRBUFSIZE] = "empty list";
2N/A int ifal_count;
2N/A int i;
2N/A
2N/A if ((ifal_count = ifaddrlist(&ifal, family, 0, errbuf)) < 0) {
2N/A int serrno = errno;
2N/A
2N/A __auditd_debug("adt_get_local_address couldn't get %d "
2N/A "addrlist %s: %s\n", family, errbuf, strerror(serrno));
2N/A errno = serrno;
2N/A return (-1);
2N/A }
2N/A
2N/A for (i = 0; i < ifal_count; i++) {
2N/A /*
2N/A * loopback always defined,
2N/A * even if there is no real address
2N/A */
2N/A if ((ifal[i].flags & (IFF_UP | IFF_LOOPBACK)) == IFF_UP) {
2N/A break;
2N/A }
2N/A }
2N/A if (i >= ifal_count) {
2N/A free(ifal);
2N/A /*
2N/A * Callers of adt_get_hostIP() can only return
2N/A * errno to their callers and eventually the application.
2N/A * Picked one that seemed least worse for saying no
2N/A * usable address for Audit terminal ID.
2N/A */
2N/A errno = ENETDOWN;
2N/A return (-1);
2N/A }
2N/A
2N/A *al = ifal[i];
2N/A free(ifal);
2N/A return (0);
2N/A}