Skip to content

Commit 1e9a1be

Browse files
committedJun 15, 2013
Merge pull request #1 from msva/master
a little bundle
2 parents 3a50722 + 49c376e commit 1e9a1be

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed
 

‎Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ $(COMPAT_DIR)/compat-5.1.o: $(COMPAT_DIR)/compat-5.1.c
2020
$(CC) -c $(CFLAGS) -o $@ $(COMPAT_DIR)/compat-5.1.c
2121

2222
install: src/$(LIBNAME)
23-
mkdir -p $(LUA_LIBDIR)
24-
cp src/$(LIBNAME) $(LUA_LIBDIR)
25-
cd $(LUA_LIBDIR); ln -f -s $(LIBNAME) $T.so
23+
mkdir -p $(DESTDIR)$(LUA_LIBDIR)
24+
cp src/$(LIBNAME) $(DESTDIR)$(LUA_LIBDIR)
25+
cd $(DESTDIR)$(LUA_LIBDIR); ln -f -s $(LIBNAME) $T.so
2626

2727
clean:
2828
rm -f $(OBJS) src/$(LIBNAME)

‎config

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LIB_OPTION= -shared #for Linux
1313
#LIB_OPTION= -bundle -undefined dynamic_lookup #for MacOS X
1414

1515
# Lua version number (first and second digits of target version)
16-
LUA_VERSION_NUM= 500
16+
LUA_VERSION_NUM= 501
1717
LIBNAME= $T.so.$V
1818
COMPAT_DIR= ../compat/src
1919

‎src/lualdap.c

+57-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <stdlib.h>
88
#include <string.h>
9+
#include <assert.h>
910

1011
#ifdef WIN32
1112
#include <Winsock2.h>
@@ -19,10 +20,14 @@
1920
#include "ldap.h"
2021
#endif
2122

22-
#include "lua.h"
23-
#include "lauxlib.h"
24-
#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
25-
#include "compat-5.1.h"
23+
#include <lua.h>
24+
#include <lauxlib.h>
25+
26+
#if LUA_VERSION_NUM < 502
27+
/* lua_rawlen: Not entirely correct, but should work anyway */
28+
# define lua_rawlen lua_objlen
29+
# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
30+
# define luaL_setfuncs(L,l,n) (assert(n==0), luaL_register(L,NULL,l))
2631
#endif
2732

