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 "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 #define ENUM_ONLY_MAPPED True
41 #define ENUM_ALL_MAPPED False
44 /****************************************************************************
45 dump the mapping group mapping to a text file
46 ****************************************************************************/
47 static const char *decode_sid_name_use(enum SID_NAME_USE name_use)
52 case SID_NAME_DOM_GRP:
53 return "Domain group";
58 case SID_NAME_WKN_GRP:
59 return "Builtin group";
60 case SID_NAME_DELETED:
62 case SID_NAME_INVALID:
64 case SID_NAME_UNKNOWN:
66 return "Unknown type";
70 /****************************************************************************
71 Open the group mapping tdb.
72 ****************************************************************************/
73 static TDB_CONTEXT *tdbgroup_open(const char *file)
77 TDB_CONTEXT *tdb = tdb_open(file, 0, TDB_DEFAULT, O_RDONLY, 0600);
79 DEBUG(0,("Failed to open group mapping database\n"));
83 /* Cope with byte-reversed older versions of the db. */
84 vers_id = tdb_fetch_int32(tdb, "INFO/version");
85 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
86 /* Written on a bigendian machine with old fetch_int code. Save as le. */
87 vers_id = DATABASE_VERSION_V2;
90 if (vers_id != DATABASE_VERSION_V2) {
97 /****************************************************************************
98 Return the sid and the type of the unix group.
99 ****************************************************************************/
101 static BOOL get_group_map_from_sid(TDB_CONTEXT *tdb, struct dom_sid sid, struct samba3_groupmapping *map)
107 /* the key is the SID, retrieving is direct */
109 kbuf.dptr = talloc_asprintf(tdb, "%s%s", GROUP_PREFIX, dom_sid_string(tdb, &sid));
110 kbuf.dsize = strlen(key)+1;
112 dbuf = tdb_fetch(tdb, kbuf);
116 ret = tdb_unpack(tdb, dbuf.dptr, dbuf.dsize, "ddff",
117 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
119 SAFE_FREE(dbuf.dptr);
122 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
126 map->sid = dom_sid_dup(tdb, &sid);
131 /****************************************************************************
132 Return the sid and the type of the unix group.
133 ****************************************************************************/
135 static BOOL get_group_map_from_gid(TDB_CONTEXT *tdb, gid_t gid, struct samba3_groupmapping *map)
137 TDB_DATA kbuf, dbuf, newkey;
140 /* we need to enumerate the TDB to find the GID */
142 for (kbuf = tdb_firstkey(tdb);
144 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
146 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
148 dbuf = tdb_fetch(tdb, kbuf);
153 map->sid = dom_sid_parse_talloc(tdb, kbuf.dptr+strlen(GROUP_PREFIX));
155 ret = tdb_unpack(tdb, dbuf.dptr, dbuf.dsize, "ddff",
156 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
158 SAFE_FREE(dbuf.dptr);
161 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
166 SAFE_FREE(kbuf.dptr);
174 /****************************************************************************
175 Return the sid and the type of the unix group.
176 ****************************************************************************/
178 static BOOL get_group_map_from_ntname(TDB_CONTEXT *tdb, const char *name, struct samba3_groupmapping *map)
180 TDB_DATA kbuf, dbuf, newkey;
183 /* we need to enumerate the TDB to find the name */
185 for (kbuf = tdb_firstkey(tdb);
187 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
189 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
191 dbuf = tdb_fetch(tdb, kbuf);
195 map->sid = dom_sid_parse_talloc(tdb, kbuf.dptr+strlen(GROUP_PREFIX));
197 ret = tdb_unpack(tdb, dbuf.dptr, dbuf.dsize, "ddff",
198 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
200 SAFE_FREE(dbuf.dptr);
203 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
207 if (StrCaseCmp(name, map->nt_name)==0) {
208 SAFE_FREE(kbuf.dptr);
216 /****************************************************************************
217 Enumerate the group mapping.
218 ****************************************************************************/
220 static BOOL enum_group_mapping(TDB_CONTEXT *tdb, enum SID_NAME_USE sid_name_use, struct samba3_groupmapping **rmap,
221 int *num_entries, BOOL unix_only)
223 TDB_DATA kbuf, dbuf, newkey;
224 struct samba3_groupmapping map;
225 struct samba3_groupmapping *mapt;
232 for (kbuf = tdb_firstkey(tdb);
234 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
236 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
239 dbuf = tdb_fetch(tdb, kbuf);
243 map.sid = dom_sid_parse_talloc(tdb, kbuf.dptr+strlen(GROUP_PREFIX));
245 ret = tdb_unpack(tdb, dbuf.dptr, dbuf.dsize, "ddff",
246 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
248 SAFE_FREE(dbuf.dptr);
251 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
255 /* list only the type or everything if UNKNOWN */
256 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
257 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
261 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
262 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
266 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map.nt_name ,decode_sid_name_use(map.sid_name_use)));
268 mapt = talloc_realloc(tdb, *rmap, struct samba3_groupmapping, entries+1);
270 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
277 mapt[entries].gid = map.gid;
278 mapt[entries].sid = dom_sid_dup(tdb, map.sid);
279 mapt[entries].sid_name_use = map.sid_name_use;
280 mapt[entries].nt_name = map.nt_name;
281 mapt[entries].comment = map.comment;
287 *num_entries=entries;
292 /* This operation happens on session setup, so it should better be fast. We
293 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
295 static NTSTATUS one_alias_membership(TDB_CONTEXT *tdb,
296 const struct dom_sid *member, struct dom_sid **sids, int *num)
301 char * key = talloc_asprintf(tdb, "%s%s", MEMBEROF_PREFIX, dom_sid_string(tdb, member));
303 kbuf.dsize = strlen(key)+1;
306 dbuf = tdb_fetch(tdb, kbuf);
308 if (dbuf.dptr == NULL) {
314 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
316 struct dom_sid alias;
318 if (!string_to_sid(&alias, string_sid))
321 add_sid_to_array_unique(NULL, &alias, sids, num);
324 return NT_STATUS_NO_MEMORY;
327 SAFE_FREE(dbuf.dptr);
331 static NTSTATUS alias_memberships(TDB_CONTEXT *tdb, const struct dom_sid *members, int num_members,
332 struct dom_sid **sids, int *num)
339 for (i=0; i<num_members; i++) {
340 NTSTATUS status = one_alias_membership(tdb, &members[i], sids, num);
341 if (!NT_STATUS_IS_OK(status))
347 struct aliasmem_closure {
348 const struct dom_sid *alias;
349 struct dom_sid **sids;
353 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
356 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
358 fstring alias_string;
360 if (strncmp(key.dptr, MEMBEROF_PREFIX,
361 strlen(MEMBEROF_PREFIX)) != 0)
366 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
368 struct dom_sid alias, member;
369 const char *member_string;
372 if (!string_to_sid(&alias, alias_string))
375 if (sid_compare(closure->alias, &alias) != 0)
378 /* Ok, we found the alias we're looking for in the membership
379 * list currently scanned. The key represents the alias
380 * member. Add that. */
382 member_string = strchr(key.dptr, '/');
384 /* Above we tested for MEMBEROF_PREFIX which includes the
387 SMB_ASSERT(member_string != NULL);
390 if (!string_to_sid(&member, member_string))
393 add_sid_to_array(NULL, &member, closure->sids, closure->num);
399 static NTSTATUS enum_aliasmem(TDB_CONTEXT *tdb, const struct dom_sid *alias, struct dom_sid **sids, int *num)
401 struct samba3_groupmapping map;
402 struct aliasmem_closure closure;
404 if (!get_group_map_from_sid(*alias, &map))
405 return NT_STATUS_NO_SUCH_ALIAS;
407 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
408 (map.sid_name_use != SID_NAME_WKN_GRP) )
409 return NT_STATUS_NO_SUCH_ALIAS;
414 closure.alias = alias;
418 tdb_traverse(tdb, collect_aliasmem, &closure);
424 * High level functions
425 * better to use them than the lower ones.
427 * we are checking if the group is in the mapping file
428 * and if the group is an existing unix group
432 /* get a domain group from it's SID */
434 /* get a local (alias) group from it's SID */
436 static BOOL get_local_group_from_sid(TDB_CONTEXT *tdb, struct dom_sid *sid, struct samba3_groupmapping *map)
440 /* The group is in the mapping table */
441 ret = pdb_getgrsid(map, *sid);
446 if ( ( (map->sid_name_use != SID_NAME_ALIAS) &&
447 (map->sid_name_use != SID_NAME_WKN_GRP) )
449 || (getgrgid(map->gid) == NULL) )
455 /* local groups only exist in the group mapping DB so this
459 /* the group isn't in the mapping table.
460 * make one based on the unix information */
464 sid_peek_rid(sid, &alias_rid);
465 map->gid=pdb_group_rid_to_gid(alias_rid);
467 grp = getgrgid(map->gid);
469 DEBUG(3,("get_local_group_from_sid: No unix group for [%ul]\n", map->gid));
473 map->sid_name_use=SID_NAME_ALIAS;
475 fstrcpy(map->nt_name, grp->gr_name);
476 fstrcpy(map->comment, "Local Unix Group");
478 sid_copy(&map->sid, sid);