r12823: Fix up the provison and newuser code in SWAT. This also cleans up the
[kai/samba.git] / source4 / 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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "auth/auth.h"
27 #include "scripting/ejs/smbcalls.h"
28
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)
33 {
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 };
40         NTSTATUS nt_status;
41
42         /*
43           darn, we need some way to get the right event_context here
44         */
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"));
49                 goto done;
50         }
51
52         user_info = talloc(tmp_ctx, struct auth_usersupplied_info);
53         if (!user_info) {
54                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
55                 mprSetPropertyValue(auth, "report", mprString("talloc failed"));
56                 goto done;
57         }
58
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;
64
65         user_info->workstation_name = workstation;
66
67         user_info->remote_host = remote_host;
68
69         user_info->password_state = AUTH_PASSWORD_PLAIN;
70         user_info->password.plaintext = talloc_strdup(user_info, password);
71
72         user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME |
73                 USER_INFO_DONT_CHECK_UNIX_ACCOUNT;
74
75         user_info->logon_parameters = 0;
76
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));
83                 goto done;
84         }
85
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));
90                 goto done;
91         }
92
93         session_info_obj = mprInitObject(eid, "session_info", 0, NULL);
94
95         mprSetPtrChild(session_info_obj, "session_info", session_info);
96         talloc_steal(mprMemCtx(), session_info);
97
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));
102
103 done:
104         return 0;
105 }
106
107 /*
108   perform user authentication, returning an array of results
109
110 */
111 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
112 {
113         TALLOC_CTX *tmp_ctx;
114         const char *username;
115         const char *password;
116         const char *domain;
117         const char *workstation;
118         struct MprVar auth;
119         struct cli_credentials *creds;
120         struct socket_address *remote_host;
121
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.");
124                 return -1;
125         }
126
127         /* get credential values from credentials object */
128         creds = mprGetPtr(argv[0], "creds");
129         if (creds == NULL) {
130                 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
131                 return -1;
132         }
133
134         remote_host = mprGetPtr(argv[1], "socket_address");
135         if (remote_host == NULL) {
136                 ejsSetErrorMsg(eid, "userAuth requires a socket address second parameter");
137                 return -1;
138         }
139
140         tmp_ctx = talloc_new(mprMemCtx());      
141         
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);
146
147         if (username == NULL || password == NULL || domain == NULL) {
148                 mpr_Return(eid, mprCreateUndefinedVar());
149                 talloc_free(tmp_ctx);
150                 return 0;
151         }
152
153         auth = mprObject("auth");
154
155         if (domain && (strcmp("SYSTEM USER", domain) == 0)) {
156                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, "unix");
157         } else {
158                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, "sam");
159         }
160
161         mpr_Return(eid, auth);
162         talloc_free(tmp_ctx);
163         return 0;
164 }
165
166 /*
167   initialise credentials ejs object
168 */
169 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
170 {
171         struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
172         struct auth_session_info *session_info = system_session(mprMemCtx());
173
174         if (session_info == NULL) {
175                 return -1;
176         }
177
178         mprSetPtrChild(obj, "session_info", session_info);
179         return 0;
180 }
181
182 /*
183   setup C functions that be called from ejs
184 */
185 void smb_setup_ejs_auth(void)
186 {
187         ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
188         ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);
189 }