10Sstevel@tonic-gate /* 26119Spwernau * 30Sstevel@tonic-gate * CDDL HEADER START 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * The contents of this file are subject to the terms of the 63055Sdanmcd * Common Development and Distribution License (the "License"). 73055Sdanmcd * You may not use this file except in compliance with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 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")); 1303*6668Smarkfen case SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE: 1304*6668Smarkfen return (dgettext(TEXT_DOMAIN, 1305*6668Smarkfen "Pairing failed, target SA unsuitable for pairing")); 1306*6668Smarkfen case SADB_X_DIAGNOSTIC_PAIR_ADD_MISMATCH: 1307*6668Smarkfen return (dgettext(TEXT_DOMAIN, 1308*6668Smarkfen "Source/destination address differs from pair SA")); 1309*6668Smarkfen case SADB_X_DIAGNOSTIC_PAIR_ALREADY: 1310*6668Smarkfen return (dgettext(TEXT_DOMAIN, 1311*6668Smarkfen "Already paired with another security association")); 1312*6668Smarkfen case SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND: 1313*6668Smarkfen return (dgettext(TEXT_DOMAIN, 1314*6668Smarkfen "Command failed, pair security association not found")); 1315*6668Smarkfen case SADB_X_DIAGNOSTIC_BAD_SA_DIRECTION: 1316*6668Smarkfen return (dgettext(TEXT_DOMAIN, 1317*6668Smarkfen "Inappropriate SA direction")); 1318*6668Smarkfen case SADB_X_DIAGNOSTIC_SA_NOTFOUND: 1319*6668Smarkfen return (dgettext(TEXT_DOMAIN, 1320*6668Smarkfen "Security association not found")); 1321*6668Smarkfen case SADB_X_DIAGNOSTIC_SA_EXPIRED: 1322*6668Smarkfen return (dgettext(TEXT_DOMAIN, 1323*6668Smarkfen "Security association is not valid")); 13240Sstevel@tonic-gate default: 13254064Smarkfen return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code")); 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate } 13283055Sdanmcd 13293055Sdanmcd /* 13303055Sdanmcd * Convert an IPv6 mask to a prefix len. I assume all IPv6 masks are 13313055Sdanmcd * contiguous, so I stop at the first zero bit! 13323055Sdanmcd */ 13333055Sdanmcd int 13343055Sdanmcd in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped) 13353055Sdanmcd { 13363055Sdanmcd int rc = 0; 13373055Sdanmcd uint8_t last; 13383055Sdanmcd int limit = IPV6_ABITS; 13393055Sdanmcd 13403055Sdanmcd if (is_v4mapped) { 13413055Sdanmcd mask += ((IPV6_ABITS - IP_ABITS)/8); 13423055Sdanmcd limit = IP_ABITS; 13433055Sdanmcd } 13443055Sdanmcd 13453055Sdanmcd while (*mask == 0xff) { 13463055Sdanmcd rc += 8; 13473055Sdanmcd if (rc == limit) 13483055Sdanmcd return (limit); 13493055Sdanmcd mask++; 13503055Sdanmcd } 13513055Sdanmcd 13523055Sdanmcd last = *mask; 13533055Sdanmcd while (last != 0) { 13543055Sdanmcd rc++; 13553055Sdanmcd last = (last << 1) & 0xff; 13563055Sdanmcd } 13573055Sdanmcd 13583055Sdanmcd return (rc); 13593055Sdanmcd } 13603055Sdanmcd 13613055Sdanmcd /* 13623055Sdanmcd * Expand the diagnostic code into a message. 13633055Sdanmcd */ 13643055Sdanmcd void 13653055Sdanmcd print_diagnostic(FILE *file, uint16_t diagnostic) 13663055Sdanmcd { 13673055Sdanmcd /* Use two spaces so above strings can fit on the line. */ 13684064Smarkfen (void) fprintf(file, dgettext(TEXT_DOMAIN, 13694064Smarkfen " Diagnostic code %u: %s.\n"), 13703055Sdanmcd diagnostic, keysock_diag(diagnostic)); 13713055Sdanmcd } 13723055Sdanmcd 13733055Sdanmcd /* 13743055Sdanmcd * Prints the base PF_KEY message. 13753055Sdanmcd */ 13763055Sdanmcd void 13774867Spwernau print_sadb_msg(FILE *file, struct sadb_msg *samsg, time_t wallclock, 13784867Spwernau boolean_t vflag) 13793055Sdanmcd { 13803055Sdanmcd if (wallclock != 0) 13814867Spwernau printsatime(file, wallclock, dgettext(TEXT_DOMAIN, 13824064Smarkfen "%sTimestamp: %s\n"), "", NULL, 13833055Sdanmcd vflag); 13843055Sdanmcd 13854867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 13864867Spwernau "Base message (version %u) type "), 13873055Sdanmcd samsg->sadb_msg_version); 13883055Sdanmcd switch (samsg->sadb_msg_type) { 13893055Sdanmcd case SADB_RESERVED: 13904867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 13914064Smarkfen "RESERVED (warning: set to 0)")); 13923055Sdanmcd break; 13933055Sdanmcd case SADB_GETSPI: 13944867Spwernau (void) fprintf(file, "GETSPI"); 13953055Sdanmcd break; 13963055Sdanmcd case SADB_UPDATE: 13974867Spwernau (void) fprintf(file, "UPDATE"); 13983055Sdanmcd break; 1399*6668Smarkfen case SADB_X_UPDATEPAIR: 1400*6668Smarkfen (void) fprintf(file, "UPDATE PAIR"); 1401*6668Smarkfen break; 14023055Sdanmcd case SADB_ADD: 14034867Spwernau (void) fprintf(file, "ADD"); 14043055Sdanmcd break; 14053055Sdanmcd case SADB_DELETE: 14064867Spwernau (void) fprintf(file, "DELETE"); 14073055Sdanmcd break; 1408*6668Smarkfen case SADB_X_DELPAIR: 1409*6668Smarkfen (void) fprintf(file, "DELETE PAIR"); 1410*6668Smarkfen break; 14113055Sdanmcd case SADB_GET: 14124867Spwernau (void) fprintf(file, "GET"); 14133055Sdanmcd break; 14143055Sdanmcd case SADB_ACQUIRE: 14154867Spwernau (void) fprintf(file, "ACQUIRE"); 14163055Sdanmcd break; 14173055Sdanmcd case SADB_REGISTER: 14184867Spwernau (void) fprintf(file, "REGISTER"); 14193055Sdanmcd break; 14203055Sdanmcd case SADB_EXPIRE: 14214867Spwernau (void) fprintf(file, "EXPIRE"); 14223055Sdanmcd break; 14233055Sdanmcd case SADB_FLUSH: 14244867Spwernau (void) fprintf(file, "FLUSH"); 14253055Sdanmcd break; 14263055Sdanmcd case SADB_DUMP: 14274867Spwernau (void) fprintf(file, "DUMP"); 14283055Sdanmcd break; 14293055Sdanmcd case SADB_X_PROMISC: 14304867Spwernau (void) fprintf(file, "X_PROMISC"); 14313055Sdanmcd break; 14323055Sdanmcd case SADB_X_INVERSE_ACQUIRE: 14334867Spwernau (void) fprintf(file, "X_INVERSE_ACQUIRE"); 14343055Sdanmcd break; 14353055Sdanmcd default: 14364867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14374064Smarkfen "Unknown (%u)"), samsg->sadb_msg_type); 14383055Sdanmcd break; 14393055Sdanmcd } 14404867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, ", SA type ")); 14413055Sdanmcd 14423055Sdanmcd switch (samsg->sadb_msg_satype) { 14433055Sdanmcd case SADB_SATYPE_UNSPEC: 14444867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14454867Spwernau "<unspecified/all>")); 14463055Sdanmcd break; 14473055Sdanmcd case SADB_SATYPE_AH: 14484867Spwernau (void) fprintf(file, "AH"); 14493055Sdanmcd break; 14503055Sdanmcd case SADB_SATYPE_ESP: 14514867Spwernau (void) fprintf(file, "ESP"); 14523055Sdanmcd break; 14533055Sdanmcd case SADB_SATYPE_RSVP: 14544867Spwernau (void) fprintf(file, "RSVP"); 14553055Sdanmcd break; 14563055Sdanmcd case SADB_SATYPE_OSPFV2: 14574867Spwernau (void) fprintf(file, "OSPFv2"); 14583055Sdanmcd break; 14593055Sdanmcd case SADB_SATYPE_RIPV2: 14604867Spwernau (void) fprintf(file, "RIPv2"); 14613055Sdanmcd break; 14623055Sdanmcd case SADB_SATYPE_MIP: 14634867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Mobile IP")); 14643055Sdanmcd break; 14653055Sdanmcd default: 14664867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14674064Smarkfen "<unknown %u>"), samsg->sadb_msg_satype); 14683055Sdanmcd break; 14693055Sdanmcd } 14703055Sdanmcd 14714867Spwernau (void) fprintf(file, ".\n"); 14723055Sdanmcd 14733055Sdanmcd if (samsg->sadb_msg_errno != 0) { 14744867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14754867Spwernau "Error %s from PF_KEY.\n"), 14763055Sdanmcd strerror(samsg->sadb_msg_errno)); 14774867Spwernau print_diagnostic(file, samsg->sadb_x_msg_diagnostic); 14783055Sdanmcd } 14793055Sdanmcd 14804867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14814064Smarkfen "Message length %u bytes, seq=%u, pid=%u.\n"), 14823055Sdanmcd SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq, 14833055Sdanmcd samsg->sadb_msg_pid); 14843055Sdanmcd } 14853055Sdanmcd 14863055Sdanmcd /* 14873055Sdanmcd * Print the SA extension for PF_KEY. 14883055Sdanmcd */ 14893055Sdanmcd void 14904867Spwernau print_sa(FILE *file, char *prefix, struct sadb_sa *assoc) 14913055Sdanmcd { 14923055Sdanmcd if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) { 14934867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 14944064Smarkfen "WARNING: SA info extension length (%u) is bad."), 14953055Sdanmcd SADB_64TO8(assoc->sadb_sa_len)); 14963055Sdanmcd } 14973055Sdanmcd 14984867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14994064Smarkfen "%sSADB_ASSOC spi=0x%x, replay=%u, state="), 15003055Sdanmcd prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay); 15013055Sdanmcd switch (assoc->sadb_sa_state) { 15023055Sdanmcd case SADB_SASTATE_LARVAL: 15034867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "LARVAL")); 15043055Sdanmcd break; 15053055Sdanmcd case SADB_SASTATE_MATURE: 15064867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "MATURE")); 15073055Sdanmcd break; 15083055Sdanmcd case SADB_SASTATE_DYING: 15094867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "DYING")); 15103055Sdanmcd break; 15113055Sdanmcd case SADB_SASTATE_DEAD: 15124867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "DEAD")); 15133055Sdanmcd break; 15143055Sdanmcd default: 15154867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 15164064Smarkfen "<unknown %u>"), assoc->sadb_sa_state); 15173055Sdanmcd } 15183055Sdanmcd 15193055Sdanmcd if (assoc->sadb_sa_auth != SADB_AALG_NONE) { 15204867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 15214064Smarkfen "\n%sAuthentication algorithm = "), 15223055Sdanmcd prefix); 15234867Spwernau (void) dump_aalg(assoc->sadb_sa_auth, file); 15243055Sdanmcd } 15253055Sdanmcd 15263055Sdanmcd if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) { 15274867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 15284064Smarkfen "\n%sEncryption algorithm = "), prefix); 15294867Spwernau (void) dump_ealg(assoc->sadb_sa_encrypt, file); 15303055Sdanmcd } 15313055Sdanmcd 15324867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix, 15333055Sdanmcd assoc->sadb_sa_flags); 15343055Sdanmcd if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS) 15354867Spwernau (void) fprintf(file, "PFS "); 15363055Sdanmcd if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY) 15374867Spwernau (void) fprintf(file, "NOREPLAY "); 15383055Sdanmcd 15393055Sdanmcd /* BEGIN Solaris-specific flags. */ 15403055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED) 15414867Spwernau (void) fprintf(file, "X_USED "); 1542*6668Smarkfen if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_PAIRED) 1543*6668Smarkfen (void) fprintf(file, "X_PAIRED "); 1544*6668Smarkfen if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_OUTBOUND) 1545*6668Smarkfen (void) fprintf(file, "X_OUTBOUND "); 1546*6668Smarkfen if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_INBOUND) 1547*6668Smarkfen (void) fprintf(file, "X_INBOUND "); 15483055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE) 15494867Spwernau (void) fprintf(file, "X_UNIQUE "); 15503055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1) 15514867Spwernau (void) fprintf(file, "X_AALG1 "); 15523055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2) 15534867Spwernau (void) fprintf(file, "X_AALG2 "); 15543055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1) 15554867Spwernau (void) fprintf(file, "X_EALG1 "); 15563055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2) 15574867Spwernau (void) fprintf(file, "X_EALG2 "); 15583055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC) 15594867Spwernau (void) fprintf(file, "X_NATT_LOC "); 15603055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM) 15614867Spwernau (void) fprintf(file, "X_NATT_REM "); 15623055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL) 15634867Spwernau (void) fprintf(file, "X_TUNNEL "); 15643055Sdanmcd /* END Solaris-specific flags. */ 15653055Sdanmcd 15664867Spwernau (void) fprintf(file, ">\n"); 15673055Sdanmcd } 15683055Sdanmcd 15693055Sdanmcd void 15704867Spwernau printsatime(FILE *file, int64_t lt, const char *msg, const char *pfx, 15714867Spwernau const char *pfx2, boolean_t vflag) 15723055Sdanmcd { 15733055Sdanmcd char tbuf[TBUF_SIZE]; /* For strftime() call. */ 15743055Sdanmcd const char *tp = tbuf; 15753055Sdanmcd time_t t = lt; 15763055Sdanmcd struct tm res; 15773055Sdanmcd 15783055Sdanmcd if (t != lt) { 15793055Sdanmcd if (lt > 0) 15803055Sdanmcd t = LONG_MAX; 15813055Sdanmcd else 15823055Sdanmcd t = LONG_MIN; 15833055Sdanmcd } 15843055Sdanmcd 15853055Sdanmcd if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0) 15864064Smarkfen tp = dgettext(TEXT_DOMAIN, "<time conversion failed>"); 15874867Spwernau (void) fprintf(file, msg, pfx, tp); 15883055Sdanmcd if (vflag && (pfx2 != NULL)) 15894867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 15904064Smarkfen "%s\t(raw time value %llu)\n"), pfx2, lt); 15913055Sdanmcd } 15923055Sdanmcd 15933055Sdanmcd /* 15943055Sdanmcd * Print the SA lifetime information. (An SADB_EXT_LIFETIME_* extension.) 15953055Sdanmcd */ 15963055Sdanmcd void 15974867Spwernau print_lifetimes(FILE *file, time_t wallclock, struct sadb_lifetime *current, 15983055Sdanmcd struct sadb_lifetime *hard, struct sadb_lifetime *soft, boolean_t vflag) 15993055Sdanmcd { 16003055Sdanmcd int64_t scratch; 16014064Smarkfen char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: "); 16024064Smarkfen char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: "); 16034064Smarkfen char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: "); 16043055Sdanmcd 16053055Sdanmcd if (current != NULL && 16063055Sdanmcd current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) { 16074867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 16084064Smarkfen "WARNING: CURRENT lifetime extension length (%u) is bad."), 16093055Sdanmcd SADB_64TO8(current->sadb_lifetime_len)); 16103055Sdanmcd } 16113055Sdanmcd 16123055Sdanmcd if (hard != NULL && 16133055Sdanmcd hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) { 16144867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 16154867Spwernau "WARNING: HARD lifetime extension length (%u) is bad."), 16163055Sdanmcd SADB_64TO8(hard->sadb_lifetime_len)); 16173055Sdanmcd } 16183055Sdanmcd 16193055Sdanmcd if (soft != NULL && 16203055Sdanmcd soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) { 16214867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 16224867Spwernau "WARNING: SOFT lifetime extension length (%u) is bad."), 16233055Sdanmcd SADB_64TO8(soft->sadb_lifetime_len)); 16243055Sdanmcd } 16253055Sdanmcd 16264867Spwernau (void) fprintf(file, " LT: Lifetime information\n"); 16273055Sdanmcd 16283055Sdanmcd if (current != NULL) { 16293055Sdanmcd /* Express values as current values. */ 16304867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16313055Sdanmcd "%s%llu bytes protected, %u allocations used.\n"), 16323055Sdanmcd current_prefix, current->sadb_lifetime_bytes, 16333055Sdanmcd current->sadb_lifetime_allocations); 16344867Spwernau printsatime(file, current->sadb_lifetime_addtime, 16354064Smarkfen dgettext(TEXT_DOMAIN, "%sSA added at time %s\n"), 16363055Sdanmcd current_prefix, current_prefix, vflag); 16373055Sdanmcd if (current->sadb_lifetime_usetime != 0) { 16384867Spwernau printsatime(file, current->sadb_lifetime_usetime, 16394064Smarkfen dgettext(TEXT_DOMAIN, 16404064Smarkfen "%sSA first used at time %s\n"), 16413055Sdanmcd current_prefix, current_prefix, vflag); 16423055Sdanmcd } 16434867Spwernau printsatime(file, wallclock, dgettext(TEXT_DOMAIN, 16444064Smarkfen "%sTime now is %s\n"), current_prefix, current_prefix, 16454064Smarkfen vflag); 16463055Sdanmcd } 16473055Sdanmcd 16483055Sdanmcd if (soft != NULL) { 16494867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16504064Smarkfen "%sSoft lifetime information: "), 16513055Sdanmcd soft_prefix); 16524867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16534064Smarkfen "%llu bytes of lifetime, %u " 16543055Sdanmcd "allocations.\n"), soft->sadb_lifetime_bytes, 16553055Sdanmcd soft->sadb_lifetime_allocations); 16564867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16574064Smarkfen "%s%llu seconds of post-add lifetime.\n"), 16583055Sdanmcd soft_prefix, soft->sadb_lifetime_addtime); 16594867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16604064Smarkfen "%s%llu seconds of post-use lifetime.\n"), 16613055Sdanmcd soft_prefix, soft->sadb_lifetime_usetime); 16623055Sdanmcd /* If possible, express values as time remaining. */ 16633055Sdanmcd if (current != NULL) { 16643055Sdanmcd if (soft->sadb_lifetime_bytes != 0) 16654867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16663055Sdanmcd "%s%llu more bytes can be protected.\n"), 16673055Sdanmcd soft_prefix, 16683055Sdanmcd (soft->sadb_lifetime_bytes > 16694342Spwernau current->sadb_lifetime_bytes) ? 16703055Sdanmcd (soft->sadb_lifetime_bytes - 16714342Spwernau current->sadb_lifetime_bytes) : (0)); 16723055Sdanmcd if (soft->sadb_lifetime_addtime != 0 || 16733055Sdanmcd (soft->sadb_lifetime_usetime != 0 && 16744342Spwernau current->sadb_lifetime_usetime != 0)) { 16753055Sdanmcd int64_t adddelta, usedelta; 16763055Sdanmcd 16773055Sdanmcd if (soft->sadb_lifetime_addtime != 0) { 16783055Sdanmcd adddelta = 16793055Sdanmcd current->sadb_lifetime_addtime + 16803055Sdanmcd soft->sadb_lifetime_addtime - 16813055Sdanmcd wallclock; 16823055Sdanmcd } else { 16833055Sdanmcd adddelta = TIME_MAX; 16843055Sdanmcd } 16853055Sdanmcd 16863055Sdanmcd if (soft->sadb_lifetime_usetime != 0 && 16873055Sdanmcd current->sadb_lifetime_usetime != 0) { 16883055Sdanmcd usedelta = 16893055Sdanmcd current->sadb_lifetime_usetime + 16903055Sdanmcd soft->sadb_lifetime_usetime - 16913055Sdanmcd wallclock; 16923055Sdanmcd } else { 16933055Sdanmcd usedelta = TIME_MAX; 16943055Sdanmcd } 16954867Spwernau (void) fprintf(file, "%s", soft_prefix); 16963055Sdanmcd scratch = MIN(adddelta, usedelta); 16973055Sdanmcd if (scratch >= 0) { 16984867Spwernau (void) fprintf(file, 16994867Spwernau dgettext(TEXT_DOMAIN, 17004064Smarkfen "Soft expiration occurs in %lld " 17014064Smarkfen "seconds, "), scratch); 17023055Sdanmcd } else { 17034867Spwernau (void) fprintf(file, 17044867Spwernau dgettext(TEXT_DOMAIN, 17053055Sdanmcd "Soft expiration occurred ")); 17063055Sdanmcd } 17073055Sdanmcd scratch += wallclock; 17084867Spwernau printsatime(file, scratch, dgettext(TEXT_DOMAIN, 17094064Smarkfen "%sat %s.\n"), "", soft_prefix, vflag); 17103055Sdanmcd } 17113055Sdanmcd } 17123055Sdanmcd } 17133055Sdanmcd 17143055Sdanmcd if (hard != NULL) { 17154867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17164064Smarkfen "%sHard lifetime information: "), hard_prefix); 17174867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17184867Spwernau "%llu bytes of lifetime, %u allocations.\n"), 17194867Spwernau hard->sadb_lifetime_bytes, hard->sadb_lifetime_allocations); 17204867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17214064Smarkfen "%s%llu seconds of post-add lifetime.\n"), 17223055Sdanmcd hard_prefix, hard->sadb_lifetime_addtime); 17234867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17244064Smarkfen "%s%llu seconds of post-use lifetime.\n"), 17253055Sdanmcd hard_prefix, hard->sadb_lifetime_usetime); 17263055Sdanmcd /* If possible, express values as time remaining. */ 17273055Sdanmcd if (current != NULL) { 17283055Sdanmcd if (hard->sadb_lifetime_bytes != 0) 17294867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17303055Sdanmcd "%s%llu more bytes can be protected.\n"), 17313055Sdanmcd hard_prefix, 17323055Sdanmcd (hard->sadb_lifetime_bytes > 17334342Spwernau current->sadb_lifetime_bytes) ? 17343055Sdanmcd (hard->sadb_lifetime_bytes - 17354342Spwernau current->sadb_lifetime_bytes) : (0)); 17363055Sdanmcd if (hard->sadb_lifetime_addtime != 0 || 17373055Sdanmcd (hard->sadb_lifetime_usetime != 0 && 17384342Spwernau current->sadb_lifetime_usetime != 0)) { 17393055Sdanmcd int64_t adddelta, usedelta; 17403055Sdanmcd 17413055Sdanmcd if (hard->sadb_lifetime_addtime != 0) { 17423055Sdanmcd adddelta = 17433055Sdanmcd current->sadb_lifetime_addtime + 17443055Sdanmcd hard->sadb_lifetime_addtime - 17453055Sdanmcd wallclock; 17463055Sdanmcd } else { 17473055Sdanmcd adddelta = TIME_MAX; 17483055Sdanmcd } 17493055Sdanmcd 17503055Sdanmcd if (hard->sadb_lifetime_usetime != 0 && 17513055Sdanmcd current->sadb_lifetime_usetime != 0) { 17523055Sdanmcd usedelta = 17533055Sdanmcd current->sadb_lifetime_usetime + 17543055Sdanmcd hard->sadb_lifetime_usetime - 17553055Sdanmcd wallclock; 17563055Sdanmcd } else { 17573055Sdanmcd usedelta = TIME_MAX; 17583055Sdanmcd } 17594867Spwernau (void) fprintf(file, "%s", hard_prefix); 17603055Sdanmcd scratch = MIN(adddelta, usedelta); 17613055Sdanmcd if (scratch >= 0) { 17624867Spwernau (void) fprintf(file, 17634867Spwernau dgettext(TEXT_DOMAIN, 17644064Smarkfen "Hard expiration occurs in %lld " 17654064Smarkfen "seconds, "), scratch); 17663055Sdanmcd } else { 17674867Spwernau (void) fprintf(file, 17684867Spwernau dgettext(TEXT_DOMAIN, 17693055Sdanmcd "Hard expiration occured ")); 17703055Sdanmcd } 17713055Sdanmcd scratch += wallclock; 17724867Spwernau printsatime(file, scratch, dgettext(TEXT_DOMAIN, 17734064Smarkfen "%sat %s.\n"), "", hard_prefix, vflag); 17743055Sdanmcd } 17753055Sdanmcd } 17763055Sdanmcd } 17773055Sdanmcd } 17783055Sdanmcd 17793055Sdanmcd /* 17803055Sdanmcd * Print an SADB_EXT_ADDRESS_* extension. 17813055Sdanmcd */ 17823055Sdanmcd void 17834867Spwernau print_address(FILE *file, char *prefix, struct sadb_address *addr, 17844867Spwernau boolean_t ignore_nss) 17853055Sdanmcd { 17863055Sdanmcd struct protoent *pe; 17873055Sdanmcd 17884867Spwernau (void) fprintf(file, "%s", prefix); 17893055Sdanmcd switch (addr->sadb_address_exttype) { 17903055Sdanmcd case SADB_EXT_ADDRESS_SRC: 17914867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Source address ")); 17923055Sdanmcd break; 17933055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 17944867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17954867Spwernau "Inner source address ")); 17963055Sdanmcd break; 17973055Sdanmcd case SADB_EXT_ADDRESS_DST: 17984867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17994867Spwernau "Destination address ")); 18003055Sdanmcd break; 18013055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 18024867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18034064Smarkfen "Inner destination address ")); 18043055Sdanmcd break; 18053055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 18064867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18074867Spwernau "NAT-T local address ")); 18083055Sdanmcd break; 18093055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 18104867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18114867Spwernau "NAT-T remote address ")); 18123055Sdanmcd break; 18133055Sdanmcd } 18143055Sdanmcd 18154867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18164064Smarkfen "(proto=%d"), addr->sadb_address_proto); 18174867Spwernau if (ignore_nss == B_FALSE) { 18183055Sdanmcd if (addr->sadb_address_proto == 0) { 18194867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18204867Spwernau "/<unspecified>")); 18213055Sdanmcd } else if ((pe = getprotobynumber(addr->sadb_address_proto)) 18223055Sdanmcd != NULL) { 18234867Spwernau (void) fprintf(file, "/%s", pe->p_name); 18243055Sdanmcd } else { 18254867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18264867Spwernau "/<unknown>")); 18273055Sdanmcd } 18283055Sdanmcd } 18294867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, ")\n%s"), prefix); 18303055Sdanmcd (void) dump_sockaddr((struct sockaddr *)(addr + 1), 18314867Spwernau addr->sadb_address_prefixlen, B_FALSE, file, ignore_nss); 18323055Sdanmcd } 18333055Sdanmcd 18343055Sdanmcd /* 18353055Sdanmcd * Print an SADB_EXT_KEY extension. 18363055Sdanmcd */ 18373055Sdanmcd void 18384867Spwernau print_key(FILE *file, char *prefix, struct sadb_key *key) 18393055Sdanmcd { 18404867Spwernau (void) fprintf(file, "%s", prefix); 18413055Sdanmcd 18423055Sdanmcd switch (key->sadb_key_exttype) { 18433055Sdanmcd case SADB_EXT_KEY_AUTH: 18444867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Authentication")); 18453055Sdanmcd break; 18463055Sdanmcd case SADB_EXT_KEY_ENCRYPT: 18474867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Encryption")); 18483055Sdanmcd break; 18493055Sdanmcd } 18503055Sdanmcd 18514867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, " key.\n%s"), prefix); 18524867Spwernau (void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits, file); 18534867Spwernau (void) fprintf(file, "\n"); 18543055Sdanmcd } 18553055Sdanmcd 18563055Sdanmcd /* 18573055Sdanmcd * Print an SADB_EXT_IDENTITY_* extension. 18583055Sdanmcd */ 18593055Sdanmcd void 18604867Spwernau print_ident(FILE *file, char *prefix, struct sadb_ident *id) 18613055Sdanmcd { 18623055Sdanmcd boolean_t canprint = B_TRUE; 18633055Sdanmcd 18644867Spwernau (void) fprintf(file, "%s", prefix); 18653055Sdanmcd switch (id->sadb_ident_exttype) { 18663055Sdanmcd case SADB_EXT_IDENTITY_SRC: 18674867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Source")); 18683055Sdanmcd break; 18693055Sdanmcd case SADB_EXT_IDENTITY_DST: 18704867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Destination")); 18713055Sdanmcd break; 18723055Sdanmcd } 18733055Sdanmcd 18744867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18754064Smarkfen " identity, uid=%d, type "), id->sadb_ident_id); 18764867Spwernau canprint = dump_sadb_idtype(id->sadb_ident_type, file, NULL); 18774867Spwernau (void) fprintf(file, "\n%s", prefix); 18786119Spwernau if (canprint) { 18794867Spwernau (void) fprintf(file, "%s\n", (char *)(id + 1)); 18806119Spwernau } else { 18816119Spwernau print_asn1_name(file, (const unsigned char *)(id + 1), 18826119Spwernau SADB_64TO8(id->sadb_ident_len) - sizeof (sadb_ident_t)); 18836119Spwernau } 18843055Sdanmcd } 18853055Sdanmcd 18863055Sdanmcd /* 18873055Sdanmcd * Print an SADB_SENSITIVITY extension. 18883055Sdanmcd */ 18893055Sdanmcd void 18904867Spwernau print_sens(FILE *file, char *prefix, struct sadb_sens *sens) 18913055Sdanmcd { 18923055Sdanmcd uint64_t *bitmap = (uint64_t *)(sens + 1); 18933055Sdanmcd int i; 18943055Sdanmcd 18954867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18964064Smarkfen "%sSensitivity DPD %d, sens level=%d, integ level=%d\n"), 18973055Sdanmcd prefix, sens->sadb_sens_dpd, sens->sadb_sens_sens_level, 18983055Sdanmcd sens->sadb_sens_integ_level); 18993055Sdanmcd for (i = 0; sens->sadb_sens_sens_len-- > 0; i++, bitmap++) 19004867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19014867Spwernau "%s Sensitivity BM extended word %d 0x%llx\n"), 19024867Spwernau prefix, i, *bitmap); 19033055Sdanmcd for (i = 0; sens->sadb_sens_integ_len-- > 0; i++, bitmap++) 19044867Spwernau (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 19054867Spwernau "%s Integrity BM extended word %d 0x%llx\n"), 19064867Spwernau prefix, i, *bitmap); 19073055Sdanmcd } 19083055Sdanmcd 19093055Sdanmcd /* 19103055Sdanmcd * Print an SADB_EXT_PROPOSAL extension. 19113055Sdanmcd */ 19123055Sdanmcd void 19134867Spwernau print_prop(FILE *file, char *prefix, struct sadb_prop *prop) 19143055Sdanmcd { 19153055Sdanmcd struct sadb_comb *combs; 19163055Sdanmcd int i, numcombs; 19173055Sdanmcd 19184867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19194064Smarkfen "%sProposal, replay counter = %u.\n"), prefix, 19203055Sdanmcd prop->sadb_prop_replay); 19213055Sdanmcd 19223055Sdanmcd numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop)); 19233055Sdanmcd numcombs /= SADB_8TO64(sizeof (*combs)); 19243055Sdanmcd 19253055Sdanmcd combs = (struct sadb_comb *)(prop + 1); 19263055Sdanmcd 19273055Sdanmcd for (i = 0; i < numcombs; i++) { 19284867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19294064Smarkfen "%s Combination #%u "), prefix, i + 1); 19303055Sdanmcd if (combs[i].sadb_comb_auth != SADB_AALG_NONE) { 19314867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19324064Smarkfen "Authentication = ")); 19334867Spwernau (void) dump_aalg(combs[i].sadb_comb_auth, file); 19344867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19354064Smarkfen " minbits=%u, maxbits=%u.\n%s "), 19363055Sdanmcd combs[i].sadb_comb_auth_minbits, 19373055Sdanmcd combs[i].sadb_comb_auth_maxbits, prefix); 19383055Sdanmcd } 19393055Sdanmcd 19403055Sdanmcd if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) { 19414867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19424867Spwernau "Encryption = ")); 19434867Spwernau (void) dump_ealg(combs[i].sadb_comb_encrypt, file); 19444867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19454064Smarkfen " minbits=%u, maxbits=%u.\n%s "), 19463055Sdanmcd combs[i].sadb_comb_encrypt_minbits, 19473055Sdanmcd combs[i].sadb_comb_encrypt_maxbits, prefix); 19483055Sdanmcd } 19493055Sdanmcd 19504867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "HARD: ")); 19513055Sdanmcd if (combs[i].sadb_comb_hard_allocations) 19524867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "), 19533055Sdanmcd combs[i].sadb_comb_hard_allocations); 19543055Sdanmcd if (combs[i].sadb_comb_hard_bytes) 19554867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19564867Spwernau "bytes=%llu "), combs[i].sadb_comb_hard_bytes); 19573055Sdanmcd if (combs[i].sadb_comb_hard_addtime) 19584867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19594064Smarkfen "post-add secs=%llu "), 19603055Sdanmcd combs[i].sadb_comb_hard_addtime); 19613055Sdanmcd if (combs[i].sadb_comb_hard_usetime) 19624867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19634064Smarkfen "post-use secs=%llu"), 19643055Sdanmcd combs[i].sadb_comb_hard_usetime); 19653055Sdanmcd 19664867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%s SOFT: "), 19674867Spwernau prefix); 19683055Sdanmcd if (combs[i].sadb_comb_soft_allocations) 19694867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "), 19703055Sdanmcd combs[i].sadb_comb_soft_allocations); 19713055Sdanmcd if (combs[i].sadb_comb_soft_bytes) 19724867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19734867Spwernau "bytes=%llu "), combs[i].sadb_comb_soft_bytes); 19743055Sdanmcd if (combs[i].sadb_comb_soft_addtime) 19754867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19764064Smarkfen "post-add secs=%llu "), 19773055Sdanmcd combs[i].sadb_comb_soft_addtime); 19783055Sdanmcd if (combs[i].sadb_comb_soft_usetime) 19794867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19804064Smarkfen "post-use secs=%llu"), 19813055Sdanmcd combs[i].sadb_comb_soft_usetime); 19824867Spwernau (void) fprintf(file, "\n"); 19833055Sdanmcd } 19843055Sdanmcd } 19853055Sdanmcd 19863055Sdanmcd /* 19873055Sdanmcd * Print an extended proposal (SADB_X_EXT_EPROP). 19883055Sdanmcd */ 19893055Sdanmcd void 19904867Spwernau print_eprop(FILE *file, char *prefix, struct sadb_prop *eprop) 19913055Sdanmcd { 19923055Sdanmcd uint64_t *sofar; 19933055Sdanmcd struct sadb_x_ecomb *ecomb; 19943055Sdanmcd struct sadb_x_algdesc *algdesc; 19953055Sdanmcd int i, j; 19963055Sdanmcd 19974867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19984064Smarkfen "%sExtended Proposal, replay counter = %u, "), prefix, 19994064Smarkfen eprop->sadb_prop_replay); 20004867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20014867Spwernau "number of combinations = %u.\n"), eprop->sadb_x_prop_numecombs); 20023055Sdanmcd 20033055Sdanmcd sofar = (uint64_t *)(eprop + 1); 20043055Sdanmcd ecomb = (struct sadb_x_ecomb *)sofar; 20053055Sdanmcd 20063055Sdanmcd for (i = 0; i < eprop->sadb_x_prop_numecombs; ) { 20074867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20084064Smarkfen "%s Extended combination #%u:\n"), prefix, ++i); 20093055Sdanmcd 20104867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "%s HARD: "), 20114867Spwernau prefix); 20124867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "), 20133055Sdanmcd ecomb->sadb_x_ecomb_hard_allocations); 20144867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%llu, "), 20153055Sdanmcd ecomb->sadb_x_ecomb_hard_bytes); 20164867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20174867Spwernau "post-add secs=%llu, "), ecomb->sadb_x_ecomb_hard_addtime); 20184867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20194867Spwernau "post-use secs=%llu\n"), ecomb->sadb_x_ecomb_hard_usetime); 20203055Sdanmcd 20214867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "%s SOFT: "), 20224867Spwernau prefix); 20234867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "), 20243055Sdanmcd ecomb->sadb_x_ecomb_soft_allocations); 20254867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%llu, "), 20263055Sdanmcd ecomb->sadb_x_ecomb_soft_bytes); 20274867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20284867Spwernau "post-add secs=%llu, "), 20293055Sdanmcd ecomb->sadb_x_ecomb_soft_addtime); 20304867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20314867Spwernau "post-use secs=%llu\n"), ecomb->sadb_x_ecomb_soft_usetime); 20323055Sdanmcd 20333055Sdanmcd sofar = (uint64_t *)(ecomb + 1); 20343055Sdanmcd algdesc = (struct sadb_x_algdesc *)sofar; 20353055Sdanmcd 20363055Sdanmcd for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) { 20374867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20384064Smarkfen "%s Alg #%u "), prefix, ++j); 20393055Sdanmcd switch (algdesc->sadb_x_algdesc_satype) { 20403055Sdanmcd case SADB_SATYPE_ESP: 20414867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20424064Smarkfen "for ESP ")); 20433055Sdanmcd break; 20443055Sdanmcd case SADB_SATYPE_AH: 20454867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20464867Spwernau "for AH ")); 20473055Sdanmcd break; 20483055Sdanmcd default: 20494867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20504064Smarkfen "for satype=%d "), 20513055Sdanmcd algdesc->sadb_x_algdesc_satype); 20523055Sdanmcd } 20533055Sdanmcd switch (algdesc->sadb_x_algdesc_algtype) { 20543055Sdanmcd case SADB_X_ALGTYPE_CRYPT: 20554867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20564064Smarkfen "Encryption = ")); 20573055Sdanmcd (void) dump_ealg(algdesc->sadb_x_algdesc_alg, 20584867Spwernau file); 20593055Sdanmcd break; 20603055Sdanmcd case SADB_X_ALGTYPE_AUTH: 20614867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20624064Smarkfen "Authentication = ")); 20633055Sdanmcd (void) dump_aalg(algdesc->sadb_x_algdesc_alg, 20644867Spwernau file); 20653055Sdanmcd break; 20663055Sdanmcd default: 20674867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20684064Smarkfen "algtype(%d) = alg(%d)"), 20693055Sdanmcd algdesc->sadb_x_algdesc_algtype, 20703055Sdanmcd algdesc->sadb_x_algdesc_alg); 20713055Sdanmcd break; 20723055Sdanmcd } 20733055Sdanmcd 20744867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20754064Smarkfen " minbits=%u, maxbits=%u.\n"), 20763055Sdanmcd algdesc->sadb_x_algdesc_minbits, 20773055Sdanmcd algdesc->sadb_x_algdesc_maxbits); 20783055Sdanmcd 20793055Sdanmcd sofar = (uint64_t *)(++algdesc); 20803055Sdanmcd } 20813055Sdanmcd ecomb = (struct sadb_x_ecomb *)sofar; 20823055Sdanmcd } 20833055Sdanmcd } 20843055Sdanmcd 20853055Sdanmcd /* 20863055Sdanmcd * Print an SADB_EXT_SUPPORTED extension. 20873055Sdanmcd */ 20883055Sdanmcd void 20894867Spwernau print_supp(FILE *file, char *prefix, struct sadb_supported *supp) 20903055Sdanmcd { 20913055Sdanmcd struct sadb_alg *algs; 20923055Sdanmcd int i, numalgs; 20933055Sdanmcd 20944867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "%sSupported "), prefix); 20953055Sdanmcd switch (supp->sadb_supported_exttype) { 20963055Sdanmcd case SADB_EXT_SUPPORTED_AUTH: 20974867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "authentication")); 20983055Sdanmcd break; 20993055Sdanmcd case SADB_EXT_SUPPORTED_ENCRYPT: 21004867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "encryption")); 21013055Sdanmcd break; 21023055Sdanmcd } 21034867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, " algorithms.\n")); 21043055Sdanmcd 21053055Sdanmcd algs = (struct sadb_alg *)(supp + 1); 21063055Sdanmcd numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp)); 21073055Sdanmcd numalgs /= SADB_8TO64(sizeof (*algs)); 21083055Sdanmcd for (i = 0; i < numalgs; i++) { 21095906Svk199839 uint16_t exttype = supp->sadb_supported_exttype; 21105906Svk199839 21114867Spwernau (void) fprintf(file, "%s", prefix); 21125906Svk199839 switch (exttype) { 21133055Sdanmcd case SADB_EXT_SUPPORTED_AUTH: 21144867Spwernau (void) dump_aalg(algs[i].sadb_alg_id, file); 21153055Sdanmcd break; 21163055Sdanmcd case SADB_EXT_SUPPORTED_ENCRYPT: 21174867Spwernau (void) dump_ealg(algs[i].sadb_alg_id, file); 21183055Sdanmcd break; 21193055Sdanmcd } 21204867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 21215906Svk199839 " minbits=%u, maxbits=%u, ivlen=%u"), 21223055Sdanmcd algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits, 21233055Sdanmcd algs[i].sadb_alg_ivlen); 21245906Svk199839 if (exttype == SADB_EXT_SUPPORTED_ENCRYPT) 21255906Svk199839 (void) fprintf(file, dgettext(TEXT_DOMAIN, 21265906Svk199839 ", increment=%u"), algs[i].sadb_x_alg_increment); 21275906Svk199839 (void) fprintf(file, dgettext(TEXT_DOMAIN, ".\n")); 21283055Sdanmcd } 21293055Sdanmcd } 21303055Sdanmcd 21313055Sdanmcd /* 21323055Sdanmcd * Print an SADB_EXT_SPIRANGE extension. 21333055Sdanmcd */ 21343055Sdanmcd void 21354867Spwernau print_spirange(FILE *file, char *prefix, struct sadb_spirange *range) 21363055Sdanmcd { 21374867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 21384064Smarkfen "%sSPI Range, min=0x%x, max=0x%x\n"), prefix, 21393055Sdanmcd htonl(range->sadb_spirange_min), 21403055Sdanmcd htonl(range->sadb_spirange_max)); 21413055Sdanmcd } 21423055Sdanmcd 21433055Sdanmcd /* 21443055Sdanmcd * Print an SADB_X_EXT_KM_COOKIE extension. 21453055Sdanmcd */ 21463055Sdanmcd 21473055Sdanmcd void 21484867Spwernau print_kmc(FILE *file, char *prefix, struct sadb_x_kmc *kmc) 21493055Sdanmcd { 21503055Sdanmcd char *cookie_label; 21513055Sdanmcd 21523055Sdanmcd if ((cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie)) == 21533055Sdanmcd NULL) 21544064Smarkfen cookie_label = dgettext(TEXT_DOMAIN, "<Label not found.>"); 21553055Sdanmcd 21564867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 21574064Smarkfen "%sProtocol %u, cookie=\"%s\" (%u)\n"), prefix, 21583055Sdanmcd kmc->sadb_x_kmc_proto, cookie_label, kmc->sadb_x_kmc_cookie); 21593055Sdanmcd } 2160*6668Smarkfen /* 2161*6668Smarkfen * Print an SADB_X_EXT_PAIR extension. 2162*6668Smarkfen */ 2163*6668Smarkfen static void 2164*6668Smarkfen print_pair(FILE *file, char *prefix, struct sadb_x_pair *pair) 2165*6668Smarkfen { 2166*6668Smarkfen (void) fprintf(file, dgettext(TEXT_DOMAIN, "%sPaired with spi=0x%x\n"), 2167*6668Smarkfen prefix, ntohl(pair->sadb_x_pair_spi)); 2168*6668Smarkfen } 21693055Sdanmcd 21703055Sdanmcd /* 21713055Sdanmcd * Take a PF_KEY message pointed to buffer and print it. Useful for DUMP 21723055Sdanmcd * and GET. 21733055Sdanmcd */ 21743055Sdanmcd void 21754867Spwernau print_samsg(FILE *file, uint64_t *buffer, boolean_t want_timestamp, 21764867Spwernau boolean_t vflag, boolean_t ignore_nss) 21773055Sdanmcd { 21783055Sdanmcd uint64_t *current; 21793055Sdanmcd struct sadb_msg *samsg = (struct sadb_msg *)buffer; 21803055Sdanmcd struct sadb_ext *ext; 21813055Sdanmcd struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL; 21823055Sdanmcd int i; 21833055Sdanmcd time_t wallclock; 21843055Sdanmcd 21853055Sdanmcd (void) time(&wallclock); 21863055Sdanmcd 21874867Spwernau print_sadb_msg(file, samsg, want_timestamp ? wallclock : 0, vflag); 21883055Sdanmcd current = (uint64_t *)(samsg + 1); 21893055Sdanmcd while (current - buffer < samsg->sadb_msg_len) { 21903055Sdanmcd int lenbytes; 21913055Sdanmcd 21923055Sdanmcd ext = (struct sadb_ext *)current; 21933055Sdanmcd lenbytes = SADB_64TO8(ext->sadb_ext_len); 21943055Sdanmcd switch (ext->sadb_ext_type) { 21953055Sdanmcd case SADB_EXT_SA: 21964867Spwernau print_sa(file, dgettext(TEXT_DOMAIN, 21974064Smarkfen "SA: "), (struct sadb_sa *)current); 21983055Sdanmcd break; 21993055Sdanmcd /* 22003055Sdanmcd * Pluck out lifetimes and print them at the end. This is 22013055Sdanmcd * to show relative lifetimes. 22023055Sdanmcd */ 22033055Sdanmcd case SADB_EXT_LIFETIME_CURRENT: 22043055Sdanmcd currentlt = (struct sadb_lifetime *)current; 22053055Sdanmcd break; 22063055Sdanmcd case SADB_EXT_LIFETIME_HARD: 22073055Sdanmcd hardlt = (struct sadb_lifetime *)current; 22083055Sdanmcd break; 22093055Sdanmcd case SADB_EXT_LIFETIME_SOFT: 22103055Sdanmcd softlt = (struct sadb_lifetime *)current; 22113055Sdanmcd break; 22123055Sdanmcd 22133055Sdanmcd case SADB_EXT_ADDRESS_SRC: 22144867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "SRC: "), 22154867Spwernau (struct sadb_address *)current, ignore_nss); 22163055Sdanmcd break; 22173055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 22184867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "INS: "), 22194867Spwernau (struct sadb_address *)current, ignore_nss); 22203055Sdanmcd break; 22213055Sdanmcd case SADB_EXT_ADDRESS_DST: 22224867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "DST: "), 22234867Spwernau (struct sadb_address *)current, ignore_nss); 22243055Sdanmcd break; 22253055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 22264867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "IND: "), 22274867Spwernau (struct sadb_address *)current, ignore_nss); 22283055Sdanmcd break; 22293055Sdanmcd case SADB_EXT_KEY_AUTH: 22304867Spwernau print_key(file, dgettext(TEXT_DOMAIN, 22314064Smarkfen "AKY: "), (struct sadb_key *)current); 22323055Sdanmcd break; 22333055Sdanmcd case SADB_EXT_KEY_ENCRYPT: 22344867Spwernau print_key(file, dgettext(TEXT_DOMAIN, 22354064Smarkfen "EKY: "), (struct sadb_key *)current); 22363055Sdanmcd break; 22373055Sdanmcd case SADB_EXT_IDENTITY_SRC: 22384867Spwernau print_ident(file, dgettext(TEXT_DOMAIN, "SID: "), 22393055Sdanmcd (struct sadb_ident *)current); 22403055Sdanmcd break; 22413055Sdanmcd case SADB_EXT_IDENTITY_DST: 22424867Spwernau print_ident(file, dgettext(TEXT_DOMAIN, "DID: "), 22433055Sdanmcd (struct sadb_ident *)current); 22443055Sdanmcd break; 22453055Sdanmcd case SADB_EXT_SENSITIVITY: 22464867Spwernau print_sens(file, dgettext(TEXT_DOMAIN, "SNS: "), 22473055Sdanmcd (struct sadb_sens *)current); 22483055Sdanmcd break; 22493055Sdanmcd case SADB_EXT_PROPOSAL: 22504867Spwernau print_prop(file, dgettext(TEXT_DOMAIN, "PRP: "), 22513055Sdanmcd (struct sadb_prop *)current); 22523055Sdanmcd break; 22533055Sdanmcd case SADB_EXT_SUPPORTED_AUTH: 22544867Spwernau print_supp(file, dgettext(TEXT_DOMAIN, "SUA: "), 22553055Sdanmcd (struct sadb_supported *)current); 22563055Sdanmcd break; 22573055Sdanmcd case SADB_EXT_SUPPORTED_ENCRYPT: 22584867Spwernau print_supp(file, dgettext(TEXT_DOMAIN, "SUE: "), 22593055Sdanmcd (struct sadb_supported *)current); 22603055Sdanmcd break; 22613055Sdanmcd case SADB_EXT_SPIRANGE: 22624867Spwernau print_spirange(file, dgettext(TEXT_DOMAIN, "SPR: "), 22633055Sdanmcd (struct sadb_spirange *)current); 22643055Sdanmcd break; 22653055Sdanmcd case SADB_X_EXT_EPROP: 22664867Spwernau print_eprop(file, dgettext(TEXT_DOMAIN, "EPR: "), 22673055Sdanmcd (struct sadb_prop *)current); 22683055Sdanmcd break; 22693055Sdanmcd case SADB_X_EXT_KM_COOKIE: 22704867Spwernau print_kmc(file, dgettext(TEXT_DOMAIN, "KMC: "), 22713055Sdanmcd (struct sadb_x_kmc *)current); 22723055Sdanmcd break; 22733055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 22744867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "NRM: "), 22754867Spwernau (struct sadb_address *)current, ignore_nss); 22763055Sdanmcd break; 22773055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 22784867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "NLC: "), 22794867Spwernau (struct sadb_address *)current, ignore_nss); 22803055Sdanmcd break; 2281*6668Smarkfen case SADB_X_EXT_PAIR: 2282*6668Smarkfen print_pair(file, dgettext(TEXT_DOMAIN, "OTH: "), 2283*6668Smarkfen (struct sadb_x_pair *)current); 2284*6668Smarkfen break; 22853055Sdanmcd default: 22864867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 22873055Sdanmcd "UNK: Unknown ext. %d, len %d.\n"), 22883055Sdanmcd ext->sadb_ext_type, lenbytes); 22893055Sdanmcd for (i = 0; i < ext->sadb_ext_len; i++) 22904867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 22914064Smarkfen "UNK: 0x%llx\n"), ((uint64_t *)ext)[i]); 22923055Sdanmcd break; 22933055Sdanmcd } 22943055Sdanmcd current += (lenbytes == 0) ? 22953055Sdanmcd SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len; 22963055Sdanmcd } 22973055Sdanmcd /* 22983055Sdanmcd * Print lifetimes NOW. 22993055Sdanmcd */ 23003055Sdanmcd if (currentlt != NULL || hardlt != NULL || softlt != NULL) 23014867Spwernau print_lifetimes(file, wallclock, currentlt, hardlt, softlt, 23024867Spwernau vflag); 23033055Sdanmcd 23043055Sdanmcd if (current - buffer != samsg->sadb_msg_len) { 23054867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 23064867Spwernau "WARNING: insufficient buffer space or corrupt message.")); 23073055Sdanmcd } 23083055Sdanmcd 23094867Spwernau (void) fflush(file); /* Make sure our message is out there. */ 23103055Sdanmcd } 23113055Sdanmcd 23123055Sdanmcd /* 23133055Sdanmcd * save_XXX functions are used when "saving" the SA tables to either a 23143055Sdanmcd * file or standard output. They use the dump_XXX functions where needed, 23153055Sdanmcd * but mostly they use the rparseXXX functions. 23163055Sdanmcd */ 23173055Sdanmcd 23183055Sdanmcd /* 23193055Sdanmcd * Print save information for a lifetime extension. 23203055Sdanmcd * 23213055Sdanmcd * NOTE : It saves the lifetime in absolute terms. For example, if you 23223055Sdanmcd * had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though 23233055Sdanmcd * there may have been 59 seconds burned off the clock. 23243055Sdanmcd */ 23253055Sdanmcd boolean_t 23263055Sdanmcd save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile) 23273055Sdanmcd { 23283055Sdanmcd char *prefix; 23293055Sdanmcd 23303055Sdanmcd prefix = (lifetime->sadb_lifetime_exttype == SADB_EXT_LIFETIME_SOFT) ? 23313055Sdanmcd "soft" : "hard"; 23323055Sdanmcd 23333055Sdanmcd if (putc('\t', ofile) == EOF) 23343055Sdanmcd return (B_FALSE); 23353055Sdanmcd 23363055Sdanmcd if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile, 23373055Sdanmcd "%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0) 23383055Sdanmcd return (B_FALSE); 23393055Sdanmcd 23403055Sdanmcd if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile, 23413055Sdanmcd "%s_bytes %llu ", prefix, lifetime->sadb_lifetime_bytes) < 0) 23423055Sdanmcd return (B_FALSE); 23433055Sdanmcd 23443055Sdanmcd if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile, 23453055Sdanmcd "%s_addtime %llu ", prefix, lifetime->sadb_lifetime_addtime) < 0) 23463055Sdanmcd return (B_FALSE); 23473055Sdanmcd 23483055Sdanmcd if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile, 23493055Sdanmcd "%s_usetime %llu ", prefix, lifetime->sadb_lifetime_usetime) < 0) 23503055Sdanmcd return (B_FALSE); 23513055Sdanmcd 23523055Sdanmcd return (B_TRUE); 23533055Sdanmcd } 23543055Sdanmcd 23553055Sdanmcd /* 23563055Sdanmcd * Print save information for an address extension. 23573055Sdanmcd */ 23583055Sdanmcd boolean_t 23593055Sdanmcd save_address(struct sadb_address *addr, FILE *ofile) 23603055Sdanmcd { 23613055Sdanmcd char *printable_addr, buf[INET6_ADDRSTRLEN]; 23623055Sdanmcd const char *prefix, *pprefix; 23633055Sdanmcd struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1); 23643055Sdanmcd struct sockaddr_in *sin = (struct sockaddr_in *)sin6; 23653055Sdanmcd int af = sin->sin_family; 23663055Sdanmcd 23673055Sdanmcd /* 23683055Sdanmcd * Address-family reality check. 23693055Sdanmcd */ 23703055Sdanmcd if (af != AF_INET6 && af != AF_INET) 23713055Sdanmcd return (B_FALSE); 23723055Sdanmcd 23733055Sdanmcd switch (addr->sadb_address_exttype) { 23743055Sdanmcd case SADB_EXT_ADDRESS_SRC: 23753055Sdanmcd prefix = "src"; 23763055Sdanmcd pprefix = "sport"; 23773055Sdanmcd break; 23783055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 23793055Sdanmcd prefix = "isrc"; 23803055Sdanmcd pprefix = "isport"; 23813055Sdanmcd break; 23823055Sdanmcd case SADB_EXT_ADDRESS_DST: 23833055Sdanmcd prefix = "dst"; 23843055Sdanmcd pprefix = "dport"; 23853055Sdanmcd break; 23863055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 23873055Sdanmcd prefix = "idst"; 23883055Sdanmcd pprefix = "idport"; 23893055Sdanmcd break; 23903055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 23913055Sdanmcd prefix = "nat_loc "; 23923055Sdanmcd pprefix = "nat_lport"; 23933055Sdanmcd break; 23943055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 23953055Sdanmcd prefix = "nat_rem "; 23963055Sdanmcd pprefix = "nat_rport"; 23973055Sdanmcd break; 23983055Sdanmcd } 23993055Sdanmcd 24003055Sdanmcd if (fprintf(ofile, " %s ", prefix) < 0) 24013055Sdanmcd return (B_FALSE); 24023055Sdanmcd 24033055Sdanmcd /* 24043055Sdanmcd * Do not do address-to-name translation, given that we live in 24053055Sdanmcd * an age of names that explode into many addresses. 24063055Sdanmcd */ 24073055Sdanmcd printable_addr = (char *)inet_ntop(af, 24083055Sdanmcd (af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr, 24093055Sdanmcd buf, sizeof (buf)); 24103055Sdanmcd if (printable_addr == NULL) 24114064Smarkfen printable_addr = "Invalid IP address."; 24123055Sdanmcd if (fprintf(ofile, "%s", printable_addr) < 0) 24133055Sdanmcd return (B_FALSE); 24143055Sdanmcd if (addr->sadb_address_prefixlen != 0 && 24153055Sdanmcd !((addr->sadb_address_prefixlen == 32 && af == AF_INET) || 24164342Spwernau (addr->sadb_address_prefixlen == 128 && af == AF_INET6))) { 24173055Sdanmcd if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0) 24183055Sdanmcd return (B_FALSE); 24193055Sdanmcd } 24203055Sdanmcd 24213055Sdanmcd /* 24223055Sdanmcd * The port is in the same position for struct sockaddr_in and 24233055Sdanmcd * struct sockaddr_in6. We exploit that property here. 24243055Sdanmcd */ 24253055Sdanmcd if ((pprefix != NULL) && (sin->sin_port != 0)) 24263055Sdanmcd (void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port)); 24273055Sdanmcd 24283055Sdanmcd return (B_TRUE); 24293055Sdanmcd } 24303055Sdanmcd 24313055Sdanmcd /* 24323055Sdanmcd * Print save information for a key extension. Returns whether writing 24333055Sdanmcd * to the specified output file was successful or not. 24343055Sdanmcd */ 24353055Sdanmcd boolean_t 24363055Sdanmcd save_key(struct sadb_key *key, FILE *ofile) 24373055Sdanmcd { 24383055Sdanmcd char *prefix; 24393055Sdanmcd 24403055Sdanmcd if (putc('\t', ofile) == EOF) 24413055Sdanmcd return (B_FALSE); 24423055Sdanmcd 24433055Sdanmcd prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr"; 24443055Sdanmcd 24453055Sdanmcd if (fprintf(ofile, "%skey ", prefix) < 0) 24463055Sdanmcd return (B_FALSE); 24473055Sdanmcd 24483055Sdanmcd if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits, ofile) == -1) 24493055Sdanmcd return (B_FALSE); 24503055Sdanmcd 24513055Sdanmcd return (B_TRUE); 24523055Sdanmcd } 24533055Sdanmcd 24543055Sdanmcd /* 24553055Sdanmcd * Print save information for an identity extension. 24563055Sdanmcd */ 24573055Sdanmcd boolean_t 24583055Sdanmcd save_ident(struct sadb_ident *ident, FILE *ofile) 24593055Sdanmcd { 24603055Sdanmcd char *prefix; 24613055Sdanmcd 24623055Sdanmcd if (putc('\t', ofile) == EOF) 24633055Sdanmcd return (B_FALSE); 24643055Sdanmcd 24653055Sdanmcd prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" : 24663055Sdanmcd "dst"; 24673055Sdanmcd 24683055Sdanmcd if (fprintf(ofile, "%sidtype %s ", prefix, 24693055Sdanmcd rparseidtype(ident->sadb_ident_type)) < 0) 24703055Sdanmcd return (B_FALSE); 24713055Sdanmcd 24723055Sdanmcd if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN || 24733055Sdanmcd ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) { 24744064Smarkfen if (fprintf(ofile, dgettext(TEXT_DOMAIN, 24754064Smarkfen "<can-not-print>")) < 0) 24763055Sdanmcd return (B_FALSE); 24773055Sdanmcd } else { 24783055Sdanmcd if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0) 24793055Sdanmcd return (B_FALSE); 24803055Sdanmcd } 24813055Sdanmcd 24823055Sdanmcd return (B_TRUE); 24833055Sdanmcd } 24843055Sdanmcd 24853055Sdanmcd /* 24863055Sdanmcd * "Save" a security association to an output file. 24873055Sdanmcd * 24884064Smarkfen * NOTE the lack of calls to dgettext() because I'm outputting parseable stuff. 24893055Sdanmcd * ALSO NOTE that if you change keywords (see parsecmd()), you'll have to 24903055Sdanmcd * change them here as well. 24913055Sdanmcd */ 24923055Sdanmcd void 24933055Sdanmcd save_assoc(uint64_t *buffer, FILE *ofile) 24943055Sdanmcd { 24954064Smarkfen int terrno; 24964987Sdanmcd boolean_t seen_proto = B_FALSE, seen_iproto = B_FALSE; 24973055Sdanmcd uint64_t *current; 24983055Sdanmcd struct sadb_address *addr; 24993055Sdanmcd struct sadb_msg *samsg = (struct sadb_msg *)buffer; 25003055Sdanmcd struct sadb_ext *ext; 25013055Sdanmcd 25024064Smarkfen #define tidyup() \ 25034064Smarkfen terrno = errno; (void) fclose(ofile); errno = terrno; \ 25044064Smarkfen interactive = B_FALSE 25054064Smarkfen 25064064Smarkfen #define savenl() if (fputs(" \\\n", ofile) == EOF) \ 25074064Smarkfen { bail(dgettext(TEXT_DOMAIN, "savenl")); } 25083055Sdanmcd 25093055Sdanmcd if (fputs("# begin assoc\n", ofile) == EOF) 25104064Smarkfen bail(dgettext(TEXT_DOMAIN, 25114064Smarkfen "save_assoc: Opening comment of SA")); 25123055Sdanmcd if (fprintf(ofile, "add %s ", rparsesatype(samsg->sadb_msg_satype)) < 0) 25134064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_assoc: First line of SA")); 25143055Sdanmcd savenl(); 25153055Sdanmcd 25163055Sdanmcd current = (uint64_t *)(samsg + 1); 25173055Sdanmcd while (current - buffer < samsg->sadb_msg_len) { 25183055Sdanmcd struct sadb_sa *assoc; 25193055Sdanmcd 25203055Sdanmcd ext = (struct sadb_ext *)current; 25214987Sdanmcd addr = (struct sadb_address *)ext; /* Just in case... */ 25223055Sdanmcd switch (ext->sadb_ext_type) { 25233055Sdanmcd case SADB_EXT_SA: 25243055Sdanmcd assoc = (struct sadb_sa *)ext; 25253055Sdanmcd if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) { 25263055Sdanmcd if (fprintf(ofile, "# WARNING: SA was dying " 25273055Sdanmcd "or dead.\n") < 0) { 25284064Smarkfen tidyup(); 25294064Smarkfen bail(dgettext(TEXT_DOMAIN, 25304064Smarkfen "save_assoc: fprintf not mature")); 25313055Sdanmcd } 25323055Sdanmcd } 25333055Sdanmcd if (fprintf(ofile, " spi 0x%x ", 25344064Smarkfen ntohl(assoc->sadb_sa_spi)) < 0) { 25354064Smarkfen tidyup(); 25364064Smarkfen bail(dgettext(TEXT_DOMAIN, 25374064Smarkfen "save_assoc: fprintf spi")); 25384064Smarkfen } 25393055Sdanmcd if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) { 25403055Sdanmcd if (fprintf(ofile, "encr_alg %s ", 25413055Sdanmcd rparsealg(assoc->sadb_sa_encrypt, 25424342Spwernau IPSEC_PROTO_ESP)) < 0) { 25434064Smarkfen tidyup(); 25444064Smarkfen bail(dgettext(TEXT_DOMAIN, 25454064Smarkfen "save_assoc: fprintf encrypt")); 25464064Smarkfen } 25473055Sdanmcd } 25483055Sdanmcd if (assoc->sadb_sa_auth != SADB_AALG_NONE) { 25493055Sdanmcd if (fprintf(ofile, "auth_alg %s ", 25503055Sdanmcd rparsealg(assoc->sadb_sa_auth, 25514342Spwernau IPSEC_PROTO_AH)) < 0) { 25524064Smarkfen tidyup(); 25534064Smarkfen bail(dgettext(TEXT_DOMAIN, 25544064Smarkfen "save_assoc: fprintf auth")); 25554064Smarkfen } 25563055Sdanmcd } 25573055Sdanmcd if (fprintf(ofile, "replay %d ", 25584064Smarkfen assoc->sadb_sa_replay) < 0) { 25594064Smarkfen tidyup(); 25604064Smarkfen bail(dgettext(TEXT_DOMAIN, 25614064Smarkfen "save_assoc: fprintf replay")); 25624064Smarkfen } 25633055Sdanmcd if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC | 25643055Sdanmcd SADB_X_SAFLAGS_NATT_REM)) { 25654064Smarkfen if (fprintf(ofile, "encap udp") < 0) { 25664064Smarkfen tidyup(); 25674064Smarkfen bail(dgettext(TEXT_DOMAIN, 25684064Smarkfen "save_assoc: fprintf encap")); 25694064Smarkfen } 25703055Sdanmcd } 25713055Sdanmcd savenl(); 25723055Sdanmcd break; 25733055Sdanmcd case SADB_EXT_LIFETIME_HARD: 25743055Sdanmcd case SADB_EXT_LIFETIME_SOFT: 25754064Smarkfen if (!save_lifetime((struct sadb_lifetime *)ext, 25764064Smarkfen ofile)) { 25774064Smarkfen tidyup(); 25784064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_lifetime")); 25794064Smarkfen } 25803055Sdanmcd savenl(); 25813055Sdanmcd break; 25823055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 25833055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 25844987Sdanmcd if (!seen_iproto && addr->sadb_address_proto) { 25854987Sdanmcd (void) fprintf(ofile, " iproto %d", 25864987Sdanmcd addr->sadb_address_proto); 25874987Sdanmcd savenl(); 25884987Sdanmcd seen_iproto = B_TRUE; 25894987Sdanmcd } 25904987Sdanmcd goto skip_srcdst; /* Hack to avoid cases below... */ 25914987Sdanmcd /* FALLTHRU */ 25924987Sdanmcd case SADB_EXT_ADDRESS_SRC: 25934987Sdanmcd case SADB_EXT_ADDRESS_DST: 25943055Sdanmcd if (!seen_proto && addr->sadb_address_proto) { 25953055Sdanmcd (void) fprintf(ofile, " proto %d", 25963055Sdanmcd addr->sadb_address_proto); 25973055Sdanmcd savenl(); 25984987Sdanmcd seen_proto = B_TRUE; 25993055Sdanmcd } 26004987Sdanmcd /* FALLTHRU */ 26014987Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 26024987Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 26034987Sdanmcd skip_srcdst: 26044064Smarkfen if (!save_address(addr, ofile)) { 26054064Smarkfen tidyup(); 26064064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_address")); 26074064Smarkfen } 26083055Sdanmcd savenl(); 26093055Sdanmcd break; 26103055Sdanmcd case SADB_EXT_KEY_AUTH: 26113055Sdanmcd case SADB_EXT_KEY_ENCRYPT: 26124064Smarkfen if (!save_key((struct sadb_key *)ext, ofile)) { 26134064Smarkfen tidyup(); 26144064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_address")); 26154064Smarkfen } 26163055Sdanmcd savenl(); 26173055Sdanmcd break; 26183055Sdanmcd case SADB_EXT_IDENTITY_SRC: 26193055Sdanmcd case SADB_EXT_IDENTITY_DST: 26204064Smarkfen if (!save_ident((struct sadb_ident *)ext, ofile)) { 26214064Smarkfen tidyup(); 26224064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_address")); 26234064Smarkfen } 26243055Sdanmcd savenl(); 26253055Sdanmcd break; 26263055Sdanmcd case SADB_EXT_SENSITIVITY: 26273055Sdanmcd default: 26283055Sdanmcd /* Skip over irrelevant extensions. */ 26293055Sdanmcd break; 26303055Sdanmcd } 26313055Sdanmcd current += ext->sadb_ext_len; 26323055Sdanmcd } 26333055Sdanmcd 26344064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) { 26354064Smarkfen tidyup(); 26364064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs")); 26374064Smarkfen } 26383055Sdanmcd } 26393055Sdanmcd 26403055Sdanmcd /* 26413055Sdanmcd * Open the output file for the "save" command. 26423055Sdanmcd */ 26433055Sdanmcd FILE * 26443055Sdanmcd opensavefile(char *filename) 26453055Sdanmcd { 26463055Sdanmcd int fd; 26473055Sdanmcd FILE *retval; 26483055Sdanmcd struct stat buf; 26493055Sdanmcd 26503055Sdanmcd /* 26513055Sdanmcd * If the user specifies "-" or doesn't give a filename, then 26523055Sdanmcd * dump to stdout. Make sure to document the dangers of files 26533055Sdanmcd * that are NFS, directing your output to strange places, etc. 26543055Sdanmcd */ 26553055Sdanmcd if (filename == NULL || strcmp("-", filename) == 0) 26563055Sdanmcd return (stdout); 26573055Sdanmcd 26583055Sdanmcd /* 26593055Sdanmcd * open the file with the create bits set. Since I check for 26603055Sdanmcd * real UID == root in main(), I won't worry about the ownership 26613055Sdanmcd * problem. 26623055Sdanmcd */ 26633055Sdanmcd fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR); 26643055Sdanmcd if (fd == -1) { 26653055Sdanmcd if (errno != EEXIST) 26664064Smarkfen bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN, 26674064Smarkfen "open error"), 26683055Sdanmcd strerror(errno)); 26693055Sdanmcd fd = open(filename, O_WRONLY | O_TRUNC, 0); 26703055Sdanmcd if (fd == -1) 26714064Smarkfen bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN, 26724064Smarkfen "open error"), strerror(errno)); 26733055Sdanmcd if (fstat(fd, &buf) == -1) { 26743055Sdanmcd (void) close(fd); 26753055Sdanmcd bail_msg("%s fstat: %s", filename, strerror(errno)); 26763055Sdanmcd } 26773055Sdanmcd if (S_ISREG(buf.st_mode) && 26783055Sdanmcd ((buf.st_mode & S_IAMB) != S_IRUSR)) { 26794064Smarkfen warnx(dgettext(TEXT_DOMAIN, 26804064Smarkfen "WARNING: Save file already exists with " 26814064Smarkfen "permission %o."), buf.st_mode & S_IAMB); 26824064Smarkfen warnx(dgettext(TEXT_DOMAIN, 26834064Smarkfen "Normal users may be able to read IPsec " 26844064Smarkfen "keying material.")); 26853055Sdanmcd } 26863055Sdanmcd } 26873055Sdanmcd 26883055Sdanmcd /* Okay, we have an FD. Assign it to a stdio FILE pointer. */ 26893055Sdanmcd retval = fdopen(fd, "w"); 26903055Sdanmcd if (retval == NULL) { 26913055Sdanmcd (void) close(fd); 26924064Smarkfen bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN, 26934064Smarkfen "fdopen error"), strerror(errno)); 26943055Sdanmcd } 26953055Sdanmcd return (retval); 26963055Sdanmcd } 26973055Sdanmcd 26983055Sdanmcd const char * 26993055Sdanmcd do_inet_ntop(const void *addr, char *cp, size_t size) 27003055Sdanmcd { 27013055Sdanmcd boolean_t isv4; 27023055Sdanmcd struct in6_addr *inaddr6 = (struct in6_addr *)addr; 27033055Sdanmcd struct in_addr inaddr; 27043055Sdanmcd 27053055Sdanmcd if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) { 27063055Sdanmcd IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr); 27073055Sdanmcd } 27083055Sdanmcd 27093055Sdanmcd return (inet_ntop(isv4 ? AF_INET : AF_INET6, 27103055Sdanmcd isv4 ? (void *)&inaddr : inaddr6, cp, size)); 27113055Sdanmcd } 27123055Sdanmcd 27133055Sdanmcd char numprint[NBUF_SIZE]; 27143055Sdanmcd 27153055Sdanmcd /* 27163055Sdanmcd * Parse and reverse parse a specific SA type (AH, ESP, etc.). 27173055Sdanmcd */ 27183055Sdanmcd static struct typetable { 27193055Sdanmcd char *type; 27203055Sdanmcd int token; 27213055Sdanmcd } type_table[] = { 27223055Sdanmcd {"all", SADB_SATYPE_UNSPEC}, 27233055Sdanmcd {"ah", SADB_SATYPE_AH}, 27243055Sdanmcd {"esp", SADB_SATYPE_ESP}, 27253055Sdanmcd /* PF_KEY NOTE: More to come if net/pfkeyv2.h gets updated. */ 27263055Sdanmcd {NULL, 0} /* Token value is irrelevant for this entry. */ 27273055Sdanmcd }; 27283055Sdanmcd 27293055Sdanmcd char * 27303055Sdanmcd rparsesatype(int type) 27313055Sdanmcd { 27323055Sdanmcd struct typetable *tt = type_table; 27333055Sdanmcd 27343055Sdanmcd while (tt->type != NULL && type != tt->token) 27353055Sdanmcd tt++; 27363055Sdanmcd 27373055Sdanmcd if (tt->type == NULL) { 27383055Sdanmcd (void) snprintf(numprint, NBUF_SIZE, "%d", type); 27393055Sdanmcd } else { 27403055Sdanmcd return (tt->type); 27413055Sdanmcd } 27423055Sdanmcd 27433055Sdanmcd return (numprint); 27443055Sdanmcd } 27453055Sdanmcd 27463055Sdanmcd 27473055Sdanmcd /* 27483055Sdanmcd * Return a string containing the name of the specified numerical algorithm 27493055Sdanmcd * identifier. 27503055Sdanmcd */ 27513055Sdanmcd char * 27523055Sdanmcd rparsealg(uint8_t alg, int proto_num) 27533055Sdanmcd { 27543055Sdanmcd static struct ipsecalgent *holder = NULL; /* we're single-threaded */ 27553055Sdanmcd 27563055Sdanmcd if (holder != NULL) 27573055Sdanmcd freeipsecalgent(holder); 27583055Sdanmcd 27593055Sdanmcd holder = getipsecalgbynum(alg, proto_num, NULL); 27603055Sdanmcd if (holder == NULL) { 27613055Sdanmcd (void) snprintf(numprint, NBUF_SIZE, "%d", alg); 27623055Sdanmcd return (numprint); 27633055Sdanmcd } 27643055Sdanmcd 27653055Sdanmcd return (*(holder->a_names)); 27663055Sdanmcd } 27673055Sdanmcd 27683055Sdanmcd /* 27693055Sdanmcd * Parse and reverse parse out a source/destination ID type. 27703055Sdanmcd */ 27713055Sdanmcd static struct idtypes { 27723055Sdanmcd char *idtype; 27733055Sdanmcd uint8_t retval; 27743055Sdanmcd } idtypes[] = { 27753055Sdanmcd {"prefix", SADB_IDENTTYPE_PREFIX}, 27763055Sdanmcd {"fqdn", SADB_IDENTTYPE_FQDN}, 27773055Sdanmcd {"domain", SADB_IDENTTYPE_FQDN}, 27783055Sdanmcd {"domainname", SADB_IDENTTYPE_FQDN}, 27793055Sdanmcd {"user_fqdn", SADB_IDENTTYPE_USER_FQDN}, 27803055Sdanmcd {"mailbox", SADB_IDENTTYPE_USER_FQDN}, 27813055Sdanmcd {"der_dn", SADB_X_IDENTTYPE_DN}, 27823055Sdanmcd {"der_gn", SADB_X_IDENTTYPE_GN}, 27833055Sdanmcd {NULL, 0} 27843055Sdanmcd }; 27853055Sdanmcd 27863055Sdanmcd char * 27873055Sdanmcd rparseidtype(uint16_t type) 27883055Sdanmcd { 27893055Sdanmcd struct idtypes *idp; 27903055Sdanmcd 27913055Sdanmcd for (idp = idtypes; idp->idtype != NULL; idp++) { 27923055Sdanmcd if (type == idp->retval) 27933055Sdanmcd return (idp->idtype); 27943055Sdanmcd } 27953055Sdanmcd 27963055Sdanmcd (void) snprintf(numprint, NBUF_SIZE, "%d", type); 27973055Sdanmcd return (numprint); 27983055Sdanmcd } 27994235Smarkfen 28004235Smarkfen /* 28014235Smarkfen * This is a general purpose exit function, calling functions can specify an 28024235Smarkfen * error type. If the command calling this function was started by smf(5) the 28034235Smarkfen * error type could be used as a hint to the restarter. In the future this 28044235Smarkfen * function could be used to do something more intelligent with a process that 28054235Smarkfen * encounters an error. 28064235Smarkfen * 28074235Smarkfen * The function will handle an optional variable args error message, this 28084235Smarkfen * will be written to the error stream, typically a log file or stderr. 28094235Smarkfen */ 28104235Smarkfen void 28114235Smarkfen ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...) 28124235Smarkfen { 28134235Smarkfen int exit_status; 28144235Smarkfen va_list args; 28154235Smarkfen 28164235Smarkfen if (fp == NULL) 28174235Smarkfen fp = stderr; 28184235Smarkfen if (fmt != NULL) { 28194235Smarkfen va_start(args, fmt); 28204235Smarkfen vwarnxfp(fp, fmt, args); 28214235Smarkfen va_end(args); 28224235Smarkfen } 28234235Smarkfen 28244235Smarkfen if (fmri == NULL) { 28254235Smarkfen /* Command being run directly from a shell. */ 28264235Smarkfen switch (type) { 28274235Smarkfen case SERVICE_EXIT_OK: 28284235Smarkfen exit_status = 0; 28294235Smarkfen break; 28304235Smarkfen case SERVICE_DEGRADE: 28314235Smarkfen return; 28324235Smarkfen break; 28334235Smarkfen case SERVICE_BADPERM: 28344235Smarkfen case SERVICE_BADCONF: 28354235Smarkfen case SERVICE_MAINTAIN: 28364235Smarkfen case SERVICE_DISABLE: 28374235Smarkfen case SERVICE_FATAL: 28384235Smarkfen case SERVICE_RESTART: 28394235Smarkfen warnxfp(fp, "Fatal error - exiting."); 28404235Smarkfen exit_status = 1; 28414235Smarkfen break; 28424235Smarkfen } 28434235Smarkfen } else { 28444235Smarkfen /* Command being run as a smf(5) method. */ 28454235Smarkfen switch (type) { 28464235Smarkfen case SERVICE_EXIT_OK: 28474235Smarkfen exit_status = SMF_EXIT_OK; 28484235Smarkfen break; 28494235Smarkfen case SERVICE_DEGRADE: 28504235Smarkfen return; 28514235Smarkfen break; 28524235Smarkfen case SERVICE_BADPERM: 28534235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28544235Smarkfen "Permission error with %s."), fmri); 28554235Smarkfen exit_status = SMF_EXIT_ERR_PERM; 28564235Smarkfen break; 28574235Smarkfen case SERVICE_BADCONF: 28584235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28594235Smarkfen "Bad configuration of service %s."), fmri); 28604235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28614235Smarkfen break; 28624235Smarkfen case SERVICE_MAINTAIN: 28634235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28644235Smarkfen "Service %s needs maintenance."), fmri); 28654235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28664235Smarkfen break; 28674235Smarkfen case SERVICE_DISABLE: 28684235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28694235Smarkfen break; 28704235Smarkfen case SERVICE_FATAL: 28714235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28724235Smarkfen "Service %s fatal error."), fmri); 28734235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28744235Smarkfen break; 28754235Smarkfen case SERVICE_RESTART: 28764235Smarkfen exit_status = 1; 28774235Smarkfen break; 28784235Smarkfen } 28794235Smarkfen } 28804235Smarkfen (void) fflush(fp); 28814235Smarkfen (void) fclose(fp); 28824235Smarkfen exit(exit_status); 28834235Smarkfen } 2884