r23792: convert Samba4 to GPLv3
[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
31 static int ejs_doauth(MprVarHandle eid,
32                       TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username, 
33                       const char *password, const char *domain, const char *workstation,
34                       struct socket_address *remote_host, const char **auth_types)
35 {
36         struct auth_usersupplied_info *user_info = NULL;
37         struct auth_serversupplied_info *server_info = NULL;
38         struct auth_session_info *session_info = NULL;
39         struct auth_context *auth_context;
40         struct MprVar *session_info_obj;
41         NTSTATUS nt_status;
42
43         struct smbcalls_context *c;
44         struct event_context *ev;
45         struct messaging_context *msg;
46
47         /* Hope we can find an smbcalls_context somewhere up there... */
48         c = talloc_find_parent_bytype(tmp_ctx, struct smbcalls_context);
49         if (c) {
50                 ev = c->event_ctx;
51                 msg = c->msg_ctx;
52         } else {
53                 /* Hope we can find the event context somewhere up there... */
54                 ev = event_context_find(tmp_ctx);
55                 msg = messaging_client_init(tmp_ctx, ev);
56         }
57
58         nt_status = auth_context_create_methods(tmp_ctx, auth_types, ev, msg, &auth_context);
59         if (!NT_STATUS_IS_OK(nt_status)) {
60                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
61                 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
62                 goto done;
63         }
64
65         user_info = talloc(tmp_ctx, struct auth_usersupplied_info);
66         if (!user_info) {
67                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
68                 mprSetPropertyValue(auth, "report", mprString("talloc failed"));
69                 goto done;
70         }
71
72         user_info->mapped_state = True;
73         user_info->client.account_name = username;
74         user_info->mapped.account_name = username;
75         user_info->client.domain_name = domain;
76         user_info->mapped.domain_name = domain;
77
78         user_info->workstation_name = workstation;
79
80         user_info->remote_host = remote_host;
81
82         user_info->password_state = AUTH_PASSWORD_PLAIN;
83         user_info->password.plaintext = talloc_strdup(user_info, password);
84
85         user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME |
86                 USER_INFO_DONT_CHECK_UNIX_ACCOUNT;
87
88         user_info->logon_parameters = 0;
89
90         nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
91
92         /* Don't give the game away (any difference between no such
93          * user and wrong password) */
94         nt_status = auth_nt_status_squash(nt_status);
95
96         if (!NT_STATUS_IS_OK(nt_status)) {
97                 mprSetPropertyValue(auth, "report", 
98                                     mprString(talloc_strdup(mprMemCtx(), get_friendly_nt_error_msg(nt_status))));
99                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
100                 goto done;
101         }
102
103         nt_status = auth_generate_session_info(tmp_ctx, server_info, &session_info);
104         if (!NT_STATUS_IS_OK(nt_status)) {
105                 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
106                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
107                 goto done;
108         }
109
110         session_info_obj = mprInitObject(eid, "session_info", 0, NULL);
111
112         mprSetPtrChild(session_info_obj, "session_info", session_info);
113         talloc_steal(mprMemCtx(), session_info);
114
115         mprSetProperty(auth, "session_info", session_info_obj);
116         mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
117         mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
118         mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
119
120 done:
121         return 0;
122 }
123
124 /*
125   perform user authentication, returning an array of results
126
127 */
128 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
129 {
130         TALLOC_CTX *tmp_ctx;
131         const char *username;
132         const char *password;
133         const char *domain;
134         const char *workstation;
135         struct MprVar auth;
136         struct cli_credentials *creds;
137         struct socket_address *remote_host;
138         const char *auth_types_unix[] = { "unix", NULL };
139
140         if (argc != 2 || argv[0]->type != MPR_TYPE_OBJECT || argv[1]->type != MPR_TYPE_OBJECT) {
141                 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
142                 return -1;
143         }
144
145         /* get credential values from credentials object */
146         creds = mprGetPtr(argv[0], "creds");
147         if (creds == NULL) {
148                 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
149                 return -1;
150         }
151
152         remote_host = mprGetPtr(argv[1], "socket_address");
153         if (remote_host == NULL) {
154                 ejsSetErrorMsg(eid, "userAuth requires a socket address second parameter");
155                 return -1;
156         }
157
158         tmp_ctx = talloc_new(mprMemCtx());      
159         
160         username    = cli_credentials_get_username(creds);
161         password    = cli_credentials_get_password(creds);
162         domain      = cli_credentials_get_domain(creds);
163         workstation = cli_credentials_get_workstation(creds);
164
165         if (username == NULL || password == NULL || domain == NULL) {
166                 mpr_Return(eid, mprCreateUndefinedVar());
167                 talloc_free(tmp_ctx);
168                 return 0;
169         }
170
171         auth = mprObject("auth");
172
173         if (domain && (strcmp("SYSTEM USER", domain) == 0)) {
174                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, auth_types_unix);
175         } else {
176                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, lp_auth_methods());
177         }
178
179         mpr_Return(eid, auth);
180         talloc_free(tmp_ctx);
181         return 0;
182 }
183
184 /*
185   initialise credentials ejs object
186 */
187 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
188 {
189         struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
190         struct auth_session_info *session_info = system_session(mprMemCtx());
191
192         if (session_info == NULL) {
193                 return -1;
194         }
195
196         mprSetPtrChild(obj, "session_info", session_info);
197         return 0;
198 }
199
200 /*
201   setup C functions that be called from ejs
202 */
203 NTSTATUS smb_setup_ejs_auth(void)
204 {
205         ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
206         ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);
207         return NT_STATUS_OK;
208 }