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.
6 * Copyright (C) Volker Lendecke 2006.
7 * Copyright (C) Gerald Carter 2006.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 static TDB_CONTEXT *tdb; /* used for driver files */
28 #define DATABASE_VERSION_V1 1 /* native byte format. */
29 #define DATABASE_VERSION_V2 2 /* le format. */
31 #define GROUP_PREFIX "UNIXGROUP/"
33 /* Alias memberships are stored reverse, as memberships. The performance
34 * critical operation is to determine the aliases a SID is member of, not
35 * listing alias members. So we store a list of alias SIDs a SID is member of
36 * hanging of the member as key.
38 #define MEMBEROF_PREFIX "MEMBEROF/"
41 static NTSTATUS enum_group_mapping(const DOM_SID *sid,
42 enum SID_NAME_USE sid_name_use,
44 size_t *p_num_entries, BOOL unix_only);
45 static BOOL group_map_remove(const DOM_SID *sid);
47 /****************************************************************************
48 Open the group mapping tdb.
49 ****************************************************************************/
51 static NTSTATUS init_group_mapping(void)
53 const char *vstring = "INFO/version";
55 GROUP_MAP *map_table = NULL;
56 size_t num_entries = 0;
61 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT,
62 O_RDWR|O_CREAT, 0600);
64 DEBUG(0,("Failed to open group mapping database\n"));
65 return map_nt_error_from_unix(errno);
68 /* handle a Samba upgrade */
69 tdb_lock_bystring(tdb, vstring);
71 /* Cope with byte-reversed older versions of the db. */
72 vers_id = tdb_fetch_int32(tdb, vstring);
73 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
74 /* Written on a bigendian machine with old fetch_int code. Save as le. */
75 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
76 vers_id = DATABASE_VERSION_V2;
79 /* if its an unknown version we remove everthing in the db */
81 if (vers_id != DATABASE_VERSION_V2) {
82 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
83 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
86 tdb_unlock_bystring(tdb, vstring);
88 /* cleanup any map entries with a gid == -1 */
90 if ( NT_STATUS_IS_OK(enum_group_mapping( NULL, SID_NAME_UNKNOWN,
91 &map_table, &num_entries,
95 for ( i=0; i<num_entries; i++ ) {
96 if ( map_table[i].gid == -1 ) {
97 group_map_remove( &map_table[i].sid );
101 SAFE_FREE( map_table );
108 /****************************************************************************
109 ****************************************************************************/
110 static NTSTATUS add_mapping_entry(GROUP_MAP *map, int flag)
114 fstring string_sid="";
118 status = init_group_mapping();
119 if(!NT_STATUS_IS_OK(status)) {
120 DEBUG(0,("failed to initialize group mapping: %s\n",
125 sid_to_string(string_sid, &map->sid);
127 len = tdb_pack(buf, sizeof(buf), "ddff",
128 map->gid, map->sid_name_use, map->nt_name, map->comment);
130 if (len > sizeof(buf))
131 return NT_STATUS_NO_MEMORY;
133 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
135 kbuf.dsize = strlen(key)+1;
139 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) {
140 return map_ntstatus_from_tdb(tdb);
146 /****************************************************************************
147 Map a unix group to a newly created mapping
148 ****************************************************************************/
149 NTSTATUS map_unix_group(const struct group *grp, GROUP_MAP *pmap)
153 const char *grpname, *dom, *name;
156 if (NT_STATUS_IS_OK(pdb_getgrgid(&map, grp->gr_gid))) {
157 return NT_STATUS_GROUP_EXISTS;
160 map.gid = grp->gr_gid;
161 grpname = grp->gr_name;
163 if (lookup_name(tmp_talloc_ctx(), grpname, LOOKUP_NAME_ISOLATED,
164 &dom, &name, NULL, NULL)) {
166 const char *tmp = talloc_asprintf(
167 tmp_talloc_ctx(), "Unix Group %s", grp->gr_name);
169 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
170 grpname, dom, name, tmp));
174 if (lookup_name(tmp_talloc_ctx(), grpname, LOOKUP_NAME_ISOLATED,
175 NULL, NULL, NULL, NULL)) {
176 DEBUG(3, ("\"%s\" exists, can't map it\n", grp->gr_name));
177 return NT_STATUS_GROUP_EXISTS;
180 fstrcpy(map.nt_name, grpname);
182 if (pdb_rid_algorithm()) {
183 rid = pdb_gid_to_group_rid( grp->gr_gid );
185 if (!pdb_new_rid(&rid)) {
186 DEBUG(3, ("Could not get a new RID for %s\n",
188 return NT_STATUS_ACCESS_DENIED;
192 sid_compose(&map.sid, get_global_sam_sid(), rid);
193 map.sid_name_use = SID_NAME_DOM_GRP;
194 fstrcpy(map.comment, talloc_asprintf(tmp_talloc_ctx(), "Unix Group %s",
197 status = pdb_add_group_mapping_entry(&map);
198 if (NT_STATUS_IS_OK(status)) {
204 /****************************************************************************
205 Return the sid and the type of the unix group.
206 ****************************************************************************/
208 static NTSTATUS get_group_map_from_sid(const DOM_SID *sid, GROUP_MAP *map)
216 status = init_group_mapping();
217 if (!NT_STATUS_IS_OK(status)) {
218 DEBUG(0, ("failed to initialize group mapping: %s\n",
223 /* the key is the SID, retrieving is direct */
225 sid_to_string(string_sid, sid);
226 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
229 kbuf.dsize = strlen(key)+1;
231 dbuf = tdb_fetch(tdb, kbuf);
233 return NT_STATUS_NOT_FOUND;
236 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
237 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
239 SAFE_FREE(dbuf.dptr);
242 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
243 return NT_STATUS_INTERNAL_DB_CORRUPTION;
246 sid_copy(&map->sid, sid);
251 /****************************************************************************
252 Return the sid and the type of the unix group.
253 ****************************************************************************/
255 static NTSTATUS get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
257 TDB_DATA kbuf, dbuf, newkey;
262 status = init_group_mapping();
263 if (!NT_STATUS_IS_OK(status)) {
264 DEBUG(0, ("failed to initialize group mapping: %s\n",
269 /* we need to enumerate the TDB to find the GID */
271 for (kbuf = tdb_firstkey(tdb);
273 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
275 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
277 dbuf = tdb_fetch(tdb, kbuf);
281 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
283 string_to_sid(&map->sid, string_sid);
285 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
286 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
288 SAFE_FREE(dbuf.dptr);
291 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
292 return NT_STATUS_INTERNAL_DB_CORRUPTION;
296 SAFE_FREE(kbuf.dptr);
301 return NT_STATUS_NOT_FOUND;
304 /****************************************************************************
305 Return the sid and the type of the unix group.
306 ****************************************************************************/
308 static NTSTATUS get_group_map_from_ntname(const char *name, GROUP_MAP *map)
310 TDB_DATA kbuf, dbuf, newkey;
315 status = init_group_mapping();
316 if (!NT_STATUS_IS_OK(status)) {
317 DEBUG(0, ("get_group_map_from_ntname: failed to initialize "
318 "group mapping: %s\n", nt_errstr(status)));
322 /* we need to enumerate the TDB to find the name */
324 for (kbuf = tdb_firstkey(tdb);
326 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
328 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
330 dbuf = tdb_fetch(tdb, kbuf);
334 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
336 string_to_sid(&map->sid, string_sid);
338 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
339 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
341 SAFE_FREE(dbuf.dptr);
344 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
345 return NT_STATUS_INTERNAL_DB_CORRUPTION;
348 if ( strequal(name, map->nt_name) ) {
349 SAFE_FREE(kbuf.dptr);
354 return NT_STATUS_NOT_FOUND;
357 /****************************************************************************
358 Remove a group mapping entry.
359 ****************************************************************************/
361 static BOOL group_map_remove(const DOM_SID *sid)
367 if(!NT_STATUS_IS_OK(init_group_mapping())) {
368 DEBUG(0,("failed to initialize group mapping\n"));
372 /* the key is the SID, retrieving is direct */
374 sid_to_string(string_sid, sid);
375 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
378 kbuf.dsize = strlen(key)+1;
380 dbuf = tdb_fetch(tdb, kbuf);
384 SAFE_FREE(dbuf.dptr);
386 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
392 /****************************************************************************
393 Enumerate the group mapping.
394 ****************************************************************************/
396 static NTSTATUS enum_group_mapping(const DOM_SID *domsid,
397 enum SID_NAME_USE sid_name_use,
399 size_t *p_num_entries, BOOL unix_only)
401 TDB_DATA kbuf, dbuf, newkey;
411 status = init_group_mapping();
412 if(!NT_STATUS_IS_OK(status)) {
413 DEBUG(0, ("failed to initialize group mapping: %s\n",
421 for (kbuf = tdb_firstkey(tdb);
423 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
425 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
428 dbuf = tdb_fetch(tdb, kbuf);
432 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
434 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
435 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
437 SAFE_FREE(dbuf.dptr);
440 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
444 /* list only the type or everything if UNKNOWN */
445 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
446 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
450 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
451 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
455 string_to_sid(&grpsid, string_sid);
456 sid_copy( &map.sid, &grpsid );
458 sid_split_rid( &grpsid, &rid );
460 /* Only check the domain if we were given one */
462 if ( domsid && !sid_equal( domsid, &grpsid ) ) {
463 DEBUG(11,("enum_group_mapping: group %s is not in domain %s\n",
464 string_sid, sid_string_static(domsid)));
468 DEBUG(11,("enum_group_mapping: returning group %s of "
469 "type %s\n", map.nt_name,
470 sid_type_lookup(map.sid_name_use)));
472 (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
474 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
475 return NT_STATUS_NO_MEMORY;
480 mapt[entries].gid = map.gid;
481 sid_copy( &mapt[entries].sid, &map.sid);
482 mapt[entries].sid_name_use = map.sid_name_use;
483 fstrcpy(mapt[entries].nt_name, map.nt_name);
484 fstrcpy(mapt[entries].comment, map.comment);
490 *p_num_entries=entries;
495 /* This operation happens on session setup, so it should better be fast. We
496 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
498 static NTSTATUS one_alias_membership(const DOM_SID *member,
499 DOM_SID **sids, size_t *num)
501 fstring key, string_sid;
505 if (!NT_STATUS_IS_OK(init_group_mapping())) {
506 DEBUG(0,("failed to initialize group mapping\n"));
507 return NT_STATUS_ACCESS_DENIED;
510 sid_to_string(string_sid, member);
511 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
513 kbuf.dsize = strlen(key)+1;
516 dbuf = tdb_fetch(tdb, kbuf);
518 if (dbuf.dptr == NULL) {
524 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
528 if (!string_to_sid(&alias, string_sid))
531 add_sid_to_array_unique(NULL, &alias, sids, num);
534 return NT_STATUS_NO_MEMORY;
537 SAFE_FREE(dbuf.dptr);
541 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
542 DOM_SID **sids, size_t *num)
549 for (i=0; i<num_members; i++) {
550 NTSTATUS status = one_alias_membership(&members[i], sids, num);
551 if (!NT_STATUS_IS_OK(status))
557 static BOOL is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
562 /* This feels the wrong way round, but the on-disk data structure
563 * dictates it this way. */
564 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
567 for (i=0; i<num; i++) {
568 if (sid_compare(alias, &sids[i]) == 0) {
577 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
583 char *new_memberstring;
586 if(!NT_STATUS_IS_OK(init_group_mapping())) {
587 DEBUG(0,("failed to initialize group mapping\n"));
588 return NT_STATUS_ACCESS_DENIED;
591 if (!NT_STATUS_IS_OK(get_group_map_from_sid(alias, &map)))
592 return NT_STATUS_NO_SUCH_ALIAS;
594 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
595 (map.sid_name_use != SID_NAME_WKN_GRP) )
596 return NT_STATUS_NO_SUCH_ALIAS;
598 if (is_aliasmem(alias, member))
599 return NT_STATUS_MEMBER_IN_ALIAS;
601 sid_to_string(string_sid, member);
602 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
604 kbuf.dsize = strlen(key)+1;
607 dbuf = tdb_fetch(tdb, kbuf);
609 sid_to_string(string_sid, alias);
611 if (dbuf.dptr != NULL) {
612 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
615 new_memberstring = SMB_STRDUP(string_sid);
618 if (new_memberstring == NULL)
619 return NT_STATUS_NO_MEMORY;
621 SAFE_FREE(dbuf.dptr);
622 dbuf.dsize = strlen(new_memberstring)+1;
623 dbuf.dptr = new_memberstring;
625 result = tdb_store(tdb, kbuf, dbuf, 0);
627 SAFE_FREE(new_memberstring);
629 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
632 struct aliasmem_closure {
633 const DOM_SID *alias;
638 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
641 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
643 fstring alias_string;
645 if (strncmp(key.dptr, MEMBEROF_PREFIX,
646 strlen(MEMBEROF_PREFIX)) != 0)
651 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
653 DOM_SID alias, member;
654 const char *member_string;
657 if (!string_to_sid(&alias, alias_string))
660 if (sid_compare(closure->alias, &alias) != 0)
663 /* Ok, we found the alias we're looking for in the membership
664 * list currently scanned. The key represents the alias
665 * member. Add that. */
667 member_string = strchr(key.dptr, '/');
669 /* Above we tested for MEMBEROF_PREFIX which includes the
672 SMB_ASSERT(member_string != NULL);
675 if (!string_to_sid(&member, member_string))
678 add_sid_to_array(NULL, &member, closure->sids, closure->num);
684 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
687 struct aliasmem_closure closure;
689 if(!NT_STATUS_IS_OK(init_group_mapping())) {
690 DEBUG(0,("failed to initialize group mapping\n"));
691 return NT_STATUS_ACCESS_DENIED;
694 if (!NT_STATUS_IS_OK(get_group_map_from_sid(alias, &map)))
695 return NT_STATUS_NO_SUCH_ALIAS;
697 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
698 (map.sid_name_use != SID_NAME_WKN_GRP) )
699 return NT_STATUS_NO_SUCH_ALIAS;
704 closure.alias = alias;
708 tdb_traverse(tdb, collect_aliasmem, &closure);
712 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
723 result = alias_memberships(member, 1, &sids, &num);
725 if (!NT_STATUS_IS_OK(result))
728 for (i=0; i<num; i++) {
729 if (sid_compare(&sids[i], alias) == 0) {
737 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
741 sids[i] = sids[num-1];
745 sid_to_string(sid_string, member);
746 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, sid_string);
748 kbuf.dsize = strlen(key)+1;
752 return tdb_delete(tdb, kbuf) == 0 ?
753 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
755 member_string = SMB_STRDUP("");
757 if (member_string == NULL) {
759 return NT_STATUS_NO_MEMORY;
762 for (i=0; i<num; i++) {
763 char *s = member_string;
765 sid_to_string(sid_string, &sids[i]);
766 asprintf(&member_string, "%s %s", s, sid_string);
769 if (member_string == NULL) {
771 return NT_STATUS_NO_MEMORY;
775 dbuf.dsize = strlen(member_string)+1;
776 dbuf.dptr = member_string;
778 result = tdb_store(tdb, kbuf, dbuf, 0) == 0 ?
779 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
782 SAFE_FREE(member_string);
789 * High level functions
790 * better to use them than the lower ones.
792 * we are checking if the group is in the mapping file
793 * and if the group is an existing unix group
797 /* get a domain group from it's SID */
799 NTSTATUS get_domain_group_from_sid(const DOM_SID *sid, GROUP_MAP *map)
804 status = init_group_mapping();
805 if (!NT_STATUS_IS_OK(status)) {
806 DEBUG(0, ("failed to initialize group mapping: %s\n",
811 DEBUG(10, ("get_domain_group_from_sid\n"));
813 /* if the group is NOT in the database, it CAN NOT be a domain group */
816 status = pdb_getgrsid(map, sid);
819 /* special case check for rid 513 */
821 if ( !NT_STATUS_IS_OK(status) ) {
824 sid_peek_rid( sid, &rid );
826 if ( rid == DOMAIN_GROUP_RID_USERS ) {
827 fstrcpy( map->nt_name, "None" );
828 fstrcpy( map->comment, "Ordinary Users" );
829 sid_copy( &map->sid, sid );
830 map->sid_name_use = SID_NAME_DOM_GRP;
838 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
840 /* if it's not a domain group, continue */
841 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
842 return NT_STATUS_OBJECT_TYPE_MISMATCH;
845 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
848 return NT_STATUS_NOT_FOUND;
851 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
853 grp = getgrgid(map->gid);
855 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
856 return NT_STATUS_NOT_FOUND;
859 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
864 /****************************************************************************
865 Create a UNIX group on demand.
866 ****************************************************************************/
868 int smb_create_group(const char *unix_group, gid_t *new_gid)
876 /* defer to scripts */
878 if ( *lp_addgroup_script() ) {
879 pstrcpy(add_script, lp_addgroup_script());
880 pstring_sub(add_script, "%g", unix_group);
881 ret = smbrun(add_script, &fd);
882 DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
890 if (read(fd, output, sizeof(output)) > 0) {
891 *new_gid = (gid_t)strtoul(output, NULL, 10);
900 struct group *grp = getgrnam(unix_group);
903 *new_gid = grp->gr_gid;
909 /****************************************************************************
910 Delete a UNIX group on demand.
911 ****************************************************************************/
913 int smb_delete_group(const char *unix_group)
918 /* defer to scripts */
920 if ( *lp_delgroup_script() ) {
921 pstrcpy(del_script, lp_delgroup_script());
922 pstring_sub(del_script, "%g", unix_group);
923 ret = smbrun(del_script,NULL);
924 DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
931 /****************************************************************************
932 Set a user's primary UNIX group.
933 ****************************************************************************/
934 int smb_set_primary_group(const char *unix_group, const char* unix_user)
939 /* defer to scripts */
941 if ( *lp_setprimarygroup_script() ) {
942 pstrcpy(add_script, lp_setprimarygroup_script());
943 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
944 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
945 ret = smbrun(add_script,NULL);
947 DEBUG(ret ? 0 : 3,("smb_set_primary_group: "
948 "Running the command `%s' gave %d\n",add_script,ret));
955 /****************************************************************************
956 Add a user to a UNIX group.
957 ****************************************************************************/
959 int smb_add_user_group(const char *unix_group, const char *unix_user)
964 /* defer to scripts */
966 if ( *lp_addusertogroup_script() ) {
967 pstrcpy(add_script, lp_addusertogroup_script());
968 pstring_sub(add_script, "%g", unix_group);
969 pstring_sub(add_script, "%u", unix_user);
970 ret = smbrun(add_script,NULL);
971 DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
978 /****************************************************************************
979 Delete a user from a UNIX group
980 ****************************************************************************/
982 int smb_delete_user_group(const char *unix_group, const char *unix_user)
987 /* defer to scripts */
989 if ( *lp_deluserfromgroup_script() ) {
990 pstrcpy(del_script, lp_deluserfromgroup_script());
991 pstring_sub(del_script, "%g", unix_group);
992 pstring_sub(del_script, "%u", unix_user);
993 ret = smbrun(del_script,NULL);
994 DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
1002 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1005 return get_group_map_from_sid(sid, map);
1008 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1011 return get_group_map_from_gid(gid, map);
1014 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1017 return get_group_map_from_ntname(name, map);
1020 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
1023 return add_mapping_entry(map, TDB_INSERT);
1026 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
1029 return add_mapping_entry(map, TDB_REPLACE);
1032 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
1035 return group_map_remove(&sid) ?
1036 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1039 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
1041 enum SID_NAME_USE sid_name_use,
1042 GROUP_MAP **pp_rmap,
1043 size_t *p_num_entries,
1046 return enum_group_mapping(sid, sid_name_use, pp_rmap, p_num_entries,
1050 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
1051 const char *name, uint32 *rid)
1054 enum SID_NAME_USE type;
1059 TALLOC_CTX *mem_ctx;
1062 DEBUG(10, ("Trying to create alias %s\n", name));
1064 mem_ctx = talloc_new(NULL);
1065 if (mem_ctx == NULL) {
1066 return NT_STATUS_NO_MEMORY;
1069 exists = lookup_name(mem_ctx, name, LOOKUP_NAME_ISOLATED,
1070 NULL, NULL, &sid, &type);
1071 TALLOC_FREE(mem_ctx);
1074 return NT_STATUS_ALIAS_EXISTS;
1077 if (!winbind_allocate_gid(&gid)) {
1078 DEBUG(3, ("Could not get a gid out of winbind\n"));
1079 return NT_STATUS_ACCESS_DENIED;
1082 if (!pdb_new_rid(&new_rid)) {
1083 DEBUG(0, ("Could not allocate a RID -- wasted a gid :-(\n"));
1084 return NT_STATUS_ACCESS_DENIED;
1087 DEBUG(10, ("Creating alias %s with gid %d and rid %d\n",
1088 name, gid, new_rid));
1090 sid_copy(&sid, get_global_sam_sid());
1091 sid_append_rid(&sid, new_rid);
1094 sid_copy(&map.sid, &sid);
1095 map.sid_name_use = SID_NAME_ALIAS;
1096 fstrcpy(map.nt_name, name);
1097 fstrcpy(map.comment, "");
1099 status = pdb_add_group_mapping_entry(&map);
1101 if (!NT_STATUS_IS_OK(status)) {
1102 DEBUG(0, ("Could not add group mapping entry for alias %s "
1103 "(%s)\n", name, nt_errstr(status)));
1109 return NT_STATUS_OK;
1112 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
1115 return pdb_delete_group_mapping_entry(*sid);
1118 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
1120 struct acct_info *info)
1124 if (!NT_STATUS_IS_OK(pdb_getgrsid(&map, sid)))
1125 return NT_STATUS_NO_SUCH_ALIAS;
1127 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1128 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1129 DEBUG(2, ("%s is a %s, expected an alias\n",
1130 sid_string_static(sid),
1131 sid_type_lookup(map.sid_name_use)));
1132 return NT_STATUS_NO_SUCH_ALIAS;
1135 fstrcpy(info->acct_name, map.nt_name);
1136 fstrcpy(info->acct_desc, map.comment);
1137 sid_peek_rid(&map.sid, &info->rid);
1138 return NT_STATUS_OK;
1141 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
1143 struct acct_info *info)
1147 if (!NT_STATUS_IS_OK(pdb_getgrsid(&map, sid)))
1148 return NT_STATUS_NO_SUCH_ALIAS;
1150 fstrcpy(map.nt_name, info->acct_name);
1151 fstrcpy(map.comment, info->acct_desc);
1153 return pdb_update_group_mapping_entry(&map);
1156 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
1157 const DOM_SID *alias, const DOM_SID *member)
1159 return add_aliasmem(alias, member);
1162 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
1163 const DOM_SID *alias, const DOM_SID *member)
1165 return del_aliasmem(alias, member);
1168 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
1169 const DOM_SID *alias, DOM_SID **pp_members,
1170 size_t *p_num_members)
1172 return enum_aliasmem(alias, pp_members, p_num_members);
1175 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
1176 TALLOC_CTX *mem_ctx,
1177 const DOM_SID *domain_sid,
1178 const DOM_SID *members,
1180 uint32 **pp_alias_rids,
1181 size_t *p_num_alias_rids)
1183 DOM_SID *alias_sids;
1184 size_t i, num_alias_sids;
1190 result = alias_memberships(members, num_members,
1191 &alias_sids, &num_alias_sids);
1193 if (!NT_STATUS_IS_OK(result))
1196 *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32, num_alias_sids);
1197 if (*pp_alias_rids == NULL)
1198 return NT_STATUS_NO_MEMORY;
1200 *p_num_alias_rids = 0;
1202 for (i=0; i<num_alias_sids; i++) {
1203 if (!sid_peek_check_rid(domain_sid, &alias_sids[i],
1204 &(*pp_alias_rids)[*p_num_alias_rids]))
1206 *p_num_alias_rids += 1;
1209 SAFE_FREE(alias_sids);
1211 return NT_STATUS_OK;
1214 /********************************************************************
1215 Really just intended to be called by smbd
1216 ********************************************************************/
1218 NTSTATUS pdb_create_builtin_alias(uint32 rid)
1221 enum SID_NAME_USE type;
1224 TALLOC_CTX *mem_ctx;
1226 const char *name = NULL;
1229 DEBUG(10, ("Trying to create builtin alias %d\n", rid));
1231 if ( !sid_compose( &sid, &global_sid_Builtin, rid ) ) {
1232 return NT_STATUS_NO_SUCH_ALIAS;
1235 if ( (mem_ctx = talloc_new(NULL)) == NULL ) {
1236 return NT_STATUS_NO_MEMORY;
1239 if ( !lookup_sid(mem_ctx, &sid, NULL, &name, &type) ) {
1240 TALLOC_FREE( mem_ctx );
1241 return NT_STATUS_NO_SUCH_ALIAS;
1244 /* validate RID so copy the name and move on */
1246 fstrcpy( groupname, name );
1247 TALLOC_FREE( mem_ctx );
1249 if (!winbind_allocate_gid(&gid)) {
1250 DEBUG(3, ("pdb_create_builtin_alias: Could not get a gid out of winbind\n"));
1251 return NT_STATUS_ACCESS_DENIED;
1254 DEBUG(10,("Creating alias %s with gid %d\n", name, gid));
1257 sid_copy(&map.sid, &sid);
1258 map.sid_name_use = SID_NAME_ALIAS;
1259 fstrcpy(map.nt_name, name);
1260 fstrcpy(map.comment, "");
1262 status = pdb_add_group_mapping_entry(&map);
1264 if (!NT_STATUS_IS_OK(status)) {
1265 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
1266 "(%s)\n", rid, nt_errstr(status)));