1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
#include <sys/systeminfo.h>
#include "ldap_common.h"
#ifdef DEBUG
/*
* Debugging routine for printing the value of a result
* structure
*/
int
printresult(ns_ldap_result_t *result)
{
int i, j, k;
ns_ldap_entry_t *curEntry;
printf("--------------------------------------\n");
printf("entries_count %d\n", result->entries_count);
curEntry = result->entry;
for (i = 0; i < result->entries_count; i++) {
printf("entry %d has attr_count = %d \n",
i, curEntry->attr_count);
for (j = 0; j < curEntry->attr_count; j++) {
printf("entry %d has attr_pair[%d] = %s \n",
i, j, curEntry->attr_pair[j]->attrname);
for (k = 0;
(k < curEntry->attr_pair[j]->value_count) &&
(curEntry->attr_pair[j]->attrvalue[k]);
k++)
printf("entry %d has "
"attr_pair[%d]->attrvalue[%d] = %s \n",
i, j, k,
curEntry->attr_pair[j]->attrvalue[k]);
}
printf("\n--------------------------------------\n");
curEntry = curEntry->next;
}
return (1);
}
#endif
/*
*
*/
ns_ldap_attr_t *
getattr(ns_ldap_result_t *result, int i)
{
ns_ldap_entry_t *entry;
#ifdef DEBUG
(void) fprintf(stdout, "\n[ldap_utils.c: getattr]\n");
#endif /* DEBUG */
if (result != NULL) {
entry = result->entry;
} else {
return (NULL);
}
if (result->entries_count == 0) {
return (NULL);
} else {
return (entry->attr_pair[i]);
}
}
/*
* _get_domain_name() passes the dn one level up from cdn, e.g.,
* a pointer pointing to "ou= ..." for the cdn's listed below:
* dn: cn=hostname+ipHostNumber="109.34.54.76", ou= ...
* dn: echo+IpServiceProtocol=udp, ou= ...
* to __ns_ldap_dn2domain() to retrieve the domain name associated
* with cdn.
*/
char *
_get_domain_name(char *cdn)
{
char **rdns;
char *pdn, *domain = NULL;
int nrdns;
int len = 0;
const ns_cred_t *cred = NULL;
ns_ldap_error_t *error;
/* break the cdn into its components */
rdns = ldap_explode_dn(cdn, 0);
if (rdns == NULL || *rdns == NULL)
return (NULL);
/* construct parent dn */
for (nrdns = 1; rdns[nrdns]; nrdns++)
len += strlen(rdns[nrdns]) + 1;
if (len == 0)
len = strlen(rdns[0]);
pdn = (char *)malloc(len + 1);
if (pdn == NULL) {
ldap_value_free(rdns);
return (NULL);
}
*pdn = '\0';
if (nrdns == 1)
(void) strcat(pdn, rdns[0]);
else {
for (nrdns = 1; rdns[nrdns]; nrdns++) {
(void) strcat(pdn, rdns[nrdns]);
(void) strcat(pdn, ",");
}
/* remove the last ',' */
pdn[strlen(pdn) - 1] = '\0';
}
/* get domain name */
(void) __ns_ldap_dn2domain(pdn, &domain, cred, &error);
ldap_value_free(rdns);
free(pdn);
return (domain);
}
/*
* "109.34.54.76" -> 109.34.54.76
*/
const char *
_strip_quotes(char *ipaddress)
{
char *cp = (char *)NULL;
/* look for first " */
if ((cp = strchr(ipaddress, '"')) == NULL)
return ((char *)ipaddress);
ipaddress++;
/* look for last " */
if ((cp = strchr(ipaddress, '"')) == NULL)
return ((char *)ipaddress);
*cp++ = '\0';
return (ipaddress);
}
/*
* This is a copy of a routine in libnsl/nss/netdir_inet.c. It is
* here because /etc/lib/nss_ldap.so.1 cannot call routines in
* libnsl. Care should be taken to keep the two copies in sync.
*/
int
__nss2herrno(nss_status_t nsstat)
{
switch (nsstat) {
case NSS_SUCCESS:
return (0);
case NSS_NOTFOUND:
return (HOST_NOT_FOUND);
case NSS_TRYAGAIN:
return (TRY_AGAIN);
case NSS_UNAVAIL:
default: /* keep gcc happy */
return (NO_RECOVERY);
}
/* NOTREACHED */
}
/*
* This is a generic filter call back function for
* merging the filter from service search descriptor with
* an existing search filter. This routine expects userdata
* contain a format string with a single %s in it, and will
* use the format string with sprintf() to insert the SSD filter.
*
* This routine is passed to the __ns_ldap_list() or
* __ns_ldap_firstEntry() APIs as the filter call back
* together with the userdata. For example,
* the gethostbyname processing may call __ns_ldap_list() with
* "(&(objectClass=ipHost)(cn=sys1))" as filter, this function
* as the filter call back, and "(&(%s)(cn=sys1))" as the
* userdata, this routine will in turn gets call to produce
* "(&(department=sds)(cn=sys1))" as the real search
* filter, if the input SSD contains a filter "department=sds".
*/
int
_merge_SSD_filter(const ns_ldap_search_desc_t *desc,
char **realfilter,
const void *userdata)
{
int len;
char *checker;
#ifdef DEBUG
(void) fprintf(stdout, "\n[ldap_utils.c: _merge_SSD_filter]\n");
#endif /* DEBUG */
/* sanity check */
if (realfilter == NULL)
return (NS_LDAP_INVALID_PARAM);
*realfilter = NULL;
if (desc == NULL || desc->filter == NULL || userdata == NULL)
return (NS_LDAP_INVALID_PARAM);
/* Parameter check. We only want one %s here, otherwise bail. */
len = 0; /* Reuse 'len' as "Number of %s hits"... */
checker = (char *)userdata;
do {
checker = strchr(checker, '%');
if (checker != NULL) {
if (len > 0 || *(checker + 1) != 's')
return (NS_LDAP_INVALID_PARAM);
len++; /* Got our %s. */
checker += 2;
} else if (len != 1)
return (NS_LDAP_INVALID_PARAM);
} while (checker != NULL);
#ifdef DEBUG
(void) fprintf(stdout, "\n[userdata: %s]\n", (char *)userdata);
(void) fprintf(stdout, "\n[SSD filter: %s]\n", desc->filter);
#endif /* DEBUG */
len = strlen(userdata) + strlen(desc->filter) + 1;
*realfilter = (char *)malloc(len);
if (*realfilter == NULL)
return (NS_LDAP_MEMORY);
(void) sprintf(*realfilter, (char *)userdata, desc->filter);
#ifdef DEBUG
(void) fprintf(stdout, "\n[new filter: %s]\n", *realfilter);
#endif /* DEBUG */
return (NS_LDAP_SUCCESS);
}
static char
hex_char(int n)
{
return ("0123456789abcdef"[n & 0xf]);
}
int
_ldap_filter_name(char *filter_name, const char *name, int filter_name_size)
{
char *end = filter_name + filter_name_size;
char c;
for (; *name; name++) {
c = *name;
switch (c) {
case '*':
case '(':
case ')':
case '\\':
if (end <= filter_name + 3)
return (-1);
*filter_name++ = '\\';
*filter_name++ = hex_char(c >> 4);
*filter_name++ = hex_char(c & 0xf);
break;
default:
if (end <= filter_name + 1)
return (-1);
*filter_name++ = c;
break;
}
}
if (end <= filter_name)
return (-1);
*filter_name = '\0';
return (0);
}
|