keep obsolete file in samba4 source directory.
[kai/samba-autobuild/.git] / source4 / 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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "param/param.h"
25 #include "scripting/ejs/smbcalls.h"
26 #include "version.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,              "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         mpr_ReturnString(eid, type);
64         return 0;
65 }
66
67 /*
68   return the native type of a variable
69 */
70 static int ejs_typeof_native(MprVarHandle eid, int argc, struct MprVar **argv)
71 {
72         const struct {
73                 MprType type;
74                 const char *name;
75         } types[] = {
76                 { MPR_TYPE_UNDEFINED,        "undefined" },
77                 { MPR_TYPE_NULL,             "null" },
78                 { MPR_TYPE_BOOL,             "boolean" },
79                 { MPR_TYPE_CFUNCTION,        "c_function" },
80                 { MPR_TYPE_FLOAT,            "float" },
81                 { MPR_TYPE_INT,              "integer" },
82                 { MPR_TYPE_INT64,            "integer64" },
83                 { MPR_TYPE_OBJECT,           "object" },
84                 { MPR_TYPE_FUNCTION,         "js_function" },
85                 { MPR_TYPE_STRING,           "string" },
86                 { MPR_TYPE_STRING_CFUNCTION, "string_c_function" },
87                 { MPR_TYPE_PTR,              "pointer" }
88         };
89         int i;
90         const char *type = NULL;
91
92         if (argc != 1) return -1;
93         
94         for (i=0;i<ARRAY_SIZE(types);i++) {
95                 if (argv[0]->type == types[i].type) {
96                         type = types[i].name;
97                         break;
98                 }
99         }
100         if (type == NULL) return -1;
101
102         mpr_ReturnString(eid, type);
103         return 0;
104 }
105
106 /*
107   libinclude() allows you to include js files using a search path specified
108   in "js include =" in smb.conf. 
109 */
110 static int ejs_libinclude(int eid, int argc, char **argv)
111 {
112         int i, j;
113         const char **js_include = lp_js_include(mprLpCtx());
114
115         if (js_include == NULL || js_include[0] == NULL) {
116                 ejsSetErrorMsg(eid, "js include path not set");
117                 return -1;
118         }
119
120         for (i = 0; i < argc; i++) {
121                 const char *script = argv[i];
122                 struct MprVar result;
123                 char *path, *emsg;
124                 int ret;
125
126                 /* use specfied path to search for requested file */
127                 for (j=0;js_include[j];j++) {
128                         path = talloc_asprintf(mprMemCtx(), "%s/%s", js_include[j], script);
129                         if (path == NULL) {
130                                 return -1;
131                         }
132                         if (file_exist(path)) {
133
134                                 ret = ejsEvalFile(eid, path, &result, &emsg);
135                                 talloc_free(path);
136                                 if (ret < 0) {
137                                         ejsSetErrorMsg(eid, "%s: %s", script, emsg);
138                                         return -1;
139                                 }
140                                 break;
141                         }
142                         talloc_free(path);
143                 }
144
145                 if (js_include[j] == NULL) {
146                         ejsSetErrorMsg(eid, "unable to include '%s'", script);
147                         return -1;
148                 }
149         }
150         return 0;
151 }
152
153 /*
154   return the current version
155 */
156 static int ejs_version(MprVarHandle eid, int argc, struct MprVar **argv)
157 {
158         mpr_ReturnString(eid, SAMBA_VERSION_STRING);
159         return 0;
160 }
161
162
163 static void (*ejs_exception_handler) (const char *) = NULL;
164
165 _PUBLIC_ void ejs_exception(const char *reason)
166 {
167         ejs_exception_handler(reason);          
168 }
169
170 /*
171   setup C functions that be called from ejs
172 */
173 void smb_setup_ejs_functions(void (*exception_handler)(const char *))
174 {
175         extern NTSTATUS ejs_init_security(void);
176         extern NTSTATUS ejs_init_initshutdown(void);
177         extern NTSTATUS smb_setup_ejs_reg(void);
178         extern NTSTATUS smb_setup_ejs_string(void);
179         extern NTSTATUS ejs_init_lsarpc(void);
180         extern NTSTATUS ejs_init_rpcecho(void);
181         extern NTSTATUS ejs_init_winreg(void);
182         extern NTSTATUS smb_setup_ejs_random(void);
183         extern NTSTATUS smb_setup_ejs_config(void);
184         extern NTSTATUS ejs_init_misc(void);
185         extern NTSTATUS ejs_init_netdfs(void);
186         extern NTSTATUS smb_setup_ejs_datablob(void);
187         extern NTSTATUS smb_setup_ejs_auth(void);
188         extern NTSTATUS smb_setup_ejs_nss(void);
189         extern NTSTATUS ejs_init_samr(void);
190         extern NTSTATUS ejs_init_wkssvc(void);
191         extern NTSTATUS smb_setup_ejs_system(void);
192         extern NTSTATUS smb_setup_ejs_ldb(void);
193         extern NTSTATUS ejs_init_svcctl(void);
194         extern NTSTATUS smb_setup_ejs_net(void);
195         extern NTSTATUS ejs_init_srvsvc(void);
196         extern NTSTATUS ejs_init_netlogon(void);
197         extern NTSTATUS ejs_init_drsuapi(void);
198         extern NTSTATUS ejs_init_irpc(void);
199         extern NTSTATUS ejs_init_eventlog(void);
200         init_module_fn static_init[] = { STATIC_smbcalls_MODULES };
201         init_module_fn *shared_init;
202
203         ejs_exception_handler = exception_handler;
204
205         smb_setup_ejs_options();
206         smb_setup_ejs_credentials();
207         
208         shared_init = load_samba_modules(NULL, mprLpCtx(), "smbcalls");
209         
210         run_init_functions(static_init);
211         run_init_functions(shared_init);
212
213         talloc_free(shared_init);
214
215         ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
216         ejsDefineCFunction(-1, "nativeTypeOf", ejs_typeof_native, NULL, MPR_VAR_SCRIPT_HANDLE);
217         ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
218         ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE);
219 }
220