Created
April 5, 2009 09:53
-
-
Save sarahhodne/90421 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ldap.h> | |
#include "ruby.h" | |
#include "rldap.h" | |
#include "connection.h" | |
#include "errors.h" | |
static void ldap_c_free (RB_LDAP_DATA * ldapdata) | |
{ | |
if (ldapdata->ldap && ldapdata->bind) | |
{ | |
ldap_unbind (ldapdata->ldap); | |
}; | |
}; | |
static void ldap_c_mark (RB_LDAP_DATA * ldapdata) | |
{ | |
/* empty */ | |
}; | |
static VALUE ldap_c_new(VALUE klass, LDAP * cldap) { | |
VALUE conn; | |
RB_LDAP_DATA *ldapdata; | |
conn = Data_Make_Struct(klass, RB_LDAP_DATA, ldap_c_mark, ldap_c_free, ldapdata); | |
ldapdata->ldap = cldap; | |
ldapdata->err = 0; | |
ldapdata->bind = 0; | |
return conn; | |
} | |
static VALUE ldap_c_alloc(VALUE klass) { | |
return ldap_c_new(klass, (LDAP *) 0); | |
} | |
static VALUE ldap_c_initialize(VALUE self, VALUE url) { | |
RB_LDAP_DATA *ldapdata; | |
Data_Get_Struct(self, RB_LDAP_DATA, ldapdata); | |
ldap_initialize(&ldapdata->ldap, RSTRING(url)->ptr); | |
if (!ldapdata->ldap) | |
{ | |
rb_raise(rb_eLDAP_ConnectionError, "can't initialize an LDAP session"); | |
} | |
return self; | |
} | |
static VALUE ldap_c_search(int argc, VALUE *argv, VALUE self) { | |
LDAPMessage **cmsg; | |
RB_LDAP_DATA *ldapdata; | |
VALUE scope, filter, base, attrs, attrsonly, tmp; | |
LDAPControl **sctrls, **cctrls; | |
struct timeval tv; | |
LDAP *cldap; | |
int i; | |
char **cattrs; | |
char *cbase; | |
int cscope; | |
int climit; | |
char *cfilter; | |
int cattrsonly; | |
int retval; | |
cattrs = NULL; | |
sctrls = NULL; | |
cctrls = NULL; | |
cattrsonly = 0; | |
climit = 0; | |
GET_LDAP_DATA(self, ldapdata); | |
cldap = ldapdata->ldap; | |
tv.tv_sec = 0; | |
tv.tv_usec = 0; | |
switch (rb_scan_args(argc, argv, "32", &base, &scope, &filter, &attrs, &attrsonly)) { | |
case 5: | |
cattrsonly = (attrsonly == Qtrue) ? 1 : 0; | |
case 4: | |
if (TYPE (attrs) == T_NIL) { | |
cattrs = NULL; | |
} else { | |
if (TYPE (attrs) == T_STRING) | |
attrs = rb_ary_to_ary (attrs); | |
else | |
Check_Type (attrs, T_ARRAY); | |
if (RARRAY (attrs)->len == 0) { | |
cattrs = NULL; | |
} else { | |
cattrs = ALLOCA_N (char *, (RARRAY (attrs)->len + 1)); | |
for (i = 0; i < RARRAY (attrs)->len; i++) { | |
cattrs[i] = StringValueCStr (RARRAY (attrs)->ptr[i]); | |
}; | |
cattrs[RARRAY (attrs)->len] = NULL; | |
} | |
} | |
case 3: | |
cbase = StringValueCStr (base); | |
cscope = NUM2INT (scope); | |
cfilter = StringValueCStr (filter); | |
break; | |
default: | |
rb_bug ("ldap_c_search"); | |
}; | |
retval = ldap_search_ext_s (cldap, cbase, cscope, cfilter, | |
cattrs, cattrsonly, | |
sctrls, cctrls, | |
NULL, climit, cmsg); | |
printf(ldap_err2string(retval)); | |
printf("\n"); | |
return INT2FIX(retval); | |
} | |
void Init_ldap_connection() { | |
rb_cLDAP_Connection = rb_define_class_under(rb_mLDAP, "Connection", rb_cObject); | |
rb_define_alloc_func(rb_cLDAP_Connection, ldap_c_alloc); | |
rb_define_method(rb_cLDAP_Connection, "initialize", ldap_c_initialize, 1); | |
rb_define_method(rb_cLDAP_Connection, "search", ldap_c_search, -1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment