3e96f8bef2a296d448f986b9cfbd4a83f32c3987
[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 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 "smb_build.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   libinclude() allows you to include js files using a search path specified
70   in "js include =" in smb.conf. 
71 */
72 static int ejs_libinclude(int eid, int argc, char **argv)
73 {
74         int i, j;
75         const char **js_include = lp_js_include();
76
77         if (js_include == NULL || js_include[0] == NULL) {
78                 ejsSetErrorMsg(eid, "js include path not set");
79                 return -1;
80         }
81
82         for (i = 0; i < argc; i++) {
83                 const char *script = argv[i];
84
85                 for (j=0;js_include[j];j++) {
86                         char *path;
87                         path = talloc_asprintf(mprMemCtx(), "%s/%s", js_include[j], script);
88                         if (path == NULL) {
89                                 return -1;
90                         }
91                         if (file_exist(path)) {
92                                 int ret;
93                                 struct MprVar result;
94                                 char *emsg;
95
96                                 ret = ejsEvalFile(eid, path, &result, &emsg);
97                                 talloc_free(path);
98                                 if (ret < 0) {
99                                         ejsSetErrorMsg(eid, "%s: %s", script, emsg);
100                                         return -1;
101                                 }
102                                 break;
103                         }
104                         talloc_free(path);
105                 }
106                 if (js_include[j] == NULL) {
107                         ejsSetErrorMsg(eid, "unable to include '%s'", script);
108                         return -1;
109                 }
110         }
111         return 0;
112 }
113
114
115 /*
116   setup C functions that be called from ejs
117 */
118 void smb_setup_ejs_functions(void)
119 {
120         init_module_fn static_init[] = STATIC_SMBCALLS_MODULES;
121         init_module_fn *shared_init;
122
123         smb_setup_ejs_config();
124         smb_setup_ejs_ldb();
125         smb_setup_ejs_nbt();
126         smb_setup_ejs_cli();
127         smb_setup_ejs_auth();
128         smb_setup_ejs_options();
129         smb_setup_ejs_nss();
130         smb_setup_ejs_string();
131         smb_setup_ejs_random();
132         smb_setup_ejs_system();
133         smb_setup_ejs_credentials();
134         smb_setup_ejs_samba3();
135         smb_setup_ejs_param();
136         smb_setup_ejs_datablob();
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 }
150