r9712: Bunch of small fixes
[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_idmapdb *idmap)
43 {
44         TDB_CONTEXT *tdb;
45         TDB_DATA key, val;
46
47         /* Open idmap repository */
48         if (!(tdb = tdb_open(fn, 0, TDB_DEFAULT, O_RDONLY, 0644))) {
49                 DEBUG(0, ("idmap_init: Unable to open idmap database '%s'\n", fn));
50                 return NT_STATUS_UNSUCCESSFUL;
51         }
52
53         idmap->mapping_count = 0;
54         idmap->mappings = NULL;
55         idmap->user_hwm = tdb_fetch_int32(tdb, HWM_USER);
56         idmap->group_hwm = tdb_fetch_int32(tdb, HWM_GROUP);
57
58         for (key = tdb_firstkey(tdb); key.dptr; key = tdb_nextkey(tdb, key)) 
59         {
60                 struct samba3_idmap_mapping map;
61                 
62                 if (strncmp(key.dptr, "GID ", 4) == 0) {
63                         map.type = IDMAP_GROUP;
64                         map.unix_id = atoi(key.dptr+4);
65                         val = tdb_fetch(tdb, key);
66                         map.sid = dom_sid_parse_talloc(ctx, val.dptr);
67                 } else if (strncmp(key.dptr, "UID ", 4) == 0) {
68                         map.type = IDMAP_USER;
69                         map.unix_id = atoi(key.dptr+4);
70                         val = tdb_fetch(tdb, key);
71                         map.sid = dom_sid_parse_talloc(ctx, val.dptr);
72                 } else {
73                         continue;
74                 }
75
76                 idmap->mappings = talloc_realloc(ctx, idmap->mappings, struct samba3_idmap_mapping, idmap->mapping_count+1);
77
78                 idmap->mappings[idmap->mapping_count] = map;
79                 idmap->mapping_count++;
80         }
81
82         tdb_close(tdb);
83
84         return NT_STATUS_OK;
85 }