2 Unix SMB/CIFS implementation.
4 provide hooks into smbd C calls from ejs scripts
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Tim Potter 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "param/param.h"
25 #include "scripting/ejs/smbcalls.h"
29 return the type of a variable
31 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
37 { MPR_TYPE_UNDEFINED, "undefined" },
38 { MPR_TYPE_NULL, "object" },
39 { MPR_TYPE_BOOL, "boolean" },
40 { MPR_TYPE_CFUNCTION, "function" },
41 { MPR_TYPE_FLOAT, "number" },
42 { MPR_TYPE_INT, "number" },
43 { MPR_TYPE_INT64, "number" },
44 { MPR_TYPE_OBJECT, "object" },
45 { MPR_TYPE_FUNCTION, "function" },
46 { MPR_TYPE_STRING, "string" },
47 { MPR_TYPE_STRING_CFUNCTION, "function" },
48 { MPR_TYPE_PTR, "pointer" }
51 const char *type = NULL;
53 if (argc != 1) return -1;
55 for (i=0;i<ARRAY_SIZE(types);i++) {
56 if (argv[0]->type == types[i].type) {
61 if (type == NULL) return -1;
63 mpr_ReturnString(eid, type);
68 return the native type of a variable
70 static int ejs_typeof_native(MprVarHandle eid, int argc, struct MprVar **argv)
76 { MPR_TYPE_UNDEFINED, "undefined" },
77 { MPR_TYPE_NULL, "null" },
78 { MPR_TYPE_BOOL, "boolean" },
79 { MPR_TYPE_CFUNCTION, "c_function" },
80 { MPR_TYPE_FLOAT, "float" },
81 { MPR_TYPE_INT, "integer" },
82 { MPR_TYPE_INT64, "integer64" },
83 { MPR_TYPE_OBJECT, "object" },
84 { MPR_TYPE_FUNCTION, "js_function" },
85 { MPR_TYPE_STRING, "string" },
86 { MPR_TYPE_STRING_CFUNCTION, "string_c_function" },
87 { MPR_TYPE_PTR, "pointer" }
90 const char *type = NULL;
92 if (argc != 1) return -1;
94 for (i=0;i<ARRAY_SIZE(types);i++) {
95 if (argv[0]->type == types[i].type) {
100 if (type == NULL) return -1;
102 mpr_ReturnString(eid, type);
107 libinclude() allows you to include js files using a search path specified
108 in "js include =" in smb.conf.
110 static int ejs_libinclude(int eid, int argc, char **argv)
113 const char **js_include = lp_js_include(mprLpCtx());
115 if (js_include == NULL || js_include[0] == NULL) {
116 ejsSetErrorMsg(eid, "js include path not set");
120 for (i = 0; i < argc; i++) {
121 const char *script = argv[i];
122 struct MprVar result;
126 /* use specfied path to search for requested file */
127 for (j=0;js_include[j];j++) {
128 path = talloc_asprintf(mprMemCtx(), "%s/%s", js_include[j], script);
132 if (file_exist(path)) {
134 ret = ejsEvalFile(eid, path, &result, &emsg);
137 ejsSetErrorMsg(eid, "%s: %s", script, emsg);
145 if (js_include[j] == NULL) {
146 ejsSetErrorMsg(eid, "unable to include '%s'", script);
154 return the current version
156 static int ejs_version(MprVarHandle eid, int argc, struct MprVar **argv)
158 mpr_ReturnString(eid, SAMBA_VERSION_STRING);
163 static void (*ejs_exception_handler) (const char *) = NULL;
165 _PUBLIC_ void ejs_exception(const char *reason)
167 ejs_exception_handler(reason);
171 setup C functions that be called from ejs
173 void smb_setup_ejs_functions(void (*exception_handler)(const char *))
175 extern NTSTATUS ejs_init_security(void);
176 extern NTSTATUS ejs_init_initshutdown(void);
177 extern NTSTATUS smb_setup_ejs_reg(void);
178 extern NTSTATUS smb_setup_ejs_string(void);
179 extern NTSTATUS ejs_init_lsarpc(void);
180 extern NTSTATUS ejs_init_rpcecho(void);
181 extern NTSTATUS ejs_init_winreg(void);
182 extern NTSTATUS smb_setup_ejs_random(void);
183 extern NTSTATUS smb_setup_ejs_config(void);
184 extern NTSTATUS ejs_init_misc(void);
185 extern NTSTATUS ejs_init_netdfs(void);
186 extern NTSTATUS smb_setup_ejs_datablob(void);
187 extern NTSTATUS smb_setup_ejs_auth(void);
188 extern NTSTATUS smb_setup_ejs_nss(void);
189 extern NTSTATUS ejs_init_samr(void);
190 extern NTSTATUS ejs_init_wkssvc(void);
191 extern NTSTATUS smb_setup_ejs_system(void);
192 extern NTSTATUS smb_setup_ejs_ldb(void);
193 extern NTSTATUS ejs_init_svcctl(void);
194 extern NTSTATUS smb_setup_ejs_net(void);
195 extern NTSTATUS ejs_init_srvsvc(void);
196 extern NTSTATUS ejs_init_netlogon(void);
197 extern NTSTATUS ejs_init_drsuapi(void);
198 extern NTSTATUS ejs_init_irpc(void);
199 extern NTSTATUS ejs_init_eventlog(void);
200 init_module_fn static_init[] = { STATIC_smbcalls_MODULES };
201 init_module_fn *shared_init;
203 ejs_exception_handler = exception_handler;
205 smb_setup_ejs_options();
206 smb_setup_ejs_credentials();
207 smb_setup_ejs_literal();
209 shared_init = load_samba_modules(NULL, mprLpCtx(), "smbcalls");
211 run_init_functions(static_init);
212 run_init_functions(shared_init);
214 talloc_free(shared_init);
216 ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
217 ejsDefineCFunction(-1, "nativeTypeOf", ejs_typeof_native, NULL, MPR_VAR_SCRIPT_HANDLE);
218 ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
219 ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE);