2 Unix SMB/CIFS implementation.
4 provide hooks into smbd C calls from ejs scripts
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Tim Potter 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "lib/ejs/ejs.h"
26 #include "auth/auth.h"
29 return the type of a variable
31 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
37 { MPR_TYPE_UNDEFINED, "undefined" },
38 { MPR_TYPE_NULL, "object" },
39 { MPR_TYPE_BOOL, "boolean" },
40 { MPR_TYPE_CFUNCTION, "function" },
41 { MPR_TYPE_FLOAT, "number" },
42 { MPR_TYPE_INT, "number" },
43 { MPR_TYPE_INT64, "number" },
44 { MPR_TYPE_OBJECT, "object" },
45 { MPR_TYPE_FUNCTION, "function" },
46 { MPR_TYPE_STRING, "string" },
47 { MPR_TYPE_STRING_CFUNCTION, "function" },
48 { MPR_TYPE_PTR, "C pointer" }
51 const char *type = NULL;
53 if (argc != 1) return -1;
55 for (i=0;i<ARRAY_SIZE(types);i++) {
56 if (argv[0]->type == types[i].type) {
61 if (type == NULL) return -1;
63 ejsSetReturnString(eid, type);
68 setup a return of a string list
70 void ejs_returnlist(MprVarHandle eid, const char *name, const char **list)
72 ejsSetReturnValue(eid, mprList(name, list));
75 static int ejs_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username, const char *password, const char *domain, const char *remote_host)
77 struct auth_usersupplied_info *user_info = NULL;
78 struct auth_serversupplied_info *server_info = NULL;
79 struct auth_context *auth_context;
80 const char *auth_unix[] = { "unix", NULL };
85 darn, we need some way to get the right event_context here
87 nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context, NULL);
88 if (!NT_STATUS_IS_OK(nt_status)) {
89 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
90 mprSetPropertyValue(auth, "report", mprCreateStringVar("Auth System Failure", 1));
94 pw_blob = data_blob(password, strlen(password)),
95 make_user_info(tmp_ctx, username, username,
97 remote_host, remote_host,
101 USER_INFO_CASE_INSENSITIVE_USERNAME |
102 USER_INFO_DONT_CHECK_UNIX_ACCOUNT,
104 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
105 if (!NT_STATUS_IS_OK(nt_status)) {
106 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
107 mprSetPropertyValue(auth, "report", mprCreateStringVar("Login Failed", 1));
111 mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
112 mprSetPropertyValue(auth, "username", mprCreateStringVar(server_info->account_name, 1));
113 mprSetPropertyValue(auth, "domain", mprCreateStringVar(server_info->domain_name, 1));
120 perform user authentication, returning an array of results
123 var authinfo = new Object();
124 authinfo.username = myname;
125 authinfo.password = mypass;
126 authinfo.domain = mydom;
127 authinfo.rhost = request['REMOTE_HOST'];
128 auth = userAuth(authinfo);
130 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
133 const char *username;
134 const char *password;
136 const char *remote_host;
139 if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
140 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
144 username = mprToString(mprGetProperty(argv[0], "username", NULL));
145 password = mprToString(mprGetProperty(argv[0], "password", NULL));
146 domain = mprToString(mprGetProperty(argv[0], "domain", NULL));
147 remote_host = mprToString(mprGetProperty(argv[0], "rhost", NULL));
149 tmp_ctx = talloc_new(mprMemCtx());
150 auth = mprCreateObjVar("auth", MPR_DEFAULT_HASH_SIZE);
152 if (strcmp("System User", domain) == 0) {
154 ejs_systemAuth(tmp_ctx, &auth, username, password, domain, remote_host);
157 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
158 mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 1));
161 ejsSetReturnValue(eid, auth);
162 talloc_free(tmp_ctx);
166 static int ejs_domain_list(MprVarHandle eid, int argc, char **argv)
172 ejsSetErrorMsg(eid, "domList invalid arguments");
176 list = mprCreateObjVar("list", MPR_DEFAULT_HASH_SIZE);
177 dom = mprCreateStringVar("System User", 1);
178 mprCreateProperty(&list, "0", &dom);
180 ejsSetReturnValue(eid, list);
187 setup C functions that be called from ejs
189 void smb_setup_ejs_functions(void)
191 smb_setup_ejs_config();
197 ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
198 ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE);
199 ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);