b4848d4323fcaa3aab7b9ffa724a8b255b4fe6d3
[jelmer/samba4-debian.git] / source / scripting / ejs / smbcalls_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    ejs auth functions
5
6    Copyright (C) Simo Sorce 2005
7    Copyright (C) Andrew Tridgell 2005
8    
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.
13    
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.
18    
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/>.
21 */
22
23 #include "includes.h"
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"
31
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)
38 {
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;
44         NTSTATUS nt_status;
45         bool set;
46
47         struct smbcalls_context *c;
48         struct event_context *ev;
49         struct messaging_context *msg;
50
51         /* Hope we can find an smbcalls_context somewhere up there... */
52         c = talloc_find_parent_bytype(tmp_ctx, struct smbcalls_context);
53         if (c) {
54                 ev = c->event_ctx;
55                 msg = c->msg_ctx;
56         } else {
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);
60         }
61
62         if (auth_types) {
63                 nt_status = auth_context_create_methods(tmp_ctx, auth_types, ev, msg, &auth_context);
64         } else {
65                 nt_status = auth_context_create(tmp_ctx, ev, msg, &auth_context);
66         }
67         if (!NT_STATUS_IS_OK(nt_status)) {
68                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
69                 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
70                 goto done;
71         }
72
73         user_info = talloc(tmp_ctx, struct auth_usersupplied_info);
74         if (!user_info) {
75                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
76                 mprSetPropertyValue(auth, "report", mprString("talloc failed"));
77                 goto done;
78         }
79
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;
85
86         user_info->workstation_name = workstation;
87
88         user_info->remote_host = remote_host;
89
90         user_info->password_state = AUTH_PASSWORD_PLAIN;
91         user_info->password.plaintext = talloc_strdup(user_info, password);
92
93         user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME |
94                 USER_INFO_DONT_CHECK_UNIX_ACCOUNT;
95
96         user_info->logon_parameters = 0;
97
98         nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
99
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);
103
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));
108                 goto done;
109         }
110
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));
115                 goto done;
116         }
117
118         if (security_token_has_nt_authenticated_users(session_info->security_token)) {
119                 mprSetPropertyValue(auth, "user_class", mprString("USER"));
120                 set = true;
121         }
122         
123         if (security_token_has_builtin_administrators(session_info->security_token)) {
124                 mprSetPropertyValue(auth, "user_class", mprString("ADMINISTRATOR"));
125                 set = true;
126         }
127
128         if (security_token_is_system(session_info->security_token)) {
129                 mprSetPropertyValue(auth, "user_class", mprString("SYSTEM"));
130                 set = true;
131         }
132
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));
136                 goto done;
137         }
138
139         if (!set) {
140                 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
141                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
142         }
143         
144         session_info_obj = mprInitObject(eid, "session_info", 0, NULL);
145
146         mprSetPtrChild(session_info_obj, "session_info", session_info);
147         talloc_steal(mprMemCtx(), session_info);
148
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));
153
154 done:
155         return 0;
156 }
157
158 /*
159   perform user authentication, returning an array of results
160
161 */
162 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
163 {
164         TALLOC_CTX *tmp_ctx;
165         const char *username;
166         const char *password;
167         const char *domain;
168         const char *workstation;
169         struct MprVar auth;
170         struct cli_credentials *creds;
171         struct socket_address *remote_host;
172         const char *auth_types_unix[] = { "unix", NULL };
173
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.");
176                 return -1;
177         }
178
179         /* get credential values from credentials object */
180         creds = mprGetPtr(argv[0], "creds");
181         if (creds == NULL) {
182                 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
183                 return -1;
184         }
185
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");
189                 return -1;
190         }
191
192         tmp_ctx = talloc_new(mprMemCtx());      
193         
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);
198
199         if (username == NULL || password == NULL || domain == NULL) {
200                 mpr_Return(eid, mprCreateUndefinedVar());
201                 talloc_free(tmp_ctx);
202                 return 0;
203         }
204
205         auth = mprObject("auth");
206
207         if (domain && (strcmp("SYSTEM USER", domain) == 0)) {
208                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, auth_types_unix);
209         } else {
210                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, NULL);
211         }
212
213         mpr_Return(eid, auth);
214         talloc_free(tmp_ctx);
215         return 0;
216 }
217
218 /*
219   initialise credentials ejs object
220 */
221 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
222 {
223         struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
224         struct auth_session_info *session_info = system_session(mprMemCtx());
225
226         if (session_info == NULL) {
227                 return -1;
228         }
229
230         mprSetPtrChild(obj, "session_info", session_info);
231         return 0;
232 }
233
234 /*
235   setup C functions that be called from ejs
236 */
237 NTSTATUS smb_setup_ejs_auth(void)
238 {
239         ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
240         ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);
241         return NT_STATUS_OK;
242 }