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 /* 22*909c1a33Ston * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 2345916cd2Sjpk * Use is subject to license terms. 2445916cd2Sjpk * 2545916cd2Sjpk * From "tsol_getrhent.c 7.6 00/09/22 SMI; TSOL 2.x" 2645916cd2Sjpk */ 2745916cd2Sjpk 2845916cd2Sjpk #pragma ident "%Z%%M% %I% %E% SMI" 2945916cd2Sjpk 3045916cd2Sjpk #include <stdio.h> 3145916cd2Sjpk #include <nss_dbdefs.h> 3245916cd2Sjpk #include <libtsnet.h> 3345916cd2Sjpk #include <sys/types.h> 3445916cd2Sjpk #include <sys/socket.h> 3545916cd2Sjpk #include <netinet/in.h> 3645916cd2Sjpk #include <arpa/inet.h> 3745916cd2Sjpk #include <string.h> 3845916cd2Sjpk #include <secdb.h> 3945916cd2Sjpk #include <nss.h> 4045916cd2Sjpk #include <libtsnet.h> 4145916cd2Sjpk #include <libintl.h> 4245916cd2Sjpk 4345916cd2Sjpk extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *); /* from lib.c */ 4445916cd2Sjpk 4545916cd2Sjpk static int tsol_rh_stayopen; /* Unsynchronized, but it affects only */ 4645916cd2Sjpk /* efficiency, not correctness */ 4745916cd2Sjpk static DEFINE_NSS_DB_ROOT(db_root); 4845916cd2Sjpk static DEFINE_NSS_GETENT(context); 4945916cd2Sjpk 5045916cd2Sjpk static void 5145916cd2Sjpk _nss_initf_tsol_rh(nss_db_params_t *p) 5245916cd2Sjpk { 5345916cd2Sjpk p->name = NSS_DBNAM_TSOL_RH; 5445916cd2Sjpk p->default_config = NSS_DEFCONF_TSOL_RH; 5545916cd2Sjpk } 5645916cd2Sjpk 5745916cd2Sjpk tsol_rhent_t * 5845916cd2Sjpk tsol_getrhbyaddr(const void *addrp, size_t len, int af) 5945916cd2Sjpk { 6045916cd2Sjpk int err = 0; 6145916cd2Sjpk char *errstr = NULL; 6245916cd2Sjpk char buf[NSS_BUFLEN_TSOL_RH]; 6345916cd2Sjpk tsol_rhstr_t result; 6445916cd2Sjpk tsol_rhstr_t *rhstrp = NULL; 6545916cd2Sjpk nss_XbyY_args_t arg; 6645916cd2Sjpk 6745916cd2Sjpk NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr); 6845916cd2Sjpk 6945916cd2Sjpk arg.key.hostaddr.addr = (const char *)addrp; 7045916cd2Sjpk arg.key.hostaddr.len = len; 7145916cd2Sjpk arg.key.hostaddr.type = af; 7245916cd2Sjpk arg.stayopen = tsol_rh_stayopen; 7345916cd2Sjpk arg.h_errno = TSOL_NOT_FOUND; 7445916cd2Sjpk arg.status = nss_search(&db_root, _nss_initf_tsol_rh, 7545916cd2Sjpk NSS_DBOP_TSOL_RH_BYADDR, &arg); 7645916cd2Sjpk rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg); 7745916cd2Sjpk 7845916cd2Sjpk #ifdef DEBUG 7945916cd2Sjpk (void) fprintf(stdout, "tsol_getrhbyaddr %s: %s\n", 8045916cd2Sjpk (char *)addrp, rhstrp ? rhstrp->template : "NULL"); 8145916cd2Sjpk #endif /* DEBUG */ 8245916cd2Sjpk 8345916cd2Sjpk if (rhstrp == NULL) 8445916cd2Sjpk return (NULL); 8545916cd2Sjpk 8645916cd2Sjpk return (rhstr_to_ent(rhstrp, &err, &errstr)); 8745916cd2Sjpk } 8845916cd2Sjpk 8945916cd2Sjpk void 9045916cd2Sjpk tsol_setrhent(int stay) 9145916cd2Sjpk { 9245916cd2Sjpk tsol_rh_stayopen |= stay; 9345916cd2Sjpk nss_setent(&db_root, _nss_initf_tsol_rh, &context); 9445916cd2Sjpk } 9545916cd2Sjpk 9645916cd2Sjpk void 9745916cd2Sjpk tsol_endrhent(void) 9845916cd2Sjpk { 9945916cd2Sjpk tsol_rh_stayopen = 0; 10045916cd2Sjpk nss_endent(&db_root, _nss_initf_tsol_rh, &context); 10145916cd2Sjpk nss_delete(&db_root); 10245916cd2Sjpk } 10345916cd2Sjpk 10445916cd2Sjpk tsol_rhent_t * 10545916cd2Sjpk tsol_getrhent(void) 10645916cd2Sjpk { 10745916cd2Sjpk int err = 0; 10845916cd2Sjpk char *errstr = NULL; 10945916cd2Sjpk char buf[NSS_BUFLEN_TSOL_RH]; 11045916cd2Sjpk tsol_rhstr_t result; 11145916cd2Sjpk tsol_rhstr_t *rhstrp = NULL; 11245916cd2Sjpk nss_XbyY_args_t arg; 11345916cd2Sjpk 11445916cd2Sjpk NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr); 11545916cd2Sjpk /* No key, no stayopen */ 11645916cd2Sjpk arg.status = nss_getent(&db_root, _nss_initf_tsol_rh, &context, &arg); 11745916cd2Sjpk rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg); 11845916cd2Sjpk 11945916cd2Sjpk #ifdef DEBUG 12045916cd2Sjpk (void) fprintf(stdout, "tsol_getrhent: %s\n", 12145916cd2Sjpk rhstrp ? rhstrp->template : "NULL"); 12245916cd2Sjpk #endif /* DEBUG */ 12345916cd2Sjpk 12445916cd2Sjpk if (rhstrp == NULL) 12545916cd2Sjpk return (NULL); 12645916cd2Sjpk 12745916cd2Sjpk return (rhstr_to_ent(rhstrp, &err, &errstr)); 12845916cd2Sjpk } 12945916cd2Sjpk 13045916cd2Sjpk tsol_rhent_t * 131*909c1a33Ston tsol_fgetrhent(FILE *f, boolean_t *error) 13245916cd2Sjpk { 13345916cd2Sjpk int err = 0; 13445916cd2Sjpk char *errstr = NULL; 13545916cd2Sjpk char buf[NSS_BUFLEN_TSOL_RH]; 13645916cd2Sjpk tsol_rhstr_t result; 13745916cd2Sjpk tsol_rhstr_t *rhstrp = NULL; 13845916cd2Sjpk tsol_rhent_t *rhentp = NULL; 13945916cd2Sjpk nss_XbyY_args_t arg; 14045916cd2Sjpk 14145916cd2Sjpk NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr); 14245916cd2Sjpk _nss_XbyY_fgets(f, &arg); 14345916cd2Sjpk rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg); 14445916cd2Sjpk if (rhstrp == NULL) 14545916cd2Sjpk return (NULL); 14645916cd2Sjpk rhentp = rhstr_to_ent(rhstrp, &err, &errstr); 14745916cd2Sjpk while (rhentp == NULL) { 14845916cd2Sjpk /* 14945916cd2Sjpk * Loop until we find a non-blank, non-comment line, or 15045916cd2Sjpk * until EOF. No need to log blank lines, comments. 15145916cd2Sjpk */ 152*909c1a33Ston if (err != LTSNET_EMPTY) { 15345916cd2Sjpk (void) fprintf(stderr, "%s: %.32s%s: %s\n", 15445916cd2Sjpk gettext("Error parsing tnrhdb file"), errstr, 15545916cd2Sjpk (strlen(errstr) > 32)? "...": "", 15645916cd2Sjpk (char *)tsol_strerror(err, errno)); 157*909c1a33Ston *error = B_TRUE; 158*909c1a33Ston } 15945916cd2Sjpk _nss_XbyY_fgets(f, &arg); 16045916cd2Sjpk rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg); 16145916cd2Sjpk if (rhstrp == NULL) /* EOF */ 16245916cd2Sjpk return (NULL); 16345916cd2Sjpk rhentp = rhstr_to_ent(rhstrp, &err, &errstr); 16445916cd2Sjpk } 16545916cd2Sjpk return (rhentp); 16645916cd2Sjpk } 16745916cd2Sjpk 16845916cd2Sjpk /* 16945916cd2Sjpk * This is the callback routine for nss. 17045916cd2Sjpk */ 17145916cd2Sjpk int 17245916cd2Sjpk str_to_rhstr(const char *instr, int lenstr, void *entp, char *buffer, 17345916cd2Sjpk int buflen) 17445916cd2Sjpk { 17545916cd2Sjpk int len; 17645916cd2Sjpk char *str = NULL; 17745916cd2Sjpk char *last = NULL; 17845916cd2Sjpk char *sep = KV_TOKEN_DELIMIT; 17945916cd2Sjpk tsol_rhstr_t *rhstrp = (tsol_rhstr_t *)entp; 18045916cd2Sjpk 18145916cd2Sjpk if ((instr >= buffer && (buffer + buflen) > instr) || 18245916cd2Sjpk (buffer >= instr && (instr + lenstr) > buffer)) 18345916cd2Sjpk return (NSS_STR_PARSE_PARSE); 18445916cd2Sjpk if (lenstr >= buflen) 18545916cd2Sjpk return (NSS_STR_PARSE_ERANGE); 18645916cd2Sjpk (void) strncpy(buffer, instr, buflen); 18745916cd2Sjpk str = _strtok_escape(buffer, sep, &last); 18845916cd2Sjpk rhstrp->address = _do_unescape(str); 18945916cd2Sjpk /* 19045916cd2Sjpk * _do_unesape uses isspace() which removes "\n". 19145916cd2Sjpk * we keep "\n" as we use it in checking for 19245916cd2Sjpk * blank lines. 19345916cd2Sjpk */ 19445916cd2Sjpk if (strcmp(instr, "\n") == 0) 19545916cd2Sjpk rhstrp->address = "\n"; 19645916cd2Sjpk rhstrp->template = _strtok_escape(NULL, sep, &last); 19745916cd2Sjpk if (rhstrp->template != NULL) { 19845916cd2Sjpk len = strlen(rhstrp->template); 19945916cd2Sjpk if (rhstrp->template[len - 1] == '\n') 20045916cd2Sjpk rhstrp->template[len - 1] = '\0'; 20145916cd2Sjpk } 20245916cd2Sjpk if (rhstrp->address == NULL) 20345916cd2Sjpk rhstrp->family = 0; 20445916cd2Sjpk else if (strchr(rhstrp->address, ':') == NULL) 20545916cd2Sjpk rhstrp->family = AF_INET; 20645916cd2Sjpk else 20745916cd2Sjpk rhstrp->family = AF_INET6; 20845916cd2Sjpk 20945916cd2Sjpk #ifdef DEBUG 21045916cd2Sjpk (void) fprintf(stdout, 21145916cd2Sjpk "str_to_rhstr:str - %s\taddress - %s\n\ttemplate - %s\n", 21245916cd2Sjpk instr, rhstrp->address ? rhstrp->address : "NULL", 21345916cd2Sjpk rhstrp->template ? rhstrp->template : "NULL"); 21445916cd2Sjpk #endif /* DEBUG */ 21545916cd2Sjpk 21645916cd2Sjpk return (NSS_STR_PARSE_SUCCESS); 21745916cd2Sjpk } 21845916cd2Sjpk 21945916cd2Sjpk tsol_host_type_t 22045916cd2Sjpk tsol_getrhtype(char *rhost) { 22145916cd2Sjpk int herr; 22245916cd2Sjpk struct hostent *hp; 22345916cd2Sjpk in6_addr_t in6; 22445916cd2Sjpk char abuf[INET6_ADDRSTRLEN]; 22545916cd2Sjpk tsol_rhent_t rhent; 22645916cd2Sjpk tsol_tpent_t tp; 22745916cd2Sjpk 22845916cd2Sjpk if ((hp = getipnodebyname(rhost, AF_INET6, 22945916cd2Sjpk AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED, &herr)) == NULL) { 23045916cd2Sjpk return (UNLABELED); 23145916cd2Sjpk } 23245916cd2Sjpk 23345916cd2Sjpk (void) memset(&rhent, 0, sizeof (rhent)); 23445916cd2Sjpk (void) memcpy(&in6, hp->h_addr, hp->h_length); 23545916cd2Sjpk 23645916cd2Sjpk if (IN6_IS_ADDR_V4MAPPED(&in6)) { 23745916cd2Sjpk rhent.rh_address.ta_family = AF_INET; 23845916cd2Sjpk IN6_V4MAPPED_TO_INADDR(&in6, &rhent.rh_address.ta_addr_v4); 23945916cd2Sjpk (void) inet_ntop(AF_INET, &rhent.rh_address.ta_addr_v4, abuf, 24045916cd2Sjpk sizeof (abuf)); 24145916cd2Sjpk } else { 24245916cd2Sjpk rhent.rh_address.ta_family = AF_INET6; 24345916cd2Sjpk rhent.rh_address.ta_addr_v6 = in6; 24445916cd2Sjpk (void) inet_ntop(AF_INET6, &in6, abuf, sizeof (abuf)); 24545916cd2Sjpk } 24645916cd2Sjpk 24745916cd2Sjpk if (tnrh(TNDB_GET, &rhent) != 0) 24845916cd2Sjpk return (UNLABELED); 24945916cd2Sjpk 25045916cd2Sjpk if (rhent.rh_template[0] == '\0') 25145916cd2Sjpk return (UNLABELED); 25245916cd2Sjpk 25345916cd2Sjpk (void) strlcpy(tp.name, rhent.rh_template, sizeof (tp.name)); 25445916cd2Sjpk 25545916cd2Sjpk if (tnrhtp(TNDB_GET, &tp) != 0) 25645916cd2Sjpk return (UNLABELED); 25745916cd2Sjpk 25845916cd2Sjpk return (tp.host_type); 25945916cd2Sjpk } 260