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
5c1f59b3eSpaulson  * Common Development and Distribution License (the "License").
6c1f59b3eSpaulson  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*d0fa49b7STony Nguyen  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * convert binary audit records to syslog messages and
267c478bd9Sstevel@tonic-gate  * send them off to syslog
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * auditd_plugin_open(), auditd_plugin() and auditd_plugin_close()
327c478bd9Sstevel@tonic-gate  * implement a replacable library for use by auditd; they are a
337c478bd9Sstevel@tonic-gate  * project private interface and may change without notice.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate #define	DEBUG	0
377c478bd9Sstevel@tonic-gate #if DEBUG
387c478bd9Sstevel@tonic-gate #define	DPRINT(x) {fprintf x; }
397c478bd9Sstevel@tonic-gate #else
407c478bd9Sstevel@tonic-gate #define	DPRINT(x)
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
447c478bd9Sstevel@tonic-gate #include <assert.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <grp.h>
487c478bd9Sstevel@tonic-gate #include <libintl.h>
497c478bd9Sstevel@tonic-gate #include <netdb.h>
507c478bd9Sstevel@tonic-gate #include <netinet/in.h>
517c478bd9Sstevel@tonic-gate #include <pthread.h>
527c478bd9Sstevel@tonic-gate #include <pwd.h>
537c478bd9Sstevel@tonic-gate #include <stdio.h>
547c478bd9Sstevel@tonic-gate #include <stdlib.h>
557c478bd9Sstevel@tonic-gate #include <string.h>
567c478bd9Sstevel@tonic-gate #include <time.h>
577c478bd9Sstevel@tonic-gate #include <syslog.h>
587c478bd9Sstevel@tonic-gate #include <sys/types.h>
597c478bd9Sstevel@tonic-gate #include <sys/socket.h>
607c478bd9Sstevel@tonic-gate #include <unistd.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
637c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
647c478bd9Sstevel@tonic-gate #include <security/auditd.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include "toktable.h"
677c478bd9Sstevel@tonic-gate #include "sysplugin.h"
687c478bd9Sstevel@tonic-gate #include "systoken.h"
697c478bd9Sstevel@tonic-gate #include <audit_plugin.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #if DEBUG
727c478bd9Sstevel@tonic-gate static FILE	*dbfp;			/* debug file */
737c478bd9Sstevel@tonic-gate #endif
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate extern void init_tokens();
767c478bd9Sstevel@tonic-gate extern int parse_token(parse_context_t *);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static au_mask_t	mask;
797c478bd9Sstevel@tonic-gate static int		initialized = 0;
807c478bd9Sstevel@tonic-gate static size_t		maxavail;
817c478bd9Sstevel@tonic-gate static pthread_mutex_t	log_mutex;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	ELLIPSIS	"..."
847c478bd9Sstevel@tonic-gate #define	ELLIPSIS_SIZE	(sizeof (ELLIPSIS) - 1)
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * simple hashing for uid and hostname lookup
887c478bd9Sstevel@tonic-gate  *
897c478bd9Sstevel@tonic-gate  * performance tests showed that cacheing the hostname, uid, and gid
907c478bd9Sstevel@tonic-gate  * make about a 40% difference for short audit records and regularly
917c478bd9Sstevel@tonic-gate  * repeating hostname, uid, etc
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  * ht_type and ht_ip are only used for hostname lookup cacheing.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate typedef struct hashtable {
967c478bd9Sstevel@tonic-gate 	uint32_t	ht_key;
977c478bd9Sstevel@tonic-gate 	uint32_t	ht_type;
987c478bd9Sstevel@tonic-gate 	uint32_t	ht_ip[4];
997c478bd9Sstevel@tonic-gate 	char		*ht_value;
1007c478bd9Sstevel@tonic-gate 	size_t		ht_length;
1017c478bd9Sstevel@tonic-gate } hashtable_t;
1027c478bd9Sstevel@tonic-gate #define	HOSTHASHSIZE	128
1037c478bd9Sstevel@tonic-gate #define	UIDHASHSIZE	128
1047c478bd9Sstevel@tonic-gate #define	GIDHASHSIZE	32
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static hashtable_t	uidhash[UIDHASHSIZE];
1077c478bd9Sstevel@tonic-gate static hashtable_t	gidhash[GIDHASHSIZE];
1087c478bd9Sstevel@tonic-gate static hashtable_t	hosthash[HOSTHASHSIZE];
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	STRCONSTARGS(s)	(s), (sizeof (s) - 1)
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * the hash "handles" collisions by overwriting the old
1137c478bd9Sstevel@tonic-gate  * hash entry with the new.  Perfection is not the goal.
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  * the key (s) is a 32 bit integer, handled here as
1167c478bd9Sstevel@tonic-gate  * four bytes.  If the hash size is increased beyond
1177c478bd9Sstevel@tonic-gate  * 256, this macro will need some work.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate #define	HASH(s,  r, m)	{\
1207c478bd9Sstevel@tonic-gate 				uint32_t	_mush = 0;\
1217c478bd9Sstevel@tonic-gate 				int		_i;\
1227c478bd9Sstevel@tonic-gate 				for (_i = 0; _i < 4; _i++) {\
1237c478bd9Sstevel@tonic-gate 					_mush ^= *(s)++;\
1247c478bd9Sstevel@tonic-gate 				}\
1257c478bd9Sstevel@tonic-gate 				r = _mush % m;\
1267c478bd9Sstevel@tonic-gate 			}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * The default mask for sysplugin is to reject all record types.
1317c478bd9Sstevel@tonic-gate  * The parameters input here select which classes to allow.
1327c478bd9Sstevel@tonic-gate  *
1337c478bd9Sstevel@tonic-gate  * getauditflgsbin() outputs error messages to syslog.
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  * caller must hold log_mutex
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static auditd_rc_t
1397c478bd9Sstevel@tonic-gate setmask(const char *flags)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	au_mask_t tmask;
1427c478bd9Sstevel@tonic-gate 	char	*input, *ip, c;
1437c478bd9Sstevel@tonic-gate 	auditd_rc_t	rc = AUDITD_SUCCESS;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	mask.am_success = 0x0;
1467c478bd9Sstevel@tonic-gate 	mask.am_failure = 0x0;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (flags != NULL) {
1497c478bd9Sstevel@tonic-gate 		/*
1507c478bd9Sstevel@tonic-gate 		 * getauditflagsbin doesn't like blanks, but admins do
1517c478bd9Sstevel@tonic-gate 		 */
1527c478bd9Sstevel@tonic-gate 		input = malloc(strlen(flags) + 1);
1537c478bd9Sstevel@tonic-gate 		if (input == NULL)
1547c478bd9Sstevel@tonic-gate 			return (AUDITD_NO_MEMORY);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		ip = input;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		for (; (c = *flags) != '\0'; flags++) {
1597c478bd9Sstevel@tonic-gate 			if (c == ' ')
1607c478bd9Sstevel@tonic-gate 				continue;
1617c478bd9Sstevel@tonic-gate 			*ip++ = c;
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 		*ip = '\0';
1647c478bd9Sstevel@tonic-gate 		if (getauditflagsbin(input, &tmask) == 0) {
1657c478bd9Sstevel@tonic-gate 			mask.am_success |= tmask.am_success;
1667c478bd9Sstevel@tonic-gate 			mask.am_failure |= tmask.am_failure;
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	if ((mask.am_success | mask.am_failure) == 0) {
1707c478bd9Sstevel@tonic-gate 		rc = AUDITD_INVALID;
1717c478bd9Sstevel@tonic-gate 		__audit_syslog("audit_syslog.so", LOG_CONS | LOG_NDELAY,
1727c478bd9Sstevel@tonic-gate 		    LOG_DAEMON, LOG_ERR,
1737c478bd9Sstevel@tonic-gate 		    gettext("plugin is configured with empty class mask\n"));
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	free(input);
1767c478bd9Sstevel@tonic-gate 	return (rc);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * based on the current value of mask, either keep or toss the
1817c478bd9Sstevel@tonic-gate  * current audit record.  The input is 1 for success, -1 for
1827c478bd9Sstevel@tonic-gate  * failure.  0 means no exit or return token was seen.
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  * au_preselect returns 1 for keep it, 0 for delete it, and
1857c478bd9Sstevel@tonic-gate  * -1 for some sort of error.  Here, 1 and -1 are considered
1867c478bd9Sstevel@tonic-gate  * equivalent.  tossit() returns 1 for delete it and 0 for
1877c478bd9Sstevel@tonic-gate  * keep it.
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate static int
1917c478bd9Sstevel@tonic-gate tossit(au_event_t id, int passfail)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	int	rc;
1947c478bd9Sstevel@tonic-gate 	int	selFlag;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	switch (passfail) {
1977c478bd9Sstevel@tonic-gate 	case 1:
1987c478bd9Sstevel@tonic-gate 		selFlag = AU_PRS_SUCCESS;
1997c478bd9Sstevel@tonic-gate 		break;
2007c478bd9Sstevel@tonic-gate 	case -1:
2017c478bd9Sstevel@tonic-gate 		selFlag = AU_PRS_FAILURE;
2027c478bd9Sstevel@tonic-gate 		break;
2037c478bd9Sstevel@tonic-gate 	default:		/* no exit or return token */
2047c478bd9Sstevel@tonic-gate 		selFlag = AU_PRS_BOTH;
2057c478bd9Sstevel@tonic-gate 		break;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&log_mutex);
2087c478bd9Sstevel@tonic-gate 	rc = au_preselect(id, &mask, selFlag, AU_PRS_USECACHE);
2097c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&log_mutex);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	return (rc == 0);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * the three bytes for ellipsis could potentially be longer than the
2167c478bd9Sstevel@tonic-gate  * space available for text if maxavail is within two bytes of
2177c478bd9Sstevel@tonic-gate  * OUTPUT_BUF_SIZE, which can happen if the hostname is one or two
2187c478bd9Sstevel@tonic-gate  * characters long.  If there isn't room for ellipsis, there isn't
2197c478bd9Sstevel@tonic-gate  * room for the data, so it is simply dropped.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static size_t
2237c478bd9Sstevel@tonic-gate fromleft(char *p, size_t avail, char *attrname, size_t attrlen, char *txt,
2247c478bd9Sstevel@tonic-gate 	size_t txtlen)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	size_t	len;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (avail < attrlen + ELLIPSIS_SIZE)
2297c478bd9Sstevel@tonic-gate 		return (0);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	(void) memcpy(p, attrname, attrlen);
2327c478bd9Sstevel@tonic-gate 	p += attrlen;
2337c478bd9Sstevel@tonic-gate 	avail -= attrlen;
2347c478bd9Sstevel@tonic-gate 	if (txtlen > avail) {
2357c478bd9Sstevel@tonic-gate 		(void) memcpy(p, ELLIPSIS, ELLIPSIS_SIZE);
2367c478bd9Sstevel@tonic-gate 		txt += txtlen - (avail - ELLIPSIS_SIZE);
2377c478bd9Sstevel@tonic-gate 		(void) memcpy(p + ELLIPSIS_SIZE, txt, avail - ELLIPSIS_SIZE);
2387c478bd9Sstevel@tonic-gate 		len = attrlen + avail;
2397c478bd9Sstevel@tonic-gate 		p += avail;
2407c478bd9Sstevel@tonic-gate 	} else {
2417c478bd9Sstevel@tonic-gate 		(void) memcpy(p, txt, txtlen);
2427c478bd9Sstevel@tonic-gate 		len = attrlen + txtlen;
2437c478bd9Sstevel@tonic-gate 		p += txtlen;
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	*p = '\0';
2467c478bd9Sstevel@tonic-gate 	return (len);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static size_t
2507c478bd9Sstevel@tonic-gate fromright(char *p, size_t avail, char *attrname, size_t attrlen, char *txt,
2517c478bd9Sstevel@tonic-gate 	size_t txtlen)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	size_t	len;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (avail < attrlen + ELLIPSIS_SIZE)
2567c478bd9Sstevel@tonic-gate 		return (0);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	(void) memcpy(p, attrname, attrlen);
2597c478bd9Sstevel@tonic-gate 	p += attrlen;
2607c478bd9Sstevel@tonic-gate 	avail -= attrlen;
2617c478bd9Sstevel@tonic-gate 	if (txtlen > avail) {
2627c478bd9Sstevel@tonic-gate 		(void) memcpy(p, txt, avail - ELLIPSIS_SIZE);
2637c478bd9Sstevel@tonic-gate 		(void) memcpy(p + (avail - ELLIPSIS_SIZE),
2647c478bd9Sstevel@tonic-gate 		    ELLIPSIS, ELLIPSIS_SIZE);
2657c478bd9Sstevel@tonic-gate 		len = attrlen + avail;
2667c478bd9Sstevel@tonic-gate 		p += avail;
2677c478bd9Sstevel@tonic-gate 	} else {
2687c478bd9Sstevel@tonic-gate 		(void) memcpy(p, txt, txtlen);
2697c478bd9Sstevel@tonic-gate 		p += txtlen;
2707c478bd9Sstevel@tonic-gate 		len = attrlen + txtlen;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	*p = '\0';
2737c478bd9Sstevel@tonic-gate 	return (len);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate static int
2777c478bd9Sstevel@tonic-gate init_hash(hashtable_t *table, int bad_key, int table_length,
2787c478bd9Sstevel@tonic-gate     size_t max_value)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	int	i;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	for (i = 0; i < table_length; i++) {
2837c478bd9Sstevel@tonic-gate 		table[i].ht_value = malloc(max_value + 1);
2847c478bd9Sstevel@tonic-gate 		table[i].ht_key = bad_key;
2857c478bd9Sstevel@tonic-gate 		table[i].ht_length = 0;
2867c478bd9Sstevel@tonic-gate 		if (table[i].ht_value == NULL) {
2877c478bd9Sstevel@tonic-gate 			int	j;
2887c478bd9Sstevel@tonic-gate 			for (j = 0; j < i; j++)
2897c478bd9Sstevel@tonic-gate 				free(table[j].ht_value);
2907c478bd9Sstevel@tonic-gate 			return (-1);
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 		*(table[i].ht_value) = '\0';
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	return (0);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static void
2987c478bd9Sstevel@tonic-gate free_hash(hashtable_t *table, int table_length)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	int	i;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	for (i = 0; i < table_length; i++) {
3037c478bd9Sstevel@tonic-gate 		free(table[i].ht_value);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * do IP -> hostname lookup
3107c478bd9Sstevel@tonic-gate  */
3117c478bd9Sstevel@tonic-gate #define	UNKNOWN		"unknown"
3127c478bd9Sstevel@tonic-gate #define	UNKNOWN_LEN	(sizeof (UNKNOWN))
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate static size_t
3157c478bd9Sstevel@tonic-gate gethname(au_tid_addr_t *tid, char *p, size_t max, char *prefix,
3167c478bd9Sstevel@tonic-gate     size_t prefix_len)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	size_t			len, l;
3197c478bd9Sstevel@tonic-gate 	struct hostent		*host;
3207c478bd9Sstevel@tonic-gate 	int			rc;
3217c478bd9Sstevel@tonic-gate 	int			af;
3227c478bd9Sstevel@tonic-gate 	int			ix;
3237c478bd9Sstevel@tonic-gate 	char			*hash_key;
3247c478bd9Sstevel@tonic-gate 	uint32_t		key;
3257c478bd9Sstevel@tonic-gate 	int			match;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if (prefix_len > max)
3287c478bd9Sstevel@tonic-gate 		return (0);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	(void) memcpy(p, prefix, prefix_len);
3317c478bd9Sstevel@tonic-gate 	p += prefix_len;
3327c478bd9Sstevel@tonic-gate 	max -= prefix_len;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (tid->at_type == AU_IPv6) {
3357c478bd9Sstevel@tonic-gate 		key = tid->at_addr[0] ^
3367c478bd9Sstevel@tonic-gate 			tid->at_addr[1] ^
3377c478bd9Sstevel@tonic-gate 			tid->at_addr[2] ^
3387c478bd9Sstevel@tonic-gate 			tid->at_addr[3];
3397c478bd9Sstevel@tonic-gate 	} else
3407c478bd9Sstevel@tonic-gate 		key = (tid->at_addr[0]);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	hash_key = (char *)&key;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	HASH(hash_key, ix, HOSTHASHSIZE);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	match = 0;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if (key == 0) {
3497c478bd9Sstevel@tonic-gate 		l = UNKNOWN_LEN;	/* includes end of string */
3507c478bd9Sstevel@tonic-gate 		if (l > max)
3517c478bd9Sstevel@tonic-gate 			l = max;
3527c478bd9Sstevel@tonic-gate 		len = prefix_len + strlcpy(p, UNKNOWN, l);
3537c478bd9Sstevel@tonic-gate 		return (len);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (tid->at_type == AU_IPv6) {
3577c478bd9Sstevel@tonic-gate 		if ((key == hosthash[ix].ht_key) &&
3587c478bd9Sstevel@tonic-gate 		    (hosthash[ix].ht_type == tid->at_type)) {
3597c478bd9Sstevel@tonic-gate 			int i;
3607c478bd9Sstevel@tonic-gate 			match = 1;
3617c478bd9Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
3627c478bd9Sstevel@tonic-gate 				if (hosthash[ix].ht_ip[i] != tid->at_addr[i]) {
3637c478bd9Sstevel@tonic-gate 					match = 0;
3647c478bd9Sstevel@tonic-gate 					break;
3657c478bd9Sstevel@tonic-gate 				}
3667c478bd9Sstevel@tonic-gate 			}
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 	} else if (key == hosthash[ix].ht_key) {
3697c478bd9Sstevel@tonic-gate 		match = 1;
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 	if (!match) {
3727c478bd9Sstevel@tonic-gate 		hosthash[ix].ht_key = key;
3737c478bd9Sstevel@tonic-gate 		hosthash[ix].ht_type = tid->at_type;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		if (tid->at_type == AU_IPv4) {
3767c478bd9Sstevel@tonic-gate 			hosthash[ix].ht_ip[0] = tid->at_addr[0];
3777c478bd9Sstevel@tonic-gate 			af = AF_INET;
3787c478bd9Sstevel@tonic-gate 		} else {
3797c478bd9Sstevel@tonic-gate 			(void) memcpy((char *)hosthash[ix].ht_ip,
3807c478bd9Sstevel@tonic-gate 			    (char *)tid->at_addr, AU_IPv6);
3817c478bd9Sstevel@tonic-gate 			af = AF_INET6;
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 		host = getipnodebyaddr((const void *)tid->at_addr,
3847c478bd9Sstevel@tonic-gate 		    tid->at_type, af, &rc);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 		if (host == NULL) {
3877c478bd9Sstevel@tonic-gate 			(void) inet_ntop(af, (void *)tid->at_addr,
3887c478bd9Sstevel@tonic-gate 			    hosthash[ix].ht_value, MAXHOSTNAMELEN);
3897c478bd9Sstevel@tonic-gate 			hosthash[ix].ht_length = strlen(hosthash[ix].ht_value);
3907c478bd9Sstevel@tonic-gate 		} else {
3917c478bd9Sstevel@tonic-gate 			hosthash[ix].ht_length = strlcpy(hosthash[ix].ht_value,
3927c478bd9Sstevel@tonic-gate 			    host->h_name,  MAXHOSTNAMELEN);
3937c478bd9Sstevel@tonic-gate 			freehostent(host);
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	l = hosthash[ix].ht_length + 1;
3977c478bd9Sstevel@tonic-gate 	if (l > max)
3987c478bd9Sstevel@tonic-gate 		l = max;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	len = prefix_len + strlcpy(p, hosthash[ix].ht_value, l);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	return (len);
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate  * the appropriate buffer length for getpwuid_r() isn't documented;
4067c478bd9Sstevel@tonic-gate  * 1024 should be enough.
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate #define	GETPWUID_BUFF_LEN	1024
4097c478bd9Sstevel@tonic-gate #define	USERNAMELEN		256
4107c478bd9Sstevel@tonic-gate #define	GIDNAMELEN		256
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate static size_t
4137c478bd9Sstevel@tonic-gate getuname(uid_t uid, gid_t gid, char *p, size_t max, char *prefix,
4147c478bd9Sstevel@tonic-gate     size_t prefix_len)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	struct passwd		pw;
4177c478bd9Sstevel@tonic-gate 	char			pw_buf[GETPWUID_BUFF_LEN];
4187c478bd9Sstevel@tonic-gate 	size_t			len, l;
4197c478bd9Sstevel@tonic-gate 	struct group		gr;
4207c478bd9Sstevel@tonic-gate 	int			ix;
4217c478bd9Sstevel@tonic-gate 	char			*hash_key;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (prefix_len > max)
4247c478bd9Sstevel@tonic-gate 		return (0);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	len = prefix_len;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	(void) memcpy(p, prefix, len);
4297c478bd9Sstevel@tonic-gate 	p += len;
4307c478bd9Sstevel@tonic-gate 	max -= len;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	hash_key = (char *)&uid;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	HASH(hash_key, ix, UIDHASHSIZE);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (uid != uidhash[ix].ht_key) {
4377c478bd9Sstevel@tonic-gate 		uidhash[ix].ht_key = uid;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		if ((getpwuid_r(uid, &pw, pw_buf, GETPWUID_BUFF_LEN)) == NULL)
4407c478bd9Sstevel@tonic-gate 			l = snprintf(uidhash[ix].ht_value, USERNAMELEN,
4417c478bd9Sstevel@tonic-gate 			    "%d", uid);
4427c478bd9Sstevel@tonic-gate 		else
4437c478bd9Sstevel@tonic-gate 			l = strlcpy(uidhash[ix].ht_value, pw.pw_name,
4447c478bd9Sstevel@tonic-gate 			    USERNAMELEN);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		uidhash[ix].ht_length = l;
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	l = uidhash[ix].ht_length + 1;
4497c478bd9Sstevel@tonic-gate 	if (l > max)
4507c478bd9Sstevel@tonic-gate 		l = max;
4517c478bd9Sstevel@tonic-gate 	(void) memcpy(p, uidhash[ix].ht_value, l);
4527c478bd9Sstevel@tonic-gate 	len += l - 1;
4537c478bd9Sstevel@tonic-gate 
454f48205beScasper 	if (gid != (gid_t)-2) {
4557c478bd9Sstevel@tonic-gate 		p += l - 1;
4567c478bd9Sstevel@tonic-gate 		max -= l - 1;
4577c478bd9Sstevel@tonic-gate 		if (max < 2)
4587c478bd9Sstevel@tonic-gate 			return (len);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		hash_key = (char *)&gid;
4617c478bd9Sstevel@tonic-gate 		HASH(hash_key, ix, GIDHASHSIZE);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		if (gid != gidhash[ix].ht_key) {
4647c478bd9Sstevel@tonic-gate 			gidhash[ix].ht_key = gid;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 			if (getgrgid_r(gid, &gr,  pw_buf, GETPWUID_BUFF_LEN) ==
4677c478bd9Sstevel@tonic-gate 			    NULL)
4687c478bd9Sstevel@tonic-gate 				gidhash[ix].ht_length =
4697c478bd9Sstevel@tonic-gate 				    snprintf(gidhash[ix].ht_value, GIDNAMELEN,
4707c478bd9Sstevel@tonic-gate 					"%d", gid);
4717c478bd9Sstevel@tonic-gate 			else
4727c478bd9Sstevel@tonic-gate 				gidhash[ix].ht_length =
4737c478bd9Sstevel@tonic-gate 				    strlcpy(gidhash[ix].ht_value,
4747c478bd9Sstevel@tonic-gate 				    gr.gr_name, GIDNAMELEN);
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 		*p++ = ':';
4777c478bd9Sstevel@tonic-gate 		len++;
4787c478bd9Sstevel@tonic-gate 		max--;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		l = gidhash[ix].ht_length + 1;
4817c478bd9Sstevel@tonic-gate 		if (l > max)
4827c478bd9Sstevel@tonic-gate 			l = max;
4837c478bd9Sstevel@tonic-gate 		(void) memcpy(p, gidhash[ix].ht_value, l);
4847c478bd9Sstevel@tonic-gate 		len += l - 1;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	return (len);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * filter() parse input; toss if not wanted.
4917c478bd9Sstevel@tonic-gate  *
4927c478bd9Sstevel@tonic-gate  * the input value sequence is a number generated when the buffer
4937c478bd9Sstevel@tonic-gate  * was queued.  ctx.out.sf_sequence, if not -1, is the sequence number
4947c478bd9Sstevel@tonic-gate  * generated in c2audit.  It is not part of the "official" syslog
4957c478bd9Sstevel@tonic-gate  * output but is included if DEBUG is on.
4967c478bd9Sstevel@tonic-gate  */
4977c478bd9Sstevel@tonic-gate #define	EVENT_NAME_LEN	32
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate static auditd_rc_t
5007c478bd9Sstevel@tonic-gate filter(const char *input, uint32_t sequence, char *output,
5017c478bd9Sstevel@tonic-gate     size_t in_len, size_t out_len)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	parse_context_t		ctx;
5047c478bd9Sstevel@tonic-gate 	char			*bp;
5057c478bd9Sstevel@tonic-gate 	auditd_rc_t		rc = AUDITD_SUCCESS;
5067c478bd9Sstevel@tonic-gate 	auditd_rc_t		rc_ret = AUDITD_SUCCESS;
5077c478bd9Sstevel@tonic-gate 	size_t			used, remaining;
5087c478bd9Sstevel@tonic-gate 	char			*last_adr; /* infinite loop check */
5097c478bd9Sstevel@tonic-gate 	int			token_count = 0;
5107c478bd9Sstevel@tonic-gate 	int			parse_rc;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	static parse_context_t	initial_ctx;
5137c478bd9Sstevel@tonic-gate 	static int		first = 1;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	if (first) {
5167c478bd9Sstevel@tonic-gate 		first = 0;
5177c478bd9Sstevel@tonic-gate 
518c1f59b3eSpaulson 		/*
519c1f59b3eSpaulson 		 * Any member or submember of parse_context_t which utilizes
520c1f59b3eSpaulson 		 * allocated memory must free() the memory after calling
521c1f59b3eSpaulson 		 * parse_token() for both the preselected and non-preselected
522c1f59b3eSpaulson 		 * cases.
523c1f59b3eSpaulson 		 * New additions to parse_context_t or its submembers need to
524c1f59b3eSpaulson 		 * have this same treatment.
525c1f59b3eSpaulson 		 */
5267c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_eventid = 0;
5277c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_reclen = 0;
5287c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_pass = 0;
5297c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_asid = 0;
530f48205beScasper 		initial_ctx.out.sf_auid = (uid_t)-2;
531f48205beScasper 		initial_ctx.out.sf_euid = (uid_t)-2;
532f48205beScasper 		initial_ctx.out.sf_egid = (gid_t)-2;
5337c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_tid.at_type = 0;
534f48205beScasper 		initial_ctx.out.sf_pauid = (uid_t)-2;
535f48205beScasper 		initial_ctx.out.sf_peuid = (uid_t)2;
5367c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_uauthlen = 0;
5377c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_uauth = NULL;
5387c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_pathlen = 0;
5397c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_path = NULL;
5407c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_atpathlen = 0;
5417c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_atpath = NULL;
5427c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_textlen = 0;
5437c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_text = NULL;
5447c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_sequence = -1;
5457c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_zonelen = 0;
5467c478bd9Sstevel@tonic-gate 		initial_ctx.out.sf_zonename = NULL;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		init_tokens();		/* cmd/praudit/toktable.c */
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 	(void) memcpy(&ctx, &initial_ctx, sizeof (parse_context_t));
5517c478bd9Sstevel@tonic-gate 	ctx.id = sequence;
5527c478bd9Sstevel@tonic-gate 	ctx.adr.adr_stream = (char *)input;
5537c478bd9Sstevel@tonic-gate 	ctx.adr.adr_now = (char *)input;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	last_adr = NULL;
5567c478bd9Sstevel@tonic-gate 	while ((ctx.adr.adr_now - ctx.adr.adr_stream) < in_len) {
5577c478bd9Sstevel@tonic-gate 		assert(last_adr != ctx.adr.adr_now);
5587c478bd9Sstevel@tonic-gate 		token_count++;
5597c478bd9Sstevel@tonic-gate 		last_adr = ctx.adr.adr_now;
5607c478bd9Sstevel@tonic-gate 		if ((parse_rc = parse_token(&ctx)) != 0) {
5617c478bd9Sstevel@tonic-gate 			char	message[256];
5627c478bd9Sstevel@tonic-gate 			au_event_ent_t	*event;
5637c478bd9Sstevel@tonic-gate 			char	event_name[EVENT_NAME_LEN];
5647c478bd9Sstevel@tonic-gate 			char	sequence_str[EVENT_NAME_LEN];
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			if (cacheauevent(&event, ctx.out.sf_eventid) < 0)
5677c478bd9Sstevel@tonic-gate 				(void) snprintf(event_name, EVENT_NAME_LEN,
568*d0fa49b7STony Nguyen 				    "%hu", ctx.out.sf_eventid);
5697c478bd9Sstevel@tonic-gate 			else
5707c478bd9Sstevel@tonic-gate 				(void) strlcpy(event_name, event->ae_desc,
5717c478bd9Sstevel@tonic-gate 				    EVENT_NAME_LEN);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 			if (token_count < 2)
5747c478bd9Sstevel@tonic-gate 				/* leave rc_ret unchanged */
5757c478bd9Sstevel@tonic-gate 				rc = AUDITD_INVALID;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 			if (ctx.out.sf_sequence != -1)
5787c478bd9Sstevel@tonic-gate 				(void) snprintf(sequence_str, EVENT_NAME_LEN,
5797c478bd9Sstevel@tonic-gate 				    " (seq=%u) ", ctx.out.sf_sequence);
5807c478bd9Sstevel@tonic-gate 			else
5817c478bd9Sstevel@tonic-gate 				sequence_str[0] = '\0';
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 			(void) snprintf(message, 256,
5847c478bd9Sstevel@tonic-gate 			    gettext("error before token %d (previous token=%d)"
5857c478bd9Sstevel@tonic-gate 			    " of record type %s%s\n"),
5867c478bd9Sstevel@tonic-gate 			    token_count, parse_rc, event_name, sequence_str);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, message));
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 			__audit_syslog("audit_syslog.so",
5917c478bd9Sstevel@tonic-gate 			    LOG_PID | LOG_ODELAY | LOG_CONS,
5927c478bd9Sstevel@tonic-gate 			    LOG_DAEMON, LOG_ALERT, message);
5937c478bd9Sstevel@tonic-gate 			break;
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 	if (rc == AUDITD_SUCCESS) {
5977c478bd9Sstevel@tonic-gate 		if (tossit(ctx.out.sf_eventid, ctx.out.sf_pass)) {
5987c478bd9Sstevel@tonic-gate #if DEBUG
5997c478bd9Sstevel@tonic-gate 			if (ctx.out.sf_sequence != -1)
6007c478bd9Sstevel@tonic-gate 				fprintf(dbfp,
601*d0fa49b7STony Nguyen 				    "syslog tossed (event=%hu) record %u "
6027c478bd9Sstevel@tonic-gate 				    "/ buffer %u\n",
6037c478bd9Sstevel@tonic-gate 				    ctx.out.sf_eventid, ctx.out.sf_sequence,
6047c478bd9Sstevel@tonic-gate 				    sequence);
6057c478bd9Sstevel@tonic-gate 			else
6067c478bd9Sstevel@tonic-gate 				fprintf(dbfp,
607*d0fa49b7STony Nguyen 				    "syslog tossed (event=%hu) buffer %u\n",
6087c478bd9Sstevel@tonic-gate 				    ctx.out.sf_eventid, sequence);
6097c478bd9Sstevel@tonic-gate #endif
610c1f59b3eSpaulson 
611c1f59b3eSpaulson 			/*
612c1f59b3eSpaulson 			 * Members or submembers of parse_context_t which
613c1f59b3eSpaulson 			 * utilize allocated memory need to free() the memory
614c1f59b3eSpaulson 			 * here to handle the case of not being preselected as
615c1f59b3eSpaulson 			 * well as below for when the event is preselected.
616c1f59b3eSpaulson 			 * New additions to parse_context_t or any of its
617c1f59b3eSpaulson 			 * submembers need to get the same treatment.
618c1f59b3eSpaulson 			 */
619c1f59b3eSpaulson 			if (ctx.out.sf_uauthlen > 0) {
620c1f59b3eSpaulson 				free(ctx.out.sf_uauth);
621c1f59b3eSpaulson 				ctx.out.sf_uauth = NULL;
622c1f59b3eSpaulson 				ctx.out.sf_uauthlen = 0;
623c1f59b3eSpaulson 			}
624c1f59b3eSpaulson 			if (ctx.out.sf_pathlen > 0) {
625c1f59b3eSpaulson 				free(ctx.out.sf_path);
626c1f59b3eSpaulson 				ctx.out.sf_path = NULL;
627c1f59b3eSpaulson 				ctx.out.sf_pathlen = 0;
628c1f59b3eSpaulson 			}
629c1f59b3eSpaulson 			if (ctx.out.sf_atpathlen > 0) {
630c1f59b3eSpaulson 				free(ctx.out.sf_atpath);
631c1f59b3eSpaulson 				ctx.out.sf_atpath = NULL;
632c1f59b3eSpaulson 				ctx.out.sf_atpathlen = 0;
633c1f59b3eSpaulson 			}
634c1f59b3eSpaulson 			if (ctx.out.sf_textlen > 0) {
635c1f59b3eSpaulson 				free(ctx.out.sf_text);
636c1f59b3eSpaulson 				ctx.out.sf_text = NULL;
637c1f59b3eSpaulson 				ctx.out.sf_textlen = 0;
638c1f59b3eSpaulson 			}
639c1f59b3eSpaulson 			if (ctx.out.sf_zonelen > 0) {
640c1f59b3eSpaulson 				free(ctx.out.sf_zonename);
641c1f59b3eSpaulson 				ctx.out.sf_zonename = NULL;
642c1f59b3eSpaulson 				ctx.out.sf_zonelen = 0;
643c1f59b3eSpaulson 			}
644c1f59b3eSpaulson 
6457c478bd9Sstevel@tonic-gate 			return (-1);	/* tell caller it was tossed */
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 		bp = output;
6487c478bd9Sstevel@tonic-gate 		remaining = out_len;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		if (ctx.out.sf_eventid != 0) {
6517c478bd9Sstevel@tonic-gate 			au_event_ent_t	*event;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 			if (cacheauevent(&event, ctx.out.sf_eventid) < 0)
654*d0fa49b7STony Nguyen 				used = snprintf(bp, remaining, "%hu",
6557c478bd9Sstevel@tonic-gate 				    ctx.out.sf_eventid);
6567c478bd9Sstevel@tonic-gate 			else
6577c478bd9Sstevel@tonic-gate 				used = strlcpy(bp, event->ae_desc, remaining);
6587c478bd9Sstevel@tonic-gate 			bp += used;
6597c478bd9Sstevel@tonic-gate 			remaining -= used;
6607c478bd9Sstevel@tonic-gate 		}
6617c478bd9Sstevel@tonic-gate 		if (ctx.out.sf_pass != 0) {
6627c478bd9Sstevel@tonic-gate 			if (ctx.out.sf_pass < 0)
6637c478bd9Sstevel@tonic-gate 				used = strlcpy(bp, " failed", remaining);
6647c478bd9Sstevel@tonic-gate 			else
6657c478bd9Sstevel@tonic-gate 				used = strlcpy(bp, " ok", remaining);
6667c478bd9Sstevel@tonic-gate 			bp += used;
6677c478bd9Sstevel@tonic-gate 			remaining -= used;
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 		if (ctx.out.sf_asid != 0) {
6707c478bd9Sstevel@tonic-gate 			used = snprintf(bp, remaining, " session %u",
6717c478bd9Sstevel@tonic-gate 			    ctx.out.sf_asid);
6727c478bd9Sstevel@tonic-gate 			remaining -= used;
6737c478bd9Sstevel@tonic-gate 			bp += used;
6747c478bd9Sstevel@tonic-gate 		}
675f48205beScasper 		if (ctx.out.sf_auid != (uid_t)-2) {
6767c478bd9Sstevel@tonic-gate 			used = getuname(ctx.out.sf_auid, -2, bp, remaining,
6777c478bd9Sstevel@tonic-gate 			    STRCONSTARGS(" by "));
6787c478bd9Sstevel@tonic-gate 			bp += used;
6797c478bd9Sstevel@tonic-gate 			remaining -= used;
6807c478bd9Sstevel@tonic-gate 		}
681f48205beScasper 		if (ctx.out.sf_euid != (uid_t)-2) {
6827c478bd9Sstevel@tonic-gate 			/* 4 = strlen(" as ") */
6837c478bd9Sstevel@tonic-gate 			used = getuname(ctx.out.sf_euid, ctx.out.sf_egid, bp,
6847c478bd9Sstevel@tonic-gate 			    remaining, STRCONSTARGS(" as "));
6857c478bd9Sstevel@tonic-gate 			bp += used;
6867c478bd9Sstevel@tonic-gate 			remaining -= used;
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 		if (ctx.out.sf_zonename != NULL) {
6897c478bd9Sstevel@tonic-gate 			used = fromright(bp, remaining,
6907c478bd9Sstevel@tonic-gate 			    STRCONSTARGS(" in "),
6917c478bd9Sstevel@tonic-gate 			    ctx.out.sf_zonename, ctx.out.sf_zonelen);
6927c478bd9Sstevel@tonic-gate 			free(ctx.out.sf_zonename);
6937c478bd9Sstevel@tonic-gate 			bp += used;
6947c478bd9Sstevel@tonic-gate 			remaining -= used;
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 		if (ctx.out.sf_tid.at_type != 0) {
6977c478bd9Sstevel@tonic-gate 			/* 6 = strlen(" from ") */
6987c478bd9Sstevel@tonic-gate 			used = gethname(&(ctx.out.sf_tid), bp, remaining,
6997c478bd9Sstevel@tonic-gate 			    STRCONSTARGS(" from "));
7007c478bd9Sstevel@tonic-gate 			bp += used;
7017c478bd9Sstevel@tonic-gate 			remaining -= used;
7027c478bd9Sstevel@tonic-gate 		}
703f48205beScasper 		if (ctx.out.sf_pauid != (uid_t)-2) {
7047c478bd9Sstevel@tonic-gate 			/* 11 = strlen(" proc_auid ") */
7057c478bd9Sstevel@tonic-gate 			used = getuname(ctx.out.sf_pauid, -2, bp, remaining,
7067c478bd9Sstevel@tonic-gate 			    STRCONSTARGS(" proc_auid "));
7077c478bd9Sstevel@tonic-gate 			bp += used;
7087c478bd9Sstevel@tonic-gate 			remaining -= used;
7097c478bd9Sstevel@tonic-gate 		}
710f48205beScasper 		if (ctx.out.sf_peuid != (uid_t)-2) {
7117c478bd9Sstevel@tonic-gate 			used = getuname(ctx.out.sf_peuid, -2, bp, remaining,
7127c478bd9Sstevel@tonic-gate 			    STRCONSTARGS(" proc_uid "));
7137c478bd9Sstevel@tonic-gate 			bp += used;
7147c478bd9Sstevel@tonic-gate 			remaining -= used;
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate #if DEBUG
7177c478bd9Sstevel@tonic-gate 		/*
7187c478bd9Sstevel@tonic-gate 		 * with performance testing, this has the effect of
7197c478bd9Sstevel@tonic-gate 		 * making that each message is unique, so syslogd
7207c478bd9Sstevel@tonic-gate 		 * won't collect a series of messages as "last message
7217c478bd9Sstevel@tonic-gate 		 * repeated n times," another reason why DEBUG 0
7227c478bd9Sstevel@tonic-gate 		 * should perform better than DEBUG 1.  However the
7237c478bd9Sstevel@tonic-gate 		 * intention is to help debug lost data problems
7247c478bd9Sstevel@tonic-gate 		 */
7257c478bd9Sstevel@tonic-gate 		if (ctx.out.sf_sequence != -1) {
7267c478bd9Sstevel@tonic-gate 			fprintf(dbfp,
7277c478bd9Sstevel@tonic-gate 			    "syslog writing record %u / buffer %u\n",
7287c478bd9Sstevel@tonic-gate 			    ctx.out.sf_sequence, sequence);
7297c478bd9Sstevel@tonic-gate 			used = snprintf(bp, remaining, "  seq %u",
7307c478bd9Sstevel@tonic-gate 			    ctx.out.sf_sequence, sequence);
7317c478bd9Sstevel@tonic-gate 			remaining -= used;
7327c478bd9Sstevel@tonic-gate 			bp += used;
7337c478bd9Sstevel@tonic-gate 		} else
7347c478bd9Sstevel@tonic-gate 			fprintf(dbfp, "syslog writing buffer %u\n", sequence);
7357c478bd9Sstevel@tonic-gate #endif
7367c478bd9Sstevel@tonic-gate 		/*
7377c478bd9Sstevel@tonic-gate 		 * Long fields that may need truncation go here in
7387c478bd9Sstevel@tonic-gate 		 * order of decreasing priority.  Paths are truncated
7397c478bd9Sstevel@tonic-gate 		 * from the left, text from the right.
7407c478bd9Sstevel@tonic-gate 		 */
7417c478bd9Sstevel@tonic-gate 		if (ctx.out.sf_path != NULL) {
7427c478bd9Sstevel@tonic-gate 			used = fromleft(bp, remaining, STRCONSTARGS(" obj "),
7437c478bd9Sstevel@tonic-gate 			    ctx.out.sf_path, ctx.out.sf_pathlen);
7447c478bd9Sstevel@tonic-gate 			free(ctx.out.sf_path);
7457c478bd9Sstevel@tonic-gate 			bp += used;
7467c478bd9Sstevel@tonic-gate 			remaining -= used;
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 		if (ctx.out.sf_atpath != NULL) {
7497c478bd9Sstevel@tonic-gate 			used = fromleft(bp, remaining,
7507c478bd9Sstevel@tonic-gate 			    STRCONSTARGS(" attr_obj "),
7517c478bd9Sstevel@tonic-gate 			    ctx.out.sf_atpath, ctx.out.sf_atpathlen);
7527c478bd9Sstevel@tonic-gate 			free(ctx.out.sf_atpath);
7537c478bd9Sstevel@tonic-gate 			bp += used;
7547c478bd9Sstevel@tonic-gate 			remaining -= used;
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 		if (ctx.out.sf_uauth != NULL) {
7577c478bd9Sstevel@tonic-gate 			used = fromright(bp, remaining, STRCONSTARGS(" uauth "),
7587c478bd9Sstevel@tonic-gate 			    ctx.out.sf_uauth, ctx.out.sf_uauthlen);
7597c478bd9Sstevel@tonic-gate 			free(ctx.out.sf_path);
7607c478bd9Sstevel@tonic-gate 			bp += used;
7617c478bd9Sstevel@tonic-gate 			remaining -= used;
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 		if (ctx.out.sf_text != NULL) {
7647c478bd9Sstevel@tonic-gate 			used = fromright(bp, remaining,
7657c478bd9Sstevel@tonic-gate 			    STRCONSTARGS(AU_TEXT_NAME),
7667c478bd9Sstevel@tonic-gate 			    ctx.out.sf_text, ctx.out.sf_textlen);
7677c478bd9Sstevel@tonic-gate 			free(ctx.out.sf_text);
7687c478bd9Sstevel@tonic-gate 			bp += used;
7697c478bd9Sstevel@tonic-gate 			remaining -= used;
7707c478bd9Sstevel@tonic-gate 		}
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 	return (rc_ret);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate /*
7767c478bd9Sstevel@tonic-gate  * 1024 is max syslog record size, 48 is minimum header length,
7777c478bd9Sstevel@tonic-gate  * assuming a hostname length of 0.  maxavail reduces use of the
7787c478bd9Sstevel@tonic-gate  * allocated space by the length of the hostname (see maxavail)
7797c478bd9Sstevel@tonic-gate  */
7807c478bd9Sstevel@tonic-gate #define	OUTPUT_BUF_SIZE	1024 - 48
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate /* ARGSUSED */
7837c478bd9Sstevel@tonic-gate auditd_rc_t
7847c478bd9Sstevel@tonic-gate auditd_plugin(const char *input, size_t in_len, uint32_t sequence, char **error)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	char		*outbuf;
7877c478bd9Sstevel@tonic-gate 	auditd_rc_t	rc = AUDITD_SUCCESS;
7887c478bd9Sstevel@tonic-gate #if DEBUG
7897c478bd9Sstevel@tonic-gate 	static	uint32_t	last_sequence = 0;
7907c478bd9Sstevel@tonic-gate 	static	uint32_t	write_count = 0;
7917c478bd9Sstevel@tonic-gate 	static	uint32_t	toss_count = 0;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if ((last_sequence > 0) && (sequence != last_sequence + 1))
7947c478bd9Sstevel@tonic-gate 		fprintf(dbfp, "syslog: buffer sequence=%d but prev=%d\n",
7957c478bd9Sstevel@tonic-gate 				sequence, last_sequence);
7967c478bd9Sstevel@tonic-gate 	last_sequence = sequence;
7977c478bd9Sstevel@tonic-gate #endif
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	*error = NULL;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	outbuf = malloc(OUTPUT_BUF_SIZE);
8027c478bd9Sstevel@tonic-gate 	if (outbuf == NULL) {
8037c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "syslog: out of memory; seq=%u\n",
8047c478bd9Sstevel@tonic-gate 		    sequence));
8057c478bd9Sstevel@tonic-gate 		rc = AUDITD_NO_MEMORY;
8067c478bd9Sstevel@tonic-gate 		*error = strdup(gettext("Can't allocate buffers"));
8077c478bd9Sstevel@tonic-gate 	} else {
8087c478bd9Sstevel@tonic-gate 		rc = filter(input, sequence, outbuf, in_len, maxavail);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 		if (rc == AUDITD_SUCCESS) {
8117c478bd9Sstevel@tonic-gate 			__audit_syslog("audit", LOG_NDELAY,
8127c478bd9Sstevel@tonic-gate 			    LOG_AUDIT, LOG_NOTICE, outbuf);
8137c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "syslog: write_count=%u, "
8147c478bd9Sstevel@tonic-gate 			    "buffer=%u, tossed=%d\n",
8157c478bd9Sstevel@tonic-gate 			    ++write_count, sequence, toss_count));
8167c478bd9Sstevel@tonic-gate 		} else if (rc > 0) {	/* -1 == discard it */
8177c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "syslog: parse failed for buffer %u\n",
8187c478bd9Sstevel@tonic-gate 			    sequence));
8197c478bd9Sstevel@tonic-gate 			*error = strdup(gettext(
8207c478bd9Sstevel@tonic-gate 			    "Unable to parse audit record"));
8217c478bd9Sstevel@tonic-gate 		} else {
8227c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "syslog: rc = %d (-1 is discard), "
8237c478bd9Sstevel@tonic-gate 			    "sequence=%u, toss_count=%d\n",
8247c478bd9Sstevel@tonic-gate 			    rc, sequence, ++toss_count));
8257c478bd9Sstevel@tonic-gate 			rc = 0;
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 		free(outbuf);
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 	return (rc);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate auditd_rc_t
8337c478bd9Sstevel@tonic-gate auditd_plugin_open(const kva_t *kvlist, char **ret_list, char **error)
8347c478bd9Sstevel@tonic-gate {
8357c478bd9Sstevel@tonic-gate 	char		localname[MAXHOSTNAMELEN + 1];
8367c478bd9Sstevel@tonic-gate 	auditd_rc_t	rc;
8377c478bd9Sstevel@tonic-gate 	char		*value;
8387c478bd9Sstevel@tonic-gate 	/* kva_match doesn't do const, so copy the pointer */
8397c478bd9Sstevel@tonic-gate 	kva_t		*kva = (kva_t *)kvlist;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	*error = NULL;
8427c478bd9Sstevel@tonic-gate 	*ret_list = NULL;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	if ((kvlist == NULL) || ((value = kva_match(kva, "p_flags")) == NULL)) {
8457c478bd9Sstevel@tonic-gate 		*error = strdup(gettext(
8467c478bd9Sstevel@tonic-gate 		    "The \"p_flags\" attribute is missing."));
8477c478bd9Sstevel@tonic-gate 		return (AUDITD_INVALID);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 	if (!initialized) {
8507c478bd9Sstevel@tonic-gate #if DEBUG
8517c478bd9Sstevel@tonic-gate 		dbfp = __auditd_debug_file_open();
8527c478bd9Sstevel@tonic-gate #endif
8537c478bd9Sstevel@tonic-gate 		initialized = 1;
8547c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_init(&log_mutex, NULL);
8557c478bd9Sstevel@tonic-gate 		/*
8567c478bd9Sstevel@tonic-gate 		 * calculate length of the local hostname for adjusting the
8577c478bd9Sstevel@tonic-gate 		 * estimate of how much space is taken by the syslog header.
8587c478bd9Sstevel@tonic-gate 		 * If the local hostname isn't available, leave some room
8597c478bd9Sstevel@tonic-gate 		 * anyway.  (The -2 is for the blanks on either side of the
8607c478bd9Sstevel@tonic-gate 		 * hostname in the syslog message.)
8617c478bd9Sstevel@tonic-gate 		 */
8627c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&log_mutex);
8637c478bd9Sstevel@tonic-gate 		if (gethostname(localname, MAXHOSTNAMELEN))
8647c478bd9Sstevel@tonic-gate 			maxavail = OUTPUT_BUF_SIZE - 20;
8657c478bd9Sstevel@tonic-gate 		else
8667c478bd9Sstevel@tonic-gate 			maxavail = OUTPUT_BUF_SIZE - strlen(localname) - 2;
8677c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&log_mutex);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		if (init_hash(hosthash, 0, HOSTHASHSIZE, MAXHOSTNAMELEN))
8707c478bd9Sstevel@tonic-gate 			return (AUDITD_NO_MEMORY);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		if (init_hash(uidhash, -2, UIDHASHSIZE, USERNAMELEN))
8737c478bd9Sstevel@tonic-gate 			return (AUDITD_NO_MEMORY);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 		if (init_hash(gidhash, -2, GIDHASHSIZE, GIDNAMELEN))
8767c478bd9Sstevel@tonic-gate 			return (AUDITD_NO_MEMORY);
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&log_mutex);
8797c478bd9Sstevel@tonic-gate 	if ((rc = setmask(value)) != AUDITD_SUCCESS)
8807c478bd9Sstevel@tonic-gate 		*error = strdup(gettext(
8817c478bd9Sstevel@tonic-gate 		    "incorrect p_flags setting; no records will be output"));
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&log_mutex);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	return (rc);
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate auditd_rc_t
8897c478bd9Sstevel@tonic-gate auditd_plugin_close(char **error)
8907c478bd9Sstevel@tonic-gate {
8917c478bd9Sstevel@tonic-gate 	*error = NULL;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if (initialized) {
8947c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_destroy(&log_mutex);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 		free_hash(hosthash, HOSTHASHSIZE);
8977c478bd9Sstevel@tonic-gate 		free_hash(uidhash, UIDHASHSIZE);
8987c478bd9Sstevel@tonic-gate 		free_hash(gidhash, GIDHASHSIZE);
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 	initialized = 0;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	return (AUDITD_SUCCESS);
9037c478bd9Sstevel@tonic-gate }
904