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