r7255: Change syntax of resolveName() js function to be more like the
[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 #include "auth/auth.h"
29
30 /*
31   return the type of a variable
32 */
33 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
34 {
35         const struct {
36                 MprType type;
37                 const char *name;
38         } types[] = {
39                 { MPR_TYPE_UNDEFINED,        "undefined" },
40                 { MPR_TYPE_NULL,             "object" },
41                 { MPR_TYPE_BOOL,             "boolean" },
42                 { MPR_TYPE_CFUNCTION,        "function" },
43                 { MPR_TYPE_FLOAT,            "number" },
44                 { MPR_TYPE_INT,              "number" },
45                 { MPR_TYPE_INT64,            "number" },
46                 { MPR_TYPE_OBJECT,           "object" },
47                 { MPR_TYPE_FUNCTION,         "function" },
48                 { MPR_TYPE_STRING,           "string" },
49                 { MPR_TYPE_STRING_CFUNCTION, "function" }
50         };
51         int i;
52         const char *type = NULL;
53
54         if (argc != 1) return -1;
55         
56         for (i=0;i<ARRAY_SIZE(types);i++) {
57                 if (argv[0]->type == types[i].type) {
58                         type = types[i].name;
59                         break;
60                 }
61         }
62         if (type == NULL) return -1;
63
64         ejsSetReturnString(eid, type);
65         return 0;
66 }
67
68 /*
69   setup a return of a string list
70 */
71 static void ejs_returnlist(MprVarHandle eid, 
72                            const char *name, const char **list)
73 {
74         ejsSetReturnValue(eid, mprList(name, list));
75 }
76
77 /*
78   return a list of defined services
79 */
80 static int ejs_lpServices(MprVarHandle eid, int argc, char **argv)
81 {
82         int i;
83         const char **list = NULL;
84         if (argc != 0) return -1;
85         
86         for (i=0;i<lp_numservices();i++) {
87                 list = str_list_add(list, lp_servicename(i));
88         }
89         talloc_steal(mprMemCtx(), list);
90         ejs_returnlist(eid, "services", list);
91         return 0;
92 }
93
94
95 /*
96   allow access to loadparm variables from inside ejs scripts in swat
97   
98   can be called in 4 ways:
99
100     v = lpGet("type:parm");             gets a parametric variable
101     v = lpGet("share", "type:parm");    gets a parametric variable on a share
102     v = lpGet("parm");                  gets a global variable
103     v = lpGet("share", "parm");         gets a share variable
104
105   the returned variable is a ejs object. It is an array object for lists.  
106 */
107 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
108 {
109         struct parm_struct *parm = NULL;
110         void *parm_ptr = NULL;
111         int i;
112
113         if (argc < 1) return -1;
114
115         if (argc == 2) {
116                 /* its a share parameter */
117                 int snum = lp_servicenumber(argv[0]);
118                 if (snum == -1) {
119                         return -1;
120                 }
121                 if (strchr(argv[1], ':')) {
122                         /* its a parametric option on a share */
123                         const char *type = talloc_strndup(mprMemCtx(), 
124                                                           argv[1], 
125                                                           strcspn(argv[1], ":"));
126                         const char *option = strchr(argv[1], ':') + 1;
127                         const char *value;
128                         if (type == NULL || option == NULL) return -1;
129                         value = lp_get_parametric(snum, type, option);
130                         if (value == NULL) return -1;
131                         ejsSetReturnString(eid, value);
132                         return 0;
133                 }
134
135                 parm = lp_parm_struct(argv[1]);
136                 if (parm == NULL || parm->class == P_GLOBAL) {
137                         return -1;
138                 }
139                 parm_ptr = lp_parm_ptr(snum, parm);
140         } else if (strchr(argv[0], ':')) {
141                 /* its a global parametric option */
142                 const char *type = talloc_strndup(mprMemCtx(), 
143                                                   argv[0], strcspn(argv[0], ":"));
144                 const char *option = strchr(argv[0], ':') + 1;
145                 const char *value;
146                 if (type == NULL || option == NULL) return -1;
147                 value = lp_get_parametric(-1, type, option);
148                 if (value == NULL) return -1;
149                 ejsSetReturnString(eid, value);
150                 return 0;
151         } else {
152                 /* its a global parameter */
153                 parm = lp_parm_struct(argv[0]);
154                 if (parm == NULL) return -1;
155                 parm_ptr = parm->ptr;
156         }
157
158         if (parm == NULL || parm_ptr == NULL) {
159                 return -1;
160         }
161
162         /* construct and return the right type of ejs object */
163         switch (parm->type) {
164         case P_STRING:
165         case P_USTRING:
166                 ejsSetReturnString(eid, *(char **)parm_ptr);
167                 break;
168         case P_BOOL:
169                 ejsSetReturnValue(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
170                 break;
171         case P_INTEGER:
172                 ejsSetReturnValue(eid, mprCreateIntegerVar(*(int *)parm_ptr));
173                 break;
174         case P_ENUM:
175                 for (i=0; parm->enum_list[i].name; i++) {
176                         if (*(int *)parm_ptr == parm->enum_list[i].value) {
177                                 ejsSetReturnString(eid, parm->enum_list[i].name);
178                                 return 0;
179                         }
180                 }
181                 return -1;      
182         case P_LIST: 
183                 ejs_returnlist(eid, parm->label, *(const char ***)parm_ptr);
184                 break;
185         case P_SEP:
186                 return -1;
187         }
188         return 0;
189 }
190
191
192 /*
193   perform an ldb search, returning an array of results
194
195   syntax:
196      ldbSearch("dbfile", "expression");
197      var attrs = new Array("attr1", "attr2", "attr3");
198      ldbSearch("dbfile", "expression", attrs);
199 */
200 static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv)
201 {
202         const char **attrs = NULL;
203         const char *expression, *dbfile;
204         TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
205         struct ldb_context *ldb;
206         int ret;
207         struct ldb_message **res;
208
209         /* validate arguments */
210         if (argc < 2 || argc > 3 ||
211             argv[0]->type != MPR_TYPE_STRING) {
212                 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
213                 goto failed;
214         }
215         if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
216                 ejsSetErrorMsg(eid, "ldbSearch attributes must be an object");
217                 goto failed;
218         }
219
220         dbfile     = mprToString(argv[0]);
221         expression = mprToString(argv[1]);
222         if (argc > 2) {
223                 attrs = mprToList(tmp_ctx, argv[2]);
224         }
225         if (dbfile == NULL || expression == NULL) {
226                 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
227                 goto failed;
228         }
229
230         ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
231         if (ldb == NULL) {
232                 ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile);
233                 goto failed;
234         }
235
236         ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
237         if (ret == -1) {
238                 ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb));
239                 goto failed;
240         }
241
242         ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message"));
243
244         talloc_free(tmp_ctx);
245         return 0;
246
247 failed:
248         talloc_free(tmp_ctx);
249         return -1;
250 }
251
252 /*
253   look up a netbios name
254
255   syntax:
256     resolveName(result, "frogurt");
257     resolveName(result, "frogurt", 0x1c);
258 */
259
260 static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv)
261 {
262         int result = -1;
263         struct nbt_name name;
264         TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());  
265         NTSTATUS nt_status;
266         const char *reply_addr;
267
268         /* validate arguments */
269         if (argc < 2 || argc > 3) {
270                 ejsSetErrorMsg(eid, "resolveName invalid arguments");
271                 goto done;
272         }
273
274         if (argv[1]->type != MPR_TYPE_STRING) {
275                 ejsSetErrorMsg(eid, "resolveName invalid arguments");
276                 goto done;
277         }
278         
279         if (argc == 2) {
280                 make_nbt_name_client(&name, mprToString(argv[1]));
281         } else {
282                 if (argv[1]->type != MPR_TYPE_INT) {
283                         ejsSetErrorMsg(eid, "resolveName invalid arguments");
284                         goto done;
285                 }
286                 make_nbt_name(&name, mprToString(argv[1]), mprToInt(argv[2]));
287         }
288
289         result = 0;
290
291         nt_status = resolve_name(&name, tmp_ctx, &reply_addr);
292
293         if (NT_STATUS_IS_OK(nt_status)) {
294                 mprDestroyAllVars(argv[0]);
295                 *argv[0] = mprCreateStringVar(reply_addr, True);
296         }
297
298         ejsSetReturnValue(eid, mprNTSTATUS(nt_status));
299
300  done:
301         talloc_free(tmp_ctx);
302         return result;
303 }
304
305 static int ejs_userAuth(MprVarHandle eid, int argc, char **argv)
306 {
307         struct auth_usersupplied_info *user_info = NULL;
308         struct auth_serversupplied_info *server_info = NULL;
309         struct auth_context *auth_context;
310         TALLOC_CTX *tmp_ctx;
311         struct MprVar auth;
312         NTSTATUS nt_status;
313         DATA_BLOB pw_blob;
314
315         if (argc != 3 || *argv[0] == 0 || *argv[2] == 0) {
316                 ejsSetErrorMsg(eid, "userAuth invalid arguments");
317                 return -1;
318         }
319
320         tmp_ctx = talloc_new(mprMemCtx());      
321         auth = mprCreateObjVar("auth", MPR_DEFAULT_HASH_SIZE);
322
323         if (strcmp("System User", argv[2]) == 0) {
324                 const char *auth_unix[] = { "unix", NULL };
325
326                 nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context);
327                 if (!NT_STATUS_IS_OK(nt_status)) {
328                         mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
329                         mprSetPropertyValue(&auth, "report", mprCreateStringVar("Auth System Failure", 0));
330                         goto done;
331                 }
332
333                 pw_blob = data_blob(argv[1], strlen(argv[1])),
334                 make_user_info(tmp_ctx, argv[0], argv[0],
335                                         argv[2], argv[2],
336                                         "foowks", "fooip",
337                                         NULL, NULL,
338                                         NULL, NULL,
339                                         &pw_blob, False,
340                                         0x05, &user_info);
341                 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
342                 if (!NT_STATUS_IS_OK(nt_status)) {
343                         mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
344                         mprSetPropertyValue(&auth, "report", mprCreateStringVar("Login Failed", 0));
345                         goto done;
346                 }
347
348                 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(server_info->authenticated));
349                 mprSetPropertyValue(&auth, "username", mprCreateStringVar(server_info->account_name, 0));
350                 mprSetPropertyValue(&auth, "domain", mprCreateStringVar(server_info->domain_name, 0));
351
352         }  else {
353                 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
354                 mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 0));
355         }
356
357 done:
358         ejsSetReturnValue(eid, auth);
359         talloc_free(tmp_ctx);
360         return 0;
361 }
362
363 static int ejs_domain_list(MprVarHandle eid, int argc, char **argv)
364 {
365         struct MprVar list;
366         struct MprVar dom;
367
368         if (argc != 0) {
369                 ejsSetErrorMsg(eid, "domList invalid arguments");
370                 return -1;
371         }
372
373         list = mprCreateObjVar("list", MPR_DEFAULT_HASH_SIZE);
374         dom = mprCreateStringVar("System User", 1);
375         mprCreateProperty(&list, "0", &dom);
376
377         ejsSetReturnValue(eid, list);
378
379         return 0;
380 }
381
382
383 /*
384   setup the C functions that be called from ejs
385 */
386 void smb_setup_ejs_functions(void)
387 {
388         ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE);
389         ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE);
390         ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
391         ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE);
392         ejsDefineCFunction(-1, "resolveName", ejs_resolve_name, NULL, MPR_VAR_SCRIPT_HANDLE);
393         ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE);
394         ejsDefineStringCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
395 }