Back to index

courier  0.68.2
Classes | Functions
libldapsearch.h File Reference
#include "config.h"
#include <ldap.h>
#include <stdio.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  ldapsearch

Functions

struct ldapsearchl_search_alloc (const char *host, int port, const char *userid, const char *password, const char *base)
void l_search_free (struct ldapsearch *s)
int l_search_do (struct ldapsearch *s, const char *lookupkey, int(*callback_func)(const char *utf8_name, const char *address, void *callback_arg), void *callback_arg)
int l_search_ping (struct ldapsearch *s)

Class Documentation

struct ldapsearch

Definition at line 24 of file libldapsearch.h.

Class Members
char * base
LDAP * handle

Function Documentation

struct ldapsearch* l_search_alloc ( const char *  host,
int  port,
const char *  userid,
const char *  password,
const char *  base 
) [read]

Definition at line 26 of file libldapsearch.c.

{
       char *buf;

       struct ldapsearch *p=(struct ldapsearch *)
              malloc(sizeof(struct ldapsearch));

       if (!p)
              return NULL;

       if ((p->base=strdup(base)) == NULL)
       {
              free(p);
              return NULL;
       }

       if ((buf=malloc(strlen(host)+100)) == NULL)
       {
              free(p->base);
              free(p);
              return NULL;
       }

       sprintf(buf, "ldap://%s:%d", host, port);

       if (ldap_initialize(&p->handle, buf) != LDAP_SUCCESS)
       {
              free(buf);
              free(p->base);
              free(p);
              return NULL;
       }
       free(buf);

       if (userid && *userid)
       {
              struct berval cred;

              cred.bv_len=password && *password ? strlen(password):0;
              cred.bv_val=password && *password ? (char *)password:NULL;

              if (ldap_sasl_bind_s(p->handle, userid, NULL, &cred, NULL,
                                 NULL, NULL))
              {
                     l_search_free(p);
                     errno=EPERM;
                     return NULL;
              }
       }

       return p;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int l_search_do ( struct ldapsearch s,
const char *  lookupkey,
int(*)(const char *utf8_name, const char *address, void *callback_arg)  callback_func,
void *  callback_arg 
)

Definition at line 195 of file libldapsearch.c.

{
       char *k;
       const char *filter;
       int rc_code;
       int found;

       k=encode_key(lookupkey);
       if (!k)
              return -1;

       filter=getenv("LDAP_SEARCH_FILTER_EXACT");
       if (!filter)
              filter="(|(uid=@)(sn=@)(cn=@))";

       rc_code=l_search_do_filter(s, callback_func, callback_arg,
                               filter, k, &found);

       if (rc_code == 0 && !found)
       {
              filter=getenv("LDAP_SEARCH_FILTER_APPROX");
              if (!filter)
                     filter="(|(uid=@*)(sn=@*)(mail=@*)(cn=@*))";

              rc_code=l_search_do_filter(s, callback_func, callback_arg,
                                      filter, k, &found);
       }
       free(k);
       return rc_code;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void l_search_free ( struct ldapsearch s)

Definition at line 83 of file libldapsearch.c.

{
       if (s->handle)
              ldap_unbind_ext(s->handle, NULL, NULL);
       free(s->base);
       free(s);
}

Here is the caller graph for this function:

int l_search_ping ( struct ldapsearch s)

Definition at line 365 of file libldapsearch.c.

{
       char *attrs[2];
       struct timeval tv;
       LDAPMessage *result;
       int rc;
       int msgid;

       if (s->handle == NULL)
       {
              errno=ETIMEDOUT;  /* Timeout previously */
              return -1;
       }

       attrs[0]="objectClass";
       attrs[1]=NULL;

       tv.tv_sec=60*60;
       tv.tv_usec=0;

       if (ldap_search_ext(s->handle, s->base, LDAP_SCOPE_BASE,
                         "objectClass=*", attrs, 0, NULL, NULL, &tv,
                         1000000, &msgid) < 0)
              return -1;

       do
       {
              const char *timeout=getenv("LDAP_SEARCH_TIMEOUT");

              tv.tv_sec=atoi(timeout ? timeout:"30");
              tv.tv_usec=0;

              rc=ldap_result(s->handle, msgid, 0, &tv, &result);

              if (rc <= 0)
              {
                     if (rc == 0)
                            errno=ETIMEDOUT;

                     ldap_unbind_ext(s->handle, NULL, NULL);
                     s->handle=NULL;

                     return -1;
              }

              ldap_msgfree(result);
       } while (rc != LDAP_RES_SEARCH_RESULT);
       return 0;
}

Here is the call graph for this function: