xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/tsol_getrhent.c (revision 2830:5228d1267a01)
11676Sjpk /*
21676Sjpk  * CDDL HEADER START
31676Sjpk  *
41676Sjpk  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
71676Sjpk  *
81676Sjpk  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91676Sjpk  * or http://www.opensolaris.org/os/licensing.
101676Sjpk  * See the License for the specific language governing permissions
111676Sjpk  * and limitations under the License.
121676Sjpk  *
131676Sjpk  * When distributing Covered Code, include this CDDL HEADER in each
141676Sjpk  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151676Sjpk  * If applicable, add the following below this CDDL HEADER, with the
161676Sjpk  * fields enclosed by brackets "[]" replaced with your own identifying
171676Sjpk  * information: Portions Copyright [yyyy] [name of copyright owner]
181676Sjpk  *
191676Sjpk  * CDDL HEADER END
201676Sjpk  */
211676Sjpk /*
221676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
231676Sjpk  * Use is subject to license terms.
241676Sjpk  */
251676Sjpk 
261676Sjpk #pragma ident	"%Z%%M%	%I%	%E% SMI"
271676Sjpk 
281676Sjpk #include <netdb.h>
291676Sjpk #include "ldap_common.h"
301676Sjpk #include <sys/types.h>
311676Sjpk #include <sys/socket.h>
321676Sjpk #include <netinet/in.h>
331676Sjpk #include <arpa/inet.h>
341676Sjpk #include <sys/tsol/tndb.h>
351676Sjpk 
361676Sjpk /* tnrhdb attributes filters */
371676Sjpk #define	_TNRHDB_ADDR		"ipTnetNumber"
381676Sjpk #define	_TNRHDB_TNAME		"ipTnetTemplateName"
391676Sjpk #define	_F_GETTNDBBYADDR	"(&(objectClass=ipTnetHost)(ipTnetNumber=%s))"
401676Sjpk #define	_F_GETTNDBBYADDR_SSD	"(&(%%s)(ipTnetNumber=%s))"
411676Sjpk 
421676Sjpk static const char *tnrhdb_attrs[] = {
431676Sjpk 	_TNRHDB_ADDR,
441676Sjpk 	_TNRHDB_TNAME,
451676Sjpk 	NULL
461676Sjpk };
471676Sjpk 
48*2830Sdjl static void
escape_colon(char * in,char * out)49*2830Sdjl escape_colon(char *in, char *out) {
50*2830Sdjl 	int i, j;
51*2830Sdjl 	for (i = 0, j = 0; in[i] != '\0'; i++) {
52*2830Sdjl 		if (in[i] == ':') {
53*2830Sdjl 			out[j++] = '\\';
54*2830Sdjl 			out[j++] = in[i];
55*2830Sdjl 		} else
56*2830Sdjl 			out[j++] = in[i];
57*2830Sdjl 	}
58*2830Sdjl 	out[j] = '\0';
59*2830Sdjl }
601676Sjpk 
61*2830Sdjl /*
62*2830Sdjl  * _nss_ldap_tnrhdb2str is the data marshaling method for the tnrhdb
63*2830Sdjl  * (tsol_getrhbyaddr()/tsol_getrhent()) backend processes.
64*2830Sdjl  * This method is called after a successful ldap search has been performed.
65*2830Sdjl  * This method will parse the ldap search values into the file format.
66*2830Sdjl  *
67*2830Sdjl  * e.g.
68*2830Sdjl  *
69*2830Sdjl  * 192.168.120.6:public
70*2830Sdjl  * fec0\:\:a00\:20ff\:fea0\:21f7:cipso
71*2830Sdjl  *
72*2830Sdjl  */
73*2830Sdjl static int
_nss_ldap_tnrhdb2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)74*2830Sdjl _nss_ldap_tnrhdb2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
75*2830Sdjl {
76*2830Sdjl 	int			nss_result = NSS_STR_PARSE_SUCCESS;
77*2830Sdjl 	int			len = 0;
78*2830Sdjl 	char			*buffer = NULL;
79*2830Sdjl 	char			**addr, **template, *addr_out;
80*2830Sdjl 	ns_ldap_result_t	*result = be->result;
81*2830Sdjl 	char addr6[INET6_ADDRSTRLEN + 5]; /* 5 '\' for ':' at most */
82*2830Sdjl 
83*2830Sdjl 	if (result == NULL)
84*2830Sdjl 		return (NSS_STR_PARSE_PARSE);
85*2830Sdjl 
86*2830Sdjl 	addr = __ns_ldap_getAttr(result->entry, _TNRHDB_ADDR);
87*2830Sdjl 	if (addr == NULL || addr[0] == NULL || (strlen(addr[0]) < 1)) {
88*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
89*2830Sdjl 		goto result_tnrhdb2str;
901676Sjpk 	}
91*2830Sdjl 
92*2830Sdjl 	/*
93*2830Sdjl 	 * Escape ':' in IPV6.
94*2830Sdjl 	 * The value is stored in LDAP directory without escape charaters.
95*2830Sdjl 	 */
96*2830Sdjl 	if (strchr(addr[0], ':') != NULL) {
97*2830Sdjl 		escape_colon(addr[0], addr6);
98*2830Sdjl 		addr_out = addr6;
99*2830Sdjl 	} else
100*2830Sdjl 		addr_out = addr[0];
101*2830Sdjl 
102*2830Sdjl 	template = __ns_ldap_getAttr(result->entry, _TNRHDB_TNAME);
103*2830Sdjl 	if (template == NULL || template[0] == NULL ||
104*2830Sdjl 			(strlen(template[0]) < 1)) {
1051676Sjpk 		nss_result = NSS_STR_PARSE_PARSE;
106*2830Sdjl 		goto result_tnrhdb2str;
1071676Sjpk 	}
108*2830Sdjl 	/* "addr:template" */
109*2830Sdjl 	len = strlen(addr_out) + strlen(template[0]) + 2;
110*2830Sdjl 
111*2830Sdjl 	if (argp->buf.result != NULL) {
112*2830Sdjl 		if ((be->buffer = calloc(1, len)) == NULL) {
1131676Sjpk 			nss_result = NSS_STR_PARSE_PARSE;
114*2830Sdjl 			goto result_tnrhdb2str;
1151676Sjpk 		}
116*2830Sdjl 		be->buflen = len - 1;
117*2830Sdjl 		buffer = be->buffer;
118*2830Sdjl 	} else
119*2830Sdjl 		buffer = argp->buf.buffer;
1201676Sjpk 
121*2830Sdjl 	(void) snprintf(buffer, len, "%s:%s", addr_out, template[0]);
1221676Sjpk 
123*2830Sdjl result_tnrhdb2str:
1241676Sjpk 	(void) __ns_ldap_freeResult(&be->result);
1251676Sjpk 	return (nss_result);
1261676Sjpk }
1271676Sjpk 
1281676Sjpk 
1291676Sjpk static nss_status_t
getbyaddr(ldap_backend_ptr be,void * a)1301676Sjpk getbyaddr(ldap_backend_ptr be, void *a)
1311676Sjpk {
1321676Sjpk 	char		searchfilter[SEARCHFILTERLEN];
1331676Sjpk 	char		userdata[SEARCHFILTERLEN];
1341676Sjpk 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1351676Sjpk 
136*2830Sdjl 	if (argp->key.hostaddr.addr == NULL ||
137*2830Sdjl 		(argp->key.hostaddr.type != AF_INET &&
138*2830Sdjl 		argp->key.hostaddr.type != AF_INET6))
139*2830Sdjl 			return (NSS_NOTFOUND);
140*2830Sdjl 	if (strchr(argp->key.hostaddr.addr, ':') != NULL) {
141*2830Sdjl 		/* IPV6 */
142*2830Sdjl 		if (argp->key.hostaddr.type == AF_INET)
143*2830Sdjl 			return (NSS_NOTFOUND);
144*2830Sdjl 	} else {
145*2830Sdjl 		/* IPV4 */
146*2830Sdjl 		if (argp->key.hostaddr.type == AF_INET6)
147*2830Sdjl 			return (NSS_NOTFOUND);
148*2830Sdjl 	}
1491676Sjpk 
150*2830Sdjl 	/*
151*2830Sdjl 	 * The IPV6 addresses are saved in the directory without '\'s.
152*2830Sdjl 	 * So don't need to escape colons in IPV6 addresses.
153*2830Sdjl 	 */
1541676Sjpk 	if (snprintf(searchfilter, sizeof (searchfilter), _F_GETTNDBBYADDR,
155*2830Sdjl 	    argp->key.hostaddr.addr) < 0)
1561676Sjpk 		return ((nss_status_t)NSS_NOTFOUND);
1571676Sjpk 
1581676Sjpk 	if (snprintf(userdata, sizeof (userdata), _F_GETTNDBBYADDR_SSD,
159*2830Sdjl 	    argp->key.hostaddr.addr) < 0)
1601676Sjpk 		return ((nss_status_t)NSS_NOTFOUND);
1611676Sjpk 
1621676Sjpk 	return (_nss_ldap_lookup(be, argp, _TNRHDB, searchfilter, NULL,
1631676Sjpk 	    _merge_SSD_filter, userdata));
1641676Sjpk }
1651676Sjpk 
1661676Sjpk 
1671676Sjpk static ldap_backend_op_t tnrhdb_ops[] = {
1681676Sjpk 	_nss_ldap_destr,
1691676Sjpk 	_nss_ldap_endent,
1701676Sjpk 	_nss_ldap_setent,
1711676Sjpk 	_nss_ldap_getent,
1721676Sjpk 	getbyaddr
1731676Sjpk };
1741676Sjpk 
1751676Sjpk 
1761676Sjpk /* ARGSUSED */
1771676Sjpk nss_backend_t *
_nss_ldap_tnrhdb_constr(const char * dummy1,const char * dummy2,const char * dummy3,const char * dummy4,const char * dummy5)1781676Sjpk _nss_ldap_tnrhdb_constr(const char *dummy1,
1791676Sjpk     const char *dummy2,
1801676Sjpk     const char *dummy3,
1811676Sjpk     const char *dummy4,
1821676Sjpk     const char *dummy5)
1831676Sjpk {
1841676Sjpk 	return ((nss_backend_t *)_nss_ldap_constr(tnrhdb_ops,
1851676Sjpk 		sizeof (tnrhdb_ops)/sizeof (tnrhdb_ops[0]), _TNRHDB,
186*2830Sdjl 		tnrhdb_attrs, _nss_ldap_tnrhdb2str));
1871676Sjpk }
188