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