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));
534 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
535 if (set->set==NULL) {
536 DEBUG(0,("get_group_map_from_sid: could not allocate memory for privileges\n"));
540 for (i=0; i<set->count; i++)
541 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
542 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
544 SAFE_FREE(dbuf.dptr);
545 if (ret != dbuf.dsize) {
546 DEBUG(0,("get_group_map_from_sid: group mapping TDB corrupted ?\n"));
551 /* we don't want the privileges */
552 if (with_priv==MAPPING_WITHOUT_PRIV)
555 sid_copy(&map->sid, &sid);
561 /****************************************************************************
562 return the sid and the type of the unix group
563 ****************************************************************************/
564 BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map, BOOL with_priv)
566 TDB_DATA kbuf, dbuf, newkey;
572 init_group_mapping();
574 /* we need to enumerate the TDB to find the GID */
576 for (kbuf = tdb_firstkey(tdb);
578 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
580 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
582 dbuf = tdb_fetch(tdb, kbuf);
583 if (!dbuf.dptr) continue;
585 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
587 string_to_sid(&map->sid, string_sid);
589 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
590 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
593 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
595 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
596 if (set->set==NULL) {
597 DEBUG(0,("get_group_map_from_gid: could not allocate memory for privileges\n"));
601 for (i=0; i<set->count; i++)
602 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
603 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
605 SAFE_FREE(dbuf.dptr);
606 if (ret != dbuf.dsize){
613 free_privilege(&map->priv_set);
623 /****************************************************************************
624 return the sid and the type of the unix group
625 ****************************************************************************/
626 BOOL get_group_map_from_ntname(char *name, GROUP_MAP *map, BOOL with_priv)
628 TDB_DATA kbuf, dbuf, newkey;
634 init_group_mapping();
636 /* we need to enumerate the TDB to find the name */
638 for (kbuf = tdb_firstkey(tdb);
640 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
642 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
644 dbuf = tdb_fetch(tdb, kbuf);
645 if (!dbuf.dptr) continue;
647 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
649 string_to_sid(&map->sid, string_sid);
651 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
652 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->systemaccount);
655 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
657 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
658 if (set->set==NULL) {
659 DEBUG(0,("get_group_map_from_ntname: could not allocate memory for privileges\n"));
663 for (i=0; i<set->count; i++)
664 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
665 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
667 SAFE_FREE(dbuf.dptr);
668 if (ret != dbuf.dsize) {
673 if (StrCaseCmp(name, map->nt_name)==0) {
675 free_privilege(&map->priv_set);
685 /****************************************************************************
686 remove a group mapping entry
687 ****************************************************************************/
688 BOOL group_map_remove(DOM_SID sid)
694 init_group_mapping();
696 /* the key is the SID, retrieving is direct */
698 sid_to_string(string_sid, &sid);
699 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
702 kbuf.dsize = strlen(key)+1;
704 dbuf = tdb_fetch(tdb, kbuf);
705 if (!dbuf.dptr) return False;
707 SAFE_FREE(dbuf.dptr);
709 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
716 /****************************************************************************
717 enumerate the group mapping
718 ****************************************************************************/
719 BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
720 int *num_entries, BOOL unix_only, BOOL with_priv)
722 TDB_DATA kbuf, dbuf, newkey;
732 init_group_mapping();
737 for (kbuf = tdb_firstkey(tdb);
739 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
741 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
744 dbuf = tdb_fetch(tdb, kbuf);
748 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
750 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
751 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment, &map.systemaccount);
756 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "d", &set->count);
759 set->set=(LUID_ATTR *)malloc(set->count*sizeof(LUID_ATTR));
760 if (set->set==NULL) {
761 DEBUG(0,("enum_group_mapping: could not allocate memory for privileges\n"));
766 for (i=0; i<set->count; i++)
767 ret += tdb_unpack(dbuf.dptr+ret, dbuf.dsize-ret, "ddd",
768 &(set->set[i].luid.low), &(set->set[i].luid.high), &(set->set[i].attr));
770 SAFE_FREE(dbuf.dptr);
771 if (ret != dbuf.dsize) {
772 DEBUG(11,("enum_group_mapping: error in memory size\n"));
777 /* list only the type or everything if UNKNOWN */
778 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
779 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
784 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
785 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
790 string_to_sid(&map.sid, string_sid);
792 decode_sid_name_use(group_type, map.sid_name_use);
793 DEBUG(11,("enum_group_mapping: returning group %s of type %s\n", map.nt_name ,group_type));
795 mapt=(GROUP_MAP *)Realloc((*rmap), (entries+1)*sizeof(GROUP_MAP));
797 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
805 mapt[entries].gid = map.gid;
806 sid_copy( &mapt[entries].sid, &map.sid);
807 mapt[entries].sid_name_use = map.sid_name_use;
808 fstrcpy(mapt[entries].nt_name, map.nt_name);
809 fstrcpy(mapt[entries].comment, map.comment);
810 mapt[entries].systemaccount=map.systemaccount;
811 mapt[entries].priv_set.count=set->count;
812 mapt[entries].priv_set.control=set->control;
813 mapt[entries].priv_set.set=set->set;
815 free_privilege(&(mapt[entries].priv_set));
820 *num_entries=entries;
825 /****************************************************************************
826 convert a privilege string to a privilege array
827 ****************************************************************************/
828 void convert_priv_from_text(PRIVILEGE_SET *se_priv, char *privilege)
835 /* By default no privilege */
836 init_privilege(se_priv);
841 while(next_token(&p, tok, " ", sizeof(tok)) ) {
842 for (i=0; i<=PRIV_ALL_INDEX; i++) {
843 if (StrCaseCmp(privs[i].priv, tok)==0) {
846 set.luid.low=privs[i].se_priv;
847 add_privilege(se_priv, set);
853 /****************************************************************************
854 convert a privilege array to a privilege string
855 ****************************************************************************/
856 void convert_priv_to_text(PRIVILEGE_SET *se_priv, char *privilege)
863 ZERO_STRUCTP(privilege);
865 if (check_empty_privilege(se_priv)) {
866 fstrcat(privilege, "No privilege");
870 for(i=0; i<se_priv->count; i++) {
872 while (privs[j].se_priv!=se_priv->set[i].luid.low && j<=PRIV_ALL_INDEX) {
876 fstrcat(privilege, privs[j].priv);
877 fstrcat(privilege, " ");
884 * High level functions
885 * better to use them than the lower ones.
887 * we are checking if the group is in the mapping file
888 * and if the group is an existing unix group
892 /* get a domain group from it's SID */
894 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
898 DEBUG(10, ("get_domain_group_from_sid\n"));
900 /* if the group is NOT in the database, it CAN NOT be a domain group */
901 if(!get_group_map_from_sid(sid, map, with_priv))
904 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
906 /* if it's not a domain group, continue */
907 if (map->sid_name_use!=SID_NAME_DOM_GRP) {
909 free_privilege(&map->priv_set);
913 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
917 free_privilege(&map->priv_set);
921 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%d\n",map->gid));
923 if ( (grp=getgrgid(map->gid)) == NULL) {
924 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
926 free_privilege(&map->priv_set);
930 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
936 /* get a local (alias) group from it's SID */
938 BOOL get_local_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
942 /* The group is in the mapping table */
943 if(get_group_map_from_sid(sid, map, with_priv)) {
944 if (map->sid_name_use!=SID_NAME_ALIAS) {
946 free_privilege(&map->priv_set);
952 free_privilege(&map->priv_set);
956 if ( (grp=getgrgid(map->gid)) == NULL) {
958 free_privilege(&map->priv_set);
962 /* the group isn't in the mapping table.
963 * make one based on the unix information */
966 sid_peek_rid(&sid, &alias_rid);
967 map->gid=pdb_group_rid_to_gid(alias_rid);
969 if ((grp=getgrgid(map->gid)) == NULL)
972 map->sid_name_use=SID_NAME_ALIAS;
973 map->systemaccount=PR_ACCESS_FROM_NETWORK;
975 fstrcpy(map->nt_name, grp->gr_name);
976 fstrcpy(map->comment, "Local Unix Group");
978 init_privilege(&map->priv_set);
980 sid_copy(&map->sid, &sid);
986 /* get a builtin group from it's SID */
988 BOOL get_builtin_group_from_sid(DOM_SID sid, GROUP_MAP *map, BOOL with_priv)
992 if(!get_group_map_from_sid(sid, map, with_priv))
995 if (map->sid_name_use!=SID_NAME_WKN_GRP) {
997 free_privilege(&map->priv_set);
1003 free_privilege(&map->priv_set);
1007 if ( (grp=getgrgid(map->gid)) == NULL) {
1009 free_privilege(&map->priv_set);
1018 /****************************************************************************
1019 Returns a GROUP_MAP struct based on the gid.
1020 ****************************************************************************/
1021 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map, BOOL with_priv)
1025 if ( (grp=getgrgid(gid)) == NULL)
1029 * make a group map from scratch if doesn't exist.
1031 if (!get_group_map_from_gid(gid, map, with_priv)) {
1033 map->sid_name_use=SID_NAME_ALIAS;
1034 map->systemaccount=PR_ACCESS_FROM_NETWORK;
1035 init_privilege(&map->priv_set);
1037 /* interim solution until we have a last RID allocated */
1039 sid_copy(&map->sid, &global_sam_sid);
1040 sid_append_rid(&map->sid, pdb_gid_to_group_rid(gid));
1042 fstrcpy(map->nt_name, grp->gr_name);
1043 fstrcpy(map->comment, "Local Unix Group");
1052 /****************************************************************************
1053 Get the member users of a group and
1054 all the users who have that group as primary.
1056 give back an array of uid
1057 return the grand number of users
1060 TODO: sort the list and remove duplicate. JFM.
1062 ****************************************************************************/
1064 BOOL get_uid_list_of_group(gid_t gid, uid_t **uid, int *num_uids)
1075 if ( (grp=getgrgid(gid)) == NULL)
1078 gr = grp->gr_mem[0];
1079 DEBUG(10, ("getting members\n"));
1081 while (gr && (*gr != (char)'\0')) {
1082 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
1084 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
1089 if( (pwd=getpwnam(gr)) !=NULL) {
1090 (*uid)[*num_uids]=pwd->pw_uid;
1093 gr = grp->gr_mem[++i];
1095 DEBUG(10, ("got [%d] members\n", *num_uids));
1098 while ((pwd=getpwent()) != NULL) {
1099 if (pwd->pw_gid==gid) {
1100 u = Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
1102 DEBUG(0,("get_uid_list_of_group: unable to enlarge uid list!\n"));
1106 (*uid)[*num_uids]=pwd->pw_uid;
1112 DEBUG(10, ("got primary groups, members: [%d]\n", *num_uids));
1117 /****************************************************************************
1118 Create a UNIX group on demand.
1119 ****************************************************************************/
1121 int smb_create_group(char *unix_group)
1126 pstrcpy(add_script, lp_addgroup_script());
1127 if (! *add_script) return -1;
1128 pstring_sub(add_script, "%g", unix_group);
1129 ret = smbrun(add_script,NULL);
1130 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
1134 /****************************************************************************
1135 Delete a UNIX group on demand.
1136 ****************************************************************************/
1138 int smb_delete_group(char *unix_group)
1143 pstrcpy(del_script, lp_delgroup_script());
1144 if (! *del_script) return -1;
1145 pstring_sub(del_script, "%g", unix_group);
1146 ret = smbrun(del_script,NULL);
1147 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
1151 /****************************************************************************
1152 Create a UNIX group on demand.
1153 ****************************************************************************/
1155 int smb_add_user_group(char *unix_group, char *unix_user)
1160 pstrcpy(add_script, lp_addusertogroup_script());
1161 if (! *add_script) return -1;
1162 pstring_sub(add_script, "%g", unix_group);
1163 pstring_sub(add_script, "%u", unix_user);
1164 ret = smbrun(add_script,NULL);
1165 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
1169 /****************************************************************************
1170 Delete a UNIX group on demand.
1171 ****************************************************************************/
1173 int smb_delete_user_group(const char *unix_group, const char *unix_user)
1178 pstrcpy(del_script, lp_deluserfromgroup_script());
1179 if (! *del_script) return -1;
1180 pstring_sub(del_script, "%g", unix_group);
1181 pstring_sub(del_script, "%u", unix_user);
1182 ret = smbrun(del_script,NULL);
1183 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));