home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libpref / admin / prefldap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  2.8 KB  |  112 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. #ifdef XP_WIN
  24. #include "jsapi.h"
  25. #endif
  26. #include "prefldap.h"
  27.  
  28. /*
  29.  * Creates an LDAP search URL given a comma-separated list of attributes.
  30.  * Returns a list of key=values separated by '\n'
  31.  */
  32. #if defined(XP_WIN)
  33. PR_IMPLEMENT(char*) 
  34. #else
  35. char * 
  36. #endif
  37. pref_get_ldap_attributes(char* host, char* base, char* filter, char* attrs,
  38.     char** return_error)
  39. {
  40.     char *value = NULL;
  41.     LDAP* ld;
  42.     int err, i;
  43.     char *url;
  44.     LDAPMessage *result;
  45.     LDAPMessage    *e;
  46.     char *a;
  47.     BerElement    *ber;
  48.     char **vals;
  49.     
  50.     ld = ldap_init(host, LDAP_PORT);
  51.     if (!ld)
  52.         return value;
  53.         
  54.     url = (char*) malloc(sizeof(char) *
  55.          (strlen(host) + strlen(base) + strlen(filter) + strlen(attrs) + 20));
  56.     if (!url)
  57.         return value;
  58.         
  59.     XP_SPRINTF(url, "ldap://%s/%s?%s?sub?%s", host, base, attrs, filter);
  60.     
  61.     err = ldap_url_search_s( ld, url, 0, &result );
  62.     XP_FREE(url);
  63.     if (err != LDAP_SUCCESS) {
  64.         *return_error = ldap_err2string(err);
  65.         return value;
  66.     }
  67.     
  68.     e = ldap_first_entry( ld, result );
  69.  
  70.     if (e) {
  71.         a = ldap_first_attribute( ld, e, &ber );
  72.         if (a) {
  73.             int total_buf_size = 200;
  74.             int remaining_buf_size = total_buf_size;
  75.             value = (char*) malloc(sizeof(char*) * total_buf_size);
  76.             if (!value)
  77.                 return NULL;
  78.             value[0] = '\0';
  79.             
  80.             for ( ; a != NULL; a = ldap_next_attribute( ld, e, ber )) {
  81.                 vals = ldap_get_values( ld, e, a );
  82.                 if (vals && vals[0]) {
  83.                     remaining_buf_size -= (strlen(a) + strlen(vals[0]) + 2);
  84.                     if (remaining_buf_size < 1) {
  85.                         remaining_buf_size += 2 * total_buf_size;
  86.                         total_buf_size += 2 * total_buf_size;
  87.                         value = (char*) realloc(value, sizeof(char*) * total_buf_size);
  88.                         if (!value)
  89.                             return NULL;
  90.                     }
  91.                     
  92.                     strcat(value, "\n");
  93.                     strcat(value, a);
  94.                     strcat(value, "=");
  95.                     strcat(value, vals[0]);
  96.                     
  97.                     ldap_value_free( vals );
  98.                 }
  99.             }
  100.             ldap_memfree(a);
  101.         }
  102.         if (ber)
  103.             ber_free(ber, 0);
  104.     }
  105.  
  106.     ldap_msgfree(result);
  107.     ldap_unbind(ld);
  108.     
  109.     return value;
  110. }
  111.  
  112.