xref: /illumos-gate/usr/src/lib/nsswitch/ldap/common/getnetmasks.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <netdb.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <sys/socket.h>
32 #include "ldap_common.h"
33 
34 /* netmasks attributes filters */
35 #define	_N_NETWORK	"ipnetworknumber"
36 #define	_N_NETMASK	"ipnetmasknumber"
37 
38 #define	_F_GETMASKBYNET	"(&(objectClass=ipNetwork)(ipNetworkNumber=%s))"
39 #define	_F_GETMASKBYNET_SSD	"(&(%%s)(ipNetworkNumber=%s))"
40 
41 static const char *netmasks_attrs[] = {
42 	_N_NETWORK,
43 	_N_NETMASK,
44 	(char *)NULL
45 };
46 
47 
48 /*
49  * _nss_ldap_netmasks2str is the data marshaling method for the netmasks
50  * getXbyY * (e.g., getnetmaskby[net|addr]()) backend processes.
51  * This method is called after a successful ldap search has been performed.
52  * This method will parse the ldap search values into the file format.
53  *
54  * getnetmaskbykey set argp->buf.buffer to NULL and argp->buf.buflen to 0
55  * and argp->buf.result to non-NULL.
56  * The front end marshaller str2add expects "netmask" only
57  *
58  * e.g.
59  *
60  * 255.255.255.0
61  *
62  *
63  */
64 
65 static int
66 _nss_ldap_netmasks2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
67 {
68 	int		nss_result, len;
69 	ns_ldap_result_t	*result = be->result;
70 	char		*buffer, **netmask;
71 
72 	if (result == NULL)
73 		return (NSS_STR_PARSE_PARSE);
74 
75 	nss_result = NSS_STR_PARSE_SUCCESS;
76 
77 	netmask = __ns_ldap_getAttr(result->entry, _N_NETMASK);
78 	if (netmask == NULL || netmask[0] == NULL ||
79 				(strlen(netmask[0]) < 1)) {
80 			nss_result = NSS_STR_PARSE_PARSE;
81 			goto result_nmks2str;
82 	}
83 	/* Add a trailing null for debugging purpose */
84 	len = strlen(netmask[0]) + 1;
85 	if (argp->buf.result != NULL) {
86 		if ((be->buffer = calloc(1, len)) == NULL) {
87 			nss_result = NSS_STR_PARSE_PARSE;
88 			goto result_nmks2str;
89 		}
90 		be->buflen = len - 1;
91 		buffer = be->buffer;
92 	} else
93 		buffer = argp->buf.buffer;
94 
95 
96 	(void) snprintf(buffer, len, "%s", netmask[0]);
97 
98 result_nmks2str:
99 
100 	(void) __ns_ldap_freeResult(&be->result);
101 	return ((int)nss_result);
102 }
103 
104 /*
105  * getbynet gets a network mask by address. This function constructs an
106  * ldap search filter using the netmask name invocation parameter and the
107  * getmaskbynet search filter defined. Once the filter is constructed, we
108  * search for a matching entry and marshal the data results into struct
109  * in_addr for the frontend process. The function _nss_ldap_netmasks2ent
110  * performs the data marshaling.
111  */
112 
113 static nss_status_t
114 getbynet(ldap_backend_ptr be, void *a)
115 {
116 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
117 	char		searchfilter[SEARCHFILTERLEN];
118 	char		userdata[SEARCHFILTERLEN];
119 	char		netnumber[SEARCHFILTERLEN];
120 	int		ret;
121 
122 	if (_ldap_filter_name(netnumber, argp->key.name, sizeof (netnumber))
123 			!= 0)
124 		return ((nss_status_t)NSS_NOTFOUND);
125 	ret = snprintf(searchfilter, sizeof (searchfilter),
126 	    _F_GETMASKBYNET, netnumber);
127 	if (ret >= sizeof (searchfilter) || ret < 0)
128 		return ((nss_status_t)NSS_NOTFOUND);
129 
130 	ret = snprintf(userdata, sizeof (userdata),
131 	    _F_GETMASKBYNET_SSD, netnumber);
132 	if (ret >= sizeof (userdata) || ret < 0)
133 		return ((nss_status_t)NSS_NOTFOUND);
134 
135 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
136 		_NETMASKS, searchfilter, NULL,
137 		_merge_SSD_filter, userdata));
138 }
139 
140 
141 static ldap_backend_op_t netmasks_ops[] = {
142 	_nss_ldap_destr,
143 	getbynet
144 };
145 
146 
147 /*
148  * _nss_ldap_netmasks_constr is where life begins. This function calls
149  * the generic ldap constructor function to define and build the abstract
150  * data types required to support ldap operations.
151  */
152 
153 /*ARGSUSED0*/
154 nss_backend_t *
155 _nss_ldap_netmasks_constr(const char *dummy1, const char *dummy2,
156 			const char *dummy3)
157 {
158 
159 	return ((nss_backend_t *)_nss_ldap_constr(netmasks_ops,
160 		sizeof (netmasks_ops)/sizeof (netmasks_ops[0]), _NETMASKS,
161 		netmasks_attrs, _nss_ldap_netmasks2str));
162 }
163