r7135: make typeof() complient with ECMA 11.4.3
[kai/samba.git] / source4 / scripting / ejs / smbcalls.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 "lib/ejs/ejs.h"
25 #include "param/loadparm.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28
29 /*
30   return the type of a variable
31 */
32 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
33 {
34         const struct {
35                 MprType type;
36                 const char *name;
37         } types[] = {
38                 { MPR_TYPE_UNDEFINED,        "undefined" },
39                 { MPR_TYPE_NULL,             "object" },
40                 { MPR_TYPE_BOOL,             "boolean" },
41                 { MPR_TYPE_CFUNCTION,        "function" },
42                 { MPR_TYPE_FLOAT,            "number" },
43                 { MPR_TYPE_INT,              "number" },
44                 { MPR_TYPE_INT64,            "number" },
45                 { MPR_TYPE_OBJECT,           "object" },
46                 { MPR_TYPE_FUNCTION,         "function" },
47                 { MPR_TYPE_STRING,           "string" },
48                 { MPR_TYPE_STRING_CFUNCTION, "function" }
49         };
50         int i;
51         const char *type = NULL;
52
53         if (argc != 1) return -1;
54         
55         for (i=0;i<ARRAY_SIZE(types);i++) {
56                 if (argv[0]->type == types[i].type) {
57                         type = types[i].name;
58                         break;
59                 }
60         }
61         if (type == NULL) return -1;
62
63         ejsSetReturnString(eid, type);
64         return 0;
65 }
66
67 /*
68   setup a return of a string list
69 */
70 static void ejs_returnlist(MprVarHandle eid, 
71                            const char *name, const char **list)
72 {
73         ejsSetReturnValue(eid, mprList(name, list));
74 }
75
76 /*
77   return a list of defined services
78 */
79 static int ejs_lpServices(MprVarHandle eid, int argc, char **argv)
80 {
81         int i;
82         const char **list = NULL;
83         if (argc != 0) return -1;
84         
85         for (i=0;i<lp_numservices();i++) {
86                 list = str_list_add(list, lp_servicename(i));
87         }
88         talloc_steal(mprMemCtx(), list);
89         ejs_returnlist(eid, "services", list);
90         return 0;
91 }
92
93
94 /*
95   allow access to loadparm variables from inside ejs scripts in swat
96   
97   can be called in 4 ways:
98
99     v = lpGet("type:parm");             gets a parametric variable
100     v = lpGet("share", "type:parm");    gets a parametric variable on a share
101     v = lpGet("parm");                  gets a global variable
102     v = lpGet("share", "parm");         gets a share variable
103
104   the returned variable is a ejs object. It is an array object for lists.  
105 */
106 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
107 {
108         struct parm_struct *parm = NULL;
109         void *parm_ptr = NULL;
110         int i;
111
112         if (argc < 1) return -1;
113
114         if (argc == 2) {
115                 /* its a share parameter */
116                 int snum = lp_servicenumber(argv[0]);
117                 if (snum == -1) {
118                         return -1;
119                 }
120                 if (strchr(argv[1], ':')) {
121                         /* its a parametric option on a share */
122                         const char *type = talloc_strndup(mprMemCtx(), 
123                                                           argv[1], 
124                                                           strcspn(argv[1], ":"));
125                         const char *option = strchr(argv[1], ':') + 1;
126                         const char *value;
127                         if (type == NULL || option == NULL) return -1;
128                         value = lp_get_parametric(snum, type, option);
129                         if (value == NULL) return -1;
130                         ejsSetReturnString(eid, value);
131                         return 0;
132                 }
133
134                 parm = lp_parm_struct(argv[1]);
135                 if (parm == NULL || parm->class == P_GLOBAL) {
136                         return -1;
137                 }
138                 parm_ptr = lp_parm_ptr(snum, parm);
139         } else if (strchr(argv[0], ':')) {
140                 /* its a global parametric option */
141                 const char *type = talloc_strndup(mprMemCtx(), 
142                                                   argv[0], strcspn(argv[0], ":"));
143                 const char *option = strchr(argv[0], ':') + 1;
144                 const char *value;
145                 if (type == NULL || option == NULL) return -1;
146                 value = lp_get_parametric(-1, type, option);
147                 if (value == NULL) return -1;
148                 ejsSetReturnString(eid, value);
149                 return 0;
150         } else {
151                 /* its a global parameter */
152                 parm = lp_parm_struct(argv[0]);
153                 if (parm == NULL) return -1;
154                 parm_ptr = parm->ptr;
155         }
156
157         if (parm == NULL || parm_ptr == NULL) {
158                 return -1;
159         }
160
161         /* construct and return the right type of ejs object */
162         switch (parm->type) {
163         case P_STRING:
164         case P_USTRING:
165                 ejsSetReturnString(eid, *(char **)parm_ptr);
166                 break;
167         case P_BOOL:
168                 ejsSetReturnValue(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
169                 break;
170         case P_INTEGER:
171                 ejsSetReturnValue(eid, mprCreateIntegerVar(*(int *)parm_ptr));
172                 break;
173         case P_ENUM:
174                 for (i=0; parm->enum_list[i].name; i++) {
175                         if (*(int *)parm_ptr == parm->enum_list[i].value) {
176                                 ejsSetReturnString(eid, parm->enum_list[i].name);
177                                 return 0;
178                         }
179                 }
180                 return -1;      
181         case P_LIST: 
182                 ejs_returnlist(eid, parm->label, *(const char ***)parm_ptr);
183                 break;
184         case P_SEP:
185                 return -1;
186         }
187         return 0;
188 }
189
190
191 /*
192   perform an ldb search, returning an array of results
193
194   syntax:
195      ldbSearch("dbfile", "expression");
196      var attrs = new Array("attr1", "attr2", "attr3");
197      ldbSearch("dbfile", "expression", attrs);
198 */
199 static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv)
200 {
201         const char **attrs = NULL;
202         const char *expression, *dbfile;
203         TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
204         struct ldb_context *ldb;
205         int ret;
206         struct ldb_message **res;
207
208         /* validate arguments */
209         if (argc < 2 || argc > 3 ||
210             argv[0]->type != MPR_TYPE_STRING) {
211                 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
212                 goto failed;
213         }
214         if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
215                 ejsSetErrorMsg(eid, "ldbSearch attributes must be an object");
216                 goto failed;
217         }
218
219         dbfile     = mprToString(argv[0]);
220         expression = mprToString(argv[1]);
221         if (argc > 2) {
222                 attrs = mprToList(tmp_ctx, argv[2]);
223         }
224         if (dbfile == NULL || expression == NULL) {
225                 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
226                 goto failed;
227         }
228
229         ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
230         if (ldb == NULL) {
231                 ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile);
232                 goto failed;
233         }
234
235         ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
236         if (ret == -1) {
237                 ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb));
238                 goto failed;
239         }
240
241         ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message"));
242
243         talloc_free(tmp_ctx);
244         return 0;
245
246 failed:
247         talloc_free(tmp_ctx);
248         return -1;
249 }
250
251 /*
252   look up a netbios name
253
254   syntax:
255     resolveName("frogurt");
256     resolveName("frogurt", 0x1c);
257 */
258
259 static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv)
260 {
261         struct nbt_name name;
262         TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());  
263         NTSTATUS result;
264         const char *reply_addr;
265
266         /* validate arguments */
267         if (argc == 1) {
268                 if (argv[0]->type != MPR_TYPE_STRING) {
269                         ejsSetErrorMsg(eid, "resolveName invalid arguments");
270                         goto failed;
271                 }
272                 make_nbt_name_client(&name, mprToString(argv[0]));
273         } else if (argc == 2) {
274                 if (argv[1]->type != MPR_TYPE_INT) {
275                         ejsSetErrorMsg(eid, "resolveName invalid arguments");
276                         goto failed;
277                 }
278                 make_nbt_name(&name, mprToString(argv[0]), mprToInt(argv[1]));
279         } else {
280                 ejsSetErrorMsg(eid, "resolveName invalid arguments");
281                 goto failed;
282         }
283
284         result = resolve_name(&name, tmp_ctx, &reply_addr);
285
286         if (!NT_STATUS_IS_OK(result)) {
287                 ejsSetErrorMsg(eid, "resolveName name not found");
288                 goto failed;
289         }
290                 
291         ejsSetReturnString(eid, reply_addr);
292
293         talloc_free(tmp_ctx);
294         return 0;
295
296  failed:
297         talloc_free(tmp_ctx);
298         return -1;
299 }
300
301 /*
302   setup the C functions that be called from ejs
303 */
304 void smb_setup_ejs_functions(void)
305 {
306         ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE);
307         ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE);
308         ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
309         ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE);
310         ejsDefineCFunction(-1, "resolveName", ejs_resolve_name, NULL, MPR_VAR_SCRIPT_HANDLE);
311 }