]> git.sur5r.net Git - openldap/blob - libraries/libldap/url.c
6098bb8910cbaa2b088dc29abb19e317c8628bd2
[openldap] / libraries / libldap / url.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1996 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  LIBLDAP url.c -- LDAP URL (RFC 2255) related routines
11  *
12  *  LDAP URLs look like this:
13  *    ldap[s]://host:port[/[dn[?[attributes][?[scope][?[filter][?exts]]]]]]
14  *
15  *  where:
16  *   attributes is a comma separated list
17  *   scope is one of these three strings:  base one sub (default=base)
18  *   filter is an string-represented filter as in RFC 2254
19  *
20  *  e.g.,  ldap://host:port/dc=com?o,cn?base?o=openldap?extension
21  *
22  *  We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
23  */
24
25 #include "portable.h"
26
27 #include <stdio.h>
28
29 #include <ac/stdlib.h>
30
31 #include <ac/ctype.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
34 #include <ac/time.h>
35
36 #include "ldap-int.h"
37
38
39 /* local functions */
40 static const char* skip_url_prefix LDAP_P((
41         const char *url,
42         int *enclosedp,
43         unsigned long *properties,
44         int *protocol));
45
46
47 int
48 ldap_is_ldap_url( LDAP_CONST char *url )
49 {
50         int     enclosed, protocol;
51         unsigned long properties;
52
53         if( url == NULL ) {
54                 return 0;
55         }
56
57         if( skip_url_prefix( url, &enclosed, &properties, &protocol) == NULL ) {
58                 return 0;
59         }
60
61         return !(properties & LDAP_URL_USE_SSL);
62 }
63
64 int
65 ldap_is_ldaps_url( LDAP_CONST char *url )
66 {
67         int     enclosed, protocol;
68         unsigned long properties;
69
70         if( url == NULL ) {
71                 return 0;
72         }
73
74         if( skip_url_prefix( url, &enclosed, &properties, &protocol) == NULL ) {
75                 return 0;
76         }
77
78         return (properties & LDAP_URL_USE_SSL);
79 }
80
81 static const char*
82 skip_url_prefix(
83         const char *url,
84         int *enclosedp,
85         unsigned long *properties,
86         int *protocol
87         )
88 {
89 /*
90  * return non-zero if this looks like a LDAP URL; zero if not
91  * if non-zero returned, *urlp will be moved past "ldap://" part of URL
92  */
93         const char *p;
94
95         if ( url == NULL ) {
96                 return( NULL );
97         }
98
99         p = url;
100
101         /* skip leading '<' (if any) */
102         if ( *p == '<' ) {
103                 *enclosedp = 1;
104                 ++p;
105         } else {
106                 *enclosedp = 0;
107         }
108
109         /* skip leading "URL:" (if any) */
110         if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 )
111         {
112                 p += LDAP_URL_URLCOLON_LEN;
113         }
114
115         *properties = 0;
116
117         /* check for "ldap://" prefix */
118         if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
119                 /* skip over "ldap://" prefix and return success */
120                 p += LDAP_URL_PREFIX_LEN;
121                 *protocol = LDAP_PROTO_TCP;
122                 return( p );
123         }
124
125         /* check for "ldaps://" prefix */
126         if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
127                 /* skip over "ldaps://" prefix and return success */
128                 p += LDAPS_URL_PREFIX_LEN;
129                 *protocol = LDAP_PROTO_TCP;
130                 *properties |= LDAP_URL_USE_SSL;
131                 return( p );
132         }
133
134         /* check for "ldapi://" prefix */
135         if ( strncasecmp( p, LDAPI_URL_PREFIX, LDAPI_URL_PREFIX_LEN ) == 0 ) {
136                 /* skip over "ldapi://" prefix and return success */
137                 p += LDAPI_URL_PREFIX_LEN;
138                 *protocol = LDAP_PROTO_LOCAL;
139                 return( p );
140         }
141
142         /* check for "ldapis://" prefix: should this be legal? */
143         if ( strncasecmp( p, LDAPIS_URL_PREFIX, LDAPIS_URL_PREFIX_LEN ) == 0 ) {
144                 /* skip over "ldapis://" prefix and return success */
145                 p += LDAPIS_URL_PREFIX_LEN;
146                 *protocol = LDAP_PROTO_LOCAL;
147                 *properties |= LDAP_URL_USE_SSL;
148                 return( p );
149         }
150
151         return( NULL );
152 }
153
154
155 static int str2scope( const char *p )
156 {
157         if ( strcasecmp( p, "one" ) == 0 ) {
158                 return LDAP_SCOPE_ONELEVEL;
159
160         } else if ( strcasecmp( p, "onetree" ) == 0 ) {
161                 return LDAP_SCOPE_ONELEVEL;
162
163         } else if ( strcasecmp( p, "base" ) == 0 ) {
164                 return LDAP_SCOPE_BASE;
165
166         } else if ( strcasecmp( p, "sub" ) == 0 ) {
167                 return LDAP_SCOPE_SUBTREE;
168
169         } else if ( strcasecmp( p, "subtree" ) == 0 ) {
170                 return LDAP_SCOPE_SUBTREE;
171         }
172
173         return( -1 );
174 }
175
176
177 int
178 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
179 {
180 /*
181  *  Pick apart the pieces of an LDAP URL.
182  */
183
184         LDAPURLDesc     *ludp;
185         char    *p, *q;
186         int             i, enclosed, protocol;
187         unsigned long properties;
188         const char *url_tmp;
189         char *url;
190
191         if( url_in == NULL && ludpp == NULL ) {
192                 return LDAP_URL_ERR_PARAM;
193         }
194
195         Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
196
197         *ludpp = NULL;  /* pessimistic */
198
199         url_tmp = skip_url_prefix( url_in, &enclosed, &properties, &protocol );
200
201         if ( url_tmp == NULL ) {
202                 return LDAP_URL_ERR_NOTLDAP;
203         }
204
205         /* make working copy of the remainder of the URL */
206         if (( url = LDAP_STRDUP( url_tmp )) == NULL ) {
207                 return( LDAP_URL_ERR_MEM );
208         }
209
210         if ( enclosed ) {
211                 p = &url[strlen(url)-1];
212
213                 if( *p != '>' ) {
214                         LDAP_FREE( url );
215                         return LDAP_URL_ERR_BADENCLOSURE;
216                 }
217
218                 *p = '\0';
219         }
220
221         /* allocate return struct */
222         ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
223
224         if ( ludp == NULL ) {
225                 LDAP_FREE( url );
226                 return LDAP_URL_ERR_MEM;
227         }
228
229         ludp->lud_next = NULL;
230         ludp->lud_host = NULL;
231         ludp->lud_port = 0;
232         ludp->lud_dn = NULL;
233         ludp->lud_attrs = NULL;
234         ludp->lud_filter = NULL;
235         ludp->lud_properties = properties;
236         ludp->lud_protocol = protocol;
237         ludp->lud_scope = LDAP_SCOPE_BASE;
238
239         ludp->lud_filter = LDAP_STRDUP("(objectClass=*)");
240
241         if( ludp->lud_filter == NULL ) {
242                 LDAP_FREE( url );
243                 ldap_free_urldesc( ludp );
244                 return LDAP_URL_ERR_MEM;
245         }
246
247         /* scan forward for '/' that marks end of hostport and begin. of dn */
248         p = strchr( url, '/' );
249
250         if( p != NULL ) {
251                 /* terminate hostport; point to start of dn */
252                 *p++ = '\0';
253         }
254
255         if (( q = strchr( url, ':' )) != NULL ) {
256                 *q++ = '\0';
257                 ldap_pvt_hex_unescape( q );
258
259                 if( *q == '\0' ) {
260                         LDAP_FREE( url );
261                         ldap_free_urldesc( ludp );
262                         return LDAP_URL_ERR_BADURL;
263                 }
264
265                 ludp->lud_port = atoi( q );
266         }
267
268         ldap_pvt_hex_unescape( url );
269         ludp->lud_host = LDAP_STRDUP( url );
270
271         if( ludp->lud_host == NULL ) {
272                 LDAP_FREE( url );
273                 ldap_free_urldesc( ludp );
274                 return LDAP_URL_ERR_MEM;
275         }
276
277         /*
278          * Kluge.  ldap://111.222.333.444:389??cn=abc,o=company
279          *
280          * On early Novell releases, search references/referrals were returned
281          * in this format, i.e., the dn was kind of in the scope position,
282          * but the required slash is missing. The whole thing is illegal syntax,
283          * but we need to account for it. Fortunately it can't be confused with
284          * anything real.
285          */
286         if( (p == NULL) && ((q = strchr( q, '?')) != NULL)) {
287                 q++;            
288                 /* ? immediately followed by question */
289                 if( *q == '?') {
290                         q++;
291                         if( *q != '\0' ) {
292                                 /* parse dn part */
293                                 ldap_pvt_hex_unescape( q );
294                                 ludp->lud_dn = LDAP_STRDUP( q );
295                         } else {
296                                 ludp->lud_dn = LDAP_STRDUP( "" );
297                         }
298
299                         if( ludp->lud_dn == NULL ) {
300                                 LDAP_FREE( url );
301                                 ldap_free_urldesc( ludp );
302                                 return LDAP_URL_ERR_MEM;
303                         }
304                 }
305         }
306
307         if( p == NULL ) {
308                 LDAP_FREE( url );
309                 *ludpp = ludp;
310                 return LDAP_URL_SUCCESS;
311         }
312
313         /* scan forward for '?' that may marks end of dn */
314         q = strchr( p, '?' );
315
316         if( q != NULL ) {
317                 /* terminate dn part */
318                 *q++ = '\0';
319         }
320
321         if( *p != '\0' ) {
322                 /* parse dn part */
323                 ldap_pvt_hex_unescape( p );
324                 ludp->lud_dn = LDAP_STRDUP( p );
325         } else {
326                 ludp->lud_dn = LDAP_STRDUP( "" );
327         }
328
329         if( ludp->lud_dn == NULL ) {
330                 LDAP_FREE( url );
331                 ldap_free_urldesc( ludp );
332                 return LDAP_URL_ERR_MEM;
333         }
334
335         if( q == NULL ) {
336                 /* no more */
337                 LDAP_FREE( url );
338                 *ludpp = ludp;
339                 return LDAP_URL_SUCCESS;
340         }
341
342         /* scan forward for '?' that may marks end of attributes */
343         p = q;
344         q = strchr( p, '?' );
345
346         if( q != NULL ) {
347                 /* terminate attributes part */
348                 *q++ = '\0';
349         }
350
351         if( *p != '\0' ) {
352                 /* parse attributes */
353                 ldap_pvt_hex_unescape( p );
354                 ludp->lud_attrs = ldap_str2charray( p, "," );
355
356                 if( ludp->lud_attrs == NULL ) {
357                         LDAP_FREE( url );
358                         ldap_free_urldesc( ludp );
359                         return LDAP_URL_ERR_BADATTRS;
360                 }
361         }
362
363         if ( q == NULL ) {
364                 /* no more */
365                 LDAP_FREE( url );
366                 *ludpp = ludp;
367                 return LDAP_URL_SUCCESS;
368         }
369
370         /* scan forward for '?' that may marks end of scope */
371         p = q;
372         q = strchr( p, '?' );
373
374         if( q != NULL ) {
375                 /* terminate the scope part */
376                 *q++ = '\0';
377         }
378
379         if( *p != '\0' ) {
380                 /* parse the scope */
381                 ldap_pvt_hex_unescape( p );
382                 ludp->lud_scope = str2scope( p );
383
384                 if( ludp->lud_scope == -1 ) {
385                         LDAP_FREE( url );
386                         ldap_free_urldesc( ludp );
387                         return LDAP_URL_ERR_BADSCOPE;
388                 }
389         }
390
391         if ( q == NULL ) {
392                 /* no more */
393                 LDAP_FREE( url );
394                 *ludpp = ludp;
395                 return LDAP_URL_SUCCESS;
396         }
397
398         /* scan forward for '?' that may marks end of filter */
399         p = q;
400         q = strchr( p, '?' );
401
402         if( q != NULL ) {
403                 /* terminate the filter part */
404                 *q++ = '\0';
405         }
406
407         if( *p != '\0' ) {
408                 /* parse the filter */
409                 ldap_pvt_hex_unescape( p );
410
411                 if( ! *p ) {
412                         /* missing filter */
413                         LDAP_FREE( url );
414                         ldap_free_urldesc( ludp );
415                         return LDAP_URL_ERR_BADFILTER;
416                 }
417
418                 LDAP_FREE( ludp->lud_filter );
419                 ludp->lud_filter = LDAP_STRDUP( p );
420
421                 if( ludp->lud_filter == NULL ) {
422                         LDAP_FREE( url );
423                         ldap_free_urldesc( ludp );
424                         return LDAP_URL_ERR_MEM;
425                 }
426         }
427
428         if ( q == NULL ) {
429                 /* no more */
430                 LDAP_FREE( url );
431                 *ludpp = ludp;
432                 return LDAP_URL_SUCCESS;
433         }
434
435         /* scan forward for '?' that may marks end of extensions */
436         p = q;
437         q = strchr( p, '?' );
438
439         if( q != NULL ) {
440                 /* extra '?' */
441                 LDAP_FREE( url );
442                 ldap_free_urldesc( ludp );
443                 return LDAP_URL_ERR_BADURL;
444         }
445
446         /* parse the extensions */
447         ludp->lud_exts = ldap_str2charray( p, "," );
448
449         if( ludp->lud_exts == NULL ) {
450                 LDAP_FREE( url );
451                 ldap_free_urldesc( ludp );
452                 return LDAP_URL_ERR_BADEXTS;
453         }
454
455         for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
456                 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
457         }
458
459         if( i == 0 ) {
460                 /* must have 1 or more */
461                 ldap_charray_free( ludp->lud_exts );
462                 LDAP_FREE( url );
463                 ldap_free_urldesc( ludp );
464                 return LDAP_URL_ERR_BADEXTS;
465         }
466
467         /* no more */
468         *ludpp = ludp;
469         LDAP_FREE( url );
470         return LDAP_URL_SUCCESS;
471 }
472
473 LDAPURLDesc *
474 ldap_url_dup ( LDAPURLDesc *ludp )
475 {
476         LDAPURLDesc *dest;
477
478         if ( ludp == NULL ) {
479                 return NULL;
480         }
481
482         dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
483         if (dest == NULL)
484                 return NULL;
485         
486         *dest = *ludp;
487         dest->lud_next = NULL;
488
489         if ( ludp->lud_host != NULL ) {
490                 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
491                 if (dest->lud_host == NULL) {
492                         ldap_free_urldesc(dest);
493                         return NULL;
494                 }
495         }
496
497         if ( ludp->lud_dn != NULL ) {
498                 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
499                 if (dest->lud_dn == NULL) {
500                         ldap_free_urldesc(dest);
501                         return NULL;
502                 }
503         }
504
505         if ( ludp->lud_filter != NULL ) {
506                 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
507                 if (dest->lud_filter == NULL) {
508                         ldap_free_urldesc(dest);
509                         return NULL;
510                 }
511         }
512
513         if ( ludp->lud_attrs != NULL ) {
514                 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
515                 if (dest->lud_attrs == NULL) {
516                         ldap_free_urldesc(dest);
517                         return NULL;
518                 }
519         }
520
521         if ( ludp->lud_exts != NULL ) {
522                 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
523                 if (dest->lud_exts == NULL) {
524                         ldap_free_urldesc(dest);
525                         return NULL;
526                 }
527         }
528
529         return dest;
530 }
531
532 LDAPURLDesc *
533 ldap_url_duplist (LDAPURLDesc *ludlist)
534 {
535         LDAPURLDesc *dest, *tail, *ludp, *newludp;
536
537         dest = NULL;
538         tail = NULL;
539         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
540                 newludp = ldap_url_dup(ludp);
541                 if (newludp == NULL) {
542                         ldap_free_urllist(dest);
543                         return NULL;
544                 }
545                 if (tail == NULL)
546                         dest = newludp;
547                 else
548                         tail->lud_next = newludp;
549                 tail = newludp;
550         }
551         return dest;
552 }
553
554 int
555 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
556 {
557         int i, rc;
558         LDAPURLDesc *ludp;
559         char **urls;
560
561         *ludlist = NULL;
562
563         if (url == NULL)
564                 return LDAP_PARAM_ERROR;
565
566         urls = ldap_str2charray((char *)url, ", ");
567         if (urls == NULL)
568                 return LDAP_NO_MEMORY;
569
570         /* count the URLs... */
571         for (i = 0; urls[i] != NULL; i++) ;
572         /* ...and put them in the "stack" backward */
573         while (--i >= 0) {
574                 rc = ldap_url_parse( urls[i], &ludp );
575                 if ( rc != 0 ) {
576                         ldap_charray_free(urls);
577                         ldap_free_urllist(*ludlist);
578                         *ludlist = NULL;
579                         return rc;
580                 }
581                 ludp->lud_next = *ludlist;
582                 *ludlist = ludp;
583         }
584         ldap_charray_free(urls);
585         return LDAP_SUCCESS;
586 }
587
588 int
589 ldap_url_parsehosts (LDAPURLDesc **ludlist, const char *hosts )
590 {
591         int i;
592         LDAPURLDesc *ludp;
593         char **specs, *p;
594
595         *ludlist = NULL;
596
597         if (hosts == NULL)
598                 return LDAP_PARAM_ERROR;
599
600         specs = ldap_str2charray((char *)hosts, ", ");
601         if (specs == NULL)
602                 return LDAP_NO_MEMORY;
603
604         /* count the URLs... */
605         for (i = 0; specs[i] != NULL; i++) ;
606         /* ...and put them in the "stack" backward */
607         while (--i >= 0) {
608                 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
609                 if (ludp == NULL) {
610                         ldap_charray_free(specs);
611                         ldap_free_urllist(*ludlist);
612                         *ludlist = NULL;
613                         return LDAP_NO_MEMORY;
614                 }
615                 ludp->lud_host = specs[i];
616                 specs[i] = NULL;
617                 p = strchr(ludp->lud_host, ':');
618                 if (p != NULL) {
619                         *p++ = 0;
620                         ldap_pvt_hex_unescape(p);
621                         ludp->lud_port = atoi(p);
622                 }
623                 ldap_pvt_hex_unescape(ludp->lud_host);
624                 ludp->lud_protocol = LDAP_PROTO_TCP;
625                 ludp->lud_properties = 0;
626                 ludp->lud_next = *ludlist;
627                 *ludlist = ludp;
628         }
629
630         /* this should be an array of NULLs now */
631         ldap_charray_free(specs);
632         return LDAP_SUCCESS;
633 }
634
635 char *
636 ldap_url_list2hosts (LDAPURLDesc *ludlist)
637 {
638         LDAPURLDesc *ludp;
639         int size;
640         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
641
642         if (ludlist == NULL)
643                 return NULL;
644
645         /* figure out how big the string is */
646         size = 1;       /* nul-term */
647         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
648                 size += strlen(ludp->lud_host) + 1;             /* host and space */
649                 if (ludp->lud_port != 0)
650                         size += sprintf(buf, ":%d", ludp->lud_port);
651         }
652         s = LDAP_MALLOC(size);
653         if (s == NULL)
654                 return NULL;
655
656         p = s;
657         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
658                 strcpy(p, ludp->lud_host);
659                 p += strlen(ludp->lud_host);
660                 if (ludp->lud_port != 0)
661                         p += sprintf(p, ":%d", ludp->lud_port);
662                 *p++ = ' ';
663         }
664         if (p != s)
665                 p--;    /* nuke that extra space */
666         *p = 0;
667         return s;
668 }
669
670 char *
671 ldap_url_list2urls (LDAPURLDesc *ludlist)
672 {
673         LDAPURLDesc *ludp;
674         int size;
675         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
676
677         if (ludlist == NULL)
678                 return NULL;
679
680         /* figure out how big the string is */
681         size = 1;       /* nul-term */
682         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
683                 size += strlen(ludp->lud_host) + 1 + sizeof("ldapis:///");      /* prefix, host, /, and space */
684                 if (ludp->lud_port != 0)
685                         size += sprintf(buf, ":%d", ludp->lud_port);
686         }
687         s = LDAP_MALLOC(size);
688         if (s == NULL)
689                 return NULL;
690
691         p = s;
692         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
693                 p += sprintf(p, "ldap%s://%s", (ludp->lud_properties & LDAP_URL_USE_SSL) ? "s" : "", ludp->lud_host);
694                 if (ludp->lud_port != 0)
695                         p += sprintf(p, ":%d", ludp->lud_port);
696                 *p++ = '/';
697                 *p++ = ' ';
698         }
699         if (p != s)
700                 p--;    /* nuke that extra space */
701         *p = 0;
702         return s;
703 }
704
705 void
706 ldap_free_urllist( LDAPURLDesc *ludlist )
707 {
708         LDAPURLDesc *ludp, *next;
709
710         for (ludp = ludlist; ludp != NULL; ludp = next) {
711                 next = ludp->lud_next;
712                 ldap_free_urldesc(ludp);
713         }
714 }
715
716 void
717 ldap_free_urldesc( LDAPURLDesc *ludp )
718 {
719         if ( ludp == NULL ) {
720                 return;
721         }
722         
723         if ( ludp->lud_host != NULL ) {
724                 LDAP_FREE( ludp->lud_host );
725         }
726
727         if ( ludp->lud_dn != NULL ) {
728                 LDAP_FREE( ludp->lud_dn );
729         }
730
731         if ( ludp->lud_filter != NULL ) {
732                 LDAP_FREE( ludp->lud_filter);
733         }
734
735         if ( ludp->lud_attrs != NULL ) {
736                 LDAP_VFREE( ludp->lud_attrs );
737         }
738
739         if ( ludp->lud_exts != NULL ) {
740                 LDAP_VFREE( ludp->lud_exts );
741         }
742
743         LDAP_FREE( ludp );
744 }
745
746
747
748 int
749 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
750 {
751         int             err;
752         LDAPURLDesc     *ludp;
753         BerElement      *ber;
754         LDAPreqinfo  bind;
755
756         if ( ldap_url_parse( url, &ludp ) != 0 ) {
757                 ld->ld_errno = LDAP_PARAM_ERROR;
758                 return( -1 );
759         }
760
761         ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
762             ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
763                 -1, -1 );
764
765         if ( ber == NULL ) {
766                 err = -1;
767         } else {
768                 bind.ri_request = LDAP_REQ_SEARCH;
769                 bind.ri_msgid = ld->ld_msgid;
770                 bind.ri_url = (char *)url;
771                 err = ldap_send_server_request(
772                                         ld, ber, ld->ld_msgid, NULL,
773                                         (ludp->lud_host != NULL || ludp->lud_port != 0)
774                                                 ? ludp : NULL,
775                                         NULL, &bind );
776         }
777
778         ldap_free_urldesc( ludp );
779         return( err );
780 }
781
782
783 int
784 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
785         struct timeval *timeout, LDAPMessage **res )
786 {
787         int     msgid;
788
789         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
790                 return( ld->ld_errno );
791         }
792
793         if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
794                 return( ld->ld_errno );
795         }
796
797         if ( ld->ld_errno == LDAP_TIMEOUT ) {
798                 (void) ldap_abandon( ld, msgid );
799                 ld->ld_errno = LDAP_TIMEOUT;
800                 return( ld->ld_errno );
801         }
802
803         return( ldap_result2error( ld, *res, 0 ));
804 }
805
806
807 int
808 ldap_url_search_s(
809         LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
810 {
811         int     msgid;
812
813         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
814                 return( ld->ld_errno );
815         }
816
817         if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
818                 return( ld->ld_errno );
819         }
820
821         return( ldap_result2error( ld, *res, 0 ));
822 }
823
824
825 void
826 ldap_pvt_hex_unescape( char *s )
827 {
828 /*
829 * Remove URL hex escapes from s... done in place.  The basic concept for
830 * this routine is borrowed from the WWW library HTUnEscape() routine.
831 */
832         char    *p;
833
834         for ( p = s; *s != '\0'; ++s ) {
835                 if ( *s == '%' ) {
836                         if ( *++s != '\0' ) {
837                                 *p = ldap_pvt_unhex( *s ) << 4;
838                         }
839                         if ( *++s != '\0' ) {
840                                 *p++ += ldap_pvt_unhex( *s );
841                         }
842                 } else {
843                         *p++ = *s;
844                 }
845         }
846
847         *p = '\0';
848 }
849
850
851 int
852 ldap_pvt_unhex( int c )
853 {
854         return( c >= '0' && c <= '9' ? c - '0'
855             : c >= 'A' && c <= 'F' ? c - 'A' + 10
856             : c - 'a' + 10 );
857 }