From 76b50ffff9a28d8815f91debe9ca8a9d7a5de743 Mon Sep 17 00:00:00 2001 From: Leonardo Godinho Date: Tue, 13 Mar 2007 22:07:33 +0000 Subject: Added support to ADSI (WinLDAP). Thanks to Mark Edgar for most of the hackish defines. --- lualdap/src/lualdap.c | 61 ++++++++++++++++++++++++++++++---------------- lualdap/src/open2winldap.h | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 21 deletions(-) create mode 100755 lualdap/src/open2winldap.h (limited to 'lualdap/src') diff --git a/lualdap/src/lualdap.c b/lualdap/src/lualdap.c index dee1c34..87da68e 100755 --- a/lualdap/src/lualdap.c +++ b/lualdap/src/lualdap.c @@ -1,18 +1,23 @@ /* ** LuaLDAP ** See Copyright Notice in license.html -** $Id: lualdap.c,v 1.46 2007-02-07 15:05:48 godinho Exp $ +** $Id: lualdap.c,v 1.47 2007-03-13 22:07:33 godinho Exp $ */ #include #include + #ifdef WIN32 #include #else #include #endif +#ifdef WINLDAP +#include "open2winldap.h" +#else #include "ldap.h" +#endif #include "lua.h" #include "lauxlib.h" @@ -20,6 +25,15 @@ #include "compat-5.1.h" #endif +#ifdef WINLDAPAPI +#define timeval l_timeval +typedef ULONG ldap_int_t; +typedef PCHAR ldap_pchar_t; +#else +typedef int ldap_int_t; +typedef const char * ldap_pchar_t; +#endif + #define LUALDAP_PREFIX "LuaLDAP: " #define LUALDAP_TABLENAME "lualdap" #define LUALDAP_CONNECTION_METATABLE "LuaLDAP connection" @@ -426,7 +440,7 @@ static int result_message (lua_State *L) { /* ** Push a function to process the LDAP result. */ -static int create_future (lua_State *L, int rc, int conn, int msgid, int code) { +static int create_future (lua_State *L, ldap_int_t rc, int conn, ldap_int_t msgid, int code) { if (rc != LDAP_SUCCESS) return faildirect (L, ldap_err2string (rc)); lua_pushvalue (L, conn); /* push connection as #1 upvalue */ @@ -463,9 +477,9 @@ static int lualdap_close (lua_State *L) { */ static int lualdap_add (lua_State *L) { conn_data *conn = getconnection (L); - const char *dn = luaL_checkstring (L, 2); + ldap_pchar_t dn = (ldap_pchar_t) luaL_checkstring (L, 2); attrs_data attrs; - int rc, msgid; + ldap_int_t rc, msgid; A_init (&attrs); if (lua_istable (L, 3)) A_tab2mod (L, &attrs, 3, LUALDAP_MOD_ADD); @@ -485,10 +499,10 @@ static int lualdap_add (lua_State *L) { */ static int lualdap_compare (lua_State *L) { conn_data *conn = getconnection (L); - const char *dn = luaL_checkstring (L, 2); - const char *attr = luaL_checkstring (L, 3); + ldap_pchar_t dn = (ldap_pchar_t) luaL_checkstring (L, 2); + ldap_pchar_t attr = (ldap_pchar_t) luaL_checkstring (L, 3); BerValue bvalue; - int rc, msgid; + ldap_int_t rc, msgid; bvalue.bv_val = (char *)luaL_checkstring (L, 4); bvalue.bv_len = lua_strlen (L, 4); rc = ldap_compare_ext (conn->ld, dn, attr, &bvalue, NULL, NULL, &msgid); @@ -504,8 +518,8 @@ static int lualdap_compare (lua_State *L) { */ static int lualdap_delete (lua_State *L) { conn_data *conn = getconnection (L); - const char *dn = luaL_checkstring (L, 2); - int rc, msgid; + ldap_pchar_t dn = (ldap_pchar_t) luaL_checkstring (L, 2); + ldap_int_t rc, msgid; rc = ldap_delete_ext (conn->ld, dn, NULL, NULL, &msgid); return create_future (L, rc, 1, msgid, LDAP_RES_DELETE); } @@ -539,9 +553,10 @@ static int op2code (const char *s) { */ static int lualdap_modify (lua_State *L) { conn_data *conn = getconnection (L); - const char *dn = luaL_checkstring (L, 2); + ldap_pchar_t dn = (ldap_pchar_t) luaL_checkstring (L, 2); attrs_data attrs; - int rc, msgid, param = 3; + ldap_int_t rc, msgid; + int param = 3; A_init (&attrs); while (lua_istable (L, param)) { int op; @@ -565,12 +580,12 @@ static int lualdap_modify (lua_State *L) { */ static int lualdap_rename (lua_State *L) { conn_data *conn = getconnection (L); - const char *dn = luaL_checkstring (L, 2); - const char *rdn = luaL_checkstring (L, 3); - const char *par = luaL_optlstring (L, 4, NULL, NULL); + ldap_pchar_t dn = (ldap_pchar_t) luaL_checkstring (L, 2); + ldap_pchar_t rdn = (ldap_pchar_t) luaL_checkstring (L, 3); + ldap_pchar_t par = (ldap_pchar_t) luaL_optlstring (L, 4, NULL, NULL); const int del = luaL_optnumber (L, 5, 0); - int msgid; - int rc = ldap_rename (conn->ld, dn, rdn, par, del, NULL, NULL, &msgid); + ldap_int_t msgid; + ldap_int_t rc = ldap_rename (conn->ld, dn, rdn, par, del, NULL, NULL, &msgid); return create_future (L, rc, 1, msgid, LDAP_RES_MODDN); } @@ -679,6 +694,8 @@ static int next_message (lua_State *L) { ret = 2; /* two return values */ break; } +/*No reference to LDAP_RES_SEARCH_REFERENCE on MSDN. Maybe there is a replacement to it?*/ +#ifdef LDAP_RES_SEARCH_REFERENCE case LDAP_RES_SEARCH_REFERENCE: { LDAPMessage *ref = ldap_first_reference (conn->ld, msg); push_dn (L, conn->ld, ref); /* is this supposed to work? */ @@ -686,6 +703,7 @@ static int next_message (lua_State *L) { ret = 2; /* two return values */ break; } +#endif case LDAP_RES_SEARCH_RESULT: /* close search object to avoid reuse */ search_close (L, search); @@ -788,7 +806,8 @@ static struct timeval *get_timeout_param (lua_State *L, struct timeval *st) { */ static int lualdap_search (lua_State *L) { conn_data *conn = getconnection (L); - const char *base, *filter; + ldap_pchar_t base; + ldap_pchar_t filter; char *attrs[LUALDAP_MAX_ATTRS]; int scope, attrsonly, msgid, rc, sizelimit; struct timeval st, *timeout; @@ -799,8 +818,8 @@ static int lualdap_search (lua_State *L) { return 2; /* get other parameters */ attrsonly = booltabparam (L, "attrsonly", 0); - base = strtabparam (L, "base", NULL); - filter = strtabparam (L, "filter", NULL); + base = (ldap_pchar_t) strtabparam (L, "base", NULL); + filter = (ldap_pchar_t) strtabparam (L, "filter", NULL); scope = string2scope (L, strtabparam (L, "scope", NULL)); sizelimit = longtabparam (L, "sizelimit", LDAP_NO_LIMIT); timeout = get_timeout_param (L, &st); @@ -915,8 +934,8 @@ static int lualdap_createmeta (lua_State *L) { ** @return #1 Userdata with connection structure. */ static int lualdap_open_simple (lua_State *L) { - const char *host = luaL_checkstring (L, 1); - const char *who = luaL_optstring (L, 2, NULL); + ldap_pchar_t host = (ldap_pchar_t) luaL_checkstring (L, 1); + ldap_pchar_t who = (ldap_pchar_t) luaL_optstring (L, 2, NULL); 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)); diff --git a/lualdap/src/open2winldap.h b/lualdap/src/open2winldap.h new file mode 100755 index 0000000..ad592d8 --- /dev/null +++ b/lualdap/src/open2winldap.h @@ -0,0 +1,47 @@ +#include + +/* For some reason MSDN mentions LDAP_RES_MODDN, but not LDAP_RES_MODRDN. */ +#ifndef LDAP_RES_MODDN +#define LDAP_RES_MODDN LDAP_RES_MODRDN +#endif + +/* MSDN doesn't mention LDAP_OPT_SUCCESS, it uses LDAP_SUCCESS intead */ +#ifndef LDAP_OPT_SUCCESS +#define LDAP_OPT_SUCCESS LDAP_SUCCESS +#endif + +/* MSDN doesn't mention LDAP_SCOPE_DEFAULT, so default will be LDAP_SCOPE_SUBTREE */ +#ifndef LDAP_SCOPE_DEFAULT +#define LDAP_SCOPE_DEFAULT LDAP_SCOPE_SUBTREE +#endif + +/* MSDN doesn't mention this function at all. Unfortunately, LDAPMessage an opaque type. */ +#define ldap_msgtype(m) ((m)->lm_msgtype) + +#define ldap_first_message ldap_first_entry + +/* The WinLDAP API allows comparisons against either string or binary values */ +#undef ldap_compare_ext + +/* The WinLDAP API uses ULONG seconds instead of a struct timeval. */ +#undef ldap_search_ext + +/* The WinLDAP API has a different number of arguments for this */ +#undef ldap_start_tls_s + +#ifdef UNICODE +#define ldap_compare_ext(ld,dn,a,v,sc,cc,msg) \ + ldap_compare_extW(ld,dn,a,0,v,sc,cc,msg) +#define ldap_search_ext(ld,base,scope,f,a,o,sc,cc,t,s,msg) \ + ldap_search_extW(ld,base,scope,f,a,o,sc,cc,(t)?(t)->tv_sec:0,s,msg) +#define ldap_start_tls_s(ld,sc,cc) \ + ldap_start_tls_sW(ld,0,0,sc,cc) +#else +#define ldap_compare_ext(ld,dn,a,v,sc,cc,msg) \ + ldap_compare_extA(ld,dn,a,0,v,sc,cc,msg) +#define ldap_search_ext(ld,base,scope,f,a,o,sc,cc,t,s,msg) \ + ldap_search_extA(ld,base,scope,f,a,o,sc,cc,(t)?(t)->tv_sec:0,s,msg) +#define ldap_start_tls_s(ld,sc,cc) \ + ldap_start_tls_sA(ld,0,0,sc,cc) +#endif + -- cgit v1.2.3-59-g8ed1b