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 Open the group mapping tdb.
148 ****************************************************************************/
150 BOOL init_group_mapping(void)
152 static pid_t local_pid;
153 char *vstring = "INFO/version";
156 if (tdb && local_pid == sys_getpid())
158 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
160 DEBUG(0,("Failed to open group mapping database\n"));
164 local_pid = sys_getpid();
166 /* handle a Samba upgrade */
167 tdb_lock_bystring(tdb, vstring);
169 /* Cope with byte-reversed older versions of the db. */
170 vers_id = tdb_fetch_int32(tdb, vstring);
171 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
172 /* Written on a bigendian machine with old fetch_int code. Save as le. */
173 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
174 vers_id = DATABASE_VERSION_V2;
177 if (vers_id != DATABASE_VERSION_V2) {
178 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
179 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
182 tdb_unlock_bystring(tdb, vstring);
184 /* write a list of default groups */
185 if(!default_group_mapping())
191 /****************************************************************************
192 ****************************************************************************/
193 BOOL add_mapping_entry(GROUP_MAP *map, int flag)
197 fstring string_sid="";
202 init_group_mapping();
204 sid_to_string(string_sid, &map->sid);
206 len = tdb_pack(buf, sizeof(buf), "ddffd",
207 map->gid, map->sid_name_use, map->nt_name, map->comment, map->systemaccount);
209 /* write the privilege list in the TDB database */
212 len += tdb_pack(buf+len, sizeof(buf)-len, "d", set->count);
213 for (i=0; i<set->count; i++)
214 len += tdb_pack(buf+len, sizeof(buf)-len, "ddd",
215 set->set[i].luid.low, set->set[i].luid.high, set->set[i].attr);
217 if (len > sizeof(buf))
220 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
222 kbuf.dsize = strlen(key)+1;
226 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
231 /****************************************************************************
232 initialise first time the mapping list
233 ****************************************************************************/
234 BOOL add_initial_entry(gid_t gid, fstring sid, enum SID_NAME_USE sid_name_use,
235 fstring nt_name, fstring comment, PRIVILEGE_SET priv_set, uint32 systemaccount)
240 string_to_sid(&map.sid, sid);
241 map.sid_name_use=sid_name_use;
242 fstrcpy(map.nt_name, nt_name);
243 fstrcpy(map.comment, comment);
244 map.systemaccount=systemaccount;
246 map.priv_set.count=priv_set.count;
247 map.priv_set.set=priv_set.set;
249 add_mapping_entry(&map, TDB_INSERT);
254 /****************************************************************************
255 initialise a privilege list
256 ****************************************************************************/
257 void init_privilege(PRIVILEGE_SET *priv_set)
264 /****************************************************************************
265 free a privilege list
266 ****************************************************************************/
267 BOOL free_privilege(PRIVILEGE_SET *priv_set)
269 if (priv_set->count==0) {
270 DEBUG(100,("free_privilege: count=0, nothing to clear ?\n"));
274 if (priv_set->set==NULL) {
275 DEBUG(0,("free_privilege: list ptr is NULL, very strange !\n"));
279 safe_free(priv_set->set);
287 /****************************************************************************
288 add a privilege to a privilege array
289 ****************************************************************************/
290 BOOL add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
294 /* check if the privilege is not already in the list */
295 if (check_priv_in_privilege(priv_set, set))
298 /* we can allocate memory to add the new privilege */
300 new_set=(LUID_ATTR *)Realloc(priv_set->set, (priv_set->count+1)*(sizeof(LUID_ATTR)));
302 DEBUG(0,("add_privilege: could not Realloc memory to add a new privilege\n"));
306 new_set[priv_set->count].luid.high=set.luid.high;
307 new_set[priv_set->count].luid.low=set.luid.low;
308 new_set[priv_set->count].attr=set.attr;
311 priv_set->set=new_set;
316 /****************************************************************************
317 add all the privileges to a privilege array
318 ****************************************************************************/
319 BOOL add_all_privilege(PRIVILEGE_SET *priv_set)
326 set.luid.low=SE_PRIV_ADD_USERS;
327 add_privilege(priv_set, set);
329 set.luid.low=SE_PRIV_ADD_MACHINES;
330 add_privilege(priv_set, set);
332 set.luid.low=SE_PRIV_PRINT_OPERATOR;
333 add_privilege(priv_set, set);
338 /****************************************************************************
339 check if the privilege list is empty
340 ****************************************************************************/
341 BOOL check_empty_privilege(PRIVILEGE_SET *priv_set)
344 if (priv_set->count!=0)
350 /****************************************************************************
351 check if the privilege is in the privilege list
352 ****************************************************************************/
353 BOOL check_priv_in_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
357 /* if the list is empty, obviously we can't have it */
358 if (check_empty_privilege(priv_set))
361 for (i=0; i<priv_set->count; i++) {
364 cur_set=&priv_set->set[i];
365 /* check only the low and high part. Checking the attr field has no meaning */
366 if( (cur_set->luid.low==set.luid.low) && (cur_set->luid.high==set.luid.high) )
373 /****************************************************************************
374 remove a privilege to a privilege array
375 ****************************************************************************/
376 BOOL remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
382 /* check if the privilege is in the list */
383 if (!check_priv_in_privilege(priv_set, set))
386 /* special case if it's the only privilege in the list */
387 if (priv_set->count==1) {
388 free_privilege(priv_set);
389 init_privilege(priv_set);
395 * the privilege is there, create a new list,
396 * and copy the other privileges
399 old_set=priv_set->set;
401 new_set=(LUID_ATTR *)malloc((priv_set->count-1)*(sizeof(LUID_ATTR)));
403 DEBUG(0,("remove_privilege: could not malloc memory for new privilege list\n"));
407 for (i=0, j=0; i<priv_set->count; i++) {
408 if ((old_set[i].luid.low==set.luid.low) &&
409 (old_set[i].luid.high==set.luid.high)) {
413 new_set[j].luid.low=old_set[i].luid.low;
414 new_set[j].luid.high=old_set[i].luid.high;
415 new_set[j].attr=old_set[i].attr;
420 if (j!=priv_set->count-1) {
421 DEBUG(0,("remove_privilege: mismatch ! difference is not -1\n"));
422 DEBUGADD(0,("old count:%d, new count:%d\n", priv_set->count, j));
427 /* ok everything is fine */
430 priv_set->set=new_set;
437 /****************************************************************************
438 initialise first time the mapping list
439 ****************************************************************************/
440 BOOL default_group_mapping(void)
450 PRIVILEGE_SET privilege_none;
451 PRIVILEGE_SET privilege_all;
452 PRIVILEGE_SET privilege_print_op;
454 init_privilege(&privilege_none);
455 init_privilege(&privilege_all);
456 init_privilege(&privilege_print_op);
460 set.luid.low=SE_PRIV_PRINT_OPERATOR;
461 add_privilege(&privilege_print_op, set);
463 add_all_privilege(&privilege_all);
465 /* Add the Wellknown groups */
467 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_ALIAS, "Administrators", "", privilege_all, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
468 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_ALIAS, "Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
469 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_ALIAS, "Guests", "", privilege_none, PR_ACCESS_FROM_NETWORK);
470 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_ALIAS, "Power Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
472 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_ALIAS, "Account Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
473 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_ALIAS, "System Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
474 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);
475 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_ALIAS, "Backup Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
477 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_ALIAS, "Replicators", "", privilege_none, PR_ACCESS_FROM_NETWORK);
479 /* Add the defaults domain groups */
481 sid_copy(&sid_admins, &global_sam_sid);
482 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
483 sid_to_string(str_admins, &sid_admins);
484 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "", privilege_all, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
486 sid_copy(&sid_users, &global_sam_sid);
487 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
488 sid_to_string(str_users, &sid_users);
489 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
491 sid_copy(&sid_guests, &global_sam_sid);
492 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
493 sid_to_string(str_guests, &sid_guests);
494 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "", privilege_none, PR_ACCESS_FROM_NETWORK);
500 /****************************************************************************
501 return the sid and the type of the unix group
502 ****************************************************************************/
503 BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
512 init_group_mapping();
514 /* the key is the SID, retrieving is direct */
516 sid_to_string(string_sid, &sid);
517 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
520 kbuf.dsize = strlen(key)+1;
522 dbuf = tdb_fetch(tdb, kbuf);
523 if (!dbuf.dptr) return False;
525 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
526 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
530 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
532 DEBUG(10,("get_group_map_from_sid: %d privileges\n", map->priv_set.count));
536 set->set=(LUID_ATTR *)smb_xmalloc(set->count*sizeof(LUID_ATTR));
539 for (i=0; i<set->count; i++)
540 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
541 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
543 SAFE_FREE(dbuf.dptr);
544 if (ret != dbuf.dsize) {
545 DEBUG(0,("get_group_map_from_sid: group mapping TDB corrupted ?\n"));
550 /* we don't want the privileges */
551 if (with_priv==MAPPING_WITHOUT_PRIV)
554 sid_copy(&map->sid, &sid);
560 /****************************************************************************
561 return the sid and the type of the unix group
562 ****************************************************************************/
563 BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map, BOOL with_priv)
565 TDB_DATA kbuf, dbuf, newkey;
571 init_group_mapping();
573 /* we need to enumerate the TDB to find the GID */
575 for (kbuf = tdb_firstkey(tdb);
577 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
579 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
581 dbuf = tdb_fetch(tdb, kbuf);
582 if (!dbuf.dptr) continue;
584 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
586 string_to_sid(&map->sid, string_sid);
588 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
589 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
592 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
595 set->set=(LUID_ATTR *)smb_xmalloc(set->count*sizeof(LUID_ATTR));
598 for (i=0; i<set->count; i++)
599 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
600 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
602 SAFE_FREE(dbuf.dptr);
603 if (ret != dbuf.dsize){
610 free_privilege(&map->priv_set);
620 /****************************************************************************
621 return the sid and the type of the unix group
622 ****************************************************************************/
623 BOOL get_group_map_from_ntname(char *name, GROUP_MAP *map, BOOL with_priv)
625 TDB_DATA kbuf, dbuf, newkey;
631 init_group_mapping();
633 /* we need to enumerate the TDB to find the name */
635 for (kbuf = tdb_firstkey(tdb);
637 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
639 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
641 dbuf = tdb_fetch(tdb, kbuf);
642 if (!dbuf.dptr) continue;
644 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
646 string_to_sid(&map->sid, string_sid);
648 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
649 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
652 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
654 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
655 if (set->set==NULL) {
656 DEBUG(0,("get_group_map_from_ntname: could not allocate memory for privileges\n"));
660 for (i=0; i<set->count; i++)
661 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
662 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
664 SAFE_FREE(dbuf.dptr);
665 if (ret != dbuf.dsize) {
670 if (StrCaseCmp(name, map->nt_name)==0) {
672 free_privilege(&map->priv_set);
682 /****************************************************************************
683 remove a group mapping entry
684 ****************************************************************************/
685 BOOL group_map_remove(DOM_SID sid)
691 init_group_mapping();
693 /* the key is the SID, retrieving is direct */
695 sid_to_string(string_sid, &sid);
696 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
699 kbuf.dsize = strlen(key)+1;
701 dbuf = tdb_fetch(tdb, kbuf);
702 if (!dbuf.dptr) return False;
704 SAFE_FREE(dbuf.dptr);
706 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
713 /****************************************************************************
714 enumerate the group mapping
715 ****************************************************************************/
716 BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
717 int *num_entries, BOOL unix_only, BOOL with_priv)
719 TDB_DATA kbuf, dbuf, newkey;
729 init_group_mapping();
734 for (kbuf = tdb_firstkey(tdb);
736 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
738 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
741 dbuf = tdb_fetch(tdb, kbuf);
745 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
747 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
748 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment, &map.systemaccount);
753 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
756 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
757 if (set->set==NULL) {
758 DEBUG(0,("enum_group_mapping: could not allocate memory for privileges\n"));
763 for (i=0; i<set->count; i++)
764 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
765 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
767 SAFE_FREE(dbuf.dptr);
768 if (ret != dbuf.dsize) {
769 DEBUG(11,("enum_group_mapping: error in memory size\n"));
774 /* list only the type or everything if UNKNOWN */
775 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
776 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
781 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
782 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
787 string_to_sid(&map.sid, string_sid);
789 decode_sid_name_use(group_type, map.sid_name_use);
790 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map.nt_name ,group_type));
792 mapt=(GROUP_MAP *)Realloc((*rmap), (entries+1)*sizeof(GROUP_MAP));
794 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
802 mapt[entries].gid = map.gid;
803 sid_copy( &mapt[entries].sid, &map.sid);
804 mapt[entries].sid_name_use = map.sid_name_use;
805 fstrcpy(mapt[entries].nt_name, map.nt_name);
806 fstrcpy(mapt[entries].comment, map.comment);
807 mapt[entries].systemaccount=map.systemaccount;
808 mapt[entries].priv_set.count=set->count;
809 mapt[entries].priv_set.control=set->control;
810 mapt[entries].priv_set.set=set->set;
812 free_privilege(&(mapt[entries].priv_set));
817 *num_entries=entries;
822 /****************************************************************************
823 convert a privilege string to a privilege array
824 ****************************************************************************/
825 void convert_priv_from_text(PRIVILEGE_SET *se_priv, char *privilege)
832 /* By default no privilege */
833 init_privilege(se_priv);
838 while(next_token(&p, tok, " ", sizeof(tok)) ) {
839 for (i=0; i<=PRIV_ALL_INDEX; i++) {
840 if (StrCaseCmp(privs[i].priv, tok)==0) {
843 set.luid.low=privs[i].se_priv;
844 add_privilege(se_priv, set);
850 /****************************************************************************
851 convert a privilege array to a privilege string
852 ****************************************************************************/
853 void convert_priv_to_text(PRIVILEGE_SET *se_priv, char *privilege)
860 ZERO_STRUCTP(privilege);
862 if (check_empty_privilege(se_priv)) {
863 fstrcat(privilege, "No privilege");
867 for(i=0; i<se_priv->count; i++) {
869 while (privs[j].se_priv!=se_priv->set[i].luid.low && j<=PRIV_ALL_INDEX) {
873 fstrcat(privilege, privs[j].priv);
874 fstrcat(privilege, " ");
881 * High level functions
882 * better to use them than the lower ones.
884 * we are checking if the group is in the mapping file
885 * and if the group is an existing unix group
889 /* get a domain group from it's SID */
891 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
895 DEBUG(10, ("get_domain_group_from_sid\n"));
897 /* if the group is NOT in the database, it CAN NOT be a domain group */
898 if(!get_group_map_from_sid(sid, map, with_priv))
901 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
903 /* if it's not a domain group, continue */
904 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
906 free_privilege(&map->priv_set);
910 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
914 free_privilege(&map->priv_set);
918 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%d\n",map->gid));
920 if ( (grp=getgrgid(map->gid)) == NULL) {
921 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
923 free_privilege(&map->priv_set);
927 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
933 /* get a local (alias) group from it's SID */
935 BOOL get_local_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
939 /* The group is in the mapping table */
940 if(get_group_map_from_sid(sid, map, with_priv)) {
941 if (map->sid_name_use!=SID_NAME_ALIAS) {
943 free_privilege(&map->priv_set);
949 free_privilege(&map->priv_set);
953 if ( (grp=getgrgid(map->gid)) == NULL) {
955 free_privilege(&map->priv_set);
959 /* the group isn't in the mapping table.
960 * make one based on the unix information */
963 sid_peek_rid(&sid, &alias_rid);
964 map->gid=pdb_group_rid_to_gid(alias_rid);
966 if ((grp=getgrgid(map->gid)) == NULL)
969 map->sid_name_use=SID_NAME_ALIAS;
970 map->systemaccount=PR_ACCESS_FROM_NETWORK;
972 fstrcpy(map->nt_name, grp->gr_name);
973 fstrcpy(map->comment, "Local Unix Group");
975 init_privilege(&map->priv_set);
977 sid_copy(&map->sid, &sid);
983 /* get a builtin group from it's SID */
985 BOOL get_builtin_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
989 if(!get_group_map_from_sid(sid, map, with_priv))
992 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
994 free_privilege(&map->priv_set);
1000 free_privilege(&map->priv_set);
1004 if ( (grp=getgrgid(map->gid)) == NULL) {
1006 free_privilege(&map->priv_set);
1015 /****************************************************************************
1016 Returns a GROUP_MAP struct based on the gid.
1017 ****************************************************************************/
1018 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map, BOOL with_priv)
1022 if ( (grp=getgrgid(gid)) == NULL)
1026 * make a group map from scratch if doesn't exist.
1028 if (!get_group_map_from_gid(gid, map, with_priv)) {
1030 map->sid_name_use=SID_NAME_ALIAS;
1031 map->systemaccount=PR_ACCESS_FROM_NETWORK;
1032 init_privilege(&map->priv_set);
1034 /* interim solution until we have a last RID allocated */
1036 sid_copy(&map->sid, &global_sam_sid);
1037 sid_append_rid(&map->sid, pdb_gid_to_group_rid(gid));
1039 fstrcpy(map->nt_name, grp->gr_name);
1040 fstrcpy(map->comment, "Local Unix Group");
1049 /****************************************************************************
1050 Get the member users of a group and
1051 all the users who have that group as primary.
1053 give back an array of uid
1054 return the grand number of users
1057 TODO: sort the list and remove duplicate. JFM.
1059 ****************************************************************************/
1061 BOOL get_uid_list_of_group(gid_t gid, uid_t **uid, int *num_uids)
1072 if ( (grp=getgrgid(gid)) == NULL)
1075 gr = grp->gr_mem[0];
1076 DEBUG(10, ("getting members\n"));
1078 while (gr && (*gr != (char)'\0')) {
1079 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
1081 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
1086 if( (pwd=getpwnam(gr)) !=NULL) {
1087 (*uid)[*num_uids]=pwd->pw_uid;
1090 gr = grp->gr_mem[++i];
1092 DEBUG(10, ("got [%d] members\n", *num_uids));
1095 while ((pwd=getpwent()) != NULL) {
1096 if (pwd->pw_gid==gid) {
1097 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
1099 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
1103 (*uid)[*num_uids]=pwd->pw_uid;
1109 DEBUG(10, ("got primary groups, members: [%d]\n", *num_uids));
1114 /****************************************************************************
1115 Create a UNIX group on demand.
1116 ****************************************************************************/
1118 int smb_create_group(char *unix_group)
1123 pstrcpy(add_script, lp_addgroup_script());
1124 if (! *add_script) return -1;
1125 pstring_sub(add_script, "%g", unix_group);
1126 ret = smbrun(add_script,NULL);
1127 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
1131 /****************************************************************************
1132 Delete a UNIX group on demand.
1133 ****************************************************************************/
1135 int smb_delete_group(char *unix_group)
1140 pstrcpy(del_script, lp_delgroup_script());
1141 if (! *del_script) return -1;
1142 pstring_sub(del_script, "%g", unix_group);
1143 ret = smbrun(del_script,NULL);
1144 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
1148 /****************************************************************************
1149 Create a UNIX group on demand.
1150 ****************************************************************************/
1152 int smb_add_user_group(char *unix_group, char *unix_user)
1157 pstrcpy(add_script, lp_addusertogroup_script());
1158 if (! *add_script) return -1;
1159 pstring_sub(add_script, "%g", unix_group);
1160 pstring_sub(add_script, "%u", unix_user);
1161 ret = smbrun(add_script,NULL);
1162 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1166 /****************************************************************************
1167 Delete a UNIX group on demand.
1168 ****************************************************************************/
1170 int smb_delete_user_group(const char *unix_group, const char *unix_user)
1175 pstrcpy(del_script, lp_deluserfromgroup_script());
1176 if (! *del_script) return -1;
1177 pstring_sub(del_script, "%g", unix_group);
1178 pstring_sub(del_script, "%u", unix_user);
1179 ret = smbrun(del_script,NULL);
1180 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));