auth: Fix a typo
[Samba.git] / source3 / groupdb / mapping_tdb.c
bloba39b03d9945f724962f4320446028273aa8f355c
1 /*
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.
8 *
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/>.
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "passdb.h"
26 #include "groupdb/mapping.h"
27 #include "dbwrap/dbwrap.h"
28 #include "dbwrap/dbwrap_open.h"
29 #include "util_tdb.h"
30 #include "../libcli/security/security.h"
31 #include "groupdb/mapping_tdb.h"
32 #include "lib/util/smb_strtox.h"
34 static struct db_context *db; /* used for driver files */
36 static bool enum_group_mapping(const struct dom_sid *domsid,
37 enum lsa_SidType sid_name_use,
38 GROUP_MAP ***pp_rmap,
39 size_t *p_num_entries,
40 bool unix_only);
41 static bool group_map_remove(const struct dom_sid *sid);
43 static bool mapping_switch(const char *ldb_path);
45 /****************************************************************************
46 Open the group mapping tdb.
47 ****************************************************************************/
48 static bool init_group_mapping(void)
50 char *tdb_path;
51 char *ldb_path;
53 if (db != NULL) {
54 return true;
57 tdb_path = state_path(talloc_tos(), "group_mapping.tdb");
58 if (tdb_path == NULL) {
59 return false;
61 db = db_open(NULL, tdb_path, 0,
62 TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
63 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
64 if (db == NULL) {
65 DEBUG(0, ("Failed to open group mapping database: %s\n",
66 strerror(errno)));
67 talloc_free(tdb_path);
68 return false;
71 ldb_path = state_path(talloc_tos(), "group_mapping.ldb");
72 if (ldb_path == NULL) {
73 talloc_free(tdb_path);
74 return false;
76 if (file_exist(ldb_path) && !mapping_switch(ldb_path)) {
77 unlink(tdb_path);
78 talloc_free(tdb_path);
79 talloc_free(ldb_path);
80 return false;
82 } else {
83 /* handle upgrade from old versions of the database */
84 #if 0 /* -- Needs conversion to dbwrap -- */
85 const char *vstring = "INFO/version";
86 int32 vers_id;
87 GROUP_MAP *map_table = NULL;
88 size_t num_entries = 0;
90 /* handle a Samba upgrade */
91 tdb_lock_bystring(tdb, vstring);
93 /* Cope with byte-reversed older versions of the db. */
94 vers_id = tdb_fetch_int32(tdb, vstring);
95 if ((vers_id == DATABASE_VERSION_V1)
96 || (IREV(vers_id) == DATABASE_VERSION_V1)) {
98 * Written on a bigendian machine with old fetch_int
99 * code. Save as le.
101 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
102 vers_id = DATABASE_VERSION_V2;
105 /* if its an unknown version we remove everything in the db */
107 if (vers_id != DATABASE_VERSION_V2) {
108 tdb_wipe_all(tdb);
109 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
112 tdb_unlock_bystring(tdb, vstring);
114 /* cleanup any map entries with a gid == -1 */
116 if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table,
117 &num_entries, False ) ) {
118 int i;
120 for ( i=0; i<num_entries; i++ ) {
121 if ( map_table[i].gid == -1 ) {
122 group_map_remove( &map_table[i].sid );
126 SAFE_FREE( map_table );
128 #endif
130 talloc_free(tdb_path);
131 talloc_free(ldb_path);
132 return true;
135 static char *group_mapping_key(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
137 struct dom_sid_buf sidstr;
139 return talloc_asprintf(
140 mem_ctx, "%s%s", GROUP_PREFIX, dom_sid_str_buf(sid, &sidstr));
143 /****************************************************************************
144 ****************************************************************************/
145 static bool add_mapping_entry(GROUP_MAP *map, int flag)
147 char *key, *buf;
148 int len;
149 NTSTATUS status;
151 key = group_mapping_key(talloc_tos(), &map->sid);
152 if (key == NULL) {
153 return false;
156 len = tdb_pack(NULL, 0, "ddff",
157 map->gid, map->sid_name_use, map->nt_name, map->comment);
159 buf = talloc_array(key, char, len);
160 if (!buf) {
161 TALLOC_FREE(key);
162 return false;
164 len = tdb_pack((uint8_t *)buf, len, "ddff", map->gid,
165 map->sid_name_use, map->nt_name, map->comment);
167 status = dbwrap_trans_store(
168 db, string_term_tdb_data(key),
169 make_tdb_data((uint8_t *)buf, len), TDB_REPLACE);
171 TALLOC_FREE(key);
173 return NT_STATUS_IS_OK(status);
177 /****************************************************************************
178 Return the sid and the type of the unix group.
179 ****************************************************************************/
181 static bool get_group_map_from_sid(struct dom_sid sid, GROUP_MAP *map)
183 TDB_DATA dbuf;
184 char *key;
185 int ret = 0;
186 NTSTATUS status;
187 fstring nt_name;
188 fstring comment;
190 /* the key is the SID, retrieving is direct */
192 key = group_mapping_key(talloc_tos(), &sid);
193 if (key == NULL) {
194 return false;
197 status = dbwrap_fetch_bystring(db, key, key, &dbuf);
198 if (!NT_STATUS_IS_OK(status)) {
199 TALLOC_FREE(key);
200 return false;
203 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
204 &map->gid, &map->sid_name_use,
205 &nt_name, &comment);
207 TALLOC_FREE(key);
209 if ( ret == -1 ) {
210 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
211 return false;
214 sid_copy(&map->sid, &sid);
216 map->nt_name = talloc_strdup(map, nt_name);
217 if (!map->nt_name) {
218 return false;
220 map->comment = talloc_strdup(map, comment);
221 if (!map->comment) {
222 return false;
225 return true;
228 static bool dbrec2map(const struct db_record *rec, GROUP_MAP *map)
230 TDB_DATA key = dbwrap_record_get_key(rec);
231 TDB_DATA value = dbwrap_record_get_value(rec);
232 int ret = 0;
233 fstring nt_name;
234 fstring comment;
236 if ((key.dsize < strlen(GROUP_PREFIX))
237 || (strncmp((char *)key.dptr, GROUP_PREFIX,
238 GROUP_PREFIX_LEN) != 0)) {
239 return False;
242 if (!string_to_sid(&map->sid, (const char *)key.dptr
243 + GROUP_PREFIX_LEN)) {
244 return False;
247 ret = tdb_unpack(value.dptr, value.dsize, "ddff",
248 &map->gid, &map->sid_name_use,
249 &nt_name, &comment);
251 if (ret == -1) {
252 DEBUG(3, ("dbrec2map: tdb_unpack failure\n"));
253 return false;
256 map->nt_name = talloc_strdup(map, nt_name);
257 if (!map->nt_name) {
258 return false;
260 map->comment = talloc_strdup(map, comment);
261 if (!map->comment) {
262 return false;
265 return true;
268 struct find_map_state {
269 bool found;
270 const char *name; /* If != NULL, look for name */
271 gid_t gid; /* valid iff name == NULL */
272 GROUP_MAP *map;
275 static int find_map(struct db_record *rec, void *private_data)
277 struct find_map_state *state = (struct find_map_state *)private_data;
279 if (!dbrec2map(rec, state->map)) {
280 DEBUG(10, ("failed to unpack map\n"));
281 return 0;
284 if (state->name != NULL) {
285 if (strequal(state->name, state->map->nt_name)) {
286 state->found = true;
287 return 1;
290 else {
291 if (state->map->gid == state->gid) {
292 state->found = true;
293 return 1;
297 return 0;
300 /****************************************************************************
301 Return the sid and the type of the unix group.
302 ****************************************************************************/
304 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
306 struct find_map_state state;
308 state.found = false;
309 state.name = NULL; /* Indicate we're looking for gid */
310 state.gid = gid;
311 state.map = map;
313 dbwrap_traverse_read(db, find_map, (void *)&state, NULL);
315 return state.found;
318 /****************************************************************************
319 Return the sid and the type of the unix group.
320 ****************************************************************************/
322 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
324 struct find_map_state state;
326 state.found = false;
327 state.name = name;
328 state.map = map;
330 dbwrap_traverse_read(db, find_map, (void *)&state, NULL);
332 return state.found;
335 /****************************************************************************
336 Remove a group mapping entry.
337 ****************************************************************************/
339 static bool group_map_remove(const struct dom_sid *sid)
341 char *key;
342 NTSTATUS status;
344 key = group_mapping_key(talloc_tos(), sid);
345 if (key == NULL) {
346 return false;
349 status = dbwrap_trans_delete(db, string_term_tdb_data(key));
351 TALLOC_FREE(key);
352 return NT_STATUS_IS_OK(status);
355 /****************************************************************************
356 Enumerate the group mapping.
357 ****************************************************************************/
359 struct enum_map_state {
360 const struct dom_sid *domsid;
361 enum lsa_SidType sid_name_use;
362 bool unix_only;
364 size_t num_maps;
365 GROUP_MAP **maps;
368 static int collect_map(struct db_record *rec, void *private_data)
370 struct enum_map_state *state = (struct enum_map_state *)private_data;
371 GROUP_MAP *map;
372 GROUP_MAP **tmp;
374 map = talloc_zero(NULL, GROUP_MAP);
375 if (!map) {
376 DEBUG(0, ("Unable to allocate group map!\n"));
377 return 1;
380 if (!dbrec2map(rec, map)) {
381 TALLOC_FREE(map);
382 return 0;
384 /* list only the type or everything if UNKNOWN */
385 if (state->sid_name_use != SID_NAME_UNKNOWN
386 && state->sid_name_use != map->sid_name_use) {
387 DEBUG(11,("enum_group_mapping: group %s is not of the "
388 "requested type\n", map->nt_name));
389 TALLOC_FREE(map);
390 return 0;
393 if ((state->unix_only == ENUM_ONLY_MAPPED) && (map->gid == -1)) {
394 DEBUG(11,("enum_group_mapping: group %s is non mapped\n",
395 map->nt_name));
396 TALLOC_FREE(map);
397 return 0;
400 if ((state->domsid != NULL) &&
401 (dom_sid_compare_domain(state->domsid, &map->sid) != 0)) {
402 struct dom_sid_buf buf;
403 DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
404 dom_sid_str_buf(&map->sid, &buf)));
405 TALLOC_FREE(map);
406 return 0;
409 tmp = talloc_realloc(NULL, state->maps, GROUP_MAP *,
410 state->num_maps + 1);
411 if (!tmp) {
412 DEBUG(0,("enum_group_mapping: Unable to enlarge group "
413 "map!\n"));
414 TALLOC_FREE(map);
415 return 1;
418 state->maps = tmp;
419 state->maps[state->num_maps] = talloc_move(state->maps, &map);
420 state->num_maps++;
421 return 0;
424 static bool enum_group_mapping(const struct dom_sid *domsid,
425 enum lsa_SidType sid_name_use,
426 GROUP_MAP ***pp_rmap,
427 size_t *p_num_entries, bool unix_only)
429 struct enum_map_state state;
430 NTSTATUS status;
432 state.domsid = domsid;
433 state.sid_name_use = sid_name_use;
434 state.unix_only = unix_only;
435 state.num_maps = 0;
436 state.maps = NULL;
438 status = dbwrap_traverse_read(db, collect_map, (void *)&state, NULL);
439 if (!NT_STATUS_IS_OK(status)) {
440 TALLOC_FREE(state.maps);
441 return false;
444 *pp_rmap = state.maps;
445 *p_num_entries = state.num_maps;
447 return true;
450 /* This operation happens on session setup, so it should better be fast. We
451 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
453 static NTSTATUS one_alias_membership(const struct dom_sid *member,
454 struct dom_sid **sids, size_t *num)
456 struct dom_sid_buf tmp;
457 fstring key;
458 char *string_sid;
459 TDB_DATA dbuf;
460 const char *p;
461 NTSTATUS status;
462 TALLOC_CTX *frame = talloc_stackframe();
464 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX,
465 dom_sid_str_buf(member, &tmp));
467 status = dbwrap_fetch_bystring(db, frame, key, &dbuf);
468 if (!NT_STATUS_IS_OK(status)) {
469 TALLOC_FREE(frame);
470 return NT_STATUS_OK;
473 p = (const char *)dbuf.dptr;
475 while (next_token_talloc(frame, &p, &string_sid, " ")) {
476 struct dom_sid alias;
477 uint32_t num_sids;
479 if (!string_to_sid(&alias, string_sid))
480 continue;
482 num_sids = *num;
483 status= add_sid_to_array_unique(NULL, &alias, sids, &num_sids);
484 if (!NT_STATUS_IS_OK(status)) {
485 goto done;
487 *num = num_sids;
490 done:
491 TALLOC_FREE(frame);
492 return status;
495 static NTSTATUS alias_memberships(const struct dom_sid *members, size_t num_members,
496 struct dom_sid **sids, size_t *num)
498 size_t i;
500 *num = 0;
501 *sids = NULL;
503 for (i=0; i<num_members; i++) {
504 NTSTATUS status = one_alias_membership(&members[i], sids, num);
505 if (!NT_STATUS_IS_OK(status))
506 return status;
508 return NT_STATUS_OK;
511 static bool is_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
513 struct dom_sid *sids;
514 size_t i;
515 size_t num;
517 /* This feels the wrong way round, but the on-disk data structure
518 * dictates it this way. */
519 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
520 return False;
522 for (i=0; i<num; i++) {
523 if (dom_sid_compare(alias, &sids[i]) == 0) {
524 TALLOC_FREE(sids);
525 return True;
528 TALLOC_FREE(sids);
529 return False;
533 static NTSTATUS add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
535 GROUP_MAP *map;
536 char *key;
537 struct dom_sid_buf string_sid;
538 char *new_memberstring;
539 struct db_record *rec;
540 NTSTATUS status;
541 TDB_DATA value;
543 map = talloc_zero(talloc_tos(), GROUP_MAP);
544 if (!map) {
545 return NT_STATUS_NO_MEMORY;
548 if (!get_group_map_from_sid(*alias, map)) {
549 TALLOC_FREE(map);
550 return NT_STATUS_NO_SUCH_ALIAS;
553 if ((map->sid_name_use != SID_NAME_ALIAS) &&
554 (map->sid_name_use != SID_NAME_WKN_GRP)) {
555 TALLOC_FREE(map);
556 return NT_STATUS_NO_SUCH_ALIAS;
559 TALLOC_FREE(map);
561 if (is_aliasmem(alias, member))
562 return NT_STATUS_MEMBER_IN_ALIAS;
564 key = talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX,
565 dom_sid_str_buf(member, &string_sid));
566 if (key == NULL) {
567 return NT_STATUS_NO_MEMORY;
570 if (dbwrap_transaction_start(db) != 0) {
571 DEBUG(0, ("transaction_start failed\n"));
572 return NT_STATUS_INTERNAL_DB_CORRUPTION;
575 rec = dbwrap_fetch_locked(db, key, string_term_tdb_data(key));
577 if (rec == NULL) {
578 DEBUG(10, ("fetch_lock failed\n"));
579 TALLOC_FREE(key);
580 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
581 goto cancel;
584 value = dbwrap_record_get_value(rec);
586 dom_sid_str_buf(alias, &string_sid);
588 if (value.dptr != NULL) {
589 new_memberstring = talloc_asprintf(
590 key, "%s %s", (char *)(value.dptr), string_sid.buf);
591 } else {
592 new_memberstring = talloc_strdup(key, string_sid.buf);
595 if (new_memberstring == NULL) {
596 TALLOC_FREE(key);
597 status = NT_STATUS_NO_MEMORY;
598 goto cancel;
601 status = dbwrap_record_store(rec, string_term_tdb_data(new_memberstring), 0);
603 TALLOC_FREE(key);
605 if (!NT_STATUS_IS_OK(status)) {
606 DEBUG(10, ("Could not store record: %s\n", nt_errstr(status)));
607 goto cancel;
610 if (dbwrap_transaction_commit(db) != 0) {
611 DEBUG(0, ("transaction_commit failed\n"));
612 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
613 return status;
616 return NT_STATUS_OK;
618 cancel:
619 if (dbwrap_transaction_cancel(db) != 0) {
620 smb_panic("transaction_cancel failed");
623 return status;
626 struct aliasmem_state {
627 TALLOC_CTX *mem_ctx;
628 const struct dom_sid *alias;
629 struct dom_sid **sids;
630 size_t *num;
633 static int collect_aliasmem(struct db_record *rec, void *priv)
635 struct aliasmem_state *state = (struct aliasmem_state *)priv;
636 const char *p;
637 char *alias_string;
638 TALLOC_CTX *frame;
639 TDB_DATA key = dbwrap_record_get_key(rec);
640 TDB_DATA value = dbwrap_record_get_value(rec);
642 if (strncmp((const char *)key.dptr, MEMBEROF_PREFIX,
643 MEMBEROF_PREFIX_LEN) != 0)
644 return 0;
646 p = (const char *)value.dptr;
648 frame = talloc_stackframe();
650 while (next_token_talloc(frame, &p, &alias_string, " ")) {
651 struct dom_sid alias, member;
652 const char *member_string;
653 uint32_t num_sids;
655 if (!string_to_sid(&alias, alias_string))
656 continue;
658 if (dom_sid_compare(state->alias, &alias) != 0)
659 continue;
661 /* Ok, we found the alias we're looking for in the membership
662 * list currently scanned. The key represents the alias
663 * member. Add that. */
665 member_string = strchr((const char *)key.dptr, '/');
667 /* Above we tested for MEMBEROF_PREFIX which includes the
668 * slash. */
670 SMB_ASSERT(member_string != NULL);
671 member_string += 1;
673 if (!string_to_sid(&member, member_string))
674 continue;
676 num_sids = *state->num;
677 if (!NT_STATUS_IS_OK(add_sid_to_array(state->mem_ctx, &member,
678 state->sids,
679 &num_sids)))
681 /* talloc fail. */
682 break;
684 *state->num = num_sids;
687 TALLOC_FREE(frame);
688 return 0;
691 static NTSTATUS enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
692 struct dom_sid **sids, size_t *num)
694 GROUP_MAP *map;
695 struct aliasmem_state state;
697 map = talloc_zero(talloc_tos(), GROUP_MAP);
698 if (!map) {
699 return NT_STATUS_NO_MEMORY;
702 if (!get_group_map_from_sid(*alias, map)) {
703 TALLOC_FREE(map);
704 return NT_STATUS_NO_SUCH_ALIAS;
707 if ((map->sid_name_use != SID_NAME_ALIAS) &&
708 (map->sid_name_use != SID_NAME_WKN_GRP)) {
709 TALLOC_FREE(map);
710 return NT_STATUS_NO_SUCH_ALIAS;
713 TALLOC_FREE(map);
715 *sids = NULL;
716 *num = 0;
718 state.alias = alias;
719 state.sids = sids;
720 state.num = num;
721 state.mem_ctx = mem_ctx;
723 dbwrap_traverse_read(db, collect_aliasmem, &state, NULL);
724 return NT_STATUS_OK;
727 static NTSTATUS del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
729 NTSTATUS status;
730 struct dom_sid *sids;
731 size_t i, num;
732 bool found = False;
733 char *member_string;
734 char *key;
735 struct dom_sid_buf sid_string;
737 if (dbwrap_transaction_start(db) != 0) {
738 DEBUG(0, ("transaction_start failed\n"));
739 return NT_STATUS_INTERNAL_DB_CORRUPTION;
742 status = alias_memberships(member, 1, &sids, &num);
744 if (!NT_STATUS_IS_OK(status)) {
745 goto cancel;
748 for (i=0; i<num; i++) {
749 if (dom_sid_compare(&sids[i], alias) == 0) {
750 found = True;
751 break;
755 if (!found) {
756 TALLOC_FREE(sids);
757 status = NT_STATUS_MEMBER_NOT_IN_ALIAS;
758 goto cancel;
761 if (i < num)
762 sids[i] = sids[num-1];
764 num -= 1;
766 key = talloc_asprintf(
767 sids,
768 "%s%s",
769 MEMBEROF_PREFIX,
770 dom_sid_str_buf(member, &sid_string));
771 if (key == NULL) {
772 TALLOC_FREE(sids);
773 status = NT_STATUS_NO_MEMORY;
774 goto cancel;
777 if (num == 0) {
778 status = dbwrap_delete_bystring(db, key);
779 goto commit;
782 member_string = talloc_strdup(sids, "");
783 if (member_string == NULL) {
784 TALLOC_FREE(sids);
785 status = NT_STATUS_NO_MEMORY;
786 goto cancel;
789 for (i=0; i<num; i++) {
791 member_string = talloc_asprintf_append_buffer(
792 member_string,
793 " %s",
794 dom_sid_str_buf(&sids[i], &sid_string));
796 if (member_string == NULL) {
797 TALLOC_FREE(sids);
798 status = NT_STATUS_NO_MEMORY;
799 goto cancel;
803 status = dbwrap_store_bystring(
804 db, key, string_term_tdb_data(member_string), 0);
805 commit:
806 TALLOC_FREE(sids);
808 if (!NT_STATUS_IS_OK(status)) {
809 DEBUG(10, ("dbwrap_store_bystring failed: %s\n",
810 nt_errstr(status)));
811 goto cancel;
814 if (dbwrap_transaction_commit(db) != 0) {
815 DEBUG(0, ("transaction_commit failed\n"));
816 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
817 return status;
820 return NT_STATUS_OK;
822 cancel:
823 if (dbwrap_transaction_cancel(db) != 0) {
824 smb_panic("transaction_cancel failed");
826 return status;
830 /* -- ldb->tdb switching code -------------------------------------------- */
832 /* change this if the data format ever changes */
833 #define LTDB_PACKING_FORMAT 0x26011967
835 /* old packing formats (not supported for now,
836 * it was never used for group mapping AFAIK) */
837 #define LTDB_PACKING_FORMAT_NODN 0x26011966
839 static unsigned int pull_uint32(uint8_t *p, int ofs)
841 p += ofs;
842 return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
846 unpack a ldb message from a linear buffer in TDB_DATA
848 static int convert_ldb_record(TDB_CONTEXT *ltdb, TDB_DATA key,
849 TDB_DATA data, void *ptr)
851 TALLOC_CTX *tmp_ctx = talloc_tos();
852 GROUP_MAP *map = NULL;
853 uint8_t *p;
854 uint32_t format;
855 uint32_t num_el;
856 unsigned int remaining;
857 unsigned int i, j;
858 size_t len;
859 char *name;
860 char *val;
861 uint32_t num_mem = 0;
862 struct dom_sid *members = NULL;
863 int error = 0;
865 p = (uint8_t *)data.dptr;
866 if (data.dsize < 8) {
867 errno = EIO;
868 goto failed;
871 format = pull_uint32(p, 0);
872 num_el = pull_uint32(p, 4);
873 p += 8;
875 remaining = data.dsize - 8;
877 switch (format) {
878 case LTDB_PACKING_FORMAT:
879 len = strnlen((char *)p, remaining);
880 if (len == remaining) {
881 errno = EIO;
882 goto failed;
885 if (*p == '@') {
886 /* ignore special LDB attributes */
887 return 0;
890 if (strncmp((char *)p, "rid=", 4)) {
891 /* unknown entry, ignore */
892 DEBUG(3, ("Found unknown entry in group mapping "
893 "database named [%s]\n", (char *)p));
894 return 0;
897 remaining -= len + 1;
898 p += len + 1;
899 break;
901 case LTDB_PACKING_FORMAT_NODN:
902 default:
903 errno = EIO;
904 goto failed;
907 if (num_el == 0) {
908 /* bad entry, ignore */
909 return 0;
912 if (num_el > remaining / 6) {
913 errno = EIO;
914 goto failed;
917 map = talloc_zero(NULL, GROUP_MAP);
918 if (!map) {
919 errno = ENOMEM;
920 goto failed;
923 for (i = 0; i < num_el; i++) {
924 uint32_t num_vals;
926 if (remaining < 10) {
927 errno = EIO;
928 goto failed;
930 len = strnlen((char *)p, remaining - 6);
931 if (len == remaining - 6) {
932 errno = EIO;
933 goto failed;
935 name = talloc_strndup(tmp_ctx, (char *)p, len);
936 if (name == NULL) {
937 errno = ENOMEM;
938 goto failed;
940 remaining -= len + 1;
941 p += len + 1;
943 num_vals = pull_uint32(p, 0);
944 if (strcasecmp_m(name, "member") == 0) {
945 num_mem = num_vals;
946 members = talloc_array(tmp_ctx, struct dom_sid, num_mem);
947 if (members == NULL) {
948 errno = ENOMEM;
949 goto failed;
951 } else if (num_vals != 1) {
952 errno = EIO;
953 goto failed;
956 p += 4;
957 remaining -= 4;
959 for (j = 0; j < num_vals; j++) {
960 len = pull_uint32(p, 0);
961 if (len > remaining-5) {
962 errno = EIO;
963 goto failed;
966 val = talloc_strndup(tmp_ctx, (char *)(p + 4), len);
967 if (val == NULL) {
968 errno = ENOMEM;
969 goto failed;
972 remaining -= len+4+1;
973 p += len+4+1;
975 /* we ignore unknown or uninteresting attributes
976 * (objectclass, etc.) */
977 if (strcasecmp_m(name, "gidNumber") == 0) {
978 map->gid = smb_strtoul(val,
979 NULL,
981 &error,
982 SMB_STR_FULL_STR_CONV);
983 if (error != 0) {
984 errno = EIO;
985 goto failed;
987 } else if (strcasecmp_m(name, "sid") == 0) {
988 if (!string_to_sid(&map->sid, val)) {
989 errno = EIO;
990 goto failed;
992 } else if (strcasecmp_m(name, "sidNameUse") == 0) {
993 map->sid_name_use =
994 smb_strtoul(val,
995 NULL,
997 &error,
998 SMB_STR_FULL_STR_CONV);
999 if (error != 0) {
1000 errno = EIO;
1001 goto failed;
1003 } else if (strcasecmp_m(name, "ntname") == 0) {
1004 map->nt_name = talloc_strdup(map, val);
1005 if (!map->nt_name) {
1006 errno = ENOMEM;
1007 goto failed;
1009 } else if (strcasecmp_m(name, "comment") == 0) {
1010 map->comment = talloc_strdup(map, val);
1011 if (!map->comment) {
1012 errno = ENOMEM;
1013 goto failed;
1015 } else if (strcasecmp_m(name, "member") == 0) {
1016 if (!string_to_sid(&members[j], val)) {
1017 errno = EIO;
1018 goto failed;
1022 TALLOC_FREE(val);
1025 TALLOC_FREE(name);
1028 if (map->nt_name == NULL) {
1029 errno = EIO;
1030 goto failed;
1033 if (map->comment == NULL) {
1034 map->comment = talloc_strdup(map, "");
1036 if (map->comment == NULL) {
1037 errno = ENOMEM;
1038 goto failed;
1041 if (!add_mapping_entry(map, 0)) {
1042 errno = EIO;
1043 goto failed;
1046 if (num_mem) {
1047 for (j = 0; j < num_mem; j++) {
1048 NTSTATUS status;
1049 status = add_aliasmem(&map->sid, &members[j]);
1050 if (!NT_STATUS_IS_OK(status)) {
1051 errno = EIO;
1052 goto failed;
1057 if (remaining != 0) {
1058 DEBUG(0, ("Error: %d bytes unread in ltdb_unpack_data\n",
1059 remaining));
1062 TALLOC_FREE(map);
1063 return 0;
1065 failed:
1066 TALLOC_FREE(map);
1067 return -1;
1070 static bool mapping_switch(const char *ldb_path)
1072 TDB_CONTEXT *ltdb;
1073 TALLOC_CTX *frame;
1074 char *new_path;
1075 int ret;
1077 frame = talloc_stackframe();
1079 ltdb = tdb_open_log(ldb_path, 0, TDB_DEFAULT, O_RDONLY, 0600);
1080 if (ltdb == NULL) goto failed;
1082 /* ldb is just a very fancy tdb, read out raw data and perform
1083 * conversion */
1084 ret = tdb_traverse(ltdb, convert_ldb_record, NULL);
1085 if (ret < 0) goto failed;
1087 if (ltdb) {
1088 tdb_close(ltdb);
1089 ltdb = NULL;
1092 /* now rename the old db out of the way */
1093 new_path = state_path(talloc_tos(), "group_mapping.ldb.replaced");
1094 if (!new_path) {
1095 goto failed;
1097 if (rename(ldb_path, new_path) != 0) {
1098 DEBUG(0,("Failed to rename old group mapping database\n"));
1099 goto failed;
1101 TALLOC_FREE(frame);
1102 return True;
1104 failed:
1105 DEBUG(0, ("Failed to switch to tdb group mapping database\n"));
1106 if (ltdb) tdb_close(ltdb);
1107 TALLOC_FREE(frame);
1108 return False;
1111 static const struct mapping_backend tdb_backend = {
1112 .add_mapping_entry = add_mapping_entry,
1113 .get_group_map_from_sid = get_group_map_from_sid,
1114 .get_group_map_from_gid = get_group_map_from_gid,
1115 .get_group_map_from_ntname = get_group_map_from_ntname,
1116 .group_map_remove = group_map_remove,
1117 .enum_group_mapping = enum_group_mapping,
1118 .one_alias_membership = one_alias_membership,
1119 .add_aliasmem = add_aliasmem,
1120 .del_aliasmem = del_aliasmem,
1121 .enum_aliasmem = enum_aliasmem
1125 initialise the tdb mapping backend
1127 const struct mapping_backend *groupdb_tdb_init(void)
1129 if (!init_group_mapping()) {
1130 DEBUG(0,("Failed to initialise tdb mapping backend\n"));
1131 return NULL;
1134 return &tdb_backend;