2 Unix SMB/CIFS implementation.
4 provide hooks into smbd C calls from ejs scripts
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/ejs/ejs.h"
25 #include "lib/ldb/include/ldb.h"
28 perform an ldb search, returning an array of results
31 ldbSearch("dbfile", "expression");
32 var attrs = new Array("attr1", "attr2", "attr3");
33 ldbSearch("dbfile", "expression", attrs);
35 static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv)
37 const char **attrs = NULL;
38 const char *expression, *dbfile;
39 TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
40 struct ldb_context *ldb;
42 struct ldb_message **res;
44 /* validate arguments */
45 if (argc < 2 || argc > 3 ||
46 argv[0]->type != MPR_TYPE_STRING) {
47 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
50 if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
51 ejsSetErrorMsg(eid, "ldbSearch attributes must be an object");
55 dbfile = mprToString(argv[0]);
56 expression = mprToString(argv[1]);
58 attrs = mprToList(tmp_ctx, argv[2]);
60 if (dbfile == NULL || expression == NULL) {
61 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
65 ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
67 ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile);
71 ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
73 ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb));
77 ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message"));
88 setup C functions that be called from ejs
90 void smb_setup_ejs_ldb(void)
92 ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE);