r10656: BIG merge from trunk. Features not copied over
[sfrench/samba-autobuild/.git] / source3 / 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(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         if (sid_check_is_domain(sid)) {
104                 fstrcpy(nt_domain, get_global_sam_name());
105                 return True;
106         }
107
108         while (special_domains[i].sid != NULL) {
109                 DEBUG(5,("map_domain_sid_to_name: compare: %s\n",
110                          sid_string_static(special_domains[i].sid)));
111                 if (sid_equal(special_domains[i].sid, sid)) {           
112                         fstrcpy(nt_domain, special_domains[i].name);
113                         DEBUG(5,("map_domain_sid_to_name: found '%s'\n",
114                                  nt_domain));
115                         return True;
116                 }
117                 i++;
118         }
119
120         DEBUG(5,("map_domain_sid_to_name: mapping for %s not found\n",
121                  sid_string_static(sid)));
122
123         return False;
124 }
125
126 /**************************************************************************
127  Looks up a known username from one of the known domains.
128 ***************************************************************************/
129
130 BOOL lookup_special_sid(const DOM_SID *sid, const char **domain,
131                         const char **name, enum SID_NAME_USE *type)
132 {
133         int i;
134         DOM_SID dom_sid;
135         uint32 rid;
136         const known_sid_users *users = NULL;
137
138         sid_copy(&dom_sid, sid);
139         if (!sid_split_rid(&dom_sid, &rid)) {
140                 DEBUG(2, ("Could not split rid from SID\n"));
141                 return False;
142         }
143
144         for (i=0; special_domains[i].sid != NULL; i++) {
145                 if (sid_equal(&dom_sid, special_domains[i].sid)) {
146                         *domain = special_domains[i].name;
147                         users = special_domains[i].known_users;
148                         break;
149                 }
150         }
151
152         if (users == NULL) {
153                 DEBUG(10, ("SID %s is no special sid\n",
154                            sid_string_static(sid)));
155                 return False;
156         }
157
158         for (i=0; users[i].known_user_name != NULL; i++) {
159                 if (rid == users[i].rid) {
160                         *name = users[i].known_user_name;
161                         *type = users[i].sid_name_use;
162                         return True;
163                 }
164         }
165
166         DEBUG(10, ("RID of special SID %s not found\n",
167                    sid_string_static(sid)));
168
169         return False;
170 }
171
172 /*****************************************************************
173  Check if the SID is our domain SID (S-1-5-21-x-y-z).
174 *****************************************************************/  
175
176 BOOL sid_check_is_domain(const DOM_SID *sid)
177 {
178         return sid_equal(sid, get_global_sam_sid());
179 }
180
181 /*****************************************************************
182  Check if the SID is our domain SID (S-1-5-21-x-y-z).
183 *****************************************************************/  
184
185 BOOL sid_check_is_in_our_domain(const DOM_SID *sid)
186 {
187         DOM_SID dom_sid;
188         uint32 rid;
189
190         sid_copy(&dom_sid, sid);
191         sid_split_rid(&dom_sid, &rid);
192         
193         return sid_equal(&dom_sid, get_global_sam_sid());
194 }
195
196 /**************************************************************************
197  Try and map a name to one of the well known SIDs.
198 ***************************************************************************/
199
200 BOOL map_name_to_wellknown_sid(DOM_SID *sid, enum SID_NAME_USE *use, const char *name)
201 {
202         int i, j;
203
204         DEBUG(10,("map_name_to_wellknown_sid: looking up %s\n", name));
205
206         for (i=0; special_domains[i].sid != NULL; i++) {
207                 const known_sid_users *users = special_domains[i].known_users;
208
209                 if (users == NULL)
210                         continue;
211
212                 for (j=0; users[j].known_user_name != NULL; j++) {
213                         if ( strequal(users[j].known_user_name, name) ) {
214                                 sid_copy(sid, special_domains[i].sid);
215                                 sid_append_rid(sid, users[j].rid);
216                                 *use = users[j].sid_name_use;
217                                 return True;
218                         }
219                 }
220         }
221
222         return False;
223 }
224
225