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 "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "param/loadparm.h"
29 return a list of defined services
31 static int ejs_lpServices(MprVarHandle eid, int argc, char **argv)
34 const char **list = NULL;
35 if (argc != 0) return -1;
37 for (i=0;i<lp_numservices();i++) {
38 list = str_list_add(list, lp_servicename(i));
40 talloc_steal(mprMemCtx(), list);
41 mpr_Return(eid, mprList("services", list));
47 return a list of parameter categories
49 static int ejs_lpCategories(MprVarHandle eid, int argc, char **argv)
51 struct parm_struct *parm_table = lp_parm_table();
53 const char **list = NULL;
54 if (argc != 0) return -1;
56 for (i=0;parm_table[i].label;i++) {
57 if (parm_table[i].class == P_SEPARATOR) {
58 list = str_list_add(list, parm_table[i].label);
61 talloc_steal(mprMemCtx(), list);
62 mpr_Return(eid, mprList("categories", list));
68 allow access to loadparm variables from inside ejs scripts in swat
70 can be called in 4 ways:
72 v = lp.get("type:parm"); gets a parametric variable
73 v = lp.get("share", "type:parm"); gets a parametric variable on a share
74 v = lp.get("parm"); gets a global variable
75 v = lp.get("share", "parm"); gets a share variable
77 the returned variable is a ejs object. It is an array object for lists.
79 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
81 struct parm_struct *parm = NULL;
82 void *parm_ptr = NULL;
85 if (argc < 1) return -1;
88 /* its a share parameter */
89 int snum = lp_servicenumber(argv[0]);
93 if (strchr(argv[1], ':')) {
94 /* its a parametric option on a share */
95 const char *type = talloc_strndup(mprMemCtx(),
97 strcspn(argv[1], ":"));
98 const char *option = strchr(argv[1], ':') + 1;
100 if (type == NULL || option == NULL) return -1;
101 value = lp_get_parametric(snum, type, option);
102 if (value == NULL) return -1;
103 mpr_ReturnString(eid, value);
107 parm = lp_parm_struct(argv[1]);
108 if (parm == NULL || parm->class == P_GLOBAL) {
111 parm_ptr = lp_parm_ptr(snum, parm);
112 } else if (strchr(argv[0], ':')) {
113 /* its a global parametric option */
114 const char *type = talloc_strndup(mprMemCtx(),
115 argv[0], strcspn(argv[0], ":"));
116 const char *option = strchr(argv[0], ':') + 1;
118 if (type == NULL || option == NULL) return -1;
119 value = lp_get_parametric(-1, type, option);
120 if (value == NULL) return -1;
121 mpr_ReturnString(eid, value);
124 /* its a global parameter */
125 parm = lp_parm_struct(argv[0]);
126 if (parm == NULL) return -1;
127 parm_ptr = parm->ptr;
130 if (parm == NULL || parm_ptr == NULL) {
134 /* construct and return the right type of ejs object */
135 switch (parm->type) {
138 mpr_ReturnString(eid, *(char **)parm_ptr);
141 mpr_Return(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
144 mpr_Return(eid, mprCreateIntegerVar(*(int *)parm_ptr));
147 for (i=0; parm->enum_list[i].name; i++) {
148 if (*(int *)parm_ptr == parm->enum_list[i].value) {
149 mpr_ReturnString(eid, parm->enum_list[i].name);
155 mpr_Return(eid, mprList(parm->label, *(const char ***)parm_ptr));
165 set a smb.conf parameter. Only sets in memory, not permanent
167 can be called in 4 ways:
169 ok = lp.set("parm", "value");
171 static int ejs_lpSet(MprVarHandle eid, int argc, char **argv)
174 ejsSetErrorMsg(eid, "lp.set invalid arguments");
178 mpr_Return(eid, mprCreateBoolVar(lp_set_cmdline(argv[0], argv[1])));
187 static int ejs_lpReload(MprVarHandle eid, int argc, char **argv)
189 BOOL ret = lp_load();
193 mpr_Return(eid, mprCreateBoolVar(ret));
198 initialise loadparm ejs subsystem
200 static int ejs_loadparm_init(MprVarHandle eid, int argc, struct MprVar **argv)
202 struct MprVar *obj = mprInitObject(eid, "loadparm", argc, argv);
204 mprSetStringCFunction(obj, "get", ejs_lpGet);
205 mprSetStringCFunction(obj, "set", ejs_lpSet);
206 mprSetStringCFunction(obj, "reload", ejs_lpReload);
207 mprSetStringCFunction(obj, "services", ejs_lpServices);
208 mprSetStringCFunction(obj, "categories", ejs_lpCategories);
213 setup C functions that be called from ejs
215 void smb_setup_ejs_config(void)
217 ejsDefineCFunction(-1, "loadparm_init", ejs_loadparm_init, NULL, MPR_VAR_SCRIPT_HANDLE);