add Replicator and RAS Servers to list of builtin SIDs we resolve; bug 608
[Samba/gebeck_regimport.git] / source3 / passdb / util_sam_sid.c
blobc4fd08746c7e91caee66930c48d110f6fcfa45f5
1 /*
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.
23 #include "includes.h"
25 #define MAX_SID_NAMES 7
27 typedef struct _known_sid_users {
28 uint32 rid;
29 enum SID_NAME_USE sid_name_use;
30 const char *known_user_name;
31 } known_sid_users;
33 static struct sid_name_map_info
35 DOM_SID *sid;
36 const char *name;
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 { BUILTIN_ALIAS_RID_REPLICATOR, SID_NAME_ALIAS, "Replicator" },
80 { BUILTIN_ALIAS_RID_RAS_SERVERS, SID_NAME_ALIAS, "RAS Servers" },
81 { 0, (enum SID_NAME_USE)0, NULL}};
83 /**************************************************************************
84 Quick init function.
85 *************************************************************************/
87 static void init_sid_name_map (void)
89 int i = 0;
91 if (sid_name_map_initialized) return;
93 generate_wellknown_sids();
95 if ((lp_security() == SEC_USER) && lp_domain_logons()) {
96 sid_name_map[i].sid = get_global_sam_sid();
97 /* This is not lp_workgroup() for good reason:
98 it must stay around longer than the lp_*()
99 strings do */
100 sid_name_map[i].name = strdup(lp_workgroup());
101 sid_name_map[i].known_users = NULL;
102 i++;
103 sid_name_map[i].sid = get_global_sam_sid();
104 sid_name_map[i].name = strdup(global_myname());
105 sid_name_map[i].known_users = NULL;
106 i++;
107 } else {
108 sid_name_map[i].sid = get_global_sam_sid();
109 sid_name_map[i].name = strdup(global_myname());
110 sid_name_map[i].known_users = NULL;
111 i++;
114 sid_name_map[i].sid = &global_sid_Builtin;
115 sid_name_map[i].name = "BUILTIN";
116 sid_name_map[i].known_users = &builtin_groups[0];
117 i++;
119 sid_name_map[i].sid = &global_sid_World_Domain;
120 sid_name_map[i].name = "";
121 sid_name_map[i].known_users = &everyone_users[0];
122 i++;
124 sid_name_map[i].sid = &global_sid_Creator_Owner_Domain;
125 sid_name_map[i].name = "";
126 sid_name_map[i].known_users = &creator_owner_users[0];
127 i++;
129 sid_name_map[i].sid = &global_sid_NT_Authority;
130 sid_name_map[i].name = "NT Authority";
131 sid_name_map[i].known_users = &nt_authority_users[0];
132 i++;
134 /* End of array. */
135 sid_name_map[i].sid = NULL;
136 sid_name_map[i].name = NULL;
137 sid_name_map[i].known_users = NULL;
139 sid_name_map_initialized = True;
141 return;
144 /**************************************************************************
145 Turns a domain SID into a name, returned in the nt_domain argument.
146 ***************************************************************************/
148 BOOL map_domain_sid_to_name(DOM_SID *sid, fstring nt_domain)
150 fstring sid_str;
151 int i = 0;
153 sid_to_string(sid_str, sid);
155 if (!sid_name_map_initialized)
156 init_sid_name_map();
158 DEBUG(5,("map_domain_sid_to_name: %s\n", sid_str));
160 if (nt_domain == NULL)
161 return False;
163 while (sid_name_map[i].sid != NULL) {
164 sid_to_string(sid_str, sid_name_map[i].sid);
165 DEBUG(5,("map_domain_sid_to_name: compare: %s\n", sid_str));
166 if (sid_equal(sid_name_map[i].sid, sid)) {
167 fstrcpy(nt_domain, sid_name_map[i].name);
168 DEBUG(5,("map_domain_sid_to_name: found '%s'\n", nt_domain));
169 return True;
171 i++;
174 DEBUG(5,("map_domain_sid_to_name: mapping for %s not found\n", sid_str));
176 return False;
179 /**************************************************************************
180 Looks up a known username from one of the known domains.
181 ***************************************************************************/
183 BOOL lookup_known_rid(DOM_SID *sid, uint32 rid, char *name, enum SID_NAME_USE *psid_name_use)
185 int i = 0;
186 struct sid_name_map_info *psnm;
188 if (!sid_name_map_initialized)
189 init_sid_name_map();
191 for(i = 0; sid_name_map[i].sid != NULL; i++) {
192 psnm = &sid_name_map[i];
193 if(sid_equal(psnm->sid, sid)) {
194 int j;
195 for(j = 0; psnm->known_users && psnm->known_users[j].known_user_name != NULL; j++) {
196 if(rid == psnm->known_users[j].rid) {
197 DEBUG(5,("lookup_builtin_rid: rid = %u, domain = '%s', user = '%s'\n",
198 (unsigned int)rid, psnm->name, psnm->known_users[j].known_user_name ));
199 fstrcpy( name, psnm->known_users[j].known_user_name);
200 *psid_name_use = psnm->known_users[j].sid_name_use;
201 return True;
207 return False;
210 /**************************************************************************
211 Turns a domain name into a SID.
212 *** side-effect: if the domain name is NULL, it is set to our domain ***
213 ***************************************************************************/
215 BOOL map_domain_name_to_sid(DOM_SID *sid, char *nt_domain)
217 int i = 0;
219 if (nt_domain == NULL) {
220 DEBUG(5,("map_domain_name_to_sid: mapping NULL domain to our SID.\n"));
221 sid_copy(sid, get_global_sam_sid());
222 return True;
225 if (nt_domain[0] == 0) {
226 fstrcpy(nt_domain, global_myname());
227 DEBUG(5,("map_domain_name_to_sid: overriding blank name to %s\n", nt_domain));
228 sid_copy(sid, get_global_sam_sid());
229 return True;
232 DEBUG(5,("map_domain_name_to_sid: %s\n", nt_domain));
234 if (!sid_name_map_initialized)
235 init_sid_name_map();
237 while (sid_name_map[i].name != NULL) {
238 DEBUG(5,("map_domain_name_to_sid: compare: %s\n", sid_name_map[i].name));
239 if (strequal(sid_name_map[i].name, nt_domain)) {
240 fstring sid_str;
241 sid_copy(sid, sid_name_map[i].sid);
242 sid_to_string(sid_str, sid_name_map[i].sid);
243 DEBUG(5,("map_domain_name_to_sid: found %s\n", sid_str));
244 return True;
246 i++;
249 DEBUG(0,("map_domain_name_to_sid: mapping to %s not found.\n", nt_domain));
250 return False;
253 /*****************************************************************
254 Check if the SID is our domain SID (S-1-5-21-x-y-z).
255 *****************************************************************/
257 BOOL sid_check_is_domain(const DOM_SID *sid)
259 return sid_equal(sid, get_global_sam_sid());
262 /*****************************************************************
263 Check if the SID is our domain SID (S-1-5-21-x-y-z).
264 *****************************************************************/
266 BOOL sid_check_is_in_our_domain(const DOM_SID *sid)
268 DOM_SID dom_sid;
269 uint32 rid;
271 sid_copy(&dom_sid, sid);
272 sid_split_rid(&dom_sid, &rid);
274 return sid_equal(&dom_sid, get_global_sam_sid());
277 /**************************************************************************
278 Try and map a name to one of the well known SIDs.
279 ***************************************************************************/
281 BOOL map_name_to_wellknown_sid(DOM_SID *sid, enum SID_NAME_USE *use, const char *name)
283 int i, j;
285 if (!sid_name_map_initialized)
286 init_sid_name_map();
288 for (i=0; sid_name_map[i].sid != NULL; i++) {
289 const known_sid_users *users = sid_name_map[i].known_users;
291 if (users == NULL)
292 continue;
294 for (j=0; users[j].known_user_name != NULL; j++) {
295 if (strequal(users[j].known_user_name, name) == 0) {
296 sid_copy(sid, sid_name_map[i].sid);
297 sid_append_rid(sid, users[j].rid);
298 *use = users[j].sid_name_use;
299 return True;
304 return False;