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(MprVarHandle eid,
30 TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username,
31 const char *password, const char *domain, const char *workstation,
32 struct socket_address *remote_host, const char *authtype)
34 struct auth_usersupplied_info *user_info = NULL;
35 struct auth_serversupplied_info *server_info = NULL;
36 struct auth_session_info *session_info = NULL;
37 struct auth_context *auth_context;
38 struct MprVar *session_info_obj;
39 const char *auth_types[] = { authtype, NULL };
43 darn, we need some way to get the right event_context here
45 nt_status = auth_context_create(tmp_ctx, auth_types, &auth_context, NULL);
46 if (!NT_STATUS_IS_OK(nt_status)) {
47 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
48 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
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 = workstation;
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 user_info->logon_parameters = 0;
77 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
78 if (!NT_STATUS_IS_OK(nt_status)) {
79 mprSetPropertyValue(auth, "report",
80 mprString(talloc_asprintf(mprMemCtx(), "Login Failed: %s",
81 get_friendly_nt_error_msg(nt_status))));
82 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
86 nt_status = auth_generate_session_info(tmp_ctx, server_info, &session_info);
87 if (!NT_STATUS_IS_OK(nt_status)) {
88 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
89 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
93 session_info_obj = mprInitObject(eid, "session_info", 0, NULL);
95 mprSetPtrChild(session_info_obj, "session_info", session_info);
96 talloc_steal(mprMemCtx(), session_info);
98 mprSetProperty(auth, "session_info", session_info_obj);
99 mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
100 mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
101 mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
108 perform user authentication, returning an array of results
111 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
114 const char *username;
115 const char *password;
117 const char *workstation;
119 struct cli_credentials *creds;
120 struct socket_address *remote_host;
122 if (argc != 2 || argv[0]->type != MPR_TYPE_OBJECT || argv[1]->type != MPR_TYPE_OBJECT) {
123 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
127 /* get credential values from credentials object */
128 creds = mprGetPtr(argv[0], "creds");
130 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
134 remote_host = mprGetPtr(argv[1], "socket_address");
135 if (remote_host == NULL) {
136 ejsSetErrorMsg(eid, "userAuth requires a socket address second parameter");
140 tmp_ctx = talloc_new(mprMemCtx());
142 username = cli_credentials_get_username(creds);
143 password = cli_credentials_get_password(creds);
144 domain = cli_credentials_get_domain(creds);
145 workstation = cli_credentials_get_workstation(creds);
147 if (username == NULL || password == NULL || domain == NULL) {
148 mpr_Return(eid, mprCreateUndefinedVar());
149 talloc_free(tmp_ctx);
153 auth = mprObject("auth");
155 if (domain && (strcmp("SYSTEM USER", domain) == 0)) {
156 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, "unix");
158 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, "sam");
161 mpr_Return(eid, auth);
162 talloc_free(tmp_ctx);
167 initialise credentials ejs object
169 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
171 struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
172 struct auth_session_info *session_info = system_session(mprMemCtx());
174 if (session_info == NULL) {
178 mprSetPtrChild(obj, "session_info", session_info);
183 setup C functions that be called from ejs
185 void smb_setup_ejs_auth(void)
187 ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
188 ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);