1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved.
3*0Sstevel@tonic-gate * Use is subject to license terms.
4*0Sstevel@tonic-gate */
5*0Sstevel@tonic-gate
6*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
11*0Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
12*0Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
13*0Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
14*0Sstevel@tonic-gate *
15*0Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
16*0Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17*0Sstevel@tonic-gate * implied. See the License for the specific language governing
18*0Sstevel@tonic-gate * rights and limitations under the License.
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
21*0Sstevel@tonic-gate * March 31, 1998.
22*0Sstevel@tonic-gate *
23*0Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
24*0Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
25*0Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
26*0Sstevel@tonic-gate * Rights Reserved.
27*0Sstevel@tonic-gate *
28*0Sstevel@tonic-gate * Contributor(s):
29*0Sstevel@tonic-gate */
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate * Copyright (c) 1996 Regents of the University of Michigan.
32*0Sstevel@tonic-gate * All rights reserved.
33*0Sstevel@tonic-gate *
34*0Sstevel@tonic-gate */
35*0Sstevel@tonic-gate /* LIBLDAP url.c -- LDAP URL related routines
36*0Sstevel@tonic-gate *
37*0Sstevel@tonic-gate * LDAP URLs look like this:
38*0Sstevel@tonic-gate * l d a p : / / hostport / dn [ ? attributes [ ? scope [ ? filter ] ] ]
39*0Sstevel@tonic-gate *
40*0Sstevel@tonic-gate * where:
41*0Sstevel@tonic-gate * attributes is a comma separated list
42*0Sstevel@tonic-gate * scope is one of these three strings: base one sub (default=base)
43*0Sstevel@tonic-gate * filter is an string-represented filter as in RFC 1558
44*0Sstevel@tonic-gate *
45*0Sstevel@tonic-gate * e.g., ldap://ldap.itd.umich.edu/c=US?o,description?one?o=umich
46*0Sstevel@tonic-gate *
47*0Sstevel@tonic-gate * We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
48*0Sstevel@tonic-gate */
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate #if 0
51*0Sstevel@tonic-gate #ifndef lint
52*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1996 Regents of the University of Michigan.\nAll rights reserved.\n";
53*0Sstevel@tonic-gate #endif
54*0Sstevel@tonic-gate #endif
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate #include "ldap-int.h"
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate static int skip_url_prefix( const char **urlp, int *enclosedp, int *securep );
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate int
63*0Sstevel@tonic-gate LDAP_CALL
ldap_is_ldap_url(const char * url)64*0Sstevel@tonic-gate ldap_is_ldap_url( const char *url )
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate int enclosed, secure;
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate return( url != NULL
69*0Sstevel@tonic-gate && skip_url_prefix( &url, &enclosed, &secure ));
70*0Sstevel@tonic-gate }
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate static int
skip_url_prefix(const char ** urlp,int * enclosedp,int * securep)74*0Sstevel@tonic-gate skip_url_prefix( const char **urlp, int *enclosedp, int *securep )
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate /*
77*0Sstevel@tonic-gate * return non-zero if this looks like a LDAP URL; zero if not
78*0Sstevel@tonic-gate * if non-zero returned, *urlp will be moved past "ldap://" part of URL
79*0Sstevel@tonic-gate * The data that *urlp points to is not changed by this function.
80*0Sstevel@tonic-gate */
81*0Sstevel@tonic-gate if ( *urlp == NULL ) {
82*0Sstevel@tonic-gate return( 0 );
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate /* skip leading '<' (if any) */
86*0Sstevel@tonic-gate if ( **urlp == '<' ) {
87*0Sstevel@tonic-gate *enclosedp = 1;
88*0Sstevel@tonic-gate ++*urlp;
89*0Sstevel@tonic-gate } else {
90*0Sstevel@tonic-gate *enclosedp = 0;
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate /* skip leading "URL:" (if any) */
94*0Sstevel@tonic-gate if ( strlen( *urlp ) >= LDAP_URL_URLCOLON_LEN && strncasecmp(
95*0Sstevel@tonic-gate *urlp, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
96*0Sstevel@tonic-gate *urlp += LDAP_URL_URLCOLON_LEN;
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate /* check for an "ldap://" prefix */
100*0Sstevel@tonic-gate if ( strlen( *urlp ) >= LDAP_URL_PREFIX_LEN && strncasecmp( *urlp,
101*0Sstevel@tonic-gate LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
102*0Sstevel@tonic-gate /* skip over URL prefix and return success */
103*0Sstevel@tonic-gate *urlp += LDAP_URL_PREFIX_LEN;
104*0Sstevel@tonic-gate *securep = 0;
105*0Sstevel@tonic-gate return( 1 );
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate /* check for an "ldaps://" prefix */
109*0Sstevel@tonic-gate if ( strlen( *urlp ) >= LDAPS_URL_PREFIX_LEN && strncasecmp( *urlp,
110*0Sstevel@tonic-gate LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
111*0Sstevel@tonic-gate /* skip over URL prefix and return success */
112*0Sstevel@tonic-gate *urlp += LDAPS_URL_PREFIX_LEN;
113*0Sstevel@tonic-gate *securep = 1;
114*0Sstevel@tonic-gate return( 1 );
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate return( 0 ); /* not an LDAP URL */
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate int
122*0Sstevel@tonic-gate LDAP_CALL
ldap_url_parse(const char * url,LDAPURLDesc ** ludpp)123*0Sstevel@tonic-gate ldap_url_parse( const char *url, LDAPURLDesc **ludpp )
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate * Pick apart the pieces of an LDAP URL.
127*0Sstevel@tonic-gate */
128*0Sstevel@tonic-gate int rc;
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate if (( rc = nsldapi_url_parse( url, ludpp, 1 )) == 0 ) {
131*0Sstevel@tonic-gate if ( (*ludpp)->lud_scope == -1 ) {
132*0Sstevel@tonic-gate (*ludpp)->lud_scope = LDAP_SCOPE_BASE;
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate if ( (*ludpp)->lud_filter == NULL ) {
135*0Sstevel@tonic-gate (*ludpp)->lud_filter = "(objectclass=*)";
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate if ( *((*ludpp)->lud_dn) == '\0' ) {
138*0Sstevel@tonic-gate (*ludpp)->lud_dn = NULL;
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate return( rc );
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate /* same as ldap_url_parse(), but dn is not require */
146*0Sstevel@tonic-gate int
147*0Sstevel@tonic-gate LDAP_CALL
ldap_url_parse_nodn(const char * url,LDAPURLDesc ** ludpp)148*0Sstevel@tonic-gate ldap_url_parse_nodn(const char *url, LDAPURLDesc **ludpp)
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate /*
151*0Sstevel@tonic-gate * Pick apart the pieces of an LDAP URL.
152*0Sstevel@tonic-gate */
153*0Sstevel@tonic-gate int rc;
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate if ((rc = nsldapi_url_parse(url, ludpp, 0)) == 0) {
156*0Sstevel@tonic-gate if ((*ludpp)->lud_scope == -1) {
157*0Sstevel@tonic-gate (*ludpp)->lud_scope = LDAP_SCOPE_BASE;
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate if ((*ludpp)->lud_filter == NULL) {
160*0Sstevel@tonic-gate (*ludpp)->lud_filter = "(objectclass=*)";
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate if ((*ludpp)->lud_dn && *((*ludpp)->lud_dn) == '\0') {
163*0Sstevel@tonic-gate (*ludpp)->lud_dn = NULL;
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate return (rc);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate /*
172*0Sstevel@tonic-gate * like ldap_url_parse() with a few exceptions:
173*0Sstevel@tonic-gate * 1) if dn_required is zero, a missing DN does not generate an error
174*0Sstevel@tonic-gate * (we just leave the lud_dn field NULL)
175*0Sstevel@tonic-gate * 2) no defaults are set for lud_scope and lud_filter (they are set to -1
176*0Sstevel@tonic-gate * and NULL respectively if no SCOPE or FILTER are present in the URL).
177*0Sstevel@tonic-gate * 3) when there is a zero-length DN in a URL we do not set lud_dn to NULL.
178*0Sstevel@tonic-gate * 4) if an LDAPv3 URL extensions are included,
179*0Sstevel@tonic-gate */
180*0Sstevel@tonic-gate int
nsldapi_url_parse(const char * url,LDAPURLDesc ** ludpp,int dn_required)181*0Sstevel@tonic-gate nsldapi_url_parse( const char *url, LDAPURLDesc **ludpp, int dn_required )
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate LDAPURLDesc *ludp;
185*0Sstevel@tonic-gate char *urlcopy, *attrs, *scope, *extensions = NULL, *p, *q;
186*0Sstevel@tonic-gate int enclosed, secure, i, nattrs, at_start;
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_url_parse(%s)\n", url, 0, 0 );
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate if ( url == NULL || ludpp == NULL ) {
191*0Sstevel@tonic-gate return( LDAP_URL_ERR_PARAM );
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate *ludpp = NULL; /* pessimistic */
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate if ( !skip_url_prefix( &url, &enclosed, &secure )) {
197*0Sstevel@tonic-gate return( LDAP_URL_ERR_NOTLDAP );
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate /* allocate return struct */
201*0Sstevel@tonic-gate if (( ludp = (LDAPURLDesc *)NSLDAPI_CALLOC( 1, sizeof( LDAPURLDesc )))
202*0Sstevel@tonic-gate == NULLLDAPURLDESC ) {
203*0Sstevel@tonic-gate return( LDAP_URL_ERR_MEM );
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate if ( secure ) {
207*0Sstevel@tonic-gate ludp->lud_options |= LDAP_URL_OPT_SECURE;
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate /* make working copy of the remainder of the URL */
211*0Sstevel@tonic-gate if (( urlcopy = nsldapi_strdup( url )) == NULL ) {
212*0Sstevel@tonic-gate ldap_free_urldesc( ludp );
213*0Sstevel@tonic-gate return( LDAP_URL_ERR_MEM );
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate if ( enclosed && *((p = urlcopy + strlen( urlcopy ) - 1)) == '>' ) {
217*0Sstevel@tonic-gate *p = '\0';
218*0Sstevel@tonic-gate }
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate /* initialize scope and filter */
221*0Sstevel@tonic-gate ludp->lud_scope = -1;
222*0Sstevel@tonic-gate ludp->lud_filter = NULL;
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gate /* lud_string is the only malloc'd string space we use */
225*0Sstevel@tonic-gate ludp->lud_string = urlcopy;
226*0Sstevel@tonic-gate
227*0Sstevel@tonic-gate /* scan forward for '/' that marks end of hostport and begin. of dn */
228*0Sstevel@tonic-gate if (( ludp->lud_dn = strchr( urlcopy, '/' )) == NULL ) {
229*0Sstevel@tonic-gate if ( dn_required ) {
230*0Sstevel@tonic-gate ldap_free_urldesc( ludp );
231*0Sstevel@tonic-gate return( LDAP_URL_ERR_NODN );
232*0Sstevel@tonic-gate }
233*0Sstevel@tonic-gate } else {
234*0Sstevel@tonic-gate /* terminate hostport; point to start of dn */
235*0Sstevel@tonic-gate *ludp->lud_dn++ = '\0';
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate if ( *urlcopy == '\0' ) {
240*0Sstevel@tonic-gate ludp->lud_host = NULL;
241*0Sstevel@tonic-gate } else {
242*0Sstevel@tonic-gate ludp->lud_host = urlcopy;
243*0Sstevel@tonic-gate nsldapi_hex_unescape( ludp->lud_host );
244*0Sstevel@tonic-gate
245*0Sstevel@tonic-gate /*
246*0Sstevel@tonic-gate * Locate and strip off optional port number (:#) in host
247*0Sstevel@tonic-gate * portion of URL.
248*0Sstevel@tonic-gate *
249*0Sstevel@tonic-gate * If more than one space-separated host is listed, we only
250*0Sstevel@tonic-gate * look for a port number within the right-most one since
251*0Sstevel@tonic-gate * ldap_init() will handle host parameters that look like
252*0Sstevel@tonic-gate * host:port anyway.
253*0Sstevel@tonic-gate */
254*0Sstevel@tonic-gate if (( p = strrchr( ludp->lud_host, ' ' )) == NULL ) {
255*0Sstevel@tonic-gate p = ludp->lud_host;
256*0Sstevel@tonic-gate } else {
257*0Sstevel@tonic-gate ++p;
258*0Sstevel@tonic-gate }
259*0Sstevel@tonic-gate if ( *p == '[' && ( q = strchr( p, ']' )) != NULL ) {
260*0Sstevel@tonic-gate /* square brackets present -- skip past them */
261*0Sstevel@tonic-gate p = q++;
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate if (( p = strchr( p, ':' )) != NULL ) {
264*0Sstevel@tonic-gate *p++ = '\0';
265*0Sstevel@tonic-gate ludp->lud_port = atoi( p );
266*0Sstevel@tonic-gate if ( *ludp->lud_host == '\0' ) {
267*0Sstevel@tonic-gate /*
268*0Sstevel@tonic-gate * no hostname and a port: invalid hostcode
269*0Sstevel@tonic-gate * according to RFC 1738
270*0Sstevel@tonic-gate */
271*0Sstevel@tonic-gate ldap_free_urldesc(ludp);
272*0Sstevel@tonic-gate return (LDAP_URL_ERR_HOSTPORT);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate }
275*0Sstevel@tonic-gate }
276*0Sstevel@tonic-gate
277*0Sstevel@tonic-gate /* scan for '?' that marks end of dn and beginning of attributes */
278*0Sstevel@tonic-gate attrs = NULL;
279*0Sstevel@tonic-gate if ( ludp->lud_dn != NULL &&
280*0Sstevel@tonic-gate ( attrs = strchr( ludp->lud_dn, '?' )) != NULL ) {
281*0Sstevel@tonic-gate /* terminate dn; point to start of attrs. */
282*0Sstevel@tonic-gate *attrs++ = '\0';
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gate /* scan for '?' that marks end of attrs and begin. of scope */
285*0Sstevel@tonic-gate if (( p = strchr( attrs, '?' )) != NULL ) {
286*0Sstevel@tonic-gate /*
287*0Sstevel@tonic-gate * terminate attrs; point to start of scope and scan for
288*0Sstevel@tonic-gate * '?' that marks end of scope and begin. of filter
289*0Sstevel@tonic-gate */
290*0Sstevel@tonic-gate *p++ = '\0';
291*0Sstevel@tonic-gate scope = p;
292*0Sstevel@tonic-gate
293*0Sstevel@tonic-gate if (( p = strchr( scope, '?' )) != NULL ) {
294*0Sstevel@tonic-gate /* terminate scope; point to start of filter */
295*0Sstevel@tonic-gate *p++ = '\0';
296*0Sstevel@tonic-gate if ( *p != '\0' ) {
297*0Sstevel@tonic-gate ludp->lud_filter = p;
298*0Sstevel@tonic-gate /*
299*0Sstevel@tonic-gate * scan for the '?' that marks the end
300*0Sstevel@tonic-gate * of the filter and the start of any
301*0Sstevel@tonic-gate * extensions
302*0Sstevel@tonic-gate */
303*0Sstevel@tonic-gate if (( p = strchr( ludp->lud_filter, '?' ))
304*0Sstevel@tonic-gate != NULL ) {
305*0Sstevel@tonic-gate *p++ = '\0'; /* term. filter */
306*0Sstevel@tonic-gate extensions = p;
307*0Sstevel@tonic-gate }
308*0Sstevel@tonic-gate if ( *ludp->lud_filter == '\0' ) {
309*0Sstevel@tonic-gate ludp->lud_filter = NULL;
310*0Sstevel@tonic-gate } else {
311*0Sstevel@tonic-gate nsldapi_hex_unescape( ludp->lud_filter );
312*0Sstevel@tonic-gate }
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate }
315*0Sstevel@tonic-gate
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate if ( strcasecmp( scope, "one" ) == 0 ) {
318*0Sstevel@tonic-gate ludp->lud_scope = LDAP_SCOPE_ONELEVEL;
319*0Sstevel@tonic-gate } else if ( strcasecmp( scope, "base" ) == 0 ) {
320*0Sstevel@tonic-gate ludp->lud_scope = LDAP_SCOPE_BASE;
321*0Sstevel@tonic-gate } else if ( strcasecmp( scope, "sub" ) == 0 ) {
322*0Sstevel@tonic-gate ludp->lud_scope = LDAP_SCOPE_SUBTREE;
323*0Sstevel@tonic-gate } else if ( *scope != '\0' ) {
324*0Sstevel@tonic-gate ldap_free_urldesc( ludp );
325*0Sstevel@tonic-gate return( LDAP_URL_ERR_BADSCOPE );
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gate if ( ludp->lud_dn != NULL ) {
331*0Sstevel@tonic-gate nsldapi_hex_unescape( ludp->lud_dn );
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gate /*
335*0Sstevel@tonic-gate * if attrs list was included, turn it into a null-terminated array
336*0Sstevel@tonic-gate */
337*0Sstevel@tonic-gate if ( attrs != NULL && *attrs != '\0' ) {
338*0Sstevel@tonic-gate nsldapi_hex_unescape( attrs );
339*0Sstevel@tonic-gate for ( nattrs = 1, p = attrs; *p != '\0'; ++p ) {
340*0Sstevel@tonic-gate if ( *p == ',' ) {
341*0Sstevel@tonic-gate ++nattrs;
342*0Sstevel@tonic-gate }
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate
345*0Sstevel@tonic-gate if (( ludp->lud_attrs = (char **)NSLDAPI_CALLOC( nattrs + 1,
346*0Sstevel@tonic-gate sizeof( char * ))) == NULL ) {
347*0Sstevel@tonic-gate ldap_free_urldesc( ludp );
348*0Sstevel@tonic-gate return( LDAP_URL_ERR_MEM );
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate
351*0Sstevel@tonic-gate for ( i = 0, p = attrs; i < nattrs; ++i ) {
352*0Sstevel@tonic-gate ludp->lud_attrs[ i ] = p;
353*0Sstevel@tonic-gate if (( p = strchr( p, ',' )) != NULL ) {
354*0Sstevel@tonic-gate *p++ ='\0';
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate nsldapi_hex_unescape( ludp->lud_attrs[ i ] );
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate }
359*0Sstevel@tonic-gate
360*0Sstevel@tonic-gate /* if extensions list was included, check for critical ones */
361*0Sstevel@tonic-gate if ( extensions != NULL && *extensions != '\0' ) {
362*0Sstevel@tonic-gate /* Note: at present, we do not recognize ANY extensions */
363*0Sstevel@tonic-gate at_start = 1;
364*0Sstevel@tonic-gate for ( p = extensions; *p != '\0'; ++p ) {
365*0Sstevel@tonic-gate if ( at_start ) {
366*0Sstevel@tonic-gate if ( *p == '!' ) { /* critical extension */
367*0Sstevel@tonic-gate ldap_free_urldesc( ludp );
368*0Sstevel@tonic-gate /* this is what iplanet did *
369*0Sstevel@tonic-gate return( LDAP_URL_UNRECOGNIZED_CRITICAL_EXTENSION );
370*0Sstevel@tonic-gate * and this is what we do */
371*0Sstevel@tonic-gate return( LDAP_URL_ERR_PARAM );
372*0Sstevel@tonic-gate }
373*0Sstevel@tonic-gate at_start = 0;
374*0Sstevel@tonic-gate } else if ( *p == ',' ) {
375*0Sstevel@tonic-gate at_start = 1;
376*0Sstevel@tonic-gate }
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate }
379*0Sstevel@tonic-gate
380*0Sstevel@tonic-gate
381*0Sstevel@tonic-gate *ludpp = ludp;
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gate return( 0 );
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gate
387*0Sstevel@tonic-gate void
388*0Sstevel@tonic-gate LDAP_CALL
ldap_free_urldesc(LDAPURLDesc * ludp)389*0Sstevel@tonic-gate ldap_free_urldesc( LDAPURLDesc *ludp )
390*0Sstevel@tonic-gate {
391*0Sstevel@tonic-gate if ( ludp != NULLLDAPURLDESC ) {
392*0Sstevel@tonic-gate if ( ludp->lud_string != NULL ) {
393*0Sstevel@tonic-gate NSLDAPI_FREE( ludp->lud_string );
394*0Sstevel@tonic-gate }
395*0Sstevel@tonic-gate if ( ludp->lud_attrs != NULL ) {
396*0Sstevel@tonic-gate NSLDAPI_FREE( ludp->lud_attrs );
397*0Sstevel@tonic-gate }
398*0Sstevel@tonic-gate NSLDAPI_FREE( ludp );
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate }
401*0Sstevel@tonic-gate
402*0Sstevel@tonic-gate
403*0Sstevel@tonic-gate int
404*0Sstevel@tonic-gate LDAP_CALL
ldap_url_search(LDAP * ld,const char * url,int attrsonly)405*0Sstevel@tonic-gate ldap_url_search( LDAP *ld, const char *url, int attrsonly )
406*0Sstevel@tonic-gate {
407*0Sstevel@tonic-gate int err, msgid;
408*0Sstevel@tonic-gate LDAPURLDesc *ludp;
409*0Sstevel@tonic-gate BerElement *ber;
410*0Sstevel@tonic-gate LDAPServer *srv;
411*0Sstevel@tonic-gate char *host;
412*0Sstevel@tonic-gate
413*0Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
414*0Sstevel@tonic-gate return( -1 ); /* punt */
415*0Sstevel@tonic-gate }
416*0Sstevel@tonic-gate
417*0Sstevel@tonic-gate if ( ldap_url_parse( url, &ludp ) != 0 ) {
418*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
419*0Sstevel@tonic-gate return( -1 );
420*0Sstevel@tonic-gate }
421*0Sstevel@tonic-gate
422*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
423*0Sstevel@tonic-gate msgid = ++ld->ld_msgid;
424*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
425*0Sstevel@tonic-gate
426*0Sstevel@tonic-gate if ( nsldapi_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
427*0Sstevel@tonic-gate ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
428*0Sstevel@tonic-gate -1, -1, msgid, &ber ) != LDAP_SUCCESS ) {
429*0Sstevel@tonic-gate return( -1 );
430*0Sstevel@tonic-gate }
431*0Sstevel@tonic-gate
432*0Sstevel@tonic-gate err = 0;
433*0Sstevel@tonic-gate
434*0Sstevel@tonic-gate if ( ludp->lud_host == NULL ) {
435*0Sstevel@tonic-gate host = ld->ld_defhost;
436*0Sstevel@tonic-gate } else {
437*0Sstevel@tonic-gate host = ludp->lud_host;
438*0Sstevel@tonic-gate }
439*0Sstevel@tonic-gate
440*0Sstevel@tonic-gate if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1, sizeof( LDAPServer )))
441*0Sstevel@tonic-gate == NULL || ( host != NULL &&
442*0Sstevel@tonic-gate ( srv->lsrv_host = nsldapi_strdup( host )) == NULL )) {
443*0Sstevel@tonic-gate if ( srv != NULL ) {
444*0Sstevel@tonic-gate NSLDAPI_FREE( srv );
445*0Sstevel@tonic-gate }
446*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
447*0Sstevel@tonic-gate err = -1;
448*0Sstevel@tonic-gate } else {
449*0Sstevel@tonic-gate if ( ludp->lud_port != 0 ) {
450*0Sstevel@tonic-gate /* URL includes a port - use it */
451*0Sstevel@tonic-gate srv->lsrv_port = ludp->lud_port;
452*0Sstevel@tonic-gate } else if ( ludp->lud_host == NULL ) {
453*0Sstevel@tonic-gate /* URL has no port or host - use port from ld */
454*0Sstevel@tonic-gate srv->lsrv_port = ld->ld_defport;
455*0Sstevel@tonic-gate } else if (( ludp->lud_options & LDAP_URL_OPT_SECURE ) == 0 ) {
456*0Sstevel@tonic-gate /* ldap URL has a host but no port - use std. port */
457*0Sstevel@tonic-gate srv->lsrv_port = LDAP_PORT;
458*0Sstevel@tonic-gate } else {
459*0Sstevel@tonic-gate /* ldaps URL has a host but no port - use std. port */
460*0Sstevel@tonic-gate srv->lsrv_port = LDAPS_PORT;
461*0Sstevel@tonic-gate }
462*0Sstevel@tonic-gate }
463*0Sstevel@tonic-gate
464*0Sstevel@tonic-gate if (( ludp->lud_options & LDAP_URL_OPT_SECURE ) != 0 ) {
465*0Sstevel@tonic-gate srv->lsrv_options |= LDAP_SRV_OPT_SECURE;
466*0Sstevel@tonic-gate }
467*0Sstevel@tonic-gate
468*0Sstevel@tonic-gate if ( err != 0 ) {
469*0Sstevel@tonic-gate ber_free( ber, 1 );
470*0Sstevel@tonic-gate } else {
471*0Sstevel@tonic-gate err = nsldapi_send_server_request( ld, ber, msgid, NULL, srv,
472*0Sstevel@tonic-gate NULL, NULL, 1 );
473*0Sstevel@tonic-gate }
474*0Sstevel@tonic-gate
475*0Sstevel@tonic-gate ldap_free_urldesc( ludp );
476*0Sstevel@tonic-gate return( err );
477*0Sstevel@tonic-gate }
478*0Sstevel@tonic-gate
479*0Sstevel@tonic-gate
480*0Sstevel@tonic-gate int
481*0Sstevel@tonic-gate LDAP_CALL
ldap_url_search_st(LDAP * ld,const char * url,int attrsonly,struct timeval * timeout,LDAPMessage ** res)482*0Sstevel@tonic-gate ldap_url_search_st( LDAP *ld, const char *url, int attrsonly,
483*0Sstevel@tonic-gate struct timeval *timeout, LDAPMessage **res )
484*0Sstevel@tonic-gate {
485*0Sstevel@tonic-gate int msgid;
486*0Sstevel@tonic-gate
487*0Sstevel@tonic-gate /*
488*0Sstevel@tonic-gate * It is an error to pass in a zero'd timeval.
489*0Sstevel@tonic-gate */
490*0Sstevel@tonic-gate if ( timeout != NULL && timeout->tv_sec == 0 &&
491*0Sstevel@tonic-gate timeout->tv_usec == 0 ) {
492*0Sstevel@tonic-gate if ( ld != NULL ) {
493*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
494*0Sstevel@tonic-gate }
495*0Sstevel@tonic-gate if ( res != NULL ) {
496*0Sstevel@tonic-gate *res = NULL;
497*0Sstevel@tonic-gate }
498*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
499*0Sstevel@tonic-gate }
500*0Sstevel@tonic-gate
501*0Sstevel@tonic-gate if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
502*0Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate
505*0Sstevel@tonic-gate if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
506*0Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
507*0Sstevel@tonic-gate }
508*0Sstevel@tonic-gate
509*0Sstevel@tonic-gate if ( LDAP_GET_LDERRNO( ld, NULL, NULL ) == LDAP_TIMEOUT ) {
510*0Sstevel@tonic-gate (void) ldap_abandon( ld, msgid );
511*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_TIMEOUT, NULL, NULL );
512*0Sstevel@tonic-gate return( LDAP_TIMEOUT );
513*0Sstevel@tonic-gate }
514*0Sstevel@tonic-gate
515*0Sstevel@tonic-gate return( ldap_result2error( ld, *res, 0 ));
516*0Sstevel@tonic-gate }
517*0Sstevel@tonic-gate
518*0Sstevel@tonic-gate
519*0Sstevel@tonic-gate int
520*0Sstevel@tonic-gate LDAP_CALL
ldap_url_search_s(LDAP * ld,const char * url,int attrsonly,LDAPMessage ** res)521*0Sstevel@tonic-gate ldap_url_search_s( LDAP *ld, const char *url, int attrsonly, LDAPMessage **res )
522*0Sstevel@tonic-gate {
523*0Sstevel@tonic-gate int msgid;
524*0Sstevel@tonic-gate
525*0Sstevel@tonic-gate if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
526*0Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
527*0Sstevel@tonic-gate }
528*0Sstevel@tonic-gate
529*0Sstevel@tonic-gate if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
530*0Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
531*0Sstevel@tonic-gate }
532*0Sstevel@tonic-gate
533*0Sstevel@tonic-gate return( ldap_result2error( ld, *res, 0 ));
534*0Sstevel@tonic-gate }
535*0Sstevel@tonic-gate
536*0Sstevel@tonic-gate #ifdef _SOLARIS_SDK
537*0Sstevel@tonic-gate /*
538*0Sstevel@tonic-gate * Locate the LDAP URL associated with a DNS domain name.
539*0Sstevel@tonic-gate *
540*0Sstevel@tonic-gate * The supplied DNS domain name is converted into a distinguished
541*0Sstevel@tonic-gate * name. The directory entry specified by that distinguished name
542*0Sstevel@tonic-gate * is searched for a labeledURI attribute. If successful then the
543*0Sstevel@tonic-gate * LDAP URL is returned. If unsuccessful then that entry's parent
544*0Sstevel@tonic-gate * is searched and so on until the target distinguished name is
545*0Sstevel@tonic-gate * reduced to only two nameparts.
546*0Sstevel@tonic-gate *
547*0Sstevel@tonic-gate * For example, if 'ny.eng.wiz.com' is the DNS domain then the
548*0Sstevel@tonic-gate * following entries are searched until one succeeds:
549*0Sstevel@tonic-gate * dc=ny,dc=eng,dc=wiz,dc=com
550*0Sstevel@tonic-gate * dc=eng,dc=wiz,dc=com
551*0Sstevel@tonic-gate * dc=wiz,dc=com
552*0Sstevel@tonic-gate *
553*0Sstevel@tonic-gate * If dns_name is NULL then the environment variable LOCALDOMAIN is used.
554*0Sstevel@tonic-gate * If attrs is not NULL then it is appended to the URL's attribute list.
555*0Sstevel@tonic-gate * If scope is not NULL then it overrides the URL's scope.
556*0Sstevel@tonic-gate * If filter is not NULL then it is merged with the URL's filter.
557*0Sstevel@tonic-gate *
558*0Sstevel@tonic-gate * If an error is encountered then zero is returned, otherwise a string
559*0Sstevel@tonic-gate * URL is returned. The caller should free the returned string if it is
560*0Sstevel@tonic-gate * non-zero.
561*0Sstevel@tonic-gate */
562*0Sstevel@tonic-gate
563*0Sstevel@tonic-gate char *
ldap_dns_to_url(LDAP * ld,char * dns_name,char * attrs,char * scope,char * filter)564*0Sstevel@tonic-gate ldap_dns_to_url(
565*0Sstevel@tonic-gate LDAP *ld,
566*0Sstevel@tonic-gate char *dns_name,
567*0Sstevel@tonic-gate char *attrs,
568*0Sstevel@tonic-gate char *scope,
569*0Sstevel@tonic-gate char *filter
570*0Sstevel@tonic-gate )
571*0Sstevel@tonic-gate {
572*0Sstevel@tonic-gate char *dn;
573*0Sstevel@tonic-gate char *url = 0;
574*0Sstevel@tonic-gate char *url2 = 0;
575*0Sstevel@tonic-gate LDAPURLDesc *urldesc;
576*0Sstevel@tonic-gate char *cp;
577*0Sstevel@tonic-gate char *cp2;
578*0Sstevel@tonic-gate size_t attrs_len = 0;
579*0Sstevel@tonic-gate size_t scope_len = 0;
580*0Sstevel@tonic-gate size_t filter_len = 0;
581*0Sstevel@tonic-gate int nameparts;
582*0Sstevel@tonic-gate int no_attrs = 0;
583*0Sstevel@tonic-gate int no_scope = 0;
584*0Sstevel@tonic-gate
585*0Sstevel@tonic-gate if (dns_name == 0) {
586*0Sstevel@tonic-gate dns_name = (char *)getenv("LOCALDOMAIN");
587*0Sstevel@tonic-gate }
588*0Sstevel@tonic-gate
589*0Sstevel@tonic-gate if ((ld == NULL) || ((dn = ldap_dns_to_dn(dns_name, &nameparts)) ==
590*0Sstevel@tonic-gate NULL))
591*0Sstevel@tonic-gate return (0);
592*0Sstevel@tonic-gate
593*0Sstevel@tonic-gate if ((url = ldap_dn_to_url(ld, dn, nameparts)) == NULL) {
594*0Sstevel@tonic-gate free(dn);
595*0Sstevel@tonic-gate return (0);
596*0Sstevel@tonic-gate }
597*0Sstevel@tonic-gate free(dn);
598*0Sstevel@tonic-gate
599*0Sstevel@tonic-gate /* merge filter and/or scope and/or attributes with URL */
600*0Sstevel@tonic-gate if (attrs || scope || filter) {
601*0Sstevel@tonic-gate
602*0Sstevel@tonic-gate if (attrs)
603*0Sstevel@tonic-gate attrs_len = strlen(attrs) + 2; /* for comma and NULL */
604*0Sstevel@tonic-gate
605*0Sstevel@tonic-gate if (scope)
606*0Sstevel@tonic-gate scope_len = strlen(scope) + 1; /* for NULL */
607*0Sstevel@tonic-gate
608*0Sstevel@tonic-gate if (filter)
609*0Sstevel@tonic-gate filter_len = strlen(filter) + 4;
610*0Sstevel@tonic-gate /* for ampersand, parentheses and NULL */
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gate if (ldap_is_ldap_url(url)) {
613*0Sstevel@tonic-gate
614*0Sstevel@tonic-gate if ((url2 = (char *)malloc(attrs_len + scope_len +
615*0Sstevel@tonic-gate filter_len + strlen(url) + 1)) == NULL) {
616*0Sstevel@tonic-gate return (0);
617*0Sstevel@tonic-gate }
618*0Sstevel@tonic-gate cp = url;
619*0Sstevel@tonic-gate cp2 = url2;
620*0Sstevel@tonic-gate
621*0Sstevel@tonic-gate /* copy URL scheme, hostname, port number and DN */
622*0Sstevel@tonic-gate while (*cp && (*cp != '?')) {
623*0Sstevel@tonic-gate *cp2++ = *cp++;
624*0Sstevel@tonic-gate }
625*0Sstevel@tonic-gate
626*0Sstevel@tonic-gate /* handle URL attributes */
627*0Sstevel@tonic-gate
628*0Sstevel@tonic-gate if (*cp == '?') { /* test first '?' */
629*0Sstevel@tonic-gate *cp2++ = *cp++; /* copy first '?' */
630*0Sstevel@tonic-gate
631*0Sstevel@tonic-gate if (*cp == '?') { /* test second '?' */
632*0Sstevel@tonic-gate
633*0Sstevel@tonic-gate /* insert supplied attributes */
634*0Sstevel@tonic-gate if (attrs) {
635*0Sstevel@tonic-gate while (*attrs) {
636*0Sstevel@tonic-gate *cp2++ = *attrs++;
637*0Sstevel@tonic-gate }
638*0Sstevel@tonic-gate } else {
639*0Sstevel@tonic-gate no_attrs = 1;
640*0Sstevel@tonic-gate }
641*0Sstevel@tonic-gate
642*0Sstevel@tonic-gate } else {
643*0Sstevel@tonic-gate
644*0Sstevel@tonic-gate /* copy URL attributes */
645*0Sstevel@tonic-gate while (*cp && (*cp != '?')) {
646*0Sstevel@tonic-gate *cp2++ = *cp++;
647*0Sstevel@tonic-gate }
648*0Sstevel@tonic-gate
649*0Sstevel@tonic-gate /* append supplied attributes */
650*0Sstevel@tonic-gate if (attrs) {
651*0Sstevel@tonic-gate *cp2++ = ',';
652*0Sstevel@tonic-gate while (*attrs) {
653*0Sstevel@tonic-gate *cp2++ = *attrs++;
654*0Sstevel@tonic-gate }
655*0Sstevel@tonic-gate }
656*0Sstevel@tonic-gate }
657*0Sstevel@tonic-gate
658*0Sstevel@tonic-gate } else {
659*0Sstevel@tonic-gate /* append supplied attributes */
660*0Sstevel@tonic-gate if (attrs) {
661*0Sstevel@tonic-gate *cp2++ = '?';
662*0Sstevel@tonic-gate while (*attrs) {
663*0Sstevel@tonic-gate *cp2++ = *attrs++;
664*0Sstevel@tonic-gate }
665*0Sstevel@tonic-gate } else {
666*0Sstevel@tonic-gate no_attrs = 1;
667*0Sstevel@tonic-gate }
668*0Sstevel@tonic-gate }
669*0Sstevel@tonic-gate
670*0Sstevel@tonic-gate /* handle URL scope */
671*0Sstevel@tonic-gate
672*0Sstevel@tonic-gate if (*cp == '?') { /* test second '?' */
673*0Sstevel@tonic-gate *cp2++ = *cp++; /* copy second '?' */
674*0Sstevel@tonic-gate
675*0Sstevel@tonic-gate if (*cp == '?') { /* test third '?' */
676*0Sstevel@tonic-gate
677*0Sstevel@tonic-gate /* insert supplied scope */
678*0Sstevel@tonic-gate if (scope) {
679*0Sstevel@tonic-gate while (*scope) {
680*0Sstevel@tonic-gate *cp2++ = *scope++;
681*0Sstevel@tonic-gate }
682*0Sstevel@tonic-gate } else {
683*0Sstevel@tonic-gate no_scope = 1;
684*0Sstevel@tonic-gate }
685*0Sstevel@tonic-gate
686*0Sstevel@tonic-gate } else {
687*0Sstevel@tonic-gate
688*0Sstevel@tonic-gate if (scope) {
689*0Sstevel@tonic-gate /* skip over URL scope */
690*0Sstevel@tonic-gate while (*cp && (*cp != '?')) {
691*0Sstevel@tonic-gate *cp++;
692*0Sstevel@tonic-gate }
693*0Sstevel@tonic-gate /* insert supplied scope */
694*0Sstevel@tonic-gate while (*scope) {
695*0Sstevel@tonic-gate *cp2++ = *scope++;
696*0Sstevel@tonic-gate }
697*0Sstevel@tonic-gate } else {
698*0Sstevel@tonic-gate
699*0Sstevel@tonic-gate /* copy URL scope */
700*0Sstevel@tonic-gate while (*cp && (*cp != '?')) {
701*0Sstevel@tonic-gate *cp2++ = *cp++;
702*0Sstevel@tonic-gate }
703*0Sstevel@tonic-gate }
704*0Sstevel@tonic-gate }
705*0Sstevel@tonic-gate
706*0Sstevel@tonic-gate } else {
707*0Sstevel@tonic-gate /* append supplied scope */
708*0Sstevel@tonic-gate if (scope) {
709*0Sstevel@tonic-gate if (no_attrs) {
710*0Sstevel@tonic-gate *cp2++ = '?';
711*0Sstevel@tonic-gate }
712*0Sstevel@tonic-gate *cp2++ = '?';
713*0Sstevel@tonic-gate while (*scope) {
714*0Sstevel@tonic-gate *cp2++ = *scope++;
715*0Sstevel@tonic-gate }
716*0Sstevel@tonic-gate } else {
717*0Sstevel@tonic-gate no_scope = 1;
718*0Sstevel@tonic-gate }
719*0Sstevel@tonic-gate }
720*0Sstevel@tonic-gate
721*0Sstevel@tonic-gate /* handle URL filter */
722*0Sstevel@tonic-gate
723*0Sstevel@tonic-gate if (*cp == '?') { /* test third '?' */
724*0Sstevel@tonic-gate *cp2++ = *cp++; /* copy third '?' */
725*0Sstevel@tonic-gate
726*0Sstevel@tonic-gate if (filter) {
727*0Sstevel@tonic-gate
728*0Sstevel@tonic-gate /* merge URL and supplied filters */
729*0Sstevel@tonic-gate
730*0Sstevel@tonic-gate *cp2++ = '(';
731*0Sstevel@tonic-gate *cp2++ = '&';
732*0Sstevel@tonic-gate /* copy URL filter */
733*0Sstevel@tonic-gate while (*cp) {
734*0Sstevel@tonic-gate *cp2++ = *cp++;
735*0Sstevel@tonic-gate }
736*0Sstevel@tonic-gate /* append supplied filter */
737*0Sstevel@tonic-gate while (*filter) {
738*0Sstevel@tonic-gate *cp2++ = *filter++;
739*0Sstevel@tonic-gate }
740*0Sstevel@tonic-gate *cp2++ = ')';
741*0Sstevel@tonic-gate } else {
742*0Sstevel@tonic-gate
743*0Sstevel@tonic-gate /* copy URL filter */
744*0Sstevel@tonic-gate while (*cp) {
745*0Sstevel@tonic-gate *cp2++ = *cp++;
746*0Sstevel@tonic-gate }
747*0Sstevel@tonic-gate }
748*0Sstevel@tonic-gate
749*0Sstevel@tonic-gate } else {
750*0Sstevel@tonic-gate /* append supplied filter */
751*0Sstevel@tonic-gate if (filter) {
752*0Sstevel@tonic-gate if (no_scope) {
753*0Sstevel@tonic-gate if (no_attrs) {
754*0Sstevel@tonic-gate *cp2++ = '?';
755*0Sstevel@tonic-gate }
756*0Sstevel@tonic-gate *cp2++ = '?';
757*0Sstevel@tonic-gate }
758*0Sstevel@tonic-gate *cp2++ = '?';
759*0Sstevel@tonic-gate while (*filter) {
760*0Sstevel@tonic-gate *cp2++ = *filter++;
761*0Sstevel@tonic-gate }
762*0Sstevel@tonic-gate }
763*0Sstevel@tonic-gate }
764*0Sstevel@tonic-gate
765*0Sstevel@tonic-gate *cp2++ = '\0';
766*0Sstevel@tonic-gate free (url);
767*0Sstevel@tonic-gate url = url2;
768*0Sstevel@tonic-gate
769*0Sstevel@tonic-gate } else {
770*0Sstevel@tonic-gate return (0); /* not an LDAP URL */
771*0Sstevel@tonic-gate }
772*0Sstevel@tonic-gate }
773*0Sstevel@tonic-gate return (url);
774*0Sstevel@tonic-gate }
775*0Sstevel@tonic-gate
776*0Sstevel@tonic-gate /*
777*0Sstevel@tonic-gate * Locate the LDAP URL associated with a distinguished name.
778*0Sstevel@tonic-gate *
779*0Sstevel@tonic-gate * The number of nameparts in the supplied distinguished name must be
780*0Sstevel@tonic-gate * provided. The specified directory entry is searched for a labeledURI
781*0Sstevel@tonic-gate * attribute. If successful then the LDAP URL is returned. If unsuccessful
782*0Sstevel@tonic-gate * then that entry's parent is searched and so on until the target
783*0Sstevel@tonic-gate * distinguished name is reduced to only two nameparts.
784*0Sstevel@tonic-gate *
785*0Sstevel@tonic-gate * For example, if 'l=ny,ou=eng,o=wiz,c=us' is the distinguished name
786*0Sstevel@tonic-gate * then the following entries are searched until one succeeds:
787*0Sstevel@tonic-gate * l=ny,ou=eng,o=wiz,c=us
788*0Sstevel@tonic-gate * ou=eng,o=wiz,c=us
789*0Sstevel@tonic-gate * o=wiz,c=us
790*0Sstevel@tonic-gate *
791*0Sstevel@tonic-gate * If an error is encountered then zero is returned, otherwise a string
792*0Sstevel@tonic-gate * URL is returned. The caller should free the returned string if it is
793*0Sstevel@tonic-gate * non-zero.
794*0Sstevel@tonic-gate */
795*0Sstevel@tonic-gate
796*0Sstevel@tonic-gate char *
ldap_dn_to_url(LDAP * ld,char * dn,int nameparts)797*0Sstevel@tonic-gate ldap_dn_to_url(
798*0Sstevel@tonic-gate LDAP *ld,
799*0Sstevel@tonic-gate char *dn,
800*0Sstevel@tonic-gate int nameparts
801*0Sstevel@tonic-gate )
802*0Sstevel@tonic-gate {
803*0Sstevel@tonic-gate char *next_dn = dn;
804*0Sstevel@tonic-gate char *url = 0;
805*0Sstevel@tonic-gate char *attrs[2] = {"labeledURI", 0};
806*0Sstevel@tonic-gate LDAPMessage *res, *e;
807*0Sstevel@tonic-gate char **vals;
808*0Sstevel@tonic-gate
809*0Sstevel@tonic-gate /*
810*0Sstevel@tonic-gate * Search for a URL in the named entry or its parent entry.
811*0Sstevel@tonic-gate * Continue until only 2 nameparts remain.
812*0Sstevel@tonic-gate */
813*0Sstevel@tonic-gate while (dn && (nameparts > 1) && (! url)) {
814*0Sstevel@tonic-gate
815*0Sstevel@tonic-gate /* search for the labeledURI attribute */
816*0Sstevel@tonic-gate if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE,
817*0Sstevel@tonic-gate "(objectClass=*)", attrs, 0, &res) == LDAP_SUCCESS) {
818*0Sstevel@tonic-gate
819*0Sstevel@tonic-gate /* locate the first entry returned */
820*0Sstevel@tonic-gate if ((e = ldap_first_entry(ld, res)) != NULL) {
821*0Sstevel@tonic-gate
822*0Sstevel@tonic-gate /* locate the labeledURI attribute */
823*0Sstevel@tonic-gate if ((vals =
824*0Sstevel@tonic-gate ldap_get_values(ld, e, "labeledURI")) !=
825*0Sstevel@tonic-gate NULL) {
826*0Sstevel@tonic-gate
827*0Sstevel@tonic-gate /* copy the attribute value */
828*0Sstevel@tonic-gate if ((url = strdup((char *)vals[0])) !=
829*0Sstevel@tonic-gate NULL) {
830*0Sstevel@tonic-gate ldap_value_free(vals);
831*0Sstevel@tonic-gate }
832*0Sstevel@tonic-gate }
833*0Sstevel@tonic-gate }
834*0Sstevel@tonic-gate /* free the search results */
835*0Sstevel@tonic-gate if (res != NULL) {
836*0Sstevel@tonic-gate ldap_msgfree(res);
837*0Sstevel@tonic-gate }
838*0Sstevel@tonic-gate }
839*0Sstevel@tonic-gate
840*0Sstevel@tonic-gate if (! url) {
841*0Sstevel@tonic-gate /* advance along the DN by one namepart */
842*0Sstevel@tonic-gate if (next_dn = strchr(dn, ',')) {
843*0Sstevel@tonic-gate next_dn++;
844*0Sstevel@tonic-gate dn = next_dn;
845*0Sstevel@tonic-gate nameparts--;
846*0Sstevel@tonic-gate }
847*0Sstevel@tonic-gate }
848*0Sstevel@tonic-gate }
849*0Sstevel@tonic-gate
850*0Sstevel@tonic-gate return (url);
851*0Sstevel@tonic-gate }
852*0Sstevel@tonic-gate
853*0Sstevel@tonic-gate #endif /* _SOLARIS_SDK */
854