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_doauth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username,
30 const char *password, const char *domain, const char *remote_host,
33 struct auth_usersupplied_info *user_info = NULL;
34 struct auth_serversupplied_info *server_info = NULL;
35 struct auth_context *auth_context;
36 const char *auth_types[] = { authtype, NULL };
41 darn, we need some way to get the right event_context here
43 nt_status = auth_context_create(tmp_ctx, auth_types, &auth_context, NULL);
44 if (!NT_STATUS_IS_OK(nt_status)) {
45 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
46 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
50 pw_blob = data_blob(password, strlen(password)+1);
52 user_info = talloc(tmp_ctx, struct auth_usersupplied_info);
54 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
55 mprSetPropertyValue(auth, "report", mprString("talloc failed"));
59 user_info->mapped_state = True;
60 user_info->client.account_name = username;
61 user_info->mapped.account_name = username;
62 user_info->client.domain_name = domain;
63 user_info->mapped.domain_name = domain;
65 user_info->workstation_name = remote_host;
67 user_info->remote_host = remote_host;
69 user_info->password_state = AUTH_PASSWORD_PLAIN;
70 user_info->password.plaintext = talloc_strdup(user_info, password);
72 user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME |
73 USER_INFO_DONT_CHECK_UNIX_ACCOUNT;
75 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
76 if (!NT_STATUS_IS_OK(nt_status)) {
77 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
78 mprSetPropertyValue(auth, "report", mprString("Login Failed"));
82 mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
83 mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
84 mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
91 perform user authentication, returning an array of results
94 var authinfo = new Object();
95 authinfo.username = myname;
96 authinfo.password = mypass;
97 authinfo.domain = mydom;
98 authinfo.rhost = request['REMOTE_HOST'];
99 auth = userAuth(authinfo);
101 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
104 const char *username;
105 const char *password;
107 const char *remote_host;
110 if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
111 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
115 username = mprToString(mprGetProperty(argv[0], "username", NULL));
116 password = mprToString(mprGetProperty(argv[0], "password", NULL));
117 domain = mprToString(mprGetProperty(argv[0], "domain", NULL));
118 remote_host = mprToString(mprGetProperty(argv[0], "rhost", NULL));
120 if (username == NULL || password == NULL || domain == NULL) {
121 mpr_Return(eid, mprCreateUndefinedVar());
125 tmp_ctx = talloc_new(mprMemCtx());
126 auth = mprObject("auth");
128 if (domain && strcmp("System User", domain) == 0) {
129 ejs_doauth(tmp_ctx, &auth, username, password, domain, remote_host, "unix");
131 ejs_doauth(tmp_ctx, &auth, username, password, domain, remote_host, "sam");
134 mpr_Return(eid, auth);
135 talloc_free(tmp_ctx);
140 setup C functions that be called from ejs
142 void smb_setup_ejs_auth(void)
144 ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);