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.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
290 "<resource_id> <expr> [<baseDN> [<scope> [<attrs>]]]");
294 ldb = session.resources.get(params[0], error);
295 if (ldb["__type"] == "_JsonRpcError")
300 /* Retrieve parameters */
301 var expr = params[1];
302 var baseDN = params[2];
303 var scope = params[3];
304 var attrs = params[4];
306 /* Fill in optional parameters */
307 if (params.length < 3) baseDN = null;
308 if (params.length < 4) scope = "one";
309 if (params.length < 5) attrs = null;
311 /* Determine scope value */
314 scope = ldb.SCOPE_BASE;
316 else if (scope == "one")
318 scope = ldb.SCOPE_ONE;
320 else if (scope == "subtree")
322 scope = ldb.SCOPE_SUBTREE;
324 else if (scope == "default")
326 scope = ldb.SCOPE_DEFAULT;
330 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
331 "invalid scope: " + scope);
335 return ldb.search(expr, baseDN, scope, attrs);
337 jsonrpc.method.search = _search;
341 * Add data to the database
344 * The resource id of the open database, previously returned by connect()
347 * An LDIF string representing the data to be added
350 * An object of class JsonRpcError.
356 function _add(params, error)
358 if (params.length != 2)
360 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
361 "usage: <resource_id> <ldif>");
365 ldb = session.resources.get(params[0], error);
366 if (ldb["__type"] == "_JsonRpcError")
371 return ldb.add(params[1]);
373 jsonrpc.method.add = _add;
377 * Modify data in the database
380 * The resource id of the open database, previously returned by connect()
383 * An LDIF string representing the data to be modified
386 * An object of class JsonRpcError.
392 function _modify(params, error)
394 if (params.length != 2)
396 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
397 "usage: <resource_id> <ldif>");
401 ldb = session.resources.get(params[0], error);
402 if (ldb["__type"] == "_JsonRpcError")
407 return ldb.modify(params[1]);
409 jsonrpc.method.modify = _modify;
413 * Delete data from the database
416 * The resource id of the open database, previously returned by connect()
419 * The DN to be located and deleted
422 * An object of class JsonRpcError.
428 function _del(params, error)
430 if (params.length != 2)
432 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
433 "usage: <resource_id> <dn>");
437 ldb = session.resources.get(params[0], error);
438 if (ldb["__type"] == "_JsonRpcError")
443 return ldb.del(params[1]);
445 jsonrpc.method.del = _del;
449 * Rename data in the database
452 * The resource id of the open database, previously returned by connect()
455 * The DN to be renamed
458 * The new name for the DN being renamed
461 * An object of class JsonRpcError.
467 function _rename(params, error)
469 if (params.length != 3)
471 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
472 "usage: <resource_id> <old_dn> <new_dn>");
476 ldb = session.resources.get(params[0], error);
477 if (ldb["__type"] == "_JsonRpcError")
482 return ldb.rename(params[1], params[2]);
484 jsonrpc.method.rename = _rename;
488 * Base64-encode a string
491 * The resource id of the open database, previously returned by connect()
494 * The string to be base64 encoded
497 * An object of class JsonRpcError.
500 * Success: encoded string
501 * Failure: `undefined`
503 function _base64encode(params, error)
505 if (params.length != 2)
507 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
508 "usage: <resource_id> <string_to_be_encoded>");
512 ldb = session.resources.get(params[0], error);
513 if (ldb["__type"] == "_JsonRpcError")
518 return ldb.base64encode(params[1]);
520 jsonrpc.method.base64encode = _base64encode;
524 * Base64-decode a string
527 * The resource id of the open database, previously returned by connect()
530 * The string to be base64 decoded
533 * An object of class JsonRpcError.
536 * Success: decoded string
537 * Failure: `undefined`
539 function _base64decode(params, error)
541 if (params.length != 2)
543 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
544 "usage: <resource_id> <string_to_be_decoded>");
548 ldb = session.resources.get(params[0], error);
549 if (ldb["__type"] == "_JsonRpcError")
554 return ldb.base64decode(params[1]);
556 jsonrpc.method.base64decode = _base64decode;
563 * The resource id of the open database, previously returned by connect()
566 * The DN to be escaped
569 * An object of class JsonRpcError.
572 * Success: escaped string
575 function _base64decode(params, error)
577 if (params.length != 2)
579 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
580 "usage: <resource_id> <string_to_be_decoded>");
584 ldb = session.resources.get(params[0], error);
585 if (ldb["__type"] == "_JsonRpcError")
590 return ldb.base64decode(params[1]);
592 jsonrpc.method.base64decode = _base64decode;
596 * Retrieve a description of the most recent error
599 * The resource id of the open database, previously returned by connect()
602 * An object of class JsonRpcError.
605 * The most recent error string for the ldb specified by the resource id
607 function _errstring(params, error)
609 if (params.length != 1)
611 error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
612 "usage: <resource_id>");
616 ldb = session.resources.get(params[0], error);
617 if (ldb["__type"] == "_JsonRpcError")
622 return ldb.errstring();
624 jsonrpc.method.errstring = _errstring;