5aab8cf2c27bad30c0c8a013166838d5ed785b05
[bbaumbach/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 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   libinclude() allows you to include js files using a search path specified
71   in "js include =" in smb.conf. 
72 */
73 static int ejs_libinclude(int eid, int argc, char **argv)
74 {
75         int i, j;
76         const char **js_include = lp_js_include();
77
78         if (js_include == NULL || js_include[0] == NULL) {
79                 ejsSetErrorMsg(eid, "js include path not set");
80                 return -1;
81         }
82
83         for (i = 0; i < argc; i++) {
84                 const char *script = argv[i];
85
86                 for (j=0;js_include[j];j++) {
87                         char *path;
88                         path = talloc_asprintf(mprMemCtx(), "%s/%s", js_include[j], script);
89                         if (path == NULL) {
90                                 return -1;
91                         }
92                         if (file_exist(path)) {
93                                 int ret;
94                                 struct MprVar result;
95                                 char *emsg;
96
97                                 ret = ejsEvalFile(eid, path, &result, &emsg);
98                                 talloc_free(path);
99                                 if (ret < 0) {
100                                         ejsSetErrorMsg(eid, "%s: %s", script, emsg);
101                                         return -1;
102                                 }
103                                 break;
104                         }
105                         talloc_free(path);
106                 }
107                 if (js_include[j] == NULL) {
108                         ejsSetErrorMsg(eid, "unable to include '%s'", script);
109                         return -1;
110                 }
111         }
112         return 0;
113 }
114
115 /*
116   return the current version
117 */
118 static int ejs_version(MprVarHandle eid, int argc, struct MprVar **argv)
119 {
120         mpr_ReturnString(eid, SAMBA_VERSION_STRING);
121         return 0;
122 }
123
124
125 /*
126   setup C functions that be called from ejs
127 */
128 void smb_setup_ejs_functions(void)
129 {
130         init_module_fn static_init[] = STATIC_smbcalls_MODULES;
131         init_module_fn *shared_init;
132
133         smb_setup_ejs_cli();
134         smb_setup_ejs_options();
135         smb_setup_ejs_credentials();
136         smb_setup_ejs_param();
137         
138         ejsnet_setup();
139
140         shared_init = load_samba_modules(NULL, "ejs");
141         
142         run_init_functions(static_init);
143         run_init_functions(shared_init);
144
145         talloc_free(shared_init);
146
147         ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
148         ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
149         ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE);
150 }
151