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