2 Unix SMB/CIFS implementation.
4 provide hooks into smbd C calls from ejs scripts
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/ejs/ejs.h"
25 #include "param/loadparm.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "auth/auth.h"
31 return the type of a variable
33 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
39 { MPR_TYPE_UNDEFINED, "undefined" },
40 { MPR_TYPE_NULL, "object" },
41 { MPR_TYPE_BOOL, "boolean" },
42 { MPR_TYPE_CFUNCTION, "function" },
43 { MPR_TYPE_FLOAT, "number" },
44 { MPR_TYPE_INT, "number" },
45 { MPR_TYPE_INT64, "number" },
46 { MPR_TYPE_OBJECT, "object" },
47 { MPR_TYPE_FUNCTION, "function" },
48 { MPR_TYPE_STRING, "string" },
49 { MPR_TYPE_STRING_CFUNCTION, "function" }
52 const char *type = NULL;
54 if (argc != 1) return -1;
56 for (i=0;i<ARRAY_SIZE(types);i++) {
57 if (argv[0]->type == types[i].type) {
62 if (type == NULL) return -1;
64 ejsSetReturnString(eid, type);
69 setup a return of a string list
71 static void ejs_returnlist(MprVarHandle eid,
72 const char *name, const char **list)
74 ejsSetReturnValue(eid, mprList(name, list));
78 return a list of defined services
80 static int ejs_lpServices(MprVarHandle eid, int argc, char **argv)
83 const char **list = NULL;
84 if (argc != 0) return -1;
86 for (i=0;i<lp_numservices();i++) {
87 list = str_list_add(list, lp_servicename(i));
89 talloc_steal(mprMemCtx(), list);
90 ejs_returnlist(eid, "services", list);
96 allow access to loadparm variables from inside ejs scripts in swat
98 can be called in 4 ways:
100 v = lpGet("type:parm"); gets a parametric variable
101 v = lpGet("share", "type:parm"); gets a parametric variable on a share
102 v = lpGet("parm"); gets a global variable
103 v = lpGet("share", "parm"); gets a share variable
105 the returned variable is a ejs object. It is an array object for lists.
107 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
109 struct parm_struct *parm = NULL;
110 void *parm_ptr = NULL;
113 if (argc < 1) return -1;
116 /* its a share parameter */
117 int snum = lp_servicenumber(argv[0]);
121 if (strchr(argv[1], ':')) {
122 /* its a parametric option on a share */
123 const char *type = talloc_strndup(mprMemCtx(),
125 strcspn(argv[1], ":"));
126 const char *option = strchr(argv[1], ':') + 1;
128 if (type == NULL || option == NULL) return -1;
129 value = lp_get_parametric(snum, type, option);
130 if (value == NULL) return -1;
131 ejsSetReturnString(eid, value);
135 parm = lp_parm_struct(argv[1]);
136 if (parm == NULL || parm->class == P_GLOBAL) {
139 parm_ptr = lp_parm_ptr(snum, parm);
140 } else if (strchr(argv[0], ':')) {
141 /* its a global parametric option */
142 const char *type = talloc_strndup(mprMemCtx(),
143 argv[0], strcspn(argv[0], ":"));
144 const char *option = strchr(argv[0], ':') + 1;
146 if (type == NULL || option == NULL) return -1;
147 value = lp_get_parametric(-1, type, option);
148 if (value == NULL) return -1;
149 ejsSetReturnString(eid, value);
152 /* its a global parameter */
153 parm = lp_parm_struct(argv[0]);
154 if (parm == NULL) return -1;
155 parm_ptr = parm->ptr;
158 if (parm == NULL || parm_ptr == NULL) {
162 /* construct and return the right type of ejs object */
163 switch (parm->type) {
166 ejsSetReturnString(eid, *(char **)parm_ptr);
169 ejsSetReturnValue(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
172 ejsSetReturnValue(eid, mprCreateIntegerVar(*(int *)parm_ptr));
175 for (i=0; parm->enum_list[i].name; i++) {
176 if (*(int *)parm_ptr == parm->enum_list[i].value) {
177 ejsSetReturnString(eid, parm->enum_list[i].name);
183 ejs_returnlist(eid, parm->label, *(const char ***)parm_ptr);
193 perform an ldb search, returning an array of results
196 ldbSearch("dbfile", "expression");
197 var attrs = new Array("attr1", "attr2", "attr3");
198 ldbSearch("dbfile", "expression", attrs);
200 static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv)
202 const char **attrs = NULL;
203 const char *expression, *dbfile;
204 TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
205 struct ldb_context *ldb;
207 struct ldb_message **res;
209 /* validate arguments */
210 if (argc < 2 || argc > 3 ||
211 argv[0]->type != MPR_TYPE_STRING) {
212 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
215 if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
216 ejsSetErrorMsg(eid, "ldbSearch attributes must be an object");
220 dbfile = mprToString(argv[0]);
221 expression = mprToString(argv[1]);
223 attrs = mprToList(tmp_ctx, argv[2]);
225 if (dbfile == NULL || expression == NULL) {
226 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
230 ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
232 ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile);
236 ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
238 ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb));
242 ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message"));
244 talloc_free(tmp_ctx);
248 talloc_free(tmp_ctx);
253 look up a netbios name
256 resolveName("frogurt");
257 resolveName("frogurt", 0x1c);
260 static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv)
262 struct nbt_name name;
263 TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
265 const char *reply_addr;
267 /* validate arguments */
269 if (argv[0]->type != MPR_TYPE_STRING) {
270 ejsSetErrorMsg(eid, "resolveName invalid arguments");
273 make_nbt_name_client(&name, mprToString(argv[0]));
274 } else if (argc == 2) {
275 if (argv[1]->type != MPR_TYPE_INT) {
276 ejsSetErrorMsg(eid, "resolveName invalid arguments");
279 make_nbt_name(&name, mprToString(argv[0]), mprToInt(argv[1]));
281 ejsSetErrorMsg(eid, "resolveName invalid arguments");
285 result = resolve_name(&name, tmp_ctx, &reply_addr);
287 if (!NT_STATUS_IS_OK(result)) {
288 ejsSetErrorMsg(eid, "resolveName name not found");
292 ejsSetReturnString(eid, reply_addr);
294 talloc_free(tmp_ctx);
298 talloc_free(tmp_ctx);
302 static int ejs_userAuth(MprVarHandle eid, int argc, char **argv)
304 struct auth_usersupplied_info *user_info = NULL;
305 struct auth_serversupplied_info *server_info = NULL;
306 struct auth_context *auth_context;
313 if (argc != 3 || *argv[0] == 0 || *argv[2] == 0) {
314 ejsSetErrorMsg(eid, "userAuth invalid arguments");
318 tmp_ctx = talloc_new(mprMemCtx());
319 auth = mprCreateObjVar("auth", MPR_DEFAULT_HASH_SIZE);
321 if (strcmp("System User", argv[2]) == 0) {
322 const char *auth_unix[] = { "unix", NULL };
324 nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context);
325 if (!NT_STATUS_IS_OK(nt_status)) {
326 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
327 mprSetPropertyValue(&auth, "report", mprCreateStringVar("Auth System Failure", 0));
331 pw_blob = data_blob(argv[1], strlen(argv[1])),
332 make_user_info(tmp_ctx, argv[0], argv[0],
339 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
340 if (!NT_STATUS_IS_OK(nt_status)) {
341 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
342 mprSetPropertyValue(&auth, "report", mprCreateStringVar("Login Failed", 0));
346 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(server_info->authenticated));
347 mprSetPropertyValue(&auth, "username", mprCreateStringVar(server_info->account_name, 0));
348 mprSetPropertyValue(&auth, "domain", mprCreateStringVar(server_info->domain_name, 0));
351 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
352 mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 0));
356 ejsSetReturnValue(eid, auth);
357 talloc_free(tmp_ctx);
361 static int ejs_domain_list(MprVarHandle eid, int argc, char **argv)
367 ejsSetErrorMsg(eid, "domList invalid arguments");
371 list = mprCreateObjVar("list", MPR_DEFAULT_HASH_SIZE);
372 dom = mprCreateStringVar("System User", 1);
373 mprCreateProperty(&list, "0", &dom);
375 ejsSetReturnValue(eid, list);
382 setup the C functions that be called from ejs
384 void smb_setup_ejs_functions(void)
386 ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE);
387 ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE);
388 ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
389 ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE);
390 ejsDefineCFunction(-1, "resolveName", ejs_resolve_name, NULL, MPR_VAR_SCRIPT_HANDLE);
391 ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE);
392 ejsDefineStringCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);