libcli: Do not overwrite pointer on realloc failure
[samba.git] / libcli / security / util_sid.c
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
11
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.
16
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.
21
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/>.
24 */
25
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"
30
31 /*
32  * Some useful sids, more well known sids can be found at
33  * http://support.microsoft.com/kb/243330/EN-US/
34  */
35
36
37 /* S-1 */
38 const struct dom_sid global_sid_World_Domain =               /* Everyone domain */
39 { 1, 0, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
40 /* S-1-1 */
41 const struct dom_sid global_sid_World =                      /* Everyone */
42 { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
43 /* S-1-2 */
44 const struct dom_sid global_sid_Local_Authority =            /* Local Authority */
45 { 1, 0, {0,0,0,0,0,2}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
46 /* S-1-3 */
47 const struct dom_sid global_sid_Creator_Owner_Domain =       /* Creator Owner domain */
48 { 1, 0, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
49 /* S-1-5 */
50 const struct dom_sid global_sid_NT_Authority =                  /* NT Authority */
51 { 1, 0, {0,0,0,0,0,5}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
52 /* S-1-5-18 */
53 const struct dom_sid global_sid_System =                        /* System */
54 { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
55 /* S-1-0 */
56 const struct dom_sid global_sid_NULL =                          /* NULL sid */
57 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
58 /* S-1-5-11 */
59 const struct dom_sid global_sid_Authenticated_Users =   /* All authenticated rids */
60 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
61 #if 0
62 /* for documentation S-1-5-12 */
63 const struct dom_sid global_sid_Restriced =                     /* Restriced Code */
64 { 1, 1, {0,0,0,0,0,5}, {12,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
65 #endif
66
67 /* S-1-18 */
68 const struct dom_sid global_sid_Asserted_Identity =       /* Asserted Identity */
69 { 1, 0, {0,0,0,0,0,18}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
70 /* S-1-18-1 */
71 const struct dom_sid global_sid_Asserted_Identity_Service =     /* Asserted Identity Service */
72 { 1, 1, {0,0,0,0,0,18}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
73 /* S-1-18-2 */
74 const struct dom_sid global_sid_Asserted_Identity_Authentication_Authority =    /* Asserted Identity Authentication Authority */
75 { 1, 1, {0,0,0,0,0,18}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
76
77 /* S-1-5-2 */
78 const struct dom_sid global_sid_Network =                       /* Network rids */
79 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
80
81 /* S-1-3 */
82 const struct dom_sid global_sid_Creator_Owner =         /* Creator Owner */
83 { 1, 1, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
84 /* S-1-3-1 */
85 const struct dom_sid global_sid_Creator_Group =         /* Creator Group */
86 { 1, 1, {0,0,0,0,0,3}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
87 /* S-1-3-4 */
88 const struct dom_sid global_sid_Owner_Rights =          /* Owner Rights */
89 { 1, 1, {0,0,0,0,0,3}, {4,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
90 /* S-1-5-7 */
91 const struct dom_sid global_sid_Anonymous =                     /* Anonymous login */
92 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
93 /* S-1-5-9 */
94 const struct dom_sid global_sid_Enterprise_DCs =                /* Enterprise DCs */
95 { 1, 1, {0,0,0,0,0,5}, {9,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
96 /* S-1-5-32 */
97 const struct dom_sid global_sid_Builtin =                       /* Local well-known domain */
98 { 1, 1, {0,0,0,0,0,5}, {32,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
99 /* S-1-5-32-544 */
100 const struct dom_sid global_sid_Builtin_Administrators =        /* Builtin administrators */
101 { 1, 2, {0,0,0,0,0,5}, {32,544,0,0,0,0,0,0,0,0,0,0,0,0,0}};
102 /* S-1-5-32-545 */
103 const struct dom_sid global_sid_Builtin_Users =         /* Builtin users */
104 { 1, 2, {0,0,0,0,0,5}, {32,545,0,0,0,0,0,0,0,0,0,0,0,0,0}};
105 /* S-1-5-32-546 */
106 const struct dom_sid global_sid_Builtin_Guests =                /* Builtin guest users */
107 { 1, 2, {0,0,0,0,0,5}, {32,546,0,0,0,0,0,0,0,0,0,0,0,0,0}};
108 /* S-1-5-32-547 */
109 const struct dom_sid global_sid_Builtin_Power_Users =   /* Builtin power users */
110 { 1, 2, {0,0,0,0,0,5}, {32,547,0,0,0,0,0,0,0,0,0,0,0,0,0}};
111 /* S-1-5-32-548 */
112 const struct dom_sid global_sid_Builtin_Account_Operators =     /* Builtin account operators */
113 { 1, 2, {0,0,0,0,0,5}, {32,548,0,0,0,0,0,0,0,0,0,0,0,0,0}};
114 /* S-1-5-32-549 */
115 const struct dom_sid global_sid_Builtin_Server_Operators =      /* Builtin server operators */
116 { 1, 2, {0,0,0,0,0,5}, {32,549,0,0,0,0,0,0,0,0,0,0,0,0,0}};
117 /* S-1-5-32-550 */
118 const struct dom_sid global_sid_Builtin_Print_Operators =       /* Builtin print operators */
119 { 1, 2, {0,0,0,0,0,5}, {32,550,0,0,0,0,0,0,0,0,0,0,0,0,0}};
120 /* S-1-5-32-551 */
121 const struct dom_sid global_sid_Builtin_Backup_Operators =      /* Builtin backup operators */
122 { 1, 2, {0,0,0,0,0,5}, {32,551,0,0,0,0,0,0,0,0,0,0,0,0,0}};
123 /* S-1-5-32-552 */
124 const struct dom_sid global_sid_Builtin_Replicator =            /* Builtin replicator */
125 { 1, 2, {0,0,0,0,0,5}, {32,552,0,0,0,0,0,0,0,0,0,0,0,0,0}};
126 /* S-1-5-32-554 */
127 const struct dom_sid global_sid_Builtin_PreWin2kAccess =        /* Builtin pre win2k access */
128 { 1, 2, {0,0,0,0,0,5}, {32,554,0,0,0,0,0,0,0,0,0,0,0,0,0}};
129
130 /* S-1-22-1 */
131 const struct dom_sid global_sid_Unix_Users =                    /* Unmapped Unix users */
132 { 1, 1, {0,0,0,0,0,22}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
133 /* S-1-22-2 */
134 const struct dom_sid global_sid_Unix_Groups =                   /* Unmapped Unix groups */
135 { 1, 1, {0,0,0,0,0,22}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
136
137 /*
138  * http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
139  */
140 /* S-1-5-88 */
141 const struct dom_sid global_sid_Unix_NFS =             /* MS NFS and Apple style */
142 { 1, 1, {0,0,0,0,0,5}, {88,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
143 /* S-1-5-88-1 */
144 const struct dom_sid global_sid_Unix_NFS_Users =                /* Unix uid, MS NFS and Apple style */
145 { 1, 2, {0,0,0,0,0,5}, {88,1,0,0,0,0,0,0,0,0,0,0,0,0,0}};
146 /* S-1-5-88-2 */
147 const struct dom_sid global_sid_Unix_NFS_Groups =               /* Unix gid, MS NFS and Apple style */
148 { 1, 2, {0,0,0,0,0,5}, {88,2,0,0,0,0,0,0,0,0,0,0,0,0,0}};
149 /* S-1-5-88-3 */
150 const struct dom_sid global_sid_Unix_NFS_Mode =                 /* Unix mode */
151 { 1, 2, {0,0,0,0,0,5}, {88,3,0,0,0,0,0,0,0,0,0,0,0,0,0}};
152 /* Unused, left here for documentary purposes */
153 #if 0
154 const struct dom_sid global_sid_Unix_NFS_Other =                /* Unix other, MS NFS and Apple style */
155 { 1, 2, {0,0,0,0,0,5}, {88,4,0,0,0,0,0,0,0,0,0,0,0,0,0}};
156 #endif
157
158 /* Unused, left here for documentary purposes */
159 #if 0
160 #define SECURITY_NULL_SID_AUTHORITY    0
161 #define SECURITY_WORLD_SID_AUTHORITY   1
162 #define SECURITY_LOCAL_SID_AUTHORITY   2
163 #define SECURITY_CREATOR_SID_AUTHORITY 3
164 #define SECURITY_NT_AUTHORITY          5
165 #endif
166
167 static struct dom_sid system_sid_array[1] =
168 { { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
169 static const struct security_token system_token = {
170         .num_sids       = ARRAY_SIZE(system_sid_array),
171         .sids           = system_sid_array,
172         .privilege_mask = SE_ALL_PRIVS
173 };
174
175 /****************************************************************************
176  Lookup string names for SID types.
177 ****************************************************************************/
178
179 static const struct {
180         enum lsa_SidType sid_type;
181         const char *string;
182 } sid_name_type[] = {
183         {SID_NAME_USE_NONE, "None"},
184         {SID_NAME_USER, "User"},
185         {SID_NAME_DOM_GRP, "Domain Group"},
186         {SID_NAME_DOMAIN, "Domain"},
187         {SID_NAME_ALIAS, "Local Group"},
188         {SID_NAME_WKN_GRP, "Well-known Group"},
189         {SID_NAME_DELETED, "Deleted Account"},
190         {SID_NAME_INVALID, "Invalid Account"},
191         {SID_NAME_UNKNOWN, "UNKNOWN"},
192         {SID_NAME_COMPUTER, "Computer"}
193 };
194
195 const char *sid_type_lookup(uint32_t sid_type)
196 {
197         int i;
198
199         /* Look through list */
200         for (i=0; i < ARRAY_SIZE(sid_name_type); i++) {
201                 if (sid_name_type[i].sid_type == sid_type) {
202                         return sid_name_type[i].string;
203                 }
204         }
205
206         /* Default return */
207         return "SID *TYPE* is INVALID";
208 }
209
210 /**************************************************************************
211  Create the SYSTEM token.
212 ***************************************************************************/
213
214 const struct security_token *get_system_token(void)
215 {
216         return &system_token;
217 }
218
219 bool sid_compose(struct dom_sid *dst, const struct dom_sid *domain_sid, uint32_t rid)
220 {
221         sid_copy(dst, domain_sid);
222         return sid_append_rid(dst, rid);
223 }
224
225 /*****************************************************************
226  Removes the last rid from the end of a sid
227 *****************************************************************/
228
229 bool sid_split_rid(struct dom_sid *sid, uint32_t *rid)
230 {
231         if (sid->num_auths > 0) {
232                 sid->num_auths--;
233                 if (rid != NULL) {
234                         *rid = sid->sub_auths[sid->num_auths];
235                 }
236                 return true;
237         }
238         return false;
239 }
240
241 /*****************************************************************
242  Return the last rid from the end of a sid
243 *****************************************************************/
244
245 bool sid_peek_rid(const struct dom_sid *sid, uint32_t *rid)
246 {
247         if (!sid || !rid)
248                 return false;
249
250         if (sid->num_auths > 0) {
251                 *rid = sid->sub_auths[sid->num_auths - 1];
252                 return true;
253         }
254         return false;
255 }
256
257 /*****************************************************************
258  Return the last rid from the end of a sid
259  and check the sid against the exp_dom_sid
260 *****************************************************************/
261
262 bool sid_peek_check_rid(const struct dom_sid *exp_dom_sid, const struct dom_sid *sid, uint32_t *rid)
263 {
264         if (!exp_dom_sid || !sid || !rid)
265                 return false;
266
267         if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
268                 return false;
269         }
270
271         if (sid_compare_domain(exp_dom_sid, sid)!=0){
272                 *rid=(-1);
273                 return false;
274         }
275
276         return sid_peek_rid(sid, rid);
277 }
278
279 /*****************************************************************
280  Copies a sid
281 *****************************************************************/
282
283 void sid_copy(struct dom_sid *dst, const struct dom_sid *src)
284 {
285         int i;
286
287         *dst = (struct dom_sid) {
288                 .sid_rev_num = src->sid_rev_num,
289                 .num_auths = src->num_auths,
290         };
291
292         memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
293
294         for (i = 0; i < src->num_auths; i++)
295                 dst->sub_auths[i] = src->sub_auths[i];
296 }
297
298 /*****************************************************************
299  Parse a on-the-wire SID to a struct dom_sid.
300 *****************************************************************/
301
302 bool sid_parse(const uint8_t *inbuf, size_t len, struct dom_sid *sid)
303 {
304         DATA_BLOB in = data_blob_const(inbuf, len);
305         enum ndr_err_code ndr_err;
306
307         ndr_err = ndr_pull_struct_blob_all(
308                 &in, NULL, sid, (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
309         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
310                 return false;
311         }
312         return true;
313 }
314
315 /*****************************************************************
316  See if 2 SIDs are in the same domain
317  this just compares the leading sub-auths
318 *****************************************************************/
319
320 int sid_compare_domain(const struct dom_sid *sid1, const struct dom_sid *sid2)
321 {
322         int n, i;
323
324         n = MIN(sid1->num_auths, sid2->num_auths);
325
326         for (i = n-1; i >= 0; --i)
327                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
328                         return sid1->sub_auths[i] - sid2->sub_auths[i];
329
330         return dom_sid_compare_auth(sid1, sid2);
331 }
332
333 /********************************************************************
334  Add SID to an array SIDs
335 ********************************************************************/
336
337 NTSTATUS add_sid_to_array(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
338                           struct dom_sid **sids, uint32_t *num)
339 {
340         struct dom_sid *tmp;
341
342         tmp = talloc_realloc(mem_ctx, *sids, struct dom_sid, (*num)+1);
343         if (tmp == NULL) {
344                 *num = 0;
345                 return NT_STATUS_NO_MEMORY;
346         }
347         *sids = tmp;
348
349         sid_copy(&((*sids)[*num]), sid);
350         *num += 1;
351
352         return NT_STATUS_OK;
353 }
354
355
356 /********************************************************************
357  Add SID to an array SIDs ensuring that it is not already there
358 ********************************************************************/
359
360 NTSTATUS add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
361                                  struct dom_sid **sids, uint32_t *num_sids)
362 {
363         uint32_t i;
364
365         for (i=0; i<(*num_sids); i++) {
366                 if (dom_sid_equal(sid, &(*sids)[i])) {
367                         return NT_STATUS_OK;
368                 }
369         }
370
371         return add_sid_to_array(mem_ctx, sid, sids, num_sids);
372 }
373
374 /********************************************************************
375  Remove SID from an array
376 ********************************************************************/
377
378 void del_sid_from_array(const struct dom_sid *sid, struct dom_sid **sids,
379                         uint32_t *num)
380 {
381         struct dom_sid *sid_list = *sids;
382         uint32_t i;
383
384         for ( i=0; i<*num; i++ ) {
385
386                 /* if we find the SID, then decrement the count
387                    and break out of the loop */
388
389                 if (dom_sid_equal(sid, &sid_list[i])) {
390                         *num -= 1;
391                         break;
392                 }
393         }
394
395         /* This loop will copy the remainder of the array
396            if i < num of sids in the array */
397
398         for ( ; i<*num; i++ ) {
399                 sid_copy( &sid_list[i], &sid_list[i+1] );
400         }
401
402         return;
403 }
404
405 bool add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
406                              uint32_t rid, uint32_t **pp_rids, size_t *p_num)
407 {
408         size_t i;
409
410         for (i=0; i<*p_num; i++) {
411                 if ((*pp_rids)[i] == rid)
412                         return true;
413         }
414
415         *pp_rids = talloc_realloc(mem_ctx, *pp_rids, uint32_t, *p_num+1);
416
417         if (*pp_rids == NULL) {
418                 *p_num = 0;
419                 return false;
420         }
421
422         (*pp_rids)[*p_num] = rid;
423         *p_num += 1;
424         return true;
425 }
426
427 bool is_null_sid(const struct dom_sid *sid)
428 {
429         static const struct dom_sid null_sid = {0};
430         return dom_sid_equal(sid, &null_sid);
431 }