r17586: merge lib/netif into lib/socket and use -lnsl -lsocket on the
[kai/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 #include "system/network.h"
28 #include "lib/socket/netif.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();i++) {
40                 list = str_list_add(list, lp_servicename(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 swat
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                 /* its a share parameter */
91                 int snum = lp_servicenumber(argv[0]);
92                 if (snum == -1) {
93                         return -1;
94                 }
95                 if (strchr(argv[1], ':')) {
96                         /* its a parametric option on a share */
97                         const char *type = talloc_strndup(mprMemCtx(), 
98                                                           argv[1], 
99                                                           strcspn(argv[1], ":"));
100                         const char *option = strchr(argv[1], ':') + 1;
101                         const char *value;
102                         if (type == NULL || option == NULL) return -1;
103                         value = lp_get_parametric(snum, type, option);
104                         if (value == NULL) return -1;
105                         mpr_ReturnString(eid, value);
106                         return 0;
107                 }
108
109                 parm = lp_parm_struct(argv[1]);
110                 if (parm == NULL || parm->class == P_GLOBAL) {
111                         return -1;
112                 }
113                 parm_ptr = lp_parm_ptr(snum, parm);
114         } else if (strchr(argv[0], ':')) {
115                 /* its a global parametric option */
116                 const char *type = talloc_strndup(mprMemCtx(), 
117                                                   argv[0], strcspn(argv[0], ":"));
118                 const char *option = strchr(argv[0], ':') + 1;
119                 const char *value;
120                 if (type == NULL || option == NULL) return -1;
121                 value = lp_get_parametric(-1, type, option);
122                 if (value == NULL) return -1;
123                 mpr_ReturnString(eid, value);
124                 return 0;
125         } else {
126                 /* its a global parameter */
127                 parm = lp_parm_struct(argv[0]);
128                 if (parm == NULL) return -1;
129                 parm_ptr = lp_parm_ptr(-1, parm);
130         }
131
132         if (parm == NULL || parm_ptr == NULL) {
133                 return -1;
134         }
135
136         /* construct and return the right type of ejs object */
137         switch (parm->type) {
138         case P_STRING:
139         case P_USTRING:
140                 mpr_ReturnString(eid, *(char **)parm_ptr);
141                 break;
142         case P_BOOL:
143                 mpr_Return(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
144                 break;
145         case P_INTEGER:
146         case P_BYTES:
147                 mpr_Return(eid, mprCreateIntegerVar(*(int *)parm_ptr));
148                 break;
149         case P_ENUM:
150                 for (i=0; parm->enum_list[i].name; i++) {
151                         if (*(int *)parm_ptr == parm->enum_list[i].value) {
152                                 mpr_ReturnString(eid, parm->enum_list[i].name);
153                                 return 0;
154                         }
155                 }
156                 return -1;      
157         case P_LIST: 
158                 mpr_Return(eid, mprList(parm->label, *(const char ***)parm_ptr));
159                 break;
160         case P_SEP:
161                 return -1;
162         }
163         return 0;
164 }
165
166
167 /*
168   set a smb.conf parameter. Only sets in memory, not permanent
169   
170   can be called in 4 ways:
171
172     ok = lp.set("parm", "value");
173 */
174 static int ejs_lpSet(MprVarHandle eid, int argc, char **argv)
175 {
176         if (argc != 2) {
177                 ejsSetErrorMsg(eid, "lp.set invalid arguments");
178                 return -1;
179         }
180
181         mpr_Return(eid, mprCreateBoolVar(lp_set_cmdline(argv[0], argv[1])));
182         return 0;
183 }
184
185 /*
186   reload smb.conf
187   
188     ok = lp.reload();
189 */
190 static int ejs_lpReload(MprVarHandle eid, int argc, char **argv)
191 {
192         BOOL ret = lp_load();
193         if (ret) {
194                 unload_interfaces();
195         }
196         mpr_Return(eid, mprCreateBoolVar(ret));
197         return 0;
198 }
199
200 /*
201   initialise loadparm ejs subsystem
202 */
203 static int ejs_loadparm_init(MprVarHandle eid, int argc, struct MprVar **argv)
204 {
205         struct MprVar *obj = mprInitObject(eid, "loadparm", argc, argv);
206
207         mprSetStringCFunction(obj, "get", ejs_lpGet);
208         mprSetStringCFunction(obj, "set", ejs_lpSet);
209         mprSetStringCFunction(obj, "reload", ejs_lpReload);
210         mprSetStringCFunction(obj, "services", ejs_lpServices);
211         mprSetStringCFunction(obj, "categories", ejs_lpCategories);
212         return 0;
213 }
214
215 /*
216   setup C functions that be called from ejs
217 */
218 NTSTATUS smb_setup_ejs_config(void)
219 {
220         ejsDefineCFunction(-1, "loadparm_init", ejs_loadparm_init, NULL, MPR_VAR_SCRIPT_HANDLE);
221         return NT_STATUS_OK;
222 }