From f52a74021512aace67f7ecba33ed130102f47533 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 29 May 2005 00:13:10 +0000 Subject: r7061: A ejs scripting client. This should allow javascript to be run in a command line environment instead of inside the web server. It doesn't work yet though, rather an exception is thrown when trying to call ejsDefineStringCFunction(). (This used to be commit 3444cd5429dfef5a67d5bf7818ae08e4e8cc5ccc) --- source4/scripting/ejs/smbscript.c | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 source4/scripting/ejs/smbscript.c (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c new file mode 100644 index 0000000000..f1c3f0b46d --- /dev/null +++ b/source4/scripting/ejs/smbscript.c @@ -0,0 +1,62 @@ +/* + Unix SMB/CIFS implementation. + + Standalone client for ESP scripting. + + Copyright (C) Tim Potter 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "web_server/ejs/ejs.h" + +void http_exception(const char *reason) +{ + fprintf(stderr, "smbscript exception: %s", reason); + exit(1); +} + +extern void ejsDefineStringCFunction(EjsId eid, const char *functionName, + MprStringCFunction fn, void *thisPtr, int flags); + +static int writeProc(MprVarHandle userHandle, int argc, char **argv) +{ + int i; + + mprAssert(argv); + for (i = 0; i < argc; i++) { + printf("%s", argv[i]); + } + return 0; +} + + int main(int argc, const char *argv[]) +{ + EjsId eid; + EjsHandle primary, alternate; + MprVar result; + char *emsg; + + ejsOpen(0, 0, 0); + eid = ejsOpenEngine(primary, alternate); + ejsDefineStringCFunction(eid, "write", writeProc, NULL, 0); + ejsEvalScript(0, "write(\"hello\n\");", &result, &emsg); + ejsClose(); + + printf("emsg = %s\n", emsg); + + return 0; +} -- cgit From e95c8f19116813dd03cce957c3367254782915fd Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 29 May 2005 03:25:21 +0000 Subject: r7063: Do error checking on the ejs functions. Tridge says there is a bug in defining per-engine CFunction's so move calls to ejsDefineStringCFunction() above the ejsOpenEngine() call. Test script now works! (This used to be commit 5e2458ae6c863ff29b85fff3d093f7f4fa9dc2be) --- source4/scripting/ejs/smbscript.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index f1c3f0b46d..85064d3b44 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -29,9 +29,6 @@ void http_exception(const char *reason) exit(1); } -extern void ejsDefineStringCFunction(EjsId eid, const char *functionName, - MprStringCFunction fn, void *thisPtr, int flags); - static int writeProc(MprVarHandle userHandle, int argc, char **argv) { int i; @@ -50,13 +47,26 @@ static int writeProc(MprVarHandle userHandle, int argc, char **argv) MprVar result; char *emsg; - ejsOpen(0, 0, 0); - eid = ejsOpenEngine(primary, alternate); - ejsDefineStringCFunction(eid, "write", writeProc, NULL, 0); - ejsEvalScript(0, "write(\"hello\n\");", &result, &emsg); - ejsClose(); + if (ejsOpen(0, 0, 0) != 0) { + fprintf(stderr, "smbscript: ejsOpen(): unable to initialise " + "EJ subsystem\n"); + exit(1); + } - printf("emsg = %s\n", emsg); + ejsDefineStringCFunction(-1, "write", writeProc, NULL, 0); + + if ((eid = ejsOpenEngine(primary, alternate)) == (EjsId)-1) { + fprintf(stderr, "smbscript: ejsOpenEngine(): unable to " + "initialise an EJS engine\n"); + exit(1); + } + + if (ejsEvalScript(0, "write(\"hello\n\");", &result, &emsg) == -1) { + fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg); + exit(1); + } + + ejsClose(); return 0; } -- cgit From de850cb754f8ea49acc80e4303a9835578768154 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 29 May 2005 03:29:10 +0000 Subject: r7064: Clean up handle parameter passing after peeking at tridge's ejstest.c (This used to be commit 805b6c7cf0f1e05fbb690bdfc93938747e13e6cd) --- source4/scripting/ejs/smbscript.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 85064d3b44..1c823a4d8e 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -43,11 +43,11 @@ static int writeProc(MprVarHandle userHandle, int argc, char **argv) int main(int argc, const char *argv[]) { EjsId eid; - EjsHandle primary, alternate; + EjsHandle handle; MprVar result; char *emsg; - if (ejsOpen(0, 0, 0) != 0) { + if (ejsOpen(NULL, NULL, NULL) != 0) { fprintf(stderr, "smbscript: ejsOpen(): unable to initialise " "EJ subsystem\n"); exit(1); @@ -55,13 +55,13 @@ static int writeProc(MprVarHandle userHandle, int argc, char **argv) ejsDefineStringCFunction(-1, "write", writeProc, NULL, 0); - if ((eid = ejsOpenEngine(primary, alternate)) == (EjsId)-1) { + if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) { fprintf(stderr, "smbscript: ejsOpenEngine(): unable to " "initialise an EJS engine\n"); exit(1); } - if (ejsEvalScript(0, "write(\"hello\n\");", &result, &emsg) == -1) { + if (ejsEvalScript(eid, "write(\"hello\n\");", &result, &emsg) == -1) { fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg); exit(1); } -- cgit From 14ade23914b328fcb5924488c2416dfa7748e4ab Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 29 May 2005 03:53:36 +0000 Subject: r7065: Move ejs from web_server to lib so it can be shared with smbscript. (This used to be commit b83dc8fbfb9ffe30654bc4869398f50dd9ccccb7) --- source4/scripting/ejs/smbscript.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 1c823a4d8e..f1ff03ae49 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -21,7 +21,7 @@ */ #include "includes.h" -#include "web_server/ejs/ejs.h" +#include "lib/ejs/ejs.h" void http_exception(const char *reason) { -- cgit From 7756b990f38495bf98e335b56bdc510659c34bdb Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 29 May 2005 04:10:22 +0000 Subject: r7066: Rename http_exception to ejs_exception. (This used to be commit f2e59d3adfd7813c3c2090350f8ff2a99a5533e9) --- source4/scripting/ejs/smbscript.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index f1ff03ae49..0b0a446839 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -23,7 +23,7 @@ #include "includes.h" #include "lib/ejs/ejs.h" -void http_exception(const char *reason) +void ejs_exception(const char *reason) { fprintf(stderr, "smbscript exception: %s", reason); exit(1); @@ -61,7 +61,7 @@ static int writeProc(MprVarHandle userHandle, int argc, char **argv) exit(1); } - if (ejsEvalScript(eid, "write(\"hello\n\");", &result, &emsg) == -1) { + if (ejsEvalFile(eid, (char *)argv[1], &result, &emsg) == -1) { fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg); exit(1); } -- cgit From 21f3a3921c03777beb7e3072152ae33956b38807 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 29 May 2005 08:12:16 +0000 Subject: r7069: Add a little usage message to smbscript and fix a compiler warning. My compiler still complains about "handle" (scripting/ejs/smbscrip.c:46) possibly not being initialized and to me this looks true. Running smbscript with the trivial write("Hello, world\n"); also leaves some memory around. Volker (This used to be commit 06d27a19213dc8fe6dfc948a5e8cbafa74db7a29) --- source4/scripting/ejs/smbscript.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 0b0a446839..57f42688d6 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -47,6 +47,11 @@ static int writeProc(MprVarHandle userHandle, int argc, char **argv) MprVar result; char *emsg; + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + if (ejsOpen(NULL, NULL, NULL) != 0) { fprintf(stderr, "smbscript: ejsOpen(): unable to initialise " "EJ subsystem\n"); @@ -61,7 +66,8 @@ static int writeProc(MprVarHandle userHandle, int argc, char **argv) exit(1); } - if (ejsEvalFile(eid, (char *)argv[1], &result, &emsg) == -1) { + if (ejsEvalFile(eid, discard_const_p(char, argv[1]), &result, + &emsg) == -1) { fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg); exit(1); } -- cgit From 8754c793bfe79e87febb026e5915e054c23cfede Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 29 May 2005 11:35:56 +0000 Subject: r7072: moved the esp hooks calls to the ejs level, so we can call them from both esp scripts and ejs scripts. This allows the smbscript program to call all the existing extension calls like lpGet() and ldbSearch() Also fixed smbscript to load smb.conf, and setup logging for DEBUG() I left the unixAuth() routine in web_server/calls.c at the moment, as that is really only useful for esp scripts. I imagine that as we extend esp/ejs, we will put some functions in scripting/ejs/ for use in both ejs and esp, and some functions in web_server/ where they will only be accessed by esp web scripts (This used to be commit e59ae64f60d388a5634559e4e0887e4676b70871) --- source4/scripting/ejs/mprutil.c | 159 +++++++++++++++++++++++ source4/scripting/ejs/smbcalls.c | 259 ++++++++++++++++++++++++++++++++++++++ source4/scripting/ejs/smbscript.c | 27 ++-- 3 files changed, 433 insertions(+), 12 deletions(-) create mode 100644 source4/scripting/ejs/mprutil.c create mode 100644 source4/scripting/ejs/smbcalls.c (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c new file mode 100644 index 0000000000..a1e26ecb2c --- /dev/null +++ b/source4/scripting/ejs/mprutil.c @@ -0,0 +1,159 @@ +/* + Unix SMB/CIFS implementation. + + utility functions for manipulating mpr variables in ejs calls + + Copyright (C) Andrew Tridgell 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "lib/ejs/ejs.h" +#include "lib/ldb/include/ldb.h" + +/* + add an indexed array element to a property +*/ +static void mprAddArray(struct MprVar *var, int i, struct MprVar v) +{ + char idx[16]; + mprItoa(i, idx, sizeof(idx)); + mprCreateProperty(var, idx, &v); +} + +/* + construct a MprVar from a list +*/ +struct MprVar mprList(const char *name, const char **list) +{ + struct MprVar var; + int i; + + var = mprCreateObjVar(name, MPR_DEFAULT_HASH_SIZE); + for (i=0;list && list[i];i++) { + mprAddArray(&var, i, mprCreateStringVar(list[i], 1)); + } + return var; +} + +/* + construct a string MprVar from a lump of data +*/ +struct MprVar mprData(const uint8_t *p, size_t length) +{ + struct MprVar var; + char *s = talloc_strndup(mprMemCtx(), p, length); + if (s == NULL) { + return mprCreateUndefinedVar(); + } + var = mprCreateStringVar(s, 1); + talloc_free(s); + return var; +} + +/* + turn a ldb_message into a ejs object variable +*/ +struct MprVar mprLdbMessage(struct ldb_message *msg) +{ + struct MprVar var; + int i; + /* we force some attributes to always be an array in the + returned structure. This makes the scripting easier, as you don't + need a special case for the single value case */ + const char *multivalued[] = { "objectClass", "memberOf", "privilege", + "member", NULL }; + struct MprVar val; + + var = mprCreateObjVar(msg->dn, MPR_DEFAULT_HASH_SIZE); + + for (i=0;inum_elements;i++) { + struct ldb_message_element *el = &msg->elements[i]; + if (el->num_values == 1 && + !str_list_check_ci(multivalued, el->name)) { + val = mprData(el->values[0].data, el->values[0].length); + } else { + int j; + val = mprCreateObjVar(el->name, MPR_DEFAULT_HASH_SIZE); + for (j=0;jnum_values;j++) { + mprAddArray(&val, j, + mprData(el->values[j].data, + el->values[j].length)); + } + } + mprCreateProperty(&var, el->name, &val); + } + + /* add the dn if it is not already specified */ + if (mprGetProperty(&var, "dn", 0) == 0) { + val = mprCreateStringVar(msg->dn, 1); + mprCreateProperty(&var, "dn", &val); + } + + return var; +} + + +/* + turn an array of ldb_messages into a ejs object variable +*/ +struct MprVar mprLdbArray(struct ldb_message **msg, int count, const char *name) +{ + struct MprVar res; + int i; + + res = mprCreateObjVar(name?name:"(NULL)", MPR_DEFAULT_HASH_SIZE); + for (i=0;itype != MPR_TYPE_STRING) return NULL; + return v->string; +} + +/* + turn a MprVar object variable into a string list + this assumes the object variable consists only of strings +*/ +const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v) +{ + const char **list = NULL; + struct MprVar *el; + + if (v->type != MPR_TYPE_OBJECT || + v->properties == NULL) { + return NULL; + } + for (el=mprGetFirstProperty(v, MPR_ENUM_DATA); + el; + el=mprGetNextProperty(v, el, MPR_ENUM_DATA)) { + const char *s = mprToString(el); + if (s) { + list = str_list_add(list, s); + } + } + talloc_steal(mem_ctx, list); + return list; +} + diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c new file mode 100644 index 0000000000..664bb74df3 --- /dev/null +++ b/source4/scripting/ejs/smbcalls.c @@ -0,0 +1,259 @@ +/* + Unix SMB/CIFS implementation. + + provide hooks into smbd C calls from ejs scripts + + Copyright (C) Andrew Tridgell 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "lib/ejs/ejs.h" +#include "param/loadparm.h" +#include "lib/ldb/include/ldb.h" + +/* + return the type of a variable +*/ +static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv) +{ + const struct { + MprType type; + const char *name; + } types[] = { + { MPR_TYPE_UNDEFINED, "undefined" }, + { MPR_TYPE_NULL, "null" }, + { MPR_TYPE_BOOL, "boolean" }, + { MPR_TYPE_CFUNCTION, "function" }, + { MPR_TYPE_FLOAT, "float" }, + { MPR_TYPE_INT, "int" }, + { MPR_TYPE_INT64, "int64" }, + { MPR_TYPE_OBJECT, "object" }, + { MPR_TYPE_FUNCTION, "function" }, + { MPR_TYPE_STRING, "string" }, + { MPR_TYPE_STRING_CFUNCTION, "function" } + }; + int i; + const char *type = "unknown"; + + if (argc != 1) return -1; + + for (i=0;itype == types[i].type) { + type = types[i].name; + break; + } + } + + ejsSetReturnString(eid, type); + return 0; +} + +/* + setup a return of a string list +*/ +static void ejs_returnlist(MprVarHandle eid, + const char *name, const char **list) +{ + ejsSetReturnValue(eid, mprList(name, list)); +} + +/* + return a list of defined services +*/ +static int ejs_lpServices(MprVarHandle eid, int argc, char **argv) +{ + int i; + const char **list = NULL; + if (argc != 0) return -1; + + for (i=0;iclass == P_GLOBAL) { + return -1; + } + parm_ptr = lp_parm_ptr(snum, parm); + } else if (strchr(argv[0], ':')) { + /* its a global parametric option */ + const char *type = talloc_strndup(mprMemCtx(), + argv[0], strcspn(argv[0], ":")); + const char *option = strchr(argv[0], ':') + 1; + const char *value; + if (type == NULL || option == NULL) return -1; + value = lp_get_parametric(-1, type, option); + if (value == NULL) return -1; + ejsSetReturnString(eid, value); + return 0; + } else { + /* its a global parameter */ + parm = lp_parm_struct(argv[0]); + if (parm == NULL) return -1; + parm_ptr = parm->ptr; + } + + if (parm == NULL || parm_ptr == NULL) { + return -1; + } + + /* construct and return the right type of ejs object */ + switch (parm->type) { + case P_STRING: + case P_USTRING: + ejsSetReturnString(eid, *(char **)parm_ptr); + break; + case P_BOOL: + ejsSetReturnValue(eid, mprCreateBoolVar(*(BOOL *)parm_ptr)); + break; + case P_INTEGER: + ejsSetReturnValue(eid, mprCreateIntegerVar(*(int *)parm_ptr)); + break; + case P_ENUM: + for (i=0; parm->enum_list[i].name; i++) { + if (*(int *)parm_ptr == parm->enum_list[i].value) { + ejsSetReturnString(eid, parm->enum_list[i].name); + return 0; + } + } + return -1; + case P_LIST: + ejs_returnlist(eid, parm->label, *(const char ***)parm_ptr); + break; + case P_SEP: + return -1; + } + return 0; +} + + +/* + perform an ldb search, returning an array of results + + syntax: + ldbSearch("dbfile", "expression"); + var attrs = new Array("attr1", "attr2", "attr3"); + ldbSearch("dbfile", "expression", attrs); +*/ +static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv) +{ + const char **attrs = NULL; + const char *expression, *dbfile; + TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); + struct ldb_context *ldb; + int ret; + struct ldb_message **res; + + /* validate arguments */ + if (argc < 2 || argc > 3 || + argv[0]->type != MPR_TYPE_STRING) { + ejsSetErrorMsg(eid, "ldbSearch invalid arguments"); + goto failed; + } + if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) { + ejsSetErrorMsg(eid, "ldbSearch attributes must be an object"); + goto failed; + } + + dbfile = mprToString(argv[0]); + expression = mprToString(argv[1]); + if (argc > 2) { + attrs = mprToList(tmp_ctx, argv[2]); + } + if (dbfile == NULL || expression == NULL) { + ejsSetErrorMsg(eid, "ldbSearch invalid arguments"); + goto failed; + } + + ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL); + if (ldb == NULL) { + ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile); + goto failed; + } + + ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res); + if (ret == -1) { + ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb)); + goto failed; + } + + ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message")); + + talloc_free(tmp_ctx); + return 0; + +failed: + talloc_free(tmp_ctx); + return -1; +} + + +/* + setup the C functions that be called from ejs +*/ +void smb_setup_ejs_functions(void) +{ + ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, 0); + ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, 0); + ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, 0); + ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, 0); +} diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 57f42688d6..593d3b0cf5 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -21,6 +21,7 @@ */ #include "includes.h" +#include "dynconfig.h" #include "lib/ejs/ejs.h" void ejs_exception(const char *reason) @@ -29,36 +30,36 @@ void ejs_exception(const char *reason) exit(1); } -static int writeProc(MprVarHandle userHandle, int argc, char **argv) -{ - int i; - - mprAssert(argv); - for (i = 0; i < argc; i++) { - printf("%s", argv[i]); - } - return 0; -} - int main(int argc, const char *argv[]) { EjsId eid; EjsHandle handle; MprVar result; char *emsg; + TALLOC_CTX *mem_ctx = talloc_new(NULL); if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } + setup_logging(argv[0],DEBUG_STDOUT); + + if (!lp_load(dyn_CONFIGFILE,True,False,False)) { + fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n", + argv[0], dyn_CONFIGFILE); + exit(1); + } + + mprSetCtx(mem_ctx); + if (ejsOpen(NULL, NULL, NULL) != 0) { fprintf(stderr, "smbscript: ejsOpen(): unable to initialise " "EJ subsystem\n"); exit(1); } - ejsDefineStringCFunction(-1, "write", writeProc, NULL, 0); + smb_setup_ejs_functions(); if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) { fprintf(stderr, "smbscript: ejsOpenEngine(): unable to " @@ -74,5 +75,7 @@ static int writeProc(MprVarHandle userHandle, int argc, char **argv) ejsClose(); + talloc_free(mem_ctx); + return 0; } -- cgit From 8f84f7cdecf12396eb5c1723e5e1ccf813cd9a51 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 29 May 2005 11:43:52 +0000 Subject: r7074: we should load all shares in smbscript (This used to be commit 92f85507df2bce5e246484860a43748321f2291e) --- source4/scripting/ejs/smbscript.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 593d3b0cf5..c6b1b7a192 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -45,7 +45,7 @@ void ejs_exception(const char *reason) setup_logging(argv[0],DEBUG_STDOUT); - if (!lp_load(dyn_CONFIGFILE,True,False,False)) { + if (!lp_load(dyn_CONFIGFILE, False, False, False)) { fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n", argv[0], dyn_CONFIGFILE); exit(1); -- cgit From f477a741293673adad784c55a749031a290bd072 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 29 May 2005 11:58:14 +0000 Subject: r7075: added support for ARGV[] in ejs scripts (This used to be commit 3db568eb6bb383c4c1e1fd0c7f043a9914dcc3cc) --- source4/scripting/ejs/smbscript.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index c6b1b7a192..3378885147 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -37,8 +37,11 @@ void ejs_exception(const char *reason) MprVar result; char *emsg; TALLOC_CTX *mem_ctx = talloc_new(NULL); + const char **argv_list = NULL; + struct MprVar v; + int i; - if (argc != 2) { + if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } @@ -67,6 +70,14 @@ void ejs_exception(const char *reason) exit(1); } + /* setup ARGV[] in the ejs environment */ + for (i=2;i Date: Sun, 29 May 2005 12:41:59 +0000 Subject: r7078: - fix an uninitialised variable in smbscript - fixed handle passing in the smb/ejs interface calls, so they can be called safely from esp (This used to be commit 45ea1b64413de577366939b2106f657f6c47b1bd) --- source4/scripting/ejs/smbcalls.c | 8 ++++---- source4/scripting/ejs/smbscript.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 664bb74df3..e436fc78df 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -252,8 +252,8 @@ failed: */ void smb_setup_ejs_functions(void) { - ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, 0); - ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, 0); - ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, 0); - ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, 0); + ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE); } diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 3378885147..021dafa4ae 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -33,7 +33,7 @@ void ejs_exception(const char *reason) int main(int argc, const char *argv[]) { EjsId eid; - EjsHandle handle; + EjsHandle handle = 0; MprVar result; char *emsg; TALLOC_CTX *mem_ctx = talloc_new(NULL); -- cgit From 0b2f972c8a5205f648ed747b04c5232c694afd30 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 29 May 2005 21:45:38 +0000 Subject: r7080: Fix typo in error message. (This used to be commit fcf177c86e2bfbc993352b80824487e5df0a3f63) --- source4/scripting/ejs/smbscript.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 021dafa4ae..a15fce57b0 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -80,7 +80,7 @@ void ejs_exception(const char *reason) /* run the script */ if (ejsEvalFile(eid, discard_const_p(char, argv[1]), &result, &emsg) == -1) { - fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg); + fprintf(stderr, "smbscript: ejsEvalFile(): %s\n", emsg); exit(1); } -- cgit From 18fa5ea9ef2d9a20d076790123c131d2dbdc9c22 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 29 May 2005 22:10:51 +0000 Subject: r7081: Add mprToInt() function. (This used to be commit 790a46f53bd5b6994cbf6aed670df1407a44e2f3) --- source4/scripting/ejs/mprutil.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c index a1e26ecb2c..3e47fdf9a3 100644 --- a/source4/scripting/ejs/mprutil.c +++ b/source4/scripting/ejs/mprutil.c @@ -132,6 +132,15 @@ const char *mprToString(const struct MprVar *v) return v->string; } +/* + turn a MprVar integer variable into an int + */ +int mprToInt(const struct MprVar *v) +{ + if (v->type != MPR_TYPE_INT) return 0; + return v->integer; +} + /* turn a MprVar object variable into a string list this assumes the object variable consists only of strings -- cgit From 520e2258c9949f9129a2e124035c0200c0c59b39 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 29 May 2005 22:11:32 +0000 Subject: r7082: Call load_interfaces() in smbscript initialisation. (This used to be commit 54051bf8bbb18653adafb37cc6181617ca60b781) --- source4/scripting/ejs/smbscript.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index a15fce57b0..754a49ccb1 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -54,6 +54,8 @@ void ejs_exception(const char *reason) exit(1); } + load_interfaces(); + mprSetCtx(mem_ctx); if (ejsOpen(NULL, NULL, NULL) != 0) { -- cgit From 4f1a5b716972bc2af916a528399c9dc0c2f18c35 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 29 May 2005 22:14:21 +0000 Subject: r7083: Add a ejs hook to the resolve_name() function. We need to figure out what the best way to return NTSTATUS codes. In the Python wrappers I threw an exception which could be caught by some code, but I'm not sure whether this is possible in ejs. (This used to be commit 6911e46c6a576a379ea06f9ba3ef6a62653170f0) --- source4/scripting/ejs/smbcalls.c | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index e436fc78df..02b40c46d8 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -24,6 +24,7 @@ #include "lib/ejs/ejs.h" #include "param/loadparm.h" #include "lib/ldb/include/ldb.h" +#include "librpc/gen_ndr/ndr_nbt.h" /* return the type of a variable @@ -246,6 +247,55 @@ failed: return -1; } +/* + look up a netbios name + + syntax: + resolveName("frogurt"); + resolveName("frogurt", 0x1c); +*/ + +static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv) +{ + struct nbt_name name; + TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); + NTSTATUS result; + const char *reply_addr; + + /* validate arguments */ + if (argc == 1) { + if (argv[0]->type != MPR_TYPE_STRING) { + ejsSetErrorMsg(eid, "resolveName invalid arguments"); + goto failed; + } + make_nbt_name_client(&name, mprToString(argv[0])); + } else if (argc == 2) { + if (argv[1]->type != MPR_TYPE_INT) { + ejsSetErrorMsg(eid, "resolveName invalid arguments"); + goto failed; + } + make_nbt_name(&name, mprToString(argv[0]), mprToInt(argv[1])); + } else { + ejsSetErrorMsg(eid, "resolveName invalid arguments"); + goto failed; + } + + result = resolve_name(&name, tmp_ctx, &reply_addr); + + if (!NT_STATUS_IS_OK(result)) { + ejsSetErrorMsg(eid, "resolveName name not found"); + goto failed; + } + + ejsSetReturnString(eid, reply_addr); + + talloc_free(tmp_ctx); + return 0; + + failed: + talloc_free(tmp_ctx); + return -1; +} /* setup the C functions that be called from ejs @@ -256,4 +306,5 @@ void smb_setup_ejs_functions(void) ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "resolveName", ejs_resolve_name, NULL, MPR_VAR_SCRIPT_HANDLE); } -- cgit From a05bd5e9d654b97e1a2128a5048b68b7efd9ccd7 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 31 May 2005 04:12:55 +0000 Subject: r7135: make typeof() complient with ECMA 11.4.3 (This used to be commit 29ab4cc6cb244255ab75af7ae2076b51209f4f2d) --- source4/scripting/ejs/smbcalls.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 02b40c46d8..fc2c16a456 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -35,20 +35,20 @@ static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv) MprType type; const char *name; } types[] = { - { MPR_TYPE_UNDEFINED, "undefined" }, - { MPR_TYPE_NULL, "null" }, - { MPR_TYPE_BOOL, "boolean" }, - { MPR_TYPE_CFUNCTION, "function" }, - { MPR_TYPE_FLOAT, "float" }, - { MPR_TYPE_INT, "int" }, - { MPR_TYPE_INT64, "int64" }, - { MPR_TYPE_OBJECT, "object" }, - { MPR_TYPE_FUNCTION, "function" }, - { MPR_TYPE_STRING, "string" }, + { MPR_TYPE_UNDEFINED, "undefined" }, + { MPR_TYPE_NULL, "object" }, + { MPR_TYPE_BOOL, "boolean" }, + { MPR_TYPE_CFUNCTION, "function" }, + { MPR_TYPE_FLOAT, "number" }, + { MPR_TYPE_INT, "number" }, + { MPR_TYPE_INT64, "number" }, + { MPR_TYPE_OBJECT, "object" }, + { MPR_TYPE_FUNCTION, "function" }, + { MPR_TYPE_STRING, "string" }, { MPR_TYPE_STRING_CFUNCTION, "function" } }; int i; - const char *type = "unknown"; + const char *type = NULL; if (argc != 1) return -1; @@ -58,6 +58,7 @@ static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv) break; } } + if (type == NULL) return -1; ejsSetReturnString(eid, type); return 0; -- cgit From afeaf137c4cbcbde1d6e4c9921bb769a1d486aff Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 3 Jun 2005 08:00:42 +0000 Subject: r7215: Convert smbscript to use ejsEvalScript() and file_load() instead of ejsEvalFile(). Still need to add advancement of the script past the hash-bang line but it's home time now!! (This used to be commit 14a2053c045a2df1d68838900c833c2a15cb5a36) --- source4/scripting/ejs/smbscript.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 754a49ccb1..323e604f8d 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -35,7 +35,8 @@ void ejs_exception(const char *reason) EjsId eid; EjsHandle handle = 0; MprVar result; - char *emsg; + char *emsg, *script; + size_t script_size; TALLOC_CTX *mem_ctx = talloc_new(NULL); const char **argv_list = NULL; struct MprVar v; @@ -79,10 +80,12 @@ void ejs_exception(const char *reason) v = mprList("ARGV", argv_list); mprCreateProperty(ejsGetGlobalObject(eid), "ARGV", &v); + /* load the script and advance past interpreter line*/ + script = file_load(argv[1], &script_size); + /* run the script */ - if (ejsEvalFile(eid, discard_const_p(char, argv[1]), &result, - &emsg) == -1) { - fprintf(stderr, "smbscript: ejsEvalFile(): %s\n", emsg); + if (ejsEvalScript(eid, script, &result, &emsg) == -1) { + fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg); exit(1); } -- cgit From c1b95bd467109793333c15ea44ec910ffe3e86b4 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 3 Jun 2005 12:04:26 +0000 Subject: r7223: Advance script past interpreter line. (This used to be commit 31b9fadbed656f666f587a9dcb5a7627a2d388aa) --- source4/scripting/ejs/smbscript.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 323e604f8d..43a9377143 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -83,6 +83,17 @@ void ejs_exception(const char *reason) /* load the script and advance past interpreter line*/ script = file_load(argv[1], &script_size); + if ((script_size > 2) && script[0] == '#' && script[1] == '!') { + script += 2; + script_size -= 2; + while (script_size) { + if (*script == '\r' || *script == '\n') + break; + script++; + script_size--; + } + } + /* run the script */ if (ejsEvalScript(eid, script, &result, &emsg) == -1) { fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg); -- cgit From e8cbe7f3a4d828619285cdfebf5b786ddebf7928 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 3 Jun 2005 12:31:56 +0000 Subject: r7225: Create a MprVar object from a NTSTATUS, e.g: res: { is_err: true, is_ok: false, errstr: "NT_STATUS_IO_TIMEOUT", v: -1073741643 } (This used to be commit d81d5f8317ca82a08e6fc38ef7313fad2e631281) --- source4/scripting/ejs/mprutil.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c index 3e47fdf9a3..4aee7d1c50 100644 --- a/source4/scripting/ejs/mprutil.c +++ b/source4/scripting/ejs/mprutil.c @@ -166,3 +166,26 @@ const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v) return list; } +/* + turn a NTSTATUS into a MprVar object with lots of funky properties +*/ +struct MprVar mprNTSTATUS(NTSTATUS status) +{ + struct MprVar res, val; + + res = mprCreateObjVar("ntstatus", MPR_DEFAULT_HASH_SIZE); + + val = mprCreateStringVar(nt_errstr(status), 1); + mprCreateProperty(&res, "errstr", &val); + + val = mprCreateIntegerVar(NT_STATUS_V(status)); + mprCreateProperty(&res, "v", &val); + + val = mprCreateBoolVar(NT_STATUS_IS_OK(status)); + mprCreateProperty(&res, "is_ok", &val); + + val = mprCreateBoolVar(NT_STATUS_IS_ERR(status)); + mprCreateProperty(&res, "is_err", &val); + + return res; +} -- cgit From 266c37e5dc97879e30e790cd87d2ec1f43907477 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Fri, 3 Jun 2005 14:17:18 +0000 Subject: r7238: Add pam auth support in swat (This used to be commit 8a98572a3b5dba58181dc402dbebae5452656012) --- source4/scripting/ejs/smbcalls.c | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index fc2c16a456..8a02111bd5 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -25,6 +25,7 @@ #include "param/loadparm.h" #include "lib/ldb/include/ldb.h" #include "librpc/gen_ndr/ndr_nbt.h" +#include "auth/auth.h" /* return the type of a variable @@ -298,6 +299,85 @@ static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv) return -1; } +static int ejs_userAuth(MprVarHandle eid, int argc, char **argv) +{ + struct auth_usersupplied_info *user_info = NULL; + struct auth_serversupplied_info *server_info = NULL; + struct auth_context *auth_context; + TALLOC_CTX *tmp_ctx; + struct MprVar auth; + NTSTATUS nt_status; + DATA_BLOB pw_blob; + int ret; + + if (argc != 3 || *argv[0] == 0 || *argv[2] == 0) { + ejsSetErrorMsg(eid, "userAuth invalid arguments"); + return -1; + } + + tmp_ctx = talloc_new(mprMemCtx()); + auth = mprCreateObjVar("auth", MPR_DEFAULT_HASH_SIZE); + + if (strcmp("System User", argv[2]) == 0) { + const char *auth_unix[] = { "unix", NULL }; + + nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context); + if (!NT_STATUS_IS_OK(nt_status)) { + mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False)); + mprSetPropertyValue(&auth, "report", mprCreateStringVar("Auth System Failure", 0)); + goto done; + } + + pw_blob = data_blob(argv[1], strlen(argv[1])), + make_user_info(tmp_ctx, argv[0], argv[0], + argv[2], argv[2], + "foowks", "fooip", + NULL, NULL, + NULL, NULL, + &pw_blob, False, + 0x05, &user_info); + nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info); + if (!NT_STATUS_IS_OK(nt_status)) { + mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False)); + mprSetPropertyValue(&auth, "report", mprCreateStringVar("Login Failed", 0)); + goto done; + } + + mprSetPropertyValue(&auth, "result", mprCreateBoolVar(server_info->authenticated)); + mprSetPropertyValue(&auth, "username", mprCreateStringVar(server_info->account_name, 0)); + mprSetPropertyValue(&auth, "domain", mprCreateStringVar(server_info->domain_name, 0)); + + } else { + mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False)); + mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 0)); + } + +done: + ejsSetReturnValue(eid, auth); + talloc_free(tmp_ctx); + return 0; +} + +static int ejs_domain_list(MprVarHandle eid, int argc, char **argv) +{ + struct MprVar list; + struct MprVar dom; + + if (argc != 0) { + ejsSetErrorMsg(eid, "domList invalid arguments"); + return -1; + } + + list = mprCreateObjVar("list", MPR_DEFAULT_HASH_SIZE); + dom = mprCreateStringVar("System User", 1); + mprCreateProperty(&list, "0", &dom); + + ejsSetReturnValue(eid, list); + + return 0; +} + + /* setup the C functions that be called from ejs */ @@ -308,4 +388,6 @@ void smb_setup_ejs_functions(void) ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "resolveName", ejs_resolve_name, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineStringCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE); } -- cgit From 384ad5c71b012d11e61d5a9ad8059423f771cccb Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sat, 4 Jun 2005 01:06:30 +0000 Subject: r7254: Add a mprWERROR() function with the same attributes as mprNTSTATUS. (This used to be commit 2fa6f7bb2b8390f6486f6531212b556e98a6c528) --- source4/scripting/ejs/mprutil.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c index 4aee7d1c50..2a1036d9a4 100644 --- a/source4/scripting/ejs/mprutil.c +++ b/source4/scripting/ejs/mprutil.c @@ -189,3 +189,27 @@ struct MprVar mprNTSTATUS(NTSTATUS status) return res; } + +/* + turn a WERROR into a MprVar object with lots of funky properties +*/ +struct MprVar mprWERROR(WERROR status) +{ + struct MprVar res, val; + + res = mprCreateObjVar("werror", MPR_DEFAULT_HASH_SIZE); + + val = mprCreateStringVar(win_errstr(status), 1); + mprCreateProperty(&res, "errstr", &val); + + val = mprCreateIntegerVar(W_ERROR_V(status)); + mprCreateProperty(&res, "v", &val); + + val = mprCreateBoolVar(W_ERROR_IS_OK(status)); + mprCreateProperty(&res, "is_ok", &val); + + val = mprCreateBoolVar(True); + mprCreateProperty(&res, "is_err", &val); + + return res; +} -- cgit From 383c5fb68a212eed60750793ad4b7e4bbcbcd2d5 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sat, 4 Jun 2005 01:08:52 +0000 Subject: r7255: Change syntax of resolveName() js function to be more like the resolve_name() C function. I can't figure out how to return variables by reference though. Writing to argv[] doesn't seem to work. (This used to be commit aef99859f28570b60f4fefe2981160269f6eb02a) --- source4/scripting/ejs/smbcalls.c | 54 +++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 8a02111bd5..5d4c61b04b 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -253,50 +253,53 @@ failed: look up a netbios name syntax: - resolveName("frogurt"); - resolveName("frogurt", 0x1c); + resolveName(result, "frogurt"); + resolveName(result, "frogurt", 0x1c); */ static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv) { + int result = -1; struct nbt_name name; TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); - NTSTATUS result; + NTSTATUS nt_status; const char *reply_addr; /* validate arguments */ - if (argc == 1) { - if (argv[0]->type != MPR_TYPE_STRING) { - ejsSetErrorMsg(eid, "resolveName invalid arguments"); - goto failed; - } - make_nbt_name_client(&name, mprToString(argv[0])); - } else if (argc == 2) { + if (argc < 2 || argc > 3) { + ejsSetErrorMsg(eid, "resolveName invalid arguments"); + goto done; + } + + if (argv[1]->type != MPR_TYPE_STRING) { + ejsSetErrorMsg(eid, "resolveName invalid arguments"); + goto done; + } + + if (argc == 2) { + make_nbt_name_client(&name, mprToString(argv[1])); + } else { if (argv[1]->type != MPR_TYPE_INT) { ejsSetErrorMsg(eid, "resolveName invalid arguments"); - goto failed; + goto done; } - make_nbt_name(&name, mprToString(argv[0]), mprToInt(argv[1])); - } else { - ejsSetErrorMsg(eid, "resolveName invalid arguments"); - goto failed; + make_nbt_name(&name, mprToString(argv[1]), mprToInt(argv[2])); } - result = resolve_name(&name, tmp_ctx, &reply_addr); + result = 0; - if (!NT_STATUS_IS_OK(result)) { - ejsSetErrorMsg(eid, "resolveName name not found"); - goto failed; + nt_status = resolve_name(&name, tmp_ctx, &reply_addr); + + if (NT_STATUS_IS_OK(nt_status)) { + mprDestroyAllVars(argv[0]); + *argv[0] = mprCreateStringVar(reply_addr, True); } - - ejsSetReturnString(eid, reply_addr); - talloc_free(tmp_ctx); - return 0; + ejsSetReturnValue(eid, mprNTSTATUS(nt_status)); - failed: + done: talloc_free(tmp_ctx); - return -1; + return result; } static int ejs_userAuth(MprVarHandle eid, int argc, char **argv) @@ -308,7 +311,6 @@ static int ejs_userAuth(MprVarHandle eid, int argc, char **argv) struct MprVar auth; NTSTATUS nt_status; DATA_BLOB pw_blob; - int ret; if (argc != 3 || *argv[0] == 0 || *argv[2] == 0) { ejsSetErrorMsg(eid, "userAuth invalid arguments"); -- cgit From b1243510793cd135f1464dcfeec0b7e88999d1a0 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sat, 4 Jun 2005 03:32:18 +0000 Subject: r7261: Pass by reference is done in js via MPR_TYPE_OBJECT. Update argument parsing and example for resolveName(). (This used to be commit 1a4a54931733ebfa743401a184fe460c044427b4) --- source4/scripting/ejs/smbcalls.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 5d4c61b04b..93496bd9c9 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -271,6 +271,11 @@ static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv) goto done; } + if (argv[0]->type != MPR_TYPE_OBJECT) { + ejsSetErrorMsg(eid, "resolvename invalid arguments"); + goto done; + } + if (argv[1]->type != MPR_TYPE_STRING) { ejsSetErrorMsg(eid, "resolveName invalid arguments"); goto done; @@ -291,8 +296,8 @@ static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv) nt_status = resolve_name(&name, tmp_ctx, &reply_addr); if (NT_STATUS_IS_OK(nt_status)) { - mprDestroyAllVars(argv[0]); - *argv[0] = mprCreateStringVar(reply_addr, True); + mprSetPropertyValue(argv[0], "value", + mprCreateStringVar(reply_addr, 1)); } ejsSetReturnValue(eid, mprNTSTATUS(nt_status)); -- cgit From d6555cadb7015d407a2a17538d8a1ccd073a41e6 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sat, 4 Jun 2005 03:35:38 +0000 Subject: r7262: Add a length property to ARGV array. (This used to be commit 4b775c619b7abed52d158ab70505320753a0c9cb) --- source4/scripting/ejs/smbscript.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 43a9377143..4bde40a955 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -78,6 +78,7 @@ void ejs_exception(const char *reason) argv_list = str_list_add(argv_list, argv[i]); } v = mprList("ARGV", argv_list); + mprSetPropertyValue(&v, "length", mprCreateIntegerVar(argc - 2)); mprCreateProperty(ejsGetGlobalObject(eid), "ARGV", &v); /* load the script and advance past interpreter line*/ -- cgit From 32f2e9806b267782125fed3a6162ea895e634eef Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sat, 4 Jun 2005 03:51:38 +0000 Subject: r7263: Exit smbscript with the intepreter return value (defaults to 0). Change the exit value for an exception, usage error and other non-js errors to 127 which is kinda like the return value for the system(3) function. (This used to be commit c77a232b1152a27e2d8ffb719aefba6c6b2ba6df) --- source4/scripting/ejs/smbscript.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 4bde40a955..02a3aff28e 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -1,7 +1,7 @@ /* Unix SMB/CIFS implementation. - Standalone client for ESP scripting. + Standalone client for ejs scripting. Copyright (C) Tim Potter 2005 @@ -27,7 +27,7 @@ void ejs_exception(const char *reason) { fprintf(stderr, "smbscript exception: %s", reason); - exit(1); + exit(127); } int main(int argc, const char *argv[]) @@ -39,12 +39,12 @@ void ejs_exception(const char *reason) size_t script_size; TALLOC_CTX *mem_ctx = talloc_new(NULL); const char **argv_list = NULL; - struct MprVar v; - int i; + struct MprVar v, *return_var; + int exit_status, i; if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); - exit(1); + exit(127); } setup_logging(argv[0],DEBUG_STDOUT); @@ -52,7 +52,7 @@ void ejs_exception(const char *reason) if (!lp_load(dyn_CONFIGFILE, False, False, False)) { fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n", argv[0], dyn_CONFIGFILE); - exit(1); + exit(127); } load_interfaces(); @@ -62,7 +62,7 @@ void ejs_exception(const char *reason) if (ejsOpen(NULL, NULL, NULL) != 0) { fprintf(stderr, "smbscript: ejsOpen(): unable to initialise " "EJ subsystem\n"); - exit(1); + exit(127); } smb_setup_ejs_functions(); @@ -70,7 +70,7 @@ void ejs_exception(const char *reason) if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) { fprintf(stderr, "smbscript: ejsOpenEngine(): unable to " "initialise an EJS engine\n"); - exit(1); + exit(127); } /* setup ARGV[] in the ejs environment */ @@ -98,12 +98,15 @@ void ejs_exception(const char *reason) /* run the script */ if (ejsEvalScript(eid, script, &result, &emsg) == -1) { fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg); - exit(1); + exit(127); } + return_var = ejsGetReturnValue(eid); + exit_status = return_var->integer; + ejsClose(); talloc_free(mem_ctx); - return 0; + return exit_status; } -- cgit From f84d007c438dd43f2cd5a9b6ca583976c8e777fa Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sat, 4 Jun 2005 07:04:43 +0000 Subject: r7266: Split the different types of js function defines into separate files, as there are going to be a lot more of them. (This used to be commit b086768589ee27de6616945bbea42b18b40d25d5) --- source4/scripting/ejs/smbcalls.c | 250 +------------------------------- source4/scripting/ejs/smbcalls.h | 25 ++++ source4/scripting/ejs/smbcalls_config.c | 149 +++++++++++++++++++ source4/scripting/ejs/smbcalls_ldb.c | 93 ++++++++++++ source4/scripting/ejs/smbcalls_nbt.c | 91 ++++++++++++ 5 files changed, 365 insertions(+), 243 deletions(-) create mode 100644 source4/scripting/ejs/smbcalls.h create mode 100644 source4/scripting/ejs/smbcalls_config.c create mode 100644 source4/scripting/ejs/smbcalls_ldb.c create mode 100644 source4/scripting/ejs/smbcalls_nbt.c (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 93496bd9c9..54dc8d4e62 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -4,6 +4,7 @@ provide hooks into smbd C calls from ejs scripts Copyright (C) Andrew Tridgell 2005 + Copyright (C) Tim Potter 2005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,9 +23,6 @@ #include "includes.h" #include "lib/ejs/ejs.h" -#include "param/loadparm.h" -#include "lib/ldb/include/ldb.h" -#include "librpc/gen_ndr/ndr_nbt.h" #include "auth/auth.h" /* @@ -68,245 +66,11 @@ static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv) /* setup a return of a string list */ -static void ejs_returnlist(MprVarHandle eid, - const char *name, const char **list) + void ejs_returnlist(MprVarHandle eid, const char *name, const char **list) { ejsSetReturnValue(eid, mprList(name, list)); } -/* - return a list of defined services -*/ -static int ejs_lpServices(MprVarHandle eid, int argc, char **argv) -{ - int i; - const char **list = NULL; - if (argc != 0) return -1; - - for (i=0;iclass == P_GLOBAL) { - return -1; - } - parm_ptr = lp_parm_ptr(snum, parm); - } else if (strchr(argv[0], ':')) { - /* its a global parametric option */ - const char *type = talloc_strndup(mprMemCtx(), - argv[0], strcspn(argv[0], ":")); - const char *option = strchr(argv[0], ':') + 1; - const char *value; - if (type == NULL || option == NULL) return -1; - value = lp_get_parametric(-1, type, option); - if (value == NULL) return -1; - ejsSetReturnString(eid, value); - return 0; - } else { - /* its a global parameter */ - parm = lp_parm_struct(argv[0]); - if (parm == NULL) return -1; - parm_ptr = parm->ptr; - } - - if (parm == NULL || parm_ptr == NULL) { - return -1; - } - - /* construct and return the right type of ejs object */ - switch (parm->type) { - case P_STRING: - case P_USTRING: - ejsSetReturnString(eid, *(char **)parm_ptr); - break; - case P_BOOL: - ejsSetReturnValue(eid, mprCreateBoolVar(*(BOOL *)parm_ptr)); - break; - case P_INTEGER: - ejsSetReturnValue(eid, mprCreateIntegerVar(*(int *)parm_ptr)); - break; - case P_ENUM: - for (i=0; parm->enum_list[i].name; i++) { - if (*(int *)parm_ptr == parm->enum_list[i].value) { - ejsSetReturnString(eid, parm->enum_list[i].name); - return 0; - } - } - return -1; - case P_LIST: - ejs_returnlist(eid, parm->label, *(const char ***)parm_ptr); - break; - case P_SEP: - return -1; - } - return 0; -} - - -/* - perform an ldb search, returning an array of results - - syntax: - ldbSearch("dbfile", "expression"); - var attrs = new Array("attr1", "attr2", "attr3"); - ldbSearch("dbfile", "expression", attrs); -*/ -static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv) -{ - const char **attrs = NULL; - const char *expression, *dbfile; - TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); - struct ldb_context *ldb; - int ret; - struct ldb_message **res; - - /* validate arguments */ - if (argc < 2 || argc > 3 || - argv[0]->type != MPR_TYPE_STRING) { - ejsSetErrorMsg(eid, "ldbSearch invalid arguments"); - goto failed; - } - if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) { - ejsSetErrorMsg(eid, "ldbSearch attributes must be an object"); - goto failed; - } - - dbfile = mprToString(argv[0]); - expression = mprToString(argv[1]); - if (argc > 2) { - attrs = mprToList(tmp_ctx, argv[2]); - } - if (dbfile == NULL || expression == NULL) { - ejsSetErrorMsg(eid, "ldbSearch invalid arguments"); - goto failed; - } - - ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL); - if (ldb == NULL) { - ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile); - goto failed; - } - - ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res); - if (ret == -1) { - ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb)); - goto failed; - } - - ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message")); - - talloc_free(tmp_ctx); - return 0; - -failed: - talloc_free(tmp_ctx); - return -1; -} - -/* - look up a netbios name - - syntax: - resolveName(result, "frogurt"); - resolveName(result, "frogurt", 0x1c); -*/ - -static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv) -{ - int result = -1; - struct nbt_name name; - TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); - NTSTATUS nt_status; - const char *reply_addr; - - /* validate arguments */ - if (argc < 2 || argc > 3) { - ejsSetErrorMsg(eid, "resolveName invalid arguments"); - goto done; - } - - if (argv[0]->type != MPR_TYPE_OBJECT) { - ejsSetErrorMsg(eid, "resolvename invalid arguments"); - goto done; - } - - if (argv[1]->type != MPR_TYPE_STRING) { - ejsSetErrorMsg(eid, "resolveName invalid arguments"); - goto done; - } - - if (argc == 2) { - make_nbt_name_client(&name, mprToString(argv[1])); - } else { - if (argv[1]->type != MPR_TYPE_INT) { - ejsSetErrorMsg(eid, "resolveName invalid arguments"); - goto done; - } - make_nbt_name(&name, mprToString(argv[1]), mprToInt(argv[2])); - } - - result = 0; - - nt_status = resolve_name(&name, tmp_ctx, &reply_addr); - - if (NT_STATUS_IS_OK(nt_status)) { - mprSetPropertyValue(argv[0], "value", - mprCreateStringVar(reply_addr, 1)); - } - - ejsSetReturnValue(eid, mprNTSTATUS(nt_status)); - - done: - talloc_free(tmp_ctx); - return result; -} - static int ejs_userAuth(MprVarHandle eid, int argc, char **argv) { struct auth_usersupplied_info *user_info = NULL; @@ -386,15 +150,15 @@ static int ejs_domain_list(MprVarHandle eid, int argc, char **argv) /* - setup the C functions that be called from ejs + setup C functions that be called from ejs */ void smb_setup_ejs_functions(void) { - ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE); + smb_setup_ejs_config(); + smb_setup_ejs_ldb(); + smb_setup_ejs_nbt(); + ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "resolveName", ejs_resolve_name, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineStringCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE); } diff --git a/source4/scripting/ejs/smbcalls.h b/source4/scripting/ejs/smbcalls.h new file mode 100644 index 0000000000..8b26e13873 --- /dev/null +++ b/source4/scripting/ejs/smbcalls.h @@ -0,0 +1,25 @@ +/* + Unix SMB/CIFS implementation. + + provide hooks into smbd C calls from ejs scripts + + Copyright (C) Andrew Tridgell 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "lib/ejs/ejs.h" + +void ejs_returnlist(MprVarHandle eid, const char *name, const char **list); diff --git a/source4/scripting/ejs/smbcalls_config.c b/source4/scripting/ejs/smbcalls_config.c new file mode 100644 index 0000000000..cd0ecbd767 --- /dev/null +++ b/source4/scripting/ejs/smbcalls_config.c @@ -0,0 +1,149 @@ +/* + Unix SMB/CIFS implementation. + + provide hooks into smbd C calls from ejs scripts + + Copyright (C) Andrew Tridgell 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "scripting/ejs/smbcalls.h" +#include "lib/ejs/ejs.h" +#include "param/loadparm.h" + +/* + return a list of defined services +*/ +static int ejs_lpServices(MprVarHandle eid, int argc, char **argv) +{ + int i; + const char **list = NULL; + if (argc != 0) return -1; + + for (i=0;iclass == P_GLOBAL) { + return -1; + } + parm_ptr = lp_parm_ptr(snum, parm); + } else if (strchr(argv[0], ':')) { + /* its a global parametric option */ + const char *type = talloc_strndup(mprMemCtx(), + argv[0], strcspn(argv[0], ":")); + const char *option = strchr(argv[0], ':') + 1; + const char *value; + if (type == NULL || option == NULL) return -1; + value = lp_get_parametric(-1, type, option); + if (value == NULL) return -1; + ejsSetReturnString(eid, value); + return 0; + } else { + /* its a global parameter */ + parm = lp_parm_struct(argv[0]); + if (parm == NULL) return -1; + parm_ptr = parm->ptr; + } + + if (parm == NULL || parm_ptr == NULL) { + return -1; + } + + /* construct and return the right type of ejs object */ + switch (parm->type) { + case P_STRING: + case P_USTRING: + ejsSetReturnString(eid, *(char **)parm_ptr); + break; + case P_BOOL: + ejsSetReturnValue(eid, mprCreateBoolVar(*(BOOL *)parm_ptr)); + break; + case P_INTEGER: + ejsSetReturnValue(eid, mprCreateIntegerVar(*(int *)parm_ptr)); + break; + case P_ENUM: + for (i=0; parm->enum_list[i].name; i++) { + if (*(int *)parm_ptr == parm->enum_list[i].value) { + ejsSetReturnString(eid, parm->enum_list[i].name); + return 0; + } + } + return -1; + case P_LIST: + ejs_returnlist(eid, parm->label, *(const char ***)parm_ptr); + break; + case P_SEP: + return -1; + } + return 0; +} + +/* + setup C functions that be called from ejs +*/ +void smb_setup_ejs_config(void) +{ + ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE); +} diff --git a/source4/scripting/ejs/smbcalls_ldb.c b/source4/scripting/ejs/smbcalls_ldb.c new file mode 100644 index 0000000000..cae3857686 --- /dev/null +++ b/source4/scripting/ejs/smbcalls_ldb.c @@ -0,0 +1,93 @@ +/* + Unix SMB/CIFS implementation. + + provide hooks into smbd C calls from ejs scripts + + Copyright (C) Andrew Tridgell 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "lib/ejs/ejs.h" +#include "lib/ldb/include/ldb.h" + +/* + perform an ldb search, returning an array of results + + syntax: + ldbSearch("dbfile", "expression"); + var attrs = new Array("attr1", "attr2", "attr3"); + ldbSearch("dbfile", "expression", attrs); +*/ +static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv) +{ + const char **attrs = NULL; + const char *expression, *dbfile; + TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); + struct ldb_context *ldb; + int ret; + struct ldb_message **res; + + /* validate arguments */ + if (argc < 2 || argc > 3 || + argv[0]->type != MPR_TYPE_STRING) { + ejsSetErrorMsg(eid, "ldbSearch invalid arguments"); + goto failed; + } + if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) { + ejsSetErrorMsg(eid, "ldbSearch attributes must be an object"); + goto failed; + } + + dbfile = mprToString(argv[0]); + expression = mprToString(argv[1]); + if (argc > 2) { + attrs = mprToList(tmp_ctx, argv[2]); + } + if (dbfile == NULL || expression == NULL) { + ejsSetErrorMsg(eid, "ldbSearch invalid arguments"); + goto failed; + } + + ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL); + if (ldb == NULL) { + ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile); + goto failed; + } + + ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res); + if (ret == -1) { + ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb)); + goto failed; + } + + ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message")); + + talloc_free(tmp_ctx); + return 0; + +failed: + talloc_free(tmp_ctx); + return -1; +} + +/* + setup C functions that be called from ejs +*/ +void smb_setup_ejs_ldb(void) +{ + ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE); +} diff --git a/source4/scripting/ejs/smbcalls_nbt.c b/source4/scripting/ejs/smbcalls_nbt.c new file mode 100644 index 0000000000..a82b636c9c --- /dev/null +++ b/source4/scripting/ejs/smbcalls_nbt.c @@ -0,0 +1,91 @@ +/* + Unix SMB/CIFS implementation. + + provide hooks into smbd C calls from ejs scripts + + Copyright (C) Tim Potter 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "lib/ejs/ejs.h" +#include "librpc/gen_ndr/ndr_nbt.h" + +/* + look up a netbios name + + syntax: + resolveName(result, "frogurt"); + resolveName(result, "frogurt", 0x1c); +*/ + +static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv) +{ + int result = -1; + struct nbt_name name; + TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); + NTSTATUS nt_status; + const char *reply_addr; + + /* validate arguments */ + if (argc < 2 || argc > 3) { + ejsSetErrorMsg(eid, "resolveName invalid arguments"); + goto done; + } + + if (argv[0]->type != MPR_TYPE_OBJECT) { + ejsSetErrorMsg(eid, "resolvename invalid arguments"); + goto done; + } + + if (argv[1]->type != MPR_TYPE_STRING) { + ejsSetErrorMsg(eid, "resolveName invalid arguments"); + goto done; + } + + if (argc == 2) { + make_nbt_name_client(&name, mprToString(argv[1])); + } else { + if (argv[1]->type != MPR_TYPE_INT) { + ejsSetErrorMsg(eid, "resolveName invalid arguments"); + goto done; + } + make_nbt_name(&name, mprToString(argv[1]), mprToInt(argv[2])); + } + + result = 0; + + nt_status = resolve_name(&name, tmp_ctx, &reply_addr); + + if (NT_STATUS_IS_OK(nt_status)) { + mprSetPropertyValue(argv[0], "value", + mprCreateStringVar(reply_addr, 1)); + } + + ejsSetReturnValue(eid, mprNTSTATUS(nt_status)); + + done: + talloc_free(tmp_ctx); + return result; +} + +/* + setup C functions that be called from ejs +*/ +void smb_setup_ejs_nbt(void) +{ + ejsDefineCFunction(-1, "resolveName", ejs_resolve_name, NULL, MPR_VAR_SCRIPT_HANDLE); +} -- cgit From b7bc13209f5133a117428f2ce58d995399fb684b Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sat, 4 Jun 2005 08:23:15 +0000 Subject: r7267: REMOTE_HOST is a better choice (This used to be commit 41bae267e29614d300ec2505c927ab17ccbbe64f) --- source4/scripting/ejs/smbcalls.c | 99 ++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 34 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 54dc8d4e62..3ca6003ee4 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -71,54 +71,85 @@ static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv) ejsSetReturnValue(eid, mprList(name, list)); } -static int ejs_userAuth(MprVarHandle eid, int argc, char **argv) +static int ejs_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username, const char *password, const char *domain, const char *remote_host) { struct auth_usersupplied_info *user_info = NULL; struct auth_serversupplied_info *server_info = NULL; struct auth_context *auth_context; - TALLOC_CTX *tmp_ctx; - struct MprVar auth; + const char *auth_unix[] = { "unix", NULL }; NTSTATUS nt_status; DATA_BLOB pw_blob; - if (argc != 3 || *argv[0] == 0 || *argv[2] == 0) { - ejsSetErrorMsg(eid, "userAuth invalid arguments"); - return -1; + nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context); + if (!NT_STATUS_IS_OK(nt_status)) { + mprSetPropertyValue(auth, "result", mprCreateBoolVar(False)); + mprSetPropertyValue(auth, "report", mprCreateStringVar("Auth System Failure", 0)); + goto done; } - tmp_ctx = talloc_new(mprMemCtx()); - auth = mprCreateObjVar("auth", MPR_DEFAULT_HASH_SIZE); + pw_blob = data_blob(password, strlen(password)), + make_user_info(tmp_ctx, username, username, + domain, domain, + remote_host, remote_host, + NULL, NULL, + NULL, NULL, + &pw_blob, False, + USER_INFO_CASE_INSENSITIVE_USERNAME | + USER_INFO_DONT_CHECK_UNIX_ACCOUNT, + &user_info); + nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info); + if (!NT_STATUS_IS_OK(nt_status)) { + mprSetPropertyValue(auth, "result", mprCreateBoolVar(False)); + mprSetPropertyValue(auth, "report", mprCreateStringVar("Login Failed", 0)); + goto done; + } - if (strcmp("System User", argv[2]) == 0) { - const char *auth_unix[] = { "unix", NULL }; + mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated)); + mprSetPropertyValue(auth, "username", mprCreateStringVar(server_info->account_name, 0)); + mprSetPropertyValue(auth, "domain", mprCreateStringVar(server_info->domain_name, 0)); - nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context); - if (!NT_STATUS_IS_OK(nt_status)) { - mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False)); - mprSetPropertyValue(&auth, "report", mprCreateStringVar("Auth System Failure", 0)); - goto done; - } +done: + return 0; +} - pw_blob = data_blob(argv[1], strlen(argv[1])), - make_user_info(tmp_ctx, argv[0], argv[0], - argv[2], argv[2], - "foowks", "fooip", - NULL, NULL, - NULL, NULL, - &pw_blob, False, - 0x05, &user_info); - nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info); - if (!NT_STATUS_IS_OK(nt_status)) { - mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False)); - mprSetPropertyValue(&auth, "report", mprCreateStringVar("Login Failed", 0)); - goto done; - } +/* + perform user authentication, returning an array of results + + syntax: + var authinfo = new Object(); + authinfo.username = myname; + authinfo.password = mypass; + authinfo.domain = mydom; + authinfo.rhost = request['REMOTE_HOST']; + auth = userAuth(authinfo); +*/ +static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv) +{ + TALLOC_CTX *tmp_ctx; + const char *username; + const char *password; + const char *domain; + const char *remote_host; + struct MprVar auth; + + if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) { + ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object."); + return -1; + } + + username = mprToString(mprGetProperty(argv[0], "username", NULL)); + password = mprToString(mprGetProperty(argv[0], "password", NULL)); + domain = mprToString(mprGetProperty(argv[0], "domain", NULL)); + remote_host = mprToString(mprGetProperty(argv[0], "rhost", NULL)); + + tmp_ctx = talloc_new(mprMemCtx()); + auth = mprCreateObjVar("auth", MPR_DEFAULT_HASH_SIZE); - mprSetPropertyValue(&auth, "result", mprCreateBoolVar(server_info->authenticated)); - mprSetPropertyValue(&auth, "username", mprCreateStringVar(server_info->account_name, 0)); - mprSetPropertyValue(&auth, "domain", mprCreateStringVar(server_info->domain_name, 0)); + if (strcmp("System User", domain) == 0) { + ejs_systemAuth(tmp_ctx, &auth, username, password, domain, remote_host); } else { + mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False)); mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 0)); } @@ -160,5 +191,5 @@ void smb_setup_ejs_functions(void) ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineStringCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE); } -- cgit From 791db4bf72f282840a7bfefa2a3ccf9b3191a6e6 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sat, 4 Jun 2005 08:54:07 +0000 Subject: r7268: allocate the strings to avoid them disappearing under our feet (This used to be commit ddd7454cb3e2445fd32682b380be89c70f8a22cb) --- source4/scripting/ejs/smbcalls.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 3ca6003ee4..8ea5967ef4 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -83,7 +83,7 @@ static int ejs_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char * nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context); if (!NT_STATUS_IS_OK(nt_status)) { mprSetPropertyValue(auth, "result", mprCreateBoolVar(False)); - mprSetPropertyValue(auth, "report", mprCreateStringVar("Auth System Failure", 0)); + mprSetPropertyValue(auth, "report", mprCreateStringVar("Auth System Failure", 1)); goto done; } @@ -100,13 +100,13 @@ static int ejs_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char * nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info); if (!NT_STATUS_IS_OK(nt_status)) { mprSetPropertyValue(auth, "result", mprCreateBoolVar(False)); - mprSetPropertyValue(auth, "report", mprCreateStringVar("Login Failed", 0)); + mprSetPropertyValue(auth, "report", mprCreateStringVar("Login Failed", 1)); goto done; } mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated)); - mprSetPropertyValue(auth, "username", mprCreateStringVar(server_info->account_name, 0)); - mprSetPropertyValue(auth, "domain", mprCreateStringVar(server_info->domain_name, 0)); + mprSetPropertyValue(auth, "username", mprCreateStringVar(server_info->account_name, 1)); + mprSetPropertyValue(auth, "domain", mprCreateStringVar(server_info->domain_name, 1)); done: return 0; @@ -151,7 +151,7 @@ static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv) } else { mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False)); - mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 0)); + mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 1)); } done: -- cgit From 8a2e208e45001305a4fa18f92c4109a33b6ed407 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Tue, 7 Jun 2005 06:38:13 +0000 Subject: r7350: Remove unused label. (This used to be commit ada004595760605134eb741da9c549521938c2ce) --- source4/scripting/ejs/smbcalls.c | 1 - 1 file changed, 1 deletion(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 8ea5967ef4..00a2547bac 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -154,7 +154,6 @@ static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv) mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 1)); } -done: ejsSetReturnValue(eid, auth); talloc_free(tmp_ctx); return 0; -- cgit From 6eae7eb3c446e26d8003846a79471e6232b1b92e Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Tue, 7 Jun 2005 07:00:28 +0000 Subject: r7351: Start of ejs smb client library. I need to figure out a nice API here that doesn't expose too much of the cifs protocol but still allows people to do neat things. Also, talloc lifetimes need to be thought about properly. (This used to be commit 8062e808ef5102b96e1b3de046c858e4a69b4d82) --- source4/scripting/ejs/smbcalls.c | 1 + source4/scripting/ejs/smbcalls_cli.c | 89 ++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 source4/scripting/ejs/smbcalls_cli.c (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 00a2547bac..46fce4b98e 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -187,6 +187,7 @@ void smb_setup_ejs_functions(void) smb_setup_ejs_config(); smb_setup_ejs_ldb(); smb_setup_ejs_nbt(); + smb_setup_ejs_cli(); ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE); diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c new file mode 100644 index 0000000000..adb55a0184 --- /dev/null +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -0,0 +1,89 @@ +/* + Unix SMB/CIFS implementation. + + provide hooks into smbd C calls from ejs scripts + + Copyright (C) Tim Potter 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "lib/ejs/ejs.h" +#include "librpc/gen_ndr/ndr_nbt.h" + +/* Connect to a server */ + +static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) +{ + struct smbcli_socket *sock; + struct smbcli_transport *transport; + struct nbt_name calling, called; + NTSTATUS result; + + if (argc != 1) { + ejsSetErrorMsg(eid, "connect invalid arguments"); + return -1; + } + + /* Socket connect */ + + sock = smbcli_sock_init(NULL, NULL); + + if (!sock) { + ejsSetErrorMsg(eid, "socket initialisation failed"); + return -1; + } + + if (!smbcli_sock_connect_byname(sock, argv[0], 0)) { + ejsSetErrorMsg(eid, "socket connect failed"); + return -1; + } + + transport = smbcli_transport_init(sock, sock, True); + + if (!transport) { + ejsSetErrorMsg(eid, "transport init failed"); + return -1; + } + + /* Send a netbios session request */ + + make_nbt_name_client(&calling, lp_netbios_name()); + + nbt_choose_called_name(NULL, &called, argv[0], NBT_NAME_SERVER); + + if (!smbcli_transport_connect(transport, &calling, &called)) { + ejsSetErrorMsg(eid, "transport establishment failed"); + return -1; + } + + result = smb_raw_negotiate(transport, lp_maxprotocol()); + + if (!NT_STATUS_IS_OK(result)) { + ejsSetReturnValue(eid, mprNTSTATUS(result)); + return 0; + } + + return 0; +} + +/* + setup C functions that be called from ejs +*/ +void smb_setup_ejs_cli(void) +{ + ejsDefineStringCFunction(-1, "connect", ejs_cli_connect, NULL, MPR_VAR_SCRIPT_HANDLE); +} -- cgit From 0b119901d8d948e62d46f760b2dd40ba5331afc9 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Thu, 9 Jun 2005 07:28:21 +0000 Subject: r7422: Create a ejs object to wrap a smbcli_transport pointer. (This used to be commit a55e40651d06f416871ccbe04ad3b7d25444d645) --- source4/scripting/ejs/smbcalls_cli.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c index adb55a0184..de31d7a900 100644 --- a/source4/scripting/ejs/smbcalls_cli.c +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -24,6 +24,20 @@ #include "lib/ejs/ejs.h" #include "librpc/gen_ndr/ndr_nbt.h" +static struct MprVar mprTransport(struct smbcli_transport *transport) +{ + struct MprVar res, val; + + res = mprCreateObjVar("transport", MPR_DEFAULT_HASH_SIZE); + + val = mprCreateStringVar(talloc_get_name(transport), 1); + mprCreateProperty(&res, "name", &val); + + /* TODO: Create a C pointer "value" property */ + + return res; +} + /* Connect to a server */ static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) @@ -77,6 +91,10 @@ static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) return 0; } + /* Return a socket object */ + + ejsSetReturnValue(eid, mprTransport(transport)); + return 0; } -- cgit From aa7b6b3ea57b0e36b7b43ed2f312ee724e243541 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 10 Jun 2005 08:00:02 +0000 Subject: r7457: Return an ejs C pointer object from the ejs connect() function. Add a session_setup() function that does an anonymous session setup. Will add credential passing later. (This used to be commit 832332de5db7102085e7c44d4256fb199d8123eb) --- source4/scripting/ejs/smbcalls_cli.c | 78 ++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 16 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c index de31d7a900..bfc71948b9 100644 --- a/source4/scripting/ejs/smbcalls_cli.c +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -23,20 +23,8 @@ #include "includes.h" #include "lib/ejs/ejs.h" #include "librpc/gen_ndr/ndr_nbt.h" - -static struct MprVar mprTransport(struct smbcli_transport *transport) -{ - struct MprVar res, val; - - res = mprCreateObjVar("transport", MPR_DEFAULT_HASH_SIZE); - - val = mprCreateStringVar(talloc_get_name(transport), 1); - mprCreateProperty(&res, "name", &val); - - /* TODO: Create a C pointer "value" property */ - - return res; -} +#include "libcli/raw/libcliraw.h" +#include "libcli/composite/composite.h" /* Connect to a server */ @@ -93,7 +81,64 @@ static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) /* Return a socket object */ - ejsSetReturnValue(eid, mprTransport(transport)); + ejsSetReturnValue(eid, mprCreatePtrVar(transport, talloc_get_name(transport))); + + return 0; +} + +/* Perform a session setup: + + session_setup(conn, "DOMAIN\USERNAME%PASSWORD"); + session_setup(conn, USERNAME, PASSWORD); + session_setup(conn, DOMAIN, USERNAME, PASSWORD); + + */ + +static int ejs_cli_ssetup(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_transport *transport; + struct smbcli_session *session; + struct smb_composite_sesssetup setup; + struct cli_credentials *creds; + NTSTATUS status; + + /* Argument parsing */ + + if (argc < 1 || argc > 3) { + ejsSetErrorMsg(eid, "session_setup invalid arguments"); + return -1; + } + + if (argv[0]->type != MPR_TYPE_PTR) { + ejsSetErrorMsg(eid, "first arg is not a connect handle"); + return -1; + } + + transport = argv[0]->ptr; + + /* Do session setup */ + + session = smbcli_session_init(transport, transport, True); + if (!session) { + ejsSetErrorMsg(eid, "session init failed"); + return -1; + } + + creds = cli_credentials_init(session); + cli_credentials_set_anonymous(creds); + + setup.in.sesskey = transport->negotiate.sesskey; + setup.in.capabilities = transport->negotiate.capabilities; + setup.in.credentials = creds; + setup.in.workgroup = lp_workgroup(); + + status = smb_composite_sesssetup(session, &setup); + + session->vuid = setup.out.vuid; + + /* Return a session object */ + + ejsSetReturnValue(eid, mprCreatePtrVar(session, talloc_get_name(session))); return 0; } @@ -103,5 +148,6 @@ static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) */ void smb_setup_ejs_cli(void) { - ejsDefineStringCFunction(-1, "connect", ejs_cli_connect, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineStringCFunction(-1, "connect", ejs_cli_connect, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "session_setup", ejs_cli_ssetup, NULL, MPR_VAR_SCRIPT_HANDLE); } -- cgit From 6ef2a41aa1e6cfeb88ee621c63327593b51315ae Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 10 Jun 2005 12:43:11 +0000 Subject: r7461: this is the start of some code for mapping IDL onto ejs. This is hand written code, and it doesn't work or even compile yet. I am committing it to make it easier to discuss the approach with jelmer and tpot. The intention is that this code will eventually end up being mostly auto-generated (with the utility functions split out, just like librpc/ndr/*.c) (This used to be commit 30e876e9c2ba73a3bc26e7708110e00a8552a75e) --- source4/scripting/ejs/smbcalls_irpc.c | 197 ++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 source4/scripting/ejs/smbcalls_irpc.c (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls_irpc.c b/source4/scripting/ejs/smbcalls_irpc.c new file mode 100644 index 0000000000..9b0efc69e4 --- /dev/null +++ b/source4/scripting/ejs/smbcalls_irpc.c @@ -0,0 +1,197 @@ +/* + Unix SMB/CIFS implementation. + + provide hooks into IRPC calls from ejs scripts. + + Copyright (C) Andrew Tridgell 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ +/* + I hope that this code will eventually be autogenerated. + + + In this code, the convention is: + + ejs_pull_*() from MprVar -> C structure + ejs_push_*() from C structure -> MprVar + + note that for ejs calls, pull only ever needs to do + NDR_IN, and push only ever needs to do NDR_OUT. This is + because ejs code is (at least for the moment) only used for + the client, not the server + + also note that we don't need the NDR_SCALARS/NDR_BUFFERS stuff, as + we aren't dealing with wire marshalling where scalars and buffers + are separated +*/ + +#include "includes.h" +#include "lib/ejs/ejs.h" +#include "librpc/gen_ndr/ndr_irpc.h" + +struct ndr_ejs { + /* nothing here yet */ + int dummy; +}; + +struct enum_table { + uint32_t evalue; + const char *name; +}; + +#define EJS_ASSERT(condition) do { \ + if (!(condition)) { \ + DEBUG(0,("ejs assert failed at %s: %s\n", __location__, #condition)); \ + return NT_STATUS_INVALID_PARAMETER; \ + } \ +} while (0) + + +/* first some common helper functions */ + +static NTSTATUS ejs_pull_enum(struct ndr_ejs *ndr, + uint32_t *evalue, + struct MprVar *v, + const struct enum_table *etable) +{ + const char *s; + int i; + EJS_ASSERT(v->type == MPR_TYPE_STRING); + s = v->string; + for (i=0;etable[i].name;i++) { + if (strcmp(s, etable[i].name) == 0) { + *evalue = etable[i].evalue; + return NT_STATUS_OK; + } + } + return NT_STATUS_INVALID_PARAMETER; +} + +static NTSTATUS ejs_push_enum(struct ndr_ejs *ndr, + uint32_t evalue, + struct MprVar *v, + const struct enum_table *etable) +{ + int i; + for (i=0;etable[i].name;i++) { + if (evalue == etable[i].evalue) { + *v = mprCreateStringVar(etable[i].name, 0); + return NT_STATUS_OK; + } + } + return NT_STATUS_INVALID_PARAMETER; +} + +static NTSTATUS ejs_push_hyper(struct ndr_ejs *ndr, + uint64_t evalue, + struct MprVar *v, + const char *name) +{ +} + + +NTSTATUS ejs_element(struct ndr_ejs *ndr, + struct MprVar *v, + const char *name, + struct MprVar **e) +{ + *e = mprGetProperty(v, name, NULL); + if (*e == NULL) { + return NT_STATUS_INVALID_PARAMETER; + } + return NT_STATUS_OK; +} + + +/* when we auto-generate, the enum tables should also be used by + the ndr_print_*() functions for non-ejs handling of enums +*/ +static const struct enum_table enum_table_nbdt_info_level[] = { + {NBTD_INFO_STATISTICS, "NBTD_INFO_STATISTICS"}, + {-1, NULL} +}; + + + +/* pull-side functions for nbtd_information call */ + +static NTSTATUS ejs_pull_nbtd_info_level(struct ndr_ejs *ndr, + enum nbtd_info_level *r, + struct MprVar *v) +{ + uint32_t e; + NDR_CHECK(ejs_pull_enum(ndr, &e, v, enum_table_nbdt_info_level)); + *r = e; + return NT_STATUS_OK; +} + +static NTSTATUS ejs_pull_nbtd_information(struct ndr_ejs *ndr, + struct nbtd_information *r, + struct MprVar *v) +{ + struct MprVar *e; + NDR_CHECK(ejs_element(ndr, v, "level", &e)); + NDR_CHECK(ejs_pull_nbtd_info_level(ndr, &r->in.level, e)); + return NT_STATUS_OK; +} + + +/* push side functions for nbtd_information */ + +static NTSTATUS ejs_push_nbtd_info_level(struct ndr_ejs *ndr, enum nbtd_info_level r, + struct MprVar *v) +{ + NDR_CHECK(ejs_push_enum(ndr, r, v, enum_table_nbdt_info_level)); + return NT_STATUS_OK; +} + +static NTSTATUS ejs_push_nbtd_statistics(struct ndr_ejs *ndr, struct nbtd_statistics *r, + struct MprVar *v) +{ + NDR_CHECK(ejs_push_hyper(ndr, r->total_received, v, "total_received")); + NDR_CHECK(ejs_push_hyper(ndr, r->total_sent, v, "total_sent")); + NDR_CHECK(ejs_push_hyper(ndr, r->query_count, v, "query_count")); + NDR_CHECK(ejs_push_hyper(ndr, r->register_count, v, "register_count")); + NDR_CHECK(ejs_push_hyper(ndr, r->release_count, v, "release_count")); + NDR_CHECK(ejs_push_hyper(ndr, r->refresh_count, v, "refresh_count")); + return NT_STATUS_OK; +} + +static NTSTATUS ejs_push_nbtd_info(struct ndr_ejs *ndr, union nbtd_info *r, + struct MprVar *v) +{ + int level; + level = ejs_push_get_switch_value(ndr, r); + switch (level) { + case NBTD_INFO_STATISTICS: + NDR_CHECK(ejs_push_object(ndr, r->stats, + (ejs_push_fn_t)ejs_push_nbtd_statistics, + v, "stats")); + break; + default: + return ejs_push_error(ndr, NDR_ERR_BAD_SWITCH, "Bad switch value %u", level); + } + return NT_STATUS_OK; +} + +static NTSTATUS ndr_push_nbtd_information(struct ndr_push *ndr, + struct nbtd_information *r, struct MprVar *v) +{ + NDR_CHECK(ejs_push_set_switch_value(ndr, &r->out.info, r->in.level)); + NDR_CHECK(ejs_push_object(ndr, &r->out.info, + (ejs_push_fn_t)ejs_push_nbtd_info, v, "info")); + return NT_STATUS_OK; +} -- cgit From 16a5d7c1755ece7f93db0eba42844152c916c11d Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sat, 11 Jun 2005 02:39:14 +0000 Subject: r7477: Add MPR_TYPE_PTR to ejs_typeof(). (This used to be commit 8574f64ca2bda864f472067ae594285b6f5dc957) --- source4/scripting/ejs/smbcalls.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 46fce4b98e..1b5737db29 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -44,7 +44,8 @@ static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv) { MPR_TYPE_OBJECT, "object" }, { MPR_TYPE_FUNCTION, "function" }, { MPR_TYPE_STRING, "string" }, - { MPR_TYPE_STRING_CFUNCTION, "function" } + { MPR_TYPE_STRING_CFUNCTION, "function" }, + { MPR_TYPE_PTR, "C pointer" } }; int i; const char *type = NULL; -- cgit From 9e555f75e2b4982804f8effb4f2b4da689d714e8 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 12 Jun 2005 06:37:25 +0000 Subject: r7500: Initialise module subsystems. (This used to be commit 564dfe14d00e80a0d373ab0fc17803ffaac0892e) --- source4/scripting/ejs/smbscript.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 02a3aff28e..68d94f6d2a 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -57,6 +57,8 @@ void ejs_exception(const char *reason) load_interfaces(); + smbscript_init_subsystems; + mprSetCtx(mem_ctx); if (ejsOpen(NULL, NULL, NULL) != 0) { -- cgit From ae23fe35e28c38c3ce1c523c49ea03a4045c2ca2 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 12 Jun 2005 06:40:17 +0000 Subject: r7501: Fix credential initialisation in ejs session setup. Implement four arg and anonymous version of command. Implement ejs tconx. (This used to be commit 3b7df1037de813d93b284d3b4438b083e668e29f) --- source4/scripting/ejs/smbcalls_cli.c | 134 +++++++++++++++++++++++++++++++++-- 1 file changed, 127 insertions(+), 7 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c index bfc71948b9..02459abd4b 100644 --- a/source4/scripting/ejs/smbcalls_cli.c +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -81,7 +81,8 @@ static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) /* Return a socket object */ - ejsSetReturnValue(eid, mprCreatePtrVar(transport, talloc_get_name(transport))); + ejsSetReturnValue(eid, mprCreatePtrVar(transport, + talloc_get_name(transport))); return 0; } @@ -91,6 +92,7 @@ static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) session_setup(conn, "DOMAIN\USERNAME%PASSWORD"); session_setup(conn, USERNAME, PASSWORD); session_setup(conn, DOMAIN, USERNAME, PASSWORD); + session_setup(conn); // anonymous */ @@ -101,32 +103,68 @@ static int ejs_cli_ssetup(MprVarHandle eid, int argc, MprVar **argv) struct smb_composite_sesssetup setup; struct cli_credentials *creds; NTSTATUS status; + int result = -1; /* Argument parsing */ - if (argc < 1 || argc > 3) { + if (argc < 1 || argc > 4) { ejsSetErrorMsg(eid, "session_setup invalid arguments"); return -1; } - if (argv[0]->type != MPR_TYPE_PTR) { + if (!mprVarIsPtr(argv[0]->type)) { ejsSetErrorMsg(eid, "first arg is not a connect handle"); return -1; } transport = argv[0]->ptr; + creds = cli_credentials_init(transport); + cli_credentials_set_conf(creds); + + if (argc == 4) { + + /* DOMAIN, USERNAME, PASSWORD form */ + + if (!mprVarIsString(argv[1]->type)) { + ejsSetErrorMsg(eid, "arg 1 must be a string"); + goto done; + } + + cli_credentials_set_domain(creds, argv[1]->string, + CRED_SPECIFIED); + + if (!mprVarIsString(argv[2]->type)) { + ejsSetErrorMsg(eid, "arg 2 must be a string"); + goto done; + } + + cli_credentials_set_username(creds, argv[2]->string, + CRED_SPECIFIED); + + if (!mprVarIsString(argv[3]->type)) { + ejsSetErrorMsg(eid, "arg 3 must be a string"); + goto done; + } + + cli_credentials_set_password(creds, argv[3]->string, + CRED_SPECIFIED); + + } else { + + /* Anonymous connection */ + + cli_credentials_set_anonymous(creds); + } /* Do session setup */ session = smbcli_session_init(transport, transport, True); + if (!session) { ejsSetErrorMsg(eid, "session init failed"); return -1; } - creds = cli_credentials_init(session); - cli_credentials_set_anonymous(creds); - setup.in.sesskey = transport->negotiate.sesskey; setup.in.capabilities = transport->negotiate.capabilities; setup.in.credentials = creds; @@ -134,11 +172,92 @@ static int ejs_cli_ssetup(MprVarHandle eid, int argc, MprVar **argv) status = smb_composite_sesssetup(session, &setup); + if (!NT_STATUS_IS_OK(status)) { + ejsSetErrorMsg(eid, "session setup: %s", nt_errstr(status)); + return -1; + } + session->vuid = setup.out.vuid; /* Return a session object */ - ejsSetReturnValue(eid, mprCreatePtrVar(session, talloc_get_name(session))); + ejsSetReturnValue(eid, mprCreatePtrVar(session, + talloc_get_name(session))); + + result = 0; + + done: + talloc_free(creds); + return result; +} + +/* Perform a tree connect + + session_setup(session, SHARE); + + */ + +static int ejs_cli_tcon(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_session *session; + struct smbcli_tree *tree; + union smb_tcon tcon; + TALLOC_CTX *mem_ctx; + NTSTATUS status; + char *password = ""; + + /* Argument parsing */ + + if (argc != 2) { + ejsSetErrorMsg(eid, "tree_connect invalid arguments"); + return -1; + } + + if (!mprVarIsPtr(argv[0]->type)) { + ejsSetErrorMsg(eid, "first arg is not a session handle"); + return -1; + } + + session = argv[0]->ptr; + tree = smbcli_tree_init(session, session, True); + + if (!tree) { + ejsSetErrorMsg(eid, "tree init failed"); + return -1; + } + + mem_ctx = talloc_init("tcon"); + if (!mem_ctx) { + ejsSetErrorMsg(eid, "talloc_init failed"); + return -1; + } + + /* Do tree connect */ + + tcon.generic.level = RAW_TCON_TCONX; + tcon.tconx.in.flags = 0; + + if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) { + tcon.tconx.in.password = data_blob(NULL, 0); + } else if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) { + tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24); + if (session->transport->negotiate.secblob.length < 8) { + ejsSetErrorMsg(eid, "invalid security blob"); + return -1; + } + SMBencrypt(password, session->transport->negotiate.secblob.data, tcon.tconx.in.password.data); + } else { + tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1); + } + + tcon.tconx.in.path = argv[1]->string; + tcon.tconx.in.device = "?????"; + + status = smb_tree_connect(tree, mem_ctx, &tcon); + + tree->tid = tcon.tconx.out.tid; + + talloc_free(mem_ctx); return 0; } @@ -150,4 +269,5 @@ void smb_setup_ejs_cli(void) { ejsDefineStringCFunction(-1, "connect", ejs_cli_connect, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "session_setup", ejs_cli_ssetup, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "tree_connect", ejs_cli_tcon, NULL, MPR_VAR_SCRIPT_HANDLE); } -- cgit From fb3874bfacb7fb7199e415f8ad16598790ec9c36 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 12 Jun 2005 07:03:32 +0000 Subject: r7505: Add more argument forms for session_setup(). Throw an exception if tree connect fails. (This used to be commit 5b67f2c3d91487fec38b300b4f71792cd9164a78) --- source4/scripting/ejs/smbcalls_cli.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c index 02459abd4b..b32917515d 100644 --- a/source4/scripting/ejs/smbcalls_cli.c +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -89,7 +89,7 @@ static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) /* Perform a session setup: - session_setup(conn, "DOMAIN\USERNAME%PASSWORD"); + session_setup(conn, "DOMAIN\\USERNAME%PASSWORD"); session_setup(conn, USERNAME, PASSWORD); session_setup(conn, DOMAIN, USERNAME, PASSWORD); session_setup(conn); // anonymous @@ -149,6 +149,34 @@ static int ejs_cli_ssetup(MprVarHandle eid, int argc, MprVar **argv) cli_credentials_set_password(creds, argv[3]->string, CRED_SPECIFIED); + } else if (argc == 3) { + + /* USERNAME, PASSWORD form */ + + if (!mprVarIsString(argv[1]->type)) { + ejsSetErrorMsg(eid, "arg1 must be a string"); + goto done; + } + + cli_credentials_set_username(creds, argv[1]->string, + CRED_SPECIFIED); + + if (!mprVarIsString(argv[2]->type)) { + + ejsSetErrorMsg(eid, "arg2 must be a string"); + goto done; + } + + cli_credentials_set_password(creds, argv[2]->string, + CRED_SPECIFIED); + + } else if (argc == 2) { + + /* DOMAIN/USERNAME%PASSWORD form */ + + cli_credentials_parse_string(creds, argv[1]->string, + CRED_SPECIFIED); + } else { /* Anonymous connection */ @@ -255,6 +283,11 @@ static int ejs_cli_tcon(MprVarHandle eid, int argc, MprVar **argv) status = smb_tree_connect(tree, mem_ctx, &tcon); + if (!NT_STATUS_IS_OK(status)) { + ejsSetErrorMsg(eid, "session setup: %s", nt_errstr(status)); + return -1; + } + tree->tid = tcon.tconx.out.tid; talloc_free(mem_ctx); -- cgit From 00e2b7c1b49b128488cf977b40b086b935fb605a Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 13 Jun 2005 10:01:11 +0000 Subject: r7530: Simply calling convention of lp_load(). This always loads all the services, as we now don't have an easy way to split out smbd. Andrew Bartlett (This used to be commit 990e061939c76b559c4f5914c5fc6ca1b13e19dd) --- source4/scripting/ejs/smbscript.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 68d94f6d2a..2795e4f127 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -49,7 +49,7 @@ void ejs_exception(const char *reason) setup_logging(argv[0],DEBUG_STDOUT); - if (!lp_load(dyn_CONFIGFILE, False, False, False)) { + if (!lp_load(dyn_CONFIGFILE)) { fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n", argv[0], dyn_CONFIGFILE); exit(127); -- cgit From 487d1afe3021844f96ed19861a03ea1958676f67 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Tue, 14 Jun 2005 07:14:59 +0000 Subject: r7570: Add tree, session and socket disconnect ejs functions. (This used to be commit 63577628b0ee7e6b33053484190189e99cb56a5b) --- source4/scripting/ejs/smbcalls_cli.c | 122 ++++++++++++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 8 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c index b32917515d..fa147ca299 100644 --- a/source4/scripting/ejs/smbcalls_cli.c +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -54,7 +54,7 @@ static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) return -1; } - transport = smbcli_transport_init(sock, sock, True); + transport = smbcli_transport_init(sock, sock, False); if (!transport) { ejsSetErrorMsg(eid, "transport init failed"); @@ -186,7 +186,7 @@ static int ejs_cli_ssetup(MprVarHandle eid, int argc, MprVar **argv) /* Do session setup */ - session = smbcli_session_init(transport, transport, True); + session = smbcli_session_init(transport, transport, False); if (!session) { ejsSetErrorMsg(eid, "session init failed"); @@ -201,7 +201,7 @@ static int ejs_cli_ssetup(MprVarHandle eid, int argc, MprVar **argv) status = smb_composite_sesssetup(session, &setup); if (!NT_STATUS_IS_OK(status)) { - ejsSetErrorMsg(eid, "session setup: %s", nt_errstr(status)); + ejsSetErrorMsg(eid, "session_setup: %s", nt_errstr(status)); return -1; } @@ -221,11 +221,11 @@ static int ejs_cli_ssetup(MprVarHandle eid, int argc, MprVar **argv) /* Perform a tree connect - session_setup(session, SHARE); + tree_connect(session, SHARE); */ -static int ejs_cli_tcon(MprVarHandle eid, int argc, MprVar **argv) +static int ejs_cli_tree_connect(MprVarHandle eid, int argc, MprVar **argv) { struct smbcli_session *session; struct smbcli_tree *tree; @@ -247,7 +247,7 @@ static int ejs_cli_tcon(MprVarHandle eid, int argc, MprVar **argv) } session = argv[0]->ptr; - tree = smbcli_tree_init(session, session, True); + tree = smbcli_tree_init(session, session, False); if (!tree) { ejsSetErrorMsg(eid, "tree init failed"); @@ -284,7 +284,7 @@ static int ejs_cli_tcon(MprVarHandle eid, int argc, MprVar **argv) status = smb_tree_connect(tree, mem_ctx, &tcon); if (!NT_STATUS_IS_OK(status)) { - ejsSetErrorMsg(eid, "session setup: %s", nt_errstr(status)); + ejsSetErrorMsg(eid, "tree_connect: %s", nt_errstr(status)); return -1; } @@ -292,6 +292,109 @@ static int ejs_cli_tcon(MprVarHandle eid, int argc, MprVar **argv) talloc_free(mem_ctx); + ejsSetReturnValue(eid, mprCreatePtrVar(tree, + talloc_get_name(tree))); + + return 0; +} + +/* Perform a tree disconnect + + tree_disconnect(tree); + + */ +static int ejs_cli_tree_disconnect(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_tree *tree; + NTSTATUS status; + + /* Argument parsing */ + + if (argc != 1) { + ejsSetErrorMsg(eid, "tree_disconnect invalid arguments"); + return -1; + } + + if (!mprVarIsPtr(argv[0]->type)) { + ejsSetErrorMsg(eid, "first arg is not a tree handle"); + return -1; + } + + tree = argv[0]->ptr; + + status = smb_tree_disconnect(tree); + + if (!NT_STATUS_IS_OK(status)) { + ejsSetErrorMsg(eid, "tree_disconnect: %s", nt_errstr(status)); + return -1; + } + + talloc_free(tree); + + return 0; +} + +/* Perform a ulogoff + + session_logoff(session); + + */ +static int ejs_cli_session_logoff(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_session *session; + NTSTATUS status; + + /* Argument parsing */ + + if (argc != 1) { + ejsSetErrorMsg(eid, "session_logoff invalid arguments"); + return -1; + } + + if (!mprVarIsPtr(argv[0]->type)) { + ejsSetErrorMsg(eid, "first arg is not a session handle"); + return -1; + } + + session = argv[0]->ptr; + + status = smb_raw_ulogoff(session); + + if (!NT_STATUS_IS_OK(status)) { + ejsSetErrorMsg(eid, "session_logoff: %s", nt_errstr(status)); + return -1; + } + + talloc_free(session); + + return 0; +} + +/* Perform a connection close + + disconnect(conn); + + */ +static int ejs_cli_disconnect(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_sock *sock; + + /* Argument parsing */ + + if (argc != 1) { + ejsSetErrorMsg(eid, "disconnect invalid arguments"); + return -1; + } + + if (!mprVarIsPtr(argv[0]->type)) { + ejsSetErrorMsg(eid, "first arg is not a connect handle"); + return -1; + } + + sock = argv[0]->ptr; + + talloc_free(sock); + return 0; } @@ -302,5 +405,8 @@ void smb_setup_ejs_cli(void) { ejsDefineStringCFunction(-1, "connect", ejs_cli_connect, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "session_setup", ejs_cli_ssetup, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "tree_connect", ejs_cli_tcon, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "tree_connect", ejs_cli_tree_connect, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "tree_disconnect", ejs_cli_tree_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "session_logoff", ejs_cli_session_logoff, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "disconnect", ejs_cli_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE); } -- cgit From af237084ecd4f9928c6c282b9c5c73598d5c73d6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 16 Jun 2005 11:36:09 +0000 Subject: r7633: this patch started as an attempt to make the dcerpc code use a given event_context for the socket_connect() call, so that when things that use dcerpc are running alongside anything else it doesn't block the whole process during a connect. Then of course I needed to change any code that created a dcerpc connection (such as the auth code) to also take an event context, and anything that called that and so on .... thus the size of the patch. There were 3 places where I punted: - abartlet wanted me to add a gensec_set_event_context() call instead of adding it to the gensec init calls. Andrew, my apologies for not doing this. I didn't do it as adding a new parameter allowed me to catch all the callers with the compiler. Now that its done, we could go back and use gensec_set_event_context() - the ejs code calls auth initialisation, which means it should pass in the event context from the web server. I punted on that. Needs fixing. - I used a NULL event context in dcom_get_pipe(). This is equivalent to what we did already, but should be fixed to use a callers event context. Jelmer, can you think of a clean way to do that? I also cleaned up a couple of things: - libnet_context_destroy() makes no sense. I removed it. - removed some unused vars in various places (This used to be commit 3a3025485bdb8f600ab528c0b4b4eef0c65e3fc9) --- source4/scripting/ejs/smbcalls.c | 5 ++++- source4/scripting/ejs/smbcalls_cli.c | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 1b5737db29..6444ec63cc 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -81,7 +81,10 @@ static int ejs_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char * NTSTATUS nt_status; DATA_BLOB pw_blob; - nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context); + /* + darn, we need some way to get the right event_context here + */ + nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context, NULL); if (!NT_STATUS_IS_OK(nt_status)) { mprSetPropertyValue(auth, "result", mprCreateBoolVar(False)); mprSetPropertyValue(auth, "report", mprCreateStringVar("Auth System Failure", 1)); diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c index fa147ca299..f144fd61e5 100644 --- a/source4/scripting/ejs/smbcalls_cli.c +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -232,7 +232,7 @@ static int ejs_cli_tree_connect(MprVarHandle eid, int argc, MprVar **argv) union smb_tcon tcon; TALLOC_CTX *mem_ctx; NTSTATUS status; - char *password = ""; + const char *password = ""; /* Argument parsing */ -- cgit From 864de2a4ab9ed2cf2f8850516a021973941af87a Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 26 Jun 2005 05:18:50 +0000 Subject: r7922: Comment out complicated connect/session/tree API for the moment. Replace with tree_connect() and tree_disconnect() functions. (This used to be commit 9ed763f7fa9e6138d8c1da29c1e0adf2e18cb5e1) --- source4/scripting/ejs/smbcalls_cli.c | 85 +++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c index f144fd61e5..7a693fc180 100644 --- a/source4/scripting/ejs/smbcalls_cli.c +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -22,10 +22,13 @@ #include "includes.h" #include "lib/ejs/ejs.h" -#include "librpc/gen_ndr/ndr_nbt.h" #include "libcli/raw/libcliraw.h" #include "libcli/composite/composite.h" +#if 0 + +#include "librpc/gen_ndr/ndr_nbt.h" + /* Connect to a server */ static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) @@ -398,15 +401,93 @@ static int ejs_cli_disconnect(MprVarHandle eid, int argc, MprVar **argv) return 0; } +#endif + +/* Perform a tree connect: + + tree_handle = tree_connect("\\\\frogurt\\homes", "user%pass"); + */ + +static int ejs_tree_connect(MprVarHandle eid, int argc, char **argv) +{ + struct cli_credentials *creds; + struct smbcli_tree *tree; + const char *hostname, *sharename; + NTSTATUS result; + TALLOC_CTX *mem_ctx; + + if (argc != 2) { + ejsSetErrorMsg(eid, "tree_connect(): invalid number of args"); + return -1; + } + + /* Set up host, share destination */ + + mem_ctx = talloc_init(NULL); + smbcli_parse_unc(argv[0], mem_ctx, &hostname, &sharename); + + /* Set up credentials */ + + creds = cli_credentials_init(NULL); + cli_credentials_set_conf(creds); + cli_credentials_parse_string(creds, argv[1], CRED_SPECIFIED); + + /* Do connect */ + + result = smbcli_tree_full_connection(NULL, &tree, hostname, 0, + sharename, "?????", creds, NULL); + + talloc_free(mem_ctx); + + if (!NT_STATUS_IS_OK(result)) { + ejsSetReturnValue(eid, mprNTSTATUS(result)); + return 0; + } + + ejsSetReturnValue(eid, mprCreatePtrVar(tree, talloc_get_name(tree))); + + return 0; +} + +/* Perform a tree disconnect: + + tree_disconnect(tree_handle); + */ + +static int ejs_tree_disconnect(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_tree *tree; + NTSTATUS result; + + if (!mprVarIsPtr(argv[0]->type) || + !talloc_check_name(argv[0]->ptr, "struct smbcli_tree")) { + ejsSetErrorMsg(eid, "first arg is not a tree handle"); + return -1; + } + + tree = talloc_check_name(argv[0]->ptr, "struct smbcli_tree"); + + result = smb_tree_disconnect(tree); + + ejsSetReturnValue(eid, mprNTSTATUS(result)); + + return 0; +} + /* setup C functions that be called from ejs */ void smb_setup_ejs_cli(void) { + ejsDefineStringCFunction(-1, "tree_connect", ejs_tree_connect, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "tree_disconnect", ejs_tree_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE); + +#if 0 ejsDefineStringCFunction(-1, "connect", ejs_cli_connect, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "session_setup", ejs_cli_ssetup, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "tree_connect", ejs_cli_tree_connect, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "tree_disconnect", ejs_cli_tree_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "session_logoff", ejs_cli_session_logoff, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "disconnect", ejs_cli_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "disconnect", ejs_cli_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE); +#endif } -- cgit From ae73a40ad4c09a93abe00ee99ea868b0196d3449 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 26 Jun 2005 05:43:16 +0000 Subject: r7924: Add mkdir() and rmdir() functions. Write a macro to check tree handle parameters. (This used to be commit 043feb131f1877886a9ab7e25ada5e54692f6487) --- source4/scripting/ejs/smbcalls_cli.c | 83 +++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c index 7a693fc180..46813f8f23 100644 --- a/source4/scripting/ejs/smbcalls_cli.c +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -449,6 +449,9 @@ static int ejs_tree_connect(MprVarHandle eid, int argc, char **argv) return 0; } +#define IS_TREE_HANDLE(x) (mprVarIsPtr((x)->type) && \ + talloc_check_name((x)->ptr, "struct smbcli_tree")) + /* Perform a tree disconnect: tree_disconnect(tree_handle); @@ -459,8 +462,13 @@ static int ejs_tree_disconnect(MprVarHandle eid, int argc, MprVar **argv) struct smbcli_tree *tree; NTSTATUS result; - if (!mprVarIsPtr(argv[0]->type) || - !talloc_check_name(argv[0]->ptr, "struct smbcli_tree")) { + if (argc != 1) { + ejsSetErrorMsg(eid, + "tree_disconnect(): invalid number of args"); + return -1; + } + + if (!IS_TREE_HANDLE(argv[0])) { ejsSetErrorMsg(eid, "first arg is not a tree handle"); return -1; } @@ -474,6 +482,74 @@ static int ejs_tree_disconnect(MprVarHandle eid, int argc, MprVar **argv) return 0; } +/* Perform a tree connect: + + result = mkdir(tree_handle, DIRNAME); + */ + +static int ejs_mkdir(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_tree *tree; + NTSTATUS result; + + if (argc != 2) { + ejsSetErrorMsg(eid, "mkdir(): invalid number of args"); + return -1; + } + + if (!IS_TREE_HANDLE(argv[0])) { + ejsSetErrorMsg(eid, "first arg is not a tree handle"); + return -1; + } + + tree = argv[0]->ptr; + + if (!mprVarIsString(argv[1]->type)) { + ejsSetErrorMsg(eid, "arg 2 must be a string"); + return -1; + } + + result = smbcli_mkdir(tree, argv[1]->string); + + ejsSetReturnValue(eid, mprNTSTATUS(result)); + + return 0; +} + +/* Perform a tree connect: + + result = rmdir(tree_handle, DIRNAME); + */ + +static int ejs_rmdir(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_tree *tree; + NTSTATUS result; + + if (argc != 2) { + ejsSetErrorMsg(eid, "rmdir(): invalid number of args"); + return -1; + } + + if (!IS_TREE_HANDLE(argv[0])) { + ejsSetErrorMsg(eid, "first arg is not a tree handle"); + return -1; + } + + tree = argv[0]->ptr; + + if (!mprVarIsString(argv[1]->type)) { + ejsSetErrorMsg(eid, "arg 2 must be a string"); + return -1; + } + + result = smbcli_rmdir(tree, argv[1]->string); + + ejsSetReturnValue(eid, mprNTSTATUS(result)); + + return 0; +} + /* setup C functions that be called from ejs */ @@ -482,6 +558,9 @@ void smb_setup_ejs_cli(void) ejsDefineStringCFunction(-1, "tree_connect", ejs_tree_connect, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "tree_disconnect", ejs_tree_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "mkdir", ejs_mkdir, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "rmdir", ejs_rmdir, NULL, MPR_VAR_SCRIPT_HANDLE); + #if 0 ejsDefineStringCFunction(-1, "connect", ejs_cli_connect, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "session_setup", ejs_cli_ssetup, NULL, MPR_VAR_SCRIPT_HANDLE); -- cgit From c36e1ae46c266c9a6f012e5715a8df69faad04cd Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 26 Jun 2005 07:34:55 +0000 Subject: r7928: Add rename, unlink and list commands. list() returns a list of strings, but maybe it should be a list of objects with size, attrib, short name etc. (This used to be commit 696aa182d5a159c26b80829e1eae9a9894cb7986) --- source4/scripting/ejs/smbcalls_cli.c | 141 ++++++++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 2 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c index 46813f8f23..ca8fbd3ea2 100644 --- a/source4/scripting/ejs/smbcalls_cli.c +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -24,6 +24,7 @@ #include "lib/ejs/ejs.h" #include "libcli/raw/libcliraw.h" #include "libcli/composite/composite.h" +#include "clilist.h" #if 0 @@ -482,7 +483,7 @@ static int ejs_tree_disconnect(MprVarHandle eid, int argc, MprVar **argv) return 0; } -/* Perform a tree connect: +/* Create a directory: result = mkdir(tree_handle, DIRNAME); */ @@ -516,7 +517,7 @@ static int ejs_mkdir(MprVarHandle eid, int argc, MprVar **argv) return 0; } -/* Perform a tree connect: +/* Remove a directory: result = rmdir(tree_handle, DIRNAME); */ @@ -550,6 +551,138 @@ static int ejs_rmdir(MprVarHandle eid, int argc, MprVar **argv) return 0; } +/* Rename a file or directory: + + result = rename(tree_handle, SRCFILE, DESTFILE); + */ + +static int ejs_rename(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_tree *tree; + NTSTATUS result; + + if (argc != 3) { + ejsSetErrorMsg(eid, "rename(): invalid number of args"); + return -1; + } + + if (!IS_TREE_HANDLE(argv[0])) { + ejsSetErrorMsg(eid, "first arg is not a tree handle"); + return -1; + } + + tree = argv[0]->ptr; + + if (!mprVarIsString(argv[1]->type)) { + ejsSetErrorMsg(eid, "arg 2 must be a string"); + return -1; + } + + if (!mprVarIsString(argv[2]->type)) { + ejsSetErrorMsg(eid, "arg 3 must be a string"); + return -1; + } + + result = smbcli_rename(tree, argv[1]->string, argv[2]->string); + + ejsSetReturnValue(eid, mprNTSTATUS(result)); + + return 0; +} + +/* Unlink a file or directory: + + result = unlink(tree_handle, FILENAME); + */ + +static int ejs_unlink(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_tree *tree; + NTSTATUS result; + + if (argc != 2) { + ejsSetErrorMsg(eid, "unlink(): invalid number of args"); + return -1; + } + + if (!IS_TREE_HANDLE(argv[0])) { + ejsSetErrorMsg(eid, "first arg is not a tree handle"); + return -1; + } + + tree = argv[0]->ptr; + + if (!mprVarIsString(argv[1]->type)) { + ejsSetErrorMsg(eid, "arg 2 must be a string"); + return -1; + } + + result = smbcli_unlink(tree, argv[1]->string); + + ejsSetReturnValue(eid, mprNTSTATUS(result)); + + return 0; +} + +/* List directory contents + + result = list(tree_handle, ARG1, ...); + */ + +static void ejs_list_helper(struct clilist_file_info *info, const char *mask, + void *state) + +{ + MprVar *result = (MprVar *)state, value; + char idx[16]; + + mprItoa(result->properties->numDataItems, idx, sizeof(idx)); + value = mprCreateStringVar(info->name, 1); + mprCreateProperty(result, idx, &value); +} + +static int ejs_list(MprVarHandle eid, int argc, MprVar **argv) +{ + struct smbcli_tree *tree; + char *mask; + uint16_t attribute; + MprVar result; + + if (argc != 3) { + ejsSetErrorMsg(eid, "list(): invalid number of args"); + return -1; + } + + if (!IS_TREE_HANDLE(argv[0])) { + ejsSetErrorMsg(eid, "first arg is not a tree handle"); + return -1; + } + + tree = argv[0]->ptr; + + if (!mprVarIsString(argv[1]->type)) { + ejsSetErrorMsg(eid, "arg 2 must be a string"); + return -1; + } + + mask = argv[1]->string; + + if (!mprVarIsNumber(argv[2]->type)) { + ejsSetErrorMsg(eid, "arg 3 must be a number"); + return -1; + } + + attribute = mprVarToInteger(argv[2]); + + result = mprCreateObjVar("list", MPR_DEFAULT_HASH_SIZE); + + smbcli_list(tree, mask, attribute, ejs_list_helper, &result); + + ejsSetReturnValue(eid, result); + + return 0; +} + /* setup C functions that be called from ejs */ @@ -560,6 +693,10 @@ void smb_setup_ejs_cli(void) ejsDefineCFunction(-1, "mkdir", ejs_mkdir, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineCFunction(-1, "rmdir", ejs_rmdir, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "rename", ejs_rename, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "unlink", ejs_unlink, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "list", ejs_list, NULL, MPR_VAR_SCRIPT_HANDLE); + #if 0 ejsDefineStringCFunction(-1, "connect", ejs_cli_connect, NULL, MPR_VAR_SCRIPT_HANDLE); -- cgit From 643e5d8239ba105a5ac99ecc513289a17402714b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 2 Jul 2005 05:21:17 +0000 Subject: r8069: the beginnings of code to allow rpc calls to be made from ejs tpot, note that this shows how you can modify passed in MprVar variables in C call (This used to be commit a782541db3de6ca3b599a220265cf9e6cb0c4d7b) --- source4/scripting/ejs/config.mk | 23 +++++++ source4/scripting/ejs/mprutil.c | 26 ++++++++ source4/scripting/ejs/smbcalls.c | 1 + source4/scripting/ejs/smbcalls_rpc.c | 116 +++++++++++++++++++++++++++++++++++ source4/scripting/ejs/smbscript.c | 43 ++++++++----- 5 files changed, 194 insertions(+), 15 deletions(-) create mode 100644 source4/scripting/ejs/config.mk create mode 100644 source4/scripting/ejs/smbcalls_rpc.c (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/config.mk b/source4/scripting/ejs/config.mk new file mode 100644 index 0000000000..d5003d07e7 --- /dev/null +++ b/source4/scripting/ejs/config.mk @@ -0,0 +1,23 @@ +####################### +# Start LIBRARY SMBCALLS +[SUBSYSTEM::SMBCALLS] +OBJ_FILES = \ + scripting/ejs/smbcalls.o \ + scripting/ejs/smbcalls_config.o \ + scripting/ejs/smbcalls_ldb.o \ + scripting/ejs/smbcalls_nbt.o \ + scripting/ejs/smbcalls_cli.o \ + scripting/ejs/smbcalls_rpc.o \ + scripting/ejs/mprutil.o +REQUIRED_SUBSYSTEMS = AUTH EJS LIBBASIC RPC_NDR_ECHO +# End SUBSYSTEM SMBCALLS +####################### + +####################### +# Start BINARY SMBSCRIPT +[BINARY::smbscript] +OBJ_FILES = \ + scripting/ejs/smbscript.o +REQUIRED_SUBSYSTEMS = EJS LIBBASIC SMBCALLS CONFIG LIBSMB RPC LIBCMDLINE +# End BINARY SMBSCRIPT +####################### diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c index 2a1036d9a4..cd5ec3b5cd 100644 --- a/source4/scripting/ejs/mprutil.c +++ b/source4/scripting/ejs/mprutil.c @@ -213,3 +213,29 @@ struct MprVar mprWERROR(WERROR status) return res; } + + +/* + set a pointer in a existing MprVar +*/ +void mprSetPtr(struct MprVar *v, const char *propname, void *p) +{ + struct MprVar val = mprCreatePtrVar(p, talloc_get_name(p)); + mprCreateProperty(v, propname, &val); +} + +/* + get a pointer from a MprVar +*/ +void *mprGetPtr(struct MprVar *v, const char *propname) +{ + struct MprVar *val; + val = mprGetProperty(v, propname, NULL); + if (val == NULL) { + return NULL; + } + if (val->type != MPR_TYPE_PTR) { + return NULL; + } + return val->ptr; +} diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 6444ec63cc..041bd59f1a 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -192,6 +192,7 @@ void smb_setup_ejs_functions(void) smb_setup_ejs_ldb(); smb_setup_ejs_nbt(); smb_setup_ejs_cli(); + smb_setup_ejs_rpc(); ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE); diff --git a/source4/scripting/ejs/smbcalls_rpc.c b/source4/scripting/ejs/smbcalls_rpc.c new file mode 100644 index 0000000000..22aa2b59f8 --- /dev/null +++ b/source4/scripting/ejs/smbcalls_rpc.c @@ -0,0 +1,116 @@ +/* + Unix SMB/CIFS implementation. + + provide interfaces to rpc calls from ejs scripts + + Copyright (C) Andrew Tridgell 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "lib/ejs/ejs.h" +#include "librpc/gen_ndr/ndr_echo.h" +#include "lib/cmdline/popt_common.h" + +/* + connect to an rpc server + example: + var conn = new Object(); + status = rpc_connect(conn, "ncacn_ip_tcp:localhost", "rpcecho"); +*/ +static int ejs_rpc_connect(MprVarHandle eid, int argc, struct MprVar **argv) +{ + const char *binding, *pipe_name; + const struct dcerpc_interface_table *iface; + NTSTATUS status; + struct dcerpc_pipe *p; + struct MprVar *conn; + + /* validate arguments */ + if (argc != 3 || + argv[0]->type != MPR_TYPE_OBJECT || + argv[1]->type != MPR_TYPE_STRING || + argv[2]->type != MPR_TYPE_STRING) { + ejsSetErrorMsg(eid, "rpc_connect invalid arguments"); + return -1; + } + + conn = argv[0]; + binding = mprToString(argv[1]); + pipe_name = mprToString(argv[2]); + + iface = idl_iface_by_name(pipe_name); + if (iface == NULL) { + status = NT_STATUS_OBJECT_NAME_INVALID; + goto done; + } + + status = dcerpc_pipe_connect(mprMemCtx(), &p, binding, + iface->uuid, iface->if_version, + cmdline_credentials, NULL); + if (NT_STATUS_IS_OK(status)) { + mprSetPtr(conn, "pipe", p); + } + +done: + ejsSetReturnValue(eid, mprNTSTATUS(status)); + return 0; +} + + +/* + make an rpc call + example: + status = rpc_call(conn, "echo_AddOne", io); +*/ +static int ejs_rpc_call(MprVarHandle eid, int argc, struct MprVar **argv) +{ + struct dcerpc_pipe *p; + struct MprVar *conn, *io; + const char *call; + NTSTATUS status; + + if (argc != 3 || + argv[0]->type != MPR_TYPE_OBJECT || + argv[1]->type != MPR_TYPE_STRING || + argv[2]->type != MPR_TYPE_OBJECT) { + ejsSetErrorMsg(eid, "rpc_call invalid arguments"); + return -1; + } + + conn = argv[0]; + call = mprToString(argv[1]); + io = argv[2]; + + p = mprGetPtr(conn, "pipe"); + if (p == NULL) { + ejsSetErrorMsg(eid, "rpc_call invalid pipe"); + return -1; + } + + status = NT_STATUS_NOT_IMPLEMENTED; + ejsSetReturnValue(eid, mprNTSTATUS(status)); + return 0; +} + +/* + setup C functions that be called from ejs +*/ +void smb_setup_ejs_rpc(void) +{ + ejsDefineCFunction(-1, "rpc_connect", ejs_rpc_connect, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "rpc_call", ejs_rpc_call, NULL, MPR_VAR_SCRIPT_HANDLE); +} diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index 2795e4f127..aa0fc42c48 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -4,6 +4,7 @@ Standalone client for ejs scripting. Copyright (C) Tim Potter 2005 + Copyright (C) Andrew Tridgell 2005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,6 +22,7 @@ */ #include "includes.h" +#include "lib/cmdline/popt_common.h" #include "dynconfig.h" #include "lib/ejs/ejs.h" @@ -30,7 +32,7 @@ void ejs_exception(const char *reason) exit(127); } - int main(int argc, const char *argv[]) + int main(int argc, const char **argv) { EjsId eid; EjsHandle handle = 0; @@ -39,28 +41,39 @@ void ejs_exception(const char *reason) size_t script_size; TALLOC_CTX *mem_ctx = talloc_new(NULL); const char **argv_list = NULL; + const char *fname; struct MprVar v, *return_var; int exit_status, i; + poptContext pc; + int opt; + struct poptOption long_options[] = { + POPT_AUTOHELP + POPT_COMMON_SAMBA + POPT_COMMON_CREDENTIALS + POPT_COMMON_VERSION + POPT_TABLEEND + }; - if (argc < 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); - exit(127); - } + popt_common_dont_ask(); - setup_logging(argv[0],DEBUG_STDOUT); + pc = poptGetContext("smbscript", argc, argv, long_options, 0); - if (!lp_load(dyn_CONFIGFILE)) { - fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n", - argv[0], dyn_CONFIGFILE); - exit(127); - } + poptSetOtherOptionHelp(pc, "