xref: /titanic_53/usr/src/cmd/praudit/format.c (revision a7746f662862b6ac0a85751d8adbc897743a83e1)
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>
52*a7746f66Stz204579 #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 	char	*p;
16407c478bd9Sstevel@tonic-gate 	short	length;
16417c478bd9Sstevel@tonic-gate 	int	returnstat;
16427c478bd9Sstevel@tonic-gate 	uval_t	uval;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	/*
16457c478bd9Sstevel@tonic-gate 	 * We need to know how much space to allocate for our string, so
16467c478bd9Sstevel@tonic-gate 	 * read the length first, then call pr_adr_char to read those bytes.
16477c478bd9Sstevel@tonic-gate 	 */
16487c478bd9Sstevel@tonic-gate 	if (status < 0)
16497c478bd9Sstevel@tonic-gate 		return (status);
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_short(context, &length, 1)) != 0)
16527c478bd9Sstevel@tonic-gate 		return (returnstat);
16537c478bd9Sstevel@tonic-gate 	if ((c = (char *)malloc(length + 1)) == NULL)
16547c478bd9Sstevel@tonic-gate 		return (-1);
16557c478bd9Sstevel@tonic-gate 	if ((p = (char *)malloc((length * 2) + 1)) == NULL) {
16567c478bd9Sstevel@tonic-gate 		free(c);
16577c478bd9Sstevel@tonic-gate 		return (-1);
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, c, length)) != 0) {
16607c478bd9Sstevel@tonic-gate 		free(c);
16617c478bd9Sstevel@tonic-gate 		free(p);
16627c478bd9Sstevel@tonic-gate 		return (returnstat);
16637c478bd9Sstevel@tonic-gate 	}
16647c478bd9Sstevel@tonic-gate 	convertascii(p, c, length - 1);
16657c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
16667c478bd9Sstevel@tonic-gate 	uval.string_val = p;
16677c478bd9Sstevel@tonic-gate 	returnstat = pa_print(context, &uval, flag);
16687c478bd9Sstevel@tonic-gate 	free(c);
16697c478bd9Sstevel@tonic-gate 	free(p);
16707c478bd9Sstevel@tonic-gate 	return (returnstat);
16717c478bd9Sstevel@tonic-gate }
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate /*
16747c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
16757c478bd9Sstevel@tonic-gate  * pa_file_string: Retrieves a file string from the input stream and prints it
16767c478bd9Sstevel@tonic-gate  *		  if status >= 0
16777c478bd9Sstevel@tonic-gate  * return codes : -1 - error
16787c478bd9Sstevel@tonic-gate  *		:  0 - successful
16797c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
16807c478bd9Sstevel@tonic-gate  */
16817c478bd9Sstevel@tonic-gate int
16827c478bd9Sstevel@tonic-gate pa_file_string(pr_context_t *context, int status, int flag)
16837c478bd9Sstevel@tonic-gate {
16847c478bd9Sstevel@tonic-gate 	char	*c;
16857c478bd9Sstevel@tonic-gate 	char	*p;
16867c478bd9Sstevel@tonic-gate 	short	length;
16877c478bd9Sstevel@tonic-gate 	int	returnstat;
16887c478bd9Sstevel@tonic-gate 	uval_t	uval;
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	/*
16917c478bd9Sstevel@tonic-gate 	 * We need to know how much space to allocate for our string, so
16927c478bd9Sstevel@tonic-gate 	 * read the length first, then call pr_adr_char to read those bytes.
16937c478bd9Sstevel@tonic-gate 	 */
16947c478bd9Sstevel@tonic-gate 	if (status < 0)
16957c478bd9Sstevel@tonic-gate 		return (status);
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_short(context, &length, 1)) != 0)
16987c478bd9Sstevel@tonic-gate 		return (returnstat);
16997c478bd9Sstevel@tonic-gate 	if ((c = (char *)malloc(length + 1)) == NULL)
17007c478bd9Sstevel@tonic-gate 		return (-1);
17017c478bd9Sstevel@tonic-gate 	if ((p = (char *)malloc((length * 2) + 1)) == NULL) {
17027c478bd9Sstevel@tonic-gate 		free(c);
17037c478bd9Sstevel@tonic-gate 		return (-1);
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_char(context, c, length)) != 0) {
17067c478bd9Sstevel@tonic-gate 		free(c);
17077c478bd9Sstevel@tonic-gate 		free(p);
17087c478bd9Sstevel@tonic-gate 		return (returnstat);
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	if (is_file_token(context->tokenid))
17127c478bd9Sstevel@tonic-gate 		context->audit_rec_len += length;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	convertascii(p, c, length - 1);
17157c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_STRING;
17167c478bd9Sstevel@tonic-gate 	uval.string_val = p;
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	if (returnstat == 0)
17197c478bd9Sstevel@tonic-gate 		returnstat = finish_open_tag(context);
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	if (returnstat == 0)
17227c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	free(c);
17257c478bd9Sstevel@tonic-gate 	free(p);
17267c478bd9Sstevel@tonic-gate 	return (returnstat);
17277c478bd9Sstevel@tonic-gate }
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate static int
17307c478bd9Sstevel@tonic-gate pa_putstr_xml(pr_context_t *context, int printable, char *str, size_t len)
17317c478bd9Sstevel@tonic-gate {
17327c478bd9Sstevel@tonic-gate 	int	err;
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	if (!printable) {
17357c478bd9Sstevel@tonic-gate 		/*
17367c478bd9Sstevel@tonic-gate 		 * Unprintable chars should always be converted to the
17377c478bd9Sstevel@tonic-gate 		 * visible form. If there are unprintable characters which
17387c478bd9Sstevel@tonic-gate 		 * require special treatment in xml, those should be
17397c478bd9Sstevel@tonic-gate 		 * handled here.
17407c478bd9Sstevel@tonic-gate 		 */
17417c478bd9Sstevel@tonic-gate 		do {
17427c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "\\%03o",
17437c478bd9Sstevel@tonic-gate 			    (unsigned char)*str++);
17447c478bd9Sstevel@tonic-gate 		} while (err == 0 && --len != 0);
17457c478bd9Sstevel@tonic-gate 		return (err);
17467c478bd9Sstevel@tonic-gate 	}
17477c478bd9Sstevel@tonic-gate 	/* printable characters */
17487c478bd9Sstevel@tonic-gate 	if (len == 1) {
17497c478bd9Sstevel@tonic-gate 		/*
17507c478bd9Sstevel@tonic-gate 		 * check for the special chars only when char size was 1
17517c478bd9Sstevel@tonic-gate 		 * ie, ignore special chars appear in the middle of multibyte
17527c478bd9Sstevel@tonic-gate 		 * sequence.
17537c478bd9Sstevel@tonic-gate 		 */
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 		/* Escape for XML */
17567c478bd9Sstevel@tonic-gate 		switch (*str) {
17577c478bd9Sstevel@tonic-gate 		case '&':
17587c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "%s", "&amp;");
17597c478bd9Sstevel@tonic-gate 			break;
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 		case '<':
17627c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "%s", "&lt;");
17637c478bd9Sstevel@tonic-gate 			break;
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 		case '>':
17667c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "%s", "&gt;");
17677c478bd9Sstevel@tonic-gate 			break;
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 		case '\"':
17707c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "%s", "&quot;");
17717c478bd9Sstevel@tonic-gate 			break;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 		case '\'':
17747c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "%s", "&apos;");
17757c478bd9Sstevel@tonic-gate 			break;
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 		default:
17787c478bd9Sstevel@tonic-gate 			err = pr_putchar(context, *str);
17797c478bd9Sstevel@tonic-gate 			break;
17807c478bd9Sstevel@tonic-gate 		}
17817c478bd9Sstevel@tonic-gate 		return (err);
17827c478bd9Sstevel@tonic-gate 	}
17837c478bd9Sstevel@tonic-gate 	do {
17847c478bd9Sstevel@tonic-gate 		err = pr_putchar(context, *str++);
17857c478bd9Sstevel@tonic-gate 	} while (err == 0 && --len != 0);
17867c478bd9Sstevel@tonic-gate 	return (err);
17877c478bd9Sstevel@tonic-gate }
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate static int
17907c478bd9Sstevel@tonic-gate pa_putstr(pr_context_t *context, int printable, char *str, size_t len)
17917c478bd9Sstevel@tonic-gate {
17927c478bd9Sstevel@tonic-gate 	int	err;
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	if (context->format & PRF_XMLM)
17957c478bd9Sstevel@tonic-gate 		return (pa_putstr_xml(context, printable, str, len));
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	if (!printable) {
17987c478bd9Sstevel@tonic-gate 		do {
17997c478bd9Sstevel@tonic-gate 			err = pr_printf(context, "\\%03o",
18007c478bd9Sstevel@tonic-gate 			    (unsigned char)*str++);
18017c478bd9Sstevel@tonic-gate 		} while (err == 0 && --len != 0);
18027c478bd9Sstevel@tonic-gate 		return (err);
18037c478bd9Sstevel@tonic-gate 	}
18047c478bd9Sstevel@tonic-gate 	do {
18057c478bd9Sstevel@tonic-gate 		err = pr_putchar(context, *str++);
18067c478bd9Sstevel@tonic-gate 	} while (err == 0 && --len != 0);
18077c478bd9Sstevel@tonic-gate 	return (err);
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate int
18117c478bd9Sstevel@tonic-gate pa_string(pr_context_t *context, int status, int flag)
18127c478bd9Sstevel@tonic-gate {
18137c478bd9Sstevel@tonic-gate 	int	rstat, wstat;
18147c478bd9Sstevel@tonic-gate 	int	i, printable, eos;
18157c478bd9Sstevel@tonic-gate 	int	mlen, rlen;
18167c478bd9Sstevel@tonic-gate 	int	mbmax = MB_CUR_MAX;
18177c478bd9Sstevel@tonic-gate 	wchar_t	wc;
18187c478bd9Sstevel@tonic-gate 	char	mbuf[MB_LEN_MAX + 1];
18197c478bd9Sstevel@tonic-gate 	char	c;
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	if (status < 0)
18227c478bd9Sstevel@tonic-gate 		return (status);
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	rstat = wstat = 0;
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	if (mbmax == 1) {
18277c478bd9Sstevel@tonic-gate 		while (wstat == 0) {
18287c478bd9Sstevel@tonic-gate 			if ((rstat = pr_adr_char(context, &c, 1)) < 0)
18297c478bd9Sstevel@tonic-gate 				break;
18307c478bd9Sstevel@tonic-gate 			if (c == '\0')
18317c478bd9Sstevel@tonic-gate 				break;
18327c478bd9Sstevel@tonic-gate 			printable = isprint((unsigned char)c);
18337c478bd9Sstevel@tonic-gate 			wstat = pa_putstr(context, printable, &c, 1);
18347c478bd9Sstevel@tonic-gate 		}
18357c478bd9Sstevel@tonic-gate 		goto done;
18367c478bd9Sstevel@tonic-gate 	}
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	mlen = eos = 0;
18397c478bd9Sstevel@tonic-gate 	while (wstat == 0) {
18407c478bd9Sstevel@tonic-gate 		rlen = 0;
18417c478bd9Sstevel@tonic-gate 		do {
18427c478bd9Sstevel@tonic-gate 			if (!eos) {
18437c478bd9Sstevel@tonic-gate 				rstat = pr_adr_char(context, &c, 1);
18447c478bd9Sstevel@tonic-gate 				if (rstat != 0 || c == '\0')
18457c478bd9Sstevel@tonic-gate 					eos = 1;
18467c478bd9Sstevel@tonic-gate 				else
18477c478bd9Sstevel@tonic-gate 					mbuf[mlen++] = c;
18487c478bd9Sstevel@tonic-gate 			}
18497c478bd9Sstevel@tonic-gate 			rlen = mbtowc(&wc, mbuf, mlen);
18507c478bd9Sstevel@tonic-gate 		} while (!eos && mlen < mbmax && rlen <= 0);
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 		if (mlen == 0)
18537c478bd9Sstevel@tonic-gate 			break;	/* end of string */
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 		if (rlen <= 0) { /* no good sequence */
18567c478bd9Sstevel@tonic-gate 			rlen = 1;
18577c478bd9Sstevel@tonic-gate 			printable = 0;
18587c478bd9Sstevel@tonic-gate 		} else {
18597c478bd9Sstevel@tonic-gate 			printable = iswprint(wc);
18607c478bd9Sstevel@tonic-gate 		}
18617c478bd9Sstevel@tonic-gate 		wstat = pa_putstr(context, printable, mbuf, rlen);
18627c478bd9Sstevel@tonic-gate 		mlen -= rlen;
18637c478bd9Sstevel@tonic-gate 		if (mlen > 0) {
18647c478bd9Sstevel@tonic-gate 			for (i = 0; i < mlen; i++)
18657c478bd9Sstevel@tonic-gate 				mbuf[i] = mbuf[rlen + i];
18667c478bd9Sstevel@tonic-gate 		}
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate done:
18707c478bd9Sstevel@tonic-gate 	if (wstat == 0)
18717c478bd9Sstevel@tonic-gate 		wstat = do_newline(context, flag);
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	if (wstat == 0 && context->data_mode == FILEMODE)
18747c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	return ((rstat != 0 || wstat != 0) ? -1 : 0);
18777c478bd9Sstevel@tonic-gate }
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate /*
18807c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
18817c478bd9Sstevel@tonic-gate  * pa_adr_u_int32: Issues pr_adr_u_int32 to retrieve the next ADR item from
18827c478bd9Sstevel@tonic-gate  *		  the input stream pointed to by audit_adr, and prints it
18837c478bd9Sstevel@tonic-gate  *		  if status = 0
18847c478bd9Sstevel@tonic-gate  * return codes : -1 - error
18857c478bd9Sstevel@tonic-gate  *		:  0 - successful
18867c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
18877c478bd9Sstevel@tonic-gate  */
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate int
18917c478bd9Sstevel@tonic-gate pa_adr_u_int32(pr_context_t *context, int status, int flag)
18927c478bd9Sstevel@tonic-gate {
18937c478bd9Sstevel@tonic-gate 	uint32_t c;
18947c478bd9Sstevel@tonic-gate 	uval_t	uval;
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	if (status >= 0) {
18977c478bd9Sstevel@tonic-gate 		if (pr_adr_u_int32(context, &c, 1) == 0) {
18987c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT32;
18997c478bd9Sstevel@tonic-gate 			uval.uint32_val = c;
19007c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
19017c478bd9Sstevel@tonic-gate 		} else
19027c478bd9Sstevel@tonic-gate 			return (-1);
19037c478bd9Sstevel@tonic-gate 	} else
19047c478bd9Sstevel@tonic-gate 		return (status);
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate /*
19107c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19117c478bd9Sstevel@tonic-gate  * pa_adr_u_int64: Issues pr_adr_u_int64 to retrieve the next ADR item from the
19127c478bd9Sstevel@tonic-gate  *		  input stream pointed to by audit_adr, and prints it
19137c478bd9Sstevel@tonic-gate  *		  if status = 0
19147c478bd9Sstevel@tonic-gate  * return codes : -1 - error
19157c478bd9Sstevel@tonic-gate  *		:  0 - successful
19167c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19177c478bd9Sstevel@tonic-gate  */
19187c478bd9Sstevel@tonic-gate int
19197c478bd9Sstevel@tonic-gate pa_adr_u_int64(pr_context_t *context, int status, int flag)
19207c478bd9Sstevel@tonic-gate {
19217c478bd9Sstevel@tonic-gate 	uint64_t c;
19227c478bd9Sstevel@tonic-gate 	uval_t	uval;
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	if (status >= 0) {
19257c478bd9Sstevel@tonic-gate 		if (pr_adr_u_int64(context, &c, 1) == 0) {
19267c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT64;
19277c478bd9Sstevel@tonic-gate 			uval.uint64_val = c;
19287c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
19297c478bd9Sstevel@tonic-gate 		} else
19307c478bd9Sstevel@tonic-gate 			return (-1);
19317c478bd9Sstevel@tonic-gate 	} else
19327c478bd9Sstevel@tonic-gate 		return (status);
19337c478bd9Sstevel@tonic-gate }
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate /*
19377c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19387c478bd9Sstevel@tonic-gate  * pa_adr_u_short: Issues pr_adr_u_short to retrieve the next ADR item from
19397c478bd9Sstevel@tonic-gate  *			the input stream pointed to by audit_adr, and prints it
19407c478bd9Sstevel@tonic-gate  *			if status = 0
19417c478bd9Sstevel@tonic-gate  * return codes : -1 - error
19427c478bd9Sstevel@tonic-gate  *		:  0 - successful
19437c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19447c478bd9Sstevel@tonic-gate  */
19457c478bd9Sstevel@tonic-gate int
19467c478bd9Sstevel@tonic-gate pa_adr_u_short(pr_context_t *context, int status, int flag)
19477c478bd9Sstevel@tonic-gate {
19487c478bd9Sstevel@tonic-gate 	ushort_t c;
19497c478bd9Sstevel@tonic-gate 	uval_t	uval;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	if (status >= 0) {
19527c478bd9Sstevel@tonic-gate 		if (pr_adr_u_short(context, &c, 1) == 0) {
19537c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_USHORT;
19547c478bd9Sstevel@tonic-gate 			uval.ushort_val = c;
19557c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
19567c478bd9Sstevel@tonic-gate 		} else
19577c478bd9Sstevel@tonic-gate 			return (-1);
19587c478bd9Sstevel@tonic-gate 	} else
19597c478bd9Sstevel@tonic-gate 		return (status);
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate /*
19637c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19647c478bd9Sstevel@tonic-gate  * pa_reclen: Issues pr_adr_u_long to retrieve the length of the record
19657c478bd9Sstevel@tonic-gate  *		  from the input stream pointed to by audit_adr,
19667c478bd9Sstevel@tonic-gate  *		  and prints it (unless format is XML) if status = 0
19677c478bd9Sstevel@tonic-gate  * return codes : -1 - error
19687c478bd9Sstevel@tonic-gate  *		:  0 - successful
19697c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19707c478bd9Sstevel@tonic-gate  */
19717c478bd9Sstevel@tonic-gate int
19727c478bd9Sstevel@tonic-gate pa_reclen(pr_context_t *context, int status)
19737c478bd9Sstevel@tonic-gate {
19747c478bd9Sstevel@tonic-gate 	uint32_t c;
19757c478bd9Sstevel@tonic-gate 	uval_t	uval;
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	if (status >= 0) {
19787c478bd9Sstevel@tonic-gate 		if ((int)pr_adr_u_int32(context, &c, 1) == 0) {
19797c478bd9Sstevel@tonic-gate 			context->audit_rec_len = c;
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 			/* Don't print this for XML format */
19827c478bd9Sstevel@tonic-gate 			if (context->format & PRF_XMLM) {
19837c478bd9Sstevel@tonic-gate 				return (0);
19847c478bd9Sstevel@tonic-gate 			} else {
19857c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_UINT32;
19867c478bd9Sstevel@tonic-gate 				uval.uint32_val = c;
19877c478bd9Sstevel@tonic-gate 				return (pa_print(context, &uval, 0));
19887c478bd9Sstevel@tonic-gate 			}
19897c478bd9Sstevel@tonic-gate 		} else
19907c478bd9Sstevel@tonic-gate 			return (-1);
19917c478bd9Sstevel@tonic-gate 	} else
19927c478bd9Sstevel@tonic-gate 		return (status);
19937c478bd9Sstevel@tonic-gate }
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate /*
19967c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
19977c478bd9Sstevel@tonic-gate  * pa_mode	: Issues pr_adr_u_short to retrieve the next ADR item from
19987c478bd9Sstevel@tonic-gate  *		the input stream pointed to by audit_adr, and prints it
19997c478bd9Sstevel@tonic-gate  *		in octal if status = 0
20007c478bd9Sstevel@tonic-gate  * return codes : -1 - error
20017c478bd9Sstevel@tonic-gate  *		:  0 - successful
20027c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
20037c478bd9Sstevel@tonic-gate  */
20047c478bd9Sstevel@tonic-gate int
20057c478bd9Sstevel@tonic-gate pa_mode(pr_context_t *context, int status, int flag)
20067c478bd9Sstevel@tonic-gate {
20077c478bd9Sstevel@tonic-gate 	uint32_t c;
20087c478bd9Sstevel@tonic-gate 	uval_t	uval;
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	if (status >= 0) {
20117c478bd9Sstevel@tonic-gate 		if (pr_adr_u_int32(context, &c, 1) == 0) {
20127c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_LOCT;
20137c478bd9Sstevel@tonic-gate 			uval.uint32_val = c;
20147c478bd9Sstevel@tonic-gate 			return (pa_print(context, &uval, flag));
20157c478bd9Sstevel@tonic-gate 		} else
20167c478bd9Sstevel@tonic-gate 			return (-1);
20177c478bd9Sstevel@tonic-gate 	} else
20187c478bd9Sstevel@tonic-gate 		return (status);
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
2021*a7746f66Stz204579 static int
2022*a7746f66Stz204579 pa_print_uid(pr_context_t *context, uid_t uid, int status, int flag)
20237c478bd9Sstevel@tonic-gate {
20247c478bd9Sstevel@tonic-gate 	int	returnstat;
20257c478bd9Sstevel@tonic-gate 	struct passwd *pw;
20267c478bd9Sstevel@tonic-gate 	uval_t	uval;
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	if (status < 0)
20297c478bd9Sstevel@tonic-gate 		return (status);
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_RAWM)) {
20327c478bd9Sstevel@tonic-gate 		/* get password file entry */
20337c478bd9Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) == NULL) {
20347c478bd9Sstevel@tonic-gate 			returnstat = 1;
20357c478bd9Sstevel@tonic-gate 		} else {
20367c478bd9Sstevel@tonic-gate 			/* print in ASCII form */
20377c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_STRING;
20387c478bd9Sstevel@tonic-gate 			uval.string_val = pw->pw_name;
20397c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
20407c478bd9Sstevel@tonic-gate 		}
20417c478bd9Sstevel@tonic-gate 	}
20427c478bd9Sstevel@tonic-gate 	/* print in integer form */
20437c478bd9Sstevel@tonic-gate 	if ((context->format & PRF_RAWM) || (returnstat == 1)) {
20447c478bd9Sstevel@tonic-gate 		uval.uvaltype = PRA_INT32;
20457c478bd9Sstevel@tonic-gate 		uval.int32_val = uid;
20467c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
20477c478bd9Sstevel@tonic-gate 	}
20487c478bd9Sstevel@tonic-gate 	return (returnstat);
20497c478bd9Sstevel@tonic-gate }
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate /*
20537c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
2054*a7746f66Stz204579  * pa_pw_uid()	: Issues pr_adr_u_int32 to reads uid from input stream
20557c478bd9Sstevel@tonic-gate  *		pointed to by audit_adr, and displays it in either
20567c478bd9Sstevel@tonic-gate  *		raw form or its ASCII representation, if status >= 0.
20577c478bd9Sstevel@tonic-gate  * return codes : -1 - error
20587c478bd9Sstevel@tonic-gate  * 		:  1 - warning, passwd entry not found
20597c478bd9Sstevel@tonic-gate  *		:  0 - successful
20607c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
20617c478bd9Sstevel@tonic-gate  */
20627c478bd9Sstevel@tonic-gate int
2063*a7746f66Stz204579 pa_pw_uid(pr_context_t *context, int status, int flag)
20647c478bd9Sstevel@tonic-gate {
2065*a7746f66Stz204579 	uint32_t uid;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	if (status < 0)
20687c478bd9Sstevel@tonic-gate 		return (status);
20697c478bd9Sstevel@tonic-gate 
2070*a7746f66Stz204579 	if (pr_adr_u_int32(context, &uid, 1) != 0)
2071*a7746f66Stz204579 		/* cannot retrieve uid */
20727c478bd9Sstevel@tonic-gate 		return (-1);
20737c478bd9Sstevel@tonic-gate 
2074*a7746f66Stz204579 	return (pa_print_uid(context, uid, status, flag));
2075*a7746f66Stz204579 }
2076*a7746f66Stz204579 
2077*a7746f66Stz204579 static int
2078*a7746f66Stz204579 pa_print_gid(pr_context_t *context, gid_t gid, int status, int flag)
2079*a7746f66Stz204579 {
2080*a7746f66Stz204579 	int	returnstat;
2081*a7746f66Stz204579 	struct group *gr;
2082*a7746f66Stz204579 	uval_t	uval;
2083*a7746f66Stz204579 
2084*a7746f66Stz204579 	if (status < 0)
2085*a7746f66Stz204579 		return (status);
2086*a7746f66Stz204579 
20877c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_RAWM)) {
20887c478bd9Sstevel@tonic-gate 		/* get group file entry */
20897c478bd9Sstevel@tonic-gate 		if ((gr = getgrgid(gid)) == NULL) {
20907c478bd9Sstevel@tonic-gate 			returnstat = 1;
20917c478bd9Sstevel@tonic-gate 		} else {
20927c478bd9Sstevel@tonic-gate 			/* print in ASCII form */
20937c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_STRING;
20947c478bd9Sstevel@tonic-gate 			uval.string_val = gr->gr_name;
20957c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
20967c478bd9Sstevel@tonic-gate 		}
20977c478bd9Sstevel@tonic-gate 	}
20987c478bd9Sstevel@tonic-gate 	/* print in integer form */
20997c478bd9Sstevel@tonic-gate 	if ((context->format & PRF_RAWM) || (returnstat == 1)) {
21007c478bd9Sstevel@tonic-gate 		uval.uvaltype = PRA_INT32;
21017c478bd9Sstevel@tonic-gate 		uval.int32_val = gid;
21027c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
21037c478bd9Sstevel@tonic-gate 	}
21047c478bd9Sstevel@tonic-gate 	return (returnstat);
21057c478bd9Sstevel@tonic-gate }
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate /*
21097c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
2110*a7746f66Stz204579  * pa_gr_uid()	: Issues pr_adr_u_int32 to reads group uid from input stream
2111*a7746f66Stz204579  *			pointed to by audit_adr, and displays it in either
2112*a7746f66Stz204579  *			raw form or its ASCII representation, if status >= 0.
2113*a7746f66Stz204579  * return codes : -1 - error
2114*a7746f66Stz204579  * 		:  1 - warning, passwd entry not found
2115*a7746f66Stz204579  *		:  0 - successful
2116*a7746f66Stz204579  * -----------------------------------------------------------------------
2117*a7746f66Stz204579  */
2118*a7746f66Stz204579 int
2119*a7746f66Stz204579 pa_gr_uid(pr_context_t *context, int status, int flag)
2120*a7746f66Stz204579 {
2121*a7746f66Stz204579 	uint32_t gid;
2122*a7746f66Stz204579 
2123*a7746f66Stz204579 	if (status < 0)
2124*a7746f66Stz204579 		return (status);
2125*a7746f66Stz204579 
2126*a7746f66Stz204579 	if (pr_adr_u_int32(context, &gid, 1) != 0)
2127*a7746f66Stz204579 		/* cannot retrieve gid */
2128*a7746f66Stz204579 		return (-1);
2129*a7746f66Stz204579 
2130*a7746f66Stz204579 	return (pa_print_gid(context, gid, status, flag));
2131*a7746f66Stz204579 }
2132*a7746f66Stz204579 
2133*a7746f66Stz204579 
2134*a7746f66Stz204579 /*
2135*a7746f66Stz204579  * -----------------------------------------------------------------------
21367c478bd9Sstevel@tonic-gate  * pa_pw_uid_gr_gid()	: Issues pr_adr_u_int32 to reads uid or group uid
21377c478bd9Sstevel@tonic-gate  *			from input stream
21387c478bd9Sstevel@tonic-gate  *			pointed to by audit_adr, and displays it in either
21397c478bd9Sstevel@tonic-gate  *			raw form or its ASCII representation, if status >= 0.
21407c478bd9Sstevel@tonic-gate  * return codes : -1 - error
21417c478bd9Sstevel@tonic-gate  * 		:  1 - warning, passwd entry not found
21427c478bd9Sstevel@tonic-gate  *		:  0 - successful
21437c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
21447c478bd9Sstevel@tonic-gate  */
21457c478bd9Sstevel@tonic-gate int
21467c478bd9Sstevel@tonic-gate pa_pw_uid_gr_gid(pr_context_t *context, int status, int flag)
21477c478bd9Sstevel@tonic-gate {
21487c478bd9Sstevel@tonic-gate 	int	returnstat;
21497c478bd9Sstevel@tonic-gate 	uint32_t	value;
21507c478bd9Sstevel@tonic-gate 	uval_t		uval;
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	if (status < 0)
21537c478bd9Sstevel@tonic-gate 		return (status);
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	/* get value of a_type */
21567c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int32(context, &value, 1)) != 0)
21577c478bd9Sstevel@tonic-gate 		return (returnstat);
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_ACLTYPE)) != 0)
21607c478bd9Sstevel@tonic-gate 		return (returnstat);
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	uval.uvaltype = PRA_UINT32;
21637c478bd9Sstevel@tonic-gate 	uval.uint32_val = value;
21647c478bd9Sstevel@tonic-gate 	if ((returnstat = pa_print(context, &uval, flag)) != 0)
21657c478bd9Sstevel@tonic-gate 		return (returnstat);
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	if ((returnstat = close_tag(context, TAG_ACLTYPE)) != 0)
21687c478bd9Sstevel@tonic-gate 		return (returnstat);
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_ACLVAL)) != 0)
21717c478bd9Sstevel@tonic-gate 		return (returnstat);
21727c478bd9Sstevel@tonic-gate 	/*
21737c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
21747c478bd9Sstevel@tonic-gate 	 * The "mask" and "other" strings refer to the class mask
21757c478bd9Sstevel@tonic-gate 	 * and other (or world) entries in an ACL.
21767c478bd9Sstevel@tonic-gate 	 * The "unrecognized" string refers to an unrecognized ACL
21777c478bd9Sstevel@tonic-gate 	 * entry.
21787c478bd9Sstevel@tonic-gate 	 */
21797c478bd9Sstevel@tonic-gate 	switch (value) {
21807c478bd9Sstevel@tonic-gate 		case USER_OBJ:
21817c478bd9Sstevel@tonic-gate 		case USER:
21827c478bd9Sstevel@tonic-gate 			returnstat = pa_pw_uid(context, returnstat, flag);
21837c478bd9Sstevel@tonic-gate 			break;
21847c478bd9Sstevel@tonic-gate 		case GROUP_OBJ:
21857c478bd9Sstevel@tonic-gate 		case GROUP:
21867c478bd9Sstevel@tonic-gate 			returnstat = pa_gr_uid(context, returnstat, flag);
21877c478bd9Sstevel@tonic-gate 			break;
21887c478bd9Sstevel@tonic-gate 		case CLASS_OBJ:
21897c478bd9Sstevel@tonic-gate 			returnstat = pr_adr_u_int32(context, &value, 1);
21907c478bd9Sstevel@tonic-gate 			if (returnstat != 0)
21917c478bd9Sstevel@tonic-gate 				return (returnstat);
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 			if (!(context->format & PRF_RAWM)) {
21947c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_STRING;
21957c478bd9Sstevel@tonic-gate 				uval.string_val = gettext("mask");
21967c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
21977c478bd9Sstevel@tonic-gate 			} else {
21987c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_UINT32;
21997c478bd9Sstevel@tonic-gate 				uval.uint32_val = value;
22008f9294f3Sgww 				if ((returnstat =
22018f9294f3Sgww 				    pa_print(context, &uval, flag)) != 0) {
22027c478bd9Sstevel@tonic-gate 					return (returnstat);
22037c478bd9Sstevel@tonic-gate 				}
22048f9294f3Sgww 			}
22057c478bd9Sstevel@tonic-gate 			break;
22067c478bd9Sstevel@tonic-gate 		case OTHER_OBJ:
22077c478bd9Sstevel@tonic-gate 			returnstat = pr_adr_u_int32(context, &value, 1);
22087c478bd9Sstevel@tonic-gate 			if (returnstat != 0)
22097c478bd9Sstevel@tonic-gate 				return (returnstat);
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 			if (!(context->format & PRF_RAWM)) {
22127c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_STRING;
22137c478bd9Sstevel@tonic-gate 				uval.string_val = gettext("other");
22147c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
22157c478bd9Sstevel@tonic-gate 			} else {
22167c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_UINT32;
22177c478bd9Sstevel@tonic-gate 				uval.uint32_val = value;
22188f9294f3Sgww 				if ((returnstat =
22198f9294f3Sgww 				    pa_print(context, &uval, flag)) != 0) {
22207c478bd9Sstevel@tonic-gate 					return (returnstat);
22217c478bd9Sstevel@tonic-gate 				}
22228f9294f3Sgww 			}
22237c478bd9Sstevel@tonic-gate 			break;
22247c478bd9Sstevel@tonic-gate 		default:
22257c478bd9Sstevel@tonic-gate 			returnstat = pr_adr_u_int32(context, &value, 1);
22267c478bd9Sstevel@tonic-gate 			if (returnstat != 0)
22277c478bd9Sstevel@tonic-gate 				return (returnstat);
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 			if (!(context->format & PRF_RAWM)) {
22307c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_STRING;
22317c478bd9Sstevel@tonic-gate 				uval.string_val = gettext("unrecognized");
22327c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
22337c478bd9Sstevel@tonic-gate 			} else {
22347c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_UINT32;
22357c478bd9Sstevel@tonic-gate 				uval.uint32_val = value;
22368f9294f3Sgww 				if ((returnstat =
22378f9294f3Sgww 				    pa_print(context, &uval, flag)) != 0) {
22387c478bd9Sstevel@tonic-gate 					return (returnstat);
22397c478bd9Sstevel@tonic-gate 				}
22407c478bd9Sstevel@tonic-gate 			}
22418f9294f3Sgww 	}
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 	if ((returnstat = close_tag(context, TAG_ACLVAL)) != 0)
22447c478bd9Sstevel@tonic-gate 		return (returnstat);
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 	return (returnstat);
22477c478bd9Sstevel@tonic-gate }
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate /*
22517c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
22527c478bd9Sstevel@tonic-gate  * pa_event_modifier(): Issues pr_adr_u_short to retrieve the next ADR item from
22537c478bd9Sstevel@tonic-gate  *		  the input stream pointed to by audit_adr.  This is the
22547c478bd9Sstevel@tonic-gate  *		  event type, and is displayed in hex;
22557c478bd9Sstevel@tonic-gate  * return codes : -1 - error
22567c478bd9Sstevel@tonic-gate  *		:  0 - successful
22577c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
22587c478bd9Sstevel@tonic-gate  */
22597c478bd9Sstevel@tonic-gate int
22607c478bd9Sstevel@tonic-gate pa_event_modifier(pr_context_t *context, int status,  int flag)
22617c478bd9Sstevel@tonic-gate {
22627c478bd9Sstevel@tonic-gate 	int	returnstat;
22637c478bd9Sstevel@tonic-gate 	ushort_t emodifier;
22647c478bd9Sstevel@tonic-gate 	uval_t	uval;
22657c478bd9Sstevel@tonic-gate 	char	modstring[64];
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	if (status < 0)
22687c478bd9Sstevel@tonic-gate 		return (status);
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_short(context, &emodifier, 1)) != 0)
22717c478bd9Sstevel@tonic-gate 		return (returnstat);
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 	/* For XML, only print when modifier is non-zero */
22747c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_XMLM) || (emodifier != 0)) {
22757c478bd9Sstevel@tonic-gate 		uval.uvaltype = PRA_STRING;
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 		returnstat = open_tag(context, TAG_EVMOD);
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 		if (returnstat >= 0) {
22807c478bd9Sstevel@tonic-gate 			if (!(context->format & PRF_RAWM)) {
22817c478bd9Sstevel@tonic-gate 				eventmodifier2string(emodifier, modstring,
22827c478bd9Sstevel@tonic-gate 				    sizeof (modstring));
22837c478bd9Sstevel@tonic-gate 				uval.string_val = modstring;
22847c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
22857c478bd9Sstevel@tonic-gate 			} else {
22867c478bd9Sstevel@tonic-gate 				uval.string_val = hexconvert((char *)&emodifier,
22877c478bd9Sstevel@tonic-gate 				    sizeof (emodifier), sizeof (emodifier));
22887c478bd9Sstevel@tonic-gate 				if (uval.string_val) {
22897c478bd9Sstevel@tonic-gate 					returnstat = pa_print(context, &uval,
22907c478bd9Sstevel@tonic-gate 					    flag);
22917c478bd9Sstevel@tonic-gate 					free(uval.string_val);
22927c478bd9Sstevel@tonic-gate 				}
22937c478bd9Sstevel@tonic-gate 			}
22947c478bd9Sstevel@tonic-gate 		}
22957c478bd9Sstevel@tonic-gate 		if (returnstat >= 0)
22967c478bd9Sstevel@tonic-gate 			returnstat = close_tag(context, TAG_EVMOD);
22977c478bd9Sstevel@tonic-gate 	}
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 	return (returnstat);
23007c478bd9Sstevel@tonic-gate }
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate /*
23047c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
23057c478bd9Sstevel@tonic-gate  * pa_event_type(): Issues pr_adr_u_short to retrieve the next ADR item from
23067c478bd9Sstevel@tonic-gate  *		  the input stream pointed to by audit_adr.  This is the
23077c478bd9Sstevel@tonic-gate  *		  event type, and is displayed in either raw or
23087c478bd9Sstevel@tonic-gate  *		  ASCII form as appropriate
23097c478bd9Sstevel@tonic-gate  * return codes : -1 - error
23107c478bd9Sstevel@tonic-gate  *		:  0 - successful
23117c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
23127c478bd9Sstevel@tonic-gate  */
23137c478bd9Sstevel@tonic-gate int
23147c478bd9Sstevel@tonic-gate pa_event_type(pr_context_t *context, int status,  int flag)
23157c478bd9Sstevel@tonic-gate {
23167c478bd9Sstevel@tonic-gate 	ushort_t etype;
23177c478bd9Sstevel@tonic-gate 	int	returnstat;
23187c478bd9Sstevel@tonic-gate 	au_event_ent_t *p_event = NULL;
23197c478bd9Sstevel@tonic-gate 	uval_t	uval;
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	if (status >= 0) {
23227c478bd9Sstevel@tonic-gate 		if ((returnstat = pr_adr_u_short(context, &etype, 1)) == 0) {
23237c478bd9Sstevel@tonic-gate 			if (!(context->format & PRF_RAWM)) {
23247c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_STRING;
23257c478bd9Sstevel@tonic-gate 				if (context->format & PRF_NOCACHE) {
23267c478bd9Sstevel@tonic-gate 					p_event = getauevnum(etype);
23277c478bd9Sstevel@tonic-gate 				} else {
23287c478bd9Sstevel@tonic-gate 					(void) cacheauevent(&p_event, etype);
23297c478bd9Sstevel@tonic-gate 				}
23307c478bd9Sstevel@tonic-gate 				if (p_event != NULL) {
23317c478bd9Sstevel@tonic-gate 					if (context->format & PRF_SHORTM)
23327c478bd9Sstevel@tonic-gate 						uval.string_val =
23337c478bd9Sstevel@tonic-gate 						    p_event->ae_name;
23347c478bd9Sstevel@tonic-gate 					else
23357c478bd9Sstevel@tonic-gate 						uval.string_val =
23367c478bd9Sstevel@tonic-gate 						    p_event->ae_desc;
23377c478bd9Sstevel@tonic-gate 				} else {
23387c478bd9Sstevel@tonic-gate 					uval.string_val =
23397c478bd9Sstevel@tonic-gate 					    gettext("invalid event number");
23407c478bd9Sstevel@tonic-gate 				}
23417c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
23427c478bd9Sstevel@tonic-gate 			} else {
23437c478bd9Sstevel@tonic-gate 				uval.uvaltype = PRA_USHORT;
23447c478bd9Sstevel@tonic-gate 				uval.ushort_val = etype;
23457c478bd9Sstevel@tonic-gate 				returnstat = pa_print(context, &uval, flag);
23467c478bd9Sstevel@tonic-gate 			}
23477c478bd9Sstevel@tonic-gate 		}
23487c478bd9Sstevel@tonic-gate 		return (returnstat);
23497c478bd9Sstevel@tonic-gate 	} else
23507c478bd9Sstevel@tonic-gate 		return (status);
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate }
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate /*
23567c478bd9Sstevel@tonic-gate  * Print time from struct timeval to millisecond resolution.
23577c478bd9Sstevel@tonic-gate  *
23587c478bd9Sstevel@tonic-gate  *	typedef long	time_t;		time of day in seconds
23597c478bd9Sstevel@tonic-gate  *	typedef	long	useconds_t;	signed # of microseconds
23607c478bd9Sstevel@tonic-gate  *
23617c478bd9Sstevel@tonic-gate  * struct timeval {
23627c478bd9Sstevel@tonic-gate  *	time_t		tv_sec;		seconds
23637c478bd9Sstevel@tonic-gate  *	suseconds_t	tv_usec;	and microseconds
23647c478bd9Sstevel@tonic-gate  * };
23657c478bd9Sstevel@tonic-gate  */
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate int
23687c478bd9Sstevel@tonic-gate pa_utime32(pr_context_t *context, int status, int flag)
23697c478bd9Sstevel@tonic-gate {
23707c478bd9Sstevel@tonic-gate 	uint32_t scale = 1000;		/* usec to msec */
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 	return (do_mtime32(context, status, flag, scale));
23737c478bd9Sstevel@tonic-gate }
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate /*
23767c478bd9Sstevel@tonic-gate  * Print time from timestruc_t to millisecond resolution.
23777c478bd9Sstevel@tonic-gate  *
23787c478bd9Sstevel@tonic-gate  *	typedef struct timespec timestruct_t;
23797c478bd9Sstevel@tonic-gate  * struct timespec{
23807c478bd9Sstevel@tonic-gate  *	time_t	tv_sec;		seconds
23817c478bd9Sstevel@tonic-gate  *	long	tv_nsec;	and nanoseconds
23827c478bd9Sstevel@tonic-gate  * };
23837c478bd9Sstevel@tonic-gate  */
23847c478bd9Sstevel@tonic-gate int
23857c478bd9Sstevel@tonic-gate pa_ntime32(pr_context_t *context, int status, int flag)
23867c478bd9Sstevel@tonic-gate {
23877c478bd9Sstevel@tonic-gate 	uint32_t scale = 1000000;	/* nsec to msec */
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 	return (do_mtime32(context, status, flag, scale));
23907c478bd9Sstevel@tonic-gate }
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate /*
23937c478bd9Sstevel@tonic-gate  * Format the timezone +/- HH:MM and terminate the string
23947c478bd9Sstevel@tonic-gate  * Note tm and tv_sec are the same time.
23957c478bd9Sstevel@tonic-gate  * Too bad strftime won't produce an ISO 8601 time zone numeric
23967c478bd9Sstevel@tonic-gate  */
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate #define	MINS	(24L * 60)
23997c478bd9Sstevel@tonic-gate static void
24007c478bd9Sstevel@tonic-gate tzone(struct tm *tm, time_t *tv_sec, char *p)
24017c478bd9Sstevel@tonic-gate {
24027c478bd9Sstevel@tonic-gate 	struct tm *gmt;
24037c478bd9Sstevel@tonic-gate 	int min_off;
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 	gmt = gmtime(tv_sec);
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	min_off = ((tm->tm_hour - gmt->tm_hour) * 60) +
24087c478bd9Sstevel@tonic-gate 	    (tm->tm_min - gmt->tm_min);
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	if (tm->tm_year < gmt->tm_year)		/* cross new year */
24117c478bd9Sstevel@tonic-gate 		min_off -= MINS;
24127c478bd9Sstevel@tonic-gate 	else if (tm->tm_year > gmt->tm_year)
24137c478bd9Sstevel@tonic-gate 		min_off += MINS;
24147c478bd9Sstevel@tonic-gate 	else if (tm->tm_yday < gmt->tm_yday)	/* cross dateline */
24157c478bd9Sstevel@tonic-gate 		min_off -= MINS;
24167c478bd9Sstevel@tonic-gate 	else if (tm->tm_yday > gmt->tm_yday)
24177c478bd9Sstevel@tonic-gate 		min_off += MINS;
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	if (min_off < 0) {
24207c478bd9Sstevel@tonic-gate 		min_off = -min_off;
24217c478bd9Sstevel@tonic-gate 		*p++ = '-';
24227c478bd9Sstevel@tonic-gate 	} else {
24237c478bd9Sstevel@tonic-gate 		*p++ = '+';
24247c478bd9Sstevel@tonic-gate 	}
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	*p++ = min_off / 600 + '0';		/* 10s of hours */
24277c478bd9Sstevel@tonic-gate 	min_off = min_off - min_off / 600 * 600;
24287c478bd9Sstevel@tonic-gate 	*p++ = min_off / 60 % 10 + '0';		/* hours */
24297c478bd9Sstevel@tonic-gate 	min_off = min_off - min_off / 60 * 60;
24307c478bd9Sstevel@tonic-gate 	*p++ = ':';
24317c478bd9Sstevel@tonic-gate 	*p++ = min_off / 10 + '0';		/* 10s of minutes */
24327c478bd9Sstevel@tonic-gate 	*p++ = min_off % 10 + '0';		/* minutes */
24337c478bd9Sstevel@tonic-gate 	*p = '\0';
24347c478bd9Sstevel@tonic-gate }
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate /*
24377c478bd9Sstevel@tonic-gate  * Format the milliseconds in place in the string.
24387c478bd9Sstevel@tonic-gate  * Borrowed from strftime.c:itoa()
24397c478bd9Sstevel@tonic-gate  */
24407c478bd9Sstevel@tonic-gate static void
24417c478bd9Sstevel@tonic-gate msec32(uint32_t msec, char *p)
24427c478bd9Sstevel@tonic-gate {
24437c478bd9Sstevel@tonic-gate 	*p++ = msec / 100 + '0';
24447c478bd9Sstevel@tonic-gate 	msec  = msec - msec / 100 * 100;
24457c478bd9Sstevel@tonic-gate 	*p++ = msec / 10 + '0';
24467c478bd9Sstevel@tonic-gate 	*p++ = msec % 10 +'0';
24477c478bd9Sstevel@tonic-gate }
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate /*
24507c478bd9Sstevel@tonic-gate  * Format time and print relative to scale factor from micro/nano seconds.
24517c478bd9Sstevel@tonic-gate  */
24527c478bd9Sstevel@tonic-gate static int
24537c478bd9Sstevel@tonic-gate do_mtime32(pr_context_t *context, int status, int flag, uint32_t scale)
24547c478bd9Sstevel@tonic-gate {
24557c478bd9Sstevel@tonic-gate 	uint32_t t32;
24567c478bd9Sstevel@tonic-gate 	time_t tv_sec;
24577c478bd9Sstevel@tonic-gate 	struct tm tm;
24587c478bd9Sstevel@tonic-gate 	char	time_created[sizeof ("YYYY-MM-DD HH:MM:SS.sss -HH:MM")];
24597c478bd9Sstevel@tonic-gate 	int	returnstat;
24607c478bd9Sstevel@tonic-gate 	uval_t	uval;
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	if (status < 0)
24637c478bd9Sstevel@tonic-gate 		return (status);
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_ISO)) != 0)
24667c478bd9Sstevel@tonic-gate 		return (returnstat);
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int32(context,
24697c478bd9Sstevel@tonic-gate 	    (uint32_t *)&tv_sec, 1)) != 0)
24707c478bd9Sstevel@tonic-gate 		return (returnstat);
24717c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int32(context, &t32, 1)) == 0) {
24727c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
24737c478bd9Sstevel@tonic-gate 			(void) localtime_r(&tv_sec, &tm);
24747c478bd9Sstevel@tonic-gate 			(void) strftime(time_created,
24757c478bd9Sstevel@tonic-gate 			    sizeof ("YYYY-MM-DD HH:MM:SS.xxx "),
24767c478bd9Sstevel@tonic-gate 			    "%Y-%m-%d %H:%M:%S.xxx ", &tm);
24777c478bd9Sstevel@tonic-gate 			msec32(t32/scale,
24787c478bd9Sstevel@tonic-gate 			    &time_created[sizeof ("YYYY-MM-DD HH:MM:SS.")-1]);
24797c478bd9Sstevel@tonic-gate 			tzone(&tm, &tv_sec,
24807c478bd9Sstevel@tonic-gate 			    &time_created[
24817c478bd9Sstevel@tonic-gate 			    sizeof ("YYYY-MM-DD HH:MM:SS.xxx ")-1]);
24827c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_STRING;
24837c478bd9Sstevel@tonic-gate 			uval.string_val = time_created;
24847c478bd9Sstevel@tonic-gate 		} else {
24857c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT32;
24867c478bd9Sstevel@tonic-gate 			uval.uint32_val = (uint32_t)tv_sec;
24877c478bd9Sstevel@tonic-gate 			(void) pa_print(context, &uval, 0);
24887c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT32;
24897c478bd9Sstevel@tonic-gate 			uval.uint32_val = t32;
24907c478bd9Sstevel@tonic-gate 		}
24917c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
24927c478bd9Sstevel@tonic-gate 	}
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 	if (returnstat == 0)
24957c478bd9Sstevel@tonic-gate 		return (close_tag(context, TAG_ISO));
24967c478bd9Sstevel@tonic-gate 	else
24977c478bd9Sstevel@tonic-gate 		return (returnstat);
24987c478bd9Sstevel@tonic-gate }
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate /*
25017c478bd9Sstevel@tonic-gate  * Print time from struct timeval to millisecond resolution.
25027c478bd9Sstevel@tonic-gate  *
25037c478bd9Sstevel@tonic-gate  *	typedef long	time_t;		time of day in seconds
25047c478bd9Sstevel@tonic-gate  *	typedef	long	useconds_t;	signed # of microseconds
25057c478bd9Sstevel@tonic-gate  *
25067c478bd9Sstevel@tonic-gate  * struct timeval {
25077c478bd9Sstevel@tonic-gate  *	time_t		tv_sec;		seconds
25087c478bd9Sstevel@tonic-gate  *	suseconds_t	tv_usec;	and microseconds
25097c478bd9Sstevel@tonic-gate  * };
25107c478bd9Sstevel@tonic-gate  */
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate int
25137c478bd9Sstevel@tonic-gate pa_utime64(pr_context_t *context, int status, int flag)
25147c478bd9Sstevel@tonic-gate {
25157c478bd9Sstevel@tonic-gate 	uint64_t scale = 1000;		/* usec to msec */
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	return (do_mtime64(context, status, flag, scale));
25187c478bd9Sstevel@tonic-gate }
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate /*
25217c478bd9Sstevel@tonic-gate  * Print time from timestruc_t to millisecond resolution.
25227c478bd9Sstevel@tonic-gate  *
25237c478bd9Sstevel@tonic-gate  *	typedef struct timespec timestruct_t;
25247c478bd9Sstevel@tonic-gate  * struct timespec{
25257c478bd9Sstevel@tonic-gate  *	time_t	tv_sec;		seconds
25267c478bd9Sstevel@tonic-gate  *	long	tv_nsec;	and nanoseconds
25277c478bd9Sstevel@tonic-gate  * };
25287c478bd9Sstevel@tonic-gate  */
25297c478bd9Sstevel@tonic-gate int
25307c478bd9Sstevel@tonic-gate pa_ntime64(pr_context_t *context, int status, int flag)
25317c478bd9Sstevel@tonic-gate {
25327c478bd9Sstevel@tonic-gate 	uint64_t scale = 1000000;	/* nsec to msec */
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate 	return (do_mtime64(context, status, flag, scale));
25357c478bd9Sstevel@tonic-gate }
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate /*
25387c478bd9Sstevel@tonic-gate  * Format the milliseconds in place in the string.
25397c478bd9Sstevel@tonic-gate  * Borrowed from strftime.c:itoa()
25407c478bd9Sstevel@tonic-gate  */
25417c478bd9Sstevel@tonic-gate static void
25427c478bd9Sstevel@tonic-gate msec64(uint64_t msec, char *p)
25437c478bd9Sstevel@tonic-gate {
25447c478bd9Sstevel@tonic-gate 	*p++ = msec / 100 + '0';
25457c478bd9Sstevel@tonic-gate 	msec = msec - msec / 100 * 100;
25467c478bd9Sstevel@tonic-gate 	*p++ = msec / 10 + '0';
25477c478bd9Sstevel@tonic-gate 	*p++ = msec % 10 +'0';
25487c478bd9Sstevel@tonic-gate }
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate /*
25517c478bd9Sstevel@tonic-gate  * Format time and print relative to scale factor from micro/nano seconds.
25527c478bd9Sstevel@tonic-gate  */
25537c478bd9Sstevel@tonic-gate static int
25547c478bd9Sstevel@tonic-gate do_mtime64(pr_context_t *context, int status, int flag, uint64_t scale)
25557c478bd9Sstevel@tonic-gate {
25567c478bd9Sstevel@tonic-gate 	uint64_t t64_sec;
25577c478bd9Sstevel@tonic-gate 	uint64_t t64_msec;
25587c478bd9Sstevel@tonic-gate 	time_t tv_sec;
25597c478bd9Sstevel@tonic-gate 	struct tm tm;
25607c478bd9Sstevel@tonic-gate 	char	time_created[sizeof ("YYYY-MM-DD HH:MM:SS.sss -HH:MM")];
25617c478bd9Sstevel@tonic-gate 	int	returnstat;
25627c478bd9Sstevel@tonic-gate 	uval_t	uval;
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 	if (status < 0)
25657c478bd9Sstevel@tonic-gate 		return (status);
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	if ((returnstat = open_tag(context, TAG_ISO)) != 0)
25687c478bd9Sstevel@tonic-gate 		return (returnstat);
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int64(context, &t64_sec, 1)) != 0)
25717c478bd9Sstevel@tonic-gate 		return (returnstat);
25727c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_u_int64(context, &t64_msec, 1)) == 0) {
25737c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
25747c478bd9Sstevel@tonic-gate #ifndef	_LP64
25757c478bd9Sstevel@tonic-gate 			/*
25767c478bd9Sstevel@tonic-gate 			 * N.B.
25777c478bd9Sstevel@tonic-gate 			 * This fails for years from 2038
25787c478bd9Sstevel@tonic-gate 			 * The Y2K+38 problem
25797c478bd9Sstevel@tonic-gate 			 */
25807c478bd9Sstevel@tonic-gate #endif	/* !_LP64 */
25817c478bd9Sstevel@tonic-gate 			tv_sec = (time_t)t64_sec;
25827c478bd9Sstevel@tonic-gate 			(void) localtime_r(&tv_sec, &tm);
25837c478bd9Sstevel@tonic-gate 			(void) strftime(time_created,
25847c478bd9Sstevel@tonic-gate 			    sizeof ("YYYY-MM-DD HH:MM:SS.xxx "),
25857c478bd9Sstevel@tonic-gate 			    "%Y-%m-%d %H:%M:%S.xxx ", &tm);
25867c478bd9Sstevel@tonic-gate 			msec64(t64_msec/scale,
25877c478bd9Sstevel@tonic-gate 			    &time_created[sizeof ("YYYY-MM-DD HH:MM:SS.")-1]);
25887c478bd9Sstevel@tonic-gate 			tzone(&tm, &tv_sec,
25897c478bd9Sstevel@tonic-gate 			    &time_created[
25907c478bd9Sstevel@tonic-gate 			    sizeof ("YYYY-MM-DD HH:MM:SS.xxx ")-1]);
25917c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_STRING;
25927c478bd9Sstevel@tonic-gate 			uval.string_val = time_created;
25937c478bd9Sstevel@tonic-gate 		} else {
25947c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT64;
25957c478bd9Sstevel@tonic-gate 			uval.uint64_val = t64_sec;
25967c478bd9Sstevel@tonic-gate 			(void) pa_print(context, &uval, 0);
25977c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT64;
25987c478bd9Sstevel@tonic-gate 			uval.uint64_val = t64_msec;
25997c478bd9Sstevel@tonic-gate 		}
26007c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
26017c478bd9Sstevel@tonic-gate 	}
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	if (returnstat < 0)
26047c478bd9Sstevel@tonic-gate 		return (returnstat);
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 	return (close_tag(context, TAG_ISO));
26077c478bd9Sstevel@tonic-gate }
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate /*
26107c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
26117c478bd9Sstevel@tonic-gate  * pa_error()   :  convert the return token error code.
26127c478bd9Sstevel@tonic-gate  *
26137c478bd9Sstevel@tonic-gate  * output	: buf string representing return token error code.
26147c478bd9Sstevel@tonic-gate  *
26157c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
26167c478bd9Sstevel@tonic-gate  */
26177c478bd9Sstevel@tonic-gate void
26187c478bd9Sstevel@tonic-gate pa_error(const uchar_t err, char *buf, size_t buflen)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	if (err == 0) {
26217c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf, gettext("success"), buflen);
26227c478bd9Sstevel@tonic-gate 	} else if ((char)err == -1) {
26237c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf, gettext("failure"), buflen);
26247c478bd9Sstevel@tonic-gate 	} else {
26257c478bd9Sstevel@tonic-gate 		char *emsg = strerror(err);
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 		if (emsg != NULL) {
26287c478bd9Sstevel@tonic-gate 			(void) strlcpy(buf, gettext("failure: "), buflen);
26297c478bd9Sstevel@tonic-gate 			(void) strlcat(buf, emsg, buflen);
26307c478bd9Sstevel@tonic-gate 		} else {
26317c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, buflen, "%s%d",
26327c478bd9Sstevel@tonic-gate 			    gettext("failure: "), err);
26337c478bd9Sstevel@tonic-gate 		}
26347c478bd9Sstevel@tonic-gate 	}
26357c478bd9Sstevel@tonic-gate }
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate /*
26387c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
26397c478bd9Sstevel@tonic-gate  * pa_retval()   :  convert the return token return value code.
26407c478bd9Sstevel@tonic-gate  *
26417c478bd9Sstevel@tonic-gate  * output	: buf string representing return token error code.
26427c478bd9Sstevel@tonic-gate  *
26437c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
26447c478bd9Sstevel@tonic-gate  */
26457c478bd9Sstevel@tonic-gate void
26467c478bd9Sstevel@tonic-gate pa_retval(const int32_t retval, char *buf, size_t buflen)
26477c478bd9Sstevel@tonic-gate {
26487c478bd9Sstevel@tonic-gate 	struct msg_text	*msglist = &adt_msg_text[ADT_LIST_FAIL_VALUE];
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 	if ((retval + msglist->ml_offset >= msglist->ml_min_index) &&
26517c478bd9Sstevel@tonic-gate 	    (retval + msglist->ml_offset <= msglist->ml_max_index)) {
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf,
26547c478bd9Sstevel@tonic-gate 		    gettext(msglist->ml_msg_list[retval + msglist->ml_offset]),
26557c478bd9Sstevel@tonic-gate 		    buflen);
26567c478bd9Sstevel@tonic-gate 	} else if ((retval >= ADT_FAIL_PAM) &&
26577c478bd9Sstevel@tonic-gate 	    (retval < ADT_FAIL_PAM + PAM_TOTAL_ERRNUM))
26587c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf, pam_strerror(NULL, retval - ADT_FAIL_PAM),
26597c478bd9Sstevel@tonic-gate 		    buflen);
26607c478bd9Sstevel@tonic-gate 	else
26617c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, buflen, "%d", retval);
26627c478bd9Sstevel@tonic-gate }
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate /*
26667c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
26677c478bd9Sstevel@tonic-gate  * pa_printstr()	:  print a given string, translating unprintables
26687c478bd9Sstevel@tonic-gate  *			:  as needed.
26697c478bd9Sstevel@tonic-gate  */
26707c478bd9Sstevel@tonic-gate static int
26717c478bd9Sstevel@tonic-gate pa_printstr(pr_context_t *context, char *str)
26727c478bd9Sstevel@tonic-gate {
26737c478bd9Sstevel@tonic-gate 	int	err = 0;
26747c478bd9Sstevel@tonic-gate 	int	len, printable;
26757c478bd9Sstevel@tonic-gate 	int	mbmax = MB_CUR_MAX;
26767c478bd9Sstevel@tonic-gate 	wchar_t	wc;
26777c478bd9Sstevel@tonic-gate 	char	c;
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 	if (mbmax == 1) {
26807c478bd9Sstevel@tonic-gate 		/* fast path */
26817c478bd9Sstevel@tonic-gate 		while (err == 0 && *str != '\0') {
26827c478bd9Sstevel@tonic-gate 			c = *str++;
26837c478bd9Sstevel@tonic-gate 			printable = isprint((unsigned char)c);
26847c478bd9Sstevel@tonic-gate 			err = pa_putstr(context, printable, &c, 1);
26857c478bd9Sstevel@tonic-gate 		}
26867c478bd9Sstevel@tonic-gate 		return (err);
26877c478bd9Sstevel@tonic-gate 	}
26887c478bd9Sstevel@tonic-gate 	while (err == 0 && *str != '\0') {
26897c478bd9Sstevel@tonic-gate 		len = mbtowc(&wc, str, mbmax);
26907c478bd9Sstevel@tonic-gate 		if (len <= 0) {
26917c478bd9Sstevel@tonic-gate 			len = 1;
26927c478bd9Sstevel@tonic-gate 			printable = 0;
26937c478bd9Sstevel@tonic-gate 		} else {
26947c478bd9Sstevel@tonic-gate 			printable = iswprint(wc);
26957c478bd9Sstevel@tonic-gate 		}
26967c478bd9Sstevel@tonic-gate 		err = pa_putstr(context, printable, str, len);
26977c478bd9Sstevel@tonic-gate 		str += len;
26987c478bd9Sstevel@tonic-gate 	}
26997c478bd9Sstevel@tonic-gate 	return (err);
27007c478bd9Sstevel@tonic-gate }
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate /*
27037c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
27047c478bd9Sstevel@tonic-gate  * pa_print()	:  print as one str or formatted for easy reading.
27057c478bd9Sstevel@tonic-gate  * 		: flag - indicates whether to output a new line for
27067c478bd9Sstevel@tonic-gate  *		: multi-line output.
27077c478bd9Sstevel@tonic-gate  * 		:		= 0; no new line
27087c478bd9Sstevel@tonic-gate  *		:		= 1; new line if regular output
27097c478bd9Sstevel@tonic-gate  * output	: The audit record information is displayed in the
27107c478bd9Sstevel@tonic-gate  *		  type specified by uvaltype and value specified in
27117c478bd9Sstevel@tonic-gate  *		  uval.  The printing of the delimiter or newline is
27127c478bd9Sstevel@tonic-gate  *		  determined by PRF_ONELINE, and the flag value,
27137c478bd9Sstevel@tonic-gate  *		  as follows:
27147c478bd9Sstevel@tonic-gate  *			+--------+------+------+-----------------+
27157c478bd9Sstevel@tonic-gate  *			|ONELINE | flag | last | Action          |
27167c478bd9Sstevel@tonic-gate  *			+--------+------+------+-----------------+
27177c478bd9Sstevel@tonic-gate  *			|    Y   |   Y  |   T  | print new line  |
27187c478bd9Sstevel@tonic-gate  *			|    Y   |   Y  |   F  | print delimiter |
27197c478bd9Sstevel@tonic-gate  *			|    Y   |   N  |   T  | print new line  |
27207c478bd9Sstevel@tonic-gate  *			|    Y   |   N  |   F  | print delimiter |
27217c478bd9Sstevel@tonic-gate  *			|    N   |   Y  |   T  | print new line  |
27227c478bd9Sstevel@tonic-gate  *			|    N   |   Y  |   F  | print new line  |
27237c478bd9Sstevel@tonic-gate  *			|    N   |   N  |   T  | print new line  |
27247c478bd9Sstevel@tonic-gate  *			|    N   |   N  |   F  | print delimiter |
27257c478bd9Sstevel@tonic-gate  *			+--------+------+------+-----------------+
27267c478bd9Sstevel@tonic-gate  *
27277c478bd9Sstevel@tonic-gate  * return codes : -1 - error
27287c478bd9Sstevel@tonic-gate  *		0 - successful
27297c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
27307c478bd9Sstevel@tonic-gate  */
27317c478bd9Sstevel@tonic-gate int
27327c478bd9Sstevel@tonic-gate pa_print(pr_context_t *context, uval_t *uval, int flag)
27337c478bd9Sstevel@tonic-gate {
27347c478bd9Sstevel@tonic-gate 	int	returnstat = 0;
27357c478bd9Sstevel@tonic-gate 	int	last;
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 	switch (uval->uvaltype) {
27387c478bd9Sstevel@tonic-gate 	case PRA_INT32:
27397c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%d", uval->int32_val);
27407c478bd9Sstevel@tonic-gate 		break;
27417c478bd9Sstevel@tonic-gate 	case PRA_UINT32:
27427c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%u", uval->uint32_val);
27437c478bd9Sstevel@tonic-gate 		break;
27447c478bd9Sstevel@tonic-gate 	case PRA_INT64:
27457c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%"PRId64, uval->int64_val);
27467c478bd9Sstevel@tonic-gate 		break;
27477c478bd9Sstevel@tonic-gate 	case PRA_UINT64:
27487c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%"PRIu64, uval->uint64_val);
27497c478bd9Sstevel@tonic-gate 		break;
27507c478bd9Sstevel@tonic-gate 	case PRA_SHORT:
27517c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%hd", uval->short_val);
27527c478bd9Sstevel@tonic-gate 		break;
27537c478bd9Sstevel@tonic-gate 	case PRA_USHORT:
27547c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%hu", uval->ushort_val);
27557c478bd9Sstevel@tonic-gate 		break;
27567c478bd9Sstevel@tonic-gate 	case PRA_CHAR:
27577c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%c", uval->char_val);
27587c478bd9Sstevel@tonic-gate 		break;
27597c478bd9Sstevel@tonic-gate 	case PRA_BYTE:
27607c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%d", uval->char_val);
27617c478bd9Sstevel@tonic-gate 		break;
27627c478bd9Sstevel@tonic-gate 	case PRA_STRING:
27637c478bd9Sstevel@tonic-gate 		returnstat = pa_printstr(context, uval->string_val);
27647c478bd9Sstevel@tonic-gate 		break;
27657c478bd9Sstevel@tonic-gate 	case PRA_HEX32:
27667c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "0x%x", uval->int32_val);
27677c478bd9Sstevel@tonic-gate 		break;
27687c478bd9Sstevel@tonic-gate 	case PRA_HEX64:
27697c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "0x%"PRIx64, uval->int64_val);
27707c478bd9Sstevel@tonic-gate 		break;
27717c478bd9Sstevel@tonic-gate 	case PRA_SHEX:
27727c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "0x%hx", uval->short_val);
27737c478bd9Sstevel@tonic-gate 		break;
27747c478bd9Sstevel@tonic-gate 	case PRA_OCT:
27757c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%ho", uval->ushort_val);
27767c478bd9Sstevel@tonic-gate 		break;
27777c478bd9Sstevel@tonic-gate 	case PRA_LOCT:
27787c478bd9Sstevel@tonic-gate 		returnstat = pr_printf(context, "%o", (int)uval->uint32_val);
27797c478bd9Sstevel@tonic-gate 		break;
27807c478bd9Sstevel@tonic-gate 	default:
27817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("praudit: Unknown type.\n"));
27827c478bd9Sstevel@tonic-gate 		returnstat = -1;
27837c478bd9Sstevel@tonic-gate 		break;
27847c478bd9Sstevel@tonic-gate 	}
27857c478bd9Sstevel@tonic-gate 	if (returnstat < 0)
27867c478bd9Sstevel@tonic-gate 		return (returnstat);
27877c478bd9Sstevel@tonic-gate 
27887c478bd9Sstevel@tonic-gate 	last = (context->audit_adr->adr_now ==
27897c478bd9Sstevel@tonic-gate 	    (context->audit_rec_start + context->audit_rec_len));
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_XMLM)) {
27927c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_ONELINE)) {
27937c478bd9Sstevel@tonic-gate 			if ((flag == 1) || last)
27947c478bd9Sstevel@tonic-gate 				returnstat = pr_putchar(context, '\n');
27957c478bd9Sstevel@tonic-gate 			else
27967c478bd9Sstevel@tonic-gate 				returnstat = pr_printf(context, "%s",
27977c478bd9Sstevel@tonic-gate 				    context->SEPARATOR);
27987c478bd9Sstevel@tonic-gate 		} else {
27997c478bd9Sstevel@tonic-gate 			if (!last)
28007c478bd9Sstevel@tonic-gate 				returnstat = pr_printf(context, "%s",
28017c478bd9Sstevel@tonic-gate 				    context->SEPARATOR);
28027c478bd9Sstevel@tonic-gate 			else
28037c478bd9Sstevel@tonic-gate 				returnstat = pr_putchar(context, '\n');
28047c478bd9Sstevel@tonic-gate 		}
28057c478bd9Sstevel@tonic-gate 	}
28067c478bd9Sstevel@tonic-gate 	if ((returnstat == 0) && (context->data_mode == FILEMODE))
28077c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	return (returnstat);
28107c478bd9Sstevel@tonic-gate }
28117c478bd9Sstevel@tonic-gate 
28128dc347dbStz204579 static struct cntrl_mapping {
28138dc347dbStz204579 	char from;
28148dc347dbStz204579 	char to;
28158dc347dbStz204579 } cntrl_map[] = {
28168dc347dbStz204579 	'\0', '0',
28178dc347dbStz204579 	'\a', 'a',
28188dc347dbStz204579 	'\b', 'b',
28198dc347dbStz204579 	'\t', 't',
28208dc347dbStz204579 	'\f', 'f',
28218dc347dbStz204579 	'\n', 'n',
28228dc347dbStz204579 	'\r', 'r',
28238dc347dbStz204579 	'\v', 'v'
28248dc347dbStz204579 };
28258dc347dbStz204579 
28268dc347dbStz204579 static int cntrl_map_entries = sizeof (cntrl_map)
28278dc347dbStz204579 	/ sizeof (struct cntrl_mapping);
28288dc347dbStz204579 
28297c478bd9Sstevel@tonic-gate /*
28307c478bd9Sstevel@tonic-gate  * Convert binary data to ASCII for printing.
28317c478bd9Sstevel@tonic-gate  */
28327c478bd9Sstevel@tonic-gate void
28337c478bd9Sstevel@tonic-gate convertascii(char *p, char *c, int size)
28347c478bd9Sstevel@tonic-gate {
28357c478bd9Sstevel@tonic-gate 	register int	i;
28368dc347dbStz204579 	register int	j, match;
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
28397c478bd9Sstevel@tonic-gate 		*(c + i) = (char)toascii(*(c + i));
28407c478bd9Sstevel@tonic-gate 		if ((int)iscntrl(*(c + i))) {
28418dc347dbStz204579 			for (j = match = 0; j < cntrl_map_entries; j++)
28428dc347dbStz204579 				if (cntrl_map[j].from == *(c + i)) {
28438dc347dbStz204579 					*p++ = '\\';
28448dc347dbStz204579 					*p++ = cntrl_map[j].to;
28458dc347dbStz204579 					match = 1;
28468dc347dbStz204579 				}
28478dc347dbStz204579 			if (!match) {
28487c478bd9Sstevel@tonic-gate 				*p++ = '^';
28497c478bd9Sstevel@tonic-gate 				*p++ = (char)(*(c + i) + 0x40);
28508dc347dbStz204579 			}
28517c478bd9Sstevel@tonic-gate 		} else
28527c478bd9Sstevel@tonic-gate 			*p++ = *(c + i);
28537c478bd9Sstevel@tonic-gate 	}
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	*p = '\0';
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate }
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate /*
28617c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
28627c478bd9Sstevel@tonic-gate  * pa_xgeneric: Process Xobject token and display contents
28637c478bd9Sstevel@tonic-gate  *		      This routine will handle many of the attribute
28647c478bd9Sstevel@tonic-gate  *		      types introduced in TS 2.x, such as:
28657c478bd9Sstevel@tonic-gate  *
28667c478bd9Sstevel@tonic-gate  *		      AUT_XCOLORMAP, AUT_XCURSOR, AUT_XFONT,
28677c478bd9Sstevel@tonic-gate  *		      AUT_XGC, AUT_XPIXMAP, AUT_XWINDOW
28687c478bd9Sstevel@tonic-gate  *
28697c478bd9Sstevel@tonic-gate  * NOTE: At the time of call, the token id has been retrieved
28707c478bd9Sstevel@tonic-gate  *
28717c478bd9Sstevel@tonic-gate  * return codes		: -1 - error
28727c478bd9Sstevel@tonic-gate  *			:  0 - successful
28737c478bd9Sstevel@tonic-gate  * NOTE: At the time of call, the xatom token id has been retrieved
28747c478bd9Sstevel@tonic-gate  *
28757c478bd9Sstevel@tonic-gate  * Format of xobj
28767c478bd9Sstevel@tonic-gate  *	text token id		adr_char
28777c478bd9Sstevel@tonic-gate  * 	XID 			adr_u_int32
28787c478bd9Sstevel@tonic-gate  * 	creator uid		adr_pw_uid
28797c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
28807c478bd9Sstevel@tonic-gate  */
28817c478bd9Sstevel@tonic-gate int
28827c478bd9Sstevel@tonic-gate pa_xgeneric(pr_context_t *context)
28837c478bd9Sstevel@tonic-gate {
28847c478bd9Sstevel@tonic-gate 	int	returnstat;
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate 	returnstat = process_tag(context, TAG_XID, 0, 0);
28877c478bd9Sstevel@tonic-gate 	return (process_tag(context, TAG_XCUID, returnstat, 1));
28887c478bd9Sstevel@tonic-gate }
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate /*
28927c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------------
28937c478bd9Sstevel@tonic-gate  * pa_liaison : Issues pr_adr_char to retrieve the next ADR item from the
28947c478bd9Sstevel@tonic-gate  *			input stream pointed to by audit_adr, and prints it
28957c478bd9Sstevel@tonic-gate  *			if status >= 0 either in ASCII or raw form
28967c478bd9Sstevel@tonic-gate  * return codes : -1 - error
28977c478bd9Sstevel@tonic-gate  *		: 0 - successful
28987c478bd9Sstevel@tonic-gate  *		: 1 - warning, unknown label type
28997c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------------
29007c478bd9Sstevel@tonic-gate  */
29017c478bd9Sstevel@tonic-gate int
29027c478bd9Sstevel@tonic-gate pa_liaison(pr_context_t *context, int status, int flag)
29037c478bd9Sstevel@tonic-gate {
29047c478bd9Sstevel@tonic-gate 	int	returnstat;
29057c478bd9Sstevel@tonic-gate 	int32_t	li;
29067c478bd9Sstevel@tonic-gate 	uval_t	uval;
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	if (status >= 0) {
29097c478bd9Sstevel@tonic-gate 		if ((returnstat = pr_adr_int32(context, &li, 1)) != 0) {
29107c478bd9Sstevel@tonic-gate 			return (returnstat);
29117c478bd9Sstevel@tonic-gate 		}
29127c478bd9Sstevel@tonic-gate 		if (!(context->format & PRF_RAWM)) {
29137c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_UINT32;
29147c478bd9Sstevel@tonic-gate 			uval.uint32_val = li;
29157c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
29167c478bd9Sstevel@tonic-gate 		}
29177c478bd9Sstevel@tonic-gate 		/* print in hexadecimal form */
29187c478bd9Sstevel@tonic-gate 		if ((context->format & PRF_RAWM) || (returnstat == 1)) {
29197c478bd9Sstevel@tonic-gate 			uval.uvaltype = PRA_HEX32;
29207c478bd9Sstevel@tonic-gate 			uval.uint32_val = li;
29217c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
29227c478bd9Sstevel@tonic-gate 		}
29237c478bd9Sstevel@tonic-gate 		return (returnstat);
29247c478bd9Sstevel@tonic-gate 	} else
29257c478bd9Sstevel@tonic-gate 		return (status);
29267c478bd9Sstevel@tonic-gate }
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate /*
29297c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------------
29307c478bd9Sstevel@tonic-gate  * pa_xid : Issues pr_adr_int32 to retrieve the XID from the input
29317c478bd9Sstevel@tonic-gate  *	      stream pointed to by audit_adr, and prints it if
29327c478bd9Sstevel@tonic-gate  *	      status >= 0 either in ASCII or raw form
29337c478bd9Sstevel@tonic-gate  * return codes : -1 - error
29347c478bd9Sstevel@tonic-gate  *		:  0 - successful
29357c478bd9Sstevel@tonic-gate  *		:  1 - warning, unknown label type
29367c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------------
29377c478bd9Sstevel@tonic-gate  */
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate int
29407c478bd9Sstevel@tonic-gate pa_xid(pr_context_t *context, int status, int flag)
29417c478bd9Sstevel@tonic-gate {
29427c478bd9Sstevel@tonic-gate 	int returnstat;
29437c478bd9Sstevel@tonic-gate 	int32_t xid;
29447c478bd9Sstevel@tonic-gate 	uval_t	uval;
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	if (status < 0)
29477c478bd9Sstevel@tonic-gate 		return (status);
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	/* get XID from stream */
29507c478bd9Sstevel@tonic-gate 	if ((returnstat = pr_adr_int32(context, (int32_t *)&xid, 1)) != 0)
29517c478bd9Sstevel@tonic-gate 		return (returnstat);
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	if (!(context->format & PRF_RAWM)) {
29547c478bd9Sstevel@tonic-gate 		uval.uvaltype = PRA_STRING;
29557c478bd9Sstevel@tonic-gate 		uval.string_val = hexconvert((char *)&xid, sizeof (xid),
29567c478bd9Sstevel@tonic-gate 		    sizeof (xid));
29577c478bd9Sstevel@tonic-gate 		if (uval.string_val) {
29587c478bd9Sstevel@tonic-gate 			returnstat = pa_print(context, &uval, flag);
29597c478bd9Sstevel@tonic-gate 			free(uval.string_val);
29607c478bd9Sstevel@tonic-gate 		}
29617c478bd9Sstevel@tonic-gate 	} else {
29627c478bd9Sstevel@tonic-gate 		uval.uvaltype = PRA_INT32;
29637c478bd9Sstevel@tonic-gate 		uval.int32_val = xid;
29647c478bd9Sstevel@tonic-gate 		returnstat = pa_print(context, &uval, flag);
29657c478bd9Sstevel@tonic-gate 	}
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 	return (returnstat);
29687c478bd9Sstevel@tonic-gate }
2969*a7746f66Stz204579 
2970*a7746f66Stz204579 static int
2971*a7746f66Stz204579 pa_ace_flags(pr_context_t *context, ace_t *ace, int status, int flag)
2972*a7746f66Stz204579 {
2973*a7746f66Stz204579 	int	returnstat;
2974*a7746f66Stz204579 	uval_t	uval;
2975*a7746f66Stz204579 
2976*a7746f66Stz204579 	if (status < 0)
2977*a7746f66Stz204579 		return (status);
2978*a7746f66Stz204579 
2979*a7746f66Stz204579 	/*
2980*a7746f66Stz204579 	 * TRANSLATION_NOTE
2981*a7746f66Stz204579 	 * ace->a_flags refers to access flags of ZFS/NFSv4 ACL entry.
2982*a7746f66Stz204579 	 */
2983*a7746f66Stz204579 	if ((returnstat = open_tag(context, TAG_ACEFLAGS)) != 0)
2984*a7746f66Stz204579 		return (returnstat);
2985*a7746f66Stz204579 	if (!(context->format & PRF_RAWM)) {
2986*a7746f66Stz204579 		uval.uvaltype = PRA_STRING;
2987*a7746f66Stz204579 		switch (ace->a_flags & ACE_TYPE_FLAGS) {
2988*a7746f66Stz204579 		case ACE_OWNER:
2989*a7746f66Stz204579 			uval.string_val = gettext(OWNERAT_TXT);
2990*a7746f66Stz204579 			break;
2991*a7746f66Stz204579 		case ACE_GROUP | ACE_IDENTIFIER_GROUP:
2992*a7746f66Stz204579 			uval.string_val = gettext(GROUPAT_TXT);
2993*a7746f66Stz204579 			break;
2994*a7746f66Stz204579 		case ACE_IDENTIFIER_GROUP:
2995*a7746f66Stz204579 			uval.string_val = gettext(GROUP_TXT);
2996*a7746f66Stz204579 			break;
2997*a7746f66Stz204579 		case ACE_EVERYONE:
2998*a7746f66Stz204579 			uval.string_val = gettext(EVERYONEAT_TXT);
2999*a7746f66Stz204579 			break;
3000*a7746f66Stz204579 		case 0:
3001*a7746f66Stz204579 			uval.string_val = gettext(USER_TXT);
3002*a7746f66Stz204579 			break;
3003*a7746f66Stz204579 		default:
3004*a7746f66Stz204579 			uval.uvaltype = PRA_USHORT;
3005*a7746f66Stz204579 			uval.uint32_val = ace->a_flags;
3006*a7746f66Stz204579 		}
3007*a7746f66Stz204579 	} else {
3008*a7746f66Stz204579 		uval.uvaltype = PRA_USHORT;
3009*a7746f66Stz204579 		uval.uint32_val = ace->a_flags;
3010*a7746f66Stz204579 	}
3011*a7746f66Stz204579 	if ((returnstat = pa_print(context, &uval, flag)) != 0)
3012*a7746f66Stz204579 		return (returnstat);
3013*a7746f66Stz204579 	return (close_tag(context, TAG_ACEFLAGS));
3014*a7746f66Stz204579 }
3015*a7746f66Stz204579 
3016*a7746f66Stz204579 static int
3017*a7746f66Stz204579 pa_ace_who(pr_context_t *context, ace_t *ace, int status, int flag)
3018*a7746f66Stz204579 {
3019*a7746f66Stz204579 	int		returnstat;
3020*a7746f66Stz204579 
3021*a7746f66Stz204579 	if (status < 0)
3022*a7746f66Stz204579 		return (status);
3023*a7746f66Stz204579 
3024*a7746f66Stz204579 	/*
3025*a7746f66Stz204579 	 * TRANSLATION_NOTE
3026*a7746f66Stz204579 	 * ace->a_who refers to user id or group id of ZFS/NFSv4 ACL entry.
3027*a7746f66Stz204579 	 */
3028*a7746f66Stz204579 	if ((returnstat = open_tag(context, TAG_ACEID)) != 0)
3029*a7746f66Stz204579 		return (returnstat);
3030*a7746f66Stz204579 	switch (ace->a_flags & ACE_TYPE_FLAGS) {
3031*a7746f66Stz204579 	case ACE_IDENTIFIER_GROUP:	/* group id */
3032*a7746f66Stz204579 		returnstat = pa_print_gid(context, ace->a_who, returnstat,
3033*a7746f66Stz204579 		    flag);
3034*a7746f66Stz204579 		break;
3035*a7746f66Stz204579 	default:			/* user id */
3036*a7746f66Stz204579 		returnstat = pa_print_uid(context, ace->a_who, returnstat,
3037*a7746f66Stz204579 		    flag);
3038*a7746f66Stz204579 		break;
3039*a7746f66Stz204579 	}
3040*a7746f66Stz204579 	if (returnstat < 0)
3041*a7746f66Stz204579 		return (returnstat);
3042*a7746f66Stz204579 	return (close_tag(context, TAG_ACEID));
3043*a7746f66Stz204579 }
3044*a7746f66Stz204579 
3045*a7746f66Stz204579 /*
3046*a7746f66Stz204579  * Appends what to str, (re)allocating str if necessary.
3047*a7746f66Stz204579  */
3048*a7746f66Stz204579 #define	INITIAL_ALLOC	256
3049*a7746f66Stz204579 static int
3050*a7746f66Stz204579 strappend(char **str, char *what, size_t *alloc)
3051*a7746f66Stz204579 {
3052*a7746f66Stz204579 	char	*s, *newstr;
3053*a7746f66Stz204579 	size_t	needed;
3054*a7746f66Stz204579 
3055*a7746f66Stz204579 	s = *str;
3056*a7746f66Stz204579 
3057*a7746f66Stz204579 	if (s == NULL) {
3058*a7746f66Stz204579 		s = malloc(INITIAL_ALLOC);
3059*a7746f66Stz204579 		if (s == NULL) {
3060*a7746f66Stz204579 			*alloc = 0;
3061*a7746f66Stz204579 			return (-1);
3062*a7746f66Stz204579 		}
3063*a7746f66Stz204579 		*alloc = INITIAL_ALLOC;
3064*a7746f66Stz204579 		s[0] = '\0';
3065*a7746f66Stz204579 		*str = s;
3066*a7746f66Stz204579 	}
3067*a7746f66Stz204579 
3068*a7746f66Stz204579 	needed = strlen(s) + strlen(what) + 1;
3069*a7746f66Stz204579 	if (*alloc < needed) {
3070*a7746f66Stz204579 		newstr = realloc(s, needed);
3071*a7746f66Stz204579 		if (newstr == NULL)
3072*a7746f66Stz204579 			return (-1);
3073*a7746f66Stz204579 		s = newstr;
3074*a7746f66Stz204579 		*alloc = needed;
3075*a7746f66Stz204579 		*str = s;
3076*a7746f66Stz204579 	}
3077*a7746f66Stz204579 	(void) strlcat(s, what, *alloc);
3078*a7746f66Stz204579 
3079*a7746f66Stz204579 	return (0);
3080*a7746f66Stz204579 }
3081*a7746f66Stz204579 
3082*a7746f66Stz204579 static int
3083*a7746f66Stz204579 pa_ace_access_mask(pr_context_t *context, ace_t *ace, int status, int flag)
3084*a7746f66Stz204579 {
3085*a7746f66Stz204579 	int	returnstat, i;
3086*a7746f66Stz204579 	uval_t	uval;
3087*a7746f66Stz204579 	char	*permstr = NULL;
3088*a7746f66Stz204579 	size_t	permstr_alloc = 0;
3089*a7746f66Stz204579 
3090*a7746f66Stz204579 	if (status < 0)
3091*a7746f66Stz204579 		return (status);
3092*a7746f66Stz204579 
3093*a7746f66Stz204579 	/*
3094*a7746f66Stz204579 	 * TRANSLATION_NOTE
3095*a7746f66Stz204579 	 * ace->a_access_mask refers to access mask of ZFS/NFSv4 ACL entry.
3096*a7746f66Stz204579 	 */
3097*a7746f66Stz204579 	if ((returnstat = open_tag(context, TAG_ACEMASK)) != 0)
3098*a7746f66Stz204579 		return (returnstat);
3099*a7746f66Stz204579 	if (context->format & PRF_SHORTM &&
3100*a7746f66Stz204579 	    ((permstr = malloc(15)) != NULL)) {
3101*a7746f66Stz204579 		for (i = 0; i < 14; i++)
3102*a7746f66Stz204579 			permstr[i] = '-';
3103*a7746f66Stz204579 
3104*a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_DATA)
3105*a7746f66Stz204579 			permstr[0] = 'r';
3106*a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_DATA)
3107*a7746f66Stz204579 			permstr[1] = 'w';
3108*a7746f66Stz204579 		if (ace->a_access_mask & ACE_EXECUTE)
3109*a7746f66Stz204579 			permstr[2] = 'x';
3110*a7746f66Stz204579 		if (ace->a_access_mask & ACE_APPEND_DATA)
3111*a7746f66Stz204579 			permstr[3] = 'p';
3112*a7746f66Stz204579 		if (ace->a_access_mask & ACE_DELETE)
3113*a7746f66Stz204579 			permstr[4] = 'd';
3114*a7746f66Stz204579 		if (ace->a_access_mask & ACE_DELETE_CHILD)
3115*a7746f66Stz204579 			permstr[5] = 'D';
3116*a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_ATTRIBUTES)
3117*a7746f66Stz204579 			permstr[6] = 'a';
3118*a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_ATTRIBUTES)
3119*a7746f66Stz204579 			permstr[7] = 'A';
3120*a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_NAMED_ATTRS)
3121*a7746f66Stz204579 			permstr[8] = 'R';
3122*a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_NAMED_ATTRS)
3123*a7746f66Stz204579 			permstr[9] = 'W';
3124*a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_ACL)
3125*a7746f66Stz204579 			permstr[10] = 'c';
3126*a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_ACL)
3127*a7746f66Stz204579 			permstr[11] = 'C';
3128*a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_OWNER)
3129*a7746f66Stz204579 			permstr[12] = 'o';
3130*a7746f66Stz204579 		if (ace->a_access_mask & ACE_SYNCHRONIZE)
3131*a7746f66Stz204579 			permstr[13] = 's';
3132*a7746f66Stz204579 		permstr[14] = '\0';
3133*a7746f66Stz204579 		uval.uvaltype = PRA_STRING;
3134*a7746f66Stz204579 		uval.string_val = permstr;
3135*a7746f66Stz204579 	} else if (!(context->format & PRF_RAWM)) {
3136*a7746f66Stz204579 
3137*a7746f66Stz204579 		/*
3138*a7746f66Stz204579 		 * Note this differs from acltext.c:ace_perm_txt()
3139*a7746f66Stz204579 		 * because we don't know if the acl belongs to a file
3140*a7746f66Stz204579 		 * or directory. ace mask value are the same
3141*a7746f66Stz204579 		 * nonetheless, see sys/acl.h
3142*a7746f66Stz204579 		 */
3143*a7746f66Stz204579 		if (ace->a_access_mask & ACE_LIST_DIRECTORY) {
3144*a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(READ_DIR_TXT),
3145*a7746f66Stz204579 			    &permstr_alloc);
3146*a7746f66Stz204579 		}
3147*a7746f66Stz204579 		if (ace->a_access_mask & ACE_ADD_FILE) {
3148*a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(ADD_FILE_TXT),
3149*a7746f66Stz204579 			    &permstr_alloc);
3150*a7746f66Stz204579 		}
3151*a7746f66Stz204579 		if (ace->a_access_mask & ACE_ADD_SUBDIRECTORY) {
3152*a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(ADD_DIR_TXT),
3153*a7746f66Stz204579 			    &permstr_alloc);
3154*a7746f66Stz204579 		}
3155*a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_NAMED_ATTRS) {
3156*a7746f66Stz204579 			returnstat = strappend(&permstr,
3157*a7746f66Stz204579 			    gettext(READ_XATTR_TXT), &permstr_alloc);
3158*a7746f66Stz204579 		}
3159*a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_NAMED_ATTRS) {
3160*a7746f66Stz204579 			returnstat = strappend(&permstr,
3161*a7746f66Stz204579 			    gettext(WRITE_XATTR_TXT), &permstr_alloc);
3162*a7746f66Stz204579 		}
3163*a7746f66Stz204579 		if (ace->a_access_mask & ACE_EXECUTE) {
3164*a7746f66Stz204579 			returnstat = strappend(&permstr,
3165*a7746f66Stz204579 			    gettext(EXECUTE_TXT), &permstr_alloc);
3166*a7746f66Stz204579 		}
3167*a7746f66Stz204579 		if (ace->a_access_mask & ACE_DELETE_CHILD) {
3168*a7746f66Stz204579 			returnstat = strappend(&permstr,
3169*a7746f66Stz204579 			    gettext(DELETE_CHILD_TXT), &permstr_alloc);
3170*a7746f66Stz204579 		}
3171*a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_ATTRIBUTES) {
3172*a7746f66Stz204579 			returnstat = strappend(&permstr,
3173*a7746f66Stz204579 			    gettext(READ_ATTRIBUTES_TXT), &permstr_alloc);
3174*a7746f66Stz204579 		}
3175*a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_ATTRIBUTES) {
3176*a7746f66Stz204579 			returnstat = strappend(&permstr,
3177*a7746f66Stz204579 			    gettext(WRITE_ATTRIBUTES_TXT), &permstr_alloc);
3178*a7746f66Stz204579 		}
3179*a7746f66Stz204579 		if (ace->a_access_mask & ACE_DELETE) {
3180*a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(DELETE_TXT),
3181*a7746f66Stz204579 			    &permstr_alloc);
3182*a7746f66Stz204579 		}
3183*a7746f66Stz204579 		if (ace->a_access_mask & ACE_READ_ACL) {
3184*a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(READ_ACL_TXT),
3185*a7746f66Stz204579 			    &permstr_alloc);
3186*a7746f66Stz204579 		}
3187*a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_ACL) {
3188*a7746f66Stz204579 			returnstat = strappend(&permstr, gettext(WRITE_ACL_TXT),
3189*a7746f66Stz204579 			    &permstr_alloc);
3190*a7746f66Stz204579 		}
3191*a7746f66Stz204579 		if (ace->a_access_mask & ACE_WRITE_OWNER) {
3192*a7746f66Stz204579 			returnstat = strappend(&permstr,
3193*a7746f66Stz204579 			    gettext(WRITE_OWNER_TXT), &permstr_alloc);
3194*a7746f66Stz204579 		}
3195*a7746f66Stz204579 		if (ace->a_access_mask & ACE_SYNCHRONIZE) {
3196*a7746f66Stz204579 			returnstat = strappend(&permstr,
3197*a7746f66Stz204579 			    gettext(SYNCHRONIZE_TXT), &permstr_alloc);
3198*a7746f66Stz204579 		}
3199*a7746f66Stz204579 		if (permstr[strlen(permstr) - 1] == '/')
3200*a7746f66Stz204579 			permstr[strlen(permstr) - 1] = '\0';
3201*a7746f66Stz204579 		uval.uvaltype = PRA_STRING;
3202*a7746f66Stz204579 		uval.string_val = permstr;
3203*a7746f66Stz204579 	}
3204*a7746f66Stz204579 	if ((permstr == NULL) || (returnstat != 0) ||
3205*a7746f66Stz204579 	    (context->format & PRF_RAWM)) {
3206*a7746f66Stz204579 		uval.uvaltype = PRA_UINT32;
3207*a7746f66Stz204579 		uval.uint32_val = ace->a_access_mask;
3208*a7746f66Stz204579 	}
3209*a7746f66Stz204579 	returnstat = pa_print(context, &uval, flag);
3210*a7746f66Stz204579 
3211*a7746f66Stz204579 	if (permstr != NULL)
3212*a7746f66Stz204579 		free(permstr);
3213*a7746f66Stz204579 	if (returnstat != 0)
3214*a7746f66Stz204579 		return (returnstat);
3215*a7746f66Stz204579 	return (close_tag(context, TAG_ACEMASK));
3216*a7746f66Stz204579 }
3217*a7746f66Stz204579 
3218*a7746f66Stz204579 static int
3219*a7746f66Stz204579 pa_ace_type(pr_context_t *context, ace_t *ace, int status, int flag)
3220*a7746f66Stz204579 {
3221*a7746f66Stz204579 	int	returnstat;
3222*a7746f66Stz204579 	uval_t	uval;
3223*a7746f66Stz204579 
3224*a7746f66Stz204579 	if (status < 0)
3225*a7746f66Stz204579 		return (status);
3226*a7746f66Stz204579 
3227*a7746f66Stz204579 	/*
3228*a7746f66Stz204579 	 * TRANSLATION_NOTE
3229*a7746f66Stz204579 	 * ace->a_type refers to access type of ZFS/NFSv4 ACL entry.
3230*a7746f66Stz204579 	 */
3231*a7746f66Stz204579 	if ((returnstat = open_tag(context, TAG_ACETYPE)) != 0)
3232*a7746f66Stz204579 		return (returnstat);
3233*a7746f66Stz204579 	if (!(context->format & PRF_RAWM)) {
3234*a7746f66Stz204579 		uval.uvaltype = PRA_STRING;
3235*a7746f66Stz204579 		switch (ace->a_type) {
3236*a7746f66Stz204579 		case ACE_ACCESS_ALLOWED_ACE_TYPE:
3237*a7746f66Stz204579 			uval.string_val = gettext(ALLOW_TXT);
3238*a7746f66Stz204579 			break;
3239*a7746f66Stz204579 		case ACE_ACCESS_DENIED_ACE_TYPE:
3240*a7746f66Stz204579 			uval.string_val = gettext(DENY_TXT);
3241*a7746f66Stz204579 			break;
3242*a7746f66Stz204579 		case ACE_SYSTEM_AUDIT_ACE_TYPE:
3243*a7746f66Stz204579 			uval.string_val = gettext(AUDIT_TXT);
3244*a7746f66Stz204579 			break;
3245*a7746f66Stz204579 		case ACE_SYSTEM_ALARM_ACE_TYPE:
3246*a7746f66Stz204579 			uval.string_val = gettext(ALARM_TXT);
3247*a7746f66Stz204579 			break;
3248*a7746f66Stz204579 		default:
3249*a7746f66Stz204579 			uval.string_val = gettext(UNKNOWN_TXT);
3250*a7746f66Stz204579 		}
3251*a7746f66Stz204579 	} else {
3252*a7746f66Stz204579 		uval.uvaltype = PRA_USHORT;
3253*a7746f66Stz204579 		uval.uint32_val = ace->a_type;
3254*a7746f66Stz204579 	}
3255*a7746f66Stz204579 	if ((returnstat = pa_print(context, &uval, flag)) != 0)
3256*a7746f66Stz204579 		return (returnstat);
3257*a7746f66Stz204579 	return (close_tag(context, TAG_ACETYPE));
3258*a7746f66Stz204579 }
3259*a7746f66Stz204579 
3260*a7746f66Stz204579 int
3261*a7746f66Stz204579 pa_ace(pr_context_t *context, int status, int flag)
3262*a7746f66Stz204579 {
3263*a7746f66Stz204579 	int		returnstat;
3264*a7746f66Stz204579 	ace_t		ace;
3265*a7746f66Stz204579 
3266*a7746f66Stz204579 	if (status < 0)
3267*a7746f66Stz204579 		return (status);
3268*a7746f66Stz204579 
3269*a7746f66Stz204579 	if ((returnstat = pr_adr_u_int32(context, &ace.a_who, 1)) != 0)
3270*a7746f66Stz204579 		return (returnstat);
3271*a7746f66Stz204579 	if ((returnstat = pr_adr_u_int32(context, &ace.a_access_mask, 1)) != 0)
3272*a7746f66Stz204579 		return (returnstat);
3273*a7746f66Stz204579 	if ((returnstat = pr_adr_u_short(context, &ace.a_flags, 1)) != 0)
3274*a7746f66Stz204579 		return (returnstat);
3275*a7746f66Stz204579 	if ((returnstat = pr_adr_u_short(context, &ace.a_type, 1)) != 0)
3276*a7746f66Stz204579 		return (returnstat);
3277*a7746f66Stz204579 
3278*a7746f66Stz204579 	if ((returnstat = pa_ace_flags(context, &ace, returnstat, 0)) != 0)
3279*a7746f66Stz204579 		return (returnstat);
3280*a7746f66Stz204579 	/* pa_ace_who can returns 1 if uid/gid is not found */
3281*a7746f66Stz204579 	if ((returnstat = pa_ace_who(context, &ace, returnstat, 0)) < 0)
3282*a7746f66Stz204579 		return (returnstat);
3283*a7746f66Stz204579 	if ((returnstat = pa_ace_access_mask(context, &ace,
3284*a7746f66Stz204579 	    returnstat, 0)) != 0)
3285*a7746f66Stz204579 		return (returnstat);
3286*a7746f66Stz204579 	return (pa_ace_type(context, &ace, returnstat, flag));
3287*a7746f66Stz204579 }
3288