s3:registry: remove usage of reg_objects from net_rpc_printer.c
[Samba.git] / libcli / security / util_sid.c
blob9a24a4ab8fab4c681899cef11ecc700fc09dc4a0
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 Copyright (C) Stefan (metze) Metzmacher 2002
8 Copyright (C) Simo Sorce 2002
9 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2005
10 Copyright (C) Andrew Bartlett 2010
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "includes.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "../librpc/gen_ndr/netlogon.h"
29 #include "../libcli/security/security.h"
32 * Some useful sids, more well known sids can be found at
33 * http://support.microsoft.com/kb/243330/EN-US/
37 const struct dom_sid global_sid_World_Domain = /* Everyone domain */
38 { 1, 0, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
39 const struct dom_sid global_sid_World = /* Everyone */
40 { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
41 const struct dom_sid global_sid_Local_Authority = /* Local Authority */
42 { 1, 0, {0,0,0,0,0,2}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
43 const struct dom_sid global_sid_Creator_Owner_Domain = /* Creator Owner domain */
44 { 1, 0, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
45 const struct dom_sid global_sid_NT_Authority = /* NT Authority */
46 { 1, 0, {0,0,0,0,0,5}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
47 const struct dom_sid global_sid_System = /* System */
48 { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
49 const struct dom_sid global_sid_NULL = /* NULL sid */
50 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
51 const struct dom_sid global_sid_Authenticated_Users = /* All authenticated rids */
52 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
53 #if 0
54 /* for documentation */
55 const struct dom_sid global_sid_Restriced = /* Restriced Code */
56 { 1, 1, {0,0,0,0,0,5}, {12,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
57 #endif
58 const struct dom_sid global_sid_Network = /* Network rids */
59 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
61 const struct dom_sid global_sid_Creator_Owner = /* Creator Owner */
62 { 1, 1, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
63 const struct dom_sid global_sid_Creator_Group = /* Creator Group */
64 { 1, 1, {0,0,0,0,0,3}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
65 const struct dom_sid global_sid_Owner_Rights = /* Owner Rights */
66 { 1, 1, {0,0,0,0,0,3}, {4,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
67 const struct dom_sid global_sid_Anonymous = /* Anonymous login */
68 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
69 const struct dom_sid global_sid_Enterprise_DCs = /* Enterprise DCs */
70 { 1, 1, {0,0,0,0,0,5}, {9,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
71 const struct dom_sid global_sid_Builtin = /* Local well-known domain */
72 { 1, 1, {0,0,0,0,0,5}, {32,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
73 const struct dom_sid global_sid_Builtin_Administrators = /* Builtin administrators */
74 { 1, 2, {0,0,0,0,0,5}, {32,544,0,0,0,0,0,0,0,0,0,0,0,0,0}};
75 const struct dom_sid global_sid_Builtin_Users = /* Builtin users */
76 { 1, 2, {0,0,0,0,0,5}, {32,545,0,0,0,0,0,0,0,0,0,0,0,0,0}};
77 const struct dom_sid global_sid_Builtin_Guests = /* Builtin guest users */
78 { 1, 2, {0,0,0,0,0,5}, {32,546,0,0,0,0,0,0,0,0,0,0,0,0,0}};
79 const struct dom_sid global_sid_Builtin_Power_Users = /* Builtin power users */
80 { 1, 2, {0,0,0,0,0,5}, {32,547,0,0,0,0,0,0,0,0,0,0,0,0,0}};
81 const struct dom_sid global_sid_Builtin_Account_Operators = /* Builtin account operators */
82 { 1, 2, {0,0,0,0,0,5}, {32,548,0,0,0,0,0,0,0,0,0,0,0,0,0}};
83 const struct dom_sid global_sid_Builtin_Server_Operators = /* Builtin server operators */
84 { 1, 2, {0,0,0,0,0,5}, {32,549,0,0,0,0,0,0,0,0,0,0,0,0,0}};
85 const struct dom_sid global_sid_Builtin_Print_Operators = /* Builtin print operators */
86 { 1, 2, {0,0,0,0,0,5}, {32,550,0,0,0,0,0,0,0,0,0,0,0,0,0}};
87 const struct dom_sid global_sid_Builtin_Backup_Operators = /* Builtin backup operators */
88 { 1, 2, {0,0,0,0,0,5}, {32,551,0,0,0,0,0,0,0,0,0,0,0,0,0}};
89 const struct dom_sid global_sid_Builtin_Replicator = /* Builtin replicator */
90 { 1, 2, {0,0,0,0,0,5}, {32,552,0,0,0,0,0,0,0,0,0,0,0,0,0}};
91 const struct dom_sid global_sid_Builtin_PreWin2kAccess = /* Builtin pre win2k access */
92 { 1, 2, {0,0,0,0,0,5}, {32,554,0,0,0,0,0,0,0,0,0,0,0,0,0}};
94 const struct dom_sid global_sid_Unix_Users = /* Unmapped Unix users */
95 { 1, 1, {0,0,0,0,0,22}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
96 const struct dom_sid global_sid_Unix_Groups = /* Unmapped Unix groups */
97 { 1, 1, {0,0,0,0,0,22}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
99 /* Unused, left here for documentary purposes */
100 #if 0
101 #define SECURITY_NULL_SID_AUTHORITY 0
102 #define SECURITY_WORLD_SID_AUTHORITY 1
103 #define SECURITY_LOCAL_SID_AUTHORITY 2
104 #define SECURITY_CREATOR_SID_AUTHORITY 3
105 #define SECURITY_NT_AUTHORITY 5
106 #endif
108 static struct dom_sid system_sid_array[1] =
109 { { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
110 static const struct security_token system_token = {
111 .num_sids = ARRAY_SIZE(system_sid_array),
112 .sids = system_sid_array,
113 .privilege_mask = SE_ALL_PRIVS
116 /****************************************************************************
117 Lookup string names for SID types.
118 ****************************************************************************/
120 static const struct {
121 enum lsa_SidType sid_type;
122 const char *string;
123 } sid_name_type[] = {
124 {SID_NAME_USE_NONE, "None"},
125 {SID_NAME_USER, "User"},
126 {SID_NAME_DOM_GRP, "Domain Group"},
127 {SID_NAME_DOMAIN, "Domain"},
128 {SID_NAME_ALIAS, "Local Group"},
129 {SID_NAME_WKN_GRP, "Well-known Group"},
130 {SID_NAME_DELETED, "Deleted Account"},
131 {SID_NAME_INVALID, "Invalid Account"},
132 {SID_NAME_UNKNOWN, "UNKNOWN"},
133 {SID_NAME_COMPUTER, "Computer"}
136 const char *sid_type_lookup(uint32_t sid_type)
138 int i;
140 /* Look through list */
141 for (i=0; i < ARRAY_SIZE(sid_name_type); i++) {
142 if (sid_name_type[i].sid_type == sid_type) {
143 return sid_name_type[i].string;
147 /* Default return */
148 return "SID *TYPE* is INVALID";
151 /**************************************************************************
152 Create the SYSTEM token.
153 ***************************************************************************/
155 const struct security_token *get_system_token(void)
157 return &system_token;
160 bool sid_compose(struct dom_sid *dst, const struct dom_sid *domain_sid, uint32_t rid)
162 sid_copy(dst, domain_sid);
163 return sid_append_rid(dst, rid);
166 /*****************************************************************
167 Removes the last rid from the end of a sid
168 *****************************************************************/
170 bool sid_split_rid(struct dom_sid *sid, uint32_t *rid)
172 if (sid->num_auths > 0) {
173 sid->num_auths--;
174 if (rid != NULL) {
175 *rid = sid->sub_auths[sid->num_auths];
177 return true;
179 return false;
182 /*****************************************************************
183 Return the last rid from the end of a sid
184 *****************************************************************/
186 bool sid_peek_rid(const struct dom_sid *sid, uint32_t *rid)
188 if (!sid || !rid)
189 return false;
191 if (sid->num_auths > 0) {
192 *rid = sid->sub_auths[sid->num_auths - 1];
193 return true;
195 return false;
198 /*****************************************************************
199 Return the last rid from the end of a sid
200 and check the sid against the exp_dom_sid
201 *****************************************************************/
203 bool sid_peek_check_rid(const struct dom_sid *exp_dom_sid, const struct dom_sid *sid, uint32_t *rid)
205 if (!exp_dom_sid || !sid || !rid)
206 return false;
208 if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
209 return false;
212 if (sid_compare_domain(exp_dom_sid, sid)!=0){
213 *rid=(-1);
214 return false;
217 return sid_peek_rid(sid, rid);
220 /*****************************************************************
221 Copies a sid
222 *****************************************************************/
224 void sid_copy(struct dom_sid *dst, const struct dom_sid *src)
226 int i;
228 ZERO_STRUCTP(dst);
230 dst->sid_rev_num = src->sid_rev_num;
231 dst->num_auths = src->num_auths;
233 memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
235 for (i = 0; i < src->num_auths; i++)
236 dst->sub_auths[i] = src->sub_auths[i];
239 /*****************************************************************
240 Parse a on-the-wire SID (in a DATA_BLOB) to a struct dom_sid.
241 *****************************************************************/
243 bool sid_blob_parse(DATA_BLOB in, struct dom_sid *sid)
245 enum ndr_err_code ndr_err;
246 ndr_err = ndr_pull_struct_blob_all(&in, NULL, sid,
247 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
248 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
249 return false;
251 return true;
254 /*****************************************************************
255 Parse a on-the-wire SID to a struct dom_sid.
256 *****************************************************************/
258 bool sid_parse(const char *inbuf, size_t len, struct dom_sid *sid)
260 DATA_BLOB in = data_blob_const(inbuf, len);
261 return sid_blob_parse(in, sid);
264 /*****************************************************************
265 See if 2 SIDs are in the same domain
266 this just compares the leading sub-auths
267 *****************************************************************/
269 int sid_compare_domain(const struct dom_sid *sid1, const struct dom_sid *sid2)
271 int n, i;
273 n = MIN(sid1->num_auths, sid2->num_auths);
275 for (i = n-1; i >= 0; --i)
276 if (sid1->sub_auths[i] != sid2->sub_auths[i])
277 return sid1->sub_auths[i] - sid2->sub_auths[i];
279 return dom_sid_compare_auth(sid1, sid2);
282 /********************************************************************
283 Add SID to an array SIDs
284 ********************************************************************/
286 NTSTATUS add_sid_to_array(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
287 struct dom_sid **sids, uint32_t *num)
289 *sids = talloc_realloc(mem_ctx, *sids, struct dom_sid,
290 (*num)+1);
291 if (*sids == NULL) {
292 *num = 0;
293 return NT_STATUS_NO_MEMORY;
296 sid_copy(&((*sids)[*num]), sid);
297 *num += 1;
299 return NT_STATUS_OK;
303 /********************************************************************
304 Add SID to an array SIDs ensuring that it is not already there
305 ********************************************************************/
307 NTSTATUS add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
308 struct dom_sid **sids, uint32_t *num_sids)
310 uint32_t i;
312 for (i=0; i<(*num_sids); i++) {
313 if (dom_sid_compare(sid, &(*sids)[i]) == 0)
314 return NT_STATUS_OK;
317 return add_sid_to_array(mem_ctx, sid, sids, num_sids);
320 /********************************************************************
321 Remove SID from an array
322 ********************************************************************/
324 void del_sid_from_array(const struct dom_sid *sid, struct dom_sid **sids,
325 uint32_t *num)
327 struct dom_sid *sid_list = *sids;
328 uint32_t i;
330 for ( i=0; i<*num; i++ ) {
332 /* if we find the SID, then decrement the count
333 and break out of the loop */
335 if (dom_sid_equal(sid, &sid_list[i])) {
336 *num -= 1;
337 break;
341 /* This loop will copy the remainder of the array
342 if i < num of sids in the array */
344 for ( ; i<*num; i++ ) {
345 sid_copy( &sid_list[i], &sid_list[i+1] );
348 return;
351 bool add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
352 uint32_t rid, uint32_t **pp_rids, size_t *p_num)
354 size_t i;
356 for (i=0; i<*p_num; i++) {
357 if ((*pp_rids)[i] == rid)
358 return true;
361 *pp_rids = talloc_realloc(mem_ctx, *pp_rids, uint32_t, *p_num+1);
363 if (*pp_rids == NULL) {
364 *p_num = 0;
365 return false;
368 (*pp_rids)[*p_num] = rid;
369 *p_num += 1;
370 return true;
373 bool is_null_sid(const struct dom_sid *sid)
375 static const struct dom_sid null_sid = {0};
376 return dom_sid_equal(sid, &null_sid);