Skip to content

Commit

Permalink
Merge pull request #1 from msva/master
Browse files Browse the repository at this point in the history
a little bundle
  • Loading branch information
mwild1 committed Jun 15, 2013
2 parents 3a50722 + 49c376e commit 1e9a1be
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ $(COMPAT_DIR)/compat-5.1.o: $(COMPAT_DIR)/compat-5.1.c
$(CC) -c $(CFLAGS) -o $@ $(COMPAT_DIR)/compat-5.1.c

install: src/$(LIBNAME)
mkdir -p $(LUA_LIBDIR)
cp src/$(LIBNAME) $(LUA_LIBDIR)
cd $(LUA_LIBDIR); ln -f -s $(LIBNAME) $T.so
mkdir -p $(DESTDIR)$(LUA_LIBDIR)
cp src/$(LIBNAME) $(DESTDIR)$(LUA_LIBDIR)
cd $(DESTDIR)$(LUA_LIBDIR); ln -f -s $(LIBNAME) $T.so

clean:
rm -f $(OBJS) src/$(LIBNAME)
2 changes: 1 addition & 1 deletion config
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LIB_OPTION= -shared #for Linux
#LIB_OPTION= -bundle -undefined dynamic_lookup #for MacOS X

# Lua version number (first and second digits of target version)
LUA_VERSION_NUM= 500
LUA_VERSION_NUM= 501
LIBNAME= $T.so.$V
COMPAT_DIR= ../compat/src

Expand Down
73 changes: 57 additions & 16 deletions src/lualdap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <stdlib.h>
#include <string.h>
#include <assert.h>

#ifdef WIN32
#include <Winsock2.h>
Expand All @@ -19,10 +20,14 @@
#include "ldap.h"
#endif

#include "lua.h"
#include "lauxlib.h"
#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
#include "compat-5.1.h"
#include <lua.h>
#include <lauxlib.h>

#if LUA_VERSION_NUM < 502
/* lua_rawlen: Not entirely correct, but should work anyway */
# define lua_rawlen lua_objlen
# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
# define luaL_setfuncs(L,l,n) (assert(n==0), luaL_register(L,NULL,l))
#endif

