xref: /titanic_50/usr/src/lib/nsswitch/ldap/common/getnetent.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <netdb.h>
297c478bd9Sstevel@tonic-gate #include <netinet/in.h>
307c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
317c478bd9Sstevel@tonic-gate #include <sys/socket.h>
327c478bd9Sstevel@tonic-gate #include "ns_internal.h"
337c478bd9Sstevel@tonic-gate #include "ldap_common.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /* networks attributes filters */
367c478bd9Sstevel@tonic-gate #define	_N_NAME		"cn"
377c478bd9Sstevel@tonic-gate #define	_N_NETWORK	"ipnetworknumber"
387c478bd9Sstevel@tonic-gate #define	_F_GETNETBYNAME	"(&(objectClass=ipNetwork)(cn=%s))"
397c478bd9Sstevel@tonic-gate #define	_F_GETNETBYNAME_SSD	"(&(%%s)(cn=%s))"
40*cb5caa98Sdjl #define	_F_GETNETBYADDR	"(&(objectClass=ipNetwork)(|(ipNetworkNumber=%s)" \
41*cb5caa98Sdjl 						"(ipNetworkNumber=%s)))"
42*cb5caa98Sdjl #define	_F_GETNETBYADDR_SSD	"(&(%%s)(|(ipNetworkNumber=%s)" \
43*cb5caa98Sdjl 						"(ipNetworkNumber=%s)))"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static const char *networks_attrs[] = {
467c478bd9Sstevel@tonic-gate 	_N_NAME,
477c478bd9Sstevel@tonic-gate 	_N_NETWORK,
487c478bd9Sstevel@tonic-gate 	(char *)NULL
497c478bd9Sstevel@tonic-gate };
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
52*cb5caa98Sdjl  * _nss_ldap_networks2str is the data marshaling method for the networks
537c478bd9Sstevel@tonic-gate  * getXbyY * (e.g., getbyname(), getbyaddr(), getnetent() backend processes.
547c478bd9Sstevel@tonic-gate  * This method is called after a successful ldap search has been performed.
55*cb5caa98Sdjl  * This method will parse the ldap search values into the file format.
56*cb5caa98Sdjl  * e.g.
57*cb5caa98Sdjl  *
58*cb5caa98Sdjl  * SunRay-ce2	10.34.96.0	SunRay
59*cb5caa98Sdjl  *
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate static int
_nss_ldap_networks2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)62*cb5caa98Sdjl _nss_ldap_networks2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
637c478bd9Sstevel@tonic-gate {
64*cb5caa98Sdjl 	uint_t		i;
657c478bd9Sstevel@tonic-gate 	int		nss_result;
66*cb5caa98Sdjl 	int		buflen = 0, len;
67*cb5caa98Sdjl 	char		**network, *cname = NULL;
68*cb5caa98Sdjl 	char		*buffer = NULL;
697c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
70*cb5caa98Sdjl 	ns_ldap_attr_t	*names;
717c478bd9Sstevel@tonic-gate 
72*cb5caa98Sdjl 	if (result == NULL)
73*cb5caa98Sdjl 		return (NSS_STR_PARSE_PARSE);
74*cb5caa98Sdjl 	buflen = argp->buf.buflen;
75*cb5caa98Sdjl 
76*cb5caa98Sdjl 	if (argp->buf.result != NULL) {
77*cb5caa98Sdjl 		if ((be->buffer = calloc(1, buflen)) == NULL) {
78*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
79*cb5caa98Sdjl 			goto result_net2str;
807c478bd9Sstevel@tonic-gate 		}
81*cb5caa98Sdjl 		buffer = be->buffer;
82*cb5caa98Sdjl 	} else
83*cb5caa98Sdjl 		buffer = argp->buf.buffer;
847c478bd9Sstevel@tonic-gate 
85*cb5caa98Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
867c478bd9Sstevel@tonic-gate 	(void) memset(argp->buf.buffer, 0, buflen);
877c478bd9Sstevel@tonic-gate 
88*cb5caa98Sdjl 	names = __ns_ldap_getAttrStruct(result->entry,  _N_NAME);
89*cb5caa98Sdjl 	if (names == NULL || names->attrvalue == NULL) {
90*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
91*cb5caa98Sdjl 		goto result_net2str;
927c478bd9Sstevel@tonic-gate 	}
93*cb5caa98Sdjl 	/* Get the canonical name */
94*cb5caa98Sdjl 	cname = __s_api_get_canonical_name(result->entry, names, 1);
957c478bd9Sstevel@tonic-gate 	/*
96*cb5caa98Sdjl 	 * The definition of the object class  "ipNetwork" has a
97*cb5caa98Sdjl 	 * discrepency between RFC 2307 and 2307bis.
98*cb5caa98Sdjl 	 * In 2307, "cn" is a MUST attribute. In 2307bis, "cn" is a
99*cb5caa98Sdjl 	 * MAY attribute.
100*cb5caa98Sdjl 	 * If "cn" is a MAY attribute, it does not  appear in RDN and can't
101*cb5caa98Sdjl 	 * be derived from RDN as a canonical "cn" name. In that case, use 1st
1027c478bd9Sstevel@tonic-gate 	 * "cn" value as the official name.
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 	if (cname == NULL)
1057c478bd9Sstevel@tonic-gate 		/* 2307bis case */
106*cb5caa98Sdjl 		cname = names->attrvalue[0];
107*cb5caa98Sdjl 	if (cname == NULL || (len = strlen(cname)) < 1) {
108*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
109*cb5caa98Sdjl 		goto result_net2str;
110*cb5caa98Sdjl 	}
111*cb5caa98Sdjl 	network = __ns_ldap_getAttr(result->entry, _N_NETWORK);
112*cb5caa98Sdjl 	if (network == NULL || network[0] == NULL ||
113*cb5caa98Sdjl 			(len = strlen(network[0])) < 1) {
114*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
115*cb5caa98Sdjl 		goto result_net2str;
116*cb5caa98Sdjl 	}
117*cb5caa98Sdjl 	len = snprintf(buffer, buflen,  "%s %s", cname, network[0]);
118*cb5caa98Sdjl 	TEST_AND_ADJUST(len, buffer, buflen, result_net2str);
119*cb5caa98Sdjl 	/* Append aliases */
120*cb5caa98Sdjl 	for (i = 0; i < names->value_count; i++) {
121*cb5caa98Sdjl 		if (names->attrvalue[i] == NULL) {
122*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
123*cb5caa98Sdjl 			goto result_net2str;
124*cb5caa98Sdjl 		}
125*cb5caa98Sdjl 		/* Skip the canonical name */
126*cb5caa98Sdjl 		if (strcasecmp(names->attrvalue[i], cname) != 0) {
127*cb5caa98Sdjl 			len = snprintf(buffer, buflen,  " %s",
128*cb5caa98Sdjl 					names->attrvalue[i]);
129*cb5caa98Sdjl 			TEST_AND_ADJUST(len, buffer, buflen, result_net2str);
130*cb5caa98Sdjl 		}
131*cb5caa98Sdjl 	}
1327c478bd9Sstevel@tonic-gate 
133*cb5caa98Sdjl 	/* The front end marshaller doesn't need to copy trailing nulls */
134*cb5caa98Sdjl 	if (argp->buf.result != NULL)
135*cb5caa98Sdjl 		be->buflen = strlen(be->buffer);
1367c478bd9Sstevel@tonic-gate 
137*cb5caa98Sdjl result_net2str:
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
140*cb5caa98Sdjl 	return (nss_result);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * Takes an unsigned integer in host order, and returns a printable
1457c478bd9Sstevel@tonic-gate  * string for it as a network number.  To allow for the possibility of
1467c478bd9Sstevel@tonic-gate  * naming subnets, only trailing dot-zeros are truncated.
147*cb5caa98Sdjl  * buf2 is untruncated version.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate 
nettoa(int anet,char * buf,char * buf2,int buflen)150*cb5caa98Sdjl static int nettoa(int anet, char *buf, char *buf2, int buflen)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	int		addr;
1537c478bd9Sstevel@tonic-gate 	char		*p;
1547c478bd9Sstevel@tonic-gate 	struct in_addr	in;
1557c478bd9Sstevel@tonic-gate 
156*cb5caa98Sdjl 	if (buf == NULL || buf2 == NULL)
1577c478bd9Sstevel@tonic-gate 		return ((int)1);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	in = inet_makeaddr(anet, INADDR_ANY);
1607c478bd9Sstevel@tonic-gate 	addr = in.s_addr;
161*cb5caa98Sdjl 	if (inet_ntop(AF_INET, (const void *)&in, buf2, INET_ADDRSTRLEN)
162*cb5caa98Sdjl 			== NULL)
163*cb5caa98Sdjl 		return ((int)1);
164*cb5caa98Sdjl 	if (strlcpy(buf, buf2, buflen) >= buflen)
1657c478bd9Sstevel@tonic-gate 		return ((int)1);
1667c478bd9Sstevel@tonic-gate 	if ((IN_CLASSA_HOST & htonl(addr)) == 0) {
1677c478bd9Sstevel@tonic-gate 		p = strchr(buf, '.');
1687c478bd9Sstevel@tonic-gate 		if (p == NULL)
1697c478bd9Sstevel@tonic-gate 			return ((int)1);
1707c478bd9Sstevel@tonic-gate 		*p = 0;
1717c478bd9Sstevel@tonic-gate 	} else if ((IN_CLASSB_HOST & htonl(addr)) == 0) {
1727c478bd9Sstevel@tonic-gate 		p = strchr(buf, '.');
1737c478bd9Sstevel@tonic-gate 		if (p == NULL)
1747c478bd9Sstevel@tonic-gate 			return ((int)1);
1757c478bd9Sstevel@tonic-gate 		p = strchr(p + 1, '.');
1767c478bd9Sstevel@tonic-gate 		if (p == NULL)
1777c478bd9Sstevel@tonic-gate 			return ((int)1);
1787c478bd9Sstevel@tonic-gate 		*p = 0;
1797c478bd9Sstevel@tonic-gate 	} else if ((IN_CLASSC_HOST & htonl(addr)) == 0) {
1807c478bd9Sstevel@tonic-gate 		p = strrchr(buf, '.');
1817c478bd9Sstevel@tonic-gate 		if (p == NULL)
1827c478bd9Sstevel@tonic-gate 			return ((int)1);
1837c478bd9Sstevel@tonic-gate 		*p = 0;
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	return ((int)0);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * getbyname gets a network entry by name. This function constructs an
1927c478bd9Sstevel@tonic-gate  * ldap search filter using the network name invocation parameter and the
1937c478bd9Sstevel@tonic-gate  * getnetbyname search filter defined. Once the filter is constructed, we
1947c478bd9Sstevel@tonic-gate  * search for a matching entry and marshal the data results into struct
1957c478bd9Sstevel@tonic-gate  * netent for the frontend process. The function _nss_ldap_networks2ent
1967c478bd9Sstevel@tonic-gate  * performs the data marshaling.
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)2007c478bd9Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
2037c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2047c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
2057c478bd9Sstevel@tonic-gate 	char		netname[SEARCHFILTERLEN];
2067c478bd9Sstevel@tonic-gate 	int		ret;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (_ldap_filter_name(netname, argp->key.name, sizeof (netname)) != 0)
2097c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
2127c478bd9Sstevel@tonic-gate 	    _F_GETNETBYNAME, netname);
2137c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
2147c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
2177c478bd9Sstevel@tonic-gate 	    _F_GETNETBYNAME_SSD, netname);
2187c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
2197c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
2227c478bd9Sstevel@tonic-gate 		_NETWORKS, searchfilter, NULL,
2237c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * getbyaddr gets a network entry by ip address. This function constructs an
2297c478bd9Sstevel@tonic-gate  * ldap search filter using the name invocation parameter and the getnetbyaddr
2307c478bd9Sstevel@tonic-gate  * search filter defined. Once the filter is constructed, we search for a
2317c478bd9Sstevel@tonic-gate  * matching entry and marshal the data results into struct netent for the
2327c478bd9Sstevel@tonic-gate  * frontend process. The function _nss_ldap_networks2ent performs the data
2337c478bd9Sstevel@tonic-gate  * marshaling.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate static nss_status_t
getbyaddr(ldap_backend_ptr be,void * a)2377c478bd9Sstevel@tonic-gate getbyaddr(ldap_backend_ptr be, void *a)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
240*cb5caa98Sdjl 	char		addrstr[INET_ADDRSTRLEN], addrstr2[INET_ADDRSTRLEN];
2417c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2427c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
2437c478bd9Sstevel@tonic-gate 	int		ret;
2447c478bd9Sstevel@tonic-gate 
245*cb5caa98Sdjl 	if (nettoa((int)argp->key.netaddr.net, addrstr, addrstr2,
246*cb5caa98Sdjl 				INET_ADDRSTRLEN) != 0)
2477c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_UNAVAIL);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
250*cb5caa98Sdjl 	    _F_GETNETBYADDR, addrstr, addrstr2);
2517c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
2527c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
255*cb5caa98Sdjl 	    _F_GETNETBYADDR_SSD, addrstr, addrstr2);
2567c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
2577c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
2607c478bd9Sstevel@tonic-gate 		_NETWORKS, searchfilter, NULL,
2617c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static ldap_backend_op_t net_ops[] = {
2657c478bd9Sstevel@tonic-gate 	_nss_ldap_destr,
2667c478bd9Sstevel@tonic-gate 	_nss_ldap_endent,
2677c478bd9Sstevel@tonic-gate 	_nss_ldap_setent,
2687c478bd9Sstevel@tonic-gate 	_nss_ldap_getent,
2697c478bd9Sstevel@tonic-gate 	getbyname,
2707c478bd9Sstevel@tonic-gate 	getbyaddr
2717c478bd9Sstevel@tonic-gate };
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * _nss_ldap_networks_constr is where life begins. This function calls the
2767c478bd9Sstevel@tonic-gate  * generic ldap constructor function to define and build the abstract data
2777c478bd9Sstevel@tonic-gate  * types required to support ldap operations.
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
2817c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_ldap_networks_constr(const char * dummy1,const char * dummy2,const char * dummy3)2827c478bd9Sstevel@tonic-gate _nss_ldap_networks_constr(const char *dummy1, const char *dummy2,
2837c478bd9Sstevel@tonic-gate 			const char *dummy3)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(net_ops,
2877c478bd9Sstevel@tonic-gate 		sizeof (net_ops)/sizeof (net_ops[0]), _NETWORKS,
288*cb5caa98Sdjl 		networks_attrs, _nss_ldap_networks2str));
2897c478bd9Sstevel@tonic-gate }
290