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"
30 return the type of a variable
32 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
38 { MPR_TYPE_UNDEFINED, "undefined" },
39 { MPR_TYPE_NULL, "null" },
40 { MPR_TYPE_BOOL, "boolean" },
41 { MPR_TYPE_CFUNCTION, "function" },
42 { MPR_TYPE_FLOAT, "float" },
43 { MPR_TYPE_INT, "int" },
44 { MPR_TYPE_INT64, "int64" },
45 { MPR_TYPE_OBJECT, "object" },
46 { MPR_TYPE_FUNCTION, "function" },
47 { MPR_TYPE_STRING, "string" },
48 { MPR_TYPE_STRING_CFUNCTION, "function" }
51 const char *type = "unknown";
53 if (argc != 1) return -1;
55 for (i=0;i<ARRAY_SIZE(types);i++) {
56 if (argv[0]->type == types[i].type) {
62 ejsSetReturnString(eid, type);
67 setup a return of a string list
69 static void ejs_returnlist(MprVarHandle eid,
70 const char *name, const char **list)
72 ejsSetReturnValue(eid, mprList(name, list));
76 return a list of defined services
78 static int ejs_lpServices(MprVarHandle eid, int argc, char **argv)
81 const char **list = NULL;
82 if (argc != 0) return -1;
84 for (i=0;i<lp_numservices();i++) {
85 list = str_list_add(list, lp_servicename(i));
87 talloc_steal(mprMemCtx(), list);
88 ejs_returnlist(eid, "services", list);
94 allow access to loadparm variables from inside ejs scripts in swat
96 can be called in 4 ways:
98 v = lpGet("type:parm"); gets a parametric variable
99 v = lpGet("share", "type:parm"); gets a parametric variable on a share
100 v = lpGet("parm"); gets a global variable
101 v = lpGet("share", "parm"); gets a share variable
103 the returned variable is a ejs object. It is an array object for lists.
105 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
107 struct parm_struct *parm = NULL;
108 void *parm_ptr = NULL;
111 if (argc < 1) return -1;
114 /* its a share parameter */
115 int snum = lp_servicenumber(argv[0]);
119 if (strchr(argv[1], ':')) {
120 /* its a parametric option on a share */
121 const char *type = talloc_strndup(mprMemCtx(),
123 strcspn(argv[1], ":"));
124 const char *option = strchr(argv[1], ':') + 1;
126 if (type == NULL || option == NULL) return -1;
127 value = lp_get_parametric(snum, type, option);
128 if (value == NULL) return -1;
129 ejsSetReturnString(eid, value);
133 parm = lp_parm_struct(argv[1]);
134 if (parm == NULL || parm->class == P_GLOBAL) {
137 parm_ptr = lp_parm_ptr(snum, parm);
138 } else if (strchr(argv[0], ':')) {
139 /* its a global parametric option */
140 const char *type = talloc_strndup(mprMemCtx(),
141 argv[0], strcspn(argv[0], ":"));
142 const char *option = strchr(argv[0], ':') + 1;
144 if (type == NULL || option == NULL) return -1;
145 value = lp_get_parametric(-1, type, option);
146 if (value == NULL) return -1;
147 ejsSetReturnString(eid, value);
150 /* its a global parameter */
151 parm = lp_parm_struct(argv[0]);
152 if (parm == NULL) return -1;
153 parm_ptr = parm->ptr;
156 if (parm == NULL || parm_ptr == NULL) {
160 /* construct and return the right type of ejs object */
161 switch (parm->type) {
164 ejsSetReturnString(eid, *(char **)parm_ptr);
167 ejsSetReturnValue(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
170 ejsSetReturnValue(eid, mprCreateIntegerVar(*(int *)parm_ptr));
173 for (i=0; parm->enum_list[i].name; i++) {
174 if (*(int *)parm_ptr == parm->enum_list[i].value) {
175 ejsSetReturnString(eid, parm->enum_list[i].name);
181 ejs_returnlist(eid, parm->label, *(const char ***)parm_ptr);
191 perform an ldb search, returning an array of results
194 ldbSearch("dbfile", "expression");
195 var attrs = new Array("attr1", "attr2", "attr3");
196 ldbSearch("dbfile", "expression", attrs);
198 static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv)
200 const char **attrs = NULL;
201 const char *expression, *dbfile;
202 TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
203 struct ldb_context *ldb;
205 struct ldb_message **res;
207 /* validate arguments */
208 if (argc < 2 || argc > 3 ||
209 argv[0]->type != MPR_TYPE_STRING) {
210 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
213 if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
214 ejsSetErrorMsg(eid, "ldbSearch attributes must be an object");
218 dbfile = mprToString(argv[0]);
219 expression = mprToString(argv[1]);
221 attrs = mprToList(tmp_ctx, argv[2]);
223 if (dbfile == NULL || expression == NULL) {
224 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
228 ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
230 ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile);
234 ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
236 ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb));
240 ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message"));
242 talloc_free(tmp_ctx);
246 talloc_free(tmp_ctx);
251 look up a netbios name
254 resolveName("frogurt");
255 resolveName("frogurt", 0x1c);
258 static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv)
260 struct nbt_name name;
261 TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
263 const char *reply_addr;
265 /* validate arguments */
267 if (argv[0]->type != MPR_TYPE_STRING) {
268 ejsSetErrorMsg(eid, "resolveName invalid arguments");
271 make_nbt_name_client(&name, mprToString(argv[0]));
272 } else if (argc == 2) {
273 if (argv[1]->type != MPR_TYPE_INT) {
274 ejsSetErrorMsg(eid, "resolveName invalid arguments");
277 make_nbt_name(&name, mprToString(argv[0]), mprToInt(argv[1]));
279 ejsSetErrorMsg(eid, "resolveName invalid arguments");
283 result = resolve_name(&name, tmp_ctx, &reply_addr);
285 if (!NT_STATUS_IS_OK(result)) {
286 ejsSetErrorMsg(eid, "resolveName name not found");
290 ejsSetReturnString(eid, reply_addr);
292 talloc_free(tmp_ctx);
296 talloc_free(tmp_ctx);
301 setup the C functions that be called from ejs
303 void smb_setup_ejs_functions(void)
305 ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE);
306 ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE);
307 ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
308 ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE);
309 ejsDefineCFunction(-1, "resolveName", ejs_resolve_name, NULL, MPR_VAR_SCRIPT_HANDLE);