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