3a30ab0a65d8460dfdc692acc497d43017452e1e
[ira/wip.git] / source3 / passdb / util_wellknown.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Lookup routines for well-known SIDs
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Luke Kenneth Caseson Leighton 1998-1999
6    Copyright (C) Jeremy Allison  1999
7    Copyright (C) Volker Lendecke 2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24
25 struct rid_name_map {
26         uint32 rid;
27         const char *name;
28 };
29
30 struct sid_name_map_info
31 {
32         const DOM_SID *sid;
33         const char *name;
34         const struct rid_name_map *known_users;
35 };
36
37 static const struct rid_name_map everyone_users[] = {
38         { 0, "Everyone" },
39         { 0, NULL}};
40
41 static const struct rid_name_map creator_owner_users[] = {
42         { 0, "Creator Owner" },
43         { 1, "Creator Group" },
44         { 0, NULL}};
45
46 static const struct rid_name_map nt_authority_users[] = {
47         {  1, "Dialup" },
48         {  2, "Network"},
49         {  3, "Batch"},
50         {  4, "Interactive"},
51         {  6, "Service"},
52         {  7, "AnonymousLogon"},
53         {  8, "Proxy"},
54         {  9, "ServerLogon"},
55         { 10, "Self"},
56         { 11, "Authenticated Users"},
57         { 12, "Restricted"},
58         { 13, "Terminal Server User"},
59         { 14, "Remote Interactive Logon"},
60         { 15, "This Organization"},
61         { 18, "SYSTEM"},
62         { 19, "Local Service"},
63         { 20, "Network Service"},
64         {  0,  NULL}};
65
66 static struct sid_name_map_info special_domains[] = {
67         { &global_sid_World_Domain, "", everyone_users },
68         { &global_sid_Creator_Owner_Domain, "", creator_owner_users },
69         { &global_sid_NT_Authority, "NT Authority", nt_authority_users },
70         { NULL, NULL, NULL }};
71
72 bool sid_check_is_wellknown_domain(const DOM_SID *sid, const char **name)
73 {
74         int i;
75
76         for (i=0; special_domains[i].sid != NULL; i++) {
77                 if (sid_equal(sid, special_domains[i].sid)) {
78                         if (name != NULL) {
79                                 *name = special_domains[i].name;
80                         }
81                         return True;
82                 }
83         }
84         return False;
85 }
86
87 bool sid_check_is_in_wellknown_domain(const DOM_SID *sid)
88 {
89         DOM_SID dom_sid;
90         uint32 rid;
91
92         sid_copy(&dom_sid, sid);
93         sid_split_rid(&dom_sid, &rid);
94         
95         return sid_check_is_wellknown_domain(&dom_sid, NULL);
96 }
97
98 /**************************************************************************
99  Looks up a known username from one of the known domains.
100 ***************************************************************************/
101
102 bool lookup_wellknown_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
103                           const char **domain, const char **name)
104 {
105         int i;
106         DOM_SID dom_sid;
107         uint32 rid;
108         const struct rid_name_map *users = NULL;
109
110         sid_copy(&dom_sid, sid);
111         if (!sid_split_rid(&dom_sid, &rid)) {
112                 DEBUG(2, ("Could not split rid from SID\n"));
113                 return False;
114         }
115
116         for (i=0; special_domains[i].sid != NULL; i++) {
117                 if (sid_equal(&dom_sid, special_domains[i].sid)) {
118                         *domain = talloc_strdup(mem_ctx,
119                                                 special_domains[i].name);
120                         users = special_domains[i].known_users;
121                         break;
122                 }
123         }
124
125         if (users == NULL) {
126                 DEBUG(10, ("SID %s is no special sid\n", sid_string_dbg(sid)));
127                 return False;
128         }
129
130         for (i=0; users[i].name != NULL; i++) {
131                 if (rid == users[i].rid) {
132                         *name = talloc_strdup(mem_ctx, users[i].name);
133                         return True;
134                 }
135         }
136
137         DEBUG(10, ("RID of special SID %s not found\n", sid_string_dbg(sid)));
138
139         return False;
140 }
141
142 /**************************************************************************
143  Try and map a name to one of the well known SIDs.
144 ***************************************************************************/
145
146 bool lookup_wellknown_name(TALLOC_CTX *mem_ctx, const char *name,
147                            DOM_SID *sid, const char **domain)
148 {
149         int i, j;
150
151         DEBUG(10,("map_name_to_wellknown_sid: looking up %s\n", name));
152
153         for (i=0; special_domains[i].sid != NULL; i++) {
154                 const struct rid_name_map *users =
155                         special_domains[i].known_users;
156
157                 if (users == NULL)
158                         continue;
159
160                 for (j=0; users[j].name != NULL; j++) {
161                         if ( strequal(users[j].name, name) ) {
162                                 sid_copy(sid, special_domains[i].sid);
163                                 sid_append_rid(sid, users[j].rid);
164                                 *domain = talloc_strdup(
165                                         mem_ctx, special_domains[i].name);
166                                 return True;
167                         }
168                 }
169         }
170
171         return False;
172 }