home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libpref / src / prefldap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  2.7 KB  |  104 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* LDAP functions callable from JavaScript */
  20.  
  21. #include "ldap.h"
  22. #include "xp_mcom.h"
  23. #include "prefldap.h"
  24.  
  25. /*
  26.  * Creates an LDAP search URL given a comma-separated list of attributes.
  27.  * Returns a list of key=values separated by '\n'
  28.  */
  29. char * pref_get_ldap_attributes(char* host, char* base, char* filter, char* attrs,
  30.     char** return_error)
  31. {
  32.     char *value = NULL;
  33.     LDAP* ld;
  34.     int err, i;
  35.     char *url;
  36.     LDAPMessage *result;
  37.     LDAPMessage    *e;
  38.     char *a;
  39.     BerElement    *ber;
  40.     char **vals;
  41.     
  42.     ld = ldap_init(host, LDAP_PORT);
  43.     if (!ld)
  44.         return value;
  45.         
  46.     url = (char*) malloc(sizeof(char) *
  47.          (strlen(host) + strlen(base) + strlen(filter) + strlen(attrs) + 20));
  48.     if (!url)
  49.         return value;
  50.         
  51.     XP_SPRINTF(url, "ldap://%s/%s?%s?sub?%s", host, base, attrs, filter);
  52.     
  53.     err = ldap_url_search_s( ld, url, 0, &result );
  54.     XP_FREE(url);
  55.     if (err != LDAP_SUCCESS) {
  56.         *return_error = ldap_err2string(err);
  57.         return value;
  58.     }
  59.     
  60.     e = ldap_first_entry( ld, result );
  61.  
  62.     if (e) {
  63.         a = ldap_first_attribute( ld, e, &ber );
  64.         if (a) {
  65.             int total_buf_size = 200;
  66.             int remaining_buf_size = total_buf_size;
  67.             value = (char*) malloc(sizeof(char*) * total_buf_size);
  68.             if (!value)
  69.                 return NULL;
  70.             value[0] = '\0';
  71.             
  72.             for ( ; a != NULL; a = ldap_next_attribute( ld, e, ber )) {
  73.                 vals = ldap_get_values( ld, e, a );
  74.                 if (vals && vals[0]) {
  75.                     remaining_buf_size -= (strlen(a) + strlen(vals[0]) + 2);
  76.                     if (remaining_buf_size < 1) {
  77.                         remaining_buf_size += 2 * total_buf_size;
  78.                         total_buf_size += 2 * total_buf_size;
  79.                         value = (char*) realloc(value, sizeof(char*) * total_buf_size);
  80.                         if (!value)
  81.                             return NULL;
  82.                     }
  83.                     
  84.                     strcat(value, "\n");
  85.                     strcat(value, a);
  86.                     strcat(value, "=");
  87.                     strcat(value, vals[0]);
  88.                     
  89.                     ldap_value_free( vals );
  90.                 }
  91.             }
  92.             ldap_memfree(a);
  93.         }
  94.         if (ber)
  95.             ber_free(ber, 0);
  96.     }
  97.  
  98.     ldap_msgfree(result);
  99.     ldap_unbind(ld);
  100.     
  101.     return value;
  102. }
  103.  
  104.