17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 58c3c55e7Spaulson * Common Development and Distribution License (the "License"). 68c3c55e7Spaulson * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*7257d1b4Sraf 227c478bd9Sstevel@tonic-gate /* 235b7f77adStw21770 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <bsm/adt.h> 307c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h> 317c478bd9Sstevel@tonic-gate #include <assert.h> 327c478bd9Sstevel@tonic-gate #include <bsm/audit.h> 337c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h> 347c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h> 357c478bd9Sstevel@tonic-gate #include <door.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include <generic.h> 387c478bd9Sstevel@tonic-gate #include <md5.h> 397c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 407c478bd9Sstevel@tonic-gate #include <netdb.h> 417c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 427c478bd9Sstevel@tonic-gate #include <pwd.h> 437c478bd9Sstevel@tonic-gate #include <sys/stat.h> 447c478bd9Sstevel@tonic-gate #include <time.h> 457c478bd9Sstevel@tonic-gate #include <stdlib.h> 467c478bd9Sstevel@tonic-gate #include <string.h> 477c478bd9Sstevel@tonic-gate #include <synch.h> 487c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 497c478bd9Sstevel@tonic-gate #include <syslog.h> 507c478bd9Sstevel@tonic-gate #include <thread.h> 517c478bd9Sstevel@tonic-gate #include <unistd.h> 527c478bd9Sstevel@tonic-gate #include <adt_xlate.h> 537c478bd9Sstevel@tonic-gate #include <adt_ucred.h> 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate static int adt_selected(struct adt_event_state *, au_event_t, int); 567c478bd9Sstevel@tonic-gate static int adt_init(adt_internal_state_t *, int); 577c478bd9Sstevel@tonic-gate static int adt_import(adt_internal_state_t *, const adt_export_data_t *); 58c529a23fSgww static m_label_t *adt_ucred_label(ucred_t *); 59b3baaabfSgww static void adt_setto_unaudited(adt_internal_state_t *); 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #ifdef C2_DEBUG 627c478bd9Sstevel@tonic-gate #define DPRINTF(x) {printf x; } 637c478bd9Sstevel@tonic-gate #define DFLUSH fflush(stdout); 647c478bd9Sstevel@tonic-gate #else 657c478bd9Sstevel@tonic-gate #define DPRINTF(x) 667c478bd9Sstevel@tonic-gate #define DFLUSH 677c478bd9Sstevel@tonic-gate #endif 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate static int auditstate = AUC_DISABLED; /* default state */ 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* 727c478bd9Sstevel@tonic-gate * adt_write_syslog 737c478bd9Sstevel@tonic-gate * 747c478bd9Sstevel@tonic-gate * errors that are not the user's fault (bugs or whatever in 757c478bd9Sstevel@tonic-gate * the underlying audit code are noted in syslog.) 767c478bd9Sstevel@tonic-gate * 777c478bd9Sstevel@tonic-gate * Avoid calling adt_write_syslog for things that can happen 787c478bd9Sstevel@tonic-gate * at high volume. 797c478bd9Sstevel@tonic-gate * 807c478bd9Sstevel@tonic-gate * syslog's open (openlog) and close (closelog) are interesting; 817c478bd9Sstevel@tonic-gate * openlog *may* create a file descriptor and is optional. closelog 827c478bd9Sstevel@tonic-gate * *will* close any open file descriptors and is also optional. 837c478bd9Sstevel@tonic-gate * 847c478bd9Sstevel@tonic-gate * Since syslog may also be used by the calling application, the 857c478bd9Sstevel@tonic-gate * choice is to avoid openlog, which sets some otherwise useful 867c478bd9Sstevel@tonic-gate * parameters, and to embed "Solaris_audit" in the log message. 877c478bd9Sstevel@tonic-gate */ 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate void 907c478bd9Sstevel@tonic-gate adt_write_syslog(const char *message, int err) 917c478bd9Sstevel@tonic-gate { 927c478bd9Sstevel@tonic-gate int save_errno; 937c478bd9Sstevel@tonic-gate int mask_priority; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate save_errno = errno; 967c478bd9Sstevel@tonic-gate errno = err; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate DPRINTF(("syslog called: %s\n", message)); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate mask_priority = setlogmask(LOG_MASK(LOG_ALERT)); 1017c478bd9Sstevel@tonic-gate syslog(LOG_ALERT, "Solaris_audit %s: %m", message, err); 1027c478bd9Sstevel@tonic-gate (void) setlogmask(mask_priority); 1037c478bd9Sstevel@tonic-gate errno = save_errno; 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* 1077c478bd9Sstevel@tonic-gate * return true if audit is enabled. "Enabled" is any state 1087c478bd9Sstevel@tonic-gate * other than AUC_DISABLED. 1097c478bd9Sstevel@tonic-gate * 1107c478bd9Sstevel@tonic-gate * states are 1117c478bd9Sstevel@tonic-gate * AUC_INIT_AUDIT -- c2audit queuing enabled. 1127c478bd9Sstevel@tonic-gate * AUC_AUDITING -- up and running 1137c478bd9Sstevel@tonic-gate * AUC_DISABLED -- no audit subsystem loaded 1147c478bd9Sstevel@tonic-gate * AUC_UNSET -- early boot state 1157c478bd9Sstevel@tonic-gate * AUC_NOAUDIT -- subsystem loaded, turned off via 1167c478bd9Sstevel@tonic-gate * auditon(A_SETCOND...) 1177c478bd9Sstevel@tonic-gate * AUC_NOSPACE -- up and running, but log partitions are full 1187c478bd9Sstevel@tonic-gate * 1197c478bd9Sstevel@tonic-gate * For purpose of this API, anything but AUC_DISABLED or 1207c478bd9Sstevel@tonic-gate * AUC_UNSET is enabled; however one never actually sees 1217c478bd9Sstevel@tonic-gate * AUC_DISABLED since auditon returns EINVAL in that case. Any 1227c478bd9Sstevel@tonic-gate * auditon error is considered the same as EINVAL for our 1237c478bd9Sstevel@tonic-gate * purpose. auditstate is not changed by auditon if an error 1247c478bd9Sstevel@tonic-gate * is returned. 1257c478bd9Sstevel@tonic-gate */ 1267c478bd9Sstevel@tonic-gate 127b08d8a12Sgww /* 128b08d8a12Sgww * XXX this should probably be eliminated and adt_audit_state() replace it. 129b08d8a12Sgww * All the legitimate uses are to not fork a waiting process for 130b08d8a12Sgww * process exit processing, as in su, login, dtlogin. Other bogus 131b08d8a12Sgww * users are zoneadmd and init. 132b08d8a12Sgww * All but dtlogin are in ON, so we can do this without cross gate 133b08d8a12Sgww * synchronization. 134b08d8a12Sgww */ 135b08d8a12Sgww 1367c478bd9Sstevel@tonic-gate boolean_t 137b08d8a12Sgww adt_audit_enabled(void) 138b08d8a12Sgww { 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate (void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate)); 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate return (auditstate != AUC_DISABLED); 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* 146b08d8a12Sgww * See adt_audit_enabled() for state discussions. 147b08d8a12Sgww * The state parameter is a hedge until all the uses become clear. 148b08d8a12Sgww * Likely if adt_audit_enabled is brought internal to this file, 149b08d8a12Sgww * it can take a parameter discussing the state. 150b08d8a12Sgww */ 151b08d8a12Sgww 152b08d8a12Sgww boolean_t 153b08d8a12Sgww adt_audit_state(int state) 154b08d8a12Sgww { 155b08d8a12Sgww 156b08d8a12Sgww (void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate)); 157b08d8a12Sgww 158b08d8a12Sgww return (auditstate == state); 159b08d8a12Sgww } 160b08d8a12Sgww 161b08d8a12Sgww /* 1627c478bd9Sstevel@tonic-gate * The man page for getpwuid_r says the buffer must be big enough 1637c478bd9Sstevel@tonic-gate * or ERANGE will be returned, but offers no guidance for how big 1647c478bd9Sstevel@tonic-gate * the buffer should be or a way to calculate it. If you get 1657c478bd9Sstevel@tonic-gate * ERANGE, double pwd_buff's size. 1667c478bd9Sstevel@tonic-gate * 1677c478bd9Sstevel@tonic-gate * This may be called even when auditing is off. 1687c478bd9Sstevel@tonic-gate */ 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate #define NAFLAG_LEN 512 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate static int 1737c478bd9Sstevel@tonic-gate adt_get_mask_from_user(uid_t uid, au_mask_t *mask) 1747c478bd9Sstevel@tonic-gate { 1757c478bd9Sstevel@tonic-gate struct passwd pwd; 1767c478bd9Sstevel@tonic-gate char pwd_buff[NSS_BUFSIZ]; 1777c478bd9Sstevel@tonic-gate char naflag_buf[NAFLAG_LEN]; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate if (auditstate == AUC_DISABLED) { 1807c478bd9Sstevel@tonic-gate mask->am_success = 0; 1817c478bd9Sstevel@tonic-gate mask->am_failure = 0; 182f48205beScasper } else if (uid <= MAXUID) { 1837c478bd9Sstevel@tonic-gate if (getpwuid_r(uid, &pwd, pwd_buff, NSS_BUFSIZ) == NULL) { 1847c478bd9Sstevel@tonic-gate /* 1857c478bd9Sstevel@tonic-gate * getpwuid_r returns NULL without setting 1867c478bd9Sstevel@tonic-gate * errno if the user does not exist; only 1877c478bd9Sstevel@tonic-gate * if the input is the wrong length does it 1887c478bd9Sstevel@tonic-gate * set errno. 1897c478bd9Sstevel@tonic-gate */ 1907c478bd9Sstevel@tonic-gate if (errno != ERANGE) 1917c478bd9Sstevel@tonic-gate errno = EINVAL; 1927c478bd9Sstevel@tonic-gate return (-1); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate if (au_user_mask(pwd.pw_name, mask)) { 1957c478bd9Sstevel@tonic-gate errno = EFAULT; /* undetermined failure */ 1967c478bd9Sstevel@tonic-gate return (-1); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate } else if (getacna(naflag_buf, NAFLAG_LEN - 1) == 0) { 1997c478bd9Sstevel@tonic-gate if (getauditflagsbin(naflag_buf, mask)) 2007c478bd9Sstevel@tonic-gate return (-1); 2017c478bd9Sstevel@tonic-gate } else { 2027c478bd9Sstevel@tonic-gate return (-1); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate return (0); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate /* 2087c478bd9Sstevel@tonic-gate * adt_get_unique_id -- generate a hopefully unique 32 bit value 2097c478bd9Sstevel@tonic-gate * 2107c478bd9Sstevel@tonic-gate * there will be a follow up to replace this with the use of /dev/random 2117c478bd9Sstevel@tonic-gate * 2127c478bd9Sstevel@tonic-gate * An MD5 hash is taken on a buffer of 2137c478bd9Sstevel@tonic-gate * hostname . audit id . unix time . pid . count 2147c478bd9Sstevel@tonic-gate * 2157c478bd9Sstevel@tonic-gate * "count = noise++;" is subject to a race condition but I don't 2167c478bd9Sstevel@tonic-gate * see a need to put a lock around it. 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate 2195b7f77adStw21770 au_id_t 2205b7f77adStw21770 adt_get_unique_id(au_id_t uid) 2217c478bd9Sstevel@tonic-gate { 2227c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN]; 2237c478bd9Sstevel@tonic-gate union { 2247c478bd9Sstevel@tonic-gate au_id_t v[4]; 2257c478bd9Sstevel@tonic-gate unsigned char obuff[128/8]; 2267c478bd9Sstevel@tonic-gate } output; 2277c478bd9Sstevel@tonic-gate MD5_CTX context; 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate static int noise = 0; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate int count = noise++; 2327c478bd9Sstevel@tonic-gate time_t timebits = time(NULL); 2337c478bd9Sstevel@tonic-gate pid_t pidbits = getpid(); 2347c478bd9Sstevel@tonic-gate au_id_t retval = 0; 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate if (gethostname(hostname, MAXHOSTNAMELEN)) { 2377c478bd9Sstevel@tonic-gate adt_write_syslog("gethostname call failed", errno); 2387c478bd9Sstevel@tonic-gate (void) strncpy(hostname, "invalidHostName", MAXHOSTNAMELEN); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate while (retval == 0) { /* 0 is the only invalid result */ 2427c478bd9Sstevel@tonic-gate MD5Init(&context); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate MD5Update(&context, (unsigned char *)hostname, 2457c478bd9Sstevel@tonic-gate (unsigned int) strlen((const char *)hostname)); 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate MD5Update(&context, (unsigned char *) &uid, sizeof (uid_t)); 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate MD5Update(&context, 2507c478bd9Sstevel@tonic-gate (unsigned char *) &timebits, sizeof (time_t)); 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate MD5Update(&context, (unsigned char *) &pidbits, 2537c478bd9Sstevel@tonic-gate sizeof (pid_t)); 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate MD5Update(&context, (unsigned char *) &(count), sizeof (int)); 2567c478bd9Sstevel@tonic-gate MD5Final(output.obuff, &context); 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate retval = output.v[count % 4]; 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate return (retval); 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate /* 2647c478bd9Sstevel@tonic-gate * the following "port" function deals with the following issues: 2657c478bd9Sstevel@tonic-gate * 2667c478bd9Sstevel@tonic-gate * 1 the kernel and ucred deal with a dev_t as a 64 bit value made 2677c478bd9Sstevel@tonic-gate * up from a 32 bit major and 32 bit minor. 2687c478bd9Sstevel@tonic-gate * 2 User space deals with a dev_t as either the above 64 bit value 2697c478bd9Sstevel@tonic-gate * or a 32 bit value made from a 14 bit major and an 18 bit minor. 2707c478bd9Sstevel@tonic-gate * 3 The various audit interfaces (except ucred) pass the 32 or 2717c478bd9Sstevel@tonic-gate * 64 bit version depending the architecture of the userspace 2727c478bd9Sstevel@tonic-gate * application. If you get a port value from ucred and pass it 2737c478bd9Sstevel@tonic-gate * to the kernel via auditon(), it must be squeezed into a 32 2747c478bd9Sstevel@tonic-gate * bit value because the kernel knows the userspace app's bit 2757c478bd9Sstevel@tonic-gate * size. 2767c478bd9Sstevel@tonic-gate * 2777c478bd9Sstevel@tonic-gate * The internal state structure for adt (adt_internal_state_t) uses 2787c478bd9Sstevel@tonic-gate * dev_t, so adt converts data from ucred to fit. The import/export 2797c478bd9Sstevel@tonic-gate * functions, however, can't know if they are importing/exporting 2807c478bd9Sstevel@tonic-gate * from 64 or 32 bit applications, so they always send 64 bits and 2817c478bd9Sstevel@tonic-gate * the 32 bit end(s) are responsible to convert 32 -> 64 -> 32 as 2827c478bd9Sstevel@tonic-gate * appropriate. 2837c478bd9Sstevel@tonic-gate */ 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate /* 2867c478bd9Sstevel@tonic-gate * adt_cpy_tid() -- if lib is 64 bit, just copy it (dev_t and port are 2877c478bd9Sstevel@tonic-gate * both 64 bits). If lib is 32 bits, squeeze the two-int port into 2887c478bd9Sstevel@tonic-gate * a 32 bit dev_t. A port fits in the "minor" part of au_port_t, 2897c478bd9Sstevel@tonic-gate * so it isn't broken up into pieces. (When it goes to the kernel 2907c478bd9Sstevel@tonic-gate * and back, however, it will have been split into major/minor 2917c478bd9Sstevel@tonic-gate * pieces.) 2927c478bd9Sstevel@tonic-gate */ 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate static void 2957c478bd9Sstevel@tonic-gate adt_cpy_tid(au_tid_addr_t *dest, const au_tid64_addr_t *src) 2967c478bd9Sstevel@tonic-gate { 2977c478bd9Sstevel@tonic-gate #ifdef _LP64 2987c478bd9Sstevel@tonic-gate (void) memcpy(dest, src, sizeof (au_tid_addr_t)); 2997c478bd9Sstevel@tonic-gate #else 3007c478bd9Sstevel@tonic-gate dest->at_type = src->at_type; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate dest->at_port = src->at_port.at_minor & MAXMIN32; 3037c478bd9Sstevel@tonic-gate dest->at_port |= (src->at_port.at_major & MAXMAJ32) << 3047c478bd9Sstevel@tonic-gate NBITSMINOR32; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate (void) memcpy(dest->at_addr, src->at_addr, 4 * sizeof (uint32_t)); 3077c478bd9Sstevel@tonic-gate #endif 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate /* 3117c478bd9Sstevel@tonic-gate * adt_start_session -- create interface handle, create context 3127c478bd9Sstevel@tonic-gate * 3137c478bd9Sstevel@tonic-gate * The imported_state input is normally NULL, if not, it represents 3147c478bd9Sstevel@tonic-gate * a continued session; its values obviate the need for a subsequent 3157c478bd9Sstevel@tonic-gate * call to adt_set_user(). 3167c478bd9Sstevel@tonic-gate * 31726fba2a6Sgww * The flag is used to decide how to set the initial state of the session. 31826fba2a6Sgww * If 0, the session is "no audit" until a call to adt_set_user; if 31926fba2a6Sgww * ADT_USE_PROC_DATA, the session is built from the process audit 3207c478bd9Sstevel@tonic-gate * characteristics obtained from the kernel. If imported_state is 3217c478bd9Sstevel@tonic-gate * not NULL, the resulting audit mask is an OR of the current process 3227c478bd9Sstevel@tonic-gate * audit mask and that passed in. 3237c478bd9Sstevel@tonic-gate * 3247c478bd9Sstevel@tonic-gate * The basic model is that the caller can use the pointer returned 3257c478bd9Sstevel@tonic-gate * by adt_start_session whether or not auditing is enabled or an 3267c478bd9Sstevel@tonic-gate * error was returned. The functions that take the session handle 3277c478bd9Sstevel@tonic-gate * as input generally return without doing anything if auditing is 3287c478bd9Sstevel@tonic-gate * disabled. 3297c478bd9Sstevel@tonic-gate */ 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate int 3327c478bd9Sstevel@tonic-gate adt_start_session(adt_session_data_t **new_session, 33326fba2a6Sgww const adt_export_data_t *imported_state, adt_session_flags_t flags) 3347c478bd9Sstevel@tonic-gate { 3357c478bd9Sstevel@tonic-gate adt_internal_state_t *state; 3367c478bd9Sstevel@tonic-gate adt_session_flags_t flgmask = ADT_FLAGS_ALL; 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate *new_session = NULL; /* assume failure */ 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate /* ensure that auditstate is set */ 3417c478bd9Sstevel@tonic-gate (void) adt_audit_enabled(); 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate if ((flags & ~flgmask) != 0) { 3447c478bd9Sstevel@tonic-gate errno = EINVAL; 3457c478bd9Sstevel@tonic-gate goto return_err; 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate state = calloc(1, sizeof (adt_internal_state_t)); 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate if (state == NULL) 3507c478bd9Sstevel@tonic-gate goto return_err; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate if (adt_init(state, flags & ADT_USE_PROC_DATA) != 0) 3537c478bd9Sstevel@tonic-gate goto return_err_free; /* errno from adt_init() */ 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate /* 3567c478bd9Sstevel@tonic-gate * The imported state overwrites the initial state if the 3577c478bd9Sstevel@tonic-gate * imported state represents a valid audit trail 3587c478bd9Sstevel@tonic-gate */ 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate if (imported_state != NULL) { 361c529a23fSgww if (adt_import(state, imported_state) != 0) { 3627c478bd9Sstevel@tonic-gate goto return_err_free; 363c529a23fSgww } 36426fba2a6Sgww } else if (flags & ADT_USE_PROC_DATA) { 36526fba2a6Sgww state->as_session_model = ADT_PROCESS_MODEL; 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate state->as_flags = flags; 3687c478bd9Sstevel@tonic-gate DPRINTF(("(%d) Starting session id = %08X\n", 3697c478bd9Sstevel@tonic-gate getpid(), state->as_info.ai_asid)); 3707c478bd9Sstevel@tonic-gate 371c529a23fSgww if (state->as_audit_enabled) { 3727c478bd9Sstevel@tonic-gate *new_session = (adt_session_data_t *)state; 373c529a23fSgww } else { 3747c478bd9Sstevel@tonic-gate free(state); 375c529a23fSgww } 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate return (0); 3787c478bd9Sstevel@tonic-gate return_err_free: 3797c478bd9Sstevel@tonic-gate free(state); 3807c478bd9Sstevel@tonic-gate return_err: 3817c478bd9Sstevel@tonic-gate adt_write_syslog("audit session create failed", errno); 3827c478bd9Sstevel@tonic-gate return (-1); 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * adt_get_asid() and adt_set_asid() 3877c478bd9Sstevel@tonic-gate * 3887c478bd9Sstevel@tonic-gate * if you use this interface, you are responsible to insure that the 3897c478bd9Sstevel@tonic-gate * rest of the session data is populated correctly before calling 3907c478bd9Sstevel@tonic-gate * adt_proccess_attr() 3917c478bd9Sstevel@tonic-gate * 3927c478bd9Sstevel@tonic-gate * neither of these are intended for general use and will likely 3937c478bd9Sstevel@tonic-gate * remain private interfaces for a long time. Forever is a long 3947c478bd9Sstevel@tonic-gate * time. In the case of adt_set_asid(), you should have a very, 3957c478bd9Sstevel@tonic-gate * very good reason for setting your own session id. The process 3967c478bd9Sstevel@tonic-gate * audit characteristics are not changed by put, use adt_set_proc(). 3977c478bd9Sstevel@tonic-gate * 3987c478bd9Sstevel@tonic-gate * These are "volatile" (more changable than "evolving") and will 3997c478bd9Sstevel@tonic-gate * probably change in the S10 period. 4007c478bd9Sstevel@tonic-gate */ 40126fba2a6Sgww 4027c478bd9Sstevel@tonic-gate void 4037c478bd9Sstevel@tonic-gate adt_get_asid(const adt_session_data_t *session_data, au_asid_t *asid) 4047c478bd9Sstevel@tonic-gate { 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate if (session_data == NULL) { 4077c478bd9Sstevel@tonic-gate *asid = 0; 4087c478bd9Sstevel@tonic-gate } else { 4097c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check == 4107c478bd9Sstevel@tonic-gate ADT_VALID); 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate *asid = ((adt_internal_state_t *)session_data)->as_info.ai_asid; 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate void 41726fba2a6Sgww adt_set_asid(const adt_session_data_t *session_data, const au_asid_t session_id) 4187c478bd9Sstevel@tonic-gate { 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate if (session_data != NULL) { 4217c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check == 4227c478bd9Sstevel@tonic-gate ADT_VALID); 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_have_user_data |= 4257c478bd9Sstevel@tonic-gate ADT_HAVE_ASID; 4267c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_info.ai_asid = 4277c478bd9Sstevel@tonic-gate session_id; 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate /* 4327c478bd9Sstevel@tonic-gate * adt_get_auid() and adt_set_auid() 4337c478bd9Sstevel@tonic-gate * 4347c478bd9Sstevel@tonic-gate * neither of these are intended for general use and will likely 4357c478bd9Sstevel@tonic-gate * remain private interfaces for a long time. Forever is a long 4367c478bd9Sstevel@tonic-gate * time. In the case of adt_set_auid(), you should have a very, 4377c478bd9Sstevel@tonic-gate * very good reason for setting your own audit id. The process 4387c478bd9Sstevel@tonic-gate * audit characteristics are not changed by put, use adt_set_proc(). 4397c478bd9Sstevel@tonic-gate */ 44026fba2a6Sgww 4417c478bd9Sstevel@tonic-gate void 4427c478bd9Sstevel@tonic-gate adt_get_auid(const adt_session_data_t *session_data, au_id_t *auid) 4437c478bd9Sstevel@tonic-gate { 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate if (session_data == NULL) { 4467c478bd9Sstevel@tonic-gate *auid = AU_NOAUDITID; 4477c478bd9Sstevel@tonic-gate } else { 4487c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check == 4497c478bd9Sstevel@tonic-gate ADT_VALID); 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate *auid = ((adt_internal_state_t *)session_data)->as_info.ai_auid; 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate void 4567c478bd9Sstevel@tonic-gate adt_set_auid(const adt_session_data_t *session_data, const au_id_t audit_id) 4577c478bd9Sstevel@tonic-gate { 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (session_data != NULL) { 4607c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check == 4617c478bd9Sstevel@tonic-gate ADT_VALID); 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_have_user_data |= 4647c478bd9Sstevel@tonic-gate ADT_HAVE_AUID; 4657c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_info.ai_auid = 4667c478bd9Sstevel@tonic-gate audit_id; 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate /* 4717c478bd9Sstevel@tonic-gate * adt_get_termid(), adt_set_termid() 4727c478bd9Sstevel@tonic-gate * 4737c478bd9Sstevel@tonic-gate * if you use this interface, you are responsible to insure that the 4747c478bd9Sstevel@tonic-gate * rest of the session data is populated correctly before calling 4757c478bd9Sstevel@tonic-gate * adt_proccess_attr() 4767c478bd9Sstevel@tonic-gate * 4777c478bd9Sstevel@tonic-gate * The process audit characteristics are not changed by put, use 4787c478bd9Sstevel@tonic-gate * adt_set_proc(). 4797c478bd9Sstevel@tonic-gate */ 48026fba2a6Sgww 4817c478bd9Sstevel@tonic-gate void 4827c478bd9Sstevel@tonic-gate adt_get_termid(const adt_session_data_t *session_data, au_tid_addr_t *termid) 4837c478bd9Sstevel@tonic-gate { 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate if (session_data == NULL) { 4867c478bd9Sstevel@tonic-gate (void) memset(termid, 0, sizeof (au_tid_addr_t)); 4877c478bd9Sstevel@tonic-gate termid->at_type = AU_IPv4; 4887c478bd9Sstevel@tonic-gate } else { 4897c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check == 4907c478bd9Sstevel@tonic-gate ADT_VALID); 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate *termid = 4937c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_info.ai_termid; 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate void 4987c478bd9Sstevel@tonic-gate adt_set_termid(const adt_session_data_t *session_data, 4997c478bd9Sstevel@tonic-gate const au_tid_addr_t *termid) 5007c478bd9Sstevel@tonic-gate { 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate if (session_data != NULL) { 5037c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check == 5047c478bd9Sstevel@tonic-gate ADT_VALID); 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_info.ai_termid = 5077c478bd9Sstevel@tonic-gate *termid; 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_have_user_data |= 5107c478bd9Sstevel@tonic-gate ADT_HAVE_TID; 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate /* 5157c478bd9Sstevel@tonic-gate * adt_get_mask(), adt_set_mask() 5167c478bd9Sstevel@tonic-gate * 5177c478bd9Sstevel@tonic-gate * if you use this interface, you are responsible to insure that the 5187c478bd9Sstevel@tonic-gate * rest of the session data is populated correctly before calling 5197c478bd9Sstevel@tonic-gate * adt_proccess_attr() 5207c478bd9Sstevel@tonic-gate * 5217c478bd9Sstevel@tonic-gate * The process audit characteristics are not changed by put, use 5227c478bd9Sstevel@tonic-gate * adt_set_proc(). 5237c478bd9Sstevel@tonic-gate */ 52426fba2a6Sgww 5257c478bd9Sstevel@tonic-gate void 5267c478bd9Sstevel@tonic-gate adt_get_mask(const adt_session_data_t *session_data, au_mask_t *mask) 5277c478bd9Sstevel@tonic-gate { 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate if (session_data == NULL) { 5307c478bd9Sstevel@tonic-gate mask->am_success = 0; 5317c478bd9Sstevel@tonic-gate mask->am_failure = 0; 5327c478bd9Sstevel@tonic-gate } else { 5337c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check == 5347c478bd9Sstevel@tonic-gate ADT_VALID); 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate *mask = ((adt_internal_state_t *)session_data)->as_info.ai_mask; 5377c478bd9Sstevel@tonic-gate } 5387c478bd9Sstevel@tonic-gate } 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate void 5417c478bd9Sstevel@tonic-gate adt_set_mask(const adt_session_data_t *session_data, const au_mask_t *mask) 5427c478bd9Sstevel@tonic-gate { 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate if (session_data != NULL) { 5457c478bd9Sstevel@tonic-gate assert(((adt_internal_state_t *)session_data)->as_check == 5467c478bd9Sstevel@tonic-gate ADT_VALID); 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_info.ai_mask = *mask; 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate ((adt_internal_state_t *)session_data)->as_have_user_data |= 5517c478bd9Sstevel@tonic-gate ADT_HAVE_MASK; 5527c478bd9Sstevel@tonic-gate } 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate /* 5567c478bd9Sstevel@tonic-gate * helpers for adt_load_termid 5577c478bd9Sstevel@tonic-gate */ 55826fba2a6Sgww 5597c478bd9Sstevel@tonic-gate static void 5607c478bd9Sstevel@tonic-gate adt_do_ipv6_address(struct sockaddr_in6 *peer, struct sockaddr_in6 *sock, 5617c478bd9Sstevel@tonic-gate au_tid_addr_t *termid) 5627c478bd9Sstevel@tonic-gate { 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate termid->at_port = ((peer->sin6_port<<16) | (sock->sin6_port)); 5657c478bd9Sstevel@tonic-gate termid->at_type = AU_IPv6; 5667c478bd9Sstevel@tonic-gate (void) memcpy(termid->at_addr, &peer->sin6_addr, 4 * sizeof (uint_t)); 5677c478bd9Sstevel@tonic-gate } 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate static void 5707c478bd9Sstevel@tonic-gate adt_do_ipv4_address(struct sockaddr_in *peer, struct sockaddr_in *sock, 5717c478bd9Sstevel@tonic-gate au_tid_addr_t *termid) 5727c478bd9Sstevel@tonic-gate { 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate termid->at_port = ((peer->sin_port<<16) | (sock->sin_port)); 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate termid->at_type = AU_IPv4; 5777c478bd9Sstevel@tonic-gate termid->at_addr[0] = (uint32_t)peer->sin_addr.s_addr; 5787c478bd9Sstevel@tonic-gate (void) memset(&(termid->at_addr[1]), 0, 3 * sizeof (uint_t)); 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate /* 5827c478bd9Sstevel@tonic-gate * adt_load_termid: convenience function; inputs file handle and 5837c478bd9Sstevel@tonic-gate * outputs an au_tid_addr struct. 5847c478bd9Sstevel@tonic-gate * 5857c478bd9Sstevel@tonic-gate * This code was stolen from audit_settid.c; it differs from audit_settid() 5867c478bd9Sstevel@tonic-gate * in that it does not write the terminal id to the process. 5877c478bd9Sstevel@tonic-gate */ 5887c478bd9Sstevel@tonic-gate 5897c478bd9Sstevel@tonic-gate int 5907c478bd9Sstevel@tonic-gate adt_load_termid(int fd, adt_termid_t **termid) 5917c478bd9Sstevel@tonic-gate { 5927c478bd9Sstevel@tonic-gate au_tid_addr_t *p_term; 5937c478bd9Sstevel@tonic-gate struct sockaddr_in6 peer; 5947c478bd9Sstevel@tonic-gate struct sockaddr_in6 sock; 5957c478bd9Sstevel@tonic-gate int peerlen = sizeof (peer); 5967c478bd9Sstevel@tonic-gate int socklen = sizeof (sock); 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate *termid = NULL; 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate /* get peer name if its a socket, else assume local terminal */ 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate if (getpeername(fd, (struct sockaddr *)&peer, (socklen_t *)&peerlen) 6037c478bd9Sstevel@tonic-gate < 0) { 6047c478bd9Sstevel@tonic-gate if (errno == ENOTSOCK) 6057c478bd9Sstevel@tonic-gate return (adt_load_hostname(NULL, termid)); 6067c478bd9Sstevel@tonic-gate goto return_err; 6077c478bd9Sstevel@tonic-gate } 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) 6107c478bd9Sstevel@tonic-gate goto return_err; 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate /* get sock name */ 6137c478bd9Sstevel@tonic-gate if (getsockname(fd, (struct sockaddr *)&sock, 6147c478bd9Sstevel@tonic-gate (socklen_t *)&socklen) < 0) 6157c478bd9Sstevel@tonic-gate goto return_err_free; 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate if (peer.sin6_family == AF_INET6) { 6187c478bd9Sstevel@tonic-gate adt_do_ipv6_address(&peer, &sock, p_term); 6197c478bd9Sstevel@tonic-gate } else { 6207c478bd9Sstevel@tonic-gate adt_do_ipv4_address((struct sockaddr_in *)&peer, 6217c478bd9Sstevel@tonic-gate (struct sockaddr_in *)&sock, p_term); 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate *termid = (adt_termid_t *)p_term; 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate return (0); 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate return_err_free: 6287c478bd9Sstevel@tonic-gate free(p_term); 6297c478bd9Sstevel@tonic-gate return_err: 6307c478bd9Sstevel@tonic-gate return (-1); 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate static boolean_t 6347c478bd9Sstevel@tonic-gate adt_have_termid(au_tid_addr_t *dest) 6357c478bd9Sstevel@tonic-gate { 6367c478bd9Sstevel@tonic-gate struct auditinfo_addr audit_data; 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate if (getaudit_addr(&audit_data, sizeof (audit_data)) < 0) { 6397c478bd9Sstevel@tonic-gate adt_write_syslog("getaudit failed", errno); 6407c478bd9Sstevel@tonic-gate return (B_FALSE); 6417c478bd9Sstevel@tonic-gate } 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate if ((audit_data.ai_termid.at_type == 0) || 6447c478bd9Sstevel@tonic-gate (audit_data.ai_termid.at_addr[0] | 6457c478bd9Sstevel@tonic-gate audit_data.ai_termid.at_addr[1] | 6467c478bd9Sstevel@tonic-gate audit_data.ai_termid.at_addr[2] | 6477c478bd9Sstevel@tonic-gate audit_data.ai_termid.at_addr[3]) == 0) 6487c478bd9Sstevel@tonic-gate return (B_FALSE); 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate (void) memcpy(dest, &(audit_data.ai_termid), 6517c478bd9Sstevel@tonic-gate sizeof (au_tid_addr_t)); 6527c478bd9Sstevel@tonic-gate 6537c478bd9Sstevel@tonic-gate return (B_TRUE); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate static int 6577c478bd9Sstevel@tonic-gate adt_get_hostIP(const char *hostname, au_tid_addr_t *p_term) 6587c478bd9Sstevel@tonic-gate { 6597c478bd9Sstevel@tonic-gate struct addrinfo *ai; 6607c478bd9Sstevel@tonic-gate void *p; 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate if (getaddrinfo(hostname, NULL, NULL, &ai) != 0) 6637c478bd9Sstevel@tonic-gate return (-1); 6647c478bd9Sstevel@tonic-gate 6657c478bd9Sstevel@tonic-gate switch (ai->ai_family) { 6667c478bd9Sstevel@tonic-gate case AF_INET: 6677c478bd9Sstevel@tonic-gate /* LINTED */ 6687c478bd9Sstevel@tonic-gate p = &((struct sockaddr_in *)ai->ai_addr)->sin_addr; 6697c478bd9Sstevel@tonic-gate (void) memcpy(p_term->at_addr, p, 6707c478bd9Sstevel@tonic-gate sizeof (((struct sockaddr_in *)NULL)->sin_addr)); 6717c478bd9Sstevel@tonic-gate p_term->at_type = AU_IPv4; 6727c478bd9Sstevel@tonic-gate break; 6737c478bd9Sstevel@tonic-gate case AF_INET6: 6747c478bd9Sstevel@tonic-gate /* LINTED */ 6757c478bd9Sstevel@tonic-gate p = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr, 6767c478bd9Sstevel@tonic-gate (void) memcpy(p_term->at_addr, p, 6777c478bd9Sstevel@tonic-gate sizeof (((struct sockaddr_in6 *)NULL)->sin6_addr)); 6787c478bd9Sstevel@tonic-gate p_term->at_type = AU_IPv6; 6797c478bd9Sstevel@tonic-gate break; 6807c478bd9Sstevel@tonic-gate default: 6817c478bd9Sstevel@tonic-gate return (-1); 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate freeaddrinfo(ai); 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate return (0); 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate /* 6907c478bd9Sstevel@tonic-gate * adt_load_hostname() is called when the caller does not have a file 6917c478bd9Sstevel@tonic-gate * handle that gives access to the socket info or any other way to 6927c478bd9Sstevel@tonic-gate * pass in both port and ip address. The hostname input is ignored if 6937c478bd9Sstevel@tonic-gate * the terminal id has already been set; instead it returns the 6947c478bd9Sstevel@tonic-gate * existing terminal id. 6957c478bd9Sstevel@tonic-gate * 6967c478bd9Sstevel@tonic-gate * If audit is off and the hostname lookup fails, no error is 6977c478bd9Sstevel@tonic-gate * returned, since an error may be interpreted by the caller 6987c478bd9Sstevel@tonic-gate * as grounds for denying a login. Otherwise the caller would 6997c478bd9Sstevel@tonic-gate * need to be aware of the audit state. 7007c478bd9Sstevel@tonic-gate */ 70126fba2a6Sgww 7027c478bd9Sstevel@tonic-gate int 7037c478bd9Sstevel@tonic-gate adt_load_hostname(const char *hostname, adt_termid_t **termid) 7047c478bd9Sstevel@tonic-gate { 7057c478bd9Sstevel@tonic-gate char localhost[ADT_STRING_MAX + 1]; 7067c478bd9Sstevel@tonic-gate au_tid_addr_t *p_term; 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate *termid = NULL; 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate if (!adt_audit_enabled()) 7117c478bd9Sstevel@tonic-gate return (0); 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) 7147c478bd9Sstevel@tonic-gate goto return_err; 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate if (adt_have_termid(p_term)) { 7177c478bd9Sstevel@tonic-gate *termid = (adt_termid_t *)p_term; 7187c478bd9Sstevel@tonic-gate return (0); 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate p_term->at_port = 0; 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate if (hostname == NULL || *hostname == '\0') { 7237c478bd9Sstevel@tonic-gate (void) sysinfo(SI_HOSTNAME, localhost, ADT_STRING_MAX); 7247c478bd9Sstevel@tonic-gate hostname = localhost; 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate if (adt_get_hostIP(hostname, p_term)) 7277c478bd9Sstevel@tonic-gate goto return_err_free; 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate *termid = (adt_termid_t *)p_term; 7307c478bd9Sstevel@tonic-gate return (0); 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate return_err_free: 7337c478bd9Sstevel@tonic-gate free(p_term); 7347c478bd9Sstevel@tonic-gate 7357c478bd9Sstevel@tonic-gate return_err: 7367c478bd9Sstevel@tonic-gate if ((auditstate == AUC_DISABLED) || 7377c478bd9Sstevel@tonic-gate (auditstate == AUC_NOAUDIT)) 7387c478bd9Sstevel@tonic-gate return (0); 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate return (-1); 7417c478bd9Sstevel@tonic-gate } 7427c478bd9Sstevel@tonic-gate 7437c478bd9Sstevel@tonic-gate /* 7447c478bd9Sstevel@tonic-gate * adt_load_ttyname() is called when the caller does not have a file 7457c478bd9Sstevel@tonic-gate * handle that gives access to the local terminal or any other way 7467c478bd9Sstevel@tonic-gate * of determining the device id. The ttyname input is ignored if 7477c478bd9Sstevel@tonic-gate * the terminal id has already been set; instead it returns the 7487c478bd9Sstevel@tonic-gate * existing terminal id. 7497c478bd9Sstevel@tonic-gate * 7507c478bd9Sstevel@tonic-gate * If audit is off and the ttyname lookup fails, no error is 7517c478bd9Sstevel@tonic-gate * returned, since an error may be interpreted by the caller 7527c478bd9Sstevel@tonic-gate * as grounds for denying a login. Otherwise the caller would 7537c478bd9Sstevel@tonic-gate * need to be aware of the audit state. 7547c478bd9Sstevel@tonic-gate */ 75526fba2a6Sgww 7567c478bd9Sstevel@tonic-gate int 7577c478bd9Sstevel@tonic-gate adt_load_ttyname(const char *ttyname, adt_termid_t **termid) 7587c478bd9Sstevel@tonic-gate { 7597c478bd9Sstevel@tonic-gate char localhost[ADT_STRING_MAX + 1]; 7607c478bd9Sstevel@tonic-gate au_tid_addr_t *p_term; 7617c478bd9Sstevel@tonic-gate struct stat stat_buf; 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate *termid = NULL; 7647c478bd9Sstevel@tonic-gate 7657c478bd9Sstevel@tonic-gate if (!adt_audit_enabled()) 7667c478bd9Sstevel@tonic-gate return (0); 7677c478bd9Sstevel@tonic-gate 7687c478bd9Sstevel@tonic-gate if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) 7697c478bd9Sstevel@tonic-gate goto return_err; 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate if (adt_have_termid(p_term)) { 7727c478bd9Sstevel@tonic-gate *termid = (adt_termid_t *)p_term; 7737c478bd9Sstevel@tonic-gate return (0); 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate 7767c478bd9Sstevel@tonic-gate p_term->at_port = 0; 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate if (sysinfo(SI_HOSTNAME, localhost, ADT_STRING_MAX) < 0) 7797c478bd9Sstevel@tonic-gate goto return_err_free; /* errno from sysinfo */ 7807c478bd9Sstevel@tonic-gate 7817c478bd9Sstevel@tonic-gate if (ttyname != NULL) { 7827c478bd9Sstevel@tonic-gate if (stat(ttyname, &stat_buf) < 0) 7837c478bd9Sstevel@tonic-gate goto return_err_free; 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate p_term->at_port = stat_buf.st_rdev; 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate if (adt_get_hostIP(localhost, p_term)) 7897c478bd9Sstevel@tonic-gate goto return_err_free; 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate *termid = (adt_termid_t *)p_term; 7927c478bd9Sstevel@tonic-gate return (0); 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate return_err_free: 7957c478bd9Sstevel@tonic-gate free(p_term); 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate return_err: 7987c478bd9Sstevel@tonic-gate if ((auditstate == AUC_DISABLED) || 7997c478bd9Sstevel@tonic-gate (auditstate == AUC_NOAUDIT)) 8007c478bd9Sstevel@tonic-gate return (0); 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate return (-1); 8037c478bd9Sstevel@tonic-gate } 8047c478bd9Sstevel@tonic-gate 8057c478bd9Sstevel@tonic-gate /* 8067c478bd9Sstevel@tonic-gate * adt_get_session_id returns a stringified representation of 8077c478bd9Sstevel@tonic-gate * the audit session id. See also adt_get_asid() for how to 8087c478bd9Sstevel@tonic-gate * get the unexpurgated version. No guarantees as to how long 8097c478bd9Sstevel@tonic-gate * the returned string will be or its general form; hex for now. 8107c478bd9Sstevel@tonic-gate * 8117c478bd9Sstevel@tonic-gate * An empty string is returned if auditing is off; length = 1 8127c478bd9Sstevel@tonic-gate * and the pointer is valid. 8137c478bd9Sstevel@tonic-gate * 8147c478bd9Sstevel@tonic-gate * returns strlen + 1 if buffer is valid; else 0 and errno. 8157c478bd9Sstevel@tonic-gate */ 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate size_t 8187c478bd9Sstevel@tonic-gate adt_get_session_id(const adt_session_data_t *session_data, char **buff) 8197c478bd9Sstevel@tonic-gate { 8207c478bd9Sstevel@tonic-gate au_asid_t session_id; 8217c478bd9Sstevel@tonic-gate size_t length; 8227c478bd9Sstevel@tonic-gate /* 8237c478bd9Sstevel@tonic-gate * output is 0x followed by 8247c478bd9Sstevel@tonic-gate * two characters per byte 8257c478bd9Sstevel@tonic-gate * plus terminator, 8267c478bd9Sstevel@tonic-gate * except leading 0's are suppressed, so a few bytes may 8277c478bd9Sstevel@tonic-gate * be unused. 8287c478bd9Sstevel@tonic-gate */ 8297c478bd9Sstevel@tonic-gate length = 2 + (2 * sizeof (session_id)) + 1; 8307c478bd9Sstevel@tonic-gate *buff = malloc(length); 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate if (*buff == NULL) { 833c529a23fSgww return (0); 8347c478bd9Sstevel@tonic-gate } 8357c478bd9Sstevel@tonic-gate if (session_data == NULL) { /* NULL is not an error */ 8367c478bd9Sstevel@tonic-gate **buff = '\0'; 837c529a23fSgww return (1); 8387c478bd9Sstevel@tonic-gate } 8397c478bd9Sstevel@tonic-gate adt_get_asid(session_data, &session_id); 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate length = snprintf(*buff, length, "0x%X", (int)session_id); 8427c478bd9Sstevel@tonic-gate 8437c478bd9Sstevel@tonic-gate /* length < 1 is a bug: the session data type may have changed */ 8447c478bd9Sstevel@tonic-gate assert(length > 0); 8457c478bd9Sstevel@tonic-gate 8467c478bd9Sstevel@tonic-gate return (length); 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate /* 8507c478bd9Sstevel@tonic-gate * adt_end_session -- close handle, clear context 8517c478bd9Sstevel@tonic-gate * 8527c478bd9Sstevel@tonic-gate * if as_check is invalid, no harm, no foul, EXCEPT that this could 8537c478bd9Sstevel@tonic-gate * be an attempt to free data already free'd, so output to syslog 8547c478bd9Sstevel@tonic-gate * to help explain why the process cored dumped. 8557c478bd9Sstevel@tonic-gate */ 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate int 8587c478bd9Sstevel@tonic-gate adt_end_session(adt_session_data_t *session_data) 8597c478bd9Sstevel@tonic-gate { 8607c478bd9Sstevel@tonic-gate adt_internal_state_t *state; 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate if (session_data != NULL) { 8637c478bd9Sstevel@tonic-gate state = (adt_internal_state_t *)session_data; 864c529a23fSgww if (state->as_check != ADT_VALID) { 8657c478bd9Sstevel@tonic-gate adt_write_syslog("freeing invalid data", EINVAL); 866c529a23fSgww } else { 8677c478bd9Sstevel@tonic-gate state->as_check = 0; 868c529a23fSgww m_label_free(state->as_label); 8697c478bd9Sstevel@tonic-gate free(session_data); 8707c478bd9Sstevel@tonic-gate } 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate /* no errors yet defined */ 8737c478bd9Sstevel@tonic-gate return (0); 8747c478bd9Sstevel@tonic-gate } 8757c478bd9Sstevel@tonic-gate 8767c478bd9Sstevel@tonic-gate /* 8777c478bd9Sstevel@tonic-gate * adt_dup_session -- copy the session data 8787c478bd9Sstevel@tonic-gate */ 8797c478bd9Sstevel@tonic-gate 8807c478bd9Sstevel@tonic-gate int 8817c478bd9Sstevel@tonic-gate adt_dup_session(const adt_session_data_t *source, adt_session_data_t **dest) 8827c478bd9Sstevel@tonic-gate { 8837c478bd9Sstevel@tonic-gate adt_internal_state_t *source_state; 884c529a23fSgww adt_internal_state_t *dest_state = NULL; 8857c478bd9Sstevel@tonic-gate int rc = 0; 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate if (source != NULL) { 8887c478bd9Sstevel@tonic-gate source_state = (adt_internal_state_t *)source; 8897c478bd9Sstevel@tonic-gate assert(source_state->as_check == ADT_VALID); 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate dest_state = malloc(sizeof (adt_internal_state_t)); 8927c478bd9Sstevel@tonic-gate if (dest_state == NULL) { 8937c478bd9Sstevel@tonic-gate rc = -1; 8947c478bd9Sstevel@tonic-gate goto return_rc; 8957c478bd9Sstevel@tonic-gate } 8967c478bd9Sstevel@tonic-gate (void) memcpy(dest_state, source, 8977c478bd9Sstevel@tonic-gate sizeof (struct adt_internal_state)); 898c529a23fSgww 899c529a23fSgww if (source_state->as_label != NULL) { 900c529a23fSgww dest_state->as_label = NULL; 901c529a23fSgww if ((rc = m_label_dup(&dest_state->as_label, 902c529a23fSgww source_state->as_label)) != 0) { 903c529a23fSgww free(dest_state); 904c529a23fSgww dest_state = NULL; 905c529a23fSgww } 906c529a23fSgww } 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate return_rc: 909c529a23fSgww *dest = (adt_session_data_t *)dest_state; 9107c478bd9Sstevel@tonic-gate return (rc); 9117c478bd9Sstevel@tonic-gate } 9127c478bd9Sstevel@tonic-gate 9137c478bd9Sstevel@tonic-gate /* 9147c478bd9Sstevel@tonic-gate * from_export_format() 9157c478bd9Sstevel@tonic-gate * read from a network order buffer into struct adt_session_data 9167c478bd9Sstevel@tonic-gate */ 91726fba2a6Sgww 9187c478bd9Sstevel@tonic-gate static size_t 9197c478bd9Sstevel@tonic-gate adt_from_export_format(adt_internal_state_t *internal, 9207c478bd9Sstevel@tonic-gate const adt_export_data_t *external) 9217c478bd9Sstevel@tonic-gate { 9227c478bd9Sstevel@tonic-gate struct export_header head; 9237c478bd9Sstevel@tonic-gate struct export_link link; 9247c478bd9Sstevel@tonic-gate adr_t context; 9257c478bd9Sstevel@tonic-gate int32_t offset; 9267c478bd9Sstevel@tonic-gate int32_t length; 9277c478bd9Sstevel@tonic-gate int32_t version; 928c529a23fSgww size_t label_len; 9297c478bd9Sstevel@tonic-gate char *p = (char *)external; 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate adrm_start(&context, (char *)external); 9327c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&head, 4); 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate if ((internal->as_check = head.ax_check) != ADT_VALID) { 9357c478bd9Sstevel@tonic-gate errno = EINVAL; 9367c478bd9Sstevel@tonic-gate return (0); 9377c478bd9Sstevel@tonic-gate } 9387c478bd9Sstevel@tonic-gate offset = head.ax_link.ax_offset; 9397c478bd9Sstevel@tonic-gate version = head.ax_link.ax_version; 9407c478bd9Sstevel@tonic-gate length = head.ax_buffer_length; 9417c478bd9Sstevel@tonic-gate 9427c478bd9Sstevel@tonic-gate /* 943c529a23fSgww * Skip newer versions. 9447c478bd9Sstevel@tonic-gate */ 945c529a23fSgww while (version > PROTOCOL_VERSION_2) { 946c529a23fSgww if (offset < 1) { 9477c478bd9Sstevel@tonic-gate return (0); /* failed to match version */ 948c529a23fSgww } 9497c478bd9Sstevel@tonic-gate p += offset; /* point to next version # */ 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate if (p > (char *)external + length) { 9527c478bd9Sstevel@tonic-gate return (0); 9537c478bd9Sstevel@tonic-gate } 9547c478bd9Sstevel@tonic-gate adrm_start(&context, p); 9557c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&link, 2); 9567c478bd9Sstevel@tonic-gate offset = link.ax_offset; 9577c478bd9Sstevel@tonic-gate version = link.ax_version; 9587c478bd9Sstevel@tonic-gate assert(version != 0); 9597c478bd9Sstevel@tonic-gate } 960c529a23fSgww /* 961c529a23fSgww * Adjust buffer pointer to the first data item (euid). 962c529a23fSgww */ 963c529a23fSgww if (p == (char *)external) { 9647c478bd9Sstevel@tonic-gate adrm_start(&context, (char *)(p + sizeof (head))); 965c529a23fSgww } else { 9667c478bd9Sstevel@tonic-gate adrm_start(&context, (char *)(p + sizeof (link))); 967c529a23fSgww } 968c529a23fSgww /* 969c529a23fSgww * if down rev version, neither pid nor label are included 970c529a23fSgww * in v1 ax_size_of_tsol_data intentionally ignored 971c529a23fSgww */ 972c529a23fSgww if (version == PROTOCOL_VERSION_1) { 9737c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_euid), 1); 9747c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_ruid), 1); 9757c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_egid), 1); 9767c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_rgid), 1); 9777c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1); 978c529a23fSgww adrm_int32(&context, 979c529a23fSgww (int *)&(internal->as_info.ai_mask.am_success), 2); 980c529a23fSgww adrm_int32(&context, 981c529a23fSgww (int *)&(internal->as_info.ai_termid.at_port), 1); 982c529a23fSgww adrm_int32(&context, 983c529a23fSgww (int *)&(internal->as_info.ai_termid.at_type), 1); 984c529a23fSgww adrm_int32(&context, 985c529a23fSgww (int *)&(internal->as_info.ai_termid.at_addr[0]), 4); 9867c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1); 9877c478bd9Sstevel@tonic-gate adrm_int32(&context, (int *)&(internal->as_audit_enabled), 1); 988c529a23fSgww internal->as_pid = (pid_t)-1; 989c529a23fSgww internal->as_label = NULL; 990c529a23fSgww } else if (version == PROTOCOL_VERSION_2) { 991c529a23fSgww adrm_int32(&context, (int *)&(internal->as_euid), 1); 992c529a23fSgww adrm_int32(&context, (int *)&(internal->as_ruid), 1); 993c529a23fSgww adrm_int32(&context, (int *)&(internal->as_egid), 1); 994c529a23fSgww adrm_int32(&context, (int *)&(internal->as_rgid), 1); 995c529a23fSgww adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1); 996c529a23fSgww adrm_int32(&context, 997c529a23fSgww (int *)&(internal->as_info.ai_mask.am_success), 2); 998c529a23fSgww adrm_int32(&context, 999c529a23fSgww (int *)&(internal->as_info.ai_termid.at_port), 1); 1000c529a23fSgww adrm_int32(&context, 1001c529a23fSgww (int *)&(internal->as_info.ai_termid.at_type), 1); 1002c529a23fSgww adrm_int32(&context, 1003c529a23fSgww (int *)&(internal->as_info.ai_termid.at_addr[0]), 4); 1004c529a23fSgww adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1); 1005c529a23fSgww adrm_int32(&context, (int *)&(internal->as_audit_enabled), 1); 1006c529a23fSgww adrm_int32(&context, (int *)&(internal->as_pid), 1); 1007c529a23fSgww adrm_int32(&context, (int *)&label_len, 1); 1008c529a23fSgww if (label_len > 0) { 1009c529a23fSgww /* read in and deal with different sized labels. */ 1010c529a23fSgww size_t my_label_len = blabel_size(); 10117c478bd9Sstevel@tonic-gate 1012c529a23fSgww if ((internal->as_label = 1013c529a23fSgww m_label_alloc(MAC_LABEL)) == NULL) { 1014c529a23fSgww return (0); 1015c529a23fSgww } 1016c529a23fSgww if (label_len > my_label_len) { 1017c529a23fSgww errno = EINVAL; 1018c529a23fSgww m_label_free(internal->as_label); 1019c529a23fSgww return (0); 1020c529a23fSgww } 1021c529a23fSgww (void) memset(internal->as_label, 0, my_label_len); 1022c529a23fSgww adrm_int32(&context, (int *)(internal->as_label), 1023c529a23fSgww label_len / sizeof (int32_t)); 1024c529a23fSgww } else { 1025c529a23fSgww internal->as_label = NULL; 1026c529a23fSgww } 1027c529a23fSgww } 10287c478bd9Sstevel@tonic-gate 1029c529a23fSgww return (length); 10307c478bd9Sstevel@tonic-gate } 10317c478bd9Sstevel@tonic-gate 10327c478bd9Sstevel@tonic-gate /* 1033c529a23fSgww * adt_to_export_format 10347c478bd9Sstevel@tonic-gate * read from struct adt_session_data into a network order buffer. 10357c478bd9Sstevel@tonic-gate * 10367c478bd9Sstevel@tonic-gate * (network order 'cause this data may be shared with a remote host.) 10377c478bd9Sstevel@tonic-gate */ 10387c478bd9Sstevel@tonic-gate 10397c478bd9Sstevel@tonic-gate static size_t 10407c478bd9Sstevel@tonic-gate adt_to_export_format(adt_export_data_t *external, 10417c478bd9Sstevel@tonic-gate adt_internal_state_t *internal) 10427c478bd9Sstevel@tonic-gate { 10437c478bd9Sstevel@tonic-gate struct export_header head; 10447c478bd9Sstevel@tonic-gate struct export_link tail; 10457c478bd9Sstevel@tonic-gate adr_t context; 1046c529a23fSgww size_t label_len = 0; 10477c478bd9Sstevel@tonic-gate 10487c478bd9Sstevel@tonic-gate adrm_start(&context, (char *)external); 10497c478bd9Sstevel@tonic-gate 1050c529a23fSgww if (internal->as_label != NULL) { 1051c529a23fSgww label_len = blabel_size(); 1052c529a23fSgww } 1053c529a23fSgww 10547c478bd9Sstevel@tonic-gate head.ax_check = ADT_VALID; 1055c529a23fSgww head.ax_buffer_length = sizeof (struct adt_export_data) + label_len; 1056c529a23fSgww 1057c529a23fSgww /* version 2 first */ 1058c529a23fSgww 1059c529a23fSgww head.ax_link.ax_version = PROTOCOL_VERSION_2; 1060c529a23fSgww head.ax_link.ax_offset = sizeof (struct export_header) + 1061c529a23fSgww sizeof (struct adt_export_v2) + label_len; 1062c529a23fSgww 10637c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&head, 4); 10647c478bd9Sstevel@tonic-gate 10657c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_euid), 1); 10667c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_ruid), 1); 10677c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_egid), 1); 10687c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_rgid), 1); 10697c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1); 10707c478bd9Sstevel@tonic-gate adrm_putint32(&context, 10717c478bd9Sstevel@tonic-gate (int *)&(internal->as_info.ai_mask.am_success), 2); 10727c478bd9Sstevel@tonic-gate adrm_putint32(&context, 10737c478bd9Sstevel@tonic-gate (int *)&(internal->as_info.ai_termid.at_port), 1); 10747c478bd9Sstevel@tonic-gate adrm_putint32(&context, 10757c478bd9Sstevel@tonic-gate (int *)&(internal->as_info.ai_termid.at_type), 1); 10767c478bd9Sstevel@tonic-gate adrm_putint32(&context, 10777c478bd9Sstevel@tonic-gate (int *)&(internal->as_info.ai_termid.at_addr[0]), 4); 10787c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1); 10797c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&(internal->as_audit_enabled), 1); 1080c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_pid), 1); 1081c529a23fSgww adrm_putint32(&context, (int *)&label_len, 1); 1082c529a23fSgww if (internal->as_label != NULL) { 1083c529a23fSgww /* serialize the label */ 1084c529a23fSgww adrm_putint32(&context, (int *)(internal->as_label), 1085c529a23fSgww (label_len / sizeof (int32_t))); 1086c529a23fSgww } 10877c478bd9Sstevel@tonic-gate 1088c529a23fSgww /* now version 1 */ 1089c529a23fSgww 1090c529a23fSgww tail.ax_version = PROTOCOL_VERSION_1; 1091c529a23fSgww tail.ax_offset = 0; 1092c529a23fSgww 1093c529a23fSgww adrm_putint32(&context, (int *)&tail, 2); 1094c529a23fSgww 1095c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_euid), 1); 1096c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_ruid), 1); 1097c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_egid), 1); 1098c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_rgid), 1); 1099c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1); 1100c529a23fSgww adrm_putint32(&context, 1101c529a23fSgww (int *)&(internal->as_info.ai_mask.am_success), 2); 1102c529a23fSgww adrm_putint32(&context, 1103c529a23fSgww (int *)&(internal->as_info.ai_termid.at_port), 1); 1104c529a23fSgww adrm_putint32(&context, 1105c529a23fSgww (int *)&(internal->as_info.ai_termid.at_type), 1); 1106c529a23fSgww adrm_putint32(&context, 1107c529a23fSgww (int *)&(internal->as_info.ai_termid.at_addr[0]), 4); 1108c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1); 1109c529a23fSgww adrm_putint32(&context, (int *)&(internal->as_audit_enabled), 1); 1110c529a23fSgww /* ignored in v1 */ 1111c529a23fSgww adrm_putint32(&context, (int *)&label_len, 1); 1112c529a23fSgww 1113c529a23fSgww /* finally terminator */ 1114c529a23fSgww 11157c478bd9Sstevel@tonic-gate tail.ax_version = 0; /* invalid version number */ 11167c478bd9Sstevel@tonic-gate tail.ax_offset = 0; 11177c478bd9Sstevel@tonic-gate 11187c478bd9Sstevel@tonic-gate adrm_putint32(&context, (int *)&tail, 2); 11197c478bd9Sstevel@tonic-gate 1120c529a23fSgww return (head.ax_buffer_length); 11217c478bd9Sstevel@tonic-gate } 11227c478bd9Sstevel@tonic-gate 1123c529a23fSgww 11247c478bd9Sstevel@tonic-gate /* 11257c478bd9Sstevel@tonic-gate * adt_import_proc() is used by a server acting on behalf 11267c478bd9Sstevel@tonic-gate * of a client which has connected via an ipc mechanism such as 11277c478bd9Sstevel@tonic-gate * a door. 11287c478bd9Sstevel@tonic-gate * 11297c478bd9Sstevel@tonic-gate * Since the interface is via ucred, the info.ap_termid.port 11307c478bd9Sstevel@tonic-gate * value is always the 64 bit version. What is stored depends 11317c478bd9Sstevel@tonic-gate * on how libbsm is compiled. 11327c478bd9Sstevel@tonic-gate */ 113326fba2a6Sgww 11347c478bd9Sstevel@tonic-gate size_t 113526fba2a6Sgww adt_import_proc(pid_t pid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid, 113626fba2a6Sgww adt_export_data_t **external) 11377c478bd9Sstevel@tonic-gate { 11387c478bd9Sstevel@tonic-gate size_t length = 0; 11397c478bd9Sstevel@tonic-gate adt_internal_state_t *state; 11407c478bd9Sstevel@tonic-gate ucred_t *ucred; 11417c478bd9Sstevel@tonic-gate const au_tid64_addr_t *tid; 11427c478bd9Sstevel@tonic-gate 11437c478bd9Sstevel@tonic-gate state = calloc(1, sizeof (adt_internal_state_t)); 11447c478bd9Sstevel@tonic-gate 11457c478bd9Sstevel@tonic-gate if (state == NULL) 1146c529a23fSgww return (0); 11477c478bd9Sstevel@tonic-gate 11487c478bd9Sstevel@tonic-gate if (adt_init(state, 0) != 0) 11497c478bd9Sstevel@tonic-gate goto return_length_free; /* errno from adt_init() */ 11507c478bd9Sstevel@tonic-gate 11517c478bd9Sstevel@tonic-gate /* 11527c478bd9Sstevel@tonic-gate * ucred_getauid() returns AU_NOAUDITID if audit is off, which 11537c478bd9Sstevel@tonic-gate * is the right answer for adt_import_proc(). 11547c478bd9Sstevel@tonic-gate * 11557c478bd9Sstevel@tonic-gate * Create a local context as near as possible. 11567c478bd9Sstevel@tonic-gate */ 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate ucred = ucred_get(pid); 11597c478bd9Sstevel@tonic-gate 11607c478bd9Sstevel@tonic-gate if (ucred == NULL) 11617c478bd9Sstevel@tonic-gate goto return_length_free; 11627c478bd9Sstevel@tonic-gate 11637c478bd9Sstevel@tonic-gate state->as_ruid = ruid != ADT_NO_CHANGE ? ruid : ucred_getruid(ucred); 11647c478bd9Sstevel@tonic-gate state->as_euid = euid != ADT_NO_CHANGE ? euid : ucred_geteuid(ucred); 11657c478bd9Sstevel@tonic-gate state->as_rgid = rgid != ADT_NO_CHANGE ? rgid : ucred_getrgid(ucred); 11667c478bd9Sstevel@tonic-gate state->as_egid = egid != ADT_NO_CHANGE ? egid : ucred_getegid(ucred); 11677c478bd9Sstevel@tonic-gate 11687c478bd9Sstevel@tonic-gate state->as_info.ai_auid = ucred_getauid(ucred); 11697c478bd9Sstevel@tonic-gate 11707c478bd9Sstevel@tonic-gate if (state->as_info.ai_auid == AU_NOAUDITID) { 11717c478bd9Sstevel@tonic-gate state->as_info.ai_asid = adt_get_unique_id(ruid); 11727c478bd9Sstevel@tonic-gate 11737c478bd9Sstevel@tonic-gate if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask))) 11747c478bd9Sstevel@tonic-gate goto return_all_free; 11757c478bd9Sstevel@tonic-gate } else { 11767c478bd9Sstevel@tonic-gate const au_mask_t *mask = ucred_getamask(ucred); 11777c478bd9Sstevel@tonic-gate 11787c478bd9Sstevel@tonic-gate if (mask != NULL) 11797c478bd9Sstevel@tonic-gate state->as_info.ai_mask = *mask; 11807c478bd9Sstevel@tonic-gate else 11817c478bd9Sstevel@tonic-gate goto return_all_free; 11827c478bd9Sstevel@tonic-gate 11837c478bd9Sstevel@tonic-gate state->as_info.ai_asid = ucred_getasid(ucred); 11847c478bd9Sstevel@tonic-gate } 11857c478bd9Sstevel@tonic-gate 11867c478bd9Sstevel@tonic-gate tid = ucred_getatid(ucred); 11877c478bd9Sstevel@tonic-gate 11887c478bd9Sstevel@tonic-gate if (tid != NULL) { 11897c478bd9Sstevel@tonic-gate adt_cpy_tid(&(state->as_info.ai_termid), tid); 11907c478bd9Sstevel@tonic-gate } else { 11917c478bd9Sstevel@tonic-gate (void) memset((void *)&(state->as_info.ai_termid), 0, 11927c478bd9Sstevel@tonic-gate sizeof (au_tid_addr_t)); 11937c478bd9Sstevel@tonic-gate state->as_info.ai_termid.at_type = AU_IPv4; 11947c478bd9Sstevel@tonic-gate } 11957c478bd9Sstevel@tonic-gate 11967c478bd9Sstevel@tonic-gate DPRINTF(("import_proc/asid = %X %u\n", state->as_info.ai_asid, 11977c478bd9Sstevel@tonic-gate state->as_info.ai_asid)); 11987c478bd9Sstevel@tonic-gate 11997c478bd9Sstevel@tonic-gate DPRINTF(("import_proc/masks = %X %X\n", 12007c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_success, 12017c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_failure)); 12027c478bd9Sstevel@tonic-gate 1203c529a23fSgww if (state->as_label == NULL) { 12047c478bd9Sstevel@tonic-gate *external = malloc(sizeof (adt_export_data_t)); 1205c529a23fSgww } else { 1206c529a23fSgww *external = malloc(sizeof (adt_export_data_t) + blabel_size()); 1207c529a23fSgww } 12087c478bd9Sstevel@tonic-gate 12097c478bd9Sstevel@tonic-gate if (*external == NULL) 12107c478bd9Sstevel@tonic-gate goto return_all_free; 12117c478bd9Sstevel@tonic-gate 12127c478bd9Sstevel@tonic-gate length = adt_to_export_format(*external, state); 12137c478bd9Sstevel@tonic-gate /* 12147c478bd9Sstevel@tonic-gate * yes, state is supposed to be free'd for both pass and fail 12157c478bd9Sstevel@tonic-gate */ 12167c478bd9Sstevel@tonic-gate return_all_free: 12177c478bd9Sstevel@tonic-gate ucred_free(ucred); 12187c478bd9Sstevel@tonic-gate return_length_free: 12197c478bd9Sstevel@tonic-gate free(state); 12207c478bd9Sstevel@tonic-gate return (length); 12217c478bd9Sstevel@tonic-gate } 12227c478bd9Sstevel@tonic-gate 12237c478bd9Sstevel@tonic-gate /* 1224c529a23fSgww * adt_ucred_label() -- if label is available, duplicate it. 1225c529a23fSgww */ 1226c529a23fSgww 1227c529a23fSgww static m_label_t * 1228c529a23fSgww adt_ucred_label(ucred_t *uc) 1229c529a23fSgww { 1230c529a23fSgww m_label_t *ul = NULL; 1231c529a23fSgww 1232c529a23fSgww if (ucred_getlabel(uc) != NULL) { 1233c529a23fSgww (void) m_label_dup(&ul, ucred_getlabel(uc)); 1234c529a23fSgww } 1235c529a23fSgww 1236c529a23fSgww return (ul); 1237c529a23fSgww } 1238c529a23fSgww 1239c529a23fSgww /* 12407c478bd9Sstevel@tonic-gate * adt_import() -- convert from network order to machine-specific order 12417c478bd9Sstevel@tonic-gate */ 124226fba2a6Sgww 12437c478bd9Sstevel@tonic-gate static int 12447c478bd9Sstevel@tonic-gate adt_import(adt_internal_state_t *internal, const adt_export_data_t *external) 12457c478bd9Sstevel@tonic-gate { 12467c478bd9Sstevel@tonic-gate au_mask_t mask; 12477c478bd9Sstevel@tonic-gate 12487c478bd9Sstevel@tonic-gate /* save local audit enabled state */ 12497c478bd9Sstevel@tonic-gate int local_audit_enabled = internal->as_audit_enabled; 12507c478bd9Sstevel@tonic-gate 12517c478bd9Sstevel@tonic-gate if (adt_from_export_format(internal, external) < 1) 12527c478bd9Sstevel@tonic-gate return (-1); /* errno from adt_from_export_format */ 12537c478bd9Sstevel@tonic-gate 12547c478bd9Sstevel@tonic-gate /* 12557c478bd9Sstevel@tonic-gate * If audit isn't enabled on the remote, they were unable 12567c478bd9Sstevel@tonic-gate * to generate the audit mask, so generate it based on 12577c478bd9Sstevel@tonic-gate * local configuration. If the user id has changed, the 12587c478bd9Sstevel@tonic-gate * resulting mask may miss some subtleties that occurred 12597c478bd9Sstevel@tonic-gate * on the remote system. 12607c478bd9Sstevel@tonic-gate * 12617c478bd9Sstevel@tonic-gate * If the remote failed to generate a terminal id, it is not 12627c478bd9Sstevel@tonic-gate * recoverable. 12637c478bd9Sstevel@tonic-gate */ 12647c478bd9Sstevel@tonic-gate 12657c478bd9Sstevel@tonic-gate if (!internal->as_audit_enabled) { 12667c478bd9Sstevel@tonic-gate if (adt_get_mask_from_user(internal->as_info.ai_auid, 12677c478bd9Sstevel@tonic-gate &(internal->as_info.ai_mask))) 12687c478bd9Sstevel@tonic-gate return (-1); 12697c478bd9Sstevel@tonic-gate if (internal->as_info.ai_auid != internal->as_ruid) { 12707c478bd9Sstevel@tonic-gate if (adt_get_mask_from_user(internal->as_info.ai_auid, 12717c478bd9Sstevel@tonic-gate &mask)) 12727c478bd9Sstevel@tonic-gate return (-1); 12737c478bd9Sstevel@tonic-gate internal->as_info.ai_mask.am_success |= 12747c478bd9Sstevel@tonic-gate mask.am_success; 12757c478bd9Sstevel@tonic-gate internal->as_info.ai_mask.am_failure |= 12767c478bd9Sstevel@tonic-gate mask.am_failure; 12777c478bd9Sstevel@tonic-gate } 12787c478bd9Sstevel@tonic-gate } 12797c478bd9Sstevel@tonic-gate internal->as_audit_enabled = local_audit_enabled; 12807c478bd9Sstevel@tonic-gate 12817c478bd9Sstevel@tonic-gate DPRINTF(("(%d)imported asid = %X %u\n", getpid(), 12827c478bd9Sstevel@tonic-gate internal->as_info.ai_asid, 12837c478bd9Sstevel@tonic-gate internal->as_info.ai_asid)); 12847c478bd9Sstevel@tonic-gate 12857c478bd9Sstevel@tonic-gate internal->as_have_user_data = ADT_HAVE_ALL; 12867c478bd9Sstevel@tonic-gate 12877c478bd9Sstevel@tonic-gate return (0); 12887c478bd9Sstevel@tonic-gate } 12897c478bd9Sstevel@tonic-gate 12907c478bd9Sstevel@tonic-gate /* 12917c478bd9Sstevel@tonic-gate * adt_export_session_data() 12927c478bd9Sstevel@tonic-gate * copies a adt_session_data struct into a network order buffer 12937c478bd9Sstevel@tonic-gate * 12947c478bd9Sstevel@tonic-gate * In a misconfigured network, the local host may have auditing 12957c478bd9Sstevel@tonic-gate * off while the destination may have auditing on, so if there 12967c478bd9Sstevel@tonic-gate * is sufficient memory, a buffer will be returned even in the 12977c478bd9Sstevel@tonic-gate * audit off case. 12987c478bd9Sstevel@tonic-gate */ 129926fba2a6Sgww 13007c478bd9Sstevel@tonic-gate size_t 13017c478bd9Sstevel@tonic-gate adt_export_session_data(const adt_session_data_t *internal, 13027c478bd9Sstevel@tonic-gate adt_export_data_t **external) 13037c478bd9Sstevel@tonic-gate { 13047c478bd9Sstevel@tonic-gate size_t length = 0; 13057c478bd9Sstevel@tonic-gate 1306dd6707eeSgww if ((internal != NULL) && 1307dd6707eeSgww ((adt_internal_state_t *)internal)->as_label != NULL) { 1308c529a23fSgww length = blabel_size(); 1309c529a23fSgww } 1310c529a23fSgww 1311c529a23fSgww *external = malloc(sizeof (adt_export_data_t) + length); 13127c478bd9Sstevel@tonic-gate 13137c478bd9Sstevel@tonic-gate if (*external == NULL) 1314c529a23fSgww return (0); 13157c478bd9Sstevel@tonic-gate 13167c478bd9Sstevel@tonic-gate if (internal == NULL) { 1317c529a23fSgww adt_internal_state_t *dummy; 1318c529a23fSgww 13197c478bd9Sstevel@tonic-gate dummy = malloc(sizeof (adt_internal_state_t)); 13207c478bd9Sstevel@tonic-gate if (dummy == NULL) 13217c478bd9Sstevel@tonic-gate goto return_length_free; 13227c478bd9Sstevel@tonic-gate 13237c478bd9Sstevel@tonic-gate if (adt_init(dummy, 0)) { /* 0 == don't copy from proc */ 13247c478bd9Sstevel@tonic-gate free(dummy); 13257c478bd9Sstevel@tonic-gate goto return_length_free; 13267c478bd9Sstevel@tonic-gate } 13277c478bd9Sstevel@tonic-gate length = adt_to_export_format(*external, dummy); 13287c478bd9Sstevel@tonic-gate free(dummy); 13297c478bd9Sstevel@tonic-gate } else { 13307c478bd9Sstevel@tonic-gate length = adt_to_export_format(*external, 13317c478bd9Sstevel@tonic-gate (adt_internal_state_t *)internal); 13327c478bd9Sstevel@tonic-gate } 13337c478bd9Sstevel@tonic-gate return (length); 13347c478bd9Sstevel@tonic-gate 13357c478bd9Sstevel@tonic-gate return_length_free: 13367c478bd9Sstevel@tonic-gate free(*external); 13377c478bd9Sstevel@tonic-gate *external = NULL; 1338c529a23fSgww return (0); 13397c478bd9Sstevel@tonic-gate } 13407c478bd9Sstevel@tonic-gate 13417c478bd9Sstevel@tonic-gate static void 13427c478bd9Sstevel@tonic-gate adt_setto_unaudited(adt_internal_state_t *state) 13437c478bd9Sstevel@tonic-gate { 13447c478bd9Sstevel@tonic-gate state->as_ruid = AU_NOAUDITID; 13457c478bd9Sstevel@tonic-gate state->as_euid = AU_NOAUDITID; 13467c478bd9Sstevel@tonic-gate state->as_rgid = AU_NOAUDITID; 13477c478bd9Sstevel@tonic-gate state->as_egid = AU_NOAUDITID; 1348c529a23fSgww state->as_pid = (pid_t)-1; 1349c529a23fSgww state->as_label = NULL; 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate if (state->as_audit_enabled) { 13527c478bd9Sstevel@tonic-gate state->as_info.ai_asid = 0; 13537c478bd9Sstevel@tonic-gate state->as_info.ai_auid = AU_NOAUDITID; 13547c478bd9Sstevel@tonic-gate 13557c478bd9Sstevel@tonic-gate (void) memset((void *)&(state->as_info.ai_termid), 0, 13567c478bd9Sstevel@tonic-gate sizeof (au_tid_addr_t)); 13577c478bd9Sstevel@tonic-gate state->as_info.ai_termid.at_type = AU_IPv4; 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate (void) memset((void *)&(state->as_info.ai_mask), 0, 13607c478bd9Sstevel@tonic-gate sizeof (au_mask_t)); 13617c478bd9Sstevel@tonic-gate state->as_have_user_data = 0; 13627c478bd9Sstevel@tonic-gate } 13637c478bd9Sstevel@tonic-gate } 13647c478bd9Sstevel@tonic-gate 13657c478bd9Sstevel@tonic-gate /* 13667c478bd9Sstevel@tonic-gate * adt_init -- set session context by copying the audit characteristics 13677c478bd9Sstevel@tonic-gate * from the proc and picking up current uid/tid information. 13687c478bd9Sstevel@tonic-gate * 13697c478bd9Sstevel@tonic-gate * By default, an audit session is based on the process; the default 13707c478bd9Sstevel@tonic-gate * is overriden by adt_set_user() 13717c478bd9Sstevel@tonic-gate */ 137226fba2a6Sgww 13737c478bd9Sstevel@tonic-gate static int 13747c478bd9Sstevel@tonic-gate adt_init(adt_internal_state_t *state, int use_proc_data) 13757c478bd9Sstevel@tonic-gate { 13767c478bd9Sstevel@tonic-gate 13777c478bd9Sstevel@tonic-gate state->as_audit_enabled = (auditstate == AUC_DISABLED) ? 0 : 1; 13787c478bd9Sstevel@tonic-gate 13797c478bd9Sstevel@tonic-gate if (use_proc_data) { 13807c478bd9Sstevel@tonic-gate state->as_ruid = getuid(); 13817c478bd9Sstevel@tonic-gate state->as_euid = geteuid(); 13827c478bd9Sstevel@tonic-gate state->as_rgid = getgid(); 13837c478bd9Sstevel@tonic-gate state->as_egid = getegid(); 1384c529a23fSgww state->as_pid = getpid(); 13857c478bd9Sstevel@tonic-gate 13867c478bd9Sstevel@tonic-gate if (state->as_audit_enabled) { 13877c478bd9Sstevel@tonic-gate const au_tid64_addr_t *tid; 13887c478bd9Sstevel@tonic-gate const au_mask_t *mask; 138926fba2a6Sgww ucred_t *ucred = ucred_get(P_MYID); 13907c478bd9Sstevel@tonic-gate 13917c478bd9Sstevel@tonic-gate /* 13927c478bd9Sstevel@tonic-gate * Even if the ucred is NULL, the underlying 13937c478bd9Sstevel@tonic-gate * credential may have a valid terminal id; if the 13947c478bd9Sstevel@tonic-gate * terminal id is set, then that's good enough. An 13957c478bd9Sstevel@tonic-gate * example of where this matters is failed login, 13967c478bd9Sstevel@tonic-gate * where rlogin/telnet sets the terminal id before 13977c478bd9Sstevel@tonic-gate * calling login; login does not load the credential 13987c478bd9Sstevel@tonic-gate * since auth failed. 13997c478bd9Sstevel@tonic-gate */ 14007c478bd9Sstevel@tonic-gate if (ucred == NULL) { 14017c478bd9Sstevel@tonic-gate if (!adt_have_termid( 14027c478bd9Sstevel@tonic-gate &(state->as_info.ai_termid))) 14037c478bd9Sstevel@tonic-gate return (-1); 14047c478bd9Sstevel@tonic-gate } else { 14057c478bd9Sstevel@tonic-gate mask = ucred_getamask(ucred); 14067c478bd9Sstevel@tonic-gate if (mask != NULL) { 14077c478bd9Sstevel@tonic-gate state->as_info.ai_mask = *mask; 14087c478bd9Sstevel@tonic-gate } else { 14097c478bd9Sstevel@tonic-gate ucred_free(ucred); 14107c478bd9Sstevel@tonic-gate return (-1); 14117c478bd9Sstevel@tonic-gate } 14127c478bd9Sstevel@tonic-gate tid = ucred_getatid(ucred); 14137c478bd9Sstevel@tonic-gate if (tid != NULL) { 14147c478bd9Sstevel@tonic-gate adt_cpy_tid(&(state->as_info.ai_termid), 14157c478bd9Sstevel@tonic-gate tid); 14167c478bd9Sstevel@tonic-gate } else { 14177c478bd9Sstevel@tonic-gate ucred_free(ucred); 14187c478bd9Sstevel@tonic-gate return (-1); 14197c478bd9Sstevel@tonic-gate } 14207c478bd9Sstevel@tonic-gate state->as_info.ai_asid = ucred_getasid(ucred); 14217c478bd9Sstevel@tonic-gate state->as_info.ai_auid = ucred_getauid(ucred); 1422c529a23fSgww state->as_label = adt_ucred_label(ucred); 14237c478bd9Sstevel@tonic-gate ucred_free(ucred); 14247c478bd9Sstevel@tonic-gate } 14257c478bd9Sstevel@tonic-gate state->as_have_user_data = ADT_HAVE_ALL; 14267c478bd9Sstevel@tonic-gate } 14277c478bd9Sstevel@tonic-gate } else { 14287c478bd9Sstevel@tonic-gate adt_setto_unaudited(state); 14297c478bd9Sstevel@tonic-gate } 14307c478bd9Sstevel@tonic-gate state->as_session_model = ADT_SESSION_MODEL; /* default */ 14317c478bd9Sstevel@tonic-gate 14327c478bd9Sstevel@tonic-gate if (state->as_audit_enabled && 14337c478bd9Sstevel@tonic-gate auditon(A_GETPOLICY, (caddr_t)&(state->as_kernel_audit_policy), 14347c478bd9Sstevel@tonic-gate sizeof (state->as_kernel_audit_policy))) { 14357c478bd9Sstevel@tonic-gate return (-1); /* errno set by auditon */ 14367c478bd9Sstevel@tonic-gate } 14377c478bd9Sstevel@tonic-gate state->as_check = ADT_VALID; 14387c478bd9Sstevel@tonic-gate return (0); 14397c478bd9Sstevel@tonic-gate } 14407c478bd9Sstevel@tonic-gate 14417c478bd9Sstevel@tonic-gate /* 14427c478bd9Sstevel@tonic-gate * adt_set_proc 14437c478bd9Sstevel@tonic-gate * 14447c478bd9Sstevel@tonic-gate * Copy the current session state to the process. If this function 14457c478bd9Sstevel@tonic-gate * is called, the model becomes a process model rather than a 14467c478bd9Sstevel@tonic-gate * session model. 14477c478bd9Sstevel@tonic-gate * 14487c478bd9Sstevel@tonic-gate * In the current implementation, the value state->as_have_user_data 14498c3c55e7Spaulson * must contain all of: ADT_HAVE_{AUID,MASK,TID,ASID}. These are all set 14508c3c55e7Spaulson * by adt_set_user() when the ADT_SETTID or ADT_NEW flag is passed in. 14517c478bd9Sstevel@tonic-gate * 14527c478bd9Sstevel@tonic-gate */ 14537c478bd9Sstevel@tonic-gate 14547c478bd9Sstevel@tonic-gate int 14557c478bd9Sstevel@tonic-gate adt_set_proc(const adt_session_data_t *session_data) 14567c478bd9Sstevel@tonic-gate { 14577c478bd9Sstevel@tonic-gate int rc; 14587c478bd9Sstevel@tonic-gate adt_internal_state_t *state; 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate if (auditstate == AUC_DISABLED || (session_data == NULL)) 14617c478bd9Sstevel@tonic-gate return (0); 14627c478bd9Sstevel@tonic-gate 14637c478bd9Sstevel@tonic-gate state = (adt_internal_state_t *)session_data; 14647c478bd9Sstevel@tonic-gate 14657c478bd9Sstevel@tonic-gate assert(state->as_check == ADT_VALID); 14667c478bd9Sstevel@tonic-gate 14678c3c55e7Spaulson if ((state->as_have_user_data & (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) != 14688c3c55e7Spaulson (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) { 14697c478bd9Sstevel@tonic-gate errno = EINVAL; 14707c478bd9Sstevel@tonic-gate goto return_err; 14717c478bd9Sstevel@tonic-gate } 14727c478bd9Sstevel@tonic-gate 14738c3c55e7Spaulson rc = setaudit_addr((auditinfo_addr_t *)&(state->as_info), 14748c3c55e7Spaulson sizeof (auditinfo_addr_t)); 14757c478bd9Sstevel@tonic-gate 14767c478bd9Sstevel@tonic-gate if (rc < 0) 14777c478bd9Sstevel@tonic-gate goto return_err; /* errno set by setaudit_addr() */ 14787c478bd9Sstevel@tonic-gate 14797c478bd9Sstevel@tonic-gate state->as_session_model = ADT_PROCESS_MODEL; 14807c478bd9Sstevel@tonic-gate 14817c478bd9Sstevel@tonic-gate return (0); 14827c478bd9Sstevel@tonic-gate 14837c478bd9Sstevel@tonic-gate return_err: 14847c478bd9Sstevel@tonic-gate adt_write_syslog("failed to set process audit characteristics", errno); 14857c478bd9Sstevel@tonic-gate return (-1); 14867c478bd9Sstevel@tonic-gate } 14877c478bd9Sstevel@tonic-gate 14887c478bd9Sstevel@tonic-gate static int 14897c478bd9Sstevel@tonic-gate adt_newuser(adt_internal_state_t *state, uid_t ruid, au_tid_addr_t *termid) 14907c478bd9Sstevel@tonic-gate { 14917c478bd9Sstevel@tonic-gate au_tid_addr_t no_tid = {0, AU_IPv4, 0, 0, 0, 0}; 14927c478bd9Sstevel@tonic-gate au_mask_t no_mask = {0, 0}; 14937c478bd9Sstevel@tonic-gate 14947c478bd9Sstevel@tonic-gate if (ruid == ADT_NO_AUDIT) { 14957c478bd9Sstevel@tonic-gate state->as_info.ai_auid = AU_NOAUDITID; 14967c478bd9Sstevel@tonic-gate state->as_info.ai_asid = 0; 14977c478bd9Sstevel@tonic-gate state->as_info.ai_termid = no_tid; 14987c478bd9Sstevel@tonic-gate state->as_info.ai_mask = no_mask; 14997c478bd9Sstevel@tonic-gate return (0); 15007c478bd9Sstevel@tonic-gate } 15017c478bd9Sstevel@tonic-gate state->as_info.ai_auid = ruid; 15027c478bd9Sstevel@tonic-gate state->as_info.ai_asid = adt_get_unique_id(ruid); 15037c478bd9Sstevel@tonic-gate if (termid != NULL) 15047c478bd9Sstevel@tonic-gate state->as_info.ai_termid = *termid; 15057c478bd9Sstevel@tonic-gate 15067c478bd9Sstevel@tonic-gate if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask))) 15077c478bd9Sstevel@tonic-gate return (-1); 15087c478bd9Sstevel@tonic-gate 15092e74cda7Sgww /* Assume intending to audit as this process */ 15102e74cda7Sgww 15112e74cda7Sgww if (state->as_pid == (pid_t)-1) 15122e74cda7Sgww state->as_pid = getpid(); 15132e74cda7Sgww 15142e74cda7Sgww if (is_system_labeled() && state->as_label == NULL) { 15152e74cda7Sgww ucred_t *ucred = ucred_get(P_MYID); 15162e74cda7Sgww 15172e74cda7Sgww state->as_label = adt_ucred_label(ucred); 15182e74cda7Sgww ucred_free(ucred); 15192e74cda7Sgww } 15202e74cda7Sgww 15217c478bd9Sstevel@tonic-gate return (0); 15227c478bd9Sstevel@tonic-gate } 152326fba2a6Sgww 15247c478bd9Sstevel@tonic-gate static int 15257c478bd9Sstevel@tonic-gate adt_changeuser(adt_internal_state_t *state, uid_t ruid) 15267c478bd9Sstevel@tonic-gate { 15277c478bd9Sstevel@tonic-gate au_mask_t mask; 15287c478bd9Sstevel@tonic-gate 15297c478bd9Sstevel@tonic-gate if (!(state->as_have_user_data & ADT_HAVE_AUID)) 15307c478bd9Sstevel@tonic-gate state->as_info.ai_auid = ruid; 15317c478bd9Sstevel@tonic-gate if (!(state->as_have_user_data & ADT_HAVE_ASID)) 15327c478bd9Sstevel@tonic-gate state->as_info.ai_asid = adt_get_unique_id(ruid); 15337c478bd9Sstevel@tonic-gate 1534f48205beScasper if (ruid <= MAXEPHUID) { 15357c478bd9Sstevel@tonic-gate if (adt_get_mask_from_user(ruid, &mask)) 15367c478bd9Sstevel@tonic-gate return (-1); 15377c478bd9Sstevel@tonic-gate 15387c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_success |= mask.am_success; 15397c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_failure |= mask.am_failure; 15407c478bd9Sstevel@tonic-gate } 15417c478bd9Sstevel@tonic-gate DPRINTF(("changed mask to %08X/%08X for ruid=%d\n", 15427c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_success, 15437c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_failure, 15447c478bd9Sstevel@tonic-gate ruid)); 15457c478bd9Sstevel@tonic-gate return (0); 15467c478bd9Sstevel@tonic-gate } 154726fba2a6Sgww 15487c478bd9Sstevel@tonic-gate /* 15497c478bd9Sstevel@tonic-gate * adt_set_user -- see also adt_set_from_ucred() 15507c478bd9Sstevel@tonic-gate * 15517c478bd9Sstevel@tonic-gate * ADT_NO_ATTRIB is a valid uid/gid meaning "not known" or 155226fba2a6Sgww * "unattributed." If ruid, change the model to session. 15537c478bd9Sstevel@tonic-gate * 15547c478bd9Sstevel@tonic-gate * ADT_NO_CHANGE is a valid uid/gid meaning "do not change this value" 15557c478bd9Sstevel@tonic-gate * only valid with ADT_UPDATE. 15567c478bd9Sstevel@tonic-gate * 155726fba2a6Sgww * ADT_NO_AUDIT is the external equivalent to AU_NOAUDITID -- there 15587c478bd9Sstevel@tonic-gate * isn't a good reason to call adt_set_user() with it unless you don't 15597c478bd9Sstevel@tonic-gate * have a good value yet and intend to replace it later; auid will be 15607c478bd9Sstevel@tonic-gate * AU_NOAUDITID. 15617c478bd9Sstevel@tonic-gate * 15627c478bd9Sstevel@tonic-gate * adt_set_user should be called even if auditing is not enabled 15637c478bd9Sstevel@tonic-gate * so that adt_export_session_data() will have useful stuff to 15647c478bd9Sstevel@tonic-gate * work with. 15657c478bd9Sstevel@tonic-gate * 15667c478bd9Sstevel@tonic-gate * See the note preceding adt_set_proc() about the use of ADT_HAVE_TID 15677c478bd9Sstevel@tonic-gate * and ADT_HAVE_ALL. 15687c478bd9Sstevel@tonic-gate */ 156926fba2a6Sgww 15707c478bd9Sstevel@tonic-gate int 157126fba2a6Sgww adt_set_user(const adt_session_data_t *session_data, uid_t euid, gid_t egid, 157226fba2a6Sgww uid_t ruid, gid_t rgid, const adt_termid_t *termid, 15737c478bd9Sstevel@tonic-gate enum adt_user_context user_context) 15747c478bd9Sstevel@tonic-gate { 15757c478bd9Sstevel@tonic-gate adt_internal_state_t *state; 15767c478bd9Sstevel@tonic-gate int rc; 15777c478bd9Sstevel@tonic-gate 15787c478bd9Sstevel@tonic-gate if (session_data == NULL) /* no session exists to audit */ 15797c478bd9Sstevel@tonic-gate return (0); 15807c478bd9Sstevel@tonic-gate 15817c478bd9Sstevel@tonic-gate state = (adt_internal_state_t *)session_data; 15827c478bd9Sstevel@tonic-gate assert(state->as_check == ADT_VALID); 15837c478bd9Sstevel@tonic-gate 15847c478bd9Sstevel@tonic-gate switch (user_context) { 15857c478bd9Sstevel@tonic-gate case ADT_NEW: 15867c478bd9Sstevel@tonic-gate if (ruid == ADT_NO_CHANGE || euid == ADT_NO_CHANGE || 15877c478bd9Sstevel@tonic-gate rgid == ADT_NO_CHANGE || egid == ADT_NO_CHANGE) { 15887c478bd9Sstevel@tonic-gate errno = EINVAL; 15897c478bd9Sstevel@tonic-gate return (-1); 15907c478bd9Sstevel@tonic-gate } 15917c478bd9Sstevel@tonic-gate if ((rc = adt_newuser(state, ruid, 15927c478bd9Sstevel@tonic-gate (au_tid_addr_t *)termid)) != 0) 15937c478bd9Sstevel@tonic-gate return (rc); 15947c478bd9Sstevel@tonic-gate 15957c478bd9Sstevel@tonic-gate state->as_have_user_data = ADT_HAVE_ALL; 15967c478bd9Sstevel@tonic-gate break; 15977c478bd9Sstevel@tonic-gate case ADT_UPDATE: 15987c478bd9Sstevel@tonic-gate if (state->as_have_user_data != ADT_HAVE_ALL) { 15997c478bd9Sstevel@tonic-gate errno = EINVAL; 16007c478bd9Sstevel@tonic-gate return (-1); 16017c478bd9Sstevel@tonic-gate } 16027c478bd9Sstevel@tonic-gate 16037c478bd9Sstevel@tonic-gate if (ruid != ADT_NO_CHANGE) 16047c478bd9Sstevel@tonic-gate if ((rc = adt_changeuser(state, ruid)) != 0) 16057c478bd9Sstevel@tonic-gate return (rc); 16067c478bd9Sstevel@tonic-gate break; 16077c478bd9Sstevel@tonic-gate case ADT_USER: 16087c478bd9Sstevel@tonic-gate if (state->as_have_user_data != ADT_HAVE_ALL) { 16097c478bd9Sstevel@tonic-gate errno = EINVAL; 16107c478bd9Sstevel@tonic-gate return (-1); 16117c478bd9Sstevel@tonic-gate } 16127c478bd9Sstevel@tonic-gate break; 16137c478bd9Sstevel@tonic-gate case ADT_SETTID: 16147c478bd9Sstevel@tonic-gate assert(termid != NULL); 16157c478bd9Sstevel@tonic-gate state->as_info.ai_termid = *((au_tid_addr_t *)termid); 16167c478bd9Sstevel@tonic-gate /* avoid fooling pam_setcred()... */ 16177c478bd9Sstevel@tonic-gate state->as_info.ai_auid = AU_NOAUDITID; 16187c478bd9Sstevel@tonic-gate state->as_info.ai_asid = 0; 16197c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_failure = 0; 16207c478bd9Sstevel@tonic-gate state->as_info.ai_mask.am_success = 0; 16217c478bd9Sstevel@tonic-gate state->as_have_user_data = ADT_HAVE_TID | 16227c478bd9Sstevel@tonic-gate ADT_HAVE_AUID | ADT_HAVE_ASID | ADT_HAVE_MASK; 16237c478bd9Sstevel@tonic-gate return (0); 16247c478bd9Sstevel@tonic-gate default: 16257c478bd9Sstevel@tonic-gate errno = EINVAL; 16267c478bd9Sstevel@tonic-gate return (-1); 16277c478bd9Sstevel@tonic-gate } 16287c478bd9Sstevel@tonic-gate 16297c478bd9Sstevel@tonic-gate if (ruid == ADT_NO_AUDIT) { 16307c478bd9Sstevel@tonic-gate state->as_ruid = AU_NOAUDITID; 16317c478bd9Sstevel@tonic-gate state->as_euid = AU_NOAUDITID; 16327c478bd9Sstevel@tonic-gate state->as_rgid = AU_NOAUDITID; 16337c478bd9Sstevel@tonic-gate state->as_egid = AU_NOAUDITID; 16347c478bd9Sstevel@tonic-gate } else { 16357c478bd9Sstevel@tonic-gate if (ruid != ADT_NO_CHANGE) 16367c478bd9Sstevel@tonic-gate state->as_ruid = ruid; 16377c478bd9Sstevel@tonic-gate if (euid != ADT_NO_CHANGE) 16387c478bd9Sstevel@tonic-gate state->as_euid = euid; 16397c478bd9Sstevel@tonic-gate if (rgid != ADT_NO_CHANGE) 16407c478bd9Sstevel@tonic-gate state->as_rgid = rgid; 16417c478bd9Sstevel@tonic-gate if (egid != ADT_NO_CHANGE) 16427c478bd9Sstevel@tonic-gate state->as_egid = egid; 16437c478bd9Sstevel@tonic-gate } 16447c478bd9Sstevel@tonic-gate 164526fba2a6Sgww if (ruid == ADT_NO_ATTRIB) { 164626fba2a6Sgww state->as_session_model = ADT_SESSION_MODEL; 164726fba2a6Sgww } 164826fba2a6Sgww 16497c478bd9Sstevel@tonic-gate return (0); 16507c478bd9Sstevel@tonic-gate } 165126fba2a6Sgww 16527c478bd9Sstevel@tonic-gate /* 16537c478bd9Sstevel@tonic-gate * adt_set_from_ucred() 16547c478bd9Sstevel@tonic-gate * 16557c478bd9Sstevel@tonic-gate * an alternate to adt_set_user that fills the same role but uses 16567c478bd9Sstevel@tonic-gate * a pointer to a ucred rather than a list of id's. If the ucred 16577c478bd9Sstevel@tonic-gate * pointer is NULL, use the credential from the this process. 16587c478bd9Sstevel@tonic-gate * 16597c478bd9Sstevel@tonic-gate * A key difference is that for ADT_NEW, adt_set_from_ucred() does 16607c478bd9Sstevel@tonic-gate * not overwrite the asid and auid unless auid has not been set. 16617c478bd9Sstevel@tonic-gate * ADT_NEW differs from ADT_UPDATE in that it does not OR together 16627c478bd9Sstevel@tonic-gate * the incoming audit mask with the one that already exists. 16637c478bd9Sstevel@tonic-gate * 16647c478bd9Sstevel@tonic-gate * adt_set_from_ucred should be called even if auditing is not enabled 16657c478bd9Sstevel@tonic-gate * so that adt_export_session_data() will have useful stuff to 16667c478bd9Sstevel@tonic-gate * work with. 16677c478bd9Sstevel@tonic-gate */ 166826fba2a6Sgww 16697c478bd9Sstevel@tonic-gate int 167026fba2a6Sgww adt_set_from_ucred(const adt_session_data_t *session_data, const ucred_t *uc, 167126fba2a6Sgww enum adt_user_context user_context) 16727c478bd9Sstevel@tonic-gate { 16737c478bd9Sstevel@tonic-gate adt_internal_state_t *state; 16747c478bd9Sstevel@tonic-gate int rc = -1; 16757c478bd9Sstevel@tonic-gate const au_tid64_addr_t *tid64; 16767c478bd9Sstevel@tonic-gate au_tid_addr_t termid, *tid; 167726706799Sme23304 ucred_t *ucred = (ucred_t *)uc; 167826706799Sme23304 boolean_t local_uc = B_FALSE; 16797c478bd9Sstevel@tonic-gate 16807c478bd9Sstevel@tonic-gate if (session_data == NULL) /* no session exists to audit */ 16817c478bd9Sstevel@tonic-gate return (0); 16827c478bd9Sstevel@tonic-gate 16837c478bd9Sstevel@tonic-gate state = (adt_internal_state_t *)session_data; 16847c478bd9Sstevel@tonic-gate assert(state->as_check == ADT_VALID); 16857c478bd9Sstevel@tonic-gate 16867c478bd9Sstevel@tonic-gate if (ucred == NULL) { 1687b3baaabfSgww ucred = ucred_get(P_MYID); 16887c478bd9Sstevel@tonic-gate 16897c478bd9Sstevel@tonic-gate if (ucred == NULL) 16907c478bd9Sstevel@tonic-gate goto return_rc; 169126706799Sme23304 local_uc = B_TRUE; 16927c478bd9Sstevel@tonic-gate } 16937c478bd9Sstevel@tonic-gate 16947c478bd9Sstevel@tonic-gate switch (user_context) { 16957c478bd9Sstevel@tonic-gate case ADT_NEW: 16967c478bd9Sstevel@tonic-gate tid64 = ucred_getatid(ucred); 16977c478bd9Sstevel@tonic-gate if (tid64 != NULL) { 16987c478bd9Sstevel@tonic-gate adt_cpy_tid(&termid, tid64); 16997c478bd9Sstevel@tonic-gate tid = &termid; 17007c478bd9Sstevel@tonic-gate } else { 17017c478bd9Sstevel@tonic-gate tid = NULL; 17027c478bd9Sstevel@tonic-gate } 17037c478bd9Sstevel@tonic-gate if (ucred_getauid(ucred) == AU_NOAUDITID) { 1704b3baaabfSgww adt_setto_unaudited(state); 1705b3baaabfSgww state->as_have_user_data = ADT_HAVE_ALL; 1706b3baaabfSgww rc = 0; 17077c478bd9Sstevel@tonic-gate goto return_rc; 17087c478bd9Sstevel@tonic-gate } else { 17097c478bd9Sstevel@tonic-gate state->as_info.ai_auid = ucred_getauid(ucred); 17107c478bd9Sstevel@tonic-gate state->as_info.ai_asid = ucred_getasid(ucred); 17117c478bd9Sstevel@tonic-gate state->as_info.ai_mask = *ucred_getamask(ucred); 17127c478bd9Sstevel@tonic-gate state->as_info.ai_termid = *tid; 17137c478bd9Sstevel@tonic-gate } 17147c478bd9Sstevel@tonic-gate state->as_have_user_data = ADT_HAVE_ALL; 17157c478bd9Sstevel@tonic-gate break; 17167c478bd9Sstevel@tonic-gate case ADT_UPDATE: 17177c478bd9Sstevel@tonic-gate if (state->as_have_user_data != ADT_HAVE_ALL) { 17187c478bd9Sstevel@tonic-gate errno = EINVAL; 17197c478bd9Sstevel@tonic-gate goto return_rc; 17207c478bd9Sstevel@tonic-gate } 17217c478bd9Sstevel@tonic-gate 17227c478bd9Sstevel@tonic-gate if ((rc = adt_changeuser(state, ucred_getruid(ucred))) != 0) 17237c478bd9Sstevel@tonic-gate goto return_rc; 17247c478bd9Sstevel@tonic-gate break; 17257c478bd9Sstevel@tonic-gate case ADT_USER: 17267c478bd9Sstevel@tonic-gate if (state->as_have_user_data != ADT_HAVE_ALL) { 17277c478bd9Sstevel@tonic-gate errno = EINVAL; 17287c478bd9Sstevel@tonic-gate goto return_rc; 17297c478bd9Sstevel@tonic-gate } 17307c478bd9Sstevel@tonic-gate break; 17317c478bd9Sstevel@tonic-gate default: 17327c478bd9Sstevel@tonic-gate errno = EINVAL; 17337c478bd9Sstevel@tonic-gate goto return_rc; 17347c478bd9Sstevel@tonic-gate } 17357c478bd9Sstevel@tonic-gate rc = 0; 17367c478bd9Sstevel@tonic-gate 17377c478bd9Sstevel@tonic-gate state->as_ruid = ucred_getruid(ucred); 17387c478bd9Sstevel@tonic-gate state->as_euid = ucred_geteuid(ucred); 17397c478bd9Sstevel@tonic-gate state->as_rgid = ucred_getrgid(ucred); 17407c478bd9Sstevel@tonic-gate state->as_egid = ucred_getegid(ucred); 1741c529a23fSgww state->as_pid = ucred_getpid(ucred); 1742c529a23fSgww state->as_label = adt_ucred_label(ucred); 17437c478bd9Sstevel@tonic-gate 17447c478bd9Sstevel@tonic-gate return_rc: 174526706799Sme23304 if (local_uc) { 174626706799Sme23304 ucred_free(ucred); 174726706799Sme23304 } 17487c478bd9Sstevel@tonic-gate return (rc); 17497c478bd9Sstevel@tonic-gate } 17507c478bd9Sstevel@tonic-gate 17517c478bd9Sstevel@tonic-gate /* 17527c478bd9Sstevel@tonic-gate * adt_alloc_event() returns a pointer to allocated memory 17537c478bd9Sstevel@tonic-gate * 17547c478bd9Sstevel@tonic-gate */ 17557c478bd9Sstevel@tonic-gate 17567c478bd9Sstevel@tonic-gate adt_event_data_t 17577c478bd9Sstevel@tonic-gate *adt_alloc_event(const adt_session_data_t *session_data, au_event_t event_id) 17587c478bd9Sstevel@tonic-gate { 17597c478bd9Sstevel@tonic-gate struct adt_event_state *event_state; 17607c478bd9Sstevel@tonic-gate adt_internal_state_t *session_state; 17617c478bd9Sstevel@tonic-gate adt_event_data_t *return_event = NULL; 17627c478bd9Sstevel@tonic-gate /* 17637c478bd9Sstevel@tonic-gate * need to return a valid event pointer even if audit is 17647c478bd9Sstevel@tonic-gate * off, else the caller will end up either (1) keeping its 17657c478bd9Sstevel@tonic-gate * own flags for on/off or (2) writing to a NULL pointer. 17667c478bd9Sstevel@tonic-gate * If auditing is on, the session data must be valid; otherwise 17677c478bd9Sstevel@tonic-gate * we don't care. 17687c478bd9Sstevel@tonic-gate */ 17697c478bd9Sstevel@tonic-gate if (session_data != NULL) { 17707c478bd9Sstevel@tonic-gate session_state = (adt_internal_state_t *)session_data; 17717c478bd9Sstevel@tonic-gate assert(session_state->as_check == ADT_VALID); 17727c478bd9Sstevel@tonic-gate } 17737c478bd9Sstevel@tonic-gate event_state = calloc(1, sizeof (struct adt_event_state)); 17747c478bd9Sstevel@tonic-gate if (event_state == NULL) 17757c478bd9Sstevel@tonic-gate goto return_ptr; 17767c478bd9Sstevel@tonic-gate 17777c478bd9Sstevel@tonic-gate event_state->ae_check = ADT_VALID; 17787c478bd9Sstevel@tonic-gate 17797c478bd9Sstevel@tonic-gate event_state->ae_event_id = event_id; 17807c478bd9Sstevel@tonic-gate event_state->ae_session = (struct adt_internal_state *)session_data; 17817c478bd9Sstevel@tonic-gate 17827c478bd9Sstevel@tonic-gate return_event = (adt_event_data_t *)&(event_state->ae_event_data); 17837c478bd9Sstevel@tonic-gate 17847c478bd9Sstevel@tonic-gate /* 17857c478bd9Sstevel@tonic-gate * preload data so the adt_au_*() functions can detect un-supplied 17867c478bd9Sstevel@tonic-gate * values (0 and NULL are free via calloc()). 17877c478bd9Sstevel@tonic-gate */ 17887c478bd9Sstevel@tonic-gate adt_preload(event_id, return_event); 17897c478bd9Sstevel@tonic-gate 17907c478bd9Sstevel@tonic-gate return_ptr: 17917c478bd9Sstevel@tonic-gate return (return_event); 17927c478bd9Sstevel@tonic-gate } 17937c478bd9Sstevel@tonic-gate 17947c478bd9Sstevel@tonic-gate /* 17957c478bd9Sstevel@tonic-gate * adt_getXlateTable -- look up translation table address for event id 17967c478bd9Sstevel@tonic-gate */ 17977c478bd9Sstevel@tonic-gate 17987c478bd9Sstevel@tonic-gate static struct translation * 17997c478bd9Sstevel@tonic-gate adt_getXlateTable(au_event_t event_id) 18007c478bd9Sstevel@tonic-gate { 18017c478bd9Sstevel@tonic-gate /* xlate_table is global in adt_xlate.c */ 18027c478bd9Sstevel@tonic-gate struct translation **p_xlate = &xlate_table[0]; 18037c478bd9Sstevel@tonic-gate struct translation *p_event; 18047c478bd9Sstevel@tonic-gate 18057c478bd9Sstevel@tonic-gate while (*p_xlate != NULL) { 18067c478bd9Sstevel@tonic-gate p_event = *p_xlate; 18077c478bd9Sstevel@tonic-gate if (event_id == p_event->tx_external_event) 18087c478bd9Sstevel@tonic-gate return (p_event); 18097c478bd9Sstevel@tonic-gate p_xlate++; 18107c478bd9Sstevel@tonic-gate } 18117c478bd9Sstevel@tonic-gate return (NULL); 18127c478bd9Sstevel@tonic-gate } 18137c478bd9Sstevel@tonic-gate 18147c478bd9Sstevel@tonic-gate /* 18157c478bd9Sstevel@tonic-gate * adt_calcOffsets 18167c478bd9Sstevel@tonic-gate * 18177c478bd9Sstevel@tonic-gate * the call to this function is surrounded by a mutex. 18187c478bd9Sstevel@tonic-gate * 18197c478bd9Sstevel@tonic-gate * i walks down the table picking up next_token. j walks again to 18207c478bd9Sstevel@tonic-gate * calculate the offset to the input data. k points to the next 18217c478bd9Sstevel@tonic-gate * token's row. Finally, l, is used to sum the values in the 18227c478bd9Sstevel@tonic-gate * datadef array. 18237c478bd9Sstevel@tonic-gate * 18247c478bd9Sstevel@tonic-gate * What's going on? The entry array is in the order of the input 18257c478bd9Sstevel@tonic-gate * fields but the processing of array entries is in the order of 18267c478bd9Sstevel@tonic-gate * the output (see next_token). Calculating the offset to the 18277c478bd9Sstevel@tonic-gate * "next" input can't be done in the outer loop (i) since i doesn't 18287c478bd9Sstevel@tonic-gate * point to the current entry and it can't be done with the k index 18297c478bd9Sstevel@tonic-gate * because it doesn't represent the order of input fields. 18307c478bd9Sstevel@tonic-gate * 18317c478bd9Sstevel@tonic-gate * While the resulting algorithm is n**2, it is only done once per 18327c478bd9Sstevel@tonic-gate * event type. 18337c478bd9Sstevel@tonic-gate */ 18347c478bd9Sstevel@tonic-gate 18357c478bd9Sstevel@tonic-gate /* 18367c478bd9Sstevel@tonic-gate * adt_calcOffsets is only called once per event type, but it uses 18377c478bd9Sstevel@tonic-gate * the address alignment of memory allocated for that event as if it 18387c478bd9Sstevel@tonic-gate * were the same for all subsequently allocated memory. This is 18397c478bd9Sstevel@tonic-gate * guaranteed by calloc/malloc. Arrays take special handling since 18407c478bd9Sstevel@tonic-gate * what matters for figuring out the correct alignment is the size 18417c478bd9Sstevel@tonic-gate * of the array element. 18427c478bd9Sstevel@tonic-gate */ 18437c478bd9Sstevel@tonic-gate 18447c478bd9Sstevel@tonic-gate static void 18457c478bd9Sstevel@tonic-gate adt_calcOffsets(struct entry *p_entry, int tablesize, void *p_data) 18467c478bd9Sstevel@tonic-gate { 18477c478bd9Sstevel@tonic-gate int i, j; 18487c478bd9Sstevel@tonic-gate size_t this_size, prev_size; 18497c478bd9Sstevel@tonic-gate void *struct_start = p_data; 18507c478bd9Sstevel@tonic-gate 18517c478bd9Sstevel@tonic-gate for (i = 0; i < tablesize; i++) { 18527c478bd9Sstevel@tonic-gate if (p_entry[i].en_type_def == NULL) { 18537c478bd9Sstevel@tonic-gate p_entry[i].en_offset = 0; 18547c478bd9Sstevel@tonic-gate continue; 18557c478bd9Sstevel@tonic-gate } 18567c478bd9Sstevel@tonic-gate prev_size = 0; 18577c478bd9Sstevel@tonic-gate p_entry[i].en_offset = (char *)p_data - (char *)struct_start; 18587c478bd9Sstevel@tonic-gate 18597c478bd9Sstevel@tonic-gate for (j = 0; j < p_entry[i].en_count_types; j++) { 18607c478bd9Sstevel@tonic-gate if (p_entry[i].en_type_def[j].dd_datatype == ADT_MSG) 18617c478bd9Sstevel@tonic-gate this_size = sizeof (enum adt_generic); 18627c478bd9Sstevel@tonic-gate else 18637c478bd9Sstevel@tonic-gate this_size = 18647c478bd9Sstevel@tonic-gate p_entry[i].en_type_def[j].dd_input_size; 18657c478bd9Sstevel@tonic-gate 18667c478bd9Sstevel@tonic-gate /* adj for first entry */ 18677c478bd9Sstevel@tonic-gate if (prev_size == 0) 18687c478bd9Sstevel@tonic-gate prev_size = this_size; 18697c478bd9Sstevel@tonic-gate 18707c478bd9Sstevel@tonic-gate if (p_entry[i].en_type_def[j].dd_datatype == 18717c478bd9Sstevel@tonic-gate ADT_UINT32ARRAY) { 18727c478bd9Sstevel@tonic-gate p_data = (char *)adt_adjust_address(p_data, 18737c478bd9Sstevel@tonic-gate prev_size, sizeof (uint32_t)) + 18747c478bd9Sstevel@tonic-gate this_size - sizeof (uint32_t); 18757c478bd9Sstevel@tonic-gate 18767c478bd9Sstevel@tonic-gate prev_size = sizeof (uint32_t); 18777c478bd9Sstevel@tonic-gate } else { 18787c478bd9Sstevel@tonic-gate p_data = adt_adjust_address(p_data, prev_size, 18797c478bd9Sstevel@tonic-gate this_size); 18807c478bd9Sstevel@tonic-gate prev_size = this_size; 18817c478bd9Sstevel@tonic-gate } 18827c478bd9Sstevel@tonic-gate } 18837c478bd9Sstevel@tonic-gate } 18847c478bd9Sstevel@tonic-gate } 18857c478bd9Sstevel@tonic-gate 18867c478bd9Sstevel@tonic-gate /* 18877c478bd9Sstevel@tonic-gate * adt_generate_event 18887c478bd9Sstevel@tonic-gate * generate event record from external struct. The order is based on 18897c478bd9Sstevel@tonic-gate * the output tokens, allowing for the possibility that the input data 18907c478bd9Sstevel@tonic-gate * is in a different order. 18917c478bd9Sstevel@tonic-gate * 18927c478bd9Sstevel@tonic-gate */ 18937c478bd9Sstevel@tonic-gate 18947c478bd9Sstevel@tonic-gate static void 18957c478bd9Sstevel@tonic-gate adt_generate_event(const adt_event_data_t *p_extdata, 18967c478bd9Sstevel@tonic-gate struct adt_event_state *p_event, 18977c478bd9Sstevel@tonic-gate struct translation *p_xlate) 18987c478bd9Sstevel@tonic-gate { 18997c478bd9Sstevel@tonic-gate struct entry *p_entry; 19007c478bd9Sstevel@tonic-gate static mutex_t lock = DEFAULTMUTEX; 19017c478bd9Sstevel@tonic-gate 19027c478bd9Sstevel@tonic-gate p_entry = p_xlate->tx_first_entry; 19037c478bd9Sstevel@tonic-gate assert(p_entry != NULL); 19047c478bd9Sstevel@tonic-gate 19057c478bd9Sstevel@tonic-gate p_event->ae_internal_id = p_xlate->tx_internal_event; 19067c478bd9Sstevel@tonic-gate adt_token_open(p_event); 19077c478bd9Sstevel@tonic-gate 19087c478bd9Sstevel@tonic-gate /* 19097c478bd9Sstevel@tonic-gate * offsets are not pre-calculated; the initial offsets are all 19107c478bd9Sstevel@tonic-gate * 0; valid offsets are >= 0. Offsets for no-input tokens such 19117c478bd9Sstevel@tonic-gate * as subject are set to -1 by adt_calcOffset() 19127c478bd9Sstevel@tonic-gate */ 19137c478bd9Sstevel@tonic-gate if (p_xlate->tx_offsetsCalculated == 0) { 1914*7257d1b4Sraf (void) mutex_lock(&lock); 19157c478bd9Sstevel@tonic-gate p_xlate->tx_offsetsCalculated = 1; 19167c478bd9Sstevel@tonic-gate 19177c478bd9Sstevel@tonic-gate adt_calcOffsets(p_xlate->tx_top_entry, p_xlate->tx_entries, 19187c478bd9Sstevel@tonic-gate (void *)p_extdata); 1919*7257d1b4Sraf (void) mutex_unlock(&lock); 19207c478bd9Sstevel@tonic-gate } 19217c478bd9Sstevel@tonic-gate while (p_entry != NULL) { 19227c478bd9Sstevel@tonic-gate adt_generate_token(p_entry, (char *)p_extdata, 19237c478bd9Sstevel@tonic-gate p_event); 19247c478bd9Sstevel@tonic-gate 19257c478bd9Sstevel@tonic-gate p_entry = p_entry->en_next_token; 19267c478bd9Sstevel@tonic-gate } 19277c478bd9Sstevel@tonic-gate adt_token_close(p_event); 19287c478bd9Sstevel@tonic-gate } 19297c478bd9Sstevel@tonic-gate 19307c478bd9Sstevel@tonic-gate /* 19317c478bd9Sstevel@tonic-gate * adt_put_event -- main event generation function. 19327c478bd9Sstevel@tonic-gate * The input "event" is the address of the struct containing 19337c478bd9Sstevel@tonic-gate * event-specific data. 19347c478bd9Sstevel@tonic-gate * 19357c478bd9Sstevel@tonic-gate * However if auditing is off or the session handle 19367c478bd9Sstevel@tonic-gate * is NULL, no attempt to write a record is made. 19377c478bd9Sstevel@tonic-gate */ 19387c478bd9Sstevel@tonic-gate 19397c478bd9Sstevel@tonic-gate int 194026fba2a6Sgww adt_put_event(const adt_event_data_t *event, int status, int return_val) 19417c478bd9Sstevel@tonic-gate { 19427c478bd9Sstevel@tonic-gate struct adt_event_state *event_state; 19437c478bd9Sstevel@tonic-gate struct translation *xlate; 19447c478bd9Sstevel@tonic-gate int rc = 0; 19457c478bd9Sstevel@tonic-gate 19467c478bd9Sstevel@tonic-gate if (event == NULL) { 19477c478bd9Sstevel@tonic-gate errno = EINVAL; 19487c478bd9Sstevel@tonic-gate rc = -1; 19497c478bd9Sstevel@tonic-gate goto return_rc; 19507c478bd9Sstevel@tonic-gate } 19517c478bd9Sstevel@tonic-gate event_state = (struct adt_event_state *)event; 19527c478bd9Sstevel@tonic-gate 19537c478bd9Sstevel@tonic-gate /* if audit off or this is a broken session, exit */ 19547c478bd9Sstevel@tonic-gate if (auditstate == AUC_DISABLED || (event_state->ae_session == NULL)) 19557c478bd9Sstevel@tonic-gate goto return_rc; 19567c478bd9Sstevel@tonic-gate 19577c478bd9Sstevel@tonic-gate assert(event_state->ae_check == ADT_VALID); 19587c478bd9Sstevel@tonic-gate 19597c478bd9Sstevel@tonic-gate event_state->ae_rc = status; 19607c478bd9Sstevel@tonic-gate event_state->ae_type = return_val; 19617c478bd9Sstevel@tonic-gate 19627c478bd9Sstevel@tonic-gate /* look up the event */ 19637c478bd9Sstevel@tonic-gate 19647c478bd9Sstevel@tonic-gate xlate = adt_getXlateTable(event_state->ae_event_id); 19657c478bd9Sstevel@tonic-gate 19667c478bd9Sstevel@tonic-gate if (xlate == NULL) { 19677c478bd9Sstevel@tonic-gate errno = EINVAL; 19687c478bd9Sstevel@tonic-gate rc = -1; 19697c478bd9Sstevel@tonic-gate goto return_rc; 19707c478bd9Sstevel@tonic-gate } 19717c478bd9Sstevel@tonic-gate DPRINTF(("got event %d\n", xlate->tx_internal_event)); 19727c478bd9Sstevel@tonic-gate 19737c478bd9Sstevel@tonic-gate if (adt_selected(event_state, xlate->tx_internal_event, status)) 19747c478bd9Sstevel@tonic-gate adt_generate_event(event, event_state, xlate); 19757c478bd9Sstevel@tonic-gate 19767c478bd9Sstevel@tonic-gate return_rc: 19777c478bd9Sstevel@tonic-gate return (rc); 19787c478bd9Sstevel@tonic-gate } 19797c478bd9Sstevel@tonic-gate 19807c478bd9Sstevel@tonic-gate /* 19817c478bd9Sstevel@tonic-gate * adt_free_event -- invalidate and free 19827c478bd9Sstevel@tonic-gate */ 19837c478bd9Sstevel@tonic-gate 19847c478bd9Sstevel@tonic-gate void 19857c478bd9Sstevel@tonic-gate adt_free_event(adt_event_data_t *event) 19867c478bd9Sstevel@tonic-gate { 19877c478bd9Sstevel@tonic-gate struct adt_event_state *event_state; 19887c478bd9Sstevel@tonic-gate 19897c478bd9Sstevel@tonic-gate if (event == NULL) 19907c478bd9Sstevel@tonic-gate return; 19917c478bd9Sstevel@tonic-gate 19927c478bd9Sstevel@tonic-gate event_state = (struct adt_event_state *)event; 19937c478bd9Sstevel@tonic-gate 19947c478bd9Sstevel@tonic-gate assert(event_state->ae_check == ADT_VALID); 19957c478bd9Sstevel@tonic-gate 19967c478bd9Sstevel@tonic-gate event_state->ae_check = 0; 19977c478bd9Sstevel@tonic-gate 19987c478bd9Sstevel@tonic-gate free(event_state); 19997c478bd9Sstevel@tonic-gate } 20007c478bd9Sstevel@tonic-gate 20017c478bd9Sstevel@tonic-gate /* 20027c478bd9Sstevel@tonic-gate * adt_is_selected -- helper to adt_selected(), below. 20037c478bd9Sstevel@tonic-gate * 20047c478bd9Sstevel@tonic-gate * "sorf" is "success or fail" status; au_preselect compares 20057c478bd9Sstevel@tonic-gate * that with success, fail, or both. 20067c478bd9Sstevel@tonic-gate */ 20077c478bd9Sstevel@tonic-gate 20087c478bd9Sstevel@tonic-gate static int 20097c478bd9Sstevel@tonic-gate adt_is_selected(au_event_t e, au_mask_t *m, int sorf) 20107c478bd9Sstevel@tonic-gate { 20117c478bd9Sstevel@tonic-gate int prs_sorf; 20127c478bd9Sstevel@tonic-gate 20137c478bd9Sstevel@tonic-gate if (sorf == 0) 20147c478bd9Sstevel@tonic-gate prs_sorf = AU_PRS_SUCCESS; 20157c478bd9Sstevel@tonic-gate else 20167c478bd9Sstevel@tonic-gate prs_sorf = AU_PRS_FAILURE; 20177c478bd9Sstevel@tonic-gate 20187c478bd9Sstevel@tonic-gate return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD)); 20197c478bd9Sstevel@tonic-gate } 20207c478bd9Sstevel@tonic-gate 20217c478bd9Sstevel@tonic-gate /* 20227c478bd9Sstevel@tonic-gate * selected -- see if this event is preselected. 20237c478bd9Sstevel@tonic-gate * 20247c478bd9Sstevel@tonic-gate * if errors are encountered trying to check a preselection mask 20257c478bd9Sstevel@tonic-gate * or look up a user name, the event is selected. Otherwise, the 20267c478bd9Sstevel@tonic-gate * preselection mask is used for the job. 20277c478bd9Sstevel@tonic-gate */ 20287c478bd9Sstevel@tonic-gate 20297c478bd9Sstevel@tonic-gate static int 20307c478bd9Sstevel@tonic-gate adt_selected(struct adt_event_state *event, au_event_t actual_id, int status) 20317c478bd9Sstevel@tonic-gate { 20327c478bd9Sstevel@tonic-gate adt_internal_state_t *sp; 20337c478bd9Sstevel@tonic-gate au_mask_t namask; 20347c478bd9Sstevel@tonic-gate 20357c478bd9Sstevel@tonic-gate sp = event->ae_session; 20367c478bd9Sstevel@tonic-gate 20377c478bd9Sstevel@tonic-gate if ((sp->as_have_user_data & ADT_HAVE_IDS) == 0) { 20387c478bd9Sstevel@tonic-gate adt_write_syslog("No user data available", EINVAL); 20397c478bd9Sstevel@tonic-gate return (1); /* default is "selected" */ 20407c478bd9Sstevel@tonic-gate } 20417c478bd9Sstevel@tonic-gate 20427c478bd9Sstevel@tonic-gate /* non-attributable? */ 20437c478bd9Sstevel@tonic-gate if ((sp->as_info.ai_auid == AU_NOAUDITID) || 20447c478bd9Sstevel@tonic-gate (sp->as_info.ai_auid == ADT_NO_AUDIT)) { 20457c478bd9Sstevel@tonic-gate if (auditon(A_GETKMASK, (caddr_t)&namask, 20467c478bd9Sstevel@tonic-gate sizeof (namask)) != 0) { 20477c478bd9Sstevel@tonic-gate adt_write_syslog("auditon failure", errno); 20487c478bd9Sstevel@tonic-gate return (1); 20497c478bd9Sstevel@tonic-gate } 20507c478bd9Sstevel@tonic-gate return (adt_is_selected(actual_id, &namask, status)); 20517c478bd9Sstevel@tonic-gate } else { 20527c478bd9Sstevel@tonic-gate return (adt_is_selected(actual_id, &(sp->as_info.ai_mask), 20537c478bd9Sstevel@tonic-gate status)); 20547c478bd9Sstevel@tonic-gate } 20557c478bd9Sstevel@tonic-gate } 2056