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.
25 #include "groupdb/mapping.h"
27 /****************************************************************************
28 initialise first time the mapping list
29 ****************************************************************************/
30 NTSTATUS add_initial_entry(gid_t gid, const char *sid, enum lsa_SidType sid_name_use, const char *nt_name, const char *comment)
34 if(!init_group_mapping()) {
35 DEBUG(0,("failed to initialize group mapping\n"));
36 return NT_STATUS_UNSUCCESSFUL;
40 if (!string_to_sid(&map.sid, sid)) {
41 DEBUG(0, ("string_to_sid failed: %s", sid));
42 return NT_STATUS_UNSUCCESSFUL;
45 map.sid_name_use=sid_name_use;
46 fstrcpy(map.nt_name, nt_name);
47 fstrcpy(map.comment, comment);
49 return pdb_add_group_mapping_entry(&map);
52 /****************************************************************************
53 Map a unix group to a newly created mapping
54 ****************************************************************************/
55 NTSTATUS map_unix_group(const struct group *grp, GROUP_MAP *pmap)
59 const char *grpname, *dom, *name;
62 if (pdb_getgrgid(&map, grp->gr_gid)) {
63 return NT_STATUS_GROUP_EXISTS;
66 map.gid = grp->gr_gid;
67 grpname = grp->gr_name;
69 if (lookup_name(tmp_talloc_ctx(), grpname, LOOKUP_NAME_ISOLATED,
70 &dom, &name, NULL, NULL)) {
72 const char *tmp = talloc_asprintf(
73 tmp_talloc_ctx(), "Unix Group %s", grp->gr_name);
75 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
76 grpname, dom, name, tmp));
80 if (lookup_name(tmp_talloc_ctx(), grpname, LOOKUP_NAME_ISOLATED,
81 NULL, NULL, NULL, NULL)) {
82 DEBUG(3, ("\"%s\" exists, can't map it\n", grp->gr_name));
83 return NT_STATUS_GROUP_EXISTS;
86 fstrcpy(map.nt_name, grpname);
88 if (pdb_rid_algorithm()) {
89 rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
91 if (!pdb_new_rid(&rid)) {
92 DEBUG(3, ("Could not get a new RID for %s\n",
94 return NT_STATUS_ACCESS_DENIED;
98 sid_compose(&map.sid, get_global_sam_sid(), rid);
99 map.sid_name_use = SID_NAME_DOM_GRP;
100 fstrcpy(map.comment, talloc_asprintf(tmp_talloc_ctx(), "Unix Group %s",
103 status = pdb_add_group_mapping_entry(&map);
104 if (NT_STATUS_IS_OK(status)) {
116 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
117 DOM_SID **sids, size_t *num)
124 for (i=0; i<num_members; i++) {
125 NTSTATUS status = one_alias_membership(&members[i], sids, num);
126 if (!NT_STATUS_IS_OK(status))
132 struct aliasmem_closure {
133 const DOM_SID *alias;
142 * High level functions
143 * better to use them than the lower ones.
145 * we are checking if the group is in the mapping file
146 * and if the group is an existing unix group
150 /* get a domain group from it's SID */
152 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
157 if(!init_group_mapping()) {
158 DEBUG(0,("failed to initialize group mapping\n"));
162 DEBUG(10, ("get_domain_group_from_sid\n"));
164 /* if the group is NOT in the database, it CAN NOT be a domain group */
167 ret = pdb_getgrsid(map, sid);
170 /* special case check for rid 513 */
175 sid_peek_rid( &sid, &rid );
177 if ( rid == DOMAIN_GROUP_RID_USERS ) {
178 fstrcpy( map->nt_name, "None" );
179 fstrcpy( map->comment, "Ordinary Users" );
180 sid_copy( &map->sid, &sid );
181 map->sid_name_use = SID_NAME_DOM_GRP;
189 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
191 /* if it's not a domain group, continue */
192 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
196 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
202 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
204 grp = getgrgid(map->gid);
206 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
210 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
215 /****************************************************************************
216 Create a UNIX group on demand.
217 ****************************************************************************/
219 int smb_create_group(const char *unix_group, gid_t *new_gid)
227 /* defer to scripts */
229 if ( *lp_addgroup_script() ) {
230 pstrcpy(add_script, lp_addgroup_script());
231 pstring_sub(add_script, "%g", unix_group);
232 ret = smbrun(add_script, &fd);
233 DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
235 smb_nscd_flush_group_cache();
244 if (read(fd, output, sizeof(output)) > 0) {
245 *new_gid = (gid_t)strtoul(output, NULL, 10);
254 struct group *grp = getgrnam(unix_group);
257 *new_gid = grp->gr_gid;
263 /****************************************************************************
264 Delete a UNIX group on demand.
265 ****************************************************************************/
267 int smb_delete_group(const char *unix_group)
272 /* defer to scripts */
274 if ( *lp_delgroup_script() ) {
275 pstrcpy(del_script, lp_delgroup_script());
276 pstring_sub(del_script, "%g", unix_group);
277 ret = smbrun(del_script,NULL);
278 DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
280 smb_nscd_flush_group_cache();
288 /****************************************************************************
289 Set a user's primary UNIX group.
290 ****************************************************************************/
291 int smb_set_primary_group(const char *unix_group, const char* unix_user)
296 /* defer to scripts */
298 if ( *lp_setprimarygroup_script() ) {
299 pstrcpy(add_script, lp_setprimarygroup_script());
300 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
301 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
302 ret = smbrun(add_script,NULL);
304 DEBUG(ret ? 0 : 3,("smb_set_primary_group: "
305 "Running the command `%s' gave %d\n",add_script,ret));
307 smb_nscd_flush_group_cache();
315 /****************************************************************************
316 Add a user to a UNIX group.
317 ****************************************************************************/
319 int smb_add_user_group(const char *unix_group, const char *unix_user)
324 /* defer to scripts */
326 if ( *lp_addusertogroup_script() ) {
327 pstrcpy(add_script, lp_addusertogroup_script());
328 pstring_sub(add_script, "%g", unix_group);
329 pstring_sub(add_script, "%u", unix_user);
330 ret = smbrun(add_script,NULL);
331 DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
333 smb_nscd_flush_group_cache();
341 /****************************************************************************
342 Delete a user from a UNIX group
343 ****************************************************************************/
345 int smb_delete_user_group(const char *unix_group, const char *unix_user)
350 /* defer to scripts */
352 if ( *lp_deluserfromgroup_script() ) {
353 pstrcpy(del_script, lp_deluserfromgroup_script());
354 pstring_sub(del_script, "%g", unix_group);
355 pstring_sub(del_script, "%u", unix_user);
356 ret = smbrun(del_script,NULL);
357 DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
359 smb_nscd_flush_group_cache();
368 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
371 return get_group_map_from_sid(sid, map) ?
372 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
375 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
378 return get_group_map_from_gid(gid, map) ?
379 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
382 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
385 return get_group_map_from_ntname(name, map) ?
386 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
389 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
392 return add_mapping_entry(map, TDB_INSERT) ?
393 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
396 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
399 return add_mapping_entry(map, TDB_REPLACE) ?
400 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
403 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
406 return group_map_remove(&sid) ?
407 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
410 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
411 const DOM_SID *sid, enum lsa_SidType sid_name_use,
412 GROUP_MAP **pp_rmap, size_t *p_num_entries,
415 return enum_group_mapping(sid, sid_name_use, pp_rmap, p_num_entries, unix_only) ?
416 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
419 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
420 const char *name, uint32 *rid)
423 enum lsa_SidType type;
431 DEBUG(10, ("Trying to create alias %s\n", name));
433 mem_ctx = talloc_new(NULL);
434 if (mem_ctx == NULL) {
435 return NT_STATUS_NO_MEMORY;
438 exists = lookup_name(mem_ctx, name, LOOKUP_NAME_ISOLATED,
439 NULL, NULL, &sid, &type);
440 TALLOC_FREE(mem_ctx);
443 return NT_STATUS_ALIAS_EXISTS;
446 if (!winbind_allocate_gid(&gid)) {
447 DEBUG(3, ("Could not get a gid out of winbind\n"));
448 return NT_STATUS_ACCESS_DENIED;
451 if (!pdb_new_rid(&new_rid)) {
452 DEBUG(0, ("Could not allocate a RID -- wasted a gid :-(\n"));
453 return NT_STATUS_ACCESS_DENIED;
456 DEBUG(10, ("Creating alias %s with gid %d and rid %d\n",
457 name, gid, new_rid));
459 sid_copy(&sid, get_global_sam_sid());
460 sid_append_rid(&sid, new_rid);
463 sid_copy(&map.sid, &sid);
464 map.sid_name_use = SID_NAME_ALIAS;
465 fstrcpy(map.nt_name, name);
466 fstrcpy(map.comment, "");
468 status = pdb_add_group_mapping_entry(&map);
470 if (!NT_STATUS_IS_OK(status)) {
471 DEBUG(0, ("Could not add group mapping entry for alias %s "
472 "(%s)\n", name, nt_errstr(status)));
481 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
484 return pdb_delete_group_mapping_entry(*sid);
487 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
489 struct acct_info *info)
493 if (!pdb_getgrsid(&map, *sid))
494 return NT_STATUS_NO_SUCH_ALIAS;
496 if ((map.sid_name_use != SID_NAME_ALIAS) &&
497 (map.sid_name_use != SID_NAME_WKN_GRP)) {
498 DEBUG(2, ("%s is a %s, expected an alias\n",
499 sid_string_static(sid),
500 sid_type_lookup(map.sid_name_use)));
501 return NT_STATUS_NO_SUCH_ALIAS;
504 fstrcpy(info->acct_name, map.nt_name);
505 fstrcpy(info->acct_desc, map.comment);
506 sid_peek_rid(&map.sid, &info->rid);
510 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
512 struct acct_info *info)
516 if (!pdb_getgrsid(&map, *sid))
517 return NT_STATUS_NO_SUCH_ALIAS;
519 fstrcpy(map.nt_name, info->acct_name);
520 fstrcpy(map.comment, info->acct_desc);
522 return pdb_update_group_mapping_entry(&map);
525 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
526 const DOM_SID *alias, const DOM_SID *member)
528 return add_aliasmem(alias, member);
531 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
532 const DOM_SID *alias, const DOM_SID *member)
534 return del_aliasmem(alias, member);
537 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
538 const DOM_SID *alias, DOM_SID **pp_members,
539 size_t *p_num_members)
541 return enum_aliasmem(alias, pp_members, p_num_members);
544 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
546 const DOM_SID *domain_sid,
547 const DOM_SID *members,
549 uint32 **pp_alias_rids,
550 size_t *p_num_alias_rids)
553 size_t i, num_alias_sids;
559 result = alias_memberships(members, num_members,
560 &alias_sids, &num_alias_sids);
562 if (!NT_STATUS_IS_OK(result))
565 *p_num_alias_rids = 0;
567 if (num_alias_sids == 0) {
568 TALLOC_FREE(alias_sids);
572 *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32, num_alias_sids);
573 if (*pp_alias_rids == NULL)
574 return NT_STATUS_NO_MEMORY;
576 for (i=0; i<num_alias_sids; i++) {
577 if (!sid_peek_check_rid(domain_sid, &alias_sids[i],
578 &(*pp_alias_rids)[*p_num_alias_rids]))
580 *p_num_alias_rids += 1;
583 TALLOC_FREE(alias_sids);
588 /**********************************************************************
589 no ops for passdb backends that don't implement group mapping
590 *********************************************************************/
592 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
595 return NT_STATUS_UNSUCCESSFUL;
598 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
601 return NT_STATUS_UNSUCCESSFUL;
604 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
607 return NT_STATUS_UNSUCCESSFUL;
610 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
613 return NT_STATUS_UNSUCCESSFUL;
616 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
619 return NT_STATUS_UNSUCCESSFUL;
622 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
625 return NT_STATUS_UNSUCCESSFUL;
628 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
629 enum lsa_SidType sid_name_use,
630 GROUP_MAP **rmap, size_t *num_entries,
633 return NT_STATUS_UNSUCCESSFUL;
636 /****************************************************************************
637 These need to be redirected through pdb_interface.c
638 ****************************************************************************/
639 BOOL pdb_get_dom_grp_info(const DOM_SID *sid, struct acct_info *info)
645 res = get_domain_group_from_sid(*sid, &map);
651 fstrcpy(info->acct_name, map.nt_name);
652 fstrcpy(info->acct_desc, map.comment);
653 sid_peek_rid(sid, &info->rid);
657 BOOL pdb_set_dom_grp_info(const DOM_SID *sid, const struct acct_info *info)
661 if (!get_domain_group_from_sid(*sid, &map))
664 fstrcpy(map.nt_name, info->acct_name);
665 fstrcpy(map.comment, info->acct_desc);
667 return NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map));
670 /********************************************************************
671 Really just intended to be called by smbd
672 ********************************************************************/
674 NTSTATUS pdb_create_builtin_alias(uint32 rid)
677 enum lsa_SidType type;
682 const char *name = NULL;
685 DEBUG(10, ("Trying to create builtin alias %d\n", rid));
687 if ( !sid_compose( &sid, &global_sid_Builtin, rid ) ) {
688 return NT_STATUS_NO_SUCH_ALIAS;
691 if ( (mem_ctx = talloc_new(NULL)) == NULL ) {
692 return NT_STATUS_NO_MEMORY;
695 if ( !lookup_sid(mem_ctx, &sid, NULL, &name, &type) ) {
696 TALLOC_FREE( mem_ctx );
697 return NT_STATUS_NO_SUCH_ALIAS;
700 /* validate RID so copy the name and move on */
702 fstrcpy( groupname, name );
703 TALLOC_FREE( mem_ctx );
705 if (!winbind_allocate_gid(&gid)) {
706 DEBUG(3, ("pdb_create_builtin_alias: Could not get a gid out of winbind\n"));
707 return NT_STATUS_ACCESS_DENIED;
710 DEBUG(10,("Creating alias %s with gid %d\n", name, gid));
713 sid_copy(&map.sid, &sid);
714 map.sid_name_use = SID_NAME_ALIAS;
715 fstrcpy(map.nt_name, name);
716 fstrcpy(map.comment, "");
718 status = pdb_add_group_mapping_entry(&map);
720 if (!NT_STATUS_IS_OK(status)) {
721 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
722 "(%s)\n", rid, nt_errstr(status)));