145916cd2Sjpk /* 245916cd2Sjpk * CDDL HEADER START 345916cd2Sjpk * 445916cd2Sjpk * The contents of this file are subject to the terms of the 545916cd2Sjpk * Common Development and Distribution License (the "License"). 645916cd2Sjpk * You may not use this file except in compliance with the License. 745916cd2Sjpk * 845916cd2Sjpk * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 945916cd2Sjpk * or http://www.opensolaris.org/os/licensing. 1045916cd2Sjpk * See the License for the specific language governing permissions 1145916cd2Sjpk * and limitations under the License. 1245916cd2Sjpk * 1345916cd2Sjpk * When distributing Covered Code, include this CDDL HEADER in each 1445916cd2Sjpk * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1545916cd2Sjpk * If applicable, add the following below this CDDL HEADER, with the 1645916cd2Sjpk * fields enclosed by brackets "[]" replaced with your own identifying 1745916cd2Sjpk * information: Portions Copyright [yyyy] [name of copyright owner] 1845916cd2Sjpk * 1945916cd2Sjpk * CDDL HEADER END 2045916cd2Sjpk */ 2145916cd2Sjpk /* 2245916cd2Sjpk * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 2345916cd2Sjpk * Use is subject to license terms. 2445916cd2Sjpk */ 2545916cd2Sjpk 2645916cd2Sjpk #pragma ident "%Z%%M% %I% %E% SMI" 2745916cd2Sjpk 2845916cd2Sjpk #include "files_common.h" 2945916cd2Sjpk #include <string.h> 3045916cd2Sjpk #include <libtsnet.h> 31*cb5caa98Sdjl #include <netinet/in.h> 3245916cd2Sjpk 3345916cd2Sjpk /* 3445916cd2Sjpk * files/tsol_getrhent.c -- 3545916cd2Sjpk * "files" backend for nsswitch "tnrhdb" database 3645916cd2Sjpk */ 3745916cd2Sjpk static int 38*cb5caa98Sdjl check_addr(nss_XbyY_args_t *args, const char *line, int linelen) 3945916cd2Sjpk { 40*cb5caa98Sdjl const char *limit, *linep, *keyp; 41*cb5caa98Sdjl char prev; 42*cb5caa98Sdjl int ipv6; 4345916cd2Sjpk 44*cb5caa98Sdjl linep = line; 45*cb5caa98Sdjl limit = line + linelen; 46*cb5caa98Sdjl keyp = args->key.hostaddr.addr; 47*cb5caa98Sdjl prev = '\0'; 48*cb5caa98Sdjl 49*cb5caa98Sdjl if (strstr(linep, "\\:") != NULL) 50*cb5caa98Sdjl ipv6 = 1; 51*cb5caa98Sdjl else 52*cb5caa98Sdjl ipv6 = 0; 53*cb5caa98Sdjl 54*cb5caa98Sdjl /* 55*cb5caa98Sdjl * compare addr in 56*cb5caa98Sdjl * 57*cb5caa98Sdjl * 192.168.120.6:public 58*cb5caa98Sdjl * fec0\:\:a00\:20ff\:fea0\:21f7:cipso 59*cb5caa98Sdjl * 60*cb5caa98Sdjl * ':' is the seperator. 61*cb5caa98Sdjl */ 62*cb5caa98Sdjl 63*cb5caa98Sdjl while (*keyp && linep < limit && *keyp == *linep) { 64*cb5caa98Sdjl if ((ipv6 == 0 && *linep == ':') || 65*cb5caa98Sdjl (ipv6 == 1 && prev != '\\' && *linep == ':')) 66*cb5caa98Sdjl break; 67*cb5caa98Sdjl 68*cb5caa98Sdjl prev = *linep; 69*cb5caa98Sdjl keyp++; 70*cb5caa98Sdjl linep++; 71*cb5caa98Sdjl } 72*cb5caa98Sdjl if (*keyp == '\0' && linep < limit && ((ipv6 == 0 && *linep == ':') || 73*cb5caa98Sdjl (ipv6 == 1 && prev != '\\' && *linep == ':'))) 7445916cd2Sjpk return (1); 7545916cd2Sjpk 7645916cd2Sjpk return (0); 7745916cd2Sjpk } 7845916cd2Sjpk 79*cb5caa98Sdjl static void 80*cb5caa98Sdjl escape_colon(const char *in, char *out) { 81*cb5caa98Sdjl int i, j; 82*cb5caa98Sdjl for (i = 0, j = 0; in[i] != '\0'; i++) { 83*cb5caa98Sdjl if (in[i] == ':') { 84*cb5caa98Sdjl out[j++] = '\\'; 85*cb5caa98Sdjl out[j++] = in[i]; 86*cb5caa98Sdjl } else 87*cb5caa98Sdjl out[j++] = in[i]; 88*cb5caa98Sdjl } 89*cb5caa98Sdjl out[j] = '\0'; 90*cb5caa98Sdjl } 91*cb5caa98Sdjl 9245916cd2Sjpk static nss_status_t 9345916cd2Sjpk getbyaddr(files_backend_ptr_t be, void *a) 9445916cd2Sjpk { 9545916cd2Sjpk nss_XbyY_args_t *argp = a; 96*cb5caa98Sdjl char addr6[INET6_ADDRSTRLEN + 5]; /* 5 '\' for ':' */ 97*cb5caa98Sdjl const char *addr = NULL; 98*cb5caa98Sdjl nss_status_t rc; 9945916cd2Sjpk 100*cb5caa98Sdjl if (argp->key.hostaddr.addr == NULL || 101*cb5caa98Sdjl (argp->key.hostaddr.type != AF_INET && 102*cb5caa98Sdjl argp->key.hostaddr.type != AF_INET6)) 103*cb5caa98Sdjl return (NSS_NOTFOUND); 104*cb5caa98Sdjl if (strchr(argp->key.hostaddr.addr, ':') != NULL) { 105*cb5caa98Sdjl /* IPV6 */ 106*cb5caa98Sdjl if (argp->key.hostaddr.type == AF_INET) 107*cb5caa98Sdjl return (NSS_NOTFOUND); 108*cb5caa98Sdjl escape_colon(argp->key.hostaddr.addr, addr6); 109*cb5caa98Sdjl /* save the key in original format */ 110*cb5caa98Sdjl addr = argp->key.hostaddr.addr; 111*cb5caa98Sdjl /* Replace the key with escaped format */ 112*cb5caa98Sdjl argp->key.hostaddr.addr = addr6; 113*cb5caa98Sdjl } else { 114*cb5caa98Sdjl /* IPV4 */ 115*cb5caa98Sdjl if (argp->key.hostaddr.type == AF_INET6) 116*cb5caa98Sdjl return (NSS_NOTFOUND); 117*cb5caa98Sdjl } 118*cb5caa98Sdjl 119*cb5caa98Sdjl rc = _nss_files_XY_all(be, argp, 1, 120*cb5caa98Sdjl argp->key.hostaddr.addr, check_addr); 121*cb5caa98Sdjl 122*cb5caa98Sdjl /* restore argp->key.hostaddr.addr */ 123*cb5caa98Sdjl if (addr) 124*cb5caa98Sdjl argp->key.hostaddr.addr = addr; 125*cb5caa98Sdjl 126*cb5caa98Sdjl return (rc); 12745916cd2Sjpk } 12845916cd2Sjpk 12945916cd2Sjpk static files_backend_op_t tsol_rh_ops[] = { 13045916cd2Sjpk _nss_files_destr, 13145916cd2Sjpk _nss_files_endent, 13245916cd2Sjpk _nss_files_setent, 13345916cd2Sjpk _nss_files_getent_netdb, 13445916cd2Sjpk getbyaddr 13545916cd2Sjpk }; 13645916cd2Sjpk 13745916cd2Sjpk nss_backend_t * 138*cb5caa98Sdjl /* LINTED E_FUNC_ARG_UNUSED */ 13945916cd2Sjpk _nss_files_tnrhdb_constr(const char *dummy1, const char *dummy2, 140*cb5caa98Sdjl /* LINTED E_FUNC_ARG_UNUSED */ 14145916cd2Sjpk const char *dummy3) 14245916cd2Sjpk { 14345916cd2Sjpk return (_nss_files_constr(tsol_rh_ops, 14445916cd2Sjpk sizeof (tsol_rh_ops) / sizeof (tsol_rh_ops[0]), TNRHDB_PATH, 14545916cd2Sjpk NSS_LINELEN_TSOL_RH, NULL)); 14645916cd2Sjpk } 147