xref: /onnv-gate/usr/src/lib/libnsl/ipsec/algs.c (revision 1219)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22132Srobinson 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
30*1219Sraf #include "mt.h"
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/errno.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <ipsec_util.h>
350Sstevel@tonic-gate #include <netdb.h>
360Sstevel@tonic-gate #include <fcntl.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <synch.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <strings.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate #include <syslog.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /* Globals... */
460Sstevel@tonic-gate static rwlock_t proto_rw = DEFAULTRWLOCK; /* Protects cached algorithm list. */
470Sstevel@tonic-gate static time_t proto_last_update;
480Sstevel@tonic-gate static ipsec_proto_t *protos;
490Sstevel@tonic-gate static int num_protos;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate void
520Sstevel@tonic-gate _clean_trash(ipsec_proto_t *proto, int num)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	int alg_offset;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	if (proto == NULL)
570Sstevel@tonic-gate 		return;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	while (num-- != 0) {
600Sstevel@tonic-gate 		free(proto[num].proto_name);
610Sstevel@tonic-gate 		free(proto[num].proto_pkg);
620Sstevel@tonic-gate 		for (alg_offset = 0; alg_offset < proto[num].proto_numalgs;
630Sstevel@tonic-gate 		    alg_offset++)
640Sstevel@tonic-gate 			freeipsecalgent(proto[num].proto_algs[alg_offset]);
650Sstevel@tonic-gate 		free(proto[num].proto_algs);
660Sstevel@tonic-gate 		for (alg_offset = 0; alg_offset < proto[num].proto_algs_npkgs;
670Sstevel@tonic-gate 		    alg_offset++)
680Sstevel@tonic-gate 			free(proto[num].proto_algs_pkgs[alg_offset].pkg_name);
690Sstevel@tonic-gate 		free(proto[num].proto_algs_pkgs);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	free(proto);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static const char *pipechar = "|";
760Sstevel@tonic-gate static const char *comma = ",";
770Sstevel@tonic-gate static const char *dash = "-";
780Sstevel@tonic-gate static const char *slash = "/";
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * Returns >= 0 if success (and > 0 means "increment").
820Sstevel@tonic-gate  * Returns -1 if failure.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate static int
850Sstevel@tonic-gate build_keysizes(int **sizep, char *input_string)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	char *lasts, *token;
880Sstevel@tonic-gate 	int *key_sizes = NULL, num_sizes, key_low, key_high, key_default;
890Sstevel@tonic-gate 	int key_increment = 0;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/*
920Sstevel@tonic-gate 	 * Okay, let's check the format of the key string.  It'll be either:
930Sstevel@tonic-gate 	 *
940Sstevel@tonic-gate 	 * enumeration: size1,size2...,sizeN
950Sstevel@tonic-gate 	 * range: defaultSize/sizeLow-sizeHi,increment
960Sstevel@tonic-gate 	 *
970Sstevel@tonic-gate 	 * In the case of an enumeration, the default key size is the
980Sstevel@tonic-gate 	 * first one in the list.
990Sstevel@tonic-gate 	 */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (strchr(input_string, '/') != NULL) {
1020Sstevel@tonic-gate 		/* key sizes specified by range */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 		/* default */
1050Sstevel@tonic-gate 		token = strtok_r(input_string, slash, &lasts);
1060Sstevel@tonic-gate 		if (token == NULL || (key_default = atoi(token)) == 0)
1070Sstevel@tonic-gate 			return (-1);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		/* low */
1100Sstevel@tonic-gate 		token = strtok_r(NULL, dash, &lasts);
1110Sstevel@tonic-gate 		if (token == NULL || (key_low = atoi(token)) == 0)
1120Sstevel@tonic-gate 			return (-1);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		/* high */
1150Sstevel@tonic-gate 		token = strtok_r(NULL, comma, &lasts);
1160Sstevel@tonic-gate 		if (token == NULL || (key_high = atoi(token)) == 0 ||
1170Sstevel@tonic-gate 		    key_high <= key_low)
1180Sstevel@tonic-gate 			return (-1);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		/* increment */
1210Sstevel@tonic-gate 		token = strtok_r(NULL, "", &lasts);
1220Sstevel@tonic-gate 		if (token == NULL || (key_increment = atoi(token)) == 0)
1230Sstevel@tonic-gate 			return (-1);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		key_sizes = (int *)malloc(LIBIPSEC_ALGS_KEY_NUM_VAL *
1260Sstevel@tonic-gate 		    sizeof (int));
1270Sstevel@tonic-gate 		if (key_sizes == NULL)
1280Sstevel@tonic-gate 			return (-1);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 		key_sizes[LIBIPSEC_ALGS_KEY_DEF_IDX] = key_default;
1310Sstevel@tonic-gate 		key_sizes[LIBIPSEC_ALGS_KEY_MIN_IDX] = key_low;
1320Sstevel@tonic-gate 		key_sizes[LIBIPSEC_ALGS_KEY_MAX_IDX] = key_high;
1330Sstevel@tonic-gate 		key_sizes[LIBIPSEC_ALGS_KEY_MAX_IDX + 1] = 0;
1340Sstevel@tonic-gate 	} else {
1350Sstevel@tonic-gate 		/* key sizes specified by enumeration */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 		key_sizes = (int *)malloc(sizeof (int));
1380Sstevel@tonic-gate 		if (key_sizes == NULL)
1390Sstevel@tonic-gate 			return (-1);
1400Sstevel@tonic-gate 		num_sizes = 0;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		token = strtok_r(input_string, comma, &lasts);
1430Sstevel@tonic-gate 		if (token == NULL || key_sizes == NULL)
1440Sstevel@tonic-gate 			return (-1);
1450Sstevel@tonic-gate 		*key_sizes = 0;
1460Sstevel@tonic-gate 		do {
1470Sstevel@tonic-gate 			int *nks;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 			nks = (int *)realloc(key_sizes,
1500Sstevel@tonic-gate 			    sizeof (int) * ((++num_sizes) + 1));
1510Sstevel@tonic-gate 			if (nks == NULL) {
1520Sstevel@tonic-gate 				free(key_sizes);
1530Sstevel@tonic-gate 				return (-1);
1540Sstevel@tonic-gate 			}
1550Sstevel@tonic-gate 			key_sizes = nks;
1560Sstevel@tonic-gate 			/* Can't check for atoi() == 0 here... */
1570Sstevel@tonic-gate 			key_sizes[num_sizes - 1] = atoi(token);
1580Sstevel@tonic-gate 			key_sizes[num_sizes] = 0;
1590Sstevel@tonic-gate 		} while ((token = strtok_r(NULL, comma, &lasts)) != NULL);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	*sizep = key_sizes;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	return (key_increment);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * Find the execution mode corresponding to the given string.
1690Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate int
1720Sstevel@tonic-gate _str_to_ipsec_exec_mode(char *str, ipsecalgs_exec_mode_t *exec_mode)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	if (strcmp(str, "sync") == 0) {
1750Sstevel@tonic-gate 		*exec_mode = LIBIPSEC_ALGS_EXEC_SYNC;
1760Sstevel@tonic-gate 		return (0);
1770Sstevel@tonic-gate 	} else if (strcmp(str, "async") == 0) {
1780Sstevel@tonic-gate 		*exec_mode = LIBIPSEC_ALGS_EXEC_ASYNC;
1790Sstevel@tonic-gate 		return (0);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	return (-1);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Given a file pointer, read all the text from the file and convert it into
1870Sstevel@tonic-gate  * a bunch of ipsec_proto_t's, each with an array of struct ipsecalgent
1880Sstevel@tonic-gate  * pointers - one for each algorithm.
1890Sstevel@tonic-gate  */
1900Sstevel@tonic-gate static ipsec_proto_t *
1910Sstevel@tonic-gate build_list(FILE *f, int *num)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	char line[1024];
1940Sstevel@tonic-gate 	char *token, *lasts, *alg_names, *ef_name, *key_string, *block_string;
1950Sstevel@tonic-gate 	char *proto_name;
1960Sstevel@tonic-gate 	ipsec_proto_t *rc = NULL, *new_proto = NULL;
1970Sstevel@tonic-gate 	int *block_sizes, *key_sizes;
1980Sstevel@tonic-gate 	int rc_num = 0, key_increment;
1990Sstevel@tonic-gate 	int new_num, alg_num, num_sizes;
2000Sstevel@tonic-gate 	struct ipsecalgent *curalg, **newalglist;
2010Sstevel@tonic-gate 	char cur_pkg[1024];
2020Sstevel@tonic-gate 	boolean_t doing_pkg = B_FALSE;
2030Sstevel@tonic-gate 	ipsecalgs_exec_mode_t exec_mode;
2040Sstevel@tonic-gate 	char diag_buf[128];
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	diag_buf[0] = '\0';
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	while (fgets(line, sizeof (line), f) != NULL) {
2090Sstevel@tonic-gate 		if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PROTO,
2100Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_PROTO) - 1) != 0 &&
2110Sstevel@tonic-gate 		    strncasecmp(line, LIBIPSEC_ALGS_LINE_ALG,
2120Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_ALG) - 1) != 0 &&
2130Sstevel@tonic-gate 		    strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGSTART,
2140Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1) != 0 &&
2150Sstevel@tonic-gate 		    strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGEND,
2160Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_PKGEND) - 1) != 0) {
2170Sstevel@tonic-gate 			if ((token = strtok_r(line, " \t\n", &lasts)) == NULL ||
2180Sstevel@tonic-gate 			    token[0] == '#') {
2190Sstevel@tonic-gate 				continue;
2200Sstevel@tonic-gate 			} else {
221132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
2220Sstevel@tonic-gate 					"non-recognized start of line");
2230Sstevel@tonic-gate 				goto bail;
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PROTO,
2280Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_PROTO) - 1) == 0) {
2290Sstevel@tonic-gate 			/* current line defines a new protocol */
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 			/* skip the protocol token */
2320Sstevel@tonic-gate 			token = strtok_r(line, pipechar, &lasts);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 			/* protocol number */
2350Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
2360Sstevel@tonic-gate 			if (token == NULL || (new_num = atoi(token)) == 0) {
237132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
2380Sstevel@tonic-gate 				    "invalid protocol number");
2390Sstevel@tonic-gate 				goto bail;
2400Sstevel@tonic-gate 			}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 			/* protocol name */
2430Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
2440Sstevel@tonic-gate 			if (token == NULL) {
245132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
2460Sstevel@tonic-gate 				    "cannot read protocol name");
2470Sstevel@tonic-gate 				goto bail;
2480Sstevel@tonic-gate 			}
2490Sstevel@tonic-gate 			proto_name = token;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 			/* execution mode */
2520Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
2530Sstevel@tonic-gate 			if (token == NULL) {
254132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
2550Sstevel@tonic-gate 				    "cannot read execution mode");
2560Sstevel@tonic-gate 				goto bail;
2570Sstevel@tonic-gate 			}
2580Sstevel@tonic-gate 			/* remove trailing '\n' */
2590Sstevel@tonic-gate 			token[strlen(token) - 1] = '\0';
2600Sstevel@tonic-gate 			if (_str_to_ipsec_exec_mode(token, &exec_mode) != 0) {
261132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
2620Sstevel@tonic-gate 				    "invalid execution mode: \"%s\"", token);
2630Sstevel@tonic-gate 				goto bail;
2640Sstevel@tonic-gate 			}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 			/* initialize protocol structure */
2670Sstevel@tonic-gate 			rc_num++;
2680Sstevel@tonic-gate 			new_proto = (ipsec_proto_t *)realloc(rc,
2690Sstevel@tonic-gate 			    sizeof (ipsec_proto_t) * rc_num);
2700Sstevel@tonic-gate 			rc = new_proto;
2710Sstevel@tonic-gate 			if (new_proto == NULL)
2720Sstevel@tonic-gate 				goto bail;
2730Sstevel@tonic-gate 			new_proto += (rc_num - 1);
2740Sstevel@tonic-gate 			new_proto->proto_num = new_num;
2750Sstevel@tonic-gate 			new_proto->proto_algs = NULL;
2760Sstevel@tonic-gate 			new_proto->proto_numalgs = 0;
2770Sstevel@tonic-gate 			new_proto->proto_name = strdup(proto_name);
2780Sstevel@tonic-gate 			if (new_proto->proto_name == NULL)
2790Sstevel@tonic-gate 				goto bail;
2800Sstevel@tonic-gate 			new_proto->proto_exec_mode = exec_mode;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 			if (doing_pkg) {
2830Sstevel@tonic-gate 				/* record proto as being part of current pkg */
2840Sstevel@tonic-gate 				new_proto->proto_pkg = strdup(cur_pkg);
2850Sstevel@tonic-gate 				if (new_proto->proto_pkg == NULL)
2860Sstevel@tonic-gate 					goto bail;
2870Sstevel@tonic-gate 			} else {
2880Sstevel@tonic-gate 				new_proto->proto_pkg = NULL;
2890Sstevel@tonic-gate 			}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 			new_proto->proto_algs_pkgs = NULL;
2920Sstevel@tonic-gate 			new_proto->proto_algs_npkgs = 0;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		} else if (strncasecmp(line, LIBIPSEC_ALGS_LINE_ALG,
2950Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_ALG) - 1) == 0) {
2960Sstevel@tonic-gate 			/* current line defines a new algorithm */
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 			/* skip the algorithm token */
2990Sstevel@tonic-gate 			token = strtok_r(line, pipechar, &lasts);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 			/* protocol number */
3020Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3030Sstevel@tonic-gate 			if (token == NULL || (new_num = atoi(token)) == 0) {
304132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3050Sstevel@tonic-gate 				    "invalid algorithm number");
3060Sstevel@tonic-gate 				goto bail;
3070Sstevel@tonic-gate 			}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 			/* We can be O(N) for now.  There aren't that many. */
3100Sstevel@tonic-gate 			for (new_proto = rc; new_proto < (rc + new_num);
3110Sstevel@tonic-gate 			    new_proto++)
3120Sstevel@tonic-gate 				if (new_proto->proto_num == new_num)
3130Sstevel@tonic-gate 					break;
3140Sstevel@tonic-gate 			if (new_proto == (rc + new_num)) {
315132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3160Sstevel@tonic-gate 				    "invalid protocol number %d for algorithm",
3170Sstevel@tonic-gate 				    new_num);
3180Sstevel@tonic-gate 				goto bail;
3190Sstevel@tonic-gate 			}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 			/* algorithm number */
3220Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3230Sstevel@tonic-gate 			if (token == NULL) {
324132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3250Sstevel@tonic-gate 				    "cannot read algorithm number");
3260Sstevel@tonic-gate 				goto bail;
3270Sstevel@tonic-gate 			}
3280Sstevel@tonic-gate 			/* Can't check for 0 here. */
3290Sstevel@tonic-gate 			alg_num = atoi(token);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 			/* algorithm names */
3320Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3330Sstevel@tonic-gate 			if (token == NULL) {
334132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3350Sstevel@tonic-gate 				    "cannot read algorithm number");
3360Sstevel@tonic-gate 				goto bail;
3370Sstevel@tonic-gate 			}
3380Sstevel@tonic-gate 			alg_names = token;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 			/* mechanism name */
3410Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3420Sstevel@tonic-gate 			if (token == NULL) {
343132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3440Sstevel@tonic-gate 				    "cannot read mechanism name for alg %d "
3450Sstevel@tonic-gate 				    "(proto %d)", alg_num,
3460Sstevel@tonic-gate 				    new_proto->proto_num);
3470Sstevel@tonic-gate 				goto bail;
3480Sstevel@tonic-gate 			}
3490Sstevel@tonic-gate 			ef_name = token;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 			/* key sizes */
3520Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3530Sstevel@tonic-gate 			if (token == NULL) {
354132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3550Sstevel@tonic-gate 				    "cannot read key sizes for alg %d "
3560Sstevel@tonic-gate 				    "(proto %d)", alg_num,
3570Sstevel@tonic-gate 				    new_proto->proto_num);
3580Sstevel@tonic-gate 				goto bail;
3590Sstevel@tonic-gate 			}
3600Sstevel@tonic-gate 			key_string = token;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 			/* block sizes */
3630Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3640Sstevel@tonic-gate 			if (token == NULL) {
365132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3660Sstevel@tonic-gate 				    "cannot read mechanism name for alg %d "
3670Sstevel@tonic-gate 				    "(proto %d)", alg_num,
3680Sstevel@tonic-gate 				    new_proto->proto_num);
3690Sstevel@tonic-gate 				goto bail;
3700Sstevel@tonic-gate 			}
3710Sstevel@tonic-gate 			block_string = token;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 			/* extract key sizes */
3740Sstevel@tonic-gate 			key_increment = build_keysizes(&key_sizes, key_string);
3750Sstevel@tonic-gate 			if (key_increment == -1) {
376132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3770Sstevel@tonic-gate 				    "invalid key sizes for alg %d (proto %d)",
3780Sstevel@tonic-gate 				    alg_num, new_proto->proto_num);
3790Sstevel@tonic-gate 				goto bail;
3800Sstevel@tonic-gate 			}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 			/* extract block sizes */
3830Sstevel@tonic-gate 			block_sizes = (int *)malloc(sizeof (int));
3840Sstevel@tonic-gate 			if (block_sizes == NULL) {
3850Sstevel@tonic-gate 				free(key_sizes);
3860Sstevel@tonic-gate 				goto bail;
3870Sstevel@tonic-gate 			}
3880Sstevel@tonic-gate 			num_sizes = 0;
3890Sstevel@tonic-gate 			token = strtok_r(block_string, comma, &lasts);
3900Sstevel@tonic-gate 			if (token == NULL) {
391132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3920Sstevel@tonic-gate 				    "invalid block sizes for alg %d (proto %d)",
3930Sstevel@tonic-gate 				    alg_num, new_proto->proto_num);
3940Sstevel@tonic-gate 				free(key_sizes);
3950Sstevel@tonic-gate 				goto bail;
3960Sstevel@tonic-gate 			}
3970Sstevel@tonic-gate 			*block_sizes = 0;
3980Sstevel@tonic-gate 			do {
3990Sstevel@tonic-gate 				int *nbk;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 				nbk = (int *)realloc(block_sizes,
4020Sstevel@tonic-gate 				    sizeof (int) * ((++num_sizes) + 1));
4030Sstevel@tonic-gate 				if (nbk == NULL) {
4040Sstevel@tonic-gate 					free(key_sizes);
4050Sstevel@tonic-gate 					free(block_sizes);
4060Sstevel@tonic-gate 					goto bail;
4070Sstevel@tonic-gate 				}
4080Sstevel@tonic-gate 				block_sizes = nbk;
4090Sstevel@tonic-gate 				/* Can't check for 0 here... */
4100Sstevel@tonic-gate 				block_sizes[num_sizes - 1] = atoi(token);
4110Sstevel@tonic-gate 				block_sizes[num_sizes] = 0;
4120Sstevel@tonic-gate 			} while ((token = strtok_r(NULL, comma, &lasts)) !=
4130Sstevel@tonic-gate 			    NULL);
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 			/* Allocate a new struct ipsecalgent. */
4160Sstevel@tonic-gate 			curalg = (struct ipsecalgent *)calloc(
4170Sstevel@tonic-gate 			    sizeof (struct ipsecalgent), 1);
4180Sstevel@tonic-gate 			if (curalg == NULL) {
4190Sstevel@tonic-gate 				free(key_sizes);
4200Sstevel@tonic-gate 				free(block_sizes);
4210Sstevel@tonic-gate 				goto bail;
4220Sstevel@tonic-gate 			}
4230Sstevel@tonic-gate 			curalg->a_proto_num = new_num;
4240Sstevel@tonic-gate 			curalg->a_alg_num = alg_num;
4250Sstevel@tonic-gate 			curalg->a_block_sizes = block_sizes;
4260Sstevel@tonic-gate 			curalg->a_key_sizes = key_sizes;
4270Sstevel@tonic-gate 			curalg->a_key_increment = key_increment;
4280Sstevel@tonic-gate 			if ((curalg->a_mech_name = strdup(ef_name)) == NULL) {
4290Sstevel@tonic-gate 				freeipsecalgent(curalg);
4300Sstevel@tonic-gate 				goto bail;
4310Sstevel@tonic-gate 			}
4320Sstevel@tonic-gate 			/* Set names. */
4330Sstevel@tonic-gate 			curalg->a_names = (char **)malloc(sizeof (char *));
4340Sstevel@tonic-gate 			num_sizes = 0;	/* Recycle "sizes" */
4350Sstevel@tonic-gate 			token = strtok_r(alg_names, comma, &lasts);
4360Sstevel@tonic-gate 			if (curalg->a_names == NULL || token == NULL) {
4370Sstevel@tonic-gate 				freeipsecalgent(curalg);
4380Sstevel@tonic-gate 				goto bail;
4390Sstevel@tonic-gate 			}
4400Sstevel@tonic-gate 			do {
4410Sstevel@tonic-gate 				char **nnames;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 				nnames = (char **)realloc(curalg->a_names,
4440Sstevel@tonic-gate 				    sizeof (char *) * ((++num_sizes) + 1));
4450Sstevel@tonic-gate 				if (nnames == NULL) {
4460Sstevel@tonic-gate 					freeipsecalgent(curalg);
4470Sstevel@tonic-gate 					goto bail;
4480Sstevel@tonic-gate 				}
4490Sstevel@tonic-gate 				curalg->a_names = nnames;
4500Sstevel@tonic-gate 				curalg->a_names[num_sizes] = NULL;
4510Sstevel@tonic-gate 				curalg->a_names[num_sizes - 1] =
4520Sstevel@tonic-gate 				    strdup(token);
4530Sstevel@tonic-gate 				if (curalg->a_names[num_sizes - 1] == NULL) {
4540Sstevel@tonic-gate 					freeipsecalgent(curalg);
4550Sstevel@tonic-gate 					goto bail;
4560Sstevel@tonic-gate 				}
4570Sstevel@tonic-gate 			} while ((token = strtok_r(NULL, comma, &lasts)) !=
4580Sstevel@tonic-gate 			    NULL);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 			if (doing_pkg) {
4610Sstevel@tonic-gate 				/* record alg as being part of current pkg */
4620Sstevel@tonic-gate 				int npkgs = new_proto->proto_algs_npkgs;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 				new_proto->proto_algs_pkgs = realloc(
4650Sstevel@tonic-gate 				    new_proto->proto_algs_pkgs,
4660Sstevel@tonic-gate 				    (npkgs + 1) * sizeof (ipsecalgs_pkg_t));
4670Sstevel@tonic-gate 				if (new_proto->proto_algs_pkgs == NULL)
4680Sstevel@tonic-gate 					goto bail;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 				new_proto->proto_algs_pkgs[npkgs].alg_num =
4710Sstevel@tonic-gate 					curalg->a_alg_num;
4720Sstevel@tonic-gate 				new_proto->proto_algs_pkgs[npkgs].pkg_name =
4730Sstevel@tonic-gate 					strdup(cur_pkg);
4740Sstevel@tonic-gate 				if (new_proto->proto_algs_pkgs[npkgs].pkg_name
4750Sstevel@tonic-gate 				    == NULL)
4760Sstevel@tonic-gate 					goto bail;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 				new_proto->proto_algs_npkgs = npkgs + 1;
4790Sstevel@tonic-gate 			}
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 			/* add new alg to protocol */
4820Sstevel@tonic-gate 			newalglist = realloc(new_proto->proto_algs,
4830Sstevel@tonic-gate 			    (new_proto->proto_numalgs + 1) *
4840Sstevel@tonic-gate 			    sizeof (struct ipsecalgent *));
4850Sstevel@tonic-gate 			if (newalglist == NULL) {
4860Sstevel@tonic-gate 				freeipsecalgent(curalg);
4870Sstevel@tonic-gate 				goto bail;
4880Sstevel@tonic-gate 			}
4890Sstevel@tonic-gate 			newalglist[new_proto->proto_numalgs] = curalg;
4900Sstevel@tonic-gate 			new_proto->proto_numalgs++;
4910Sstevel@tonic-gate 			new_proto->proto_algs = newalglist;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		} else if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGSTART,
4940Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1) == 0) {
4950Sstevel@tonic-gate 			/* start of package delimiter */
4960Sstevel@tonic-gate 			if (doing_pkg) {
497132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
4980Sstevel@tonic-gate 				    "duplicate package start delimiters");
4990Sstevel@tonic-gate 				goto bail;
5000Sstevel@tonic-gate 			}
5010Sstevel@tonic-gate 			(void) strncpy(cur_pkg, line +
5020Sstevel@tonic-gate 			    (sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1),
5030Sstevel@tonic-gate 			    sizeof (cur_pkg));
5040Sstevel@tonic-gate 			/* remove trailing '\n' */
5050Sstevel@tonic-gate 			cur_pkg[strlen(cur_pkg) - 1] = '\0';
5060Sstevel@tonic-gate 			doing_pkg = B_TRUE;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 		} else {
5090Sstevel@tonic-gate 			/* end of package delimiter */
5100Sstevel@tonic-gate 			char tmp_pkg[1024];
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 			if (!doing_pkg) {
513132Srobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
5140Sstevel@tonic-gate 				    "end package delimiter without start");
5150Sstevel@tonic-gate 				goto bail;
5160Sstevel@tonic-gate 			}
5170Sstevel@tonic-gate 			/*
5180Sstevel@tonic-gate 			 * Get specified pkg name, fail if it doesn't match
5190Sstevel@tonic-gate 			 * the package specified by the last # Begin.
5200Sstevel@tonic-gate 			 */
5210Sstevel@tonic-gate 			(void) strncpy(tmp_pkg, line +
5220Sstevel@tonic-gate 			    (sizeof (LIBIPSEC_ALGS_LINE_PKGEND) - 1),
5230Sstevel@tonic-gate 			    sizeof (tmp_pkg));
5240Sstevel@tonic-gate 			/* remove trailing '\n' */
5250Sstevel@tonic-gate 			tmp_pkg[strlen(tmp_pkg) - 1] = '\0';
5260Sstevel@tonic-gate 			if (strncmp(cur_pkg, tmp_pkg, sizeof (cur_pkg)) != 0)
5270Sstevel@tonic-gate 				goto bail;
5280Sstevel@tonic-gate 			doing_pkg = B_FALSE;
5290Sstevel@tonic-gate 		}
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	*num = rc_num;
5330Sstevel@tonic-gate 	return (rc);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate bail:
5360Sstevel@tonic-gate 	if (strlen(diag_buf) > 0) {
5370Sstevel@tonic-gate 		syslog(LOG_ERR, "possibly corrupt %s file: %s\n",
5380Sstevel@tonic-gate 		    INET_IPSECALGSFILE, diag_buf);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 	_clean_trash(rc, rc_num);
5410Sstevel@tonic-gate 	return (NULL);
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate /*
5450Sstevel@tonic-gate  * If alg_context is NULL, update the library's cached copy of
5460Sstevel@tonic-gate  * INET_IPSECALGSFILE.  If alg_context is non-NULL, hang a
5470Sstevel@tonic-gate  * library-internal representation of a cached copy.  The latter is useful
5480Sstevel@tonic-gate  * for routines in libipsecutil that _write_ the contents out.
5490Sstevel@tonic-gate  */
5500Sstevel@tonic-gate void
5510Sstevel@tonic-gate _build_internal_algs(ipsec_proto_t **alg_context, int *alg_nums)
5520Sstevel@tonic-gate {
5530Sstevel@tonic-gate 	FILE *f = NULL;
5540Sstevel@tonic-gate 	int fd, rc, trash_num;
5550Sstevel@tonic-gate 	ipsec_proto_t *new_protos = NULL, *trash;
5560Sstevel@tonic-gate 	time_t filetime;
5570Sstevel@tonic-gate 	struct stat statbuf;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	/*
5600Sstevel@tonic-gate 	 * Construct new_protos from the file.
5610Sstevel@tonic-gate 	 */
5620Sstevel@tonic-gate 	if (alg_context == NULL) {
5630Sstevel@tonic-gate 		/*
5640Sstevel@tonic-gate 		 * Check the time w/o holding the lock.  This is just a
5650Sstevel@tonic-gate 		 * cache reality check.  We'll do it again for real if this
5660Sstevel@tonic-gate 		 * surface check fails.
5670Sstevel@tonic-gate 		 */
5680Sstevel@tonic-gate 		if (stat(INET_IPSECALGSFILE, &statbuf) == -1 ||
5690Sstevel@tonic-gate 		    (statbuf.st_mtime < proto_last_update &&
5700Sstevel@tonic-gate 			protos != NULL))
5710Sstevel@tonic-gate 			return;
572132Srobinson 		(void) rw_wrlock(&proto_rw);
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	fd = open(INET_IPSECALGSFILE, O_RDONLY);
5760Sstevel@tonic-gate 	if (fd != -1) {
5770Sstevel@tonic-gate 		f = fdopen(fd, "r");
5780Sstevel@tonic-gate 		if (f == NULL) {
579132Srobinson 			(void) close(fd);
5800Sstevel@tonic-gate 		} else {
5810Sstevel@tonic-gate 			rc = fstat(fd, &statbuf);
5820Sstevel@tonic-gate 			if (rc != -1) {
5830Sstevel@tonic-gate 				/*
5840Sstevel@tonic-gate 				 * Update if the file is newer than our
5850Sstevel@tonic-gate 				 * last cached copy.
5860Sstevel@tonic-gate 				 */
5870Sstevel@tonic-gate 				filetime = statbuf.st_mtime;
5880Sstevel@tonic-gate 				if (alg_context != NULL ||
5890Sstevel@tonic-gate 				    filetime > proto_last_update)
5900Sstevel@tonic-gate 					new_protos = build_list(f, &rc);
5910Sstevel@tonic-gate 			}
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (alg_context == NULL) {
5960Sstevel@tonic-gate 		/*
5970Sstevel@tonic-gate 		 * If we have failed anywhere above, new_protoss will be NULL.
5980Sstevel@tonic-gate 		 * This way, the previous cached protos will still be intact.
5990Sstevel@tonic-gate 		 */
6000Sstevel@tonic-gate 		if (new_protos != NULL) {
6010Sstevel@tonic-gate 			proto_last_update = filetime;
6020Sstevel@tonic-gate 			trash = protos;
6030Sstevel@tonic-gate 			trash_num = num_protos;
6040Sstevel@tonic-gate 			protos = new_protos;
6050Sstevel@tonic-gate 			num_protos = rc;
6060Sstevel@tonic-gate 		} else {
6070Sstevel@tonic-gate 			/*
6080Sstevel@tonic-gate 			 * Else the original protocols and algorithms lists
6090Sstevel@tonic-gate 			 * remains the same.
6100Sstevel@tonic-gate 			 */
6110Sstevel@tonic-gate 			trash = NULL;
6120Sstevel@tonic-gate 		}
613132Srobinson 		(void) rw_unlock(&proto_rw);
6140Sstevel@tonic-gate 		_clean_trash(trash, trash_num);
6150Sstevel@tonic-gate 	} else {
6160Sstevel@tonic-gate 		/*
6170Sstevel@tonic-gate 		 * Assume caller has done the appropriate locking,
6180Sstevel@tonic-gate 		 * cleanup, etc.  And if new_protos is NULL, it's the caller's
6190Sstevel@tonic-gate 		 * problem.
6200Sstevel@tonic-gate 		 */
6210Sstevel@tonic-gate 		*alg_context = new_protos;
6220Sstevel@tonic-gate 		*alg_nums = rc;
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	/* Since f is read-only, can avoid all of the failures... */
6260Sstevel@tonic-gate 	if (f != NULL)
6270Sstevel@tonic-gate 		(void) fclose(f);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate  * Assume input is 0-terminated.
6320Sstevel@tonic-gate  */
6330Sstevel@tonic-gate static int *
6340Sstevel@tonic-gate duplicate_intarr(int *orig)
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 	size_t allocsize = sizeof (int);
6370Sstevel@tonic-gate 	int *iwalker = orig;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (orig == NULL)
6400Sstevel@tonic-gate 		return (NULL);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	while (*iwalker != 0) {
6430Sstevel@tonic-gate 		allocsize += sizeof (int);
6440Sstevel@tonic-gate 		iwalker++;
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	iwalker = malloc(allocsize);
6480Sstevel@tonic-gate 	if (iwalker != NULL)
649132Srobinson 		(void) memcpy(iwalker, orig, allocsize);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	return (iwalker);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate  * Assume input is NULL terminated.
6560Sstevel@tonic-gate  */
6570Sstevel@tonic-gate static char **
6580Sstevel@tonic-gate duplicate_strarr(char **orig)
6590Sstevel@tonic-gate {
6600Sstevel@tonic-gate 	int i;
6610Sstevel@tonic-gate 	char **swalker;
6620Sstevel@tonic-gate 	char **newbie;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if (orig == NULL)
6650Sstevel@tonic-gate 		return (NULL);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	/* count number of elements in source array */
6680Sstevel@tonic-gate 	for (swalker = orig; *swalker != NULL; swalker++);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	/* use calloc() to get NULL-initialization */
6710Sstevel@tonic-gate 	newbie = calloc(swalker - orig + 1, sizeof (char *));
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (newbie != NULL) {
6740Sstevel@tonic-gate 		/* do the copy */
6750Sstevel@tonic-gate 		for (i = 0; orig[i] != NULL; i++) {
6760Sstevel@tonic-gate 			newbie[i] = strdup(orig[i]);
6770Sstevel@tonic-gate 			if (newbie[i] == NULL) {
6780Sstevel@tonic-gate 				for (swalker = newbie; *swalker != NULL;
6790Sstevel@tonic-gate 				    swalker++)
6800Sstevel@tonic-gate 					free(*swalker);
6810Sstevel@tonic-gate 				free(newbie);
6820Sstevel@tonic-gate 				return (NULL);
6830Sstevel@tonic-gate 			}
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	return (newbie);
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate struct ipsecalgent *
6910Sstevel@tonic-gate _duplicate_alg(struct ipsecalgent *orig)
6920Sstevel@tonic-gate {
6930Sstevel@tonic-gate 	struct ipsecalgent *rc;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/* use calloc() to get NULL-initialization. */
6960Sstevel@tonic-gate 	rc = calloc(1, sizeof (struct ipsecalgent));
6970Sstevel@tonic-gate 	if (rc == NULL)
6980Sstevel@tonic-gate 		return (NULL);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	rc->a_proto_num = orig->a_proto_num;
7010Sstevel@tonic-gate 	rc->a_alg_num = orig->a_alg_num;
7020Sstevel@tonic-gate 	rc->a_key_increment = orig->a_key_increment;
7030Sstevel@tonic-gate 	rc->a_mech_name = strdup(orig->a_mech_name);
7040Sstevel@tonic-gate 	rc->a_block_sizes = duplicate_intarr(orig->a_block_sizes);
7050Sstevel@tonic-gate 	rc->a_key_sizes = duplicate_intarr(orig->a_key_sizes);
7060Sstevel@tonic-gate 	rc->a_names = duplicate_strarr(orig->a_names);
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	if (rc->a_mech_name == NULL || rc->a_block_sizes == NULL ||
7090Sstevel@tonic-gate 	    rc->a_key_sizes == NULL || rc->a_names == NULL) {
7100Sstevel@tonic-gate 		freeipsecalgent(rc);
7110Sstevel@tonic-gate 		return (NULL);
7120Sstevel@tonic-gate 	}
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	return (rc);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate /*
7180Sstevel@tonic-gate  * Assume the rwlock is held for reading.
7190Sstevel@tonic-gate  */
7200Sstevel@tonic-gate static ipsec_proto_t *
7210Sstevel@tonic-gate findprotobynum(int proto_num)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	int i;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	for (i = 0; i < num_protos; i++) {
7260Sstevel@tonic-gate 		if (protos[i].proto_num == proto_num)
7270Sstevel@tonic-gate 			return (protos + i);
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	return (NULL);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate static ipsec_proto_t *
7340Sstevel@tonic-gate findprotobyname(const char *name)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	int i;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	if (name == NULL)
7390Sstevel@tonic-gate 		return (NULL);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	for (i = 0; i < num_protos; i++) {
7420Sstevel@tonic-gate 		/* Can use strcasecmp because our proto_name is bounded. */
7430Sstevel@tonic-gate 		if (strcasecmp(protos[i].proto_name, name) == 0)
7440Sstevel@tonic-gate 			return (protos + i);
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	return (NULL);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate int *
7510Sstevel@tonic-gate _real_getipsecprotos(int *nentries)
7520Sstevel@tonic-gate {
7530Sstevel@tonic-gate 	int *rc, i;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	if (nentries == NULL)
7560Sstevel@tonic-gate 		return (NULL);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
7590Sstevel@tonic-gate 
760132Srobinson 	(void) rw_rdlock(&proto_rw);
7610Sstevel@tonic-gate 	*nentries = num_protos;
7620Sstevel@tonic-gate 	/*
7630Sstevel@tonic-gate 	 * Allocate 1 byte if there are no protocols so a non-NULL return
7640Sstevel@tonic-gate 	 * happens.
7650Sstevel@tonic-gate 	 */
7660Sstevel@tonic-gate 	rc = malloc((num_protos == 0) ? 1 : num_protos * sizeof (int));
7670Sstevel@tonic-gate 	if (rc != NULL) {
7680Sstevel@tonic-gate 		for (i = 0; i < num_protos; i++)
7690Sstevel@tonic-gate 			rc[i] = protos[i].proto_num;
7700Sstevel@tonic-gate 	}
771132Srobinson 	(void) rw_unlock(&proto_rw);
7720Sstevel@tonic-gate 	return (rc);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate int *
7760Sstevel@tonic-gate _real_getipsecalgs(int *nentries, int proto_num)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	int *rc = NULL, i;
7790Sstevel@tonic-gate 	ipsec_proto_t *proto;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if (nentries == NULL)
7820Sstevel@tonic-gate 		return (NULL);
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
7850Sstevel@tonic-gate 
786132Srobinson 	(void) rw_rdlock(&proto_rw);
7870Sstevel@tonic-gate 	proto = findprotobynum(proto_num);
7880Sstevel@tonic-gate 	if (proto != NULL) {
7890Sstevel@tonic-gate 		*nentries = proto->proto_numalgs;
7900Sstevel@tonic-gate 		/*
7910Sstevel@tonic-gate 		 * Allocate 1 byte if there are no algorithms so a non-NULL
7920Sstevel@tonic-gate 		 * return happens.
7930Sstevel@tonic-gate 		 */
7940Sstevel@tonic-gate 		rc = malloc((proto->proto_numalgs == 0) ? 1 :
7950Sstevel@tonic-gate 		    proto->proto_numalgs * sizeof (int));
7960Sstevel@tonic-gate 		if (rc != NULL) {
7970Sstevel@tonic-gate 			for (i = 0; i < proto->proto_numalgs; i++)
7980Sstevel@tonic-gate 				rc[i] = proto->proto_algs[i]->a_alg_num;
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 	}
801132Srobinson 	(void) rw_unlock(&proto_rw);
8020Sstevel@tonic-gate 	return (rc);
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate struct ipsecalgent *
8060Sstevel@tonic-gate getipsecalgbyname(const char *name, int proto_num, int *errnop)
8070Sstevel@tonic-gate {
8080Sstevel@tonic-gate 	ipsec_proto_t *proto;
8090Sstevel@tonic-gate 	struct ipsecalgent *rc = NULL;
8100Sstevel@tonic-gate 	int i, my_errno = ENOENT;
8110Sstevel@tonic-gate 	char **name_check;
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
8140Sstevel@tonic-gate 	if (name == NULL) {
8150Sstevel@tonic-gate 		my_errno = EFAULT;
8160Sstevel@tonic-gate 		goto bail;
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 
819132Srobinson 	(void) rw_rdlock(&proto_rw);
8200Sstevel@tonic-gate 	proto = findprotobynum(proto_num);
8210Sstevel@tonic-gate 	if (proto != NULL) {
8220Sstevel@tonic-gate 		for (i = 0; i < proto->proto_numalgs; i++) {
8230Sstevel@tonic-gate 			for (name_check = proto->proto_algs[i]->a_names;
8240Sstevel@tonic-gate 			    *name_check != NULL; name_check++) {
8250Sstevel@tonic-gate 				/*
8260Sstevel@tonic-gate 				 * Can use strcasecmp because our name_check
8270Sstevel@tonic-gate 				 * is bounded.
8280Sstevel@tonic-gate 				 */
8290Sstevel@tonic-gate 				if (strcasecmp(*name_check, name) == 0) {
8300Sstevel@tonic-gate 					/* found match */
8310Sstevel@tonic-gate 					rc = _duplicate_alg(
8320Sstevel@tonic-gate 					    proto->proto_algs[i]);
8330Sstevel@tonic-gate 					my_errno = (rc == NULL) ? ENOMEM : 0;
834132Srobinson 					(void) rw_unlock(&proto_rw);
8350Sstevel@tonic-gate 					goto bail;
8360Sstevel@tonic-gate 				}
8370Sstevel@tonic-gate 			}
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 	} else {
8400Sstevel@tonic-gate 		my_errno = EINVAL;
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
843132Srobinson 	(void) rw_unlock(&proto_rw);
8440Sstevel@tonic-gate bail:
8450Sstevel@tonic-gate 	if (errnop != NULL)
8460Sstevel@tonic-gate 		*errnop = my_errno;
8470Sstevel@tonic-gate 	return (rc);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate struct ipsecalgent *
8510Sstevel@tonic-gate getipsecalgbynum(int alg_num, int proto_num, int *errnop)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	ipsec_proto_t *proto;
8540Sstevel@tonic-gate 	struct ipsecalgent *rc = NULL;
8550Sstevel@tonic-gate 	int i, my_errno = ENOENT;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
8580Sstevel@tonic-gate 
859132Srobinson 	(void) rw_rdlock(&proto_rw);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	proto = findprotobynum(proto_num);
8620Sstevel@tonic-gate 	if (proto != NULL) {
8630Sstevel@tonic-gate 		for (i = 0; i < proto->proto_numalgs; i++) {
8640Sstevel@tonic-gate 			if (proto->proto_algs[i]->a_alg_num == alg_num) {
8650Sstevel@tonic-gate 				rc = _duplicate_alg(proto->proto_algs[i]);
8660Sstevel@tonic-gate 				my_errno = (rc == NULL) ? ENOMEM : 0;
8670Sstevel@tonic-gate 				break;
8680Sstevel@tonic-gate 			}
8690Sstevel@tonic-gate 		}
8700Sstevel@tonic-gate 	} else {
8710Sstevel@tonic-gate 		my_errno = EINVAL;
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 
874132Srobinson 	(void) rw_unlock(&proto_rw);
8750Sstevel@tonic-gate 	if (errnop != NULL)
8760Sstevel@tonic-gate 		*errnop = my_errno;
8770Sstevel@tonic-gate 	return (rc);
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate int
8810Sstevel@tonic-gate getipsecprotobyname(const char *proto_name)
8820Sstevel@tonic-gate {
8830Sstevel@tonic-gate 	int rc = -1;
8840Sstevel@tonic-gate 	ipsec_proto_t *proto;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
8870Sstevel@tonic-gate 
888132Srobinson 	(void) rw_rdlock(&proto_rw);
8890Sstevel@tonic-gate 	proto = findprotobyname(proto_name);
8900Sstevel@tonic-gate 	if (proto != NULL)
8910Sstevel@tonic-gate 		rc = proto->proto_num;
892132Srobinson 	(void) rw_unlock(&proto_rw);
8930Sstevel@tonic-gate 	return (rc);
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate char *
8970Sstevel@tonic-gate getipsecprotobynum(int proto_num)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	ipsec_proto_t *proto;
9000Sstevel@tonic-gate 	char *rc = NULL;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
9030Sstevel@tonic-gate 
904132Srobinson 	(void) rw_rdlock(&proto_rw);
9050Sstevel@tonic-gate 	proto = findprotobynum(proto_num);
9060Sstevel@tonic-gate 	if (proto != NULL)
9070Sstevel@tonic-gate 		rc = strdup(proto->proto_name);
9080Sstevel@tonic-gate 
909132Srobinson 	(void) rw_unlock(&proto_rw);
9100Sstevel@tonic-gate 	return (rc);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate void
9140Sstevel@tonic-gate freeipsecalgent(struct ipsecalgent *ptr)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate 	char **walker;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	if (ptr == NULL)
9190Sstevel@tonic-gate 		return;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	if (ptr->a_names != NULL) {
9220Sstevel@tonic-gate 		for (walker = ptr->a_names; *walker != NULL; walker++)
9230Sstevel@tonic-gate 			free(*walker);
9240Sstevel@tonic-gate 	}
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	/*
9270Sstevel@tonic-gate 	 * Remember folks, free(NULL) works.
9280Sstevel@tonic-gate 	 */
9290Sstevel@tonic-gate 	free(ptr->a_names);
9300Sstevel@tonic-gate 	free(ptr->a_mech_name);
9310Sstevel@tonic-gate 	free(ptr->a_block_sizes);
9320Sstevel@tonic-gate 	free(ptr->a_key_sizes);
9330Sstevel@tonic-gate 	free(ptr);
9340Sstevel@tonic-gate }
935