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 int DEBUGLEVEL
;
26 extern DOM_SID global_sam_sid
;
28 static TDB_CONTEXT
*tdb
; /* used for driver files */
30 #define DATABASE_VERSION 1
31 #define GROUP_PREFIX "UNIXGROUP/"
34 {SE_PRIV_NONE
, "no_privs", "No privilege"},
35 {SE_PRIV_ADD_USERS
, "add_users", "add users"},
36 {SE_PRIV_ADD_MACHINES
, "add_computers", ""},
37 {SE_PRIV_PRINT_OPERATOR
, "print_op", ""},
38 {SE_PRIV_ALL
, "all_privs", ""}
42 { 2, "SeCreateTokenPrivilege" },
43 { 3, "SeAssignPrimaryTokenPrivilege" },
44 { 4, "SeLockMemoryPrivilege" },
45 { 5, "SeIncreaseQuotaPrivilege" },
46 { 6, "SeMachineAccountPrivilege" },
47 { 7, "SeTcbPrivilege" },
48 { 8, "SeSecurityPrivilege" },
49 { 9, "SeTakeOwnershipPrivilege" },
50 { 10, "SeLoadDriverPrivilege" },
51 { 11, "SeSystemProfilePrivilege" },
52 { 12, "SeSystemtimePrivilege" },
53 { 13, "SeProfileSingleProcessPrivilege" },
54 { 14, "SeIncreaseBasePriorityPrivilege" },
55 { 15, "SeCreatePagefilePrivilege" },
56 { 16, "SeCreatePermanentPrivilege" },
57 { 17, "SeBackupPrivilege" },
58 { 18, "SeRestorePrivilege" },
59 { 19, "SeShutdownPrivilege" },
60 { 20, "SeDebugPrivilege" },
61 { 21, "SeAuditPrivilege" },
62 { 22, "SeSystemEnvironmentPrivilege" },
63 { 23, "SeChangeNotifyPrivilege" },
64 { 24, "SeRemoteShutdownPrivilege" },
69 /****************************************************************************
70 check if the user has the required privilege.
71 ****************************************************************************/
72 static BOOL
se_priv_access_check(NT_USER_TOKEN
*token
, uint32 privilege
)
74 /* no token, no privilege */
78 if ((token
->privilege
& privilege
)==privilege
)
85 /****************************************************************************
86 dump the mapping group mapping to a text file
87 ****************************************************************************/
88 char *decode_sid_name_use(fstring group
, enum SID_NAME_USE name_use
)
90 static fstring group_type
;
94 fstrcpy(group_type
,"User");
96 case SID_NAME_DOM_GRP
:
97 fstrcpy(group_type
,"Domain group");
100 fstrcpy(group_type
,"Domain");
103 fstrcpy(group_type
,"Local group");
105 case SID_NAME_WKN_GRP
:
106 fstrcpy(group_type
,"Builtin group");
108 case SID_NAME_DELETED
:
109 fstrcpy(group_type
,"Deleted");
111 case SID_NAME_INVALID
:
112 fstrcpy(group_type
,"Invalid");
114 case SID_NAME_UNKNOWN
:
116 fstrcpy(group_type
,"Unknown type");
120 fstrcpy(group
, group_type
);
124 /****************************************************************************
125 open the group mapping tdb
126 ****************************************************************************/
127 BOOL
init_group_mapping(void)
129 static pid_t local_pid
;
130 char *vstring
= "INFO/version";
132 if (tdb
&& local_pid
== sys_getpid()) return True
;
133 tdb
= tdb_open_log(lock_path("group_mapping.tdb"), 0, 0, O_RDWR
|O_CREAT
, 0600);
135 DEBUG(0,("Failed to open group mapping database\n"));
139 local_pid
= sys_getpid();
141 /* handle a Samba upgrade */
142 tdb_lock_bystring(tdb
, vstring
);
143 if (tdb_fetch_int(tdb
, vstring
) != DATABASE_VERSION
) {
144 tdb_traverse(tdb
, (tdb_traverse_func
)tdb_delete
, NULL
);
145 tdb_store_int(tdb
, vstring
, DATABASE_VERSION
);
147 tdb_unlock_bystring(tdb
, vstring
);
153 /****************************************************************************
154 ****************************************************************************/
155 BOOL
add_mapping_entry(GROUP_MAP
*map
, int flag
)
162 sid_to_string(string_sid
, &map
->sid
);
164 len
= tdb_pack(buf
, sizeof(buf
), "ddffd",
165 map
->gid
, map
->sid_name_use
, map
->nt_name
, map
->comment
, map
->privilege
);
167 if (len
> sizeof(buf
)) return False
;
169 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
171 kbuf
.dsize
= strlen(key
)+1;
175 if (tdb_store(tdb
, kbuf
, dbuf
, flag
) != 0) return False
;
180 /****************************************************************************
181 initialise first time the mapping list
182 ****************************************************************************/
183 BOOL
add_initial_entry(gid_t gid
, fstring sid
, enum SID_NAME_USE sid_name_use
,
184 fstring nt_name
, fstring comment
, uint32 privilege
)
189 string_to_sid(&map
.sid
, sid
);
190 map
.sid_name_use
=sid_name_use
;
191 fstrcpy(map
.nt_name
, nt_name
);
192 fstrcpy(map
.comment
, comment
);
193 map
.privilege
=privilege
;
195 add_mapping_entry(&map
, TDB_INSERT
);
200 /****************************************************************************
201 initialise first time the mapping list
202 ****************************************************************************/
203 BOOL
default_group_mapping(void)
213 /* Add the Wellknown groups */
215 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP
, "Administrators", "", SE_PRIV_ALL
);
216 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP
, "Users", "", SE_PRIV_NONE
);
217 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP
, "Guests", "", SE_PRIV_NONE
);
218 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP
, "Power Users", "", SE_PRIV_NONE
);
220 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP
, "Account Operators", "", SE_PRIV_NONE
);
221 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP
, "System Operators", "", SE_PRIV_NONE
);
222 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP
, "Print Operators", "", SE_PRIV_PRINT_OPERATOR
);
223 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP
, "Backup Operators", "", SE_PRIV_NONE
);
225 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP
, "Replicators", "", SE_PRIV_NONE
);
227 /* Add the defaults domain groups */
229 sid_copy(&sid_admins
, &global_sam_sid
);
230 sid_append_rid(&sid_admins
, DOMAIN_GROUP_RID_ADMINS
);
231 sid_to_string(str_admins
, &sid_admins
);
232 add_initial_entry(-1, str_admins
, SID_NAME_DOM_GRP
, "Domain Admins", "", SE_PRIV_ALL
);
234 sid_copy(&sid_users
, &global_sam_sid
);
235 sid_append_rid(&sid_users
, DOMAIN_GROUP_RID_USERS
);
236 sid_to_string(str_users
, &sid_users
);
237 add_initial_entry(-1, str_users
, SID_NAME_DOM_GRP
, "Domain Users", "", SE_PRIV_NONE
);
239 sid_copy(&sid_guests
, &global_sam_sid
);
240 sid_append_rid(&sid_guests
, DOMAIN_GROUP_RID_GUESTS
);
241 sid_to_string(str_guests
, &sid_guests
);
242 add_initial_entry(-1, str_guests
, SID_NAME_DOM_GRP
, "Domain Guests", "", SE_PRIV_NONE
);
248 /****************************************************************************
249 return the sid and the type of the unix group
250 ****************************************************************************/
251 BOOL
get_group_map_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
258 /* the key is the SID, retrieving is direct */
260 sid_to_string(string_sid
, &sid
);
261 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
264 kbuf
.dsize
= strlen(key
)+1;
266 dbuf
= tdb_fetch(tdb
, kbuf
);
267 if (!dbuf
.dptr
) return False
;
269 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddffd",
270 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
, &map
->privilege
);
272 safe_free(dbuf
.dptr
);
273 if (ret
!= dbuf
.dsize
) {
274 DEBUG(0,("get_group_map_from_sid: mapping TDB corrupted ?\n"));
278 sid_copy(&map
->sid
, &sid
);
284 /****************************************************************************
285 return the sid and the type of the unix group
286 ****************************************************************************/
287 BOOL
get_group_map_from_gid(gid_t gid
, GROUP_MAP
*map
)
289 TDB_DATA kbuf
, dbuf
, newkey
;
293 /* we need to enumerate the TDB to find the GID */
295 for (kbuf
= tdb_firstkey(tdb
);
297 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
299 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0) continue;
301 dbuf
= tdb_fetch(tdb
, kbuf
);
302 if (!dbuf
.dptr
) continue;
304 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
306 string_to_sid(&map
->sid
, string_sid
);
308 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddffd",
309 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
, &map
->privilege
);
311 safe_free(dbuf
.dptr
);
312 if (ret
!= dbuf
.dsize
) continue;
321 /****************************************************************************
322 return the sid and the type of the unix group
323 ****************************************************************************/
324 BOOL
get_group_map_from_ntname(char *name
, GROUP_MAP
*map
)
326 TDB_DATA kbuf
, dbuf
, newkey
;
330 /* we need to enumerate the TDB to find the GID */
332 for (kbuf
= tdb_firstkey(tdb
);
334 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
336 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0) continue;
338 dbuf
= tdb_fetch(tdb
, kbuf
);
339 if (!dbuf
.dptr
) continue;
341 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
343 string_to_sid(&map
->sid
, string_sid
);
345 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddffd",
346 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
, &map
->comment
, &map
->privilege
);
348 safe_free(dbuf
.dptr
);
349 if (ret
!= dbuf
.dsize
) continue;
351 if (StrCaseCmp(name
, map
->nt_name
)==0)
359 /****************************************************************************
360 enumerate the group mapping
361 ****************************************************************************/
362 BOOL
group_map_remove(DOM_SID sid
)
368 /* the key is the SID, retrieving is direct */
370 sid_to_string(string_sid
, &sid
);
371 slprintf(key
, sizeof(key
), "%s%s", GROUP_PREFIX
, string_sid
);
374 kbuf
.dsize
= strlen(key
)+1;
376 dbuf
= tdb_fetch(tdb
, kbuf
);
377 if (!dbuf
.dptr
) return False
;
379 safe_free(dbuf
.dptr
);
381 if(tdb_delete(tdb
, kbuf
) != TDB_SUCCESS
)
388 /****************************************************************************
389 enumerate the group mapping
390 ****************************************************************************/
391 BOOL
enum_group_mapping(enum SID_NAME_USE sid_name_use
, GROUP_MAP
**rmap
,
392 int *num_entries
, BOOL unix_only
)
394 TDB_DATA kbuf
, dbuf
, newkey
;
398 GROUP_MAP
*mapt
=NULL
;
405 for (kbuf
= tdb_firstkey(tdb
);
407 newkey
= tdb_nextkey(tdb
, kbuf
), safe_free(kbuf
.dptr
), kbuf
=newkey
) {
409 if (strncmp(kbuf
.dptr
, GROUP_PREFIX
, strlen(GROUP_PREFIX
)) != 0)
412 dbuf
= tdb_fetch(tdb
, kbuf
);
416 fstrcpy(string_sid
, kbuf
.dptr
+strlen(GROUP_PREFIX
));
418 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddffd",
419 &map
.gid
, &map
.sid_name_use
, &map
.nt_name
, &map
.comment
, &map
.privilege
);
421 safe_free(dbuf
.dptr
);
422 if (ret
!= dbuf
.dsize
)
425 /* list only the type or everything if UNKNOWN */
426 if (sid_name_use
!=SID_NAME_UNKNOWN
&& sid_name_use
!=map
.sid_name_use
)
429 if (unix_only
==ENUM_ONLY_MAPPED
&& map
.gid
==-1)
432 string_to_sid(&map
.sid
, string_sid
);
434 decode_sid_name_use(group_type
, map
.sid_name_use
);
436 mapt
=(GROUP_MAP
*)Realloc(mapt
, (entries
+1)*sizeof(GROUP_MAP
));
438 mapt
[entries
].gid
= map
.gid
;
439 sid_copy( &mapt
[entries
].sid
, &map
.sid
);
440 mapt
[entries
].sid_name_use
= map
.sid_name_use
;
441 fstrcpy(mapt
[entries
].nt_name
, map
.nt_name
);
442 fstrcpy(mapt
[entries
].comment
, map
.comment
);
443 mapt
[entries
].privilege
= map
.privilege
;
449 *num_entries
=entries
;
454 /****************************************************************************
455 convert a privilege list to a privilege value
456 ****************************************************************************/
457 void convert_priv_from_text(uint32
*se_priv
, char *privilege
)
463 /* By default no privilege */
469 while(next_token(&p
, tok
, " ", sizeof(tok
)) ) {
470 for (i
=0; i
<=PRIV_ALL_INDEX
; i
++) {
471 if (StrCaseCmp(privs
[i
].priv
, tok
)==0)
472 (*se_priv
)+=privs
[i
].se_priv
;
477 /****************************************************************************
478 convert a privilege value to a privilege list
479 ****************************************************************************/
480 void convert_priv_to_text(uint32 se_priv
, char *privilege
)
487 ZERO_STRUCTP(privilege
);
489 if (se_priv
==SE_PRIV_NONE
) {
490 fstrcat(privilege
, privs
[0].priv
);
494 if (se_priv
==SE_PRIV_ALL
) {
495 fstrcat(privilege
, privs
[PRIV_ALL_INDEX
].priv
);
499 for (i
=1; privs
[i
].se_priv
!=SE_PRIV_ALL
; i
++) {
500 if ( (se_priv
& privs
[i
].se_priv
) == privs
[i
].se_priv
) {
501 fstrcat(privilege
, privs
[i
].priv
);
502 fstrcat(privilege
, " ");
510 * High level functions
511 * better to use them than the lower ones.
513 * we are checking if the group is in the mapping file
514 * and if the group is an existing unix group
518 /* get a domain group from it's SID */
520 BOOL
get_domain_group_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
524 DEBUG(10, ("get_domain_group_from_sid\n"));
526 /* if the group is NOT in the database, it CAN NOT be a domain group */
527 if(!get_group_map_from_sid(sid
, map
))
530 DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
532 /* if it's not a domain group, continue */
533 if (map
->sid_name_use
!=SID_NAME_DOM_GRP
)
536 DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
541 DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%d\n",map
->gid
));
543 if ( (grp
=getgrgid(map
->gid
)) == NULL
)
546 DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
552 /* get a local (alias) group from it's SID */
554 BOOL
get_local_group_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
558 /* The group is in the mapping table */
559 if(get_group_map_from_sid(sid
, map
)) {
560 if (map
->sid_name_use
!=SID_NAME_ALIAS
)
566 if ( (grp
=getgrgid(map
->gid
)) == NULL
)
569 /* the group isn't in the mapping table.
570 * make one based on the unix information */
573 sid_split_rid(&sid
, &alias_rid
);
574 map
->gid
=pdb_user_rid_to_gid(alias_rid
);
576 if ((grp
=getgrgid(map
->gid
)) == NULL
)
579 map
->sid_name_use
=SID_NAME_ALIAS
;
581 fstrcpy(map
->nt_name
, grp
->gr_name
);
582 fstrcpy(map
->comment
, "Local Unix Group");
584 map
->privilege
=SE_PRIV_NONE
;
591 /* get a builtin group from it's SID */
593 BOOL
get_builtin_group_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
597 if(!get_group_map_from_sid(sid
, map
))
600 if (map
->sid_name_use
!=SID_NAME_WKN_GRP
)
606 if ( (grp
=getgrgid(map
->gid
)) == NULL
)
614 /****************************************************************************
615 Returns a GROUP_MAP struct based on the gid.
616 ****************************************************************************/
617 BOOL
get_group_from_gid(gid_t gid
, GROUP_MAP
*map
)
621 if ( (grp
=getgrgid(gid
)) == NULL
)
625 * make a group map from scratch if doesn't exist.
627 if (!get_group_map_from_gid(gid
, map
)) {
629 map
->sid_name_use
=SID_NAME_ALIAS
;
630 map
->privilege
=SE_PRIV_NONE
;
632 sid_copy(&map
->sid
, &global_sam_sid
);
633 sid_append_rid(&map
->sid
, pdb_gid_to_group_rid(gid
));
635 fstrcpy(map
->nt_name
, grp
->gr_name
);
636 fstrcpy(map
->comment
, "Local Unix Group");
645 /****************************************************************************
646 Get the member users of a group and
647 all the users who have that group as primary.
649 give back an array of uid
650 return the grand number of users
653 TODO: sort the list and remove duplicate. JFM.
655 ****************************************************************************/
657 BOOL
get_uid_list_of_group(gid_t gid
, uid_t
**uid
, int *num_uids
)
667 if ( (grp
=getgrgid(gid
)) == NULL
)
671 DEBUG(10, ("getting members\n"));
673 while (gr
&& (*gr
!= (char)'\0')) {
674 (*uid
)=Realloc((*uid
), sizeof(uid_t
)*(*num_uids
+1));
676 if( (pwd
=getpwnam(gr
)) !=NULL
) {
677 (*uid
)[*num_uids
]=pwd
->pw_uid
;
680 gr
= grp
->gr_mem
[++i
];
682 DEBUG(10, ("got [%d] members\n", *num_uids
));
685 while ((pwd
=getpwent()) != NULL
) {
686 if (pwd
->pw_gid
==gid
) {
687 (*uid
)=Realloc((*uid
), sizeof(uid_t
)*(*num_uids
+1));
688 (*uid
)[*num_uids
]=pwd
->pw_uid
;
694 DEBUG(10, ("got primary groups, members: [%d]\n", *num_uids
));
699 /****************************************************************************
700 Create a UNIX group on demand.
701 ****************************************************************************/
703 int smb_create_group(char *unix_group
)
708 pstrcpy(add_script
, lp_addgroup_script());
709 if (! *add_script
) return -1;
710 pstring_sub(add_script
, "%g", unix_group
);
711 ret
= smbrun(add_script
,NULL
);
712 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script
,ret
));
716 /****************************************************************************
717 Delete a UNIX group on demand.
718 ****************************************************************************/
720 int smb_delete_group(char *unix_group
)
725 pstrcpy(del_script
, lp_delgroup_script());
726 if (! *del_script
) return -1;
727 pstring_sub(del_script
, "%g", unix_group
);
728 ret
= smbrun(del_script
,NULL
);
729 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script
,ret
));
733 /****************************************************************************
734 Create a UNIX group on demand.
735 ****************************************************************************/
737 int smb_add_user_group(char *unix_group
, char *unix_user
)
742 pstrcpy(add_script
, lp_addusertogroup_script());
743 if (! *add_script
) return -1;
744 pstring_sub(add_script
, "%g", unix_group
);
745 pstring_sub(add_script
, "%u", unix_user
);
746 ret
= smbrun(add_script
,NULL
);
747 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script
,ret
));
751 /****************************************************************************
752 Delete a UNIX group on demand.
753 ****************************************************************************/
755 int smb_delete_user_group(char *unix_group
, char *unix_user
)
760 pstrcpy(del_script
, lp_deluserfromgroup_script());
761 if (! *del_script
) return -1;
762 pstring_sub(del_script
, "%g", unix_group
);
763 pstring_sub(del_script
, "%u", unix_user
);
764 ret
= smbrun(del_script
,NULL
);
765 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script
,ret
));