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 static const struct mapping_backend
*backend
;
33 initialise a group mapping backend
35 static bool init_group_mapping(void)
37 if (backend
!= NULL
) {
38 /* already initialised */
42 backend
= groupdb_tdb_init();
44 return backend
!= NULL
;
47 /****************************************************************************
48 initialise first time the mapping list
49 ****************************************************************************/
50 NTSTATUS
add_initial_entry(gid_t gid
, const char *sid
, enum lsa_SidType sid_name_use
, const char *nt_name
, const char *comment
)
54 if(!init_group_mapping()) {
55 DEBUG(0,("failed to initialize group mapping\n"));
56 return NT_STATUS_UNSUCCESSFUL
;
60 if (!string_to_sid(&map
.sid
, sid
)) {
61 DEBUG(0, ("string_to_sid failed: %s", sid
));
62 return NT_STATUS_UNSUCCESSFUL
;
65 map
.sid_name_use
=sid_name_use
;
66 fstrcpy(map
.nt_name
, nt_name
);
67 fstrcpy(map
.comment
, comment
);
69 return pdb_add_group_mapping_entry(&map
);
72 static NTSTATUS
alias_memberships(const struct dom_sid
*members
, size_t num_members
,
73 struct dom_sid
**sids
, size_t *num
)
80 for (i
=0; i
<num_members
; i
++) {
81 NTSTATUS status
= backend
->one_alias_membership(&members
[i
], sids
, num
);
82 if (!NT_STATUS_IS_OK(status
))
88 struct aliasmem_closure
{
89 const struct dom_sid
*alias
;
90 struct dom_sid
**sids
;
98 * High level functions
99 * better to use them than the lower ones.
101 * we are checking if the group is in the mapping file
102 * and if the group is an existing unix group
106 /* get a domain group from it's SID */
108 bool get_domain_group_from_sid(struct dom_sid sid
, GROUP_MAP
*map
)
113 if(!init_group_mapping()) {
114 DEBUG(0,("failed to initialize group mapping\n"));
118 DEBUG(10, ("get_domain_group_from_sid\n"));
120 /* if the group is NOT in the database, it CAN NOT be a domain group */
123 ret
= pdb_getgrsid(map
, sid
);
126 /* special case check for rid 513 */
131 sid_peek_rid( &sid
, &rid
);
133 if ( rid
== DOMAIN_RID_USERS
) {
134 fstrcpy( map
->nt_name
, "None" );
135 fstrcpy( map
->comment
, "Ordinary Users" );
136 sid_copy( &map
->sid
, &sid
);
137 map
->sid_name_use
= SID_NAME_DOM_GRP
;
138 map
->gid
= (gid_t
)-1;
144 DEBUG(10, ("get_domain_group_from_sid: SID found in passdb\n"));
146 /* if it's not a domain group, continue */
147 if (map
->sid_name_use
!=SID_NAME_DOM_GRP
) {
151 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
157 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map
->gid
));
159 grp
= getgrgid(map
->gid
);
161 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
165 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
170 /****************************************************************************
171 Create a UNIX group on demand.
172 ****************************************************************************/
174 int smb_create_group(const char *unix_group
, gid_t
*new_gid
)
176 char *add_script
= NULL
;
182 /* defer to scripts */
184 if ( *lp_addgroup_script() ) {
185 TALLOC_CTX
*ctx
= talloc_tos();
187 add_script
= talloc_strdup(ctx
,
188 lp_addgroup_script());
192 add_script
= talloc_string_sub(ctx
,
193 add_script
, "%g", unix_group
);
198 ret
= smbrun(add_script
, &fd
);
199 DEBUG(ret
? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script
,ret
));
201 smb_nscd_flush_group_cache();
210 if (read(fd
, output
, sizeof(output
)) > 0) {
211 *new_gid
= (gid_t
)strtoul(output
, NULL
, 10);
220 struct group
*grp
= getgrnam(unix_group
);
223 *new_gid
= grp
->gr_gid
;
229 /****************************************************************************
230 Delete a UNIX group on demand.
231 ****************************************************************************/
233 int smb_delete_group(const char *unix_group
)
235 char *del_script
= NULL
;
238 /* defer to scripts */
240 if ( *lp_delgroup_script() ) {
241 TALLOC_CTX
*ctx
= talloc_tos();
243 del_script
= talloc_strdup(ctx
,
244 lp_delgroup_script());
248 del_script
= talloc_string_sub(ctx
,
249 del_script
, "%g", unix_group
);
253 ret
= smbrun(del_script
,NULL
);
254 DEBUG(ret
? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script
,ret
));
256 smb_nscd_flush_group_cache();
264 /****************************************************************************
265 Set a user's primary UNIX group.
266 ****************************************************************************/
268 int smb_set_primary_group(const char *unix_group
, const char* unix_user
)
270 char *add_script
= NULL
;
273 /* defer to scripts */
275 if ( *lp_setprimarygroup_script() ) {
276 TALLOC_CTX
*ctx
= talloc_tos();
278 add_script
= talloc_strdup(ctx
,
279 lp_setprimarygroup_script());
283 add_script
= talloc_all_string_sub(ctx
,
284 add_script
, "%g", unix_group
);
288 add_script
= talloc_string_sub(ctx
,
289 add_script
, "%u", unix_user
);
293 ret
= smbrun(add_script
,NULL
);
295 DEBUG(ret
? 0 : 3,("smb_set_primary_group: "
296 "Running the command `%s' gave %d\n",add_script
,ret
));
298 smb_nscd_flush_group_cache();
306 /****************************************************************************
307 Add a user to a UNIX group.
308 ****************************************************************************/
310 int smb_add_user_group(const char *unix_group
, const char *unix_user
)
312 char *add_script
= NULL
;
315 /* defer to scripts */
317 if ( *lp_addusertogroup_script() ) {
318 TALLOC_CTX
*ctx
= talloc_tos();
320 add_script
= talloc_strdup(ctx
,
321 lp_addusertogroup_script());
325 add_script
= talloc_string_sub(ctx
,
326 add_script
, "%g", unix_group
);
330 add_script
= talloc_string_sub2(ctx
,
331 add_script
, "%u", unix_user
, true, false, true);
335 ret
= smbrun(add_script
,NULL
);
336 DEBUG(ret
? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script
,ret
));
338 smb_nscd_flush_group_cache();
346 /****************************************************************************
347 Delete a user from a UNIX group
348 ****************************************************************************/
350 int smb_delete_user_group(const char *unix_group
, const char *unix_user
)
352 char *del_script
= NULL
;
355 /* defer to scripts */
357 if ( *lp_deluserfromgroup_script() ) {
358 TALLOC_CTX
*ctx
= talloc_tos();
360 del_script
= talloc_strdup(ctx
,
361 lp_deluserfromgroup_script());
365 del_script
= talloc_string_sub(ctx
,
366 del_script
, "%g", unix_group
);
370 del_script
= talloc_string_sub2(ctx
,
371 del_script
, "%u", unix_user
, true, false, true);
375 ret
= smbrun(del_script
,NULL
);
376 DEBUG(ret
? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script
,ret
));
378 smb_nscd_flush_group_cache();
387 NTSTATUS
pdb_default_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
390 if (!init_group_mapping()) {
391 DEBUG(0,("failed to initialize group mapping\n"));
392 return NT_STATUS_UNSUCCESSFUL
;
394 return backend
->get_group_map_from_sid(sid
, map
) ?
395 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
398 NTSTATUS
pdb_default_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
401 if (!init_group_mapping()) {
402 DEBUG(0,("failed to initialize group mapping\n"));
403 return NT_STATUS_UNSUCCESSFUL
;
405 return backend
->get_group_map_from_gid(gid
, map
) ?
406 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
409 NTSTATUS
pdb_default_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
412 if (!init_group_mapping()) {
413 DEBUG(0,("failed to initialize group mapping\n"));
414 return NT_STATUS_UNSUCCESSFUL
;
416 return backend
->get_group_map_from_ntname(name
, map
) ?
417 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
420 NTSTATUS
pdb_default_add_group_mapping_entry(struct pdb_methods
*methods
,
423 if (!init_group_mapping()) {
424 DEBUG(0,("failed to initialize group mapping\n"));
425 return NT_STATUS_UNSUCCESSFUL
;
427 return backend
->add_mapping_entry(map
, TDB_INSERT
) ?
428 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
431 NTSTATUS
pdb_default_update_group_mapping_entry(struct pdb_methods
*methods
,
434 if (!init_group_mapping()) {
435 DEBUG(0,("failed to initialize group mapping\n"));
436 return NT_STATUS_UNSUCCESSFUL
;
438 return backend
->add_mapping_entry(map
, TDB_REPLACE
) ?
439 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
442 NTSTATUS
pdb_default_delete_group_mapping_entry(struct pdb_methods
*methods
,
445 if (!init_group_mapping()) {
446 DEBUG(0,("failed to initialize group mapping\n"));
447 return NT_STATUS_UNSUCCESSFUL
;
449 return backend
->group_map_remove(&sid
) ?
450 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
453 NTSTATUS
pdb_default_enum_group_mapping(struct pdb_methods
*methods
,
454 const struct dom_sid
*sid
, enum lsa_SidType sid_name_use
,
455 GROUP_MAP
**pp_rmap
, size_t *p_num_entries
,
458 if (!init_group_mapping()) {
459 DEBUG(0,("failed to initialize group mapping\n"));
460 return NT_STATUS_UNSUCCESSFUL
;
462 return backend
->enum_group_mapping(sid
, sid_name_use
, pp_rmap
, p_num_entries
, unix_only
) ?
463 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
466 NTSTATUS
pdb_default_create_alias(struct pdb_methods
*methods
,
467 const char *name
, uint32
*rid
)
470 enum lsa_SidType type
;
478 DEBUG(10, ("Trying to create alias %s\n", name
));
480 mem_ctx
= talloc_new(NULL
);
481 if (mem_ctx
== NULL
) {
482 return NT_STATUS_NO_MEMORY
;
485 exists
= lookup_name(mem_ctx
, name
, LOOKUP_NAME_LOCAL
,
486 NULL
, NULL
, &sid
, &type
);
487 TALLOC_FREE(mem_ctx
);
490 return NT_STATUS_ALIAS_EXISTS
;
493 if (!pdb_new_rid(&new_rid
)) {
494 DEBUG(0, ("Could not allocate a RID.\n"));
495 return NT_STATUS_ACCESS_DENIED
;
498 sid_compose(&sid
, get_global_sam_sid(), new_rid
);
500 if (!winbind_allocate_gid(&gid
)) {
501 DEBUG(3, ("Could not get a gid out of winbind - "
502 "wasted a rid :-(\n"));
503 return NT_STATUS_ACCESS_DENIED
;
506 DEBUG(10, ("Creating alias %s with gid %u and rid %u\n",
507 name
, (unsigned int)gid
, (unsigned int)new_rid
));
510 sid_copy(&map
.sid
, &sid
);
511 map
.sid_name_use
= SID_NAME_ALIAS
;
512 fstrcpy(map
.nt_name
, name
);
513 fstrcpy(map
.comment
, "");
515 status
= pdb_add_group_mapping_entry(&map
);
517 if (!NT_STATUS_IS_OK(status
)) {
518 DEBUG(0, ("Could not add group mapping entry for alias %s "
519 "(%s)\n", name
, nt_errstr(status
)));
528 NTSTATUS
pdb_default_delete_alias(struct pdb_methods
*methods
,
529 const struct dom_sid
*sid
)
531 return pdb_delete_group_mapping_entry(*sid
);
534 NTSTATUS
pdb_default_get_aliasinfo(struct pdb_methods
*methods
,
535 const struct dom_sid
*sid
,
536 struct acct_info
*info
)
540 if (!pdb_getgrsid(&map
, *sid
))
541 return NT_STATUS_NO_SUCH_ALIAS
;
543 if ((map
.sid_name_use
!= SID_NAME_ALIAS
) &&
544 (map
.sid_name_use
!= SID_NAME_WKN_GRP
)) {
545 DEBUG(2, ("%s is a %s, expected an alias\n",
547 sid_type_lookup(map
.sid_name_use
)));
548 return NT_STATUS_NO_SUCH_ALIAS
;
551 fstrcpy(info
->acct_name
, map
.nt_name
);
552 fstrcpy(info
->acct_desc
, map
.comment
);
553 sid_peek_rid(&map
.sid
, &info
->rid
);
557 NTSTATUS
pdb_default_set_aliasinfo(struct pdb_methods
*methods
,
558 const struct dom_sid
*sid
,
559 struct acct_info
*info
)
563 if (!pdb_getgrsid(&map
, *sid
))
564 return NT_STATUS_NO_SUCH_ALIAS
;
566 fstrcpy(map
.nt_name
, info
->acct_name
);
567 fstrcpy(map
.comment
, info
->acct_desc
);
569 return pdb_update_group_mapping_entry(&map
);
572 NTSTATUS
pdb_default_add_aliasmem(struct pdb_methods
*methods
,
573 const struct dom_sid
*alias
, const struct dom_sid
*member
)
575 if (!init_group_mapping()) {
576 DEBUG(0,("failed to initialize group mapping\n"));
577 return NT_STATUS_UNSUCCESSFUL
;
579 return backend
->add_aliasmem(alias
, member
);
582 NTSTATUS
pdb_default_del_aliasmem(struct pdb_methods
*methods
,
583 const struct dom_sid
*alias
, const struct dom_sid
*member
)
585 if (!init_group_mapping()) {
586 DEBUG(0,("failed to initialize group mapping\n"));
587 return NT_STATUS_UNSUCCESSFUL
;
589 return backend
->del_aliasmem(alias
, member
);
592 NTSTATUS
pdb_default_enum_aliasmem(struct pdb_methods
*methods
,
593 const struct dom_sid
*alias
, TALLOC_CTX
*mem_ctx
,
594 struct dom_sid
**pp_members
, size_t *p_num_members
)
596 if (!init_group_mapping()) {
597 DEBUG(0,("failed to initialize group mapping\n"));
598 return NT_STATUS_UNSUCCESSFUL
;
600 return backend
->enum_aliasmem(alias
, mem_ctx
, pp_members
,
604 NTSTATUS
pdb_default_alias_memberships(struct pdb_methods
*methods
,
606 const struct dom_sid
*domain_sid
,
607 const struct dom_sid
*members
,
609 uint32
**pp_alias_rids
,
610 size_t *p_num_alias_rids
)
612 struct dom_sid
*alias_sids
;
613 size_t i
, num_alias_sids
;
616 if (!init_group_mapping()) {
617 DEBUG(0,("failed to initialize group mapping\n"));
618 return NT_STATUS_UNSUCCESSFUL
;
624 result
= alias_memberships(members
, num_members
,
625 &alias_sids
, &num_alias_sids
);
627 if (!NT_STATUS_IS_OK(result
))
630 *p_num_alias_rids
= 0;
632 if (num_alias_sids
== 0) {
633 TALLOC_FREE(alias_sids
);
637 *pp_alias_rids
= TALLOC_ARRAY(mem_ctx
, uint32
, num_alias_sids
);
638 if (*pp_alias_rids
== NULL
)
639 return NT_STATUS_NO_MEMORY
;
641 for (i
=0; i
<num_alias_sids
; i
++) {
642 if (!sid_peek_check_rid(domain_sid
, &alias_sids
[i
],
643 &(*pp_alias_rids
)[*p_num_alias_rids
]))
645 *p_num_alias_rids
+= 1;
648 TALLOC_FREE(alias_sids
);
653 /**********************************************************************
654 no ops for passdb backends that don't implement group mapping
655 *********************************************************************/
657 NTSTATUS
pdb_nop_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
660 return NT_STATUS_UNSUCCESSFUL
;
663 NTSTATUS
pdb_nop_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
666 return NT_STATUS_UNSUCCESSFUL
;
669 NTSTATUS
pdb_nop_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
672 return NT_STATUS_UNSUCCESSFUL
;
675 NTSTATUS
pdb_nop_add_group_mapping_entry(struct pdb_methods
*methods
,
678 return NT_STATUS_UNSUCCESSFUL
;
681 NTSTATUS
pdb_nop_update_group_mapping_entry(struct pdb_methods
*methods
,
684 return NT_STATUS_UNSUCCESSFUL
;
687 NTSTATUS
pdb_nop_delete_group_mapping_entry(struct pdb_methods
*methods
,
690 return NT_STATUS_UNSUCCESSFUL
;
693 NTSTATUS
pdb_nop_enum_group_mapping(struct pdb_methods
*methods
,
694 enum lsa_SidType sid_name_use
,
695 GROUP_MAP
**rmap
, size_t *num_entries
,
698 return NT_STATUS_UNSUCCESSFUL
;
701 /****************************************************************************
702 These need to be redirected through pdb_interface.c
703 ****************************************************************************/
704 bool pdb_get_dom_grp_info(const struct dom_sid
*sid
, struct acct_info
*info
)
710 res
= get_domain_group_from_sid(*sid
, &map
);
716 fstrcpy(info
->acct_name
, map
.nt_name
);
717 fstrcpy(info
->acct_desc
, map
.comment
);
718 sid_peek_rid(sid
, &info
->rid
);
722 bool pdb_set_dom_grp_info(const struct dom_sid
*sid
, const struct acct_info
*info
)
726 if (!get_domain_group_from_sid(*sid
, &map
))
729 fstrcpy(map
.nt_name
, info
->acct_name
);
730 fstrcpy(map
.comment
, info
->acct_desc
);
732 return NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map
));
735 /********************************************************************
736 Really just intended to be called by smbd
737 ********************************************************************/
739 NTSTATUS
pdb_create_builtin_alias(uint32 rid
)
742 enum lsa_SidType type
;
747 const char *name
= NULL
;
750 DEBUG(10, ("Trying to create builtin alias %d\n", rid
));
752 if ( !sid_compose( &sid
, &global_sid_Builtin
, rid
) ) {
753 return NT_STATUS_NO_SUCH_ALIAS
;
756 if ( (mem_ctx
= talloc_new(NULL
)) == NULL
) {
757 return NT_STATUS_NO_MEMORY
;
760 if ( !lookup_sid(mem_ctx
, &sid
, NULL
, &name
, &type
) ) {
761 TALLOC_FREE( mem_ctx
);
762 return NT_STATUS_NO_SUCH_ALIAS
;
765 /* validate RID so copy the name and move on */
767 fstrcpy( groupname
, name
);
768 TALLOC_FREE( mem_ctx
);
770 if (!winbind_allocate_gid(&gid
)) {
771 DEBUG(3, ("pdb_create_builtin_alias: Could not get a gid out of winbind\n"));
772 return NT_STATUS_ACCESS_DENIED
;
775 DEBUG(10,("Creating alias %s with gid %u\n", groupname
, (unsigned int)gid
));
778 sid_copy(&map
.sid
, &sid
);
779 map
.sid_name_use
= SID_NAME_ALIAS
;
780 fstrcpy(map
.nt_name
, groupname
);
781 fstrcpy(map
.comment
, "");
783 status
= pdb_add_group_mapping_entry(&map
);
785 if (!NT_STATUS_IS_OK(status
)) {
786 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
787 "(%s)\n", rid
, nt_errstr(status
)));