help-smalltalk
[Top][All Lists]
Advanced

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

[Help-smalltalk] Re: Calling LDAP C API from Smalltalk


From: Stephen
Subject: [Help-smalltalk] Re: Calling LDAP C API from Smalltalk
Date: Fri, 26 Sep 2008 22:20:54 +1200
User-agent: Thunderbird 2.0.0.16 (Macintosh/20080707)

Hi,

Paolo, I've been working through the program outline you replied with and have an authenticated connection to the LDAP server working. I'm now trying to call ldap_search to get some LDAP entries returned, but have an error when I call the search API.

I've included the script code below at the end of this message... if the email client wraps and makes it difficult to use, I can make it available on our web server.

The error I'm getting is this:-

stephenw$ ./ldapapi.st
started
... about to run the search.
./ldapapi.st:117: Attempt to pass an instance of SmallInteger as a void *
(ip 14)CFunctionDescriptor(CCallable)>>#callInto:
(ip 8)LDAP>>#searchWithBase:scope:filter:attributes:isAttrsOnly:serverctrls:clientctrls:timeout:sizelimit:searchResult:
(ip 62)UndefinedObject>>#executeStatements
(ip 0)<bottom>
Abort trap


So what is causing the error is the question!
Looking at the ldap_search_ext_s definition, all the parameters are quite straightforward except (I think) "scope" and "LDAPMessage".

First of all, re scope, the sample program provided in my first post inserts LDAP_SCOPE_BASE for this parameter and when I looked that up in ldap.h I get
  #define LDAP_SCOPE_BASE                 ((ber_int_t) 0x0000)
I don't know what ber_int_t does, so tried both of 0 and 16r0000 in my program. No change.

The second thing that it would be good to check is the definition in Smalltalk for ldap_search_ext_s, and in particular how I've defined #{LDAPMessage}. Is it correct?

Thank you for your assistance - I trust the fix is something simple.
---

By the way: how is one supposed to define class variables?
I used this link http://www.gnu.org/software/smalltalk/manual/gst.html#Syntax and defined scopeBase and nolimit in the LDAP class. I tried inserting LDAP nolimit as you did in your sample code (timeout: LDAP nolimit),but got an error..
LDAP error: did not understand #nolimit
MessageNotUnderstood(Exception)>>signal (AnsiExcept.st:216)

Cheers

Regards
Stephen

----------------------------------
stephenw$ cat ldapapi.st.post
#!/usr/local/bin/gst -f
"call LDAP API"

"  CObject extend [ "
        "Method commented out for now; wouldn't compile"
"  isNull [ ^address = 0 ]
  ] "

  CObject subclass: LDAPMessage [
      LDAPMessage class >> free [
        <cCall: 'ldap_msgfree' returning: #int args: #(#self)>
        ]
  ]

  CObject subclass: LDAP [
      "class variables"
      scopeBase := 0.
      nolimit := 0.

      LDAP class >> openOn: host port: port [
          <cCall: 'ldap_open' returning: #{LDAP} args: #(#string #int)>
      ]

      LDAP class >> errorString: resultNum [
          "Utility methods might also go on the class side."
          <cCall: 'ldap_err2string' returning: #string args: #(#int)>
      ]

      simpleBindWithDN: who passwd: secret [
<cCall: 'ldap_simple_bind_s' returning: #int args: #(#self #string #string)>
      ]

        " ldap_search_ext_s C definition --------------
        LDAP_F( int )
      ldap_search_ext_s LDAP_P((
        LDAP                    *ld,
        LDAP_CONST char *base,
        int                             scope,
        LDAP_CONST char *filter,
        char                    **attrs,
        int                             attrsonly,
        LDAPControl             **serverctrls,
        LDAPControl             **clientctrls,
        struct timeval  *timeout,
        int                             sizelimit,
        LDAPMessage             **res ));
        rc = ldap_search_ext_s( ld, FIND_DN, LDAP_SCOPE_BASE,
    ""(objectclass=*)"", NULL, 0, NULL, NULL, LDAP_NO_LIMIT,
    LDAP_NO_LIMIT, &result
                                        --------------- "
        searchWithBase: baseDN
          scope: aScope
          filter: aFilter
          attributes: theAttribs
          isAttrsOnly: attrsOnly
          serverctrls: serverControl
          clientctrls: clientControl
          timeout: timeval
          sizelimit: numResults
          searchResult: aSearchResult [
<cCall: 'ldap_search_ext_s' returning: #int args: #(#self #string #int #string #cObject #int #cObject #cObject #cObject #int #{LDAPMessage})>
        ]

        " ldap_first_entry C definition    --------------
        LDAP_F( LDAPMessage * )
        ldap_first_entry LDAP_P((
                LDAP *ld,
                LDAPMessage *chain ));
                                        --------------"
        firstEntry: searchResult [
<cCall: 'ldap_first_entry' returning: #{LDAPMessage} args: #(#self #{LDAPMessage})> ]

  ]

"mainline"
| ldapcall ldap resultCode errorMsg hostName baseDN |
Transcript showCr: 'started'.
DLD addLibrary: 'libldap'.
hostName := 'aserver'.
baseDN := 'ou=Users,dc=example,dc=com'.
supervisorDN := 'uid=sw,ou=Users,dc=example,dc=com'.

ldap := LDAP openOn: hostName port: 389.
ldap isNil ifTrue: [ File checkError. ObjectMemory quit: 1 ].
resultCode := ldap simpleBindWithDN: supervisorDN passwd: 'apwd'.
(resultCode > 0) ifTrue: [
        "Transcript showCr: ('%1 is result from bind' % { resultCode })."
        errorCodeMsg := LDAP errorString: resultCode.
Transcript showCr: 'The LDAP Bind failed with the message: ',errorCodeMsg ].

(resultCode = 0) ifTrue: [
        Transcript showCr: '... about to run the search.'.
        resultCode := ldap searchWithBase: supervisorDN
          scope: 16r0000
          filter: '(objectclass=*)'
          attributes: nil
          isAttrsOnly: 0
          serverctrls: nil
          clientctrls: nil
          timeout: 0
          sizelimit: 0
          searchResult: (searchResult := LDAPMessage new).
        Transcript showCr: '... search completed'.      
]


"
entry := ldap firstEntry: searchResult.
entry isNil ifFalse: [
        ('Found results with base DN: ', baseDN) displayN1.
] "

!





reply via email to

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