2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Luke Kenneth Caseson Leighton 1998-1999
6 Copyright (C) Jeremy Allison 1999
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 #define MAX_SID_NAMES 7
27 typedef struct _known_sid_users
{
29 enum SID_NAME_USE sid_name_use
;
30 const char *known_user_name
;
33 static struct sid_name_map_info
37 const known_sid_users
*known_users
;
38 } sid_name_map
[MAX_SID_NAMES
];
40 extern DOM_SID global_sid_Builtin
; /* Local well-known domain */
41 extern DOM_SID global_sid_World_Domain
; /* Everyone domain */
42 extern DOM_SID global_sid_Creator_Owner_Domain
; /* Creator Owner domain */
43 extern DOM_SID global_sid_NT_Authority
; /* NT Authority */
46 static BOOL sid_name_map_initialized
= False
;
47 /* static known_sid_users no_users[] = {{0, 0, NULL}}; */
49 static const known_sid_users everyone_users
[] = {
50 { 0, SID_NAME_WKN_GRP
, "Everyone" },
51 {0, (enum SID_NAME_USE
)0, NULL
}};
53 static const known_sid_users creator_owner_users
[] = {
54 { 0, SID_NAME_WKN_GRP
, "Creator Owner" },
55 { 1, SID_NAME_WKN_GRP
, "Creator Group" },
56 {0, (enum SID_NAME_USE
)0, NULL
}};
58 static const known_sid_users nt_authority_users
[] = {
59 { 1, SID_NAME_ALIAS
, "Dialup" },
60 { 2, SID_NAME_ALIAS
, "Network"},
61 { 3, SID_NAME_ALIAS
, "Batch"},
62 { 4, SID_NAME_ALIAS
, "Interactive"},
63 { 6, SID_NAME_ALIAS
, "Service"},
64 { 7, SID_NAME_ALIAS
, "AnonymousLogon"},
65 { 8, SID_NAME_ALIAS
, "Proxy"},
66 { 9, SID_NAME_ALIAS
, "ServerLogon"},
67 { 11, SID_NAME_ALIAS
, "Authenticated Users"},
68 { 18, SID_NAME_ALIAS
, "SYSTEM"},
69 { 0, (enum SID_NAME_USE
)0, NULL
}};
71 static const known_sid_users builtin_groups
[] = {
72 { BUILTIN_ALIAS_RID_ADMINS
, SID_NAME_ALIAS
, "Administrators" },
73 { BUILTIN_ALIAS_RID_USERS
, SID_NAME_ALIAS
, "Users" },
74 { BUILTIN_ALIAS_RID_GUESTS
, SID_NAME_ALIAS
, "Guests" },
75 { BUILTIN_ALIAS_RID_ACCOUNT_OPS
, SID_NAME_ALIAS
, "Account Operators" },
76 { BUILTIN_ALIAS_RID_SYSTEM_OPS
, SID_NAME_ALIAS
, "Server Operators" },
77 { BUILTIN_ALIAS_RID_PRINT_OPS
, SID_NAME_ALIAS
, "Print Operators" },
78 { BUILTIN_ALIAS_RID_BACKUP_OPS
, SID_NAME_ALIAS
, "Backup Operators" },
79 { 0, (enum SID_NAME_USE
)0, NULL
}};
81 /**************************************************************************
83 *************************************************************************/
85 static void init_sid_name_map (void)
89 if (sid_name_map_initialized
) return;
91 generate_wellknown_sids();
93 if ((lp_security() == SEC_USER
) && lp_domain_logons()) {
94 sid_name_map
[i
].sid
= get_global_sam_sid();
95 /* This is not lp_workgroup() for good reason:
96 it must stay around longer than the lp_*()
98 sid_name_map
[i
].name
= strdup(lp_workgroup());
99 sid_name_map
[i
].known_users
= NULL
;
101 sid_name_map
[i
].sid
= get_global_sam_sid();
102 sid_name_map
[i
].name
= strdup(global_myname());
103 sid_name_map
[i
].known_users
= NULL
;
106 sid_name_map
[i
].sid
= get_global_sam_sid();
107 sid_name_map
[i
].name
= strdup(global_myname());
108 sid_name_map
[i
].known_users
= NULL
;
112 sid_name_map
[i
].sid
= &global_sid_Builtin
;
113 sid_name_map
[i
].name
= "BUILTIN";
114 sid_name_map
[i
].known_users
= &builtin_groups
[0];
117 sid_name_map
[i
].sid
= &global_sid_World_Domain
;
118 sid_name_map
[i
].name
= "";
119 sid_name_map
[i
].known_users
= &everyone_users
[0];
122 sid_name_map
[i
].sid
= &global_sid_Creator_Owner_Domain
;
123 sid_name_map
[i
].name
= "";
124 sid_name_map
[i
].known_users
= &creator_owner_users
[0];
127 sid_name_map
[i
].sid
= &global_sid_NT_Authority
;
128 sid_name_map
[i
].name
= "NT Authority";
129 sid_name_map
[i
].known_users
= &nt_authority_users
[0];
133 sid_name_map
[i
].sid
= NULL
;
134 sid_name_map
[i
].name
= NULL
;
135 sid_name_map
[i
].known_users
= NULL
;
137 sid_name_map_initialized
= True
;
142 /**************************************************************************
143 Turns a domain SID into a name, returned in the nt_domain argument.
144 ***************************************************************************/
146 BOOL
map_domain_sid_to_name(DOM_SID
*sid
, fstring nt_domain
)
151 sid_to_string(sid_str
, sid
);
153 if (!sid_name_map_initialized
)
156 DEBUG(5,("map_domain_sid_to_name: %s\n", sid_str
));
158 if (nt_domain
== NULL
)
161 while (sid_name_map
[i
].sid
!= NULL
) {
162 sid_to_string(sid_str
, sid_name_map
[i
].sid
);
163 DEBUG(5,("map_domain_sid_to_name: compare: %s\n", sid_str
));
164 if (sid_equal(sid_name_map
[i
].sid
, sid
)) {
165 fstrcpy(nt_domain
, sid_name_map
[i
].name
);
166 DEBUG(5,("map_domain_sid_to_name: found '%s'\n", nt_domain
));
172 DEBUG(5,("map_domain_sid_to_name: mapping for %s not found\n", sid_str
));
177 /**************************************************************************
178 Looks up a known username from one of the known domains.
179 ***************************************************************************/
181 BOOL
lookup_known_rid(DOM_SID
*sid
, uint32 rid
, char *name
, enum SID_NAME_USE
*psid_name_use
)
184 struct sid_name_map_info
*psnm
;
186 if (!sid_name_map_initialized
)
189 for(i
= 0; sid_name_map
[i
].sid
!= NULL
; i
++) {
190 psnm
= &sid_name_map
[i
];
191 if(sid_equal(psnm
->sid
, sid
)) {
193 for(j
= 0; psnm
->known_users
&& psnm
->known_users
[j
].known_user_name
!= NULL
; j
++) {
194 if(rid
== psnm
->known_users
[j
].rid
) {
195 DEBUG(5,("lookup_builtin_rid: rid = %u, domain = '%s', user = '%s'\n",
196 (unsigned int)rid
, psnm
->name
, psnm
->known_users
[j
].known_user_name
));
197 fstrcpy( name
, psnm
->known_users
[j
].known_user_name
);
198 *psid_name_use
= psnm
->known_users
[j
].sid_name_use
;
208 /**************************************************************************
209 Turns a domain name into a SID.
210 *** side-effect: if the domain name is NULL, it is set to our domain ***
211 ***************************************************************************/
213 BOOL
map_domain_name_to_sid(DOM_SID
*sid
, char *nt_domain
)
217 if (nt_domain
== NULL
) {
218 DEBUG(5,("map_domain_name_to_sid: mapping NULL domain to our SID.\n"));
219 sid_copy(sid
, get_global_sam_sid());
223 if (nt_domain
[0] == 0) {
224 fstrcpy(nt_domain
, global_myname());
225 DEBUG(5,("map_domain_name_to_sid: overriding blank name to %s\n", nt_domain
));
226 sid_copy(sid
, get_global_sam_sid());
230 DEBUG(5,("map_domain_name_to_sid: %s\n", nt_domain
));
232 if (!sid_name_map_initialized
)
235 while (sid_name_map
[i
].name
!= NULL
) {
236 DEBUG(5,("map_domain_name_to_sid: compare: %s\n", sid_name_map
[i
].name
));
237 if (strequal(sid_name_map
[i
].name
, nt_domain
)) {
239 sid_copy(sid
, sid_name_map
[i
].sid
);
240 sid_to_string(sid_str
, sid_name_map
[i
].sid
);
241 DEBUG(5,("map_domain_name_to_sid: found %s\n", sid_str
));
247 DEBUG(0,("map_domain_name_to_sid: mapping to %s not found.\n", nt_domain
));
251 /*****************************************************************
252 Check if the SID is our domain SID (S-1-5-21-x-y-z).
253 *****************************************************************/
255 BOOL
sid_check_is_domain(const DOM_SID
*sid
)
257 return sid_equal(sid
, get_global_sam_sid());
260 /*****************************************************************
261 Check if the SID is our domain SID (S-1-5-21-x-y-z).
262 *****************************************************************/
264 BOOL
sid_check_is_in_our_domain(const DOM_SID
*sid
)
269 sid_copy(&dom_sid
, sid
);
270 sid_split_rid(&dom_sid
, &rid
);
272 return sid_equal(&dom_sid
, get_global_sam_sid());
275 /**************************************************************************
276 Try and map a name to one of the well known SIDs.
277 ***************************************************************************/
279 BOOL
map_name_to_wellknown_sid(DOM_SID
*sid
, enum SID_NAME_USE
*use
, const char *name
)
283 if (!sid_name_map_initialized
)
286 for (i
=0; sid_name_map
[i
].sid
!= NULL
; i
++) {
287 const known_sid_users
*users
= sid_name_map
[i
].known_users
;
292 for (j
=0; users
[j
].known_user_name
!= NULL
; j
++) {
293 if (strequal(users
[j
].known_user_name
, name
) == 0) {
294 sid_copy(sid
, sid_name_map
[i
].sid
);
295 sid_append_rid(sid
, users
[j
].rid
);
296 *use
= users
[j
].sid_name_use
;