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"
31 static int ejs_doauth(MprVarHandle eid,
32 TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username,
33 const char *password, const char *domain, const char *workstation,
34 struct socket_address *remote_host, const char **auth_types)
36 struct auth_usersupplied_info *user_info = NULL;
37 struct auth_serversupplied_info *server_info = NULL;
38 struct auth_session_info *session_info = NULL;
39 struct auth_context *auth_context;
40 struct MprVar *session_info_obj;
43 struct smbcalls_context *c;
44 struct event_context *ev;
45 struct messaging_context *msg;
47 /* Hope we can find an smbcalls_context somewhere up there... */
48 c = talloc_find_parent_bytype(tmp_ctx, struct smbcalls_context);
53 /* Hope we can find the event context somewhere up there... */
54 ev = event_context_find(tmp_ctx);
55 msg = messaging_client_init(tmp_ctx, ev);
58 nt_status = auth_context_create_methods(tmp_ctx, auth_types, ev, msg, &auth_context);
59 if (!NT_STATUS_IS_OK(nt_status)) {
60 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
61 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
65 user_info = talloc(tmp_ctx, struct auth_usersupplied_info);
67 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
68 mprSetPropertyValue(auth, "report", mprString("talloc failed"));
72 user_info->mapped_state = True;
73 user_info->client.account_name = username;
74 user_info->mapped.account_name = username;
75 user_info->client.domain_name = domain;
76 user_info->mapped.domain_name = domain;
78 user_info->workstation_name = workstation;
80 user_info->remote_host = remote_host;
82 user_info->password_state = AUTH_PASSWORD_PLAIN;
83 user_info->password.plaintext = talloc_strdup(user_info, password);
85 user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME |
86 USER_INFO_DONT_CHECK_UNIX_ACCOUNT;
88 user_info->logon_parameters = 0;
90 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
92 /* Don't give the game away (any difference between no such
93 * user and wrong password) */
94 nt_status = auth_nt_status_squash(nt_status);
96 if (!NT_STATUS_IS_OK(nt_status)) {
97 mprSetPropertyValue(auth, "report",
98 mprString(talloc_strdup(mprMemCtx(), get_friendly_nt_error_msg(nt_status))));
99 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
103 nt_status = auth_generate_session_info(tmp_ctx, server_info, &session_info);
104 if (!NT_STATUS_IS_OK(nt_status)) {
105 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
106 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
110 session_info_obj = mprInitObject(eid, "session_info", 0, NULL);
112 mprSetPtrChild(session_info_obj, "session_info", session_info);
113 talloc_steal(mprMemCtx(), session_info);
115 mprSetProperty(auth, "session_info", session_info_obj);
116 mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
117 mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
118 mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
125 perform user authentication, returning an array of results
128 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
131 const char *username;
132 const char *password;
134 const char *workstation;
136 struct cli_credentials *creds;
137 struct socket_address *remote_host;
138 const char *auth_types_unix[] = { "unix", NULL };
140 if (argc != 2 || argv[0]->type != MPR_TYPE_OBJECT || argv[1]->type != MPR_TYPE_OBJECT) {
141 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
145 /* get credential values from credentials object */
146 creds = mprGetPtr(argv[0], "creds");
148 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
152 remote_host = mprGetPtr(argv[1], "socket_address");
153 if (remote_host == NULL) {
154 ejsSetErrorMsg(eid, "userAuth requires a socket address second parameter");
158 tmp_ctx = talloc_new(mprMemCtx());
160 username = cli_credentials_get_username(creds);
161 password = cli_credentials_get_password(creds);
162 domain = cli_credentials_get_domain(creds);
163 workstation = cli_credentials_get_workstation(creds);
165 if (username == NULL || password == NULL || domain == NULL) {
166 mpr_Return(eid, mprCreateUndefinedVar());
167 talloc_free(tmp_ctx);
171 auth = mprObject("auth");
173 if (domain && (strcmp("SYSTEM USER", domain) == 0)) {
174 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, auth_types_unix);
176 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, lp_auth_methods());
179 mpr_Return(eid, auth);
180 talloc_free(tmp_ctx);
185 initialise credentials ejs object
187 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
189 struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
190 struct auth_session_info *session_info = system_session(mprMemCtx());
192 if (session_info == NULL) {
196 mprSetPtrChild(obj, "session_info", session_info);
201 setup C functions that be called from ejs
203 NTSTATUS smb_setup_ejs_auth(void)
205 ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
206 ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);