help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] Calling LDAP C API from Smalltalk


From: Stephen
Subject: [Help-smalltalk] Calling LDAP C API from Smalltalk
Date: Fri, 19 Sep 2008 07:32:06 +1200
User-agent: Thunderbird 2.0.0.16 (Macintosh/20080707)

Hi All,

I'm hoping someone can give me a hand to link to the C LDAP API. The aim is to access LDAP data from GNU Smalltalk server scripts and I figure using the C interface is the simplest way of doing this.

I haven't done much C programming. Secondly, I'm running Mac OS X 10.5, and have Linux available as well if need be.

LDAP C API
As far as I can see there are no callbacks in the LDAP API, so Library calls should be fine.

Section 5.2 of the documentation says "First, the mapping between these functions and string names for the functions needs to be established in your module".

So in order to call the LDAP C API from Smalltalk I have some questions:-
1. How does one action "DLD class>>#addLibrary:" so as to link GNU Smalltalk with the LDAP library? I don't know what it means! 2. Where is the mapping specified between the C function, and the Smalltalk name that I am to provide for the function? I wasn't able to find this in the documentation. For example, the first function is an ldap_init and the C definition looks like this:-

       #include <ldap.h>

       LDAP *ldap_open(host, port)
       char *host;
       int port;

       LDAP *ldap_init(host, port)
       char *host;
       int port;

What are the steps required to map the function above to a definition similar to the one below?
ldap_open: aString
         <cCall: 'system' returning: #cObject args: ...

(ldap api is at http://www.openldap.org/software/man.cgi?query=ldap_open&apropos=0&sektion=3&manpath=OpenLDAP+2.0-Release&format=html )

3. In the definition above, I'm expecting to get back a pointer to an LDAP struct. Would the return type in the Smalltalk function be a "cObject"?

If it helps, the definition in ldap.h for the LDAP struct is just...
   typedef struct ldap LDAP;

My aim is to get the program below running from Smalltalk if at all possible!

------------------
http://docs.sun.com/source/816-5616-10/example.htm#16961

#include <stdio.h>
#include "ldap.h"

/* Adjust these setting for your own LDAP server */
#define HOSTNAME "localhost"
#define PORT_NUMBER  LDAP_PORT
#define FIND_DN "uid=bjensen, ou=People, o=airius.com"

int
main( int argc, char **argv )
{
  LDAP         *ld;
  LDAPMessage  *result, *e;
  BerElement   *ber;
  char         *a;
  char         **vals;
  int          i, rc;

  /* Get a handle to an LDAP connection. */
  if ( (ld = ldap_init( HOSTNAME, PORT_NUMBER )) == NULL ) {
    perror( "ldap_init" );
    return( 1 );
  }

  /* Bind anonymously to the LDAP server. */
  rc = ldap_simple_bind_s( ld, NULL, NULL );
  if ( rc != LDAP_SUCCESS ) {
    fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(rc));
    return( 1 );
  }

  /* Search for the entry. */
  if ( ( rc = ldap_search_ext_s( ld, FIND_DN, LDAP_SCOPE_BASE,
    "(objectclass=*)", NULL, 0, NULL, NULL, LDAP_NO_LIMIT,
    LDAP_NO_LIMIT, &result ) ) != LDAP_SUCCESS ) {
    fprintf(stderr, "ldap_search_ext_s: %s\n", ldap_err2string(rc));
    return( 1 );
  }

  /* Since we are doing a base search, there should be only
     one matching entry. */
  e = ldap_first_entry( ld, result );
  if ( e != NULL ) {
    printf( "\nFound %s:\n\n", FIND_DN );

    /* Iterate through each attribute in the entry. */
    for ( a = ldap_first_attribute( ld, e, &ber );
      a != NULL; a = ldap_next_attribute( ld, e, ber ) ) {

      /* For each attribute, print the attribute name and values. */
      if ((vals = ldap_get_values( ld, e, a)) != NULL ) {
        for ( i = 0; vals[i] != NULL; i++ ) {
          printf( "%s: %s\n", a, vals[i] );
        }
        ldap_value_free( vals );
      }
      ldap_memfree( a );
    }
    if ( ber != NULL ) {
      ber_free( ber, 0 );
    }
  }
  ldap_msgfree( result );
  ldap_unbind( ld );
  return( 0 );
}
-------------------------

Thank you
Stephen




reply via email to

[Prev in Thread] Current Thread [Next in Thread]