r684: Note the fact that NTLMv2 provides a weath of futher testcases...
[bbaumbach/samba-autobuild/.git] / source4 / passdb / util_sam_sid.c
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
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24
25 #define MAX_SID_NAMES   7
26
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;
32
33 static struct sid_name_map_info
34 {
35         DOM_SID *sid;
36         const char *name;
37         const known_sid_users *known_users;
38 } sid_name_map[MAX_SID_NAMES];
39
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 */
44
45
46 static BOOL sid_name_map_initialized = False;
47 /* static known_sid_users no_users[] = {{0, 0, NULL}}; */
48
49 static const known_sid_users everyone_users[] = {
50         { 0, SID_NAME_WKN_GRP, "Everyone" },
51         {0, (enum SID_NAME_USE)0, NULL}};
52
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}};
57
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}};
70
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}};
80
81 /**************************************************************************
82  Quick init function.
83 *************************************************************************/
84
85 static void init_sid_name_map (void)
86 {
87         int i = 0;
88         
89         if (sid_name_map_initialized) return;
90
91         generate_wellknown_sids();
92
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_*() 
97                    strings do */
98                 sid_name_map[i].name = strdup(lp_workgroup());
99                 sid_name_map[i].known_users = NULL;
100                 i++;
101                 sid_name_map[i].sid = get_global_sam_sid();
102                 sid_name_map[i].name = strdup(lp_netbios_name());
103                 sid_name_map[i].known_users = NULL;
104                 i++;
105         } else {
106                 sid_name_map[i].sid = get_global_sam_sid();
107                 sid_name_map[i].name = strdup(lp_netbios_name());
108                 sid_name_map[i].known_users = NULL;
109                 i++;
110         }
111
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];
115         i++;
116         
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];
120         i++;
121
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];
125         i++;
126                 
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];
130         i++;
131                 
132         /* End of array. */
133         sid_name_map[i].sid = NULL;
134         sid_name_map[i].name = NULL;
135         sid_name_map[i].known_users = NULL;
136         
137         sid_name_map_initialized = True;
138                 
139         return;
140 }
141
142 /**************************************************************************
143  Turns a domain SID into a name, returned in the nt_domain argument.
144 ***************************************************************************/
145
146 BOOL map_domain_sid_to_name(DOM_SID *sid, fstring nt_domain)
147 {
148         fstring sid_str;
149         int i = 0;
150         
151         sid_to_string(sid_str, sid);
152
153         if (!sid_name_map_initialized) 
154                 init_sid_name_map();
155
156         DEBUG(5,("map_domain_sid_to_name: %s\n", sid_str));
157
158         if (nt_domain == NULL)
159                 return False;
160
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));
167                         return True;
168                 }
169                 i++;
170         }
171
172         DEBUG(5,("map_domain_sid_to_name: mapping for %s not found\n", sid_str));
173
174     return False;
175 }
176
177 /**************************************************************************
178  Looks up a known username from one of the known domains.
179 ***************************************************************************/
180
181 BOOL lookup_known_rid(DOM_SID *sid, uint32 rid, char *name, enum SID_NAME_USE *psid_name_use)
182 {
183         int i = 0;
184         struct sid_name_map_info *psnm;
185
186         if (!sid_name_map_initialized) 
187                 init_sid_name_map();
188
189         for(i = 0; sid_name_map[i].sid != NULL; i++) {
190                 psnm = &sid_name_map[i];
191                 if(sid_equal(psnm->sid, sid)) {
192                         int j;
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;
199                                         return True;
200                                 }
201                         }
202                 }
203         }
204
205         return False;
206 }
207
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 ***************************************************************************/
212
213 BOOL map_domain_name_to_sid(DOM_SID *sid, char *nt_domain)
214 {
215         int i = 0;
216
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());
220                 return True;
221         }
222
223         if (nt_domain[0] == 0) {
224                 fstrcpy(nt_domain, lp_netbios_name());
225                 DEBUG(5,("map_domain_name_to_sid: overriding blank name to %s\n", nt_domain));
226                 sid_copy(sid, get_global_sam_sid());
227                 return True;
228         }
229
230         DEBUG(5,("map_domain_name_to_sid: %s\n", nt_domain));
231
232         if (!sid_name_map_initialized) 
233                 init_sid_name_map();
234
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)) {
238                         fstring sid_str;
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));
242                         return True;
243                 }
244                 i++;
245         }
246
247         DEBUG(0,("map_domain_name_to_sid: mapping to %s not found.\n", nt_domain));
248         return False;
249 }
250
251 /*****************************************************************
252  Check if the SID is our domain SID (S-1-5-21-x-y-z).
253 *****************************************************************/  
254
255 BOOL sid_check_is_domain(const DOM_SID *sid)
256 {
257         return sid_equal(sid, get_global_sam_sid());
258 }
259
260 /*****************************************************************
261  Check if the SID is our domain SID (S-1-5-21-x-y-z).
262 *****************************************************************/  
263
264 BOOL sid_check_is_in_our_domain(const DOM_SID *sid)
265 {
266         DOM_SID dom_sid;
267         uint32 rid;
268
269         sid_copy(&dom_sid, sid);
270         sid_split_rid(&dom_sid, &rid);
271         
272         return sid_equal(&dom_sid, get_global_sam_sid());
273 }
274
275 /**************************************************************************
276  Try and map a name to one of the well known SIDs.
277 ***************************************************************************/
278
279 BOOL map_name_to_wellknown_sid(DOM_SID *sid, enum SID_NAME_USE *use, const char *name)
280 {
281         int i, j;
282
283         if (!sid_name_map_initialized)
284                 init_sid_name_map();
285
286         for (i=0; sid_name_map[i].sid != NULL; i++) {
287                 const known_sid_users *users = sid_name_map[i].known_users;
288
289                 if (users == NULL)
290                         continue;
291
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;
297                                 return True;
298                         }
299                 }
300         }
301
302         return False;
303 }