2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2006,
5 * Copyright (C) Jean François Micouleau 1998-2001.
6 * Copyright (C) Volker Lendecke 2006.
7 * Copyright (C) Gerald Carter 2006.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "system/filesys.h"
26 #include "groupdb/mapping.h"
27 #include "dbwrap/dbwrap.h"
28 #include "dbwrap/dbwrap_open.h"
30 #include "../libcli/security/security.h"
31 #include "groupdb/mapping_tdb.h"
33 static struct db_context *db; /* used for driver files */
35 static bool enum_group_mapping(const struct dom_sid *domsid,
36 enum lsa_SidType sid_name_use,
38 size_t *p_num_entries,
40 static bool group_map_remove(const struct dom_sid *sid);
42 static bool mapping_switch(const char *ldb_path);
44 /****************************************************************************
45 Open the group mapping tdb.
46 ****************************************************************************/
47 static bool init_group_mapping(void)
55 db = db_open(NULL, state_path("group_mapping.tdb"), 0,
56 TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
59 DEBUG(0, ("Failed to open group mapping database: %s\n",
64 ldb_path = state_path("group_mapping.ldb");
65 if (file_exist(ldb_path) && !mapping_switch(ldb_path)) {
66 unlink(state_path("group_mapping.tdb"));
70 /* handle upgrade from old versions of the database */
71 #if 0 /* -- Needs conversion to dbwrap -- */
72 const char *vstring = "INFO/version";
74 GROUP_MAP *map_table = NULL;
75 size_t num_entries = 0;
77 /* handle a Samba upgrade */
78 tdb_lock_bystring(tdb, vstring);
80 /* Cope with byte-reversed older versions of the db. */
81 vers_id = tdb_fetch_int32(tdb, vstring);
82 if ((vers_id == DATABASE_VERSION_V1)
83 || (IREV(vers_id) == DATABASE_VERSION_V1)) {
85 * Written on a bigendian machine with old fetch_int
88 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
89 vers_id = DATABASE_VERSION_V2;
92 /* if its an unknown version we remove everthing in the db */
94 if (vers_id != DATABASE_VERSION_V2) {
96 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
99 tdb_unlock_bystring(tdb, vstring);
101 /* cleanup any map entries with a gid == -1 */
103 if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table,
104 &num_entries, False ) ) {
107 for ( i=0; i<num_entries; i++ ) {
108 if ( map_table[i].gid == -1 ) {
109 group_map_remove( &map_table[i].sid );
113 SAFE_FREE( map_table );
120 static char *group_mapping_key(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
122 char *sidstr, *result;
124 sidstr = sid_string_talloc(talloc_tos(), sid);
125 if (sidstr == NULL) {
129 result = talloc_asprintf(mem_ctx, "%s%s", GROUP_PREFIX, sidstr);
135 /****************************************************************************
136 ****************************************************************************/
137 static bool add_mapping_entry(GROUP_MAP *map, int flag)
143 key = group_mapping_key(talloc_tos(), &map->sid);
148 len = tdb_pack(NULL, 0, "ddff",
149 map->gid, map->sid_name_use, map->nt_name, map->comment);
151 buf = talloc_array(key, char, len);
156 len = tdb_pack((uint8 *)buf, len, "ddff", map->gid,
157 map->sid_name_use, map->nt_name, map->comment);
159 status = dbwrap_trans_store(
160 db, string_term_tdb_data(key),
161 make_tdb_data((uint8_t *)buf, len), TDB_REPLACE);
165 return NT_STATUS_IS_OK(status);
169 /****************************************************************************
170 Return the sid and the type of the unix group.
171 ****************************************************************************/
173 static bool get_group_map_from_sid(struct dom_sid sid, GROUP_MAP *map)
182 /* the key is the SID, retrieving is direct */
184 key = group_mapping_key(talloc_tos(), &sid);
189 status = dbwrap_fetch_bystring(db, key, key, &dbuf);
190 if (!NT_STATUS_IS_OK(status)) {
195 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
196 &map->gid, &map->sid_name_use,
202 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
206 sid_copy(&map->sid, &sid);
208 map->nt_name = talloc_strdup(map, nt_name);
212 map->comment = talloc_strdup(map, comment);
220 static bool dbrec2map(const struct db_record *rec, GROUP_MAP *map)
222 TDB_DATA key = dbwrap_record_get_key(rec);
223 TDB_DATA value = dbwrap_record_get_value(rec);
228 if ((key.dsize < strlen(GROUP_PREFIX))
229 || (strncmp((char *)key.dptr, GROUP_PREFIX,
230 GROUP_PREFIX_LEN) != 0)) {
234 if (!string_to_sid(&map->sid, (const char *)key.dptr
235 + GROUP_PREFIX_LEN)) {
239 ret = tdb_unpack(value.dptr, value.dsize, "ddff",
240 &map->gid, &map->sid_name_use,
244 DEBUG(3, ("dbrec2map: tdb_unpack failure\n"));
248 map->nt_name = talloc_strdup(map, nt_name);
252 map->comment = talloc_strdup(map, comment);
260 struct find_map_state {
262 const char *name; /* If != NULL, look for name */
263 gid_t gid; /* valid iff name == NULL */
267 static int find_map(struct db_record *rec, void *private_data)
269 struct find_map_state *state = (struct find_map_state *)private_data;
271 if (!dbrec2map(rec, state->map)) {
272 DEBUG(10, ("failed to unpack map\n"));
276 if (state->name != NULL) {
277 if (strequal(state->name, state->map->nt_name)) {
283 if (state->map->gid == state->gid) {
292 /****************************************************************************
293 Return the sid and the type of the unix group.
294 ****************************************************************************/
296 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
298 struct find_map_state state;
301 state.name = NULL; /* Indicate we're looking for gid */
305 dbwrap_traverse_read(db, find_map, (void *)&state, NULL);
310 /****************************************************************************
311 Return the sid and the type of the unix group.
312 ****************************************************************************/
314 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
316 struct find_map_state state;
322 dbwrap_traverse_read(db, find_map, (void *)&state, NULL);
327 /****************************************************************************
328 Remove a group mapping entry.
329 ****************************************************************************/
331 static bool group_map_remove(const struct dom_sid *sid)
336 key = group_mapping_key(talloc_tos(), sid);
341 status = dbwrap_trans_delete(db, string_term_tdb_data(key));
344 return NT_STATUS_IS_OK(status);
347 /****************************************************************************
348 Enumerate the group mapping.
349 ****************************************************************************/
351 struct enum_map_state {
352 const struct dom_sid *domsid;
353 enum lsa_SidType sid_name_use;
360 static int collect_map(struct db_record *rec, void *private_data)
362 struct enum_map_state *state = (struct enum_map_state *)private_data;
366 map = talloc_zero(NULL, GROUP_MAP);
368 DEBUG(0, ("Unable to allocate group map!\n"));
372 if (!dbrec2map(rec, map)) {
376 /* list only the type or everything if UNKNOWN */
377 if (state->sid_name_use != SID_NAME_UNKNOWN
378 && state->sid_name_use != map->sid_name_use) {
379 DEBUG(11,("enum_group_mapping: group %s is not of the "
380 "requested type\n", map->nt_name));
385 if ((state->unix_only == ENUM_ONLY_MAPPED) && (map->gid == -1)) {
386 DEBUG(11,("enum_group_mapping: group %s is non mapped\n",
392 if ((state->domsid != NULL) &&
393 (dom_sid_compare_domain(state->domsid, &map->sid) != 0)) {
394 DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
395 sid_string_dbg(&map->sid)));
400 tmp = talloc_realloc(NULL, state->maps, GROUP_MAP *,
401 state->num_maps + 1);
403 DEBUG(0,("enum_group_mapping: Unable to enlarge group "
410 state->maps[state->num_maps] = talloc_move(state->maps, &map);
415 static bool enum_group_mapping(const struct dom_sid *domsid,
416 enum lsa_SidType sid_name_use,
417 GROUP_MAP ***pp_rmap,
418 size_t *p_num_entries, bool unix_only)
420 struct enum_map_state state;
423 state.domsid = domsid;
424 state.sid_name_use = sid_name_use;
425 state.unix_only = unix_only;
429 status = dbwrap_traverse_read(db, collect_map, (void *)&state, NULL);
430 if (!NT_STATUS_IS_OK(status)) {
431 TALLOC_FREE(state.maps);
435 *pp_rmap = state.maps;
436 *p_num_entries = state.num_maps;
441 /* This operation happens on session setup, so it should better be fast. We
442 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
444 static NTSTATUS one_alias_membership(const struct dom_sid *member,
445 struct dom_sid **sids, size_t *num)
452 NTSTATUS status = NT_STATUS_OK;
453 TALLOC_CTX *frame = talloc_stackframe();
455 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX,
456 sid_to_fstring(tmp, member));
458 status = dbwrap_fetch_bystring(db, frame, key, &dbuf);
459 if (!NT_STATUS_IS_OK(status)) {
464 p = (const char *)dbuf.dptr;
466 while (next_token_talloc(frame, &p, &string_sid, " ")) {
467 struct dom_sid alias;
470 if (!string_to_sid(&alias, string_sid))
474 status= add_sid_to_array_unique(NULL, &alias, sids, &num_sids);
475 if (!NT_STATUS_IS_OK(status)) {
486 static NTSTATUS alias_memberships(const struct dom_sid *members, size_t num_members,
487 struct dom_sid **sids, size_t *num)
494 for (i=0; i<num_members; i++) {
495 NTSTATUS status = one_alias_membership(&members[i], sids, num);
496 if (!NT_STATUS_IS_OK(status))
502 static bool is_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
504 struct dom_sid *sids;
508 /* This feels the wrong way round, but the on-disk data structure
509 * dictates it this way. */
510 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
513 for (i=0; i<num; i++) {
514 if (dom_sid_compare(alias, &sids[i]) == 0) {
524 static NTSTATUS add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
529 char *new_memberstring;
530 struct db_record *rec;
534 map = talloc_zero(talloc_tos(), GROUP_MAP);
536 return NT_STATUS_NO_MEMORY;
539 if (!get_group_map_from_sid(*alias, map)) {
541 return NT_STATUS_NO_SUCH_ALIAS;
544 if ((map->sid_name_use != SID_NAME_ALIAS) &&
545 (map->sid_name_use != SID_NAME_WKN_GRP)) {
547 return NT_STATUS_NO_SUCH_ALIAS;
552 if (is_aliasmem(alias, member))
553 return NT_STATUS_MEMBER_IN_ALIAS;
555 sid_to_fstring(string_sid, member);
557 key = talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX,
560 return NT_STATUS_NO_MEMORY;
563 if (dbwrap_transaction_start(db) != 0) {
564 DEBUG(0, ("transaction_start failed\n"));
565 return NT_STATUS_INTERNAL_DB_CORRUPTION;
568 rec = dbwrap_fetch_locked(db, key, string_term_tdb_data(key));
571 DEBUG(10, ("fetch_lock failed\n"));
573 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
577 value = dbwrap_record_get_value(rec);
579 sid_to_fstring(string_sid, alias);
581 if (value.dptr != NULL) {
582 new_memberstring = talloc_asprintf(
583 key, "%s %s", (char *)(value.dptr), string_sid);
585 new_memberstring = talloc_strdup(key, string_sid);
588 if (new_memberstring == NULL) {
590 status = NT_STATUS_NO_MEMORY;
594 status = dbwrap_record_store(rec, string_term_tdb_data(new_memberstring), 0);
598 if (!NT_STATUS_IS_OK(status)) {
599 DEBUG(10, ("Could not store record: %s\n", nt_errstr(status)));
603 if (dbwrap_transaction_commit(db) != 0) {
604 DEBUG(0, ("transaction_commit failed\n"));
605 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
612 if (dbwrap_transaction_cancel(db) != 0) {
613 smb_panic("transaction_cancel failed");
619 struct aliasmem_state {
621 const struct dom_sid *alias;
622 struct dom_sid **sids;
626 static int collect_aliasmem(struct db_record *rec, void *priv)
628 struct aliasmem_state *state = (struct aliasmem_state *)priv;
632 TDB_DATA key = dbwrap_record_get_key(rec);
633 TDB_DATA value = dbwrap_record_get_value(rec);
635 if (strncmp((const char *)key.dptr, MEMBEROF_PREFIX,
636 MEMBEROF_PREFIX_LEN) != 0)
639 p = (const char *)value.dptr;
641 frame = talloc_stackframe();
643 while (next_token_talloc(frame, &p, &alias_string, " ")) {
644 struct dom_sid alias, member;
645 const char *member_string;
648 if (!string_to_sid(&alias, alias_string))
651 if (dom_sid_compare(state->alias, &alias) != 0)
654 /* Ok, we found the alias we're looking for in the membership
655 * list currently scanned. The key represents the alias
656 * member. Add that. */
658 member_string = strchr((const char *)key.dptr, '/');
660 /* Above we tested for MEMBEROF_PREFIX which includes the
663 SMB_ASSERT(member_string != NULL);
666 if (!string_to_sid(&member, member_string))
669 num_sids = *state->num;
670 if (!NT_STATUS_IS_OK(add_sid_to_array(state->mem_ctx, &member,
677 *state->num = num_sids;
684 static NTSTATUS enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
685 struct dom_sid **sids, size_t *num)
688 struct aliasmem_state state;
690 map = talloc_zero(talloc_tos(), GROUP_MAP);
692 return NT_STATUS_NO_MEMORY;
695 if (!get_group_map_from_sid(*alias, map)) {
697 return NT_STATUS_NO_SUCH_ALIAS;
700 if ((map->sid_name_use != SID_NAME_ALIAS) &&
701 (map->sid_name_use != SID_NAME_WKN_GRP)) {
703 return NT_STATUS_NO_SUCH_ALIAS;
714 state.mem_ctx = mem_ctx;
716 dbwrap_traverse_read(db, collect_aliasmem, &state, NULL);
720 static NTSTATUS del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
723 struct dom_sid *sids;
730 if (dbwrap_transaction_start(db) != 0) {
731 DEBUG(0, ("transaction_start failed\n"));
732 return NT_STATUS_INTERNAL_DB_CORRUPTION;
735 status = alias_memberships(member, 1, &sids, &num);
737 if (!NT_STATUS_IS_OK(status)) {
741 for (i=0; i<num; i++) {
742 if (dom_sid_compare(&sids[i], alias) == 0) {
750 status = NT_STATUS_MEMBER_NOT_IN_ALIAS;
755 sids[i] = sids[num-1];
759 sid_to_fstring(sid_string, member);
761 key = talloc_asprintf(sids, "%s%s", MEMBEROF_PREFIX, sid_string);
764 status = NT_STATUS_NO_MEMORY;
769 status = dbwrap_delete_bystring(db, key);
773 member_string = talloc_strdup(sids, "");
774 if (member_string == NULL) {
776 status = NT_STATUS_NO_MEMORY;
780 for (i=0; i<num; i++) {
782 sid_to_fstring(sid_string, &sids[i]);
784 member_string = talloc_asprintf_append_buffer(
785 member_string, " %s", sid_string);
787 if (member_string == NULL) {
789 status = NT_STATUS_NO_MEMORY;
794 status = dbwrap_store_bystring(
795 db, key, string_term_tdb_data(member_string), 0);
799 if (!NT_STATUS_IS_OK(status)) {
800 DEBUG(10, ("dbwrap_store_bystring failed: %s\n",
805 if (dbwrap_transaction_commit(db) != 0) {
806 DEBUG(0, ("transaction_commit failed\n"));
807 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
814 if (dbwrap_transaction_cancel(db) != 0) {
815 smb_panic("transaction_cancel failed");
821 /* -- ldb->tdb switching code -------------------------------------------- */
823 /* change this if the data format ever changes */
824 #define LTDB_PACKING_FORMAT 0x26011967
826 /* old packing formats (not supported for now,
827 * it was never used for group mapping AFAIK) */
828 #define LTDB_PACKING_FORMAT_NODN 0x26011966
830 static unsigned int pull_uint32(uint8_t *p, int ofs)
833 return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
837 unpack a ldb message from a linear buffer in TDB_DATA
839 static int convert_ldb_record(TDB_CONTEXT *ltdb, TDB_DATA key,
840 TDB_DATA data, void *ptr)
842 TALLOC_CTX *tmp_ctx = talloc_tos();
843 GROUP_MAP *map = NULL;
847 unsigned int remaining;
853 uint32_t num_mem = 0;
854 struct dom_sid *members = NULL;
856 p = (uint8_t *)data.dptr;
857 if (data.dsize < 8) {
862 format = pull_uint32(p, 0);
863 num_el = pull_uint32(p, 4);
866 remaining = data.dsize - 8;
869 case LTDB_PACKING_FORMAT:
870 len = strnlen((char *)p, remaining);
871 if (len == remaining) {
877 /* ignore special LDB attributes */
881 if (strncmp((char *)p, "rid=", 4)) {
882 /* unknown entry, ignore */
883 DEBUG(3, ("Found unknown entry in group mapping "
884 "database named [%s]\n", (char *)p));
888 remaining -= len + 1;
892 case LTDB_PACKING_FORMAT_NODN:
899 /* bad entry, ignore */
903 if (num_el > remaining / 6) {
908 map = talloc_zero(NULL, GROUP_MAP);
914 for (i = 0; i < num_el; i++) {
917 if (remaining < 10) {
921 len = strnlen((char *)p, remaining - 6);
922 if (len == remaining - 6) {
926 name = talloc_strndup(tmp_ctx, (char *)p, len);
931 remaining -= len + 1;
934 num_vals = pull_uint32(p, 0);
935 if (strcasecmp_m(name, "member") == 0) {
937 members = talloc_array(tmp_ctx, struct dom_sid, num_mem);
938 if (members == NULL) {
942 } else if (num_vals != 1) {
950 for (j = 0; j < num_vals; j++) {
951 len = pull_uint32(p, 0);
952 if (len > remaining-5) {
957 val = talloc_strndup(tmp_ctx, (char *)(p + 4), len);
963 remaining -= len+4+1;
966 /* we ignore unknown or uninteresting attributes
967 * (objectclass, etc.) */
968 if (strcasecmp_m(name, "gidNumber") == 0) {
969 map->gid = strtoul(val, &q, 10);
974 } else if (strcasecmp_m(name, "sid") == 0) {
975 if (!string_to_sid(&map->sid, val)) {
979 } else if (strcasecmp_m(name, "sidNameUse") == 0) {
980 map->sid_name_use = strtoul(val, &q, 10);
985 } else if (strcasecmp_m(name, "ntname") == 0) {
986 map->nt_name = talloc_strdup(map, val);
991 } else if (strcasecmp_m(name, "comment") == 0) {
992 map->comment = talloc_strdup(map, val);
997 } else if (strcasecmp_m(name, "member") == 0) {
998 if (!string_to_sid(&members[j], val)) {
1010 if (map->nt_name == NULL) {
1015 if (map->comment == NULL) {
1016 map->comment = talloc_strdup(map, "");
1018 if (map->comment == NULL) {
1023 if (!add_mapping_entry(map, 0)) {
1029 for (j = 0; j < num_mem; j++) {
1031 status = add_aliasmem(&map->sid, &members[j]);
1032 if (!NT_STATUS_IS_OK(status)) {
1039 if (remaining != 0) {
1040 DEBUG(0, ("Errror: %d bytes unread in ltdb_unpack_data\n",
1052 static bool mapping_switch(const char *ldb_path)
1059 frame = talloc_stackframe();
1061 ltdb = tdb_open_log(ldb_path, 0, TDB_DEFAULT, O_RDONLY, 0600);
1062 if (ltdb == NULL) goto failed;
1064 /* ldb is just a very fancy tdb, read out raw data and perform
1066 ret = tdb_traverse(ltdb, convert_ldb_record, NULL);
1067 if (ret < 0) goto failed;
1074 /* now rename the old db out of the way */
1075 new_path = state_path("group_mapping.ldb.replaced");
1079 if (rename(ldb_path, new_path) != 0) {
1080 DEBUG(0,("Failed to rename old group mapping database\n"));
1087 DEBUG(0, ("Failed to switch to tdb group mapping database\n"));
1088 if (ltdb) tdb_close(ltdb);
1093 static const struct mapping_backend tdb_backend = {
1094 .add_mapping_entry = add_mapping_entry,
1095 .get_group_map_from_sid = get_group_map_from_sid,
1096 .get_group_map_from_gid = get_group_map_from_gid,
1097 .get_group_map_from_ntname = get_group_map_from_ntname,
1098 .group_map_remove = group_map_remove,
1099 .enum_group_mapping = enum_group_mapping,
1100 .one_alias_membership = one_alias_membership,
1101 .add_aliasmem = add_aliasmem,
1102 .del_aliasmem = del_aliasmem,
1103 .enum_aliasmem = enum_aliasmem
1107 initialise the tdb mapping backend
1109 const struct mapping_backend *groupdb_tdb_init(void)
1111 if (!init_group_mapping()) {
1112 DEBUG(0,("Failed to initialise tdb mapping backend\n"));
1116 return &tdb_backend;