f488b0745ff966aabfc6bc9b10cd3e63b59bfa51
[jelmer/samba4-debian.git] / source / scripting / ejs / smbcalls_config.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide hooks into smbd C calls from ejs scripts
5
6    Copyright (C) Andrew Tridgell 2005
7    
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 3 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "scripting/ejs/smbcalls.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "param/loadparm.h"
26 #include "system/network.h"
27 #include "lib/socket/netif.h"
28 #include "param/param.h"
29
30 /*
31   return a list of defined services
32 */
33 static int ejs_lpServices(MprVarHandle eid, int argc, char **argv)
34 {
35         int i;
36         const char **list = NULL;
37         if (argc != 0) return -1;
38         
39         for (i=0;i<lp_numservices(global_loadparm);i++) {
40                 list = str_list_add(list, lp_servicename(lp_servicebynum(global_loadparm, i)));
41         }
42         talloc_steal(mprMemCtx(), list);
43         mpr_Return(eid, mprList("services", list));
44         return 0;
45 }
46
47
48 /*
49   return a list of parameter categories
50 */
51 static int ejs_lpCategories(MprVarHandle eid, int argc, char **argv)
52 {
53         struct parm_struct *parm_table = lp_parm_table();
54         int i;
55         const char **list = NULL;
56         if (argc != 0) return -1;
57         
58         for (i=0;parm_table[i].label;i++) {
59                 if (parm_table[i].class == P_SEPARATOR) {
60                         list = str_list_add(list, parm_table[i].label);
61                 }
62         }
63         talloc_steal(mprMemCtx(), list);
64         mpr_Return(eid, mprList("categories", list));
65         return 0;
66 }
67
68
69 /*
70   allow access to loadparm variables from inside ejs scripts in web apps
71   
72   can be called in 4 ways:
73
74     v = lp.get("type:parm");             gets a parametric variable
75     v = lp.get("share", "type:parm");    gets a parametric variable on a share
76     v = lp.get("parm");                  gets a global variable
77     v = lp.get("share", "parm");         gets a share variable
78
79   the returned variable is a ejs object. It is an array object for lists.  
80 */
81 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
82 {
83         struct parm_struct *parm = NULL;
84         void *parm_ptr = NULL;
85         int i;
86
87         if (argc < 1) return -1;
88
89         if (argc == 2) {
90                 struct loadparm_service *service;
91                 /* its a share parameter */
92                 service = lp_service(global_loadparm, argv[0]);
93                 if (service == NULL) {
94                         mpr_Return(eid, mprCreateUndefinedVar());
95                         return 0;
96                 }
97                 if (strchr(argv[1], ':')) {
98                         /* its a parametric option on a share */
99                         const char *type = talloc_strndup(mprMemCtx(), 
100                                                           argv[1], 
101                                                           strcspn(argv[1], ":"));
102                         const char *option = strchr(argv[1], ':') + 1;
103                         const char *value;
104                         if (type == NULL || option == NULL) {
105                                 mpr_Return(eid, mprCreateUndefinedVar());
106                                 return 0;
107                         }
108                         value = lp_get_parametric(service, type, option);
109                         if (value == NULL) {
110                                 mpr_Return(eid, mprCreateUndefinedVar());
111                                 return 0;
112                         }
113                         mpr_ReturnString(eid, value);
114                         return 0;
115                 }
116
117                 parm = lp_parm_struct(argv[1]);
118                 if (parm == NULL || parm->class == P_GLOBAL) {
119                         mpr_Return(eid, mprCreateUndefinedVar());
120                         return 0;
121                 }
122                 parm_ptr = lp_parm_ptr(service, parm);
123         } else if (strchr(argv[0], ':')) {
124                 /* its a global parametric option */
125                 const char *type = talloc_strndup(mprMemCtx(), 
126                                                   argv[0], strcspn(argv[0], ":"));
127                 const char *option = strchr(argv[0], ':') + 1;
128                 const char *value;
129                 if (type == NULL || option == NULL) {
130                         mpr_Return(eid, mprCreateUndefinedVar());
131                         return 0;
132                 }
133                 value = lp_get_parametric(NULL, type, option);
134                 if (value == NULL) {
135                         mpr_Return(eid, mprCreateUndefinedVar());
136                         return 0;
137                 }
138                 mpr_ReturnString(eid, value);
139                 return 0;
140         } else {
141                 /* its a global parameter */
142                 parm = lp_parm_struct(argv[0]);
143                 if (parm == NULL) {
144                         mpr_Return(eid, mprCreateUndefinedVar());
145                         return 0;
146                 }
147                 parm_ptr = lp_parm_ptr(NULL, parm);
148         }
149
150         if (parm == NULL || parm_ptr == NULL) {
151                 mpr_Return(eid, mprCreateUndefinedVar());
152                 return 0;
153         }
154
155         /* construct and return the right type of ejs object */
156         switch (parm->type) {
157         case P_STRING:
158         case P_USTRING:
159                 mpr_ReturnString(eid, *(char **)parm_ptr);
160                 break;
161         case P_BOOL:
162                 mpr_Return(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
163                 break;
164         case P_INTEGER:
165         case P_OCTAL:
166         case P_BYTES:
167                 mpr_Return(eid, mprCreateIntegerVar(*(int *)parm_ptr));
168                 break;
169         case P_ENUM:
170                 for (i=0; parm->enum_list[i].name; i++) {
171                         if (*(int *)parm_ptr == parm->enum_list[i].value) {
172                                 mpr_ReturnString(eid, parm->enum_list[i].name);
173                                 return 0;
174                         }
175                 }
176                 mpr_Return(eid, mprCreateUndefinedVar());
177                 return 0;       
178         case P_LIST: 
179                 mpr_Return(eid, mprList(parm->label, *(const char ***)parm_ptr));
180                 break;
181         case P_SEP:
182                 mpr_Return(eid, mprCreateUndefinedVar());
183                 return 0;
184         }
185         return 0;
186 }
187
188
189 /*
190   set a smb.conf parameter. Only sets in memory, not permanent
191   
192   can be called in 4 ways:
193
194     ok = lp.set("parm", "value");
195 */
196 static int ejs_lpSet(MprVarHandle eid, int argc, char **argv)
197 {
198         if (argc != 2) {
199                 ejsSetErrorMsg(eid, "lp.set invalid arguments");
200                 return -1;
201         }
202
203         mpr_Return(eid, mprCreateBoolVar(lp_set_cmdline(global_loadparm, argv[0], argv[1])));
204         return 0;
205 }
206
207 /*
208   reload smb.conf
209   
210     ok = lp.reload();
211 */
212 static int ejs_lpReload(MprVarHandle eid, int argc, char **argv)
213 {
214         BOOL ret = lp_load();
215         if (ret) {
216                 unload_interfaces();
217         }
218         mpr_Return(eid, mprCreateBoolVar(ret));
219         return 0;
220 }
221
222 /*
223   initialise loadparm ejs subsystem
224 */
225 static int ejs_loadparm_init(MprVarHandle eid, int argc, struct MprVar **argv)
226 {
227         struct MprVar *obj = mprInitObject(eid, "loadparm", argc, argv);
228
229         mprSetStringCFunction(obj, "get", ejs_lpGet);
230         mprSetStringCFunction(obj, "set", ejs_lpSet);
231         mprSetStringCFunction(obj, "reload", ejs_lpReload);
232         mprSetStringCFunction(obj, "services", ejs_lpServices);
233         mprSetStringCFunction(obj, "categories", ejs_lpCategories);
234         return 0;
235 }
236
237 /*
238   setup C functions that be called from ejs
239 */
240 NTSTATUS smb_setup_ejs_config(void)
241 {
242         ejsDefineCFunction(-1, "loadparm_init", ejs_loadparm_init, NULL, MPR_VAR_SCRIPT_HANDLE);
243         return NT_STATUS_OK;
244 }