2 * Unix SMB/CIFS implementation.
4 * group mapping code on top of ldb
6 * Copyright (C) Andrew Tridgell 2006
8 * based on tdb group mapping code from groupdb/mapping.c
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.
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.
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/>.
25 #include "groupdb/mapping.h"
26 #include "lib/ldb/include/includes.h"
27 #include "lib/ldb/include/ldb_errors.h"
29 static struct ldb_context *ldb;
31 static bool mapping_upgrade(const char *tdb_path);
34 connect to the group mapping ldb
36 static bool init_group_mapping(void)
39 const char *init_ldif[] =
40 { "dn: @ATTRIBUTES\n" \
41 "ntName: CASE_INSENSITIVE\n" \
44 "@IDXATTR: gidNumber\n" \
45 "@IDXATTR: ntName\n" \
46 "@IDXATTR: member\n" };
47 const char *db_path, *tdb_path;
55 /* this is needed as Samba3 doesn't have this globally yet */
58 db_path = state_path("group_mapping.ldb");
61 if (ldb == NULL) goto failed;
63 existed = file_exist(db_path, NULL);
65 if (lp_parm_bool(-1, "groupmap", "nosync", False)) {
66 flags |= LDB_FLG_NOSYNC;
70 flags |= LDB_FLG_NOMMAP;
73 ret = ldb_connect(ldb, db_path, flags, NULL);
74 if (ret != LDB_SUCCESS) {
79 /* initialise the ldb with an index */
80 struct ldb_ldif *ldif;
82 for (i=0;i<ARRAY_SIZE(init_ldif);i++) {
83 ldif = ldb_ldif_read_string(ldb, &init_ldif[i]);
84 if (ldif == NULL) goto failed;
85 ret = ldb_add(ldb, ldif->msg);
87 if (ret == -1) goto failed;
91 /* possibly upgrade */
92 tdb_path = state_path("group_mapping.tdb");
93 if (file_exist(tdb_path, NULL) && !mapping_upgrade(tdb_path)) {
94 unlink(state_path("group_mapping.ldb"));
101 DEBUG(0,("Failed to open group mapping ldb '%s' - '%s'\n",
102 db_path, ldb?ldb_errstring(ldb):strerror(errno)));
110 form the DN for a mapping entry from a SID
112 static struct ldb_dn *mapping_dn(TALLOC_CTX *mem_ctx, const DOM_SID *sid)
118 sid_copy(&domsid, sid);
119 if (!sid_split_rid(&domsid, &rid)) {
122 if (!sid_to_string(string_sid, &domsid)) {
125 /* we split by domain and rid so we can do a subtree search
126 when we only want one domain */
127 return ldb_dn_string_compose(mem_ctx, NULL, "rid=%u,domain=%s",
132 add a group mapping entry
134 static bool add_mapping_entry(GROUP_MAP *map, int flag)
136 struct ldb_message *msg;
140 msg = ldb_msg_new(ldb);
145 msg->dn = mapping_dn(msg, &map->sid);
146 if (msg->dn == NULL) {
150 if (ldb_msg_add_string(msg, "objectClass", "groupMap") != LDB_SUCCESS ||
151 ldb_msg_add_string(msg, "sid",
152 sid_to_string(string_sid, &map->sid)) != LDB_SUCCESS ||
153 ldb_msg_add_fmt(msg, "gidNumber", "%u", (unsigned)map->gid) != LDB_SUCCESS ||
154 ldb_msg_add_fmt(msg, "sidNameUse", "%u", (unsigned)map->sid_name_use) != LDB_SUCCESS ||
155 ldb_msg_add_string(msg, "comment", map->comment) != LDB_SUCCESS ||
156 ldb_msg_add_string(msg, "ntName", map->nt_name) != LDB_SUCCESS) {
160 ret = ldb_add(ldb, msg);
162 /* if it exists we update it. This is a hangover from the semantics the
164 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
165 for (i=0;i<msg->num_elements;i++) {
166 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
168 ret = ldb_modify(ldb, msg);
173 return ret == LDB_SUCCESS;
181 unpack a ldb message into a GROUP_MAP structure
183 static bool msg_to_group_map(struct ldb_message *msg, GROUP_MAP *map)
187 map->gid = ldb_msg_find_attr_as_int(msg, "gidNumber", -1);
188 map->sid_name_use = ldb_msg_find_attr_as_int(msg, "sidNameUse", -1);
189 fstrcpy(map->nt_name, ldb_msg_find_attr_as_string(msg, "ntName", NULL));
190 fstrcpy(map->comment, ldb_msg_find_attr_as_string(msg, "comment", NULL));
191 sidstr = ldb_msg_find_attr_as_string(msg, "sid", NULL);
193 if (!string_to_sid(&map->sid, sidstr) ||
194 map->gid == (gid_t)-1 ||
195 map->sid_name_use == (enum lsa_SidType)-1) {
196 DEBUG(0,("Unable to unpack group mapping\n"));
204 return a group map entry for a given sid
206 static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
210 struct ldb_result *res=NULL;
212 dn = mapping_dn(ldb, &sid);
213 if (dn == NULL) goto failed;
215 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
216 talloc_steal(dn, res);
217 if (ret != LDB_SUCCESS || res->count != 1) {
221 if (!msg_to_group_map(res->msgs[0], map)) goto failed;
232 return a group map entry for a given gid
234 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
238 struct ldb_result *res=NULL;
240 expr = talloc_asprintf(ldb, "(&(gidNumber=%u)(objectClass=groupMap))",
242 if (expr == NULL) goto failed;
244 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
245 talloc_steal(expr, res);
246 if (ret != LDB_SUCCESS || res->count != 1) goto failed;
248 if (!msg_to_group_map(res->msgs[0], map)) goto failed;
259 Return the sid and the type of the unix group.
261 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
265 struct ldb_result *res=NULL;
267 expr = talloc_asprintf(ldb, "(&(ntName=%s)(objectClass=groupMap))", name);
268 if (expr == NULL) goto failed;
270 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
271 talloc_steal(expr, res);
272 if (ret != LDB_SUCCESS || res->count != 1) goto failed;
274 if (!msg_to_group_map(res->msgs[0], map)) goto failed;
285 Remove a group mapping entry.
287 static bool group_map_remove(const DOM_SID *sid)
292 dn = mapping_dn(ldb, sid);
296 ret = ldb_delete(ldb, dn);
299 return ret == LDB_SUCCESS;
304 Enumerate the group mappings for a domain
306 static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use,
308 size_t *p_num_entries, bool unix_only)
313 struct ldb_result *res;
314 struct ldb_dn *basedn=NULL;
317 tmp_ctx = talloc_new(ldb);
318 if (tmp_ctx == NULL) goto failed;
320 if (sid_name_use == SID_NAME_UNKNOWN) {
321 expr = talloc_asprintf(tmp_ctx, "(&(objectClass=groupMap))");
323 expr = talloc_asprintf(tmp_ctx, "(&(sidNameUse=%u)(objectClass=groupMap))",
326 if (expr == NULL) goto failed;
328 /* we do a subtree search on the domain */
329 if (domsid != NULL) {
330 sid_to_string(name, domsid);
331 basedn = ldb_dn_string_compose(tmp_ctx, NULL, "domain=%s", name);
332 if (basedn == NULL) goto failed;
335 ret = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
336 if (ret != LDB_SUCCESS) goto failed;
341 for (i=0;i<res->count;i++) {
342 (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP,
344 if (!(*pp_rmap)) goto failed;
346 if (!msg_to_group_map(res->msgs[i], &(*pp_rmap)[*p_num_entries])) {
353 talloc_free(tmp_ctx);
357 talloc_free(tmp_ctx);
362 This operation happens on session setup, so it should better be fast. We
363 store a list of aliases a SID is member of hanging off MEMBEROF/SID.
365 static NTSTATUS one_alias_membership(const DOM_SID *member,
366 DOM_SID **sids, size_t *num)
368 const char *attrs[] = {
375 struct ldb_result *res=NULL;
377 NTSTATUS status = NT_STATUS_INTERNAL_DB_CORRUPTION;
379 if (!sid_to_string(string_sid, member)) {
380 return NT_STATUS_INVALID_PARAMETER;
383 expr = talloc_asprintf(ldb, "(&(member=%s)(objectClass=groupMap))",
385 if (expr == NULL) goto failed;
387 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, attrs, &res);
388 talloc_steal(expr, res);
389 if (ret != LDB_SUCCESS) {
393 for (i=0;i<res->count;i++) {
394 struct ldb_message_element *el;
395 el = ldb_msg_find_element(res->msgs[i], "sid");
396 if (el == NULL || el->num_values != 1) {
397 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
400 string_to_sid(&alias, (char *)el->values[0].data);
401 if (!add_sid_to_array_unique(NULL, &alias, sids, num)) {
402 status = NT_STATUS_NO_MEMORY;
416 add/remove a member field
418 static NTSTATUS modify_aliasmem(const DOM_SID *alias, const DOM_SID *member,
423 struct ldb_message msg;
424 struct ldb_message_element el;
429 if (!get_group_map_from_sid(*alias, &map)) {
430 sid_to_string(string_sid, alias);
431 return NT_STATUS_NO_SUCH_ALIAS;
434 if ((map.sid_name_use != SID_NAME_ALIAS) &&
435 (map.sid_name_use != SID_NAME_WKN_GRP)) {
436 DEBUG(0,("sid_name_use=%d\n", map.sid_name_use));
437 return NT_STATUS_NO_SUCH_ALIAS;
440 tmp_ctx = talloc_new(NULL);
441 if (tmp_ctx == NULL) {
442 return NT_STATUS_NO_MEMORY;
445 msg.dn = mapping_dn(tmp_ctx, alias);
446 if (msg.dn == NULL) {
447 return NT_STATUS_NO_MEMORY;
449 msg.num_elements = 1;
451 el.flags = operation;
452 el.name = talloc_strdup(tmp_ctx, "member");
455 sid_to_string(string_sid, member);
456 val.data = (uint8_t *)string_sid;
457 val.length = strlen(string_sid);
459 ret = ldb_modify(ldb, &msg);
460 talloc_free(tmp_ctx);
462 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
463 return NT_STATUS_NO_SUCH_ALIAS;
466 if (operation == LDB_FLAG_MOD_ADD &&
467 ret == LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
468 return NT_STATUS_MEMBER_IN_ALIAS;
471 return (ret == LDB_SUCCESS ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
474 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
476 return modify_aliasmem(alias, member, LDB_FLAG_MOD_ADD);
479 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
481 return modify_aliasmem(alias, member, LDB_FLAG_MOD_DELETE);
486 enumerate sids that have the given alias set in member
488 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
490 const char *attrs[] = {
495 struct ldb_result *res=NULL;
497 struct ldb_message_element *el;
502 dn = mapping_dn(ldb, alias);
504 return NT_STATUS_NO_MEMORY;
507 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, NULL, attrs, &res);
508 talloc_steal(dn, res);
509 if (ret == LDB_SUCCESS && res->count == 0) {
513 if (ret != LDB_SUCCESS) {
515 return NT_STATUS_INTERNAL_DB_CORRUPTION;
518 el = ldb_msg_find_element(res->msgs[0], "member");
521 return NT_STATUS_INTERNAL_DB_CORRUPTION;
524 for (i=0;i<el->num_values;i++) {
526 string_to_sid(&sid, (const char *)el->values[i].data);
527 if (!add_sid_to_array_unique(NULL, &sid, sids, num)) {
529 return NT_STATUS_NO_MEMORY;
538 upgrade one group mapping record from the old tdb format
540 static int upgrade_map_record(TDB_CONTEXT *tdb_ctx, TDB_DATA key,
541 TDB_DATA data, void *state)
546 if (strncmp((char *)key.dptr, GROUP_PREFIX,
547 MIN(key.dsize, strlen(GROUP_PREFIX))) != 0) {
551 if (!string_to_sid(&map.sid, strlen(GROUP_PREFIX) + (const char *)key.dptr)) {
552 DEBUG(0,("Bad sid key '%s' during upgrade\n", (const char *)key.dptr));
557 ret = tdb_unpack(data.dptr, data.dsize, "ddff",
558 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
560 DEBUG(0,("Failed to unpack group map record during upgrade\n"));
565 if (!add_mapping_entry(&map, 0)) {
566 DEBUG(0,("Failed to add mapping entry during upgrade\n"));
575 upgrade one alias record from the old tdb format
577 static int upgrade_alias_record(TDB_CONTEXT *tdb_ctx, TDB_DATA key,
578 TDB_DATA data, void *state)
580 const char *p = (const char *)data.dptr;
584 if (strncmp((char *)key.dptr, MEMBEROF_PREFIX,
585 MIN(key.dsize, strlen(MEMBEROF_PREFIX))) != 0) {
589 if (!string_to_sid(&member, strlen(MEMBEROF_PREFIX) + (const char *)key.dptr)) {
590 DEBUG(0,("Bad alias key %s during upgrade\n",
591 (const char *)key.dptr));
595 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
598 string_to_sid(&alias, string_sid);
599 status = add_aliasmem(&alias, &member);
600 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_ALIAS)) {
601 DEBUG(0,("Ignoring orphaned alias record '%s'\n",
603 } else if (!NT_STATUS_IS_OK(status)) {
604 DEBUG(0,("Failed to add alias member during upgrade - %s\n",
615 upgrade from a old style tdb
617 static bool mapping_upgrade(const char *tdb_path)
619 static TDB_CONTEXT *tdb;
622 tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDWR, 0600);
623 if (tdb == NULL) goto failed;
625 /* we have to do the map records first, as alias records may
627 ret = tdb_traverse(tdb, upgrade_map_record, &status);
628 if (ret == -1 || status == -1) goto failed;
630 ret = tdb_traverse(tdb, upgrade_alias_record, &status);
631 if (ret == -1 || status == -1) goto failed;
639 const char *old_path = tdb_path;
640 char *new_path = state_path("group_mapping.tdb.upgraded");
645 if (rename(old_path, new_path) != 0) {
646 DEBUG(0,("Failed to rename old group mapping database\n"));
653 DEBUG(0,("Failed to upgrade group mapping database\n"));
654 if (tdb) tdb_close(tdb);
660 static const struct mapping_backend ldb_backend = {
661 .add_mapping_entry = add_mapping_entry,
662 .get_group_map_from_sid = get_group_map_from_sid,
663 .get_group_map_from_gid = get_group_map_from_gid,
664 .get_group_map_from_ntname = get_group_map_from_ntname,
665 .group_map_remove = group_map_remove,
666 .enum_group_mapping = enum_group_mapping,
667 .one_alias_membership = one_alias_membership,
668 .add_aliasmem = add_aliasmem,
669 .del_aliasmem = del_aliasmem,
670 .enum_aliasmem = enum_aliasmem
674 initialise the ldb mapping backend
676 const struct mapping_backend *groupdb_ldb_init(void)
678 if (!init_group_mapping()) {
679 DEBUG(0,("Failed to initialise ldb mapping backend\n"));