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