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 "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "lib/ldb/include/ldb.h"
31 static struct ldb_context *ejs_ldb_db(int eid, struct MprVar *v)
33 struct ldb_context *ldb = mprGetPtr(v, "ldb");
35 ejsSetErrorMsg(eid, "invalid ldb connection");
41 perform an ldb search, returning an array of results
44 res = ldb.search(db, "expression");
45 var attrs = new Array("attr1", "attr2", "attr3");
46 ldb.search(db, "expression", attrs);
48 static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv)
50 const char **attrs = NULL;
51 const char *expression;
52 TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
53 struct ldb_context *ldb;
55 struct ldb_message **res;
57 /* validate arguments */
58 if (argc < 2 || argc > 3) {
59 ejsSetErrorMsg(eid, "ldb.search invalid arguments");
62 if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
63 ejsSetErrorMsg(eid, "ldb.search attributes must be an object");
67 ldb = ejs_ldb_db(eid, argv[0]);
72 expression = mprToString(argv[1]);
73 if (expression == NULL) {
74 ejsSetErrorMsg(eid, "ldb.search invalid arguments");
78 attrs = mprToList(tmp_ctx, argv[2]);
81 ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
83 ejsSetErrorMsg(eid, "ldb.search failed - %s", ldb_errstring(ldb));
84 mpr_Return(eid, mprCreateUndefinedVar());
86 mpr_Return(eid, mprLdbArray(res, ret, "ldb_message"));
99 perform an ldb add or modify
101 static int ejs_ldbAddModify(MprVarHandle eid, int argc, struct MprVar **argv,
102 int fn(struct ldb_context *, const struct ldb_message *))
104 const char *ldifstring;
105 struct ldb_context *ldb;
106 struct ldb_ldif *ldif;
110 ejsSetErrorMsg(eid, "ldb.add/modify invalid arguments");
114 ldifstring = mprToString(argv[1]);
115 if (ldifstring == NULL) {
116 ejsSetErrorMsg(eid, "ldb.add/modify invalid arguments");
120 ldb = ejs_ldb_db(eid, argv[0]);
125 while ((ldif = ldb_ldif_read_string(ldb, &ldifstring))) {
126 ret = fn(ldb, ldif->msg);
131 mpr_Return(eid, mprCreateBoolVar(ret == 0));
137 perform an ldb delete
139 ok = ldb.delete(db, dn);
141 static int ejs_ldbDelete(MprVarHandle eid, int argc, struct MprVar **argv)
144 struct ldb_context *ldb;
148 ejsSetErrorMsg(eid, "ldb.delete invalid arguments");
152 dn = mprToString(argv[1]);
154 ldb = ejs_ldb_db(eid, argv[0]);
158 ret = ldb_delete(ldb, dn);
160 mpr_Return(eid, mprCreateBoolVar(ret == 0));
165 perform an ldb rename
167 ok = ldb.rename(db, dn1, dn2);
169 static int ejs_ldbRename(MprVarHandle eid, int argc, struct MprVar **argv)
171 const char *dn1, *dn2;
172 struct ldb_context *ldb;
176 ejsSetErrorMsg(eid, "ldb.rename invalid arguments");
180 dn1 = mprToString(argv[1]);
181 dn2 = mprToString(argv[2]);
182 if (dn1 == NULL || dn2 == NULL) {
183 ejsSetErrorMsg(eid, "ldb.rename invalid arguments");
187 ldb = ejs_ldb_db(eid, argv[0]);
192 ret = ldb_rename(ldb, dn1, dn2);
194 mpr_Return(eid, mprCreateBoolVar(ret == 0));
199 perform an ldb modify
202 ok = ldb.modify(db, ldifstring);
204 static int ejs_ldbAdd(MprVarHandle eid, int argc, struct MprVar **argv)
206 return ejs_ldbAddModify(eid, argc, argv, ldb_add);
213 ok = ldb.add(db, ldifstring);
215 static int ejs_ldbModify(MprVarHandle eid, int argc, struct MprVar **argv)
217 return ejs_ldbAddModify(eid, argc, argv, ldb_modify);
221 connect to a database
223 db = ldb.connect(dbfile);
225 static int ejs_ldbConnect(MprVarHandle eid, int argc, char **argv)
227 struct ldb_context *ldb;
232 ejsSetErrorMsg(eid, "ldb.connect invalid arguments");
238 ldb = ldb_wrap_connect(mprMemCtx(), dbfile, 0, NULL);
240 ejsSetErrorMsg(eid, "ldb.connect failed to open %s", dbfile);
241 mpr_Return(eid, mprCreateUndefinedVar());
245 mprSetPtrChild(&v, "ldb", ldb);
253 initialise ldb ejs subsystem
255 static int ejs_ldb_init(MprVarHandle eid, int argc, struct MprVar **argv)
257 struct MprVar ldb = mprObject("ldb");
259 mprSetStringCFunction(&ldb, "connect", ejs_ldbConnect);
260 mprSetCFunction(&ldb, "search", ejs_ldbSearch);
261 mprSetCFunction(&ldb, "add", ejs_ldbAdd);
262 mprSetCFunction(&ldb, "modify", ejs_ldbModify);
263 mprSetCFunction(&ldb, "delete", ejs_ldbDelete);
264 mprSetCFunction(&ldb, "rename", ejs_ldbRename);
266 mpr_Return(eid, ldb);
272 setup C functions that be called from ejs
274 void smb_setup_ejs_ldb(void)
276 ejsDefineCFunction(-1, "ldb_init", ejs_ldb_init, NULL, MPR_VAR_SCRIPT_HANDLE);