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.
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/"
30 #define ALIASMEM_PREFIX "ALIASMEMBERS/"
32 /****************************************************************************
33 dump the mapping group mapping to a text file
34 ****************************************************************************/
35 char *decode_sid_name_use(fstring group
, enum SID_NAME_USE name_use
)
37 static fstring group_type
;
41 fstrcpy(group_type
,"User");
43 case SID_NAME_DOM_GRP
:
44 fstrcpy(group_type
,"Domain group");
47 fstrcpy(group_type
,"Domain");
50 fstrcpy(group_type
,"Local group");
52 case SID_NAME_WKN_GRP
:
53 fstrcpy(group_type
,"Builtin group");
55 case SID_NAME_DELETED
:
56 fstrcpy(group_type
,"Deleted");
58 case SID_NAME_INVALID
:
59 fstrcpy(group_type
,"Invalid");
61 case SID_NAME_UNKNOWN
:
63 fstrcpy(group_type
,"Unknown type");
67 fstrcpy(group
, group_type
);
71 /****************************************************************************
72 initialise first time the mapping list - called from init_group_mapping()
73 ****************************************************************************/
74 static BOOL
default_group_mapping(void)
83 /* Add the Wellknown groups */
85 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP
, "Administrators", "");
86 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP
, "Users", "");
87 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP
, "Guests", "");
88 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP
, "Power Users", "");
89 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP
, "Account Operators", "");
90 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP
, "System Operators", "");
91 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP
, "Print Operators", "");
92 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP
, "Backup Operators", "");
93 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP
, "Replicators", "");
95 /* Add the defaults domain groups */
97 sid_copy(&sid_admins
, get_global_sam_sid());
98 sid_append_rid(&sid_admins
, DOMAIN_GROUP_RID_ADMINS
);
99 sid_to_string(str_admins
, &sid_admins
);
100 add_initial_entry(-1, str_admins
, SID_NAME_DOM_GRP
, "Domain Admins", "");
102 sid_copy(&sid_users
, get_global_sam_sid());
103 sid_append_rid(&sid_users
, DOMAIN_GROUP_RID_USERS
);
104 sid_to_string(str_users
, &sid_users
);
105 add_initial_entry(-1, str_users
, SID_NAME_DOM_GRP
, "Domain Users", "");
107 sid_copy(&sid_guests
, get_global_sam_sid());
108 sid_append_rid(&sid_guests
, DOMAIN_GROUP_RID_GUESTS
);
109 sid_to_string(str_guests
, &sid_guests
);
110 add_initial_entry(-1, str_guests
, SID_NAME_DOM_GRP
, "Domain Guests", "");
115 /****************************************************************************
116 Open the group mapping tdb.
117 ****************************************************************************/
119 static BOOL
init_group_mapping(void)
121 static pid_t local_pid
;
122 const char *vstring
= "INFO/version";
125 if (tdb
&& local_pid
== sys_getpid())
127 tdb
= tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
129 DEBUG(0,("Failed to open group mapping database\n"));
133 local_pid
= sys_getpid();
135 /* handle a Samba upgrade */
136 tdb_lock_bystring(tdb
, vstring
, 0);
138 /* Cope with byte-reversed older versions of the db. */
139 vers_id
= tdb_fetch_int32(tdb
, vstring
);
140 if ((vers_id
== DATABASE_VERSION_V1
) || (IREV(vers_id
) == DATABASE_VERSION_V1
)) {
141 /* Written on a bigendian machine with old fetch_int code. Save as le. */
142 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
143 vers_id
= DATABASE_VERSION_V2
;
146 if (vers_id
!= DATABASE_VERSION_V2
) {
147 tdb_traverse(tdb
, tdb_traverse_delete_fn
, NULL
);
148 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
151 tdb_unlock_bystring(tdb
, vstring
);
153 /* write a list of default groups */
154 if(!default_group_mapping())
160 /****************************************************************************
161 ****************************************************************************/
162 static BOOL
add_mapping_entry(GROUP_MAP
*map
, int flag
)
166 fstring string_sid
="";
169 if(!init_group_mapping()) {
170 DEBUG(0,("failed to initialize group mapping\n"));
174 sid_to_string(string_sid
, &map
->sid
);
176 len
= tdb_pack(buf
, sizeof(buf
), "ddff",
177 map
->gid
, map
->sid_name_use
, map
->nt_name
, map
->comment
);
179 if (len
> sizeof(buf
))
182 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
184 kbuf
.dsize
= strlen(key
)+1;
188 if (tdb_store(tdb
, kbuf
, dbuf
, flag
) != 0) return False
;
193 /****************************************************************************
194 initialise first time the mapping list
195 ****************************************************************************/
196 BOOL
add_initial_entry(gid_t gid
, const char *sid
, enum SID_NAME_USE sid_name_use
, const char *nt_name
, const char *comment
)
200 if(!init_group_mapping()) {
201 DEBUG(0,("failed to initialize group mapping\n"));
206 if (!string_to_sid(&map
.sid
, sid
)) {
207 DEBUG(0, ("string_to_sid failed: %s", sid
));
211 map
.sid_name_use
=sid_name_use
;
212 fstrcpy(map
.nt_name
, nt_name
);
213 fstrcpy(map
.comment
, comment
);
215 return pdb_add_group_mapping_entry(&map
);
218 /****************************************************************************
219 Return the sid and the type of the unix group.
220 ****************************************************************************/
222 static BOOL
get_group_map_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
229 if(!init_group_mapping()) {
230 DEBUG(0,("failed to initialize group mapping\n"));
234 /* the key is the SID, retrieving is direct */
236 sid_to_string(string_sid
, &sid
);
237 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
240 kbuf
.dsize
= strlen(key
)+1;
242 dbuf
= tdb_fetch(tdb
, kbuf
);
246 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
247 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
);
249 SAFE_FREE(dbuf
.dptr
);
252 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
256 sid_copy(&map
->sid
, &sid
);
261 /****************************************************************************
262 Return the sid and the type of the unix group.
263 ****************************************************************************/
265 static BOOL
get_group_map_from_gid(gid_t gid
, GROUP_MAP
*map
)
267 TDB_DATA kbuf
, dbuf
, newkey
;
271 if(!init_group_mapping()) {
272 DEBUG(0,("failed to initialize group mapping\n"));
276 /* we need to enumerate the TDB to find the GID */
278 for (kbuf
= tdb_firstkey(tdb
);
280 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
282 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0) continue;
284 dbuf
= tdb_fetch(tdb
, kbuf
);
288 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
290 string_to_sid(&map
->sid
, string_sid
);
292 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
293 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
);
295 SAFE_FREE(dbuf
.dptr
);
298 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
303 SAFE_FREE(kbuf
.dptr
);
311 /****************************************************************************
312 Return the sid and the type of the unix group.
313 ****************************************************************************/
315 static BOOL
get_group_map_from_ntname(const char *name
, GROUP_MAP
*map
)
317 TDB_DATA kbuf
, dbuf
, newkey
;
321 if(!init_group_mapping()) {
322 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping\n"));
326 /* we need to enumerate the TDB to find the name */
328 for (kbuf
= tdb_firstkey(tdb
);
330 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
332 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0) continue;
334 dbuf
= tdb_fetch(tdb
, kbuf
);
338 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
340 string_to_sid(&map
->sid
, string_sid
);
342 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
343 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
);
345 SAFE_FREE(dbuf
.dptr
);
348 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
352 if (StrCaseCmp(name
, map
->nt_name
)==0) {
353 SAFE_FREE(kbuf
.dptr
);
361 /****************************************************************************
362 Remove a group mapping entry.
363 ****************************************************************************/
365 static BOOL
group_map_remove(const DOM_SID
*sid
)
371 if(!init_group_mapping()) {
372 DEBUG(0,("failed to initialize group mapping\n"));
376 /* the key is the SID, retrieving is direct */
378 sid_to_string(string_sid
, sid
);
379 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
382 kbuf
.dsize
= strlen(key
)+1;
384 dbuf
= tdb_fetch(tdb
, kbuf
);
388 SAFE_FREE(dbuf
.dptr
);
390 if(tdb_delete(tdb
, kbuf
) != TDB_SUCCESS
)
396 /****************************************************************************
397 Enumerate the group mapping.
398 ****************************************************************************/
400 static BOOL
enum_group_mapping(enum SID_NAME_USE sid_name_use
, GROUP_MAP
**rmap
,
401 int *num_entries
, BOOL unix_only
)
403 TDB_DATA kbuf
, dbuf
, newkey
;
411 if(!init_group_mapping()) {
412 DEBUG(0,("failed to initialize group mapping\n"));
419 for (kbuf
= tdb_firstkey(tdb
);
421 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
423 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0)
426 dbuf
= tdb_fetch(tdb
, kbuf
);
430 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
432 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
433 &map
.gid
, &map
.sid_name_use
, &map
.nt_name
, &map
.comment
);
435 SAFE_FREE(dbuf
.dptr
);
438 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
442 /* list only the type or everything if UNKNOWN */
443 if (sid_name_use
!=SID_NAME_UNKNOWN
&& sid_name_use
!=map
.sid_name_use
) {
444 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map
.nt_name
));
448 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
449 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map
.nt_name
));
453 string_to_sid(&map
.sid
, string_sid
);
455 decode_sid_name_use(group_type
, map
.sid_name_use
);
456 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map
.nt_name
,group_type
));
458 mapt
=(GROUP_MAP
*)Realloc((*rmap
), (entries
+1)*sizeof(GROUP_MAP
));
460 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
467 mapt
[entries
].gid
= map
.gid
;
468 sid_copy( &mapt
[entries
].sid
, &map
.sid
);
469 mapt
[entries
].sid_name_use
= map
.sid_name_use
;
470 fstrcpy(mapt
[entries
].nt_name
, map
.nt_name
);
471 fstrcpy(mapt
[entries
].comment
, map
.comment
);
477 *num_entries
=entries
;
482 static NTSTATUS
add_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
488 char *new_memberstring
;
491 if(!init_group_mapping()) {
492 DEBUG(0,("failed to initialize group mapping\n"));
493 return NT_STATUS_ACCESS_DENIED
;
496 if (!get_group_map_from_sid(*alias
, &map
))
497 return NT_STATUS_NO_SUCH_ALIAS
;
499 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
500 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
501 return NT_STATUS_NO_SUCH_ALIAS
;
503 sid_to_string(string_sid
, alias
);
504 slprintf(key
, sizeof(key
), "%s%s", ALIASMEM_PREFIX
, string_sid
);
506 kbuf
.dsize
= strlen(key
)+1;
509 dbuf
= tdb_fetch(tdb
, kbuf
);
511 sid_to_string(string_sid
, member
);
513 if (dbuf
.dptr
!= NULL
) {
514 asprintf(&new_memberstring
, "%s %s", (char *)(dbuf
.dptr
),
517 new_memberstring
= strdup(string_sid
);
520 if (new_memberstring
== NULL
)
521 return NT_STATUS_NO_MEMORY
;
523 SAFE_FREE(dbuf
.dptr
);
524 dbuf
.dsize
= strlen(new_memberstring
)+1;
525 dbuf
.dptr
= new_memberstring
;
527 result
= tdb_store(tdb
, kbuf
, dbuf
, 0);
529 SAFE_FREE(new_memberstring
);
531 return (result
== 0 ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
);
534 static NTSTATUS
enum_aliasmem(const DOM_SID
*alias
, DOM_SID
**sids
, int *num
)
542 if(!init_group_mapping()) {
543 DEBUG(0,("failed to initialize group mapping\n"));
544 return NT_STATUS_ACCESS_DENIED
;
547 if (!get_group_map_from_sid(*alias
, &map
))
548 return NT_STATUS_NO_SUCH_ALIAS
;
550 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
551 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
552 return NT_STATUS_NO_SUCH_ALIAS
;
557 sid_to_string(string_sid
, alias
);
558 slprintf(key
, sizeof(key
), "%s%s", ALIASMEM_PREFIX
, string_sid
);
560 kbuf
.dsize
= strlen(key
)+1;
563 dbuf
= tdb_fetch(tdb
, kbuf
);
565 if (dbuf
.dptr
== NULL
) {
571 while (next_token(&p
, string_sid
, " ", sizeof(string_sid
))) {
575 if (!string_to_sid(&sid
, string_sid
))
578 add_sid_to_array(&sid
, sids
, num
);
581 return NT_STATUS_NO_MEMORY
;
584 SAFE_FREE(dbuf
.dptr
);
589 /* This is racy as hell, but hey, it's only a prototype :-) */
591 static NTSTATUS
del_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
602 result
= enum_aliasmem(alias
, &sids
, &num
);
604 if (!NT_STATUS_IS_OK(result
))
607 for (i
=0; i
<num
; i
++) {
608 if (sid_compare(&sids
[i
], member
) == 0) {
616 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
620 sids
[i
] = sids
[num
-1];
624 member_string
= strdup("");
626 if (member_string
== NULL
) {
628 return NT_STATUS_NO_MEMORY
;
631 for (i
=0; i
<num
; i
++) {
632 char *s
= member_string
;
634 sid_to_string(sid_string
, &sids
[i
]);
635 asprintf(&member_string
, "%s %s", s
, sid_string
);
638 if (member_string
== NULL
) {
640 return NT_STATUS_NO_MEMORY
;
644 sid_to_string(sid_string
, alias
);
645 slprintf(key
, sizeof(key
), "%s%s", ALIASMEM_PREFIX
, sid_string
);
647 kbuf
.dsize
= strlen(key
)+1;
649 dbuf
.dsize
= strlen(member_string
)+1;
650 dbuf
.dptr
= member_string
;
652 result
= tdb_store(tdb
, kbuf
, dbuf
, 0) == 0 ?
653 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
656 SAFE_FREE(member_string
);
661 static BOOL
is_foreign_alias_member(const DOM_SID
*sid
, const DOM_SID
*alias
)
667 if (!NT_STATUS_IS_OK(enum_aliasmem(alias
, &members
, &num
)))
670 for (i
=0; i
<num
; i
++) {
672 if (sid_compare(&members
[i
], sid
) == 0) {
682 static NTSTATUS
alias_memberships(const DOM_SID
*sid
, DOM_SID
**sids
, int *num
)
690 if (!enum_group_mapping(SID_NAME_WKN_GRP
, &maps
, &num_maps
, False
))
691 return NT_STATUS_NO_MEMORY
;
693 for (i
=0; i
<num_maps
; i
++) {
695 if (is_foreign_alias_member(sid
, &maps
[i
].sid
)) {
697 add_sid_to_array(&maps
[i
].sid
, sids
, num
);
701 return NT_STATUS_NO_MEMORY
;
707 if (!enum_group_mapping(SID_NAME_ALIAS
, &maps
, &num_maps
, False
))
708 return NT_STATUS_NO_MEMORY
;
710 for (i
=0; i
<num_maps
; i
++) {
711 if (is_foreign_alias_member(sid
, &maps
[i
].sid
)) {
713 add_sid_to_array(&maps
[i
].sid
, sids
, num
);
717 return NT_STATUS_NO_MEMORY
;
728 * High level functions
729 * better to use them than the lower ones.
731 * we are checking if the group is in the mapping file
732 * and if the group is an existing unix group
736 /* get a domain group from it's SID */
738 BOOL
get_domain_group_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
743 if(!init_group_mapping()) {
744 DEBUG(0,("failed to initialize group mapping\n"));
748 DEBUG(10, ("get_domain_group_from_sid\n"));
750 /* if the group is NOT in the database, it CAN NOT be a domain group */
753 ret
= pdb_getgrsid(map
, sid
);
759 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
761 /* if it's not a domain group, continue */
762 if (map
->sid_name_use
!=SID_NAME_DOM_GRP
) {
766 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
772 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map
->gid
));
774 grp
= getgrgid(map
->gid
);
776 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
780 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
786 /* get a local (alias) group from it's SID */
788 BOOL
get_local_group_from_sid(DOM_SID
*sid
, GROUP_MAP
*map
)
792 if(!init_group_mapping()) {
793 DEBUG(0,("failed to initialize group mapping\n"));
797 /* The group is in the mapping table */
799 ret
= pdb_getgrsid(map
, *sid
);
805 if ( ( (map
->sid_name_use
!= SID_NAME_ALIAS
) &&
806 (map
->sid_name_use
!= SID_NAME_WKN_GRP
) )
808 || (getgrgid(map
->gid
) == NULL
) )
814 /* local groups only exist in the group mapping DB so this
818 /* the group isn't in the mapping table.
819 * make one based on the unix information */
823 sid_peek_rid(sid
, &alias_rid
);
824 map
->gid
=pdb_group_rid_to_gid(alias_rid
);
826 grp
= getgrgid(map
->gid
);
828 DEBUG(3,("get_local_group_from_sid: No unix group for [%ul]\n", map
->gid
));
832 map
->sid_name_use
=SID_NAME_ALIAS
;
834 fstrcpy(map
->nt_name
, grp
->gr_name
);
835 fstrcpy(map
->comment
, "Local Unix Group");
837 sid_copy(&map
->sid
, sid
);
844 /* get a builtin group from it's SID */
846 BOOL
get_builtin_group_from_sid(DOM_SID
*sid
, GROUP_MAP
*map
)
852 if(!init_group_mapping()) {
853 DEBUG(0,("failed to initialize group mapping\n"));
858 ret
= pdb_getgrsid(map
, *sid
);
864 if (map
->sid_name_use
!=SID_NAME_WKN_GRP
) {
872 if ( (grp
=getgrgid(map
->gid
)) == NULL
) {
881 /****************************************************************************
882 Returns a GROUP_MAP struct based on the gid.
883 ****************************************************************************/
884 BOOL
get_group_from_gid(gid_t gid
, GROUP_MAP
*map
)
889 if(!init_group_mapping()) {
890 DEBUG(0,("failed to initialize group mapping\n"));
894 if ( (grp
=getgrgid(gid
)) == NULL
)
898 * make a group map from scratch if doesn't exist.
902 ret
= pdb_getgrgid(map
, gid
);
907 map
->sid_name_use
=SID_NAME_ALIAS
;
909 /* interim solution until we have a last RID allocated */
911 sid_copy(&map
->sid
, get_global_sam_sid());
912 sid_append_rid(&map
->sid
, pdb_gid_to_group_rid(gid
));
914 fstrcpy(map
->nt_name
, grp
->gr_name
);
915 fstrcpy(map
->comment
, "Local Unix Group");
924 /****************************************************************************
925 Get the member users of a group and
926 all the users who have that group as primary.
928 give back an array of SIDS
929 return the grand number of users
932 TODO: sort the list and remove duplicate. JFM.
934 ****************************************************************************/
936 BOOL
get_sid_list_of_group(gid_t gid
, DOM_SID
**sids
, int *num_sids
)
946 struct sys_pwent
*userlist
;
947 struct sys_pwent
*user
;
949 if(!init_group_mapping()) {
950 DEBUG(0,("failed to initialize group mapping\n"));
957 if ( (grp
=getgrgid(gid
)) == NULL
)
961 DEBUG(10, ("getting members\n"));
963 while (gr
&& (*gr
!= (char)'\0')) {
964 SAM_ACCOUNT
*group_member_acct
= NULL
;
966 s
= Realloc((*sids
), sizeof(**sids
)*(*num_sids
+1));
968 DEBUG(0,("get_uid_list_of_group: unable to enlarge SID list!\n"));
973 if (!NT_STATUS_IS_OK(pdb_init_sam(&group_member_acct
))) {
978 found_user
= pdb_getsampwnam(group_member_acct
, gr
);
982 sid_copy(&(*sids
)[*num_sids
], pdb_get_user_sid(group_member_acct
));
986 pdb_free_sam(&group_member_acct
);
988 gr
= grp
->gr_mem
[++i
];
990 DEBUG(10, ("got [%d] members\n", *num_sids
));
994 user
= userlist
= getpwent_list();
996 while (user
!= NULL
) {
998 SAM_ACCOUNT
*group_member_acct
= NULL
;
1001 if (user
->pw_gid
!= gid
) {
1006 s
= Realloc((*sids
), sizeof(**sids
)*(*num_sids
+1));
1008 DEBUG(0,("get_sid_list_of_group: unable to enlarge "
1010 pwent_free(userlist
);
1016 if (!NT_STATUS_IS_OK(pdb_init_sam(&group_member_acct
))) {
1021 found_user
= pdb_getsampwnam(group_member_acct
, user
->pw_name
);
1025 sid_copy(&(*sids
)[*num_sids
],
1026 pdb_get_user_sid(group_member_acct
));
1029 DEBUG(4,("get_sid_list_of_group: User %s [uid == %lu] "
1030 "has no samba account\n",
1031 user
->pw_name
, (unsigned long)user
->pw_uid
));
1032 if (algorithmic_uid_to_sid(&(*sids
)[*num_sids
],
1036 pdb_free_sam(&group_member_acct
);
1040 pwent_free(userlist
);
1041 DEBUG(10, ("got primary groups, members: [%d]\n", *num_sids
));
1045 if ( NT_STATUS_IS_OK(gid_to_sid(&sid
, gid
)) &&
1046 NT_STATUS_IS_OK(enum_aliasmem(&sid
, &members
, &num_members
)) ) {
1048 for (i
=0; i
<num_members
; i
++) {
1049 add_sid_to_array(&members
[i
], sids
, num_sids
);
1059 /****************************************************************************
1060 Create a UNIX group on demand.
1061 ****************************************************************************/
1063 int smb_create_group(char *unix_group
, gid_t
*new_gid
)
1071 /* defer to scripts */
1073 if ( *lp_addgroup_script() ) {
1074 pstrcpy(add_script
, lp_addgroup_script());
1075 pstring_sub(add_script
, "%g", unix_group
);
1076 ret
= smbrun(add_script
, (new_gid
!=NULL
) ? &fd
: NULL
);
1077 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script
,ret
));
1085 if (read(fd
, output
, sizeof(output
)) > 0) {
1086 *new_gid
= (gid_t
)strtoul(output
, NULL
, 10);
1092 } else if ( winbind_create_group( unix_group
, NULL
) ) {
1094 DEBUG(3,("smb_create_group: winbindd created the group (%s)\n",
1099 if (*new_gid
== 0) {
1100 struct group
*grp
= getgrnam(unix_group
);
1103 *new_gid
= grp
->gr_gid
;
1109 /****************************************************************************
1110 Delete a UNIX group on demand.
1111 ****************************************************************************/
1113 int smb_delete_group(char *unix_group
)
1118 /* defer to scripts */
1120 if ( *lp_delgroup_script() ) {
1121 pstrcpy(del_script
, lp_delgroup_script());
1122 pstring_sub(del_script
, "%g", unix_group
);
1123 ret
= smbrun(del_script
,NULL
);
1124 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script
,ret
));
1128 if ( winbind_delete_group( unix_group
) ) {
1129 DEBUG(3,("smb_delete_group: winbindd deleted the group (%s)\n",
1137 /****************************************************************************
1138 Set a user's primary UNIX group.
1139 ****************************************************************************/
1140 int smb_set_primary_group(const char *unix_group
, const char* unix_user
)
1145 /* defer to scripts */
1147 if ( *lp_setprimarygroup_script() ) {
1148 pstrcpy(add_script
, lp_setprimarygroup_script());
1149 all_string_sub(add_script
, "%g", unix_group
, sizeof(add_script
));
1150 all_string_sub(add_script
, "%u", unix_user
, sizeof(add_script
));
1151 ret
= smbrun(add_script
,NULL
);
1152 DEBUG(3,("smb_set_primary_group: "
1153 "Running the command `%s' gave %d\n",add_script
,ret
));
1159 if ( winbind_set_user_primary_group( unix_user
, unix_group
) ) {
1160 DEBUG(3,("smb_delete_group: winbindd set the group (%s) as the primary group for user (%s)\n",
1161 unix_group
, unix_user
));
1168 /****************************************************************************
1169 Add a user to a UNIX group.
1170 ****************************************************************************/
1172 int smb_add_user_group(char *unix_group
, char *unix_user
)
1177 /* defer to scripts */
1179 if ( *lp_addusertogroup_script() ) {
1180 pstrcpy(add_script
, lp_addusertogroup_script());
1181 pstring_sub(add_script
, "%g", unix_group
);
1182 pstring_sub(add_script
, "%u", unix_user
);
1183 ret
= smbrun(add_script
,NULL
);
1184 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script
,ret
));
1190 if ( winbind_add_user_to_group( unix_user
, unix_group
) ) {
1191 DEBUG(3,("smb_delete_group: winbindd added user (%s) to the group (%s)\n",
1192 unix_user
, unix_group
));
1199 /****************************************************************************
1200 Delete a user from a UNIX group
1201 ****************************************************************************/
1203 int smb_delete_user_group(const char *unix_group
, const char *unix_user
)
1208 /* defer to scripts */
1210 if ( *lp_deluserfromgroup_script() ) {
1211 pstrcpy(del_script
, lp_deluserfromgroup_script());
1212 pstring_sub(del_script
, "%g", unix_group
);
1213 pstring_sub(del_script
, "%u", unix_user
);
1214 ret
= smbrun(del_script
,NULL
);
1215 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script
,ret
));
1221 if ( winbind_remove_user_from_group( unix_user
, unix_group
) ) {
1222 DEBUG(3,("smb_delete_group: winbindd removed user (%s) from the group (%s)\n",
1223 unix_user
, unix_group
));
1231 NTSTATUS
pdb_default_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1234 return get_group_map_from_sid(sid
, map
) ?
1235 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1238 NTSTATUS
pdb_default_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1241 return get_group_map_from_gid(gid
, map
) ?
1242 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1245 NTSTATUS
pdb_default_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1248 return get_group_map_from_ntname(name
, map
) ?
1249 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1252 NTSTATUS
pdb_default_add_group_mapping_entry(struct pdb_methods
*methods
,
1255 return add_mapping_entry(map
, TDB_INSERT
) ?
1256 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1259 NTSTATUS
pdb_default_update_group_mapping_entry(struct pdb_methods
*methods
,
1262 return add_mapping_entry(map
, TDB_REPLACE
) ?
1263 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1266 NTSTATUS
pdb_default_delete_group_mapping_entry(struct pdb_methods
*methods
,
1269 return group_map_remove(&sid
) ?
1270 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1273 NTSTATUS
pdb_default_find_alias(struct pdb_methods
*methods
,
1274 const char *name
, DOM_SID
*sid
)
1278 if (!get_group_map_from_ntname(name
, &map
))
1279 return NT_STATUS_NO_SUCH_ALIAS
;
1281 if ((map
.sid_name_use
!= SID_NAME_WKN_GRP
) &&
1282 (map
.sid_name_use
!= SID_NAME_ALIAS
))
1283 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
1285 sid_copy(sid
, &map
.sid
);
1286 return NT_STATUS_OK
;
1289 NTSTATUS
pdb_default_create_alias(struct pdb_methods
*methods
,
1290 const char *name
, uint32
*rid
)
1293 enum SID_NAME_USE type
;
1297 if (lookup_name(get_global_sam_name(), name
, &sid
, &type
))
1298 return NT_STATUS_ALIAS_EXISTS
;
1300 if (!winbind_allocate_rid(&new_rid
))
1301 return NT_STATUS_ACCESS_DENIED
;
1303 sid_copy(&sid
, get_global_sam_sid());
1304 sid_append_rid(&sid
, new_rid
);
1306 /* Here we allocate the gid */
1307 if (!winbind_sid_to_gid(&gid
, &sid
)) {
1308 DEBUG(0, ("Could not get gid for new RID\n"));
1309 return NT_STATUS_ACCESS_DENIED
;
1312 if (!add_initial_entry(gid
, sid_string_static(&sid
), SID_NAME_ALIAS
,
1314 DEBUG(0, ("Could not add group mapping entry for alias %s\n",
1316 return NT_STATUS_ACCESS_DENIED
;
1321 return NT_STATUS_OK
;
1324 NTSTATUS
pdb_default_delete_alias(struct pdb_methods
*methods
,
1327 if (!group_map_remove(sid
))
1328 return NT_STATUS_ACCESS_DENIED
;
1330 return NT_STATUS_OK
;
1333 NTSTATUS
pdb_default_enum_aliases(struct pdb_methods
*methods
,
1335 uint32 start_idx
, uint32 max_entries
,
1336 uint32
*num_aliases
,
1337 struct acct_info
**info
)
1339 extern DOM_SID global_sid_Builtin
;
1343 enum SID_NAME_USE type
= SID_NAME_UNKNOWN
;
1345 if (sid_compare(sid
, get_global_sam_sid()) == 0)
1346 type
= SID_NAME_ALIAS
;
1348 if (sid_compare(sid
, &global_sid_Builtin
) == 0)
1349 type
= SID_NAME_WKN_GRP
;
1351 if (!enum_group_mapping(type
, &map
, &num_maps
, False
) ||
1358 if (start_idx
> num_maps
) {
1364 *num_aliases
= num_maps
- start_idx
;
1366 if (*num_aliases
> max_entries
)
1367 *num_aliases
= max_entries
;
1369 *info
= malloc(sizeof(struct acct_info
) * (*num_aliases
));
1371 for (i
=0; i
<*num_aliases
; i
++) {
1372 fstrcpy((*info
)[i
].acct_name
, map
[i
+start_idx
].nt_name
);
1373 fstrcpy((*info
)[i
].acct_desc
, map
[i
+start_idx
].comment
);
1374 sid_peek_rid(&map
[i
].sid
, &(*info
)[i
+start_idx
].rid
);
1379 return NT_STATUS_OK
;
1382 NTSTATUS
pdb_default_get_aliasinfo(struct pdb_methods
*methods
,
1384 struct acct_info
*info
)
1388 if (!get_group_map_from_sid(*sid
, &map
))
1389 return NT_STATUS_NO_SUCH_ALIAS
;
1391 fstrcpy(info
->acct_name
, map
.nt_name
);
1392 fstrcpy(info
->acct_desc
, map
.comment
);
1393 sid_peek_rid(&map
.sid
, &info
->rid
);
1394 return NT_STATUS_OK
;
1397 NTSTATUS
pdb_default_set_aliasinfo(struct pdb_methods
*methods
,
1399 struct acct_info
*info
)
1403 if (!get_group_map_from_sid(*sid
, &map
))
1404 return NT_STATUS_NO_SUCH_ALIAS
;
1406 fstrcpy(map
.comment
, info
->acct_desc
);
1408 if (!add_mapping_entry(&map
, TDB_REPLACE
))
1409 return NT_STATUS_ACCESS_DENIED
;
1411 return NT_STATUS_OK
;
1414 NTSTATUS
pdb_default_enum_group_mapping(struct pdb_methods
*methods
,
1415 enum SID_NAME_USE sid_name_use
,
1416 GROUP_MAP
**rmap
, int *num_entries
,
1419 return enum_group_mapping(sid_name_use
, rmap
, num_entries
, unix_only
) ?
1420 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1423 NTSTATUS
pdb_default_add_aliasmem(struct pdb_methods
*methods
,
1424 const DOM_SID
*alias
, const DOM_SID
*member
)
1426 return add_aliasmem(alias
, member
);
1429 NTSTATUS
pdb_default_del_aliasmem(struct pdb_methods
*methods
,
1430 const DOM_SID
*alias
, const DOM_SID
*member
)
1432 return del_aliasmem(alias
, member
);
1435 NTSTATUS
pdb_default_enum_aliasmem(struct pdb_methods
*methods
,
1436 const DOM_SID
*alias
, DOM_SID
**members
,
1439 return enum_aliasmem(alias
, members
, num_members
);
1442 NTSTATUS
pdb_default_alias_memberships(struct pdb_methods
*methods
,
1444 DOM_SID
**aliases
, int *num
)
1446 return alias_memberships(sid
, aliases
, num
);
1449 /**********************************************************************
1450 no ops for passdb backends that don't implement group mapping
1451 *********************************************************************/
1453 NTSTATUS
pdb_nop_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1456 return NT_STATUS_UNSUCCESSFUL
;
1459 NTSTATUS
pdb_nop_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1462 return NT_STATUS_UNSUCCESSFUL
;
1465 NTSTATUS
pdb_nop_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1468 return NT_STATUS_UNSUCCESSFUL
;
1471 NTSTATUS
pdb_nop_add_group_mapping_entry(struct pdb_methods
*methods
,
1474 return NT_STATUS_UNSUCCESSFUL
;
1477 NTSTATUS
pdb_nop_update_group_mapping_entry(struct pdb_methods
*methods
,
1480 return NT_STATUS_UNSUCCESSFUL
;
1483 NTSTATUS
pdb_nop_delete_group_mapping_entry(struct pdb_methods
*methods
,
1486 return NT_STATUS_UNSUCCESSFUL
;
1489 NTSTATUS
pdb_nop_enum_group_mapping(struct pdb_methods
*methods
,
1490 enum SID_NAME_USE sid_name_use
,
1491 GROUP_MAP
**rmap
, int *num_entries
,
1494 return NT_STATUS_UNSUCCESSFUL
;