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,
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 const char *auth_types[] = { authtype, NULL };
42 darn, we need some way to get the right event_context here
44 nt_status = auth_context_create(tmp_ctx, auth_types, &auth_context, NULL);
45 if (!NT_STATUS_IS_OK(nt_status)) {
46 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
47 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
51 user_info = talloc(tmp_ctx, struct auth_usersupplied_info);
53 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
54 mprSetPropertyValue(auth, "report", mprString("talloc failed"));
58 user_info->mapped_state = True;
59 user_info->client.account_name = username;
60 user_info->mapped.account_name = username;
61 user_info->client.domain_name = domain;
62 user_info->mapped.domain_name = domain;
64 user_info->workstation_name = workstation;
66 user_info->remote_host = NULL;
68 user_info->password_state = AUTH_PASSWORD_PLAIN;
69 user_info->password.plaintext = talloc_strdup(user_info, password);
71 user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME |
72 USER_INFO_DONT_CHECK_UNIX_ACCOUNT;
74 user_info->logon_parameters = 0;
76 nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
77 if (!NT_STATUS_IS_OK(nt_status)) {
78 mprSetPropertyValue(auth, "report", mprString("Login Failed"));
79 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
83 nt_status = auth_generate_session_info(tmp_ctx, server_info, &session_info);
84 if (!NT_STATUS_IS_OK(nt_status)) {
85 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
86 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
90 talloc_steal(mprMemCtx(), session_info);
91 mprSetThisPtr(eid, "session_info", session_info);
93 mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
94 mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
95 mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
102 perform user authentication, returning an array of results
105 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
108 const char *username;
109 const char *password;
111 const char *workstation;
113 struct cli_credentials *creds;
115 if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
116 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
120 /* get credential values from credentials object */
121 creds = mprGetPtr(argv[0], "creds");
123 ejsSetErrorMsg(eid, "userAuth requires a 'creds' element");
127 tmp_ctx = talloc_new(mprMemCtx());
129 username = cli_credentials_get_username(creds);
130 password = cli_credentials_get_password(creds);
131 domain = cli_credentials_get_domain(creds);
132 workstation = cli_credentials_get_workstation(creds);
134 if (username == NULL || password == NULL || domain == NULL) {
135 mpr_Return(eid, mprCreateUndefinedVar());
136 talloc_free(tmp_ctx);
140 auth = mprObject("auth");
142 if (domain && (strcmp("System User", domain) == 0)) {
143 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, "unix");
145 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, "sam");
148 mpr_Return(eid, auth);
149 talloc_free(tmp_ctx);
154 initialise credentials ejs object
156 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
158 struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
159 struct auth_session_info *session_info = system_session(mprMemCtx());
161 if (session_info == NULL) {
165 mprSetPtrChild(obj, "session_info", session_info);
170 setup C functions that be called from ejs
172 void smb_setup_ejs_auth(void)
174 ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
175 ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);