r7078: - fix an uninitialised variable in smbscript
[kai/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    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/ejs/ejs.h"
25 #include "param/loadparm.h"
26 #include "lib/ldb/include/ldb.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, "null" },
39                 { MPR_TYPE_BOOL, "boolean" },
40                 { MPR_TYPE_CFUNCTION, "function" },
41                 { MPR_TYPE_FLOAT, "float" },
42                 { MPR_TYPE_INT, "int" },
43                 { MPR_TYPE_INT64, "int64" },
44                 { MPR_TYPE_OBJECT, "object" },
45                 { MPR_TYPE_FUNCTION, "function" },
46                 { MPR_TYPE_STRING, "string" },
47                 { MPR_TYPE_STRING_CFUNCTION, "function" }
48         };
49         int i;
50         const char *type = "unknown";
51
52         if (argc != 1) return -1;
53         
54         for (i=0;i<ARRAY_SIZE(types);i++) {
55                 if (argv[0]->type == types[i].type) {
56                         type = types[i].name;
57                         break;
58                 }
59         }
60
61         ejsSetReturnString(eid, type);
62         return 0;
63 }
64
65 /*
66   setup a return of a string list
67 */
68 static void ejs_returnlist(MprVarHandle eid, 
69                            const char *name, const char **list)
70 {
71         ejsSetReturnValue(eid, mprList(name, list));
72 }
73
74 /*
75   return a list of defined services
76 */
77 static int ejs_lpServices(MprVarHandle eid, int argc, char **argv)
78 {
79         int i;
80         const char **list = NULL;
81         if (argc != 0) return -1;
82         
83         for (i=0;i<lp_numservices();i++) {
84                 list = str_list_add(list, lp_servicename(i));
85         }
86         talloc_steal(mprMemCtx(), list);
87         ejs_returnlist(eid, "services", list);
88         return 0;
89 }
90
91
92 /*
93   allow access to loadparm variables from inside ejs scripts in swat
94   
95   can be called in 4 ways:
96
97     v = lpGet("type:parm");             gets a parametric variable
98     v = lpGet("share", "type:parm");    gets a parametric variable on a share
99     v = lpGet("parm");                  gets a global variable
100     v = lpGet("share", "parm");         gets a share variable
101
102   the returned variable is a ejs object. It is an array object for lists.  
103 */
104 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
105 {
106         struct parm_struct *parm = NULL;
107         void *parm_ptr = NULL;
108         int i;
109
110         if (argc < 1) return -1;
111
112         if (argc == 2) {
113                 /* its a share parameter */
114                 int snum = lp_servicenumber(argv[0]);
115                 if (snum == -1) {
116                         return -1;
117                 }
118                 if (strchr(argv[1], ':')) {
119                         /* its a parametric option on a share */
120                         const char *type = talloc_strndup(mprMemCtx(), 
121                                                           argv[1], 
122                                                           strcspn(argv[1], ":"));
123                         const char *option = strchr(argv[1], ':') + 1;
124                         const char *value;
125                         if (type == NULL || option == NULL) return -1;
126                         value = lp_get_parametric(snum, type, option);
127                         if (value == NULL) return -1;
128                         ejsSetReturnString(eid, value);
129                         return 0;
130                 }
131
132                 parm = lp_parm_struct(argv[1]);
133                 if (parm == NULL || parm->class == P_GLOBAL) {
134                         return -1;
135                 }
136                 parm_ptr = lp_parm_ptr(snum, parm);
137         } else if (strchr(argv[0], ':')) {
138                 /* its a global parametric option */
139                 const char *type = talloc_strndup(mprMemCtx(), 
140                                                   argv[0], strcspn(argv[0], ":"));
141                 const char *option = strchr(argv[0], ':') + 1;
142                 const char *value;
143                 if (type == NULL || option == NULL) return -1;
144                 value = lp_get_parametric(-1, type, option);
145                 if (value == NULL) return -1;
146                 ejsSetReturnString(eid, value);
147                 return 0;
148         } else {
149                 /* its a global parameter */
150                 parm = lp_parm_struct(argv[0]);
151                 if (parm == NULL) return -1;
152                 parm_ptr = parm->ptr;
153         }
154
155         if (parm == NULL || parm_ptr == NULL) {
156                 return -1;
157         }
158
159         /* construct and return the right type of ejs object */
160         switch (parm->type) {
161         case P_STRING:
162         case P_USTRING:
163                 ejsSetReturnString(eid, *(char **)parm_ptr);
164                 break;
165         case P_BOOL:
166                 ejsSetReturnValue(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
167                 break;
168         case P_INTEGER:
169                 ejsSetReturnValue(eid, mprCreateIntegerVar(*(int *)parm_ptr));
170                 break;
171         case P_ENUM:
172                 for (i=0; parm->enum_list[i].name; i++) {
173                         if (*(int *)parm_ptr == parm->enum_list[i].value) {
174                                 ejsSetReturnString(eid, parm->enum_list[i].name);
175                                 return 0;
176                         }
177                 }
178                 return -1;      
179         case P_LIST: 
180                 ejs_returnlist(eid, parm->label, *(const char ***)parm_ptr);
181                 break;
182         case P_SEP:
183                 return -1;
184         }
185         return 0;
186 }
187
188
189 /*
190   perform an ldb search, returning an array of results
191
192   syntax:
193      ldbSearch("dbfile", "expression");
194      var attrs = new Array("attr1", "attr2", "attr3");
195      ldbSearch("dbfile", "expression", attrs);
196 */
197 static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv)
198 {
199         const char **attrs = NULL;
200         const char *expression, *dbfile;
201         TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
202         struct ldb_context *ldb;
203         int ret;
204         struct ldb_message **res;
205
206         /* validate arguments */
207         if (argc < 2 || argc > 3 ||
208             argv[0]->type != MPR_TYPE_STRING) {
209                 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
210                 goto failed;
211         }
212         if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
213                 ejsSetErrorMsg(eid, "ldbSearch attributes must be an object");
214                 goto failed;
215         }
216
217         dbfile     = mprToString(argv[0]);
218         expression = mprToString(argv[1]);
219         if (argc > 2) {
220                 attrs = mprToList(tmp_ctx, argv[2]);
221         }
222         if (dbfile == NULL || expression == NULL) {
223                 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
224                 goto failed;
225         }
226
227         ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
228         if (ldb == NULL) {
229                 ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile);
230                 goto failed;
231         }
232
233         ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
234         if (ret == -1) {
235                 ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb));
236                 goto failed;
237         }
238
239         ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message"));
240
241         talloc_free(tmp_ctx);
242         return 0;
243
244 failed:
245         talloc_free(tmp_ctx);
246         return -1;
247 }
248
249
250 /*
251   setup the C functions that be called from ejs
252 */
253 void smb_setup_ejs_functions(void)
254 {
255         ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE);
256         ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE);
257         ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
258         ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE);
259 }