]> git.sur5r.net Git - openldap/blob - libraries/libldap/url.c
Remove lint
[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         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         dest->lud_next = NULL;
458
459         if ( ludp->lud_host != NULL ) {
460                 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
461                 if (dest->lud_host == NULL) {
462                         ldap_free_urldesc(dest);
463                         return NULL;
464                 }
465         }
466
467         if ( ludp->lud_dn != NULL ) {
468                 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
469                 if (dest->lud_dn == NULL) {
470                         ldap_free_urldesc(dest);
471                         return NULL;
472                 }
473         }
474
475         if ( ludp->lud_filter != NULL ) {
476                 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
477                 if (dest->lud_filter == NULL) {
478                         ldap_free_urldesc(dest);
479                         return NULL;
480                 }
481         }
482
483         if ( ludp->lud_attrs != NULL ) {
484                 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
485                 if (dest->lud_attrs == NULL) {
486                         ldap_free_urldesc(dest);
487                         return NULL;
488                 }
489         }
490
491         if ( ludp->lud_exts != NULL ) {
492                 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
493                 if (dest->lud_exts == NULL) {
494                         ldap_free_urldesc(dest);
495                         return NULL;
496                 }
497         }
498
499         return dest;
500 }
501
502 LDAPURLDesc *
503 ldap_url_duplist (LDAPURLDesc *ludlist)
504 {
505         LDAPURLDesc *dest, *tail, *ludp, *newludp;
506
507         dest = NULL;
508         tail = NULL;
509         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
510                 newludp = ldap_url_dup(ludp);
511                 if (newludp == NULL) {
512                         ldap_free_urllist(dest);
513                         return NULL;
514                 }
515                 if (tail == NULL)
516                         dest = newludp;
517                 else
518                         tail->lud_next = newludp;
519                 tail = newludp;
520         }
521         return dest;
522 }
523
524 int
525 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
526 {
527         int i, rc;
528         LDAPURLDesc *ludp;
529         char **urls;
530
531         *ludlist = NULL;
532
533         if (url == NULL)
534                 return LDAP_PARAM_ERROR;
535
536         urls = ldap_str2charray((char *)url, ", ");
537         if (urls == NULL)
538                 return LDAP_NO_MEMORY;
539
540         /* count the URLs... */
541         for (i = 0; urls[i] != NULL; i++) ;
542         /* ...and put them in the "stack" backward */
543         while (--i >= 0) {
544                 rc = ldap_url_parse( urls[i], &ludp );
545                 if ( rc != 0 ) {
546                         ldap_charray_free(urls);
547                         ldap_free_urllist(*ludlist);
548                         *ludlist = NULL;
549                         return rc;
550                 }
551                 ludp->lud_next = *ludlist;
552                 *ludlist = ludp;
553         }
554         ldap_charray_free(urls);
555         return LDAP_SUCCESS;
556 }
557
558 int
559 ldap_url_parsehosts (LDAPURLDesc **ludlist, const char *hosts )
560 {
561         int i;
562         LDAPURLDesc *ludp;
563         char **specs, *p;
564
565         *ludlist = NULL;
566
567         if (hosts == NULL)
568                 return LDAP_PARAM_ERROR;
569
570         specs = ldap_str2charray((char *)hosts, ", ");
571         if (specs == NULL)
572                 return LDAP_NO_MEMORY;
573
574         /* count the URLs... */
575         for (i = 0; specs[i] != NULL; i++) ;
576         /* ...and put them in the "stack" backward */
577         while (--i >= 0) {
578                 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
579                 if (ludp == NULL) {
580                         ldap_charray_free(specs);
581                         ldap_free_urllist(*ludlist);
582                         *ludlist = NULL;
583                         return LDAP_NO_MEMORY;
584                 }
585                 ludp->lud_host = specs[i];
586                 specs[i] = NULL;
587                 p = strchr(ludp->lud_host, ':');
588                 if (p != NULL) {
589                         *p++ = 0;
590                         ldap_pvt_hex_unescape(p);
591                         ludp->lud_port = atoi(p);
592                 }
593                 ldap_pvt_hex_unescape(ludp->lud_host);
594                 ludp->lud_protocol = LDAP_PROTO_TCP;
595                 ludp->lud_properties = 0;
596                 ludp->lud_next = *ludlist;
597                 *ludlist = ludp;
598         }
599
600         /* this should be an array of NULLs now */
601         ldap_charray_free(specs);
602         return LDAP_SUCCESS;
603 }
604
605 char *
606 ldap_url_list2hosts (LDAPURLDesc *ludlist)
607 {
608         LDAPURLDesc *ludp;
609         int size;
610         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
611
612         if (ludlist == NULL)
613                 return NULL;
614
615         /* figure out how big the string is */
616         size = 1;       /* nul-term */
617         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
618                 size += strlen(ludp->lud_host) + 1;             /* host and space */
619                 if (ludp->lud_port != 0)
620                         size += sprintf(buf, ":%d", ludp->lud_port);
621         }
622         s = LDAP_MALLOC(size);
623         if (s == NULL)
624                 return NULL;
625
626         p = s;
627         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
628                 strcpy(p, ludp->lud_host);
629                 p += strlen(ludp->lud_host);
630                 if (ludp->lud_port != 0)
631                         p += sprintf(p, ":%d", ludp->lud_port);
632                 *p++ = ' ';
633         }
634         if (p != s)
635                 p--;    /* nuke that extra space */
636         *p = 0;
637         return s;
638 }
639
640 char *
641 ldap_url_list2urls (LDAPURLDesc *ludlist)
642 {
643         LDAPURLDesc *ludp;
644         int size;
645         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
646
647         if (ludlist == NULL)
648                 return NULL;
649
650         /* figure out how big the string is */
651         size = 1;       /* nul-term */
652         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
653                 size += strlen(ludp->lud_host) + 1 + sizeof("ldapis:///");      /* prefix, host, /, and space */
654                 if (ludp->lud_port != 0)
655                         size += sprintf(buf, ":%d", ludp->lud_port);
656         }
657         s = LDAP_MALLOC(size);
658         if (s == NULL)
659                 return NULL;
660
661         p = s;
662         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
663                 p += sprintf(p, "ldap%s://%s", (ludp->lud_properties & LDAP_URL_USE_SSL) ? "s" : "", ludp->lud_host);
664                 if (ludp->lud_port != 0)
665                         p += sprintf(p, ":%d", ludp->lud_port);
666                 *p++ = '/';
667                 *p++ = ' ';
668         }
669         if (p != s)
670                 p--;    /* nuke that extra space */
671         *p = 0;
672         return s;
673 }
674
675 void
676 ldap_free_urllist( LDAPURLDesc *ludlist )
677 {
678         LDAPURLDesc *ludp, *next;
679
680         for (ludp = ludlist; ludp != NULL; ludp = next) {
681                 next = ludp->lud_next;
682                 ldap_free_urldesc(ludp);
683         }
684 }
685
686 void
687 ldap_free_urldesc( LDAPURLDesc *ludp )
688 {
689         if ( ludp == NULL ) {
690                 return;
691         }
692         
693         if ( ludp->lud_host != NULL ) {
694                 LDAP_FREE( ludp->lud_host );
695         }
696
697         if ( ludp->lud_dn != NULL ) {
698                 LDAP_FREE( ludp->lud_dn );
699         }
700
701         if ( ludp->lud_filter != NULL ) {
702                 LDAP_FREE( ludp->lud_filter);
703         }
704
705         if ( ludp->lud_attrs != NULL ) {
706                 LDAP_VFREE( ludp->lud_attrs );
707         }
708
709         if ( ludp->lud_exts != NULL ) {
710                 LDAP_VFREE( ludp->lud_exts );
711         }
712
713         LDAP_FREE( ludp );
714 }
715
716
717
718 int
719 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
720 {
721         int             err;
722         LDAPURLDesc     *ludp;
723         BerElement      *ber;
724
725         if ( ldap_url_parse( url, &ludp ) != 0 ) {
726                 ld->ld_errno = LDAP_PARAM_ERROR;
727                 return( -1 );
728         }
729
730         ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
731             ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
732                 -1, -1 );
733
734         if ( ber == NULL ) {
735                 err = -1;
736         } else {
737                 err = ldap_send_server_request(
738                                         ld, ber, ld->ld_msgid, NULL,
739                                         (ludp->lud_host != NULL || ludp->lud_port != 0)
740                                                 ? ludp : NULL,
741                                         NULL, 1 );
742         }
743
744         ldap_free_urldesc( ludp );
745         return( err );
746 }
747
748
749 int
750 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
751         struct timeval *timeout, LDAPMessage **res )
752 {
753         int     msgid;
754
755         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
756                 return( ld->ld_errno );
757         }
758
759         if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
760                 return( ld->ld_errno );
761         }
762
763         if ( ld->ld_errno == LDAP_TIMEOUT ) {
764                 (void) ldap_abandon( ld, msgid );
765                 ld->ld_errno = LDAP_TIMEOUT;
766                 return( ld->ld_errno );
767         }
768
769         return( ldap_result2error( ld, *res, 0 ));
770 }
771
772
773 int
774 ldap_url_search_s(
775         LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
776 {
777         int     msgid;
778
779         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
780                 return( ld->ld_errno );
781         }
782
783         if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
784                 return( ld->ld_errno );
785         }
786
787         return( ldap_result2error( ld, *res, 0 ));
788 }
789
790
791 void
792 ldap_pvt_hex_unescape( char *s )
793 {
794 /*
795 * Remove URL hex escapes from s... done in place.  The basic concept for
796 * this routine is borrowed from the WWW library HTUnEscape() routine.
797 */
798         char    *p;
799
800         for ( p = s; *s != '\0'; ++s ) {
801                 if ( *s == '%' ) {
802                         if ( *++s != '\0' ) {
803                                 *p = ldap_pvt_unhex( *s ) << 4;
804                         }
805                         if ( *++s != '\0' ) {
806                                 *p++ += ldap_pvt_unhex( *s );
807                         }
808                 } else {
809                         *p++ = *s;
810                 }
811         }
812
813         *p = '\0';
814 }
815
816
817 int
818 ldap_pvt_unhex( int c )
819 {
820         return( c >= '0' && c <= '9' ? c - '0'
821             : c >= 'A' && c <= 'F' ? c - 'A' + 10
822             : c - 'a' + 10 );
823 }