Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / source / groupdb / mapping.c
blob902068204aca11c239d77e6e99a3e947c429c381
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 2 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, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 static TDB_CONTEXT *tdb; /* used for driver files */
28 #define DATABASE_VERSION_V1 1 /* native byte format. */
29 #define DATABASE_VERSION_V2 2 /* le format. */
31 #define GROUP_PREFIX "UNIXGROUP/"
33 /* Alias memberships are stored reverse, as memberships. The performance
34 * critical operation is to determine the aliases a SID is member of, not
35 * listing alias members. So we store a list of alias SIDs a SID is member of
36 * hanging of the member as key.
38 #define MEMBEROF_PREFIX "MEMBEROF/"
41 static BOOL enum_group_mapping(const DOM_SID *sid, enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
42 size_t *p_num_entries, BOOL unix_only);
43 static BOOL group_map_remove(const DOM_SID *sid);
45 /****************************************************************************
46 Open the group mapping tdb.
47 ****************************************************************************/
49 static BOOL init_group_mapping(void)
51 const char *vstring = "INFO/version";
52 int32 vers_id;
53 GROUP_MAP *map_table = NULL;
54 size_t num_entries = 0;
56 if (tdb)
57 return True;
59 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
60 if (!tdb) {
61 DEBUG(0,("Failed to open group mapping database\n"));
62 return False;
65 /* handle a Samba upgrade */
66 tdb_lock_bystring(tdb, vstring);
68 /* Cope with byte-reversed older versions of the db. */
69 vers_id = tdb_fetch_int32(tdb, vstring);
70 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
71 /* Written on a bigendian machine with old fetch_int code. Save as le. */
72 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
73 vers_id = DATABASE_VERSION_V2;
76 /* if its an unknown version we remove everthing in the db */
78 if (vers_id != DATABASE_VERSION_V2) {
79 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
80 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
83 tdb_unlock_bystring(tdb, vstring);
85 /* cleanup any map entries with a gid == -1 */
87 if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table, &num_entries, False ) ) {
88 int i;
90 for ( i=0; i<num_entries; i++ ) {
91 if ( map_table[i].gid == -1 ) {
92 group_map_remove( &map_table[i].sid );
96 SAFE_FREE( map_table );
100 return True;
103 /****************************************************************************
104 ****************************************************************************/
105 static BOOL add_mapping_entry(GROUP_MAP *map, int flag)
107 TDB_DATA kbuf, dbuf;
108 pstring key, buf;
109 fstring string_sid="";
110 int len;
112 if(!init_group_mapping()) {
113 DEBUG(0,("failed to initialize group mapping\n"));
114 return(False);
117 sid_to_string(string_sid, &map->sid);
119 len = tdb_pack(buf, sizeof(buf), "ddff",
120 map->gid, map->sid_name_use, map->nt_name, map->comment);
122 if (len > sizeof(buf))
123 return False;
125 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
127 kbuf.dsize = strlen(key)+1;
128 kbuf.dptr = key;
129 dbuf.dsize = len;
130 dbuf.dptr = buf;
131 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
133 return True;
136 /****************************************************************************
137 initialise first time the mapping list
138 ****************************************************************************/
139 NTSTATUS add_initial_entry(gid_t gid, const char *sid, enum SID_NAME_USE sid_name_use, const char *nt_name, const char *comment)
141 GROUP_MAP map;
143 if(!init_group_mapping()) {
144 DEBUG(0,("failed to initialize group mapping\n"));
145 return NT_STATUS_UNSUCCESSFUL;
148 map.gid=gid;
149 if (!string_to_sid(&map.sid, sid)) {
150 DEBUG(0, ("string_to_sid failed: %s", sid));
151 return NT_STATUS_UNSUCCESSFUL;
154 map.sid_name_use=sid_name_use;
155 fstrcpy(map.nt_name, nt_name);
156 fstrcpy(map.comment, comment);
158 return pdb_add_group_mapping_entry(&map);
161 /****************************************************************************
162 Map a unix group to a newly created mapping
163 ****************************************************************************/
164 NTSTATUS map_unix_group(const struct group *grp, GROUP_MAP *pmap)
166 NTSTATUS status;
167 GROUP_MAP map;
168 const char *grpname, *dom, *name;
169 uint32 rid;
171 if (pdb_getgrgid(&map, grp->gr_gid)) {
172 return NT_STATUS_GROUP_EXISTS;
175 map.gid = grp->gr_gid;
176 grpname = grp->gr_name;
178 if (lookup_name(tmp_talloc_ctx(), grpname, LOOKUP_NAME_ISOLATED,
179 &dom, &name, NULL, NULL)) {
181 const char *tmp = talloc_asprintf(
182 tmp_talloc_ctx(), "Unix Group %s", grp->gr_name);
184 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
185 grpname, dom, name, tmp));
186 grpname = tmp;
189 if (lookup_name(tmp_talloc_ctx(), grpname, LOOKUP_NAME_ISOLATED,
190 NULL, NULL, NULL, NULL)) {
191 DEBUG(3, ("\"%s\" exists, can't map it\n", grp->gr_name));
192 return NT_STATUS_GROUP_EXISTS;
195 fstrcpy(map.nt_name, grpname);
197 if (pdb_rid_algorithm()) {
198 rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
199 } else {
200 if (!pdb_new_rid(&rid)) {
201 DEBUG(3, ("Could not get a new RID for %s\n",
202 grp->gr_name));
203 return NT_STATUS_ACCESS_DENIED;
207 sid_compose(&map.sid, get_global_sam_sid(), rid);
208 map.sid_name_use = SID_NAME_DOM_GRP;
209 fstrcpy(map.comment, talloc_asprintf(tmp_talloc_ctx(), "Unix Group %s",
210 grp->gr_name));
212 status = pdb_add_group_mapping_entry(&map);
213 if (NT_STATUS_IS_OK(status)) {
214 *pmap = map;
216 return status;
219 /****************************************************************************
220 Return the sid and the type of the unix group.
221 ****************************************************************************/
223 static BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
225 TDB_DATA kbuf, dbuf;
226 pstring key;
227 fstring string_sid;
228 int ret = 0;
230 if(!init_group_mapping()) {
231 DEBUG(0,("failed to initialize group mapping\n"));
232 return(False);
235 /* the key is the SID, retrieving is direct */
237 sid_to_string(string_sid, &sid);
238 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
240 kbuf.dptr = key;
241 kbuf.dsize = strlen(key)+1;
243 dbuf = tdb_fetch(tdb, kbuf);
244 if (!dbuf.dptr)
245 return False;
247 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
248 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
250 SAFE_FREE(dbuf.dptr);
252 if ( ret == -1 ) {
253 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
254 return False;
257 sid_copy(&map->sid, &sid);
259 return True;
262 /****************************************************************************
263 Return the sid and the type of the unix group.
264 ****************************************************************************/
266 static BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
268 TDB_DATA kbuf, dbuf, newkey;
269 fstring string_sid;
270 int ret;
272 if(!init_group_mapping()) {
273 DEBUG(0,("failed to initialize group mapping\n"));
274 return(False);
277 /* we need to enumerate the TDB to find the GID */
279 for (kbuf = tdb_firstkey(tdb);
280 kbuf.dptr;
281 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
283 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
285 dbuf = tdb_fetch(tdb, kbuf);
286 if (!dbuf.dptr)
287 continue;
289 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
291 string_to_sid(&map->sid, string_sid);
293 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
294 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
296 SAFE_FREE(dbuf.dptr);
298 if ( ret == -1 ) {
299 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
300 return False;
303 if (gid==map->gid) {
304 SAFE_FREE(kbuf.dptr);
305 return True;
309 return False;
312 /****************************************************************************
313 Return the sid and the type of the unix group.
314 ****************************************************************************/
316 static BOOL get_group_map_from_ntname(const char *name, GROUP_MAP *map)
318 TDB_DATA kbuf, dbuf, newkey;
319 fstring string_sid;
320 int ret;
322 if(!init_group_mapping()) {
323 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping\n"));
324 return(False);
327 /* we need to enumerate the TDB to find the name */
329 for (kbuf = tdb_firstkey(tdb);
330 kbuf.dptr;
331 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
333 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
335 dbuf = tdb_fetch(tdb, kbuf);
336 if (!dbuf.dptr)
337 continue;
339 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
341 string_to_sid(&map->sid, string_sid);
343 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
344 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
346 SAFE_FREE(dbuf.dptr);
348 if ( ret == -1 ) {
349 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
350 return False;
353 if ( strequal(name, map->nt_name) ) {
354 SAFE_FREE(kbuf.dptr);
355 return True;
359 return False;
362 /****************************************************************************
363 Remove a group mapping entry.
364 ****************************************************************************/
366 static BOOL group_map_remove(const DOM_SID *sid)
368 TDB_DATA kbuf, dbuf;
369 pstring key;
370 fstring string_sid;
372 if(!init_group_mapping()) {
373 DEBUG(0,("failed to initialize group mapping\n"));
374 return(False);
377 /* the key is the SID, retrieving is direct */
379 sid_to_string(string_sid, sid);
380 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
382 kbuf.dptr = key;
383 kbuf.dsize = strlen(key)+1;
385 dbuf = tdb_fetch(tdb, kbuf);
386 if (!dbuf.dptr)
387 return False;
389 SAFE_FREE(dbuf.dptr);
391 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
392 return False;
394 return True;
397 /****************************************************************************
398 Enumerate the group mapping.
399 ****************************************************************************/
401 static BOOL enum_group_mapping(const DOM_SID *domsid, enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
402 size_t *p_num_entries, BOOL unix_only)
404 TDB_DATA kbuf, dbuf, newkey;
405 fstring string_sid;
406 GROUP_MAP map;
407 GROUP_MAP *mapt;
408 int ret;
409 size_t entries=0;
410 DOM_SID grpsid;
411 uint32 rid;
413 if(!init_group_mapping()) {
414 DEBUG(0,("failed to initialize group mapping\n"));
415 return(False);
418 *p_num_entries=0;
419 *pp_rmap=NULL;
421 for (kbuf = tdb_firstkey(tdb);
422 kbuf.dptr;
423 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
425 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
426 continue;
428 dbuf = tdb_fetch(tdb, kbuf);
429 if (!dbuf.dptr)
430 continue;
432 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
434 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
435 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
437 SAFE_FREE(dbuf.dptr);
439 if ( ret == -1 ) {
440 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
441 continue;
444 /* list only the type or everything if UNKNOWN */
445 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
446 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
447 continue;
450 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
451 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
452 continue;
455 string_to_sid(&grpsid, string_sid);
456 sid_copy( &map.sid, &grpsid );
458 sid_split_rid( &grpsid, &rid );
460 /* Only check the domain if we were given one */
462 if ( domsid && !sid_equal( domsid, &grpsid ) ) {
463 DEBUG(11,("enum_group_mapping: group %s is not in domain %s\n",
464 string_sid, sid_string_static(domsid)));
465 continue;
468 DEBUG(11,("enum_group_mapping: returning group %s of "
469 "type %s\n", map.nt_name,
470 sid_type_lookup(map.sid_name_use)));
472 (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
473 if (!(*pp_rmap)) {
474 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
475 return False;
478 mapt = (*pp_rmap);
480 mapt[entries].gid = map.gid;
481 sid_copy( &mapt[entries].sid, &map.sid);
482 mapt[entries].sid_name_use = map.sid_name_use;
483 fstrcpy(mapt[entries].nt_name, map.nt_name);
484 fstrcpy(mapt[entries].comment, map.comment);
486 entries++;
490 *p_num_entries=entries;
492 return True;
495 /* This operation happens on session setup, so it should better be fast. We
496 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
498 static NTSTATUS one_alias_membership(const DOM_SID *member,
499 DOM_SID **sids, size_t *num)
501 fstring key, string_sid;
502 TDB_DATA kbuf, dbuf;
503 const char *p;
505 if (!init_group_mapping()) {
506 DEBUG(0,("failed to initialize group mapping\n"));
507 return NT_STATUS_ACCESS_DENIED;
510 sid_to_string(string_sid, member);
511 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
513 kbuf.dsize = strlen(key)+1;
514 kbuf.dptr = key;
516 dbuf = tdb_fetch(tdb, kbuf);
518 if (dbuf.dptr == NULL) {
519 return NT_STATUS_OK;
522 p = dbuf.dptr;
524 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
526 DOM_SID alias;
528 if (!string_to_sid(&alias, string_sid))
529 continue;
531 add_sid_to_array_unique(NULL, &alias, sids, num);
533 if (sids == NULL)
534 return NT_STATUS_NO_MEMORY;
537 SAFE_FREE(dbuf.dptr);
538 return NT_STATUS_OK;
541 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
542 DOM_SID **sids, size_t *num)
544 size_t i;
546 *num = 0;
547 *sids = NULL;
549 for (i=0; i<num_members; i++) {
550 NTSTATUS status = one_alias_membership(&members[i], sids, num);
551 if (!NT_STATUS_IS_OK(status))
552 return status;
554 return NT_STATUS_OK;
557 static BOOL is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
559 DOM_SID *sids;
560 size_t i, num;
562 /* This feels the wrong way round, but the on-disk data structure
563 * dictates it this way. */
564 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
565 return False;
567 for (i=0; i<num; i++) {
568 if (sid_compare(alias, &sids[i]) == 0) {
569 SAFE_FREE(sids);
570 return True;
573 SAFE_FREE(sids);
574 return False;
577 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
579 GROUP_MAP map;
580 TDB_DATA kbuf, dbuf;
581 pstring key;
582 fstring string_sid;
583 char *new_memberstring;
584 int result;
586 if(!init_group_mapping()) {
587 DEBUG(0,("failed to initialize group mapping\n"));
588 return NT_STATUS_ACCESS_DENIED;
591 if (!get_group_map_from_sid(*alias, &map))
592 return NT_STATUS_NO_SUCH_ALIAS;
594 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
595 (map.sid_name_use != SID_NAME_WKN_GRP) )
596 return NT_STATUS_NO_SUCH_ALIAS;
598 if (is_aliasmem(alias, member))
599 return NT_STATUS_MEMBER_IN_ALIAS;
601 sid_to_string(string_sid, member);
602 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
604 kbuf.dsize = strlen(key)+1;
605 kbuf.dptr = key;
607 dbuf = tdb_fetch(tdb, kbuf);
609 sid_to_string(string_sid, alias);
611 if (dbuf.dptr != NULL) {
612 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
613 string_sid);
614 } else {
615 new_memberstring = SMB_STRDUP(string_sid);
618 if (new_memberstring == NULL)
619 return NT_STATUS_NO_MEMORY;
621 SAFE_FREE(dbuf.dptr);
622 dbuf.dsize = strlen(new_memberstring)+1;
623 dbuf.dptr = new_memberstring;
625 result = tdb_store(tdb, kbuf, dbuf, 0);
627 SAFE_FREE(new_memberstring);
629 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
632 struct aliasmem_closure {
633 const DOM_SID *alias;
634 DOM_SID **sids;
635 size_t *num;
638 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
639 void *state)
641 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
642 const char *p;
643 fstring alias_string;
645 if (strncmp(key.dptr, MEMBEROF_PREFIX,
646 strlen(MEMBEROF_PREFIX)) != 0)
647 return 0;
649 p = data.dptr;
651 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
653 DOM_SID alias, member;
654 const char *member_string;
657 if (!string_to_sid(&alias, alias_string))
658 continue;
660 if (sid_compare(closure->alias, &alias) != 0)
661 continue;
663 /* Ok, we found the alias we're looking for in the membership
664 * list currently scanned. The key represents the alias
665 * member. Add that. */
667 member_string = strchr(key.dptr, '/');
669 /* Above we tested for MEMBEROF_PREFIX which includes the
670 * slash. */
672 SMB_ASSERT(member_string != NULL);
673 member_string += 1;
675 if (!string_to_sid(&member, member_string))
676 continue;
678 add_sid_to_array(NULL, &member, closure->sids, closure->num);
681 return 0;
684 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
686 GROUP_MAP map;
687 struct aliasmem_closure closure;
689 if(!init_group_mapping()) {
690 DEBUG(0,("failed to initialize group mapping\n"));
691 return NT_STATUS_ACCESS_DENIED;
694 if (!get_group_map_from_sid(*alias, &map))
695 return NT_STATUS_NO_SUCH_ALIAS;
697 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
698 (map.sid_name_use != SID_NAME_WKN_GRP) )
699 return NT_STATUS_NO_SUCH_ALIAS;
701 *sids = NULL;
702 *num = 0;
704 closure.alias = alias;
705 closure.sids = sids;
706 closure.num = num;
708 tdb_traverse(tdb, collect_aliasmem, &closure);
709 return NT_STATUS_OK;
712 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
714 NTSTATUS result;
715 DOM_SID *sids;
716 size_t i, num;
717 BOOL found = False;
718 char *member_string;
719 TDB_DATA kbuf, dbuf;
720 pstring key;
721 fstring sid_string;
723 result = alias_memberships(member, 1, &sids, &num);
725 if (!NT_STATUS_IS_OK(result))
726 return result;
728 for (i=0; i<num; i++) {
729 if (sid_compare(&sids[i], alias) == 0) {
730 found = True;
731 break;
735 if (!found) {
736 SAFE_FREE(sids);
737 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
740 if (i < num)
741 sids[i] = sids[num-1];
743 num -= 1;
745 sid_to_string(sid_string, member);
746 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, sid_string);
748 kbuf.dsize = strlen(key)+1;
749 kbuf.dptr = key;
751 if (num == 0)
752 return tdb_delete(tdb, kbuf) == 0 ?
753 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
755 member_string = SMB_STRDUP("");
757 if (member_string == NULL) {
758 SAFE_FREE(sids);
759 return NT_STATUS_NO_MEMORY;
762 for (i=0; i<num; i++) {
763 char *s = member_string;
765 sid_to_string(sid_string, &sids[i]);
766 asprintf(&member_string, "%s %s", s, sid_string);
768 SAFE_FREE(s);
769 if (member_string == NULL) {
770 SAFE_FREE(sids);
771 return NT_STATUS_NO_MEMORY;
775 dbuf.dsize = strlen(member_string)+1;
776 dbuf.dptr = member_string;
778 result = tdb_store(tdb, kbuf, dbuf, 0) == 0 ?
779 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
781 SAFE_FREE(sids);
782 SAFE_FREE(member_string);
784 return result;
789 * High level functions
790 * better to use them than the lower ones.
792 * we are checking if the group is in the mapping file
793 * and if the group is an existing unix group
797 /* get a domain group from it's SID */
799 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
801 struct group *grp;
802 BOOL ret;
804 if(!init_group_mapping()) {
805 DEBUG(0,("failed to initialize group mapping\n"));
806 return(False);
809 DEBUG(10, ("get_domain_group_from_sid\n"));
811 /* if the group is NOT in the database, it CAN NOT be a domain group */
813 become_root();
814 ret = pdb_getgrsid(map, sid);
815 unbecome_root();
817 /* special case check for rid 513 */
819 if ( !ret ) {
820 uint32 rid;
822 sid_peek_rid( &sid, &rid );
824 if ( rid == DOMAIN_GROUP_RID_USERS ) {
825 fstrcpy( map->nt_name, "None" );
826 fstrcpy( map->comment, "Ordinary Users" );
827 sid_copy( &map->sid, &sid );
828 map->sid_name_use = SID_NAME_DOM_GRP;
830 return True;
833 return False;
836 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
838 /* if it's not a domain group, continue */
839 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
840 return False;
843 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
845 if (map->gid==-1) {
846 return False;
849 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
851 grp = getgrgid(map->gid);
852 if ( !grp ) {
853 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
854 return False;
857 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
859 return True;
862 /****************************************************************************
863 Create a UNIX group on demand.
864 ****************************************************************************/
866 int smb_create_group(const char *unix_group, gid_t *new_gid)
868 pstring add_script;
869 int ret = -1;
870 int fd = 0;
872 *new_gid = 0;
874 /* defer to scripts */
876 if ( *lp_addgroup_script() ) {
877 pstrcpy(add_script, lp_addgroup_script());
878 pstring_sub(add_script, "%g", unix_group);
879 ret = smbrun(add_script, &fd);
880 DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
881 if (ret == 0) {
882 smb_nscd_flush_group_cache();
884 if (ret != 0)
885 return ret;
887 if (fd != 0) {
888 fstring output;
890 *new_gid = 0;
891 if (read(fd, output, sizeof(output)) > 0) {
892 *new_gid = (gid_t)strtoul(output, NULL, 10);
895 close(fd);
900 if (*new_gid == 0) {
901 struct group *grp = getgrnam(unix_group);
903 if (grp != NULL)
904 *new_gid = grp->gr_gid;
907 return ret;
910 /****************************************************************************
911 Delete a UNIX group on demand.
912 ****************************************************************************/
914 int smb_delete_group(const char *unix_group)
916 pstring del_script;
917 int ret;
919 /* defer to scripts */
921 if ( *lp_delgroup_script() ) {
922 pstrcpy(del_script, lp_delgroup_script());
923 pstring_sub(del_script, "%g", unix_group);
924 ret = smbrun(del_script,NULL);
925 DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
926 if (ret == 0) {
927 smb_nscd_flush_group_cache();
929 return ret;
932 return -1;
935 /****************************************************************************
936 Set a user's primary UNIX group.
937 ****************************************************************************/
938 int smb_set_primary_group(const char *unix_group, const char* unix_user)
940 pstring add_script;
941 int ret;
943 /* defer to scripts */
945 if ( *lp_setprimarygroup_script() ) {
946 pstrcpy(add_script, lp_setprimarygroup_script());
947 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
948 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
949 ret = smbrun(add_script,NULL);
950 flush_pwnam_cache();
951 DEBUG(ret ? 0 : 3,("smb_set_primary_group: "
952 "Running the command `%s' gave %d\n",add_script,ret));
953 if (ret == 0) {
954 smb_nscd_flush_group_cache();
956 return ret;
959 return -1;
962 /****************************************************************************
963 Add a user to a UNIX group.
964 ****************************************************************************/
966 int smb_add_user_group(const char *unix_group, const char *unix_user)
968 pstring add_script;
969 int ret;
971 /* defer to scripts */
973 if ( *lp_addusertogroup_script() ) {
974 pstrcpy(add_script, lp_addusertogroup_script());
975 pstring_sub(add_script, "%g", unix_group);
976 pstring_sub(add_script, "%u", unix_user);
977 ret = smbrun(add_script,NULL);
978 DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
979 if (ret == 0) {
980 smb_nscd_flush_group_cache();
982 return ret;
985 return -1;
988 /****************************************************************************
989 Delete a user from a UNIX group
990 ****************************************************************************/
992 int smb_delete_user_group(const char *unix_group, const char *unix_user)
994 pstring del_script;
995 int ret;
997 /* defer to scripts */
999 if ( *lp_deluserfromgroup_script() ) {
1000 pstrcpy(del_script, lp_deluserfromgroup_script());
1001 pstring_sub(del_script, "%g", unix_group);
1002 pstring_sub(del_script, "%u", unix_user);
1003 ret = smbrun(del_script,NULL);
1004 DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
1005 if (ret == 0) {
1006 smb_nscd_flush_group_cache();
1008 return ret;
1011 return -1;
1015 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1016 DOM_SID sid)
1018 return get_group_map_from_sid(sid, map) ?
1019 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1022 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1023 gid_t gid)
1025 return get_group_map_from_gid(gid, map) ?
1026 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1029 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1030 const char *name)
1032 return get_group_map_from_ntname(name, map) ?
1033 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1036 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
1037 GROUP_MAP *map)
1039 return add_mapping_entry(map, TDB_INSERT) ?
1040 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1043 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
1044 GROUP_MAP *map)
1046 return add_mapping_entry(map, TDB_REPLACE) ?
1047 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1050 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
1051 DOM_SID sid)
1053 return group_map_remove(&sid) ?
1054 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1057 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
1058 const DOM_SID *sid, enum SID_NAME_USE sid_name_use,
1059 GROUP_MAP **pp_rmap, size_t *p_num_entries,
1060 BOOL unix_only)
1062 return enum_group_mapping(sid, sid_name_use, pp_rmap, p_num_entries, unix_only) ?
1063 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1066 NTSTATUS pdb_default_find_alias(struct pdb_methods *methods,
1067 const char *name, DOM_SID *sid)
1069 GROUP_MAP map;
1071 if (!pdb_getgrnam(&map, name))
1072 return NT_STATUS_NO_SUCH_ALIAS;
1074 if ((map.sid_name_use != SID_NAME_WKN_GRP) &&
1075 (map.sid_name_use != SID_NAME_ALIAS))
1076 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1078 sid_copy(sid, &map.sid);
1079 return NT_STATUS_OK;
1082 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
1083 const char *name, uint32 *rid)
1085 DOM_SID sid;
1086 enum SID_NAME_USE type;
1087 uint32 new_rid;
1088 gid_t gid;
1089 BOOL exists;
1090 GROUP_MAP map;
1091 TALLOC_CTX *mem_ctx;
1092 NTSTATUS status;
1094 DEBUG(10, ("Trying to create alias %s\n", name));
1096 mem_ctx = talloc_new(NULL);
1097 if (mem_ctx == NULL) {
1098 return NT_STATUS_NO_MEMORY;
1101 exists = lookup_name(mem_ctx, name, LOOKUP_NAME_ISOLATED,
1102 NULL, NULL, &sid, &type);
1103 TALLOC_FREE(mem_ctx);
1105 if (exists) {
1106 return NT_STATUS_ALIAS_EXISTS;
1109 if (!winbind_allocate_gid(&gid)) {
1110 DEBUG(3, ("Could not get a gid out of winbind\n"));
1111 return NT_STATUS_ACCESS_DENIED;
1114 if (!pdb_new_rid(&new_rid)) {
1115 DEBUG(0, ("Could not allocate a RID -- wasted a gid :-(\n"));
1116 return NT_STATUS_ACCESS_DENIED;
1119 DEBUG(10, ("Creating alias %s with gid %d and rid %d\n",
1120 name, gid, new_rid));
1122 sid_copy(&sid, get_global_sam_sid());
1123 sid_append_rid(&sid, new_rid);
1125 map.gid = gid;
1126 sid_copy(&map.sid, &sid);
1127 map.sid_name_use = SID_NAME_ALIAS;
1128 fstrcpy(map.nt_name, name);
1129 fstrcpy(map.comment, "");
1131 status = pdb_add_group_mapping_entry(&map);
1133 if (!NT_STATUS_IS_OK(status)) {
1134 DEBUG(0, ("Could not add group mapping entry for alias %s "
1135 "(%s)\n", name, nt_errstr(status)));
1136 return status;
1139 *rid = new_rid;
1141 return NT_STATUS_OK;
1144 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
1145 const DOM_SID *sid)
1147 return pdb_delete_group_mapping_entry(*sid);
1150 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
1151 const DOM_SID *sid,
1152 struct acct_info *info)
1154 GROUP_MAP map;
1156 if (!pdb_getgrsid(&map, *sid))
1157 return NT_STATUS_NO_SUCH_ALIAS;
1159 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1160 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1161 DEBUG(2, ("%s is a %s, expected an alias\n",
1162 sid_string_static(sid),
1163 sid_type_lookup(map.sid_name_use)));
1164 return NT_STATUS_NO_SUCH_ALIAS;
1167 fstrcpy(info->acct_name, map.nt_name);
1168 fstrcpy(info->acct_desc, map.comment);
1169 sid_peek_rid(&map.sid, &info->rid);
1170 return NT_STATUS_OK;
1173 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
1174 const DOM_SID *sid,
1175 struct acct_info *info)
1177 GROUP_MAP map;
1179 if (!pdb_getgrsid(&map, *sid))
1180 return NT_STATUS_NO_SUCH_ALIAS;
1182 fstrcpy(map.nt_name, info->acct_name);
1183 fstrcpy(map.comment, info->acct_desc);
1185 return pdb_update_group_mapping_entry(&map);
1188 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
1189 const DOM_SID *alias, const DOM_SID *member)
1191 return add_aliasmem(alias, member);
1194 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
1195 const DOM_SID *alias, const DOM_SID *member)
1197 return del_aliasmem(alias, member);
1200 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
1201 const DOM_SID *alias, DOM_SID **pp_members,
1202 size_t *p_num_members)
1204 return enum_aliasmem(alias, pp_members, p_num_members);
1207 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
1208 TALLOC_CTX *mem_ctx,
1209 const DOM_SID *domain_sid,
1210 const DOM_SID *members,
1211 size_t num_members,
1212 uint32 **pp_alias_rids,
1213 size_t *p_num_alias_rids)
1215 DOM_SID *alias_sids;
1216 size_t i, num_alias_sids;
1217 NTSTATUS result;
1219 alias_sids = NULL;
1220 num_alias_sids = 0;
1222 result = alias_memberships(members, num_members,
1223 &alias_sids, &num_alias_sids);
1225 if (!NT_STATUS_IS_OK(result))
1226 return result;
1228 *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32, num_alias_sids);
1229 if (*pp_alias_rids == NULL)
1230 return NT_STATUS_NO_MEMORY;
1232 *p_num_alias_rids = 0;
1234 for (i=0; i<num_alias_sids; i++) {
1235 if (!sid_peek_check_rid(domain_sid, &alias_sids[i],
1236 &(*pp_alias_rids)[*p_num_alias_rids]))
1237 continue;
1238 *p_num_alias_rids += 1;
1241 SAFE_FREE(alias_sids);
1243 return NT_STATUS_OK;
1246 /**********************************************************************
1247 no ops for passdb backends that don't implement group mapping
1248 *********************************************************************/
1250 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1251 DOM_SID sid)
1253 return NT_STATUS_UNSUCCESSFUL;
1256 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1257 gid_t gid)
1259 return NT_STATUS_UNSUCCESSFUL;
1262 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1263 const char *name)
1265 return NT_STATUS_UNSUCCESSFUL;
1268 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
1269 GROUP_MAP *map)
1271 return NT_STATUS_UNSUCCESSFUL;
1274 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
1275 GROUP_MAP *map)
1277 return NT_STATUS_UNSUCCESSFUL;
1280 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
1281 DOM_SID sid)
1283 return NT_STATUS_UNSUCCESSFUL;
1286 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
1287 enum SID_NAME_USE sid_name_use,
1288 GROUP_MAP **rmap, size_t *num_entries,
1289 BOOL unix_only)
1291 return NT_STATUS_UNSUCCESSFUL;
1294 /****************************************************************************
1295 These need to be redirected through pdb_interface.c
1296 ****************************************************************************/
1297 BOOL pdb_get_dom_grp_info(const DOM_SID *sid, struct acct_info *info)
1299 GROUP_MAP map;
1300 BOOL res;
1302 become_root();
1303 res = get_domain_group_from_sid(*sid, &map);
1304 unbecome_root();
1306 if (!res)
1307 return False;
1309 fstrcpy(info->acct_name, map.nt_name);
1310 fstrcpy(info->acct_desc, map.comment);
1311 sid_peek_rid(sid, &info->rid);
1312 return True;
1315 BOOL pdb_set_dom_grp_info(const DOM_SID *sid, const struct acct_info *info)
1317 GROUP_MAP map;
1319 if (!get_domain_group_from_sid(*sid, &map))
1320 return False;
1322 fstrcpy(map.nt_name, info->acct_name);
1323 fstrcpy(map.comment, info->acct_desc);
1325 return NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map));
1328 /********************************************************************
1329 Really just intended to be called by smbd
1330 ********************************************************************/
1332 NTSTATUS pdb_create_builtin_alias(uint32 rid)
1334 DOM_SID sid;
1335 enum SID_NAME_USE type;
1336 gid_t gid;
1337 GROUP_MAP map;
1338 TALLOC_CTX *mem_ctx;
1339 NTSTATUS status;
1340 const char *name = NULL;
1341 fstring groupname;
1343 DEBUG(10, ("Trying to create builtin alias %d\n", rid));
1345 if ( !sid_compose( &sid, &global_sid_Builtin, rid ) ) {
1346 return NT_STATUS_NO_SUCH_ALIAS;
1349 if ( (mem_ctx = talloc_new(NULL)) == NULL ) {
1350 return NT_STATUS_NO_MEMORY;
1353 if ( !lookup_sid(mem_ctx, &sid, NULL, &name, &type) ) {
1354 TALLOC_FREE( mem_ctx );
1355 return NT_STATUS_NO_SUCH_ALIAS;
1358 /* validate RID so copy the name and move on */
1360 fstrcpy( groupname, name );
1361 TALLOC_FREE( mem_ctx );
1363 if (!winbind_allocate_gid(&gid)) {
1364 DEBUG(3, ("pdb_create_builtin_alias: Could not get a gid out of winbind\n"));
1365 return NT_STATUS_ACCESS_DENIED;
1368 DEBUG(10,("Creating alias %s with gid %d\n", name, gid));
1370 map.gid = gid;
1371 sid_copy(&map.sid, &sid);
1372 map.sid_name_use = SID_NAME_ALIAS;
1373 fstrcpy(map.nt_name, name);
1374 fstrcpy(map.comment, "");
1376 status = pdb_add_group_mapping_entry(&map);
1378 if (!NT_STATUS_IS_OK(status)) {
1379 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
1380 "(%s)\n", rid, nt_errstr(status)));
1383 return status;