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.
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.
23 #include "lib/samba3/samba3.h"
24 #include "lib/tdb/include/tdb.h"
25 #include "lib/util/util_tdb.h"
26 #include "system/filesys.h"
27 #include "libcli/security/security.h"
29 #define DATABASE_VERSION_V1 1 /* native byte format. */
30 #define DATABASE_VERSION_V2 2 /* le format. */
32 #define GROUP_PREFIX "UNIXGROUP/"
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.
39 #define MEMBEROF_PREFIX "MEMBEROF/"
41 /****************************************************************************
42 Open the group mapping tdb.
43 ****************************************************************************/
44 NTSTATUS samba3_read_grouptdb(const char *file, TALLOC_CTX *ctx, struct samba3_groupdb *db)
47 TDB_DATA kbuf, dbuf, newkey;
51 tdb = tdb_open(file, 0, TDB_DEFAULT, O_RDONLY, 0600);
53 DEBUG(0,("Failed to open group mapping database\n"));
54 return NT_STATUS_UNSUCCESSFUL;
57 /* Cope with byte-reversed older versions of the db. */
58 vers_id = tdb_fetch_int32(tdb, "INFO/version");
59 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
60 /* Written on a bigendian machine with old fetch_int code. Save as le. */
61 vers_id = DATABASE_VERSION_V2;
64 if (vers_id != DATABASE_VERSION_V2) {
65 DEBUG(0, ("Group database version mismatch: %d\n", vers_id));
66 return NT_STATUS_UNSUCCESSFUL;
69 db->groupmappings = NULL;
70 db->groupmap_count = 0;
74 for (kbuf = tdb_firstkey(tdb);
76 newkey = tdb_nextkey(tdb, kbuf), free(kbuf.dptr), kbuf=newkey) {
77 struct samba3_groupmapping map;
78 const char *k = (const char *)kbuf.dptr;
80 if (strncmp(k, GROUP_PREFIX, strlen(GROUP_PREFIX)) == 0)
82 dbuf = tdb_fetch(tdb, kbuf);
88 map.sid = dom_sid_parse_talloc(ctx, k+strlen(GROUP_PREFIX));
90 ret = tdb_unpack(tdb, (char *)dbuf.dptr, dbuf.dsize, "dd",
91 &map.gid, &map.sid_name_use);
94 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
98 map.nt_name = talloc_strdup(ctx, (const char *)(dbuf.dptr+ret));
99 map.comment = talloc_strdup(ctx, (const char *)(dbuf.dptr+ret+strlen(map.nt_name)));
101 db->groupmappings = talloc_realloc(ctx, db->groupmappings, struct samba3_groupmapping, db->groupmap_count+1);
103 if (!db->groupmappings)
104 return NT_STATUS_NO_MEMORY;
106 db->groupmappings[db->groupmap_count] = map;
108 db->groupmap_count++;
109 } else if (strncmp(k, MEMBEROF_PREFIX, strlen(MEMBEROF_PREFIX)) == 0)
111 struct samba3_alias alias;
112 const char **member_strlist;
115 dbuf = tdb_fetch(tdb, kbuf);
119 alias.sid = dom_sid_parse_talloc(ctx, k+strlen(MEMBEROF_PREFIX));
120 alias.member_count = 0;
121 alias.members = NULL;
123 member_strlist = str_list_make_shell(ctx, (const char *)dbuf.dptr, " ");
125 for (i = 0; member_strlist[i]; i++) {
126 alias.members = talloc_realloc(ctx, alias.members, struct dom_sid *, alias.member_count+1);
127 alias.members[alias.member_count] = dom_sid_parse_talloc(ctx, member_strlist[i]);
128 alias.member_count++;
131 talloc_free(member_strlist);
133 db->aliases = talloc_realloc(ctx, db->aliases, struct samba3_alias, db->alias_count+1);
134 db->aliases[db->alias_count] = alias;