r22882: It seems entirly reasonable to follow metze's suggestion and check for
[Samba.git] / services / samba / ldb.esp
blobf69abde3886a15a5b38e22a0461a0cf97b3f9f8e
1 <%
2 /*
3  * Copyright:
4  *   (C) 2006 by Derrell Lipman
5  *       All rights reserved
6  *
7  * License:
8  *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
9  */
12  * JSON-RPC mappings to the ldb ejs functions
13  */
15 /* We'll be saving resources in the session */
16 jsonrpc_include("resources.esp");
19 /**
20  * Local function to determine if the requested database is one which we allow
21  * access to.
22  *
23  * @param dbRequested
24  *   Name of the database which is being requested to be opened
25  *
26  * @return
27  *   true if access is allowed; false otherwise.
28  */
29 function accessAllowed(dbRequested)
31     /* Databases allowed to connect to */
32     dbAllowed = new Array();
33     dbAllowed[dbAllowed.length] = "sam.ldb";
35     for (var i = 0; i < dbAllowed.length; i++)
36     {
37         if (dbRequested == dbAllowed[i])
38         {
39             return true;
40         }
41     }
43     return false;
47 /**
48  * Connect to a database
49  *
50  * @param params[0]
51  *   Database name
52  *
53  * @param params[1..n]
54  *   Option (e.g. "modules:modlist")
55  *
56  * @param error
57  *   An object of class JsonRpcError.
58  *
59  * @return
60  *   Success: The resource id to be used for future access to the database
61  *   Failure: error event
62  *
63  * @note
64  *   Credentials or session_info may be set up first.
65  */
66 function _connect(params, error)
68     if (params.length < 1)
69     {
70         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
71                        "usage: <db_name> [<option> ...]");
72         return error;
73     }
75     /* First, see if this database was already opened */
76     var resourceId = session.resources.find("ldb:" + params[0], error);
77     if (resourceId != undefined)
78     {
79         /* It was.  Give 'em the resource id */
80         return resourceId;
81     }
83     /* Ensure that the database name is one that is allowed to be opened */
84     if (! accessAllowed(params[0]))
85     {
86         error.setError(-1, "Invalid or disallowed database name");
87         return error;
88     }
90     /* Get access to loadparm functions */
91     var lp = loadparm_init();
93     /* Determine the private directory */
94     var private_dir = lp.get("private dir");
96     /* Database was not previously opened.  Connect to it. */
97     ldb = ldb_init();
98     ldb.session_info = session.authinfo.session_info;
99     ldb.credentials = session.authinfo.credentials;
100     var ret = ldb.connect(private_dir + "/" + params[0]);
101     if (ret && ldb.db)
102     {
103         return session.resources.set(ldb, "ldb:" + params[0], error);
104     }
105     else
106     {
107         error.setError(-1, "ldb.connect failed");
108         return error;
109     }
111 jsonrpc.method.connect = _connect;
115  * Close a database
117  * @param params[0]
118  *   The resource id of the open database, previously returned by connect()
120  * @param error
121  *   An object of class JsonRpcError.
123  * @return
124  *   Success: True
125  *   Failure: Will only fail with invalid parameters, and throws an error
126  */
127 function _close(params, error)
129     if (params.length != 1)
130     {
131         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
132                        "usage: <resource_id>");
133         return error;
134     }
136     ldb = session.resources.get(params[0], error);
137     if (ldb["__type"] == "_JsonRpcError")
138     {
139         return ldb;
140     }
142     var ret = ldb.close();
143     
144     /* If close succeeded, release the stored resource */
145     if (ret)
146     {
147         session.resources.release(params[0], error);
148     }
150     return ret;
152 jsonrpc.method.close = _close;
156  * Begin a transaction
158  * @param params[0]
159  *   The resource id of the open database, previously returned by connect()
161  * @param error
162  *   An object of class JsonRpcError.
164  * @return
165  *   Success: True
166  *   Failure: False
167  */
168 function _transaction_start(params, error)
170     if (params.length != 1)
171     {
172         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
173                        "usage: <resource_id>");
174         return error;
175     }
177     ldb = session.resources.get(params[0], error);
178     if (ldb["__type"] == "_JsonRpcError")
179     {
180         return ldb;
181     }
183     return ldb.transaction_start();
185 jsonrpc.method.transaction_start = _transaction_start;
189  * Cancel a transaction
191  * @param params[0]
192  *   The resource id of the open database, previously returned by connect()
194  * @param error
195  *   An object of class JsonRpcError.
197  * @return
198  *   Success: True
199  *   Failure: False
200  */
201 function _transaction_cancel(params, error)
203     if (params.length != 1)
204     {
205         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
206                        "usage: <resource_id>");
207         return error;
208     }
210     ldb = session.resources.get(params[0], error);
211     if (ldb["__type"] == "_JsonRpcError")
212     {
213         return ldb;
214     }
216     return ldb.transaction_cancel();
218 jsonrpc.method.transaction_cancel = _transaction_cancel;
222  * Commit a transaction
224  * @param params[0]
225  *   The resource id of the open database, previously returned by connect()
227  * @param error
228  *   An object of class JsonRpcError.
230  * @return
231  *   Success: True
232  *   Failure: False
233  */
234 function _transaction_commit(params, error)
236     if (params.length != 1)
237     {
238         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
239                        "usage: <resource_id>");
240         return error;
241     }
243     ldb = session.resources.get(params[0], error);
244     if (ldb["__type"] == "_JsonRpcError")
245     {
246         return ldb;
247     }
249     return ldb.transaction_commit();
251 jsonrpc.method.transaction_commit = _transaction_commit;
255  * Issue a Search request
257  * @param params[0]
258  *   The resource id of the open database, previously returned by connect()
260  * @param params[1]
261  *   Search expression
263  * @param params[2]
264  *   Base DN
266  * @param params[3]
267  *   Scope: "default", "base", "one" or "subtree"
269  * @param params[4]
270  *   Attributes: an array object
272  * @param error
273  *   An object of class JsonRpcError.
275  * @return
276  *   Success: found object
277  *   Failure: `undefined`
279  * @note
280  *   If params[4] is missing, assume no attributes
281  *   If params[3..4] are missing, also assume "default" scope
282  *   If params[2..4] are missing, also assume null base DN
283  */
284 function _search(params, error)
286     if (params.length < 2 || params.length > 5)
287     {
288         error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
289         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
290                        "usage: " +
291                        "<resource_id> <expr> [<baseDN> [<scope> [<attrs>]]]");
292         return error;
293     }
295     ldb = session.resources.get(params[0], error);
296     if (ldb["__type"] == "_JsonRpcError")
297     {
298         return ldb;
299     }
301     /* Retrieve parameters */
302     var expr = params[1];
303     var baseDN = params[2];
304     var scope = params[3];
305     var attrs = params[4];
307     /* Fill in optional parameters */
308     if (params.length < 3) baseDN = null;
309     if (params.length < 4) scope = "one";
310     if (params.length < 5) attrs = null;
312     /* Determine scope value */
313     if (scope == "base")
314     {
315         scope = ldb.SCOPE_BASE;
316     }
317     else if (scope == "one")
318     {
319         scope = ldb.SCOPE_ONE;
320     }
321     else if (scope == "subtree")
322     {
323         scope = ldb.SCOPE_SUBTREE;
324     }
325     else if (scope == "default")
326     {
327         scope = ldb.SCOPE_DEFAULT;
328     }
329     else
330     {
331         error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
332         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
333                        "invalid scope: " + scope);
334         return error;
335     }
337     var res = ldb.search(expr, baseDN, scope, attrs);
339     if (res.error != 0) {
340         error.setError(res.error, res.errstr);
341         return error;
342     }
344     return res.msgs;
346 jsonrpc.method.search = _search;
350  * Add data to the database
352  * @param params[0]
353  *   The resource id of the open database, previously returned by connect()
355  * @param params[1]
356  *   An LDIF string representing the data to be added
358  * @param error
359  *   An object of class JsonRpcError.
361  * @return
362  *   Success: True
363  *   Failure: False
364  */
365 function _add(params, error)
367     if (params.length != 2)
368     {
369         error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
370         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
371                        "usage: <resource_id> <ldif>");
372         return error;
373     }
375     ldb = session.resources.get(params[0], error);
376     if (ldb["__type"] == "_JsonRpcError")
377     {
378         return ldb;
379     }
381     var res = ldb.add(params[1]);
382     if (res.error != 0) {
383         error.setError(res.error, res.errstr);
384         return error;
385     }
387     return true;
389 jsonrpc.method.add = _add;
393  * Modify data in the database
395  * @param params[0]
396  *   The resource id of the open database, previously returned by connect()
398  * @param params[1]
399  *   An LDIF string representing the data to be modified
401  * @param error
402  *   An object of class JsonRpcError.
404  * @return
405  *   Success: True
406  *   Failure: False
407  */
408 function _modify(params, error)
410     if (params.length != 2)
411     {
412         error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
413         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
414                        "usage: <resource_id> <ldif>");
415         return error;
416     }
418     ldb = session.resources.get(params[0], error);
419     if (ldb["__type"] == "_JsonRpcError")
420     {
421         return ldb;
422     }
424     var res = ldb.modify(params[1]);
425     if (res.error != 0) {
426         error.setError(res.error, res.errstr);
427         return error;
428     }
430     return true;
432 jsonrpc.method.modify = _modify;
436  * Delete data from the database
438  * @param params[0]
439  *   The resource id of the open database, previously returned by connect()
441  * @param params[1]
442  *   The DN to be located and deleted
444  * @param error
445  *   An object of class JsonRpcError.
447  * @return
448  *   Success: True
449  *   Failure: False
450  */
451 function _del(params, error)
453     if (params.length != 2)
454     {
455         error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
456         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
457                        "usage: <resource_id> <dn>");
458         return error;
459     }
461     ldb = session.resources.get(params[0], error);
462     if (ldb["__type"] == "_JsonRpcError")
463     {
464         return ldb;
465     }
467     var res = ldb.del(params[1]);
468     if (res.error != 0) {
469         error.setError(res.error, res.errstr);
470         return error;
471     }
473     return true;
475 jsonrpc.method.del = _del;
479  * Rename data in the database
481  * @param params[0]
482  *   The resource id of the open database, previously returned by connect()
484  * @param params[1]
485  *   The DN to be renamed
487  * @param params[2]
488  *   The new name for the DN being renamed
490  * @param error
491  *   An object of class JsonRpcError.
493  * @return
494  *   Success: True
495  *   Failure: False
496  */
497 function _rename(params, error)
499     if (params.length != 3)
500     {
501         error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
502         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
503                        "usage: <resource_id> <old_dn> <new_dn>");
504         return error;
505     }
507     ldb = session.resources.get(params[0], error);
508     if (ldb["__type"] == "_JsonRpcError")
509     {
510         return ldb;
511     }
513     var res = ldb.rename(params[1], params[2]);
514     if (res.error != 0) {
515         error.setError(res.error, res.errstr);
516         return error;
517     }
519     return true;
521 jsonrpc.method.rename = _rename;
525  * Base64-encode a string
527  * @param params[0]
528  *   The resource id of the open database, previously returned by connect()
530  * @param params[1]
531  *   The string to be base64 encoded
533  * @param error
534  *   An object of class JsonRpcError.
536  * @return
537  *   Success: encoded string
538  *   Failure: `undefined`
539  */
540 function _base64encode(params, error)
542     if (params.length != 2)
543     {
544         error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
545         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
546                        "usage: <resource_id> <string_to_be_encoded>");
547         return error;
548     }
550     ldb = session.resources.get(params[0], error);
551     if (ldb["__type"] == "_JsonRpcError")
552     {
553         return ldb;
554     }
556     return ldb.base64encode(params[1]);
558 jsonrpc.method.base64encode = _base64encode;
562  * Base64-decode a string
564  * @param params[0]
565  *   The resource id of the open database, previously returned by connect()
567  * @param params[1]
568  *   The string to be base64 decoded
570  * @param error
571  *   An object of class JsonRpcError.
573  * @return
574  *   Success: decoded string
575  *   Failure: `undefined`
576  */
577 function _base64decode(params, error)
579     if (params.length != 2)
580     {
581         error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
582         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
583                        "usage: <resource_id> <string_to_be_decoded>");
584         return error;
585     }
587     ldb = session.resources.get(params[0], error);
588     if (ldb["__type"] == "_JsonRpcError")
589     {
590         return ldb;
591     }
593     return ldb.base64decode(params[1]);
595 jsonrpc.method.base64decode = _base64decode;
599  * escape a DN
601  * @param params[0]
602  *   The resource id of the open database, previously returned by connect()
604  * @param params[1]
605  *   The DN to be escaped
607  * @param error
608  *   An object of class JsonRpcError.
610  * @return
611  *   Success: escaped string
612  *   Failure: undefined
613  */
614 function _base64decode(params, error)
616     if (params.length != 2)
617     {
618         error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
619         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
620                        "usage: <resource_id> <string_to_be_decoded>");
621         return error;
622     }
624     ldb = session.resources.get(params[0], error);
625     if (ldb["__type"] == "_JsonRpcError")
626     {
627         return ldb;
628     }
630     return ldb.base64decode(params[1]);
632 jsonrpc.method.base64decode = _base64decode;
636  * Retrieve a description of the most recent error
638  * @param params[0]
639  *   The resource id of the open database, previously returned by connect()
641  * @param error
642  *   An object of class JsonRpcError.
644  * @return
645  *   The most recent error string for the ldb specified by the resource id
646  */
647 function _errstring(params, error)
649     if (params.length != 1)
650     {
651         error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
652         error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
653                        "usage: <resource_id>");
654         return error;
655     }
657     ldb = session.resources.get(params[0], error);
658     if (ldb["__type"] == "_JsonRpcError")
659     {
660         return ldb;
661     }
663     return ldb.errstring();
665 jsonrpc.method.errstring = _errstring;
671  * Local Variables:
672  * mode: c
673  * End:
674  */