summaryrefslogtreecommitdiff
path: root/services/samba/ldb.esp
blob: 2eff5ba57d02376f445451333f9bd9380b9702ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<%
/*
 * Copyright:
 *   (C) 2006 by Derrell Lipman
 *       All rights reserved
 *
 * License:
 *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
 */

/*
 * JSON-RPC mappings to the ldb ejs functions
 */

/* We'll be saving resources in the session */
jsonrpc_include("resources.esp");


/**
 * Connect to a database
 *
 * @param params[0]
 *   Database name
 *
 * @param params[1..n]
 *   Option (e.g. "modules:modlist")
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: The resource id to be used for future access to the database
 *   Failure: -1
 *
 * @note
 *   Credentials or session_info may be set up first.
 */
function _connect(params, error)
{
    if (params.length < 1)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <db_name> [<option> ...]");
        return error;
    }

    ldb = ldb_init();
    var ret = ldb.connect(params[0]);
    if (ret && ldb.db)
    {
        return session.resources.set(ldb,
                                     session.resources.Type.ldb,
                                     error);
    }
    else
    {
        error.setError(-1, "ldb.connect failed");
        return error;
    }
}
jsonrpc.method.connect = _connect;


/**
 * Close a database
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: True
 *   Failure: Will only fail with invalid parameters, and throws an error
 */
function _close(params, error)
{
    if (params.length != 1)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    var ret = ldb.close();
    
    /* If close succeeded, release the stored resource */
    if (ret)
    {
        session.resources.release(params[0], error);
    }

    return ret;
}
jsonrpc.method.close = _close;


/**
 * Begin a transaction
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: True
 *   Failure: False
 */
function _transaction_start(params, error)
{
    if (params.length != 1)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.transaction_start();
}
jsonrpc.method.transaction_start = _transaction_start;


/**
 * Cancel a transaction
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: True
 *   Failure: False
 */
function _transaction_cancel(params, error)
{
    if (params.length != 1)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.transaction_cancel();
}
jsonrpc.method.transaction_cancel = _transaction_cancel;


/**
 * Commit a transaction
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: True
 *   Failure: False
 */
function _transaction_commit(params, error)
{
    if (params.length != 1)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.transaction_commit();
}
jsonrpc.method.transaction_commit = _transaction_commit;


/**
 * Issue a Search request
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param params[1]
 *   Search expression
 *
 * @param params[2]
 *   Base DN
 *
 * @param params[3]
 *   Scope: "default", "base", "one" or "subtree"
 *
 * @param params[4]
 *   Attributes: an array object
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: found object
 *   Failure: `undefined`
 *
 * @note
 *   If params[4] is missing, assume no attributes
 *   If params[3..4] are missing, also assume "default" scope
 *   If params[2..4] are missing, also assume null base DN
 */
function _search(params, error)
{
    if (params.length < 2 || params.length > 5)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: " +
                       "<resource_id> <expr> [<baseDN> [<scope> [<attrs>]]]");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    /* Retrieve parameters */
    var expr = params[1];
    var baseDN = params[2];
    var scope = params[3];
    var attrs = params[4];

    /* Fill in optional parameters */
    if (params.length < 3) baseDN = null;
    if (params.length < 4) scope = "one";
    if (params.length < 5) attrs = null;

    /* Determine scope value */
    if (scope == "base")
    {
        scope = ldb.SCOPE_BASE;
    }
    else if (scope == "one")
    {
        scope = ldb.SCOPE_ONE;
    }
    else if (scope == "subtree")
    {
        scope = ldb.SCOPE_SUBTREE;
    }
    else if (scope == "default")
    {
        scope = ldb.SCOPE_DEFAULT;
    }
    else
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "invalid scope");
        return error;
    }

    return ldb.transaction_search(expr, baseDN, scope, attrs);
}
jsonrpc.method.search = _search;


/**
 * Add data to the database
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param params[1]
 *   An LDIF string representing the data to be added
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: True
 *   Failure: False
 */
function _add(params, error)
{
    if (params.length != 2)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id> <ldif>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.add(params[1]);
}
jsonrpc.method.add = _add;


/**
 * Modify data in the database
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param params[1]
 *   An LDIF string representing the data to be modified
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: True
 *   Failure: False
 */
function _modify(params, error)
{
    if (params.length != 2)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id> <ldif>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.modify(params[1]);
}
jsonrpc.method.modify = _modify;


/**
 * Delete data from the database
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param params[1]
 *   The DN to be located and deleted
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: True
 *   Failure: False
 */
function _del(params, error)
{
    if (params.length != 2)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id> <dn>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.del(params[1]);
}
jsonrpc.method.del = _del;


/**
 * Rename data in the database
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param params[1]
 *   The DN to be renamed
 *
 * @param params[2]
 *   The new name for the DN being renamed
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: True
 *   Failure: False
 */
function _rename(params, error)
{
    if (params.length != 3)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id> <old_dn> <new_dn>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.rename(params[1], params[2]);
}
jsonrpc.method.rename = _rename;


/**
 * Base64-encode a string
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param params[1]
 *   The string to be base64 encoded
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: encoded string
 *   Failure: `undefined`
 */
function _base64encode(params, error)
{
    if (params.length != 2)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id> <string_to_be_encoded>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.base64encode(params[1]);
}
jsonrpc.method.base64encode = _base64encode;


/**
 * Base64-decode a string
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param params[1]
 *   The string to be base64 decoded
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: decoded string
 *   Failure: `undefined`
 */
function _base64decode(params, error)
{
    if (params.length != 2)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id> <string_to_be_decoded>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.base64decode(params[1]);
}
jsonrpc.method.base64decode = _base64decode;


/**
 * escape a DN
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param params[1]
 *   The DN to be escaped
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: escaped string
 *   Failure: undefined
 */
function _base64decode(params, error)
{
    if (params.length != 2)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id> <string_to_be_decoded>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.base64decode(params[1]);
}
jsonrpc.method.base64decode = _base64decode;


/**
 * Retrieve a description of the most recent error
 *
 * @param params[0]
 *   The resource id of the open database, previously returned by connect()
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   The most recent error string for the ldb specified by the resource id
 */
function _errstring(params, error)
{
    if (params.length != 1)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "usage: <resource_id>");
        return error;
    }

    ldb = session.resources.get(params[0],
                                session.resources.Type.ldb,
                                error);
    if (ldb["__type"] == "_JsonRpcError")
    {
        return ldb;
    }

    return ldb.errstring();
}
jsonrpc.method.errstring = _errstring;




/*
 * Local Variables:
 * mode: c
 * End:
 */
%>