xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/getnetent.c (revision 2830:5228d1267a01)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <netdb.h>
290Sstevel@tonic-gate #include <netinet/in.h>
300Sstevel@tonic-gate #include <arpa/inet.h>
310Sstevel@tonic-gate #include <sys/socket.h>
320Sstevel@tonic-gate #include "ns_internal.h"
330Sstevel@tonic-gate #include "ldap_common.h"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /* networks attributes filters */
360Sstevel@tonic-gate #define	_N_NAME		"cn"
370Sstevel@tonic-gate #define	_N_NETWORK	"ipnetworknumber"
380Sstevel@tonic-gate #define	_F_GETNETBYNAME	"(&(objectClass=ipNetwork)(cn=%s))"
390Sstevel@tonic-gate #define	_F_GETNETBYNAME_SSD	"(&(%%s)(cn=%s))"
40*2830Sdjl #define	_F_GETNETBYADDR	"(&(objectClass=ipNetwork)(|(ipNetworkNumber=%s)" \
41*2830Sdjl 						"(ipNetworkNumber=%s)))"
42*2830Sdjl #define	_F_GETNETBYADDR_SSD	"(&(%%s)(|(ipNetworkNumber=%s)" \
43*2830Sdjl 						"(ipNetworkNumber=%s)))"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static const char *networks_attrs[] = {
460Sstevel@tonic-gate 	_N_NAME,
470Sstevel@tonic-gate 	_N_NETWORK,
480Sstevel@tonic-gate 	(char *)NULL
490Sstevel@tonic-gate };
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
52*2830Sdjl  * _nss_ldap_networks2str is the data marshaling method for the networks
530Sstevel@tonic-gate  * getXbyY * (e.g., getbyname(), getbyaddr(), getnetent() backend processes.
540Sstevel@tonic-gate  * This method is called after a successful ldap search has been performed.
55*2830Sdjl  * This method will parse the ldap search values into the file format.
56*2830Sdjl  * e.g.
57*2830Sdjl  *
58*2830Sdjl  * SunRay-ce2	10.34.96.0	SunRay
59*2830Sdjl  *
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate static int
_nss_ldap_networks2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)62*2830Sdjl _nss_ldap_networks2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
630Sstevel@tonic-gate {
64*2830Sdjl 	uint_t		i;
650Sstevel@tonic-gate 	int		nss_result;
66*2830Sdjl 	int		buflen = 0, len;
67*2830Sdjl 	char		**network, *cname = NULL;
68*2830Sdjl 	char		*buffer = NULL;
690Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
70*2830Sdjl 	ns_ldap_attr_t	*names;
71*2830Sdjl 
72*2830Sdjl 	if (result == NULL)
73*2830Sdjl 		return (NSS_STR_PARSE_PARSE);
74*2830Sdjl 	buflen = argp->buf.buflen;
750Sstevel@tonic-gate 
76*2830Sdjl 	if (argp->buf.result != NULL) {
77*2830Sdjl 		if ((be->buffer = calloc(1, buflen)) == NULL) {
78*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
79*2830Sdjl 			goto result_net2str;
80*2830Sdjl 		}
81*2830Sdjl 		buffer = be->buffer;
82*2830Sdjl 	} else
83*2830Sdjl 		buffer = argp->buf.buffer;
840Sstevel@tonic-gate 
85*2830Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
860Sstevel@tonic-gate 	(void) memset(argp->buf.buffer, 0, buflen);
870Sstevel@tonic-gate 
88*2830Sdjl 	names = __ns_ldap_getAttrStruct(result->entry,  _N_NAME);
89*2830Sdjl 	if (names == NULL || names->attrvalue == NULL) {
90*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
91*2830Sdjl 		goto result_net2str;
92*2830Sdjl 	}
93*2830Sdjl 	/* Get the canonical name */
94*2830Sdjl 	cname = __s_api_get_canonical_name(result->entry, names, 1);
95*2830Sdjl 	/*
96*2830Sdjl 	 * The definition of the object class  "ipNetwork" has a
97*2830Sdjl 	 * discrepency between RFC 2307 and 2307bis.
98*2830Sdjl 	 * In 2307, "cn" is a MUST attribute. In 2307bis, "cn" is a
99*2830Sdjl 	 * MAY attribute.
100*2830Sdjl 	 * If "cn" is a MAY attribute, it does not  appear in RDN and can't
101*2830Sdjl 	 * be derived from RDN as a canonical "cn" name. In that case, use 1st
102*2830Sdjl 	 * "cn" value as the official name.
103*2830Sdjl 	 */
104*2830Sdjl 	if (cname == NULL)
105*2830Sdjl 		/* 2307bis case */
106*2830Sdjl 		cname = names->attrvalue[0];
107*2830Sdjl 	if (cname == NULL || (len = strlen(cname)) < 1) {
108*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
109*2830Sdjl 		goto result_net2str;
110*2830Sdjl 	}
111*2830Sdjl 	network = __ns_ldap_getAttr(result->entry, _N_NETWORK);
112*2830Sdjl 	if (network == NULL || network[0] == NULL ||
113*2830Sdjl 			(len = strlen(network[0])) < 1) {
114*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
115*2830Sdjl 		goto result_net2str;
116*2830Sdjl 	}
117*2830Sdjl 	len = snprintf(buffer, buflen,  "%s %s", cname, network[0]);
118*2830Sdjl 	TEST_AND_ADJUST(len, buffer, buflen, result_net2str);
119*2830Sdjl 	/* Append aliases */
120*2830Sdjl 	for (i = 0; i < names->value_count; i++) {
121*2830Sdjl 		if (names->attrvalue[i] == NULL) {
122*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
123*2830Sdjl 			goto result_net2str;
124*2830Sdjl 		}
125*2830Sdjl 		/* Skip the canonical name */
126*2830Sdjl 		if (strcasecmp(names->attrvalue[i], cname) != 0) {
127*2830Sdjl 			len = snprintf(buffer, buflen,  " %s",
128*2830Sdjl 					names->attrvalue[i]);
129*2830Sdjl 			TEST_AND_ADJUST(len, buffer, buflen, result_net2str);
130*2830Sdjl 		}
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
133*2830Sdjl 	/* The front end marshaller doesn't need to copy trailing nulls */
134*2830Sdjl 	if (argp->buf.result != NULL)
135*2830Sdjl 		be->buflen = strlen(be->buffer);
1360Sstevel@tonic-gate 
137*2830Sdjl result_net2str:
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
140*2830Sdjl 	return (nss_result);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * Takes an unsigned integer in host order, and returns a printable
1450Sstevel@tonic-gate  * string for it as a network number.  To allow for the possibility of
1460Sstevel@tonic-gate  * naming subnets, only trailing dot-zeros are truncated.
147*2830Sdjl  * buf2 is untruncated version.
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate 
nettoa(int anet,char * buf,char * buf2,int buflen)150*2830Sdjl static int nettoa(int anet, char *buf, char *buf2, int buflen)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	int		addr;
1530Sstevel@tonic-gate 	char		*p;
1540Sstevel@tonic-gate 	struct in_addr	in;
1550Sstevel@tonic-gate 
156*2830Sdjl 	if (buf == NULL || buf2 == NULL)
1570Sstevel@tonic-gate 		return ((int)1);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	in = inet_makeaddr(anet, INADDR_ANY);
1600Sstevel@tonic-gate 	addr = in.s_addr;
161*2830Sdjl 	if (inet_ntop(AF_INET, (const void *)&in, buf2, INET_ADDRSTRLEN)
162*2830Sdjl 			== NULL)
163*2830Sdjl 		return ((int)1);
164*2830Sdjl 	if (strlcpy(buf, buf2, buflen) >= buflen)
1650Sstevel@tonic-gate 		return ((int)1);
1660Sstevel@tonic-gate 	if ((IN_CLASSA_HOST & htonl(addr)) == 0) {
1670Sstevel@tonic-gate 		p = strchr(buf, '.');
1680Sstevel@tonic-gate 		if (p == NULL)
1690Sstevel@tonic-gate 			return ((int)1);
1700Sstevel@tonic-gate 		*p = 0;
1710Sstevel@tonic-gate 	} else if ((IN_CLASSB_HOST & htonl(addr)) == 0) {
1720Sstevel@tonic-gate 		p = strchr(buf, '.');
1730Sstevel@tonic-gate 		if (p == NULL)
1740Sstevel@tonic-gate 			return ((int)1);
1750Sstevel@tonic-gate 		p = strchr(p + 1, '.');
1760Sstevel@tonic-gate 		if (p == NULL)
1770Sstevel@tonic-gate 			return ((int)1);
1780Sstevel@tonic-gate 		*p = 0;
1790Sstevel@tonic-gate 	} else if ((IN_CLASSC_HOST & htonl(addr)) == 0) {
1800Sstevel@tonic-gate 		p = strrchr(buf, '.');
1810Sstevel@tonic-gate 		if (p == NULL)
1820Sstevel@tonic-gate 			return ((int)1);
1830Sstevel@tonic-gate 		*p = 0;
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	return ((int)0);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate  * getbyname gets a network entry by name. This function constructs an
1920Sstevel@tonic-gate  * ldap search filter using the network name invocation parameter and the
1930Sstevel@tonic-gate  * getnetbyname search filter defined. Once the filter is constructed, we
1940Sstevel@tonic-gate  * search for a matching entry and marshal the data results into struct
1950Sstevel@tonic-gate  * netent for the frontend process. The function _nss_ldap_networks2ent
1960Sstevel@tonic-gate  * performs the data marshaling.
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)2000Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
2030Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2040Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
2050Sstevel@tonic-gate 	char		netname[SEARCHFILTERLEN];
2060Sstevel@tonic-gate 	int		ret;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (_ldap_filter_name(netname, argp->key.name, sizeof (netname)) != 0)
2090Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
2120Sstevel@tonic-gate 	    _F_GETNETBYNAME, netname);
2130Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
2140Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
2170Sstevel@tonic-gate 	    _F_GETNETBYNAME_SSD, netname);
2180Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
2190Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
2220Sstevel@tonic-gate 		_NETWORKS, searchfilter, NULL,
2230Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /*
2280Sstevel@tonic-gate  * getbyaddr gets a network entry by ip address. This function constructs an
2290Sstevel@tonic-gate  * ldap search filter using the name invocation parameter and the getnetbyaddr
2300Sstevel@tonic-gate  * search filter defined. Once the filter is constructed, we search for a
2310Sstevel@tonic-gate  * matching entry and marshal the data results into struct netent for the
2320Sstevel@tonic-gate  * frontend process. The function _nss_ldap_networks2ent performs the data
2330Sstevel@tonic-gate  * marshaling.
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static nss_status_t
getbyaddr(ldap_backend_ptr be,void * a)2370Sstevel@tonic-gate getbyaddr(ldap_backend_ptr be, void *a)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
240*2830Sdjl 	char		addrstr[INET_ADDRSTRLEN], addrstr2[INET_ADDRSTRLEN];
2410Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2420Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
2430Sstevel@tonic-gate 	int		ret;
2440Sstevel@tonic-gate 
245*2830Sdjl 	if (nettoa((int)argp->key.netaddr.net, addrstr, addrstr2,
246*2830Sdjl 				INET_ADDRSTRLEN) != 0)
2470Sstevel@tonic-gate 		return ((nss_status_t)NSS_UNAVAIL);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
250*2830Sdjl 	    _F_GETNETBYADDR, addrstr, addrstr2);
2510Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
2520Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
255*2830Sdjl 	    _F_GETNETBYADDR_SSD, addrstr, addrstr2);
2560Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
2570Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
2600Sstevel@tonic-gate 		_NETWORKS, searchfilter, NULL,
2610Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate static ldap_backend_op_t net_ops[] = {
2650Sstevel@tonic-gate 	_nss_ldap_destr,
2660Sstevel@tonic-gate 	_nss_ldap_endent,
2670Sstevel@tonic-gate 	_nss_ldap_setent,
2680Sstevel@tonic-gate 	_nss_ldap_getent,
2690Sstevel@tonic-gate 	getbyname,
2700Sstevel@tonic-gate 	getbyaddr
2710Sstevel@tonic-gate };
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate  * _nss_ldap_networks_constr is where life begins. This function calls the
2760Sstevel@tonic-gate  * generic ldap constructor function to define and build the abstract data
2770Sstevel@tonic-gate  * types required to support ldap operations.
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*ARGSUSED0*/
2810Sstevel@tonic-gate nss_backend_t *
_nss_ldap_networks_constr(const char * dummy1,const char * dummy2,const char * dummy3)2820Sstevel@tonic-gate _nss_ldap_networks_constr(const char *dummy1, const char *dummy2,
2830Sstevel@tonic-gate 			const char *dummy3)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(net_ops,
2870Sstevel@tonic-gate 		sizeof (net_ops)/sizeof (net_ops[0]), _NETWORKS,
288*2830Sdjl 		networks_attrs, _nss_ldap_networks2str));
2890Sstevel@tonic-gate }
290