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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "lib/appweb/ejs/ejs.h"
25 #include "auth/auth.h"
26 #include "auth/credentials/credentials.h"
27 #include "scripting/ejs/smbcalls.h"
28 #include "lib/events/events.h"
29 #include "lib/messaging/irpc.h"
30 #include "libcli/security/security.h"
32 static int ejs_doauth(MprVarHandle eid,
33 TALLOC_CTX *tmp_ctx, struct MprVar *auth,
34 const char *username, const char *password,
35 const char *domain, const char *workstation,
36 struct socket_address *remote_host,
37 const char **auth_types)
39 struct auth_usersupplied_info *user_info = NULL;
40 struct auth_serversupplied_info *server_info = NULL;
41 struct auth_session_info *session_info = NULL;
42 struct auth_context *auth_context;
43 struct MprVar *session_info_obj;
47 struct smbcalls_context *c;
48 struct event_context *ev;
49 struct messaging_context *msg;
51 /* Hope we can find an smbcalls_context somewhere up there... */
52 c = talloc_find_parent_bytype(tmp_ctx, struct smbcalls_context);
57 /* Hope we can find the event context somewhere up there... */
58 ev = event_context_find(tmp_ctx);
59 msg = messaging_client_init(tmp_ctx, lp_messaging_path(tmp_ctx, global_loadparm), ev);
63 nt_status = auth_context_create_methods(tmp_ctx, auth_types, ev, msg, &auth_context);
65 nt_status = auth_context_create(tmp_ctx, ev, msg, &auth_context);
67 if (!NT_STATUS_IS_OK(nt_status)) {
68 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
69 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
73 user_info = talloc(tmp_ctx, struct auth_usersupplied_info);
75 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
76 mprSetPropertyValue(auth, "report", mprString("talloc failed"));
80 user_info->mapped_state = true;
81 user_info->client.account_name = username;
82 user_info->mapped.account_name = username;
83 user_info->client.domain_name = domain;
84 user_info->mapped.domain_name = domain;
86 user_info->workstation_name = workstation;
88 user_info->remote_host = remote_host;
90 user_info->password_state = AUTH_PASSWORD_PLAIN;
91 user_info->password.plaintext = talloc_strdup(user_info, password);
93 user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME |
94 USER_INFO_DONT_CHECK_UNIX_ACCOUNT;
96 user_info->logon_parameters = 0;
98 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
100 /* Don't give the game away (any difference between no such
101 * user and wrong password) */
102 nt_status = auth_nt_status_squash(nt_status);
104 if (!NT_STATUS_IS_OK(nt_status)) {
105 mprSetPropertyValue(auth, "report",
106 mprString(talloc_strdup(mprMemCtx(), get_friendly_nt_error_msg(nt_status))));
107 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
111 nt_status = auth_generate_session_info(tmp_ctx, server_info, &session_info);
112 if (!NT_STATUS_IS_OK(nt_status)) {
113 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
114 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
118 if (security_token_has_nt_authenticated_users(session_info->security_token)) {
119 mprSetPropertyValue(auth, "user_class", mprString("USER"));
123 if (security_token_has_builtin_administrators(session_info->security_token)) {
124 mprSetPropertyValue(auth, "user_class", mprString("ADMINISTRATOR"));
128 if (security_token_is_system(session_info->security_token)) {
129 mprSetPropertyValue(auth, "user_class", mprString("SYSTEM"));
133 if (security_token_is_anonymous(session_info->security_token)) {
134 mprSetPropertyValue(auth, "report", mprString("Anonymous login not permitted"));
135 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
140 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
141 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
144 session_info_obj = mprInitObject(eid, "session_info", 0, NULL);
146 mprSetPtrChild(session_info_obj, "session_info", session_info);
147 talloc_steal(mprMemCtx(), session_info);
149 mprSetProperty(auth, "session_info", session_info_obj);
150 mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
151 mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
152 mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
159 perform user authentication, returning an array of results
162 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
165 const char *username;
166 const char *password;
168 const char *workstation;
170 struct cli_credentials *creds;
171 struct socket_address *remote_host;
172 const char *auth_types_unix[] = { "unix", NULL };
174 if (argc != 2 || argv[0]->type != MPR_TYPE_OBJECT || argv[1]->type != MPR_TYPE_OBJECT) {
175 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
179 /* get credential values from credentials object */
180 creds = mprGetPtr(argv[0], "creds");
182 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
186 remote_host = (struct socket_address *)mprGetPtr(argv[1], "socket_address");
187 if (remote_host == NULL) {
188 ejsSetErrorMsg(eid, "userAuth requires a socket address second parameter");
192 tmp_ctx = talloc_new(mprMemCtx());
194 username = cli_credentials_get_username(creds);
195 password = cli_credentials_get_password(creds);
196 domain = cli_credentials_get_domain(creds);
197 workstation = cli_credentials_get_workstation(creds);
199 if (username == NULL || password == NULL || domain == NULL) {
200 mpr_Return(eid, mprCreateUndefinedVar());
201 talloc_free(tmp_ctx);
205 auth = mprObject("auth");
207 if (domain && (strcmp("SYSTEM USER", domain) == 0)) {
208 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, auth_types_unix);
210 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, NULL);
213 mpr_Return(eid, auth);
214 talloc_free(tmp_ctx);
219 initialise credentials ejs object
221 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
223 struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
224 struct auth_session_info *session_info = system_session(mprMemCtx());
226 if (session_info == NULL) {
230 mprSetPtrChild(obj, "session_info", session_info);
235 setup C functions that be called from ejs
237 NTSTATUS smb_setup_ejs_auth(void)
239 ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
240 ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);