~ubuntu-branches/ubuntu/lucid/curl/lucid-security

« back to all changes in this revision

Viewing changes to lib/ldap.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-12-12 15:04:52 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20051212150452-2ymlra67b2p7kjyy
Tags: 7.15.1-1ubuntu1
Resynchronise with Debian to get URL parser overflow fix from 7.15.1
(CVE-2005-4077).

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: ldap.c,v 1.49 2005/03/11 05:28:07 danf Exp $
 
21
 * $Id: ldap.c,v 1.51 2005/11/01 16:27:27 giva Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
61
61
#include "strtok.h"
62
62
#include "ldap.h"
63
63
#include "memory.h"
 
64
#include "base64.h"
64
65
 
65
66
#define _MPRINTF_REPLACE /* use our functions only */
66
67
#include <curl/mprintf.h>
78
79
#ifndef LDAP_SIZELIMIT_EXCEEDED
79
80
#define LDAP_SIZELIMIT_EXCEEDED 4
80
81
#endif
 
82
#ifndef LDAP_VERSION2
 
83
#define LDAP_VERSION2 2
 
84
#endif
 
85
#ifndef LDAP_VERSION3
 
86
#define LDAP_VERSION3 3
 
87
#endif
 
88
#ifndef LDAP_OPT_PROTOCOL_VERSION
 
89
#define LDAP_OPT_PROTOCOL_VERSION 0x0011
 
90
#endif
81
91
 
82
92
#define DLOPEN_MODE   RTLD_LAZY  /*! assume all dlopen() implementations have
83
93
                                   this */
115
125
#endif
116
126
#endif
117
127
 
 
128
struct bv {
 
129
  unsigned long bv_len;
 
130
  char  *bv_val;
 
131
};
 
132
 
118
133
static int DynaOpen(const char **mod_name)
119
134
{
120
135
#if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
145
160
  return (libldap != NULL);
146
161
 
147
162
#else
148
 
  (void) mod_name;
 
163
  *mod_name = "";
149
164
  return (0);
150
165
#endif
151
166
}
247
262
  char  *(__cdecl *ldap_get_dn)(void *, void *);
248
263
  char  *(__cdecl *ldap_first_attribute)(void *, void *, void **);
249
264
  char  *(__cdecl *ldap_next_attribute)(void *, void *, void *);
250
 
  char **(__cdecl *ldap_get_values)(void *, void *, const char *);
251
 
  void   (__cdecl *ldap_value_free)(char **);
 
265
  void **(__cdecl *ldap_get_values_len)(void *, void *, const char *);
 
266
  void   (__cdecl *ldap_value_free_len)(void **);
252
267
  void   (__cdecl *ldap_memfree)(void *);
253
268
  void   (__cdecl *ber_free)(void *, int);
 
269
  int    (__cdecl *ldap_set_option)(void *, int, void *);
254
270
 
255
271
  void *server;
256
272
  LDAPURLDesc *ludp = NULL;
259
275
  void *entryIterator;     /*! type should be 'LDAPMessage *' */
260
276
  int num = 0;
261
277
  struct SessionHandle *data=conn->data;
 
278
  int ldap_proto;
 
279
  char *val_b64;
 
280
  size_t val_b64_sz;
262
281
 
263
282
  *done = TRUE; /* unconditionally */
264
283
  infof(data, "LDAP local: %s\n", data->change.url);
272
291
   * pointer-to-object (data) and pointer-to-function.
273
292
   */
274
293
  DYNA_GET_FUNCTION(void *(__cdecl *)(char *, int), ldap_init);
275
 
  DYNA_GET_FUNCTION(int (__cdecl *)(void *, char *, char *), ldap_simple_bind_s);
 
294
  DYNA_GET_FUNCTION(int (__cdecl *)(void *, char *, char *),
 
295
                    ldap_simple_bind_s);
276
296
  DYNA_GET_FUNCTION(int (__cdecl *)(void *), ldap_unbind_s);
277
297
#ifndef WIN32
278
298
  DYNA_GET_FUNCTION(int (*)(char *, LDAPURLDesc **), ldap_url_parse);
279
299
  DYNA_GET_FUNCTION(void (*)(void *), ldap_free_urldesc);
280
300
#endif
281
301
  DYNA_GET_FUNCTION(int (__cdecl *)(void *, char *, int, char *, char **, int,
282
 
                            void **), ldap_search_s);
 
302
                                    void **), ldap_search_s);
283
303
  DYNA_GET_FUNCTION(void *(__cdecl *)(void *, void *), ldap_first_entry);
284
304
  DYNA_GET_FUNCTION(void *(__cdecl *)(void *, void *), ldap_next_entry);
