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_V1 1 /* native byte format. */
30 #define DATABASE_VERSION_V2 2 /* le format. */
32 #define GROUP_PREFIX "UNIXGROUP/"
35 {SE_PRIV_NONE, "no_privs", "No privilege" }, /* this one MUST be first */
36 {SE_PRIV_ADD_MACHINES, "SeMachineAccountPrivilege", "Add workstations to the domain" },
37 {SE_PRIV_SEC_PRIV, "SeSecurityPrivilege", "Manage the audit logs" },
38 {SE_PRIV_TAKE_OWNER, "SeTakeOwnershipPrivilege", "Take ownership of file" },
39 {SE_PRIV_ADD_USERS, "SaAddUsers", "Add users to the domain - Samba" },
40 {SE_PRIV_PRINT_OPERATOR, "SaPrintOp", "Add or remove printers - Samba" },
41 {SE_PRIV_ALL, "SaAllPrivs", "all privileges" }
45 { 2, "SeCreateTokenPrivilege" },
46 { 3, "SeAssignPrimaryTokenPrivilege" },
47 { 4, "SeLockMemoryPrivilege" },
48 { 5, "SeIncreaseQuotaPrivilege" },
49 { 6, "SeMachineAccountPrivilege" },
50 { 7, "SeTcbPrivilege" },
51 { 8, "SeSecurityPrivilege" },
52 { 9, "SeTakeOwnershipPrivilege" },
53 { 10, "SeLoadDriverPrivilege" },
54 { 11, "SeSystemProfilePrivilege" },
55 { 12, "SeSystemtimePrivilege" },
56 { 13, "SeProfileSingleProcessPrivilege" },
57 { 14, "SeIncreaseBasePriorityPrivilege" },
58 { 15, "SeCreatePagefilePrivilege" },
59 { 16, "SeCreatePermanentPrivilege" },
60 { 17, "SeBackupPrivilege" },
61 { 18, "SeRestorePrivilege" },
62 { 19, "SeShutdownPrivilege" },
63 { 20, "SeDebugPrivilege" },
64 { 21, "SeAuditPrivilege" },
65 { 22, "SeSystemEnvironmentPrivilege" },
66 { 23, "SeChangeNotifyPrivilege" },
67 { 24, "SeRemoteShutdownPrivilege" },
68 { 25, "SeUndockPrivilege" },
69 { 26, "SeSyncAgentPrivilege" },
70 { 27, "SeEnableDelegationPrivilege" },
75 * Those are not really privileges like the other ones.
76 * They are handled in a special case and called
80 * SeUnsolicitedInputPrivilege
83 * SeInteractiveLogonRight
84 * SeDenyInteractiveLogonRight
85 * SeDenyNetworkLogonRight
86 * SeDenyBatchLogonRight
87 * SeDenyBatchLogonRight
91 /****************************************************************************
92 check if the user has the required privilege.
93 ****************************************************************************/
94 static BOOL se_priv_access_check(NT_USER_TOKEN *token, uint32 privilege)
96 /* no token, no privilege */
100 if ((token->privilege & privilege)==privilege)
107 /****************************************************************************
108 dump the mapping group mapping to a text file
109 ****************************************************************************/
110 char *decode_sid_name_use(fstring group, enum SID_NAME_USE name_use)
112 static fstring group_type;
116 fstrcpy(group_type,"User");
118 case SID_NAME_DOM_GRP:
119 fstrcpy(group_type,"Domain group");
121 case SID_NAME_DOMAIN:
122 fstrcpy(group_type,"Domain");
125 fstrcpy(group_type,"Local group");
127 case SID_NAME_WKN_GRP:
128 fstrcpy(group_type,"Builtin group");
130 case SID_NAME_DELETED:
131 fstrcpy(group_type,"Deleted");
133 case SID_NAME_INVALID:
134 fstrcpy(group_type,"Invalid");
136 case SID_NAME_UNKNOWN:
138 fstrcpy(group_type,"Unknown type");
142 fstrcpy(group, group_type);
146 /****************************************************************************
147 initialise first time the mapping list - called from init_group_mapping()
148 ****************************************************************************/
149 static BOOL default_group_mapping(void)
159 PRIVILEGE_SET privilege_none;
160 PRIVILEGE_SET privilege_all;
161 PRIVILEGE_SET privilege_print_op;
163 init_privilege(&privilege_none);
164 init_privilege(&privilege_all);
165 init_privilege(&privilege_print_op);
169 set.luid.low=SE_PRIV_PRINT_OPERATOR;
170 add_privilege(&privilege_print_op, set);
172 add_all_privilege(&privilege_all);
174 /* Add the Wellknown groups */
176 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_ALIAS, "Administrators", "", privilege_all, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
177 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_ALIAS, "Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
178 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_ALIAS, "Guests", "", privilege_none, PR_ACCESS_FROM_NETWORK);
179 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_ALIAS, "Power Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
181 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_ALIAS, "Account Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
182 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_ALIAS, "System Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
183 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_ALIAS, "Print Operators", "", privilege_print_op, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
184 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_ALIAS, "Backup Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
186 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_ALIAS, "Replicators", "", privilege_none, PR_ACCESS_FROM_NETWORK);
188 /* Add the defaults domain groups */
190 sid_copy(&sid_admins, &global_sam_sid);
191 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
192 sid_to_string(str_admins, &sid_admins);
193 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "", privilege_all, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
195 sid_copy(&sid_users, &global_sam_sid);
196 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
197 sid_to_string(str_users, &sid_users);
198 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
200 sid_copy(&sid_guests, &global_sam_sid);
201 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
202 sid_to_string(str_guests, &sid_guests);
203 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "", privilege_none, PR_ACCESS_FROM_NETWORK);
208 /****************************************************************************
209 Open the group mapping tdb.
210 ****************************************************************************/
212 static BOOL init_group_mapping(void)
214 static pid_t local_pid;
215 char *vstring = "INFO/version";
218 if (tdb && local_pid == sys_getpid())
220 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
222 DEBUG(0,("Failed to open group mapping database\n"));
226 local_pid = sys_getpid();
228 /* handle a Samba upgrade */
229 tdb_lock_bystring(tdb, vstring);
231 /* Cope with byte-reversed older versions of the db. */
232 vers_id = tdb_fetch_int32(tdb, vstring);
233 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
234 /* Written on a bigendian machine with old fetch_int code. Save as le. */
235 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
236 vers_id = DATABASE_VERSION_V2;
239 if (vers_id != DATABASE_VERSION_V2) {
240 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
241 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
244 tdb_unlock_bystring(tdb, vstring);
246 /* write a list of default groups */
247 if(!default_group_mapping())
253 /****************************************************************************
254 ****************************************************************************/
255 BOOL add_mapping_entry(GROUP_MAP *map, int flag)
259 fstring string_sid="";
264 if(!init_group_mapping()) {
265 DEBUG(0,("failed to initialize group mapping"));
269 sid_to_string(string_sid, &map->sid);
271 len = tdb_pack(buf, sizeof(buf), "ddffd",
272 map->gid, map->sid_name_use, map->nt_name, map->comment, map->systemaccount);
274 /* write the privilege list in the TDB database */
277 len += tdb_pack(buf+len, sizeof(buf)-len, "d", set->count);
278 for (i=0; i<set->count; i++)
279 len += tdb_pack(buf+len, sizeof(buf)-len, "ddd",
280 set->set[i].luid.low, set->set[i].luid.high, set->set[i].attr);
282 if (len > sizeof(buf))
285 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
287 kbuf.dsize = strlen(key)+1;
291 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
296 /****************************************************************************
297 initialise first time the mapping list
298 ****************************************************************************/
299 BOOL add_initial_entry(gid_t gid, fstring sid, enum SID_NAME_USE sid_name_use,
300 fstring nt_name, fstring comment, PRIVILEGE_SET priv_set, uint32 systemaccount)
304 if(!init_group_mapping()) {
305 DEBUG(0,("failed to initialize group mapping"));
310 string_to_sid(&map.sid, sid);
311 map.sid_name_use=sid_name_use;
312 fstrcpy(map.nt_name, nt_name);
313 fstrcpy(map.comment, comment);
314 map.systemaccount=systemaccount;
316 map.priv_set.count=priv_set.count;
317 map.priv_set.set=priv_set.set;
319 add_mapping_entry(&map, TDB_INSERT);
324 /****************************************************************************
325 initialise a privilege list
326 ****************************************************************************/
327 void init_privilege(PRIVILEGE_SET *priv_set)
334 /****************************************************************************
335 free a privilege list
336 ****************************************************************************/
337 BOOL free_privilege(PRIVILEGE_SET *priv_set)
339 if (priv_set->count==0) {
340 DEBUG(100,("free_privilege: count=0, nothing to clear ?\n"));
344 if (priv_set->set==NULL) {
345 DEBUG(0,("free_privilege: list ptr is NULL, very strange !\n"));
349 safe_free(priv_set->set);
357 /****************************************************************************
358 add a privilege to a privilege array
359 ****************************************************************************/
360 BOOL add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
364 /* check if the privilege is not already in the list */
365 if (check_priv_in_privilege(priv_set, set))
368 /* we can allocate memory to add the new privilege */
370 new_set=(LUID_ATTR *)Realloc(priv_set->set, (priv_set->count+1)*(sizeof(LUID_ATTR)));
372 DEBUG(0,("add_privilege: could not Realloc memory to add a new privilege\n"));
376 new_set[priv_set->count].luid.high=set.luid.high;
377 new_set[priv_set->count].luid.low=set.luid.low;
378 new_set[priv_set->count].attr=set.attr;
381 priv_set->set=new_set;
386 /****************************************************************************
387 add all the privileges to a privilege array
388 ****************************************************************************/
389 BOOL add_all_privilege(PRIVILEGE_SET *priv_set)
396 set.luid.low=SE_PRIV_ADD_USERS;
397 add_privilege(priv_set, set);
399 set.luid.low=SE_PRIV_ADD_MACHINES;
400 add_privilege(priv_set, set);
402 set.luid.low=SE_PRIV_PRINT_OPERATOR;
403 add_privilege(priv_set, set);
408 /****************************************************************************
409 check if the privilege list is empty
410 ****************************************************************************/
411 BOOL check_empty_privilege(PRIVILEGE_SET *priv_set)
413 return (priv_set->count == 0);
416 /****************************************************************************
417 check if the privilege is in the privilege list
418 ****************************************************************************/
419 BOOL check_priv_in_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
423 /* if the list is empty, obviously we can't have it */
424 if (check_empty_privilege(priv_set))
427 for (i=0; i<priv_set->count; i++) {
430 cur_set=&priv_set->set[i];
431 /* check only the low and high part. Checking the attr field has no meaning */
432 if( (cur_set->luid.low==set.luid.low) && (cur_set->luid.high==set.luid.high) )
439 /****************************************************************************
440 remove a privilege to a privilege array
441 ****************************************************************************/
442 BOOL remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
448 /* check if the privilege is in the list */
449 if (!check_priv_in_privilege(priv_set, set))
452 /* special case if it's the only privilege in the list */
453 if (priv_set->count==1) {
454 free_privilege(priv_set);
455 init_privilege(priv_set);
461 * the privilege is there, create a new list,
462 * and copy the other privileges
465 old_set=priv_set->set;
467 new_set=(LUID_ATTR *)malloc((priv_set->count-1)*(sizeof(LUID_ATTR)));
469 DEBUG(0,("remove_privilege: could not malloc memory for new privilege list\n"));
473 for (i=0, j=0; i<priv_set->count; i++) {
474 if ((old_set[i].luid.low==set.luid.low) &&
475 (old_set[i].luid.high==set.luid.high)) {
479 new_set[j].luid.low=old_set[i].luid.low;
480 new_set[j].luid.high=old_set[i].luid.high;
481 new_set[j].attr=old_set[i].attr;
486 if (j!=priv_set->count-1) {
487 DEBUG(0,("remove_privilege: mismatch ! difference is not -1\n"));
488 DEBUGADD(0,("old count:%d, new count:%d\n", priv_set->count, j));
493 /* ok everything is fine */
496 priv_set->set=new_set;
503 /****************************************************************************
504 return the sid and the type of the unix group
505 ****************************************************************************/
506 BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
515 if(!init_group_mapping()) {
516 DEBUG(0,("failed to initialize group mapping"));
520 /* the key is the SID, retrieving is direct */
522 sid_to_string(string_sid, &sid);
523 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
526 kbuf.dsize = strlen(key)+1;
528 dbuf = tdb_fetch(tdb, kbuf);
529 if (!dbuf.dptr) return False;
531 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
532 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
536 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
538 DEBUG(10,("get_group_map_from_sid: %d privileges\n", map->priv_set.count));
542 set->set=(LUID_ATTR *)smb_xmalloc(set->count*sizeof(LUID_ATTR));
545 for (i=0; i<set->count; i++)
546 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
547 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
549 SAFE_FREE(dbuf.dptr);
550 if (ret != dbuf.dsize) {
551 DEBUG(0,("get_group_map_from_sid: group mapping TDB corrupted ?\n"));
556 /* we don't want the privileges */
557 if (with_priv==MAPPING_WITHOUT_PRIV)
560 sid_copy(&map->sid, &sid);
566 /****************************************************************************
567 return the sid and the type of the unix group
568 ****************************************************************************/
569 BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map, BOOL with_priv)
571 TDB_DATA kbuf, dbuf, newkey;
577 if(!init_group_mapping()) {
578 DEBUG(0,("failed to initialize group mapping"));
582 /* we need to enumerate the TDB to find the GID */
584 for (kbuf = tdb_firstkey(tdb);
586 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
588 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
590 dbuf = tdb_fetch(tdb, kbuf);
591 if (!dbuf.dptr) continue;
593 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
595 string_to_sid(&map->sid, string_sid);
597 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
598 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
601 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
604 set->set=(LUID_ATTR *)smb_xmalloc(set->count*sizeof(LUID_ATTR));
607 for (i=0; i<set->count; i++)
608 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
609 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
611 SAFE_FREE(dbuf.dptr);
612 if (ret != dbuf.dsize){
619 free_privilege(&map->priv_set);
629 /****************************************************************************
630 return the sid and the type of the unix group
631 ****************************************************************************/
632 BOOL get_group_map_from_ntname(char *name, GROUP_MAP *map, BOOL with_priv)
634 TDB_DATA kbuf, dbuf, newkey;
640 if(!init_group_mapping()) {
641 DEBUG(0,("failed to initialize group mapping"));
645 /* we need to enumerate the TDB to find the name */
647 for (kbuf = tdb_firstkey(tdb);
649 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
651 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
653 dbuf = tdb_fetch(tdb, kbuf);
654 if (!dbuf.dptr) continue;
656 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
658 string_to_sid(&map->sid, string_sid);
660 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
661 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
664 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
666 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
667 if (set->set==NULL) {
668 DEBUG(0,("get_group_map_from_ntname: could not allocate memory for privileges\n"));
672 for (i=0; i<set->count; i++)
673 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
674 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
676 SAFE_FREE(dbuf.dptr);
677 if (ret != dbuf.dsize) {
682 if (StrCaseCmp(name, map->nt_name)==0) {
684 free_privilege(&map->priv_set);
694 /****************************************************************************
695 remove a group mapping entry
696 ****************************************************************************/
697 BOOL group_map_remove(DOM_SID sid)
703 if(!init_group_mapping()) {
704 DEBUG(0,("failed to initialize group mapping"));
708 /* the key is the SID, retrieving is direct */
710 sid_to_string(string_sid, &sid);
711 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
714 kbuf.dsize = strlen(key)+1;
716 dbuf = tdb_fetch(tdb, kbuf);
717 if (!dbuf.dptr) return False;
719 SAFE_FREE(dbuf.dptr);
721 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
728 /****************************************************************************
729 enumerate the group mapping
730 ****************************************************************************/
731 BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
732 int *num_entries, BOOL unix_only, BOOL with_priv)
734 TDB_DATA kbuf, dbuf, newkey;
744 if(!init_group_mapping()) {
745 DEBUG(0,("failed to initialize group mapping"));
752 for (kbuf = tdb_firstkey(tdb);
754 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
756 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
759 dbuf = tdb_fetch(tdb, kbuf);
763 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
765 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
766 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment, &map.systemaccount);
771 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
774 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
775 if (set->set==NULL) {
776 DEBUG(0,("enum_group_mapping: could not allocate memory for privileges\n"));
781 for (i=0; i<set->count; i++)
782 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
783 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
785 SAFE_FREE(dbuf.dptr);
786 if (ret != dbuf.dsize) {
787 DEBUG(11,("enum_group_mapping: error in memory size\n"));
792 /* list only the type or everything if UNKNOWN */
793 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
794 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
799 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
800 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
805 string_to_sid(&map.sid, string_sid);
807 decode_sid_name_use(group_type, map.sid_name_use);
808 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map.nt_name ,group_type));
810 mapt=(GROUP_MAP *)Realloc((*rmap), (entries+1)*sizeof(GROUP_MAP));
812 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
820 mapt[entries].gid = map.gid;
821 sid_copy( &mapt[entries].sid, &map.sid);
822 mapt[entries].sid_name_use = map.sid_name_use;
823 fstrcpy(mapt[entries].nt_name, map.nt_name);
824 fstrcpy(mapt[entries].comment, map.comment);
825 mapt[entries].systemaccount=map.systemaccount;
826 mapt[entries].priv_set.count=set->count;
827 mapt[entries].priv_set.control=set->control;
828 mapt[entries].priv_set.set=set->set;
830 free_privilege(&(mapt[entries].priv_set));
835 *num_entries=entries;
840 /****************************************************************************
841 convert a privilege string to a privilege array
842 ****************************************************************************/
843 void convert_priv_from_text(PRIVILEGE_SET *se_priv, char *privilege)
850 /* By default no privilege */
851 init_privilege(se_priv);
856 while(next_token(&p, tok, " ", sizeof(tok)) ) {
857 for (i=0; i<=PRIV_ALL_INDEX; i++) {
858 if (StrCaseCmp(privs[i].priv, tok)==0) {
861 set.luid.low=privs[i].se_priv;
862 add_privilege(se_priv, set);
868 /****************************************************************************
869 convert a privilege array to a privilege string
870 ****************************************************************************/
871 void convert_priv_to_text(PRIVILEGE_SET *se_priv, char *privilege)
878 ZERO_STRUCTP(privilege);
880 if (check_empty_privilege(se_priv)) {
881 fstrcat(privilege, "No privilege");
885 for(i=0; i<se_priv->count; i++) {
887 while (privs[j].se_priv!=se_priv->set[i].luid.low && j<=PRIV_ALL_INDEX) {
891 fstrcat(privilege, privs[j].priv);
892 fstrcat(privilege, " ");
899 * High level functions
900 * better to use them than the lower ones.
902 * we are checking if the group is in the mapping file
903 * and if the group is an existing unix group
907 /* get a domain group from it's SID */
909 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
913 if(!init_group_mapping()) {
914 DEBUG(0,("failed to initialize group mapping"));
918 DEBUG(10, ("get_domain_group_from_sid\n"));
920 /* if the group is NOT in the database, it CAN NOT be a domain group */
921 if(!get_group_map_from_sid(sid, map, with_priv))
924 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
926 /* if it's not a domain group, continue */
927 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
929 free_privilege(&map->priv_set);
933 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
937 free_privilege(&map->priv_set);
941 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%d\n",map->gid));
943 if ( (grp=getgrgid(map->gid)) == NULL) {
944 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
946 free_privilege(&map->priv_set);
950 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
956 /* get a local (alias) group from it's SID */
958 BOOL get_local_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
962 if(!init_group_mapping()) {
963 DEBUG(0,("failed to initialize group mapping"));
967 /* The group is in the mapping table */
968 if(get_group_map_from_sid(sid, map, with_priv)) {
969 if (map->sid_name_use!=SID_NAME_ALIAS) {
971 free_privilege(&map->priv_set);
977 free_privilege(&map->priv_set);
981 if ( (grp=getgrgid(map->gid)) == NULL) {
983 free_privilege(&map->priv_set);
987 /* the group isn't in the mapping table.
988 * make one based on the unix information */
991 sid_peek_rid(&sid, &alias_rid);
992 map->gid=pdb_group_rid_to_gid(alias_rid);
994 if ((grp=getgrgid(map->gid)) == NULL)
997 map->sid_name_use=SID_NAME_ALIAS;
998 map->systemaccount=PR_ACCESS_FROM_NETWORK;
1000 fstrcpy(map->nt_name, grp->gr_name);
1001 fstrcpy(map->comment, "Local Unix Group");
1003 init_privilege(&map->priv_set);
1005 sid_copy(&map->sid, &sid);
1011 /* get a builtin group from it's SID */
1013 BOOL get_builtin_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
1017 if(!init_group_mapping()) {
1018 DEBUG(0,("failed to initialize group mapping"));
1022 if(!get_group_map_from_sid(sid, map, with_priv))
1025 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
1027 free_privilege(&map->priv_set);
1033 free_privilege(&map->priv_set);
1037 if ( (grp=getgrgid(map->gid)) == NULL) {
1039 free_privilege(&map->priv_set);
1048 /****************************************************************************
1049 Returns a GROUP_MAP struct based on the gid.
1050 ****************************************************************************/
1051 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map, BOOL with_priv)
1055 if(!init_group_mapping()) {
1056 DEBUG(0,("failed to initialize group mapping"));
1060 if ( (grp=getgrgid(gid)) == NULL)
1064 * make a group map from scratch if doesn't exist.
1066 if (!get_group_map_from_gid(gid, map, with_priv)) {
1068 map->sid_name_use=SID_NAME_ALIAS;
1069 map->systemaccount=PR_ACCESS_FROM_NETWORK;
1070 init_privilege(&map->priv_set);
1072 /* interim solution until we have a last RID allocated */
1074 sid_copy(&map->sid, &global_sam_sid);
1075 sid_append_rid(&map->sid, pdb_gid_to_group_rid(gid));
1077 fstrcpy(map->nt_name, grp->gr_name);
1078 fstrcpy(map->comment, "Local Unix Group");
1087 /****************************************************************************
1088 Get the member users of a group and
1089 all the users who have that group as primary.
1091 give back an array of uid
1092 return the grand number of users
1095 TODO: sort the list and remove duplicate. JFM.
1097 ****************************************************************************/
1099 BOOL get_uid_list_of_group(gid_t gid, uid_t **uid, int *num_uids)
1107 if(!init_group_mapping()) {
1108 DEBUG(0,("failed to initialize group mapping"));
1115 if ( (grp=getgrgid(gid)) == NULL)
1118 gr = grp->gr_mem[0];
1119 DEBUG(10, ("getting members\n"));
1121 while (gr && (*gr != (char)'\0')) {
1122 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
1124 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
1129 if( (pwd=getpwnam_alloc(gr)) !=NULL) {
1130 (*uid)[*num_uids]=pwd->pw_uid;
1134 gr = grp->gr_mem[++i];
1136 DEBUG(10, ("got [%d] members\n", *num_uids));
1139 while ((pwd=getpwent()) != NULL) {
1140 if (pwd->pw_gid==gid) {
1141 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
1143 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
1147 (*uid)[*num_uids]=pwd->pw_uid;
1153 DEBUG(10, ("got primary groups, members: [%d]\n", *num_uids));
1158 /****************************************************************************
1159 Create a UNIX group on demand.
1160 ****************************************************************************/
1162 int smb_create_group(char *unix_group)
1167 pstrcpy(add_script, lp_addgroup_script());
1168 if (! *add_script) return -1;
1169 pstring_sub(add_script, "%g", unix_group);
1170 ret = smbrun(add_script,NULL);
1171 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
1175 /****************************************************************************
1176 Delete a UNIX group on demand.
1177 ****************************************************************************/
1179 int smb_delete_group(char *unix_group)
1184 pstrcpy(del_script, lp_delgroup_script());
1185 if (! *del_script) return -1;
1186 pstring_sub(del_script, "%g", unix_group);
1187 ret = smbrun(del_script,NULL);
1188 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
1192 /****************************************************************************
1193 Create a UNIX group on demand.
1194 ****************************************************************************/
1196 int smb_add_user_group(char *unix_group, char *unix_user)
1201 pstrcpy(add_script, lp_addusertogroup_script());
1202 if (! *add_script) return -1;
1203 pstring_sub(add_script, "%g", unix_group);
1204 pstring_sub(add_script, "%u", unix_user);
1205 ret = smbrun(add_script,NULL);
1206 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1210 /****************************************************************************
1211 Delete a UNIX group on demand.
1212 ****************************************************************************/
1214 int smb_delete_user_group(const char *unix_group, const char *unix_user)
1219 pstrcpy(del_script, lp_deluserfromgroup_script());
1220 if (! *del_script) return -1;
1221 pstring_sub(del_script, "%g", unix_group);
1222 pstring_sub(del_script, "%u", unix_user);
1223 ret = smbrun(del_script,NULL);
1224 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));