s3-passdb: add passdb.h where needed.
[samba.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 #include "passdb.h"
25 #include "../libcli/security/security.h"
26
27 struct rid_name_map {
28         uint32 rid;
29         const char *name;
30 };
31
32 struct sid_name_map_info
33 {
34         const struct dom_sid *sid;
35         const char *name;
36         const struct rid_name_map *known_users;
37 };
38
39 static const struct rid_name_map everyone_users[] = {
40         { 0, "Everyone" },
41         { 0, NULL}};
42
43 static const struct rid_name_map creator_owner_users[] = {
44         { 0, "Creator Owner" },
45         { 1, "Creator Group" },
46         { 0, NULL}};
47
48 static const struct rid_name_map nt_authority_users[] = {
49         {  1, "Dialup" },
50         {  2, "Network"},
51         {  3, "Batch"},
52         {  4, "Interactive"},
53         {  6, "Service"},
54         {  7, "AnonymousLogon"},
55         {  7, "Anonymous Logon"},
56         {  8, "Proxy"},
57         {  9, "ServerLogon"},
58         { 10, "Self"},
59         { 11, "Authenticated Users"},
60         { 12, "Restricted"},
61         { 13, "Terminal Server User"},
62         { 14, "Remote Interactive Logon"},
63         { 15, "This Organization"},
64         { 18, "SYSTEM"},
65         { 19, "Local Service"},
66         { 20, "Network Service"},
67         {  0,  NULL}};
68
69 static struct sid_name_map_info special_domains[] = {
70         { &global_sid_World_Domain, "", everyone_users },
71         { &global_sid_Creator_Owner_Domain, "", creator_owner_users },
72         { &global_sid_NT_Authority, "NT Authority", nt_authority_users },
73         { NULL, NULL, NULL }};
74
75 bool sid_check_is_wellknown_domain(const struct dom_sid *sid, const char **name)
76 {
77         int i;
78
79         for (i=0; special_domains[i].sid != NULL; i++) {
80                 if (dom_sid_equal(sid, special_domains[i].sid)) {
81                         if (name != NULL) {
82                                 *name = special_domains[i].name;
83                         }
84                         return True;
85                 }
86         }
87         return False;
88 }
89
90 bool sid_check_is_in_wellknown_domain(const struct dom_sid *sid)
91 {
92         struct dom_sid dom_sid;
93
94         sid_copy(&dom_sid, sid);
95         sid_split_rid(&dom_sid, NULL);
96
97         return sid_check_is_wellknown_domain(&dom_sid, NULL);
98 }
99
100 /**************************************************************************
101  Looks up a known username from one of the known domains.
102 ***************************************************************************/
103
104 bool lookup_wellknown_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
105                           const char **domain, const char **name)
106 {
107         int i;
108         struct dom_sid dom_sid;
109         uint32 rid;
110         const struct rid_name_map *users = NULL;
111
112         sid_copy(&dom_sid, sid);
113         if (!sid_split_rid(&dom_sid, &rid)) {
114                 DEBUG(2, ("Could not split rid from SID\n"));
115                 return False;
116         }
117
118         for (i=0; special_domains[i].sid != NULL; i++) {
119                 if (dom_sid_equal(&dom_sid, special_domains[i].sid)) {
120                         *domain = talloc_strdup(mem_ctx,
121                                                 special_domains[i].name);
122                         users = special_domains[i].known_users;
123                         break;
124                 }
125         }
126
127         if (users == NULL) {
128                 DEBUG(10, ("SID %s is no special sid\n", sid_string_dbg(sid)));
129                 return False;
130         }
131
132         for (i=0; users[i].name != NULL; i++) {
133                 if (rid == users[i].rid) {
134                         *name = talloc_strdup(mem_ctx, users[i].name);
135                         return True;
136                 }
137         }
138
139         DEBUG(10, ("RID of special SID %s not found\n", sid_string_dbg(sid)));
140
141         return False;
142 }
143
144 /**************************************************************************
145  Try and map a name to one of the well known SIDs.
146 ***************************************************************************/
147
148 bool lookup_wellknown_name(TALLOC_CTX *mem_ctx, const char *name,
149                            struct dom_sid *sid, const char **domain)
150 {
151         int i, j;
152
153         DEBUG(10,("map_name_to_wellknown_sid: looking up %s\n", name));
154
155         for (i=0; special_domains[i].sid != NULL; i++) {
156                 const struct rid_name_map *users =
157                         special_domains[i].known_users;
158
159                 if (users == NULL)
160                         continue;
161
162                 for (j=0; users[j].name != NULL; j++) {
163                         if ( strequal(users[j].name, name) ) {
164                                 sid_compose(sid, special_domains[i].sid,
165                                             users[j].rid);
166                                 *domain = talloc_strdup(
167                                         mem_ctx, special_domains[i].name);
168                                 return True;
169                         }
170                 }
171         }
172
173         return False;
174 }