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.ErrorCode.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 var ret = ldb.connect(private_dir + "/" + params[0]);
101 return session.resources.set(ldb, "ldb:" + params[0], error);
105 error.setError(-1, "ldb.connect failed");
109 jsonrpc.method.connect = _connect;
116 * The resource id of the open database, previously returned by connect()
119 * An object of class JsonRpcError.
123 * Failure: Will only fail with invalid parameters, and throws an error
125 function _close(params, error)
127 if (params.length != 1)
129 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
130 "usage: <resource_id>");
134 ldb = session.resources.get(params[0], error);
135 if (ldb["__type"] == "_JsonRpcError")
140 var ret = ldb.close();
142 /* If close succeeded, release the stored resource */
145 session.resources.release(params[0], error);
150 jsonrpc.method.close = _close;
154 * Begin a transaction
157 * The resource id of the open database, previously returned by connect()
160 * An object of class JsonRpcError.
166 function _transaction_start(params, error)
168 if (params.length != 1)
170 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
171 "usage: <resource_id>");
175 ldb = session.resources.get(params[0], error);
176 if (ldb["__type"] == "_JsonRpcError")
181 return ldb.transaction_start();
183 jsonrpc.method.transaction_start = _transaction_start;
187 * Cancel a transaction
190 * The resource id of the open database, previously returned by connect()
193 * An object of class JsonRpcError.
199 function _transaction_cancel(params, error)
201 if (params.length != 1)
203 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
204 "usage: <resource_id>");
208 ldb = session.resources.get(params[0], error);
209 if (ldb["__type"] == "_JsonRpcError")
214 return ldb.transaction_cancel();
216 jsonrpc.method.transaction_cancel = _transaction_cancel;
220 * Commit a transaction
223 * The resource id of the open database, previously returned by connect()
226 * An object of class JsonRpcError.
232 function _transaction_commit(params, error)
234 if (params.length != 1)
236 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
237 "usage: <resource_id>");
241 ldb = session.resources.get(params[0], error);
242 if (ldb["__type"] == "_JsonRpcError")
247 return ldb.transaction_commit();
249 jsonrpc.method.transaction_commit = _transaction_commit;
253 * Issue a Search request
256 * The resource id of the open database, previously returned by connect()
265 * Scope: "default", "base", "one" or "subtree"
268 * Attributes: an array object
271 * An object of class JsonRpcError.
274 * Success: found object
275 * Failure: `undefined`
278 * If params[4] is missing, assume no attributes
279 * If params[3..4] are missing, also assume "default" scope
280 * If params[2..4] are missing, also assume null base DN
282 function _search(params, error)
284 if (params.length < 2 || params.length > 5)
286 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
288 "<resource_id> <expr> [<baseDN> [<scope> [<attrs>]]]");
292 ldb = session.resources.get(params[0], error);
293 if (ldb["__type"] == "_JsonRpcError")
298 /* Retrieve parameters */
299 var expr = params[1];
300 var baseDN = params[2];
301 var scope = params[3];
302 var attrs = params[4];
304 /* Fill in optional parameters */
305 if (params.length < 3) baseDN = null;
306 if (params.length < 4) scope = "one";
307 if (params.length < 5) attrs = null;
309 /* Determine scope value */
312 scope = ldb.SCOPE_BASE;
314 else if (scope == "one")
316 scope = ldb.SCOPE_ONE;
318 else if (scope == "subtree")
320 scope = ldb.SCOPE_SUBTREE;
322 else if (scope == "default")
324 scope = ldb.SCOPE_DEFAULT;
328 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
329 "invalid scope: " + scope);
333 return ldb.search(expr, baseDN, scope, attrs);
335 jsonrpc.method.search = _search;
339 * Add data to the database
342 * The resource id of the open database, previously returned by connect()
345 * An LDIF string representing the data to be added
348 * An object of class JsonRpcError.
354 function _add(params, error)
356 if (params.length != 2)
358 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
359 "usage: <resource_id> <ldif>");
363 ldb = session.resources.get(params[0], error);
364 if (ldb["__type"] == "_JsonRpcError")
369 return ldb.add(params[1]);
371 jsonrpc.method.add = _add;
375 * Modify data in the database
378 * The resource id of the open database, previously returned by connect()
381 * An LDIF string representing the data to be modified
384 * An object of class JsonRpcError.
390 function _modify(params, error)
392 if (params.length != 2)
394 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
395 "usage: <resource_id> <ldif>");
399 ldb = session.resources.get(params[0], error);
400 if (ldb["__type"] == "_JsonRpcError")
405 return ldb.modify(params[1]);
407 jsonrpc.method.modify = _modify;
411 * Delete data from the database
414 * The resource id of the open database, previously returned by connect()
417 * The DN to be located and deleted
420 * An object of class JsonRpcError.
426 function _del(params, error)
428 if (params.length != 2)
430 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
431 "usage: <resource_id> <dn>");
435 ldb = session.resources.get(params[0], error);
436 if (ldb["__type"] == "_JsonRpcError")
441 return ldb.del(params[1]);
443 jsonrpc.method.del = _del;
447 * Rename data in the database
450 * The resource id of the open database, previously returned by connect()
453 * The DN to be renamed
456 * The new name for the DN being renamed
459 * An object of class JsonRpcError.
465 function _rename(params, error)
467 if (params.length != 3)
469 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
470 "usage: <resource_id> <old_dn> <new_dn>");
474 ldb = session.resources.get(params[0], error);
475 if (ldb["__type"] == "_JsonRpcError")
480 return ldb.rename(params[1], params[2]);
482 jsonrpc.method.rename = _rename;
486 * Base64-encode a string
489 * The resource id of the open database, previously returned by connect()
492 * The string to be base64 encoded
495 * An object of class JsonRpcError.
498 * Success: encoded string
499 * Failure: `undefined`
501 function _base64encode(params, error)
503 if (params.length != 2)
505 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
506 "usage: <resource_id> <string_to_be_encoded>");
510 ldb = session.resources.get(params[0], error);
511 if (ldb["__type"] == "_JsonRpcError")
516 return ldb.base64encode(params[1]);
518 jsonrpc.method.base64encode = _base64encode;
522 * Base64-decode a string
525 * The resource id of the open database, previously returned by connect()
528 * The string to be base64 decoded
531 * An object of class JsonRpcError.
534 * Success: decoded string
535 * Failure: `undefined`
537 function _base64decode(params, error)
539 if (params.length != 2)
541 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
542 "usage: <resource_id> <string_to_be_decoded>");
546 ldb = session.resources.get(params[0], error);
547 if (ldb["__type"] == "_JsonRpcError")
552 return ldb.base64decode(params[1]);
554 jsonrpc.method.base64decode = _base64decode;
561 * The resource id of the open database, previously returned by connect()
564 * The DN to be escaped
567 * An object of class JsonRpcError.
570 * Success: escaped string
573 function _base64decode(params, error)
575 if (params.length != 2)
577 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
578 "usage: <resource_id> <string_to_be_decoded>");
582 ldb = session.resources.get(params[0], error);
583 if (ldb["__type"] == "_JsonRpcError")
588 return ldb.base64decode(params[1]);
590 jsonrpc.method.base64decode = _base64decode;
594 * Retrieve a description of the most recent error
597 * The resource id of the open database, previously returned by connect()
600 * An object of class JsonRpcError.
603 * The most recent error string for the ldb specified by the resource id
605 function _errstring(params, error)
607 if (params.length != 1)
609 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
610 "usage: <resource_id>");
614 ldb = session.resources.get(params[0], error);
615 if (ldb["__type"] == "_JsonRpcError")
620 return ldb.errstring();
622 jsonrpc.method.errstring = _errstring;