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