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 "groupdb/mapping.h"
27 static struct db_context *db; /* used for driver files */
29 static bool enum_group_mapping(const struct dom_sid *domsid,
30 enum lsa_SidType sid_name_use,
32 size_t *p_num_entries,
34 static bool group_map_remove(const struct dom_sid *sid);
36 static bool mapping_switch(const char *ldb_path);
38 /****************************************************************************
39 Open the group mapping tdb.
40 ****************************************************************************/
41 static bool init_group_mapping(void)
49 db = db_open(NULL, state_path("group_mapping.tdb"), 0,
50 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
52 DEBUG(0, ("Failed to open group mapping database: %s\n",
57 ldb_path = state_path("group_mapping.ldb");
58 if (file_exist(ldb_path) && !mapping_switch(ldb_path)) {
59 unlink(state_path("group_mapping.tdb"));
63 /* handle upgrade from old versions of the database */
64 #if 0 /* -- Needs conversion to dbwrap -- */
65 const char *vstring = "INFO/version";
67 GROUP_MAP *map_table = NULL;
68 size_t num_entries = 0;
70 /* handle a Samba upgrade */
71 tdb_lock_bystring(tdb, vstring);
73 /* Cope with byte-reversed older versions of the db. */
74 vers_id = tdb_fetch_int32(tdb, vstring);
75 if ((vers_id == DATABASE_VERSION_V1)
76 || (IREV(vers_id) == DATABASE_VERSION_V1)) {
78 * Written on a bigendian machine with old fetch_int
81 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
82 vers_id = DATABASE_VERSION_V2;
85 /* if its an unknown version we remove everthing in the db */
87 if (vers_id != DATABASE_VERSION_V2) {
89 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
92 tdb_unlock_bystring(tdb, vstring);
94 /* cleanup any map entries with a gid == -1 */
96 if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table,
97 &num_entries, False ) ) {
100 for ( i=0; i<num_entries; i++ ) {
101 if ( map_table[i].gid == -1 ) {
102 group_map_remove( &map_table[i].sid );
106 SAFE_FREE( map_table );
113 static char *group_mapping_key(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
115 char *sidstr, *result;
117 sidstr = sid_string_talloc(talloc_tos(), sid);
118 if (sidstr == NULL) {
122 result = talloc_asprintf(mem_ctx, "%s%s", GROUP_PREFIX, sidstr);
128 /****************************************************************************
129 ****************************************************************************/
130 static bool add_mapping_entry(GROUP_MAP *map, int flag)
136 key = group_mapping_key(talloc_tos(), &map->sid);
141 len = tdb_pack(NULL, 0, "ddff",
142 map->gid, map->sid_name_use, map->nt_name, map->comment);
144 buf = TALLOC_ARRAY(key, char, len);
149 len = tdb_pack((uint8 *)buf, len, "ddff", map->gid,
150 map->sid_name_use, map->nt_name, map->comment);
152 status = dbwrap_trans_store(
153 db, string_term_tdb_data(key),
154 make_tdb_data((uint8_t *)buf, len), TDB_REPLACE);
158 return NT_STATUS_IS_OK(status);
162 /****************************************************************************
163 Return the sid and the type of the unix group.
164 ****************************************************************************/
166 static bool get_group_map_from_sid(struct dom_sid sid, GROUP_MAP *map)
172 /* the key is the SID, retrieving is direct */
174 key = group_mapping_key(talloc_tos(), &sid);
179 dbuf = dbwrap_fetch_bystring(db, key, key);
180 if (dbuf.dptr == NULL) {
185 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
186 &map->gid, &map->sid_name_use,
187 &map->nt_name, &map->comment);
192 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
196 sid_copy(&map->sid, &sid);
201 static bool dbrec2map(const struct db_record *rec, GROUP_MAP *map)
203 if ((rec->key.dsize < strlen(GROUP_PREFIX))
204 || (strncmp((char *)rec->key.dptr, GROUP_PREFIX,
205 GROUP_PREFIX_LEN) != 0)) {
209 if (!string_to_sid(&map->sid, (const char *)rec->key.dptr
210 + GROUP_PREFIX_LEN)) {
214 return tdb_unpack(rec->value.dptr, rec->value.dsize, "ddff",
215 &map->gid, &map->sid_name_use, &map->nt_name,
216 &map->comment) != -1;
219 struct find_map_state {
221 const char *name; /* If != NULL, look for name */
222 gid_t gid; /* valid iff name == NULL */
226 static int find_map(struct db_record *rec, void *private_data)
228 struct find_map_state *state = (struct find_map_state *)private_data;
230 if (!dbrec2map(rec, state->map)) {
231 DEBUG(10, ("failed to unpack map\n"));
235 if (state->name != NULL) {
236 if (strequal(state->name, state->map->nt_name)) {
242 if (state->map->gid == state->gid) {
251 /****************************************************************************
252 Return the sid and the type of the unix group.
253 ****************************************************************************/
255 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
257 struct find_map_state state;
260 state.name = NULL; /* Indicate we're looking for gid */
264 db->traverse_read(db, find_map, (void *)&state);
269 /****************************************************************************
270 Return the sid and the type of the unix group.
271 ****************************************************************************/
273 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
275 struct find_map_state state;
281 db->traverse_read(db, find_map, (void *)&state);
286 /****************************************************************************
287 Remove a group mapping entry.
288 ****************************************************************************/
290 static bool group_map_remove(const struct dom_sid *sid)
295 key = group_mapping_key(talloc_tos(), sid);
300 status = dbwrap_trans_delete(db, string_term_tdb_data(key));
303 return NT_STATUS_IS_OK(status);
306 /****************************************************************************
307 Enumerate the group mapping.
308 ****************************************************************************/
310 struct enum_map_state {
311 const struct dom_sid *domsid;
312 enum lsa_SidType sid_name_use;
319 static int collect_map(struct db_record *rec, void *private_data)
321 struct enum_map_state *state = (struct enum_map_state *)private_data;
325 if (!dbrec2map(rec, &map)) {
328 /* list only the type or everything if UNKNOWN */
329 if (state->sid_name_use != SID_NAME_UNKNOWN
330 && state->sid_name_use != map.sid_name_use) {
331 DEBUG(11,("enum_group_mapping: group %s is not of the "
332 "requested type\n", map.nt_name));
336 if ((state->unix_only == ENUM_ONLY_MAPPED) && (map.gid == -1)) {
337 DEBUG(11,("enum_group_mapping: group %s is non mapped\n",
342 if ((state->domsid != NULL) &&
343 (sid_compare_domain(state->domsid, &map.sid) != 0)) {
344 DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
345 sid_string_dbg(&map.sid)));
349 if (!(tmp = SMB_REALLOC_ARRAY(state->maps, GROUP_MAP,
350 state->num_maps+1))) {
351 DEBUG(0,("enum_group_mapping: Unable to enlarge group "
357 state->maps[state->num_maps] = map;
362 static bool enum_group_mapping(const struct dom_sid *domsid,
363 enum lsa_SidType sid_name_use,
365 size_t *p_num_entries, bool unix_only)
367 struct enum_map_state state;
369 state.domsid = domsid;
370 state.sid_name_use = sid_name_use;
371 state.unix_only = unix_only;
375 if (db->traverse_read(db, collect_map, (void *)&state) < 0) {
379 *pp_rmap = state.maps;
380 *p_num_entries = state.num_maps;
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. */
388 static NTSTATUS one_alias_membership(const struct dom_sid *member,
389 struct dom_sid **sids, size_t *num)
396 NTSTATUS status = NT_STATUS_OK;
397 TALLOC_CTX *frame = talloc_stackframe();
399 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX,
400 sid_to_fstring(tmp, member));
402 dbuf = dbwrap_fetch_bystring(db, frame, key);
403 if (dbuf.dptr == NULL) {
408 p = (const char *)dbuf.dptr;
410 while (next_token_talloc(frame, &p, &string_sid, " ")) {
411 struct dom_sid alias;
414 if (!string_to_sid(&alias, string_sid))
418 status= add_sid_to_array_unique(NULL, &alias, sids, &num_sids);
419 if (!NT_STATUS_IS_OK(status)) {
430 static NTSTATUS alias_memberships(const struct dom_sid *members, size_t num_members,
431 struct dom_sid **sids, size_t *num)
438 for (i=0; i<num_members; i++) {
439 NTSTATUS status = one_alias_membership(&members[i], sids, num);
440 if (!NT_STATUS_IS_OK(status))
446 static bool is_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
448 struct dom_sid *sids;
452 /* This feels the wrong way round, but the on-disk data structure
453 * dictates it this way. */
454 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
457 for (i=0; i<num; i++) {
458 if (sid_compare(alias, &sids[i]) == 0) {
468 static NTSTATUS add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
473 char *new_memberstring;
474 struct db_record *rec;
477 if (!get_group_map_from_sid(*alias, &map))
478 return NT_STATUS_NO_SUCH_ALIAS;
480 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
481 (map.sid_name_use != SID_NAME_WKN_GRP) )
482 return NT_STATUS_NO_SUCH_ALIAS;
484 if (is_aliasmem(alias, member))
485 return NT_STATUS_MEMBER_IN_ALIAS;
487 sid_to_fstring(string_sid, member);
489 key = talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX,
492 return NT_STATUS_NO_MEMORY;
495 if (db->transaction_start(db) != 0) {
496 DEBUG(0, ("transaction_start failed\n"));
497 return NT_STATUS_INTERNAL_DB_CORRUPTION;
500 rec = db->fetch_locked(db, key, string_term_tdb_data(key));
503 DEBUG(10, ("fetch_lock failed\n"));
505 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
509 sid_to_fstring(string_sid, alias);
511 if (rec->value.dptr != NULL) {
512 new_memberstring = talloc_asprintf(
513 key, "%s %s", (char *)(rec->value.dptr), string_sid);
515 new_memberstring = talloc_strdup(key, string_sid);
518 if (new_memberstring == NULL) {
520 status = NT_STATUS_NO_MEMORY;
524 status = rec->store(rec, string_term_tdb_data(new_memberstring), 0);
528 if (!NT_STATUS_IS_OK(status)) {
529 DEBUG(10, ("Could not store record: %s\n", nt_errstr(status)));
533 if (db->transaction_commit(db) != 0) {
534 DEBUG(0, ("transaction_commit failed\n"));
535 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
542 if (db->transaction_cancel(db) != 0) {
543 smb_panic("transaction_cancel failed");
549 struct aliasmem_state {
551 const struct dom_sid *alias;
552 struct dom_sid **sids;
556 static int collect_aliasmem(struct db_record *rec, void *priv)
558 struct aliasmem_state *state = (struct aliasmem_state *)priv;
563 if (strncmp((const char *)rec->key.dptr, MEMBEROF_PREFIX,
564 MEMBEROF_PREFIX_LEN) != 0)
567 p = (const char *)rec->value.dptr;
569 frame = talloc_stackframe();
571 while (next_token_talloc(frame, &p, &alias_string, " ")) {
572 struct dom_sid alias, member;
573 const char *member_string;
576 if (!string_to_sid(&alias, alias_string))
579 if (sid_compare(state->alias, &alias) != 0)
582 /* Ok, we found the alias we're looking for in the membership
583 * list currently scanned. The key represents the alias
584 * member. Add that. */
586 member_string = strchr((const char *)rec->key.dptr, '/');
588 /* Above we tested for MEMBEROF_PREFIX which includes the
591 SMB_ASSERT(member_string != NULL);
594 if (!string_to_sid(&member, member_string))
597 num_sids = *state->num;
598 if (!NT_STATUS_IS_OK(add_sid_to_array(state->mem_ctx, &member,
605 *state->num = num_sids;
612 static NTSTATUS enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
613 struct dom_sid **sids, size_t *num)
616 struct aliasmem_state state;
618 if (!get_group_map_from_sid(*alias, &map))
619 return NT_STATUS_NO_SUCH_ALIAS;
621 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
622 (map.sid_name_use != SID_NAME_WKN_GRP) )
623 return NT_STATUS_NO_SUCH_ALIAS;
631 state.mem_ctx = mem_ctx;
633 db->traverse_read(db, collect_aliasmem, &state);
637 static NTSTATUS del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
640 struct dom_sid *sids;
647 if (db->transaction_start(db) != 0) {
648 DEBUG(0, ("transaction_start failed\n"));
649 return NT_STATUS_INTERNAL_DB_CORRUPTION;
652 status = alias_memberships(member, 1, &sids, &num);
654 if (!NT_STATUS_IS_OK(status)) {
658 for (i=0; i<num; i++) {
659 if (sid_compare(&sids[i], alias) == 0) {
667 status = NT_STATUS_MEMBER_NOT_IN_ALIAS;
672 sids[i] = sids[num-1];
676 sid_to_fstring(sid_string, member);
678 key = talloc_asprintf(sids, "%s%s", MEMBEROF_PREFIX, sid_string);
681 status = NT_STATUS_NO_MEMORY;
686 status = dbwrap_delete_bystring(db, key);
690 member_string = talloc_strdup(sids, "");
691 if (member_string == NULL) {
693 status = NT_STATUS_NO_MEMORY;
697 for (i=0; i<num; i++) {
699 sid_to_fstring(sid_string, &sids[i]);
701 member_string = talloc_asprintf_append_buffer(
702 member_string, " %s", sid_string);
704 if (member_string == NULL) {
706 status = NT_STATUS_NO_MEMORY;
711 status = dbwrap_store_bystring(
712 db, key, string_term_tdb_data(member_string), 0);
716 if (!NT_STATUS_IS_OK(status)) {
717 DEBUG(10, ("dbwrap_store_bystring failed: %s\n",
722 if (db->transaction_commit(db) != 0) {
723 DEBUG(0, ("transaction_commit failed\n"));
724 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
731 if (db->transaction_cancel(db) != 0) {
732 smb_panic("transaction_cancel failed");
738 /* -- ldb->tdb switching code -------------------------------------------- */
740 /* change this if the data format ever changes */
741 #define LTDB_PACKING_FORMAT 0x26011967
743 /* old packing formats (not supported for now,
744 * it was never used for group mapping AFAIK) */
745 #define LTDB_PACKING_FORMAT_NODN 0x26011966
747 static unsigned int pull_uint32(uint8_t *p, int ofs)
750 return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
754 unpack a ldb message from a linear buffer in TDB_DATA
756 static int convert_ldb_record(TDB_CONTEXT *ltdb, TDB_DATA key,
757 TDB_DATA data, void *ptr)
759 TALLOC_CTX *tmp_ctx = talloc_tos();
764 unsigned int remaining;
770 uint32_t num_mem = 0;
771 struct dom_sid *members = NULL;
773 p = (uint8_t *)data.dptr;
774 if (data.dsize < 8) {
779 format = pull_uint32(p, 0);
780 num_el = pull_uint32(p, 4);
783 remaining = data.dsize - 8;
786 case LTDB_PACKING_FORMAT:
787 len = strnlen((char *)p, remaining);
788 if (len == remaining) {
794 /* ignore special LDB attributes */
798 if (strncmp((char *)p, "rid=", 4)) {
799 /* unknown entry, ignore */
800 DEBUG(3, ("Found unknown entry in group mapping "
801 "database named [%s]\n", (char *)p));
805 remaining -= len + 1;
809 case LTDB_PACKING_FORMAT_NODN:
816 /* bad entry, ignore */
820 if (num_el > remaining / 6) {
827 for (i = 0; i < num_el; i++) {
830 if (remaining < 10) {
834 len = strnlen((char *)p, remaining - 6);
835 if (len == remaining - 6) {
839 name = talloc_strndup(tmp_ctx, (char *)p, len);
844 remaining -= len + 1;
847 num_vals = pull_uint32(p, 0);
848 if (StrCaseCmp(name, "member") == 0) {
850 members = talloc_array(tmp_ctx, struct dom_sid, num_mem);
851 if (members == NULL) {
855 } else if (num_vals != 1) {
863 for (j = 0; j < num_vals; j++) {
864 len = pull_uint32(p, 0);
865 if (len > remaining-5) {
870 val = talloc_strndup(tmp_ctx, (char *)(p + 4), len);
876 remaining -= len+4+1;
879 /* we ignore unknown or uninteresting attributes
880 * (objectclass, etc.) */
881 if (StrCaseCmp(name, "gidNumber") == 0) {
882 map.gid = strtoul(val, &q, 10);
887 } else if (StrCaseCmp(name, "sid") == 0) {
888 if (!string_to_sid(&map.sid, val)) {
892 } else if (StrCaseCmp(name, "sidNameUse") == 0) {
893 map.sid_name_use = strtoul(val, &q, 10);
898 } else if (StrCaseCmp(name, "ntname") == 0) {
899 strlcpy(map.nt_name, val,
900 sizeof(map.nt_name) -1);
901 } else if (StrCaseCmp(name, "comment") == 0) {
902 strlcpy(map.comment, val,
903 sizeof(map.comment) -1);
904 } else if (StrCaseCmp(name, "member") == 0) {
905 if (!string_to_sid(&members[j], val)) {
917 if (!add_mapping_entry(&map, 0)) {
923 for (j = 0; j < num_mem; j++) {
925 status = add_aliasmem(&map.sid, &members[j]);
926 if (!NT_STATUS_IS_OK(status)) {
933 if (remaining != 0) {
934 DEBUG(0, ("Errror: %d bytes unread in ltdb_unpack_data\n",
944 static bool mapping_switch(const char *ldb_path)
951 frame = talloc_stackframe();
953 ltdb = tdb_open_log(ldb_path, 0, TDB_DEFAULT, O_RDONLY, 0600);
954 if (ltdb == NULL) goto failed;
956 /* ldb is just a very fancy tdb, read out raw data and perform
958 ret = tdb_traverse(ltdb, convert_ldb_record, NULL);
959 if (ret == -1) goto failed;
966 /* now rename the old db out of the way */
967 new_path = state_path("group_mapping.ldb.replaced");
971 if (rename(ldb_path, new_path) != 0) {
972 DEBUG(0,("Failed to rename old group mapping database\n"));
979 DEBUG(0, ("Failed to switch to tdb group mapping database\n"));
980 if (ltdb) tdb_close(ltdb);
985 static const struct mapping_backend tdb_backend = {
986 .add_mapping_entry = add_mapping_entry,
987 .get_group_map_from_sid = get_group_map_from_sid,
988 .get_group_map_from_gid = get_group_map_from_gid,
989 .get_group_map_from_ntname = get_group_map_from_ntname,
990 .group_map_remove = group_map_remove,
991 .enum_group_mapping = enum_group_mapping,
992 .one_alias_membership = one_alias_membership,
993 .add_aliasmem = add_aliasmem,
994 .del_aliasmem = del_aliasmem,
995 .enum_aliasmem = enum_aliasmem
999 initialise the tdb mapping backend
1001 const struct mapping_backend *groupdb_tdb_init(void)
1003 if (!init_group_mapping()) {
1004 DEBUG(0,("Failed to initialise tdb mapping backend\n"));
1008 return &tdb_backend;