source: branches/samba-3.3.x/source/groupdb/mapping_ldb.c

Last change on this file was 274, checked in by Herwig Bauernfeind, 16 years ago

Update 3.3 branch to 3.3.5

File size: 16.9 KB
Line 
1/*
2 * Unix SMB/CIFS implementation.
3 *
4 * group mapping code on top of ldb
5 *
6 * Copyright (C) Andrew Tridgell 2006
7 *
8 * based on tdb group mapping code from groupdb/mapping.c
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program 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
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include "includes.h"
25#include "groupdb/mapping.h"
26#include "lib/ldb/include/includes.h"
27#include "lib/ldb/include/ldb_errors.h"
28
29static struct ldb_context *ldb;
30
31static bool mapping_upgrade(const char *tdb_path);
32
33/*
34 connect to the group mapping ldb
35*/
36static bool init_group_mapping(void)
37{
38 bool existed;
39 const char *init_ldif[] =
40 { "dn: @ATTRIBUTES\n" \
41 "ntName: CASE_INSENSITIVE\n" \
42 "\n",
43 "dn: @INDEXLIST\n" \
44 "@IDXATTR: gidNumber\n" \
45 "@IDXATTR: ntName\n" \
46 "@IDXATTR: member\n" };
47 const char *db_path, *tdb_path;
48 int ret;
49 int flags = 0;
50
51 if (ldb != NULL) {
52 return True;
53 }
54
55 /* this is needed as Samba3 doesn't have this globally yet */
56 ldb_global_init();
57
58 db_path = state_path("group_mapping.ldb");
59
60 ldb = ldb_init(NULL);
61 if (ldb == NULL) goto failed;
62
63 /* Ensure this db is created read/write for root only. */
64 ldb_set_create_perms(ldb, 0600);
65
66 existed = file_exist(db_path, NULL);
67
68 if (lp_parm_bool(-1, "groupmap", "nosync", False)) {
69 flags |= LDB_FLG_NOSYNC;
70 }
71
72 if (!lp_use_mmap()) {
73 flags |= LDB_FLG_NOMMAP;
74 }
75
76 ret = ldb_connect(ldb, db_path, flags, NULL);
77 if (ret != LDB_SUCCESS) {
78 goto failed;
79 }
80
81 /* force the permissions on the ldb to 0600 - this will fix
82 existing databases as well as new ones */
83#ifndef __OS2__
84 if (chmod(db_path, 0600) != 0) {
85 goto failed;
86 }
87#endif
88
89 if (!existed) {
90 /* initialise the ldb with an index */
91 struct ldb_ldif *ldif;
92 int i;
93 for (i=0;i<ARRAY_SIZE(init_ldif);i++) {
94 ldif = ldb_ldif_read_string(ldb, &init_ldif[i]);
95 if (ldif == NULL) goto failed;
96 ret = ldb_add(ldb, ldif->msg);
97 talloc_free(ldif);
98 if (ret == -1) goto failed;
99 }
100 }
101
102 /* possibly upgrade */
103 tdb_path = state_path("group_mapping.tdb");
104 if (file_exist(tdb_path, NULL) && !mapping_upgrade(tdb_path)) {
105 unlink(state_path("group_mapping.ldb"));
106 goto failed;
107 }
108
109 return True;
110
111failed:
112 DEBUG(0,("Failed to open group mapping ldb '%s' - '%s'\n",
113 db_path, ldb?ldb_errstring(ldb):strerror(errno)));
114 talloc_free(ldb);
115 ldb = NULL;
116 return False;
117}
118
119
120/*
121 form the DN for a mapping entry from a SID
122 */
123static struct ldb_dn *mapping_dn(TALLOC_CTX *mem_ctx, const DOM_SID *sid)
124{
125 fstring string_sid;
126 uint32_t rid;
127 DOM_SID domsid;
128
129 sid_copy(&domsid, sid);
130 if (!sid_split_rid(&domsid, &rid)) {
131 return NULL;
132 }
133 if (!sid_to_fstring(string_sid, &domsid)) {
134 return NULL;
135 }
136 /* we split by domain and rid so we can do a subtree search
137 when we only want one domain */
138 return ldb_dn_string_compose(mem_ctx, NULL, "rid=%u,domain=%s",
139 rid, string_sid);
140}
141
142/*
143 add a group mapping entry
144 */
145static bool add_mapping_entry(GROUP_MAP *map, int flag)
146{
147 struct ldb_message *msg;
148 int ret, i;
149 fstring string_sid;
150
151 msg = ldb_msg_new(ldb);
152 if (msg == NULL) {
153 return False;
154 }
155
156 msg->dn = mapping_dn(msg, &map->sid);
157 if (msg->dn == NULL) {
158 goto failed;
159 }
160
161 if (ldb_msg_add_string(msg, "objectClass", "groupMap") != LDB_SUCCESS ||
162 ldb_msg_add_string(msg, "sid",
163 sid_to_fstring(string_sid, &map->sid)) != LDB_SUCCESS ||
164 ldb_msg_add_fmt(msg, "gidNumber", "%u", (unsigned)map->gid) != LDB_SUCCESS ||
165 ldb_msg_add_fmt(msg, "sidNameUse", "%u", (unsigned)map->sid_name_use) != LDB_SUCCESS ||
166 ldb_msg_add_string(msg, "comment", map->comment) != LDB_SUCCESS ||
167 ldb_msg_add_string(msg, "ntName", map->nt_name) != LDB_SUCCESS) {
168 goto failed;
169 }
170
171 ret = ldb_add(ldb, msg);
172
173 /* if it exists we update it. This is a hangover from the semantics the
174 tdb backend had */
175 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
176 for (i=0;i<msg->num_elements;i++) {
177 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
178 }
179 ret = ldb_modify(ldb, msg);
180 }
181
182 talloc_free(msg);
183
184 return ret == LDB_SUCCESS;
185
186failed:
187 talloc_free(msg);
188 return False;
189}
190
191/*
192 unpack a ldb message into a GROUP_MAP structure
193*/
194static bool msg_to_group_map(struct ldb_message *msg, GROUP_MAP *map)
195{
196 const char *sidstr;
197
198 map->gid = ldb_msg_find_attr_as_int(msg, "gidNumber", -1);
199 map->sid_name_use = ldb_msg_find_attr_as_int(msg, "sidNameUse", -1);
200 fstrcpy(map->nt_name, ldb_msg_find_attr_as_string(msg, "ntName", NULL));
201 fstrcpy(map->comment, ldb_msg_find_attr_as_string(msg, "comment", NULL));
202 sidstr = ldb_msg_find_attr_as_string(msg, "sid", NULL);
203
204 if (!string_to_sid(&map->sid, sidstr) ||
205 map->gid == (gid_t)-1 ||
206 map->sid_name_use == (enum lsa_SidType)-1) {
207 DEBUG(0,("Unable to unpack group mapping\n"));
208 return False;
209 }
210
211 return True;
212}
213
214/*
215 return a group map entry for a given sid
216*/
217static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
218{
219 int ret;
220 struct ldb_dn *dn;
221 struct ldb_result *res=NULL;
222
223 dn = mapping_dn(ldb, &sid);
224 if (dn == NULL) goto failed;
225
226 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
227 talloc_steal(dn, res);
228 if (ret != LDB_SUCCESS || res->count != 1) {
229 goto failed;
230 }
231
232 if (!msg_to_group_map(res->msgs[0], map)) goto failed;
233
234 talloc_free(dn);
235 return True;
236
237failed:
238 talloc_free(dn);
239 return False;
240}
241
242/*
243 return a group map entry for a given gid
244*/
245static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
246{
247 int ret;
248 char *expr;
249 struct ldb_result *res=NULL;
250
251 expr = talloc_asprintf(ldb, "(&(gidNumber=%u)(objectClass=groupMap))",
252 (unsigned)gid);
253 if (expr == NULL) goto failed;
254
255 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
256 talloc_steal(expr, res);
257 if (ret != LDB_SUCCESS || res->count != 1) goto failed;
258
259 if (!msg_to_group_map(res->msgs[0], map)) goto failed;
260
261 talloc_free(expr);
262 return True;
263
264failed:
265 talloc_free(expr);
266 return False;
267}
268
269/*
270 Return the sid and the type of the unix group.
271*/
272static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
273{
274 int ret;
275 char *expr;
276 struct ldb_result *res=NULL;
277
278 expr = talloc_asprintf(ldb, "(&(ntName=%s)(objectClass=groupMap))", name);
279 if (expr == NULL) goto failed;
280
281 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
282 talloc_steal(expr, res);
283 if (ret != LDB_SUCCESS || res->count != 1) goto failed;
284
285 if (!msg_to_group_map(res->msgs[0], map)) goto failed;
286
287 talloc_free(expr);
288 return True;
289
290failed:
291 talloc_free(expr);
292 return False;
293}
294
295/*
296 Remove a group mapping entry.
297*/
298static bool group_map_remove(const DOM_SID *sid)
299{
300 struct ldb_dn *dn;
301 int ret;
302
303 dn = mapping_dn(ldb, sid);
304 if (dn == NULL) {
305 return False;
306 }
307 ret = ldb_delete(ldb, dn);
308 talloc_free(dn);
309
310 return ret == LDB_SUCCESS;
311}
312
313
314/*
315 Enumerate the group mappings for a domain
316*/
317static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use,
318 GROUP_MAP **pp_rmap,
319 size_t *p_num_entries, bool unix_only)
320{
321 int i, ret;
322 char *expr;
323 fstring name;
324 struct ldb_result *res = NULL;
325 struct ldb_dn *basedn=NULL;
326 TALLOC_CTX *tmp_ctx;
327
328 tmp_ctx = talloc_new(ldb);
329 if (tmp_ctx == NULL) goto failed;
330
331 if (sid_name_use == SID_NAME_UNKNOWN) {
332 expr = talloc_asprintf(tmp_ctx, "(&(objectClass=groupMap))");
333 } else {
334 expr = talloc_asprintf(tmp_ctx, "(&(sidNameUse=%u)(objectClass=groupMap))",
335 sid_name_use);
336 }
337 if (expr == NULL) goto failed;
338
339 /* we do a subtree search on the domain */
340 if (domsid != NULL) {
341 sid_to_fstring(name, domsid);
342 basedn = ldb_dn_string_compose(tmp_ctx, NULL, "domain=%s", name);
343 if (basedn == NULL) goto failed;
344 }
345
346 ret = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
347 talloc_steal(tmp_ctx, res);
348 if (ret != LDB_SUCCESS) goto failed;
349
350 (*pp_rmap) = NULL;
351 *p_num_entries = 0;
352
353 for (i=0;i<res->count;i++) {
354 (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP,
355 (*p_num_entries)+1);
356 if (!(*pp_rmap)) goto failed;
357
358 if (!msg_to_group_map(res->msgs[i], &(*pp_rmap)[*p_num_entries])) {
359 goto failed;
360 }
361
362 (*p_num_entries)++;
363 }
364
365 talloc_free(tmp_ctx);
366 return True;
367
368failed:
369 talloc_free(tmp_ctx);
370 return False;
371}
372
373/*
374 This operation happens on session setup, so it should better be fast. We
375 store a list of aliases a SID is member of hanging off MEMBEROF/SID.
376*/
377static NTSTATUS one_alias_membership(const DOM_SID *member,
378 DOM_SID **sids, size_t *num)
379{
380 const char *attrs[] = {
381 "sid",
382 NULL
383 };
384 DOM_SID alias;
385 char *expr;
386 int ret, i;
387 struct ldb_result *res=NULL;
388 fstring string_sid;
389 NTSTATUS status = NT_STATUS_INTERNAL_DB_CORRUPTION;
390
391 if (!sid_to_fstring(string_sid, member)) {
392 return NT_STATUS_INVALID_PARAMETER;
393 }
394
395 expr = talloc_asprintf(ldb, "(&(member=%s)(objectClass=groupMap))",
396 string_sid);
397 if (expr == NULL) goto failed;
398
399 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, attrs, &res);
400 talloc_steal(expr, res);
401 if (ret != LDB_SUCCESS) {
402 goto failed;
403 }
404
405 for (i=0;i<res->count;i++) {
406 struct ldb_message_element *el;
407 el = ldb_msg_find_element(res->msgs[i], "sid");
408 if (el == NULL || el->num_values != 1) {
409 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
410 goto failed;
411 }
412 string_to_sid(&alias, (char *)el->values[0].data);
413 status = add_sid_to_array_unique(NULL, &alias, sids, num);
414 if (!NT_STATUS_IS_OK(status)) {
415 goto failed;
416 }
417 }
418
419 talloc_free(expr);
420 return NT_STATUS_OK;
421
422failed:
423 talloc_free(expr);
424 return status;
425}
426
427/*
428 add/remove a member field
429*/
430static NTSTATUS modify_aliasmem(const DOM_SID *alias, const DOM_SID *member,
431 int operation)
432{
433 fstring string_sid;
434 int ret;
435 struct ldb_message msg;
436 struct ldb_message_element el;
437 struct ldb_val val;
438 TALLOC_CTX *tmp_ctx;
439 GROUP_MAP map;
440
441 if (!get_group_map_from_sid(*alias, &map)) {
442 sid_to_fstring(string_sid, alias);
443 return NT_STATUS_NO_SUCH_ALIAS;
444 }
445
446 if ((map.sid_name_use != SID_NAME_ALIAS) &&
447 (map.sid_name_use != SID_NAME_WKN_GRP)) {
448 DEBUG(0,("sid_name_use=%d\n", map.sid_name_use));
449 return NT_STATUS_NO_SUCH_ALIAS;
450 }
451
452 tmp_ctx = talloc_new(NULL);
453 if (tmp_ctx == NULL) {
454 return NT_STATUS_NO_MEMORY;
455 }
456
457 msg.dn = mapping_dn(tmp_ctx, alias);
458 if (msg.dn == NULL) {
459 return NT_STATUS_NO_MEMORY;
460 }
461 msg.num_elements = 1;
462 msg.elements = &el;
463 el.flags = operation;
464 el.name = talloc_strdup(tmp_ctx, "member");
465 el.num_values = 1;
466 el.values = &val;
467 sid_to_fstring(string_sid, member);
468 val.data = (uint8_t *)string_sid;
469 val.length = strlen(string_sid);
470
471 ret = ldb_modify(ldb, &msg);
472 talloc_free(tmp_ctx);
473
474 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
475 return NT_STATUS_NO_SUCH_ALIAS;
476 }
477
478 if (operation == LDB_FLAG_MOD_ADD &&
479 ret == LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
480 return NT_STATUS_MEMBER_IN_ALIAS;
481 }
482
483 return (ret == LDB_SUCCESS ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
484}
485
486static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
487{
488 return modify_aliasmem(alias, member, LDB_FLAG_MOD_ADD);
489}
490
491static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
492{
493 return modify_aliasmem(alias, member, LDB_FLAG_MOD_DELETE);
494}
495
496
497/*
498 enumerate sids that have the given alias set in member
499*/
500static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
501{
502 const char *attrs[] = {
503 "member",
504 NULL
505 };
506 int ret, i;
507 NTSTATUS status = NT_STATUS_OK;
508 struct ldb_result *res=NULL;
509 struct ldb_dn *dn;
510 struct ldb_message_element *el;
511
512 *sids = NULL;
513 *num = 0;
514
515 dn = mapping_dn(ldb, alias);
516 if (dn == NULL) {
517 return NT_STATUS_NO_MEMORY;
518 }
519
520 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, NULL, attrs, &res);
521 talloc_steal(dn, res);
522 if (ret == LDB_SUCCESS && res->count == 0) {
523 talloc_free(dn);
524 return NT_STATUS_OK;
525 }
526 if (ret != LDB_SUCCESS) {
527 talloc_free(dn);
528 return NT_STATUS_INTERNAL_DB_CORRUPTION;
529 }
530
531 el = ldb_msg_find_element(res->msgs[0], "member");
532 if (el == NULL) {
533 talloc_free(dn);
534 return NT_STATUS_OK;
535 }
536
537 for (i=0;i<el->num_values;i++) {
538 DOM_SID sid;
539 string_to_sid(&sid, (const char *)el->values[i].data);
540 status = add_sid_to_array_unique(NULL, &sid, sids, num);
541 if (!NT_STATUS_IS_OK(status)) {
542 goto done;
543 }
544 }
545
546done:
547 talloc_free(dn);
548 return status;
549}
550
551/*
552 upgrade one group mapping record from the old tdb format
553*/
554static int upgrade_map_record(TDB_CONTEXT *tdb_ctx, TDB_DATA key,
555 TDB_DATA data, void *state)
556{
557 int ret;
558 GROUP_MAP map;
559
560 if (strncmp((char *)key.dptr, GROUP_PREFIX,
561 MIN(key.dsize, strlen(GROUP_PREFIX))) != 0) {
562 return 0;
563 }
564
565 if (!string_to_sid(&map.sid, strlen(GROUP_PREFIX) + (const char *)key.dptr)) {
566 DEBUG(0,("Bad sid key '%s' during upgrade\n", (const char *)key.dptr));
567 *(int *)state = -1;
568 return -1;
569 }
570
571 ret = tdb_unpack(data.dptr, data.dsize, "ddff",
572 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
573 if (ret == -1) {
574 DEBUG(0,("Failed to unpack group map record during upgrade\n"));
575 *(int *)state = -1;
576 return -1;
577 }
578
579 if ((int)map.gid == -1) {
580 /*
581 * Ignore old invalid mappings
582 */
583 return 0;
584 }
585
586 if (!add_mapping_entry(&map, 0)) {
587 DEBUG(0,("Failed to add mapping entry during upgrade\n"));
588 *(int *)state = -1;
589 return -1;
590 }
591
592 return 0;
593}
594
595/*
596 upgrade one alias record from the old tdb format
597*/
598static int upgrade_alias_record(TDB_CONTEXT *tdb_ctx, TDB_DATA key,
599 TDB_DATA data, void *state)
600{
601 const char *p = (const char *)data.dptr;
602 char *string_sid;
603 DOM_SID member;
604 TALLOC_CTX *frame;
605
606 if (strncmp((char *)key.dptr, MEMBEROF_PREFIX,
607 MIN(key.dsize, strlen(MEMBEROF_PREFIX))) != 0) {
608 return 0;
609 }
610
611 if (!string_to_sid(&member, strlen(MEMBEROF_PREFIX) + (const char *)key.dptr)) {
612 DEBUG(0,("Bad alias key %s during upgrade\n",
613 (const char *)key.dptr));
614 *(int *)state = -1;
615 }
616
617 frame = talloc_stackframe();
618 while (next_token_talloc(frame,&p, &string_sid, " ")) {
619 DOM_SID alias;
620 NTSTATUS status;
621 string_to_sid(&alias, string_sid);
622 status = add_aliasmem(&alias, &member);
623 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_ALIAS)) {
624 DEBUG(0,("Ignoring orphaned alias record '%s'\n",
625 string_sid));
626 } else if (!NT_STATUS_IS_OK(status)) {
627 DEBUG(0,("Failed to add alias member during upgrade - %s\n",
628 nt_errstr(status)));
629 *(int *)state = -1;
630 TALLOC_FREE(frame);
631 return -1;
632 }
633 }
634 TALLOC_FREE(frame);
635 return 0;
636}
637
638/*
639 upgrade from a old style tdb
640*/
641static bool mapping_upgrade(const char *tdb_path)
642{
643 static TDB_CONTEXT *tdb;
644 int ret, status=0;
645
646 tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDWR, 0600);
647 if (tdb == NULL) goto failed;
648
649 /* we have to do the map records first, as alias records may
650 reference them */
651 ret = tdb_traverse(tdb, upgrade_map_record, &status);
652 if (ret == -1 || status == -1) goto failed;
653
654 ret = tdb_traverse(tdb, upgrade_alias_record, &status);
655 if (ret == -1 || status == -1) goto failed;
656
657 if (tdb) {
658 tdb_close(tdb);
659 tdb = NULL;
660 }
661
662 {
663 const char *old_path = tdb_path;
664 char *new_path = state_path("group_mapping.tdb.upgraded");
665
666 if (!new_path) {
667 goto failed;
668 }
669 if (rename(old_path, new_path) != 0) {
670 DEBUG(0,("Failed to rename old group mapping database\n"));
671 goto failed;
672 }
673 }
674 return True;
675
676failed:
677 DEBUG(0,("Failed to upgrade group mapping database\n"));
678 if (tdb) tdb_close(tdb);
679 return False;
680}
681
682
683
684static const struct mapping_backend ldb_backend = {
685 .add_mapping_entry = add_mapping_entry,
686 .get_group_map_from_sid = get_group_map_from_sid,
687 .get_group_map_from_gid = get_group_map_from_gid,
688 .get_group_map_from_ntname = get_group_map_from_ntname,
689 .group_map_remove = group_map_remove,
690 .enum_group_mapping = enum_group_mapping,
691 .one_alias_membership = one_alias_membership,
692 .add_aliasmem = add_aliasmem,
693 .del_aliasmem = del_aliasmem,
694 .enum_aliasmem = enum_aliasmem
695};
696
697/*
698 initialise the ldb mapping backend
699 */
700const struct mapping_backend *groupdb_ldb_init(void)
701{
702 if (!init_group_mapping()) {
703 DEBUG(0,("Failed to initialise ldb mapping backend\n"));
704 return NULL;
705 }
706
707 return &ldb_backend;
708}
Note: See TracBrowser for help on using the repository browser.