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