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 /****************************************************************************
39 initialise first time the mapping list - called from init_group_mapping()
40 ****************************************************************************/
41 static BOOL default_group_mapping(void)
50 /* Add the Wellknown groups */
52 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP, "Administrators", "");
53 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP, "Users", "");
54 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP, "Guests", "");
55 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP, "Power Users", "");
56 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP, "Account Operators", "");
57 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP, "System Operators", "");
58 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP, "Print Operators", "");
59 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP, "Backup Operators", "");
60 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP, "Replicators", "");
62 /* Add the defaults domain groups */
64 sid_copy(&sid_admins, get_global_sam_sid());
65 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
66 sid_to_string(str_admins, &sid_admins);
67 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "");
69 sid_copy(&sid_users, get_global_sam_sid());
70 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
71 sid_to_string(str_users, &sid_users);
72 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "");
74 sid_copy(&sid_guests, get_global_sam_sid());
75 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
76 sid_to_string(str_guests, &sid_guests);
77 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "");
82 /****************************************************************************
83 Open the group mapping tdb.
84 ****************************************************************************/
86 static BOOL init_group_mapping(void)
88 const char *vstring = "INFO/version";
93 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
95 DEBUG(0,("Failed to open group mapping database\n"));
99 /* handle a Samba upgrade */
100 tdb_lock_bystring(tdb, vstring, 0);
102 /* Cope with byte-reversed older versions of the db. */
103 vers_id = tdb_fetch_int32(tdb, vstring);
104 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
105 /* Written on a bigendian machine with old fetch_int code. Save as le. */
106 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
107 vers_id = DATABASE_VERSION_V2;
110 if (vers_id != DATABASE_VERSION_V2) {
111 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
112 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
115 tdb_unlock_bystring(tdb, vstring);
117 /* write a list of default groups */
118 if(!default_group_mapping())
124 /****************************************************************************
125 ****************************************************************************/
126 static BOOL add_mapping_entry(GROUP_MAP *map, int flag)
130 fstring string_sid="";
133 if(!init_group_mapping()) {
134 DEBUG(0,("failed to initialize group mapping\n"));
138 sid_to_string(string_sid, &map->sid);
140 len = tdb_pack(buf, sizeof(buf), "ddff",
141 map->gid, map->sid_name_use, map->nt_name, map->comment);
143 if (len > sizeof(buf))
146 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
148 kbuf.dsize = strlen(key)+1;
152 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
157 /****************************************************************************
158 initialise first time the mapping list
159 ****************************************************************************/
160 BOOL add_initial_entry(gid_t gid, const char *sid, enum SID_NAME_USE sid_name_use, const char *nt_name, const char *comment)
164 if(!init_group_mapping()) {
165 DEBUG(0,("failed to initialize group mapping\n"));
170 if (!string_to_sid(&map.sid, sid)) {
171 DEBUG(0, ("string_to_sid failed: %s", sid));
175 map.sid_name_use=sid_name_use;
176 fstrcpy(map.nt_name, nt_name);
177 fstrcpy(map.comment, comment);
179 return pdb_add_group_mapping_entry(&map);
182 /****************************************************************************
183 Return the sid and the type of the unix group.
184 ****************************************************************************/
186 static BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
193 if(!init_group_mapping()) {
194 DEBUG(0,("failed to initialize group mapping\n"));
198 /* the key is the SID, retrieving is direct */
200 sid_to_string(string_sid, &sid);
201 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
204 kbuf.dsize = strlen(key)+1;
206 dbuf = tdb_fetch(tdb, kbuf);
210 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
211 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
213 SAFE_FREE(dbuf.dptr);
216 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
220 sid_copy(&map->sid, &sid);
225 /****************************************************************************
226 Return the sid and the type of the unix group.
227 ****************************************************************************/
229 static BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
231 TDB_DATA kbuf, dbuf, newkey;
235 if(!init_group_mapping()) {
236 DEBUG(0,("failed to initialize group mapping\n"));
240 /* we need to enumerate the TDB to find the GID */
242 for (kbuf = tdb_firstkey(tdb);
244 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
246 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
248 dbuf = tdb_fetch(tdb, kbuf);
252 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
254 string_to_sid(&map->sid, string_sid);
256 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
257 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
259 SAFE_FREE(dbuf.dptr);
262 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
267 SAFE_FREE(kbuf.dptr);
275 /****************************************************************************
276 Return the sid and the type of the unix group.
277 ****************************************************************************/
279 static BOOL get_group_map_from_ntname(const char *name, GROUP_MAP *map)
281 TDB_DATA kbuf, dbuf, newkey;
285 if(!init_group_mapping()) {
286 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping\n"));
290 /* we need to enumerate the TDB to find the name */
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_ntname: tdb_unpack failure\n"));
316 if (StrCaseCmp(name, map->nt_name)==0) {
317 SAFE_FREE(kbuf.dptr);
325 /****************************************************************************
326 Remove a group mapping entry.
327 ****************************************************************************/
329 static BOOL group_map_remove(const DOM_SID *sid)
335 if(!init_group_mapping()) {
336 DEBUG(0,("failed to initialize group mapping\n"));
340 /* the key is the SID, retrieving is direct */
342 sid_to_string(string_sid, sid);
343 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
346 kbuf.dsize = strlen(key)+1;
348 dbuf = tdb_fetch(tdb, kbuf);
352 SAFE_FREE(dbuf.dptr);
354 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
360 /****************************************************************************
361 Enumerate the group mapping.
362 ****************************************************************************/
364 static BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
365 size_t *p_num_entries, BOOL unix_only)
367 TDB_DATA kbuf, dbuf, newkey;
374 if(!init_group_mapping()) {
375 DEBUG(0,("failed to initialize group mapping\n"));
382 for (kbuf = tdb_firstkey(tdb);
384 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
386 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
389 dbuf = tdb_fetch(tdb, kbuf);
393 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
395 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
396 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
398 SAFE_FREE(dbuf.dptr);
401 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
405 /* list only the type or everything if UNKNOWN */
406 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
407 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
411 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
412 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
416 string_to_sid(&map.sid, string_sid);
418 DEBUG(11,("enum_group_mapping: returning group %s of "
419 "type %s\n", map.nt_name,
420 sid_type_lookup(map.sid_name_use)));
422 mapt= SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
424 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
431 mapt[entries].gid = map.gid;
432 sid_copy( &mapt[entries].sid, &map.sid);
433 mapt[entries].sid_name_use = map.sid_name_use;
434 fstrcpy(mapt[entries].nt_name, map.nt_name);
435 fstrcpy(mapt[entries].comment, map.comment);
441 *p_num_entries=entries;
446 /* This operation happens on session setup, so it should better be fast. We
447 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
449 static NTSTATUS one_alias_membership(const DOM_SID *member,
450 DOM_SID **sids, size_t *num)
452 fstring key, string_sid;
456 if (!init_group_mapping()) {
457 DEBUG(0,("failed to initialize group mapping\n"));
458 return NT_STATUS_ACCESS_DENIED;
461 sid_to_string(string_sid, member);
462 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
464 kbuf.dsize = strlen(key)+1;
467 dbuf = tdb_fetch(tdb, kbuf);
469 if (dbuf.dptr == NULL) {
475 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
479 if (!string_to_sid(&alias, string_sid))
482 add_sid_to_array_unique(NULL, &alias, sids, num);
485 return NT_STATUS_NO_MEMORY;
488 SAFE_FREE(dbuf.dptr);
492 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
493 DOM_SID **sids, size_t *num)
500 for (i=0; i<num_members; i++) {
501 NTSTATUS status = one_alias_membership(&members[i], sids, num);
502 if (!NT_STATUS_IS_OK(status))
508 static BOOL is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
513 /* This feels the wrong way round, but the on-disk data structure
514 * dictates it this way. */
515 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
518 for (i=0; i<num; i++) {
519 if (sid_compare(alias, &sids[i]) == 0) {
528 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
534 char *new_memberstring;
537 if(!init_group_mapping()) {
538 DEBUG(0,("failed to initialize group mapping\n"));
539 return NT_STATUS_ACCESS_DENIED;
542 if (!get_group_map_from_sid(*alias, &map))
543 return NT_STATUS_NO_SUCH_ALIAS;
545 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
546 (map.sid_name_use != SID_NAME_WKN_GRP) )
547 return NT_STATUS_NO_SUCH_ALIAS;
549 if (is_aliasmem(alias, member))
550 return NT_STATUS_MEMBER_IN_ALIAS;
552 sid_to_string(string_sid, member);
553 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
555 kbuf.dsize = strlen(key)+1;
558 dbuf = tdb_fetch(tdb, kbuf);
560 sid_to_string(string_sid, alias);
562 if (dbuf.dptr != NULL) {
563 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
566 new_memberstring = SMB_STRDUP(string_sid);
569 if (new_memberstring == NULL)
570 return NT_STATUS_NO_MEMORY;
572 SAFE_FREE(dbuf.dptr);
573 dbuf.dsize = strlen(new_memberstring)+1;
574 dbuf.dptr = new_memberstring;
576 result = tdb_store(tdb, kbuf, dbuf, 0);
578 SAFE_FREE(new_memberstring);
580 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
583 struct aliasmem_closure {
584 const DOM_SID *alias;
589 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
592 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
594 fstring alias_string;
596 if (strncmp(key.dptr, MEMBEROF_PREFIX,
597 strlen(MEMBEROF_PREFIX)) != 0)
602 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
604 DOM_SID alias, member;
605 const char *member_string;
608 if (!string_to_sid(&alias, alias_string))
611 if (sid_compare(closure->alias, &alias) != 0)
614 /* Ok, we found the alias we're looking for in the membership
615 * list currently scanned. The key represents the alias
616 * member. Add that. */
618 member_string = strchr(key.dptr, '/');
620 /* Above we tested for MEMBEROF_PREFIX which includes the
623 SMB_ASSERT(member_string != NULL);
626 if (!string_to_sid(&member, member_string))
629 add_sid_to_array(NULL, &member, closure->sids, closure->num);
635 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
638 struct aliasmem_closure closure;
640 if(!init_group_mapping()) {
641 DEBUG(0,("failed to initialize group mapping\n"));
642 return NT_STATUS_ACCESS_DENIED;
645 if (!get_group_map_from_sid(*alias, &map))
646 return NT_STATUS_NO_SUCH_ALIAS;
648 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
649 (map.sid_name_use != SID_NAME_WKN_GRP) )
650 return NT_STATUS_NO_SUCH_ALIAS;
655 closure.alias = alias;
659 tdb_traverse(tdb, collect_aliasmem, &closure);
663 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
674 result = alias_memberships(member, 1, &sids, &num);
676 if (!NT_STATUS_IS_OK(result))
679 for (i=0; i<num; i++) {
680 if (sid_compare(&sids[i], alias) == 0) {
688 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
692 sids[i] = sids[num-1];
696 sid_to_string(sid_string, member);
697 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, sid_string);
699 kbuf.dsize = strlen(key)+1;
703 return tdb_delete(tdb, kbuf) == 0 ?
704 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
706 member_string = SMB_STRDUP("");
708 if (member_string == NULL) {
710 return NT_STATUS_NO_MEMORY;
713 for (i=0; i<num; i++) {
714 char *s = member_string;
716 sid_to_string(sid_string, &sids[i]);
717 asprintf(&member_string, "%s %s", s, sid_string);
720 if (member_string == NULL) {
722 return NT_STATUS_NO_MEMORY;
726 dbuf.dsize = strlen(member_string)+1;
727 dbuf.dptr = member_string;
729 result = tdb_store(tdb, kbuf, dbuf, 0) == 0 ?
730 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
733 SAFE_FREE(member_string);
740 * High level functions
741 * better to use them than the lower ones.
743 * we are checking if the group is in the mapping file
744 * and if the group is an existing unix group
748 /* get a domain group from it's SID */
750 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
755 if(!init_group_mapping()) {
756 DEBUG(0,("failed to initialize group mapping\n"));
760 DEBUG(10, ("get_domain_group_from_sid\n"));
762 /* if the group is NOT in the database, it CAN NOT be a domain group */
765 ret = pdb_getgrsid(map, sid);
771 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
773 /* if it's not a domain group, continue */
774 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
778 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
784 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
786 grp = getgrgid(map->gid);
788 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
792 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
798 /* get a local (alias) group from it's SID */
800 BOOL get_local_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
804 if(!init_group_mapping()) {
805 DEBUG(0,("failed to initialize group mapping\n"));
809 /* The group is in the mapping table */
811 ret = pdb_getgrsid(map, *sid);
817 if ( ( (map->sid_name_use != SID_NAME_ALIAS) &&
818 (map->sid_name_use != SID_NAME_WKN_GRP) )
820 || (getgrgid(map->gid) == NULL) )
826 /* local groups only exist in the group mapping DB so this
830 /* the group isn't in the mapping table.
831 * make one based on the unix information */
835 sid_peek_rid(sid, &alias_rid);
836 map->gid=pdb_group_rid_to_gid(alias_rid);
838 grp = getgrgid(map->gid);
840 DEBUG(3,("get_local_group_from_sid: No unix group for [%ul]\n", map->gid));
844 map->sid_name_use=SID_NAME_ALIAS;
846 fstrcpy(map->nt_name, grp->gr_name);
847 fstrcpy(map->comment, "Local Unix Group");
849 sid_copy(&map->sid, sid);
856 /* get a builtin group from it's SID */
858 BOOL get_builtin_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
863 if(!init_group_mapping()) {
864 DEBUG(0,("failed to initialize group mapping\n"));
869 ret = pdb_getgrsid(map, *sid);
875 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
883 if ( getgrgid(map->gid) == NULL) {
890 /****************************************************************************
891 Create a UNIX group on demand.
892 ****************************************************************************/
894 int smb_create_group(char *unix_group, gid_t *new_gid)
902 /* defer to scripts */
904 if ( *lp_addgroup_script() ) {
905 pstrcpy(add_script, lp_addgroup_script());
906 pstring_sub(add_script, "%g", unix_group);
907 ret = smbrun(add_script, (new_gid!=NULL) ? &fd : NULL);
908 DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
916 if (read(fd, output, sizeof(output)) > 0) {
917 *new_gid = (gid_t)strtoul(output, NULL, 10);
926 struct group *grp = getgrnam(unix_group);
929 *new_gid = grp->gr_gid;
935 /****************************************************************************
936 Delete a UNIX group on demand.
937 ****************************************************************************/
939 int smb_delete_group(char *unix_group)
944 /* defer to scripts */
946 if ( *lp_delgroup_script() ) {
947 pstrcpy(del_script, lp_delgroup_script());
948 pstring_sub(del_script, "%g", unix_group);
949 ret = smbrun(del_script,NULL);
950 DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
957 /****************************************************************************
958 Set a user's primary UNIX group.
959 ****************************************************************************/
960 int smb_set_primary_group(const char *unix_group, const char* unix_user)
965 /* defer to scripts */
967 if ( *lp_setprimarygroup_script() ) {
968 pstrcpy(add_script, lp_setprimarygroup_script());
969 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
970 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
971 ret = smbrun(add_script,NULL);
973 DEBUG(ret ? 0 : 3,("smb_set_primary_group: "
974 "Running the command `%s' gave %d\n",add_script,ret));
981 /****************************************************************************
982 Add a user to a UNIX group.
983 ****************************************************************************/
985 int smb_add_user_group(char *unix_group, char *unix_user)
990 /* defer to scripts */
992 if ( *lp_addusertogroup_script() ) {
993 pstrcpy(add_script, lp_addusertogroup_script());
994 pstring_sub(add_script, "%g", unix_group);
995 pstring_sub(add_script, "%u", unix_user);
996 ret = smbrun(add_script,NULL);
997 DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1004 /****************************************************************************
1005 Delete a user from a UNIX group
1006 ****************************************************************************/
1008 int smb_delete_user_group(const char *unix_group, const char *unix_user)
1013 /* defer to scripts */
1015 if ( *lp_deluserfromgroup_script() ) {
1016 pstrcpy(del_script, lp_deluserfromgroup_script());
1017 pstring_sub(del_script, "%g", unix_group);
1018 pstring_sub(del_script, "%u", unix_user);
1019 ret = smbrun(del_script,NULL);
1020 DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
1028 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1031 return get_group_map_from_sid(sid, map) ?
1032 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1035 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1038 return get_group_map_from_gid(gid, map) ?
1039 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1042 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1045 return get_group_map_from_ntname(name, map) ?
1046 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1049 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
1052 return add_mapping_entry(map, TDB_INSERT) ?
1053 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1056 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
1059 return add_mapping_entry(map, TDB_REPLACE) ?
1060 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1063 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
1066 return group_map_remove(&sid) ?
1067 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1070 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
1071 enum SID_NAME_USE sid_name_use,
1072 GROUP_MAP **pp_rmap, size_t *p_num_entries,
1075 return enum_group_mapping(sid_name_use, pp_rmap, p_num_entries, unix_only) ?
1076 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1079 NTSTATUS pdb_default_find_alias(struct pdb_methods *methods,
1080 const char *name, DOM_SID *sid)
1084 if (!pdb_getgrnam(&map, name))
1085 return NT_STATUS_NO_SUCH_ALIAS;
1087 if ((map.sid_name_use != SID_NAME_WKN_GRP) &&
1088 (map.sid_name_use != SID_NAME_ALIAS))
1089 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1091 sid_copy(sid, &map.sid);
1092 return NT_STATUS_OK;
1095 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
1096 const char *name, uint32 *rid)
1099 enum SID_NAME_USE type;
1105 TALLOC_CTX *mem_ctx = talloc_new(NULL);
1107 if (mem_ctx == NULL) {
1108 return NT_STATUS_NO_MEMORY;
1111 exists = lookup_name(mem_ctx, name, LOOKUP_NAME_ISOLATED,
1112 NULL, NULL, &sid, &type);
1113 talloc_free(mem_ctx);
1116 return NT_STATUS_ALIAS_EXISTS;
1119 if (!winbind_allocate_rid_and_gid(&new_rid, &gid))
1120 return NT_STATUS_ACCESS_DENIED;
1122 sid_copy(&sid, get_global_sam_sid());
1123 sid_append_rid(&sid, new_rid);
1126 sid_copy(&map.sid, &sid);
1127 map.sid_name_use = SID_NAME_ALIAS;
1128 fstrcpy(map.nt_name, name);
1129 fstrcpy(map.comment, "");
1131 if (!pdb_add_group_mapping_entry(&map)) {
1132 DEBUG(0, ("Could not add group mapping entry for alias %s\n",
1134 return NT_STATUS_ACCESS_DENIED;
1139 return NT_STATUS_OK;
1142 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
1145 return pdb_delete_group_mapping_entry(*sid) ?
1146 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1149 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
1151 struct acct_info *info)
1155 if (!pdb_getgrsid(&map, *sid))
1156 return NT_STATUS_NO_SUCH_ALIAS;
1158 fstrcpy(info->acct_name, map.nt_name);
1159 fstrcpy(info->acct_desc, map.comment);
1160 sid_peek_rid(&map.sid, &info->rid);
1161 return NT_STATUS_OK;
1164 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
1166 struct acct_info *info)
1170 if (!pdb_getgrsid(&map, *sid))
1171 return NT_STATUS_NO_SUCH_ALIAS;
1173 fstrcpy(map.comment, info->acct_desc);
1175 if (!pdb_update_group_mapping_entry(&map))
1176 return NT_STATUS_ACCESS_DENIED;
1178 return NT_STATUS_OK;
1181 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
1182 const DOM_SID *alias, const DOM_SID *member)
1184 return add_aliasmem(alias, member);
1187 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
1188 const DOM_SID *alias, const DOM_SID *member)
1190 return del_aliasmem(alias, member);
1193 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
1194 const DOM_SID *alias, DOM_SID **pp_members,
1195 size_t *p_num_members)
1197 return enum_aliasmem(alias, pp_members, p_num_members);
1200 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
1201 TALLOC_CTX *mem_ctx,
1202 const DOM_SID *domain_sid,
1203 const DOM_SID *members,
1205 uint32 **pp_alias_rids,
1206 size_t *p_num_alias_rids)
1208 DOM_SID *alias_sids;
1209 size_t i, num_alias_sids;
1215 result = alias_memberships(members, num_members,
1216 &alias_sids, &num_alias_sids);
1218 if (!NT_STATUS_IS_OK(result))
1221 *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32, num_alias_sids);
1222 if (*pp_alias_rids == NULL)
1223 return NT_STATUS_NO_MEMORY;
1225 *p_num_alias_rids = 0;
1227 for (i=0; i<num_alias_sids; i++) {
1228 if (!sid_peek_check_rid(domain_sid, &alias_sids[i],
1229 &(*pp_alias_rids)[*p_num_alias_rids]))
1231 *p_num_alias_rids += 1;
1234 SAFE_FREE(alias_sids);
1236 return NT_STATUS_OK;
1239 /**********************************************************************
1240 no ops for passdb backends that don't implement group mapping
1241 *********************************************************************/
1243 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1246 return NT_STATUS_UNSUCCESSFUL;
1249 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1252 return NT_STATUS_UNSUCCESSFUL;
1255 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1258 return NT_STATUS_UNSUCCESSFUL;
1261 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
1264 return NT_STATUS_UNSUCCESSFUL;
1267 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
1270 return NT_STATUS_UNSUCCESSFUL;
1273 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
1276 return NT_STATUS_UNSUCCESSFUL;
1279 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
1280 enum SID_NAME_USE sid_name_use,
1281 GROUP_MAP **rmap, size_t *num_entries,
1284 return NT_STATUS_UNSUCCESSFUL;
1287 /****************************************************************************
1288 These need to be redirected through pdb_interface.c
1289 ****************************************************************************/
1290 BOOL pdb_get_dom_grp_info(const DOM_SID *sid, struct acct_info *info)
1296 res = get_domain_group_from_sid(*sid, &map);
1302 fstrcpy(info->acct_name, map.nt_name);
1303 fstrcpy(info->acct_desc, map.comment);
1304 sid_peek_rid(sid, &info->rid);
1308 BOOL pdb_set_dom_grp_info(const DOM_SID *sid, const struct acct_info *info)
1312 if (!get_domain_group_from_sid(*sid, &map))
1315 fstrcpy(map.nt_name, info->acct_name);
1316 fstrcpy(map.comment, info->acct_desc);
1318 return pdb_update_group_mapping_entry(&map);