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

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

Update 3.2 to 3.2.12

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