r4231: commiting changes to 3.0.10
[Samba.git] / source / groupdb / mapping.c
blob9a34e02f6752b1ce1c2eccda9d792233141d98cd
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/"
31 /* Alias memberships are stored reverse, as memberships. The performance
32 * critical operation is to determine the aliases a SID is member of, not
33 * listing alias members. So we store a list of alias SIDs a SID is member of
34 * hanging of the member as key.
36 #define MEMBEROF_PREFIX "MEMBEROF/"
38 PRIVS privs[] = {
39 {SE_PRIV_NONE, "no_privs", "No privilege" }, /* this one MUST be first */
40 {SE_PRIV_ADD_MACHINES, "SeMachineAccountPrivilege", "Add workstations to the domain" },
41 {SE_PRIV_SEC_PRIV, "SeSecurityPrivilege", "Manage the audit logs" },
42 {SE_PRIV_TAKE_OWNER, "SeTakeOwnershipPrivilege", "Take ownership of file" },
43 {SE_PRIV_ADD_USERS, "SaAddUsers", "Add users to the domain - Samba" },
44 {SE_PRIV_PRINT_OPERATOR, "SaPrintOp", "Add or remove printers - Samba" },
45 {SE_PRIV_ALL, "SaAllPrivs", "all privileges" }
49 /****************************************************************************
50 dump the mapping group mapping to a text file
51 ****************************************************************************/
52 char *decode_sid_name_use(fstring group, enum SID_NAME_USE name_use)
54 static fstring group_type;
56 switch(name_use) {
57 case SID_NAME_USER:
58 fstrcpy(group_type,"User");
59 break;
60 case SID_NAME_DOM_GRP:
61 fstrcpy(group_type,"Domain group");
62 break;
63 case SID_NAME_DOMAIN:
64 fstrcpy(group_type,"Domain");
65 break;
66 case SID_NAME_ALIAS:
67 fstrcpy(group_type,"Local group");
68 break;
69 case SID_NAME_WKN_GRP:
70 fstrcpy(group_type,"Builtin group");
71 break;
72 case SID_NAME_DELETED:
73 fstrcpy(group_type,"Deleted");
74 break;
75 case SID_NAME_INVALID:
76 fstrcpy(group_type,"Invalid");
77 break;
78 case SID_NAME_UNKNOWN:
79 default:
80 fstrcpy(group_type,"Unknown type");
81 break;
84 fstrcpy(group, group_type);
85 return group_type;
88 /****************************************************************************
89 initialise first time the mapping list - called from init_group_mapping()
90 ****************************************************************************/
91 static BOOL default_group_mapping(void)
93 DOM_SID sid_admins;
94 DOM_SID sid_users;
95 DOM_SID sid_guests;
96 fstring str_admins;
97 fstring str_users;
98 fstring str_guests;
100 /* Add the Wellknown groups */
102 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP, "Administrators", "");
103 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP, "Users", "");
104 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP, "Guests", "");
105 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP, "Power Users", "");
106 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP, "Account Operators", "");
107 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP, "System Operators", "");
108 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP, "Print Operators", "");
109 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP, "Backup Operators", "");
110 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP, "Replicators", "");
112 /* Add the defaults domain groups */
114 sid_copy(&sid_admins, get_global_sam_sid());
115 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
116 sid_to_string(str_admins, &sid_admins);
117 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "");
119 sid_copy(&sid_users, get_global_sam_sid());
120 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
121 sid_to_string(str_users, &sid_users);
122 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "");
124 sid_copy(&sid_guests, get_global_sam_sid());
125 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
126 sid_to_string(str_guests, &sid_guests);
127 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "");
129 return True;
132 /****************************************************************************
133 Open the group mapping tdb.
134 ****************************************************************************/
136 static BOOL init_group_mapping(void)
138 const char *vstring = "INFO/version";
139 int32 vers_id;
141 if (tdb)
142 return True;
143 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
144 if (!tdb) {
145 DEBUG(0,("Failed to open group mapping database\n"));
146 return False;
149 /* handle a Samba upgrade */
150 tdb_lock_bystring(tdb, vstring, 0);
152 /* Cope with byte-reversed older versions of the db. */
153 vers_id = tdb_fetch_int32(tdb, vstring);
154 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
155 /* Written on a bigendian machine with old fetch_int code. Save as le. */
156 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
157 vers_id = DATABASE_VERSION_V2;
160 if (vers_id != DATABASE_VERSION_V2) {
161 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
162 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
165 tdb_unlock_bystring(tdb, vstring);
167 /* write a list of default groups */
168 if(!default_group_mapping())
169 return False;
171 return True;
174 /****************************************************************************
175 ****************************************************************************/
176 static BOOL add_mapping_entry(GROUP_MAP *map, int flag)
178 TDB_DATA kbuf, dbuf;
179 pstring key, buf;
180 fstring string_sid="";
181 int len;
183 if(!init_group_mapping()) {
184 DEBUG(0,("failed to initialize group mapping\n"));
185 return(False);
188 sid_to_string(string_sid, &map->sid);
190 len = tdb_pack(buf, sizeof(buf), "ddff",
191 map->gid, map->sid_name_use, map->nt_name, map->comment);
193 if (len > sizeof(buf))
194 return False;
196 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
198 kbuf.dsize = strlen(key)+1;
199 kbuf.dptr = key;
200 dbuf.dsize = len;
201 dbuf.dptr = buf;
202 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
204 return True;
207 /****************************************************************************
208 initialise first time the mapping list
209 ****************************************************************************/
210 BOOL add_initial_entry(gid_t gid, const char *sid, enum SID_NAME_USE sid_name_use, const char *nt_name, const char *comment)
212 GROUP_MAP map;
214 if(!init_group_mapping()) {
215 DEBUG(0,("failed to initialize group mapping\n"));
216 return(False);
219 map.gid=gid;
220 if (!string_to_sid(&map.sid, sid)) {
221 DEBUG(0, ("string_to_sid failed: %s", sid));
222 return False;
225 map.sid_name_use=sid_name_use;
226 fstrcpy(map.nt_name, nt_name);
227 fstrcpy(map.comment, comment);
229 return pdb_add_group_mapping_entry(&map);
232 /****************************************************************************
233 Return the sid and the type of the unix group.
234 ****************************************************************************/
236 static BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
238 TDB_DATA kbuf, dbuf;
239 pstring key;
240 fstring string_sid;
241 int ret = 0;
243 if(!init_group_mapping()) {
244 DEBUG(0,("failed to initialize group mapping\n"));
245 return(False);
248 /* the key is the SID, retrieving is direct */
250 sid_to_string(string_sid, &sid);
251 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
253 kbuf.dptr = key;
254 kbuf.dsize = strlen(key)+1;
256 dbuf = tdb_fetch(tdb, kbuf);
257 if (!dbuf.dptr)
258 return False;
260 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
261 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
263 SAFE_FREE(dbuf.dptr);
265 if ( ret == -1 ) {
266 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
267 return False;
270 sid_copy(&map->sid, &sid);
272 return True;
275 /****************************************************************************
276 Return the sid and the type of the unix group.
277 ****************************************************************************/
279 static BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
281 TDB_DATA kbuf, dbuf, newkey;
282 fstring string_sid;
283 int ret;
285 if(!init_group_mapping()) {
286 DEBUG(0,("failed to initialize group mapping\n"));
287 return(False);
290 /* we need to enumerate the TDB to find the GID */
292 for (kbuf = tdb_firstkey(tdb);
293 kbuf.dptr;
294 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
296 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
298 dbuf = tdb_fetch(tdb, kbuf);
299 if (!dbuf.dptr)
300 continue;
302 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
304 string_to_sid(&map->sid, string_sid);
306 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
307 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
309 SAFE_FREE(dbuf.dptr);
311 if ( ret == -1 ) {
312 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
313 return False;
316 if (gid==map->gid) {
317 SAFE_FREE(kbuf.dptr);
318 return True;
322 return False;
325 /****************************************************************************
326 Return the sid and the type of the unix group.
327 ****************************************************************************/
329 static BOOL get_group_map_from_ntname(const char *name, GROUP_MAP *map)
331 TDB_DATA kbuf, dbuf, newkey;
332 fstring string_sid;
333 int ret;
335 if(!init_group_mapping()) {
336 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping\n"));
337 return(False);
340 /* we need to enumerate the TDB to find the name */
342 for (kbuf = tdb_firstkey(tdb);
343 kbuf.dptr;
344 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
346 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
348 dbuf = tdb_fetch(tdb, kbuf);
349 if (!dbuf.dptr)
350 continue;
352 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
354 string_to_sid(&map->sid, string_sid);
356 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
357 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
359 SAFE_FREE(dbuf.dptr);
361 if ( ret == -1 ) {
362 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
363 return False;
366 if (StrCaseCmp(name, map->nt_name)==0) {
367 SAFE_FREE(kbuf.dptr);
368 return True;
372 return False;
375 /****************************************************************************
376 Remove a group mapping entry.
377 ****************************************************************************/
379 static BOOL group_map_remove(DOM_SID sid)
381 TDB_DATA kbuf, dbuf;
382 pstring key;
383 fstring string_sid;
385 if(!init_group_mapping()) {
386 DEBUG(0,("failed to initialize group mapping\n"));
387 return(False);
390 /* the key is the SID, retrieving is direct */
392 sid_to_string(string_sid, &sid);
393 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
395 kbuf.dptr = key;
396 kbuf.dsize = strlen(key)+1;
398 dbuf = tdb_fetch(tdb, kbuf);
399 if (!dbuf.dptr)
400 return False;
402 SAFE_FREE(dbuf.dptr);
404 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
405 return False;
407 return True;
410 /****************************************************************************
411 Enumerate the group mapping.
412 ****************************************************************************/
414 static BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
415 int *num_entries, BOOL unix_only)
417 TDB_DATA kbuf, dbuf, newkey;
418 fstring string_sid;
419 fstring group_type;
420 GROUP_MAP map;
421 GROUP_MAP *mapt;
422 int ret;
423 int entries=0;
425 if(!init_group_mapping()) {
426 DEBUG(0,("failed to initialize group mapping\n"));
427 return(False);
430 *num_entries=0;
431 *rmap=NULL;
433 for (kbuf = tdb_firstkey(tdb);
434 kbuf.dptr;
435 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
437 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
438 continue;
440 dbuf = tdb_fetch(tdb, kbuf);
441 if (!dbuf.dptr)
442 continue;
444 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
446 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
447 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
449 SAFE_FREE(dbuf.dptr);
451 if ( ret == -1 ) {
452 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
453 continue;
456 /* list only the type or everything if UNKNOWN */
457 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
458 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
459 continue;
462 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
463 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
464 continue;
467 string_to_sid(&map.sid, string_sid);
469 decode_sid_name_use(group_type, map.sid_name_use);
470 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map.nt_name ,group_type));
472 mapt= SMB_REALLOC_ARRAY((*rmap), GROUP_MAP, entries+1);
473 if (!mapt) {
474 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
475 SAFE_FREE(*rmap);
476 return False;
478 else
479 (*rmap) = mapt;
481 mapt[entries].gid = map.gid;
482 sid_copy( &mapt[entries].sid, &map.sid);
483 mapt[entries].sid_name_use = map.sid_name_use;
484 fstrcpy(mapt[entries].nt_name, map.nt_name);
485 fstrcpy(mapt[entries].comment, map.comment);
487 entries++;
491 *num_entries=entries;
493 return True;
496 /* This operation happens on session setup, so it should better be fast. We
497 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
499 static NTSTATUS alias_memberships(const DOM_SID *sid, DOM_SID **sids, int *num)
501 fstring key, string_sid;
502 TDB_DATA kbuf, dbuf;
503 const char *p;
505 *num = 0;
506 *sids = NULL;
508 if (!init_group_mapping()) {
509 DEBUG(0,("failed to initialize group mapping\n"));
510 return NT_STATUS_ACCESS_DENIED;
513 sid_to_string(string_sid, sid);
514 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
516 kbuf.dsize = strlen(key)+1;
517 kbuf.dptr = key;
519 dbuf = tdb_fetch(tdb, kbuf);
521 if (dbuf.dptr == NULL) {
522 return NT_STATUS_OK;
525 p = dbuf.dptr;
527 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
529 DOM_SID alias;
531 if (!string_to_sid(&alias, string_sid))
532 continue;
534 add_sid_to_array(&alias, sids, num);
536 if (sids == NULL)
537 return NT_STATUS_NO_MEMORY;
540 SAFE_FREE(dbuf.dptr);
541 return NT_STATUS_OK;
544 static BOOL is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
546 DOM_SID *sids;
547 int i, num;
549 /* This feels the wrong way round, but the on-disk data structure
550 * dictates it this way. */
551 if (!NT_STATUS_IS_OK(alias_memberships(member, &sids, &num)))
552 return False;
554 for (i=0; i<num; i++) {
555 if (sid_compare(alias, &sids[i]) == 0) {
556 SAFE_FREE(sids);
557 return True;
560 SAFE_FREE(sids);
561 return False;
564 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
566 GROUP_MAP map;
567 TDB_DATA kbuf, dbuf;
568 pstring key;
569 fstring string_sid;
570 char *new_memberstring;
571 int result;
573 if(!init_group_mapping()) {
574 DEBUG(0,("failed to initialize group mapping\n"));
575 return NT_STATUS_ACCESS_DENIED;
578 if (!get_group_map_from_sid(*alias, &map))
579 return NT_STATUS_NO_SUCH_ALIAS;
581 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
582 (map.sid_name_use != SID_NAME_WKN_GRP) )
583 return NT_STATUS_NO_SUCH_ALIAS;
585 if (is_aliasmem(alias, member))
586 return NT_STATUS_MEMBER_IN_ALIAS;
588 sid_to_string(string_sid, member);
589 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
591 kbuf.dsize = strlen(key)+1;
592 kbuf.dptr = key;
594 dbuf = tdb_fetch(tdb, kbuf);
596 sid_to_string(string_sid, alias);
598 if (dbuf.dptr != NULL) {
599 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
600 string_sid);
601 } else {
602 new_memberstring = SMB_STRDUP(string_sid);
605 if (new_memberstring == NULL)
606 return NT_STATUS_NO_MEMORY;
608 SAFE_FREE(dbuf.dptr);
609 dbuf.dsize = strlen(new_memberstring)+1;
610 dbuf.dptr = new_memberstring;
612 result = tdb_store(tdb, kbuf, dbuf, 0);
614 SAFE_FREE(new_memberstring);
616 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
619 struct aliasmem_closure {
620 const DOM_SID *alias;
621 DOM_SID **sids;
622 int *num;
625 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
626 void *state)
628 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
629 const char *p;
630 fstring alias_string;
632 if (strncmp(key.dptr, MEMBEROF_PREFIX,
633 strlen(MEMBEROF_PREFIX)) != 0)
634 return 0;
636 p = data.dptr;
638 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
640 DOM_SID alias, member;
641 const char *member_string;
644 if (!string_to_sid(&alias, alias_string))
645 continue;
647 if (sid_compare(closure->alias, &alias) != 0)
648 continue;
650 /* Ok, we found the alias we're looking for in the membership
651 * list currently scanned. The key represents the alias
652 * member. Add that. */
654 member_string = strchr(key.dptr, '/');
656 /* Above we tested for MEMBEROF_PREFIX which includes the
657 * slash. */
659 SMB_ASSERT(member_string != NULL);
660 member_string += 1;
662 if (!string_to_sid(&member, member_string))
663 continue;
665 add_sid_to_array(&member, closure->sids, closure->num);
668 return 0;
671 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, int *num)
673 GROUP_MAP map;
674 struct aliasmem_closure closure;
676 if(!init_group_mapping()) {
677 DEBUG(0,("failed to initialize group mapping\n"));
678 return NT_STATUS_ACCESS_DENIED;
681 if (!get_group_map_from_sid(*alias, &map))
682 return NT_STATUS_NO_SUCH_ALIAS;
684 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
685 (map.sid_name_use != SID_NAME_WKN_GRP) )
686 return NT_STATUS_NO_SUCH_ALIAS;
688 *sids = NULL;
689 *num = 0;
691 closure.alias = alias;
692 closure.sids = sids;
693 closure.num = num;
695 tdb_traverse(tdb, collect_aliasmem, &closure);
696 return NT_STATUS_OK;
699 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
701 NTSTATUS result;
702 DOM_SID *sids;
703 int i, num;
704 BOOL found = False;
705 char *member_string;
706 TDB_DATA kbuf, dbuf;
707 pstring key;
708 fstring sid_string;
710 result = alias_memberships(member, &sids, &num);
712 if (!NT_STATUS_IS_OK(result))
713 return result;
715 for (i=0; i<num; i++) {
716 if (sid_compare(&sids[i], alias) == 0) {
717 found = True;
718 break;
722 if (!found) {
723 SAFE_FREE(sids);
724 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
727 if (i < num)
728 sids[i] = sids[num-1];
730 num -= 1;
732 sid_to_string(sid_string, member);
733 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, sid_string);
735 kbuf.dsize = strlen(key)+1;
736 kbuf.dptr = key;
738 if (num == 0)
739 return tdb_delete(tdb, kbuf) == 0 ?
740 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
742 member_string = SMB_STRDUP("");
744 if (member_string == NULL) {
745 SAFE_FREE(sids);
746 return NT_STATUS_NO_MEMORY;
749 for (i=0; i<num; i++) {
750 char *s = member_string;
752 sid_to_string(sid_string, &sids[i]);
753 asprintf(&member_string, "%s %s", s, sid_string);
755 SAFE_FREE(s);
756 if (member_string == NULL) {
757 SAFE_FREE(sids);
758 return NT_STATUS_NO_MEMORY;
762 dbuf.dsize = strlen(member_string)+1;
763 dbuf.dptr = member_string;
765 result = tdb_store(tdb, kbuf, dbuf, 0) == 0 ?
766 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
768 SAFE_FREE(sids);
769 SAFE_FREE(member_string);
771 return result;
776 * High level functions
777 * better to use them than the lower ones.
779 * we are checking if the group is in the mapping file
780 * and if the group is an existing unix group
784 /* get a domain group from it's SID */
786 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
788 struct group *grp;
789 BOOL ret;
791 if(!init_group_mapping()) {
792 DEBUG(0,("failed to initialize group mapping\n"));
793 return(False);
796 DEBUG(10, ("get_domain_group_from_sid\n"));
798 /* if the group is NOT in the database, it CAN NOT be a domain group */
800 become_root();
801 ret = pdb_getgrsid(map, sid);
802 unbecome_root();
804 if ( !ret )
805 return False;
807 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
809 /* if it's not a domain group, continue */
810 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
811 return False;
814 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
816 if (map->gid==-1) {
817 return False;
820 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
822 grp = getgrgid(map->gid);
823 if ( !grp ) {
824 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
825 return False;
828 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
830 return True;
834 /* get a local (alias) group from it's SID */
836 BOOL get_local_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
838 BOOL ret;
840 if(!init_group_mapping()) {
841 DEBUG(0,("failed to initialize group mapping\n"));
842 return(False);
845 /* The group is in the mapping table */
846 become_root();
847 ret = pdb_getgrsid(map, *sid);
848 unbecome_root();
850 if ( !ret )
851 return False;
853 if ( ( (map->sid_name_use != SID_NAME_ALIAS) &&
854 (map->sid_name_use != SID_NAME_WKN_GRP) )
855 || (map->gid == -1)
856 || (getgrgid(map->gid) == NULL) )
858 return False;
861 #if 1 /* JERRY */
862 /* local groups only exist in the group mapping DB so this
863 is not necessary */
865 else {
866 /* the group isn't in the mapping table.
867 * make one based on the unix information */
868 uint32 alias_rid;
869 struct group *grp;
871 sid_peek_rid(sid, &alias_rid);
872 map->gid=pdb_group_rid_to_gid(alias_rid);
874 grp = getgrgid(map->gid);
875 if ( !grp ) {
876 DEBUG(3,("get_local_group_from_sid: No unix group for [%ul]\n", map->gid));
877 return False;
880 map->sid_name_use=SID_NAME_ALIAS;
882 fstrcpy(map->nt_name, grp->gr_name);
883 fstrcpy(map->comment, "Local Unix Group");
885 sid_copy(&map->sid, sid);
887 #endif
889 return True;
892 /* get a builtin group from it's SID */
894 BOOL get_builtin_group_from_sid(DOM_SID *sid, GROUP_MAP *map)
896 struct group *grp;
897 BOOL ret;
900 if(!init_group_mapping()) {
901 DEBUG(0,("failed to initialize group mapping\n"));
902 return(False);
905 become_root();
906 ret = pdb_getgrsid(map, *sid);
907 unbecome_root();
909 if ( !ret )
910 return False;
912 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
913 return False;
916 if (map->gid==-1) {
917 return False;
920 if ( (grp=getgrgid(map->gid)) == NULL) {
921 return False;
924 return True;
929 /****************************************************************************
930 Returns a GROUP_MAP struct based on the gid.
931 ****************************************************************************/
932 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map)
934 struct group *grp;
935 BOOL ret;
937 if(!init_group_mapping()) {
938 DEBUG(0,("failed to initialize group mapping\n"));
939 return(False);
942 if ( (grp=getgrgid(gid)) == NULL)
943 return False;
945 become_root();
946 ret = pdb_getgrgid(map, gid);
947 unbecome_root();
949 if ( !ret ) {
950 return False;
953 return True;
957 /****************************************************************************
958 Create a UNIX group on demand.
959 ****************************************************************************/
961 int smb_create_group(char *unix_group, gid_t *new_gid)
963 pstring add_script;
964 int ret = -1;
965 int fd = 0;
967 *new_gid = 0;
969 /* defer to scripts */
971 if ( *lp_addgroup_script() ) {
972 pstrcpy(add_script, lp_addgroup_script());
973 pstring_sub(add_script, "%g", unix_group);
974 ret = smbrun(add_script, (new_gid!=NULL) ? &fd : NULL);
975 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
976 if (ret != 0)
977 return ret;
979 if (fd != 0) {
980 fstring output;
982 *new_gid = 0;
983 if (read(fd, output, sizeof(output)) > 0) {
984 *new_gid = (gid_t)strtoul(output, NULL, 10);
987 close(fd);
990 } else if ( winbind_create_group( unix_group, NULL ) ) {
992 DEBUG(3,("smb_create_group: winbindd created the group (%s)\n",
993 unix_group));
994 ret = 0;
997 if (*new_gid == 0) {
998 struct group *grp = getgrnam(unix_group);
1000 if (grp != NULL)
1001 *new_gid = grp->gr_gid;
1004 return ret;
1007 /****************************************************************************
1008 Delete a UNIX group on demand.
1009 ****************************************************************************/
1011 int smb_delete_group(char *unix_group)
1013 pstring del_script;
1014 int ret;
1016 /* defer to scripts */
1018 if ( *lp_delgroup_script() ) {
1019 pstrcpy(del_script, lp_delgroup_script());
1020 pstring_sub(del_script, "%g", unix_group);
1021 ret = smbrun(del_script,NULL);
1022 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
1023 return ret;
1026 if ( winbind_delete_group( unix_group ) ) {
1027 DEBUG(3,("smb_delete_group: winbindd deleted the group (%s)\n",
1028 unix_group));
1029 return 0;
1032 return -1;
1035 /****************************************************************************
1036 Set a user's primary UNIX group.
1037 ****************************************************************************/
1038 int smb_set_primary_group(const char *unix_group, const char* unix_user)
1040 pstring add_script;
1041 int ret;
1043 /* defer to scripts */
1045 if ( *lp_setprimarygroup_script() ) {
1046 pstrcpy(add_script, lp_setprimarygroup_script());
1047 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
1048 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
1049 ret = smbrun(add_script,NULL);
1050 DEBUG(3,("smb_set_primary_group: "
1051 "Running the command `%s' gave %d\n",add_script,ret));
1052 return ret;
1055 /* Try winbindd */
1057 if ( winbind_set_user_primary_group( unix_user, unix_group ) ) {
1058 DEBUG(3,("smb_delete_group: winbindd set the group (%s) as the primary group for user (%s)\n",
1059 unix_group, unix_user));
1060 return 0;
1063 return -1;
1066 /****************************************************************************
1067 Add a user to a UNIX group.
1068 ****************************************************************************/
1070 int smb_add_user_group(char *unix_group, char *unix_user)
1072 pstring add_script;
1073 int ret;
1075 /* defer to scripts */
1077 if ( *lp_addusertogroup_script() ) {
1078 pstrcpy(add_script, lp_addusertogroup_script());
1079 pstring_sub(add_script, "%g", unix_group);
1080 pstring_sub(add_script, "%u", unix_user);
1081 ret = smbrun(add_script,NULL);
1082 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1083 return ret;
1086 /* Try winbindd */
1088 if ( winbind_add_user_to_group( unix_user, unix_group ) ) {
1089 DEBUG(3,("smb_delete_group: winbindd added user (%s) to the group (%s)\n",
1090 unix_user, unix_group));
1091 return -1;
1094 return -1;
1097 /****************************************************************************
1098 Delete a user from a UNIX group
1099 ****************************************************************************/
1101 int smb_delete_user_group(const char *unix_group, const char *unix_user)
1103 pstring del_script;
1104 int ret;
1106 /* defer to scripts */
1108 if ( *lp_deluserfromgroup_script() ) {
1109 pstrcpy(del_script, lp_deluserfromgroup_script());
1110 pstring_sub(del_script, "%g", unix_group);
1111 pstring_sub(del_script, "%u", unix_user);
1112 ret = smbrun(del_script,NULL);
1113 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
1114 return ret;
1117 /* Try winbindd */
1119 if ( winbind_remove_user_from_group( unix_user, unix_group ) ) {
1120 DEBUG(3,("smb_delete_group: winbindd removed user (%s) from the group (%s)\n",
1121 unix_user, unix_group));
1122 return 0;
1125 return -1;
1129 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1130 DOM_SID sid)
1132 return get_group_map_from_sid(sid, map) ?
1133 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1136 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1137 gid_t gid)
1139 return get_group_map_from_gid(gid, map) ?
1140 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1143 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1144 const char *name)
1146 return get_group_map_from_ntname(name, map) ?
1147 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1150 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
1151 GROUP_MAP *map)
1153 return add_mapping_entry(map, TDB_INSERT) ?
1154 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1157 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
1158 GROUP_MAP *map)
1160 return add_mapping_entry(map, TDB_REPLACE) ?
1161 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1164 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
1165 DOM_SID sid)
1167 return group_map_remove(sid) ?
1168 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1171 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
1172 enum SID_NAME_USE sid_name_use,
1173 GROUP_MAP **rmap, int *num_entries,
1174 BOOL unix_only)
1176 return enum_group_mapping(sid_name_use, rmap, num_entries, unix_only) ?
1177 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1180 NTSTATUS pdb_default_find_alias(struct pdb_methods *methods,
1181 const char *name, DOM_SID *sid)
1183 GROUP_MAP map;
1185 if (!pdb_getgrnam(&map, name))
1186 return NT_STATUS_NO_SUCH_ALIAS;
1188 if ((map.sid_name_use != SID_NAME_WKN_GRP) &&
1189 (map.sid_name_use != SID_NAME_ALIAS))
1190 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1192 sid_copy(sid, &map.sid);
1193 return NT_STATUS_OK;
1196 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
1197 const char *name, uint32 *rid)
1199 DOM_SID sid;
1200 enum SID_NAME_USE type;
1201 uint32 new_rid;
1202 gid_t gid;
1204 GROUP_MAP map;
1206 if (lookup_name(get_global_sam_name(), name, &sid, &type))
1207 return NT_STATUS_ALIAS_EXISTS;
1209 if (!winbind_allocate_rid(&new_rid))
1210 return NT_STATUS_ACCESS_DENIED;
1212 sid_copy(&sid, get_global_sam_sid());
1213 sid_append_rid(&sid, new_rid);
1215 /* Here we allocate the gid */
1216 if (!winbind_sid_to_gid(&gid, &sid)) {
1217 DEBUG(0, ("Could not get gid for new RID\n"));
1218 return NT_STATUS_ACCESS_DENIED;
1221 map.gid = gid;
1222 sid_copy(&map.sid, &sid);
1223 map.sid_name_use = SID_NAME_ALIAS;
1224 fstrcpy(map.nt_name, name);
1225 fstrcpy(map.comment, "");
1227 if (!pdb_add_group_mapping_entry(&map)) {
1228 DEBUG(0, ("Could not add group mapping entry for alias %s\n",
1229 name));
1230 return NT_STATUS_ACCESS_DENIED;
1233 *rid = new_rid;
1235 return NT_STATUS_OK;
1238 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
1239 const DOM_SID *sid)
1241 return pdb_delete_group_mapping_entry(*sid) ?
1242 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1245 NTSTATUS pdb_default_enum_aliases(struct pdb_methods *methods,
1246 const DOM_SID *sid,
1247 uint32 start_idx, uint32 max_entries,
1248 uint32 *num_aliases,
1249 struct acct_info **info)
1251 extern DOM_SID global_sid_Builtin;
1253 GROUP_MAP *map;
1254 int i, num_maps;
1255 enum SID_NAME_USE type = SID_NAME_UNKNOWN;
1257 if (sid_compare(sid, get_global_sam_sid()) == 0)
1258 type = SID_NAME_ALIAS;
1260 if (sid_compare(sid, &global_sid_Builtin) == 0)
1261 type = SID_NAME_WKN_GRP;
1263 if (!pdb_enum_group_mapping(type, &map, &num_maps, False) ||
1264 (num_maps == 0)) {
1265 *num_aliases = 0;
1266 *info = NULL;
1267 goto done;
1270 if (start_idx > num_maps) {
1271 *num_aliases = 0;
1272 *info = NULL;
1273 goto done;
1276 *num_aliases = num_maps - start_idx;
1278 if (*num_aliases > max_entries)
1279 *num_aliases = max_entries;
1281 *info = SMB_MALLOC_ARRAY(struct acct_info, *num_aliases);
1283 for (i=0; i<*num_aliases; i++) {
1284 fstrcpy((*info)[i].acct_name, map[i+start_idx].nt_name);
1285 fstrcpy((*info)[i].acct_desc, map[i+start_idx].comment);
1286 sid_peek_rid(&map[i].sid, &(*info)[i+start_idx].rid);
1289 done:
1290 SAFE_FREE(map);
1291 return NT_STATUS_OK;
1294 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
1295 const DOM_SID *sid,
1296 struct acct_info *info)
1298 GROUP_MAP map;
1300 if (!pdb_getgrsid(&map, *sid))
1301 return NT_STATUS_NO_SUCH_ALIAS;
1303 fstrcpy(info->acct_name, map.nt_name);
1304 fstrcpy(info->acct_desc, map.comment);
1305 sid_peek_rid(&map.sid, &info->rid);
1306 return NT_STATUS_OK;
1309 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
1310 const DOM_SID *sid,
1311 struct acct_info *info)
1313 GROUP_MAP map;
1315 if (!pdb_getgrsid(&map, *sid))
1316 return NT_STATUS_NO_SUCH_ALIAS;
1318 fstrcpy(map.comment, info->acct_desc);
1320 if (!pdb_update_group_mapping_entry(&map))
1321 return NT_STATUS_ACCESS_DENIED;
1323 return NT_STATUS_OK;
1326 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
1327 const DOM_SID *alias, const DOM_SID *member)
1329 return add_aliasmem(alias, member);
1332 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
1333 const DOM_SID *alias, const DOM_SID *member)
1335 return del_aliasmem(alias, member);
1338 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
1339 const DOM_SID *alias, DOM_SID **members,
1340 int *num_members)
1342 return enum_aliasmem(alias, members, num_members);
1345 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
1346 const DOM_SID *sid,
1347 DOM_SID **aliases, int *num)
1349 return alias_memberships(sid, aliases, num);
1352 /**********************************************************************
1353 no ops for passdb backends that don't implement group mapping
1354 *********************************************************************/
1356 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
1357 DOM_SID sid)
1359 return NT_STATUS_UNSUCCESSFUL;
1362 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
1363 gid_t gid)
1365 return NT_STATUS_UNSUCCESSFUL;
1368 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
1369 const char *name)
1371 return NT_STATUS_UNSUCCESSFUL;
1374 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
1375 GROUP_MAP *map)
1377 return NT_STATUS_UNSUCCESSFUL;
1380 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
1381 GROUP_MAP *map)
1383 return NT_STATUS_UNSUCCESSFUL;
1386 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
1387 DOM_SID sid)
1389 return NT_STATUS_UNSUCCESSFUL;
1392 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
1393 enum SID_NAME_USE sid_name_use,
1394 GROUP_MAP **rmap, int *num_entries,
1395 BOOL unix_only)
1397 return NT_STATUS_UNSUCCESSFUL;