xref: /titanic_51/usr/src/cmd/praudit/format.c (revision f72effdea24d97a107b04e4b041cf5081dae0ee9)
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
581490fd2Sgww  * Common Development and Distribution License (the "License").
681490fd2Sgww  * 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 /*
228dc347dbStz204579  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #define	_REENTRANT
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <grp.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <netdb.h>
357c478bd9Sstevel@tonic-gate #include <time.h>
367c478bd9Sstevel@tonic-gate #include <pwd.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <wchar.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
457c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
467c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
477c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
507c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
517c478bd9Sstevel@tonic-gate #include <sys/types.h>
52a7746f66Stz204579 #include <aclutils.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include "praudit.h"
557c478bd9Sstevel@tonic-gate #include "toktable.h"
567c478bd9Sstevel@tonic-gate #include "adt_xlate.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static void	convertascii(char *p, char *c, int size);
597c478bd9Sstevel@tonic-gate static int	convertbinary(char *p, char *c, int size);
607c478bd9Sstevel@tonic-gate static void	eventmodifier2string(ushort_t emodifier, char *modstring,
617c478bd9Sstevel@tonic-gate     size_t modlen);
627c478bd9Sstevel@tonic-gate static int	do_mtime32(pr_context_t *context, int status, int flag,
637c478bd9Sstevel@tonic-gate     uint32_t scale);
647c478bd9Sstevel@tonic-gate static int	do_mtime64(pr_context_t *context, int status, int flag,
657c478bd9Sstevel@tonic-gate     uint64_t scale);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * ------------------------------------------------------
697c478bd9Sstevel@tonic-gate  * field widths for arbitrary data token type
707c478bd9Sstevel@tonic-gate  * ------------------------------------------------------
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate static struct fw {
737c478bd9Sstevel@tonic-gate 	char	basic_unit;
747c478bd9Sstevel@tonic-gate 	struct {
757c478bd9Sstevel@tonic-gate 		char	print_base;
767c478bd9Sstevel@tonic-gate 		int	field_width;
777c478bd9Sstevel@tonic-gate 	} pwidth[5];
787c478bd9Sstevel@tonic-gate } fwidth[] = {
797c478bd9Sstevel@tonic-gate 	/* character data type, 8 bits */
807c478bd9Sstevel@tonic-gate 		AUR_CHAR,	AUP_BINARY,	12,
817c478bd9Sstevel@tonic-gate 				AUP_OCTAL,	 6,
827c478bd9Sstevel@tonic-gate 				AUP_DECIMAL,	 6,
837c478bd9Sstevel@tonic-gate 				AUP_HEX,	 6,
848dc347dbStz204579 				AUP_STRING,	 1,
857c478bd9Sstevel@tonic-gate 		AUR_BYTE,	AUP_BINARY,	12,
867c478bd9Sstevel@tonic-gate 				AUP_OCTAL,	 6,
877c478bd9Sstevel@tonic-gate 				AUP_DECIMAL,	 6,
887c478bd9Sstevel@tonic-gate 				AUP_HEX,	 6,
898dc347dbStz204579 				AUP_STRING,	 1,
907c478bd9Sstevel@tonic-gate 		AUR_SHORT,	AUP_BINARY,	20,
917c478bd9Sstevel@tonic-gate 				AUP_OCTAL,	10,
927c478bd9Sstevel@tonic-gate 				AUP_DECIMAL,	10,
937c478bd9Sstevel@tonic-gate 				AUP_HEX,	 8,
947c478bd9Sstevel@tonic-gate 				AUP_STRING,	 6,
957c478bd9Sstevel@tonic-gate 		AUR_INT32,	AUP_BINARY,	36,
967c478bd9Sstevel@tonic-gate 				AUP_OCTAL,	18,
977c478bd9Sstevel@tonic-gate 				AUP_DECIMAL,	18,
987c478bd9Sstevel@tonic-gate 				AUP_HEX,	12,
997c478bd9Sstevel@tonic-gate 				AUP_STRING,	10,
1007c478bd9Sstevel@tonic-gate 		AUR_INT64,	AUP_BINARY,	68,
1017c478bd9Sstevel@tonic-gate 				AUP_OCTAL,	34,
1027c478bd9Sstevel@tonic-gate 				AUP_DECIMAL,	34,
1037c478bd9Sstevel@tonic-gate 				AUP_HEX,	20,
1047c478bd9Sstevel@tonic-gate 				AUP_STRING,	20};
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static int	numwidthentries = sizeof (fwidth)
1087c478bd9Sstevel@tonic-gate 			/ sizeof (struct fw);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
1137c478bd9Sstevel@tonic-gate  * do_newline:
1147c478bd9Sstevel@tonic-gate  *		  Print a newline, if needed according to various formatting
1157c478bd9Sstevel@tonic-gate  *		  rules.
1167c478bd9Sstevel@tonic-gate  * return codes :   0 - success
1177c478bd9Sstevel@tonic-gate  *		:  -1 - error
1187c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate int
1217c478bd9Sstevel@tonic-gate do_newline(pr_context_t *context, int flag)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	int	retstat = 0;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_ONELINE) && (flag == 1))
1267c478bd9Sstevel@tonic-gate 		retstat = pr_putchar(context, '\n');
1277c478bd9Sstevel@tonic-gate 	else if (!(context->format & PRF_XMLM))
1287c478bd9Sstevel@tonic-gate 		retstat = pr_printf(context, "%s", context->SEPARATOR);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	return (retstat);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate int
1347c478bd9Sstevel@tonic-gate open_tag(pr_context_t *context, int tagnum)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	int		err = 0;
1377c478bd9Sstevel@tonic-gate 	token_desc_t	*tag;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* no-op if not doing XML format */
1407c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_XMLM))
1417c478bd9Sstevel@tonic-gate 		return (0);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	tag = &tokentable[tagnum];
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/*
1467c478bd9Sstevel@tonic-gate 	 * First if needed do an implicit finish of a pending open for an
1477c478bd9Sstevel@tonic-gate 	 * extended tag.  I.e., for the extended tag xxx:
1487c478bd9Sstevel@tonic-gate 	 *	<xxx a=".." b=".."> ...  </xxx>
1497c478bd9Sstevel@tonic-gate 	 * -- insert a close bracket after the last attribute
1507c478bd9Sstevel@tonic-gate 	 * (in other words, when the 1st non-attribute is opened while
1517c478bd9Sstevel@tonic-gate 	 * this is pending). Note that only one tag could be pending at
1527c478bd9Sstevel@tonic-gate 	 * a given time -- it couldn't be nested.
1537c478bd9Sstevel@tonic-gate 	 */
1547c478bd9Sstevel@tonic-gate 	if (context->pending_flag && (tag->t_type != T_ATTRIBUTE)) {
1557c478bd9Sstevel@tonic-gate 		/* complete pending extended open */
1567c478bd9Sstevel@tonic-gate 		err = pr_putchar(context, '>');
1577c478bd9Sstevel@tonic-gate 		if (err != 0)
1587c478bd9Sstevel@tonic-gate 			return (err);
1597c478bd9Sstevel@tonic-gate 		context->pending_flag = 0;
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (is_header_token(tagnum) || is_file_token(tagnum)) {
1637c478bd9Sstevel@tonic-gate 		/* File token or new record on new line */
1647c478bd9Sstevel@tonic-gate 		err = pr_putchar(context, '\n');
1657c478bd9Sstevel@tonic-gate 	} else if (is_token(tagnum)) {
1667c478bd9Sstevel@tonic-gate 		/* Each token on new line if possible */
1677c478bd9Sstevel@tonic-gate 		err = do_newline(context, 1);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	if (err != 0)
1707c478bd9Sstevel@tonic-gate 		return (err);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	switch (tag->t_type) {
1737c478bd9Sstevel@tonic-gate 	case T_ATTRIBUTE:
1747c478bd9Sstevel@tonic-gate 		err = pr_printf(context, " %s=\"", tag->t_tagname);
1757c478bd9Sstevel@tonic-gate 		break;
1767c478bd9Sstevel@tonic-gate 	case T_ELEMENT:
1777c478bd9Sstevel@tonic-gate 		err = pr_printf(context, "<%s>", tag->t_tagname);
1787c478bd9Sstevel@tonic-gate 		break;
1797c478bd9Sstevel@tonic-gate 	case T_ENCLOSED:
1807c478bd9Sstevel@tonic-gate 		err = pr_printf(context, "<%s", tag->t_tagname);
1817c478bd9Sstevel@tonic-gate 		break;
1827c478bd9Sstevel@tonic-gate 	case T_EXTENDED:
1837c478bd9Sstevel@tonic-gate 		err = pr_printf(context, "<%s", tag->t_tagname);
1847c478bd9Sstevel@tonic-gate 		if (err == 0)
1857c478bd9Sstevel@tonic-gate 			context->pending_flag = tagnum;
1867c478bd9Sstevel@tonic-gate 		break;
1877c478bd9Sstevel@tonic-gate 	default:
1887c478bd9Sstevel@tonic-gate 		break;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (is_header_token(tagnum) && (err == 0))
1927c478bd9Sstevel@tonic-gate 		context->current_rec = tagnum;	/* set start of new record */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	return (err);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Do an implicit close of a record when needed.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate int
2017c478bd9Sstevel@tonic-gate check_close_rec(pr_context_t *context, int tagnum)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	int	err = 0;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/* no-op if not doing XML format */
2067c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_XMLM))
2077c478bd9Sstevel@tonic-gate 		return (0);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * If we're opening a header or the file token (i.e., starting a new
2117c478bd9Sstevel@tonic-gate 	 * record), if there's a current record in progress do an implicit
2127c478bd9Sstevel@tonic-gate 	 * close of it.
2137c478bd9Sstevel@tonic-gate 	 */
2147c478bd9Sstevel@tonic-gate 	if ((is_header_token(tagnum) || is_file_token(tagnum)) &&
2157c478bd9Sstevel@tonic-gate 	    context->current_rec) {
2167c478bd9Sstevel@tonic-gate 		err = do_newline(context, 1);
2177c478bd9Sstevel@tonic-gate 		if (err == 0)
2187c478bd9Sstevel@tonic-gate 			err = close_tag(context, context->current_rec);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	return (err);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * explicit finish of a pending open for an extended tag.
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate int
2287c478bd9Sstevel@tonic-gate finish_open_tag(pr_context_t *context)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	int	err = 0;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/* no-op if not doing XML format */
2337c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_XMLM))
2347c478bd9Sstevel@tonic-gate 		return (0);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (context->pending_flag) {
2377c478bd9Sstevel@tonic-gate 		/* complete pending extended open */
2387c478bd9Sstevel@tonic-gate 		err = pr_putchar(context, '>');
2397c478bd9Sstevel@tonic-gate 		if (err == 0)
2407c478bd9Sstevel@tonic-gate 			context->pending_flag = 0;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	return (err);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate int
2467c478bd9Sstevel@tonic-gate close_tag(pr_context_t *context, int tagnum)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	int		err = 0;
2497c478bd9Sstevel@tonic-gate 	token_desc_t	*tag;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/* no-op if not doing XML format */
2527c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_XMLM))
2537c478bd9Sstevel@tonic-gate 		return (0);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	tag = &tokentable[tagnum];
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	switch (tag->t_type) {
2587c478bd9Sstevel@tonic-gate 	case T_ATTRIBUTE:
2597c478bd9Sstevel@tonic-gate 		err = pr_putchar(context, '\"');
2607c478bd9Sstevel@tonic-gate 		break;
2617c478bd9Sstevel@tonic-gate 	case T_ELEMENT:
2627c478bd9Sstevel@tonic-gate 		err = pr_printf(context, "</%s>", tag->t_tagname);
2637c478bd9Sstevel@tonic-gate 		break;
2647c478bd9Sstevel@tonic-gate 	case T_ENCLOSED:
2657c478bd9Sstevel@tonic-gate 		err = pr_printf(context, "/>");
2667c478bd9Sstevel@tonic-gate 		break;
2677c478bd9Sstevel@tonic-gate 	case T_EXTENDED:
2687c478bd9Sstevel@tonic-gate 		err = pr_printf(context, "</%s>", tag->t_tagname);
2697c478bd9Sstevel@tonic-gate 		break;
2707c478bd9Sstevel@tonic-gate 	default:
2717c478bd9Sstevel@tonic-gate 		break;
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if (is_header_token(tagnum) && (err == 0))
2757c478bd9Sstevel@tonic-gate 		context->current_rec = 0;	/* closing rec; none current */
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	return (err);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
2827c478bd9Sstevel@tonic-gate  * process_tag:
2837c478bd9Sstevel@tonic-gate  *		  Calls the routine corresponding to the tag
2847c478bd9Sstevel@tonic-gate  *		  Note that to use this mechanism, all such routines must
2857c478bd9Sstevel@tonic-gate  *		  take 2 ints for their parameters; the first of these is
2867c478bd9Sstevel@tonic-gate  *		  the current status.
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  *		  flag = 1 for newline / delimiter, else 0
2897c478bd9Sstevel@tonic-gate  * return codes : -1 - error
2907c478bd9Sstevel@tonic-gate  *		:  0 - successful
2917c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate int
2947c478bd9Sstevel@tonic-gate process_tag(pr_context_t *context, int tagnum, int status, int flag)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	int retstat;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	retstat = status;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (retstat)
3017c478bd9Sstevel@tonic-gate 		return (retstat);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if ((tagnum > 0) && (tagnum <= MAXTAG) &&
3047c478bd9Sstevel@tonic-gate 	    (tokentable[tagnum].func != NOFUNC)) {
3057c478bd9Sstevel@tonic-gate 		retstat = open_tag(context, tagnum);
3067c478bd9Sstevel@tonic-gate 		if (!retstat)
3077c478bd9Sstevel@tonic-gate 			retstat = (*tokentable[tagnum].func)(context, status,
3087c478bd9Sstevel@tonic-gate 			    flag);
3097c478bd9Sstevel@tonic-gate 		if (!retstat)
3107c478bd9Sstevel@tonic-gate 			retstat = close_tag(context, tagnum);
3117c478bd9Sstevel@tonic-gate 		return (retstat);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	/* here if token id is not in table */
3147c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("praudit: No code associated with "
3157c478bd9Sstevel@tonic-gate 	    "tag id %d\n"), tagnum);
3167c478bd9Sstevel@tonic-gate 	return (0);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate void
3207c478bd9Sstevel@tonic-gate get_Hname(uint32_t addr, char *buf, size_t buflen)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	extern char	*inet_ntoa(const struct in_addr);
3237c478bd9Sstevel@tonic-gate 	struct hostent *phe;
3247c478bd9Sstevel@tonic-gate 	struct in_addr ia;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	phe = gethostbyaddr((const char *)&addr, 4, AF_INET);
3277c478bd9Sstevel@tonic-gate 	if (phe == (struct hostent *)0) {
3287c478bd9Sstevel@tonic-gate 		ia.s_addr = addr;
3297c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, buflen, "%s", inet_ntoa(ia));
3307c478bd9Sstevel@tonic-gate 		return;
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 	ia.s_addr = addr;
3337c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, buflen, "%s", phe->h_name);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate void
3377c478bd9Sstevel@tonic-gate get_Hname_ex(uint32_t *addr, char *buf, size_t buflen)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	struct hostent *phe;
3407c478bd9Sstevel@tonic-gate 	int err;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	phe = getipnodebyaddr((const void *)addr, 16, AF_INET6, &err);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (phe == (struct hostent *)0) {
3457c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *)addr, buf, buflen);
3467c478bd9Sstevel@tonic-gate 	} else
3477c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, buflen, "%s", phe->h_name);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (phe)
3507c478bd9Sstevel@tonic-gate 		freehostent(phe);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate int
3547c478bd9Sstevel@tonic-gate pa_hostname(pr_context_t *context, int status, int flag)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	int	returnstat;
3577c478bd9Sstevel@tonic-gate 	uint32_t	ip_addr;
3587c478bd9Sstevel@tonic-gate 	struct in_addr ia;
3597c478bd9Sstevel@tonic-gate 	uval_t uval;
3607c478bd9Sstevel@tonic-gate 	char	buf[256];
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (status <  0)
3637c478bd9Sstevel@tonic-gate 		return (status);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, (char *)&ip_addr, 4)) != 0)
3667c478bd9Sstevel@tonic-gate 		return (returnstat);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_RAWM)) {
3717c478bd9Sstevel@tonic-gate 		uval.string_val = buf;
3727c478bd9Sstevel@tonic-gate 		get_Hname(ip_addr, buf, sizeof (buf));
3737c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
3747c478bd9Sstevel@tonic-gate 	} else {
3757c478bd9Sstevel@tonic-gate 		ia.s_addr = ip_addr;
3767c478bd9Sstevel@tonic-gate 		if ((uval.string_val = inet_ntoa(ia)) == NULL)
3777c478bd9Sstevel@tonic-gate 			return (-1);
3787c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	return (returnstat);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate int
3847c478bd9Sstevel@tonic-gate pa_hostname_ex(pr_context_t *context, int status, int flag)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	int	returnstat;
3877c478bd9Sstevel@tonic-gate 	uint32_t	ip_type;
3887c478bd9Sstevel@tonic-gate 	uint32_t	ip_addr[4];
3897c478bd9Sstevel@tonic-gate 	struct in_addr ia;
3907c478bd9Sstevel@tonic-gate 	char buf[256];
3917c478bd9Sstevel@tonic-gate 	uval_t uval;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if (status <  0)
3947c478bd9Sstevel@tonic-gate 		return (status);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/* get ip type */
3977c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_int32(context, (int32_t *)&ip_type, 1)) != 0)
3987c478bd9Sstevel@tonic-gate 		return (returnstat);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/* only IPv4 and IPv6 addresses are legal */
4017c478bd9Sstevel@tonic-gate 	if ((ip_type != AU_IPv4) && (ip_type != AU_IPv6))
4027c478bd9Sstevel@tonic-gate 		return (-1);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	/* get ip address */
4057c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, (char *)ip_addr, ip_type)) != 0)
4067c478bd9Sstevel@tonic-gate 			return (returnstat);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_HOSTID)) != 0)
4097c478bd9Sstevel@tonic-gate 		return (returnstat);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
4127c478bd9Sstevel@tonic-gate 	if (ip_type == AU_IPv4) {		/* ipv4 address */
4137c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
4147c478bd9Sstevel@tonic-gate 			uval.string_val = buf;
4157c478bd9Sstevel@tonic-gate 			get_Hname(ip_addr[0], buf, sizeof (buf));
4167c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
4177c478bd9Sstevel@tonic-gate 		} else {
4187c478bd9Sstevel@tonic-gate 			ia.s_addr = ip_addr[0];
4197c478bd9Sstevel@tonic-gate 			if ((uval.string_val = inet_ntoa(ia)) == NULL)
4207c478bd9Sstevel@tonic-gate 				return (-1);
4217c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 	} else if (ip_type == AU_IPv6) {	/* IPv6 addresss (128 bits) */
4247c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
4257c478bd9Sstevel@tonic-gate 			uval.string_val = buf;
4267c478bd9Sstevel@tonic-gate 			get_Hname_ex(ip_addr, buf, sizeof (buf));
4277c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
4287c478bd9Sstevel@tonic-gate 		} else {
4297c478bd9Sstevel@tonic-gate 			uval.string_val = (char *)buf;
4307c478bd9Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, (void *)ip_addr, buf,
4317c478bd9Sstevel@tonic-gate 			    sizeof (buf));
4327c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (returnstat != 0)
4377c478bd9Sstevel@tonic-gate 		return (returnstat);
4387c478bd9Sstevel@tonic-gate 	return (close_tag(context, TAG_HOSTID));
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate int
4427c478bd9Sstevel@tonic-gate pa_hostname_so(pr_context_t *context, int status, int flag)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	int		returnstat;
4457c478bd9Sstevel@tonic-gate 	short		ip_type;
4467c478bd9Sstevel@tonic-gate 	ushort_t	ip_port;
4477c478bd9Sstevel@tonic-gate 	uint32_t	ip_addr[4];
4487c478bd9Sstevel@tonic-gate 	struct in_addr ia;
4497c478bd9Sstevel@tonic-gate 	char buf[256];
4507c478bd9Sstevel@tonic-gate 	uval_t uval;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	if (status <  0)
4537c478bd9Sstevel@tonic-gate 		return (status);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/* get ip type */
4567c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_short(context, &ip_type, 1)) != 0)
4577c478bd9Sstevel@tonic-gate 		return (returnstat);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/* only IPv4 and IPv6 addresses are legal */
4607c478bd9Sstevel@tonic-gate 	if ((ip_type != AU_IPv4) && (ip_type != AU_IPv6))
4617c478bd9Sstevel@tonic-gate 		return (-1);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/* get local ip port */
4647c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_short(context, &ip_port, 1)) != 0)
4657c478bd9Sstevel@tonic-gate 		return (returnstat);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_SOCKEXLPORT)) != 0)
4687c478bd9Sstevel@tonic-gate 		return (returnstat);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
4717c478bd9Sstevel@tonic-gate 	uval.string_val = hexconvert((char *)&ip_port, sizeof (ip_port),
4727c478bd9Sstevel@tonic-gate 	    sizeof (ip_port));
4737c478bd9Sstevel@tonic-gate 	if (uval.string_val) {
4747c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, 0);
4757c478bd9Sstevel@tonic-gate 		free(uval.string_val);
4767c478bd9Sstevel@tonic-gate 	} else
4777c478bd9Sstevel@tonic-gate 		returnstat = -1;
4787c478bd9Sstevel@tonic-gate 	if (returnstat)
4797c478bd9Sstevel@tonic-gate 		return (returnstat);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if ((returnstat = close_tag(context, TAG_SOCKEXLPORT)) != 0)
4827c478bd9Sstevel@tonic-gate 		return (returnstat);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/* get local ip address */
4857c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, (char *)ip_addr, ip_type)) != 0)
4867c478bd9Sstevel@tonic-gate 			return (returnstat);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_SOCKEXLADDR)) != 0)
4897c478bd9Sstevel@tonic-gate 		return (returnstat);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if (ip_type == AU_IPv4) {		/* ipv4 address */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
4947c478bd9Sstevel@tonic-gate 			uval.string_val = buf;
4957c478bd9Sstevel@tonic-gate 			get_Hname(ip_addr[0], buf, sizeof (buf));
4967c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, 0);
4977c478bd9Sstevel@tonic-gate 		} else {
4987c478bd9Sstevel@tonic-gate 			ia.s_addr = ip_addr[0];
4997c478bd9Sstevel@tonic-gate 			if ((uval.string_val = inet_ntoa(ia)) == NULL)
5007c478bd9Sstevel@tonic-gate 				return (-1);
5017c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, 0);
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	} else if (ip_type == AU_IPv6) {	/* IPv6 addresss (128 bits) */
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
5077c478bd9Sstevel@tonic-gate 			uval.string_val = buf;
5087c478bd9Sstevel@tonic-gate 			get_Hname_ex(ip_addr, buf, sizeof (buf));
5097c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, 0);
5107c478bd9Sstevel@tonic-gate 		} else {
5117c478bd9Sstevel@tonic-gate 			uval.string_val = (char *)buf;
5127c478bd9Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, (void *)ip_addr, buf,
5137c478bd9Sstevel@tonic-gate 			    sizeof (buf));
5147c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, 0);
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 	} else
5177c478bd9Sstevel@tonic-gate 		returnstat = -1;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if (returnstat)
5207c478bd9Sstevel@tonic-gate 		return (returnstat);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if ((returnstat = close_tag(context, TAG_SOCKEXLADDR)) != 0)
5237c478bd9Sstevel@tonic-gate 		return (returnstat);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/* get foreign ip port */
5267c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_short(context, &ip_port, 1)) != 0)
5277c478bd9Sstevel@tonic-gate 		return (returnstat);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_SOCKEXFPORT)) != 0)
5307c478bd9Sstevel@tonic-gate 		return (returnstat);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	uval.string_val = hexconvert((char *)&ip_port, sizeof (ip_port),
5337c478bd9Sstevel@tonic-gate 	    sizeof (ip_port));
5347c478bd9Sstevel@tonic-gate 	if (uval.string_val) {
5357c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, 0);
5367c478bd9Sstevel@tonic-gate 		free(uval.string_val);
5377c478bd9Sstevel@tonic-gate 	} else
5387c478bd9Sstevel@tonic-gate 		returnstat = -1;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	if (returnstat)
5417c478bd9Sstevel@tonic-gate 		return (returnstat);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if ((returnstat = close_tag(context, TAG_SOCKEXFPORT)) != 0)
5447c478bd9Sstevel@tonic-gate 		return (returnstat);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	/* get foreign ip address */
5477c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, (char *)ip_addr, ip_type)) != 0)
5487c478bd9Sstevel@tonic-gate 			return (returnstat);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_SOCKEXFADDR)) != 0)
5517c478bd9Sstevel@tonic-gate 		return (returnstat);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (ip_type == AU_IPv4) {		/* ipv4 address */
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
5567c478bd9Sstevel@tonic-gate 			uval.string_val = buf;
5577c478bd9Sstevel@tonic-gate 			get_Hname(ip_addr[0], buf, sizeof (buf));
5587c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
5597c478bd9Sstevel@tonic-gate 		} else {
5607c478bd9Sstevel@tonic-gate 			ia.s_addr = ip_addr[0];
5617c478bd9Sstevel@tonic-gate 			if ((uval.string_val = inet_ntoa(ia)) == NULL)
5627c478bd9Sstevel@tonic-gate 				return (-1);
5637c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	} else if (ip_type == AU_IPv6) {	/* IPv6 addresss (128 bits) */
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
5697c478bd9Sstevel@tonic-gate 			uval.string_val = buf;
5707c478bd9Sstevel@tonic-gate 			get_Hname_ex(ip_addr, buf, sizeof (buf));
5717c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
5727c478bd9Sstevel@tonic-gate 		} else {
5737c478bd9Sstevel@tonic-gate 			uval.string_val = (char *)buf;
5747c478bd9Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, (void *)ip_addr, buf,
5757c478bd9Sstevel@tonic-gate 			    sizeof (buf));
5767c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 	} else
5797c478bd9Sstevel@tonic-gate 		returnstat = -1;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if (returnstat)
5827c478bd9Sstevel@tonic-gate 		return (returnstat);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if ((returnstat = close_tag(context, TAG_SOCKEXFADDR)) != 0)
5857c478bd9Sstevel@tonic-gate 		return (returnstat);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	return (returnstat);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate #define	NBITSMAJOR64	32	/* # of major device bits in 64-bit Solaris */
5927c478bd9Sstevel@tonic-gate #define	NBITSMINOR64	32	/* # of minor device bits in 64-bit Solaris */
5937c478bd9Sstevel@tonic-gate #define	MAXMAJ64	0xfffffffful	/* max major value */
5947c478bd9Sstevel@tonic-gate #define	MAXMIN64	0xfffffffful	/* max minor value */
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate #define	NBITSMAJOR32	14	/* # of SVR4 major device bits */
5977c478bd9Sstevel@tonic-gate #define	NBITSMINOR32	18	/* # of SVR4 minor device bits */
5987c478bd9Sstevel@tonic-gate #define	NMAXMAJ32	0x3fff	/* SVR4 max major value */
5997c478bd9Sstevel@tonic-gate #define	NMAXMIN32	0x3ffff	/* MAX minor for 3b2 software drivers. */
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate static int32_t
6037c478bd9Sstevel@tonic-gate minor_64(uint64_t dev)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	if (dev == NODEV) {
6067c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6077c478bd9Sstevel@tonic-gate 		return (NODEV);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	return (int32_t)(dev & MAXMIN64);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate static int32_t
6137c478bd9Sstevel@tonic-gate major_64(uint64_t dev)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	uint32_t maj;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	maj = (uint32_t)(dev >> NBITSMINOR64);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (dev == NODEV || maj > MAXMAJ64) {
6207c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6217c478bd9Sstevel@tonic-gate 		return (NODEV);
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 	return (int32_t)(maj);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate static int32_t
6277c478bd9Sstevel@tonic-gate minor_32(uint32_t dev)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	if (dev == NODEV) {
6307c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6317c478bd9Sstevel@tonic-gate 		return (NODEV);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 	return (int32_t)(dev & MAXMIN32);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate static int32_t
6377c478bd9Sstevel@tonic-gate major_32(uint32_t dev)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	uint32_t maj;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	maj = (uint32_t)(dev >> NBITSMINOR32);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	if (dev == NODEV || maj > MAXMAJ32) {
6447c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6457c478bd9Sstevel@tonic-gate 		return (NODEV);
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate 	return (int32_t)(maj);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
6537c478bd9Sstevel@tonic-gate  * pa_tid() 	: Process terminal id and display contents
6547c478bd9Sstevel@tonic-gate  * return codes	: -1 - error
6557c478bd9Sstevel@tonic-gate  *		:  0 - successful
6567c478bd9Sstevel@tonic-gate  *
6577c478bd9Sstevel@tonic-gate  *	terminal id port		adr_int32
6587c478bd9Sstevel@tonic-gate  *	terminal id machine		adr_int32
6597c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
6607c478bd9Sstevel@tonic-gate  */
6617c478bd9Sstevel@tonic-gate int
6627c478bd9Sstevel@tonic-gate pa_tid32(pr_context_t *context, int status, int flag)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	int	returnstat;
6657c478bd9Sstevel@tonic-gate 	int32_t dev_maj_min;
6667c478bd9Sstevel@tonic-gate 	uint32_t	ip_addr;
6677c478bd9Sstevel@tonic-gate 	struct in_addr ia;
6687c478bd9Sstevel@tonic-gate 	char	*ipstring;
6697c478bd9Sstevel@tonic-gate 	char	buf[256];
6707c478bd9Sstevel@tonic-gate 	uval_t	uval;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if (status <  0)
6737c478bd9Sstevel@tonic-gate 		return (status);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_int32(context, &dev_maj_min, 1)) != 0)
6767c478bd9Sstevel@tonic-gate 		return (returnstat);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, (char *)&ip_addr, 4)) != 0)
6797c478bd9Sstevel@tonic-gate 		return (returnstat);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
6827c478bd9Sstevel@tonic-gate 	uval.string_val = buf;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_RAWM)) {
6857c478bd9Sstevel@tonic-gate 		char	hostname[256];
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		get_Hname(ip_addr, hostname, sizeof (hostname));
6887c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%d %d %s",
6897c478bd9Sstevel@tonic-gate 		    major_32(dev_maj_min),
6907c478bd9Sstevel@tonic-gate 		    minor_32(dev_maj_min),
6917c478bd9Sstevel@tonic-gate 		    hostname);
6927c478bd9Sstevel@tonic-gate 		return (pa_print(context, &uval, flag));
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	ia.s_addr = ip_addr;
6967c478bd9Sstevel@tonic-gate 	if ((ipstring = inet_ntoa(ia)) == NULL)
6977c478bd9Sstevel@tonic-gate 		return (-1);
6987c478bd9Sstevel@tonic-gate 
6998f9294f3Sgww 	(void) snprintf(buf, sizeof (buf), "%d %d %s", major_32(dev_maj_min),
7007c478bd9Sstevel@tonic-gate 	    minor_32(dev_maj_min),
7017c478bd9Sstevel@tonic-gate 	    ipstring);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	return (pa_print(context, &uval, flag));
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate int
7077c478bd9Sstevel@tonic-gate pa_tid32_ex(pr_context_t *context, int status, int flag)
7087c478bd9Sstevel@tonic-gate {
7097c478bd9Sstevel@tonic-gate 	int		returnstat;
7107c478bd9Sstevel@tonic-gate 	int32_t		dev_maj_min;
7117c478bd9Sstevel@tonic-gate 	uint32_t	ip_addr[16];
7127c478bd9Sstevel@tonic-gate 	uint32_t	ip_type;
7137c478bd9Sstevel@tonic-gate 	struct in_addr	ia;
7147c478bd9Sstevel@tonic-gate 	char		*ipstring;
7157c478bd9Sstevel@tonic-gate 	char		hostname[256];
7167c478bd9Sstevel@tonic-gate 	char		buf[256];
7177c478bd9Sstevel@tonic-gate 	char		tbuf[256];
7187c478bd9Sstevel@tonic-gate 	uval_t		uval;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if (status <  0)
7217c478bd9Sstevel@tonic-gate 		return (status);
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/* get port info */
7247c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_int32(context, &dev_maj_min, 1)) != 0)
7257c478bd9Sstevel@tonic-gate 		return (returnstat);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	/* get address type */
7287c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int32(context, &ip_type, 1)) != 0)
7297c478bd9Sstevel@tonic-gate 		return (returnstat);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	/* legal address types are either AU_IPv4 or AU_IPv6 only */
7327c478bd9Sstevel@tonic-gate 	if ((ip_type != AU_IPv4) && (ip_type != AU_IPv6))
7337c478bd9Sstevel@tonic-gate 		return (-1);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	/* get address (4/16) */
7367c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, (char *)ip_addr, ip_type)) != 0)
7377c478bd9Sstevel@tonic-gate 		return (returnstat);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
7407c478bd9Sstevel@tonic-gate 	if (ip_type == AU_IPv4) {
7417c478bd9Sstevel@tonic-gate 		uval.string_val = buf;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
7447c478bd9Sstevel@tonic-gate 			get_Hname(ip_addr[0], hostname, sizeof (hostname));
7457c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%d %d %s",
7468f9294f3Sgww 			    major_32(dev_maj_min), minor_32(dev_maj_min),
7477c478bd9Sstevel@tonic-gate 			    hostname);
7487c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
7497c478bd9Sstevel@tonic-gate 		}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		ia.s_addr = ip_addr[0];
7527c478bd9Sstevel@tonic-gate 		if ((ipstring = inet_ntoa(ia)) == NULL)
7537c478bd9Sstevel@tonic-gate 			return (-1);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%d %d %s",
7568f9294f3Sgww 		    major_32(dev_maj_min), minor_32(dev_maj_min), ipstring);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		return (pa_print(context, &uval, flag));
7597c478bd9Sstevel@tonic-gate 	} else {
7607c478bd9Sstevel@tonic-gate 		uval.string_val = buf;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
7637c478bd9Sstevel@tonic-gate 			get_Hname_ex(ip_addr, hostname, sizeof (hostname));
7647c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%d %d %s",
7658f9294f3Sgww 			    major_32(dev_maj_min), minor_32(dev_maj_min),
7667c478bd9Sstevel@tonic-gate 			    hostname);
7677c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *) ip_addr, tbuf,
7717c478bd9Sstevel@tonic-gate 		    sizeof (tbuf));
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%d %d %s",
7748f9294f3Sgww 		    major_32(dev_maj_min), minor_32(dev_maj_min), tbuf);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		return (pa_print(context, &uval, flag));
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate int
7817c478bd9Sstevel@tonic-gate pa_ip_addr(pr_context_t *context, int status, int flag)
7827c478bd9Sstevel@tonic-gate {
7837c478bd9Sstevel@tonic-gate 	int		returnstat;
7847c478bd9Sstevel@tonic-gate 	uval_t		uval;
7857c478bd9Sstevel@tonic-gate 	uint32_t	ip_addr[4];
7867c478bd9Sstevel@tonic-gate 	uint32_t	ip_type;
7877c478bd9Sstevel@tonic-gate 	struct in_addr	ia;
7887c478bd9Sstevel@tonic-gate 	char		*ipstring;
7897c478bd9Sstevel@tonic-gate 	char		hostname[256];
7907c478bd9Sstevel@tonic-gate 	char		buf[256];
7917c478bd9Sstevel@tonic-gate 	char		tbuf[256];
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if (status <  0)
7947c478bd9Sstevel@tonic-gate 		return (status);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	/* get address type */
7977c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int32(context, &ip_type, 1)) != 0)
7987c478bd9Sstevel@tonic-gate 		return (returnstat);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	/* legal address type is AU_IPv4 or AU_IPv6 */
8017c478bd9Sstevel@tonic-gate 	if ((ip_type != AU_IPv4) && (ip_type != AU_IPv6))
8027c478bd9Sstevel@tonic-gate 		return (-1);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/* get address (4/16) */
8057c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, (char *)ip_addr, ip_type)) != 0)
8067c478bd9Sstevel@tonic-gate 		return (returnstat);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
8097c478bd9Sstevel@tonic-gate 	if (ip_type == AU_IPv4) {
8107c478bd9Sstevel@tonic-gate 		uval.string_val = buf;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
8137c478bd9Sstevel@tonic-gate 			get_Hname(ip_addr[0], hostname, sizeof (hostname));
8148f9294f3Sgww 			(void) snprintf(buf, sizeof (buf), "%s", hostname);
8157c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 		ia.s_addr = ip_addr[0];
8197c478bd9Sstevel@tonic-gate 		if ((ipstring = inet_ntoa(ia)) == NULL)
8207c478bd9Sstevel@tonic-gate 			return (-1);
8217c478bd9Sstevel@tonic-gate 
8228f9294f3Sgww 		(void) snprintf(buf, sizeof (buf), "%s", ipstring);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 		return (pa_print(context, &uval, flag));
8257c478bd9Sstevel@tonic-gate 	} else {
8267c478bd9Sstevel@tonic-gate 		uval.string_val = buf;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
8297c478bd9Sstevel@tonic-gate 			get_Hname_ex(ip_addr, hostname, sizeof (hostname));
8307c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s",
8317c478bd9Sstevel@tonic-gate 			    hostname);
8327c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *) ip_addr, tbuf,
8367c478bd9Sstevel@tonic-gate 		    sizeof (tbuf));
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s", tbuf);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 		return (pa_print(context, &uval, flag));
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate int
8467c478bd9Sstevel@tonic-gate pa_tid64(pr_context_t *context, int status, int flag)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	int	returnstat;
8497c478bd9Sstevel@tonic-gate 	int64_t dev_maj_min;
8507c478bd9Sstevel@tonic-gate 	uint32_t	ip_addr;
8517c478bd9Sstevel@tonic-gate 	struct in_addr ia;
8527c478bd9Sstevel@tonic-gate 	char	*ipstring;
8537c478bd9Sstevel@tonic-gate 	char	buf[256];
8547c478bd9Sstevel@tonic-gate 	uval_t	uval;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	if (status <  0)
8577c478bd9Sstevel@tonic-gate 		return (status);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_int64(context, &dev_maj_min, 1)) != 0)
8607c478bd9Sstevel@tonic-gate 		return (returnstat);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, (char *)&ip_addr, 4)) != 0)
8637c478bd9Sstevel@tonic-gate 		return (returnstat);
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
8667c478bd9Sstevel@tonic-gate 	uval.string_val = buf;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_RAWM)) {
8697c478bd9Sstevel@tonic-gate 		char	hostname[256];
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		get_Hname(ip_addr, hostname, sizeof (hostname));
8727c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%d %d %s",
8738f9294f3Sgww 		    major_64(dev_maj_min), minor_64(dev_maj_min), hostname);
8747c478bd9Sstevel@tonic-gate 		return (pa_print(context, &uval, flag));
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	ia.s_addr = ip_addr;
8787c478bd9Sstevel@tonic-gate 	if ((ipstring = inet_ntoa(ia)) == NULL)
8797c478bd9Sstevel@tonic-gate 		return (-1);
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%d %d %s",
8828f9294f3Sgww 	    major_64(dev_maj_min), minor_64(dev_maj_min), ipstring);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	return (pa_print(context, &uval, flag));
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate int
8887c478bd9Sstevel@tonic-gate pa_tid64_ex(pr_context_t *context, int status, int flag)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	int		returnstat;
8917c478bd9Sstevel@tonic-gate 	int64_t		dev_maj_min;
8927c478bd9Sstevel@tonic-gate 	uint32_t	ip_addr[4];
8937c478bd9Sstevel@tonic-gate 	uint32_t	ip_type;
8947c478bd9Sstevel@tonic-gate 	struct in_addr	ia;
8957c478bd9Sstevel@tonic-gate 	char		*ipstring;
8967c478bd9Sstevel@tonic-gate 	char		hostname[256];
8977c478bd9Sstevel@tonic-gate 	char		buf[256];
8987c478bd9Sstevel@tonic-gate 	char		tbuf[256];
8997c478bd9Sstevel@tonic-gate 	uval_t		uval;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	if (status <  0)
9027c478bd9Sstevel@tonic-gate 		return (status);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/* get port info */
9057c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_int64(context, &dev_maj_min, 1)) != 0)
9067c478bd9Sstevel@tonic-gate 		return (returnstat);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/* get address type */
9097c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int32(context, &ip_type, 1)) != 0)
9107c478bd9Sstevel@tonic-gate 		return (returnstat);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	/* legal address types are either AU_IPv4 or AU_IPv6 only */
9137c478bd9Sstevel@tonic-gate 	if ((ip_type != AU_IPv4) && (ip_type != AU_IPv6))
9147c478bd9Sstevel@tonic-gate 		return (-1);
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	/* get address (4/16) */
9177c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, (char *)&ip_addr, ip_type)) != 0)
9187c478bd9Sstevel@tonic-gate 		return (returnstat);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
9217c478bd9Sstevel@tonic-gate 	if (ip_type == AU_IPv4) {
9227c478bd9Sstevel@tonic-gate 		uval.string_val = buf;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
9257c478bd9Sstevel@tonic-gate 			get_Hname(ip_addr[0], hostname, sizeof (hostname));
9267c478bd9Sstevel@tonic-gate 			uval.string_val = buf;
9277c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%d %d %s",
9288f9294f3Sgww 			    major_64(dev_maj_min), minor_64(dev_maj_min),
9297c478bd9Sstevel@tonic-gate 			    hostname);
9307c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
9317c478bd9Sstevel@tonic-gate 		}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		ia.s_addr = ip_addr[0];
9347c478bd9Sstevel@tonic-gate 		if ((ipstring = inet_ntoa(ia)) == NULL)
9357c478bd9Sstevel@tonic-gate 			return (-1);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%d %d %s",
9388f9294f3Sgww 		    major_64(dev_maj_min), minor_64(dev_maj_min), ipstring);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 		return (pa_print(context, &uval, flag));
9417c478bd9Sstevel@tonic-gate 	} else {
9427c478bd9Sstevel@tonic-gate 		uval.string_val = buf;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
9457c478bd9Sstevel@tonic-gate 			get_Hname_ex(ip_addr, hostname, sizeof (hostname));
9467c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%d %d %s",
9478f9294f3Sgww 			    major_64(dev_maj_min), minor_64(dev_maj_min),
9487c478bd9Sstevel@tonic-gate 			    hostname);
9497c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
9507c478bd9Sstevel@tonic-gate 		}
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *)ip_addr, tbuf,
9537c478bd9Sstevel@tonic-gate 		    sizeof (tbuf));
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%d %d %s",
9568f9294f3Sgww 		    major_64(dev_maj_min), minor_64(dev_maj_min), tbuf);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		return (pa_print(context, &uval, flag));
9597c478bd9Sstevel@tonic-gate 	}
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate /*
9647c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------------
9657c478bd9Sstevel@tonic-gate  * findfieldwidth:
9667c478bd9Sstevel@tonic-gate  * Returns the field width based on the basic unit and print mode.
9677c478bd9Sstevel@tonic-gate  * This routine is called to determine the field width for the
9687c478bd9Sstevel@tonic-gate  * data items in the arbitrary data token where the tokens are
9697c478bd9Sstevel@tonic-gate  * to be printed in more than one line.  The field width can be
9707c478bd9Sstevel@tonic-gate  * found in the fwidth structure.
9717c478bd9Sstevel@tonic-gate  *
9727c478bd9Sstevel@tonic-gate  * Input parameters:
9737c478bd9Sstevel@tonic-gate  * basicunit	Can be one of AUR_CHAR, AUR_BYTE, AUR_SHORT,
9747c478bd9Sstevel@tonic-gate  *		AUR_INT32, or AUR_INT64
9757c478bd9Sstevel@tonic-gate  * howtoprint	Print mode. Can be one of AUP_BINARY, AUP_OCTAL,
9767c478bd9Sstevel@tonic-gate  *		AUP_DECIMAL, or AUP_HEX.
9777c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------------
9787c478bd9Sstevel@tonic-gate  */
9797c478bd9Sstevel@tonic-gate int
9807c478bd9Sstevel@tonic-gate findfieldwidth(char basicunit, char howtoprint)
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate 	int	i, j;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	for (i = 0; i < numwidthentries; i++) {
9857c478bd9Sstevel@tonic-gate 		if (fwidth[i].basic_unit == basicunit) {
9867c478bd9Sstevel@tonic-gate 			for (j = 0; j <= 4; j++) {
9877c478bd9Sstevel@tonic-gate 				if (fwidth[i].pwidth[j].print_base ==
9888f9294f3Sgww 				    howtoprint) {
9898f9294f3Sgww 					return (
9908f9294f3Sgww 					    fwidth[i].pwidth[j].field_width);
9918f9294f3Sgww 				}
9927c478bd9Sstevel@tonic-gate 			}
9937c478bd9Sstevel@tonic-gate 			/*
9947c478bd9Sstevel@tonic-gate 			 * if we got here, then we didn't get what we were after
9957c478bd9Sstevel@tonic-gate 			 */
9967c478bd9Sstevel@tonic-gate 			return (0);
9977c478bd9Sstevel@tonic-gate 		}
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 	/* if we got here, we didn't get what we wanted either */
10007c478bd9Sstevel@tonic-gate 	return (0);
10017c478bd9Sstevel@tonic-gate }
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate /*
10057c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
10067c478bd9Sstevel@tonic-gate  * pa_cmd: Retrieves the cmd item from the input stream.
10077c478bd9Sstevel@tonic-gate  * return codes : -1 - error
10087c478bd9Sstevel@tonic-gate  *		:  0 - successful
10097c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
10107c478bd9Sstevel@tonic-gate  */
10117c478bd9Sstevel@tonic-gate int
10127c478bd9Sstevel@tonic-gate pa_cmd(pr_context_t *context, int status, int flag)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	char	*cmd;  /* cmd */
10157c478bd9Sstevel@tonic-gate 	short	length;
10167c478bd9Sstevel@tonic-gate 	int	returnstat;
10177c478bd9Sstevel@tonic-gate 	uval_t	uval;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/*
10207c478bd9Sstevel@tonic-gate 	 * We need to know how much space to allocate for our string, so
10217c478bd9Sstevel@tonic-gate 	 * read the length first, then call pr_adr_char to read those bytes.
10227c478bd9Sstevel@tonic-gate 	 */
10237c478bd9Sstevel@tonic-gate 	if (status >= 0) {
10247c478bd9Sstevel@tonic-gate 		if (pr_adr_short(context, &length, 1) == 0) {
10257c478bd9Sstevel@tonic-gate 			if ((cmd = (char *)malloc(length + 1)) == NULL)
10267c478bd9Sstevel@tonic-gate 				return (-1);
10277c478bd9Sstevel@tonic-gate 			if (pr_adr_char(context, cmd, length) == 0) {
10287c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_STRING;
10297c478bd9Sstevel@tonic-gate 				uval.string_val = cmd;
10307c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
10317c478bd9Sstevel@tonic-gate 			} else {
10327c478bd9Sstevel@tonic-gate 				returnstat = -1;
10337c478bd9Sstevel@tonic-gate 			}
10347c478bd9Sstevel@tonic-gate 			free(cmd);
10357c478bd9Sstevel@tonic-gate 			return (returnstat);
10367c478bd9Sstevel@tonic-gate 		} else
10377c478bd9Sstevel@tonic-gate 			return (-1);
10387c478bd9Sstevel@tonic-gate 	} else
10397c478bd9Sstevel@tonic-gate 		return (status);
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate /*
10457c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
10467c478bd9Sstevel@tonic-gate  * pa_adr_byte	: Issues pr_adr_char to retrieve the next ADR item from
10477c478bd9Sstevel@tonic-gate  *		  the input stream pointed to by audit_adr, and prints it
10487c478bd9Sstevel@tonic-gate  *		  as an integer if status >= 0
10497c478bd9Sstevel@tonic-gate  * return codes : -1 - error
10507c478bd9Sstevel@tonic-gate  *		:  0 - successful
10517c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
10527c478bd9Sstevel@tonic-gate  */
10537c478bd9Sstevel@tonic-gate int
10547c478bd9Sstevel@tonic-gate pa_adr_byte(pr_context_t *context, int status, int flag)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	char	c;
10577c478bd9Sstevel@tonic-gate 	uval_t	uval;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	if (status >= 0) {
10607c478bd9Sstevel@tonic-gate 		if (pr_adr_char(context, &c, 1) == 0) {
10617c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_BYTE;
10627c478bd9Sstevel@tonic-gate 			uval.char_val = c;
10637c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
10647c478bd9Sstevel@tonic-gate 		} else
10657c478bd9Sstevel@tonic-gate 			return (-1);
10667c478bd9Sstevel@tonic-gate 	} else
10677c478bd9Sstevel@tonic-gate 		return (status);
10687c478bd9Sstevel@tonic-gate }
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate /*
10717c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
10727c478bd9Sstevel@tonic-gate  * pa_adr_charhex: Issues pr_adr_char to retrieve the next ADR item from
10737c478bd9Sstevel@tonic-gate  *			the input stream pointed to by audit_adr, and prints it
10747c478bd9Sstevel@tonic-gate  *			in hexadecimal if status >= 0
10757c478bd9Sstevel@tonic-gate  * return codes  : -1 - error
10767c478bd9Sstevel@tonic-gate  *		 :  0 - successful
10777c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
10787c478bd9Sstevel@tonic-gate  */
10797c478bd9Sstevel@tonic-gate int
10807c478bd9Sstevel@tonic-gate pa_adr_charhex(pr_context_t *context, int status, int flag)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate 	char	p[2];
10837c478bd9Sstevel@tonic-gate 	int	returnstat;
10847c478bd9Sstevel@tonic-gate 	uval_t	uval;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if (status >= 0) {
10877c478bd9Sstevel@tonic-gate 		p[0] = p[1] = 0;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 		if ((returnstat = pr_adr_char(context, p, 1)) == 0) {
10907c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_STRING;
10917c478bd9Sstevel@tonic-gate 			uval.string_val = hexconvert(p, sizeof (char),
10927c478bd9Sstevel@tonic-gate 			    sizeof (char));
10937c478bd9Sstevel@tonic-gate 			if (uval.string_val) {
10947c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
10957c478bd9Sstevel@tonic-gate 				free(uval.string_val);
10967c478bd9Sstevel@tonic-gate 			}
10977c478bd9Sstevel@tonic-gate 		}
10987c478bd9Sstevel@tonic-gate 		return (returnstat);
10997c478bd9Sstevel@tonic-gate 	} else
11007c478bd9Sstevel@tonic-gate 		return (status);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate /*
11047c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
11057c478bd9Sstevel@tonic-gate  * pa_adr_int32	: Issues pr_adr_int32 to retrieve the next ADR item from the
11067c478bd9Sstevel@tonic-gate  *		  input stream pointed to by audit_adr, and prints it
11077c478bd9Sstevel@tonic-gate  *		  if status >= 0
11087c478bd9Sstevel@tonic-gate  * return codes : -1 - error
11097c478bd9Sstevel@tonic-gate  *		:  0 - successful
11107c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
11117c478bd9Sstevel@tonic-gate  */
11127c478bd9Sstevel@tonic-gate int
11137c478bd9Sstevel@tonic-gate pa_adr_int32(pr_context_t *context, int status, int flag)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	int32_t	c;
11167c478bd9Sstevel@tonic-gate 	uval_t	uval;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	if (status >= 0) {
11197c478bd9Sstevel@tonic-gate 		if (pr_adr_int32(context, &c, 1) == 0) {
11207c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_INT32;
11217c478bd9Sstevel@tonic-gate 			uval.int32_val = c;
11227c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
11237c478bd9Sstevel@tonic-gate 		} else
11247c478bd9Sstevel@tonic-gate 			return (-1);
11257c478bd9Sstevel@tonic-gate 	} else
11267c478bd9Sstevel@tonic-gate 		return (status);
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate /*
11337c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
11347c478bd9Sstevel@tonic-gate  * pa_adr_int64	: Issues pr_adr_int64 to retrieve the next ADR item from the
11357c478bd9Sstevel@tonic-gate  *		  input stream pointed to by audit_adr, and prints it
11367c478bd9Sstevel@tonic-gate  *		  if status >= 0
11377c478bd9Sstevel@tonic-gate  * return codes : -1 - error
11387c478bd9Sstevel@tonic-gate  *		:  0 - successful
11397c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
11407c478bd9Sstevel@tonic-gate  */
11417c478bd9Sstevel@tonic-gate int
11427c478bd9Sstevel@tonic-gate pa_adr_int64(pr_context_t *context, int status, int flag)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	int64_t	c;
11457c478bd9Sstevel@tonic-gate 	uval_t	uval;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	if (status >= 0) {
11487c478bd9Sstevel@tonic-gate 		if (pr_adr_int64(context, &c, 1) == 0) {
11497c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_INT64;
11507c478bd9Sstevel@tonic-gate 			uval.int64_val = c;
11517c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
11527c478bd9Sstevel@tonic-gate 		} else
11537c478bd9Sstevel@tonic-gate 			return (-1);
11547c478bd9Sstevel@tonic-gate 	} else
11557c478bd9Sstevel@tonic-gate 		return (status);
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate /*
11597c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
11607c478bd9Sstevel@tonic-gate  * pa_adr_int64hex: Issues pr_adr_int64 to retrieve the next ADR item from the
11617c478bd9Sstevel@tonic-gate  *			input stream pointed to by audit_adr, and prints it
11627c478bd9Sstevel@tonic-gate  *			in hexadecimal if status >= 0
11637c478bd9Sstevel@tonic-gate  * return codes  : -1 - error
11647c478bd9Sstevel@tonic-gate  *		:  0 - successful
11657c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
11667c478bd9Sstevel@tonic-gate  */
11677c478bd9Sstevel@tonic-gate int
11687c478bd9Sstevel@tonic-gate pa_adr_int32hex(pr_context_t *context, int status, int flag)
11697c478bd9Sstevel@tonic-gate {
11707c478bd9Sstevel@tonic-gate 	int32_t	l;
11717c478bd9Sstevel@tonic-gate 	int	returnstat;
11727c478bd9Sstevel@tonic-gate 	uval_t	uval;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	if (status >= 0) {
11757c478bd9Sstevel@tonic-gate 		if ((returnstat = pr_adr_int32(context, &l, 1)) == 0) {
11767c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_HEX32;
11777c478bd9Sstevel@tonic-gate 			uval.int32_val = l;
11787c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
11797c478bd9Sstevel@tonic-gate 		}
11807c478bd9Sstevel@tonic-gate 		return (returnstat);
11817c478bd9Sstevel@tonic-gate 	} else
11827c478bd9Sstevel@tonic-gate 		return (status);
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate /*
11867c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
11877c478bd9Sstevel@tonic-gate  * pa_adr_int64hex: Issues pr_adr_int64 to retrieve the next ADR item from the
11887c478bd9Sstevel@tonic-gate  *			input stream pointed to by audit_adr, and prints it
11897c478bd9Sstevel@tonic-gate  *			in hexadecimal if status >= 0
11907c478bd9Sstevel@tonic-gate  * return codes  : -1 - error
11917c478bd9Sstevel@tonic-gate  *		:  0 - successful
11927c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
11937c478bd9Sstevel@tonic-gate  */
11947c478bd9Sstevel@tonic-gate int
11957c478bd9Sstevel@tonic-gate pa_adr_int64hex(pr_context_t *context, int status, int flag)
11967c478bd9Sstevel@tonic-gate {
11977c478bd9Sstevel@tonic-gate 	int64_t	l;
11987c478bd9Sstevel@tonic-gate 	int	returnstat;
11997c478bd9Sstevel@tonic-gate 	uval_t	uval;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	if (status >= 0) {
12027c478bd9Sstevel@tonic-gate 		if ((returnstat = pr_adr_int64(context, &l, 1)) == 0) {
12037c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_HEX64;
12047c478bd9Sstevel@tonic-gate 			uval.int64_val = l;
12057c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
12067c478bd9Sstevel@tonic-gate 		}
12077c478bd9Sstevel@tonic-gate 		return (returnstat);
12087c478bd9Sstevel@tonic-gate 	} else
12097c478bd9Sstevel@tonic-gate 		return (status);
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate /*
12147c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
12157c478bd9Sstevel@tonic-gate  * bu2string: Maps a print basic unit type to a string.
12167c478bd9Sstevel@tonic-gate  * returns  : The string mapping or "unknown basic unit type".
12177c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
12187c478bd9Sstevel@tonic-gate  */
12197c478bd9Sstevel@tonic-gate char *
12207c478bd9Sstevel@tonic-gate bu2string(char basic_unit)
12217c478bd9Sstevel@tonic-gate {
12227c478bd9Sstevel@tonic-gate 	register int	i;
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	struct bu_map_ent {
12257c478bd9Sstevel@tonic-gate 		char	basic_unit;
12267c478bd9Sstevel@tonic-gate 		char	*string;
12277c478bd9Sstevel@tonic-gate 	};
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	/*
12307c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
12317c478bd9Sstevel@tonic-gate 	 * These names are data units when displaying the arbitrary data
12327c478bd9Sstevel@tonic-gate 	 * token.
12337c478bd9Sstevel@tonic-gate 	 */
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	static struct bu_map_ent bu_map[] = {
12367c478bd9Sstevel@tonic-gate 				{ AUR_BYTE, "byte" },
12377c478bd9Sstevel@tonic-gate 				{ AUR_CHAR, "char" },
12387c478bd9Sstevel@tonic-gate 				{ AUR_SHORT, "short" },
12397c478bd9Sstevel@tonic-gate 				{ AUR_INT32, "int32" },
12407c478bd9Sstevel@tonic-gate 				{ AUR_INT64, "int64" } 	};
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (bu_map) / sizeof (struct bu_map_ent); i++)
12437c478bd9Sstevel@tonic-gate 		if (basic_unit == bu_map[i].basic_unit)
12447c478bd9Sstevel@tonic-gate 			return (gettext(bu_map[i].string));
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	return (gettext("unknown basic unit type"));
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate /*
12517c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
12527c478bd9Sstevel@tonic-gate  * eventmodifier2string: Maps event modifier flags to a readable string.
12537c478bd9Sstevel@tonic-gate  * returns: The string mapping or "none".
12547c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
12557c478bd9Sstevel@tonic-gate  */
12567c478bd9Sstevel@tonic-gate static void
12577c478bd9Sstevel@tonic-gate eventmodifier2string(ushort_t emodifier, char *modstring, size_t modlen)
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate 	register int	i, j;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	struct em_map_ent {
12627c478bd9Sstevel@tonic-gate 		int	mask;
12637c478bd9Sstevel@tonic-gate 		char	*string;
12647c478bd9Sstevel@tonic-gate 	};
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	/*
12677c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
12687c478bd9Sstevel@tonic-gate 	 * These abbreviations represent the event modifier field of the
12697c478bd9Sstevel@tonic-gate 	 * header token.  To gain a better understanding of each modifier,
12708f9294f3Sgww 	 * read
12718f9294f3Sgww 	 * System Administration Guide: Security Services >> Solaris Auditing
12728f9294f3Sgww 	 * at http://docs.sun.com.
12737c478bd9Sstevel@tonic-gate 	 */
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	static struct em_map_ent em_map[] = {
12767c478bd9Sstevel@tonic-gate 		{ (int)PAD_READ,	"rd" },	/* data read from object */
12777c478bd9Sstevel@tonic-gate 		{ (int)PAD_WRITE,	"wr" },	/* data written to object */
12787c478bd9Sstevel@tonic-gate 		{ (int)PAD_SPRIVUSE,	"sp" },	/* successfully used priv */
12797c478bd9Sstevel@tonic-gate 		{ (int)PAD_FPRIVUSE,	"fp" },	/* failed use of priv */
12807c478bd9Sstevel@tonic-gate 		{ (int)PAD_NONATTR,	"na" },	/* non-attributable event */
12817c478bd9Sstevel@tonic-gate 		{ (int)PAD_FAILURE,	"fe" }	/* fail audit event */
12827c478bd9Sstevel@tonic-gate 	};
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	modstring[0] = '\0';
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < sizeof (em_map) / sizeof (struct em_map_ent);
12877c478bd9Sstevel@tonic-gate 	    i++) {
12887c478bd9Sstevel@tonic-gate 		if ((int)emodifier & em_map[i].mask) {
12897c478bd9Sstevel@tonic-gate 			if (j++)
12907c478bd9Sstevel@tonic-gate 				(void) strlcat(modstring, ":", modlen);
12917c478bd9Sstevel@tonic-gate 			(void) strlcat(modstring, em_map[i].string, modlen);
12927c478bd9Sstevel@tonic-gate 		}
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate }
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate /*
12987c478bd9Sstevel@tonic-gate  * ---------------------------------------------------------
12997c478bd9Sstevel@tonic-gate  * convert_char_to_string:
13007c478bd9Sstevel@tonic-gate  *   Converts a byte to string depending on the print mode
13017c478bd9Sstevel@tonic-gate  * input	: printmode, which may be one of AUP_BINARY,
13027c478bd9Sstevel@tonic-gate  *		  AUP_OCTAL, AUP_DECIMAL, and AUP_HEX
13037c478bd9Sstevel@tonic-gate  *		  c, which is the byte to convert
13047c478bd9Sstevel@tonic-gate  * output	: p, which is a pointer to the location where
13057c478bd9Sstevel@tonic-gate  *		  the resulting string is to be stored
13067c478bd9Sstevel@tonic-gate  *  ----------------------------------------------------------
13077c478bd9Sstevel@tonic-gate  */
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate int
13107c478bd9Sstevel@tonic-gate convert_char_to_string(char printmode, char c, char *p)
13117c478bd9Sstevel@tonic-gate {
13127c478bd9Sstevel@tonic-gate 	union {
13137c478bd9Sstevel@tonic-gate 		char	c1[4];
13147c478bd9Sstevel@tonic-gate 		int	c2;
13157c478bd9Sstevel@tonic-gate 	} dat;
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	dat.c2 = 0;
13187c478bd9Sstevel@tonic-gate 	dat.c1[3] = c;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if (printmode == AUP_BINARY)
13217c478bd9Sstevel@tonic-gate 		(void) convertbinary(p, &c, sizeof (char));
13227c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_OCTAL)
13237c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "%o", (int)dat.c2);
13247c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_DECIMAL)
13257c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "%d", c);
13267c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_HEX)
13277c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "0x%x", (int)dat.c2);
13287c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_STRING)
13297c478bd9Sstevel@tonic-gate 		convertascii(p, &c, sizeof (char));
13307c478bd9Sstevel@tonic-gate 	return (0);
13317c478bd9Sstevel@tonic-gate }
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate /*
13347c478bd9Sstevel@tonic-gate  * --------------------------------------------------------------
13357c478bd9Sstevel@tonic-gate  * convert_short_to_string:
13367c478bd9Sstevel@tonic-gate  * Converts a short integer to string depending on the print mode
13377c478bd9Sstevel@tonic-gate  * input	: printmode, which may be one of AUP_BINARY,
13387c478bd9Sstevel@tonic-gate  *		AUP_OCTAL, AUP_DECIMAL, and AUP_HEX
13397c478bd9Sstevel@tonic-gate  *		c, which is the short integer to convert
13407c478bd9Sstevel@tonic-gate  * output	: p, which is a pointer to the location where
13417c478bd9Sstevel@tonic-gate  *		the resulting string is to be stored
13427c478bd9Sstevel@tonic-gate  * ---------------------------------------------------------------
13437c478bd9Sstevel@tonic-gate  */
13447c478bd9Sstevel@tonic-gate int
13457c478bd9Sstevel@tonic-gate convert_short_to_string(char printmode, short c, char *p)
13467c478bd9Sstevel@tonic-gate {
13477c478bd9Sstevel@tonic-gate 	union {
13487c478bd9Sstevel@tonic-gate 		short	c1[2];
13497c478bd9Sstevel@tonic-gate 		int	c2;
13507c478bd9Sstevel@tonic-gate 	} dat;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	dat.c2 = 0;
13537c478bd9Sstevel@tonic-gate 	dat.c1[1] = c;
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	if (printmode == AUP_BINARY)
13567c478bd9Sstevel@tonic-gate 		(void) convertbinary(p, (char *)&c, sizeof (short));
13577c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_OCTAL)
13587c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "%o", (int)dat.c2);
13597c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_DECIMAL)
13607c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "%hd", c);
13617c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_HEX)
13627c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "0x%x", (int)dat.c2);
13637c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_STRING)
13647c478bd9Sstevel@tonic-gate 		convertascii(p, (char *)&c, sizeof (short));
13657c478bd9Sstevel@tonic-gate 	return (0);
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate /*
13697c478bd9Sstevel@tonic-gate  * ---------------------------------------------------------
13707c478bd9Sstevel@tonic-gate  * convert_int32_to_string:
13717c478bd9Sstevel@tonic-gate  * Converts a integer to string depending on the print mode
13727c478bd9Sstevel@tonic-gate  * input	: printmode, which may be one of AUP_BINARY,
13737c478bd9Sstevel@tonic-gate  *		AUP_OCTAL, AUP_DECIMAL, and AUP_HEX
13747c478bd9Sstevel@tonic-gate  *		c, which is the integer to convert
13757c478bd9Sstevel@tonic-gate  * output	: p, which is a pointer to the location where
13767c478bd9Sstevel@tonic-gate  *		the resulting string is to be stored
13777c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------
13787c478bd9Sstevel@tonic-gate  */
13797c478bd9Sstevel@tonic-gate int
13807c478bd9Sstevel@tonic-gate convert_int32_to_string(char printmode, int32_t c, char *p)
13817c478bd9Sstevel@tonic-gate {
13827c478bd9Sstevel@tonic-gate 	if (printmode == AUP_BINARY)
13837c478bd9Sstevel@tonic-gate 		(void) convertbinary(p, (char *)&c, sizeof (int32_t));
13847c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_OCTAL)
13857c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "%o", c);
13867c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_DECIMAL)
13877c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "%d", c);
13887c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_HEX)
13897c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "0x%x", c);
13907c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_STRING)
13917c478bd9Sstevel@tonic-gate 		convertascii(p, (char *)&c, sizeof (int));
13927c478bd9Sstevel@tonic-gate 	return (0);
13937c478bd9Sstevel@tonic-gate }
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate /*
13967c478bd9Sstevel@tonic-gate  * ---------------------------------------------------------
13977c478bd9Sstevel@tonic-gate  * convert_int64_to_string:
13987c478bd9Sstevel@tonic-gate  * Converts a integer to string depending on the print mode
13997c478bd9Sstevel@tonic-gate  * input	: printmode, which may be one of AUP_BINARY,
14007c478bd9Sstevel@tonic-gate  *		AUP_OCTAL, AUP_DECIMAL, and AUP_HEX
14017c478bd9Sstevel@tonic-gate  *		c, which is the integer to convert
14027c478bd9Sstevel@tonic-gate  * output	: p, which is a pointer to the location where
14037c478bd9Sstevel@tonic-gate  *		the resulting string is to be stored
14047c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------
14057c478bd9Sstevel@tonic-gate  */
14067c478bd9Sstevel@tonic-gate int
14077c478bd9Sstevel@tonic-gate convert_int64_to_string(char printmode, int64_t c, char *p)
14087c478bd9Sstevel@tonic-gate {
14097c478bd9Sstevel@tonic-gate 	if (printmode == AUP_BINARY)
14107c478bd9Sstevel@tonic-gate 		(void) convertbinary(p, (char *)&c, sizeof (int64_t));
14117c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_OCTAL)
14127c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "%"PRIo64, c);
14137c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_DECIMAL)
14147c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "%"PRId64, c);
14157c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_HEX)
14167c478bd9Sstevel@tonic-gate 		(void) sprintf(p, "0x%"PRIx64, c);
14177c478bd9Sstevel@tonic-gate 	else if (printmode == AUP_STRING)
14187c478bd9Sstevel@tonic-gate 		convertascii(p, (char *)&c, sizeof (int64_t));
14197c478bd9Sstevel@tonic-gate 	return (0);
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate /*
14247c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------
14257c478bd9Sstevel@tonic-gate  * convertbinary:
14267c478bd9Sstevel@tonic-gate  * Converts a unit c of 'size' bytes long into a binary string
14277c478bd9Sstevel@tonic-gate  * and returns it into the position pointed to by p
14287c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------
14297c478bd9Sstevel@tonic-gate  */
14307c478bd9Sstevel@tonic-gate int
14317c478bd9Sstevel@tonic-gate convertbinary(char *p, char *c, int size)
14327c478bd9Sstevel@tonic-gate {
14337c478bd9Sstevel@tonic-gate 	char	*s, *t, *ss;
14347c478bd9Sstevel@tonic-gate 	int	i, j;
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	if ((s = (char *)malloc(8 * size + 1)) == NULL)
14377c478bd9Sstevel@tonic-gate 		return (0);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	ss = s;
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	/* first convert to binary */
14427c478bd9Sstevel@tonic-gate 	t = s;
14437c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
14447c478bd9Sstevel@tonic-gate 		for (j = 0; j < 8; j++)
14457c478bd9Sstevel@tonic-gate 			(void) sprintf(t++, "%d", ((*c >> (7 - j)) & (0x01)));
14467c478bd9Sstevel@tonic-gate 		c++;
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate 	*t = '\0';
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	/* now string leading zero's if any */
14517c478bd9Sstevel@tonic-gate 	j = strlen(s) - 1;
14527c478bd9Sstevel@tonic-gate 	for (i = 0; i < j; i++) {
14537c478bd9Sstevel@tonic-gate 		if (*s != '0')
14547c478bd9Sstevel@tonic-gate 			break;
14557c478bd9Sstevel@tonic-gate 			else
14567c478bd9Sstevel@tonic-gate 			s++;
14577c478bd9Sstevel@tonic-gate 	}
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	/* now copy the contents of s to p */
14607c478bd9Sstevel@tonic-gate 	t = p;
14617c478bd9Sstevel@tonic-gate 	for (i = 0; i < (8 * size + 1); i++) {
14627c478bd9Sstevel@tonic-gate 		if (*s == '\0') {
14637c478bd9Sstevel@tonic-gate 			*t = '\0';
14647c478bd9Sstevel@tonic-gate 			break;
14657c478bd9Sstevel@tonic-gate 		}
14667c478bd9Sstevel@tonic-gate 		*t++ = *s++;
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate 	free(ss);
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	return (1);
14717c478bd9Sstevel@tonic-gate }
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate static char hex[] = "0123456789abcdef";
14757c478bd9Sstevel@tonic-gate /*
14767c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
14777c478bd9Sstevel@tonic-gate  * hexconvert	: Converts a string of (size) bytes to hexadecimal, and
14787c478bd9Sstevel@tonic-gate  *		returns the hexadecimal string.
14797c478bd9Sstevel@tonic-gate  * returns	: - NULL if memory cannot be allocated for the string, or
14807c478bd9Sstevel@tonic-gate  *		- pointer to the hexadecimal string if successful
14817c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
14827c478bd9Sstevel@tonic-gate  */
14837c478bd9Sstevel@tonic-gate char *
14847c478bd9Sstevel@tonic-gate hexconvert(char *c, int size, int chunk)
14857c478bd9Sstevel@tonic-gate {
14867c478bd9Sstevel@tonic-gate 	register char	*s, *t;
14877c478bd9Sstevel@tonic-gate 	register int	i, j, k;
14887c478bd9Sstevel@tonic-gate 	int	numchunks;
14897c478bd9Sstevel@tonic-gate 	int	leftovers;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	if (size <= 0)
14927c478bd9Sstevel@tonic-gate 		return (NULL);
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	if ((s = (char *)malloc((size * 5) + 1)) == NULL)
14957c478bd9Sstevel@tonic-gate 		return (NULL);
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	if (chunk > size || chunk <= 0)
14987c478bd9Sstevel@tonic-gate 		chunk = size;
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	numchunks = size / chunk;
15017c478bd9Sstevel@tonic-gate 	leftovers = size % chunk;
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	t = s;
15047c478bd9Sstevel@tonic-gate 	for (i = j = 0; i < numchunks; i++) {
15057c478bd9Sstevel@tonic-gate 		if (j++) {
15067c478bd9Sstevel@tonic-gate 			*t++ = ' ';
15077c478bd9Sstevel@tonic-gate 		}
15087c478bd9Sstevel@tonic-gate 		*t++ = '0';
15097c478bd9Sstevel@tonic-gate 		*t++ = 'x';
15107c478bd9Sstevel@tonic-gate 		for (k = 0; k < chunk; k++) {
15117c478bd9Sstevel@tonic-gate 			*t++ = hex[(uint_t)((uchar_t)*c >> 4)];
15127c478bd9Sstevel@tonic-gate 			*t++ = hex[(uint_t)((uchar_t)*c & 0xF)];
15137c478bd9Sstevel@tonic-gate 			c++;
15147c478bd9Sstevel@tonic-gate 		}
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	if (leftovers) {
15187c478bd9Sstevel@tonic-gate 		*t++ = ' ';
15197c478bd9Sstevel@tonic-gate 		*t++ = '0';
15207c478bd9Sstevel@tonic-gate 		*t++ = 'x';
15217c478bd9Sstevel@tonic-gate 		for (i = 0; i < leftovers; i++) {
15227c478bd9Sstevel@tonic-gate 			*t++ = hex[(uint_t)((uchar_t)*c >> 4)];
15237c478bd9Sstevel@tonic-gate 			*t++ = hex[(uint_t)((uchar_t)*c & 0xF)];
15247c478bd9Sstevel@tonic-gate 			c++;
15257c478bd9Sstevel@tonic-gate 		}
15267c478bd9Sstevel@tonic-gate 	}
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	*t = '\0';
15297c478bd9Sstevel@tonic-gate 	return (s);
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate /*
15347c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
15357c478bd9Sstevel@tonic-gate  * htp2string: Maps a print suggestion to a string.
15367c478bd9Sstevel@tonic-gate  * returns   : The string mapping or "unknown print suggestion".
15377c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
15387c478bd9Sstevel@tonic-gate  */
15397c478bd9Sstevel@tonic-gate char *
15407c478bd9Sstevel@tonic-gate htp2string(char print_sugg)
15417c478bd9Sstevel@tonic-gate {
15427c478bd9Sstevel@tonic-gate 	register int	i;
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	struct htp_map_ent {
15457c478bd9Sstevel@tonic-gate 		char	print_sugg;
15467c478bd9Sstevel@tonic-gate 		char	*print_string;
15477c478bd9Sstevel@tonic-gate 	};
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	/*
15507c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
15517c478bd9Sstevel@tonic-gate 	 * These names are data types when displaying the arbitrary data
15527c478bd9Sstevel@tonic-gate 	 * token.
15537c478bd9Sstevel@tonic-gate 	 */
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	static struct htp_map_ent htp_map[] = {
15567c478bd9Sstevel@tonic-gate 				{ AUP_BINARY, "binary" },
15577c478bd9Sstevel@tonic-gate 				{ AUP_OCTAL, "octal" },
15587c478bd9Sstevel@tonic-gate 				{ AUP_DECIMAL, "decimal" },
15597c478bd9Sstevel@tonic-gate 				{ AUP_HEX, "hexadecimal" },
15607c478bd9Sstevel@tonic-gate 				{ AUP_STRING, "string" } 	};
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (htp_map) / sizeof (struct htp_map_ent); i++)
15637c478bd9Sstevel@tonic-gate 		if (print_sugg == htp_map[i].print_sugg)
15647c478bd9Sstevel@tonic-gate 			return (gettext(htp_map[i].print_string));
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	return (gettext("unknown print suggestion"));
15677c478bd9Sstevel@tonic-gate }
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate /*
15707c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------------------
15717c478bd9Sstevel@tonic-gate  * pa_adr_short: Issues pr_adr_short to retrieve the next ADR item from the
15727c478bd9Sstevel@tonic-gate  *		input stream pointed to by audit_adr, and prints it
15737c478bd9Sstevel@tonic-gate  *		if status >= 0
15747c478bd9Sstevel@tonic-gate  * return codes: -1 - error
15757c478bd9Sstevel@tonic-gate  *		:  0 - successful
15767c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------------------
15777c478bd9Sstevel@tonic-gate  */
15787c478bd9Sstevel@tonic-gate int
15797c478bd9Sstevel@tonic-gate pa_adr_short(pr_context_t *context, int status, int flag)
15807c478bd9Sstevel@tonic-gate {
15817c478bd9Sstevel@tonic-gate 	short	c;
15827c478bd9Sstevel@tonic-gate 	uval_t	uval;
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	if (status >= 0) {
15857c478bd9Sstevel@tonic-gate 		if (pr_adr_short(context, &c, 1) == 0) {
15867c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_SHORT;
15877c478bd9Sstevel@tonic-gate 			uval.short_val = c;
15887c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
15897c478bd9Sstevel@tonic-gate 		} else
15907c478bd9Sstevel@tonic-gate 			return (-1);
15917c478bd9Sstevel@tonic-gate 	} else
15927c478bd9Sstevel@tonic-gate 		return (status);
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate /*
15967c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
15977c478bd9Sstevel@tonic-gate  * pa_adr_shorthex: Issues pr_adr_short to retrieve the next ADR item from the
15987c478bd9Sstevel@tonic-gate  *			input stream pointed to by audit_adr, and prints it
15997c478bd9Sstevel@tonic-gate  *			in hexadecimal if status >= 0
16007c478bd9Sstevel@tonic-gate  * return codes  : -1 - error
16017c478bd9Sstevel@tonic-gate  *		:  0 - successful
16027c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
16037c478bd9Sstevel@tonic-gate  */
16047c478bd9Sstevel@tonic-gate int
16057c478bd9Sstevel@tonic-gate pa_adr_shorthex(pr_context_t *context, int status, int flag)
16067c478bd9Sstevel@tonic-gate {
16077c478bd9Sstevel@tonic-gate 	short	s;
16087c478bd9Sstevel@tonic-gate 	int	returnstat;
16097c478bd9Sstevel@tonic-gate 	uval_t	uval;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	if (status >= 0) {
16127c478bd9Sstevel@tonic-gate 		if ((returnstat = pr_adr_short(context, &s, 1)) == 0) {
16137c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_STRING;
16147c478bd9Sstevel@tonic-gate 			uval.string_val = hexconvert((char *)&s, sizeof (s),
16157c478bd9Sstevel@tonic-gate 			    sizeof (s));
16167c478bd9Sstevel@tonic-gate 			if (uval.string_val) {
16177c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
16187c478bd9Sstevel@tonic-gate 				free(uval.string_val);
16197c478bd9Sstevel@tonic-gate 			}
16207c478bd9Sstevel@tonic-gate 		}
16217c478bd9Sstevel@tonic-gate 		return (returnstat);
16227c478bd9Sstevel@tonic-gate 	} else
16237c478bd9Sstevel@tonic-gate 		return (status);
16247c478bd9Sstevel@tonic-gate }
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate /*
16287c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
16297c478bd9Sstevel@tonic-gate  * pa_adr_string: Retrieves a string from the input stream and prints it
16307c478bd9Sstevel@tonic-gate  *		  if status >= 0
16317c478bd9Sstevel@tonic-gate  * return codes : -1 - error
16327c478bd9Sstevel@tonic-gate  *		:  0 - successful
16337c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
16347c478bd9Sstevel@tonic-gate  */
16357c478bd9Sstevel@tonic-gate int
16367c478bd9Sstevel@tonic-gate pa_adr_string(pr_context_t *context, int status, int flag)
16377c478bd9Sstevel@tonic-gate {
16387c478bd9Sstevel@tonic-gate 	char	*c;
16397c478bd9Sstevel@tonic-gate 	short	length;
16407c478bd9Sstevel@tonic-gate 	int	returnstat;
16417c478bd9Sstevel@tonic-gate 	uval_t	uval;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	/*
16447c478bd9Sstevel@tonic-gate 	 * We need to know how much space to allocate for our string, so
16457c478bd9Sstevel@tonic-gate 	 * read the length first, then call pr_adr_char to read those bytes.
16467c478bd9Sstevel@tonic-gate 	 */
16477c478bd9Sstevel@tonic-gate 	if (status < 0)
16487c478bd9Sstevel@tonic-gate 		return (status);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_short(context, &length, 1)) != 0)
16517c478bd9Sstevel@tonic-gate 		return (returnstat);
16527c478bd9Sstevel@tonic-gate 	if ((c = (char *)malloc(length + 1)) == NULL)
16537c478bd9Sstevel@tonic-gate 		return (-1);
16547c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, c, length)) != 0) {
16557c478bd9Sstevel@tonic-gate 		free(c);
16567c478bd9Sstevel@tonic-gate 		return (returnstat);
16577c478bd9Sstevel@tonic-gate 	}
165885298dc1Snakanon 
16597c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
166085298dc1Snakanon 	uval.string_val = c;
16617c478bd9Sstevel@tonic-gate 	returnstat = pa_print(context, &uval, flag);
16627c478bd9Sstevel@tonic-gate 	free(c);
16637c478bd9Sstevel@tonic-gate 	return (returnstat);
16647c478bd9Sstevel@tonic-gate }
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate /*
16677c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
16687c478bd9Sstevel@tonic-gate  * pa_file_string: Retrieves a file string from the input stream and prints it
16697c478bd9Sstevel@tonic-gate  *		  if status >= 0
16707c478bd9Sstevel@tonic-gate  * return codes : -1 - error
16717c478bd9Sstevel@tonic-gate  *		:  0 - successful
16727c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
16737c478bd9Sstevel@tonic-gate  */
16747c478bd9Sstevel@tonic-gate int
16757c478bd9Sstevel@tonic-gate pa_file_string(pr_context_t *context, int status, int flag)
16767c478bd9Sstevel@tonic-gate {
16777c478bd9Sstevel@tonic-gate 	char	*c;
16787c478bd9Sstevel@tonic-gate 	char	*p;
16797c478bd9Sstevel@tonic-gate 	short	length;
16807c478bd9Sstevel@tonic-gate 	int	returnstat;
16817c478bd9Sstevel@tonic-gate 	uval_t	uval;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	/*
16847c478bd9Sstevel@tonic-gate 	 * We need to know how much space to allocate for our string, so
16857c478bd9Sstevel@tonic-gate 	 * read the length first, then call pr_adr_char to read those bytes.
16867c478bd9Sstevel@tonic-gate 	 */
16877c478bd9Sstevel@tonic-gate 	if (status < 0)
16887c478bd9Sstevel@tonic-gate 		return (status);
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_short(context, &length, 1)) != 0)
16917c478bd9Sstevel@tonic-gate 		return (returnstat);
16927c478bd9Sstevel@tonic-gate 	if ((c = (char *)malloc(length + 1)) == NULL)
16937c478bd9Sstevel@tonic-gate 		return (-1);
169485298dc1Snakanon 	if ((p = (char *)malloc((length * 4) + 1)) == NULL) {
16957c478bd9Sstevel@tonic-gate 		free(c);
16967c478bd9Sstevel@tonic-gate 		return (-1);
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, c, length)) != 0) {
16997c478bd9Sstevel@tonic-gate 		free(c);
17007c478bd9Sstevel@tonic-gate 		free(p);
17017c478bd9Sstevel@tonic-gate 		return (returnstat);
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	if (is_file_token(context->tokenid))
17057c478bd9Sstevel@tonic-gate 		context->audit_rec_len += length;
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	convertascii(p, c, length - 1);
17087c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
17097c478bd9Sstevel@tonic-gate 	uval.string_val = p;
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	if (returnstat == 0)
17127c478bd9Sstevel@tonic-gate 		returnstat = finish_open_tag(context);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	if (returnstat == 0)
17157c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	free(c);
17187c478bd9Sstevel@tonic-gate 	free(p);
17197c478bd9Sstevel@tonic-gate 	return (returnstat);
17207c478bd9Sstevel@tonic-gate }
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate static int
17237c478bd9Sstevel@tonic-gate pa_putstr_xml(pr_context_t *context, int printable, char *str, size_t len)
17247c478bd9Sstevel@tonic-gate {
17257c478bd9Sstevel@tonic-gate 	int	err;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	if (!printable) {
17287c478bd9Sstevel@tonic-gate 		/*
17297c478bd9Sstevel@tonic-gate 		 * Unprintable chars should always be converted to the
17307c478bd9Sstevel@tonic-gate 		 * visible form. If there are unprintable characters which
17317c478bd9Sstevel@tonic-gate 		 * require special treatment in xml, those should be
17327c478bd9Sstevel@tonic-gate 		 * handled here.
17337c478bd9Sstevel@tonic-gate 		 */
17347c478bd9Sstevel@tonic-gate 		do {
17357c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "\\%03o",
17367c478bd9Sstevel@tonic-gate 			    (unsigned char)*str++);
17377c478bd9Sstevel@tonic-gate 		} while (err == 0 && --len != 0);
17387c478bd9Sstevel@tonic-gate 		return (err);
17397c478bd9Sstevel@tonic-gate 	}
17407c478bd9Sstevel@tonic-gate 	/* printable characters */
17417c478bd9Sstevel@tonic-gate 	if (len == 1) {
17427c478bd9Sstevel@tonic-gate 		/*
17437c478bd9Sstevel@tonic-gate 		 * check for the special chars only when char size was 1
17447c478bd9Sstevel@tonic-gate 		 * ie, ignore special chars appear in the middle of multibyte
17457c478bd9Sstevel@tonic-gate 		 * sequence.
17467c478bd9Sstevel@tonic-gate 		 */
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 		/* Escape for XML */
17497c478bd9Sstevel@tonic-gate 		switch (*str) {
17507c478bd9Sstevel@tonic-gate 		case '&':
17517c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "%s", "&amp;");
17527c478bd9Sstevel@tonic-gate 			break;
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 		case '<':
17557c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "%s", "&lt;");
17567c478bd9Sstevel@tonic-gate 			break;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 		case '>':
17597c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "%s", "&gt;");
17607c478bd9Sstevel@tonic-gate 			break;
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 		case '\"':
17637c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "%s", "&quot;");
17647c478bd9Sstevel@tonic-gate 			break;
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 		case '\'':
17677c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "%s", "&apos;");
17687c478bd9Sstevel@tonic-gate 			break;
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 		default:
17717c478bd9Sstevel@tonic-gate 			err = pr_putchar(context, *str);
17727c478bd9Sstevel@tonic-gate 			break;
17737c478bd9Sstevel@tonic-gate 		}
17747c478bd9Sstevel@tonic-gate 		return (err);
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 	do {
17777c478bd9Sstevel@tonic-gate 		err = pr_putchar(context, *str++);
17787c478bd9Sstevel@tonic-gate 	} while (err == 0 && --len != 0);
17797c478bd9Sstevel@tonic-gate 	return (err);
17807c478bd9Sstevel@tonic-gate }
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate static int
17837c478bd9Sstevel@tonic-gate pa_putstr(pr_context_t *context, int printable, char *str, size_t len)
17847c478bd9Sstevel@tonic-gate {
17857c478bd9Sstevel@tonic-gate 	int	err;
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	if (context->format & PRF_XMLM)
17887c478bd9Sstevel@tonic-gate 		return (pa_putstr_xml(context, printable, str, len));
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	if (!printable) {
17917c478bd9Sstevel@tonic-gate 		do {
17927c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "\\%03o",
17937c478bd9Sstevel@tonic-gate 			    (unsigned char)*str++);
17947c478bd9Sstevel@tonic-gate 		} while (err == 0 && --len != 0);
17957c478bd9Sstevel@tonic-gate 		return (err);
17967c478bd9Sstevel@tonic-gate 	}
17977c478bd9Sstevel@tonic-gate 	do {
17987c478bd9Sstevel@tonic-gate 		err = pr_putchar(context, *str++);
17997c478bd9Sstevel@tonic-gate 	} while (err == 0 && --len != 0);
18007c478bd9Sstevel@tonic-gate 	return (err);
18017c478bd9Sstevel@tonic-gate }
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate int
18047c478bd9Sstevel@tonic-gate pa_string(pr_context_t *context, int status, int flag)
18057c478bd9Sstevel@tonic-gate {
18067c478bd9Sstevel@tonic-gate 	int	rstat, wstat;
18077c478bd9Sstevel@tonic-gate 	int	i, printable, eos;
18087c478bd9Sstevel@tonic-gate 	int	mlen, rlen;
18097c478bd9Sstevel@tonic-gate 	int	mbmax = MB_CUR_MAX;
18107c478bd9Sstevel@tonic-gate 	wchar_t	wc;
18117c478bd9Sstevel@tonic-gate 	char	mbuf[MB_LEN_MAX + 1];
18127c478bd9Sstevel@tonic-gate 	char	c;
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	if (status < 0)
18157c478bd9Sstevel@tonic-gate 		return (status);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	rstat = wstat = 0;
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	if (mbmax == 1) {
18207c478bd9Sstevel@tonic-gate 		while (wstat == 0) {
18217c478bd9Sstevel@tonic-gate 			if ((rstat = pr_adr_char(context, &c, 1)) < 0)
18227c478bd9Sstevel@tonic-gate 				break;
18237c478bd9Sstevel@tonic-gate 			if (c == '\0')
18247c478bd9Sstevel@tonic-gate 				break;
18257c478bd9Sstevel@tonic-gate 			printable = isprint((unsigned char)c);
18267c478bd9Sstevel@tonic-gate 			wstat = pa_putstr(context, printable, &c, 1);
18277c478bd9Sstevel@tonic-gate 		}
18287c478bd9Sstevel@tonic-gate 		goto done;
18297c478bd9Sstevel@tonic-gate 	}
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	mlen = eos = 0;
18327c478bd9Sstevel@tonic-gate 	while (wstat == 0) {
18337c478bd9Sstevel@tonic-gate 		rlen = 0;
18347c478bd9Sstevel@tonic-gate 		do {
18357c478bd9Sstevel@tonic-gate 			if (!eos) {
18367c478bd9Sstevel@tonic-gate 				rstat = pr_adr_char(context, &c, 1);
18377c478bd9Sstevel@tonic-gate 				if (rstat != 0 || c == '\0')
18387c478bd9Sstevel@tonic-gate 					eos = 1;
18397c478bd9Sstevel@tonic-gate 				else
18407c478bd9Sstevel@tonic-gate 					mbuf[mlen++] = c;
18417c478bd9Sstevel@tonic-gate 			}
18427c478bd9Sstevel@tonic-gate 			rlen = mbtowc(&wc, mbuf, mlen);
18437c478bd9Sstevel@tonic-gate 		} while (!eos && mlen < mbmax && rlen <= 0);
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 		if (mlen == 0)
18467c478bd9Sstevel@tonic-gate 			break;	/* end of string */
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 		if (rlen <= 0) { /* no good sequence */
18497c478bd9Sstevel@tonic-gate 			rlen = 1;
18507c478bd9Sstevel@tonic-gate 			printable = 0;
18517c478bd9Sstevel@tonic-gate 		} else {
18527c478bd9Sstevel@tonic-gate 			printable = iswprint(wc);
18537c478bd9Sstevel@tonic-gate 		}
18547c478bd9Sstevel@tonic-gate 		wstat = pa_putstr(context, printable, mbuf, rlen);
18557c478bd9Sstevel@tonic-gate 		mlen -= rlen;
18567c478bd9Sstevel@tonic-gate 		if (mlen > 0) {
18577c478bd9Sstevel@tonic-gate 			for (i = 0; i < mlen; i++)
18587c478bd9Sstevel@tonic-gate 				mbuf[i] = mbuf[rlen + i];
18597c478bd9Sstevel@tonic-gate 		}
18607c478bd9Sstevel@tonic-gate 	}
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate done:
18637c478bd9Sstevel@tonic-gate 	if (wstat == 0)
18647c478bd9Sstevel@tonic-gate 		wstat = do_newline(context, flag);
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	if (wstat == 0 && context->data_mode == FILEMODE)
18677c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	return ((rstat != 0 || wstat != 0) ? -1 : 0);
18707c478bd9Sstevel@tonic-gate }
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate /*
18737c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
18747c478bd9Sstevel@tonic-gate  * pa_adr_u_int32: Issues pr_adr_u_int32 to retrieve the next ADR item from
18757c478bd9Sstevel@tonic-gate  *		  the input stream pointed to by audit_adr, and prints it
18767c478bd9Sstevel@tonic-gate  *		  if status = 0
18777c478bd9Sstevel@tonic-gate  * return codes : -1 - error
18787c478bd9Sstevel@tonic-gate  *		:  0 - successful
18797c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
18807c478bd9Sstevel@tonic-gate  */
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate int
18847c478bd9Sstevel@tonic-gate pa_adr_u_int32(pr_context_t *context, int status, int flag)
18857c478bd9Sstevel@tonic-gate {
18867c478bd9Sstevel@tonic-gate 	uint32_t c;
18877c478bd9Sstevel@tonic-gate 	uval_t	uval;
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	if (status >= 0) {
18907c478bd9Sstevel@tonic-gate 		if (pr_adr_u_int32(context, &c, 1) == 0) {
18917c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT32;
18927c478bd9Sstevel@tonic-gate 			uval.uint32_val = c;
18937c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
18947c478bd9Sstevel@tonic-gate 		} else
18957c478bd9Sstevel@tonic-gate 			return (-1);
18967c478bd9Sstevel@tonic-gate 	} else
18977c478bd9Sstevel@tonic-gate 		return (status);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate /*
19037c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19047c478bd9Sstevel@tonic-gate  * pa_adr_u_int64: Issues pr_adr_u_int64 to retrieve the next ADR item from the
19057c478bd9Sstevel@tonic-gate  *		  input stream pointed to by audit_adr, and prints it
19067c478bd9Sstevel@tonic-gate  *		  if status = 0
19077c478bd9Sstevel@tonic-gate  * return codes : -1 - error
19087c478bd9Sstevel@tonic-gate  *		:  0 - successful
19097c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19107c478bd9Sstevel@tonic-gate  */
19117c478bd9Sstevel@tonic-gate int
19127c478bd9Sstevel@tonic-gate pa_adr_u_int64(pr_context_t *context, int status, int flag)
19137c478bd9Sstevel@tonic-gate {
19147c478bd9Sstevel@tonic-gate 	uint64_t c;
19157c478bd9Sstevel@tonic-gate 	uval_t	uval;
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if (status >= 0) {
19187c478bd9Sstevel@tonic-gate 		if (pr_adr_u_int64(context, &c, 1) == 0) {
19197c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT64;
19207c478bd9Sstevel@tonic-gate 			uval.uint64_val = c;
19217c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
19227c478bd9Sstevel@tonic-gate 		} else
19237c478bd9Sstevel@tonic-gate 			return (-1);
19247c478bd9Sstevel@tonic-gate 	} else
19257c478bd9Sstevel@tonic-gate 		return (status);
19267c478bd9Sstevel@tonic-gate }
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate /*
19307c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19317c478bd9Sstevel@tonic-gate  * pa_adr_u_short: Issues pr_adr_u_short to retrieve the next ADR item from
19327c478bd9Sstevel@tonic-gate  *			the input stream pointed to by audit_adr, and prints it
19337c478bd9Sstevel@tonic-gate  *			if status = 0
19347c478bd9Sstevel@tonic-gate  * return codes : -1 - error
19357c478bd9Sstevel@tonic-gate  *		:  0 - successful
19367c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19377c478bd9Sstevel@tonic-gate  */
19387c478bd9Sstevel@tonic-gate int
19397c478bd9Sstevel@tonic-gate pa_adr_u_short(pr_context_t *context, int status, int flag)
19407c478bd9Sstevel@tonic-gate {
19417c478bd9Sstevel@tonic-gate 	ushort_t c;
19427c478bd9Sstevel@tonic-gate 	uval_t	uval;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	if (status >= 0) {
19457c478bd9Sstevel@tonic-gate 		if (pr_adr_u_short(context, &c, 1) == 0) {
19467c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_USHORT;
19477c478bd9Sstevel@tonic-gate 			uval.ushort_val = c;
19487c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
19497c478bd9Sstevel@tonic-gate 		} else
19507c478bd9Sstevel@tonic-gate 			return (-1);
19517c478bd9Sstevel@tonic-gate 	} else
19527c478bd9Sstevel@tonic-gate 		return (status);
19537c478bd9Sstevel@tonic-gate }
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate /*
19567c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19577c478bd9Sstevel@tonic-gate  * pa_reclen: Issues pr_adr_u_long to retrieve the length of the record
19587c478bd9Sstevel@tonic-gate  *		  from the input stream pointed to by audit_adr,
19597c478bd9Sstevel@tonic-gate  *		  and prints it (unless format is XML) if status = 0
19607c478bd9Sstevel@tonic-gate  * return codes : -1 - error
19617c478bd9Sstevel@tonic-gate  *		:  0 - successful
19627c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19637c478bd9Sstevel@tonic-gate  */
19647c478bd9Sstevel@tonic-gate int
19657c478bd9Sstevel@tonic-gate pa_reclen(pr_context_t *context, int status)
19667c478bd9Sstevel@tonic-gate {
19677c478bd9Sstevel@tonic-gate 	uint32_t c;
19687c478bd9Sstevel@tonic-gate 	uval_t	uval;
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	if (status >= 0) {
19717c478bd9Sstevel@tonic-gate 		if ((int)pr_adr_u_int32(context, &c, 1) == 0) {
19727c478bd9Sstevel@tonic-gate 			context->audit_rec_len = c;
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 			/* Don't print this for XML format */
19757c478bd9Sstevel@tonic-gate 			if (context->format & PRF_XMLM) {
19767c478bd9Sstevel@tonic-gate 				return (0);
19777c478bd9Sstevel@tonic-gate 			} else {
19787c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_UINT32;
19797c478bd9Sstevel@tonic-gate 				uval.uint32_val = c;
19807c478bd9Sstevel@tonic-gate 				return (pa_print(context, &uval, 0));
19817c478bd9Sstevel@tonic-gate 			}
19827c478bd9Sstevel@tonic-gate 		} else
19837c478bd9Sstevel@tonic-gate 			return (-1);
19847c478bd9Sstevel@tonic-gate 	} else
19857c478bd9Sstevel@tonic-gate 		return (status);
19867c478bd9Sstevel@tonic-gate }
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate /*
19897c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19907c478bd9Sstevel@tonic-gate  * pa_mode	: Issues pr_adr_u_short to retrieve the next ADR item from
19917c478bd9Sstevel@tonic-gate  *		the input stream pointed to by audit_adr, and prints it
19927c478bd9Sstevel@tonic-gate  *		in octal if status = 0
19937c478bd9Sstevel@tonic-gate  * return codes : -1 - error
19947c478bd9Sstevel@tonic-gate  *		:  0 - successful
19957c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19967c478bd9Sstevel@tonic-gate  */
19977c478bd9Sstevel@tonic-gate int
19987c478bd9Sstevel@tonic-gate pa_mode(pr_context_t *context, int status, int flag)
19997c478bd9Sstevel@tonic-gate {
20007c478bd9Sstevel@tonic-gate 	uint32_t c;
20017c478bd9Sstevel@tonic-gate 	uval_t	uval;
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	if (status >= 0) {
20047c478bd9Sstevel@tonic-gate 		if (pr_adr_u_int32(context, &c, 1) == 0) {
20057c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_LOCT;
20067c478bd9Sstevel@tonic-gate 			uval.uint32_val = c;
20077c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
20087c478bd9Sstevel@tonic-gate 		} else
20097c478bd9Sstevel@tonic-gate 			return (-1);
20107c478bd9Sstevel@tonic-gate 	} else
20117c478bd9Sstevel@tonic-gate 		return (status);
20127c478bd9Sstevel@tonic-gate }
20137c478bd9Sstevel@tonic-gate 
2014a7746f66Stz204579 static int
2015a7746f66Stz204579 pa_print_uid(pr_context_t *context, uid_t uid, int status, int flag)
20167c478bd9Sstevel@tonic-gate {
20177c478bd9Sstevel@tonic-gate 	int	returnstat;
20187c478bd9Sstevel@tonic-gate 	struct passwd *pw;
20197c478bd9Sstevel@tonic-gate 	uval_t	uval;
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	if (status < 0)
20227c478bd9Sstevel@tonic-gate 		return (status);
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_RAWM)) {
20257c478bd9Sstevel@tonic-gate 		/* get password file entry */
20267c478bd9Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) == NULL) {
20277c478bd9Sstevel@tonic-gate 			returnstat = 1;
20287c478bd9Sstevel@tonic-gate 		} else {
20297c478bd9Sstevel@tonic-gate 			/* print in ASCII form */
20307c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_STRING;
20317c478bd9Sstevel@tonic-gate 			uval.string_val = pw->pw_name;
20327c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
20337c478bd9Sstevel@tonic-gate 		}
20347c478bd9Sstevel@tonic-gate 	}
20357c478bd9Sstevel@tonic-gate 	/* print in integer form */
20367c478bd9Sstevel@tonic-gate 	if ((context->format & PRF_RAWM) || (returnstat == 1)) {
20377c478bd9Sstevel@tonic-gate 		uval.uvaltype = PRA_INT32;
20387c478bd9Sstevel@tonic-gate 		uval.int32_val = uid;
20397c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
20407c478bd9Sstevel@tonic-gate 	}
20417c478bd9Sstevel@tonic-gate 	return (returnstat);
20427c478bd9Sstevel@tonic-gate }
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate /*
20467c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
2047a7746f66Stz204579  * pa_pw_uid()	: Issues pr_adr_u_int32 to reads uid from input stream
20487c478bd9Sstevel@tonic-gate  *		pointed to by audit_adr, and displays it in either
20497c478bd9Sstevel@tonic-gate  *		raw form or its ASCII representation, if status >= 0.
20507c478bd9Sstevel@tonic-gate  * return codes : -1 - error
20517c478bd9Sstevel@tonic-gate  * 		:  1 - warning, passwd entry not found
20527c478bd9Sstevel@tonic-gate  *		:  0 - successful
20537c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
20547c478bd9Sstevel@tonic-gate  */
20557c478bd9Sstevel@tonic-gate int
2056a7746f66Stz204579 pa_pw_uid(pr_context_t *context, int status, int flag)
20577c478bd9Sstevel@tonic-gate {
2058a7746f66Stz204579 	uint32_t uid;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	if (status < 0)
20617c478bd9Sstevel@tonic-gate 		return (status);
20627c478bd9Sstevel@tonic-gate 
2063a7746f66Stz204579 	if (pr_adr_u_int32(context, &uid, 1) != 0)
2064a7746f66Stz204579 		/* cannot retrieve uid */
20657c478bd9Sstevel@tonic-gate 		return (-1);
20667c478bd9Sstevel@tonic-gate 
2067a7746f66Stz204579 	return (pa_print_uid(context, uid, status, flag));
2068a7746f66Stz204579 }
2069a7746f66Stz204579 
2070a7746f66Stz204579 static int
2071a7746f66Stz204579 pa_print_gid(pr_context_t *context, gid_t gid, int status, int flag)
2072a7746f66Stz204579 {
2073a7746f66Stz204579 	int	returnstat;
2074a7746f66Stz204579 	struct group *gr;
2075a7746f66Stz204579 	uval_t	uval;
2076a7746f66Stz204579 
2077a7746f66Stz204579 	if (status < 0)
2078a7746f66Stz204579 		return (status);
2079a7746f66Stz204579 
20807c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_RAWM)) {
20817c478bd9Sstevel@tonic-gate 		/* get group file entry */
20827c478bd9Sstevel@tonic-gate 		if ((gr = getgrgid(gid)) == NULL) {
20837c478bd9Sstevel@tonic-gate 			returnstat = 1;
20847c478bd9Sstevel@tonic-gate 		} else {
20857c478bd9Sstevel@tonic-gate 			/* print in ASCII form */
20867c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_STRING;
20877c478bd9Sstevel@tonic-gate 			uval.string_val = gr->gr_name;
20887c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
20897c478bd9Sstevel@tonic-gate 		}
20907c478bd9Sstevel@tonic-gate 	}
20917c478bd9Sstevel@tonic-gate 	/* print in integer form */
20927c478bd9Sstevel@tonic-gate 	if ((context->format & PRF_RAWM) || (returnstat == 1)) {
20937c478bd9Sstevel@tonic-gate 		uval.uvaltype = PRA_INT32;
20947c478bd9Sstevel@tonic-gate 		uval.int32_val = gid;
20957c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 	return (returnstat);
20987c478bd9Sstevel@tonic-gate }
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate /*
21027c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
2103a7746f66Stz204579  * pa_gr_uid()	: Issues pr_adr_u_int32 to reads group uid from input stream
2104a7746f66Stz204579  *			pointed to by audit_adr, and displays it in either
2105a7746f66Stz204579  *			raw form or its ASCII representation, if status >= 0.
2106a7746f66Stz204579  * return codes : -1 - error
2107a7746f66Stz204579  * 		:  1 - warning, passwd entry not found
2108a7746f66Stz204579  *		:  0 - successful
2109a7746f66Stz204579  * -----------------------------------------------------------------------
2110a7746f66Stz204579  */
2111a7746f66Stz204579 int
2112a7746f66Stz204579 pa_gr_uid(pr_context_t *context, int status, int flag)
2113a7746f66Stz204579 {
2114a7746f66Stz204579 	uint32_t gid;
2115a7746f66Stz204579 
2116a7746f66Stz204579 	if (status < 0)
2117a7746f66Stz204579 		return (status);
2118a7746f66Stz204579 
2119a7746f66Stz204579 	if (pr_adr_u_int32(context, &gid, 1) != 0)
2120a7746f66Stz204579 		/* cannot retrieve gid */
2121a7746f66Stz204579 		return (-1);
2122a7746f66Stz204579 
2123a7746f66Stz204579 	return (pa_print_gid(context, gid, status, flag));
2124a7746f66Stz204579 }
2125a7746f66Stz204579 
2126a7746f66Stz204579 
2127a7746f66Stz204579 /*
2128a7746f66Stz204579  * -----------------------------------------------------------------------
21297c478bd9Sstevel@tonic-gate  * pa_pw_uid_gr_gid()	: Issues pr_adr_u_int32 to reads uid or group uid
21307c478bd9Sstevel@tonic-gate  *			from input stream
21317c478bd9Sstevel@tonic-gate  *			pointed to by audit_adr, and displays it in either
21327c478bd9Sstevel@tonic-gate  *			raw form or its ASCII representation, if status >= 0.
21337c478bd9Sstevel@tonic-gate  * return codes : -1 - error
21347c478bd9Sstevel@tonic-gate  * 		:  1 - warning, passwd entry not found
21357c478bd9Sstevel@tonic-gate  *		:  0 - successful
21367c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
21377c478bd9Sstevel@tonic-gate  */
21387c478bd9Sstevel@tonic-gate int
21397c478bd9Sstevel@tonic-gate pa_pw_uid_gr_gid(pr_context_t *context, int status, int flag)
21407c478bd9Sstevel@tonic-gate {
21417c478bd9Sstevel@tonic-gate 	int	returnstat;
21427c478bd9Sstevel@tonic-gate 	uint32_t	value;
21437c478bd9Sstevel@tonic-gate 	uval_t		uval;
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	if (status < 0)
21467c478bd9Sstevel@tonic-gate 		return (status);
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 	/* get value of a_type */
21497c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int32(context, &value, 1)) != 0)
21507c478bd9Sstevel@tonic-gate 		return (returnstat);
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_ACLTYPE)) != 0)
21537c478bd9Sstevel@tonic-gate 		return (returnstat);
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_UINT32;
21567c478bd9Sstevel@tonic-gate 	uval.uint32_val = value;
21577c478bd9Sstevel@tonic-gate 	if ((returnstat = pa_print(context, &uval, flag)) != 0)
21587c478bd9Sstevel@tonic-gate 		return (returnstat);
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	if ((returnstat = close_tag(context, TAG_ACLTYPE)) != 0)
21617c478bd9Sstevel@tonic-gate 		return (returnstat);
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_ACLVAL)) != 0)
21647c478bd9Sstevel@tonic-gate 		return (returnstat);
21657c478bd9Sstevel@tonic-gate 	/*
21667c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
21677c478bd9Sstevel@tonic-gate 	 * The "mask" and "other" strings refer to the class mask
21687c478bd9Sstevel@tonic-gate 	 * and other (or world) entries in an ACL.
21697c478bd9Sstevel@tonic-gate 	 * The "unrecognized" string refers to an unrecognized ACL
21707c478bd9Sstevel@tonic-gate 	 * entry.
21717c478bd9Sstevel@tonic-gate 	 */
21727c478bd9Sstevel@tonic-gate 	switch (value) {
21737c478bd9Sstevel@tonic-gate 		case USER_OBJ:
21747c478bd9Sstevel@tonic-gate 		case USER:
21757c478bd9Sstevel@tonic-gate 			returnstat = pa_pw_uid(context, returnstat, flag);
21767c478bd9Sstevel@tonic-gate 			break;
21777c478bd9Sstevel@tonic-gate 		case GROUP_OBJ:
21787c478bd9Sstevel@tonic-gate 		case GROUP:
21797c478bd9Sstevel@tonic-gate 			returnstat = pa_gr_uid(context, returnstat, flag);
21807c478bd9Sstevel@tonic-gate 			break;
21817c478bd9Sstevel@tonic-gate 		case CLASS_OBJ:
21827c478bd9Sstevel@tonic-gate 			returnstat = pr_adr_u_int32(context, &value, 1);
21837c478bd9Sstevel@tonic-gate 			if (returnstat != 0)
21847c478bd9Sstevel@tonic-gate 				return (returnstat);
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 			if (!(context->format & PRF_RAWM)) {
21877c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_STRING;
21887c478bd9Sstevel@tonic-gate 				uval.string_val = gettext("mask");
21897c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
21907c478bd9Sstevel@tonic-gate 			} else {
21917c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_UINT32;
21927c478bd9Sstevel@tonic-gate 				uval.uint32_val = value;
21938f9294f3Sgww 				if ((returnstat =
21948f9294f3Sgww 				    pa_print(context, &uval, flag)) != 0) {
21957c478bd9Sstevel@tonic-gate 					return (returnstat);
21967c478bd9Sstevel@tonic-gate 				}
21978f9294f3Sgww 			}
21987c478bd9Sstevel@tonic-gate 			break;
21997c478bd9Sstevel@tonic-gate 		case OTHER_OBJ:
22007c478bd9Sstevel@tonic-gate 			returnstat = pr_adr_u_int32(context, &value, 1);
22017c478bd9Sstevel@tonic-gate 			if (returnstat != 0)
22027c478bd9Sstevel@tonic-gate 				return (returnstat);
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 			if (!(context->format & PRF_RAWM)) {
22057c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_STRING;
22067c478bd9Sstevel@tonic-gate 				uval.string_val = gettext("other");
22077c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
22087c478bd9Sstevel@tonic-gate 			} else {
22097c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_UINT32;
22107c478bd9Sstevel@tonic-gate 				uval.uint32_val = value;
22118f9294f3Sgww 				if ((returnstat =
22128f9294f3Sgww 				    pa_print(context, &uval, flag)) != 0) {
22137c478bd9Sstevel@tonic-gate 					return (returnstat);
22147c478bd9Sstevel@tonic-gate 				}
22158f9294f3Sgww 			}
22167c478bd9Sstevel@tonic-gate 			break;
22177c478bd9Sstevel@tonic-gate 		default:
22187c478bd9Sstevel@tonic-gate 			returnstat = pr_adr_u_int32(context, &value, 1);
22197c478bd9Sstevel@tonic-gate 			if (returnstat != 0)
22207c478bd9Sstevel@tonic-gate 				return (returnstat);
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 			if (!(context->format & PRF_RAWM)) {
22237c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_STRING;
22247c478bd9Sstevel@tonic-gate 				uval.string_val = gettext("unrecognized");
22257c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
22267c478bd9Sstevel@tonic-gate 			} else {
22277c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_UINT32;
22287c478bd9Sstevel@tonic-gate 				uval.uint32_val = value;
22298f9294f3Sgww 				if ((returnstat =
22308f9294f3Sgww 				    pa_print(context, &uval, flag)) != 0) {
22317c478bd9Sstevel@tonic-gate 					return (returnstat);
22327c478bd9Sstevel@tonic-gate 				}
22337c478bd9Sstevel@tonic-gate 			}
22348f9294f3Sgww 	}
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	if ((returnstat = close_tag(context, TAG_ACLVAL)) != 0)
22377c478bd9Sstevel@tonic-gate 		return (returnstat);
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	return (returnstat);
22407c478bd9Sstevel@tonic-gate }
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate /*
22447c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
22457c478bd9Sstevel@tonic-gate  * pa_event_modifier(): Issues pr_adr_u_short to retrieve the next ADR item from
22467c478bd9Sstevel@tonic-gate  *		  the input stream pointed to by audit_adr.  This is the
22477c478bd9Sstevel@tonic-gate  *		  event type, and is displayed in hex;
22487c478bd9Sstevel@tonic-gate  * return codes : -1 - error
22497c478bd9Sstevel@tonic-gate  *		:  0 - successful
22507c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
22517c478bd9Sstevel@tonic-gate  */
22527c478bd9Sstevel@tonic-gate int
22537c478bd9Sstevel@tonic-gate pa_event_modifier(pr_context_t *context, int status,  int flag)
22547c478bd9Sstevel@tonic-gate {
22557c478bd9Sstevel@tonic-gate 	int	returnstat;
22567c478bd9Sstevel@tonic-gate 	ushort_t emodifier;
22577c478bd9Sstevel@tonic-gate 	uval_t	uval;
22587c478bd9Sstevel@tonic-gate 	char	modstring[64];
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	if (status < 0)
22617c478bd9Sstevel@tonic-gate 		return (status);
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_short(context, &emodifier, 1)) != 0)
22647c478bd9Sstevel@tonic-gate 		return (returnstat);
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 	/* For XML, only print when modifier is non-zero */
22677c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_XMLM) || (emodifier != 0)) {
22687c478bd9Sstevel@tonic-gate 		uval.uvaltype = PRA_STRING;
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 		returnstat = open_tag(context, TAG_EVMOD);
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 		if (returnstat >= 0) {
22737c478bd9Sstevel@tonic-gate 			if (!(context->format & PRF_RAWM)) {
22747c478bd9Sstevel@tonic-gate 				eventmodifier2string(emodifier, modstring,
22757c478bd9Sstevel@tonic-gate 				    sizeof (modstring));
22767c478bd9Sstevel@tonic-gate 				uval.string_val = modstring;
22777c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
22787c478bd9Sstevel@tonic-gate 			} else {
22797c478bd9Sstevel@tonic-gate 				uval.string_val = hexconvert((char *)&emodifier,
22807c478bd9Sstevel@tonic-gate 				    sizeof (emodifier), sizeof (emodifier));
22817c478bd9Sstevel@tonic-gate 				if (uval.string_val) {
22827c478bd9Sstevel@tonic-gate 					returnstat = pa_print(context, &uval,
22837c478bd9Sstevel@tonic-gate 					    flag);
22847c478bd9Sstevel@tonic-gate 					free(uval.string_val);
22857c478bd9Sstevel@tonic-gate 				}
22867c478bd9Sstevel@tonic-gate 			}
22877c478bd9Sstevel@tonic-gate 		}
22887c478bd9Sstevel@tonic-gate 		if (returnstat >= 0)
22897c478bd9Sstevel@tonic-gate 			returnstat = close_tag(context, TAG_EVMOD);
22907c478bd9Sstevel@tonic-gate 	}
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	return (returnstat);
22937c478bd9Sstevel@tonic-gate }
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate /*
22977c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
22987c478bd9Sstevel@tonic-gate  * pa_event_type(): Issues pr_adr_u_short to retrieve the next ADR item from
22997c478bd9Sstevel@tonic-gate  *		  the input stream pointed to by audit_adr.  This is the
23007c478bd9Sstevel@tonic-gate  *		  event type, and is displayed in either raw or
23017c478bd9Sstevel@tonic-gate  *		  ASCII form as appropriate
23027c478bd9Sstevel@tonic-gate  * return codes : -1 - error
23037c478bd9Sstevel@tonic-gate  *		:  0 - successful
23047c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
23057c478bd9Sstevel@tonic-gate  */
23067c478bd9Sstevel@tonic-gate int
23077c478bd9Sstevel@tonic-gate pa_event_type(pr_context_t *context, int status,  int flag)
23087c478bd9Sstevel@tonic-gate {
23097c478bd9Sstevel@tonic-gate 	ushort_t etype;
23107c478bd9Sstevel@tonic-gate 	int	returnstat;
23117c478bd9Sstevel@tonic-gate 	au_event_ent_t *p_event = NULL;
23127c478bd9Sstevel@tonic-gate 	uval_t	uval;
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	if (status >= 0) {
23157c478bd9Sstevel@tonic-gate 		if ((returnstat = pr_adr_u_short(context, &etype, 1)) == 0) {
23167c478bd9Sstevel@tonic-gate 			if (!(context->format & PRF_RAWM)) {
23177c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_STRING;
23187c478bd9Sstevel@tonic-gate 				if (context->format & PRF_NOCACHE) {
23197c478bd9Sstevel@tonic-gate 					p_event = getauevnum(etype);
23207c478bd9Sstevel@tonic-gate 				} else {
23217c478bd9Sstevel@tonic-gate 					(void) cacheauevent(&p_event, etype);
23227c478bd9Sstevel@tonic-gate 				}
23237c478bd9Sstevel@tonic-gate 				if (p_event != NULL) {
23247c478bd9Sstevel@tonic-gate 					if (context->format & PRF_SHORTM)
23257c478bd9Sstevel@tonic-gate 						uval.string_val =
23267c478bd9Sstevel@tonic-gate 						    p_event->ae_name;
23277c478bd9Sstevel@tonic-gate 					else
23287c478bd9Sstevel@tonic-gate 						uval.string_val =
23297c478bd9Sstevel@tonic-gate 						    p_event->ae_desc;
23307c478bd9Sstevel@tonic-gate 				} else {
23317c478bd9Sstevel@tonic-gate 					uval.string_val =
23327c478bd9Sstevel@tonic-gate 					    gettext("invalid event number");
23337c478bd9Sstevel@tonic-gate 				}
23347c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
23357c478bd9Sstevel@tonic-gate 			} else {
23367c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_USHORT;
23377c478bd9Sstevel@tonic-gate 				uval.ushort_val = etype;
23387c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
23397c478bd9Sstevel@tonic-gate 			}
23407c478bd9Sstevel@tonic-gate 		}
23417c478bd9Sstevel@tonic-gate 		return (returnstat);
23427c478bd9Sstevel@tonic-gate 	} else
23437c478bd9Sstevel@tonic-gate 		return (status);
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate }
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate /*
23497c478bd9Sstevel@tonic-gate  * Print time from struct timeval to millisecond resolution.
23507c478bd9Sstevel@tonic-gate  *
23517c478bd9Sstevel@tonic-gate  *	typedef long	time_t;		time of day in seconds
23527c478bd9Sstevel@tonic-gate  *	typedef	long	useconds_t;	signed # of microseconds
23537c478bd9Sstevel@tonic-gate  *
23547c478bd9Sstevel@tonic-gate  * struct timeval {
23557c478bd9Sstevel@tonic-gate  *	time_t		tv_sec;		seconds
23567c478bd9Sstevel@tonic-gate  *	suseconds_t	tv_usec;	and microseconds
23577c478bd9Sstevel@tonic-gate  * };
23587c478bd9Sstevel@tonic-gate  */
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate int
23617c478bd9Sstevel@tonic-gate pa_utime32(pr_context_t *context, int status, int flag)
23627c478bd9Sstevel@tonic-gate {
23637c478bd9Sstevel@tonic-gate 	uint32_t scale = 1000;		/* usec to msec */
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	return (do_mtime32(context, status, flag, scale));
23667c478bd9Sstevel@tonic-gate }
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate /*
23697c478bd9Sstevel@tonic-gate  * Print time from timestruc_t to millisecond resolution.
23707c478bd9Sstevel@tonic-gate  *
23717c478bd9Sstevel@tonic-gate  *	typedef struct timespec timestruct_t;
23727c478bd9Sstevel@tonic-gate  * struct timespec{
23737c478bd9Sstevel@tonic-gate  *	time_t	tv_sec;		seconds
23747c478bd9Sstevel@tonic-gate  *	long	tv_nsec;	and nanoseconds
23757c478bd9Sstevel@tonic-gate  * };
23767c478bd9Sstevel@tonic-gate  */
23777c478bd9Sstevel@tonic-gate int
23787c478bd9Sstevel@tonic-gate pa_ntime32(pr_context_t *context, int status, int flag)
23797c478bd9Sstevel@tonic-gate {
23807c478bd9Sstevel@tonic-gate 	uint32_t scale = 1000000;	/* nsec to msec */
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	return (do_mtime32(context, status, flag, scale));
23837c478bd9Sstevel@tonic-gate }
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate /*
23867c478bd9Sstevel@tonic-gate  * Format the timezone +/- HH:MM and terminate the string
23877c478bd9Sstevel@tonic-gate  * Note tm and tv_sec are the same time.
23887c478bd9Sstevel@tonic-gate  * Too bad strftime won't produce an ISO 8601 time zone numeric
23897c478bd9Sstevel@tonic-gate  */
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate #define	MINS	(24L * 60)
23927c478bd9Sstevel@tonic-gate static void
23937c478bd9Sstevel@tonic-gate tzone(struct tm *tm, time_t *tv_sec, char *p)
23947c478bd9Sstevel@tonic-gate {
23957c478bd9Sstevel@tonic-gate 	struct tm *gmt;
23967c478bd9Sstevel@tonic-gate 	int min_off;
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	gmt = gmtime(tv_sec);
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	min_off = ((tm->tm_hour - gmt->tm_hour) * 60) +
24017c478bd9Sstevel@tonic-gate 	    (tm->tm_min - gmt->tm_min);
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	if (tm->tm_year < gmt->tm_year)		/* cross new year */
24047c478bd9Sstevel@tonic-gate 		min_off -= MINS;
24057c478bd9Sstevel@tonic-gate 	else if (tm->tm_year > gmt->tm_year)
24067c478bd9Sstevel@tonic-gate 		min_off += MINS;
24077c478bd9Sstevel@tonic-gate 	else if (tm->tm_yday < gmt->tm_yday)	/* cross dateline */
24087c478bd9Sstevel@tonic-gate 		min_off -= MINS;
24097c478bd9Sstevel@tonic-gate 	else if (tm->tm_yday > gmt->tm_yday)
24107c478bd9Sstevel@tonic-gate 		min_off += MINS;
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	if (min_off < 0) {
24137c478bd9Sstevel@tonic-gate 		min_off = -min_off;
24147c478bd9Sstevel@tonic-gate 		*p++ = '-';
24157c478bd9Sstevel@tonic-gate 	} else {
24167c478bd9Sstevel@tonic-gate 		*p++ = '+';
24177c478bd9Sstevel@tonic-gate 	}
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	*p++ = min_off / 600 + '0';		/* 10s of hours */
24207c478bd9Sstevel@tonic-gate 	min_off = min_off - min_off / 600 * 600;
24217c478bd9Sstevel@tonic-gate 	*p++ = min_off / 60 % 10 + '0';		/* hours */
24227c478bd9Sstevel@tonic-gate 	min_off = min_off - min_off / 60 * 60;
24237c478bd9Sstevel@tonic-gate 	*p++ = ':';
24247c478bd9Sstevel@tonic-gate 	*p++ = min_off / 10 + '0';		/* 10s of minutes */
24257c478bd9Sstevel@tonic-gate 	*p++ = min_off % 10 + '0';		/* minutes */
24267c478bd9Sstevel@tonic-gate 	*p = '\0';
24277c478bd9Sstevel@tonic-gate }
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate /*
24307c478bd9Sstevel@tonic-gate  * Format the milliseconds in place in the string.
24317c478bd9Sstevel@tonic-gate  * Borrowed from strftime.c:itoa()
24327c478bd9Sstevel@tonic-gate  */
24337c478bd9Sstevel@tonic-gate static void
24347c478bd9Sstevel@tonic-gate msec32(uint32_t msec, char *p)
24357c478bd9Sstevel@tonic-gate {
24367c478bd9Sstevel@tonic-gate 	*p++ = msec / 100 + '0';
24377c478bd9Sstevel@tonic-gate 	msec  = msec - msec / 100 * 100;
24387c478bd9Sstevel@tonic-gate 	*p++ = msec / 10 + '0';
24397c478bd9Sstevel@tonic-gate 	*p++ = msec % 10 +'0';
24407c478bd9Sstevel@tonic-gate }
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate /*
24437c478bd9Sstevel@tonic-gate  * Format time and print relative to scale factor from micro/nano seconds.
24447c478bd9Sstevel@tonic-gate  */
24457c478bd9Sstevel@tonic-gate static int
24467c478bd9Sstevel@tonic-gate do_mtime32(pr_context_t *context, int status, int flag, uint32_t scale)
24477c478bd9Sstevel@tonic-gate {
24487c478bd9Sstevel@tonic-gate 	uint32_t t32;
24497c478bd9Sstevel@tonic-gate 	time_t tv_sec;
24507c478bd9Sstevel@tonic-gate 	struct tm tm;
24517c478bd9Sstevel@tonic-gate 	char	time_created[sizeof ("YYYY-MM-DD HH:MM:SS.sss -HH:MM")];
24527c478bd9Sstevel@tonic-gate 	int	returnstat;
24537c478bd9Sstevel@tonic-gate 	uval_t	uval;
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 	if (status < 0)
24567c478bd9Sstevel@tonic-gate 		return (status);
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_ISO)) != 0)
24597c478bd9Sstevel@tonic-gate 		return (returnstat);
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int32(context,
24627c478bd9Sstevel@tonic-gate 	    (uint32_t *)&tv_sec, 1)) != 0)
24637c478bd9Sstevel@tonic-gate 		return (returnstat);
24647c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int32(context, &t32, 1)) == 0) {
24657c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
24667c478bd9Sstevel@tonic-gate 			(void) localtime_r(&tv_sec, &tm);
24677c478bd9Sstevel@tonic-gate 			(void) strftime(time_created,
24687c478bd9Sstevel@tonic-gate 			    sizeof ("YYYY-MM-DD HH:MM:SS.xxx "),
24697c478bd9Sstevel@tonic-gate 			    "%Y-%m-%d %H:%M:%S.xxx ", &tm);
24707c478bd9Sstevel@tonic-gate 			msec32(t32/scale,
24717c478bd9Sstevel@tonic-gate 			    &time_created[sizeof ("YYYY-MM-DD HH:MM:SS.")-1]);
24727c478bd9Sstevel@tonic-gate 			tzone(&tm, &tv_sec,
24737c478bd9Sstevel@tonic-gate 			    &time_created[
24747c478bd9Sstevel@tonic-gate 			    sizeof ("YYYY-MM-DD HH:MM:SS.xxx ")-1]);
24757c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_STRING;
24767c478bd9Sstevel@tonic-gate 			uval.string_val = time_created;
24777c478bd9Sstevel@tonic-gate 		} else {
24787c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT32;
24797c478bd9Sstevel@tonic-gate 			uval.uint32_val = (uint32_t)tv_sec;
24807c478bd9Sstevel@tonic-gate 			(void) pa_print(context, &uval, 0);
24817c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT32;
24827c478bd9Sstevel@tonic-gate 			uval.uint32_val = t32;
24837c478bd9Sstevel@tonic-gate 		}
24847c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
24857c478bd9Sstevel@tonic-gate 	}
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	if (returnstat == 0)
24887c478bd9Sstevel@tonic-gate 		return (close_tag(context, TAG_ISO));
24897c478bd9Sstevel@tonic-gate 	else
24907c478bd9Sstevel@tonic-gate 		return (returnstat);
24917c478bd9Sstevel@tonic-gate }
24927c478bd9Sstevel@tonic-gate 
24937c478bd9Sstevel@tonic-gate /*
24947c478bd9Sstevel@tonic-gate  * Print time from struct timeval to millisecond resolution.
24957c478bd9Sstevel@tonic-gate  *
24967c478bd9Sstevel@tonic-gate  *	typedef long	time_t;		time of day in seconds
24977c478bd9Sstevel@tonic-gate  *	typedef	long	useconds_t;	signed # of microseconds
24987c478bd9Sstevel@tonic-gate  *
24997c478bd9Sstevel@tonic-gate  * struct timeval {
25007c478bd9Sstevel@tonic-gate  *	time_t		tv_sec;		seconds
25017c478bd9Sstevel@tonic-gate  *	suseconds_t	tv_usec;	and microseconds
25027c478bd9Sstevel@tonic-gate  * };
25037c478bd9Sstevel@tonic-gate  */
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate int
25067c478bd9Sstevel@tonic-gate pa_utime64(pr_context_t *context, int status, int flag)
25077c478bd9Sstevel@tonic-gate {
25087c478bd9Sstevel@tonic-gate 	uint64_t scale = 1000;		/* usec to msec */
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	return (do_mtime64(context, status, flag, scale));
25117c478bd9Sstevel@tonic-gate }
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate /*
25147c478bd9Sstevel@tonic-gate  * Print time from timestruc_t to millisecond resolution.
25157c478bd9Sstevel@tonic-gate  *
25167c478bd9Sstevel@tonic-gate  *	typedef struct timespec timestruct_t;
25177c478bd9Sstevel@tonic-gate  * struct timespec{
25187c478bd9Sstevel@tonic-gate  *	time_t	tv_sec;		seconds
25197c478bd9Sstevel@tonic-gate  *	long	tv_nsec;	and nanoseconds
25207c478bd9Sstevel@tonic-gate  * };
25217c478bd9Sstevel@tonic-gate  */
25227c478bd9Sstevel@tonic-gate int
25237c478bd9Sstevel@tonic-gate pa_ntime64(pr_context_t *context, int status, int flag)
25247c478bd9Sstevel@tonic-gate {
25257c478bd9Sstevel@tonic-gate 	uint64_t scale = 1000000;	/* nsec to msec */
25267c478bd9Sstevel@tonic-gate 
25277c478bd9Sstevel@tonic-gate 	return (do_mtime64(context, status, flag, scale));
25287c478bd9Sstevel@tonic-gate }
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate /*
25317c478bd9Sstevel@tonic-gate  * Format the milliseconds in place in the string.
25327c478bd9Sstevel@tonic-gate  * Borrowed from strftime.c:itoa()
25337c478bd9Sstevel@tonic-gate  */
25347c478bd9Sstevel@tonic-gate static void
25357c478bd9Sstevel@tonic-gate msec64(uint64_t msec, char *p)
25367c478bd9Sstevel@tonic-gate {
25377c478bd9Sstevel@tonic-gate 	*p++ = msec / 100 + '0';
25387c478bd9Sstevel@tonic-gate 	msec = msec - msec / 100 * 100;
25397c478bd9Sstevel@tonic-gate 	*p++ = msec / 10 + '0';
25407c478bd9Sstevel@tonic-gate 	*p++ = msec % 10 +'0';
25417c478bd9Sstevel@tonic-gate }
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate /*
25447c478bd9Sstevel@tonic-gate  * Format time and print relative to scale factor from micro/nano seconds.
25457c478bd9Sstevel@tonic-gate  */
25467c478bd9Sstevel@tonic-gate static int
25477c478bd9Sstevel@tonic-gate do_mtime64(pr_context_t *context, int status, int flag, uint64_t scale)
25487c478bd9Sstevel@tonic-gate {
25497c478bd9Sstevel@tonic-gate 	uint64_t t64_sec;
25507c478bd9Sstevel@tonic-gate 	uint64_t t64_msec;
25517c478bd9Sstevel@tonic-gate 	time_t tv_sec;
25527c478bd9Sstevel@tonic-gate 	struct tm tm;
25537c478bd9Sstevel@tonic-gate 	char	time_created[sizeof ("YYYY-MM-DD HH:MM:SS.sss -HH:MM")];
25547c478bd9Sstevel@tonic-gate 	int	returnstat;
25557c478bd9Sstevel@tonic-gate 	uval_t	uval;
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 	if (status < 0)
25587c478bd9Sstevel@tonic-gate 		return (status);
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_ISO)) != 0)
25617c478bd9Sstevel@tonic-gate 		return (returnstat);
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int64(context, &t64_sec, 1)) != 0)
25647c478bd9Sstevel@tonic-gate 		return (returnstat);
25657c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int64(context, &t64_msec, 1)) == 0) {
25667c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
25677c478bd9Sstevel@tonic-gate #ifndef	_LP64
25687c478bd9Sstevel@tonic-gate 			/*
25697c478bd9Sstevel@tonic-gate 			 * N.B.
25707c478bd9Sstevel@tonic-gate 			 * This fails for years from 2038
25717c478bd9Sstevel@tonic-gate 			 * The Y2K+38 problem
25727c478bd9Sstevel@tonic-gate 			 */
25737c478bd9Sstevel@tonic-gate #endif	/* !_LP64 */
25747c478bd9Sstevel@tonic-gate 			tv_sec = (time_t)t64_sec;
25757c478bd9Sstevel@tonic-gate 			(void) localtime_r(&tv_sec, &tm);
25767c478bd9Sstevel@tonic-gate 			(void) strftime(time_created,
25777c478bd9Sstevel@tonic-gate 			    sizeof ("YYYY-MM-DD HH:MM:SS.xxx "),
25787c478bd9Sstevel@tonic-gate 			    "%Y-%m-%d %H:%M:%S.xxx ", &tm);
25797c478bd9Sstevel@tonic-gate 			msec64(t64_msec/scale,
25807c478bd9Sstevel@tonic-gate 			    &time_created[sizeof ("YYYY-MM-DD HH:MM:SS.")-1]);
25817c478bd9Sstevel@tonic-gate 			tzone(&tm, &tv_sec,
25827c478bd9Sstevel@tonic-gate 			    &time_created[
25837c478bd9Sstevel@tonic-gate 			    sizeof ("YYYY-MM-DD HH:MM:SS.xxx ")-1]);
25847c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_STRING;
25857c478bd9Sstevel@tonic-gate 			uval.string_val = time_created;
25867c478bd9Sstevel@tonic-gate 		} else {
25877c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT64;
25887c478bd9Sstevel@tonic-gate 			uval.uint64_val = t64_sec;
25897c478bd9Sstevel@tonic-gate 			(void) pa_print(context, &uval, 0);
25907c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT64;
25917c478bd9Sstevel@tonic-gate 			uval.uint64_val = t64_msec;
25927c478bd9Sstevel@tonic-gate 		}
25937c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
25947c478bd9Sstevel@tonic-gate 	}
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 	if (returnstat < 0)
25977c478bd9Sstevel@tonic-gate 		return (returnstat);
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 	return (close_tag(context, TAG_ISO));
26007c478bd9Sstevel@tonic-gate }
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate /*
26037c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
26047c478bd9Sstevel@tonic-gate  * pa_error()   :  convert the return token error code.
26057c478bd9Sstevel@tonic-gate  *
26067c478bd9Sstevel@tonic-gate  * output	: buf string representing return token error code.
26077c478bd9Sstevel@tonic-gate  *
26087c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
26097c478bd9Sstevel@tonic-gate  */
26107c478bd9Sstevel@tonic-gate void
26117c478bd9Sstevel@tonic-gate pa_error(const uchar_t err, char *buf, size_t buflen)
26127c478bd9Sstevel@tonic-gate {
2613*f72effdeSgww 	if (err == ADT_SUCCESS) {
26147c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf, gettext("success"), buflen);
2615*f72effdeSgww 	} else if ((char)err == ADT_FAILURE) {
26167c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf, gettext("failure"), buflen);
26177c478bd9Sstevel@tonic-gate 	} else {
26187c478bd9Sstevel@tonic-gate 		char *emsg = strerror(err);
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 		if (emsg != NULL) {
26217c478bd9Sstevel@tonic-gate 			(void) strlcpy(buf, gettext("failure: "), buflen);
26227c478bd9Sstevel@tonic-gate 			(void) strlcat(buf, emsg, buflen);
26237c478bd9Sstevel@tonic-gate 		} else {
26247c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, buflen, "%s%d",
26257c478bd9Sstevel@tonic-gate 			    gettext("failure: "), err);
26267c478bd9Sstevel@tonic-gate 		}
26277c478bd9Sstevel@tonic-gate 	}
26287c478bd9Sstevel@tonic-gate }
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate /*
26317c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
26327c478bd9Sstevel@tonic-gate  * pa_retval()   :  convert the return token return value code.
26337c478bd9Sstevel@tonic-gate  *
26347c478bd9Sstevel@tonic-gate  * output	: buf string representing return token error code.
26357c478bd9Sstevel@tonic-gate  *
26367c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
26377c478bd9Sstevel@tonic-gate  */
26387c478bd9Sstevel@tonic-gate void
2639*f72effdeSgww pa_retval(const uchar_t err, const int32_t retval, char *buf, size_t buflen)
26407c478bd9Sstevel@tonic-gate {
26417c478bd9Sstevel@tonic-gate 	struct msg_text	*msglist = &adt_msg_text[ADT_LIST_FAIL_VALUE];
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 	if ((retval + msglist->ml_offset >= msglist->ml_min_index) &&
26447c478bd9Sstevel@tonic-gate 	    (retval + msglist->ml_offset <= msglist->ml_max_index)) {
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf,
26477c478bd9Sstevel@tonic-gate 		    gettext(msglist->ml_msg_list[retval + msglist->ml_offset]),
26487c478bd9Sstevel@tonic-gate 		    buflen);
26497c478bd9Sstevel@tonic-gate 	} else if ((retval >= ADT_FAIL_PAM) &&
2650*f72effdeSgww 	    (retval < ADT_FAIL_PAM + PAM_TOTAL_ERRNUM)) {
26517c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf, pam_strerror(NULL, retval - ADT_FAIL_PAM),
26527c478bd9Sstevel@tonic-gate 		    buflen);
2653*f72effdeSgww 	} else if ((char)err == ADT_FAILURE) {
2654*f72effdeSgww 		char *emsg = strerror(retval);
2655*f72effdeSgww 
2656*f72effdeSgww 		if (emsg != NULL) {
2657*f72effdeSgww 			(void) strlcpy(buf, emsg, buflen);
2658*f72effdeSgww 		} else {
26597c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, buflen, "%d", retval);
26607c478bd9Sstevel@tonic-gate 		}
2661*f72effdeSgww 	} else {
2662*f72effdeSgww 		(void) snprintf(buf, buflen, "%d", retval);
2663*f72effdeSgww 	}
2664*f72effdeSgww }
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate /*
26687c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
26697c478bd9Sstevel@tonic-gate  * pa_printstr()	:  print a given string, translating unprintables
26707c478bd9Sstevel@tonic-gate  *			:  as needed.
26717c478bd9Sstevel@tonic-gate  */
26727c478bd9Sstevel@tonic-gate static int
26737c478bd9Sstevel@tonic-gate pa_printstr(pr_context_t *context, char *str)
26747c478bd9Sstevel@tonic-gate {
26757c478bd9Sstevel@tonic-gate 	int	err = 0;
26767c478bd9Sstevel@tonic-gate 	int	len, printable;
26777c478bd9Sstevel@tonic-gate 	int	mbmax = MB_CUR_MAX;
26787c478bd9Sstevel@tonic-gate 	wchar_t	wc;
26797c478bd9Sstevel@tonic-gate 	char	c;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	if (mbmax == 1) {
26827c478bd9Sstevel@tonic-gate 		/* fast path */
26837c478bd9Sstevel@tonic-gate 		while (err == 0 && *str != '\0') {
26847c478bd9Sstevel@tonic-gate 			c = *str++;
26857c478bd9Sstevel@tonic-gate 			printable = isprint((unsigned char)c);
26867c478bd9Sstevel@tonic-gate 			err = pa_putstr(context, printable, &c, 1);
26877c478bd9Sstevel@tonic-gate 		}
26887c478bd9Sstevel@tonic-gate 		return (err);
26897c478bd9Sstevel@tonic-gate 	}
26907c478bd9Sstevel@tonic-gate 	while (err == 0 && *str != '\0') {
26917c478bd9Sstevel@tonic-gate 		len = mbtowc(&wc, str, mbmax);
26927c478bd9Sstevel@tonic-gate 		if (len <= 0) {
26937c478bd9Sstevel@tonic-gate 			len = 1;
26947c478bd9Sstevel@tonic-gate 			printable = 0;
26957c478bd9Sstevel@tonic-gate 		} else {
26967c478bd9Sstevel@tonic-gate 			printable = iswprint(wc);
26977c478bd9Sstevel@tonic-gate 		}
26987c478bd9Sstevel@tonic-gate 		err = pa_putstr(context, printable, str, len);
26997c478bd9Sstevel@tonic-gate 		str += len;
27007c478bd9Sstevel@tonic-gate 	}
27017c478bd9Sstevel@tonic-gate 	return (err);
27027c478bd9Sstevel@tonic-gate }
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate /*
27057c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
27067c478bd9Sstevel@tonic-gate  * pa_print()	:  print as one str or formatted for easy reading.
27077c478bd9Sstevel@tonic-gate  * 		: flag - indicates whether to output a new line for
27087c478bd9Sstevel@tonic-gate  *		: multi-line output.
27097c478bd9Sstevel@tonic-gate  * 		:		= 0; no new line
27107c478bd9Sstevel@tonic-gate  *		:		= 1; new line if regular output
27117c478bd9Sstevel@tonic-gate  * output	: The audit record information is displayed in the
27127c478bd9Sstevel@tonic-gate  *		  type specified by uvaltype and value specified in
27137c478bd9Sstevel@tonic-gate  *		  uval.  The printing of the delimiter or newline is
27147c478bd9Sstevel@tonic-gate  *		  determined by PRF_ONELINE, and the flag value,
27157c478bd9Sstevel@tonic-gate  *		  as follows:
27167c478bd9Sstevel@tonic-gate  *			+--------+------+------+-----------------+
27177c478bd9Sstevel@tonic-gate  *			|ONELINE | flag | last | Action          |
27187c478bd9Sstevel@tonic-gate  *			+--------+------+------+-----------------+
27197c478bd9Sstevel@tonic-gate  *			|    Y   |   Y  |   T  | print new line  |
27207c478bd9Sstevel@tonic-gate  *			|    Y   |   Y  |   F  | print delimiter |
27217c478bd9Sstevel@tonic-gate  *			|    Y   |   N  |   T  | print new line  |
27227c478bd9Sstevel@tonic-gate  *			|    Y   |   N  |   F  | print delimiter |
27237c478bd9Sstevel@tonic-gate  *			|    N   |   Y  |   T  | print new line  |
27247c478bd9Sstevel@tonic-gate  *			|    N   |   Y  |   F  | print new line  |
27257c478bd9Sstevel@tonic-gate  *			|    N   |   N  |   T  | print new line  |
27267c478bd9Sstevel@tonic-gate  *			|    N   |   N  |   F  | print delimiter |
27277c478bd9Sstevel@tonic-gate  *			+--------+------+------+-----------------+
27287c478bd9Sstevel@tonic-gate  *
27297c478bd9Sstevel@tonic-gate  * return codes : -1 - error
27307c478bd9Sstevel@tonic-gate  *		0 - successful
27317c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
27327c478bd9Sstevel@tonic-gate  */
27337c478bd9Sstevel@tonic-gate int
27347c478bd9Sstevel@tonic-gate pa_print(pr_context_t *context, uval_t *uval, int flag)
27357c478bd9Sstevel@tonic-gate {
27367c478bd9Sstevel@tonic-gate 	int	returnstat = 0;
27377c478bd9Sstevel@tonic-gate 	int	last;
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate 	switch (uval->uvaltype) {
27407c478bd9Sstevel@tonic-gate 	case PRA_INT32:
27417c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%d", uval->int32_val);
27427c478bd9Sstevel@tonic-gate 		break;
27437c478bd9Sstevel@tonic-gate 	case PRA_UINT32:
27447c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%u", uval->uint32_val);
27457c478bd9Sstevel@tonic-gate 		break;
27467c478bd9Sstevel@tonic-gate 	case PRA_INT64:
27477c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%"PRId64, uval->int64_val);
27487c478bd9Sstevel@tonic-gate 		break;
27497c478bd9Sstevel@tonic-gate 	case PRA_UINT64:
27507c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%"PRIu64, uval->uint64_val);
27517c478bd9Sstevel@tonic-gate 		break;
27527c478bd9Sstevel@tonic-gate 	case PRA_SHORT:
27537c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%hd", uval->short_val);
27547c478bd9Sstevel@tonic-gate 		break;
27557c478bd9Sstevel@tonic-gate 	case PRA_USHORT:
27567c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%hu", uval->ushort_val);
27577c478bd9Sstevel@tonic-gate 		break;
27587c478bd9Sstevel@tonic-gate 	case PRA_CHAR:
27597c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%c", uval->char_val);
27607c478bd9Sstevel@tonic-gate 		break;
27617c478bd9Sstevel@tonic-gate 	case PRA_BYTE:
27627c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%d", uval->char_val);
27637c478bd9Sstevel@tonic-gate 		break;
27647c478bd9Sstevel@tonic-gate 	case PRA_STRING:
27657c478bd9Sstevel@tonic-gate 		returnstat = pa_printstr(context, uval->string_val);
27667c478bd9Sstevel@tonic-gate 		break;
27677c478bd9Sstevel@tonic-gate 	case PRA_HEX32:
27687c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "0x%x", uval->int32_val);
27697c478bd9Sstevel@tonic-gate 		break;
27707c478bd9Sstevel@tonic-gate 	case PRA_HEX64:
27717c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "0x%"PRIx64, uval->int64_val);
27727c478bd9Sstevel@tonic-gate 		break;
27737c478bd9Sstevel@tonic-gate 	case PRA_SHEX:
27747c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "0x%hx", uval->short_val);
27757c478bd9Sstevel@tonic-gate 		break;
27767c478bd9Sstevel@tonic-gate 	case PRA_OCT:
27777c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%ho", uval->ushort_val);
27787c478bd9Sstevel@tonic-gate 		break;
27797c478bd9Sstevel@tonic-gate 	case PRA_LOCT:
27807c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%o", (int)uval->uint32_val);
27817c478bd9Sstevel@tonic-gate 		break;
27827c478bd9Sstevel@tonic-gate 	default:
27837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("praudit: Unknown type.\n"));
27847c478bd9Sstevel@tonic-gate 		returnstat = -1;
27857c478bd9Sstevel@tonic-gate 		break;
27867c478bd9Sstevel@tonic-gate 	}
27877c478bd9Sstevel@tonic-gate 	if (returnstat < 0)
27887c478bd9Sstevel@tonic-gate 		return (returnstat);
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	last = (context->audit_adr->adr_now ==
27917c478bd9Sstevel@tonic-gate 	    (context->audit_rec_start + context->audit_rec_len));
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_XMLM)) {
27947c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_ONELINE)) {
27957c478bd9Sstevel@tonic-gate 			if ((flag == 1) || last)
27967c478bd9Sstevel@tonic-gate 				returnstat = pr_putchar(context, '\n');
27977c478bd9Sstevel@tonic-gate 			else
27987c478bd9Sstevel@tonic-gate 				returnstat = pr_printf(context, "%s",
27997c478bd9Sstevel@tonic-gate 				    context->SEPARATOR);
28007c478bd9Sstevel@tonic-gate 		} else {
28017c478bd9Sstevel@tonic-gate 			if (!last)
28027c478bd9Sstevel@tonic-gate 				returnstat = pr_printf(context, "%s",
28037c478bd9Sstevel@tonic-gate 				    context->SEPARATOR);
28047c478bd9Sstevel@tonic-gate 			else
28057c478bd9Sstevel@tonic-gate 				returnstat = pr_putchar(context, '\n');
28067c478bd9Sstevel@tonic-gate 		}
28077c478bd9Sstevel@tonic-gate 	}
28087c478bd9Sstevel@tonic-gate 	if ((returnstat == 0) && (context->data_mode == FILEMODE))
28097c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	return (returnstat);
28127c478bd9Sstevel@tonic-gate }
28137c478bd9Sstevel@tonic-gate 
28148dc347dbStz204579 static struct cntrl_mapping {
28158dc347dbStz204579 	char from;
28168dc347dbStz204579 	char to;
28178dc347dbStz204579 } cntrl_map[] = {
28188dc347dbStz204579 	'\0', '0',
28198dc347dbStz204579 	'\a', 'a',
28208dc347dbStz204579 	'\b', 'b',
28218dc347dbStz204579 	'\t', 't',
28228dc347dbStz204579 	'\f', 'f',
28238dc347dbStz204579 	'\n', 'n',
28248dc347dbStz204579 	'\r', 'r',
28258dc347dbStz204579 	'\v', 'v'
28268dc347dbStz204579 };
28278dc347dbStz204579 
28288dc347dbStz204579 static int cntrl_map_entries = sizeof (cntrl_map)
28298dc347dbStz204579 	/ sizeof (struct cntrl_mapping);
28308dc347dbStz204579 
28317c478bd9Sstevel@tonic-gate /*
28327c478bd9Sstevel@tonic-gate  * Convert binary data to ASCII for printing.
28337c478bd9Sstevel@tonic-gate  */
28347c478bd9Sstevel@tonic-gate void
28357c478bd9Sstevel@tonic-gate convertascii(char *p, char *c, int size)
28367c478bd9Sstevel@tonic-gate {
283785298dc1Snakanon 	int	i, j, uc;
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
284085298dc1Snakanon 		uc = (unsigned char)*(c + i);
284185298dc1Snakanon 		if (isascii(uc)) {
284285298dc1Snakanon 			if (iscntrl(uc)) {
284385298dc1Snakanon 				for (j = 0; j < cntrl_map_entries; j++) {
284485298dc1Snakanon 					if (cntrl_map[j].from == uc) {
28458dc347dbStz204579 						*p++ = '\\';
28468dc347dbStz204579 						*p++ = cntrl_map[j].to;
284785298dc1Snakanon 						break;
28488dc347dbStz204579 					}
284985298dc1Snakanon 				}
285085298dc1Snakanon 				if (j == cntrl_map_entries) {
28517c478bd9Sstevel@tonic-gate 					*p++ = '^';
285285298dc1Snakanon 					*p++ = (char)(uc ^ 0100);
28538dc347dbStz204579 				}
285485298dc1Snakanon 			} else {
285585298dc1Snakanon 				*p++ = (char)uc;
28567c478bd9Sstevel@tonic-gate 			}
285785298dc1Snakanon 		} else {
285885298dc1Snakanon 			p += sprintf(p, "\\%03o", uc);
285985298dc1Snakanon 		}
286085298dc1Snakanon 	}
28617c478bd9Sstevel@tonic-gate 	*p = '\0';
28627c478bd9Sstevel@tonic-gate }
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate /*
28657c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
28667c478bd9Sstevel@tonic-gate  * pa_xgeneric: Process Xobject token and display contents
28677c478bd9Sstevel@tonic-gate  *		      This routine will handle many of the attribute
28687c478bd9Sstevel@tonic-gate  *		      types introduced in TS 2.x, such as:
28697c478bd9Sstevel@tonic-gate  *
28707c478bd9Sstevel@tonic-gate  *		      AUT_XCOLORMAP, AUT_XCURSOR, AUT_XFONT,
28717c478bd9Sstevel@tonic-gate  *		      AUT_XGC, AUT_XPIXMAP, AUT_XWINDOW
28727c478bd9Sstevel@tonic-gate  *
28737c478bd9Sstevel@tonic-gate  * NOTE: At the time of call, the token id has been retrieved
28747c478bd9Sstevel@tonic-gate  *
28757c478bd9Sstevel@tonic-gate  * return codes		: -1 - error
28767c478bd9Sstevel@tonic-gate  *			:  0 - successful
28777c478bd9Sstevel@tonic-gate  * NOTE: At the time of call, the xatom token id has been retrieved
28787c478bd9Sstevel@tonic-gate  *
28797c478bd9Sstevel@tonic-gate  * Format of xobj
28807c478bd9Sstevel@tonic-gate  *	text token id		adr_char
28817c478bd9Sstevel@tonic-gate  * 	XID 			adr_u_int32
28827c478bd9Sstevel@tonic-gate  * 	creator uid		adr_pw_uid
28837c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
28847c478bd9Sstevel@tonic-gate  */
28857c478bd9Sstevel@tonic-gate int
28867c478bd9Sstevel@tonic-gate pa_xgeneric(pr_context_t *context)
28877c478bd9Sstevel@tonic-gate {
28887c478bd9Sstevel@tonic-gate 	int	returnstat;
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	returnstat = process_tag(context, TAG_XID, 0, 0);
28917c478bd9Sstevel@tonic-gate 	return (process_tag(context, TAG_XCUID, returnstat, 1));
28927c478bd9Sstevel@tonic-gate }
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate /*
28967c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------------
28977c478bd9Sstevel@tonic-gate  * pa_liaison : Issues pr_adr_char to retrieve the next ADR item from the
28987c478bd9Sstevel@tonic-gate  *			input stream pointed to by audit_adr, and prints it
28997c478bd9Sstevel@tonic-gate  *			if status >= 0 either in ASCII or raw form
29007c478bd9Sstevel@tonic-gate  * return codes : -1 - error
29017c478bd9Sstevel@tonic-gate  *		: 0 - successful
29027c478bd9Sstevel@tonic-gate  *		: 1 - warning, unknown label type
29037c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
29047c478bd9Sstevel@tonic-gate  */
29057c478bd9Sstevel@tonic-gate int
29067c478bd9Sstevel@tonic-gate pa_liaison(pr_context_t *context, int status, int flag)
29077c478bd9Sstevel@tonic-gate {
29087c478bd9Sstevel@tonic-gate 	int	returnstat;
29097c478bd9Sstevel@tonic-gate 	int32_t	li;
29107c478bd9Sstevel@tonic-gate 	uval_t	uval;
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	if (status >= 0) {
29137c478bd9Sstevel@tonic-gate 		if ((returnstat = pr_adr_int32(context, &li, 1)) != 0) {
29147c478bd9Sstevel@tonic-gate 			return (returnstat);
29157c478bd9Sstevel@tonic-gate 		}
29167c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
29177c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT32;
29187c478bd9Sstevel@tonic-gate 			uval.uint32_val = li;
29197c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
29207c478bd9Sstevel@tonic-gate 		}
29217c478bd9Sstevel@tonic-gate 		/* print in hexadecimal form */
29227c478bd9Sstevel@tonic-gate 		if ((context->format & PRF_RAWM) || (returnstat == 1)) {
29237c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_HEX32;
29247c478bd9Sstevel@tonic-gate 			uval.uint32_val = li;
29257c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
29267c478bd9Sstevel@tonic-gate 		}
29277c478bd9Sstevel@tonic-gate 		return (returnstat);
29287c478bd9Sstevel@tonic-gate 	} else
29297c478bd9Sstevel@tonic-gate 		return (status);
29307c478bd9Sstevel@tonic-gate }
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate /*
29337c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------------
29347c478bd9Sstevel@tonic-gate  * pa_xid : Issues pr_adr_int32 to retrieve the XID from the input
29357c478bd9Sstevel@tonic-gate  *	      stream pointed to by audit_adr, and prints it if
29367c478bd9Sstevel@tonic-gate  *	      status >= 0 either in ASCII or raw form
29377c478bd9Sstevel@tonic-gate  * return codes : -1 - error
29387c478bd9Sstevel@tonic-gate  *		:  0 - successful
29397c478bd9Sstevel@tonic-gate  *		:  1 - warning, unknown label type
29407c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------------
29417c478bd9Sstevel@tonic-gate  */
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate int
29447c478bd9Sstevel@tonic-gate pa_xid(pr_context_t *context, int status, int flag)
29457c478bd9Sstevel@tonic-gate {
29467c478bd9Sstevel@tonic-gate 	int returnstat;
29477c478bd9Sstevel@tonic-gate 	int32_t xid;
29487c478bd9Sstevel@tonic-gate 	uval_t	uval;
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate 	if (status < 0)
29517c478bd9Sstevel@tonic-gate 		return (status);
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	/* get XID from stream */
29547c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_int32(context, (int32_t *)&xid, 1)) != 0)
29557c478bd9Sstevel@tonic-gate 		return (returnstat);
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_RAWM)) {
29587c478bd9Sstevel@tonic-gate 		uval.uvaltype = PRA_STRING;
29597c478bd9Sstevel@tonic-gate 		uval.string_val = hexconvert((char *)&xid, sizeof (xid),
29607c478bd9Sstevel@tonic-gate 		    sizeof (xid));
29617c478bd9Sstevel@tonic-gate 		if (uval.string_val) {
29627c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
29637c478bd9Sstevel@tonic-gate 			free(uval.string_val);
29647c478bd9Sstevel@tonic-gate 		}
29657c478bd9Sstevel@tonic-gate 	} else {
29667c478bd9Sstevel@tonic-gate 		uval.uvaltype = PRA_INT32;
29677c478bd9Sstevel@tonic-gate 		uval.int32_val = xid;
29687c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
29697c478bd9Sstevel@tonic-gate 	}
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	return (returnstat);
29727c478bd9Sstevel@tonic-gate }
2973a7746f66Stz204579 
2974a7746f66Stz204579 static int
2975a7746f66Stz204579 pa_ace_flags(pr_context_t *context, ace_t *ace, int status, int flag)
2976a7746f66Stz204579 {
2977a7746f66Stz204579 	int	returnstat;
2978a7746f66Stz204579 	uval_t	uval;
2979a7746f66Stz204579 
2980a7746f66Stz204579 	if (status < 0)
2981a7746f66Stz204579 		return (status);
2982a7746f66Stz204579 
2983a7746f66Stz204579 	/*
2984a7746f66Stz204579 	 * TRANSLATION_NOTE
2985a7746f66Stz204579 	 * ace->a_flags refers to access flags of ZFS/NFSv4 ACL entry.
2986a7746f66Stz204579 	 */
2987a7746f66Stz204579 	if ((returnstat = open_tag(context, TAG_ACEFLAGS)) != 0)
2988a7746f66Stz204579 		return (returnstat);
2989a7746f66Stz204579 	if (!(context->format & PRF_RAWM)) {
2990a7746f66Stz204579 		uval.uvaltype = PRA_STRING;
2991a7746f66Stz204579 		switch (ace->a_flags & ACE_TYPE_FLAGS) {
2992a7746f66Stz204579 		case ACE_OWNER:
2993a7746f66Stz204579 			uval.string_val = gettext(OWNERAT_TXT);
2994a7746f66Stz204579 			break;
2995a7746f66Stz204579 		case ACE_GROUP | ACE_IDENTIFIER_GROUP:
2996a7746f66Stz204579 			uval.string_val = gettext(GROUPAT_TXT);
2997a7746f66Stz204579 			break;
2998a7746f66Stz204579 		case ACE_IDENTIFIER_GROUP:
2999a7746f66Stz204579 			uval.string_val = gettext(GROUP_TXT);
3000a7746f66Stz204579 			break;
3001a7746f66Stz204579 		case ACE_EVERYONE:
3002a7746f66Stz204579 			uval.string_val = gettext(EVERYONEAT_TXT);
3003a7746f66Stz204579 			break;
3004a7746f66Stz204579 		case 0:
3005a7746f66Stz204579 			uval.string_val = gettext(USER_TXT);
3006a7746f66Stz204579 			break;
3007a7746f66Stz204579 		default:
3008a7746f66Stz204579 			uval.uvaltype = PRA_USHORT;
3009a7746f66Stz204579 			uval.uint32_val = ace->a_flags;
3010a7746f66Stz204579 		}
3011a7746f66Stz204579 	} else {
3012a7746f66Stz204579 		uval.uvaltype = PRA_USHORT;
3013a7746f66Stz204579 		uval.uint32_val = ace->a_flags;
3014a7746f66Stz204579 	}
3015a7746f66Stz204579 	if ((returnstat = pa_print(context, &uval, flag)) != 0)
3016a7746f66Stz204579 		return (returnstat);
3017a7746f66Stz204579 	return (close_tag(context, TAG_ACEFLAGS));
3018a7746f66Stz204579 }
3019a7746f66Stz204579 
3020a7746f66Stz204579 static int
3021a7746f66Stz204579 pa_ace_who(pr_context_t *context, ace_t *ace, int status, int flag)
3022a7746f66Stz204579 {
3023a7746f66Stz204579 	int		returnstat;
3024a7746f66Stz204579 
3025a7746f66Stz204579 	if (status < 0)
3026a7746f66Stz204579 		return (status);
3027a7746f66Stz204579 
3028a7746f66Stz204579 	/*
3029a7746f66Stz204579 	 * TRANSLATION_NOTE
3030a7746f66Stz204579 	 * ace->a_who refers to user id or group id of ZFS/NFSv4 ACL entry.
3031a7746f66Stz204579 	 */
3032a7746f66Stz204579 	if ((returnstat = open_tag(context, TAG_ACEID)) != 0)
3033a7746f66Stz204579 		return (returnstat);
3034a7746f66Stz204579 	switch (ace->a_flags & ACE_TYPE_FLAGS) {
3035a7746f66Stz204579 	case ACE_IDENTIFIER_GROUP:	/* group id */
3036a7746f66Stz204579 		returnstat = pa_print_gid(context, ace->a_who, returnstat,
3037a7746f66Stz204579 		    flag);
3038a7746f66Stz204579 		break;
3039a7746f66Stz204579 	default:			/* user id */
3040a7746f66Stz204579 		returnstat = pa_print_uid(context, ace->a_who, returnstat,
3041a7746f66Stz204579 		    flag);
3042a7746f66Stz204579 		break;
3043a7746f66Stz204579 	}
3044a7746f66Stz204579 	if (returnstat < 0)
3045a7746f66Stz204579 		return (returnstat);
3046a7746f66Stz204579 	return (close_tag(context, TAG_ACEID));
3047a7746f66Stz204579 }
3048a7746f66Stz204579 
3049a7746f66Stz204579 /*
3050a7746f66Stz204579  * Appends what to str, (re)allocating str if necessary.
3051a7746f66Stz204579  */
3052a7746f66Stz204579 #define	INITIAL_ALLOC	256
3053a7746f66Stz204579 static int
3054a7746f66Stz204579 strappend(char **str, char *what, size_t *alloc)
3055a7746f66Stz204579 {
3056a7746f66Stz204579 	char	*s, *newstr;
3057a7746f66Stz204579 	size_t	needed;
3058a7746f66Stz204579 
3059a7746f66Stz204579 	s = *str;
3060a7746f66Stz204579 
3061a7746f66Stz204579 	if (s == NULL) {
3062a7746f66Stz204579 		s = malloc(INITIAL_ALLOC);
3063a7746f66Stz204579 		if (s == NULL) {
3064a7746f66Stz204579 			*alloc = 0;
3065a7746f66Stz204579 			return (-1);
3066a7746f66Stz204579 		}
3067a7746f66Stz204579 		*alloc = INITIAL_ALLOC;
3068a7746f66Stz204579 		s[0] = '\0';
3069a7746f66Stz204579 		*str = s;
3070a7746f66Stz204579 	}
3071a7746f66Stz204579 
3072a7746f66Stz204579 	needed = strlen(s) + strlen(what) + 1;
3073a7746f66Stz204579 	if (*alloc < needed) {
3074a7746f66Stz204579 		newstr = realloc(s, needed);
3075a7746f66Stz204579 		if (newstr == NULL)
3076a7746f66Stz204579 			return (-1);
3077a7746f66Stz204579 		s = newstr;
3078a7746f66Stz204579 		*alloc = needed;
3079a7746f66Stz204579 		*str = s;
3080a7746f66Stz204579 	}
3081a7746f66Stz204579 	(void) strlcat(s, what, *alloc);
3082a7746f66Stz204579 
3083a7746f66Stz204579 	return (0);
3084a7746f66Stz204579 }
3085a7746f66Stz204579 
3086a7746f66Stz204579 static int
3087a7746f66Stz204579 pa_ace_access_mask(pr_context_t *context, ace_t *ace, int status, int flag)
3088a7746f66Stz204579 {
3089a7746f66Stz204579 	int	returnstat, i;
3090a7746f66Stz204579 	uval_t	uval;
3091a7746f66Stz204579 	char	*permstr = NULL;
3092a7746f66Stz204579 	size_t	permstr_alloc = 0;
3093a7746f66Stz204579 
3094a7746f66Stz204579 	if (status < 0)
3095a7746f66Stz204579 		return (status);
3096a7746f66Stz204579 
3097a7746f66Stz204579 	/*
3098a7746f66Stz204579 	 * TRANSLATION_NOTE
3099a7746f66Stz204579 	 * ace->a_access_mask refers to access mask of ZFS/NFSv4 ACL entry.
3100a7746f66Stz204579 	 */
3101a7746f66Stz204579 	if ((returnstat = open_tag(context, TAG_ACEMASK)) != 0)
3102a7746f66Stz204579 		return (returnstat);
3103a7746f66Stz204579 	if (context->format & PRF_SHORTM &&
3104a7746f66Stz204579 	    ((permstr = malloc(15)) != NULL)) {
3105a7746f66Stz204579 		for (i = 0; i < 14; i++)
3106a7746f66Stz204579 			permstr[i] = '-';
3107a7746f66Stz204579 
3108a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_DATA)
3109a7746f66Stz204579 			permstr[0] = 'r';
3110a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_DATA)
3111a7746f66Stz204579 			permstr[1] = 'w';
3112a7746f66Stz204579 		if (ace->a_access_mask & ACE_EXECUTE)
3113a7746f66Stz204579 			permstr[2] = 'x';
3114a7746f66Stz204579 		if (ace->a_access_mask & ACE_APPEND_DATA)
3115a7746f66Stz204579 			permstr[3] = 'p';
3116a7746f66Stz204579 		if (ace->a_access_mask & ACE_DELETE)
3117a7746f66Stz204579 			permstr[4] = 'd';
3118a7746f66Stz204579 		if (ace->a_access_mask & ACE_DELETE_CHILD)
3119a7746f66Stz204579 			permstr[5] = 'D';
3120a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_ATTRIBUTES)
3121a7746f66Stz204579 			permstr[6] = 'a';
3122a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_ATTRIBUTES)
3123a7746f66Stz204579 			permstr[7] = 'A';
3124a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_NAMED_ATTRS)
3125a7746f66Stz204579 			permstr[8] = 'R';
3126a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_NAMED_ATTRS)
3127a7746f66Stz204579 			permstr[9] = 'W';
3128a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_ACL)
3129a7746f66Stz204579 			permstr[10] = 'c';
3130a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_ACL)
3131a7746f66Stz204579 			permstr[11] = 'C';
3132a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_OWNER)
3133a7746f66Stz204579 			permstr[12] = 'o';
3134a7746f66Stz204579 		if (ace->a_access_mask & ACE_SYNCHRONIZE)
3135a7746f66Stz204579 			permstr[13] = 's';
3136a7746f66Stz204579 		permstr[14] = '\0';
3137a7746f66Stz204579 		uval.uvaltype = PRA_STRING;
3138a7746f66Stz204579 		uval.string_val = permstr;
3139a7746f66Stz204579 	} else if (!(context->format & PRF_RAWM)) {
3140a7746f66Stz204579 
3141a7746f66Stz204579 		/*
3142a7746f66Stz204579 		 * Note this differs from acltext.c:ace_perm_txt()
3143a7746f66Stz204579 		 * because we don't know if the acl belongs to a file
3144a7746f66Stz204579 		 * or directory. ace mask value are the same
3145a7746f66Stz204579 		 * nonetheless, see sys/acl.h
3146a7746f66Stz204579 		 */
3147a7746f66Stz204579 		if (ace->a_access_mask & ACE_LIST_DIRECTORY) {
3148a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(READ_DIR_TXT),
3149a7746f66Stz204579 			    &permstr_alloc);
3150a7746f66Stz204579 		}
3151a7746f66Stz204579 		if (ace->a_access_mask & ACE_ADD_FILE) {
3152a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(ADD_FILE_TXT),
3153a7746f66Stz204579 			    &permstr_alloc);
3154a7746f66Stz204579 		}
3155a7746f66Stz204579 		if (ace->a_access_mask & ACE_ADD_SUBDIRECTORY) {
3156a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(ADD_DIR_TXT),
3157a7746f66Stz204579 			    &permstr_alloc);
3158a7746f66Stz204579 		}
3159a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_NAMED_ATTRS) {
3160a7746f66Stz204579 			returnstat = strappend(&permstr,
3161a7746f66Stz204579 			    gettext(READ_XATTR_TXT), &permstr_alloc);
3162a7746f66Stz204579 		}
3163a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_NAMED_ATTRS) {
3164a7746f66Stz204579 			returnstat = strappend(&permstr,
3165a7746f66Stz204579 			    gettext(WRITE_XATTR_TXT), &permstr_alloc);
3166a7746f66Stz204579 		}
3167a7746f66Stz204579 		if (ace->a_access_mask & ACE_EXECUTE) {
3168a7746f66Stz204579 			returnstat = strappend(&permstr,
3169a7746f66Stz204579 			    gettext(EXECUTE_TXT), &permstr_alloc);
3170a7746f66Stz204579 		}
3171a7746f66Stz204579 		if (ace->a_access_mask & ACE_DELETE_CHILD) {
3172a7746f66Stz204579 			returnstat = strappend(&permstr,
3173a7746f66Stz204579 			    gettext(DELETE_CHILD_TXT), &permstr_alloc);
3174a7746f66Stz204579 		}
3175a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_ATTRIBUTES) {
3176a7746f66Stz204579 			returnstat = strappend(&permstr,
3177a7746f66Stz204579 			    gettext(READ_ATTRIBUTES_TXT), &permstr_alloc);
3178a7746f66Stz204579 		}
3179a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_ATTRIBUTES) {
3180a7746f66Stz204579 			returnstat = strappend(&permstr,
3181a7746f66Stz204579 			    gettext(WRITE_ATTRIBUTES_TXT), &permstr_alloc);
3182a7746f66Stz204579 		}
3183a7746f66Stz204579 		if (ace->a_access_mask & ACE_DELETE) {
3184a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(DELETE_TXT),
3185a7746f66Stz204579 			    &permstr_alloc);
3186a7746f66Stz204579 		}
3187a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_ACL) {
3188a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(READ_ACL_TXT),
3189a7746f66Stz204579 			    &permstr_alloc);
3190a7746f66Stz204579 		}
3191a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_ACL) {
3192a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(WRITE_ACL_TXT),
3193a7746f66Stz204579 			    &permstr_alloc);
3194a7746f66Stz204579 		}
3195a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_OWNER) {
3196a7746f66Stz204579 			returnstat = strappend(&permstr,
3197a7746f66Stz204579 			    gettext(WRITE_OWNER_TXT), &permstr_alloc);
3198a7746f66Stz204579 		}
3199a7746f66Stz204579 		if (ace->a_access_mask & ACE_SYNCHRONIZE) {
3200a7746f66Stz204579 			returnstat = strappend(&permstr,
3201a7746f66Stz204579 			    gettext(SYNCHRONIZE_TXT), &permstr_alloc);
3202a7746f66Stz204579 		}
3203a7746f66Stz204579 		if (permstr[strlen(permstr) - 1] == '/')
3204a7746f66Stz204579 			permstr[strlen(permstr) - 1] = '\0';
3205a7746f66Stz204579 		uval.uvaltype = PRA_STRING;
3206a7746f66Stz204579 		uval.string_val = permstr;
3207a7746f66Stz204579 	}
3208a7746f66Stz204579 	if ((permstr == NULL) || (returnstat != 0) ||
3209a7746f66Stz204579 	    (context->format & PRF_RAWM)) {
3210a7746f66Stz204579 		uval.uvaltype = PRA_UINT32;
3211a7746f66Stz204579 		uval.uint32_val = ace->a_access_mask;
3212a7746f66Stz204579 	}
3213a7746f66Stz204579 	returnstat = pa_print(context, &uval, flag);
3214a7746f66Stz204579 
3215a7746f66Stz204579 	if (permstr != NULL)
3216a7746f66Stz204579 		free(permstr);
3217a7746f66Stz204579 	if (returnstat != 0)
3218a7746f66Stz204579 		return (returnstat);
3219a7746f66Stz204579 	return (close_tag(context, TAG_ACEMASK));
3220a7746f66Stz204579 }
3221a7746f66Stz204579 
3222a7746f66Stz204579 static int
3223a7746f66Stz204579 pa_ace_type(pr_context_t *context, ace_t *ace, int status, int flag)
3224a7746f66Stz204579 {
3225a7746f66Stz204579 	int	returnstat;
3226a7746f66Stz204579 	uval_t	uval;
3227a7746f66Stz204579 
3228a7746f66Stz204579 	if (status < 0)
3229a7746f66Stz204579 		return (status);
3230a7746f66Stz204579 
3231a7746f66Stz204579 	/*
3232a7746f66Stz204579 	 * TRANSLATION_NOTE
3233a7746f66Stz204579 	 * ace->a_type refers to access type of ZFS/NFSv4 ACL entry.
3234a7746f66Stz204579 	 */
3235a7746f66Stz204579 	if ((returnstat = open_tag(context, TAG_ACETYPE)) != 0)
3236a7746f66Stz204579 		return (returnstat);
3237a7746f66Stz204579 	if (!(context->format & PRF_RAWM)) {
3238a7746f66Stz204579 		uval.uvaltype = PRA_STRING;
3239a7746f66Stz204579 		switch (ace->a_type) {
3240a7746f66Stz204579 		case ACE_ACCESS_ALLOWED_ACE_TYPE:
3241a7746f66Stz204579 			uval.string_val = gettext(ALLOW_TXT);
3242a7746f66Stz204579 			break;
3243a7746f66Stz204579 		case ACE_ACCESS_DENIED_ACE_TYPE:
3244a7746f66Stz204579 			uval.string_val = gettext(DENY_TXT);
3245a7746f66Stz204579 			break;
3246a7746f66Stz204579 		case ACE_SYSTEM_AUDIT_ACE_TYPE:
3247a7746f66Stz204579 			uval.string_val = gettext(AUDIT_TXT);
3248a7746f66Stz204579 			break;
3249a7746f66Stz204579 		case ACE_SYSTEM_ALARM_ACE_TYPE:
3250a7746f66Stz204579 			uval.string_val = gettext(ALARM_TXT);
3251a7746f66Stz204579 			break;
3252a7746f66Stz204579 		default:
3253a7746f66Stz204579 			uval.string_val = gettext(UNKNOWN_TXT);
3254a7746f66Stz204579 		}
3255a7746f66Stz204579 	} else {
3256a7746f66Stz204579 		uval.uvaltype = PRA_USHORT;
3257a7746f66Stz204579 		uval.uint32_val = ace->a_type;
3258a7746f66Stz204579 	}
3259a7746f66Stz204579 	if ((returnstat = pa_print(context, &uval, flag)) != 0)
3260a7746f66Stz204579 		return (returnstat);
3261a7746f66Stz204579 	return (close_tag(context, TAG_ACETYPE));
3262a7746f66Stz204579 }
3263a7746f66Stz204579 
3264a7746f66Stz204579 int
3265a7746f66Stz204579 pa_ace(pr_context_t *context, int status, int flag)
3266a7746f66Stz204579 {
3267a7746f66Stz204579 	int		returnstat;
3268a7746f66Stz204579 	ace_t		ace;
3269a7746f66Stz204579 
3270a7746f66Stz204579 	if (status < 0)
3271a7746f66Stz204579 		return (status);
3272a7746f66Stz204579 
3273a7746f66Stz204579 	if ((returnstat = pr_adr_u_int32(context, &ace.a_who, 1)) != 0)
3274a7746f66Stz204579 		return (returnstat);
3275a7746f66Stz204579 	if ((returnstat = pr_adr_u_int32(context, &ace.a_access_mask, 1)) != 0)
3276a7746f66Stz204579 		return (returnstat);
3277a7746f66Stz204579 	if ((returnstat = pr_adr_u_short(context, &ace.a_flags, 1)) != 0)
3278a7746f66Stz204579 		return (returnstat);
3279a7746f66Stz204579 	if ((returnstat = pr_adr_u_short(context, &ace.a_type, 1)) != 0)
3280a7746f66Stz204579 		return (returnstat);
3281a7746f66Stz204579 
3282a7746f66Stz204579 	if ((returnstat = pa_ace_flags(context, &ace, returnstat, 0)) != 0)
3283a7746f66Stz204579 		return (returnstat);
3284a7746f66Stz204579 	/* pa_ace_who can returns 1 if uid/gid is not found */
3285a7746f66Stz204579 	if ((returnstat = pa_ace_who(context, &ace, returnstat, 0)) < 0)
3286a7746f66Stz204579 		return (returnstat);
3287a7746f66Stz204579 	if ((returnstat = pa_ace_access_mask(context, &ace,
3288a7746f66Stz204579 	    returnstat, 0)) != 0)
3289a7746f66Stz204579 		return (returnstat);
3290a7746f66Stz204579 	return (pa_ace_type(context, &ace, returnstat, flag));
3291a7746f66Stz204579 }
3292