ef3b86a8b4818631fd31e15187c4db8159edc4bc
[ira/wip.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(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username, 
30                       const char *password, const char *domain, const char *remote_host,
31                       const char *authtype)
32 {
33         struct auth_usersupplied_info *user_info = NULL;
34         struct auth_serversupplied_info *server_info = NULL;
35         struct auth_context *auth_context;
36         const char *auth_types[] = { authtype, NULL };
37         NTSTATUS nt_status;
38         DATA_BLOB pw_blob;
39
40         /*
41           darn, we need some way to get the right event_context here
42         */
43         nt_status = auth_context_create(tmp_ctx, auth_types, &auth_context, NULL);
44         if (!NT_STATUS_IS_OK(nt_status)) {
45                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
46                 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
47                 goto done;
48         }
49
50         pw_blob = data_blob(password, strlen(password)+1);
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 = remote_host;
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         nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
76         if (!NT_STATUS_IS_OK(nt_status)) {
77                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
78                 mprSetPropertyValue(auth, "report", mprString("Login Failed"));
79                 goto done;
80         }
81
82         mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
83         mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
84         mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
85
86 done:
87         return 0;
88 }
89
90 /*
91   perform user authentication, returning an array of results
92
93   syntax:
94     var authinfo = new Object();
95     authinfo.username = myname;
96     authinfo.password = mypass;
97     authinfo.domain = mydom;
98     authinfo.rhost = request['REMOTE_HOST'];
99     auth = userAuth(authinfo);
100 */
101 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
102 {
103         TALLOC_CTX *tmp_ctx;
104         const char *username;
105         const char *password;
106         const char *domain;
107         const char *remote_host;
108         struct MprVar auth;
109         struct cli_credentials *creds;
110
111         if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
112                 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
113                 return -1;
114         }
115
116         /* get credential values from credentials object */
117         creds = mprGetPtr(argv[0], "creds");
118         if (creds == NULL) {
119                 ejsSetErrorMsg(eid, "userAuth requires a 'creds' element");
120                 return -1;
121         }
122         username    = cli_credentials_get_username(creds);
123         password    = cli_credentials_get_password(creds);
124         domain      = cli_credentials_get_domain(creds);
125         remote_host = cli_credentials_get_workstation(creds);
126
127         if (username == NULL || password == NULL || domain == NULL) {
128                 mpr_Return(eid, mprCreateUndefinedVar());
129                 return 0;
130         }
131
132         tmp_ctx = talloc_new(mprMemCtx());      
133         auth = mprObject("auth");
134
135         if (domain && strcmp("System User", domain) == 0) {
136                 ejs_doauth(tmp_ctx, &auth, username, password, domain, remote_host, "unix");
137         } else {
138                 ejs_doauth(tmp_ctx, &auth, username, password, domain, remote_host, "sam");
139         }
140
141         mpr_Return(eid, auth);
142         talloc_free(tmp_ctx);
143         return 0;
144 }
145
146 /*
147   setup C functions that be called from ejs
148 */
149 void smb_setup_ejs_auth(void)
150 {
151         ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
152 }