r8340: - added sys_gmtime()
[sfrench/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 "scripting/ejs/smbcalls.h"
25 #include "lib/ejs/ejs.h"
26 #include "lib/ldb/include/ldb.h"
27
28 /*
29   perform an ldb search, returning an array of results
30
31   syntax:
32      ldbSearch("dbfile", "expression");
33      var attrs = new Array("attr1", "attr2", "attr3");
34      ldbSearch("dbfile", "expression", attrs);
35 */
36 static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv)
37 {
38         const char **attrs = NULL;
39         const char *expression, *dbfile;
40         TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
41         struct ldb_context *ldb;
42         int ret;
43         struct ldb_message **res;
44
45         /* validate arguments */
46         if (argc < 2 || argc > 3 ||
47             argv[0]->type != MPR_TYPE_STRING) {
48                 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
49                 goto failed;
50         }
51         if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
52                 ejsSetErrorMsg(eid, "ldbSearch attributes must be an object");
53                 goto failed;
54         }
55
56         dbfile     = mprToString(argv[0]);
57         expression = mprToString(argv[1]);
58         if (argc > 2) {
59                 attrs = mprToList(tmp_ctx, argv[2]);
60         }
61         if (dbfile == NULL || expression == NULL) {
62                 ejsSetErrorMsg(eid, "ldbSearch invalid arguments");
63                 goto failed;
64         }
65
66         ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
67         if (ldb == NULL) {
68                 ejsSetErrorMsg(eid, "ldbSearch failed to open %s", dbfile);
69                 goto failed;
70         }
71
72         ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
73         if (ret == -1) {
74                 ejsSetErrorMsg(eid, "ldbSearch failed - %s", ldb_errstring(ldb));
75                 goto failed;
76         }
77
78         mpr_Return(eid, mprLdbArray(res, ret, "ldb_message"));
79
80         talloc_free(tmp_ctx);
81         return 0;
82
83 failed:
84         talloc_free(tmp_ctx);
85         return -1;
86 }
87
88
89 /*
90   perform an ldb add or modify
91 */
92 static int ejs_ldbAddModify(MprVarHandle eid, int argc, char **argv,
93                             int fn(struct ldb_context *, const struct ldb_message *))
94 {
95         const char *ldifstring, *dbfile;
96         struct ldb_context *ldb;
97         struct ldb_ldif *ldif;
98         int ret;
99
100         if (argc != 2) {
101                 ejsSetErrorMsg(eid, "ldbAddModify invalid arguments");
102                 return -1;
103         }
104
105         dbfile     = argv[0];
106         ldifstring = argv[1];
107
108         ldb = ldb_wrap_connect(mprMemCtx(), dbfile, 0, NULL);
109         if (ldb == NULL) {
110                 ejsSetErrorMsg(eid, "ldbAddModify failed to open %s", dbfile);
111                 goto failed;
112         }
113
114         while ((ldif = ldb_ldif_read_string(ldb, &ldifstring))) {
115                 ret = fn(ldb, ldif->msg);
116                 talloc_free(ldif);
117         }
118
119         mpr_Return(eid, mprCreateBoolVar(ret == 0));
120         talloc_free(ldb);
121         return 0;
122
123 failed:
124         talloc_free(ldb);
125         return -1;
126 }
127
128
129 /*
130   perform an ldb delete
131   usage:
132    ok = ldbDelete(dbfile, dn);
133 */
134 static int ejs_ldbDelete(MprVarHandle eid, int argc, char **argv)
135 {
136         const char *dn, *dbfile;
137         struct ldb_context *ldb;
138         int ret;
139
140         if (argc != 2) {
141                 ejsSetErrorMsg(eid, "ldbDelete invalid arguments");
142                 return -1;
143         }
144
145         dbfile  = argv[0];
146         dn      = argv[1];
147
148         ldb = ldb_wrap_connect(mprMemCtx(), dbfile, 0, NULL);
149         if (ldb == NULL) {
150                 ejsSetErrorMsg(eid, "ldbDelete failed to open %s", dbfile);
151                 goto failed;
152         }
153
154         ret = ldb_delete(ldb, dn);
155
156         mpr_Return(eid, mprCreateBoolVar(ret == 0));
157         talloc_free(ldb);
158         return 0;
159
160 failed:
161         talloc_free(ldb);
162         return -1;
163 }
164
165 /*
166   perform an ldb rename
167   usage:
168    ok = ldbRename(dbfile, dn1, dn2);
169 */
170 static int ejs_ldbRename(MprVarHandle eid, int argc, char **argv)
171 {
172         const char *dn1, *dn2, *dbfile;
173         struct ldb_context *ldb;
174         int ret;
175
176         if (argc != 3) {
177                 ejsSetErrorMsg(eid, "ldbRename invalid arguments");
178                 return -1;
179         }
180
181         dbfile = argv[0];
182         dn1    = argv[1];
183         dn2    = argv[2];
184
185         ldb = ldb_wrap_connect(mprMemCtx(), dbfile, 0, NULL);
186         if (ldb == NULL) {
187                 ejsSetErrorMsg(eid, "ldbRename failed to open %s", dbfile);
188                 goto failed;
189         }
190
191         ret = ldb_rename(ldb, dn1, dn2);
192
193         mpr_Return(eid, mprCreateBoolVar(ret == 0));
194         talloc_free(ldb);
195         return 0;
196
197 failed:
198         talloc_free(ldb);
199         return -1;
200 }
201
202 /*
203   perform an ldb modify
204
205   syntax:
206     ok = ldbModify("dbfile", ldifstring);
207 */
208 static int ejs_ldbAdd(MprVarHandle eid, int argc, char **argv)
209 {
210         return ejs_ldbAddModify(eid, argc, argv, ldb_add);
211 }
212
213 /*
214   perform an ldb add
215
216   syntax:
217     ok = ldbAdd("dbfile", ldifstring);
218 */
219 static int ejs_ldbModify(MprVarHandle eid, int argc, char **argv)
220 {
221         return ejs_ldbAddModify(eid, argc, argv, ldb_modify);
222 }
223
224
225
226 /*
227   setup C functions that be called from ejs
228 */
229 void smb_setup_ejs_ldb(void)
230 {
231         ejsDefineCFunction(-1, "ldbSearch", ejs_ldbSearch, NULL, MPR_VAR_SCRIPT_HANDLE);
232         ejsDefineStringCFunction(-1, "ldbAdd", ejs_ldbAdd, NULL, MPR_VAR_SCRIPT_HANDLE);
233         ejsDefineStringCFunction(-1, "ldbModify", ejs_ldbModify, NULL, MPR_VAR_SCRIPT_HANDLE);
234         ejsDefineStringCFunction(-1, "ldbDelete", ejs_ldbDelete, NULL, MPR_VAR_SCRIPT_HANDLE);
235         ejsDefineStringCFunction(-1, "ldbRename", ejs_ldbRename, NULL, MPR_VAR_SCRIPT_HANDLE);
236 }