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 "groupdb/mapping.h"
26 static const struct mapping_backend
*backend
;
29 initialise a group mapping backend
31 static bool init_group_mapping(void)
33 if (backend
!= NULL
) {
34 /* already initialised */
38 backend
= groupdb_tdb_init();
40 return backend
!= NULL
;
43 /****************************************************************************
44 initialise first time the mapping list
45 ****************************************************************************/
46 NTSTATUS
add_initial_entry(gid_t gid
, const char *sid
, enum lsa_SidType sid_name_use
, const char *nt_name
, const char *comment
)
50 if(!init_group_mapping()) {
51 DEBUG(0,("failed to initialize group mapping\n"));
52 return NT_STATUS_UNSUCCESSFUL
;
56 if (!string_to_sid(&map
.sid
, sid
)) {
57 DEBUG(0, ("string_to_sid failed: %s", sid
));
58 return NT_STATUS_UNSUCCESSFUL
;
61 map
.sid_name_use
=sid_name_use
;
62 fstrcpy(map
.nt_name
, nt_name
);
63 fstrcpy(map
.comment
, comment
);
65 return pdb_add_group_mapping_entry(&map
);
68 static NTSTATUS
alias_memberships(const struct dom_sid
*members
, size_t num_members
,
69 struct dom_sid
**sids
, size_t *num
)
76 for (i
=0; i
<num_members
; i
++) {
77 NTSTATUS status
= backend
->one_alias_membership(&members
[i
], sids
, num
);
78 if (!NT_STATUS_IS_OK(status
))
84 struct aliasmem_closure
{
85 const struct dom_sid
*alias
;
86 struct dom_sid
**sids
;
94 * High level functions
95 * better to use them than the lower ones.
97 * we are checking if the group is in the mapping file
98 * and if the group is an existing unix group
102 /* get a domain group from it's SID */
104 bool get_domain_group_from_sid(struct dom_sid sid
, GROUP_MAP
*map
)
109 if(!init_group_mapping()) {
110 DEBUG(0,("failed to initialize group mapping\n"));
114 DEBUG(10, ("get_domain_group_from_sid\n"));
116 /* if the group is NOT in the database, it CAN NOT be a domain group */
119 ret
= pdb_getgrsid(map
, sid
);
122 /* special case check for rid 513 */
127 sid_peek_rid( &sid
, &rid
);
129 if ( rid
== DOMAIN_RID_USERS
) {
130 fstrcpy( map
->nt_name
, "None" );
131 fstrcpy( map
->comment
, "Ordinary Users" );
132 sid_copy( &map
->sid
, &sid
);
133 map
->sid_name_use
= SID_NAME_DOM_GRP
;
134 map
->gid
= (gid_t
)-1;
140 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
142 /* if it's not a domain group, continue */
143 if (map
->sid_name_use
!=SID_NAME_DOM_GRP
) {
147 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
153 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map
->gid
));
155 grp
= getgrgid(map
->gid
);
157 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
161 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
166 /****************************************************************************
167 Create a UNIX group on demand.
168 ****************************************************************************/
170 int smb_create_group(const char *unix_group
, gid_t
*new_gid
)
172 char *add_script
= NULL
;
178 /* defer to scripts */
180 if ( *lp_addgroup_script() ) {
181 TALLOC_CTX
*ctx
= talloc_tos();
183 add_script
= talloc_strdup(ctx
,
184 lp_addgroup_script());
188 add_script
= talloc_string_sub(ctx
,
189 add_script
, "%g", unix_group
);
194 ret
= smbrun(add_script
, &fd
);
195 DEBUG(ret
? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script
,ret
));
197 smb_nscd_flush_group_cache();
206 if (read(fd
, output
, sizeof(output
)) > 0) {
207 *new_gid
= (gid_t
)strtoul(output
, NULL
, 10);
216 struct group
*grp
= getgrnam(unix_group
);
219 *new_gid
= grp
->gr_gid
;
225 /****************************************************************************
226 Delete a UNIX group on demand.
227 ****************************************************************************/
229 int smb_delete_group(const char *unix_group
)
231 char *del_script
= NULL
;
234 /* defer to scripts */
236 if ( *lp_delgroup_script() ) {
237 TALLOC_CTX
*ctx
= talloc_tos();
239 del_script
= talloc_strdup(ctx
,
240 lp_delgroup_script());
244 del_script
= talloc_string_sub(ctx
,
245 del_script
, "%g", unix_group
);
249 ret
= smbrun(del_script
,NULL
);
250 DEBUG(ret
? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script
,ret
));
252 smb_nscd_flush_group_cache();
260 /****************************************************************************
261 Set a user's primary UNIX group.
262 ****************************************************************************/
264 int smb_set_primary_group(const char *unix_group
, const char* unix_user
)
266 char *add_script
= NULL
;
269 /* defer to scripts */
271 if ( *lp_setprimarygroup_script() ) {
272 TALLOC_CTX
*ctx
= talloc_tos();
274 add_script
= talloc_strdup(ctx
,
275 lp_setprimarygroup_script());
279 add_script
= talloc_all_string_sub(ctx
,
280 add_script
, "%g", unix_group
);
284 add_script
= talloc_string_sub(ctx
,
285 add_script
, "%u", unix_user
);
289 ret
= smbrun(add_script
,NULL
);
291 DEBUG(ret
? 0 : 3,("smb_set_primary_group: "
292 "Running the command `%s' gave %d\n",add_script
,ret
));
294 smb_nscd_flush_group_cache();
302 /****************************************************************************
303 Add a user to a UNIX group.
304 ****************************************************************************/
306 int smb_add_user_group(const char *unix_group
, const char *unix_user
)
308 char *add_script
= NULL
;
311 /* defer to scripts */
313 if ( *lp_addusertogroup_script() ) {
314 TALLOC_CTX
*ctx
= talloc_tos();
316 add_script
= talloc_strdup(ctx
,
317 lp_addusertogroup_script());
321 add_script
= talloc_string_sub(ctx
,
322 add_script
, "%g", unix_group
);
326 add_script
= talloc_string_sub(ctx
,
327 add_script
, "%u", unix_user
);
331 ret
= smbrun(add_script
,NULL
);
332 DEBUG(ret
? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script
,ret
));
334 smb_nscd_flush_group_cache();
342 /****************************************************************************
343 Delete a user from a UNIX group
344 ****************************************************************************/
346 int smb_delete_user_group(const char *unix_group
, const char *unix_user
)
348 char *del_script
= NULL
;
351 /* defer to scripts */
353 if ( *lp_deluserfromgroup_script() ) {
354 TALLOC_CTX
*ctx
= talloc_tos();
356 del_script
= talloc_strdup(ctx
,
357 lp_deluserfromgroup_script());
361 del_script
= talloc_string_sub(ctx
,
362 del_script
, "%g", unix_group
);
366 del_script
= talloc_string_sub(ctx
,
367 del_script
, "%u", unix_user
);
371 ret
= smbrun(del_script
,NULL
);
372 DEBUG(ret
? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script
,ret
));
374 smb_nscd_flush_group_cache();
383 NTSTATUS
pdb_default_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
386 if (!init_group_mapping()) {
387 DEBUG(0,("failed to initialize group mapping\n"));
388 return NT_STATUS_UNSUCCESSFUL
;
390 return backend
->get_group_map_from_sid(sid
, map
) ?
391 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
394 NTSTATUS
pdb_default_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
397 if (!init_group_mapping()) {
398 DEBUG(0,("failed to initialize group mapping\n"));
399 return NT_STATUS_UNSUCCESSFUL
;
401 return backend
->get_group_map_from_gid(gid
, map
) ?
402 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
405 NTSTATUS
pdb_default_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
408 if (!init_group_mapping()) {
409 DEBUG(0,("failed to initialize group mapping\n"));
410 return NT_STATUS_UNSUCCESSFUL
;
412 return backend
->get_group_map_from_ntname(name
, map
) ?
413 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
416 NTSTATUS
pdb_default_add_group_mapping_entry(struct pdb_methods
*methods
,
419 if (!init_group_mapping()) {
420 DEBUG(0,("failed to initialize group mapping\n"));
421 return NT_STATUS_UNSUCCESSFUL
;
423 return backend
->add_mapping_entry(map
, TDB_INSERT
) ?
424 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
427 NTSTATUS
pdb_default_update_group_mapping_entry(struct pdb_methods
*methods
,
430 if (!init_group_mapping()) {
431 DEBUG(0,("failed to initialize group mapping\n"));
432 return NT_STATUS_UNSUCCESSFUL
;
434 return backend
->add_mapping_entry(map
, TDB_REPLACE
) ?
435 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
438 NTSTATUS
pdb_default_delete_group_mapping_entry(struct pdb_methods
*methods
,
441 if (!init_group_mapping()) {
442 DEBUG(0,("failed to initialize group mapping\n"));
443 return NT_STATUS_UNSUCCESSFUL
;
445 return backend
->group_map_remove(&sid
) ?
446 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
449 NTSTATUS
pdb_default_enum_group_mapping(struct pdb_methods
*methods
,
450 const struct dom_sid
*sid
, enum lsa_SidType sid_name_use
,
451 GROUP_MAP
**pp_rmap
, size_t *p_num_entries
,
454 if (!init_group_mapping()) {
455 DEBUG(0,("failed to initialize group mapping\n"));
456 return NT_STATUS_UNSUCCESSFUL
;
458 return backend
->enum_group_mapping(sid
, sid_name_use
, pp_rmap
, p_num_entries
, unix_only
) ?
459 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
462 NTSTATUS
pdb_default_create_alias(struct pdb_methods
*methods
,
463 const char *name
, uint32
*rid
)
466 enum lsa_SidType type
;
474 DEBUG(10, ("Trying to create alias %s\n", name
));
476 mem_ctx
= talloc_new(NULL
);
477 if (mem_ctx
== NULL
) {
478 return NT_STATUS_NO_MEMORY
;
481 exists
= lookup_name(mem_ctx
, name
, LOOKUP_NAME_LOCAL
,
482 NULL
, NULL
, &sid
, &type
);
483 TALLOC_FREE(mem_ctx
);
486 return NT_STATUS_ALIAS_EXISTS
;
489 if (!pdb_new_rid(&new_rid
)) {
490 DEBUG(0, ("Could not allocate a RID.\n"));
491 return NT_STATUS_ACCESS_DENIED
;
494 sid_compose(&sid
, get_global_sam_sid(), new_rid
);
496 if (!winbind_allocate_gid(&gid
)) {
497 DEBUG(3, ("Could not get a gid out of winbind - "
498 "wasted a rid :-(\n"));
499 return NT_STATUS_ACCESS_DENIED
;
502 DEBUG(10, ("Creating alias %s with gid %u and rid %u\n",
503 name
, (unsigned int)gid
, (unsigned int)new_rid
));
506 sid_copy(&map
.sid
, &sid
);
507 map
.sid_name_use
= SID_NAME_ALIAS
;
508 fstrcpy(map
.nt_name
, name
);
509 fstrcpy(map
.comment
, "");
511 status
= pdb_add_group_mapping_entry(&map
);
513 if (!NT_STATUS_IS_OK(status
)) {
514 DEBUG(0, ("Could not add group mapping entry for alias %s "
515 "(%s)\n", name
, nt_errstr(status
)));
524 NTSTATUS
pdb_default_delete_alias(struct pdb_methods
*methods
,
525 const struct dom_sid
*sid
)
527 return pdb_delete_group_mapping_entry(*sid
);
530 NTSTATUS
pdb_default_get_aliasinfo(struct pdb_methods
*methods
,
531 const struct dom_sid
*sid
,
532 struct acct_info
*info
)
536 if (!pdb_getgrsid(&map
, *sid
))
537 return NT_STATUS_NO_SUCH_ALIAS
;
539 if ((map
.sid_name_use
!= SID_NAME_ALIAS
) &&
540 (map
.sid_name_use
!= SID_NAME_WKN_GRP
)) {
541 DEBUG(2, ("%s is a %s, expected an alias\n",
543 sid_type_lookup(map
.sid_name_use
)));
544 return NT_STATUS_NO_SUCH_ALIAS
;
547 fstrcpy(info
->acct_name
, map
.nt_name
);
548 fstrcpy(info
->acct_desc
, map
.comment
);
549 sid_peek_rid(&map
.sid
, &info
->rid
);
553 NTSTATUS
pdb_default_set_aliasinfo(struct pdb_methods
*methods
,
554 const struct dom_sid
*sid
,
555 struct acct_info
*info
)
559 if (!pdb_getgrsid(&map
, *sid
))
560 return NT_STATUS_NO_SUCH_ALIAS
;
562 fstrcpy(map
.nt_name
, info
->acct_name
);
563 fstrcpy(map
.comment
, info
->acct_desc
);
565 return pdb_update_group_mapping_entry(&map
);
568 NTSTATUS
pdb_default_add_aliasmem(struct pdb_methods
*methods
,
569 const struct dom_sid
*alias
, const struct dom_sid
*member
)
571 if (!init_group_mapping()) {
572 DEBUG(0,("failed to initialize group mapping\n"));
573 return NT_STATUS_UNSUCCESSFUL
;
575 return backend
->add_aliasmem(alias
, member
);
578 NTSTATUS
pdb_default_del_aliasmem(struct pdb_methods
*methods
,
579 const struct dom_sid
*alias
, const struct dom_sid
*member
)
581 if (!init_group_mapping()) {
582 DEBUG(0,("failed to initialize group mapping\n"));
583 return NT_STATUS_UNSUCCESSFUL
;
585 return backend
->del_aliasmem(alias
, member
);
588 NTSTATUS
pdb_default_enum_aliasmem(struct pdb_methods
*methods
,
589 const struct dom_sid
*alias
, TALLOC_CTX
*mem_ctx
,
590 struct dom_sid
**pp_members
, size_t *p_num_members
)
592 if (!init_group_mapping()) {
593 DEBUG(0,("failed to initialize group mapping\n"));
594 return NT_STATUS_UNSUCCESSFUL
;
596 return backend
->enum_aliasmem(alias
, mem_ctx
, pp_members
,
600 NTSTATUS
pdb_default_alias_memberships(struct pdb_methods
*methods
,
602 const struct dom_sid
*domain_sid
,
603 const struct dom_sid
*members
,
605 uint32
**pp_alias_rids
,
606 size_t *p_num_alias_rids
)
608 struct dom_sid
*alias_sids
;
609 size_t i
, num_alias_sids
;
612 if (!init_group_mapping()) {
613 DEBUG(0,("failed to initialize group mapping\n"));
614 return NT_STATUS_UNSUCCESSFUL
;
620 result
= alias_memberships(members
, num_members
,
621 &alias_sids
, &num_alias_sids
);
623 if (!NT_STATUS_IS_OK(result
))
626 *p_num_alias_rids
= 0;
628 if (num_alias_sids
== 0) {
629 TALLOC_FREE(alias_sids
);
633 *pp_alias_rids
= TALLOC_ARRAY(mem_ctx
, uint32
, num_alias_sids
);
634 if (*pp_alias_rids
== NULL
)
635 return NT_STATUS_NO_MEMORY
;
637 for (i
=0; i
<num_alias_sids
; i
++) {
638 if (!sid_peek_check_rid(domain_sid
, &alias_sids
[i
],
639 &(*pp_alias_rids
)[*p_num_alias_rids
]))
641 *p_num_alias_rids
+= 1;
644 TALLOC_FREE(alias_sids
);
649 /**********************************************************************
650 no ops for passdb backends that don't implement group mapping
651 *********************************************************************/
653 NTSTATUS
pdb_nop_getgrsid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
656 return NT_STATUS_UNSUCCESSFUL
;
659 NTSTATUS
pdb_nop_getgrgid(struct pdb_methods
*methods
, GROUP_MAP
*map
,
662 return NT_STATUS_UNSUCCESSFUL
;
665 NTSTATUS
pdb_nop_getgrnam(struct pdb_methods
*methods
, GROUP_MAP
*map
,
668 return NT_STATUS_UNSUCCESSFUL
;
671 NTSTATUS
pdb_nop_add_group_mapping_entry(struct pdb_methods
*methods
,
674 return NT_STATUS_UNSUCCESSFUL
;
677 NTSTATUS
pdb_nop_update_group_mapping_entry(struct pdb_methods
*methods
,
680 return NT_STATUS_UNSUCCESSFUL
;
683 NTSTATUS
pdb_nop_delete_group_mapping_entry(struct pdb_methods
*methods
,
686 return NT_STATUS_UNSUCCESSFUL
;
689 NTSTATUS
pdb_nop_enum_group_mapping(struct pdb_methods
*methods
,
690 enum lsa_SidType sid_name_use
,
691 GROUP_MAP
**rmap
, size_t *num_entries
,
694 return NT_STATUS_UNSUCCESSFUL
;
697 /****************************************************************************
698 These need to be redirected through pdb_interface.c
699 ****************************************************************************/
700 bool pdb_get_dom_grp_info(const struct dom_sid
*sid
, struct acct_info
*info
)
706 res
= get_domain_group_from_sid(*sid
, &map
);
712 fstrcpy(info
->acct_name
, map
.nt_name
);
713 fstrcpy(info
->acct_desc
, map
.comment
);
714 sid_peek_rid(sid
, &info
->rid
);
718 bool pdb_set_dom_grp_info(const struct dom_sid
*sid
, const struct acct_info
*info
)
722 if (!get_domain_group_from_sid(*sid
, &map
))
725 fstrcpy(map
.nt_name
, info
->acct_name
);
726 fstrcpy(map
.comment
, info
->acct_desc
);
728 return NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map
));
731 /********************************************************************
732 Really just intended to be called by smbd
733 ********************************************************************/
735 NTSTATUS
pdb_create_builtin_alias(uint32 rid
)
738 enum lsa_SidType type
;
743 const char *name
= NULL
;
746 DEBUG(10, ("Trying to create builtin alias %d\n", rid
));
748 if ( !sid_compose( &sid
, &global_sid_Builtin
, rid
) ) {
749 return NT_STATUS_NO_SUCH_ALIAS
;
752 if ( (mem_ctx
= talloc_new(NULL
)) == NULL
) {
753 return NT_STATUS_NO_MEMORY
;
756 if ( !lookup_sid(mem_ctx
, &sid
, NULL
, &name
, &type
) ) {
757 TALLOC_FREE( mem_ctx
);
758 return NT_STATUS_NO_SUCH_ALIAS
;
761 /* validate RID so copy the name and move on */
763 fstrcpy( groupname
, name
);
764 TALLOC_FREE( mem_ctx
);
766 if (!winbind_allocate_gid(&gid
)) {
767 DEBUG(3, ("pdb_create_builtin_alias: Could not get a gid out of winbind\n"));
768 return NT_STATUS_ACCESS_DENIED
;
771 DEBUG(10,("Creating alias %s with gid %u\n", groupname
, (unsigned int)gid
));
774 sid_copy(&map
.sid
, &sid
);
775 map
.sid_name_use
= SID_NAME_ALIAS
;
776 fstrcpy(map
.nt_name
, groupname
);
777 fstrcpy(map
.comment
, "");
779 status
= pdb_add_group_mapping_entry(&map
);
781 if (!NT_STATUS_IS_OK(status
)) {
782 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
783 "(%s)\n", rid
, nt_errstr(status
)));