fc8f8c40b87e1aecab568deeea06e47c7599ed68
[sfrench/samba-autobuild/.git] / source4 / lib / samba3 / group.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-2000,
5  *  Copyright (C) Jean Fran├žois Micouleau      1998-2001.
6  *  
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *  
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "includes.h"
23 #include "system/iconv.h"
24 #include "lib/samba3/samba3.h"
25 #include "lib/tdb/include/tdbutil.h"
26 #include "system/filesys.h"
27 #include "pstring.h"
28
29 #define DATABASE_VERSION_V1 1 /* native byte format. */
30 #define DATABASE_VERSION_V2 2 /* le format. */
31
32 #define GROUP_PREFIX "UNIXGROUP/"
33
34 /* Alias memberships are stored reverse, as memberships. The performance
35  * critical operation is to determine the aliases a SID is member of, not
36  * listing alias members. So we store a list of alias SIDs a SID is member of
37  * hanging of the member as key.
38  */
39 #define MEMBEROF_PREFIX "MEMBEROF/"
40
41 /****************************************************************************
42  Open the group mapping tdb.
43 ****************************************************************************/
44 NTSTATUS samba3_read_grouptdb(const char *file, TALLOC_CTX *ctx, struct samba3_groupdb *db)
45 {
46         int32_t vers_id;
47         TDB_DATA kbuf, dbuf, newkey;
48         int ret;
49         
50         TDB_CONTEXT *tdb = tdb_open(file, 0, TDB_DEFAULT, O_RDONLY, 0600);
51         if (!tdb) {
52                 DEBUG(0,("Failed to open group mapping database\n"));
53                 return NT_STATUS_UNSUCCESSFUL;
54         }
55
56         /* Cope with byte-reversed older versions of the db. */
57         vers_id = tdb_fetch_int32(tdb, "INFO/version");
58         if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
59                 /* Written on a bigendian machine with old fetch_int code. Save as le. */
60                 vers_id = DATABASE_VERSION_V2;
61         }
62
63         if (vers_id != DATABASE_VERSION_V2) {
64                 return NT_STATUS_UNSUCCESSFUL;
65         }
66
67         db->groupmappings = NULL;
68         db->groupmap_count = 0;
69         db->aliases = NULL;
70         db->alias_count = 0;
71
72         for (kbuf = tdb_firstkey(tdb); 
73              kbuf.dptr; 
74              newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
75                 struct samba3_groupmapping map;
76
77                 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) == 0)
78                 {
79                         dbuf = tdb_fetch(tdb, kbuf);
80                         if (!dbuf.dptr)
81                                 continue;
82
83                         map.sid = dom_sid_parse_talloc(tdb, kbuf.dptr+strlen(GROUP_PREFIX));
84
85                         ret = tdb_unpack(tdb, dbuf.dptr, dbuf.dsize, "ddff",
86                                                          &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
87
88                         SAFE_FREE(dbuf.dptr);
89
90                         if ( ret == -1 ) {
91                                 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
92                                 continue;
93                         }
94
95                         db->groupmappings = talloc_realloc(tdb, db->groupmappings, struct samba3_groupmapping, db->groupmap_count+1);
96
97                         if (!db->groupmappings) 
98                                 return NT_STATUS_NO_MEMORY;
99
100                         db->groupmappings[db->groupmap_count] = map;
101
102                         db->groupmap_count++;
103                 } 
104
105                 if (strncmp(kbuf.dptr, MEMBEROF_PREFIX, strlen(MEMBEROF_PREFIX)) == 0)
106                 {
107                         struct samba3_alias alias;
108                         pstring alias_string;
109                         const char *p;
110
111                         dbuf = tdb_fetch(tdb, kbuf);
112                         if (!dbuf.dptr)
113                                 continue;
114
115                         alias.sid = dom_sid_parse_talloc(ctx, kbuf.dptr+strlen(MEMBEROF_PREFIX));
116                         alias.member_count = 0;
117                         alias.members = NULL;
118
119                         p = dbuf.dptr;
120                         while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
121
122                                 alias.members = talloc_realloc(ctx, alias.members, struct dom_sid *, alias.member_count+1);
123                                 alias.members[alias.member_count] = dom_sid_parse_talloc(ctx, alias_string);
124                                 alias.member_count++;
125
126                         }
127
128                         db->aliases = talloc_realloc(ctx, db->aliases, struct samba3_alias, db->alias_count+1);
129                         db->aliases[db->alias_count] = alias;
130                         db->alias_count++;
131                 }
132         }
133
134         tdb_close(tdb);
135
136         return NT_STATUS_OK;
137 }