xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/getnetmasks.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 "ldap_common.h"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /* netmasks attributes filters */
350Sstevel@tonic-gate #define	_N_NETWORK	"ipnetworknumber"
360Sstevel@tonic-gate #define	_N_NETMASK	"ipnetmasknumber"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	_F_GETMASKBYNET	"(&(objectClass=ipNetwork)(ipNetworkNumber=%s))"
390Sstevel@tonic-gate #define	_F_GETMASKBYNET_SSD	"(&(%%s)(ipNetworkNumber=%s))"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static const char *netmasks_attrs[] = {
420Sstevel@tonic-gate 	_N_NETWORK,
430Sstevel@tonic-gate 	_N_NETMASK,
440Sstevel@tonic-gate 	(char *)NULL
450Sstevel@tonic-gate };
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
49*2830Sdjl  * _nss_ldap_netmasks2str is the data marshaling method for the netmasks
50*2830Sdjl  * getXbyY * (e.g., getnetmaskby[net|addr]()) backend processes.
51*2830Sdjl  * This method is called after a successful ldap search has been performed.
52*2830Sdjl  * This method will parse the ldap search values into the file format.
53*2830Sdjl  *
54*2830Sdjl  * getnetmaskbykey set argp->buf.buffer to NULL and argp->buf.buflen to 0
55*2830Sdjl  * and argp->buf.result to non-NULL.
56*2830Sdjl  * The front end marshaller str2add expects "netmask" only
57*2830Sdjl  *
58*2830Sdjl  * e.g.
59*2830Sdjl  *
60*2830Sdjl  * 255.255.255.0
61*2830Sdjl  *
62*2830Sdjl  *
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static int
_nss_ldap_netmasks2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)66*2830Sdjl _nss_ldap_netmasks2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
670Sstevel@tonic-gate {
68*2830Sdjl 	int		nss_result, len;
690Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
70*2830Sdjl 	char		*buffer, **netmask;
71*2830Sdjl 
72*2830Sdjl 	if (result == NULL)
73*2830Sdjl 		return (NSS_STR_PARSE_PARSE);
74*2830Sdjl 
75*2830Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
760Sstevel@tonic-gate 
77*2830Sdjl 	netmask = __ns_ldap_getAttr(result->entry, _N_NETMASK);
78*2830Sdjl 	if (netmask == NULL || netmask[0] == NULL ||
79*2830Sdjl 				(strlen(netmask[0]) < 1)) {
80*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
81*2830Sdjl 			goto result_nmks2str;
820Sstevel@tonic-gate 	}
83*2830Sdjl 	/* Add a trailing null for debugging purpose */
84*2830Sdjl 	len = strlen(netmask[0]) + 1;
85*2830Sdjl 	if (argp->buf.result != NULL) {
86*2830Sdjl 		if ((be->buffer = calloc(1, len)) == NULL) {
87*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
88*2830Sdjl 			goto result_nmks2str;
890Sstevel@tonic-gate 		}
90*2830Sdjl 		be->buflen = len - 1;
91*2830Sdjl 		buffer = be->buffer;
92*2830Sdjl 	} else
93*2830Sdjl 		buffer = argp->buf.buffer;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 
96*2830Sdjl 	(void) snprintf(buffer, len, "%s", netmask[0]);
97*2830Sdjl 
98*2830Sdjl result_nmks2str:
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
1010Sstevel@tonic-gate 	return ((int)nss_result);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * getbynet gets a network mask by address. This function constructs an
1060Sstevel@tonic-gate  * ldap search filter using the netmask name invocation parameter and the
1070Sstevel@tonic-gate  * getmaskbynet search filter defined. Once the filter is constructed, we
1080Sstevel@tonic-gate  * search for a matching entry and marshal the data results into struct
1090Sstevel@tonic-gate  * in_addr for the frontend process. The function _nss_ldap_netmasks2ent
1100Sstevel@tonic-gate  * performs the data marshaling.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static nss_status_t
getbynet(ldap_backend_ptr be,void * a)1140Sstevel@tonic-gate getbynet(ldap_backend_ptr be, void *a)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1170Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1180Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1190Sstevel@tonic-gate 	char		netnumber[SEARCHFILTERLEN];
1200Sstevel@tonic-gate 	int		ret;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if (_ldap_filter_name(netnumber, argp->key.name, sizeof (netnumber))
1230Sstevel@tonic-gate 			!= 0)
1240Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1250Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
1260Sstevel@tonic-gate 	    _F_GETMASKBYNET, netnumber);
1270Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
1280Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
1310Sstevel@tonic-gate 	    _F_GETMASKBYNET_SSD, netnumber);
1320Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
1330Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
1360Sstevel@tonic-gate 		_NETMASKS, searchfilter, NULL,
1370Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate static ldap_backend_op_t netmasks_ops[] = {
1420Sstevel@tonic-gate 	_nss_ldap_destr,
1430Sstevel@tonic-gate 	getbynet
1440Sstevel@tonic-gate };
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * _nss_ldap_netmasks_constr is where life begins. This function calls
1490Sstevel@tonic-gate  * the generic ldap constructor function to define and build the abstract
1500Sstevel@tonic-gate  * data types required to support ldap operations.
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*ARGSUSED0*/
1540Sstevel@tonic-gate nss_backend_t *
_nss_ldap_netmasks_constr(const char * dummy1,const char * dummy2,const char * dummy3)1550Sstevel@tonic-gate _nss_ldap_netmasks_constr(const char *dummy1, const char *dummy2,
1560Sstevel@tonic-gate 			const char *dummy3)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(netmasks_ops,
1600Sstevel@tonic-gate 		sizeof (netmasks_ops)/sizeof (netmasks_ops[0]), _NETMASKS,
161*2830Sdjl 		netmasks_attrs, _nss_ldap_netmasks2str));
1620Sstevel@tonic-gate }
163