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"
26 #include "groupdb/mapping.h"
27 #include "../libcli/security/security.h"
28 #include "lib/winbind_util.h"
30 #include "groupdb/mapping_tdb.h"
32 static const struct mapping_backend
*backend
;
35 initialise a group mapping backend
37 static bool init_group_mapping(void)
39 if (backend
!= NULL
) {
40 /* already initialised */
44 backend
= groupdb_tdb_init();
46 return backend
!= NULL
;
49 /****************************************************************************
50 initialise first time the mapping list
51 ****************************************************************************/
52 NTSTATUS
add_initial_entry(gid_t gid
, const char *sid
, enum lsa_SidType sid_name_use
, const char *nt_name
, const char *comment
)
57 if(!init_group_mapping()) {
58 DEBUG(0,("failed to initialize group mapping\n"));
59 return NT_STATUS_UNSUCCESSFUL
;
62 map
= talloc_zero(NULL
, GROUP_MAP
);
64 return NT_STATUS_NO_MEMORY
;
68 if (!string_to_sid(&map
->sid
, sid
)) {
69 DEBUG(0, ("string_to_sid failed: %s", sid
));
70 status
= NT_STATUS_UNSUCCESSFUL
;
74 map
->sid_name_use
=sid_name_use
;
75 map
->nt_name
= talloc_strdup(map
, nt_name
);
77 status
= NT_STATUS_NO_MEMORY
;
82 map
->comment
= talloc_strdup(map
, comment
);
84 map
->comment
= talloc_strdup(map
, "");
87 status
= NT_STATUS_NO_MEMORY
;
91 status
= pdb_add_group_mapping_entry(map
);
98 static NTSTATUS
alias_memberships(const struct dom_sid
*members
, size_t num_members
,
99 struct dom_sid
**sids
, size_t *num
)
106 for (i
=0; i
<num_members
; i
++) {
107 NTSTATUS status
= backend
->one_alias_membership(&members
[i
], sids
, num
);
108 if (!NT_STATUS_IS_OK(status
))
114 struct aliasmem_closure
{
115 const struct dom_sid
*alias
;
116 struct dom_sid
**sids
;
124 * High level functions
125 * better to use them than the lower ones.
127 * we are checking if the group is in the mapping file
128 * and if the group is an existing unix group
132 /* get a domain group from it's SID */
134 bool get_domain_group_from_sid(struct dom_sid sid
, GROUP_MAP
*map
)
139 if(!init_group_mapping()) {
140 DEBUG(0,("failed to initialize group mapping\n"));
144 DEBUG(10, ("get_domain_group_from_sid\n"));
146 /* if the group is NOT in the database, it CAN NOT be a domain group */
149 ret
= pdb_getgrsid(map
, sid
);
152 /* special case check for rid 513 */
157 sid_peek_rid( &sid
, &rid
);
159 if ( rid
== DOMAIN_RID_USERS
) {
160 map
->nt_name
= talloc_strdup(map
, "None");
164 map
->comment
= talloc_strdup(map
, "Ordinary Users");
168 sid_copy( &map
->sid
, &sid
);
169 map
->sid_name_use
= SID_NAME_DOM_GRP
;
170 map
->gid
= (gid_t
)-1;
176 DEBUG(10, ("get_domain_group_from_sid: SID found in passdb\n"));
178 /* if it's not a domain group, continue */
179 if (map
->sid_name_use
!=SID_NAME_DOM_GRP
) {
183 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
189 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map
->gid
));
191 grp
= getgrgid(map
->gid
);
193 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
197 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
202 /****************************************************************************
203 Create a UNIX group on demand.
204 ****************************************************************************/
206 int smb_create_group(const char *unix_group
, gid_t
*new_gid
)
208 const struct loadparm_substitution
*lp_sub
=
209 loadparm_s3_global_substitution();
210 char *add_script
= NULL
;
217 /* defer to scripts */
219 if ( *lp_add_group_script(talloc_tos(), lp_sub
) ) {
220 TALLOC_CTX
*ctx
= talloc_tos();
222 add_script
= talloc_strdup(ctx
,
223 lp_add_group_script(ctx
, lp_sub
));
227 add_script
= talloc_string_sub(ctx
,
228 add_script
, "%g", unix_group
);
233 ret
= smbrun(add_script
, &fd
, NULL
);
234 DEBUG(ret
? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script
,ret
));
236 smb_nscd_flush_group_cache();
247 nread
= read(fd
, output
, sizeof(output
)-1);
249 output
[nread
] = '\0';
250 *new_gid
= (gid_t
)smb_strtoul(output
,
268 struct group
*grp
= getgrnam(unix_group
);
271 *new_gid
= grp
->gr_gid
;
277 /****************************************************************************
278 Delete a UNIX group on demand.
279 ****************************************************************************/
281 int smb_delete_group(const char *unix_group
)
283 const struct loadparm_substitution
*lp_sub
=
284 loadparm_s3_global_substitution();
285 char *del_script
= NULL
;
288 /* defer to scripts */
290 if ( *lp_delete_group_script(talloc_tos(), lp_sub
) ) {
291 TALLOC_CTX
*ctx
= talloc_tos();
293 del_script
= talloc_strdup(ctx
,
294 lp_delete_group_script(ctx
, lp_sub
));
298 del_script
= talloc_string_sub(ctx
,
299 del_script
, "%g", unix_group
);
303 ret
= smbrun(del_script
, NULL
, NULL
);
304 DEBUG(ret
? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script
,ret
));
306 smb_nscd_flush_group_cache();
314 /****************************************************************************
315 Set a user's primary UNIX group.
316 ****************************************************************************/
318 int smb_set_primary_group(const char *unix_group
, const char* unix_user
)
320 const struct loadparm_substitution
*lp_sub
=
321 loadparm_s3_global_substitution();
322 char *add_script
= NULL
;
325 /* defer to scripts */
327 if ( *lp_set_primary_group_script(talloc_tos(), lp_sub
) ) {
328 TALLOC_CTX
*ctx
= talloc_tos();
330 add_script
= talloc_strdup(ctx
,
331 lp_set_primary_group_script(ctx
, lp_sub
));
335 add_script
= talloc_all_string_sub(ctx
,
336 add_script
, "%g", unix_group
);
340 add_script
= talloc_string_sub(ctx
,
341 add_script
, "%u", unix_user
);
345 ret
= smbrun(add_script
, NULL
, NULL
);
347 DEBUG(ret
? 0 : 3,("smb_set_primary_group: "
348 "Running the command `%s' gave %d\n",add_script
,ret
));
350 smb_nscd_flush_group_cache();
358 /****************************************************************************
359 Add a user to a UNIX group.
360 ****************************************************************************/
362 int smb_add_user_group(const char *unix_group
, const char *unix_user
)
364 const struct loadparm_substitution
*lp_sub
=
365 loadparm_s3_global_substitution();
366 char *add_script
= NULL
;
369 /* defer to scripts */
371 if ( *lp_add_user_to_group_script(talloc_tos(), lp_sub
) ) {
372 TALLOC_CTX
*ctx
= talloc_tos();
374 add_script
= talloc_strdup(ctx
,
375 lp_add_user_to_group_script(ctx
, lp_sub
));
379 add_script
= talloc_string_sub(ctx
,
380 add_script
, "%g", unix_group
);
384 add_script
= talloc_string_sub2(ctx
,
385 add_script
, "%u", unix_user
, true, false, true);
389 ret
= smbrun(add_script
, NULL
, NULL
);
390 DEBUG(ret
? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script
,ret
));
392 smb_nscd_flush_group_cache();
400 /****************************************************************************
401 Delete a user from a UNIX group
402 ****************************************************************************/
404 int smb_delete_user_group(const char *unix_group
, const char *unix_user
)
406 const struct loadparm_substitution
*lp_sub
=
407 loadparm_s3_global_substitution();
408 char *del_script
= NULL
;
411 /* defer to scripts */
413 if ( *lp_delete_user_from_group_script(talloc_tos(), lp_sub
) ) {
414 TALLOC_CTX
*ctx
= talloc_tos();
416 del_script
= talloc_strdup(ctx
,
417 lp_delete_user_from_group_script(ctx
, lp_sub
));
421 del_script
= talloc_string_sub(ctx
,
422 del_script
, "%g", unix_group
);
426 del_script
= talloc_string_sub2(ctx
,
427 del_script
, "%u", unix_user
, true, false, true);
431 ret
= smbrun(del_script
, NULL
, NULL
);
432 DEBUG(ret
? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script
,ret
));
434 smb_nscd_flush_group_cache();
443 NTSTATUS
pdb_default_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
446 if (!init_group_mapping()) {
447 DEBUG(0,("failed to initialize group mapping\n"));
448 return NT_STATUS_UNSUCCESSFUL
;
450 return backend
->get_group_map_from_sid(sid
, map
) ?
451 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
454 NTSTATUS
pdb_default_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
457 if (!init_group_mapping()) {
458 DEBUG(0,("failed to initialize group mapping\n"));
459 return NT_STATUS_UNSUCCESSFUL
;
461 return backend
->get_group_map_from_gid(gid
, map
) ?
462 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
465 NTSTATUS
pdb_default_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
468 if (!init_group_mapping()) {
469 DEBUG(0,("failed to initialize group mapping\n"));
470 return NT_STATUS_UNSUCCESSFUL
;
472 return backend
->get_group_map_from_ntname(name
, map
) ?
473 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
476 NTSTATUS
pdb_default_add_group_mapping_entry(struct pdb_methods
*methods
,
479 if (!init_group_mapping()) {
480 DEBUG(0,("failed to initialize group mapping\n"));
481 return NT_STATUS_UNSUCCESSFUL
;
483 return backend
->add_mapping_entry(map
, TDB_INSERT
) ?
484 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
487 NTSTATUS
pdb_default_update_group_mapping_entry(struct pdb_methods
*methods
,
490 if (!init_group_mapping()) {
491 DEBUG(0,("failed to initialize group mapping\n"));
492 return NT_STATUS_UNSUCCESSFUL
;
494 return backend
->add_mapping_entry(map
, TDB_REPLACE
) ?
495 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
498 NTSTATUS
pdb_default_delete_group_mapping_entry(struct pdb_methods
*methods
,
501 if (!init_group_mapping()) {
502 DEBUG(0,("failed to initialize group mapping\n"));
503 return NT_STATUS_UNSUCCESSFUL
;
505 return backend
->group_map_remove(&sid
) ?
506 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
509 NTSTATUS
pdb_default_enum_group_mapping(struct pdb_methods
*methods
,
510 const struct dom_sid
*sid
,
511 enum lsa_SidType sid_name_use
,
512 GROUP_MAP
***pp_rmap
,
513 size_t *p_num_entries
,
516 if (!init_group_mapping()) {
517 DEBUG(0,("failed to initialize group mapping\n"));
518 return NT_STATUS_UNSUCCESSFUL
;
520 return backend
->enum_group_mapping(sid
, sid_name_use
, pp_rmap
, p_num_entries
, unix_only
) ?
521 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
524 NTSTATUS
pdb_default_create_alias(struct pdb_methods
*methods
,
525 const char *name
, uint32_t *rid
)
528 enum lsa_SidType type
;
536 DEBUG(10, ("Trying to create alias %s\n", name
));
538 mem_ctx
= talloc_new(NULL
);
539 if (mem_ctx
== NULL
) {
540 return NT_STATUS_NO_MEMORY
;
543 exists
= lookup_name(mem_ctx
, name
, LOOKUP_NAME_LOCAL
,
544 NULL
, NULL
, &sid
, &type
);
547 status
= NT_STATUS_ALIAS_EXISTS
;
551 if (!pdb_new_rid(&new_rid
)) {
552 DEBUG(0, ("Could not allocate a RID.\n"));
553 status
= NT_STATUS_ACCESS_DENIED
;
557 sid_compose(&sid
, get_global_sam_sid(), new_rid
);
559 if (!winbind_allocate_gid(&gid
)) {
560 DEBUG(3, ("Could not get a gid out of winbind - "
561 "wasted a rid :-(\n"));
562 status
= NT_STATUS_ACCESS_DENIED
;
566 DEBUG(10, ("Creating alias %s with gid %u and rid %u\n",
567 name
, (unsigned int)gid
, (unsigned int)new_rid
));
569 map
= talloc_zero(mem_ctx
, GROUP_MAP
);
571 status
= NT_STATUS_NO_MEMORY
;
576 sid_copy(&map
->sid
, &sid
);
577 map
->sid_name_use
= SID_NAME_ALIAS
;
578 map
->nt_name
= talloc_strdup(map
, name
);
580 status
= NT_STATUS_NO_MEMORY
;
583 map
->comment
= talloc_strdup(map
, "");
585 status
= NT_STATUS_NO_MEMORY
;
589 status
= pdb_add_group_mapping_entry(map
);
591 if (!NT_STATUS_IS_OK(status
)) {
592 DEBUG(0, ("Could not add group mapping entry for alias %s "
593 "(%s)\n", name
, nt_errstr(status
)));
600 TALLOC_FREE(mem_ctx
);
604 NTSTATUS
pdb_default_delete_alias(struct pdb_methods
*methods
,
605 const struct dom_sid
*sid
)
607 return pdb_delete_group_mapping_entry(*sid
);
610 NTSTATUS
pdb_default_get_aliasinfo(struct pdb_methods
*methods
,
611 const struct dom_sid
*sid
,
612 struct acct_info
*info
)
614 NTSTATUS status
= NT_STATUS_OK
;
617 map
= talloc_zero(NULL
, GROUP_MAP
);
619 return NT_STATUS_NO_MEMORY
;
622 if (!pdb_getgrsid(map
, *sid
)) {
623 status
= NT_STATUS_NO_SUCH_ALIAS
;
627 if ((map
->sid_name_use
!= SID_NAME_ALIAS
) &&
628 (map
->sid_name_use
!= SID_NAME_WKN_GRP
)) {
629 struct dom_sid_buf buf
;
630 DEBUG(2, ("%s is a %s, expected an alias\n",
631 dom_sid_str_buf(sid
, &buf
),
632 sid_type_lookup(map
->sid_name_use
)));
633 status
= NT_STATUS_NO_SUCH_ALIAS
;
637 info
->acct_name
= talloc_move(info
, &map
->nt_name
);
638 if (!info
->acct_name
) {
639 status
= NT_STATUS_NO_MEMORY
;
642 info
->acct_desc
= talloc_move(info
, &map
->comment
);
643 if (!info
->acct_desc
) {
644 status
= NT_STATUS_NO_MEMORY
;
647 sid_peek_rid(&map
->sid
, &info
->rid
);
654 NTSTATUS
pdb_default_set_aliasinfo(struct pdb_methods
*methods
,
655 const struct dom_sid
*sid
,
656 struct acct_info
*info
)
661 map
= talloc_zero(NULL
, GROUP_MAP
);
663 return NT_STATUS_NO_MEMORY
;
666 if (!pdb_getgrsid(map
, *sid
)) {
667 status
= NT_STATUS_NO_SUCH_ALIAS
;
671 map
->nt_name
= talloc_strdup(map
, info
->acct_name
);
673 status
= NT_STATUS_NO_MEMORY
;
676 map
->comment
= talloc_strdup(map
, info
->acct_desc
);
678 status
= NT_STATUS_NO_MEMORY
;
682 status
= pdb_update_group_mapping_entry(map
);
689 NTSTATUS
pdb_default_add_aliasmem(struct pdb_methods
*methods
,
690 const struct dom_sid
*alias
, const struct dom_sid
*member
)
692 if (!init_group_mapping()) {
693 DEBUG(0,("failed to initialize group mapping\n"));
694 return NT_STATUS_UNSUCCESSFUL
;
696 return backend
->add_aliasmem(alias
, member
);
699 NTSTATUS
pdb_default_del_aliasmem(struct pdb_methods
*methods
,
700 const struct dom_sid
*alias
, const struct dom_sid
*member
)
702 if (!init_group_mapping()) {
703 DEBUG(0,("failed to initialize group mapping\n"));
704 return NT_STATUS_UNSUCCESSFUL
;
706 return backend
->del_aliasmem(alias
, member
);
709 NTSTATUS
pdb_default_enum_aliasmem(struct pdb_methods
*methods
,
710 const struct dom_sid
*alias
, TALLOC_CTX
*mem_ctx
,
711 struct dom_sid
**pp_members
, size_t *p_num_members
)
713 if (!init_group_mapping()) {
714 DEBUG(0,("failed to initialize group mapping\n"));
715 return NT_STATUS_UNSUCCESSFUL
;
717 return backend
->enum_aliasmem(alias
, mem_ctx
, pp_members
,
721 NTSTATUS
pdb_default_alias_memberships(struct pdb_methods
*methods
,
723 const struct dom_sid
*domain_sid
,
724 const struct dom_sid
*members
,
726 uint32_t **pp_alias_rids
,
727 size_t *p_num_alias_rids
)
729 struct dom_sid
*alias_sids
;
730 size_t i
, num_alias_sids
;
733 if (!init_group_mapping()) {
734 DEBUG(0,("failed to initialize group mapping\n"));
735 return NT_STATUS_UNSUCCESSFUL
;
741 result
= alias_memberships(members
, num_members
,
742 &alias_sids
, &num_alias_sids
);
744 if (!NT_STATUS_IS_OK(result
))
747 *p_num_alias_rids
= 0;
749 if (num_alias_sids
== 0) {
750 TALLOC_FREE(alias_sids
);
754 *pp_alias_rids
= talloc_array(mem_ctx
, uint32_t, num_alias_sids
);
755 if (*pp_alias_rids
== NULL
)
756 return NT_STATUS_NO_MEMORY
;
758 for (i
=0; i
<num_alias_sids
; i
++) {
759 if (!sid_peek_check_rid(domain_sid
, &alias_sids
[i
],
760 &(*pp_alias_rids
)[*p_num_alias_rids
]))
762 *p_num_alias_rids
+= 1;
765 TALLOC_FREE(alias_sids
);
770 /**********************************************************************
771 no ops for passdb backends that don't implement group mapping
772 *********************************************************************/
774 NTSTATUS
pdb_nop_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
777 return NT_STATUS_UNSUCCESSFUL
;
780 NTSTATUS
pdb_nop_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
783 return NT_STATUS_UNSUCCESSFUL
;
786 NTSTATUS
pdb_nop_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
789 return NT_STATUS_UNSUCCESSFUL
;
792 NTSTATUS
pdb_nop_add_group_mapping_entry(struct pdb_methods
*methods
,
795 return NT_STATUS_UNSUCCESSFUL
;
798 NTSTATUS
pdb_nop_update_group_mapping_entry(struct pdb_methods
*methods
,
801 return NT_STATUS_UNSUCCESSFUL
;
804 NTSTATUS
pdb_nop_delete_group_mapping_entry(struct pdb_methods
*methods
,
807 return NT_STATUS_UNSUCCESSFUL
;
810 NTSTATUS
pdb_nop_enum_group_mapping(struct pdb_methods
*methods
,
811 enum lsa_SidType sid_name_use
,
812 GROUP_MAP
**rmap
, size_t *num_entries
,
815 return NT_STATUS_UNSUCCESSFUL
;
819 * @brief Add a new group mapping
821 * @param[in] gid gid to use to store the mapping. If gid is 0,
822 * new gid will be allocated from winbind
824 * @return Normal NTSTATUS return
826 NTSTATUS
pdb_create_builtin_alias(uint32_t rid
, gid_t gid
)
829 enum lsa_SidType type
;
833 const char *name
= NULL
;
835 DEBUG(10, ("Trying to create builtin alias %d\n", rid
));
837 if ( !sid_compose( &sid
, &global_sid_Builtin
, rid
) ) {
838 return NT_STATUS_NO_SUCH_ALIAS
;
841 /* use map as overall temp mem context */
842 map
= talloc_zero(NULL
, GROUP_MAP
);
844 return NT_STATUS_NO_MEMORY
;
847 if (!lookup_sid(map
, &sid
, NULL
, &name
, &type
)) {
848 status
= NT_STATUS_NO_SUCH_ALIAS
;
853 if (!winbind_allocate_gid(&gidformap
)) {
854 DEBUG(3, ("pdb_create_builtin_alias: Could not get a "
855 "gid out of winbind\n"));
856 status
= NT_STATUS_ACCESS_DENIED
;
863 DEBUG(10, ("Creating alias %s with gid %u\n", name
,
864 (unsigned) gidformap
));
866 map
->gid
= gidformap
;
867 sid_copy(&map
->sid
, &sid
);
868 map
->sid_name_use
= SID_NAME_ALIAS
;
869 map
->nt_name
= talloc_strdup(map
, name
);
871 status
= NT_STATUS_NO_MEMORY
;
874 map
->comment
= talloc_strdup(map
, "");
876 status
= NT_STATUS_NO_MEMORY
;
880 status
= pdb_add_group_mapping_entry(map
);
882 if (!NT_STATUS_IS_OK(status
)) {
883 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
884 "(%s)\n", rid
, nt_errstr(status
)));