r8628: add retries to the normal paths of nbt name resolution. UDP broadcasts are...
[kai/samba.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 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_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username, const char *password, const char *domain, const char *remote_host)
30 {
31         struct auth_usersupplied_info *user_info = NULL;
32         struct auth_serversupplied_info *server_info = NULL;
33         struct auth_context *auth_context;
34         const char *auth_unix[] = { "unix", NULL };
35         NTSTATUS nt_status;
36         DATA_BLOB pw_blob;
37
38         /*
39           darn, we need some way to get the right event_context here
40         */
41         nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context, NULL);
42         if (!NT_STATUS_IS_OK(nt_status)) {
43                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
44                 mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
45                 goto done;
46         }
47
48         pw_blob = data_blob(password, strlen(password)+1),
49         make_user_info(tmp_ctx, username, username,
50                                 domain, domain,
51                                 remote_host, remote_host,
52                                 NULL, NULL,
53                                 NULL, NULL,
54                                 &pw_blob, False,
55                                 USER_INFO_CASE_INSENSITIVE_USERNAME |
56                                 USER_INFO_DONT_CHECK_UNIX_ACCOUNT,
57                                 &user_info);
58         nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
59         if (!NT_STATUS_IS_OK(nt_status)) {
60                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
61                 mprSetPropertyValue(auth, "report", mprString("Login Failed"));
62                 goto done;
63         }
64
65         mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
66         mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
67         mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
68
69 done:
70         return 0;
71 }
72
73 /*
74   perform user authentication, returning an array of results
75
76   syntax:
77     var authinfo = new Object();
78     authinfo.username = myname;
79     authinfo.password = mypass;
80     authinfo.domain = mydom;
81     authinfo.rhost = request['REMOTE_HOST'];
82     auth = userAuth(authinfo);
83 */
84 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
85 {
86         TALLOC_CTX *tmp_ctx;
87         const char *username;
88         const char *password;
89         const char *domain;
90         const char *remote_host;
91         struct MprVar auth;
92
93         if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
94                 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
95                 return -1;
96         }
97
98         username = mprToString(mprGetProperty(argv[0], "username", NULL));
99         password = mprToString(mprGetProperty(argv[0], "password", NULL));
100         domain = mprToString(mprGetProperty(argv[0], "domain", NULL));
101         remote_host = mprToString(mprGetProperty(argv[0], "rhost", NULL));
102
103         tmp_ctx = talloc_new(mprMemCtx());      
104         auth = mprObject("auth");
105
106         if (domain && strcmp("System User", domain) == 0) {
107
108                 ejs_systemAuth(tmp_ctx, &auth, username, password, domain, remote_host);
109         }  else {
110
111                 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
112                 mprSetPropertyValue(&auth, "report", mprString("Unknown Domain"));
113         }
114
115         mpr_Return(eid, auth);
116         talloc_free(tmp_ctx);
117         return 0;
118 }
119
120 static int ejs_domain_list(MprVarHandle eid, int argc, char **argv)
121 {
122         struct MprVar list;
123
124         if (argc != 0) {
125                 ejsSetErrorMsg(eid, "domList invalid arguments");
126                 return -1;
127         }
128
129         list = mprObject("list");
130         mprSetVar(&list, "0", mprString("System User"));
131
132         mpr_Return(eid, list);
133
134         return 0;
135 }
136
137 /*
138   setup C functions that be called from ejs
139 */
140 void smb_setup_ejs_auth(void)
141 {
142         ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE);
143         ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
144 }