2 Unix SMB/CIFS implementation.
6 Copyright (C) Simo Sorce 2005
7 Copyright (C) Andrew Tridgell 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/appweb/ejs/ejs.h"
26 #include "auth/auth.h"
27 #include "scripting/ejs/smbcalls.h"
29 static int ejs_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username, const char *password, const char *domain, const char *remote_host)
31 struct auth_usersupplied_info *user_info = NULL;
32 struct auth_serversupplied_info *server_info = NULL;
33 struct auth_context *auth_context;
34 const char *auth_unix[] = { "unix", NULL };
39 darn, we need some way to get the right event_context here
41 nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context, NULL);
42 if (!NT_STATUS_IS_OK(nt_status)) {
43 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
44 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
48 pw_blob = data_blob(password, strlen(password)+1),
49 make_user_info(tmp_ctx, username, username,
51 remote_host, remote_host,
55 USER_INFO_CASE_INSENSITIVE_USERNAME |
56 USER_INFO_DONT_CHECK_UNIX_ACCOUNT,
58 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
59 if (!NT_STATUS_IS_OK(nt_status)) {
60 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
61 mprSetPropertyValue(auth, "report", mprString("Login Failed"));
65 mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
66 mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
67 mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
74 perform user authentication, returning an array of results
77 var authinfo = new Object();
78 authinfo.username = myname;
79 authinfo.password = mypass;
80 authinfo.domain = mydom;
81 authinfo.rhost = request['REMOTE_HOST'];
82 auth = userAuth(authinfo);
84 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
90 const char *remote_host;
93 if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
94 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
98 username = mprToString(mprGetProperty(argv[0], "username", NULL));
99 password = mprToString(mprGetProperty(argv[0], "password", NULL));
100 domain = mprToString(mprGetProperty(argv[0], "domain", NULL));
101 remote_host = mprToString(mprGetProperty(argv[0], "rhost", NULL));
103 tmp_ctx = talloc_new(mprMemCtx());
104 auth = mprObject("auth");
106 if (domain && strcmp("System User", domain) == 0) {
108 ejs_systemAuth(tmp_ctx, &auth, username, password, domain, remote_host);
111 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
112 mprSetPropertyValue(&auth, "report", mprString("Unknown Domain"));
115 mpr_Return(eid, auth);
116 talloc_free(tmp_ctx);
120 static int ejs_domain_list(MprVarHandle eid, int argc, char **argv)
125 ejsSetErrorMsg(eid, "domList invalid arguments");
129 list = mprObject("list");
130 mprSetVar(&list, "0", mprString("System User"));
132 mpr_Return(eid, list);
138 setup C functions that be called from ejs
140 void smb_setup_ejs_auth(void)
142 ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE);
143 ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);