Fix memleak in groupdb. Spotted by Metze
[Samba.git] / source / groupdb / mapping.c
blob5d2d28f152604d755ab7a65af0000df796205e6b
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 PRIVS privs[] = {
32 {SE_PRIV_NONE, "no_privs", "No privilege" }, /* this one MUST be first */
33 {SE_PRIV_ADD_MACHINES, "SeMachineAccountPrivilege", "Add workstations to the domain" },
34 {SE_PRIV_SEC_PRIV, "SeSecurityPrivilege", "Manage the audit logs" },
35 {SE_PRIV_TAKE_OWNER, "SeTakeOwnershipPrivilege", "Take ownership of file" },
36 {SE_PRIV_ADD_USERS, "SaAddUsers", "Add users to the domain - Samba" },
37 {SE_PRIV_PRINT_OPERATOR, "SaPrintOp", "Add or remove printers - Samba" },
38 {SE_PRIV_ALL, "SaAllPrivs", "all privileges" }
42 /****************************************************************************
43 dump the mapping group mapping to a text file
44 ****************************************************************************/
45 char *decode_sid_name_use(fstring group, enum SID_NAME_USE name_use)
47 static fstring group_type;
49 switch(name_use) {
50 case SID_NAME_USER:
51 fstrcpy(group_type,"User");
52 break;
53 case SID_NAME_DOM_GRP:
54 fstrcpy(group_type,"Domain group");
55 break;
56 case SID_NAME_DOMAIN:
57 fstrcpy(group_type,"Domain");
58 break;
59 case SID_NAME_ALIAS:
60 fstrcpy(group_type,"Local group");
61 break;
62 case SID_NAME_WKN_GRP:
63 fstrcpy(group_type,"Builtin group");
64 break;
65 case SID_NAME_DELETED:
66 fstrcpy(group_type,"Deleted");
67 break;
68 case SID_NAME_INVALID:
69 fstrcpy(group_type,"Invalid");
70 break;
71 case SID_NAME_UNKNOWN:
72 default:
73 fstrcpy(group_type,"Unknown type");
74 break;
77 fstrcpy(group, group_type);
78 return group_type;
81 /****************************************************************************
82 initialise first time the mapping list - called from init_group_mapping()
83 ****************************************************************************/
84 static BOOL default_group_mapping(void)
86 DOM_SID sid_admins;
87 DOM_SID sid_users;
88 DOM_SID sid_guests;
89 fstring str_admins;
90 fstring str_users;
91 fstring str_guests;
93 /* Add the Wellknown groups */
95 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP, "Administrators", "");
96 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP, "Users", "");
97 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP, "Guests", "");
98 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP, "Power Users", "");
99 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP, "Account Operators", "");
100 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP, "System Operators", "");
101 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP, "Print Operators", "");
102 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP, "Backup Operators", "");
103 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP, "Replicators", "");
105 /* Add the defaults domain groups */
107 sid_copy(&sid_admins, get_global_sam_sid());
108 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
109 sid_to_string(str_admins, &sid_admins);
110 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "");
112 sid_copy(&sid_users, get_global_sam_sid());
113 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
114 sid_to_string(str_users, &sid_users);
115 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "");
117 sid_copy(&sid_guests, get_global_sam_sid());
118 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
119 sid_to_string(str_guests, &sid_guests);
120 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "");
122 return True;
125 /****************************************************************************
126 Open the group mapping tdb.
127 ****************************************************************************/
129 static BOOL init_group_mapping(void)
131 static pid_t local_pid;
132 const char *vstring = "INFO/version";
133 int32 vers_id;
135 if (tdb && local_pid == sys_getpid())
136 return True;
137 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
138 if (!tdb) {
139 DEBUG(0,("Failed to open group mapping database\n"));
140 return False;
143 local_pid = sys_getpid();
145 /* handle a Samba upgrade */
146 tdb_lock_bystring(tdb, vstring, 0);
148 /* Cope with byte-reversed older versions of the db. */
149 vers_id = tdb_fetch_int32(tdb, vstring);
150 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
151 /* Written on a bigendian machine with old fetch_int code. Save as le. */
152 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
153 vers_id = DATABASE_VERSION_V2;
156 if (vers_id != DATABASE_VERSION_V2) {
157 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
158 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
161 tdb_unlock_bystring(tdb, vstring);
163 /* write a list of default groups */
164 if(!default_group_mapping())
165 return False;
167 return True;
170 /****************************************************************************
171 ****************************************************************************/
172 static BOOL add_mapping_entry(GROUP_MAP *map, int flag)
174 TDB_DATA kbuf, dbuf;
175 pstring key, buf;
176 fstring string_sid="";
177 int len;
179 if(!init_group_mapping()) {
180 DEBUG(0,("failed to initialize group mapping"));
181 return(False);
184 sid_to_string(string_sid, &map->sid);
186 len = tdb_pack(buf, sizeof(buf), "ddff",
187 map->gid, map->sid_name_use, map->nt_name, map->comment);
189 if (len > sizeof(buf))
190 return False;
192 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
194 kbuf.dsize = strlen(key)+1;
195 kbuf.dptr = key;
196 dbuf.dsize = len;
197 dbuf.dptr = buf;
198 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
200 return True;
203 /****************************************************************************
204 initialise first time the mapping list
205 ****************************************************************************/
206 BOOL add_initial_entry(gid_t gid, const char *sid, enum SID_NAME_USE sid_name_use, const char *nt_name, const char *comment)
208 GROUP_MAP map;
210 if(!init_group_mapping()) {
211 DEBUG(0,("failed to initialize group mapping"));
212 return(False);
215 map.gid=gid;
216 if (!string_to_sid(&map.sid, sid)) {
217 DEBUG(0, ("string_to_sid failed: %s", sid));
218 return False;
221 map.sid_name_use=sid_name_use;
222 fstrcpy(map.nt_name, nt_name);
223 fstrcpy(map.comment, comment);
225 pdb_add_group_mapping_entry(&map);
227 return True;
230 /****************************************************************************
231 Return the sid and the type of the unix group.
232 ****************************************************************************/
234 static BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
236 TDB_DATA kbuf, dbuf;
237 pstring key;
238 fstring string_sid;
239 int ret;
241 if(!init_group_mapping()) {
242 DEBUG(0,("failed to initialize group mapping"));
243 return(False);
246 /* the key is the SID, retrieving is direct */
248 sid_to_string(string_sid, &sid);
249 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
251 kbuf.dptr = key;
252 kbuf.dsize = strlen(key)+1;
254 dbuf = tdb_fetch(tdb, kbuf);
255 if (!dbuf.dptr)
256 return False;
258 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
259 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
261 SAFE_FREE(dbuf.dptr);
263 sid_copy(&map->sid, &sid);
265 return True;
268 /****************************************************************************
269 Return the sid and the type of the unix group.
270 ****************************************************************************/
272 static BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
274 TDB_DATA kbuf, dbuf, newkey;
275 fstring string_sid;
276 int ret;
278 if(!init_group_mapping()) {
279 DEBUG(0,("failed to initialize group mapping"));
280 return(False);
283 /* we need to enumerate the TDB to find the GID */
285 for (kbuf = tdb_firstkey(tdb);
286 kbuf.dptr;
287 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
289 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
291 dbuf = tdb_fetch(tdb, kbuf);
292 if (!dbuf.dptr)
293 continue;
295 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
297 string_to_sid(&map->sid, string_sid);
299 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
300 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
302 SAFE_FREE(dbuf.dptr);
304 if (gid==map->gid) {
305 SAFE_FREE(kbuf.dptr);
306 return True;
310 return False;
313 /****************************************************************************
314 Return the sid and the type of the unix group.
315 ****************************************************************************/
317 static BOOL get_group_map_from_ntname(const char *name, GROUP_MAP *map)
319 TDB_DATA kbuf, dbuf, newkey;
320 fstring string_sid;
321 int ret;
323 if(!init_group_mapping()) {
324 DEBUG(0,("get_group_map_from_ntname:failed to initialize group mapping"));
325 return(False);
328 /* we need to enumerate the TDB to find the name */
330 for (kbuf = tdb_firstkey(tdb);
331 kbuf.dptr;
332 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
334 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
336 dbuf = tdb_fetch(tdb, kbuf);
337 if (!dbuf.dptr)
338 continue;
340 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
342 string_to_sid(&map->sid, string_sid);
344 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
345 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
347 SAFE_FREE(dbuf.dptr);
349 if (StrCaseCmp(name, map->nt_name)==0) {
350 SAFE_FREE(kbuf.dptr);
351 return True;
355 return False;
358 /****************************************************************************
359 Remove a group mapping entry.
360 ****************************************************************************/
362 static BOOL group_map_remove(DOM_SID sid)
364 TDB_DATA kbuf, dbuf;
365 pstring key;
366 fstring string_sid;
368 if(!init_group_mapping()) {
369 DEBUG(0,("failed to initialize group mapping"));
370 return(False);
373 /* the key is the SID, retrieving is direct */
375 sid_to_string(string_sid, &sid);
376 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
378 kbuf.dptr = key;
379 kbuf.dsize = strlen(key)+1;
381 dbuf = tdb_fetch(tdb, kbuf);
382 if (!dbuf.dptr)
383 return False;
385 SAFE_FREE(dbuf.dptr);
387 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
388 return False;
390 return True;
393 /****************************************************************************
394 Enumerate the group mapping.
395 ****************************************************************************/
397 static BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
398 int *num_entries, BOOL unix_only)
400 TDB_DATA kbuf, dbuf, newkey;
401 fstring string_sid;
402 fstring group_type;
403 GROUP_MAP map;
404 GROUP_MAP *mapt;
405 int ret;
406 int entries=0;
408 if(!init_group_mapping()) {
409 DEBUG(0,("failed to initialize group mapping"));
410 return(False);
413 *num_entries=0;
414 *rmap=NULL;
416 for (kbuf = tdb_firstkey(tdb);
417 kbuf.dptr;
418 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
420 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
421 continue;
423 dbuf = tdb_fetch(tdb, kbuf);
424 if (!dbuf.dptr)
425 continue;
427 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
429 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
430 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
432 SAFE_FREE(dbuf.dptr);
434 /* list only the type or everything if UNKNOWN */
435 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
436 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
437 continue;
440 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
441 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
442 continue;
445 string_to_sid(&map.sid, string_sid);
447 decode_sid_name_use(group_type, map.sid_name_use);
448 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map.nt_name ,group_type));
450 mapt=(GROUP_MAP *)Realloc((*rmap), (entries+1)*sizeof(GROUP_MAP));
451 if (!mapt) {
452 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
453 SAFE_FREE(*rmap);
454 return False;
456 else
457 (*rmap) = mapt;
459 mapt[entries].gid = map.gid;
460 sid_copy( &mapt[entries].sid, &map.sid);
461 mapt[entries].sid_name_use = map.sid_name_use;
462 fstrcpy(mapt[entries].nt_name, map.nt_name);
463 fstrcpy(mapt[entries].comment, map.comment);
465 entries++;
469 *num_entries=entries;
471 return True;
476 * High level functions
477 * better to use them than the lower ones.
479 * we are checking if the group is in the mapping file
480 * and if the group is an existing unix group
484 /* get a domain group from it's SID */
486 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
488 struct group *grp;
490 if(!init_group_mapping()) {
491 DEBUG(0,("failed to initialize group mapping"));
492 return(False);
495 DEBUG(10, ("get_domain_group_from_sid\n"));
497 /* if the group is NOT in the database, it CAN NOT be a domain group */
498 if(!pdb_getgrsid(map, sid))
499 return False;
501 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
503 /* if it's not a domain group, continue */
504 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
505 return False;
508 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
510 if (map->gid==-1) {
511 return False;
514 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%d\n",map->gid));
516 if ( (grp=getgrgid(map->gid)) == NULL) {
517 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
518 return False;
521 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
523 return True;
527 /* get a local (alias) group from it's SID */
529 BOOL get_local_group_from_sid(DOM_SID sid, GROUP_MAP *map)
531 struct group *grp;
533 if(!init_group_mapping()) {
534 DEBUG(0,("failed to initialize group mapping"));
535 return(False);
538 /* The group is in the mapping table */
539 if(pdb_getgrsid(map, sid)) {
540 if (map->sid_name_use!=SID_NAME_ALIAS) {
541 return False;
544 if (map->gid==-1) {
545 return False;
548 if ( (grp=getgrgid(map->gid)) == NULL) {
549 return False;
551 } else {
552 /* the group isn't in the mapping table.
553 * make one based on the unix information */
554 uint32 alias_rid;
556 sid_peek_rid(&sid, &alias_rid);
557 map->gid=pdb_group_rid_to_gid(alias_rid);
559 if ((grp=getgrgid(map->gid)) == NULL)
560 return False;
562 map->sid_name_use=SID_NAME_ALIAS;
564 fstrcpy(map->nt_name, grp->gr_name);
565 fstrcpy(map->comment, "Local Unix Group");
567 sid_copy(&map->sid, &sid);
570 return True;
573 /* get a builtin group from it's SID */
575 BOOL get_builtin_group_from_sid(DOM_SID sid, GROUP_MAP *map)
577 struct group *grp;
579 if(!init_group_mapping()) {
580 DEBUG(0,("failed to initialize group mapping"));
581 return(False);
584 if(!pdb_getgrsid(map, sid))
585 return False;
587 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
588 return False;
591 if (map->gid==-1) {
592 return False;
595 if ( (grp=getgrgid(map->gid)) == NULL) {
596 return False;
599 return True;
604 /****************************************************************************
605 Returns a GROUP_MAP struct based on the gid.
606 ****************************************************************************/
607 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map)
609 struct group *grp;
611 if(!init_group_mapping()) {
612 DEBUG(0,("failed to initialize group mapping"));
613 return(False);
616 if ( (grp=getgrgid(gid)) == NULL)
617 return False;
620 * make a group map from scratch if doesn't exist.
622 if (!pdb_getgrgid(map, gid)) {
623 map->gid=gid;
624 map->sid_name_use=SID_NAME_ALIAS;
626 /* interim solution until we have a last RID allocated */
628 sid_copy(&map->sid, get_global_sam_sid());
629 sid_append_rid(&map->sid, pdb_gid_to_group_rid(gid));
631 fstrcpy(map->nt_name, grp->gr_name);
632 fstrcpy(map->comment, "Local Unix Group");
635 return True;
641 /****************************************************************************
642 Get the member users of a group and
643 all the users who have that group as primary.
645 give back an array of uid
646 return the grand number of users
649 TODO: sort the list and remove duplicate. JFM.
651 ****************************************************************************/
653 BOOL get_uid_list_of_group(gid_t gid, uid_t **uid, int *num_uids)
655 struct group *grp;
656 struct passwd *pwd;
657 int i=0;
658 char *gr;
659 uid_t *u;
661 if(!init_group_mapping()) {
662 DEBUG(0,("failed to initialize group mapping"));
663 return(False);
666 *num_uids = 0;
667 *uid=NULL;
669 if ( (grp=getgrgid(gid)) == NULL)
670 return False;
672 gr = grp->gr_mem[0];
673 DEBUG(10, ("getting members\n"));
675 while (gr && (*gr != (char)'\0')) {
676 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
677 if (!u) {
678 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
679 return False;
681 else (*uid) = u;
683 if( (pwd=getpwnam_alloc(gr)) !=NULL) {
684 (*uid)[*num_uids]=pwd->pw_uid;
685 (*num_uids)++;
686 passwd_free(&pwd);
688 gr = grp->gr_mem[++i];
690 DEBUG(10, ("got [%d] members\n", *num_uids));
692 setpwent();
693 while ((pwd=getpwent()) != NULL) {
694 if (pwd->pw_gid==gid) {
695 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
696 if (!u) {
697 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
698 return False;
700 else (*uid) = u;
701 (*uid)[*num_uids]=pwd->pw_uid;
703 (*num_uids)++;
706 endpwent();
707 DEBUG(10, ("got primary groups, members: [%d]\n", *num_uids));
709 return True;
712 /****************************************************************************
713 Create a UNIX group on demand.
714 ****************************************************************************/
716 int smb_create_group(char *unix_group, gid_t *new_gid)
718 pstring add_script;
719 int ret;
720 int fd = 0;
722 pstrcpy(add_script, lp_addgroup_script());
723 if (! *add_script) return -1;
724 pstring_sub(add_script, "%g", unix_group);
725 ret = smbrun(add_script, (new_gid!=NULL) ? &fd : NULL);
726 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
727 if (ret != 0)
728 return ret;
730 if (fd != 0) {
731 fstring output;
733 *new_gid = 0;
734 if (read(fd, output, sizeof(output)) > 0) {
735 *new_gid = (gid_t)strtoul(output, NULL, 10);
737 close(fd);
739 if (*new_gid == 0) {
740 /* The output was garbage. We assume nobody
741 will create group 0 via smbd. Now we try to
742 get the group via getgrnam. */
744 struct group *grp = getgrnam(unix_group);
745 if (grp != NULL)
746 *new_gid = grp->gr_gid;
747 else
748 return 1;
752 return ret;
755 /****************************************************************************
756 Delete a UNIX group on demand.
757 ****************************************************************************/
759 int smb_delete_group(char *unix_group)
761 pstring del_script;
762 int ret;
764 pstrcpy(del_script, lp_delgroup_script());
765 if (! *del_script) return -1;
766 pstring_sub(del_script, "%g", unix_group);
767 ret = smbrun(del_script,NULL);
768 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
769 return ret;
772 /****************************************************************************
773 Set a user's primary UNIX group.
774 ****************************************************************************/
775 int smb_set_primary_group(const char *unix_group, const char* unix_user)
777 pstring add_script;
778 int ret;
780 pstrcpy(add_script, lp_setprimarygroup_script());
781 if (! *add_script) return -1;
782 all_string_sub(add_script, "%g", unix_group, sizeof(add_script));
783 all_string_sub(add_script, "%u", unix_user, sizeof(add_script));
784 ret = smbrun(add_script,NULL);
785 DEBUG(3,("smb_set_primary_group: "
786 "Running the command `%s' gave %d\n",add_script,ret));
787 return ret;
790 /****************************************************************************
791 Add a user to a UNIX group.
792 ****************************************************************************/
794 int smb_add_user_group(char *unix_group, char *unix_user)
796 pstring add_script;
797 int ret;
799 pstrcpy(add_script, lp_addusertogroup_script());
800 if (! *add_script) return -1;
801 pstring_sub(add_script, "%g", unix_group);
802 pstring_sub(add_script, "%u", unix_user);
803 ret = smbrun(add_script,NULL);
804 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
805 return ret;
808 /****************************************************************************
809 Delete a user from a UNIX group
810 ****************************************************************************/
812 int smb_delete_user_group(const char *unix_group, const char *unix_user)
814 pstring del_script;
815 int ret;
817 pstrcpy(del_script, lp_deluserfromgroup_script());
818 if (! *del_script) return -1;
819 pstring_sub(del_script, "%g", unix_group);
820 pstring_sub(del_script, "%u", unix_user);
821 ret = smbrun(del_script,NULL);
822 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
823 return ret;
827 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
828 DOM_SID sid)
830 return get_group_map_from_sid(sid, map) ?
831 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
834 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
835 gid_t gid)
837 return get_group_map_from_gid(gid, map) ?
838 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
841 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
842 const char *name)
844 return get_group_map_from_ntname(name, map) ?
845 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
848 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
849 GROUP_MAP *map)
851 return add_mapping_entry(map, TDB_INSERT) ?
852 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
855 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
856 GROUP_MAP *map)
858 return add_mapping_entry(map, TDB_REPLACE) ?
859 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
862 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
863 DOM_SID sid)
865 return group_map_remove(sid) ?
866 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
869 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
870 enum SID_NAME_USE sid_name_use,
871 GROUP_MAP **rmap, int *num_entries,
872 BOOL unix_only)
874 return enum_group_mapping(sid_name_use, rmap, num_entries, unix_only) ?
875 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
878 /**********************************************************************
879 no ops for passdb backends that don't implement group mapping
880 *********************************************************************/
882 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
883 DOM_SID sid)
885 return NT_STATUS_UNSUCCESSFUL;
888 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
889 gid_t gid)
891 return NT_STATUS_UNSUCCESSFUL;
894 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
895 const char *name)
897 return NT_STATUS_UNSUCCESSFUL;
900 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
901 GROUP_MAP *map)
903 return NT_STATUS_UNSUCCESSFUL;
906 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
907 GROUP_MAP *map)
909 return NT_STATUS_UNSUCCESSFUL;
912 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
913 DOM_SID sid)
915 return NT_STATUS_UNSUCCESSFUL;
918 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
919 enum SID_NAME_USE sid_name_use,
920 GROUP_MAP **rmap, int *num_entries,
921 BOOL unix_only)
923 return NT_STATUS_UNSUCCESSFUL;