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/"
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/"
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
;
58 fstrcpy(group_type
,"User");
60 case SID_NAME_DOM_GRP
:
61 fstrcpy(group_type
,"Domain group");
64 fstrcpy(group_type
,"Domain");
67 fstrcpy(group_type
,"Local group");
69 case SID_NAME_WKN_GRP
:
70 fstrcpy(group_type
,"Builtin group");
72 case SID_NAME_DELETED
:
73 fstrcpy(group_type
,"Deleted");
75 case SID_NAME_INVALID
:
76 fstrcpy(group_type
,"Invalid");
78 case SID_NAME_UNKNOWN
:
80 fstrcpy(group_type
,"Unknown type");
84 fstrcpy(group
, group_type
);
88 /****************************************************************************
89 initialise first time the mapping list - called from init_group_mapping()
90 ****************************************************************************/
91 static BOOL
default_group_mapping(void)
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", "");
132 /****************************************************************************
133 Open the group mapping tdb.
134 ****************************************************************************/
136 static BOOL
init_group_mapping(void)
138 const char *vstring
= "INFO/version";
143 tdb
= tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
145 DEBUG(0,("Failed to open group mapping database\n"));
149 /* handle a Samba upgrade */
150 tdb_lock_bystring(tdb
, vstring
, 0);
152 /* Cope with byte-reversed older versions of the db. */
153 vers_id
= tdb_fetch_int32(tdb
, vstring
);
154 if ((vers_id
== DATABASE_VERSION_V1
) || (IREV(vers_id
) == DATABASE_VERSION_V1
)) {
155 /* Written on a bigendian machine with old fetch_int code. Save as le. */
156 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
157 vers_id
= DATABASE_VERSION_V2
;
160 if (vers_id
!= DATABASE_VERSION_V2
) {
161 tdb_traverse(tdb
, tdb_traverse_delete_fn
, NULL
);
162 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
165 tdb_unlock_bystring(tdb
, vstring
);
167 /* write a list of default groups */
168 if(!default_group_mapping())
174 /****************************************************************************
175 ****************************************************************************/
176 static BOOL
add_mapping_entry(GROUP_MAP
*map
, int flag
)
180 fstring string_sid
="";
183 if(!init_group_mapping()) {
184 DEBUG(0,("failed to initialize group mapping\n"));
188 sid_to_string(string_sid
, &map
->sid
);
190 len
= tdb_pack(buf
, sizeof(buf
), "ddff",
191 map
->gid
, map
->sid_name_use
, map
->nt_name
, map
->comment
);
193 if (len
> sizeof(buf
))
196 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
198 kbuf
.dsize
= strlen(key
)+1;
202 if (tdb_store(tdb
, kbuf
, dbuf
, flag
) != 0) return False
;
207 /****************************************************************************
208 initialise first time the mapping list
209 ****************************************************************************/
210 BOOL
add_initial_entry(gid_t gid
, const char *sid
, enum SID_NAME_USE sid_name_use
, const char *nt_name
, const char *comment
)
214 if(!init_group_mapping()) {
215 DEBUG(0,("failed to initialize group mapping\n"));
220 if (!string_to_sid(&map
.sid
, sid
)) {
221 DEBUG(0, ("string_to_sid failed: %s", sid
));
225 map
.sid_name_use
=sid_name_use
;
226 fstrcpy(map
.nt_name
, nt_name
);
227 fstrcpy(map
.comment
, comment
);
229 return pdb_add_group_mapping_entry(&map
);
232 /****************************************************************************
233 Return the sid and the type of the unix group.
234 ****************************************************************************/
236 static BOOL
get_group_map_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
243 if(!init_group_mapping()) {
244 DEBUG(0,("failed to initialize group mapping\n"));
248 /* the key is the SID, retrieving is direct */
250 sid_to_string(string_sid
, &sid
);
251 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
254 kbuf
.dsize
= strlen(key
)+1;
256 dbuf
= tdb_fetch(tdb
, kbuf
);
260 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
261 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
);
263 SAFE_FREE(dbuf
.dptr
);
266 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
270 sid_copy(&map
->sid
, &sid
);
275 /****************************************************************************
276 Return the sid and the type of the unix group.
277 ****************************************************************************/
279 static BOOL
get_group_map_from_gid(gid_t gid
, GROUP_MAP
*map
)
281 TDB_DATA kbuf
, dbuf
, newkey
;
285 if(!init_group_mapping()) {
286 DEBUG(0,("failed to initialize group mapping\n"));
290 /* we need to enumerate the TDB to find the GID */
292 for (kbuf
= tdb_firstkey(tdb
);
294 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
296 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0) continue;
298 dbuf
= tdb_fetch(tdb
, kbuf
);
302 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
304 string_to_sid(&map
->sid
, string_sid
);
306 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
307 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
);
309 SAFE_FREE(dbuf
.dptr
);
312 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
317 SAFE_FREE(kbuf
.dptr
);
325 /****************************************************************************
326 Return the sid and the type of the unix group.
327 ****************************************************************************/
329 static BOOL
get_group_map_from_ntname(const char *name
, GROUP_MAP
*map
)
331 TDB_DATA kbuf
, dbuf
, newkey
;
335 if(!init_group_mapping()) {
336 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping\n"));
340 /* we need to enumerate the TDB to find the name */
342 for (kbuf
= tdb_firstkey(tdb
);
344 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
346 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0) continue;
348 dbuf
= tdb_fetch(tdb
, kbuf
);
352 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
354 string_to_sid(&map
->sid
, string_sid
);
356 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
357 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
);
359 SAFE_FREE(dbuf
.dptr
);
362 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
366 if (StrCaseCmp(name
, map
->nt_name
)==0) {
367 SAFE_FREE(kbuf
.dptr
);
375 /****************************************************************************
376 Remove a group mapping entry.
377 ****************************************************************************/
379 static BOOL
group_map_remove(DOM_SID sid
)
385 if(!init_group_mapping()) {
386 DEBUG(0,("failed to initialize group mapping\n"));
390 /* the key is the SID, retrieving is direct */
392 sid_to_string(string_sid
, &sid
);
393 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
396 kbuf
.dsize
= strlen(key
)+1;
398 dbuf
= tdb_fetch(tdb
, kbuf
);
402 SAFE_FREE(dbuf
.dptr
);
404 if(tdb_delete(tdb
, kbuf
) != TDB_SUCCESS
)
410 /****************************************************************************
411 Enumerate the group mapping.
412 ****************************************************************************/
414 static BOOL
enum_group_mapping(enum SID_NAME_USE sid_name_use
, GROUP_MAP
**rmap
,
415 int *num_entries
, BOOL unix_only
)
417 TDB_DATA kbuf
, dbuf
, newkey
;
425 if(!init_group_mapping()) {
426 DEBUG(0,("failed to initialize group mapping\n"));
433 for (kbuf
= tdb_firstkey(tdb
);
435 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
437 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0)
440 dbuf
= tdb_fetch(tdb
, kbuf
);
444 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
446 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
447 &map
.gid
, &map
.sid_name_use
, &map
.nt_name
, &map
.comment
);
449 SAFE_FREE(dbuf
.dptr
);
452 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
456 /* list only the type or everything if UNKNOWN */
457 if (sid_name_use
!=SID_NAME_UNKNOWN
&& sid_name_use
!=map
.sid_name_use
) {
458 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map
.nt_name
));
462 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1) {
463 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map
.nt_name
));
467 string_to_sid(&map
.sid
, string_sid
);
469 decode_sid_name_use(group_type
, map
.sid_name_use
);
470 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map
.nt_name
,group_type
));
472 mapt
= SMB_REALLOC_ARRAY((*rmap
), GROUP_MAP
, entries
+1);
474 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
481 mapt
[entries
].gid
= map
.gid
;
482 sid_copy( &mapt
[entries
].sid
, &map
.sid
);
483 mapt
[entries
].sid_name_use
= map
.sid_name_use
;
484 fstrcpy(mapt
[entries
].nt_name
, map
.nt_name
);
485 fstrcpy(mapt
[entries
].comment
, map
.comment
);
491 *num_entries
=entries
;
496 /* This operation happens on session setup, so it should better be fast. We
497 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
499 static NTSTATUS
alias_memberships(const DOM_SID
*sid
, DOM_SID
**sids
, int *num
)
501 fstring key
, string_sid
;
508 if (!init_group_mapping()) {
509 DEBUG(0,("failed to initialize group mapping\n"));
510 return NT_STATUS_ACCESS_DENIED
;
513 sid_to_string(string_sid
, sid
);
514 slprintf(key
, sizeof(key
), "%s%s", MEMBEROF_PREFIX
, string_sid
);
516 kbuf
.dsize
= strlen(key
)+1;
519 dbuf
= tdb_fetch(tdb
, kbuf
);
521 if (dbuf
.dptr
== NULL
) {
527 while (next_token(&p
, string_sid
, " ", sizeof(string_sid
))) {
531 if (!string_to_sid(&alias
, string_sid
))
534 add_sid_to_array(&alias
, sids
, num
);
537 return NT_STATUS_NO_MEMORY
;
540 SAFE_FREE(dbuf
.dptr
);
544 static BOOL
is_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
549 /* This feels the wrong way round, but the on-disk data structure
550 * dictates it this way. */
551 if (!NT_STATUS_IS_OK(alias_memberships(member
, &sids
, &num
)))
554 for (i
=0; i
<num
; i
++) {
555 if (sid_compare(alias
, &sids
[i
]) == 0) {
564 static NTSTATUS
add_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
570 char *new_memberstring
;
573 if(!init_group_mapping()) {
574 DEBUG(0,("failed to initialize group mapping\n"));
575 return NT_STATUS_ACCESS_DENIED
;
578 if (!get_group_map_from_sid(*alias
, &map
))
579 return NT_STATUS_NO_SUCH_ALIAS
;
581 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
582 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
583 return NT_STATUS_NO_SUCH_ALIAS
;
585 if (is_aliasmem(alias
, member
))
586 return NT_STATUS_MEMBER_IN_ALIAS
;
588 sid_to_string(string_sid
, member
);
589 slprintf(key
, sizeof(key
), "%s%s", MEMBEROF_PREFIX
, string_sid
);
591 kbuf
.dsize
= strlen(key
)+1;
594 dbuf
= tdb_fetch(tdb
, kbuf
);
596 sid_to_string(string_sid
, alias
);
598 if (dbuf
.dptr
!= NULL
) {
599 asprintf(&new_memberstring
, "%s %s", (char *)(dbuf
.dptr
),
602 new_memberstring
= SMB_STRDUP(string_sid
);
605 if (new_memberstring
== NULL
)
606 return NT_STATUS_NO_MEMORY
;
608 SAFE_FREE(dbuf
.dptr
);
609 dbuf
.dsize
= strlen(new_memberstring
)+1;
610 dbuf
.dptr
= new_memberstring
;
612 result
= tdb_store(tdb
, kbuf
, dbuf
, 0);
614 SAFE_FREE(new_memberstring
);
616 return (result
== 0 ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
);
619 struct aliasmem_closure
{
620 const DOM_SID
*alias
;
625 static int collect_aliasmem(TDB_CONTEXT
*tdb_ctx
, TDB_DATA key
, TDB_DATA data
,
628 struct aliasmem_closure
*closure
= (struct aliasmem_closure
*)state
;
630 fstring alias_string
;
632 if (strncmp(key
.dptr
, MEMBEROF_PREFIX
,
633 strlen(MEMBEROF_PREFIX
)) != 0)
638 while (next_token(&p
, alias_string
, " ", sizeof(alias_string
))) {
640 DOM_SID alias
, member
;
641 const char *member_string
;
644 if (!string_to_sid(&alias
, alias_string
))
647 if (sid_compare(closure
->alias
, &alias
) != 0)
650 /* Ok, we found the alias we're looking for in the membership
651 * list currently scanned. The key represents the alias
652 * member. Add that. */
654 member_string
= strchr(key
.dptr
, '/');
656 /* Above we tested for MEMBEROF_PREFIX which includes the
659 SMB_ASSERT(member_string
!= NULL
);
662 if (!string_to_sid(&member
, member_string
))
665 add_sid_to_array(&member
, closure
->sids
, closure
->num
);
671 static NTSTATUS
enum_aliasmem(const DOM_SID
*alias
, DOM_SID
**sids
, int *num
)
674 struct aliasmem_closure closure
;
676 if(!init_group_mapping()) {
677 DEBUG(0,("failed to initialize group mapping\n"));
678 return NT_STATUS_ACCESS_DENIED
;
681 if (!get_group_map_from_sid(*alias
, &map
))
682 return NT_STATUS_NO_SUCH_ALIAS
;
684 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
685 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
686 return NT_STATUS_NO_SUCH_ALIAS
;
691 closure
.alias
= alias
;
695 tdb_traverse(tdb
, collect_aliasmem
, &closure
);
699 static NTSTATUS
del_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
710 result
= alias_memberships(member
, &sids
, &num
);
712 if (!NT_STATUS_IS_OK(result
))
715 for (i
=0; i
<num
; i
++) {
716 if (sid_compare(&sids
[i
], alias
) == 0) {
724 return NT_STATUS_MEMBER_NOT_IN_ALIAS
;
728 sids
[i
] = sids
[num
-1];
732 sid_to_string(sid_string
, member
);
733 slprintf(key
, sizeof(key
), "%s%s", MEMBEROF_PREFIX
, sid_string
);
735 kbuf
.dsize
= strlen(key
)+1;
739 return tdb_delete(tdb
, kbuf
) == 0 ?
740 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
742 member_string
= SMB_STRDUP("");
744 if (member_string
== NULL
) {
746 return NT_STATUS_NO_MEMORY
;
749 for (i
=0; i
<num
; i
++) {
750 char *s
= member_string
;
752 sid_to_string(sid_string
, &sids
[i
]);
753 asprintf(&member_string
, "%s %s", s
, sid_string
);
756 if (member_string
== NULL
) {
758 return NT_STATUS_NO_MEMORY
;
762 dbuf
.dsize
= strlen(member_string
)+1;
763 dbuf
.dptr
= member_string
;
765 result
= tdb_store(tdb
, kbuf
, dbuf
, 0) == 0 ?
766 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
769 SAFE_FREE(member_string
);
776 * High level functions
777 * better to use them than the lower ones.
779 * we are checking if the group is in the mapping file
780 * and if the group is an existing unix group
784 /* get a domain group from it's SID */
786 BOOL
get_domain_group_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
791 if(!init_group_mapping()) {
792 DEBUG(0,("failed to initialize group mapping\n"));
796 DEBUG(10, ("get_domain_group_from_sid\n"));
798 /* if the group is NOT in the database, it CAN NOT be a domain group */
801 ret
= pdb_getgrsid(map
, sid
);
807 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
809 /* if it's not a domain group, continue */
810 if (map
->sid_name_use
!=SID_NAME_DOM_GRP
) {
814 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
820 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map
->gid
));
822 grp
= getgrgid(map
->gid
);
824 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
828 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
834 /* get a local (alias) group from it's SID */
836 BOOL
get_local_group_from_sid(DOM_SID
*sid
, GROUP_MAP
*map
)
840 if(!init_group_mapping()) {
841 DEBUG(0,("failed to initialize group mapping\n"));
845 /* The group is in the mapping table */
847 ret
= pdb_getgrsid(map
, *sid
);
853 if ( ( (map
->sid_name_use
!= SID_NAME_ALIAS
) &&
854 (map
->sid_name_use
!= SID_NAME_WKN_GRP
) )
856 || (getgrgid(map
->gid
) == NULL
) )
862 /* local groups only exist in the group mapping DB so this
866 /* the group isn't in the mapping table.
867 * make one based on the unix information */
871 sid_peek_rid(sid
, &alias_rid
);
872 map
->gid
=pdb_group_rid_to_gid(alias_rid
);
874 grp
= getgrgid(map
->gid
);
876 DEBUG(3,("get_local_group_from_sid: No unix group for [%ul]\n", map
->gid
));
880 map
->sid_name_use
=SID_NAME_ALIAS
;
882 fstrcpy(map
->nt_name
, grp
->gr_name
);
883 fstrcpy(map
->comment
, "Local Unix Group");
885 sid_copy(&map
->sid
, sid
);
892 /* get a builtin group from it's SID */
894 BOOL
get_builtin_group_from_sid(DOM_SID
*sid
, GROUP_MAP
*map
)
900 if(!init_group_mapping()) {
901 DEBUG(0,("failed to initialize group mapping\n"));
906 ret
= pdb_getgrsid(map
, *sid
);
912 if (map
->sid_name_use
!=SID_NAME_WKN_GRP
) {
920 if ( (grp
=getgrgid(map
->gid
)) == NULL
) {
929 /****************************************************************************
930 Returns a GROUP_MAP struct based on the gid.
931 ****************************************************************************/
932 BOOL
get_group_from_gid(gid_t gid
, GROUP_MAP
*map
)
937 if(!init_group_mapping()) {
938 DEBUG(0,("failed to initialize group mapping\n"));
942 if ( (grp
=getgrgid(gid
)) == NULL
)
946 ret
= pdb_getgrgid(map
, gid
);
957 /****************************************************************************
958 Create a UNIX group on demand.
959 ****************************************************************************/
961 int smb_create_group(char *unix_group
, gid_t
*new_gid
)
969 /* defer to scripts */
971 if ( *lp_addgroup_script() ) {
972 pstrcpy(add_script
, lp_addgroup_script());
973 pstring_sub(add_script
, "%g", unix_group
);
974 ret
= smbrun(add_script
, (new_gid
!=NULL
) ? &fd
: NULL
);
975 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script
,ret
));
983 if (read(fd
, output
, sizeof(output
)) > 0) {
984 *new_gid
= (gid_t
)strtoul(output
, NULL
, 10);
990 } else if ( winbind_create_group( unix_group
, NULL
) ) {
992 DEBUG(3,("smb_create_group: winbindd created the group (%s)\n",
998 struct group
*grp
= getgrnam(unix_group
);
1001 *new_gid
= grp
->gr_gid
;
1007 /****************************************************************************
1008 Delete a UNIX group on demand.
1009 ****************************************************************************/
1011 int smb_delete_group(char *unix_group
)
1016 /* defer to scripts */
1018 if ( *lp_delgroup_script() ) {
1019 pstrcpy(del_script
, lp_delgroup_script());
1020 pstring_sub(del_script
, "%g", unix_group
);
1021 ret
= smbrun(del_script
,NULL
);
1022 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script
,ret
));
1026 if ( winbind_delete_group( unix_group
) ) {
1027 DEBUG(3,("smb_delete_group: winbindd deleted the group (%s)\n",
1035 /****************************************************************************
1036 Set a user's primary UNIX group.
1037 ****************************************************************************/
1038 int smb_set_primary_group(const char *unix_group
, const char* unix_user
)
1043 /* defer to scripts */
1045 if ( *lp_setprimarygroup_script() ) {
1046 pstrcpy(add_script
, lp_setprimarygroup_script());
1047 all_string_sub(add_script
, "%g", unix_group
, sizeof(add_script
));
1048 all_string_sub(add_script
, "%u", unix_user
, sizeof(add_script
));
1049 ret
= smbrun(add_script
,NULL
);
1050 DEBUG(3,("smb_set_primary_group: "
1051 "Running the command `%s' gave %d\n",add_script
,ret
));
1057 if ( winbind_set_user_primary_group( unix_user
, unix_group
) ) {
1058 DEBUG(3,("smb_delete_group: winbindd set the group (%s) as the primary group for user (%s)\n",
1059 unix_group
, unix_user
));
1066 /****************************************************************************
1067 Add a user to a UNIX group.
1068 ****************************************************************************/
1070 int smb_add_user_group(char *unix_group
, char *unix_user
)
1075 /* defer to scripts */
1077 if ( *lp_addusertogroup_script() ) {
1078 pstrcpy(add_script
, lp_addusertogroup_script());
1079 pstring_sub(add_script
, "%g", unix_group
);
1080 pstring_sub(add_script
, "%u", unix_user
);
1081 ret
= smbrun(add_script
,NULL
);
1082 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script
,ret
));
1088 if ( winbind_add_user_to_group( unix_user
, unix_group
) ) {
1089 DEBUG(3,("smb_delete_group: winbindd added user (%s) to the group (%s)\n",
1090 unix_user
, unix_group
));
1097 /****************************************************************************
1098 Delete a user from a UNIX group
1099 ****************************************************************************/
1101 int smb_delete_user_group(const char *unix_group
, const char *unix_user
)
1106 /* defer to scripts */
1108 if ( *lp_deluserfromgroup_script() ) {
1109 pstrcpy(del_script
, lp_deluserfromgroup_script());
1110 pstring_sub(del_script
, "%g", unix_group
);
1111 pstring_sub(del_script
, "%u", unix_user
);
1112 ret
= smbrun(del_script
,NULL
);
1113 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script
,ret
));
1119 if ( winbind_remove_user_from_group( unix_user
, unix_group
) ) {
1120 DEBUG(3,("smb_delete_group: winbindd removed user (%s) from the group (%s)\n",
1121 unix_user
, unix_group
));
1129 NTSTATUS
pdb_default_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1132 return get_group_map_from_sid(sid
, map
) ?
1133 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1136 NTSTATUS
pdb_default_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1139 return get_group_map_from_gid(gid
, map
) ?
1140 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1143 NTSTATUS
pdb_default_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1146 return get_group_map_from_ntname(name
, map
) ?
1147 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1150 NTSTATUS
pdb_default_add_group_mapping_entry(struct pdb_methods
*methods
,
1153 return add_mapping_entry(map
, TDB_INSERT
) ?
1154 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1157 NTSTATUS
pdb_default_update_group_mapping_entry(struct pdb_methods
*methods
,
1160 return add_mapping_entry(map
, TDB_REPLACE
) ?
1161 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1164 NTSTATUS
pdb_default_delete_group_mapping_entry(struct pdb_methods
*methods
,
1167 return group_map_remove(sid
) ?
1168 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1171 NTSTATUS
pdb_default_enum_group_mapping(struct pdb_methods
*methods
,
1172 enum SID_NAME_USE sid_name_use
,
1173 GROUP_MAP
**rmap
, int *num_entries
,
1176 return enum_group_mapping(sid_name_use
, rmap
, num_entries
, unix_only
) ?
1177 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1180 NTSTATUS
pdb_default_find_alias(struct pdb_methods
*methods
,
1181 const char *name
, DOM_SID
*sid
)
1185 if (!pdb_getgrnam(&map
, name
))
1186 return NT_STATUS_NO_SUCH_ALIAS
;
1188 if ((map
.sid_name_use
!= SID_NAME_WKN_GRP
) &&
1189 (map
.sid_name_use
!= SID_NAME_ALIAS
))
1190 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
1192 sid_copy(sid
, &map
.sid
);
1193 return NT_STATUS_OK
;
1196 NTSTATUS
pdb_default_create_alias(struct pdb_methods
*methods
,
1197 const char *name
, uint32
*rid
)
1200 enum SID_NAME_USE type
;
1206 if (lookup_name(get_global_sam_name(), name
, &sid
, &type
))
1207 return NT_STATUS_ALIAS_EXISTS
;
1209 if (!winbind_allocate_rid(&new_rid
))
1210 return NT_STATUS_ACCESS_DENIED
;
1212 sid_copy(&sid
, get_global_sam_sid());
1213 sid_append_rid(&sid
, new_rid
);
1215 /* Here we allocate the gid */
1216 if (!winbind_sid_to_gid(&gid
, &sid
)) {
1217 DEBUG(0, ("Could not get gid for new RID\n"));
1218 return NT_STATUS_ACCESS_DENIED
;
1222 sid_copy(&map
.sid
, &sid
);
1223 map
.sid_name_use
= SID_NAME_ALIAS
;
1224 fstrcpy(map
.nt_name
, name
);
1225 fstrcpy(map
.comment
, "");
1227 if (!pdb_add_group_mapping_entry(&map
)) {
1228 DEBUG(0, ("Could not add group mapping entry for alias %s\n",
1230 return NT_STATUS_ACCESS_DENIED
;
1235 return NT_STATUS_OK
;
1238 NTSTATUS
pdb_default_delete_alias(struct pdb_methods
*methods
,
1241 return pdb_delete_group_mapping_entry(*sid
) ?
1242 NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1245 NTSTATUS
pdb_default_enum_aliases(struct pdb_methods
*methods
,
1247 uint32 start_idx
, uint32 max_entries
,
1248 uint32
*num_aliases
,
1249 struct acct_info
**info
)
1251 extern DOM_SID global_sid_Builtin
;
1255 enum SID_NAME_USE type
= SID_NAME_UNKNOWN
;
1257 if (sid_compare(sid
, get_global_sam_sid()) == 0)
1258 type
= SID_NAME_ALIAS
;
1260 if (sid_compare(sid
, &global_sid_Builtin
) == 0)
1261 type
= SID_NAME_WKN_GRP
;
1263 if (!pdb_enum_group_mapping(type
, &map
, &num_maps
, False
) ||
1270 if (start_idx
> num_maps
) {
1276 *num_aliases
= num_maps
- start_idx
;
1278 if (*num_aliases
> max_entries
)
1279 *num_aliases
= max_entries
;
1281 *info
= SMB_MALLOC_ARRAY(struct acct_info
, *num_aliases
);
1283 for (i
=0; i
<*num_aliases
; i
++) {
1284 fstrcpy((*info
)[i
].acct_name
, map
[i
+start_idx
].nt_name
);
1285 fstrcpy((*info
)[i
].acct_desc
, map
[i
+start_idx
].comment
);
1286 sid_peek_rid(&map
[i
].sid
, &(*info
)[i
+start_idx
].rid
);
1291 return NT_STATUS_OK
;
1294 NTSTATUS
pdb_default_get_aliasinfo(struct pdb_methods
*methods
,
1296 struct acct_info
*info
)
1300 if (!pdb_getgrsid(&map
, *sid
))
1301 return NT_STATUS_NO_SUCH_ALIAS
;
1303 fstrcpy(info
->acct_name
, map
.nt_name
);
1304 fstrcpy(info
->acct_desc
, map
.comment
);
1305 sid_peek_rid(&map
.sid
, &info
->rid
);
1306 return NT_STATUS_OK
;
1309 NTSTATUS
pdb_default_set_aliasinfo(struct pdb_methods
*methods
,
1311 struct acct_info
*info
)
1315 if (!pdb_getgrsid(&map
, *sid
))
1316 return NT_STATUS_NO_SUCH_ALIAS
;
1318 fstrcpy(map
.comment
, info
->acct_desc
);
1320 if (!pdb_update_group_mapping_entry(&map
))
1321 return NT_STATUS_ACCESS_DENIED
;
1323 return NT_STATUS_OK
;
1326 NTSTATUS
pdb_default_add_aliasmem(struct pdb_methods
*methods
,
1327 const DOM_SID
*alias
, const DOM_SID
*member
)
1329 return add_aliasmem(alias
, member
);
1332 NTSTATUS
pdb_default_del_aliasmem(struct pdb_methods
*methods
,
1333 const DOM_SID
*alias
, const DOM_SID
*member
)
1335 return del_aliasmem(alias
, member
);
1338 NTSTATUS
pdb_default_enum_aliasmem(struct pdb_methods
*methods
,
1339 const DOM_SID
*alias
, DOM_SID
**members
,
1342 return enum_aliasmem(alias
, members
, num_members
);
1345 NTSTATUS
pdb_default_alias_memberships(struct pdb_methods
*methods
,
1347 DOM_SID
**aliases
, int *num
)
1349 return alias_memberships(sid
, aliases
, num
);
1352 /**********************************************************************
1353 no ops for passdb backends that don't implement group mapping
1354 *********************************************************************/
1356 NTSTATUS
pdb_nop_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1359 return NT_STATUS_UNSUCCESSFUL
;
1362 NTSTATUS
pdb_nop_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1365 return NT_STATUS_UNSUCCESSFUL
;
1368 NTSTATUS
pdb_nop_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
1371 return NT_STATUS_UNSUCCESSFUL
;
1374 NTSTATUS
pdb_nop_add_group_mapping_entry(struct pdb_methods
*methods
,
1377 return NT_STATUS_UNSUCCESSFUL
;
1380 NTSTATUS
pdb_nop_update_group_mapping_entry(struct pdb_methods
*methods
,
1383 return NT_STATUS_UNSUCCESSFUL
;
1386 NTSTATUS
pdb_nop_delete_group_mapping_entry(struct pdb_methods
*methods
,
1389 return NT_STATUS_UNSUCCESSFUL
;
1392 NTSTATUS
pdb_nop_enum_group_mapping(struct pdb_methods
*methods
,
1393 enum SID_NAME_USE sid_name_use
,
1394 GROUP_MAP
**rmap
, int *num_entries
,
1397 return NT_STATUS_UNSUCCESSFUL
;