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 const char *backend_string;
35 if (backend != NULL) {
36 /* already initialised */
40 /* default to using the ldb backend. This parameter should
41 disappear in future versions of Samba3, but for now it
42 provides a safety net in case any major problems are
43 discovered with ldb after the release */
44 backend_string = lp_parm_const_string(-1, "groupdb", "backend", "ldb");
46 if (strcmp(backend_string, "ldb") == 0) {
47 backend = groupdb_ldb_init();
48 } else if (strcmp(backend_string, "tdb") == 0) {
49 backend = groupdb_tdb_init();
51 DEBUG(0,("Unknown groupdb backend '%s'\n", backend_string));
52 smb_panic("Unknown groupdb backend");
54 return backend != NULL;
57 /****************************************************************************
58 initialise first time the mapping list
59 ****************************************************************************/
60 NTSTATUS add_initial_entry(gid_t gid, const char *sid, enum lsa_SidType sid_name_use, const char *nt_name, const char *comment)
64 if(!init_group_mapping()) {
65 DEBUG(0,("failed to initialize group mapping\n"));
66 return NT_STATUS_UNSUCCESSFUL;
70 if (!string_to_sid(&map.sid, sid)) {
71 DEBUG(0, ("string_to_sid failed: %s", sid));
72 return NT_STATUS_UNSUCCESSFUL;
75 map.sid_name_use=sid_name_use;
76 fstrcpy(map.nt_name, nt_name);
77 fstrcpy(map.comment, comment);
79 return pdb_add_group_mapping_entry(&map);
82 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
83 DOM_SID **sids, size_t *num)
90 for (i=0; i<num_members; i++) {
91 NTSTATUS status = backend->one_alias_membership(&members[i], sids, num);
92 if (!NT_STATUS_IS_OK(status))
98 struct aliasmem_closure {
108 * High level functions
109 * better to use them than the lower ones.
111 * we are checking if the group is in the mapping file
112 * and if the group is an existing unix group
116 /* get a domain group from it's SID */
118 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
123 if(!init_group_mapping()) {
124 DEBUG(0,("failed to initialize group mapping\n"));
128 DEBUG(10, ("get_domain_group_from_sid\n"));
130 /* if the group is NOT in the database, it CAN NOT be a domain group */
133 ret = pdb_getgrsid(map, sid);
136 /* special case check for rid 513 */
141 sid_peek_rid( &sid, &rid );
143 if ( rid == DOMAIN_GROUP_RID_USERS ) {
144 fstrcpy( map->nt_name, "None" );
145 fstrcpy( map->comment, "Ordinary Users" );
146 sid_copy( &map->sid, &sid );
147 map->sid_name_use = SID_NAME_DOM_GRP;
155 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
157 /* if it's not a domain group, continue */
158 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
162 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
168 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
170 grp = getgrgid(map->gid);
172 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
176 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
181 /****************************************************************************
182 Create a UNIX group on demand.
183 ****************************************************************************/
185 int smb_create_group(const char *unix_group, gid_t *new_gid)
193 /* defer to scripts */
195 if ( *lp_addgroup_script() ) {
196 pstrcpy(add_script, lp_addgroup_script());
197 pstring_sub(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)
238 /* defer to scripts */
240 if ( *lp_delgroup_script() ) {
241 pstrcpy(del_script, lp_delgroup_script());
242 pstring_sub(del_script, "%g", unix_group);
243 ret = smbrun(del_script,NULL);
244 DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
246 smb_nscd_flush_group_cache();
254 /****************************************************************************
255 Set a user's primary UNIX group.
256 ****************************************************************************/
257 int smb_set_primary_group(const char *unix_group, const char* unix_user)
262 /* defer to scripts */
264 if ( *lp_setprimarygroup_script() ) {
265 pstrcpy(add_script, lp_setprimarygroup_script());
266 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
267 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
268 ret = smbrun(add_script,NULL);
270 DEBUG(ret ? 0 : 3,("smb_set_primary_group: "
271 "Running the command `%s' gave %d\n",add_script,ret));
273 smb_nscd_flush_group_cache();
281 /****************************************************************************
282 Add a user to a UNIX group.
283 ****************************************************************************/
285 int smb_add_user_group(const char *unix_group, const char *unix_user)
290 /* defer to scripts */
292 if ( *lp_addusertogroup_script() ) {
293 pstrcpy(add_script, lp_addusertogroup_script());
294 pstring_sub(add_script, "%g", unix_group);
295 pstring_sub(add_script, "%u", unix_user);
296 ret = smbrun(add_script,NULL);
297 DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
299 smb_nscd_flush_group_cache();
307 /****************************************************************************
308 Delete a user from a UNIX group
309 ****************************************************************************/
311 int smb_delete_user_group(const char *unix_group, const char *unix_user)
316 /* defer to scripts */
318 if ( *lp_deluserfromgroup_script() ) {
319 pstrcpy(del_script, lp_deluserfromgroup_script());
320 pstring_sub(del_script, "%g", unix_group);
321 pstring_sub(del_script, "%u", unix_user);
322 ret = smbrun(del_script,NULL);
323 DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
325 smb_nscd_flush_group_cache();
334 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
337 if (!init_group_mapping()) {
338 DEBUG(0,("failed to initialize group mapping\n"));
339 return NT_STATUS_UNSUCCESSFUL;
341 return backend->get_group_map_from_sid(sid, map) ?
342 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
345 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
348 if (!init_group_mapping()) {
349 DEBUG(0,("failed to initialize group mapping\n"));
350 return NT_STATUS_UNSUCCESSFUL;
352 return backend->get_group_map_from_gid(gid, map) ?
353 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
356 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
359 if (!init_group_mapping()) {
360 DEBUG(0,("failed to initialize group mapping\n"));
361 return NT_STATUS_UNSUCCESSFUL;
363 return backend->get_group_map_from_ntname(name, map) ?
364 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
367 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
370 if (!init_group_mapping()) {
371 DEBUG(0,("failed to initialize group mapping\n"));
372 return NT_STATUS_UNSUCCESSFUL;
374 return backend->add_mapping_entry(map, TDB_INSERT) ?
375 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
378 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
381 if (!init_group_mapping()) {
382 DEBUG(0,("failed to initialize group mapping\n"));
383 return NT_STATUS_UNSUCCESSFUL;
385 return backend->add_mapping_entry(map, TDB_REPLACE) ?
386 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
389 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
392 if (!init_group_mapping()) {
393 DEBUG(0,("failed to initialize group mapping\n"));
394 return NT_STATUS_UNSUCCESSFUL;
396 return backend->group_map_remove(&sid) ?
397 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
400 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
401 const DOM_SID *sid, enum lsa_SidType sid_name_use,
402 GROUP_MAP **pp_rmap, size_t *p_num_entries,
405 if (!init_group_mapping()) {
406 DEBUG(0,("failed to initialize group mapping\n"));
407 return NT_STATUS_UNSUCCESSFUL;
409 return backend->enum_group_mapping(sid, sid_name_use, pp_rmap, p_num_entries, unix_only) ?
410 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
413 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
414 const char *name, uint32 *rid)
417 enum lsa_SidType type;
425 DEBUG(10, ("Trying to create alias %s\n", name));
427 mem_ctx = talloc_new(NULL);
428 if (mem_ctx == NULL) {
429 return NT_STATUS_NO_MEMORY;
432 exists = lookup_name(mem_ctx, name, LOOKUP_NAME_ISOLATED,
433 NULL, NULL, &sid, &type);
434 TALLOC_FREE(mem_ctx);
437 return NT_STATUS_ALIAS_EXISTS;
440 if (!winbind_allocate_gid(&gid)) {
441 DEBUG(3, ("Could not get a gid out of winbind\n"));
442 return NT_STATUS_ACCESS_DENIED;
445 if (!pdb_new_rid(&new_rid)) {
446 DEBUG(0, ("Could not allocate a RID -- wasted a gid :-(\n"));
447 return NT_STATUS_ACCESS_DENIED;
450 DEBUG(10, ("Creating alias %s with gid %d and rid %d\n",
451 name, gid, new_rid));
453 sid_copy(&sid, get_global_sam_sid());
454 sid_append_rid(&sid, new_rid);
457 sid_copy(&map.sid, &sid);
458 map.sid_name_use = SID_NAME_ALIAS;
459 fstrcpy(map.nt_name, name);
460 fstrcpy(map.comment, "");
462 status = pdb_add_group_mapping_entry(&map);
464 if (!NT_STATUS_IS_OK(status)) {
465 DEBUG(0, ("Could not add group mapping entry for alias %s "
466 "(%s)\n", name, nt_errstr(status)));
475 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
478 return pdb_delete_group_mapping_entry(*sid);
481 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
483 struct acct_info *info)
487 if (!pdb_getgrsid(&map, *sid))
488 return NT_STATUS_NO_SUCH_ALIAS;
490 if ((map.sid_name_use != SID_NAME_ALIAS) &&
491 (map.sid_name_use != SID_NAME_WKN_GRP)) {
492 DEBUG(2, ("%s is a %s, expected an alias\n",
493 sid_string_static(sid),
494 sid_type_lookup(map.sid_name_use)));
495 return NT_STATUS_NO_SUCH_ALIAS;
498 fstrcpy(info->acct_name, map.nt_name);
499 fstrcpy(info->acct_desc, map.comment);
500 sid_peek_rid(&map.sid, &info->rid);
504 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
506 struct acct_info *info)
510 if (!pdb_getgrsid(&map, *sid))
511 return NT_STATUS_NO_SUCH_ALIAS;
513 fstrcpy(map.nt_name, info->acct_name);
514 fstrcpy(map.comment, info->acct_desc);
516 return pdb_update_group_mapping_entry(&map);
519 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
520 const DOM_SID *alias, const DOM_SID *member)
522 if (!init_group_mapping()) {
523 DEBUG(0,("failed to initialize group mapping\n"));
524 return NT_STATUS_UNSUCCESSFUL;
526 return backend->add_aliasmem(alias, member);
529 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
530 const DOM_SID *alias, const DOM_SID *member)
532 if (!init_group_mapping()) {
533 DEBUG(0,("failed to initialize group mapping\n"));
534 return NT_STATUS_UNSUCCESSFUL;
536 return backend->del_aliasmem(alias, member);
539 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
540 const DOM_SID *alias, DOM_SID **pp_members,
541 size_t *p_num_members)
543 if (!init_group_mapping()) {
544 DEBUG(0,("failed to initialize group mapping\n"));
545 return NT_STATUS_UNSUCCESSFUL;
547 return backend->enum_aliasmem(alias, pp_members, p_num_members);
550 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
552 const DOM_SID *domain_sid,
553 const DOM_SID *members,
555 uint32 **pp_alias_rids,
556 size_t *p_num_alias_rids)
559 size_t i, num_alias_sids;
562 if (!init_group_mapping()) {
563 DEBUG(0,("failed to initialize group mapping\n"));
564 return NT_STATUS_UNSUCCESSFUL;
570 result = alias_memberships(members, num_members,
571 &alias_sids, &num_alias_sids);
573 if (!NT_STATUS_IS_OK(result))
576 *p_num_alias_rids = 0;
578 if (num_alias_sids == 0) {
579 TALLOC_FREE(alias_sids);
583 *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32, num_alias_sids);
584 if (*pp_alias_rids == NULL)
585 return NT_STATUS_NO_MEMORY;
587 for (i=0; i<num_alias_sids; i++) {
588 if (!sid_peek_check_rid(domain_sid, &alias_sids[i],
589 &(*pp_alias_rids)[*p_num_alias_rids]))
591 *p_num_alias_rids += 1;
594 TALLOC_FREE(alias_sids);
599 /**********************************************************************
600 no ops for passdb backends that don't implement group mapping
601 *********************************************************************/
603 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
606 return NT_STATUS_UNSUCCESSFUL;
609 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
612 return NT_STATUS_UNSUCCESSFUL;
615 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
618 return NT_STATUS_UNSUCCESSFUL;
621 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
624 return NT_STATUS_UNSUCCESSFUL;
627 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
630 return NT_STATUS_UNSUCCESSFUL;
633 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
636 return NT_STATUS_UNSUCCESSFUL;
639 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
640 enum lsa_SidType sid_name_use,
641 GROUP_MAP **rmap, size_t *num_entries,
644 return NT_STATUS_UNSUCCESSFUL;
647 /****************************************************************************
648 These need to be redirected through pdb_interface.c
649 ****************************************************************************/
650 BOOL pdb_get_dom_grp_info(const DOM_SID *sid, struct acct_info *info)
656 res = get_domain_group_from_sid(*sid, &map);
662 fstrcpy(info->acct_name, map.nt_name);
663 fstrcpy(info->acct_desc, map.comment);
664 sid_peek_rid(sid, &info->rid);
668 BOOL pdb_set_dom_grp_info(const DOM_SID *sid, const struct acct_info *info)
672 if (!get_domain_group_from_sid(*sid, &map))
675 fstrcpy(map.nt_name, info->acct_name);
676 fstrcpy(map.comment, info->acct_desc);
678 return NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map));
681 /********************************************************************
682 Really just intended to be called by smbd
683 ********************************************************************/
685 NTSTATUS pdb_create_builtin_alias(uint32 rid)
688 enum lsa_SidType type;
693 const char *name = NULL;
696 DEBUG(10, ("Trying to create builtin alias %d\n", rid));
698 if ( !sid_compose( &sid, &global_sid_Builtin, rid ) ) {
699 return NT_STATUS_NO_SUCH_ALIAS;
702 if ( (mem_ctx = talloc_new(NULL)) == NULL ) {
703 return NT_STATUS_NO_MEMORY;
706 if ( !lookup_sid(mem_ctx, &sid, NULL, &name, &type) ) {
707 TALLOC_FREE( mem_ctx );
708 return NT_STATUS_NO_SUCH_ALIAS;
711 /* validate RID so copy the name and move on */
713 fstrcpy( groupname, name );
714 TALLOC_FREE( mem_ctx );
716 if (!winbind_allocate_gid(&gid)) {
717 DEBUG(3, ("pdb_create_builtin_alias: Could not get a gid out of winbind\n"));
718 return NT_STATUS_ACCESS_DENIED;
721 DEBUG(10,("Creating alias %s with gid %d\n", name, gid));
724 sid_copy(&map.sid, &sid);
725 map.sid_name_use = SID_NAME_ALIAS;
726 fstrcpy(map.nt_name, name);
727 fstrcpy(map.comment, "");
729 status = pdb_add_group_mapping_entry(&map);
731 if (!NT_STATUS_IS_OK(status)) {
732 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
733 "(%s)\n", rid, nt_errstr(status)));