r7266: Split the different types of js function defines into separate files,
[bbaumbach/samba-autobuild/.git] / source4 / scripting / ejs / smbcalls_ldb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide hooks into smbd C calls from ejs scripts
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24 #include "lib/ejs/ejs.h"
25 #include "lib/ldb/include/ldb.h"
26
27 /*
28   perform an ldb search, returning an array of results
29
30   syntax:
31      ldbSearch("dbfile", "expression");
32      var attrs = new Array("attr1", "attr2", "attr3");
33      ldbSearch("dbfile", "expression", attrs);
34 */
35 static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv)
36 {
37         const char **attrs = NULL;
38         const char *expression, *dbfile;
39         TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
40         struct ldb_context *ldb;
41         int ret;
42         struct ldb_message **res;
43
44         /* validate arguments */
45         if (argc < 2 || argc > 3 ||
46             argv[0]->type != MPR_TYPE_STRING) {
47                 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
48                 goto failed;
49         }
50         if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
51                 ejsSetErrorMsg(eid, "ldbSearch attributes must be an object");
52                 goto failed;
53         }
54
55         dbfile     = mprToString(argv[0]);
56         expression = mprToString(argv[1]);
57         if (argc > 2) {
58                 attrs = mprToList(tmp_ctx, argv[2]);
59         }
60         if (dbfile == NULL || expression == NULL) {
61                 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
62                 goto failed;
63         }
64
65         ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
66         if (ldb == NULL) {
67                 ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile);
68                 goto failed;
69         }
70
71         ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
72         if (ret == -1) {
73                 ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb));
74                 goto failed;
75         }
76
77         ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message"));
78
79         talloc_free(tmp_ctx);
80         return 0;
81
82 failed:
83         talloc_free(tmp_ctx);
84         return -1;
85 }
86
87 /*
88   setup C functions that be called from ejs
89 */
90 void smb_setup_ejs_ldb(void)
91 {
92         ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE);
93 }