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 one_alias_membership(const DOM_SID *member,
500 DOM_SID **sids, int *num)
502 fstring key, string_sid;
506 if (!init_group_mapping()) {
507 DEBUG(0,("failed to initialize group mapping\n"));
508 return NT_STATUS_ACCESS_DENIED;
511 sid_to_string(string_sid, member);
512 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
514 kbuf.dsize = strlen(key)+1;
517 dbuf = tdb_fetch(tdb, kbuf);
519 if (dbuf.dptr == NULL) {
525 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
529 if (!string_to_sid(&alias, string_sid))
532 add_sid_to_array_unique(&alias, sids, num);
535 return NT_STATUS_NO_MEMORY;
538 SAFE_FREE(dbuf.dptr);
542 static NTSTATUS alias_memberships(const DOM_SID *members, int num_members,
543 DOM_SID **sids, int *num)
550 for (i=0; i<num_members; i++) {
551 NTSTATUS status = one_alias_membership(&members[i], sids, num);
552 if (!NT_STATUS_IS_OK(status))
558 static BOOL is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
563 /* This feels the wrong way round, but the on-disk data structure
564 * dictates it this way. */
565 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
568 for (i=0; i<num; i++) {
569 if (sid_compare(alias, &sids[i]) == 0) {
578 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
584 char *new_memberstring;
587 if(!init_group_mapping()) {
588 DEBUG(0,("failed to initialize group mapping\n"));
589 return NT_STATUS_ACCESS_DENIED;
592 if (!get_group_map_from_sid(*alias, &map))
593 return NT_STATUS_NO_SUCH_ALIAS;
595 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
596 (map.sid_name_use != SID_NAME_WKN_GRP) )
597 return NT_STATUS_NO_SUCH_ALIAS;
599 if (is_aliasmem(alias, member))
600 return NT_STATUS_MEMBER_IN_ALIAS;
602 sid_to_string(string_sid, member);
603 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
605 kbuf.dsize = strlen(key)+1;
608 dbuf = tdb_fetch(tdb, kbuf);
610 sid_to_string(string_sid, alias);
612 if (dbuf.dptr != NULL) {
613 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
616 new_memberstring = SMB_STRDUP(string_sid);
619 if (new_memberstring == NULL)
620 return NT_STATUS_NO_MEMORY;
622 SAFE_FREE(dbuf.dptr);
623 dbuf.dsize = strlen(new_memberstring)+1;
624 dbuf.dptr = new_memberstring;
626 result = tdb_store(tdb, kbuf, dbuf, 0);
628 SAFE_FREE(new_memberstring);
630 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
633 struct aliasmem_closure {
634 const DOM_SID *alias;
639 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
642 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
644 fstring alias_string;
646 if (strncmp(key.dptr, MEMBEROF_PREFIX,
647 strlen(MEMBEROF_PREFIX)) != 0)
652 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
654 DOM_SID alias, member;
655 const char *member_string;
658 if (!string_to_sid(&alias, alias_string))
661 if (sid_compare(closure->alias, &alias) != 0)
664 /* Ok, we found the alias we're looking for in the membership
665 * list currently scanned. The key represents the alias
666 * member. Add that. */
668 member_string = strchr(key.dptr, '/');
670 /* Above we tested for MEMBEROF_PREFIX which includes the
673 SMB_ASSERT(member_string != NULL);
676 if (!string_to_sid(&member, member_string))
679 add_sid_to_array(&member, closure->sids, closure->num);
685 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, int *num)
688 struct aliasmem_closure closure;
690 if(!init_group_mapping()) {
691 DEBUG(0,("failed to initialize group mapping\n"));
692 return NT_STATUS_ACCESS_DENIED;
695 if (!get_group_map_from_sid(*alias, &map))
696 return NT_STATUS_NO_SUCH_ALIAS;
698 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
699 (map.sid_name_use != SID_NAME_WKN_GRP) )
700 return NT_STATUS_NO_SUCH_ALIAS;
705 closure.alias = alias;
709 tdb_traverse(tdb, collect_aliasmem, &closure);
713 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
724 result = alias_memberships(member, 1, &sids, &num);
726 if (!NT_STATUS_IS_OK(result))
729 for (i=0; i<num; i++) {
730 if (sid_compare(&sids[i], alias) == 0) {
738 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
742 sids[i] = sids[num-1];
746 sid_to_string(sid_string, member);
747 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, sid_string);
749 kbuf.dsize = strlen(key)+1;
753 return tdb_delete(tdb, kbuf) == 0 ?
754 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
756 member_string = SMB_STRDUP("");
758 if (member_string == NULL) {
760 return NT_STATUS_NO_MEMORY;
763 for (i=0; i<num; i++) {
764 char *s = member_string;
766 sid_to_string(sid_string, &sids[i]);
767 asprintf(&member_string, "%s %s", s, sid_string);
770 if (member_string == NULL) {
772 return NT_STATUS_NO_MEMORY;
776 dbuf.dsize = strlen(member_string)+1;
777 dbuf.dptr = member_string;
779 result = tdb_store(tdb, kbuf, dbuf, 0) == 0 ?
780 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
783 SAFE_FREE(member_string);
790 * High level functions
791 * better to use them than the lower ones.
793 * we are checking if the group is in the mapping file
794 * and if the group is an existing unix group
798 /* get a domain group from it's SID */
800 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
805 if(!init_group_mapping()) {
806 DEBUG(0,("failed to initialize group mapping\n"));
810 DEBUG(10, ("get_domain_group_from_sid\n"));
812 /* if the group is NOT in the database, it CAN NOT be a domain group */
815 ret = pdb_getgrsid(map, sid);
821 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
823 /* if it's not a domain group, continue */
824 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
828 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
834 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
836 grp = getgrgid(map->gid);
838 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
842 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
848 /* get a local (alias) group from it's SID */
850 BOOL get_local_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
854 if(!init_group_mapping()) {
855 DEBUG(0,("failed to initialize group mapping\n"));
859 /* The group is in the mapping table */
861 ret = pdb_getgrsid(map, *sid);
867 if ( ( (map->sid_name_use != SID_NAME_ALIAS) &&
868 (map->sid_name_use != SID_NAME_WKN_GRP) )
870 || (getgrgid(map->gid) == NULL) )
876 /* local groups only exist in the group mapping DB so this
880 /* the group isn't in the mapping table.
881 * make one based on the unix information */
885 sid_peek_rid(sid, &alias_rid);
886 map->gid=pdb_group_rid_to_gid(alias_rid);
888 grp = getgrgid(map->gid);
890 DEBUG(3,("get_local_group_from_sid: No unix group for [%ul]\n", map->gid));
894 map->sid_name_use=SID_NAME_ALIAS;
896 fstrcpy(map->nt_name, grp->gr_name);
897 fstrcpy(map->comment, "Local Unix Group");
899 sid_copy(&map->sid, sid);
906 /* get a builtin group from it's SID */
908 BOOL get_builtin_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
914 if(!init_group_mapping()) {
915 DEBUG(0,("failed to initialize group mapping\n"));
920 ret = pdb_getgrsid(map, *sid);
926 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
934 if ( (grp=getgrgid(map->gid)) == NULL) {
943 /****************************************************************************
944 Returns a GROUP_MAP struct based on the gid.
945 ****************************************************************************/
946 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map)
951 if(!init_group_mapping()) {
952 DEBUG(0,("failed to initialize group mapping\n"));
956 if ( (grp=getgrgid(gid)) == NULL)
960 ret = pdb_getgrgid(map, gid);
971 /****************************************************************************
972 Create a UNIX group on demand.
973 ****************************************************************************/
975 int smb_create_group(char *unix_group, gid_t *new_gid)
983 /* defer to scripts */
985 if ( *lp_addgroup_script() ) {
986 pstrcpy(add_script, lp_addgroup_script());
987 pstring_sub(add_script, "%g", unix_group);
988 ret = smbrun(add_script, (new_gid!=NULL) ? &fd : NULL);
989 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
997 if (read(fd, output, sizeof(output)) > 0) {
998 *new_gid = (gid_t)strtoul(output, NULL, 10);
1004 } else if ( winbind_create_group( unix_group, NULL ) ) {
1006 DEBUG(3,("smb_create_group: winbindd created the group (%s)\n",
1011 if (*new_gid == 0) {
1012 struct group *grp = getgrnam(unix_group);
1015 *new_gid = grp->gr_gid;
1021 /****************************************************************************
1022 Delete a UNIX group on demand.
1023 ****************************************************************************/
1025 int smb_delete_group(char *unix_group)
1030 /* defer to scripts */
1032 if ( *lp_delgroup_script() ) {
1033 pstrcpy(del_script, lp_delgroup_script());
1034 pstring_sub(del_script, "%g", unix_group);
1035 ret = smbrun(del_script,NULL);
1036 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
1040 if ( winbind_delete_group( unix_group ) ) {
1041 DEBUG(3,("smb_delete_group: winbindd deleted the group (%s)\n",
1049 /****************************************************************************
1050 Set a user's primary UNIX group.
1051 ****************************************************************************/
1052 int smb_set_primary_group(const char *unix_group, const char* unix_user)
1057 /* defer to scripts */
1059 if ( *lp_setprimarygroup_script() ) {
1060 pstrcpy(add_script, lp_setprimarygroup_script());
1061 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
1062 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
1063 ret = smbrun(add_script,NULL);
1064 DEBUG(3,("smb_set_primary_group: "
1065 "Running the command `%s' gave %d\n",add_script,ret));
1071 if ( winbind_set_user_primary_group( unix_user, unix_group ) ) {
1072 DEBUG(3,("smb_delete_group: winbindd set the group (%s) as the primary group for user (%s)\n",
1073 unix_group, unix_user));
1080 /****************************************************************************
1081 Add a user to a UNIX group.
1082 ****************************************************************************/
1084 int smb_add_user_group(char *unix_group, char *unix_user)
1089 /* defer to scripts */
1091 if ( *lp_addusertogroup_script() ) {
1092 pstrcpy(add_script, lp_addusertogroup_script());
1093 pstring_sub(add_script, "%g", unix_group);
1094 pstring_sub(add_script, "%u", unix_user);
1095 ret = smbrun(add_script,NULL);
1096 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1102 if ( winbind_add_user_to_group( unix_user, unix_group ) ) {
1103 DEBUG(3,("smb_delete_group: winbindd added user (%s) to the group (%s)\n",
1104 unix_user, unix_group));
1111 /****************************************************************************
1112 Delete a user from a UNIX group
1113 ****************************************************************************/
1115 int smb_delete_user_group(const char *unix_group, const char *unix_user)
1120 /* defer to scripts */
1122 if ( *lp_deluserfromgroup_script() ) {
1123 pstrcpy(del_script, lp_deluserfromgroup_script());
1124 pstring_sub(del_script, "%g", unix_group);
1125 pstring_sub(del_script, "%u", unix_user);
1126 ret = smbrun(del_script,NULL);
1127 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
1133 if ( winbind_remove_user_from_group( unix_user, unix_group ) ) {
1134 DEBUG(3,("smb_delete_group: winbindd removed user (%s) from the group (%s)\n",
1135 unix_user, unix_group));
1143 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1146 return get_group_map_from_sid(sid, map) ?
1147 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1150 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1153 return get_group_map_from_gid(gid, map) ?
1154 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1157 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1160 return get_group_map_from_ntname(name, map) ?
1161 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1164 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
1167 return add_mapping_entry(map, TDB_INSERT) ?
1168 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1171 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
1174 return add_mapping_entry(map, TDB_REPLACE) ?
1175 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1178 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
1181 return group_map_remove(sid) ?
1182 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1185 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
1186 enum SID_NAME_USE sid_name_use,
1187 GROUP_MAP **rmap, int *num_entries,
1190 return enum_group_mapping(sid_name_use, rmap, num_entries, unix_only) ?
1191 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1194 NTSTATUS pdb_default_find_alias(struct pdb_methods *methods,
1195 const char *name, DOM_SID *sid)
1199 if (!pdb_getgrnam(&map, name))
1200 return NT_STATUS_NO_SUCH_ALIAS;
1202 if ((map.sid_name_use != SID_NAME_WKN_GRP) &&
1203 (map.sid_name_use != SID_NAME_ALIAS))
1204 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1206 sid_copy(sid, &map.sid);
1207 return NT_STATUS_OK;
1210 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
1211 const char *name, uint32 *rid)
1214 enum SID_NAME_USE type;
1220 if (lookup_name(get_global_sam_name(), name, &sid, &type))
1221 return NT_STATUS_ALIAS_EXISTS;
1223 if (!winbind_allocate_rid(&new_rid))
1224 return NT_STATUS_ACCESS_DENIED;
1226 sid_copy(&sid, get_global_sam_sid());
1227 sid_append_rid(&sid, new_rid);
1229 /* Here we allocate the gid */
1230 if (!winbind_sid_to_gid(&gid, &sid)) {
1231 DEBUG(0, ("Could not get gid for new RID\n"));
1232 return NT_STATUS_ACCESS_DENIED;
1236 sid_copy(&map.sid, &sid);
1237 map.sid_name_use = SID_NAME_ALIAS;
1238 fstrcpy(map.nt_name, name);
1239 fstrcpy(map.comment, "");
1241 if (!pdb_add_group_mapping_entry(&map)) {
1242 DEBUG(0, ("Could not add group mapping entry for alias %s\n",
1244 return NT_STATUS_ACCESS_DENIED;
1249 return NT_STATUS_OK;
1252 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
1255 return pdb_delete_group_mapping_entry(*sid) ?
1256 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1259 NTSTATUS pdb_default_enum_aliases(struct pdb_methods *methods,
1261 uint32 start_idx, uint32 max_entries,
1262 uint32 *num_aliases,
1263 struct acct_info **info)
1265 extern DOM_SID global_sid_Builtin;
1269 enum SID_NAME_USE type = SID_NAME_UNKNOWN;
1271 if (sid_compare(sid, get_global_sam_sid()) == 0)
1272 type = SID_NAME_ALIAS;
1274 if (sid_compare(sid, &global_sid_Builtin) == 0)
1275 type = SID_NAME_WKN_GRP;
1277 if (!pdb_enum_group_mapping(type, &map, &num_maps, False) ||
1284 if (start_idx > num_maps) {
1290 *num_aliases = num_maps - start_idx;
1292 if (*num_aliases > max_entries)
1293 *num_aliases = max_entries;
1295 *info = SMB_MALLOC_ARRAY(struct acct_info, *num_aliases);
1297 for (i=0; i<*num_aliases; i++) {
1298 fstrcpy((*info)[i].acct_name, map[i+start_idx].nt_name);
1299 fstrcpy((*info)[i].acct_desc, map[i+start_idx].comment);
1300 sid_peek_rid(&map[i].sid, &(*info)[i+start_idx].rid);
1305 return NT_STATUS_OK;
1308 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
1310 struct acct_info *info)
1314 if (!pdb_getgrsid(&map, *sid))
1315 return NT_STATUS_NO_SUCH_ALIAS;
1317 fstrcpy(info->acct_name, map.nt_name);
1318 fstrcpy(info->acct_desc, map.comment);
1319 sid_peek_rid(&map.sid, &info->rid);
1320 return NT_STATUS_OK;
1323 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
1325 struct acct_info *info)
1329 if (!pdb_getgrsid(&map, *sid))
1330 return NT_STATUS_NO_SUCH_ALIAS;
1332 fstrcpy(map.comment, info->acct_desc);
1334 if (!pdb_update_group_mapping_entry(&map))
1335 return NT_STATUS_ACCESS_DENIED;
1337 return NT_STATUS_OK;
1340 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
1341 const DOM_SID *alias, const DOM_SID *member)
1343 return add_aliasmem(alias, member);
1346 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
1347 const DOM_SID *alias, const DOM_SID *member)
1349 return del_aliasmem(alias, member);
1352 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
1353 const DOM_SID *alias, DOM_SID **members,
1356 return enum_aliasmem(alias, members, num_members);
1359 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
1360 const DOM_SID *members,
1362 DOM_SID **aliases, int *num)
1364 return alias_memberships(members, num_members, aliases, num);
1367 /**********************************************************************
1368 no ops for passdb backends that don't implement group mapping
1369 *********************************************************************/
1371 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1374 return NT_STATUS_UNSUCCESSFUL;
1377 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1380 return NT_STATUS_UNSUCCESSFUL;
1383 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1386 return NT_STATUS_UNSUCCESSFUL;
1389 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
1392 return NT_STATUS_UNSUCCESSFUL;
1395 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
1398 return NT_STATUS_UNSUCCESSFUL;
1401 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
1404 return NT_STATUS_UNSUCCESSFUL;
1407 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
1408 enum SID_NAME_USE sid_name_use,
1409 GROUP_MAP **rmap, int *num_entries,
1412 return NT_STATUS_UNSUCCESSFUL;