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/"
33 {SE_PRIV_NONE
, "no_privs", "No privilege" }, /* this one MUST be first */
34 {SE_PRIV_ADD_MACHINES
, "SeMachineAccountPrivilege", "Add workstations to the domain" },
35 {SE_PRIV_SEC_PRIV
, "SeSecurityPrivilege", "Manage the audit logs" },
36 {SE_PRIV_TAKE_OWNER
, "SeTakeOwnershipPrivilege", "Take ownership of file" },
37 {SE_PRIV_ADD_USERS
, "SaAddUsers", "Add users to the domain - Samba" },
38 {SE_PRIV_PRINT_OPERATOR
, "SaPrintOp", "Add or remove printers - Samba" },
39 {SE_PRIV_ALL
, "SaAllPrivs", "all privileges" }
43 /****************************************************************************
44 dump the mapping group mapping to a text file
45 ****************************************************************************/
46 char *decode_sid_name_use(fstring group
, enum SID_NAME_USE name_use
)
48 static fstring group_type
;
52 fstrcpy(group_type
,"User");
54 case SID_NAME_DOM_GRP
:
55 fstrcpy(group_type
,"Domain group");
58 fstrcpy(group_type
,"Domain");
61 fstrcpy(group_type
,"Local group");
63 case SID_NAME_WKN_GRP
:
64 fstrcpy(group_type
,"Builtin group");
66 case SID_NAME_DELETED
:
67 fstrcpy(group_type
,"Deleted");
69 case SID_NAME_INVALID
:
70 fstrcpy(group_type
,"Invalid");
72 case SID_NAME_UNKNOWN
:
74 fstrcpy(group_type
,"Unknown type");
78 fstrcpy(group
, group_type
);
82 /****************************************************************************
83 initialise first time the mapping list - called from init_group_mapping()
84 ****************************************************************************/
85 static BOOL
default_group_mapping(void)
94 /* Add the Wellknown groups */
96 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP
, "Administrators", "");
97 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP
, "Users", "");
98 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP
, "Guests", "");
99 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP
, "Power Users", "");
100 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP
, "Account Operators", "");
101 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP
, "System Operators", "");
102 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP
, "Print Operators", "");
103 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP
, "Backup Operators", "");
104 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP
, "Replicators", "");
106 /* Add the defaults domain groups */
108 sid_copy(&sid_admins
, get_global_sam_sid());
109 sid_append_rid(&sid_admins
, DOMAIN_GROUP_RID_ADMINS
);
110 sid_to_string(str_admins
, &sid_admins
);
111 add_initial_entry(-1, str_admins
, SID_NAME_DOM_GRP
, "Domain Admins", "");
113 sid_copy(&sid_users
, get_global_sam_sid());
114 sid_append_rid(&sid_users
, DOMAIN_GROUP_RID_USERS
);
115 sid_to_string(str_users
, &sid_users
);
116 add_initial_entry(-1, str_users
, SID_NAME_DOM_GRP
, "Domain Users", "");
118 sid_copy(&sid_guests
, get_global_sam_sid());
119 sid_append_rid(&sid_guests
, DOMAIN_GROUP_RID_GUESTS
);
120 sid_to_string(str_guests
, &sid_guests
);
121 add_initial_entry(-1, str_guests
, SID_NAME_DOM_GRP
, "Domain Guests", "");
126 /****************************************************************************
127 Open the group mapping tdb.
128 ****************************************************************************/
130 static BOOL
init_group_mapping(void)
132 static pid_t local_pid
;
133 const char *vstring
= "INFO/version";
136 if (tdb
&& local_pid
== sys_getpid())
138 tdb
= tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
140 DEBUG(0,("Failed to open group mapping database\n"));
144 local_pid
= sys_getpid();
146 /* handle a Samba upgrade */
147 tdb_lock_bystring(tdb
, vstring
, 0);
149 /* Cope with byte-reversed older versions of the db. */
150 vers_id
= tdb_fetch_int32(tdb
, vstring
);
151 if ((vers_id
== DATABASE_VERSION_V1
) || (IREV(vers_id
) == DATABASE_VERSION_V1
)) {
152 /* Written on a bigendian machine with old fetch_int code. Save as le. */
153 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
154 vers_id
= DATABASE_VERSION_V2
;
157 if (vers_id
!= DATABASE_VERSION_V2
) {
158 tdb_traverse(tdb
, tdb_traverse_delete_fn
, NULL
);
159 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
162 tdb_unlock_bystring(tdb
, vstring
);
164 /* write a list of default groups */
165 if(!default_group_mapping())
171 /****************************************************************************
172 ****************************************************************************/
173 static BOOL
add_mapping_entry(GROUP_MAP
*map
, int flag
)
177 fstring string_sid
="";
180 if(!init_group_mapping()) {
181 DEBUG(0,("failed to initialize group mapping"));
185 sid_to_string(string_sid
, &map
->sid
);
187 len
= tdb_pack(buf
, sizeof(buf
), "ddff",
188 map
->gid
, map
->sid_name_use
, map
->nt_name
, map
->comment
);
190 if (len
> sizeof(buf
))
193 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
195 kbuf
.dsize
= strlen(key
)+1;
199 if (tdb_store(tdb
, kbuf
, dbuf
, flag
) != 0) return False
;
204 /****************************************************************************
205 initialise first time the mapping list
206 ****************************************************************************/
207 BOOL
add_initial_entry(gid_t gid
, const char *sid
, enum SID_NAME_USE sid_name_use
, const char *nt_name
, const char *comment
)
211 if(!init_group_mapping()) {
212 DEBUG(0,("failed to initialize group mapping"));
217 if (!string_to_sid(&map
.sid
, sid
)) {
218 DEBUG(0, ("string_to_sid failed: %s", sid
));
222 map
.sid_name_use
=sid_name_use
;
223 fstrcpy(map
.nt_name
, nt_name
);
224 fstrcpy(map
.comment
, comment
);
226 return pdb_add_group_mapping_entry(&map
);
229 /****************************************************************************
230 Return the sid and the type of the unix group.
231 ****************************************************************************/
233 static BOOL
get_group_map_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
240 if(!init_group_mapping()) {
241 DEBUG(0,("failed to initialize group mapping"));
245 /* the key is the SID, retrieving is direct */
247 sid_to_string(string_sid
, &sid
);
248 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
251 kbuf
.dsize
= strlen(key
)+1;
253 dbuf
= tdb_fetch(tdb
, kbuf
);
257 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
258 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
);
260 SAFE_FREE(dbuf
.dptr
);
263 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
267 sid_copy(&map
->sid
, &sid
);
272 /****************************************************************************
273 Return the sid and the type of the unix group.
274 ****************************************************************************/
276 static BOOL
get_group_map_from_gid(gid_t gid
, GROUP_MAP
*map
)
278 TDB_DATA kbuf
, dbuf
, newkey
;
282 if(!init_group_mapping()) {
283 DEBUG(0,("failed to initialize group mapping"));
287 /* we need to enumerate the TDB to find the GID */
289 for (kbuf
= tdb_firstkey(tdb
);
291 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
293 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0) continue;
295 dbuf
= tdb_fetch(tdb
, kbuf
);
299 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
301 string_to_sid(&map
->sid
, string_sid
);
303 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
304 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
);
306 SAFE_FREE(dbuf
.dptr
);
309 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
314 SAFE_FREE(kbuf
.dptr
);
322 /****************************************************************************
323 Return the sid and the type of the unix group.
324 ****************************************************************************/
326 static BOOL
get_group_map_from_ntname(const char *name
, GROUP_MAP
*map
)
328 TDB_DATA kbuf
, dbuf
, newkey
;
332 if(!init_group_mapping()) {
333 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping"));
337 /* we need to enumerate the TDB to find the name */
339 for (kbuf
= tdb_firstkey(tdb
);
341 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
343 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0) continue;
345 dbuf
= tdb_fetch(tdb
, kbuf
);
349 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
351 string_to_sid(&map
->sid
, string_sid
);
353 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
354 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
);
356 SAFE_FREE(dbuf
.dptr
);
359 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
363 if (StrCaseCmp(name
, map
->nt_name
)==0) {
364 SAFE_FREE(kbuf
.dptr
);
372 /****************************************************************************
373 Remove a group mapping entry.
374 ****************************************************************************/
376 static BOOL
group_map_remove(DOM_SID sid
)
382 if(!init_group_mapping()) {
383 DEBUG(0,("failed to initialize group mapping"));
387 /* the key is the SID, retrieving is direct */
389 sid_to_string(string_sid
, &sid
);
390 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
393 kbuf
.dsize
= strlen(key
)+1;
395 dbuf
= tdb_fetch(tdb
, kbuf
);
399 SAFE_FREE(dbuf
.dptr
);
401 if(tdb_delete(tdb
, kbuf
) != TDB_SUCCESS
)
407 /****************************************************************************
408 Enumerate the group mapping.
409 ****************************************************************************/
411 static BOOL
enum_group_mapping(enum SID_NAME_USE sid_name_use
, GROUP_MAP
**rmap
,
412 int *num_entries
, BOOL unix_only
)
414 TDB_DATA kbuf
, dbuf
, newkey
;
422 if(!init_group_mapping()) {
423 DEBUG(0,("failed to initialize group mapping"));
430 for (kbuf
= tdb_firstkey(tdb
);
432 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
434 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0)
437 dbuf
= tdb_fetch(tdb
, kbuf
);
441 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
443 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
444 &map
.gid
, &map
.sid_name_use
, &map
.nt_name
, &map
.comment
);
446 SAFE_FREE(dbuf
.dptr
);
449 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
453 /* list only the type or everything if UNKNOWN */
454 if (sid_name_use
!=SID_NAME_UNKNOWN
&& sid_name_use
!=map
.sid_name_use
) {
455 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map
.nt_name
));
459 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
460 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map
.nt_name
));
464 string_to_sid(&map
.sid
, string_sid
);
466 decode_sid_name_use(group_type
, map
.sid_name_use
);
467 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map
.nt_name
,group_type
));
469 mapt
=(GROUP_MAP
*)Realloc((*rmap
), (entries
+1)*sizeof(GROUP_MAP
));
471 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
478 mapt
[entries
].gid
= map
.gid
;
479 sid_copy( &mapt
[entries
].sid
, &map
.sid
);
480 mapt
[entries
].sid_name_use
= map
.sid_name_use
;
481 fstrcpy(mapt
[entries
].nt_name
, map
.nt_name
);
482 fstrcpy(mapt
[entries
].comment
, map
.comment
);
488 *num_entries
=entries
;
493 static NTSTATUS
add_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
499 char *new_memberstring
;
502 if(!init_group_mapping()) {
503 DEBUG(0,("failed to initialize group mapping"));
504 return NT_STATUS_ACCESS_DENIED
;
507 if (!get_group_map_from_sid(*alias
, &map
))
508 return NT_STATUS_NO_SUCH_ALIAS
;
510 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
511 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
512 return NT_STATUS_NO_SUCH_ALIAS
;
514 sid_to_string(string_sid
, alias
);
515 slprintf(key
, sizeof(key
), "%s%s", ALIASMEM_PREFIX
, string_sid
);
517 kbuf
.dsize
= strlen(key
)+1;
520 dbuf
= tdb_fetch(tdb
, kbuf
);
522 sid_to_string(string_sid
, member
);
524 if (dbuf
.dptr
!= NULL
) {
525 asprintf(&new_memberstring
, "%s %s", (char *)(dbuf
.dptr
),
528 new_memberstring
= strdup(string_sid
);
531 if (new_memberstring
== NULL
)
532 return NT_STATUS_NO_MEMORY
;
534 SAFE_FREE(dbuf
.dptr
);
535 dbuf
.dsize
= strlen(new_memberstring
)+1;
536 dbuf
.dptr
= new_memberstring
;
538 result
= tdb_store(tdb
, kbuf
, dbuf
, 0);
540 SAFE_FREE(new_memberstring
);
542 return (result
== 0 ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
);
545 static BOOL
add_sid_to_array(DOM_SID sid
, DOM_SID
**sids
, int *num
)
547 *sids
= Realloc(*sids
, ((*num
)+1) * sizeof(DOM_SID
));
552 sid_copy(&((*sids
)[*num
]), &sid
);
558 static NTSTATUS
enum_aliasmem(const DOM_SID
*alias
, DOM_SID
**sids
, int *num
)
566 if(!init_group_mapping()) {
567 DEBUG(0,("failed to initialize group mapping"));
568 return NT_STATUS_ACCESS_DENIED
;
571 if (!get_group_map_from_sid(*alias
, &map
))
572 return NT_STATUS_NO_SUCH_ALIAS
;
574 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
575 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
576 return NT_STATUS_NO_SUCH_ALIAS
;
581 sid_to_string(string_sid
, alias
);
582 slprintf(key
, sizeof(key
), "%s%s", ALIASMEM_PREFIX
, string_sid
);
584 kbuf
.dsize
= strlen(key
)+1;
587 dbuf
= tdb_fetch(tdb
, kbuf
);
589 if (dbuf
.dptr
== NULL
) {
595 while (next_token(&p
, string_sid
, " ", sizeof(string_sid
))) {
599 if (!string_to_sid(&sid
, string_sid
))
602 if (!add_sid_to_array(sid
, sids
, num
))
603 return NT_STATUS_NO_MEMORY
;
606 SAFE_FREE(dbuf
.dptr
);
611 /* This is racy as hell, but hey, it's only a prototype :-) */
613 static NTSTATUS
del_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
624 result
= enum_aliasmem(alias
, &sids
, &num
);
626 if (!NT_STATUS_IS_OK(result
))
629 for (i
=0; i
<num
; i
++) {
630 if (sid_compare(&sids
[i
], member
) == 0) {
638 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
642 sids
[i
] = sids
[num
-1];
646 member_string
= strdup("");
648 if (member_string
== NULL
) {
650 return NT_STATUS_NO_MEMORY
;
653 for (i
=0; i
<num
; i
++) {
654 char *s
= member_string
;
656 sid_to_string(sid_string
, &sids
[i
]);
657 asprintf(&member_string
, "%s %s", s
, sid_string
);
660 if (member_string
== NULL
) {
662 return NT_STATUS_NO_MEMORY
;
666 sid_to_string(sid_string
, alias
);
667 slprintf(key
, sizeof(key
), "%s%s", ALIASMEM_PREFIX
, sid_string
);
669 kbuf
.dsize
= strlen(key
)+1;
671 dbuf
.dsize
= strlen(member_string
)+1;
672 dbuf
.dptr
= member_string
;
674 result
= tdb_store(tdb
, kbuf
, dbuf
, 0) == 0 ?
675 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
678 SAFE_FREE(member_string
);
683 static BOOL
is_foreign_alias_member(const DOM_SID
*sid
, const DOM_SID
*alias
)
689 if (!NT_STATUS_IS_OK(enum_aliasmem(alias
, &members
, &num
)))
692 for (i
=0; i
<num
; i
++) {
694 if (sid_compare(&members
[i
], sid
) == 0) {
704 static NTSTATUS
alias_memberships(const DOM_SID
*sid
, DOM_SID
**sids
, int *num
)
712 if (!enum_group_mapping(SID_NAME_WKN_GRP
, &maps
, &num_maps
, False
))
713 return NT_STATUS_NO_MEMORY
;
715 for (i
=0; i
<num_maps
; i
++) {
716 if (is_foreign_alias_member(sid
, &maps
[i
].sid
))
717 add_sid_to_array(maps
[i
].sid
, sids
, num
);
721 if (!enum_group_mapping(SID_NAME_ALIAS
, &maps
, &num_maps
, False
))
722 return NT_STATUS_NO_MEMORY
;
724 for (i
=0; i
<num_maps
; i
++) {
725 if (is_foreign_alias_member(sid
, &maps
[i
].sid
))
726 add_sid_to_array(maps
[i
].sid
, sids
, num
);
735 * High level functions
736 * better to use them than the lower ones.
738 * we are checking if the group is in the mapping file
739 * and if the group is an existing unix group
743 /* get a domain group from it's SID */
745 BOOL
get_domain_group_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
750 if(!init_group_mapping()) {
751 DEBUG(0,("failed to initialize group mapping"));
755 DEBUG(10, ("get_domain_group_from_sid\n"));
757 /* if the group is NOT in the database, it CAN NOT be a domain group */
760 ret
= pdb_getgrsid(map
, sid
);
766 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
768 /* if it's not a domain group, continue */
769 if (map
->sid_name_use
!=SID_NAME_DOM_GRP
) {
773 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
779 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map
->gid
));
781 grp
= getgrgid(map
->gid
);
783 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
787 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
793 /* get a local (alias) group from it's SID */
795 BOOL
get_local_group_from_sid(DOM_SID
*sid
, GROUP_MAP
*map
)
799 if(!init_group_mapping()) {
800 DEBUG(0,("failed to initialize group mapping"));
804 /* The group is in the mapping table */
806 ret
= pdb_getgrsid(map
, *sid
);
812 if ( ( (map
->sid_name_use
!= SID_NAME_ALIAS
) &&
813 (map
->sid_name_use
!= SID_NAME_WKN_GRP
) )
815 || (getgrgid(map
->gid
) == NULL
) )
821 /* local groups only exist in the group mapping DB so this
825 /* the group isn't in the mapping table.
826 * make one based on the unix information */
830 sid_peek_rid(sid
, &alias_rid
);
831 map
->gid
=pdb_group_rid_to_gid(alias_rid
);
833 grp
= getgrgid(map
->gid
);
835 DEBUG(3,("get_local_group_from_sid: No unix group for [%ul]\n", map
->gid
));
839 map
->sid_name_use
=SID_NAME_ALIAS
;
841 fstrcpy(map
->nt_name
, grp
->gr_name
);
842 fstrcpy(map
->comment
, "Local Unix Group");
844 sid_copy(&map
->sid
, sid
);
851 /* get a builtin group from it's SID */
853 BOOL
get_builtin_group_from_sid(DOM_SID
*sid
, GROUP_MAP
*map
)
859 if(!init_group_mapping()) {
860 DEBUG(0,("failed to initialize group mapping"));
865 ret
= pdb_getgrsid(map
, *sid
);
871 if (map
->sid_name_use
!=SID_NAME_WKN_GRP
) {
879 if ( (grp
=getgrgid(map
->gid
)) == NULL
) {
888 /****************************************************************************
889 Returns a GROUP_MAP struct based on the gid.
890 ****************************************************************************/
891 BOOL
get_group_from_gid(gid_t gid
, GROUP_MAP
*map
)
896 if(!init_group_mapping()) {
897 DEBUG(0,("failed to initialize group mapping"));
901 if ( (grp
=getgrgid(gid
)) == NULL
)
905 * make a group map from scratch if doesn't exist.
909 ret
= pdb_getgrgid(map
, gid
);
914 map
->sid_name_use
=SID_NAME_ALIAS
;
916 /* interim solution until we have a last RID allocated */
918 sid_copy(&map
->sid
, get_global_sam_sid());
919 sid_append_rid(&map
->sid
, pdb_gid_to_group_rid(gid
));
921 fstrcpy(map
->nt_name
, grp
->gr_name
);
922 fstrcpy(map
->comment
, "Local Unix Group");
931 /****************************************************************************
932 Get the member users of a group and
933 all the users who have that group as primary.
935 give back an array of SIDS
936 return the grand number of users
939 TODO: sort the list and remove duplicate. JFM.
941 ****************************************************************************/
943 BOOL
get_sid_list_of_group(gid_t gid
, DOM_SID
**sids
, int *num_sids
)
953 struct sys_pwent
*userlist
;
954 struct sys_pwent
*user
;
956 if(!init_group_mapping()) {
957 DEBUG(0,("failed to initialize group mapping"));
964 if ( (grp
=getgrgid(gid
)) == NULL
)
968 DEBUG(10, ("getting members\n"));
970 while (gr
&& (*gr
!= (char)'\0')) {
971 SAM_ACCOUNT
*group_member_acct
= NULL
;
973 s
= Realloc((*sids
), sizeof(**sids
)*(*num_sids
+1));
975 DEBUG(0,("get_uid_list_of_group: unable to enlarge SID list!\n"));
980 if (!NT_STATUS_IS_OK(pdb_init_sam(&group_member_acct
))) {
985 found_user
= pdb_getsampwnam(group_member_acct
, gr
);
989 sid_copy(&(*sids
)[*num_sids
], pdb_get_user_sid(group_member_acct
));
993 pdb_free_sam(&group_member_acct
);
995 gr
= grp
->gr_mem
[++i
];
997 DEBUG(10, ("got [%d] members\n", *num_sids
));
1001 user
= userlist
= getpwent_list();
1003 while (user
!= NULL
) {
1005 SAM_ACCOUNT
*group_member_acct
= NULL
;
1008 if (user
->pw_gid
!= gid
) {
1013 s
= Realloc((*sids
), sizeof(**sids
)*(*num_sids
+1));
1015 DEBUG(0,("get_sid_list_of_group: unable to enlarge "
1017 pwent_free(userlist
);
1023 if (!NT_STATUS_IS_OK(pdb_init_sam(&group_member_acct
))) {
1028 found_user
= pdb_getsampwnam(group_member_acct
, user
->pw_name
);
1032 sid_copy(&(*sids
)[*num_sids
],
1033 pdb_get_user_sid(group_member_acct
));
1036 DEBUG(4,("get_sid_list_of_group: User %s [uid == %lu] "
1037 "has no samba account\n",
1038 user
->pw_name
, (unsigned long)user
->pw_uid
));
1039 if (algorithmic_uid_to_sid(&(*sids
)[*num_sids
],
1043 pdb_free_sam(&group_member_acct
);
1047 pwent_free(userlist
);
1048 DEBUG(10, ("got primary groups, members: [%d]\n", *num_sids
));
1052 if ( NT_STATUS_IS_OK(gid_to_sid(&sid
, gid
)) &&
1053 NT_STATUS_IS_OK(enum_aliasmem(&sid
, &members
, &num_members
)) ) {
1055 for (i
=0; i
<num_members
; i
++) {
1056 add_sid_to_array(members
[i
], sids
, num_sids
);
1063 /****************************************************************************
1064 Create a UNIX group on demand.
1065 ****************************************************************************/
1067 int smb_create_group(char *unix_group
, gid_t
*new_gid
)
1075 /* defer to scripts */
1077 if ( *lp_addgroup_script() ) {
1078 pstrcpy(add_script
, lp_addgroup_script());
1079 pstring_sub(add_script
, "%g", unix_group
);
1080 ret
= smbrun(add_script
, (new_gid
!=NULL
) ? &fd
: NULL
);
1081 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script
,ret
));
1089 if (read(fd
, output
, sizeof(output
)) > 0) {
1090 *new_gid
= (gid_t
)strtoul(output
, NULL
, 10);
1096 } else if ( winbind_create_group( unix_group
, NULL
) ) {
1098 DEBUG(3,("smb_create_group: winbindd created the group (%s)\n",
1103 if (*new_gid
== 0) {
1104 struct group
*grp
= getgrnam(unix_group
);
1107 *new_gid
= grp
->gr_gid
;
1113 /****************************************************************************
1114 Delete a UNIX group on demand.
1115 ****************************************************************************/
1117 int smb_delete_group(char *unix_group
)
1122 /* defer to scripts */
1124 if ( *lp_delgroup_script() ) {
1125 pstrcpy(del_script
, lp_delgroup_script());
1126 pstring_sub(del_script
, "%g", unix_group
);
1127 ret
= smbrun(del_script
,NULL
);
1128 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script
,ret
));
1132 if ( winbind_delete_group( unix_group
) ) {
1133 DEBUG(3,("smb_delete_group: winbindd deleted the group (%s)\n",
1141 /****************************************************************************
1142 Set a user's primary UNIX group.
1143 ****************************************************************************/
1144 int smb_set_primary_group(const char *unix_group
, const char* unix_user
)
1149 /* defer to scripts */
1151 if ( *lp_setprimarygroup_script() ) {
1152 pstrcpy(add_script
, lp_setprimarygroup_script());
1153 all_string_sub(add_script
, "%g", unix_group
, sizeof(add_script
));
1154 all_string_sub(add_script
, "%u", unix_user
, sizeof(add_script
));
1155 ret
= smbrun(add_script
,NULL
);
1156 DEBUG(3,("smb_set_primary_group: "
1157 "Running the command `%s' gave %d\n",add_script
,ret
));
1163 if ( winbind_set_user_primary_group( unix_user
, unix_group
) ) {
1164 DEBUG(3,("smb_delete_group: winbindd set the group (%s) as the primary group for user (%s)\n",
1165 unix_group
, unix_user
));
1172 /****************************************************************************
1173 Add a user to a UNIX group.
1174 ****************************************************************************/
1176 int smb_add_user_group(char *unix_group
, char *unix_user
)
1181 /* defer to scripts */
1183 if ( *lp_addusertogroup_script() ) {
1184 pstrcpy(add_script
, lp_addusertogroup_script());
1185 pstring_sub(add_script
, "%g", unix_group
);
1186 pstring_sub(add_script
, "%u", unix_user
);
1187 ret
= smbrun(add_script
,NULL
);
1188 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script
,ret
));
1194 if ( winbind_add_user_to_group( unix_user
, unix_group
) ) {
1195 DEBUG(3,("smb_delete_group: winbindd added user (%s) to the group (%s)\n",
1196 unix_user
, unix_group
));
1203 /****************************************************************************
1204 Delete a user from a UNIX group
1205 ****************************************************************************/
1207 int smb_delete_user_group(const char *unix_group
, const char *unix_user
)
1212 /* defer to scripts */
1214 if ( *lp_deluserfromgroup_script() ) {
1215 pstrcpy(del_script
, lp_deluserfromgroup_script());
1216 pstring_sub(del_script
, "%g", unix_group
);
1217 pstring_sub(del_script
, "%u", unix_user
);
1218 ret
= smbrun(del_script
,NULL
);
1219 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script
,ret
));
1225 if ( winbind_remove_user_from_group( unix_user
, unix_group
) ) {
1226 DEBUG(3,("smb_delete_group: winbindd removed user (%s) from the group (%s)\n",
1227 unix_user
, unix_group
));
1235 NTSTATUS
pdb_default_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1238 return get_group_map_from_sid(sid
, map
) ?
1239 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1242 NTSTATUS
pdb_default_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1245 return get_group_map_from_gid(gid
, map
) ?
1246 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1249 NTSTATUS
pdb_default_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1252 return get_group_map_from_ntname(name
, map
) ?
1253 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1256 NTSTATUS
pdb_default_add_group_mapping_entry(struct pdb_methods
*methods
,
1259 return add_mapping_entry(map
, TDB_INSERT
) ?
1260 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1263 NTSTATUS
pdb_default_update_group_mapping_entry(struct pdb_methods
*methods
,
1266 return add_mapping_entry(map
, TDB_REPLACE
) ?
1267 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1270 NTSTATUS
pdb_default_delete_group_mapping_entry(struct pdb_methods
*methods
,
1273 return group_map_remove(sid
) ?
1274 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1277 NTSTATUS
pdb_default_enum_group_mapping(struct pdb_methods
*methods
,
1278 enum SID_NAME_USE sid_name_use
,
1279 GROUP_MAP
**rmap
, int *num_entries
,
1282 return enum_group_mapping(sid_name_use
, rmap
, num_entries
, unix_only
) ?
1283 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1286 NTSTATUS
pdb_default_add_aliasmem(struct pdb_methods
*methods
,
1287 const DOM_SID
*alias
, const DOM_SID
*member
)
1289 return add_aliasmem(alias
, member
);
1292 NTSTATUS
pdb_default_del_aliasmem(struct pdb_methods
*methods
,
1293 const DOM_SID
*alias
, const DOM_SID
*member
)
1295 return del_aliasmem(alias
, member
);
1298 NTSTATUS
pdb_default_enum_aliasmem(struct pdb_methods
*methods
,
1299 const DOM_SID
*alias
, DOM_SID
**members
,
1302 return enum_aliasmem(alias
, members
, num_members
);
1305 NTSTATUS
pdb_default_alias_memberships(struct pdb_methods
*methods
,
1307 DOM_SID
**aliases
, int *num
)
1309 return alias_memberships(sid
, aliases
, num
);
1312 /**********************************************************************
1313 no ops for passdb backends that don't implement group mapping
1314 *********************************************************************/
1316 NTSTATUS
pdb_nop_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1319 return NT_STATUS_UNSUCCESSFUL
;
1322 NTSTATUS
pdb_nop_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1325 return NT_STATUS_UNSUCCESSFUL
;
1328 NTSTATUS
pdb_nop_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1331 return NT_STATUS_UNSUCCESSFUL
;
1334 NTSTATUS
pdb_nop_add_group_mapping_entry(struct pdb_methods
*methods
,
1337 return NT_STATUS_UNSUCCESSFUL
;
1340 NTSTATUS
pdb_nop_update_group_mapping_entry(struct pdb_methods
*methods
,
1343 return NT_STATUS_UNSUCCESSFUL
;
1346 NTSTATUS
pdb_nop_delete_group_mapping_entry(struct pdb_methods
*methods
,
1349 return NT_STATUS_UNSUCCESSFUL
;
1352 NTSTATUS
pdb_nop_enum_group_mapping(struct pdb_methods
*methods
,
1353 enum SID_NAME_USE sid_name_use
,
1354 GROUP_MAP
**rmap
, int *num_entries
,
1357 return NT_STATUS_UNSUCCESSFUL
;