10Sstevel@tonic-gate /*
26119Spwernau  *
30Sstevel@tonic-gate  * CDDL HEADER START
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
63055Sdanmcd  * Common Development and Distribution License (the "License").
73055Sdanmcd  * You may not use this file except in compliance with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
239086SVladimir.Kotal@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <unistd.h>
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <stdarg.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <sys/sysconf.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <sys/socket.h>
390Sstevel@tonic-gate #include <netdb.h>
400Sstevel@tonic-gate #include <netinet/in.h>
410Sstevel@tonic-gate #include <arpa/inet.h>
420Sstevel@tonic-gate #include <net/pfkeyv2.h>
430Sstevel@tonic-gate #include <net/pfpolicy.h>
440Sstevel@tonic-gate #include <libintl.h>
450Sstevel@tonic-gate #include <setjmp.h>
460Sstevel@tonic-gate #include <libgen.h>
474235Smarkfen #include <libscf.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include "ipsec_util.h"
500Sstevel@tonic-gate #include "ikedoor.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * This file contains support functions that are shared by the ipsec
544867Spwernau  * utilities and daemons including ipseckey(1m), ikeadm(1m) and in.iked(1m).
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
574867Spwernau 
584867Spwernau #define	EFD(file) (((file) == stdout) ? stderr : (file))
594867Spwernau 
609086SVladimir.Kotal@Sun.COM /* Limits for interactive mode. */
619086SVladimir.Kotal@Sun.COM #define	MAX_LINE_LEN	IBUF_SIZE
629086SVladimir.Kotal@Sun.COM #define	MAX_CMD_HIST	64000	/* in bytes */
639086SVladimir.Kotal@Sun.COM 
640Sstevel@tonic-gate /* Set standard default/initial values for globals... */
650Sstevel@tonic-gate boolean_t pflag = B_FALSE;	/* paranoid w.r.t. printing keying material */
660Sstevel@tonic-gate boolean_t nflag = B_FALSE;	/* avoid nameservice? */
670Sstevel@tonic-gate boolean_t interactive = B_FALSE;	/* util not running on cmdline */
680Sstevel@tonic-gate boolean_t readfile = B_FALSE;	/* cmds are being read from a file */
690Sstevel@tonic-gate uint_t	lineno = 0;		/* track location if reading cmds from file */
704235Smarkfen uint_t	lines_added = 0;
714235Smarkfen uint_t	lines_parsed = 0;
720Sstevel@tonic-gate jmp_buf	env;		/* for error recovery in interactive/readfile modes */
734235Smarkfen char *my_fmri = NULL;
744235Smarkfen FILE *debugfile = stderr;
759086SVladimir.Kotal@Sun.COM static GetLine *gl = NULL;	/* for interactive mode */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Print errno and exit if cmdline or readfile, reset state if interactive
794064Smarkfen  * The error string *what should be dgettext()'d before calling bail().
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate void
820Sstevel@tonic-gate bail(char *what)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	if (errno != 0)
850Sstevel@tonic-gate 		warn(what);
860Sstevel@tonic-gate 	else
874235Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "Error: %s"), what);
880Sstevel@tonic-gate 	if (readfile) {
894235Smarkfen 		return;
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 	if (interactive && !readfile)
920Sstevel@tonic-gate 		longjmp(env, 2);
934235Smarkfen 	EXIT_FATAL(NULL);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Print caller-supplied variable-arg error msg, then exit if cmdline or
980Sstevel@tonic-gate  * readfile, or reset state if interactive.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate /*PRINTFLIKE1*/
1010Sstevel@tonic-gate void
1020Sstevel@tonic-gate bail_msg(char *fmt, ...)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	va_list	ap;
1050Sstevel@tonic-gate 	char	msgbuf[BUFSIZ];
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	va_start(ap, fmt);
1080Sstevel@tonic-gate 	(void) vsnprintf(msgbuf, BUFSIZ, fmt, ap);
1090Sstevel@tonic-gate 	va_end(ap);
1100Sstevel@tonic-gate 	if (readfile)
1114064Smarkfen 		warnx(dgettext(TEXT_DOMAIN,
1124064Smarkfen 		    "ERROR on line %u:\n%s\n"), lineno,  msgbuf);
1130Sstevel@tonic-gate 	else
1144064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "ERROR: %s\n"), msgbuf);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (interactive && !readfile)
1170Sstevel@tonic-gate 		longjmp(env, 1);
1180Sstevel@tonic-gate 
1194235Smarkfen 	EXIT_FATAL(NULL);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * dump_XXX functions produce ASCII output from various structures.
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  * Because certain errors need to do this to stderr, dump_XXX functions
1270Sstevel@tonic-gate  * take a FILE pointer.
1280Sstevel@tonic-gate  *
1290Sstevel@tonic-gate  * If an error occured while writing to the specified file, these
1300Sstevel@tonic-gate  * functions return -1, zero otherwise.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate int
1343055Sdanmcd dump_sockaddr(struct sockaddr *sa, uint8_t prefixlen, boolean_t addr_only,
1354867Spwernau     FILE *where, boolean_t ignore_nss)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	struct sockaddr_in	*sin;
1380Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
1390Sstevel@tonic-gate 	char			*printable_addr, *protocol;
1400Sstevel@tonic-gate 	uint8_t			*addrptr;
1413055Sdanmcd 	/* Add 4 chars to hold '/nnn' for prefixes. */
1423055Sdanmcd 	char			storage[INET6_ADDRSTRLEN + 4];
1430Sstevel@tonic-gate 	uint16_t		port;
1440Sstevel@tonic-gate 	boolean_t		unspec;
1450Sstevel@tonic-gate 	struct hostent		*hp;
1460Sstevel@tonic-gate 	int			getipnode_errno, addrlen;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	switch (sa->sa_family) {
1490Sstevel@tonic-gate 	case AF_INET:
1500Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1510Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sa;
1520Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin->sin_addr;
1530Sstevel@tonic-gate 		port = sin->sin_port;
1540Sstevel@tonic-gate 		protocol = "AF_INET";
1550Sstevel@tonic-gate 		unspec = (sin->sin_addr.s_addr == 0);
1560Sstevel@tonic-gate 		addrlen = sizeof (sin->sin_addr);
1570Sstevel@tonic-gate 		break;
1580Sstevel@tonic-gate 	case AF_INET6:
1590Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1600Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sa;
1610Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin6->sin6_addr;
1620Sstevel@tonic-gate 		port = sin6->sin6_port;
1630Sstevel@tonic-gate 		protocol = "AF_INET6";
1640Sstevel@tonic-gate 		unspec = IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr);
1650Sstevel@tonic-gate 		addrlen = sizeof (sin6->sin6_addr);
1660Sstevel@tonic-gate 		break;
1670Sstevel@tonic-gate 	default:
1680Sstevel@tonic-gate 		return (0);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (inet_ntop(sa->sa_family, addrptr, storage, INET6_ADDRSTRLEN) ==
1720Sstevel@tonic-gate 	    NULL) {
1734064Smarkfen 		printable_addr = dgettext(TEXT_DOMAIN, "Invalid IP address.");
1740Sstevel@tonic-gate 	} else {
1753055Sdanmcd 		char prefix[5];	/* "/nnn" with terminator. */
1763055Sdanmcd 
1773055Sdanmcd 		(void) snprintf(prefix, sizeof (prefix), "/%d", prefixlen);
1780Sstevel@tonic-gate 		printable_addr = storage;
1793055Sdanmcd 		if (prefixlen != 0) {
1803055Sdanmcd 			(void) strlcat(printable_addr, prefix,
1813055Sdanmcd 			    sizeof (storage));
1823055Sdanmcd 		}
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 	if (addr_only) {
1850Sstevel@tonic-gate 		if (fprintf(where, "%s", printable_addr) < 0)
1860Sstevel@tonic-gate 			return (-1);
1870Sstevel@tonic-gate 	} else {
1884064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
1894064Smarkfen 		    "%s: port %d, %s"), protocol,
1900Sstevel@tonic-gate 		    ntohs(port), printable_addr) < 0)
1910Sstevel@tonic-gate 			return (-1);
1924867Spwernau 		if (ignore_nss == B_FALSE) {
1930Sstevel@tonic-gate 			/*
1940Sstevel@tonic-gate 			 * Do AF_independent reverse hostname lookup here.
1950Sstevel@tonic-gate 			 */
1960Sstevel@tonic-gate 			if (unspec) {
1970Sstevel@tonic-gate 				if (fprintf(where,
1984064Smarkfen 				    dgettext(TEXT_DOMAIN,
1994064Smarkfen 				    " <unspecified>")) < 0)
2000Sstevel@tonic-gate 					return (-1);
2010Sstevel@tonic-gate 			} else {
2020Sstevel@tonic-gate 				hp = getipnodebyaddr((char *)addrptr, addrlen,
2030Sstevel@tonic-gate 				    sa->sa_family, &getipnode_errno);
2040Sstevel@tonic-gate 				if (hp != NULL) {
2050Sstevel@tonic-gate 					if (fprintf(where,
2060Sstevel@tonic-gate 					    " (%s)", hp->h_name) < 0)
2070Sstevel@tonic-gate 						return (-1);
2080Sstevel@tonic-gate 					freehostent(hp);
2090Sstevel@tonic-gate 				} else {
2100Sstevel@tonic-gate 					if (fprintf(where,
2114064Smarkfen 					    dgettext(TEXT_DOMAIN,
2124064Smarkfen 					    " <unknown>")) < 0)
2130Sstevel@tonic-gate 						return (-1);
2140Sstevel@tonic-gate 				}
2150Sstevel@tonic-gate 			}
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 		if (fputs(".\n", where) == EOF)
2180Sstevel@tonic-gate 			return (-1);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 	return (0);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
22410824SMark.Fenwick@Sun.COM  * Dump a key, any salt and bitlen.
22510824SMark.Fenwick@Sun.COM  * The key is made up of a stream of bits. If the algorithm requires a salt
22610824SMark.Fenwick@Sun.COM  * value, this will also be part of the dumped key. The last "saltbits" of the
22710824SMark.Fenwick@Sun.COM  * key string, reading left to right will be the salt value. To make it easier
22810824SMark.Fenwick@Sun.COM  * to see which bits make up the key, the salt value is enclosed in []'s.
22910824SMark.Fenwick@Sun.COM  * This function can also be called when ipseckey(1m) -s is run, this "saves"
23010824SMark.Fenwick@Sun.COM  * the SAs, including the key to a file. When this is the case, the []'s are
23110824SMark.Fenwick@Sun.COM  * not printed.
23210824SMark.Fenwick@Sun.COM  *
23310824SMark.Fenwick@Sun.COM  * The implementation allows the kernel to be told about the length of the salt
23410824SMark.Fenwick@Sun.COM  * in whole bytes only. If this changes, this function will need to be updated.
2350Sstevel@tonic-gate  */
2360Sstevel@tonic-gate int
23710824SMark.Fenwick@Sun.COM dump_key(uint8_t *keyp, uint_t bitlen, uint_t saltbits, FILE *where,
23810824SMark.Fenwick@Sun.COM     boolean_t separate_salt)
2390Sstevel@tonic-gate {
24010824SMark.Fenwick@Sun.COM 	int	numbytes, saltbytes;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	numbytes = SADB_1TO8(bitlen);
24310824SMark.Fenwick@Sun.COM 	saltbytes = SADB_1TO8(saltbits);
24410824SMark.Fenwick@Sun.COM 	numbytes += saltbytes;
24510824SMark.Fenwick@Sun.COM 
2460Sstevel@tonic-gate 	/* The & 0x7 is to check for leftover bits. */
2470Sstevel@tonic-gate 	if ((bitlen & 0x7) != 0)
2480Sstevel@tonic-gate 		numbytes++;
24910824SMark.Fenwick@Sun.COM 
2500Sstevel@tonic-gate 	while (numbytes-- != 0) {
2510Sstevel@tonic-gate 		if (pflag) {
2520Sstevel@tonic-gate 			/* Print no keys if paranoid */
2530Sstevel@tonic-gate 			if (fprintf(where, "XX") < 0)
2540Sstevel@tonic-gate 				return (-1);
2550Sstevel@tonic-gate 		} else {
2560Sstevel@tonic-gate 			if (fprintf(where, "%02x", *keyp++) < 0)
2570Sstevel@tonic-gate 				return (-1);
2580Sstevel@tonic-gate 		}
25910824SMark.Fenwick@Sun.COM 		if (separate_salt && saltbytes != 0 &&
26010824SMark.Fenwick@Sun.COM 		    numbytes == saltbytes) {
26110824SMark.Fenwick@Sun.COM 			if (fprintf(where, "[") < 0)
26210824SMark.Fenwick@Sun.COM 				return (-1);
26310824SMark.Fenwick@Sun.COM 		}
2640Sstevel@tonic-gate 	}
26510824SMark.Fenwick@Sun.COM 
26610824SMark.Fenwick@Sun.COM 	if (separate_salt && saltbits != 0) {
26710824SMark.Fenwick@Sun.COM 		if (fprintf(where, "]/%u+%u", bitlen, saltbits) < 0)
26810824SMark.Fenwick@Sun.COM 			return (-1);
26910824SMark.Fenwick@Sun.COM 	} else {
27010824SMark.Fenwick@Sun.COM 		if (fprintf(where, "/%u", bitlen + saltbits) < 0)
27110824SMark.Fenwick@Sun.COM 			return (-1);
27210824SMark.Fenwick@Sun.COM 	}
27310824SMark.Fenwick@Sun.COM 
2740Sstevel@tonic-gate 	return (0);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate  * Print an authentication or encryption algorithm
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate static int
2810Sstevel@tonic-gate dump_generic_alg(uint8_t alg_num, int proto_num, FILE *where)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	struct ipsecalgent *alg;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_num, proto_num, NULL);
2860Sstevel@tonic-gate 	if (alg == NULL) {
2874064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
2884064Smarkfen 		    "<unknown %u>"), alg_num) < 0)
2890Sstevel@tonic-gate 			return (-1);
2900Sstevel@tonic-gate 		return (0);
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * Special-case <none> for backward output compat.
2950Sstevel@tonic-gate 	 * Assume that SADB_AALG_NONE == SADB_EALG_NONE.
2960Sstevel@tonic-gate 	 */
2970Sstevel@tonic-gate 	if (alg_num == SADB_AALG_NONE) {
2984064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
2994064Smarkfen 		    "<none>"), where) == EOF)
3000Sstevel@tonic-gate 			return (-1);
3010Sstevel@tonic-gate 	} else {
3020Sstevel@tonic-gate 		if (fputs(alg->a_names[0], where) == EOF)
3030Sstevel@tonic-gate 			return (-1);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	freeipsecalgent(alg);
3070Sstevel@tonic-gate 	return (0);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate int
3110Sstevel@tonic-gate dump_aalg(uint8_t aalg, FILE *where)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	return (dump_generic_alg(aalg, IPSEC_PROTO_AH, where));
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate int
3170Sstevel@tonic-gate dump_ealg(uint8_t ealg, FILE *where)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	return (dump_generic_alg(ealg, IPSEC_PROTO_ESP, where));
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate  * Print an SADB_IDENTTYPE string
3240Sstevel@tonic-gate  *
3250Sstevel@tonic-gate  * Also return TRUE if the actual ident may be printed, FALSE if not.
3260Sstevel@tonic-gate  *
3270Sstevel@tonic-gate  * If rc is not NULL, set its value to -1 if an error occured while writing
3280Sstevel@tonic-gate  * to the specified file, zero otherwise.
3290Sstevel@tonic-gate  */
3300Sstevel@tonic-gate boolean_t
3310Sstevel@tonic-gate dump_sadb_idtype(uint8_t idtype, FILE *where, int *rc)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate 	boolean_t canprint = B_TRUE;
3340Sstevel@tonic-gate 	int rc_val = 0;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	switch (idtype) {
3370Sstevel@tonic-gate 	case SADB_IDENTTYPE_PREFIX:
3384064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "prefix"), where) == EOF)
3390Sstevel@tonic-gate 			rc_val = -1;
3400Sstevel@tonic-gate 		break;
3410Sstevel@tonic-gate 	case SADB_IDENTTYPE_FQDN:
3424064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "FQDN"), where) == EOF)
3430Sstevel@tonic-gate 			rc_val = -1;
3440Sstevel@tonic-gate 		break;
3450Sstevel@tonic-gate 	case SADB_IDENTTYPE_USER_FQDN:
3464064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
3474064Smarkfen 		    "user-FQDN (mbox)"), where) == EOF)
3480Sstevel@tonic-gate 			rc_val = -1;
3490Sstevel@tonic-gate 		break;
3500Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_DN:
3514064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Distinguished Name"),
3520Sstevel@tonic-gate 		    where) == EOF)
3530Sstevel@tonic-gate 			rc_val = -1;
3540Sstevel@tonic-gate 		canprint = B_FALSE;
3550Sstevel@tonic-gate 		break;
3560Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_GN:
3574064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Generic Name"),
3584064Smarkfen 		    where) == EOF)
3590Sstevel@tonic-gate 			rc_val = -1;
3600Sstevel@tonic-gate 		canprint = B_FALSE;
3610Sstevel@tonic-gate 		break;
3620Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_KEY_ID:
3634064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Generic key id"),
3644064Smarkfen 		    where) == EOF)
3650Sstevel@tonic-gate 			rc_val = -1;
3660Sstevel@tonic-gate 		break;
3670Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_ADDR_RANGE:
3684064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Address range"), where) == EOF)
3690Sstevel@tonic-gate 			rc_val = -1;
3700Sstevel@tonic-gate 		break;
3710Sstevel@tonic-gate 	default:
3724064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
3734064Smarkfen 		    "<unknown %u>"), idtype) < 0)
3740Sstevel@tonic-gate 			rc_val = -1;
3750Sstevel@tonic-gate 		break;
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (rc != NULL)
3790Sstevel@tonic-gate 		*rc = rc_val;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	return (canprint);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate /*
3850Sstevel@tonic-gate  * Slice an argv/argc vector from an interactive line or a read-file line.
3860Sstevel@tonic-gate  */
3870Sstevel@tonic-gate static int
3880Sstevel@tonic-gate create_argv(char *ibuf, int *newargc, char ***thisargv)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	unsigned int argvlen = START_ARG;
3910Sstevel@tonic-gate 	char **current;
3920Sstevel@tonic-gate 	boolean_t firstchar = B_TRUE;
3930Sstevel@tonic-gate 	boolean_t inquotes = B_FALSE;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	*thisargv = malloc(sizeof (char *) * argvlen);
3960Sstevel@tonic-gate 	if ((*thisargv) == NULL)
3970Sstevel@tonic-gate 		return (MEMORY_ALLOCATION);
3980Sstevel@tonic-gate 	current = *thisargv;
3990Sstevel@tonic-gate 	*current = NULL;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	for (; *ibuf != '\0'; ibuf++) {
4020Sstevel@tonic-gate 		if (isspace(*ibuf)) {
4030Sstevel@tonic-gate 			if (inquotes) {
4040Sstevel@tonic-gate 				continue;
4050Sstevel@tonic-gate 			}
4060Sstevel@tonic-gate 			if (*current != NULL) {
4070Sstevel@tonic-gate 				*ibuf = '\0';
4080Sstevel@tonic-gate 				current++;
4090Sstevel@tonic-gate 				if (*thisargv + argvlen == current) {
4100Sstevel@tonic-gate 					/* Regrow ***thisargv. */
4110Sstevel@tonic-gate 					if (argvlen == TOO_MANY_ARGS) {
4120Sstevel@tonic-gate 						free(*thisargv);
4130Sstevel@tonic-gate 						return (TOO_MANY_TOKENS);
4140Sstevel@tonic-gate 					}
4150Sstevel@tonic-gate 					/* Double the allocation. */
4160Sstevel@tonic-gate 					current = realloc(*thisargv,
4170Sstevel@tonic-gate 					    sizeof (char *) * (argvlen << 1));
4180Sstevel@tonic-gate 					if (current == NULL) {
4190Sstevel@tonic-gate 						free(*thisargv);
4200Sstevel@tonic-gate 						return (MEMORY_ALLOCATION);
4210Sstevel@tonic-gate 					}
4220Sstevel@tonic-gate 					*thisargv = current;
4230Sstevel@tonic-gate 					current += argvlen;
4240Sstevel@tonic-gate 					argvlen <<= 1;	/* Double the size. */
4250Sstevel@tonic-gate 				}
4260Sstevel@tonic-gate 				*current = NULL;
4270Sstevel@tonic-gate 			}
4280Sstevel@tonic-gate 		} else {
4290Sstevel@tonic-gate 			if (firstchar) {
4300Sstevel@tonic-gate 				firstchar = B_FALSE;
4314235Smarkfen 				if (*ibuf == COMMENT_CHAR || *ibuf == '\n') {
4320Sstevel@tonic-gate 					free(*thisargv);
4330Sstevel@tonic-gate 					return (COMMENT_LINE);
4340Sstevel@tonic-gate 				}
4350Sstevel@tonic-gate 			}
4360Sstevel@tonic-gate 			if (*ibuf == QUOTE_CHAR) {
4370Sstevel@tonic-gate 				if (inquotes) {
4380Sstevel@tonic-gate 					inquotes = B_FALSE;
4390Sstevel@tonic-gate 					*ibuf = '\0';
4400Sstevel@tonic-gate 				} else {
4410Sstevel@tonic-gate 					inquotes = B_TRUE;
4420Sstevel@tonic-gate 				}
4430Sstevel@tonic-gate 				continue;
4440Sstevel@tonic-gate 			}
4450Sstevel@tonic-gate 			if (*current == NULL) {
4460Sstevel@tonic-gate 				*current = ibuf;
4470Sstevel@tonic-gate 				(*newargc)++;
4480Sstevel@tonic-gate 			}
4490Sstevel@tonic-gate 		}
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	/*
4530Sstevel@tonic-gate 	 * Tricky corner case...
4540Sstevel@tonic-gate 	 * I've parsed _exactly_ the amount of args as I have space.  It
4550Sstevel@tonic-gate 	 * won't return NULL-terminated, and bad things will happen to
4560Sstevel@tonic-gate 	 * the caller.
4570Sstevel@tonic-gate 	 */
4580Sstevel@tonic-gate 	if (argvlen == *newargc) {
4590Sstevel@tonic-gate 		current = realloc(*thisargv, sizeof (char *) * (argvlen + 1));
4600Sstevel@tonic-gate 		if (current == NULL) {
4610Sstevel@tonic-gate 			free(*thisargv);
4620Sstevel@tonic-gate 			return (MEMORY_ALLOCATION);
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 		*thisargv = current;
4650Sstevel@tonic-gate 		current[argvlen] = NULL;
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	return (SUCCESS);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /*
4729086SVladimir.Kotal@Sun.COM  * init interactive mode if needed and not yet initialized
4739086SVladimir.Kotal@Sun.COM  */
4749086SVladimir.Kotal@Sun.COM static void
4759086SVladimir.Kotal@Sun.COM init_interactive(FILE *infile, CplMatchFn *match_fn)
4769086SVladimir.Kotal@Sun.COM {
4779086SVladimir.Kotal@Sun.COM 	if (infile == stdin) {
4789086SVladimir.Kotal@Sun.COM 		if (gl == NULL) {
4799086SVladimir.Kotal@Sun.COM 			if ((gl = new_GetLine(MAX_LINE_LEN,
4809086SVladimir.Kotal@Sun.COM 			    MAX_CMD_HIST)) == NULL)
4819086SVladimir.Kotal@Sun.COM 				errx(1, dgettext(TEXT_DOMAIN,
4829086SVladimir.Kotal@Sun.COM 				    "tecla initialization failed"));
4839086SVladimir.Kotal@Sun.COM 
4849086SVladimir.Kotal@Sun.COM 			if (gl_customize_completion(gl, NULL,
4859086SVladimir.Kotal@Sun.COM 			    match_fn) != 0) {
4869086SVladimir.Kotal@Sun.COM 				(void) del_GetLine(gl);
4879086SVladimir.Kotal@Sun.COM 				errx(1, dgettext(TEXT_DOMAIN,
4889086SVladimir.Kotal@Sun.COM 				    "tab completion failed to initialize"));
4899086SVladimir.Kotal@Sun.COM 			}
4909086SVladimir.Kotal@Sun.COM 
4919086SVladimir.Kotal@Sun.COM 			/*
4929086SVladimir.Kotal@Sun.COM 			 * In interactive mode we only want to terminate
4939086SVladimir.Kotal@Sun.COM 			 * when explicitly requested (e.g. by a command).
4949086SVladimir.Kotal@Sun.COM 			 */
4959086SVladimir.Kotal@Sun.COM 			(void) sigset(SIGINT, SIG_IGN);
4969086SVladimir.Kotal@Sun.COM 		}
4979086SVladimir.Kotal@Sun.COM 	} else {
4989086SVladimir.Kotal@Sun.COM 		readfile = B_TRUE;
4999086SVladimir.Kotal@Sun.COM 	}
5009086SVladimir.Kotal@Sun.COM }
5019086SVladimir.Kotal@Sun.COM 
5029086SVladimir.Kotal@Sun.COM /*
5039086SVladimir.Kotal@Sun.COM  * free tecla data structure
5049086SVladimir.Kotal@Sun.COM  */
5059086SVladimir.Kotal@Sun.COM static void
5069086SVladimir.Kotal@Sun.COM fini_interactive(void)
5079086SVladimir.Kotal@Sun.COM {
5089086SVladimir.Kotal@Sun.COM 	if (gl != NULL)
5099086SVladimir.Kotal@Sun.COM 		(void) del_GetLine(gl);
5109086SVladimir.Kotal@Sun.COM }
5119086SVladimir.Kotal@Sun.COM 
5129086SVladimir.Kotal@Sun.COM /*
5139086SVladimir.Kotal@Sun.COM  * Get single input line, wrapping around interactive and non-interactive
5149086SVladimir.Kotal@Sun.COM  * mode.
5159086SVladimir.Kotal@Sun.COM  */
5169086SVladimir.Kotal@Sun.COM static char *
5179086SVladimir.Kotal@Sun.COM do_getstr(FILE *infile, char *prompt, char *ibuf, size_t ibuf_size)
5189086SVladimir.Kotal@Sun.COM {
5199086SVladimir.Kotal@Sun.COM 	char	*line;
5209086SVladimir.Kotal@Sun.COM 
5219086SVladimir.Kotal@Sun.COM 	if (infile != stdin)
5229086SVladimir.Kotal@Sun.COM 		return (fgets(ibuf, ibuf_size, infile));
5239086SVladimir.Kotal@Sun.COM 
5249086SVladimir.Kotal@Sun.COM 	/*
5259086SVladimir.Kotal@Sun.COM 	 * If the user hits ^C then we want to catch it and
5269086SVladimir.Kotal@Sun.COM 	 * start over.  If the user hits EOF then we want to
5279086SVladimir.Kotal@Sun.COM 	 * bail out.
5289086SVladimir.Kotal@Sun.COM 	 */
5299086SVladimir.Kotal@Sun.COM once_again:
5309086SVladimir.Kotal@Sun.COM 	line = gl_get_line(gl, prompt, NULL, -1);
5319086SVladimir.Kotal@Sun.COM 	if (gl_return_status(gl) == GLR_SIGNAL) {
5329086SVladimir.Kotal@Sun.COM 		gl_abandon_line(gl);
5339086SVladimir.Kotal@Sun.COM 		goto once_again;
5349086SVladimir.Kotal@Sun.COM 	} else if (gl_return_status(gl) == GLR_ERROR) {
5359086SVladimir.Kotal@Sun.COM 		gl_abandon_line(gl);
5369086SVladimir.Kotal@Sun.COM 		errx(1, dgettext(TEXT_DOMAIN, "Error reading terminal: %s\n"),
5379086SVladimir.Kotal@Sun.COM 		    gl_error_message(gl, NULL, 0));
5389086SVladimir.Kotal@Sun.COM 	} else {
5399086SVladimir.Kotal@Sun.COM 		if (line != NULL) {
5409086SVladimir.Kotal@Sun.COM 			if (strlcpy(ibuf, line, ibuf_size) >= ibuf_size)
5419086SVladimir.Kotal@Sun.COM 				warnx(dgettext(TEXT_DOMAIN,
5429086SVladimir.Kotal@Sun.COM 				    "Line too long (max=%d chars)"),
5439086SVladimir.Kotal@Sun.COM 				    ibuf_size);
5449086SVladimir.Kotal@Sun.COM 			line = ibuf;
5459086SVladimir.Kotal@Sun.COM 		}
5469086SVladimir.Kotal@Sun.COM 	}
5479086SVladimir.Kotal@Sun.COM 
5489086SVladimir.Kotal@Sun.COM 	return (line);
5499086SVladimir.Kotal@Sun.COM }
5509086SVladimir.Kotal@Sun.COM 
5519086SVladimir.Kotal@Sun.COM /*
5520Sstevel@tonic-gate  * Enter a mode where commands are read from a file.  Treat stdin special.
5530Sstevel@tonic-gate  */
5540Sstevel@tonic-gate void
5554235Smarkfen do_interactive(FILE *infile, char *configfile, char *promptstring,
5569086SVladimir.Kotal@Sun.COM     char *my_fmri, parse_cmdln_fn parseit, CplMatchFn *match_fn)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	char		ibuf[IBUF_SIZE], holder[IBUF_SIZE];
5594235Smarkfen 	char		*hptr, **thisargv, *ebuf;
5600Sstevel@tonic-gate 	int		thisargc;
5610Sstevel@tonic-gate 	boolean_t	continue_in_progress = B_FALSE;
5629086SVladimir.Kotal@Sun.COM 	char		*s;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	(void) setjmp(env);
5650Sstevel@tonic-gate 
5664235Smarkfen 	ebuf = NULL;
5670Sstevel@tonic-gate 	interactive = B_TRUE;
5680Sstevel@tonic-gate 	bzero(ibuf, IBUF_SIZE);
5690Sstevel@tonic-gate 
5709086SVladimir.Kotal@Sun.COM 	/* panics for us */
5719086SVladimir.Kotal@Sun.COM 	init_interactive(infile, match_fn);
5720Sstevel@tonic-gate 
5739086SVladimir.Kotal@Sun.COM 	while ((s = do_getstr(infile, promptstring, ibuf, IBUF_SIZE)) != NULL) {
5740Sstevel@tonic-gate 		if (readfile)
5750Sstevel@tonic-gate 			lineno++;
5760Sstevel@tonic-gate 		thisargc = 0;
5770Sstevel@tonic-gate 		thisargv = NULL;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		/*
5800Sstevel@tonic-gate 		 * Check byte IBUF_SIZE - 2, because byte IBUF_SIZE - 1 will
5810Sstevel@tonic-gate 		 * be null-terminated because of fgets().
5820Sstevel@tonic-gate 		 */
5830Sstevel@tonic-gate 		if (ibuf[IBUF_SIZE - 2] != '\0') {
5849086SVladimir.Kotal@Sun.COM 			if (infile == stdin) {
5859086SVladimir.Kotal@Sun.COM 				/* do_getstr() issued a warning already */
5869086SVladimir.Kotal@Sun.COM 				bzero(ibuf, IBUF_SIZE);
5879086SVladimir.Kotal@Sun.COM 				continue;
5889086SVladimir.Kotal@Sun.COM 			} else {
5899086SVladimir.Kotal@Sun.COM 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
5909086SVladimir.Kotal@Sun.COM 				    debugfile, dgettext(TEXT_DOMAIN,
5919086SVladimir.Kotal@Sun.COM 				    "Line %d too big."), lineno);
5929086SVladimir.Kotal@Sun.COM 			}
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 		if (!continue_in_progress) {
5960Sstevel@tonic-gate 			/* Use -2 because of \n from fgets. */
5970Sstevel@tonic-gate 			if (ibuf[strlen(ibuf) - 2] == CONT_CHAR) {
5980Sstevel@tonic-gate 				/*
5990Sstevel@tonic-gate 				 * Can use strcpy here, I've checked the
6000Sstevel@tonic-gate 				 * length already.
6010Sstevel@tonic-gate 				 */
6020Sstevel@tonic-gate 				(void) strcpy(holder, ibuf);
6030Sstevel@tonic-gate 				hptr = &(holder[strlen(holder)]);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
6060Sstevel@tonic-gate 				hptr[-2] = ' ';
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 				continue_in_progress = B_TRUE;
6090Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
6100Sstevel@tonic-gate 				continue;
6110Sstevel@tonic-gate 			}
6120Sstevel@tonic-gate 		} else {
6130Sstevel@tonic-gate 			/* Handle continuations... */
6140Sstevel@tonic-gate 			(void) strncpy(hptr, ibuf,
6150Sstevel@tonic-gate 			    (size_t)(&(holder[IBUF_SIZE]) - hptr));
6160Sstevel@tonic-gate 			if (holder[IBUF_SIZE - 1] != '\0') {
6174235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
6184235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
6194235Smarkfen 				    "Command buffer overrun."));
6200Sstevel@tonic-gate 			}
6210Sstevel@tonic-gate 			/* Use - 2 because of \n from fgets. */
6220Sstevel@tonic-gate 			if (hptr[strlen(hptr) - 2] == CONT_CHAR) {
6230Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
6240Sstevel@tonic-gate 				hptr += strlen(hptr);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
6270Sstevel@tonic-gate 				hptr[-2] = ' ';
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 				continue;
6300Sstevel@tonic-gate 			} else {
6310Sstevel@tonic-gate 				continue_in_progress = B_FALSE;
6320Sstevel@tonic-gate 				/*
6330Sstevel@tonic-gate 				 * I've already checked the length...
6340Sstevel@tonic-gate 				 */
6350Sstevel@tonic-gate 				(void) strcpy(ibuf, holder);
6360Sstevel@tonic-gate 			}
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 
6394235Smarkfen 		/*
6404235Smarkfen 		 * Just in case the command fails keep a copy of the
6414235Smarkfen 		 * command buffer for diagnostic output.
6424235Smarkfen 		 */
6434235Smarkfen 		if (readfile) {
6444235Smarkfen 			/*
6454235Smarkfen 			 * The error buffer needs to be big enough to
6464235Smarkfen 			 * hold the longest command string, plus
6474235Smarkfen 			 * some extra text, see below.
6484235Smarkfen 			 */
6494235Smarkfen 			ebuf = calloc((IBUF_SIZE * 2), sizeof (char));
6504235Smarkfen 			if (ebuf == NULL) {
6514235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
6524235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
6534235Smarkfen 				    "Memory allocation error."));
6544235Smarkfen 			} else {
6554235Smarkfen 				(void) snprintf(ebuf, (IBUF_SIZE * 2),
6564235Smarkfen 				    dgettext(TEXT_DOMAIN,
6574235Smarkfen 				    "Config file entry near line %u "
6584235Smarkfen 				    "caused error(s) or warnings:\n\n%s\n\n"),
6594235Smarkfen 				    lineno, ibuf);
6604235Smarkfen 			}
6614235Smarkfen 		}
6624235Smarkfen 
6630Sstevel@tonic-gate 		switch (create_argv(ibuf, &thisargc, &thisargv)) {
6640Sstevel@tonic-gate 		case TOO_MANY_TOKENS:
6654235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
6664235Smarkfen 			    dgettext(TEXT_DOMAIN, "Too many input tokens."));
6670Sstevel@tonic-gate 			break;
6680Sstevel@tonic-gate 		case MEMORY_ALLOCATION:
6694235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
6704235Smarkfen 			    dgettext(TEXT_DOMAIN, "Memory allocation error."));
6710Sstevel@tonic-gate 			break;
6720Sstevel@tonic-gate 		case COMMENT_LINE:
6730Sstevel@tonic-gate 			/* Comment line. */
6744235Smarkfen 			free(ebuf);
6750Sstevel@tonic-gate 			break;
6760Sstevel@tonic-gate 		default:
6774235Smarkfen 			if (thisargc != 0) {
6784235Smarkfen 				lines_parsed++;
6794235Smarkfen 				/* ebuf consumed */
6804342Spwernau 				parseit(thisargc, thisargv, ebuf, readfile);
6814235Smarkfen 			} else {
6824235Smarkfen 				free(ebuf);
6834235Smarkfen 			}
6840Sstevel@tonic-gate 			free(thisargv);
6850Sstevel@tonic-gate 			if (infile == stdin) {
6860Sstevel@tonic-gate 				(void) printf("%s", promptstring);
6870Sstevel@tonic-gate 				(void) fflush(stdout);
6880Sstevel@tonic-gate 			}
6890Sstevel@tonic-gate 			break;
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate 		bzero(ibuf, IBUF_SIZE);
6920Sstevel@tonic-gate 	}
6934342Spwernau 
6944342Spwernau 	/*
6954342Spwernau 	 * The following code is ipseckey specific. This should never be
6964342Spwernau 	 * used by ikeadm which also calls this function because ikeadm
6974342Spwernau 	 * only runs interactively. If this ever changes this code block
6984342Spwernau 	 * sould be revisited.
6994342Spwernau 	 */
7004342Spwernau 	if (readfile) {
7014342Spwernau 		if (lines_parsed != 0 && lines_added == 0) {
7024342Spwernau 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
7034342Spwernau 			    dgettext(TEXT_DOMAIN, "Configuration file did not "
7044342Spwernau 			    "contain any valid SAs"));
7054342Spwernau 		}
7064342Spwernau 
7074342Spwernau 		/*
7084342Spwernau 		 * There were errors. Putting the service in maintenance mode.
7094342Spwernau 		 * When svc.startd(1M) allows services to degrade themselves,
7104342Spwernau 		 * this should be revisited.
7114342Spwernau 		 *
7124342Spwernau 		 * If this function was called from a program running as a
7134342Spwernau 		 * smf_method(5), print a warning message. Don't spew out the
7144342Spwernau 		 * errors as these will end up in the smf(5) log file which is
7154342Spwernau 		 * publically readable, the errors may contain sensitive
7164342Spwernau 		 * information.
7174342Spwernau 		 */
7184342Spwernau 		if ((lines_added < lines_parsed) && (configfile != NULL)) {
7194342Spwernau 			if (my_fmri != NULL) {
7204342Spwernau 				ipsecutil_exit(SERVICE_BADCONF, my_fmri,
7214342Spwernau 				    debugfile, dgettext(TEXT_DOMAIN,
7224342Spwernau 				    "The configuration file contained %d "
7234342Spwernau 				    "errors.\n"
7244342Spwernau 				    "Manually check the configuration with:\n"
7254342Spwernau 				    "ipseckey -c %s\n"
7264342Spwernau 				    "Use svcadm(1M) to clear maintenance "
7274342Spwernau 				    "condition when errors are resolved.\n"),
7284342Spwernau 				    lines_parsed - lines_added, configfile);
7294342Spwernau 			} else {
7304342Spwernau 				EXIT_BADCONFIG(NULL);
7314342Spwernau 			}
7324342Spwernau 		} else {
7334342Spwernau 			if (my_fmri != NULL)
7344342Spwernau 				ipsecutil_exit(SERVICE_EXIT_OK, my_fmri,
7354342Spwernau 				    debugfile, dgettext(TEXT_DOMAIN,
7364342Spwernau 				    "%d actions successfully processed."),
7374342Spwernau 				    lines_added);
7384342Spwernau 		}
7394342Spwernau 	} else {
7409086SVladimir.Kotal@Sun.COM 		/* no newline upon Ctrl-D */
7419086SVladimir.Kotal@Sun.COM 		if (s != NULL)
7429086SVladimir.Kotal@Sun.COM 			(void) putchar('\n');
7430Sstevel@tonic-gate 		(void) fflush(stdout);
7440Sstevel@tonic-gate 	}
7459086SVladimir.Kotal@Sun.COM 
7469086SVladimir.Kotal@Sun.COM 	fini_interactive();
7479086SVladimir.Kotal@Sun.COM 
7484235Smarkfen 	EXIT_OK(NULL);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate /*
7520Sstevel@tonic-gate  * Functions to parse strings that represent a debug or privilege level.
7530Sstevel@tonic-gate  * These functions are copied from main.c and door.c in usr.lib/in.iked/common.
7540Sstevel@tonic-gate  * If this file evolves into a common library that may be used by in.iked
7550Sstevel@tonic-gate  * as well as the usr.sbin utilities, those duplicate functions should be
7560Sstevel@tonic-gate  * deleted.
7570Sstevel@tonic-gate  *
7580Sstevel@tonic-gate  * A privilege level may be represented by a simple keyword, corresponding
7590Sstevel@tonic-gate  * to one of the possible levels.  A debug level may be represented by a
7600Sstevel@tonic-gate  * series of keywords, separated by '+' or '-', indicating categories to
7610Sstevel@tonic-gate  * be added or removed from the set of categories in the debug level.
7620Sstevel@tonic-gate  * For example, +all-op corresponds to level 0xfffffffb (all flags except
7630Sstevel@tonic-gate  * for D_OP set); while p1+p2+pfkey corresponds to level 0x38.  Note that
7640Sstevel@tonic-gate  * the leading '+' is implicit; the first keyword in the list must be for
7650Sstevel@tonic-gate  * a category that is to be added.
7660Sstevel@tonic-gate  *
7670Sstevel@tonic-gate  * These parsing functions make use of a local version of strtok, strtok_d,
7680Sstevel@tonic-gate  * which includes an additional parameter, char *delim.  This param is filled
7690Sstevel@tonic-gate  * in with the character which ends the returned token.  In other words,
7700Sstevel@tonic-gate  * this version of strtok, in addition to returning the token, also returns
7710Sstevel@tonic-gate  * the single character delimiter from the original string which marked the
7720Sstevel@tonic-gate  * end of the token.
7730Sstevel@tonic-gate  */
7740Sstevel@tonic-gate static char *
7750Sstevel@tonic-gate strtok_d(char *string, const char *sepset, char *delim)
7760Sstevel@tonic-gate {
7770Sstevel@tonic-gate 	static char	*lasts;
7780Sstevel@tonic-gate 	char		*q, *r;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	/* first or subsequent call */
7810Sstevel@tonic-gate 	if (string == NULL)
7820Sstevel@tonic-gate 		string = lasts;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	if (string == 0)		/* return if no tokens remaining */
7850Sstevel@tonic-gate 		return (NULL);
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	q = string + strspn(string, sepset);	/* skip leading separators */
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	if (*q == '\0')			/* return if no tokens remaining */
7900Sstevel@tonic-gate 		return (NULL);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if ((r = strpbrk(q, sepset)) == NULL) {		/* move past token */
7930Sstevel@tonic-gate 		lasts = 0;	/* indicate that this is last token */
7940Sstevel@tonic-gate 	} else {
7950Sstevel@tonic-gate 		*delim = *r;	/* save delimitor */
7960Sstevel@tonic-gate 		*r = '\0';
7970Sstevel@tonic-gate 		lasts = r + 1;
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate 	return (q);
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate static keywdtab_t	privtab[] = {
8030Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"base" },
8040Sstevel@tonic-gate 	{ IKE_PRIV_MODKEYS,	"modkeys" },
8050Sstevel@tonic-gate 	{ IKE_PRIV_KEYMAT,	"keymat" },
8060Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"0" },
8070Sstevel@tonic-gate };
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate int
8100Sstevel@tonic-gate privstr2num(char *str)
8110Sstevel@tonic-gate {
8120Sstevel@tonic-gate 	keywdtab_t	*pp;
8130Sstevel@tonic-gate 	char		*endp;
8140Sstevel@tonic-gate 	int		 priv;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	for (pp = privtab; pp < A_END(privtab); pp++) {
8170Sstevel@tonic-gate 		if (strcasecmp(str, pp->kw_str) == 0)
8180Sstevel@tonic-gate 			return (pp->kw_tag);
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	priv = strtol(str, &endp, 0);
8220Sstevel@tonic-gate 	if (*endp == '\0')
8230Sstevel@tonic-gate 		return (priv);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	return (-1);
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate static keywdtab_t	dbgtab[] = {
8290Sstevel@tonic-gate 	{ D_CERT,	"cert" },
8300Sstevel@tonic-gate 	{ D_KEY,	"key" },
8310Sstevel@tonic-gate 	{ D_OP,		"op" },
8320Sstevel@tonic-gate 	{ D_P1,		"p1" },
8330Sstevel@tonic-gate 	{ D_P1,		"phase1" },
8340Sstevel@tonic-gate 	{ D_P2,		"p2" },
8350Sstevel@tonic-gate 	{ D_P2,		"phase2" },
8360Sstevel@tonic-gate 	{ D_PFKEY,	"pfkey" },
8370Sstevel@tonic-gate 	{ D_POL,	"pol" },
8380Sstevel@tonic-gate 	{ D_POL,	"policy" },
8390Sstevel@tonic-gate 	{ D_PROP,	"prop" },
8400Sstevel@tonic-gate 	{ D_DOOR,	"door" },
8410Sstevel@tonic-gate 	{ D_CONFIG,	"config" },
842*10934Ssommerfeld@sun.com 	{ D_LABEL,	"label" },
8430Sstevel@tonic-gate 	{ D_ALL,	"all" },
8440Sstevel@tonic-gate 	{ 0,		"0" },
8450Sstevel@tonic-gate };
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate int
8480Sstevel@tonic-gate dbgstr2num(char *str)
8490Sstevel@tonic-gate {
8500Sstevel@tonic-gate 	keywdtab_t	*dp;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	for (dp = dbgtab; dp < A_END(dbgtab); dp++) {
8530Sstevel@tonic-gate 		if (strcasecmp(str, dp->kw_str) == 0)
8540Sstevel@tonic-gate 			return (dp->kw_tag);
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 	return (D_INVALID);
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate int
8600Sstevel@tonic-gate parsedbgopts(char *optarg)
8610Sstevel@tonic-gate {
8620Sstevel@tonic-gate 	char	*argp, *endp, op, nextop;
8630Sstevel@tonic-gate 	int	mask = 0, new;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	mask = strtol(optarg, &endp, 0);
8660Sstevel@tonic-gate 	if (*endp == '\0')
8670Sstevel@tonic-gate 		return (mask);
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	op = optarg[0];
8700Sstevel@tonic-gate 	if (op != '-')
8710Sstevel@tonic-gate 		op = '+';
8720Sstevel@tonic-gate 	argp = strtok_d(optarg, "+-", &nextop);
8730Sstevel@tonic-gate 	do {
8740Sstevel@tonic-gate 		new = dbgstr2num(argp);
8750Sstevel@tonic-gate 		if (new == D_INVALID) {
8760Sstevel@tonic-gate 			/* we encountered an invalid keywd */
8770Sstevel@tonic-gate 			return (new);
8780Sstevel@tonic-gate 		}
8790Sstevel@tonic-gate 		if (op == '+') {
8800Sstevel@tonic-gate 			mask |= new;
8810Sstevel@tonic-gate 		} else {
8820Sstevel@tonic-gate 			mask &= ~new;
8830Sstevel@tonic-gate 		}
8840Sstevel@tonic-gate 		op = nextop;
8850Sstevel@tonic-gate 	} while ((argp = strtok_d(NULL, "+-", &nextop)) != NULL);
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	return (mask);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate /*
8920Sstevel@tonic-gate  * functions to manipulate the kmcookie-label mapping file
8930Sstevel@tonic-gate  */
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate /*
8960Sstevel@tonic-gate  * Open, lockf, fdopen the given file, returning a FILE * on success,
8970Sstevel@tonic-gate  * or NULL on failure.
8980Sstevel@tonic-gate  */
8990Sstevel@tonic-gate FILE *
9000Sstevel@tonic-gate kmc_open_and_lock(char *name)
9010Sstevel@tonic-gate {
9020Sstevel@tonic-gate 	int	fd, rtnerr;
9030Sstevel@tonic-gate 	FILE	*fp;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	if ((fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
9060Sstevel@tonic-gate 		return (NULL);
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate 	if (lockf(fd, F_LOCK, 0) < 0) {
9090Sstevel@tonic-gate 		return (NULL);
9100Sstevel@tonic-gate 	}
9110Sstevel@tonic-gate 	if ((fp = fdopen(fd, "a+")) == NULL) {
9120Sstevel@tonic-gate 		return (NULL);
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate 	if (fseek(fp, 0, SEEK_SET) < 0) {
9150Sstevel@tonic-gate 		/* save errno in case fclose changes it */
9160Sstevel@tonic-gate 		rtnerr = errno;
9170Sstevel@tonic-gate 		(void) fclose(fp);
9180Sstevel@tonic-gate 		errno = rtnerr;
9190Sstevel@tonic-gate 		return (NULL);
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 	return (fp);
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate /*
9250Sstevel@tonic-gate  * Extract an integer cookie and string label from a line from the
9260Sstevel@tonic-gate  * kmcookie-label file.  Return -1 on failure, 0 on success.
9270Sstevel@tonic-gate  */
9280Sstevel@tonic-gate int
9290Sstevel@tonic-gate kmc_parse_line(char *line, int *cookie, char **label)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate 	char	*cookiestr;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	*cookie = 0;
9340Sstevel@tonic-gate 	*label = NULL;
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	cookiestr = strtok(line, " \t\n");
9370Sstevel@tonic-gate 	if (cookiestr == NULL) {
9380Sstevel@tonic-gate 		return (-1);
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	/* Everything that follows, up to the newline, is the label. */
9420Sstevel@tonic-gate 	*label = strtok(NULL, "\n");
9430Sstevel@tonic-gate 	if (*label == NULL) {
9440Sstevel@tonic-gate 		return (-1);
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	*cookie = atoi(cookiestr);
9480Sstevel@tonic-gate 	return (0);
9490Sstevel@tonic-gate }
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate /*
9520Sstevel@tonic-gate  * Insert a mapping into the file (if it's not already there), given the
9530Sstevel@tonic-gate  * new label.  Return the assigned cookie, or -1 on error.
9540Sstevel@tonic-gate  */
9550Sstevel@tonic-gate int
9560Sstevel@tonic-gate kmc_insert_mapping(char *label)
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate 	FILE	*map;
9594757Sdanmcd 	char	linebuf[IBUF_SIZE];
9600Sstevel@tonic-gate 	char	*cur_label;
9610Sstevel@tonic-gate 	int	max_cookie = 0, cur_cookie, rtn_cookie;
9620Sstevel@tonic-gate 	int	rtnerr = 0;
9630Sstevel@tonic-gate 	boolean_t	found = B_FALSE;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/* open and lock the file; will sleep until lock is available */
9660Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
9670Sstevel@tonic-gate 		/* kmc_open_and_lock() sets errno appropriately */
9680Sstevel@tonic-gate 		return (-1);
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
9720Sstevel@tonic-gate 
9734757Sdanmcd 		/* Skip blank lines, which often come near EOF. */
9744757Sdanmcd 		if (strlen(linebuf) == 0)
9754757Sdanmcd 			continue;
9764757Sdanmcd 
9770Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
9780Sstevel@tonic-gate 			rtnerr = EINVAL;
9790Sstevel@tonic-gate 			goto error;
9800Sstevel@tonic-gate 		}
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 		if (cur_cookie > max_cookie)
9830Sstevel@tonic-gate 			max_cookie = cur_cookie;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 		if ((!found) && (strcmp(cur_label, label) == 0)) {
9860Sstevel@tonic-gate 			found = B_TRUE;
9870Sstevel@tonic-gate 			rtn_cookie = cur_cookie;
9880Sstevel@tonic-gate 		}
9890Sstevel@tonic-gate 	}
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	if (!found) {
9920Sstevel@tonic-gate 		rtn_cookie = ++max_cookie;
9930Sstevel@tonic-gate 		if ((fprintf(map, "%u\t%s\n", rtn_cookie, label) < 0) ||
9940Sstevel@tonic-gate 		    (fflush(map) < 0)) {
9950Sstevel@tonic-gate 			rtnerr = errno;
9960Sstevel@tonic-gate 			goto error;
9970Sstevel@tonic-gate 		}
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 	(void) fclose(map);
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	return (rtn_cookie);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate error:
10040Sstevel@tonic-gate 	(void) fclose(map);
10050Sstevel@tonic-gate 	errno = rtnerr;
10060Sstevel@tonic-gate 	return (-1);
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate /*
10100Sstevel@tonic-gate  * Lookup the given cookie and return its corresponding label.  Return
10110Sstevel@tonic-gate  * a pointer to the label on success, NULL on error (or if the label is
10120Sstevel@tonic-gate  * not found).  Note that the returned label pointer points to a static
10130Sstevel@tonic-gate  * string, so the label will be overwritten by a subsequent call to the
10140Sstevel@tonic-gate  * function; the function is also not thread-safe as a result.
10150Sstevel@tonic-gate  */
10160Sstevel@tonic-gate char *
10170Sstevel@tonic-gate kmc_lookup_by_cookie(int cookie)
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	FILE		*map;
10204757Sdanmcd 	static char	linebuf[IBUF_SIZE];
10210Sstevel@tonic-gate 	char		*cur_label;
10220Sstevel@tonic-gate 	int		cur_cookie;
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
10250Sstevel@tonic-gate 		return (NULL);
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
10310Sstevel@tonic-gate 			(void) fclose(map);
10320Sstevel@tonic-gate 			return (NULL);
10330Sstevel@tonic-gate 		}
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 		if (cookie == cur_cookie) {
10360Sstevel@tonic-gate 			(void) fclose(map);
10370Sstevel@tonic-gate 			return (cur_label);
10380Sstevel@tonic-gate 		}
10390Sstevel@tonic-gate 	}
10400Sstevel@tonic-gate 	(void) fclose(map);
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	return (NULL);
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate /*
10460Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
10470Sstevel@tonic-gate  * Return values include:
10480Sstevel@tonic-gate  *
10490Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
10500Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
10510Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
10520Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
10530Sstevel@tonic-gate  *		extv[0].
10540Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
10550Sstevel@tonic-gate  *		unknown header.
10560Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
10570Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
10580Sstevel@tonic-gate  *
10590Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
10600Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
10610Sstevel@tonic-gate  */
10620Sstevel@tonic-gate int
10630Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize,
10640Sstevel@tonic-gate     char *diag_buf, uint_t diag_buf_len)
10650Sstevel@tonic-gate {
10660Sstevel@tonic-gate 	int i;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	if (diag_buf != NULL)
10690Sstevel@tonic-gate 		diag_buf[0] = '\0';
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	for (i = 1; i <= SPD_EXT_MAX; i++)
10720Sstevel@tonic-gate 		extv[i] = NULL;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	i = 0;
10750Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	extv[0] = (spd_ext_t *)(basehdr + 1);
10780Sstevel@tonic-gate 	msgsize = SPD_64TO8(msgsize);
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	while ((char *)extv[0] < ((char *)basehdr + msgsize)) {
10810Sstevel@tonic-gate 		/* Check for unknown headers. */
10820Sstevel@tonic-gate 		i++;
10830Sstevel@tonic-gate 		if (extv[0]->spd_ext_type == 0 ||
10840Sstevel@tonic-gate 		    extv[0]->spd_ext_type > SPD_EXT_MAX) {
10850Sstevel@tonic-gate 			if (diag_buf != NULL) {
10860Sstevel@tonic-gate 				(void) snprintf(diag_buf, diag_buf_len,
10870Sstevel@tonic-gate 				    "spdsock ext 0x%X unknown: 0x%X",
10880Sstevel@tonic-gate 				    i, extv[0]->spd_ext_type);
10890Sstevel@tonic-gate 			}
10900Sstevel@tonic-gate 			return (KGE_UNK);
10910Sstevel@tonic-gate 		}
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		/*
10940Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
10950Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
10960Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
10970Sstevel@tonic-gate 		 */
10980Sstevel@tonic-gate 		if (extv[0]->spd_ext_len == 0 ||
10990Sstevel@tonic-gate 		    (uint8_t *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
11000Sstevel@tonic-gate 		    (uint8_t *)((uint8_t *)basehdr + msgsize))
11010Sstevel@tonic-gate 			return (KGE_LEN);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 		/* Check for redundant headers. */
11040Sstevel@tonic-gate 		if (extv[extv[0]->spd_ext_type] != NULL)
11050Sstevel@tonic-gate 			return (KGE_DUP);
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
11080Sstevel@tonic-gate 		extv[extv[0]->spd_ext_type] = extv[0];
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
11110Sstevel@tonic-gate 		extv[0] = (spd_ext_t *)
11120Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	/* Everything's cool. */
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	/*
11180Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
11190Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
11200Sstevel@tonic-gate 	 */
11210Sstevel@tonic-gate 	if (extv[0] == (spd_ext_t *)(basehdr + 1))
11220Sstevel@tonic-gate 		extv[0] = NULL;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	return (KGE_OK);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate const char *
11280Sstevel@tonic-gate spdsock_diag(int diagnostic)
11290Sstevel@tonic-gate {
11300Sstevel@tonic-gate 	switch (diagnostic) {
11310Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NONE:
11324064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no error"));
11330Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNKNOWN_EXT:
11344064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown extension"));
11350Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_EXTLEN:
11364064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad extension length"));
11370Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_RULE_EXT:
11384064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no rule extension"));
11390Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_ADDR_LEN:
11404064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad address len"));
11410Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MIXED_AF:
11424064Smarkfen 		return (dgettext(TEXT_DOMAIN, "mixed address family"));
11430Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_NO_MEM:
11444064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: no memory"));
11450Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT:
11464064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: wrong action count"));
11470Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_TYPE:
11484064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad type"));
11490Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_FLAGS:
11504064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad flags"));
11510Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_INCON_FLAGS:
11524064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: inconsistent flags"));
11530Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLPORT:
11544064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local port"));
11550Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLPORT:
11564064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local port"));
11570Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMPORT:
11584064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote port"));
11590Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMPORT:
11604064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote port"));
11610Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_PROTO:
11624064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed proto"));
11630Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_PROTO:
11644064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate proto"));
11650Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLADDR:
11664064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local address"));
11670Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLADDR:
11684064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local address"));
11690Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMADDR:
11704064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote address"));
11710Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMADDR:
11724064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote address"));
11730Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_ACTION:
11744064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed action"));
11750Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_ACTION:
11764064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate action"));
11770Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULE:
11784064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed rule"));
11790Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULE:
11804064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate rule"));
11810Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULESET:
11824064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed ruleset"));
11830Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULESET:
11844064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate ruleset"));
11850Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_INVALID_RULE_INDEX:
11864064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid rule index"));
11870Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_SPDID:
11884064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad spdid"));
11890Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_MSG_TYPE:
11904064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad message type"));
11910Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_ALG:
11924064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH algorithm"));
11930Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG:
11944064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11954064Smarkfen 		    "unsupported ESP encryption algorithm"));
11960Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG:
11974064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11984064Smarkfen 		    "unsupported ESP authentication algorithm"));
11990Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_KEYSIZE:
12004064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH key size"));
12010Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_KEYSIZE:
12024064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12034064Smarkfen 		    "unsupported ESP encryption key size"));
12040Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_KEYSIZE:
12054064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12064064Smarkfen 		    "unsupported ESP authentication key size"));
12070Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_ACTION_EXT:
12084064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No ACTION extension"));
12090Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_ID_RANGE:
12104064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid algorithm identifer"));
12110Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES:
12124064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12134064Smarkfen 		    "number of key sizes inconsistent"));
12140Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES:
12154064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12164064Smarkfen 		    "number of block sizes inconsistent"));
12170Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN:
12184064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid mechanism name length"));
12193055Sdanmcd 	case SPD_DIAGNOSTIC_NOT_GLOBAL_OP:
12204064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12214064Smarkfen 		    "operation not applicable to all policies"));
12223055Sdanmcd 	case SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS:
12234064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12244064Smarkfen 		    "using selectors on a transport-mode tunnel"));
12250Sstevel@tonic-gate 	default:
12264064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown diagnostic"));
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate }
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate /*
12310Sstevel@tonic-gate  * PF_KEY Diagnostic table.
12320Sstevel@tonic-gate  *
12330Sstevel@tonic-gate  * PF_KEY NOTE:  If you change pfkeyv2.h's SADB_X_DIAGNOSTIC_* space, this is
12340Sstevel@tonic-gate  * where you need to add new messages.
12350Sstevel@tonic-gate  */
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate const char *
12380Sstevel@tonic-gate keysock_diag(int diagnostic)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate 	switch (diagnostic) {
12413055Sdanmcd 	case SADB_X_DIAGNOSTIC_NONE:
12424064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No diagnostic"));
12430Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_MSG:
12444064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown message type"));
12450Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_EXT:
12464064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown extension type"));
12470Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EXTLEN:
12484064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad extension length"));
12490Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE:
12504064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12514064Smarkfen 		    "Unknown Security Association type"));
12520Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SATYPE_NEEDED:
12534064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12544064Smarkfen 		    "Specific Security Association type needed"));
12550Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_SADBS:
12564064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12574064Smarkfen 		    "No Security Association Databases present"));
12580Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_EXT:
12594064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12604064Smarkfen 		    "No extensions needed for message"));
12610Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC_AF:
12624064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address family"));
12630Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST_AF:
12644064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12654064Smarkfen 		    "Bad destination address family"));
12660Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_PROXY_AF:
12674064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12684064Smarkfen 		    "Bad inner-source address family"));
12690Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AF_MISMATCH:
12704064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12714064Smarkfen 		    "Source/destination address family mismatch"));
12720Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC:
12734064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address value"));
12740Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST:
12754064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad destination address value"));
12760Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ALLOC_HSERR:
12774064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12784064Smarkfen 		    "Soft allocations limit more than hard limit"));
12790Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BYTES_HSERR:
12804064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12814064Smarkfen 		    "Soft bytes limit more than hard limit"));
12820Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ADDTIME_HSERR:
12834064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft add expiration time later "
12840Sstevel@tonic-gate 		    "than hard expiration time"));
12850Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_USETIME_HSERR:
12864064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft use expiration time later "
12870Sstevel@tonic-gate 		    "than hard expiration time"));
12880Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SRC:
12894064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing source address"));
12900Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_DST:
12914064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing destination address"));
12920Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SA:
12934064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SA extension"));
12940Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_EKEY:
12954064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing encryption key"));
12960Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_AKEY:
12974064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing authentication key"));
12980Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_RANGE:
12994064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SPI range"));
13000Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SRC:
13014064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate source address"));
13020Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_DST:
13034064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate destination address"));
13040Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SA:
13054064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SA extension"));
13060Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_EKEY:
13074064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate encryption key"));
13080Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_AKEY:
13094064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate authentication key"));
13100Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_RANGE:
13114064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SPI range"));
13120Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SRC:
13134064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed source address"));
13140Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_DST:
13154064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed destination address"));
13160Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SA:
13174064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SA extension"));
13180Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_EKEY:
13194064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed encryption key"));
13200Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_AKEY:
13214064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed authentication key"));
13220Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_RANGE:
13234064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SPI range"));
13240Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AKEY_PRESENT:
13254064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Authentication key not needed"));
13260Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_EKEY_PRESENT:
13274064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Encryption key not needed"));
13280Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_PROP_PRESENT:
13294064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Proposal extension not needed"));
13300Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SUPP_PRESENT:
13314064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13324064Smarkfen 		    "Supported algorithms extension not needed"));
13330Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AALG:
13344064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13354064Smarkfen 		    "Unsupported authentication algorithm"));
13360Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EALG:
13374064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13384064Smarkfen 		    "Unsupported encryption algorithm"));
13390Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SAFLAGS:
13404064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA flags"));
13410Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SASTATE:
13424064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA state"));
13430Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AKEYBITS:
13444064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13454064Smarkfen 		    "Bad number of authentication bits"));
13460Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EKEYBITS:
13474064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13484064Smarkfen 		    "Bad number of encryption bits"));
13490Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ENCR_NOTSUPP:
13504064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13514064Smarkfen 		    "Encryption not supported for this SA type"));
13520Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_EKEY:
13534064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak encryption key"));
13540Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_AKEY:
13554064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak authentication key"));
13560Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMP:
13574064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13584064Smarkfen 		    "Duplicate key management protocol"));
13590Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMC:
13604064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13614064Smarkfen 		    "Duplicate key management cookie"));
13620Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_LOC:
13634064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T local address"));
13640Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_REM:
13654064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T remote address"));
13660Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_LOC:
13674064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T local address"));
13680Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_REM:
13694064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13704064Smarkfen 		    "Duplicate NAT-T remote address"));
13710Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC:
13724064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed NAT-T local address"));
13730Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM:
13744064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13754064Smarkfen 		    "Malformed NAT-T remote address"));
13760Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_PORTS:
13774064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T ports"));
13783055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_SRC:
13794064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing inner source address"));
13803055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_DST:
13814064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13824064Smarkfen 		    "Missing inner destination address"));
13833055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC:
13844064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13854064Smarkfen 		    "Duplicate inner source address"));
13863055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST:
13874064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13884064Smarkfen 		    "Duplicate inner destination address"));
13893055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC:
13904064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13914064Smarkfen 		    "Malformed inner source address"));
13923055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST:
13934064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13944064Smarkfen 		    "Malformed inner destination address"));
13953055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC:
13964064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13974064Smarkfen 		    "Invalid inner-source prefix length "));
13983055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_DST:
13994064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14004064Smarkfen 		    "Invalid inner-destination prefix length"));
14013055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF:
14024064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14034064Smarkfen 		    "Bad inner-destination address family"));
14043055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH:
14054064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14063055Sdanmcd 		    "Inner source/destination address family mismatch"));
14073055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF:
14084064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14094064Smarkfen 		    "Bad NAT-T remote address family"));
14103055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF:
14114064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14124064Smarkfen 		    "Bad NAT-T local address family"));
14133055Sdanmcd 	case SADB_X_DIAGNOSTIC_PROTO_MISMATCH:
14144064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14154064Smarkfen 		    "Source/desination protocol mismatch"));
14163055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_PROTO_MISMATCH:
14174064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14184064Smarkfen 		    "Inner source/desination protocol mismatch"));
14193055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUAL_PORT_SETS:
14204064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14214064Smarkfen 		    "Both inner ports and outer ports are set"));
14226668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE:
14236668Smarkfen 		return (dgettext(TEXT_DOMAIN,
14246668Smarkfen 		    "Pairing failed, target SA unsuitable for pairing"));
14256668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_ADD_MISMATCH:
14266668Smarkfen 		return (dgettext(TEXT_DOMAIN,
14276668Smarkfen 		    "Source/destination address differs from pair SA"));
14286668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_ALREADY:
14296668Smarkfen 		return (dgettext(TEXT_DOMAIN,
14306668Smarkfen 		    "Already paired with another security association"));
14316668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND:
14326668Smarkfen 		return (dgettext(TEXT_DOMAIN,
14336668Smarkfen 		    "Command failed, pair security association not found"));
14346668Smarkfen 	case SADB_X_DIAGNOSTIC_BAD_SA_DIRECTION:
14356668Smarkfen 		return (dgettext(TEXT_DOMAIN,
14366668Smarkfen 		    "Inappropriate SA direction"));
14376668Smarkfen 	case SADB_X_DIAGNOSTIC_SA_NOTFOUND:
14386668Smarkfen 		return (dgettext(TEXT_DOMAIN,
14396668Smarkfen 		    "Security association not found"));
14406668Smarkfen 	case SADB_X_DIAGNOSTIC_SA_EXPIRED:
14416668Smarkfen 		return (dgettext(TEXT_DOMAIN,
14426668Smarkfen 		    "Security association is not valid"));
144310019SMark.Fenwick@Sun.COM 	case SADB_X_DIAGNOSTIC_BAD_CTX:
144410019SMark.Fenwick@Sun.COM 		return (dgettext(TEXT_DOMAIN,
144510019SMark.Fenwick@Sun.COM 		    "Algorithm invalid or not supported by Crypto Framework"));
144610019SMark.Fenwick@Sun.COM 	case SADB_X_DIAGNOSTIC_INVALID_REPLAY:
144710019SMark.Fenwick@Sun.COM 		return (dgettext(TEXT_DOMAIN,
144810019SMark.Fenwick@Sun.COM 		    "Invalid Replay counter"));
144910019SMark.Fenwick@Sun.COM 	case SADB_X_DIAGNOSTIC_MISSING_LIFETIME:
145010019SMark.Fenwick@Sun.COM 		return (dgettext(TEXT_DOMAIN,
145110019SMark.Fenwick@Sun.COM 		    "Inappropriate lifetimes"));
14520Sstevel@tonic-gate 	default:
14534064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code"));
14540Sstevel@tonic-gate 	}
14550Sstevel@tonic-gate }
14563055Sdanmcd 
14573055Sdanmcd /*
14583055Sdanmcd  * Convert an IPv6 mask to a prefix len.  I assume all IPv6 masks are
14593055Sdanmcd  * contiguous, so I stop at the first zero bit!
14603055Sdanmcd  */
14613055Sdanmcd int
14623055Sdanmcd in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped)
14633055Sdanmcd {
14643055Sdanmcd 	int rc = 0;
14653055Sdanmcd 	uint8_t last;
14663055Sdanmcd 	int limit = IPV6_ABITS;
14673055Sdanmcd 
14683055Sdanmcd 	if (is_v4mapped) {
14693055Sdanmcd 		mask += ((IPV6_ABITS - IP_ABITS)/8);
14703055Sdanmcd 		limit = IP_ABITS;
14713055Sdanmcd 	}
14723055Sdanmcd 
14733055Sdanmcd 	while (*mask == 0xff) {
14743055Sdanmcd 		rc += 8;
14753055Sdanmcd 		if (rc == limit)
14763055Sdanmcd 			return (limit);
14773055Sdanmcd 		mask++;
14783055Sdanmcd 	}
14793055Sdanmcd 
14803055Sdanmcd 	last = *mask;
14813055Sdanmcd 	while (last != 0) {
14823055Sdanmcd 		rc++;
14833055Sdanmcd 		last = (last << 1) & 0xff;
14843055Sdanmcd 	}
14853055Sdanmcd 
14863055Sdanmcd 	return (rc);
14873055Sdanmcd }
14883055Sdanmcd 
14893055Sdanmcd /*
14903055Sdanmcd  * Expand the diagnostic code into a message.
14913055Sdanmcd  */
14923055Sdanmcd void
14933055Sdanmcd print_diagnostic(FILE *file, uint16_t diagnostic)
14943055Sdanmcd {
14953055Sdanmcd 	/* Use two spaces so above strings can fit on the line. */
14964064Smarkfen 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
14974064Smarkfen 	    "  Diagnostic code %u:  %s.\n"),
14983055Sdanmcd 	    diagnostic, keysock_diag(diagnostic));
14993055Sdanmcd }
15003055Sdanmcd 
15013055Sdanmcd /*
15023055Sdanmcd  * Prints the base PF_KEY message.
15033055Sdanmcd  */
15043055Sdanmcd void
15054867Spwernau print_sadb_msg(FILE *file, struct sadb_msg *samsg, time_t wallclock,
15064867Spwernau     boolean_t vflag)
15073055Sdanmcd {
15083055Sdanmcd 	if (wallclock != 0)
15094867Spwernau 		printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
15104064Smarkfen 		    "%sTimestamp: %s\n"), "", NULL,
15113055Sdanmcd 		    vflag);
15123055Sdanmcd 
15134867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
15144867Spwernau 	    "Base message (version %u) type "),
15153055Sdanmcd 	    samsg->sadb_msg_version);
15163055Sdanmcd 	switch (samsg->sadb_msg_type) {
15173055Sdanmcd 	case SADB_RESERVED:
15184867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
15194064Smarkfen 		    "RESERVED (warning: set to 0)"));
15203055Sdanmcd 		break;
15213055Sdanmcd 	case SADB_GETSPI:
15224867Spwernau 		(void) fprintf(file, "GETSPI");
15233055Sdanmcd 		break;
15243055Sdanmcd 	case SADB_UPDATE:
15254867Spwernau 		(void) fprintf(file, "UPDATE");
15263055Sdanmcd 		break;
15276668Smarkfen 	case SADB_X_UPDATEPAIR:
15286668Smarkfen 		(void) fprintf(file, "UPDATE PAIR");
15296668Smarkfen 		break;
15303055Sdanmcd 	case SADB_ADD:
15314867Spwernau 		(void) fprintf(file, "ADD");
15323055Sdanmcd 		break;
15333055Sdanmcd 	case SADB_DELETE:
15344867Spwernau 		(void) fprintf(file, "DELETE");
15353055Sdanmcd 		break;
15366668Smarkfen 	case SADB_X_DELPAIR:
15376668Smarkfen 		(void) fprintf(file, "DELETE PAIR");
15386668Smarkfen 		break;
15393055Sdanmcd 	case SADB_GET:
15404867Spwernau 		(void) fprintf(file, "GET");
15413055Sdanmcd 		break;
15423055Sdanmcd 	case SADB_ACQUIRE:
15434867Spwernau 		(void) fprintf(file, "ACQUIRE");
15443055Sdanmcd 		break;
15453055Sdanmcd 	case SADB_REGISTER:
15464867Spwernau 		(void) fprintf(file, "REGISTER");
15473055Sdanmcd 		break;
15483055Sdanmcd 	case SADB_EXPIRE:
15494867Spwernau 		(void) fprintf(file, "EXPIRE");
15503055Sdanmcd 		break;
15513055Sdanmcd 	case SADB_FLUSH:
15524867Spwernau 		(void) fprintf(file, "FLUSH");
15533055Sdanmcd 		break;
15543055Sdanmcd 	case SADB_DUMP:
15554867Spwernau 		(void) fprintf(file, "DUMP");
15563055Sdanmcd 		break;
15573055Sdanmcd 	case SADB_X_PROMISC:
15584867Spwernau 		(void) fprintf(file, "X_PROMISC");
15593055Sdanmcd 		break;
15603055Sdanmcd 	case SADB_X_INVERSE_ACQUIRE:
15614867Spwernau 		(void) fprintf(file, "X_INVERSE_ACQUIRE");
15623055Sdanmcd 		break;
15633055Sdanmcd 	default:
15644867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
15654064Smarkfen 		    "Unknown (%u)"), samsg->sadb_msg_type);
15663055Sdanmcd 		break;
15673055Sdanmcd 	}
15684867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, ", SA type "));
15693055Sdanmcd 
15703055Sdanmcd 	switch (samsg->sadb_msg_satype) {
15713055Sdanmcd 	case SADB_SATYPE_UNSPEC:
15724867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
15734867Spwernau 		    "<unspecified/all>"));
15743055Sdanmcd 		break;
15753055Sdanmcd 	case SADB_SATYPE_AH:
15764867Spwernau 		(void) fprintf(file, "AH");
15773055Sdanmcd 		break;
15783055Sdanmcd 	case SADB_SATYPE_ESP:
15794867Spwernau 		(void) fprintf(file, "ESP");
15803055Sdanmcd 		break;
15813055Sdanmcd 	case SADB_SATYPE_RSVP:
15824867Spwernau 		(void) fprintf(file, "RSVP");
15833055Sdanmcd 		break;
15843055Sdanmcd 	case SADB_SATYPE_OSPFV2:
15854867Spwernau 		(void) fprintf(file, "OSPFv2");
15863055Sdanmcd 		break;
15873055Sdanmcd 	case SADB_SATYPE_RIPV2:
15884867Spwernau 		(void) fprintf(file, "RIPv2");
15893055Sdanmcd 		break;
15903055Sdanmcd 	case SADB_SATYPE_MIP:
15914867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Mobile IP"));
15923055Sdanmcd 		break;
15933055Sdanmcd 	default:
15944867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
15954064Smarkfen 		    "<unknown %u>"), samsg->sadb_msg_satype);
15963055Sdanmcd 		break;
15973055Sdanmcd 	}
15983055Sdanmcd 
15994867Spwernau 	(void) fprintf(file, ".\n");
16003055Sdanmcd 
16013055Sdanmcd 	if (samsg->sadb_msg_errno != 0) {
16024867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16034867Spwernau 		    "Error %s from PF_KEY.\n"),
16043055Sdanmcd 		    strerror(samsg->sadb_msg_errno));
16054867Spwernau 		print_diagnostic(file, samsg->sadb_x_msg_diagnostic);
16063055Sdanmcd 	}
16073055Sdanmcd 
16084867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
16094064Smarkfen 	    "Message length %u bytes, seq=%u, pid=%u.\n"),
16103055Sdanmcd 	    SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq,
16113055Sdanmcd 	    samsg->sadb_msg_pid);
16123055Sdanmcd }
16133055Sdanmcd 
16143055Sdanmcd /*
16153055Sdanmcd  * Print the SA extension for PF_KEY.
16163055Sdanmcd  */
16173055Sdanmcd void
16184867Spwernau print_sa(FILE *file, char *prefix, struct sadb_sa *assoc)
16193055Sdanmcd {
16203055Sdanmcd 	if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) {
16214867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
16224064Smarkfen 		    "WARNING: SA info extension length (%u) is bad."),
16233055Sdanmcd 		    SADB_64TO8(assoc->sadb_sa_len));
16243055Sdanmcd 	}
16253055Sdanmcd 
16264867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
16277749SThejaswini.Singarajipura@Sun.COM 	    "%sSADB_ASSOC spi=0x%x, replay window size=%u, state="),
16283055Sdanmcd 	    prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay);
16293055Sdanmcd 	switch (assoc->sadb_sa_state) {
16303055Sdanmcd 	case SADB_SASTATE_LARVAL:
16314867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "LARVAL"));
16323055Sdanmcd 		break;
16333055Sdanmcd 	case SADB_SASTATE_MATURE:
16344867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "MATURE"));
16353055Sdanmcd 		break;
16363055Sdanmcd 	case SADB_SASTATE_DYING:
16374867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "DYING"));
16383055Sdanmcd 		break;
16393055Sdanmcd 	case SADB_SASTATE_DEAD:
16404867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "DEAD"));
16413055Sdanmcd 		break;
16427749SThejaswini.Singarajipura@Sun.COM 	case SADB_X_SASTATE_ACTIVE_ELSEWHERE:
16437749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16447749SThejaswini.Singarajipura@Sun.COM 		    "ACTIVE_ELSEWHERE"));
16457749SThejaswini.Singarajipura@Sun.COM 		break;
16467749SThejaswini.Singarajipura@Sun.COM 	case SADB_X_SASTATE_IDLE:
16477749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "IDLE"));
16487749SThejaswini.Singarajipura@Sun.COM 		break;
16493055Sdanmcd 	default:
16504867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16514064Smarkfen 		    "<unknown %u>"), assoc->sadb_sa_state);
16523055Sdanmcd 	}
16533055Sdanmcd 
16543055Sdanmcd 	if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
16554867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16564064Smarkfen 		    "\n%sAuthentication algorithm = "),
16573055Sdanmcd 		    prefix);
16584867Spwernau 		(void) dump_aalg(assoc->sadb_sa_auth, file);
16593055Sdanmcd 	}
16603055Sdanmcd 
16613055Sdanmcd 	if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
16624867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16634064Smarkfen 		    "\n%sEncryption algorithm = "), prefix);
16644867Spwernau 		(void) dump_ealg(assoc->sadb_sa_encrypt, file);
16653055Sdanmcd 	}
16663055Sdanmcd 
16674867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix,
16683055Sdanmcd 	    assoc->sadb_sa_flags);
16693055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS)
16704867Spwernau 		(void) fprintf(file, "PFS ");
16713055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY)
16724867Spwernau 		(void) fprintf(file, "NOREPLAY ");
16733055Sdanmcd 
16743055Sdanmcd 	/* BEGIN Solaris-specific flags. */
16753055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED)
16764867Spwernau 		(void) fprintf(file, "X_USED ");
16776668Smarkfen 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_PAIRED)
16786668Smarkfen 		(void) fprintf(file, "X_PAIRED ");
16796668Smarkfen 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_OUTBOUND)
16806668Smarkfen 		(void) fprintf(file, "X_OUTBOUND ");
16816668Smarkfen 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_INBOUND)
16826668Smarkfen 		(void) fprintf(file, "X_INBOUND ");
16833055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE)
16844867Spwernau 		(void) fprintf(file, "X_UNIQUE ");
16853055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1)
16864867Spwernau 		(void) fprintf(file, "X_AALG1 ");
16873055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2)
16884867Spwernau 		(void) fprintf(file, "X_AALG2 ");
16893055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1)
16904867Spwernau 		(void) fprintf(file, "X_EALG1 ");
16913055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2)
16924867Spwernau 		(void) fprintf(file, "X_EALG2 ");
16933055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC)
16944867Spwernau 		(void) fprintf(file, "X_NATT_LOC ");
16953055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM)
16964867Spwernau 		(void) fprintf(file, "X_NATT_REM ");
16973055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL)
16984867Spwernau 		(void) fprintf(file, "X_TUNNEL ");
16997066Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATTED)
17007066Sdanmcd 		(void) fprintf(file, "X_NATTED ");
17013055Sdanmcd 	/* END Solaris-specific flags. */
17023055Sdanmcd 
17034867Spwernau 	(void) fprintf(file, ">\n");
17043055Sdanmcd }
17053055Sdanmcd 
17063055Sdanmcd void
17074867Spwernau printsatime(FILE *file, int64_t lt, const char *msg, const char *pfx,
17084867Spwernau     const char *pfx2, boolean_t vflag)
17093055Sdanmcd {
17103055Sdanmcd 	char tbuf[TBUF_SIZE]; /* For strftime() call. */
17113055Sdanmcd 	const char *tp = tbuf;
17123055Sdanmcd 	time_t t = lt;
17133055Sdanmcd 	struct tm res;
17143055Sdanmcd 
17153055Sdanmcd 	if (t != lt) {
17163055Sdanmcd 		if (lt > 0)
17173055Sdanmcd 			t = LONG_MAX;
17183055Sdanmcd 		else
17193055Sdanmcd 			t = LONG_MIN;
17203055Sdanmcd 	}
17213055Sdanmcd 
17223055Sdanmcd 	if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0)
17234064Smarkfen 		tp = dgettext(TEXT_DOMAIN, "<time conversion failed>");
17244867Spwernau 	(void) fprintf(file, msg, pfx, tp);
17253055Sdanmcd 	if (vflag && (pfx2 != NULL))
17264867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17277320Sdanmcd@sun.com 		    "%s\t(raw time value %" PRIu64 ")\n"), pfx2, lt);
17283055Sdanmcd }
17293055Sdanmcd 
17303055Sdanmcd /*
17313055Sdanmcd  * Print the SA lifetime information.  (An SADB_EXT_LIFETIME_* extension.)
17323055Sdanmcd  */
17333055Sdanmcd void
17344867Spwernau print_lifetimes(FILE *file, time_t wallclock, struct sadb_lifetime *current,
17357749SThejaswini.Singarajipura@Sun.COM     struct sadb_lifetime *hard, struct sadb_lifetime *soft,
17367749SThejaswini.Singarajipura@Sun.COM     struct sadb_lifetime *idle, boolean_t vflag)
17373055Sdanmcd {
17383055Sdanmcd 	int64_t scratch;
17394064Smarkfen 	char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: ");
17404064Smarkfen 	char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: ");
17414064Smarkfen 	char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: ");
17427749SThejaswini.Singarajipura@Sun.COM 	char *idle_prefix = dgettext(TEXT_DOMAIN, "ILT: ");
17433055Sdanmcd 
17443055Sdanmcd 	if (current != NULL &&
17453055Sdanmcd 	    current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) {
17464867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
17474064Smarkfen 		    "WARNING: CURRENT lifetime extension length (%u) is bad."),
17483055Sdanmcd 		    SADB_64TO8(current->sadb_lifetime_len));
17493055Sdanmcd 	}
17503055Sdanmcd 
17513055Sdanmcd 	if (hard != NULL &&
17523055Sdanmcd 	    hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) {
17534867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
17544867Spwernau 		    "WARNING: HARD lifetime extension length (%u) is bad."),
17553055Sdanmcd 		    SADB_64TO8(hard->sadb_lifetime_len));
17563055Sdanmcd 	}
17573055Sdanmcd 
17583055Sdanmcd 	if (soft != NULL &&
17593055Sdanmcd 	    soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) {
17604867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
17614867Spwernau 		    "WARNING: SOFT lifetime extension length (%u) is bad."),
17623055Sdanmcd 		    SADB_64TO8(soft->sadb_lifetime_len));
17633055Sdanmcd 	}
17643055Sdanmcd 
17657749SThejaswini.Singarajipura@Sun.COM 	if (idle != NULL &&
17667749SThejaswini.Singarajipura@Sun.COM 	    idle->sadb_lifetime_len != SADB_8TO64(sizeof (*idle))) {
17677749SThejaswini.Singarajipura@Sun.COM 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
17687749SThejaswini.Singarajipura@Sun.COM 		    "WARNING: IDLE lifetime extension length (%u) is bad."),
17697749SThejaswini.Singarajipura@Sun.COM 		    SADB_64TO8(idle->sadb_lifetime_len));
17707749SThejaswini.Singarajipura@Sun.COM 	}
17717749SThejaswini.Singarajipura@Sun.COM 
17724867Spwernau 	(void) fprintf(file, " LT: Lifetime information\n");
17733055Sdanmcd 
17743055Sdanmcd 	if (current != NULL) {
17753055Sdanmcd 		/* Express values as current values. */
17764867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17777320Sdanmcd@sun.com 		    "%s%" PRIu64 " bytes protected, %u allocations used.\n"),
17783055Sdanmcd 		    current_prefix, current->sadb_lifetime_bytes,
17793055Sdanmcd 		    current->sadb_lifetime_allocations);
17804867Spwernau 		printsatime(file, current->sadb_lifetime_addtime,
17814064Smarkfen 		    dgettext(TEXT_DOMAIN, "%sSA added at time %s\n"),
17823055Sdanmcd 		    current_prefix, current_prefix, vflag);
17833055Sdanmcd 		if (current->sadb_lifetime_usetime != 0) {
17844867Spwernau 			printsatime(file, current->sadb_lifetime_usetime,
17854064Smarkfen 			    dgettext(TEXT_DOMAIN,
17864064Smarkfen 			    "%sSA first used at time %s\n"),
17873055Sdanmcd 			    current_prefix, current_prefix, vflag);
17883055Sdanmcd 		}
17894867Spwernau 		printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
17904064Smarkfen 		    "%sTime now is %s\n"), current_prefix, current_prefix,
17914064Smarkfen 		    vflag);
17923055Sdanmcd 	}
17933055Sdanmcd 
17943055Sdanmcd 	if (soft != NULL) {
17954867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17964064Smarkfen 		    "%sSoft lifetime information:  "),
17973055Sdanmcd 		    soft_prefix);
17984867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17997320Sdanmcd@sun.com 		    "%" PRIu64 " bytes of lifetime, %u "
18003055Sdanmcd 		    "allocations.\n"), soft->sadb_lifetime_bytes,
18013055Sdanmcd 		    soft->sadb_lifetime_allocations);
18024867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18037320Sdanmcd@sun.com 		    "%s%" PRIu64 " seconds of post-add lifetime.\n"),
18043055Sdanmcd 		    soft_prefix, soft->sadb_lifetime_addtime);
18054867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18067320Sdanmcd@sun.com 		    "%s%" PRIu64 " seconds of post-use lifetime.\n"),
18073055Sdanmcd 		    soft_prefix, soft->sadb_lifetime_usetime);
18083055Sdanmcd 		/* If possible, express values as time remaining. */
18093055Sdanmcd 		if (current != NULL) {
18103055Sdanmcd 			if (soft->sadb_lifetime_bytes != 0)
18117320Sdanmcd@sun.com 				(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s%"
18127320Sdanmcd@sun.com 				    PRIu64 " more bytes can be protected.\n"),
18133055Sdanmcd 				    soft_prefix,
18143055Sdanmcd 				    (soft->sadb_lifetime_bytes >
18154342Spwernau 				    current->sadb_lifetime_bytes) ?
18163055Sdanmcd 				    (soft->sadb_lifetime_bytes -
18174342Spwernau 				    current->sadb_lifetime_bytes) : (0));
18183055Sdanmcd 			if (soft->sadb_lifetime_addtime != 0 ||
18193055Sdanmcd 			    (soft->sadb_lifetime_usetime != 0 &&
18204342Spwernau 			    current->sadb_lifetime_usetime != 0)) {
18213055Sdanmcd 				int64_t adddelta, usedelta;
18223055Sdanmcd 
18233055Sdanmcd 				if (soft->sadb_lifetime_addtime != 0) {
18243055Sdanmcd 					adddelta =
18253055Sdanmcd 					    current->sadb_lifetime_addtime +
18263055Sdanmcd 					    soft->sadb_lifetime_addtime -
18273055Sdanmcd 					    wallclock;
18283055Sdanmcd 				} else {
18293055Sdanmcd 					adddelta = TIME_MAX;
18303055Sdanmcd 				}
18313055Sdanmcd 
18323055Sdanmcd 				if (soft->sadb_lifetime_usetime != 0 &&
18333055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
18343055Sdanmcd 					usedelta =
18353055Sdanmcd 					    current->sadb_lifetime_usetime +
18363055Sdanmcd 					    soft->sadb_lifetime_usetime -
18373055Sdanmcd 					    wallclock;
18383055Sdanmcd 				} else {
18393055Sdanmcd 					usedelta = TIME_MAX;
18403055Sdanmcd 				}
18414867Spwernau 				(void) fprintf(file, "%s", soft_prefix);
18423055Sdanmcd 				scratch = MIN(adddelta, usedelta);
18433055Sdanmcd 				if (scratch >= 0) {
18444867Spwernau 					(void) fprintf(file,
18454867Spwernau 					    dgettext(TEXT_DOMAIN,
18467320Sdanmcd@sun.com 					    "Soft expiration occurs in %"
18477320Sdanmcd@sun.com 					    PRId64 " seconds, "), scratch);
18483055Sdanmcd 				} else {
18494867Spwernau 					(void) fprintf(file,
18504867Spwernau 					    dgettext(TEXT_DOMAIN,
18513055Sdanmcd 					    "Soft expiration occurred "));
18523055Sdanmcd 				}
18533055Sdanmcd 				scratch += wallclock;
18544867Spwernau 				printsatime(file, scratch, dgettext(TEXT_DOMAIN,
18554064Smarkfen 				    "%sat %s.\n"), "", soft_prefix, vflag);
18563055Sdanmcd 			}
18573055Sdanmcd 		}
18583055Sdanmcd 	}
18593055Sdanmcd 
18603055Sdanmcd 	if (hard != NULL) {
18614867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18624064Smarkfen 		    "%sHard lifetime information:  "), hard_prefix);
18634867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18647320Sdanmcd@sun.com 		    "%" PRIu64 " bytes of lifetime, %u allocations.\n"),
18654867Spwernau 		    hard->sadb_lifetime_bytes, hard->sadb_lifetime_allocations);
18664867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18677320Sdanmcd@sun.com 		    "%s%" PRIu64 " seconds of post-add lifetime.\n"),
18683055Sdanmcd 		    hard_prefix, hard->sadb_lifetime_addtime);
18694867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18707320Sdanmcd@sun.com 		    "%s%" PRIu64 " seconds of post-use lifetime.\n"),
18713055Sdanmcd 		    hard_prefix, hard->sadb_lifetime_usetime);
18723055Sdanmcd 		/* If possible, express values as time remaining. */
18733055Sdanmcd 		if (current != NULL) {
18743055Sdanmcd 			if (hard->sadb_lifetime_bytes != 0)
18757320Sdanmcd@sun.com 				(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s%"
18767320Sdanmcd@sun.com 				    PRIu64 " more bytes can be protected.\n"),
18773055Sdanmcd 				    hard_prefix,
18783055Sdanmcd 				    (hard->sadb_lifetime_bytes >
18794342Spwernau 				    current->sadb_lifetime_bytes) ?
18803055Sdanmcd 				    (hard->sadb_lifetime_bytes -
18814342Spwernau 				    current->sadb_lifetime_bytes) : (0));
18823055Sdanmcd 			if (hard->sadb_lifetime_addtime != 0 ||
18833055Sdanmcd 			    (hard->sadb_lifetime_usetime != 0 &&
18844342Spwernau 			    current->sadb_lifetime_usetime != 0)) {
18853055Sdanmcd 				int64_t adddelta, usedelta;
18863055Sdanmcd 
18873055Sdanmcd 				if (hard->sadb_lifetime_addtime != 0) {
18883055Sdanmcd 					adddelta =
18893055Sdanmcd 					    current->sadb_lifetime_addtime +
18903055Sdanmcd 					    hard->sadb_lifetime_addtime -
18913055Sdanmcd 					    wallclock;
18923055Sdanmcd 				} else {
18933055Sdanmcd 					adddelta = TIME_MAX;
18943055Sdanmcd 				}
18953055Sdanmcd 
18963055Sdanmcd 				if (hard->sadb_lifetime_usetime != 0 &&
18973055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
18983055Sdanmcd 					usedelta =
18993055Sdanmcd 					    current->sadb_lifetime_usetime +
19003055Sdanmcd 					    hard->sadb_lifetime_usetime -
19013055Sdanmcd 					    wallclock;
19023055Sdanmcd 				} else {
19033055Sdanmcd 					usedelta = TIME_MAX;
19043055Sdanmcd 				}
19054867Spwernau 				(void) fprintf(file, "%s", hard_prefix);
19063055Sdanmcd 				scratch = MIN(adddelta, usedelta);
19073055Sdanmcd 				if (scratch >= 0) {
19084867Spwernau 					(void) fprintf(file,
19094867Spwernau 					    dgettext(TEXT_DOMAIN,
19107320Sdanmcd@sun.com 					    "Hard expiration occurs in %"
19117320Sdanmcd@sun.com 					    PRId64 " seconds, "), scratch);
19123055Sdanmcd 				} else {
19134867Spwernau 					(void) fprintf(file,
19144867Spwernau 					    dgettext(TEXT_DOMAIN,
19153055Sdanmcd 					    "Hard expiration occured "));
19163055Sdanmcd 				}
19173055Sdanmcd 				scratch += wallclock;
19184867Spwernau 				printsatime(file, scratch, dgettext(TEXT_DOMAIN,
19194064Smarkfen 				    "%sat %s.\n"), "", hard_prefix, vflag);
19203055Sdanmcd 			}
19213055Sdanmcd 		}
19223055Sdanmcd 	}
19237749SThejaswini.Singarajipura@Sun.COM 	if (idle != NULL) {
19247749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
19257749SThejaswini.Singarajipura@Sun.COM 		    "%sIdle lifetime information:  "), idle_prefix);
19267749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
19277749SThejaswini.Singarajipura@Sun.COM 		    "%s%llu seconds of post-add lifetime.\n"),
19287749SThejaswini.Singarajipura@Sun.COM 		    idle_prefix, idle->sadb_lifetime_addtime);
19297749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
19307749SThejaswini.Singarajipura@Sun.COM 		    "%s%llu seconds of post-use lifetime.\n"),
19317749SThejaswini.Singarajipura@Sun.COM 		    idle_prefix, idle->sadb_lifetime_usetime);
19327749SThejaswini.Singarajipura@Sun.COM 	}
19333055Sdanmcd }
19343055Sdanmcd 
19353055Sdanmcd /*
19363055Sdanmcd  * Print an SADB_EXT_ADDRESS_* extension.
19373055Sdanmcd  */
19383055Sdanmcd void
19394867Spwernau print_address(FILE *file, char *prefix, struct sadb_address *addr,
19404867Spwernau     boolean_t ignore_nss)
19413055Sdanmcd {
19423055Sdanmcd 	struct protoent *pe;
19433055Sdanmcd 
19444867Spwernau 	(void) fprintf(file, "%s", prefix);
19453055Sdanmcd 	switch (addr->sadb_address_exttype) {
19463055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
19474867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Source address "));
19483055Sdanmcd 		break;
19493055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
19504867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
19514867Spwernau 		    "Inner source address "));
19523055Sdanmcd 		break;
19533055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
19544867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
19554867Spwernau 		    "Destination address "));
19563055Sdanmcd 		break;
19573055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
19584867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
19594064Smarkfen 		    "Inner destination address "));
19603055Sdanmcd 		break;
19613055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
19624867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
19634867Spwernau 		    "NAT-T local address "));
19643055Sdanmcd 		break;
19653055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
19664867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
19674867Spwernau 		    "NAT-T remote address "));
19683055Sdanmcd 		break;
19693055Sdanmcd 	}
19703055Sdanmcd 
19714867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
19724064Smarkfen 	    "(proto=%d"), addr->sadb_address_proto);
19734867Spwernau 	if (ignore_nss == B_FALSE) {
19743055Sdanmcd 		if (addr->sadb_address_proto == 0) {
19754867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19764867Spwernau 			    "/<unspecified>"));
19773055Sdanmcd 		} else if ((pe = getprotobynumber(addr->sadb_address_proto))
19783055Sdanmcd 		    != NULL) {
19794867Spwernau 			(void) fprintf(file, "/%s", pe->p_name);
19803055Sdanmcd 		} else {
19814867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19824867Spwernau 			    "/<unknown>"));
19833055Sdanmcd 		}
19843055Sdanmcd 	}
19854867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, ")\n%s"), prefix);
19863055Sdanmcd 	(void) dump_sockaddr((struct sockaddr *)(addr + 1),
19874867Spwernau 	    addr->sadb_address_prefixlen, B_FALSE, file, ignore_nss);
19883055Sdanmcd }
19893055Sdanmcd 
19903055Sdanmcd /*
19913055Sdanmcd  * Print an SADB_EXT_KEY extension.
19923055Sdanmcd  */
19933055Sdanmcd void
19944867Spwernau print_key(FILE *file, char *prefix, struct sadb_key *key)
19953055Sdanmcd {
19964867Spwernau 	(void) fprintf(file, "%s", prefix);
19973055Sdanmcd 
19983055Sdanmcd 	switch (key->sadb_key_exttype) {
19993055Sdanmcd 	case SADB_EXT_KEY_AUTH:
20004867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Authentication"));
20013055Sdanmcd 		break;
20023055Sdanmcd 	case SADB_EXT_KEY_ENCRYPT:
20034867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Encryption"));
20043055Sdanmcd 		break;
20053055Sdanmcd 	}
20063055Sdanmcd 
20074867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, " key.\n%s"), prefix);
200810824SMark.Fenwick@Sun.COM 	(void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits,
200910824SMark.Fenwick@Sun.COM 	    key->sadb_key_reserved, file, B_TRUE);
20104867Spwernau 	(void) fprintf(file, "\n");
20113055Sdanmcd }
20123055Sdanmcd 
20133055Sdanmcd /*
20143055Sdanmcd  * Print an SADB_EXT_IDENTITY_* extension.
20153055Sdanmcd  */
20163055Sdanmcd void
20174867Spwernau print_ident(FILE *file, char *prefix, struct sadb_ident *id)
20183055Sdanmcd {
20193055Sdanmcd 	boolean_t canprint = B_TRUE;
20203055Sdanmcd 
20214867Spwernau 	(void) fprintf(file, "%s", prefix);
20223055Sdanmcd 	switch (id->sadb_ident_exttype) {
20233055Sdanmcd 	case SADB_EXT_IDENTITY_SRC:
20244867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Source"));
20253055Sdanmcd 		break;
20263055Sdanmcd 	case SADB_EXT_IDENTITY_DST:
20274867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Destination"));
20283055Sdanmcd 		break;
20293055Sdanmcd 	}
20303055Sdanmcd 
20314867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
20324064Smarkfen 	    " identity, uid=%d, type "), id->sadb_ident_id);
20334867Spwernau 	canprint = dump_sadb_idtype(id->sadb_ident_type, file, NULL);
20344867Spwernau 	(void) fprintf(file, "\n%s", prefix);
20356119Spwernau 	if (canprint) {
20364867Spwernau 		(void) fprintf(file, "%s\n", (char *)(id + 1));
20376119Spwernau 	} else {
20386119Spwernau 		print_asn1_name(file, (const unsigned char *)(id + 1),
20396119Spwernau 		    SADB_64TO8(id->sadb_ident_len) - sizeof (sadb_ident_t));
20406119Spwernau 	}
20413055Sdanmcd }
20423055Sdanmcd 
20433055Sdanmcd /*
2044*10934Ssommerfeld@sun.com  * Convert sadb_sens extension into binary security label.
2045*10934Ssommerfeld@sun.com  */
2046*10934Ssommerfeld@sun.com 
2047*10934Ssommerfeld@sun.com #include <tsol/label.h>
2048*10934Ssommerfeld@sun.com #include <sys/tsol/tndb.h>
2049*10934Ssommerfeld@sun.com #include <sys/tsol/label_macro.h>
2050*10934Ssommerfeld@sun.com 
2051*10934Ssommerfeld@sun.com void
2052*10934Ssommerfeld@sun.com ipsec_convert_sens_to_bslabel(const struct sadb_sens *sens, bslabel_t *sl)
2053*10934Ssommerfeld@sun.com {
2054*10934Ssommerfeld@sun.com 	uint64_t *bitmap = (uint64_t *)(sens + 1);
2055*10934Ssommerfeld@sun.com 	int bitmap_len = SADB_64TO8(sens->sadb_sens_sens_len);
2056*10934Ssommerfeld@sun.com 
2057*10934Ssommerfeld@sun.com 	bsllow(sl);
2058*10934Ssommerfeld@sun.com 	LCLASS_SET((_bslabel_impl_t *)sl, sens->sadb_sens_sens_level);
2059*10934Ssommerfeld@sun.com 	bcopy(bitmap, &((_bslabel_impl_t *)sl)->compartments,
2060*10934Ssommerfeld@sun.com 	    bitmap_len);
2061*10934Ssommerfeld@sun.com }
2062*10934Ssommerfeld@sun.com 
2063*10934Ssommerfeld@sun.com void
2064*10934Ssommerfeld@sun.com ipsec_convert_bslabel_to_string(bslabel_t *sl, char **plabel)
2065*10934Ssommerfeld@sun.com {
2066*10934Ssommerfeld@sun.com 	if (label_to_str(sl, plabel, M_LABEL, DEF_NAMES) != 0) {
2067*10934Ssommerfeld@sun.com 		*plabel = strdup(dgettext(TEXT_DOMAIN,
2068*10934Ssommerfeld@sun.com 		    "** Label conversion failed **"));
2069*10934Ssommerfeld@sun.com 	}
2070*10934Ssommerfeld@sun.com }
2071*10934Ssommerfeld@sun.com 
2072*10934Ssommerfeld@sun.com void
2073*10934Ssommerfeld@sun.com ipsec_convert_bslabel_to_hex(bslabel_t *sl, char **plabel)
2074*10934Ssommerfeld@sun.com {
2075*10934Ssommerfeld@sun.com 	if (label_to_str(sl, plabel, M_INTERNAL, DEF_NAMES) != 0) {
2076*10934Ssommerfeld@sun.com 		*plabel = strdup(dgettext(TEXT_DOMAIN,
2077*10934Ssommerfeld@sun.com 		    "** Label conversion failed **"));
2078*10934Ssommerfeld@sun.com 	}
2079*10934Ssommerfeld@sun.com }
2080*10934Ssommerfeld@sun.com 
2081*10934Ssommerfeld@sun.com int
2082*10934Ssommerfeld@sun.com ipsec_convert_sl_to_sens(int doi, bslabel_t *sl, sadb_sens_t *sens)
2083*10934Ssommerfeld@sun.com {
2084*10934Ssommerfeld@sun.com 	uint8_t *bitmap;
2085*10934Ssommerfeld@sun.com 	int sens_len = sizeof (sadb_sens_t) + _C_LEN * 4;
2086*10934Ssommerfeld@sun.com 
2087*10934Ssommerfeld@sun.com 
2088*10934Ssommerfeld@sun.com 	if (sens == NULL)
2089*10934Ssommerfeld@sun.com 		return (sens_len);
2090*10934Ssommerfeld@sun.com 
2091*10934Ssommerfeld@sun.com 
2092*10934Ssommerfeld@sun.com 	(void) memset(sens, 0, sens_len);
2093*10934Ssommerfeld@sun.com 
2094*10934Ssommerfeld@sun.com 	sens->sadb_sens_exttype = SADB_EXT_SENSITIVITY;
2095*10934Ssommerfeld@sun.com 	sens->sadb_sens_len = SADB_8TO64(sens_len);
2096*10934Ssommerfeld@sun.com 	sens->sadb_sens_dpd = doi;
2097*10934Ssommerfeld@sun.com 
2098*10934Ssommerfeld@sun.com 	sens->sadb_sens_sens_level = LCLASS(sl);
2099*10934Ssommerfeld@sun.com 	sens->sadb_sens_integ_level = 0;
2100*10934Ssommerfeld@sun.com 	sens->sadb_sens_sens_len = _C_LEN >> 1;
2101*10934Ssommerfeld@sun.com 	sens->sadb_sens_integ_len = 0;
2102*10934Ssommerfeld@sun.com 
2103*10934Ssommerfeld@sun.com 	sens->sadb_x_sens_flags = 0;
2104*10934Ssommerfeld@sun.com 
2105*10934Ssommerfeld@sun.com 	bitmap = (uint8_t *)(sens + 1);
2106*10934Ssommerfeld@sun.com 	bcopy(&(((_bslabel_impl_t *)sl)->compartments), bitmap, _C_LEN * 4);
2107*10934Ssommerfeld@sun.com 
2108*10934Ssommerfeld@sun.com 	return (sens_len);
2109*10934Ssommerfeld@sun.com }
2110*10934Ssommerfeld@sun.com 
2111*10934Ssommerfeld@sun.com 
2112*10934Ssommerfeld@sun.com /*
21133055Sdanmcd  * Print an SADB_SENSITIVITY extension.
21143055Sdanmcd  */
21153055Sdanmcd void
2116*10934Ssommerfeld@sun.com print_sens(FILE *file, char *prefix, const struct sadb_sens *sens,
2117*10934Ssommerfeld@sun.com 	boolean_t ignore_nss)
21183055Sdanmcd {
2119*10934Ssommerfeld@sun.com 	char *plabel;
2120*10934Ssommerfeld@sun.com 	char *hlabel;
21213055Sdanmcd 	uint64_t *bitmap = (uint64_t *)(sens + 1);
2122*10934Ssommerfeld@sun.com 	bslabel_t sl;
21233055Sdanmcd 	int i;
2124*10934Ssommerfeld@sun.com 	int sens_len = sens->sadb_sens_sens_len;
2125*10934Ssommerfeld@sun.com 	int integ_len = sens->sadb_sens_integ_len;
2126*10934Ssommerfeld@sun.com 	boolean_t inner = (sens->sadb_sens_exttype == SADB_EXT_SENSITIVITY);
2127*10934Ssommerfeld@sun.com 	const char *sensname = inner ?
2128*10934Ssommerfeld@sun.com 	    dgettext(TEXT_DOMAIN, "Plaintext Sensitivity") :
2129*10934Ssommerfeld@sun.com 	    dgettext(TEXT_DOMAIN, "Ciphertext Sensitivity");
2130*10934Ssommerfeld@sun.com 
2131*10934Ssommerfeld@sun.com 	ipsec_convert_sens_to_bslabel(sens, &sl);
21323055Sdanmcd 
21334867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
2134*10934Ssommerfeld@sun.com 	    "%s%s DPD %d, sens level=%d, integ level=%d, flags=%x\n"),
2135*10934Ssommerfeld@sun.com 	    prefix, sensname, sens->sadb_sens_dpd, sens->sadb_sens_sens_level,
2136*10934Ssommerfeld@sun.com 	    sens->sadb_sens_integ_level, sens->sadb_x_sens_flags);
2137*10934Ssommerfeld@sun.com 
2138*10934Ssommerfeld@sun.com 	ipsec_convert_bslabel_to_hex(&sl, &hlabel);
2139*10934Ssommerfeld@sun.com 
2140*10934Ssommerfeld@sun.com 	if (ignore_nss) {
21414867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2142*10934Ssommerfeld@sun.com 		    "%s %s Label: %s\n"), prefix, sensname, hlabel);
2143*10934Ssommerfeld@sun.com 
2144*10934Ssommerfeld@sun.com 		for (i = 0; i < sens_len; i++, bitmap++)
2145*10934Ssommerfeld@sun.com 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
2146*10934Ssommerfeld@sun.com 			    "%s %s BM extended word %d 0x%" PRIx64 "\n"),
2147*10934Ssommerfeld@sun.com 			    prefix, sensname, i, *bitmap);
2148*10934Ssommerfeld@sun.com 
2149*10934Ssommerfeld@sun.com 	} else {
2150*10934Ssommerfeld@sun.com 		ipsec_convert_bslabel_to_string(&sl, &plabel);
2151*10934Ssommerfeld@sun.com 
2152*10934Ssommerfeld@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2153*10934Ssommerfeld@sun.com 		    "%s %s Label: %s (%s)\n"),
2154*10934Ssommerfeld@sun.com 		    prefix, sensname, plabel, hlabel);
2155*10934Ssommerfeld@sun.com 		free(plabel);
2156*10934Ssommerfeld@sun.com 
2157*10934Ssommerfeld@sun.com 	}
2158*10934Ssommerfeld@sun.com 	free(hlabel);
2159*10934Ssommerfeld@sun.com 
2160*10934Ssommerfeld@sun.com 	bitmap = (uint64_t *)(sens + 1 + sens_len);
2161*10934Ssommerfeld@sun.com 
2162*10934Ssommerfeld@sun.com 	for (i = 0; i < integ_len; i++, bitmap++)
2163*10934Ssommerfeld@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
21647320Sdanmcd@sun.com 		    "%s Integrity BM extended word %d 0x%" PRIx64 "\n"),
21654867Spwernau 		    prefix, i, *bitmap);
21663055Sdanmcd }
21673055Sdanmcd 
21683055Sdanmcd /*
21693055Sdanmcd  * Print an SADB_EXT_PROPOSAL extension.
21703055Sdanmcd  */
21713055Sdanmcd void
21724867Spwernau print_prop(FILE *file, char *prefix, struct sadb_prop *prop)
21733055Sdanmcd {
21743055Sdanmcd 	struct sadb_comb *combs;
21753055Sdanmcd 	int i, numcombs;
21763055Sdanmcd 
21774867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
21784064Smarkfen 	    "%sProposal, replay counter = %u.\n"), prefix,
21793055Sdanmcd 	    prop->sadb_prop_replay);
21803055Sdanmcd 
21813055Sdanmcd 	numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop));
21823055Sdanmcd 	numcombs /= SADB_8TO64(sizeof (*combs));
21833055Sdanmcd 
21843055Sdanmcd 	combs = (struct sadb_comb *)(prop + 1);
21853055Sdanmcd 
21863055Sdanmcd 	for (i = 0; i < numcombs; i++) {
21874867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
21884064Smarkfen 		    "%s Combination #%u "), prefix, i + 1);
21893055Sdanmcd 		if (combs[i].sadb_comb_auth != SADB_AALG_NONE) {
21904867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
21914064Smarkfen 			    "Authentication = "));
21924867Spwernau 			(void) dump_aalg(combs[i].sadb_comb_auth, file);
21934867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
21944064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
21953055Sdanmcd 			    combs[i].sadb_comb_auth_minbits,
21963055Sdanmcd 			    combs[i].sadb_comb_auth_maxbits, prefix);
21973055Sdanmcd 		}
21983055Sdanmcd 
21993055Sdanmcd 		if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) {
22004867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
22014867Spwernau 			    "Encryption = "));
22024867Spwernau 			(void) dump_ealg(combs[i].sadb_comb_encrypt, file);
22034867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
22044064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
22053055Sdanmcd 			    combs[i].sadb_comb_encrypt_minbits,
22063055Sdanmcd 			    combs[i].sadb_comb_encrypt_maxbits, prefix);
22073055Sdanmcd 		}
22083055Sdanmcd 
22094867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "HARD: "));
22103055Sdanmcd 		if (combs[i].sadb_comb_hard_allocations)
22114867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
22123055Sdanmcd 			    combs[i].sadb_comb_hard_allocations);
22133055Sdanmcd 		if (combs[i].sadb_comb_hard_bytes)
22147320Sdanmcd@sun.com 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%"
22157320Sdanmcd@sun.com 			    PRIu64 " "), combs[i].sadb_comb_hard_bytes);
22163055Sdanmcd 		if (combs[i].sadb_comb_hard_addtime)
22174867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
22187320Sdanmcd@sun.com 			    "post-add secs=%" PRIu64 " "),
22193055Sdanmcd 			    combs[i].sadb_comb_hard_addtime);
22203055Sdanmcd 		if (combs[i].sadb_comb_hard_usetime)
22214867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
22227320Sdanmcd@sun.com 			    "post-use secs=%" PRIu64 ""),
22233055Sdanmcd 			    combs[i].sadb_comb_hard_usetime);
22243055Sdanmcd 
22254867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%s SOFT: "),
22264867Spwernau 		    prefix);
22273055Sdanmcd 		if (combs[i].sadb_comb_soft_allocations)
22284867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
22293055Sdanmcd 			    combs[i].sadb_comb_soft_allocations);
22303055Sdanmcd 		if (combs[i].sadb_comb_soft_bytes)
22317320Sdanmcd@sun.com 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%"
22327320Sdanmcd@sun.com 			    PRIu64 " "), combs[i].sadb_comb_soft_bytes);
22333055Sdanmcd 		if (combs[i].sadb_comb_soft_addtime)
22344867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
22357320Sdanmcd@sun.com 			    "post-add secs=%" PRIu64 " "),
22363055Sdanmcd 			    combs[i].sadb_comb_soft_addtime);
22373055Sdanmcd 		if (combs[i].sadb_comb_soft_usetime)
22384867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
22397320Sdanmcd@sun.com 			    "post-use secs=%" PRIu64 ""),
22403055Sdanmcd 			    combs[i].sadb_comb_soft_usetime);
22414867Spwernau 		(void) fprintf(file, "\n");
22423055Sdanmcd 	}
22433055Sdanmcd }
22443055Sdanmcd 
22453055Sdanmcd /*
22463055Sdanmcd  * Print an extended proposal (SADB_X_EXT_EPROP).
22473055Sdanmcd  */
22483055Sdanmcd void
22494867Spwernau print_eprop(FILE *file, char *prefix, struct sadb_prop *eprop)
22503055Sdanmcd {
22513055Sdanmcd 	uint64_t *sofar;
22523055Sdanmcd 	struct sadb_x_ecomb *ecomb;
22533055Sdanmcd 	struct sadb_x_algdesc *algdesc;
22543055Sdanmcd 	int i, j;
22553055Sdanmcd 
22564867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
22574064Smarkfen 	    "%sExtended Proposal, replay counter = %u, "), prefix,
22584064Smarkfen 	    eprop->sadb_prop_replay);
22594867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
22604867Spwernau 	    "number of combinations = %u.\n"), eprop->sadb_x_prop_numecombs);
22613055Sdanmcd 
22623055Sdanmcd 	sofar = (uint64_t *)(eprop + 1);
22633055Sdanmcd 	ecomb = (struct sadb_x_ecomb *)sofar;
22643055Sdanmcd 
22653055Sdanmcd 	for (i = 0; i < eprop->sadb_x_prop_numecombs; ) {
22664867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
22674064Smarkfen 		    "%s Extended combination #%u:\n"), prefix, ++i);
22683055Sdanmcd 
22694867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s HARD: "),
22704867Spwernau 		    prefix);
22714867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
22723055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_allocations);
22737320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%" PRIu64
22747320Sdanmcd@sun.com 		    ", "), ecomb->sadb_x_ecomb_hard_bytes);
22757320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-add secs=%"
22767320Sdanmcd@sun.com 		    PRIu64 ", "), ecomb->sadb_x_ecomb_hard_addtime);
22777320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-use secs=%"
22787320Sdanmcd@sun.com 		    PRIu64 "\n"), ecomb->sadb_x_ecomb_hard_usetime);
22793055Sdanmcd 
22804867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s SOFT: "),
22814867Spwernau 		    prefix);
22824867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
22833055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_allocations);
22847320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
22857320Sdanmcd@sun.com 		    "bytes=%" PRIu64 ", "), ecomb->sadb_x_ecomb_soft_bytes);
22864867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
22877320Sdanmcd@sun.com 		    "post-add secs=%" PRIu64 ", "),
22883055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_addtime);
22897320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-use secs=%"
22907320Sdanmcd@sun.com 		    PRIu64 "\n"), ecomb->sadb_x_ecomb_soft_usetime);
22913055Sdanmcd 
22923055Sdanmcd 		sofar = (uint64_t *)(ecomb + 1);
22933055Sdanmcd 		algdesc = (struct sadb_x_algdesc *)sofar;
22943055Sdanmcd 
22953055Sdanmcd 		for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) {
22964867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
22974064Smarkfen 			    "%s Alg #%u "), prefix, ++j);
22983055Sdanmcd 			switch (algdesc->sadb_x_algdesc_satype) {
22993055Sdanmcd 			case SADB_SATYPE_ESP:
23004867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
23014064Smarkfen 				    "for ESP "));
23023055Sdanmcd 				break;
23033055Sdanmcd 			case SADB_SATYPE_AH:
23044867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
23054867Spwernau 				    "for AH "));
23063055Sdanmcd 				break;
23073055Sdanmcd 			default:
23084867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
23094064Smarkfen 				    "for satype=%d "),
23103055Sdanmcd 				    algdesc->sadb_x_algdesc_satype);
23113055Sdanmcd 			}
23123055Sdanmcd 			switch (algdesc->sadb_x_algdesc_algtype) {
23133055Sdanmcd 			case SADB_X_ALGTYPE_CRYPT:
23144867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
23154064Smarkfen 				    "Encryption = "));
23163055Sdanmcd 				(void) dump_ealg(algdesc->sadb_x_algdesc_alg,
23174867Spwernau 				    file);
23183055Sdanmcd 				break;
23193055Sdanmcd 			case SADB_X_ALGTYPE_AUTH:
23204867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
23214064Smarkfen 				    "Authentication = "));
23223055Sdanmcd 				(void) dump_aalg(algdesc->sadb_x_algdesc_alg,
23234867Spwernau 				    file);
23243055Sdanmcd 				break;
23253055Sdanmcd 			default:
23264867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
23274064Smarkfen 				    "algtype(%d) = alg(%d)"),
23283055Sdanmcd 				    algdesc->sadb_x_algdesc_algtype,
23293055Sdanmcd 				    algdesc->sadb_x_algdesc_alg);
23303055Sdanmcd 				break;
23313055Sdanmcd 			}
23323055Sdanmcd 
23334867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
233410824SMark.Fenwick@Sun.COM 			    "  minbits=%u, maxbits=%u, saltbits=%u\n"),
23353055Sdanmcd 			    algdesc->sadb_x_algdesc_minbits,
233610824SMark.Fenwick@Sun.COM 			    algdesc->sadb_x_algdesc_maxbits,
233710824SMark.Fenwick@Sun.COM 			    algdesc->sadb_x_algdesc_reserved);
23383055Sdanmcd 
23393055Sdanmcd 			sofar = (uint64_t *)(++algdesc);
23403055Sdanmcd 		}
23413055Sdanmcd 		ecomb = (struct sadb_x_ecomb *)sofar;
23423055Sdanmcd 	}
23433055Sdanmcd }
23443055Sdanmcd 
23453055Sdanmcd /*
23463055Sdanmcd  * Print an SADB_EXT_SUPPORTED extension.
23473055Sdanmcd  */
23483055Sdanmcd void
23494867Spwernau print_supp(FILE *file, char *prefix, struct sadb_supported *supp)
23503055Sdanmcd {
23513055Sdanmcd 	struct sadb_alg *algs;
23523055Sdanmcd 	int i, numalgs;
23533055Sdanmcd 
23544867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "%sSupported "), prefix);
23553055Sdanmcd 	switch (supp->sadb_supported_exttype) {
23563055Sdanmcd 	case SADB_EXT_SUPPORTED_AUTH:
23574867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "authentication"));
23583055Sdanmcd 		break;
23593055Sdanmcd 	case SADB_EXT_SUPPORTED_ENCRYPT:
23604867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "encryption"));
23613055Sdanmcd 		break;
23623055Sdanmcd 	}
23634867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, " algorithms.\n"));
23643055Sdanmcd 
23653055Sdanmcd 	algs = (struct sadb_alg *)(supp + 1);
23663055Sdanmcd 	numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp));
23673055Sdanmcd 	numalgs /= SADB_8TO64(sizeof (*algs));
23683055Sdanmcd 	for (i = 0; i < numalgs; i++) {
23695906Svk199839 		uint16_t exttype = supp->sadb_supported_exttype;
23705906Svk199839 
23714867Spwernau 		(void) fprintf(file, "%s", prefix);
23725906Svk199839 		switch (exttype) {
23733055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
23744867Spwernau 			(void) dump_aalg(algs[i].sadb_alg_id, file);
23753055Sdanmcd 			break;
23763055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
23774867Spwernau 			(void) dump_ealg(algs[i].sadb_alg_id, file);
23783055Sdanmcd 			break;
23793055Sdanmcd 		}
23804867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
238110824SMark.Fenwick@Sun.COM 		    " minbits=%u, maxbits=%u, ivlen=%u, saltbits=%u"),
23823055Sdanmcd 		    algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits,
238310824SMark.Fenwick@Sun.COM 		    algs[i].sadb_alg_ivlen, algs[i].sadb_x_alg_saltbits);
23845906Svk199839 		if (exttype == SADB_EXT_SUPPORTED_ENCRYPT)
23855906Svk199839 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
23865906Svk199839 			    ", increment=%u"), algs[i].sadb_x_alg_increment);
23875906Svk199839 		(void) fprintf(file, dgettext(TEXT_DOMAIN, ".\n"));
23883055Sdanmcd 	}
23893055Sdanmcd }
23903055Sdanmcd 
23913055Sdanmcd /*
23923055Sdanmcd  * Print an SADB_EXT_SPIRANGE extension.
23933055Sdanmcd  */
23943055Sdanmcd void
23954867Spwernau print_spirange(FILE *file, char *prefix, struct sadb_spirange *range)
23963055Sdanmcd {
23974867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
23984064Smarkfen 	    "%sSPI Range, min=0x%x, max=0x%x\n"), prefix,
23993055Sdanmcd 	    htonl(range->sadb_spirange_min),
24003055Sdanmcd 	    htonl(range->sadb_spirange_max));
24013055Sdanmcd }
24023055Sdanmcd 
24033055Sdanmcd /*
24043055Sdanmcd  * Print an SADB_X_EXT_KM_COOKIE extension.
24053055Sdanmcd  */
24063055Sdanmcd 
24073055Sdanmcd void
24084867Spwernau print_kmc(FILE *file, char *prefix, struct sadb_x_kmc *kmc)
24093055Sdanmcd {
24103055Sdanmcd 	char *cookie_label;
24113055Sdanmcd 
24123055Sdanmcd 	if ((cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie)) ==
24133055Sdanmcd 	    NULL)
24144064Smarkfen 		cookie_label = dgettext(TEXT_DOMAIN, "<Label not found.>");
24153055Sdanmcd 
24164867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
24174064Smarkfen 	    "%sProtocol %u, cookie=\"%s\" (%u)\n"), prefix,
24183055Sdanmcd 	    kmc->sadb_x_kmc_proto, cookie_label, kmc->sadb_x_kmc_cookie);
24193055Sdanmcd }
24207749SThejaswini.Singarajipura@Sun.COM 
24217749SThejaswini.Singarajipura@Sun.COM /*
24227749SThejaswini.Singarajipura@Sun.COM  * Print an SADB_X_EXT_REPLAY_CTR extension.
24237749SThejaswini.Singarajipura@Sun.COM  */
24247749SThejaswini.Singarajipura@Sun.COM 
24257749SThejaswini.Singarajipura@Sun.COM void
24267749SThejaswini.Singarajipura@Sun.COM print_replay(FILE *file, char *prefix, sadb_x_replay_ctr_t *repl)
24277749SThejaswini.Singarajipura@Sun.COM {
24287749SThejaswini.Singarajipura@Sun.COM 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
24297749SThejaswini.Singarajipura@Sun.COM 	    "%sReplay Value "), prefix);
24307749SThejaswini.Singarajipura@Sun.COM 	if ((repl->sadb_x_rc_replay32 == 0) &&
24317749SThejaswini.Singarajipura@Sun.COM 	    (repl->sadb_x_rc_replay64 == 0)) {
24327749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
24337749SThejaswini.Singarajipura@Sun.COM 		    "<Value not found.>"));
24347749SThejaswini.Singarajipura@Sun.COM 	}
24357749SThejaswini.Singarajipura@Sun.COM 	/*
24367749SThejaswini.Singarajipura@Sun.COM 	 * We currently do not support a 64-bit replay value.
24377749SThejaswini.Singarajipura@Sun.COM 	 * RFC 4301 will require one, however, and we have a field
24387749SThejaswini.Singarajipura@Sun.COM 	 * in place when 4301 is built.
24397749SThejaswini.Singarajipura@Sun.COM 	 */
24407749SThejaswini.Singarajipura@Sun.COM 	(void) fprintf(file, "% " PRIu64 "\n",
24417749SThejaswini.Singarajipura@Sun.COM 	    ((repl->sadb_x_rc_replay32 == 0) ?
24427749SThejaswini.Singarajipura@Sun.COM 	    repl->sadb_x_rc_replay64 : repl->sadb_x_rc_replay32));
24437749SThejaswini.Singarajipura@Sun.COM }
24446668Smarkfen /*
24456668Smarkfen  * Print an SADB_X_EXT_PAIR extension.
24466668Smarkfen  */
24476668Smarkfen static void
24486668Smarkfen print_pair(FILE *file, char *prefix, struct sadb_x_pair *pair)
24496668Smarkfen {
24506668Smarkfen 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "%sPaired with spi=0x%x\n"),
24516668Smarkfen 	    prefix, ntohl(pair->sadb_x_pair_spi));
24526668Smarkfen }
24533055Sdanmcd 
24543055Sdanmcd /*
24553055Sdanmcd  * Take a PF_KEY message pointed to buffer and print it.  Useful for DUMP
24563055Sdanmcd  * and GET.
24573055Sdanmcd  */
24583055Sdanmcd void
24594867Spwernau print_samsg(FILE *file, uint64_t *buffer, boolean_t want_timestamp,
24604867Spwernau     boolean_t vflag, boolean_t ignore_nss)
24613055Sdanmcd {
24623055Sdanmcd 	uint64_t *current;
24633055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
24643055Sdanmcd 	struct sadb_ext *ext;
24653055Sdanmcd 	struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL;
24667749SThejaswini.Singarajipura@Sun.COM 	struct sadb_lifetime *idlelt = NULL;
24673055Sdanmcd 	int i;
24683055Sdanmcd 	time_t wallclock;
24693055Sdanmcd 
24703055Sdanmcd 	(void) time(&wallclock);
24713055Sdanmcd 
24724867Spwernau 	print_sadb_msg(file, samsg, want_timestamp ? wallclock : 0, vflag);
24733055Sdanmcd 	current = (uint64_t *)(samsg + 1);
24743055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
24753055Sdanmcd 		int lenbytes;
24763055Sdanmcd 
24773055Sdanmcd 		ext = (struct sadb_ext *)current;
24783055Sdanmcd 		lenbytes = SADB_64TO8(ext->sadb_ext_len);
24793055Sdanmcd 		switch (ext->sadb_ext_type) {
24803055Sdanmcd 		case SADB_EXT_SA:
24814867Spwernau 			print_sa(file, dgettext(TEXT_DOMAIN,
24824064Smarkfen 			    "SA: "), (struct sadb_sa *)current);
24833055Sdanmcd 			break;
24843055Sdanmcd 		/*
24853055Sdanmcd 		 * Pluck out lifetimes and print them at the end.  This is
24863055Sdanmcd 		 * to show relative lifetimes.
24873055Sdanmcd 		 */
24883055Sdanmcd 		case SADB_EXT_LIFETIME_CURRENT:
24893055Sdanmcd 			currentlt = (struct sadb_lifetime *)current;
24903055Sdanmcd 			break;
24913055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
24923055Sdanmcd 			hardlt = (struct sadb_lifetime *)current;
24933055Sdanmcd 			break;
24943055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
24953055Sdanmcd 			softlt = (struct sadb_lifetime *)current;
24963055Sdanmcd 			break;
24977749SThejaswini.Singarajipura@Sun.COM 		case SADB_X_EXT_LIFETIME_IDLE:
24987749SThejaswini.Singarajipura@Sun.COM 			idlelt = (struct sadb_lifetime *)current;
24997749SThejaswini.Singarajipura@Sun.COM 			break;
25003055Sdanmcd 
25013055Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
25024867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "SRC: "),
25034867Spwernau 			    (struct sadb_address *)current, ignore_nss);
25043055Sdanmcd 			break;
25053055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
25064867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "INS: "),
25074867Spwernau 			    (struct sadb_address *)current, ignore_nss);
25083055Sdanmcd 			break;
25093055Sdanmcd 		case SADB_EXT_ADDRESS_DST:
25104867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "DST: "),
25114867Spwernau 			    (struct sadb_address *)current, ignore_nss);
25123055Sdanmcd 			break;
25133055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
25144867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "IND: "),
25154867Spwernau 			    (struct sadb_address *)current, ignore_nss);
25163055Sdanmcd 			break;
25173055Sdanmcd 		case SADB_EXT_KEY_AUTH:
25184867Spwernau 			print_key(file, dgettext(TEXT_DOMAIN,
25194064Smarkfen 			    "AKY: "), (struct sadb_key *)current);
25203055Sdanmcd 			break;
25213055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
25224867Spwernau 			print_key(file, dgettext(TEXT_DOMAIN,
25234064Smarkfen 			    "EKY: "), (struct sadb_key *)current);
25243055Sdanmcd 			break;
25253055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
25264867Spwernau 			print_ident(file, dgettext(TEXT_DOMAIN, "SID: "),
25273055Sdanmcd 			    (struct sadb_ident *)current);
25283055Sdanmcd 			break;
25293055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
25304867Spwernau 			print_ident(file, dgettext(TEXT_DOMAIN, "DID: "),
25313055Sdanmcd 			    (struct sadb_ident *)current);
25323055Sdanmcd 			break;
25333055Sdanmcd 		case SADB_EXT_SENSITIVITY:
25344867Spwernau 			print_sens(file, dgettext(TEXT_DOMAIN, "SNS: "),
2535*10934Ssommerfeld@sun.com 			    (struct sadb_sens *)current, ignore_nss);
25363055Sdanmcd 			break;
25373055Sdanmcd 		case SADB_EXT_PROPOSAL:
25384867Spwernau 			print_prop(file, dgettext(TEXT_DOMAIN, "PRP: "),
25393055Sdanmcd 			    (struct sadb_prop *)current);
25403055Sdanmcd 			break;
25413055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
25424867Spwernau 			print_supp(file, dgettext(TEXT_DOMAIN, "SUA: "),
25433055Sdanmcd 			    (struct sadb_supported *)current);
25443055Sdanmcd 			break;
25453055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
25464867Spwernau 			print_supp(file, dgettext(TEXT_DOMAIN, "SUE: "),
25473055Sdanmcd 			    (struct sadb_supported *)current);
25483055Sdanmcd 			break;
25493055Sdanmcd 		case SADB_EXT_SPIRANGE:
25504867Spwernau 			print_spirange(file, dgettext(TEXT_DOMAIN, "SPR: "),
25513055Sdanmcd 			    (struct sadb_spirange *)current);
25523055Sdanmcd 			break;
25533055Sdanmcd 		case SADB_X_EXT_EPROP:
25544867Spwernau 			print_eprop(file, dgettext(TEXT_DOMAIN, "EPR: "),
25553055Sdanmcd 			    (struct sadb_prop *)current);
25563055Sdanmcd 			break;
25573055Sdanmcd 		case SADB_X_EXT_KM_COOKIE:
25584867Spwernau 			print_kmc(file, dgettext(TEXT_DOMAIN, "KMC: "),
25593055Sdanmcd 			    (struct sadb_x_kmc *)current);
25603055Sdanmcd 			break;
25613055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
25624867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "NRM: "),
25634867Spwernau 			    (struct sadb_address *)current, ignore_nss);
25643055Sdanmcd 			break;
25653055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
25664867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "NLC: "),
25674867Spwernau 			    (struct sadb_address *)current, ignore_nss);
25683055Sdanmcd 			break;
25696668Smarkfen 		case SADB_X_EXT_PAIR:
25706668Smarkfen 			print_pair(file, dgettext(TEXT_DOMAIN, "OTH: "),
25716668Smarkfen 			    (struct sadb_x_pair *)current);
25726668Smarkfen 			break;
2573*10934Ssommerfeld@sun.com 		case SADB_X_EXT_OUTER_SENS:
2574*10934Ssommerfeld@sun.com 			print_sens(file, dgettext(TEXT_DOMAIN, "OSN: "),
2575*10934Ssommerfeld@sun.com 			    (struct sadb_sens *)current, ignore_nss);
2576*10934Ssommerfeld@sun.com 			break;
25777749SThejaswini.Singarajipura@Sun.COM 		case SADB_X_EXT_REPLAY_VALUE:
25787749SThejaswini.Singarajipura@Sun.COM 			(void) print_replay(file, dgettext(TEXT_DOMAIN,
25797749SThejaswini.Singarajipura@Sun.COM 			    "RPL: "), (sadb_x_replay_ctr_t *)current);
25807749SThejaswini.Singarajipura@Sun.COM 			break;
25813055Sdanmcd 		default:
25824867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
25833055Sdanmcd 			    "UNK: Unknown ext. %d, len %d.\n"),
25843055Sdanmcd 			    ext->sadb_ext_type, lenbytes);
25853055Sdanmcd 			for (i = 0; i < ext->sadb_ext_len; i++)
25864867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
25877320Sdanmcd@sun.com 				    "UNK: 0x%" PRIx64 "\n"),
25887320Sdanmcd@sun.com 				    ((uint64_t *)ext)[i]);
25893055Sdanmcd 			break;
25903055Sdanmcd 		}
25913055Sdanmcd 		current += (lenbytes == 0) ?
25923055Sdanmcd 		    SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len;
25933055Sdanmcd 	}
25943055Sdanmcd 	/*
25953055Sdanmcd 	 * Print lifetimes NOW.
25963055Sdanmcd 	 */
25977749SThejaswini.Singarajipura@Sun.COM 	if (currentlt != NULL || hardlt != NULL || softlt != NULL ||
25987749SThejaswini.Singarajipura@Sun.COM 	    idlelt != NULL)
25997749SThejaswini.Singarajipura@Sun.COM 		print_lifetimes(file, wallclock, currentlt, hardlt,
26007749SThejaswini.Singarajipura@Sun.COM 		    softlt, idlelt, vflag);
26013055Sdanmcd 
26023055Sdanmcd 	if (current - buffer != samsg->sadb_msg_len) {
26034867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
26044867Spwernau 		    "WARNING: insufficient buffer space or corrupt message."));
26053055Sdanmcd 	}
26063055Sdanmcd 
26074867Spwernau 	(void) fflush(file);	/* Make sure our message is out there. */
26083055Sdanmcd }
26093055Sdanmcd 
26103055Sdanmcd /*
26113055Sdanmcd  * save_XXX functions are used when "saving" the SA tables to either a
26123055Sdanmcd  * file or standard output.  They use the dump_XXX functions where needed,
26133055Sdanmcd  * but mostly they use the rparseXXX functions.
26143055Sdanmcd  */
26153055Sdanmcd 
26163055Sdanmcd /*
26173055Sdanmcd  * Print save information for a lifetime extension.
26183055Sdanmcd  *
26193055Sdanmcd  * NOTE : It saves the lifetime in absolute terms.  For example, if you
26203055Sdanmcd  * had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though
26213055Sdanmcd  * there may have been 59 seconds burned off the clock.
26223055Sdanmcd  */
26233055Sdanmcd boolean_t
26243055Sdanmcd save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile)
26253055Sdanmcd {
26263055Sdanmcd 	char *prefix;
26273055Sdanmcd 
26287749SThejaswini.Singarajipura@Sun.COM 	switch (lifetime->sadb_lifetime_exttype) {
26297749SThejaswini.Singarajipura@Sun.COM 	case SADB_EXT_LIFETIME_HARD:
26307749SThejaswini.Singarajipura@Sun.COM 		prefix = "hard";
26317749SThejaswini.Singarajipura@Sun.COM 		break;
26327749SThejaswini.Singarajipura@Sun.COM 	case SADB_EXT_LIFETIME_SOFT:
26337749SThejaswini.Singarajipura@Sun.COM 		prefix = "soft";
26347749SThejaswini.Singarajipura@Sun.COM 		break;
26357749SThejaswini.Singarajipura@Sun.COM 	case SADB_X_EXT_LIFETIME_IDLE:
26367749SThejaswini.Singarajipura@Sun.COM 		prefix = "idle";
26377749SThejaswini.Singarajipura@Sun.COM 		break;
26387749SThejaswini.Singarajipura@Sun.COM 	}
26393055Sdanmcd 
26403055Sdanmcd 	if (putc('\t', ofile) == EOF)
26413055Sdanmcd 		return (B_FALSE);
26423055Sdanmcd 
26433055Sdanmcd 	if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile,
26443055Sdanmcd 	    "%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0)
26453055Sdanmcd 		return (B_FALSE);
26463055Sdanmcd 
26473055Sdanmcd 	if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile,
26487320Sdanmcd@sun.com 	    "%s_bytes %" PRIu64 " ", prefix, lifetime->sadb_lifetime_bytes) < 0)
26493055Sdanmcd 		return (B_FALSE);
26503055Sdanmcd 
26513055Sdanmcd 	if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile,
26527320Sdanmcd@sun.com 	    "%s_addtime %" PRIu64 " ", prefix,
26537320Sdanmcd@sun.com 	    lifetime->sadb_lifetime_addtime) < 0)
26543055Sdanmcd 		return (B_FALSE);
26553055Sdanmcd 
26563055Sdanmcd 	if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile,
26577320Sdanmcd@sun.com 	    "%s_usetime %" PRIu64 " ", prefix,
26587320Sdanmcd@sun.com 	    lifetime->sadb_lifetime_usetime) < 0)
26593055Sdanmcd 		return (B_FALSE);
26603055Sdanmcd 
26613055Sdanmcd 	return (B_TRUE);
26623055Sdanmcd }
26633055Sdanmcd 
26643055Sdanmcd /*
26653055Sdanmcd  * Print save information for an address extension.
26663055Sdanmcd  */
26673055Sdanmcd boolean_t
26683055Sdanmcd save_address(struct sadb_address *addr, FILE *ofile)
26693055Sdanmcd {
26703055Sdanmcd 	char *printable_addr, buf[INET6_ADDRSTRLEN];
26713055Sdanmcd 	const char *prefix, *pprefix;
26723055Sdanmcd 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1);
26733055Sdanmcd 	struct sockaddr_in *sin = (struct sockaddr_in *)sin6;
26743055Sdanmcd 	int af = sin->sin_family;
26753055Sdanmcd 
26763055Sdanmcd 	/*
26773055Sdanmcd 	 * Address-family reality check.
26783055Sdanmcd 	 */
26793055Sdanmcd 	if (af != AF_INET6 && af != AF_INET)
26803055Sdanmcd 		return (B_FALSE);
26813055Sdanmcd 
26823055Sdanmcd 	switch (addr->sadb_address_exttype) {
26833055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
26843055Sdanmcd 		prefix = "src";
26853055Sdanmcd 		pprefix = "sport";
26863055Sdanmcd 		break;
26873055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
26883055Sdanmcd 		prefix = "isrc";
26893055Sdanmcd 		pprefix = "isport";
26903055Sdanmcd 		break;
26913055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
26923055Sdanmcd 		prefix = "dst";
26933055Sdanmcd 		pprefix = "dport";
26943055Sdanmcd 		break;
26953055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
26963055Sdanmcd 		prefix = "idst";
26973055Sdanmcd 		pprefix = "idport";
26983055Sdanmcd 		break;
26993055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
27003055Sdanmcd 		prefix = "nat_loc ";
27013055Sdanmcd 		pprefix = "nat_lport";
27023055Sdanmcd 		break;
27033055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
27043055Sdanmcd 		prefix = "nat_rem ";
27053055Sdanmcd 		pprefix = "nat_rport";
27063055Sdanmcd 		break;
27073055Sdanmcd 	}
27083055Sdanmcd 
27093055Sdanmcd 	if (fprintf(ofile, "    %s ", prefix) < 0)
27103055Sdanmcd 		return (B_FALSE);
27113055Sdanmcd 
27123055Sdanmcd 	/*
27133055Sdanmcd 	 * Do not do address-to-name translation, given that we live in
27143055Sdanmcd 	 * an age of names that explode into many addresses.
27153055Sdanmcd 	 */
27163055Sdanmcd 	printable_addr = (char *)inet_ntop(af,
27173055Sdanmcd 	    (af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr,
27183055Sdanmcd 	    buf, sizeof (buf));
27193055Sdanmcd 	if (printable_addr == NULL)
27204064Smarkfen 		printable_addr = "Invalid IP address.";
27213055Sdanmcd 	if (fprintf(ofile, "%s", printable_addr) < 0)
27223055Sdanmcd 		return (B_FALSE);
27233055Sdanmcd 	if (addr->sadb_address_prefixlen != 0 &&
27243055Sdanmcd 	    !((addr->sadb_address_prefixlen == 32 && af == AF_INET) ||
27254342Spwernau 	    (addr->sadb_address_prefixlen == 128 && af == AF_INET6))) {
27263055Sdanmcd 		if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0)
27273055Sdanmcd 			return (B_FALSE);
27283055Sdanmcd 	}
27293055Sdanmcd 
27303055Sdanmcd 	/*
27313055Sdanmcd 	 * The port is in the same position for struct sockaddr_in and
27323055Sdanmcd 	 * struct sockaddr_in6.  We exploit that property here.
27333055Sdanmcd 	 */
27343055Sdanmcd 	if ((pprefix != NULL) && (sin->sin_port != 0))
27353055Sdanmcd 		(void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port));
27363055Sdanmcd 
27373055Sdanmcd 	return (B_TRUE);
27383055Sdanmcd }
27393055Sdanmcd 
27403055Sdanmcd /*
27413055Sdanmcd  * Print save information for a key extension. Returns whether writing
27423055Sdanmcd  * to the specified output file was successful or not.
27433055Sdanmcd  */
27443055Sdanmcd boolean_t
27453055Sdanmcd save_key(struct sadb_key *key, FILE *ofile)
27463055Sdanmcd {
27473055Sdanmcd 	char *prefix;
27483055Sdanmcd 
27493055Sdanmcd 	if (putc('\t', ofile) == EOF)
27503055Sdanmcd 		return (B_FALSE);
27513055Sdanmcd 
27523055Sdanmcd 	prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr";
27533055Sdanmcd 
27543055Sdanmcd 	if (fprintf(ofile, "%skey ", prefix) < 0)
27553055Sdanmcd 		return (B_FALSE);
27563055Sdanmcd 
275710824SMark.Fenwick@Sun.COM 	if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits,
275810824SMark.Fenwick@Sun.COM 	    key->sadb_key_reserved, ofile, B_FALSE) == -1)
27593055Sdanmcd 		return (B_FALSE);
27603055Sdanmcd 
27613055Sdanmcd 	return (B_TRUE);
27623055Sdanmcd }
27633055Sdanmcd 
27643055Sdanmcd /*
27653055Sdanmcd  * Print save information for an identity extension.
27663055Sdanmcd  */
27673055Sdanmcd boolean_t
27683055Sdanmcd save_ident(struct sadb_ident *ident, FILE *ofile)
27693055Sdanmcd {
27703055Sdanmcd 	char *prefix;
27713055Sdanmcd 
27723055Sdanmcd 	if (putc('\t', ofile) == EOF)
27733055Sdanmcd 		return (B_FALSE);
27743055Sdanmcd 
27753055Sdanmcd 	prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" :
27763055Sdanmcd 	    "dst";
27773055Sdanmcd 
27783055Sdanmcd 	if (fprintf(ofile, "%sidtype %s ", prefix,
27793055Sdanmcd 	    rparseidtype(ident->sadb_ident_type)) < 0)
27803055Sdanmcd 		return (B_FALSE);
27813055Sdanmcd 
27823055Sdanmcd 	if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN ||
27833055Sdanmcd 	    ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) {
27844064Smarkfen 		if (fprintf(ofile, dgettext(TEXT_DOMAIN,
27854064Smarkfen 		    "<can-not-print>")) < 0)
27863055Sdanmcd 			return (B_FALSE);
27873055Sdanmcd 	} else {
27883055Sdanmcd 		if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0)
27893055Sdanmcd 			return (B_FALSE);
27903055Sdanmcd 	}
27913055Sdanmcd 
27923055Sdanmcd 	return (B_TRUE);
27933055Sdanmcd }
27943055Sdanmcd 
2795*10934Ssommerfeld@sun.com boolean_t
2796*10934Ssommerfeld@sun.com save_sens(struct sadb_sens *sens, FILE *ofile)
2797*10934Ssommerfeld@sun.com {
2798*10934Ssommerfeld@sun.com 	char *prefix;
2799*10934Ssommerfeld@sun.com 	char *hlabel;
2800*10934Ssommerfeld@sun.com 	bslabel_t sl;
2801*10934Ssommerfeld@sun.com 
2802*10934Ssommerfeld@sun.com 	if (putc('\t', ofile) == EOF)
2803*10934Ssommerfeld@sun.com 		return (B_FALSE);
2804*10934Ssommerfeld@sun.com 
2805*10934Ssommerfeld@sun.com 	if (sens->sadb_sens_exttype == SADB_EXT_SENSITIVITY)
2806*10934Ssommerfeld@sun.com 		prefix = "label";
2807*10934Ssommerfeld@sun.com 	else if ((sens->sadb_x_sens_flags & SADB_X_SENS_IMPLICIT) == 0)
2808*10934Ssommerfeld@sun.com 		prefix = "outer-label";
2809*10934Ssommerfeld@sun.com 	else
2810*10934Ssommerfeld@sun.com 		prefix = "implicit-label";
2811*10934Ssommerfeld@sun.com 
2812*10934Ssommerfeld@sun.com 	ipsec_convert_sens_to_bslabel(sens, &sl);
2813*10934Ssommerfeld@sun.com 	ipsec_convert_bslabel_to_hex(&sl, &hlabel);
2814*10934Ssommerfeld@sun.com 
2815*10934Ssommerfeld@sun.com 	if (fprintf(ofile, "%s %s ", prefix, hlabel) < 0) {
2816*10934Ssommerfeld@sun.com 		free(hlabel);
2817*10934Ssommerfeld@sun.com 		return (B_FALSE);
2818*10934Ssommerfeld@sun.com 	}
2819*10934Ssommerfeld@sun.com 	free(hlabel);
2820*10934Ssommerfeld@sun.com 
2821*10934Ssommerfeld@sun.com 	return (B_TRUE);
2822*10934Ssommerfeld@sun.com }
2823*10934Ssommerfeld@sun.com 
28243055Sdanmcd /*
28253055Sdanmcd  * "Save" a security association to an output file.
28263055Sdanmcd  *
28274064Smarkfen  * NOTE the lack of calls to dgettext() because I'm outputting parseable stuff.
28283055Sdanmcd  * ALSO NOTE that if you change keywords (see parsecmd()), you'll have to
28293055Sdanmcd  * change them here as well.
28303055Sdanmcd  */
28313055Sdanmcd void
28323055Sdanmcd save_assoc(uint64_t *buffer, FILE *ofile)
28333055Sdanmcd {
28344064Smarkfen 	int terrno;
28354987Sdanmcd 	boolean_t seen_proto = B_FALSE, seen_iproto = B_FALSE;
28363055Sdanmcd 	uint64_t *current;
28373055Sdanmcd 	struct sadb_address *addr;
28387749SThejaswini.Singarajipura@Sun.COM 	struct sadb_x_replay_ctr *repl;
28393055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
28403055Sdanmcd 	struct sadb_ext *ext;
28413055Sdanmcd 
28424064Smarkfen #define	tidyup() \
28434064Smarkfen 	terrno = errno; (void) fclose(ofile); errno = terrno; \
28444064Smarkfen 	interactive = B_FALSE
28454064Smarkfen 
28464064Smarkfen #define	savenl() if (fputs(" \\\n", ofile) == EOF) \
28474064Smarkfen 	{ bail(dgettext(TEXT_DOMAIN, "savenl")); }
28483055Sdanmcd 
28493055Sdanmcd 	if (fputs("# begin assoc\n", ofile) == EOF)
28504064Smarkfen 		bail(dgettext(TEXT_DOMAIN,
28514064Smarkfen 		    "save_assoc: Opening comment of SA"));
28523055Sdanmcd 	if (fprintf(ofile, "add %s ", rparsesatype(samsg->sadb_msg_satype)) < 0)
28534064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: First line of SA"));
28543055Sdanmcd 	savenl();
28553055Sdanmcd 
28563055Sdanmcd 	current = (uint64_t *)(samsg + 1);
28573055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
28583055Sdanmcd 		struct sadb_sa *assoc;
28593055Sdanmcd 
28603055Sdanmcd 		ext = (struct sadb_ext *)current;
28614987Sdanmcd 		addr = (struct sadb_address *)ext;  /* Just in case... */
28623055Sdanmcd 		switch (ext->sadb_ext_type) {
28633055Sdanmcd 		case SADB_EXT_SA:
28643055Sdanmcd 			assoc = (struct sadb_sa *)ext;
28653055Sdanmcd 			if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
28663055Sdanmcd 				if (fprintf(ofile, "# WARNING: SA was dying "
28673055Sdanmcd 				    "or dead.\n") < 0) {
28684064Smarkfen 					tidyup();
28694064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
28704064Smarkfen 					    "save_assoc: fprintf not mature"));
28713055Sdanmcd 				}
28723055Sdanmcd 			}
28733055Sdanmcd 			if (fprintf(ofile, "    spi 0x%x ",
28744064Smarkfen 			    ntohl(assoc->sadb_sa_spi)) < 0) {
28754064Smarkfen 				tidyup();
28764064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
28774064Smarkfen 				    "save_assoc: fprintf spi"));
28784064Smarkfen 			}
28793055Sdanmcd 			if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
28803055Sdanmcd 				if (fprintf(ofile, "encr_alg %s ",
28813055Sdanmcd 				    rparsealg(assoc->sadb_sa_encrypt,
28824342Spwernau 				    IPSEC_PROTO_ESP)) < 0) {
28834064Smarkfen 					tidyup();
28844064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
28854064Smarkfen 					    "save_assoc: fprintf encrypt"));
28864064Smarkfen 				}
28873055Sdanmcd 			}
28883055Sdanmcd 			if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
28893055Sdanmcd 				if (fprintf(ofile, "auth_alg %s ",
28903055Sdanmcd 				    rparsealg(assoc->sadb_sa_auth,
28914342Spwernau 				    IPSEC_PROTO_AH)) < 0) {
28924064Smarkfen 					tidyup();
28934064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
28944064Smarkfen 					    "save_assoc: fprintf auth"));
28954064Smarkfen 				}
28963055Sdanmcd 			}
28973055Sdanmcd 			if (fprintf(ofile, "replay %d ",
28984064Smarkfen 			    assoc->sadb_sa_replay) < 0) {
28994064Smarkfen 				tidyup();
29004064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
29014064Smarkfen 				    "save_assoc: fprintf replay"));
29024064Smarkfen 			}
29033055Sdanmcd 			if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC |
29043055Sdanmcd 			    SADB_X_SAFLAGS_NATT_REM)) {
29054064Smarkfen 				if (fprintf(ofile, "encap udp") < 0) {
29064064Smarkfen 					tidyup();
29074064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
29084064Smarkfen 					    "save_assoc: fprintf encap"));
29094064Smarkfen 				}
29103055Sdanmcd 			}
29113055Sdanmcd 			savenl();
29123055Sdanmcd 			break;
29133055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
29143055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
29157749SThejaswini.Singarajipura@Sun.COM 		case SADB_X_EXT_LIFETIME_IDLE:
29164064Smarkfen 			if (!save_lifetime((struct sadb_lifetime *)ext,
29174064Smarkfen 			    ofile)) {
29184064Smarkfen 				tidyup();
29194064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_lifetime"));
29204064Smarkfen 			}
29213055Sdanmcd 			savenl();
29223055Sdanmcd 			break;
29233055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
29243055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
29254987Sdanmcd 			if (!seen_iproto && addr->sadb_address_proto) {
29264987Sdanmcd 				(void) fprintf(ofile, "    iproto %d",
29274987Sdanmcd 				    addr->sadb_address_proto);
29284987Sdanmcd 				savenl();
29294987Sdanmcd 				seen_iproto = B_TRUE;
29304987Sdanmcd 			}
29314987Sdanmcd 			goto skip_srcdst;  /* Hack to avoid cases below... */
29324987Sdanmcd 			/* FALLTHRU */
29334987Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
29344987Sdanmcd 		case SADB_EXT_ADDRESS_DST:
29353055Sdanmcd 			if (!seen_proto && addr->sadb_address_proto) {
29363055Sdanmcd 				(void) fprintf(ofile, "    proto %d",
29373055Sdanmcd 				    addr->sadb_address_proto);
29383055Sdanmcd 				savenl();
29394987Sdanmcd 				seen_proto = B_TRUE;
29403055Sdanmcd 			}
29414987Sdanmcd 			/* FALLTHRU */
29424987Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
29434987Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
29444987Sdanmcd skip_srcdst:
29454064Smarkfen 			if (!save_address(addr, ofile)) {
29464064Smarkfen 				tidyup();
29474064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
29484064Smarkfen 			}
29493055Sdanmcd 			savenl();
29503055Sdanmcd 			break;
29513055Sdanmcd 		case SADB_EXT_KEY_AUTH:
29523055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
29534064Smarkfen 			if (!save_key((struct sadb_key *)ext, ofile)) {
29544064Smarkfen 				tidyup();
29554064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
29564064Smarkfen 			}
29573055Sdanmcd 			savenl();
29583055Sdanmcd 			break;
29593055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
29603055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
29614064Smarkfen 			if (!save_ident((struct sadb_ident *)ext, ofile)) {
29624064Smarkfen 				tidyup();
29634064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
29644064Smarkfen 			}
29653055Sdanmcd 			savenl();
29663055Sdanmcd 			break;
29677749SThejaswini.Singarajipura@Sun.COM 		case SADB_X_EXT_REPLAY_VALUE:
29687749SThejaswini.Singarajipura@Sun.COM 			repl = (sadb_x_replay_ctr_t *)ext;
29697749SThejaswini.Singarajipura@Sun.COM 			if ((repl->sadb_x_rc_replay32 == 0) &&
29707749SThejaswini.Singarajipura@Sun.COM 			    (repl->sadb_x_rc_replay64 == 0)) {
29717749SThejaswini.Singarajipura@Sun.COM 				tidyup();
29727749SThejaswini.Singarajipura@Sun.COM 				bail(dgettext(TEXT_DOMAIN, "Replay Value"));
29737749SThejaswini.Singarajipura@Sun.COM 			}
29747749SThejaswini.Singarajipura@Sun.COM 			if (fprintf(ofile, "replay_value %" PRIu64 "",
29757749SThejaswini.Singarajipura@Sun.COM 			    (repl->sadb_x_rc_replay32 == 0 ?
29767749SThejaswini.Singarajipura@Sun.COM 			    repl->sadb_x_rc_replay64 :
29777749SThejaswini.Singarajipura@Sun.COM 			    repl->sadb_x_rc_replay32)) < 0) {
29787749SThejaswini.Singarajipura@Sun.COM 				tidyup();
29797749SThejaswini.Singarajipura@Sun.COM 				bail(dgettext(TEXT_DOMAIN,
29807749SThejaswini.Singarajipura@Sun.COM 				    "save_assoc: fprintf replay value"));
29817749SThejaswini.Singarajipura@Sun.COM 			}
29827749SThejaswini.Singarajipura@Sun.COM 			savenl();
29837749SThejaswini.Singarajipura@Sun.COM 			break;
29843055Sdanmcd 		case SADB_EXT_SENSITIVITY:
2985*10934Ssommerfeld@sun.com 		case SADB_X_EXT_OUTER_SENS:
2986*10934Ssommerfeld@sun.com 			if (!save_sens((struct sadb_sens *)ext, ofile)) {
2987*10934Ssommerfeld@sun.com 				tidyup();
2988*10934Ssommerfeld@sun.com 				bail(dgettext(TEXT_DOMAIN, "save_sens"));
2989*10934Ssommerfeld@sun.com 			}
2990*10934Ssommerfeld@sun.com 			savenl();
2991*10934Ssommerfeld@sun.com 			break;
29923055Sdanmcd 		default:
29933055Sdanmcd 			/* Skip over irrelevant extensions. */
29943055Sdanmcd 			break;
29953055Sdanmcd 		}
29963055Sdanmcd 		current += ext->sadb_ext_len;
29973055Sdanmcd 	}
29983055Sdanmcd 
29994064Smarkfen 	if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) {
30004064Smarkfen 		tidyup();
30014064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs"));
30024064Smarkfen 	}
30033055Sdanmcd }
30043055Sdanmcd 
30053055Sdanmcd /*
30063055Sdanmcd  * Open the output file for the "save" command.
30073055Sdanmcd  */
30083055Sdanmcd FILE *
30093055Sdanmcd opensavefile(char *filename)
30103055Sdanmcd {
30113055Sdanmcd 	int fd;
30123055Sdanmcd 	FILE *retval;
30133055Sdanmcd 	struct stat buf;
30143055Sdanmcd 
30153055Sdanmcd 	/*
30163055Sdanmcd 	 * If the user specifies "-" or doesn't give a filename, then
30173055Sdanmcd 	 * dump to stdout.  Make sure to document the dangers of files
30183055Sdanmcd 	 * that are NFS, directing your output to strange places, etc.
30193055Sdanmcd 	 */
30203055Sdanmcd 	if (filename == NULL || strcmp("-", filename) == 0)
30213055Sdanmcd 		return (stdout);
30223055Sdanmcd 
30233055Sdanmcd 	/*
30243055Sdanmcd 	 * open the file with the create bits set.  Since I check for
30253055Sdanmcd 	 * real UID == root in main(), I won't worry about the ownership
30263055Sdanmcd 	 * problem.
30273055Sdanmcd 	 */
30283055Sdanmcd 	fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR);
30293055Sdanmcd 	if (fd == -1) {
30303055Sdanmcd 		if (errno != EEXIST)
30314064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
30324064Smarkfen 			    "open error"),
30333055Sdanmcd 			    strerror(errno));
30343055Sdanmcd 		fd = open(filename, O_WRONLY | O_TRUNC, 0);
30353055Sdanmcd 		if (fd == -1)
30364064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
30374064Smarkfen 			    "open error"), strerror(errno));
30383055Sdanmcd 		if (fstat(fd, &buf) == -1) {
30393055Sdanmcd 			(void) close(fd);
30403055Sdanmcd 			bail_msg("%s fstat: %s", filename, strerror(errno));
30413055Sdanmcd 		}
30423055Sdanmcd 		if (S_ISREG(buf.st_mode) &&
30433055Sdanmcd 		    ((buf.st_mode & S_IAMB) != S_IRUSR)) {
30444064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
30454064Smarkfen 			    "WARNING: Save file already exists with "
30464064Smarkfen 			    "permission %o."), buf.st_mode & S_IAMB);
30474064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
30484064Smarkfen 			    "Normal users may be able to read IPsec "
30494064Smarkfen 			    "keying material."));
30503055Sdanmcd 		}
30513055Sdanmcd 	}
30523055Sdanmcd 
30533055Sdanmcd 	/* Okay, we have an FD.  Assign it to a stdio FILE pointer. */
30543055Sdanmcd 	retval = fdopen(fd, "w");
30553055Sdanmcd 	if (retval == NULL) {
30563055Sdanmcd 		(void) close(fd);
30574064Smarkfen 		bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
30584064Smarkfen 		    "fdopen error"), strerror(errno));
30593055Sdanmcd 	}
30603055Sdanmcd 	return (retval);
30613055Sdanmcd }
30623055Sdanmcd 
30633055Sdanmcd const char *
30643055Sdanmcd do_inet_ntop(const void *addr, char *cp, size_t size)
30653055Sdanmcd {
30663055Sdanmcd 	boolean_t isv4;
30673055Sdanmcd 	struct in6_addr *inaddr6 = (struct in6_addr *)addr;
30683055Sdanmcd 	struct in_addr inaddr;
30693055Sdanmcd 
30703055Sdanmcd 	if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) {
30713055Sdanmcd 		IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr);
30723055Sdanmcd 	}
30733055Sdanmcd 
30743055Sdanmcd 	return (inet_ntop(isv4 ? AF_INET : AF_INET6,
30753055Sdanmcd 	    isv4 ? (void *)&inaddr : inaddr6, cp, size));
30763055Sdanmcd }
30773055Sdanmcd 
30783055Sdanmcd char numprint[NBUF_SIZE];
30793055Sdanmcd 
30803055Sdanmcd /*
30813055Sdanmcd  * Parse and reverse parse a specific SA type (AH, ESP, etc.).
30823055Sdanmcd  */
30833055Sdanmcd static struct typetable {
30843055Sdanmcd 	char *type;
30853055Sdanmcd 	int token;
30863055Sdanmcd } type_table[] = {
30873055Sdanmcd 	{"all", SADB_SATYPE_UNSPEC},
30883055Sdanmcd 	{"ah",  SADB_SATYPE_AH},
30893055Sdanmcd 	{"esp", SADB_SATYPE_ESP},
30903055Sdanmcd 	/* PF_KEY NOTE:  More to come if net/pfkeyv2.h gets updated. */
30913055Sdanmcd 	{NULL, 0}	/* Token value is irrelevant for this entry. */
30923055Sdanmcd };
30933055Sdanmcd 
30943055Sdanmcd char *
30953055Sdanmcd rparsesatype(int type)
30963055Sdanmcd {
30973055Sdanmcd 	struct typetable *tt = type_table;
30983055Sdanmcd 
30993055Sdanmcd 	while (tt->type != NULL && type != tt->token)
31003055Sdanmcd 		tt++;
31013055Sdanmcd 
31023055Sdanmcd 	if (tt->type == NULL) {
31033055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", type);
31043055Sdanmcd 	} else {
31053055Sdanmcd 		return (tt->type);
31063055Sdanmcd 	}
31073055Sdanmcd 
31083055Sdanmcd 	return (numprint);
31093055Sdanmcd }
31103055Sdanmcd 
31113055Sdanmcd 
31123055Sdanmcd /*
31133055Sdanmcd  * Return a string containing the name of the specified numerical algorithm
31143055Sdanmcd  * identifier.
31153055Sdanmcd  */
31163055Sdanmcd char *
31173055Sdanmcd rparsealg(uint8_t alg, int proto_num)
31183055Sdanmcd {
31193055Sdanmcd 	static struct ipsecalgent *holder = NULL; /* we're single-threaded */
31203055Sdanmcd 
31213055Sdanmcd 	if (holder != NULL)
31223055Sdanmcd 		freeipsecalgent(holder);
31233055Sdanmcd 
31243055Sdanmcd 	holder = getipsecalgbynum(alg, proto_num, NULL);
31253055Sdanmcd 	if (holder == NULL) {
31263055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", alg);
31273055Sdanmcd 		return (numprint);
31283055Sdanmcd 	}
31293055Sdanmcd 
31303055Sdanmcd 	return (*(holder->a_names));
31313055Sdanmcd }
31323055Sdanmcd 
31333055Sdanmcd /*
31343055Sdanmcd  * Parse and reverse parse out a source/destination ID type.
31353055Sdanmcd  */
31363055Sdanmcd static struct idtypes {
31373055Sdanmcd 	char *idtype;
31383055Sdanmcd 	uint8_t retval;
31393055Sdanmcd } idtypes[] = {
31403055Sdanmcd 	{"prefix",	SADB_IDENTTYPE_PREFIX},
31413055Sdanmcd 	{"fqdn",	SADB_IDENTTYPE_FQDN},
31423055Sdanmcd 	{"domain",	SADB_IDENTTYPE_FQDN},
31433055Sdanmcd 	{"domainname",	SADB_IDENTTYPE_FQDN},
31443055Sdanmcd 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
31453055Sdanmcd 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
31463055Sdanmcd 	{"der_dn",	SADB_X_IDENTTYPE_DN},
31473055Sdanmcd 	{"der_gn",	SADB_X_IDENTTYPE_GN},
31483055Sdanmcd 	{NULL,		0}
31493055Sdanmcd };
31503055Sdanmcd 
31513055Sdanmcd char *
31523055Sdanmcd rparseidtype(uint16_t type)
31533055Sdanmcd {
31543055Sdanmcd 	struct idtypes *idp;
31553055Sdanmcd 
31563055Sdanmcd 	for (idp = idtypes; idp->idtype != NULL; idp++) {
31573055Sdanmcd 		if (type == idp->retval)
31583055Sdanmcd 			return (idp->idtype);
31593055Sdanmcd 	}
31603055Sdanmcd 
31613055Sdanmcd 	(void) snprintf(numprint, NBUF_SIZE, "%d", type);
31623055Sdanmcd 	return (numprint);
31633055Sdanmcd }
31644235Smarkfen 
31654235Smarkfen /*
31664235Smarkfen  * This is a general purpose exit function, calling functions can specify an
31674235Smarkfen  * error type. If the command calling this function was started by smf(5) the
31684235Smarkfen  * error type could be used as a hint to the restarter. In the future this
31694235Smarkfen  * function could be used to do something more intelligent with a process that
317010019SMark.Fenwick@Sun.COM  * encounters an error. If exit() is called with an error code other than those
317110019SMark.Fenwick@Sun.COM  * defined by smf(5), the program will just get restarted. Unless restarting
317210019SMark.Fenwick@Sun.COM  * is likely to resolve the error condition, its probably sensible to just
317310019SMark.Fenwick@Sun.COM  * log the error and keep running.
317410019SMark.Fenwick@Sun.COM  *
317510019SMark.Fenwick@Sun.COM  * The SERVICE_* exit_types mean nothing if the command was run from the
317610019SMark.Fenwick@Sun.COM  * command line, just exit(). There are two special cases:
317710019SMark.Fenwick@Sun.COM  *
317810019SMark.Fenwick@Sun.COM  * SERVICE_DEGRADE - Not implemented in smf(5), one day it could hint that
317910019SMark.Fenwick@Sun.COM  *                   the service is not running as well is it could. For
318010019SMark.Fenwick@Sun.COM  *                   now, don't do anything, just record the error.
318110019SMark.Fenwick@Sun.COM  * DEBUG_FATAL - Something happened, if the command was being run in debug
318210019SMark.Fenwick@Sun.COM  *               mode, exit() as you really want to know something happened,
318310019SMark.Fenwick@Sun.COM  *               otherwise just keep running. This is ignored when running
318410019SMark.Fenwick@Sun.COM  *               under smf(5).
31854235Smarkfen  *
31864235Smarkfen  * The function will handle an optional variable args error message, this
31874235Smarkfen  * will be written to the error stream, typically a log file or stderr.
31884235Smarkfen  */
31894235Smarkfen void
31904235Smarkfen ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...)
31914235Smarkfen {
31924235Smarkfen 	int exit_status;
31934235Smarkfen 	va_list args;
31944235Smarkfen 
31954235Smarkfen 	if (fp == NULL)
31964235Smarkfen 		fp = stderr;
31974235Smarkfen 	if (fmt != NULL) {
31984235Smarkfen 		va_start(args, fmt);
31994235Smarkfen 		vwarnxfp(fp, fmt, args);
32004235Smarkfen 		va_end(args);
32014235Smarkfen 	}
32024235Smarkfen 
32034235Smarkfen 	if (fmri == NULL) {
32044235Smarkfen 		/* Command being run directly from a shell. */
32054235Smarkfen 		switch (type) {
32064235Smarkfen 		case SERVICE_EXIT_OK:
32074235Smarkfen 			exit_status = 0;
32084235Smarkfen 			break;
32094235Smarkfen 		case SERVICE_DEGRADE:
32104235Smarkfen 			return;
32114235Smarkfen 			break;
32124235Smarkfen 		case SERVICE_BADPERM:
32134235Smarkfen 		case SERVICE_BADCONF:
32144235Smarkfen 		case SERVICE_MAINTAIN:
32154235Smarkfen 		case SERVICE_DISABLE:
32164235Smarkfen 		case SERVICE_FATAL:
32174235Smarkfen 		case SERVICE_RESTART:
321810019SMark.Fenwick@Sun.COM 		case DEBUG_FATAL:
32194235Smarkfen 			warnxfp(fp, "Fatal error - exiting.");
32204235Smarkfen 			exit_status = 1;
32214235Smarkfen 			break;
32224235Smarkfen 		}
32234235Smarkfen 	} else {
32244235Smarkfen 		/* Command being run as a smf(5) method. */
32254235Smarkfen 		switch (type) {
32264235Smarkfen 		case SERVICE_EXIT_OK:
32274235Smarkfen 			exit_status = SMF_EXIT_OK;
32284235Smarkfen 			break;
322910019SMark.Fenwick@Sun.COM 		case SERVICE_DEGRADE: /* Not implemented yet. */
323010019SMark.Fenwick@Sun.COM 		case DEBUG_FATAL:
323110019SMark.Fenwick@Sun.COM 			/* Keep running, don't exit(). */
32324235Smarkfen 			return;
32334235Smarkfen 			break;
32344235Smarkfen 		case SERVICE_BADPERM:
32354235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
32364235Smarkfen 			    "Permission error with %s."), fmri);
32374235Smarkfen 			exit_status = SMF_EXIT_ERR_PERM;
32384235Smarkfen 			break;
32394235Smarkfen 		case SERVICE_BADCONF:
32404235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
32414235Smarkfen 			    "Bad configuration of service %s."), fmri);
32424235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
32434235Smarkfen 			break;
32444235Smarkfen 		case SERVICE_MAINTAIN:
32454235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
32464235Smarkfen 			    "Service %s needs maintenance."), fmri);
32474235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
32484235Smarkfen 			break;
32494235Smarkfen 		case SERVICE_DISABLE:
32504235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
32514235Smarkfen 			break;
32524235Smarkfen 		case SERVICE_FATAL:
32534235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
32544235Smarkfen 			    "Service %s fatal error."), fmri);
32554235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
32564235Smarkfen 			break;
32574235Smarkfen 		case SERVICE_RESTART:
32584235Smarkfen 			exit_status = 1;
32594235Smarkfen 			break;
32604235Smarkfen 		}
32614235Smarkfen 	}
32624235Smarkfen 	(void) fflush(fp);
32634235Smarkfen 	(void) fclose(fp);
32644235Smarkfen 	exit(exit_status);
32654235Smarkfen }
3266