nbt: Fix offset check in ndr_pull_component
[Samba/gebeck_regimport.git] / source3 / lib / util_wellknown.c
blob19ac7cee348fc8a7650d91363b34f636f047c1ff
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
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.
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.
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/>.
23 #include "includes.h"
24 #include "../libcli/security/security.h"
26 struct rid_name_map {
27 uint32 rid;
28 const char *name;
31 struct sid_name_map_info
33 const struct dom_sid *sid;
34 const char *name;
35 const struct rid_name_map *known_users;
38 static const struct rid_name_map everyone_users[] = {
39 { 0, "Everyone" },
40 { 0, NULL}};
42 static const struct rid_name_map creator_owner_users[] = {
43 { 0, "Creator Owner" },
44 { 1, "Creator Group" },
45 { 2, "Creator Owner Server" },
46 { 3, "Creator Group Server" },
47 { 4, "Owner Rights" },
48 { 0, NULL}};
50 static const struct rid_name_map nt_authority_users[] = {
51 { 1, "Dialup" },
52 { 2, "Network"},
53 { 3, "Batch"},
54 { 4, "Interactive"},
55 { 6, "Service"},
56 { 7, "Anonymous Logon"},
57 { 8, "Proxy"},
58 { 9, "Enterprise Domain Controllers"},
59 { 10, "Self"},
60 { 11, "Authenticated Users"},
61 { 12, "Restricted"},
62 { 13, "Terminal Server User"},
63 { 14, "Remote Interactive Logon"},
64 { 15, "This Organization"},
65 { 17, "IUSR"},
66 { 18, "SYSTEM"},
67 { 19, "Local Service"},
68 { 20, "Network Service"},
69 { 0, NULL}};
71 static struct sid_name_map_info special_domains[] = {
72 { &global_sid_World_Domain, "", everyone_users },
73 { &global_sid_Creator_Owner_Domain, "", creator_owner_users },
74 { &global_sid_NT_Authority, "NT Authority", nt_authority_users },
75 { NULL, NULL, NULL }};
77 bool sid_check_is_wellknown_domain(const struct dom_sid *sid, const char **name)
79 int i;
81 for (i=0; special_domains[i].sid != NULL; i++) {
82 if (dom_sid_equal(sid, special_domains[i].sid)) {
83 if (name != NULL) {
84 *name = special_domains[i].name;
86 return True;
89 return False;
92 bool sid_check_is_in_wellknown_domain(const struct dom_sid *sid)
94 struct dom_sid dom_sid;
96 sid_copy(&dom_sid, sid);
97 sid_split_rid(&dom_sid, NULL);
99 return sid_check_is_wellknown_domain(&dom_sid, NULL);
102 /**************************************************************************
103 Looks up a known username from one of the known domains.
104 ***************************************************************************/
106 bool lookup_wellknown_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
107 const char **domain, const char **name)
109 int i;
110 struct dom_sid dom_sid;
111 uint32 rid;
112 const struct rid_name_map *users = NULL;
114 sid_copy(&dom_sid, sid);
115 if (!sid_split_rid(&dom_sid, &rid)) {
116 DEBUG(2, ("Could not split rid from SID\n"));
117 return False;
120 for (i=0; special_domains[i].sid != NULL; i++) {
121 if (dom_sid_equal(&dom_sid, special_domains[i].sid)) {
122 *domain = talloc_strdup(mem_ctx,
123 special_domains[i].name);
124 users = special_domains[i].known_users;
125 break;
129 if (users == NULL) {
130 DEBUG(10, ("SID %s is no special sid\n", sid_string_dbg(sid)));
131 return False;
134 for (i=0; users[i].name != NULL; i++) {
135 if (rid == users[i].rid) {
136 *name = talloc_strdup(mem_ctx, users[i].name);
137 return True;
141 DEBUG(10, ("RID of special SID %s not found\n", sid_string_dbg(sid)));
143 return False;
146 /**************************************************************************
147 Try and map a name to one of the well known SIDs.
148 ***************************************************************************/
150 bool lookup_wellknown_name(TALLOC_CTX *mem_ctx, const char *name,
151 struct dom_sid *sid, const char **domain)
153 int i, j;
155 DEBUG(10,("map_name_to_wellknown_sid: looking up %s\n", name));
157 for (i=0; special_domains[i].sid != NULL; i++) {
158 const struct rid_name_map *users =
159 special_domains[i].known_users;
161 if (users == NULL)
162 continue;
164 for (j=0; users[j].name != NULL; j++) {
165 if ( strequal(users[j].name, name) ) {
166 sid_compose(sid, special_domains[i].sid,
167 users[j].rid);
168 *domain = talloc_strdup(
169 mem_ctx, special_domains[i].name);
170 return True;
175 return False;