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