r9555: More updates. Everything except for secrets.c compiles now..
[sfrench/samba-autobuild/.git] / source4 / lib / samba3 / idmap.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    idmap TDB backend
5
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
10    
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 2 of the License, or
14    (at your option) any later version.
15    
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.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27 #include "lib/tdb/include/tdbutil.h"
28 #include "lib/samba3/samba3.h"
29 #include "system/filesys.h"
30
31 /* High water mark keys */
32 #define HWM_GROUP  "GROUP HWM"
33 #define HWM_USER   "USER HWM"
34
35 /* idmap version determines auto-conversion */
36 #define IDMAP_VERSION 2
37
38 /*****************************************************************************
39  Initialise idmap database. 
40 *****************************************************************************/
41
42 NTSTATUS samba3_read_idmap( const char *fn, TALLOC_CTX *ctx, struct samba3_idmap *idmap )
43 {
44         int32_t version;
45         TDB_CONTEXT *tdb;
46         TDB_DATA key, val;
47
48         /* Open idmap repository */
49         if (!(tdb = tdb_open(fn, 0, TDB_DEFAULT, O_RDONLY, 0644))) {
50                 DEBUG(0, ("idmap_init: Unable to open idmap database\n"));
51                 return NT_STATUS_UNSUCCESSFUL;
52         }
53
54         /* check against earlier versions */
55         version = tdb_fetch_int32(tdb, "IDMAP_VERSION");
56         if (version != IDMAP_VERSION) {
57                 DEBUG(0, ("idmap_init: Unable to open idmap database, it's in an old format!\n"));
58                 return NT_STATUS_INTERNAL_DB_ERROR;
59         }
60
61         idmap->mapping_count = 0;
62         idmap->mappings = NULL;
63         idmap->user_hwm = tdb_fetch_int32(tdb, HWM_USER);
64         idmap->group_hwm = tdb_fetch_int32(tdb, HWM_GROUP);
65
66         for (key = tdb_firstkey(tdb); key.dptr; key = tdb_nextkey(tdb, key)) 
67         {
68                 struct samba3_idmap_mapping map;
69                 
70                 if (strncmp(key.dptr, "GID ", 4) == 0) {
71                         map.type = IDMAP_GROUP;
72                         map.unix_id = atoi(key.dptr+4);
73                         val = tdb_fetch(tdb, key);
74                         map.sid = dom_sid_parse_talloc(ctx, val.dptr);
75                 } else if (strncmp(key.dptr, "UID ", 4) == 0) {
76                         map.type = IDMAP_USER;
77                         map.unix_id = atoi(key.dptr+4);
78                         val = tdb_fetch(tdb, key);
79                         map.sid = dom_sid_parse_talloc(ctx, val.dptr);
80                 } else {
81                         continue;
82                 }
83
84                 idmap->mappings = talloc_realloc(ctx, idmap->mappings, struct samba3_idmap_mapping, idmap->mapping_count+1);
85
86                 idmap->mappings[idmap->mapping_count] = map;
87                 idmap->mapping_count++;
88         }
89
90         tdb_close(tdb);
91
92         return NT_STATUS_OK;
93 }