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"
25 #include "groupdb/mapping.h"
27 #include "../libcli/security/security.h"
29 static struct db_context *db; /* used for driver files */
31 static bool enum_group_mapping(const struct dom_sid *domsid,
32 enum lsa_SidType sid_name_use,
34 size_t *p_num_entries,
36 static bool group_map_remove(const struct dom_sid *sid);
38 static bool mapping_switch(const char *ldb_path);
40 /****************************************************************************
41 Open the group mapping tdb.
42 ****************************************************************************/
43 static bool init_group_mapping(void)
51 db = db_open(NULL, state_path("group_mapping.tdb"), 0,
52 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
54 DEBUG(0, ("Failed to open group mapping database: %s\n",
59 ldb_path = state_path("group_mapping.ldb");
60 if (file_exist(ldb_path) && !mapping_switch(ldb_path)) {
61 unlink(state_path("group_mapping.tdb"));
65 /* handle upgrade from old versions of the database */
66 #if 0 /* -- Needs conversion to dbwrap -- */
67 const char *vstring = "INFO/version";
69 GROUP_MAP *map_table = NULL;
70 size_t num_entries = 0;
72 /* handle a Samba upgrade */
73 tdb_lock_bystring(tdb, vstring);
75 /* Cope with byte-reversed older versions of the db. */
76 vers_id = tdb_fetch_int32(tdb, vstring);
77 if ((vers_id == DATABASE_VERSION_V1)
78 || (IREV(vers_id) == DATABASE_VERSION_V1)) {
80 * Written on a bigendian machine with old fetch_int
83 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
84 vers_id = DATABASE_VERSION_V2;
87 /* if its an unknown version we remove everthing in the db */
89 if (vers_id != DATABASE_VERSION_V2) {
91 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
94 tdb_unlock_bystring(tdb, vstring);
96 /* cleanup any map entries with a gid == -1 */
98 if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table,
99 &num_entries, False ) ) {
102 for ( i=0; i<num_entries; i++ ) {
103 if ( map_table[i].gid == -1 ) {
104 group_map_remove( &map_table[i].sid );
108 SAFE_FREE( map_table );
115 static char *group_mapping_key(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
117 char *sidstr, *result;
119 sidstr = sid_string_talloc(talloc_tos(), sid);
120 if (sidstr == NULL) {
124 result = talloc_asprintf(mem_ctx, "%s%s", GROUP_PREFIX, sidstr);
130 /****************************************************************************
131 ****************************************************************************/
132 static bool add_mapping_entry(GROUP_MAP *map, int flag)
138 key = group_mapping_key(talloc_tos(), &map->sid);
143 len = tdb_pack(NULL, 0, "ddff",
144 map->gid, map->sid_name_use, map->nt_name, map->comment);
146 buf = TALLOC_ARRAY(key, char, len);
151 len = tdb_pack((uint8 *)buf, len, "ddff", map->gid,
152 map->sid_name_use, map->nt_name, map->comment);
154 status = dbwrap_trans_store(
155 db, string_term_tdb_data(key),
156 make_tdb_data((uint8_t *)buf, len), TDB_REPLACE);
160 return NT_STATUS_IS_OK(status);
164 /****************************************************************************
165 Return the sid and the type of the unix group.
166 ****************************************************************************/
168 static bool get_group_map_from_sid(struct dom_sid sid, GROUP_MAP *map)
174 /* the key is the SID, retrieving is direct */
176 key = group_mapping_key(talloc_tos(), &sid);
181 dbuf = dbwrap_fetch_bystring(db, key, key);
182 if (dbuf.dptr == NULL) {
187 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
188 &map->gid, &map->sid_name_use,
189 &map->nt_name, &map->comment);
194 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
198 sid_copy(&map->sid, &sid);
203 static bool dbrec2map(const struct db_record *rec, GROUP_MAP *map)
205 if ((rec->key.dsize < strlen(GROUP_PREFIX))
206 || (strncmp((char *)rec->key.dptr, GROUP_PREFIX,
207 GROUP_PREFIX_LEN) != 0)) {
211 if (!string_to_sid(&map->sid, (const char *)rec->key.dptr
212 + GROUP_PREFIX_LEN)) {
216 return tdb_unpack(rec->value.dptr, rec->value.dsize, "ddff",
217 &map->gid, &map->sid_name_use, &map->nt_name,
218 &map->comment) != -1;
221 struct find_map_state {
223 const char *name; /* If != NULL, look for name */
224 gid_t gid; /* valid iff name == NULL */
228 static int find_map(struct db_record *rec, void *private_data)
230 struct find_map_state *state = (struct find_map_state *)private_data;
232 if (!dbrec2map(rec, state->map)) {
233 DEBUG(10, ("failed to unpack map\n"));
237 if (state->name != NULL) {
238 if (strequal(state->name, state->map->nt_name)) {
244 if (state->map->gid == state->gid) {
253 /****************************************************************************
254 Return the sid and the type of the unix group.
255 ****************************************************************************/
257 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
259 struct find_map_state state;
262 state.name = NULL; /* Indicate we're looking for gid */
266 db->traverse_read(db, find_map, (void *)&state);
271 /****************************************************************************
272 Return the sid and the type of the unix group.
273 ****************************************************************************/
275 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
277 struct find_map_state state;
283 db->traverse_read(db, find_map, (void *)&state);
288 /****************************************************************************
289 Remove a group mapping entry.
290 ****************************************************************************/
292 static bool group_map_remove(const struct dom_sid *sid)
297 key = group_mapping_key(talloc_tos(), sid);
302 status = dbwrap_trans_delete(db, string_term_tdb_data(key));
305 return NT_STATUS_IS_OK(status);
308 /****************************************************************************
309 Enumerate the group mapping.
310 ****************************************************************************/
312 struct enum_map_state {
313 const struct dom_sid *domsid;
314 enum lsa_SidType sid_name_use;
321 static int collect_map(struct db_record *rec, void *private_data)
323 struct enum_map_state *state = (struct enum_map_state *)private_data;
327 if (!dbrec2map(rec, &map)) {
330 /* list only the type or everything if UNKNOWN */
331 if (state->sid_name_use != SID_NAME_UNKNOWN
332 && state->sid_name_use != map.sid_name_use) {
333 DEBUG(11,("enum_group_mapping: group %s is not of the "
334 "requested type\n", map.nt_name));
338 if ((state->unix_only == ENUM_ONLY_MAPPED) && (map.gid == -1)) {
339 DEBUG(11,("enum_group_mapping: group %s is non mapped\n",
344 if ((state->domsid != NULL) &&
345 (dom_sid_compare_domain(state->domsid, &map.sid) != 0)) {
346 DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
347 sid_string_dbg(&map.sid)));
351 if (!(tmp = SMB_REALLOC_ARRAY(state->maps, GROUP_MAP,
352 state->num_maps+1))) {
353 DEBUG(0,("enum_group_mapping: Unable to enlarge group "
359 state->maps[state->num_maps] = map;
364 static bool enum_group_mapping(const struct dom_sid *domsid,
365 enum lsa_SidType sid_name_use,
367 size_t *p_num_entries, bool unix_only)
369 struct enum_map_state state;
371 state.domsid = domsid;
372 state.sid_name_use = sid_name_use;
373 state.unix_only = unix_only;
377 if (db->traverse_read(db, collect_map, (void *)&state) < 0) {
381 *pp_rmap = state.maps;
382 *p_num_entries = state.num_maps;
387 /* This operation happens on session setup, so it should better be fast. We
388 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
390 static NTSTATUS one_alias_membership(const struct dom_sid *member,
391 struct dom_sid **sids, size_t *num)
398 NTSTATUS status = NT_STATUS_OK;
399 TALLOC_CTX *frame = talloc_stackframe();
401 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX,
402 sid_to_fstring(tmp, member));
404 dbuf = dbwrap_fetch_bystring(db, frame, key);
405 if (dbuf.dptr == NULL) {
410 p = (const char *)dbuf.dptr;
412 while (next_token_talloc(frame, &p, &string_sid, " ")) {
413 struct dom_sid alias;
416 if (!string_to_sid(&alias, string_sid))
420 status= add_sid_to_array_unique(NULL, &alias, sids, &num_sids);
421 if (!NT_STATUS_IS_OK(status)) {
432 static NTSTATUS alias_memberships(const struct dom_sid *members, size_t num_members,
433 struct dom_sid **sids, size_t *num)
440 for (i=0; i<num_members; i++) {
441 NTSTATUS status = one_alias_membership(&members[i], sids, num);
442 if (!NT_STATUS_IS_OK(status))
448 static bool is_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
450 struct dom_sid *sids;
454 /* This feels the wrong way round, but the on-disk data structure
455 * dictates it this way. */
456 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
459 for (i=0; i<num; i++) {
460 if (dom_sid_compare(alias, &sids[i]) == 0) {
470 static NTSTATUS add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
475 char *new_memberstring;
476 struct db_record *rec;
479 if (!get_group_map_from_sid(*alias, &map))
480 return NT_STATUS_NO_SUCH_ALIAS;
482 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
483 (map.sid_name_use != SID_NAME_WKN_GRP) )
484 return NT_STATUS_NO_SUCH_ALIAS;
486 if (is_aliasmem(alias, member))
487 return NT_STATUS_MEMBER_IN_ALIAS;
489 sid_to_fstring(string_sid, member);
491 key = talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX,
494 return NT_STATUS_NO_MEMORY;
497 if (db->transaction_start(db) != 0) {
498 DEBUG(0, ("transaction_start failed\n"));
499 return NT_STATUS_INTERNAL_DB_CORRUPTION;
502 rec = db->fetch_locked(db, key, string_term_tdb_data(key));
505 DEBUG(10, ("fetch_lock failed\n"));
507 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
511 sid_to_fstring(string_sid, alias);
513 if (rec->value.dptr != NULL) {
514 new_memberstring = talloc_asprintf(
515 key, "%s %s", (char *)(rec->value.dptr), string_sid);
517 new_memberstring = talloc_strdup(key, string_sid);
520 if (new_memberstring == NULL) {
522 status = NT_STATUS_NO_MEMORY;
526 status = rec->store(rec, string_term_tdb_data(new_memberstring), 0);
530 if (!NT_STATUS_IS_OK(status)) {
531 DEBUG(10, ("Could not store record: %s\n", nt_errstr(status)));
535 if (db->transaction_commit(db) != 0) {
536 DEBUG(0, ("transaction_commit failed\n"));
537 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
544 if (db->transaction_cancel(db) != 0) {
545 smb_panic("transaction_cancel failed");
551 struct aliasmem_state {
553 const struct dom_sid *alias;
554 struct dom_sid **sids;
558 static int collect_aliasmem(struct db_record *rec, void *priv)
560 struct aliasmem_state *state = (struct aliasmem_state *)priv;
565 if (strncmp((const char *)rec->key.dptr, MEMBEROF_PREFIX,
566 MEMBEROF_PREFIX_LEN) != 0)
569 p = (const char *)rec->value.dptr;
571 frame = talloc_stackframe();
573 while (next_token_talloc(frame, &p, &alias_string, " ")) {
574 struct dom_sid alias, member;
575 const char *member_string;
578 if (!string_to_sid(&alias, alias_string))
581 if (dom_sid_compare(state->alias, &alias) != 0)
584 /* Ok, we found the alias we're looking for in the membership
585 * list currently scanned. The key represents the alias
586 * member. Add that. */
588 member_string = strchr((const char *)rec->key.dptr, '/');
590 /* Above we tested for MEMBEROF_PREFIX which includes the
593 SMB_ASSERT(member_string != NULL);
596 if (!string_to_sid(&member, member_string))
599 num_sids = *state->num;
600 if (!NT_STATUS_IS_OK(add_sid_to_array(state->mem_ctx, &member,
607 *state->num = num_sids;
614 static NTSTATUS enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
615 struct dom_sid **sids, size_t *num)
618 struct aliasmem_state state;
620 if (!get_group_map_from_sid(*alias, &map))
621 return NT_STATUS_NO_SUCH_ALIAS;
623 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
624 (map.sid_name_use != SID_NAME_WKN_GRP) )
625 return NT_STATUS_NO_SUCH_ALIAS;
633 state.mem_ctx = mem_ctx;
635 db->traverse_read(db, collect_aliasmem, &state);
639 static NTSTATUS del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
642 struct dom_sid *sids;
649 if (db->transaction_start(db) != 0) {
650 DEBUG(0, ("transaction_start failed\n"));
651 return NT_STATUS_INTERNAL_DB_CORRUPTION;
654 status = alias_memberships(member, 1, &sids, &num);
656 if (!NT_STATUS_IS_OK(status)) {
660 for (i=0; i<num; i++) {
661 if (dom_sid_compare(&sids[i], alias) == 0) {
669 status = NT_STATUS_MEMBER_NOT_IN_ALIAS;
674 sids[i] = sids[num-1];
678 sid_to_fstring(sid_string, member);
680 key = talloc_asprintf(sids, "%s%s", MEMBEROF_PREFIX, sid_string);
683 status = NT_STATUS_NO_MEMORY;
688 status = dbwrap_delete_bystring(db, key);
692 member_string = talloc_strdup(sids, "");
693 if (member_string == NULL) {
695 status = NT_STATUS_NO_MEMORY;
699 for (i=0; i<num; i++) {
701 sid_to_fstring(sid_string, &sids[i]);
703 member_string = talloc_asprintf_append_buffer(
704 member_string, " %s", sid_string);
706 if (member_string == NULL) {
708 status = NT_STATUS_NO_MEMORY;
713 status = dbwrap_store_bystring(
714 db, key, string_term_tdb_data(member_string), 0);
718 if (!NT_STATUS_IS_OK(status)) {
719 DEBUG(10, ("dbwrap_store_bystring failed: %s\n",
724 if (db->transaction_commit(db) != 0) {
725 DEBUG(0, ("transaction_commit failed\n"));
726 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
733 if (db->transaction_cancel(db) != 0) {
734 smb_panic("transaction_cancel failed");
740 /* -- ldb->tdb switching code -------------------------------------------- */
742 /* change this if the data format ever changes */
743 #define LTDB_PACKING_FORMAT 0x26011967
745 /* old packing formats (not supported for now,
746 * it was never used for group mapping AFAIK) */
747 #define LTDB_PACKING_FORMAT_NODN 0x26011966
749 static unsigned int pull_uint32(uint8_t *p, int ofs)
752 return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
756 unpack a ldb message from a linear buffer in TDB_DATA
758 static int convert_ldb_record(TDB_CONTEXT *ltdb, TDB_DATA key,
759 TDB_DATA data, void *ptr)
761 TALLOC_CTX *tmp_ctx = talloc_tos();
766 unsigned int remaining;
772 uint32_t num_mem = 0;
773 struct dom_sid *members = NULL;
775 p = (uint8_t *)data.dptr;
776 if (data.dsize < 8) {
781 format = pull_uint32(p, 0);
782 num_el = pull_uint32(p, 4);
785 remaining = data.dsize - 8;
788 case LTDB_PACKING_FORMAT:
789 len = strnlen((char *)p, remaining);
790 if (len == remaining) {
796 /* ignore special LDB attributes */
800 if (strncmp((char *)p, "rid=", 4)) {
801 /* unknown entry, ignore */
802 DEBUG(3, ("Found unknown entry in group mapping "
803 "database named [%s]\n", (char *)p));
807 remaining -= len + 1;
811 case LTDB_PACKING_FORMAT_NODN:
818 /* bad entry, ignore */
822 if (num_el > remaining / 6) {
829 for (i = 0; i < num_el; i++) {
832 if (remaining < 10) {
836 len = strnlen((char *)p, remaining - 6);
837 if (len == remaining - 6) {
841 name = talloc_strndup(tmp_ctx, (char *)p, len);
846 remaining -= len + 1;
849 num_vals = pull_uint32(p, 0);
850 if (StrCaseCmp(name, "member") == 0) {
852 members = talloc_array(tmp_ctx, struct dom_sid, num_mem);
853 if (members == NULL) {
857 } else if (num_vals != 1) {
865 for (j = 0; j < num_vals; j++) {
866 len = pull_uint32(p, 0);
867 if (len > remaining-5) {
872 val = talloc_strndup(tmp_ctx, (char *)(p + 4), len);
878 remaining -= len+4+1;
881 /* we ignore unknown or uninteresting attributes
882 * (objectclass, etc.) */
883 if (StrCaseCmp(name, "gidNumber") == 0) {
884 map.gid = strtoul(val, &q, 10);
889 } else if (StrCaseCmp(name, "sid") == 0) {
890 if (!string_to_sid(&map.sid, val)) {
894 } else if (StrCaseCmp(name, "sidNameUse") == 0) {
895 map.sid_name_use = strtoul(val, &q, 10);
900 } else if (StrCaseCmp(name, "ntname") == 0) {
901 strlcpy(map.nt_name, val,
902 sizeof(map.nt_name) -1);
903 } else if (StrCaseCmp(name, "comment") == 0) {
904 strlcpy(map.comment, val,
905 sizeof(map.comment) -1);
906 } else if (StrCaseCmp(name, "member") == 0) {
907 if (!string_to_sid(&members[j], val)) {
919 if (!add_mapping_entry(&map, 0)) {
925 for (j = 0; j < num_mem; j++) {
927 status = add_aliasmem(&map.sid, &members[j]);
928 if (!NT_STATUS_IS_OK(status)) {
935 if (remaining != 0) {
936 DEBUG(0, ("Errror: %d bytes unread in ltdb_unpack_data\n",
946 static bool mapping_switch(const char *ldb_path)
953 frame = talloc_stackframe();
955 ltdb = tdb_open_log(ldb_path, 0, TDB_DEFAULT, O_RDONLY, 0600);
956 if (ltdb == NULL) goto failed;
958 /* ldb is just a very fancy tdb, read out raw data and perform
960 ret = tdb_traverse(ltdb, convert_ldb_record, NULL);
961 if (ret == -1) goto failed;
968 /* now rename the old db out of the way */
969 new_path = state_path("group_mapping.ldb.replaced");
973 if (rename(ldb_path, new_path) != 0) {
974 DEBUG(0,("Failed to rename old group mapping database\n"));
981 DEBUG(0, ("Failed to switch to tdb group mapping database\n"));
982 if (ltdb) tdb_close(ltdb);
987 static const struct mapping_backend tdb_backend = {
988 .add_mapping_entry = add_mapping_entry,
989 .get_group_map_from_sid = get_group_map_from_sid,
990 .get_group_map_from_gid = get_group_map_from_gid,
991 .get_group_map_from_ntname = get_group_map_from_ntname,
992 .group_map_remove = group_map_remove,
993 .enum_group_mapping = enum_group_mapping,
994 .one_alias_membership = one_alias_membership,
995 .add_aliasmem = add_aliasmem,
996 .del_aliasmem = del_aliasmem,
997 .enum_aliasmem = enum_aliasmem
1001 initialise the tdb mapping backend
1003 const struct mapping_backend *groupdb_tdb_init(void)
1005 if (!init_group_mapping()) {
1006 DEBUG(0,("Failed to initialise tdb mapping backend\n"));
1010 return &tdb_backend;