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 return ldb.search(expr, baseDN, scope, attrs);
339 jsonrpc.method.search = _search;
343 * Add data to the database
346 * The resource id of the open database, previously returned by connect()
349 * An LDIF string representing the data to be added
352 * An object of class JsonRpcError.
358 function _add(params, error)
360 if (params.length != 2)
362 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
363 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
364 "usage: <resource_id> <ldif>");
368 ldb = session.resources.get(params[0], error);
369 if (ldb["__type"] == "_JsonRpcError")
374 return ldb.add(params[1]);
376 jsonrpc.method.add = _add;
380 * Modify data in the database
383 * The resource id of the open database, previously returned by connect()
386 * An LDIF string representing the data to be modified
389 * An object of class JsonRpcError.
395 function _modify(params, error)
397 if (params.length != 2)
399 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
400 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
401 "usage: <resource_id> <ldif>");
405 ldb = session.resources.get(params[0], error);
406 if (ldb["__type"] == "_JsonRpcError")
411 return ldb.modify(params[1]);
413 jsonrpc.method.modify = _modify;
417 * Delete data from the database
420 * The resource id of the open database, previously returned by connect()
423 * The DN to be located and deleted
426 * An object of class JsonRpcError.
432 function _del(params, error)
434 if (params.length != 2)
436 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
437 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
438 "usage: <resource_id> <dn>");
442 ldb = session.resources.get(params[0], error);
443 if (ldb["__type"] == "_JsonRpcError")
448 return ldb.del(params[1]);
450 jsonrpc.method.del = _del;
454 * Rename data in the database
457 * The resource id of the open database, previously returned by connect()
460 * The DN to be renamed
463 * The new name for the DN being renamed
466 * An object of class JsonRpcError.
472 function _rename(params, error)
474 if (params.length != 3)
476 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
477 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
478 "usage: <resource_id> <old_dn> <new_dn>");
482 ldb = session.resources.get(params[0], error);
483 if (ldb["__type"] == "_JsonRpcError")
488 return ldb.rename(params[1], params[2]);
490 jsonrpc.method.rename = _rename;
494 * Base64-encode a string
497 * The resource id of the open database, previously returned by connect()
500 * The string to be base64 encoded
503 * An object of class JsonRpcError.
506 * Success: encoded string
507 * Failure: `undefined`
509 function _base64encode(params, error)
511 if (params.length != 2)
513 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
514 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
515 "usage: <resource_id> <string_to_be_encoded>");
519 ldb = session.resources.get(params[0], error);
520 if (ldb["__type"] == "_JsonRpcError")
525 return ldb.base64encode(params[1]);
527 jsonrpc.method.base64encode = _base64encode;
531 * Base64-decode a string
534 * The resource id of the open database, previously returned by connect()
537 * The string to be base64 decoded
540 * An object of class JsonRpcError.
543 * Success: decoded string
544 * Failure: `undefined`
546 function _base64decode(params, error)
548 if (params.length != 2)
550 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
551 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
552 "usage: <resource_id> <string_to_be_decoded>");
556 ldb = session.resources.get(params[0], error);
557 if (ldb["__type"] == "_JsonRpcError")
562 return ldb.base64decode(params[1]);
564 jsonrpc.method.base64decode = _base64decode;
571 * The resource id of the open database, previously returned by connect()
574 * The DN to be escaped
577 * An object of class JsonRpcError.
580 * Success: escaped string
583 function _base64decode(params, error)
585 if (params.length != 2)
587 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
588 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
589 "usage: <resource_id> <string_to_be_decoded>");
593 ldb = session.resources.get(params[0], error);
594 if (ldb["__type"] == "_JsonRpcError")
599 return ldb.base64decode(params[1]);
601 jsonrpc.method.base64decode = _base64decode;
605 * Retrieve a description of the most recent error
608 * The resource id of the open database, previously returned by connect()
611 * An object of class JsonRpcError.
614 * The most recent error string for the ldb specified by the resource id
616 function _errstring(params, error)
618 if (params.length != 1)
620 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
621 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
622 "usage: <resource_id>");
626 ldb = session.resources.get(params[0], error);
627 if (ldb["__type"] == "_JsonRpcError")
632 return ldb.errstring();
634 jsonrpc.method.errstring = _errstring;