Initial import
[samba] / source / 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 struct sid_name_map_info
34 {
35         const DOM_SID *sid;
36         const char *name;
37         const known_sid_users *known_users;
38 };
39
40 static const known_sid_users everyone_users[] = {
41         { 0, SID_NAME_WKN_GRP, "Everyone" },
42         {0, (enum SID_NAME_USE)0, NULL}};
43
44 static const known_sid_users creator_owner_users[] = {
45         { 0, SID_NAME_WKN_GRP, "Creator Owner" },
46         { 1, SID_NAME_WKN_GRP, "Creator Group" },
47         {0, (enum SID_NAME_USE)0, NULL}};
48
49 static const known_sid_users nt_authority_users[] = {
50         {  1, SID_NAME_WKN_GRP, "Dialup" },
51         {  2, SID_NAME_WKN_GRP, "Network"},
52         {  3, SID_NAME_WKN_GRP, "Batch"},
53         {  4, SID_NAME_WKN_GRP, "Interactive"},
54         {  6, SID_NAME_WKN_GRP, "Service"},
55         {  7, SID_NAME_WKN_GRP, "AnonymousLogon"},
56         {  8, SID_NAME_WKN_GRP, "Proxy"},
57         {  9, SID_NAME_WKN_GRP, "ServerLogon"},
58         { 10, SID_NAME_WKN_GRP, "Self"},
59         { 11, SID_NAME_WKN_GRP, "Authenticated Users"},
60         { 12, SID_NAME_WKN_GRP, "Restricted"},
61         { 13, SID_NAME_WKN_GRP, "Terminal Server User"},
62         { 14, SID_NAME_WKN_GRP, "Remote Interactive Logon"},
63         { 15, SID_NAME_WKN_GRP, "This Organization"},
64         { 18, SID_NAME_WKN_GRP, "SYSTEM"},
65         { 19, SID_NAME_WKN_GRP, "Local Service"},
66         { 20, SID_NAME_WKN_GRP, "Network Service"},
67         {  0, (enum SID_NAME_USE)0, NULL}};
68
69 static const known_sid_users builtin_groups[] = {
70         { BUILTIN_ALIAS_RID_ADMINS, SID_NAME_ALIAS, "Administrators" },
71         { BUILTIN_ALIAS_RID_USERS, SID_NAME_ALIAS, "Users" },
72         { BUILTIN_ALIAS_RID_GUESTS, SID_NAME_ALIAS, "Guests" },
73         { BUILTIN_ALIAS_RID_POWER_USERS, SID_NAME_ALIAS, "Power Users" },
74         { BUILTIN_ALIAS_RID_ACCOUNT_OPS, SID_NAME_ALIAS, "Account Operators" },
75         { BUILTIN_ALIAS_RID_SYSTEM_OPS, SID_NAME_ALIAS, "Server Operators" },
76         { BUILTIN_ALIAS_RID_PRINT_OPS, SID_NAME_ALIAS, "Print Operators" },
77         { BUILTIN_ALIAS_RID_BACKUP_OPS, SID_NAME_ALIAS, "Backup Operators" },
78         { BUILTIN_ALIAS_RID_REPLICATOR, SID_NAME_ALIAS, "Replicator" },
79         { BUILTIN_ALIAS_RID_RAS_SERVERS, SID_NAME_ALIAS, "RAS Servers" },
80         { BUILTIN_ALIAS_RID_PRE_2K_ACCESS, SID_NAME_ALIAS, "Pre-Windows 2000 Compatible Access" },
81         {  0, (enum SID_NAME_USE)0, NULL}};
82
83 static struct sid_name_map_info special_domains[] = {
84         { &global_sid_Builtin, "BUILTIN", builtin_groups },
85         { &global_sid_World_Domain, "", everyone_users },
86         { &global_sid_Creator_Owner_Domain, "", creator_owner_users },
87         { &global_sid_NT_Authority, "NT Authority", nt_authority_users },
88         { NULL, NULL, NULL }};
89
90 /**************************************************************************
91  Turns a domain SID into a name, returned in the nt_domain argument.
92 ***************************************************************************/
93
94 BOOL map_domain_sid_to_name(const DOM_SID *sid, fstring nt_domain)
95 {
96         fstring sid_str;
97         int i = 0;
98         
99         sid_to_string(sid_str, sid);
100
101         DEBUG(5,("map_domain_sid_to_name: %s\n", sid_str));
102
103         while (special_domains[i].sid != NULL) {
104                 DEBUG(5,("map_domain_sid_to_name: compare: %s\n",
105                          sid_string_static(special_domains[i].sid)));
106                 if (sid_equal(special_domains[i].sid, sid)) {           
107                         fstrcpy(nt_domain, special_domains[i].name);
108                         DEBUG(5,("map_domain_sid_to_name: found '%s'\n",
109                                  nt_domain));
110                         return True;
111                 }
112                 i++;
113         }
114
115         DEBUG(5,("map_domain_sid_to_name: mapping for %s not found\n",
116                  sid_string_static(sid)));
117
118         return False;
119 }
120
121 /**************************************************************************
122  Looks up a known username from one of the known domains.
123 ***************************************************************************/
124
125 BOOL lookup_special_sid(const DOM_SID *sid, const char **domain,
126                         const char **name, enum SID_NAME_USE *type)
127 {
128         int i;
129         DOM_SID dom_sid;
130         uint32 rid;
131         const known_sid_users *users = NULL;
132
133         sid_copy(&dom_sid, sid);
134         if (!sid_split_rid(&dom_sid, &rid)) {
135                 DEBUG(2, ("Could not split rid from SID\n"));
136                 return False;
137         }
138
139         for (i=0; special_domains[i].sid != NULL; i++) {
140                 if (sid_equal(&dom_sid, special_domains[i].sid)) {
141                         *domain = special_domains[i].name;
142                         users = special_domains[i].known_users;
143                         break;
144                 }
145         }
146
147         if (users == NULL) {
148                 DEBUG(10, ("SID %s is no special sid\n",
149                            sid_string_static(sid)));
150                 return False;
151         }
152
153         for (i=0; users[i].known_user_name != NULL; i++) {
154                 if (rid == users[i].rid) {
155                         *name = users[i].known_user_name;
156                         *type = users[i].sid_name_use;
157                         return True;
158                 }
159         }
160
161         DEBUG(10, ("RID of special SID %s not found\n",
162                    sid_string_static(sid)));
163
164         return False;
165 }
166
167 /*******************************************************************
168  Look up a rid in the BUILTIN domain
169  ********************************************************************/
170 BOOL lookup_builtin_rid(uint32 rid, fstring name)
171 {
172         const known_sid_users *aliases = builtin_groups;
173         int i;
174
175         for (i=0; aliases[i].known_user_name != NULL; i++) {
176                 if (rid == aliases[i].rid) {
177                         fstrcpy(name, aliases[i].known_user_name);
178                         return True;
179                 }
180         }
181
182         return False;
183 }
184
185 /*****************************************************************
186  Check if the SID is our domain SID (S-1-5-21-x-y-z).
187 *****************************************************************/  
188
189 BOOL sid_check_is_domain(const DOM_SID *sid)
190 {
191         return sid_equal(sid, get_global_sam_sid());
192 }
193
194 /*****************************************************************
195  Check if the SID is our domain SID (S-1-5-21-x-y-z).
196 *****************************************************************/  
197
198 BOOL sid_check_is_in_our_domain(const DOM_SID *sid)
199 {
200         DOM_SID dom_sid;
201         uint32 rid;
202
203         sid_copy(&dom_sid, sid);
204         sid_split_rid(&dom_sid, &rid);
205         
206         return sid_equal(&dom_sid, get_global_sam_sid());
207 }
208
209 /**************************************************************************
210  Try and map a name to one of the well known SIDs.
211 ***************************************************************************/
212
213 BOOL map_name_to_wellknown_sid(DOM_SID *sid, enum SID_NAME_USE *use, const char *name)
214 {
215         int i, j;
216
217         DEBUG(10,("map_name_to_wellknown_sid: looking up %s\n", name));
218
219         for (i=0; special_domains[i].sid != NULL; i++) {
220                 const known_sid_users *users = special_domains[i].known_users;
221
222                 if (users == NULL)
223                         continue;
224
225                 for (j=0; users[j].known_user_name != NULL; j++) {
226                         if ( strequal(users[j].known_user_name, name) ) {
227                                 sid_copy(sid, special_domains[i].sid);
228                                 sid_append_rid(sid, users[j].rid);
229                                 *use = users[j].sid_name_use;
230                                 return True;
231                         }
232                 }
233         }
234
235         return False;
236 }
237
238