285
305
  DYNA_GET_FUNCTION(char *(__cdecl *)(int), ldap_err2string);
286
306
  DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *), ldap_get_dn);
287
 
  DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void **), ldap_first_attribute);
288
 
  DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void *), ldap_next_attribute);
289
 
  DYNA_GET_FUNCTION(char **(__cdecl *)(void *, void *, const char *), ldap_get_values);
290
 
  DYNA_GET_FUNCTION(void (__cdecl *)(char **), ldap_value_free);
 
307
  DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void **),
 
308
                    ldap_first_attribute);
 
309
  DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void *),
 
310
                    ldap_next_attribute);
 
311
  DYNA_GET_FUNCTION(void **(__cdecl *)(void *, void *, const char *),
 
312
                    ldap_get_values_len);
 
313
  DYNA_GET_FUNCTION(void (__cdecl *)(void **), ldap_value_free_len);
291
314
  DYNA_GET_FUNCTION(void (__cdecl *)(void *), ldap_memfree);
292
315
  DYNA_GET_FUNCTION(void (__cdecl *)(void *, int), ber_free);
 
316
  DYNA_GET_FUNCTION(int (__cdecl *)(void *, int, void *), ldap_set_option);
293
317
 
294
318
  server = (*ldap_init)(conn->host.name, (int)conn->port);
295
319
  if (server == NULL) {
299
323
    goto quit;
300
324
  }
301
325
 
 
326
  ldap_proto = LDAP_VERSION3;
 
327
  (*ldap_set_option)(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
302
328
  rc = (*ldap_simple_bind_s)(server,
303
329
                             conn->bits.user_passwd ? conn->user : NULL,
304
330
                             conn->bits.user_passwd ? conn->passwd : NULL);
305
331
  if (rc != 0) {
 
332
    ldap_proto = LDAP_VERSION2;
 
333
    (*ldap_set_option)(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
 
334
    rc = (*ldap_simple_bind_s)(server,
 
335
                               conn->bits.user_passwd ? conn->user : NULL,
 
336
                               conn->bits.user_passwd ? conn->passwd : NULL);
 
337
  }
 
338
  if (rc != 0) {
306
339
     failf(data, "LDAP local: %s", (*ldap_err2string)(rc));
307
340
     status = CURLE_LDAP_CANNOT_BIND;
308
341
     goto quit;
346
379
         attribute;
347
380
         attribute = (*ldap_next_attribute)(server, entryIterator, ber))
348
381
    {
349
 
      char **vals = (*ldap_get_values)(server, entryIterator, attribute);
 
382
      struct bv **vals = (struct bv **)
 
383
        (*ldap_get_values_len)(server, entryIterator, attribute);
350
384
 
351
385
      if (vals != NULL)
352
386
      {
353
387
        for (i = 0; (vals[i] != NULL); i++)
354
388
        {
355
389
          Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\t", 1);
356
 
          Curl_client_write(data, CLIENTWRITE_BODY, (char*) attribute, 0);
 
390
          Curl_client_write(data, CLIENTWRITE_BODY, (char *) attribute, 0);
357
391
          Curl_client_write(data, CLIENTWRITE_BODY, (char *)": ", 2);
358
 
          Curl_client_write(data, CLIENTWRITE_BODY, vals[i], 0);
 
392
          if ((strlen(attribute) > 7) &&
 
393
              (strcmp(";binary",
 
394
                      (char *)attribute +
 
395
                      (strlen((char *)attribute) - 7)) == 0)) {
 
396
            /* Binary attribute, encode to base64. */
 
397
            val_b64_sz = Curl_base64_encode(vals[i]->bv_val, vals[i]->bv_len,
 
398
                                            &val_b64);
 
399
            if (val_b64_sz > 0) {
 
400
              Curl_client_write(data, CLIENTWRITE_BODY, val_b64, val_b64_sz);
 
401
              free(val_b64);
 
402
            }
 
403
          } else
 
404
            Curl_client_write(data, CLIENTWRITE_BODY, vals[i]->bv_val,
 
405
                              vals[i]->bv_len);
359
406
          Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 0);
360
407
        }
361
408
 
362
409
        /* Free memory used to store values */
363
 
        (*ldap_value_free)(vals);
 
410
        (*ldap_value_free_len)((void **)vals);
364
411
      }
365
412
      Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
366
413
 
518
565
     return LDAP_NO_MEMORY;
519
566
 
520
567
  p = strchr(ludp->lud_dn, '?');
521
 
  LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) : strlen(ludp->lud_dn),
522
 
               ludp->lud_dn));
 
568
  LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
 
569
               strlen(ludp->lud_dn), ludp->lud_dn));
523
570
 
524
571
  if (!p)
525
572
     goto success;