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