Merge commit 'release-4-0-0alpha1' into v4-0-test
[kai/samba.git] / source / scripting / ejs / mprutil.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    utility functions for manipulating mpr variables in ejs calls
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/appweb/ejs/ejs.h"
24 #include "lib/ldb/include/ldb.h"
25 #include "scripting/ejs/smbcalls.h"
26
27 /*
28   return a default mpr object
29 */
30 struct MprVar mprObject(const char *name)
31 {
32         return ejsCreateObj(name && *name?name:"(NULL)", MPR_DEFAULT_HASH_SIZE);
33 }
34
35 /*
36   return a empty mpr array
37 */
38 struct MprVar mprArray(const char *name)
39 {
40         return ejsCreateArray(name && *name?name:"(NULL)", 0);
41 }
42
43 /*
44   find a mpr component, allowing for sub objects, using the '.' convention
45 */
46  NTSTATUS mprGetVar(struct MprVar **v, const char *name)
47 {
48         const char *p = strchr(name, '.');
49         char *objname;
50         NTSTATUS status;
51         if (p == NULL) {
52                 *v = mprGetProperty(*v, name, NULL);
53                 if (*v == NULL) {
54                         DEBUG(1,("mprGetVar unable to find '%s'\n", name));
55                         return NT_STATUS_INVALID_PARAMETER;
56                 }
57                 return NT_STATUS_OK;
58         }
59         objname = talloc_strndup(mprMemCtx(), name, p-name);
60         NT_STATUS_HAVE_NO_MEMORY(objname);
61         *v = mprGetProperty(*v, objname, NULL);
62         NT_STATUS_HAVE_NO_MEMORY(*v);
63         status = mprGetVar(v, p+1);
64         talloc_free(objname);
65         return status;
66 }
67
68
69 /*
70   set a mpr component, allowing for sub objects, using the '.' convention
71   destroys 'val' after setting
72 */
73  NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val)
74 {
75         const char *p = strchr(name, '.');
76         char *objname;
77         struct MprVar *v2;
78         NTSTATUS status;
79         if (p == NULL) {
80                 v2 = mprSetProperty(v, name, &val);
81                 if (v2 == NULL) {
82                         DEBUG(1,("mprSetVar unable to set '%s'\n", name));
83                         return NT_STATUS_INVALID_PARAMETER_MIX;
84                 }
85                 mprDestroyVar(&val);
86                 return NT_STATUS_OK;
87         }
88         objname = talloc_strndup(mprMemCtx(), name, p-name);
89         if (objname == NULL) {
90                 return NT_STATUS_NO_MEMORY;
91         }
92         v2 = mprGetProperty(v, objname, NULL);
93         if (v2 == NULL) {
94                 mprSetVar(v, objname, mprObject(objname));
95                 v2 = mprGetProperty(v, objname, NULL);
96         }
97         status = mprSetVar(v2, p+1, val);
98         talloc_free(objname);
99         return status;
100 }
101
102
103
104 /*
105   add an indexed array element to a property
106 */
107  void mprAddArray(struct MprVar *var, int i, struct MprVar v)
108 {
109         char idx[16];
110         mprItoa(i, idx, sizeof(idx));
111         mprSetVar(var, idx, v);
112 }
113
114 /*
115   construct a MprVar from a list
116 */
117 struct MprVar mprList(const char *name, const char **list)
118 {
119         struct MprVar var;
120         int i;
121
122         var = mprArray(name);
123         for (i=0;list && list[i];i++) {
124                 mprAddArray(&var, i, mprString(list[i]));
125         }
126         return var;
127 }
128
129 /*
130   construct a MprVar from a string, using NULL if needed
131 */
132 struct MprVar mprString(const char *s)
133 {
134         if (s == NULL) {
135                 return mprCreatePtrVar(NULL);
136         }
137         return mprCreateStringVar(s, true);
138 }
139
140 /*
141   construct a string MprVar from a lump of data
142 */
143 struct MprVar mprData(const uint8_t *p, size_t length)
144 {
145         struct MprVar var;
146         char *s = talloc_strndup(mprMemCtx(), (const char *)p, length);
147         if (s == NULL) {
148                 return mprCreateUndefinedVar();
149         }
150         var = mprString(s);
151         talloc_free(s);
152         return var;
153 }
154
155 /*
156   turn a ldb_message into a ejs object variable
157 */
158 static struct MprVar mprLdbMessage(struct ldb_context *ldb, struct ldb_message *msg)
159 {
160         struct MprVar var;
161         int i;
162         /* we force some attributes to always be an array in the
163            returned structure. This makes the scripting easier, as you don't 
164            need a special case for the single value case */
165         const char *multivalued[] = { "objectClass", "memberOf", "privilege", 
166                                             "member", NULL };
167
168         var = mprObject(ldb_dn_alloc_linearized(msg, msg->dn));
169
170         for (i=0;i<msg->num_elements;i++) {
171                 struct ldb_message_element *el = &msg->elements[i];
172                 struct MprVar val;
173                 const struct ldb_schema_attribute *a;
174                 struct ldb_val v;
175
176                 a = ldb_schema_attribute_by_name(ldb, el->name);
177                 if (a == NULL) {
178                         goto failed;
179                 }
180
181                 if (el->num_values == 1 &&
182                     !str_list_check_ci(multivalued, el->name)) {
183                         if (a->syntax->ldif_write_fn(ldb, msg, &el->values[0], &v) != 0) {
184                                 goto failed;
185                         }
186                         /* FIXME: nasty hack, remove me when ejs will support
187                          * arbitrary string and does not truncate on \0 */
188                         if (strlen((char *)v.data) != v.length) {
189                                 val = mprDataBlob(v);
190                         } else {
191                                 val = mprData(v.data, v.length);
192                         }
193                 } else {
194                         int j;
195                         val = mprArray(el->name);
196                         for (j=0;j<el->num_values;j++) {
197                                 if (a->syntax->ldif_write_fn(ldb, msg, 
198                                                              &el->values[j], &v) != 0) {
199                                         goto failed;
200                                 }
201                                 /* FIXME: nasty hack, remove me when ejs will support
202                                  * arbitrary string and does not truncate on \0 */
203                                 if (strlen((char *)v.data) != v.length) {
204                                         mprAddArray(&val, j, mprDataBlob(v));
205                                 } else {
206                                         mprAddArray(&val, j, mprData(v.data, v.length));
207                                 }
208                         }
209                 }
210                 mprSetVar(&var, el->name, val);
211         }
212
213         /* add the dn if it is not already specified */
214         if (mprGetProperty(&var, "dn", 0) == 0) {
215                 mprSetVar(&var, "dn", mprString(ldb_dn_alloc_linearized(msg, msg->dn)));
216         }
217         
218         return var;             
219 failed:
220         return mprCreateUndefinedVar();
221 }
222
223
224 /*
225   build a MprVar result object for ldb operations with lots of funky properties
226 */
227 struct MprVar mprLdbResult(struct ldb_context *ldb, int err, struct ldb_result *result)
228 {
229         struct MprVar ret;
230         struct MprVar ary;
231
232         ret = mprObject("ldbret");
233
234         mprSetVar(&ret, "error", mprCreateIntegerVar(err));
235         mprSetVar(&ret, "errstr", mprString(ldb_errstring(ldb)));
236
237         ary = mprArray("ldb_message");
238         if (result) {
239                 int i;
240
241                 for (i = 0; i < result->count; i++) {
242                         mprAddArray(&ary, i, mprLdbMessage(ldb, result->msgs[i]));
243                 }
244         }
245
246         mprSetVar(&ret, "msgs", ary);
247
248         /* TODO: add referrals, exteded ops, and controls */
249
250         return ret;
251 }
252
253
254 /*
255   turn a MprVar string variable into a const char *
256  */
257 const char *mprToString(struct MprVar *v)
258 {
259         if (v->trigger) {
260                 mprReadProperty(v, 0);
261         }
262         if (!mprVarIsString(v->type)) return NULL;
263         return v->string;
264 }
265
266 /*
267   turn a MprVar integer variable into an int
268  */
269 int mprToInt(struct MprVar *v)
270 {
271         if (v->trigger) {
272                 mprReadProperty(v, 0);
273         }
274         if (!mprVarIsNumber(v->type)) return 0;
275         return mprVarToNumber(v);
276 }
277
278 /*
279   turn a MprVar object variable into a string list
280   this assumes the object variable consists only of strings
281 */
282 const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v)
283 {
284         const char **list = NULL;
285         struct MprVar *el;
286
287         if (v->type != MPR_TYPE_OBJECT ||
288             v->properties == NULL) {
289                 return NULL;
290         }
291         for (el=mprGetFirstProperty(v, MPR_ENUM_DATA);
292              el;
293              el=mprGetNextProperty(v, el, MPR_ENUM_DATA)) {
294                 const char *s = mprToString(el);
295                 if (s) {
296                         list = str_list_add(list, s);
297                 }
298         }
299         talloc_steal(mem_ctx, list);
300         return list;
301 }
302
303
304 /*
305   turn a MprVar object variable into a string list
306   this assumes the object variable is an array of strings
307 */
308 const char **mprToArray(TALLOC_CTX *mem_ctx, struct MprVar *v)
309 {
310         const char **list = NULL;
311         struct MprVar *len;
312         int length, i;
313
314         len = mprGetProperty(v, "length", NULL);
315         if (len == NULL) {
316                 return NULL;
317         }
318         length = mprToInt(len);
319
320         for (i=0;i<length;i++) {
321                 char idx[16];
322                 struct MprVar *vs;
323                 mprItoa(i, idx, sizeof(idx));           
324                 vs = mprGetProperty(v, idx, NULL);
325                 if (vs == NULL || vs->type != MPR_TYPE_STRING) {
326                         talloc_free(list);
327                         return NULL;
328                 }
329                 list = str_list_add(list, mprToString(vs));
330         }
331         talloc_steal(mem_ctx, list);
332         return list;
333 }
334
335 /*
336   turn a NTSTATUS into a MprVar object with lots of funky properties
337 */
338 struct MprVar mprNTSTATUS(NTSTATUS status)
339 {
340         struct MprVar res;
341
342         res = mprObject("ntstatus");
343
344         mprSetVar(&res, "errstr", mprString(nt_errstr(status)));
345         mprSetVar(&res, "v", mprCreateIntegerVar(NT_STATUS_V(status)));
346         mprSetVar(&res, "is_ok", mprCreateBoolVar(NT_STATUS_IS_OK(status)));
347         mprSetVar(&res, "is_err", mprCreateBoolVar(NT_STATUS_IS_ERR(status)));
348
349         return res;
350 }
351
352 /*
353   create a data-blob in a mpr variable
354 */
355 struct MprVar mprDataBlob(DATA_BLOB blob)
356 {
357         struct MprVar res;
358         struct datablob *pblob = talloc(mprMemCtx(), struct datablob);
359         *pblob = data_blob_talloc(pblob, blob.data, blob.length);
360
361         res = mprObject("DATA_BLOB");
362
363         mprSetVar(&res, "size", mprCreateIntegerVar(blob.length));
364         mprSetPtrChild(&res, "blob", pblob);
365
366         return res;
367 }
368
369 /*
370   return a data blob from a mpr var created using mprDataBlob
371 */
372 struct datablob *mprToDataBlob(struct MprVar *v)
373 {
374         return talloc_get_type(mprGetPtr(v, "blob"), struct datablob);
375 }
376
377 /*
378   turn a WERROR into a MprVar object with lots of funky properties
379 */
380 struct MprVar mprWERROR(WERROR status)
381 {
382         struct MprVar res;
383
384         res = mprObject("werror");
385
386         mprSetVar(&res, "errstr", mprString(win_errstr(status)));
387         mprSetVar(&res, "v", mprCreateIntegerVar(W_ERROR_V(status)));
388         mprSetVar(&res, "is_ok", mprCreateBoolVar(W_ERROR_IS_OK(status)));
389         mprSetVar(&res, "is_err", mprCreateBoolVar(!W_ERROR_IS_OK(status)));
390
391         return res;
392 }
393
394
395 /*
396   set a pointer in a existing MprVar
397 */
398 void mprSetPtr(struct MprVar *v, const char *propname, const void *p)
399 {
400         mprSetVar(v, propname, mprCreatePtrVar(discard_const(p)));
401 }
402
403 /*
404   set a pointer in a existing MprVar, freeing it when the property goes away
405 */
406 void mprSetPtrChild(struct MprVar *v, const char *propname, const void *p)
407 {
408         mprSetVar(v, propname, mprCreatePtrVar(discard_const(p)));
409         v = mprGetProperty(v, propname, NULL);
410         v->allocatedData = 1;
411         talloc_steal(mprMemCtx(), p);
412 }
413
414 /*
415   get a pointer from a MprVar
416 */
417 void *mprGetPtr(struct MprVar *v, const char *propname)
418 {
419         struct MprVar *val;
420         val = mprGetProperty(v, propname, NULL);
421         if (val == NULL) {
422                 return NULL;
423         }
424         if (val->type != MPR_TYPE_PTR) {
425                 return NULL;
426         }
427         return val->ptr;
428 }
429
430 /*
431   set the return value then free the variable
432 */
433  void mpr_Return(int eid, struct MprVar v)
434
435         ejsSetReturnValue(eid, v);
436         mprDestroyVar(&v);
437 }
438
439 /*
440   set the return value then free the variable
441 */
442 void mpr_ReturnString(int eid, const char *s)
443
444         mpr_Return(eid, mprString(s));
445 }
446
447
448 /*
449   set a C function in a variable
450 */
451  void mprSetCFunction(struct MprVar *obj, const char *name, MprCFunction fn)
452 {
453         mprSetVar(obj, name, mprCreateCFunctionVar(fn, obj, MPR_VAR_SCRIPT_HANDLE));
454 }
455
456 /*
457   set a string C function in a variable
458 */
459  void mprSetStringCFunction(struct MprVar *obj, const char *name, MprStringCFunction fn)
460 {
461         mprSetVar(obj, name, mprCreateStringCFunctionVar(fn, obj, MPR_VAR_SCRIPT_HANDLE));
462 }
463
464 /*
465   get a pointer in the current object
466 */
467 void *mprGetThisPtr(int eid, const char *name)
468 {
469         struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0);
470         return mprGetPtr(this, name);
471 }
472
473 /*
474   set a pointer as a child of the local object
475 */
476 void mprSetThisPtr(int eid, const char *name, void *ptr)
477 {
478         struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0);
479         mprSetPtrChild(this, name, ptr);
480 }
481
482 /*
483   used by object xxx_init() routines to allow for the caller
484   to supply a pre-existing object to add properties to,
485   or create a new object. This makes inheritance easy
486 */
487 struct MprVar *mprInitObject(int eid, const char *name, int argc, struct MprVar **argv)
488 {
489         if (argc > 0 && mprVarIsObject(argv[0]->type)) {
490                 return argv[0];
491         }
492         mpr_Return(eid, mprObject(name));
493         return ejsGetReturnValue(eid);
494 }