s3:build: fix typo in definition of --enable-external-libtdb
[Samba/gbeck.git] / source3 / groupdb / mapping.c
blob3646e04d0a3e5fd6e372d0de57dcb810e3c49880
1 /*
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.
8 *
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/>.
23 #include "includes.h"
24 #include "system/passwd.h"
25 #include "passdb.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 */
39 return True;
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)
52 GROUP_MAP map;
54 if(!init_group_mapping()) {
55 DEBUG(0,("failed to initialize group mapping\n"));
56 return NT_STATUS_UNSUCCESSFUL;
59 map.gid=gid;
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)
75 size_t i;
77 *num = 0;
78 *sids = NULL;
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))
83 return status;
85 return NT_STATUS_OK;
88 struct aliasmem_closure {
89 const struct dom_sid *alias;
90 struct dom_sid **sids;
91 size_t *num;
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)
110 struct group *grp;
111 bool ret;
113 if(!init_group_mapping()) {
114 DEBUG(0,("failed to initialize group mapping\n"));
115 return(False);
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 */
122 become_root();
123 ret = pdb_getgrsid(map, sid);
124 unbecome_root();
126 /* special case check for rid 513 */
128 if ( !ret ) {
129 uint32 rid;
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;
139 return True;
141 return False;
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) {
148 return False;
151 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
153 if (map->gid==-1) {
154 return False;
157 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
159 grp = getgrgid(map->gid);
160 if ( !grp ) {
161 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
162 return False;
165 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
167 return True;
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;
177 int ret = -1;
178 int fd = 0;
180 *new_gid = 0;
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());
189 if (!add_script) {
190 return -1;
192 add_script = talloc_string_sub(ctx,
193 add_script, "%g", unix_group);
194 if (!add_script) {
195 return -1;
198 ret = smbrun(add_script, &fd);
199 DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
200 if (ret == 0) {
201 smb_nscd_flush_group_cache();
203 if (ret != 0)
204 return ret;
206 if (fd != 0) {
207 fstring output;
209 *new_gid = 0;
210 if (read(fd, output, sizeof(output)) > 0) {
211 *new_gid = (gid_t)strtoul(output, NULL, 10);
214 close(fd);
219 if (*new_gid == 0) {
220 struct group *grp = getgrnam(unix_group);
222 if (grp != NULL)
223 *new_gid = grp->gr_gid;
226 return ret;
229 /****************************************************************************
230 Delete a UNIX group on demand.
231 ****************************************************************************/
233 int smb_delete_group(const char *unix_group)
235 char *del_script = NULL;
236 int ret = -1;
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());
245 if (!del_script) {
246 return -1;
248 del_script = talloc_string_sub(ctx,
249 del_script, "%g", unix_group);
250 if (!del_script) {
251 return -1;
253 ret = smbrun(del_script,NULL);
254 DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
255 if (ret == 0) {
256 smb_nscd_flush_group_cache();
258 return ret;
261 return -1;
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;
271 int ret = -1;
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());
280 if (!add_script) {
281 return -1;
283 add_script = talloc_all_string_sub(ctx,
284 add_script, "%g", unix_group);
285 if (!add_script) {
286 return -1;
288 add_script = talloc_string_sub(ctx,
289 add_script, "%u", unix_user);
290 if (!add_script) {
291 return -1;
293 ret = smbrun(add_script,NULL);
294 flush_pwnam_cache();
295 DEBUG(ret ? 0 : 3,("smb_set_primary_group: "
296 "Running the command `%s' gave %d\n",add_script,ret));
297 if (ret == 0) {
298 smb_nscd_flush_group_cache();
300 return ret;
303 return -1;
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;
313 int ret = -1;
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());
322 if (!add_script) {
323 return -1;
325 add_script = talloc_string_sub(ctx,
326 add_script, "%g", unix_group);
327 if (!add_script) {
328 return -1;
330 add_script = talloc_string_sub2(ctx,
331 add_script, "%u", unix_user, true, false, true);
332 if (!add_script) {
333 return -1;
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));
337 if (ret == 0) {
338 smb_nscd_flush_group_cache();
340 return ret;
343 return -1;
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;
353 int ret = -1;
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());
362 if (!del_script) {
363 return -1;
365 del_script = talloc_string_sub(ctx,
366 del_script, "%g", unix_group);
367 if (!del_script) {
368 return -1;
370 del_script = talloc_string_sub2(ctx,
371 del_script, "%u", unix_user, true, false, true);
372 if (!del_script) {
373 return -1;
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));
377 if (ret == 0) {
378 smb_nscd_flush_group_cache();
380 return ret;
383 return -1;
387 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
388 struct dom_sid sid)
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,
399 gid_t gid)
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,
410 const char *name)
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,
421 GROUP_MAP *map)
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,
432 GROUP_MAP *map)
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,
443 struct dom_sid sid)
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,
456 bool unix_only)
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)
469 struct dom_sid sid;
470 enum lsa_SidType type;
471 uint32 new_rid;
472 gid_t gid;
473 bool exists;
474 GROUP_MAP map;
475 TALLOC_CTX *mem_ctx;
476 NTSTATUS status;
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);
489 if (exists) {
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));
509 map.gid = gid;
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)));
520 return status;
523 *rid = new_rid;
525 return NT_STATUS_OK;
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)
538 GROUP_MAP map;
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",
546 sid_string_dbg(sid),
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);
554 return NT_STATUS_OK;
557 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
558 const struct dom_sid *sid,
559 struct acct_info *info)
561 GROUP_MAP map;
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,
601 p_num_members);
604 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
605 TALLOC_CTX *mem_ctx,
606 const struct dom_sid *domain_sid,
607 const struct dom_sid *members,
608 size_t num_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;
614 NTSTATUS result;
616 if (!init_group_mapping()) {
617 DEBUG(0,("failed to initialize group mapping\n"));
618 return NT_STATUS_UNSUCCESSFUL;
621 alias_sids = NULL;
622 num_alias_sids = 0;
624 result = alias_memberships(members, num_members,
625 &alias_sids, &num_alias_sids);
627 if (!NT_STATUS_IS_OK(result))
628 return result;
630 *p_num_alias_rids = 0;
632 if (num_alias_sids == 0) {
633 TALLOC_FREE(alias_sids);
634 return NT_STATUS_OK;
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]))
644 continue;
645 *p_num_alias_rids += 1;
648 TALLOC_FREE(alias_sids);
650 return NT_STATUS_OK;
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,
658 struct dom_sid sid)
660 return NT_STATUS_UNSUCCESSFUL;
663 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
664 gid_t gid)
666 return NT_STATUS_UNSUCCESSFUL;
669 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
670 const char *name)
672 return NT_STATUS_UNSUCCESSFUL;
675 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
676 GROUP_MAP *map)
678 return NT_STATUS_UNSUCCESSFUL;
681 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
682 GROUP_MAP *map)
684 return NT_STATUS_UNSUCCESSFUL;
687 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
688 struct dom_sid sid)
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,
696 bool unix_only)
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)
706 GROUP_MAP map;
707 bool res;
709 become_root();
710 res = get_domain_group_from_sid(*sid, &map);
711 unbecome_root();
713 if (!res)
714 return False;
716 fstrcpy(info->acct_name, map.nt_name);
717 fstrcpy(info->acct_desc, map.comment);
718 sid_peek_rid(sid, &info->rid);
719 return True;
722 bool pdb_set_dom_grp_info(const struct dom_sid *sid, const struct acct_info *info)
724 GROUP_MAP map;
726 if (!get_domain_group_from_sid(*sid, &map))
727 return False;
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)
741 struct dom_sid sid;
742 enum lsa_SidType type;
743 gid_t gid;
744 GROUP_MAP map;
745 TALLOC_CTX *mem_ctx;
746 NTSTATUS status;
747 const char *name = NULL;
748 fstring groupname;
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));
777 map.gid = 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)));
790 return status;