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 ****************************************************************************/
148 BOOL init_group_mapping(void)
150 static pid_t local_pid;
151 char *vstring = "INFO/version";
154 if (tdb && local_pid == sys_getpid())
156 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
158 DEBUG(0,("Failed to open group mapping database\n"));
162 local_pid = sys_getpid();
164 /* handle a Samba upgrade */
165 tdb_lock_bystring(tdb, vstring);
167 /* Cope with byte-reversed older versions of the db. */
168 vers_id = tdb_fetch_int32(tdb, vstring);
169 if ((vers_id != DATABASE_VERSION) && (IREV(vers_id) == DATABASE_VERSION)) {
170 /* Written on a bigendian machine with old fetch_int code. Save as le. */
171 tdb_store_int32(tdb, vstring, DATABASE_VERSION);
172 vers_id = DATABASE_VERSION;
175 if (vers_id != DATABASE_VERSION) {
176 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
177 tdb_store_int32(tdb, vstring, DATABASE_VERSION);
180 tdb_unlock_bystring(tdb, vstring);
182 /* write a list of default groups */
183 if(!default_group_mapping())
189 /****************************************************************************
190 ****************************************************************************/
191 BOOL add_mapping_entry(GROUP_MAP *map, int flag)
195 fstring string_sid="";
200 init_group_mapping();
202 sid_to_string(string_sid, &map->sid);
204 len = tdb_pack(buf, sizeof(buf), "ddffd",
205 map->gid, map->sid_name_use, map->nt_name, map->comment, map->systemaccount);
207 /* write the privilege list in the TDB database */
210 len += tdb_pack(buf+len, sizeof(buf)-len, "d", set->count);
211 for (i=0; i<set->count; i++)
212 len += tdb_pack(buf+len, sizeof(buf)-len, "ddd",
213 set->set[i].luid.low, set->set[i].luid.high, set->set[i].attr);
215 if (len > sizeof(buf))
218 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
220 kbuf.dsize = strlen(key)+1;
224 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
229 /****************************************************************************
230 initialise first time the mapping list
231 ****************************************************************************/
232 BOOL add_initial_entry(gid_t gid, fstring sid, enum SID_NAME_USE sid_name_use,
233 fstring nt_name, fstring comment, PRIVILEGE_SET priv_set, uint32 systemaccount)
238 string_to_sid(&map.sid, sid);
239 map.sid_name_use=sid_name_use;
240 fstrcpy(map.nt_name, nt_name);
241 fstrcpy(map.comment, comment);
242 map.systemaccount=systemaccount;
244 map.priv_set.count=priv_set.count;
245 map.priv_set.set=priv_set.set;
247 add_mapping_entry(&map, TDB_INSERT);
252 /****************************************************************************
253 initialise a privilege list
254 ****************************************************************************/
255 void init_privilege(PRIVILEGE_SET *priv_set)
262 /****************************************************************************
263 free a privilege list
264 ****************************************************************************/
265 BOOL free_privilege(PRIVILEGE_SET *priv_set)
267 if (priv_set->count==0) {
268 DEBUG(100,("free_privilege: count=0, nothing to clear ?\n"));
272 if (priv_set->set==NULL) {
273 DEBUG(0,("free_privilege: list ptr is NULL, very strange !\n"));
277 safe_free(priv_set->set);
285 /****************************************************************************
286 add a privilege to a privilege array
287 ****************************************************************************/
288 BOOL add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
292 /* check if the privilege is not already in the list */
293 if (check_priv_in_privilege(priv_set, set))
296 /* we can allocate memory to add the new privilege */
298 new_set=(LUID_ATTR *)Realloc(priv_set->set, (priv_set->count+1)*(sizeof(LUID_ATTR)));
300 DEBUG(0,("add_privilege: could not Realloc memory to add a new privilege\n"));
304 new_set[priv_set->count].luid.high=set.luid.high;
305 new_set[priv_set->count].luid.low=set.luid.low;
306 new_set[priv_set->count].attr=set.attr;
309 priv_set->set=new_set;
314 /****************************************************************************
315 add all the privileges to a privilege array
316 ****************************************************************************/
317 BOOL add_all_privilege(PRIVILEGE_SET *priv_set)
324 set.luid.low=SE_PRIV_ADD_USERS;
325 add_privilege(priv_set, set);
327 set.luid.low=SE_PRIV_ADD_MACHINES;
328 add_privilege(priv_set, set);
330 set.luid.low=SE_PRIV_PRINT_OPERATOR;
331 add_privilege(priv_set, set);
336 /****************************************************************************
337 check if the privilege list is empty
338 ****************************************************************************/
339 BOOL check_empty_privilege(PRIVILEGE_SET *priv_set)
342 if (priv_set->count!=0)
348 /****************************************************************************
349 check if the privilege is in the privilege list
350 ****************************************************************************/
351 BOOL check_priv_in_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
355 /* if the list is empty, obviously we can't have it */
356 if (check_empty_privilege(priv_set))
359 for (i=0; i<priv_set->count; i++) {
362 cur_set=&priv_set->set[i];
363 /* check only the low and high part. Checking the attr field has no meaning */
364 if( (cur_set->luid.low==set.luid.low) && (cur_set->luid.high==set.luid.high) )
371 /****************************************************************************
372 remove a privilege to a privilege array
373 ****************************************************************************/
374 BOOL remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
380 /* check if the privilege is in the list */
381 if (!check_priv_in_privilege(priv_set, set))
384 /* special case if it's the only privilege in the list */
385 if (priv_set->count==1) {
386 free_privilege(priv_set);
387 init_privilege(priv_set);
393 * the privilege is there, create a new list,
394 * and copy the other privileges
397 old_set=priv_set->set;
399 new_set=(LUID_ATTR *)malloc((priv_set->count-1)*(sizeof(LUID_ATTR)));
401 DEBUG(0,("remove_privilege: could not malloc memory for new privilege list\n"));
405 for (i=0, j=0; i<priv_set->count; i++) {
406 if ((old_set[i].luid.low==set.luid.low) &&
407 (old_set[i].luid.high==set.luid.high)) {
411 new_set[j].luid.low=old_set[i].luid.low;
412 new_set[j].luid.high=old_set[i].luid.high;
413 new_set[j].attr=old_set[i].attr;
418 if (j!=priv_set->count-1) {
419 DEBUG(0,("remove_privilege: mismatch ! difference is not -1\n"));
420 DEBUGADD(0,("old count:%d, new count:%d\n", priv_set->count, j));
425 /* ok everything is fine */
428 priv_set->set=new_set;
435 /****************************************************************************
436 initialise first time the mapping list
437 ****************************************************************************/
438 BOOL default_group_mapping(void)
448 PRIVILEGE_SET privilege_none;
449 PRIVILEGE_SET privilege_all;
450 PRIVILEGE_SET privilege_print_op;
452 init_privilege(&privilege_none);
453 init_privilege(&privilege_all);
454 init_privilege(&privilege_print_op);
458 set.luid.low=SE_PRIV_PRINT_OPERATOR;
459 add_privilege(&privilege_print_op, set);
461 add_all_privilege(&privilege_all);
463 /* Add the Wellknown groups */
465 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_ALIAS, "Administrators", "", privilege_all, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
466 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_ALIAS, "Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
467 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_ALIAS, "Guests", "", privilege_none, PR_ACCESS_FROM_NETWORK);
468 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_ALIAS, "Power Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
470 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_ALIAS, "Account Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
471 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_ALIAS, "System Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
472 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);
473 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_ALIAS, "Backup Operators", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
475 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_ALIAS, "Replicators", "", privilege_none, PR_ACCESS_FROM_NETWORK);
477 /* Add the defaults domain groups */
479 sid_copy(&sid_admins, &global_sam_sid);
480 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
481 sid_to_string(str_admins, &sid_admins);
482 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "", privilege_all, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
484 sid_copy(&sid_users, &global_sam_sid);
485 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
486 sid_to_string(str_users, &sid_users);
487 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "", privilege_none, PR_ACCESS_FROM_NETWORK|PR_LOG_ON_LOCALLY);
489 sid_copy(&sid_guests, &global_sam_sid);
490 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
491 sid_to_string(str_guests, &sid_guests);
492 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "", privilege_none, PR_ACCESS_FROM_NETWORK);
498 /****************************************************************************
499 return the sid and the type of the unix group
500 ****************************************************************************/
501 BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
510 init_group_mapping();
512 /* the key is the SID, retrieving is direct */
514 sid_to_string(string_sid, &sid);
515 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
518 kbuf.dsize = strlen(key)+1;
520 dbuf = tdb_fetch(tdb, kbuf);
521 if (!dbuf.dptr) return False;
523 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
524 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
528 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
530 DEBUG(10,("get_group_map_from_sid: %d privileges\n", map->priv_set.count));
532 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
533 if (set->set==NULL) {
534 DEBUG(0,("get_group_map_from_sid: could not allocate memory for privileges\n"));
538 for (i=0; i<set->count; i++)
539 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
540 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
542 SAFE_FREE(dbuf.dptr);
543 if (ret != dbuf.dsize) {
544 DEBUG(0,("get_group_map_from_sid: group mapping TDB corrupted ?\n"));
549 /* we don't want the privileges */
550 if (with_priv==MAPPING_WITHOUT_PRIV)
553 sid_copy(&map->sid, &sid);
559 /****************************************************************************
560 return the sid and the type of the unix group
561 ****************************************************************************/
562 BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map, BOOL with_priv)
564 TDB_DATA kbuf, dbuf, newkey;
570 init_group_mapping();
572 /* we need to enumerate the TDB to find the GID */
574 for (kbuf = tdb_firstkey(tdb);
576 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
578 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
580 dbuf = tdb_fetch(tdb, kbuf);
581 if (!dbuf.dptr) continue;
583 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
585 string_to_sid(&map->sid, string_sid);
587 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
588 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
591 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
593 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
594 if (set->set==NULL) {
595 DEBUG(0,("get_group_map_from_gid: could not allocate memory for privileges\n"));
599 for (i=0; i<set->count; i++)
600 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
601 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
603 SAFE_FREE(dbuf.dptr);
604 if (ret != dbuf.dsize){
611 free_privilege(&map->priv_set);
621 /****************************************************************************
622 return the sid and the type of the unix group
623 ****************************************************************************/
624 BOOL get_group_map_from_ntname(char *name, GROUP_MAP *map, BOOL with_priv)
626 TDB_DATA kbuf, dbuf, newkey;
632 init_group_mapping();
634 /* we need to enumerate the TDB to find the name */
636 for (kbuf = tdb_firstkey(tdb);
638 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
640 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
642 dbuf = tdb_fetch(tdb, kbuf);
643 if (!dbuf.dptr) continue;
645 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
647 string_to_sid(&map->sid, string_sid);
649 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
650 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
653 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
655 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
656 if (set->set==NULL) {
657 DEBUG(0,("get_group_map_from_ntname: could not allocate memory for privileges\n"));
661 for (i=0; i<set->count; i++)
662 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
663 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
665 SAFE_FREE(dbuf.dptr);
666 if (ret != dbuf.dsize) {
671 if (StrCaseCmp(name, map->nt_name)==0) {
673 free_privilege(&map->priv_set);
683 /****************************************************************************
684 remove a group mapping entry
685 ****************************************************************************/
686 BOOL group_map_remove(DOM_SID sid)
692 init_group_mapping();
694 /* the key is the SID, retrieving is direct */
696 sid_to_string(string_sid, &sid);
697 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
700 kbuf.dsize = strlen(key)+1;
702 dbuf = tdb_fetch(tdb, kbuf);
703 if (!dbuf.dptr) return False;
705 SAFE_FREE(dbuf.dptr);
707 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
714 /****************************************************************************
715 enumerate the group mapping
716 ****************************************************************************/
717 BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
718 int *num_entries, BOOL unix_only, BOOL with_priv)
720 TDB_DATA kbuf, dbuf, newkey;
730 init_group_mapping();
735 for (kbuf = tdb_firstkey(tdb);
737 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
739 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
742 dbuf = tdb_fetch(tdb, kbuf);
746 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
748 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
749 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment, &map.systemaccount);
754 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
757 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
758 if (set->set==NULL) {
759 DEBUG(0,("enum_group_mapping: could not allocate memory for privileges\n"));
764 for (i=0; i<set->count; i++)
765 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
766 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
768 SAFE_FREE(dbuf.dptr);
769 if (ret != dbuf.dsize) {
770 DEBUG(11,("enum_group_mapping: error in memory size\n"));
775 /* list only the type or everything if UNKNOWN */
776 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
777 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
782 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
783 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
788 string_to_sid(&map.sid, string_sid);
790 decode_sid_name_use(group_type, map.sid_name_use);
791 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map.nt_name ,group_type));
793 mapt=(GROUP_MAP *)Realloc((*rmap), (entries+1)*sizeof(GROUP_MAP));
795 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
803 mapt[entries].gid = map.gid;
804 sid_copy( &mapt[entries].sid, &map.sid);
805 mapt[entries].sid_name_use = map.sid_name_use;
806 fstrcpy(mapt[entries].nt_name, map.nt_name);
807 fstrcpy(mapt[entries].comment, map.comment);
808 mapt[entries].systemaccount=map.systemaccount;
809 mapt[entries].priv_set.count=set->count;
810 mapt[entries].priv_set.control=set->control;
811 mapt[entries].priv_set.set=set->set;
813 free_privilege(&(mapt[entries].priv_set));
818 *num_entries=entries;
823 /****************************************************************************
824 convert a privilege string to a privilege array
825 ****************************************************************************/
826 void convert_priv_from_text(PRIVILEGE_SET *se_priv, char *privilege)
833 /* By default no privilege */
834 init_privilege(se_priv);
839 while(next_token(&p, tok, " ", sizeof(tok)) ) {
840 for (i=0; i<=PRIV_ALL_INDEX; i++) {
841 if (StrCaseCmp(privs[i].priv, tok)==0) {
844 set.luid.low=privs[i].se_priv;
845 add_privilege(se_priv, set);
851 /****************************************************************************
852 convert a privilege array to a privilege string
853 ****************************************************************************/
854 void convert_priv_to_text(PRIVILEGE_SET *se_priv, char *privilege)
861 ZERO_STRUCTP(privilege);
863 if (check_empty_privilege(se_priv)) {
864 fstrcat(privilege, "No privilege");
868 for(i=0; i<se_priv->count; i++) {
870 while (privs[j].se_priv!=se_priv->set[i].luid.low && j<=PRIV_ALL_INDEX) {
874 fstrcat(privilege, privs[j].priv);
875 fstrcat(privilege, " ");
882 * High level functions
883 * better to use them than the lower ones.
885 * we are checking if the group is in the mapping file
886 * and if the group is an existing unix group
890 /* get a domain group from it's SID */
892 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
896 DEBUG(10, ("get_domain_group_from_sid\n"));
898 /* if the group is NOT in the database, it CAN NOT be a domain group */
899 if(!get_group_map_from_sid(sid, map, with_priv))
902 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
904 /* if it's not a domain group, continue */
905 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
907 free_privilege(&map->priv_set);
911 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
915 free_privilege(&map->priv_set);
919 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%d\n",map->gid));
921 if ( (grp=getgrgid(map->gid)) == NULL) {
922 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
924 free_privilege(&map->priv_set);
928 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
934 /* get a local (alias) group from it's SID */
936 BOOL get_local_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
940 /* The group is in the mapping table */
941 if(get_group_map_from_sid(sid, map, with_priv)) {
942 if (map->sid_name_use!=SID_NAME_ALIAS) {
944 free_privilege(&map->priv_set);
950 free_privilege(&map->priv_set);
954 if ( (grp=getgrgid(map->gid)) == NULL) {
956 free_privilege(&map->priv_set);
960 /* the group isn't in the mapping table.
961 * make one based on the unix information */
964 sid_peek_rid(&sid, &alias_rid);
965 map->gid=pdb_group_rid_to_gid(alias_rid);
967 if ((grp=getgrgid(map->gid)) == NULL)
970 map->sid_name_use=SID_NAME_ALIAS;
971 map->systemaccount=PR_ACCESS_FROM_NETWORK;
973 fstrcpy(map->nt_name, grp->gr_name);
974 fstrcpy(map->comment, "Local Unix Group");
976 init_privilege(&map->priv_set);
978 sid_copy(&map->sid, &sid);
984 /* get a builtin group from it's SID */
986 BOOL get_builtin_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
990 if(!get_group_map_from_sid(sid, map, with_priv))
993 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
995 free_privilege(&map->priv_set);
1001 free_privilege(&map->priv_set);
1005 if ( (grp=getgrgid(map->gid)) == NULL) {
1007 free_privilege(&map->priv_set);
1016 /****************************************************************************
1017 Returns a GROUP_MAP struct based on the gid.
1018 ****************************************************************************/
1019 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map, BOOL with_priv)
1023 if ( (grp=getgrgid(gid)) == NULL)
1027 * make a group map from scratch if doesn't exist.
1029 if (!get_group_map_from_gid(gid, map, with_priv)) {
1031 map->sid_name_use=SID_NAME_ALIAS;
1032 map->systemaccount=PR_ACCESS_FROM_NETWORK;
1033 init_privilege(&map->priv_set);
1035 /* interim solution until we have a last RID allocated */
1037 sid_copy(&map->sid, &global_sam_sid);
1038 sid_append_rid(&map->sid, pdb_gid_to_group_rid(gid));
1040 fstrcpy(map->nt_name, grp->gr_name);
1041 fstrcpy(map->comment, "Local Unix Group");
1050 /****************************************************************************
1051 Get the member users of a group and
1052 all the users who have that group as primary.
1054 give back an array of uid
1055 return the grand number of users
1058 TODO: sort the list and remove duplicate. JFM.
1060 ****************************************************************************/
1062 BOOL get_uid_list_of_group(gid_t gid, uid_t **uid, int *num_uids)
1073 if ( (grp=getgrgid(gid)) == NULL)
1076 gr = grp->gr_mem[0];
1077 DEBUG(10, ("getting members\n"));
1079 while (gr && (*gr != (char)'\0')) {
1080 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
1082 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
1087 if( (pwd=getpwnam(gr)) !=NULL) {
1088 (*uid)[*num_uids]=pwd->pw_uid;
1091 gr = grp->gr_mem[++i];
1093 DEBUG(10, ("got [%d] members\n", *num_uids));
1096 while ((pwd=getpwent()) != NULL) {
1097 if (pwd->pw_gid==gid) {
1098 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
1100 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
1104 (*uid)[*num_uids]=pwd->pw_uid;
1110 DEBUG(10, ("got primary groups, members: [%d]\n", *num_uids));
1115 /****************************************************************************
1116 Create a UNIX group on demand.
1117 ****************************************************************************/
1119 int smb_create_group(char *unix_group)
1124 pstrcpy(add_script, lp_addgroup_script());
1125 if (! *add_script) return -1;
1126 pstring_sub(add_script, "%g", unix_group);
1127 ret = smbrun(add_script,NULL);
1128 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
1132 /****************************************************************************
1133 Delete a UNIX group on demand.
1134 ****************************************************************************/
1136 int smb_delete_group(char *unix_group)
1141 pstrcpy(del_script, lp_delgroup_script());
1142 if (! *del_script) return -1;
1143 pstring_sub(del_script, "%g", unix_group);
1144 ret = smbrun(del_script,NULL);
1145 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
1149 /****************************************************************************
1150 Create a UNIX group on demand.
1151 ****************************************************************************/
1153 int smb_add_user_group(char *unix_group, char *unix_user)
1158 pstrcpy(add_script, lp_addusertogroup_script());
1159 if (! *add_script) return -1;
1160 pstring_sub(add_script, "%g", unix_group);
1161 pstring_sub(add_script, "%u", unix_user);
1162 ret = smbrun(add_script,NULL);
1163 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1167 /****************************************************************************
1168 Delete a UNIX group on demand.
1169 ****************************************************************************/
1171 int smb_delete_user_group(const char *unix_group, const char *unix_user)
1176 pstrcpy(del_script, lp_deluserfromgroup_script());
1177 if (! *del_script) return -1;
1178 pstring_sub(del_script, "%g", unix_group);
1179 pstring_sub(del_script, "%u", unix_user);
1180 ret = smbrun(del_script,NULL);
1181 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));