Home | History | Annotate | Download | only in common
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  *
     25  * From "tsol_getrhent.c	7.6	00/09/22 SMI; TSOL 2.x"
     26  */
     27 
     28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     29 
     30 #include <stdio.h>
     31 #include <nss_dbdefs.h>
     32 #include <libtsnet.h>
     33 #include <sys/types.h>
     34 #include <sys/socket.h>
     35 #include <netinet/in.h>
     36 #include <arpa/inet.h>
     37 #include <string.h>
     38 #include <secdb.h>
     39 #include <nss.h>
     40 #include <libtsnet.h>
     41 #include <libintl.h>
     42 
     43 extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);	/* from lib.c */
     44 
     45 static int tsol_rh_stayopen;	/* Unsynchronized, but it affects only	*/
     46 				/*   efficiency, not correctness	*/
     47 static DEFINE_NSS_DB_ROOT(db_root);
     48 static DEFINE_NSS_GETENT(context);
     49 
     50 static void
     51 _nss_initf_tsol_rh(nss_db_params_t *p)
     52 {
     53 	p->name	= NSS_DBNAM_TSOL_RH;
     54 	p->default_config = NSS_DEFCONF_TSOL_RH;
     55 }
     56 
     57 tsol_rhent_t *
     58 tsol_getrhbyaddr(const void *addrp, size_t len, int af)
     59 {
     60 	int		err = 0;
     61 	char		*errstr = NULL;
     62 	char		buf[NSS_BUFLEN_TSOL_RH];
     63 	tsol_rhstr_t	result;
     64 	tsol_rhstr_t	*rhstrp = NULL;
     65 	nss_XbyY_args_t arg;
     66 
     67 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr);
     68 
     69 	arg.key.hostaddr.addr = (const char *)addrp;
     70 	arg.key.hostaddr.len = len;
     71 	arg.key.hostaddr.type = af;
     72 	arg.stayopen = tsol_rh_stayopen;
     73 	arg.h_errno = TSOL_NOT_FOUND;
     74 	arg.status = nss_search(&db_root, _nss_initf_tsol_rh,
     75 	    NSS_DBOP_TSOL_RH_BYADDR, &arg);
     76 	rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
     77 
     78 #ifdef	DEBUG
     79 	(void) fprintf(stdout, "tsol_getrhbyaddr %s: %s\n",
     80 	    (char *)addrp, rhstrp ? rhstrp->template : "NULL");
     81 #endif	/* DEBUG */
     82 
     83 	if (rhstrp == NULL)
     84 		return (NULL);
     85 
     86 	return (rhstr_to_ent(rhstrp, &err, &errstr));
     87 }
     88 
     89 void
     90 tsol_setrhent(int stay)
     91 {
     92 	tsol_rh_stayopen |= stay;
     93 	nss_setent(&db_root, _nss_initf_tsol_rh, &context);
     94 }
     95 
     96 void
     97 tsol_endrhent(void)
     98 {
     99 	tsol_rh_stayopen = 0;
    100 	nss_endent(&db_root, _nss_initf_tsol_rh, &context);
    101 	nss_delete(&db_root);
    102 }
    103 
    104 tsol_rhent_t *
    105 tsol_getrhent(void)
    106 {
    107 	int			err = 0;
    108 	char			*errstr = NULL;
    109 	char			buf[NSS_BUFLEN_TSOL_RH];
    110 	tsol_rhstr_t		result;
    111 	tsol_rhstr_t		*rhstrp = NULL;
    112 	nss_XbyY_args_t		arg;
    113 
    114 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr);
    115 	/* No key, no stayopen */
    116 	arg.status = nss_getent(&db_root, _nss_initf_tsol_rh, &context, &arg);
    117 	rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
    118 
    119 #ifdef	DEBUG
    120 	(void) fprintf(stdout, "tsol_getrhent: %s\n",
    121 	    rhstrp ? rhstrp->template : "NULL");
    122 #endif	/* DEBUG */
    123 
    124 	if (rhstrp == NULL)
    125 		return (NULL);
    126 
    127 	return (rhstr_to_ent(rhstrp, &err, &errstr));
    128 }
    129 
    130 tsol_rhent_t *
    131 tsol_fgetrhent(FILE *f, boolean_t *error)
    132 {
    133 	int		err = 0;
    134 	char		*errstr = NULL;
    135 	char		buf[NSS_BUFLEN_TSOL_RH];
    136 	tsol_rhstr_t	result;
    137 	tsol_rhstr_t	*rhstrp = NULL;
    138 	tsol_rhent_t	*rhentp = NULL;
    139 	nss_XbyY_args_t	arg;
    140 
    141 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr);
    142 	_nss_XbyY_fgets(f, &arg);
    143 	rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
    144 	if (rhstrp == NULL)
    145 		return (NULL);
    146 	rhentp = rhstr_to_ent(rhstrp, &err, &errstr);
    147 	while (rhentp == NULL) {
    148 		/*
    149 		 * Loop until we find a non-blank, non-comment line, or
    150 		 * until EOF. No need to log blank lines, comments.
    151 		 */
    152 		if (err != LTSNET_EMPTY) {
    153 			(void) fprintf(stderr, "%s: %.32s%s: %s\n",
    154 			    gettext("Error parsing tnrhdb file"), errstr,
    155 			    (strlen(errstr) > 32)? "...": "",
    156 			    (char *)tsol_strerror(err, errno));
    157 			*error = B_TRUE;
    158 		}
    159 		_nss_XbyY_fgets(f, &arg);
    160 		rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
    161 		if (rhstrp == NULL)	/* EOF */
    162 			return (NULL);
    163 		rhentp = rhstr_to_ent(rhstrp, &err, &errstr);
    164 	}
    165 	return (rhentp);
    166 }
    167 
    168 /*
    169  * This is the callback routine for nss.
    170  */
    171 int
    172 str_to_rhstr(const char *instr, int lenstr, void *entp, char *buffer,
    173     int buflen)
    174 {
    175 	int		len;
    176 	char		*str = NULL;
    177 	char		*last = NULL;
    178 	char		*sep = KV_TOKEN_DELIMIT;
    179 	tsol_rhstr_t	*rhstrp = (tsol_rhstr_t *)entp;
    180 
    181 	if ((instr >= buffer && (buffer + buflen) > instr) ||
    182 	    (buffer >= instr && (instr + lenstr) > buffer))
    183 		return (NSS_STR_PARSE_PARSE);
    184 	if (lenstr >= buflen)
    185 		return (NSS_STR_PARSE_ERANGE);
    186 	(void) strncpy(buffer, instr, buflen);
    187 	str = _strtok_escape(buffer, sep, &last);
    188 	rhstrp->address = _do_unescape(str);
    189 	/*
    190 	 * _do_unesape uses isspace() which removes "\n".
    191 	 * we keep "\n" as we use it in checking for
    192 	 * blank lines.
    193 	 */
    194 	if (strcmp(instr, "\n") == 0)
    195 		rhstrp->address = "\n";
    196 	rhstrp->template = _strtok_escape(NULL, sep, &last);
    197 	if (rhstrp->template != NULL) {
    198 		len = strlen(rhstrp->template);
    199 		if (rhstrp->template[len - 1] == '\n')
    200 			rhstrp->template[len - 1] = '\0';
    201 	}
    202 	if (rhstrp->address == NULL)
    203 		rhstrp->family = 0;
    204 	else if (strchr(rhstrp->address, ':') == NULL)
    205 		rhstrp->family = AF_INET;
    206 	else
    207 		rhstrp->family = AF_INET6;
    208 
    209 #ifdef	DEBUG
    210 	(void) fprintf(stdout,
    211 	    "str_to_rhstr:str - %s\taddress - %s\n\ttemplate - %s\n",
    212 	    instr, rhstrp->address ? rhstrp->address : "NULL",
    213 	    rhstrp->template ? rhstrp->template : "NULL");
    214 #endif	/* DEBUG */
    215 
    216 	return (NSS_STR_PARSE_SUCCESS);
    217 }
    218 
    219 tsol_host_type_t
    220 tsol_getrhtype(char *rhost) {
    221 	int herr;
    222 	struct hostent *hp;
    223 	in6_addr_t in6;
    224 	char abuf[INET6_ADDRSTRLEN];
    225 	tsol_rhent_t rhent;
    226 	tsol_tpent_t tp;
    227 
    228 	if ((hp = getipnodebyname(rhost, AF_INET6,
    229 	    AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED, &herr)) == NULL) {
    230 		return (UNLABELED);
    231 	}
    232 
    233 	(void) memset(&rhent, 0, sizeof (rhent));
    234 	(void) memcpy(&in6, hp->h_addr, hp->h_length);
    235 
    236 	if (IN6_IS_ADDR_V4MAPPED(&in6)) {
    237 		rhent.rh_address.ta_family = AF_INET;
    238 		IN6_V4MAPPED_TO_INADDR(&in6, &rhent.rh_address.ta_addr_v4);
    239 		(void) inet_ntop(AF_INET, &rhent.rh_address.ta_addr_v4, abuf,
    240 		    sizeof (abuf));
    241 	} else {
    242 		rhent.rh_address.ta_family = AF_INET6;
    243 		rhent.rh_address.ta_addr_v6 = in6;
    244 		(void) inet_ntop(AF_INET6, &in6, abuf, sizeof (abuf));
    245 	}
    246 
    247 	if (tnrh(TNDB_GET, &rhent) != 0)
    248 		return (UNLABELED);
    249 
    250 	if (rhent.rh_template[0] == '\0')
    251 		return (UNLABELED);
    252 
    253 	(void) strlcpy(tp.name, rhent.rh_template, sizeof (tp.name));
    254 
    255 	if (tnrhtp(TNDB_GET, &tp) != 0)
    256 		return (UNLABELED);
    257 
    258 	return (tp.host_type);
    259 }
    260