ldap: use the new protocol handler setup
[platform/upstream/curl.git] / lib / ldap.c
1 /***************************************************************************
2  *                      _   _ ____  _
3  *  Project         ___| | | |  _ \| |
4  *                 / __| | | | |_) | |
5  *                | (__| |_| |  _ <| |___
6  *                 \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "setup.h"
24
25 #if !defined(CURL_DISABLE_LDAP) && !defined(USE_OPENLDAP)
26
27 /*
28  * Notice that USE_OPENLDAP is only a source code selection switch. When
29  * libcurl is built with USE_OPENLDAP defined the libcurl source code that
30  * gets compiled is the code from openldap.c, otherwise the code that gets
31  * compiled is the code from ldap.c.
32  *
33  * When USE_OPENLDAP is defined a recent version of the OpenLDAP library
34  * might be required for compilation and runtime. In order to use ancient
35  * OpenLDAP library versions, USE_OPENLDAP shall not be defined.
36  */
37
38 /* -- WIN32 approved -- */
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44 #include <errno.h>
45
46 #ifdef CURL_LDAP_WIN            /* Use Windows LDAP implementation. */
47 # include <winldap.h>
48 # ifndef LDAP_VENDOR_NAME
49 #  error Your Platform SDK is NOT sufficient for LDAP support! Update your Platform SDK, or disable LDAP support!
50 # else
51 #  include <winber.h>
52 # endif
53 #else
54 # define LDAP_DEPRECATED 1      /* Be sure ldap_init() is defined. */
55 # ifdef HAVE_LBER_H
56 #  include <lber.h>
57 # endif
58 # include <ldap.h>
59 # if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
60 #  include <ldap_ssl.h>
61 # endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
62 #endif
63
64 #ifdef HAVE_UNISTD_H
65 # include <unistd.h>
66 #endif
67
68 #include "urldata.h"
69 #include <curl/curl.h>
70 #include "sendf.h"
71 #include "escape.h"
72 #include "progress.h"
73 #include "transfer.h"
74 #include "strequal.h"
75 #include "strtok.h"
76 #include "curl_ldap.h"
77 #include "curl_memory.h"
78 #include "curl_base64.h"
79 #include "rawstr.h"
80
81 #define _MPRINTF_REPLACE /* use our functions only */
82 #include <curl/mprintf.h>
83
84 #include "memdebug.h"
85
86 #ifndef HAVE_LDAP_URL_PARSE
87
88 /* Use our own implementation. */
89
90 typedef struct {
91     char   *lud_host;
92     int     lud_port;
93     char   *lud_dn;
94     char  **lud_attrs;
95     int     lud_scope;
96     char   *lud_filter;
97     char  **lud_exts;
98 } CURL_LDAPURLDesc;
99
100 #undef LDAPURLDesc
101 #define LDAPURLDesc             CURL_LDAPURLDesc
102
103 static int  _ldap_url_parse (const struct connectdata *conn,
104                              LDAPURLDesc **ludp);
105 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
106
107 #undef ldap_free_urldesc
108 #define ldap_free_urldesc       _ldap_free_urldesc
109 #endif
110
111 #ifdef DEBUG_LDAP
112   #define LDAP_TRACE(x)   do { \
113                             _ldap_trace ("%u: ", __LINE__); \
114                             _ldap_trace x; \
115                           } while(0)
116
117   static void _ldap_trace (const char *fmt, ...);
118 #else
119   #define LDAP_TRACE(x)   ((void)0)
120 #endif
121
122
123 static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
124
125 /*
126  * LDAP protocol handler.
127  */
128
129 const struct Curl_handler Curl_handler_ldap = {
130   "LDAP",                               /* scheme */
131   ZERO_NULL,                            /* setup_connection */
132   Curl_ldap,                            /* do_it */
133   ZERO_NULL,                            /* done */
134   ZERO_NULL,                            /* do_more */
135   ZERO_NULL,                            /* connect_it */
136   ZERO_NULL,                            /* connecting */
137   ZERO_NULL,                            /* doing */
138   ZERO_NULL,                            /* proto_getsock */
139   ZERO_NULL,                            /* doing_getsock */
140   ZERO_NULL,                            /* perform_getsock */
141   ZERO_NULL,                            /* disconnect */
142   PORT_LDAP,                            /* defport */
143   CURLPROTO_LDAP,                       /* protocol */
144   PROTOPTS_NONE                         /* flags */
145 };
146
147 #ifdef HAVE_LDAP_SSL
148 /*
149  * LDAPS protocol handler.
150  */
151
152 const struct Curl_handler Curl_handler_ldaps = {
153   "LDAPS",                              /* scheme */
154   ZERO_NULL,                            /* setup_connection */
155   Curl_ldap,                            /* do_it */
156   ZERO_NULL,                            /* done */
157   ZERO_NULL,                            /* do_more */
158   ZERO_NULL,                            /* connect_it */
159   ZERO_NULL,                            /* connecting */
160   ZERO_NULL,                            /* doing */
161   ZERO_NULL,                            /* proto_getsock */
162   ZERO_NULL,                            /* doing_getsock */
163   ZERO_NULL,                            /* perform_getsock */
164   ZERO_NULL,                            /* disconnect */
165   PORT_LDAPS,                           /* defport */
166   CURLPROTO_LDAP | CURLPROTO_LDAPS,     /* protocol */
167   PROTOPT_SSL                           /* flags */
168 };
169 #endif
170
171
172 static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
173 {
174   CURLcode status = CURLE_OK;
175   int rc = 0;
176   LDAP *server = NULL;
177   LDAPURLDesc *ludp = NULL;
178   LDAPMessage *result = NULL;
179   LDAPMessage *entryIterator;
180   int num = 0;
181   struct SessionHandle *data=conn->data;
182   int ldap_proto = LDAP_VERSION3;
183   int ldap_ssl = 0;
184   char *val_b64;
185   size_t val_b64_sz;
186   curl_off_t dlsize=0;
187 #ifdef LDAP_OPT_NETWORK_TIMEOUT
188   struct timeval ldap_timeout = {10,0}; /* 10 sec connection/search timeout */
189 #endif
190
191   *done = TRUE; /* unconditionally */
192   infof(data, "LDAP local: LDAP Vendor = %s ; LDAP Version = %d\n",
193           LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION);
194   infof(data, "LDAP local: %s\n", data->change.url);
195
196 #ifdef HAVE_LDAP_URL_PARSE
197   rc = ldap_url_parse(data->change.url, &ludp);
198 #else
199   rc = _ldap_url_parse(conn, &ludp);
200 #endif
201   if(rc != 0) {
202     failf(data, "LDAP local: %s", ldap_err2string(rc));
203     status = CURLE_LDAP_INVALID_URL;
204     goto quit;
205   }
206
207   /* Get the URL scheme ( either ldap or ldaps ) */
208   if(conn->given->protocol & CURLPROTO_LDAPS)
209     ldap_ssl = 1;
210   infof(data, "LDAP local: trying to establish %s connection\n",
211           ldap_ssl ? "encrypted" : "cleartext");
212
213 #ifdef LDAP_OPT_NETWORK_TIMEOUT
214   ldap_set_option(NULL, LDAP_OPT_NETWORK_TIMEOUT, &ldap_timeout);
215 #endif
216   ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
217
218   if(ldap_ssl) {
219 #ifdef HAVE_LDAP_SSL
220 #ifdef CURL_LDAP_WIN
221     /* Win32 LDAP SDK doesnt support insecure mode without CA! */
222     server = ldap_sslinit(conn->host.name, (int)conn->port, 1);
223     ldap_set_option(server, LDAP_OPT_SSL, LDAP_OPT_ON);
224 #else
225     int ldap_option;
226     char* ldap_ca = data->set.str[STRING_SSL_CAFILE];
227 #if defined(CURL_HAS_NOVELL_LDAPSDK)
228     rc = ldapssl_client_init(NULL, NULL);
229     if(rc != LDAP_SUCCESS) {
230       failf(data, "LDAP local: ldapssl_client_init %s", ldap_err2string(rc));
231       status = CURLE_SSL_CERTPROBLEM;
232       goto quit;
233     }
234     if(data->set.ssl.verifypeer) {
235       /* Novell SDK supports DER or BASE64 files. */
236       int cert_type = LDAPSSL_CERT_FILETYPE_B64;
237       if((data->set.str[STRING_CERT_TYPE]) &&
238          (Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "DER")))
239         cert_type = LDAPSSL_CERT_FILETYPE_DER;
240       if(!ldap_ca) {
241         failf(data, "LDAP local: ERROR %s CA cert not set!",
242               (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"));
243         status = CURLE_SSL_CERTPROBLEM;
244         goto quit;
245       }
246       infof(data, "LDAP local: using %s CA cert '%s'\n",
247               (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
248               ldap_ca);
249       rc = ldapssl_add_trusted_cert(ldap_ca, cert_type);
250       if(rc != LDAP_SUCCESS) {
251         failf(data, "LDAP local: ERROR setting %s CA cert: %s",
252                 (cert_type == LDAPSSL_CERT_FILETYPE_DER ? "DER" : "PEM"),
253                 ldap_err2string(rc));
254         status = CURLE_SSL_CERTPROBLEM;
255         goto quit;
256       }
257       ldap_option = LDAPSSL_VERIFY_SERVER;
258     } else {
259       ldap_option = LDAPSSL_VERIFY_NONE;
260     }
261     rc = ldapssl_set_verify_mode(ldap_option);
262     if(rc != LDAP_SUCCESS) {
263       failf(data, "LDAP local: ERROR setting cert verify mode: %s",
264               ldap_err2string(rc));
265       status = CURLE_SSL_CERTPROBLEM;
266       goto quit;
267     }
268     server = ldapssl_init(conn->host.name, (int)conn->port, 1);
269     if(server == NULL) {
270       failf(data, "LDAP local: Cannot connect to %s:%hu",
271               conn->host.name, conn->port);
272       status = CURLE_COULDNT_CONNECT;
273       goto quit;
274     }
275 #elif defined(LDAP_OPT_X_TLS)
276     if(data->set.ssl.verifypeer) {
277       /* OpenLDAP SDK supports BASE64 files. */
278       if((data->set.str[STRING_CERT_TYPE]) &&
279          (!Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
280         failf(data, "LDAP local: ERROR OpenLDAP does only support PEM cert-type!");
281         status = CURLE_SSL_CERTPROBLEM;
282         goto quit;
283       }
284       if(!ldap_ca) {
285         failf(data, "LDAP local: ERROR PEM CA cert not set!");
286         status = CURLE_SSL_CERTPROBLEM;
287         goto quit;
288       }
289       infof(data, "LDAP local: using PEM CA cert: %s\n", ldap_ca);
290       rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, ldap_ca);
291       if(rc != LDAP_SUCCESS) {
292         failf(data, "LDAP local: ERROR setting PEM CA cert: %s",
293                 ldap_err2string(rc));
294         status = CURLE_SSL_CERTPROBLEM;
295         goto quit;
296       }
297       ldap_option = LDAP_OPT_X_TLS_DEMAND;
298     } else {
299       ldap_option = LDAP_OPT_X_TLS_NEVER;
300     }
301     rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
302     if(rc != LDAP_SUCCESS) {
303       failf(data, "LDAP local: ERROR setting cert verify mode: %s",
304               ldap_err2string(rc));
305       status = CURLE_SSL_CERTPROBLEM;
306       goto quit;
307     }
308     server = ldap_init(conn->host.name, (int)conn->port);
309     if(server == NULL) {
310       failf(data, "LDAP local: Cannot connect to %s:%hu",
311               conn->host.name, conn->port);
312       status = CURLE_COULDNT_CONNECT;
313       goto quit;
314     }
315     ldap_option = LDAP_OPT_X_TLS_HARD;
316     rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
317     if(rc != LDAP_SUCCESS) {
318       failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
319               ldap_err2string(rc));
320       status = CURLE_SSL_CERTPROBLEM;
321       goto quit;
322     }
323 /*
324     rc = ldap_start_tls_s(server, NULL, NULL);
325     if(rc != LDAP_SUCCESS) {
326       failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
327               ldap_err2string(rc));
328       status = CURLE_SSL_CERTPROBLEM;
329       goto quit;
330     }
331 */
332 #else
333     /* we should probably never come up to here since configure
334        should check in first place if we can support LDAP SSL/TLS */
335     failf(data, "LDAP local: SSL/TLS not supported with this version "
336             "of the OpenLDAP toolkit\n");
337     status = CURLE_SSL_CERTPROBLEM;
338     goto quit;
339 #endif
340 #endif
341 #endif /* CURL_LDAP_USE_SSL */
342   } else {
343     server = ldap_init(conn->host.name, (int)conn->port);
344     if(server == NULL) {
345       failf(data, "LDAP local: Cannot connect to %s:%hu",
346               conn->host.name, conn->port);
347       status = CURLE_COULDNT_CONNECT;
348       goto quit;
349     }
350   }
351 #ifdef CURL_LDAP_WIN
352   ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
353 #endif
354
355   rc = ldap_simple_bind_s(server,
356                           conn->bits.user_passwd ? conn->user : NULL,
357                           conn->bits.user_passwd ? conn->passwd : NULL);
358   if(!ldap_ssl && rc != 0) {
359     ldap_proto = LDAP_VERSION2;
360     ldap_set_option(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
361     rc = ldap_simple_bind_s(server,
362                             conn->bits.user_passwd ? conn->user : NULL,
363                             conn->bits.user_passwd ? conn->passwd : NULL);
364   }
365   if(rc != 0) {
366      failf(data, "LDAP local: ldap_simple_bind_s %s", ldap_err2string(rc));
367      status = CURLE_LDAP_CANNOT_BIND;
368      goto quit;
369   }
370
371   rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
372                      ludp->lud_filter, ludp->lud_attrs, 0, &result);
373
374   if(rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
375     failf(data, "LDAP remote: %s", ldap_err2string(rc));
376     status = CURLE_LDAP_SEARCH_FAILED;
377     goto quit;
378   }
379
380   for(num = 0, entryIterator = ldap_first_entry(server, result);
381       entryIterator;
382       entryIterator = ldap_next_entry(server, entryIterator), num++)
383   {
384     BerElement *ber = NULL;
385     char  *attribute;       /*! suspicious that this isn't 'const' */
386     char  *dn = ldap_get_dn(server, entryIterator);
387     int i;
388
389     Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
390     Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
391     Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
392
393     dlsize += strlen(dn)+5;
394
395     for (attribute = ldap_first_attribute(server, entryIterator, &ber);
396          attribute;
397          attribute = ldap_next_attribute(server, entryIterator, ber))
398     {
399       BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
400
401       if(vals != NULL)
402       {
403         for (i = 0; (vals[i] != NULL); i++)
404         {
405           Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
406           Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
407           Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
408           dlsize += strlen(attribute)+3;
409
410           if((strlen(attribute) > 7) &&
411               (strcmp(";binary",
412                       (char *)attribute +
413                       (strlen((char *)attribute) - 7)) == 0)) {
414             /* Binary attribute, encode to base64. */
415             val_b64_sz = Curl_base64_encode(data,
416                                             vals[i]->bv_val,
417                                             vals[i]->bv_len,
418                                             &val_b64);
419             if(val_b64_sz > 0) {
420               Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
421               free(val_b64);
422               dlsize += val_b64_sz;
423             }
424           }
425           else {
426             Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
427                               vals[i]->bv_len);
428             dlsize += vals[i]->bv_len;
429           }
430           Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
431           dlsize++;
432         }
433
434         /* Free memory used to store values */
435         ldap_value_free_len(vals);
436       }
437       Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
438       dlsize++;
439       Curl_pgrsSetDownloadCounter(data, dlsize);
440       ldap_memfree(attribute);
441     }
442     ldap_memfree(dn);
443     if(ber)
444        ber_free(ber, 0);
445   }
446
447 quit:
448   if(result) {
449     ldap_msgfree(result);
450     LDAP_TRACE (("Received %d entries\n", num));
451   }
452   if(rc == LDAP_SIZELIMIT_EXCEEDED)
453     infof(data, "There are more than %d entries\n", num);
454   if(ludp)
455     ldap_free_urldesc(ludp);
456   if(server)
457     ldap_unbind_s(server);
458 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
459   if(ldap_ssl)
460     ldapssl_client_deinit();
461 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
462
463   /* no data to transfer */
464   Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
465   conn->bits.close = TRUE;
466
467   return status;
468 }
469
470 #ifdef DEBUG_LDAP
471 static void _ldap_trace (const char *fmt, ...)
472 {
473   static int do_trace = -1;
474   va_list args;
475
476   if(do_trace == -1) {
477     const char *env = getenv("CURL_TRACE");
478     do_trace = (env && strtol(env, NULL, 10) > 0);
479   }
480   if(!do_trace)
481     return;
482
483   va_start (args, fmt);
484   vfprintf (stderr, fmt, args);
485   va_end (args);
486 }
487 #endif
488
489 #ifndef HAVE_LDAP_URL_PARSE
490
491 /*
492  * Return scope-value for a scope-string.
493  */
494 static int str2scope (const char *p)
495 {
496   if(strequal(p, "one"))
497      return LDAP_SCOPE_ONELEVEL;
498   if(strequal(p, "onetree"))
499      return LDAP_SCOPE_ONELEVEL;
500   if(strequal(p, "base"))
501      return LDAP_SCOPE_BASE;
502   if(strequal(p, "sub"))
503      return LDAP_SCOPE_SUBTREE;
504   if(strequal( p, "subtree"))
505      return LDAP_SCOPE_SUBTREE;
506   return (-1);
507 }
508
509 /*
510  * Split 'str' into strings separated by commas.
511  * Note: res[] points into 'str'.
512  */
513 static char **split_str (char *str)
514 {
515   char **res, *lasts, *s;
516   int  i;
517
518   for (i = 2, s = strchr(str,','); s; i++)
519      s = strchr(++s,',');
520
521   res = calloc(i, sizeof(char*));
522   if(!res)
523     return NULL;
524
525   for (i = 0, s = strtok_r(str, ",", &lasts); s;
526        s = strtok_r(NULL, ",", &lasts), i++)
527     res[i] = s;
528   return res;
529 }
530
531 /*
532  * Unescape the LDAP-URL components
533  */
534 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
535 {
536   int i;
537
538   if(ludp->lud_filter) {
539     ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
540     if(!ludp->lud_filter)
541        return (FALSE);
542   }
543
544   for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
545     ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
546     if(!ludp->lud_attrs[i])
547        return (FALSE);
548   }
549
550   for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
551     ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
552     if(!ludp->lud_exts[i])
553        return (FALSE);
554   }
555
556   if(ludp->lud_dn) {
557     char *dn = ludp->lud_dn;
558     char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
559
560     free(dn);
561     ludp->lud_dn = new_dn;
562     if(!new_dn)
563        return (FALSE);
564   }
565   return (TRUE);
566 }
567
568 /*
569  * Break apart the pieces of an LDAP URL.
570  * Syntax:
571  *   ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
572  *
573  * <hostname> already known from 'conn->host.name'.
574  * <port>     already known from 'conn->remote_port'.
575  * extract the rest from 'conn->data->state.path+1'. All fields are optional.
576  * e.g.
577  *   ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
578  * yields ludp->lud_dn = "".
579  *
580  * Defined in RFC4516 section 2.
581  */
582 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
583 {
584   char *p, *q;
585   int i;
586
587   if(!conn->data ||
588       !conn->data->state.path ||
589       conn->data->state.path[0] != '/' ||
590       !checkprefix("LDAP", conn->data->change.url))
591     return LDAP_INVALID_SYNTAX;
592
593   ludp->lud_scope = LDAP_SCOPE_BASE;
594   ludp->lud_port  = conn->remote_port;
595   ludp->lud_host  = conn->host.name;
596
597   /* parse DN (Distinguished Name).
598    */
599   ludp->lud_dn = strdup(conn->data->state.path+1);
600   if(!ludp->lud_dn)
601     return LDAP_NO_MEMORY;
602
603   p = strchr(ludp->lud_dn, '?');
604   LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
605                strlen(ludp->lud_dn), ludp->lud_dn));
606
607   if(!p)
608     goto success;
609
610   *p++ = '\0';
611
612   /* parse attributes. skip "??".
613    */
614   q = strchr(p, '?');
615   if(q)
616     *q++ = '\0';
617
618   if(*p && *p != '?') {
619     ludp->lud_attrs = split_str(p);
620     if(!ludp->lud_attrs)
621       return LDAP_NO_MEMORY;
622
623     for (i = 0; ludp->lud_attrs[i]; i++)
624       LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
625   }
626
627   p = q;
628   if(!p)
629     goto success;
630
631   /* parse scope. skip "??"
632    */
633   q = strchr(p, '?');
634   if(q)
635     *q++ = '\0';
636
637   if(*p && *p != '?') {
638     ludp->lud_scope = str2scope(p);
639     if(ludp->lud_scope == -1)
640       return LDAP_INVALID_SYNTAX;
641     LDAP_TRACE (("scope %d\n", ludp->lud_scope));
642   }
643
644   p = q;
645   if(!p)
646     goto success;
647
648   /* parse filter
649    */
650   q = strchr(p, '?');
651   if(q)
652     *q++ = '\0';
653   if(!*p)
654     return LDAP_INVALID_SYNTAX;
655
656   ludp->lud_filter = p;
657   LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
658
659   p = q;
660   if(!p)
661     goto success;
662
663   /* parse extensions
664    */
665   ludp->lud_exts = split_str(p);
666   if(!ludp->lud_exts)
667     return LDAP_NO_MEMORY;
668
669   for (i = 0; ludp->lud_exts[i]; i++)
670     LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
671
672   success:
673   if(!unescape_elements(conn->data, ludp))
674     return LDAP_NO_MEMORY;
675   return LDAP_SUCCESS;
676 }
677
678 static int _ldap_url_parse (const struct connectdata *conn,
679                             LDAPURLDesc **ludpp)
680 {
681   LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
682   int rc;
683
684   *ludpp = NULL;
685   if(!ludp)
686      return LDAP_NO_MEMORY;
687
688   rc = _ldap_url_parse2 (conn, ludp);
689   if(rc != LDAP_SUCCESS) {
690     _ldap_free_urldesc(ludp);
691     ludp = NULL;
692   }
693   *ludpp = ludp;
694   return (rc);
695 }
696
697 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
698 {
699   int i;
700
701   if(!ludp)
702      return;
703
704   if(ludp->lud_dn)
705      free(ludp->lud_dn);
706
707   if(ludp->lud_filter)
708      free(ludp->lud_filter);
709
710   if(ludp->lud_attrs) {
711     for (i = 0; ludp->lud_attrs[i]; i++)
712        free(ludp->lud_attrs[i]);
713     free(ludp->lud_attrs);
714   }
715
716   if(ludp->lud_exts) {
717     for (i = 0; ludp->lud_exts[i]; i++)
718        free(ludp->lud_exts[i]);
719     free(ludp->lud_exts);
720   }
721   free (ludp);
722 }
723 #endif  /* !HAVE_LDAP_URL_PARSE */
724 #endif  /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */