r12531: 'make quicktest' was taking 15 minutes on my system due to failing DNS
[vlendec/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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "param/loadparm.h"
27
28 /*
29   return a list of defined services
30 */
31 static int ejs_lpServices(MprVarHandle eid, int argc, char **argv)
32 {
33         int i;
34         const char **list = NULL;
35         if (argc != 0) return -1;
36         
37         for (i=0;i<lp_numservices();i++) {
38                 list = str_list_add(list, lp_servicename(i));
39         }
40         talloc_steal(mprMemCtx(), list);
41         mpr_Return(eid, mprList("services", list));
42         return 0;
43 }
44
45
46 /*
47   return a list of parameter categories
48 */
49 static int ejs_lpCategories(MprVarHandle eid, int argc, char **argv)
50 {
51         struct parm_struct *parm_table = lp_parm_table();
52         int i;
53         const char **list = NULL;
54         if (argc != 0) return -1;
55         
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);
59                 }
60         }
61         talloc_steal(mprMemCtx(), list);
62         mpr_Return(eid, mprList("categories", list));
63         return 0;
64 }
65
66
67 /*
68   allow access to loadparm variables from inside ejs scripts in swat
69   
70   can be called in 4 ways:
71
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
76
77   the returned variable is a ejs object. It is an array object for lists.  
78 */
79 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
80 {
81         struct parm_struct *parm = NULL;
82         void *parm_ptr = NULL;
83         int i;
84
85         if (argc < 1) return -1;
86
87         if (argc == 2) {
88                 /* its a share parameter */
89                 int snum = lp_servicenumber(argv[0]);
90                 if (snum == -1) {
91                         return -1;
92                 }
93                 if (strchr(argv[1], ':')) {
94                         /* its a parametric option on a share */
95                         const char *type = talloc_strndup(mprMemCtx(), 
96                                                           argv[1], 
97                                                           strcspn(argv[1], ":"));
98                         const char *option = strchr(argv[1], ':') + 1;
99                         const char *value;
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);
104                         return 0;
105                 }
106
107                 parm = lp_parm_struct(argv[1]);
108                 if (parm == NULL || parm->class == P_GLOBAL) {
109                         return -1;
110                 }
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;
117                 const char *value;
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);
122                 return 0;
123         } else {
124                 /* its a global parameter */
125                 parm = lp_parm_struct(argv[0]);
126                 if (parm == NULL) return -1;
127                 parm_ptr = parm->ptr;
128         }
129
130         if (parm == NULL || parm_ptr == NULL) {
131                 return -1;
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                 mpr_Return(eid, mprCreateIntegerVar(*(int *)parm_ptr));
145                 break;
146         case P_ENUM:
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);
150                                 return 0;
151                         }
152                 }
153                 return -1;      
154         case P_LIST: 
155                 mpr_Return(eid, mprList(parm->label, *(const char ***)parm_ptr));
156                 break;
157         case P_SEP:
158                 return -1;
159         }
160         return 0;
161 }
162
163
164 /*
165   set a smb.conf parameter. Only sets in memory, not permanent
166   
167   can be called in 4 ways:
168
169     ok = lp.set("parm", "value");
170 */
171 static int ejs_lpSet(MprVarHandle eid, int argc, char **argv)
172 {
173         if (argc != 2) {
174                 ejsSetErrorMsg(eid, "lp.set invalid arguments");
175                 return -1;
176         }
177
178         mpr_Return(eid, mprCreateBoolVar(lp_set_cmdline(argv[0], argv[1])));
179         return 0;
180 }
181
182 /*
183   reload smb.conf
184   
185     ok = lp.reload();
186 */
187 static int ejs_lpReload(MprVarHandle eid, int argc, char **argv)
188 {
189         BOOL ret = lp_load();
190         if (ret) {
191                 unload_interfaces();
192         }
193         mpr_Return(eid, mprCreateBoolVar(ret));
194         return 0;
195 }
196
197 /*
198   initialise loadparm ejs subsystem
199 */
200 static int ejs_loadparm_init(MprVarHandle eid, int argc, struct MprVar **argv)
201 {
202         struct MprVar *obj = mprInitObject(eid, "loadparm", argc, argv);
203
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);
209         return 0;
210 }
211
212 /*
213   setup C functions that be called from ejs
214 */
215 void smb_setup_ejs_config(void)
216 {
217         ejsDefineCFunction(-1, "loadparm_init", ejs_loadparm_init, NULL, MPR_VAR_SCRIPT_HANDLE);
218 }