xref: /onnv-gate/usr/src/lib/libipsecutil/common/ipsec_util.c (revision 11379:752a9bf31c52)
10Sstevel@tonic-gate /*
26119Spwernau  *
30Sstevel@tonic-gate  * CDDL HEADER START
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
63055Sdanmcd  * Common Development and Distribution License (the "License").
73055Sdanmcd  * You may not use this file except in compliance with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
239086SVladimir.Kotal@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <unistd.h>
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <stdarg.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <sys/sysconf.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <sys/socket.h>
390Sstevel@tonic-gate #include <netdb.h>
400Sstevel@tonic-gate #include <netinet/in.h>
410Sstevel@tonic-gate #include <arpa/inet.h>
420Sstevel@tonic-gate #include <net/pfkeyv2.h>
430Sstevel@tonic-gate #include <net/pfpolicy.h>
440Sstevel@tonic-gate #include <libintl.h>
450Sstevel@tonic-gate #include <setjmp.h>
460Sstevel@tonic-gate #include <libgen.h>
474235Smarkfen #include <libscf.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include "ipsec_util.h"
500Sstevel@tonic-gate #include "ikedoor.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * This file contains support functions that are shared by the ipsec
544867Spwernau  * utilities and daemons including ipseckey(1m), ikeadm(1m) and in.iked(1m).
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
574867Spwernau 
584867Spwernau #define	EFD(file) (((file) == stdout) ? stderr : (file))
594867Spwernau 
609086SVladimir.Kotal@Sun.COM /* Limits for interactive mode. */
619086SVladimir.Kotal@Sun.COM #define	MAX_LINE_LEN	IBUF_SIZE
629086SVladimir.Kotal@Sun.COM #define	MAX_CMD_HIST	64000	/* in bytes */
639086SVladimir.Kotal@Sun.COM 
640Sstevel@tonic-gate /* Set standard default/initial values for globals... */
650Sstevel@tonic-gate boolean_t pflag = B_FALSE;	/* paranoid w.r.t. printing keying material */
660Sstevel@tonic-gate boolean_t nflag = B_FALSE;	/* avoid nameservice? */
670Sstevel@tonic-gate boolean_t interactive = B_FALSE;	/* util not running on cmdline */
680Sstevel@tonic-gate boolean_t readfile = B_FALSE;	/* cmds are being read from a file */
690Sstevel@tonic-gate uint_t	lineno = 0;		/* track location if reading cmds from file */
704235Smarkfen uint_t	lines_added = 0;
714235Smarkfen uint_t	lines_parsed = 0;
720Sstevel@tonic-gate jmp_buf	env;		/* for error recovery in interactive/readfile modes */
734235Smarkfen char *my_fmri = NULL;
744235Smarkfen FILE *debugfile = stderr;
759086SVladimir.Kotal@Sun.COM static GetLine *gl = NULL;	/* for interactive mode */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Print errno and exit if cmdline or readfile, reset state if interactive
794064Smarkfen  * The error string *what should be dgettext()'d before calling bail().
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate void
bail(char * what)820Sstevel@tonic-gate bail(char *what)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	if (errno != 0)
850Sstevel@tonic-gate 		warn(what);
860Sstevel@tonic-gate 	else
874235Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "Error: %s"), what);
880Sstevel@tonic-gate 	if (readfile) {
894235Smarkfen 		return;
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 	if (interactive && !readfile)
920Sstevel@tonic-gate 		longjmp(env, 2);
934235Smarkfen 	EXIT_FATAL(NULL);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Print caller-supplied variable-arg error msg, then exit if cmdline or
980Sstevel@tonic-gate  * readfile, or reset state if interactive.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate /*PRINTFLIKE1*/
1010Sstevel@tonic-gate void
bail_msg(char * fmt,...)1020Sstevel@tonic-gate bail_msg(char *fmt, ...)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	va_list	ap;
1050Sstevel@tonic-gate 	char	msgbuf[BUFSIZ];
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	va_start(ap, fmt);
1080Sstevel@tonic-gate 	(void) vsnprintf(msgbuf, BUFSIZ, fmt, ap);
1090Sstevel@tonic-gate 	va_end(ap);
1100Sstevel@tonic-gate 	if (readfile)
1114064Smarkfen 		warnx(dgettext(TEXT_DOMAIN,
1124064Smarkfen 		    "ERROR on line %u:\n%s\n"), lineno,  msgbuf);
1130Sstevel@tonic-gate 	else
1144064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "ERROR: %s\n"), msgbuf);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (interactive && !readfile)
1170Sstevel@tonic-gate 		longjmp(env, 1);
1180Sstevel@tonic-gate 
1194235Smarkfen 	EXIT_FATAL(NULL);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
122*11379SVladimir.Kotal@Sun.COM /*
123*11379SVladimir.Kotal@Sun.COM  * bytecnt2str() wrapper. Zeroes out the input buffer and if the number
124*11379SVladimir.Kotal@Sun.COM  * of bytes to be converted is more than 1K, it will produce readable string
125*11379SVladimir.Kotal@Sun.COM  * in parentheses, store it in the original buffer and return the pointer to it.
126*11379SVladimir.Kotal@Sun.COM  * Maximum length of the returned string is 14 characters (not including
127*11379SVladimir.Kotal@Sun.COM  * the terminating zero).
128*11379SVladimir.Kotal@Sun.COM  */
129*11379SVladimir.Kotal@Sun.COM char *
bytecnt2out(uint64_t num,char * buf,size_t bufsiz,int flags)130*11379SVladimir.Kotal@Sun.COM bytecnt2out(uint64_t num, char *buf, size_t bufsiz, int flags)
131*11379SVladimir.Kotal@Sun.COM {
132*11379SVladimir.Kotal@Sun.COM 	char *str;
133*11379SVladimir.Kotal@Sun.COM 
134*11379SVladimir.Kotal@Sun.COM 	(void) memset(buf, '\0', bufsiz);
135*11379SVladimir.Kotal@Sun.COM 
136*11379SVladimir.Kotal@Sun.COM 	if (num > 1024) {
137*11379SVladimir.Kotal@Sun.COM 		/* Return empty string in case of out-of-memory. */
138*11379SVladimir.Kotal@Sun.COM 		if ((str = malloc(bufsiz)) == NULL)
139*11379SVladimir.Kotal@Sun.COM 			return (buf);
140*11379SVladimir.Kotal@Sun.COM 
141*11379SVladimir.Kotal@Sun.COM 		(void) bytecnt2str(num, str, bufsiz);
142*11379SVladimir.Kotal@Sun.COM 		/* Detect overflow. */
143*11379SVladimir.Kotal@Sun.COM 		if (strlen(str) == 0) {
144*11379SVladimir.Kotal@Sun.COM 			free(str);
145*11379SVladimir.Kotal@Sun.COM 			return (buf);
146*11379SVladimir.Kotal@Sun.COM 		}
147*11379SVladimir.Kotal@Sun.COM 
148*11379SVladimir.Kotal@Sun.COM 		/* Emit nothing in case of overflow. */
149*11379SVladimir.Kotal@Sun.COM 		if (snprintf(buf, bufsiz, "%s(%sB)%s",
150*11379SVladimir.Kotal@Sun.COM 		    flags & SPC_BEGIN ? " " : "", str,
151*11379SVladimir.Kotal@Sun.COM 		    flags & SPC_END ? " " : "") >= bufsiz)
152*11379SVladimir.Kotal@Sun.COM 			(void) memset(buf, '\0', bufsiz);
153*11379SVladimir.Kotal@Sun.COM 
154*11379SVladimir.Kotal@Sun.COM 		free(str);
155*11379SVladimir.Kotal@Sun.COM 	}
156*11379SVladimir.Kotal@Sun.COM 
157*11379SVladimir.Kotal@Sun.COM 	return (buf);
158*11379SVladimir.Kotal@Sun.COM }
159*11379SVladimir.Kotal@Sun.COM 
160*11379SVladimir.Kotal@Sun.COM /*
161*11379SVladimir.Kotal@Sun.COM  * Convert 64-bit number to human readable string. Useful mainly for the
162*11379SVladimir.Kotal@Sun.COM  * byte lifetime counters. Returns pointer to the user supplied buffer.
163*11379SVladimir.Kotal@Sun.COM  * Able to convert up to Exabytes. Maximum length of the string produced
164*11379SVladimir.Kotal@Sun.COM  * is 9 characters (not counting the terminating zero).
165*11379SVladimir.Kotal@Sun.COM  */
166*11379SVladimir.Kotal@Sun.COM char *
bytecnt2str(uint64_t num,char * buf,size_t buflen)167*11379SVladimir.Kotal@Sun.COM bytecnt2str(uint64_t num, char *buf, size_t buflen)
168*11379SVladimir.Kotal@Sun.COM {
169*11379SVladimir.Kotal@Sun.COM 	uint64_t n = num;
170*11379SVladimir.Kotal@Sun.COM 	char u;
171*11379SVladimir.Kotal@Sun.COM 	int index = 0;
172*11379SVladimir.Kotal@Sun.COM 
173*11379SVladimir.Kotal@Sun.COM 	while (n >= 1024) {
174*11379SVladimir.Kotal@Sun.COM 		n /= 1024;
175*11379SVladimir.Kotal@Sun.COM 		index++;
176*11379SVladimir.Kotal@Sun.COM 	}
177*11379SVladimir.Kotal@Sun.COM 
178*11379SVladimir.Kotal@Sun.COM 	/* The field has all units this function can represent. */
179*11379SVladimir.Kotal@Sun.COM 	u = " KMGTPE"[index];
180*11379SVladimir.Kotal@Sun.COM 
181*11379SVladimir.Kotal@Sun.COM 	if (index == 0) {
182*11379SVladimir.Kotal@Sun.COM 		/* Less than 1K */
183*11379SVladimir.Kotal@Sun.COM 		if (snprintf(buf, buflen, "%llu ", num) >= buflen)
184*11379SVladimir.Kotal@Sun.COM 			(void) memset(buf, '\0', buflen);
185*11379SVladimir.Kotal@Sun.COM 	} else {
186*11379SVladimir.Kotal@Sun.COM 		/* Otherwise display 2 precision digits. */
187*11379SVladimir.Kotal@Sun.COM 		if (snprintf(buf, buflen, "%.2f %c",
188*11379SVladimir.Kotal@Sun.COM 		    (double)num / (1ULL << index * 10), u) >= buflen)
189*11379SVladimir.Kotal@Sun.COM 			(void) memset(buf, '\0', buflen);
190*11379SVladimir.Kotal@Sun.COM 	}
191*11379SVladimir.Kotal@Sun.COM 
192*11379SVladimir.Kotal@Sun.COM 	return (buf);
193*11379SVladimir.Kotal@Sun.COM }
194*11379SVladimir.Kotal@Sun.COM 
195*11379SVladimir.Kotal@Sun.COM /*
196*11379SVladimir.Kotal@Sun.COM  * secs2str() wrapper. Zeroes out the input buffer and if the number of
197*11379SVladimir.Kotal@Sun.COM  * seconds to be converted is more than minute, it will produce readable
198*11379SVladimir.Kotal@Sun.COM  * string in parentheses, store it in the original buffer and return the
199*11379SVladimir.Kotal@Sun.COM  * pointer to it.
200*11379SVladimir.Kotal@Sun.COM  */
201*11379SVladimir.Kotal@Sun.COM char *
secs2out(unsigned int secs,char * buf,int bufsiz,int flags)202*11379SVladimir.Kotal@Sun.COM secs2out(unsigned int secs, char *buf, int bufsiz, int flags)
203*11379SVladimir.Kotal@Sun.COM {
204*11379SVladimir.Kotal@Sun.COM 	char *str;
205*11379SVladimir.Kotal@Sun.COM 
206*11379SVladimir.Kotal@Sun.COM 	(void) memset(buf, '\0', bufsiz);
207*11379SVladimir.Kotal@Sun.COM 
208*11379SVladimir.Kotal@Sun.COM 	if (secs > 60) {
209*11379SVladimir.Kotal@Sun.COM 		/* Return empty string in case of out-of-memory. */
210*11379SVladimir.Kotal@Sun.COM 		if ((str = malloc(bufsiz)) == NULL)
211*11379SVladimir.Kotal@Sun.COM 			return (buf);
212*11379SVladimir.Kotal@Sun.COM 
213*11379SVladimir.Kotal@Sun.COM 		(void) secs2str(secs, str, bufsiz);
214*11379SVladimir.Kotal@Sun.COM 		/* Detect overflow. */
215*11379SVladimir.Kotal@Sun.COM 		if (strlen(str) == 0) {
216*11379SVladimir.Kotal@Sun.COM 			free(str);
217*11379SVladimir.Kotal@Sun.COM 			return (buf);
218*11379SVladimir.Kotal@Sun.COM 		}
219*11379SVladimir.Kotal@Sun.COM 
220*11379SVladimir.Kotal@Sun.COM 		/* Emit nothing in case of overflow. */
221*11379SVladimir.Kotal@Sun.COM 		if (snprintf(buf, bufsiz, "%s(%s)%s",
222*11379SVladimir.Kotal@Sun.COM 		    flags & SPC_BEGIN ? " " : "", str,
223*11379SVladimir.Kotal@Sun.COM 		    flags & SPC_END ? " " : "") >= bufsiz)
224*11379SVladimir.Kotal@Sun.COM 			(void) memset(buf, '\0', bufsiz);
225*11379SVladimir.Kotal@Sun.COM 
226*11379SVladimir.Kotal@Sun.COM 		free(str);
227*11379SVladimir.Kotal@Sun.COM 	}
228*11379SVladimir.Kotal@Sun.COM 
229*11379SVladimir.Kotal@Sun.COM 	return (buf);
230*11379SVladimir.Kotal@Sun.COM }
231*11379SVladimir.Kotal@Sun.COM 
232*11379SVladimir.Kotal@Sun.COM /*
233*11379SVladimir.Kotal@Sun.COM  * Convert number of seconds to human readable string. Useful mainly for
234*11379SVladimir.Kotal@Sun.COM  * the lifetime counters. Returns pointer to the user supplied buffer.
235*11379SVladimir.Kotal@Sun.COM  * Able to convert up to days.
236*11379SVladimir.Kotal@Sun.COM  */
237*11379SVladimir.Kotal@Sun.COM char *
secs2str(unsigned int secs,char * buf,int bufsiz)238*11379SVladimir.Kotal@Sun.COM secs2str(unsigned int secs, char *buf, int bufsiz)
239*11379SVladimir.Kotal@Sun.COM {
240*11379SVladimir.Kotal@Sun.COM 	double val = secs;
241*11379SVladimir.Kotal@Sun.COM 	char *unit = "second";
242*11379SVladimir.Kotal@Sun.COM 
243*11379SVladimir.Kotal@Sun.COM 	if (val >= 24*60*60) {
244*11379SVladimir.Kotal@Sun.COM 		val /= 86400;
245*11379SVladimir.Kotal@Sun.COM 		unit = "day";
246*11379SVladimir.Kotal@Sun.COM 	} else if (val >= 60*60) {
247*11379SVladimir.Kotal@Sun.COM 		val /= 60*60;
248*11379SVladimir.Kotal@Sun.COM 		unit = "hour";
249*11379SVladimir.Kotal@Sun.COM 	} else if (val >= 60) {
250*11379SVladimir.Kotal@Sun.COM 		val /= 60;
251*11379SVladimir.Kotal@Sun.COM 		unit = "minute";
252*11379SVladimir.Kotal@Sun.COM 	}
253*11379SVladimir.Kotal@Sun.COM 
254*11379SVladimir.Kotal@Sun.COM 	/* Emit nothing in case of overflow. */
255*11379SVladimir.Kotal@Sun.COM 	if (snprintf(buf, bufsiz, "%.2f %s%s", val, unit,
256*11379SVladimir.Kotal@Sun.COM 	    val >= 2 ? "s" : "") >= bufsiz)
257*11379SVladimir.Kotal@Sun.COM 		(void) memset(buf, '\0', bufsiz);
258*11379SVladimir.Kotal@Sun.COM 
259*11379SVladimir.Kotal@Sun.COM 	return (buf);
260*11379SVladimir.Kotal@Sun.COM }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate  * dump_XXX functions produce ASCII output from various structures.
2640Sstevel@tonic-gate  *
2650Sstevel@tonic-gate  * Because certain errors need to do this to stderr, dump_XXX functions
2660Sstevel@tonic-gate  * take a FILE pointer.
2670Sstevel@tonic-gate  *
2680Sstevel@tonic-gate  * If an error occured while writing to the specified file, these
2690Sstevel@tonic-gate  * functions return -1, zero otherwise.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate int
dump_sockaddr(struct sockaddr * sa,uint8_t prefixlen,boolean_t addr_only,FILE * where,boolean_t ignore_nss)2733055Sdanmcd dump_sockaddr(struct sockaddr *sa, uint8_t prefixlen, boolean_t addr_only,
2744867Spwernau     FILE *where, boolean_t ignore_nss)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	struct sockaddr_in	*sin;
2770Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2780Sstevel@tonic-gate 	char			*printable_addr, *protocol;
2790Sstevel@tonic-gate 	uint8_t			*addrptr;
2803055Sdanmcd 	/* Add 4 chars to hold '/nnn' for prefixes. */
2813055Sdanmcd 	char			storage[INET6_ADDRSTRLEN + 4];
2820Sstevel@tonic-gate 	uint16_t		port;
2830Sstevel@tonic-gate 	boolean_t		unspec;
2840Sstevel@tonic-gate 	struct hostent		*hp;
2850Sstevel@tonic-gate 	int			getipnode_errno, addrlen;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	switch (sa->sa_family) {
2880Sstevel@tonic-gate 	case AF_INET:
2890Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2900Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sa;
2910Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin->sin_addr;
2920Sstevel@tonic-gate 		port = sin->sin_port;
2930Sstevel@tonic-gate 		protocol = "AF_INET";
2940Sstevel@tonic-gate 		unspec = (sin->sin_addr.s_addr == 0);
2950Sstevel@tonic-gate 		addrlen = sizeof (sin->sin_addr);
2960Sstevel@tonic-gate 		break;
2970Sstevel@tonic-gate 	case AF_INET6:
2980Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2990Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sa;
3000Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin6->sin6_addr;
3010Sstevel@tonic-gate 		port = sin6->sin6_port;
3020Sstevel@tonic-gate 		protocol = "AF_INET6";
3030Sstevel@tonic-gate 		unspec = IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr);
3040Sstevel@tonic-gate 		addrlen = sizeof (sin6->sin6_addr);
3050Sstevel@tonic-gate 		break;
3060Sstevel@tonic-gate 	default:
3070Sstevel@tonic-gate 		return (0);
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (inet_ntop(sa->sa_family, addrptr, storage, INET6_ADDRSTRLEN) ==
3110Sstevel@tonic-gate 	    NULL) {
3124064Smarkfen 		printable_addr = dgettext(TEXT_DOMAIN, "Invalid IP address.");
3130Sstevel@tonic-gate 	} else {
3143055Sdanmcd 		char prefix[5];	/* "/nnn" with terminator. */
3153055Sdanmcd 
3163055Sdanmcd 		(void) snprintf(prefix, sizeof (prefix), "/%d", prefixlen);
3170Sstevel@tonic-gate 		printable_addr = storage;
3183055Sdanmcd 		if (prefixlen != 0) {
3193055Sdanmcd 			(void) strlcat(printable_addr, prefix,
3203055Sdanmcd 			    sizeof (storage));
3213055Sdanmcd 		}
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 	if (addr_only) {
3240Sstevel@tonic-gate 		if (fprintf(where, "%s", printable_addr) < 0)
3250Sstevel@tonic-gate 			return (-1);
3260Sstevel@tonic-gate 	} else {
3274064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
3284064Smarkfen 		    "%s: port %d, %s"), protocol,
3290Sstevel@tonic-gate 		    ntohs(port), printable_addr) < 0)
3300Sstevel@tonic-gate 			return (-1);
3314867Spwernau 		if (ignore_nss == B_FALSE) {
3320Sstevel@tonic-gate 			/*
3330Sstevel@tonic-gate 			 * Do AF_independent reverse hostname lookup here.
3340Sstevel@tonic-gate 			 */
3350Sstevel@tonic-gate 			if (unspec) {
3360Sstevel@tonic-gate 				if (fprintf(where,
3374064Smarkfen 				    dgettext(TEXT_DOMAIN,
3384064Smarkfen 				    " <unspecified>")) < 0)
3390Sstevel@tonic-gate 					return (-1);
3400Sstevel@tonic-gate 			} else {
3410Sstevel@tonic-gate 				hp = getipnodebyaddr((char *)addrptr, addrlen,
3420Sstevel@tonic-gate 				    sa->sa_family, &getipnode_errno);
3430Sstevel@tonic-gate 				if (hp != NULL) {
3440Sstevel@tonic-gate 					if (fprintf(where,
3450Sstevel@tonic-gate 					    " (%s)", hp->h_name) < 0)
3460Sstevel@tonic-gate 						return (-1);
3470Sstevel@tonic-gate 					freehostent(hp);
3480Sstevel@tonic-gate 				} else {
3490Sstevel@tonic-gate 					if (fprintf(where,
3504064Smarkfen 					    dgettext(TEXT_DOMAIN,
3514064Smarkfen 					    " <unknown>")) < 0)
3520Sstevel@tonic-gate 						return (-1);
3530Sstevel@tonic-gate 				}
3540Sstevel@tonic-gate 			}
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 		if (fputs(".\n", where) == EOF)
3570Sstevel@tonic-gate 			return (-1);
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate 	return (0);
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate /*
36310824SMark.Fenwick@Sun.COM  * Dump a key, any salt and bitlen.
36410824SMark.Fenwick@Sun.COM  * The key is made up of a stream of bits. If the algorithm requires a salt
36510824SMark.Fenwick@Sun.COM  * value, this will also be part of the dumped key. The last "saltbits" of the
36610824SMark.Fenwick@Sun.COM  * key string, reading left to right will be the salt value. To make it easier
36710824SMark.Fenwick@Sun.COM  * to see which bits make up the key, the salt value is enclosed in []'s.
36810824SMark.Fenwick@Sun.COM  * This function can also be called when ipseckey(1m) -s is run, this "saves"
36910824SMark.Fenwick@Sun.COM  * the SAs, including the key to a file. When this is the case, the []'s are
37010824SMark.Fenwick@Sun.COM  * not printed.
37110824SMark.Fenwick@Sun.COM  *
37210824SMark.Fenwick@Sun.COM  * The implementation allows the kernel to be told about the length of the salt
37310824SMark.Fenwick@Sun.COM  * in whole bytes only. If this changes, this function will need to be updated.
3740Sstevel@tonic-gate  */
3750Sstevel@tonic-gate int
dump_key(uint8_t * keyp,uint_t bitlen,uint_t saltbits,FILE * where,boolean_t separate_salt)37610824SMark.Fenwick@Sun.COM dump_key(uint8_t *keyp, uint_t bitlen, uint_t saltbits, FILE *where,
37710824SMark.Fenwick@Sun.COM     boolean_t separate_salt)
3780Sstevel@tonic-gate {
37910824SMark.Fenwick@Sun.COM 	int	numbytes, saltbytes;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	numbytes = SADB_1TO8(bitlen);
38210824SMark.Fenwick@Sun.COM 	saltbytes = SADB_1TO8(saltbits);
38310824SMark.Fenwick@Sun.COM 	numbytes += saltbytes;
38410824SMark.Fenwick@Sun.COM 
3850Sstevel@tonic-gate 	/* The & 0x7 is to check for leftover bits. */
3860Sstevel@tonic-gate 	if ((bitlen & 0x7) != 0)
3870Sstevel@tonic-gate 		numbytes++;
38810824SMark.Fenwick@Sun.COM 
3890Sstevel@tonic-gate 	while (numbytes-- != 0) {
3900Sstevel@tonic-gate 		if (pflag) {
3910Sstevel@tonic-gate 			/* Print no keys if paranoid */
3920Sstevel@tonic-gate 			if (fprintf(where, "XX") < 0)
3930Sstevel@tonic-gate 				return (-1);
3940Sstevel@tonic-gate 		} else {
3950Sstevel@tonic-gate 			if (fprintf(where, "%02x", *keyp++) < 0)
3960Sstevel@tonic-gate 				return (-1);
3970Sstevel@tonic-gate 		}
39810824SMark.Fenwick@Sun.COM 		if (separate_salt && saltbytes != 0 &&
39910824SMark.Fenwick@Sun.COM 		    numbytes == saltbytes) {
40010824SMark.Fenwick@Sun.COM 			if (fprintf(where, "[") < 0)
40110824SMark.Fenwick@Sun.COM 				return (-1);
40210824SMark.Fenwick@Sun.COM 		}
4030Sstevel@tonic-gate 	}
40410824SMark.Fenwick@Sun.COM 
40510824SMark.Fenwick@Sun.COM 	if (separate_salt && saltbits != 0) {
40610824SMark.Fenwick@Sun.COM 		if (fprintf(where, "]/%u+%u", bitlen, saltbits) < 0)
40710824SMark.Fenwick@Sun.COM 			return (-1);
40810824SMark.Fenwick@Sun.COM 	} else {
40910824SMark.Fenwick@Sun.COM 		if (fprintf(where, "/%u", bitlen + saltbits) < 0)
41010824SMark.Fenwick@Sun.COM 			return (-1);
41110824SMark.Fenwick@Sun.COM 	}
41210824SMark.Fenwick@Sun.COM 
4130Sstevel@tonic-gate 	return (0);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  * Print an authentication or encryption algorithm
4180Sstevel@tonic-gate  */
4190Sstevel@tonic-gate static int
dump_generic_alg(uint8_t alg_num,int proto_num,FILE * where)4200Sstevel@tonic-gate dump_generic_alg(uint8_t alg_num, int proto_num, FILE *where)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	struct ipsecalgent *alg;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_num, proto_num, NULL);
4250Sstevel@tonic-gate 	if (alg == NULL) {
4264064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
4274064Smarkfen 		    "<unknown %u>"), alg_num) < 0)
4280Sstevel@tonic-gate 			return (-1);
4290Sstevel@tonic-gate 		return (0);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/*
4330Sstevel@tonic-gate 	 * Special-case <none> for backward output compat.
4340Sstevel@tonic-gate 	 * Assume that SADB_AALG_NONE == SADB_EALG_NONE.
4350Sstevel@tonic-gate 	 */
4360Sstevel@tonic-gate 	if (alg_num == SADB_AALG_NONE) {
4374064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
4384064Smarkfen 		    "<none>"), where) == EOF)
4390Sstevel@tonic-gate 			return (-1);
4400Sstevel@tonic-gate 	} else {
4410Sstevel@tonic-gate 		if (fputs(alg->a_names[0], where) == EOF)
4420Sstevel@tonic-gate 			return (-1);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	freeipsecalgent(alg);
4460Sstevel@tonic-gate 	return (0);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate int
dump_aalg(uint8_t aalg,FILE * where)4500Sstevel@tonic-gate dump_aalg(uint8_t aalg, FILE *where)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	return (dump_generic_alg(aalg, IPSEC_PROTO_AH, where));
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate int
dump_ealg(uint8_t ealg,FILE * where)4560Sstevel@tonic-gate dump_ealg(uint8_t ealg, FILE *where)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	return (dump_generic_alg(ealg, IPSEC_PROTO_ESP, where));
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate /*
4620Sstevel@tonic-gate  * Print an SADB_IDENTTYPE string
4630Sstevel@tonic-gate  *
4640Sstevel@tonic-gate  * Also return TRUE if the actual ident may be printed, FALSE if not.
4650Sstevel@tonic-gate  *
4660Sstevel@tonic-gate  * If rc is not NULL, set its value to -1 if an error occured while writing
4670Sstevel@tonic-gate  * to the specified file, zero otherwise.
4680Sstevel@tonic-gate  */
4690Sstevel@tonic-gate boolean_t
dump_sadb_idtype(uint8_t idtype,FILE * where,int * rc)4700Sstevel@tonic-gate dump_sadb_idtype(uint8_t idtype, FILE *where, int *rc)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	boolean_t canprint = B_TRUE;
4730Sstevel@tonic-gate 	int rc_val = 0;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	switch (idtype) {
4760Sstevel@tonic-gate 	case SADB_IDENTTYPE_PREFIX:
4774064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "prefix"), where) == EOF)
4780Sstevel@tonic-gate 			rc_val = -1;
4790Sstevel@tonic-gate 		break;
4800Sstevel@tonic-gate 	case SADB_IDENTTYPE_FQDN:
4814064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "FQDN"), where) == EOF)
4820Sstevel@tonic-gate 			rc_val = -1;
4830Sstevel@tonic-gate 		break;
4840Sstevel@tonic-gate 	case SADB_IDENTTYPE_USER_FQDN:
4854064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
4864064Smarkfen 		    "user-FQDN (mbox)"), where) == EOF)
4870Sstevel@tonic-gate 			rc_val = -1;
4880Sstevel@tonic-gate 		break;
4890Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_DN:
4904064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Distinguished Name"),
4910Sstevel@tonic-gate 		    where) == EOF)
4920Sstevel@tonic-gate 			rc_val = -1;
4930Sstevel@tonic-gate 		canprint = B_FALSE;
4940Sstevel@tonic-gate 		break;
4950Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_GN:
4964064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Generic Name"),
4974064Smarkfen 		    where) == EOF)
4980Sstevel@tonic-gate 			rc_val = -1;
4990Sstevel@tonic-gate 		canprint = B_FALSE;
5000Sstevel@tonic-gate 		break;
5010Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_KEY_ID:
5024064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Generic key id"),
5034064Smarkfen 		    where) == EOF)
5040Sstevel@tonic-gate 			rc_val = -1;
5050Sstevel@tonic-gate 		break;
5060Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_ADDR_RANGE:
5074064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Address range"), where) == EOF)
5080Sstevel@tonic-gate 			rc_val = -1;
5090Sstevel@tonic-gate 		break;
5100Sstevel@tonic-gate 	default:
5114064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
5124064Smarkfen 		    "<unknown %u>"), idtype) < 0)
5130Sstevel@tonic-gate 			rc_val = -1;
5140Sstevel@tonic-gate 		break;
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	if (rc != NULL)
5180Sstevel@tonic-gate 		*rc = rc_val;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	return (canprint);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate /*
5240Sstevel@tonic-gate  * Slice an argv/argc vector from an interactive line or a read-file line.
5250Sstevel@tonic-gate  */
5260Sstevel@tonic-gate static int
create_argv(char * ibuf,int * newargc,char *** thisargv)5270Sstevel@tonic-gate create_argv(char *ibuf, int *newargc, char ***thisargv)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	unsigned int argvlen = START_ARG;
5300Sstevel@tonic-gate 	char **current;
5310Sstevel@tonic-gate 	boolean_t firstchar = B_TRUE;
5320Sstevel@tonic-gate 	boolean_t inquotes = B_FALSE;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	*thisargv = malloc(sizeof (char *) * argvlen);
5350Sstevel@tonic-gate 	if ((*thisargv) == NULL)
5360Sstevel@tonic-gate 		return (MEMORY_ALLOCATION);
5370Sstevel@tonic-gate 	current = *thisargv;
5380Sstevel@tonic-gate 	*current = NULL;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	for (; *ibuf != '\0'; ibuf++) {
5410Sstevel@tonic-gate 		if (isspace(*ibuf)) {
5420Sstevel@tonic-gate 			if (inquotes) {
5430Sstevel@tonic-gate 				continue;
5440Sstevel@tonic-gate 			}
5450Sstevel@tonic-gate 			if (*current != NULL) {
5460Sstevel@tonic-gate 				*ibuf = '\0';
5470Sstevel@tonic-gate 				current++;
5480Sstevel@tonic-gate 				if (*thisargv + argvlen == current) {
5490Sstevel@tonic-gate 					/* Regrow ***thisargv. */
5500Sstevel@tonic-gate 					if (argvlen == TOO_MANY_ARGS) {
5510Sstevel@tonic-gate 						free(*thisargv);
5520Sstevel@tonic-gate 						return (TOO_MANY_TOKENS);
5530Sstevel@tonic-gate 					}
5540Sstevel@tonic-gate 					/* Double the allocation. */
5550Sstevel@tonic-gate 					current = realloc(*thisargv,
5560Sstevel@tonic-gate 					    sizeof (char *) * (argvlen << 1));
5570Sstevel@tonic-gate 					if (current == NULL) {
5580Sstevel@tonic-gate 						free(*thisargv);
5590Sstevel@tonic-gate 						return (MEMORY_ALLOCATION);
5600Sstevel@tonic-gate 					}
5610Sstevel@tonic-gate 					*thisargv = current;
5620Sstevel@tonic-gate 					current += argvlen;
5630Sstevel@tonic-gate 					argvlen <<= 1;	/* Double the size. */
5640Sstevel@tonic-gate 				}
5650Sstevel@tonic-gate 				*current = NULL;
5660Sstevel@tonic-gate 			}
5670Sstevel@tonic-gate 		} else {
5680Sstevel@tonic-gate 			if (firstchar) {
5690Sstevel@tonic-gate 				firstchar = B_FALSE;
5704235Smarkfen 				if (*ibuf == COMMENT_CHAR || *ibuf == '\n') {
5710Sstevel@tonic-gate 					free(*thisargv);
5720Sstevel@tonic-gate 					return (COMMENT_LINE);
5730Sstevel@tonic-gate 				}
5740Sstevel@tonic-gate 			}
5750Sstevel@tonic-gate 			if (*ibuf == QUOTE_CHAR) {
5760Sstevel@tonic-gate 				if (inquotes) {
5770Sstevel@tonic-gate 					inquotes = B_FALSE;
5780Sstevel@tonic-gate 					*ibuf = '\0';
5790Sstevel@tonic-gate 				} else {
5800Sstevel@tonic-gate 					inquotes = B_TRUE;
5810Sstevel@tonic-gate 				}
5820Sstevel@tonic-gate 				continue;
5830Sstevel@tonic-gate 			}
5840Sstevel@tonic-gate 			if (*current == NULL) {
5850Sstevel@tonic-gate 				*current = ibuf;
5860Sstevel@tonic-gate 				(*newargc)++;
5870Sstevel@tonic-gate 			}
5880Sstevel@tonic-gate 		}
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/*
5920Sstevel@tonic-gate 	 * Tricky corner case...
5930Sstevel@tonic-gate 	 * I've parsed _exactly_ the amount of args as I have space.  It
5940Sstevel@tonic-gate 	 * won't return NULL-terminated, and bad things will happen to
5950Sstevel@tonic-gate 	 * the caller.
5960Sstevel@tonic-gate 	 */
5970Sstevel@tonic-gate 	if (argvlen == *newargc) {
5980Sstevel@tonic-gate 		current = realloc(*thisargv, sizeof (char *) * (argvlen + 1));
5990Sstevel@tonic-gate 		if (current == NULL) {
6000Sstevel@tonic-gate 			free(*thisargv);
6010Sstevel@tonic-gate 			return (MEMORY_ALLOCATION);
6020Sstevel@tonic-gate 		}
6030Sstevel@tonic-gate 		*thisargv = current;
6040Sstevel@tonic-gate 		current[argvlen] = NULL;
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	return (SUCCESS);
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate /*
6119086SVladimir.Kotal@Sun.COM  * init interactive mode if needed and not yet initialized
6129086SVladimir.Kotal@Sun.COM  */
6139086SVladimir.Kotal@Sun.COM static void
init_interactive(FILE * infile,CplMatchFn * match_fn)6149086SVladimir.Kotal@Sun.COM init_interactive(FILE *infile, CplMatchFn *match_fn)
6159086SVladimir.Kotal@Sun.COM {
6169086SVladimir.Kotal@Sun.COM 	if (infile == stdin) {
6179086SVladimir.Kotal@Sun.COM 		if (gl == NULL) {
6189086SVladimir.Kotal@Sun.COM 			if ((gl = new_GetLine(MAX_LINE_LEN,
6199086SVladimir.Kotal@Sun.COM 			    MAX_CMD_HIST)) == NULL)
6209086SVladimir.Kotal@Sun.COM 				errx(1, dgettext(TEXT_DOMAIN,
6219086SVladimir.Kotal@Sun.COM 				    "tecla initialization failed"));
6229086SVladimir.Kotal@Sun.COM 
6239086SVladimir.Kotal@Sun.COM 			if (gl_customize_completion(gl, NULL,
6249086SVladimir.Kotal@Sun.COM 			    match_fn) != 0) {
6259086SVladimir.Kotal@Sun.COM 				(void) del_GetLine(gl);
6269086SVladimir.Kotal@Sun.COM 				errx(1, dgettext(TEXT_DOMAIN,
6279086SVladimir.Kotal@Sun.COM 				    "tab completion failed to initialize"));
6289086SVladimir.Kotal@Sun.COM 			}
6299086SVladimir.Kotal@Sun.COM 
6309086SVladimir.Kotal@Sun.COM 			/*
6319086SVladimir.Kotal@Sun.COM 			 * In interactive mode we only want to terminate
6329086SVladimir.Kotal@Sun.COM 			 * when explicitly requested (e.g. by a command).
6339086SVladimir.Kotal@Sun.COM 			 */
6349086SVladimir.Kotal@Sun.COM 			(void) sigset(SIGINT, SIG_IGN);
6359086SVladimir.Kotal@Sun.COM 		}
6369086SVladimir.Kotal@Sun.COM 	} else {
6379086SVladimir.Kotal@Sun.COM 		readfile = B_TRUE;
6389086SVladimir.Kotal@Sun.COM 	}
6399086SVladimir.Kotal@Sun.COM }
6409086SVladimir.Kotal@Sun.COM 
6419086SVladimir.Kotal@Sun.COM /*
6429086SVladimir.Kotal@Sun.COM  * free tecla data structure
6439086SVladimir.Kotal@Sun.COM  */
6449086SVladimir.Kotal@Sun.COM static void
fini_interactive(void)6459086SVladimir.Kotal@Sun.COM fini_interactive(void)
6469086SVladimir.Kotal@Sun.COM {
6479086SVladimir.Kotal@Sun.COM 	if (gl != NULL)
6489086SVladimir.Kotal@Sun.COM 		(void) del_GetLine(gl);
6499086SVladimir.Kotal@Sun.COM }
6509086SVladimir.Kotal@Sun.COM 
6519086SVladimir.Kotal@Sun.COM /*
6529086SVladimir.Kotal@Sun.COM  * Get single input line, wrapping around interactive and non-interactive
6539086SVladimir.Kotal@Sun.COM  * mode.
6549086SVladimir.Kotal@Sun.COM  */
6559086SVladimir.Kotal@Sun.COM static char *
do_getstr(FILE * infile,char * prompt,char * ibuf,size_t ibuf_size)6569086SVladimir.Kotal@Sun.COM do_getstr(FILE *infile, char *prompt, char *ibuf, size_t ibuf_size)
6579086SVladimir.Kotal@Sun.COM {
6589086SVladimir.Kotal@Sun.COM 	char	*line;
6599086SVladimir.Kotal@Sun.COM 
6609086SVladimir.Kotal@Sun.COM 	if (infile != stdin)
6619086SVladimir.Kotal@Sun.COM 		return (fgets(ibuf, ibuf_size, infile));
6629086SVladimir.Kotal@Sun.COM 
6639086SVladimir.Kotal@Sun.COM 	/*
6649086SVladimir.Kotal@Sun.COM 	 * If the user hits ^C then we want to catch it and
6659086SVladimir.Kotal@Sun.COM 	 * start over.  If the user hits EOF then we want to
6669086SVladimir.Kotal@Sun.COM 	 * bail out.
6679086SVladimir.Kotal@Sun.COM 	 */
6689086SVladimir.Kotal@Sun.COM once_again:
6699086SVladimir.Kotal@Sun.COM 	line = gl_get_line(gl, prompt, NULL, -1);
6709086SVladimir.Kotal@Sun.COM 	if (gl_return_status(gl) == GLR_SIGNAL) {
6719086SVladimir.Kotal@Sun.COM 		gl_abandon_line(gl);
6729086SVladimir.Kotal@Sun.COM 		goto once_again;
6739086SVladimir.Kotal@Sun.COM 	} else if (gl_return_status(gl) == GLR_ERROR) {
6749086SVladimir.Kotal@Sun.COM 		gl_abandon_line(gl);
6759086SVladimir.Kotal@Sun.COM 		errx(1, dgettext(TEXT_DOMAIN, "Error reading terminal: %s\n"),
6769086SVladimir.Kotal@Sun.COM 		    gl_error_message(gl, NULL, 0));
6779086SVladimir.Kotal@Sun.COM 	} else {
6789086SVladimir.Kotal@Sun.COM 		if (line != NULL) {
6799086SVladimir.Kotal@Sun.COM 			if (strlcpy(ibuf, line, ibuf_size) >= ibuf_size)
6809086SVladimir.Kotal@Sun.COM 				warnx(dgettext(TEXT_DOMAIN,
6819086SVladimir.Kotal@Sun.COM 				    "Line too long (max=%d chars)"),
6829086SVladimir.Kotal@Sun.COM 				    ibuf_size);
6839086SVladimir.Kotal@Sun.COM 			line = ibuf;
6849086SVladimir.Kotal@Sun.COM 		}
6859086SVladimir.Kotal@Sun.COM 	}
6869086SVladimir.Kotal@Sun.COM 
6879086SVladimir.Kotal@Sun.COM 	return (line);
6889086SVladimir.Kotal@Sun.COM }
6899086SVladimir.Kotal@Sun.COM 
6909086SVladimir.Kotal@Sun.COM /*
6910Sstevel@tonic-gate  * Enter a mode where commands are read from a file.  Treat stdin special.
6920Sstevel@tonic-gate  */
6930Sstevel@tonic-gate void
do_interactive(FILE * infile,char * configfile,char * promptstring,char * my_fmri,parse_cmdln_fn parseit,CplMatchFn * match_fn)6944235Smarkfen do_interactive(FILE *infile, char *configfile, char *promptstring,
6959086SVladimir.Kotal@Sun.COM     char *my_fmri, parse_cmdln_fn parseit, CplMatchFn *match_fn)
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate 	char		ibuf[IBUF_SIZE], holder[IBUF_SIZE];
6984235Smarkfen 	char		*hptr, **thisargv, *ebuf;
6990Sstevel@tonic-gate 	int		thisargc;
7000Sstevel@tonic-gate 	boolean_t	continue_in_progress = B_FALSE;
7019086SVladimir.Kotal@Sun.COM 	char		*s;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	(void) setjmp(env);
7040Sstevel@tonic-gate 
7054235Smarkfen 	ebuf = NULL;
7060Sstevel@tonic-gate 	interactive = B_TRUE;
7070Sstevel@tonic-gate 	bzero(ibuf, IBUF_SIZE);
7080Sstevel@tonic-gate 
7099086SVladimir.Kotal@Sun.COM 	/* panics for us */
7109086SVladimir.Kotal@Sun.COM 	init_interactive(infile, match_fn);
7110Sstevel@tonic-gate 
7129086SVladimir.Kotal@Sun.COM 	while ((s = do_getstr(infile, promptstring, ibuf, IBUF_SIZE)) != NULL) {
7130Sstevel@tonic-gate 		if (readfile)
7140Sstevel@tonic-gate 			lineno++;
7150Sstevel@tonic-gate 		thisargc = 0;
7160Sstevel@tonic-gate 		thisargv = NULL;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		/*
7190Sstevel@tonic-gate 		 * Check byte IBUF_SIZE - 2, because byte IBUF_SIZE - 1 will
7200Sstevel@tonic-gate 		 * be null-terminated because of fgets().
7210Sstevel@tonic-gate 		 */
7220Sstevel@tonic-gate 		if (ibuf[IBUF_SIZE - 2] != '\0') {
7239086SVladimir.Kotal@Sun.COM 			if (infile == stdin) {
7249086SVladimir.Kotal@Sun.COM 				/* do_getstr() issued a warning already */
7259086SVladimir.Kotal@Sun.COM 				bzero(ibuf, IBUF_SIZE);
7269086SVladimir.Kotal@Sun.COM 				continue;
7279086SVladimir.Kotal@Sun.COM 			} else {
7289086SVladimir.Kotal@Sun.COM 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
7299086SVladimir.Kotal@Sun.COM 				    debugfile, dgettext(TEXT_DOMAIN,
7309086SVladimir.Kotal@Sun.COM 				    "Line %d too big."), lineno);
7319086SVladimir.Kotal@Sun.COM 			}
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 		if (!continue_in_progress) {
7350Sstevel@tonic-gate 			/* Use -2 because of \n from fgets. */
7360Sstevel@tonic-gate 			if (ibuf[strlen(ibuf) - 2] == CONT_CHAR) {
7370Sstevel@tonic-gate 				/*
7380Sstevel@tonic-gate 				 * Can use strcpy here, I've checked the
7390Sstevel@tonic-gate 				 * length already.
7400Sstevel@tonic-gate 				 */
7410Sstevel@tonic-gate 				(void) strcpy(holder, ibuf);
7420Sstevel@tonic-gate 				hptr = &(holder[strlen(holder)]);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
7450Sstevel@tonic-gate 				hptr[-2] = ' ';
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 				continue_in_progress = B_TRUE;
7480Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
7490Sstevel@tonic-gate 				continue;
7500Sstevel@tonic-gate 			}
7510Sstevel@tonic-gate 		} else {
7520Sstevel@tonic-gate 			/* Handle continuations... */
7530Sstevel@tonic-gate 			(void) strncpy(hptr, ibuf,
7540Sstevel@tonic-gate 			    (size_t)(&(holder[IBUF_SIZE]) - hptr));
7550Sstevel@tonic-gate 			if (holder[IBUF_SIZE - 1] != '\0') {
7564235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
7574235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
7584235Smarkfen 				    "Command buffer overrun."));
7590Sstevel@tonic-gate 			}
7600Sstevel@tonic-gate 			/* Use - 2 because of \n from fgets. */
7610Sstevel@tonic-gate 			if (hptr[strlen(hptr) - 2] == CONT_CHAR) {
7620Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
7630Sstevel@tonic-gate 				hptr += strlen(hptr);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
7660Sstevel@tonic-gate 				hptr[-2] = ' ';
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 				continue;
7690Sstevel@tonic-gate 			} else {
7700Sstevel@tonic-gate 				continue_in_progress = B_FALSE;
7710Sstevel@tonic-gate 				/*
7720Sstevel@tonic-gate 				 * I've already checked the length...
7730Sstevel@tonic-gate 				 */
7740Sstevel@tonic-gate 				(void) strcpy(ibuf, holder);
7750Sstevel@tonic-gate 			}
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 
7784235Smarkfen 		/*
7794235Smarkfen 		 * Just in case the command fails keep a copy of the
7804235Smarkfen 		 * command buffer for diagnostic output.
7814235Smarkfen 		 */
7824235Smarkfen 		if (readfile) {
7834235Smarkfen 			/*
7844235Smarkfen 			 * The error buffer needs to be big enough to
7854235Smarkfen 			 * hold the longest command string, plus
7864235Smarkfen 			 * some extra text, see below.
7874235Smarkfen 			 */
7884235Smarkfen 			ebuf = calloc((IBUF_SIZE * 2), sizeof (char));
7894235Smarkfen 			if (ebuf == NULL) {
7904235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
7914235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
7924235Smarkfen 				    "Memory allocation error."));
7934235Smarkfen 			} else {
7944235Smarkfen 				(void) snprintf(ebuf, (IBUF_SIZE * 2),
7954235Smarkfen 				    dgettext(TEXT_DOMAIN,
7964235Smarkfen 				    "Config file entry near line %u "
7974235Smarkfen 				    "caused error(s) or warnings:\n\n%s\n\n"),
7984235Smarkfen 				    lineno, ibuf);
7994235Smarkfen 			}
8004235Smarkfen 		}
8014235Smarkfen 
8020Sstevel@tonic-gate 		switch (create_argv(ibuf, &thisargc, &thisargv)) {
8030Sstevel@tonic-gate 		case TOO_MANY_TOKENS:
8044235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
8054235Smarkfen 			    dgettext(TEXT_DOMAIN, "Too many input tokens."));
8060Sstevel@tonic-gate 			break;
8070Sstevel@tonic-gate 		case MEMORY_ALLOCATION:
8084235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
8094235Smarkfen 			    dgettext(TEXT_DOMAIN, "Memory allocation error."));
8100Sstevel@tonic-gate 			break;
8110Sstevel@tonic-gate 		case COMMENT_LINE:
8120Sstevel@tonic-gate 			/* Comment line. */
8134235Smarkfen 			free(ebuf);
8140Sstevel@tonic-gate 			break;
8150Sstevel@tonic-gate 		default:
8164235Smarkfen 			if (thisargc != 0) {
8174235Smarkfen 				lines_parsed++;
8184235Smarkfen 				/* ebuf consumed */
8194342Spwernau 				parseit(thisargc, thisargv, ebuf, readfile);
8204235Smarkfen 			} else {
8214235Smarkfen 				free(ebuf);
8224235Smarkfen 			}
8230Sstevel@tonic-gate 			free(thisargv);
8240Sstevel@tonic-gate 			if (infile == stdin) {
8250Sstevel@tonic-gate 				(void) printf("%s", promptstring);
8260Sstevel@tonic-gate 				(void) fflush(stdout);
8270Sstevel@tonic-gate 			}
8280Sstevel@tonic-gate 			break;
8290Sstevel@tonic-gate 		}
8300Sstevel@tonic-gate 		bzero(ibuf, IBUF_SIZE);
8310Sstevel@tonic-gate 	}
8324342Spwernau 
8334342Spwernau 	/*
8344342Spwernau 	 * The following code is ipseckey specific. This should never be
8354342Spwernau 	 * used by ikeadm which also calls this function because ikeadm
8364342Spwernau 	 * only runs interactively. If this ever changes this code block
8374342Spwernau 	 * sould be revisited.
8384342Spwernau 	 */
8394342Spwernau 	if (readfile) {
8404342Spwernau 		if (lines_parsed != 0 && lines_added == 0) {
8414342Spwernau 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
8424342Spwernau 			    dgettext(TEXT_DOMAIN, "Configuration file did not "
8434342Spwernau 			    "contain any valid SAs"));
8444342Spwernau 		}
8454342Spwernau 
8464342Spwernau 		/*
8474342Spwernau 		 * There were errors. Putting the service in maintenance mode.
8484342Spwernau 		 * When svc.startd(1M) allows services to degrade themselves,
8494342Spwernau 		 * this should be revisited.
8504342Spwernau 		 *
8514342Spwernau 		 * If this function was called from a program running as a
8524342Spwernau 		 * smf_method(5), print a warning message. Don't spew out the
8534342Spwernau 		 * errors as these will end up in the smf(5) log file which is
8544342Spwernau 		 * publically readable, the errors may contain sensitive
8554342Spwernau 		 * information.
8564342Spwernau 		 */
8574342Spwernau 		if ((lines_added < lines_parsed) && (configfile != NULL)) {
8584342Spwernau 			if (my_fmri != NULL) {
8594342Spwernau 				ipsecutil_exit(SERVICE_BADCONF, my_fmri,
8604342Spwernau 				    debugfile, dgettext(TEXT_DOMAIN,
8614342Spwernau 				    "The configuration file contained %d "
8624342Spwernau 				    "errors.\n"
8634342Spwernau 				    "Manually check the configuration with:\n"
8644342Spwernau 				    "ipseckey -c %s\n"
8654342Spwernau 				    "Use svcadm(1M) to clear maintenance "
8664342Spwernau 				    "condition when errors are resolved.\n"),
8674342Spwernau 				    lines_parsed - lines_added, configfile);
8684342Spwernau 			} else {
8694342Spwernau 				EXIT_BADCONFIG(NULL);
8704342Spwernau 			}
8714342Spwernau 		} else {
8724342Spwernau 			if (my_fmri != NULL)
8734342Spwernau 				ipsecutil_exit(SERVICE_EXIT_OK, my_fmri,
8744342Spwernau 				    debugfile, dgettext(TEXT_DOMAIN,
8754342Spwernau 				    "%d actions successfully processed."),
8764342Spwernau 				    lines_added);
8774342Spwernau 		}
8784342Spwernau 	} else {
8799086SVladimir.Kotal@Sun.COM 		/* no newline upon Ctrl-D */
8809086SVladimir.Kotal@Sun.COM 		if (s != NULL)
8819086SVladimir.Kotal@Sun.COM 			(void) putchar('\n');
8820Sstevel@tonic-gate 		(void) fflush(stdout);
8830Sstevel@tonic-gate 	}
8849086SVladimir.Kotal@Sun.COM 
8859086SVladimir.Kotal@Sun.COM 	fini_interactive();
8869086SVladimir.Kotal@Sun.COM 
8874235Smarkfen 	EXIT_OK(NULL);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate /*
8910Sstevel@tonic-gate  * Functions to parse strings that represent a debug or privilege level.
8920Sstevel@tonic-gate  * These functions are copied from main.c and door.c in usr.lib/in.iked/common.
8930Sstevel@tonic-gate  * If this file evolves into a common library that may be used by in.iked
8940Sstevel@tonic-gate  * as well as the usr.sbin utilities, those duplicate functions should be
8950Sstevel@tonic-gate  * deleted.
8960Sstevel@tonic-gate  *
8970Sstevel@tonic-gate  * A privilege level may be represented by a simple keyword, corresponding
8980Sstevel@tonic-gate  * to one of the possible levels.  A debug level may be represented by a
8990Sstevel@tonic-gate  * series of keywords, separated by '+' or '-', indicating categories to
9000Sstevel@tonic-gate  * be added or removed from the set of categories in the debug level.
9010Sstevel@tonic-gate  * For example, +all-op corresponds to level 0xfffffffb (all flags except
9020Sstevel@tonic-gate  * for D_OP set); while p1+p2+pfkey corresponds to level 0x38.  Note that
9030Sstevel@tonic-gate  * the leading '+' is implicit; the first keyword in the list must be for
9040Sstevel@tonic-gate  * a category that is to be added.
9050Sstevel@tonic-gate  *
9060Sstevel@tonic-gate  * These parsing functions make use of a local version of strtok, strtok_d,
9070Sstevel@tonic-gate  * which includes an additional parameter, char *delim.  This param is filled
9080Sstevel@tonic-gate  * in with the character which ends the returned token.  In other words,
9090Sstevel@tonic-gate  * this version of strtok, in addition to returning the token, also returns
9100Sstevel@tonic-gate  * the single character delimiter from the original string which marked the
9110Sstevel@tonic-gate  * end of the token.
9120Sstevel@tonic-gate  */
9130Sstevel@tonic-gate static char *
strtok_d(char * string,const char * sepset,char * delim)9140Sstevel@tonic-gate strtok_d(char *string, const char *sepset, char *delim)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate 	static char	*lasts;
9170Sstevel@tonic-gate 	char		*q, *r;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	/* first or subsequent call */
9200Sstevel@tonic-gate 	if (string == NULL)
9210Sstevel@tonic-gate 		string = lasts;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	if (string == 0)		/* return if no tokens remaining */
9240Sstevel@tonic-gate 		return (NULL);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	q = string + strspn(string, sepset);	/* skip leading separators */
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (*q == '\0')			/* return if no tokens remaining */
9290Sstevel@tonic-gate 		return (NULL);
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	if ((r = strpbrk(q, sepset)) == NULL) {		/* move past token */
9320Sstevel@tonic-gate 		lasts = 0;	/* indicate that this is last token */
9330Sstevel@tonic-gate 	} else {
9340Sstevel@tonic-gate 		*delim = *r;	/* save delimitor */
9350Sstevel@tonic-gate 		*r = '\0';
9360Sstevel@tonic-gate 		lasts = r + 1;
9370Sstevel@tonic-gate 	}
9380Sstevel@tonic-gate 	return (q);
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate static keywdtab_t	privtab[] = {
9420Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"base" },
9430Sstevel@tonic-gate 	{ IKE_PRIV_MODKEYS,	"modkeys" },
9440Sstevel@tonic-gate 	{ IKE_PRIV_KEYMAT,	"keymat" },
9450Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"0" },
9460Sstevel@tonic-gate };
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate int
privstr2num(char * str)9490Sstevel@tonic-gate privstr2num(char *str)
9500Sstevel@tonic-gate {
9510Sstevel@tonic-gate 	keywdtab_t	*pp;
9520Sstevel@tonic-gate 	char		*endp;
9530Sstevel@tonic-gate 	int		 priv;
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	for (pp = privtab; pp < A_END(privtab); pp++) {
9560Sstevel@tonic-gate 		if (strcasecmp(str, pp->kw_str) == 0)
9570Sstevel@tonic-gate 			return (pp->kw_tag);
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	priv = strtol(str, &endp, 0);
9610Sstevel@tonic-gate 	if (*endp == '\0')
9620Sstevel@tonic-gate 		return (priv);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	return (-1);
9650Sstevel@tonic-gate }
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate static keywdtab_t	dbgtab[] = {
9680Sstevel@tonic-gate 	{ D_CERT,	"cert" },
9690Sstevel@tonic-gate 	{ D_KEY,	"key" },
9700Sstevel@tonic-gate 	{ D_OP,		"op" },
9710Sstevel@tonic-gate 	{ D_P1,		"p1" },
9720Sstevel@tonic-gate 	{ D_P1,		"phase1" },
9730Sstevel@tonic-gate 	{ D_P2,		"p2" },
9740Sstevel@tonic-gate 	{ D_P2,		"phase2" },
9750Sstevel@tonic-gate 	{ D_PFKEY,	"pfkey" },
9760Sstevel@tonic-gate 	{ D_POL,	"pol" },
9770Sstevel@tonic-gate 	{ D_POL,	"policy" },
9780Sstevel@tonic-gate 	{ D_PROP,	"prop" },
9790Sstevel@tonic-gate 	{ D_DOOR,	"door" },
9800Sstevel@tonic-gate 	{ D_CONFIG,	"config" },
98110934Ssommerfeld@sun.com 	{ D_LABEL,	"label" },
9820Sstevel@tonic-gate 	{ D_ALL,	"all" },
9830Sstevel@tonic-gate 	{ 0,		"0" },
9840Sstevel@tonic-gate };
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate int
dbgstr2num(char * str)9870Sstevel@tonic-gate dbgstr2num(char *str)
9880Sstevel@tonic-gate {
9890Sstevel@tonic-gate 	keywdtab_t	*dp;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	for (dp = dbgtab; dp < A_END(dbgtab); dp++) {
9920Sstevel@tonic-gate 		if (strcasecmp(str, dp->kw_str) == 0)
9930Sstevel@tonic-gate 			return (dp->kw_tag);
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 	return (D_INVALID);
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate int
parsedbgopts(char * optarg)9990Sstevel@tonic-gate parsedbgopts(char *optarg)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	char	*argp, *endp, op, nextop;
10020Sstevel@tonic-gate 	int	mask = 0, new;
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	mask = strtol(optarg, &endp, 0);
10050Sstevel@tonic-gate 	if (*endp == '\0')
10060Sstevel@tonic-gate 		return (mask);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	op = optarg[0];
10090Sstevel@tonic-gate 	if (op != '-')
10100Sstevel@tonic-gate 		op = '+';
10110Sstevel@tonic-gate 	argp = strtok_d(optarg, "+-", &nextop);
10120Sstevel@tonic-gate 	do {
10130Sstevel@tonic-gate 		new = dbgstr2num(argp);
10140Sstevel@tonic-gate 		if (new == D_INVALID) {
10150Sstevel@tonic-gate 			/* we encountered an invalid keywd */
10160Sstevel@tonic-gate 			return (new);
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 		if (op == '+') {
10190Sstevel@tonic-gate 			mask |= new;
10200Sstevel@tonic-gate 		} else {
10210Sstevel@tonic-gate 			mask &= ~new;
10220Sstevel@tonic-gate 		}
10230Sstevel@tonic-gate 		op = nextop;
10240Sstevel@tonic-gate 	} while ((argp = strtok_d(NULL, "+-", &nextop)) != NULL);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	return (mask);
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate /*
10310Sstevel@tonic-gate  * functions to manipulate the kmcookie-label mapping file
10320Sstevel@tonic-gate  */
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate /*
10350Sstevel@tonic-gate  * Open, lockf, fdopen the given file, returning a FILE * on success,
10360Sstevel@tonic-gate  * or NULL on failure.
10370Sstevel@tonic-gate  */
10380Sstevel@tonic-gate FILE *
kmc_open_and_lock(char * name)10390Sstevel@tonic-gate kmc_open_and_lock(char *name)
10400Sstevel@tonic-gate {
10410Sstevel@tonic-gate 	int	fd, rtnerr;
10420Sstevel@tonic-gate 	FILE	*fp;
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	if ((fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
10450Sstevel@tonic-gate 		return (NULL);
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 	if (lockf(fd, F_LOCK, 0) < 0) {
10480Sstevel@tonic-gate 		return (NULL);
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 	if ((fp = fdopen(fd, "a+")) == NULL) {
10510Sstevel@tonic-gate 		return (NULL);
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 	if (fseek(fp, 0, SEEK_SET) < 0) {
10540Sstevel@tonic-gate 		/* save errno in case fclose changes it */
10550Sstevel@tonic-gate 		rtnerr = errno;
10560Sstevel@tonic-gate 		(void) fclose(fp);
10570Sstevel@tonic-gate 		errno = rtnerr;
10580Sstevel@tonic-gate 		return (NULL);
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 	return (fp);
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate /*
10640Sstevel@tonic-gate  * Extract an integer cookie and string label from a line from the
10650Sstevel@tonic-gate  * kmcookie-label file.  Return -1 on failure, 0 on success.
10660Sstevel@tonic-gate  */
10670Sstevel@tonic-gate int
kmc_parse_line(char * line,int * cookie,char ** label)10680Sstevel@tonic-gate kmc_parse_line(char *line, int *cookie, char **label)
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate 	char	*cookiestr;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	*cookie = 0;
10730Sstevel@tonic-gate 	*label = NULL;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	cookiestr = strtok(line, " \t\n");
10760Sstevel@tonic-gate 	if (cookiestr == NULL) {
10770Sstevel@tonic-gate 		return (-1);
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	/* Everything that follows, up to the newline, is the label. */
10810Sstevel@tonic-gate 	*label = strtok(NULL, "\n");
10820Sstevel@tonic-gate 	if (*label == NULL) {
10830Sstevel@tonic-gate 		return (-1);
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	*cookie = atoi(cookiestr);
10870Sstevel@tonic-gate 	return (0);
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate /*
10910Sstevel@tonic-gate  * Insert a mapping into the file (if it's not already there), given the
10920Sstevel@tonic-gate  * new label.  Return the assigned cookie, or -1 on error.
10930Sstevel@tonic-gate  */
10940Sstevel@tonic-gate int
kmc_insert_mapping(char * label)10950Sstevel@tonic-gate kmc_insert_mapping(char *label)
10960Sstevel@tonic-gate {
10970Sstevel@tonic-gate 	FILE	*map;
10984757Sdanmcd 	char	linebuf[IBUF_SIZE];
10990Sstevel@tonic-gate 	char	*cur_label;
11000Sstevel@tonic-gate 	int	max_cookie = 0, cur_cookie, rtn_cookie;
11010Sstevel@tonic-gate 	int	rtnerr = 0;
11020Sstevel@tonic-gate 	boolean_t	found = B_FALSE;
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	/* open and lock the file; will sleep until lock is available */
11050Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
11060Sstevel@tonic-gate 		/* kmc_open_and_lock() sets errno appropriately */
11070Sstevel@tonic-gate 		return (-1);
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
11110Sstevel@tonic-gate 
11124757Sdanmcd 		/* Skip blank lines, which often come near EOF. */
11134757Sdanmcd 		if (strlen(linebuf) == 0)
11144757Sdanmcd 			continue;
11154757Sdanmcd 
11160Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
11170Sstevel@tonic-gate 			rtnerr = EINVAL;
11180Sstevel@tonic-gate 			goto error;
11190Sstevel@tonic-gate 		}
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 		if (cur_cookie > max_cookie)
11220Sstevel@tonic-gate 			max_cookie = cur_cookie;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 		if ((!found) && (strcmp(cur_label, label) == 0)) {
11250Sstevel@tonic-gate 			found = B_TRUE;
11260Sstevel@tonic-gate 			rtn_cookie = cur_cookie;
11270Sstevel@tonic-gate 		}
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	if (!found) {
11310Sstevel@tonic-gate 		rtn_cookie = ++max_cookie;
11320Sstevel@tonic-gate 		if ((fprintf(map, "%u\t%s\n", rtn_cookie, label) < 0) ||
11330Sstevel@tonic-gate 		    (fflush(map) < 0)) {
11340Sstevel@tonic-gate 			rtnerr = errno;
11350Sstevel@tonic-gate 			goto error;
11360Sstevel@tonic-gate 		}
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 	(void) fclose(map);
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	return (rtn_cookie);
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate error:
11430Sstevel@tonic-gate 	(void) fclose(map);
11440Sstevel@tonic-gate 	errno = rtnerr;
11450Sstevel@tonic-gate 	return (-1);
11460Sstevel@tonic-gate }
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate /*
11490Sstevel@tonic-gate  * Lookup the given cookie and return its corresponding label.  Return
11500Sstevel@tonic-gate  * a pointer to the label on success, NULL on error (or if the label is
11510Sstevel@tonic-gate  * not found).  Note that the returned label pointer points to a static
11520Sstevel@tonic-gate  * string, so the label will be overwritten by a subsequent call to the
11530Sstevel@tonic-gate  * function; the function is also not thread-safe as a result.
11540Sstevel@tonic-gate  */
11550Sstevel@tonic-gate char *
kmc_lookup_by_cookie(int cookie)11560Sstevel@tonic-gate kmc_lookup_by_cookie(int cookie)
11570Sstevel@tonic-gate {
11580Sstevel@tonic-gate 	FILE		*map;
11594757Sdanmcd 	static char	linebuf[IBUF_SIZE];
11600Sstevel@tonic-gate 	char		*cur_label;
11610Sstevel@tonic-gate 	int		cur_cookie;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
11640Sstevel@tonic-gate 		return (NULL);
11650Sstevel@tonic-gate 	}
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
11700Sstevel@tonic-gate 			(void) fclose(map);
11710Sstevel@tonic-gate 			return (NULL);
11720Sstevel@tonic-gate 		}
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 		if (cookie == cur_cookie) {
11750Sstevel@tonic-gate 			(void) fclose(map);
11760Sstevel@tonic-gate 			return (cur_label);
11770Sstevel@tonic-gate 		}
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate 	(void) fclose(map);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	return (NULL);
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate /*
11850Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
11860Sstevel@tonic-gate  * Return values include:
11870Sstevel@tonic-gate  *
11880Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
11890Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
11900Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
11910Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
11920Sstevel@tonic-gate  *		extv[0].
11930Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
11940Sstevel@tonic-gate  *		unknown header.
11950Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
11960Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
11970Sstevel@tonic-gate  *
11980Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
11990Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
12000Sstevel@tonic-gate  */
12010Sstevel@tonic-gate int
spdsock_get_ext(spd_ext_t * extv[],spd_msg_t * basehdr,uint_t msgsize,char * diag_buf,uint_t diag_buf_len)12020Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize,
12030Sstevel@tonic-gate     char *diag_buf, uint_t diag_buf_len)
12040Sstevel@tonic-gate {
12050Sstevel@tonic-gate 	int i;
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	if (diag_buf != NULL)
12080Sstevel@tonic-gate 		diag_buf[0] = '\0';
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	for (i = 1; i <= SPD_EXT_MAX; i++)
12110Sstevel@tonic-gate 		extv[i] = NULL;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	i = 0;
12140Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	extv[0] = (spd_ext_t *)(basehdr + 1);
12170Sstevel@tonic-gate 	msgsize = SPD_64TO8(msgsize);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	while ((char *)extv[0] < ((char *)basehdr + msgsize)) {
12200Sstevel@tonic-gate 		/* Check for unknown headers. */
12210Sstevel@tonic-gate 		i++;
12220Sstevel@tonic-gate 		if (extv[0]->spd_ext_type == 0 ||
12230Sstevel@tonic-gate 		    extv[0]->spd_ext_type > SPD_EXT_MAX) {
12240Sstevel@tonic-gate 			if (diag_buf != NULL) {
12250Sstevel@tonic-gate 				(void) snprintf(diag_buf, diag_buf_len,
12260Sstevel@tonic-gate 				    "spdsock ext 0x%X unknown: 0x%X",
12270Sstevel@tonic-gate 				    i, extv[0]->spd_ext_type);
12280Sstevel@tonic-gate 			}
12290Sstevel@tonic-gate 			return (KGE_UNK);
12300Sstevel@tonic-gate 		}
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 		/*
12330Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
12340Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
12350Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
12360Sstevel@tonic-gate 		 */
12370Sstevel@tonic-gate 		if (extv[0]->spd_ext_len == 0 ||
12380Sstevel@tonic-gate 		    (uint8_t *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
12390Sstevel@tonic-gate 		    (uint8_t *)((uint8_t *)basehdr + msgsize))
12400Sstevel@tonic-gate 			return (KGE_LEN);
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 		/* Check for redundant headers. */
12430Sstevel@tonic-gate 		if (extv[extv[0]->spd_ext_type] != NULL)
12440Sstevel@tonic-gate 			return (KGE_DUP);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
12470Sstevel@tonic-gate 		extv[extv[0]->spd_ext_type] = extv[0];
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
12500Sstevel@tonic-gate 		extv[0] = (spd_ext_t *)
12510Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
12520Sstevel@tonic-gate 	}
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	/* Everything's cool. */
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	/*
12570Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
12580Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
12590Sstevel@tonic-gate 	 */
12600Sstevel@tonic-gate 	if (extv[0] == (spd_ext_t *)(basehdr + 1))
12610Sstevel@tonic-gate 		extv[0] = NULL;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	return (KGE_OK);
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate const char *
spdsock_diag(int diagnostic)12670Sstevel@tonic-gate spdsock_diag(int diagnostic)
12680Sstevel@tonic-gate {
12690Sstevel@tonic-gate 	switch (diagnostic) {
12700Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NONE:
12714064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no error"));
12720Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNKNOWN_EXT:
12734064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown extension"));
12740Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_EXTLEN:
12754064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad extension length"));
12760Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_RULE_EXT:
12774064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no rule extension"));
12780Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_ADDR_LEN:
12794064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad address len"));
12800Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MIXED_AF:
12814064Smarkfen 		return (dgettext(TEXT_DOMAIN, "mixed address family"));
12820Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_NO_MEM:
12834064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: no memory"));
12840Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT:
12854064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: wrong action count"));
12860Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_TYPE:
12874064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad type"));
12880Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_FLAGS:
12894064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad flags"));
12900Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_INCON_FLAGS:
12914064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: inconsistent flags"));
12920Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLPORT:
12934064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local port"));
12940Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLPORT:
12954064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local port"));
12960Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMPORT:
12974064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote port"));
12980Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMPORT:
12994064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote port"));
13000Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_PROTO:
13014064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed proto"));
13020Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_PROTO:
13034064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate proto"));
13040Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLADDR:
13054064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local address"));
13060Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLADDR:
13074064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local address"));
13080Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMADDR:
13094064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote address"));
13100Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMADDR:
13114064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote address"));
13120Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_ACTION:
13134064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed action"));
13140Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_ACTION:
13154064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate action"));
13160Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULE:
13174064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed rule"));
13180Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULE:
13194064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate rule"));
13200Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULESET:
13214064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed ruleset"));
13220Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULESET:
13234064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate ruleset"));
13240Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_INVALID_RULE_INDEX:
13254064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid rule index"));
13260Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_SPDID:
13274064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad spdid"));
13280Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_MSG_TYPE:
13294064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad message type"));
13300Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_ALG:
13314064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH algorithm"));
13320Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG:
13334064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13344064Smarkfen 		    "unsupported ESP encryption algorithm"));
13350Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG:
13364064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13374064Smarkfen 		    "unsupported ESP authentication algorithm"));
13380Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_KEYSIZE:
13394064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH key size"));
13400Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_KEYSIZE:
13414064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13424064Smarkfen 		    "unsupported ESP encryption key size"));
13430Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_KEYSIZE:
13444064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13454064Smarkfen 		    "unsupported ESP authentication key size"));
13460Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_ACTION_EXT:
13474064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No ACTION extension"));
13480Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_ID_RANGE:
13494064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid algorithm identifer"));
13500Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES:
13514064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13524064Smarkfen 		    "number of key sizes inconsistent"));
13530Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES:
13544064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13554064Smarkfen 		    "number of block sizes inconsistent"));
13560Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN:
13574064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid mechanism name length"));
13583055Sdanmcd 	case SPD_DIAGNOSTIC_NOT_GLOBAL_OP:
13594064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13604064Smarkfen 		    "operation not applicable to all policies"));
13613055Sdanmcd 	case SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS:
13624064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13634064Smarkfen 		    "using selectors on a transport-mode tunnel"));
13640Sstevel@tonic-gate 	default:
13654064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown diagnostic"));
13660Sstevel@tonic-gate 	}
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate /*
13700Sstevel@tonic-gate  * PF_KEY Diagnostic table.
13710Sstevel@tonic-gate  *
13720Sstevel@tonic-gate  * PF_KEY NOTE:  If you change pfkeyv2.h's SADB_X_DIAGNOSTIC_* space, this is
13730Sstevel@tonic-gate  * where you need to add new messages.
13740Sstevel@tonic-gate  */
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate const char *
keysock_diag(int diagnostic)13770Sstevel@tonic-gate keysock_diag(int diagnostic)
13780Sstevel@tonic-gate {
13790Sstevel@tonic-gate 	switch (diagnostic) {
13803055Sdanmcd 	case SADB_X_DIAGNOSTIC_NONE:
13814064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No diagnostic"));
13820Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_MSG:
13834064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown message type"));
13840Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_EXT:
13854064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown extension type"));
13860Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EXTLEN:
13874064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad extension length"));
13880Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE:
13894064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13904064Smarkfen 		    "Unknown Security Association type"));
13910Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SATYPE_NEEDED:
13924064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13934064Smarkfen 		    "Specific Security Association type needed"));
13940Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_SADBS:
13954064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13964064Smarkfen 		    "No Security Association Databases present"));
13970Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_EXT:
13984064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13994064Smarkfen 		    "No extensions needed for message"));
14000Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC_AF:
14014064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address family"));
14020Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST_AF:
14034064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14044064Smarkfen 		    "Bad destination address family"));
14050Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_PROXY_AF:
14064064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14074064Smarkfen 		    "Bad inner-source address family"));
14080Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AF_MISMATCH:
14094064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14104064Smarkfen 		    "Source/destination address family mismatch"));
14110Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC:
14124064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address value"));
14130Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST:
14144064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad destination address value"));
14150Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ALLOC_HSERR:
14164064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14174064Smarkfen 		    "Soft allocations limit more than hard limit"));
14180Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BYTES_HSERR:
14194064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14204064Smarkfen 		    "Soft bytes limit more than hard limit"));
14210Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ADDTIME_HSERR:
14224064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft add expiration time later "
14230Sstevel@tonic-gate 		    "than hard expiration time"));
14240Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_USETIME_HSERR:
14254064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft use expiration time later "
14260Sstevel@tonic-gate 		    "than hard expiration time"));
14270Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SRC:
14284064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing source address"));
14290Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_DST:
14304064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing destination address"));
14310Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SA:
14324064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SA extension"));
14330Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_EKEY:
14344064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing encryption key"));
14350Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_AKEY:
14364064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing authentication key"));
14370Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_RANGE:
14384064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SPI range"));
14390Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SRC:
14404064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate source address"));
14410Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_DST:
14424064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate destination address"));
14430Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SA:
14444064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SA extension"));
14450Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_EKEY:
14464064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate encryption key"));
14470Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_AKEY:
14484064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate authentication key"));
14490Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_RANGE:
14504064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SPI range"));
14510Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SRC:
14524064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed source address"));
14530Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_DST:
14544064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed destination address"));
14550Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SA:
14564064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SA extension"));
14570Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_EKEY:
14584064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed encryption key"));
14590Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_AKEY:
14604064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed authentication key"));
14610Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_RANGE:
14624064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SPI range"));
14630Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AKEY_PRESENT:
14644064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Authentication key not needed"));
14650Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_EKEY_PRESENT:
14664064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Encryption key not needed"));
14670Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_PROP_PRESENT:
14684064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Proposal extension not needed"));
14690Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SUPP_PRESENT:
14704064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14714064Smarkfen 		    "Supported algorithms extension not needed"));
14720Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AALG:
14734064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14744064Smarkfen 		    "Unsupported authentication algorithm"));
14750Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EALG:
14764064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14774064Smarkfen 		    "Unsupported encryption algorithm"));
14780Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SAFLAGS:
14794064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA flags"));
14800Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SASTATE:
14814064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA state"));
14820Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AKEYBITS:
14834064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14844064Smarkfen 		    "Bad number of authentication bits"));
14850Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EKEYBITS:
14864064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14874064Smarkfen 		    "Bad number of encryption bits"));
14880Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ENCR_NOTSUPP:
14894064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14904064Smarkfen 		    "Encryption not supported for this SA type"));
14910Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_EKEY:
14924064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak encryption key"));
14930Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_AKEY:
14944064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak authentication key"));
14950Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMP:
14964064Smarkfen 		return (dgettext(TEXT_DOMAIN,
14974064Smarkfen 		    "Duplicate key management protocol"));
14980Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMC:
14994064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15004064Smarkfen 		    "Duplicate key management cookie"));
15010Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_LOC:
15024064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T local address"));
15030Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_REM:
15044064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T remote address"));
15050Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_LOC:
15064064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T local address"));
15070Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_REM:
15084064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15094064Smarkfen 		    "Duplicate NAT-T remote address"));
15100Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC:
15114064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed NAT-T local address"));
15120Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM:
15134064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15144064Smarkfen 		    "Malformed NAT-T remote address"));
15150Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_PORTS:
15164064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T ports"));
15173055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_SRC:
15184064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing inner source address"));
15193055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_DST:
15204064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15214064Smarkfen 		    "Missing inner destination address"));
15223055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC:
15234064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15244064Smarkfen 		    "Duplicate inner source address"));
15253055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST:
15264064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15274064Smarkfen 		    "Duplicate inner destination address"));
15283055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC:
15294064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15304064Smarkfen 		    "Malformed inner source address"));
15313055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST:
15324064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15334064Smarkfen 		    "Malformed inner destination address"));
15343055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC:
15354064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15364064Smarkfen 		    "Invalid inner-source prefix length "));
15373055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_DST:
15384064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15394064Smarkfen 		    "Invalid inner-destination prefix length"));
15403055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF:
15414064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15424064Smarkfen 		    "Bad inner-destination address family"));
15433055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH:
15444064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15453055Sdanmcd 		    "Inner source/destination address family mismatch"));
15463055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF:
15474064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15484064Smarkfen 		    "Bad NAT-T remote address family"));
15493055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF:
15504064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15514064Smarkfen 		    "Bad NAT-T local address family"));
15523055Sdanmcd 	case SADB_X_DIAGNOSTIC_PROTO_MISMATCH:
15534064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15544064Smarkfen 		    "Source/desination protocol mismatch"));
15553055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_PROTO_MISMATCH:
15564064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15574064Smarkfen 		    "Inner source/desination protocol mismatch"));
15583055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUAL_PORT_SETS:
15594064Smarkfen 		return (dgettext(TEXT_DOMAIN,
15604064Smarkfen 		    "Both inner ports and outer ports are set"));
15616668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE:
15626668Smarkfen 		return (dgettext(TEXT_DOMAIN,
15636668Smarkfen 		    "Pairing failed, target SA unsuitable for pairing"));
15646668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_ADD_MISMATCH:
15656668Smarkfen 		return (dgettext(TEXT_DOMAIN,
15666668Smarkfen 		    "Source/destination address differs from pair SA"));
15676668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_ALREADY:
15686668Smarkfen 		return (dgettext(TEXT_DOMAIN,
15696668Smarkfen 		    "Already paired with another security association"));
15706668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND:
15716668Smarkfen 		return (dgettext(TEXT_DOMAIN,
15726668Smarkfen 		    "Command failed, pair security association not found"));
15736668Smarkfen 	case SADB_X_DIAGNOSTIC_BAD_SA_DIRECTION:
15746668Smarkfen 		return (dgettext(TEXT_DOMAIN,
15756668Smarkfen 		    "Inappropriate SA direction"));
15766668Smarkfen 	case SADB_X_DIAGNOSTIC_SA_NOTFOUND:
15776668Smarkfen 		return (dgettext(TEXT_DOMAIN,
15786668Smarkfen 		    "Security association not found"));
15796668Smarkfen 	case SADB_X_DIAGNOSTIC_SA_EXPIRED:
15806668Smarkfen 		return (dgettext(TEXT_DOMAIN,
15816668Smarkfen 		    "Security association is not valid"));
158210019SMark.Fenwick@Sun.COM 	case SADB_X_DIAGNOSTIC_BAD_CTX:
158310019SMark.Fenwick@Sun.COM 		return (dgettext(TEXT_DOMAIN,
158410019SMark.Fenwick@Sun.COM 		    "Algorithm invalid or not supported by Crypto Framework"));
158510019SMark.Fenwick@Sun.COM 	case SADB_X_DIAGNOSTIC_INVALID_REPLAY:
158610019SMark.Fenwick@Sun.COM 		return (dgettext(TEXT_DOMAIN,
158710019SMark.Fenwick@Sun.COM 		    "Invalid Replay counter"));
158810019SMark.Fenwick@Sun.COM 	case SADB_X_DIAGNOSTIC_MISSING_LIFETIME:
158910019SMark.Fenwick@Sun.COM 		return (dgettext(TEXT_DOMAIN,
159010019SMark.Fenwick@Sun.COM 		    "Inappropriate lifetimes"));
15910Sstevel@tonic-gate 	default:
15924064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code"));
15930Sstevel@tonic-gate 	}
15940Sstevel@tonic-gate }
15953055Sdanmcd 
15963055Sdanmcd /*
15973055Sdanmcd  * Convert an IPv6 mask to a prefix len.  I assume all IPv6 masks are
15983055Sdanmcd  * contiguous, so I stop at the first zero bit!
15993055Sdanmcd  */
16003055Sdanmcd int
in_masktoprefix(uint8_t * mask,boolean_t is_v4mapped)16013055Sdanmcd in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped)
16023055Sdanmcd {
16033055Sdanmcd 	int rc = 0;
16043055Sdanmcd 	uint8_t last;
16053055Sdanmcd 	int limit = IPV6_ABITS;
16063055Sdanmcd 
16073055Sdanmcd 	if (is_v4mapped) {
16083055Sdanmcd 		mask += ((IPV6_ABITS - IP_ABITS)/8);
16093055Sdanmcd 		limit = IP_ABITS;
16103055Sdanmcd 	}
16113055Sdanmcd 
16123055Sdanmcd 	while (*mask == 0xff) {
16133055Sdanmcd 		rc += 8;
16143055Sdanmcd 		if (rc == limit)
16153055Sdanmcd 			return (limit);
16163055Sdanmcd 		mask++;
16173055Sdanmcd 	}
16183055Sdanmcd 
16193055Sdanmcd 	last = *mask;
16203055Sdanmcd 	while (last != 0) {
16213055Sdanmcd 		rc++;
16223055Sdanmcd 		last = (last << 1) & 0xff;
16233055Sdanmcd 	}
16243055Sdanmcd 
16253055Sdanmcd 	return (rc);
16263055Sdanmcd }
16273055Sdanmcd 
16283055Sdanmcd /*
16293055Sdanmcd  * Expand the diagnostic code into a message.
16303055Sdanmcd  */
16313055Sdanmcd void
print_diagnostic(FILE * file,uint16_t diagnostic)16323055Sdanmcd print_diagnostic(FILE *file, uint16_t diagnostic)
16333055Sdanmcd {
16343055Sdanmcd 	/* Use two spaces so above strings can fit on the line. */
16354064Smarkfen 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
16364064Smarkfen 	    "  Diagnostic code %u:  %s.\n"),
16373055Sdanmcd 	    diagnostic, keysock_diag(diagnostic));
16383055Sdanmcd }
16393055Sdanmcd 
16403055Sdanmcd /*
16413055Sdanmcd  * Prints the base PF_KEY message.
16423055Sdanmcd  */
16433055Sdanmcd void
print_sadb_msg(FILE * file,struct sadb_msg * samsg,time_t wallclock,boolean_t vflag)16444867Spwernau print_sadb_msg(FILE *file, struct sadb_msg *samsg, time_t wallclock,
16454867Spwernau     boolean_t vflag)
16463055Sdanmcd {
16473055Sdanmcd 	if (wallclock != 0)
16484867Spwernau 		printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
16494064Smarkfen 		    "%sTimestamp: %s\n"), "", NULL,
16503055Sdanmcd 		    vflag);
16513055Sdanmcd 
16524867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
16534867Spwernau 	    "Base message (version %u) type "),
16543055Sdanmcd 	    samsg->sadb_msg_version);
16553055Sdanmcd 	switch (samsg->sadb_msg_type) {
16563055Sdanmcd 	case SADB_RESERVED:
16574867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16584064Smarkfen 		    "RESERVED (warning: set to 0)"));
16593055Sdanmcd 		break;
16603055Sdanmcd 	case SADB_GETSPI:
16614867Spwernau 		(void) fprintf(file, "GETSPI");
16623055Sdanmcd 		break;
16633055Sdanmcd 	case SADB_UPDATE:
16644867Spwernau 		(void) fprintf(file, "UPDATE");
16653055Sdanmcd 		break;
16666668Smarkfen 	case SADB_X_UPDATEPAIR:
16676668Smarkfen 		(void) fprintf(file, "UPDATE PAIR");
16686668Smarkfen 		break;
16693055Sdanmcd 	case SADB_ADD:
16704867Spwernau 		(void) fprintf(file, "ADD");
16713055Sdanmcd 		break;
16723055Sdanmcd 	case SADB_DELETE:
16734867Spwernau 		(void) fprintf(file, "DELETE");
16743055Sdanmcd 		break;
16756668Smarkfen 	case SADB_X_DELPAIR:
16766668Smarkfen 		(void) fprintf(file, "DELETE PAIR");
16776668Smarkfen 		break;
16783055Sdanmcd 	case SADB_GET:
16794867Spwernau 		(void) fprintf(file, "GET");
16803055Sdanmcd 		break;
16813055Sdanmcd 	case SADB_ACQUIRE:
16824867Spwernau 		(void) fprintf(file, "ACQUIRE");
16833055Sdanmcd 		break;
16843055Sdanmcd 	case SADB_REGISTER:
16854867Spwernau 		(void) fprintf(file, "REGISTER");
16863055Sdanmcd 		break;
16873055Sdanmcd 	case SADB_EXPIRE:
16884867Spwernau 		(void) fprintf(file, "EXPIRE");
16893055Sdanmcd 		break;
16903055Sdanmcd 	case SADB_FLUSH:
16914867Spwernau 		(void) fprintf(file, "FLUSH");
16923055Sdanmcd 		break;
16933055Sdanmcd 	case SADB_DUMP:
16944867Spwernau 		(void) fprintf(file, "DUMP");
16953055Sdanmcd 		break;
16963055Sdanmcd 	case SADB_X_PROMISC:
16974867Spwernau 		(void) fprintf(file, "X_PROMISC");
16983055Sdanmcd 		break;
16993055Sdanmcd 	case SADB_X_INVERSE_ACQUIRE:
17004867Spwernau 		(void) fprintf(file, "X_INVERSE_ACQUIRE");
17013055Sdanmcd 		break;
17023055Sdanmcd 	default:
17034867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17044064Smarkfen 		    "Unknown (%u)"), samsg->sadb_msg_type);
17053055Sdanmcd 		break;
17063055Sdanmcd 	}
17074867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, ", SA type "));
17083055Sdanmcd 
17093055Sdanmcd 	switch (samsg->sadb_msg_satype) {
17103055Sdanmcd 	case SADB_SATYPE_UNSPEC:
17114867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17124867Spwernau 		    "<unspecified/all>"));
17133055Sdanmcd 		break;
17143055Sdanmcd 	case SADB_SATYPE_AH:
17154867Spwernau 		(void) fprintf(file, "AH");
17163055Sdanmcd 		break;
17173055Sdanmcd 	case SADB_SATYPE_ESP:
17184867Spwernau 		(void) fprintf(file, "ESP");
17193055Sdanmcd 		break;
17203055Sdanmcd 	case SADB_SATYPE_RSVP:
17214867Spwernau 		(void) fprintf(file, "RSVP");
17223055Sdanmcd 		break;
17233055Sdanmcd 	case SADB_SATYPE_OSPFV2:
17244867Spwernau 		(void) fprintf(file, "OSPFv2");
17253055Sdanmcd 		break;
17263055Sdanmcd 	case SADB_SATYPE_RIPV2:
17274867Spwernau 		(void) fprintf(file, "RIPv2");
17283055Sdanmcd 		break;
17293055Sdanmcd 	case SADB_SATYPE_MIP:
17304867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Mobile IP"));
17313055Sdanmcd 		break;
17323055Sdanmcd 	default:
17334867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17344064Smarkfen 		    "<unknown %u>"), samsg->sadb_msg_satype);
17353055Sdanmcd 		break;
17363055Sdanmcd 	}
17373055Sdanmcd 
17384867Spwernau 	(void) fprintf(file, ".\n");
17393055Sdanmcd 
17403055Sdanmcd 	if (samsg->sadb_msg_errno != 0) {
17414867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17424867Spwernau 		    "Error %s from PF_KEY.\n"),
17433055Sdanmcd 		    strerror(samsg->sadb_msg_errno));
17444867Spwernau 		print_diagnostic(file, samsg->sadb_x_msg_diagnostic);
17453055Sdanmcd 	}
17463055Sdanmcd 
17474867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
17484064Smarkfen 	    "Message length %u bytes, seq=%u, pid=%u.\n"),
17493055Sdanmcd 	    SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq,
17503055Sdanmcd 	    samsg->sadb_msg_pid);
17513055Sdanmcd }
17523055Sdanmcd 
17533055Sdanmcd /*
17543055Sdanmcd  * Print the SA extension for PF_KEY.
17553055Sdanmcd  */
17563055Sdanmcd void
print_sa(FILE * file,char * prefix,struct sadb_sa * assoc)17574867Spwernau print_sa(FILE *file, char *prefix, struct sadb_sa *assoc)
17583055Sdanmcd {
17593055Sdanmcd 	if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) {
17604867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
17614064Smarkfen 		    "WARNING: SA info extension length (%u) is bad."),
17623055Sdanmcd 		    SADB_64TO8(assoc->sadb_sa_len));
17633055Sdanmcd 	}
17643055Sdanmcd 
17654867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
17667749SThejaswini.Singarajipura@Sun.COM 	    "%sSADB_ASSOC spi=0x%x, replay window size=%u, state="),
17673055Sdanmcd 	    prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay);
17683055Sdanmcd 	switch (assoc->sadb_sa_state) {
17693055Sdanmcd 	case SADB_SASTATE_LARVAL:
17704867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "LARVAL"));
17713055Sdanmcd 		break;
17723055Sdanmcd 	case SADB_SASTATE_MATURE:
17734867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "MATURE"));
17743055Sdanmcd 		break;
17753055Sdanmcd 	case SADB_SASTATE_DYING:
17764867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "DYING"));
17773055Sdanmcd 		break;
17783055Sdanmcd 	case SADB_SASTATE_DEAD:
17794867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "DEAD"));
17803055Sdanmcd 		break;
17817749SThejaswini.Singarajipura@Sun.COM 	case SADB_X_SASTATE_ACTIVE_ELSEWHERE:
17827749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17837749SThejaswini.Singarajipura@Sun.COM 		    "ACTIVE_ELSEWHERE"));
17847749SThejaswini.Singarajipura@Sun.COM 		break;
17857749SThejaswini.Singarajipura@Sun.COM 	case SADB_X_SASTATE_IDLE:
17867749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "IDLE"));
17877749SThejaswini.Singarajipura@Sun.COM 		break;
17883055Sdanmcd 	default:
17894867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17904064Smarkfen 		    "<unknown %u>"), assoc->sadb_sa_state);
17913055Sdanmcd 	}
17923055Sdanmcd 
17933055Sdanmcd 	if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
17944867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17954064Smarkfen 		    "\n%sAuthentication algorithm = "),
17963055Sdanmcd 		    prefix);
17974867Spwernau 		(void) dump_aalg(assoc->sadb_sa_auth, file);
17983055Sdanmcd 	}
17993055Sdanmcd 
18003055Sdanmcd 	if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
18014867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18024064Smarkfen 		    "\n%sEncryption algorithm = "), prefix);
18034867Spwernau 		(void) dump_ealg(assoc->sadb_sa_encrypt, file);
18043055Sdanmcd 	}
18053055Sdanmcd 
18064867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix,
18073055Sdanmcd 	    assoc->sadb_sa_flags);
18083055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS)
18094867Spwernau 		(void) fprintf(file, "PFS ");
18103055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY)
18114867Spwernau 		(void) fprintf(file, "NOREPLAY ");
18123055Sdanmcd 
18133055Sdanmcd 	/* BEGIN Solaris-specific flags. */
18143055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED)
18154867Spwernau 		(void) fprintf(file, "X_USED ");
18166668Smarkfen 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_PAIRED)
18176668Smarkfen 		(void) fprintf(file, "X_PAIRED ");
18186668Smarkfen 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_OUTBOUND)
18196668Smarkfen 		(void) fprintf(file, "X_OUTBOUND ");
18206668Smarkfen 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_INBOUND)
18216668Smarkfen 		(void) fprintf(file, "X_INBOUND ");
18223055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE)
18234867Spwernau 		(void) fprintf(file, "X_UNIQUE ");
18243055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1)
18254867Spwernau 		(void) fprintf(file, "X_AALG1 ");
18263055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2)
18274867Spwernau 		(void) fprintf(file, "X_AALG2 ");
18283055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1)
18294867Spwernau 		(void) fprintf(file, "X_EALG1 ");
18303055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2)
18314867Spwernau 		(void) fprintf(file, "X_EALG2 ");
18323055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC)
18334867Spwernau 		(void) fprintf(file, "X_NATT_LOC ");
18343055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM)
18354867Spwernau 		(void) fprintf(file, "X_NATT_REM ");
18363055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL)
18374867Spwernau 		(void) fprintf(file, "X_TUNNEL ");
18387066Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATTED)
18397066Sdanmcd 		(void) fprintf(file, "X_NATTED ");
18403055Sdanmcd 	/* END Solaris-specific flags. */
18413055Sdanmcd 
18424867Spwernau 	(void) fprintf(file, ">\n");
18433055Sdanmcd }
18443055Sdanmcd 
18453055Sdanmcd void
printsatime(FILE * file,int64_t lt,const char * msg,const char * pfx,const char * pfx2,boolean_t vflag)18464867Spwernau printsatime(FILE *file, int64_t lt, const char *msg, const char *pfx,
18474867Spwernau     const char *pfx2, boolean_t vflag)
18483055Sdanmcd {
18493055Sdanmcd 	char tbuf[TBUF_SIZE]; /* For strftime() call. */
18503055Sdanmcd 	const char *tp = tbuf;
18513055Sdanmcd 	time_t t = lt;
18523055Sdanmcd 	struct tm res;
18533055Sdanmcd 
18543055Sdanmcd 	if (t != lt) {
18553055Sdanmcd 		if (lt > 0)
18563055Sdanmcd 			t = LONG_MAX;
18573055Sdanmcd 		else
18583055Sdanmcd 			t = LONG_MIN;
18593055Sdanmcd 	}
18603055Sdanmcd 
18613055Sdanmcd 	if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0)
18624064Smarkfen 		tp = dgettext(TEXT_DOMAIN, "<time conversion failed>");
18634867Spwernau 	(void) fprintf(file, msg, pfx, tp);
18643055Sdanmcd 	if (vflag && (pfx2 != NULL))
18654867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18667320Sdanmcd@sun.com 		    "%s\t(raw time value %" PRIu64 ")\n"), pfx2, lt);
18673055Sdanmcd }
18683055Sdanmcd 
18693055Sdanmcd /*
18703055Sdanmcd  * Print the SA lifetime information.  (An SADB_EXT_LIFETIME_* extension.)
18713055Sdanmcd  */
18723055Sdanmcd void
print_lifetimes(FILE * file,time_t wallclock,struct sadb_lifetime * current,struct sadb_lifetime * hard,struct sadb_lifetime * soft,struct sadb_lifetime * idle,boolean_t vflag)18734867Spwernau print_lifetimes(FILE *file, time_t wallclock, struct sadb_lifetime *current,
18747749SThejaswini.Singarajipura@Sun.COM     struct sadb_lifetime *hard, struct sadb_lifetime *soft,
18757749SThejaswini.Singarajipura@Sun.COM     struct sadb_lifetime *idle, boolean_t vflag)
18763055Sdanmcd {
18773055Sdanmcd 	int64_t scratch;
18784064Smarkfen 	char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: ");
18794064Smarkfen 	char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: ");
18804064Smarkfen 	char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: ");
18817749SThejaswini.Singarajipura@Sun.COM 	char *idle_prefix = dgettext(TEXT_DOMAIN, "ILT: ");
1882*11379SVladimir.Kotal@Sun.COM 	char byte_str[BYTE_STR_SIZE]; /* byte lifetime string representation */
1883*11379SVladimir.Kotal@Sun.COM 	char secs_str[SECS_STR_SIZE]; /* buffer for seconds representation */
18843055Sdanmcd 
18853055Sdanmcd 	if (current != NULL &&
18863055Sdanmcd 	    current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) {
18874867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
18884064Smarkfen 		    "WARNING: CURRENT lifetime extension length (%u) is bad."),
18893055Sdanmcd 		    SADB_64TO8(current->sadb_lifetime_len));
18903055Sdanmcd 	}
18913055Sdanmcd 
18923055Sdanmcd 	if (hard != NULL &&
18933055Sdanmcd 	    hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) {
18944867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
18954867Spwernau 		    "WARNING: HARD lifetime extension length (%u) is bad."),
18963055Sdanmcd 		    SADB_64TO8(hard->sadb_lifetime_len));
18973055Sdanmcd 	}
18983055Sdanmcd 
18993055Sdanmcd 	if (soft != NULL &&
19003055Sdanmcd 	    soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) {
19014867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
19024867Spwernau 		    "WARNING: SOFT lifetime extension length (%u) is bad."),
19033055Sdanmcd 		    SADB_64TO8(soft->sadb_lifetime_len));
19043055Sdanmcd 	}
19053055Sdanmcd 
19067749SThejaswini.Singarajipura@Sun.COM 	if (idle != NULL &&
19077749SThejaswini.Singarajipura@Sun.COM 	    idle->sadb_lifetime_len != SADB_8TO64(sizeof (*idle))) {
19087749SThejaswini.Singarajipura@Sun.COM 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
19097749SThejaswini.Singarajipura@Sun.COM 		    "WARNING: IDLE lifetime extension length (%u) is bad."),
19107749SThejaswini.Singarajipura@Sun.COM 		    SADB_64TO8(idle->sadb_lifetime_len));
19117749SThejaswini.Singarajipura@Sun.COM 	}
19127749SThejaswini.Singarajipura@Sun.COM 
19134867Spwernau 	(void) fprintf(file, " LT: Lifetime information\n");
19143055Sdanmcd 	if (current != NULL) {
19153055Sdanmcd 		/* Express values as current values. */
19164867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1917*11379SVladimir.Kotal@Sun.COM 		    "%sCurrent lifetime information:\n"),
1918*11379SVladimir.Kotal@Sun.COM 		    current_prefix);
1919*11379SVladimir.Kotal@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1920*11379SVladimir.Kotal@Sun.COM 		    "%s%" PRIu64 " bytes %sprotected, %u allocations "
1921*11379SVladimir.Kotal@Sun.COM 		    "used.\n"), current_prefix,
1922*11379SVladimir.Kotal@Sun.COM 		    current->sadb_lifetime_bytes,
1923*11379SVladimir.Kotal@Sun.COM 		    bytecnt2out(current->sadb_lifetime_bytes, byte_str,
1924*11379SVladimir.Kotal@Sun.COM 		    sizeof (byte_str), SPC_END),
19253055Sdanmcd 		    current->sadb_lifetime_allocations);
19264867Spwernau 		printsatime(file, current->sadb_lifetime_addtime,
1927*11379SVladimir.Kotal@Sun.COM 		    dgettext(TEXT_DOMAIN, "%sSA added at time: %s\n"),
19283055Sdanmcd 		    current_prefix, current_prefix, vflag);
19293055Sdanmcd 		if (current->sadb_lifetime_usetime != 0) {
19304867Spwernau 			printsatime(file, current->sadb_lifetime_usetime,
19314064Smarkfen 			    dgettext(TEXT_DOMAIN,
19324064Smarkfen 			    "%sSA first used at time %s\n"),
19333055Sdanmcd 			    current_prefix, current_prefix, vflag);
19343055Sdanmcd 		}
19354867Spwernau 		printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
19364064Smarkfen 		    "%sTime now is %s\n"), current_prefix, current_prefix,
19374064Smarkfen 		    vflag);
19383055Sdanmcd 	}
19393055Sdanmcd 
19403055Sdanmcd 	if (soft != NULL) {
19414867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1942*11379SVladimir.Kotal@Sun.COM 		    "%sSoft lifetime information:\n"),
19433055Sdanmcd 		    soft_prefix);
19444867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1945*11379SVladimir.Kotal@Sun.COM 		    "%s%" PRIu64 " bytes %sof lifetime, %u allocations.\n"),
1946*11379SVladimir.Kotal@Sun.COM 		    soft_prefix,
1947*11379SVladimir.Kotal@Sun.COM 		    soft->sadb_lifetime_bytes,
1948*11379SVladimir.Kotal@Sun.COM 		    bytecnt2out(soft->sadb_lifetime_bytes, byte_str,
1949*11379SVladimir.Kotal@Sun.COM 		    sizeof (byte_str), SPC_END),
19503055Sdanmcd 		    soft->sadb_lifetime_allocations);
19514867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1952*11379SVladimir.Kotal@Sun.COM 		    "%s%" PRIu64 " seconds %sof post-add lifetime.\n"),
1953*11379SVladimir.Kotal@Sun.COM 		    soft_prefix, soft->sadb_lifetime_addtime,
1954*11379SVladimir.Kotal@Sun.COM 		    secs2out(soft->sadb_lifetime_addtime, secs_str,
1955*11379SVladimir.Kotal@Sun.COM 		    sizeof (secs_str), SPC_END));
19564867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1957*11379SVladimir.Kotal@Sun.COM 		    "%s%" PRIu64 " seconds %sof post-use lifetime.\n"),
1958*11379SVladimir.Kotal@Sun.COM 		    soft_prefix, soft->sadb_lifetime_usetime,
1959*11379SVladimir.Kotal@Sun.COM 		    secs2out(soft->sadb_lifetime_usetime, secs_str,
1960*11379SVladimir.Kotal@Sun.COM 		    sizeof (secs_str), SPC_END));
19613055Sdanmcd 		/* If possible, express values as time remaining. */
19623055Sdanmcd 		if (current != NULL) {
19633055Sdanmcd 			if (soft->sadb_lifetime_bytes != 0)
1964*11379SVladimir.Kotal@Sun.COM 				(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s"
1965*11379SVladimir.Kotal@Sun.COM 				    "%" PRIu64 " bytes %smore can be "
1966*11379SVladimir.Kotal@Sun.COM 				    "protected.\n"), soft_prefix,
19673055Sdanmcd 				    (soft->sadb_lifetime_bytes >
19684342Spwernau 				    current->sadb_lifetime_bytes) ?
1969*11379SVladimir.Kotal@Sun.COM 				    soft->sadb_lifetime_bytes -
1970*11379SVladimir.Kotal@Sun.COM 				    current->sadb_lifetime_bytes : 0,
1971*11379SVladimir.Kotal@Sun.COM 				    (soft->sadb_lifetime_bytes >
1972*11379SVladimir.Kotal@Sun.COM 				    current->sadb_lifetime_bytes) ?
1973*11379SVladimir.Kotal@Sun.COM 				    bytecnt2out(soft->sadb_lifetime_bytes -
1974*11379SVladimir.Kotal@Sun.COM 				    current->sadb_lifetime_bytes, byte_str,
1975*11379SVladimir.Kotal@Sun.COM 				    sizeof (byte_str), SPC_END) : "");
19763055Sdanmcd 			if (soft->sadb_lifetime_addtime != 0 ||
19773055Sdanmcd 			    (soft->sadb_lifetime_usetime != 0 &&
19784342Spwernau 			    current->sadb_lifetime_usetime != 0)) {
19793055Sdanmcd 				int64_t adddelta, usedelta;
19803055Sdanmcd 
19813055Sdanmcd 				if (soft->sadb_lifetime_addtime != 0) {
19823055Sdanmcd 					adddelta =
19833055Sdanmcd 					    current->sadb_lifetime_addtime +
19843055Sdanmcd 					    soft->sadb_lifetime_addtime -
19853055Sdanmcd 					    wallclock;
19863055Sdanmcd 				} else {
19873055Sdanmcd 					adddelta = TIME_MAX;
19883055Sdanmcd 				}
19893055Sdanmcd 
19903055Sdanmcd 				if (soft->sadb_lifetime_usetime != 0 &&
19913055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
19923055Sdanmcd 					usedelta =
19933055Sdanmcd 					    current->sadb_lifetime_usetime +
19943055Sdanmcd 					    soft->sadb_lifetime_usetime -
19953055Sdanmcd 					    wallclock;
19963055Sdanmcd 				} else {
19973055Sdanmcd 					usedelta = TIME_MAX;
19983055Sdanmcd 				}
19994867Spwernau 				(void) fprintf(file, "%s", soft_prefix);
20003055Sdanmcd 				scratch = MIN(adddelta, usedelta);
20013055Sdanmcd 				if (scratch >= 0) {
20024867Spwernau 					(void) fprintf(file,
20034867Spwernau 					    dgettext(TEXT_DOMAIN,
20047320Sdanmcd@sun.com 					    "Soft expiration occurs in %"
2005*11379SVladimir.Kotal@Sun.COM 					    PRId64 " seconds%s\n"), scratch,
2006*11379SVladimir.Kotal@Sun.COM 					    secs2out(scratch, secs_str,
2007*11379SVladimir.Kotal@Sun.COM 					    sizeof (secs_str), SPC_BEGIN));
20083055Sdanmcd 				} else {
20094867Spwernau 					(void) fprintf(file,
20104867Spwernau 					    dgettext(TEXT_DOMAIN,
2011*11379SVladimir.Kotal@Sun.COM 					    "Soft expiration occurred\n"));
20123055Sdanmcd 				}
20133055Sdanmcd 				scratch += wallclock;
20144867Spwernau 				printsatime(file, scratch, dgettext(TEXT_DOMAIN,
2015*11379SVladimir.Kotal@Sun.COM 				    "%sTime of expiration: %s.\n"),
2016*11379SVladimir.Kotal@Sun.COM 				    soft_prefix, soft_prefix, vflag);
20173055Sdanmcd 			}
20183055Sdanmcd 		}
20193055Sdanmcd 	}
20203055Sdanmcd 
20213055Sdanmcd 	if (hard != NULL) {
20224867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2023*11379SVladimir.Kotal@Sun.COM 		    "%sHard lifetime information:\n"), hard_prefix);
20244867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2025*11379SVladimir.Kotal@Sun.COM 		    "%s%" PRIu64 " bytes %sof lifetime, %u allocations.\n"),
2026*11379SVladimir.Kotal@Sun.COM 		    hard_prefix,
2027*11379SVladimir.Kotal@Sun.COM 		    hard->sadb_lifetime_bytes,
2028*11379SVladimir.Kotal@Sun.COM 		    bytecnt2out(hard->sadb_lifetime_bytes, byte_str,
2029*11379SVladimir.Kotal@Sun.COM 		    sizeof (byte_str), SPC_END),
2030*11379SVladimir.Kotal@Sun.COM 		    hard->sadb_lifetime_allocations);
20314867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2032*11379SVladimir.Kotal@Sun.COM 		    "%s%" PRIu64 " seconds %sof post-add lifetime.\n"),
2033*11379SVladimir.Kotal@Sun.COM 		    hard_prefix, hard->sadb_lifetime_addtime,
2034*11379SVladimir.Kotal@Sun.COM 		    secs2out(hard->sadb_lifetime_addtime, secs_str,
2035*11379SVladimir.Kotal@Sun.COM 		    sizeof (secs_str), SPC_END));
20364867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2037*11379SVladimir.Kotal@Sun.COM 		    "%s%" PRIu64 " seconds %sof post-use lifetime.\n"),
2038*11379SVladimir.Kotal@Sun.COM 		    hard_prefix, hard->sadb_lifetime_usetime,
2039*11379SVladimir.Kotal@Sun.COM 		    secs2out(hard->sadb_lifetime_usetime, secs_str,
2040*11379SVladimir.Kotal@Sun.COM 		    sizeof (secs_str), SPC_END));
20413055Sdanmcd 		/* If possible, express values as time remaining. */
20423055Sdanmcd 		if (current != NULL) {
20433055Sdanmcd 			if (hard->sadb_lifetime_bytes != 0)
2044*11379SVladimir.Kotal@Sun.COM 				(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s"
2045*11379SVladimir.Kotal@Sun.COM 				    "%" PRIu64 " bytes %smore can be "
2046*11379SVladimir.Kotal@Sun.COM 				    "protected.\n"), hard_prefix,
20473055Sdanmcd 				    (hard->sadb_lifetime_bytes >
20484342Spwernau 				    current->sadb_lifetime_bytes) ?
2049*11379SVladimir.Kotal@Sun.COM 				    hard->sadb_lifetime_bytes -
2050*11379SVladimir.Kotal@Sun.COM 				    current->sadb_lifetime_bytes : 0,
2051*11379SVladimir.Kotal@Sun.COM 				    (hard->sadb_lifetime_bytes >
2052*11379SVladimir.Kotal@Sun.COM 				    current->sadb_lifetime_bytes) ?
2053*11379SVladimir.Kotal@Sun.COM 				    bytecnt2out(hard->sadb_lifetime_bytes -
2054*11379SVladimir.Kotal@Sun.COM 				    current->sadb_lifetime_bytes, byte_str,
2055*11379SVladimir.Kotal@Sun.COM 				    sizeof (byte_str), SPC_END) : "");
20563055Sdanmcd 			if (hard->sadb_lifetime_addtime != 0 ||
20573055Sdanmcd 			    (hard->sadb_lifetime_usetime != 0 &&
20584342Spwernau 			    current->sadb_lifetime_usetime != 0)) {
20593055Sdanmcd 				int64_t adddelta, usedelta;
20603055Sdanmcd 
20613055Sdanmcd 				if (hard->sadb_lifetime_addtime != 0) {
20623055Sdanmcd 					adddelta =
20633055Sdanmcd 					    current->sadb_lifetime_addtime +
20643055Sdanmcd 					    hard->sadb_lifetime_addtime -
20653055Sdanmcd 					    wallclock;
20663055Sdanmcd 				} else {
20673055Sdanmcd 					adddelta = TIME_MAX;
20683055Sdanmcd 				}
20693055Sdanmcd 
20703055Sdanmcd 				if (hard->sadb_lifetime_usetime != 0 &&
20713055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
20723055Sdanmcd 					usedelta =
20733055Sdanmcd 					    current->sadb_lifetime_usetime +
20743055Sdanmcd 					    hard->sadb_lifetime_usetime -
20753055Sdanmcd 					    wallclock;
20763055Sdanmcd 				} else {
20773055Sdanmcd 					usedelta = TIME_MAX;
20783055Sdanmcd 				}
20794867Spwernau 				(void) fprintf(file, "%s", hard_prefix);
20803055Sdanmcd 				scratch = MIN(adddelta, usedelta);
20813055Sdanmcd 				if (scratch >= 0) {
20824867Spwernau 					(void) fprintf(file,
20834867Spwernau 					    dgettext(TEXT_DOMAIN,
20847320Sdanmcd@sun.com 					    "Hard expiration occurs in %"
2085*11379SVladimir.Kotal@Sun.COM 					    PRId64 " seconds%s\n"), scratch,
2086*11379SVladimir.Kotal@Sun.COM 					    secs2out(scratch, secs_str,
2087*11379SVladimir.Kotal@Sun.COM 					    sizeof (secs_str), SPC_BEGIN));
20883055Sdanmcd 				} else {
20894867Spwernau 					(void) fprintf(file,
20904867Spwernau 					    dgettext(TEXT_DOMAIN,
2091*11379SVladimir.Kotal@Sun.COM 					    "Hard expiration occurred\n"));
20923055Sdanmcd 				}
20933055Sdanmcd 				scratch += wallclock;
20944867Spwernau 				printsatime(file, scratch, dgettext(TEXT_DOMAIN,
2095*11379SVladimir.Kotal@Sun.COM 				    "%sTime of expiration: %s.\n"),
2096*11379SVladimir.Kotal@Sun.COM 				    hard_prefix, hard_prefix, vflag);
20973055Sdanmcd 			}
20983055Sdanmcd 		}
20993055Sdanmcd 	}
21007749SThejaswini.Singarajipura@Sun.COM 	if (idle != NULL) {
21017749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2102*11379SVladimir.Kotal@Sun.COM 		    "%sIdle lifetime information:\n"), idle_prefix);
21037749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2104*11379SVladimir.Kotal@Sun.COM 		    "%s%" PRIu64 " seconds %sof post-add lifetime.\n"),
2105*11379SVladimir.Kotal@Sun.COM 		    idle_prefix, idle->sadb_lifetime_addtime,
2106*11379SVladimir.Kotal@Sun.COM 		    secs2out(idle->sadb_lifetime_addtime, secs_str,
2107*11379SVladimir.Kotal@Sun.COM 		    sizeof (secs_str), SPC_END));
21087749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2109*11379SVladimir.Kotal@Sun.COM 		    "%s%" PRIu64 " seconds %sof post-use lifetime.\n"),
2110*11379SVladimir.Kotal@Sun.COM 		    idle_prefix, idle->sadb_lifetime_usetime,
2111*11379SVladimir.Kotal@Sun.COM 		    secs2out(idle->sadb_lifetime_usetime, secs_str,
2112*11379SVladimir.Kotal@Sun.COM 		    sizeof (secs_str), SPC_END));
21137749SThejaswini.Singarajipura@Sun.COM 	}
21143055Sdanmcd }
21153055Sdanmcd 
21163055Sdanmcd /*
21173055Sdanmcd  * Print an SADB_EXT_ADDRESS_* extension.
21183055Sdanmcd  */
21193055Sdanmcd void
print_address(FILE * file,char * prefix,struct sadb_address * addr,boolean_t ignore_nss)21204867Spwernau print_address(FILE *file, char *prefix, struct sadb_address *addr,
21214867Spwernau     boolean_t ignore_nss)
21223055Sdanmcd {
21233055Sdanmcd 	struct protoent *pe;
21243055Sdanmcd 
21254867Spwernau 	(void) fprintf(file, "%s", prefix);
21263055Sdanmcd 	switch (addr->sadb_address_exttype) {
21273055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
21284867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Source address "));
21293055Sdanmcd 		break;
21303055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
21314867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
21324867Spwernau 		    "Inner source address "));
21333055Sdanmcd 		break;
21343055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
21354867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
21364867Spwernau 		    "Destination address "));
21373055Sdanmcd 		break;
21383055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
21394867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
21404064Smarkfen 		    "Inner destination address "));
21413055Sdanmcd 		break;
21423055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
21434867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
21444867Spwernau 		    "NAT-T local address "));
21453055Sdanmcd 		break;
21463055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
21474867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
21484867Spwernau 		    "NAT-T remote address "));
21493055Sdanmcd 		break;
21503055Sdanmcd 	}
21513055Sdanmcd 
21524867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
21534064Smarkfen 	    "(proto=%d"), addr->sadb_address_proto);
21544867Spwernau 	if (ignore_nss == B_FALSE) {
21553055Sdanmcd 		if (addr->sadb_address_proto == 0) {
21564867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
21574867Spwernau 			    "/<unspecified>"));
21583055Sdanmcd 		} else if ((pe = getprotobynumber(addr->sadb_address_proto))
21593055Sdanmcd 		    != NULL) {
21604867Spwernau 			(void) fprintf(file, "/%s", pe->p_name);
21613055Sdanmcd 		} else {
21624867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
21634867Spwernau 			    "/<unknown>"));
21643055Sdanmcd 		}
21653055Sdanmcd 	}
21664867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, ")\n%s"), prefix);
21673055Sdanmcd 	(void) dump_sockaddr((struct sockaddr *)(addr + 1),
21684867Spwernau 	    addr->sadb_address_prefixlen, B_FALSE, file, ignore_nss);
21693055Sdanmcd }
21703055Sdanmcd 
21713055Sdanmcd /*
21723055Sdanmcd  * Print an SADB_EXT_KEY extension.
21733055Sdanmcd  */
21743055Sdanmcd void
print_key(FILE * file,char * prefix,struct sadb_key * key)21754867Spwernau print_key(FILE *file, char *prefix, struct sadb_key *key)
21763055Sdanmcd {
21774867Spwernau 	(void) fprintf(file, "%s", prefix);
21783055Sdanmcd 
21793055Sdanmcd 	switch (key->sadb_key_exttype) {
21803055Sdanmcd 	case SADB_EXT_KEY_AUTH:
21814867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Authentication"));
21823055Sdanmcd 		break;
21833055Sdanmcd 	case SADB_EXT_KEY_ENCRYPT:
21844867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Encryption"));
21853055Sdanmcd 		break;
21863055Sdanmcd 	}
21873055Sdanmcd 
21884867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, " key.\n%s"), prefix);
218910824SMark.Fenwick@Sun.COM 	(void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits,
219010824SMark.Fenwick@Sun.COM 	    key->sadb_key_reserved, file, B_TRUE);
21914867Spwernau 	(void) fprintf(file, "\n");
21923055Sdanmcd }
21933055Sdanmcd 
21943055Sdanmcd /*
21953055Sdanmcd  * Print an SADB_EXT_IDENTITY_* extension.
21963055Sdanmcd  */
21973055Sdanmcd void
print_ident(FILE * file,char * prefix,struct sadb_ident * id)21984867Spwernau print_ident(FILE *file, char *prefix, struct sadb_ident *id)
21993055Sdanmcd {
22003055Sdanmcd 	boolean_t canprint = B_TRUE;
22013055Sdanmcd 
22024867Spwernau 	(void) fprintf(file, "%s", prefix);
22033055Sdanmcd 	switch (id->sadb_ident_exttype) {
22043055Sdanmcd 	case SADB_EXT_IDENTITY_SRC:
22054867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Source"));
22063055Sdanmcd 		break;
22073055Sdanmcd 	case SADB_EXT_IDENTITY_DST:
22084867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Destination"));
22093055Sdanmcd 		break;
22103055Sdanmcd 	}
22113055Sdanmcd 
22124867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
22134064Smarkfen 	    " identity, uid=%d, type "), id->sadb_ident_id);
22144867Spwernau 	canprint = dump_sadb_idtype(id->sadb_ident_type, file, NULL);
22154867Spwernau 	(void) fprintf(file, "\n%s", prefix);
22166119Spwernau 	if (canprint) {
22174867Spwernau 		(void) fprintf(file, "%s\n", (char *)(id + 1));
22186119Spwernau 	} else {
22196119Spwernau 		print_asn1_name(file, (const unsigned char *)(id + 1),
22206119Spwernau 		    SADB_64TO8(id->sadb_ident_len) - sizeof (sadb_ident_t));
22216119Spwernau 	}
22223055Sdanmcd }
22233055Sdanmcd 
22243055Sdanmcd /*
222510934Ssommerfeld@sun.com  * Convert sadb_sens extension into binary security label.
222610934Ssommerfeld@sun.com  */
222710934Ssommerfeld@sun.com 
222810934Ssommerfeld@sun.com #include <tsol/label.h>
222910934Ssommerfeld@sun.com #include <sys/tsol/tndb.h>
223010934Ssommerfeld@sun.com #include <sys/tsol/label_macro.h>
223110934Ssommerfeld@sun.com 
223210934Ssommerfeld@sun.com void
ipsec_convert_sens_to_bslabel(const struct sadb_sens * sens,bslabel_t * sl)223310934Ssommerfeld@sun.com ipsec_convert_sens_to_bslabel(const struct sadb_sens *sens, bslabel_t *sl)
223410934Ssommerfeld@sun.com {
223510934Ssommerfeld@sun.com 	uint64_t *bitmap = (uint64_t *)(sens + 1);
223610934Ssommerfeld@sun.com 	int bitmap_len = SADB_64TO8(sens->sadb_sens_sens_len);
223710934Ssommerfeld@sun.com 
223810934Ssommerfeld@sun.com 	bsllow(sl);
223910934Ssommerfeld@sun.com 	LCLASS_SET((_bslabel_impl_t *)sl, sens->sadb_sens_sens_level);
224010934Ssommerfeld@sun.com 	bcopy(bitmap, &((_bslabel_impl_t *)sl)->compartments,
224110934Ssommerfeld@sun.com 	    bitmap_len);
224210934Ssommerfeld@sun.com }
224310934Ssommerfeld@sun.com 
224410934Ssommerfeld@sun.com void
ipsec_convert_bslabel_to_string(bslabel_t * sl,char ** plabel)224510934Ssommerfeld@sun.com ipsec_convert_bslabel_to_string(bslabel_t *sl, char **plabel)
224610934Ssommerfeld@sun.com {
224710934Ssommerfeld@sun.com 	if (label_to_str(sl, plabel, M_LABEL, DEF_NAMES) != 0) {
224810934Ssommerfeld@sun.com 		*plabel = strdup(dgettext(TEXT_DOMAIN,
224910934Ssommerfeld@sun.com 		    "** Label conversion failed **"));
225010934Ssommerfeld@sun.com 	}
225110934Ssommerfeld@sun.com }
225210934Ssommerfeld@sun.com 
225310934Ssommerfeld@sun.com void
ipsec_convert_bslabel_to_hex(bslabel_t * sl,char ** plabel)225410934Ssommerfeld@sun.com ipsec_convert_bslabel_to_hex(bslabel_t *sl, char **plabel)
225510934Ssommerfeld@sun.com {
225610934Ssommerfeld@sun.com 	if (label_to_str(sl, plabel, M_INTERNAL, DEF_NAMES) != 0) {
225710934Ssommerfeld@sun.com 		*plabel = strdup(dgettext(TEXT_DOMAIN,
225810934Ssommerfeld@sun.com 		    "** Label conversion failed **"));
225910934Ssommerfeld@sun.com 	}
226010934Ssommerfeld@sun.com }
226110934Ssommerfeld@sun.com 
226210934Ssommerfeld@sun.com int
ipsec_convert_sl_to_sens(int doi,bslabel_t * sl,sadb_sens_t * sens)226310934Ssommerfeld@sun.com ipsec_convert_sl_to_sens(int doi, bslabel_t *sl, sadb_sens_t *sens)
226410934Ssommerfeld@sun.com {
226510934Ssommerfeld@sun.com 	uint8_t *bitmap;
226610934Ssommerfeld@sun.com 	int sens_len = sizeof (sadb_sens_t) + _C_LEN * 4;
226710934Ssommerfeld@sun.com 
226810934Ssommerfeld@sun.com 
226910934Ssommerfeld@sun.com 	if (sens == NULL)
227010934Ssommerfeld@sun.com 		return (sens_len);
227110934Ssommerfeld@sun.com 
227210934Ssommerfeld@sun.com 
227310934Ssommerfeld@sun.com 	(void) memset(sens, 0, sens_len);
227410934Ssommerfeld@sun.com 
227510934Ssommerfeld@sun.com 	sens->sadb_sens_exttype = SADB_EXT_SENSITIVITY;
227610934Ssommerfeld@sun.com 	sens->sadb_sens_len = SADB_8TO64(sens_len);
227710934Ssommerfeld@sun.com 	sens->sadb_sens_dpd = doi;
227810934Ssommerfeld@sun.com 
227910934Ssommerfeld@sun.com 	sens->sadb_sens_sens_level = LCLASS(sl);
228010934Ssommerfeld@sun.com 	sens->sadb_sens_integ_level = 0;
228110934Ssommerfeld@sun.com 	sens->sadb_sens_sens_len = _C_LEN >> 1;
228210934Ssommerfeld@sun.com 	sens->sadb_sens_integ_len = 0;
228310934Ssommerfeld@sun.com 
228410934Ssommerfeld@sun.com 	sens->sadb_x_sens_flags = 0;
228510934Ssommerfeld@sun.com 
228610934Ssommerfeld@sun.com 	bitmap = (uint8_t *)(sens + 1);
228710934Ssommerfeld@sun.com 	bcopy(&(((_bslabel_impl_t *)sl)->compartments), bitmap, _C_LEN * 4);
228810934Ssommerfeld@sun.com 
228910934Ssommerfeld@sun.com 	return (sens_len);
229010934Ssommerfeld@sun.com }
229110934Ssommerfeld@sun.com 
229210934Ssommerfeld@sun.com 
229310934Ssommerfeld@sun.com /*
22943055Sdanmcd  * Print an SADB_SENSITIVITY extension.
22953055Sdanmcd  */
22963055Sdanmcd void
print_sens(FILE * file,char * prefix,const struct sadb_sens * sens,boolean_t ignore_nss)229710934Ssommerfeld@sun.com print_sens(FILE *file, char *prefix, const struct sadb_sens *sens,
229810934Ssommerfeld@sun.com 	boolean_t ignore_nss)
22993055Sdanmcd {
230010934Ssommerfeld@sun.com 	char *plabel;
230110934Ssommerfeld@sun.com 	char *hlabel;
23023055Sdanmcd 	uint64_t *bitmap = (uint64_t *)(sens + 1);
230310934Ssommerfeld@sun.com 	bslabel_t sl;
23043055Sdanmcd 	int i;
230510934Ssommerfeld@sun.com 	int sens_len = sens->sadb_sens_sens_len;
230610934Ssommerfeld@sun.com 	int integ_len = sens->sadb_sens_integ_len;
230710934Ssommerfeld@sun.com 	boolean_t inner = (sens->sadb_sens_exttype == SADB_EXT_SENSITIVITY);
230810934Ssommerfeld@sun.com 	const char *sensname = inner ?
230910934Ssommerfeld@sun.com 	    dgettext(TEXT_DOMAIN, "Plaintext Sensitivity") :
231010934Ssommerfeld@sun.com 	    dgettext(TEXT_DOMAIN, "Ciphertext Sensitivity");
231110934Ssommerfeld@sun.com 
231210934Ssommerfeld@sun.com 	ipsec_convert_sens_to_bslabel(sens, &sl);
23133055Sdanmcd 
23144867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
231510934Ssommerfeld@sun.com 	    "%s%s DPD %d, sens level=%d, integ level=%d, flags=%x\n"),
231610934Ssommerfeld@sun.com 	    prefix, sensname, sens->sadb_sens_dpd, sens->sadb_sens_sens_level,
231710934Ssommerfeld@sun.com 	    sens->sadb_sens_integ_level, sens->sadb_x_sens_flags);
231810934Ssommerfeld@sun.com 
231910934Ssommerfeld@sun.com 	ipsec_convert_bslabel_to_hex(&sl, &hlabel);
232010934Ssommerfeld@sun.com 
232110934Ssommerfeld@sun.com 	if (ignore_nss) {
23224867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
232310934Ssommerfeld@sun.com 		    "%s %s Label: %s\n"), prefix, sensname, hlabel);
232410934Ssommerfeld@sun.com 
232510934Ssommerfeld@sun.com 		for (i = 0; i < sens_len; i++, bitmap++)
232610934Ssommerfeld@sun.com 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
232710934Ssommerfeld@sun.com 			    "%s %s BM extended word %d 0x%" PRIx64 "\n"),
232810934Ssommerfeld@sun.com 			    prefix, sensname, i, *bitmap);
232910934Ssommerfeld@sun.com 
233010934Ssommerfeld@sun.com 	} else {
233110934Ssommerfeld@sun.com 		ipsec_convert_bslabel_to_string(&sl, &plabel);
233210934Ssommerfeld@sun.com 
233310934Ssommerfeld@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
233410934Ssommerfeld@sun.com 		    "%s %s Label: %s (%s)\n"),
233510934Ssommerfeld@sun.com 		    prefix, sensname, plabel, hlabel);
233610934Ssommerfeld@sun.com 		free(plabel);
233710934Ssommerfeld@sun.com 
233810934Ssommerfeld@sun.com 	}
233910934Ssommerfeld@sun.com 	free(hlabel);
234010934Ssommerfeld@sun.com 
234110934Ssommerfeld@sun.com 	bitmap = (uint64_t *)(sens + 1 + sens_len);
234210934Ssommerfeld@sun.com 
234310934Ssommerfeld@sun.com 	for (i = 0; i < integ_len; i++, bitmap++)
234410934Ssommerfeld@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
23457320Sdanmcd@sun.com 		    "%s Integrity BM extended word %d 0x%" PRIx64 "\n"),
23464867Spwernau 		    prefix, i, *bitmap);
23473055Sdanmcd }
23483055Sdanmcd 
23493055Sdanmcd /*
23503055Sdanmcd  * Print an SADB_EXT_PROPOSAL extension.
23513055Sdanmcd  */
23523055Sdanmcd void
print_prop(FILE * file,char * prefix,struct sadb_prop * prop)23534867Spwernau print_prop(FILE *file, char *prefix, struct sadb_prop *prop)
23543055Sdanmcd {
23553055Sdanmcd 	struct sadb_comb *combs;
23563055Sdanmcd 	int i, numcombs;
23573055Sdanmcd 
23584867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
23594064Smarkfen 	    "%sProposal, replay counter = %u.\n"), prefix,
23603055Sdanmcd 	    prop->sadb_prop_replay);
23613055Sdanmcd 
23623055Sdanmcd 	numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop));
23633055Sdanmcd 	numcombs /= SADB_8TO64(sizeof (*combs));
23643055Sdanmcd 
23653055Sdanmcd 	combs = (struct sadb_comb *)(prop + 1);
23663055Sdanmcd 
23673055Sdanmcd 	for (i = 0; i < numcombs; i++) {
23684867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
23694064Smarkfen 		    "%s Combination #%u "), prefix, i + 1);
23703055Sdanmcd 		if (combs[i].sadb_comb_auth != SADB_AALG_NONE) {
23714867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
23724064Smarkfen 			    "Authentication = "));
23734867Spwernau 			(void) dump_aalg(combs[i].sadb_comb_auth, file);
23744867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
23754064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
23763055Sdanmcd 			    combs[i].sadb_comb_auth_minbits,
23773055Sdanmcd 			    combs[i].sadb_comb_auth_maxbits, prefix);
23783055Sdanmcd 		}
23793055Sdanmcd 
23803055Sdanmcd 		if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) {
23814867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
23824867Spwernau 			    "Encryption = "));
23834867Spwernau 			(void) dump_ealg(combs[i].sadb_comb_encrypt, file);
23844867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
23854064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
23863055Sdanmcd 			    combs[i].sadb_comb_encrypt_minbits,
23873055Sdanmcd 			    combs[i].sadb_comb_encrypt_maxbits, prefix);
23883055Sdanmcd 		}
23893055Sdanmcd 
23904867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "HARD: "));
23913055Sdanmcd 		if (combs[i].sadb_comb_hard_allocations)
23924867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
23933055Sdanmcd 			    combs[i].sadb_comb_hard_allocations);
23943055Sdanmcd 		if (combs[i].sadb_comb_hard_bytes)
23957320Sdanmcd@sun.com 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%"
23967320Sdanmcd@sun.com 			    PRIu64 " "), combs[i].sadb_comb_hard_bytes);
23973055Sdanmcd 		if (combs[i].sadb_comb_hard_addtime)
23984867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
23997320Sdanmcd@sun.com 			    "post-add secs=%" PRIu64 " "),
24003055Sdanmcd 			    combs[i].sadb_comb_hard_addtime);
24013055Sdanmcd 		if (combs[i].sadb_comb_hard_usetime)
24024867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
24037320Sdanmcd@sun.com 			    "post-use secs=%" PRIu64 ""),
24043055Sdanmcd 			    combs[i].sadb_comb_hard_usetime);
24053055Sdanmcd 
24064867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%s SOFT: "),
24074867Spwernau 		    prefix);
24083055Sdanmcd 		if (combs[i].sadb_comb_soft_allocations)
24094867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
24103055Sdanmcd 			    combs[i].sadb_comb_soft_allocations);
24113055Sdanmcd 		if (combs[i].sadb_comb_soft_bytes)
24127320Sdanmcd@sun.com 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%"
24137320Sdanmcd@sun.com 			    PRIu64 " "), combs[i].sadb_comb_soft_bytes);
24143055Sdanmcd 		if (combs[i].sadb_comb_soft_addtime)
24154867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
24167320Sdanmcd@sun.com 			    "post-add secs=%" PRIu64 " "),
24173055Sdanmcd 			    combs[i].sadb_comb_soft_addtime);
24183055Sdanmcd 		if (combs[i].sadb_comb_soft_usetime)
24194867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
24207320Sdanmcd@sun.com 			    "post-use secs=%" PRIu64 ""),
24213055Sdanmcd 			    combs[i].sadb_comb_soft_usetime);
24224867Spwernau 		(void) fprintf(file, "\n");
24233055Sdanmcd 	}
24243055Sdanmcd }
24253055Sdanmcd 
24263055Sdanmcd /*
24273055Sdanmcd  * Print an extended proposal (SADB_X_EXT_EPROP).
24283055Sdanmcd  */
24293055Sdanmcd void
print_eprop(FILE * file,char * prefix,struct sadb_prop * eprop)24304867Spwernau print_eprop(FILE *file, char *prefix, struct sadb_prop *eprop)
24313055Sdanmcd {
24323055Sdanmcd 	uint64_t *sofar;
24333055Sdanmcd 	struct sadb_x_ecomb *ecomb;
24343055Sdanmcd 	struct sadb_x_algdesc *algdesc;
24353055Sdanmcd 	int i, j;
24363055Sdanmcd 
24374867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
24384064Smarkfen 	    "%sExtended Proposal, replay counter = %u, "), prefix,
24394064Smarkfen 	    eprop->sadb_prop_replay);
24404867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
24414867Spwernau 	    "number of combinations = %u.\n"), eprop->sadb_x_prop_numecombs);
24423055Sdanmcd 
24433055Sdanmcd 	sofar = (uint64_t *)(eprop + 1);
24443055Sdanmcd 	ecomb = (struct sadb_x_ecomb *)sofar;
24453055Sdanmcd 
24463055Sdanmcd 	for (i = 0; i < eprop->sadb_x_prop_numecombs; ) {
24474867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
24484064Smarkfen 		    "%s Extended combination #%u:\n"), prefix, ++i);
24493055Sdanmcd 
24504867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s HARD: "),
24514867Spwernau 		    prefix);
24524867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
24533055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_allocations);
24547320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%" PRIu64
24557320Sdanmcd@sun.com 		    ", "), ecomb->sadb_x_ecomb_hard_bytes);
24567320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-add secs=%"
24577320Sdanmcd@sun.com 		    PRIu64 ", "), ecomb->sadb_x_ecomb_hard_addtime);
24587320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-use secs=%"
24597320Sdanmcd@sun.com 		    PRIu64 "\n"), ecomb->sadb_x_ecomb_hard_usetime);
24603055Sdanmcd 
24614867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s SOFT: "),
24624867Spwernau 		    prefix);
24634867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
24643055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_allocations);
24657320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
24667320Sdanmcd@sun.com 		    "bytes=%" PRIu64 ", "), ecomb->sadb_x_ecomb_soft_bytes);
24674867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
24687320Sdanmcd@sun.com 		    "post-add secs=%" PRIu64 ", "),
24693055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_addtime);
24707320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-use secs=%"
24717320Sdanmcd@sun.com 		    PRIu64 "\n"), ecomb->sadb_x_ecomb_soft_usetime);
24723055Sdanmcd 
24733055Sdanmcd 		sofar = (uint64_t *)(ecomb + 1);
24743055Sdanmcd 		algdesc = (struct sadb_x_algdesc *)sofar;
24753055Sdanmcd 
24763055Sdanmcd 		for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) {
24774867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
24784064Smarkfen 			    "%s Alg #%u "), prefix, ++j);
24793055Sdanmcd 			switch (algdesc->sadb_x_algdesc_satype) {
24803055Sdanmcd 			case SADB_SATYPE_ESP:
24814867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
24824064Smarkfen 				    "for ESP "));
24833055Sdanmcd 				break;
24843055Sdanmcd 			case SADB_SATYPE_AH:
24854867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
24864867Spwernau 				    "for AH "));
24873055Sdanmcd 				break;
24883055Sdanmcd 			default:
24894867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
24904064Smarkfen 				    "for satype=%d "),
24913055Sdanmcd 				    algdesc->sadb_x_algdesc_satype);
24923055Sdanmcd 			}
24933055Sdanmcd 			switch (algdesc->sadb_x_algdesc_algtype) {
24943055Sdanmcd 			case SADB_X_ALGTYPE_CRYPT:
24954867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
24964064Smarkfen 				    "Encryption = "));
24973055Sdanmcd 				(void) dump_ealg(algdesc->sadb_x_algdesc_alg,
24984867Spwernau 				    file);
24993055Sdanmcd 				break;
25003055Sdanmcd 			case SADB_X_ALGTYPE_AUTH:
25014867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
25024064Smarkfen 				    "Authentication = "));
25033055Sdanmcd 				(void) dump_aalg(algdesc->sadb_x_algdesc_alg,
25044867Spwernau 				    file);
25053055Sdanmcd 				break;
25063055Sdanmcd 			default:
25074867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
25084064Smarkfen 				    "algtype(%d) = alg(%d)"),
25093055Sdanmcd 				    algdesc->sadb_x_algdesc_algtype,
25103055Sdanmcd 				    algdesc->sadb_x_algdesc_alg);
25113055Sdanmcd 				break;
25123055Sdanmcd 			}
25133055Sdanmcd 
25144867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
251510824SMark.Fenwick@Sun.COM 			    "  minbits=%u, maxbits=%u, saltbits=%u\n"),
25163055Sdanmcd 			    algdesc->sadb_x_algdesc_minbits,
251710824SMark.Fenwick@Sun.COM 			    algdesc->sadb_x_algdesc_maxbits,
251810824SMark.Fenwick@Sun.COM 			    algdesc->sadb_x_algdesc_reserved);
25193055Sdanmcd 
25203055Sdanmcd 			sofar = (uint64_t *)(++algdesc);
25213055Sdanmcd 		}
25223055Sdanmcd 		ecomb = (struct sadb_x_ecomb *)sofar;
25233055Sdanmcd 	}
25243055Sdanmcd }
25253055Sdanmcd 
25263055Sdanmcd /*
25273055Sdanmcd  * Print an SADB_EXT_SUPPORTED extension.
25283055Sdanmcd  */
25293055Sdanmcd void
print_supp(FILE * file,char * prefix,struct sadb_supported * supp)25304867Spwernau print_supp(FILE *file, char *prefix, struct sadb_supported *supp)
25313055Sdanmcd {
25323055Sdanmcd 	struct sadb_alg *algs;
25333055Sdanmcd 	int i, numalgs;
25343055Sdanmcd 
25354867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "%sSupported "), prefix);
25363055Sdanmcd 	switch (supp->sadb_supported_exttype) {
25373055Sdanmcd 	case SADB_EXT_SUPPORTED_AUTH:
25384867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "authentication"));
25393055Sdanmcd 		break;
25403055Sdanmcd 	case SADB_EXT_SUPPORTED_ENCRYPT:
25414867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "encryption"));
25423055Sdanmcd 		break;
25433055Sdanmcd 	}
25444867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, " algorithms.\n"));
25453055Sdanmcd 
25463055Sdanmcd 	algs = (struct sadb_alg *)(supp + 1);
25473055Sdanmcd 	numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp));
25483055Sdanmcd 	numalgs /= SADB_8TO64(sizeof (*algs));
25493055Sdanmcd 	for (i = 0; i < numalgs; i++) {
25505906Svk199839 		uint16_t exttype = supp->sadb_supported_exttype;
25515906Svk199839 
25524867Spwernau 		(void) fprintf(file, "%s", prefix);
25535906Svk199839 		switch (exttype) {
25543055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
25554867Spwernau 			(void) dump_aalg(algs[i].sadb_alg_id, file);
25563055Sdanmcd 			break;
25573055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
25584867Spwernau 			(void) dump_ealg(algs[i].sadb_alg_id, file);
25593055Sdanmcd 			break;
25603055Sdanmcd 		}
25614867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
256210824SMark.Fenwick@Sun.COM 		    " minbits=%u, maxbits=%u, ivlen=%u, saltbits=%u"),
25633055Sdanmcd 		    algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits,
256410824SMark.Fenwick@Sun.COM 		    algs[i].sadb_alg_ivlen, algs[i].sadb_x_alg_saltbits);
25655906Svk199839 		if (exttype == SADB_EXT_SUPPORTED_ENCRYPT)
25665906Svk199839 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
25675906Svk199839 			    ", increment=%u"), algs[i].sadb_x_alg_increment);
25685906Svk199839 		(void) fprintf(file, dgettext(TEXT_DOMAIN, ".\n"));
25693055Sdanmcd 	}
25703055Sdanmcd }
25713055Sdanmcd 
25723055Sdanmcd /*
25733055Sdanmcd  * Print an SADB_EXT_SPIRANGE extension.
25743055Sdanmcd  */
25753055Sdanmcd void
print_spirange(FILE * file,char * prefix,struct sadb_spirange * range)25764867Spwernau print_spirange(FILE *file, char *prefix, struct sadb_spirange *range)
25773055Sdanmcd {
25784867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
25794064Smarkfen 	    "%sSPI Range, min=0x%x, max=0x%x\n"), prefix,
25803055Sdanmcd 	    htonl(range->sadb_spirange_min),
25813055Sdanmcd 	    htonl(range->sadb_spirange_max));
25823055Sdanmcd }
25833055Sdanmcd 
25843055Sdanmcd /*
25853055Sdanmcd  * Print an SADB_X_EXT_KM_COOKIE extension.
25863055Sdanmcd  */
25873055Sdanmcd 
25883055Sdanmcd void
print_kmc(FILE * file,char * prefix,struct sadb_x_kmc * kmc)25894867Spwernau print_kmc(FILE *file, char *prefix, struct sadb_x_kmc *kmc)
25903055Sdanmcd {
25913055Sdanmcd 	char *cookie_label;
25923055Sdanmcd 
25933055Sdanmcd 	if ((cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie)) ==
25943055Sdanmcd 	    NULL)
25954064Smarkfen 		cookie_label = dgettext(TEXT_DOMAIN, "<Label not found.>");
25963055Sdanmcd 
25974867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
25984064Smarkfen 	    "%sProtocol %u, cookie=\"%s\" (%u)\n"), prefix,
25993055Sdanmcd 	    kmc->sadb_x_kmc_proto, cookie_label, kmc->sadb_x_kmc_cookie);
26003055Sdanmcd }
26017749SThejaswini.Singarajipura@Sun.COM 
26027749SThejaswini.Singarajipura@Sun.COM /*
26037749SThejaswini.Singarajipura@Sun.COM  * Print an SADB_X_EXT_REPLAY_CTR extension.
26047749SThejaswini.Singarajipura@Sun.COM  */
26057749SThejaswini.Singarajipura@Sun.COM 
26067749SThejaswini.Singarajipura@Sun.COM void
print_replay(FILE * file,char * prefix,sadb_x_replay_ctr_t * repl)26077749SThejaswini.Singarajipura@Sun.COM print_replay(FILE *file, char *prefix, sadb_x_replay_ctr_t *repl)
26087749SThejaswini.Singarajipura@Sun.COM {
26097749SThejaswini.Singarajipura@Sun.COM 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
26107749SThejaswini.Singarajipura@Sun.COM 	    "%sReplay Value "), prefix);
26117749SThejaswini.Singarajipura@Sun.COM 	if ((repl->sadb_x_rc_replay32 == 0) &&
26127749SThejaswini.Singarajipura@Sun.COM 	    (repl->sadb_x_rc_replay64 == 0)) {
26137749SThejaswini.Singarajipura@Sun.COM 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
26147749SThejaswini.Singarajipura@Sun.COM 		    "<Value not found.>"));
26157749SThejaswini.Singarajipura@Sun.COM 	}
26167749SThejaswini.Singarajipura@Sun.COM 	/*
26177749SThejaswini.Singarajipura@Sun.COM 	 * We currently do not support a 64-bit replay value.
26187749SThejaswini.Singarajipura@Sun.COM 	 * RFC 4301 will require one, however, and we have a field
26197749SThejaswini.Singarajipura@Sun.COM 	 * in place when 4301 is built.
26207749SThejaswini.Singarajipura@Sun.COM 	 */
26217749SThejaswini.Singarajipura@Sun.COM 	(void) fprintf(file, "% " PRIu64 "\n",
26227749SThejaswini.Singarajipura@Sun.COM 	    ((repl->sadb_x_rc_replay32 == 0) ?
26237749SThejaswini.Singarajipura@Sun.COM 	    repl->sadb_x_rc_replay64 : repl->sadb_x_rc_replay32));
26247749SThejaswini.Singarajipura@Sun.COM }
26256668Smarkfen /*
26266668Smarkfen  * Print an SADB_X_EXT_PAIR extension.
26276668Smarkfen  */
26286668Smarkfen static void
print_pair(FILE * file,char * prefix,struct sadb_x_pair * pair)26296668Smarkfen print_pair(FILE *file, char *prefix, struct sadb_x_pair *pair)
26306668Smarkfen {
26316668Smarkfen 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "%sPaired with spi=0x%x\n"),
26326668Smarkfen 	    prefix, ntohl(pair->sadb_x_pair_spi));
26336668Smarkfen }
26343055Sdanmcd 
26353055Sdanmcd /*
26363055Sdanmcd  * Take a PF_KEY message pointed to buffer and print it.  Useful for DUMP
26373055Sdanmcd  * and GET.
26383055Sdanmcd  */
26393055Sdanmcd void
print_samsg(FILE * file,uint64_t * buffer,boolean_t want_timestamp,boolean_t vflag,boolean_t ignore_nss)26404867Spwernau print_samsg(FILE *file, uint64_t *buffer, boolean_t want_timestamp,
26414867Spwernau     boolean_t vflag, boolean_t ignore_nss)
26423055Sdanmcd {
26433055Sdanmcd 	uint64_t *current;
26443055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
26453055Sdanmcd 	struct sadb_ext *ext;
26463055Sdanmcd 	struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL;
26477749SThejaswini.Singarajipura@Sun.COM 	struct sadb_lifetime *idlelt = NULL;
26483055Sdanmcd 	int i;
26493055Sdanmcd 	time_t wallclock;
26503055Sdanmcd 
26513055Sdanmcd 	(void) time(&wallclock);
26523055Sdanmcd 
26534867Spwernau 	print_sadb_msg(file, samsg, want_timestamp ? wallclock : 0, vflag);
26543055Sdanmcd 	current = (uint64_t *)(samsg + 1);
26553055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
26563055Sdanmcd 		int lenbytes;
26573055Sdanmcd 
26583055Sdanmcd 		ext = (struct sadb_ext *)current;
26593055Sdanmcd 		lenbytes = SADB_64TO8(ext->sadb_ext_len);
26603055Sdanmcd 		switch (ext->sadb_ext_type) {
26613055Sdanmcd 		case SADB_EXT_SA:
26624867Spwernau 			print_sa(file, dgettext(TEXT_DOMAIN,
26634064Smarkfen 			    "SA: "), (struct sadb_sa *)current);
26643055Sdanmcd 			break;
26653055Sdanmcd 		/*
26663055Sdanmcd 		 * Pluck out lifetimes and print them at the end.  This is
26673055Sdanmcd 		 * to show relative lifetimes.
26683055Sdanmcd 		 */
26693055Sdanmcd 		case SADB_EXT_LIFETIME_CURRENT:
26703055Sdanmcd 			currentlt = (struct sadb_lifetime *)current;
26713055Sdanmcd 			break;
26723055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
26733055Sdanmcd 			hardlt = (struct sadb_lifetime *)current;
26743055Sdanmcd 			break;
26753055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
26763055Sdanmcd 			softlt = (struct sadb_lifetime *)current;
26773055Sdanmcd 			break;
26787749SThejaswini.Singarajipura@Sun.COM 		case SADB_X_EXT_LIFETIME_IDLE:
26797749SThejaswini.Singarajipura@Sun.COM 			idlelt = (struct sadb_lifetime *)current;
26807749SThejaswini.Singarajipura@Sun.COM 			break;
26813055Sdanmcd 
26823055Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
26834867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "SRC: "),
26844867Spwernau 			    (struct sadb_address *)current, ignore_nss);
26853055Sdanmcd 			break;
26863055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
26874867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "INS: "),
26884867Spwernau 			    (struct sadb_address *)current, ignore_nss);
26893055Sdanmcd 			break;
26903055Sdanmcd 		case SADB_EXT_ADDRESS_DST:
26914867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "DST: "),
26924867Spwernau 			    (struct sadb_address *)current, ignore_nss);
26933055Sdanmcd 			break;
26943055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
26954867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "IND: "),
26964867Spwernau 			    (struct sadb_address *)current, ignore_nss);
26973055Sdanmcd 			break;
26983055Sdanmcd 		case SADB_EXT_KEY_AUTH:
26994867Spwernau 			print_key(file, dgettext(TEXT_DOMAIN,
27004064Smarkfen 			    "AKY: "), (struct sadb_key *)current);
27013055Sdanmcd 			break;
27023055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
27034867Spwernau 			print_key(file, dgettext(TEXT_DOMAIN,
27044064Smarkfen 			    "EKY: "), (struct sadb_key *)current);
27053055Sdanmcd 			break;
27063055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
27074867Spwernau 			print_ident(file, dgettext(TEXT_DOMAIN, "SID: "),
27083055Sdanmcd 			    (struct sadb_ident *)current);
27093055Sdanmcd 			break;
27103055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
27114867Spwernau 			print_ident(file, dgettext(TEXT_DOMAIN, "DID: "),
27123055Sdanmcd 			    (struct sadb_ident *)current);
27133055Sdanmcd 			break;
27143055Sdanmcd 		case SADB_EXT_SENSITIVITY:
27154867Spwernau 			print_sens(file, dgettext(TEXT_DOMAIN, "SNS: "),
271610934Ssommerfeld@sun.com 			    (struct sadb_sens *)current, ignore_nss);
27173055Sdanmcd 			break;
27183055Sdanmcd 		case SADB_EXT_PROPOSAL:
27194867Spwernau 			print_prop(file, dgettext(TEXT_DOMAIN, "PRP: "),
27203055Sdanmcd 			    (struct sadb_prop *)current);
27213055Sdanmcd 			break;
27223055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
27234867Spwernau 			print_supp(file, dgettext(TEXT_DOMAIN, "SUA: "),
27243055Sdanmcd 			    (struct sadb_supported *)current);
27253055Sdanmcd 			break;
27263055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
27274867Spwernau 			print_supp(file, dgettext(TEXT_DOMAIN, "SUE: "),
27283055Sdanmcd 			    (struct sadb_supported *)current);
27293055Sdanmcd 			break;
27303055Sdanmcd 		case SADB_EXT_SPIRANGE:
27314867Spwernau 			print_spirange(file, dgettext(TEXT_DOMAIN, "SPR: "),
27323055Sdanmcd 			    (struct sadb_spirange *)current);
27333055Sdanmcd 			break;
27343055Sdanmcd 		case SADB_X_EXT_EPROP:
27354867Spwernau 			print_eprop(file, dgettext(TEXT_DOMAIN, "EPR: "),
27363055Sdanmcd 			    (struct sadb_prop *)current);
27373055Sdanmcd 			break;
27383055Sdanmcd 		case SADB_X_EXT_KM_COOKIE:
27394867Spwernau 			print_kmc(file, dgettext(TEXT_DOMAIN, "KMC: "),
27403055Sdanmcd 			    (struct sadb_x_kmc *)current);
27413055Sdanmcd 			break;
27423055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
27434867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "NRM: "),
27444867Spwernau 			    (struct sadb_address *)current, ignore_nss);
27453055Sdanmcd 			break;
27463055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
27474867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "NLC: "),
27484867Spwernau 			    (struct sadb_address *)current, ignore_nss);
27493055Sdanmcd 			break;
27506668Smarkfen 		case SADB_X_EXT_PAIR:
27516668Smarkfen 			print_pair(file, dgettext(TEXT_DOMAIN, "OTH: "),
27526668Smarkfen 			    (struct sadb_x_pair *)current);
27536668Smarkfen 			break;
275410934Ssommerfeld@sun.com 		case SADB_X_EXT_OUTER_SENS:
275510934Ssommerfeld@sun.com 			print_sens(file, dgettext(TEXT_DOMAIN, "OSN: "),
275610934Ssommerfeld@sun.com 			    (struct sadb_sens *)current, ignore_nss);
275710934Ssommerfeld@sun.com 			break;
27587749SThejaswini.Singarajipura@Sun.COM 		case SADB_X_EXT_REPLAY_VALUE:
27597749SThejaswini.Singarajipura@Sun.COM 			(void) print_replay(file, dgettext(TEXT_DOMAIN,
27607749SThejaswini.Singarajipura@Sun.COM 			    "RPL: "), (sadb_x_replay_ctr_t *)current);
27617749SThejaswini.Singarajipura@Sun.COM 			break;
27623055Sdanmcd 		default:
27634867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
27643055Sdanmcd 			    "UNK: Unknown ext. %d, len %d.\n"),
27653055Sdanmcd 			    ext->sadb_ext_type, lenbytes);
27663055Sdanmcd 			for (i = 0; i < ext->sadb_ext_len; i++)
27674867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
27687320Sdanmcd@sun.com 				    "UNK: 0x%" PRIx64 "\n"),
27697320Sdanmcd@sun.com 				    ((uint64_t *)ext)[i]);
27703055Sdanmcd 			break;
27713055Sdanmcd 		}
27723055Sdanmcd 		current += (lenbytes == 0) ?
27733055Sdanmcd 		    SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len;
27743055Sdanmcd 	}
27753055Sdanmcd 	/*
27763055Sdanmcd 	 * Print lifetimes NOW.
27773055Sdanmcd 	 */
27787749SThejaswini.Singarajipura@Sun.COM 	if (currentlt != NULL || hardlt != NULL || softlt != NULL ||
27797749SThejaswini.Singarajipura@Sun.COM 	    idlelt != NULL)
27807749SThejaswini.Singarajipura@Sun.COM 		print_lifetimes(file, wallclock, currentlt, hardlt,
27817749SThejaswini.Singarajipura@Sun.COM 		    softlt, idlelt, vflag);
27823055Sdanmcd 
27833055Sdanmcd 	if (current - buffer != samsg->sadb_msg_len) {
27844867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
27854867Spwernau 		    "WARNING: insufficient buffer space or corrupt message."));
27863055Sdanmcd 	}
27873055Sdanmcd 
27884867Spwernau 	(void) fflush(file);	/* Make sure our message is out there. */
27893055Sdanmcd }
27903055Sdanmcd 
27913055Sdanmcd /*
27923055Sdanmcd  * save_XXX functions are used when "saving" the SA tables to either a
27933055Sdanmcd  * file or standard output.  They use the dump_XXX functions where needed,
27943055Sdanmcd  * but mostly they use the rparseXXX functions.
27953055Sdanmcd  */
27963055Sdanmcd 
27973055Sdanmcd /*
27983055Sdanmcd  * Print save information for a lifetime extension.
27993055Sdanmcd  *
28003055Sdanmcd  * NOTE : It saves the lifetime in absolute terms.  For example, if you
28013055Sdanmcd  * had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though
28023055Sdanmcd  * there may have been 59 seconds burned off the clock.
28033055Sdanmcd  */
28043055Sdanmcd boolean_t
save_lifetime(struct sadb_lifetime * lifetime,FILE * ofile)28053055Sdanmcd save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile)
28063055Sdanmcd {
28073055Sdanmcd 	char *prefix;
28083055Sdanmcd 
28097749SThejaswini.Singarajipura@Sun.COM 	switch (lifetime->sadb_lifetime_exttype) {
28107749SThejaswini.Singarajipura@Sun.COM 	case SADB_EXT_LIFETIME_HARD:
28117749SThejaswini.Singarajipura@Sun.COM 		prefix = "hard";
28127749SThejaswini.Singarajipura@Sun.COM 		break;
28137749SThejaswini.Singarajipura@Sun.COM 	case SADB_EXT_LIFETIME_SOFT:
28147749SThejaswini.Singarajipura@Sun.COM 		prefix = "soft";
28157749SThejaswini.Singarajipura@Sun.COM 		break;
28167749SThejaswini.Singarajipura@Sun.COM 	case SADB_X_EXT_LIFETIME_IDLE:
28177749SThejaswini.Singarajipura@Sun.COM 		prefix = "idle";
28187749SThejaswini.Singarajipura@Sun.COM 		break;
28197749SThejaswini.Singarajipura@Sun.COM 	}
28203055Sdanmcd 
28213055Sdanmcd 	if (putc('\t', ofile) == EOF)
28223055Sdanmcd 		return (B_FALSE);
28233055Sdanmcd 
28243055Sdanmcd 	if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile,
28253055Sdanmcd 	    "%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0)
28263055Sdanmcd 		return (B_FALSE);
28273055Sdanmcd 
28283055Sdanmcd 	if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile,
28297320Sdanmcd@sun.com 	    "%s_bytes %" PRIu64 " ", prefix, lifetime->sadb_lifetime_bytes) < 0)
28303055Sdanmcd 		return (B_FALSE);
28313055Sdanmcd 
28323055Sdanmcd 	if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile,
28337320Sdanmcd@sun.com 	    "%s_addtime %" PRIu64 " ", prefix,
28347320Sdanmcd@sun.com 	    lifetime->sadb_lifetime_addtime) < 0)
28353055Sdanmcd 		return (B_FALSE);
28363055Sdanmcd 
28373055Sdanmcd 	if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile,
28387320Sdanmcd@sun.com 	    "%s_usetime %" PRIu64 " ", prefix,
28397320Sdanmcd@sun.com 	    lifetime->sadb_lifetime_usetime) < 0)
28403055Sdanmcd 		return (B_FALSE);
28413055Sdanmcd 
28423055Sdanmcd 	return (B_TRUE);
28433055Sdanmcd }
28443055Sdanmcd 
28453055Sdanmcd /*
28463055Sdanmcd  * Print save information for an address extension.
28473055Sdanmcd  */
28483055Sdanmcd boolean_t
save_address(struct sadb_address * addr,FILE * ofile)28493055Sdanmcd save_address(struct sadb_address *addr, FILE *ofile)
28503055Sdanmcd {
28513055Sdanmcd 	char *printable_addr, buf[INET6_ADDRSTRLEN];
28523055Sdanmcd 	const char *prefix, *pprefix;
28533055Sdanmcd 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1);
28543055Sdanmcd 	struct sockaddr_in *sin = (struct sockaddr_in *)sin6;
28553055Sdanmcd 	int af = sin->sin_family;
28563055Sdanmcd 
28573055Sdanmcd 	/*
28583055Sdanmcd 	 * Address-family reality check.
28593055Sdanmcd 	 */
28603055Sdanmcd 	if (af != AF_INET6 && af != AF_INET)
28613055Sdanmcd 		return (B_FALSE);
28623055Sdanmcd 
28633055Sdanmcd 	switch (addr->sadb_address_exttype) {
28643055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
28653055Sdanmcd 		prefix = "src";
28663055Sdanmcd 		pprefix = "sport";
28673055Sdanmcd 		break;
28683055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
28693055Sdanmcd 		prefix = "isrc";
28703055Sdanmcd 		pprefix = "isport";
28713055Sdanmcd 		break;
28723055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
28733055Sdanmcd 		prefix = "dst";
28743055Sdanmcd 		pprefix = "dport";
28753055Sdanmcd 		break;
28763055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
28773055Sdanmcd 		prefix = "idst";
28783055Sdanmcd 		pprefix = "idport";
28793055Sdanmcd 		break;
28803055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
28813055Sdanmcd 		prefix = "nat_loc ";
28823055Sdanmcd 		pprefix = "nat_lport";
28833055Sdanmcd 		break;
28843055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
28853055Sdanmcd 		prefix = "nat_rem ";
28863055Sdanmcd 		pprefix = "nat_rport";
28873055Sdanmcd 		break;
28883055Sdanmcd 	}
28893055Sdanmcd 
28903055Sdanmcd 	if (fprintf(ofile, "    %s ", prefix) < 0)
28913055Sdanmcd 		return (B_FALSE);
28923055Sdanmcd 
28933055Sdanmcd 	/*
28943055Sdanmcd 	 * Do not do address-to-name translation, given that we live in
28953055Sdanmcd 	 * an age of names that explode into many addresses.
28963055Sdanmcd 	 */
28973055Sdanmcd 	printable_addr = (char *)inet_ntop(af,
28983055Sdanmcd 	    (af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr,
28993055Sdanmcd 	    buf, sizeof (buf));
29003055Sdanmcd 	if (printable_addr == NULL)
29014064Smarkfen 		printable_addr = "Invalid IP address.";
29023055Sdanmcd 	if (fprintf(ofile, "%s", printable_addr) < 0)
29033055Sdanmcd 		return (B_FALSE);
29043055Sdanmcd 	if (addr->sadb_address_prefixlen != 0 &&
29053055Sdanmcd 	    !((addr->sadb_address_prefixlen == 32 && af == AF_INET) ||
29064342Spwernau 	    (addr->sadb_address_prefixlen == 128 && af == AF_INET6))) {
29073055Sdanmcd 		if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0)
29083055Sdanmcd 			return (B_FALSE);
29093055Sdanmcd 	}
29103055Sdanmcd 
29113055Sdanmcd 	/*
29123055Sdanmcd 	 * The port is in the same position for struct sockaddr_in and
29133055Sdanmcd 	 * struct sockaddr_in6.  We exploit that property here.
29143055Sdanmcd 	 */
29153055Sdanmcd 	if ((pprefix != NULL) && (sin->sin_port != 0))
29163055Sdanmcd 		(void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port));
29173055Sdanmcd 
29183055Sdanmcd 	return (B_TRUE);
29193055Sdanmcd }
29203055Sdanmcd 
29213055Sdanmcd /*
29223055Sdanmcd  * Print save information for a key extension. Returns whether writing
29233055Sdanmcd  * to the specified output file was successful or not.
29243055Sdanmcd  */
29253055Sdanmcd boolean_t
save_key(struct sadb_key * key,FILE * ofile)29263055Sdanmcd save_key(struct sadb_key *key, FILE *ofile)
29273055Sdanmcd {
29283055Sdanmcd 	char *prefix;
29293055Sdanmcd 
29303055Sdanmcd 	if (putc('\t', ofile) == EOF)
29313055Sdanmcd 		return (B_FALSE);
29323055Sdanmcd 
29333055Sdanmcd 	prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr";
29343055Sdanmcd 
29353055Sdanmcd 	if (fprintf(ofile, "%skey ", prefix) < 0)
29363055Sdanmcd 		return (B_FALSE);
29373055Sdanmcd 
293810824SMark.Fenwick@Sun.COM 	if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits,
293910824SMark.Fenwick@Sun.COM 	    key->sadb_key_reserved, ofile, B_FALSE) == -1)
29403055Sdanmcd 		return (B_FALSE);
29413055Sdanmcd 
29423055Sdanmcd 	return (B_TRUE);
29433055Sdanmcd }
29443055Sdanmcd 
29453055Sdanmcd /*
29463055Sdanmcd  * Print save information for an identity extension.
29473055Sdanmcd  */
29483055Sdanmcd boolean_t
save_ident(struct sadb_ident * ident,FILE * ofile)29493055Sdanmcd save_ident(struct sadb_ident *ident, FILE *ofile)
29503055Sdanmcd {
29513055Sdanmcd 	char *prefix;
29523055Sdanmcd 
29533055Sdanmcd 	if (putc('\t', ofile) == EOF)
29543055Sdanmcd 		return (B_FALSE);
29553055Sdanmcd 
29563055Sdanmcd 	prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" :
29573055Sdanmcd 	    "dst";
29583055Sdanmcd 
29593055Sdanmcd 	if (fprintf(ofile, "%sidtype %s ", prefix,
29603055Sdanmcd 	    rparseidtype(ident->sadb_ident_type)) < 0)
29613055Sdanmcd 		return (B_FALSE);
29623055Sdanmcd 
29633055Sdanmcd 	if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN ||
29643055Sdanmcd 	    ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) {
29654064Smarkfen 		if (fprintf(ofile, dgettext(TEXT_DOMAIN,
29664064Smarkfen 		    "<can-not-print>")) < 0)
29673055Sdanmcd 			return (B_FALSE);
29683055Sdanmcd 	} else {
29693055Sdanmcd 		if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0)
29703055Sdanmcd 			return (B_FALSE);
29713055Sdanmcd 	}
29723055Sdanmcd 
29733055Sdanmcd 	return (B_TRUE);
29743055Sdanmcd }
29753055Sdanmcd 
297610934Ssommerfeld@sun.com boolean_t
save_sens(struct sadb_sens * sens,FILE * ofile)297710934Ssommerfeld@sun.com save_sens(struct sadb_sens *sens, FILE *ofile)
297810934Ssommerfeld@sun.com {
297910934Ssommerfeld@sun.com 	char *prefix;
298010934Ssommerfeld@sun.com 	char *hlabel;
298110934Ssommerfeld@sun.com 	bslabel_t sl;
298210934Ssommerfeld@sun.com 
298310934Ssommerfeld@sun.com 	if (putc('\t', ofile) == EOF)
298410934Ssommerfeld@sun.com 		return (B_FALSE);
298510934Ssommerfeld@sun.com 
298610934Ssommerfeld@sun.com 	if (sens->sadb_sens_exttype == SADB_EXT_SENSITIVITY)
298710934Ssommerfeld@sun.com 		prefix = "label";
298810934Ssommerfeld@sun.com 	else if ((sens->sadb_x_sens_flags & SADB_X_SENS_IMPLICIT) == 0)
298910934Ssommerfeld@sun.com 		prefix = "outer-label";
299010934Ssommerfeld@sun.com 	else
299110934Ssommerfeld@sun.com 		prefix = "implicit-label";
299210934Ssommerfeld@sun.com 
299310934Ssommerfeld@sun.com 	ipsec_convert_sens_to_bslabel(sens, &sl);
299410934Ssommerfeld@sun.com 	ipsec_convert_bslabel_to_hex(&sl, &hlabel);
299510934Ssommerfeld@sun.com 
299610934Ssommerfeld@sun.com 	if (fprintf(ofile, "%s %s ", prefix, hlabel) < 0) {
299710934Ssommerfeld@sun.com 		free(hlabel);
299810934Ssommerfeld@sun.com 		return (B_FALSE);
299910934Ssommerfeld@sun.com 	}
300010934Ssommerfeld@sun.com 	free(hlabel);
300110934Ssommerfeld@sun.com 
300210934Ssommerfeld@sun.com 	return (B_TRUE);
300310934Ssommerfeld@sun.com }
300410934Ssommerfeld@sun.com 
30053055Sdanmcd /*
30063055Sdanmcd  * "Save" a security association to an output file.
30073055Sdanmcd  *
30084064Smarkfen  * NOTE the lack of calls to dgettext() because I'm outputting parseable stuff.
30093055Sdanmcd  * ALSO NOTE that if you change keywords (see parsecmd()), you'll have to
30103055Sdanmcd  * change them here as well.
30113055Sdanmcd  */
30123055Sdanmcd void
save_assoc(uint64_t * buffer,FILE * ofile)30133055Sdanmcd save_assoc(uint64_t *buffer, FILE *ofile)
30143055Sdanmcd {
30154064Smarkfen 	int terrno;
30164987Sdanmcd 	boolean_t seen_proto = B_FALSE, seen_iproto = B_FALSE;
30173055Sdanmcd 	uint64_t *current;
30183055Sdanmcd 	struct sadb_address *addr;
30197749SThejaswini.Singarajipura@Sun.COM 	struct sadb_x_replay_ctr *repl;
30203055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
30213055Sdanmcd 	struct sadb_ext *ext;
30223055Sdanmcd 
30234064Smarkfen #define	tidyup() \
30244064Smarkfen 	terrno = errno; (void) fclose(ofile); errno = terrno; \
30254064Smarkfen 	interactive = B_FALSE
30264064Smarkfen 
30274064Smarkfen #define	savenl() if (fputs(" \\\n", ofile) == EOF) \
30284064Smarkfen 	{ bail(dgettext(TEXT_DOMAIN, "savenl")); }
30293055Sdanmcd 
30303055Sdanmcd 	if (fputs("# begin assoc\n", ofile) == EOF)
30314064Smarkfen 		bail(dgettext(TEXT_DOMAIN,
30324064Smarkfen 		    "save_assoc: Opening comment of SA"));
30333055Sdanmcd 	if (fprintf(ofile, "add %s ", rparsesatype(samsg->sadb_msg_satype)) < 0)
30344064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: First line of SA"));
30353055Sdanmcd 	savenl();
30363055Sdanmcd 
30373055Sdanmcd 	current = (uint64_t *)(samsg + 1);
30383055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
30393055Sdanmcd 		struct sadb_sa *assoc;
30403055Sdanmcd 
30413055Sdanmcd 		ext = (struct sadb_ext *)current;
30424987Sdanmcd 		addr = (struct sadb_address *)ext;  /* Just in case... */
30433055Sdanmcd 		switch (ext->sadb_ext_type) {
30443055Sdanmcd 		case SADB_EXT_SA:
30453055Sdanmcd 			assoc = (struct sadb_sa *)ext;
30463055Sdanmcd 			if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
30473055Sdanmcd 				if (fprintf(ofile, "# WARNING: SA was dying "
30483055Sdanmcd 				    "or dead.\n") < 0) {
30494064Smarkfen 					tidyup();
30504064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
30514064Smarkfen 					    "save_assoc: fprintf not mature"));
30523055Sdanmcd 				}
30533055Sdanmcd 			}
30543055Sdanmcd 			if (fprintf(ofile, "    spi 0x%x ",
30554064Smarkfen 			    ntohl(assoc->sadb_sa_spi)) < 0) {
30564064Smarkfen 				tidyup();
30574064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
30584064Smarkfen 				    "save_assoc: fprintf spi"));
30594064Smarkfen 			}
30603055Sdanmcd 			if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
30613055Sdanmcd 				if (fprintf(ofile, "encr_alg %s ",
30623055Sdanmcd 				    rparsealg(assoc->sadb_sa_encrypt,
30634342Spwernau 				    IPSEC_PROTO_ESP)) < 0) {
30644064Smarkfen 					tidyup();
30654064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
30664064Smarkfen 					    "save_assoc: fprintf encrypt"));
30674064Smarkfen 				}
30683055Sdanmcd 			}
30693055Sdanmcd 			if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
30703055Sdanmcd 				if (fprintf(ofile, "auth_alg %s ",
30713055Sdanmcd 				    rparsealg(assoc->sadb_sa_auth,
30724342Spwernau 				    IPSEC_PROTO_AH)) < 0) {
30734064Smarkfen 					tidyup();
30744064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
30754064Smarkfen 					    "save_assoc: fprintf auth"));
30764064Smarkfen 				}
30773055Sdanmcd 			}
30783055Sdanmcd 			if (fprintf(ofile, "replay %d ",
30794064Smarkfen 			    assoc->sadb_sa_replay) < 0) {
30804064Smarkfen 				tidyup();
30814064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
30824064Smarkfen 				    "save_assoc: fprintf replay"));
30834064Smarkfen 			}
30843055Sdanmcd 			if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC |
30853055Sdanmcd 			    SADB_X_SAFLAGS_NATT_REM)) {
30864064Smarkfen 				if (fprintf(ofile, "encap udp") < 0) {
30874064Smarkfen 					tidyup();
30884064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
30894064Smarkfen 					    "save_assoc: fprintf encap"));
30904064Smarkfen 				}
30913055Sdanmcd 			}
30923055Sdanmcd 			savenl();
30933055Sdanmcd 			break;
30943055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
30953055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
30967749SThejaswini.Singarajipura@Sun.COM 		case SADB_X_EXT_LIFETIME_IDLE:
30974064Smarkfen 			if (!save_lifetime((struct sadb_lifetime *)ext,
30984064Smarkfen 			    ofile)) {
30994064Smarkfen 				tidyup();
31004064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_lifetime"));
31014064Smarkfen 			}
31023055Sdanmcd 			savenl();
31033055Sdanmcd 			break;
31043055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
31053055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
31064987Sdanmcd 			if (!seen_iproto && addr->sadb_address_proto) {
31074987Sdanmcd 				(void) fprintf(ofile, "    iproto %d",
31084987Sdanmcd 				    addr->sadb_address_proto);
31094987Sdanmcd 				savenl();
31104987Sdanmcd 				seen_iproto = B_TRUE;
31114987Sdanmcd 			}
31124987Sdanmcd 			goto skip_srcdst;  /* Hack to avoid cases below... */
31134987Sdanmcd 			/* FALLTHRU */
31144987Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
31154987Sdanmcd 		case SADB_EXT_ADDRESS_DST:
31163055Sdanmcd 			if (!seen_proto && addr->sadb_address_proto) {
31173055Sdanmcd 				(void) fprintf(ofile, "    proto %d",
31183055Sdanmcd 				    addr->sadb_address_proto);
31193055Sdanmcd 				savenl();
31204987Sdanmcd 				seen_proto = B_TRUE;
31213055Sdanmcd 			}
31224987Sdanmcd 			/* FALLTHRU */
31234987Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
31244987Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
31254987Sdanmcd skip_srcdst:
31264064Smarkfen 			if (!save_address(addr, ofile)) {
31274064Smarkfen 				tidyup();
31284064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
31294064Smarkfen 			}
31303055Sdanmcd 			savenl();
31313055Sdanmcd 			break;
31323055Sdanmcd 		case SADB_EXT_KEY_AUTH:
31333055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
31344064Smarkfen 			if (!save_key((struct sadb_key *)ext, ofile)) {
31354064Smarkfen 				tidyup();
31364064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
31374064Smarkfen 			}
31383055Sdanmcd 			savenl();
31393055Sdanmcd 			break;
31403055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
31413055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
31424064Smarkfen 			if (!save_ident((struct sadb_ident *)ext, ofile)) {
31434064Smarkfen 				tidyup();
31444064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
31454064Smarkfen 			}
31463055Sdanmcd 			savenl();
31473055Sdanmcd 			break;
31487749SThejaswini.Singarajipura@Sun.COM 		case SADB_X_EXT_REPLAY_VALUE:
31497749SThejaswini.Singarajipura@Sun.COM 			repl = (sadb_x_replay_ctr_t *)ext;
31507749SThejaswini.Singarajipura@Sun.COM 			if ((repl->sadb_x_rc_replay32 == 0) &&
31517749SThejaswini.Singarajipura@Sun.COM 			    (repl->sadb_x_rc_replay64 == 0)) {
31527749SThejaswini.Singarajipura@Sun.COM 				tidyup();
31537749SThejaswini.Singarajipura@Sun.COM 				bail(dgettext(TEXT_DOMAIN, "Replay Value"));
31547749SThejaswini.Singarajipura@Sun.COM 			}
31557749SThejaswini.Singarajipura@Sun.COM 			if (fprintf(ofile, "replay_value %" PRIu64 "",
31567749SThejaswini.Singarajipura@Sun.COM 			    (repl->sadb_x_rc_replay32 == 0 ?
31577749SThejaswini.Singarajipura@Sun.COM 			    repl->sadb_x_rc_replay64 :
31587749SThejaswini.Singarajipura@Sun.COM 			    repl->sadb_x_rc_replay32)) < 0) {
31597749SThejaswini.Singarajipura@Sun.COM 				tidyup();
31607749SThejaswini.Singarajipura@Sun.COM 				bail(dgettext(TEXT_DOMAIN,
31617749SThejaswini.Singarajipura@Sun.COM 				    "save_assoc: fprintf replay value"));
31627749SThejaswini.Singarajipura@Sun.COM 			}
31637749SThejaswini.Singarajipura@Sun.COM 			savenl();
31647749SThejaswini.Singarajipura@Sun.COM 			break;
31653055Sdanmcd 		case SADB_EXT_SENSITIVITY:
316610934Ssommerfeld@sun.com 		case SADB_X_EXT_OUTER_SENS:
316710934Ssommerfeld@sun.com 			if (!save_sens((struct sadb_sens *)ext, ofile)) {
316810934Ssommerfeld@sun.com 				tidyup();
316910934Ssommerfeld@sun.com 				bail(dgettext(TEXT_DOMAIN, "save_sens"));
317010934Ssommerfeld@sun.com 			}
317110934Ssommerfeld@sun.com 			savenl();
317210934Ssommerfeld@sun.com 			break;
31733055Sdanmcd 		default:
31743055Sdanmcd 			/* Skip over irrelevant extensions. */
31753055Sdanmcd 			break;
31763055Sdanmcd 		}
31773055Sdanmcd 		current += ext->sadb_ext_len;
31783055Sdanmcd 	}
31793055Sdanmcd 
31804064Smarkfen 	if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) {
31814064Smarkfen 		tidyup();
31824064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs"));
31834064Smarkfen 	}
31843055Sdanmcd }
31853055Sdanmcd 
31863055Sdanmcd /*
31873055Sdanmcd  * Open the output file for the "save" command.
31883055Sdanmcd  */
31893055Sdanmcd FILE *
opensavefile(char * filename)31903055Sdanmcd opensavefile(char *filename)
31913055Sdanmcd {
31923055Sdanmcd 	int fd;
31933055Sdanmcd 	FILE *retval;
31943055Sdanmcd 	struct stat buf;
31953055Sdanmcd 
31963055Sdanmcd 	/*
31973055Sdanmcd 	 * If the user specifies "-" or doesn't give a filename, then
31983055Sdanmcd 	 * dump to stdout.  Make sure to document the dangers of files
31993055Sdanmcd 	 * that are NFS, directing your output to strange places, etc.
32003055Sdanmcd 	 */
32013055Sdanmcd 	if (filename == NULL || strcmp("-", filename) == 0)
32023055Sdanmcd 		return (stdout);
32033055Sdanmcd 
32043055Sdanmcd 	/*
32053055Sdanmcd 	 * open the file with the create bits set.  Since I check for
32063055Sdanmcd 	 * real UID == root in main(), I won't worry about the ownership
32073055Sdanmcd 	 * problem.
32083055Sdanmcd 	 */
32093055Sdanmcd 	fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR);
32103055Sdanmcd 	if (fd == -1) {
32113055Sdanmcd 		if (errno != EEXIST)
32124064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
32134064Smarkfen 			    "open error"),
32143055Sdanmcd 			    strerror(errno));
32153055Sdanmcd 		fd = open(filename, O_WRONLY | O_TRUNC, 0);
32163055Sdanmcd 		if (fd == -1)
32174064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
32184064Smarkfen 			    "open error"), strerror(errno));
32193055Sdanmcd 		if (fstat(fd, &buf) == -1) {
32203055Sdanmcd 			(void) close(fd);
32213055Sdanmcd 			bail_msg("%s fstat: %s", filename, strerror(errno));
32223055Sdanmcd 		}
32233055Sdanmcd 		if (S_ISREG(buf.st_mode) &&
32243055Sdanmcd 		    ((buf.st_mode & S_IAMB) != S_IRUSR)) {
32254064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
32264064Smarkfen 			    "WARNING: Save file already exists with "
32274064Smarkfen 			    "permission %o."), buf.st_mode & S_IAMB);
32284064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
32294064Smarkfen 			    "Normal users may be able to read IPsec "
32304064Smarkfen 			    "keying material."));
32313055Sdanmcd 		}
32323055Sdanmcd 	}
32333055Sdanmcd 
32343055Sdanmcd 	/* Okay, we have an FD.  Assign it to a stdio FILE pointer. */
32353055Sdanmcd 	retval = fdopen(fd, "w");
32363055Sdanmcd 	if (retval == NULL) {
32373055Sdanmcd 		(void) close(fd);
32384064Smarkfen 		bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
32394064Smarkfen 		    "fdopen error"), strerror(errno));
32403055Sdanmcd 	}
32413055Sdanmcd 	return (retval);
32423055Sdanmcd }
32433055Sdanmcd 
32443055Sdanmcd const char *
do_inet_ntop(const void * addr,char * cp,size_t size)32453055Sdanmcd do_inet_ntop(const void *addr, char *cp, size_t size)
32463055Sdanmcd {
32473055Sdanmcd 	boolean_t isv4;
32483055Sdanmcd 	struct in6_addr *inaddr6 = (struct in6_addr *)addr;
32493055Sdanmcd 	struct in_addr inaddr;
32503055Sdanmcd 
32513055Sdanmcd 	if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) {
32523055Sdanmcd 		IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr);
32533055Sdanmcd 	}
32543055Sdanmcd 
32553055Sdanmcd 	return (inet_ntop(isv4 ? AF_INET : AF_INET6,
32563055Sdanmcd 	    isv4 ? (void *)&inaddr : inaddr6, cp, size));
32573055Sdanmcd }
32583055Sdanmcd 
32593055Sdanmcd char numprint[NBUF_SIZE];
32603055Sdanmcd 
32613055Sdanmcd /*
32623055Sdanmcd  * Parse and reverse parse a specific SA type (AH, ESP, etc.).
32633055Sdanmcd  */
32643055Sdanmcd static struct typetable {
32653055Sdanmcd 	char *type;
32663055Sdanmcd 	int token;
32673055Sdanmcd } type_table[] = {
32683055Sdanmcd 	{"all", SADB_SATYPE_UNSPEC},
32693055Sdanmcd 	{"ah",  SADB_SATYPE_AH},
32703055Sdanmcd 	{"esp", SADB_SATYPE_ESP},
32713055Sdanmcd 	/* PF_KEY NOTE:  More to come if net/pfkeyv2.h gets updated. */
32723055Sdanmcd 	{NULL, 0}	/* Token value is irrelevant for this entry. */
32733055Sdanmcd };
32743055Sdanmcd 
32753055Sdanmcd char *
rparsesatype(int type)32763055Sdanmcd rparsesatype(int type)
32773055Sdanmcd {
32783055Sdanmcd 	struct typetable *tt = type_table;
32793055Sdanmcd 
32803055Sdanmcd 	while (tt->type != NULL && type != tt->token)
32813055Sdanmcd 		tt++;
32823055Sdanmcd 
32833055Sdanmcd 	if (tt->type == NULL) {
32843055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", type);
32853055Sdanmcd 	} else {
32863055Sdanmcd 		return (tt->type);
32873055Sdanmcd 	}
32883055Sdanmcd 
32893055Sdanmcd 	return (numprint);
32903055Sdanmcd }
32913055Sdanmcd 
32923055Sdanmcd 
32933055Sdanmcd /*
32943055Sdanmcd  * Return a string containing the name of the specified numerical algorithm
32953055Sdanmcd  * identifier.
32963055Sdanmcd  */
32973055Sdanmcd char *
rparsealg(uint8_t alg,int proto_num)32983055Sdanmcd rparsealg(uint8_t alg, int proto_num)
32993055Sdanmcd {
33003055Sdanmcd 	static struct ipsecalgent *holder = NULL; /* we're single-threaded */
33013055Sdanmcd 
33023055Sdanmcd 	if (holder != NULL)
33033055Sdanmcd 		freeipsecalgent(holder);
33043055Sdanmcd 
33053055Sdanmcd 	holder = getipsecalgbynum(alg, proto_num, NULL);
33063055Sdanmcd 	if (holder == NULL) {
33073055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", alg);
33083055Sdanmcd 		return (numprint);
33093055Sdanmcd 	}
33103055Sdanmcd 
33113055Sdanmcd 	return (*(holder->a_names));
33123055Sdanmcd }
33133055Sdanmcd 
33143055Sdanmcd /*
33153055Sdanmcd  * Parse and reverse parse out a source/destination ID type.
33163055Sdanmcd  */
33173055Sdanmcd static struct idtypes {
33183055Sdanmcd 	char *idtype;
33193055Sdanmcd 	uint8_t retval;
33203055Sdanmcd } idtypes[] = {
33213055Sdanmcd 	{"prefix",	SADB_IDENTTYPE_PREFIX},
33223055Sdanmcd 	{"fqdn",	SADB_IDENTTYPE_FQDN},
33233055Sdanmcd 	{"domain",	SADB_IDENTTYPE_FQDN},
33243055Sdanmcd 	{"domainname",	SADB_IDENTTYPE_FQDN},
33253055Sdanmcd 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
33263055Sdanmcd 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
33273055Sdanmcd 	{"der_dn",	SADB_X_IDENTTYPE_DN},
33283055Sdanmcd 	{"der_gn",	SADB_X_IDENTTYPE_GN},
33293055Sdanmcd 	{NULL,		0}
33303055Sdanmcd };
33313055Sdanmcd 
33323055Sdanmcd char *
rparseidtype(uint16_t type)33333055Sdanmcd rparseidtype(uint16_t type)
33343055Sdanmcd {
33353055Sdanmcd 	struct idtypes *idp;
33363055Sdanmcd 
33373055Sdanmcd 	for (idp = idtypes; idp->idtype != NULL; idp++) {
33383055Sdanmcd 		if (type == idp->retval)
33393055Sdanmcd 			return (idp->idtype);
33403055Sdanmcd 	}
33413055Sdanmcd 
33423055Sdanmcd 	(void) snprintf(numprint, NBUF_SIZE, "%d", type);
33433055Sdanmcd 	return (numprint);
33443055Sdanmcd }
33454235Smarkfen 
33464235Smarkfen /*
33474235Smarkfen  * This is a general purpose exit function, calling functions can specify an
33484235Smarkfen  * error type. If the command calling this function was started by smf(5) the
33494235Smarkfen  * error type could be used as a hint to the restarter. In the future this
33504235Smarkfen  * function could be used to do something more intelligent with a process that
335110019SMark.Fenwick@Sun.COM  * encounters an error. If exit() is called with an error code other than those
335210019SMark.Fenwick@Sun.COM  * defined by smf(5), the program will just get restarted. Unless restarting
335310019SMark.Fenwick@Sun.COM  * is likely to resolve the error condition, its probably sensible to just
335410019SMark.Fenwick@Sun.COM  * log the error and keep running.
335510019SMark.Fenwick@Sun.COM  *
335610019SMark.Fenwick@Sun.COM  * The SERVICE_* exit_types mean nothing if the command was run from the
335710019SMark.Fenwick@Sun.COM  * command line, just exit(). There are two special cases:
335810019SMark.Fenwick@Sun.COM  *
335910019SMark.Fenwick@Sun.COM  * SERVICE_DEGRADE - Not implemented in smf(5), one day it could hint that
336010019SMark.Fenwick@Sun.COM  *                   the service is not running as well is it could. For
336110019SMark.Fenwick@Sun.COM  *                   now, don't do anything, just record the error.
336210019SMark.Fenwick@Sun.COM  * DEBUG_FATAL - Something happened, if the command was being run in debug
336310019SMark.Fenwick@Sun.COM  *               mode, exit() as you really want to know something happened,
336410019SMark.Fenwick@Sun.COM  *               otherwise just keep running. This is ignored when running
336510019SMark.Fenwick@Sun.COM  *               under smf(5).
33664235Smarkfen  *
33674235Smarkfen  * The function will handle an optional variable args error message, this
33684235Smarkfen  * will be written to the error stream, typically a log file or stderr.
33694235Smarkfen  */
33704235Smarkfen void
ipsecutil_exit(exit_type_t type,char * fmri,FILE * fp,const char * fmt,...)33714235Smarkfen ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...)
33724235Smarkfen {
33734235Smarkfen 	int exit_status;
33744235Smarkfen 	va_list args;
33754235Smarkfen 
33764235Smarkfen 	if (fp == NULL)
33774235Smarkfen 		fp = stderr;
33784235Smarkfen 	if (fmt != NULL) {
33794235Smarkfen 		va_start(args, fmt);
33804235Smarkfen 		vwarnxfp(fp, fmt, args);
33814235Smarkfen 		va_end(args);
33824235Smarkfen 	}
33834235Smarkfen 
33844235Smarkfen 	if (fmri == NULL) {
33854235Smarkfen 		/* Command being run directly from a shell. */
33864235Smarkfen 		switch (type) {
33874235Smarkfen 		case SERVICE_EXIT_OK:
33884235Smarkfen 			exit_status = 0;
33894235Smarkfen 			break;
33904235Smarkfen 		case SERVICE_DEGRADE:
33914235Smarkfen 			return;
33924235Smarkfen 			break;
33934235Smarkfen 		case SERVICE_BADPERM:
33944235Smarkfen 		case SERVICE_BADCONF:
33954235Smarkfen 		case SERVICE_MAINTAIN:
33964235Smarkfen 		case SERVICE_DISABLE:
33974235Smarkfen 		case SERVICE_FATAL:
33984235Smarkfen 		case SERVICE_RESTART:
339910019SMark.Fenwick@Sun.COM 		case DEBUG_FATAL:
34004235Smarkfen 			warnxfp(fp, "Fatal error - exiting.");
34014235Smarkfen 			exit_status = 1;
34024235Smarkfen 			break;
34034235Smarkfen 		}
34044235Smarkfen 	} else {
34054235Smarkfen 		/* Command being run as a smf(5) method. */
34064235Smarkfen 		switch (type) {
34074235Smarkfen 		case SERVICE_EXIT_OK:
34084235Smarkfen 			exit_status = SMF_EXIT_OK;
34094235Smarkfen 			break;
341010019SMark.Fenwick@Sun.COM 		case SERVICE_DEGRADE: /* Not implemented yet. */
341110019SMark.Fenwick@Sun.COM 		case DEBUG_FATAL:
341210019SMark.Fenwick@Sun.COM 			/* Keep running, don't exit(). */
34134235Smarkfen 			return;
34144235Smarkfen 			break;
34154235Smarkfen 		case SERVICE_BADPERM:
34164235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
34174235Smarkfen 			    "Permission error with %s."), fmri);
34184235Smarkfen 			exit_status = SMF_EXIT_ERR_PERM;
34194235Smarkfen 			break;
34204235Smarkfen 		case SERVICE_BADCONF:
34214235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
34224235Smarkfen 			    "Bad configuration of service %s."), fmri);
34234235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
34244235Smarkfen 			break;
34254235Smarkfen 		case SERVICE_MAINTAIN:
34264235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
34274235Smarkfen 			    "Service %s needs maintenance."), fmri);
34284235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
34294235Smarkfen 			break;
34304235Smarkfen 		case SERVICE_DISABLE:
34314235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
34324235Smarkfen 			break;
34334235Smarkfen 		case SERVICE_FATAL:
34344235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
34354235Smarkfen 			    "Service %s fatal error."), fmri);
34364235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
34374235Smarkfen 			break;
34384235Smarkfen 		case SERVICE_RESTART:
34394235Smarkfen 			exit_status = 1;
34404235Smarkfen 			break;
34414235Smarkfen 		}
34424235Smarkfen 	}
34434235Smarkfen 	(void) fflush(fp);
34444235Smarkfen 	(void) fclose(fp);
34454235Smarkfen 	exit(exit_status);
34464235Smarkfen }
3447