r25026: Move param/param.h out of includes.h
[abartlet/samba.git/.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 "build.h"
27 #include "version.h"
28
29 /*
30   return the type of a variable
31 */
32 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
33 {
34         const struct {
35                 MprType type;
36                 const char *name;
37         } types[] = {
38                 { MPR_TYPE_UNDEFINED,        "undefined" },
39                 { MPR_TYPE_NULL,             "object" },
40                 { MPR_TYPE_BOOL,             "boolean" },
41                 { MPR_TYPE_CFUNCTION,        "function" },
42                 { MPR_TYPE_FLOAT,            "number" },
43                 { MPR_TYPE_INT,              "number" },
44                 { MPR_TYPE_INT64,            "number" },
45                 { MPR_TYPE_OBJECT,           "object" },
46                 { MPR_TYPE_FUNCTION,         "function" },
47                 { MPR_TYPE_STRING,           "string" },
48                 { MPR_TYPE_STRING_CFUNCTION, "function" },
49                 { MPR_TYPE_PTR,              "pointer" }
50         };
51         int i;
52         const char *type = NULL;
53
54         if (argc != 1) return -1;
55         
56         for (i=0;i<ARRAY_SIZE(types);i++) {
57                 if (argv[0]->type == types[i].type) {
58                         type = types[i].name;
59                         break;
60                 }
61         }
62         if (type == NULL) return -1;
63
64         mpr_ReturnString(eid, type);
65         return 0;
66 }
67
68 /*
69   return the native type of a variable
70 */
71 static int ejs_typeof_native(MprVarHandle eid, int argc, struct MprVar **argv)
72 {
73         const struct {
74                 MprType type;
75                 const char *name;
76         } types[] = {
77                 { MPR_TYPE_UNDEFINED,        "undefined" },
78                 { MPR_TYPE_NULL,             "null" },
79                 { MPR_TYPE_BOOL,             "boolean" },
80                 { MPR_TYPE_CFUNCTION,        "c_function" },
81                 { MPR_TYPE_FLOAT,            "float" },
82                 { MPR_TYPE_INT,              "integer" },
83                 { MPR_TYPE_INT64,            "integer64" },
84                 { MPR_TYPE_OBJECT,           "object" },
85                 { MPR_TYPE_FUNCTION,         "js_function" },
86                 { MPR_TYPE_STRING,           "string" },
87                 { MPR_TYPE_STRING_CFUNCTION, "string_c_function" },
88                 { MPR_TYPE_PTR,              "pointer" }
89         };
90         int i;
91         const char *type = NULL;
92
93         if (argc != 1) return -1;
94         
95         for (i=0;i<ARRAY_SIZE(types);i++) {
96                 if (argv[0]->type == types[i].type) {
97                         type = types[i].name;
98                         break;
99                 }
100         }
101         if (type == NULL) return -1;
102
103         mpr_ReturnString(eid, type);
104         return 0;
105 }
106
107 /*
108   libinclude() allows you to include js files using a search path specified
109   in "js include =" in smb.conf. 
110 */
111 static int ejs_libinclude(int eid, int argc, char **argv)
112 {
113         int i, j;
114         const char **js_include = lp_js_include();
115
116         if (js_include == NULL || js_include[0] == NULL) {
117                 ejsSetErrorMsg(eid, "js include path not set");
118                 return -1;
119         }
120
121         for (i = 0; i < argc; i++) {
122                 const char *script = argv[i];
123                 struct MprVar result;
124                 char *path, *emsg;
125                 int ret;
126
127                 /* use specfied path to search for requested file */
128                 for (j=0;js_include[j];j++) {
129                         path = talloc_asprintf(mprMemCtx(), "%s/%s", js_include[j], script);
130                         if (path == NULL) {
131                                 return -1;
132                         }
133                         if (file_exist(path)) {
134
135                                 ret = ejsEvalFile(eid, path, &result, &emsg);
136                                 talloc_free(path);
137                                 if (ret < 0) {
138                                         ejsSetErrorMsg(eid, "%s: %s", script, emsg);
139                                         return -1;
140                                 }
141                                 break;
142                         }
143                         talloc_free(path);
144                 }
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 services directory =" 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_services_dir = lp_jsonrpc_services_dir();
174         struct MprVar result;
175
176
177         if (jsonrpc_services_dir == NULL || jsonrpc_services_dir == NULL) {
178                 ejsSetErrorMsg(eid, "'jsonrpc services directory' 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",
188                                jsonrpc_services_dir,
189                                argv[0]);
190         if (path == NULL) {
191                 mpr_Return(eid, mprCreateIntegerVar(-1));
192                 return 0;
193         }
194
195         if (file_exist(path)) {
196                 ret = ejsEvalFile(eid, path, &result, &emsg);
197                 if (ret < 0) {
198                         ejsSetErrorMsg(eid, "Could not eval file");
199                         printf("file found; ret=%d (%s)\n", ret, emsg);
200                 }
201         }
202         
203         mpr_Return(eid, mprCreateIntegerVar(ret));
204         talloc_free(path);
205         return 0;
206 }
207
208
209 static void (*ejs_exception_handler) (const char *) = NULL;
210
211 _PUBLIC_ void ejs_exception(const char *reason)
212 {
213         ejs_exception_handler(reason);          
214 }
215
216 /*
217   setup C functions that be called from ejs
218 */
219 void smb_setup_ejs_functions(void (*exception_handler)(const char *))
220 {
221         init_module_fn static_init[] = STATIC_smbcalls_MODULES;
222         init_module_fn *shared_init;
223
224         ejs_exception_handler = exception_handler;
225
226         smb_setup_ejs_cli();
227         smb_setup_ejs_options();
228         smb_setup_ejs_credentials();
229         smb_setup_ejs_param();
230         smb_setup_ejs_literal();
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