r12714: Fix segfault in pdb_nds.c.
[metze/samba/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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 struct rid_name_map {
27         uint32 rid;
28         const char *name;
29 };
30
31 struct sid_name_map_info
32 {
33         const DOM_SID *sid;
34         const char *name;
35         const struct rid_name_map *known_users;
36 };
37
38 static const struct rid_name_map everyone_users[] = {
39         { 0, "Everyone" },
40         { 0, NULL}};
41
42 static const struct rid_name_map creator_owner_users[] = {
43         { 0, "Creator Owner" },
44         { 1, "Creator Group" },
45         { 0, NULL}};
46
47 static const struct rid_name_map nt_authority_users[] = {
48         {  1, "Dialup" },
49         {  2, "Network"},
50         {  3, "Batch"},
51         {  4, "Interactive"},
52         {  6, "Service"},
53         {  7, "AnonymousLogon"},
54         {  8, "Proxy"},
55         {  9, "ServerLogon"},
56         { 10, "Self"},
57         { 11, "Authenticated Users"},
58         { 12, "Restricted"},
59         { 13, "Terminal Server User"},
60         { 14, "Remote Interactive Logon"},
61         { 15, "This Organization"},
62         { 18, "SYSTEM"},
63         { 19, "Local Service"},
64         { 20, "Network Service"},
65         {  0,  NULL}};
66
67 static struct sid_name_map_info special_domains[] = {
68         { &global_sid_World_Domain, "", everyone_users },
69         { &global_sid_Creator_Owner_Domain, "", creator_owner_users },
70         { &global_sid_NT_Authority, "NT Authority", nt_authority_users },
71         { NULL, NULL, NULL }};
72
73 /**************************************************************************
74  Looks up a known username from one of the known domains.
75 ***************************************************************************/
76
77 BOOL lookup_wellknown_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
78                           const char **domain, const char **name)
79 {
80         int i;
81         DOM_SID dom_sid;
82         uint32 rid;
83         const struct rid_name_map *users = NULL;
84
85         sid_copy(&dom_sid, sid);
86         if (!sid_split_rid(&dom_sid, &rid)) {
87                 DEBUG(2, ("Could not split rid from SID\n"));
88                 return False;
89         }
90
91         for (i=0; special_domains[i].sid != NULL; i++) {
92                 if (sid_equal(&dom_sid, special_domains[i].sid)) {
93                         *domain = talloc_strdup(mem_ctx,
94                                                 special_domains[i].name);
95                         users = special_domains[i].known_users;
96                         break;
97                 }
98         }
99
100         if (users == NULL) {
101                 DEBUG(10, ("SID %s is no special sid\n",
102                            sid_string_static(sid)));
103                 return False;
104         }
105
106         for (i=0; users[i].name != NULL; i++) {
107                 if (rid == users[i].rid) {
108                         *name = talloc_strdup(mem_ctx, users[i].name);
109                         return True;
110                 }
111         }
112
113         DEBUG(10, ("RID of special SID %s not found\n",
114                    sid_string_static(sid)));
115
116         return False;
117 }
118
119 /**************************************************************************
120  Try and map a name to one of the well known SIDs.
121 ***************************************************************************/
122
123 BOOL lookup_wellknown_name(TALLOC_CTX *mem_ctx, const char *name,
124                            DOM_SID *sid, const char **domain)
125 {
126         int i, j;
127
128         DEBUG(10,("map_name_to_wellknown_sid: looking up %s\n", name));
129
130         for (i=0; special_domains[i].sid != NULL; i++) {
131                 const struct rid_name_map *users =
132                         special_domains[i].known_users;
133
134                 if (users == NULL)
135                         continue;
136
137                 for (j=0; users[j].name != NULL; j++) {
138                         if ( strequal(users[j].name, name) ) {
139                                 sid_copy(sid, special_domains[i].sid);
140                                 sid_append_rid(sid, users[j].rid);
141                                 *domain = talloc_strdup(
142                                         mem_ctx, special_domains[i].name);
143                                 return True;
144                         }
145                 }
146         }
147
148         return False;
149 }