xref: /illumos-gate/usr/src/lib/libnsl/ipsec/algs.c (revision e8031f0a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
2261961e0fSrobinson 
237c478bd9Sstevel@tonic-gate /*
24*e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
30*e8031f0aSraf #include "mt.h"
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/errno.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <ipsec_util.h>
357c478bd9Sstevel@tonic-gate #include <netdb.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <synch.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <strings.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <syslog.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /* Globals... */
467c478bd9Sstevel@tonic-gate static rwlock_t proto_rw = DEFAULTRWLOCK; /* Protects cached algorithm list. */
477c478bd9Sstevel@tonic-gate static time_t proto_last_update;
487c478bd9Sstevel@tonic-gate static ipsec_proto_t *protos;
497c478bd9Sstevel@tonic-gate static int num_protos;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate void
527c478bd9Sstevel@tonic-gate _clean_trash(ipsec_proto_t *proto, int num)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	int alg_offset;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if (proto == NULL)
577c478bd9Sstevel@tonic-gate 		return;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	while (num-- != 0) {
607c478bd9Sstevel@tonic-gate 		free(proto[num].proto_name);
617c478bd9Sstevel@tonic-gate 		free(proto[num].proto_pkg);
627c478bd9Sstevel@tonic-gate 		for (alg_offset = 0; alg_offset < proto[num].proto_numalgs;
637c478bd9Sstevel@tonic-gate 		    alg_offset++)
647c478bd9Sstevel@tonic-gate 			freeipsecalgent(proto[num].proto_algs[alg_offset]);
657c478bd9Sstevel@tonic-gate 		free(proto[num].proto_algs);
667c478bd9Sstevel@tonic-gate 		for (alg_offset = 0; alg_offset < proto[num].proto_algs_npkgs;
677c478bd9Sstevel@tonic-gate 		    alg_offset++)
687c478bd9Sstevel@tonic-gate 			free(proto[num].proto_algs_pkgs[alg_offset].pkg_name);
697c478bd9Sstevel@tonic-gate 		free(proto[num].proto_algs_pkgs);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	free(proto);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static const char *pipechar = "|";
767c478bd9Sstevel@tonic-gate static const char *comma = ",";
777c478bd9Sstevel@tonic-gate static const char *dash = "-";
787c478bd9Sstevel@tonic-gate static const char *slash = "/";
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Returns >= 0 if success (and > 0 means "increment").
827c478bd9Sstevel@tonic-gate  * Returns -1 if failure.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate static int
857c478bd9Sstevel@tonic-gate build_keysizes(int **sizep, char *input_string)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	char *lasts, *token;
887c478bd9Sstevel@tonic-gate 	int *key_sizes = NULL, num_sizes, key_low, key_high, key_default;
897c478bd9Sstevel@tonic-gate 	int key_increment = 0;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * Okay, let's check the format of the key string.  It'll be either:
937c478bd9Sstevel@tonic-gate 	 *
947c478bd9Sstevel@tonic-gate 	 * enumeration: size1,size2...,sizeN
957c478bd9Sstevel@tonic-gate 	 * range: defaultSize/sizeLow-sizeHi,increment
967c478bd9Sstevel@tonic-gate 	 *
977c478bd9Sstevel@tonic-gate 	 * In the case of an enumeration, the default key size is the
987c478bd9Sstevel@tonic-gate 	 * first one in the list.
997c478bd9Sstevel@tonic-gate 	 */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (strchr(input_string, '/') != NULL) {
1027c478bd9Sstevel@tonic-gate 		/* key sizes specified by range */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		/* default */
1057c478bd9Sstevel@tonic-gate 		token = strtok_r(input_string, slash, &lasts);
1067c478bd9Sstevel@tonic-gate 		if (token == NULL || (key_default = atoi(token)) == 0)
1077c478bd9Sstevel@tonic-gate 			return (-1);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		/* low */
1107c478bd9Sstevel@tonic-gate 		token = strtok_r(NULL, dash, &lasts);
1117c478bd9Sstevel@tonic-gate 		if (token == NULL || (key_low = atoi(token)) == 0)
1127c478bd9Sstevel@tonic-gate 			return (-1);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		/* high */
1157c478bd9Sstevel@tonic-gate 		token = strtok_r(NULL, comma, &lasts);
1167c478bd9Sstevel@tonic-gate 		if (token == NULL || (key_high = atoi(token)) == 0 ||
1177c478bd9Sstevel@tonic-gate 		    key_high <= key_low)
1187c478bd9Sstevel@tonic-gate 			return (-1);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		/* increment */
1217c478bd9Sstevel@tonic-gate 		token = strtok_r(NULL, "", &lasts);
1227c478bd9Sstevel@tonic-gate 		if (token == NULL || (key_increment = atoi(token)) == 0)
1237c478bd9Sstevel@tonic-gate 			return (-1);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		key_sizes = (int *)malloc(LIBIPSEC_ALGS_KEY_NUM_VAL *
1267c478bd9Sstevel@tonic-gate 		    sizeof (int));
1277c478bd9Sstevel@tonic-gate 		if (key_sizes == NULL)
1287c478bd9Sstevel@tonic-gate 			return (-1);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		key_sizes[LIBIPSEC_ALGS_KEY_DEF_IDX] = key_default;
1317c478bd9Sstevel@tonic-gate 		key_sizes[LIBIPSEC_ALGS_KEY_MIN_IDX] = key_low;
1327c478bd9Sstevel@tonic-gate 		key_sizes[LIBIPSEC_ALGS_KEY_MAX_IDX] = key_high;
1337c478bd9Sstevel@tonic-gate 		key_sizes[LIBIPSEC_ALGS_KEY_MAX_IDX + 1] = 0;
1347c478bd9Sstevel@tonic-gate 	} else {
1357c478bd9Sstevel@tonic-gate 		/* key sizes specified by enumeration */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		key_sizes = (int *)malloc(sizeof (int));
1387c478bd9Sstevel@tonic-gate 		if (key_sizes == NULL)
1397c478bd9Sstevel@tonic-gate 			return (-1);
1407c478bd9Sstevel@tonic-gate 		num_sizes = 0;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		token = strtok_r(input_string, comma, &lasts);
1437c478bd9Sstevel@tonic-gate 		if (token == NULL || key_sizes == NULL)
1447c478bd9Sstevel@tonic-gate 			return (-1);
1457c478bd9Sstevel@tonic-gate 		*key_sizes = 0;
1467c478bd9Sstevel@tonic-gate 		do {
1477c478bd9Sstevel@tonic-gate 			int *nks;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 			nks = (int *)realloc(key_sizes,
1507c478bd9Sstevel@tonic-gate 			    sizeof (int) * ((++num_sizes) + 1));
1517c478bd9Sstevel@tonic-gate 			if (nks == NULL) {
1527c478bd9Sstevel@tonic-gate 				free(key_sizes);
1537c478bd9Sstevel@tonic-gate 				return (-1);
1547c478bd9Sstevel@tonic-gate 			}
1557c478bd9Sstevel@tonic-gate 			key_sizes = nks;
1567c478bd9Sstevel@tonic-gate 			/* Can't check for atoi() == 0 here... */
1577c478bd9Sstevel@tonic-gate 			key_sizes[num_sizes - 1] = atoi(token);
1587c478bd9Sstevel@tonic-gate 			key_sizes[num_sizes] = 0;
1597c478bd9Sstevel@tonic-gate 		} while ((token = strtok_r(NULL, comma, &lasts)) != NULL);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	*sizep = key_sizes;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	return (key_increment);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * Find the execution mode corresponding to the given string.
1697c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate int
1727c478bd9Sstevel@tonic-gate _str_to_ipsec_exec_mode(char *str, ipsecalgs_exec_mode_t *exec_mode)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	if (strcmp(str, "sync") == 0) {
1757c478bd9Sstevel@tonic-gate 		*exec_mode = LIBIPSEC_ALGS_EXEC_SYNC;
1767c478bd9Sstevel@tonic-gate 		return (0);
1777c478bd9Sstevel@tonic-gate 	} else if (strcmp(str, "async") == 0) {
1787c478bd9Sstevel@tonic-gate 		*exec_mode = LIBIPSEC_ALGS_EXEC_ASYNC;
1797c478bd9Sstevel@tonic-gate 		return (0);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	return (-1);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * Given a file pointer, read all the text from the file and convert it into
1877c478bd9Sstevel@tonic-gate  * a bunch of ipsec_proto_t's, each with an array of struct ipsecalgent
1887c478bd9Sstevel@tonic-gate  * pointers - one for each algorithm.
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate static ipsec_proto_t *
1917c478bd9Sstevel@tonic-gate build_list(FILE *f, int *num)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	char line[1024];
1947c478bd9Sstevel@tonic-gate 	char *token, *lasts, *alg_names, *ef_name, *key_string, *block_string;
1957c478bd9Sstevel@tonic-gate 	char *proto_name;
1967c478bd9Sstevel@tonic-gate 	ipsec_proto_t *rc = NULL, *new_proto = NULL;
1977c478bd9Sstevel@tonic-gate 	int *block_sizes, *key_sizes;
1987c478bd9Sstevel@tonic-gate 	int rc_num = 0, key_increment;
1997c478bd9Sstevel@tonic-gate 	int new_num, alg_num, num_sizes;
2007c478bd9Sstevel@tonic-gate 	struct ipsecalgent *curalg, **newalglist;
2017c478bd9Sstevel@tonic-gate 	char cur_pkg[1024];
2027c478bd9Sstevel@tonic-gate 	boolean_t doing_pkg = B_FALSE;
2037c478bd9Sstevel@tonic-gate 	ipsecalgs_exec_mode_t exec_mode;
2047c478bd9Sstevel@tonic-gate 	char diag_buf[128];
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	diag_buf[0] = '\0';
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), f) != NULL) {
2097c478bd9Sstevel@tonic-gate 		if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PROTO,
2107c478bd9Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_PROTO) - 1) != 0 &&
2117c478bd9Sstevel@tonic-gate 		    strncasecmp(line, LIBIPSEC_ALGS_LINE_ALG,
2127c478bd9Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_ALG) - 1) != 0 &&
2137c478bd9Sstevel@tonic-gate 		    strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGSTART,
2147c478bd9Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1) != 0 &&
2157c478bd9Sstevel@tonic-gate 		    strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGEND,
2167c478bd9Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_PKGEND) - 1) != 0) {
2177c478bd9Sstevel@tonic-gate 			if ((token = strtok_r(line, " \t\n", &lasts)) == NULL ||
2187c478bd9Sstevel@tonic-gate 			    token[0] == '#') {
2197c478bd9Sstevel@tonic-gate 				continue;
2207c478bd9Sstevel@tonic-gate 			} else {
22161961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
2227c478bd9Sstevel@tonic-gate 					"non-recognized start of line");
2237c478bd9Sstevel@tonic-gate 				goto bail;
2247c478bd9Sstevel@tonic-gate 			}
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PROTO,
2287c478bd9Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_PROTO) - 1) == 0) {
2297c478bd9Sstevel@tonic-gate 			/* current line defines a new protocol */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 			/* skip the protocol token */
2327c478bd9Sstevel@tonic-gate 			token = strtok_r(line, pipechar, &lasts);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 			/* protocol number */
2357c478bd9Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
2367c478bd9Sstevel@tonic-gate 			if (token == NULL || (new_num = atoi(token)) == 0) {
23761961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
2387c478bd9Sstevel@tonic-gate 				    "invalid protocol number");
2397c478bd9Sstevel@tonic-gate 				goto bail;
2407c478bd9Sstevel@tonic-gate 			}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 			/* protocol name */
2437c478bd9Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
2447c478bd9Sstevel@tonic-gate 			if (token == NULL) {
24561961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
2467c478bd9Sstevel@tonic-gate 				    "cannot read protocol name");
2477c478bd9Sstevel@tonic-gate 				goto bail;
2487c478bd9Sstevel@tonic-gate 			}
2497c478bd9Sstevel@tonic-gate 			proto_name = token;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 			/* execution mode */
2527c478bd9Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
2537c478bd9Sstevel@tonic-gate 			if (token == NULL) {
25461961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
2557c478bd9Sstevel@tonic-gate 				    "cannot read execution mode");
2567c478bd9Sstevel@tonic-gate 				goto bail;
2577c478bd9Sstevel@tonic-gate 			}
2587c478bd9Sstevel@tonic-gate 			/* remove trailing '\n' */
2597c478bd9Sstevel@tonic-gate 			token[strlen(token) - 1] = '\0';
2607c478bd9Sstevel@tonic-gate 			if (_str_to_ipsec_exec_mode(token, &exec_mode) != 0) {
26161961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
2627c478bd9Sstevel@tonic-gate 				    "invalid execution mode: \"%s\"", token);
2637c478bd9Sstevel@tonic-gate 				goto bail;
2647c478bd9Sstevel@tonic-gate 			}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 			/* initialize protocol structure */
2677c478bd9Sstevel@tonic-gate 			rc_num++;
2687c478bd9Sstevel@tonic-gate 			new_proto = (ipsec_proto_t *)realloc(rc,
2697c478bd9Sstevel@tonic-gate 			    sizeof (ipsec_proto_t) * rc_num);
2707c478bd9Sstevel@tonic-gate 			rc = new_proto;
2717c478bd9Sstevel@tonic-gate 			if (new_proto == NULL)
2727c478bd9Sstevel@tonic-gate 				goto bail;
2737c478bd9Sstevel@tonic-gate 			new_proto += (rc_num - 1);
2747c478bd9Sstevel@tonic-gate 			new_proto->proto_num = new_num;
2757c478bd9Sstevel@tonic-gate 			new_proto->proto_algs = NULL;
2767c478bd9Sstevel@tonic-gate 			new_proto->proto_numalgs = 0;
2777c478bd9Sstevel@tonic-gate 			new_proto->proto_name = strdup(proto_name);
2787c478bd9Sstevel@tonic-gate 			if (new_proto->proto_name == NULL)
2797c478bd9Sstevel@tonic-gate 				goto bail;
2807c478bd9Sstevel@tonic-gate 			new_proto->proto_exec_mode = exec_mode;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 			if (doing_pkg) {
2837c478bd9Sstevel@tonic-gate 				/* record proto as being part of current pkg */
2847c478bd9Sstevel@tonic-gate 				new_proto->proto_pkg = strdup(cur_pkg);
2857c478bd9Sstevel@tonic-gate 				if (new_proto->proto_pkg == NULL)
2867c478bd9Sstevel@tonic-gate 					goto bail;
2877c478bd9Sstevel@tonic-gate 			} else {
2887c478bd9Sstevel@tonic-gate 				new_proto->proto_pkg = NULL;
2897c478bd9Sstevel@tonic-gate 			}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 			new_proto->proto_algs_pkgs = NULL;
2927c478bd9Sstevel@tonic-gate 			new_proto->proto_algs_npkgs = 0;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		} else if (strncasecmp(line, LIBIPSEC_ALGS_LINE_ALG,
2957c478bd9Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_ALG) - 1) == 0) {
2967c478bd9Sstevel@tonic-gate 			/* current line defines a new algorithm */
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 			/* skip the algorithm token */
2997c478bd9Sstevel@tonic-gate 			token = strtok_r(line, pipechar, &lasts);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 			/* protocol number */
3027c478bd9Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3037c478bd9Sstevel@tonic-gate 			if (token == NULL || (new_num = atoi(token)) == 0) {
30461961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3057c478bd9Sstevel@tonic-gate 				    "invalid algorithm number");
3067c478bd9Sstevel@tonic-gate 				goto bail;
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 			/* We can be O(N) for now.  There aren't that many. */
3107c478bd9Sstevel@tonic-gate 			for (new_proto = rc; new_proto < (rc + new_num);
3117c478bd9Sstevel@tonic-gate 			    new_proto++)
3127c478bd9Sstevel@tonic-gate 				if (new_proto->proto_num == new_num)
3137c478bd9Sstevel@tonic-gate 					break;
3147c478bd9Sstevel@tonic-gate 			if (new_proto == (rc + new_num)) {
31561961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3167c478bd9Sstevel@tonic-gate 				    "invalid protocol number %d for algorithm",
3177c478bd9Sstevel@tonic-gate 				    new_num);
3187c478bd9Sstevel@tonic-gate 				goto bail;
3197c478bd9Sstevel@tonic-gate 			}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 			/* algorithm number */
3227c478bd9Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3237c478bd9Sstevel@tonic-gate 			if (token == NULL) {
32461961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3257c478bd9Sstevel@tonic-gate 				    "cannot read algorithm number");
3267c478bd9Sstevel@tonic-gate 				goto bail;
3277c478bd9Sstevel@tonic-gate 			}
3287c478bd9Sstevel@tonic-gate 			/* Can't check for 0 here. */
3297c478bd9Sstevel@tonic-gate 			alg_num = atoi(token);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 			/* algorithm names */
3327c478bd9Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3337c478bd9Sstevel@tonic-gate 			if (token == NULL) {
33461961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3357c478bd9Sstevel@tonic-gate 				    "cannot read algorithm number");
3367c478bd9Sstevel@tonic-gate 				goto bail;
3377c478bd9Sstevel@tonic-gate 			}
3387c478bd9Sstevel@tonic-gate 			alg_names = token;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 			/* mechanism name */
3417c478bd9Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3427c478bd9Sstevel@tonic-gate 			if (token == NULL) {
34361961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3447c478bd9Sstevel@tonic-gate 				    "cannot read mechanism name for alg %d "
3457c478bd9Sstevel@tonic-gate 				    "(proto %d)", alg_num,
3467c478bd9Sstevel@tonic-gate 				    new_proto->proto_num);
3477c478bd9Sstevel@tonic-gate 				goto bail;
3487c478bd9Sstevel@tonic-gate 			}
3497c478bd9Sstevel@tonic-gate 			ef_name = token;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 			/* key sizes */
3527c478bd9Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3537c478bd9Sstevel@tonic-gate 			if (token == NULL) {
35461961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3557c478bd9Sstevel@tonic-gate 				    "cannot read key sizes for alg %d "
3567c478bd9Sstevel@tonic-gate 				    "(proto %d)", alg_num,
3577c478bd9Sstevel@tonic-gate 				    new_proto->proto_num);
3587c478bd9Sstevel@tonic-gate 				goto bail;
3597c478bd9Sstevel@tonic-gate 			}
3607c478bd9Sstevel@tonic-gate 			key_string = token;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 			/* block sizes */
3637c478bd9Sstevel@tonic-gate 			token = strtok_r(NULL, pipechar, &lasts);
3647c478bd9Sstevel@tonic-gate 			if (token == NULL) {
36561961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3667c478bd9Sstevel@tonic-gate 				    "cannot read mechanism name for alg %d "
3677c478bd9Sstevel@tonic-gate 				    "(proto %d)", alg_num,
3687c478bd9Sstevel@tonic-gate 				    new_proto->proto_num);
3697c478bd9Sstevel@tonic-gate 				goto bail;
3707c478bd9Sstevel@tonic-gate 			}
3717c478bd9Sstevel@tonic-gate 			block_string = token;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 			/* extract key sizes */
3747c478bd9Sstevel@tonic-gate 			key_increment = build_keysizes(&key_sizes, key_string);
3757c478bd9Sstevel@tonic-gate 			if (key_increment == -1) {
37661961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3777c478bd9Sstevel@tonic-gate 				    "invalid key sizes for alg %d (proto %d)",
3787c478bd9Sstevel@tonic-gate 				    alg_num, new_proto->proto_num);
3797c478bd9Sstevel@tonic-gate 				goto bail;
3807c478bd9Sstevel@tonic-gate 			}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 			/* extract block sizes */
3837c478bd9Sstevel@tonic-gate 			block_sizes = (int *)malloc(sizeof (int));
3847c478bd9Sstevel@tonic-gate 			if (block_sizes == NULL) {
3857c478bd9Sstevel@tonic-gate 				free(key_sizes);
3867c478bd9Sstevel@tonic-gate 				goto bail;
3877c478bd9Sstevel@tonic-gate 			}
3887c478bd9Sstevel@tonic-gate 			num_sizes = 0;
3897c478bd9Sstevel@tonic-gate 			token = strtok_r(block_string, comma, &lasts);
3907c478bd9Sstevel@tonic-gate 			if (token == NULL) {
39161961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
3927c478bd9Sstevel@tonic-gate 				    "invalid block sizes for alg %d (proto %d)",
3937c478bd9Sstevel@tonic-gate 				    alg_num, new_proto->proto_num);
3947c478bd9Sstevel@tonic-gate 				free(key_sizes);
3957c478bd9Sstevel@tonic-gate 				goto bail;
3967c478bd9Sstevel@tonic-gate 			}
3977c478bd9Sstevel@tonic-gate 			*block_sizes = 0;
3987c478bd9Sstevel@tonic-gate 			do {
3997c478bd9Sstevel@tonic-gate 				int *nbk;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 				nbk = (int *)realloc(block_sizes,
4027c478bd9Sstevel@tonic-gate 				    sizeof (int) * ((++num_sizes) + 1));
4037c478bd9Sstevel@tonic-gate 				if (nbk == NULL) {
4047c478bd9Sstevel@tonic-gate 					free(key_sizes);
4057c478bd9Sstevel@tonic-gate 					free(block_sizes);
4067c478bd9Sstevel@tonic-gate 					goto bail;
4077c478bd9Sstevel@tonic-gate 				}
4087c478bd9Sstevel@tonic-gate 				block_sizes = nbk;
4097c478bd9Sstevel@tonic-gate 				/* Can't check for 0 here... */
4107c478bd9Sstevel@tonic-gate 				block_sizes[num_sizes - 1] = atoi(token);
4117c478bd9Sstevel@tonic-gate 				block_sizes[num_sizes] = 0;
4127c478bd9Sstevel@tonic-gate 			} while ((token = strtok_r(NULL, comma, &lasts)) !=
4137c478bd9Sstevel@tonic-gate 			    NULL);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 			/* Allocate a new struct ipsecalgent. */
4167c478bd9Sstevel@tonic-gate 			curalg = (struct ipsecalgent *)calloc(
4177c478bd9Sstevel@tonic-gate 			    sizeof (struct ipsecalgent), 1);
4187c478bd9Sstevel@tonic-gate 			if (curalg == NULL) {
4197c478bd9Sstevel@tonic-gate 				free(key_sizes);
4207c478bd9Sstevel@tonic-gate 				free(block_sizes);
4217c478bd9Sstevel@tonic-gate 				goto bail;
4227c478bd9Sstevel@tonic-gate 			}
4237c478bd9Sstevel@tonic-gate 			curalg->a_proto_num = new_num;
4247c478bd9Sstevel@tonic-gate 			curalg->a_alg_num = alg_num;
4257c478bd9Sstevel@tonic-gate 			curalg->a_block_sizes = block_sizes;
4267c478bd9Sstevel@tonic-gate 			curalg->a_key_sizes = key_sizes;
4277c478bd9Sstevel@tonic-gate 			curalg->a_key_increment = key_increment;
4287c478bd9Sstevel@tonic-gate 			if ((curalg->a_mech_name = strdup(ef_name)) == NULL) {
4297c478bd9Sstevel@tonic-gate 				freeipsecalgent(curalg);
4307c478bd9Sstevel@tonic-gate 				goto bail;
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 			/* Set names. */
4337c478bd9Sstevel@tonic-gate 			curalg->a_names = (char **)malloc(sizeof (char *));
4347c478bd9Sstevel@tonic-gate 			num_sizes = 0;	/* Recycle "sizes" */
4357c478bd9Sstevel@tonic-gate 			token = strtok_r(alg_names, comma, &lasts);
4367c478bd9Sstevel@tonic-gate 			if (curalg->a_names == NULL || token == NULL) {
4377c478bd9Sstevel@tonic-gate 				freeipsecalgent(curalg);
4387c478bd9Sstevel@tonic-gate 				goto bail;
4397c478bd9Sstevel@tonic-gate 			}
4407c478bd9Sstevel@tonic-gate 			do {
4417c478bd9Sstevel@tonic-gate 				char **nnames;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 				nnames = (char **)realloc(curalg->a_names,
4447c478bd9Sstevel@tonic-gate 				    sizeof (char *) * ((++num_sizes) + 1));
4457c478bd9Sstevel@tonic-gate 				if (nnames == NULL) {
4467c478bd9Sstevel@tonic-gate 					freeipsecalgent(curalg);
4477c478bd9Sstevel@tonic-gate 					goto bail;
4487c478bd9Sstevel@tonic-gate 				}
4497c478bd9Sstevel@tonic-gate 				curalg->a_names = nnames;
4507c478bd9Sstevel@tonic-gate 				curalg->a_names[num_sizes] = NULL;
4517c478bd9Sstevel@tonic-gate 				curalg->a_names[num_sizes - 1] =
4527c478bd9Sstevel@tonic-gate 				    strdup(token);
4537c478bd9Sstevel@tonic-gate 				if (curalg->a_names[num_sizes - 1] == NULL) {
4547c478bd9Sstevel@tonic-gate 					freeipsecalgent(curalg);
4557c478bd9Sstevel@tonic-gate 					goto bail;
4567c478bd9Sstevel@tonic-gate 				}
4577c478bd9Sstevel@tonic-gate 			} while ((token = strtok_r(NULL, comma, &lasts)) !=
4587c478bd9Sstevel@tonic-gate 			    NULL);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 			if (doing_pkg) {
4617c478bd9Sstevel@tonic-gate 				/* record alg as being part of current pkg */
4627c478bd9Sstevel@tonic-gate 				int npkgs = new_proto->proto_algs_npkgs;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 				new_proto->proto_algs_pkgs = realloc(
4657c478bd9Sstevel@tonic-gate 				    new_proto->proto_algs_pkgs,
4667c478bd9Sstevel@tonic-gate 				    (npkgs + 1) * sizeof (ipsecalgs_pkg_t));
4677c478bd9Sstevel@tonic-gate 				if (new_proto->proto_algs_pkgs == NULL)
4687c478bd9Sstevel@tonic-gate 					goto bail;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 				new_proto->proto_algs_pkgs[npkgs].alg_num =
4717c478bd9Sstevel@tonic-gate 					curalg->a_alg_num;
4727c478bd9Sstevel@tonic-gate 				new_proto->proto_algs_pkgs[npkgs].pkg_name =
4737c478bd9Sstevel@tonic-gate 					strdup(cur_pkg);
4747c478bd9Sstevel@tonic-gate 				if (new_proto->proto_algs_pkgs[npkgs].pkg_name
4757c478bd9Sstevel@tonic-gate 				    == NULL)
4767c478bd9Sstevel@tonic-gate 					goto bail;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 				new_proto->proto_algs_npkgs = npkgs + 1;
4797c478bd9Sstevel@tonic-gate 			}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 			/* add new alg to protocol */
4827c478bd9Sstevel@tonic-gate 			newalglist = realloc(new_proto->proto_algs,
4837c478bd9Sstevel@tonic-gate 			    (new_proto->proto_numalgs + 1) *
4847c478bd9Sstevel@tonic-gate 			    sizeof (struct ipsecalgent *));
4857c478bd9Sstevel@tonic-gate 			if (newalglist == NULL) {
4867c478bd9Sstevel@tonic-gate 				freeipsecalgent(curalg);
4877c478bd9Sstevel@tonic-gate 				goto bail;
4887c478bd9Sstevel@tonic-gate 			}
4897c478bd9Sstevel@tonic-gate 			newalglist[new_proto->proto_numalgs] = curalg;
4907c478bd9Sstevel@tonic-gate 			new_proto->proto_numalgs++;
4917c478bd9Sstevel@tonic-gate 			new_proto->proto_algs = newalglist;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		} else if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGSTART,
4947c478bd9Sstevel@tonic-gate 		    sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1) == 0) {
4957c478bd9Sstevel@tonic-gate 			/* start of package delimiter */
4967c478bd9Sstevel@tonic-gate 			if (doing_pkg) {
49761961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
4987c478bd9Sstevel@tonic-gate 				    "duplicate package start delimiters");
4997c478bd9Sstevel@tonic-gate 				goto bail;
5007c478bd9Sstevel@tonic-gate 			}
5017c478bd9Sstevel@tonic-gate 			(void) strncpy(cur_pkg, line +
5027c478bd9Sstevel@tonic-gate 			    (sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1),
5037c478bd9Sstevel@tonic-gate 			    sizeof (cur_pkg));
5047c478bd9Sstevel@tonic-gate 			/* remove trailing '\n' */
5057c478bd9Sstevel@tonic-gate 			cur_pkg[strlen(cur_pkg) - 1] = '\0';
5067c478bd9Sstevel@tonic-gate 			doing_pkg = B_TRUE;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		} else {
5097c478bd9Sstevel@tonic-gate 			/* end of package delimiter */
5107c478bd9Sstevel@tonic-gate 			char tmp_pkg[1024];
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 			if (!doing_pkg) {
51361961e0fSrobinson 				(void) snprintf(diag_buf, sizeof (diag_buf),
5147c478bd9Sstevel@tonic-gate 				    "end package delimiter without start");
5157c478bd9Sstevel@tonic-gate 				goto bail;
5167c478bd9Sstevel@tonic-gate 			}
5177c478bd9Sstevel@tonic-gate 			/*
5187c478bd9Sstevel@tonic-gate 			 * Get specified pkg name, fail if it doesn't match
5197c478bd9Sstevel@tonic-gate 			 * the package specified by the last # Begin.
5207c478bd9Sstevel@tonic-gate 			 */
5217c478bd9Sstevel@tonic-gate 			(void) strncpy(tmp_pkg, line +
5227c478bd9Sstevel@tonic-gate 			    (sizeof (LIBIPSEC_ALGS_LINE_PKGEND) - 1),
5237c478bd9Sstevel@tonic-gate 			    sizeof (tmp_pkg));
5247c478bd9Sstevel@tonic-gate 			/* remove trailing '\n' */
5257c478bd9Sstevel@tonic-gate 			tmp_pkg[strlen(tmp_pkg) - 1] = '\0';
5267c478bd9Sstevel@tonic-gate 			if (strncmp(cur_pkg, tmp_pkg, sizeof (cur_pkg)) != 0)
5277c478bd9Sstevel@tonic-gate 				goto bail;
5287c478bd9Sstevel@tonic-gate 			doing_pkg = B_FALSE;
5297c478bd9Sstevel@tonic-gate 		}
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	*num = rc_num;
5337c478bd9Sstevel@tonic-gate 	return (rc);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate bail:
5367c478bd9Sstevel@tonic-gate 	if (strlen(diag_buf) > 0) {
5377c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "possibly corrupt %s file: %s\n",
5387c478bd9Sstevel@tonic-gate 		    INET_IPSECALGSFILE, diag_buf);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 	_clean_trash(rc, rc_num);
5417c478bd9Sstevel@tonic-gate 	return (NULL);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate  * If alg_context is NULL, update the library's cached copy of
5467c478bd9Sstevel@tonic-gate  * INET_IPSECALGSFILE.  If alg_context is non-NULL, hang a
5477c478bd9Sstevel@tonic-gate  * library-internal representation of a cached copy.  The latter is useful
5487c478bd9Sstevel@tonic-gate  * for routines in libipsecutil that _write_ the contents out.
5497c478bd9Sstevel@tonic-gate  */
5507c478bd9Sstevel@tonic-gate void
5517c478bd9Sstevel@tonic-gate _build_internal_algs(ipsec_proto_t **alg_context, int *alg_nums)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate 	FILE *f = NULL;
5547c478bd9Sstevel@tonic-gate 	int fd, rc, trash_num;
5557c478bd9Sstevel@tonic-gate 	ipsec_proto_t *new_protos = NULL, *trash;
5567c478bd9Sstevel@tonic-gate 	time_t filetime;
5577c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	/*
5607c478bd9Sstevel@tonic-gate 	 * Construct new_protos from the file.
5617c478bd9Sstevel@tonic-gate 	 */
5627c478bd9Sstevel@tonic-gate 	if (alg_context == NULL) {
5637c478bd9Sstevel@tonic-gate 		/*
5647c478bd9Sstevel@tonic-gate 		 * Check the time w/o holding the lock.  This is just a
5657c478bd9Sstevel@tonic-gate 		 * cache reality check.  We'll do it again for real if this
5667c478bd9Sstevel@tonic-gate 		 * surface check fails.
5677c478bd9Sstevel@tonic-gate 		 */
5687c478bd9Sstevel@tonic-gate 		if (stat(INET_IPSECALGSFILE, &statbuf) == -1 ||
5697c478bd9Sstevel@tonic-gate 		    (statbuf.st_mtime < proto_last_update &&
5707c478bd9Sstevel@tonic-gate 			protos != NULL))
5717c478bd9Sstevel@tonic-gate 			return;
57261961e0fSrobinson 		(void) rw_wrlock(&proto_rw);
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	fd = open(INET_IPSECALGSFILE, O_RDONLY);
5767c478bd9Sstevel@tonic-gate 	if (fd != -1) {
5777c478bd9Sstevel@tonic-gate 		f = fdopen(fd, "r");
5787c478bd9Sstevel@tonic-gate 		if (f == NULL) {
57961961e0fSrobinson 			(void) close(fd);
5807c478bd9Sstevel@tonic-gate 		} else {
5817c478bd9Sstevel@tonic-gate 			rc = fstat(fd, &statbuf);
5827c478bd9Sstevel@tonic-gate 			if (rc != -1) {
5837c478bd9Sstevel@tonic-gate 				/*
5847c478bd9Sstevel@tonic-gate 				 * Update if the file is newer than our
5857c478bd9Sstevel@tonic-gate 				 * last cached copy.
5867c478bd9Sstevel@tonic-gate 				 */
5877c478bd9Sstevel@tonic-gate 				filetime = statbuf.st_mtime;
5887c478bd9Sstevel@tonic-gate 				if (alg_context != NULL ||
5897c478bd9Sstevel@tonic-gate 				    filetime > proto_last_update)
5907c478bd9Sstevel@tonic-gate 					new_protos = build_list(f, &rc);
5917c478bd9Sstevel@tonic-gate 			}
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if (alg_context == NULL) {
5967c478bd9Sstevel@tonic-gate 		/*
5977c478bd9Sstevel@tonic-gate 		 * If we have failed anywhere above, new_protoss will be NULL.
5987c478bd9Sstevel@tonic-gate 		 * This way, the previous cached protos will still be intact.
5997c478bd9Sstevel@tonic-gate 		 */
6007c478bd9Sstevel@tonic-gate 		if (new_protos != NULL) {
6017c478bd9Sstevel@tonic-gate 			proto_last_update = filetime;
6027c478bd9Sstevel@tonic-gate 			trash = protos;
6037c478bd9Sstevel@tonic-gate 			trash_num = num_protos;
6047c478bd9Sstevel@tonic-gate 			protos = new_protos;
6057c478bd9Sstevel@tonic-gate 			num_protos = rc;
6067c478bd9Sstevel@tonic-gate 		} else {
6077c478bd9Sstevel@tonic-gate 			/*
6087c478bd9Sstevel@tonic-gate 			 * Else the original protocols and algorithms lists
6097c478bd9Sstevel@tonic-gate 			 * remains the same.
6107c478bd9Sstevel@tonic-gate 			 */
6117c478bd9Sstevel@tonic-gate 			trash = NULL;
6127c478bd9Sstevel@tonic-gate 		}
61361961e0fSrobinson 		(void) rw_unlock(&proto_rw);
6147c478bd9Sstevel@tonic-gate 		_clean_trash(trash, trash_num);
6157c478bd9Sstevel@tonic-gate 	} else {
6167c478bd9Sstevel@tonic-gate 		/*
6177c478bd9Sstevel@tonic-gate 		 * Assume caller has done the appropriate locking,
6187c478bd9Sstevel@tonic-gate 		 * cleanup, etc.  And if new_protos is NULL, it's the caller's
6197c478bd9Sstevel@tonic-gate 		 * problem.
6207c478bd9Sstevel@tonic-gate 		 */
6217c478bd9Sstevel@tonic-gate 		*alg_context = new_protos;
6227c478bd9Sstevel@tonic-gate 		*alg_nums = rc;
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/* Since f is read-only, can avoid all of the failures... */
6267c478bd9Sstevel@tonic-gate 	if (f != NULL)
6277c478bd9Sstevel@tonic-gate 		(void) fclose(f);
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate  * Assume input is 0-terminated.
6327c478bd9Sstevel@tonic-gate  */
6337c478bd9Sstevel@tonic-gate static int *
6347c478bd9Sstevel@tonic-gate duplicate_intarr(int *orig)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	size_t allocsize = sizeof (int);
6377c478bd9Sstevel@tonic-gate 	int *iwalker = orig;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if (orig == NULL)
6407c478bd9Sstevel@tonic-gate 		return (NULL);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	while (*iwalker != 0) {
6437c478bd9Sstevel@tonic-gate 		allocsize += sizeof (int);
6447c478bd9Sstevel@tonic-gate 		iwalker++;
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	iwalker = malloc(allocsize);
6487c478bd9Sstevel@tonic-gate 	if (iwalker != NULL)
64961961e0fSrobinson 		(void) memcpy(iwalker, orig, allocsize);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	return (iwalker);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate  * Assume input is NULL terminated.
6567c478bd9Sstevel@tonic-gate  */
6577c478bd9Sstevel@tonic-gate static char **
6587c478bd9Sstevel@tonic-gate duplicate_strarr(char **orig)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	int i;
6617c478bd9Sstevel@tonic-gate 	char **swalker;
6627c478bd9Sstevel@tonic-gate 	char **newbie;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	if (orig == NULL)
6657c478bd9Sstevel@tonic-gate 		return (NULL);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	/* count number of elements in source array */
6687c478bd9Sstevel@tonic-gate 	for (swalker = orig; *swalker != NULL; swalker++);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/* use calloc() to get NULL-initialization */
6717c478bd9Sstevel@tonic-gate 	newbie = calloc(swalker - orig + 1, sizeof (char *));
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if (newbie != NULL) {
6747c478bd9Sstevel@tonic-gate 		/* do the copy */
6757c478bd9Sstevel@tonic-gate 		for (i = 0; orig[i] != NULL; i++) {
6767c478bd9Sstevel@tonic-gate 			newbie[i] = strdup(orig[i]);
6777c478bd9Sstevel@tonic-gate 			if (newbie[i] == NULL) {
6787c478bd9Sstevel@tonic-gate 				for (swalker = newbie; *swalker != NULL;
6797c478bd9Sstevel@tonic-gate 				    swalker++)
6807c478bd9Sstevel@tonic-gate 					free(*swalker);
6817c478bd9Sstevel@tonic-gate 				free(newbie);
6827c478bd9Sstevel@tonic-gate 				return (NULL);
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	return (newbie);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate struct ipsecalgent *
6917c478bd9Sstevel@tonic-gate _duplicate_alg(struct ipsecalgent *orig)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	struct ipsecalgent *rc;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	/* use calloc() to get NULL-initialization. */
6967c478bd9Sstevel@tonic-gate 	rc = calloc(1, sizeof (struct ipsecalgent));
6977c478bd9Sstevel@tonic-gate 	if (rc == NULL)
6987c478bd9Sstevel@tonic-gate 		return (NULL);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	rc->a_proto_num = orig->a_proto_num;
7017c478bd9Sstevel@tonic-gate 	rc->a_alg_num = orig->a_alg_num;
7027c478bd9Sstevel@tonic-gate 	rc->a_key_increment = orig->a_key_increment;
7037c478bd9Sstevel@tonic-gate 	rc->a_mech_name = strdup(orig->a_mech_name);
7047c478bd9Sstevel@tonic-gate 	rc->a_block_sizes = duplicate_intarr(orig->a_block_sizes);
7057c478bd9Sstevel@tonic-gate 	rc->a_key_sizes = duplicate_intarr(orig->a_key_sizes);
7067c478bd9Sstevel@tonic-gate 	rc->a_names = duplicate_strarr(orig->a_names);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (rc->a_mech_name == NULL || rc->a_block_sizes == NULL ||
7097c478bd9Sstevel@tonic-gate 	    rc->a_key_sizes == NULL || rc->a_names == NULL) {
7107c478bd9Sstevel@tonic-gate 		freeipsecalgent(rc);
7117c478bd9Sstevel@tonic-gate 		return (NULL);
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	return (rc);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate /*
7187c478bd9Sstevel@tonic-gate  * Assume the rwlock is held for reading.
7197c478bd9Sstevel@tonic-gate  */
7207c478bd9Sstevel@tonic-gate static ipsec_proto_t *
7217c478bd9Sstevel@tonic-gate findprotobynum(int proto_num)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	int i;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_protos; i++) {
7267c478bd9Sstevel@tonic-gate 		if (protos[i].proto_num == proto_num)
7277c478bd9Sstevel@tonic-gate 			return (protos + i);
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	return (NULL);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate static ipsec_proto_t *
7347c478bd9Sstevel@tonic-gate findprotobyname(const char *name)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	int i;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	if (name == NULL)
7397c478bd9Sstevel@tonic-gate 		return (NULL);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_protos; i++) {
7427c478bd9Sstevel@tonic-gate 		/* Can use strcasecmp because our proto_name is bounded. */
7437c478bd9Sstevel@tonic-gate 		if (strcasecmp(protos[i].proto_name, name) == 0)
7447c478bd9Sstevel@tonic-gate 			return (protos + i);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	return (NULL);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate int *
7517c478bd9Sstevel@tonic-gate _real_getipsecprotos(int *nentries)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate 	int *rc, i;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	if (nentries == NULL)
7567c478bd9Sstevel@tonic-gate 		return (NULL);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
7597c478bd9Sstevel@tonic-gate 
76061961e0fSrobinson 	(void) rw_rdlock(&proto_rw);
7617c478bd9Sstevel@tonic-gate 	*nentries = num_protos;
7627c478bd9Sstevel@tonic-gate 	/*
7637c478bd9Sstevel@tonic-gate 	 * Allocate 1 byte if there are no protocols so a non-NULL return
7647c478bd9Sstevel@tonic-gate 	 * happens.
7657c478bd9Sstevel@tonic-gate 	 */
7667c478bd9Sstevel@tonic-gate 	rc = malloc((num_protos == 0) ? 1 : num_protos * sizeof (int));
7677c478bd9Sstevel@tonic-gate 	if (rc != NULL) {
7687c478bd9Sstevel@tonic-gate 		for (i = 0; i < num_protos; i++)
7697c478bd9Sstevel@tonic-gate 			rc[i] = protos[i].proto_num;
7707c478bd9Sstevel@tonic-gate 	}
77161961e0fSrobinson 	(void) rw_unlock(&proto_rw);
7727c478bd9Sstevel@tonic-gate 	return (rc);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate int *
7767c478bd9Sstevel@tonic-gate _real_getipsecalgs(int *nentries, int proto_num)
7777c478bd9Sstevel@tonic-gate {
7787c478bd9Sstevel@tonic-gate 	int *rc = NULL, i;
7797c478bd9Sstevel@tonic-gate 	ipsec_proto_t *proto;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	if (nentries == NULL)
7827c478bd9Sstevel@tonic-gate 		return (NULL);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
7857c478bd9Sstevel@tonic-gate 
78661961e0fSrobinson 	(void) rw_rdlock(&proto_rw);
7877c478bd9Sstevel@tonic-gate 	proto = findprotobynum(proto_num);
7887c478bd9Sstevel@tonic-gate 	if (proto != NULL) {
7897c478bd9Sstevel@tonic-gate 		*nentries = proto->proto_numalgs;
7907c478bd9Sstevel@tonic-gate 		/*
7917c478bd9Sstevel@tonic-gate 		 * Allocate 1 byte if there are no algorithms so a non-NULL
7927c478bd9Sstevel@tonic-gate 		 * return happens.
7937c478bd9Sstevel@tonic-gate 		 */
7947c478bd9Sstevel@tonic-gate 		rc = malloc((proto->proto_numalgs == 0) ? 1 :
7957c478bd9Sstevel@tonic-gate 		    proto->proto_numalgs * sizeof (int));
7967c478bd9Sstevel@tonic-gate 		if (rc != NULL) {
7977c478bd9Sstevel@tonic-gate 			for (i = 0; i < proto->proto_numalgs; i++)
7987c478bd9Sstevel@tonic-gate 				rc[i] = proto->proto_algs[i]->a_alg_num;
7997c478bd9Sstevel@tonic-gate 		}
8007c478bd9Sstevel@tonic-gate 	}
80161961e0fSrobinson 	(void) rw_unlock(&proto_rw);
8027c478bd9Sstevel@tonic-gate 	return (rc);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate struct ipsecalgent *
8067c478bd9Sstevel@tonic-gate getipsecalgbyname(const char *name, int proto_num, int *errnop)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	ipsec_proto_t *proto;
8097c478bd9Sstevel@tonic-gate 	struct ipsecalgent *rc = NULL;
8107c478bd9Sstevel@tonic-gate 	int i, my_errno = ENOENT;
8117c478bd9Sstevel@tonic-gate 	char **name_check;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
8147c478bd9Sstevel@tonic-gate 	if (name == NULL) {
8157c478bd9Sstevel@tonic-gate 		my_errno = EFAULT;
8167c478bd9Sstevel@tonic-gate 		goto bail;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
81961961e0fSrobinson 	(void) rw_rdlock(&proto_rw);
8207c478bd9Sstevel@tonic-gate 	proto = findprotobynum(proto_num);
8217c478bd9Sstevel@tonic-gate 	if (proto != NULL) {
8227c478bd9Sstevel@tonic-gate 		for (i = 0; i < proto->proto_numalgs; i++) {
8237c478bd9Sstevel@tonic-gate 			for (name_check = proto->proto_algs[i]->a_names;
8247c478bd9Sstevel@tonic-gate 			    *name_check != NULL; name_check++) {
8257c478bd9Sstevel@tonic-gate 				/*
8267c478bd9Sstevel@tonic-gate 				 * Can use strcasecmp because our name_check
8277c478bd9Sstevel@tonic-gate 				 * is bounded.
8287c478bd9Sstevel@tonic-gate 				 */
8297c478bd9Sstevel@tonic-gate 				if (strcasecmp(*name_check, name) == 0) {
8307c478bd9Sstevel@tonic-gate 					/* found match */
8317c478bd9Sstevel@tonic-gate 					rc = _duplicate_alg(
8327c478bd9Sstevel@tonic-gate 					    proto->proto_algs[i]);
8337c478bd9Sstevel@tonic-gate 					my_errno = (rc == NULL) ? ENOMEM : 0;
83461961e0fSrobinson 					(void) rw_unlock(&proto_rw);
8357c478bd9Sstevel@tonic-gate 					goto bail;
8367c478bd9Sstevel@tonic-gate 				}
8377c478bd9Sstevel@tonic-gate 			}
8387c478bd9Sstevel@tonic-gate 		}
8397c478bd9Sstevel@tonic-gate 	} else {
8407c478bd9Sstevel@tonic-gate 		my_errno = EINVAL;
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
84361961e0fSrobinson 	(void) rw_unlock(&proto_rw);
8447c478bd9Sstevel@tonic-gate bail:
8457c478bd9Sstevel@tonic-gate 	if (errnop != NULL)
8467c478bd9Sstevel@tonic-gate 		*errnop = my_errno;
8477c478bd9Sstevel@tonic-gate 	return (rc);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate struct ipsecalgent *
8517c478bd9Sstevel@tonic-gate getipsecalgbynum(int alg_num, int proto_num, int *errnop)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	ipsec_proto_t *proto;
8547c478bd9Sstevel@tonic-gate 	struct ipsecalgent *rc = NULL;
8557c478bd9Sstevel@tonic-gate 	int i, my_errno = ENOENT;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
8587c478bd9Sstevel@tonic-gate 
85961961e0fSrobinson 	(void) rw_rdlock(&proto_rw);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	proto = findprotobynum(proto_num);
8627c478bd9Sstevel@tonic-gate 	if (proto != NULL) {
8637c478bd9Sstevel@tonic-gate 		for (i = 0; i < proto->proto_numalgs; i++) {
8647c478bd9Sstevel@tonic-gate 			if (proto->proto_algs[i]->a_alg_num == alg_num) {
8657c478bd9Sstevel@tonic-gate 				rc = _duplicate_alg(proto->proto_algs[i]);
8667c478bd9Sstevel@tonic-gate 				my_errno = (rc == NULL) ? ENOMEM : 0;
8677c478bd9Sstevel@tonic-gate 				break;
8687c478bd9Sstevel@tonic-gate 			}
8697c478bd9Sstevel@tonic-gate 		}
8707c478bd9Sstevel@tonic-gate 	} else {
8717c478bd9Sstevel@tonic-gate 		my_errno = EINVAL;
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 
87461961e0fSrobinson 	(void) rw_unlock(&proto_rw);
8757c478bd9Sstevel@tonic-gate 	if (errnop != NULL)
8767c478bd9Sstevel@tonic-gate 		*errnop = my_errno;
8777c478bd9Sstevel@tonic-gate 	return (rc);
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate int
8817c478bd9Sstevel@tonic-gate getipsecprotobyname(const char *proto_name)
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	int rc = -1;
8847c478bd9Sstevel@tonic-gate 	ipsec_proto_t *proto;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
8877c478bd9Sstevel@tonic-gate 
88861961e0fSrobinson 	(void) rw_rdlock(&proto_rw);
8897c478bd9Sstevel@tonic-gate 	proto = findprotobyname(proto_name);
8907c478bd9Sstevel@tonic-gate 	if (proto != NULL)
8917c478bd9Sstevel@tonic-gate 		rc = proto->proto_num;
89261961e0fSrobinson 	(void) rw_unlock(&proto_rw);
8937c478bd9Sstevel@tonic-gate 	return (rc);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate char *
8977c478bd9Sstevel@tonic-gate getipsecprotobynum(int proto_num)
8987c478bd9Sstevel@tonic-gate {
8997c478bd9Sstevel@tonic-gate 	ipsec_proto_t *proto;
9007c478bd9Sstevel@tonic-gate 	char *rc = NULL;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	_build_internal_algs(NULL, NULL);
9037c478bd9Sstevel@tonic-gate 
90461961e0fSrobinson 	(void) rw_rdlock(&proto_rw);
9057c478bd9Sstevel@tonic-gate 	proto = findprotobynum(proto_num);
9067c478bd9Sstevel@tonic-gate 	if (proto != NULL)
9077c478bd9Sstevel@tonic-gate 		rc = strdup(proto->proto_name);
9087c478bd9Sstevel@tonic-gate 
90961961e0fSrobinson 	(void) rw_unlock(&proto_rw);
9107c478bd9Sstevel@tonic-gate 	return (rc);
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate void
9147c478bd9Sstevel@tonic-gate freeipsecalgent(struct ipsecalgent *ptr)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	char **walker;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
9197c478bd9Sstevel@tonic-gate 		return;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	if (ptr->a_names != NULL) {
9227c478bd9Sstevel@tonic-gate 		for (walker = ptr->a_names; *walker != NULL; walker++)
9237c478bd9Sstevel@tonic-gate 			free(*walker);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	/*
9277c478bd9Sstevel@tonic-gate 	 * Remember folks, free(NULL) works.
9287c478bd9Sstevel@tonic-gate 	 */
9297c478bd9Sstevel@tonic-gate 	free(ptr->a_names);
9307c478bd9Sstevel@tonic-gate 	free(ptr->a_mech_name);
9317c478bd9Sstevel@tonic-gate 	free(ptr->a_block_sizes);
9327c478bd9Sstevel@tonic-gate 	free(ptr->a_key_sizes);
9337c478bd9Sstevel@tonic-gate 	free(ptr);
9347c478bd9Sstevel@tonic-gate }
935