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/"
38 static struct sid_name_mapping {
39 enum SID_NAME_USE type;
41 } sid_name_use_strings[] = {
42 { SID_NAME_USE_NONE, "Not initialized" },
43 { SID_NAME_USER, "User" },
44 { SID_NAME_DOM_GRP, "Domain group" },
45 { SID_NAME_DOMAIN, "Domain" },
46 { SID_NAME_ALIAS, "Local group" },
47 { SID_NAME_WKN_GRP, "Builtin group" },
48 { SID_NAME_DELETED, "Deleted" },
49 { SID_NAME_INVALID, "Invalid" },
53 /****************************************************************************
54 dump the mapping group mapping to a text file
55 ****************************************************************************/
56 const char *decode_sid_name_use(enum SID_NAME_USE name_use)
58 struct sid_name_mapping *m;
60 for (m = sid_name_use_strings; m->name != NULL; m++) {
61 if (m->type == name_use)
65 return "Unknown type";
68 /****************************************************************************
69 initialise first time the mapping list - called from init_group_mapping()
70 ****************************************************************************/
71 static BOOL default_group_mapping(void)
80 /* Add the Wellknown groups */
82 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP, "Administrators", "");
83 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP, "Users", "");
84 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP, "Guests", "");
85 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP, "Power Users", "");
86 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP, "Account Operators", "");
87 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP, "System Operators", "");
88 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP, "Print Operators", "");
89 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP, "Backup Operators", "");
90 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP, "Replicators", "");
92 /* Add the defaults domain groups */
94 sid_copy(&sid_admins, get_global_sam_sid());
95 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
96 sid_to_string(str_admins, &sid_admins);
97 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "");
99 sid_copy(&sid_users, get_global_sam_sid());
100 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
101 sid_to_string(str_users, &sid_users);
102 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "");
104 sid_copy(&sid_guests, get_global_sam_sid());
105 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
106 sid_to_string(str_guests, &sid_guests);
107 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "");
112 /****************************************************************************
113 Open the group mapping tdb.
114 ****************************************************************************/
116 static BOOL init_group_mapping(void)
118 const char *vstring = "INFO/version";
123 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
125 DEBUG(0,("Failed to open group mapping database\n"));
129 /* handle a Samba upgrade */
130 tdb_lock_bystring(tdb, vstring, 0);
132 /* Cope with byte-reversed older versions of the db. */
133 vers_id = tdb_fetch_int32(tdb, vstring);
134 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
135 /* Written on a bigendian machine with old fetch_int code. Save as le. */
136 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
137 vers_id = DATABASE_VERSION_V2;
140 if (vers_id != DATABASE_VERSION_V2) {
141 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
142 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
145 tdb_unlock_bystring(tdb, vstring);
147 /* write a list of default groups */
148 if(!default_group_mapping())
154 /****************************************************************************
155 ****************************************************************************/
156 static BOOL add_mapping_entry(GROUP_MAP *map, int flag)
160 fstring string_sid="";
163 if(!init_group_mapping()) {
164 DEBUG(0,("failed to initialize group mapping\n"));
168 sid_to_string(string_sid, &map->sid);
170 len = tdb_pack(buf, sizeof(buf), "ddff",
171 map->gid, map->sid_name_use, map->nt_name, map->comment);
173 if (len > sizeof(buf))
176 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
178 kbuf.dsize = strlen(key)+1;
182 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
187 /****************************************************************************
188 initialise first time the mapping list
189 ****************************************************************************/
190 BOOL add_initial_entry(gid_t gid, const char *sid, enum SID_NAME_USE sid_name_use, const char *nt_name, const char *comment)
194 if(!init_group_mapping()) {
195 DEBUG(0,("failed to initialize group mapping\n"));
200 if (!string_to_sid(&map.sid, sid)) {
201 DEBUG(0, ("string_to_sid failed: %s", sid));
205 map.sid_name_use=sid_name_use;
206 fstrcpy(map.nt_name, nt_name);
207 fstrcpy(map.comment, comment);
209 return pdb_add_group_mapping_entry(&map);
212 /****************************************************************************
213 Return the sid and the type of the unix group.
214 ****************************************************************************/
216 static BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
223 if(!init_group_mapping()) {
224 DEBUG(0,("failed to initialize group mapping\n"));
228 /* the key is the SID, retrieving is direct */
230 sid_to_string(string_sid, &sid);
231 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
234 kbuf.dsize = strlen(key)+1;
236 dbuf = tdb_fetch(tdb, kbuf);
240 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
241 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
243 SAFE_FREE(dbuf.dptr);
246 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
250 sid_copy(&map->sid, &sid);
255 /****************************************************************************
256 Return the sid and the type of the unix group.
257 ****************************************************************************/
259 static BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
261 TDB_DATA kbuf, dbuf, newkey;
265 if(!init_group_mapping()) {
266 DEBUG(0,("failed to initialize group mapping\n"));
270 /* we need to enumerate the TDB to find the GID */
272 for (kbuf = tdb_firstkey(tdb);
274 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
276 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
278 dbuf = tdb_fetch(tdb, kbuf);
282 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
284 string_to_sid(&map->sid, string_sid);
286 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
287 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
289 SAFE_FREE(dbuf.dptr);
292 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
297 SAFE_FREE(kbuf.dptr);
305 /****************************************************************************
306 Return the sid and the type of the unix group.
307 ****************************************************************************/
309 static BOOL get_group_map_from_ntname(const char *name, GROUP_MAP *map)
311 TDB_DATA kbuf, dbuf, newkey;
315 if(!init_group_mapping()) {
316 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping\n"));
320 /* we need to enumerate the TDB to find the name */
322 for (kbuf = tdb_firstkey(tdb);
324 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
326 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
328 dbuf = tdb_fetch(tdb, kbuf);
332 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
334 string_to_sid(&map->sid, string_sid);
336 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
337 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
339 SAFE_FREE(dbuf.dptr);
342 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
346 if (StrCaseCmp(name, map->nt_name)==0) {
347 SAFE_FREE(kbuf.dptr);
355 /****************************************************************************
356 Remove a group mapping entry.
357 ****************************************************************************/
359 static BOOL group_map_remove(const DOM_SID *sid)
365 if(!init_group_mapping()) {
366 DEBUG(0,("failed to initialize group mapping\n"));
370 /* the key is the SID, retrieving is direct */
372 sid_to_string(string_sid, sid);
373 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
376 kbuf.dsize = strlen(key)+1;
378 dbuf = tdb_fetch(tdb, kbuf);
382 SAFE_FREE(dbuf.dptr);
384 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
390 /****************************************************************************
391 Enumerate the group mapping.
392 ****************************************************************************/
394 static BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
395 size_t *p_num_entries, BOOL unix_only)
397 TDB_DATA kbuf, dbuf, newkey;
404 if(!init_group_mapping()) {
405 DEBUG(0,("failed to initialize group mapping\n"));
412 for (kbuf = tdb_firstkey(tdb);
414 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
416 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
419 dbuf = tdb_fetch(tdb, kbuf);
423 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
425 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
426 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
428 SAFE_FREE(dbuf.dptr);
431 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
435 /* list only the type or everything if UNKNOWN */
436 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
437 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
441 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
442 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
446 string_to_sid(&map.sid, string_sid);
448 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n",
449 map.nt_name, decode_sid_name_use(map.sid_name_use)));
451 mapt= SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
453 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
460 mapt[entries].gid = map.gid;
461 sid_copy( &mapt[entries].sid, &map.sid);
462 mapt[entries].sid_name_use = map.sid_name_use;
463 fstrcpy(mapt[entries].nt_name, map.nt_name);
464 fstrcpy(mapt[entries].comment, map.comment);
470 *p_num_entries=entries;
475 /* This operation happens on session setup, so it should better be fast. We
476 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
478 static NTSTATUS one_alias_membership(const DOM_SID *member,
479 DOM_SID **sids, size_t *num)
481 fstring key, string_sid;
485 if (!init_group_mapping()) {
486 DEBUG(0,("failed to initialize group mapping\n"));
487 return NT_STATUS_ACCESS_DENIED;
490 sid_to_string(string_sid, member);
491 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
493 kbuf.dsize = strlen(key)+1;
496 dbuf = tdb_fetch(tdb, kbuf);
498 if (dbuf.dptr == NULL) {
504 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
508 if (!string_to_sid(&alias, string_sid))
511 add_sid_to_array_unique(NULL, &alias, sids, num);
514 return NT_STATUS_NO_MEMORY;
517 SAFE_FREE(dbuf.dptr);
521 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
522 DOM_SID **sids, size_t *num)
529 for (i=0; i<num_members; i++) {
530 NTSTATUS status = one_alias_membership(&members[i], sids, num);
531 if (!NT_STATUS_IS_OK(status))
537 static BOOL is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
542 /* This feels the wrong way round, but the on-disk data structure
543 * dictates it this way. */
544 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
547 for (i=0; i<num; i++) {
548 if (sid_compare(alias, &sids[i]) == 0) {
557 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
563 char *new_memberstring;
566 if(!init_group_mapping()) {
567 DEBUG(0,("failed to initialize group mapping\n"));
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;
578 if (is_aliasmem(alias, member))
579 return NT_STATUS_MEMBER_IN_ALIAS;
581 sid_to_string(string_sid, member);
582 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
584 kbuf.dsize = strlen(key)+1;
587 dbuf = tdb_fetch(tdb, kbuf);
589 sid_to_string(string_sid, alias);
591 if (dbuf.dptr != NULL) {
592 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
595 new_memberstring = SMB_STRDUP(string_sid);
598 if (new_memberstring == NULL)
599 return NT_STATUS_NO_MEMORY;
601 SAFE_FREE(dbuf.dptr);
602 dbuf.dsize = strlen(new_memberstring)+1;
603 dbuf.dptr = new_memberstring;
605 result = tdb_store(tdb, kbuf, dbuf, 0);
607 SAFE_FREE(new_memberstring);
609 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
612 struct aliasmem_closure {
613 const DOM_SID *alias;
618 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
621 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
623 fstring alias_string;
625 if (strncmp(key.dptr, MEMBEROF_PREFIX,
626 strlen(MEMBEROF_PREFIX)) != 0)
631 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
633 DOM_SID alias, member;
634 const char *member_string;
637 if (!string_to_sid(&alias, alias_string))
640 if (sid_compare(closure->alias, &alias) != 0)
643 /* Ok, we found the alias we're looking for in the membership
644 * list currently scanned. The key represents the alias
645 * member. Add that. */
647 member_string = strchr(key.dptr, '/');
649 /* Above we tested for MEMBEROF_PREFIX which includes the
652 SMB_ASSERT(member_string != NULL);
655 if (!string_to_sid(&member, member_string))
658 add_sid_to_array(NULL, &member, closure->sids, closure->num);
664 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
667 struct aliasmem_closure closure;
669 if(!init_group_mapping()) {
670 DEBUG(0,("failed to initialize group mapping\n"));
671 return NT_STATUS_ACCESS_DENIED;
674 if (!get_group_map_from_sid(*alias, &map))
675 return NT_STATUS_NO_SUCH_ALIAS;
677 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
678 (map.sid_name_use != SID_NAME_WKN_GRP) )
679 return NT_STATUS_NO_SUCH_ALIAS;
684 closure.alias = alias;
688 tdb_traverse(tdb, collect_aliasmem, &closure);
692 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
703 result = alias_memberships(member, 1, &sids, &num);
705 if (!NT_STATUS_IS_OK(result))
708 for (i=0; i<num; i++) {
709 if (sid_compare(&sids[i], alias) == 0) {
717 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
721 sids[i] = sids[num-1];
725 sid_to_string(sid_string, member);
726 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, sid_string);
728 kbuf.dsize = strlen(key)+1;
732 return tdb_delete(tdb, kbuf) == 0 ?
733 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
735 member_string = SMB_STRDUP("");
737 if (member_string == NULL) {
739 return NT_STATUS_NO_MEMORY;
742 for (i=0; i<num; i++) {
743 char *s = member_string;
745 sid_to_string(sid_string, &sids[i]);
746 asprintf(&member_string, "%s %s", s, sid_string);
749 if (member_string == NULL) {
751 return NT_STATUS_NO_MEMORY;
755 dbuf.dsize = strlen(member_string)+1;
756 dbuf.dptr = member_string;
758 result = tdb_store(tdb, kbuf, dbuf, 0) == 0 ?
759 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
762 SAFE_FREE(member_string);
769 * High level functions
770 * better to use them than the lower ones.
772 * we are checking if the group is in the mapping file
773 * and if the group is an existing unix group
777 /* get a domain group from it's SID */
779 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
784 if(!init_group_mapping()) {
785 DEBUG(0,("failed to initialize group mapping\n"));
789 DEBUG(10, ("get_domain_group_from_sid\n"));
791 /* if the group is NOT in the database, it CAN NOT be a domain group */
794 ret = pdb_getgrsid(map, sid);
800 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
802 /* if it's not a domain group, continue */
803 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
807 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
813 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
815 grp = getgrgid(map->gid);
817 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
821 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
827 /* get a local (alias) group from it's SID */
829 BOOL get_local_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
833 if(!init_group_mapping()) {
834 DEBUG(0,("failed to initialize group mapping\n"));
838 /* The group is in the mapping table */
840 ret = pdb_getgrsid(map, *sid);
846 if ( ( (map->sid_name_use != SID_NAME_ALIAS) &&
847 (map->sid_name_use != SID_NAME_WKN_GRP) )
849 || (getgrgid(map->gid) == NULL) )
855 /* local groups only exist in the group mapping DB so this
859 /* the group isn't in the mapping table.
860 * make one based on the unix information */
864 sid_peek_rid(sid, &alias_rid);
865 map->gid=pdb_group_rid_to_gid(alias_rid);
867 grp = getgrgid(map->gid);
869 DEBUG(3,("get_local_group_from_sid: No unix group for [%ul]\n", map->gid));
873 map->sid_name_use=SID_NAME_ALIAS;
875 fstrcpy(map->nt_name, grp->gr_name);
876 fstrcpy(map->comment, "Local Unix Group");
878 sid_copy(&map->sid, sid);
885 /* get a builtin group from it's SID */
887 BOOL get_builtin_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
892 if(!init_group_mapping()) {
893 DEBUG(0,("failed to initialize group mapping\n"));
898 ret = pdb_getgrsid(map, *sid);
904 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
912 if ( getgrgid(map->gid) == NULL) {
921 /****************************************************************************
922 Returns a GROUP_MAP struct based on the gid.
923 ****************************************************************************/
924 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map)
928 if(!init_group_mapping()) {
929 DEBUG(0,("failed to initialize group mapping\n"));
933 if ( getgrgid(gid) == NULL)
937 ret = pdb_getgrgid(map, gid);
947 /****************************************************************************
948 Create a UNIX group on demand.
949 ****************************************************************************/
951 int smb_create_group(char *unix_group, gid_t *new_gid)
959 /* defer to scripts */
961 if ( *lp_addgroup_script() ) {
962 pstrcpy(add_script, lp_addgroup_script());
963 pstring_sub(add_script, "%g", unix_group);
964 ret = smbrun(add_script, (new_gid!=NULL) ? &fd : NULL);
965 DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
973 if (read(fd, output, sizeof(output)) > 0) {
974 *new_gid = (gid_t)strtoul(output, NULL, 10);
983 struct group *grp = getgrnam(unix_group);
986 *new_gid = grp->gr_gid;
992 /****************************************************************************
993 Delete a UNIX group on demand.
994 ****************************************************************************/
996 int smb_delete_group(char *unix_group)
1001 /* defer to scripts */
1003 if ( *lp_delgroup_script() ) {
1004 pstrcpy(del_script, lp_delgroup_script());
1005 pstring_sub(del_script, "%g", unix_group);
1006 ret = smbrun(del_script,NULL);
1007 DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
1014 /****************************************************************************
1015 Set a user's primary UNIX group.
1016 ****************************************************************************/
1017 int smb_set_primary_group(const char *unix_group, const char* unix_user)
1022 /* defer to scripts */
1024 if ( *lp_setprimarygroup_script() ) {
1025 pstrcpy(add_script, lp_setprimarygroup_script());
1026 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
1027 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
1028 ret = smbrun(add_script,NULL);
1029 flush_pwnam_cache();
1030 DEBUG(ret ? 0 : 3,("smb_set_primary_group: "
1031 "Running the command `%s' gave %d\n",add_script,ret));
1038 /****************************************************************************
1039 Add a user to a UNIX group.
1040 ****************************************************************************/
1042 int smb_add_user_group(char *unix_group, char *unix_user)
1047 /* defer to scripts */
1049 if ( *lp_addusertogroup_script() ) {
1050 pstrcpy(add_script, lp_addusertogroup_script());
1051 pstring_sub(add_script, "%g", unix_group);
1052 pstring_sub(add_script, "%u", unix_user);
1053 ret = smbrun(add_script,NULL);
1054 DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1061 /****************************************************************************
1062 Delete a user from a UNIX group
1063 ****************************************************************************/
1065 int smb_delete_user_group(const char *unix_group, const char *unix_user)
1070 /* defer to scripts */
1072 if ( *lp_deluserfromgroup_script() ) {
1073 pstrcpy(del_script, lp_deluserfromgroup_script());
1074 pstring_sub(del_script, "%g", unix_group);
1075 pstring_sub(del_script, "%u", unix_user);
1076 ret = smbrun(del_script,NULL);
1077 DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
1085 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1088 return get_group_map_from_sid(sid, map) ?
1089 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1092 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1095 return get_group_map_from_gid(gid, map) ?
1096 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1099 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1102 return get_group_map_from_ntname(name, map) ?
1103 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1106 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
1109 return add_mapping_entry(map, TDB_INSERT) ?
1110 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1113 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
1116 return add_mapping_entry(map, TDB_REPLACE) ?
1117 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1120 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
1123 return group_map_remove(&sid) ?
1124 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1127 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
1128 enum SID_NAME_USE sid_name_use,
1129 GROUP_MAP **pp_rmap, size_t *p_num_entries,
1132 return enum_group_mapping(sid_name_use, pp_rmap, p_num_entries, unix_only) ?
1133 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1136 NTSTATUS pdb_default_find_alias(struct pdb_methods *methods,
1137 const char *name, DOM_SID *sid)
1141 if (!pdb_getgrnam(&map, name))
1142 return NT_STATUS_NO_SUCH_ALIAS;
1144 if ((map.sid_name_use != SID_NAME_WKN_GRP) &&
1145 (map.sid_name_use != SID_NAME_ALIAS))
1146 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1148 sid_copy(sid, &map.sid);
1149 return NT_STATUS_OK;
1152 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
1153 const char *name, uint32 *rid)
1156 enum SID_NAME_USE type;
1162 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1164 if (mem_ctx == NULL) {
1165 return NT_STATUS_NO_MEMORY;
1168 exists = lookup_name(mem_ctx, name, LOOKUP_NAME_ISOLATED,
1169 NULL, NULL, &sid, &type);
1170 talloc_free(mem_ctx);
1173 return NT_STATUS_ALIAS_EXISTS;
1176 if (!winbind_allocate_rid_and_gid(&new_rid, &gid))
1177 return NT_STATUS_ACCESS_DENIED;
1179 sid_copy(&sid, get_global_sam_sid());
1180 sid_append_rid(&sid, new_rid);
1183 sid_copy(&map.sid, &sid);
1184 map.sid_name_use = SID_NAME_ALIAS;
1185 fstrcpy(map.nt_name, name);
1186 fstrcpy(map.comment, "");
1188 if (!pdb_add_group_mapping_entry(&map)) {
1189 DEBUG(0, ("Could not add group mapping entry for alias %s\n",
1191 return NT_STATUS_ACCESS_DENIED;
1196 return NT_STATUS_OK;
1199 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
1202 return pdb_delete_group_mapping_entry(*sid) ?
1203 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1206 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
1208 struct acct_info *info)
1212 if (!pdb_getgrsid(&map, *sid))
1213 return NT_STATUS_NO_SUCH_ALIAS;
1215 fstrcpy(info->acct_name, map.nt_name);
1216 fstrcpy(info->acct_desc, map.comment);
1217 sid_peek_rid(&map.sid, &info->rid);
1218 return NT_STATUS_OK;
1221 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
1223 struct acct_info *info)
1227 if (!pdb_getgrsid(&map, *sid))
1228 return NT_STATUS_NO_SUCH_ALIAS;
1230 fstrcpy(map.comment, info->acct_desc);
1232 if (!pdb_update_group_mapping_entry(&map))
1233 return NT_STATUS_ACCESS_DENIED;
1235 return NT_STATUS_OK;
1238 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
1239 const DOM_SID *alias, const DOM_SID *member)
1241 return add_aliasmem(alias, member);
1244 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
1245 const DOM_SID *alias, const DOM_SID *member)
1247 return del_aliasmem(alias, member);
1250 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
1251 const DOM_SID *alias, DOM_SID **pp_members,
1252 size_t *p_num_members)
1254 return enum_aliasmem(alias, pp_members, p_num_members);
1257 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
1258 TALLOC_CTX *mem_ctx,
1259 const DOM_SID *domain_sid,
1260 const DOM_SID *members,
1262 uint32 **pp_alias_rids,
1263 size_t *p_num_alias_rids)
1265 DOM_SID *alias_sids;
1266 size_t i, num_alias_sids;
1272 result = alias_memberships(members, num_members,
1273 &alias_sids, &num_alias_sids);
1275 if (!NT_STATUS_IS_OK(result))
1278 *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32, num_alias_sids);
1279 if (*pp_alias_rids == NULL)
1280 return NT_STATUS_NO_MEMORY;
1282 *p_num_alias_rids = 0;
1284 for (i=0; i<num_alias_sids; i++) {
1285 if (!sid_peek_check_rid(domain_sid, &alias_sids[i],
1286 &(*pp_alias_rids)[*p_num_alias_rids]))
1288 *p_num_alias_rids += 1;
1291 SAFE_FREE(alias_sids);
1293 return NT_STATUS_OK;
1296 /**********************************************************************
1297 no ops for passdb backends that don't implement group mapping
1298 *********************************************************************/
1300 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1303 return NT_STATUS_UNSUCCESSFUL;
1306 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1309 return NT_STATUS_UNSUCCESSFUL;
1312 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1315 return NT_STATUS_UNSUCCESSFUL;
1318 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
1321 return NT_STATUS_UNSUCCESSFUL;
1324 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
1327 return NT_STATUS_UNSUCCESSFUL;
1330 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
1333 return NT_STATUS_UNSUCCESSFUL;
1336 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
1337 enum SID_NAME_USE sid_name_use,
1338 GROUP_MAP **rmap, size_t *num_entries,
1341 return NT_STATUS_UNSUCCESSFUL;
1344 /****************************************************************************
1345 These need to be redirected through pdb_interface.c
1346 ****************************************************************************/
1347 BOOL pdb_get_dom_grp_info(const DOM_SID *sid, struct acct_info *info)
1353 res = get_domain_group_from_sid(*sid, &map);
1359 fstrcpy(info->acct_name, map.nt_name);
1360 fstrcpy(info->acct_desc, map.comment);
1361 sid_peek_rid(sid, &info->rid);
1365 BOOL pdb_set_dom_grp_info(const DOM_SID *sid, const struct acct_info *info)
1369 if (!get_domain_group_from_sid(*sid, &map))
1372 fstrcpy(map.nt_name, info->acct_name);
1373 fstrcpy(map.comment, info->acct_desc);
1375 return pdb_update_group_mapping_entry(&map);