r12931: Remove some prefixes. We have:
[ab/samba.git/.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
79         /* Don't give the game away (any difference between no such
80          * user and wrong password) */
81         nt_status = auth_nt_status_squash(nt_status);
82
83         if (!NT_STATUS_IS_OK(nt_status)) {
84                 mprSetPropertyValue(auth, "report", 
85                                     mprString(talloc_strdup(mprMemCtx(), get_friendly_nt_error_msg(nt_status))));
86                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
87                 goto done;
88         }
89
90         nt_status = auth_generate_session_info(tmp_ctx, server_info, &session_info);
91         if (!NT_STATUS_IS_OK(nt_status)) {
92                 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
93                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
94                 goto done;
95         }
96
97         session_info_obj = mprInitObject(eid, "session_info", 0, NULL);
98
99         mprSetPtrChild(session_info_obj, "session_info", session_info);
100         talloc_steal(mprMemCtx(), session_info);
101
102         mprSetProperty(auth, "session_info", session_info_obj);
103         mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
104         mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
105         mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
106
107 done:
108         return 0;
109 }
110
111 /*
112   perform user authentication, returning an array of results
113
114 */
115 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
116 {
117         TALLOC_CTX *tmp_ctx;
118         const char *username;
119         const char *password;
120         const char *domain;
121         const char *workstation;
122         struct MprVar auth;
123         struct cli_credentials *creds;
124         struct socket_address *remote_host;
125
126         if (argc != 2 || argv[0]->type != MPR_TYPE_OBJECT || argv[1]->type != MPR_TYPE_OBJECT) {
127                 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
128                 return -1;
129         }
130
131         /* get credential values from credentials object */
132         creds = mprGetPtr(argv[0], "creds");
133         if (creds == NULL) {
134                 ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
135                 return -1;
136         }
137
138         remote_host = mprGetPtr(argv[1], "socket_address");
139         if (remote_host == NULL) {
140                 ejsSetErrorMsg(eid, "userAuth requires a socket address second parameter");
141                 return -1;
142         }
143
144         tmp_ctx = talloc_new(mprMemCtx());      
145         
146         username    = cli_credentials_get_username(creds);
147         password    = cli_credentials_get_password(creds);
148         domain      = cli_credentials_get_domain(creds);
149         workstation = cli_credentials_get_workstation(creds);
150
151         if (username == NULL || password == NULL || domain == NULL) {
152                 mpr_Return(eid, mprCreateUndefinedVar());
153                 talloc_free(tmp_ctx);
154                 return 0;
155         }
156
157         auth = mprObject("auth");
158
159         if (domain && (strcmp("SYSTEM USER", domain) == 0)) {
160                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, "unix");
161         } else {
162                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, "sam");
163         }
164
165         mpr_Return(eid, auth);
166         talloc_free(tmp_ctx);
167         return 0;
168 }
169
170 /*
171   initialise credentials ejs object
172 */
173 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
174 {
175         struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
176         struct auth_session_info *session_info = system_session(mprMemCtx());
177
178         if (session_info == NULL) {
179                 return -1;
180         }
181
182         mprSetPtrChild(obj, "session_info", session_info);
183         return 0;
184 }
185
186 /*
187   setup C functions that be called from ejs
188 */
189 void smb_setup_ejs_auth(void)
190 {
191         ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
192         ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);
193 }