xref: /onnv-gate/usr/src/lib/auditd_plugins/syslog/sysplugin.c (revision 11867:645e9a58cfab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51734Spaulson  * Common Development and Distribution License (the "License").
61734Spaulson  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2211704SJan.Friedel@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * convert binary audit records to syslog messages and
260Sstevel@tonic-gate  * send them off to syslog
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * auditd_plugin_open(), auditd_plugin() and auditd_plugin_close()
320Sstevel@tonic-gate  * implement a replacable library for use by auditd; they are a
330Sstevel@tonic-gate  * project private interface and may change without notice.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate #define	DEBUG	0
370Sstevel@tonic-gate #if DEBUG
3811706SJan.Friedel@Sun.COM #define	DPRINT(x) { (void) fprintf x; }
390Sstevel@tonic-gate #else
400Sstevel@tonic-gate #define	DPRINT(x)
410Sstevel@tonic-gate #endif
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <arpa/inet.h>
440Sstevel@tonic-gate #include <assert.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <fcntl.h>
470Sstevel@tonic-gate #include <grp.h>
480Sstevel@tonic-gate #include <libintl.h>
490Sstevel@tonic-gate #include <netdb.h>
500Sstevel@tonic-gate #include <netinet/in.h>
510Sstevel@tonic-gate #include <pthread.h>
520Sstevel@tonic-gate #include <pwd.h>
530Sstevel@tonic-gate #include <stdio.h>
540Sstevel@tonic-gate #include <stdlib.h>
550Sstevel@tonic-gate #include <string.h>
560Sstevel@tonic-gate #include <time.h>
570Sstevel@tonic-gate #include <syslog.h>
580Sstevel@tonic-gate #include <sys/types.h>
590Sstevel@tonic-gate #include <sys/socket.h>
600Sstevel@tonic-gate #include <unistd.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #include <bsm/audit.h>
630Sstevel@tonic-gate #include <bsm/audit_record.h>
640Sstevel@tonic-gate #include <security/auditd.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include "toktable.h"
670Sstevel@tonic-gate #include "sysplugin.h"
680Sstevel@tonic-gate #include "systoken.h"
690Sstevel@tonic-gate #include <audit_plugin.h>
700Sstevel@tonic-gate 
7111706SJan.Friedel@Sun.COM /* gettext() obfuscation routine for lint */
7211706SJan.Friedel@Sun.COM #ifdef __lint
7311706SJan.Friedel@Sun.COM #define	gettext(x)	x
7411706SJan.Friedel@Sun.COM #endif
7511706SJan.Friedel@Sun.COM 
760Sstevel@tonic-gate #if DEBUG
770Sstevel@tonic-gate static FILE	*dbfp;			/* debug file */
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate 
800Sstevel@tonic-gate extern void init_tokens();
810Sstevel@tonic-gate extern int parse_token(parse_context_t *);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static au_mask_t	mask;
840Sstevel@tonic-gate static int		initialized = 0;
850Sstevel@tonic-gate static size_t		maxavail;
860Sstevel@tonic-gate static pthread_mutex_t	log_mutex;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	ELLIPSIS	"..."
890Sstevel@tonic-gate #define	ELLIPSIS_SIZE	(sizeof (ELLIPSIS) - 1)
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * simple hashing for uid and hostname lookup
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * performance tests showed that cacheing the hostname, uid, and gid
950Sstevel@tonic-gate  * make about a 40% difference for short audit records and regularly
960Sstevel@tonic-gate  * repeating hostname, uid, etc
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * ht_type and ht_ip are only used for hostname lookup cacheing.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate typedef struct hashtable {
1010Sstevel@tonic-gate 	uint32_t	ht_key;
1020Sstevel@tonic-gate 	uint32_t	ht_type;
1030Sstevel@tonic-gate 	uint32_t	ht_ip[4];
1040Sstevel@tonic-gate 	char		*ht_value;
1050Sstevel@tonic-gate 	size_t		ht_length;
1060Sstevel@tonic-gate } hashtable_t;
1070Sstevel@tonic-gate #define	HOSTHASHSIZE	128
1080Sstevel@tonic-gate #define	UIDHASHSIZE	128
1090Sstevel@tonic-gate #define	GIDHASHSIZE	32
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static hashtable_t	uidhash[UIDHASHSIZE];
1120Sstevel@tonic-gate static hashtable_t	gidhash[GIDHASHSIZE];
1130Sstevel@tonic-gate static hashtable_t	hosthash[HOSTHASHSIZE];
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #define	STRCONSTARGS(s)	(s), (sizeof (s) - 1)
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * the hash "handles" collisions by overwriting the old
1180Sstevel@tonic-gate  * hash entry with the new.  Perfection is not the goal.
1190Sstevel@tonic-gate  *
1200Sstevel@tonic-gate  * the key (s) is a 32 bit integer, handled here as
1210Sstevel@tonic-gate  * four bytes.  If the hash size is increased beyond
1220Sstevel@tonic-gate  * 256, this macro will need some work.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate #define	HASH(s,  r, m)	{\
1250Sstevel@tonic-gate 				uint32_t	_mush = 0;\
1260Sstevel@tonic-gate 				int		_i;\
1270Sstevel@tonic-gate 				for (_i = 0; _i < 4; _i++) {\
1280Sstevel@tonic-gate 					_mush ^= *(s)++;\
1290Sstevel@tonic-gate 				}\
1300Sstevel@tonic-gate 				r = _mush % m;\
1310Sstevel@tonic-gate 			}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * The default mask for sysplugin is to reject all record types.
1360Sstevel@tonic-gate  * The parameters input here select which classes to allow.
1370Sstevel@tonic-gate  *
1380Sstevel@tonic-gate  * getauditflgsbin() outputs error messages to syslog.
1390Sstevel@tonic-gate  *
1400Sstevel@tonic-gate  * caller must hold log_mutex
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static auditd_rc_t
setmask(const char * flags)1440Sstevel@tonic-gate setmask(const char *flags)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	au_mask_t tmask;
1470Sstevel@tonic-gate 	char	*input, *ip, c;
1480Sstevel@tonic-gate 	auditd_rc_t	rc = AUDITD_SUCCESS;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	mask.am_success = 0x0;
1510Sstevel@tonic-gate 	mask.am_failure = 0x0;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (flags != NULL) {
1540Sstevel@tonic-gate 		/*
1550Sstevel@tonic-gate 		 * getauditflagsbin doesn't like blanks, but admins do
1560Sstevel@tonic-gate 		 */
1570Sstevel@tonic-gate 		input = malloc(strlen(flags) + 1);
1580Sstevel@tonic-gate 		if (input == NULL)
1590Sstevel@tonic-gate 			return (AUDITD_NO_MEMORY);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		ip = input;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		for (; (c = *flags) != '\0'; flags++) {
1640Sstevel@tonic-gate 			if (c == ' ')
1650Sstevel@tonic-gate 				continue;
1660Sstevel@tonic-gate 			*ip++ = c;
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 		*ip = '\0';
1690Sstevel@tonic-gate 		if (getauditflagsbin(input, &tmask) == 0) {
1700Sstevel@tonic-gate 			mask.am_success |= tmask.am_success;
1710Sstevel@tonic-gate 			mask.am_failure |= tmask.am_failure;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 	if ((mask.am_success | mask.am_failure) == 0) {
1750Sstevel@tonic-gate 		rc = AUDITD_INVALID;
1760Sstevel@tonic-gate 		__audit_syslog("audit_syslog.so", LOG_CONS | LOG_NDELAY,
1770Sstevel@tonic-gate 		    LOG_DAEMON, LOG_ERR,
1780Sstevel@tonic-gate 		    gettext("plugin is configured with empty class mask\n"));
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 	free(input);
1810Sstevel@tonic-gate 	return (rc);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * based on the current value of mask, either keep or toss the
1860Sstevel@tonic-gate  * current audit record.  The input is 1 for success, -1 for
1870Sstevel@tonic-gate  * failure.  0 means no exit or return token was seen.
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  * au_preselect returns 1 for keep it, 0 for delete it, and
1900Sstevel@tonic-gate  * -1 for some sort of error.  Here, 1 and -1 are considered
1910Sstevel@tonic-gate  * equivalent.  tossit() returns 1 for delete it and 0 for
1920Sstevel@tonic-gate  * keep it.
1930Sstevel@tonic-gate  */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate static int
tossit(au_event_t id,int passfail)1960Sstevel@tonic-gate tossit(au_event_t id, int passfail)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	int	rc;
1990Sstevel@tonic-gate 	int	selFlag;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	switch (passfail) {
2020Sstevel@tonic-gate 	case 1:
2030Sstevel@tonic-gate 		selFlag = AU_PRS_SUCCESS;
2040Sstevel@tonic-gate 		break;
2050Sstevel@tonic-gate 	case -1:
2060Sstevel@tonic-gate 		selFlag = AU_PRS_FAILURE;
2070Sstevel@tonic-gate 		break;
2080Sstevel@tonic-gate 	default:		/* no exit or return token */
2090Sstevel@tonic-gate 		selFlag = AU_PRS_BOTH;
2100Sstevel@tonic-gate 		break;
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 	(void) pthread_mutex_lock(&log_mutex);
2130Sstevel@tonic-gate 	rc = au_preselect(id, &mask, selFlag, AU_PRS_USECACHE);
2140Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&log_mutex);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	return (rc == 0);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate  * the three bytes for ellipsis could potentially be longer than the
2210Sstevel@tonic-gate  * space available for text if maxavail is within two bytes of
2220Sstevel@tonic-gate  * OUTPUT_BUF_SIZE, which can happen if the hostname is one or two
2230Sstevel@tonic-gate  * characters long.  If there isn't room for ellipsis, there isn't
2240Sstevel@tonic-gate  * room for the data, so it is simply dropped.
2250Sstevel@tonic-gate  */
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate static size_t
fromleft(char * p,size_t avail,char * attrname,size_t attrlen,char * txt,size_t txtlen)2280Sstevel@tonic-gate fromleft(char *p, size_t avail, char *attrname, size_t attrlen, char *txt,
2290Sstevel@tonic-gate 	size_t txtlen)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	size_t	len;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if (avail < attrlen + ELLIPSIS_SIZE)
2340Sstevel@tonic-gate 		return (0);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	(void) memcpy(p, attrname, attrlen);
2370Sstevel@tonic-gate 	p += attrlen;
2380Sstevel@tonic-gate 	avail -= attrlen;
2390Sstevel@tonic-gate 	if (txtlen > avail) {
2400Sstevel@tonic-gate 		(void) memcpy(p, ELLIPSIS, ELLIPSIS_SIZE);
2410Sstevel@tonic-gate 		txt += txtlen - (avail - ELLIPSIS_SIZE);
2420Sstevel@tonic-gate 		(void) memcpy(p + ELLIPSIS_SIZE, txt, avail - ELLIPSIS_SIZE);
2430Sstevel@tonic-gate 		len = attrlen + avail;
2440Sstevel@tonic-gate 		p += avail;
2450Sstevel@tonic-gate 	} else {
2460Sstevel@tonic-gate 		(void) memcpy(p, txt, txtlen);
2470Sstevel@tonic-gate 		len = attrlen + txtlen;
2480Sstevel@tonic-gate 		p += txtlen;
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 	*p = '\0';
2510Sstevel@tonic-gate 	return (len);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate static size_t
fromright(char * p,size_t avail,char * attrname,size_t attrlen,char * txt,size_t txtlen)2550Sstevel@tonic-gate fromright(char *p, size_t avail, char *attrname, size_t attrlen, char *txt,
2560Sstevel@tonic-gate 	size_t txtlen)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	size_t	len;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (avail < attrlen + ELLIPSIS_SIZE)
2610Sstevel@tonic-gate 		return (0);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	(void) memcpy(p, attrname, attrlen);
2640Sstevel@tonic-gate 	p += attrlen;
2650Sstevel@tonic-gate 	avail -= attrlen;
2660Sstevel@tonic-gate 	if (txtlen > avail) {
2670Sstevel@tonic-gate 		(void) memcpy(p, txt, avail - ELLIPSIS_SIZE);
2680Sstevel@tonic-gate 		(void) memcpy(p + (avail - ELLIPSIS_SIZE),
2690Sstevel@tonic-gate 		    ELLIPSIS, ELLIPSIS_SIZE);
2700Sstevel@tonic-gate 		len = attrlen + avail;
2710Sstevel@tonic-gate 		p += avail;
2720Sstevel@tonic-gate 	} else {
2730Sstevel@tonic-gate 		(void) memcpy(p, txt, txtlen);
2740Sstevel@tonic-gate 		p += txtlen;
2750Sstevel@tonic-gate 		len = attrlen + txtlen;
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 	*p = '\0';
2780Sstevel@tonic-gate 	return (len);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate static int
init_hash(hashtable_t * table,int bad_key,int table_length,size_t max_value)2820Sstevel@tonic-gate init_hash(hashtable_t *table, int bad_key, int table_length,
2830Sstevel@tonic-gate     size_t max_value)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	int	i;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	for (i = 0; i < table_length; i++) {
2880Sstevel@tonic-gate 		table[i].ht_value = malloc(max_value + 1);
2890Sstevel@tonic-gate 		table[i].ht_key = bad_key;
2900Sstevel@tonic-gate 		table[i].ht_length = 0;
2910Sstevel@tonic-gate 		if (table[i].ht_value == NULL) {
2920Sstevel@tonic-gate 			int	j;
2930Sstevel@tonic-gate 			for (j = 0; j < i; j++)
2940Sstevel@tonic-gate 				free(table[j].ht_value);
2950Sstevel@tonic-gate 			return (-1);
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 		*(table[i].ht_value) = '\0';
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 	return (0);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate static void
free_hash(hashtable_t * table,int table_length)3030Sstevel@tonic-gate free_hash(hashtable_t *table, int table_length)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	int	i;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	for (i = 0; i < table_length; i++) {
3080Sstevel@tonic-gate 		free(table[i].ht_value);
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * do IP -> hostname lookup
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate #define	UNKNOWN		"unknown"
3170Sstevel@tonic-gate #define	UNKNOWN_LEN	(sizeof (UNKNOWN))
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate static size_t
gethname(au_tid_addr_t * tid,char * p,size_t max,char * prefix,size_t prefix_len)3200Sstevel@tonic-gate gethname(au_tid_addr_t *tid, char *p, size_t max, char *prefix,
3210Sstevel@tonic-gate     size_t prefix_len)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	size_t			len, l;
3240Sstevel@tonic-gate 	struct hostent		*host;
3250Sstevel@tonic-gate 	int			rc;
3260Sstevel@tonic-gate 	int			af;
3270Sstevel@tonic-gate 	int			ix;
3280Sstevel@tonic-gate 	char			*hash_key;
3290Sstevel@tonic-gate 	uint32_t		key;
3300Sstevel@tonic-gate 	int			match;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (prefix_len > max)
3330Sstevel@tonic-gate 		return (0);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	(void) memcpy(p, prefix, prefix_len);
3360Sstevel@tonic-gate 	p += prefix_len;
3370Sstevel@tonic-gate 	max -= prefix_len;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (tid->at_type == AU_IPv6) {
3400Sstevel@tonic-gate 		key = tid->at_addr[0] ^
34111704SJan.Friedel@Sun.COM 		    tid->at_addr[1] ^
34211704SJan.Friedel@Sun.COM 		    tid->at_addr[2] ^
34311704SJan.Friedel@Sun.COM 		    tid->at_addr[3];
3440Sstevel@tonic-gate 	} else
3450Sstevel@tonic-gate 		key = (tid->at_addr[0]);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	hash_key = (char *)&key;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	HASH(hash_key, ix, HOSTHASHSIZE);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	match = 0;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if (key == 0) {
3540Sstevel@tonic-gate 		l = UNKNOWN_LEN;	/* includes end of string */
3550Sstevel@tonic-gate 		if (l > max)
3560Sstevel@tonic-gate 			l = max;
3570Sstevel@tonic-gate 		len = prefix_len + strlcpy(p, UNKNOWN, l);
3580Sstevel@tonic-gate 		return (len);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if (tid->at_type == AU_IPv6) {
3620Sstevel@tonic-gate 		if ((key == hosthash[ix].ht_key) &&
3630Sstevel@tonic-gate 		    (hosthash[ix].ht_type == tid->at_type)) {
3640Sstevel@tonic-gate 			int i;
3650Sstevel@tonic-gate 			match = 1;
3660Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
3670Sstevel@tonic-gate 				if (hosthash[ix].ht_ip[i] != tid->at_addr[i]) {
3680Sstevel@tonic-gate 					match = 0;
3690Sstevel@tonic-gate 					break;
3700Sstevel@tonic-gate 				}
3710Sstevel@tonic-gate 			}
3720Sstevel@tonic-gate 		}
3730Sstevel@tonic-gate 	} else if (key == hosthash[ix].ht_key) {
3740Sstevel@tonic-gate 		match = 1;
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 	if (!match) {
3770Sstevel@tonic-gate 		hosthash[ix].ht_key = key;
3780Sstevel@tonic-gate 		hosthash[ix].ht_type = tid->at_type;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		if (tid->at_type == AU_IPv4) {
3810Sstevel@tonic-gate 			hosthash[ix].ht_ip[0] = tid->at_addr[0];
3820Sstevel@tonic-gate 			af = AF_INET;
3830Sstevel@tonic-gate 		} else {
3840Sstevel@tonic-gate 			(void) memcpy((char *)hosthash[ix].ht_ip,
3850Sstevel@tonic-gate 			    (char *)tid->at_addr, AU_IPv6);
3860Sstevel@tonic-gate 			af = AF_INET6;
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 		host = getipnodebyaddr((const void *)tid->at_addr,
3890Sstevel@tonic-gate 		    tid->at_type, af, &rc);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 		if (host == NULL) {
3920Sstevel@tonic-gate 			(void) inet_ntop(af, (void *)tid->at_addr,
3930Sstevel@tonic-gate 			    hosthash[ix].ht_value, MAXHOSTNAMELEN);
3940Sstevel@tonic-gate 			hosthash[ix].ht_length = strlen(hosthash[ix].ht_value);
3950Sstevel@tonic-gate 		} else {
3960Sstevel@tonic-gate 			hosthash[ix].ht_length = strlcpy(hosthash[ix].ht_value,
3970Sstevel@tonic-gate 			    host->h_name,  MAXHOSTNAMELEN);
3980Sstevel@tonic-gate 			freehostent(host);
3990Sstevel@tonic-gate 		}
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 	l = hosthash[ix].ht_length + 1;
4020Sstevel@tonic-gate 	if (l > max)
4030Sstevel@tonic-gate 		l = max;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	len = prefix_len + strlcpy(p, hosthash[ix].ht_value, l);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	return (len);
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate  * the appropriate buffer length for getpwuid_r() isn't documented;
4110Sstevel@tonic-gate  * 1024 should be enough.
4120Sstevel@tonic-gate  */
4130Sstevel@tonic-gate #define	GETPWUID_BUFF_LEN	1024
4140Sstevel@tonic-gate #define	USERNAMELEN		256
4150Sstevel@tonic-gate #define	GIDNAMELEN		256
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate static size_t
getuname(uid_t uid,gid_t gid,char * p,size_t max,char * prefix,size_t prefix_len)4180Sstevel@tonic-gate getuname(uid_t uid, gid_t gid, char *p, size_t max, char *prefix,
4190Sstevel@tonic-gate     size_t prefix_len)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	struct passwd		pw;
4220Sstevel@tonic-gate 	char			pw_buf[GETPWUID_BUFF_LEN];
4230Sstevel@tonic-gate 	size_t			len, l;
4240Sstevel@tonic-gate 	struct group		gr;
4250Sstevel@tonic-gate 	int			ix;
4260Sstevel@tonic-gate 	char			*hash_key;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if (prefix_len > max)
4290Sstevel@tonic-gate 		return (0);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	len = prefix_len;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	(void) memcpy(p, prefix, len);
4340Sstevel@tonic-gate 	p += len;
4350Sstevel@tonic-gate 	max -= len;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	hash_key = (char *)&uid;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	HASH(hash_key, ix, UIDHASHSIZE);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	if (uid != uidhash[ix].ht_key) {
4420Sstevel@tonic-gate 		uidhash[ix].ht_key = uid;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 		if ((getpwuid_r(uid, &pw, pw_buf, GETPWUID_BUFF_LEN)) == NULL)
4450Sstevel@tonic-gate 			l = snprintf(uidhash[ix].ht_value, USERNAMELEN,
4460Sstevel@tonic-gate 			    "%d", uid);
4470Sstevel@tonic-gate 		else
4480Sstevel@tonic-gate 			l = strlcpy(uidhash[ix].ht_value, pw.pw_name,
4490Sstevel@tonic-gate 			    USERNAMELEN);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 		uidhash[ix].ht_length = l;
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	l = uidhash[ix].ht_length + 1;
4540Sstevel@tonic-gate 	if (l > max)
4550Sstevel@tonic-gate 		l = max;
4560Sstevel@tonic-gate 	(void) memcpy(p, uidhash[ix].ht_value, l);
4570Sstevel@tonic-gate 	len += l - 1;
4580Sstevel@tonic-gate 
4594321Scasper 	if (gid != (gid_t)-2) {
4600Sstevel@tonic-gate 		p += l - 1;
4610Sstevel@tonic-gate 		max -= l - 1;
4620Sstevel@tonic-gate 		if (max < 2)
4630Sstevel@tonic-gate 			return (len);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		hash_key = (char *)&gid;
4660Sstevel@tonic-gate 		HASH(hash_key, ix, GIDHASHSIZE);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		if (gid != gidhash[ix].ht_key) {
4690Sstevel@tonic-gate 			gidhash[ix].ht_key = gid;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 			if (getgrgid_r(gid, &gr,  pw_buf, GETPWUID_BUFF_LEN) ==
4720Sstevel@tonic-gate 			    NULL)
4730Sstevel@tonic-gate 				gidhash[ix].ht_length =
4740Sstevel@tonic-gate 				    snprintf(gidhash[ix].ht_value, GIDNAMELEN,
47511704SJan.Friedel@Sun.COM 				    "%d", gid);
4760Sstevel@tonic-gate 			else
4770Sstevel@tonic-gate 				gidhash[ix].ht_length =
4780Sstevel@tonic-gate 				    strlcpy(gidhash[ix].ht_value,
4790Sstevel@tonic-gate 				    gr.gr_name, GIDNAMELEN);
4800Sstevel@tonic-gate 		}
4810Sstevel@tonic-gate 		*p++ = ':';
4820Sstevel@tonic-gate 		len++;
4830Sstevel@tonic-gate 		max--;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 		l = gidhash[ix].ht_length + 1;
4860Sstevel@tonic-gate 		if (l > max)
4870Sstevel@tonic-gate 			l = max;
4880Sstevel@tonic-gate 		(void) memcpy(p, gidhash[ix].ht_value, l);
4890Sstevel@tonic-gate 		len += l - 1;
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 	return (len);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate /*
4950Sstevel@tonic-gate  * filter() parse input; toss if not wanted.
4960Sstevel@tonic-gate  *
4970Sstevel@tonic-gate  * the input value sequence is a number generated when the buffer
4980Sstevel@tonic-gate  * was queued.  ctx.out.sf_sequence, if not -1, is the sequence number
4990Sstevel@tonic-gate  * generated in c2audit.  It is not part of the "official" syslog
5000Sstevel@tonic-gate  * output but is included if DEBUG is on.
5010Sstevel@tonic-gate  */
5020Sstevel@tonic-gate #define	EVENT_NAME_LEN	32
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate static auditd_rc_t
filter(const char * input,uint64_t sequence,char * output,size_t in_len,size_t out_len)50511704SJan.Friedel@Sun.COM filter(const char *input, uint64_t sequence, char *output,
5060Sstevel@tonic-gate     size_t in_len, size_t out_len)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	parse_context_t		ctx;
5090Sstevel@tonic-gate 	char			*bp;
5100Sstevel@tonic-gate 	auditd_rc_t		rc = AUDITD_SUCCESS;
5110Sstevel@tonic-gate 	auditd_rc_t		rc_ret = AUDITD_SUCCESS;
5120Sstevel@tonic-gate 	size_t			used, remaining;
5130Sstevel@tonic-gate 	char			*last_adr; /* infinite loop check */
5140Sstevel@tonic-gate 	int			token_count = 0;
5150Sstevel@tonic-gate 	int			parse_rc;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	static parse_context_t	initial_ctx;
5180Sstevel@tonic-gate 	static int		first = 1;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (first) {
5210Sstevel@tonic-gate 		first = 0;
5220Sstevel@tonic-gate 
5231734Spaulson 		/*
5241734Spaulson 		 * Any member or submember of parse_context_t which utilizes
5251734Spaulson 		 * allocated memory must free() the memory after calling
5261734Spaulson 		 * parse_token() for both the preselected and non-preselected
5271734Spaulson 		 * cases.
5281734Spaulson 		 * New additions to parse_context_t or its submembers need to
5291734Spaulson 		 * have this same treatment.
5301734Spaulson 		 */
5310Sstevel@tonic-gate 		initial_ctx.out.sf_eventid = 0;
5320Sstevel@tonic-gate 		initial_ctx.out.sf_reclen = 0;
5330Sstevel@tonic-gate 		initial_ctx.out.sf_pass = 0;
5340Sstevel@tonic-gate 		initial_ctx.out.sf_asid = 0;
5354321Scasper 		initial_ctx.out.sf_auid = (uid_t)-2;
5364321Scasper 		initial_ctx.out.sf_euid = (uid_t)-2;
5374321Scasper 		initial_ctx.out.sf_egid = (gid_t)-2;
5380Sstevel@tonic-gate 		initial_ctx.out.sf_tid.at_type = 0;
5394321Scasper 		initial_ctx.out.sf_pauid = (uid_t)-2;
540*11867SJan.Friedel@Sun.COM 		initial_ctx.out.sf_peuid = (uid_t)-2;
5410Sstevel@tonic-gate 		initial_ctx.out.sf_uauthlen = 0;
5420Sstevel@tonic-gate 		initial_ctx.out.sf_uauth = NULL;
5430Sstevel@tonic-gate 		initial_ctx.out.sf_pathlen = 0;
5440Sstevel@tonic-gate 		initial_ctx.out.sf_path = NULL;
5450Sstevel@tonic-gate 		initial_ctx.out.sf_atpathlen = 0;
5460Sstevel@tonic-gate 		initial_ctx.out.sf_atpath = NULL;
5470Sstevel@tonic-gate 		initial_ctx.out.sf_textlen = 0;
5480Sstevel@tonic-gate 		initial_ctx.out.sf_text = NULL;
5490Sstevel@tonic-gate 		initial_ctx.out.sf_sequence = -1;
5500Sstevel@tonic-gate 		initial_ctx.out.sf_zonelen = 0;
5510Sstevel@tonic-gate 		initial_ctx.out.sf_zonename = NULL;
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 		init_tokens();		/* cmd/praudit/toktable.c */
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 	(void) memcpy(&ctx, &initial_ctx, sizeof (parse_context_t));
5560Sstevel@tonic-gate 	ctx.id = sequence;
5570Sstevel@tonic-gate 	ctx.adr.adr_stream = (char *)input;
5580Sstevel@tonic-gate 	ctx.adr.adr_now = (char *)input;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	last_adr = NULL;
5610Sstevel@tonic-gate 	while ((ctx.adr.adr_now - ctx.adr.adr_stream) < in_len) {
5620Sstevel@tonic-gate 		assert(last_adr != ctx.adr.adr_now);
5630Sstevel@tonic-gate 		token_count++;
5640Sstevel@tonic-gate 		last_adr = ctx.adr.adr_now;
5650Sstevel@tonic-gate 		if ((parse_rc = parse_token(&ctx)) != 0) {
5660Sstevel@tonic-gate 			char	message[256];
5670Sstevel@tonic-gate 			au_event_ent_t	*event;
5680Sstevel@tonic-gate 			char	event_name[EVENT_NAME_LEN];
5690Sstevel@tonic-gate 			char	sequence_str[EVENT_NAME_LEN];
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 			if (cacheauevent(&event, ctx.out.sf_eventid) < 0)
5720Sstevel@tonic-gate 				(void) snprintf(event_name, EVENT_NAME_LEN,
5737753STon.Nguyen@Sun.COM 				    "%hu", ctx.out.sf_eventid);
5740Sstevel@tonic-gate 			else
5750Sstevel@tonic-gate 				(void) strlcpy(event_name, event->ae_desc,
5760Sstevel@tonic-gate 				    EVENT_NAME_LEN);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 			if (token_count < 2)
5790Sstevel@tonic-gate 				/* leave rc_ret unchanged */
5800Sstevel@tonic-gate 				rc = AUDITD_INVALID;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 			if (ctx.out.sf_sequence != -1)
5830Sstevel@tonic-gate 				(void) snprintf(sequence_str, EVENT_NAME_LEN,
5840Sstevel@tonic-gate 				    " (seq=%u) ", ctx.out.sf_sequence);
5850Sstevel@tonic-gate 			else
5860Sstevel@tonic-gate 				sequence_str[0] = '\0';
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 			(void) snprintf(message, 256,
5890Sstevel@tonic-gate 			    gettext("error before token %d (previous token=%d)"
5900Sstevel@tonic-gate 			    " of record type %s%s\n"),
5910Sstevel@tonic-gate 			    token_count, parse_rc, event_name, sequence_str);
5920Sstevel@tonic-gate 
59311706SJan.Friedel@Sun.COM #if DEBUG
59411706SJan.Friedel@Sun.COM 			/*LINTED*/
59511706SJan.Friedel@Sun.COM 			(void) fprintf(dbfp, message);
59611706SJan.Friedel@Sun.COM #endif
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 			__audit_syslog("audit_syslog.so",
5990Sstevel@tonic-gate 			    LOG_PID | LOG_ODELAY | LOG_CONS,
6000Sstevel@tonic-gate 			    LOG_DAEMON, LOG_ALERT, message);
6010Sstevel@tonic-gate 			break;
6020Sstevel@tonic-gate 		}
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 	if (rc == AUDITD_SUCCESS) {
6050Sstevel@tonic-gate 		if (tossit(ctx.out.sf_eventid, ctx.out.sf_pass)) {
6060Sstevel@tonic-gate #if DEBUG
6070Sstevel@tonic-gate 			if (ctx.out.sf_sequence != -1)
60811706SJan.Friedel@Sun.COM 				(void) fprintf(dbfp,
6097753STon.Nguyen@Sun.COM 				    "syslog tossed (event=%hu) record %u "
61011704SJan.Friedel@Sun.COM 				    "/ buffer %llu\n",
6110Sstevel@tonic-gate 				    ctx.out.sf_eventid, ctx.out.sf_sequence,
6120Sstevel@tonic-gate 				    sequence);
6130Sstevel@tonic-gate 			else
61411706SJan.Friedel@Sun.COM 				(void) fprintf(dbfp,
61511704SJan.Friedel@Sun.COM 				    "syslog tossed (event=%hu) buffer %llu\n",
6160Sstevel@tonic-gate 				    ctx.out.sf_eventid, sequence);
6170Sstevel@tonic-gate #endif
6181734Spaulson 
6191734Spaulson 			/*
6201734Spaulson 			 * Members or submembers of parse_context_t which
6211734Spaulson 			 * utilize allocated memory need to free() the memory
6221734Spaulson 			 * here to handle the case of not being preselected as
6231734Spaulson 			 * well as below for when the event is preselected.
6241734Spaulson 			 * New additions to parse_context_t or any of its
6251734Spaulson 			 * submembers need to get the same treatment.
6261734Spaulson 			 */
6271734Spaulson 			if (ctx.out.sf_uauthlen > 0) {
6281734Spaulson 				free(ctx.out.sf_uauth);
6291734Spaulson 				ctx.out.sf_uauth = NULL;
6301734Spaulson 				ctx.out.sf_uauthlen = 0;
6311734Spaulson 			}
6321734Spaulson 			if (ctx.out.sf_pathlen > 0) {
6331734Spaulson 				free(ctx.out.sf_path);
6341734Spaulson 				ctx.out.sf_path = NULL;
6351734Spaulson 				ctx.out.sf_pathlen = 0;
6361734Spaulson 			}
6371734Spaulson 			if (ctx.out.sf_atpathlen > 0) {
6381734Spaulson 				free(ctx.out.sf_atpath);
6391734Spaulson 				ctx.out.sf_atpath = NULL;
6401734Spaulson 				ctx.out.sf_atpathlen = 0;
6411734Spaulson 			}
6421734Spaulson 			if (ctx.out.sf_textlen > 0) {
6431734Spaulson 				free(ctx.out.sf_text);
6441734Spaulson 				ctx.out.sf_text = NULL;
6451734Spaulson 				ctx.out.sf_textlen = 0;
6461734Spaulson 			}
6471734Spaulson 			if (ctx.out.sf_zonelen > 0) {
6481734Spaulson 				free(ctx.out.sf_zonename);
6491734Spaulson 				ctx.out.sf_zonename = NULL;
6501734Spaulson 				ctx.out.sf_zonelen = 0;
6511734Spaulson 			}
6521734Spaulson 
6530Sstevel@tonic-gate 			return (-1);	/* tell caller it was tossed */
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 		bp = output;
6560Sstevel@tonic-gate 		remaining = out_len;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		if (ctx.out.sf_eventid != 0) {
6590Sstevel@tonic-gate 			au_event_ent_t	*event;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 			if (cacheauevent(&event, ctx.out.sf_eventid) < 0)
6627753STon.Nguyen@Sun.COM 				used = snprintf(bp, remaining, "%hu",
6630Sstevel@tonic-gate 				    ctx.out.sf_eventid);
6640Sstevel@tonic-gate 			else
6650Sstevel@tonic-gate 				used = strlcpy(bp, event->ae_desc, remaining);
6660Sstevel@tonic-gate 			bp += used;
6670Sstevel@tonic-gate 			remaining -= used;
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 		if (ctx.out.sf_pass != 0) {
6700Sstevel@tonic-gate 			if (ctx.out.sf_pass < 0)
6710Sstevel@tonic-gate 				used = strlcpy(bp, " failed", remaining);
6720Sstevel@tonic-gate 			else
6730Sstevel@tonic-gate 				used = strlcpy(bp, " ok", remaining);
6740Sstevel@tonic-gate 			bp += used;
6750Sstevel@tonic-gate 			remaining -= used;
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 		if (ctx.out.sf_asid != 0) {
6780Sstevel@tonic-gate 			used = snprintf(bp, remaining, " session %u",
6790Sstevel@tonic-gate 			    ctx.out.sf_asid);
6800Sstevel@tonic-gate 			remaining -= used;
6810Sstevel@tonic-gate 			bp += used;
6820Sstevel@tonic-gate 		}
6834321Scasper 		if (ctx.out.sf_auid != (uid_t)-2) {
6840Sstevel@tonic-gate 			used = getuname(ctx.out.sf_auid, -2, bp, remaining,
6850Sstevel@tonic-gate 			    STRCONSTARGS(" by "));
6860Sstevel@tonic-gate 			bp += used;
6870Sstevel@tonic-gate 			remaining -= used;
6880Sstevel@tonic-gate 		}
6894321Scasper 		if (ctx.out.sf_euid != (uid_t)-2) {
6900Sstevel@tonic-gate 			/* 4 = strlen(" as ") */
6910Sstevel@tonic-gate 			used = getuname(ctx.out.sf_euid, ctx.out.sf_egid, bp,
6920Sstevel@tonic-gate 			    remaining, STRCONSTARGS(" as "));
6930Sstevel@tonic-gate 			bp += used;
6940Sstevel@tonic-gate 			remaining -= used;
6950Sstevel@tonic-gate 		}
6960Sstevel@tonic-gate 		if (ctx.out.sf_zonename != NULL) {
6970Sstevel@tonic-gate 			used = fromright(bp, remaining,
6980Sstevel@tonic-gate 			    STRCONSTARGS(" in "),
6990Sstevel@tonic-gate 			    ctx.out.sf_zonename, ctx.out.sf_zonelen);
7000Sstevel@tonic-gate 			free(ctx.out.sf_zonename);
7010Sstevel@tonic-gate 			bp += used;
7020Sstevel@tonic-gate 			remaining -= used;
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 		if (ctx.out.sf_tid.at_type != 0) {
7050Sstevel@tonic-gate 			/* 6 = strlen(" from ") */
7060Sstevel@tonic-gate 			used = gethname(&(ctx.out.sf_tid), bp, remaining,
7070Sstevel@tonic-gate 			    STRCONSTARGS(" from "));
7080Sstevel@tonic-gate 			bp += used;
7090Sstevel@tonic-gate 			remaining -= used;
7100Sstevel@tonic-gate 		}
7114321Scasper 		if (ctx.out.sf_pauid != (uid_t)-2) {
7120Sstevel@tonic-gate 			/* 11 = strlen(" proc_auid ") */
7130Sstevel@tonic-gate 			used = getuname(ctx.out.sf_pauid, -2, bp, remaining,
7140Sstevel@tonic-gate 			    STRCONSTARGS(" proc_auid "));
7150Sstevel@tonic-gate 			bp += used;
7160Sstevel@tonic-gate 			remaining -= used;
7170Sstevel@tonic-gate 		}
7184321Scasper 		if (ctx.out.sf_peuid != (uid_t)-2) {
7190Sstevel@tonic-gate 			used = getuname(ctx.out.sf_peuid, -2, bp, remaining,
7200Sstevel@tonic-gate 			    STRCONSTARGS(" proc_uid "));
7210Sstevel@tonic-gate 			bp += used;
7220Sstevel@tonic-gate 			remaining -= used;
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate #if DEBUG
7250Sstevel@tonic-gate 		/*
7260Sstevel@tonic-gate 		 * with performance testing, this has the effect of
7270Sstevel@tonic-gate 		 * making that each message is unique, so syslogd
7280Sstevel@tonic-gate 		 * won't collect a series of messages as "last message
7290Sstevel@tonic-gate 		 * repeated n times," another reason why DEBUG 0
7300Sstevel@tonic-gate 		 * should perform better than DEBUG 1.  However the
7310Sstevel@tonic-gate 		 * intention is to help debug lost data problems
7320Sstevel@tonic-gate 		 */
7330Sstevel@tonic-gate 		if (ctx.out.sf_sequence != -1) {
73411706SJan.Friedel@Sun.COM 			(void) fprintf(dbfp,
73511704SJan.Friedel@Sun.COM 			    "syslog writing record %u / buffer %llu\n",
7360Sstevel@tonic-gate 			    ctx.out.sf_sequence, sequence);
7370Sstevel@tonic-gate 			used = snprintf(bp, remaining, "  seq %u",
7380Sstevel@tonic-gate 			    ctx.out.sf_sequence, sequence);
7390Sstevel@tonic-gate 			remaining -= used;
7400Sstevel@tonic-gate 			bp += used;
7410Sstevel@tonic-gate 		} else
74211706SJan.Friedel@Sun.COM 			(void) fprintf(dbfp, "syslog writing buffer %llu\n",
74311706SJan.Friedel@Sun.COM 			    sequence);
7440Sstevel@tonic-gate #endif
7450Sstevel@tonic-gate 		/*
7460Sstevel@tonic-gate 		 * Long fields that may need truncation go here in
7470Sstevel@tonic-gate 		 * order of decreasing priority.  Paths are truncated
7480Sstevel@tonic-gate 		 * from the left, text from the right.
7490Sstevel@tonic-gate 		 */
7500Sstevel@tonic-gate 		if (ctx.out.sf_path != NULL) {
7510Sstevel@tonic-gate 			used = fromleft(bp, remaining, STRCONSTARGS(" obj "),
7520Sstevel@tonic-gate 			    ctx.out.sf_path, ctx.out.sf_pathlen);
7530Sstevel@tonic-gate 			free(ctx.out.sf_path);
7540Sstevel@tonic-gate 			bp += used;
7550Sstevel@tonic-gate 			remaining -= used;
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 		if (ctx.out.sf_atpath != NULL) {
7580Sstevel@tonic-gate 			used = fromleft(bp, remaining,
7590Sstevel@tonic-gate 			    STRCONSTARGS(" attr_obj "),
7600Sstevel@tonic-gate 			    ctx.out.sf_atpath, ctx.out.sf_atpathlen);
7610Sstevel@tonic-gate 			free(ctx.out.sf_atpath);
7620Sstevel@tonic-gate 			bp += used;
7630Sstevel@tonic-gate 			remaining -= used;
7640Sstevel@tonic-gate 		}
7650Sstevel@tonic-gate 		if (ctx.out.sf_uauth != NULL) {
7660Sstevel@tonic-gate 			used = fromright(bp, remaining, STRCONSTARGS(" uauth "),
7670Sstevel@tonic-gate 			    ctx.out.sf_uauth, ctx.out.sf_uauthlen);
7680Sstevel@tonic-gate 			free(ctx.out.sf_path);
7690Sstevel@tonic-gate 			bp += used;
7700Sstevel@tonic-gate 			remaining -= used;
7710Sstevel@tonic-gate 		}
7720Sstevel@tonic-gate 		if (ctx.out.sf_text != NULL) {
7730Sstevel@tonic-gate 			used = fromright(bp, remaining,
7740Sstevel@tonic-gate 			    STRCONSTARGS(AU_TEXT_NAME),
7750Sstevel@tonic-gate 			    ctx.out.sf_text, ctx.out.sf_textlen);
7760Sstevel@tonic-gate 			free(ctx.out.sf_text);
7770Sstevel@tonic-gate 			bp += used;
7780Sstevel@tonic-gate 			remaining -= used;
7790Sstevel@tonic-gate 		}
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 	return (rc_ret);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate /*
7850Sstevel@tonic-gate  * 1024 is max syslog record size, 48 is minimum header length,
7860Sstevel@tonic-gate  * assuming a hostname length of 0.  maxavail reduces use of the
7870Sstevel@tonic-gate  * allocated space by the length of the hostname (see maxavail)
7880Sstevel@tonic-gate  */
7890Sstevel@tonic-gate #define	OUTPUT_BUF_SIZE	1024 - 48
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate /* ARGSUSED */
7920Sstevel@tonic-gate auditd_rc_t
auditd_plugin(const char * input,size_t in_len,uint64_t sequence,char ** error)79311704SJan.Friedel@Sun.COM auditd_plugin(const char *input, size_t in_len, uint64_t sequence, char **error)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	char		*outbuf;
7960Sstevel@tonic-gate 	auditd_rc_t	rc = AUDITD_SUCCESS;
7970Sstevel@tonic-gate #if DEBUG
79811704SJan.Friedel@Sun.COM 	static	uint64_t	last_sequence = 0;
79911704SJan.Friedel@Sun.COM 	static	uint64_t	write_count = 0;
80011704SJan.Friedel@Sun.COM 	static	uint64_t	toss_count = 0;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	if ((last_sequence > 0) && (sequence != last_sequence + 1))
80311706SJan.Friedel@Sun.COM 		(void) fprintf(dbfp,
80411706SJan.Friedel@Sun.COM 		    "syslog: buffer sequence=%llu but prev=%llu\n",
80511704SJan.Friedel@Sun.COM 		    sequence, last_sequence);
8060Sstevel@tonic-gate 	last_sequence = sequence;
8070Sstevel@tonic-gate #endif
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	*error = NULL;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	outbuf = malloc(OUTPUT_BUF_SIZE);
8120Sstevel@tonic-gate 	if (outbuf == NULL) {
81311704SJan.Friedel@Sun.COM 		DPRINT((dbfp, "syslog: out of memory; seq=%llu\n",
8140Sstevel@tonic-gate 		    sequence));
8150Sstevel@tonic-gate 		rc = AUDITD_NO_MEMORY;
8160Sstevel@tonic-gate 		*error = strdup(gettext("Can't allocate buffers"));
8170Sstevel@tonic-gate 	} else {
8180Sstevel@tonic-gate 		rc = filter(input, sequence, outbuf, in_len, maxavail);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 		if (rc == AUDITD_SUCCESS) {
8210Sstevel@tonic-gate 			__audit_syslog("audit", LOG_NDELAY,
8220Sstevel@tonic-gate 			    LOG_AUDIT, LOG_NOTICE, outbuf);
82311704SJan.Friedel@Sun.COM 			DPRINT((dbfp, "syslog: write_count=%llu, "
82411704SJan.Friedel@Sun.COM 			    "buffer=%llu, tossed=%llu\n",
8250Sstevel@tonic-gate 			    ++write_count, sequence, toss_count));
8260Sstevel@tonic-gate 		} else if (rc > 0) {	/* -1 == discard it */
82711704SJan.Friedel@Sun.COM 			DPRINT((dbfp, "syslog: parse failed for buffer %llu\n",
8280Sstevel@tonic-gate 			    sequence));
8290Sstevel@tonic-gate 			*error = strdup(gettext(
8300Sstevel@tonic-gate 			    "Unable to parse audit record"));
8310Sstevel@tonic-gate 		} else {
8320Sstevel@tonic-gate 			DPRINT((dbfp, "syslog: rc = %d (-1 is discard), "
83311704SJan.Friedel@Sun.COM 			    "sequence=%llu, toss_count=%llu\n",
8340Sstevel@tonic-gate 			    rc, sequence, ++toss_count));
8350Sstevel@tonic-gate 			rc = 0;
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 		free(outbuf);
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 	return (rc);
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate auditd_rc_t
auditd_plugin_open(const kva_t * kvlist,char ** ret_list,char ** error)8430Sstevel@tonic-gate auditd_plugin_open(const kva_t *kvlist, char **ret_list, char **error)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate 	char		localname[MAXHOSTNAMELEN + 1];
8460Sstevel@tonic-gate 	auditd_rc_t	rc;
8470Sstevel@tonic-gate 	char		*value;
8480Sstevel@tonic-gate 	/* kva_match doesn't do const, so copy the pointer */
8490Sstevel@tonic-gate 	kva_t		*kva = (kva_t *)kvlist;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	*error = NULL;
8520Sstevel@tonic-gate 	*ret_list = NULL;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	if ((kvlist == NULL) || ((value = kva_match(kva, "p_flags")) == NULL)) {
8550Sstevel@tonic-gate 		*error = strdup(gettext(
8560Sstevel@tonic-gate 		    "The \"p_flags\" attribute is missing."));
8570Sstevel@tonic-gate 		return (AUDITD_INVALID);
8580Sstevel@tonic-gate 	}
8590Sstevel@tonic-gate 	if (!initialized) {
8600Sstevel@tonic-gate #if DEBUG
8610Sstevel@tonic-gate 		dbfp = __auditd_debug_file_open();
8620Sstevel@tonic-gate #endif
8630Sstevel@tonic-gate 		initialized = 1;
8640Sstevel@tonic-gate 		(void) pthread_mutex_init(&log_mutex, NULL);
8650Sstevel@tonic-gate 		/*
8660Sstevel@tonic-gate 		 * calculate length of the local hostname for adjusting the
8670Sstevel@tonic-gate 		 * estimate of how much space is taken by the syslog header.
8680Sstevel@tonic-gate 		 * If the local hostname isn't available, leave some room
8690Sstevel@tonic-gate 		 * anyway.  (The -2 is for the blanks on either side of the
8700Sstevel@tonic-gate 		 * hostname in the syslog message.)
8710Sstevel@tonic-gate 		 */
8720Sstevel@tonic-gate 		(void) pthread_mutex_lock(&log_mutex);
8730Sstevel@tonic-gate 		if (gethostname(localname, MAXHOSTNAMELEN))
8740Sstevel@tonic-gate 			maxavail = OUTPUT_BUF_SIZE - 20;
8750Sstevel@tonic-gate 		else
8760Sstevel@tonic-gate 			maxavail = OUTPUT_BUF_SIZE - strlen(localname) - 2;
8770Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&log_mutex);
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 		if (init_hash(hosthash, 0, HOSTHASHSIZE, MAXHOSTNAMELEN))
8800Sstevel@tonic-gate 			return (AUDITD_NO_MEMORY);
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 		if (init_hash(uidhash, -2, UIDHASHSIZE, USERNAMELEN))
8830Sstevel@tonic-gate 			return (AUDITD_NO_MEMORY);
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 		if (init_hash(gidhash, -2, GIDHASHSIZE, GIDNAMELEN))
8860Sstevel@tonic-gate 			return (AUDITD_NO_MEMORY);
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 	(void) pthread_mutex_lock(&log_mutex);
8890Sstevel@tonic-gate 	if ((rc = setmask(value)) != AUDITD_SUCCESS)
8900Sstevel@tonic-gate 		*error = strdup(gettext(
8910Sstevel@tonic-gate 		    "incorrect p_flags setting; no records will be output"));
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&log_mutex);
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	return (rc);
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate auditd_rc_t
auditd_plugin_close(char ** error)8990Sstevel@tonic-gate auditd_plugin_close(char **error)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate 	*error = NULL;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	if (initialized) {
9040Sstevel@tonic-gate 		(void) pthread_mutex_destroy(&log_mutex);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 		free_hash(hosthash, HOSTHASHSIZE);
9070Sstevel@tonic-gate 		free_hash(uidhash, UIDHASHSIZE);
9080Sstevel@tonic-gate 		free_hash(gidhash, GIDHASHSIZE);
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 	initialized = 0;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	return (AUDITD_SUCCESS);
9130Sstevel@tonic-gate }
914