1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | LDB wrap functions
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 2004-2009
|
---|
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 3 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, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*
|
---|
23 | the stupidity of the unix fcntl locking design forces us to never
|
---|
24 | allow a database file to be opened twice in the same process. These
|
---|
25 | wrappers provide convenient access to a tdb or ldb, taking advantage
|
---|
26 | of talloc destructors to ensure that only a single open is done
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include "includes.h"
|
---|
30 | #include "lib/events/events.h"
|
---|
31 | #include <ldb.h>
|
---|
32 | #include <ldb_errors.h>
|
---|
33 | #include "lib/ldb-samba/ldif_handlers.h"
|
---|
34 | #include "ldb_wrap.h"
|
---|
35 | #include "dsdb/samdb/samdb.h"
|
---|
36 | #include "param/param.h"
|
---|
37 | #include "../lib/util/dlinklist.h"
|
---|
38 | #include <tdb.h>
|
---|
39 |
|
---|
40 | /*
|
---|
41 | this is used to catch debug messages from ldb
|
---|
42 | */
|
---|
43 | static void ldb_wrap_debug(void *context, enum ldb_debug_level level,
|
---|
44 | const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
|
---|
45 |
|
---|
46 | static void ldb_wrap_debug(void *context, enum ldb_debug_level level,
|
---|
47 | const char *fmt, va_list ap)
|
---|
48 | {
|
---|
49 | int samba_level = -1;
|
---|
50 | char *s = NULL;
|
---|
51 | switch (level) {
|
---|
52 | case LDB_DEBUG_FATAL:
|
---|
53 | samba_level = 0;
|
---|
54 | break;
|
---|
55 | case LDB_DEBUG_ERROR:
|
---|
56 | samba_level = 1;
|
---|
57 | break;
|
---|
58 | case LDB_DEBUG_WARNING:
|
---|
59 | samba_level = 2;
|
---|
60 | break;
|
---|
61 | case LDB_DEBUG_TRACE:
|
---|
62 | samba_level = 5;
|
---|
63 | break;
|
---|
64 |
|
---|
65 | };
|
---|
66 | vasprintf(&s, fmt, ap);
|
---|
67 | if (!s) return;
|
---|
68 | DEBUG(samba_level, ("ldb: %s\n", s));
|
---|
69 | free(s);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | /*
|
---|
74 | connecting to a ldb can be a relatively expensive operation because
|
---|
75 | of the schema and partition loads. We keep a list of open ldb
|
---|
76 | contexts here, and try to re-use when possible.
|
---|
77 |
|
---|
78 | This means callers of ldb_wrap_connect() must use talloc_unlink() or
|
---|
79 | the free of a parent to destroy the context
|
---|
80 | */
|
---|
81 | static struct ldb_wrap {
|
---|
82 | struct ldb_wrap *next, *prev;
|
---|
83 | struct ldb_wrap_context {
|
---|
84 | /* the context is what we use to tell if two ldb
|
---|
85 | * connections are exactly equivalent
|
---|
86 | */
|
---|
87 | const char *url;
|
---|
88 | struct tevent_context *ev;
|
---|
89 | struct loadparm_context *lp_ctx;
|
---|
90 | struct auth_session_info *session_info;
|
---|
91 | struct cli_credentials *credentials;
|
---|
92 | unsigned int flags;
|
---|
93 | } context;
|
---|
94 | struct ldb_context *ldb;
|
---|
95 | } *ldb_wrap_list;
|
---|
96 |
|
---|
97 | /*
|
---|
98 | free a ldb_wrap structure
|
---|
99 | */
|
---|
100 | static int ldb_wrap_destructor(struct ldb_wrap *w)
|
---|
101 | {
|
---|
102 | DLIST_REMOVE(ldb_wrap_list, w);
|
---|
103 | return 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * The casefolder for s4's LDB databases - Unicode-safe
|
---|
108 | */
|
---|
109 | char *wrap_casefold(void *context, void *mem_ctx, const char *s, size_t n)
|
---|
110 | {
|
---|
111 | return strupper_talloc_n(mem_ctx, s, n);
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | struct ldb_context *samba_ldb_init(TALLOC_CTX *mem_ctx,
|
---|
116 | struct tevent_context *ev,
|
---|
117 | struct loadparm_context *lp_ctx,
|
---|
118 | struct auth_session_info *session_info,
|
---|
119 | struct cli_credentials *credentials)
|
---|
120 | {
|
---|
121 | struct ldb_context *ldb;
|
---|
122 | int ret;
|
---|
123 |
|
---|
124 | ldb = ldb_init(mem_ctx, ev);
|
---|
125 | if (ldb == NULL) {
|
---|
126 | return NULL;
|
---|
127 | }
|
---|
128 |
|
---|
129 | ldb_set_modules_dir(ldb,
|
---|
130 | talloc_asprintf(ldb,
|
---|
131 | "%s/ldb",
|
---|
132 | lpcfg_modulesdir(lp_ctx)));
|
---|
133 |
|
---|
134 | ldb_set_debug(ldb, ldb_wrap_debug, NULL);
|
---|
135 |
|
---|
136 | ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
|
---|
137 |
|
---|
138 | if (session_info) {
|
---|
139 | if (ldb_set_opaque(ldb, "sessionInfo", session_info)) {
|
---|
140 | talloc_free(ldb);
|
---|
141 | return NULL;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (credentials) {
|
---|
146 | if (ldb_set_opaque(ldb, "credentials", credentials)) {
|
---|
147 | talloc_free(ldb);
|
---|
148 | return NULL;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (ldb_set_opaque(ldb, "loadparm", lp_ctx)) {
|
---|
153 | talloc_free(ldb);
|
---|
154 | return NULL;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /* This must be done before we load the schema, as these
|
---|
158 | * handlers for objectSid and objectGUID etc must take
|
---|
159 | * precedence over the 'binary attribute' declaration in the
|
---|
160 | * schema */
|
---|
161 | ret = ldb_register_samba_handlers(ldb);
|
---|
162 | if (ret != LDB_SUCCESS) {
|
---|
163 | talloc_free(ldb);
|
---|
164 | return NULL;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* we usually want Samba databases to be private. If we later
|
---|
168 | find we need one public, we will need to add a parameter to
|
---|
169 | ldb_wrap_connect() */
|
---|
170 | ldb_set_create_perms(ldb, 0600);
|
---|
171 |
|
---|
172 | return ldb;
|
---|
173 | }
|
---|
174 |
|
---|
175 | struct ldb_context *ldb_wrap_find(const char *url,
|
---|
176 | struct tevent_context *ev,
|
---|
177 | struct loadparm_context *lp_ctx,
|
---|
178 | struct auth_session_info *session_info,
|
---|
179 | struct cli_credentials *credentials,
|
---|
180 | int flags)
|
---|
181 | {
|
---|
182 | struct ldb_wrap *w;
|
---|
183 | /* see if we can re-use an existing ldb */
|
---|
184 | for (w=ldb_wrap_list; w; w=w->next) {
|
---|
185 | if (w->context.ev == ev &&
|
---|
186 | w->context.lp_ctx == lp_ctx &&
|
---|
187 | w->context.session_info == session_info &&
|
---|
188 | w->context.credentials == credentials &&
|
---|
189 | w->context.flags == flags &&
|
---|
190 | (w->context.url == url || strcmp(w->context.url, url) == 0))
|
---|
191 | return w->ldb;
|
---|
192 | }
|
---|
193 |
|
---|
194 | return NULL;
|
---|
195 | }
|
---|
196 |
|
---|
197 | int samba_ldb_connect(struct ldb_context *ldb, struct loadparm_context *lp_ctx,
|
---|
198 | const char *url, int flags)
|
---|
199 | {
|
---|
200 | int ret;
|
---|
201 | char *real_url = NULL;
|
---|
202 |
|
---|
203 | /* allow admins to force non-sync ldb for all databases */
|
---|
204 | if (lpcfg_parm_bool(lp_ctx, NULL, "ldb", "nosync", false)) {
|
---|
205 | flags |= LDB_FLG_NOSYNC;
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (DEBUGLVL(10)) {
|
---|
209 | flags |= LDB_FLG_ENABLE_TRACING;
|
---|
210 | }
|
---|
211 |
|
---|
212 | real_url = private_path(ldb, lp_ctx, url);
|
---|
213 | if (real_url == NULL) {
|
---|
214 | return LDB_ERR_OPERATIONS_ERROR;
|
---|
215 | }
|
---|
216 |
|
---|
217 | ret = ldb_connect(ldb, real_url, flags, NULL);
|
---|
218 |
|
---|
219 | if (ret != LDB_SUCCESS) {
|
---|
220 | return ret;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /* setup for leak detection */
|
---|
224 | ldb_set_opaque(ldb, "wrap_url", real_url);
|
---|
225 |
|
---|
226 | return LDB_SUCCESS;
|
---|
227 | }
|
---|
228 |
|
---|
229 | bool ldb_wrap_add(const char *url, struct tevent_context *ev,
|
---|
230 | struct loadparm_context *lp_ctx,
|
---|
231 | struct auth_session_info *session_info,
|
---|
232 | struct cli_credentials *credentials,
|
---|
233 | int flags,
|
---|
234 | struct ldb_context *ldb)
|
---|
235 | {
|
---|
236 | struct ldb_wrap *w;
|
---|
237 | struct ldb_wrap_context c;
|
---|
238 |
|
---|
239 | /* add to the list of open ldb contexts */
|
---|
240 | w = talloc(ldb, struct ldb_wrap);
|
---|
241 | if (w == NULL) {
|
---|
242 | return false;
|
---|
243 | }
|
---|
244 |
|
---|
245 | c.url = url;
|
---|
246 | c.ev = ev;
|
---|
247 | c.lp_ctx = lp_ctx;
|
---|
248 | c.session_info = session_info;
|
---|
249 | c.credentials = credentials;
|
---|
250 | c.flags = flags;
|
---|
251 |
|
---|
252 | w->context = c;
|
---|
253 | w->context.url = talloc_strdup(w, url);
|
---|
254 | if (w->context.url == NULL) {
|
---|
255 | return false;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (session_info) {
|
---|
259 | /* take a reference to the session_info, as it is
|
---|
260 | * possible for the ldb to live longer than the
|
---|
261 | * session_info. This happens when a DRS DsBind call
|
---|
262 | * reuses a handle, but the original connection is
|
---|
263 | * shutdown. The token for the new connection is still
|
---|
264 | * valid, so we need the session_info to remain valid for
|
---|
265 | * ldb modules to use
|
---|
266 | */
|
---|
267 | if (talloc_reference(w, session_info) == NULL) {
|
---|
268 | return false;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | w->ldb = ldb;
|
---|
273 |
|
---|
274 | DLIST_ADD(ldb_wrap_list, w);
|
---|
275 |
|
---|
276 | talloc_set_destructor(w, ldb_wrap_destructor);
|
---|
277 |
|
---|
278 | return true;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | /*
|
---|
283 | wrapped connection to a ldb database
|
---|
284 | to close just talloc_free() the returned ldb_context
|
---|
285 |
|
---|
286 | TODO: We need an error_string parameter
|
---|
287 | */
|
---|
288 | struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
|
---|
289 | struct tevent_context *ev,
|
---|
290 | struct loadparm_context *lp_ctx,
|
---|
291 | const char *url,
|
---|
292 | struct auth_session_info *session_info,
|
---|
293 | struct cli_credentials *credentials,
|
---|
294 | unsigned int flags)
|
---|
295 | {
|
---|
296 | struct ldb_context *ldb;
|
---|
297 | int ret;
|
---|
298 |
|
---|
299 | ldb = ldb_wrap_find(url, ev, lp_ctx, session_info, credentials, flags);
|
---|
300 | if (ldb != NULL)
|
---|
301 | return talloc_reference(mem_ctx, ldb);
|
---|
302 |
|
---|
303 | ldb = samba_ldb_init(mem_ctx, ev, lp_ctx, session_info, credentials);
|
---|
304 |
|
---|
305 | if (ldb == NULL)
|
---|
306 | return NULL;
|
---|
307 |
|
---|
308 | ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
|
---|
309 | if (ret != LDB_SUCCESS) {
|
---|
310 | talloc_free(ldb);
|
---|
311 | return NULL;
|
---|
312 | }
|
---|
313 |
|
---|
314 | if (!ldb_wrap_add(url, ev, lp_ctx, session_info, credentials, flags, ldb)) {
|
---|
315 | talloc_free(ldb);
|
---|
316 | return NULL;
|
---|
317 | }
|
---|
318 |
|
---|
319 | DEBUG(3,("ldb_wrap open of %s\n", url));
|
---|
320 |
|
---|
321 | return ldb;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /*
|
---|
325 | when we fork() we need to make sure that any open ldb contexts have
|
---|
326 | any open transactions cancelled
|
---|
327 | */
|
---|
328 | void ldb_wrap_fork_hook(void)
|
---|
329 | {
|
---|
330 | struct ldb_wrap *w;
|
---|
331 |
|
---|
332 | for (w=ldb_wrap_list; w; w=w->next) {
|
---|
333 | if (ldb_transaction_cancel_noerr(w->ldb) != LDB_SUCCESS) {
|
---|
334 | smb_panic("Failed to cancel child transactions\n");
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (tdb_reopen_all(1) == -1) {
|
---|
339 | smb_panic("tdb_reopen_all failed\n");
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | char *ldb_relative_path(struct ldb_context *ldb,
|
---|
344 | TALLOC_CTX *mem_ctx,
|
---|
345 | const char *name)
|
---|
346 | {
|
---|
347 | const char *base_url =
|
---|
348 | (const char *)ldb_get_opaque(ldb, "ldb_url");
|
---|
349 | char *path, *p, *full_name;
|
---|
350 | if (name == NULL) {
|
---|
351 | return NULL;
|
---|
352 | }
|
---|
353 | if (strncmp("tdb://", base_url, 6) == 0) {
|
---|
354 | base_url = base_url+6;
|
---|
355 | }
|
---|
356 | path = talloc_strdup(mem_ctx, base_url);
|
---|
357 | if (path == NULL) {
|
---|
358 | return NULL;
|
---|
359 | }
|
---|
360 | if ( (p = strrchr(path, '/')) != NULL) {
|
---|
361 | p[0] = '\0';
|
---|
362 | full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
|
---|
363 | } else {
|
---|
364 | full_name = talloc_asprintf(mem_ctx, "./%s", name);
|
---|
365 | }
|
---|
366 | talloc_free(path);
|
---|
367 | return full_name;
|
---|
368 | }
|
---|