xref: /titanic_52/usr/src/lib/nsswitch/ldap/common/getnetmasks.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 "ldap_common.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /* netmasks attributes filters */
357c478bd9Sstevel@tonic-gate #define	_N_NETWORK	"ipnetworknumber"
367c478bd9Sstevel@tonic-gate #define	_N_NETMASK	"ipnetmasknumber"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	_F_GETMASKBYNET	"(&(objectClass=ipNetwork)(ipNetworkNumber=%s))"
397c478bd9Sstevel@tonic-gate #define	_F_GETMASKBYNET_SSD	"(&(%%s)(ipNetworkNumber=%s))"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static const char *netmasks_attrs[] = {
427c478bd9Sstevel@tonic-gate 	_N_NETWORK,
437c478bd9Sstevel@tonic-gate 	_N_NETMASK,
447c478bd9Sstevel@tonic-gate 	(char *)NULL
457c478bd9Sstevel@tonic-gate };
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
49*cb5caa98Sdjl  * _nss_ldap_netmasks2str is the data marshaling method for the netmasks
50*cb5caa98Sdjl  * getXbyY * (e.g., getnetmaskby[net|addr]()) backend processes.
51*cb5caa98Sdjl  * This method is called after a successful ldap search has been performed.
52*cb5caa98Sdjl  * This method will parse the ldap search values into the file format.
53*cb5caa98Sdjl  *
54*cb5caa98Sdjl  * getnetmaskbykey set argp->buf.buffer to NULL and argp->buf.buflen to 0
55*cb5caa98Sdjl  * and argp->buf.result to non-NULL.
56*cb5caa98Sdjl  * The front end marshaller str2add expects "netmask" only
57*cb5caa98Sdjl  *
58*cb5caa98Sdjl  * e.g.
59*cb5caa98Sdjl  *
60*cb5caa98Sdjl  * 255.255.255.0
61*cb5caa98Sdjl  *
62*cb5caa98Sdjl  *
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static int
66*cb5caa98Sdjl _nss_ldap_netmasks2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
677c478bd9Sstevel@tonic-gate {
68*cb5caa98Sdjl 	int		nss_result, len;
697c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
70*cb5caa98Sdjl 	char		*buffer, **netmask;
717c478bd9Sstevel@tonic-gate 
72*cb5caa98Sdjl 	if (result == NULL)
73*cb5caa98Sdjl 		return (NSS_STR_PARSE_PARSE);
747c478bd9Sstevel@tonic-gate 
75*cb5caa98Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
767c478bd9Sstevel@tonic-gate 
77*cb5caa98Sdjl 	netmask = __ns_ldap_getAttr(result->entry, _N_NETMASK);
78*cb5caa98Sdjl 	if (netmask == NULL || netmask[0] == NULL ||
79*cb5caa98Sdjl 				(strlen(netmask[0]) < 1)) {
80*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
81*cb5caa98Sdjl 			goto result_nmks2str;
827c478bd9Sstevel@tonic-gate 	}
83*cb5caa98Sdjl 	/* Add a trailing null for debugging purpose */
84*cb5caa98Sdjl 	len = strlen(netmask[0]) + 1;
85*cb5caa98Sdjl 	if (argp->buf.result != NULL) {
86*cb5caa98Sdjl 		if ((be->buffer = calloc(1, len)) == NULL) {
87*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
88*cb5caa98Sdjl 			goto result_nmks2str;
897c478bd9Sstevel@tonic-gate 		}
90*cb5caa98Sdjl 		be->buflen = len - 1;
91*cb5caa98Sdjl 		buffer = be->buffer;
92*cb5caa98Sdjl 	} else
93*cb5caa98Sdjl 		buffer = argp->buf.buffer;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 
96*cb5caa98Sdjl 	(void) snprintf(buffer, len, "%s", netmask[0]);
97*cb5caa98Sdjl 
98*cb5caa98Sdjl result_nmks2str:
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
1017c478bd9Sstevel@tonic-gate 	return ((int)nss_result);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * getbynet gets a network mask by address. This function constructs an
1067c478bd9Sstevel@tonic-gate  * ldap search filter using the netmask name invocation parameter and the
1077c478bd9Sstevel@tonic-gate  * getmaskbynet search filter defined. Once the filter is constructed, we
1087c478bd9Sstevel@tonic-gate  * search for a matching entry and marshal the data results into struct
1097c478bd9Sstevel@tonic-gate  * in_addr for the frontend process. The function _nss_ldap_netmasks2ent
1107c478bd9Sstevel@tonic-gate  * performs the data marshaling.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static nss_status_t
1147c478bd9Sstevel@tonic-gate getbynet(ldap_backend_ptr be, void *a)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1177c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1187c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1197c478bd9Sstevel@tonic-gate 	char		netnumber[SEARCHFILTERLEN];
1207c478bd9Sstevel@tonic-gate 	int		ret;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (_ldap_filter_name(netnumber, argp->key.name, sizeof (netnumber))
1237c478bd9Sstevel@tonic-gate 			!= 0)
1247c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1257c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
1267c478bd9Sstevel@tonic-gate 	    _F_GETMASKBYNET, netnumber);
1277c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
1287c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
1317c478bd9Sstevel@tonic-gate 	    _F_GETMASKBYNET_SSD, netnumber);
1327c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
1337c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
1367c478bd9Sstevel@tonic-gate 		_NETMASKS, searchfilter, NULL,
1377c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static ldap_backend_op_t netmasks_ops[] = {
1427c478bd9Sstevel@tonic-gate 	_nss_ldap_destr,
1437c478bd9Sstevel@tonic-gate 	getbynet
1447c478bd9Sstevel@tonic-gate };
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * _nss_ldap_netmasks_constr is where life begins. This function calls
1497c478bd9Sstevel@tonic-gate  * the generic ldap constructor function to define and build the abstract
1507c478bd9Sstevel@tonic-gate  * data types required to support ldap operations.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
1547c478bd9Sstevel@tonic-gate nss_backend_t *
1557c478bd9Sstevel@tonic-gate _nss_ldap_netmasks_constr(const char *dummy1, const char *dummy2,
1567c478bd9Sstevel@tonic-gate 			const char *dummy3)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(netmasks_ops,
1607c478bd9Sstevel@tonic-gate 		sizeof (netmasks_ops)/sizeof (netmasks_ops[0]), _NETMASKS,
161*cb5caa98Sdjl 		netmasks_attrs, _nss_ldap_netmasks2str));
1627c478bd9Sstevel@tonic-gate }
163