#ifdef WINLDAPAPI
Expand Down Expand Up @@ -243,7 +248,7 @@ static BerValue *A_setbval (lua_State *L, attrs_data *a, const char *n) {
value_error (L, n);
return NULL;
}
a->bvals[a->bi].bv_len = lua_strlen (L, -1);
a->bvals[a->bi].bv_len = lua_rawlen (L, -1);
a->bvals[a->bi].bv_val = (char *)lua_tostring (L, -1);
a->bi++;
return ret;
Expand Down Expand Up @@ -296,7 +301,7 @@ static BerValue **A_tab2val (lua_State *L, attrs_data *a, const char *name) {
A_setval (L, a, name);
else if (lua_istable (L, tab)) { /* list of strings */
int i;
int n = luaL_getn (L, tab);
int n = lua_rawlen (L, tab);
for (i = 1; i <= n; i++) {
lua_rawgeti (L, tab, i); /* push table element */
A_setval (L, a, name);
Expand Down Expand Up @@ -368,7 +373,7 @@ static int table2strarray (lua_State *L, int tab, char *array[], int limit) {
array[1] = NULL;
} else if (lua_istable (L, tab)) {
int i;
int n = luaL_getn (L, tab);
int n = lua_rawlen (L, tab);
if (limit < (n+1))
return luaL_error (L, LUALDAP_PREFIX"too many arguments");
for (i = 0; i < n; i++) {
Expand Down Expand Up @@ -436,10 +441,14 @@ static int result_message (lua_State *L) {
default:
lua_pushnil (L);
lua_pushliteral (L, LUALDAP_PREFIX);
lua_pushstring (L, msg);
lua_pushliteral (L, " ");
lua_pushstring (L, ldap_err2string(err));
lua_concat (L, 4);
lua_concat (L, 2);
if (msg != NULL) {
lua_pushliteral (L, " (");
lua_pushstring (L, msg);
lua_pushliteral (L, ")");
lua_concat (L, 4);
}
ret = 2;
}
ldap_memfree (mdn);
Expand Down Expand Up @@ -473,7 +482,11 @@ static int lualdap_close (lua_State *L) {
luaL_argcheck(L, conn!=NULL, 1, LUALDAP_PREFIX"LDAP connection expected");
if (conn->ld == NULL) /* already closed */
return 0;
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && LDAP_API_FEATURE_X_OPENLDAP >= 20300
ldap_unbind_ext (conn->ld, NULL, NULL);
#else
ldap_unbind (conn->ld);
#endif
conn->ld = NULL;
lua_pushnumber (L, 1);
return 1;
Expand Down Expand Up @@ -516,7 +529,7 @@ static int lualdap_compare (lua_State *L) {
BerValue bvalue;
ldap_int_t rc, msgid;
bvalue.bv_val = (char *)luaL_checkstring (L, 4);
bvalue.bv_len = lua_strlen (L, 4);
bvalue.bv_len = lua_rawlen (L, 4);
rc = ldap_compare_ext (conn->ld, dn, attr, &bvalue, NULL, NULL, &msgid);
return create_future (L, rc, 1, msgid, LDAP_RES_COMPARE);
}
Expand Down Expand Up @@ -683,6 +696,8 @@ static int next_message (lua_State *L) {
int rc;
int ret;

luaL_checktype(L, 1, LUA_TTABLE);

lua_rawgeti (L, LUA_REGISTRYINDEX, search->conn);
conn = (conn_data *)lua_touserdata (L, -1); /* get connection */

Expand Down Expand Up @@ -829,7 +844,8 @@ static int lualdap_search (lua_State *L) {

create_search (L, 1, msgid);
lua_pushcclosure (L, next_message, 1);
return 1;
lua_pushvalue(L, 2);
return 2;
}


Expand Down Expand Up @@ -870,7 +886,7 @@ static int lualdap_search_tostring (lua_State *L) {
** Create a metatable.
*/
static int lualdap_createmeta (lua_State *L) {
const luaL_reg methods[] = {
const luaL_Reg methods[] = {
{"close", lualdap_close},
{"add", lualdap_add},
{"compare", lualdap_compare},
Expand All @@ -885,7 +901,7 @@ static int lualdap_createmeta (lua_State *L) {
return 0;

/* define methods */
luaL_openlib (L, NULL, methods, 0);
luaL_setfuncs(L, methods, 0);

/* define metamethods */
lua_pushliteral (L, "__gc");
Expand Down Expand Up @@ -937,13 +953,27 @@ static int lualdap_open_simple (lua_State *L) {
const char *password = luaL_optstring (L, 3, NULL);
int use_tls = lua_toboolean (L, 4);
conn_data *conn = (conn_data *)lua_newuserdata (L, sizeof(conn_data));
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && LDAP_API_FEATURE_X_OPENLDAP >= 20300
struct berval cred = { 0, NULL };
char *host_with_schema = NULL;
#endif
int err;

/* Initialize */
lualdap_setmeta (L, LUALDAP_CONNECTION_METATABLE);
conn->version = 0;
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && LDAP_API_FEATURE_X_OPENLDAP >= 20300
host_with_schema = malloc(strlen(host) + 8);
strcpy(host_with_schema, "ldap://");
strcat(host_with_schema, host);
err = ldap_initialize(&conn->ld, host_with_schema);
free(host_with_schema);
host_with_schema = NULL;
if (err != LDAP_SUCCESS)
#else
conn->ld = ldap_init (host, LDAP_PORT);
if (conn->ld == NULL)
#endif
return faildirect(L,LUALDAP_PREFIX"Error connecting to server");
/* Set protocol version */
conn->version = LDAP_VERSION3;
Expand All @@ -957,7 +987,16 @@ static int lualdap_open_simple (lua_State *L) {
return faildirect (L, ldap_err2string (rc));
}
/* Bind to a server */
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && LDAP_API_FEATURE_X_OPENLDAP >= 20300
cred.bv_len = strlen(password);
cred.bv_val = malloc(cred.bv_len+1);
strcpy(cred.bv_val, password);
err = ldap_sasl_bind_s (conn->ld, who, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
free(cred.bv_val);
memset(&cred, 0, sizeof(cred));
#else
err = ldap_bind_s (conn->ld, who, password, LDAP_AUTH_SIMPLE);
#endif
if (err != LDAP_SUCCESS)
return faildirect (L, ldap_err2string (err));

Expand Down Expand Up @@ -985,13 +1024,15 @@ static void set_info (lua_State *L) {
** Create ldap table and register the open method.
*/
int luaopen_lualdap (lua_State *L) {
struct luaL_reg lualdap[] = {
struct luaL_Reg lualdap[] = {
{"open_simple", lualdap_open_simple},
{NULL, NULL},
};

lualdap_createmeta (L);
luaL_openlib (L, LUALDAP_TABLENAME, lualdap, 0);
luaL_newlib(L, lualdap);
lua_pushvalue(L, -1);
lua_setglobal(L, LUALDAP_TABLENAME);
set_info (L);

return 1;
Expand Down

0 comments on commit 1e9a1be

Please sign in to comment.