r7238: Add pam auth support in swat
[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("frogurt");
257     resolveName("frogurt", 0x1c);
258 */
259
260 static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv)
261 {
262         struct nbt_name name;
263         TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());  
264         NTSTATUS result;
265         const char *reply_addr;
266
267         /* validate arguments */
268         if (argc == 1) {
269                 if (argv[0]->type != MPR_TYPE_STRING) {
270                         ejsSetErrorMsg(eid, "resolveName invalid arguments");
271                         goto failed;
272                 }
273                 make_nbt_name_client(&name, mprToString(argv[0]));
274         } else if (argc == 2) {
275                 if (argv[1]->type != MPR_TYPE_INT) {
276                         ejsSetErrorMsg(eid, "resolveName invalid arguments");
277                         goto failed;
278                 }
279                 make_nbt_name(&name, mprToString(argv[0]), mprToInt(argv[1]));
280         } else {
281                 ejsSetErrorMsg(eid, "resolveName invalid arguments");
282                 goto failed;
283         }
284
285         result = resolve_name(&name, tmp_ctx, &reply_addr);
286
287         if (!NT_STATUS_IS_OK(result)) {
288                 ejsSetErrorMsg(eid, "resolveName name not found");
289                 goto failed;
290         }
291                 
292         ejsSetReturnString(eid, reply_addr);
293
294         talloc_free(tmp_ctx);
295         return 0;
296
297  failed:
298         talloc_free(tmp_ctx);
299         return -1;
300 }
301
302 static int ejs_userAuth(MprVarHandle eid, int argc, char **argv)
303 {
304         struct auth_usersupplied_info *user_info = NULL;
305         struct auth_serversupplied_info *server_info = NULL;
306         struct auth_context *auth_context;
307         TALLOC_CTX *tmp_ctx;
308         struct MprVar auth;
309         NTSTATUS nt_status;
310         DATA_BLOB pw_blob;
311         int ret;
312
313         if (argc != 3 || *argv[0] == 0 || *argv[2] == 0) {
314                 ejsSetErrorMsg(eid, "userAuth invalid arguments");
315                 return -1;
316         }
317
318         tmp_ctx = talloc_new(mprMemCtx());      
319         auth = mprCreateObjVar("auth", MPR_DEFAULT_HASH_SIZE);
320
321         if (strcmp("System User", argv[2]) == 0) {
322                 const char *auth_unix[] = { "unix", NULL };
323
324                 nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context);
325                 if (!NT_STATUS_IS_OK(nt_status)) {
326                         mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
327                         mprSetPropertyValue(&auth, "report", mprCreateStringVar("Auth System Failure", 0));
328                         goto done;
329                 }
330
331                 pw_blob = data_blob(argv[1], strlen(argv[1])),
332                 make_user_info(tmp_ctx, argv[0], argv[0],
333                                         argv[2], argv[2],
334                                         "foowks", "fooip",
335                                         NULL, NULL,
336                                         NULL, NULL,
337                                         &pw_blob, False,
338                                         0x05, &user_info);
339                 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
340                 if (!NT_STATUS_IS_OK(nt_status)) {
341                         mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
342                         mprSetPropertyValue(&auth, "report", mprCreateStringVar("Login Failed", 0));
343                         goto done;
344                 }
345
346                 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(server_info->authenticated));
347                 mprSetPropertyValue(&auth, "username", mprCreateStringVar(server_info->account_name, 0));
348                 mprSetPropertyValue(&auth, "domain", mprCreateStringVar(server_info->domain_name, 0));
349
350         }  else {
351                 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
352                 mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 0));
353         }
354
355 done:
356         ejsSetReturnValue(eid, auth);
357         talloc_free(tmp_ctx);
358         return 0;
359 }
360
361 static int ejs_domain_list(MprVarHandle eid, int argc, char **argv)
362 {
363         struct MprVar list;
364         struct MprVar dom;
365
366         if (argc != 0) {
367                 ejsSetErrorMsg(eid, "domList invalid arguments");
368                 return -1;
369         }
370
371         list = mprCreateObjVar("list", MPR_DEFAULT_HASH_SIZE);
372         dom = mprCreateStringVar("System User", 1);
373         mprCreateProperty(&list, "0", &dom);
374
375         ejsSetReturnValue(eid, list);
376
377         return 0;
378 }
379
380
381 /*
382   setup the C functions that be called from ejs
383 */
384 void smb_setup_ejs_functions(void)
385 {
386         ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE);
387         ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE);
388         ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
389         ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE);
390         ejsDefineCFunction(-1, "resolveName", ejs_resolve_name, NULL, MPR_VAR_SCRIPT_HANDLE);
391         ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE);
392         ejsDefineStringCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
393 }