2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2006,
5 * Copyright (C) Jean François Micouleau 1998-2001.
6 * Copyright (C) Volker Lendecke 2006.
7 * Copyright (C) Gerald Carter 2006.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "groupdb/mapping.h"
27 static TDB_CONTEXT *tdb; /* used for driver files */
29 /****************************************************************************
30 Open the group mapping tdb.
31 ****************************************************************************/
32 BOOL init_group_mapping(void)
34 const char *vstring = "INFO/version";
36 GROUP_MAP *map_table = NULL;
37 size_t num_entries = 0;
42 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
44 DEBUG(0,("Failed to open group mapping database\n"));
48 /* handle a Samba upgrade */
49 tdb_lock_bystring(tdb, vstring);
51 /* Cope with byte-reversed older versions of the db. */
52 vers_id = tdb_fetch_int32(tdb, vstring);
53 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
54 /* Written on a bigendian machine with old fetch_int code. Save as le. */
55 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
56 vers_id = DATABASE_VERSION_V2;
59 /* if its an unknown version we remove everthing in the db */
61 if (vers_id != DATABASE_VERSION_V2) {
62 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
63 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
66 tdb_unlock_bystring(tdb, vstring);
68 /* cleanup any map entries with a gid == -1 */
70 if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table, &num_entries, False ) ) {
73 for ( i=0; i<num_entries; i++ ) {
74 if ( map_table[i].gid == -1 ) {
75 group_map_remove( &map_table[i].sid );
79 SAFE_FREE( map_table );
86 /****************************************************************************
87 ****************************************************************************/
88 BOOL add_mapping_entry(GROUP_MAP *map, int flag)
92 fstring string_sid="";
95 if(!init_group_mapping()) {
96 DEBUG(0,("failed to initialize group mapping\n"));
100 sid_to_string(string_sid, &map->sid);
102 len = tdb_pack(buf, sizeof(buf), "ddff",
103 map->gid, map->sid_name_use, map->nt_name, map->comment);
105 if (len > sizeof(buf))
108 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
112 if (tdb_store_bystring(tdb, key, dbuf, flag) != 0) return False;
118 /****************************************************************************
119 Return the sid and the type of the unix group.
120 ****************************************************************************/
122 BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
129 if(!init_group_mapping()) {
130 DEBUG(0,("failed to initialize group mapping\n"));
134 /* the key is the SID, retrieving is direct */
136 sid_to_string(string_sid, &sid);
137 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
139 dbuf = tdb_fetch_bystring(tdb, key);
143 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
144 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
146 SAFE_FREE(dbuf.dptr);
149 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
153 sid_copy(&map->sid, &sid);
158 /****************************************************************************
159 Return the sid and the type of the unix group.
160 ****************************************************************************/
162 BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
164 TDB_DATA kbuf, dbuf, newkey;
168 if(!init_group_mapping()) {
169 DEBUG(0,("failed to initialize group mapping\n"));
173 /* we need to enumerate the TDB to find the GID */
175 for (kbuf = tdb_firstkey(tdb);
177 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
179 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
181 dbuf = tdb_fetch(tdb, kbuf);
185 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
187 string_to_sid(&map->sid, string_sid);
189 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
190 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
192 SAFE_FREE(dbuf.dptr);
195 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
200 SAFE_FREE(kbuf.dptr);
208 /****************************************************************************
209 Return the sid and the type of the unix group.
210 ****************************************************************************/
212 BOOL get_group_map_from_ntname(const char *name, GROUP_MAP *map)
214 TDB_DATA kbuf, dbuf, newkey;
218 if(!init_group_mapping()) {
219 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping\n"));
223 /* we need to enumerate the TDB to find the name */
225 for (kbuf = tdb_firstkey(tdb);
227 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
229 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
231 dbuf = tdb_fetch(tdb, kbuf);
235 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
237 string_to_sid(&map->sid, string_sid);
239 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
240 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
242 SAFE_FREE(dbuf.dptr);
245 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
249 if ( strequal(name, map->nt_name) ) {
250 SAFE_FREE(kbuf.dptr);
258 /****************************************************************************
259 Remove a group mapping entry.
260 ****************************************************************************/
262 BOOL group_map_remove(const DOM_SID *sid)
268 if(!init_group_mapping()) {
269 DEBUG(0,("failed to initialize group mapping\n"));
273 /* the key is the SID, retrieving is direct */
275 sid_to_string(string_sid, sid);
276 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
278 dbuf = tdb_fetch_bystring(tdb, key);
282 SAFE_FREE(dbuf.dptr);
284 if(tdb_delete_bystring(tdb, key) != TDB_SUCCESS)
290 /****************************************************************************
291 Enumerate the group mapping.
292 ****************************************************************************/
294 BOOL enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
295 size_t *p_num_entries, BOOL unix_only)
297 TDB_DATA kbuf, dbuf, newkey;
306 if(!init_group_mapping()) {
307 DEBUG(0,("failed to initialize group mapping\n"));
314 for (kbuf = tdb_firstkey(tdb);
316 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
318 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
321 dbuf = tdb_fetch(tdb, kbuf);
325 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
327 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
328 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
330 SAFE_FREE(dbuf.dptr);
333 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
337 /* list only the type or everything if UNKNOWN */
338 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
339 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
343 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
344 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
348 string_to_sid(&grpsid, string_sid);
349 sid_copy( &map.sid, &grpsid );
351 sid_split_rid( &grpsid, &rid );
353 /* Only check the domain if we were given one */
355 if ( domsid && !sid_equal( domsid, &grpsid ) ) {
356 DEBUG(11,("enum_group_mapping: group %s is not in domain %s\n",
357 string_sid, sid_string_static(domsid)));
361 DEBUG(11,("enum_group_mapping: returning group %s of "
362 "type %s\n", map.nt_name,
363 sid_type_lookup(map.sid_name_use)));
365 (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
367 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
373 mapt[entries].gid = map.gid;
374 sid_copy( &mapt[entries].sid, &map.sid);
375 mapt[entries].sid_name_use = map.sid_name_use;
376 fstrcpy(mapt[entries].nt_name, map.nt_name);
377 fstrcpy(mapt[entries].comment, map.comment);
383 *p_num_entries=entries;
388 /* This operation happens on session setup, so it should better be fast. We
389 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
391 NTSTATUS one_alias_membership(const DOM_SID *member,
392 DOM_SID **sids, size_t *num)
394 fstring key, string_sid;
398 if (!init_group_mapping()) {
399 DEBUG(0,("failed to initialize group mapping\n"));
400 return NT_STATUS_ACCESS_DENIED;
403 sid_to_string(string_sid, member);
404 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
406 dbuf = tdb_fetch_bystring(tdb, key);
408 if (dbuf.dptr == NULL) {
414 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
418 if (!string_to_sid(&alias, string_sid))
421 if (!add_sid_to_array_unique(NULL, &alias, sids, num)) {
422 return NT_STATUS_NO_MEMORY;
426 SAFE_FREE(dbuf.dptr);
430 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
431 DOM_SID **sids, size_t *num)
438 for (i=0; i<num_members; i++) {
439 NTSTATUS status = one_alias_membership(&members[i], sids, num);
440 if (!NT_STATUS_IS_OK(status))
446 static BOOL is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
451 /* This feels the wrong way round, but the on-disk data structure
452 * dictates it this way. */
453 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
456 for (i=0; i<num; i++) {
457 if (sid_compare(alias, &sids[i]) == 0) {
467 NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
473 char *new_memberstring;
476 if(!init_group_mapping()) {
477 DEBUG(0,("failed to initialize group mapping\n"));
478 return NT_STATUS_ACCESS_DENIED;
481 if (!get_group_map_from_sid(*alias, &map))
482 return NT_STATUS_NO_SUCH_ALIAS;
484 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
485 (map.sid_name_use != SID_NAME_WKN_GRP) )
486 return NT_STATUS_NO_SUCH_ALIAS;
488 if (is_aliasmem(alias, member))
489 return NT_STATUS_MEMBER_IN_ALIAS;
491 sid_to_string(string_sid, member);
492 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
494 dbuf = tdb_fetch_bystring(tdb, key);
496 sid_to_string(string_sid, alias);
498 if (dbuf.dptr != NULL) {
499 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
502 new_memberstring = SMB_STRDUP(string_sid);
505 if (new_memberstring == NULL)
506 return NT_STATUS_NO_MEMORY;
508 SAFE_FREE(dbuf.dptr);
509 dbuf = string_term_tdb_data(new_memberstring);
511 result = tdb_store_bystring(tdb, key, dbuf, 0);
513 SAFE_FREE(new_memberstring);
515 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
518 struct aliasmem_closure {
519 const DOM_SID *alias;
524 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
527 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
529 fstring alias_string;
531 if (strncmp(key.dptr, MEMBEROF_PREFIX,
532 strlen(MEMBEROF_PREFIX)) != 0)
537 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
539 DOM_SID alias, member;
540 const char *member_string;
543 if (!string_to_sid(&alias, alias_string))
546 if (sid_compare(closure->alias, &alias) != 0)
549 /* Ok, we found the alias we're looking for in the membership
550 * list currently scanned. The key represents the alias
551 * member. Add that. */
553 member_string = strchr(key.dptr, '/');
555 /* Above we tested for MEMBEROF_PREFIX which includes the
558 SMB_ASSERT(member_string != NULL);
561 if (!string_to_sid(&member, member_string))
564 if (!add_sid_to_array(NULL, &member, closure->sids, closure->num)) {
573 NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
576 struct aliasmem_closure closure;
578 if(!init_group_mapping()) {
579 DEBUG(0,("failed to initialize group mapping\n"));
580 return NT_STATUS_ACCESS_DENIED;
583 if (!get_group_map_from_sid(*alias, &map))
584 return NT_STATUS_NO_SUCH_ALIAS;
586 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
587 (map.sid_name_use != SID_NAME_WKN_GRP) )
588 return NT_STATUS_NO_SUCH_ALIAS;
593 closure.alias = alias;
597 tdb_traverse(tdb, collect_aliasmem, &closure);
601 NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
612 result = alias_memberships(member, 1, &sids, &num);
614 if (!NT_STATUS_IS_OK(result))
617 for (i=0; i<num; i++) {
618 if (sid_compare(&sids[i], alias) == 0) {
626 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
630 sids[i] = sids[num-1];
634 sid_to_string(sid_string, member);
635 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, sid_string);
638 return tdb_delete_bystring(tdb, key) == 0 ?
639 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
641 member_string = SMB_STRDUP("");
643 if (member_string == NULL) {
645 return NT_STATUS_NO_MEMORY;
648 for (i=0; i<num; i++) {
649 char *s = member_string;
651 sid_to_string(sid_string, &sids[i]);
652 asprintf(&member_string, "%s %s", s, sid_string);
655 if (member_string == NULL) {
657 return NT_STATUS_NO_MEMORY;
661 dbuf = string_term_tdb_data(member_string);
663 result = tdb_store_bystring(tdb, key, dbuf, 0) == 0 ?
664 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
667 SAFE_FREE(member_string);