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