r19167: - Various JSON-RPC facilities will desire to keep resources open in a
[kai/samba.git] / services / samba / ldb.esp
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  */
10
11 /*
12  * JSON-RPC mappings to the ldb ejs functions
13  */
14
15 /* We'll be saving resources in the session */
16 jsonrpc_include("resources.esp");
17
18
19 /**
20  * Connect to a database
21  *
22  * @param params[0]
23  *   Database name
24  *
25  * @param params[1..n]
26  *   Option (e.g. "modules:modlist")
27  *
28  * @param error
29  *   An object of class JsonRpcError.
30  *
31  * @return
32  *   Success: The resource id to be used for future access to the database
33  *   Failure: -1
34  *
35  * @note
36  *   Credentials or session_info may be set up first.
37  */
38 function _connect(params, error)
39 {
40     if (params.length < 1)
41     {
42         error.setError(JsonRpcError_ParameterMismatch,
43                        "usage: <db_name> [<option> ...]");
44         return error;
45     }
46
47     ldb = ldb_init();
48     var ret = ldb.connect(params[0]);
49     if (ret && ldb.db)
50     {
51         return session.resources.set(ldb,
52                                      session.resources.Type.ldb,
53                                      error);
54     }
55     else
56     {
57         error.setError(-1, "ldb.connect failed");
58         return error;
59     }
60 }
61 jsonrpc.method.connect = _connect;
62
63
64 /**
65  * Close a database
66  *
67  * @param params[0]
68  *   The resource id of the open database, previously returned by connect()
69  *
70  * @param error
71  *   An object of class JsonRpcError.
72  *
73  * @return
74  *   Success: True
75  *   Failure: Will only fail with invalid parameters, and throws an error
76  */
77 function _close(params, error)
78 {
79     if (params.length != 1)
80     {
81         error.setError(JsonRpcError_ParameterMismatch,
82                        "usage: <resource_id>");
83         return error;
84     }
85
86     ldb = session.resources.get(params[0],
87                                 session.resources.Type.ldb,
88                                 error);
89     if (ldb["__type"] == "_JsonRpcError")
90     {
91         return ldb;
92     }
93
94     var ret = ldb.close();
95     
96     /* If close succeeded, release the stored resource */
97     if (ret)
98     {
99         session.resources.release(params[0], error);
100     }
101
102     return ret;
103 }
104 jsonrpc.method.close = _close;
105
106
107 /**
108  * Begin a transaction
109  *
110  * @param params[0]
111  *   The resource id of the open database, previously returned by connect()
112  *
113  * @param error
114  *   An object of class JsonRpcError.
115  *
116  * @return
117  *   Success: True
118  *   Failure: False
119  */
120 function _transaction_start(params, error)
121 {
122     if (params.length != 1)
123     {
124         error.setError(JsonRpcError_ParameterMismatch,
125                        "usage: <resource_id>");
126         return error;
127     }
128
129     ldb = session.resources.get(params[0],
130                                 session.resources.Type.ldb,
131                                 error);
132     if (ldb["__type"] == "_JsonRpcError")
133     {
134         return ldb;
135     }
136
137     return ldb.transaction_start();
138 }
139 jsonrpc.method.transaction_start = _transaction_start;
140
141
142 /**
143  * Cancel a transaction
144  *
145  * @param params[0]
146  *   The resource id of the open database, previously returned by connect()
147  *
148  * @param error
149  *   An object of class JsonRpcError.
150  *
151  * @return
152  *   Success: True
153  *   Failure: False
154  */
155 function _transaction_cancel(params, error)
156 {
157     if (params.length != 1)
158     {
159         error.setError(JsonRpcError_ParameterMismatch,
160                        "usage: <resource_id>");
161         return error;
162     }
163
164     ldb = session.resources.get(params[0],
165                                 session.resources.Type.ldb,
166                                 error);
167     if (ldb["__type"] == "_JsonRpcError")
168     {
169         return ldb;
170     }
171
172     return ldb.transaction_cancel();
173 }
174 jsonrpc.method.transaction_cancel = _transaction_cancel;
175
176
177 /**
178  * Commit a transaction
179  *
180  * @param params[0]
181  *   The resource id of the open database, previously returned by connect()
182  *
183  * @param error
184  *   An object of class JsonRpcError.
185  *
186  * @return
187  *   Success: True
188  *   Failure: False
189  */
190 function _transaction_commit(params, error)
191 {
192     if (params.length != 1)
193     {
194         error.setError(JsonRpcError_ParameterMismatch,
195                        "usage: <resource_id>");
196         return error;
197     }
198
199     ldb = session.resources.get(params[0],
200                                 session.resources.Type.ldb,
201                                 error);
202     if (ldb["__type"] == "_JsonRpcError")
203     {
204         return ldb;
205     }
206
207     return ldb.transaction_commit();
208 }
209 jsonrpc.method.transaction_commit = _transaction_commit;
210
211
212 /**
213  * Issue a Search request
214  *
215  * @param params[0]
216  *   The resource id of the open database, previously returned by connect()
217  *
218  * @param params[1]
219  *   Search expression
220  *
221  * @param params[2]
222  *   Base DN
223  *
224  * @param params[3]
225  *   Scope: "default", "base", "one" or "subtree"
226  *
227  * @param params[4]
228  *   Attributes: an array object
229  *
230  * @param error
231  *   An object of class JsonRpcError.
232  *
233  * @return
234  *   Success: found object
235  *   Failure: `undefined`
236  *
237  * @note
238  *   If params[4] is missing, assume no attributes
239  *   If params[3..4] are missing, also assume "default" scope
240  *   If params[2..4] are missing, also assume null base DN
241  */
242 function _search(params, error)
243 {
244     if (params.length < 2 || params.length > 5)
245     {
246         error.setError(JsonRpcError_ParameterMismatch,
247                        "usage: " +
248                        "<resource_id> <expr> [<baseDN> [<scope> [<attrs>]]]");
249         return error;
250     }
251
252     ldb = session.resources.get(params[0],
253                                 session.resources.Type.ldb,
254                                 error);
255     if (ldb["__type"] == "_JsonRpcError")
256     {
257         return ldb;
258     }
259
260     /* Retrieve parameters */
261     var expr = params[1];
262     var baseDN = params[2];
263     var scope = params[3];
264     var attrs = params[4];
265
266     /* Fill in optional parameters */
267     if (params.length < 3) baseDN = null;
268     if (params.length < 4) scope = "one";
269     if (params.length < 5) attrs = null;
270
271     /* Determine scope value */
272     if (scope == "base")
273     {
274         scope = ldb.SCOPE_BASE;
275     }
276     else if (scope == "one")
277     {
278         scope = ldb.SCOPE_ONE;
279     }
280     else if (scope == "subtree")
281     {
282         scope = ldb.SCOPE_SUBTREE;
283     }
284     else if (scope == "default")
285     {
286         scope = ldb.SCOPE_DEFAULT;
287     }
288     else
289     {
290         error.setError(JsonRpcError_ParameterMismatch,
291                        "invalid scope");
292         return error;
293     }
294
295     return ldb.transaction_search(expr, baseDN, scope, attrs);
296 }
297 jsonrpc.method.search = _search;
298
299
300 /**
301  * Add data to the database
302  *
303  * @param params[0]
304  *   The resource id of the open database, previously returned by connect()
305  *
306  * @param params[1]
307  *   An LDIF string representing the data to be added
308  *
309  * @param error
310  *   An object of class JsonRpcError.
311  *
312  * @return
313  *   Success: True
314  *   Failure: False
315  */
316 function _add(params, error)
317 {
318     if (params.length != 2)
319     {
320         error.setError(JsonRpcError_ParameterMismatch,
321                        "usage: <resource_id> <ldif>");
322         return error;
323     }
324
325     ldb = session.resources.get(params[0],
326                                 session.resources.Type.ldb,
327                                 error);
328     if (ldb["__type"] == "_JsonRpcError")
329     {
330         return ldb;
331     }
332
333     return ldb.add(params[1]);
334 }
335 jsonrpc.method.add = _add;
336
337
338 /**
339  * Modify data in the database
340  *
341  * @param params[0]
342  *   The resource id of the open database, previously returned by connect()
343  *
344  * @param params[1]
345  *   An LDIF string representing the data to be modified
346  *
347  * @param error
348  *   An object of class JsonRpcError.
349  *
350  * @return
351  *   Success: True
352  *   Failure: False
353  */
354 function _modify(params, error)
355 {
356     if (params.length != 2)
357     {
358         error.setError(JsonRpcError_ParameterMismatch,
359                        "usage: <resource_id> <ldif>");
360         return error;
361     }
362
363     ldb = session.resources.get(params[0],
364                                 session.resources.Type.ldb,
365                                 error);
366     if (ldb["__type"] == "_JsonRpcError")
367     {
368         return ldb;
369     }
370
371     return ldb.modify(params[1]);
372 }
373 jsonrpc.method.modify = _modify;
374
375
376 /**
377  * Delete data from the database
378  *
379  * @param params[0]
380  *   The resource id of the open database, previously returned by connect()
381  *
382  * @param params[1]
383  *   The DN to be located and deleted
384  *
385  * @param error
386  *   An object of class JsonRpcError.
387  *
388  * @return
389  *   Success: True
390  *   Failure: False
391  */
392 function _del(params, error)
393 {
394     if (params.length != 2)
395     {
396         error.setError(JsonRpcError_ParameterMismatch,
397                        "usage: <resource_id> <dn>");
398         return error;
399     }
400
401     ldb = session.resources.get(params[0],
402                                 session.resources.Type.ldb,
403                                 error);
404     if (ldb["__type"] == "_JsonRpcError")
405     {
406         return ldb;
407     }
408
409     return ldb.del(params[1]);
410 }
411 jsonrpc.method.del = _del;
412
413
414 /**
415  * Rename data in the database
416  *
417  * @param params[0]
418  *   The resource id of the open database, previously returned by connect()
419  *
420  * @param params[1]
421  *   The DN to be renamed
422  *
423  * @param params[2]
424  *   The new name for the DN being renamed
425  *
426  * @param error
427  *   An object of class JsonRpcError.
428  *
429  * @return
430  *   Success: True
431  *   Failure: False
432  */
433 function _rename(params, error)
434 {
435     if (params.length != 3)
436     {
437         error.setError(JsonRpcError_ParameterMismatch,
438                        "usage: <resource_id> <old_dn> <new_dn>");
439         return error;
440     }
441
442     ldb = session.resources.get(params[0],
443                                 session.resources.Type.ldb,
444                                 error);
445     if (ldb["__type"] == "_JsonRpcError")
446     {
447         return ldb;
448     }
449
450     return ldb.rename(params[1], params[2]);
451 }
452 jsonrpc.method.rename = _rename;
453
454
455 /**
456  * Base64-encode a string
457  *
458  * @param params[0]
459  *   The resource id of the open database, previously returned by connect()
460  *
461  * @param params[1]
462  *   The string to be base64 encoded
463  *
464  * @param error
465  *   An object of class JsonRpcError.
466  *
467  * @return
468  *   Success: encoded string
469  *   Failure: `undefined`
470  */
471 function _base64encode(params, error)
472 {
473     if (params.length != 2)
474     {
475         error.setError(JsonRpcError_ParameterMismatch,
476                        "usage: <resource_id> <string_to_be_encoded>");
477         return error;
478     }
479
480     ldb = session.resources.get(params[0],
481                                 session.resources.Type.ldb,
482                                 error);
483     if (ldb["__type"] == "_JsonRpcError")
484     {
485         return ldb;
486     }
487
488     return ldb.base64encode(params[1]);
489 }
490 jsonrpc.method.base64encode = _base64encode;
491
492
493 /**
494  * Base64-decode a string
495  *
496  * @param params[0]
497  *   The resource id of the open database, previously returned by connect()
498  *
499  * @param params[1]
500  *   The string to be base64 decoded
501  *
502  * @param error
503  *   An object of class JsonRpcError.
504  *
505  * @return
506  *   Success: decoded string
507  *   Failure: `undefined`
508  */
509 function _base64decode(params, error)
510 {
511     if (params.length != 2)
512     {
513         error.setError(JsonRpcError_ParameterMismatch,
514                        "usage: <resource_id> <string_to_be_decoded>");
515         return error;
516     }
517
518     ldb = session.resources.get(params[0],
519                                 session.resources.Type.ldb,
520                                 error);
521     if (ldb["__type"] == "_JsonRpcError")
522     {
523         return ldb;
524     }
525
526     return ldb.base64decode(params[1]);
527 }
528 jsonrpc.method.base64decode = _base64decode;
529
530
531 /**
532  * escape a DN
533  *
534  * @param params[0]
535  *   The resource id of the open database, previously returned by connect()
536  *
537  * @param params[1]
538  *   The DN to be escaped
539  *
540  * @param error
541  *   An object of class JsonRpcError.
542  *
543  * @return
544  *   Success: escaped string
545  *   Failure: undefined
546  */
547 function _base64decode(params, error)
548 {
549     if (params.length != 2)
550     {
551         error.setError(JsonRpcError_ParameterMismatch,
552                        "usage: <resource_id> <string_to_be_decoded>");
553         return error;
554     }
555
556     ldb = session.resources.get(params[0],
557                                 session.resources.Type.ldb,
558                                 error);
559     if (ldb["__type"] == "_JsonRpcError")
560     {
561         return ldb;
562     }
563
564     return ldb.base64decode(params[1]);
565 }
566 jsonrpc.method.base64decode = _base64decode;
567
568
569 /**
570  * Retrieve a description of the most recent error
571  *
572  * @param params[0]
573  *   The resource id of the open database, previously returned by connect()
574  *
575  * @param error
576  *   An object of class JsonRpcError.
577  *
578  * @return
579  *   The most recent error string for the ldb specified by the resource id
580  */
581 function _errstring(params, error)
582 {
583     if (params.length != 1)
584     {
585         error.setError(JsonRpcError_ParameterMismatch,
586                        "usage: <resource_id>");
587         return error;
588     }
589
590     ldb = session.resources.get(params[0],
591                                 session.resources.Type.ldb,
592                                 error);
593     if (ldb["__type"] == "_JsonRpcError")
594     {
595         return ldb;
596     }
597
598     return ldb.errstring();
599 }
600 jsonrpc.method.errstring = _errstring;
601
602
603
604
605 /*
606  * Local Variables:
607  * mode: c
608  * End:
609  */
610 %>