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... */
59 msg = messaging_client_init(tmp_ctx, lp_messaging_path(tmp_ctx, mprLpCtx()),
60 lp_iconv_convenience(mprLpCtx()), ev);
64 nt_status = auth_context_create_methods(tmp_ctx, auth_types, ev, msg, mprLpCtx(), &auth_context);
66 nt_status = auth_context_create(tmp_ctx, ev, msg, mprLpCtx(), &auth_context);
68 if (!NT_STATUS_IS_OK(nt_status)) {
69 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
70 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
74 user_info = talloc(tmp_ctx, struct auth_usersupplied_info);
76 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
77 mprSetPropertyValue(auth, "report", mprString("talloc failed"));
81 user_info->mapped_state = true;
82 user_info->client.account_name = username;
83 user_info->mapped.account_name = username;
84 user_info->client.domain_name = domain;
85 user_info->mapped.domain_name = domain;
87 user_info->workstation_name = workstation;
89 user_info->remote_host = remote_host;
91 user_info->password_state = AUTH_PASSWORD_PLAIN;
92 user_info->password.plaintext = talloc_strdup(user_info, password);
94 user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME |
95 USER_INFO_DONT_CHECK_UNIX_ACCOUNT;
97 user_info->logon_parameters = 0;
99 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
101 /* Don't give the game away (any difference between no such
102 * user and wrong password) */
103 nt_status = auth_nt_status_squash(nt_status);
105 if (!NT_STATUS_IS_OK(nt_status)) {
106 mprSetPropertyValue(auth, "report",
107 mprString(talloc_strdup(mprMemCtx(), get_friendly_nt_error_msg(nt_status))));
108 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
112 nt_status = auth_generate_session_info(tmp_ctx, mprEventCtx(), mprLpCtx(), server_info, &session_info);
113 if (!NT_STATUS_IS_OK(nt_status)) {
114 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
115 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
119 if (security_token_has_nt_authenticated_users(session_info->security_token)) {
120 mprSetPropertyValue(auth, "user_class", mprString("USER"));
124 if (security_token_has_builtin_administrators(session_info->security_token)) {
125 mprSetPropertyValue(auth, "user_class", mprString("ADMINISTRATOR"));
129 if (security_token_is_system(session_info->security_token)) {
130 mprSetPropertyValue(auth, "user_class", mprString("SYSTEM"));
134 if (security_token_is_anonymous(session_info->security_token)) {
135 mprSetPropertyValue(auth, "report", mprString("Anonymous login not permitted"));
136 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
141 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
142 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
145 session_info_obj = mprInitObject(eid, "session_info", 0, NULL);
147 mprSetPtrChild(session_info_obj, "session_info", session_info);
148 talloc_steal(mprMemCtx(), session_info);
150 mprSetProperty(auth, "session_info", session_info_obj);
151 mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
152 mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
153 mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
160 perform user authentication, returning an array of results
163 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
166 const char *username;
167 const char *password;
169 const char *workstation;
171 struct cli_credentials *creds;
172 struct socket_address *remote_host;
173 const char *auth_types_unix[] = { "unix", NULL };
175 if (argc != 2 || argv[0]->type != MPR_TYPE_OBJECT || argv[1]->type != MPR_TYPE_OBJECT) {
176 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
180 /* get credential values from credentials object */
181 creds = mprGetPtr(argv[0], "creds");
183 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
187 remote_host = (struct socket_address *)mprGetPtr(argv[1], "socket_address");
188 if (remote_host == NULL) {
189 ejsSetErrorMsg(eid, "userAuth requires a socket address second parameter");
193 tmp_ctx = talloc_new(mprMemCtx());
195 username = cli_credentials_get_username(creds);
196 password = cli_credentials_get_password(creds);
197 domain = cli_credentials_get_domain(creds);
198 workstation = cli_credentials_get_workstation(creds);
200 if (username == NULL || password == NULL || domain == NULL) {
201 mpr_Return(eid, mprCreateUndefinedVar());
202 talloc_free(tmp_ctx);
206 auth = mprObject("auth");
208 if (domain && (strcmp("SYSTEM USER", domain) == 0)) {
209 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, auth_types_unix);
211 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, NULL);
214 mpr_Return(eid, auth);
215 talloc_free(tmp_ctx);
220 initialise credentials ejs object
222 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
224 struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
225 struct auth_session_info *session_info = system_session(mprMemCtx(), mprLpCtx());
227 if (session_info == NULL) {
231 mprSetPtrChild(obj, "session_info", session_info);
236 setup C functions that be called from ejs
238 NTSTATUS smb_setup_ejs_auth(void)
240 ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
241 ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);