r11518: Fix a warning
[Samba/ekacnet.git] / source4 / lib / samba3 / group.c
blobcf76bddffe3b8be5406b0753e0ecf46116d13f8c
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.
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.
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.
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"
28 #define DATABASE_VERSION_V1 1 /* native byte format. */
29 #define DATABASE_VERSION_V2 2 /* le format. */
31 #define GROUP_PREFIX "UNIXGROUP/"
33 /* Alias memberships are stored reverse, as memberships. The performance
34 * critical operation is to determine the aliases a SID is member of, not
35 * listing alias members. So we store a list of alias SIDs a SID is member of
36 * hanging of the member as key.
38 #define MEMBEROF_PREFIX "MEMBEROF/"
40 /****************************************************************************
41 Open the group mapping tdb.
42 ****************************************************************************/
43 NTSTATUS samba3_read_grouptdb(const char *file, TALLOC_CTX *ctx, struct samba3_groupdb *db)
45 int32_t vers_id;
46 TDB_DATA kbuf, dbuf, newkey;
47 int ret;
48 TDB_CONTEXT *tdb;
50 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;
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;
63 if (vers_id != DATABASE_VERSION_V2) {
64 DEBUG(0, ("Group database version mismatch: %d\n", vers_id));
65 return NT_STATUS_UNSUCCESSFUL;
68 db->groupmappings = NULL;
69 db->groupmap_count = 0;
70 db->aliases = NULL;
71 db->alias_count = 0;
73 for (kbuf = tdb_firstkey(tdb);
74 kbuf.dptr;
75 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
76 struct samba3_groupmapping map;
78 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) == 0)
80 dbuf = tdb_fetch(tdb, kbuf);
81 if (!dbuf.dptr)
82 continue;
84 ZERO_STRUCT(map);
86 map.sid = dom_sid_parse_talloc(ctx, kbuf.dptr+strlen(GROUP_PREFIX));
88 ret = tdb_unpack(tdb, dbuf.dptr, dbuf.dsize, "dd",
89 &map.gid, &map.sid_name_use);
91 if ( ret == -1 ) {
92 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
93 continue;
96 map.nt_name = talloc_strdup(ctx, dbuf.dptr+ret);
97 map.comment = talloc_strdup(ctx, dbuf.dptr+ret+strlen(map.nt_name));
99 db->groupmappings = talloc_realloc(ctx, db->groupmappings, struct samba3_groupmapping, db->groupmap_count+1);
101 if (!db->groupmappings)
102 return NT_STATUS_NO_MEMORY;
104 db->groupmappings[db->groupmap_count] = map;
106 db->groupmap_count++;
107 } else if (strncmp(kbuf.dptr, MEMBEROF_PREFIX, strlen(MEMBEROF_PREFIX)) == 0)
109 struct samba3_alias alias;
110 const char **member_strlist;
111 int i;
113 dbuf = tdb_fetch(tdb, kbuf);
114 if (!dbuf.dptr)
115 continue;
117 alias.sid = dom_sid_parse_talloc(ctx, kbuf.dptr+strlen(MEMBEROF_PREFIX));
118 alias.member_count = 0;
119 alias.members = NULL;
121 member_strlist = str_list_make_shell(ctx, dbuf.dptr, " ");
123 for (i = 0; member_strlist[i]; i++) {
124 alias.members = talloc_realloc(ctx, alias.members, struct dom_sid *, alias.member_count+1);
125 alias.members[alias.member_count] = dom_sid_parse_talloc(ctx, member_strlist[i]);
126 alias.member_count++;
129 talloc_free(member_strlist);
131 db->aliases = talloc_realloc(ctx, db->aliases, struct samba3_alias, db->alias_count+1);
132 db->aliases[db->alias_count] = alias;
133 db->alias_count++;
137 tdb_close(tdb);
139 return NT_STATUS_OK;