r12804: This patch reworks the Samba4 sockets layer to use a socket_address
[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                       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         const char *auth_types[] = { authtype, NULL };
39         NTSTATUS nt_status;
40
41         /*
42           darn, we need some way to get the right event_context here
43         */
44         nt_status = auth_context_create(tmp_ctx, auth_types, &auth_context, NULL);
45         if (!NT_STATUS_IS_OK(nt_status)) {
46                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
47                 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
48                 goto done;
49         }
50
51         user_info = talloc(tmp_ctx, struct auth_usersupplied_info);
52         if (!user_info) {
53                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
54                 mprSetPropertyValue(auth, "report", mprString("talloc failed"));
55                 goto done;
56         }
57
58         user_info->mapped_state = True;
59         user_info->client.account_name = username;
60         user_info->mapped.account_name = username;
61         user_info->client.domain_name = domain;
62         user_info->mapped.domain_name = domain;
63
64         user_info->workstation_name = workstation;
65
66         user_info->remote_host = NULL;
67
68         user_info->password_state = AUTH_PASSWORD_PLAIN;
69         user_info->password.plaintext = talloc_strdup(user_info, password);
70
71         user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME |
72                 USER_INFO_DONT_CHECK_UNIX_ACCOUNT;
73
74         user_info->logon_parameters = 0;
75
76         nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
77         if (!NT_STATUS_IS_OK(nt_status)) {
78                 mprSetPropertyValue(auth, "report", mprString("Login Failed"));
79                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
80                 goto done;
81         }
82
83         nt_status = auth_generate_session_info(tmp_ctx, server_info, &session_info);
84         if (!NT_STATUS_IS_OK(nt_status)) {
85                 mprSetPropertyValue(auth, "report", mprString("Session Info generation failed"));
86                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
87                 goto done;
88         }
89
90         talloc_steal(mprMemCtx(), session_info);
91         mprSetThisPtr(eid, "session_info", session_info);
92
93         mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
94         mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
95         mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
96
97 done:
98         return 0;
99 }
100
101 /*
102   perform user authentication, returning an array of results
103
104 */
105 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
106 {
107         TALLOC_CTX *tmp_ctx;
108         const char *username;
109         const char *password;
110         const char *domain;
111         const char *workstation;
112         struct MprVar auth;
113         struct cli_credentials *creds;
114
115         if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
116                 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
117                 return -1;
118         }
119
120         /* get credential values from credentials object */
121         creds = mprGetPtr(argv[0], "creds");
122         if (creds == NULL) {
123                 ejsSetErrorMsg(eid, "userAuth requires a 'creds' element");
124                 return -1;
125         }
126
127         tmp_ctx = talloc_new(mprMemCtx());      
128         
129         username    = cli_credentials_get_username(creds);
130         password    = cli_credentials_get_password(creds);
131         domain      = cli_credentials_get_domain(creds);
132         workstation = cli_credentials_get_workstation(creds);
133
134         if (username == NULL || password == NULL || domain == NULL) {
135                 mpr_Return(eid, mprCreateUndefinedVar());
136                 talloc_free(tmp_ctx);
137                 return 0;
138         }
139
140         auth = mprObject("auth");
141
142         if (domain && (strcmp("System User", domain) == 0)) {
143                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, "unix");
144         } else {
145                 ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, "sam");
146         }
147
148         mpr_Return(eid, auth);
149         talloc_free(tmp_ctx);
150         return 0;
151 }
152
153 /*
154   initialise credentials ejs object
155 */
156 static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
157 {
158         struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
159         struct auth_session_info *session_info = system_session(mprMemCtx());
160
161         if (session_info == NULL) {
162                 return -1;
163         }
164
165         mprSetPtrChild(obj, "session_info", session_info);
166         return 0;
167 }
168
169 /*
170   setup C functions that be called from ejs
171 */
172 void smb_setup_ejs_auth(void)
173 {
174         ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
175         ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);
176 }