2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-2000,
6 * Copyright (C) Jean François Micouleau 1998-2001.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern DOM_SID global_sam_sid;
27 static TDB_CONTEXT *tdb; /* used for driver files */
29 #define DATABASE_VERSION 1
30 #define GROUP_PREFIX "UNIXGROUP/"
33 {SE_PRIV_NONE, "no_privs", "No privilege" }, /* this one MUST be first */
34 {SE_PRIV_ADD_MACHINES, "SeMachineAccountPrivilege", "Add workstations to the domain" },
35 {SE_PRIV_SEC_PRIV, "SeSecurityPrivilege", "Manage the audit logs" },
36 {SE_PRIV_TAKE_OWNER, "SeTakeOwnershipPrivilege", "Take ownership of file" },
37 {SE_PRIV_ADD_USERS, "SaAddUsers", "Add users to the domain - Samba" },
38 {SE_PRIV_PRINT_OPERATOR, "SaPrintOp", "Add or remove printers - Samba" },
39 {SE_PRIV_ALL, "SaAllPrivs", "all privileges" }
43 { 2, "SeCreateTokenPrivilege" },
44 { 3, "SeAssignPrimaryTokenPrivilege" },
45 { 4, "SeLockMemoryPrivilege" },
46 { 5, "SeIncreaseQuotaPrivilege" },
47 { 6, "SeMachineAccountPrivilege" },
48 { 7, "SeTcbPrivilege" },
49 { 8, "SeSecurityPrivilege" },
50 { 9, "SeTakeOwnershipPrivilege" },
51 { 10, "SeLoadDriverPrivilege" },
52 { 11, "SeSystemProfilePrivilege" },
53 { 12, "SeSystemtimePrivilege" },
54 { 13, "SeProfileSingleProcessPrivilege" },
55 { 14, "SeIncreaseBasePriorityPrivilege" },
56 { 15, "SeCreatePagefilePrivilege" },
57 { 16, "SeCreatePermanentPrivilege" },
58 { 17, "SeBackupPrivilege" },
59 { 18, "SeRestorePrivilege" },
60 { 19, "SeShutdownPrivilege" },
61 { 20, "SeDebugPrivilege" },
62 { 21, "SeAuditPrivilege" },
63 { 22, "SeSystemEnvironmentPrivilege" },
64 { 23, "SeChangeNotifyPrivilege" },
65 { 24, "SeRemoteShutdownPrivilege" },
66 { 25, "SeUndockPrivilege" },
67 { 26, "SeSyncAgentPrivilege" },
68 { 27, "SeEnableDelegationPrivilege" },
73 * Those are not really privileges like the other ones.
74 * They are handled in a special case and called
78 * SeUnsolicitedInputPrivilege
81 * SeInteractiveLogonRight
82 * SeDenyInteractiveLogonRight
83 * SeDenyNetworkLogonRight
84 * SeDenyBatchLogonRight
85 * SeDenyBatchLogonRight
89 /****************************************************************************
90 check if the user has the required privilege.
91 ****************************************************************************/
92 static BOOL se_priv_access_check(NT_USER_TOKEN *token, uint32 privilege)
94 /* no token, no privilege */
98 if ((token->privilege & privilege)==privilege)
105 /****************************************************************************
106 dump the mapping group mapping to a text file
107 ****************************************************************************/
108 char *decode_sid_name_use(fstring group, enum SID_NAME_USE name_use)
110 static fstring group_type;
114 fstrcpy(group_type,"User");
116 case SID_NAME_DOM_GRP:
117 fstrcpy(group_type,"Domain group");
119 case SID_NAME_DOMAIN:
120 fstrcpy(group_type,"Domain");
123 fstrcpy(group_type,"Local group");
125 case SID_NAME_WKN_GRP:
126 fstrcpy(group_type,"Builtin group");
128 case SID_NAME_DELETED:
129 fstrcpy(group_type,"Deleted");
131 case SID_NAME_INVALID:
132 fstrcpy(group_type,"Invalid");
134 case SID_NAME_UNKNOWN:
136 fstrcpy(group_type,"Unknown type");
140 fstrcpy(group, group_type);
144 /****************************************************************************
145 open the group mapping tdb
146 ****************************************************************************/
147 BOOL init_group_mapping(void)
149 static pid_t local_pid;
150 char *vstring = "INFO/version";
152 if (tdb && local_pid == sys_getpid()) return True;
153 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
155 DEBUG(0,("Failed to open group mapping database\n"));
159 local_pid = sys_getpid();
161 /* handle a Samba upgrade */
162 tdb_lock_bystring(tdb, vstring);
163 if (tdb_fetch_int(tdb, vstring) != DATABASE_VERSION) {
164 tdb_traverse(tdb, (tdb_traverse_func)tdb_delete, NULL);
165 tdb_store_int(tdb, vstring, DATABASE_VERSION);
167 tdb_unlock_bystring(tdb, vstring);
173 /****************************************************************************
174 ****************************************************************************/
175 BOOL add_mapping_entry(GROUP_MAP *map, int flag)
179 fstring string_sid="";
184 sid_to_string(string_sid, &map->sid);
186 len = tdb_pack(buf, sizeof(buf), "ddffd",
187 map->gid, map->sid_name_use, map->nt_name, map->comment, map->systemaccount);
189 /* write the privilege list in the TDB database */
192 len += tdb_pack(buf+len, sizeof(buf)-len, "d", set->count);
193 for (i=0; i<set->count; i++)
194 len += tdb_pack(buf+len, sizeof(buf)-len, "ddd",
195 set->set[i].luid.low, set->set[i].luid.high, set->set[i].attr);
197 if (len > sizeof(buf))
200 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
202 kbuf.dsize = strlen(key)+1;
206 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
211 /****************************************************************************
212 initialise first time the mapping list
213 ****************************************************************************/
214 BOOL add_initial_entry(gid_t gid, fstring sid, enum SID_NAME_USE sid_name_use,
215 fstring nt_name, fstring comment, PRIVILEGE_SET priv_set, uint32 systemaccount)
220 string_to_sid(&map.sid, sid);
221 map.sid_name_use=sid_name_use;
222 fstrcpy(map.nt_name, nt_name);
223 fstrcpy(map.comment, comment);
224 map.systemaccount=systemaccount;
226 map.priv_set.count=priv_set.count;
227 map.priv_set.set=priv_set.set;
229 add_mapping_entry(&map, TDB_INSERT);
234 /****************************************************************************
235 initialise a privilege list
236 ****************************************************************************/
237 void init_privilege(PRIVILEGE_SET *priv_set)
244 /****************************************************************************
245 free a privilege list
246 ****************************************************************************/
247 BOOL free_privilege(PRIVILEGE_SET *priv_set)
249 if (priv_set->count==0) {
250 DEBUG(10,("free_privilege: count=0, nothing to clear ?\n"));
254 if (priv_set->set==NULL) {
255 DEBUG(0,("free_privilege: list ptr is NULL, very strange !\n"));
259 safe_free(priv_set->set);
265 /****************************************************************************
266 add a privilege to a privilege array
267 ****************************************************************************/
268 BOOL add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
272 /* check if the privilege is not already in the list */
273 if (check_priv_in_privilege(priv_set, set))
276 /* we can allocate memory to add the new privilege */
278 new_set=(LUID_ATTR *)Realloc(priv_set->set, (priv_set->count+1)*(sizeof(LUID_ATTR)));
280 DEBUG(0,("add_privilege: could not Realloc memory to add a new privilege\n"));
284 new_set[priv_set->count].luid.high=set.luid.high;
285 new_set[priv_set->count].luid.low=set.luid.low;
286 new_set[priv_set->count].attr=set.attr;
289 priv_set->set=new_set;
294 /****************************************************************************
295 add all the privileges to a privilege array
296 ****************************************************************************/
297 BOOL add_all_privilege(PRIVILEGE_SET *priv_set)
304 set.luid.low=SE_PRIV_ADD_USERS;
305 add_privilege(priv_set, set);
307 set.luid.low=SE_PRIV_ADD_MACHINES;
308 add_privilege(priv_set, set);
310 set.luid.low=SE_PRIV_PRINT_OPERATOR;
311 add_privilege(priv_set, set);
316 /****************************************************************************
317 check if the privilege list is empty
318 ****************************************************************************/
319 BOOL check_empty_privilege(PRIVILEGE_SET *priv_set)
322 if (priv_set->count!=0)
328 /****************************************************************************
329 check if the privilege is in the privilege list
330 ****************************************************************************/
331 BOOL check_priv_in_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
335 /* if the list is empty, obviously we can't have it */
336 if (check_empty_privilege(priv_set))
339 for (i=0; i<priv_set->count; i++) {
342 cur_set=&priv_set->set[i];
343 /* check only the low and high part. Checking the attr field has no meaning */
344 if( (cur_set->luid.low==set.luid.low) && (cur_set->luid.high==set.luid.high) )
351 /****************************************************************************
352 remove a privilege to a privilege array
353 ****************************************************************************/
354 BOOL remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
360 /* check if the privilege is in the list */
361 if (!check_priv_in_privilege(priv_set, set))
364 /* special case if it's the only privilege in the list */
365 if (priv_set->count==1) {
366 free_privilege(priv_set);
367 init_privilege(priv_set);
373 * the privilege is there, create a new list,
374 * and copy the other privileges
377 old_set=priv_set->set;
379 new_set=(LUID_ATTR *)malloc((priv_set->count-1)*(sizeof(LUID_ATTR)));
381 DEBUG(0,("remove_privilege: could not malloc memory for new privilege list\n"));
385 for (i=0, j=0; i<priv_set->count; i++) {
386 if ((old_set[i].luid.low==set.luid.low) &&
387 (old_set[i].luid.high==set.luid.high)) {
391 new_set[j].luid.low=old_set[i].luid.low;
392 new_set[j].luid.high=old_set[i].luid.high;
393 new_set[j].attr=old_set[i].attr;
398 if (j!=priv_set->count-1) {
399 DEBUG(0,("remove_privilege: mismatch ! difference is not -1\n"));
400 DEBUGADD(0,("old count:%d, new count:%d\n", priv_set->count, j));
405 /* ok everything is fine */
408 priv_set->set=new_set;
415 /****************************************************************************
416 initialise first time the mapping list
417 ****************************************************************************/
418 BOOL default_group_mapping(void)
428 PRIVILEGE_SET privilege_none;
429 PRIVILEGE_SET privilege_all;
430 PRIVILEGE_SET privilege_print_op;
432 init_privilege(&privilege_none);
433 init_privilege(&privilege_all);
434 init_privilege(&privilege_print_op);
438 set.luid.low=SE_PRIV_PRINT_OPERATOR;
439 add_privilege(&privilege_print_op, set);
441 add_all_privilege(&privilege_all);
443 /* Add the Wellknown groups */
445 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP, "Administrators", "", privilege_all, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
446 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP, "Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
447 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP, "Guests", "", privilege_none, PR_ACCESS_FROM_NETWORK);
448 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP, "Power Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
450 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP, "Account Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
451 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP, "System Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
452 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP, "Print Operators", "", privilege_print_op, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
453 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP, "Backup Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
455 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP, "Replicators", "", privilege_none, PR_ACCESS_FROM_NETWORK);
457 /* Add the defaults domain groups */
459 sid_copy(&sid_admins, &global_sam_sid);
460 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
461 sid_to_string(str_admins, &sid_admins);
462 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "", privilege_all, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
464 sid_copy(&sid_users, &global_sam_sid);
465 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
466 sid_to_string(str_users, &sid_users);
467 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
469 sid_copy(&sid_guests, &global_sam_sid);
470 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
471 sid_to_string(str_guests, &sid_guests);
472 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "", privilege_none, PR_ACCESS_FROM_NETWORK);
478 /****************************************************************************
479 return the sid and the type of the unix group
480 ****************************************************************************/
481 BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
490 /* the key is the SID, retrieving is direct */
492 sid_to_string(string_sid, &sid);
493 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
496 kbuf.dsize = strlen(key)+1;
498 dbuf = tdb_fetch(tdb, kbuf);
499 if (!dbuf.dptr) return False;
501 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
502 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
506 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
508 DEBUG(10,("get_group_map_from_sid: %d privileges\n", map->priv_set.count));
510 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
511 if (set->set==NULL) {
512 DEBUG(0,("get_group_map_from_sid: could not allocate memory for privileges\n"));
516 for (i=0; i<set->count; i++)
517 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
518 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
520 SAFE_FREE(dbuf.dptr);
521 if (ret != dbuf.dsize) {
522 DEBUG(0,("get_group_map_from_sid: group mapping TDB corrupted ?\n"));
527 sid_copy(&map->sid, &sid);
533 /****************************************************************************
534 return the sid and the type of the unix group
535 ****************************************************************************/
536 BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
538 TDB_DATA kbuf, dbuf, newkey;
544 /* we need to enumerate the TDB to find the GID */
546 for (kbuf = tdb_firstkey(tdb);
548 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
550 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
552 dbuf = tdb_fetch(tdb, kbuf);
553 if (!dbuf.dptr) continue;
555 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
557 string_to_sid(&map->sid, string_sid);
559 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
560 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
563 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
565 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
566 if (set->set==NULL) {
567 DEBUG(0,("get_group_map_from_sid: could not allocate memory for privileges\n"));
571 for (i=0; i<set->count; i++)
572 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
573 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
575 SAFE_FREE(dbuf.dptr);
576 if (ret != dbuf.dsize){
590 /****************************************************************************
591 return the sid and the type of the unix group
592 ****************************************************************************/
593 BOOL get_group_map_from_ntname(char *name, GROUP_MAP *map)
595 TDB_DATA kbuf, dbuf, newkey;
601 /* we need to enumerate the TDB to find the name */
603 for (kbuf = tdb_firstkey(tdb);
605 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
607 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
609 dbuf = tdb_fetch(tdb, kbuf);
610 if (!dbuf.dptr) continue;
612 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
614 string_to_sid(&map->sid, string_sid);
616 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
617 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
620 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
622 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
623 if (set->set==NULL) {
624 DEBUG(0,("get_group_map_from_sid: could not allocate memory for privileges\n"));
628 for (i=0; i<set->count; i++)
629 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
630 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
632 SAFE_FREE(dbuf.dptr);
633 if (ret != dbuf.dsize) {
638 if (StrCaseCmp(name, map->nt_name)==0)
647 /****************************************************************************
648 remove a group mapping entry
649 ****************************************************************************/
650 BOOL group_map_remove(DOM_SID sid)
656 /* the key is the SID, retrieving is direct */
658 sid_to_string(string_sid, &sid);
659 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
662 kbuf.dsize = strlen(key)+1;
664 dbuf = tdb_fetch(tdb, kbuf);
665 if (!dbuf.dptr) return False;
667 SAFE_FREE(dbuf.dptr);
669 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
676 /****************************************************************************
677 enumerate the group mapping
678 ****************************************************************************/
679 BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
680 int *num_entries, BOOL unix_only)
682 TDB_DATA kbuf, dbuf, newkey;
695 for (kbuf = tdb_firstkey(tdb);
697 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
699 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
702 dbuf = tdb_fetch(tdb, kbuf);
706 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
708 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
709 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment, &map.systemaccount);
714 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
717 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
718 if (set->set==NULL) {
719 DEBUG(0,("enum_group_mapping: could not allocate memory for privileges\n"));
724 for (i=0; i<set->count; i++)
725 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
726 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
728 SAFE_FREE(dbuf.dptr);
729 if (ret != dbuf.dsize) {
734 /* list only the type or everything if UNKNOWN */
735 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
740 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
745 string_to_sid(&map.sid, string_sid);
747 decode_sid_name_use(group_type, map.sid_name_use);
749 mapt=(GROUP_MAP *)Realloc((*rmap), (entries+1)*sizeof(GROUP_MAP));
751 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
759 mapt[entries].gid = map.gid;
760 sid_copy( &mapt[entries].sid, &map.sid);
761 mapt[entries].sid_name_use = map.sid_name_use;
762 fstrcpy(mapt[entries].nt_name, map.nt_name);
763 fstrcpy(mapt[entries].comment, map.comment);
764 mapt[entries].systemaccount=map.systemaccount;
765 mapt[entries].priv_set.count=set->count;
766 mapt[entries].priv_set.control=set->control;
767 mapt[entries].priv_set.set=set->set;
772 *num_entries=entries;
777 /****************************************************************************
778 convert a privilege string to a privilege array
779 ****************************************************************************/
780 void convert_priv_from_text(PRIVILEGE_SET *se_priv, char *privilege)
787 /* By default no privilege */
788 init_privilege(se_priv);
793 while(next_token(&p, tok, " ", sizeof(tok)) ) {
794 for (i=0; i<=PRIV_ALL_INDEX; i++) {
795 if (StrCaseCmp(privs[i].priv, tok)==0) {
798 set.luid.low=privs[i].se_priv;
799 add_privilege(se_priv, set);
805 /****************************************************************************
806 convert a privilege array to a privilege string
807 ****************************************************************************/
808 void convert_priv_to_text(PRIVILEGE_SET *se_priv, char *privilege)
815 ZERO_STRUCTP(privilege);
817 if (check_empty_privilege(se_priv)) {
818 fstrcat(privilege, "No privilege");
822 for(i=0; i<se_priv->count; i++) {
824 while (privs[j].se_priv!=se_priv->set[i].luid.low && j<=PRIV_ALL_INDEX) {
828 fstrcat(privilege, privs[j].priv);
829 fstrcat(privilege, " ");
836 * High level functions
837 * better to use them than the lower ones.
839 * we are checking if the group is in the mapping file
840 * and if the group is an existing unix group
844 /* get a domain group from it's SID */
846 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
850 DEBUG(10, ("get_domain_group_from_sid\n"));
852 /* if the group is NOT in the database, it CAN NOT be a domain group */
853 if(!get_group_map_from_sid(sid, map))
856 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
858 /* if it's not a domain group, continue */
859 if (map->sid_name_use!=SID_NAME_DOM_GRP)
862 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
867 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%d\n",map->gid));
869 if ( (grp=getgrgid(map->gid)) == NULL)
872 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
878 /* get a local (alias) group from it's SID */
880 BOOL get_local_group_from_sid(DOM_SID sid, GROUP_MAP *map)
884 /* The group is in the mapping table */
885 if(get_group_map_from_sid(sid, map)) {
886 if (map->sid_name_use!=SID_NAME_ALIAS)
892 if ( (grp=getgrgid(map->gid)) == NULL)
895 /* the group isn't in the mapping table.
896 * make one based on the unix information */
899 sid_peek_rid(&sid, &alias_rid);
900 map->gid=pdb_user_rid_to_gid(alias_rid);
902 if ((grp=getgrgid(map->gid)) == NULL)
905 map->sid_name_use=SID_NAME_ALIAS;
906 map->systemaccount=PR_ACCESS_FROM_NETWORK;
908 fstrcpy(map->nt_name, grp->gr_name);
909 fstrcpy(map->comment, "Local Unix Group");
911 init_privilege(&map->priv_set);
913 sid_copy(&map->sid, &sid);
919 /* get a builtin group from it's SID */
921 BOOL get_builtin_group_from_sid(DOM_SID sid, GROUP_MAP *map)
925 if(!get_group_map_from_sid(sid, map))
928 if (map->sid_name_use!=SID_NAME_WKN_GRP)
934 if ( (grp=getgrgid(map->gid)) == NULL)
942 /****************************************************************************
943 Returns a GROUP_MAP struct based on the gid.
944 ****************************************************************************/
945 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map)
949 if ( (grp=getgrgid(gid)) == NULL)
953 * make a group map from scratch if doesn't exist.
955 if (!get_group_map_from_gid(gid, map)) {
957 map->sid_name_use=SID_NAME_ALIAS;
958 map->systemaccount=PR_ACCESS_FROM_NETWORK;
959 init_privilege(&map->priv_set);
961 sid_copy(&map->sid, &global_sam_sid);
962 sid_append_rid(&map->sid, pdb_gid_to_group_rid(gid));
964 fstrcpy(map->nt_name, grp->gr_name);
965 fstrcpy(map->comment, "Local Unix Group");
974 /****************************************************************************
975 Get the member users of a group and
976 all the users who have that group as primary.
978 give back an array of uid
979 return the grand number of users
982 TODO: sort the list and remove duplicate. JFM.
984 ****************************************************************************/
986 BOOL get_uid_list_of_group(gid_t gid, uid_t **uid, int *num_uids)
997 if ( (grp=getgrgid(gid)) == NULL)
1000 gr = grp->gr_mem[0];
1001 DEBUG(10, ("getting members\n"));
1003 while (gr && (*gr != (char)'\0')) {
1004 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
1006 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
1011 if( (pwd=getpwnam(gr)) !=NULL) {
1012 (*uid)[*num_uids]=pwd->pw_uid;
1015 gr = grp->gr_mem[++i];
1017 DEBUG(10, ("got [%d] members\n", *num_uids));
1020 while ((pwd=getpwent()) != NULL) {
1021 if (pwd->pw_gid==gid) {
1022 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
1024 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
1028 (*uid)[*num_uids]=pwd->pw_uid;
1034 DEBUG(10, ("got primary groups, members: [%d]\n", *num_uids));
1039 /****************************************************************************
1040 Create a UNIX group on demand.
1041 ****************************************************************************/
1043 int smb_create_group(char *unix_group)
1048 pstrcpy(add_script, lp_addgroup_script());
1049 if (! *add_script) return -1;
1050 pstring_sub(add_script, "%g", unix_group);
1051 ret = smbrun(add_script,NULL);
1052 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
1056 /****************************************************************************
1057 Delete a UNIX group on demand.
1058 ****************************************************************************/
1060 int smb_delete_group(char *unix_group)
1065 pstrcpy(del_script, lp_delgroup_script());
1066 if (! *del_script) return -1;
1067 pstring_sub(del_script, "%g", unix_group);
1068 ret = smbrun(del_script,NULL);
1069 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
1073 /****************************************************************************
1074 Create a UNIX group on demand.
1075 ****************************************************************************/
1077 int smb_add_user_group(char *unix_group, char *unix_user)
1082 pstrcpy(add_script, lp_addusertogroup_script());
1083 if (! *add_script) return -1;
1084 pstring_sub(add_script, "%g", unix_group);
1085 pstring_sub(add_script, "%u", unix_user);
1086 ret = smbrun(add_script,NULL);
1087 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1091 /****************************************************************************
1092 Delete a UNIX group on demand.
1093 ****************************************************************************/
1095 int smb_delete_user_group(char *unix_group, char *unix_user)
1100 pstrcpy(del_script, lp_deluserfromgroup_script());
1101 if (! *del_script) return -1;
1102 pstring_sub(del_script, "%g", unix_group);
1103 pstring_sub(del_script, "%u", unix_user);
1104 ret = smbrun(del_script,NULL);
1105 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));