r23792: convert Samba4 to GPLv3
[ira/wip.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 3 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, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
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"
31
32 /* High water mark keys */
33 #define HWM_GROUP  "GROUP HWM"
34 #define HWM_USER   "USER HWM"
35
36 /* idmap version determines auto-conversion */
37 #define IDMAP_VERSION 2
38
39 /*****************************************************************************
40  Initialise idmap database. 
41 *****************************************************************************/
42
43 NTSTATUS samba3_read_idmap(const char *fn, TALLOC_CTX *ctx, struct samba3_idmapdb *idmap)
44 {
45         TDB_CONTEXT *tdb;
46         TDB_DATA key, val;
47         int32_t version;
48
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;
53         }
54
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);
59
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;
65         }
66
67         for (key = tdb_firstkey(tdb); key.dptr; key = tdb_nextkey(tdb, key)) 
68         {
69                 struct samba3_idmap_mapping map;
70                 const char *k = (const char *)key.dptr;
71                 const char *v;
72
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);
85                 } else {
86                         continue;
87                 }
88
89                 idmap->mappings = talloc_realloc(ctx, idmap->mappings, struct samba3_idmap_mapping, idmap->mapping_count+1);
90
91                 idmap->mappings[idmap->mapping_count] = map;
92                 idmap->mapping_count++;
93         }
94
95         tdb_close(tdb);
96
97         return NT_STATUS_OK;
98 }