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