Skip to content

Instantly share code, notes, and snippets.

@sarahhodne
Created April 5, 2009 01:20
Show Gist options
  • Save sarahhodne/90344 to your computer and use it in GitHub Desktop.
Save sarahhodne/90344 to your computer and use it in GitHub Desktop.
require "mkmf"
dir_config("ldap")
have_library("ldap", "ldap_open")
create_makefile("ldap_bindings")
#include <ldap.h>
#include "ruby.h"
static int id_url;
static int id_base;
static int id_scope;
static int id_filter;
static int id_attrsonly;
VALUE cLDAP;
VALUE cLDAP_Entry;
static void ldap_free(void *p) {
ldap_memfree(p);
}
static void ldap_message_free(void *p) {
ldap_msgfree(p);
}
static VALUE t_init(VALUE self, VALUE options) {
VALUE url;
LDAP *ldap;
url = rb_hash_aref(options, ID2SYM(id_url));
printf("Data_Get_Struct(self, LDAP, ldap);\n");
Data_Get_Struct(self, LDAP, ldap);
printf("ldap_initialize(ldap, RSTRING(url)->ptr);\n");
printf("%s\n", RSTRING(url)->ptr);
ldap_initialize(ldap, RSTRING(url)->ptr);
printf("return self;\n");
return self;
}
static VALUE t_search(VALUE self, VALUE options) {
VALUE result;
LDAP *ldap;
LDAPMessage *ldapm;
VALUE base, scope, filter, attrsonly; // Options
LDAPControl **serverctrls;
LDAPControl **clientctrls;
// Get the LDAP objects
Data_Get_Struct(self, LDAP, ldap);
// Make the result array
result = rb_ary_new();
// Get the options
base = rb_hash_aref(options, ID2SYM(id_base));
scope = rb_hash_aref(options, ID2SYM(id_scope));
filter = rb_hash_aref(options, ID2SYM(id_filter));
attrsonly = rb_hash_aref(options, ID2SYM(id_attrsonly));
ldap_search_ext_s(
ldap,
RSTRING(base)->ptr,
FIX2INT(scope),
RSTRING(filter)->ptr,
RTEST(attrsonly),
0,
serverctrls,
clientctrls,
NULL,
1000,
ldapm);
return result;
}
static VALUE ldap_alloc(VALUE klass) {
LDAP *ldap;
VALUE obj;
// Wrap LDAP object inside a Ruby LDAP object
obj = Data_Wrap_Struct(klass, 0, ldap_free, ldap);
return obj;
}
void Init_ldap_bindings() {
cLDAP = rb_define_class("LDAP", rb_cObject);
cLDAP_Entry = rb_define_class("LDAP::Entry", rb_cObject);
// Define Scope constants
rb_define_const(cLDAP, "SCOPE_ONELEVEL", INT2FIX(LDAP_SCOPE_ONELEVEL));
rb_define_const(cLDAP, "SCOPE_SUBTREE", INT2FIX(LDAP_SCOPE_SUBTREE));
rb_define_const(cLDAP, "SCOPE_CHILDREN", INT2FIX(LDAP_SCOPE_CHILDREN));
// Allocator function (to use the LDAP object)
rb_define_alloc_func(cLDAP, ldap_alloc);
// Define methods
rb_define_method(cLDAP, "initialize", t_init, 1);
rb_define_method(cLDAP, "search", t_search, 1);
// Define symbols used
id_url = rb_intern("url");
id_base = rb_intern("base");
id_scope = rb_intern("scope");
id_filter = rb_intern("filter");
id_attrsonly = rb_intern("attrsonly");
}
require 'ldap_bindings'
l = LDAP.new :url => "ldap://ldap.cluenet.org:389/"
l.search :scope => LDAP::SCOPE_CHILDREN, :base => "ou=people,dc=cluenet,dc=org", :filter => "uid=dvyjones"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment