10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53055Sdanmcd * Common Development and Distribution License (the "License"). 63055Sdanmcd * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*5906Svk199839 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <unistd.h> 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <stdarg.h> 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/stat.h> 340Sstevel@tonic-gate #include <fcntl.h> 350Sstevel@tonic-gate #include <sys/sysconf.h> 360Sstevel@tonic-gate #include <strings.h> 370Sstevel@tonic-gate #include <ctype.h> 380Sstevel@tonic-gate #include <errno.h> 390Sstevel@tonic-gate #include <sys/socket.h> 400Sstevel@tonic-gate #include <netdb.h> 410Sstevel@tonic-gate #include <netinet/in.h> 420Sstevel@tonic-gate #include <arpa/inet.h> 430Sstevel@tonic-gate #include <net/pfkeyv2.h> 440Sstevel@tonic-gate #include <net/pfpolicy.h> 450Sstevel@tonic-gate #include <libintl.h> 460Sstevel@tonic-gate #include <setjmp.h> 470Sstevel@tonic-gate #include <libgen.h> 484235Smarkfen #include <libscf.h> 490Sstevel@tonic-gate 500Sstevel@tonic-gate #include "ipsec_util.h" 510Sstevel@tonic-gate #include "ikedoor.h" 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* 540Sstevel@tonic-gate * This file contains support functions that are shared by the ipsec 554867Spwernau * utilities and daemons including ipseckey(1m), ikeadm(1m) and in.iked(1m). 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate 584867Spwernau 594867Spwernau #define EFD(file) (((file) == stdout) ? stderr : (file)) 604867Spwernau 610Sstevel@tonic-gate /* Set standard default/initial values for globals... */ 620Sstevel@tonic-gate boolean_t pflag = B_FALSE; /* paranoid w.r.t. printing keying material */ 630Sstevel@tonic-gate boolean_t nflag = B_FALSE; /* avoid nameservice? */ 640Sstevel@tonic-gate boolean_t interactive = B_FALSE; /* util not running on cmdline */ 650Sstevel@tonic-gate boolean_t readfile = B_FALSE; /* cmds are being read from a file */ 660Sstevel@tonic-gate uint_t lineno = 0; /* track location if reading cmds from file */ 674235Smarkfen uint_t lines_added = 0; 684235Smarkfen uint_t lines_parsed = 0; 690Sstevel@tonic-gate jmp_buf env; /* for error recovery in interactive/readfile modes */ 704235Smarkfen char *my_fmri = NULL; 714235Smarkfen FILE *debugfile = stderr; 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* 740Sstevel@tonic-gate * Print errno and exit if cmdline or readfile, reset state if interactive 754064Smarkfen * The error string *what should be dgettext()'d before calling bail(). 760Sstevel@tonic-gate */ 770Sstevel@tonic-gate void 780Sstevel@tonic-gate bail(char *what) 790Sstevel@tonic-gate { 800Sstevel@tonic-gate if (errno != 0) 810Sstevel@tonic-gate warn(what); 820Sstevel@tonic-gate else 834235Smarkfen warnx(dgettext(TEXT_DOMAIN, "Error: %s"), what); 840Sstevel@tonic-gate if (readfile) { 854235Smarkfen return; 860Sstevel@tonic-gate } 870Sstevel@tonic-gate if (interactive && !readfile) 880Sstevel@tonic-gate longjmp(env, 2); 894235Smarkfen EXIT_FATAL(NULL); 900Sstevel@tonic-gate } 910Sstevel@tonic-gate 920Sstevel@tonic-gate /* 930Sstevel@tonic-gate * Print caller-supplied variable-arg error msg, then exit if cmdline or 940Sstevel@tonic-gate * readfile, or reset state if interactive. 950Sstevel@tonic-gate */ 960Sstevel@tonic-gate /*PRINTFLIKE1*/ 970Sstevel@tonic-gate void 980Sstevel@tonic-gate bail_msg(char *fmt, ...) 990Sstevel@tonic-gate { 1000Sstevel@tonic-gate va_list ap; 1010Sstevel@tonic-gate char msgbuf[BUFSIZ]; 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate va_start(ap, fmt); 1040Sstevel@tonic-gate (void) vsnprintf(msgbuf, BUFSIZ, fmt, ap); 1050Sstevel@tonic-gate va_end(ap); 1060Sstevel@tonic-gate if (readfile) 1074064Smarkfen warnx(dgettext(TEXT_DOMAIN, 1084064Smarkfen "ERROR on line %u:\n%s\n"), lineno, msgbuf); 1090Sstevel@tonic-gate else 1104064Smarkfen warnx(dgettext(TEXT_DOMAIN, "ERROR: %s\n"), msgbuf); 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate if (interactive && !readfile) 1130Sstevel@tonic-gate longjmp(env, 1); 1140Sstevel@tonic-gate 1154235Smarkfen EXIT_FATAL(NULL); 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * dump_XXX functions produce ASCII output from various structures. 1210Sstevel@tonic-gate * 1220Sstevel@tonic-gate * Because certain errors need to do this to stderr, dump_XXX functions 1230Sstevel@tonic-gate * take a FILE pointer. 1240Sstevel@tonic-gate * 1250Sstevel@tonic-gate * If an error occured while writing to the specified file, these 1260Sstevel@tonic-gate * functions return -1, zero otherwise. 1270Sstevel@tonic-gate */ 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate int 1303055Sdanmcd dump_sockaddr(struct sockaddr *sa, uint8_t prefixlen, boolean_t addr_only, 1314867Spwernau FILE *where, boolean_t ignore_nss) 1320Sstevel@tonic-gate { 1330Sstevel@tonic-gate struct sockaddr_in *sin; 1340Sstevel@tonic-gate struct sockaddr_in6 *sin6; 1350Sstevel@tonic-gate char *printable_addr, *protocol; 1360Sstevel@tonic-gate uint8_t *addrptr; 1373055Sdanmcd /* Add 4 chars to hold '/nnn' for prefixes. */ 1383055Sdanmcd char storage[INET6_ADDRSTRLEN + 4]; 1390Sstevel@tonic-gate uint16_t port; 1400Sstevel@tonic-gate boolean_t unspec; 1410Sstevel@tonic-gate struct hostent *hp; 1420Sstevel@tonic-gate int getipnode_errno, addrlen; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate switch (sa->sa_family) { 1450Sstevel@tonic-gate case AF_INET: 1460Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 1470Sstevel@tonic-gate sin = (struct sockaddr_in *)sa; 1480Sstevel@tonic-gate addrptr = (uint8_t *)&sin->sin_addr; 1490Sstevel@tonic-gate port = sin->sin_port; 1500Sstevel@tonic-gate protocol = "AF_INET"; 1510Sstevel@tonic-gate unspec = (sin->sin_addr.s_addr == 0); 1520Sstevel@tonic-gate addrlen = sizeof (sin->sin_addr); 1530Sstevel@tonic-gate break; 1540Sstevel@tonic-gate case AF_INET6: 1550Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 1560Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)sa; 1570Sstevel@tonic-gate addrptr = (uint8_t *)&sin6->sin6_addr; 1580Sstevel@tonic-gate port = sin6->sin6_port; 1590Sstevel@tonic-gate protocol = "AF_INET6"; 1600Sstevel@tonic-gate unspec = IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr); 1610Sstevel@tonic-gate addrlen = sizeof (sin6->sin6_addr); 1620Sstevel@tonic-gate break; 1630Sstevel@tonic-gate default: 1640Sstevel@tonic-gate return (0); 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate if (inet_ntop(sa->sa_family, addrptr, storage, INET6_ADDRSTRLEN) == 1680Sstevel@tonic-gate NULL) { 1694064Smarkfen printable_addr = dgettext(TEXT_DOMAIN, "Invalid IP address."); 1700Sstevel@tonic-gate } else { 1713055Sdanmcd char prefix[5]; /* "/nnn" with terminator. */ 1723055Sdanmcd 1733055Sdanmcd (void) snprintf(prefix, sizeof (prefix), "/%d", prefixlen); 1740Sstevel@tonic-gate printable_addr = storage; 1753055Sdanmcd if (prefixlen != 0) { 1763055Sdanmcd (void) strlcat(printable_addr, prefix, 1773055Sdanmcd sizeof (storage)); 1783055Sdanmcd } 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate if (addr_only) { 1810Sstevel@tonic-gate if (fprintf(where, "%s", printable_addr) < 0) 1820Sstevel@tonic-gate return (-1); 1830Sstevel@tonic-gate } else { 1844064Smarkfen if (fprintf(where, dgettext(TEXT_DOMAIN, 1854064Smarkfen "%s: port %d, %s"), protocol, 1860Sstevel@tonic-gate ntohs(port), printable_addr) < 0) 1870Sstevel@tonic-gate return (-1); 1884867Spwernau if (ignore_nss == B_FALSE) { 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * Do AF_independent reverse hostname lookup here. 1910Sstevel@tonic-gate */ 1920Sstevel@tonic-gate if (unspec) { 1930Sstevel@tonic-gate if (fprintf(where, 1944064Smarkfen dgettext(TEXT_DOMAIN, 1954064Smarkfen " <unspecified>")) < 0) 1960Sstevel@tonic-gate return (-1); 1970Sstevel@tonic-gate } else { 1980Sstevel@tonic-gate hp = getipnodebyaddr((char *)addrptr, addrlen, 1990Sstevel@tonic-gate sa->sa_family, &getipnode_errno); 2000Sstevel@tonic-gate if (hp != NULL) { 2010Sstevel@tonic-gate if (fprintf(where, 2020Sstevel@tonic-gate " (%s)", hp->h_name) < 0) 2030Sstevel@tonic-gate return (-1); 2040Sstevel@tonic-gate freehostent(hp); 2050Sstevel@tonic-gate } else { 2060Sstevel@tonic-gate if (fprintf(where, 2074064Smarkfen dgettext(TEXT_DOMAIN, 2084064Smarkfen " <unknown>")) < 0) 2090Sstevel@tonic-gate return (-1); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate if (fputs(".\n", where) == EOF) 2140Sstevel@tonic-gate return (-1); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate return (0); 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * Dump a key and bitlen 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate int 2230Sstevel@tonic-gate dump_key(uint8_t *keyp, uint_t bitlen, FILE *where) 2240Sstevel@tonic-gate { 2250Sstevel@tonic-gate int numbytes; 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate numbytes = SADB_1TO8(bitlen); 2280Sstevel@tonic-gate /* The & 0x7 is to check for leftover bits. */ 2290Sstevel@tonic-gate if ((bitlen & 0x7) != 0) 2300Sstevel@tonic-gate numbytes++; 2310Sstevel@tonic-gate while (numbytes-- != 0) { 2320Sstevel@tonic-gate if (pflag) { 2330Sstevel@tonic-gate /* Print no keys if paranoid */ 2340Sstevel@tonic-gate if (fprintf(where, "XX") < 0) 2350Sstevel@tonic-gate return (-1); 2360Sstevel@tonic-gate } else { 2370Sstevel@tonic-gate if (fprintf(where, "%02x", *keyp++) < 0) 2380Sstevel@tonic-gate return (-1); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate if (fprintf(where, "/%u", bitlen) < 0) 2420Sstevel@tonic-gate return (-1); 2430Sstevel@tonic-gate return (0); 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * Print an authentication or encryption algorithm 2480Sstevel@tonic-gate */ 2490Sstevel@tonic-gate static int 2500Sstevel@tonic-gate dump_generic_alg(uint8_t alg_num, int proto_num, FILE *where) 2510Sstevel@tonic-gate { 2520Sstevel@tonic-gate struct ipsecalgent *alg; 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate alg = getipsecalgbynum(alg_num, proto_num, NULL); 2550Sstevel@tonic-gate if (alg == NULL) { 2564064Smarkfen if (fprintf(where, dgettext(TEXT_DOMAIN, 2574064Smarkfen "<unknown %u>"), alg_num) < 0) 2580Sstevel@tonic-gate return (-1); 2590Sstevel@tonic-gate return (0); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate /* 2630Sstevel@tonic-gate * Special-case <none> for backward output compat. 2640Sstevel@tonic-gate * Assume that SADB_AALG_NONE == SADB_EALG_NONE. 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate if (alg_num == SADB_AALG_NONE) { 2674064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, 2684064Smarkfen "<none>"), where) == EOF) 2690Sstevel@tonic-gate return (-1); 2700Sstevel@tonic-gate } else { 2710Sstevel@tonic-gate if (fputs(alg->a_names[0], where) == EOF) 2720Sstevel@tonic-gate return (-1); 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate freeipsecalgent(alg); 2760Sstevel@tonic-gate return (0); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate int 2800Sstevel@tonic-gate dump_aalg(uint8_t aalg, FILE *where) 2810Sstevel@tonic-gate { 2820Sstevel@tonic-gate return (dump_generic_alg(aalg, IPSEC_PROTO_AH, where)); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate int 2860Sstevel@tonic-gate dump_ealg(uint8_t ealg, FILE *where) 2870Sstevel@tonic-gate { 2880Sstevel@tonic-gate return (dump_generic_alg(ealg, IPSEC_PROTO_ESP, where)); 2890Sstevel@tonic-gate } 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate /* 2920Sstevel@tonic-gate * Print an SADB_IDENTTYPE string 2930Sstevel@tonic-gate * 2940Sstevel@tonic-gate * Also return TRUE if the actual ident may be printed, FALSE if not. 2950Sstevel@tonic-gate * 2960Sstevel@tonic-gate * If rc is not NULL, set its value to -1 if an error occured while writing 2970Sstevel@tonic-gate * to the specified file, zero otherwise. 2980Sstevel@tonic-gate */ 2990Sstevel@tonic-gate boolean_t 3000Sstevel@tonic-gate dump_sadb_idtype(uint8_t idtype, FILE *where, int *rc) 3010Sstevel@tonic-gate { 3020Sstevel@tonic-gate boolean_t canprint = B_TRUE; 3030Sstevel@tonic-gate int rc_val = 0; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate switch (idtype) { 3060Sstevel@tonic-gate case SADB_IDENTTYPE_PREFIX: 3074064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "prefix"), where) == EOF) 3080Sstevel@tonic-gate rc_val = -1; 3090Sstevel@tonic-gate break; 3100Sstevel@tonic-gate case SADB_IDENTTYPE_FQDN: 3114064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "FQDN"), where) == EOF) 3120Sstevel@tonic-gate rc_val = -1; 3130Sstevel@tonic-gate break; 3140Sstevel@tonic-gate case SADB_IDENTTYPE_USER_FQDN: 3154064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, 3164064Smarkfen "user-FQDN (mbox)"), where) == EOF) 3170Sstevel@tonic-gate rc_val = -1; 3180Sstevel@tonic-gate break; 3190Sstevel@tonic-gate case SADB_X_IDENTTYPE_DN: 3204064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Distinguished Name"), 3210Sstevel@tonic-gate where) == EOF) 3220Sstevel@tonic-gate rc_val = -1; 3230Sstevel@tonic-gate canprint = B_FALSE; 3240Sstevel@tonic-gate break; 3250Sstevel@tonic-gate case SADB_X_IDENTTYPE_GN: 3264064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Generic Name"), 3274064Smarkfen where) == EOF) 3280Sstevel@tonic-gate rc_val = -1; 3290Sstevel@tonic-gate canprint = B_FALSE; 3300Sstevel@tonic-gate break; 3310Sstevel@tonic-gate case SADB_X_IDENTTYPE_KEY_ID: 3324064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "Generic key id"), 3334064Smarkfen where) == EOF) 3340Sstevel@tonic-gate rc_val = -1; 3350Sstevel@tonic-gate break; 3360Sstevel@tonic-gate case SADB_X_IDENTTYPE_ADDR_RANGE: 3374064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "Address range"), where) == EOF) 3380Sstevel@tonic-gate rc_val = -1; 3390Sstevel@tonic-gate break; 3400Sstevel@tonic-gate default: 3414064Smarkfen if (fprintf(where, dgettext(TEXT_DOMAIN, 3424064Smarkfen "<unknown %u>"), idtype) < 0) 3430Sstevel@tonic-gate rc_val = -1; 3440Sstevel@tonic-gate break; 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate if (rc != NULL) 3480Sstevel@tonic-gate *rc = rc_val; 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate return (canprint); 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate /* 3540Sstevel@tonic-gate * Slice an argv/argc vector from an interactive line or a read-file line. 3550Sstevel@tonic-gate */ 3560Sstevel@tonic-gate static int 3570Sstevel@tonic-gate create_argv(char *ibuf, int *newargc, char ***thisargv) 3580Sstevel@tonic-gate { 3590Sstevel@tonic-gate unsigned int argvlen = START_ARG; 3600Sstevel@tonic-gate char **current; 3610Sstevel@tonic-gate boolean_t firstchar = B_TRUE; 3620Sstevel@tonic-gate boolean_t inquotes = B_FALSE; 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate *thisargv = malloc(sizeof (char *) * argvlen); 3650Sstevel@tonic-gate if ((*thisargv) == NULL) 3660Sstevel@tonic-gate return (MEMORY_ALLOCATION); 3670Sstevel@tonic-gate current = *thisargv; 3680Sstevel@tonic-gate *current = NULL; 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate for (; *ibuf != '\0'; ibuf++) { 3710Sstevel@tonic-gate if (isspace(*ibuf)) { 3720Sstevel@tonic-gate if (inquotes) { 3730Sstevel@tonic-gate continue; 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate if (*current != NULL) { 3760Sstevel@tonic-gate *ibuf = '\0'; 3770Sstevel@tonic-gate current++; 3780Sstevel@tonic-gate if (*thisargv + argvlen == current) { 3790Sstevel@tonic-gate /* Regrow ***thisargv. */ 3800Sstevel@tonic-gate if (argvlen == TOO_MANY_ARGS) { 3810Sstevel@tonic-gate free(*thisargv); 3820Sstevel@tonic-gate return (TOO_MANY_TOKENS); 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate /* Double the allocation. */ 3850Sstevel@tonic-gate current = realloc(*thisargv, 3860Sstevel@tonic-gate sizeof (char *) * (argvlen << 1)); 3870Sstevel@tonic-gate if (current == NULL) { 3880Sstevel@tonic-gate free(*thisargv); 3890Sstevel@tonic-gate return (MEMORY_ALLOCATION); 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate *thisargv = current; 3920Sstevel@tonic-gate current += argvlen; 3930Sstevel@tonic-gate argvlen <<= 1; /* Double the size. */ 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate *current = NULL; 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate } else { 3980Sstevel@tonic-gate if (firstchar) { 3990Sstevel@tonic-gate firstchar = B_FALSE; 4004235Smarkfen if (*ibuf == COMMENT_CHAR || *ibuf == '\n') { 4010Sstevel@tonic-gate free(*thisargv); 4020Sstevel@tonic-gate return (COMMENT_LINE); 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate if (*ibuf == QUOTE_CHAR) { 4060Sstevel@tonic-gate if (inquotes) { 4070Sstevel@tonic-gate inquotes = B_FALSE; 4080Sstevel@tonic-gate *ibuf = '\0'; 4090Sstevel@tonic-gate } else { 4100Sstevel@tonic-gate inquotes = B_TRUE; 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate continue; 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate if (*current == NULL) { 4150Sstevel@tonic-gate *current = ibuf; 4160Sstevel@tonic-gate (*newargc)++; 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate /* 4220Sstevel@tonic-gate * Tricky corner case... 4230Sstevel@tonic-gate * I've parsed _exactly_ the amount of args as I have space. It 4240Sstevel@tonic-gate * won't return NULL-terminated, and bad things will happen to 4250Sstevel@tonic-gate * the caller. 4260Sstevel@tonic-gate */ 4270Sstevel@tonic-gate if (argvlen == *newargc) { 4280Sstevel@tonic-gate current = realloc(*thisargv, sizeof (char *) * (argvlen + 1)); 4290Sstevel@tonic-gate if (current == NULL) { 4300Sstevel@tonic-gate free(*thisargv); 4310Sstevel@tonic-gate return (MEMORY_ALLOCATION); 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate *thisargv = current; 4340Sstevel@tonic-gate current[argvlen] = NULL; 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate return (SUCCESS); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * Enter a mode where commands are read from a file. Treat stdin special. 4420Sstevel@tonic-gate */ 4430Sstevel@tonic-gate void 4444235Smarkfen do_interactive(FILE *infile, char *configfile, char *promptstring, 4454235Smarkfen char *my_fmri, parse_cmdln_fn parseit) 4460Sstevel@tonic-gate { 4470Sstevel@tonic-gate char ibuf[IBUF_SIZE], holder[IBUF_SIZE]; 4484235Smarkfen char *hptr, **thisargv, *ebuf; 4490Sstevel@tonic-gate int thisargc; 4500Sstevel@tonic-gate boolean_t continue_in_progress = B_FALSE; 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate (void) setjmp(env); 4530Sstevel@tonic-gate 4544235Smarkfen ebuf = NULL; 4550Sstevel@tonic-gate interactive = B_TRUE; 4560Sstevel@tonic-gate bzero(ibuf, IBUF_SIZE); 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate if (infile == stdin) { 4590Sstevel@tonic-gate (void) printf("%s", promptstring); 4600Sstevel@tonic-gate (void) fflush(stdout); 4610Sstevel@tonic-gate } else { 4620Sstevel@tonic-gate readfile = B_TRUE; 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate while (fgets(ibuf, IBUF_SIZE, infile) != NULL) { 4660Sstevel@tonic-gate if (readfile) 4670Sstevel@tonic-gate lineno++; 4680Sstevel@tonic-gate thisargc = 0; 4690Sstevel@tonic-gate thisargv = NULL; 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate /* 4720Sstevel@tonic-gate * Check byte IBUF_SIZE - 2, because byte IBUF_SIZE - 1 will 4730Sstevel@tonic-gate * be null-terminated because of fgets(). 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate if (ibuf[IBUF_SIZE - 2] != '\0') { 4764235Smarkfen ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, 4774235Smarkfen dgettext(TEXT_DOMAIN, "Line %d too big."), lineno); 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate if (!continue_in_progress) { 4810Sstevel@tonic-gate /* Use -2 because of \n from fgets. */ 4820Sstevel@tonic-gate if (ibuf[strlen(ibuf) - 2] == CONT_CHAR) { 4830Sstevel@tonic-gate /* 4840Sstevel@tonic-gate * Can use strcpy here, I've checked the 4850Sstevel@tonic-gate * length already. 4860Sstevel@tonic-gate */ 4870Sstevel@tonic-gate (void) strcpy(holder, ibuf); 4880Sstevel@tonic-gate hptr = &(holder[strlen(holder)]); 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate /* Remove the CONT_CHAR from the string. */ 4910Sstevel@tonic-gate hptr[-2] = ' '; 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate continue_in_progress = B_TRUE; 4940Sstevel@tonic-gate bzero(ibuf, IBUF_SIZE); 4950Sstevel@tonic-gate continue; 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate } else { 4980Sstevel@tonic-gate /* Handle continuations... */ 4990Sstevel@tonic-gate (void) strncpy(hptr, ibuf, 5000Sstevel@tonic-gate (size_t)(&(holder[IBUF_SIZE]) - hptr)); 5010Sstevel@tonic-gate if (holder[IBUF_SIZE - 1] != '\0') { 5024235Smarkfen ipsecutil_exit(SERVICE_FATAL, my_fmri, 5034235Smarkfen debugfile, dgettext(TEXT_DOMAIN, 5044235Smarkfen "Command buffer overrun.")); 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate /* Use - 2 because of \n from fgets. */ 5070Sstevel@tonic-gate if (hptr[strlen(hptr) - 2] == CONT_CHAR) { 5080Sstevel@tonic-gate bzero(ibuf, IBUF_SIZE); 5090Sstevel@tonic-gate hptr += strlen(hptr); 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate /* Remove the CONT_CHAR from the string. */ 5120Sstevel@tonic-gate hptr[-2] = ' '; 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate continue; 5150Sstevel@tonic-gate } else { 5160Sstevel@tonic-gate continue_in_progress = B_FALSE; 5170Sstevel@tonic-gate /* 5180Sstevel@tonic-gate * I've already checked the length... 5190Sstevel@tonic-gate */ 5200Sstevel@tonic-gate (void) strcpy(ibuf, holder); 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate 5244235Smarkfen /* 5254235Smarkfen * Just in case the command fails keep a copy of the 5264235Smarkfen * command buffer for diagnostic output. 5274235Smarkfen */ 5284235Smarkfen if (readfile) { 5294235Smarkfen /* 5304235Smarkfen * The error buffer needs to be big enough to 5314235Smarkfen * hold the longest command string, plus 5324235Smarkfen * some extra text, see below. 5334235Smarkfen */ 5344235Smarkfen ebuf = calloc((IBUF_SIZE * 2), sizeof (char)); 5354235Smarkfen if (ebuf == NULL) { 5364235Smarkfen ipsecutil_exit(SERVICE_FATAL, my_fmri, 5374235Smarkfen debugfile, dgettext(TEXT_DOMAIN, 5384235Smarkfen "Memory allocation error.")); 5394235Smarkfen } else { 5404235Smarkfen (void) snprintf(ebuf, (IBUF_SIZE * 2), 5414235Smarkfen dgettext(TEXT_DOMAIN, 5424235Smarkfen "Config file entry near line %u " 5434235Smarkfen "caused error(s) or warnings:\n\n%s\n\n"), 5444235Smarkfen lineno, ibuf); 5454235Smarkfen } 5464235Smarkfen } 5474235Smarkfen 5480Sstevel@tonic-gate switch (create_argv(ibuf, &thisargc, &thisargv)) { 5490Sstevel@tonic-gate case TOO_MANY_TOKENS: 5504235Smarkfen ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, 5514235Smarkfen dgettext(TEXT_DOMAIN, "Too many input tokens.")); 5520Sstevel@tonic-gate break; 5530Sstevel@tonic-gate case MEMORY_ALLOCATION: 5544235Smarkfen ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, 5554235Smarkfen dgettext(TEXT_DOMAIN, "Memory allocation error.")); 5560Sstevel@tonic-gate break; 5570Sstevel@tonic-gate case COMMENT_LINE: 5580Sstevel@tonic-gate /* Comment line. */ 5594235Smarkfen free(ebuf); 5600Sstevel@tonic-gate break; 5610Sstevel@tonic-gate default: 5624235Smarkfen if (thisargc != 0) { 5634235Smarkfen lines_parsed++; 5644235Smarkfen /* ebuf consumed */ 5654342Spwernau parseit(thisargc, thisargv, ebuf, readfile); 5664235Smarkfen } else { 5674235Smarkfen free(ebuf); 5684235Smarkfen } 5690Sstevel@tonic-gate free(thisargv); 5700Sstevel@tonic-gate if (infile == stdin) { 5710Sstevel@tonic-gate (void) printf("%s", promptstring); 5720Sstevel@tonic-gate (void) fflush(stdout); 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate break; 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate bzero(ibuf, IBUF_SIZE); 5770Sstevel@tonic-gate } 5784342Spwernau 5794342Spwernau /* 5804342Spwernau * The following code is ipseckey specific. This should never be 5814342Spwernau * used by ikeadm which also calls this function because ikeadm 5824342Spwernau * only runs interactively. If this ever changes this code block 5834342Spwernau * sould be revisited. 5844342Spwernau */ 5854342Spwernau if (readfile) { 5864342Spwernau if (lines_parsed != 0 && lines_added == 0) { 5874342Spwernau ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, 5884342Spwernau dgettext(TEXT_DOMAIN, "Configuration file did not " 5894342Spwernau "contain any valid SAs")); 5904342Spwernau } 5914342Spwernau 5924342Spwernau /* 5934342Spwernau * There were errors. Putting the service in maintenance mode. 5944342Spwernau * When svc.startd(1M) allows services to degrade themselves, 5954342Spwernau * this should be revisited. 5964342Spwernau * 5974342Spwernau * If this function was called from a program running as a 5984342Spwernau * smf_method(5), print a warning message. Don't spew out the 5994342Spwernau * errors as these will end up in the smf(5) log file which is 6004342Spwernau * publically readable, the errors may contain sensitive 6014342Spwernau * information. 6024342Spwernau */ 6034342Spwernau if ((lines_added < lines_parsed) && (configfile != NULL)) { 6044342Spwernau if (my_fmri != NULL) { 6054342Spwernau ipsecutil_exit(SERVICE_BADCONF, my_fmri, 6064342Spwernau debugfile, dgettext(TEXT_DOMAIN, 6074342Spwernau "The configuration file contained %d " 6084342Spwernau "errors.\n" 6094342Spwernau "Manually check the configuration with:\n" 6104342Spwernau "ipseckey -c %s\n" 6114342Spwernau "Use svcadm(1M) to clear maintenance " 6124342Spwernau "condition when errors are resolved.\n"), 6134342Spwernau lines_parsed - lines_added, configfile); 6144342Spwernau } else { 6154342Spwernau EXIT_BADCONFIG(NULL); 6164342Spwernau } 6174342Spwernau } else { 6184342Spwernau if (my_fmri != NULL) 6194342Spwernau ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, 6204342Spwernau debugfile, dgettext(TEXT_DOMAIN, 6214342Spwernau "%d actions successfully processed."), 6224342Spwernau lines_added); 6234342Spwernau } 6244342Spwernau } else { 6250Sstevel@tonic-gate (void) putchar('\n'); 6260Sstevel@tonic-gate (void) fflush(stdout); 6270Sstevel@tonic-gate } 6284235Smarkfen EXIT_OK(NULL); 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate /* 6320Sstevel@tonic-gate * Functions to parse strings that represent a debug or privilege level. 6330Sstevel@tonic-gate * These functions are copied from main.c and door.c in usr.lib/in.iked/common. 6340Sstevel@tonic-gate * If this file evolves into a common library that may be used by in.iked 6350Sstevel@tonic-gate * as well as the usr.sbin utilities, those duplicate functions should be 6360Sstevel@tonic-gate * deleted. 6370Sstevel@tonic-gate * 6380Sstevel@tonic-gate * A privilege level may be represented by a simple keyword, corresponding 6390Sstevel@tonic-gate * to one of the possible levels. A debug level may be represented by a 6400Sstevel@tonic-gate * series of keywords, separated by '+' or '-', indicating categories to 6410Sstevel@tonic-gate * be added or removed from the set of categories in the debug level. 6420Sstevel@tonic-gate * For example, +all-op corresponds to level 0xfffffffb (all flags except 6430Sstevel@tonic-gate * for D_OP set); while p1+p2+pfkey corresponds to level 0x38. Note that 6440Sstevel@tonic-gate * the leading '+' is implicit; the first keyword in the list must be for 6450Sstevel@tonic-gate * a category that is to be added. 6460Sstevel@tonic-gate * 6470Sstevel@tonic-gate * These parsing functions make use of a local version of strtok, strtok_d, 6480Sstevel@tonic-gate * which includes an additional parameter, char *delim. This param is filled 6490Sstevel@tonic-gate * in with the character which ends the returned token. In other words, 6500Sstevel@tonic-gate * this version of strtok, in addition to returning the token, also returns 6510Sstevel@tonic-gate * the single character delimiter from the original string which marked the 6520Sstevel@tonic-gate * end of the token. 6530Sstevel@tonic-gate */ 6540Sstevel@tonic-gate static char * 6550Sstevel@tonic-gate strtok_d(char *string, const char *sepset, char *delim) 6560Sstevel@tonic-gate { 6570Sstevel@tonic-gate static char *lasts; 6580Sstevel@tonic-gate char *q, *r; 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate /* first or subsequent call */ 6610Sstevel@tonic-gate if (string == NULL) 6620Sstevel@tonic-gate string = lasts; 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate if (string == 0) /* return if no tokens remaining */ 6650Sstevel@tonic-gate return (NULL); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate q = string + strspn(string, sepset); /* skip leading separators */ 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate if (*q == '\0') /* return if no tokens remaining */ 6700Sstevel@tonic-gate return (NULL); 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate if ((r = strpbrk(q, sepset)) == NULL) { /* move past token */ 6730Sstevel@tonic-gate lasts = 0; /* indicate that this is last token */ 6740Sstevel@tonic-gate } else { 6750Sstevel@tonic-gate *delim = *r; /* save delimitor */ 6760Sstevel@tonic-gate *r = '\0'; 6770Sstevel@tonic-gate lasts = r + 1; 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate return (q); 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate static keywdtab_t privtab[] = { 6830Sstevel@tonic-gate { IKE_PRIV_MINIMUM, "base" }, 6840Sstevel@tonic-gate { IKE_PRIV_MODKEYS, "modkeys" }, 6850Sstevel@tonic-gate { IKE_PRIV_KEYMAT, "keymat" }, 6860Sstevel@tonic-gate { IKE_PRIV_MINIMUM, "0" }, 6870Sstevel@tonic-gate }; 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate int 6900Sstevel@tonic-gate privstr2num(char *str) 6910Sstevel@tonic-gate { 6920Sstevel@tonic-gate keywdtab_t *pp; 6930Sstevel@tonic-gate char *endp; 6940Sstevel@tonic-gate int priv; 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate for (pp = privtab; pp < A_END(privtab); pp++) { 6970Sstevel@tonic-gate if (strcasecmp(str, pp->kw_str) == 0) 6980Sstevel@tonic-gate return (pp->kw_tag); 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate priv = strtol(str, &endp, 0); 7020Sstevel@tonic-gate if (*endp == '\0') 7030Sstevel@tonic-gate return (priv); 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate return (-1); 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate static keywdtab_t dbgtab[] = { 7090Sstevel@tonic-gate { D_CERT, "cert" }, 7100Sstevel@tonic-gate { D_KEY, "key" }, 7110Sstevel@tonic-gate { D_OP, "op" }, 7120Sstevel@tonic-gate { D_P1, "p1" }, 7130Sstevel@tonic-gate { D_P1, "phase1" }, 7140Sstevel@tonic-gate { D_P2, "p2" }, 7150Sstevel@tonic-gate { D_P2, "phase2" }, 7160Sstevel@tonic-gate { D_PFKEY, "pfkey" }, 7170Sstevel@tonic-gate { D_POL, "pol" }, 7180Sstevel@tonic-gate { D_POL, "policy" }, 7190Sstevel@tonic-gate { D_PROP, "prop" }, 7200Sstevel@tonic-gate { D_DOOR, "door" }, 7210Sstevel@tonic-gate { D_CONFIG, "config" }, 7220Sstevel@tonic-gate { D_ALL, "all" }, 7230Sstevel@tonic-gate { 0, "0" }, 7240Sstevel@tonic-gate }; 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate int 7270Sstevel@tonic-gate dbgstr2num(char *str) 7280Sstevel@tonic-gate { 7290Sstevel@tonic-gate keywdtab_t *dp; 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate for (dp = dbgtab; dp < A_END(dbgtab); dp++) { 7320Sstevel@tonic-gate if (strcasecmp(str, dp->kw_str) == 0) 7330Sstevel@tonic-gate return (dp->kw_tag); 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate return (D_INVALID); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate int 7390Sstevel@tonic-gate parsedbgopts(char *optarg) 7400Sstevel@tonic-gate { 7410Sstevel@tonic-gate char *argp, *endp, op, nextop; 7420Sstevel@tonic-gate int mask = 0, new; 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate mask = strtol(optarg, &endp, 0); 7450Sstevel@tonic-gate if (*endp == '\0') 7460Sstevel@tonic-gate return (mask); 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate op = optarg[0]; 7490Sstevel@tonic-gate if (op != '-') 7500Sstevel@tonic-gate op = '+'; 7510Sstevel@tonic-gate argp = strtok_d(optarg, "+-", &nextop); 7520Sstevel@tonic-gate do { 7530Sstevel@tonic-gate new = dbgstr2num(argp); 7540Sstevel@tonic-gate if (new == D_INVALID) { 7550Sstevel@tonic-gate /* we encountered an invalid keywd */ 7560Sstevel@tonic-gate return (new); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate if (op == '+') { 7590Sstevel@tonic-gate mask |= new; 7600Sstevel@tonic-gate } else { 7610Sstevel@tonic-gate mask &= ~new; 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate op = nextop; 7640Sstevel@tonic-gate } while ((argp = strtok_d(NULL, "+-", &nextop)) != NULL); 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate return (mask); 7670Sstevel@tonic-gate } 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate /* 7710Sstevel@tonic-gate * functions to manipulate the kmcookie-label mapping file 7720Sstevel@tonic-gate */ 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate /* 7750Sstevel@tonic-gate * Open, lockf, fdopen the given file, returning a FILE * on success, 7760Sstevel@tonic-gate * or NULL on failure. 7770Sstevel@tonic-gate */ 7780Sstevel@tonic-gate FILE * 7790Sstevel@tonic-gate kmc_open_and_lock(char *name) 7800Sstevel@tonic-gate { 7810Sstevel@tonic-gate int fd, rtnerr; 7820Sstevel@tonic-gate FILE *fp; 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate if ((fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) { 7850Sstevel@tonic-gate return (NULL); 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate if (lockf(fd, F_LOCK, 0) < 0) { 7880Sstevel@tonic-gate return (NULL); 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate if ((fp = fdopen(fd, "a+")) == NULL) { 7910Sstevel@tonic-gate return (NULL); 7920Sstevel@tonic-gate } 7930Sstevel@tonic-gate if (fseek(fp, 0, SEEK_SET) < 0) { 7940Sstevel@tonic-gate /* save errno in case fclose changes it */ 7950Sstevel@tonic-gate rtnerr = errno; 7960Sstevel@tonic-gate (void) fclose(fp); 7970Sstevel@tonic-gate errno = rtnerr; 7980Sstevel@tonic-gate return (NULL); 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate return (fp); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate /* 8040Sstevel@tonic-gate * Extract an integer cookie and string label from a line from the 8050Sstevel@tonic-gate * kmcookie-label file. Return -1 on failure, 0 on success. 8060Sstevel@tonic-gate */ 8070Sstevel@tonic-gate int 8080Sstevel@tonic-gate kmc_parse_line(char *line, int *cookie, char **label) 8090Sstevel@tonic-gate { 8100Sstevel@tonic-gate char *cookiestr; 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate *cookie = 0; 8130Sstevel@tonic-gate *label = NULL; 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate cookiestr = strtok(line, " \t\n"); 8160Sstevel@tonic-gate if (cookiestr == NULL) { 8170Sstevel@tonic-gate return (-1); 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate /* Everything that follows, up to the newline, is the label. */ 8210Sstevel@tonic-gate *label = strtok(NULL, "\n"); 8220Sstevel@tonic-gate if (*label == NULL) { 8230Sstevel@tonic-gate return (-1); 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate *cookie = atoi(cookiestr); 8270Sstevel@tonic-gate return (0); 8280Sstevel@tonic-gate } 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate /* 8310Sstevel@tonic-gate * Insert a mapping into the file (if it's not already there), given the 8320Sstevel@tonic-gate * new label. Return the assigned cookie, or -1 on error. 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate int 8350Sstevel@tonic-gate kmc_insert_mapping(char *label) 8360Sstevel@tonic-gate { 8370Sstevel@tonic-gate FILE *map; 8384757Sdanmcd char linebuf[IBUF_SIZE]; 8390Sstevel@tonic-gate char *cur_label; 8400Sstevel@tonic-gate int max_cookie = 0, cur_cookie, rtn_cookie; 8410Sstevel@tonic-gate int rtnerr = 0; 8420Sstevel@tonic-gate boolean_t found = B_FALSE; 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate /* open and lock the file; will sleep until lock is available */ 8450Sstevel@tonic-gate if ((map = kmc_open_and_lock(KMCFILE)) == NULL) { 8460Sstevel@tonic-gate /* kmc_open_and_lock() sets errno appropriately */ 8470Sstevel@tonic-gate return (-1); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate while (fgets(linebuf, sizeof (linebuf), map) != NULL) { 8510Sstevel@tonic-gate 8524757Sdanmcd /* Skip blank lines, which often come near EOF. */ 8534757Sdanmcd if (strlen(linebuf) == 0) 8544757Sdanmcd continue; 8554757Sdanmcd 8560Sstevel@tonic-gate if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) { 8570Sstevel@tonic-gate rtnerr = EINVAL; 8580Sstevel@tonic-gate goto error; 8590Sstevel@tonic-gate } 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate if (cur_cookie > max_cookie) 8620Sstevel@tonic-gate max_cookie = cur_cookie; 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate if ((!found) && (strcmp(cur_label, label) == 0)) { 8650Sstevel@tonic-gate found = B_TRUE; 8660Sstevel@tonic-gate rtn_cookie = cur_cookie; 8670Sstevel@tonic-gate } 8680Sstevel@tonic-gate } 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate if (!found) { 8710Sstevel@tonic-gate rtn_cookie = ++max_cookie; 8720Sstevel@tonic-gate if ((fprintf(map, "%u\t%s\n", rtn_cookie, label) < 0) || 8730Sstevel@tonic-gate (fflush(map) < 0)) { 8740Sstevel@tonic-gate rtnerr = errno; 8750Sstevel@tonic-gate goto error; 8760Sstevel@tonic-gate } 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate (void) fclose(map); 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate return (rtn_cookie); 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate error: 8830Sstevel@tonic-gate (void) fclose(map); 8840Sstevel@tonic-gate errno = rtnerr; 8850Sstevel@tonic-gate return (-1); 8860Sstevel@tonic-gate } 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate /* 8890Sstevel@tonic-gate * Lookup the given cookie and return its corresponding label. Return 8900Sstevel@tonic-gate * a pointer to the label on success, NULL on error (or if the label is 8910Sstevel@tonic-gate * not found). Note that the returned label pointer points to a static 8920Sstevel@tonic-gate * string, so the label will be overwritten by a subsequent call to the 8930Sstevel@tonic-gate * function; the function is also not thread-safe as a result. 8940Sstevel@tonic-gate */ 8950Sstevel@tonic-gate char * 8960Sstevel@tonic-gate kmc_lookup_by_cookie(int cookie) 8970Sstevel@tonic-gate { 8980Sstevel@tonic-gate FILE *map; 8994757Sdanmcd static char linebuf[IBUF_SIZE]; 9000Sstevel@tonic-gate char *cur_label; 9010Sstevel@tonic-gate int cur_cookie; 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate if ((map = kmc_open_and_lock(KMCFILE)) == NULL) { 9040Sstevel@tonic-gate return (NULL); 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate while (fgets(linebuf, sizeof (linebuf), map) != NULL) { 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) { 9100Sstevel@tonic-gate (void) fclose(map); 9110Sstevel@tonic-gate return (NULL); 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate if (cookie == cur_cookie) { 9150Sstevel@tonic-gate (void) fclose(map); 9160Sstevel@tonic-gate return (cur_label); 9170Sstevel@tonic-gate } 9180Sstevel@tonic-gate } 9190Sstevel@tonic-gate (void) fclose(map); 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate return (NULL); 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate /* 9250Sstevel@tonic-gate * Parse basic extension headers and return in the passed-in pointer vector. 9260Sstevel@tonic-gate * Return values include: 9270Sstevel@tonic-gate * 9280Sstevel@tonic-gate * KGE_OK Everything's nice and parsed out. 9290Sstevel@tonic-gate * If there are no extensions, place NULL in extv[0]. 9300Sstevel@tonic-gate * KGE_DUP There is a duplicate extension. 9310Sstevel@tonic-gate * First instance in appropriate bin. First duplicate in 9320Sstevel@tonic-gate * extv[0]. 9330Sstevel@tonic-gate * KGE_UNK Unknown extension type encountered. extv[0] contains 9340Sstevel@tonic-gate * unknown header. 9350Sstevel@tonic-gate * KGE_LEN Extension length error. 9360Sstevel@tonic-gate * KGE_CHK High-level reality check failed on specific extension. 9370Sstevel@tonic-gate * 9380Sstevel@tonic-gate * My apologies for some of the pointer arithmetic in here. I'm thinking 9390Sstevel@tonic-gate * like an assembly programmer, yet trying to make the compiler happy. 9400Sstevel@tonic-gate */ 9410Sstevel@tonic-gate int 9420Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize, 9430Sstevel@tonic-gate char *diag_buf, uint_t diag_buf_len) 9440Sstevel@tonic-gate { 9450Sstevel@tonic-gate int i; 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate if (diag_buf != NULL) 9480Sstevel@tonic-gate diag_buf[0] = '\0'; 9490Sstevel@tonic-gate 9500Sstevel@tonic-gate for (i = 1; i <= SPD_EXT_MAX; i++) 9510Sstevel@tonic-gate extv[i] = NULL; 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate i = 0; 9540Sstevel@tonic-gate /* Use extv[0] as the "current working pointer". */ 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate extv[0] = (spd_ext_t *)(basehdr + 1); 9570Sstevel@tonic-gate msgsize = SPD_64TO8(msgsize); 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate while ((char *)extv[0] < ((char *)basehdr + msgsize)) { 9600Sstevel@tonic-gate /* Check for unknown headers. */ 9610Sstevel@tonic-gate i++; 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate if (extv[0]->spd_ext_type == 0 || 9640Sstevel@tonic-gate extv[0]->spd_ext_type > SPD_EXT_MAX) { 9650Sstevel@tonic-gate if (diag_buf != NULL) { 9660Sstevel@tonic-gate (void) snprintf(diag_buf, diag_buf_len, 9670Sstevel@tonic-gate "spdsock ext 0x%X unknown: 0x%X", 9680Sstevel@tonic-gate i, extv[0]->spd_ext_type); 9690Sstevel@tonic-gate } 9700Sstevel@tonic-gate return (KGE_UNK); 9710Sstevel@tonic-gate } 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate /* 9740Sstevel@tonic-gate * Check length. Use uint64_t because extlen is in units 9750Sstevel@tonic-gate * of 64-bit words. If length goes beyond the msgsize, 9760Sstevel@tonic-gate * return an error. (Zero length also qualifies here.) 9770Sstevel@tonic-gate */ 9780Sstevel@tonic-gate if (extv[0]->spd_ext_len == 0 || 9790Sstevel@tonic-gate (uint8_t *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) > 9800Sstevel@tonic-gate (uint8_t *)((uint8_t *)basehdr + msgsize)) 9810Sstevel@tonic-gate return (KGE_LEN); 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate /* Check for redundant headers. */ 9840Sstevel@tonic-gate if (extv[extv[0]->spd_ext_type] != NULL) 9850Sstevel@tonic-gate return (KGE_DUP); 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate /* If I make it here, assign the appropriate bin. */ 9880Sstevel@tonic-gate extv[extv[0]->spd_ext_type] = extv[0]; 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate /* Advance pointer (See above for uint64_t ptr reasoning.) */ 9910Sstevel@tonic-gate extv[0] = (spd_ext_t *) 9920Sstevel@tonic-gate ((uint64_t *)extv[0] + extv[0]->spd_ext_len); 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* Everything's cool. */ 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate /* 9980Sstevel@tonic-gate * If extv[0] == NULL, then there are no extension headers in this 9990Sstevel@tonic-gate * message. Ensure that this is the case. 10000Sstevel@tonic-gate */ 10010Sstevel@tonic-gate if (extv[0] == (spd_ext_t *)(basehdr + 1)) 10020Sstevel@tonic-gate extv[0] = NULL; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate return (KGE_OK); 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate const char * 10080Sstevel@tonic-gate spdsock_diag(int diagnostic) 10090Sstevel@tonic-gate { 10100Sstevel@tonic-gate switch (diagnostic) { 10110Sstevel@tonic-gate case SPD_DIAGNOSTIC_NONE: 10124064Smarkfen return (dgettext(TEXT_DOMAIN, "no error")); 10130Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNKNOWN_EXT: 10144064Smarkfen return (dgettext(TEXT_DOMAIN, "unknown extension")); 10150Sstevel@tonic-gate case SPD_DIAGNOSTIC_BAD_EXTLEN: 10164064Smarkfen return (dgettext(TEXT_DOMAIN, "bad extension length")); 10170Sstevel@tonic-gate case SPD_DIAGNOSTIC_NO_RULE_EXT: 10184064Smarkfen return (dgettext(TEXT_DOMAIN, "no rule extension")); 10190Sstevel@tonic-gate case SPD_DIAGNOSTIC_BAD_ADDR_LEN: 10204064Smarkfen return (dgettext(TEXT_DOMAIN, "bad address len")); 10210Sstevel@tonic-gate case SPD_DIAGNOSTIC_MIXED_AF: 10224064Smarkfen return (dgettext(TEXT_DOMAIN, "mixed address family")); 10230Sstevel@tonic-gate case SPD_DIAGNOSTIC_ADD_NO_MEM: 10244064Smarkfen return (dgettext(TEXT_DOMAIN, "add: no memory")); 10250Sstevel@tonic-gate case SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT: 10264064Smarkfen return (dgettext(TEXT_DOMAIN, "add: wrong action count")); 10270Sstevel@tonic-gate case SPD_DIAGNOSTIC_ADD_BAD_TYPE: 10284064Smarkfen return (dgettext(TEXT_DOMAIN, "add: bad type")); 10290Sstevel@tonic-gate case SPD_DIAGNOSTIC_ADD_BAD_FLAGS: 10304064Smarkfen return (dgettext(TEXT_DOMAIN, "add: bad flags")); 10310Sstevel@tonic-gate case SPD_DIAGNOSTIC_ADD_INCON_FLAGS: 10324064Smarkfen return (dgettext(TEXT_DOMAIN, "add: inconsistent flags")); 10330Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_LCLPORT: 10344064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed local port")); 10350Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_LCLPORT: 10364064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate local port")); 10370Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_REMPORT: 10384064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed remote port")); 10390Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_REMPORT: 10404064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate remote port")); 10410Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_PROTO: 10424064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed proto")); 10430Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_PROTO: 10444064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate proto")); 10450Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_LCLADDR: 10464064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed local address")); 10470Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_LCLADDR: 10484064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate local address")); 10490Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_REMADDR: 10504064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed remote address")); 10510Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_REMADDR: 10524064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate remote address")); 10530Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_ACTION: 10544064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed action")); 10550Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_ACTION: 10564064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate action")); 10570Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_RULE: 10584064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed rule")); 10590Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_RULE: 10604064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate rule")); 10610Sstevel@tonic-gate case SPD_DIAGNOSTIC_MALFORMED_RULESET: 10624064Smarkfen return (dgettext(TEXT_DOMAIN, "malformed ruleset")); 10630Sstevel@tonic-gate case SPD_DIAGNOSTIC_DUPLICATE_RULESET: 10644064Smarkfen return (dgettext(TEXT_DOMAIN, "duplicate ruleset")); 10650Sstevel@tonic-gate case SPD_DIAGNOSTIC_INVALID_RULE_INDEX: 10664064Smarkfen return (dgettext(TEXT_DOMAIN, "invalid rule index")); 10670Sstevel@tonic-gate case SPD_DIAGNOSTIC_BAD_SPDID: 10684064Smarkfen return (dgettext(TEXT_DOMAIN, "bad spdid")); 10690Sstevel@tonic-gate case SPD_DIAGNOSTIC_BAD_MSG_TYPE: 10704064Smarkfen return (dgettext(TEXT_DOMAIN, "bad message type")); 10710Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_AH_ALG: 10724064Smarkfen return (dgettext(TEXT_DOMAIN, "unsupported AH algorithm")); 10730Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG: 10744064Smarkfen return (dgettext(TEXT_DOMAIN, 10754064Smarkfen "unsupported ESP encryption algorithm")); 10760Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG: 10774064Smarkfen return (dgettext(TEXT_DOMAIN, 10784064Smarkfen "unsupported ESP authentication algorithm")); 10790Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_AH_KEYSIZE: 10804064Smarkfen return (dgettext(TEXT_DOMAIN, "unsupported AH key size")); 10810Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_KEYSIZE: 10824064Smarkfen return (dgettext(TEXT_DOMAIN, 10834064Smarkfen "unsupported ESP encryption key size")); 10840Sstevel@tonic-gate case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_KEYSIZE: 10854064Smarkfen return (dgettext(TEXT_DOMAIN, 10864064Smarkfen "unsupported ESP authentication key size")); 10870Sstevel@tonic-gate case SPD_DIAGNOSTIC_NO_ACTION_EXT: 10884064Smarkfen return (dgettext(TEXT_DOMAIN, "No ACTION extension")); 10890Sstevel@tonic-gate case SPD_DIAGNOSTIC_ALG_ID_RANGE: 10904064Smarkfen return (dgettext(TEXT_DOMAIN, "invalid algorithm identifer")); 10910Sstevel@tonic-gate case SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES: 10924064Smarkfen return (dgettext(TEXT_DOMAIN, 10934064Smarkfen "number of key sizes inconsistent")); 10940Sstevel@tonic-gate case SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES: 10954064Smarkfen return (dgettext(TEXT_DOMAIN, 10964064Smarkfen "number of block sizes inconsistent")); 10970Sstevel@tonic-gate case SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN: 10984064Smarkfen return (dgettext(TEXT_DOMAIN, "invalid mechanism name length")); 10993055Sdanmcd case SPD_DIAGNOSTIC_NOT_GLOBAL_OP: 11004064Smarkfen return (dgettext(TEXT_DOMAIN, 11014064Smarkfen "operation not applicable to all policies")); 11023055Sdanmcd case SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS: 11034064Smarkfen return (dgettext(TEXT_DOMAIN, 11044064Smarkfen "using selectors on a transport-mode tunnel")); 11050Sstevel@tonic-gate default: 11064064Smarkfen return (dgettext(TEXT_DOMAIN, "unknown diagnostic")); 11070Sstevel@tonic-gate } 11080Sstevel@tonic-gate } 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate /* 11110Sstevel@tonic-gate * PF_KEY Diagnostic table. 11120Sstevel@tonic-gate * 11130Sstevel@tonic-gate * PF_KEY NOTE: If you change pfkeyv2.h's SADB_X_DIAGNOSTIC_* space, this is 11140Sstevel@tonic-gate * where you need to add new messages. 11150Sstevel@tonic-gate */ 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate const char * 11180Sstevel@tonic-gate keysock_diag(int diagnostic) 11190Sstevel@tonic-gate { 11200Sstevel@tonic-gate switch (diagnostic) { 11213055Sdanmcd case SADB_X_DIAGNOSTIC_NONE: 11224064Smarkfen return (dgettext(TEXT_DOMAIN, "No diagnostic")); 11230Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_UNKNOWN_MSG: 11244064Smarkfen return (dgettext(TEXT_DOMAIN, "Unknown message type")); 11250Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_UNKNOWN_EXT: 11264064Smarkfen return (dgettext(TEXT_DOMAIN, "Unknown extension type")); 11270Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_EXTLEN: 11284064Smarkfen return (dgettext(TEXT_DOMAIN, "Bad extension length")); 11290Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE: 11304064Smarkfen return (dgettext(TEXT_DOMAIN, 11314064Smarkfen "Unknown Security Association type")); 11320Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_SATYPE_NEEDED: 11334064Smarkfen return (dgettext(TEXT_DOMAIN, 11344064Smarkfen "Specific Security Association type needed")); 11350Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_NO_SADBS: 11364064Smarkfen return (dgettext(TEXT_DOMAIN, 11374064Smarkfen "No Security Association Databases present")); 11380Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_NO_EXT: 11394064Smarkfen return (dgettext(TEXT_DOMAIN, 11404064Smarkfen "No extensions needed for message")); 11410Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_SRC_AF: 11424064Smarkfen return (dgettext(TEXT_DOMAIN, "Bad source address family")); 11430Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_DST_AF: 11444064Smarkfen return (dgettext(TEXT_DOMAIN, 11454064Smarkfen "Bad destination address family")); 11460Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_PROXY_AF: 11474064Smarkfen return (dgettext(TEXT_DOMAIN, 11484064Smarkfen "Bad inner-source address family")); 11490Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_AF_MISMATCH: 11504064Smarkfen return (dgettext(TEXT_DOMAIN, 11514064Smarkfen "Source/destination address family mismatch")); 11520Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_SRC: 11534064Smarkfen return (dgettext(TEXT_DOMAIN, "Bad source address value")); 11540Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_DST: 11554064Smarkfen return (dgettext(TEXT_DOMAIN, "Bad destination address value")); 11560Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_ALLOC_HSERR: 11574064Smarkfen return (dgettext(TEXT_DOMAIN, 11584064Smarkfen "Soft allocations limit more than hard limit")); 11590Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BYTES_HSERR: 11604064Smarkfen return (dgettext(TEXT_DOMAIN, 11614064Smarkfen "Soft bytes limit more than hard limit")); 11620Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_ADDTIME_HSERR: 11634064Smarkfen return (dgettext(TEXT_DOMAIN, "Soft add expiration time later " 11640Sstevel@tonic-gate "than hard expiration time")); 11650Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_USETIME_HSERR: 11664064Smarkfen return (dgettext(TEXT_DOMAIN, "Soft use expiration time later " 11670Sstevel@tonic-gate "than hard expiration time")); 11680Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_SRC: 11694064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing source address")); 11700Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_DST: 11714064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing destination address")); 11720Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_SA: 11734064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing SA extension")); 11740Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_EKEY: 11754064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing encryption key")); 11760Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_AKEY: 11774064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing authentication key")); 11780Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_RANGE: 11794064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing SPI range")); 11800Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_SRC: 11814064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate source address")); 11820Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_DST: 11834064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate destination address")); 11840Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_SA: 11854064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate SA extension")); 11860Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_EKEY: 11874064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate encryption key")); 11880Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_AKEY: 11894064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate authentication key")); 11900Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_RANGE: 11914064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate SPI range")); 11920Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_SRC: 11934064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed source address")); 11940Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_DST: 11954064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed destination address")); 11960Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_SA: 11974064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed SA extension")); 11980Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_EKEY: 11994064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed encryption key")); 12000Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_AKEY: 12014064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed authentication key")); 12020Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_RANGE: 12034064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed SPI range")); 12040Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_AKEY_PRESENT: 12054064Smarkfen return (dgettext(TEXT_DOMAIN, "Authentication key not needed")); 12060Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_EKEY_PRESENT: 12074064Smarkfen return (dgettext(TEXT_DOMAIN, "Encryption key not needed")); 12080Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_PROP_PRESENT: 12094064Smarkfen return (dgettext(TEXT_DOMAIN, "Proposal extension not needed")); 12100Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_SUPP_PRESENT: 12114064Smarkfen return (dgettext(TEXT_DOMAIN, 12124064Smarkfen "Supported algorithms extension not needed")); 12130Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_AALG: 12144064Smarkfen return (dgettext(TEXT_DOMAIN, 12154064Smarkfen "Unsupported authentication algorithm")); 12160Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_EALG: 12174064Smarkfen return (dgettext(TEXT_DOMAIN, 12184064Smarkfen "Unsupported encryption algorithm")); 12190Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_SAFLAGS: 12204064Smarkfen return (dgettext(TEXT_DOMAIN, "Invalid SA flags")); 12210Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_SASTATE: 12224064Smarkfen return (dgettext(TEXT_DOMAIN, "Invalid SA state")); 12230Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_AKEYBITS: 12244064Smarkfen return (dgettext(TEXT_DOMAIN, 12254064Smarkfen "Bad number of authentication bits")); 12260Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_BAD_EKEYBITS: 12274064Smarkfen return (dgettext(TEXT_DOMAIN, 12284064Smarkfen "Bad number of encryption bits")); 12290Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_ENCR_NOTSUPP: 12304064Smarkfen return (dgettext(TEXT_DOMAIN, 12314064Smarkfen "Encryption not supported for this SA type")); 12320Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_WEAK_EKEY: 12334064Smarkfen return (dgettext(TEXT_DOMAIN, "Weak encryption key")); 12340Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_WEAK_AKEY: 12354064Smarkfen return (dgettext(TEXT_DOMAIN, "Weak authentication key")); 12360Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_KMP: 12374064Smarkfen return (dgettext(TEXT_DOMAIN, 12384064Smarkfen "Duplicate key management protocol")); 12390Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_KMC: 12404064Smarkfen return (dgettext(TEXT_DOMAIN, 12414064Smarkfen "Duplicate key management cookie")); 12420Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_NATT_LOC: 12434064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing NAT-T local address")); 12440Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MISSING_NATT_REM: 12454064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing NAT-T remote address")); 12460Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_LOC: 12474064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T local address")); 12480Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_REM: 12494064Smarkfen return (dgettext(TEXT_DOMAIN, 12504064Smarkfen "Duplicate NAT-T remote address")); 12510Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC: 12524064Smarkfen return (dgettext(TEXT_DOMAIN, "Malformed NAT-T local address")); 12530Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM: 12544064Smarkfen return (dgettext(TEXT_DOMAIN, 12554064Smarkfen "Malformed NAT-T remote address")); 12560Sstevel@tonic-gate case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_PORTS: 12574064Smarkfen return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T ports")); 12583055Sdanmcd case SADB_X_DIAGNOSTIC_MISSING_INNER_SRC: 12594064Smarkfen return (dgettext(TEXT_DOMAIN, "Missing inner source address")); 12603055Sdanmcd case SADB_X_DIAGNOSTIC_MISSING_INNER_DST: 12614064Smarkfen return (dgettext(TEXT_DOMAIN, 12624064Smarkfen "Missing inner destination address")); 12633055Sdanmcd case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC: 12644064Smarkfen return (dgettext(TEXT_DOMAIN, 12654064Smarkfen "Duplicate inner source address")); 12663055Sdanmcd case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST: 12674064Smarkfen return (dgettext(TEXT_DOMAIN, 12684064Smarkfen "Duplicate inner destination address")); 12693055Sdanmcd case SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC: 12704064Smarkfen return (dgettext(TEXT_DOMAIN, 12714064Smarkfen "Malformed inner source address")); 12723055Sdanmcd case SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST: 12734064Smarkfen return (dgettext(TEXT_DOMAIN, 12744064Smarkfen "Malformed inner destination address")); 12753055Sdanmcd case SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC: 12764064Smarkfen return (dgettext(TEXT_DOMAIN, 12774064Smarkfen "Invalid inner-source prefix length ")); 12783055Sdanmcd case SADB_X_DIAGNOSTIC_PREFIX_INNER_DST: 12794064Smarkfen return (dgettext(TEXT_DOMAIN, 12804064Smarkfen "Invalid inner-destination prefix length")); 12813055Sdanmcd case SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF: 12824064Smarkfen return (dgettext(TEXT_DOMAIN, 12834064Smarkfen "Bad inner-destination address family")); 12843055Sdanmcd case SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH: 12854064Smarkfen return (dgettext(TEXT_DOMAIN, 12863055Sdanmcd "Inner source/destination address family mismatch")); 12873055Sdanmcd case SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF: 12884064Smarkfen return (dgettext(TEXT_DOMAIN, 12894064Smarkfen "Bad NAT-T remote address family")); 12903055Sdanmcd case SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF: 12914064Smarkfen return (dgettext(TEXT_DOMAIN, 12924064Smarkfen "Bad NAT-T local address family")); 12933055Sdanmcd case SADB_X_DIAGNOSTIC_PROTO_MISMATCH: 12944064Smarkfen return (dgettext(TEXT_DOMAIN, 12954064Smarkfen "Source/desination protocol mismatch")); 12963055Sdanmcd case SADB_X_DIAGNOSTIC_INNER_PROTO_MISMATCH: 12974064Smarkfen return (dgettext(TEXT_DOMAIN, 12984064Smarkfen "Inner source/desination protocol mismatch")); 12993055Sdanmcd case SADB_X_DIAGNOSTIC_DUAL_PORT_SETS: 13004064Smarkfen return (dgettext(TEXT_DOMAIN, 13014064Smarkfen "Both inner ports and outer ports are set")); 13020Sstevel@tonic-gate default: 13034064Smarkfen return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code")); 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate } 13063055Sdanmcd 13073055Sdanmcd /* 13083055Sdanmcd * Convert an IPv6 mask to a prefix len. I assume all IPv6 masks are 13093055Sdanmcd * contiguous, so I stop at the first zero bit! 13103055Sdanmcd */ 13113055Sdanmcd int 13123055Sdanmcd in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped) 13133055Sdanmcd { 13143055Sdanmcd int rc = 0; 13153055Sdanmcd uint8_t last; 13163055Sdanmcd int limit = IPV6_ABITS; 13173055Sdanmcd 13183055Sdanmcd if (is_v4mapped) { 13193055Sdanmcd mask += ((IPV6_ABITS - IP_ABITS)/8); 13203055Sdanmcd limit = IP_ABITS; 13213055Sdanmcd } 13223055Sdanmcd 13233055Sdanmcd while (*mask == 0xff) { 13243055Sdanmcd rc += 8; 13253055Sdanmcd if (rc == limit) 13263055Sdanmcd return (limit); 13273055Sdanmcd mask++; 13283055Sdanmcd } 13293055Sdanmcd 13303055Sdanmcd last = *mask; 13313055Sdanmcd while (last != 0) { 13323055Sdanmcd rc++; 13333055Sdanmcd last = (last << 1) & 0xff; 13343055Sdanmcd } 13353055Sdanmcd 13363055Sdanmcd return (rc); 13373055Sdanmcd } 13383055Sdanmcd 13393055Sdanmcd /* 13403055Sdanmcd * Expand the diagnostic code into a message. 13413055Sdanmcd */ 13423055Sdanmcd void 13433055Sdanmcd print_diagnostic(FILE *file, uint16_t diagnostic) 13443055Sdanmcd { 13453055Sdanmcd /* Use two spaces so above strings can fit on the line. */ 13464064Smarkfen (void) fprintf(file, dgettext(TEXT_DOMAIN, 13474064Smarkfen " Diagnostic code %u: %s.\n"), 13483055Sdanmcd diagnostic, keysock_diag(diagnostic)); 13493055Sdanmcd } 13503055Sdanmcd 13513055Sdanmcd /* 13523055Sdanmcd * Prints the base PF_KEY message. 13533055Sdanmcd */ 13543055Sdanmcd void 13554867Spwernau print_sadb_msg(FILE *file, struct sadb_msg *samsg, time_t wallclock, 13564867Spwernau boolean_t vflag) 13573055Sdanmcd { 13583055Sdanmcd if (wallclock != 0) 13594867Spwernau printsatime(file, wallclock, dgettext(TEXT_DOMAIN, 13604064Smarkfen "%sTimestamp: %s\n"), "", NULL, 13613055Sdanmcd vflag); 13623055Sdanmcd 13634867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 13644867Spwernau "Base message (version %u) type "), 13653055Sdanmcd samsg->sadb_msg_version); 13663055Sdanmcd switch (samsg->sadb_msg_type) { 13673055Sdanmcd case SADB_RESERVED: 13684867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 13694064Smarkfen "RESERVED (warning: set to 0)")); 13703055Sdanmcd break; 13713055Sdanmcd case SADB_GETSPI: 13724867Spwernau (void) fprintf(file, "GETSPI"); 13733055Sdanmcd break; 13743055Sdanmcd case SADB_UPDATE: 13754867Spwernau (void) fprintf(file, "UPDATE"); 13763055Sdanmcd break; 13773055Sdanmcd case SADB_ADD: 13784867Spwernau (void) fprintf(file, "ADD"); 13793055Sdanmcd break; 13803055Sdanmcd case SADB_DELETE: 13814867Spwernau (void) fprintf(file, "DELETE"); 13823055Sdanmcd break; 13833055Sdanmcd case SADB_GET: 13844867Spwernau (void) fprintf(file, "GET"); 13853055Sdanmcd break; 13863055Sdanmcd case SADB_ACQUIRE: 13874867Spwernau (void) fprintf(file, "ACQUIRE"); 13883055Sdanmcd break; 13893055Sdanmcd case SADB_REGISTER: 13904867Spwernau (void) fprintf(file, "REGISTER"); 13913055Sdanmcd break; 13923055Sdanmcd case SADB_EXPIRE: 13934867Spwernau (void) fprintf(file, "EXPIRE"); 13943055Sdanmcd break; 13953055Sdanmcd case SADB_FLUSH: 13964867Spwernau (void) fprintf(file, "FLUSH"); 13973055Sdanmcd break; 13983055Sdanmcd case SADB_DUMP: 13994867Spwernau (void) fprintf(file, "DUMP"); 14003055Sdanmcd break; 14013055Sdanmcd case SADB_X_PROMISC: 14024867Spwernau (void) fprintf(file, "X_PROMISC"); 14033055Sdanmcd break; 14043055Sdanmcd case SADB_X_INVERSE_ACQUIRE: 14054867Spwernau (void) fprintf(file, "X_INVERSE_ACQUIRE"); 14063055Sdanmcd break; 14073055Sdanmcd default: 14084867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14094064Smarkfen "Unknown (%u)"), samsg->sadb_msg_type); 14103055Sdanmcd break; 14113055Sdanmcd } 14124867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, ", SA type ")); 14133055Sdanmcd 14143055Sdanmcd switch (samsg->sadb_msg_satype) { 14153055Sdanmcd case SADB_SATYPE_UNSPEC: 14164867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14174867Spwernau "<unspecified/all>")); 14183055Sdanmcd break; 14193055Sdanmcd case SADB_SATYPE_AH: 14204867Spwernau (void) fprintf(file, "AH"); 14213055Sdanmcd break; 14223055Sdanmcd case SADB_SATYPE_ESP: 14234867Spwernau (void) fprintf(file, "ESP"); 14243055Sdanmcd break; 14253055Sdanmcd case SADB_SATYPE_RSVP: 14264867Spwernau (void) fprintf(file, "RSVP"); 14273055Sdanmcd break; 14283055Sdanmcd case SADB_SATYPE_OSPFV2: 14294867Spwernau (void) fprintf(file, "OSPFv2"); 14303055Sdanmcd break; 14313055Sdanmcd case SADB_SATYPE_RIPV2: 14324867Spwernau (void) fprintf(file, "RIPv2"); 14333055Sdanmcd break; 14343055Sdanmcd case SADB_SATYPE_MIP: 14354867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Mobile IP")); 14363055Sdanmcd break; 14373055Sdanmcd default: 14384867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14394064Smarkfen "<unknown %u>"), samsg->sadb_msg_satype); 14403055Sdanmcd break; 14413055Sdanmcd } 14423055Sdanmcd 14434867Spwernau (void) fprintf(file, ".\n"); 14443055Sdanmcd 14453055Sdanmcd if (samsg->sadb_msg_errno != 0) { 14464867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14474867Spwernau "Error %s from PF_KEY.\n"), 14483055Sdanmcd strerror(samsg->sadb_msg_errno)); 14494867Spwernau print_diagnostic(file, samsg->sadb_x_msg_diagnostic); 14503055Sdanmcd } 14513055Sdanmcd 14524867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14534064Smarkfen "Message length %u bytes, seq=%u, pid=%u.\n"), 14543055Sdanmcd SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq, 14553055Sdanmcd samsg->sadb_msg_pid); 14563055Sdanmcd } 14573055Sdanmcd 14583055Sdanmcd /* 14593055Sdanmcd * Print the SA extension for PF_KEY. 14603055Sdanmcd */ 14613055Sdanmcd void 14624867Spwernau print_sa(FILE *file, char *prefix, struct sadb_sa *assoc) 14633055Sdanmcd { 14643055Sdanmcd if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) { 14654867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 14664064Smarkfen "WARNING: SA info extension length (%u) is bad."), 14673055Sdanmcd SADB_64TO8(assoc->sadb_sa_len)); 14683055Sdanmcd } 14693055Sdanmcd 14704867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14714064Smarkfen "%sSADB_ASSOC spi=0x%x, replay=%u, state="), 14723055Sdanmcd prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay); 14733055Sdanmcd switch (assoc->sadb_sa_state) { 14743055Sdanmcd case SADB_SASTATE_LARVAL: 14754867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "LARVAL")); 14763055Sdanmcd break; 14773055Sdanmcd case SADB_SASTATE_MATURE: 14784867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "MATURE")); 14793055Sdanmcd break; 14803055Sdanmcd case SADB_SASTATE_DYING: 14814867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "DYING")); 14823055Sdanmcd break; 14833055Sdanmcd case SADB_SASTATE_DEAD: 14844867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "DEAD")); 14853055Sdanmcd break; 14863055Sdanmcd default: 14874867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14884064Smarkfen "<unknown %u>"), assoc->sadb_sa_state); 14893055Sdanmcd } 14903055Sdanmcd 14913055Sdanmcd if (assoc->sadb_sa_auth != SADB_AALG_NONE) { 14924867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 14934064Smarkfen "\n%sAuthentication algorithm = "), 14943055Sdanmcd prefix); 14954867Spwernau (void) dump_aalg(assoc->sadb_sa_auth, file); 14963055Sdanmcd } 14973055Sdanmcd 14983055Sdanmcd if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) { 14994867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 15004064Smarkfen "\n%sEncryption algorithm = "), prefix); 15014867Spwernau (void) dump_ealg(assoc->sadb_sa_encrypt, file); 15023055Sdanmcd } 15033055Sdanmcd 15044867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix, 15053055Sdanmcd assoc->sadb_sa_flags); 15063055Sdanmcd if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS) 15074867Spwernau (void) fprintf(file, "PFS "); 15083055Sdanmcd if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY) 15094867Spwernau (void) fprintf(file, "NOREPLAY "); 15103055Sdanmcd 15113055Sdanmcd /* BEGIN Solaris-specific flags. */ 15123055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED) 15134867Spwernau (void) fprintf(file, "X_USED "); 15143055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE) 15154867Spwernau (void) fprintf(file, "X_UNIQUE "); 15163055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1) 15174867Spwernau (void) fprintf(file, "X_AALG1 "); 15183055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2) 15194867Spwernau (void) fprintf(file, "X_AALG2 "); 15203055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1) 15214867Spwernau (void) fprintf(file, "X_EALG1 "); 15223055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2) 15234867Spwernau (void) fprintf(file, "X_EALG2 "); 15243055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC) 15254867Spwernau (void) fprintf(file, "X_NATT_LOC "); 15263055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM) 15274867Spwernau (void) fprintf(file, "X_NATT_REM "); 15283055Sdanmcd if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL) 15294867Spwernau (void) fprintf(file, "X_TUNNEL "); 15303055Sdanmcd /* END Solaris-specific flags. */ 15313055Sdanmcd 15324867Spwernau (void) fprintf(file, ">\n"); 15333055Sdanmcd } 15343055Sdanmcd 15353055Sdanmcd void 15364867Spwernau printsatime(FILE *file, int64_t lt, const char *msg, const char *pfx, 15374867Spwernau const char *pfx2, boolean_t vflag) 15383055Sdanmcd { 15393055Sdanmcd char tbuf[TBUF_SIZE]; /* For strftime() call. */ 15403055Sdanmcd const char *tp = tbuf; 15413055Sdanmcd time_t t = lt; 15423055Sdanmcd struct tm res; 15433055Sdanmcd 15443055Sdanmcd if (t != lt) { 15453055Sdanmcd if (lt > 0) 15463055Sdanmcd t = LONG_MAX; 15473055Sdanmcd else 15483055Sdanmcd t = LONG_MIN; 15493055Sdanmcd } 15503055Sdanmcd 15513055Sdanmcd if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0) 15524064Smarkfen tp = dgettext(TEXT_DOMAIN, "<time conversion failed>"); 15534867Spwernau (void) fprintf(file, msg, pfx, tp); 15543055Sdanmcd if (vflag && (pfx2 != NULL)) 15554867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 15564064Smarkfen "%s\t(raw time value %llu)\n"), pfx2, lt); 15573055Sdanmcd } 15583055Sdanmcd 15593055Sdanmcd /* 15603055Sdanmcd * Print the SA lifetime information. (An SADB_EXT_LIFETIME_* extension.) 15613055Sdanmcd */ 15623055Sdanmcd void 15634867Spwernau print_lifetimes(FILE *file, time_t wallclock, struct sadb_lifetime *current, 15643055Sdanmcd struct sadb_lifetime *hard, struct sadb_lifetime *soft, boolean_t vflag) 15653055Sdanmcd { 15663055Sdanmcd int64_t scratch; 15674064Smarkfen char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: "); 15684064Smarkfen char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: "); 15694064Smarkfen char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: "); 15703055Sdanmcd 15713055Sdanmcd if (current != NULL && 15723055Sdanmcd current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) { 15734867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 15744064Smarkfen "WARNING: CURRENT lifetime extension length (%u) is bad."), 15753055Sdanmcd SADB_64TO8(current->sadb_lifetime_len)); 15763055Sdanmcd } 15773055Sdanmcd 15783055Sdanmcd if (hard != NULL && 15793055Sdanmcd hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) { 15804867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 15814867Spwernau "WARNING: HARD lifetime extension length (%u) is bad."), 15823055Sdanmcd SADB_64TO8(hard->sadb_lifetime_len)); 15833055Sdanmcd } 15843055Sdanmcd 15853055Sdanmcd if (soft != NULL && 15863055Sdanmcd soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) { 15874867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 15884867Spwernau "WARNING: SOFT lifetime extension length (%u) is bad."), 15893055Sdanmcd SADB_64TO8(soft->sadb_lifetime_len)); 15903055Sdanmcd } 15913055Sdanmcd 15924867Spwernau (void) fprintf(file, " LT: Lifetime information\n"); 15933055Sdanmcd 15943055Sdanmcd if (current != NULL) { 15953055Sdanmcd /* Express values as current values. */ 15964867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 15973055Sdanmcd "%s%llu bytes protected, %u allocations used.\n"), 15983055Sdanmcd current_prefix, current->sadb_lifetime_bytes, 15993055Sdanmcd current->sadb_lifetime_allocations); 16004867Spwernau printsatime(file, current->sadb_lifetime_addtime, 16014064Smarkfen dgettext(TEXT_DOMAIN, "%sSA added at time %s\n"), 16023055Sdanmcd current_prefix, current_prefix, vflag); 16033055Sdanmcd if (current->sadb_lifetime_usetime != 0) { 16044867Spwernau printsatime(file, current->sadb_lifetime_usetime, 16054064Smarkfen dgettext(TEXT_DOMAIN, 16064064Smarkfen "%sSA first used at time %s\n"), 16073055Sdanmcd current_prefix, current_prefix, vflag); 16083055Sdanmcd } 16094867Spwernau printsatime(file, wallclock, dgettext(TEXT_DOMAIN, 16104064Smarkfen "%sTime now is %s\n"), current_prefix, current_prefix, 16114064Smarkfen vflag); 16123055Sdanmcd } 16133055Sdanmcd 16143055Sdanmcd if (soft != NULL) { 16154867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16164064Smarkfen "%sSoft lifetime information: "), 16173055Sdanmcd soft_prefix); 16184867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16194064Smarkfen "%llu bytes of lifetime, %u " 16203055Sdanmcd "allocations.\n"), soft->sadb_lifetime_bytes, 16213055Sdanmcd soft->sadb_lifetime_allocations); 16224867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16234064Smarkfen "%s%llu seconds of post-add lifetime.\n"), 16243055Sdanmcd soft_prefix, soft->sadb_lifetime_addtime); 16254867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16264064Smarkfen "%s%llu seconds of post-use lifetime.\n"), 16273055Sdanmcd soft_prefix, soft->sadb_lifetime_usetime); 16283055Sdanmcd /* If possible, express values as time remaining. */ 16293055Sdanmcd if (current != NULL) { 16303055Sdanmcd if (soft->sadb_lifetime_bytes != 0) 16314867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16323055Sdanmcd "%s%llu more bytes can be protected.\n"), 16333055Sdanmcd soft_prefix, 16343055Sdanmcd (soft->sadb_lifetime_bytes > 16354342Spwernau current->sadb_lifetime_bytes) ? 16363055Sdanmcd (soft->sadb_lifetime_bytes - 16374342Spwernau current->sadb_lifetime_bytes) : (0)); 16383055Sdanmcd if (soft->sadb_lifetime_addtime != 0 || 16393055Sdanmcd (soft->sadb_lifetime_usetime != 0 && 16404342Spwernau current->sadb_lifetime_usetime != 0)) { 16413055Sdanmcd int64_t adddelta, usedelta; 16423055Sdanmcd 16433055Sdanmcd if (soft->sadb_lifetime_addtime != 0) { 16443055Sdanmcd adddelta = 16453055Sdanmcd current->sadb_lifetime_addtime + 16463055Sdanmcd soft->sadb_lifetime_addtime - 16473055Sdanmcd wallclock; 16483055Sdanmcd } else { 16493055Sdanmcd adddelta = TIME_MAX; 16503055Sdanmcd } 16513055Sdanmcd 16523055Sdanmcd if (soft->sadb_lifetime_usetime != 0 && 16533055Sdanmcd current->sadb_lifetime_usetime != 0) { 16543055Sdanmcd usedelta = 16553055Sdanmcd current->sadb_lifetime_usetime + 16563055Sdanmcd soft->sadb_lifetime_usetime - 16573055Sdanmcd wallclock; 16583055Sdanmcd } else { 16593055Sdanmcd usedelta = TIME_MAX; 16603055Sdanmcd } 16614867Spwernau (void) fprintf(file, "%s", soft_prefix); 16623055Sdanmcd scratch = MIN(adddelta, usedelta); 16633055Sdanmcd if (scratch >= 0) { 16644867Spwernau (void) fprintf(file, 16654867Spwernau dgettext(TEXT_DOMAIN, 16664064Smarkfen "Soft expiration occurs in %lld " 16674064Smarkfen "seconds, "), scratch); 16683055Sdanmcd } else { 16694867Spwernau (void) fprintf(file, 16704867Spwernau dgettext(TEXT_DOMAIN, 16713055Sdanmcd "Soft expiration occurred ")); 16723055Sdanmcd } 16733055Sdanmcd scratch += wallclock; 16744867Spwernau printsatime(file, scratch, dgettext(TEXT_DOMAIN, 16754064Smarkfen "%sat %s.\n"), "", soft_prefix, vflag); 16763055Sdanmcd } 16773055Sdanmcd } 16783055Sdanmcd } 16793055Sdanmcd 16803055Sdanmcd if (hard != NULL) { 16814867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16824064Smarkfen "%sHard lifetime information: "), hard_prefix); 16834867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16844867Spwernau "%llu bytes of lifetime, %u allocations.\n"), 16854867Spwernau hard->sadb_lifetime_bytes, hard->sadb_lifetime_allocations); 16864867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16874064Smarkfen "%s%llu seconds of post-add lifetime.\n"), 16883055Sdanmcd hard_prefix, hard->sadb_lifetime_addtime); 16894867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16904064Smarkfen "%s%llu seconds of post-use lifetime.\n"), 16913055Sdanmcd hard_prefix, hard->sadb_lifetime_usetime); 16923055Sdanmcd /* If possible, express values as time remaining. */ 16933055Sdanmcd if (current != NULL) { 16943055Sdanmcd if (hard->sadb_lifetime_bytes != 0) 16954867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 16963055Sdanmcd "%s%llu more bytes can be protected.\n"), 16973055Sdanmcd hard_prefix, 16983055Sdanmcd (hard->sadb_lifetime_bytes > 16994342Spwernau current->sadb_lifetime_bytes) ? 17003055Sdanmcd (hard->sadb_lifetime_bytes - 17014342Spwernau current->sadb_lifetime_bytes) : (0)); 17023055Sdanmcd if (hard->sadb_lifetime_addtime != 0 || 17033055Sdanmcd (hard->sadb_lifetime_usetime != 0 && 17044342Spwernau current->sadb_lifetime_usetime != 0)) { 17053055Sdanmcd int64_t adddelta, usedelta; 17063055Sdanmcd 17073055Sdanmcd if (hard->sadb_lifetime_addtime != 0) { 17083055Sdanmcd adddelta = 17093055Sdanmcd current->sadb_lifetime_addtime + 17103055Sdanmcd hard->sadb_lifetime_addtime - 17113055Sdanmcd wallclock; 17123055Sdanmcd } else { 17133055Sdanmcd adddelta = TIME_MAX; 17143055Sdanmcd } 17153055Sdanmcd 17163055Sdanmcd if (hard->sadb_lifetime_usetime != 0 && 17173055Sdanmcd current->sadb_lifetime_usetime != 0) { 17183055Sdanmcd usedelta = 17193055Sdanmcd current->sadb_lifetime_usetime + 17203055Sdanmcd hard->sadb_lifetime_usetime - 17213055Sdanmcd wallclock; 17223055Sdanmcd } else { 17233055Sdanmcd usedelta = TIME_MAX; 17243055Sdanmcd } 17254867Spwernau (void) fprintf(file, "%s", hard_prefix); 17263055Sdanmcd scratch = MIN(adddelta, usedelta); 17273055Sdanmcd if (scratch >= 0) { 17284867Spwernau (void) fprintf(file, 17294867Spwernau dgettext(TEXT_DOMAIN, 17304064Smarkfen "Hard expiration occurs in %lld " 17314064Smarkfen "seconds, "), scratch); 17323055Sdanmcd } else { 17334867Spwernau (void) fprintf(file, 17344867Spwernau dgettext(TEXT_DOMAIN, 17353055Sdanmcd "Hard expiration occured ")); 17363055Sdanmcd } 17373055Sdanmcd scratch += wallclock; 17384867Spwernau printsatime(file, scratch, dgettext(TEXT_DOMAIN, 17394064Smarkfen "%sat %s.\n"), "", hard_prefix, vflag); 17403055Sdanmcd } 17413055Sdanmcd } 17423055Sdanmcd } 17433055Sdanmcd } 17443055Sdanmcd 17453055Sdanmcd /* 17463055Sdanmcd * Print an SADB_EXT_ADDRESS_* extension. 17473055Sdanmcd */ 17483055Sdanmcd void 17494867Spwernau print_address(FILE *file, char *prefix, struct sadb_address *addr, 17504867Spwernau boolean_t ignore_nss) 17513055Sdanmcd { 17523055Sdanmcd struct protoent *pe; 17533055Sdanmcd 17544867Spwernau (void) fprintf(file, "%s", prefix); 17553055Sdanmcd switch (addr->sadb_address_exttype) { 17563055Sdanmcd case SADB_EXT_ADDRESS_SRC: 17574867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Source address ")); 17583055Sdanmcd break; 17593055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 17604867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17614867Spwernau "Inner source address ")); 17623055Sdanmcd break; 17633055Sdanmcd case SADB_EXT_ADDRESS_DST: 17644867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17654867Spwernau "Destination address ")); 17663055Sdanmcd break; 17673055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 17684867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17694064Smarkfen "Inner destination address ")); 17703055Sdanmcd break; 17713055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 17724867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17734867Spwernau "NAT-T local address ")); 17743055Sdanmcd break; 17753055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 17764867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17774867Spwernau "NAT-T remote address ")); 17783055Sdanmcd break; 17793055Sdanmcd } 17803055Sdanmcd 17814867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17824064Smarkfen "(proto=%d"), addr->sadb_address_proto); 17834867Spwernau if (ignore_nss == B_FALSE) { 17843055Sdanmcd if (addr->sadb_address_proto == 0) { 17854867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17864867Spwernau "/<unspecified>")); 17873055Sdanmcd } else if ((pe = getprotobynumber(addr->sadb_address_proto)) 17883055Sdanmcd != NULL) { 17894867Spwernau (void) fprintf(file, "/%s", pe->p_name); 17903055Sdanmcd } else { 17914867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 17924867Spwernau "/<unknown>")); 17933055Sdanmcd } 17943055Sdanmcd } 17954867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, ")\n%s"), prefix); 17963055Sdanmcd (void) dump_sockaddr((struct sockaddr *)(addr + 1), 17974867Spwernau addr->sadb_address_prefixlen, B_FALSE, file, ignore_nss); 17983055Sdanmcd } 17993055Sdanmcd 18003055Sdanmcd /* 18013055Sdanmcd * Print an SADB_EXT_KEY extension. 18023055Sdanmcd */ 18033055Sdanmcd void 18044867Spwernau print_key(FILE *file, char *prefix, struct sadb_key *key) 18053055Sdanmcd { 18064867Spwernau (void) fprintf(file, "%s", prefix); 18073055Sdanmcd 18083055Sdanmcd switch (key->sadb_key_exttype) { 18093055Sdanmcd case SADB_EXT_KEY_AUTH: 18104867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Authentication")); 18113055Sdanmcd break; 18123055Sdanmcd case SADB_EXT_KEY_ENCRYPT: 18134867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Encryption")); 18143055Sdanmcd break; 18153055Sdanmcd } 18163055Sdanmcd 18174867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, " key.\n%s"), prefix); 18184867Spwernau (void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits, file); 18194867Spwernau (void) fprintf(file, "\n"); 18203055Sdanmcd } 18213055Sdanmcd 18223055Sdanmcd /* 18233055Sdanmcd * Print an SADB_EXT_IDENTITY_* extension. 18243055Sdanmcd */ 18253055Sdanmcd void 18264867Spwernau print_ident(FILE *file, char *prefix, struct sadb_ident *id) 18273055Sdanmcd { 18283055Sdanmcd boolean_t canprint = B_TRUE; 18293055Sdanmcd 18304867Spwernau (void) fprintf(file, "%s", prefix); 18313055Sdanmcd switch (id->sadb_ident_exttype) { 18323055Sdanmcd case SADB_EXT_IDENTITY_SRC: 18334867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Source")); 18343055Sdanmcd break; 18353055Sdanmcd case SADB_EXT_IDENTITY_DST: 18364867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "Destination")); 18373055Sdanmcd break; 18383055Sdanmcd } 18393055Sdanmcd 18404867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18414064Smarkfen " identity, uid=%d, type "), id->sadb_ident_id); 18424867Spwernau canprint = dump_sadb_idtype(id->sadb_ident_type, file, NULL); 18434867Spwernau (void) fprintf(file, "\n%s", prefix); 18443055Sdanmcd if (canprint) 18454867Spwernau (void) fprintf(file, "%s\n", (char *)(id + 1)); 18463055Sdanmcd else 18474867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "<cannot print>\n")); 18483055Sdanmcd } 18493055Sdanmcd 18503055Sdanmcd /* 18513055Sdanmcd * Print an SADB_SENSITIVITY extension. 18523055Sdanmcd */ 18533055Sdanmcd void 18544867Spwernau print_sens(FILE *file, char *prefix, struct sadb_sens *sens) 18553055Sdanmcd { 18563055Sdanmcd uint64_t *bitmap = (uint64_t *)(sens + 1); 18573055Sdanmcd int i; 18583055Sdanmcd 18594867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18604064Smarkfen "%sSensitivity DPD %d, sens level=%d, integ level=%d\n"), 18613055Sdanmcd prefix, sens->sadb_sens_dpd, sens->sadb_sens_sens_level, 18623055Sdanmcd sens->sadb_sens_integ_level); 18633055Sdanmcd for (i = 0; sens->sadb_sens_sens_len-- > 0; i++, bitmap++) 18644867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18654867Spwernau "%s Sensitivity BM extended word %d 0x%llx\n"), 18664867Spwernau prefix, i, *bitmap); 18673055Sdanmcd for (i = 0; sens->sadb_sens_integ_len-- > 0; i++, bitmap++) 18684867Spwernau (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 18694867Spwernau "%s Integrity BM extended word %d 0x%llx\n"), 18704867Spwernau prefix, i, *bitmap); 18713055Sdanmcd } 18723055Sdanmcd 18733055Sdanmcd /* 18743055Sdanmcd * Print an SADB_EXT_PROPOSAL extension. 18753055Sdanmcd */ 18763055Sdanmcd void 18774867Spwernau print_prop(FILE *file, char *prefix, struct sadb_prop *prop) 18783055Sdanmcd { 18793055Sdanmcd struct sadb_comb *combs; 18803055Sdanmcd int i, numcombs; 18813055Sdanmcd 18824867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18834064Smarkfen "%sProposal, replay counter = %u.\n"), prefix, 18843055Sdanmcd prop->sadb_prop_replay); 18853055Sdanmcd 18863055Sdanmcd numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop)); 18873055Sdanmcd numcombs /= SADB_8TO64(sizeof (*combs)); 18883055Sdanmcd 18893055Sdanmcd combs = (struct sadb_comb *)(prop + 1); 18903055Sdanmcd 18913055Sdanmcd for (i = 0; i < numcombs; i++) { 18924867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18934064Smarkfen "%s Combination #%u "), prefix, i + 1); 18943055Sdanmcd if (combs[i].sadb_comb_auth != SADB_AALG_NONE) { 18954867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18964064Smarkfen "Authentication = ")); 18974867Spwernau (void) dump_aalg(combs[i].sadb_comb_auth, file); 18984867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 18994064Smarkfen " minbits=%u, maxbits=%u.\n%s "), 19003055Sdanmcd combs[i].sadb_comb_auth_minbits, 19013055Sdanmcd combs[i].sadb_comb_auth_maxbits, prefix); 19023055Sdanmcd } 19033055Sdanmcd 19043055Sdanmcd if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) { 19054867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19064867Spwernau "Encryption = ")); 19074867Spwernau (void) dump_ealg(combs[i].sadb_comb_encrypt, file); 19084867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19094064Smarkfen " minbits=%u, maxbits=%u.\n%s "), 19103055Sdanmcd combs[i].sadb_comb_encrypt_minbits, 19113055Sdanmcd combs[i].sadb_comb_encrypt_maxbits, prefix); 19123055Sdanmcd } 19133055Sdanmcd 19144867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "HARD: ")); 19153055Sdanmcd if (combs[i].sadb_comb_hard_allocations) 19164867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "), 19173055Sdanmcd combs[i].sadb_comb_hard_allocations); 19183055Sdanmcd if (combs[i].sadb_comb_hard_bytes) 19194867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19204867Spwernau "bytes=%llu "), combs[i].sadb_comb_hard_bytes); 19213055Sdanmcd if (combs[i].sadb_comb_hard_addtime) 19224867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19234064Smarkfen "post-add secs=%llu "), 19243055Sdanmcd combs[i].sadb_comb_hard_addtime); 19253055Sdanmcd if (combs[i].sadb_comb_hard_usetime) 19264867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19274064Smarkfen "post-use secs=%llu"), 19283055Sdanmcd combs[i].sadb_comb_hard_usetime); 19293055Sdanmcd 19304867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%s SOFT: "), 19314867Spwernau prefix); 19323055Sdanmcd if (combs[i].sadb_comb_soft_allocations) 19334867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "), 19343055Sdanmcd combs[i].sadb_comb_soft_allocations); 19353055Sdanmcd if (combs[i].sadb_comb_soft_bytes) 19364867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19374867Spwernau "bytes=%llu "), combs[i].sadb_comb_soft_bytes); 19383055Sdanmcd if (combs[i].sadb_comb_soft_addtime) 19394867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19404064Smarkfen "post-add secs=%llu "), 19413055Sdanmcd combs[i].sadb_comb_soft_addtime); 19423055Sdanmcd if (combs[i].sadb_comb_soft_usetime) 19434867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19444064Smarkfen "post-use secs=%llu"), 19453055Sdanmcd combs[i].sadb_comb_soft_usetime); 19464867Spwernau (void) fprintf(file, "\n"); 19473055Sdanmcd } 19483055Sdanmcd } 19493055Sdanmcd 19503055Sdanmcd /* 19513055Sdanmcd * Print an extended proposal (SADB_X_EXT_EPROP). 19523055Sdanmcd */ 19533055Sdanmcd void 19544867Spwernau print_eprop(FILE *file, char *prefix, struct sadb_prop *eprop) 19553055Sdanmcd { 19563055Sdanmcd uint64_t *sofar; 19573055Sdanmcd struct sadb_x_ecomb *ecomb; 19583055Sdanmcd struct sadb_x_algdesc *algdesc; 19593055Sdanmcd int i, j; 19603055Sdanmcd 19614867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19624064Smarkfen "%sExtended Proposal, replay counter = %u, "), prefix, 19634064Smarkfen eprop->sadb_prop_replay); 19644867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19654867Spwernau "number of combinations = %u.\n"), eprop->sadb_x_prop_numecombs); 19663055Sdanmcd 19673055Sdanmcd sofar = (uint64_t *)(eprop + 1); 19683055Sdanmcd ecomb = (struct sadb_x_ecomb *)sofar; 19693055Sdanmcd 19703055Sdanmcd for (i = 0; i < eprop->sadb_x_prop_numecombs; ) { 19714867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19724064Smarkfen "%s Extended combination #%u:\n"), prefix, ++i); 19733055Sdanmcd 19744867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "%s HARD: "), 19754867Spwernau prefix); 19764867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "), 19773055Sdanmcd ecomb->sadb_x_ecomb_hard_allocations); 19784867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%llu, "), 19793055Sdanmcd ecomb->sadb_x_ecomb_hard_bytes); 19804867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19814867Spwernau "post-add secs=%llu, "), ecomb->sadb_x_ecomb_hard_addtime); 19824867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19834867Spwernau "post-use secs=%llu\n"), ecomb->sadb_x_ecomb_hard_usetime); 19843055Sdanmcd 19854867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "%s SOFT: "), 19864867Spwernau prefix); 19874867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "), 19883055Sdanmcd ecomb->sadb_x_ecomb_soft_allocations); 19894867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%llu, "), 19903055Sdanmcd ecomb->sadb_x_ecomb_soft_bytes); 19914867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19924867Spwernau "post-add secs=%llu, "), 19933055Sdanmcd ecomb->sadb_x_ecomb_soft_addtime); 19944867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 19954867Spwernau "post-use secs=%llu\n"), ecomb->sadb_x_ecomb_soft_usetime); 19963055Sdanmcd 19973055Sdanmcd sofar = (uint64_t *)(ecomb + 1); 19983055Sdanmcd algdesc = (struct sadb_x_algdesc *)sofar; 19993055Sdanmcd 20003055Sdanmcd for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) { 20014867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20024064Smarkfen "%s Alg #%u "), prefix, ++j); 20033055Sdanmcd switch (algdesc->sadb_x_algdesc_satype) { 20043055Sdanmcd case SADB_SATYPE_ESP: 20054867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20064064Smarkfen "for ESP ")); 20073055Sdanmcd break; 20083055Sdanmcd case SADB_SATYPE_AH: 20094867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20104867Spwernau "for AH ")); 20113055Sdanmcd break; 20123055Sdanmcd default: 20134867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20144064Smarkfen "for satype=%d "), 20153055Sdanmcd algdesc->sadb_x_algdesc_satype); 20163055Sdanmcd } 20173055Sdanmcd switch (algdesc->sadb_x_algdesc_algtype) { 20183055Sdanmcd case SADB_X_ALGTYPE_CRYPT: 20194867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20204064Smarkfen "Encryption = ")); 20213055Sdanmcd (void) dump_ealg(algdesc->sadb_x_algdesc_alg, 20224867Spwernau file); 20233055Sdanmcd break; 20243055Sdanmcd case SADB_X_ALGTYPE_AUTH: 20254867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20264064Smarkfen "Authentication = ")); 20273055Sdanmcd (void) dump_aalg(algdesc->sadb_x_algdesc_alg, 20284867Spwernau file); 20293055Sdanmcd break; 20303055Sdanmcd default: 20314867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20324064Smarkfen "algtype(%d) = alg(%d)"), 20333055Sdanmcd algdesc->sadb_x_algdesc_algtype, 20343055Sdanmcd algdesc->sadb_x_algdesc_alg); 20353055Sdanmcd break; 20363055Sdanmcd } 20373055Sdanmcd 20384867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 20394064Smarkfen " minbits=%u, maxbits=%u.\n"), 20403055Sdanmcd algdesc->sadb_x_algdesc_minbits, 20413055Sdanmcd algdesc->sadb_x_algdesc_maxbits); 20423055Sdanmcd 20433055Sdanmcd sofar = (uint64_t *)(++algdesc); 20443055Sdanmcd } 20453055Sdanmcd ecomb = (struct sadb_x_ecomb *)sofar; 20463055Sdanmcd } 20473055Sdanmcd } 20483055Sdanmcd 20493055Sdanmcd /* 20503055Sdanmcd * Print an SADB_EXT_SUPPORTED extension. 20513055Sdanmcd */ 20523055Sdanmcd void 20534867Spwernau print_supp(FILE *file, char *prefix, struct sadb_supported *supp) 20543055Sdanmcd { 20553055Sdanmcd struct sadb_alg *algs; 20563055Sdanmcd int i, numalgs; 20573055Sdanmcd 20584867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "%sSupported "), prefix); 20593055Sdanmcd switch (supp->sadb_supported_exttype) { 20603055Sdanmcd case SADB_EXT_SUPPORTED_AUTH: 20614867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "authentication")); 20623055Sdanmcd break; 20633055Sdanmcd case SADB_EXT_SUPPORTED_ENCRYPT: 20644867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, "encryption")); 20653055Sdanmcd break; 20663055Sdanmcd } 20674867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, " algorithms.\n")); 20683055Sdanmcd 20693055Sdanmcd algs = (struct sadb_alg *)(supp + 1); 20703055Sdanmcd numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp)); 20713055Sdanmcd numalgs /= SADB_8TO64(sizeof (*algs)); 20723055Sdanmcd for (i = 0; i < numalgs; i++) { 2073*5906Svk199839 uint16_t exttype = supp->sadb_supported_exttype; 2074*5906Svk199839 20754867Spwernau (void) fprintf(file, "%s", prefix); 2076*5906Svk199839 switch (exttype) { 20773055Sdanmcd case SADB_EXT_SUPPORTED_AUTH: 20784867Spwernau (void) dump_aalg(algs[i].sadb_alg_id, file); 20793055Sdanmcd break; 20803055Sdanmcd case SADB_EXT_SUPPORTED_ENCRYPT: 20814867Spwernau (void) dump_ealg(algs[i].sadb_alg_id, file); 20823055Sdanmcd break; 20833055Sdanmcd } 20844867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 2085*5906Svk199839 " minbits=%u, maxbits=%u, ivlen=%u"), 20863055Sdanmcd algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits, 20873055Sdanmcd algs[i].sadb_alg_ivlen); 2088*5906Svk199839 if (exttype == SADB_EXT_SUPPORTED_ENCRYPT) 2089*5906Svk199839 (void) fprintf(file, dgettext(TEXT_DOMAIN, 2090*5906Svk199839 ", increment=%u"), algs[i].sadb_x_alg_increment); 2091*5906Svk199839 (void) fprintf(file, dgettext(TEXT_DOMAIN, ".\n")); 20923055Sdanmcd } 20933055Sdanmcd } 20943055Sdanmcd 20953055Sdanmcd /* 20963055Sdanmcd * Print an SADB_EXT_SPIRANGE extension. 20973055Sdanmcd */ 20983055Sdanmcd void 20994867Spwernau print_spirange(FILE *file, char *prefix, struct sadb_spirange *range) 21003055Sdanmcd { 21014867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 21024064Smarkfen "%sSPI Range, min=0x%x, max=0x%x\n"), prefix, 21033055Sdanmcd htonl(range->sadb_spirange_min), 21043055Sdanmcd htonl(range->sadb_spirange_max)); 21053055Sdanmcd } 21063055Sdanmcd 21073055Sdanmcd /* 21083055Sdanmcd * Print an SADB_X_EXT_KM_COOKIE extension. 21093055Sdanmcd */ 21103055Sdanmcd 21113055Sdanmcd void 21124867Spwernau print_kmc(FILE *file, char *prefix, struct sadb_x_kmc *kmc) 21133055Sdanmcd { 21143055Sdanmcd char *cookie_label; 21153055Sdanmcd 21163055Sdanmcd if ((cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie)) == 21173055Sdanmcd NULL) 21184064Smarkfen cookie_label = dgettext(TEXT_DOMAIN, "<Label not found.>"); 21193055Sdanmcd 21204867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 21214064Smarkfen "%sProtocol %u, cookie=\"%s\" (%u)\n"), prefix, 21223055Sdanmcd kmc->sadb_x_kmc_proto, cookie_label, kmc->sadb_x_kmc_cookie); 21233055Sdanmcd } 21243055Sdanmcd 21253055Sdanmcd /* 21263055Sdanmcd * Take a PF_KEY message pointed to buffer and print it. Useful for DUMP 21273055Sdanmcd * and GET. 21283055Sdanmcd */ 21293055Sdanmcd void 21304867Spwernau print_samsg(FILE *file, uint64_t *buffer, boolean_t want_timestamp, 21314867Spwernau boolean_t vflag, boolean_t ignore_nss) 21323055Sdanmcd { 21333055Sdanmcd uint64_t *current; 21343055Sdanmcd struct sadb_msg *samsg = (struct sadb_msg *)buffer; 21353055Sdanmcd struct sadb_ext *ext; 21363055Sdanmcd struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL; 21373055Sdanmcd int i; 21383055Sdanmcd time_t wallclock; 21393055Sdanmcd 21403055Sdanmcd (void) time(&wallclock); 21413055Sdanmcd 21424867Spwernau print_sadb_msg(file, samsg, want_timestamp ? wallclock : 0, vflag); 21433055Sdanmcd current = (uint64_t *)(samsg + 1); 21443055Sdanmcd while (current - buffer < samsg->sadb_msg_len) { 21453055Sdanmcd int lenbytes; 21463055Sdanmcd 21473055Sdanmcd ext = (struct sadb_ext *)current; 21483055Sdanmcd lenbytes = SADB_64TO8(ext->sadb_ext_len); 21493055Sdanmcd switch (ext->sadb_ext_type) { 21503055Sdanmcd case SADB_EXT_SA: 21514867Spwernau print_sa(file, dgettext(TEXT_DOMAIN, 21524064Smarkfen "SA: "), (struct sadb_sa *)current); 21533055Sdanmcd break; 21543055Sdanmcd /* 21553055Sdanmcd * Pluck out lifetimes and print them at the end. This is 21563055Sdanmcd * to show relative lifetimes. 21573055Sdanmcd */ 21583055Sdanmcd case SADB_EXT_LIFETIME_CURRENT: 21593055Sdanmcd currentlt = (struct sadb_lifetime *)current; 21603055Sdanmcd break; 21613055Sdanmcd case SADB_EXT_LIFETIME_HARD: 21623055Sdanmcd hardlt = (struct sadb_lifetime *)current; 21633055Sdanmcd break; 21643055Sdanmcd case SADB_EXT_LIFETIME_SOFT: 21653055Sdanmcd softlt = (struct sadb_lifetime *)current; 21663055Sdanmcd break; 21673055Sdanmcd 21683055Sdanmcd case SADB_EXT_ADDRESS_SRC: 21694867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "SRC: "), 21704867Spwernau (struct sadb_address *)current, ignore_nss); 21713055Sdanmcd break; 21723055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 21734867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "INS: "), 21744867Spwernau (struct sadb_address *)current, ignore_nss); 21753055Sdanmcd break; 21763055Sdanmcd case SADB_EXT_ADDRESS_DST: 21774867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "DST: "), 21784867Spwernau (struct sadb_address *)current, ignore_nss); 21793055Sdanmcd break; 21803055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 21814867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "IND: "), 21824867Spwernau (struct sadb_address *)current, ignore_nss); 21833055Sdanmcd break; 21843055Sdanmcd case SADB_EXT_KEY_AUTH: 21854867Spwernau print_key(file, dgettext(TEXT_DOMAIN, 21864064Smarkfen "AKY: "), (struct sadb_key *)current); 21873055Sdanmcd break; 21883055Sdanmcd case SADB_EXT_KEY_ENCRYPT: 21894867Spwernau print_key(file, dgettext(TEXT_DOMAIN, 21904064Smarkfen "EKY: "), (struct sadb_key *)current); 21913055Sdanmcd break; 21923055Sdanmcd case SADB_EXT_IDENTITY_SRC: 21934867Spwernau print_ident(file, dgettext(TEXT_DOMAIN, "SID: "), 21943055Sdanmcd (struct sadb_ident *)current); 21953055Sdanmcd break; 21963055Sdanmcd case SADB_EXT_IDENTITY_DST: 21974867Spwernau print_ident(file, dgettext(TEXT_DOMAIN, "DID: "), 21983055Sdanmcd (struct sadb_ident *)current); 21993055Sdanmcd break; 22003055Sdanmcd case SADB_EXT_SENSITIVITY: 22014867Spwernau print_sens(file, dgettext(TEXT_DOMAIN, "SNS: "), 22023055Sdanmcd (struct sadb_sens *)current); 22033055Sdanmcd break; 22043055Sdanmcd case SADB_EXT_PROPOSAL: 22054867Spwernau print_prop(file, dgettext(TEXT_DOMAIN, "PRP: "), 22063055Sdanmcd (struct sadb_prop *)current); 22073055Sdanmcd break; 22083055Sdanmcd case SADB_EXT_SUPPORTED_AUTH: 22094867Spwernau print_supp(file, dgettext(TEXT_DOMAIN, "SUA: "), 22103055Sdanmcd (struct sadb_supported *)current); 22113055Sdanmcd break; 22123055Sdanmcd case SADB_EXT_SUPPORTED_ENCRYPT: 22134867Spwernau print_supp(file, dgettext(TEXT_DOMAIN, "SUE: "), 22143055Sdanmcd (struct sadb_supported *)current); 22153055Sdanmcd break; 22163055Sdanmcd case SADB_EXT_SPIRANGE: 22174867Spwernau print_spirange(file, dgettext(TEXT_DOMAIN, "SPR: "), 22183055Sdanmcd (struct sadb_spirange *)current); 22193055Sdanmcd break; 22203055Sdanmcd case SADB_X_EXT_EPROP: 22214867Spwernau print_eprop(file, dgettext(TEXT_DOMAIN, "EPR: "), 22223055Sdanmcd (struct sadb_prop *)current); 22233055Sdanmcd break; 22243055Sdanmcd case SADB_X_EXT_KM_COOKIE: 22254867Spwernau print_kmc(file, dgettext(TEXT_DOMAIN, "KMC: "), 22263055Sdanmcd (struct sadb_x_kmc *)current); 22273055Sdanmcd break; 22283055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 22294867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "NRM: "), 22304867Spwernau (struct sadb_address *)current, ignore_nss); 22313055Sdanmcd break; 22323055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 22334867Spwernau print_address(file, dgettext(TEXT_DOMAIN, "NLC: "), 22344867Spwernau (struct sadb_address *)current, ignore_nss); 22353055Sdanmcd break; 22363055Sdanmcd default: 22374867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 22383055Sdanmcd "UNK: Unknown ext. %d, len %d.\n"), 22393055Sdanmcd ext->sadb_ext_type, lenbytes); 22403055Sdanmcd for (i = 0; i < ext->sadb_ext_len; i++) 22414867Spwernau (void) fprintf(file, dgettext(TEXT_DOMAIN, 22424064Smarkfen "UNK: 0x%llx\n"), ((uint64_t *)ext)[i]); 22433055Sdanmcd break; 22443055Sdanmcd } 22453055Sdanmcd current += (lenbytes == 0) ? 22463055Sdanmcd SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len; 22473055Sdanmcd } 22483055Sdanmcd /* 22493055Sdanmcd * Print lifetimes NOW. 22503055Sdanmcd */ 22513055Sdanmcd if (currentlt != NULL || hardlt != NULL || softlt != NULL) 22524867Spwernau print_lifetimes(file, wallclock, currentlt, hardlt, softlt, 22534867Spwernau vflag); 22543055Sdanmcd 22553055Sdanmcd if (current - buffer != samsg->sadb_msg_len) { 22564867Spwernau warnxfp(EFD(file), dgettext(TEXT_DOMAIN, 22574867Spwernau "WARNING: insufficient buffer space or corrupt message.")); 22583055Sdanmcd } 22593055Sdanmcd 22604867Spwernau (void) fflush(file); /* Make sure our message is out there. */ 22613055Sdanmcd } 22623055Sdanmcd 22633055Sdanmcd /* 22643055Sdanmcd * save_XXX functions are used when "saving" the SA tables to either a 22653055Sdanmcd * file or standard output. They use the dump_XXX functions where needed, 22663055Sdanmcd * but mostly they use the rparseXXX functions. 22673055Sdanmcd */ 22683055Sdanmcd 22693055Sdanmcd /* 22703055Sdanmcd * Print save information for a lifetime extension. 22713055Sdanmcd * 22723055Sdanmcd * NOTE : It saves the lifetime in absolute terms. For example, if you 22733055Sdanmcd * had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though 22743055Sdanmcd * there may have been 59 seconds burned off the clock. 22753055Sdanmcd */ 22763055Sdanmcd boolean_t 22773055Sdanmcd save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile) 22783055Sdanmcd { 22793055Sdanmcd char *prefix; 22803055Sdanmcd 22813055Sdanmcd prefix = (lifetime->sadb_lifetime_exttype == SADB_EXT_LIFETIME_SOFT) ? 22823055Sdanmcd "soft" : "hard"; 22833055Sdanmcd 22843055Sdanmcd if (putc('\t', ofile) == EOF) 22853055Sdanmcd return (B_FALSE); 22863055Sdanmcd 22873055Sdanmcd if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile, 22883055Sdanmcd "%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0) 22893055Sdanmcd return (B_FALSE); 22903055Sdanmcd 22913055Sdanmcd if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile, 22923055Sdanmcd "%s_bytes %llu ", prefix, lifetime->sadb_lifetime_bytes) < 0) 22933055Sdanmcd return (B_FALSE); 22943055Sdanmcd 22953055Sdanmcd if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile, 22963055Sdanmcd "%s_addtime %llu ", prefix, lifetime->sadb_lifetime_addtime) < 0) 22973055Sdanmcd return (B_FALSE); 22983055Sdanmcd 22993055Sdanmcd if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile, 23003055Sdanmcd "%s_usetime %llu ", prefix, lifetime->sadb_lifetime_usetime) < 0) 23013055Sdanmcd return (B_FALSE); 23023055Sdanmcd 23033055Sdanmcd return (B_TRUE); 23043055Sdanmcd } 23053055Sdanmcd 23063055Sdanmcd /* 23073055Sdanmcd * Print save information for an address extension. 23083055Sdanmcd */ 23093055Sdanmcd boolean_t 23103055Sdanmcd save_address(struct sadb_address *addr, FILE *ofile) 23113055Sdanmcd { 23123055Sdanmcd char *printable_addr, buf[INET6_ADDRSTRLEN]; 23133055Sdanmcd const char *prefix, *pprefix; 23143055Sdanmcd struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1); 23153055Sdanmcd struct sockaddr_in *sin = (struct sockaddr_in *)sin6; 23163055Sdanmcd int af = sin->sin_family; 23173055Sdanmcd 23183055Sdanmcd /* 23193055Sdanmcd * Address-family reality check. 23203055Sdanmcd */ 23213055Sdanmcd if (af != AF_INET6 && af != AF_INET) 23223055Sdanmcd return (B_FALSE); 23233055Sdanmcd 23243055Sdanmcd switch (addr->sadb_address_exttype) { 23253055Sdanmcd case SADB_EXT_ADDRESS_SRC: 23263055Sdanmcd prefix = "src"; 23273055Sdanmcd pprefix = "sport"; 23283055Sdanmcd break; 23293055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 23303055Sdanmcd prefix = "isrc"; 23313055Sdanmcd pprefix = "isport"; 23323055Sdanmcd break; 23333055Sdanmcd case SADB_EXT_ADDRESS_DST: 23343055Sdanmcd prefix = "dst"; 23353055Sdanmcd pprefix = "dport"; 23363055Sdanmcd break; 23373055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 23383055Sdanmcd prefix = "idst"; 23393055Sdanmcd pprefix = "idport"; 23403055Sdanmcd break; 23413055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 23423055Sdanmcd prefix = "nat_loc "; 23433055Sdanmcd pprefix = "nat_lport"; 23443055Sdanmcd break; 23453055Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 23463055Sdanmcd prefix = "nat_rem "; 23473055Sdanmcd pprefix = "nat_rport"; 23483055Sdanmcd break; 23493055Sdanmcd } 23503055Sdanmcd 23513055Sdanmcd if (fprintf(ofile, " %s ", prefix) < 0) 23523055Sdanmcd return (B_FALSE); 23533055Sdanmcd 23543055Sdanmcd /* 23553055Sdanmcd * Do not do address-to-name translation, given that we live in 23563055Sdanmcd * an age of names that explode into many addresses. 23573055Sdanmcd */ 23583055Sdanmcd printable_addr = (char *)inet_ntop(af, 23593055Sdanmcd (af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr, 23603055Sdanmcd buf, sizeof (buf)); 23613055Sdanmcd if (printable_addr == NULL) 23624064Smarkfen printable_addr = "Invalid IP address."; 23633055Sdanmcd if (fprintf(ofile, "%s", printable_addr) < 0) 23643055Sdanmcd return (B_FALSE); 23653055Sdanmcd if (addr->sadb_address_prefixlen != 0 && 23663055Sdanmcd !((addr->sadb_address_prefixlen == 32 && af == AF_INET) || 23674342Spwernau (addr->sadb_address_prefixlen == 128 && af == AF_INET6))) { 23683055Sdanmcd if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0) 23693055Sdanmcd return (B_FALSE); 23703055Sdanmcd } 23713055Sdanmcd 23723055Sdanmcd /* 23733055Sdanmcd * The port is in the same position for struct sockaddr_in and 23743055Sdanmcd * struct sockaddr_in6. We exploit that property here. 23753055Sdanmcd */ 23763055Sdanmcd if ((pprefix != NULL) && (sin->sin_port != 0)) 23773055Sdanmcd (void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port)); 23783055Sdanmcd 23793055Sdanmcd return (B_TRUE); 23803055Sdanmcd } 23813055Sdanmcd 23823055Sdanmcd /* 23833055Sdanmcd * Print save information for a key extension. Returns whether writing 23843055Sdanmcd * to the specified output file was successful or not. 23853055Sdanmcd */ 23863055Sdanmcd boolean_t 23873055Sdanmcd save_key(struct sadb_key *key, FILE *ofile) 23883055Sdanmcd { 23893055Sdanmcd char *prefix; 23903055Sdanmcd 23913055Sdanmcd if (putc('\t', ofile) == EOF) 23923055Sdanmcd return (B_FALSE); 23933055Sdanmcd 23943055Sdanmcd prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr"; 23953055Sdanmcd 23963055Sdanmcd if (fprintf(ofile, "%skey ", prefix) < 0) 23973055Sdanmcd return (B_FALSE); 23983055Sdanmcd 23993055Sdanmcd if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits, ofile) == -1) 24003055Sdanmcd return (B_FALSE); 24013055Sdanmcd 24023055Sdanmcd return (B_TRUE); 24033055Sdanmcd } 24043055Sdanmcd 24053055Sdanmcd /* 24063055Sdanmcd * Print save information for an identity extension. 24073055Sdanmcd */ 24083055Sdanmcd boolean_t 24093055Sdanmcd save_ident(struct sadb_ident *ident, FILE *ofile) 24103055Sdanmcd { 24113055Sdanmcd char *prefix; 24123055Sdanmcd 24133055Sdanmcd if (putc('\t', ofile) == EOF) 24143055Sdanmcd return (B_FALSE); 24153055Sdanmcd 24163055Sdanmcd prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" : 24173055Sdanmcd "dst"; 24183055Sdanmcd 24193055Sdanmcd if (fprintf(ofile, "%sidtype %s ", prefix, 24203055Sdanmcd rparseidtype(ident->sadb_ident_type)) < 0) 24213055Sdanmcd return (B_FALSE); 24223055Sdanmcd 24233055Sdanmcd if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN || 24243055Sdanmcd ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) { 24254064Smarkfen if (fprintf(ofile, dgettext(TEXT_DOMAIN, 24264064Smarkfen "<can-not-print>")) < 0) 24273055Sdanmcd return (B_FALSE); 24283055Sdanmcd } else { 24293055Sdanmcd if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0) 24303055Sdanmcd return (B_FALSE); 24313055Sdanmcd } 24323055Sdanmcd 24333055Sdanmcd return (B_TRUE); 24343055Sdanmcd } 24353055Sdanmcd 24363055Sdanmcd /* 24373055Sdanmcd * "Save" a security association to an output file. 24383055Sdanmcd * 24394064Smarkfen * NOTE the lack of calls to dgettext() because I'm outputting parseable stuff. 24403055Sdanmcd * ALSO NOTE that if you change keywords (see parsecmd()), you'll have to 24413055Sdanmcd * change them here as well. 24423055Sdanmcd */ 24433055Sdanmcd void 24443055Sdanmcd save_assoc(uint64_t *buffer, FILE *ofile) 24453055Sdanmcd { 24464064Smarkfen int terrno; 24474987Sdanmcd boolean_t seen_proto = B_FALSE, seen_iproto = B_FALSE; 24483055Sdanmcd uint64_t *current; 24493055Sdanmcd struct sadb_address *addr; 24503055Sdanmcd struct sadb_msg *samsg = (struct sadb_msg *)buffer; 24513055Sdanmcd struct sadb_ext *ext; 24523055Sdanmcd 24534064Smarkfen #define tidyup() \ 24544064Smarkfen terrno = errno; (void) fclose(ofile); errno = terrno; \ 24554064Smarkfen interactive = B_FALSE 24564064Smarkfen 24574064Smarkfen #define savenl() if (fputs(" \\\n", ofile) == EOF) \ 24584064Smarkfen { bail(dgettext(TEXT_DOMAIN, "savenl")); } 24593055Sdanmcd 24603055Sdanmcd if (fputs("# begin assoc\n", ofile) == EOF) 24614064Smarkfen bail(dgettext(TEXT_DOMAIN, 24624064Smarkfen "save_assoc: Opening comment of SA")); 24633055Sdanmcd if (fprintf(ofile, "add %s ", rparsesatype(samsg->sadb_msg_satype)) < 0) 24644064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_assoc: First line of SA")); 24653055Sdanmcd savenl(); 24663055Sdanmcd 24673055Sdanmcd current = (uint64_t *)(samsg + 1); 24683055Sdanmcd while (current - buffer < samsg->sadb_msg_len) { 24693055Sdanmcd struct sadb_sa *assoc; 24703055Sdanmcd 24713055Sdanmcd ext = (struct sadb_ext *)current; 24724987Sdanmcd addr = (struct sadb_address *)ext; /* Just in case... */ 24733055Sdanmcd switch (ext->sadb_ext_type) { 24743055Sdanmcd case SADB_EXT_SA: 24753055Sdanmcd assoc = (struct sadb_sa *)ext; 24763055Sdanmcd if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) { 24773055Sdanmcd if (fprintf(ofile, "# WARNING: SA was dying " 24783055Sdanmcd "or dead.\n") < 0) { 24794064Smarkfen tidyup(); 24804064Smarkfen bail(dgettext(TEXT_DOMAIN, 24814064Smarkfen "save_assoc: fprintf not mature")); 24823055Sdanmcd } 24833055Sdanmcd } 24843055Sdanmcd if (fprintf(ofile, " spi 0x%x ", 24854064Smarkfen ntohl(assoc->sadb_sa_spi)) < 0) { 24864064Smarkfen tidyup(); 24874064Smarkfen bail(dgettext(TEXT_DOMAIN, 24884064Smarkfen "save_assoc: fprintf spi")); 24894064Smarkfen } 24903055Sdanmcd if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) { 24913055Sdanmcd if (fprintf(ofile, "encr_alg %s ", 24923055Sdanmcd rparsealg(assoc->sadb_sa_encrypt, 24934342Spwernau IPSEC_PROTO_ESP)) < 0) { 24944064Smarkfen tidyup(); 24954064Smarkfen bail(dgettext(TEXT_DOMAIN, 24964064Smarkfen "save_assoc: fprintf encrypt")); 24974064Smarkfen } 24983055Sdanmcd } 24993055Sdanmcd if (assoc->sadb_sa_auth != SADB_AALG_NONE) { 25003055Sdanmcd if (fprintf(ofile, "auth_alg %s ", 25013055Sdanmcd rparsealg(assoc->sadb_sa_auth, 25024342Spwernau IPSEC_PROTO_AH)) < 0) { 25034064Smarkfen tidyup(); 25044064Smarkfen bail(dgettext(TEXT_DOMAIN, 25054064Smarkfen "save_assoc: fprintf auth")); 25064064Smarkfen } 25073055Sdanmcd } 25083055Sdanmcd if (fprintf(ofile, "replay %d ", 25094064Smarkfen assoc->sadb_sa_replay) < 0) { 25104064Smarkfen tidyup(); 25114064Smarkfen bail(dgettext(TEXT_DOMAIN, 25124064Smarkfen "save_assoc: fprintf replay")); 25134064Smarkfen } 25143055Sdanmcd if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC | 25153055Sdanmcd SADB_X_SAFLAGS_NATT_REM)) { 25164064Smarkfen if (fprintf(ofile, "encap udp") < 0) { 25174064Smarkfen tidyup(); 25184064Smarkfen bail(dgettext(TEXT_DOMAIN, 25194064Smarkfen "save_assoc: fprintf encap")); 25204064Smarkfen } 25213055Sdanmcd } 25223055Sdanmcd savenl(); 25233055Sdanmcd break; 25243055Sdanmcd case SADB_EXT_LIFETIME_HARD: 25253055Sdanmcd case SADB_EXT_LIFETIME_SOFT: 25264064Smarkfen if (!save_lifetime((struct sadb_lifetime *)ext, 25274064Smarkfen ofile)) { 25284064Smarkfen tidyup(); 25294064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_lifetime")); 25304064Smarkfen } 25313055Sdanmcd savenl(); 25323055Sdanmcd break; 25333055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_SRC: 25343055Sdanmcd case SADB_X_EXT_ADDRESS_INNER_DST: 25354987Sdanmcd if (!seen_iproto && addr->sadb_address_proto) { 25364987Sdanmcd (void) fprintf(ofile, " iproto %d", 25374987Sdanmcd addr->sadb_address_proto); 25384987Sdanmcd savenl(); 25394987Sdanmcd seen_iproto = B_TRUE; 25404987Sdanmcd } 25414987Sdanmcd goto skip_srcdst; /* Hack to avoid cases below... */ 25424987Sdanmcd /* FALLTHRU */ 25434987Sdanmcd case SADB_EXT_ADDRESS_SRC: 25444987Sdanmcd case SADB_EXT_ADDRESS_DST: 25453055Sdanmcd if (!seen_proto && addr->sadb_address_proto) { 25463055Sdanmcd (void) fprintf(ofile, " proto %d", 25473055Sdanmcd addr->sadb_address_proto); 25483055Sdanmcd savenl(); 25494987Sdanmcd seen_proto = B_TRUE; 25503055Sdanmcd } 25514987Sdanmcd /* FALLTHRU */ 25524987Sdanmcd case SADB_X_EXT_ADDRESS_NATT_REM: 25534987Sdanmcd case SADB_X_EXT_ADDRESS_NATT_LOC: 25544987Sdanmcd skip_srcdst: 25554064Smarkfen if (!save_address(addr, ofile)) { 25564064Smarkfen tidyup(); 25574064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_address")); 25584064Smarkfen } 25593055Sdanmcd savenl(); 25603055Sdanmcd break; 25613055Sdanmcd case SADB_EXT_KEY_AUTH: 25623055Sdanmcd case SADB_EXT_KEY_ENCRYPT: 25634064Smarkfen if (!save_key((struct sadb_key *)ext, ofile)) { 25644064Smarkfen tidyup(); 25654064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_address")); 25664064Smarkfen } 25673055Sdanmcd savenl(); 25683055Sdanmcd break; 25693055Sdanmcd case SADB_EXT_IDENTITY_SRC: 25703055Sdanmcd case SADB_EXT_IDENTITY_DST: 25714064Smarkfen if (!save_ident((struct sadb_ident *)ext, ofile)) { 25724064Smarkfen tidyup(); 25734064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_address")); 25744064Smarkfen } 25753055Sdanmcd savenl(); 25763055Sdanmcd break; 25773055Sdanmcd case SADB_EXT_SENSITIVITY: 25783055Sdanmcd default: 25793055Sdanmcd /* Skip over irrelevant extensions. */ 25803055Sdanmcd break; 25813055Sdanmcd } 25823055Sdanmcd current += ext->sadb_ext_len; 25833055Sdanmcd } 25843055Sdanmcd 25854064Smarkfen if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) { 25864064Smarkfen tidyup(); 25874064Smarkfen bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs")); 25884064Smarkfen } 25893055Sdanmcd } 25903055Sdanmcd 25913055Sdanmcd /* 25923055Sdanmcd * Open the output file for the "save" command. 25933055Sdanmcd */ 25943055Sdanmcd FILE * 25953055Sdanmcd opensavefile(char *filename) 25963055Sdanmcd { 25973055Sdanmcd int fd; 25983055Sdanmcd FILE *retval; 25993055Sdanmcd struct stat buf; 26003055Sdanmcd 26013055Sdanmcd /* 26023055Sdanmcd * If the user specifies "-" or doesn't give a filename, then 26033055Sdanmcd * dump to stdout. Make sure to document the dangers of files 26043055Sdanmcd * that are NFS, directing your output to strange places, etc. 26053055Sdanmcd */ 26063055Sdanmcd if (filename == NULL || strcmp("-", filename) == 0) 26073055Sdanmcd return (stdout); 26083055Sdanmcd 26093055Sdanmcd /* 26103055Sdanmcd * open the file with the create bits set. Since I check for 26113055Sdanmcd * real UID == root in main(), I won't worry about the ownership 26123055Sdanmcd * problem. 26133055Sdanmcd */ 26143055Sdanmcd fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR); 26153055Sdanmcd if (fd == -1) { 26163055Sdanmcd if (errno != EEXIST) 26174064Smarkfen bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN, 26184064Smarkfen "open error"), 26193055Sdanmcd strerror(errno)); 26203055Sdanmcd fd = open(filename, O_WRONLY | O_TRUNC, 0); 26213055Sdanmcd if (fd == -1) 26224064Smarkfen bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN, 26234064Smarkfen "open error"), strerror(errno)); 26243055Sdanmcd if (fstat(fd, &buf) == -1) { 26253055Sdanmcd (void) close(fd); 26263055Sdanmcd bail_msg("%s fstat: %s", filename, strerror(errno)); 26273055Sdanmcd } 26283055Sdanmcd if (S_ISREG(buf.st_mode) && 26293055Sdanmcd ((buf.st_mode & S_IAMB) != S_IRUSR)) { 26304064Smarkfen warnx(dgettext(TEXT_DOMAIN, 26314064Smarkfen "WARNING: Save file already exists with " 26324064Smarkfen "permission %o."), buf.st_mode & S_IAMB); 26334064Smarkfen warnx(dgettext(TEXT_DOMAIN, 26344064Smarkfen "Normal users may be able to read IPsec " 26354064Smarkfen "keying material.")); 26363055Sdanmcd } 26373055Sdanmcd } 26383055Sdanmcd 26393055Sdanmcd /* Okay, we have an FD. Assign it to a stdio FILE pointer. */ 26403055Sdanmcd retval = fdopen(fd, "w"); 26413055Sdanmcd if (retval == NULL) { 26423055Sdanmcd (void) close(fd); 26434064Smarkfen bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN, 26444064Smarkfen "fdopen error"), strerror(errno)); 26453055Sdanmcd } 26463055Sdanmcd return (retval); 26473055Sdanmcd } 26483055Sdanmcd 26493055Sdanmcd const char * 26503055Sdanmcd do_inet_ntop(const void *addr, char *cp, size_t size) 26513055Sdanmcd { 26523055Sdanmcd boolean_t isv4; 26533055Sdanmcd struct in6_addr *inaddr6 = (struct in6_addr *)addr; 26543055Sdanmcd struct in_addr inaddr; 26553055Sdanmcd 26563055Sdanmcd if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) { 26573055Sdanmcd IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr); 26583055Sdanmcd } 26593055Sdanmcd 26603055Sdanmcd return (inet_ntop(isv4 ? AF_INET : AF_INET6, 26613055Sdanmcd isv4 ? (void *)&inaddr : inaddr6, cp, size)); 26623055Sdanmcd } 26633055Sdanmcd 26643055Sdanmcd char numprint[NBUF_SIZE]; 26653055Sdanmcd 26663055Sdanmcd /* 26673055Sdanmcd * Parse and reverse parse a specific SA type (AH, ESP, etc.). 26683055Sdanmcd */ 26693055Sdanmcd static struct typetable { 26703055Sdanmcd char *type; 26713055Sdanmcd int token; 26723055Sdanmcd } type_table[] = { 26733055Sdanmcd {"all", SADB_SATYPE_UNSPEC}, 26743055Sdanmcd {"ah", SADB_SATYPE_AH}, 26753055Sdanmcd {"esp", SADB_SATYPE_ESP}, 26763055Sdanmcd /* PF_KEY NOTE: More to come if net/pfkeyv2.h gets updated. */ 26773055Sdanmcd {NULL, 0} /* Token value is irrelevant for this entry. */ 26783055Sdanmcd }; 26793055Sdanmcd 26803055Sdanmcd char * 26813055Sdanmcd rparsesatype(int type) 26823055Sdanmcd { 26833055Sdanmcd struct typetable *tt = type_table; 26843055Sdanmcd 26853055Sdanmcd while (tt->type != NULL && type != tt->token) 26863055Sdanmcd tt++; 26873055Sdanmcd 26883055Sdanmcd if (tt->type == NULL) { 26893055Sdanmcd (void) snprintf(numprint, NBUF_SIZE, "%d", type); 26903055Sdanmcd } else { 26913055Sdanmcd return (tt->type); 26923055Sdanmcd } 26933055Sdanmcd 26943055Sdanmcd return (numprint); 26953055Sdanmcd } 26963055Sdanmcd 26973055Sdanmcd 26983055Sdanmcd /* 26993055Sdanmcd * Return a string containing the name of the specified numerical algorithm 27003055Sdanmcd * identifier. 27013055Sdanmcd */ 27023055Sdanmcd char * 27033055Sdanmcd rparsealg(uint8_t alg, int proto_num) 27043055Sdanmcd { 27053055Sdanmcd static struct ipsecalgent *holder = NULL; /* we're single-threaded */ 27063055Sdanmcd 27073055Sdanmcd if (holder != NULL) 27083055Sdanmcd freeipsecalgent(holder); 27093055Sdanmcd 27103055Sdanmcd holder = getipsecalgbynum(alg, proto_num, NULL); 27113055Sdanmcd if (holder == NULL) { 27123055Sdanmcd (void) snprintf(numprint, NBUF_SIZE, "%d", alg); 27133055Sdanmcd return (numprint); 27143055Sdanmcd } 27153055Sdanmcd 27163055Sdanmcd return (*(holder->a_names)); 27173055Sdanmcd } 27183055Sdanmcd 27193055Sdanmcd /* 27203055Sdanmcd * Parse and reverse parse out a source/destination ID type. 27213055Sdanmcd */ 27223055Sdanmcd static struct idtypes { 27233055Sdanmcd char *idtype; 27243055Sdanmcd uint8_t retval; 27253055Sdanmcd } idtypes[] = { 27263055Sdanmcd {"prefix", SADB_IDENTTYPE_PREFIX}, 27273055Sdanmcd {"fqdn", SADB_IDENTTYPE_FQDN}, 27283055Sdanmcd {"domain", SADB_IDENTTYPE_FQDN}, 27293055Sdanmcd {"domainname", SADB_IDENTTYPE_FQDN}, 27303055Sdanmcd {"user_fqdn", SADB_IDENTTYPE_USER_FQDN}, 27313055Sdanmcd {"mailbox", SADB_IDENTTYPE_USER_FQDN}, 27323055Sdanmcd {"der_dn", SADB_X_IDENTTYPE_DN}, 27333055Sdanmcd {"der_gn", SADB_X_IDENTTYPE_GN}, 27343055Sdanmcd {NULL, 0} 27353055Sdanmcd }; 27363055Sdanmcd 27373055Sdanmcd char * 27383055Sdanmcd rparseidtype(uint16_t type) 27393055Sdanmcd { 27403055Sdanmcd struct idtypes *idp; 27413055Sdanmcd 27423055Sdanmcd for (idp = idtypes; idp->idtype != NULL; idp++) { 27433055Sdanmcd if (type == idp->retval) 27443055Sdanmcd return (idp->idtype); 27453055Sdanmcd } 27463055Sdanmcd 27473055Sdanmcd (void) snprintf(numprint, NBUF_SIZE, "%d", type); 27483055Sdanmcd return (numprint); 27493055Sdanmcd } 27504235Smarkfen 27514235Smarkfen /* 27524235Smarkfen * This is a general purpose exit function, calling functions can specify an 27534235Smarkfen * error type. If the command calling this function was started by smf(5) the 27544235Smarkfen * error type could be used as a hint to the restarter. In the future this 27554235Smarkfen * function could be used to do something more intelligent with a process that 27564235Smarkfen * encounters an error. 27574235Smarkfen * 27584235Smarkfen * The function will handle an optional variable args error message, this 27594235Smarkfen * will be written to the error stream, typically a log file or stderr. 27604235Smarkfen */ 27614235Smarkfen void 27624235Smarkfen ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...) 27634235Smarkfen { 27644235Smarkfen int exit_status; 27654235Smarkfen va_list args; 27664235Smarkfen 27674235Smarkfen if (fp == NULL) 27684235Smarkfen fp = stderr; 27694235Smarkfen if (fmt != NULL) { 27704235Smarkfen va_start(args, fmt); 27714235Smarkfen vwarnxfp(fp, fmt, args); 27724235Smarkfen va_end(args); 27734235Smarkfen } 27744235Smarkfen 27754235Smarkfen if (fmri == NULL) { 27764235Smarkfen /* Command being run directly from a shell. */ 27774235Smarkfen switch (type) { 27784235Smarkfen case SERVICE_EXIT_OK: 27794235Smarkfen exit_status = 0; 27804235Smarkfen break; 27814235Smarkfen case SERVICE_DEGRADE: 27824235Smarkfen return; 27834235Smarkfen break; 27844235Smarkfen case SERVICE_BADPERM: 27854235Smarkfen case SERVICE_BADCONF: 27864235Smarkfen case SERVICE_MAINTAIN: 27874235Smarkfen case SERVICE_DISABLE: 27884235Smarkfen case SERVICE_FATAL: 27894235Smarkfen case SERVICE_RESTART: 27904235Smarkfen warnxfp(fp, "Fatal error - exiting."); 27914235Smarkfen exit_status = 1; 27924235Smarkfen break; 27934235Smarkfen } 27944235Smarkfen } else { 27954235Smarkfen /* Command being run as a smf(5) method. */ 27964235Smarkfen switch (type) { 27974235Smarkfen case SERVICE_EXIT_OK: 27984235Smarkfen exit_status = SMF_EXIT_OK; 27994235Smarkfen break; 28004235Smarkfen case SERVICE_DEGRADE: 28014235Smarkfen return; 28024235Smarkfen break; 28034235Smarkfen case SERVICE_BADPERM: 28044235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28054235Smarkfen "Permission error with %s."), fmri); 28064235Smarkfen exit_status = SMF_EXIT_ERR_PERM; 28074235Smarkfen break; 28084235Smarkfen case SERVICE_BADCONF: 28094235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28104235Smarkfen "Bad configuration of service %s."), fmri); 28114235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28124235Smarkfen break; 28134235Smarkfen case SERVICE_MAINTAIN: 28144235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28154235Smarkfen "Service %s needs maintenance."), fmri); 28164235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28174235Smarkfen break; 28184235Smarkfen case SERVICE_DISABLE: 28194235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28204235Smarkfen break; 28214235Smarkfen case SERVICE_FATAL: 28224235Smarkfen warnxfp(fp, dgettext(TEXT_DOMAIN, 28234235Smarkfen "Service %s fatal error."), fmri); 28244235Smarkfen exit_status = SMF_EXIT_ERR_FATAL; 28254235Smarkfen break; 28264235Smarkfen case SERVICE_RESTART: 28274235Smarkfen exit_status = 1; 28284235Smarkfen break; 28294235Smarkfen } 28304235Smarkfen } 28314235Smarkfen (void) fflush(fp); 28324235Smarkfen (void) fclose(fp); 28334235Smarkfen exit(exit_status); 28344235Smarkfen } 2835