4 * (C) 2006 by Derrell Lipman
8 * LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
12 * JSON-RPC mappings to the ldb ejs functions
15 /* We'll be saving resources in the session */
16 jsonrpc_include("resources.esp");
20 * Local function to determine if the requested database is one which we allow
24 * Name of the database which is being requested to be opened
27 * true if access is allowed; false otherwise.
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++)
37 if (dbRequested == dbAllowed[i])
48 * Connect to a database
54 * Option (e.g. "modules:modlist")
57 * An object of class JsonRpcError.
60 * Success: The resource id to be used for future access to the database
61 * Failure: error event
64 * Credentials or session_info may be set up first.
66 function _connect(params, error)
68 if (params.length < 1)
70 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
71 "usage: <db_name> [<option> ...]");
75 /* First, see if this database was already opened */
76 var resourceId = session.resources.find("ldb:" + params[0], error);
77 if (resourceId != undefined)
79 /* It was. Give 'em the resource id */
83 /* Ensure that the database name is one that is allowed to be opened */
84 if (! accessAllowed(params[0]))
86 error.setError(-1, "Invalid or disallowed database name");
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. */
98 ldb.session_info = session.authinfo.session_info;
99 ldb.credentials = session.authinfo.credentials;
100 var ret = ldb.connect(private_dir + "/" + params[0]);
103 return session.resources.set(ldb, "ldb:" + params[0], error);
107 error.setError(-1, "ldb.connect failed");
111 jsonrpc.method.connect = _connect;
118 * The resource id of the open database, previously returned by connect()
121 * An object of class JsonRpcError.
125 * Failure: Will only fail with invalid parameters, and throws an error
127 function _close(params, error)
129 if (params.length != 1)
131 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
132 "usage: <resource_id>");
136 ldb = session.resources.get(params[0], error);
137 if (ldb["__type"] == "_JsonRpcError")
142 var ret = ldb.close();
144 /* If close succeeded, release the stored resource */
147 session.resources.release(params[0], error);
152 jsonrpc.method.close = _close;
156 * Begin a transaction
159 * The resource id of the open database, previously returned by connect()
162 * An object of class JsonRpcError.
168 function _transaction_start(params, error)
170 if (params.length != 1)
172 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
173 "usage: <resource_id>");
177 ldb = session.resources.get(params[0], error);
178 if (ldb["__type"] == "_JsonRpcError")
183 return ldb.transaction_start();
185 jsonrpc.method.transaction_start = _transaction_start;
189 * Cancel a transaction
192 * The resource id of the open database, previously returned by connect()
195 * An object of class JsonRpcError.
201 function _transaction_cancel(params, error)
203 if (params.length != 1)
205 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
206 "usage: <resource_id>");
210 ldb = session.resources.get(params[0], error);
211 if (ldb["__type"] == "_JsonRpcError")
216 return ldb.transaction_cancel();
218 jsonrpc.method.transaction_cancel = _transaction_cancel;
222 * Commit a transaction
225 * The resource id of the open database, previously returned by connect()
228 * An object of class JsonRpcError.
234 function _transaction_commit(params, error)
236 if (params.length != 1)
238 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
239 "usage: <resource_id>");
243 ldb = session.resources.get(params[0], error);
244 if (ldb["__type"] == "_JsonRpcError")
249 return ldb.transaction_commit();
251 jsonrpc.method.transaction_commit = _transaction_commit;
255 * Issue a Search request
258 * The resource id of the open database, previously returned by connect()
267 * Scope: "default", "base", "one" or "subtree"
270 * Attributes: an array object
273 * An object of class JsonRpcError.
276 * Success: found object
277 * Failure: `undefined`
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
284 function _search(params, error)
286 if (params.length < 2 || params.length > 5)
288 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
289 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
291 "<resource_id> <expr> [<baseDN> [<scope> [<attrs>]]]");
295 ldb = session.resources.get(params[0], error);
296 if (ldb["__type"] == "_JsonRpcError")
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 */
315 scope = ldb.SCOPE_BASE;
317 else if (scope == "one")
319 scope = ldb.SCOPE_ONE;
321 else if (scope == "subtree")
323 scope = ldb.SCOPE_SUBTREE;
325 else if (scope == "default")
327 scope = ldb.SCOPE_DEFAULT;
331 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
332 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
333 "invalid scope: " + scope);
337 var res = ldb.search(expr, baseDN, scope, attrs);
339 if (res.error != 0) {
340 error.setError(res.error, res.errstr);
346 jsonrpc.method.search = _search;
350 * Add data to the database
353 * The resource id of the open database, previously returned by connect()
356 * An LDIF string representing the data to be added
359 * An object of class JsonRpcError.
365 function _add(params, error)
367 if (params.length != 2)
369 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
370 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
371 "usage: <resource_id> <ldif>");
375 ldb = session.resources.get(params[0], error);
376 if (ldb["__type"] == "_JsonRpcError")
381 var res = ldb.add(params[1]);
382 if (res.error != 0) {
383 error.setError(res.error, res.errstr);
389 jsonrpc.method.add = _add;
393 * Modify data in the database
396 * The resource id of the open database, previously returned by connect()
399 * An LDIF string representing the data to be modified
402 * An object of class JsonRpcError.
408 function _modify(params, error)
410 if (params.length != 2)
412 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
413 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
414 "usage: <resource_id> <ldif>");
418 ldb = session.resources.get(params[0], error);
419 if (ldb["__type"] == "_JsonRpcError")
424 var res = ldb.modify(params[1]);
425 if (res.error != 0) {
426 error.setError(res.error, res.errstr);
432 jsonrpc.method.modify = _modify;
436 * Delete data from the database
439 * The resource id of the open database, previously returned by connect()
442 * The DN to be located and deleted
445 * An object of class JsonRpcError.
451 function _del(params, error)
453 if (params.length != 2)
455 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
456 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
457 "usage: <resource_id> <dn>");
461 ldb = session.resources.get(params[0], error);
462 if (ldb["__type"] == "_JsonRpcError")
467 var res = ldb.del(params[1]);
468 if (res.error != 0) {
469 error.setError(res.error, res.errstr);
475 jsonrpc.method.del = _del;
479 * Rename data in the database
482 * The resource id of the open database, previously returned by connect()
485 * The DN to be renamed
488 * The new name for the DN being renamed
491 * An object of class JsonRpcError.
497 function _rename(params, error)
499 if (params.length != 3)
501 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
502 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
503 "usage: <resource_id> <old_dn> <new_dn>");
507 ldb = session.resources.get(params[0], error);
508 if (ldb["__type"] == "_JsonRpcError")
513 var res = ldb.rename(params[1], params[2]);
514 if (res.error != 0) {
515 error.setError(res.error, res.errstr);
521 jsonrpc.method.rename = _rename;
525 * Base64-encode a string
528 * The resource id of the open database, previously returned by connect()
531 * The string to be base64 encoded
534 * An object of class JsonRpcError.
537 * Success: encoded string
538 * Failure: `undefined`
540 function _base64encode(params, error)
542 if (params.length != 2)
544 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
545 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
546 "usage: <resource_id> <string_to_be_encoded>");
550 ldb = session.resources.get(params[0], error);
551 if (ldb["__type"] == "_JsonRpcError")
556 return ldb.base64encode(params[1]);
558 jsonrpc.method.base64encode = _base64encode;
562 * Base64-decode a string
565 * The resource id of the open database, previously returned by connect()
568 * The string to be base64 decoded
571 * An object of class JsonRpcError.
574 * Success: decoded string
575 * Failure: `undefined`
577 function _base64decode(params, error)
579 if (params.length != 2)
581 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
582 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
583 "usage: <resource_id> <string_to_be_decoded>");
587 ldb = session.resources.get(params[0], error);
588 if (ldb["__type"] == "_JsonRpcError")
593 return ldb.base64decode(params[1]);
595 jsonrpc.method.base64decode = _base64decode;
602 * The resource id of the open database, previously returned by connect()
605 * The DN to be escaped
608 * An object of class JsonRpcError.
611 * Success: escaped string
614 function _base64decode(params, error)
616 if (params.length != 2)
618 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
619 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
620 "usage: <resource_id> <string_to_be_decoded>");
624 ldb = session.resources.get(params[0], error);
625 if (ldb["__type"] == "_JsonRpcError")
630 return ldb.base64decode(params[1]);
632 jsonrpc.method.base64decode = _base64decode;
636 * Retrieve a description of the most recent error
639 * The resource id of the open database, previously returned by connect()
642 * An object of class JsonRpcError.
645 * The most recent error string for the ldb specified by the resource id
647 function _errstring(params, error)
649 if (params.length != 1)
651 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
652 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
653 "usage: <resource_id>");
657 ldb = session.resources.get(params[0], error);
658 if (ldb["__type"] == "_JsonRpcError")
663 return ldb.errstring();
665 jsonrpc.method.errstring = _errstring;