diff options
-rw-r--r-- | source4/scripting/ejs/mprutil.c | 7 | ||||
-rw-r--r-- | source4/scripting/ejs/smbcalls_ldb.c | 78 | ||||
-rw-r--r-- | source4/scripting/libjs/provision.js | 6 | ||||
-rw-r--r-- | swat/esptest/ldb.esp | 9 | ||||
-rw-r--r-- | testprogs/ejs/ldb.js | 70 |
5 files changed, 109 insertions, 61 deletions
diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c index 247cea0b06..f25064f245 100644 --- a/source4/scripting/ejs/mprutil.c +++ b/source4/scripting/ejs/mprutil.c @@ -202,6 +202,9 @@ struct MprVar mprLdbArray(struct ldb_message **msg, int count, const char *name) for (i=0;i<count;i++) { mprAddArray(&res, i, mprLdbMessage(msg[i])); } + if (i==0) { + mprSetVar(&res, "length", mprCreateIntegerVar(0)); + } return res; } @@ -372,7 +375,7 @@ void mpr_ReturnString(int eid, const char *s) */ void mprSetCFunction(struct MprVar *obj, const char *name, MprCFunction fn) { - mprSetVar(obj, name, mprCreateCFunctionVar(fn, NULL, MPR_VAR_SCRIPT_HANDLE)); + mprSetVar(obj, name, mprCreateCFunctionVar(fn, obj, MPR_VAR_SCRIPT_HANDLE)); } /* @@ -380,5 +383,5 @@ void mpr_ReturnString(int eid, const char *s) */ void mprSetStringCFunction(struct MprVar *obj, const char *name, MprStringCFunction fn) { - mprSetVar(obj, name, mprCreateStringCFunctionVar(fn, NULL, MPR_VAR_SCRIPT_HANDLE)); + mprSetVar(obj, name, mprCreateStringCFunctionVar(fn, obj, MPR_VAR_SCRIPT_HANDLE)); } diff --git a/source4/scripting/ejs/smbcalls_ldb.c b/source4/scripting/ejs/smbcalls_ldb.c index be54ac79ac..924a9665b3 100644 --- a/source4/scripting/ejs/smbcalls_ldb.c +++ b/source4/scripting/ejs/smbcalls_ldb.c @@ -28,9 +28,10 @@ /* get the connected db */ -static struct ldb_context *ejs_ldb_db(int eid, struct MprVar *v) +static struct ldb_context *ejs_ldb_db(int eid) { - struct ldb_context *ldb = mprGetPtr(v, "ldb"); + struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0); + struct ldb_context *ldb = mprGetPtr(this, "db"); if (ldb == NULL) { ejsSetErrorMsg(eid, "invalid ldb connection"); } @@ -41,9 +42,9 @@ static struct ldb_context *ejs_ldb_db(int eid, struct MprVar *v) perform an ldb search, returning an array of results syntax: - res = ldb.search(db, "expression"); + res = ldb.search("expression"); var attrs = new Array("attr1", "attr2", "attr3"); - ldb.search(db, "expression", attrs); + ldb.search("expression", attrs); */ static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv) { @@ -55,27 +56,27 @@ static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv) struct ldb_message **res; /* validate arguments */ - if (argc < 2 || argc > 3) { + if (argc < 1 || argc > 2) { ejsSetErrorMsg(eid, "ldb.search invalid arguments"); goto failed; } - if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) { + if (argc == 2 && argv[1]->type != MPR_TYPE_OBJECT) { ejsSetErrorMsg(eid, "ldb.search attributes must be an object"); goto failed; } - ldb = ejs_ldb_db(eid, argv[0]); + ldb = ejs_ldb_db(eid); if (ldb == NULL) { return -1; } - expression = mprToString(argv[1]); + expression = mprToString(argv[0]); if (expression == NULL) { ejsSetErrorMsg(eid, "ldb.search invalid arguments"); goto failed; } - if (argc > 2) { - attrs = mprToList(tmp_ctx, argv[2]); + if (argc == 2) { + attrs = mprToList(tmp_ctx, argv[1]); } ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res); @@ -106,18 +107,18 @@ static int ejs_ldbAddModify(MprVarHandle eid, int argc, struct MprVar **argv, struct ldb_ldif *ldif; int ret; - if (argc != 2) { + if (argc != 1) { ejsSetErrorMsg(eid, "ldb.add/modify invalid arguments"); return -1; } - ldifstring = mprToString(argv[1]); + ldifstring = mprToString(argv[0]); if (ldifstring == NULL) { ejsSetErrorMsg(eid, "ldb.add/modify invalid arguments"); return -1; } - ldb = ejs_ldb_db(eid, argv[0]); + ldb = ejs_ldb_db(eid); if (ldb == NULL) { return -1; } @@ -136,7 +137,7 @@ static int ejs_ldbAddModify(MprVarHandle eid, int argc, struct MprVar **argv, /* perform an ldb delete usage: - ok = ldb.delete(db, dn); + ok = ldb.delete(dn); */ static int ejs_ldbDelete(MprVarHandle eid, int argc, struct MprVar **argv) { @@ -144,14 +145,14 @@ static int ejs_ldbDelete(MprVarHandle eid, int argc, struct MprVar **argv) struct ldb_context *ldb; int ret; - if (argc != 2) { + if (argc != 1) { ejsSetErrorMsg(eid, "ldb.delete invalid arguments"); return -1; } - dn = mprToString(argv[1]); + dn = mprToString(argv[0]); - ldb = ejs_ldb_db(eid, argv[0]); + ldb = ejs_ldb_db(eid); if (ldb == NULL) { return -1; } @@ -164,7 +165,7 @@ static int ejs_ldbDelete(MprVarHandle eid, int argc, struct MprVar **argv) /* perform an ldb rename usage: - ok = ldb.rename(db, dn1, dn2); + ok = ldb.rename(dn1, dn2); */ static int ejs_ldbRename(MprVarHandle eid, int argc, struct MprVar **argv) { @@ -172,19 +173,19 @@ static int ejs_ldbRename(MprVarHandle eid, int argc, struct MprVar **argv) struct ldb_context *ldb; int ret; - if (argc != 3) { + if (argc != 2) { ejsSetErrorMsg(eid, "ldb.rename invalid arguments"); return -1; } - dn1 = mprToString(argv[1]); - dn2 = mprToString(argv[2]); + dn1 = mprToString(argv[0]); + dn2 = mprToString(argv[1]); if (dn1 == NULL || dn2 == NULL) { ejsSetErrorMsg(eid, "ldb.rename invalid arguments"); return -1; } - ldb = ejs_ldb_db(eid, argv[0]); + ldb = ejs_ldb_db(eid); if (ldb == NULL) { return -1; } @@ -199,7 +200,7 @@ static int ejs_ldbRename(MprVarHandle eid, int argc, struct MprVar **argv) perform an ldb modify syntax: - ok = ldb.modify(db, ldifstring); + ok = ldb.modify(ldifstring); */ static int ejs_ldbAdd(MprVarHandle eid, int argc, struct MprVar **argv) { @@ -210,7 +211,7 @@ static int ejs_ldbAdd(MprVarHandle eid, int argc, struct MprVar **argv) perform an ldb add syntax: - ok = ldb.add(db, ldifstring); + ok = ldb.add(ldifstring); */ static int ejs_ldbModify(MprVarHandle eid, int argc, struct MprVar **argv) { @@ -220,13 +221,13 @@ static int ejs_ldbModify(MprVarHandle eid, int argc, struct MprVar **argv) /* connect to a database usage: - db = ldb.connect(dbfile); + ok = ldb.connect(dbfile); */ static int ejs_ldbConnect(MprVarHandle eid, int argc, char **argv) { struct ldb_context *ldb; const char *dbfile; - struct MprVar v; + struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0); if (argc != 1) { ejsSetErrorMsg(eid, "ldb.connect invalid arguments"); @@ -238,13 +239,10 @@ static int ejs_ldbConnect(MprVarHandle eid, int argc, char **argv) ldb = ldb_wrap_connect(mprMemCtx(), dbfile, 0, NULL); if (ldb == NULL) { ejsSetErrorMsg(eid, "ldb.connect failed to open %s", dbfile); - mpr_Return(eid, mprCreateUndefinedVar()); } - v = mprObject("db"); - mprSetPtrChild(&v, "ldb", ldb); - - mpr_Return(eid, v); + mprSetPtrChild(this, "db", ldb); + mpr_Return(eid, mprCreateBoolVar(ldb != NULL)); return 0; } @@ -254,16 +252,18 @@ static int ejs_ldbConnect(MprVarHandle eid, int argc, char **argv) */ static int ejs_ldb_init(MprVarHandle eid, int argc, struct MprVar **argv) { - struct MprVar ldb = mprObject("ldb"); + struct MprVar *ldb; + mpr_Return(eid, mprObject("ldb")); + + ldb = ejsGetReturnValue(eid); - mprSetStringCFunction(&ldb, "connect", ejs_ldbConnect); - mprSetCFunction(&ldb, "search", ejs_ldbSearch); - mprSetCFunction(&ldb, "add", ejs_ldbAdd); - mprSetCFunction(&ldb, "modify", ejs_ldbModify); - mprSetCFunction(&ldb, "delete", ejs_ldbDelete); - mprSetCFunction(&ldb, "rename", ejs_ldbRename); + mprSetStringCFunction(ldb, "connect", ejs_ldbConnect); + mprSetCFunction(ldb, "search", ejs_ldbSearch); + mprSetCFunction(ldb, "add", ejs_ldbAdd); + mprSetCFunction(ldb, "modify", ejs_ldbModify); + mprSetCFunction(ldb, "delete", ejs_ldbDelete); + mprSetCFunction(ldb, "rename", ejs_ldbRename); - mpr_Return(eid, ldb); return 0; } diff --git a/source4/scripting/libjs/provision.js b/source4/scripting/libjs/provision.js index d4551945d1..bf39bcb992 100644 --- a/source4/scripting/libjs/provision.js +++ b/source4/scripting/libjs/provision.js @@ -131,10 +131,10 @@ function setup_ldb(ldif, dbname, subobj) data = data + extra; data = substitute_var(data, subobj); - var db = ldb.connect(dbfile); - assert(db != undefined); + var ok = ldb.connect(dbfile); + assert(ok); - ok = ldb.add(db, data); + ok = ldb.add(data); assert(ok); } diff --git a/swat/esptest/ldb.esp b/swat/esptest/ldb.esp index 38a0422c50..18427dfecd 100644 --- a/swat/esptest/ldb.esp +++ b/swat/esptest/ldb.esp @@ -8,23 +8,24 @@ var dbfile = lpGet("sam database"); var attrs = new Array("name", "dnsDomain", "objectSid", "dn"); var ldb = ldb_init(); -var db = ldb.connect(dbfile); +var ok = ldb.connect(dbfile); +assert(ok); -res = ldb.search(db, "(objectClass=domain)", attrs); +res = ldb.search("(objectClass=domain)", attrs); multi_table(res, "dn"); %> <h3>An unconstrained search</h3> <% -res = ldb.search(db, "(objectClass=user)"); +res = ldb.search("(objectClass=user)"); multi_table(res, "dn"); %> <h3>A search with bad arguments</h3> <% -res = ldb.search("blah", "foo"); +res = ldb.search("=blah", "foo"); %> all done diff --git a/testprogs/ejs/ldb.js b/testprogs/ejs/ldb.js index f27f49c7a4..dee33774b3 100644 --- a/testprogs/ejs/ldb.js +++ b/testprogs/ejs/ldb.js @@ -1,26 +1,70 @@ +#!/bin/sh +exec smbscript "$0" ${1+"$@"} /* demonstrate access to ldb databases from ejs */ -println("Trying a attribute constrained search on samdb"); -var dbfile = lpGet("sam database"); -var attrs = new Array("name", "dnsDomain", "objectSid", "dn"); var ldb = ldb_init(); -res = ldb.search(dbfile, "(objectClass=domain)", attrs); +function basic_tests(ldb) +{ + println("Running basic tests"); + ok = ldb.add(" +dn: cn=x,cn=test +objectClass: foo +x: 3 +"); + assert(ok); -printVars(res); + println("Testing ldb.search"); + var res = ldb.search("(objectClass=*)"); + assert(res[0].objectClass[0] == "foo"); + assert(res[0].dn == "cn=x,cn=test"); + assert(res[0].x == 3); -println("and now an unconstrained search"); + ok = ldb.add(" +dn: cn=x2,cn=test +objectClass: foo +x: 4 +"); + assert(ok); + var attrs = new Array("x"); + res = ldb.search("x=4", attrs); + assert(res[0].x == 4); + assert(res[0].objectClass == undefined); + assert(res[0].dn == "cn=x2,cn=test"); -var dbfile = lpGet("sam database"); -var db = ldb.connect(dbfile); -res = ldb.search(db, "(objectClass=user)"); -printVars(res); + ok = ldb.delete("cn=x,cn=test"); + assert(ok); + + ok = ldb.rename("cn=x2,cn=test", "cn=x3,cn=test"); + assert(ok); + res = ldb.search("x=4", attrs); + assert(res[0].dn == "cn=x3,cn=test"); + + ok = ldb.modify(" +dn: cn=x3,cn=test +changetype: modify +add: x +x: 7 +"); + + res = ldb.search("x=7"); + assert(res.length == 1); + assert(res[0].x.length == 2); + +} + +var sys = sys_init(); +var dbfile = "test.ldb"; +sys.unlink(dbfile); +var ok = ldb.connect("tdb://" + dbfile); +assert(ok); + +basic_tests(ldb); + +sys.unlink(dbfile); -println("and a bad search"); -res = ldb.search(db, "foo"); -println("all done"); |