6
6
7
7
#include <stdlib.h>
8
8
#include <string.h>
9
+ #include <assert.h>
9
10
10
11
#ifdef WIN32
11
12
#include <Winsock2.h>
19
20
#include "ldap.h"
20
21
#endif
21
22
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))
26
31
#endif
27
32
28
33
#ifdef WINLDAPAPI
@@ -243,7 +248,7 @@ static BerValue *A_setbval (lua_State *L, attrs_data *a, const char *n) {
243
248
value_error (L , n );
244
249
return NULL ;
245
250
}
246
- a -> bvals [a -> bi ].bv_len = lua_strlen (L , -1 );
251
+ a -> bvals [a -> bi ].bv_len = lua_rawlen (L , -1 );
247
252
a -> bvals [a -> bi ].bv_val = (char * )lua_tostring (L , -1 );
248
253
a -> bi ++ ;
249
254
return ret ;
@@ -296,7 +301,7 @@ static BerValue **A_tab2val (lua_State *L, attrs_data *a, const char *name) {
296
301
A_setval (L , a , name );
297
302
else if (lua_istable (L , tab )) { /* list of strings */
298
303
int i ;
299
- int n = luaL_getn (L , tab );
304
+ int n = lua_rawlen (L , tab );
300
305
for (i = 1 ; i <= n ; i ++ ) {
301
306
lua_rawgeti (L , tab , i ); /* push table element */
302
307
A_setval (L , a , name );
@@ -368,7 +373,7 @@ static int table2strarray (lua_State *L, int tab, char *array[], int limit) {
368
373
array [1 ] = NULL ;
369
374
} else if (lua_istable (L , tab )) {
370
375
int i ;
371
- int n = luaL_getn (L , tab );
376
+ int n = lua_rawlen (L , tab );
372
377
if (limit < (n + 1 ))
373
378
return luaL_error (L , LUALDAP_PREFIX "too many arguments" );
374
379
for (i = 0 ; i < n ; i ++ ) {
@@ -436,10 +441,14 @@ static int result_message (lua_State *L) {
436
441
default :
437
442
lua_pushnil (L );
438
443
lua_pushliteral (L , LUALDAP_PREFIX );
439
- lua_pushstring (L , msg );
440
- lua_pushliteral (L , " " );
441
444
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
+ }
443
452
ret = 2 ;
444
453
}
445
454
ldap_memfree (mdn );
@@ -473,7 +482,11 @@ static int lualdap_close (lua_State *L) {
473
482
luaL_argcheck (L , conn != NULL , 1 , LUALDAP_PREFIX "LDAP connection expected" );
474
483
if (conn -> ld == NULL ) /* already closed */
475
484
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
476
488
ldap_unbind (conn -> ld );
489
+ #endif
477
490
conn -> ld = NULL ;
478
491
lua_pushnumber (L , 1 );
479
492
return 1 ;
@@ -516,7 +529,7 @@ static int lualdap_compare (lua_State *L) {
516
529
BerValue bvalue ;
517
530
ldap_int_t rc , msgid ;
518
531
bvalue .bv_val = (char * )luaL_checkstring (L , 4 );
519
- bvalue .bv_len = lua_strlen (L , 4 );
532
+ bvalue .bv_len = lua_rawlen (L , 4 );
520
533
rc = ldap_compare_ext (conn -> ld , dn , attr , & bvalue , NULL , NULL , & msgid );
521
534
return create_future (L , rc , 1 , msgid , LDAP_RES_COMPARE );
522
535
}
@@ -683,6 +696,8 @@ static int next_message (lua_State *L) {
683
696
int rc ;
684
697
int ret ;
685
698
699
+ luaL_checktype (L , 1 , LUA_TTABLE );
700
+
686
701
lua_rawgeti (L , LUA_REGISTRYINDEX , search -> conn );
687
702
conn = (conn_data * )lua_touserdata (L , -1 ); /* get connection */
688
703
@@ -829,7 +844,8 @@ static int lualdap_search (lua_State *L) {
829
844
830
845
create_search (L , 1 , msgid );
831
846
lua_pushcclosure (L , next_message , 1 );
832
- return 1 ;
847
+ lua_pushvalue (L , 2 );
848
+ return 2 ;
833
849
}
834
850
835
851
@@ -870,7 +886,7 @@ static int lualdap_search_tostring (lua_State *L) {
870
886
** Create a metatable.
871
887
*/
872
888
static int lualdap_createmeta (lua_State * L ) {
873
- const luaL_reg methods [] = {
889
+ const luaL_Reg methods [] = {
874
890
{"close" , lualdap_close },
875
891
{"add" , lualdap_add },
876
892
{"compare" , lualdap_compare },
@@ -885,7 +901,7 @@ static int lualdap_createmeta (lua_State *L) {
885
901
return 0 ;
886
902
887
903
/* define methods */
888
- luaL_openlib ( L , NULL , methods , 0 );
904
+ luaL_setfuncs ( L , methods , 0 );
889
905
890
906
/* define metamethods */
891
907
lua_pushliteral (L , "__gc" );
@@ -937,13 +953,27 @@ static int lualdap_open_simple (lua_State *L) {
937
953
const char * password = luaL_optstring (L , 3 , NULL );
938
954
int use_tls = lua_toboolean (L , 4 );
939
955
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
940
960
int err ;
941
961
942
962
/* Initialize */
943
963
lualdap_setmeta (L , LUALDAP_CONNECTION_METATABLE );
944
964
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
945
974
conn -> ld = ldap_init (host , LDAP_PORT );
946
975
if (conn -> ld == NULL )
976
+ #endif
947
977
return faildirect (L ,LUALDAP_PREFIX "Error connecting to server" );
948
978
/* Set protocol version */
949
979
conn -> version = LDAP_VERSION3 ;
@@ -957,7 +987,16 @@ static int lualdap_open_simple (lua_State *L) {
957
987
return faildirect (L , ldap_err2string (rc ));
958
988
}
959
989
/* 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
960
998
err = ldap_bind_s (conn -> ld , who , password , LDAP_AUTH_SIMPLE );
999
+ #endif
961
1000
if (err != LDAP_SUCCESS )
962
1001
return faildirect (L , ldap_err2string (err ));
963
1002
@@ -985,13 +1024,15 @@ static void set_info (lua_State *L) {
985
1024
** Create ldap table and register the open method.
986
1025
*/
987
1026
int luaopen_lualdap (lua_State * L ) {
988
- struct luaL_reg lualdap [] = {
1027
+ struct luaL_Reg lualdap [] = {
989
1028
{"open_simple" , lualdap_open_simple },
990
1029
{NULL , NULL },
991
1030
};
992
1031
993
1032
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 );
995
1036
set_info (L );
996
1037
997
1038
return 1 ;
0 commit comments