shlwapi: SHMapHandle should not set error when NULL is passed as hShared.
[wine.git] / server / token.c
blobe58e6a4fe77b971ff1a546c613f45568ced620c7
1 /*
2 * Tokens
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2003 Mike McCormack
6 * Copyright (C) 2005 Robert Shearman
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <unistd.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
36 #include "handle.h"
37 #include "thread.h"
38 #include "process.h"
39 #include "request.h"
40 #include "security.h"
42 #include "wine/unicode.h"
44 #define MAX_SUBAUTH_COUNT 1
46 const LUID SeIncreaseQuotaPrivilege = { 5, 0 };
47 const LUID SeSecurityPrivilege = { 8, 0 };
48 const LUID SeTakeOwnershipPrivilege = { 9, 0 };
49 const LUID SeLoadDriverPrivilege = { 10, 0 };
50 const LUID SeSystemProfilePrivilege = { 11, 0 };
51 const LUID SeSystemtimePrivilege = { 12, 0 };
52 const LUID SeProfileSingleProcessPrivilege = { 13, 0 };
53 const LUID SeIncreaseBasePriorityPrivilege = { 14, 0 };
54 const LUID SeCreatePagefilePrivilege = { 15, 0 };
55 const LUID SeBackupPrivilege = { 17, 0 };
56 const LUID SeRestorePrivilege = { 18, 0 };
57 const LUID SeShutdownPrivilege = { 19, 0 };
58 const LUID SeDebugPrivilege = { 20, 0 };
59 const LUID SeSystemEnvironmentPrivilege = { 22, 0 };
60 const LUID SeChangeNotifyPrivilege = { 23, 0 };
61 const LUID SeRemoteShutdownPrivilege = { 24, 0 };
62 const LUID SeUndockPrivilege = { 25, 0 };
63 const LUID SeManageVolumePrivilege = { 28, 0 };
64 const LUID SeImpersonatePrivilege = { 29, 0 };
65 const LUID SeCreateGlobalPrivilege = { 30, 0 };
67 #define SID_N(n) struct /* same fields as struct SID */ \
68 { \
69 BYTE Revision; \
70 BYTE SubAuthorityCount; \
71 SID_IDENTIFIER_AUTHORITY IdentifierAuthority; \
72 DWORD SubAuthority[n]; \
75 static const SID world_sid = { SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY }, { SECURITY_WORLD_RID } };
76 static const SID local_sid = { SID_REVISION, 1, { SECURITY_LOCAL_SID_AUTHORITY }, { SECURITY_LOCAL_RID } };
77 static const SID interactive_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_INTERACTIVE_RID } };
78 static const SID anonymous_logon_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_ANONYMOUS_LOGON_RID } };
79 static const SID authenticated_user_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_AUTHENTICATED_USER_RID } };
80 static const SID local_system_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SYSTEM_RID } };
81 static const SID high_label_sid = { SID_REVISION, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY }, { SECURITY_MANDATORY_HIGH_RID } };
82 static const SID_N(5) local_user_sid = { SID_REVISION, 5, { SECURITY_NT_AUTHORITY }, { SECURITY_NT_NON_UNIQUE, 0, 0, 0, 1000 } };
83 static const SID_N(2) builtin_admins_sid = { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS } };
84 static const SID_N(2) builtin_users_sid = { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS } };
85 static const SID_N(3) builtin_logon_sid = { SID_REVISION, 3, { SECURITY_NT_AUTHORITY }, { SECURITY_LOGON_IDS_RID, 0, 0 } };
87 const PSID security_world_sid = (PSID)&world_sid;
88 static const PSID security_local_sid = (PSID)&local_sid;
89 static const PSID security_interactive_sid = (PSID)&interactive_sid;
90 static const PSID security_authenticated_user_sid = (PSID)&authenticated_user_sid;
91 const PSID security_local_system_sid = (PSID)&local_system_sid;
92 const PSID security_local_user_sid = (PSID)&local_user_sid;
93 const PSID security_builtin_admins_sid = (PSID)&builtin_admins_sid;
94 const PSID security_builtin_users_sid = (PSID)&builtin_users_sid;
95 const PSID security_high_label_sid = (PSID)&high_label_sid;
97 static luid_t prev_luid_value = { 1000, 0 };
99 struct token
101 struct object obj; /* object header */
102 luid_t token_id; /* system-unique id of token */
103 luid_t modified_id; /* new id allocated every time token is modified */
104 struct list privileges; /* privileges available to the token */
105 struct list groups; /* groups that the user of this token belongs to (sid_and_attributes) */
106 SID *user; /* SID of user this token represents */
107 SID *owner; /* SID of owner (points to user or one of groups) */
108 SID *primary_group; /* SID of user's primary group (points to one of groups) */
109 unsigned primary; /* is this a primary or impersonation token? */
110 ACL *default_dacl; /* the default DACL to assign to objects created by this user */
111 TOKEN_SOURCE source; /* source of the token */
112 int impersonation_level; /* impersonation level this token is capable of if non-primary token */
115 struct privilege
117 struct list entry;
118 LUID luid;
119 unsigned enabled : 1; /* is the privilege currently enabled? */
120 unsigned def : 1; /* is the privilege enabled by default? */
123 struct group
125 struct list entry;
126 unsigned enabled : 1; /* is the sid currently enabled? */
127 unsigned def : 1; /* is the sid enabled by default? */
128 unsigned logon : 1; /* is this a logon sid? */
129 unsigned mandatory: 1; /* is this sid always enabled? */
130 unsigned owner : 1; /* can this sid be an owner of an object? */
131 unsigned resource : 1; /* is this a domain-local group? */
132 unsigned deny_only: 1; /* is this a sid that should be use for denying only? */
133 SID sid;
136 static void token_dump( struct object *obj, int verbose );
137 static struct object_type *token_get_type( struct object *obj );
138 static unsigned int token_map_access( struct object *obj, unsigned int access );
139 static void token_destroy( struct object *obj );
141 static const struct object_ops token_ops =
143 sizeof(struct token), /* size */
144 token_dump, /* dump */
145 token_get_type, /* get_type */
146 no_add_queue, /* add_queue */
147 NULL, /* remove_queue */
148 NULL, /* signaled */
149 NULL, /* satisfied */
150 no_signal, /* signal */
151 no_get_fd, /* get_fd */
152 token_map_access, /* map_access */
153 default_get_sd, /* get_sd */
154 default_set_sd, /* set_sd */
155 no_lookup_name, /* lookup_name */
156 no_link_name, /* link_name */
157 NULL, /* unlink_name */
158 no_open_file, /* open_file */
159 no_kernel_obj_list, /* get_kernel_obj_list */
160 no_close_handle, /* close_handle */
161 token_destroy /* destroy */
164 static void token_dump( struct object *obj, int verbose )
166 struct token *token = (struct token *)obj;
167 assert( obj->ops == &token_ops );
168 fprintf( stderr, "Token id=%d.%u primary=%u impersonation level=%d\n", token->token_id.high_part,
169 token->token_id.low_part, token->primary, token->impersonation_level );
172 static struct object_type *token_get_type( struct object *obj )
174 static const WCHAR name[] = {'T','o','k','e','n'};
175 static const struct unicode_str str = { name, sizeof(name) };
176 return get_object_type( &str );
179 static unsigned int token_map_access( struct object *obj, unsigned int access )
181 if (access & GENERIC_READ) access |= TOKEN_READ;
182 if (access & GENERIC_WRITE) access |= TOKEN_WRITE;
183 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
184 if (access & GENERIC_ALL) access |= TOKEN_ALL_ACCESS;
185 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
188 static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] )
190 int i;
191 SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) );
192 if (!sid) return NULL;
193 sid->Revision = SID_REVISION;
194 sid->SubAuthorityCount = subauthcount;
195 sid->IdentifierAuthority = *idauthority;
196 for (i = 0; i < subauthcount; i++)
197 sid->SubAuthority[i] = subauth[i];
198 return sid;
201 void security_set_thread_token( struct thread *thread, obj_handle_t handle )
203 if (!handle)
205 if (thread->token)
206 release_object( thread->token );
207 thread->token = NULL;
209 else
211 struct token *token = (struct token *)get_handle_obj( current->process,
212 handle,
213 TOKEN_IMPERSONATE,
214 &token_ops );
215 if (token)
217 if (thread->token)
218 release_object( thread->token );
219 thread->token = token;
224 const SID *security_unix_uid_to_sid( uid_t uid )
226 /* very simple mapping: either the current user or not the current user */
227 if (uid == getuid())
228 return (const SID *)&local_user_sid;
229 else
230 return &anonymous_logon_sid;
233 static int acl_is_valid( const ACL *acl, data_size_t size )
235 ULONG i;
236 const ACE_HEADER *ace;
238 if (size < sizeof(ACL))
239 return FALSE;
241 size = min(size, MAX_ACL_LEN);
243 size -= sizeof(ACL);
245 ace = (const ACE_HEADER *)(acl + 1);
246 for (i = 0; i < acl->AceCount; i++)
248 const SID *sid;
249 data_size_t sid_size;
251 if (size < sizeof(ACE_HEADER))
252 return FALSE;
253 if (size < ace->AceSize)
254 return FALSE;
255 size -= ace->AceSize;
256 switch (ace->AceType)
258 case ACCESS_DENIED_ACE_TYPE:
259 sid = (const SID *)&((const ACCESS_DENIED_ACE *)ace)->SidStart;
260 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart);
261 break;
262 case ACCESS_ALLOWED_ACE_TYPE:
263 sid = (const SID *)&((const ACCESS_ALLOWED_ACE *)ace)->SidStart;
264 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart);
265 break;
266 case SYSTEM_AUDIT_ACE_TYPE:
267 sid = (const SID *)&((const SYSTEM_AUDIT_ACE *)ace)->SidStart;
268 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart);
269 break;
270 case SYSTEM_ALARM_ACE_TYPE:
271 sid = (const SID *)&((const SYSTEM_ALARM_ACE *)ace)->SidStart;
272 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_ALARM_ACE, SidStart);
273 break;
274 case SYSTEM_MANDATORY_LABEL_ACE_TYPE:
275 sid = (const SID *)&((const SYSTEM_MANDATORY_LABEL_ACE *)ace)->SidStart;
276 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart);
277 break;
278 default:
279 return FALSE;
281 if (sid_size < FIELD_OFFSET(SID, SubAuthority[0]) || sid_size < security_sid_len( sid ))
282 return FALSE;
283 ace = ace_next( ace );
285 return TRUE;
288 /* checks whether all members of a security descriptor fit inside the size
289 * of memory specified */
290 int sd_is_valid( const struct security_descriptor *sd, data_size_t size )
292 size_t offset = sizeof(struct security_descriptor);
293 const SID *group;
294 const SID *owner;
295 const ACL *sacl;
296 const ACL *dacl;
297 int dummy;
299 if (size < offset)
300 return FALSE;
302 if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
303 (offset + sd->owner_len > size))
304 return FALSE;
305 owner = sd_get_owner( sd );
306 if (owner)
308 size_t needed_size = security_sid_len( owner );
309 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
310 return FALSE;
312 offset += sd->owner_len;
314 if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
315 (offset + sd->group_len > size))
316 return FALSE;
317 group = sd_get_group( sd );
318 if (group)
320 size_t needed_size = security_sid_len( group );
321 if ((sd->group_len < sizeof(SID)) || (needed_size > sd->group_len))
322 return FALSE;
324 offset += sd->group_len;
326 if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
327 return FALSE;
328 sacl = sd_get_sacl( sd, &dummy );
329 if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
330 return FALSE;
331 offset += sd->sacl_len;
333 if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
334 return FALSE;
335 dacl = sd_get_dacl( sd, &dummy );
336 if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
337 return FALSE;
338 offset += sd->dacl_len;
340 return TRUE;
343 /* extract security labels from SACL */
344 ACL *extract_security_labels( const ACL *sacl )
346 size_t size = sizeof(ACL);
347 const ACE_HEADER *ace;
348 ACE_HEADER *label_ace;
349 unsigned int i, count = 0;
350 ACL *label_acl;
352 ace = (const ACE_HEADER *)(sacl + 1);
353 for (i = 0; i < sacl->AceCount; i++, ace = ace_next( ace ))
355 if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
357 size += ace->AceSize;
358 count++;
362 label_acl = mem_alloc( size );
363 if (!label_acl) return NULL;
365 label_acl->AclRevision = sacl->AclRevision;
366 label_acl->Sbz1 = 0;
367 label_acl->AclSize = size;
368 label_acl->AceCount = count;
369 label_acl->Sbz2 = 0;
370 label_ace = (ACE_HEADER *)(label_acl + 1);
372 ace = (const ACE_HEADER *)(sacl + 1);
373 for (i = 0; i < sacl->AceCount; i++, ace = ace_next( ace ))
375 if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
377 memcpy( label_ace, ace, ace->AceSize );
378 label_ace = (ACE_HEADER *)ace_next( label_ace );
381 return label_acl;
384 /* replace security labels in an existing SACL */
385 ACL *replace_security_labels( const ACL *old_sacl, const ACL *new_sacl )
387 const ACE_HEADER *ace;
388 ACE_HEADER *replaced_ace;
389 size_t size = sizeof(ACL);
390 unsigned int i, count = 0;
391 BYTE revision = ACL_REVISION;
392 ACL *replaced_acl;
394 if (old_sacl)
396 revision = max( revision, old_sacl->AclRevision );
397 ace = (const ACE_HEADER *)(old_sacl + 1);
398 for (i = 0; i < old_sacl->AceCount; i++, ace = ace_next( ace ))
400 if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
401 size += ace->AceSize;
402 count++;
406 if (new_sacl)
408 revision = max( revision, new_sacl->AclRevision );
409 ace = (const ACE_HEADER *)(new_sacl + 1);
410 for (i = 0; i < new_sacl->AceCount; i++, ace = ace_next( ace ))
412 if (ace->AceType != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
413 size += ace->AceSize;
414 count++;
418 replaced_acl = mem_alloc( size );
419 if (!replaced_acl) return NULL;
421 replaced_acl->AclRevision = revision;
422 replaced_acl->Sbz1 = 0;
423 replaced_acl->AclSize = size;
424 replaced_acl->AceCount = count;
425 replaced_acl->Sbz2 = 0;
426 replaced_ace = (ACE_HEADER *)(replaced_acl + 1);
428 if (old_sacl)
430 ace = (const ACE_HEADER *)(old_sacl + 1);
431 for (i = 0; i < old_sacl->AceCount; i++, ace = ace_next( ace ))
433 if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
434 memcpy( replaced_ace, ace, ace->AceSize );
435 replaced_ace = (ACE_HEADER *)ace_next( replaced_ace );
439 if (new_sacl)
441 ace = (const ACE_HEADER *)(new_sacl + 1);
442 for (i = 0; i < new_sacl->AceCount; i++, ace = ace_next( ace ))
444 if (ace->AceType != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
445 memcpy( replaced_ace, ace, ace->AceSize );
446 replaced_ace = (ACE_HEADER *)ace_next( replaced_ace );
450 return replaced_acl;
453 /* maps from generic rights to specific rights as given by a mapping */
454 static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping)
456 if (*mask & GENERIC_READ) *mask |= mapping->GenericRead;
457 if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite;
458 if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute;
459 if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll;
460 *mask &= 0x0FFFFFFF;
463 static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 )
465 return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart);
468 static inline void allocate_luid( luid_t *luid )
470 prev_luid_value.low_part++;
471 *luid = prev_luid_value;
474 DECL_HANDLER( allocate_locally_unique_id )
476 allocate_luid( &reply->luid );
479 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in)
481 out->Luid = in->luid;
482 out->Attributes =
483 (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
484 (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
487 static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled )
489 struct privilege *privilege = mem_alloc( sizeof(*privilege) );
490 if (privilege)
492 privilege->luid = *luid;
493 privilege->def = privilege->enabled = (enabled != 0);
494 list_add_tail( &token->privileges, &privilege->entry );
496 return privilege;
499 static inline void privilege_remove( struct privilege *privilege )
501 list_remove( &privilege->entry );
502 free( privilege );
505 static void token_destroy( struct object *obj )
507 struct token* token;
508 struct list *cursor, *cursor_next;
510 assert( obj->ops == &token_ops );
511 token = (struct token *)obj;
513 free( token->user );
515 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
517 struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
518 privilege_remove( privilege );
521 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
523 struct group *group = LIST_ENTRY( cursor, struct group, entry );
524 list_remove( &group->entry );
525 free( group );
528 free( token->default_dacl );
531 /* creates a new token.
532 * groups may be NULL if group_count is 0.
533 * privs may be NULL if priv_count is 0.
534 * default_dacl may be NULL, indicating that all objects created by the user
535 * are unsecured.
536 * modified_id may be NULL, indicating that a new modified_id luid should be
537 * allocated.
539 static struct token *create_token( unsigned primary, const SID *user,
540 const SID_AND_ATTRIBUTES *groups, unsigned int group_count,
541 const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count,
542 const ACL *default_dacl, TOKEN_SOURCE source,
543 const luid_t *modified_id,
544 int impersonation_level )
546 struct token *token = alloc_object( &token_ops );
547 if (token)
549 unsigned int i;
551 allocate_luid( &token->token_id );
552 if (modified_id)
553 token->modified_id = *modified_id;
554 else
555 allocate_luid( &token->modified_id );
556 list_init( &token->privileges );
557 list_init( &token->groups );
558 token->primary = primary;
559 /* primary tokens don't have impersonation levels */
560 if (primary)
561 token->impersonation_level = -1;
562 else
563 token->impersonation_level = impersonation_level;
564 token->default_dacl = NULL;
565 token->primary_group = NULL;
567 /* copy user */
568 token->user = memdup( user, security_sid_len( user ));
569 if (!token->user)
571 release_object( token );
572 return NULL;
575 /* copy groups */
576 for (i = 0; i < group_count; i++)
578 size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] );
579 struct group *group = mem_alloc( size );
581 if (!group)
583 release_object( token );
584 return NULL;
586 memcpy( &group->sid, groups[i].Sid, security_sid_len( groups[i].Sid ));
587 group->enabled = TRUE;
588 group->def = TRUE;
589 group->logon = (groups[i].Attributes & SE_GROUP_LOGON_ID) != 0;
590 group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) != 0;
591 group->owner = (groups[i].Attributes & SE_GROUP_OWNER) != 0;
592 group->resource = FALSE;
593 group->deny_only = FALSE;
594 list_add_tail( &token->groups, &group->entry );
595 /* Use first owner capable group as owner and primary group */
596 if (!token->primary_group && group->owner)
598 token->owner = &group->sid;
599 token->primary_group = &group->sid;
603 /* copy privileges */
604 for (i = 0; i < priv_count; i++)
606 /* note: we don't check uniqueness: the caller must make sure
607 * privs doesn't contain any duplicate luids */
608 if (!privilege_add( token, &privs[i].Luid,
609 privs[i].Attributes & SE_PRIVILEGE_ENABLED ))
611 release_object( token );
612 return NULL;
616 if (default_dacl)
618 token->default_dacl = memdup( default_dacl, default_dacl->AclSize );
619 if (!token->default_dacl)
621 release_object( token );
622 return NULL;
626 token->source = source;
628 return token;
631 struct token *token_duplicate( struct token *src_token, unsigned primary,
632 int impersonation_level, const struct security_descriptor *sd )
634 const luid_t *modified_id =
635 primary || (impersonation_level == src_token->impersonation_level) ?
636 &src_token->modified_id : NULL;
637 struct token *token = NULL;
638 struct privilege *privilege;
639 struct group *group;
641 if (!primary &&
642 (impersonation_level < SecurityAnonymous ||
643 impersonation_level > SecurityDelegation ||
644 (!src_token->primary && (impersonation_level > src_token->impersonation_level))))
646 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
647 return NULL;
650 token = create_token( primary, src_token->user, NULL, 0,
651 NULL, 0, src_token->default_dacl,
652 src_token->source, modified_id,
653 impersonation_level );
654 if (!token) return token;
656 /* copy groups */
657 token->primary_group = NULL;
658 LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct group, entry )
660 size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[group->sid.SubAuthorityCount] );
661 struct group *newgroup = mem_alloc( size );
662 if (!newgroup)
664 release_object( token );
665 return NULL;
667 memcpy( newgroup, group, size );
668 list_add_tail( &token->groups, &newgroup->entry );
669 if (src_token->primary_group == &group->sid)
671 token->owner = &newgroup->sid;
672 token->primary_group = &newgroup->sid;
675 assert( token->primary_group );
677 /* copy privileges */
678 LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
679 if (!privilege_add( token, &privilege->luid, privilege->enabled ))
681 release_object( token );
682 return NULL;
685 if (sd) default_set_sd( &token->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
686 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
688 return token;
691 static ACL *create_default_dacl( const SID *user )
693 ACCESS_ALLOWED_ACE *aaa;
694 ACL *default_dacl;
695 SID *sid;
696 size_t default_dacl_size = sizeof(ACL) +
697 2*(sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
698 sizeof(local_system_sid) +
699 security_sid_len( user );
701 default_dacl = mem_alloc( default_dacl_size );
702 if (!default_dacl) return NULL;
704 default_dacl->AclRevision = ACL_REVISION;
705 default_dacl->Sbz1 = 0;
706 default_dacl->AclSize = default_dacl_size;
707 default_dacl->AceCount = 2;
708 default_dacl->Sbz2 = 0;
710 /* GENERIC_ALL for Local System */
711 aaa = (ACCESS_ALLOWED_ACE *)(default_dacl + 1);
712 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
713 aaa->Header.AceFlags = 0;
714 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
715 sizeof(local_system_sid);
716 aaa->Mask = GENERIC_ALL;
717 sid = (SID *)&aaa->SidStart;
718 memcpy( sid, &local_system_sid, sizeof(local_system_sid) );
720 /* GENERIC_ALL for specified user */
721 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
722 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
723 aaa->Header.AceFlags = 0;
724 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) + security_sid_len( user );
725 aaa->Mask = GENERIC_ALL;
726 sid = (SID *)&aaa->SidStart;
727 memcpy( sid, user, security_sid_len( user ));
729 return default_dacl;
732 struct sid_data
734 SID_IDENTIFIER_AUTHORITY idauth;
735 int count;
736 unsigned int subauth[MAX_SUBAUTH_COUNT];
739 static struct security_descriptor *create_security_label_sd( struct token *token, PSID label_sid )
741 size_t sid_len = security_sid_len( label_sid ), sacl_size, sd_size;
742 SYSTEM_MANDATORY_LABEL_ACE *smla;
743 struct security_descriptor *sd;
744 ACL *sacl;
746 sacl_size = sizeof(ACL) + FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart) + sid_len;
747 sd_size = sizeof(struct security_descriptor) + sacl_size;
748 if (!(sd = mem_alloc( sd_size )))
749 return NULL;
751 sd->control = SE_SACL_PRESENT;
752 sd->owner_len = 0;
753 sd->group_len = 0;
754 sd->sacl_len = sacl_size;
755 sd->dacl_len = 0;
757 sacl = (ACL *)(sd + 1);
758 sacl->AclRevision = ACL_REVISION;
759 sacl->Sbz1 = 0;
760 sacl->AclSize = sacl_size;
761 sacl->AceCount = 1;
762 sacl->Sbz2 = 0;
764 smla = (SYSTEM_MANDATORY_LABEL_ACE *)(sacl + 1);
765 smla->Header.AceType = SYSTEM_MANDATORY_LABEL_ACE_TYPE;
766 smla->Header.AceFlags = 0;
767 smla->Header.AceSize = FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart) + sid_len;
768 smla->Mask = SYSTEM_MANDATORY_LABEL_NO_WRITE_UP;
769 memcpy( &smla->SidStart, label_sid, sid_len );
771 assert( sd_is_valid( sd, sd_size ) );
772 return sd;
775 int token_assign_label( struct token *token, PSID label )
777 struct security_descriptor *sd;
778 int ret = 0;
780 if ((sd = create_security_label_sd( token, label )))
782 ret = set_sd_defaults_from_token( &token->obj, sd, LABEL_SECURITY_INFORMATION, token );
783 free( sd );
786 return ret;
789 struct token *token_create_admin( void )
791 struct token *token = NULL;
792 static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
793 static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
794 static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
795 /* on Windows, this value changes every time the user logs on */
796 static const unsigned int logon_subauth[] = { SECURITY_LOGON_IDS_RID, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
797 PSID alias_admins_sid;
798 PSID alias_users_sid;
799 PSID logon_sid;
800 const SID *user_sid = security_unix_uid_to_sid( getuid() );
801 ACL *default_dacl = create_default_dacl( user_sid );
803 alias_admins_sid = security_sid_alloc( &nt_authority, ARRAY_SIZE( alias_admins_subauth ),
804 alias_admins_subauth );
805 alias_users_sid = security_sid_alloc( &nt_authority, ARRAY_SIZE( alias_users_subauth ),
806 alias_users_subauth );
807 logon_sid = security_sid_alloc( &nt_authority, ARRAY_SIZE( logon_subauth ), logon_subauth );
809 if (alias_admins_sid && alias_users_sid && logon_sid && default_dacl)
811 const LUID_AND_ATTRIBUTES admin_privs[] =
813 { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
814 { SeSecurityPrivilege , 0 },
815 { SeBackupPrivilege , 0 },
816 { SeRestorePrivilege , 0 },
817 { SeSystemtimePrivilege , 0 },
818 { SeShutdownPrivilege , 0 },
819 { SeRemoteShutdownPrivilege , 0 },
820 { SeTakeOwnershipPrivilege , 0 },
821 { SeDebugPrivilege , 0 },
822 { SeSystemEnvironmentPrivilege , 0 },
823 { SeSystemProfilePrivilege , 0 },
824 { SeProfileSingleProcessPrivilege, 0 },
825 { SeIncreaseBasePriorityPrivilege, 0 },
826 { SeLoadDriverPrivilege , SE_PRIVILEGE_ENABLED },
827 { SeCreatePagefilePrivilege , 0 },
828 { SeIncreaseQuotaPrivilege , 0 },
829 { SeUndockPrivilege , 0 },
830 { SeManageVolumePrivilege , 0 },
831 { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED },
832 { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED },
834 /* note: we don't include non-builtin groups here for the user -
835 * telling us these is the job of a client-side program */
836 const SID_AND_ATTRIBUTES admin_groups[] =
838 { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
839 { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
840 { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
841 { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
842 { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER },
843 { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
844 { logon_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_LOGON_ID },
846 static const TOKEN_SOURCE admin_source = {"SeMgr", {0, 0}};
847 token = create_token( TRUE, user_sid, admin_groups, ARRAY_SIZE( admin_groups ),
848 admin_privs, ARRAY_SIZE( admin_privs ), default_dacl,
849 admin_source, NULL, -1 );
850 /* we really need a primary group */
851 assert( token->primary_group );
854 free( logon_sid );
855 free( alias_admins_sid );
856 free( alias_users_sid );
857 free( default_dacl );
859 return token;
862 static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only )
864 struct privilege *privilege;
865 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
867 if (is_equal_luid( luid, &privilege->luid ))
869 if (enabled_only && !privilege->enabled)
870 return NULL;
871 return privilege;
874 return NULL;
877 static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs,
878 unsigned int count, LUID_AND_ATTRIBUTES *mod_privs,
879 unsigned int mod_privs_count )
881 unsigned int i, modified_count = 0;
883 /* mark as modified */
884 allocate_luid( &token->modified_id );
886 for (i = 0; i < count; i++)
888 struct privilege *privilege =
889 token_find_privilege( token, &privs[i].Luid, FALSE );
890 if (!privilege)
892 set_error( STATUS_NOT_ALL_ASSIGNED );
893 continue;
896 if (privs[i].Attributes & SE_PRIVILEGE_REMOVED)
897 privilege_remove( privilege );
898 else
900 /* save previous state for caller */
901 if (mod_privs_count)
903 luid_and_attr_from_privilege(mod_privs, privilege);
904 mod_privs++;
905 mod_privs_count--;
906 modified_count++;
909 if (privs[i].Attributes & SE_PRIVILEGE_ENABLED)
910 privilege->enabled = TRUE;
911 else
912 privilege->enabled = FALSE;
915 return modified_count;
918 static void token_disable_privileges( struct token *token )
920 struct privilege *privilege;
922 /* mark as modified */
923 allocate_luid( &token->modified_id );
925 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
926 privilege->enabled = FALSE;
929 int token_check_privileges( struct token *token, int all_required,
930 const LUID_AND_ATTRIBUTES *reqprivs,
931 unsigned int count, LUID_AND_ATTRIBUTES *usedprivs)
933 unsigned int i, enabled_count = 0;
935 for (i = 0; i < count; i++)
937 struct privilege *privilege =
938 token_find_privilege( token, &reqprivs[i].Luid, TRUE );
940 if (usedprivs)
941 usedprivs[i] = reqprivs[i];
943 if (privilege && privilege->enabled)
945 enabled_count++;
946 if (usedprivs)
947 usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS;
951 if (all_required)
952 return (enabled_count == count);
953 else
954 return (enabled_count > 0);
957 int token_sid_present( struct token *token, const SID *sid, int deny )
959 struct group *group;
961 if (security_equal_sid( token->user, sid )) return TRUE;
963 LIST_FOR_EACH_ENTRY( group, &token->groups, struct group, entry )
965 if (!group->enabled) continue;
966 if (group->deny_only && !deny) continue;
968 if (security_equal_sid( &group->sid, sid )) return TRUE;
971 return FALSE;
974 /* Checks access to a security descriptor. 'sd' must have been validated by
975 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
976 * the reason. 'status' parameter will indicate if access is granted or denied.
978 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
980 static unsigned int token_access_check( struct token *token,
981 const struct security_descriptor *sd,
982 unsigned int desired_access,
983 LUID_AND_ATTRIBUTES *privs,
984 unsigned int *priv_count,
985 const GENERIC_MAPPING *mapping,
986 unsigned int *granted_access,
987 unsigned int *status )
989 unsigned int current_access = 0;
990 unsigned int denied_access = 0;
991 ULONG i;
992 const ACL *dacl;
993 int dacl_present;
994 const ACE_HEADER *ace;
995 const SID *owner;
997 /* assume no access rights */
998 *granted_access = 0;
1000 /* fail if desired_access contains generic rights */
1001 if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
1003 if (priv_count) *priv_count = 0;
1004 return STATUS_GENERIC_NOT_MAPPED;
1007 dacl = sd_get_dacl( sd, &dacl_present );
1008 owner = sd_get_owner( sd );
1009 if (!owner || !sd_get_group( sd ))
1011 if (priv_count) *priv_count = 0;
1012 return STATUS_INVALID_SECURITY_DESCR;
1015 /* 1: Grant desired access if the object is unprotected */
1016 if (!dacl_present || !dacl)
1018 if (priv_count) *priv_count = 0;
1019 if (desired_access & MAXIMUM_ALLOWED)
1020 *granted_access = mapping->GenericAll;
1021 else
1022 *granted_access = desired_access;
1023 return *status = STATUS_SUCCESS;
1026 /* 2: Check if caller wants access to system security part. Note: access
1027 * is only granted if specifically asked for */
1028 if (desired_access & ACCESS_SYSTEM_SECURITY)
1030 const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 };
1031 LUID_AND_ATTRIBUTES retpriv = security_priv;
1032 if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
1034 if (priv_count)
1036 /* assumes that there will only be one privilege to return */
1037 if (*priv_count >= 1)
1039 *priv_count = 1;
1040 *privs = retpriv;
1042 else
1044 *priv_count = 1;
1045 return STATUS_BUFFER_TOO_SMALL;
1048 current_access |= ACCESS_SYSTEM_SECURITY;
1049 if (desired_access == current_access)
1051 *granted_access = current_access;
1052 return *status = STATUS_SUCCESS;
1055 else
1057 if (priv_count) *priv_count = 0;
1058 *status = STATUS_PRIVILEGE_NOT_HELD;
1059 return STATUS_SUCCESS;
1062 else if (priv_count) *priv_count = 0;
1064 /* 3: Check whether the token is the owner */
1065 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
1066 * checked when a "set owner" call is made, overriding the access rights
1067 * determined here. */
1068 if (token_sid_present( token, owner, FALSE ))
1070 current_access |= (READ_CONTROL | WRITE_DAC);
1071 if (desired_access == current_access)
1073 *granted_access = current_access;
1074 return *status = STATUS_SUCCESS;
1078 /* 4: Grant rights according to the DACL */
1079 ace = (const ACE_HEADER *)(dacl + 1);
1080 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
1082 const ACCESS_ALLOWED_ACE *aa_ace;
1083 const ACCESS_DENIED_ACE *ad_ace;
1084 const SID *sid;
1086 if (ace->AceFlags & INHERIT_ONLY_ACE)
1087 continue;
1089 switch (ace->AceType)
1091 case ACCESS_DENIED_ACE_TYPE:
1092 ad_ace = (const ACCESS_DENIED_ACE *)ace;
1093 sid = (const SID *)&ad_ace->SidStart;
1094 if (token_sid_present( token, sid, TRUE ))
1096 unsigned int access = ad_ace->Mask;
1097 map_generic_mask(&access, mapping);
1098 if (desired_access & MAXIMUM_ALLOWED)
1099 denied_access |= access;
1100 else
1102 denied_access |= (access & ~current_access);
1103 if (desired_access & access) goto done;
1106 break;
1107 case ACCESS_ALLOWED_ACE_TYPE:
1108 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
1109 sid = (const SID *)&aa_ace->SidStart;
1110 if (token_sid_present( token, sid, FALSE ))
1112 unsigned int access = aa_ace->Mask;
1113 map_generic_mask(&access, mapping);
1114 if (desired_access & MAXIMUM_ALLOWED)
1115 current_access |= access;
1116 else
1117 current_access |= (access & ~denied_access);
1119 break;
1122 /* don't bother carrying on checking if we've already got all of
1123 * rights we need */
1124 if (desired_access == *granted_access)
1125 break;
1128 done:
1129 if (desired_access & MAXIMUM_ALLOWED)
1130 *granted_access = current_access & ~denied_access;
1131 else
1132 if ((current_access & desired_access) == desired_access)
1133 *granted_access = current_access & desired_access;
1134 else
1135 *granted_access = 0;
1137 *status = *granted_access ? STATUS_SUCCESS : STATUS_ACCESS_DENIED;
1138 return STATUS_SUCCESS;
1141 const ACL *token_get_default_dacl( struct token *token )
1143 return token->default_dacl;
1146 const SID *token_get_user( struct token *token )
1148 return token->user;
1151 const SID *token_get_primary_group( struct token *token )
1153 return token->primary_group;
1156 int check_object_access(struct object *obj, unsigned int *access)
1158 GENERIC_MAPPING mapping;
1159 struct token *token = current->token ? current->token : current->process->token;
1160 unsigned int status;
1161 int res;
1163 mapping.GenericAll = obj->ops->map_access( obj, GENERIC_ALL );
1165 if (!obj->sd)
1167 if (*access & MAXIMUM_ALLOWED)
1168 *access = mapping.GenericAll;
1169 return TRUE;
1172 mapping.GenericRead = obj->ops->map_access( obj, GENERIC_READ );
1173 mapping.GenericWrite = obj->ops->map_access( obj, GENERIC_WRITE );
1174 mapping.GenericExecute = obj->ops->map_access( obj, GENERIC_EXECUTE );
1176 res = token_access_check( token, obj->sd, *access, NULL, NULL,
1177 &mapping, access, &status ) == STATUS_SUCCESS &&
1178 status == STATUS_SUCCESS;
1180 if (!res) set_error( STATUS_ACCESS_DENIED );
1181 return res;
1185 /* open a security token */
1186 DECL_HANDLER(open_token)
1188 if (req->flags & OPEN_TOKEN_THREAD)
1190 struct thread *thread = get_thread_from_handle( req->handle, 0 );
1191 if (thread)
1193 if (thread->token)
1195 if (!thread->token->primary && thread->token->impersonation_level <= SecurityAnonymous)
1196 set_error( STATUS_CANT_OPEN_ANONYMOUS );
1197 else
1198 reply->token = alloc_handle( current->process, thread->token,
1199 req->access, req->attributes );
1201 else
1202 set_error( STATUS_NO_TOKEN );
1203 release_object( thread );
1206 else
1208 struct process *process = get_process_from_handle( req->handle, 0 );
1209 if (process)
1211 if (process->token)
1212 reply->token = alloc_handle( current->process, process->token, req->access,
1213 req->attributes );
1214 else
1215 set_error( STATUS_NO_TOKEN );
1216 release_object( process );
1221 /* adjust the privileges held by a token */
1222 DECL_HANDLER(adjust_token_privileges)
1224 struct token *token;
1225 unsigned int access = TOKEN_ADJUST_PRIVILEGES;
1227 if (req->get_modified_state) access |= TOKEN_QUERY;
1229 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1230 access, &token_ops )))
1232 const LUID_AND_ATTRIBUTES *privs = get_req_data();
1233 LUID_AND_ATTRIBUTES *modified_privs = NULL;
1234 unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
1235 unsigned int modified_priv_count = 0;
1237 if (req->get_modified_state && !req->disable_all)
1239 unsigned int i;
1240 /* count modified privs */
1241 for (i = 0; i < priv_count; i++)
1243 struct privilege *privilege =
1244 token_find_privilege( token, &privs[i].Luid, FALSE );
1245 if (privilege && req->get_modified_state)
1246 modified_priv_count++;
1248 reply->len = modified_priv_count;
1249 modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
1250 if (modified_priv_count)
1251 modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
1253 reply->len = modified_priv_count * sizeof(*modified_privs);
1255 if (req->disable_all)
1256 token_disable_privileges( token );
1257 else
1258 token_adjust_privileges( token, privs, priv_count, modified_privs, modified_priv_count );
1260 release_object( token );
1264 /* retrieves the list of privileges that may be held be the token */
1265 DECL_HANDLER(get_token_privileges)
1267 struct token *token;
1269 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1270 TOKEN_QUERY,
1271 &token_ops )))
1273 int priv_count = 0;
1274 LUID_AND_ATTRIBUTES *privs;
1275 struct privilege *privilege;
1277 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
1278 priv_count++;
1280 reply->len = priv_count * sizeof(*privs);
1281 if (reply->len <= get_reply_max_size())
1283 privs = set_reply_data_size( priv_count * sizeof(*privs) );
1284 if (privs)
1286 int i = 0;
1287 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
1289 luid_and_attr_from_privilege( &privs[i], privilege );
1290 i++;
1294 else
1295 set_error(STATUS_BUFFER_TOO_SMALL);
1297 release_object( token );
1301 /* creates a duplicate of the token */
1302 DECL_HANDLER(duplicate_token)
1304 struct token *src_token;
1305 struct unicode_str name;
1306 const struct security_descriptor *sd;
1307 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1309 if (!objattr) return;
1311 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
1312 TOKEN_DUPLICATE,
1313 &token_ops )))
1315 struct token *token = token_duplicate( src_token, req->primary, req->impersonation_level, sd );
1316 if (token)
1318 reply->new_handle = alloc_handle_no_access_check( current->process, token, req->access, objattr->attributes );
1319 release_object( token );
1321 release_object( src_token );
1325 /* checks the specified privileges are held by the token */
1326 DECL_HANDLER(check_token_privileges)
1328 struct token *token;
1330 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1331 TOKEN_QUERY,
1332 &token_ops )))
1334 unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
1336 if (!token->primary && token->impersonation_level <= SecurityAnonymous)
1337 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
1338 else if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES))
1340 LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
1341 reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
1343 else
1344 set_error( STATUS_BUFFER_OVERFLOW );
1345 release_object( token );
1349 /* checks that a user represented by a token is allowed to access an object
1350 * represented by a security descriptor */
1351 DECL_HANDLER(access_check)
1353 data_size_t sd_size = get_req_data_size();
1354 const struct security_descriptor *sd = get_req_data();
1355 struct token *token;
1357 if (!sd_is_valid( sd, sd_size ))
1359 set_error( STATUS_ACCESS_VIOLATION );
1360 return;
1363 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1364 TOKEN_QUERY,
1365 &token_ops )))
1367 GENERIC_MAPPING mapping;
1368 unsigned int status;
1369 LUID_AND_ATTRIBUTES priv;
1370 unsigned int priv_count = 1;
1372 memset(&priv, 0, sizeof(priv));
1374 /* only impersonation tokens may be used with this function */
1375 if (token->primary)
1377 set_error( STATUS_NO_IMPERSONATION_TOKEN );
1378 release_object( token );
1379 return;
1381 /* anonymous impersonation tokens can't be used */
1382 if (token->impersonation_level <= SecurityAnonymous)
1384 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
1385 release_object( token );
1386 return;
1389 mapping.GenericRead = req->mapping_read;
1390 mapping.GenericWrite = req->mapping_write;
1391 mapping.GenericExecute = req->mapping_execute;
1392 mapping.GenericAll = req->mapping_all;
1394 status = token_access_check(
1395 token, sd, req->desired_access, &priv, &priv_count, &mapping,
1396 &reply->access_granted, &reply->access_status );
1398 reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES);
1400 if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
1402 LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) );
1403 memcpy( privs, &priv, sizeof(priv) );
1406 set_error( status );
1407 release_object( token );
1411 /* retrieves an SID from the token */
1412 DECL_HANDLER(get_token_sid)
1414 struct token *token;
1416 reply->sid_len = 0;
1418 if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
1420 const SID *sid = NULL;
1422 switch (req->which_sid)
1424 case TokenUser:
1425 assert(token->user);
1426 sid = token->user;
1427 break;
1428 case TokenPrimaryGroup:
1429 sid = token->primary_group;
1430 break;
1431 case TokenOwner:
1432 sid = token->owner;
1433 break;
1434 case TokenLogonSid:
1435 sid = (const SID *)&builtin_logon_sid;
1436 break;
1437 default:
1438 set_error( STATUS_INVALID_PARAMETER );
1439 break;
1442 if (sid)
1444 reply->sid_len = security_sid_len( sid );
1445 if (reply->sid_len <= get_reply_max_size()) set_reply_data( sid, reply->sid_len );
1446 else set_error( STATUS_BUFFER_TOO_SMALL );
1448 release_object( token );
1452 /* retrieves the groups that the user represented by the token belongs to */
1453 DECL_HANDLER(get_token_groups)
1455 struct token *token;
1457 reply->user_len = 0;
1459 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1460 TOKEN_QUERY,
1461 &token_ops )))
1463 size_t size_needed = sizeof(struct token_groups);
1464 size_t sid_size = 0;
1465 unsigned int group_count = 0;
1466 const struct group *group;
1468 LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
1470 group_count++;
1471 sid_size += security_sid_len( &group->sid );
1473 size_needed += sid_size;
1474 /* attributes size */
1475 size_needed += sizeof(unsigned int) * group_count;
1477 /* reply buffer contains size_needed bytes formatted as:
1479 unsigned int count;
1480 unsigned int attrib[count];
1481 char sid_data[];
1483 user_len includes extra data needed for TOKEN_GROUPS representation,
1484 required caller buffer size calculated here to avoid extra server call */
1485 reply->user_len = FIELD_OFFSET( TOKEN_GROUPS, Groups[group_count] ) + sid_size;
1487 if (reply->user_len <= get_reply_max_size())
1489 struct token_groups *tg = set_reply_data_size( size_needed );
1490 if (tg)
1492 unsigned int *attr_ptr = (unsigned int *)(tg + 1);
1493 SID *sid_ptr = (SID *)(attr_ptr + group_count);
1495 tg->count = group_count;
1497 LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
1500 *attr_ptr = 0;
1501 if (group->mandatory) *attr_ptr |= SE_GROUP_MANDATORY;
1502 if (group->def) *attr_ptr |= SE_GROUP_ENABLED_BY_DEFAULT;
1503 if (group->enabled) *attr_ptr |= SE_GROUP_ENABLED;
1504 if (group->owner) *attr_ptr |= SE_GROUP_OWNER;
1505 if (group->deny_only) *attr_ptr |= SE_GROUP_USE_FOR_DENY_ONLY;
1506 if (group->resource) *attr_ptr |= SE_GROUP_RESOURCE;
1507 if (group->logon) *attr_ptr |= SE_GROUP_LOGON_ID;
1509 memcpy(sid_ptr, &group->sid, security_sid_len( &group->sid ));
1511 sid_ptr = (SID *)((char *)sid_ptr + security_sid_len( &group->sid ));
1512 attr_ptr++;
1516 else set_error( STATUS_BUFFER_TOO_SMALL );
1518 release_object( token );
1522 DECL_HANDLER(get_token_impersonation_level)
1524 struct token *token;
1526 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1527 TOKEN_QUERY,
1528 &token_ops )))
1530 if (token->primary)
1531 set_error( STATUS_INVALID_PARAMETER );
1532 else
1533 reply->impersonation_level = token->impersonation_level;
1535 release_object( token );
1539 DECL_HANDLER(get_token_statistics)
1541 struct token *token;
1543 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1544 TOKEN_QUERY,
1545 &token_ops )))
1547 reply->token_id = token->token_id;
1548 reply->modified_id = token->modified_id;
1549 reply->primary = token->primary;
1550 reply->impersonation_level = token->impersonation_level;
1551 reply->group_count = list_count( &token->groups );
1552 reply->privilege_count = list_count( &token->privileges );
1554 release_object( token );
1558 DECL_HANDLER(get_token_default_dacl)
1560 struct token *token;
1562 reply->acl_len = 0;
1564 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1565 TOKEN_QUERY,
1566 &token_ops )))
1568 if (token->default_dacl)
1569 reply->acl_len = token->default_dacl->AclSize;
1571 if (reply->acl_len <= get_reply_max_size())
1573 ACL *acl_reply = set_reply_data_size( reply->acl_len );
1574 if (acl_reply)
1575 memcpy( acl_reply, token->default_dacl, reply->acl_len );
1577 else set_error( STATUS_BUFFER_TOO_SMALL );
1579 release_object( token );
1583 DECL_HANDLER(set_token_default_dacl)
1585 struct token *token;
1587 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1588 TOKEN_ADJUST_DEFAULT,
1589 &token_ops )))
1591 const ACL *acl = get_req_data();
1592 unsigned int acl_size = get_req_data_size();
1594 free( token->default_dacl );
1595 token->default_dacl = NULL;
1597 if (acl_size)
1598 token->default_dacl = memdup( acl, acl_size );
1600 release_object( token );