ldb: Permit desactivation of autocomit for every ldb_xxx_ctrl function
[Samba/gbeck.git] / lib / ldb / tools / ldbutil.c
blob882bc67be530a1cff0b435afd536845474a59df2
1 /*
2 ldb database library utility
4 Copyright (C) Matthieu Patou 2009
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 * Name: ldb
27 * Description: Common function used by ldb_add/ldb_modify/ldb_delete
29 * Author: Matthieu Patou
32 #include "ldb.h"
33 #include "ldb_module.h"
34 #include "ldbutil.h"
37 /* autostarts a transacion if none active */
38 static int ldb_do_autotransaction(struct ldb_context *ldb,
39 struct ldb_request *req)
41 int ret;
43 ret = ldb_transaction_start(ldb);
44 if (ret != LDB_SUCCESS) {
45 return ret;
48 ret = ldb_request(ldb, req);
49 if (ret == LDB_SUCCESS) {
50 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
53 if (ret == LDB_SUCCESS) {
54 return ldb_transaction_commit(ldb);
56 ldb_transaction_cancel(ldb);
58 if (ldb_errstring(ldb) == NULL) {
59 /* no error string was setup by the backend */
60 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
63 return ret;
66 Same as ldb_add but accept control
68 int ldb_add_ctrl(struct ldb_context *ldb,
69 const struct ldb_message *message,
70 struct ldb_control **controls, bool do_transaction)
72 struct ldb_request *req;
73 int ret;
75 ret = ldb_msg_sanity_check(ldb, message);
76 if (ret != LDB_SUCCESS) {
77 return ret;
80 ret = ldb_build_add_req(&req, ldb, ldb,
81 message,
82 controls,
83 NULL,
84 ldb_op_default_callback,
85 NULL);
87 if (ret != LDB_SUCCESS) return ret;
89 /* do request and autostart a transaction */
90 if (do_transaction)
91 ret = ldb_do_autotransaction(ldb, req);
93 talloc_free(req);
94 return ret;
98 same as ldb_delete but accept control
100 int ldb_delete_ctrl(struct ldb_context *ldb, struct ldb_dn *dn,
101 struct ldb_control **controls, bool do_transaction)
103 struct ldb_request *req;
104 int ret;
106 ret = ldb_build_del_req(&req, ldb, ldb,
108 controls,
109 NULL,
110 ldb_op_default_callback,
111 NULL);
113 if (ret != LDB_SUCCESS) return ret;
115 /* do request and autostart a transaction */
116 if (do_transaction)
117 ret = ldb_do_autotransaction(ldb, req);
119 talloc_free(req);
120 return ret;
125 same as ldb_modify, but accepts controls
127 int ldb_modify_ctrl(struct ldb_context *ldb,
128 const struct ldb_message *message,
129 struct ldb_control **controls, bool do_transaction)
131 struct ldb_request *req;
132 int ret;
134 ret = ldb_msg_sanity_check(ldb, message);
135 if (ret != LDB_SUCCESS) {
136 return ret;
139 ret = ldb_build_mod_req(&req, ldb, ldb,
140 message,
141 controls,
142 NULL,
143 ldb_op_default_callback,
144 NULL);
146 if (ret != LDB_SUCCESS) return ret;
148 /* do request and autostart a transaction */
149 if (do_transaction)
150 ret = ldb_do_autotransaction(ldb, req);
152 talloc_free(req);
153 return ret;
158 ldb_search with controls
160 int ldb_search_ctrl(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
161 struct ldb_result **result, struct ldb_dn *base,
162 enum ldb_scope scope, const char * const *attrs,
163 struct ldb_control **controls,
164 const char *exp_fmt, ...)
166 struct ldb_request *req;
167 struct ldb_result *res;
168 char *expression;
169 va_list ap;
170 int ret;
172 expression = NULL;
173 *result = NULL;
174 req = NULL;
176 res = talloc_zero(mem_ctx, struct ldb_result);
177 if (!res) {
178 return LDB_ERR_OPERATIONS_ERROR;
181 if (exp_fmt) {
182 va_start(ap, exp_fmt);
183 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
184 va_end(ap);
186 if (!expression) {
187 talloc_free(res);
188 return LDB_ERR_OPERATIONS_ERROR;
192 ret = ldb_build_search_req(&req, ldb, mem_ctx,
193 base?base:ldb_get_default_basedn(ldb),
194 scope,
195 expression,
196 attrs,
197 controls,
198 res,
199 ldb_search_default_callback,
200 NULL);
201 ldb_req_set_location(req, "ldb_search_ctrl");
203 if (ret != LDB_SUCCESS) goto done;
205 ret = ldb_request(ldb, req);
207 if (ret == LDB_SUCCESS) {
208 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
211 done:
212 if (ret != LDB_SUCCESS) {
213 talloc_free(res);
214 res = NULL;
217 talloc_free(expression);
218 talloc_free(req);
220 *result = res;
221 return ret;