r19757: Don't do the strrchr twice. Pointed out by Martin Kuhl.
[Samba.git] / services / samba / ldb.esp
blob2654efe9884e2e589940f1cf06a95022904e3eb6
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.ErrorCode.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     var ret = ldb.connect(private_dir + "/" + params[0]);
99     if (ret && ldb.db)
100     {
101         return session.resources.set(ldb, "ldb:" + params[0], error);
102     }
103     else
104     {
105         error.setError(-1, "ldb.connect failed");
106         return error;
107     }
109 jsonrpc.method.connect = _connect;
113  * Close a database
115  * @param params[0]
116  *   The resource id of the open database, previously returned by connect()
118  * @param error
119  *   An object of class JsonRpcError.
121  * @return
122  *   Success: True
123  *   Failure: Will only fail with invalid parameters, and throws an error
124  */
125 function _close(params, error)
127     if (params.length != 1)
128     {
129         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
130                        "usage: <resource_id>");
131         return error;
132     }
134     ldb = session.resources.get(params[0], error);
135     if (ldb["__type"] == "_JsonRpcError")
136     {
137         return ldb;
138     }
140     var ret = ldb.close();
141     
142     /* If close succeeded, release the stored resource */
143     if (ret)
144     {
145         session.resources.release(params[0], error);
146     }
148     return ret;
150 jsonrpc.method.close = _close;
154  * Begin a transaction
156  * @param params[0]
157  *   The resource id of the open database, previously returned by connect()
159  * @param error
160  *   An object of class JsonRpcError.
162  * @return
163  *   Success: True
164  *   Failure: False
165  */
166 function _transaction_start(params, error)
168     if (params.length != 1)
169     {
170         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
171                        "usage: <resource_id>");
172         return error;
173     }
175     ldb = session.resources.get(params[0], error);
176     if (ldb["__type"] == "_JsonRpcError")
177     {
178         return ldb;
179     }
181     return ldb.transaction_start();
183 jsonrpc.method.transaction_start = _transaction_start;
187  * Cancel a transaction
189  * @param params[0]
190  *   The resource id of the open database, previously returned by connect()
192  * @param error
193  *   An object of class JsonRpcError.
195  * @return
196  *   Success: True
197  *   Failure: False
198  */
199 function _transaction_cancel(params, error)
201     if (params.length != 1)
202     {
203         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
204                        "usage: <resource_id>");
205         return error;
206     }
208     ldb = session.resources.get(params[0], error);
209     if (ldb["__type"] == "_JsonRpcError")
210     {
211         return ldb;
212     }
214     return ldb.transaction_cancel();
216 jsonrpc.method.transaction_cancel = _transaction_cancel;
220  * Commit a transaction
222  * @param params[0]
223  *   The resource id of the open database, previously returned by connect()
225  * @param error
226  *   An object of class JsonRpcError.
228  * @return
229  *   Success: True
230  *   Failure: False
231  */
232 function _transaction_commit(params, error)
234     if (params.length != 1)
235     {
236         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
237                        "usage: <resource_id>");
238         return error;
239     }
241     ldb = session.resources.get(params[0], error);
242     if (ldb["__type"] == "_JsonRpcError")
243     {
244         return ldb;
245     }
247     return ldb.transaction_commit();
249 jsonrpc.method.transaction_commit = _transaction_commit;
253  * Issue a Search request
255  * @param params[0]
256  *   The resource id of the open database, previously returned by connect()
258  * @param params[1]
259  *   Search expression
261  * @param params[2]
262  *   Base DN
264  * @param params[3]
265  *   Scope: "default", "base", "one" or "subtree"
267  * @param params[4]
268  *   Attributes: an array object
270  * @param error
271  *   An object of class JsonRpcError.
273  * @return
274  *   Success: found object
275  *   Failure: `undefined`
277  * @note
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
281  */
282 function _search(params, error)
284     if (params.length < 2 || params.length > 5)
285     {
286         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
287                        "usage: " +
288                        "<resource_id> <expr> [<baseDN> [<scope> [<attrs>]]]");
289         return error;
290     }
292     ldb = session.resources.get(params[0], error);
293     if (ldb["__type"] == "_JsonRpcError")
294     {
295         return ldb;
296     }
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 */
310     if (scope == "base")
311     {
312         scope = ldb.SCOPE_BASE;
313     }
314     else if (scope == "one")
315     {
316         scope = ldb.SCOPE_ONE;
317     }
318     else if (scope == "subtree")
319     {
320         scope = ldb.SCOPE_SUBTREE;
321     }
322     else if (scope == "default")
323     {
324         scope = ldb.SCOPE_DEFAULT;
325     }
326     else
327     {
328         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
329                        "invalid scope: " + scope);
330         return error;
331     }
333     return ldb.search(expr, baseDN, scope, attrs);
335 jsonrpc.method.search = _search;
339  * Add data to the database
341  * @param params[0]
342  *   The resource id of the open database, previously returned by connect()
344  * @param params[1]
345  *   An LDIF string representing the data to be added
347  * @param error
348  *   An object of class JsonRpcError.
350  * @return
351  *   Success: True
352  *   Failure: False
353  */
354 function _add(params, error)
356     if (params.length != 2)
357     {
358         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
359                        "usage: <resource_id> <ldif>");
360         return error;
361     }
363     ldb = session.resources.get(params[0], error);
364     if (ldb["__type"] == "_JsonRpcError")
365     {
366         return ldb;
367     }
369     return ldb.add(params[1]);
371 jsonrpc.method.add = _add;
375  * Modify data in the database
377  * @param params[0]
378  *   The resource id of the open database, previously returned by connect()
380  * @param params[1]
381  *   An LDIF string representing the data to be modified
383  * @param error
384  *   An object of class JsonRpcError.
386  * @return
387  *   Success: True
388  *   Failure: False
389  */
390 function _modify(params, error)
392     if (params.length != 2)
393     {
394         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
395                        "usage: <resource_id> <ldif>");
396         return error;
397     }
399     ldb = session.resources.get(params[0], error);
400     if (ldb["__type"] == "_JsonRpcError")
401     {
402         return ldb;
403     }
405     return ldb.modify(params[1]);
407 jsonrpc.method.modify = _modify;
411  * Delete data from the database
413  * @param params[0]
414  *   The resource id of the open database, previously returned by connect()
416  * @param params[1]
417  *   The DN to be located and deleted
419  * @param error
420  *   An object of class JsonRpcError.
422  * @return
423  *   Success: True
424  *   Failure: False
425  */
426 function _del(params, error)
428     if (params.length != 2)
429     {
430         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
431                        "usage: <resource_id> <dn>");
432         return error;
433     }
435     ldb = session.resources.get(params[0], error);
436     if (ldb["__type"] == "_JsonRpcError")
437     {
438         return ldb;
439     }
441     return ldb.del(params[1]);
443 jsonrpc.method.del = _del;
447  * Rename data in the database
449  * @param params[0]
450  *   The resource id of the open database, previously returned by connect()
452  * @param params[1]
453  *   The DN to be renamed
455  * @param params[2]
456  *   The new name for the DN being renamed
458  * @param error
459  *   An object of class JsonRpcError.
461  * @return
462  *   Success: True
463  *   Failure: False
464  */
465 function _rename(params, error)
467     if (params.length != 3)
468     {
469         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
470                        "usage: <resource_id> <old_dn> <new_dn>");
471         return error;
472     }
474     ldb = session.resources.get(params[0], error);
475     if (ldb["__type"] == "_JsonRpcError")
476     {
477         return ldb;
478     }
480     return ldb.rename(params[1], params[2]);
482 jsonrpc.method.rename = _rename;
486  * Base64-encode a string
488  * @param params[0]
489  *   The resource id of the open database, previously returned by connect()
491  * @param params[1]
492  *   The string to be base64 encoded
494  * @param error
495  *   An object of class JsonRpcError.
497  * @return
498  *   Success: encoded string
499  *   Failure: `undefined`
500  */
501 function _base64encode(params, error)
503     if (params.length != 2)
504     {
505         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
506                        "usage: <resource_id> <string_to_be_encoded>");
507         return error;
508     }
510     ldb = session.resources.get(params[0], error);
511     if (ldb["__type"] == "_JsonRpcError")
512     {
513         return ldb;
514     }
516     return ldb.base64encode(params[1]);
518 jsonrpc.method.base64encode = _base64encode;
522  * Base64-decode a string
524  * @param params[0]
525  *   The resource id of the open database, previously returned by connect()
527  * @param params[1]
528  *   The string to be base64 decoded
530  * @param error
531  *   An object of class JsonRpcError.
533  * @return
534  *   Success: decoded string
535  *   Failure: `undefined`
536  */
537 function _base64decode(params, error)
539     if (params.length != 2)
540     {
541         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
542                        "usage: <resource_id> <string_to_be_decoded>");
543         return error;
544     }
546     ldb = session.resources.get(params[0], error);
547     if (ldb["__type"] == "_JsonRpcError")
548     {
549         return ldb;
550     }
552     return ldb.base64decode(params[1]);
554 jsonrpc.method.base64decode = _base64decode;
558  * escape a DN
560  * @param params[0]
561  *   The resource id of the open database, previously returned by connect()
563  * @param params[1]
564  *   The DN to be escaped
566  * @param error
567  *   An object of class JsonRpcError.
569  * @return
570  *   Success: escaped string
571  *   Failure: undefined
572  */
573 function _base64decode(params, error)
575     if (params.length != 2)
576     {
577         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
578                        "usage: <resource_id> <string_to_be_decoded>");
579         return error;
580     }
582     ldb = session.resources.get(params[0], error);
583     if (ldb["__type"] == "_JsonRpcError")
584     {
585         return ldb;
586     }
588     return ldb.base64decode(params[1]);
590 jsonrpc.method.base64decode = _base64decode;
594  * Retrieve a description of the most recent error
596  * @param params[0]
597  *   The resource id of the open database, previously returned by connect()
599  * @param error
600  *   An object of class JsonRpcError.
602  * @return
603  *   The most recent error string for the ldb specified by the resource id
604  */
605 function _errstring(params, error)
607     if (params.length != 1)
608     {
609         error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
610                        "usage: <resource_id>");
611         return error;
612     }
614     ldb = session.resources.get(params[0], error);
615     if (ldb["__type"] == "_JsonRpcError")
616     {
617         return ldb;
618     }
620     return ldb.errstring();
622 jsonrpc.method.errstring = _errstring;
628  * Local Variables:
629  * mode: c
630  * End:
631  */