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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "system/passwd.h"
25 #include "groupdb/mapping.h"
26 #include "../libcli/security/security.h"
27 #include "lib/winbind_util.h"
29 static const struct mapping_backend *backend;
32 initialise a group mapping backend
34 static bool init_group_mapping(void)
36 if (backend != NULL) {
37 /* already initialised */
41 backend = groupdb_tdb_init();
43 return backend != NULL;
46 /****************************************************************************
47 initialise first time the mapping list
48 ****************************************************************************/
49 NTSTATUS add_initial_entry(gid_t gid, const char *sid, enum lsa_SidType sid_name_use, const char *nt_name, const char *comment)
53 if(!init_group_mapping()) {
54 DEBUG(0,("failed to initialize group mapping\n"));
55 return NT_STATUS_UNSUCCESSFUL;
59 if (!string_to_sid(&map.sid, sid)) {
60 DEBUG(0, ("string_to_sid failed: %s", sid));
61 return NT_STATUS_UNSUCCESSFUL;
64 map.sid_name_use=sid_name_use;
65 fstrcpy(map.nt_name, nt_name);
66 fstrcpy(map.comment, comment);
68 return pdb_add_group_mapping_entry(&map);
71 static NTSTATUS alias_memberships(const struct dom_sid *members, size_t num_members,
72 struct dom_sid **sids, size_t *num)
79 for (i=0; i<num_members; i++) {
80 NTSTATUS status = backend->one_alias_membership(&members[i], sids, num);
81 if (!NT_STATUS_IS_OK(status))
87 struct aliasmem_closure {
88 const struct dom_sid *alias;
89 struct dom_sid **sids;
97 * High level functions
98 * better to use them than the lower ones.
100 * we are checking if the group is in the mapping file
101 * and if the group is an existing unix group
105 /* get a domain group from it's SID */
107 bool get_domain_group_from_sid(struct dom_sid sid, GROUP_MAP *map)
112 if(!init_group_mapping()) {
113 DEBUG(0,("failed to initialize group mapping\n"));
117 DEBUG(10, ("get_domain_group_from_sid\n"));
119 /* if the group is NOT in the database, it CAN NOT be a domain group */
122 ret = pdb_getgrsid(map, sid);
125 /* special case check for rid 513 */
130 sid_peek_rid( &sid, &rid );
132 if ( rid == DOMAIN_RID_USERS ) {
133 fstrcpy( map->nt_name, "None" );
134 fstrcpy( map->comment, "Ordinary Users" );
135 sid_copy( &map->sid, &sid );
136 map->sid_name_use = SID_NAME_DOM_GRP;
137 map->gid = (gid_t)-1;
143 DEBUG(10, ("get_domain_group_from_sid: SID found in passdb\n"));
145 /* if it's not a domain group, continue */
146 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
150 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
156 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
158 grp = getgrgid(map->gid);
160 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
164 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
169 /****************************************************************************
170 Create a UNIX group on demand.
171 ****************************************************************************/
173 int smb_create_group(const char *unix_group, gid_t *new_gid)
175 char *add_script = NULL;
181 /* defer to scripts */
183 if ( *lp_addgroup_script() ) {
184 TALLOC_CTX *ctx = talloc_tos();
186 add_script = talloc_strdup(ctx,
187 lp_addgroup_script());
191 add_script = talloc_string_sub(ctx,
192 add_script, "%g", unix_group);
197 ret = smbrun(add_script, &fd);
198 DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
200 smb_nscd_flush_group_cache();
209 if (read(fd, output, sizeof(output)) > 0) {
210 *new_gid = (gid_t)strtoul(output, NULL, 10);
219 struct group *grp = getgrnam(unix_group);
222 *new_gid = grp->gr_gid;
228 /****************************************************************************
229 Delete a UNIX group on demand.
230 ****************************************************************************/
232 int smb_delete_group(const char *unix_group)
234 char *del_script = NULL;
237 /* defer to scripts */
239 if ( *lp_delgroup_script() ) {
240 TALLOC_CTX *ctx = talloc_tos();
242 del_script = talloc_strdup(ctx,
243 lp_delgroup_script());
247 del_script = talloc_string_sub(ctx,
248 del_script, "%g", unix_group);
252 ret = smbrun(del_script,NULL);
253 DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
255 smb_nscd_flush_group_cache();
263 /****************************************************************************
264 Set a user's primary UNIX group.
265 ****************************************************************************/
267 int smb_set_primary_group(const char *unix_group, const char* unix_user)
269 char *add_script = NULL;
272 /* defer to scripts */
274 if ( *lp_setprimarygroup_script() ) {
275 TALLOC_CTX *ctx = talloc_tos();
277 add_script = talloc_strdup(ctx,
278 lp_setprimarygroup_script());
282 add_script = talloc_all_string_sub(ctx,
283 add_script, "%g", unix_group);
287 add_script = talloc_string_sub(ctx,
288 add_script, "%u", unix_user);
292 ret = smbrun(add_script,NULL);
294 DEBUG(ret ? 0 : 3,("smb_set_primary_group: "
295 "Running the command `%s' gave %d\n",add_script,ret));
297 smb_nscd_flush_group_cache();
305 /****************************************************************************
306 Add a user to a UNIX group.
307 ****************************************************************************/
309 int smb_add_user_group(const char *unix_group, const char *unix_user)
311 char *add_script = NULL;
314 /* defer to scripts */
316 if ( *lp_addusertogroup_script() ) {
317 TALLOC_CTX *ctx = talloc_tos();
319 add_script = talloc_strdup(ctx,
320 lp_addusertogroup_script());
324 add_script = talloc_string_sub(ctx,
325 add_script, "%g", unix_group);
329 add_script = talloc_string_sub2(ctx,
330 add_script, "%u", unix_user, true, false, true);
334 ret = smbrun(add_script,NULL);
335 DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
337 smb_nscd_flush_group_cache();
345 /****************************************************************************
346 Delete a user from a UNIX group
347 ****************************************************************************/
349 int smb_delete_user_group(const char *unix_group, const char *unix_user)
351 char *del_script = NULL;
354 /* defer to scripts */
356 if ( *lp_deluserfromgroup_script() ) {
357 TALLOC_CTX *ctx = talloc_tos();
359 del_script = talloc_strdup(ctx,
360 lp_deluserfromgroup_script());
364 del_script = talloc_string_sub(ctx,
365 del_script, "%g", unix_group);
369 del_script = talloc_string_sub2(ctx,
370 del_script, "%u", unix_user, true, false, true);
374 ret = smbrun(del_script,NULL);
375 DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
377 smb_nscd_flush_group_cache();
386 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
389 if (!init_group_mapping()) {
390 DEBUG(0,("failed to initialize group mapping\n"));
391 return NT_STATUS_UNSUCCESSFUL;
393 return backend->get_group_map_from_sid(sid, map) ?
394 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
397 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
400 if (!init_group_mapping()) {
401 DEBUG(0,("failed to initialize group mapping\n"));
402 return NT_STATUS_UNSUCCESSFUL;
404 return backend->get_group_map_from_gid(gid, map) ?
405 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
408 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
411 if (!init_group_mapping()) {
412 DEBUG(0,("failed to initialize group mapping\n"));
413 return NT_STATUS_UNSUCCESSFUL;
415 return backend->get_group_map_from_ntname(name, map) ?
416 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
419 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
422 if (!init_group_mapping()) {
423 DEBUG(0,("failed to initialize group mapping\n"));
424 return NT_STATUS_UNSUCCESSFUL;
426 return backend->add_mapping_entry(map, TDB_INSERT) ?
427 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
430 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
433 if (!init_group_mapping()) {
434 DEBUG(0,("failed to initialize group mapping\n"));
435 return NT_STATUS_UNSUCCESSFUL;
437 return backend->add_mapping_entry(map, TDB_REPLACE) ?
438 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
441 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
444 if (!init_group_mapping()) {
445 DEBUG(0,("failed to initialize group mapping\n"));
446 return NT_STATUS_UNSUCCESSFUL;
448 return backend->group_map_remove(&sid) ?
449 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
452 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
453 const struct dom_sid *sid, enum lsa_SidType sid_name_use,
454 GROUP_MAP **pp_rmap, size_t *p_num_entries,
457 if (!init_group_mapping()) {
458 DEBUG(0,("failed to initialize group mapping\n"));
459 return NT_STATUS_UNSUCCESSFUL;
461 return backend->enum_group_mapping(sid, sid_name_use, pp_rmap, p_num_entries, unix_only) ?
462 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
465 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
466 const char *name, uint32 *rid)
469 enum lsa_SidType type;
477 DEBUG(10, ("Trying to create alias %s\n", name));
479 mem_ctx = talloc_new(NULL);
480 if (mem_ctx == NULL) {
481 return NT_STATUS_NO_MEMORY;
484 exists = lookup_name(mem_ctx, name, LOOKUP_NAME_LOCAL,
485 NULL, NULL, &sid, &type);
486 TALLOC_FREE(mem_ctx);
489 return NT_STATUS_ALIAS_EXISTS;
492 if (!pdb_new_rid(&new_rid)) {
493 DEBUG(0, ("Could not allocate a RID.\n"));
494 return NT_STATUS_ACCESS_DENIED;
497 sid_compose(&sid, get_global_sam_sid(), new_rid);
499 if (!winbind_allocate_gid(&gid)) {
500 DEBUG(3, ("Could not get a gid out of winbind - "
501 "wasted a rid :-(\n"));
502 return NT_STATUS_ACCESS_DENIED;
505 DEBUG(10, ("Creating alias %s with gid %u and rid %u\n",
506 name, (unsigned int)gid, (unsigned int)new_rid));
509 sid_copy(&map.sid, &sid);
510 map.sid_name_use = SID_NAME_ALIAS;
511 fstrcpy(map.nt_name, name);
512 fstrcpy(map.comment, "");
514 status = pdb_add_group_mapping_entry(&map);
516 if (!NT_STATUS_IS_OK(status)) {
517 DEBUG(0, ("Could not add group mapping entry for alias %s "
518 "(%s)\n", name, nt_errstr(status)));
527 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
528 const struct dom_sid *sid)
530 return pdb_delete_group_mapping_entry(*sid);
533 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
534 const struct dom_sid *sid,
535 struct acct_info *info)
539 if (!pdb_getgrsid(&map, *sid))
540 return NT_STATUS_NO_SUCH_ALIAS;
542 if ((map.sid_name_use != SID_NAME_ALIAS) &&
543 (map.sid_name_use != SID_NAME_WKN_GRP)) {
544 DEBUG(2, ("%s is a %s, expected an alias\n",
546 sid_type_lookup(map.sid_name_use)));
547 return NT_STATUS_NO_SUCH_ALIAS;
550 fstrcpy(info->acct_name, map.nt_name);
551 fstrcpy(info->acct_desc, map.comment);
552 sid_peek_rid(&map.sid, &info->rid);
556 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
557 const struct dom_sid *sid,
558 struct acct_info *info)
562 if (!pdb_getgrsid(&map, *sid))
563 return NT_STATUS_NO_SUCH_ALIAS;
565 fstrcpy(map.nt_name, info->acct_name);
566 fstrcpy(map.comment, info->acct_desc);
568 return pdb_update_group_mapping_entry(&map);
571 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
572 const struct dom_sid *alias, const struct dom_sid *member)
574 if (!init_group_mapping()) {
575 DEBUG(0,("failed to initialize group mapping\n"));
576 return NT_STATUS_UNSUCCESSFUL;
578 return backend->add_aliasmem(alias, member);
581 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
582 const struct dom_sid *alias, const struct dom_sid *member)
584 if (!init_group_mapping()) {
585 DEBUG(0,("failed to initialize group mapping\n"));
586 return NT_STATUS_UNSUCCESSFUL;
588 return backend->del_aliasmem(alias, member);
591 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
592 const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
593 struct dom_sid **pp_members, size_t *p_num_members)
595 if (!init_group_mapping()) {
596 DEBUG(0,("failed to initialize group mapping\n"));
597 return NT_STATUS_UNSUCCESSFUL;
599 return backend->enum_aliasmem(alias, mem_ctx, pp_members,
603 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
605 const struct dom_sid *domain_sid,
606 const struct dom_sid *members,
608 uint32 **pp_alias_rids,
609 size_t *p_num_alias_rids)
611 struct dom_sid *alias_sids;
612 size_t i, num_alias_sids;
615 if (!init_group_mapping()) {
616 DEBUG(0,("failed to initialize group mapping\n"));
617 return NT_STATUS_UNSUCCESSFUL;
623 result = alias_memberships(members, num_members,
624 &alias_sids, &num_alias_sids);
626 if (!NT_STATUS_IS_OK(result))
629 *p_num_alias_rids = 0;
631 if (num_alias_sids == 0) {
632 TALLOC_FREE(alias_sids);
636 *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32, num_alias_sids);
637 if (*pp_alias_rids == NULL)
638 return NT_STATUS_NO_MEMORY;
640 for (i=0; i<num_alias_sids; i++) {
641 if (!sid_peek_check_rid(domain_sid, &alias_sids[i],
642 &(*pp_alias_rids)[*p_num_alias_rids]))
644 *p_num_alias_rids += 1;
647 TALLOC_FREE(alias_sids);
652 /**********************************************************************
653 no ops for passdb backends that don't implement group mapping
654 *********************************************************************/
656 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
659 return NT_STATUS_UNSUCCESSFUL;
662 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
665 return NT_STATUS_UNSUCCESSFUL;
668 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
671 return NT_STATUS_UNSUCCESSFUL;
674 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
677 return NT_STATUS_UNSUCCESSFUL;
680 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
683 return NT_STATUS_UNSUCCESSFUL;
686 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
689 return NT_STATUS_UNSUCCESSFUL;
692 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
693 enum lsa_SidType sid_name_use,
694 GROUP_MAP **rmap, size_t *num_entries,
697 return NT_STATUS_UNSUCCESSFUL;
700 /****************************************************************************
701 These need to be redirected through pdb_interface.c
702 ****************************************************************************/
703 bool pdb_get_dom_grp_info(const struct dom_sid *sid, struct acct_info *info)
709 res = get_domain_group_from_sid(*sid, &map);
715 fstrcpy(info->acct_name, map.nt_name);
716 fstrcpy(info->acct_desc, map.comment);
717 sid_peek_rid(sid, &info->rid);
721 bool pdb_set_dom_grp_info(const struct dom_sid *sid, const struct acct_info *info)
725 if (!get_domain_group_from_sid(*sid, &map))
728 fstrcpy(map.nt_name, info->acct_name);
729 fstrcpy(map.comment, info->acct_desc);
731 return NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map));
734 /********************************************************************
735 Really just intended to be called by smbd
736 ********************************************************************/
738 NTSTATUS pdb_create_builtin_alias(uint32 rid)
741 enum lsa_SidType type;
746 const char *name = NULL;
749 DEBUG(10, ("Trying to create builtin alias %d\n", rid));
751 if ( !sid_compose( &sid, &global_sid_Builtin, rid ) ) {
752 return NT_STATUS_NO_SUCH_ALIAS;
755 if ( (mem_ctx = talloc_new(NULL)) == NULL ) {
756 return NT_STATUS_NO_MEMORY;
759 if ( !lookup_sid(mem_ctx, &sid, NULL, &name, &type) ) {
760 TALLOC_FREE( mem_ctx );
761 return NT_STATUS_NO_SUCH_ALIAS;
764 /* validate RID so copy the name and move on */
766 fstrcpy( groupname, name );
767 TALLOC_FREE( mem_ctx );
769 if (!winbind_allocate_gid(&gid)) {
770 DEBUG(3, ("pdb_create_builtin_alias: Could not get a gid out of winbind\n"));
771 return NT_STATUS_ACCESS_DENIED;
774 DEBUG(10,("Creating alias %s with gid %u\n", groupname, (unsigned int)gid));
777 sid_copy(&map.sid, &sid);
778 map.sid_name_use = SID_NAME_ALIAS;
779 fstrcpy(map.nt_name, groupname);
780 fstrcpy(map.comment, "");
782 status = pdb_add_group_mapping_entry(&map);
784 if (!NT_STATUS_IS_OK(status)) {
785 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
786 "(%s)\n", rid, nt_errstr(status)));