diff options
author | Andrew Tridgell <tridge@samba.org> | 2005-07-12 02:34:49 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 13:20:07 -0500 |
commit | b1e93b296ea8f62ce0f15ccef5f6ae5339f4929f (patch) | |
tree | 2ba5c18f14d374a29a79061ee17b7dff28186355 | |
parent | 36bb0ffd4c59b17c7f942d874e18865707d3c696 (diff) | |
download | samba-b1e93b296ea8f62ce0f15ccef5f6ae5339f4929f.tar.gz samba-b1e93b296ea8f62ce0f15ccef5f6ae5339f4929f.tar.bz2 samba-b1e93b296ea8f62ce0f15ccef5f6ae5339f4929f.zip |
r8337: - use 64 bit access functions in ejs calls
- added access to smbd random functions
- fixed ordering in join()
- added sys_interfaces(), sys_hostname(), sys_nttime() and sys_ldaptime()
(This used to be commit 28c1a1f3c0cd2f8228fd8c3c695ab6f45226fa3f)
-rw-r--r-- | source4/scripting/ejs/config.mk | 2 | ||||
-rw-r--r-- | source4/scripting/ejs/mprutil.c | 38 | ||||
-rw-r--r-- | source4/scripting/ejs/smbcalls.c | 18 | ||||
-rw-r--r-- | source4/scripting/ejs/smbcalls_nbt.c | 2 | ||||
-rw-r--r-- | source4/scripting/ejs/smbcalls_nss.c | 2 | ||||
-rw-r--r-- | source4/scripting/ejs/smbcalls_options.c | 2 | ||||
-rw-r--r-- | source4/scripting/ejs/smbcalls_rand.c | 92 | ||||
-rw-r--r-- | source4/scripting/ejs/smbcalls_string.c | 2 | ||||
-rw-r--r-- | source4/scripting/ejs/smbcalls_sys.c | 95 |
9 files changed, 230 insertions, 23 deletions
diff --git a/source4/scripting/ejs/config.mk b/source4/scripting/ejs/config.mk index 5e061cb2a3..770bb67b86 100644 --- a/source4/scripting/ejs/config.mk +++ b/source4/scripting/ejs/config.mk @@ -22,6 +22,8 @@ OBJ_FILES = \ scripting/ejs/smbcalls_options.o \ scripting/ejs/smbcalls_nss.o \ scripting/ejs/smbcalls_string.o \ + scripting/ejs/smbcalls_rand.o \ + scripting/ejs/smbcalls_sys.o \ scripting/ejs/mprutil.o REQUIRED_SUBSYSTEMS = AUTH EJS LIBBASIC EJSRPC MESSAGING # End SUBSYSTEM SMBCALLS diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c index 4f799b2066..95571da245 100644 --- a/source4/scripting/ejs/mprutil.c +++ b/source4/scripting/ejs/mprutil.c @@ -211,7 +211,7 @@ struct MprVar mprLdbArray(struct ldb_message **msg, int count, const char *name) */ const char *mprToString(const struct MprVar *v) { - if (v->type != MPR_TYPE_STRING) return NULL; + if (!mprVarIsString(v->type)) return NULL; return v->string; } @@ -220,8 +220,8 @@ const char *mprToString(const struct MprVar *v) */ int mprToInt(const struct MprVar *v) { - if (v->type != MPR_TYPE_INT) return 0; - return v->integer; + if (!mprVarIsNumber(v->type)) return 0; + return mprVarToNumber(v); } /* @@ -249,6 +249,38 @@ const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v) return list; } + +/* + turn a MprVar object variable into a string list + this assumes the object variable is an array of strings +*/ +const char **mprToArray(TALLOC_CTX *mem_ctx, struct MprVar *v) +{ + const char **list = NULL; + struct MprVar *len; + int length, i; + + len = mprGetProperty(v, "length", NULL); + if (len == NULL) { + return NULL; + } + length = mprToInt(len); + + for (i=0;i<length;i++) { + char idx[16]; + struct MprVar *vs; + mprItoa(i, idx, sizeof(idx)); + vs = mprGetProperty(v, idx, NULL); + if (vs == NULL || vs->type != MPR_TYPE_STRING) { + talloc_free(list); + return NULL; + } + list = str_list_add(list, mprToString(vs)); + } + talloc_steal(mem_ctx, list); + return list; +} + /* turn a NTSTATUS into a MprVar object with lots of funky properties */ diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 2da1e8c669..e58151620f 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -65,21 +65,6 @@ static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv) } /* - return the list of configured network interfaces -*/ -static int ejs_IfaceList(MprVarHandle eid, int argc, struct MprVar **argv) -{ - int i, count = iface_count(); - struct MprVar ret = mprObject("interfaces"); - for (i=0;i<count;i++) { - mprAddArray(&ret, i, mprString(iface_n_ip(i))); - } - mpr_Return(eid, ret); - return 0; -} - - -/* libinclude() allows you to include js files using a search path specified in "js include =" in smb.conf. */ @@ -139,9 +124,10 @@ void smb_setup_ejs_functions(void) smb_setup_ejs_options(); smb_setup_ejs_nss(); smb_setup_ejs_string(); + smb_setup_ejs_random(); + smb_setup_ejs_system(); ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "IfaceList", ejs_IfaceList, NULL, MPR_VAR_SCRIPT_HANDLE); ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE); } diff --git a/source4/scripting/ejs/smbcalls_nbt.c b/source4/scripting/ejs/smbcalls_nbt.c index a4dc943f51..16c4e6aaee 100644 --- a/source4/scripting/ejs/smbcalls_nbt.c +++ b/source4/scripting/ejs/smbcalls_nbt.c @@ -60,7 +60,7 @@ static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv) if (argc == 2) { make_nbt_name_client(&name, mprToString(argv[1])); } else { - if (argv[1]->type != MPR_TYPE_INT) { + if (!mprVarIsNumber(argv[1]->type)) { ejsSetErrorMsg(eid, "resolveName invalid arguments"); goto done; } diff --git a/source4/scripting/ejs/smbcalls_nss.c b/source4/scripting/ejs/smbcalls_nss.c index abbf6cf10f..81ab02729a 100644 --- a/source4/scripting/ejs/smbcalls_nss.c +++ b/source4/scripting/ejs/smbcalls_nss.c @@ -93,7 +93,7 @@ static int ejs_getpwnam(MprVarHandle eid, int argc, struct MprVar **argv) static int ejs_getpwuid(MprVarHandle eid, int argc, struct MprVar **argv) { /* validate arguments */ - if (argc != 1 || argv[0]->type != MPR_TYPE_INT) { + if (argc != 1 || !mprVarIsNumber(argv[0]->type)) { ejsSetErrorMsg(eid, "getpwuid invalid arguments"); return -1; } diff --git a/source4/scripting/ejs/smbcalls_options.c b/source4/scripting/ejs/smbcalls_options.c index 335663acfe..df871cbb96 100644 --- a/source4/scripting/ejs/smbcalls_options.c +++ b/source4/scripting/ejs/smbcalls_options.c @@ -73,7 +73,7 @@ static int ejs_GetOptions(MprVarHandle eid, int argc, struct MprVar **argv) return -1; } - opt_argv = mprToList(tmp_ctx, argv[0]); + opt_argv = mprToArray(tmp_ctx, argv[0]); options = argv[1]; opt_argc = str_list_length(opt_argv); diff --git a/source4/scripting/ejs/smbcalls_rand.c b/source4/scripting/ejs/smbcalls_rand.c new file mode 100644 index 0000000000..d07c6ce233 --- /dev/null +++ b/source4/scripting/ejs/smbcalls_rand.c @@ -0,0 +1,92 @@ +/* + Unix SMB/CIFS implementation. + + provide access to randomisation functions + + 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 "system/passwd.h" + +/* + usage: + var i = random(); +*/ +static int ejs_random(MprVarHandle eid, int argc, struct MprVar **argv) +{ + mpr_Return(eid, mprCreateIntegerVar(generate_random())); + return 0; +} + +/* + usage: + var s = randpass(len); +*/ +static int ejs_randpass(MprVarHandle eid, int argc, struct MprVar **argv) +{ + char *s; + if (argc != 1 || !mprVarIsNumber(argv[0]->type)) { + ejsSetErrorMsg(eid, "randpass invalid arguments"); + return -1; + } + s = generate_random_str(mprMemCtx(), mprToInt(argv[0])); + mpr_Return(eid, mprString(s)); + talloc_free(s); + return 0; +} + +/* + usage: + var guid = randguid(); +*/ +static int ejs_randguid(MprVarHandle eid, int argc, struct MprVar **argv) +{ + struct GUID guid = GUID_random(); + char *s = GUID_string(mprMemCtx(), &guid); + mpr_Return(eid, mprString(s)); + talloc_free(s); + return 0; +} + +/* + usage: + var sid = randsid(); +*/ +static int ejs_randsid(MprVarHandle eid, int argc, struct MprVar **argv) +{ + char *s = talloc_asprintf(mprMemCtx(), "S-1-5-21-%8u-%8u-%8u", + (unsigned)generate_random(), + (unsigned)generate_random(), + (unsigned)generate_random()); + mpr_Return(eid, mprString(s)); + talloc_free(s); + return 0; +} + +/* + setup C functions that be called from ejs +*/ +void smb_setup_ejs_random(void) +{ + ejsDefineCFunction(-1, "random", ejs_random, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "randpass", ejs_randpass, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "randguid", ejs_randguid, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "randsid", ejs_randsid, NULL, MPR_VAR_SCRIPT_HANDLE); +} diff --git a/source4/scripting/ejs/smbcalls_string.c b/source4/scripting/ejs/smbcalls_string.c index b52a6d2182..b729f5dd3a 100644 --- a/source4/scripting/ejs/smbcalls_string.c +++ b/source4/scripting/ejs/smbcalls_string.c @@ -115,7 +115,7 @@ static int ejs_join(MprVarHandle eid, int argc, struct MprVar **argv) } separator = mprToString(argv[0]); - list = mprToList(tmp_ctx, argv[1]); + list = mprToArray(tmp_ctx, argv[1]); if (list == NULL || list[0] == NULL) { talloc_free(tmp_ctx); diff --git a/source4/scripting/ejs/smbcalls_sys.c b/source4/scripting/ejs/smbcalls_sys.c new file mode 100644 index 0000000000..a76690b597 --- /dev/null +++ b/source4/scripting/ejs/smbcalls_sys.c @@ -0,0 +1,95 @@ +/* + Unix SMB/CIFS implementation. + + provide access to system functions + + 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" + +/* + return the list of configured network interfaces +*/ +static int ejs_sys_interfaces(MprVarHandle eid, int argc, struct MprVar **argv) +{ + int i, count = iface_count(); + struct MprVar ret = mprObject("interfaces"); + for (i=0;i<count;i++) { + mprAddArray(&ret, i, mprString(iface_n_ip(i))); + } + mpr_Return(eid, ret); + return 0; +} + +/* + return the hostname from gethostname() +*/ +static int ejs_sys_hostname(MprVarHandle eid, int argc, struct MprVar **argv) +{ + char name[200]; + if (gethostname(name, sizeof(name)-1) == -1) { + ejsSetErrorMsg(eid, "gethostname failed - %s", strerror(errno)); + return -1; + } + mpr_Return(eid, mprString(name)); + return 0; +} + + +/* + return current time as a 64 bit nttime value +*/ +static int ejs_sys_nttime(MprVarHandle eid, int argc, struct MprVar **argv) +{ + struct timeval tv = timeval_current(); + struct MprVar v = mprCreateNumberVar(timeval_to_nttime(&tv)); + mpr_Return(eid, v); + return 0; +} + +/* + return a ldap time string from a nttime +*/ +static int ejs_sys_ldaptime(MprVarHandle eid, int argc, struct MprVar **argv) +{ + char *s; + time_t t; + if (argc != 1 || !mprVarIsNumber(argv[0]->type)) { + ejsSetErrorMsg(eid, "sys_ldaptime invalid arguments"); + return -1; + } + t = nt_time_to_unix(mprVarToNumber(argv[0])); + s = ldap_timestring(mprMemCtx(), t); + mpr_Return(eid, mprString(s)); + talloc_free(s); + return 0; +} + + +/* + setup C functions that be called from ejs +*/ +void smb_setup_ejs_system(void) +{ + ejsDefineCFunction(-1, "sys_interfaces", ejs_sys_interfaces, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "sys_hostname", ejs_sys_hostname, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "sys_nttime", ejs_sys_nttime, NULL, MPR_VAR_SCRIPT_HANDLE); + ejsDefineCFunction(-1, "sys_ldaptime", ejs_sys_ldaptime, NULL, MPR_VAR_SCRIPT_HANDLE); +} |