2833
#ifdef WINLDAPAPI
@@ -243,7 +248,7 @@ static BerValue *A_setbval (lua_State *L, attrs_data *a, const char *n) {
243248
value_error (L, n);
244249
return NULL;
245250
}
246-
a->bvals[a->bi].bv_len = lua_strlen (L, -1);
251+
a->bvals[a->bi].bv_len = lua_rawlen (L, -1);
247252
a->bvals[a->bi].bv_val = (char *)lua_tostring (L, -1);
248253
a->bi++;
249254
return ret;
@@ -296,7 +301,7 @@ static BerValue **A_tab2val (lua_State *L, attrs_data *a, const char *name) {
296301
A_setval (L, a, name);
297302
else if (lua_istable (L, tab)) { /* list of strings */
298303
int i;
299-
int n = luaL_getn (L, tab);
304+
int n = lua_rawlen (L, tab);
300305
for (i = 1; i <= n; i++) {
301306
lua_rawgeti (L, tab, i); /* push table element */
302307
A_setval (L, a, name);
@@ -368,7 +373,7 @@ static int table2strarray (lua_State *L, int tab, char *array[], int limit) {
368373
array[1] = NULL;
369374
} else if (lua_istable (L, tab)) {
370375
int i;
371-
int n = luaL_getn (L, tab);
376+
int n = lua_rawlen (L, tab);
372377
if (limit < (n+1))
373378
return luaL_error (L, LUALDAP_PREFIX"too many arguments");
374379
for (i = 0; i < n; i++) {
@@ -436,10 +441,14 @@ static int result_message (lua_State *L) {
436441
default:
437442
lua_pushnil (L);
438443
lua_pushliteral (L, LUALDAP_PREFIX);
439-
lua_pushstring (L, msg);
440-
lua_pushliteral (L, " ");
441444
lua_pushstring (L, ldap_err2string(err));
442-
lua_concat (L, 4);
445+
lua_concat (L, 2);
446+
if (msg != NULL) {
447+
lua_pushliteral (L, " (");
448+
lua_pushstring (L, msg);
449+
lua_pushliteral (L, ")");
450+
lua_concat (L, 4);
451+
}
443452
ret = 2;
444453
}
445454
ldap_memfree (mdn);
@@ -473,7 +482,11 @@ static int lualdap_close (lua_State *L) {
473482
luaL_argcheck(L, conn!=NULL, 1, LUALDAP_PREFIX"LDAP connection expected");
474483
if (conn->ld == NULL) /* already closed */
475484
return 0;
485+
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && LDAP_API_FEATURE_X_OPENLDAP >= 20300
486+
ldap_unbind_ext (conn->ld, NULL, NULL);
487+
#else
476488
ldap_unbind (conn->ld);
489+
#endif
477490
conn->ld = NULL;
478491
lua_pushnumber (L, 1);
479492
return 1;
@@ -516,7 +529,7 @@ static int lualdap_compare (lua_State *L) {
516529
BerValue bvalue;
517530
ldap_int_t rc, msgid;
518531
bvalue.bv_val = (char *)luaL_checkstring (L, 4);
519-
bvalue.bv_len = lua_strlen (L, 4);
532+
bvalue.bv_len = lua_rawlen (L, 4);
520533
rc = ldap_compare_ext (conn->ld, dn, attr, &bvalue, NULL, NULL, &msgid);
521534
return create_future (L, rc, 1, msgid, LDAP_RES_COMPARE);
522535
}
@@ -683,6 +696,8 @@ static int next_message (lua_State *L) {
683696
int rc;
684697
int ret;
685698

699+
luaL_checktype(L, 1, LUA_TTABLE);
700+
686701
lua_rawgeti (L, LUA_REGISTRYINDEX, search->conn);
687702
conn = (conn_data *)lua_touserdata (L, -1); /* get connection */
688703

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

830845
create_search (L, 1, msgid);
831846
lua_pushcclosure (L, next_message, 1);
832-
return 1;
847+
lua_pushvalue(L, 2);
848+
return 2;
833849
}
834850

835851

@@ -870,7 +886,7 @@ static int lualdap_search_tostring (lua_State *L) {
870886
** Create a metatable.
871887
*/
872888
static int lualdap_createmeta (lua_State *L) {
873-
const luaL_reg methods[] = {
889+
const luaL_Reg methods[] = {
874890
{"close", lualdap_close},
875891
{"add", lualdap_add},
876892
{"compare", lualdap_compare},
@@ -885,7 +901,7 @@ static int lualdap_createmeta (lua_State *L) {
885901
return 0;
886902

887903
/* define methods */
888-
luaL_openlib (L, NULL, methods, 0);
904+
luaL_setfuncs(L, methods, 0);
889905

890906
/* define metamethods */
891907
lua_pushliteral (L, "__gc");
@@ -937,13 +953,27 @@ static int lualdap_open_simple (lua_State *L) {
937953
const char *password = luaL_optstring (L, 3, NULL);
938954
int use_tls = lua_toboolean (L, 4);
939955
conn_data *conn = (conn_data *)lua_newuserdata (L, sizeof(conn_data));
956+
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && LDAP_API_FEATURE_X_OPENLDAP >= 20300
957+
struct berval cred = { 0, NULL };
958+
char *host_with_schema = NULL;
959+
#endif
940960
int err;
941961

942962
/* Initialize */
943963
lualdap_setmeta (L, LUALDAP_CONNECTION_METATABLE);
944964
conn->version = 0;
965+
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && LDAP_API_FEATURE_X_OPENLDAP >= 20300
966+
host_with_schema = malloc(strlen(host) + 8);
967+
strcpy(host_with_schema, "ldap://");
968+
strcat(host_with_schema, host);
969+
err = ldap_initialize(&conn->ld, host_with_schema);
970+
free(host_with_schema);
971+
host_with_schema = NULL;
972+
if (err != LDAP_SUCCESS)
973+
#else
945974
conn->ld = ldap_init (host, LDAP_PORT);
946975
if (conn->ld == NULL)
976+
#endif
947977
return faildirect(L,LUALDAP_PREFIX"Error connecting to server");
948978
/* Set protocol version */
949979
conn->version = LDAP_VERSION3;
@@ -957,7 +987,16 @@ static int lualdap_open_simple (lua_State *L) {
957987
return faildirect (L, ldap_err2string (rc));
958988
}
959989
/* Bind to a server */
990+
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && LDAP_API_FEATURE_X_OPENLDAP >= 20300
991+
cred.bv_len = strlen(password);
992+
cred.bv_val = malloc(cred.bv_len+1);
993+
strcpy(cred.bv_val, password);
994+
err = ldap_sasl_bind_s (conn->ld, who, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
995+
free(cred.bv_val);
996+
memset(&cred, 0, sizeof(cred));
997+
#else
960998
err = ldap_bind_s (conn->ld, who, password, LDAP_AUTH_SIMPLE);
999+
#endif
9611000
if (err != LDAP_SUCCESS)
9621001
return faildirect (L, ldap_err2string (err));
9631002

@@ -985,13 +1024,15 @@ static void set_info (lua_State *L) {
9851024
** Create ldap table and register the open method.
9861025
*/
9871026
int luaopen_lualdap (lua_State *L) {
988-
struct luaL_reg lualdap[] = {
1027+
struct luaL_Reg lualdap[] = {
9891028
{"open_simple", lualdap_open_simple},
9901029
{NULL, NULL},
9911030
};
9921031

9931032
lualdap_createmeta (L);
994-
luaL_openlib (L, LUALDAP_TABLENAME, lualdap, 0);
1033+
luaL_newlib(L, lualdap);
1034+
lua_pushvalue(L, -1);
1035+
lua_setglobal(L, LUALDAP_TABLENAME);
9951036
set_info (L);
9961037

9971038
return 1;

0 commit comments

Comments
 (0)
Please sign in to comment.