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