source: branches/samba-3.5.x/source3/groupdb/mapping_ldb.c

Last change on this file was 454, checked in by Silvan Scherrer, 15 years ago

Samba Server 3.5: merged changes from 3.3

File size: 15.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/ldb.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, 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);
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) && !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_new_fmt(mem_ctx, ldb, "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 bool result = false;
223
224 dn = mapping_dn(talloc_tos(), &sid);
225 if (dn == NULL) {
226 goto failed;
227 }
228
229 ret = ldb_search(ldb, dn, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
230 if (ret != LDB_SUCCESS || res->count != 1) {
231 goto failed;
232 }
233
234 if (!msg_to_group_map(res->msgs[0], map)) {
235 goto failed;
236 }
237
238 result = true;
239 failed:
240 talloc_free(dn);
241 return result;
242}
243
244/*
245 return a group map entry for a given gid
246*/
247static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
248{
249 int ret;
250 struct ldb_result *res=NULL;
251 bool result = false;
252
253 ret = ldb_search(ldb, talloc_tos(), &res, NULL, LDB_SCOPE_SUBTREE,
254 NULL, "(&(gidNumber=%u)(objectClass=groupMap))",
255 (unsigned)gid);
256 if (ret != LDB_SUCCESS || res->count != 1) {
257 goto failed;
258 }
259
260 if (!msg_to_group_map(res->msgs[0], map)) {
261 goto failed;
262 }
263
264 result = true;
265failed:
266 TALLOC_FREE(res);
267 return result;
268}
269
270/*
271 Return the sid and the type of the unix group.
272*/
273static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
274{
275 int ret;
276 struct ldb_result *res=NULL;
277 bool result = false;
278
279 ret = ldb_search(ldb, talloc_tos(), &res, NULL, LDB_SCOPE_SUBTREE,
280 NULL, "(&(ntName=%s)(objectClass=groupMap))", name);
281 if (ret != LDB_SUCCESS || res->count != 1) {
282 goto failed;
283 }
284
285 if (!msg_to_group_map(res->msgs[0], map)) {
286 goto failed;
287 }
288
289 result = true;
290 failed:
291 TALLOC_FREE(res);
292 return result;
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 fstring name;
323 struct ldb_result *res = NULL;
324 struct ldb_dn *basedn=NULL;
325 TALLOC_CTX *tmp_ctx;
326
327 tmp_ctx = talloc_new(ldb);
328 if (tmp_ctx == NULL) goto failed;
329
330 /* we do a subtree search on the domain */
331 if (domsid != NULL) {
332 sid_to_fstring(name, domsid);
333 basedn = ldb_dn_new_fmt(tmp_ctx, ldb, "domain=%s", name);
334 if (basedn == NULL) goto failed;
335 }
336
337 if (sid_name_use == SID_NAME_UNKNOWN) {
338 ret = ldb_search(ldb, tmp_ctx, &res, basedn, LDB_SCOPE_SUBTREE,
339 NULL, "(&(objectClass=groupMap))");
340 } else {
341 ret = ldb_search(ldb, tmp_ctx, &res, basedn, LDB_SCOPE_SUBTREE,
342 NULL, "(&(sidNameUse=%u)(objectClass=groupMap))",
343 sid_name_use);
344 }
345
346 if (ret != LDB_SUCCESS) goto failed;
347
348 (*pp_rmap) = NULL;
349 *p_num_entries = 0;
350
351 for (i=0;i<res->count;i++) {
352 (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP,
353 (*p_num_entries)+1);
354 if (!(*pp_rmap)) goto failed;
355
356 if (!msg_to_group_map(res->msgs[i], &(*pp_rmap)[*p_num_entries])) {
357 goto failed;
358 }
359
360 (*p_num_entries)++;
361 }
362
363 talloc_free(tmp_ctx);
364 return True;
365
366failed:
367 talloc_free(tmp_ctx);
368 return False;
369}
370
371/*
372 This operation happens on session setup, so it should better be fast. We
373 store a list of aliases a SID is member of hanging off MEMBEROF/SID.
374*/
375static NTSTATUS one_alias_membership(const DOM_SID *member,
376 DOM_SID **sids, size_t *num)
377{
378 const char *attrs[] = {
379 "sid",
380 NULL
381 };
382 DOM_SID alias;
383 int ret, i;
384 struct ldb_result *res=NULL;
385 fstring string_sid;
386 NTSTATUS status;
387
388 if (!sid_to_fstring(string_sid, member)) {
389 return NT_STATUS_INVALID_PARAMETER;
390 }
391
392 ret = ldb_search(ldb, talloc_tos(), &res, NULL, LDB_SCOPE_SUBTREE,
393 attrs, "(&(member=%s)(objectClass=groupMap))",
394 string_sid);
395 if (ret != LDB_SUCCESS) {
396 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
397 goto failed;
398 }
399
400 for (i=0;i<res->count;i++) {
401 struct ldb_message_element *el;
402 el = ldb_msg_find_element(res->msgs[i], "sid");
403 if (el == NULL || el->num_values != 1) {
404 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
405 goto failed;
406 }
407 string_to_sid(&alias, (char *)el->values[0].data);
408 status = add_sid_to_array_unique(NULL, &alias, sids, num);
409 if (!NT_STATUS_IS_OK(status)) {
410 goto failed;
411 }
412 }
413
414 status = NT_STATUS_OK;
415 failed:
416 TALLOC_FREE(res);
417 return status;
418}
419
420/*
421 add/remove a member field
422*/
423static NTSTATUS modify_aliasmem(const DOM_SID *alias, const DOM_SID *member,
424 int operation)
425{
426 fstring string_sid;
427 int ret;
428 struct ldb_message msg;
429 struct ldb_message_element el;
430 struct ldb_val val;
431 TALLOC_CTX *tmp_ctx;
432 GROUP_MAP map;
433
434 if (!get_group_map_from_sid(*alias, &map)) {
435 sid_to_fstring(string_sid, alias);
436 return NT_STATUS_NO_SUCH_ALIAS;
437 }
438
439 if ((map.sid_name_use != SID_NAME_ALIAS) &&
440 (map.sid_name_use != SID_NAME_WKN_GRP)) {
441 DEBUG(0,("sid_name_use=%d\n", map.sid_name_use));
442 return NT_STATUS_NO_SUCH_ALIAS;
443 }
444
445 tmp_ctx = talloc_new(NULL);
446 if (tmp_ctx == NULL) {
447 return NT_STATUS_NO_MEMORY;
448 }
449
450 msg.dn = mapping_dn(tmp_ctx, alias);
451 if (msg.dn == NULL) {
452 return NT_STATUS_NO_MEMORY;
453 }
454 msg.num_elements = 1;
455 msg.elements = &el;
456 el.flags = operation;
457 el.name = talloc_strdup(tmp_ctx, "member");
458 el.num_values = 1;
459 el.values = &val;
460 sid_to_fstring(string_sid, member);
461 val.data = (uint8_t *)string_sid;
462 val.length = strlen(string_sid);
463
464 ret = ldb_modify(ldb, &msg);
465 talloc_free(tmp_ctx);
466
467 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
468 return NT_STATUS_NO_SUCH_ALIAS;
469 }
470
471 if (operation == LDB_FLAG_MOD_ADD &&
472 ret == LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
473 return NT_STATUS_MEMBER_IN_ALIAS;
474 }
475
476 return (ret == LDB_SUCCESS ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
477}
478
479static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
480{
481 return modify_aliasmem(alias, member, LDB_FLAG_MOD_ADD);
482}
483
484static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
485{
486 return modify_aliasmem(alias, member, LDB_FLAG_MOD_DELETE);
487}
488
489
490/*
491 enumerate sids that have the given alias set in member
492*/
493static NTSTATUS enum_aliasmem(const DOM_SID *alias, TALLOC_CTX *mem_ctx,
494 DOM_SID **sids, size_t *num)
495{
496 const char *attrs[] = {
497 "member",
498 NULL
499 };
500 int ret, i;
501 NTSTATUS status = NT_STATUS_OK;
502 struct ldb_result *res=NULL;
503 struct ldb_dn *dn;
504 struct ldb_message_element *el;
505
506 *sids = NULL;
507 *num = 0;
508
509 dn = mapping_dn(ldb, alias);
510 if (dn == NULL) {
511 return NT_STATUS_NO_MEMORY;
512 }
513
514 ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_BASE, attrs, NULL);
515 if (ret == LDB_SUCCESS && res->count == 0) {
516 talloc_free(res);
517 talloc_free(dn);
518 return NT_STATUS_OK;
519 }
520 if (ret != LDB_SUCCESS) {
521 talloc_free(dn);
522 return NT_STATUS_INTERNAL_DB_CORRUPTION;
523 }
524
525 talloc_steal(dn, res);
526 el = ldb_msg_find_element(res->msgs[0], "member");
527 if (el == NULL) {
528 talloc_free(dn);
529 return NT_STATUS_OK;
530 }
531
532 for (i=0;i<el->num_values;i++) {
533 DOM_SID sid;
534 string_to_sid(&sid, (const char *)el->values[i].data);
535 status = add_sid_to_array_unique(mem_ctx, &sid, sids, num);
536 if (!NT_STATUS_IS_OK(status)) {
537 goto done;
538 }
539 }
540
541done:
542 talloc_free(dn);
543 return status;
544}
545
546/*
547 upgrade one group mapping record from the old tdb format
548*/
549static int upgrade_map_record(TDB_CONTEXT *tdb_ctx, TDB_DATA key,
550 TDB_DATA data, void *state)
551{
552 int ret;
553 GROUP_MAP map;
554
555 if (strncmp((char *)key.dptr, GROUP_PREFIX,
556 MIN(key.dsize, strlen(GROUP_PREFIX))) != 0) {
557 return 0;
558 }
559
560 if (!string_to_sid(&map.sid, strlen(GROUP_PREFIX) + (const char *)key.dptr)) {
561 DEBUG(0,("Bad sid key '%s' during upgrade\n", (const char *)key.dptr));
562 *(int *)state = -1;
563 return -1;
564 }
565
566 ret = tdb_unpack(data.dptr, data.dsize, "ddff",
567 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
568 if (ret == -1) {
569 DEBUG(0,("Failed to unpack group map record during upgrade\n"));
570 *(int *)state = -1;
571 return -1;
572 }
573
574 if ((int)map.gid == -1) {
575 /*
576 * Ignore old invalid mappings
577 */
578 return 0;
579 }
580
581 if (!add_mapping_entry(&map, 0)) {
582 DEBUG(0,("Failed to add mapping entry during upgrade\n"));
583 *(int *)state = -1;
584 return -1;
585 }
586
587 return 0;
588}
589
590/*
591 upgrade one alias record from the old tdb format
592*/
593static int upgrade_alias_record(TDB_CONTEXT *tdb_ctx, TDB_DATA key,
594 TDB_DATA data, void *state)
595{
596 const char *p = (const char *)data.dptr;
597 char *string_sid;
598 DOM_SID member;
599 TALLOC_CTX *frame;
600
601 if (strncmp((char *)key.dptr, MEMBEROF_PREFIX,
602 MIN(key.dsize, strlen(MEMBEROF_PREFIX))) != 0) {
603 return 0;
604 }
605
606 if (!string_to_sid(&member, strlen(MEMBEROF_PREFIX) + (const char *)key.dptr)) {
607 DEBUG(0,("Bad alias key %s during upgrade\n",
608 (const char *)key.dptr));
609 *(int *)state = -1;
610 }
611
612 frame = talloc_stackframe();
613 while (next_token_talloc(frame,&p, &string_sid, " ")) {
614 DOM_SID alias;
615 NTSTATUS status;
616 string_to_sid(&alias, string_sid);
617 status = add_aliasmem(&alias, &member);
618 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_ALIAS)) {
619 DEBUG(0,("Ignoring orphaned alias record '%s'\n",
620 string_sid));
621 } else if (!NT_STATUS_IS_OK(status)) {
622 DEBUG(0,("Failed to add alias member during upgrade - %s\n",
623 nt_errstr(status)));
624 *(int *)state = -1;
625 TALLOC_FREE(frame);
626 return -1;
627 }
628 }
629 TALLOC_FREE(frame);
630 return 0;
631}
632
633/*
634 upgrade from a old style tdb
635*/
636static bool mapping_upgrade(const char *tdb_path)
637{
638 static TDB_CONTEXT *tdb;
639 int ret, status=0;
640
641 tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDWR, 0600);
642 if (tdb == NULL) goto failed;
643
644 /* we have to do the map records first, as alias records may
645 reference them */
646 ret = tdb_traverse(tdb, upgrade_map_record, &status);
647 if (ret == -1 || status == -1) goto failed;
648
649 ret = tdb_traverse(tdb, upgrade_alias_record, &status);
650 if (ret == -1 || status == -1) goto failed;
651
652 if (tdb) {
653 tdb_close(tdb);
654 tdb = NULL;
655 }
656
657 {
658 const char *old_path = tdb_path;
659 char *new_path = state_path("group_mapping.tdb.upgraded");
660
661 if (!new_path) {
662 goto failed;
663 }
664 if (rename(old_path, new_path) != 0) {
665 DEBUG(0,("Failed to rename old group mapping database\n"));
666 goto failed;
667 }
668 }
669 return True;
670
671failed:
672 DEBUG(0,("Failed to upgrade group mapping database\n"));
673 if (tdb) tdb_close(tdb);
674 return False;
675}
676
677
678
679static const struct mapping_backend ldb_backend = {
680 .add_mapping_entry = add_mapping_entry,
681 .get_group_map_from_sid = get_group_map_from_sid,
682 .get_group_map_from_gid = get_group_map_from_gid,
683 .get_group_map_from_ntname = get_group_map_from_ntname,
684 .group_map_remove = group_map_remove,
685 .enum_group_mapping = enum_group_mapping,
686 .one_alias_membership = one_alias_membership,
687 .add_aliasmem = add_aliasmem,
688 .del_aliasmem = del_aliasmem,
689 .enum_aliasmem = enum_aliasmem
690};
691
692/*
693 initialise the ldb mapping backend
694 */
695const struct mapping_backend *groupdb_ldb_init(void)
696{
697 if (!init_group_mapping()) {
698 DEBUG(0,("Failed to initialise ldb mapping backend\n"));
699 return NULL;
700 }
701
702 return &ldb_backend;
703}
Note: See TracBrowser for help on using the repository browser.