r23792: convert Samba4 to GPLv3
[samba.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 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 "scripting/ejs/smbcalls.h"
25 #include "build.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();
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 /*
164  * jsonrpc_include() allows you to include jsonrpc files from a path based at
165  * "jsonrpc services directory =" in smb.conf.
166  */
167 static int jsonrpc_include(int eid, int argc, char **argv)
168 {
169         int ret = -1;
170         char *path;
171         char *emsg;
172         const char *jsonrpc_services_dir = lp_jsonrpc_services_dir();
173         struct MprVar result;
174
175
176         if (jsonrpc_services_dir == NULL || jsonrpc_services_dir == NULL) {
177                 ejsSetErrorMsg(eid, "'jsonrpc services directory' not set");
178                 return -1;
179         }
180
181         if (argc != 1) {
182                 mpr_Return(eid, mprCreateIntegerVar(-1));
183                 return 0;
184         }
185
186         path = talloc_asprintf(mprMemCtx(), "%s/%s",
187                                jsonrpc_services_dir,
188                                argv[0]);
189         if (path == NULL) {
190                 mpr_Return(eid, mprCreateIntegerVar(-1));
191                 return 0;
192         }
193
194         if (file_exist(path)) {
195                 ret = ejsEvalFile(eid, path, &result, &emsg);
196                 if (ret < 0) {
197                         ejsSetErrorMsg(eid, "Could not eval file");
198                         printf("file found; ret=%d (%s)\n", ret, emsg);
199                 }
200         }
201         
202         mpr_Return(eid, mprCreateIntegerVar(ret));
203         talloc_free(path);
204         return 0;
205 }
206
207
208 static void (*ejs_exception_handler) (const char *) = NULL;
209
210 _PUBLIC_ void ejs_exception(const char *reason)
211 {
212         ejs_exception_handler(reason);          
213 }
214
215 /*
216   setup C functions that be called from ejs
217 */
218 void smb_setup_ejs_functions(void (*exception_handler)(const char *))
219 {
220         init_module_fn static_init[] = STATIC_smbcalls_MODULES;
221         init_module_fn *shared_init;
222
223         ejs_exception_handler = exception_handler;
224
225         smb_setup_ejs_cli();
226         smb_setup_ejs_options();
227         smb_setup_ejs_credentials();
228         smb_setup_ejs_param();
229         smb_setup_ejs_literal();
230         
231         shared_init = load_samba_modules(NULL, "smbcalls");
232         
233         run_init_functions(static_init);
234         run_init_functions(shared_init);
235
236         talloc_free(shared_init);
237
238         ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
239         ejsDefineCFunction(-1, "nativeTypeOf", ejs_typeof_native, NULL, MPR_VAR_SCRIPT_HANDLE);
240         ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
241         ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE);
242         ejsDefineStringCFunction(-1, "jsonrpc_include", jsonrpc_include, NULL, MPR_VAR_SCRIPT_HANDLE);
243 }
244