89370890c8899b66e3bc3806770f858ecf862460
[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), 
60                                             lp_iconv_convenience(global_loadparm), ev);
61         }
62
63         if (auth_types) {
64                 nt_status = auth_context_create_methods(tmp_ctx, auth_types, ev, msg, global_loadparm, &auth_context);
65         } else {
66                 nt_status = auth_context_create(tmp_ctx, ev, msg, global_loadparm, &auth_context);
67         }
68         if (!NT_STATUS_IS_OK(nt_status)) {
69                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
70                 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
71                 goto done;
72         }
73
74         user_info = talloc(tmp_ctx, struct auth_usersupplied_info);
75         if (!user_info) {
76                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
77                 mprSetPropertyValue(auth, "report", mprString("talloc failed"));
78                 goto done;
79         }
80
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;
86
87         user_info->workstation_name = workstation;
88
89         user_info->remote_host = remote_host;
90
91         user_info->password_state = AUTH_PASSWORD_PLAIN;
92         user_info->password.plaintext = talloc_strdup(user_info, password);
93
94         user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME |
95                 USER_INFO_DONT_CHECK_UNIX_ACCOUNT;
96
97         user_info->logon_parameters = 0;
98
99         nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
100
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);
104
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));
109                 goto done;
110         }
111
112         nt_status = auth_generate_session_info(tmp_ctx, global_loadparm, 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));
116                 goto done;
117         }
118
119         if (security_token_has_nt_authenticated_users(session_info->security_token)) {
120                 mprSetPropertyValue(auth, "user_class", mprString("USER"));
121                 set = true;
122         }
123         
124         if (security_token_has_builtin_administrators(session_info->security_token)) {
125                 mprSetPropertyValue(auth, "user_class", mprString("ADMINISTRATOR"));
126                 set = true;
127         }
128
129         if (security_token_is_system(session_info->security_token)) {
130                 mprSetPropertyValue(auth, "user_class", mprString("SYSTEM"));
131                 set = true;
132         }
133
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));
137                 goto done;
138         }
139
140         if (!set) {
141                 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
142                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(false));
143         }
144         
145         session_info_obj = mprInitObject(eid, "session_info", 0, NULL);
146
147         mprSetPtrChild(session_info_obj, "session_info", session_info);
148         talloc_steal(mprMemCtx(), session_info);
149
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));
154
155 done:
156         return 0;
157 }
158
159 /*
160   perform user authentication, returning an array of results
161
162 */
163 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
164 {
165         TALLOC_CTX *tmp_ctx;
166         const char *username;
167         const char *password;
168         const char *domain;
169         const char *workstation;
170         struct MprVar auth;
171         struct cli_credentials *creds;
172         struct socket_address *remote_host;
173         const char *auth_types_unix[] = { "unix", NULL };
174
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.");
177                 return -1;
178         }
179
180         /* get credential values from credentials object */
181         creds = mprGetPtr(argv[0], "creds");
182         if (creds == NULL) {
183                 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
184                 return -1;
185         }
186
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");
190                 return -1;
191         }
192
193         tmp_ctx = talloc_new(mprMemCtx());      
194         
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);
199
200         if (username == NULL || password == NULL || domain == NULL) {
201                 mpr_Return(eid, mprCreateUndefinedVar());
202                 talloc_free(tmp_ctx);
203                 return 0;
204         }
205
206         auth = mprObject("auth");
207
208         if (domain && (strcmp("SYSTEM USER", domain) == 0)) {
209                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, auth_types_unix);
210         } else {
211                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, NULL);
212         }
213
214         mpr_Return(eid, auth);
215         talloc_free(tmp_ctx);
216         return 0;
217 }
218
219 /*
220   initialise credentials ejs object
221 */
222 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
223 {
224         struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
225         struct auth_session_info *session_info = system_session(mprMemCtx(), global_loadparm);
226
227         if (session_info == NULL) {
228                 return -1;
229         }
230
231         mprSetPtrChild(obj, "session_info", session_info);
232         return 0;
233 }
234
235 /*
236   setup C functions that be called from ejs
237 */
238 NTSTATUS smb_setup_ejs_auth(void)
239 {
240         ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
241         ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);
242         return NT_STATUS_OK;
243 }