23527157885b9eccc811869a6b947745e953736c
[platform/upstream/curl.git] / lib / ldap.c
1 /***************************************************************************
2  *                      _   _ ____  _
3  *  Project         ___| | | |  _ \| |
4  *                 / __| | | | |_) | |
5  *                | (__| |_| |  _ <| |___
6  *                 \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, 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 "curl_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 #ifdef CURL_LDAP_WIN            /* Use Windows LDAP implementation. */
39 # include <winldap.h>
40 # ifndef LDAP_VENDOR_NAME
41 #  error Your Platform SDK is NOT sufficient for LDAP support! \
42          Update your Platform SDK, or disable LDAP support!
43 # else
44 #  include <winber.h>
45 # endif
46 #else
47 # define LDAP_DEPRECATED 1      /* Be sure ldap_init() is defined. */
48 # ifdef HAVE_LBER_H
49 #  include <lber.h>
50 # endif
51 # include <ldap.h>
52 # if (defined(HAVE_LDAP_SSL) && defined(HAVE_LDAP_SSL_H))
53 #  include <ldap_ssl.h>
54 # endif /* HAVE_LDAP_SSL && HAVE_LDAP_SSL_H */
55 #endif
56
57 #include "urldata.h"
58 #include <curl/curl.h>
59 #include "sendf.h"
60 #include "escape.h"
61 #include "progress.h"
62 #include "transfer.h"
63 #include "strequal.h"
64 #include "strtok.h"
65 #include "curl_ldap.h"
66 #include "curl_memory.h"
67 #include "curl_base64.h"
68 #include "rawstr.h"
69
70 #define _MPRINTF_REPLACE /* use our functions only */
71 #include <curl/mprintf.h>
72
73 #include "memdebug.h"
74
75 #ifndef HAVE_LDAP_URL_PARSE
76
77 /* Use our own implementation. */
78
79 typedef struct {
80     char   *lud_host;
81     int     lud_port;
82     char   *lud_dn;
83     char  **lud_attrs;
84     int     lud_scope;
85     char   *lud_filter;
86     char  **lud_exts;
87 } CURL_LDAPURLDesc;
88
89 #undef LDAPURLDesc
90 #define LDAPURLDesc             CURL_LDAPURLDesc
91
92 static int  _ldap_url_parse (const struct connectdata *conn,
93                              LDAPURLDesc **ludp);
94 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
95
96 #undef ldap_free_urldesc
97 #define ldap_free_urldesc       _ldap_free_urldesc
98 #endif
99
100 #ifdef DEBUG_LDAP
101   #define LDAP_TRACE(x)   do { \
102                             _ldap_trace ("%u: ", __LINE__); \
103                             _ldap_trace x; \
104                           } WHILE_FALSE
105
106   static void _ldap_trace (const char *fmt, ...);
107 #else
108   #define LDAP_TRACE(x)   Curl_nop_stmt
109 #endif
110
111
112 static CURLcode Curl_ldap(struct connectdata *conn, bool *done);
113
114 /*
115  * LDAP protocol handler.
116  */
117
118 const struct Curl_handler Curl_handler_ldap = {
119   "LDAP",                               /* scheme */
120   ZERO_NULL,                            /* setup_connection */
121   Curl_ldap,                            /* do_it */
122   ZERO_NULL,                            /* done */
123   ZERO_NULL,                            /* do_more */
124   ZERO_NULL,                            /* connect_it */
125   ZERO_NULL,                            /* connecting */
126   ZERO_NULL,                            /* doing */
127   ZERO_NULL,                            /* proto_getsock */
128   ZERO_NULL,                            /* doing_getsock */
129   ZERO_NULL,                            /* domore_getsock */
130   ZERO_NULL,                            /* perform_getsock */
131   ZERO_NULL,                            /* disconnect */
132   ZERO_NULL,                            /* readwrite */
133   PORT_LDAP,                            /* defport */
134   CURLPROTO_LDAP,                       /* protocol */
135   PROTOPT_NONE                          /* flags */
136 };
137
138 #ifdef HAVE_LDAP_SSL
139 /*
140  * LDAPS protocol handler.
141  */
142
143 const struct Curl_handler Curl_handler_ldaps = {
144   "LDAPS",                              /* scheme */
145   ZERO_NULL,                            /* setup_connection */
146   Curl_ldap,                            /* do_it */
147   ZERO_NULL,                            /* done */
148   ZERO_NULL,                            /* do_more */
149   ZERO_NULL,                            /* connect_it */
150   ZERO_NULL,                            /* connecting */
151   ZERO_NULL,                            /* doing */
152   ZERO_NULL,                            /* proto_getsock */
153   ZERO_NULL,                            /* doing_getsock */
154   ZERO_NULL,                            /* domore_getsock */
155   ZERO_NULL,                            /* perform_getsock */
156   ZERO_NULL,                            /* disconnect */
157   ZERO_NULL,                            /* readwrite */
158   PORT_LDAPS,                           /* defport */
159   CURLPROTO_LDAP | CURLPROTO_LDAPS,     /* protocol */
160   PROTOPT_SSL                           /* flags */
161 };
162 #endif
163
164
165 static CURLcode Curl_ldap(struct connectdata *conn, bool *done)
166 {
167   CURLcode status = CURLE_OK;
168   int rc = 0;
169   LDAP *server = NULL;
170   LDAPURLDesc *ludp = NULL;
171   LDAPMessage *result = NULL;
172   LDAPMessage *entryIterator;
173   int num = 0;
174   struct SessionHandle *data=conn->data;
175   int ldap_proto = LDAP_VERSION3;
176   int ldap_ssl = 0;
177   char *val_b64 = NULL;
178   size_t val_b64_sz = 0;
179   curl_off_t dlsize = 0;
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(conn->given->flags & PROTOPT_SSL)
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 doesn't 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          (Curl_raw_equal(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     }
252     else
253       ldap_option = LDAPSSL_VERIFY_NONE;
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:%ld",
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          (!Curl_raw_equal(data->set.str[STRING_CERT_TYPE], "PEM"))) {
273         failf(data, "LDAP local: ERROR OpenLDAP only supports 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     }
292     else
293       ldap_option = LDAP_OPT_X_TLS_NEVER;
294
295     rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_option);
296     if(rc != LDAP_SUCCESS) {
297       failf(data, "LDAP local: ERROR setting cert verify mode: %s",
298               ldap_err2string(rc));
299       status = CURLE_SSL_CERTPROBLEM;
300       goto quit;
301     }
302     server = ldap_init(conn->host.name, (int)conn->port);
303     if(server == NULL) {
304       failf(data, "LDAP local: Cannot connect to %s:%ld",
305               conn->host.name, conn->port);
306       status = CURLE_COULDNT_CONNECT;
307       goto quit;
308     }
309     ldap_option = LDAP_OPT_X_TLS_HARD;
310     rc = ldap_set_option(server, LDAP_OPT_X_TLS, &ldap_option);
311     if(rc != LDAP_SUCCESS) {
312       failf(data, "LDAP local: ERROR setting SSL/TLS mode: %s",
313               ldap_err2string(rc));
314       status = CURLE_SSL_CERTPROBLEM;
315       goto quit;
316     }
317 /*
318     rc = ldap_start_tls_s(server, NULL, NULL);
319     if(rc != LDAP_SUCCESS) {
320       failf(data, "LDAP local: ERROR starting SSL/TLS mode: %s",
321               ldap_err2string(rc));
322       status = CURLE_SSL_CERTPROBLEM;
323       goto quit;
324     }
325 */
326 #else
327     /* we should probably never come up to here since configure
328        should check in first place if we can support LDAP SSL/TLS */
329     failf(data, "LDAP local: SSL/TLS not supported with this version "
330             "of the OpenLDAP toolkit\n");
331     status = CURLE_SSL_CERTPROBLEM;
332     goto quit;
333 #endif
334 #endif
335 #endif /* CURL_LDAP_USE_SSL */
336   }
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:%ld",
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     BerElement *ber = NULL;
379     char  *attribute;       /*! suspicious that this isn't 'const' */
380     char  *dn = ldap_get_dn(server, entryIterator);
381     int i;
382
383     Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
384     Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
385     Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
386
387     dlsize += strlen(dn)+5;
388
389     for(attribute = ldap_first_attribute(server, entryIterator, &ber);
390         attribute;
391         attribute = ldap_next_attribute(server, entryIterator, ber)) {
392       BerValue **vals = ldap_get_values_len(server, entryIterator, attribute);
393
394       if(vals != NULL) {
395         for(i = 0; (vals[i] != NULL); i++) {
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           dlsize += strlen(attribute)+3;
400
401           if((strlen(attribute) > 7) &&
402               (strcmp(";binary",
403                       (char *)attribute +
404                       (strlen((char *)attribute) - 7)) == 0)) {
405             /* Binary attribute, encode to base64. */
406             CURLcode error = Curl_base64_encode(data,
407                                                 vals[i]->bv_val,
408                                                 vals[i]->bv_len,
409                                                 &val_b64,
410                                                 &val_b64_sz);
411             if(error) {
412               ldap_value_free_len(vals);
413               ldap_memfree(attribute);
414               ldap_memfree(dn);
415               if(ber)
416                 ber_free(ber, 0);
417               status = error;
418               goto quit;
419             }
420             if(val_b64_sz > 0) {
421               Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
422               free(val_b64);
423               dlsize += val_b64_sz;
424             }
425           }
426           else {
427             Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
428                               vals[i]->bv_len);
429             dlsize += vals[i]->bv_len;
430           }
431           Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
432           dlsize++;
433         }
434
435         /* Free memory used to store values */
436         ldap_value_free_len(vals);
437       }
438       Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
439       dlsize++;
440       Curl_pgrsSetDownloadCounter(data, dlsize);
441       ldap_memfree(attribute);
442     }
443     ldap_memfree(dn);
444     if(ber)
445        ber_free(ber, 0);
446   }
447
448 quit:
449   if(result) {
450     ldap_msgfree(result);
451     LDAP_TRACE (("Received %d entries\n", num));
452   }
453   if(rc == LDAP_SIZELIMIT_EXCEEDED)
454     infof(data, "There are more than %d entries\n", num);
455   if(ludp)
456     ldap_free_urldesc(ludp);
457   if(server)
458     ldap_unbind_s(server);
459 #if defined(HAVE_LDAP_SSL) && defined(CURL_HAS_NOVELL_LDAPSDK)
460   if(ldap_ssl)
461     ldapssl_client_deinit();
462 #endif /* HAVE_LDAP_SSL && CURL_HAS_NOVELL_LDAPSDK */
463
464   /* no data to transfer */
465   Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
466   conn->bits.close = TRUE;
467
468   return status;
469 }
470
471 #ifdef DEBUG_LDAP
472 static void _ldap_trace (const char *fmt, ...)
473 {
474   static int do_trace = -1;
475   va_list args;
476
477   if(do_trace == -1) {
478     const char *env = getenv("CURL_TRACE");
479     do_trace = (env && strtol(env, NULL, 10) > 0);
480   }
481   if(!do_trace)
482     return;
483
484   va_start (args, fmt);
485   vfprintf (stderr, fmt, args);
486   va_end (args);
487 }
488 #endif
489
490 #ifndef HAVE_LDAP_URL_PARSE
491
492 /*
493  * Return scope-value for a scope-string.
494  */
495 static int str2scope (const char *p)
496 {
497   if(strequal(p, "one"))
498      return LDAP_SCOPE_ONELEVEL;
499   if(strequal(p, "onetree"))
500      return LDAP_SCOPE_ONELEVEL;
501   if(strequal(p, "base"))
502      return LDAP_SCOPE_BASE;
503   if(strequal(p, "sub"))
504      return LDAP_SCOPE_SUBTREE;
505   if(strequal( p, "subtree"))
506      return LDAP_SCOPE_SUBTREE;
507   return (-1);
508 }
509
510 /*
511  * Split 'str' into strings separated by commas.
512  * Note: res[] points into 'str'.
513  */
514 static char **split_str (char *str)
515 {
516   char **res, *lasts, *s;
517   int  i;
518
519   for(i = 2, s = strchr(str,','); s; i++)
520     s = strchr(++s,',');
521
522   res = calloc(i, sizeof(char*));
523   if(!res)
524     return NULL;
525
526   for(i = 0, s = strtok_r(str, ",", &lasts); s;
527       s = strtok_r(NULL, ",", &lasts), i++)
528     res[i] = s;
529   return res;
530 }
531
532 /*
533  * Unescape the LDAP-URL components
534  */
535 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
536 {
537   int i;
538
539   if(ludp->lud_filter) {
540     ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
541     if(!ludp->lud_filter)
542        return (FALSE);
543   }
544
545   for(i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
546     ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
547     if(!ludp->lud_attrs[i])
548       return (FALSE);
549   }
550
551   for(i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
552     ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
553     if(!ludp->lud_exts[i])
554       return (FALSE);
555   }
556
557   if(ludp->lud_dn) {
558     char *dn = ludp->lud_dn;
559     char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
560
561     free(dn);
562     ludp->lud_dn = new_dn;
563     if(!new_dn)
564        return (FALSE);
565   }
566   return (TRUE);
567 }
568
569 /*
570  * Break apart the pieces of an LDAP URL.
571  * Syntax:
572  *   ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
573  *
574  * <hostname> already known from 'conn->host.name'.
575  * <port>     already known from 'conn->remote_port'.
576  * extract the rest from 'conn->data->state.path+1'. All fields are optional.
577  * e.g.
578  *   ldap://<hostname>:<port>/?<attributes>?<scope>?<filter>
579  * yields ludp->lud_dn = "".
580  *
581  * Defined in RFC4516 section 2.
582  */
583 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
584 {
585   char *p, *q;
586   int i;
587
588   if(!conn->data ||
589       !conn->data->state.path ||
590       conn->data->state.path[0] != '/' ||
591       !checkprefix("LDAP", conn->data->change.url))
592     return LDAP_INVALID_SYNTAX;
593
594   ludp->lud_scope = LDAP_SCOPE_BASE;
595   ludp->lud_port  = conn->remote_port;
596   ludp->lud_host  = conn->host.name;
597
598   /* parse DN (Distinguished Name).
599    */
600   ludp->lud_dn = strdup(conn->data->state.path+1);
601   if(!ludp->lud_dn)
602     return LDAP_NO_MEMORY;
603
604   p = strchr(ludp->lud_dn, '?');
605   LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
606                strlen(ludp->lud_dn), ludp->lud_dn));
607
608   if(!p)
609     goto success;
610
611   *p++ = '\0';
612
613   /* parse attributes. skip "??".
614    */
615   q = strchr(p, '?');
616   if(q)
617     *q++ = '\0';
618
619   if(*p && *p != '?') {
620     ludp->lud_attrs = split_str(p);
621     if(!ludp->lud_attrs)
622       return LDAP_NO_MEMORY;
623
624     for(i = 0; ludp->lud_attrs[i]; i++)
625       LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
626   }
627
628   p = q;
629   if(!p)
630     goto success;
631
632   /* parse scope. skip "??"
633    */
634   q = strchr(p, '?');
635   if(q)
636     *q++ = '\0';
637
638   if(*p && *p != '?') {
639     ludp->lud_scope = str2scope(p);
640     if(ludp->lud_scope == -1)
641       return LDAP_INVALID_SYNTAX;
642     LDAP_TRACE (("scope %d\n", ludp->lud_scope));
643   }
644
645   p = q;
646   if(!p)
647     goto success;
648
649   /* parse filter
650    */
651   q = strchr(p, '?');
652   if(q)
653     *q++ = '\0';
654   if(!*p)
655     return LDAP_INVALID_SYNTAX;
656
657   ludp->lud_filter = p;
658   LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
659
660   p = q;
661   if(!p)
662     goto success;
663
664   /* parse extensions
665    */
666   ludp->lud_exts = split_str(p);
667   if(!ludp->lud_exts)
668     return LDAP_NO_MEMORY;
669
670   for(i = 0; ludp->lud_exts[i]; i++)
671     LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
672
673   success:
674   if(!unescape_elements(conn->data, ludp))
675     return LDAP_NO_MEMORY;
676   return LDAP_SUCCESS;
677 }
678
679 static int _ldap_url_parse (const struct connectdata *conn,
680                             LDAPURLDesc **ludpp)
681 {
682   LDAPURLDesc *ludp = calloc(1, sizeof(*ludp));
683   int rc;
684
685   *ludpp = NULL;
686   if(!ludp)
687      return LDAP_NO_MEMORY;
688
689   rc = _ldap_url_parse2 (conn, ludp);
690   if(rc != LDAP_SUCCESS) {
691     _ldap_free_urldesc(ludp);
692     ludp = NULL;
693   }
694   *ludpp = ludp;
695   return (rc);
696 }
697
698 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
699 {
700   int i;
701
702   if(!ludp)
703     return;
704
705   if(ludp->lud_dn)
706     free(ludp->lud_dn);
707
708   if(ludp->lud_filter)
709     free(ludp->lud_filter);
710
711   if(ludp->lud_attrs) {
712     for(i = 0; ludp->lud_attrs[i]; i++)
713       free(ludp->lud_attrs[i]);
714     free(ludp->lud_attrs);
715   }
716
717   if(ludp->lud_exts) {
718     for(i = 0; ludp->lud_exts[i]; i++)
719       free(ludp->lud_exts[i]);
720     free(ludp->lud_exts);
721   }
722   free (ludp);
723 }
724 #endif  /* !HAVE_LDAP_URL_PARSE */
725 #endif  /* !CURL_DISABLE_LDAP && !USE_OPENLDAP */