4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
28 #include <sys/systeminfo.h>
29 #include "ldap_common.h"
34 * Debugging routine for printing the value of a result
38 printresult(ns_ldap_result_t
*result
)
41 ns_ldap_entry_t
*curEntry
;
43 printf("--------------------------------------\n");
44 printf("entries_count %d\n", result
->entries_count
);
45 curEntry
= result
->entry
;
46 for (i
= 0; i
< result
->entries_count
; i
++) {
47 printf("entry %d has attr_count = %d \n",
48 i
, curEntry
->attr_count
);
49 for (j
= 0; j
< curEntry
->attr_count
; j
++) {
50 printf("entry %d has attr_pair[%d] = %s \n",
51 i
, j
, curEntry
->attr_pair
[j
]->attrname
);
53 (k
< curEntry
->attr_pair
[j
]->value_count
) &&
54 (curEntry
->attr_pair
[j
]->attrvalue
[k
]);
56 printf("entry %d has "
57 "attr_pair[%d]->attrvalue[%d] = %s \n",
59 curEntry
->attr_pair
[j
]->attrvalue
[k
]);
61 printf("\n--------------------------------------\n");
62 curEntry
= curEntry
->next
;
74 getattr(ns_ldap_result_t
*result
, int i
)
76 ns_ldap_entry_t
*entry
;
79 (void) fprintf(stdout
, "\n[ldap_utils.c: getattr]\n");
83 entry
= result
->entry
;
87 if (result
->entries_count
== 0) {
90 return (entry
->attr_pair
[i
]);
95 * _get_domain_name() passes the dn one level up from cdn, e.g.,
96 * a pointer pointing to "ou= ..." for the cdn's listed below:
97 * dn: cn=hostname+ipHostNumber="109.34.54.76", ou= ...
98 * dn: echo+IpServiceProtocol=udp, ou= ...
99 * to __ns_ldap_dn2domain() to retrieve the domain name associated
104 _get_domain_name(char *cdn
)
107 char *pdn
, *domain
= NULL
;
110 const ns_cred_t
*cred
= NULL
;
111 ns_ldap_error_t
*error
;
113 /* break the cdn into its components */
114 rdns
= ldap_explode_dn(cdn
, 0);
115 if (rdns
== NULL
|| *rdns
== NULL
)
118 /* construct parent dn */
119 for (nrdns
= 1; rdns
[nrdns
]; nrdns
++)
120 len
+= strlen(rdns
[nrdns
]) + 1;
122 len
= strlen(rdns
[0]);
123 pdn
= (char *)malloc(len
+ 1);
125 ldap_value_free(rdns
);
131 (void) strcat(pdn
, rdns
[0]);
133 for (nrdns
= 1; rdns
[nrdns
]; nrdns
++) {
134 (void) strcat(pdn
, rdns
[nrdns
]);
135 (void) strcat(pdn
, ",");
137 /* remove the last ',' */
138 pdn
[strlen(pdn
) - 1] = '\0';
140 /* get domain name */
141 (void) __ns_ldap_dn2domain(pdn
, &domain
, cred
, &error
);
143 ldap_value_free(rdns
);
150 * "109.34.54.76" -> 109.34.54.76
154 _strip_quotes(char *ipaddress
)
158 /* look for first " */
159 if ((cp
= strchr(ipaddress
, '"')) == NULL
)
160 return ((char *)ipaddress
);
162 /* look for last " */
163 if ((cp
= strchr(ipaddress
, '"')) == NULL
)
164 return ((char *)ipaddress
);
172 * This is a copy of a routine in libnsl/nss/netdir_inet.c. It is
173 * here because /etc/lib/nss_ldap.so.1 cannot call routines in
174 * libnsl. Care should be taken to keep the two copies in sync.
178 __nss2herrno(nss_status_t nsstat
)
184 return (HOST_NOT_FOUND
);
188 default: /* keep gcc happy */
189 return (NO_RECOVERY
);
195 * This is a generic filter call back function for
196 * merging the filter from service search descriptor with
197 * an existing search filter. This routine expects userdata
198 * contain a format string with a single %s in it, and will
199 * use the format string with sprintf() to insert the SSD filter.
201 * This routine is passed to the __ns_ldap_list() or
202 * __ns_ldap_firstEntry() APIs as the filter call back
203 * together with the userdata. For example,
204 * the gethostbyname processing may call __ns_ldap_list() with
205 * "(&(objectClass=ipHost)(cn=sys1))" as filter, this function
206 * as the filter call back, and "(&(%s)(cn=sys1))" as the
207 * userdata, this routine will in turn gets call to produce
208 * "(&(department=sds)(cn=sys1))" as the real search
209 * filter, if the input SSD contains a filter "department=sds".
212 _merge_SSD_filter(const ns_ldap_search_desc_t
*desc
,
214 const void *userdata
)
220 (void) fprintf(stdout
, "\n[ldap_utils.c: _merge_SSD_filter]\n");
224 if (realfilter
== NULL
)
225 return (NS_LDAP_INVALID_PARAM
);
228 if (desc
== NULL
|| desc
->filter
== NULL
|| userdata
== NULL
)
229 return (NS_LDAP_INVALID_PARAM
);
231 /* Parameter check. We only want one %s here, otherwise bail. */
232 len
= 0; /* Reuse 'len' as "Number of %s hits"... */
233 checker
= (char *)userdata
;
235 checker
= strchr(checker
, '%');
236 if (checker
!= NULL
) {
237 if (len
> 0 || *(checker
+ 1) != 's')
238 return (NS_LDAP_INVALID_PARAM
);
239 len
++; /* Got our %s. */
242 return (NS_LDAP_INVALID_PARAM
);
243 } while (checker
!= NULL
);
246 (void) fprintf(stdout
, "\n[userdata: %s]\n", (char *)userdata
);
247 (void) fprintf(stdout
, "\n[SSD filter: %s]\n", desc
->filter
);
250 len
= strlen(userdata
) + strlen(desc
->filter
) + 1;
252 *realfilter
= (char *)malloc(len
);
253 if (*realfilter
== NULL
)
254 return (NS_LDAP_MEMORY
);
256 (void) sprintf(*realfilter
, (char *)userdata
, desc
->filter
);
259 (void) fprintf(stdout
, "\n[new filter: %s]\n", *realfilter
);
262 return (NS_LDAP_SUCCESS
);
268 return ("0123456789abcdef"[n
& 0xf]);
272 _ldap_filter_name(char *filter_name
, const char *name
, int filter_name_size
)
274 char *end
= filter_name
+ filter_name_size
;
277 for (; *name
; name
++) {
284 if (end
<= filter_name
+ 3)
286 *filter_name
++ = '\\';
287 *filter_name
++ = hex_char(c
>> 4);
288 *filter_name
++ = hex_char(c
& 0xf);
291 if (end
<= filter_name
+ 1)
297 if (end
<= filter_name
)