r8069: the beginnings of code to allow rpc calls to be made from ejs
[jelmer/samba4-debian.git] / source / scripting / ejs / smbcalls.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide hooks into smbd C calls from ejs scripts
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Tim Potter 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/ejs/ejs.h"
26 #include "auth/auth.h"
27
28 /*
29   return the type of a variable
30 */
31 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
32 {
33         const struct {
34                 MprType type;
35                 const char *name;
36         } types[] = {
37                 { MPR_TYPE_UNDEFINED,        "undefined" },
38                 { MPR_TYPE_NULL,             "object" },
39                 { MPR_TYPE_BOOL,             "boolean" },
40                 { MPR_TYPE_CFUNCTION,        "function" },
41                 { MPR_TYPE_FLOAT,            "number" },
42                 { MPR_TYPE_INT,              "number" },
43                 { MPR_TYPE_INT64,            "number" },
44                 { MPR_TYPE_OBJECT,           "object" },
45                 { MPR_TYPE_FUNCTION,         "function" },
46                 { MPR_TYPE_STRING,           "string" },
47                 { MPR_TYPE_STRING_CFUNCTION, "function" },
48                 { MPR_TYPE_PTR,              "C pointer" }
49         };
50         int i;
51         const char *type = NULL;
52
53         if (argc != 1) return -1;
54         
55         for (i=0;i<ARRAY_SIZE(types);i++) {
56                 if (argv[0]->type == types[i].type) {
57                         type = types[i].name;
58                         break;
59                 }
60         }
61         if (type == NULL) return -1;
62
63         ejsSetReturnString(eid, type);
64         return 0;
65 }
66
67 /*
68   setup a return of a string list
69 */
70  void ejs_returnlist(MprVarHandle eid, const char *name, const char **list)
71 {
72         ejsSetReturnValue(eid, mprList(name, list));
73 }
74
75 static int ejs_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username, const char *password, const char *domain, const char *remote_host)
76 {
77         struct auth_usersupplied_info *user_info = NULL;
78         struct auth_serversupplied_info *server_info = NULL;
79         struct auth_context *auth_context;
80         const char *auth_unix[] = { "unix", NULL };
81         NTSTATUS nt_status;
82         DATA_BLOB pw_blob;
83
84         /*
85           darn, we need some way to get the right event_context here
86         */
87         nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context, NULL);
88         if (!NT_STATUS_IS_OK(nt_status)) {
89                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
90                 mprSetPropertyValue(auth, "report", mprCreateStringVar("Auth System Failure", 1));
91                 goto done;
92         }
93
94         pw_blob = data_blob(password, strlen(password)),
95         make_user_info(tmp_ctx, username, username,
96                                 domain, domain,
97                                 remote_host, remote_host,
98                                 NULL, NULL,
99                                 NULL, NULL,
100                                 &pw_blob, False,
101                                 USER_INFO_CASE_INSENSITIVE_USERNAME |
102                                 USER_INFO_DONT_CHECK_UNIX_ACCOUNT,
103                                 &user_info);
104         nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
105         if (!NT_STATUS_IS_OK(nt_status)) {
106                 mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
107                 mprSetPropertyValue(auth, "report", mprCreateStringVar("Login Failed", 1));
108                 goto done;
109         }
110
111         mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
112         mprSetPropertyValue(auth, "username", mprCreateStringVar(server_info->account_name, 1));
113         mprSetPropertyValue(auth, "domain", mprCreateStringVar(server_info->domain_name, 1));
114
115 done:
116         return 0;
117 }
118
119 /*
120   perform user authentication, returning an array of results
121
122   syntax:
123     var authinfo = new Object();
124     authinfo.username = myname;
125     authinfo.password = mypass;
126     authinfo.domain = mydom;
127     authinfo.rhost = request['REMOTE_HOST'];
128     auth = userAuth(authinfo);
129 */
130 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
131 {
132         TALLOC_CTX *tmp_ctx;
133         const char *username;
134         const char *password;
135         const char *domain;
136         const char *remote_host;
137         struct MprVar auth;
138
139         if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
140                 ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
141                 return -1;
142         }
143
144         username = mprToString(mprGetProperty(argv[0], "username", NULL));
145         password = mprToString(mprGetProperty(argv[0], "password", NULL));
146         domain = mprToString(mprGetProperty(argv[0], "domain", NULL));
147         remote_host = mprToString(mprGetProperty(argv[0], "rhost", NULL));
148
149         tmp_ctx = talloc_new(mprMemCtx());      
150         auth = mprCreateObjVar("auth", MPR_DEFAULT_HASH_SIZE);
151
152         if (strcmp("System User", domain) == 0) {
153
154                 ejs_systemAuth(tmp_ctx, &auth, username, password, domain, remote_host);
155         }  else {
156
157                 mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
158                 mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 1));
159         }
160
161         ejsSetReturnValue(eid, auth);
162         talloc_free(tmp_ctx);
163         return 0;
164 }
165
166 static int ejs_domain_list(MprVarHandle eid, int argc, char **argv)
167 {
168         struct MprVar list;
169         struct MprVar dom;
170
171         if (argc != 0) {
172                 ejsSetErrorMsg(eid, "domList invalid arguments");
173                 return -1;
174         }
175
176         list = mprCreateObjVar("list", MPR_DEFAULT_HASH_SIZE);
177         dom = mprCreateStringVar("System User", 1);
178         mprCreateProperty(&list, "0", &dom);
179
180         ejsSetReturnValue(eid, list);
181
182         return 0;
183 }
184
185
186 /*
187   setup C functions that be called from ejs
188 */
189 void smb_setup_ejs_functions(void)
190 {
191         smb_setup_ejs_config();
192         smb_setup_ejs_ldb();
193         smb_setup_ejs_nbt();
194         smb_setup_ejs_cli();
195         smb_setup_ejs_rpc();
196
197         ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
198         ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE);
199         ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
200 }