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"
29 #include "tdb_compat.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 char *add_script
= NULL
;
214 /* defer to scripts */
216 if ( *lp_add_group_script(talloc_tos()) ) {
217 TALLOC_CTX
*ctx
= talloc_tos();
219 add_script
= talloc_strdup(ctx
,
220 lp_add_group_script(ctx
));
224 add_script
= talloc_string_sub(ctx
,
225 add_script
, "%g", unix_group
);
230 ret
= smbrun(add_script
, &fd
);
231 DEBUG(ret
? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script
,ret
));
233 smb_nscd_flush_group_cache();
242 if (read(fd
, output
, sizeof(output
)) > 0) {
243 *new_gid
= (gid_t
)strtoul(output
, NULL
, 10);
252 struct group
*grp
= getgrnam(unix_group
);
255 *new_gid
= grp
->gr_gid
;
261 /****************************************************************************
262 Delete a UNIX group on demand.
263 ****************************************************************************/
265 int smb_delete_group(const char *unix_group
)
267 char *del_script
= NULL
;
270 /* defer to scripts */
272 if ( *lp_delete_group_script(talloc_tos()) ) {
273 TALLOC_CTX
*ctx
= talloc_tos();
275 del_script
= talloc_strdup(ctx
,
276 lp_delete_group_script(ctx
));
280 del_script
= talloc_string_sub(ctx
,
281 del_script
, "%g", unix_group
);
285 ret
= smbrun(del_script
,NULL
);
286 DEBUG(ret
? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script
,ret
));
288 smb_nscd_flush_group_cache();
296 /****************************************************************************
297 Set a user's primary UNIX group.
298 ****************************************************************************/
300 int smb_set_primary_group(const char *unix_group
, const char* unix_user
)
302 char *add_script
= NULL
;
305 /* defer to scripts */
307 if ( *lp_set_primary_group_script(talloc_tos()) ) {
308 TALLOC_CTX
*ctx
= talloc_tos();
310 add_script
= talloc_strdup(ctx
,
311 lp_set_primary_group_script(ctx
));
315 add_script
= talloc_all_string_sub(ctx
,
316 add_script
, "%g", unix_group
);
320 add_script
= talloc_string_sub(ctx
,
321 add_script
, "%u", unix_user
);
325 ret
= smbrun(add_script
,NULL
);
327 DEBUG(ret
? 0 : 3,("smb_set_primary_group: "
328 "Running the command `%s' gave %d\n",add_script
,ret
));
330 smb_nscd_flush_group_cache();
338 /****************************************************************************
339 Add a user to a UNIX group.
340 ****************************************************************************/
342 int smb_add_user_group(const char *unix_group
, const char *unix_user
)
344 char *add_script
= NULL
;
347 /* defer to scripts */
349 if ( *lp_add_user_to_group_script(talloc_tos()) ) {
350 TALLOC_CTX
*ctx
= talloc_tos();
352 add_script
= talloc_strdup(ctx
,
353 lp_add_user_to_group_script(ctx
));
357 add_script
= talloc_string_sub(ctx
,
358 add_script
, "%g", unix_group
);
362 add_script
= talloc_string_sub2(ctx
,
363 add_script
, "%u", unix_user
, true, false, true);
367 ret
= smbrun(add_script
,NULL
);
368 DEBUG(ret
? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script
,ret
));
370 smb_nscd_flush_group_cache();
378 /****************************************************************************
379 Delete a user from a UNIX group
380 ****************************************************************************/
382 int smb_delete_user_group(const char *unix_group
, const char *unix_user
)
384 char *del_script
= NULL
;
387 /* defer to scripts */
389 if ( *lp_delete_user_from_group_script(talloc_tos()) ) {
390 TALLOC_CTX
*ctx
= talloc_tos();
392 del_script
= talloc_strdup(ctx
,
393 lp_delete_user_from_group_script(ctx
));
397 del_script
= talloc_string_sub(ctx
,
398 del_script
, "%g", unix_group
);
402 del_script
= talloc_string_sub2(ctx
,
403 del_script
, "%u", unix_user
, true, false, true);
407 ret
= smbrun(del_script
,NULL
);
408 DEBUG(ret
? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script
,ret
));
410 smb_nscd_flush_group_cache();
419 NTSTATUS
pdb_default_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
422 if (!init_group_mapping()) {
423 DEBUG(0,("failed to initialize group mapping\n"));
424 return NT_STATUS_UNSUCCESSFUL
;
426 return backend
->get_group_map_from_sid(sid
, map
) ?
427 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
430 NTSTATUS
pdb_default_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
433 if (!init_group_mapping()) {
434 DEBUG(0,("failed to initialize group mapping\n"));
435 return NT_STATUS_UNSUCCESSFUL
;
437 return backend
->get_group_map_from_gid(gid
, map
) ?
438 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
441 NTSTATUS
pdb_default_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
444 if (!init_group_mapping()) {
445 DEBUG(0,("failed to initialize group mapping\n"));
446 return NT_STATUS_UNSUCCESSFUL
;
448 return backend
->get_group_map_from_ntname(name
, map
) ?
449 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
452 NTSTATUS
pdb_default_add_group_mapping_entry(struct pdb_methods
*methods
,
455 if (!init_group_mapping()) {
456 DEBUG(0,("failed to initialize group mapping\n"));
457 return NT_STATUS_UNSUCCESSFUL
;
459 return backend
->add_mapping_entry(map
, TDB_INSERT
) ?
460 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
463 NTSTATUS
pdb_default_update_group_mapping_entry(struct pdb_methods
*methods
,
466 if (!init_group_mapping()) {
467 DEBUG(0,("failed to initialize group mapping\n"));
468 return NT_STATUS_UNSUCCESSFUL
;
470 return backend
->add_mapping_entry(map
, TDB_REPLACE
) ?
471 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
474 NTSTATUS
pdb_default_delete_group_mapping_entry(struct pdb_methods
*methods
,
477 if (!init_group_mapping()) {
478 DEBUG(0,("failed to initialize group mapping\n"));
479 return NT_STATUS_UNSUCCESSFUL
;
481 return backend
->group_map_remove(&sid
) ?
482 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
485 NTSTATUS
pdb_default_enum_group_mapping(struct pdb_methods
*methods
,
486 const struct dom_sid
*sid
,
487 enum lsa_SidType sid_name_use
,
488 GROUP_MAP
***pp_rmap
,
489 size_t *p_num_entries
,
492 if (!init_group_mapping()) {
493 DEBUG(0,("failed to initialize group mapping\n"));
494 return NT_STATUS_UNSUCCESSFUL
;
496 return backend
->enum_group_mapping(sid
, sid_name_use
, pp_rmap
, p_num_entries
, unix_only
) ?
497 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
500 NTSTATUS
pdb_default_create_alias(struct pdb_methods
*methods
,
501 const char *name
, uint32
*rid
)
504 enum lsa_SidType type
;
512 DEBUG(10, ("Trying to create alias %s\n", name
));
514 mem_ctx
= talloc_new(NULL
);
515 if (mem_ctx
== NULL
) {
516 return NT_STATUS_NO_MEMORY
;
519 exists
= lookup_name(mem_ctx
, name
, LOOKUP_NAME_LOCAL
,
520 NULL
, NULL
, &sid
, &type
);
523 status
= NT_STATUS_ALIAS_EXISTS
;
527 if (!pdb_new_rid(&new_rid
)) {
528 DEBUG(0, ("Could not allocate a RID.\n"));
529 status
= NT_STATUS_ACCESS_DENIED
;
533 sid_compose(&sid
, get_global_sam_sid(), new_rid
);
535 if (!winbind_allocate_gid(&gid
)) {
536 DEBUG(3, ("Could not get a gid out of winbind - "
537 "wasted a rid :-(\n"));
538 status
= NT_STATUS_ACCESS_DENIED
;
542 DEBUG(10, ("Creating alias %s with gid %u and rid %u\n",
543 name
, (unsigned int)gid
, (unsigned int)new_rid
));
545 map
= talloc_zero(mem_ctx
, GROUP_MAP
);
547 status
= NT_STATUS_NO_MEMORY
;
552 sid_copy(&map
->sid
, &sid
);
553 map
->sid_name_use
= SID_NAME_ALIAS
;
554 map
->nt_name
= talloc_strdup(map
, name
);
556 status
= NT_STATUS_NO_MEMORY
;
559 map
->comment
= talloc_strdup(map
, "");
561 status
= NT_STATUS_NO_MEMORY
;
565 status
= pdb_add_group_mapping_entry(map
);
567 if (!NT_STATUS_IS_OK(status
)) {
568 DEBUG(0, ("Could not add group mapping entry for alias %s "
569 "(%s)\n", name
, nt_errstr(status
)));
576 TALLOC_FREE(mem_ctx
);
580 NTSTATUS
pdb_default_delete_alias(struct pdb_methods
*methods
,
581 const struct dom_sid
*sid
)
583 return pdb_delete_group_mapping_entry(*sid
);
586 NTSTATUS
pdb_default_get_aliasinfo(struct pdb_methods
*methods
,
587 const struct dom_sid
*sid
,
588 struct acct_info
*info
)
590 NTSTATUS status
= NT_STATUS_OK
;
593 map
= talloc_zero(NULL
, GROUP_MAP
);
595 return NT_STATUS_NO_MEMORY
;
598 if (!pdb_getgrsid(map
, *sid
)) {
599 status
= NT_STATUS_NO_SUCH_ALIAS
;
603 if ((map
->sid_name_use
!= SID_NAME_ALIAS
) &&
604 (map
->sid_name_use
!= SID_NAME_WKN_GRP
)) {
605 DEBUG(2, ("%s is a %s, expected an alias\n",
607 sid_type_lookup(map
->sid_name_use
)));
608 status
= NT_STATUS_NO_SUCH_ALIAS
;
612 info
->acct_name
= talloc_move(info
, &map
->nt_name
);
613 if (!info
->acct_name
) {
614 status
= NT_STATUS_NO_MEMORY
;
617 info
->acct_desc
= talloc_move(info
, &map
->comment
);
618 if (!info
->acct_desc
) {
619 status
= NT_STATUS_NO_MEMORY
;
622 sid_peek_rid(&map
->sid
, &info
->rid
);
629 NTSTATUS
pdb_default_set_aliasinfo(struct pdb_methods
*methods
,
630 const struct dom_sid
*sid
,
631 struct acct_info
*info
)
633 NTSTATUS status
= NT_STATUS_OK
;
636 map
= talloc_zero(NULL
, GROUP_MAP
);
638 return NT_STATUS_NO_MEMORY
;
641 if (!pdb_getgrsid(map
, *sid
)) {
642 status
= NT_STATUS_NO_SUCH_ALIAS
;
646 map
->nt_name
= talloc_strdup(map
, info
->acct_name
);
648 status
= NT_STATUS_NO_MEMORY
;
651 map
->comment
= talloc_strdup(map
, info
->acct_desc
);
653 status
= NT_STATUS_NO_MEMORY
;
657 status
= pdb_update_group_mapping_entry(map
);
664 NTSTATUS
pdb_default_add_aliasmem(struct pdb_methods
*methods
,
665 const struct dom_sid
*alias
, const struct dom_sid
*member
)
667 if (!init_group_mapping()) {
668 DEBUG(0,("failed to initialize group mapping\n"));
669 return NT_STATUS_UNSUCCESSFUL
;
671 return backend
->add_aliasmem(alias
, member
);
674 NTSTATUS
pdb_default_del_aliasmem(struct pdb_methods
*methods
,
675 const struct dom_sid
*alias
, const struct dom_sid
*member
)
677 if (!init_group_mapping()) {
678 DEBUG(0,("failed to initialize group mapping\n"));
679 return NT_STATUS_UNSUCCESSFUL
;
681 return backend
->del_aliasmem(alias
, member
);
684 NTSTATUS
pdb_default_enum_aliasmem(struct pdb_methods
*methods
,
685 const struct dom_sid
*alias
, TALLOC_CTX
*mem_ctx
,
686 struct dom_sid
**pp_members
, size_t *p_num_members
)
688 if (!init_group_mapping()) {
689 DEBUG(0,("failed to initialize group mapping\n"));
690 return NT_STATUS_UNSUCCESSFUL
;
692 return backend
->enum_aliasmem(alias
, mem_ctx
, pp_members
,
696 NTSTATUS
pdb_default_alias_memberships(struct pdb_methods
*methods
,
698 const struct dom_sid
*domain_sid
,
699 const struct dom_sid
*members
,
701 uint32
**pp_alias_rids
,
702 size_t *p_num_alias_rids
)
704 struct dom_sid
*alias_sids
;
705 size_t i
, num_alias_sids
;
708 if (!init_group_mapping()) {
709 DEBUG(0,("failed to initialize group mapping\n"));
710 return NT_STATUS_UNSUCCESSFUL
;
716 result
= alias_memberships(members
, num_members
,
717 &alias_sids
, &num_alias_sids
);
719 if (!NT_STATUS_IS_OK(result
))
722 *p_num_alias_rids
= 0;
724 if (num_alias_sids
== 0) {
725 TALLOC_FREE(alias_sids
);
729 *pp_alias_rids
= talloc_array(mem_ctx
, uint32
, num_alias_sids
);
730 if (*pp_alias_rids
== NULL
)
731 return NT_STATUS_NO_MEMORY
;
733 for (i
=0; i
<num_alias_sids
; i
++) {
734 if (!sid_peek_check_rid(domain_sid
, &alias_sids
[i
],
735 &(*pp_alias_rids
)[*p_num_alias_rids
]))
737 *p_num_alias_rids
+= 1;
740 TALLOC_FREE(alias_sids
);
745 /**********************************************************************
746 no ops for passdb backends that don't implement group mapping
747 *********************************************************************/
749 NTSTATUS
pdb_nop_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
752 return NT_STATUS_UNSUCCESSFUL
;
755 NTSTATUS
pdb_nop_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
758 return NT_STATUS_UNSUCCESSFUL
;
761 NTSTATUS
pdb_nop_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
764 return NT_STATUS_UNSUCCESSFUL
;
767 NTSTATUS
pdb_nop_add_group_mapping_entry(struct pdb_methods
*methods
,
770 return NT_STATUS_UNSUCCESSFUL
;
773 NTSTATUS
pdb_nop_update_group_mapping_entry(struct pdb_methods
*methods
,
776 return NT_STATUS_UNSUCCESSFUL
;
779 NTSTATUS
pdb_nop_delete_group_mapping_entry(struct pdb_methods
*methods
,
782 return NT_STATUS_UNSUCCESSFUL
;
785 NTSTATUS
pdb_nop_enum_group_mapping(struct pdb_methods
*methods
,
786 enum lsa_SidType sid_name_use
,
787 GROUP_MAP
**rmap
, size_t *num_entries
,
790 return NT_STATUS_UNSUCCESSFUL
;
794 * @brief Add a new group mapping
796 * @param[in] gid gid to use to store the mapping. If gid is 0,
797 * new gid will be allocated from winbind
799 * @return Normal NTSTATUS return
801 NTSTATUS
pdb_create_builtin_alias(uint32 rid
, gid_t gid
)
804 enum lsa_SidType type
;
808 const char *name
= NULL
;
810 DEBUG(10, ("Trying to create builtin alias %d\n", rid
));
812 if ( !sid_compose( &sid
, &global_sid_Builtin
, rid
) ) {
813 return NT_STATUS_NO_SUCH_ALIAS
;
816 /* use map as overall temp mem context */
817 map
= talloc_zero(NULL
, GROUP_MAP
);
819 return NT_STATUS_NO_MEMORY
;
822 if (!lookup_sid(map
, &sid
, NULL
, &name
, &type
)) {
823 status
= NT_STATUS_NO_SUCH_ALIAS
;
828 if (!winbind_allocate_gid(&gidformap
)) {
829 DEBUG(3, ("pdb_create_builtin_alias: Could not get a "
830 "gid out of winbind\n"));
831 status
= NT_STATUS_ACCESS_DENIED
;
838 DEBUG(10, ("Creating alias %s with gid %u\n", name
,
839 (unsigned) gidformap
));
841 map
->gid
= gidformap
;
842 sid_copy(&map
->sid
, &sid
);
843 map
->sid_name_use
= SID_NAME_ALIAS
;
844 map
->nt_name
= talloc_strdup(map
, name
);
846 status
= NT_STATUS_NO_MEMORY
;
849 map
->comment
= talloc_strdup(map
, "");
851 status
= NT_STATUS_NO_MEMORY
;
855 status
= pdb_add_group_mapping_entry(map
);
857 if (!NT_STATUS_IS_OK(status
)) {
858 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
859 "(%s)\n", rid
, nt_errstr(status
)));