10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53055Sdanmcd  * Common Development and Distribution License (the "License").
63055Sdanmcd  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
224064Smarkfen  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <stdarg.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <sys/sysconf.h>
360Sstevel@tonic-gate #include <strings.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <sys/socket.h>
400Sstevel@tonic-gate #include <netdb.h>
410Sstevel@tonic-gate #include <netinet/in.h>
420Sstevel@tonic-gate #include <arpa/inet.h>
430Sstevel@tonic-gate #include <net/pfkeyv2.h>
440Sstevel@tonic-gate #include <net/pfpolicy.h>
450Sstevel@tonic-gate #include <libintl.h>
460Sstevel@tonic-gate #include <setjmp.h>
470Sstevel@tonic-gate #include <libgen.h>
484235Smarkfen #include <libscf.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include "ipsec_util.h"
510Sstevel@tonic-gate #include "ikedoor.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * This file contains support functions that are shared by the ipsec
550Sstevel@tonic-gate  * utilities including ipseckey(1m) and ikeadm(1m).
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /* Set standard default/initial values for globals... */
590Sstevel@tonic-gate boolean_t pflag = B_FALSE;	/* paranoid w.r.t. printing keying material */
600Sstevel@tonic-gate boolean_t nflag = B_FALSE;	/* avoid nameservice? */
610Sstevel@tonic-gate boolean_t interactive = B_FALSE;	/* util not running on cmdline */
620Sstevel@tonic-gate boolean_t readfile = B_FALSE;	/* cmds are being read from a file */
630Sstevel@tonic-gate uint_t	lineno = 0;		/* track location if reading cmds from file */
644235Smarkfen uint_t	lines_added = 0;
654235Smarkfen uint_t	lines_parsed = 0;
660Sstevel@tonic-gate jmp_buf	env;		/* for error recovery in interactive/readfile modes */
674235Smarkfen char *my_fmri = NULL;
684235Smarkfen FILE *debugfile = stderr;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * Print errno and exit if cmdline or readfile, reset state if interactive
724064Smarkfen  * The error string *what should be dgettext()'d before calling bail().
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate void
750Sstevel@tonic-gate bail(char *what)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	if (errno != 0)
780Sstevel@tonic-gate 		warn(what);
790Sstevel@tonic-gate 	else
804235Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "Error: %s"), what);
810Sstevel@tonic-gate 	if (readfile) {
824235Smarkfen 		return;
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	if (interactive && !readfile)
850Sstevel@tonic-gate 		longjmp(env, 2);
864235Smarkfen 	EXIT_FATAL(NULL);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * Print caller-supplied variable-arg error msg, then exit if cmdline or
910Sstevel@tonic-gate  * readfile, or reset state if interactive.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate /*PRINTFLIKE1*/
940Sstevel@tonic-gate void
950Sstevel@tonic-gate bail_msg(char *fmt, ...)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	va_list	ap;
980Sstevel@tonic-gate 	char	msgbuf[BUFSIZ];
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	va_start(ap, fmt);
1010Sstevel@tonic-gate 	(void) vsnprintf(msgbuf, BUFSIZ, fmt, ap);
1020Sstevel@tonic-gate 	va_end(ap);
1030Sstevel@tonic-gate 	if (readfile)
1044064Smarkfen 		warnx(dgettext(TEXT_DOMAIN,
1054064Smarkfen 		    "ERROR on line %u:\n%s\n"), lineno,  msgbuf);
1060Sstevel@tonic-gate 	else
1074064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "ERROR: %s\n"), msgbuf);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (interactive && !readfile)
1100Sstevel@tonic-gate 		longjmp(env, 1);
1110Sstevel@tonic-gate 
1124235Smarkfen 	EXIT_FATAL(NULL);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * dump_XXX functions produce ASCII output from various structures.
1180Sstevel@tonic-gate  *
1190Sstevel@tonic-gate  * Because certain errors need to do this to stderr, dump_XXX functions
1200Sstevel@tonic-gate  * take a FILE pointer.
1210Sstevel@tonic-gate  *
1220Sstevel@tonic-gate  * If an error occured while writing to the specified file, these
1230Sstevel@tonic-gate  * functions return -1, zero otherwise.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate int
1273055Sdanmcd dump_sockaddr(struct sockaddr *sa, uint8_t prefixlen, boolean_t addr_only,
1283055Sdanmcd     FILE *where)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	struct sockaddr_in	*sin;
1310Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
1320Sstevel@tonic-gate 	char			*printable_addr, *protocol;
1330Sstevel@tonic-gate 	uint8_t			*addrptr;
1343055Sdanmcd 	/* Add 4 chars to hold '/nnn' for prefixes. */
1353055Sdanmcd 	char			storage[INET6_ADDRSTRLEN + 4];
1360Sstevel@tonic-gate 	uint16_t		port;
1370Sstevel@tonic-gate 	boolean_t		unspec;
1380Sstevel@tonic-gate 	struct hostent		*hp;
1390Sstevel@tonic-gate 	int			getipnode_errno, addrlen;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	switch (sa->sa_family) {
1420Sstevel@tonic-gate 	case AF_INET:
1430Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1440Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sa;
1450Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin->sin_addr;
1460Sstevel@tonic-gate 		port = sin->sin_port;
1470Sstevel@tonic-gate 		protocol = "AF_INET";
1480Sstevel@tonic-gate 		unspec = (sin->sin_addr.s_addr == 0);
1490Sstevel@tonic-gate 		addrlen = sizeof (sin->sin_addr);
1500Sstevel@tonic-gate 		break;
1510Sstevel@tonic-gate 	case AF_INET6:
1520Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1530Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sa;
1540Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin6->sin6_addr;
1550Sstevel@tonic-gate 		port = sin6->sin6_port;
1560Sstevel@tonic-gate 		protocol = "AF_INET6";
1570Sstevel@tonic-gate 		unspec = IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr);
1580Sstevel@tonic-gate 		addrlen = sizeof (sin6->sin6_addr);
1590Sstevel@tonic-gate 		break;
1600Sstevel@tonic-gate 	default:
1610Sstevel@tonic-gate 		return (0);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (inet_ntop(sa->sa_family, addrptr, storage, INET6_ADDRSTRLEN) ==
1650Sstevel@tonic-gate 	    NULL) {
1664064Smarkfen 		printable_addr = dgettext(TEXT_DOMAIN, "Invalid IP address.");
1670Sstevel@tonic-gate 	} else {
1683055Sdanmcd 		char prefix[5];	/* "/nnn" with terminator. */
1693055Sdanmcd 
1703055Sdanmcd 		(void) snprintf(prefix, sizeof (prefix), "/%d", prefixlen);
1710Sstevel@tonic-gate 		printable_addr = storage;
1723055Sdanmcd 		if (prefixlen != 0) {
1733055Sdanmcd 			(void) strlcat(printable_addr, prefix,
1743055Sdanmcd 			    sizeof (storage));
1753055Sdanmcd 		}
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 	if (addr_only) {
1780Sstevel@tonic-gate 		if (fprintf(where, "%s", printable_addr) < 0)
1790Sstevel@tonic-gate 			return (-1);
1800Sstevel@tonic-gate 	} else {
1814064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
1824064Smarkfen 		    "%s: port %d, %s"), protocol,
1830Sstevel@tonic-gate 		    ntohs(port), printable_addr) < 0)
1840Sstevel@tonic-gate 			return (-1);
1850Sstevel@tonic-gate 		if (!nflag) {
1860Sstevel@tonic-gate 			/*
1870Sstevel@tonic-gate 			 * Do AF_independent reverse hostname lookup here.
1880Sstevel@tonic-gate 			 */
1890Sstevel@tonic-gate 			if (unspec) {
1900Sstevel@tonic-gate 				if (fprintf(where,
1914064Smarkfen 				    dgettext(TEXT_DOMAIN,
1924064Smarkfen 				    " <unspecified>")) < 0)
1930Sstevel@tonic-gate 					return (-1);
1940Sstevel@tonic-gate 			} else {
1950Sstevel@tonic-gate 				hp = getipnodebyaddr((char *)addrptr, addrlen,
1960Sstevel@tonic-gate 				    sa->sa_family, &getipnode_errno);
1970Sstevel@tonic-gate 				if (hp != NULL) {
1980Sstevel@tonic-gate 					if (fprintf(where,
1990Sstevel@tonic-gate 					    " (%s)", hp->h_name) < 0)
2000Sstevel@tonic-gate 						return (-1);
2010Sstevel@tonic-gate 					freehostent(hp);
2020Sstevel@tonic-gate 				} else {
2030Sstevel@tonic-gate 					if (fprintf(where,
2044064Smarkfen 					    dgettext(TEXT_DOMAIN,
2054064Smarkfen 					    " <unknown>")) < 0)
2060Sstevel@tonic-gate 						return (-1);
2070Sstevel@tonic-gate 				}
2080Sstevel@tonic-gate 			}
2090Sstevel@tonic-gate 		}
2100Sstevel@tonic-gate 		if (fputs(".\n", where) == EOF)
2110Sstevel@tonic-gate 			return (-1);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 	return (0);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate  * Dump a key and bitlen
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate int
2200Sstevel@tonic-gate dump_key(uint8_t *keyp, uint_t bitlen, FILE *where)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	int	numbytes;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	numbytes = SADB_1TO8(bitlen);
2250Sstevel@tonic-gate 	/* The & 0x7 is to check for leftover bits. */
2260Sstevel@tonic-gate 	if ((bitlen & 0x7) != 0)
2270Sstevel@tonic-gate 		numbytes++;
2280Sstevel@tonic-gate 	while (numbytes-- != 0) {
2290Sstevel@tonic-gate 		if (pflag) {
2300Sstevel@tonic-gate 			/* Print no keys if paranoid */
2310Sstevel@tonic-gate 			if (fprintf(where, "XX") < 0)
2320Sstevel@tonic-gate 				return (-1);
2330Sstevel@tonic-gate 		} else {
2340Sstevel@tonic-gate 			if (fprintf(where, "%02x", *keyp++) < 0)
2350Sstevel@tonic-gate 				return (-1);
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	if (fprintf(where, "/%u", bitlen) < 0)
2390Sstevel@tonic-gate 		return (-1);
2400Sstevel@tonic-gate 	return (0);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * Print an authentication or encryption algorithm
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate static int
2470Sstevel@tonic-gate dump_generic_alg(uint8_t alg_num, int proto_num, FILE *where)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	struct ipsecalgent *alg;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_num, proto_num, NULL);
2520Sstevel@tonic-gate 	if (alg == NULL) {
2534064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
2544064Smarkfen 		    "<unknown %u>"), alg_num) < 0)
2550Sstevel@tonic-gate 			return (-1);
2560Sstevel@tonic-gate 		return (0);
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/*
2600Sstevel@tonic-gate 	 * Special-case <none> for backward output compat.
2610Sstevel@tonic-gate 	 * Assume that SADB_AALG_NONE == SADB_EALG_NONE.
2620Sstevel@tonic-gate 	 */
2630Sstevel@tonic-gate 	if (alg_num == SADB_AALG_NONE) {
2644064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
2654064Smarkfen 		    "<none>"), where) == EOF)
2660Sstevel@tonic-gate 			return (-1);
2670Sstevel@tonic-gate 	} else {
2680Sstevel@tonic-gate 		if (fputs(alg->a_names[0], where) == EOF)
2690Sstevel@tonic-gate 			return (-1);
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	freeipsecalgent(alg);
2730Sstevel@tonic-gate 	return (0);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate int
2770Sstevel@tonic-gate dump_aalg(uint8_t aalg, FILE *where)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	return (dump_generic_alg(aalg, IPSEC_PROTO_AH, where));
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate int
2830Sstevel@tonic-gate dump_ealg(uint8_t ealg, FILE *where)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	return (dump_generic_alg(ealg, IPSEC_PROTO_ESP, where));
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * Print an SADB_IDENTTYPE string
2900Sstevel@tonic-gate  *
2910Sstevel@tonic-gate  * Also return TRUE if the actual ident may be printed, FALSE if not.
2920Sstevel@tonic-gate  *
2930Sstevel@tonic-gate  * If rc is not NULL, set its value to -1 if an error occured while writing
2940Sstevel@tonic-gate  * to the specified file, zero otherwise.
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate boolean_t
2970Sstevel@tonic-gate dump_sadb_idtype(uint8_t idtype, FILE *where, int *rc)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	boolean_t canprint = B_TRUE;
3000Sstevel@tonic-gate 	int rc_val = 0;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	switch (idtype) {
3030Sstevel@tonic-gate 	case SADB_IDENTTYPE_PREFIX:
3044064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "prefix"), where) == EOF)
3050Sstevel@tonic-gate 			rc_val = -1;
3060Sstevel@tonic-gate 		break;
3070Sstevel@tonic-gate 	case SADB_IDENTTYPE_FQDN:
3084064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "FQDN"), where) == EOF)
3090Sstevel@tonic-gate 			rc_val = -1;
3100Sstevel@tonic-gate 		break;
3110Sstevel@tonic-gate 	case SADB_IDENTTYPE_USER_FQDN:
3124064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
3134064Smarkfen 		    "user-FQDN (mbox)"), where) == EOF)
3140Sstevel@tonic-gate 			rc_val = -1;
3150Sstevel@tonic-gate 		break;
3160Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_DN:
3174064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Distinguished Name"),
3180Sstevel@tonic-gate 		    where) == EOF)
3190Sstevel@tonic-gate 			rc_val = -1;
3200Sstevel@tonic-gate 		canprint = B_FALSE;
3210Sstevel@tonic-gate 		break;
3220Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_GN:
3234064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Generic Name"),
3244064Smarkfen 		    where) == EOF)
3250Sstevel@tonic-gate 			rc_val = -1;
3260Sstevel@tonic-gate 		canprint = B_FALSE;
3270Sstevel@tonic-gate 		break;
3280Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_KEY_ID:
3294064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Generic key id"),
3304064Smarkfen 		    where) == EOF)
3310Sstevel@tonic-gate 			rc_val = -1;
3320Sstevel@tonic-gate 		break;
3330Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_ADDR_RANGE:
3344064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Address range"), where) == EOF)
3350Sstevel@tonic-gate 			rc_val = -1;
3360Sstevel@tonic-gate 		break;
3370Sstevel@tonic-gate 	default:
3384064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
3394064Smarkfen 		    "<unknown %u>"), idtype) < 0)
3400Sstevel@tonic-gate 			rc_val = -1;
3410Sstevel@tonic-gate 		break;
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (rc != NULL)
3450Sstevel@tonic-gate 		*rc = rc_val;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	return (canprint);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate  * Slice an argv/argc vector from an interactive line or a read-file line.
3520Sstevel@tonic-gate  */
3530Sstevel@tonic-gate static int
3540Sstevel@tonic-gate create_argv(char *ibuf, int *newargc, char ***thisargv)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate 	unsigned int argvlen = START_ARG;
3570Sstevel@tonic-gate 	char **current;
3580Sstevel@tonic-gate 	boolean_t firstchar = B_TRUE;
3590Sstevel@tonic-gate 	boolean_t inquotes = B_FALSE;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	*thisargv = malloc(sizeof (char *) * argvlen);
3620Sstevel@tonic-gate 	if ((*thisargv) == NULL)
3630Sstevel@tonic-gate 		return (MEMORY_ALLOCATION);
3640Sstevel@tonic-gate 	current = *thisargv;
3650Sstevel@tonic-gate 	*current = NULL;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	for (; *ibuf != '\0'; ibuf++) {
3680Sstevel@tonic-gate 		if (isspace(*ibuf)) {
3690Sstevel@tonic-gate 			if (inquotes) {
3700Sstevel@tonic-gate 				continue;
3710Sstevel@tonic-gate 			}
3720Sstevel@tonic-gate 			if (*current != NULL) {
3730Sstevel@tonic-gate 				*ibuf = '\0';
3740Sstevel@tonic-gate 				current++;
3750Sstevel@tonic-gate 				if (*thisargv + argvlen == current) {
3760Sstevel@tonic-gate 					/* Regrow ***thisargv. */
3770Sstevel@tonic-gate 					if (argvlen == TOO_MANY_ARGS) {
3780Sstevel@tonic-gate 						free(*thisargv);
3790Sstevel@tonic-gate 						return (TOO_MANY_TOKENS);
3800Sstevel@tonic-gate 					}
3810Sstevel@tonic-gate 					/* Double the allocation. */
3820Sstevel@tonic-gate 					current = realloc(*thisargv,
3830Sstevel@tonic-gate 					    sizeof (char *) * (argvlen << 1));
3840Sstevel@tonic-gate 					if (current == NULL) {
3850Sstevel@tonic-gate 						free(*thisargv);
3860Sstevel@tonic-gate 						return (MEMORY_ALLOCATION);
3870Sstevel@tonic-gate 					}
3880Sstevel@tonic-gate 					*thisargv = current;
3890Sstevel@tonic-gate 					current += argvlen;
3900Sstevel@tonic-gate 					argvlen <<= 1;	/* Double the size. */
3910Sstevel@tonic-gate 				}
3920Sstevel@tonic-gate 				*current = NULL;
3930Sstevel@tonic-gate 			}
3940Sstevel@tonic-gate 		} else {
3950Sstevel@tonic-gate 			if (firstchar) {
3960Sstevel@tonic-gate 				firstchar = B_FALSE;
3974235Smarkfen 				if (*ibuf == COMMENT_CHAR || *ibuf == '\n') {
3980Sstevel@tonic-gate 					free(*thisargv);
3990Sstevel@tonic-gate 					return (COMMENT_LINE);
4000Sstevel@tonic-gate 				}
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 			if (*ibuf == QUOTE_CHAR) {
4030Sstevel@tonic-gate 				if (inquotes) {
4040Sstevel@tonic-gate 					inquotes = B_FALSE;
4050Sstevel@tonic-gate 					*ibuf = '\0';
4060Sstevel@tonic-gate 				} else {
4070Sstevel@tonic-gate 					inquotes = B_TRUE;
4080Sstevel@tonic-gate 				}
4090Sstevel@tonic-gate 				continue;
4100Sstevel@tonic-gate 			}
4110Sstevel@tonic-gate 			if (*current == NULL) {
4120Sstevel@tonic-gate 				*current = ibuf;
4130Sstevel@tonic-gate 				(*newargc)++;
4140Sstevel@tonic-gate 			}
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * Tricky corner case...
4200Sstevel@tonic-gate 	 * I've parsed _exactly_ the amount of args as I have space.  It
4210Sstevel@tonic-gate 	 * won't return NULL-terminated, and bad things will happen to
4220Sstevel@tonic-gate 	 * the caller.
4230Sstevel@tonic-gate 	 */
4240Sstevel@tonic-gate 	if (argvlen == *newargc) {
4250Sstevel@tonic-gate 		current = realloc(*thisargv, sizeof (char *) * (argvlen + 1));
4260Sstevel@tonic-gate 		if (current == NULL) {
4270Sstevel@tonic-gate 			free(*thisargv);
4280Sstevel@tonic-gate 			return (MEMORY_ALLOCATION);
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 		*thisargv = current;
4310Sstevel@tonic-gate 		current[argvlen] = NULL;
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	return (SUCCESS);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate  * Enter a mode where commands are read from a file.  Treat stdin special.
4390Sstevel@tonic-gate  */
4400Sstevel@tonic-gate void
4414235Smarkfen do_interactive(FILE *infile, char *configfile, char *promptstring,
4424235Smarkfen     char *my_fmri, parse_cmdln_fn parseit)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate 	char		ibuf[IBUF_SIZE], holder[IBUF_SIZE];
4454235Smarkfen 	char		*hptr, **thisargv, *ebuf;
4460Sstevel@tonic-gate 	int		thisargc;
4470Sstevel@tonic-gate 	boolean_t	continue_in_progress = B_FALSE;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	(void) setjmp(env);
4500Sstevel@tonic-gate 
4514235Smarkfen 	ebuf = NULL;
4520Sstevel@tonic-gate 	interactive = B_TRUE;
4530Sstevel@tonic-gate 	bzero(ibuf, IBUF_SIZE);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (infile == stdin) {
4560Sstevel@tonic-gate 		(void) printf("%s", promptstring);
4570Sstevel@tonic-gate 		(void) fflush(stdout);
4580Sstevel@tonic-gate 	} else {
4590Sstevel@tonic-gate 		readfile = B_TRUE;
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	while (fgets(ibuf, IBUF_SIZE, infile) != NULL) {
4630Sstevel@tonic-gate 		if (readfile)
4640Sstevel@tonic-gate 			lineno++;
4650Sstevel@tonic-gate 		thisargc = 0;
4660Sstevel@tonic-gate 		thisargv = NULL;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		/*
4690Sstevel@tonic-gate 		 * Check byte IBUF_SIZE - 2, because byte IBUF_SIZE - 1 will
4700Sstevel@tonic-gate 		 * be null-terminated because of fgets().
4710Sstevel@tonic-gate 		 */
4720Sstevel@tonic-gate 		if (ibuf[IBUF_SIZE - 2] != '\0') {
4734235Smarkfen 			ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile,
4744235Smarkfen 			    dgettext(TEXT_DOMAIN, "Line %d too big."), lineno);
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		if (!continue_in_progress) {
4780Sstevel@tonic-gate 			/* Use -2 because of \n from fgets. */
4790Sstevel@tonic-gate 			if (ibuf[strlen(ibuf) - 2] == CONT_CHAR) {
4800Sstevel@tonic-gate 				/*
4810Sstevel@tonic-gate 				 * Can use strcpy here, I've checked the
4820Sstevel@tonic-gate 				 * length already.
4830Sstevel@tonic-gate 				 */
4840Sstevel@tonic-gate 				(void) strcpy(holder, ibuf);
4850Sstevel@tonic-gate 				hptr = &(holder[strlen(holder)]);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
4880Sstevel@tonic-gate 				hptr[-2] = ' ';
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 				continue_in_progress = B_TRUE;
4910Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
4920Sstevel@tonic-gate 				continue;
4930Sstevel@tonic-gate 			}
4940Sstevel@tonic-gate 		} else {
4950Sstevel@tonic-gate 			/* Handle continuations... */
4960Sstevel@tonic-gate 			(void) strncpy(hptr, ibuf,
4970Sstevel@tonic-gate 			    (size_t)(&(holder[IBUF_SIZE]) - hptr));
4980Sstevel@tonic-gate 			if (holder[IBUF_SIZE - 1] != '\0') {
4994235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
5004235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
5014235Smarkfen 				    "Command buffer overrun."));
5020Sstevel@tonic-gate 			}
5030Sstevel@tonic-gate 			/* Use - 2 because of \n from fgets. */
5040Sstevel@tonic-gate 			if (hptr[strlen(hptr) - 2] == CONT_CHAR) {
5050Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
5060Sstevel@tonic-gate 				hptr += strlen(hptr);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
5090Sstevel@tonic-gate 				hptr[-2] = ' ';
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 				continue;
5120Sstevel@tonic-gate 			} else {
5130Sstevel@tonic-gate 				continue_in_progress = B_FALSE;
5140Sstevel@tonic-gate 				/*
5150Sstevel@tonic-gate 				 * I've already checked the length...
5160Sstevel@tonic-gate 				 */
5170Sstevel@tonic-gate 				(void) strcpy(ibuf, holder);
5180Sstevel@tonic-gate 			}
5190Sstevel@tonic-gate 		}
5200Sstevel@tonic-gate 
5214235Smarkfen 		/*
5224235Smarkfen 		 * Just in case the command fails keep a copy of the
5234235Smarkfen 		 * command buffer for diagnostic output.
5244235Smarkfen 		 */
5254235Smarkfen 		if (readfile) {
5264235Smarkfen 			/*
5274235Smarkfen 			 * The error buffer needs to be big enough to
5284235Smarkfen 			 * hold the longest command string, plus
5294235Smarkfen 			 * some extra text, see below.
5304235Smarkfen 			 */
5314235Smarkfen 			ebuf = calloc((IBUF_SIZE * 2), sizeof (char));
5324235Smarkfen 			if (ebuf == NULL) {
5334235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
5344235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
5354235Smarkfen 				    "Memory allocation error."));
5364235Smarkfen 			} else {
5374235Smarkfen 				(void) snprintf(ebuf, (IBUF_SIZE * 2),
5384235Smarkfen 				    dgettext(TEXT_DOMAIN,
5394235Smarkfen 				    "Config file entry near line %u "
5404235Smarkfen 				    "caused error(s) or warnings:\n\n%s\n\n"),
5414235Smarkfen 				    lineno, ibuf);
5424235Smarkfen 			}
5434235Smarkfen 		}
5444235Smarkfen 
5450Sstevel@tonic-gate 		switch (create_argv(ibuf, &thisargc, &thisargv)) {
5460Sstevel@tonic-gate 		case TOO_MANY_TOKENS:
5474235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
5484235Smarkfen 			    dgettext(TEXT_DOMAIN, "Too many input tokens."));
5490Sstevel@tonic-gate 			break;
5500Sstevel@tonic-gate 		case MEMORY_ALLOCATION:
5514235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
5524235Smarkfen 			    dgettext(TEXT_DOMAIN, "Memory allocation error."));
5530Sstevel@tonic-gate 			break;
5540Sstevel@tonic-gate 		case COMMENT_LINE:
5550Sstevel@tonic-gate 			/* Comment line. */
5564235Smarkfen 			free(ebuf);
5570Sstevel@tonic-gate 			break;
5580Sstevel@tonic-gate 		default:
5594235Smarkfen 			if (thisargc != 0) {
5604235Smarkfen 				lines_parsed++;
5614235Smarkfen 				/* ebuf consumed */
5624342Spwernau 				parseit(thisargc, thisargv, ebuf, readfile);
5634235Smarkfen 			} else {
5644235Smarkfen 				free(ebuf);
5654235Smarkfen 			}
5660Sstevel@tonic-gate 			free(thisargv);
5670Sstevel@tonic-gate 			if (infile == stdin) {
5680Sstevel@tonic-gate 				(void) printf("%s", promptstring);
5690Sstevel@tonic-gate 				(void) fflush(stdout);
5700Sstevel@tonic-gate 			}
5710Sstevel@tonic-gate 			break;
5720Sstevel@tonic-gate 		}
5730Sstevel@tonic-gate 		bzero(ibuf, IBUF_SIZE);
5740Sstevel@tonic-gate 	}
5754342Spwernau 
5764342Spwernau 	/*
5774342Spwernau 	 * The following code is ipseckey specific. This should never be
5784342Spwernau 	 * used by ikeadm which also calls this function because ikeadm
5794342Spwernau 	 * only runs interactively. If this ever changes this code block
5804342Spwernau 	 * sould be revisited.
5814342Spwernau 	 */
5824342Spwernau 	if (readfile) {
5834342Spwernau 		if (lines_parsed != 0 && lines_added == 0) {
5844342Spwernau 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
5854342Spwernau 			    dgettext(TEXT_DOMAIN, "Configuration file did not "
5864342Spwernau 			    "contain any valid SAs"));
5874342Spwernau 		}
5884342Spwernau 
5894342Spwernau 		/*
5904342Spwernau 		 * There were errors. Putting the service in maintenance mode.
5914342Spwernau 		 * When svc.startd(1M) allows services to degrade themselves,
5924342Spwernau 		 * this should be revisited.
5934342Spwernau 		 *
5944342Spwernau 		 * If this function was called from a program running as a
5954342Spwernau 		 * smf_method(5), print a warning message. Don't spew out the
5964342Spwernau 		 * errors as these will end up in the smf(5) log file which is
5974342Spwernau 		 * publically readable, the errors may contain sensitive
5984342Spwernau 		 * information.
5994342Spwernau 		 */
6004342Spwernau 		if ((lines_added < lines_parsed) && (configfile != NULL)) {
6014342Spwernau 			if (my_fmri != NULL) {
6024342Spwernau 				ipsecutil_exit(SERVICE_BADCONF, my_fmri,
6034342Spwernau 				    debugfile, dgettext(TEXT_DOMAIN,
6044342Spwernau 				    "The configuration file contained %d "
6054342Spwernau 				    "errors.\n"
6064342Spwernau 				    "Manually check the configuration with:\n"
6074342Spwernau 				    "ipseckey -c %s\n"
6084342Spwernau 				    "Use svcadm(1M) to clear maintenance "
6094342Spwernau 				    "condition when errors are resolved.\n"),
6104342Spwernau 				    lines_parsed - lines_added, configfile);
6114342Spwernau 			} else {
6124342Spwernau 				EXIT_BADCONFIG(NULL);
6134342Spwernau 			}
6144342Spwernau 		} else {
6154342Spwernau 			if (my_fmri != NULL)
6164342Spwernau 				ipsecutil_exit(SERVICE_EXIT_OK, my_fmri,
6174342Spwernau 				    debugfile, dgettext(TEXT_DOMAIN,
6184342Spwernau 				    "%d actions successfully processed."),
6194342Spwernau 				    lines_added);
6204342Spwernau 		}
6214342Spwernau 	} else {
6220Sstevel@tonic-gate 		(void) putchar('\n');
6230Sstevel@tonic-gate 		(void) fflush(stdout);
6240Sstevel@tonic-gate 	}
6254235Smarkfen 	EXIT_OK(NULL);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate /*
6290Sstevel@tonic-gate  * Functions to parse strings that represent a debug or privilege level.
6300Sstevel@tonic-gate  * These functions are copied from main.c and door.c in usr.lib/in.iked/common.
6310Sstevel@tonic-gate  * If this file evolves into a common library that may be used by in.iked
6320Sstevel@tonic-gate  * as well as the usr.sbin utilities, those duplicate functions should be
6330Sstevel@tonic-gate  * deleted.
6340Sstevel@tonic-gate  *
6350Sstevel@tonic-gate  * A privilege level may be represented by a simple keyword, corresponding
6360Sstevel@tonic-gate  * to one of the possible levels.  A debug level may be represented by a
6370Sstevel@tonic-gate  * series of keywords, separated by '+' or '-', indicating categories to
6380Sstevel@tonic-gate  * be added or removed from the set of categories in the debug level.
6390Sstevel@tonic-gate  * For example, +all-op corresponds to level 0xfffffffb (all flags except
6400Sstevel@tonic-gate  * for D_OP set); while p1+p2+pfkey corresponds to level 0x38.  Note that
6410Sstevel@tonic-gate  * the leading '+' is implicit; the first keyword in the list must be for
6420Sstevel@tonic-gate  * a category that is to be added.
6430Sstevel@tonic-gate  *
6440Sstevel@tonic-gate  * These parsing functions make use of a local version of strtok, strtok_d,
6450Sstevel@tonic-gate  * which includes an additional parameter, char *delim.  This param is filled
6460Sstevel@tonic-gate  * in with the character which ends the returned token.  In other words,
6470Sstevel@tonic-gate  * this version of strtok, in addition to returning the token, also returns
6480Sstevel@tonic-gate  * the single character delimiter from the original string which marked the
6490Sstevel@tonic-gate  * end of the token.
6500Sstevel@tonic-gate  */
6510Sstevel@tonic-gate static char *
6520Sstevel@tonic-gate strtok_d(char *string, const char *sepset, char *delim)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate 	static char	*lasts;
6550Sstevel@tonic-gate 	char		*q, *r;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	/* first or subsequent call */
6580Sstevel@tonic-gate 	if (string == NULL)
6590Sstevel@tonic-gate 		string = lasts;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	if (string == 0)		/* return if no tokens remaining */
6620Sstevel@tonic-gate 		return (NULL);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	q = string + strspn(string, sepset);	/* skip leading separators */
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	if (*q == '\0')			/* return if no tokens remaining */
6670Sstevel@tonic-gate 		return (NULL);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	if ((r = strpbrk(q, sepset)) == NULL) {		/* move past token */
6700Sstevel@tonic-gate 		lasts = 0;	/* indicate that this is last token */
6710Sstevel@tonic-gate 	} else {
6720Sstevel@tonic-gate 		*delim = *r;	/* save delimitor */
6730Sstevel@tonic-gate 		*r = '\0';
6740Sstevel@tonic-gate 		lasts = r + 1;
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 	return (q);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate static keywdtab_t	privtab[] = {
6800Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"base" },
6810Sstevel@tonic-gate 	{ IKE_PRIV_MODKEYS,	"modkeys" },
6820Sstevel@tonic-gate 	{ IKE_PRIV_KEYMAT,	"keymat" },
6830Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"0" },
6840Sstevel@tonic-gate };
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate int
6870Sstevel@tonic-gate privstr2num(char *str)
6880Sstevel@tonic-gate {
6890Sstevel@tonic-gate 	keywdtab_t	*pp;
6900Sstevel@tonic-gate 	char		*endp;
6910Sstevel@tonic-gate 	int		 priv;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	for (pp = privtab; pp < A_END(privtab); pp++) {
6940Sstevel@tonic-gate 		if (strcasecmp(str, pp->kw_str) == 0)
6950Sstevel@tonic-gate 			return (pp->kw_tag);
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	priv = strtol(str, &endp, 0);
6990Sstevel@tonic-gate 	if (*endp == '\0')
7000Sstevel@tonic-gate 		return (priv);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	return (-1);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate static keywdtab_t	dbgtab[] = {
7060Sstevel@tonic-gate 	{ D_CERT,	"cert" },
7070Sstevel@tonic-gate 	{ D_KEY,	"key" },
7080Sstevel@tonic-gate 	{ D_OP,		"op" },
7090Sstevel@tonic-gate 	{ D_P1,		"p1" },
7100Sstevel@tonic-gate 	{ D_P1,		"phase1" },
7110Sstevel@tonic-gate 	{ D_P2,		"p2" },
7120Sstevel@tonic-gate 	{ D_P2,		"phase2" },
7130Sstevel@tonic-gate 	{ D_PFKEY,	"pfkey" },
7140Sstevel@tonic-gate 	{ D_POL,	"pol" },
7150Sstevel@tonic-gate 	{ D_POL,	"policy" },
7160Sstevel@tonic-gate 	{ D_PROP,	"prop" },
7170Sstevel@tonic-gate 	{ D_DOOR,	"door" },
7180Sstevel@tonic-gate 	{ D_CONFIG,	"config" },
7190Sstevel@tonic-gate 	{ D_ALL,	"all" },
7200Sstevel@tonic-gate 	{ 0,		"0" },
7210Sstevel@tonic-gate };
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate int
7240Sstevel@tonic-gate dbgstr2num(char *str)
7250Sstevel@tonic-gate {
7260Sstevel@tonic-gate 	keywdtab_t	*dp;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	for (dp = dbgtab; dp < A_END(dbgtab); dp++) {
7290Sstevel@tonic-gate 		if (strcasecmp(str, dp->kw_str) == 0)
7300Sstevel@tonic-gate 			return (dp->kw_tag);
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 	return (D_INVALID);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate int
7360Sstevel@tonic-gate parsedbgopts(char *optarg)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate 	char	*argp, *endp, op, nextop;
7390Sstevel@tonic-gate 	int	mask = 0, new;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	mask = strtol(optarg, &endp, 0);
7420Sstevel@tonic-gate 	if (*endp == '\0')
7430Sstevel@tonic-gate 		return (mask);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	op = optarg[0];
7460Sstevel@tonic-gate 	if (op != '-')
7470Sstevel@tonic-gate 		op = '+';
7480Sstevel@tonic-gate 	argp = strtok_d(optarg, "+-", &nextop);
7490Sstevel@tonic-gate 	do {
7500Sstevel@tonic-gate 		new = dbgstr2num(argp);
7510Sstevel@tonic-gate 		if (new == D_INVALID) {
7520Sstevel@tonic-gate 			/* we encountered an invalid keywd */
7530Sstevel@tonic-gate 			return (new);
7540Sstevel@tonic-gate 		}
7550Sstevel@tonic-gate 		if (op == '+') {
7560Sstevel@tonic-gate 			mask |= new;
7570Sstevel@tonic-gate 		} else {
7580Sstevel@tonic-gate 			mask &= ~new;
7590Sstevel@tonic-gate 		}
7600Sstevel@tonic-gate 		op = nextop;
7610Sstevel@tonic-gate 	} while ((argp = strtok_d(NULL, "+-", &nextop)) != NULL);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	return (mask);
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate /*
7680Sstevel@tonic-gate  * functions to manipulate the kmcookie-label mapping file
7690Sstevel@tonic-gate  */
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate /*
7720Sstevel@tonic-gate  * Open, lockf, fdopen the given file, returning a FILE * on success,
7730Sstevel@tonic-gate  * or NULL on failure.
7740Sstevel@tonic-gate  */
7750Sstevel@tonic-gate FILE *
7760Sstevel@tonic-gate kmc_open_and_lock(char *name)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	int	fd, rtnerr;
7790Sstevel@tonic-gate 	FILE	*fp;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if ((fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
7820Sstevel@tonic-gate 		return (NULL);
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 	if (lockf(fd, F_LOCK, 0) < 0) {
7850Sstevel@tonic-gate 		return (NULL);
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 	if ((fp = fdopen(fd, "a+")) == NULL) {
7880Sstevel@tonic-gate 		return (NULL);
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 	if (fseek(fp, 0, SEEK_SET) < 0) {
7910Sstevel@tonic-gate 		/* save errno in case fclose changes it */
7920Sstevel@tonic-gate 		rtnerr = errno;
7930Sstevel@tonic-gate 		(void) fclose(fp);
7940Sstevel@tonic-gate 		errno = rtnerr;
7950Sstevel@tonic-gate 		return (NULL);
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate 	return (fp);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate /*
8010Sstevel@tonic-gate  * Extract an integer cookie and string label from a line from the
8020Sstevel@tonic-gate  * kmcookie-label file.  Return -1 on failure, 0 on success.
8030Sstevel@tonic-gate  */
8040Sstevel@tonic-gate int
8050Sstevel@tonic-gate kmc_parse_line(char *line, int *cookie, char **label)
8060Sstevel@tonic-gate {
8070Sstevel@tonic-gate 	char	*cookiestr;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	*cookie = 0;
8100Sstevel@tonic-gate 	*label = NULL;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	cookiestr = strtok(line, " \t\n");
8130Sstevel@tonic-gate 	if (cookiestr == NULL) {
8140Sstevel@tonic-gate 		return (-1);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/* Everything that follows, up to the newline, is the label. */
8180Sstevel@tonic-gate 	*label = strtok(NULL, "\n");
8190Sstevel@tonic-gate 	if (*label == NULL) {
8200Sstevel@tonic-gate 		return (-1);
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	*cookie = atoi(cookiestr);
8240Sstevel@tonic-gate 	return (0);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate  * Insert a mapping into the file (if it's not already there), given the
8290Sstevel@tonic-gate  * new label.  Return the assigned cookie, or -1 on error.
8300Sstevel@tonic-gate  */
8310Sstevel@tonic-gate int
8320Sstevel@tonic-gate kmc_insert_mapping(char *label)
8330Sstevel@tonic-gate {
8340Sstevel@tonic-gate 	FILE	*map;
835*4757Sdanmcd 	char	linebuf[IBUF_SIZE];
8360Sstevel@tonic-gate 	char	*cur_label;
8370Sstevel@tonic-gate 	int	max_cookie = 0, cur_cookie, rtn_cookie;
8380Sstevel@tonic-gate 	int	rtnerr = 0;
8390Sstevel@tonic-gate 	boolean_t	found = B_FALSE;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	/* open and lock the file; will sleep until lock is available */
8420Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
8430Sstevel@tonic-gate 		/* kmc_open_and_lock() sets errno appropriately */
8440Sstevel@tonic-gate 		return (-1);
8450Sstevel@tonic-gate 	}
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
8480Sstevel@tonic-gate 
849*4757Sdanmcd 		/* Skip blank lines, which often come near EOF. */
850*4757Sdanmcd 		if (strlen(linebuf) == 0)
851*4757Sdanmcd 			continue;
852*4757Sdanmcd 
8530Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
8540Sstevel@tonic-gate 			rtnerr = EINVAL;
8550Sstevel@tonic-gate 			goto error;
8560Sstevel@tonic-gate 		}
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		if (cur_cookie > max_cookie)
8590Sstevel@tonic-gate 			max_cookie = cur_cookie;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 		if ((!found) && (strcmp(cur_label, label) == 0)) {
8620Sstevel@tonic-gate 			found = B_TRUE;
8630Sstevel@tonic-gate 			rtn_cookie = cur_cookie;
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	if (!found) {
8680Sstevel@tonic-gate 		rtn_cookie = ++max_cookie;
8690Sstevel@tonic-gate 		if ((fprintf(map, "%u\t%s\n", rtn_cookie, label) < 0) ||
8700Sstevel@tonic-gate 		    (fflush(map) < 0)) {
8710Sstevel@tonic-gate 			rtnerr = errno;
8720Sstevel@tonic-gate 			goto error;
8730Sstevel@tonic-gate 		}
8740Sstevel@tonic-gate 	}
8750Sstevel@tonic-gate 	(void) fclose(map);
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	return (rtn_cookie);
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate error:
8800Sstevel@tonic-gate 	(void) fclose(map);
8810Sstevel@tonic-gate 	errno = rtnerr;
8820Sstevel@tonic-gate 	return (-1);
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate /*
8860Sstevel@tonic-gate  * Lookup the given cookie and return its corresponding label.  Return
8870Sstevel@tonic-gate  * a pointer to the label on success, NULL on error (or if the label is
8880Sstevel@tonic-gate  * not found).  Note that the returned label pointer points to a static
8890Sstevel@tonic-gate  * string, so the label will be overwritten by a subsequent call to the
8900Sstevel@tonic-gate  * function; the function is also not thread-safe as a result.
8910Sstevel@tonic-gate  */
8920Sstevel@tonic-gate char *
8930Sstevel@tonic-gate kmc_lookup_by_cookie(int cookie)
8940Sstevel@tonic-gate {
8950Sstevel@tonic-gate 	FILE		*map;
896*4757Sdanmcd 	static char	linebuf[IBUF_SIZE];
8970Sstevel@tonic-gate 	char		*cur_label;
8980Sstevel@tonic-gate 	int		cur_cookie;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
9010Sstevel@tonic-gate 		return (NULL);
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
9070Sstevel@tonic-gate 			(void) fclose(map);
9080Sstevel@tonic-gate 			return (NULL);
9090Sstevel@tonic-gate 		}
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 		if (cookie == cur_cookie) {
9120Sstevel@tonic-gate 			(void) fclose(map);
9130Sstevel@tonic-gate 			return (cur_label);
9140Sstevel@tonic-gate 		}
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 	(void) fclose(map);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	return (NULL);
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate /*
9220Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
9230Sstevel@tonic-gate  * Return values include:
9240Sstevel@tonic-gate  *
9250Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
9260Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
9270Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
9280Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
9290Sstevel@tonic-gate  *		extv[0].
9300Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
9310Sstevel@tonic-gate  *		unknown header.
9320Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
9330Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
9340Sstevel@tonic-gate  *
9350Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
9360Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
9370Sstevel@tonic-gate  */
9380Sstevel@tonic-gate int
9390Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize,
9400Sstevel@tonic-gate     char *diag_buf, uint_t diag_buf_len)
9410Sstevel@tonic-gate {
9420Sstevel@tonic-gate 	int i;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	if (diag_buf != NULL)
9450Sstevel@tonic-gate 		diag_buf[0] = '\0';
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	for (i = 1; i <= SPD_EXT_MAX; i++)
9480Sstevel@tonic-gate 		extv[i] = NULL;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	i = 0;
9510Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	extv[0] = (spd_ext_t *)(basehdr + 1);
9540Sstevel@tonic-gate 	msgsize = SPD_64TO8(msgsize);
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	while ((char *)extv[0] < ((char *)basehdr + msgsize)) {
9570Sstevel@tonic-gate 		/* Check for unknown headers. */
9580Sstevel@tonic-gate 		i++;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 		if (extv[0]->spd_ext_type == 0 ||
9610Sstevel@tonic-gate 		    extv[0]->spd_ext_type > SPD_EXT_MAX) {
9620Sstevel@tonic-gate 			if (diag_buf != NULL) {
9630Sstevel@tonic-gate 				(void) snprintf(diag_buf, diag_buf_len,
9640Sstevel@tonic-gate 				    "spdsock ext 0x%X unknown: 0x%X",
9650Sstevel@tonic-gate 				    i, extv[0]->spd_ext_type);
9660Sstevel@tonic-gate 			}
9670Sstevel@tonic-gate 			return (KGE_UNK);
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 		/*
9710Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
9720Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
9730Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
9740Sstevel@tonic-gate 		 */
9750Sstevel@tonic-gate 		if (extv[0]->spd_ext_len == 0 ||
9760Sstevel@tonic-gate 		    (uint8_t *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
9770Sstevel@tonic-gate 		    (uint8_t *)((uint8_t *)basehdr + msgsize))
9780Sstevel@tonic-gate 			return (KGE_LEN);
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 		/* Check for redundant headers. */
9810Sstevel@tonic-gate 		if (extv[extv[0]->spd_ext_type] != NULL)
9820Sstevel@tonic-gate 			return (KGE_DUP);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
9850Sstevel@tonic-gate 		extv[extv[0]->spd_ext_type] = extv[0];
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
9880Sstevel@tonic-gate 		extv[0] = (spd_ext_t *)
9890Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
9900Sstevel@tonic-gate 	}
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	/* Everything's cool. */
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	/*
9950Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
9960Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	if (extv[0] == (spd_ext_t *)(basehdr + 1))
9990Sstevel@tonic-gate 		extv[0] = NULL;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	return (KGE_OK);
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate const char *
10050Sstevel@tonic-gate spdsock_diag(int diagnostic)
10060Sstevel@tonic-gate {
10070Sstevel@tonic-gate 	switch (diagnostic) {
10080Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NONE:
10094064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no error"));
10100Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNKNOWN_EXT:
10114064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown extension"));
10120Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_EXTLEN:
10134064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad extension length"));
10140Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_RULE_EXT:
10154064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no rule extension"));
10160Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_ADDR_LEN:
10174064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad address len"));
10180Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MIXED_AF:
10194064Smarkfen 		return (dgettext(TEXT_DOMAIN, "mixed address family"));
10200Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_NO_MEM:
10214064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: no memory"));
10220Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT:
10234064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: wrong action count"));
10240Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_TYPE:
10254064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad type"));
10260Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_FLAGS:
10274064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad flags"));
10280Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_INCON_FLAGS:
10294064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: inconsistent flags"));
10300Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLPORT:
10314064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local port"));
10320Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLPORT:
10334064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local port"));
10340Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMPORT:
10354064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote port"));
10360Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMPORT:
10374064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote port"));
10380Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_PROTO:
10394064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed proto"));
10400Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_PROTO:
10414064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate proto"));
10420Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLADDR:
10434064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local address"));
10440Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLADDR:
10454064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local address"));
10460Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMADDR:
10474064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote address"));
10480Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMADDR:
10494064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote address"));
10500Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_ACTION:
10514064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed action"));
10520Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_ACTION:
10534064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate action"));
10540Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULE:
10554064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed rule"));
10560Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULE:
10574064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate rule"));
10580Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULESET:
10594064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed ruleset"));
10600Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULESET:
10614064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate ruleset"));
10620Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_INVALID_RULE_INDEX:
10634064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid rule index"));
10640Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_SPDID:
10654064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad spdid"));
10660Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_MSG_TYPE:
10674064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad message type"));
10680Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_ALG:
10694064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH algorithm"));
10700Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG:
10714064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10724064Smarkfen 		    "unsupported ESP encryption algorithm"));
10730Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG:
10744064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10754064Smarkfen 		    "unsupported ESP authentication algorithm"));
10760Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_KEYSIZE:
10774064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH key size"));
10780Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_KEYSIZE:
10794064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10804064Smarkfen 		    "unsupported ESP encryption key size"));
10810Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_KEYSIZE:
10824064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10834064Smarkfen 		    "unsupported ESP authentication key size"));
10840Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_ACTION_EXT:
10854064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No ACTION extension"));
10860Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_ID_RANGE:
10874064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid algorithm identifer"));
10880Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES:
10894064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10904064Smarkfen 		    "number of key sizes inconsistent"));
10910Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES:
10924064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10934064Smarkfen 		    "number of block sizes inconsistent"));
10940Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN:
10954064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid mechanism name length"));
10963055Sdanmcd 	case SPD_DIAGNOSTIC_NOT_GLOBAL_OP:
10974064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10984064Smarkfen 		    "operation not applicable to all policies"));
10993055Sdanmcd 	case SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS:
11004064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11014064Smarkfen 		    "using selectors on a transport-mode tunnel"));
11020Sstevel@tonic-gate 	default:
11034064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown diagnostic"));
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate }
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate /*
11080Sstevel@tonic-gate  * PF_KEY Diagnostic table.
11090Sstevel@tonic-gate  *
11100Sstevel@tonic-gate  * PF_KEY NOTE:  If you change pfkeyv2.h's SADB_X_DIAGNOSTIC_* space, this is
11110Sstevel@tonic-gate  * where you need to add new messages.
11120Sstevel@tonic-gate  */
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate const char *
11150Sstevel@tonic-gate keysock_diag(int diagnostic)
11160Sstevel@tonic-gate {
11170Sstevel@tonic-gate 	switch (diagnostic) {
11183055Sdanmcd 	case SADB_X_DIAGNOSTIC_NONE:
11194064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No diagnostic"));
11200Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_MSG:
11214064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown message type"));
11220Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_EXT:
11234064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown extension type"));
11240Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EXTLEN:
11254064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad extension length"));
11260Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE:
11274064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11284064Smarkfen 		    "Unknown Security Association type"));
11290Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SATYPE_NEEDED:
11304064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11314064Smarkfen 		    "Specific Security Association type needed"));
11320Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_SADBS:
11334064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11344064Smarkfen 		    "No Security Association Databases present"));
11350Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_EXT:
11364064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11374064Smarkfen 		    "No extensions needed for message"));
11380Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC_AF:
11394064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address family"));
11400Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST_AF:
11414064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11424064Smarkfen 		    "Bad destination address family"));
11430Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_PROXY_AF:
11444064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11454064Smarkfen 		    "Bad inner-source address family"));
11460Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AF_MISMATCH:
11474064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11484064Smarkfen 		    "Source/destination address family mismatch"));
11490Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC:
11504064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address value"));
11510Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST:
11524064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad destination address value"));
11530Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ALLOC_HSERR:
11544064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11554064Smarkfen 		    "Soft allocations limit more than hard limit"));
11560Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BYTES_HSERR:
11574064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11584064Smarkfen 		    "Soft bytes limit more than hard limit"));
11590Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ADDTIME_HSERR:
11604064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft add expiration time later "
11610Sstevel@tonic-gate 		    "than hard expiration time"));
11620Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_USETIME_HSERR:
11634064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft use expiration time later "
11640Sstevel@tonic-gate 		    "than hard expiration time"));
11650Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SRC:
11664064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing source address"));
11670Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_DST:
11684064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing destination address"));
11690Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SA:
11704064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SA extension"));
11710Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_EKEY:
11724064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing encryption key"));
11730Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_AKEY:
11744064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing authentication key"));
11750Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_RANGE:
11764064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SPI range"));
11770Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SRC:
11784064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate source address"));
11790Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_DST:
11804064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate destination address"));
11810Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SA:
11824064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SA extension"));
11830Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_EKEY:
11844064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate encryption key"));
11850Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_AKEY:
11864064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate authentication key"));
11870Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_RANGE:
11884064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SPI range"));
11890Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SRC:
11904064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed source address"));
11910Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_DST:
11924064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed destination address"));
11930Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SA:
11944064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SA extension"));
11950Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_EKEY:
11964064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed encryption key"));
11970Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_AKEY:
11984064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed authentication key"));
11990Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_RANGE:
12004064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SPI range"));
12010Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AKEY_PRESENT:
12024064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Authentication key not needed"));
12030Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_EKEY_PRESENT:
12044064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Encryption key not needed"));
12050Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_PROP_PRESENT:
12064064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Proposal extension not needed"));
12070Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SUPP_PRESENT:
12084064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12094064Smarkfen 		    "Supported algorithms extension not needed"));
12100Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AALG:
12114064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12124064Smarkfen 		    "Unsupported authentication algorithm"));
12130Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EALG:
12144064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12154064Smarkfen 		    "Unsupported encryption algorithm"));
12160Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SAFLAGS:
12174064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA flags"));
12180Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SASTATE:
12194064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA state"));
12200Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AKEYBITS:
12214064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12224064Smarkfen 		    "Bad number of authentication bits"));
12230Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EKEYBITS:
12244064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12254064Smarkfen 		    "Bad number of encryption bits"));
12260Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ENCR_NOTSUPP:
12274064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12284064Smarkfen 		    "Encryption not supported for this SA type"));
12290Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_EKEY:
12304064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak encryption key"));
12310Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_AKEY:
12324064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak authentication key"));
12330Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMP:
12344064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12354064Smarkfen 		    "Duplicate key management protocol"));
12360Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMC:
12374064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12384064Smarkfen 		    "Duplicate key management cookie"));
12390Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_LOC:
12404064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T local address"));
12410Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_REM:
12424064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T remote address"));
12430Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_LOC:
12444064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T local address"));
12450Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_REM:
12464064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12474064Smarkfen 		    "Duplicate NAT-T remote address"));
12480Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC:
12494064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed NAT-T local address"));
12500Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM:
12514064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12524064Smarkfen 		    "Malformed NAT-T remote address"));
12530Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_PORTS:
12544064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T ports"));
12553055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_SRC:
12564064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing inner source address"));
12573055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_DST:
12584064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12594064Smarkfen 		    "Missing inner destination address"));
12603055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC:
12614064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12624064Smarkfen 		    "Duplicate inner source address"));
12633055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST:
12644064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12654064Smarkfen 		    "Duplicate inner destination address"));
12663055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC:
12674064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12684064Smarkfen 		    "Malformed inner source address"));
12693055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST:
12704064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12714064Smarkfen 		    "Malformed inner destination address"));
12723055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC:
12734064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12744064Smarkfen 		    "Invalid inner-source prefix length "));
12753055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_DST:
12764064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12774064Smarkfen 		    "Invalid inner-destination prefix length"));
12783055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF:
12794064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12804064Smarkfen 		    "Bad inner-destination address family"));
12813055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH:
12824064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12833055Sdanmcd 		    "Inner source/destination address family mismatch"));
12843055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF:
12854064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12864064Smarkfen 		    "Bad NAT-T remote address family"));
12873055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF:
12884064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12894064Smarkfen 		    "Bad NAT-T local address family"));
12903055Sdanmcd 	case SADB_X_DIAGNOSTIC_PROTO_MISMATCH:
12914064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12924064Smarkfen 		    "Source/desination protocol mismatch"));
12933055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_PROTO_MISMATCH:
12944064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12954064Smarkfen 		    "Inner source/desination protocol mismatch"));
12963055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUAL_PORT_SETS:
12974064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12984064Smarkfen 		    "Both inner ports and outer ports are set"));
12990Sstevel@tonic-gate 	default:
13004064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code"));
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate }
13033055Sdanmcd 
13043055Sdanmcd /*
13053055Sdanmcd  * Convert an IPv6 mask to a prefix len.  I assume all IPv6 masks are
13063055Sdanmcd  * contiguous, so I stop at the first zero bit!
13073055Sdanmcd  */
13083055Sdanmcd int
13093055Sdanmcd in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped)
13103055Sdanmcd {
13113055Sdanmcd 	int rc = 0;
13123055Sdanmcd 	uint8_t last;
13133055Sdanmcd 	int limit = IPV6_ABITS;
13143055Sdanmcd 
13153055Sdanmcd 	if (is_v4mapped) {
13163055Sdanmcd 		mask += ((IPV6_ABITS - IP_ABITS)/8);
13173055Sdanmcd 		limit = IP_ABITS;
13183055Sdanmcd 	}
13193055Sdanmcd 
13203055Sdanmcd 	while (*mask == 0xff) {
13213055Sdanmcd 		rc += 8;
13223055Sdanmcd 		if (rc == limit)
13233055Sdanmcd 			return (limit);
13243055Sdanmcd 		mask++;
13253055Sdanmcd 	}
13263055Sdanmcd 
13273055Sdanmcd 	last = *mask;
13283055Sdanmcd 	while (last != 0) {
13293055Sdanmcd 		rc++;
13303055Sdanmcd 		last = (last << 1) & 0xff;
13313055Sdanmcd 	}
13323055Sdanmcd 
13333055Sdanmcd 	return (rc);
13343055Sdanmcd }
13353055Sdanmcd 
13363055Sdanmcd /*
13373055Sdanmcd  * Expand the diagnostic code into a message.
13383055Sdanmcd  */
13393055Sdanmcd void
13403055Sdanmcd print_diagnostic(FILE *file, uint16_t diagnostic)
13413055Sdanmcd {
13423055Sdanmcd 	/* Use two spaces so above strings can fit on the line. */
13434064Smarkfen 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
13444064Smarkfen 	    "  Diagnostic code %u:  %s.\n"),
13453055Sdanmcd 	    diagnostic, keysock_diag(diagnostic));
13463055Sdanmcd }
13473055Sdanmcd 
13483055Sdanmcd /*
13493055Sdanmcd  * Prints the base PF_KEY message.
13503055Sdanmcd  */
13513055Sdanmcd void
13523055Sdanmcd print_sadb_msg(struct sadb_msg *samsg, time_t wallclock, boolean_t vflag)
13533055Sdanmcd {
13543055Sdanmcd 	if (wallclock != 0)
13554064Smarkfen 		printsatime(wallclock, dgettext(TEXT_DOMAIN,
13564064Smarkfen 		    "%sTimestamp: %s\n"), "", NULL,
13573055Sdanmcd 		    vflag);
13583055Sdanmcd 
13594064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, "Base message (version %u) type "),
13603055Sdanmcd 	    samsg->sadb_msg_version);
13613055Sdanmcd 	switch (samsg->sadb_msg_type) {
13623055Sdanmcd 	case SADB_RESERVED:
13634064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
13644064Smarkfen 		    "RESERVED (warning: set to 0)"));
13653055Sdanmcd 		break;
13663055Sdanmcd 	case SADB_GETSPI:
13673055Sdanmcd 		(void) printf("GETSPI");
13683055Sdanmcd 		break;
13693055Sdanmcd 	case SADB_UPDATE:
13703055Sdanmcd 		(void) printf("UPDATE");
13713055Sdanmcd 		break;
13723055Sdanmcd 	case SADB_ADD:
13733055Sdanmcd 		(void) printf("ADD");
13743055Sdanmcd 		break;
13753055Sdanmcd 	case SADB_DELETE:
13763055Sdanmcd 		(void) printf("DELETE");
13773055Sdanmcd 		break;
13783055Sdanmcd 	case SADB_GET:
13793055Sdanmcd 		(void) printf("GET");
13803055Sdanmcd 		break;
13813055Sdanmcd 	case SADB_ACQUIRE:
13823055Sdanmcd 		(void) printf("ACQUIRE");
13833055Sdanmcd 		break;
13843055Sdanmcd 	case SADB_REGISTER:
13853055Sdanmcd 		(void) printf("REGISTER");
13863055Sdanmcd 		break;
13873055Sdanmcd 	case SADB_EXPIRE:
13883055Sdanmcd 		(void) printf("EXPIRE");
13893055Sdanmcd 		break;
13903055Sdanmcd 	case SADB_FLUSH:
13913055Sdanmcd 		(void) printf("FLUSH");
13923055Sdanmcd 		break;
13933055Sdanmcd 	case SADB_DUMP:
13943055Sdanmcd 		(void) printf("DUMP");
13953055Sdanmcd 		break;
13963055Sdanmcd 	case SADB_X_PROMISC:
13973055Sdanmcd 		(void) printf("X_PROMISC");
13983055Sdanmcd 		break;
13993055Sdanmcd 	case SADB_X_INVERSE_ACQUIRE:
14003055Sdanmcd 		(void) printf("X_INVERSE_ACQUIRE");
14013055Sdanmcd 		break;
14023055Sdanmcd 	default:
14034064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
14044064Smarkfen 		    "Unknown (%u)"), samsg->sadb_msg_type);
14053055Sdanmcd 		break;
14063055Sdanmcd 	}
14074064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, ", SA type "));
14083055Sdanmcd 
14093055Sdanmcd 	switch (samsg->sadb_msg_satype) {
14103055Sdanmcd 	case SADB_SATYPE_UNSPEC:
14114064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "<unspecified/all>"));
14123055Sdanmcd 		break;
14133055Sdanmcd 	case SADB_SATYPE_AH:
14143055Sdanmcd 		(void) printf("AH");
14153055Sdanmcd 		break;
14163055Sdanmcd 	case SADB_SATYPE_ESP:
14173055Sdanmcd 		(void) printf("ESP");
14183055Sdanmcd 		break;
14193055Sdanmcd 	case SADB_SATYPE_RSVP:
14203055Sdanmcd 		(void) printf("RSVP");
14213055Sdanmcd 		break;
14223055Sdanmcd 	case SADB_SATYPE_OSPFV2:
14233055Sdanmcd 		(void) printf("OSPFv2");
14243055Sdanmcd 		break;
14253055Sdanmcd 	case SADB_SATYPE_RIPV2:
14263055Sdanmcd 		(void) printf("RIPv2");
14273055Sdanmcd 		break;
14283055Sdanmcd 	case SADB_SATYPE_MIP:
14294064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Mobile IP"));
14303055Sdanmcd 		break;
14313055Sdanmcd 	default:
14324064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
14334064Smarkfen 		    "<unknown %u>"), samsg->sadb_msg_satype);
14343055Sdanmcd 		break;
14353055Sdanmcd 	}
14363055Sdanmcd 
14373055Sdanmcd 	(void) printf(".\n");
14383055Sdanmcd 
14393055Sdanmcd 	if (samsg->sadb_msg_errno != 0) {
14404064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Error %s from PF_KEY.\n"),
14413055Sdanmcd 		    strerror(samsg->sadb_msg_errno));
14423055Sdanmcd 		print_diagnostic(stdout, samsg->sadb_x_msg_diagnostic);
14433055Sdanmcd 	}
14443055Sdanmcd 
14454064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
14464064Smarkfen 	    "Message length %u bytes, seq=%u, pid=%u.\n"),
14473055Sdanmcd 	    SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq,
14483055Sdanmcd 	    samsg->sadb_msg_pid);
14493055Sdanmcd }
14503055Sdanmcd 
14513055Sdanmcd /*
14523055Sdanmcd  * Print the SA extension for PF_KEY.
14533055Sdanmcd  */
14543055Sdanmcd void
14553055Sdanmcd print_sa(char *prefix, struct sadb_sa *assoc)
14563055Sdanmcd {
14573055Sdanmcd 	if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) {
14584064Smarkfen 		warnx(dgettext(TEXT_DOMAIN,
14594064Smarkfen 		    "WARNING: SA info extension length (%u) is bad."),
14603055Sdanmcd 		    SADB_64TO8(assoc->sadb_sa_len));
14613055Sdanmcd 	}
14623055Sdanmcd 
14634064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
14644064Smarkfen 	    "%sSADB_ASSOC spi=0x%x, replay=%u, state="),
14653055Sdanmcd 	    prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay);
14663055Sdanmcd 	switch (assoc->sadb_sa_state) {
14673055Sdanmcd 	case SADB_SASTATE_LARVAL:
14684064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "LARVAL"));
14693055Sdanmcd 		break;
14703055Sdanmcd 	case SADB_SASTATE_MATURE:
14714064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "MATURE"));
14723055Sdanmcd 		break;
14733055Sdanmcd 	case SADB_SASTATE_DYING:
14744064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "DYING"));
14753055Sdanmcd 		break;
14763055Sdanmcd 	case SADB_SASTATE_DEAD:
14774064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "DEAD"));
14783055Sdanmcd 		break;
14793055Sdanmcd 	default:
14804064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
14814064Smarkfen 		    "<unknown %u>"), assoc->sadb_sa_state);
14823055Sdanmcd 	}
14833055Sdanmcd 
14843055Sdanmcd 	if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
14854064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
14864064Smarkfen 		    "\n%sAuthentication algorithm = "),
14873055Sdanmcd 		    prefix);
14883055Sdanmcd 		(void) dump_aalg(assoc->sadb_sa_auth, stdout);
14893055Sdanmcd 	}
14903055Sdanmcd 
14913055Sdanmcd 	if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
14924064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
14934064Smarkfen 		    "\n%sEncryption algorithm = "), prefix);
14943055Sdanmcd 		(void) dump_ealg(assoc->sadb_sa_encrypt, stdout);
14953055Sdanmcd 	}
14963055Sdanmcd 
14974064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix,
14983055Sdanmcd 	    assoc->sadb_sa_flags);
14993055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS)
15003055Sdanmcd 		(void) printf("PFS ");
15013055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY)
15023055Sdanmcd 		(void) printf("NOREPLAY ");
15033055Sdanmcd 
15043055Sdanmcd 	/* BEGIN Solaris-specific flags. */
15053055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED)
15063055Sdanmcd 		(void) printf("X_USED ");
15073055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE)
15083055Sdanmcd 		(void) printf("X_UNIQUE ");
15093055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1)
15103055Sdanmcd 		(void) printf("X_AALG1 ");
15113055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2)
15123055Sdanmcd 		(void) printf("X_AALG2 ");
15133055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1)
15143055Sdanmcd 		(void) printf("X_EALG1 ");
15153055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2)
15163055Sdanmcd 		(void) printf("X_EALG2 ");
15173055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC)
15183055Sdanmcd 		(void) printf("X_NATT_LOC ");
15193055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM)
15203055Sdanmcd 		(void) printf("X_NATT_REM ");
15213055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL)
15223055Sdanmcd 		(void) printf("X_TUNNEL ");
15233055Sdanmcd 	/* END Solaris-specific flags. */
15243055Sdanmcd 
15253055Sdanmcd 	(void) printf(">\n");
15263055Sdanmcd }
15273055Sdanmcd 
15283055Sdanmcd void
15293055Sdanmcd printsatime(int64_t lt, const char *msg, const char *pfx, const char *pfx2,
15303055Sdanmcd     boolean_t vflag)
15313055Sdanmcd {
15323055Sdanmcd 	char tbuf[TBUF_SIZE]; /* For strftime() call. */
15333055Sdanmcd 	const char *tp = tbuf;
15343055Sdanmcd 	time_t t = lt;
15353055Sdanmcd 	struct tm res;
15363055Sdanmcd 
15373055Sdanmcd 	if (t != lt) {
15383055Sdanmcd 		if (lt > 0)
15393055Sdanmcd 			t = LONG_MAX;
15403055Sdanmcd 		else
15413055Sdanmcd 			t = LONG_MIN;
15423055Sdanmcd 	}
15433055Sdanmcd 
15443055Sdanmcd 	if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0)
15454064Smarkfen 		tp = dgettext(TEXT_DOMAIN, "<time conversion failed>");
15463055Sdanmcd 	(void) printf(msg, pfx, tp);
15473055Sdanmcd 	if (vflag && (pfx2 != NULL))
15484064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
15494064Smarkfen 		    "%s\t(raw time value %llu)\n"), pfx2, lt);
15503055Sdanmcd }
15513055Sdanmcd 
15523055Sdanmcd /*
15533055Sdanmcd  * Print the SA lifetime information.  (An SADB_EXT_LIFETIME_* extension.)
15543055Sdanmcd  */
15553055Sdanmcd void
15563055Sdanmcd print_lifetimes(time_t wallclock, struct sadb_lifetime *current,
15573055Sdanmcd     struct sadb_lifetime *hard, struct sadb_lifetime *soft, boolean_t vflag)
15583055Sdanmcd {
15593055Sdanmcd 	int64_t scratch;
15604064Smarkfen 	char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: ");
15614064Smarkfen 	char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: ");
15624064Smarkfen 	char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: ");
15633055Sdanmcd 
15643055Sdanmcd 	if (current != NULL &&
15653055Sdanmcd 	    current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) {
15664064Smarkfen 		warnx(dgettext(TEXT_DOMAIN,
15674064Smarkfen 		    "WARNING: CURRENT lifetime extension length (%u) is bad."),
15683055Sdanmcd 		    SADB_64TO8(current->sadb_lifetime_len));
15693055Sdanmcd 	}
15703055Sdanmcd 
15713055Sdanmcd 	if (hard != NULL &&
15723055Sdanmcd 	    hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) {
15734064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "WARNING: HARD lifetime "
15744342Spwernau 		    "extension length (%u) is bad."),
15753055Sdanmcd 		    SADB_64TO8(hard->sadb_lifetime_len));
15763055Sdanmcd 	}
15773055Sdanmcd 
15783055Sdanmcd 	if (soft != NULL &&
15793055Sdanmcd 	    soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) {
15804064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "WARNING: SOFT lifetime "
15813055Sdanmcd 		    "extension length (%u) is bad."),
15823055Sdanmcd 		    SADB_64TO8(soft->sadb_lifetime_len));
15833055Sdanmcd 	}
15843055Sdanmcd 
15853055Sdanmcd 	(void) printf(" LT: Lifetime information\n");
15863055Sdanmcd 
15873055Sdanmcd 	if (current != NULL) {
15883055Sdanmcd 		/* Express values as current values. */
15894064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
15903055Sdanmcd 		    "%s%llu bytes protected, %u allocations used.\n"),
15913055Sdanmcd 		    current_prefix, current->sadb_lifetime_bytes,
15923055Sdanmcd 		    current->sadb_lifetime_allocations);
15933055Sdanmcd 		printsatime(current->sadb_lifetime_addtime,
15944064Smarkfen 		    dgettext(TEXT_DOMAIN, "%sSA added at time %s\n"),
15953055Sdanmcd 		    current_prefix, current_prefix, vflag);
15963055Sdanmcd 		if (current->sadb_lifetime_usetime != 0) {
15973055Sdanmcd 			printsatime(current->sadb_lifetime_usetime,
15984064Smarkfen 			    dgettext(TEXT_DOMAIN,
15994064Smarkfen 			    "%sSA first used at time %s\n"),
16003055Sdanmcd 			    current_prefix, current_prefix, vflag);
16013055Sdanmcd 		}
16024064Smarkfen 		printsatime(wallclock, dgettext(TEXT_DOMAIN,
16034064Smarkfen 		    "%sTime now is %s\n"), current_prefix, current_prefix,
16044064Smarkfen 		    vflag);
16053055Sdanmcd 	}
16063055Sdanmcd 
16073055Sdanmcd 	if (soft != NULL) {
16084064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16094064Smarkfen 		    "%sSoft lifetime information:  "),
16103055Sdanmcd 		    soft_prefix);
16114064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16124064Smarkfen 		    "%llu bytes of lifetime, %u "
16133055Sdanmcd 		    "allocations.\n"), soft->sadb_lifetime_bytes,
16143055Sdanmcd 		    soft->sadb_lifetime_allocations);
16154064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16164064Smarkfen 		    "%s%llu seconds of post-add lifetime.\n"),
16173055Sdanmcd 		    soft_prefix, soft->sadb_lifetime_addtime);
16184064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16194064Smarkfen 		    "%s%llu seconds of post-use lifetime.\n"),
16203055Sdanmcd 		    soft_prefix, soft->sadb_lifetime_usetime);
16213055Sdanmcd 		/* If possible, express values as time remaining. */
16223055Sdanmcd 		if (current != NULL) {
16233055Sdanmcd 			if (soft->sadb_lifetime_bytes != 0)
16244064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
16253055Sdanmcd 				    "%s%llu more bytes can be protected.\n"),
16263055Sdanmcd 				    soft_prefix,
16273055Sdanmcd 				    (soft->sadb_lifetime_bytes >
16284342Spwernau 				    current->sadb_lifetime_bytes) ?
16293055Sdanmcd 				    (soft->sadb_lifetime_bytes -
16304342Spwernau 				    current->sadb_lifetime_bytes) : (0));
16313055Sdanmcd 			if (soft->sadb_lifetime_addtime != 0 ||
16323055Sdanmcd 			    (soft->sadb_lifetime_usetime != 0 &&
16334342Spwernau 			    current->sadb_lifetime_usetime != 0)) {
16343055Sdanmcd 				int64_t adddelta, usedelta;
16353055Sdanmcd 
16363055Sdanmcd 				if (soft->sadb_lifetime_addtime != 0) {
16373055Sdanmcd 					adddelta =
16383055Sdanmcd 					    current->sadb_lifetime_addtime +
16393055Sdanmcd 					    soft->sadb_lifetime_addtime -
16403055Sdanmcd 					    wallclock;
16413055Sdanmcd 				} else {
16423055Sdanmcd 					adddelta = TIME_MAX;
16433055Sdanmcd 				}
16443055Sdanmcd 
16453055Sdanmcd 				if (soft->sadb_lifetime_usetime != 0 &&
16463055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
16473055Sdanmcd 					usedelta =
16483055Sdanmcd 					    current->sadb_lifetime_usetime +
16493055Sdanmcd 					    soft->sadb_lifetime_usetime -
16503055Sdanmcd 					    wallclock;
16513055Sdanmcd 				} else {
16523055Sdanmcd 					usedelta = TIME_MAX;
16533055Sdanmcd 				}
16543055Sdanmcd 				(void) printf("%s", soft_prefix);
16553055Sdanmcd 				scratch = MIN(adddelta, usedelta);
16563055Sdanmcd 				if (scratch >= 0) {
16574064Smarkfen 					(void) printf(dgettext(TEXT_DOMAIN,
16584064Smarkfen 					    "Soft expiration occurs in %lld "
16594064Smarkfen 					    "seconds, "), scratch);
16603055Sdanmcd 				} else {
16614064Smarkfen 					(void) printf(dgettext(TEXT_DOMAIN,
16623055Sdanmcd 					    "Soft expiration occurred "));
16633055Sdanmcd 				}
16643055Sdanmcd 				scratch += wallclock;
16654064Smarkfen 				printsatime(scratch, dgettext(TEXT_DOMAIN,
16664064Smarkfen 				    "%sat %s.\n"), "", soft_prefix, vflag);
16673055Sdanmcd 			}
16683055Sdanmcd 		}
16693055Sdanmcd 	}
16703055Sdanmcd 
16713055Sdanmcd 	if (hard != NULL) {
16724064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16734064Smarkfen 		    "%sHard lifetime information:  "), hard_prefix);
16744064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "%llu bytes of lifetime, "
16753055Sdanmcd 		    "%u allocations.\n"), hard->sadb_lifetime_bytes,
16763055Sdanmcd 		    hard->sadb_lifetime_allocations);
16774064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16784064Smarkfen 		    "%s%llu seconds of post-add lifetime.\n"),
16793055Sdanmcd 		    hard_prefix, hard->sadb_lifetime_addtime);
16804064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16814064Smarkfen 		    "%s%llu seconds of post-use lifetime.\n"),
16823055Sdanmcd 		    hard_prefix, hard->sadb_lifetime_usetime);
16833055Sdanmcd 		/* If possible, express values as time remaining. */
16843055Sdanmcd 		if (current != NULL) {
16853055Sdanmcd 			if (hard->sadb_lifetime_bytes != 0)
16864064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
16873055Sdanmcd 				    "%s%llu more bytes can be protected.\n"),
16883055Sdanmcd 				    hard_prefix,
16893055Sdanmcd 				    (hard->sadb_lifetime_bytes >
16904342Spwernau 				    current->sadb_lifetime_bytes) ?
16913055Sdanmcd 				    (hard->sadb_lifetime_bytes -
16924342Spwernau 				    current->sadb_lifetime_bytes) : (0));
16933055Sdanmcd 			if (hard->sadb_lifetime_addtime != 0 ||
16943055Sdanmcd 			    (hard->sadb_lifetime_usetime != 0 &&
16954342Spwernau 			    current->sadb_lifetime_usetime != 0)) {
16963055Sdanmcd 				int64_t adddelta, usedelta;
16973055Sdanmcd 
16983055Sdanmcd 				if (hard->sadb_lifetime_addtime != 0) {
16993055Sdanmcd 					adddelta =
17003055Sdanmcd 					    current->sadb_lifetime_addtime +
17013055Sdanmcd 					    hard->sadb_lifetime_addtime -
17023055Sdanmcd 					    wallclock;
17033055Sdanmcd 				} else {
17043055Sdanmcd 					adddelta = TIME_MAX;
17053055Sdanmcd 				}
17063055Sdanmcd 
17073055Sdanmcd 				if (hard->sadb_lifetime_usetime != 0 &&
17083055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
17093055Sdanmcd 					usedelta =
17103055Sdanmcd 					    current->sadb_lifetime_usetime +
17113055Sdanmcd 					    hard->sadb_lifetime_usetime -
17123055Sdanmcd 					    wallclock;
17133055Sdanmcd 				} else {
17143055Sdanmcd 					usedelta = TIME_MAX;
17153055Sdanmcd 				}
17163055Sdanmcd 				(void) printf("%s", hard_prefix);
17173055Sdanmcd 				scratch = MIN(adddelta, usedelta);
17183055Sdanmcd 				if (scratch >= 0) {
17194064Smarkfen 					(void) printf(dgettext(TEXT_DOMAIN,
17204064Smarkfen 					    "Hard expiration occurs in %lld "
17214064Smarkfen 					    "seconds, "), scratch);
17223055Sdanmcd 				} else {
17234064Smarkfen 					(void) printf(dgettext(TEXT_DOMAIN,
17243055Sdanmcd 					    "Hard expiration occured "));
17253055Sdanmcd 				}
17263055Sdanmcd 				scratch += wallclock;
17274064Smarkfen 				printsatime(scratch, dgettext(TEXT_DOMAIN,
17284064Smarkfen 				    "%sat %s.\n"), "", hard_prefix, vflag);
17293055Sdanmcd 			}
17303055Sdanmcd 		}
17313055Sdanmcd 	}
17323055Sdanmcd }
17333055Sdanmcd 
17343055Sdanmcd /*
17353055Sdanmcd  * Print an SADB_EXT_ADDRESS_* extension.
17363055Sdanmcd  */
17373055Sdanmcd void
17383055Sdanmcd print_address(char *prefix, struct sadb_address *addr)
17393055Sdanmcd {
17403055Sdanmcd 	struct protoent *pe;
17413055Sdanmcd 
17423055Sdanmcd 	(void) printf("%s", prefix);
17433055Sdanmcd 	switch (addr->sadb_address_exttype) {
17443055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
17454064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Source address "));
17463055Sdanmcd 		break;
17473055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
17484064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Inner source address "));
17493055Sdanmcd 		break;
17503055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
17514064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Destination address "));
17523055Sdanmcd 		break;
17533055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
17544064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
17554064Smarkfen 		    "Inner destination address "));
17563055Sdanmcd 		break;
17573055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
17584064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "NAT-T local address "));
17593055Sdanmcd 		break;
17603055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
17614064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "NAT-T remote address "));
17623055Sdanmcd 		break;
17633055Sdanmcd 	}
17643055Sdanmcd 
17654064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
17664064Smarkfen 	    "(proto=%d"), addr->sadb_address_proto);
17673055Sdanmcd 	if (!nflag) {
17683055Sdanmcd 		if (addr->sadb_address_proto == 0) {
17694064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "/<unspecified>"));
17703055Sdanmcd 		} else if ((pe = getprotobynumber(addr->sadb_address_proto))
17713055Sdanmcd 		    != NULL) {
17723055Sdanmcd 			(void) printf("/%s", pe->p_name);
17733055Sdanmcd 		} else {
17744064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "/<unknown>"));
17753055Sdanmcd 		}
17763055Sdanmcd 	}
17774064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, ")\n%s"), prefix);
17783055Sdanmcd 	(void) dump_sockaddr((struct sockaddr *)(addr + 1),
17793055Sdanmcd 	    addr->sadb_address_prefixlen, B_FALSE, stdout);
17803055Sdanmcd }
17813055Sdanmcd 
17823055Sdanmcd /*
17833055Sdanmcd  * Print an SADB_EXT_KEY extension.
17843055Sdanmcd  */
17853055Sdanmcd void
17863055Sdanmcd print_key(char *prefix, struct sadb_key *key)
17873055Sdanmcd {
17883055Sdanmcd 	(void) printf("%s", prefix);
17893055Sdanmcd 
17903055Sdanmcd 	switch (key->sadb_key_exttype) {
17913055Sdanmcd 	case SADB_EXT_KEY_AUTH:
17924064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Authentication"));
17933055Sdanmcd 		break;
17943055Sdanmcd 	case SADB_EXT_KEY_ENCRYPT:
17954064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Encryption"));
17963055Sdanmcd 		break;
17973055Sdanmcd 	}
17983055Sdanmcd 
17994064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, " key.\n%s"), prefix);
18003055Sdanmcd 	(void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits, stdout);
18013055Sdanmcd 	(void) putchar('\n');
18023055Sdanmcd }
18033055Sdanmcd 
18043055Sdanmcd /*
18053055Sdanmcd  * Print an SADB_EXT_IDENTITY_* extension.
18063055Sdanmcd  */
18073055Sdanmcd void
18083055Sdanmcd print_ident(char *prefix, struct sadb_ident *id)
18093055Sdanmcd {
18103055Sdanmcd 	boolean_t canprint = B_TRUE;
18113055Sdanmcd 
18123055Sdanmcd 	(void) printf("%s", prefix);
18133055Sdanmcd 	switch (id->sadb_ident_exttype) {
18143055Sdanmcd 	case SADB_EXT_IDENTITY_SRC:
18154064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Source"));
18163055Sdanmcd 		break;
18173055Sdanmcd 	case SADB_EXT_IDENTITY_DST:
18184064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Destination"));
18193055Sdanmcd 		break;
18203055Sdanmcd 	}
18213055Sdanmcd 
18224064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
18234064Smarkfen 	    " identity, uid=%d, type "), id->sadb_ident_id);
18243055Sdanmcd 	canprint = dump_sadb_idtype(id->sadb_ident_type, stdout, NULL);
18253055Sdanmcd 	(void) printf("\n%s", prefix);
18263055Sdanmcd 	if (canprint)
18273055Sdanmcd 		(void) printf("%s\n", (char *)(id + 1));
18283055Sdanmcd 	else
18294064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "<cannot print>\n"));
18303055Sdanmcd }
18313055Sdanmcd 
18323055Sdanmcd /*
18333055Sdanmcd  * Print an SADB_SENSITIVITY extension.
18343055Sdanmcd  */
18353055Sdanmcd void
18363055Sdanmcd print_sens(char *prefix, struct sadb_sens *sens)
18373055Sdanmcd {
18383055Sdanmcd 	uint64_t *bitmap = (uint64_t *)(sens + 1);
18393055Sdanmcd 	int i;
18403055Sdanmcd 
18413055Sdanmcd 	(void) printf(
18424064Smarkfen 	    dgettext(TEXT_DOMAIN,
18434064Smarkfen 	    "%sSensitivity DPD %d, sens level=%d, integ level=%d\n"),
18443055Sdanmcd 	    prefix, sens->sadb_sens_dpd, sens->sadb_sens_sens_level,
18453055Sdanmcd 	    sens->sadb_sens_integ_level);
18463055Sdanmcd 	for (i = 0; sens->sadb_sens_sens_len-- > 0; i++, bitmap++)
18473055Sdanmcd 		(void) printf(
18484064Smarkfen 		    dgettext(TEXT_DOMAIN,
18494064Smarkfen 		    "%s Sensitivity BM extended word %d 0x%llx\n"), i, *bitmap);
18503055Sdanmcd 	for (i = 0; sens->sadb_sens_integ_len-- > 0; i++, bitmap++)
18513055Sdanmcd 		(void) printf(
18524064Smarkfen 		    dgettext(TEXT_DOMAIN,
18534064Smarkfen 		    "%s Integrity BM extended word %d 0x%llx\n"), i, *bitmap);
18543055Sdanmcd }
18553055Sdanmcd 
18563055Sdanmcd /*
18573055Sdanmcd  * Print an SADB_EXT_PROPOSAL extension.
18583055Sdanmcd  */
18593055Sdanmcd void
18603055Sdanmcd print_prop(char *prefix, struct sadb_prop *prop)
18613055Sdanmcd {
18623055Sdanmcd 	struct sadb_comb *combs;
18633055Sdanmcd 	int i, numcombs;
18643055Sdanmcd 
18654064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
18664064Smarkfen 	    "%sProposal, replay counter = %u.\n"), prefix,
18673055Sdanmcd 	    prop->sadb_prop_replay);
18683055Sdanmcd 
18693055Sdanmcd 	numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop));
18703055Sdanmcd 	numcombs /= SADB_8TO64(sizeof (*combs));
18713055Sdanmcd 
18723055Sdanmcd 	combs = (struct sadb_comb *)(prop + 1);
18733055Sdanmcd 
18743055Sdanmcd 	for (i = 0; i < numcombs; i++) {
18754064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
18764064Smarkfen 		    "%s Combination #%u "), prefix, i + 1);
18773055Sdanmcd 		if (combs[i].sadb_comb_auth != SADB_AALG_NONE) {
18784064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
18794064Smarkfen 			    "Authentication = "));
18803055Sdanmcd 			(void) dump_aalg(combs[i].sadb_comb_auth, stdout);
18814064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
18824064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
18833055Sdanmcd 			    combs[i].sadb_comb_auth_minbits,
18843055Sdanmcd 			    combs[i].sadb_comb_auth_maxbits, prefix);
18853055Sdanmcd 		}
18863055Sdanmcd 
18873055Sdanmcd 		if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) {
18884064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "Encryption = "));
18893055Sdanmcd 			(void) dump_ealg(combs[i].sadb_comb_encrypt, stdout);
18904064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
18914064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
18923055Sdanmcd 			    combs[i].sadb_comb_encrypt_minbits,
18933055Sdanmcd 			    combs[i].sadb_comb_encrypt_maxbits, prefix);
18943055Sdanmcd 		}
18953055Sdanmcd 
18964064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "HARD: "));
18973055Sdanmcd 		if (combs[i].sadb_comb_hard_allocations)
18984064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "alloc=%u "),
18993055Sdanmcd 			    combs[i].sadb_comb_hard_allocations);
19003055Sdanmcd 		if (combs[i].sadb_comb_hard_bytes)
19014064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "bytes=%llu "),
19023055Sdanmcd 			    combs[i].sadb_comb_hard_bytes);
19033055Sdanmcd 		if (combs[i].sadb_comb_hard_addtime)
19044064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
19054064Smarkfen 			    "post-add secs=%llu "),
19063055Sdanmcd 			    combs[i].sadb_comb_hard_addtime);
19073055Sdanmcd 		if (combs[i].sadb_comb_hard_usetime)
19084064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
19094064Smarkfen 			    "post-use secs=%llu"),
19103055Sdanmcd 			    combs[i].sadb_comb_hard_usetime);
19113055Sdanmcd 
19124064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "\n%s SOFT: "), prefix);
19133055Sdanmcd 		if (combs[i].sadb_comb_soft_allocations)
19144064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "alloc=%u "),
19153055Sdanmcd 			    combs[i].sadb_comb_soft_allocations);
19163055Sdanmcd 		if (combs[i].sadb_comb_soft_bytes)
19174064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "bytes=%llu "),
19183055Sdanmcd 			    combs[i].sadb_comb_soft_bytes);
19193055Sdanmcd 		if (combs[i].sadb_comb_soft_addtime)
19204064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
19214064Smarkfen 			    "post-add secs=%llu "),
19223055Sdanmcd 			    combs[i].sadb_comb_soft_addtime);
19233055Sdanmcd 		if (combs[i].sadb_comb_soft_usetime)
19244064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
19254064Smarkfen 			    "post-use secs=%llu"),
19263055Sdanmcd 			    combs[i].sadb_comb_soft_usetime);
19273055Sdanmcd 		(void) putchar('\n');
19283055Sdanmcd 	}
19293055Sdanmcd }
19303055Sdanmcd 
19313055Sdanmcd /*
19323055Sdanmcd  * Print an extended proposal (SADB_X_EXT_EPROP).
19333055Sdanmcd  */
19343055Sdanmcd void
19353055Sdanmcd print_eprop(char *prefix, struct sadb_prop *eprop)
19363055Sdanmcd {
19373055Sdanmcd 	uint64_t *sofar;
19383055Sdanmcd 	struct sadb_x_ecomb *ecomb;
19393055Sdanmcd 	struct sadb_x_algdesc *algdesc;
19403055Sdanmcd 	int i, j;
19413055Sdanmcd 
19424064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
19434064Smarkfen 	    "%sExtended Proposal, replay counter = %u, "), prefix,
19444064Smarkfen 	    eprop->sadb_prop_replay);
19454064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, "number of combinations = %u.\n"),
19463055Sdanmcd 	    eprop->sadb_x_prop_numecombs);
19473055Sdanmcd 
19483055Sdanmcd 	sofar = (uint64_t *)(eprop + 1);
19493055Sdanmcd 	ecomb = (struct sadb_x_ecomb *)sofar;
19503055Sdanmcd 
19513055Sdanmcd 	for (i = 0; i < eprop->sadb_x_prop_numecombs; ) {
19524064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
19534064Smarkfen 		    "%s Extended combination #%u:\n"), prefix, ++i);
19543055Sdanmcd 
19554064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "%s HARD: "), prefix);
19564064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "alloc=%u, "),
19573055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_allocations);
19584064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "bytes=%llu, "),
19593055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_bytes);
19604064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "post-add secs=%llu, "),
19613055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_addtime);
19624064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "post-use secs=%llu\n"),
19633055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_usetime);
19643055Sdanmcd 
19654064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "%s SOFT: "), prefix);
19664064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "alloc=%u, "),
19673055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_allocations);
19684064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "bytes=%llu, "),
19693055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_bytes);
19704064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "post-add secs=%llu, "),
19713055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_addtime);
19724064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "post-use secs=%llu\n"),
19733055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_usetime);
19743055Sdanmcd 
19753055Sdanmcd 		sofar = (uint64_t *)(ecomb + 1);
19763055Sdanmcd 		algdesc = (struct sadb_x_algdesc *)sofar;
19773055Sdanmcd 
19783055Sdanmcd 		for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) {
19794064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
19804064Smarkfen 			    "%s Alg #%u "), prefix, ++j);
19813055Sdanmcd 			switch (algdesc->sadb_x_algdesc_satype) {
19823055Sdanmcd 			case SADB_SATYPE_ESP:
19834064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
19844064Smarkfen 				    "for ESP "));
19853055Sdanmcd 				break;
19863055Sdanmcd 			case SADB_SATYPE_AH:
19874064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN, "for AH "));
19883055Sdanmcd 				break;
19893055Sdanmcd 			default:
19904064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
19914064Smarkfen 				    "for satype=%d "),
19923055Sdanmcd 				    algdesc->sadb_x_algdesc_satype);
19933055Sdanmcd 			}
19943055Sdanmcd 			switch (algdesc->sadb_x_algdesc_algtype) {
19953055Sdanmcd 			case SADB_X_ALGTYPE_CRYPT:
19964064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
19974064Smarkfen 				    "Encryption = "));
19983055Sdanmcd 				(void) dump_ealg(algdesc->sadb_x_algdesc_alg,
19993055Sdanmcd 				    stdout);
20003055Sdanmcd 				break;
20013055Sdanmcd 			case SADB_X_ALGTYPE_AUTH:
20024064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
20034064Smarkfen 				    "Authentication = "));
20043055Sdanmcd 				(void) dump_aalg(algdesc->sadb_x_algdesc_alg,
20053055Sdanmcd 				    stdout);
20063055Sdanmcd 				break;
20073055Sdanmcd 			default:
20084064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
20094064Smarkfen 				    "algtype(%d) = alg(%d)"),
20103055Sdanmcd 				    algdesc->sadb_x_algdesc_algtype,
20113055Sdanmcd 				    algdesc->sadb_x_algdesc_alg);
20123055Sdanmcd 				break;
20133055Sdanmcd 			}
20143055Sdanmcd 
20154064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
20164064Smarkfen 			    "  minbits=%u, maxbits=%u.\n"),
20173055Sdanmcd 			    algdesc->sadb_x_algdesc_minbits,
20183055Sdanmcd 			    algdesc->sadb_x_algdesc_maxbits);
20193055Sdanmcd 
20203055Sdanmcd 			sofar = (uint64_t *)(++algdesc);
20213055Sdanmcd 		}
20223055Sdanmcd 		ecomb = (struct sadb_x_ecomb *)sofar;
20233055Sdanmcd 	}
20243055Sdanmcd }
20253055Sdanmcd 
20263055Sdanmcd /*
20273055Sdanmcd  * Print an SADB_EXT_SUPPORTED extension.
20283055Sdanmcd  */
20293055Sdanmcd void
20303055Sdanmcd print_supp(char *prefix, struct sadb_supported *supp)
20313055Sdanmcd {
20323055Sdanmcd 	struct sadb_alg *algs;
20333055Sdanmcd 	int i, numalgs;
20343055Sdanmcd 
20354064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, "%sSupported "), prefix);
20363055Sdanmcd 	switch (supp->sadb_supported_exttype) {
20373055Sdanmcd 	case SADB_EXT_SUPPORTED_AUTH:
20384064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "authentication"));
20393055Sdanmcd 		break;
20403055Sdanmcd 	case SADB_EXT_SUPPORTED_ENCRYPT:
20414064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "encryption"));
20423055Sdanmcd 		break;
20433055Sdanmcd 	}
20444064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, " algorithms.\n"));
20453055Sdanmcd 
20463055Sdanmcd 	algs = (struct sadb_alg *)(supp + 1);
20473055Sdanmcd 	numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp));
20483055Sdanmcd 	numalgs /= SADB_8TO64(sizeof (*algs));
20493055Sdanmcd 	for (i = 0; i < numalgs; i++) {
20503055Sdanmcd 		(void) printf("%s", prefix);
20513055Sdanmcd 		switch (supp->sadb_supported_exttype) {
20523055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
20533055Sdanmcd 			(void) dump_aalg(algs[i].sadb_alg_id, stdout);
20543055Sdanmcd 			break;
20553055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
20563055Sdanmcd 			(void) dump_ealg(algs[i].sadb_alg_id, stdout);
20573055Sdanmcd 			break;
20583055Sdanmcd 		}
20594064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
20604064Smarkfen 		    " minbits=%u, maxbits=%u, ivlen=%u.\n"),
20613055Sdanmcd 		    algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits,
20623055Sdanmcd 		    algs[i].sadb_alg_ivlen);
20633055Sdanmcd 	}
20643055Sdanmcd }
20653055Sdanmcd 
20663055Sdanmcd /*
20673055Sdanmcd  * Print an SADB_EXT_SPIRANGE extension.
20683055Sdanmcd  */
20693055Sdanmcd void
20703055Sdanmcd print_spirange(char *prefix, struct sadb_spirange *range)
20713055Sdanmcd {
20724064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
20734064Smarkfen 	    "%sSPI Range, min=0x%x, max=0x%x\n"), prefix,
20743055Sdanmcd 	    htonl(range->sadb_spirange_min),
20753055Sdanmcd 	    htonl(range->sadb_spirange_max));
20763055Sdanmcd }
20773055Sdanmcd 
20783055Sdanmcd /*
20793055Sdanmcd  * Print an SADB_X_EXT_KM_COOKIE extension.
20803055Sdanmcd  */
20813055Sdanmcd 
20823055Sdanmcd void
20833055Sdanmcd print_kmc(char *prefix, struct sadb_x_kmc *kmc)
20843055Sdanmcd {
20853055Sdanmcd 	char *cookie_label;
20863055Sdanmcd 
20873055Sdanmcd 	if ((cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie)) ==
20883055Sdanmcd 	    NULL)
20894064Smarkfen 		cookie_label = dgettext(TEXT_DOMAIN, "<Label not found.>");
20903055Sdanmcd 
20914064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
20924064Smarkfen 	    "%sProtocol %u, cookie=\"%s\" (%u)\n"), prefix,
20933055Sdanmcd 	    kmc->sadb_x_kmc_proto, cookie_label, kmc->sadb_x_kmc_cookie);
20943055Sdanmcd }
20953055Sdanmcd 
20963055Sdanmcd /*
20973055Sdanmcd  * Take a PF_KEY message pointed to buffer and print it.  Useful for DUMP
20983055Sdanmcd  * and GET.
20993055Sdanmcd  */
21003055Sdanmcd void
21013055Sdanmcd print_samsg(uint64_t *buffer, boolean_t want_timestamp, boolean_t vflag)
21023055Sdanmcd {
21033055Sdanmcd 	uint64_t *current;
21043055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
21053055Sdanmcd 	struct sadb_ext *ext;
21063055Sdanmcd 	struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL;
21073055Sdanmcd 	int i;
21083055Sdanmcd 	time_t wallclock;
21093055Sdanmcd 
21103055Sdanmcd 	(void) time(&wallclock);
21113055Sdanmcd 
21123055Sdanmcd 	print_sadb_msg(samsg, want_timestamp ? wallclock : 0, vflag);
21133055Sdanmcd 	current = (uint64_t *)(samsg + 1);
21143055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
21153055Sdanmcd 		int lenbytes;
21163055Sdanmcd 
21173055Sdanmcd 		ext = (struct sadb_ext *)current;
21183055Sdanmcd 		lenbytes = SADB_64TO8(ext->sadb_ext_len);
21193055Sdanmcd 		switch (ext->sadb_ext_type) {
21203055Sdanmcd 		case SADB_EXT_SA:
21214064Smarkfen 			print_sa(dgettext(TEXT_DOMAIN,
21224064Smarkfen 			    "SA: "), (struct sadb_sa *)current);
21233055Sdanmcd 			break;
21243055Sdanmcd 		/*
21253055Sdanmcd 		 * Pluck out lifetimes and print them at the end.  This is
21263055Sdanmcd 		 * to show relative lifetimes.
21273055Sdanmcd 		 */
21283055Sdanmcd 		case SADB_EXT_LIFETIME_CURRENT:
21293055Sdanmcd 			currentlt = (struct sadb_lifetime *)current;
21303055Sdanmcd 			break;
21313055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
21323055Sdanmcd 			hardlt = (struct sadb_lifetime *)current;
21333055Sdanmcd 			break;
21343055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
21353055Sdanmcd 			softlt = (struct sadb_lifetime *)current;
21363055Sdanmcd 			break;
21373055Sdanmcd 
21383055Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
21394064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "SRC: "),
21403055Sdanmcd 			    (struct sadb_address *)current);
21413055Sdanmcd 			break;
21423055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
21434064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "INS: "),
21443055Sdanmcd 			    (struct sadb_address *)current);
21453055Sdanmcd 			break;
21463055Sdanmcd 		case SADB_EXT_ADDRESS_DST:
21474064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "DST: "),
21483055Sdanmcd 			    (struct sadb_address *)current);
21493055Sdanmcd 			break;
21503055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
21514064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "IND: "),
21523055Sdanmcd 			    (struct sadb_address *)current);
21533055Sdanmcd 			break;
21543055Sdanmcd 		case SADB_EXT_KEY_AUTH:
21554064Smarkfen 			print_key(dgettext(TEXT_DOMAIN,
21564064Smarkfen 			    "AKY: "), (struct sadb_key *)current);
21573055Sdanmcd 			break;
21583055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
21594064Smarkfen 			print_key(dgettext(TEXT_DOMAIN,
21604064Smarkfen 			    "EKY: "), (struct sadb_key *)current);
21613055Sdanmcd 			break;
21623055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
21634064Smarkfen 			print_ident(dgettext(TEXT_DOMAIN, "SID: "),
21643055Sdanmcd 			    (struct sadb_ident *)current);
21653055Sdanmcd 			break;
21663055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
21674064Smarkfen 			print_ident(dgettext(TEXT_DOMAIN, "DID: "),
21683055Sdanmcd 			    (struct sadb_ident *)current);
21693055Sdanmcd 			break;
21703055Sdanmcd 		case SADB_EXT_SENSITIVITY:
21714064Smarkfen 			print_sens(dgettext(TEXT_DOMAIN, "SNS: "),
21723055Sdanmcd 			    (struct sadb_sens *)current);
21733055Sdanmcd 			break;
21743055Sdanmcd 		case SADB_EXT_PROPOSAL:
21754064Smarkfen 			print_prop(dgettext(TEXT_DOMAIN, "PRP: "),
21763055Sdanmcd 			    (struct sadb_prop *)current);
21773055Sdanmcd 			break;
21783055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
21794064Smarkfen 			print_supp(dgettext(TEXT_DOMAIN, "SUA: "),
21803055Sdanmcd 			    (struct sadb_supported *)current);
21813055Sdanmcd 			break;
21823055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
21834064Smarkfen 			print_supp(dgettext(TEXT_DOMAIN, "SUE: "),
21843055Sdanmcd 			    (struct sadb_supported *)current);
21853055Sdanmcd 			break;
21863055Sdanmcd 		case SADB_EXT_SPIRANGE:
21874064Smarkfen 			print_spirange(dgettext(TEXT_DOMAIN, "SPR: "),
21883055Sdanmcd 			    (struct sadb_spirange *)current);
21893055Sdanmcd 			break;
21903055Sdanmcd 		case SADB_X_EXT_EPROP:
21914064Smarkfen 			print_eprop(dgettext(TEXT_DOMAIN, "EPR: "),
21923055Sdanmcd 			    (struct sadb_prop *)current);
21933055Sdanmcd 			break;
21943055Sdanmcd 		case SADB_X_EXT_KM_COOKIE:
21954064Smarkfen 			print_kmc(dgettext(TEXT_DOMAIN, "KMC: "),
21963055Sdanmcd 			    (struct sadb_x_kmc *)current);
21973055Sdanmcd 			break;
21983055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
21994064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "NRM: "),
22003055Sdanmcd 			    (struct sadb_address *)current);
22013055Sdanmcd 			break;
22023055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
22034064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "NLC: "),
22043055Sdanmcd 			    (struct sadb_address *)current);
22053055Sdanmcd 			break;
22063055Sdanmcd 		default:
22074064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
22083055Sdanmcd 			    "UNK: Unknown ext. %d, len %d.\n"),
22093055Sdanmcd 			    ext->sadb_ext_type, lenbytes);
22103055Sdanmcd 			for (i = 0; i < ext->sadb_ext_len; i++)
22114064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
22124064Smarkfen 				    "UNK: 0x%llx\n"), ((uint64_t *)ext)[i]);
22133055Sdanmcd 			break;
22143055Sdanmcd 		}
22153055Sdanmcd 		current += (lenbytes == 0) ?
22163055Sdanmcd 		    SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len;
22173055Sdanmcd 	}
22183055Sdanmcd 	/*
22193055Sdanmcd 	 * Print lifetimes NOW.
22203055Sdanmcd 	 */
22213055Sdanmcd 	if (currentlt != NULL || hardlt != NULL || softlt != NULL)
22223055Sdanmcd 		print_lifetimes(wallclock, currentlt, hardlt, softlt, vflag);
22233055Sdanmcd 
22243055Sdanmcd 	if (current - buffer != samsg->sadb_msg_len) {
22254064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "WARNING: insufficient buffer "
22264342Spwernau 		    "space or corrupt message."));
22273055Sdanmcd 	}
22283055Sdanmcd 
22293055Sdanmcd 	(void) fflush(stdout);	/* Make sure our message is out there. */
22303055Sdanmcd }
22313055Sdanmcd 
22323055Sdanmcd /*
22333055Sdanmcd  * save_XXX functions are used when "saving" the SA tables to either a
22343055Sdanmcd  * file or standard output.  They use the dump_XXX functions where needed,
22353055Sdanmcd  * but mostly they use the rparseXXX functions.
22363055Sdanmcd  */
22373055Sdanmcd 
22383055Sdanmcd /*
22393055Sdanmcd  * Print save information for a lifetime extension.
22403055Sdanmcd  *
22413055Sdanmcd  * NOTE : It saves the lifetime in absolute terms.  For example, if you
22423055Sdanmcd  * had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though
22433055Sdanmcd  * there may have been 59 seconds burned off the clock.
22443055Sdanmcd  */
22453055Sdanmcd boolean_t
22463055Sdanmcd save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile)
22473055Sdanmcd {
22483055Sdanmcd 	char *prefix;
22493055Sdanmcd 
22503055Sdanmcd 	prefix = (lifetime->sadb_lifetime_exttype == SADB_EXT_LIFETIME_SOFT) ?
22513055Sdanmcd 	    "soft" : "hard";
22523055Sdanmcd 
22533055Sdanmcd 	if (putc('\t', ofile) == EOF)
22543055Sdanmcd 		return (B_FALSE);
22553055Sdanmcd 
22563055Sdanmcd 	if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile,
22573055Sdanmcd 	    "%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0)
22583055Sdanmcd 		return (B_FALSE);
22593055Sdanmcd 
22603055Sdanmcd 	if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile,
22613055Sdanmcd 	    "%s_bytes %llu ", prefix, lifetime->sadb_lifetime_bytes) < 0)
22623055Sdanmcd 		return (B_FALSE);
22633055Sdanmcd 
22643055Sdanmcd 	if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile,
22653055Sdanmcd 	    "%s_addtime %llu ", prefix, lifetime->sadb_lifetime_addtime) < 0)
22663055Sdanmcd 		return (B_FALSE);
22673055Sdanmcd 
22683055Sdanmcd 	if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile,
22693055Sdanmcd 	    "%s_usetime %llu ", prefix, lifetime->sadb_lifetime_usetime) < 0)
22703055Sdanmcd 		return (B_FALSE);
22713055Sdanmcd 
22723055Sdanmcd 	return (B_TRUE);
22733055Sdanmcd }
22743055Sdanmcd 
22753055Sdanmcd /*
22763055Sdanmcd  * Print save information for an address extension.
22773055Sdanmcd  */
22783055Sdanmcd boolean_t
22793055Sdanmcd save_address(struct sadb_address *addr, FILE *ofile)
22803055Sdanmcd {
22813055Sdanmcd 	char *printable_addr, buf[INET6_ADDRSTRLEN];
22823055Sdanmcd 	const char *prefix, *pprefix;
22833055Sdanmcd 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1);
22843055Sdanmcd 	struct sockaddr_in *sin = (struct sockaddr_in *)sin6;
22853055Sdanmcd 	int af = sin->sin_family;
22863055Sdanmcd 
22873055Sdanmcd 	/*
22883055Sdanmcd 	 * Address-family reality check.
22893055Sdanmcd 	 */
22903055Sdanmcd 	if (af != AF_INET6 && af != AF_INET)
22913055Sdanmcd 		return (B_FALSE);
22923055Sdanmcd 
22933055Sdanmcd 	switch (addr->sadb_address_exttype) {
22943055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
22953055Sdanmcd 		prefix = "src";
22963055Sdanmcd 		pprefix = "sport";
22973055Sdanmcd 		break;
22983055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
22993055Sdanmcd 		prefix = "isrc";
23003055Sdanmcd 		pprefix = "isport";
23013055Sdanmcd 		break;
23023055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
23033055Sdanmcd 		prefix = "dst";
23043055Sdanmcd 		pprefix = "dport";
23053055Sdanmcd 		break;
23063055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
23073055Sdanmcd 		prefix = "idst";
23083055Sdanmcd 		pprefix = "idport";
23093055Sdanmcd 		break;
23103055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
23113055Sdanmcd 		prefix = "nat_loc ";
23123055Sdanmcd 		pprefix = "nat_lport";
23133055Sdanmcd 		break;
23143055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
23153055Sdanmcd 		prefix = "nat_rem ";
23163055Sdanmcd 		pprefix = "nat_rport";
23173055Sdanmcd 		break;
23183055Sdanmcd 	}
23193055Sdanmcd 
23203055Sdanmcd 	if (fprintf(ofile, "    %s ", prefix) < 0)
23213055Sdanmcd 		return (B_FALSE);
23223055Sdanmcd 
23233055Sdanmcd 	/*
23243055Sdanmcd 	 * Do not do address-to-name translation, given that we live in
23253055Sdanmcd 	 * an age of names that explode into many addresses.
23263055Sdanmcd 	 */
23273055Sdanmcd 	printable_addr = (char *)inet_ntop(af,
23283055Sdanmcd 	    (af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr,
23293055Sdanmcd 	    buf, sizeof (buf));
23303055Sdanmcd 	if (printable_addr == NULL)
23314064Smarkfen 		printable_addr = "Invalid IP address.";
23323055Sdanmcd 	if (fprintf(ofile, "%s", printable_addr) < 0)
23333055Sdanmcd 		return (B_FALSE);
23343055Sdanmcd 	if (addr->sadb_address_prefixlen != 0 &&
23353055Sdanmcd 	    !((addr->sadb_address_prefixlen == 32 && af == AF_INET) ||
23364342Spwernau 	    (addr->sadb_address_prefixlen == 128 && af == AF_INET6))) {
23373055Sdanmcd 		if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0)
23383055Sdanmcd 			return (B_FALSE);
23393055Sdanmcd 	}
23403055Sdanmcd 
23413055Sdanmcd 	/*
23423055Sdanmcd 	 * The port is in the same position for struct sockaddr_in and
23433055Sdanmcd 	 * struct sockaddr_in6.  We exploit that property here.
23443055Sdanmcd 	 */
23453055Sdanmcd 	if ((pprefix != NULL) && (sin->sin_port != 0))
23463055Sdanmcd 		(void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port));
23473055Sdanmcd 
23483055Sdanmcd 	return (B_TRUE);
23493055Sdanmcd }
23503055Sdanmcd 
23513055Sdanmcd /*
23523055Sdanmcd  * Print save information for a key extension. Returns whether writing
23533055Sdanmcd  * to the specified output file was successful or not.
23543055Sdanmcd  */
23553055Sdanmcd boolean_t
23563055Sdanmcd save_key(struct sadb_key *key, FILE *ofile)
23573055Sdanmcd {
23583055Sdanmcd 	char *prefix;
23593055Sdanmcd 
23603055Sdanmcd 	if (putc('\t', ofile) == EOF)
23613055Sdanmcd 		return (B_FALSE);
23623055Sdanmcd 
23633055Sdanmcd 	prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr";
23643055Sdanmcd 
23653055Sdanmcd 	if (fprintf(ofile, "%skey ", prefix) < 0)
23663055Sdanmcd 		return (B_FALSE);
23673055Sdanmcd 
23683055Sdanmcd 	if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits, ofile) == -1)
23693055Sdanmcd 		return (B_FALSE);
23703055Sdanmcd 
23713055Sdanmcd 	return (B_TRUE);
23723055Sdanmcd }
23733055Sdanmcd 
23743055Sdanmcd /*
23753055Sdanmcd  * Print save information for an identity extension.
23763055Sdanmcd  */
23773055Sdanmcd boolean_t
23783055Sdanmcd save_ident(struct sadb_ident *ident, FILE *ofile)
23793055Sdanmcd {
23803055Sdanmcd 	char *prefix;
23813055Sdanmcd 
23823055Sdanmcd 	if (putc('\t', ofile) == EOF)
23833055Sdanmcd 		return (B_FALSE);
23843055Sdanmcd 
23853055Sdanmcd 	prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" :
23863055Sdanmcd 	    "dst";
23873055Sdanmcd 
23883055Sdanmcd 	if (fprintf(ofile, "%sidtype %s ", prefix,
23893055Sdanmcd 	    rparseidtype(ident->sadb_ident_type)) < 0)
23903055Sdanmcd 		return (B_FALSE);
23913055Sdanmcd 
23923055Sdanmcd 	if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN ||
23933055Sdanmcd 	    ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) {
23944064Smarkfen 		if (fprintf(ofile, dgettext(TEXT_DOMAIN,
23954064Smarkfen 		    "<can-not-print>")) < 0)
23963055Sdanmcd 			return (B_FALSE);
23973055Sdanmcd 	} else {
23983055Sdanmcd 		if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0)
23993055Sdanmcd 			return (B_FALSE);
24003055Sdanmcd 	}
24013055Sdanmcd 
24023055Sdanmcd 	return (B_TRUE);
24033055Sdanmcd }
24043055Sdanmcd 
24053055Sdanmcd /*
24063055Sdanmcd  * "Save" a security association to an output file.
24073055Sdanmcd  *
24084064Smarkfen  * NOTE the lack of calls to dgettext() because I'm outputting parseable stuff.
24093055Sdanmcd  * ALSO NOTE that if you change keywords (see parsecmd()), you'll have to
24103055Sdanmcd  * change them here as well.
24113055Sdanmcd  */
24123055Sdanmcd void
24133055Sdanmcd save_assoc(uint64_t *buffer, FILE *ofile)
24143055Sdanmcd {
24154064Smarkfen 	int terrno;
24163055Sdanmcd 	int seen_proto = 0;
24173055Sdanmcd 	uint64_t *current;
24183055Sdanmcd 	struct sadb_address *addr;
24193055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
24203055Sdanmcd 	struct sadb_ext *ext;
24213055Sdanmcd 
24224064Smarkfen #define	tidyup() \
24234064Smarkfen 	terrno = errno; (void) fclose(ofile); errno = terrno; \
24244064Smarkfen 	interactive = B_FALSE
24254064Smarkfen 
24264064Smarkfen #define	savenl() if (fputs(" \\\n", ofile) == EOF) \
24274064Smarkfen 	{ bail(dgettext(TEXT_DOMAIN, "savenl")); }
24283055Sdanmcd 
24293055Sdanmcd 	if (fputs("# begin assoc\n", ofile) == EOF)
24304064Smarkfen 		bail(dgettext(TEXT_DOMAIN,
24314064Smarkfen 		    "save_assoc: Opening comment of SA"));
24323055Sdanmcd 	if (fprintf(ofile, "add %s ", rparsesatype(samsg->sadb_msg_satype)) < 0)
24334064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: First line of SA"));
24343055Sdanmcd 	savenl();
24353055Sdanmcd 
24363055Sdanmcd 	current = (uint64_t *)(samsg + 1);
24373055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
24383055Sdanmcd 		struct sadb_sa *assoc;
24393055Sdanmcd 
24403055Sdanmcd 		ext = (struct sadb_ext *)current;
24413055Sdanmcd 		switch (ext->sadb_ext_type) {
24423055Sdanmcd 		case SADB_EXT_SA:
24433055Sdanmcd 			assoc = (struct sadb_sa *)ext;
24443055Sdanmcd 			if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
24453055Sdanmcd 				if (fprintf(ofile, "# WARNING: SA was dying "
24463055Sdanmcd 				    "or dead.\n") < 0) {
24474064Smarkfen 					tidyup();
24484064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24494064Smarkfen 					    "save_assoc: fprintf not mature"));
24503055Sdanmcd 				}
24513055Sdanmcd 			}
24523055Sdanmcd 			if (fprintf(ofile, "    spi 0x%x ",
24534064Smarkfen 			    ntohl(assoc->sadb_sa_spi)) < 0) {
24544064Smarkfen 				tidyup();
24554064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
24564064Smarkfen 				    "save_assoc: fprintf spi"));
24574064Smarkfen 			}
24583055Sdanmcd 			if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
24593055Sdanmcd 				if (fprintf(ofile, "encr_alg %s ",
24603055Sdanmcd 				    rparsealg(assoc->sadb_sa_encrypt,
24614342Spwernau 				    IPSEC_PROTO_ESP)) < 0) {
24624064Smarkfen 					tidyup();
24634064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24644064Smarkfen 					    "save_assoc: fprintf encrypt"));
24654064Smarkfen 				}
24663055Sdanmcd 			}
24673055Sdanmcd 			if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
24683055Sdanmcd 				if (fprintf(ofile, "auth_alg %s ",
24693055Sdanmcd 				    rparsealg(assoc->sadb_sa_auth,
24704342Spwernau 				    IPSEC_PROTO_AH)) < 0) {
24714064Smarkfen 					tidyup();
24724064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24734064Smarkfen 					    "save_assoc: fprintf auth"));
24744064Smarkfen 				}
24753055Sdanmcd 			}
24763055Sdanmcd 			if (fprintf(ofile, "replay %d ",
24774064Smarkfen 			    assoc->sadb_sa_replay) < 0) {
24784064Smarkfen 				tidyup();
24794064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
24804064Smarkfen 				    "save_assoc: fprintf replay"));
24814064Smarkfen 			}
24823055Sdanmcd 			if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC |
24833055Sdanmcd 			    SADB_X_SAFLAGS_NATT_REM)) {
24844064Smarkfen 				if (fprintf(ofile, "encap udp") < 0) {
24854064Smarkfen 					tidyup();
24864064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24874064Smarkfen 					    "save_assoc: fprintf encap"));
24884064Smarkfen 				}
24893055Sdanmcd 			}
24903055Sdanmcd 			savenl();
24913055Sdanmcd 			break;
24923055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
24933055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
24944064Smarkfen 			if (!save_lifetime((struct sadb_lifetime *)ext,
24954064Smarkfen 			    ofile)) {
24964064Smarkfen 				tidyup();
24974064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_lifetime"));
24984064Smarkfen 			}
24993055Sdanmcd 			savenl();
25003055Sdanmcd 			break;
25013055Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
25023055Sdanmcd 		case SADB_EXT_ADDRESS_DST:
25033055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
25043055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
25053055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
25063055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
25073055Sdanmcd 			addr = (struct sadb_address *)ext;
25083055Sdanmcd 			if (!seen_proto && addr->sadb_address_proto) {
25093055Sdanmcd 				(void) fprintf(ofile, "    proto %d",
25103055Sdanmcd 				    addr->sadb_address_proto);
25113055Sdanmcd 				savenl();
25123055Sdanmcd 				seen_proto = 1;
25133055Sdanmcd 			}
25144064Smarkfen 			if (!save_address(addr, ofile)) {
25154064Smarkfen 				tidyup();
25164064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
25174064Smarkfen 			}
25183055Sdanmcd 			savenl();
25193055Sdanmcd 			break;
25203055Sdanmcd 		case SADB_EXT_KEY_AUTH:
25213055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
25224064Smarkfen 			if (!save_key((struct sadb_key *)ext, ofile)) {
25234064Smarkfen 				tidyup();
25244064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
25254064Smarkfen 			}
25263055Sdanmcd 			savenl();
25273055Sdanmcd 			break;
25283055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
25293055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
25304064Smarkfen 			if (!save_ident((struct sadb_ident *)ext, ofile)) {
25314064Smarkfen 				tidyup();
25324064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
25334064Smarkfen 			}
25343055Sdanmcd 			savenl();
25353055Sdanmcd 			break;
25363055Sdanmcd 		case SADB_EXT_SENSITIVITY:
25373055Sdanmcd 		default:
25383055Sdanmcd 			/* Skip over irrelevant extensions. */
25393055Sdanmcd 			break;
25403055Sdanmcd 		}
25413055Sdanmcd 		current += ext->sadb_ext_len;
25423055Sdanmcd 	}
25433055Sdanmcd 
25444064Smarkfen 	if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) {
25454064Smarkfen 		tidyup();
25464064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs"));
25474064Smarkfen 	}
25483055Sdanmcd }
25493055Sdanmcd 
25503055Sdanmcd /*
25513055Sdanmcd  * Open the output file for the "save" command.
25523055Sdanmcd  */
25533055Sdanmcd FILE *
25543055Sdanmcd opensavefile(char *filename)
25553055Sdanmcd {
25563055Sdanmcd 	int fd;
25573055Sdanmcd 	FILE *retval;
25583055Sdanmcd 	struct stat buf;
25593055Sdanmcd 
25603055Sdanmcd 	/*
25613055Sdanmcd 	 * If the user specifies "-" or doesn't give a filename, then
25623055Sdanmcd 	 * dump to stdout.  Make sure to document the dangers of files
25633055Sdanmcd 	 * that are NFS, directing your output to strange places, etc.
25643055Sdanmcd 	 */
25653055Sdanmcd 	if (filename == NULL || strcmp("-", filename) == 0)
25663055Sdanmcd 		return (stdout);
25673055Sdanmcd 
25683055Sdanmcd 	/*
25693055Sdanmcd 	 * open the file with the create bits set.  Since I check for
25703055Sdanmcd 	 * real UID == root in main(), I won't worry about the ownership
25713055Sdanmcd 	 * problem.
25723055Sdanmcd 	 */
25733055Sdanmcd 	fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR);
25743055Sdanmcd 	if (fd == -1) {
25753055Sdanmcd 		if (errno != EEXIST)
25764064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
25774064Smarkfen 			    "open error"),
25783055Sdanmcd 			    strerror(errno));
25793055Sdanmcd 		fd = open(filename, O_WRONLY | O_TRUNC, 0);
25803055Sdanmcd 		if (fd == -1)
25814064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
25824064Smarkfen 			    "open error"), strerror(errno));
25833055Sdanmcd 		if (fstat(fd, &buf) == -1) {
25843055Sdanmcd 			(void) close(fd);
25853055Sdanmcd 			bail_msg("%s fstat: %s", filename, strerror(errno));
25863055Sdanmcd 		}
25873055Sdanmcd 		if (S_ISREG(buf.st_mode) &&
25883055Sdanmcd 		    ((buf.st_mode & S_IAMB) != S_IRUSR)) {
25894064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
25904064Smarkfen 			    "WARNING: Save file already exists with "
25914064Smarkfen 			    "permission %o."), buf.st_mode & S_IAMB);
25924064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
25934064Smarkfen 			    "Normal users may be able to read IPsec "
25944064Smarkfen 			    "keying material."));
25953055Sdanmcd 		}
25963055Sdanmcd 	}
25973055Sdanmcd 
25983055Sdanmcd 	/* Okay, we have an FD.  Assign it to a stdio FILE pointer. */
25993055Sdanmcd 	retval = fdopen(fd, "w");
26003055Sdanmcd 	if (retval == NULL) {
26013055Sdanmcd 		(void) close(fd);
26024064Smarkfen 		bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
26034064Smarkfen 		    "fdopen error"), strerror(errno));
26043055Sdanmcd 	}
26053055Sdanmcd 	return (retval);
26063055Sdanmcd }
26073055Sdanmcd 
26083055Sdanmcd const char *
26093055Sdanmcd do_inet_ntop(const void *addr, char *cp, size_t size)
26103055Sdanmcd {
26113055Sdanmcd 	boolean_t isv4;
26123055Sdanmcd 	struct in6_addr *inaddr6 = (struct in6_addr *)addr;
26133055Sdanmcd 	struct in_addr inaddr;
26143055Sdanmcd 
26153055Sdanmcd 	if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) {
26163055Sdanmcd 		IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr);
26173055Sdanmcd 	}
26183055Sdanmcd 
26193055Sdanmcd 	return (inet_ntop(isv4 ? AF_INET : AF_INET6,
26203055Sdanmcd 	    isv4 ? (void *)&inaddr : inaddr6, cp, size));
26213055Sdanmcd }
26223055Sdanmcd 
26233055Sdanmcd char numprint[NBUF_SIZE];
26243055Sdanmcd 
26253055Sdanmcd /*
26263055Sdanmcd  * Parse and reverse parse a specific SA type (AH, ESP, etc.).
26273055Sdanmcd  */
26283055Sdanmcd static struct typetable {
26293055Sdanmcd 	char *type;
26303055Sdanmcd 	int token;
26313055Sdanmcd } type_table[] = {
26323055Sdanmcd 	{"all", SADB_SATYPE_UNSPEC},
26333055Sdanmcd 	{"ah",  SADB_SATYPE_AH},
26343055Sdanmcd 	{"esp", SADB_SATYPE_ESP},
26353055Sdanmcd 	/* PF_KEY NOTE:  More to come if net/pfkeyv2.h gets updated. */
26363055Sdanmcd 	{NULL, 0}	/* Token value is irrelevant for this entry. */
26373055Sdanmcd };
26383055Sdanmcd 
26393055Sdanmcd char *
26403055Sdanmcd rparsesatype(int type)
26413055Sdanmcd {
26423055Sdanmcd 	struct typetable *tt = type_table;
26433055Sdanmcd 
26443055Sdanmcd 	while (tt->type != NULL && type != tt->token)
26453055Sdanmcd 		tt++;
26463055Sdanmcd 
26473055Sdanmcd 	if (tt->type == NULL) {
26483055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", type);
26493055Sdanmcd 	} else {
26503055Sdanmcd 		return (tt->type);
26513055Sdanmcd 	}
26523055Sdanmcd 
26533055Sdanmcd 	return (numprint);
26543055Sdanmcd }
26553055Sdanmcd 
26563055Sdanmcd 
26573055Sdanmcd /*
26583055Sdanmcd  * Return a string containing the name of the specified numerical algorithm
26593055Sdanmcd  * identifier.
26603055Sdanmcd  */
26613055Sdanmcd char *
26623055Sdanmcd rparsealg(uint8_t alg, int proto_num)
26633055Sdanmcd {
26643055Sdanmcd 	static struct ipsecalgent *holder = NULL; /* we're single-threaded */
26653055Sdanmcd 
26663055Sdanmcd 	if (holder != NULL)
26673055Sdanmcd 		freeipsecalgent(holder);
26683055Sdanmcd 
26693055Sdanmcd 	holder = getipsecalgbynum(alg, proto_num, NULL);
26703055Sdanmcd 	if (holder == NULL) {
26713055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", alg);
26723055Sdanmcd 		return (numprint);
26733055Sdanmcd 	}
26743055Sdanmcd 
26753055Sdanmcd 	return (*(holder->a_names));
26763055Sdanmcd }
26773055Sdanmcd 
26783055Sdanmcd /*
26793055Sdanmcd  * Parse and reverse parse out a source/destination ID type.
26803055Sdanmcd  */
26813055Sdanmcd static struct idtypes {
26823055Sdanmcd 	char *idtype;
26833055Sdanmcd 	uint8_t retval;
26843055Sdanmcd } idtypes[] = {
26853055Sdanmcd 	{"prefix",	SADB_IDENTTYPE_PREFIX},
26863055Sdanmcd 	{"fqdn",	SADB_IDENTTYPE_FQDN},
26873055Sdanmcd 	{"domain",	SADB_IDENTTYPE_FQDN},
26883055Sdanmcd 	{"domainname",	SADB_IDENTTYPE_FQDN},
26893055Sdanmcd 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
26903055Sdanmcd 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
26913055Sdanmcd 	{"der_dn",	SADB_X_IDENTTYPE_DN},
26923055Sdanmcd 	{"der_gn",	SADB_X_IDENTTYPE_GN},
26933055Sdanmcd 	{NULL,		0}
26943055Sdanmcd };
26953055Sdanmcd 
26963055Sdanmcd char *
26973055Sdanmcd rparseidtype(uint16_t type)
26983055Sdanmcd {
26993055Sdanmcd 	struct idtypes *idp;
27003055Sdanmcd 
27013055Sdanmcd 	for (idp = idtypes; idp->idtype != NULL; idp++) {
27023055Sdanmcd 		if (type == idp->retval)
27033055Sdanmcd 			return (idp->idtype);
27043055Sdanmcd 	}
27053055Sdanmcd 
27063055Sdanmcd 	(void) snprintf(numprint, NBUF_SIZE, "%d", type);
27073055Sdanmcd 	return (numprint);
27083055Sdanmcd }
27094235Smarkfen 
27104235Smarkfen /*
27114235Smarkfen  * This is a general purpose exit function, calling functions can specify an
27124235Smarkfen  * error type. If the command calling this function was started by smf(5) the
27134235Smarkfen  * error type could be used as a hint to the restarter. In the future this
27144235Smarkfen  * function could be used to do something more intelligent with a process that
27154235Smarkfen  * encounters an error.
27164235Smarkfen  *
27174235Smarkfen  * The function will handle an optional variable args error message, this
27184235Smarkfen  * will be written to the error stream, typically a log file or stderr.
27194235Smarkfen  */
27204235Smarkfen void
27214235Smarkfen ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...)
27224235Smarkfen {
27234235Smarkfen 	int exit_status;
27244235Smarkfen 	va_list args;
27254235Smarkfen 
27264235Smarkfen 	if (fp == NULL)
27274235Smarkfen 		fp = stderr;
27284235Smarkfen 	if (fmt != NULL) {
27294235Smarkfen 		va_start(args, fmt);
27304235Smarkfen 		vwarnxfp(fp, fmt, args);
27314235Smarkfen 		va_end(args);
27324235Smarkfen 	}
27334235Smarkfen 
27344235Smarkfen 	if (fmri == NULL) {
27354235Smarkfen 		/* Command being run directly from a shell. */
27364235Smarkfen 		switch (type) {
27374235Smarkfen 		case SERVICE_EXIT_OK:
27384235Smarkfen 			exit_status = 0;
27394235Smarkfen 			break;
27404235Smarkfen 		case SERVICE_DEGRADE:
27414235Smarkfen 			return;
27424235Smarkfen 			break;
27434235Smarkfen 		case SERVICE_BADPERM:
27444235Smarkfen 		case SERVICE_BADCONF:
27454235Smarkfen 		case SERVICE_MAINTAIN:
27464235Smarkfen 		case SERVICE_DISABLE:
27474235Smarkfen 		case SERVICE_FATAL:
27484235Smarkfen 		case SERVICE_RESTART:
27494235Smarkfen 			warnxfp(fp, "Fatal error - exiting.");
27504235Smarkfen 			exit_status = 1;
27514235Smarkfen 			break;
27524235Smarkfen 		}
27534235Smarkfen 	} else {
27544235Smarkfen 		/* Command being run as a smf(5) method. */
27554235Smarkfen 		switch (type) {
27564235Smarkfen 		case SERVICE_EXIT_OK:
27574235Smarkfen 			exit_status = SMF_EXIT_OK;
27584235Smarkfen 			break;
27594235Smarkfen 		case SERVICE_DEGRADE:
27604235Smarkfen 			return;
27614235Smarkfen 			break;
27624235Smarkfen 		case SERVICE_BADPERM:
27634235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
27644235Smarkfen 			    "Permission error with %s."), fmri);
27654235Smarkfen 			exit_status = SMF_EXIT_ERR_PERM;
27664235Smarkfen 			break;
27674235Smarkfen 		case SERVICE_BADCONF:
27684235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
27694235Smarkfen 			    "Bad configuration of service %s."), fmri);
27704235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
27714235Smarkfen 			break;
27724235Smarkfen 		case SERVICE_MAINTAIN:
27734235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
27744235Smarkfen 			    "Service %s needs maintenance."), fmri);
27754235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
27764235Smarkfen 			break;
27774235Smarkfen 		case SERVICE_DISABLE:
27784235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
27794235Smarkfen 			break;
27804235Smarkfen 		case SERVICE_FATAL:
27814235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
27824235Smarkfen 			    "Service %s fatal error."), fmri);
27834235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
27844235Smarkfen 			break;
27854235Smarkfen 		case SERVICE_RESTART:
27864235Smarkfen 			exit_status = 1;
27874235Smarkfen 			break;
27884235Smarkfen 		}
27894235Smarkfen 	}
27904235Smarkfen 	(void) fflush(fp);
27914235Smarkfen 	(void) fclose(fp);
27924235Smarkfen 	exit(exit_status);
27934235Smarkfen }
2794