summaryrefslogtreecommitdiff
path: root/source3/lib/util_seaccess.c
blob: 68f900b34dbf68c9c21dc2328aae6d93d1c44efd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
   Unix SMB/Netbios implementation.
   Version 2.0
   Copyright (C) Luke Kenneth Casson Leighton 1996-2000.
   Copyright (C) Tim Potter 2000.
   Copyright (C) Re-written by Jeremy Allison 2000.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "nterr.h"
#include "sids.h"

extern int DEBUGLEVEL;

/**********************************************************************************
 Check if this ACE has a SID in common with the token.
**********************************************************************************/

static BOOL token_sid_in_ace( NT_USER_TOKEN *token, SEC_ACE *ace)
{
	size_t i;

	for (i = 0; i < token->num_sids; i++) {
		if (sid_equal(&ace->sid, &token->user_sids[i]))
			return True;
	}

	return False;
}

/*********************************************************************************
 Check an ACE against a SID.  We return the remaining needed permission
 bits not yet granted. Zero means permission allowed (no more needed bits).
**********************************************************************************/

static uint32 check_ace(SEC_ACE *ace, NT_USER_TOKEN *token, uint32 acc_desired, uint32 *status)
{
	uint32 mask = ace->info.mask;

#if 0

	/* I think there is some aspect of inheritable ACEs that we don't
	   understand.  A 'Manage Documents' permission has the following
	   ACE entries (after generic mapping has been applied):

	   S-1-5-21-1067277791-1719175008-3000797951-1033 0 9 0x000f000c
	   S-1-5-21-1067277791-1719175008-3000797951-1033 0 2 0x00020000

	   Now a user wanting to print calls se_access_check() with desired
	   access PRINTER_ACCESS_USE (0x00000008).  This is only allowed if
	   the inherit only ACE, flags & SEC_ACE_FLAG_INHERIT_ONLY (0x8) is
	   checked.  A similar argument is used to explain how a user with
	   'Full Control' permission can print.

	   Having both the flags SEC_ACE_FLAG_INHERIT_ONLY and
	   SEC_ACE_FLAG_OBJECT_INHERIT set in an ACE doesn't seem to make
	   sense.  According to the MSDN, an inherit only ACE "indicates an
	   [...] ACE which does not control access to the object to which
	   it is attached" and an object inherit ACE for "non-container
	   child objects [they] inherit the ACE as an effective ACE".
	   These two flags don't seem to make sense when combined.  Does
	   the object inherit override the inherit only flag?  We are also
	   talking about access to a printer object, not a printer job so
	   inheritance shouldn't even be involved.  -tpot */

	/*
	 * Inherit only is ignored.
	 */

	if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
		return acc_desired;
	}

#endif

	/*
	 * If this ACE has no SID in common with the token,
	 * ignore it as it cannot be used to make an access
	 * determination.
	 */

	if (!token_sid_in_ace( token, ace))
		return acc_desired;	

	switch (ace->type) {
		case SEC_ACE_TYPE_ACCESS_ALLOWED:
			/*
			 * This is explicitly allowed.
			 * Remove the bits from the remaining
			 * access required. Return the remaining
			 * bits needed.
			 */
			acc_desired &= ~mask;
			break;
		case SEC_ACE_TYPE_ACCESS_DENIED:
			/*
			 * This is explicitly denied.
			 * If any bits match terminate here,
			 * we are denied.
			 */
			if (acc_desired & mask) {
				*status = NT_STATUS_ACCESS_DENIED;
				return 0xFFFFFFFF;
			}
			break;
		case SEC_ACE_TYPE_SYSTEM_ALARM:
		case SEC_ACE_TYPE_SYSTEM_AUDIT:
			*status = NT_STATUS_NOT_IMPLEMENTED;
			return 0xFFFFFFFF;
		default:
			*status = NT_STATUS_INVALID_PARAMETER;
			return 0xFFFFFFFF;
	}

	return acc_desired;
}

/*********************************************************************************
 Maximum access was requested. Calculate the max possible. Fail if it doesn't
 include other bits requested.
**********************************************************************************/ 

static BOOL get_max_access( SEC_ACL *acl, NT_USER_TOKEN *token, uint32 *granted, uint32 desired, uint32 *status)
{
	uint32 acc_denied = 0;
	uint32 acc_granted = 0;
	size_t i;
	
	for ( i = 0 ; i < acl->num_aces; i++) {
		SEC_ACE *ace = &acl->ace[i];
		uint32 mask = ace->info.mask;

		if (!token_sid_in_ace( token, ace))
			continue;

		switch (ace->type) {
			case SEC_ACE_TYPE_ACCESS_ALLOWED:
				acc_granted |= (mask & ~acc_denied);
				break;
			case SEC_ACE_TYPE_ACCESS_DENIED:
				acc_denied |= (mask & ~acc_granted);
				break;
			case SEC_ACE_TYPE_SYSTEM_ALARM:
			case SEC_ACE_TYPE_SYSTEM_AUDIT:
				*status = NT_STATUS_NOT_IMPLEMENTED;
				*granted = 0;
				return False;
			default:
				*status = NT_STATUS_INVALID_PARAMETER;
				*granted = 0;
				return False;
		}                           
	}

	/*
	 * If we were granted no access, or we desired bits that we
	 * didn't get, then deny.
	 */

	if ((acc_granted == 0) || ((acc_granted & desired) != desired)) {
		*status = NT_STATUS_ACCESS_DENIED;
		*granted = 0;
		return False;
	}

	/*
	 * Return the access we did get.
	 */

	*granted = acc_granted;
	*status = NT_STATUS_NOPROBLEMO;
	return True;
}

