r247: Fixup a couple of debug messages.
[Samba/gebeck_regimport.git] / source3 / groupdb / mapping.c
blob548651dfd538c6b1453c887f63525d543e8a56f5
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"
24 static TDB_CONTEXT *tdb; /* used for driver files */
26 #define DATABASE_VERSION_V1 1 /* native byte format. */
27 #define DATABASE_VERSION_V2 2 /* le format. */
29 #define GROUP_PREFIX "UNIXGROUP/"
31 /* Alias memberships are stored reverse, as memberships. The performance
32 * critical operation is to determine the aliases a SID is member of, not
33 * listing alias members. So we store a list of alias SIDs a SID is member of
34 * hanging of the member as key.
36 #define MEMBEROF_PREFIX "MEMBEROF/"
38 PRIVS privs[] = {
39 {SE_PRIV_NONE, "no_privs", "No privilege" }, /* this one MUST be first */
40 {SE_PRIV_ADD_MACHINES, "SeMachineAccountPrivilege", "Add workstations to the domain" },
41 {SE_PRIV_SEC_PRIV, "SeSecurityPrivilege", "Manage the audit logs" },
42 {SE_PRIV_TAKE_OWNER, "SeTakeOwnershipPrivilege", "Take ownership of file" },
43 {SE_PRIV_ADD_USERS, "SaAddUsers", "Add users to the domain - Samba" },
44 {SE_PRIV_PRINT_OPERATOR, "SaPrintOp", "Add or remove printers - Samba" },
45 {SE_PRIV_ALL, "SaAllPrivs", "all privileges" }
49 /****************************************************************************
50 dump the mapping group mapping to a text file
51 ****************************************************************************/
52 char *decode_sid_name_use(fstring group, enum SID_NAME_USE name_use)
54 static fstring group_type;
56 switch(name_use) {
57 case SID_NAME_USER:
58 fstrcpy(group_type,"User");
59 break;
60 case SID_NAME_DOM_GRP:
61 fstrcpy(group_type,"Domain group");
62 break;
63 case SID_NAME_DOMAIN:
64 fstrcpy(group_type,"Domain");
65 break;
66 case SID_NAME_ALIAS:
67 fstrcpy(group_type,"Local group");
68 break;
69 case SID_NAME_WKN_GRP:
70 fstrcpy(group_type,"Builtin group");
71 break;
72 case SID_NAME_DELETED:
73 fstrcpy(group_type,"Deleted");
74 break;
75 case SID_NAME_INVALID:
76 fstrcpy(group_type,"Invalid");
77 break;
78 case SID_NAME_UNKNOWN:
79 default:
80 fstrcpy(group_type,"Unknown type");
81 break;
84 fstrcpy(group, group_type);
85 return group_type;
88 /****************************************************************************
89 initialise first time the mapping list - called from init_group_mapping()
90 ****************************************************************************/
91 static BOOL default_group_mapping(void)
93 DOM_SID sid_admins;
94 DOM_SID sid_users;
95 DOM_SID sid_guests;
96 fstring str_admins;
97 fstring str_users;
98 fstring str_guests;
100 /* Add the Wellknown groups */
102 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP, "Administrators", "");
103 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP, "Users", "");
104 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP, "Guests", "");
105 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP, "Power Users", "");
106 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP, "Account Operators", "");
107 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP, "System Operators", "");
108 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP, "Print Operators", "");
109 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP, "Backup Operators", "");
110 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP, "Replicators", "");
112 /* Add the defaults domain groups */
114 sid_copy(&sid_admins, get_global_sam_sid());
115 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
116 sid_to_string(str_admins, &sid_admins);
117 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "");
119 sid_copy(&sid_users, get_global_sam_sid());
120 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
121 sid_to_string(str_users, &sid_users);
122 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "");
124 sid_copy(&sid_guests, get_global_sam_sid());
125 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
126 sid_to_string(str_guests, &sid_guests);
127 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "");
129 return True;
132 /****************************************************************************
133 Open the group mapping tdb.
134 ****************************************************************************/
136 static BOOL init_group_mapping(void)
138 static pid_t local_pid;
139 const char *vstring = "INFO/version";
140 int32 vers_id;
142 if (tdb && local_pid == sys_getpid())
143 return True;
144 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
145 if (!tdb) {
146 DEBUG(0,("Failed to open group mapping database\n"));
147 return False;
150 local_pid = sys_getpid();
152 /* handle a Samba upgrade */
153 tdb_lock_bystring(tdb, vstring, 0);
155 /* Cope with byte-reversed older versions of the db. */
156 vers_id = tdb_fetch_int32(tdb, vstring);
157 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
158 /* Written on a bigendian machine with old fetch_int code. Save as le. */
159 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
160 vers_id = DATABASE_VERSION_V2;
163 if (vers_id != DATABASE_VERSION_V2) {
164 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
165 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
168 tdb_unlock_bystring(tdb, vstring);
170 /* write a list of default groups */
171 if(!default_group_mapping())
172 return False;
174 return True;
177 /****************************************************************************
178 ****************************************************************************/
179 static BOOL add_mapping_entry(GROUP_MAP *map, int flag)
181 TDB_DATA kbuf, dbuf;
182 pstring key, buf;
183 fstring string_sid="";
184 int len;
186 if(!init_group_mapping()) {
187 DEBUG(0,("failed to initialize group mapping\n"));
188 return(False);
191 sid_to_string(string_sid, &map->sid);
193 len = tdb_pack(buf, sizeof(buf), "ddff",
194 map->gid, map->sid_name_use, map->nt_name, map->comment);
196 if (len > sizeof(buf))
197 return False;
199 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
201 kbuf.dsize = strlen(key)+1;
202 kbuf.dptr = key;
203 dbuf.dsize = len;
204 dbuf.dptr = buf;
205 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
207 return True;
210 /****************************************************************************
211 initialise first time the mapping list
212 ****************************************************************************/
213 BOOL add_initial_entry(gid_t gid, const char *sid, enum SID_NAME_USE sid_name_use, const char *nt_name, const char *comment)
215 GROUP_MAP map;
217 if(!init_group_mapping()) {
218 DEBUG(0,("failed to initialize group mapping\n"));
219 return(False);
222 map.gid=gid;
223 if (!string_to_sid(&map.sid, sid)) {
224 DEBUG(0, ("string_to_sid failed: %s", sid));
225 return False;
228 map.sid_name_use=sid_name_use;
229 fstrcpy(map.nt_name, nt_name);
230 fstrcpy(map.comment, comment);
232 return pdb_add_group_mapping_entry(&map);
235 /****************************************************************************
236 Return the sid and the type of the unix group.
237 ****************************************************************************/
239 static BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
241 TDB_DATA kbuf, dbuf;
242 pstring key;
243 fstring string_sid;
244 int ret = 0;
246 if(!init_group_mapping()) {
247 DEBUG(0,("failed to initialize group mapping\n"));
248 return(False);
251 /* the key is the SID, retrieving is direct */
253 sid_to_string(string_sid, &sid);
254 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
256 kbuf.dptr = key;
257 kbuf.dsize = strlen(key)+1;
259 dbuf = tdb_fetch(tdb, kbuf);
260 if (!dbuf.dptr)
261 return False;
263 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
264 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
266 SAFE_FREE(dbuf.dptr);
268 if ( ret == -1 ) {
269 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
270 return False;
273 sid_copy(&map->sid, &sid);
275 return True;
278 /****************************************************************************
279 Return the sid and the type of the unix group.
280 ****************************************************************************/
282 static BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
284 TDB_DATA kbuf, dbuf, newkey;
285 fstring string_sid;
286 int ret;
288 if(!init_group_mapping()) {
289 DEBUG(0,("failed to initialize group mapping\n"));
290 return(False);
293 /* we need to enumerate the TDB to find the GID */
295 for (kbuf = tdb_firstkey(tdb);
296 kbuf.dptr;
297 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
299 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
301 dbuf = tdb_fetch(tdb, kbuf);
302 if (!dbuf.dptr)
303 continue;
305 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
307 string_to_sid(&map->sid, string_sid);
309 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
310 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
312 SAFE_FREE(dbuf.dptr);
314 if ( ret == -1 ) {
315 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
316 return False;
319 if (gid==map->gid) {
320 SAFE_FREE(kbuf.dptr);
321 return True;
325 return False;
328 /****************************************************************************
329 Return the sid and the type of the unix group.
330 ****************************************************************************/
332 static BOOL get_group_map_from_ntname(const char *name, GROUP_MAP *map)
334 TDB_DATA kbuf, dbuf, newkey;
335 fstring string_sid;
336 int ret;
338 if(!init_group_mapping()) {
339 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping\n"));
340 return(False);
343 /* we need to enumerate the TDB to find the name */
345 for (kbuf = tdb_firstkey(tdb);
346 kbuf.dptr;
347 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
349 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
351 dbuf = tdb_fetch(tdb, kbuf);
352 if (!dbuf.dptr)
353 continue;
355 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
357 string_to_sid(&map->sid, string_sid);
359 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
360 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
362 SAFE_FREE(dbuf.dptr);
364 if ( ret == -1 ) {
365 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
366 return False;
369 if (StrCaseCmp(name, map->nt_name)==0) {
370 SAFE_FREE(kbuf.dptr);
371 return True;
375 return False;
378 /****************************************************************************
379 Remove a group mapping entry.
380 ****************************************************************************/
382 static BOOL group_map_remove(DOM_SID sid)
384 TDB_DATA kbuf, dbuf;
385 pstring key;
386 fstring string_sid;
388 if(!init_group_mapping()) {
389 DEBUG(0,("failed to initialize group mapping\n"));
390 return(False);
393 /* the key is the SID, retrieving is direct */
395 sid_to_string(string_sid, &sid);
396 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
398 kbuf.dptr = key;
399 kbuf.dsize = strlen(key)+1;
401 dbuf = tdb_fetch(tdb, kbuf);
402 if (!dbuf.dptr)
403 return False;
405 SAFE_FREE(dbuf.dptr);
407 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
408 return False;
410 return True;
413 /****************************************************************************
414 Enumerate the group mapping.
415 ****************************************************************************/
417 static BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
418 int *num_entries, BOOL unix_only)
420 TDB_DATA kbuf, dbuf, newkey;
421 fstring string_sid;
422 fstring group_type;
423 GROUP_MAP map;
424 GROUP_MAP *mapt;
425 int ret;
426 int entries=0;
428 if(!init_group_mapping()) {
429 DEBUG(0,("failed to initialize group mapping\n"));
430 return(False);
433 *num_entries=0;
434 *rmap=NULL;
436 for (kbuf = tdb_firstkey(tdb);
437 kbuf.dptr;
438 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
440 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
441 continue;
443 dbuf = tdb_fetch(tdb, kbuf);
444 if (!dbuf.dptr)
445 continue;
447 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
449 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
450 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
452 SAFE_FREE(dbuf.dptr);
454 if ( ret == -1 ) {
455 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
456 continue;
459 /* list only the type or everything if UNKNOWN */
460 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
461 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
462 continue;
465 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
466 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
467 continue;
470 string_to_sid(&map.sid, string_sid);
472 decode_sid_name_use(group_type, map.sid_name_use);
473 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map.nt_name ,group_type));
475 mapt=(GROUP_MAP *)Realloc((*rmap), (entries+1)*sizeof(GROUP_MAP));
476 if (!mapt) {
477 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
478 SAFE_FREE(*rmap);
479 return False;
481 else
482 (*rmap) = mapt;
484 mapt[entries].gid = map.gid;
485 sid_copy( &mapt[entries].sid, &map.sid);
486 mapt[entries].sid_name_use = map.sid_name_use;
487 fstrcpy(mapt[entries].nt_name, map.nt_name);
488 fstrcpy(mapt[entries].comment, map.comment);
490 entries++;
494 *num_entries=entries;
496 return True;
499 /* This operation happens on session setup, so it should better be fast. We
500 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
502 static NTSTATUS alias_memberships(const DOM_SID *sid, DOM_SID **sids, int *num)
504 fstring key, string_sid;
505 TDB_DATA kbuf, dbuf;
506 const char *p;
508 *num = 0;
509 *sids = NULL;
511 if (!init_group_mapping()) {
512 DEBUG(0,("failed to initialize group mapping\n"));
513 return NT_STATUS_ACCESS_DENIED;
516 sid_to_string(string_sid, sid);
517 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
519 kbuf.dsize = strlen(key)+1;
520 kbuf.dptr = key;
522 dbuf = tdb_fetch(tdb, kbuf);
524 if (dbuf.dptr == NULL) {
525 return NT_STATUS_OK;
528 p = dbuf.dptr;
530 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
532 DOM_SID alias;
534 if (!string_to_sid(&alias, string_sid))
535 continue;
537 add_sid_to_array(&alias, sids, num);
539 if (sids == NULL)
540 return NT_STATUS_NO_MEMORY;
543 SAFE_FREE(dbuf.dptr);
544 return NT_STATUS_OK;
547 static BOOL is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
549 DOM_SID *sids;
550 int i, num;
552 /* This feels the wrong way round, but the on-disk data structure
553 * dictates it this way. */
554 if (!NT_STATUS_IS_OK(alias_memberships(member, &sids, &num)))
555 return False;
557 for (i=0; i<num; i++) {
558 if (sid_compare(alias, &sids[i]) == 0) {
559 SAFE_FREE(sids);
560 return True;
563 SAFE_FREE(sids);
564 return False;
567 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
569 GROUP_MAP map;
570 TDB_DATA kbuf, dbuf;
571 pstring key;
572 fstring string_sid;
573 char *new_memberstring;
574 int result;
576 if(!init_group_mapping()) {
577 DEBUG(0,("failed to initialize group mapping\n"));
578 return NT_STATUS_ACCESS_DENIED;
581 if (!get_group_map_from_sid(*alias, &map))
582 return NT_STATUS_NO_SUCH_ALIAS;
584 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
585 (map.sid_name_use != SID_NAME_WKN_GRP) )
586 return NT_STATUS_NO_SUCH_ALIAS;
588 if (is_aliasmem(alias, member))
589 return NT_STATUS_MEMBER_IN_ALIAS;
591 sid_to_string(string_sid, member);
592 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
594 kbuf.dsize = strlen(key)+1;
595 kbuf.dptr = key;
597 dbuf = tdb_fetch(tdb, kbuf);
599 sid_to_string(string_sid, alias);
601 if (dbuf.dptr != NULL) {
602 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
603 string_sid);
604 } else {
605 new_memberstring = strdup(string_sid);
608 if (new_memberstring == NULL)
609 return NT_STATUS_NO_MEMORY;
611 SAFE_FREE(dbuf.dptr);
612 dbuf.dsize = strlen(new_memberstring)+1;
613 dbuf.dptr = new_memberstring;
615 result = tdb_store(tdb, kbuf, dbuf, 0);
617 SAFE_FREE(new_memberstring);
619 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
622 struct aliasmem_closure {
623 const DOM_SID *alias;
624 DOM_SID **sids;
625 int *num;
628 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
629 void *state)
631 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
632 const char *p;
633 fstring alias_string;
635 if (strncmp(key.dptr, MEMBEROF_PREFIX,
636 strlen(MEMBEROF_PREFIX)) != 0)
637 return 0;
639 p = data.dptr;
641 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
643 DOM_SID alias, member;
644 const char *member_string;
647 if (!string_to_sid(&alias, alias_string))
648 continue;
650 if (sid_compare(closure->alias, &alias) != 0)
651 continue;
653 /* Ok, we found the alias we're looking for in the membership
654 * list currently scanned. The key represents the alias
655 * member. Add that. */
657 member_string = strchr(key.dptr, '/');
659 /* Above we tested for MEMBEROF_PREFIX which includes the
660 * slash. */
662 SMB_ASSERT(member_string != NULL);
663 member_string += 1;
665 if (!string_to_sid(&member, member_string))
666 continue;
668 add_sid_to_array(&member, closure->sids, closure->num);
671 return 0;
674 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, int *num)
676 GROUP_MAP map;
677 struct aliasmem_closure closure;
679 if(!init_group_mapping()) {
680 DEBUG(0,("failed to initialize group mapping\n"));
681 return NT_STATUS_ACCESS_DENIED;
684 if (!get_group_map_from_sid(*alias, &map))
685 return NT_STATUS_NO_SUCH_ALIAS;
687 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
688 (map.sid_name_use != SID_NAME_WKN_GRP) )
689 return NT_STATUS_NO_SUCH_ALIAS;
691 *sids = NULL;
692 *num = 0;
694 closure.alias = alias;
695 closure.sids = sids;
696 closure.num = num;
698 tdb_traverse(tdb, collect_aliasmem, &closure);
699 return NT_STATUS_OK;
702 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
704 NTSTATUS result;
705 DOM_SID *sids;
706 int i, num;
707 BOOL found = False;
708 char *member_string;
709 TDB_DATA kbuf, dbuf;
710 pstring key;
711 fstring sid_string;
713 result = alias_memberships(member, &sids, &num);
715 if (!NT_STATUS_IS_OK(result))
716 return result;
718 for (i=0; i<num; i++) {
719 if (sid_compare(&sids[i], alias) == 0) {
720 found = True;
721 break;
725 if (!found) {
726 SAFE_FREE(sids);
727 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
730 if (i < num)
731 sids[i] = sids[num-1];
733 num -= 1;
735 sid_to_string(sid_string, member);
736 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, sid_string);
738 kbuf.dsize = strlen(key)+1;
739 kbuf.dptr = key;
741 if (num == 0)
742 return tdb_delete(tdb, kbuf) == 0 ?
743 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
745 member_string = strdup("");
747 if (member_string == NULL) {
748 SAFE_FREE(sids);
749 return NT_STATUS_NO_MEMORY;
752 for (i=0; i<num; i++) {
753 char *s = member_string;
755 sid_to_string(sid_string, &sids[i]);
756 asprintf(&member_string, "%s %s", s, sid_string);
758 SAFE_FREE(s);
759 if (member_string == NULL) {
760 SAFE_FREE(sids);
761 return NT_STATUS_NO_MEMORY;
765 dbuf.dsize = strlen(member_string)+1;
766 dbuf.dptr = member_string;
768 result = tdb_store(tdb, kbuf, dbuf, 0) == 0 ?
769 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
771 SAFE_FREE(sids);
772 SAFE_FREE(member_string);
774 return result;
779 * High level functions
780 * better to use them than the lower ones.
782 * we are checking if the group is in the mapping file
783 * and if the group is an existing unix group
787 /* get a domain group from it's SID */
789 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
791 struct group *grp;
792 BOOL ret;
794 if(!init_group_mapping()) {
795 DEBUG(0,("failed to initialize group mapping\n"));
796 return(False);
799 DEBUG(10, ("get_domain_group_from_sid\n"));
801 /* if the group is NOT in the database, it CAN NOT be a domain group */
803 become_root();
804 ret = pdb_getgrsid(map, sid);
805 unbecome_root();
807 if ( !ret )
808 return False;
810 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
812 /* if it's not a domain group, continue */
813 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
814 return False;
817 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
819 if (map->gid==-1) {
820 return False;
823 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
825 grp = getgrgid(map->gid);
826 if ( !grp ) {
827 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
828 return False;
831 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
833 return True;
837 /* get a local (alias) group from it's SID */
839 BOOL get_local_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
841 BOOL ret;
843 if(!init_group_mapping()) {
844 DEBUG(0,("failed to initialize group mapping\n"));
845 return(False);
848 /* The group is in the mapping table */
849 become_root();
850 ret = pdb_getgrsid(map, *sid);
851 unbecome_root();
853 if ( !ret )
854 return False;
856 if ( ( (map->sid_name_use != SID_NAME_ALIAS) &&
857 (map->sid_name_use != SID_NAME_WKN_GRP) )
858 || (map->gid == -1)
859 || (getgrgid(map->gid) == NULL) )
861 return False;
864 #if 1 /* JERRY */
865 /* local groups only exist in the group mapping DB so this
866 is not necessary */
868 else {
869 /* the group isn't in the mapping table.
870 * make one based on the unix information */
871 uint32 alias_rid;
872 struct group *grp;
874 sid_peek_rid(sid, &alias_rid);
875 map->gid=pdb_group_rid_to_gid(alias_rid);
877 grp = getgrgid(map->gid);
878 if ( !grp ) {
879 DEBUG(3,("get_local_group_from_sid: No unix group for [%ul]\n", map->gid));
880 return False;
883 map->sid_name_use=SID_NAME_ALIAS;
885 fstrcpy(map->nt_name, grp->gr_name);
886 fstrcpy(map->comment, "Local Unix Group");
888 sid_copy(&map->sid, sid);
890 #endif
892 return True;
895 /* get a builtin group from it's SID */
897 BOOL get_builtin_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
899 struct group *grp;
900 BOOL ret;
903 if(!init_group_mapping()) {
904 DEBUG(0,("failed to initialize group mapping\n"));
905 return(False);
908 become_root();
909 ret = pdb_getgrsid(map, *sid);
910 unbecome_root();
912 if ( !ret )
913 return False;
915 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
916 return False;
919 if (map->gid==-1) {
920 return False;
923 if ( (grp=getgrgid(map->gid)) == NULL) {
924 return False;
927 return True;
932 /****************************************************************************
933 Returns a GROUP_MAP struct based on the gid.
934 ****************************************************************************/
935 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map)
937 struct group *grp;
938 BOOL ret;
940 if(!init_group_mapping()) {
941 DEBUG(0,("failed to initialize group mapping\n"));
942 return(False);
945 if ( (grp=getgrgid(gid)) == NULL)
946 return False;
949 * make a group map from scratch if doesn't exist.
952 become_root();
953 ret = pdb_getgrgid(map, gid);
954 unbecome_root();
956 if ( !ret ) {
957 map->gid=gid;
958 map->sid_name_use=SID_NAME_ALIAS;
960 /* interim solution until we have a last RID allocated */
962 sid_copy(&map->sid, get_global_sam_sid());
963 sid_append_rid(&map->sid, pdb_gid_to_group_rid(gid));
965 fstrcpy(map->nt_name, grp->gr_name);
966 fstrcpy(map->comment, "Local Unix Group");
969 return True;
975 /****************************************************************************
976 Get the member users of a group and
977 all the users who have that group as primary.
979 give back an array of SIDS
980 return the grand number of users
983 TODO: sort the list and remove duplicate. JFM.
985 ****************************************************************************/
987 BOOL get_sid_list_of_group(gid_t gid, DOM_SID **sids, int *num_sids)
989 struct group *grp;
990 int i=0;
991 char *gr;
992 DOM_SID *s;
994 struct sys_pwent *userlist;
995 struct sys_pwent *user;
997 if(!init_group_mapping()) {
998 DEBUG(0,("failed to initialize group mapping\n"));
999 return(False);
1002 *num_sids = 0;
1003 *sids=NULL;
1005 if ( (grp=getgrgid(gid)) == NULL)
1006 return False;
1008 gr = grp->gr_mem[0];
1009 DEBUG(10, ("getting members\n"));
1011 while (gr && (*gr != (char)'\0')) {
1012 SAM_ACCOUNT *group_member_acct = NULL;
1013 BOOL found_user;
1014 s = Realloc((*sids), sizeof(**sids)*(*num_sids+1));
1015 if (!s) {
1016 DEBUG(0,("get_uid_list_of_group: unable to enlarge SID list!\n"));
1017 return False;
1019 else (*sids) = s;
1021 if (!NT_STATUS_IS_OK(pdb_init_sam(&group_member_acct))) {
1022 continue;
1025 become_root();
1026 found_user = pdb_getsampwnam(group_member_acct, gr);
1027 unbecome_root();
1029 if (found_user) {
1030 sid_copy(&(*sids)[*num_sids], pdb_get_user_sid(group_member_acct));
1031 (*num_sids)++;
1034 pdb_free_sam(&group_member_acct);
1036 gr = grp->gr_mem[++i];
1038 DEBUG(10, ("got [%d] members\n", *num_sids));
1040 winbind_off();
1042 user = userlist = getpwent_list();
1044 while (user != NULL) {
1046 SAM_ACCOUNT *group_member_acct = NULL;
1047 BOOL found_user;
1049 if (user->pw_gid != gid) {
1050 user = user->next;
1051 continue;
1054 s = Realloc((*sids), sizeof(**sids)*(*num_sids+1));
1055 if (!s) {
1056 DEBUG(0,("get_sid_list_of_group: unable to enlarge "
1057 "SID list!\n"));
1058 pwent_free(userlist);
1059 winbind_on();
1060 return False;
1062 else (*sids) = s;
1064 if (!NT_STATUS_IS_OK(pdb_init_sam(&group_member_acct))) {
1065 continue;
1068 become_root();
1069 found_user = pdb_getsampwnam(group_member_acct, user->pw_name);
1070 unbecome_root();
1072 if (found_user) {
1073 sid_copy(&(*sids)[*num_sids],
1074 pdb_get_user_sid(group_member_acct));
1075 (*num_sids)++;
1076 } else {
1077 DEBUG(4,("get_sid_list_of_group: User %s [uid == %lu] "
1078 "has no samba account\n",
1079 user->pw_name, (unsigned long)user->pw_uid));
1080 if (algorithmic_uid_to_sid(&(*sids)[*num_sids],
1081 user->pw_uid))
1082 (*num_sids)++;
1084 pdb_free_sam(&group_member_acct);
1086 user = user->next;
1088 pwent_free(userlist);
1089 DEBUG(10, ("got primary groups, members: [%d]\n", *num_sids));
1091 winbind_on();
1092 return True;
1095 /****************************************************************************
1096 Create a UNIX group on demand.
1097 ****************************************************************************/
1099 int smb_create_group(char *unix_group, gid_t *new_gid)
1101 pstring add_script;
1102 int ret = -1;
1103 int fd = 0;
1105 *new_gid = 0;
1107 /* defer to scripts */
1109 if ( *lp_addgroup_script() ) {
1110 pstrcpy(add_script, lp_addgroup_script());
1111 pstring_sub(add_script, "%g", unix_group);
1112 ret = smbrun(add_script, (new_gid!=NULL) ? &fd : NULL);
1113 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
1114 if (ret != 0)
1115 return ret;
1117 if (fd != 0) {
1118 fstring output;
1120 *new_gid = 0;
1121 if (read(fd, output, sizeof(output)) > 0) {
1122 *new_gid = (gid_t)strtoul(output, NULL, 10);
1125 close(fd);
1128 } else if ( winbind_create_group( unix_group, NULL ) ) {
1130 DEBUG(3,("smb_create_group: winbindd created the group (%s)\n",
1131 unix_group));
1132 ret = 0;
1135 if (*new_gid == 0) {
1136 struct group *grp = getgrnam(unix_group);
1138 if (grp != NULL)
1139 *new_gid = grp->gr_gid;
1142 return ret;
1145 /****************************************************************************
1146 Delete a UNIX group on demand.
1147 ****************************************************************************/
1149 int smb_delete_group(char *unix_group)
1151 pstring del_script;
1152 int ret;
1154 /* defer to scripts */
1156 if ( *lp_delgroup_script() ) {
1157 pstrcpy(del_script, lp_delgroup_script());
1158 pstring_sub(del_script, "%g", unix_group);
1159 ret = smbrun(del_script,NULL);
1160 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
1161 return ret;
1164 if ( winbind_delete_group( unix_group ) ) {
1165 DEBUG(3,("smb_delete_group: winbindd deleted the group (%s)\n",
1166 unix_group));
1167 return 0;
1170 return -1;
1173 /****************************************************************************
1174 Set a user's primary UNIX group.
1175 ****************************************************************************/
1176 int smb_set_primary_group(const char *unix_group, const char* unix_user)
1178 pstring add_script;
1179 int ret;
1181 /* defer to scripts */
1183 if ( *lp_setprimarygroup_script() ) {
1184 pstrcpy(add_script, lp_setprimarygroup_script());
1185 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
1186 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
1187 ret = smbrun(add_script,NULL);
1188 DEBUG(3,("smb_set_primary_group: "
1189 "Running the command `%s' gave %d\n",add_script,ret));
1190 return ret;
1193 /* Try winbindd */
1195 if ( winbind_set_user_primary_group( unix_user, unix_group ) ) {
1196 DEBUG(3,("smb_delete_group: winbindd set the group (%s) as the primary group for user (%s)\n",
1197 unix_group, unix_user));
1198 return 0;
1201 return -1;
1204 /****************************************************************************
1205 Add a user to a UNIX group.
1206 ****************************************************************************/
1208 int smb_add_user_group(char *unix_group, char *unix_user)
1210 pstring add_script;
1211 int ret;
1213 /* defer to scripts */
1215 if ( *lp_addusertogroup_script() ) {
1216 pstrcpy(add_script, lp_addusertogroup_script());
1217 pstring_sub(add_script, "%g", unix_group);
1218 pstring_sub(add_script, "%u", unix_user);
1219 ret = smbrun(add_script,NULL);
1220 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1221 return ret;
1224 /* Try winbindd */
1226 if ( winbind_add_user_to_group( unix_user, unix_group ) ) {
1227 DEBUG(3,("smb_delete_group: winbindd added user (%s) to the group (%s)\n",
1228 unix_user, unix_group));
1229 return -1;
1232 return -1;
1235 /****************************************************************************
1236 Delete a user from a UNIX group
1237 ****************************************************************************/
1239 int smb_delete_user_group(const char *unix_group, const char *unix_user)
1241 pstring del_script;
1242 int ret;
1244 /* defer to scripts */
1246 if ( *lp_deluserfromgroup_script() ) {
1247 pstrcpy(del_script, lp_deluserfromgroup_script());
1248 pstring_sub(del_script, "%g", unix_group);
1249 pstring_sub(del_script, "%u", unix_user);
1250 ret = smbrun(del_script,NULL);
1251 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
1252 return ret;
1255 /* Try winbindd */
1257 if ( winbind_remove_user_from_group( unix_user, unix_group ) ) {
1258 DEBUG(3,("smb_delete_group: winbindd removed user (%s) from the group (%s)\n",
1259 unix_user, unix_group));
1260 return 0;
1263 return -1;
1267 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1268 DOM_SID sid)
1270 return get_group_map_from_sid(sid, map) ?
1271 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1274 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1275 gid_t gid)
1277 return get_group_map_from_gid(gid, map) ?
1278 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1281 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1282 const char *name)
1284 return get_group_map_from_ntname(name, map) ?
1285 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1288 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
1289 GROUP_MAP *map)
1291 return add_mapping_entry(map, TDB_INSERT) ?
1292 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1295 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
1296 GROUP_MAP *map)
1298 return add_mapping_entry(map, TDB_REPLACE) ?
1299 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1302 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
1303 DOM_SID sid)
1305 return group_map_remove(sid) ?
1306 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1309 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
1310 enum SID_NAME_USE sid_name_use,
1311 GROUP_MAP **rmap, int *num_entries,
1312 BOOL unix_only)
1314 return enum_group_mapping(sid_name_use, rmap, num_entries, unix_only) ?
1315 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1318 NTSTATUS pdb_default_find_alias(struct pdb_methods *methods,
1319 const char *name, DOM_SID *sid)
1321 GROUP_MAP map;
1323 if (!pdb_getgrnam(&map, name))
1324 return NT_STATUS_NO_SUCH_ALIAS;
1326 if ((map.sid_name_use != SID_NAME_WKN_GRP) &&
1327 (map.sid_name_use != SID_NAME_ALIAS))
1328 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1330 sid_copy(sid, &map.sid);
1331 return NT_STATUS_OK;
1334 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
1335 const char *name, uint32 *rid)
1337 DOM_SID sid;
1338 enum SID_NAME_USE type;
1339 uint32 new_rid;
1340 gid_t gid;
1342 GROUP_MAP map;
1344 if (lookup_name(get_global_sam_name(), name, &sid, &type))
1345 return NT_STATUS_ALIAS_EXISTS;
1347 if (!winbind_allocate_rid(&new_rid))
1348 return NT_STATUS_ACCESS_DENIED;
1350 sid_copy(&sid, get_global_sam_sid());
1351 sid_append_rid(&sid, new_rid);
1353 /* Here we allocate the gid */
1354 if (!winbind_sid_to_gid(&gid, &sid)) {
1355 DEBUG(0, ("Could not get gid for new RID\n"));
1356 return NT_STATUS_ACCESS_DENIED;
1359 map.gid = gid;
1360 sid_copy(&map.sid, &sid);
1361 map.sid_name_use = SID_NAME_ALIAS;
1362 fstrcpy(map.nt_name, name);
1363 fstrcpy(map.comment, "");
1365 if (!pdb_add_group_mapping_entry(&map)) {
1366 DEBUG(0, ("Could not add group mapping entry for alias %s\n",
1367 name));
1368 return NT_STATUS_ACCESS_DENIED;
1371 *rid = new_rid;
1373 return NT_STATUS_OK;
1376 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
1377 const DOM_SID *sid)
1379 return pdb_delete_group_mapping_entry(*sid) ?
1380 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1383 NTSTATUS pdb_default_enum_aliases(struct pdb_methods *methods,
1384 const DOM_SID *sid,
1385 uint32 start_idx, uint32 max_entries,
1386 uint32 *num_aliases,
1387 struct acct_info **info)
1389 extern DOM_SID global_sid_Builtin;
1391 GROUP_MAP *map;
1392 int i, num_maps;
1393 enum SID_NAME_USE type = SID_NAME_UNKNOWN;
1395 if (sid_compare(sid, get_global_sam_sid()) == 0)
1396 type = SID_NAME_ALIAS;
1398 if (sid_compare(sid, &global_sid_Builtin) == 0)
1399 type = SID_NAME_WKN_GRP;
1401 if (!pdb_enum_group_mapping(type, &map, &num_maps, False) ||
1402 (num_maps == 0)) {
1403 *num_aliases = 0;
1404 *info = NULL;
1405 goto done;
1408 if (start_idx > num_maps) {
1409 *num_aliases = 0;
1410 *info = NULL;
1411 goto done;
1414 *num_aliases = num_maps - start_idx;
1416 if (*num_aliases > max_entries)
1417 *num_aliases = max_entries;
1419 *info = malloc(sizeof(struct acct_info) * (*num_aliases));
1421 for (i=0; i<*num_aliases; i++) {
1422 fstrcpy((*info)[i].acct_name, map[i+start_idx].nt_name);
1423 fstrcpy((*info)[i].acct_desc, map[i+start_idx].comment);
1424 sid_peek_rid(&map[i].sid, &(*info)[i+start_idx].rid);
1427 done:
1428 SAFE_FREE(map);
1429 return NT_STATUS_OK;
1432 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
1433 const DOM_SID *sid,
1434 struct acct_info *info)
1436 GROUP_MAP map;
1438 if (!pdb_getgrsid(&map, *sid))
1439 return NT_STATUS_NO_SUCH_ALIAS;
1441 fstrcpy(info->acct_name, map.nt_name);
1442 fstrcpy(info->acct_desc, map.comment);
1443 sid_peek_rid(&map.sid, &info->rid);
1444 return NT_STATUS_OK;
1447 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
1448 const DOM_SID *sid,
1449 struct acct_info *info)
1451 GROUP_MAP map;
1453 if (!pdb_getgrsid(&map, *sid))
1454 return NT_STATUS_NO_SUCH_ALIAS;
1456 fstrcpy(map.comment, info->acct_desc);
1458 if (!pdb_update_group_mapping_entry(&map))
1459 return NT_STATUS_ACCESS_DENIED;
1461 return NT_STATUS_OK;
1464 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
1465 const DOM_SID *alias, const DOM_SID *member)
1467 return add_aliasmem(alias, member);
1470 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
1471 const DOM_SID *alias, const DOM_SID *member)
1473 return del_aliasmem(alias, member);
1476 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
1477 const DOM_SID *alias, DOM_SID **members,
1478 int *num_members)
1480 return enum_aliasmem(alias, members, num_members);
1483 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
1484 const DOM_SID *sid,
1485 DOM_SID **aliases, int *num)
1487 return alias_memberships(sid, aliases, num);
1490 /**********************************************************************
1491 no ops for passdb backends that don't implement group mapping
1492 *********************************************************************/
1494 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1495 DOM_SID sid)
1497 return NT_STATUS_UNSUCCESSFUL;
1500 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1501 gid_t gid)
1503 return NT_STATUS_UNSUCCESSFUL;
1506 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1507 const char *name)
1509 return NT_STATUS_UNSUCCESSFUL;
1512 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
1513 GROUP_MAP *map)
1515 return NT_STATUS_UNSUCCESSFUL;
1518 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
1519 GROUP_MAP *map)
1521 return NT_STATUS_UNSUCCESSFUL;
1524 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
1525 DOM_SID sid)
1527 return NT_STATUS_UNSUCCESSFUL;
1530 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
1531 enum SID_NAME_USE sid_name_use,
1532 GROUP_MAP **rmap, int *num_entries,
1533 BOOL unix_only)
1535 return NT_STATUS_UNSUCCESSFUL;