Line data Source code
1 : /*
2 : ldb database library utility
3 :
4 : Copyright (C) Matthieu Patou 2009
5 :
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
9 :
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.
14 :
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.
19 :
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/>.
22 : */
23 :
24 : /*
25 : * Name: ldb
26 : *
27 : * Description: Common function used by ldb_add/ldb_modify/ldb_delete
28 : *
29 : * Author: Matthieu Patou
30 : */
31 :
32 : #include "replace.h"
33 : #include "ldb.h"
34 : #include "ldb_module.h"
35 : #include "ldbutil.h"
36 :
37 :
38 : /* autostarts a transaction if none active */
39 5642 : static int ldb_do_autotransaction(struct ldb_context *ldb,
40 : struct ldb_request *req)
41 : {
42 132 : int ret;
43 :
44 5642 : ret = ldb_transaction_start(ldb);
45 5642 : if (ret != LDB_SUCCESS) {
46 0 : return ret;
47 : }
48 :
49 5642 : ret = ldb_request(ldb, req);
50 5642 : if (ret == LDB_SUCCESS) {
51 5640 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
52 : }
53 :
54 5642 : if (ret == LDB_SUCCESS) {
55 5635 : return ldb_transaction_commit(ldb);
56 : }
57 7 : ldb_transaction_cancel(ldb);
58 :
59 7 : if (ldb_errstring(ldb) == NULL) {
60 : /* no error string was setup by the backend */
61 0 : ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
62 : }
63 :
64 2 : return ret;
65 : }
66 : /*
67 : Same as ldb_add but accept control
68 : */
69 2719 : int ldb_add_ctrl(struct ldb_context *ldb,
70 : const struct ldb_message *message,
71 : struct ldb_control **controls)
72 : {
73 82 : struct ldb_request *req;
74 82 : int ret;
75 :
76 2719 : ret = ldb_msg_sanity_check(ldb, message);
77 2719 : if (ret != LDB_SUCCESS) {
78 0 : return ret;
79 : }
80 :
81 2719 : ret = ldb_build_add_req(&req, ldb, ldb,
82 : message,
83 : controls,
84 : NULL,
85 : ldb_op_default_callback,
86 : NULL);
87 :
88 2719 : if (ret != LDB_SUCCESS) return ret;
89 :
90 : /* do request and autostart a transaction */
91 2719 : ret = ldb_do_autotransaction(ldb, req);
92 :
93 2719 : talloc_free(req);
94 2719 : return ret;
95 : }
96 :
97 : /*
98 : same as ldb_delete but accept control
99 : */
100 2497 : int ldb_delete_ctrl(struct ldb_context *ldb, struct ldb_dn *dn,
101 : struct ldb_control **controls)
102 : {
103 5 : struct ldb_request *req;
104 5 : int ret;
105 :
106 2497 : ret = ldb_build_del_req(&req, ldb, ldb,
107 : dn,
108 : controls,
109 : NULL,
110 : ldb_op_default_callback,
111 : NULL);
112 :
113 2497 : if (ret != LDB_SUCCESS) return ret;
114 :
115 : /* do request and autostart a transaction */
116 2497 : ret = ldb_do_autotransaction(ldb, req);
117 :
118 2497 : talloc_free(req);
119 2497 : return ret;
120 : }
121 :
122 :
123 : /*
124 : same as ldb_modify, but accepts controls
125 : */
126 426 : int ldb_modify_ctrl(struct ldb_context *ldb,
127 : const struct ldb_message *message,
128 : struct ldb_control **controls)
129 : {
130 45 : struct ldb_request *req;
131 45 : int ret;
132 :
133 426 : ret = ldb_msg_sanity_check(ldb, message);
134 426 : if (ret != LDB_SUCCESS) {
135 0 : return ret;
136 : }
137 :
138 426 : ret = ldb_build_mod_req(&req, ldb, ldb,
139 : message,
140 : controls,
141 : NULL,
142 : ldb_op_default_callback,
143 : NULL);
144 :
145 426 : if (ret != LDB_SUCCESS) return ret;
146 :
147 : /* do request and autostart a transaction */
148 426 : ret = ldb_do_autotransaction(ldb, req);
149 :
150 426 : talloc_free(req);
151 426 : return ret;
152 : }
153 :
154 :
155 : /*
156 : ldb_search with controls
157 : */
158 0 : int ldb_search_ctrl(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
159 : struct ldb_result **result, struct ldb_dn *base,
160 : enum ldb_scope scope, const char * const *attrs,
161 : struct ldb_control **controls,
162 : const char *exp_fmt, ...)
163 : {
164 0 : struct ldb_request *req;
165 0 : struct ldb_result *res;
166 0 : char *expression;
167 0 : va_list ap;
168 0 : int ret;
169 :
170 0 : expression = NULL;
171 0 : *result = NULL;
172 0 : req = NULL;
173 :
174 0 : res = talloc_zero(mem_ctx, struct ldb_result);
175 0 : if (!res) {
176 0 : return LDB_ERR_OPERATIONS_ERROR;
177 : }
178 :
179 0 : if (exp_fmt) {
180 0 : va_start(ap, exp_fmt);
181 0 : expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
182 0 : va_end(ap);
183 :
184 0 : if (!expression) {
185 0 : talloc_free(res);
186 0 : return LDB_ERR_OPERATIONS_ERROR;
187 : }
188 : }
189 :
190 0 : ret = ldb_build_search_req(&req, ldb, mem_ctx,
191 0 : base?base:ldb_get_default_basedn(ldb),
192 : scope,
193 : expression,
194 : attrs,
195 : controls,
196 : res,
197 : ldb_search_default_callback,
198 : NULL);
199 0 : ldb_req_set_location(req, "ldb_search_ctrl");
200 :
201 0 : if (ret != LDB_SUCCESS) goto done;
202 :
203 0 : ret = ldb_request(ldb, req);
204 :
205 0 : if (ret == LDB_SUCCESS) {
206 0 : ret = ldb_wait(req->handle, LDB_WAIT_ALL);
207 : }
208 :
209 0 : done:
210 0 : if (ret != LDB_SUCCESS) {
211 0 : talloc_free(res);
212 0 : res = NULL;
213 : }
214 :
215 0 : talloc_free(expression);
216 0 : talloc_free(req);
217 :
218 0 : *result = res;
219 0 : return ret;
220 : }
|