/* Map generic access rights to object specific rights.  This technique is
   used to give meaning to assigning read, write, execute and all access to
   objects.  Each type of object has its own mapping of generic to object
   specific access rights. */

void se_map_generic(uint32 *access_mask, struct generic_mapping *mapping)
{
	uint32 old_mask = *access_mask;

	if (*access_mask & GENERIC_READ_ACCESS) {
		*access_mask &= ~GENERIC_READ_ACCESS;
		*access_mask |= mapping->generic_read;
	}

	if (*access_mask & GENERIC_WRITE_ACCESS) {
		*access_mask &= ~GENERIC_WRITE_ACCESS;
		*access_mask |= mapping->generic_write;
	}

	if (*access_mask & GENERIC_EXECUTE_ACCESS) {
		*access_mask &= ~GENERIC_EXECUTE_ACCESS;
		*access_mask |= mapping->generic_execute;
	}

	if (*access_mask & GENERIC_ALL_ACCESS) {
		*access_mask &= ~GENERIC_ALL_ACCESS;
		*access_mask |= mapping->generic_all;
	}

	if (old_mask != *access_mask) {
		DEBUG(10, ("se_map_generic(): mapped mask 0x%08x to 0x%08x\n",
			   old_mask, *access_mask));
	}
}

/*****************************************************************************
 Check access rights of a user against a security descriptor.  Look at
 each ACE in the security descriptor until an access denied ACE denies
 any of the desired rights to the user or any of the users groups, or one
 or more ACEs explicitly grant all requested access rights.  See
 "Access-Checking" document in MSDN.
*****************************************************************************/ 

BOOL se_access_check(SEC_DESC *sd, struct current_user *user,
		     uint32 acc_desired, uint32 *acc_granted, uint32 *status)
{
	extern NT_USER_TOKEN anonymous_token;
	size_t i;
	SEC_ACL *acl;
	fstring sid_str;
	NT_USER_TOKEN *token = user->nt_user_token ? user->nt_user_token : &anonymous_token;
	uint32 tmp_acc_desired = acc_desired;

	if (!status || !acc_granted)
		return False;

	*status = NT_STATUS_NOPROBLEMO;
	*acc_granted = 0;

	DEBUG(10,("se_access_check: requested access %x, for uid %u\n", 
				(unsigned int)acc_desired, (unsigned int)user->uid ));

	/*
	 * No security descriptor or security descriptor with no DACL
	 * present allows all access.
	 */

	/* ACL must have something in it */

	if (!sd || (sd && (!(sd->type & SEC_DESC_DACL_PRESENT) || sd->dacl == NULL))) {
		*status = NT_STATUS_NOPROBLEMO;
		*acc_granted = acc_desired;
		DEBUG(5, ("se_access_check: no sd or blank DACL, access allowed\n"));
		return True;
	}

	/* The user sid is the first in the token */

	DEBUG(3, ("se_access_check: user sid is %s\n", sid_to_string(sid_str, &token->user_sids[0]) ));

	for (i = 1; i < token->num_sids; i++) {
		DEBUG(3, ("se_access_check: also %s\n",
			  sid_to_string(sid_str, &token->user_sids[i])));
	}

	/* Is the token the owner of the SID ? */

	if (sd->owner_sid) {
		for (i = 0; i < token->num_sids; i++) {
			if (sid_equal(&token->user_sids[i], sd->owner_sid)) {
				/*
				 * The owner always has SEC_RIGHTS_WRITE_DAC & READ_CONTROL.
				 */
				if (tmp_acc_desired & WRITE_DAC_ACCESS)
					tmp_acc_desired &= ~WRITE_DAC_ACCESS;
				if (tmp_acc_desired & READ_CONTROL_ACCESS)
					tmp_acc_desired &= ~READ_CONTROL_ACCESS;
			}
		}
	}

	acl = sd->dacl;

	if (tmp_acc_desired & MAXIMUM_ALLOWED_ACCESS) {
		tmp_acc_desired &= ~MAXIMUM_ALLOWED_ACCESS;
		return get_max_access( acl, token, acc_granted, tmp_acc_desired, status);
	}

	for ( i = 0 ; i < acl->num_aces && tmp_acc_desired != 0; i++) {
		SEC_ACE *ace = &acl->ace[i];

		DEBUG(10,("se_access_check: ACE %u: type %d, flags = 0x%02x, SID = %s mask = %x, current desired = %x\n",
			  (unsigned int)i, ace->type, ace->flags,
			  sid_to_string(sid_str, &ace->sid),
			  (unsigned int) ace->info.mask, 
			  (unsigned int)tmp_acc_desired ));

		tmp_acc_desired = check_ace( ace, token, tmp_acc_desired, status);
		if (*status != NT_STATUS_NOPROBLEMO) {
			*acc_granted = 0;
			DEBUG(5,("se_access_check: ACE %u denied with status %x.\n", (unsigned int)i, (unsigned int)*status ));
			return False;
		}
	}

	/*
	 * If there are no more desired permissions left then
	 * access was allowed.
	 */

	if (tmp_acc_desired == 0) {
		*acc_granted = acc_desired;
		*status = NT_STATUS_NOPROBLEMO;
		DEBUG(5,("se_access_check: access (%x) granted.\n", (unsigned int)acc_desired ));
		return True;
	}
		
	*acc_granted = 0;
	*status = NT_STATUS_ACCESS_DENIED;
	DEBUG(5,("se_access_check: access (%x) denied.\n", (unsigned int)acc_desired ));
	return False;
}