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"
31 #include "lib/util/smb_strtox.h"
33 static const struct mapping_backend
*backend
;
36 initialise a group mapping backend
38 static bool init_group_mapping(void)
40 if (backend
!= NULL
) {
41 /* already initialised */
45 backend
= groupdb_tdb_init();
47 return backend
!= NULL
;
50 /****************************************************************************
51 initialise first time the mapping list
52 ****************************************************************************/
53 NTSTATUS
add_initial_entry(gid_t gid
, const char *sid
, enum lsa_SidType sid_name_use
, const char *nt_name
, const char *comment
)
58 if(!init_group_mapping()) {
59 DEBUG(0,("failed to initialize group mapping\n"));
60 return NT_STATUS_UNSUCCESSFUL
;
63 map
= talloc_zero(NULL
, GROUP_MAP
);
65 return NT_STATUS_NO_MEMORY
;
69 if (!string_to_sid(&map
->sid
, sid
)) {
70 DEBUG(0, ("string_to_sid failed: %s\n", sid
));
71 status
= NT_STATUS_UNSUCCESSFUL
;
75 map
->sid_name_use
=sid_name_use
;
76 map
->nt_name
= talloc_strdup(map
, nt_name
);
78 status
= NT_STATUS_NO_MEMORY
;
83 map
->comment
= talloc_strdup(map
, comment
);
85 map
->comment
= talloc_strdup(map
, "");
88 status
= NT_STATUS_NO_MEMORY
;
92 status
= pdb_add_group_mapping_entry(map
);
99 static NTSTATUS
alias_memberships(const struct dom_sid
*members
, size_t num_members
,
100 struct dom_sid
**sids
, size_t *num
)
107 for (i
=0; i
<num_members
; i
++) {
108 NTSTATUS status
= backend
->one_alias_membership(&members
[i
], sids
, num
);
109 if (!NT_STATUS_IS_OK(status
))
115 struct aliasmem_closure
{
116 const struct dom_sid
*alias
;
117 struct dom_sid
**sids
;
125 * High level functions
126 * better to use them than the lower ones.
128 * we are checking if the group is in the mapping file
129 * and if the group is an existing unix group
133 /* get a domain group from it's SID */
135 bool get_domain_group_from_sid(struct dom_sid sid
, GROUP_MAP
*map
)
140 if(!init_group_mapping()) {
141 DEBUG(0,("failed to initialize group mapping\n"));
145 DEBUG(10, ("get_domain_group_from_sid\n"));
147 /* if the group is NOT in the database, it CAN NOT be a domain group */
150 ret
= pdb_getgrsid(map
, sid
);
153 /* special case check for rid 513 */
158 sid_peek_rid( &sid
, &rid
);
160 if ( rid
== DOMAIN_RID_USERS
) {
161 map
->nt_name
= talloc_strdup(map
, "None");
165 map
->comment
= talloc_strdup(map
, "Ordinary Users");
169 sid_copy( &map
->sid
, &sid
);
170 map
->sid_name_use
= SID_NAME_DOM_GRP
;
171 map
->gid
= (gid_t
)-1;
177 DEBUG(10, ("get_domain_group_from_sid: SID found in passdb\n"));
179 /* if it's not a domain group, continue */
180 if (map
->sid_name_use
!=SID_NAME_DOM_GRP
) {
184 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
190 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map
->gid
));
192 grp
= getgrgid(map
->gid
);
194 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
198 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
203 /****************************************************************************
204 Create a UNIX group on demand.
205 ****************************************************************************/
207 int smb_create_group(const char *unix_group
, gid_t
*new_gid
)
209 const struct loadparm_substitution
*lp_sub
=
210 loadparm_s3_global_substitution();
211 char *add_script
= NULL
;
218 /* defer to scripts */
220 if ( *lp_add_group_script(talloc_tos(), lp_sub
) ) {
221 TALLOC_CTX
*ctx
= talloc_tos();
223 add_script
= talloc_strdup(ctx
,
224 lp_add_group_script(ctx
, lp_sub
));
228 add_script
= talloc_string_sub(ctx
,
229 add_script
, "%g", unix_group
);
234 ret
= smbrun(add_script
, &fd
, NULL
);
235 DEBUG(ret
? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script
,ret
));
237 smb_nscd_flush_group_cache();
248 nread
= read(fd
, output
, sizeof(output
)-1);
250 output
[nread
] = '\0';
251 *new_gid
= (gid_t
)smb_strtoul(output
,
269 struct group
*grp
= getgrnam(unix_group
);
272 *new_gid
= grp
->gr_gid
;
278 /****************************************************************************
279 Delete a UNIX group on demand.
280 ****************************************************************************/
282 int smb_delete_group(const char *unix_group
)
284 const struct loadparm_substitution
*lp_sub
=
285 loadparm_s3_global_substitution();
286 char *del_script
= NULL
;
289 /* defer to scripts */
291 if ( *lp_delete_group_script(talloc_tos(), lp_sub
) ) {
292 TALLOC_CTX
*ctx
= talloc_tos();
294 del_script
= talloc_strdup(ctx
,
295 lp_delete_group_script(ctx
, lp_sub
));
299 del_script
= talloc_string_sub(ctx
,
300 del_script
, "%g", unix_group
);
304 ret
= smbrun(del_script
, NULL
, NULL
);
305 DEBUG(ret
? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script
,ret
));
307 smb_nscd_flush_group_cache();
315 /****************************************************************************
316 Set a user's primary UNIX group.
317 ****************************************************************************/
319 int smb_set_primary_group(const char *unix_group
, const char* unix_user
)
321 const struct loadparm_substitution
*lp_sub
=
322 loadparm_s3_global_substitution();
323 char *add_script
= NULL
;
326 /* defer to scripts */
328 if ( *lp_set_primary_group_script(talloc_tos(), lp_sub
) ) {
329 TALLOC_CTX
*ctx
= talloc_tos();
331 add_script
= talloc_strdup(ctx
,
332 lp_set_primary_group_script(ctx
, lp_sub
));
336 add_script
= talloc_all_string_sub(ctx
,
337 add_script
, "%g", unix_group
);
341 add_script
= talloc_string_sub(ctx
,
342 add_script
, "%u", unix_user
);
346 ret
= smbrun(add_script
, NULL
, NULL
);
348 DEBUG(ret
? 0 : 3,("smb_set_primary_group: "
349 "Running the command `%s' gave %d\n",add_script
,ret
));
351 smb_nscd_flush_group_cache();
359 /****************************************************************************
360 Add a user to a UNIX group.
361 ****************************************************************************/
363 int smb_add_user_group(const char *unix_group
, const char *unix_user
)
365 const struct loadparm_substitution
*lp_sub
=
366 loadparm_s3_global_substitution();
367 char *add_script
= NULL
;
370 /* defer to scripts */
372 if ( *lp_add_user_to_group_script(talloc_tos(), lp_sub
) ) {
373 TALLOC_CTX
*ctx
= talloc_tos();
375 add_script
= talloc_strdup(ctx
,
376 lp_add_user_to_group_script(ctx
, lp_sub
));
380 add_script
= talloc_string_sub(ctx
,
381 add_script
, "%g", unix_group
);
385 add_script
= talloc_string_sub2(ctx
,
386 add_script
, "%u", unix_user
, true, false, true);
390 ret
= smbrun(add_script
, NULL
, NULL
);
391 DEBUG(ret
? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script
,ret
));
393 smb_nscd_flush_group_cache();
401 /****************************************************************************
402 Delete a user from a UNIX group
403 ****************************************************************************/
405 int smb_delete_user_group(const char *unix_group
, const char *unix_user
)
407 const struct loadparm_substitution
*lp_sub
=
408 loadparm_s3_global_substitution();
409 char *del_script
= NULL
;
412 /* defer to scripts */
414 if ( *lp_delete_user_from_group_script(talloc_tos(), lp_sub
) ) {
415 TALLOC_CTX
*ctx
= talloc_tos();
417 del_script
= talloc_strdup(ctx
,
418 lp_delete_user_from_group_script(ctx
, lp_sub
));
422 del_script
= talloc_string_sub(ctx
,
423 del_script
, "%g", unix_group
);
427 del_script
= talloc_string_sub2(ctx
,
428 del_script
, "%u", unix_user
, true, false, true);
432 ret
= smbrun(del_script
, NULL
, NULL
);
433 DEBUG(ret
? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script
,ret
));
435 smb_nscd_flush_group_cache();
444 NTSTATUS
pdb_default_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
447 if (!init_group_mapping()) {
448 DEBUG(0,("failed to initialize group mapping\n"));
449 return NT_STATUS_UNSUCCESSFUL
;
451 return backend
->get_group_map_from_sid(sid
, map
) ?
452 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
455 NTSTATUS
pdb_default_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
458 if (!init_group_mapping()) {
459 DEBUG(0,("failed to initialize group mapping\n"));
460 return NT_STATUS_UNSUCCESSFUL
;
462 return backend
->get_group_map_from_gid(gid
, map
) ?
463 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
466 NTSTATUS
pdb_default_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
469 if (!init_group_mapping()) {
470 DEBUG(0,("failed to initialize group mapping\n"));
471 return NT_STATUS_UNSUCCESSFUL
;
473 return backend
->get_group_map_from_ntname(name
, map
) ?
474 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
477 NTSTATUS
pdb_default_add_group_mapping_entry(struct pdb_methods
*methods
,
480 if (!init_group_mapping()) {
481 DEBUG(0,("failed to initialize group mapping\n"));
482 return NT_STATUS_UNSUCCESSFUL
;
484 return backend
->add_mapping_entry(map
, TDB_INSERT
) ?
485 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
488 NTSTATUS
pdb_default_update_group_mapping_entry(struct pdb_methods
*methods
,
491 if (!init_group_mapping()) {
492 DEBUG(0,("failed to initialize group mapping\n"));
493 return NT_STATUS_UNSUCCESSFUL
;
495 return backend
->add_mapping_entry(map
, TDB_REPLACE
) ?
496 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
499 NTSTATUS
pdb_default_delete_group_mapping_entry(struct pdb_methods
*methods
,
502 if (!init_group_mapping()) {
503 DEBUG(0,("failed to initialize group mapping\n"));
504 return NT_STATUS_UNSUCCESSFUL
;
506 return backend
->group_map_remove(&sid
) ?
507 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
510 NTSTATUS
pdb_default_enum_group_mapping(struct pdb_methods
*methods
,
511 const struct dom_sid
*sid
,
512 enum lsa_SidType sid_name_use
,
513 GROUP_MAP
***pp_rmap
,
514 size_t *p_num_entries
,
517 if (!init_group_mapping()) {
518 DEBUG(0,("failed to initialize group mapping\n"));
519 return NT_STATUS_UNSUCCESSFUL
;
521 return backend
->enum_group_mapping(sid
, sid_name_use
, pp_rmap
, p_num_entries
, unix_only
) ?
522 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
525 NTSTATUS
pdb_default_create_alias(struct pdb_methods
*methods
,
526 const char *name
, uint32_t *rid
)
529 enum lsa_SidType type
;
537 DEBUG(10, ("Trying to create alias %s\n", name
));
539 mem_ctx
= talloc_new(NULL
);
540 if (mem_ctx
== NULL
) {
541 return NT_STATUS_NO_MEMORY
;
544 exists
= lookup_name(mem_ctx
, name
, LOOKUP_NAME_LOCAL
,
545 NULL
, NULL
, &sid
, &type
);
548 status
= NT_STATUS_ALIAS_EXISTS
;
552 if (!pdb_new_rid(&new_rid
)) {
553 DEBUG(0, ("Could not allocate a RID.\n"));
554 status
= NT_STATUS_ACCESS_DENIED
;
558 sid_compose(&sid
, get_global_sam_sid(), new_rid
);
560 if (!winbind_allocate_gid(&gid
)) {
561 DEBUG(3, ("Could not get a gid out of winbind - "
562 "wasted a rid :-(\n"));
563 status
= NT_STATUS_ACCESS_DENIED
;
567 DEBUG(10, ("Creating alias %s with gid %u and rid %u\n",
568 name
, (unsigned int)gid
, (unsigned int)new_rid
));
570 map
= talloc_zero(mem_ctx
, GROUP_MAP
);
572 status
= NT_STATUS_NO_MEMORY
;
577 sid_copy(&map
->sid
, &sid
);
578 map
->sid_name_use
= SID_NAME_ALIAS
;
579 map
->nt_name
= talloc_strdup(map
, name
);
581 status
= NT_STATUS_NO_MEMORY
;
584 map
->comment
= talloc_strdup(map
, "");
586 status
= NT_STATUS_NO_MEMORY
;
590 status
= pdb_add_group_mapping_entry(map
);
592 if (!NT_STATUS_IS_OK(status
)) {
593 DEBUG(0, ("Could not add group mapping entry for alias %s "
594 "(%s)\n", name
, nt_errstr(status
)));
601 TALLOC_FREE(mem_ctx
);
605 NTSTATUS
pdb_default_delete_alias(struct pdb_methods
*methods
,
606 const struct dom_sid
*sid
)
608 return pdb_delete_group_mapping_entry(*sid
);
611 NTSTATUS
pdb_default_get_aliasinfo(struct pdb_methods
*methods
,
612 const struct dom_sid
*sid
,
613 struct acct_info
*info
)
615 NTSTATUS status
= NT_STATUS_OK
;
618 map
= talloc_zero(NULL
, GROUP_MAP
);
620 return NT_STATUS_NO_MEMORY
;
623 if (!pdb_getgrsid(map
, *sid
)) {
624 status
= NT_STATUS_NO_SUCH_ALIAS
;
628 if ((map
->sid_name_use
!= SID_NAME_ALIAS
) &&
629 (map
->sid_name_use
!= SID_NAME_WKN_GRP
)) {
630 struct dom_sid_buf buf
;
631 DEBUG(2, ("%s is a %s, expected an alias\n",
632 dom_sid_str_buf(sid
, &buf
),
633 sid_type_lookup(map
->sid_name_use
)));
634 status
= NT_STATUS_NO_SUCH_ALIAS
;
638 info
->acct_name
= talloc_move(info
, &map
->nt_name
);
639 if (!info
->acct_name
) {
640 status
= NT_STATUS_NO_MEMORY
;
643 info
->acct_desc
= talloc_move(info
, &map
->comment
);
644 if (!info
->acct_desc
) {
645 status
= NT_STATUS_NO_MEMORY
;
648 sid_peek_rid(&map
->sid
, &info
->rid
);
655 NTSTATUS
pdb_default_set_aliasinfo(struct pdb_methods
*methods
,
656 const struct dom_sid
*sid
,
657 struct acct_info
*info
)
662 map
= talloc_zero(NULL
, GROUP_MAP
);
664 return NT_STATUS_NO_MEMORY
;
667 if (!pdb_getgrsid(map
, *sid
)) {
668 status
= NT_STATUS_NO_SUCH_ALIAS
;
672 map
->nt_name
= talloc_strdup(map
, info
->acct_name
);
674 status
= NT_STATUS_NO_MEMORY
;
677 map
->comment
= talloc_strdup(map
, info
->acct_desc
);
679 status
= NT_STATUS_NO_MEMORY
;
683 status
= pdb_update_group_mapping_entry(map
);
690 NTSTATUS
pdb_default_add_aliasmem(struct pdb_methods
*methods
,
691 const struct dom_sid
*alias
, const struct dom_sid
*member
)
693 if (!init_group_mapping()) {
694 DEBUG(0,("failed to initialize group mapping\n"));
695 return NT_STATUS_UNSUCCESSFUL
;
697 return backend
->add_aliasmem(alias
, member
);
700 NTSTATUS
pdb_default_del_aliasmem(struct pdb_methods
*methods
,
701 const struct dom_sid
*alias
, const struct dom_sid
*member
)
703 if (!init_group_mapping()) {
704 DEBUG(0,("failed to initialize group mapping\n"));
705 return NT_STATUS_UNSUCCESSFUL
;
707 return backend
->del_aliasmem(alias
, member
);
710 NTSTATUS
pdb_default_enum_aliasmem(struct pdb_methods
*methods
,
711 const struct dom_sid
*alias
, TALLOC_CTX
*mem_ctx
,
712 struct dom_sid
**pp_members
, size_t *p_num_members
)
714 if (!init_group_mapping()) {
715 DEBUG(0,("failed to initialize group mapping\n"));
716 return NT_STATUS_UNSUCCESSFUL
;
718 return backend
->enum_aliasmem(alias
, mem_ctx
, pp_members
,
722 NTSTATUS
pdb_default_alias_memberships(struct pdb_methods
*methods
,
724 const struct dom_sid
*domain_sid
,
725 const struct dom_sid
*members
,
727 uint32_t **pp_alias_rids
,
728 size_t *p_num_alias_rids
)
730 struct dom_sid
*alias_sids
;
731 size_t i
, num_alias_sids
;
734 if (!init_group_mapping()) {
735 DEBUG(0,("failed to initialize group mapping\n"));
736 return NT_STATUS_UNSUCCESSFUL
;
742 result
= alias_memberships(members
, num_members
,
743 &alias_sids
, &num_alias_sids
);
745 if (!NT_STATUS_IS_OK(result
))
748 *p_num_alias_rids
= 0;
750 if (num_alias_sids
== 0) {
751 TALLOC_FREE(alias_sids
);
755 *pp_alias_rids
= talloc_array(mem_ctx
, uint32_t, num_alias_sids
);
756 if (*pp_alias_rids
== NULL
)
757 return NT_STATUS_NO_MEMORY
;
759 for (i
=0; i
<num_alias_sids
; i
++) {
760 if (!sid_peek_check_rid(domain_sid
, &alias_sids
[i
],
761 &(*pp_alias_rids
)[*p_num_alias_rids
]))
763 *p_num_alias_rids
+= 1;
766 TALLOC_FREE(alias_sids
);
771 /**********************************************************************
772 no ops for passdb backends that don't implement group mapping
773 *********************************************************************/
775 NTSTATUS
pdb_nop_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
778 return NT_STATUS_UNSUCCESSFUL
;
781 NTSTATUS
pdb_nop_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
784 return NT_STATUS_UNSUCCESSFUL
;
787 NTSTATUS
pdb_nop_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
790 return NT_STATUS_UNSUCCESSFUL
;
793 NTSTATUS
pdb_nop_add_group_mapping_entry(struct pdb_methods
*methods
,
796 return NT_STATUS_UNSUCCESSFUL
;
799 NTSTATUS
pdb_nop_update_group_mapping_entry(struct pdb_methods
*methods
,
802 return NT_STATUS_UNSUCCESSFUL
;
805 NTSTATUS
pdb_nop_delete_group_mapping_entry(struct pdb_methods
*methods
,
808 return NT_STATUS_UNSUCCESSFUL
;
811 NTSTATUS
pdb_nop_enum_group_mapping(struct pdb_methods
*methods
,
812 enum lsa_SidType sid_name_use
,
813 GROUP_MAP
**rmap
, size_t *num_entries
,
816 return NT_STATUS_UNSUCCESSFUL
;
820 * @brief Add a new group mapping
822 * @param[in] gid gid to use to store the mapping. If gid is 0,
823 * new gid will be allocated from winbind
825 * @return Normal NTSTATUS return
827 NTSTATUS
pdb_create_builtin_alias(uint32_t rid
, gid_t gid
)
830 enum lsa_SidType type
;
834 const char *name
= NULL
;
836 DEBUG(10, ("Trying to create builtin alias %d\n", rid
));
838 if ( !sid_compose( &sid
, &global_sid_Builtin
, rid
) ) {
839 return NT_STATUS_NO_SUCH_ALIAS
;
842 /* use map as overall temp mem context */
843 map
= talloc_zero(NULL
, GROUP_MAP
);
845 return NT_STATUS_NO_MEMORY
;
848 if (!lookup_sid(map
, &sid
, NULL
, &name
, &type
)) {
849 status
= NT_STATUS_NO_SUCH_ALIAS
;
854 if (!winbind_allocate_gid(&gidformap
)) {
855 DEBUG(3, ("pdb_create_builtin_alias: Could not get a "
856 "gid out of winbind\n"));
857 status
= NT_STATUS_ACCESS_DENIED
;
864 DEBUG(10, ("Creating alias %s with gid %u\n", name
,
865 (unsigned) gidformap
));
867 map
->gid
= gidformap
;
868 sid_copy(&map
->sid
, &sid
);
869 map
->sid_name_use
= SID_NAME_ALIAS
;
870 map
->nt_name
= talloc_strdup(map
, name
);
872 status
= NT_STATUS_NO_MEMORY
;
875 map
->comment
= talloc_strdup(map
, "");
877 status
= NT_STATUS_NO_MEMORY
;
881 status
= pdb_add_group_mapping_entry(map
);
883 if (!NT_STATUS_IS_OK(status
)) {
884 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
885 "(%s)\n", rid
, nt_errstr(status
)));