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/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 allow access to loadparm variables from inside ejs scripts in swat
49 can be called in 4 ways:
51 v = lpGet("type:parm"); gets a parametric variable
52 v = lpGet("share", "type:parm"); gets a parametric variable on a share
53 v = lpGet("parm"); gets a global variable
54 v = lpGet("share", "parm"); gets a share variable
56 the returned variable is a ejs object. It is an array object for lists.
58 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
60 struct parm_struct *parm = NULL;
61 void *parm_ptr = NULL;
64 if (argc < 1) return -1;
67 /* its a share parameter */
68 int snum = lp_servicenumber(argv[0]);
72 if (strchr(argv[1], ':')) {
73 /* its a parametric option on a share */
74 const char *type = talloc_strndup(mprMemCtx(),
76 strcspn(argv[1], ":"));
77 const char *option = strchr(argv[1], ':') + 1;
79 if (type == NULL || option == NULL) return -1;
80 value = lp_get_parametric(snum, type, option);
81 if (value == NULL) return -1;
82 mpr_ReturnString(eid, value);
86 parm = lp_parm_struct(argv[1]);
87 if (parm == NULL || parm->class == P_GLOBAL) {
90 parm_ptr = lp_parm_ptr(snum, parm);
91 } else if (strchr(argv[0], ':')) {
92 /* its a global parametric option */
93 const char *type = talloc_strndup(mprMemCtx(),
94 argv[0], strcspn(argv[0], ":"));
95 const char *option = strchr(argv[0], ':') + 1;
97 if (type == NULL || option == NULL) return -1;
98 value = lp_get_parametric(-1, type, option);
99 if (value == NULL) return -1;
100 mpr_ReturnString(eid, value);
103 /* its a global parameter */
104 parm = lp_parm_struct(argv[0]);
105 if (parm == NULL) return -1;
106 parm_ptr = parm->ptr;
109 if (parm == NULL || parm_ptr == NULL) {
113 /* construct and return the right type of ejs object */
114 switch (parm->type) {
117 mpr_ReturnString(eid, *(char **)parm_ptr);
120 mpr_Return(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
123 mpr_Return(eid, mprCreateIntegerVar(*(int *)parm_ptr));
126 for (i=0; parm->enum_list[i].name; i++) {
127 if (*(int *)parm_ptr == parm->enum_list[i].value) {
128 mpr_ReturnString(eid, parm->enum_list[i].name);
134 mpr_Return(eid, mprList(parm->label, *(const char ***)parm_ptr));
143 setup C functions that be called from ejs
145 void smb_setup_ejs_config(void)
147 ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE);
148 ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE);