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 */ 22*61961e0fSrobinson 237c478bd9Sstevel@tonic-gate /* 24*61961e0fSrobinson * Copyright 2005 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 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); 1427c478bd9Sstevel@tonic-gate if (token == NULL || key_sizes == NULL) 1437c478bd9Sstevel@tonic-gate return (-1); 1447c478bd9Sstevel@tonic-gate *key_sizes = 0; 1457c478bd9Sstevel@tonic-gate do { 1467c478bd9Sstevel@tonic-gate int *nks; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate nks = (int *)realloc(key_sizes, 1497c478bd9Sstevel@tonic-gate sizeof (int) * ((++num_sizes) + 1)); 1507c478bd9Sstevel@tonic-gate if (nks == NULL) { 1517c478bd9Sstevel@tonic-gate free(key_sizes); 1527c478bd9Sstevel@tonic-gate return (-1); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate key_sizes = nks; 1557c478bd9Sstevel@tonic-gate /* Can't check for atoi() == 0 here... */ 1567c478bd9Sstevel@tonic-gate key_sizes[num_sizes - 1] = atoi(token); 1577c478bd9Sstevel@tonic-gate key_sizes[num_sizes] = 0; 1587c478bd9Sstevel@tonic-gate } while ((token = strtok_r(NULL, comma, &lasts)) != NULL); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate *sizep = key_sizes; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate return (key_increment); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * Find the execution mode corresponding to the given string. 1687c478bd9Sstevel@tonic-gate * Returns 0 on success, -1 on failure. 1697c478bd9Sstevel@tonic-gate */ 1707c478bd9Sstevel@tonic-gate int 1717c478bd9Sstevel@tonic-gate _str_to_ipsec_exec_mode(char *str, ipsecalgs_exec_mode_t *exec_mode) 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate if (strcmp(str, "sync") == 0) { 1747c478bd9Sstevel@tonic-gate *exec_mode = LIBIPSEC_ALGS_EXEC_SYNC; 1757c478bd9Sstevel@tonic-gate return (0); 1767c478bd9Sstevel@tonic-gate } else if (strcmp(str, "async") == 0) { 1777c478bd9Sstevel@tonic-gate *exec_mode = LIBIPSEC_ALGS_EXEC_ASYNC; 1787c478bd9Sstevel@tonic-gate return (0); 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate return (-1); 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate /* 1857c478bd9Sstevel@tonic-gate * Given a file pointer, read all the text from the file and convert it into 1867c478bd9Sstevel@tonic-gate * a bunch of ipsec_proto_t's, each with an array of struct ipsecalgent 1877c478bd9Sstevel@tonic-gate * pointers - one for each algorithm. 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate static ipsec_proto_t * 1907c478bd9Sstevel@tonic-gate build_list(FILE *f, int *num) 1917c478bd9Sstevel@tonic-gate { 1927c478bd9Sstevel@tonic-gate char line[1024]; 1937c478bd9Sstevel@tonic-gate char *token, *lasts, *alg_names, *ef_name, *key_string, *block_string; 1947c478bd9Sstevel@tonic-gate char *proto_name; 1957c478bd9Sstevel@tonic-gate ipsec_proto_t *rc = NULL, *new_proto = NULL; 1967c478bd9Sstevel@tonic-gate int *block_sizes, *key_sizes; 1977c478bd9Sstevel@tonic-gate int rc_num = 0, key_increment; 1987c478bd9Sstevel@tonic-gate int new_num, alg_num, num_sizes; 1997c478bd9Sstevel@tonic-gate struct ipsecalgent *curalg, **newalglist; 2007c478bd9Sstevel@tonic-gate char cur_pkg[1024]; 2017c478bd9Sstevel@tonic-gate boolean_t doing_pkg = B_FALSE; 2027c478bd9Sstevel@tonic-gate ipsecalgs_exec_mode_t exec_mode; 2037c478bd9Sstevel@tonic-gate char diag_buf[128]; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate diag_buf[0] = '\0'; 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), f) != NULL) { 2087c478bd9Sstevel@tonic-gate if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PROTO, 2097c478bd9Sstevel@tonic-gate sizeof (LIBIPSEC_ALGS_LINE_PROTO) - 1) != 0 && 2107c478bd9Sstevel@tonic-gate strncasecmp(line, LIBIPSEC_ALGS_LINE_ALG, 2117c478bd9Sstevel@tonic-gate sizeof (LIBIPSEC_ALGS_LINE_ALG) - 1) != 0 && 2127c478bd9Sstevel@tonic-gate strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGSTART, 2137c478bd9Sstevel@tonic-gate sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1) != 0 && 2147c478bd9Sstevel@tonic-gate strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGEND, 2157c478bd9Sstevel@tonic-gate sizeof (LIBIPSEC_ALGS_LINE_PKGEND) - 1) != 0) { 2167c478bd9Sstevel@tonic-gate if ((token = strtok_r(line, " \t\n", &lasts)) == NULL || 2177c478bd9Sstevel@tonic-gate token[0] == '#') { 2187c478bd9Sstevel@tonic-gate continue; 2197c478bd9Sstevel@tonic-gate } else { 220*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 2217c478bd9Sstevel@tonic-gate "non-recognized start of line"); 2227c478bd9Sstevel@tonic-gate goto bail; 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PROTO, 2277c478bd9Sstevel@tonic-gate sizeof (LIBIPSEC_ALGS_LINE_PROTO) - 1) == 0) { 2287c478bd9Sstevel@tonic-gate /* current line defines a new protocol */ 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate /* skip the protocol token */ 2317c478bd9Sstevel@tonic-gate token = strtok_r(line, pipechar, &lasts); 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate /* protocol number */ 2347c478bd9Sstevel@tonic-gate token = strtok_r(NULL, pipechar, &lasts); 2357c478bd9Sstevel@tonic-gate if (token == NULL || (new_num = atoi(token)) == 0) { 236*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 2377c478bd9Sstevel@tonic-gate "invalid protocol number"); 2387c478bd9Sstevel@tonic-gate goto bail; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* protocol name */ 2427c478bd9Sstevel@tonic-gate token = strtok_r(NULL, pipechar, &lasts); 2437c478bd9Sstevel@tonic-gate if (token == NULL) { 244*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 2457c478bd9Sstevel@tonic-gate "cannot read protocol name"); 2467c478bd9Sstevel@tonic-gate goto bail; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate proto_name = token; 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate /* execution mode */ 2517c478bd9Sstevel@tonic-gate token = strtok_r(NULL, pipechar, &lasts); 2527c478bd9Sstevel@tonic-gate if (token == NULL) { 253*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 2547c478bd9Sstevel@tonic-gate "cannot read execution mode"); 2557c478bd9Sstevel@tonic-gate goto bail; 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate /* remove trailing '\n' */ 2587c478bd9Sstevel@tonic-gate token[strlen(token) - 1] = '\0'; 2597c478bd9Sstevel@tonic-gate if (_str_to_ipsec_exec_mode(token, &exec_mode) != 0) { 260*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 2617c478bd9Sstevel@tonic-gate "invalid execution mode: \"%s\"", token); 2627c478bd9Sstevel@tonic-gate goto bail; 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate /* initialize protocol structure */ 2667c478bd9Sstevel@tonic-gate rc_num++; 2677c478bd9Sstevel@tonic-gate new_proto = (ipsec_proto_t *)realloc(rc, 2687c478bd9Sstevel@tonic-gate sizeof (ipsec_proto_t) * rc_num); 2697c478bd9Sstevel@tonic-gate rc = new_proto; 2707c478bd9Sstevel@tonic-gate if (new_proto == NULL) 2717c478bd9Sstevel@tonic-gate goto bail; 2727c478bd9Sstevel@tonic-gate new_proto += (rc_num - 1); 2737c478bd9Sstevel@tonic-gate new_proto->proto_num = new_num; 2747c478bd9Sstevel@tonic-gate new_proto->proto_algs = NULL; 2757c478bd9Sstevel@tonic-gate new_proto->proto_numalgs = 0; 2767c478bd9Sstevel@tonic-gate new_proto->proto_name = strdup(proto_name); 2777c478bd9Sstevel@tonic-gate if (new_proto->proto_name == NULL) 2787c478bd9Sstevel@tonic-gate goto bail; 2797c478bd9Sstevel@tonic-gate new_proto->proto_exec_mode = exec_mode; 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate if (doing_pkg) { 2827c478bd9Sstevel@tonic-gate /* record proto as being part of current pkg */ 2837c478bd9Sstevel@tonic-gate new_proto->proto_pkg = strdup(cur_pkg); 2847c478bd9Sstevel@tonic-gate if (new_proto->proto_pkg == NULL) 2857c478bd9Sstevel@tonic-gate goto bail; 2867c478bd9Sstevel@tonic-gate } else { 2877c478bd9Sstevel@tonic-gate new_proto->proto_pkg = NULL; 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate new_proto->proto_algs_pkgs = NULL; 2917c478bd9Sstevel@tonic-gate new_proto->proto_algs_npkgs = 0; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate } else if (strncasecmp(line, LIBIPSEC_ALGS_LINE_ALG, 2947c478bd9Sstevel@tonic-gate sizeof (LIBIPSEC_ALGS_LINE_ALG) - 1) == 0) { 2957c478bd9Sstevel@tonic-gate /* current line defines a new algorithm */ 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate /* skip the algorithm token */ 2987c478bd9Sstevel@tonic-gate token = strtok_r(line, pipechar, &lasts); 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate /* protocol number */ 3017c478bd9Sstevel@tonic-gate token = strtok_r(NULL, pipechar, &lasts); 3027c478bd9Sstevel@tonic-gate if (token == NULL || (new_num = atoi(token)) == 0) { 303*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 3047c478bd9Sstevel@tonic-gate "invalid algorithm number"); 3057c478bd9Sstevel@tonic-gate goto bail; 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate /* We can be O(N) for now. There aren't that many. */ 3097c478bd9Sstevel@tonic-gate for (new_proto = rc; new_proto < (rc + new_num); 3107c478bd9Sstevel@tonic-gate new_proto++) 3117c478bd9Sstevel@tonic-gate if (new_proto->proto_num == new_num) 3127c478bd9Sstevel@tonic-gate break; 3137c478bd9Sstevel@tonic-gate if (new_proto == (rc + new_num)) { 314*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 3157c478bd9Sstevel@tonic-gate "invalid protocol number %d for algorithm", 3167c478bd9Sstevel@tonic-gate new_num); 3177c478bd9Sstevel@tonic-gate goto bail; 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate /* algorithm number */ 3217c478bd9Sstevel@tonic-gate token = strtok_r(NULL, pipechar, &lasts); 3227c478bd9Sstevel@tonic-gate if (token == NULL) { 323*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 3247c478bd9Sstevel@tonic-gate "cannot read algorithm number"); 3257c478bd9Sstevel@tonic-gate goto bail; 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate /* Can't check for 0 here. */ 3287c478bd9Sstevel@tonic-gate alg_num = atoi(token); 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate /* algorithm names */ 3317c478bd9Sstevel@tonic-gate token = strtok_r(NULL, pipechar, &lasts); 3327c478bd9Sstevel@tonic-gate if (token == NULL) { 333*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 3347c478bd9Sstevel@tonic-gate "cannot read algorithm number"); 3357c478bd9Sstevel@tonic-gate goto bail; 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate alg_names = token; 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate /* mechanism name */ 3407c478bd9Sstevel@tonic-gate token = strtok_r(NULL, pipechar, &lasts); 3417c478bd9Sstevel@tonic-gate if (token == NULL) { 342*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 3437c478bd9Sstevel@tonic-gate "cannot read mechanism name for alg %d " 3447c478bd9Sstevel@tonic-gate "(proto %d)", alg_num, 3457c478bd9Sstevel@tonic-gate new_proto->proto_num); 3467c478bd9Sstevel@tonic-gate goto bail; 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate ef_name = token; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* key sizes */ 3517c478bd9Sstevel@tonic-gate token = strtok_r(NULL, pipechar, &lasts); 3527c478bd9Sstevel@tonic-gate if (token == NULL) { 353*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 3547c478bd9Sstevel@tonic-gate "cannot read key sizes for alg %d " 3557c478bd9Sstevel@tonic-gate "(proto %d)", alg_num, 3567c478bd9Sstevel@tonic-gate new_proto->proto_num); 3577c478bd9Sstevel@tonic-gate goto bail; 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate key_string = token; 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate /* block sizes */ 3627c478bd9Sstevel@tonic-gate token = strtok_r(NULL, pipechar, &lasts); 3637c478bd9Sstevel@tonic-gate if (token == NULL) { 364*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 3657c478bd9Sstevel@tonic-gate "cannot read mechanism name for alg %d " 3667c478bd9Sstevel@tonic-gate "(proto %d)", alg_num, 3677c478bd9Sstevel@tonic-gate new_proto->proto_num); 3687c478bd9Sstevel@tonic-gate goto bail; 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate block_string = token; 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate /* extract key sizes */ 3737c478bd9Sstevel@tonic-gate key_increment = build_keysizes(&key_sizes, key_string); 3747c478bd9Sstevel@tonic-gate if (key_increment == -1) { 375*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 3767c478bd9Sstevel@tonic-gate "invalid key sizes for alg %d (proto %d)", 3777c478bd9Sstevel@tonic-gate alg_num, new_proto->proto_num); 3787c478bd9Sstevel@tonic-gate goto bail; 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate /* extract block sizes */ 3827c478bd9Sstevel@tonic-gate block_sizes = (int *)malloc(sizeof (int)); 3837c478bd9Sstevel@tonic-gate if (block_sizes == NULL) { 3847c478bd9Sstevel@tonic-gate free(key_sizes); 3857c478bd9Sstevel@tonic-gate goto bail; 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate num_sizes = 0; 3887c478bd9Sstevel@tonic-gate token = strtok_r(block_string, comma, &lasts); 3897c478bd9Sstevel@tonic-gate if (token == NULL) { 390*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 3917c478bd9Sstevel@tonic-gate "invalid block sizes for alg %d (proto %d)", 3927c478bd9Sstevel@tonic-gate alg_num, new_proto->proto_num); 3937c478bd9Sstevel@tonic-gate free(key_sizes); 3947c478bd9Sstevel@tonic-gate goto bail; 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate *block_sizes = 0; 3977c478bd9Sstevel@tonic-gate do { 3987c478bd9Sstevel@tonic-gate int *nbk; 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate nbk = (int *)realloc(block_sizes, 4017c478bd9Sstevel@tonic-gate sizeof (int) * ((++num_sizes) + 1)); 4027c478bd9Sstevel@tonic-gate if (nbk == NULL) { 4037c478bd9Sstevel@tonic-gate free(key_sizes); 4047c478bd9Sstevel@tonic-gate free(block_sizes); 4057c478bd9Sstevel@tonic-gate goto bail; 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate block_sizes = nbk; 4087c478bd9Sstevel@tonic-gate /* Can't check for 0 here... */ 4097c478bd9Sstevel@tonic-gate block_sizes[num_sizes - 1] = atoi(token); 4107c478bd9Sstevel@tonic-gate block_sizes[num_sizes] = 0; 4117c478bd9Sstevel@tonic-gate } while ((token = strtok_r(NULL, comma, &lasts)) != 4127c478bd9Sstevel@tonic-gate NULL); 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate /* Allocate a new struct ipsecalgent. */ 4157c478bd9Sstevel@tonic-gate curalg = (struct ipsecalgent *)calloc( 4167c478bd9Sstevel@tonic-gate sizeof (struct ipsecalgent), 1); 4177c478bd9Sstevel@tonic-gate if (curalg == NULL) { 4187c478bd9Sstevel@tonic-gate free(key_sizes); 4197c478bd9Sstevel@tonic-gate free(block_sizes); 4207c478bd9Sstevel@tonic-gate goto bail; 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate curalg->a_proto_num = new_num; 4237c478bd9Sstevel@tonic-gate curalg->a_alg_num = alg_num; 4247c478bd9Sstevel@tonic-gate curalg->a_block_sizes = block_sizes; 4257c478bd9Sstevel@tonic-gate curalg->a_key_sizes = key_sizes; 4267c478bd9Sstevel@tonic-gate curalg->a_key_increment = key_increment; 4277c478bd9Sstevel@tonic-gate if ((curalg->a_mech_name = strdup(ef_name)) == NULL) { 4287c478bd9Sstevel@tonic-gate freeipsecalgent(curalg); 4297c478bd9Sstevel@tonic-gate goto bail; 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate /* Set names. */ 4327c478bd9Sstevel@tonic-gate curalg->a_names = (char **)malloc(sizeof (char *)); 4337c478bd9Sstevel@tonic-gate num_sizes = 0; /* Recycle "sizes" */ 4347c478bd9Sstevel@tonic-gate token = strtok_r(alg_names, comma, &lasts); 4357c478bd9Sstevel@tonic-gate if (curalg->a_names == NULL || token == NULL) { 4367c478bd9Sstevel@tonic-gate freeipsecalgent(curalg); 4377c478bd9Sstevel@tonic-gate goto bail; 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate do { 4407c478bd9Sstevel@tonic-gate char **nnames; 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate nnames = (char **)realloc(curalg->a_names, 4437c478bd9Sstevel@tonic-gate sizeof (char *) * ((++num_sizes) + 1)); 4447c478bd9Sstevel@tonic-gate if (nnames == NULL) { 4457c478bd9Sstevel@tonic-gate freeipsecalgent(curalg); 4467c478bd9Sstevel@tonic-gate goto bail; 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate curalg->a_names = nnames; 4497c478bd9Sstevel@tonic-gate curalg->a_names[num_sizes] = NULL; 4507c478bd9Sstevel@tonic-gate curalg->a_names[num_sizes - 1] = 4517c478bd9Sstevel@tonic-gate strdup(token); 4527c478bd9Sstevel@tonic-gate if (curalg->a_names[num_sizes - 1] == NULL) { 4537c478bd9Sstevel@tonic-gate freeipsecalgent(curalg); 4547c478bd9Sstevel@tonic-gate goto bail; 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate } while ((token = strtok_r(NULL, comma, &lasts)) != 4577c478bd9Sstevel@tonic-gate NULL); 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (doing_pkg) { 4607c478bd9Sstevel@tonic-gate /* record alg as being part of current pkg */ 4617c478bd9Sstevel@tonic-gate int npkgs = new_proto->proto_algs_npkgs; 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate new_proto->proto_algs_pkgs = realloc( 4647c478bd9Sstevel@tonic-gate new_proto->proto_algs_pkgs, 4657c478bd9Sstevel@tonic-gate (npkgs + 1) * sizeof (ipsecalgs_pkg_t)); 4667c478bd9Sstevel@tonic-gate if (new_proto->proto_algs_pkgs == NULL) 4677c478bd9Sstevel@tonic-gate goto bail; 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate new_proto->proto_algs_pkgs[npkgs].alg_num = 4707c478bd9Sstevel@tonic-gate curalg->a_alg_num; 4717c478bd9Sstevel@tonic-gate new_proto->proto_algs_pkgs[npkgs].pkg_name = 4727c478bd9Sstevel@tonic-gate strdup(cur_pkg); 4737c478bd9Sstevel@tonic-gate if (new_proto->proto_algs_pkgs[npkgs].pkg_name 4747c478bd9Sstevel@tonic-gate == NULL) 4757c478bd9Sstevel@tonic-gate goto bail; 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate new_proto->proto_algs_npkgs = npkgs + 1; 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate /* add new alg to protocol */ 4817c478bd9Sstevel@tonic-gate newalglist = realloc(new_proto->proto_algs, 4827c478bd9Sstevel@tonic-gate (new_proto->proto_numalgs + 1) * 4837c478bd9Sstevel@tonic-gate sizeof (struct ipsecalgent *)); 4847c478bd9Sstevel@tonic-gate if (newalglist == NULL) { 4857c478bd9Sstevel@tonic-gate freeipsecalgent(curalg); 4867c478bd9Sstevel@tonic-gate goto bail; 4877c478bd9Sstevel@tonic-gate } 4887c478bd9Sstevel@tonic-gate newalglist[new_proto->proto_numalgs] = curalg; 4897c478bd9Sstevel@tonic-gate new_proto->proto_numalgs++; 4907c478bd9Sstevel@tonic-gate new_proto->proto_algs = newalglist; 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate } else if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGSTART, 4937c478bd9Sstevel@tonic-gate sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1) == 0) { 4947c478bd9Sstevel@tonic-gate /* start of package delimiter */ 4957c478bd9Sstevel@tonic-gate if (doing_pkg) { 496*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 4977c478bd9Sstevel@tonic-gate "duplicate package start delimiters"); 4987c478bd9Sstevel@tonic-gate goto bail; 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate (void) strncpy(cur_pkg, line + 5017c478bd9Sstevel@tonic-gate (sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1), 5027c478bd9Sstevel@tonic-gate sizeof (cur_pkg)); 5037c478bd9Sstevel@tonic-gate /* remove trailing '\n' */ 5047c478bd9Sstevel@tonic-gate cur_pkg[strlen(cur_pkg) - 1] = '\0'; 5057c478bd9Sstevel@tonic-gate doing_pkg = B_TRUE; 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate } else { 5087c478bd9Sstevel@tonic-gate /* end of package delimiter */ 5097c478bd9Sstevel@tonic-gate char tmp_pkg[1024]; 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate if (!doing_pkg) { 512*61961e0fSrobinson (void) snprintf(diag_buf, sizeof (diag_buf), 5137c478bd9Sstevel@tonic-gate "end package delimiter without start"); 5147c478bd9Sstevel@tonic-gate goto bail; 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate /* 5177c478bd9Sstevel@tonic-gate * Get specified pkg name, fail if it doesn't match 5187c478bd9Sstevel@tonic-gate * the package specified by the last # Begin. 5197c478bd9Sstevel@tonic-gate */ 5207c478bd9Sstevel@tonic-gate (void) strncpy(tmp_pkg, line + 5217c478bd9Sstevel@tonic-gate (sizeof (LIBIPSEC_ALGS_LINE_PKGEND) - 1), 5227c478bd9Sstevel@tonic-gate sizeof (tmp_pkg)); 5237c478bd9Sstevel@tonic-gate /* remove trailing '\n' */ 5247c478bd9Sstevel@tonic-gate tmp_pkg[strlen(tmp_pkg) - 1] = '\0'; 5257c478bd9Sstevel@tonic-gate if (strncmp(cur_pkg, tmp_pkg, sizeof (cur_pkg)) != 0) 5267c478bd9Sstevel@tonic-gate goto bail; 5277c478bd9Sstevel@tonic-gate doing_pkg = B_FALSE; 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate *num = rc_num; 5327c478bd9Sstevel@tonic-gate return (rc); 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate bail: 5357c478bd9Sstevel@tonic-gate if (strlen(diag_buf) > 0) { 5367c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "possibly corrupt %s file: %s\n", 5377c478bd9Sstevel@tonic-gate INET_IPSECALGSFILE, diag_buf); 5387c478bd9Sstevel@tonic-gate } 5397c478bd9Sstevel@tonic-gate _clean_trash(rc, rc_num); 5407c478bd9Sstevel@tonic-gate return (NULL); 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate /* 5447c478bd9Sstevel@tonic-gate * If alg_context is NULL, update the library's cached copy of 5457c478bd9Sstevel@tonic-gate * INET_IPSECALGSFILE. If alg_context is non-NULL, hang a 5467c478bd9Sstevel@tonic-gate * library-internal representation of a cached copy. The latter is useful 5477c478bd9Sstevel@tonic-gate * for routines in libipsecutil that _write_ the contents out. 5487c478bd9Sstevel@tonic-gate */ 5497c478bd9Sstevel@tonic-gate void 5507c478bd9Sstevel@tonic-gate _build_internal_algs(ipsec_proto_t **alg_context, int *alg_nums) 5517c478bd9Sstevel@tonic-gate { 5527c478bd9Sstevel@tonic-gate FILE *f = NULL; 5537c478bd9Sstevel@tonic-gate int fd, rc, trash_num; 5547c478bd9Sstevel@tonic-gate ipsec_proto_t *new_protos = NULL, *trash; 5557c478bd9Sstevel@tonic-gate time_t filetime; 5567c478bd9Sstevel@tonic-gate struct stat statbuf; 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate /* 5597c478bd9Sstevel@tonic-gate * Construct new_protos from the file. 5607c478bd9Sstevel@tonic-gate */ 5617c478bd9Sstevel@tonic-gate if (alg_context == NULL) { 5627c478bd9Sstevel@tonic-gate /* 5637c478bd9Sstevel@tonic-gate * Check the time w/o holding the lock. This is just a 5647c478bd9Sstevel@tonic-gate * cache reality check. We'll do it again for real if this 5657c478bd9Sstevel@tonic-gate * surface check fails. 5667c478bd9Sstevel@tonic-gate */ 5677c478bd9Sstevel@tonic-gate if (stat(INET_IPSECALGSFILE, &statbuf) == -1 || 5687c478bd9Sstevel@tonic-gate (statbuf.st_mtime < proto_last_update && 5697c478bd9Sstevel@tonic-gate protos != NULL)) 5707c478bd9Sstevel@tonic-gate return; 571*61961e0fSrobinson (void) rw_wrlock(&proto_rw); 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate fd = open(INET_IPSECALGSFILE, O_RDONLY); 5757c478bd9Sstevel@tonic-gate if (fd != -1) { 5767c478bd9Sstevel@tonic-gate f = fdopen(fd, "r"); 5777c478bd9Sstevel@tonic-gate if (f == NULL) { 578*61961e0fSrobinson (void) close(fd); 5797c478bd9Sstevel@tonic-gate } else { 5807c478bd9Sstevel@tonic-gate rc = fstat(fd, &statbuf); 5817c478bd9Sstevel@tonic-gate if (rc != -1) { 5827c478bd9Sstevel@tonic-gate /* 5837c478bd9Sstevel@tonic-gate * Update if the file is newer than our 5847c478bd9Sstevel@tonic-gate * last cached copy. 5857c478bd9Sstevel@tonic-gate */ 5867c478bd9Sstevel@tonic-gate filetime = statbuf.st_mtime; 5877c478bd9Sstevel@tonic-gate if (alg_context != NULL || 5887c478bd9Sstevel@tonic-gate filetime > proto_last_update) 5897c478bd9Sstevel@tonic-gate new_protos = build_list(f, &rc); 5907c478bd9Sstevel@tonic-gate } 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate if (alg_context == NULL) { 5957c478bd9Sstevel@tonic-gate /* 5967c478bd9Sstevel@tonic-gate * If we have failed anywhere above, new_protoss will be NULL. 5977c478bd9Sstevel@tonic-gate * This way, the previous cached protos will still be intact. 5987c478bd9Sstevel@tonic-gate */ 5997c478bd9Sstevel@tonic-gate if (new_protos != NULL) { 6007c478bd9Sstevel@tonic-gate proto_last_update = filetime; 6017c478bd9Sstevel@tonic-gate trash = protos; 6027c478bd9Sstevel@tonic-gate trash_num = num_protos; 6037c478bd9Sstevel@tonic-gate protos = new_protos; 6047c478bd9Sstevel@tonic-gate num_protos = rc; 6057c478bd9Sstevel@tonic-gate } else { 6067c478bd9Sstevel@tonic-gate /* 6077c478bd9Sstevel@tonic-gate * Else the original protocols and algorithms lists 6087c478bd9Sstevel@tonic-gate * remains the same. 6097c478bd9Sstevel@tonic-gate */ 6107c478bd9Sstevel@tonic-gate trash = NULL; 6117c478bd9Sstevel@tonic-gate } 612*61961e0fSrobinson (void) rw_unlock(&proto_rw); 6137c478bd9Sstevel@tonic-gate _clean_trash(trash, trash_num); 6147c478bd9Sstevel@tonic-gate } else { 6157c478bd9Sstevel@tonic-gate /* 6167c478bd9Sstevel@tonic-gate * Assume caller has done the appropriate locking, 6177c478bd9Sstevel@tonic-gate * cleanup, etc. And if new_protos is NULL, it's the caller's 6187c478bd9Sstevel@tonic-gate * problem. 6197c478bd9Sstevel@tonic-gate */ 6207c478bd9Sstevel@tonic-gate *alg_context = new_protos; 6217c478bd9Sstevel@tonic-gate *alg_nums = rc; 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate /* Since f is read-only, can avoid all of the failures... */ 6257c478bd9Sstevel@tonic-gate if (f != NULL) 6267c478bd9Sstevel@tonic-gate (void) fclose(f); 6277c478bd9Sstevel@tonic-gate } 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate /* 6307c478bd9Sstevel@tonic-gate * Assume input is 0-terminated. 6317c478bd9Sstevel@tonic-gate */ 6327c478bd9Sstevel@tonic-gate static int * 6337c478bd9Sstevel@tonic-gate duplicate_intarr(int *orig) 6347c478bd9Sstevel@tonic-gate { 6357c478bd9Sstevel@tonic-gate size_t allocsize = sizeof (int); 6367c478bd9Sstevel@tonic-gate int *iwalker = orig; 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate if (orig == NULL) 6397c478bd9Sstevel@tonic-gate return (NULL); 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate while (*iwalker != 0) { 6427c478bd9Sstevel@tonic-gate allocsize += sizeof (int); 6437c478bd9Sstevel@tonic-gate iwalker++; 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate iwalker = malloc(allocsize); 6477c478bd9Sstevel@tonic-gate if (iwalker != NULL) 648*61961e0fSrobinson (void) memcpy(iwalker, orig, allocsize); 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate return (iwalker); 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate 6537c478bd9Sstevel@tonic-gate /* 6547c478bd9Sstevel@tonic-gate * Assume input is NULL terminated. 6557c478bd9Sstevel@tonic-gate */ 6567c478bd9Sstevel@tonic-gate static char ** 6577c478bd9Sstevel@tonic-gate duplicate_strarr(char **orig) 6587c478bd9Sstevel@tonic-gate { 6597c478bd9Sstevel@tonic-gate int i; 6607c478bd9Sstevel@tonic-gate char **swalker; 6617c478bd9Sstevel@tonic-gate char **newbie; 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate if (orig == NULL) 6647c478bd9Sstevel@tonic-gate return (NULL); 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate /* count number of elements in source array */ 6677c478bd9Sstevel@tonic-gate for (swalker = orig; *swalker != NULL; swalker++); 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate /* use calloc() to get NULL-initialization */ 6707c478bd9Sstevel@tonic-gate newbie = calloc(swalker - orig + 1, sizeof (char *)); 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate if (newbie != NULL) { 6737c478bd9Sstevel@tonic-gate /* do the copy */ 6747c478bd9Sstevel@tonic-gate for (i = 0; orig[i] != NULL; i++) { 6757c478bd9Sstevel@tonic-gate newbie[i] = strdup(orig[i]); 6767c478bd9Sstevel@tonic-gate if (newbie[i] == NULL) { 6777c478bd9Sstevel@tonic-gate for (swalker = newbie; *swalker != NULL; 6787c478bd9Sstevel@tonic-gate swalker++) 6797c478bd9Sstevel@tonic-gate free(*swalker); 6807c478bd9Sstevel@tonic-gate free(newbie); 6817c478bd9Sstevel@tonic-gate return (NULL); 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate return (newbie); 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate struct ipsecalgent * 6907c478bd9Sstevel@tonic-gate _duplicate_alg(struct ipsecalgent *orig) 6917c478bd9Sstevel@tonic-gate { 6927c478bd9Sstevel@tonic-gate struct ipsecalgent *rc; 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate /* use calloc() to get NULL-initialization. */ 6957c478bd9Sstevel@tonic-gate rc = calloc(1, sizeof (struct ipsecalgent)); 6967c478bd9Sstevel@tonic-gate if (rc == NULL) 6977c478bd9Sstevel@tonic-gate return (NULL); 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate rc->a_proto_num = orig->a_proto_num; 7007c478bd9Sstevel@tonic-gate rc->a_alg_num = orig->a_alg_num; 7017c478bd9Sstevel@tonic-gate rc->a_key_increment = orig->a_key_increment; 7027c478bd9Sstevel@tonic-gate rc->a_mech_name = strdup(orig->a_mech_name); 7037c478bd9Sstevel@tonic-gate rc->a_block_sizes = duplicate_intarr(orig->a_block_sizes); 7047c478bd9Sstevel@tonic-gate rc->a_key_sizes = duplicate_intarr(orig->a_key_sizes); 7057c478bd9Sstevel@tonic-gate rc->a_names = duplicate_strarr(orig->a_names); 7067c478bd9Sstevel@tonic-gate 7077c478bd9Sstevel@tonic-gate if (rc->a_mech_name == NULL || rc->a_block_sizes == NULL || 7087c478bd9Sstevel@tonic-gate rc->a_key_sizes == NULL || rc->a_names == NULL) { 7097c478bd9Sstevel@tonic-gate freeipsecalgent(rc); 7107c478bd9Sstevel@tonic-gate return (NULL); 7117c478bd9Sstevel@tonic-gate } 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate return (rc); 7147c478bd9Sstevel@tonic-gate } 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate /* 7177c478bd9Sstevel@tonic-gate * Assume the rwlock is held for reading. 7187c478bd9Sstevel@tonic-gate */ 7197c478bd9Sstevel@tonic-gate static ipsec_proto_t * 7207c478bd9Sstevel@tonic-gate findprotobynum(int proto_num) 7217c478bd9Sstevel@tonic-gate { 7227c478bd9Sstevel@tonic-gate int i; 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate for (i = 0; i < num_protos; i++) { 7257c478bd9Sstevel@tonic-gate if (protos[i].proto_num == proto_num) 7267c478bd9Sstevel@tonic-gate return (protos + i); 7277c478bd9Sstevel@tonic-gate } 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate return (NULL); 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate static ipsec_proto_t * 7337c478bd9Sstevel@tonic-gate findprotobyname(const char *name) 7347c478bd9Sstevel@tonic-gate { 7357c478bd9Sstevel@tonic-gate int i; 7367c478bd9Sstevel@tonic-gate 7377c478bd9Sstevel@tonic-gate if (name == NULL) 7387c478bd9Sstevel@tonic-gate return (NULL); 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate for (i = 0; i < num_protos; i++) { 7417c478bd9Sstevel@tonic-gate /* Can use strcasecmp because our proto_name is bounded. */ 7427c478bd9Sstevel@tonic-gate if (strcasecmp(protos[i].proto_name, name) == 0) 7437c478bd9Sstevel@tonic-gate return (protos + i); 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate return (NULL); 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate int * 7507c478bd9Sstevel@tonic-gate _real_getipsecprotos(int *nentries) 7517c478bd9Sstevel@tonic-gate { 7527c478bd9Sstevel@tonic-gate int *rc, i; 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate if (nentries == NULL) 7557c478bd9Sstevel@tonic-gate return (NULL); 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate _build_internal_algs(NULL, NULL); 7587c478bd9Sstevel@tonic-gate 759*61961e0fSrobinson (void) rw_rdlock(&proto_rw); 7607c478bd9Sstevel@tonic-gate *nentries = num_protos; 7617c478bd9Sstevel@tonic-gate /* 7627c478bd9Sstevel@tonic-gate * Allocate 1 byte if there are no protocols so a non-NULL return 7637c478bd9Sstevel@tonic-gate * happens. 7647c478bd9Sstevel@tonic-gate */ 7657c478bd9Sstevel@tonic-gate rc = malloc((num_protos == 0) ? 1 : num_protos * sizeof (int)); 7667c478bd9Sstevel@tonic-gate if (rc != NULL) { 7677c478bd9Sstevel@tonic-gate for (i = 0; i < num_protos; i++) 7687c478bd9Sstevel@tonic-gate rc[i] = protos[i].proto_num; 7697c478bd9Sstevel@tonic-gate } 770*61961e0fSrobinson (void) rw_unlock(&proto_rw); 7717c478bd9Sstevel@tonic-gate return (rc); 7727c478bd9Sstevel@tonic-gate } 7737c478bd9Sstevel@tonic-gate 7747c478bd9Sstevel@tonic-gate int * 7757c478bd9Sstevel@tonic-gate _real_getipsecalgs(int *nentries, int proto_num) 7767c478bd9Sstevel@tonic-gate { 7777c478bd9Sstevel@tonic-gate int *rc = NULL, i; 7787c478bd9Sstevel@tonic-gate ipsec_proto_t *proto; 7797c478bd9Sstevel@tonic-gate 7807c478bd9Sstevel@tonic-gate if (nentries == NULL) 7817c478bd9Sstevel@tonic-gate return (NULL); 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate _build_internal_algs(NULL, NULL); 7847c478bd9Sstevel@tonic-gate 785*61961e0fSrobinson (void) rw_rdlock(&proto_rw); 7867c478bd9Sstevel@tonic-gate proto = findprotobynum(proto_num); 7877c478bd9Sstevel@tonic-gate if (proto != NULL) { 7887c478bd9Sstevel@tonic-gate *nentries = proto->proto_numalgs; 7897c478bd9Sstevel@tonic-gate /* 7907c478bd9Sstevel@tonic-gate * Allocate 1 byte if there are no algorithms so a non-NULL 7917c478bd9Sstevel@tonic-gate * return happens. 7927c478bd9Sstevel@tonic-gate */ 7937c478bd9Sstevel@tonic-gate rc = malloc((proto->proto_numalgs == 0) ? 1 : 7947c478bd9Sstevel@tonic-gate proto->proto_numalgs * sizeof (int)); 7957c478bd9Sstevel@tonic-gate if (rc != NULL) { 7967c478bd9Sstevel@tonic-gate for (i = 0; i < proto->proto_numalgs; i++) 7977c478bd9Sstevel@tonic-gate rc[i] = proto->proto_algs[i]->a_alg_num; 7987c478bd9Sstevel@tonic-gate } 7997c478bd9Sstevel@tonic-gate } 800*61961e0fSrobinson (void) rw_unlock(&proto_rw); 8017c478bd9Sstevel@tonic-gate return (rc); 8027c478bd9Sstevel@tonic-gate } 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate struct ipsecalgent * 8057c478bd9Sstevel@tonic-gate getipsecalgbyname(const char *name, int proto_num, int *errnop) 8067c478bd9Sstevel@tonic-gate { 8077c478bd9Sstevel@tonic-gate ipsec_proto_t *proto; 8087c478bd9Sstevel@tonic-gate struct ipsecalgent *rc = NULL; 8097c478bd9Sstevel@tonic-gate int i, my_errno = ENOENT; 8107c478bd9Sstevel@tonic-gate char **name_check; 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate _build_internal_algs(NULL, NULL); 8137c478bd9Sstevel@tonic-gate if (name == NULL) { 8147c478bd9Sstevel@tonic-gate my_errno = EFAULT; 8157c478bd9Sstevel@tonic-gate goto bail; 8167c478bd9Sstevel@tonic-gate } 8177c478bd9Sstevel@tonic-gate 818*61961e0fSrobinson (void) rw_rdlock(&proto_rw); 8197c478bd9Sstevel@tonic-gate proto = findprotobynum(proto_num); 8207c478bd9Sstevel@tonic-gate if (proto != NULL) { 8217c478bd9Sstevel@tonic-gate for (i = 0; i < proto->proto_numalgs; i++) { 8227c478bd9Sstevel@tonic-gate for (name_check = proto->proto_algs[i]->a_names; 8237c478bd9Sstevel@tonic-gate *name_check != NULL; name_check++) { 8247c478bd9Sstevel@tonic-gate /* 8257c478bd9Sstevel@tonic-gate * Can use strcasecmp because our name_check 8267c478bd9Sstevel@tonic-gate * is bounded. 8277c478bd9Sstevel@tonic-gate */ 8287c478bd9Sstevel@tonic-gate if (strcasecmp(*name_check, name) == 0) { 8297c478bd9Sstevel@tonic-gate /* found match */ 8307c478bd9Sstevel@tonic-gate rc = _duplicate_alg( 8317c478bd9Sstevel@tonic-gate proto->proto_algs[i]); 8327c478bd9Sstevel@tonic-gate my_errno = (rc == NULL) ? ENOMEM : 0; 833*61961e0fSrobinson (void) rw_unlock(&proto_rw); 8347c478bd9Sstevel@tonic-gate goto bail; 8357c478bd9Sstevel@tonic-gate } 8367c478bd9Sstevel@tonic-gate } 8377c478bd9Sstevel@tonic-gate } 8387c478bd9Sstevel@tonic-gate } else { 8397c478bd9Sstevel@tonic-gate my_errno = EINVAL; 8407c478bd9Sstevel@tonic-gate } 8417c478bd9Sstevel@tonic-gate 842*61961e0fSrobinson (void) rw_unlock(&proto_rw); 8437c478bd9Sstevel@tonic-gate bail: 8447c478bd9Sstevel@tonic-gate if (errnop != NULL) 8457c478bd9Sstevel@tonic-gate *errnop = my_errno; 8467c478bd9Sstevel@tonic-gate return (rc); 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate struct ipsecalgent * 8507c478bd9Sstevel@tonic-gate getipsecalgbynum(int alg_num, int proto_num, int *errnop) 8517c478bd9Sstevel@tonic-gate { 8527c478bd9Sstevel@tonic-gate ipsec_proto_t *proto; 8537c478bd9Sstevel@tonic-gate struct ipsecalgent *rc = NULL; 8547c478bd9Sstevel@tonic-gate int i, my_errno = ENOENT; 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate _build_internal_algs(NULL, NULL); 8577c478bd9Sstevel@tonic-gate 858*61961e0fSrobinson (void) rw_rdlock(&proto_rw); 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate proto = findprotobynum(proto_num); 8617c478bd9Sstevel@tonic-gate if (proto != NULL) { 8627c478bd9Sstevel@tonic-gate for (i = 0; i < proto->proto_numalgs; i++) { 8637c478bd9Sstevel@tonic-gate if (proto->proto_algs[i]->a_alg_num == alg_num) { 8647c478bd9Sstevel@tonic-gate rc = _duplicate_alg(proto->proto_algs[i]); 8657c478bd9Sstevel@tonic-gate my_errno = (rc == NULL) ? ENOMEM : 0; 8667c478bd9Sstevel@tonic-gate break; 8677c478bd9Sstevel@tonic-gate } 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate } else { 8707c478bd9Sstevel@tonic-gate my_errno = EINVAL; 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate 873*61961e0fSrobinson (void) rw_unlock(&proto_rw); 8747c478bd9Sstevel@tonic-gate if (errnop != NULL) 8757c478bd9Sstevel@tonic-gate *errnop = my_errno; 8767c478bd9Sstevel@tonic-gate return (rc); 8777c478bd9Sstevel@tonic-gate } 8787c478bd9Sstevel@tonic-gate 8797c478bd9Sstevel@tonic-gate int 8807c478bd9Sstevel@tonic-gate getipsecprotobyname(const char *proto_name) 8817c478bd9Sstevel@tonic-gate { 8827c478bd9Sstevel@tonic-gate int rc = -1; 8837c478bd9Sstevel@tonic-gate ipsec_proto_t *proto; 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate _build_internal_algs(NULL, NULL); 8867c478bd9Sstevel@tonic-gate 887*61961e0fSrobinson (void) rw_rdlock(&proto_rw); 8887c478bd9Sstevel@tonic-gate proto = findprotobyname(proto_name); 8897c478bd9Sstevel@tonic-gate if (proto != NULL) 8907c478bd9Sstevel@tonic-gate rc = proto->proto_num; 891*61961e0fSrobinson (void) rw_unlock(&proto_rw); 8927c478bd9Sstevel@tonic-gate return (rc); 8937c478bd9Sstevel@tonic-gate } 8947c478bd9Sstevel@tonic-gate 8957c478bd9Sstevel@tonic-gate char * 8967c478bd9Sstevel@tonic-gate getipsecprotobynum(int proto_num) 8977c478bd9Sstevel@tonic-gate { 8987c478bd9Sstevel@tonic-gate ipsec_proto_t *proto; 8997c478bd9Sstevel@tonic-gate char *rc = NULL; 9007c478bd9Sstevel@tonic-gate 9017c478bd9Sstevel@tonic-gate _build_internal_algs(NULL, NULL); 9027c478bd9Sstevel@tonic-gate 903*61961e0fSrobinson (void) rw_rdlock(&proto_rw); 9047c478bd9Sstevel@tonic-gate proto = findprotobynum(proto_num); 9057c478bd9Sstevel@tonic-gate if (proto != NULL) 9067c478bd9Sstevel@tonic-gate rc = strdup(proto->proto_name); 9077c478bd9Sstevel@tonic-gate 908*61961e0fSrobinson (void) rw_unlock(&proto_rw); 9097c478bd9Sstevel@tonic-gate return (rc); 9107c478bd9Sstevel@tonic-gate } 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate void 9137c478bd9Sstevel@tonic-gate freeipsecalgent(struct ipsecalgent *ptr) 9147c478bd9Sstevel@tonic-gate { 9157c478bd9Sstevel@tonic-gate char **walker; 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate if (ptr == NULL) 9187c478bd9Sstevel@tonic-gate return; 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate if (ptr->a_names != NULL) { 9217c478bd9Sstevel@tonic-gate for (walker = ptr->a_names; *walker != NULL; walker++) 9227c478bd9Sstevel@tonic-gate free(*walker); 9237c478bd9Sstevel@tonic-gate } 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate /* 9267c478bd9Sstevel@tonic-gate * Remember folks, free(NULL) works. 9277c478bd9Sstevel@tonic-gate */ 9287c478bd9Sstevel@tonic-gate free(ptr->a_names); 9297c478bd9Sstevel@tonic-gate free(ptr->a_mech_name); 9307c478bd9Sstevel@tonic-gate free(ptr->a_block_sizes); 9317c478bd9Sstevel@tonic-gate free(ptr->a_key_sizes); 9327c478bd9Sstevel@tonic-gate free(ptr); 9337c478bd9Sstevel@tonic-gate } 934