10Sstevel@tonic-gate /* 2*6119Spwernau * 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 /* 235906Svk199839 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <unistd.h> 300Sstevel@tonic-gate #include <stdio.h> 310Sstevel@tonic-gate #include <stdlib.h> 320Sstevel@tonic-gate #include <stdarg.h> 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate #include <sys/stat.h> 350Sstevel@tonic-gate #include <fcntl.h> 360Sstevel@tonic-gate #include <sys/sysconf.h> 370Sstevel@tonic-gate #include <strings.h> 380Sstevel@tonic-gate #include <ctype.h> 390Sstevel@tonic-gate #include <errno.h> 400Sstevel@tonic-gate #include <sys/socket.h> 410Sstevel@tonic-gate #include <netdb.h> 420Sstevel@tonic-gate #include <netinet/in.h> 430Sstevel@tonic-gate #include <arpa/inet.h> 440Sstevel@tonic-gate #include <net/pfkeyv2.h> 450Sstevel@tonic-gate #include <net/pfpolicy.h> 460Sstevel@tonic-gate #include <libintl.h> 470Sstevel@tonic-gate #include <setjmp.h> 480Sstevel@tonic-gate #include <libgen.h> 494235Smarkfen #include <libscf.h> 500Sstevel@tonic-gate 510Sstevel@tonic-gate #include "ipsec_util.h" 520Sstevel@tonic-gate #include "ikedoor.h" 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* 550Sstevel@tonic-gate * This file contains support functions that are shared by the ipsec 564867Spwernau * utilities and daemons including ipseckey(1m), ikeadm(1m) and in.iked(1m). 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate 594867Spwernau 604867Spwernau #define EFD(file) (((file) == stdout) ? stderr : (file)) 614867Spwernau 620Sstevel@tonic-gate /* Set standard default/initial values for globals... */ 630Sstevel@tonic-gate boolean_t pflag = B_FALSE; /* paranoid w.r.t. printing keying material */ 640Sstevel@tonic-gate boolean_t nflag = B_FALSE; /* avoid nameservice? */ 650Sstevel@tonic-gate boolean_t interactive = B_FALSE; /* util not running on cmdline */ 660Sstevel@tonic-gate boolean_t readfile = B_FALSE; /* cmds are being read from a file */ 670Sstevel@tonic-gate uint_t lineno = 0; /* track location if reading cmds from file */ 684235Smarkfen uint_t lines_added = 0; 694235Smarkfen uint_t lines_parsed = 0; 700Sstevel@tonic-gate jmp_buf env; /* for error recovery in interactive/readfile modes */ 714235Smarkfen char *my_fmri = NULL; 724235Smarkfen FILE *debugfile = stderr; 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * Print errno and exit if cmdline or readfile, reset state if interactive 764064Smarkfen * The error string *what should be dgettext()'d before calling bail(). 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate void 790Sstevel@tonic-gate bail(char *what) 800Sstevel@tonic-gate { 810Sstevel@tonic-gate if (errno != 0) 820Sstevel@tonic-gate warn(what); 830Sstevel@tonic-gate else 844235Smarkfen warnx(dgettext(TEXT_DOMAIN, "Error: %s"), what); 850Sstevel@tonic-gate if (readfile) { 864235Smarkfen return; 870Sstevel@tonic-gate } 880Sstevel@tonic-gate if (interactive && !readfile) 890Sstevel@tonic-gate longjmp(env, 2); 904235Smarkfen EXIT_FATAL(NULL); 910Sstevel@tonic-gate } 920Sstevel@tonic-gate 930Sstevel@tonic-gate /* 940Sstevel@tonic-gate * Print caller-supplied variable-arg error msg, then exit if cmdline or 950Sstevel@tonic-gate * readfile, or reset state if interactive. 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate /*PRINTFLIKE1*/ 980Sstevel@tonic-gate void 990Sstevel@tonic-gate bail_msg(char *fmt, ...) 1000Sstevel@tonic-gate { 1010Sstevel@tonic-gate va_list ap; 1020Sstevel@tonic-gate char msgbuf[BUFSIZ]; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate va_start(ap, fmt); 1050Sstevel@tonic-gate (void) vsnprintf(msgbuf, BUFSIZ, fmt, ap); 1060Sstevel@tonic-gate va_end(ap); 1070Sstevel@tonic-gate if (readfile) 1084064Smarkfen warnx(dgettext(TEXT_DOMAIN, 1094064Smarkfen "ERROR on line %u:\n%s\n"), lineno, msgbuf); 1100Sstevel@tonic-gate else 1114064Smarkfen warnx(dgettext(TEXT_DOMAIN, "ERROR: %s\n"), msgbuf); 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate if (interactive && !readfile) 1140Sstevel@tonic-gate longjmp(env, 1); 1150Sstevel@tonic-gate 1164235Smarkfen EXIT_FATAL(NULL); 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * dump_XXX functions produce ASCII output from various structures. 1220Sstevel@tonic-gate * 1230Sstevel@tonic-gate * Because certain errors need to do this to stderr, dump_XXX functions 1240Sstevel@tonic-gate * take a FILE pointer. 1250Sstevel@tonic-gate * 1260Sstevel@tonic-gate * If an error occured while writing to the specified file, these 1270Sstevel@tonic-gate * functions return -1, zero otherwise. 1280Sstevel@tonic-gate */ 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate int 1313055Sdanmcd dump_sockaddr(struct sockaddr *sa, uint8_t prefixlen, boolean_t addr_only, 1324867Spwernau FILE *where, boolean_t ignore_nss) 1330Sstevel@tonic-gate { 1340Sstevel@tonic-gate struct sockaddr_in *sin; 1350Sstevel@tonic-gate struct sockaddr_in6 *sin6; 1360Sstevel@tonic-gate char *printable_addr, *protocol; 1370Sstevel@tonic-gate uint8_t *addrptr; 1383055Sdanmcd /* Add 4 chars to hold '/nnn' for prefixes. */ 1393055Sdanmcd char storage[INET6_ADDRSTRLEN + 4]; 1400Sstevel@tonic-gate uint16_t port; 1410Sstevel@tonic-gate boolean_t unspec; 1420Sstevel@tonic-gate struct hostent *hp; 1430Sstevel@tonic-gate int getipnode_errno, addrlen; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate switch (sa->sa_family) { 1460Sstevel@tonic-gate case AF_INET: 1470Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 1480Sstevel@tonic-gate sin = (struct sockaddr_in *)sa; 1490Sstevel@tonic-gate addrptr = (uint8_t *)&sin->sin_addr; 1500Sstevel@tonic-gate port = sin->sin_port; 1510Sstevel@tonic-gate protocol = "AF_INET"; 1520Sstevel@tonic-gate unspec = (sin->sin_addr.s_addr == 0); 1530Sstevel@tonic-gate addrlen = sizeof (sin->sin_addr); 1540Sstevel@tonic-gate break; 1550Sstevel@tonic-gate case AF_INET6: 1560Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 1570Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)sa; 1580Sstevel@tonic-gate addrptr = (uint8_t *)&sin6->sin6_addr; 1590Sstevel@tonic-gate port = sin6->sin6_port; 1600Sstevel@tonic-gate protocol = "AF_INET6"; 1610Sstevel@tonic-gate unspec = IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr); 1620Sstevel@tonic-gate addrlen = sizeof (sin6->sin6_addr); 1630Sstevel@tonic-gate break; 1640Sstevel@tonic-gate default: 1650Sstevel@tonic-gate return (0); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate if (inet_ntop(sa->sa_family, addrptr, storage, INET6_ADDRSTRLEN) == 1690Sstevel@tonic-gate NULL) { 1704064Smarkfen printable_addr = dgettext(TEXT_DOMAIN, "Invalid IP address."); 1710Sstevel@tonic-gate } else { 1723055Sdanmcd char prefix[5]; /* "/nnn" with terminator. */ 1733055Sdanmcd 1743055Sdanmcd (void) snprintf(prefix, sizeof (prefix), "/%d", prefixlen); 1750Sstevel@tonic-gate printable_addr = storage; 1763055Sdanmcd if (prefixlen != 0) { 1773055Sdanmcd (void) strlcat(printable_addr, prefix, 1783055Sdanmcd sizeof (storage)); 1793055Sdanmcd } 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate if (addr_only) { 1820Sstevel@tonic-gate if (fprintf(where, "%s", printable_addr) < 0) 1830Sstevel@tonic-gate return (-1); 1840Sstevel@tonic-gate } else { 1854064Smarkfen if (fprintf(where, dgettext(TEXT_DOMAIN, 1864064Smarkfen "%s: port %d, %s"), protocol, 1870Sstevel@tonic-gate ntohs(port), printable_addr) < 0) 1880Sstevel@tonic-gate return (-1); 1894867Spwernau if (ignore_nss == B_FALSE) { 1900Sstevel@tonic-gate /* 1910Sstevel@tonic-gate * Do AF_independent reverse hostname lookup here. 1920Sstevel@tonic-gate */ 1930Sstevel@tonic-gate if (unspec) { 1940Sstevel@tonic-gate if (fprintf(where, 1954064Smarkfen dgettext(TEXT_DOMAIN, 1964064Smarkfen " <unspecified>")) < 0) 1970Sstevel@tonic-gate return (-1); 1980Sstevel@tonic-gate } else { 1990Sstevel@tonic-gate hp = getipnodebyaddr((char *)addrptr, addrlen, 2000Sstevel@tonic-gate sa->sa_family, &getipnode_errno); 2010Sstevel@tonic-gate if (hp != NULL) { 2020Sstevel@tonic-gate if (fprintf(where, 2030Sstevel@tonic-gate " (%s)", hp->h_name) < 0) 2040Sstevel@tonic-gate return (-1); 2050Sstevel@tonic-gate freehostent(hp); 2060Sstevel@tonic-gate } else { 2070Sstevel@tonic-gate if (fprintf(where, 2084064Smarkfen dgettext(TEXT_DOMAIN, 2094064Smarkfen " <unknown>")) < 0) 2100Sstevel@tonic-gate return (-1); 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate if (fputs(".\n", where) == EOF) 2150Sstevel@tonic-gate return (-1); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate return (0); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* 2210Sstevel@tonic-gate * Dump a key and bitlen 2220Sstevel@tonic-gate */ 2230Sstevel@tonic-gate int 2240Sstevel@tonic-gate dump_key(uint8_t *keyp, uint_t bitlen, FILE *where) 2250Sstevel@tonic-gate { 2260Sstevel@tonic-gate int numbytes; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate numbytes = SADB_1TO8(bitlen); 2290Sstevel@tonic-gate /* The & 0x7 is to check for leftover bits. */ 2300Sstevel@tonic-gate if ((bitlen & 0x7) != 0) 2310Sstevel@tonic-gate numbytes++; 2320Sstevel@tonic-gate while (numbytes-- != 0) { 2330Sstevel@tonic-gate if (pflag) { 2340Sstevel@tonic-gate /* Print no keys if paranoid */ 2350Sstevel@tonic-gate if (fprintf(where, "XX") < 0) 2360Sstevel@tonic-gate return (-1); 2370Sstevel@tonic-gate } else { 2380Sstevel@tonic-gate if (fprintf(where, "%02x", *keyp++) < 0) 2390Sstevel@tonic-gate return (-1); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate if (fprintf(where, "/%u", bitlen) < 0) 2430Sstevel@tonic-gate return (-1); 2440Sstevel@tonic-gate return (0); 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * Print an authentication or encryption algorithm 2490Sstevel@tonic-gate */ 2500Sstevel@tonic-gate static int 2510Sstevel@tonic-gate dump_generic_alg(uint8_t alg_num, int proto_num, FILE *where) 2520Sstevel@tonic-gate { 2530Sstevel@tonic-gate struct ipsecalgent *alg; 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate alg = getipsecalgbynum(alg_num, proto_num, NULL); 2560Sstevel@tonic-gate if (alg == NULL) { 2574064Smarkfen if (fprintf(where, dgettext(TEXT_DOMAIN, 2584064Smarkfen "<unknown %u>"), alg_num) < 0) 2590Sstevel@tonic-gate return (-1); 2600Sstevel@tonic-gate return (0); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * Special-case <none> for backward output compat. 2650Sstevel@tonic-gate * Assume that SADB_AALG_NONE == SADB_EALG_NONE. 2660Sstevel@tonic-gate */ 2670Sstevel@tonic-gate if (alg_num == SADB_AALG_NONE) { 2684064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, 2694064Smarkfen "<none>"), where) == EOF) 2700Sstevel@tonic-gate return (-1); 2710Sstevel@tonic-gate } else { 2720Sstevel@tonic-gate if (fputs(alg->a_names[0], where) == EOF) 2730Sstevel@tonic-gate return (-1); 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate freeipsecalgent(alg); 2770Sstevel@tonic-gate return (0); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate int 2810Sstevel@tonic-gate dump_aalg(uint8_t aalg, FILE *where) 2820Sstevel@tonic-gate { 2830Sstevel@tonic-gate return (dump_generic_alg(aalg, IPSEC_PROTO_AH, where)); 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate int 2870Sstevel@tonic-gate dump_ealg(uint8_t ealg, FILE *where) 2880Sstevel@tonic-gate { 2890Sstevel@tonic-gate return (dump_generic_alg(ealg, IPSEC_PROTO_ESP, where)); 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * Print an SADB_IDENTTYPE string 2940Sstevel@tonic-gate * 2950Sstevel@tonic-gate * Also return TRUE if the actual ident may be printed, FALSE if not. 2960Sstevel@tonic-gate * 2970Sstevel@tonic-gate * If rc is not NULL, set its value to -1 if an error occured while writing 2980Sstevel@tonic-gate * to the specified file, zero otherwise. 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate boolean_t 3010Sstevel@tonic-gate dump_sadb_idtype(uint8_t idtype, FILE *where, int *rc) 3020Sstevel@tonic-gate { 3030Sstevel@tonic-gate boolean_t canprint = B_TRUE; 3040Sstevel@tonic-gate int rc_val = 0; 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate switch (idtype) { 3070Sstevel@tonic-gate case SADB_IDENTTYPE_PREFIX: 3084064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "prefix"), where) == EOF) 3090Sstevel@tonic-gate rc_val = -1; 3100Sstevel@tonic-gate break; 3110Sstevel@tonic-gate case SADB_IDENTTYPE_FQDN: 3124064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "FQDN"), where) == EOF) 3130Sstevel@tonic-gate rc_val = -1; 3140Sstevel@tonic-gate break; 3150Sstevel@tonic-gate case SADB_IDENTTYPE_USER_FQDN: 3164064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, 3174064Smarkfen "user-FQDN (mbox)"), where) == EOF) 3180Sstevel@tonic-gate rc_val = -1; 3190Sstevel@tonic-gate break; 3200Sstevel@tonic-gate case SADB_X_IDENTTYPE_DN: 3214064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Distinguished Name"), 3220Sstevel@tonic-gate where) == EOF) 3230Sstevel@tonic-gate rc_val = -1; 3240Sstevel@tonic-gate canprint = B_FALSE; 3250Sstevel@tonic-gate break; 3260Sstevel@tonic-gate case SADB_X_IDENTTYPE_GN: 3274064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Generic Name"), 3284064Smarkfen where) == EOF) 3290Sstevel@tonic-gate rc_val = -1; 3300Sstevel@tonic-gate canprint = B_FALSE; 3310Sstevel@tonic-gate break; 3320Sstevel@tonic-gate case SADB_X_IDENTTYPE_KEY_ID: 3334064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "Generic key id"), 3344064Smarkfen where) == EOF) 3350Sstevel@tonic-gate rc_val = -1; 3360Sstevel@tonic-gate break; 3370Sstevel@tonic-gate case SADB_X_IDENTTYPE_ADDR_RANGE: 3384064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "Address range"), where) == EOF) 3390Sstevel@tonic-gate rc_val = -1; 3400Sstevel@tonic-gate break; 3410Sstevel@tonic-gate default: 3424064Smarkfen if (fprintf(where, dgettext(TEXT_DOMAIN, 3434064Smarkfen "<unknown %u>"), idtype) < 0) 3440Sstevel@tonic-gate rc_val = -1; 3450Sstevel@tonic-gate break; 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate if (rc != NULL) 3490Sstevel@tonic-gate *rc = rc_val; 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate return (canprint); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate /* 3550Sstevel@tonic-gate * Slice an argv/argc vector from an interactive line or a read-file line. 3560Sstevel@tonic-gate */ 3570Sstevel@tonic-gate static int 3580Sstevel@tonic-gate create_argv(char *ibuf, int *newargc, char ***thisargv) 3590Sstevel@tonic-gate { 3600Sstevel@tonic-gate unsigned int argvlen = START_ARG; 3610Sstevel@tonic-gate char **current; 3620Sstevel@tonic-gate boolean_t firstchar = B_TRUE; 3630Sstevel@tonic-gate boolean_t inquotes = B_FALSE; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate *thisargv = malloc(sizeof (char *) * argvlen); 3660Sstevel@tonic-gate if ((*thisargv) == NULL) 3670Sstevel@tonic-gate return (MEMORY_ALLOCATION); 3680Sstevel@tonic-gate current = *thisargv; 3690Sstevel@tonic-gate *current = NULL; 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate for (; *ibuf != '\0'; ibuf++) { 3720Sstevel@tonic-gate if (isspace(*ibuf)) { 3730Sstevel@tonic-gate if (inquotes) { 3740Sstevel@tonic-gate continue; 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate if (*current != NULL) { 3770Sstevel@tonic-gate *ibuf = '\0'; 3780Sstevel@tonic-gate current++; 3790Sstevel@tonic-gate if (*thisargv + argvlen == current) { 3800Sstevel@tonic-gate /* Regrow ***thisargv. */ 3810Sstevel@tonic-gate if (argvlen == TOO_MANY_ARGS) { 3820Sstevel@tonic-gate free(*thisargv); 3830Sstevel@tonic-gate return (TOO_MANY_TOKENS); 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate /* Double the allocation. */ 3860Sstevel@tonic-gate current = realloc(*thisargv, 3870Sstevel@tonic-gate sizeof (char *) * (argvlen << 1)); 3880Sstevel@tonic-gate if (current == NULL) { 3890Sstevel@tonic-gate free(*thisargv); 3900Sstevel@tonic-gate return (MEMORY_ALLOCATION); 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate *thisargv = current; 3930Sstevel@tonic-gate current += argvlen; 3940Sstevel@tonic-gate argvlen <<= 1; /* Double the size. */ 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate *current = NULL; 3970Sstevel@tonic-gate } 3980Sstevel@tonic-gate } else { 3990Sstevel@tonic-gate if (firstchar) { 4000Sstevel@tonic-gate firstchar = B_FALSE; 4014235Smarkfen if (*ibuf == COMMENT_CHAR || *ibuf == '\n') { 4020Sstevel@tonic-gate free(*thisargv); 4030Sstevel@tonic-gate return (COMMENT_LINE); 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate if (*ibuf == QUOTE_CHAR) { 4070Sstevel@tonic-gate if (inquotes) { 4080Sstevel@tonic-gate inquotes = B_FALSE; 4090Sstevel@tonic-gate *ibuf = '\0'; 4100Sstevel@tonic-gate } else { 4110Sstevel@tonic-gate inquotes = B_TRUE; 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate continue; 4140Sstevel@tonic-gate } 4150Sstevel@tonic-gate if (*current == NULL) { 4160Sstevel@tonic-gate *current = ibuf; 4170Sstevel@tonic-gate (*newargc)++; 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate /* 4230Sstevel@tonic-gate * Tricky corner case... 4240Sstevel@tonic-gate * I've parsed _exactly_ the amount of args as I have space. It 4250Sstevel@tonic-gate * won't return NULL-terminated, and bad things will happen to 4260Sstevel@tonic-gate * the caller. 4270Sstevel@tonic-gate */ 4280Sstevel@tonic-gate if (argvlen == *newargc) { 4290Sstevel@tonic-gate current = realloc(*thisargv, sizeof (char *) * (argvlen + 1)); 4300Sstevel@tonic-gate if (current == NULL) { 4310Sstevel@tonic-gate free(*thisargv); 4320Sstevel@tonic-gate return (MEMORY_ALLOCATION); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate *thisargv = current; 4350Sstevel@tonic-gate current[argvlen] = NULL; 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate return (SUCCESS); 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate /* 4420Sstevel@tonic-gate * Enter a mode where commands are read from a file. Treat stdin special. 4430Sstevel@tonic-gate */ 4440Sstevel@tonic-gate void 4454235Smarkfen do_interactive(FILE *infile, char *configfile, char *promptstring, 4464235Smarkfen char *my_fmri, parse_cmdln_fn parseit) 4470Sstevel@tonic-gate { 4480Sstevel@tonic-gate char ibuf[IBUF_SIZE], holder[IBUF_SIZE]; 4494235Smarkfen char *hptr, **thisargv, *ebuf; 4500Sstevel@tonic-gate int thisargc; 4510Sstevel@tonic-gate boolean_t continue_in_progress = B_FALSE; 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate (void) setjmp(env); 4540Sstevel@tonic-gate 4554235Smarkfen ebuf = NULL; 4560Sstevel@tonic-gate interactive = B_TRUE; 4570Sstevel@tonic-gate bzero(ibuf, IBUF_SIZE); 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate if (infile == stdin) { 4600Sstevel@tonic-gate (void) printf("%s", promptstring); 4610Sstevel@tonic-gate (void) fflush(stdout); 4620Sstevel@tonic-gate } else { 4630Sstevel@tonic-gate readfile = B_TRUE; 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate while (fgets(ibuf, IBUF_SIZE, infile) != NULL) { 4670Sstevel@tonic-gate if (readfile) 4680Sstevel@tonic-gate lineno++; 4690Sstevel@tonic-gate thisargc = 0; 4700Sstevel@tonic-gate thisargv = NULL; 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate /* 4730Sstevel@tonic-gate * Check byte IBUF_SIZE - 2, because byte IBUF_SIZE - 1 will 4740Sstevel@tonic-gate * be null-terminated because of fgets(). 4750Sstevel@tonic-gate */ 4760Sstevel@tonic-gate if (ibuf[IBUF_SIZE - 2] != '\0') { 4774235Smarkfen ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, 4784235Smarkfen dgettext(TEXT_DOMAIN, "Line %d too big."), lineno); 4790Sstevel@tonic-gate } 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate if (!continue_in_progress) { 4820Sstevel@tonic-gate /* Use -2 because of \n from fgets. */ 4830Sstevel@tonic-gate if (ibuf[strlen(ibuf) - 2] == CONT_CHAR) { 4840Sstevel@tonic-gate /* 4850Sstevel@tonic-gate * Can use strcpy here, I've checked the 4860Sstevel@tonic-gate * length already. 4870Sstevel@tonic-gate */ 4880Sstevel@tonic-gate (void) strcpy(holder, ibuf); 4890Sstevel@tonic-gate hptr = &(holder[strlen(holder)]); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate /* Remove the CONT_CHAR from the string. */ 4920Sstevel@tonic-gate hptr[-2] = ' '; 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate continue_in_progress = B_TRUE; 4950Sstevel@tonic-gate bzero(ibuf, IBUF_SIZE); 4960Sstevel@tonic-gate continue; 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate } else { 4990Sstevel@tonic-gate /* Handle continuations... */ 5000Sstevel@tonic-gate (void) strncpy(hptr, ibuf, 5010Sstevel@tonic-gate (size_t)(&(holder[IBUF_SIZE]) - hptr)); 5020Sstevel@tonic-gate if (holder[IBUF_SIZE - 1] != '\0') { 5034235Smarkfen ipsecutil_exit(SERVICE_FATAL, my_fmri, 5044235Smarkfen debugfile, dgettext(TEXT_DOMAIN, 5054235Smarkfen "Command buffer overrun.")); 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate /* Use - 2 because of \n from fgets. */ 5080Sstevel@tonic-gate if (hptr[strlen(hptr) - 2] == CONT_CHAR) { 5090Sstevel@tonic-gate bzero(ibuf, IBUF_SIZE); 5100Sstevel@tonic-gate hptr += strlen(hptr); 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate /* Remove the CONT_CHAR from the string. */ 5130Sstevel@tonic-gate hptr[-2] = ' '; 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate continue; 5160Sstevel@tonic-gate } else { 5170Sstevel@tonic-gate continue_in_progress = B_FALSE; 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * I've already checked the length... 5200Sstevel@tonic-gate */ 5210Sstevel@tonic-gate (void) strcpy(ibuf, holder); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate 5254235Smarkfen /* 5264235Smarkfen * Just in case the command fails keep a copy of the 5274235Smarkfen * command buffer for diagnostic output. 5284235Smarkfen */ 5294235Smarkfen if (readfile) { 5304235Smarkfen /* 5314235Smarkfen * The error buffer needs to be big enough to 5324235Smarkfen * hold the longest command string, plus 5334235Smarkfen * some extra text, see below. 5344235Smarkfen */ 5354235Smarkfen ebuf = calloc((IBUF_SIZE * 2), sizeof (char)); 5364235Smarkfen if (ebuf == NULL) { 5374235Smarkfen ipsecutil_exit(SERVICE_FATAL, my_fmri, 5384235Smarkfen debugfile, dgettext(TEXT_DOMAIN, 5394235Smarkfen "Memory allocation error.")); 5404235Smarkfen } else { 5414235Smarkfen (void) snprintf(ebuf, (IBUF_SIZE * 2), 5424235Smarkfen dgettext(TEXT_DOMAIN, 5434235Smarkfen "Config file entry near line %u " 5444235Smarkfen "caused error(s) or warnings:\n\n%s\n\n"), 5454235Smarkfen lineno, ibuf); 5464235Smarkfen } 5474235Smarkfen } 5484235Smarkfen 5490Sstevel@tonic-gate switch (create_argv(ibuf, &thisargc, &thisargv)) { 5500Sstevel@tonic-gate case TOO_MANY_TOKENS: 5514235Smarkfen ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, 5524235Smarkfen dgettext(TEXT_DOMAIN, "Too many input tokens.")); 5530Sstevel@tonic-gate break; 5540Sstevel@tonic-gate case MEMORY_ALLOCATION: 5554235Smarkfen ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, 5564235Smarkfen dgettext(TEXT_DOMAIN, "Memory allocation error.")); 5570Sstevel@tonic-gate break; 5580Sstevel@tonic-gate case COMMENT_LINE: 5590Sstevel@tonic-gate /* Comment line. */ 5604235Smarkfen free(ebuf); 5610Sstevel@tonic-gate break; 5620Sstevel@tonic-gate default: 5634235Smarkfen if (thisargc != 0) { 5644235Smarkfen lines_parsed++; 5654235Smarkfen /* ebuf consumed */ 5664342Spwernau parseit(thisargc, thisargv, ebuf, readfile); 5674235Smarkfen } else { 5684235Smarkfen free(ebuf); 5694235Smarkfen } 5700Sstevel@tonic-gate free(thisargv); 5710Sstevel@tonic-gate if (infile == stdin) { 5720Sstevel@tonic-gate (void) printf("%s", promptstring); 5730Sstevel@tonic-gate (void) fflush(stdout); 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate break; 5760Sstevel@tonic-gate } 5770Sstevel@tonic-gate bzero(ibuf, IBUF_SIZE); 5780Sstevel@tonic-gate } 5794342Spwernau 5804342Spwernau /* 5814342Spwernau * The following code is ipseckey specific. This should never be 5824342Spwernau * used by ikeadm which also calls this function because ikeadm 5834342Spwernau * only runs interactively. If this ever changes this code block 5844342Spwernau * sould be revisited. 5854342Spwernau */ 5864342Spwernau if (readfile) { 5874342Spwernau if (lines_parsed != 0 && lines_added == 0) { 5884342Spwernau ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, 5894342Spwernau dgettext(TEXT_DOMAIN, "Configuration file did not " 5904342Spwernau "contain any valid SAs")); 5914342Spwernau } 5924342Spwernau 5934342Spwernau /* 5944342Spwernau * There were errors. Putting the service in maintenance mode. 5954342Spwernau * When svc.startd(1M) allows services to degrade themselves, 5964342Spwernau * this should be revisited. 5974342Spwernau * 5984342Spwernau * If this function was called from a program running as a 5994342Spwernau * smf_method(5), print a warning message. Don't spew out the 6004342Spwernau * errors as these will end up in the smf(5) log file which is 6014342Spwernau * publically readable, the errors may contain sensitive 6024342Spwernau * information. 6034342Spwernau */ 6044342Spwernau if ((lines_added < lines_parsed) && (configfile != NULL)) { 6054342Spwernau if (my_fmri != NULL) { 6064342Spwernau ipsecutil_exit(SERVICE_BADCONF, my_fmri, 6074342Spwernau debugfile, dgettext(TEXT_DOMAIN, 6084342Spwernau "The configuration file contained %d " 6094342Spwernau "errors.\n" 6104342Spwernau "Manually check the configuration with:\n" 6114342Spwernau "ipseckey -c %s\n" 6124342Spwernau "Use svcadm(1M) to clear maintenance " 6134342Spwernau "condition when errors are resolved.\n"), 6144342Spwernau lines_parsed - lines_added, configfile); 6154342Spwernau } else { 6164342Spwernau EXIT_BADCONFIG(NULL); 6174342Spwernau } 6184342Spwernau } else { 6194342Spwernau if (my_fmri != NULL) 6204342Spwernau ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, 6214342Spwernau debugfile, dgettext(TEXT_DOMAIN, 6224342Spwernau "%d actions successfully processed."), 6234342Spwernau lines_added); 6244342Spwernau } 6254342Spwernau } else { 6260Sstevel@tonic-gate (void) putchar('\n'); 6270Sstevel@tonic-gate (void) fflush(stdout); 6280Sstevel@tonic-gate } 6294235Smarkfen EXIT_OK(NULL); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate /* 6330Sstevel@tonic-gate * Functions to parse strings that represent a debug or privilege level. 6340Sstevel@tonic-gate * These functions are copied from main.c and door.c in usr.lib/in.iked/common. 6350Sstevel@tonic-gate * If this file evolves into a common library that may be used by in.iked 6360Sstevel@tonic-gate * as well as the usr.sbin utilities, those duplicate functions should be 6370Sstevel@tonic-gate * deleted. 6380Sstevel@tonic-gate * 6390Sstevel@tonic-gate * A privilege level may be represented by a simple keyword, corresponding 6400Sstevel@tonic-gate * to one of the possible levels. A debug level may be represented by a 6410Sstevel@tonic-gate * series of keywords, separated by '+' or '-', indicating categories to 6420Sstevel@tonic-gate * be added or removed from the set of categories in the debug level. 6430Sstevel@tonic-gate * For example, +all-op corresponds to level 0xfffffffb (all flags except 6440Sstevel@tonic-gate * for D_OP set); while p1+p2+pfkey corresponds to level 0x38. Note that 6450Sstevel@tonic-gate * the leading '+' is implicit; the first keyword in the list must be for 6460Sstevel@tonic-gate * a category that is to be added. 6470Sstevel@tonic-gate * 6480Sstevel@tonic-gate * These parsing functions make use of a local version of strtok, strtok_d, 6490Sstevel@tonic-gate * which includes an additional parameter, char *delim. This param is filled 6500Sstevel@tonic-gate * in with the character which ends the returned token. In other words, 6510Sstevel@tonic-gate * this version of strtok, in addition to returning the token, also returns 6520Sstevel@tonic-gate * the single character delimiter from the original string which marked the 6530Sstevel@tonic-gate * end of the token. 6540Sstevel@tonic-gate */ 6550Sstevel@tonic-gate static char * 6560Sstevel@tonic-gate strtok_d(char *string, const char *sepset, char *delim) 6570Sstevel@tonic-gate { 6580Sstevel@tonic-gate static char *lasts; 6590Sstevel@tonic-gate char *q, *r; 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate /* first or subsequent call */ 6620Sstevel@tonic-gate if (string == NULL) 6630Sstevel@tonic-gate string = lasts; 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate if (string == 0) /* return if no tokens remaining */ 6660Sstevel@tonic-gate return (NULL); 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate q = string + strspn(string, sepset); /* skip leading separators */ 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate if (*q == '\0') /* return if no tokens remaining */ 6710Sstevel@tonic-gate return (NULL); 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate if ((r = strpbrk(q, sepset)) == NULL) { /* move past token */ 6740Sstevel@tonic-gate lasts = 0; /* indicate that this is last token */ 6750Sstevel@tonic-gate } else { 6760Sstevel@tonic-gate *delim = *r; /* save delimitor */ 6770Sstevel@tonic-gate *r = '\0'; 6780Sstevel@tonic-gate lasts = r + 1; 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate return (q); 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate static keywdtab_t privtab[] = { 6840Sstevel@tonic-gate { IKE_PRIV_MINIMUM, "base" }, 6850Sstevel@tonic-gate { IKE_PRIV_MODKEYS, "modkeys" }, 6860Sstevel@tonic-gate { IKE_PRIV_KEYMAT, "keymat" }, 6870Sstevel@tonic-gate { IKE_PRIV_MINIMUM, "0" }, 6880Sstevel@tonic-gate }; 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate int 6910Sstevel@tonic-gate privstr2num(char *str) 6920Sstevel@tonic-gate { 6930Sstevel@tonic-gate keywdtab_t *pp; 6940Sstevel@tonic-gate char *endp; 6950Sstevel@tonic-gate int priv; 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate for (pp = privtab; pp < A_END(privtab); pp++) { 6980Sstevel@tonic-gate if (strcasecmp(str, pp->kw_str) == 0) 6990Sstevel@tonic-gate return (pp->kw_tag); 7000Sstevel@tonic-gate } 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate priv = strtol(str, &endp, 0); 7030Sstevel@tonic-gate if (*endp == '\0') 7040Sstevel@tonic-gate return (priv); 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate return (-1); 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate static keywdtab_t dbgtab[] = { 7100Sstevel@tonic-gate { D_CERT, "cert" }, 7110Sstevel@tonic-gate { D_KEY, "key" }, 7120Sstevel@tonic-gate { D_OP, "op" }, 7130Sstevel@tonic-gate { D_P1, "p1" }, 7140Sstevel@tonic-gate { D_P1, "phase1" }, 7150Sstevel@tonic-gate { D_P2, "p2" }, 7160Sstevel@tonic-gate { D_P2, "phase2" }, 7170Sstevel@tonic-gate { D_PFKEY, "pfkey" }, 7180Sstevel@tonic-gate { D_POL, "pol" }, 7190Sstevel@tonic-gate { D_POL, "policy" }, 7200Sstevel@tonic-gate { D_PROP, "prop" }, 7210Sstevel@tonic-gate { D_DOOR, "door" }, 7220Sstevel@tonic-gate { D_CONFIG, "config" }, 7230Sstevel@tonic-gate { D_ALL, "all" }, 7240Sstevel@tonic-gate { 0, "0" }, 7250Sstevel@tonic-gate }; 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate int 7280Sstevel@tonic-gate dbgstr2num(char *str) 7290Sstevel@tonic-gate { 7300Sstevel@tonic-gate keywdtab_t *dp; 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate for (dp = dbgtab; dp < A_END(dbgtab); dp++) { 7330Sstevel@tonic-gate if (strcasecmp(str, dp->kw_str) == 0) 7340Sstevel@tonic-gate return (dp->kw_tag); 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate return (D_INVALID); 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate int 7400Sstevel@tonic-gate parsedbgopts(char *optarg) 7410Sstevel@tonic-gate { 7420Sstevel@tonic-gate char *argp, *endp, op, nextop; 7430Sstevel@tonic-gate int mask = 0, new; 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate mask = strtol(optarg, &endp, 0); 7460Sstevel@tonic-gate if (*endp == '\0') 7470Sstevel@tonic-gate return (mask); 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate op = optarg[0]; 7500Sstevel@tonic-gate if (op != '-') 7510Sstevel@tonic-gate op = '+'; 7520Sstevel@tonic-gate argp = strtok_d(optarg, "+-", &nextop); 7530Sstevel@tonic-gate do { 7540Sstevel@tonic-gate new = dbgstr2num(argp); 7550Sstevel@tonic-gate if (new == D_INVALID) { 7560Sstevel@tonic-gate /* we encountered an invalid keywd */ 7570Sstevel@tonic-gate return (new); 7580Sstevel@tonic-gate } 7590Sstevel@tonic-gate if (op == '+') { 7600Sstevel@tonic-gate mask |= new; 7610Sstevel@tonic-gate } else { 7620Sstevel@tonic-gate mask &= ~new; 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate op = nextop; 7650Sstevel@tonic-gate } while ((argp = strtok_d(NULL, "+-", &nextop)) != NULL); 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate return (mask); 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * functions to manipulate the kmcookie-label mapping file 7730Sstevel@tonic-gate */ 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate /* 7760Sstevel@tonic-gate * Open, lockf, fdopen the given file, returning a FILE * on success, 7770Sstevel@tonic-gate * or NULL on failure. 7780Sstevel@tonic-gate */ 7790Sstevel@tonic-gate FILE * 7800Sstevel@tonic-gate kmc_open_and_lock(char *name) 7810Sstevel@tonic-gate { 7820Sstevel@tonic-gate int fd, rtnerr; 7830Sstevel@tonic-gate FILE *fp; 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate if ((fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) { 7860Sstevel@tonic-gate return (NULL); 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate if (lockf(fd, F_LOCK, 0) < 0) { 7890Sstevel@tonic-gate return (NULL); 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate if ((fp = fdopen(fd, "a+")) == NULL) { 7920Sstevel@tonic-gate return (NULL); 7930Sstevel@tonic-gate } 7940Sstevel@tonic-gate if (fseek(fp, 0, SEEK_SET) < 0) { 7950Sstevel@tonic-gate /* save errno in case fclose changes it */ 7960Sstevel@tonic-gate rtnerr = errno; 7970Sstevel@tonic-gate (void) fclose(fp); 7980Sstevel@tonic-gate errno = rtnerr; 7990Sstevel@tonic-gate return (NULL); 8000Sstevel@tonic-gate } 8010Sstevel@tonic-gate return (fp); 8020Sstevel@tonic-gate } 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate /* 8050Sstevel@tonic-gate * Extract an integer cookie and string label from a line from the 8060Sstevel@tonic-gate * kmcookie-label file. Return -1 on failure, 0 on success. 8070Sstevel@tonic-gate */ 8080Sstevel@tonic-gate int 8090Sstevel@tonic-gate kmc_parse_line(char *line, int *cookie, char **label) 8100Sstevel@tonic-gate { 8110Sstevel@tonic-gate char *cookiestr; 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate *cookie = 0; 8140Sstevel@tonic-gate *label = NULL; 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate cookiestr = strtok(line, " \t\n"); 8170Sstevel@tonic-gate if (cookiestr == NULL) { 8180Sstevel@tonic-gate return (-1); 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate /* Everything that follows, up to the newline, is the label. */ 8220Sstevel@tonic-gate *label = strtok(NULL, "\n"); 8230Sstevel@tonic-gate if (*label == NULL) { 8240Sstevel@tonic-gate return (-1); 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate *cookie = atoi(cookiestr); 8280Sstevel@tonic-gate return (0); 8290Sstevel@tonic-gate } 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate /* 8320Sstevel@tonic-gate * Insert a mapping into the file (if it's not already there), given the 8330Sstevel@tonic-gate * new label. Return the assigned cookie, or -1 on error. 8340Sstevel@tonic-gate */ 8350Sstevel@tonic-gate int 8360Sstevel@tonic-gate kmc_insert_mapping(char *label) 8370Sstevel@tonic-gate { 8380Sstevel@tonic-gate FILE *map; 8394757Sdanmcd char linebuf[IBUF_SIZE]; 8400Sstevel@tonic-gate char *cur_label; 8410Sstevel@tonic-gate int max_cookie = 0, cur_cookie, rtn_cookie; 8420Sstevel@tonic-gate int rtnerr = 0; 8430Sstevel@tonic-gate boolean_t found = B_FALSE; 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate /* open and lock the file; will sleep until lock is available */ 8460Sstevel@tonic-gate if ((map = kmc_open_and_lock(KMCFILE)) == NULL) { 8470Sstevel@tonic-gate /* kmc_open_and_lock() sets errno appropriately */ 8480Sstevel@tonic-gate return (-1); 8490Sstevel@tonic-gate } 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate while (fgets(linebuf, sizeof (linebuf), map) != NULL) { 8520Sstevel@tonic-gate 8534757Sdanmcd /* Skip blank lines, which often come near EOF. */ 8544757Sdanmcd if (strlen(linebuf) == 0) 8554757Sdanmcd continue; 8564757Sdanmcd 8570Sstevel@tonic-gate if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) { 8580Sstevel@tonic-gate rtnerr = EINVAL; 8590Sstevel@tonic-gate goto error; 8600Sstevel@tonic-gate } 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate if (cur_cookie > max_cookie) 8630Sstevel@tonic-gate max_cookie = cur_cookie; 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate if ((!found) && (strcmp(cur_label, label) == 0)) { 8660Sstevel@tonic-gate found = B_TRUE; 8670Sstevel@tonic-gate rtn_cookie = cur_cookie; 8680Sstevel@tonic-gate } 8690Sstevel@tonic-gate } 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate if (!found) { 8720Sstevel@tonic-gate rtn_cookie = ++max_cookie; 8730Sstevel@tonic-gate if ((fprintf(map, "%u\t%s\n", rtn_cookie, label) < 0) || 8740Sstevel@tonic-gate (fflush(map) < 0)) { 8750Sstevel@tonic-gate rtnerr = errno; 8760Sstevel@tonic-gate goto error; 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate (void) fclose(map); 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate return (rtn_cookie); 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate error: 8840Sstevel@tonic-gate (void) fclose(map); 8850Sstevel@tonic-gate errno = rtnerr; 8860Sstevel@tonic-gate return (-1); 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate /* 8900Sstevel@tonic-gate * Lookup the given cookie and return its corresponding label. Return 8910Sstevel@tonic-gate * a pointer to the label on success, NULL on error (or if the label is 8920Sstevel@tonic-gate * not found). Note that the returned label pointer points to a static 8930Sstevel@tonic-gate * string, so the label will be overwritten by a subsequent call to the 8940Sstevel@tonic-gate * function; the function is also not thread-safe as a result. 8950Sstevel@tonic-gate */ 8960Sstevel@tonic-gate char * 8970Sstevel@tonic-gate kmc_lookup_by_cookie(int cookie) 8980Sstevel@tonic-gate { 8990Sstevel@tonic-gate FILE *map; 9004757Sdanmcd static char linebuf[IBUF_SIZE]; 9010Sstevel@tonic-gate char *cur_label; 9020Sstevel@tonic-gate int cur_cookie; 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate if ((map = kmc_open_and_lock(KMCFILE)) == NULL) { 9050Sstevel@tonic-gate return (NULL); 9060Sstevel@tonic-gate } 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate while (fgets(linebuf, sizeof (linebuf), map) != NULL) { 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) { 9110Sstevel@tonic-gate (void) fclose(map); 9120Sstevel@tonic-gate return (NULL); 9130Sstevel@tonic-gate } 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate if (cookie == cur_cookie) { 9160Sstevel@tonic-gate (void) fclose(map); 9170Sstevel@tonic-gate return (cur_label); 9180Sstevel@tonic-gate } 9190Sstevel@tonic-gate } 9200Sstevel@tonic-gate (void) fclose(map); 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate return (NULL); 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate /* 9260Sstevel@tonic-gate * Parse basic extension headers and return in the passed-in pointer vector. 9270Sstevel@tonic-gate * Return values include: 9280Sstevel@tonic-gate * 9290Sstevel@tonic-gate * KGE_OK Everything's nice and parsed out. 9300Sstevel@tonic-gate * If there are no extensions, place NULL in extv[0]. 9310Sstevel@tonic-gate * KGE_DUP There is a duplicate extension. 9320Sstevel@tonic-gate * First instance in appropriate bin. First duplicate in 9330Sstevel@tonic-gate * extv[0]. 9340Sstevel@tonic-gate * KGE_UNK Unknown extension type encountered. extv[0] contains 9350Sstevel@tonic-gate * unknown header. 9360Sstevel@tonic-gate * KGE_LEN Extension length error. 9370Sstevel@tonic-gate * KGE_CHK High-level reality check failed on specific extension. 9380Sstevel@tonic-gate * 9390Sstevel@tonic-gate * My apologies for some of the pointer arithmetic in here. I'm thinking 9400Sstevel@tonic-gate * like an assembly programmer, yet trying to make the compiler happy. 9410Sstevel@tonic-gate */ 9420Sstevel@tonic-gate int 9430Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize, 9440Sstevel@tonic-gate char *diag_buf, uint_t diag_buf_len) 9450Sstevel@tonic-gate { 9460Sstevel@tonic-gate int i; 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate if (diag_buf != NULL) 9490Sstevel@tonic-gate diag_buf[0] = '\0'; 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate for (i = 1; i <= SPD_EXT_MAX; i++) 9520Sstevel@tonic-gate extv[i] = NULL; 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate i = 0; 9550Sstevel@tonic-gate /* Use extv[0] as the "current working pointer". */ 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate extv[0] = (spd_ext_t *)(basehdr + 1); 9580Sstevel@tonic-gate msgsize = SPD_64TO8(msgsize); 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate while ((char *)extv[0] < ((char *)basehdr + msgsize)) { 9610Sstevel@tonic-gate /* Check for unknown headers. */ 9620Sstevel@tonic-gate i++; 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate if (extv[0]->spd_ext_type == 0 || 9650Sstevel@tonic-gate extv[0]->spd_ext_type > SPD_EXT_MAX) { 9660Sstevel@tonic-gate if (diag_buf != NULL) { 9670Sstevel@tonic-gate (void) snprintf(diag_buf, diag_buf_len, 9680Sstevel@tonic-gate "spdsock ext 0x%X unknown: 0x%X", 9690Sstevel@tonic-gate i, extv[0]->spd_ext_type); 9700Sstevel@tonic-gate } 9710Sstevel@tonic-gate return (KGE_UNK); 9720Sstevel@tonic-gate } 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate /* 9750Sstevel@tonic-gate * Check length. Use uint64_t because extlen is in units 9760Sstevel@tonic-gate * of 64-bit words. If length goes beyond the msgsize, 9770Sstevel@tonic-gate * return an error. (Zero length also qualifies here.) 9780Sstevel@tonic-gate */ 9790Sstevel@tonic-gate if (extv[0]->spd_ext_len == 0 || 9800Sstevel@tonic-gate (uint8_t *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) > 9810Sstevel@tonic-gate (uint8_t *)((uint8_t *)basehdr + msgsize)) 9820Sstevel@tonic-gate return (KGE_LEN); 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate /* Check for redundant headers. */ 9850Sstevel@tonic-gate if (extv[extv[0]->spd_ext_type] != NULL) 9860Sstevel@tonic-gate return (KGE_DUP); 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate /* If I make it here, assign the appropriate bin. */ 9890Sstevel@tonic-gate extv[extv[0]->spd_ext_type] = extv[0]; 9900Sstevel@tonic-gate 9910Sstevel@tonic-gate /* Advance pointer (See above for uint64_t ptr reasoning.) */ 9920Sstevel@tonic-gate extv[0] = (spd_ext_t *) 9930Sstevel@tonic-gate ((uint64_t *)extv[0] + extv[0]->spd_ext_len); 9940Sstevel@tonic-gate } 9950Sstevel@tonic-gate 9960Sstevel@tonic-gate /* Everything's cool. */ 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate /* 9990Sstevel@tonic-gate * If extv[0] == NULL, then there are no extension headers in this 10000Sstevel@tonic-gate * message. Ensure that this is the case. 10010Sstevel@tonic-gate */ 10020Sstevel@tonic-gate if (extv[0] == (spd_ext_t *)(basehdr + 1)) 10030Sstevel@tonic-gate extv[0] = NULL; 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate return (KGE_OK); 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate const char * 10090Sstevel@tonic-gate spdsock_diag(int diagnostic) 10100Sstevel@tonic-gate { 10110Sstevel@tonic-gate switch (diagnostic) { 10120Sstevel@tonic-gate case SPD_DIAGNOSTIC_NONE: 10134064Smarkfen return (dgettext(TEXT_DOMAIN, "no error")); 10140Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNKNOWN_EXT: 10154064Smarkfen return (dgettext(TEXT_DOMAIN, "unknown extension")); 10160Sstevel@tonic-gate case SPD_DIAGNOSTIC_BAD_EXTLEN: 10174064Smarkfen return (dgettext(TEXT_DOMAIN, "bad extension length")); 10180Sstevel@tonic-gate case SPD_DIAGNOSTIC_NO_RULE_EXT: 10194064Smarkfen return (dgettext(TEXT_DOMAIN, "no rule extension")); 10200Sstevel@tonic-gate case SPD_DIAGNOSTIC_BAD_ADDR_LEN: 10214064Smarkfen return (dgettext(TEXT_DOMAIN, "bad address len")); 10220Sstevel@tonic-gate case SPD_DIAGNOSTIC_MIXED_AF: 10234064Smarkfen return (dgettext(TEXT_DOMAIN, "mixed address family")); 10240Sstevel@tonic-gate case SPD_DIAGNOSTIC_ADD_NO_MEM: 10254064Smarkfen return (dgettext(TEXT_DOMAIN, "add: no memory")); 10260Sstevel@tonic-gate case SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT: 10274064Smarkfen return (dgettext(TEXT_DOMAIN, "add: wrong action count")); 10280Sstevel@tonic-gate case SPD_DIAGNOSTIC_ADD_BAD_TYPE: 10294064Smarkfen return (dgettext(TEXT_DOMAIN, "add: bad type")); 10300Sstevel@tonic-gate case SPD_DIAGNOSTIC_ADD_BAD_FLAGS: 10314064Smarkfen return (dgettext(TEXT_DOMAIN, "add: bad flags")); 10320Sstevel@tonic-gate case SPD_DIAGNOSTIC_ADD_INCON_FLAGS: 10334064Smarkfen return (dgettext(TEXT_DOMAIN, "add: inconsistent flags")); 10340Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_LCLPORT: 10354064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed local port")); 10360Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_LCLPORT: 10374064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate local port")); 10380Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_REMPORT: 10394064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed remote port")); 10400Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_REMPORT: 10414064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate remote port")); 10420Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_PROTO: 10434064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed proto")); 10440Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_PROTO: 10454064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate proto")); 10460Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_LCLADDR: 10474064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed local address")); 10480Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_LCLADDR: 10494064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate local address")); 10500Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_REMADDR: 10514064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed remote address")); 10520Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_REMADDR: 10534064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate remote address")); 10540Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_ACTION: 10554064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed action")); 10560Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_ACTION: 10574064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate action")); 10580Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_RULE: 10594064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed rule")); 10600Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_RULE: 10614064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate rule")); 10620Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_RULESET: 10634064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed ruleset")); 10640Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_RULESET: 10654064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate ruleset")); 10660Sstevel@tonic-gate case SPD_DIAGNOSTIC_INVALID_RULE_INDEX: 10674064Smarkfen return (dgettext(TEXT_DOMAIN, "invalid rule index")); 10680Sstevel@tonic-gate case SPD_DIAGNOSTIC_BAD_SPDID: 10694064Smarkfen return (dgettext(TEXT_DOMAIN, "bad spdid")); 10700Sstevel@tonic-gate case SPD_DIAGNOSTIC_BAD_MSG_TYPE: 10714064Smarkfen return (dgettext(TEXT_DOMAIN, "bad message type")); 10720Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_AH_ALG: 10734064Smarkfen return (dgettext(TEXT_DOMAIN, "unsupported AH algorithm")); 10740Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG: 10754064Smarkfen return (dgettext(TEXT_DOMAIN, 10764064Smarkfen "unsupported ESP encryption algorithm")); 10770Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG: 10784064Smarkfen return (dgettext(TEXT_DOMAIN, 10794064Smarkfen "unsupported ESP authentication algorithm")); 10800Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_AH_KEYSIZE: 10814064Smarkfen return (dgettext(TEXT_DOMAIN, "unsupported AH key size")); 10820Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_KEYSIZE: 10834064Smarkfen return (dgettext(TEXT_DOMAIN, 10844064Smarkfen "unsupported ESP encryption key size")); 10850Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_KEYSIZE: 10864064Smarkfen return (dgettext(TEXT_DOMAIN, 10874064Smarkfen "unsupported ESP authentication key size")); 10880Sstevel@tonic-gate case SPD_DIAGNOSTIC_NO_ACTION_EXT: 10894064Smarkfen return (dgettext(TEXT_DOMAIN, "No ACTION extension")); 10900Sstevel@tonic-gate case SPD_DIAGNOSTIC_ALG_ID_RANGE: 10914064Smarkfen return (dgettext(TEXT_DOMAIN, "invalid algorithm identifer")); 10920Sstevel@tonic-gate case SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES: 10934064Smarkfen return (dgettext(TEXT_DOMAIN, 10944064Smarkfen "number of key sizes inconsistent")); 10950Sstevel@tonic-gate case SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES: 10964064Smarkfen return (dgettext(TEXT_DOMAIN, 10974064Smarkfen "number of block sizes inconsistent")); 10980Sstevel@tonic-gate case SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN: 10994064Smarkfen return (dgettext(TEXT_DOMAIN, "invalid mechanism name length")); 11003055Sdanmcd case SPD_DIAGNOSTIC_NOT_GLOBAL_OP: 11014064Smarkfen return (dgettext(TEXT_DOMAIN, 11024064Smarkfen "operation not applicable to all policies")); 11033055Sdanmcd case SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS: 11044064Smarkfen return (dgettext(TEXT_DOMAIN, 11054064Smarkfen "using selectors on a transport-mode tunnel")); 11060Sstevel@tonic-gate default: 11074064Smarkfen return (dgettext(TEXT_DOMAIN, "unknown diagnostic")); 11080Sstevel@tonic-gate } 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate /* 11120Sstevel@tonic-gate * PF_KEY Diagnostic table. 11130Sstevel@tonic-gate * 11140Sstevel@tonic-gate * PF_KEY NOTE: If you change pfkeyv2.h's SADB_X_DIAGNOSTIC_* space, this is 11150Sstevel@tonic-gate * where you need to add new messages. 11160Sstevel@tonic-gate */ 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate const char * 11190Sstevel@tonic-gate keysock_diag(int diagnostic) 11200Sstevel@tonic-gate { 11210Sstevel@tonic-gate switch (diagnostic) { 11223055Sdanmcd case SADB_X_DIAGNOSTIC_NONE: 11234064Smarkfen return (dgettext(TEXT_DOMAIN, "No diagnostic")); 11240Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_UNKNOWN_MSG: 11254064Smarkfen return (dgettext(TEXT_DOMAIN, "Unknown message type")); 11260Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_UNKNOWN_EXT: 11274064Smarkfen return (dgettext(TEXT_DOMAIN, "Unknown extension type")); 11280Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_EXTLEN: 11294064Smarkfen return (dgettext(TEXT_DOMAIN, "Bad extension length")); 11300Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE: 11314064Smarkfen return (dgettext(TEXT_DOMAIN, 11324064Smarkfen "Unknown Security Association type")); 11330Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_SATYPE_NEEDED: 11344064Smarkfen return (dgettext(TEXT_DOMAIN, 11354064Smarkfen "Specific Security Association type needed")); 11360Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_NO_SADBS: 11374064Smarkfen return (dgettext(TEXT_DOMAIN, 11384064Smarkfen "No Security Association Databases present")); 11390Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_NO_EXT: 11404064Smarkfen return (dgettext(TEXT_DOMAIN, 11414064Smarkfen "No extensions needed for message")); 11420Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_SRC_AF: 11434064Smarkfen return (dgettext(TEXT_DOMAIN, "Bad source address family")); 11440Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_DST_AF: 11454064Smarkfen return (dgettext(TEXT_DOMAIN, 11464064Smarkfen "Bad destination address family")); 11470Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_PROXY_AF: 11484064Smarkfen return (dgettext(TEXT_DOMAIN, 11494064Smarkfen "Bad inner-source address family")); 11500Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_AF_MISMATCH: 11514064Smarkfen return (dgettext(TEXT_DOMAIN, 11524064Smarkfen "Source/destination address family mismatch")); 11530Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_SRC: 11544064Smarkfen return (dgettext(TEXT_DOMAIN, "Bad source address value")); 11550Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_DST: 11564064Smarkfen return (dgettext(TEXT_DOMAIN, "Bad destination address value")); 11570Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_ALLOC_HSERR: 11584064Smarkfen return (dgettext(TEXT_DOMAIN, 11594064Smarkfen "Soft allocations limit more than hard limit")); 11600Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BYTES_HSERR: 11614064Smarkfen return (dgettext(TEXT_DOMAIN, 11624064Smarkfen "Soft bytes limit more than hard limit")); 11630Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_ADDTIME_HSERR: 11644064Smarkfen return (dgettext(TEXT_DOMAIN, "Soft add expiration time later " 11650Sstevel@tonic-gate "than hard expiration time")); 11660Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_USETIME_HSERR: 11674064Smarkfen return (dgettext(TEXT_DOMAIN, "Soft use expiration time later " 11680Sstevel@tonic-gate "than hard expiration time")); 11690Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_SRC: 11704064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing source address")); 11710Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_DST: 11724064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing destination address")); 11730Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_SA: 11744064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing SA extension")); 11750Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_EKEY: 11764064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing encryption key")); 11770Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_AKEY: 11784064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing authentication key")); 11790Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_RANGE: 11804064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing SPI range")); 11810Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_SRC: 11824064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate source address")); 11830Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_DST: 11844064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate destination address")); 11850Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_SA: 11864064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate SA extension")); 11870Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_EKEY: 11884064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate encryption key")); 11890Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_AKEY: 11904064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate authentication key")); 11910Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_RANGE: 11924064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate SPI range")); 11930Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_SRC: 11944064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed source address")); 11950Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_DST: 11964064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed destination address")); 11970Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_SA: 11984064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed SA extension")); 11990Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_EKEY: 12004064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed encryption key")); 12010Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_AKEY: 12024064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed authentication key")); 12030Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_RANGE: 12044064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed SPI range")); 12050Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_AKEY_PRESENT: 12064064Smarkfen return (dgettext(TEXT_DOMAIN, "Authentication key not needed")); 12070Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_EKEY_PRESENT: 12084064Smarkfen return (dgettext(TEXT_DOMAIN, "Encryption key not needed")); 12090Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_PROP_PRESENT: 12104064Smarkfen return (dgettext(TEXT_DOMAIN, "Proposal extension not needed")); 12110Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_SUPP_PRESENT: 12124064Smarkfen return (dgettext(TEXT_DOMAIN, 12134064Smarkfen "Supported algorithms extension not needed")); 12140Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_AALG: 12154064Smarkfen return (dgettext(TEXT_DOMAIN, 12164064Smarkfen "Unsupported authentication algorithm")); 12170Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_EALG: 12184064Smarkfen return (dgettext(TEXT_DOMAIN, 12194064Smarkfen "Unsupported encryption algorithm")); 12200Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_SAFLAGS: 12214064Smarkfen return (dgettext(TEXT_DOMAIN, "Invalid SA flags")); 12220Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_SASTATE: 12234064Smarkfen return (dgettext(TEXT_DOMAIN, "Invalid SA state")); 12240Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_AKEYBITS: 12254064Smarkfen return (dgettext(TEXT_DOMAIN, 12264064Smarkfen "Bad number of authentication bits")); 12270Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_EKEYBITS: 12284064Smarkfen return (dgettext(TEXT_DOMAIN, 12294064Smarkfen "Bad number of encryption bits")); 12300Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_ENCR_NOTSUPP: 12314064Smarkfen return (dgettext(TEXT_DOMAIN, 12324064Smarkfen "Encryption not supported for this SA type")); 12330Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_WEAK_EKEY: 12344064Smarkfen return (dgettext(TEXT_DOMAIN, "Weak encryption key")); 12350Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_WEAK_AKEY: 12364064Smarkfen return (dgettext(TEXT_DOMAIN, "Weak authentication key")); 12370Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_KMP: 12384064Smarkfen return (dgettext(TEXT_DOMAIN, 12394064Smarkfen "Duplicate key management protocol")); 12400Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_KMC: 12414064Smarkfen return (dgettext(TEXT_DOMAIN, 12424064Smarkfen "Duplicate key management cookie")); 12430Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_NATT_LOC: 12444064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing NAT-T local address")); 12450Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_NATT_REM: 12464064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing NAT-T remote address")); 12470Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_LOC: 12484064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T local address")); 12490Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_REM: 12504064Smarkfen return (dgettext(TEXT_DOMAIN, 12514064Smarkfen "Duplicate NAT-T remote address")); 12520Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC: 12534064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed NAT-T local address")); 12540Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM: 12554064Smarkfen return (dgettext(TEXT_DOMAIN, 12564064Smarkfen "Malformed NAT-T remote address")); 12570Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_PORTS: 12584064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T ports")); 12593055Sdanmcd case SADB_X_DIAGNOSTIC_MISSING_INNER_SRC: 12604064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing inner source address")); 12613055Sdanmcd case SADB_X_DIAGNOSTIC_MISSING_INNER_DST: 12624064Smarkfen return (dgettext(TEXT_DOMAIN, 12634064Smarkfen "Missing inner destination address")); 12643055Sdanmcd case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC: 12654064Smarkfen return (dgettext(TEXT_DOMAIN, 12664064Smarkfen "Duplicate inner source address")); 12673055Sdanmcd case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST: 12684064Smarkfen return (dgettext(TEXT_DOMAIN, 12694064Smarkfen "Duplicate inner destination address")); 12703055Sdanmcd case SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC: 12714064Smarkfen return (dgettext(TEXT_DOMAIN, 12724064Smarkfen "Malformed inner source address")); 12733055Sdanmcd case SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST: 12744064Smarkfen return (dgettext(TEXT_DOMAIN, 12754064Smarkfen "Malformed inner destination address")); 12763055Sdanmcd case SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC: 12774064Smarkfen return (dgettext(TEXT_DOMAIN, 12784064Smarkfen "Invalid inner-source prefix length ")); 12793055Sdanmcd case SADB_X_DIAGNOSTIC_PREFIX_INNER_DST: 12804064Smarkfen return (dgettext(TEXT_DOMAIN, 12814064Smarkfen "Invalid inner-destination prefix length")); 12823055Sdanmcd case SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF: 12834064Smarkfen return (dgettext(TEXT_DOMAIN, 12844064Smarkfen "Bad inner-destination address family")); 12853055Sdanmcd case SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH: 12864064Smarkfen return (dgettext(TEXT_DOMAIN, 12873055Sdanmcd "Inner source/destination address family mismatch")); 12883055Sdanmcd case SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF: 12894064Smarkfen return (dgettext(TEXT_DOMAIN, 12904064Smarkfen "Bad NAT-T remote address family")); 12913055Sdanmcd case SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF: 12924064Smarkfen return (dgettext(TEXT_DOMAIN, 12934064Smarkfen "Bad NAT-T local address family")); 12943055Sdanmcd case SADB_X_DIAGNOSTIC_PROTO_MISMATCH: 12954064Smarkfen return (dgettext(TEXT_DOMAIN, 12964064Smarkfen "Source/desination protocol mismatch")); 12973055Sdanmcd case SADB_X_DIAGNOSTIC_INNER_PROTO_MISMATCH: 12984064Smarkfen return (dgettext(TEXT_DOMAIN, 12994064Smarkfen "Inner source/desination protocol mismatch")); 13003055Sdanmcd case SADB_X_DIAGNOSTIC_DUAL_PORT_SETS: 13014064Smarkfen return (dgettext(TEXT_DOMAIN, 13024064Smarkfen "Both inner ports and outer ports are set")); 13030Sstevel@tonic-gate default: 13044064Smarkfen return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code")); 13050Sstevel@tonic-gate } 13060Sstevel@tonic-gate } 13073055Sdanmcd 13083055Sdanmcd /* 13093055Sdanmcd * Convert an IPv6 mask to a prefix len. I assume all IPv6 masks are 13103055Sdanmcd * contiguous, so I stop at the first zero bit! 13113055Sdanmcd */ 13123055Sdanmcd int 13133055Sdanmcd in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped) 13143055Sdanmcd { 13153055Sdanmcd int rc = 0; 13163055Sdanmcd uint8_t last; 13173055Sdanmcd int limit = IPV6_ABITS; 13183055Sdanmcd 13193055Sdanmcd if (is_v4mapped) { 13203055Sdanmcd mask += ((IPV6_ABITS - IP_ABITS)/8); 13213055Sdanmcd limit = IP_ABITS; 13223055Sdanmcd } 13233055Sdanmcd 13243055Sdanmcd while (*mask == 0xff) { 13253055Sdanmcd rc += 8; 13263055Sdanmcd if (rc == limit) 13273055Sdanmcd return (limit); 13283055Sdanmcd mask++; 13293055Sdanmcd } 13303055Sdanmcd 13313055Sdanmcd last = *mask; 13323055Sdanmcd while (last != 0) { 13333055Sdanmcd rc++; 13343055Sdanmcd last = (last << 1) & 0xff; 13353055Sdanmcd } 13363055Sdanmcd 13373055Sdanmcd return (rc); 13383055Sdanmcd } 13393055Sdanmcd 13403055Sdanmcd /* 13413055Sdanmcd * Expand the diagnostic code into a message. 13423055Sdanmcd */ 13433055Sdanmcd void 13443055Sdanmcd print_diagnostic(FILE *file, uint16_t diagnostic) 13453055Sdanmcd { 13463055Sdanmcd /* Use two spaces so above strings can fit on the line. */ 13474064Smarkfen (void) fprintf(file, dgettext(TEXT_DOMAIN, 13484064Smarkfen " Diagnostic code %u: %s.\n"), 13493055Sdanmcd diagnostic, keysock_diag(diagnostic)); 13503055Sdanmcd } 13513055Sdanmcd 13523055Sdanmcd /* 13533055Sdanmcd * Prints the base PF_KEY message. 13543055Sdanmcd */ 13553055Sdanmcd void 13564867Spwernau print_sadb_msg(FILE *file, struct sadb_msg *samsg, time_t wallclock, 13574867Spwernau boolean_t vflag) 13583055Sdanmcd { 13593055Sdanmcd if (wallclock != 0) 13604867Spwernau printsatime(file, wallclock, dgettext(TEXT_DOMAIN, 13614064Smarkfen "%sTimestamp: %s\n"), "", NULL, 13623055Sdanmcd vflag); 13633055Sdanmcd 13644867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 13654867Spwernau "Base message (version %u) type "), 13663055Sdanmcd samsg->sadb_msg_version); 13673055Sdanmcd switch (samsg->sadb_msg_type) { 13683055Sdanmcd case SADB_RESERVED: 13694867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 13704064Smarkfen "RESERVED (warning: set to 0)")); 13713055Sdanmcd break; 13723055Sdanmcd case SADB_GETSPI: 13734867Spwernau (void) fprintf(file, "GETSPI"); 13743055Sdanmcd break; 13753055Sdanmcd case SADB_UPDATE: 13764867Spwernau (void) fprintf(file, "UPDATE"); 13773055Sdanmcd break; 13783055Sdanmcd case SADB_ADD: 13794867Spwernau (void) fprintf(file, "ADD"); 13803055Sdanmcd break; 13813055Sdanmcd case SADB_DELETE: 13824867Spwernau (void) fprintf(file, "DELETE"); 13833055Sdanmcd break; 13843055Sdanmcd case SADB_GET: 13854867Spwernau (void) fprintf(file, "GET"); 13863055Sdanmcd break; 13873055Sdanmcd case SADB_ACQUIRE: 13884867Spwernau (void) fprintf(file, "ACQUIRE"); 13893055Sdanmcd break; 13903055Sdanmcd case SADB_REGISTER: 13914867Spwernau (void) fprintf(file, "REGISTER"); 13923055Sdanmcd break; 13933055Sdanmcd case SADB_EXPIRE: 13944867Spwernau (void) fprintf(file, "EXPIRE"); 13953055Sdanmcd break; 13963055Sdanmcd case SADB_FLUSH: 13974867Spwernau (void) fprintf(file, "FLUSH"); 13983055Sdanmcd break; 13993055Sdanmcd case SADB_DUMP: 14004867Spwernau (void) fprintf(file, "DUMP"); 14013055Sdanmcd break; 14023055Sdanmcd case SADB_X_PROMISC: 14034867Spwernau (void) fprintf(file, "X_PROMISC"); 14043055Sdanmcd break; 14053055Sdanmcd case SADB_X_INVERSE_ACQUIRE: 14064867Spwernau (void) fprintf(file, "X_INVERSE_ACQUIRE"); 14073055Sdanmcd break; 14083055Sdanmcd default: 14094867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14104064Smarkfen "Unknown (%u)"), samsg->sadb_msg_type); 14113055Sdanmcd break; 14123055Sdanmcd } 14134867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, ", SA type ")); 14143055Sdanmcd 14153055Sdanmcd switch (samsg->sadb_msg_satype) { 14163055Sdanmcd case SADB_SATYPE_UNSPEC: 14174867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14184867Spwernau "<unspecified/all>")); 14193055Sdanmcd break; 14203055Sdanmcd case SADB_SATYPE_AH: 14214867Spwernau (void) fprintf(file, "AH"); 14223055Sdanmcd break; 14233055Sdanmcd case SADB_SATYPE_ESP: 14244867Spwernau (void) fprintf(file, "ESP"); 14253055Sdanmcd break; 14263055Sdanmcd case SADB_SATYPE_RSVP: 14274867Spwernau (void) fprintf(file, "RSVP"); 14283055Sdanmcd break; 14293055Sdanmcd case SADB_SATYPE_OSPFV2: 14304867Spwernau (void) fprintf(file, "OSPFv2"); 14313055Sdanmcd break; 14323055Sdanmcd case SADB_SATYPE_RIPV2: 14334867Spwernau (void) fprintf(file, "RIPv2"); 14343055Sdanmcd break; 14353055Sdanmcd case SADB_SATYPE_MIP: 14364867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Mobile IP")); 14373055Sdanmcd break; 14383055Sdanmcd default: 14394867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14404064Smarkfen "<unknown %u>"), samsg->sadb_msg_satype); 14413055Sdanmcd break; 14423055Sdanmcd } 14433055Sdanmcd 14444867Spwernau (void) fprintf(file, ".\n"); 14453055Sdanmcd 14463055Sdanmcd if (samsg->sadb_msg_errno != 0) { 14474867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14484867Spwernau "Error %s from PF_KEY.\n"), 14493055Sdanmcd strerror(samsg->sadb_msg_errno)); 14504867Spwernau print_diagnostic(file, samsg->sadb_x_msg_diagnostic); 14513055Sdanmcd } 14523055Sdanmcd 14534867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14544064Smarkfen "Message length %u bytes, seq=%u, pid=%u.\n"), 14553055Sdanmcd SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq, 14563055Sdanmcd samsg->sadb_msg_pid); 14573055Sdanmcd } 14583055Sdanmcd 14593055Sdanmcd /* 14603055Sdanmcd * Print the SA extension for PF_KEY. 14613055Sdanmcd */ 14623055Sdanmcd void 14634867Spwernau print_sa(FILE *file, char *prefix, struct sadb_sa *assoc) 14643055Sdanmcd { 14653055Sdanmcd if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) { 14664867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 14674064Smarkfen "WARNING: SA info extension length (%u) is bad."), 14683055Sdanmcd SADB_64TO8(assoc->sadb_sa_len)); 14693055Sdanmcd } 14703055Sdanmcd 14714867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14724064Smarkfen "%sSADB_ASSOC spi=0x%x, replay=%u, state="), 14733055Sdanmcd prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay); 14743055Sdanmcd switch (assoc->sadb_sa_state) { 14753055Sdanmcd case SADB_SASTATE_LARVAL: 14764867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "LARVAL")); 14773055Sdanmcd break; 14783055Sdanmcd case SADB_SASTATE_MATURE: 14794867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "MATURE")); 14803055Sdanmcd break; 14813055Sdanmcd case SADB_SASTATE_DYING: 14824867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "DYING")); 14833055Sdanmcd break; 14843055Sdanmcd case SADB_SASTATE_DEAD: 14854867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "DEAD")); 14863055Sdanmcd break; 14873055Sdanmcd default: 14884867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14894064Smarkfen "<unknown %u>"), assoc->sadb_sa_state); 14903055Sdanmcd } 14913055Sdanmcd 14923055Sdanmcd if (assoc->sadb_sa_auth != SADB_AALG_NONE) { 14934867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14944064Smarkfen "\n%sAuthentication algorithm = "), 14953055Sdanmcd prefix); 14964867Spwernau (void) dump_aalg(assoc->sadb_sa_auth, file); 14973055Sdanmcd } 14983055Sdanmcd 14993055Sdanmcd if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) { 15004867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 15014064Smarkfen "\n%sEncryption algorithm = "), prefix); 15024867Spwernau (void) dump_ealg(assoc->sadb_sa_encrypt, file); 15033055Sdanmcd } 15043055Sdanmcd 15054867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix, 15063055Sdanmcd assoc->sadb_sa_flags); 15073055Sdanmcd if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS) 15084867Spwernau (void) fprintf(file, "PFS "); 15093055Sdanmcd if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY) 15104867Spwernau (void) fprintf(file, "NOREPLAY "); 15113055Sdanmcd 15123055Sdanmcd /* BEGIN Solaris-specific flags. */ 15133055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED) 15144867Spwernau (void) fprintf(file, "X_USED "); 15153055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE) 15164867Spwernau (void) fprintf(file, "X_UNIQUE "); 15173055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1) 15184867Spwernau (void) fprintf(file, "X_AALG1 "); 15193055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2) 15204867Spwernau (void) fprintf(file, "X_AALG2 "); 15213055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1) 15224867Spwernau (void) fprintf(file, "X_EALG1 "); 15233055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2) 15244867Spwernau (void) fprintf(file, "X_EALG2 "); 15253055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC) 15264867Spwernau (void) fprintf(file, "X_NATT_LOC "); 15273055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM) 15284867Spwernau (void) fprintf(file, "X_NATT_REM "); 15293055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL) 15304867Spwernau (void) fprintf(file, "X_TUNNEL "); 15313055Sdanmcd /* END Solaris-specific flags. */ 15323055Sdanmcd 15334867Spwernau (void) fprintf(file, ">\n"); 15343055Sdanmcd } 15353055Sdanmcd 15363055Sdanmcd void 15374867Spwernau printsatime(FILE *file, int64_t lt, const char *msg, const char *pfx, 15384867Spwernau const char *pfx2, boolean_t vflag) 15393055Sdanmcd { 15403055Sdanmcd char tbuf[TBUF_SIZE]; /* For strftime() call. */ 15413055Sdanmcd const char *tp = tbuf; 15423055Sdanmcd time_t t = lt; 15433055Sdanmcd struct tm res; 15443055Sdanmcd 15453055Sdanmcd if (t != lt) { 15463055Sdanmcd if (lt > 0) 15473055Sdanmcd t = LONG_MAX; 15483055Sdanmcd else 15493055Sdanmcd t = LONG_MIN; 15503055Sdanmcd } 15513055Sdanmcd 15523055Sdanmcd if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0) 15534064Smarkfen tp = dgettext(TEXT_DOMAIN, "<time conversion failed>"); 15544867Spwernau (void) fprintf(file, msg, pfx, tp); 15553055Sdanmcd if (vflag && (pfx2 != NULL)) 15564867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 15574064Smarkfen "%s\t(raw time value %llu)\n"), pfx2, lt); 15583055Sdanmcd } 15593055Sdanmcd 15603055Sdanmcd /* 15613055Sdanmcd * Print the SA lifetime information. (An SADB_EXT_LIFETIME_* extension.) 15623055Sdanmcd */ 15633055Sdanmcd void 15644867Spwernau print_lifetimes(FILE *file, time_t wallclock, struct sadb_lifetime *current, 15653055Sdanmcd struct sadb_lifetime *hard, struct sadb_lifetime *soft, boolean_t vflag) 15663055Sdanmcd { 15673055Sdanmcd int64_t scratch; 15684064Smarkfen char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: "); 15694064Smarkfen char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: "); 15704064Smarkfen char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: "); 15713055Sdanmcd 15723055Sdanmcd if (current != NULL && 15733055Sdanmcd current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) { 15744867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 15754064Smarkfen "WARNING: CURRENT lifetime extension length (%u) is bad."), 15763055Sdanmcd SADB_64TO8(current->sadb_lifetime_len)); 15773055Sdanmcd } 15783055Sdanmcd 15793055Sdanmcd if (hard != NULL && 15803055Sdanmcd hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) { 15814867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 15824867Spwernau "WARNING: HARD lifetime extension length (%u) is bad."), 15833055Sdanmcd SADB_64TO8(hard->sadb_lifetime_len)); 15843055Sdanmcd } 15853055Sdanmcd 15863055Sdanmcd if (soft != NULL && 15873055Sdanmcd soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) { 15884867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 15894867Spwernau "WARNING: SOFT lifetime extension length (%u) is bad."), 15903055Sdanmcd SADB_64TO8(soft->sadb_lifetime_len)); 15913055Sdanmcd } 15923055Sdanmcd 15934867Spwernau (void) fprintf(file, " LT: Lifetime information\n"); 15943055Sdanmcd 15953055Sdanmcd if (current != NULL) { 15963055Sdanmcd /* Express values as current values. */ 15974867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 15983055Sdanmcd "%s%llu bytes protected, %u allocations used.\n"), 15993055Sdanmcd current_prefix, current->sadb_lifetime_bytes, 16003055Sdanmcd current->sadb_lifetime_allocations); 16014867Spwernau printsatime(file, current->sadb_lifetime_addtime, 16024064Smarkfen dgettext(TEXT_DOMAIN, "%sSA added at time %s\n"), 16033055Sdanmcd current_prefix, current_prefix, vflag); 16043055Sdanmcd if (current->sadb_lifetime_usetime != 0) { 16054867Spwernau printsatime(file, current->sadb_lifetime_usetime, 16064064Smarkfen dgettext(TEXT_DOMAIN, 16074064Smarkfen "%sSA first used at time %s\n"), 16083055Sdanmcd current_prefix, current_prefix, vflag); 16093055Sdanmcd } 16104867Spwernau printsatime(file, wallclock, dgettext(TEXT_DOMAIN, 16114064Smarkfen "%sTime now is %s\n"), current_prefix, current_prefix, 16124064Smarkfen vflag); 16133055Sdanmcd } 16143055Sdanmcd 16153055Sdanmcd if (soft != NULL) { 16164867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16174064Smarkfen "%sSoft lifetime information: "), 16183055Sdanmcd soft_prefix); 16194867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16204064Smarkfen "%llu bytes of lifetime, %u " 16213055Sdanmcd "allocations.\n"), soft->sadb_lifetime_bytes, 16223055Sdanmcd soft->sadb_lifetime_allocations); 16234867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16244064Smarkfen "%s%llu seconds of post-add lifetime.\n"), 16253055Sdanmcd soft_prefix, soft->sadb_lifetime_addtime); 16264867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16274064Smarkfen "%s%llu seconds of post-use lifetime.\n"), 16283055Sdanmcd soft_prefix, soft->sadb_lifetime_usetime); 16293055Sdanmcd /* If possible, express values as time remaining. */ 16303055Sdanmcd if (current != NULL) { 16313055Sdanmcd if (soft->sadb_lifetime_bytes != 0) 16324867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16333055Sdanmcd "%s%llu more bytes can be protected.\n"), 16343055Sdanmcd soft_prefix, 16353055Sdanmcd (soft->sadb_lifetime_bytes > 16364342Spwernau current->sadb_lifetime_bytes) ? 16373055Sdanmcd (soft->sadb_lifetime_bytes - 16384342Spwernau current->sadb_lifetime_bytes) : (0)); 16393055Sdanmcd if (soft->sadb_lifetime_addtime != 0 || 16403055Sdanmcd (soft->sadb_lifetime_usetime != 0 && 16414342Spwernau current->sadb_lifetime_usetime != 0)) { 16423055Sdanmcd int64_t adddelta, usedelta; 16433055Sdanmcd 16443055Sdanmcd if (soft->sadb_lifetime_addtime != 0) { 16453055Sdanmcd adddelta = 16463055Sdanmcd current->sadb_lifetime_addtime + 16473055Sdanmcd soft->sadb_lifetime_addtime - 16483055Sdanmcd wallclock; 16493055Sdanmcd } else { 16503055Sdanmcd adddelta = TIME_MAX; 16513055Sdanmcd } 16523055Sdanmcd 16533055Sdanmcd if (soft->sadb_lifetime_usetime != 0 && 16543055Sdanmcd current->sadb_lifetime_usetime != 0) { 16553055Sdanmcd usedelta = 16563055Sdanmcd current->sadb_lifetime_usetime + 16573055Sdanmcd soft->sadb_lifetime_usetime - 16583055Sdanmcd wallclock; 16593055Sdanmcd } else { 16603055Sdanmcd usedelta = TIME_MAX; 16613055Sdanmcd } 16624867Spwernau (void) fprintf(file, "%s", soft_prefix); 16633055Sdanmcd scratch = MIN(adddelta, usedelta); 16643055Sdanmcd if (scratch >= 0) { 16654867Spwernau (void) fprintf(file, 16664867Spwernau dgettext(TEXT_DOMAIN, 16674064Smarkfen "Soft expiration occurs in %lld " 16684064Smarkfen "seconds, "), scratch); 16693055Sdanmcd } else { 16704867Spwernau (void) fprintf(file, 16714867Spwernau dgettext(TEXT_DOMAIN, 16723055Sdanmcd "Soft expiration occurred ")); 16733055Sdanmcd } 16743055Sdanmcd scratch += wallclock; 16754867Spwernau printsatime(file, scratch, dgettext(TEXT_DOMAIN, 16764064Smarkfen "%sat %s.\n"), "", soft_prefix, vflag); 16773055Sdanmcd } 16783055Sdanmcd } 16793055Sdanmcd } 16803055Sdanmcd 16813055Sdanmcd if (hard != NULL) { 16824867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16834064Smarkfen "%sHard lifetime information: "), hard_prefix); 16844867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16854867Spwernau "%llu bytes of lifetime, %u allocations.\n"), 16864867Spwernau hard->sadb_lifetime_bytes, hard->sadb_lifetime_allocations); 16874867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16884064Smarkfen "%s%llu seconds of post-add lifetime.\n"), 16893055Sdanmcd hard_prefix, hard->sadb_lifetime_addtime); 16904867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16914064Smarkfen "%s%llu seconds of post-use lifetime.\n"), 16923055Sdanmcd hard_prefix, hard->sadb_lifetime_usetime); 16933055Sdanmcd /* If possible, express values as time remaining. */ 16943055Sdanmcd if (current != NULL) { 16953055Sdanmcd if (hard->sadb_lifetime_bytes != 0) 16964867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16973055Sdanmcd "%s%llu more bytes can be protected.\n"), 16983055Sdanmcd hard_prefix, 16993055Sdanmcd (hard->sadb_lifetime_bytes > 17004342Spwernau current->sadb_lifetime_bytes) ? 17013055Sdanmcd (hard->sadb_lifetime_bytes - 17024342Spwernau current->sadb_lifetime_bytes) : (0)); 17033055Sdanmcd if (hard->sadb_lifetime_addtime != 0 || 17043055Sdanmcd (hard->sadb_lifetime_usetime != 0 && 17054342Spwernau current->sadb_lifetime_usetime != 0)) { 17063055Sdanmcd int64_t adddelta, usedelta; 17073055Sdanmcd 17083055Sdanmcd if (hard->sadb_lifetime_addtime != 0) { 17093055Sdanmcd adddelta = 17103055Sdanmcd current->sadb_lifetime_addtime + 17113055Sdanmcd hard->sadb_lifetime_addtime - 17123055Sdanmcd wallclock; 17133055Sdanmcd } else { 17143055Sdanmcd adddelta = TIME_MAX; 17153055Sdanmcd } 17163055Sdanmcd 17173055Sdanmcd if (hard->sadb_lifetime_usetime != 0 && 17183055Sdanmcd current->sadb_lifetime_usetime != 0) { 17193055Sdanmcd usedelta = 17203055Sdanmcd current->sadb_lifetime_usetime + 17213055Sdanmcd hard->sadb_lifetime_usetime - 17223055Sdanmcd wallclock; 17233055Sdanmcd } else { 17243055Sdanmcd usedelta = TIME_MAX; 17253055Sdanmcd } 17264867Spwernau (void) fprintf(file, "%s", hard_prefix); 17273055Sdanmcd scratch = MIN(adddelta, usedelta); 17283055Sdanmcd if (scratch >= 0) { 17294867Spwernau (void) fprintf(file, 17304867Spwernau dgettext(TEXT_DOMAIN, 17314064Smarkfen "Hard expiration occurs in %lld " 17324064Smarkfen "seconds, "), scratch); 17333055Sdanmcd } else { 17344867Spwernau (void) fprintf(file, 17354867Spwernau dgettext(TEXT_DOMAIN, 17363055Sdanmcd "Hard expiration occured ")); 17373055Sdanmcd } 17383055Sdanmcd scratch += wallclock; 17394867Spwernau printsatime(file, scratch, dgettext(TEXT_DOMAIN, 17404064Smarkfen "%sat %s.\n"), "", hard_prefix, vflag); 17413055Sdanmcd } 17423055Sdanmcd } 17433055Sdanmcd } 17443055Sdanmcd } 17453055Sdanmcd 17463055Sdanmcd /* 17473055Sdanmcd * Print an SADB_EXT_ADDRESS_* extension. 17483055Sdanmcd */ 17493055Sdanmcd void 17504867Spwernau print_address(FILE *file, char *prefix, struct sadb_address *addr, 17514867Spwernau boolean_t ignore_nss) 17523055Sdanmcd { 17533055Sdanmcd struct protoent *pe; 17543055Sdanmcd 17554867Spwernau (void) fprintf(file, "%s", prefix); 17563055Sdanmcd switch (addr->sadb_address_exttype) { 17573055Sdanmcd case SADB_EXT_ADDRESS_SRC: 17584867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Source address ")); 17593055Sdanmcd break; 17603055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 17614867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17624867Spwernau "Inner source address ")); 17633055Sdanmcd break; 17643055Sdanmcd case SADB_EXT_ADDRESS_DST: 17654867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17664867Spwernau "Destination address ")); 17673055Sdanmcd break; 17683055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 17694867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17704064Smarkfen "Inner destination address ")); 17713055Sdanmcd break; 17723055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 17734867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17744867Spwernau "NAT-T local address ")); 17753055Sdanmcd break; 17763055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 17774867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17784867Spwernau "NAT-T remote address ")); 17793055Sdanmcd break; 17803055Sdanmcd } 17813055Sdanmcd 17824867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17834064Smarkfen "(proto=%d"), addr->sadb_address_proto); 17844867Spwernau if (ignore_nss == B_FALSE) { 17853055Sdanmcd if (addr->sadb_address_proto == 0) { 17864867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17874867Spwernau "/<unspecified>")); 17883055Sdanmcd } else if ((pe = getprotobynumber(addr->sadb_address_proto)) 17893055Sdanmcd != NULL) { 17904867Spwernau (void) fprintf(file, "/%s", pe->p_name); 17913055Sdanmcd } else { 17924867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17934867Spwernau "/<unknown>")); 17943055Sdanmcd } 17953055Sdanmcd } 17964867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, ")\n%s"), prefix); 17973055Sdanmcd (void) dump_sockaddr((struct sockaddr *)(addr + 1), 17984867Spwernau addr->sadb_address_prefixlen, B_FALSE, file, ignore_nss); 17993055Sdanmcd } 18003055Sdanmcd 18013055Sdanmcd /* 18023055Sdanmcd * Print an SADB_EXT_KEY extension. 18033055Sdanmcd */ 18043055Sdanmcd void 18054867Spwernau print_key(FILE *file, char *prefix, struct sadb_key *key) 18063055Sdanmcd { 18074867Spwernau (void) fprintf(file, "%s", prefix); 18083055Sdanmcd 18093055Sdanmcd switch (key->sadb_key_exttype) { 18103055Sdanmcd case SADB_EXT_KEY_AUTH: 18114867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Authentication")); 18123055Sdanmcd break; 18133055Sdanmcd case SADB_EXT_KEY_ENCRYPT: 18144867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Encryption")); 18153055Sdanmcd break; 18163055Sdanmcd } 18173055Sdanmcd 18184867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, " key.\n%s"), prefix); 18194867Spwernau (void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits, file); 18204867Spwernau (void) fprintf(file, "\n"); 18213055Sdanmcd } 18223055Sdanmcd 18233055Sdanmcd /* 18243055Sdanmcd * Print an SADB_EXT_IDENTITY_* extension. 18253055Sdanmcd */ 18263055Sdanmcd void 18274867Spwernau print_ident(FILE *file, char *prefix, struct sadb_ident *id) 18283055Sdanmcd { 18293055Sdanmcd boolean_t canprint = B_TRUE; 18303055Sdanmcd 18314867Spwernau (void) fprintf(file, "%s", prefix); 18323055Sdanmcd switch (id->sadb_ident_exttype) { 18333055Sdanmcd case SADB_EXT_IDENTITY_SRC: 18344867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Source")); 18353055Sdanmcd break; 18363055Sdanmcd case SADB_EXT_IDENTITY_DST: 18374867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Destination")); 18383055Sdanmcd break; 18393055Sdanmcd } 18403055Sdanmcd 18414867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18424064Smarkfen " identity, uid=%d, type "), id->sadb_ident_id); 18434867Spwernau canprint = dump_sadb_idtype(id->sadb_ident_type, file, NULL); 18444867Spwernau (void) fprintf(file, "\n%s", prefix); 1845*6119Spwernau if (canprint) { 18464867Spwernau (void) fprintf(file, "%s\n", (char *)(id + 1)); 1847*6119Spwernau } else { 1848*6119Spwernau print_asn1_name(file, (const unsigned char *)(id + 1), 1849*6119Spwernau SADB_64TO8(id->sadb_ident_len) - sizeof (sadb_ident_t)); 1850*6119Spwernau } 18513055Sdanmcd } 18523055Sdanmcd 18533055Sdanmcd /* 18543055Sdanmcd * Print an SADB_SENSITIVITY extension. 18553055Sdanmcd */ 18563055Sdanmcd void 18574867Spwernau print_sens(FILE *file, char *prefix, struct sadb_sens *sens) 18583055Sdanmcd { 18593055Sdanmcd uint64_t *bitmap = (uint64_t *)(sens + 1); 18603055Sdanmcd int i; 18613055Sdanmcd 18624867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18634064Smarkfen "%sSensitivity DPD %d, sens level=%d, integ level=%d\n"), 18643055Sdanmcd prefix, sens->sadb_sens_dpd, sens->sadb_sens_sens_level, 18653055Sdanmcd sens->sadb_sens_integ_level); 18663055Sdanmcd for (i = 0; sens->sadb_sens_sens_len-- > 0; i++, bitmap++) 18674867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18684867Spwernau "%s Sensitivity BM extended word %d 0x%llx\n"), 18694867Spwernau prefix, i, *bitmap); 18703055Sdanmcd for (i = 0; sens->sadb_sens_integ_len-- > 0; i++, bitmap++) 18714867Spwernau (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 18724867Spwernau "%s Integrity BM extended word %d 0x%llx\n"), 18734867Spwernau prefix, i, *bitmap); 18743055Sdanmcd } 18753055Sdanmcd 18763055Sdanmcd /* 18773055Sdanmcd * Print an SADB_EXT_PROPOSAL extension. 18783055Sdanmcd */ 18793055Sdanmcd void 18804867Spwernau print_prop(FILE *file, char *prefix, struct sadb_prop *prop) 18813055Sdanmcd { 18823055Sdanmcd struct sadb_comb *combs; 18833055Sdanmcd int i, numcombs; 18843055Sdanmcd 18854867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18864064Smarkfen "%sProposal, replay counter = %u.\n"), prefix, 18873055Sdanmcd prop->sadb_prop_replay); 18883055Sdanmcd 18893055Sdanmcd numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop)); 18903055Sdanmcd numcombs /= SADB_8TO64(sizeof (*combs)); 18913055Sdanmcd 18923055Sdanmcd combs = (struct sadb_comb *)(prop + 1); 18933055Sdanmcd 18943055Sdanmcd for (i = 0; i < numcombs; i++) { 18954867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18964064Smarkfen "%s Combination #%u "), prefix, i + 1); 18973055Sdanmcd if (combs[i].sadb_comb_auth != SADB_AALG_NONE) { 18984867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18994064Smarkfen "Authentication = ")); 19004867Spwernau (void) dump_aalg(combs[i].sadb_comb_auth, file); 19014867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19024064Smarkfen " minbits=%u, maxbits=%u.\n%s "), 19033055Sdanmcd combs[i].sadb_comb_auth_minbits, 19043055Sdanmcd combs[i].sadb_comb_auth_maxbits, prefix); 19053055Sdanmcd } 19063055Sdanmcd 19073055Sdanmcd if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) { 19084867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19094867Spwernau "Encryption = ")); 19104867Spwernau (void) dump_ealg(combs[i].sadb_comb_encrypt, file); 19114867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19124064Smarkfen " minbits=%u, maxbits=%u.\n%s "), 19133055Sdanmcd combs[i].sadb_comb_encrypt_minbits, 19143055Sdanmcd combs[i].sadb_comb_encrypt_maxbits, prefix); 19153055Sdanmcd } 19163055Sdanmcd 19174867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "HARD: ")); 19183055Sdanmcd if (combs[i].sadb_comb_hard_allocations) 19194867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "), 19203055Sdanmcd combs[i].sadb_comb_hard_allocations); 19213055Sdanmcd if (combs[i].sadb_comb_hard_bytes) 19224867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19234867Spwernau "bytes=%llu "), combs[i].sadb_comb_hard_bytes); 19243055Sdanmcd if (combs[i].sadb_comb_hard_addtime) 19254867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19264064Smarkfen "post-add secs=%llu "), 19273055Sdanmcd combs[i].sadb_comb_hard_addtime); 19283055Sdanmcd if (combs[i].sadb_comb_hard_usetime) 19294867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19304064Smarkfen "post-use secs=%llu"), 19313055Sdanmcd combs[i].sadb_comb_hard_usetime); 19323055Sdanmcd 19334867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%s SOFT: "), 19344867Spwernau prefix); 19353055Sdanmcd if (combs[i].sadb_comb_soft_allocations) 19364867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "), 19373055Sdanmcd combs[i].sadb_comb_soft_allocations); 19383055Sdanmcd if (combs[i].sadb_comb_soft_bytes) 19394867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19404867Spwernau "bytes=%llu "), combs[i].sadb_comb_soft_bytes); 19413055Sdanmcd if (combs[i].sadb_comb_soft_addtime) 19424867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19434064Smarkfen "post-add secs=%llu "), 19443055Sdanmcd combs[i].sadb_comb_soft_addtime); 19453055Sdanmcd if (combs[i].sadb_comb_soft_usetime) 19464867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19474064Smarkfen "post-use secs=%llu"), 19483055Sdanmcd combs[i].sadb_comb_soft_usetime); 19494867Spwernau (void) fprintf(file, "\n"); 19503055Sdanmcd } 19513055Sdanmcd } 19523055Sdanmcd 19533055Sdanmcd /* 19543055Sdanmcd * Print an extended proposal (SADB_X_EXT_EPROP). 19553055Sdanmcd */ 19563055Sdanmcd void 19574867Spwernau print_eprop(FILE *file, char *prefix, struct sadb_prop *eprop) 19583055Sdanmcd { 19593055Sdanmcd uint64_t *sofar; 19603055Sdanmcd struct sadb_x_ecomb *ecomb; 19613055Sdanmcd struct sadb_x_algdesc *algdesc; 19623055Sdanmcd int i, j; 19633055Sdanmcd 19644867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19654064Smarkfen "%sExtended Proposal, replay counter = %u, "), prefix, 19664064Smarkfen eprop->sadb_prop_replay); 19674867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19684867Spwernau "number of combinations = %u.\n"), eprop->sadb_x_prop_numecombs); 19693055Sdanmcd 19703055Sdanmcd sofar = (uint64_t *)(eprop + 1); 19713055Sdanmcd ecomb = (struct sadb_x_ecomb *)sofar; 19723055Sdanmcd 19733055Sdanmcd for (i = 0; i < eprop->sadb_x_prop_numecombs; ) { 19744867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19754064Smarkfen "%s Extended combination #%u:\n"), prefix, ++i); 19763055Sdanmcd 19774867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "%s HARD: "), 19784867Spwernau prefix); 19794867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "), 19803055Sdanmcd ecomb->sadb_x_ecomb_hard_allocations); 19814867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%llu, "), 19823055Sdanmcd ecomb->sadb_x_ecomb_hard_bytes); 19834867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19844867Spwernau "post-add secs=%llu, "), ecomb->sadb_x_ecomb_hard_addtime); 19854867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19864867Spwernau "post-use secs=%llu\n"), ecomb->sadb_x_ecomb_hard_usetime); 19873055Sdanmcd 19884867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "%s SOFT: "), 19894867Spwernau prefix); 19904867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "), 19913055Sdanmcd ecomb->sadb_x_ecomb_soft_allocations); 19924867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%llu, "), 19933055Sdanmcd ecomb->sadb_x_ecomb_soft_bytes); 19944867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19954867Spwernau "post-add secs=%llu, "), 19963055Sdanmcd ecomb->sadb_x_ecomb_soft_addtime); 19974867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19984867Spwernau "post-use secs=%llu\n"), ecomb->sadb_x_ecomb_soft_usetime); 19993055Sdanmcd 20003055Sdanmcd sofar = (uint64_t *)(ecomb + 1); 20013055Sdanmcd algdesc = (struct sadb_x_algdesc *)sofar; 20023055Sdanmcd 20033055Sdanmcd for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) { 20044867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20054064Smarkfen "%s Alg #%u "), prefix, ++j); 20063055Sdanmcd switch (algdesc->sadb_x_algdesc_satype) { 20073055Sdanmcd case SADB_SATYPE_ESP: 20084867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20094064Smarkfen "for ESP ")); 20103055Sdanmcd break; 20113055Sdanmcd case SADB_SATYPE_AH: 20124867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20134867Spwernau "for AH ")); 20143055Sdanmcd break; 20153055Sdanmcd default: 20164867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20174064Smarkfen "for satype=%d "), 20183055Sdanmcd algdesc->sadb_x_algdesc_satype); 20193055Sdanmcd } 20203055Sdanmcd switch (algdesc->sadb_x_algdesc_algtype) { 20213055Sdanmcd case SADB_X_ALGTYPE_CRYPT: 20224867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20234064Smarkfen "Encryption = ")); 20243055Sdanmcd (void) dump_ealg(algdesc->sadb_x_algdesc_alg, 20254867Spwernau file); 20263055Sdanmcd break; 20273055Sdanmcd case SADB_X_ALGTYPE_AUTH: 20284867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20294064Smarkfen "Authentication = ")); 20303055Sdanmcd (void) dump_aalg(algdesc->sadb_x_algdesc_alg, 20314867Spwernau file); 20323055Sdanmcd break; 20333055Sdanmcd default: 20344867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20354064Smarkfen "algtype(%d) = alg(%d)"), 20363055Sdanmcd algdesc->sadb_x_algdesc_algtype, 20373055Sdanmcd algdesc->sadb_x_algdesc_alg); 20383055Sdanmcd break; 20393055Sdanmcd } 20403055Sdanmcd 20414867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20424064Smarkfen " minbits=%u, maxbits=%u.\n"), 20433055Sdanmcd algdesc->sadb_x_algdesc_minbits, 20443055Sdanmcd algdesc->sadb_x_algdesc_maxbits); 20453055Sdanmcd 20463055Sdanmcd sofar = (uint64_t *)(++algdesc); 20473055Sdanmcd } 20483055Sdanmcd ecomb = (struct sadb_x_ecomb *)sofar; 20493055Sdanmcd } 20503055Sdanmcd } 20513055Sdanmcd 20523055Sdanmcd /* 20533055Sdanmcd * Print an SADB_EXT_SUPPORTED extension. 20543055Sdanmcd */ 20553055Sdanmcd void 20564867Spwernau print_supp(FILE *file, char *prefix, struct sadb_supported *supp) 20573055Sdanmcd { 20583055Sdanmcd struct sadb_alg *algs; 20593055Sdanmcd int i, numalgs; 20603055Sdanmcd 20614867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "%sSupported "), prefix); 20623055Sdanmcd switch (supp->sadb_supported_exttype) { 20633055Sdanmcd case SADB_EXT_SUPPORTED_AUTH: 20644867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "authentication")); 20653055Sdanmcd break; 20663055Sdanmcd case SADB_EXT_SUPPORTED_ENCRYPT: 20674867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "encryption")); 20683055Sdanmcd break; 20693055Sdanmcd } 20704867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, " algorithms.\n")); 20713055Sdanmcd 20723055Sdanmcd algs = (struct sadb_alg *)(supp + 1); 20733055Sdanmcd numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp)); 20743055Sdanmcd numalgs /= SADB_8TO64(sizeof (*algs)); 20753055Sdanmcd for (i = 0; i < numalgs; i++) { 20765906Svk199839 uint16_t exttype = supp->sadb_supported_exttype; 20775906Svk199839 20784867Spwernau (void) fprintf(file, "%s", prefix); 20795906Svk199839 switch (exttype) { 20803055Sdanmcd case SADB_EXT_SUPPORTED_AUTH: 20814867Spwernau (void) dump_aalg(algs[i].sadb_alg_id, file); 20823055Sdanmcd break; 20833055Sdanmcd case SADB_EXT_SUPPORTED_ENCRYPT: 20844867Spwernau (void) dump_ealg(algs[i].sadb_alg_id, file); 20853055Sdanmcd break; 20863055Sdanmcd } 20874867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20885906Svk199839 " minbits=%u, maxbits=%u, ivlen=%u"), 20893055Sdanmcd algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits, 20903055Sdanmcd algs[i].sadb_alg_ivlen); 20915906Svk199839 if (exttype == SADB_EXT_SUPPORTED_ENCRYPT) 20925906Svk199839 (void) fprintf(file, dgettext(TEXT_DOMAIN, 20935906Svk199839 ", increment=%u"), algs[i].sadb_x_alg_increment); 20945906Svk199839 (void) fprintf(file, dgettext(TEXT_DOMAIN, ".\n")); 20953055Sdanmcd } 20963055Sdanmcd } 20973055Sdanmcd 20983055Sdanmcd /* 20993055Sdanmcd * Print an SADB_EXT_SPIRANGE extension. 21003055Sdanmcd */ 21013055Sdanmcd void 21024867Spwernau print_spirange(FILE *file, char *prefix, struct sadb_spirange *range) 21033055Sdanmcd { 21044867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 21054064Smarkfen "%sSPI Range, min=0x%x, max=0x%x\n"), prefix, 21063055Sdanmcd htonl(range->sadb_spirange_min), 21073055Sdanmcd htonl(range->sadb_spirange_max)); 21083055Sdanmcd } 21093055Sdanmcd 21103055Sdanmcd /* 21113055Sdanmcd * Print an SADB_X_EXT_KM_COOKIE extension. 21123055Sdanmcd */ 21133055Sdanmcd 21143055Sdanmcd void 21154867Spwernau print_kmc(FILE *file, char *prefix, struct sadb_x_kmc *kmc) 21163055Sdanmcd { 21173055Sdanmcd char *cookie_label; 21183055Sdanmcd 21193055Sdanmcd if ((cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie)) == 21203055Sdanmcd NULL) 21214064Smarkfen cookie_label = dgettext(TEXT_DOMAIN, "<Label not found.>"); 21223055Sdanmcd 21234867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 21244064Smarkfen "%sProtocol %u, cookie=\"%s\" (%u)\n"), prefix, 21253055Sdanmcd kmc->sadb_x_kmc_proto, cookie_label, kmc->sadb_x_kmc_cookie); 21263055Sdanmcd } 21273055Sdanmcd 21283055Sdanmcd /* 21293055Sdanmcd * Take a PF_KEY message pointed to buffer and print it. Useful for DUMP 21303055Sdanmcd * and GET. 21313055Sdanmcd */ 21323055Sdanmcd void 21334867Spwernau print_samsg(FILE *file, uint64_t *buffer, boolean_t want_timestamp, 21344867Spwernau boolean_t vflag, boolean_t ignore_nss) 21353055Sdanmcd { 21363055Sdanmcd uint64_t *current; 21373055Sdanmcd struct sadb_msg *samsg = (struct sadb_msg *)buffer; 21383055Sdanmcd struct sadb_ext *ext; 21393055Sdanmcd struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL; 21403055Sdanmcd int i; 21413055Sdanmcd time_t wallclock; 21423055Sdanmcd 21433055Sdanmcd (void) time(&wallclock); 21443055Sdanmcd 21454867Spwernau print_sadb_msg(file, samsg, want_timestamp ? wallclock : 0, vflag); 21463055Sdanmcd current = (uint64_t *)(samsg + 1); 21473055Sdanmcd while (current - buffer < samsg->sadb_msg_len) { 21483055Sdanmcd int lenbytes; 21493055Sdanmcd 21503055Sdanmcd ext = (struct sadb_ext *)current; 21513055Sdanmcd lenbytes = SADB_64TO8(ext->sadb_ext_len); 21523055Sdanmcd switch (ext->sadb_ext_type) { 21533055Sdanmcd case SADB_EXT_SA: 21544867Spwernau print_sa(file, dgettext(TEXT_DOMAIN, 21554064Smarkfen "SA: "), (struct sadb_sa *)current); 21563055Sdanmcd break; 21573055Sdanmcd /* 21583055Sdanmcd * Pluck out lifetimes and print them at the end. This is 21593055Sdanmcd * to show relative lifetimes. 21603055Sdanmcd */ 21613055Sdanmcd case SADB_EXT_LIFETIME_CURRENT: 21623055Sdanmcd currentlt = (struct sadb_lifetime *)current; 21633055Sdanmcd break; 21643055Sdanmcd case SADB_EXT_LIFETIME_HARD: 21653055Sdanmcd hardlt = (struct sadb_lifetime *)current; 21663055Sdanmcd break; 21673055Sdanmcd case SADB_EXT_LIFETIME_SOFT: 21683055Sdanmcd softlt = (struct sadb_lifetime *)current; 21693055Sdanmcd break; 21703055Sdanmcd 21713055Sdanmcd case SADB_EXT_ADDRESS_SRC: 21724867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "SRC: "), 21734867Spwernau (struct sadb_address *)current, ignore_nss); 21743055Sdanmcd break; 21753055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 21764867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "INS: "), 21774867Spwernau (struct sadb_address *)current, ignore_nss); 21783055Sdanmcd break; 21793055Sdanmcd case SADB_EXT_ADDRESS_DST: 21804867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "DST: "), 21814867Spwernau (struct sadb_address *)current, ignore_nss); 21823055Sdanmcd break; 21833055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 21844867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "IND: "), 21854867Spwernau (struct sadb_address *)current, ignore_nss); 21863055Sdanmcd break; 21873055Sdanmcd case SADB_EXT_KEY_AUTH: 21884867Spwernau print_key(file, dgettext(TEXT_DOMAIN, 21894064Smarkfen "AKY: "), (struct sadb_key *)current); 21903055Sdanmcd break; 21913055Sdanmcd case SADB_EXT_KEY_ENCRYPT: 21924867Spwernau print_key(file, dgettext(TEXT_DOMAIN, 21934064Smarkfen "EKY: "), (struct sadb_key *)current); 21943055Sdanmcd break; 21953055Sdanmcd case SADB_EXT_IDENTITY_SRC: 21964867Spwernau print_ident(file, dgettext(TEXT_DOMAIN, "SID: "), 21973055Sdanmcd (struct sadb_ident *)current); 21983055Sdanmcd break; 21993055Sdanmcd case SADB_EXT_IDENTITY_DST: 22004867Spwernau print_ident(file, dgettext(TEXT_DOMAIN, "DID: "), 22013055Sdanmcd (struct sadb_ident *)current); 22023055Sdanmcd break; 22033055Sdanmcd case SADB_EXT_SENSITIVITY: 22044867Spwernau print_sens(file, dgettext(TEXT_DOMAIN, "SNS: "), 22053055Sdanmcd (struct sadb_sens *)current); 22063055Sdanmcd break; 22073055Sdanmcd case SADB_EXT_PROPOSAL: 22084867Spwernau print_prop(file, dgettext(TEXT_DOMAIN, "PRP: "), 22093055Sdanmcd (struct sadb_prop *)current); 22103055Sdanmcd break; 22113055Sdanmcd case SADB_EXT_SUPPORTED_AUTH: 22124867Spwernau print_supp(file, dgettext(TEXT_DOMAIN, "SUA: "), 22133055Sdanmcd (struct sadb_supported *)current); 22143055Sdanmcd break; 22153055Sdanmcd case SADB_EXT_SUPPORTED_ENCRYPT: 22164867Spwernau print_supp(file, dgettext(TEXT_DOMAIN, "SUE: "), 22173055Sdanmcd (struct sadb_supported *)current); 22183055Sdanmcd break; 22193055Sdanmcd case SADB_EXT_SPIRANGE: 22204867Spwernau print_spirange(file, dgettext(TEXT_DOMAIN, "SPR: "), 22213055Sdanmcd (struct sadb_spirange *)current); 22223055Sdanmcd break; 22233055Sdanmcd case SADB_X_EXT_EPROP: 22244867Spwernau print_eprop(file, dgettext(TEXT_DOMAIN, "EPR: "), 22253055Sdanmcd (struct sadb_prop *)current); 22263055Sdanmcd break; 22273055Sdanmcd case SADB_X_EXT_KM_COOKIE: 22284867Spwernau print_kmc(file, dgettext(TEXT_DOMAIN, "KMC: "), 22293055Sdanmcd (struct sadb_x_kmc *)current); 22303055Sdanmcd break; 22313055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 22324867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "NRM: "), 22334867Spwernau (struct sadb_address *)current, ignore_nss); 22343055Sdanmcd break; 22353055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 22364867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "NLC: "), 22374867Spwernau (struct sadb_address *)current, ignore_nss); 22383055Sdanmcd break; 22393055Sdanmcd default: 22404867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 22413055Sdanmcd "UNK: Unknown ext. %d, len %d.\n"), 22423055Sdanmcd ext->sadb_ext_type, lenbytes); 22433055Sdanmcd for (i = 0; i < ext->sadb_ext_len; i++) 22444867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 22454064Smarkfen "UNK: 0x%llx\n"), ((uint64_t *)ext)[i]); 22463055Sdanmcd break; 22473055Sdanmcd } 22483055Sdanmcd current += (lenbytes == 0) ? 22493055Sdanmcd SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len; 22503055Sdanmcd } 22513055Sdanmcd /* 22523055Sdanmcd * Print lifetimes NOW. 22533055Sdanmcd */ 22543055Sdanmcd if (currentlt != NULL || hardlt != NULL || softlt != NULL) 22554867Spwernau print_lifetimes(file, wallclock, currentlt, hardlt, softlt, 22564867Spwernau vflag); 22573055Sdanmcd 22583055Sdanmcd if (current - buffer != samsg->sadb_msg_len) { 22594867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 22604867Spwernau "WARNING: insufficient buffer space or corrupt message.")); 22613055Sdanmcd } 22623055Sdanmcd 22634867Spwernau (void) fflush(file); /* Make sure our message is out there. */ 22643055Sdanmcd } 22653055Sdanmcd 22663055Sdanmcd /* 22673055Sdanmcd * save_XXX functions are used when "saving" the SA tables to either a 22683055Sdanmcd * file or standard output. They use the dump_XXX functions where needed, 22693055Sdanmcd * but mostly they use the rparseXXX functions. 22703055Sdanmcd */ 22713055Sdanmcd 22723055Sdanmcd /* 22733055Sdanmcd * Print save information for a lifetime extension. 22743055Sdanmcd * 22753055Sdanmcd * NOTE : It saves the lifetime in absolute terms. For example, if you 22763055Sdanmcd * had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though 22773055Sdanmcd * there may have been 59 seconds burned off the clock. 22783055Sdanmcd */ 22793055Sdanmcd boolean_t 22803055Sdanmcd save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile) 22813055Sdanmcd { 22823055Sdanmcd char *prefix; 22833055Sdanmcd 22843055Sdanmcd prefix = (lifetime->sadb_lifetime_exttype == SADB_EXT_LIFETIME_SOFT) ? 22853055Sdanmcd "soft" : "hard"; 22863055Sdanmcd 22873055Sdanmcd if (putc('\t', ofile) == EOF) 22883055Sdanmcd return (B_FALSE); 22893055Sdanmcd 22903055Sdanmcd if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile, 22913055Sdanmcd "%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0) 22923055Sdanmcd return (B_FALSE); 22933055Sdanmcd 22943055Sdanmcd if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile, 22953055Sdanmcd "%s_bytes %llu ", prefix, lifetime->sadb_lifetime_bytes) < 0) 22963055Sdanmcd return (B_FALSE); 22973055Sdanmcd 22983055Sdanmcd if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile, 22993055Sdanmcd "%s_addtime %llu ", prefix, lifetime->sadb_lifetime_addtime) < 0) 23003055Sdanmcd return (B_FALSE); 23013055Sdanmcd 23023055Sdanmcd if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile, 23033055Sdanmcd "%s_usetime %llu ", prefix, lifetime->sadb_lifetime_usetime) < 0) 23043055Sdanmcd return (B_FALSE); 23053055Sdanmcd 23063055Sdanmcd return (B_TRUE); 23073055Sdanmcd } 23083055Sdanmcd 23093055Sdanmcd /* 23103055Sdanmcd * Print save information for an address extension. 23113055Sdanmcd */ 23123055Sdanmcd boolean_t 23133055Sdanmcd save_address(struct sadb_address *addr, FILE *ofile) 23143055Sdanmcd { 23153055Sdanmcd char *printable_addr, buf[INET6_ADDRSTRLEN]; 23163055Sdanmcd const char *prefix, *pprefix; 23173055Sdanmcd struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1); 23183055Sdanmcd struct sockaddr_in *sin = (struct sockaddr_in *)sin6; 23193055Sdanmcd int af = sin->sin_family; 23203055Sdanmcd 23213055Sdanmcd /* 23223055Sdanmcd * Address-family reality check. 23233055Sdanmcd */ 23243055Sdanmcd if (af != AF_INET6 && af != AF_INET) 23253055Sdanmcd return (B_FALSE); 23263055Sdanmcd 23273055Sdanmcd switch (addr->sadb_address_exttype) { 23283055Sdanmcd case SADB_EXT_ADDRESS_SRC: 23293055Sdanmcd prefix = "src"; 23303055Sdanmcd pprefix = "sport"; 23313055Sdanmcd break; 23323055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 23333055Sdanmcd prefix = "isrc"; 23343055Sdanmcd pprefix = "isport"; 23353055Sdanmcd break; 23363055Sdanmcd case SADB_EXT_ADDRESS_DST: 23373055Sdanmcd prefix = "dst"; 23383055Sdanmcd pprefix = "dport"; 23393055Sdanmcd break; 23403055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 23413055Sdanmcd prefix = "idst"; 23423055Sdanmcd pprefix = "idport"; 23433055Sdanmcd break; 23443055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 23453055Sdanmcd prefix = "nat_loc "; 23463055Sdanmcd pprefix = "nat_lport"; 23473055Sdanmcd break; 23483055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 23493055Sdanmcd prefix = "nat_rem "; 23503055Sdanmcd pprefix = "nat_rport"; 23513055Sdanmcd break; 23523055Sdanmcd } 23533055Sdanmcd 23543055Sdanmcd if (fprintf(ofile, " %s ", prefix) < 0) 23553055Sdanmcd return (B_FALSE); 23563055Sdanmcd 23573055Sdanmcd /* 23583055Sdanmcd * Do not do address-to-name translation, given that we live in 23593055Sdanmcd * an age of names that explode into many addresses. 23603055Sdanmcd */ 23613055Sdanmcd printable_addr = (char *)inet_ntop(af, 23623055Sdanmcd (af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr, 23633055Sdanmcd buf, sizeof (buf)); 23643055Sdanmcd if (printable_addr == NULL) 23654064Smarkfen printable_addr = "Invalid IP address."; 23663055Sdanmcd if (fprintf(ofile, "%s", printable_addr) < 0) 23673055Sdanmcd return (B_FALSE); 23683055Sdanmcd if (addr->sadb_address_prefixlen != 0 && 23693055Sdanmcd !((addr->sadb_address_prefixlen == 32 && af == AF_INET) || 23704342Spwernau (addr->sadb_address_prefixlen == 128 && af == AF_INET6))) { 23713055Sdanmcd if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0) 23723055Sdanmcd return (B_FALSE); 23733055Sdanmcd } 23743055Sdanmcd 23753055Sdanmcd /* 23763055Sdanmcd * The port is in the same position for struct sockaddr_in and 23773055Sdanmcd * struct sockaddr_in6. We exploit that property here. 23783055Sdanmcd */ 23793055Sdanmcd if ((pprefix != NULL) && (sin->sin_port != 0)) 23803055Sdanmcd (void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port)); 23813055Sdanmcd 23823055Sdanmcd return (B_TRUE); 23833055Sdanmcd } 23843055Sdanmcd 23853055Sdanmcd /* 23863055Sdanmcd * Print save information for a key extension. Returns whether writing 23873055Sdanmcd * to the specified output file was successful or not. 23883055Sdanmcd */ 23893055Sdanmcd boolean_t 23903055Sdanmcd save_key(struct sadb_key *key, FILE *ofile) 23913055Sdanmcd { 23923055Sdanmcd char *prefix; 23933055Sdanmcd 23943055Sdanmcd if (putc('\t', ofile) == EOF) 23953055Sdanmcd return (B_FALSE); 23963055Sdanmcd 23973055Sdanmcd prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr"; 23983055Sdanmcd 23993055Sdanmcd if (fprintf(ofile, "%skey ", prefix) < 0) 24003055Sdanmcd return (B_FALSE); 24013055Sdanmcd 24023055Sdanmcd if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits, ofile) == -1) 24033055Sdanmcd return (B_FALSE); 24043055Sdanmcd 24053055Sdanmcd return (B_TRUE); 24063055Sdanmcd } 24073055Sdanmcd 24083055Sdanmcd /* 24093055Sdanmcd * Print save information for an identity extension. 24103055Sdanmcd */ 24113055Sdanmcd boolean_t 24123055Sdanmcd save_ident(struct sadb_ident *ident, FILE *ofile) 24133055Sdanmcd { 24143055Sdanmcd char *prefix; 24153055Sdanmcd 24163055Sdanmcd if (putc('\t', ofile) == EOF) 24173055Sdanmcd return (B_FALSE); 24183055Sdanmcd 24193055Sdanmcd prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" : 24203055Sdanmcd "dst"; 24213055Sdanmcd 24223055Sdanmcd if (fprintf(ofile, "%sidtype %s ", prefix, 24233055Sdanmcd rparseidtype(ident->sadb_ident_type)) < 0) 24243055Sdanmcd return (B_FALSE); 24253055Sdanmcd 24263055Sdanmcd if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN || 24273055Sdanmcd ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) { 24284064Smarkfen if (fprintf(ofile, dgettext(TEXT_DOMAIN, 24294064Smarkfen "<can-not-print>")) < 0) 24303055Sdanmcd return (B_FALSE); 24313055Sdanmcd } else { 24323055Sdanmcd if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0) 24333055Sdanmcd return (B_FALSE); 24343055Sdanmcd } 24353055Sdanmcd 24363055Sdanmcd return (B_TRUE); 24373055Sdanmcd } 24383055Sdanmcd 24393055Sdanmcd /* 24403055Sdanmcd * "Save" a security association to an output file. 24413055Sdanmcd * 24424064Smarkfen * NOTE the lack of calls to dgettext() because I'm outputting parseable stuff. 24433055Sdanmcd * ALSO NOTE that if you change keywords (see parsecmd()), you'll have to 24443055Sdanmcd * change them here as well. 24453055Sdanmcd */ 24463055Sdanmcd void 24473055Sdanmcd save_assoc(uint64_t *buffer, FILE *ofile) 24483055Sdanmcd { 24494064Smarkfen int terrno; 24504987Sdanmcd boolean_t seen_proto = B_FALSE, seen_iproto = B_FALSE; 24513055Sdanmcd uint64_t *current; 24523055Sdanmcd struct sadb_address *addr; 24533055Sdanmcd struct sadb_msg *samsg = (struct sadb_msg *)buffer; 24543055Sdanmcd struct sadb_ext *ext; 24553055Sdanmcd 24564064Smarkfen #define tidyup() \ 24574064Smarkfen terrno = errno; (void) fclose(ofile); errno = terrno; \ 24584064Smarkfen interactive = B_FALSE 24594064Smarkfen 24604064Smarkfen #define savenl() if (fputs(" \\\n", ofile) == EOF) \ 24614064Smarkfen { bail(dgettext(TEXT_DOMAIN, "savenl")); } 24623055Sdanmcd 24633055Sdanmcd if (fputs("# begin assoc\n", ofile) == EOF) 24644064Smarkfen bail(dgettext(TEXT_DOMAIN, 24654064Smarkfen "save_assoc: Opening comment of SA")); 24663055Sdanmcd if (fprintf(ofile, "add %s ", rparsesatype(samsg->sadb_msg_satype)) < 0) 24674064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_assoc: First line of SA")); 24683055Sdanmcd savenl(); 24693055Sdanmcd 24703055Sdanmcd current = (uint64_t *)(samsg + 1); 24713055Sdanmcd while (current - buffer < samsg->sadb_msg_len) { 24723055Sdanmcd struct sadb_sa *assoc; 24733055Sdanmcd 24743055Sdanmcd ext = (struct sadb_ext *)current; 24754987Sdanmcd addr = (struct sadb_address *)ext; /* Just in case... */ 24763055Sdanmcd switch (ext->sadb_ext_type) { 24773055Sdanmcd case SADB_EXT_SA: 24783055Sdanmcd assoc = (struct sadb_sa *)ext; 24793055Sdanmcd if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) { 24803055Sdanmcd if (fprintf(ofile, "# WARNING: SA was dying " 24813055Sdanmcd "or dead.\n") < 0) { 24824064Smarkfen tidyup(); 24834064Smarkfen bail(dgettext(TEXT_DOMAIN, 24844064Smarkfen "save_assoc: fprintf not mature")); 24853055Sdanmcd } 24863055Sdanmcd } 24873055Sdanmcd if (fprintf(ofile, " spi 0x%x ", 24884064Smarkfen ntohl(assoc->sadb_sa_spi)) < 0) { 24894064Smarkfen tidyup(); 24904064Smarkfen bail(dgettext(TEXT_DOMAIN, 24914064Smarkfen "save_assoc: fprintf spi")); 24924064Smarkfen } 24933055Sdanmcd if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) { 24943055Sdanmcd if (fprintf(ofile, "encr_alg %s ", 24953055Sdanmcd rparsealg(assoc->sadb_sa_encrypt, 24964342Spwernau IPSEC_PROTO_ESP)) < 0) { 24974064Smarkfen tidyup(); 24984064Smarkfen bail(dgettext(TEXT_DOMAIN, 24994064Smarkfen "save_assoc: fprintf encrypt")); 25004064Smarkfen } 25013055Sdanmcd } 25023055Sdanmcd if (assoc->sadb_sa_auth != SADB_AALG_NONE) { 25033055Sdanmcd if (fprintf(ofile, "auth_alg %s ", 25043055Sdanmcd rparsealg(assoc->sadb_sa_auth, 25054342Spwernau IPSEC_PROTO_AH)) < 0) { 25064064Smarkfen tidyup(); 25074064Smarkfen bail(dgettext(TEXT_DOMAIN, 25084064Smarkfen "save_assoc: fprintf auth")); 25094064Smarkfen } 25103055Sdanmcd } 25113055Sdanmcd if (fprintf(ofile, "replay %d ", 25124064Smarkfen assoc->sadb_sa_replay) < 0) { 25134064Smarkfen tidyup(); 25144064Smarkfen bail(dgettext(TEXT_DOMAIN, 25154064Smarkfen "save_assoc: fprintf replay")); 25164064Smarkfen } 25173055Sdanmcd if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC | 25183055Sdanmcd SADB_X_SAFLAGS_NATT_REM)) { 25194064Smarkfen if (fprintf(ofile, "encap udp") < 0) { 25204064Smarkfen tidyup(); 25214064Smarkfen bail(dgettext(TEXT_DOMAIN, 25224064Smarkfen "save_assoc: fprintf encap")); 25234064Smarkfen } 25243055Sdanmcd } 25253055Sdanmcd savenl(); 25263055Sdanmcd break; 25273055Sdanmcd case SADB_EXT_LIFETIME_HARD: 25283055Sdanmcd case SADB_EXT_LIFETIME_SOFT: 25294064Smarkfen if (!save_lifetime((struct sadb_lifetime *)ext, 25304064Smarkfen ofile)) { 25314064Smarkfen tidyup(); 25324064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_lifetime")); 25334064Smarkfen } 25343055Sdanmcd savenl(); 25353055Sdanmcd break; 25363055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 25373055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 25384987Sdanmcd if (!seen_iproto && addr->sadb_address_proto) { 25394987Sdanmcd (void) fprintf(ofile, " iproto %d", 25404987Sdanmcd addr->sadb_address_proto); 25414987Sdanmcd savenl(); 25424987Sdanmcd seen_iproto = B_TRUE; 25434987Sdanmcd } 25444987Sdanmcd goto skip_srcdst; /* Hack to avoid cases below... */ 25454987Sdanmcd /* FALLTHRU */ 25464987Sdanmcd case SADB_EXT_ADDRESS_SRC: 25474987Sdanmcd case SADB_EXT_ADDRESS_DST: 25483055Sdanmcd if (!seen_proto && addr->sadb_address_proto) { 25493055Sdanmcd (void) fprintf(ofile, " proto %d", 25503055Sdanmcd addr->sadb_address_proto); 25513055Sdanmcd savenl(); 25524987Sdanmcd seen_proto = B_TRUE; 25533055Sdanmcd } 25544987Sdanmcd /* FALLTHRU */ 25554987Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 25564987Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 25574987Sdanmcd skip_srcdst: 25584064Smarkfen if (!save_address(addr, ofile)) { 25594064Smarkfen tidyup(); 25604064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_address")); 25614064Smarkfen } 25623055Sdanmcd savenl(); 25633055Sdanmcd break; 25643055Sdanmcd case SADB_EXT_KEY_AUTH: 25653055Sdanmcd case SADB_EXT_KEY_ENCRYPT: 25664064Smarkfen if (!save_key((struct sadb_key *)ext, ofile)) { 25674064Smarkfen tidyup(); 25684064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_address")); 25694064Smarkfen } 25703055Sdanmcd savenl(); 25713055Sdanmcd break; 25723055Sdanmcd case SADB_EXT_IDENTITY_SRC: 25733055Sdanmcd case SADB_EXT_IDENTITY_DST: 25744064Smarkfen if (!save_ident((struct sadb_ident *)ext, ofile)) { 25754064Smarkfen tidyup(); 25764064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_address")); 25774064Smarkfen } 25783055Sdanmcd savenl(); 25793055Sdanmcd break; 25803055Sdanmcd case SADB_EXT_SENSITIVITY: 25813055Sdanmcd default: 25823055Sdanmcd /* Skip over irrelevant extensions. */ 25833055Sdanmcd break; 25843055Sdanmcd } 25853055Sdanmcd current += ext->sadb_ext_len; 25863055Sdanmcd } 25873055Sdanmcd 25884064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) { 25894064Smarkfen tidyup(); 25904064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs")); 25914064Smarkfen } 25923055Sdanmcd } 25933055Sdanmcd 25943055Sdanmcd /* 25953055Sdanmcd * Open the output file for the "save" command. 25963055Sdanmcd */ 25973055Sdanmcd FILE * 25983055Sdanmcd opensavefile(char *filename) 25993055Sdanmcd { 26003055Sdanmcd int fd; 26013055Sdanmcd FILE *retval; 26023055Sdanmcd struct stat buf; 26033055Sdanmcd 26043055Sdanmcd /* 26053055Sdanmcd * If the user specifies "-" or doesn't give a filename, then 26063055Sdanmcd * dump to stdout. Make sure to document the dangers of files 26073055Sdanmcd * that are NFS, directing your output to strange places, etc. 26083055Sdanmcd */ 26093055Sdanmcd if (filename == NULL || strcmp("-", filename) == 0) 26103055Sdanmcd return (stdout); 26113055Sdanmcd 26123055Sdanmcd /* 26133055Sdanmcd * open the file with the create bits set. Since I check for 26143055Sdanmcd * real UID == root in main(), I won't worry about the ownership 26153055Sdanmcd * problem. 26163055Sdanmcd */ 26173055Sdanmcd fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR); 26183055Sdanmcd if (fd == -1) { 26193055Sdanmcd if (errno != EEXIST) 26204064Smarkfen bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN, 26214064Smarkfen "open error"), 26223055Sdanmcd strerror(errno)); 26233055Sdanmcd fd = open(filename, O_WRONLY | O_TRUNC, 0); 26243055Sdanmcd if (fd == -1) 26254064Smarkfen bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN, 26264064Smarkfen "open error"), strerror(errno)); 26273055Sdanmcd if (fstat(fd, &buf) == -1) { 26283055Sdanmcd (void) close(fd); 26293055Sdanmcd bail_msg("%s fstat: %s", filename, strerror(errno)); 26303055Sdanmcd } 26313055Sdanmcd if (S_ISREG(buf.st_mode) && 26323055Sdanmcd ((buf.st_mode & S_IAMB) != S_IRUSR)) { 26334064Smarkfen warnx(dgettext(TEXT_DOMAIN, 26344064Smarkfen "WARNING: Save file already exists with " 26354064Smarkfen "permission %o."), buf.st_mode & S_IAMB); 26364064Smarkfen warnx(dgettext(TEXT_DOMAIN, 26374064Smarkfen "Normal users may be able to read IPsec " 26384064Smarkfen "keying material.")); 26393055Sdanmcd } 26403055Sdanmcd } 26413055Sdanmcd 26423055Sdanmcd /* Okay, we have an FD. Assign it to a stdio FILE pointer. */ 26433055Sdanmcd retval = fdopen(fd, "w"); 26443055Sdanmcd if (retval == NULL) { 26453055Sdanmcd (void) close(fd); 26464064Smarkfen bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN, 26474064Smarkfen "fdopen error"), strerror(errno)); 26483055Sdanmcd } 26493055Sdanmcd return (retval); 26503055Sdanmcd } 26513055Sdanmcd 26523055Sdanmcd const char * 26533055Sdanmcd do_inet_ntop(const void *addr, char *cp, size_t size) 26543055Sdanmcd { 26553055Sdanmcd boolean_t isv4; 26563055Sdanmcd struct in6_addr *inaddr6 = (struct in6_addr *)addr; 26573055Sdanmcd struct in_addr inaddr; 26583055Sdanmcd 26593055Sdanmcd if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) { 26603055Sdanmcd IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr); 26613055Sdanmcd } 26623055Sdanmcd 26633055Sdanmcd return (inet_ntop(isv4 ? AF_INET : AF_INET6, 26643055Sdanmcd isv4 ? (void *)&inaddr : inaddr6, cp, size)); 26653055Sdanmcd } 26663055Sdanmcd 26673055Sdanmcd char numprint[NBUF_SIZE]; 26683055Sdanmcd 26693055Sdanmcd /* 26703055Sdanmcd * Parse and reverse parse a specific SA type (AH, ESP, etc.). 26713055Sdanmcd */ 26723055Sdanmcd static struct typetable { 26733055Sdanmcd char *type; 26743055Sdanmcd int token; 26753055Sdanmcd } type_table[] = { 26763055Sdanmcd {"all", SADB_SATYPE_UNSPEC}, 26773055Sdanmcd {"ah", SADB_SATYPE_AH}, 26783055Sdanmcd {"esp", SADB_SATYPE_ESP}, 26793055Sdanmcd /* PF_KEY NOTE: More to come if net/pfkeyv2.h gets updated. */ 26803055Sdanmcd {NULL, 0} /* Token value is irrelevant for this entry. */ 26813055Sdanmcd }; 26823055Sdanmcd 26833055Sdanmcd char * 26843055Sdanmcd rparsesatype(int type) 26853055Sdanmcd { 26863055Sdanmcd struct typetable *tt = type_table; 26873055Sdanmcd 26883055Sdanmcd while (tt->type != NULL && type != tt->token) 26893055Sdanmcd tt++; 26903055Sdanmcd 26913055Sdanmcd if (tt->type == NULL) { 26923055Sdanmcd (void) snprintf(numprint, NBUF_SIZE, "%d", type); 26933055Sdanmcd } else { 26943055Sdanmcd return (tt->type); 26953055Sdanmcd } 26963055Sdanmcd 26973055Sdanmcd return (numprint); 26983055Sdanmcd } 26993055Sdanmcd 27003055Sdanmcd 27013055Sdanmcd /* 27023055Sdanmcd * Return a string containing the name of the specified numerical algorithm 27033055Sdanmcd * identifier. 27043055Sdanmcd */ 27053055Sdanmcd char * 27063055Sdanmcd rparsealg(uint8_t alg, int proto_num) 27073055Sdanmcd { 27083055Sdanmcd static struct ipsecalgent *holder = NULL; /* we're single-threaded */ 27093055Sdanmcd 27103055Sdanmcd if (holder != NULL) 27113055Sdanmcd freeipsecalgent(holder); 27123055Sdanmcd 27133055Sdanmcd holder = getipsecalgbynum(alg, proto_num, NULL); 27143055Sdanmcd if (holder == NULL) { 27153055Sdanmcd (void) snprintf(numprint, NBUF_SIZE, "%d", alg); 27163055Sdanmcd return (numprint); 27173055Sdanmcd } 27183055Sdanmcd 27193055Sdanmcd return (*(holder->a_names)); 27203055Sdanmcd } 27213055Sdanmcd 27223055Sdanmcd /* 27233055Sdanmcd * Parse and reverse parse out a source/destination ID type. 27243055Sdanmcd */ 27253055Sdanmcd static struct idtypes { 27263055Sdanmcd char *idtype; 27273055Sdanmcd uint8_t retval; 27283055Sdanmcd } idtypes[] = { 27293055Sdanmcd {"prefix", SADB_IDENTTYPE_PREFIX}, 27303055Sdanmcd {"fqdn", SADB_IDENTTYPE_FQDN}, 27313055Sdanmcd {"domain", SADB_IDENTTYPE_FQDN}, 27323055Sdanmcd {"domainname", SADB_IDENTTYPE_FQDN}, 27333055Sdanmcd {"user_fqdn", SADB_IDENTTYPE_USER_FQDN}, 27343055Sdanmcd {"mailbox", SADB_IDENTTYPE_USER_FQDN}, 27353055Sdanmcd {"der_dn", SADB_X_IDENTTYPE_DN}, 27363055Sdanmcd {"der_gn", SADB_X_IDENTTYPE_GN}, 27373055Sdanmcd {NULL, 0} 27383055Sdanmcd }; 27393055Sdanmcd 27403055Sdanmcd char * 27413055Sdanmcd rparseidtype(uint16_t type) 27423055Sdanmcd { 27433055Sdanmcd struct idtypes *idp; 27443055Sdanmcd 27453055Sdanmcd for (idp = idtypes; idp->idtype != NULL; idp++) { 27463055Sdanmcd if (type == idp->retval) 27473055Sdanmcd return (idp->idtype); 27483055Sdanmcd } 27493055Sdanmcd 27503055Sdanmcd (void) snprintf(numprint, NBUF_SIZE, "%d", type); 27513055Sdanmcd return (numprint); 27523055Sdanmcd } 27534235Smarkfen 27544235Smarkfen /* 27554235Smarkfen * This is a general purpose exit function, calling functions can specify an 27564235Smarkfen * error type. If the command calling this function was started by smf(5) the 27574235Smarkfen * error type could be used as a hint to the restarter. In the future this 27584235Smarkfen * function could be used to do something more intelligent with a process that 27594235Smarkfen * encounters an error. 27604235Smarkfen * 27614235Smarkfen * The function will handle an optional variable args error message, this 27624235Smarkfen * will be written to the error stream, typically a log file or stderr. 27634235Smarkfen */ 27644235Smarkfen void 27654235Smarkfen ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...) 27664235Smarkfen { 27674235Smarkfen int exit_status; 27684235Smarkfen va_list args; 27694235Smarkfen 27704235Smarkfen if (fp == NULL) 27714235Smarkfen fp = stderr; 27724235Smarkfen if (fmt != NULL) { 27734235Smarkfen va_start(args, fmt); 27744235Smarkfen vwarnxfp(fp, fmt, args); 27754235Smarkfen va_end(args); 27764235Smarkfen } 27774235Smarkfen 27784235Smarkfen if (fmri == NULL) { 27794235Smarkfen /* Command being run directly from a shell. */ 27804235Smarkfen switch (type) { 27814235Smarkfen case SERVICE_EXIT_OK: 27824235Smarkfen exit_status = 0; 27834235Smarkfen break; 27844235Smarkfen case SERVICE_DEGRADE: 27854235Smarkfen return; 27864235Smarkfen break; 27874235Smarkfen case SERVICE_BADPERM: 27884235Smarkfen case SERVICE_BADCONF: 27894235Smarkfen case SERVICE_MAINTAIN: 27904235Smarkfen case SERVICE_DISABLE: 27914235Smarkfen case SERVICE_FATAL: 27924235Smarkfen case SERVICE_RESTART: 27934235Smarkfen warnxfp(fp, "Fatal error - exiting."); 27944235Smarkfen exit_status = 1; 27954235Smarkfen break; 27964235Smarkfen } 27974235Smarkfen } else { 27984235Smarkfen /* Command being run as a smf(5) method. */ 27994235Smarkfen switch (type) { 28004235Smarkfen case SERVICE_EXIT_OK: 28014235Smarkfen exit_status = SMF_EXIT_OK; 28024235Smarkfen break; 28034235Smarkfen case SERVICE_DEGRADE: 28044235Smarkfen return; 28054235Smarkfen break; 28064235Smarkfen case SERVICE_BADPERM: 28074235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28084235Smarkfen "Permission error with %s."), fmri); 28094235Smarkfen exit_status = SMF_EXIT_ERR_PERM; 28104235Smarkfen break; 28114235Smarkfen case SERVICE_BADCONF: 28124235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28134235Smarkfen "Bad configuration of service %s."), fmri); 28144235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28154235Smarkfen break; 28164235Smarkfen case SERVICE_MAINTAIN: 28174235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28184235Smarkfen "Service %s needs maintenance."), fmri); 28194235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28204235Smarkfen break; 28214235Smarkfen case SERVICE_DISABLE: 28224235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28234235Smarkfen break; 28244235Smarkfen case SERVICE_FATAL: 28254235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28264235Smarkfen "Service %s fatal error."), fmri); 28274235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28284235Smarkfen break; 28294235Smarkfen case SERVICE_RESTART: 28304235Smarkfen exit_status = 1; 28314235Smarkfen break; 28324235Smarkfen } 28334235Smarkfen } 28344235Smarkfen (void) fflush(fp); 28354235Smarkfen (void) fclose(fp); 28364235Smarkfen exit(exit_status); 28374235Smarkfen } 2838