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