Add and delete aliases via srv_samr_nt. For that I added a RID allocation call
[Samba/gebeck_regimport.git] / source3 / groupdb / mapping.c
blobcbf022f377257ed0ccb1b2d0bf39d64d8872c61d
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 static TDB_CONTEXT *tdb; /* used for driver files */
26 #define DATABASE_VERSION_V1 1 /* native byte format. */
27 #define DATABASE_VERSION_V2 2 /* le format. */
29 #define GROUP_PREFIX "UNIXGROUP/"
30 #define ALIASMEM_PREFIX "ALIASMEMBERS/"
32 /****************************************************************************
33 dump the mapping group mapping to a text file
34 ****************************************************************************/
35 char *decode_sid_name_use(fstring group, enum SID_NAME_USE name_use)
37 static fstring group_type;
39 switch(name_use) {
40 case SID_NAME_USER:
41 fstrcpy(group_type,"User");
42 break;
43 case SID_NAME_DOM_GRP:
44 fstrcpy(group_type,"Domain group");
45 break;
46 case SID_NAME_DOMAIN:
47 fstrcpy(group_type,"Domain");
48 break;
49 case SID_NAME_ALIAS:
50 fstrcpy(group_type,"Local group");
51 break;
52 case SID_NAME_WKN_GRP:
53 fstrcpy(group_type,"Builtin group");
54 break;
55 case SID_NAME_DELETED:
56 fstrcpy(group_type,"Deleted");
57 break;
58 case SID_NAME_INVALID:
59 fstrcpy(group_type,"Invalid");
60 break;
61 case SID_NAME_UNKNOWN:
62 default:
63 fstrcpy(group_type,"Unknown type");
64 break;
67 fstrcpy(group, group_type);
68 return group_type;
71 /****************************************************************************
72 initialise first time the mapping list - called from init_group_mapping()
73 ****************************************************************************/
74 static BOOL default_group_mapping(void)
76 DOM_SID sid_admins;
77 DOM_SID sid_users;
78 DOM_SID sid_guests;
79 fstring str_admins;
80 fstring str_users;
81 fstring str_guests;
83 /* Add the Wellknown groups */
85 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP, "Administrators", "");
86 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP, "Users", "");
87 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP, "Guests", "");
88 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP, "Power Users", "");
89 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP, "Account Operators", "");
90 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP, "System Operators", "");
91 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP, "Print Operators", "");
92 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP, "Backup Operators", "");
93 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP, "Replicators", "");
95 /* Add the defaults domain groups */
97 sid_copy(&sid_admins, get_global_sam_sid());
98 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
99 sid_to_string(str_admins, &sid_admins);
100 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "");
102 sid_copy(&sid_users, get_global_sam_sid());
103 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
104 sid_to_string(str_users, &sid_users);
105 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "");
107 sid_copy(&sid_guests, get_global_sam_sid());
108 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
109 sid_to_string(str_guests, &sid_guests);
110 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "");
112 return True;
115 /****************************************************************************
116 Open the group mapping tdb.
117 ****************************************************************************/
119 static BOOL init_group_mapping(void)
121 static pid_t local_pid;
122 const char *vstring = "INFO/version";
123 int32 vers_id;
125 if (tdb && local_pid == sys_getpid())
126 return True;
127 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
128 if (!tdb) {
129 DEBUG(0,("Failed to open group mapping database\n"));
130 return False;
133 local_pid = sys_getpid();
135 /* handle a Samba upgrade */
136 tdb_lock_bystring(tdb, vstring, 0);
138 /* Cope with byte-reversed older versions of the db. */
139 vers_id = tdb_fetch_int32(tdb, vstring);
140 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
141 /* Written on a bigendian machine with old fetch_int code. Save as le. */
142 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
143 vers_id = DATABASE_VERSION_V2;
146 if (vers_id != DATABASE_VERSION_V2) {
147 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
148 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
151 tdb_unlock_bystring(tdb, vstring);
153 /* write a list of default groups */
154 if(!default_group_mapping())
155 return False;
157 return True;
160 /****************************************************************************
161 ****************************************************************************/
162 static BOOL add_mapping_entry(GROUP_MAP *map, int flag)
164 TDB_DATA kbuf, dbuf;
165 pstring key, buf;
166 fstring string_sid="";
167 int len;
169 if(!init_group_mapping()) {
170 DEBUG(0,("failed to initialize group mapping\n"));
171 return(False);
174 sid_to_string(string_sid, &map->sid);
176 len = tdb_pack(buf, sizeof(buf), "ddff",
177 map->gid, map->sid_name_use, map->nt_name, map->comment);
179 if (len > sizeof(buf))
180 return False;
182 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
184 kbuf.dsize = strlen(key)+1;
185 kbuf.dptr = key;
186 dbuf.dsize = len;
187 dbuf.dptr = buf;
188 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
190 return True;
193 /****************************************************************************
194 initialise first time the mapping list
195 ****************************************************************************/
196 BOOL add_initial_entry(gid_t gid, const char *sid, enum SID_NAME_USE sid_name_use, const char *nt_name, const char *comment)
198 GROUP_MAP map;
200 if(!init_group_mapping()) {
201 DEBUG(0,("failed to initialize group mapping\n"));
202 return(False);
205 map.gid=gid;
206 if (!string_to_sid(&map.sid, sid)) {
207 DEBUG(0, ("string_to_sid failed: %s", sid));
208 return False;
211 map.sid_name_use=sid_name_use;
212 fstrcpy(map.nt_name, nt_name);
213 fstrcpy(map.comment, comment);
215 return pdb_add_group_mapping_entry(&map);
218 /****************************************************************************
219 Return the sid and the type of the unix group.
220 ****************************************************************************/
222 static BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
224 TDB_DATA kbuf, dbuf;
225 pstring key;
226 fstring string_sid;
227 int ret = 0;
229 if(!init_group_mapping()) {
230 DEBUG(0,("failed to initialize group mapping\n"));
231 return(False);
234 /* the key is the SID, retrieving is direct */
236 sid_to_string(string_sid, &sid);
237 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
239 kbuf.dptr = key;
240 kbuf.dsize = strlen(key)+1;
242 dbuf = tdb_fetch(tdb, kbuf);
243 if (!dbuf.dptr)
244 return False;
246 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
247 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
249 SAFE_FREE(dbuf.dptr);
251 if ( ret == -1 ) {
252 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
253 return False;
256 sid_copy(&map->sid, &sid);
258 return True;
261 /****************************************************************************
262 Return the sid and the type of the unix group.
263 ****************************************************************************/
265 static BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
267 TDB_DATA kbuf, dbuf, newkey;
268 fstring string_sid;
269 int ret;
271 if(!init_group_mapping()) {
272 DEBUG(0,("failed to initialize group mapping\n"));
273 return(False);
276 /* we need to enumerate the TDB to find the GID */
278 for (kbuf = tdb_firstkey(tdb);
279 kbuf.dptr;
280 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
282 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
284 dbuf = tdb_fetch(tdb, kbuf);
285 if (!dbuf.dptr)
286 continue;
288 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
290 string_to_sid(&map->sid, string_sid);
292 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
293 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
295 SAFE_FREE(dbuf.dptr);
297 if ( ret == -1 ) {
298 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
299 return False;
302 if (gid==map->gid) {
303 SAFE_FREE(kbuf.dptr);
304 return True;
308 return False;
311 /****************************************************************************
312 Return the sid and the type of the unix group.
313 ****************************************************************************/
315 static BOOL get_group_map_from_ntname(const char *name, GROUP_MAP *map)
317 TDB_DATA kbuf, dbuf, newkey;
318 fstring string_sid;
319 int ret;
321 if(!init_group_mapping()) {
322 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping\n"));
323 return(False);
326 /* we need to enumerate the TDB to find the name */
328 for (kbuf = tdb_firstkey(tdb);
329 kbuf.dptr;
330 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
332 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
334 dbuf = tdb_fetch(tdb, kbuf);
335 if (!dbuf.dptr)
336 continue;
338 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
340 string_to_sid(&map->sid, string_sid);
342 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
343 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
345 SAFE_FREE(dbuf.dptr);
347 if ( ret == -1 ) {
348 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
349 return False;
352 if (StrCaseCmp(name, map->nt_name)==0) {
353 SAFE_FREE(kbuf.dptr);
354 return True;
358 return False;
361 /****************************************************************************
362 Remove a group mapping entry.
363 ****************************************************************************/
365 static BOOL group_map_remove(const DOM_SID *sid)
367 TDB_DATA kbuf, dbuf;
368 pstring key;
369 fstring string_sid;
371 if(!init_group_mapping()) {
372 DEBUG(0,("failed to initialize group mapping\n"));
373 return(False);
376 /* the key is the SID, retrieving is direct */
378 sid_to_string(string_sid, sid);
379 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
381 kbuf.dptr = key;
382 kbuf.dsize = strlen(key)+1;
384 dbuf = tdb_fetch(tdb, kbuf);
385 if (!dbuf.dptr)
386 return False;
388 SAFE_FREE(dbuf.dptr);
390 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
391 return False;
393 return True;
396 /****************************************************************************
397 Enumerate the group mapping.
398 ****************************************************************************/
400 static BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
401 int *num_entries, BOOL unix_only)
403 TDB_DATA kbuf, dbuf, newkey;
404 fstring string_sid;
405 fstring group_type;
406 GROUP_MAP map;
407 GROUP_MAP *mapt;
408 int ret;
409 int entries=0;
411 if(!init_group_mapping()) {
412 DEBUG(0,("failed to initialize group mapping\n"));
413 return(False);
416 *num_entries=0;
417 *rmap=NULL;
419 for (kbuf = tdb_firstkey(tdb);
420 kbuf.dptr;
421 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
423 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
424 continue;
426 dbuf = tdb_fetch(tdb, kbuf);
427 if (!dbuf.dptr)
428 continue;
430 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
432 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
433 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
435 SAFE_FREE(dbuf.dptr);
437 if ( ret == -1 ) {
438 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
439 continue;
442 /* list only the type or everything if UNKNOWN */
443 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
444 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
445 continue;
448 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
449 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
450 continue;
453 string_to_sid(&map.sid, string_sid);
455 decode_sid_name_use(group_type, map.sid_name_use);
456 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map.nt_name ,group_type));
458 mapt=(GROUP_MAP *)Realloc((*rmap), (entries+1)*sizeof(GROUP_MAP));
459 if (!mapt) {
460 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
461 SAFE_FREE(*rmap);
462 return False;
464 else
465 (*rmap) = mapt;
467 mapt[entries].gid = map.gid;
468 sid_copy( &mapt[entries].sid, &map.sid);
469 mapt[entries].sid_name_use = map.sid_name_use;
470 fstrcpy(mapt[entries].nt_name, map.nt_name);
471 fstrcpy(mapt[entries].comment, map.comment);
473 entries++;
477 *num_entries=entries;
479 return True;
482 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
484 GROUP_MAP map;
485 TDB_DATA kbuf, dbuf;
486 pstring key;
487 fstring string_sid;
488 char *new_memberstring;
489 int result;
491 if(!init_group_mapping()) {
492 DEBUG(0,("failed to initialize group mapping\n"));
493 return NT_STATUS_ACCESS_DENIED;
496 if (!get_group_map_from_sid(*alias, &map))
497 return NT_STATUS_NO_SUCH_ALIAS;
499 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
500 (map.sid_name_use != SID_NAME_WKN_GRP) )
501 return NT_STATUS_NO_SUCH_ALIAS;
503 sid_to_string(string_sid, alias);
504 slprintf(key, sizeof(key), "%s%s", ALIASMEM_PREFIX, string_sid);
506 kbuf.dsize = strlen(key)+1;
507 kbuf.dptr = key;
509 dbuf = tdb_fetch(tdb, kbuf);
511 sid_to_string(string_sid, member);
513 if (dbuf.dptr != NULL) {
514 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
515 string_sid);
516 } else {
517 new_memberstring = strdup(string_sid);
520 if (new_memberstring == NULL)
521 return NT_STATUS_NO_MEMORY;
523 SAFE_FREE(dbuf.dptr);
524 dbuf.dsize = strlen(new_memberstring)+1;
525 dbuf.dptr = new_memberstring;
527 result = tdb_store(tdb, kbuf, dbuf, 0);
529 SAFE_FREE(new_memberstring);
531 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
534 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, int *num)
536 GROUP_MAP map;
537 TDB_DATA kbuf, dbuf;
538 pstring key;
539 fstring string_sid;
540 const char *p;
542 if(!init_group_mapping()) {
543 DEBUG(0,("failed to initialize group mapping\n"));
544 return NT_STATUS_ACCESS_DENIED;
547 if (!get_group_map_from_sid(*alias, &map))
548 return NT_STATUS_NO_SUCH_ALIAS;
550 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
551 (map.sid_name_use != SID_NAME_WKN_GRP) )
552 return NT_STATUS_NO_SUCH_ALIAS;
554 *sids = NULL;
555 *num = 0;
557 sid_to_string(string_sid, alias);
558 slprintf(key, sizeof(key), "%s%s", ALIASMEM_PREFIX, string_sid);
560 kbuf.dsize = strlen(key)+1;
561 kbuf.dptr = key;
563 dbuf = tdb_fetch(tdb, kbuf);
565 if (dbuf.dptr == NULL) {
566 return NT_STATUS_OK;
569 p = dbuf.dptr;
571 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
573 DOM_SID sid;
575 if (!string_to_sid(&sid, string_sid))
576 continue;
578 add_sid_to_array(&sid, sids, num);
580 if (sids == NULL)
581 return NT_STATUS_NO_MEMORY;
584 SAFE_FREE(dbuf.dptr);
586 return NT_STATUS_OK;
589 /* This is racy as hell, but hey, it's only a prototype :-) */
591 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
593 NTSTATUS result;
594 DOM_SID *sids;
595 int i, num;
596 BOOL found = False;
597 char *member_string;
598 TDB_DATA kbuf, dbuf;
599 pstring key;
600 fstring sid_string;
602 result = enum_aliasmem(alias, &sids, &num);
604 if (!NT_STATUS_IS_OK(result))
605 return result;
607 for (i=0; i<num; i++) {
608 if (sid_compare(&sids[i], member) == 0) {
609 found = True;
610 break;
614 if (!found) {
615 SAFE_FREE(sids);
616 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
619 if (i < num)
620 sids[i] = sids[num-1];
622 num -= 1;
624 member_string = strdup("");
626 if (member_string == NULL) {
627 SAFE_FREE(sids);
628 return NT_STATUS_NO_MEMORY;
631 for (i=0; i<num; i++) {
632 char *s = member_string;
634 sid_to_string(sid_string, &sids[i]);
635 asprintf(&member_string, "%s %s", s, sid_string);
637 SAFE_FREE(s);
638 if (member_string == NULL) {
639 SAFE_FREE(sids);
640 return NT_STATUS_NO_MEMORY;
644 sid_to_string(sid_string, alias);
645 slprintf(key, sizeof(key), "%s%s", ALIASMEM_PREFIX, sid_string);
647 kbuf.dsize = strlen(key)+1;
648 kbuf.dptr = key;
649 dbuf.dsize = strlen(member_string)+1;
650 dbuf.dptr = member_string;
652 result = tdb_store(tdb, kbuf, dbuf, 0) == 0 ?
653 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
655 SAFE_FREE(sids);
656 SAFE_FREE(member_string);
658 return result;
661 static BOOL is_foreign_alias_member(const DOM_SID *sid, const DOM_SID *alias)
663 DOM_SID *members;
664 int i, num;
665 BOOL result = False;
667 if (!NT_STATUS_IS_OK(enum_aliasmem(alias, &members, &num)))
668 return False;
670 for (i=0; i<num; i++) {
672 if (sid_compare(&members[i], sid) == 0) {
673 result = True;
674 break;
678 SAFE_FREE(members);
679 return result;
682 static NTSTATUS alias_memberships(const DOM_SID *sid, DOM_SID **sids, int *num)
684 GROUP_MAP *maps;
685 int i, num_maps;
687 *num = 0;
688 *sids = NULL;
690 if (!enum_group_mapping(SID_NAME_WKN_GRP, &maps, &num_maps, False))
691 return NT_STATUS_NO_MEMORY;
693 for (i=0; i<num_maps; i++) {
695 if (is_foreign_alias_member(sid, &maps[i].sid)) {
697 add_sid_to_array(&maps[i].sid, sids, num);
699 if (sids == NULL) {
700 SAFE_FREE(maps);
701 return NT_STATUS_NO_MEMORY;
705 SAFE_FREE(maps);
707 if (!enum_group_mapping(SID_NAME_ALIAS, &maps, &num_maps, False))
708 return NT_STATUS_NO_MEMORY;
710 for (i=0; i<num_maps; i++) {
711 if (is_foreign_alias_member(sid, &maps[i].sid)) {
713 add_sid_to_array(&maps[i].sid, sids, num);
715 if (sids == NULL) {
716 SAFE_FREE(maps);
717 return NT_STATUS_NO_MEMORY;
721 SAFE_FREE(maps);
723 return NT_STATUS_OK;
728 * High level functions
729 * better to use them than the lower ones.
731 * we are checking if the group is in the mapping file
732 * and if the group is an existing unix group
736 /* get a domain group from it's SID */
738 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
740 struct group *grp;
741 BOOL ret;
743 if(!init_group_mapping()) {
744 DEBUG(0,("failed to initialize group mapping\n"));
745 return(False);
748 DEBUG(10, ("get_domain_group_from_sid\n"));
750 /* if the group is NOT in the database, it CAN NOT be a domain group */
752 become_root();
753 ret = pdb_getgrsid(map, sid);
754 unbecome_root();
756 if ( !ret )
757 return False;
759 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
761 /* if it's not a domain group, continue */
762 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
763 return False;
766 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
768 if (map->gid==-1) {
769 return False;
772 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
774 grp = getgrgid(map->gid);
775 if ( !grp ) {
776 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
777 return False;
780 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
782 return True;
786 /* get a local (alias) group from it's SID */
788 BOOL get_local_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
790 BOOL ret;
792 if(!init_group_mapping()) {
793 DEBUG(0,("failed to initialize group mapping\n"));
794 return(False);
797 /* The group is in the mapping table */
798 become_root();
799 ret = pdb_getgrsid(map, *sid);
800 unbecome_root();
802 if ( !ret )
803 return False;
805 if ( ( (map->sid_name_use != SID_NAME_ALIAS) &&
806 (map->sid_name_use != SID_NAME_WKN_GRP) )
807 || (map->gid == -1)
808 || (getgrgid(map->gid) == NULL) )
810 return False;
813 #if 1 /* JERRY */
814 /* local groups only exist in the group mapping DB so this
815 is not necessary */
817 else {
818 /* the group isn't in the mapping table.
819 * make one based on the unix information */
820 uint32 alias_rid;
821 struct group *grp;
823 sid_peek_rid(sid, &alias_rid);
824 map->gid=pdb_group_rid_to_gid(alias_rid);
826 grp = getgrgid(map->gid);
827 if ( !grp ) {
828 DEBUG(3,("get_local_group_from_sid: No unix group for [%ul]\n", map->gid));
829 return False;
832 map->sid_name_use=SID_NAME_ALIAS;
834 fstrcpy(map->nt_name, grp->gr_name);
835 fstrcpy(map->comment, "Local Unix Group");
837 sid_copy(&map->sid, sid);
839 #endif
841 return True;
844 /* get a builtin group from it's SID */
846 BOOL get_builtin_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
848 struct group *grp;
849 BOOL ret;
852 if(!init_group_mapping()) {
853 DEBUG(0,("failed to initialize group mapping\n"));
854 return(False);
857 become_root();
858 ret = pdb_getgrsid(map, *sid);
859 unbecome_root();
861 if ( !ret )
862 return False;
864 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
865 return False;
868 if (map->gid==-1) {
869 return False;
872 if ( (grp=getgrgid(map->gid)) == NULL) {
873 return False;
876 return True;
881 /****************************************************************************
882 Returns a GROUP_MAP struct based on the gid.
883 ****************************************************************************/
884 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map)
886 struct group *grp;
887 BOOL ret;
889 if(!init_group_mapping()) {
890 DEBUG(0,("failed to initialize group mapping\n"));
891 return(False);
894 if ( (grp=getgrgid(gid)) == NULL)
895 return False;
898 * make a group map from scratch if doesn't exist.
901 become_root();
902 ret = pdb_getgrgid(map, gid);
903 unbecome_root();
905 if ( !ret ) {
906 map->gid=gid;
907 map->sid_name_use=SID_NAME_ALIAS;
909 /* interim solution until we have a last RID allocated */
911 sid_copy(&map->sid, get_global_sam_sid());
912 sid_append_rid(&map->sid, pdb_gid_to_group_rid(gid));
914 fstrcpy(map->nt_name, grp->gr_name);
915 fstrcpy(map->comment, "Local Unix Group");
918 return True;
924 /****************************************************************************
925 Get the member users of a group and
926 all the users who have that group as primary.
928 give back an array of SIDS
929 return the grand number of users
932 TODO: sort the list and remove duplicate. JFM.
934 ****************************************************************************/
936 BOOL get_sid_list_of_group(gid_t gid, DOM_SID **sids, int *num_sids)
938 struct group *grp;
939 int i=0;
940 char *gr;
941 DOM_SID *s;
942 DOM_SID sid;
943 DOM_SID *members;
944 int num_members;
946 struct sys_pwent *userlist;
947 struct sys_pwent *user;
949 if(!init_group_mapping()) {
950 DEBUG(0,("failed to initialize group mapping\n"));
951 return(False);
954 *num_sids = 0;
955 *sids=NULL;
957 if ( (grp=getgrgid(gid)) == NULL)
958 return False;
960 gr = grp->gr_mem[0];
961 DEBUG(10, ("getting members\n"));
963 while (gr && (*gr != (char)'\0')) {
964 SAM_ACCOUNT *group_member_acct = NULL;
965 BOOL found_user;
966 s = Realloc((*sids), sizeof(**sids)*(*num_sids+1));
967 if (!s) {
968 DEBUG(0,("get_uid_list_of_group: unable to enlarge SID list!\n"));
969 return False;
971 else (*sids) = s;
973 if (!NT_STATUS_IS_OK(pdb_init_sam(&group_member_acct))) {
974 continue;
977 become_root();
978 found_user = pdb_getsampwnam(group_member_acct, gr);
979 unbecome_root();
981 if (found_user) {
982 sid_copy(&(*sids)[*num_sids], pdb_get_user_sid(group_member_acct));
983 (*num_sids)++;
986 pdb_free_sam(&group_member_acct);
988 gr = grp->gr_mem[++i];
990 DEBUG(10, ("got [%d] members\n", *num_sids));
992 winbind_off();
994 user = userlist = getpwent_list();
996 while (user != NULL) {
998 SAM_ACCOUNT *group_member_acct = NULL;
999 BOOL found_user;
1001 if (user->pw_gid != gid) {
1002 user = user->next;
1003 continue;
1006 s = Realloc((*sids), sizeof(**sids)*(*num_sids+1));
1007 if (!s) {
1008 DEBUG(0,("get_sid_list_of_group: unable to enlarge "
1009 "SID list!\n"));
1010 pwent_free(userlist);
1011 winbind_on();
1012 return False;
1014 else (*sids) = s;
1016 if (!NT_STATUS_IS_OK(pdb_init_sam(&group_member_acct))) {
1017 continue;
1020 become_root();
1021 found_user = pdb_getsampwnam(group_member_acct, user->pw_name);
1022 unbecome_root();
1024 if (found_user) {
1025 sid_copy(&(*sids)[*num_sids],
1026 pdb_get_user_sid(group_member_acct));
1027 (*num_sids)++;
1028 } else {
1029 DEBUG(4,("get_sid_list_of_group: User %s [uid == %lu] "
1030 "has no samba account\n",
1031 user->pw_name, (unsigned long)user->pw_uid));
1032 if (algorithmic_uid_to_sid(&(*sids)[*num_sids],
1033 user->pw_uid))
1034 (*num_sids)++;
1036 pdb_free_sam(&group_member_acct);
1038 user = user->next;
1040 pwent_free(userlist);
1041 DEBUG(10, ("got primary groups, members: [%d]\n", *num_sids));
1043 winbind_on();
1045 if ( NT_STATUS_IS_OK(gid_to_sid(&sid, gid)) &&
1046 NT_STATUS_IS_OK(enum_aliasmem(&sid, &members, &num_members)) ) {
1048 for (i=0; i<num_members; i++) {
1049 add_sid_to_array(&members[i], sids, num_sids);
1051 if (sids == NULL)
1052 return False;
1056 return True;
1059 /****************************************************************************
1060 Create a UNIX group on demand.
1061 ****************************************************************************/
1063 int smb_create_group(char *unix_group, gid_t *new_gid)
1065 pstring add_script;
1066 int ret = -1;
1067 int fd = 0;
1069 *new_gid = 0;
1071 /* defer to scripts */
1073 if ( *lp_addgroup_script() ) {
1074 pstrcpy(add_script, lp_addgroup_script());
1075 pstring_sub(add_script, "%g", unix_group);
1076 ret = smbrun(add_script, (new_gid!=NULL) ? &fd : NULL);
1077 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
1078 if (ret != 0)
1079 return ret;
1081 if (fd != 0) {
1082 fstring output;
1084 *new_gid = 0;
1085 if (read(fd, output, sizeof(output)) > 0) {
1086 *new_gid = (gid_t)strtoul(output, NULL, 10);
1089 close(fd);
1092 } else if ( winbind_create_group( unix_group, NULL ) ) {
1094 DEBUG(3,("smb_create_group: winbindd created the group (%s)\n",
1095 unix_group));
1096 ret = 0;
1099 if (*new_gid == 0) {
1100 struct group *grp = getgrnam(unix_group);
1102 if (grp != NULL)
1103 *new_gid = grp->gr_gid;
1106 return ret;
1109 /****************************************************************************
1110 Delete a UNIX group on demand.
1111 ****************************************************************************/
1113 int smb_delete_group(char *unix_group)
1115 pstring del_script;
1116 int ret;
1118 /* defer to scripts */
1120 if ( *lp_delgroup_script() ) {
1121 pstrcpy(del_script, lp_delgroup_script());
1122 pstring_sub(del_script, "%g", unix_group);
1123 ret = smbrun(del_script,NULL);
1124 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
1125 return ret;
1128 if ( winbind_delete_group( unix_group ) ) {
1129 DEBUG(3,("smb_delete_group: winbindd deleted the group (%s)\n",
1130 unix_group));
1131 return 0;
1134 return -1;
1137 /****************************************************************************
1138 Set a user's primary UNIX group.
1139 ****************************************************************************/
1140 int smb_set_primary_group(const char *unix_group, const char* unix_user)
1142 pstring add_script;
1143 int ret;
1145 /* defer to scripts */
1147 if ( *lp_setprimarygroup_script() ) {
1148 pstrcpy(add_script, lp_setprimarygroup_script());
1149 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
1150 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
1151 ret = smbrun(add_script,NULL);
1152 DEBUG(3,("smb_set_primary_group: "
1153 "Running the command `%s' gave %d\n",add_script,ret));
1154 return ret;
1157 /* Try winbindd */
1159 if ( winbind_set_user_primary_group( unix_user, unix_group ) ) {
1160 DEBUG(3,("smb_delete_group: winbindd set the group (%s) as the primary group for user (%s)\n",
1161 unix_group, unix_user));
1162 return 0;
1165 return -1;
1168 /****************************************************************************
1169 Add a user to a UNIX group.
1170 ****************************************************************************/
1172 int smb_add_user_group(char *unix_group, char *unix_user)
1174 pstring add_script;
1175 int ret;
1177 /* defer to scripts */
1179 if ( *lp_addusertogroup_script() ) {
1180 pstrcpy(add_script, lp_addusertogroup_script());
1181 pstring_sub(add_script, "%g", unix_group);
1182 pstring_sub(add_script, "%u", unix_user);
1183 ret = smbrun(add_script,NULL);
1184 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1185 return ret;
1188 /* Try winbindd */
1190 if ( winbind_add_user_to_group( unix_user, unix_group ) ) {
1191 DEBUG(3,("smb_delete_group: winbindd added user (%s) to the group (%s)\n",
1192 unix_user, unix_group));
1193 return -1;
1196 return -1;
1199 /****************************************************************************
1200 Delete a user from a UNIX group
1201 ****************************************************************************/
1203 int smb_delete_user_group(const char *unix_group, const char *unix_user)
1205 pstring del_script;
1206 int ret;
1208 /* defer to scripts */
1210 if ( *lp_deluserfromgroup_script() ) {
1211 pstrcpy(del_script, lp_deluserfromgroup_script());
1212 pstring_sub(del_script, "%g", unix_group);
1213 pstring_sub(del_script, "%u", unix_user);
1214 ret = smbrun(del_script,NULL);
1215 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
1216 return ret;
1219 /* Try winbindd */
1221 if ( winbind_remove_user_from_group( unix_user, unix_group ) ) {
1222 DEBUG(3,("smb_delete_group: winbindd removed user (%s) from the group (%s)\n",
1223 unix_user, unix_group));
1224 return 0;
1227 return -1;
1231 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1232 DOM_SID sid)
1234 return get_group_map_from_sid(sid, map) ?
1235 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1238 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1239 gid_t gid)
1241 return get_group_map_from_gid(gid, map) ?
1242 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1245 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1246 const char *name)
1248 return get_group_map_from_ntname(name, map) ?
1249 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1252 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
1253 GROUP_MAP *map)
1255 return add_mapping_entry(map, TDB_INSERT) ?
1256 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1259 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
1260 GROUP_MAP *map)
1262 return add_mapping_entry(map, TDB_REPLACE) ?
1263 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1266 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
1267 DOM_SID sid)
1269 return group_map_remove(&sid) ?
1270 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1273 NTSTATUS pdb_default_find_alias(struct pdb_methods *methods,
1274 const char *name, DOM_SID *sid)
1276 GROUP_MAP map;
1278 if (!get_group_map_from_ntname(name, &map))
1279 return NT_STATUS_NO_SUCH_ALIAS;
1281 if ((map.sid_name_use != SID_NAME_WKN_GRP) &&
1282 (map.sid_name_use != SID_NAME_ALIAS))
1283 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1285 sid_copy(sid, &map.sid);
1286 return NT_STATUS_OK;
1289 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
1290 const char *name, uint32 *rid)
1292 DOM_SID sid;
1293 enum SID_NAME_USE type;
1294 uint32 new_rid;
1295 gid_t gid;
1297 if (lookup_name(get_global_sam_name(), name, &sid, &type))
1298 return NT_STATUS_ALIAS_EXISTS;
1300 if (!winbind_allocate_rid(&new_rid))
1301 return NT_STATUS_ACCESS_DENIED;
1303 sid_copy(&sid, get_global_sam_sid());
1304 sid_append_rid(&sid, new_rid);
1306 /* Here we allocate the gid */
1307 if (!winbind_sid_to_gid(&gid, &sid)) {
1308 DEBUG(0, ("Could not get gid for new RID\n"));
1309 return NT_STATUS_ACCESS_DENIED;
1312 if (!add_initial_entry(gid, sid_string_static(&sid), SID_NAME_ALIAS,
1313 name, "")) {
1314 DEBUG(0, ("Could not add group mapping entry for alias %s\n",
1315 name));
1316 return NT_STATUS_ACCESS_DENIED;
1319 *rid = new_rid;
1321 return NT_STATUS_OK;
1324 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
1325 const DOM_SID *sid)
1327 if (!group_map_remove(sid))
1328 return NT_STATUS_ACCESS_DENIED;
1330 return NT_STATUS_OK;
1333 NTSTATUS pdb_default_enum_aliases(struct pdb_methods *methods,
1334 const DOM_SID *sid,
1335 uint32 start_idx, uint32 max_entries,
1336 uint32 *num_aliases,
1337 struct acct_info **info)
1339 extern DOM_SID global_sid_Builtin;
1341 GROUP_MAP *map;
1342 int i, num_maps;
1343 enum SID_NAME_USE type = SID_NAME_UNKNOWN;
1345 if (sid_compare(sid, get_global_sam_sid()) == 0)
1346 type = SID_NAME_ALIAS;
1348 if (sid_compare(sid, &global_sid_Builtin) == 0)
1349 type = SID_NAME_WKN_GRP;
1351 if (!enum_group_mapping(type, &map, &num_maps, False) ||
1352 (num_maps == 0)) {
1353 *num_aliases = 0;
1354 *info = NULL;
1355 goto done;
1358 if (start_idx > num_maps) {
1359 *num_aliases = 0;
1360 *info = NULL;
1361 goto done;
1364 *num_aliases = num_maps - start_idx;
1366 if (*num_aliases > max_entries)
1367 *num_aliases = max_entries;
1369 *info = malloc(sizeof(struct acct_info) * (*num_aliases));
1371 for (i=0; i<*num_aliases; i++) {
1372 fstrcpy((*info)[i].acct_name, map[i+start_idx].nt_name);
1373 fstrcpy((*info)[i].acct_desc, map[i+start_idx].comment);
1374 sid_peek_rid(&map[i].sid, &(*info)[i+start_idx].rid);
1377 done:
1378 SAFE_FREE(map);
1379 return NT_STATUS_OK;
1382 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
1383 const DOM_SID *sid,
1384 struct acct_info *info)
1386 GROUP_MAP map;
1388 if (!get_group_map_from_sid(*sid, &map))
1389 return NT_STATUS_NO_SUCH_ALIAS;
1391 fstrcpy(info->acct_name, map.nt_name);
1392 fstrcpy(info->acct_desc, map.comment);
1393 sid_peek_rid(&map.sid, &info->rid);
1394 return NT_STATUS_OK;
1397 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
1398 const DOM_SID *sid,
1399 struct acct_info *info)
1401 GROUP_MAP map;
1403 if (!get_group_map_from_sid(*sid, &map))
1404 return NT_STATUS_NO_SUCH_ALIAS;
1406 fstrcpy(map.comment, info->acct_desc);
1408 if (!add_mapping_entry(&map, TDB_REPLACE))
1409 return NT_STATUS_ACCESS_DENIED;
1411 return NT_STATUS_OK;
1414 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
1415 enum SID_NAME_USE sid_name_use,
1416 GROUP_MAP **rmap, int *num_entries,
1417 BOOL unix_only)
1419 return enum_group_mapping(sid_name_use, rmap, num_entries, unix_only) ?
1420 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1423 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
1424 const DOM_SID *alias, const DOM_SID *member)
1426 return add_aliasmem(alias, member);
1429 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
1430 const DOM_SID *alias, const DOM_SID *member)
1432 return del_aliasmem(alias, member);
1435 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
1436 const DOM_SID *alias, DOM_SID **members,
1437 int *num_members)
1439 return enum_aliasmem(alias, members, num_members);
1442 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
1443 const DOM_SID *sid,
1444 DOM_SID **aliases, int *num)
1446 return alias_memberships(sid, aliases, num);
1449 /**********************************************************************
1450 no ops for passdb backends that don't implement group mapping
1451 *********************************************************************/
1453 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1454 DOM_SID sid)
1456 return NT_STATUS_UNSUCCESSFUL;
1459 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1460 gid_t gid)
1462 return NT_STATUS_UNSUCCESSFUL;
1465 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1466 const char *name)
1468 return NT_STATUS_UNSUCCESSFUL;
1471 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
1472 GROUP_MAP *map)
1474 return NT_STATUS_UNSUCCESSFUL;
1477 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
1478 GROUP_MAP *map)
1480 return NT_STATUS_UNSUCCESSFUL;
1483 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
1484 DOM_SID sid)
1486 return NT_STATUS_UNSUCCESSFUL;
1489 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
1490 enum SID_NAME_USE sid_name_use,
1491 GROUP_MAP **rmap, int *num_entries,
1492 BOOL unix_only)
1494 return NT_STATUS_UNSUCCESSFUL;