]> git.sur5r.net Git - openldap/blob - libraries/libldap/url.c
Fix ldaps / TLS processing...
[openldap] / libraries / libldap / url.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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         if( p == NULL ) {
278                 LDAP_FREE( url );
279                 *ludpp = ludp;
280                 return LDAP_URL_SUCCESS;
281         }
282
283         /* scan forward for '?' that may marks end of dn */
284         q = strchr( p, '?' );
285
286         if( q != NULL ) {
287                 /* terminate dn part */
288                 *q++ = '\0';
289         }
290
291         if( *p != '\0' ) {
292                 /* parse dn part */
293                 ldap_pvt_hex_unescape( p );
294                 ludp->lud_dn = LDAP_STRDUP( p );
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         if( q == NULL ) {
306                 /* no more */
307                 LDAP_FREE( url );
308                 *ludpp = ludp;
309                 return LDAP_URL_SUCCESS;
310         }
311
312         /* scan forward for '?' that may marks end of attributes */
313         p = q;
314         q = strchr( p, '?' );
315
316         if( q != NULL ) {
317                 /* terminate attributes part */
318                 *q++ = '\0';
319         }
320
321         if( *p != '\0' ) {
322                 /* parse attributes */
323                 ldap_pvt_hex_unescape( p );
324                 ludp->lud_attrs = ldap_str2charray( p, "," );
325
326                 if( ludp->lud_attrs == NULL ) {
327                         LDAP_FREE( url );
328                         ldap_free_urldesc( ludp );
329                         return LDAP_URL_ERR_BADATTRS;
330                 }
331         }
332
333         if ( q == NULL ) {
334                 /* no more */
335                 LDAP_FREE( url );
336                 *ludpp = ludp;
337                 return LDAP_URL_SUCCESS;
338         }
339
340         /* scan forward for '?' that may marks end of scope */
341         p = q;
342         q = strchr( p, '?' );
343
344         if( q != NULL ) {
345                 /* terminate the scope part */
346                 *q++ = '\0';
347         }
348
349         if( *p != '\0' ) {
350                 /* parse the scope */
351                 ldap_pvt_hex_unescape( p );
352                 ludp->lud_scope = str2scope( p );
353
354                 if( ludp->lud_scope == -1 ) {
355                         LDAP_FREE( url );
356                         ldap_free_urldesc( ludp );
357                         return LDAP_URL_ERR_BADSCOPE;
358                 }
359         }
360
361         if ( q == NULL ) {
362                 /* no more */
363                 LDAP_FREE( url );
364                 *ludpp = ludp;
365                 return LDAP_URL_SUCCESS;
366         }
367
368         /* scan forward for '?' that may marks end of filter */
369         p = q;
370         q = strchr( p, '?' );
371
372         if( q != NULL ) {
373                 /* terminate the filter part */
374                 *q++ = '\0';
375         }
376
377         if( *p != '\0' ) {
378                 /* parse the filter */
379                 ldap_pvt_hex_unescape( p );
380
381                 if( ! *p ) {
382                         /* missing filter */
383                         LDAP_FREE( url );
384                         ldap_free_urldesc( ludp );
385                         return LDAP_URL_ERR_BADFILTER;
386                 }
387
388                 LDAP_FREE( ludp->lud_filter );
389                 ludp->lud_filter = LDAP_STRDUP( p );
390
391                 if( ludp->lud_filter == NULL ) {
392                         LDAP_FREE( url );
393                         ldap_free_urldesc( ludp );
394                         return LDAP_URL_ERR_MEM;
395                 }
396         }
397
398         if ( q == NULL ) {
399                 /* no more */
400                 LDAP_FREE( url );
401                 *ludpp = ludp;
402                 return LDAP_URL_SUCCESS;
403         }
404
405         /* scan forward for '?' that may marks end of extensions */
406         p = q;
407         q = strchr( p, '?' );
408
409         if( q != NULL ) {
410                 /* extra '?' */
411                 LDAP_FREE( url );
412                 ldap_free_urldesc( ludp );
413                 return LDAP_URL_ERR_BADURL;
414         }
415
416         /* parse the extensions */
417         ludp->lud_exts = ldap_str2charray( p, "," );
418
419         if( ludp->lud_exts == NULL ) {
420                 LDAP_FREE( url );
421                 ldap_free_urldesc( ludp );
422                 return LDAP_URL_ERR_BADEXTS;
423         }
424
425         for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
426                 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
427         }
428
429         if( i == 0 ) {
430                 /* must have 1 or more */
431                 ldap_charray_free( ludp->lud_exts );
432                 LDAP_FREE( url );
433                 ldap_free_urldesc( ludp );
434                 return LDAP_URL_ERR_BADEXTS;
435         }
436
437         /* no more */
438         *ludpp = ludp;
439         LDAP_FREE( url );
440         return LDAP_URL_SUCCESS;
441 }
442
443 LDAPURLDesc *
444 ldap_url_dup ( LDAPURLDesc *ludp )
445 {
446         LDAPURLDesc *dest;
447
448         if ( ludp == NULL ) {
449                 return NULL;
450         }
451
452         dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
453         if (dest == NULL)
454                 return NULL;
455         
456         *dest = *ludp;
457
458         if ( ludp->lud_host != NULL ) {
459                 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
460                 if (dest->lud_host == NULL) {
461                         ldap_free_urldesc(dest);
462                         return NULL;
463                 }
464         }
465
466         if ( ludp->lud_dn != NULL ) {
467                 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
468                 if (dest->lud_dn == NULL) {
469                         ldap_free_urldesc(dest);
470                         return NULL;
471                 }
472         }
473
474         if ( ludp->lud_filter != NULL ) {
475                 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
476                 if (dest->lud_filter == NULL) {
477                         ldap_free_urldesc(dest);
478                         return NULL;
479                 }
480         }
481
482         if ( ludp->lud_attrs != NULL ) {
483                 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
484                 if (dest->lud_attrs == NULL) {
485                         ldap_free_urldesc(dest);
486                         return NULL;
487                 }
488         }
489
490         if ( ludp->lud_exts != NULL ) {
491                 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
492                 if (dest->lud_exts == NULL) {
493                         ldap_free_urldesc(dest);
494                         return NULL;
495                 }
496         }
497
498         return dest;
499 }
500
501 LDAPURLDesc *
502 ldap_url_duplist (LDAPURLDesc *ludlist)
503 {
504         LDAPURLDesc *dest, *tail, *ludp, *newludp;
505
506         dest = NULL;
507         tail = NULL;
508         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
509                 newludp = ldap_url_dup(ludp);
510                 if (newludp == NULL) {
511                         ldap_free_urllist(dest);
512                         return NULL;
513                 }
514                 if (tail == NULL)
515                         dest = newludp;
516                 else
517                         tail->lud_next = newludp;
518                 tail = newludp;
519         }
520         return dest;
521 }
522
523 int
524 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
525 {
526         int i, rc;
527         LDAPURLDesc *ludp;
528         char **urls;
529
530         *ludlist = NULL;
531
532         if (url == NULL)
533                 return LDAP_PARAM_ERROR;
534
535         urls = ldap_str2charray((char *)url, ", ");
536         if (urls == NULL)
537                 return LDAP_NO_MEMORY;
538
539         /* count the URLs... */
540         for (i = 0; urls[i] != NULL; i++) ;
541         /* ...and put them in the "stack" backward */
542         while (--i >= 0) {
543                 rc = ldap_url_parse( urls[i], &ludp );
544                 if ( rc != 0 ) {
545                         ldap_charray_free(urls);
546                         ldap_free_urllist(*ludlist);
547                         *ludlist = NULL;
548                         return rc;
549                 }
550                 ludp->lud_next = *ludlist;
551                 *ludlist = ludp;
552         }
553         ldap_charray_free(urls);
554         return LDAP_SUCCESS;
555 }
556
557 int
558 ldap_url_parsehosts (LDAPURLDesc **ludlist, const char *hosts )
559 {
560         int i;
561         LDAPURLDesc *ludp;
562         char **specs, *p;
563
564         *ludlist = NULL;
565
566         if (hosts == NULL)
567                 return LDAP_PARAM_ERROR;
568
569         specs = ldap_str2charray((char *)hosts, ", ");
570         if (specs == NULL)
571                 return LDAP_NO_MEMORY;
572
573         /* count the URLs... */
574         for (i = 0; specs[i] != NULL; i++) ;
575         /* ...and put them in the "stack" backward */
576         while (--i >= 0) {
577                 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
578                 if (ludp == NULL) {
579                         ldap_charray_free(specs);
580                         ldap_free_urllist(*ludlist);
581                         *ludlist = NULL;
582                         return LDAP_NO_MEMORY;
583                 }
584                 ludp->lud_host = specs[i];
585                 specs[i] = NULL;
586                 p = strchr(ludp->lud_host, ':');
587                 if (p != NULL) {
588                         *p++ = 0;
589                         ldap_pvt_hex_unescape(p);
590                         ludp->lud_port = atoi(p);
591                 }
592                 ldap_pvt_hex_unescape(ludp->lud_host);
593                 ludp->lud_protocol = LDAP_PROTO_TCP;
594                 ludp->lud_properties = 0;
595                 ludp->lud_next = *ludlist;
596                 *ludlist = ludp;
597         }
598
599         /* this should be an array of NULLs now */
600         ldap_charray_free(specs);
601         return LDAP_SUCCESS;
602 }
603
604 char *
605 ldap_url_list2hosts (LDAPURLDesc *ludlist)
606 {
607         LDAPURLDesc *ludp;
608         int size;
609         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
610
611         if (ludlist == NULL)
612                 return NULL;
613
614         /* figure out how big the string is */
615         size = 1;       /* nul-term */
616         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
617                 size += strlen(ludp->lud_host) + 1;             /* host and space */
618                 if (ludp->lud_port != 0)
619                         size += sprintf(buf, ":%d", ludp->lud_port);
620         }
621         s = LDAP_MALLOC(size);
622         if (s == NULL)
623                 return NULL;
624
625         p = s;
626         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
627                 strcpy(p, ludp->lud_host);
628                 p += strlen(ludp->lud_host);
629                 if (ludp->lud_port != 0)
630                         p += sprintf(p, ":%d", ludp->lud_port);
631                 *p++ = ' ';
632         }
633         if (p != s)
634                 p--;    /* nuke that extra space */
635         *p = 0;
636         return s;
637 }
638
639 char *
640 ldap_url_list2urls (LDAPURLDesc *ludlist)
641 {
642         LDAPURLDesc *ludp;
643         int size;
644         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
645
646         if (ludlist == NULL)
647                 return NULL;
648
649         /* figure out how big the string is */
650         size = 1;       /* nul-term */
651         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
652                 size += strlen(ludp->lud_host) + 1 + sizeof("ldapis:///");      /* prefix, host, /, and space */
653                 if (ludp->lud_port != 0)
654                         size += sprintf(buf, ":%d", ludp->lud_port);
655         }
656         s = LDAP_MALLOC(size);
657         if (s == NULL)
658                 return NULL;
659
660         p = s;
661         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
662                 p += sprintf(p, "ldap%s://%s", (ludp->lud_properties & LDAP_URL_USE_SSL) ? "s" : "", ludp->lud_host);
663                 if (ludp->lud_port != 0)
664                         p += sprintf(p, ":%d", ludp->lud_port);
665                 *p++ = '/';
666                 *p++ = ' ';
667         }
668         if (p != s)
669                 p--;    /* nuke that extra space */
670         *p = 0;
671         return s;
672 }
673
674 void
675 ldap_free_urllist( LDAPURLDesc *ludlist )
676 {
677         LDAPURLDesc *ludp, *next;
678
679         for (ludp = ludlist; ludp != NULL; ludp = next) {
680                 next = ludp->lud_next;
681                 ldap_free_urldesc(ludp);
682         }
683 }
684
685 void
686 ldap_free_urldesc( LDAPURLDesc *ludp )
687 {
688         if ( ludp == NULL ) {
689                 return;
690         }
691         
692         if ( ludp->lud_host != NULL ) {
693                 LDAP_FREE( ludp->lud_host );
694         }
695
696         if ( ludp->lud_dn != NULL ) {
697                 LDAP_FREE( ludp->lud_dn );
698         }
699
700         if ( ludp->lud_filter != NULL ) {
701                 LDAP_FREE( ludp->lud_filter);
702         }
703
704         if ( ludp->lud_attrs != NULL ) {
705                 LDAP_VFREE( ludp->lud_attrs );
706         }
707
708         if ( ludp->lud_exts != NULL ) {
709                 LDAP_VFREE( ludp->lud_exts );
710         }
711
712         LDAP_FREE( ludp );
713 }
714
715
716
717 int
718 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
719 {
720         int             err;
721         LDAPURLDesc     *ludp;
722         BerElement      *ber;
723
724         if ( ldap_url_parse( url, &ludp ) != 0 ) {
725                 ld->ld_errno = LDAP_PARAM_ERROR;
726                 return( -1 );
727         }
728
729         ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
730             ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
731                 -1, -1 );
732
733         if ( ber == NULL ) {
734                 err = -1;
735         } else {
736                 err = ldap_send_server_request(
737                                         ld, ber, ld->ld_msgid, NULL,
738                                         (ludp->lud_host != NULL || ludp->lud_port != 0)
739                                                 ? ludp : NULL,
740                                         NULL, 1 );
741         }
742
743         ldap_free_urldesc( ludp );
744         return( err );
745 }
746
747
748 int
749 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
750         struct timeval *timeout, LDAPMessage **res )
751 {
752         int     msgid;
753
754         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
755                 return( ld->ld_errno );
756         }
757
758         if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
759                 return( ld->ld_errno );
760         }
761
762         if ( ld->ld_errno == LDAP_TIMEOUT ) {
763                 (void) ldap_abandon( ld, msgid );
764                 ld->ld_errno = LDAP_TIMEOUT;
765                 return( ld->ld_errno );
766         }
767
768         return( ldap_result2error( ld, *res, 0 ));
769 }
770
771
772 int
773 ldap_url_search_s(
774         LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
775 {
776         int     msgid;
777
778         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
779                 return( ld->ld_errno );
780         }
781
782         if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
783                 return( ld->ld_errno );
784         }
785
786         return( ldap_result2error( ld, *res, 0 ));
787 }
788
789
790 void
791 ldap_pvt_hex_unescape( char *s )
792 {
793 /*
794 * Remove URL hex escapes from s... done in place.  The basic concept for
795 * this routine is borrowed from the WWW library HTUnEscape() routine.
796 */
797         char    *p;
798
799         for ( p = s; *s != '\0'; ++s ) {
800                 if ( *s == '%' ) {
801                         if ( *++s != '\0' ) {
802                                 *p = ldap_pvt_unhex( *s ) << 4;
803                         }
804                         if ( *++s != '\0' ) {
805                                 *p++ += ldap_pvt_unhex( *s );
806                         }
807                 } else {
808                         *p++ = *s;
809                 }
810         }
811
812         *p = '\0';
813 }
814
815
816 int
817 ldap_pvt_unhex( int c )
818 {
819         return( c >= '0' && c <= '9' ? c - '0'
820             : c >= 'A' && c <= 'F' ? c - 'A' + 10
821             : c - 'a' + 10 );
822 }