r19165: handle errors better for jsonrpc. generate an error object whenever possible
[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/appweb/ejs/ejs.h"
26 #include "scripting/ejs/smbcalls.h"
27 #include "build.h"
28 #include "version.h"
29
30 /*
31   return the type of a variable
32 */
33 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
34 {
35         const struct {
36                 MprType type;
37                 const char *name;
38         } types[] = {
39                 { MPR_TYPE_UNDEFINED,        "undefined" },
40                 { MPR_TYPE_NULL,             "object" },
41                 { MPR_TYPE_BOOL,             "boolean" },
42                 { MPR_TYPE_CFUNCTION,        "function" },
43                 { MPR_TYPE_FLOAT,            "number" },
44                 { MPR_TYPE_INT,              "number" },
45                 { MPR_TYPE_INT64,            "number" },
46                 { MPR_TYPE_OBJECT,           "object" },
47                 { MPR_TYPE_FUNCTION,         "function" },
48                 { MPR_TYPE_STRING,           "string" },
49                 { MPR_TYPE_STRING_CFUNCTION, "function" },
50                 { MPR_TYPE_PTR,              "pointer" }
51         };
52         int i;
53         const char *type = NULL;
54
55         if (argc != 1) return -1;
56         
57         for (i=0;i<ARRAY_SIZE(types);i++) {
58                 if (argv[0]->type == types[i].type) {
59                         type = types[i].name;
60                         break;
61                 }
62         }
63         if (type == NULL) return -1;
64
65         mpr_ReturnString(eid, type);
66         return 0;
67 }
68
69 /*
70   return the native type of a variable
71 */
72 static int ejs_typeof_native(MprVarHandle eid, int argc, struct MprVar **argv)
73 {
74         const struct {
75                 MprType type;
76                 const char *name;
77         } types[] = {
78                 { MPR_TYPE_UNDEFINED,        "undefined" },
79                 { MPR_TYPE_NULL,             "null" },
80                 { MPR_TYPE_BOOL,             "boolean" },
81                 { MPR_TYPE_CFUNCTION,        "c_function" },
82                 { MPR_TYPE_FLOAT,            "float" },
83                 { MPR_TYPE_INT,              "integer" },
84                 { MPR_TYPE_INT64,            "integer64" },
85                 { MPR_TYPE_OBJECT,           "object" },
86                 { MPR_TYPE_FUNCTION,         "js_function" },
87                 { MPR_TYPE_STRING,           "string" },
88                 { MPR_TYPE_STRING_CFUNCTION, "string_c_function" },
89                 { MPR_TYPE_PTR,              "pointer" }
90         };
91         int i;
92         const char *type = NULL;
93
94         if (argc != 1) return -1;
95         
96         for (i=0;i<ARRAY_SIZE(types);i++) {
97                 if (argv[0]->type == types[i].type) {
98                         type = types[i].name;
99                         break;
100                 }
101         }
102         if (type == NULL) return -1;
103
104         mpr_ReturnString(eid, type);
105         return 0;
106 }
107
108 /*
109   libinclude() allows you to include js files using a search path specified
110   in "js include =" in smb.conf. 
111 */
112 static int ejs_libinclude(int eid, int argc, char **argv)
113 {
114         int i, j;
115         const char **js_include = lp_js_include();
116
117         if (js_include == NULL || js_include[0] == NULL) {
118                 ejsSetErrorMsg(eid, "js include path not set");
119                 return -1;
120         }
121
122         for (i = 0; i < argc; i++) {
123                 const char *script = argv[i];
124
125                 for (j=0;js_include[j];j++) {
126                         char *path;
127                         path = talloc_asprintf(mprMemCtx(), "%s/%s", js_include[j], script);
128                         if (path == NULL) {
129                                 return -1;
130                         }
131                         if (file_exist(path)) {
132                                 int ret;
133                                 struct MprVar result;
134                                 char *emsg;
135
136                                 ret = ejsEvalFile(eid, path, &result, &emsg);
137                                 talloc_free(path);
138                                 if (ret < 0) {
139                                         ejsSetErrorMsg(eid, "%s: %s", script, emsg);
140                                         return -1;
141                                 }
142                                 break;
143                         }
144                         talloc_free(path);
145                 }
146                 if (js_include[j] == NULL) {
147                         ejsSetErrorMsg(eid, "unable to include '%s'", script);
148                         return -1;
149                 }
150         }
151         return 0;
152 }
153
154 /*
155   return the current version
156 */
157 static int ejs_version(MprVarHandle eid, int argc, struct MprVar **argv)
158 {
159         mpr_ReturnString(eid, SAMBA_VERSION_STRING);
160         return 0;
161 }
162
163
164 /*
165  * jsonrpc_include() allows you to include jsonrpc files from a path based at
166  * "jsonrpc base =" in smb.conf.
167  */
168 static int jsonrpc_include(int eid, int argc, char **argv)
169 {
170         int ret = -1;
171         char *path;
172         char *emsg;
173         const char *jsonrpc_base = lp_jsonrpc_base();
174         struct MprVar result;
175
176
177         if (jsonrpc_base == NULL || jsonrpc_base == NULL) {
178                 ejsSetErrorMsg(eid, "js include path not set");
179                 return -1;
180         }
181
182         if (argc != 1) {
183                 mpr_Return(eid, mprCreateIntegerVar(-1));
184                 return 0;
185         }
186
187         path = talloc_asprintf(mprMemCtx(), "%s/%s", jsonrpc_base, argv[0]);
188         if (path == NULL) {
189                 mpr_Return(eid, mprCreateIntegerVar(-1));
190                 return 0;
191         }
192
193         if (file_exist(path)) {
194                 ret = ejsEvalFile(eid, path, &result, &emsg);
195                 if (ret < 0) {
196                         ejsSetErrorMsg(eid, "Could not eval file");
197                         printf("file found; ret=%d (%s)\n", ret, emsg);
198                 }
199         }
200         
201         mpr_Return(eid, mprCreateIntegerVar(ret));
202         talloc_free(path);
203         return 0;
204 }
205
206
207 static void (*ejs_exception_handler) (const char *) = NULL;
208
209 _PUBLIC_ void ejs_exception(const char *reason)
210 {
211         ejs_exception_handler(reason);          
212 }
213
214 /*
215   setup C functions that be called from ejs
216 */
217 void smb_setup_ejs_functions(void (*exception_handler)(const char *))
218 {
219         init_module_fn static_init[] = STATIC_smbcalls_MODULES;
220         init_module_fn *shared_init;
221
222         ejs_exception_handler = exception_handler;
223
224         smb_setup_ejs_cli();
225         smb_setup_ejs_options();
226         smb_setup_ejs_credentials();
227         smb_setup_ejs_param();
228         smb_setup_ejs_literal();
229         
230         ejsnet_setup();
231
232         shared_init = load_samba_modules(NULL, "smbcalls");
233         
234         run_init_functions(static_init);
235         run_init_functions(shared_init);
236
237         talloc_free(shared_init);
238
239         ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
240         ejsDefineCFunction(-1, "nativeTypeOf", ejs_typeof_native, NULL, MPR_VAR_SCRIPT_HANDLE);
241         ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
242         ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE);
243         ejsDefineStringCFunction(-1, "jsonrpc_include", jsonrpc_include, NULL, MPR_VAR_SCRIPT_HANDLE);
244 }
245