2 Unix SMB/CIFS implementation.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) Simo Sorce 2003
9 Copyright (C) Jelmer Vernooij 2005
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "lib/tdb/include/tdb.h"
27 #include "lib/util/util_tdb.h"
28 #include "lib/samba3/samba3.h"
29 #include "system/filesys.h"
30 #include "libcli/security/security.h"
32 /* High water mark keys */
33 #define HWM_GROUP "GROUP HWM"
34 #define HWM_USER "USER HWM"
36 /* idmap version determines auto-conversion */
37 #define IDMAP_VERSION 2
39 /*****************************************************************************
40 Initialise idmap database.
41 *****************************************************************************/
43 NTSTATUS samba3_read_idmap(const char *fn, TALLOC_CTX *ctx, struct samba3_idmapdb *idmap)
49 /* Open idmap repository */
50 if (!(tdb = tdb_open(fn, 0, TDB_DEFAULT, O_RDONLY, 0644))) {
51 DEBUG(0, ("idmap_init: Unable to open idmap database '%s'\n", fn));
52 return NT_STATUS_UNSUCCESSFUL;
55 idmap->mapping_count = 0;
56 idmap->mappings = NULL;
57 idmap->user_hwm = tdb_fetch_int32(tdb, HWM_USER);
58 idmap->group_hwm = tdb_fetch_int32(tdb, HWM_GROUP);
60 /* check against earlier versions */
61 version = tdb_fetch_int32(tdb, "IDMAP_VERSION");
62 if (version != IDMAP_VERSION) {
63 DEBUG(0, ("idmap_init: Unable to open idmap database, it's in an old format!\n"));
64 return NT_STATUS_INTERNAL_DB_ERROR;
67 for (key = tdb_firstkey(tdb); key.dptr; key = tdb_nextkey(tdb, key))
69 struct samba3_idmap_mapping map;
70 const char *k = (const char *)key.dptr;
73 if (strncmp(k, "GID ", 4) == 0) {
74 map.type = IDMAP_GROUP;
75 map.unix_id = atoi(k+4);
76 val = tdb_fetch(tdb, key);
77 v = (const char *)val.dptr;
78 map.sid = dom_sid_parse_talloc(ctx, v);
79 } else if (strncmp(k, "UID ", 4) == 0) {
80 map.type = IDMAP_USER;
81 map.unix_id = atoi(k+4);
82 val = tdb_fetch(tdb, key);
83 v = (const char *)val.dptr;
84 map.sid = dom_sid_parse_talloc(ctx, v);
89 idmap->mappings = talloc_realloc(ctx, idmap->mappings, struct samba3_idmap_mapping, idmap->mapping_count+1);
91 idmap->mappings[idmap->mapping_count] = map;
92 idmap->mapping_count++;