xref: /illumos-gate/usr/src/lib/nsswitch/ldap/common/getnetent.c (revision 42b53e0fbc5c05289c3d334bb864b784fafe5ce4)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <netdb.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <sys/socket.h>
30 #include "ns_internal.h"
31 #include "ldap_common.h"
32 
33 /* networks attributes filters */
34 #define	_N_NAME		"cn"
35 #define	_N_NETWORK	"ipnetworknumber"
36 #define	_F_GETNETBYNAME	"(&(objectClass=ipNetwork)(cn=%s))"
37 #define	_F_GETNETBYNAME_SSD	"(&(%%s)(cn=%s))"
38 #define	_F_GETNETBYADDR	"(&(objectClass=ipNetwork)(|(ipNetworkNumber=%s)" \
39 						"(ipNetworkNumber=%s)))"
40 #define	_F_GETNETBYADDR_SSD	"(&(%%s)(|(ipNetworkNumber=%s)" \
41 						"(ipNetworkNumber=%s)))"
42 
43 static const char *networks_attrs[] = {
44 	_N_NAME,
45 	_N_NETWORK,
46 	(char *)NULL
47 };
48 
49 /*
50  * _nss_ldap_networks2str is the data marshaling method for the networks
51  * getXbyY * (e.g., getbyname(), getbyaddr(), getnetent() backend processes.
52  * This method is called after a successful ldap search has been performed.
53  * This method will parse the ldap search values into the file format.
54  * e.g.
55  *
56  * SunRay-ce2	10.34.96.0	SunRay
57  *
58  */
59 static int
60 _nss_ldap_networks2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
61 {
62 	uint_t		i;
63 	int		nss_result;
64 	int		buflen = 0, len;
65 	char		**network, *cname = NULL;
66 	char		*buffer = NULL;
67 	ns_ldap_result_t	*result = be->result;
68 	ns_ldap_attr_t	*names;
69 
70 	if (result == NULL)
71 		return (NSS_STR_PARSE_PARSE);
72 	buflen = argp->buf.buflen;
73 
74 	if (argp->buf.result != NULL) {
75 		if ((be->buffer = calloc(1, buflen)) == NULL) {
76 			nss_result = NSS_STR_PARSE_PARSE;
77 			goto result_net2str;
78 		}
79 		buffer = be->buffer;
80 	} else
81 		buffer = argp->buf.buffer;
82 
83 	nss_result = NSS_STR_PARSE_SUCCESS;
84 	(void) memset(argp->buf.buffer, 0, buflen);
85 
86 	names = __ns_ldap_getAttrStruct(result->entry,  _N_NAME);
87 	if (names == NULL || names->attrvalue == NULL) {
88 		nss_result = NSS_STR_PARSE_PARSE;
89 		goto result_net2str;
90 	}
91 	/* Get the canonical name */
92 	cname = __s_api_get_canonical_name(result->entry, names, 1);
93 	/*
94 	 * The definition of the object class  "ipNetwork" has a
95 	 * discrepency between RFC 2307 and 2307bis.
96 	 * In 2307, "cn" is a MUST attribute. In 2307bis, "cn" is a
97 	 * MAY attribute.
98 	 * If "cn" is a MAY attribute, it does not  appear in RDN and can't
99 	 * be derived from RDN as a canonical "cn" name. In that case, use 1st
100 	 * "cn" value as the official name.
101 	 */
102 	if (cname == NULL)
103 		/* 2307bis case */
104 		cname = names->attrvalue[0];
105 	if (cname == NULL || (len = strlen(cname)) < 1) {
106 		nss_result = NSS_STR_PARSE_PARSE;
107 		goto result_net2str;
108 	}
109 	network = __ns_ldap_getAttr(result->entry, _N_NETWORK);
110 	if (network == NULL || network[0] == NULL ||
111 			(len = strlen(network[0])) < 1) {
112 		nss_result = NSS_STR_PARSE_PARSE;
113 		goto result_net2str;
114 	}
115 	len = snprintf(buffer, buflen,  "%s %s", cname, network[0]);
116 	TEST_AND_ADJUST(len, buffer, buflen, result_net2str);
117 	/* Append aliases */
118 	for (i = 0; i < names->value_count; i++) {
119 		if (names->attrvalue[i] == NULL) {
120 			nss_result = NSS_STR_PARSE_PARSE;
121 			goto result_net2str;
122 		}
123 		/* Skip the canonical name */
124 		if (strcasecmp(names->attrvalue[i], cname) != 0) {
125 			len = snprintf(buffer, buflen,  " %s",
126 					names->attrvalue[i]);
127 			TEST_AND_ADJUST(len, buffer, buflen, result_net2str);
128 		}
129 	}
130 
131 	/* The front end marshaller doesn't need to copy trailing nulls */
132 	if (argp->buf.result != NULL)
133 		be->buflen = strlen(be->buffer);
134 
135 result_net2str:
136 
137 	(void) __ns_ldap_freeResult(&be->result);
138 	return (nss_result);
139 }
140 
141 /*
142  * Takes an unsigned integer in host order, and returns a printable
143  * string for it as a network number.  To allow for the possibility of
144  * naming subnets, only trailing dot-zeros are truncated.
145  * buf2 is untruncated version.
146  */
147 
148 static int nettoa(int anet, char *buf, char *buf2, int buflen)
149 {
150 	int		addr;
151 	char		*p;
152 	struct in_addr	in;
153 
154 	if (buf == NULL || buf2 == NULL)
155 		return ((int)1);
156 
157 	in = inet_makeaddr(anet, INADDR_ANY);
158 	addr = in.s_addr;
159 	if (inet_ntop(AF_INET, (const void *)&in, buf2, INET_ADDRSTRLEN)
160 			== NULL)
161 		return ((int)1);
162 	if (strlcpy(buf, buf2, buflen) >= buflen)
163 		return ((int)1);
164 	if ((IN_CLASSA_HOST & htonl(addr)) == 0) {
165 		p = strchr(buf, '.');
166 		if (p == NULL)
167 			return ((int)1);
168 		*p = 0;
169 	} else if ((IN_CLASSB_HOST & htonl(addr)) == 0) {
170 		p = strchr(buf, '.');
171 		if (p == NULL)
172 			return ((int)1);
173 		p = strchr(p + 1, '.');
174 		if (p == NULL)
175 			return ((int)1);
176 		*p = 0;
177 	} else if ((IN_CLASSC_HOST & htonl(addr)) == 0) {
178 		p = strrchr(buf, '.');
179 		if (p == NULL)
180 			return ((int)1);
181 		*p = 0;
182 	}
183 
184 	return ((int)0);
185 }
186 
187 
188 /*
189  * getbyname gets a network entry by name. This function constructs an
190  * ldap search filter using the network name invocation parameter and the
191  * getnetbyname search filter defined. Once the filter is constructed, we
192  * search for a matching entry and marshal the data results into struct
193  * netent for the frontend process. The function _nss_ldap_networks2ent
194  * performs the data marshaling.
195  */
196 
197 static nss_status_t
198 getbyname(ldap_backend_ptr be, void *a)
199 {
200 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
201 	char		searchfilter[SEARCHFILTERLEN];
202 	char		userdata[SEARCHFILTERLEN];
203 	char		netname[SEARCHFILTERLEN];
204 	int		ret;
205 
206 	if (_ldap_filter_name(netname, argp->key.name, sizeof (netname)) != 0)
207 		return ((nss_status_t)NSS_NOTFOUND);
208 
209 	ret = snprintf(searchfilter, sizeof (searchfilter),
210 	    _F_GETNETBYNAME, netname);
211 	if (ret >= sizeof (searchfilter) || ret < 0)
212 		return ((nss_status_t)NSS_NOTFOUND);
213 
214 	ret = snprintf(userdata, sizeof (userdata),
215 	    _F_GETNETBYNAME_SSD, netname);
216 	if (ret >= sizeof (userdata) || ret < 0)
217 		return ((nss_status_t)NSS_NOTFOUND);
218 
219 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
220 		_NETWORKS, searchfilter, NULL,
221 		_merge_SSD_filter, userdata));
222 }
223 
224 
225 /*
226  * getbyaddr gets a network entry by ip address. This function constructs an
227  * ldap search filter using the name invocation parameter and the getnetbyaddr
228  * search filter defined. Once the filter is constructed, we search for a
229  * matching entry and marshal the data results into struct netent for the
230  * frontend process. The function _nss_ldap_networks2ent performs the data
231  * marshaling.
232  */
233 
234 static nss_status_t
235 getbyaddr(ldap_backend_ptr be, void *a)
236 {
237 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
238 	char		addrstr[INET_ADDRSTRLEN], addrstr2[INET_ADDRSTRLEN];
239 	char		searchfilter[SEARCHFILTERLEN];
240 	char		userdata[SEARCHFILTERLEN];
241 	int		ret;
242 
243 	if (nettoa((int)argp->key.netaddr.net, addrstr, addrstr2,
244 				INET_ADDRSTRLEN) != 0)
245 		return ((nss_status_t)NSS_UNAVAIL);
246 
247 	ret = snprintf(searchfilter, sizeof (searchfilter),
248 	    _F_GETNETBYADDR, addrstr, addrstr2);
249 	if (ret >= sizeof (searchfilter) || ret < 0)
250 		return ((nss_status_t)NSS_NOTFOUND);
251 
252 	ret = snprintf(userdata, sizeof (userdata),
253 	    _F_GETNETBYADDR_SSD, addrstr, addrstr2);
254 	if (ret >= sizeof (userdata) || ret < 0)
255 		return ((nss_status_t)NSS_NOTFOUND);
256 
257 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
258 		_NETWORKS, searchfilter, NULL,
259 		_merge_SSD_filter, userdata));
260 }
261 
262 static ldap_backend_op_t net_ops[] = {
263 	_nss_ldap_destr,
264 	_nss_ldap_endent,
265 	_nss_ldap_setent,
266 	_nss_ldap_getent,
267 	getbyname,
268 	getbyaddr
269 };
270 
271 
272 /*
273  * _nss_ldap_networks_constr is where life begins. This function calls the
274  * generic ldap constructor function to define and build the abstract data
275  * types required to support ldap operations.
276  */
277 
278 /*ARGSUSED0*/
279 nss_backend_t *
280 _nss_ldap_networks_constr(const char *dummy1, const char *dummy2,
281 			const char *dummy3)
282 {
283 
284 	return ((nss_backend_t *)_nss_ldap_constr(net_ops,
285 		sizeof (net_ops)/sizeof (net_ops[0]), _NETWORKS,
286 		networks_attrs, _nss_ldap_networks2str));
287 }
288