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"
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, "null" },
39 { MPR_TYPE_BOOL, "boolean" },
40 { MPR_TYPE_CFUNCTION, "function" },
41 { MPR_TYPE_FLOAT, "float" },
42 { MPR_TYPE_INT, "int" },
43 { MPR_TYPE_INT64, "int64" },
44 { MPR_TYPE_OBJECT, "object" },
45 { MPR_TYPE_FUNCTION, "function" },
46 { MPR_TYPE_STRING, "string" },
47 { MPR_TYPE_STRING_CFUNCTION, "function" }
50 const char *type = "unknown";
52 if (argc != 1) return -1;
54 for (i=0;i<ARRAY_SIZE(types);i++) {
55 if (argv[0]->type == types[i].type) {
61 ejsSetReturnString(eid, type);
66 setup a return of a string list
68 static void ejs_returnlist(MprVarHandle eid,
69 const char *name, const char **list)
71 ejsSetReturnValue(eid, mprList(name, list));
75 return a list of defined services
77 static int ejs_lpServices(MprVarHandle eid, int argc, char **argv)
80 const char **list = NULL;
81 if (argc != 0) return -1;
83 for (i=0;i<lp_numservices();i++) {
84 list = str_list_add(list, lp_servicename(i));
86 talloc_steal(mprMemCtx(), list);
87 ejs_returnlist(eid, "services", list);
93 allow access to loadparm variables from inside ejs scripts in swat
95 can be called in 4 ways:
97 v = lpGet("type:parm"); gets a parametric variable
98 v = lpGet("share", "type:parm"); gets a parametric variable on a share
99 v = lpGet("parm"); gets a global variable
100 v = lpGet("share", "parm"); gets a share variable
102 the returned variable is a ejs object. It is an array object for lists.
104 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
106 struct parm_struct *parm = NULL;
107 void *parm_ptr = NULL;
110 if (argc < 1) return -1;
113 /* its a share parameter */
114 int snum = lp_servicenumber(argv[0]);
118 if (strchr(argv[1], ':')) {
119 /* its a parametric option on a share */
120 const char *type = talloc_strndup(mprMemCtx(),
122 strcspn(argv[1], ":"));
123 const char *option = strchr(argv[1], ':') + 1;
125 if (type == NULL || option == NULL) return -1;
126 value = lp_get_parametric(snum, type, option);
127 if (value == NULL) return -1;
128 ejsSetReturnString(eid, value);
132 parm = lp_parm_struct(argv[1]);
133 if (parm == NULL || parm->class == P_GLOBAL) {
136 parm_ptr = lp_parm_ptr(snum, parm);
137 } else if (strchr(argv[0], ':')) {
138 /* its a global parametric option */
139 const char *type = talloc_strndup(mprMemCtx(),
140 argv[0], strcspn(argv[0], ":"));
141 const char *option = strchr(argv[0], ':') + 1;
143 if (type == NULL || option == NULL) return -1;
144 value = lp_get_parametric(-1, type, option);
145 if (value == NULL) return -1;
146 ejsSetReturnString(eid, value);
149 /* its a global parameter */
150 parm = lp_parm_struct(argv[0]);
151 if (parm == NULL) return -1;
152 parm_ptr = parm->ptr;
155 if (parm == NULL || parm_ptr == NULL) {
159 /* construct and return the right type of ejs object */
160 switch (parm->type) {
163 ejsSetReturnString(eid, *(char **)parm_ptr);
166 ejsSetReturnValue(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
169 ejsSetReturnValue(eid, mprCreateIntegerVar(*(int *)parm_ptr));
172 for (i=0; parm->enum_list[i].name; i++) {
173 if (*(int *)parm_ptr == parm->enum_list[i].value) {
174 ejsSetReturnString(eid, parm->enum_list[i].name);
180 ejs_returnlist(eid, parm->label, *(const char ***)parm_ptr);
190 perform an ldb search, returning an array of results
193 ldbSearch("dbfile", "expression");
194 var attrs = new Array("attr1", "attr2", "attr3");
195 ldbSearch("dbfile", "expression", attrs);
197 static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv)
199 const char **attrs = NULL;
200 const char *expression, *dbfile;
201 TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
202 struct ldb_context *ldb;
204 struct ldb_message **res;
206 /* validate arguments */
207 if (argc < 2 || argc > 3 ||
208 argv[0]->type != MPR_TYPE_STRING) {
209 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
212 if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
213 ejsSetErrorMsg(eid, "ldbSearch attributes must be an object");
217 dbfile = mprToString(argv[0]);
218 expression = mprToString(argv[1]);
220 attrs = mprToList(tmp_ctx, argv[2]);
222 if (dbfile == NULL || expression == NULL) {
223 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
227 ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
229 ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile);
233 ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
235 ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb));
239 ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message"));
241 talloc_free(tmp_ctx);
245 talloc_free(tmp_ctx);
251 setup the C functions that be called from ejs
253 void smb_setup_ejs_functions(void)
255 ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE);
256 ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE);
257 ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
258 ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE);