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
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <netdb.h>
277c478bd9Sstevel@tonic-gate #include <netinet/in.h>
287c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
297c478bd9Sstevel@tonic-gate #include <sys/socket.h>
307c478bd9Sstevel@tonic-gate #include "ns_internal.h"
317c478bd9Sstevel@tonic-gate #include "ldap_common.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /* networks attributes filters */
347c478bd9Sstevel@tonic-gate #define	_N_NAME		"cn"
357c478bd9Sstevel@tonic-gate #define	_N_NETWORK	"ipnetworknumber"
367c478bd9Sstevel@tonic-gate #define	_F_GETNETBYNAME	"(&(objectClass=ipNetwork)(cn=%s))"
377c478bd9Sstevel@tonic-gate #define	_F_GETNETBYNAME_SSD	"(&(%%s)(cn=%s))"
38*cb5caa98Sdjl #define	_F_GETNETBYADDR	"(&(objectClass=ipNetwork)(|(ipNetworkNumber=%s)" \
39*cb5caa98Sdjl 						"(ipNetworkNumber=%s)))"
40*cb5caa98Sdjl #define	_F_GETNETBYADDR_SSD	"(&(%%s)(|(ipNetworkNumber=%s)" \
41*cb5caa98Sdjl 						"(ipNetworkNumber=%s)))"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static const char *networks_attrs[] = {
447c478bd9Sstevel@tonic-gate 	_N_NAME,
457c478bd9Sstevel@tonic-gate 	_N_NETWORK,
467c478bd9Sstevel@tonic-gate 	(char *)NULL
477c478bd9Sstevel@tonic-gate };
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
50*cb5caa98Sdjl  * _nss_ldap_networks2str is the data marshaling method for the networks
517c478bd9Sstevel@tonic-gate  * getXbyY * (e.g., getbyname(), getbyaddr(), getnetent() backend processes.
527c478bd9Sstevel@tonic-gate  * This method is called after a successful ldap search has been performed.
53*cb5caa98Sdjl  * This method will parse the ldap search values into the file format.
54*cb5caa98Sdjl  * e.g.
55*cb5caa98Sdjl  *
56*cb5caa98Sdjl  * SunRay-ce2	10.34.96.0	SunRay
57*cb5caa98Sdjl  *
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate static int
_nss_ldap_networks2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)60*cb5caa98Sdjl _nss_ldap_networks2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
617c478bd9Sstevel@tonic-gate {
62*cb5caa98Sdjl 	uint_t		i;
637c478bd9Sstevel@tonic-gate 	int		nss_result;
64*cb5caa98Sdjl 	int		buflen = 0, len;
65*cb5caa98Sdjl 	char		**network, *cname = NULL;
66*cb5caa98Sdjl 	char		*buffer = NULL;
677c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
68*cb5caa98Sdjl 	ns_ldap_attr_t	*names;
697c478bd9Sstevel@tonic-gate 
70*cb5caa98Sdjl 	if (result == NULL)
71*cb5caa98Sdjl 		return (NSS_STR_PARSE_PARSE);
72*cb5caa98Sdjl 	buflen = argp->buf.buflen;
737c478bd9Sstevel@tonic-gate 
74*cb5caa98Sdjl 	if (argp->buf.result != NULL) {
75*cb5caa98Sdjl 		if ((be->buffer = calloc(1, buflen)) == NULL) {
76*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
77*cb5caa98Sdjl 			goto result_net2str;
78*cb5caa98Sdjl 		}
79*cb5caa98Sdjl 		buffer = be->buffer;
80*cb5caa98Sdjl 	} else
81*cb5caa98Sdjl 		buffer = argp->buf.buffer;
82*cb5caa98Sdjl 
83*cb5caa98Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
847c478bd9Sstevel@tonic-gate 	(void) memset(argp->buf.buffer, 0, buflen);
857c478bd9Sstevel@tonic-gate 
86*cb5caa98Sdjl 	names = __ns_ldap_getAttrStruct(result->entry,  _N_NAME);
87*cb5caa98Sdjl 	if (names == NULL || names->attrvalue == NULL) {
88*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
89*cb5caa98Sdjl 		goto result_net2str;
907c478bd9Sstevel@tonic-gate 	}
91*cb5caa98Sdjl 	/* Get the canonical name */
92*cb5caa98Sdjl 	cname = __s_api_get_canonical_name(result->entry, names, 1);
93*cb5caa98Sdjl 	/*
94*cb5caa98Sdjl 	 * The definition of the object class  "ipNetwork" has a
95*cb5caa98Sdjl 	 * discrepency between RFC 2307 and 2307bis.
96*cb5caa98Sdjl 	 * In 2307, "cn" is a MUST attribute. In 2307bis, "cn" is a
97*cb5caa98Sdjl 	 * MAY attribute.
98*cb5caa98Sdjl 	 * If "cn" is a MAY attribute, it does not  appear in RDN and can't
99*cb5caa98Sdjl 	 * be derived from RDN as a canonical "cn" name. In that case, use 1st
100*cb5caa98Sdjl 	 * "cn" value as the official name.
101*cb5caa98Sdjl 	 */
102*cb5caa98Sdjl 	if (cname == NULL)
103*cb5caa98Sdjl 		/* 2307bis case */
104*cb5caa98Sdjl 		cname = names->attrvalue[0];
105*cb5caa98Sdjl 	if (cname == NULL || (len = strlen(cname)) < 1) {
106*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
107*cb5caa98Sdjl 		goto result_net2str;
108*cb5caa98Sdjl 	}
109*cb5caa98Sdjl 	network = __ns_ldap_getAttr(result->entry, _N_NETWORK);
110*cb5caa98Sdjl 	if (network == NULL || network[0] == NULL ||
111*cb5caa98Sdjl 			(len = strlen(network[0])) < 1) {
112*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
113*cb5caa98Sdjl 		goto result_net2str;
114*cb5caa98Sdjl 	}
115*cb5caa98Sdjl 	len = snprintf(buffer, buflen,  "%s %s", cname, network[0]);
116*cb5caa98Sdjl 	TEST_AND_ADJUST(len, buffer, buflen, result_net2str);
117*cb5caa98Sdjl 	/* Append aliases */
118*cb5caa98Sdjl 	for (i = 0; i < names->value_count; i++) {
119*cb5caa98Sdjl 		if (names->attrvalue[i] == NULL) {
120*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
121*cb5caa98Sdjl 			goto result_net2str;
1227c478bd9Sstevel@tonic-gate 		}
123*cb5caa98Sdjl 		/* Skip the canonical name */
124*cb5caa98Sdjl 		if (strcasecmp(names->attrvalue[i], cname) != 0) {
125*cb5caa98Sdjl 			len = snprintf(buffer, buflen,  " %s",
126*cb5caa98Sdjl 					names->attrvalue[i]);
127*cb5caa98Sdjl 			TEST_AND_ADJUST(len, buffer, buflen, result_net2str);
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
131*cb5caa98Sdjl 	/* The front end marshaller doesn't need to copy trailing nulls */
132*cb5caa98Sdjl 	if (argp->buf.result != NULL)
133*cb5caa98Sdjl 		be->buflen = strlen(be->buffer);
134*cb5caa98Sdjl 
135*cb5caa98Sdjl result_net2str:
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
138*cb5caa98Sdjl 	return (nss_result);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * Takes an unsigned integer in host order, and returns a printable
1437c478bd9Sstevel@tonic-gate  * string for it as a network number.  To allow for the possibility of
1447c478bd9Sstevel@tonic-gate  * naming subnets, only trailing dot-zeros are truncated.
145*cb5caa98Sdjl  * buf2 is untruncated version.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate 
nettoa(int anet,char * buf,char * buf2,int buflen)148*cb5caa98Sdjl static int nettoa(int anet, char *buf, char *buf2, int buflen)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	int		addr;
1517c478bd9Sstevel@tonic-gate 	char		*p;
1527c478bd9Sstevel@tonic-gate 	struct in_addr	in;
1537c478bd9Sstevel@tonic-gate 
154*cb5caa98Sdjl 	if (buf == NULL || buf2 == NULL)
1557c478bd9Sstevel@tonic-gate 		return ((int)1);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	in = inet_makeaddr(anet, INADDR_ANY);
1587c478bd9Sstevel@tonic-gate 	addr = in.s_addr;
159*cb5caa98Sdjl 	if (inet_ntop(AF_INET, (const void *)&in, buf2, INET_ADDRSTRLEN)
160*cb5caa98Sdjl 			== NULL)
161*cb5caa98Sdjl 		return ((int)1);
162*cb5caa98Sdjl 	if (strlcpy(buf, buf2, buflen) >= buflen)
1637c478bd9Sstevel@tonic-gate 		return ((int)1);
1647c478bd9Sstevel@tonic-gate 	if ((IN_CLASSA_HOST & htonl(addr)) == 0) {
1657c478bd9Sstevel@tonic-gate 		p = strchr(buf, '.');
1667c478bd9Sstevel@tonic-gate 		if (p == NULL)
1677c478bd9Sstevel@tonic-gate 			return ((int)1);
1687c478bd9Sstevel@tonic-gate 		*p = 0;
1697c478bd9Sstevel@tonic-gate 	} else if ((IN_CLASSB_HOST & htonl(addr)) == 0) {
1707c478bd9Sstevel@tonic-gate 		p = strchr(buf, '.');
1717c478bd9Sstevel@tonic-gate 		if (p == NULL)
1727c478bd9Sstevel@tonic-gate 			return ((int)1);
1737c478bd9Sstevel@tonic-gate 		p = strchr(p + 1, '.');
1747c478bd9Sstevel@tonic-gate 		if (p == NULL)
1757c478bd9Sstevel@tonic-gate 			return ((int)1);
1767c478bd9Sstevel@tonic-gate 		*p = 0;
1777c478bd9Sstevel@tonic-gate 	} else if ((IN_CLASSC_HOST & htonl(addr)) == 0) {
1787c478bd9Sstevel@tonic-gate 		p = strrchr(buf, '.');
1797c478bd9Sstevel@tonic-gate 		if (p == NULL)
1807c478bd9Sstevel@tonic-gate 			return ((int)1);
1817c478bd9Sstevel@tonic-gate 		*p = 0;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	return ((int)0);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * getbyname gets a network entry by name. This function constructs an
1907c478bd9Sstevel@tonic-gate  * ldap search filter using the network name invocation parameter and the
1917c478bd9Sstevel@tonic-gate  * getnetbyname search filter defined. Once the filter is constructed, we
1927c478bd9Sstevel@tonic-gate  * search for a matching entry and marshal the data results into struct
1937c478bd9Sstevel@tonic-gate  * netent for the frontend process. The function _nss_ldap_networks2ent
1947c478bd9Sstevel@tonic-gate  * performs the data marshaling.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)1987c478bd9Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
2017c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2027c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
2037c478bd9Sstevel@tonic-gate 	char		netname[SEARCHFILTERLEN];
2047c478bd9Sstevel@tonic-gate 	int		ret;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (_ldap_filter_name(netname, argp->key.name, sizeof (netname)) != 0)
2077c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
2107c478bd9Sstevel@tonic-gate 	    _F_GETNETBYNAME, netname);
2117c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
2127c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
2157c478bd9Sstevel@tonic-gate 	    _F_GETNETBYNAME_SSD, netname);
2167c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
2177c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
2207c478bd9Sstevel@tonic-gate 		_NETWORKS, searchfilter, NULL,
2217c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * getbyaddr gets a network entry by ip address. This function constructs an
2277c478bd9Sstevel@tonic-gate  * ldap search filter using the name invocation parameter and the getnetbyaddr
2287c478bd9Sstevel@tonic-gate  * search filter defined. Once the filter is constructed, we search for a
2297c478bd9Sstevel@tonic-gate  * matching entry and marshal the data results into struct netent for the
2307c478bd9Sstevel@tonic-gate  * frontend process. The function _nss_ldap_networks2ent performs the data
2317c478bd9Sstevel@tonic-gate  * marshaling.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static nss_status_t
getbyaddr(ldap_backend_ptr be,void * a)2357c478bd9Sstevel@tonic-gate getbyaddr(ldap_backend_ptr be, void *a)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
238*cb5caa98Sdjl 	char		addrstr[INET_ADDRSTRLEN], addrstr2[INET_ADDRSTRLEN];
2397c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2407c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
2417c478bd9Sstevel@tonic-gate 	int		ret;
2427c478bd9Sstevel@tonic-gate 
243*cb5caa98Sdjl 	if (nettoa((int)argp->key.netaddr.net, addrstr, addrstr2,
244*cb5caa98Sdjl 				INET_ADDRSTRLEN) != 0)
2457c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_UNAVAIL);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
248*cb5caa98Sdjl 	    _F_GETNETBYADDR, addrstr, addrstr2);
2497c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
2507c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
253*cb5caa98Sdjl 	    _F_GETNETBYADDR_SSD, addrstr, addrstr2);
2547c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
2557c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
2587c478bd9Sstevel@tonic-gate 		_NETWORKS, searchfilter, NULL,
2597c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static ldap_backend_op_t net_ops[] = {
2637c478bd9Sstevel@tonic-gate 	_nss_ldap_destr,
2647c478bd9Sstevel@tonic-gate 	_nss_ldap_endent,
2657c478bd9Sstevel@tonic-gate 	_nss_ldap_setent,
2667c478bd9Sstevel@tonic-gate 	_nss_ldap_getent,
2677c478bd9Sstevel@tonic-gate 	getbyname,
2687c478bd9Sstevel@tonic-gate 	getbyaddr
2697c478bd9Sstevel@tonic-gate };
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * _nss_ldap_networks_constr is where life begins. This function calls the
2747c478bd9Sstevel@tonic-gate  * generic ldap constructor function to define and build the abstract data
2757c478bd9Sstevel@tonic-gate  * types required to support ldap operations.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
2797c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_ldap_networks_constr(const char * dummy1,const char * dummy2,const char * dummy3)2807c478bd9Sstevel@tonic-gate _nss_ldap_networks_constr(const char *dummy1, const char *dummy2,
2817c478bd9Sstevel@tonic-gate 			const char *dummy3)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(net_ops,
2857c478bd9Sstevel@tonic-gate 		sizeof (net_ops)/sizeof (net_ops[0]), _NETWORKS,
286*cb5caa98Sdjl 		networks_attrs, _nss_ldap_networks2str));
2877c478bd9Sstevel@tonic-gate }
288