Introduce mprLpCtx() similar to mprMemCtx() for loadparm_context used by
[bbaumbach/samba-autobuild/.git] / source4 / 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(mprLpCtx());i++) {
40                 list = str_list_add(list, lp_servicename(lp_servicebynum(mprLpCtx(), i)));
41         }
42         talloc_steal(mprMemCtx(), list);
43         mpr_Return(eid, mprList("services", list));
44         return 0;
45 }
46
47
48 /*
49   allow access to loadparm variables from inside ejs scripts in web apps
50   
51   can be called in 4 ways:
52
53     v = lp.get("type:parm");             gets a parametric variable
54     v = lp.get("share", "type:parm");    gets a parametric variable on a share
55     v = lp.get("parm");                  gets a global variable
56     v = lp.get("share", "parm");         gets a share variable
57
58   the returned variable is a ejs object. It is an array object for lists.  
59 */
60 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
61 {
62         struct parm_struct *parm = NULL;
63         void *parm_ptr = NULL;
64         int i;
65
66         if (argc < 1) return -1;
67
68         if (argc == 2) {
69                 struct loadparm_service *service;
70                 /* its a share parameter */
71                 service = lp_service(mprLpCtx(), argv[0]);
72                 if (service == NULL) {
73                         mpr_Return(eid, mprCreateUndefinedVar());
74                         return 0;
75                 }
76                 if (strchr(argv[1], ':')) {
77                         /* its a parametric option on a share */
78                         const char *type = talloc_strndup(mprMemCtx(), 
79                                                           argv[1], 
80                                                           strcspn(argv[1], ":"));
81                         const char *option = strchr(argv[1], ':') + 1;
82                         const char *value;
83                         if (type == NULL || option == NULL) {
84                                 mpr_Return(eid, mprCreateUndefinedVar());
85                                 return 0;
86                         }
87                         value = lp_get_parametric(mprLpCtx(), service, type, option);
88                         if (value == NULL) {
89                                 mpr_Return(eid, mprCreateUndefinedVar());
90                                 return 0;
91                         }
92                         mpr_ReturnString(eid, value);
93                         return 0;
94                 }
95
96                 parm = lp_parm_struct(argv[1]);
97                 if (parm == NULL || parm->class == P_GLOBAL) {
98                         mpr_Return(eid, mprCreateUndefinedVar());
99                         return 0;
100                 }
101                 parm_ptr = lp_parm_ptr(mprLpCtx(), service, parm);
102         } else if (strchr(argv[0], ':')) {
103                 /* its a global parametric option */
104                 const char *type = talloc_strndup(mprMemCtx(), 
105                                                   argv[0], strcspn(argv[0], ":"));
106                 const char *option = strchr(argv[0], ':') + 1;
107                 const char *value;
108                 if (type == NULL || option == NULL) {
109                         mpr_Return(eid, mprCreateUndefinedVar());
110                         return 0;
111                 }
112                 value = lp_get_parametric(mprLpCtx(), NULL, type, option);
113                 if (value == NULL) {
114                         mpr_Return(eid, mprCreateUndefinedVar());
115                         return 0;
116                 }
117                 mpr_ReturnString(eid, value);
118                 return 0;
119         } else {
120                 /* its a global parameter */
121                 parm = lp_parm_struct(argv[0]);
122                 if (parm == NULL) {
123                         mpr_Return(eid, mprCreateUndefinedVar());
124                         return 0;
125                 }
126                 parm_ptr = lp_parm_ptr(mprLpCtx(), NULL, parm);
127         }
128
129         if (parm == NULL || parm_ptr == NULL) {
130                 mpr_Return(eid, mprCreateUndefinedVar());
131                 return 0;
132         }
133
134         /* construct and return the right type of ejs object */
135         switch (parm->type) {
136         case P_STRING:
137         case P_USTRING:
138                 mpr_ReturnString(eid, *(char **)parm_ptr);
139                 break;
140         case P_BOOL:
141                 mpr_Return(eid, mprCreateBoolVar(*(bool *)parm_ptr));
142                 break;
143         case P_INTEGER:
144         case P_OCTAL:
145         case P_BYTES:
146                 mpr_Return(eid, mprCreateIntegerVar(*(int *)parm_ptr));
147                 break;
148         case P_ENUM:
149                 for (i=0; parm->enum_list[i].name; i++) {
150                         if (*(int *)parm_ptr == parm->enum_list[i].value) {
151                                 mpr_ReturnString(eid, parm->enum_list[i].name);
152                                 return 0;
153                         }
154                 }
155                 mpr_Return(eid, mprCreateUndefinedVar());
156                 return 0;       
157         case P_LIST: 
158                 mpr_Return(eid, mprList(parm->label, *(const char ***)parm_ptr));
159                 break;
160         }
161         return 0;
162 }
163
164 /*
165     v = lp.filename();         obtain filename
166 */
167 static int ejs_lpFilename(MprVarHandle eid, int argc, char **argv)
168 {
169         mpr_ReturnString(eid, lp_configfile(mprLpCtx()));
170         return 0;
171 }
172
173 /*
174   set a smb.conf parameter. Only sets in memory, not permanent
175   
176   can be called in 4 ways:
177
178     ok = lp.set("parm", "value");
179 */
180 static int ejs_lpSet(MprVarHandle eid, int argc, char **argv)
181 {
182         if (argc != 2) {
183                 ejsSetErrorMsg(eid, "lp.set invalid arguments");
184                 return -1;
185         }
186
187         mpr_Return(eid, mprCreateBoolVar(lp_set_cmdline(mprLpCtx(), argv[0], argv[1])));
188         return 0;
189 }
190
191 /*
192   reload smb.conf
193   
194     ok = lp.reload();
195 */
196 static int ejs_lpReload(MprVarHandle eid, int argc, char **argv)
197 {
198         bool ret;
199         const char *filename = lp_configfile(mprLpCtx());
200
201         ret = lp_load(mprLpCtx(), filename);
202         mpr_Return(eid, mprCreateBoolVar(ret));
203         return 0;
204 }
205
206 /*
207   initialise loadparm ejs subsystem
208 */
209 static int ejs_loadparm_init(MprVarHandle eid, int argc, struct MprVar **argv)
210 {
211         struct MprVar *obj = mprInitObject(eid, "loadparm", argc, argv);
212
213         mprSetStringCFunction(obj, "get", ejs_lpGet);
214         mprSetStringCFunction(obj, "set", ejs_lpSet);
215         mprSetStringCFunction(obj, "reload", ejs_lpReload);
216         mprSetStringCFunction(obj, "services", ejs_lpServices);
217         mprSetStringCFunction(obj, "filename", ejs_lpFilename);
218         return 0;
219 }
220
221 /*
222   setup C functions that be called from ejs
223 */
224 NTSTATUS smb_setup_ejs_config(void)
225 {
226         ejsDefineCFunction(-1, "loadparm_init", ejs_loadparm_init, NULL, MPR_VAR_SCRIPT_HANDLE);
227         return NT_STATUS_OK;
228 }