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