mshtml: Added IDOMEvent::get_timeStamp implementation.
[wine.git] / server / token.c
blob532d7b74059f7298b463538fcaec6e26e1676c44
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 static const SID world_sid = { SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY }, { SECURITY_WORLD_RID } };
68 static const SID local_sid = { SID_REVISION, 1, { SECURITY_LOCAL_SID_AUTHORITY }, { SECURITY_LOCAL_RID } };
69 static const SID interactive_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_INTERACTIVE_RID } };
70 static const SID anonymous_logon_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_ANONYMOUS_LOGON_RID } };
71 static const SID authenticated_user_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_AUTHENTICATED_USER_RID } };
72 static const SID local_system_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SYSTEM_RID } };
73 static const SID high_label_sid = { SID_REVISION, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY }, { SECURITY_MANDATORY_HIGH_RID } };
74 static const struct /* same fields as struct SID */
76 BYTE Revision;
77 BYTE SubAuthorityCount;
78 SID_IDENTIFIER_AUTHORITY IdentifierAuthority;
79 DWORD SubAuthority[5];
80 } local_user_sid = { SID_REVISION, 5, { SECURITY_NT_AUTHORITY }, { SECURITY_NT_NON_UNIQUE, 0, 0, 0, 1000 } };
81 static const struct /* same fields as struct SID */
83 BYTE Revision;
84 BYTE SubAuthorityCount;
85 SID_IDENTIFIER_AUTHORITY IdentifierAuthority;
86 DWORD SubAuthority[2];
87 } builtin_admins_sid = { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS } };
88 static const struct /* same fields as struct SID */
90 BYTE Revision;
91 BYTE SubAuthorityCount;
92 SID_IDENTIFIER_AUTHORITY IdentifierAuthority;
93 DWORD SubAuthority[2];
94 } builtin_users_sid = { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS } };
96 const PSID security_world_sid = (PSID)&world_sid;
97 static const PSID security_local_sid = (PSID)&local_sid;
98 static const PSID security_interactive_sid = (PSID)&interactive_sid;
99 static const PSID security_authenticated_user_sid = (PSID)&authenticated_user_sid;
100 const PSID security_local_system_sid = (PSID)&local_system_sid;
101 const PSID security_local_user_sid = (PSID)&local_user_sid;
102 const PSID security_builtin_admins_sid = (PSID)&builtin_admins_sid;
103 const PSID security_builtin_users_sid = (PSID)&builtin_users_sid;
104 const PSID security_high_label_sid = (PSID)&high_label_sid;
106 static luid_t prev_luid_value = { 1000, 0 };
108 struct token
110 struct object obj; /* object header */
111 luid_t token_id; /* system-unique id of token */
112 luid_t modified_id; /* new id allocated every time token is modified */
113 struct list privileges; /* privileges available to the token */
114 struct list groups; /* groups that the user of this token belongs to (sid_and_attributes) */
115 SID *user; /* SID of user this token represents */
116 SID *primary_group; /* SID of user's primary group */
117 unsigned primary; /* is this a primary or impersonation token? */
118 ACL *default_dacl; /* the default DACL to assign to objects created by this user */
119 TOKEN_SOURCE source; /* source of the token */
120 int impersonation_level; /* impersonation level this token is capable of if non-primary token */
123 struct privilege
125 struct list entry;
126 LUID luid;
127 unsigned enabled : 1; /* is the privilege currently enabled? */
128 unsigned def : 1; /* is the privilege enabled by default? */
131 struct group
133 struct list entry;
134 unsigned enabled : 1; /* is the sid currently enabled? */
135 unsigned def : 1; /* is the sid enabled by default? */
136 unsigned logon : 1; /* is this a logon sid? */
137 unsigned mandatory: 1; /* is this sid always enabled? */
138 unsigned owner : 1; /* can this sid be an owner of an object? */
139 unsigned resource : 1; /* is this a domain-local group? */
140 unsigned deny_only: 1; /* is this a sid that should be use for denying only? */
141 SID sid;
144 static void token_dump( struct object *obj, int verbose );
145 static unsigned int token_map_access( struct object *obj, unsigned int access );
146 static void token_destroy( struct object *obj );
148 static const struct object_ops token_ops =
150 sizeof(struct token), /* size */
151 token_dump, /* dump */
152 no_get_type, /* get_type */
153 no_add_queue, /* add_queue */
154 NULL, /* remove_queue */
155 NULL, /* signaled */
156 NULL, /* satisfied */
157 no_signal, /* signal */
158 no_get_fd, /* get_fd */
159 token_map_access, /* map_access */
160 default_get_sd, /* get_sd */
161 default_set_sd, /* set_sd */
162 no_lookup_name, /* lookup_name */
163 no_link_name, /* link_name */
164 NULL, /* unlink_name */
165 no_open_file, /* open_file */
166 no_close_handle, /* close_handle */
167 token_destroy /* destroy */
171 static void token_dump( struct object *obj, int verbose )
173 fprintf( stderr, "Security token\n" );
174 /* FIXME: dump token members */
177 static unsigned int token_map_access( struct object *obj, unsigned int access )
179 if (access & GENERIC_READ) access |= TOKEN_READ;
180 if (access & GENERIC_WRITE) access |= TOKEN_WRITE;
181 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
182 if (access & GENERIC_ALL) access |= TOKEN_ALL_ACCESS;
183 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
186 static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] )
188 int i;
189 SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) );
190 if (!sid) return NULL;
191 sid->Revision = SID_REVISION;
192 sid->SubAuthorityCount = subauthcount;
193 sid->IdentifierAuthority = *idauthority;
194 for (i = 0; i < subauthcount; i++)
195 sid->SubAuthority[i] = subauth[i];
196 return sid;
199 void security_set_thread_token( struct thread *thread, obj_handle_t handle )
201 if (!handle)
203 if (thread->token)
204 release_object( thread->token );
205 thread->token = NULL;
207 else
209 struct token *token = (struct token *)get_handle_obj( current->process,
210 handle,
211 TOKEN_IMPERSONATE,
212 &token_ops );
213 if (token)
215 if (thread->token)
216 release_object( thread->token );
217 thread->token = token;
222 const SID *security_unix_uid_to_sid( uid_t uid )
224 /* very simple mapping: either the current user or not the current user */
225 if (uid == getuid())
226 return (const SID *)&local_user_sid;
227 else
228 return &anonymous_logon_sid;
231 static int acl_is_valid( const ACL *acl, data_size_t size )
233 ULONG i;
234 const ACE_HEADER *ace;
236 if (size < sizeof(ACL))
237 return FALSE;
239 size = min(size, MAX_ACL_LEN);
241 size -= sizeof(ACL);
243 ace = (const ACE_HEADER *)(acl + 1);
244 for (i = 0; i < acl->AceCount; i++)
246 const SID *sid;
247 data_size_t sid_size;
249 if (size < sizeof(ACE_HEADER))
250 return FALSE;
251 if (size < ace->AceSize)
252 return FALSE;
253 size -= ace->AceSize;
254 switch (ace->AceType)
256 case ACCESS_DENIED_ACE_TYPE:
257 sid = (const SID *)&((const ACCESS_DENIED_ACE *)ace)->SidStart;
258 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart);
259 break;
260 case ACCESS_ALLOWED_ACE_TYPE:
261 sid = (const SID *)&((const ACCESS_ALLOWED_ACE *)ace)->SidStart;
262 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart);
263 break;
264 case SYSTEM_AUDIT_ACE_TYPE:
265 sid = (const SID *)&((const SYSTEM_AUDIT_ACE *)ace)->SidStart;
266 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart);
267 break;
268 case SYSTEM_ALARM_ACE_TYPE:
269 sid = (const SID *)&((const SYSTEM_ALARM_ACE *)ace)->SidStart;
270 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_ALARM_ACE, SidStart);
271 break;
272 case SYSTEM_MANDATORY_LABEL_ACE_TYPE:
273 sid = (const SID *)&((const SYSTEM_MANDATORY_LABEL_ACE *)ace)->SidStart;
274 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart);
275 break;
276 default:
277 return FALSE;
279 if (sid_size < FIELD_OFFSET(SID, SubAuthority[0]) || sid_size < security_sid_len( sid ))
280 return FALSE;
281 ace = ace_next( ace );
283 return TRUE;
286 /* checks whether all members of a security descriptor fit inside the size
287 * of memory specified */
288 int sd_is_valid( const struct security_descriptor *sd, data_size_t size )
290 size_t offset = sizeof(struct security_descriptor);
291 const SID *group;
292 const SID *owner;
293 const ACL *sacl;
294 const ACL *dacl;
295 int dummy;
297 if (size < offset)
298 return FALSE;
300 if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
301 (offset + sd->owner_len > size))
302 return FALSE;
303 owner = sd_get_owner( sd );
304 if (owner)
306 size_t needed_size = security_sid_len( owner );
307 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
308 return FALSE;
310 offset += sd->owner_len;
312 if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
313 (offset + sd->group_len > size))
314 return FALSE;
315 group = sd_get_group( sd );
316 if (group)
318 size_t needed_size = security_sid_len( group );
319 if ((sd->group_len < sizeof(SID)) || (needed_size > sd->group_len))
320 return FALSE;
322 offset += sd->group_len;
324 if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
325 return FALSE;
326 sacl = sd_get_sacl( sd, &dummy );
327 if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
328 return FALSE;
329 offset += sd->sacl_len;
331 if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
332 return FALSE;
333 dacl = sd_get_dacl( sd, &dummy );
334 if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
335 return FALSE;
336 offset += sd->dacl_len;
338 return TRUE;
341 /* extract security labels from SACL */
342 ACL *extract_security_labels( const ACL *sacl )
344 size_t size = sizeof(ACL);
345 const ACE_HEADER *ace;
346 ACE_HEADER *label_ace;
347 unsigned int i, count = 0;
348 ACL *label_acl;
350 ace = (const ACE_HEADER *)(sacl + 1);
351 for (i = 0; i < sacl->AceCount; i++, ace = ace_next( ace ))
353 if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
355 size += ace->AceSize;
356 count++;
360 label_acl = mem_alloc( size );
361 if (!label_acl) return NULL;
363 label_acl->AclRevision = sacl->AclRevision;
364 label_acl->Sbz1 = 0;
365 label_acl->AclSize = size;
366 label_acl->AceCount = count;
367 label_acl->Sbz2 = 0;
368 label_ace = (ACE_HEADER *)(label_acl + 1);
370 ace = (const ACE_HEADER *)(sacl + 1);
371 for (i = 0; i < sacl->AceCount; i++, ace = ace_next( ace ))
373 if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
375 memcpy( label_ace, ace, ace->AceSize );
376 label_ace = (ACE_HEADER *)ace_next( label_ace );
379 return label_acl;
382 /* replace security labels in an existing SACL */
383 ACL *replace_security_labels( const ACL *old_sacl, const ACL *new_sacl )
385 const ACE_HEADER *ace;
386 ACE_HEADER *replaced_ace;
387 size_t size = sizeof(ACL);
388 unsigned int i, count = 0;
389 BYTE revision = ACL_REVISION;
390 ACL *replaced_acl;
392 if (old_sacl)
394 revision = max( revision, old_sacl->AclRevision );
395 ace = (const ACE_HEADER *)(old_sacl + 1);
396 for (i = 0; i < old_sacl->AceCount; i++, ace = ace_next( ace ))
398 if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
399 size += ace->AceSize;
400 count++;
404 if (new_sacl)
406 revision = max( revision, new_sacl->AclRevision );
407 ace = (const ACE_HEADER *)(new_sacl + 1);
408 for (i = 0; i < new_sacl->AceCount; i++, ace = ace_next( ace ))
410 if (ace->AceType != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
411 size += ace->AceSize;
412 count++;
416 replaced_acl = mem_alloc( size );
417 if (!replaced_acl) return NULL;
419 replaced_acl->AclRevision = revision;
420 replaced_acl->Sbz1 = 0;
421 replaced_acl->AclSize = size;
422 replaced_acl->AceCount = count;
423 replaced_acl->Sbz2 = 0;
424 replaced_ace = (ACE_HEADER *)(replaced_acl + 1);
426 if (old_sacl)
428 ace = (const ACE_HEADER *)(old_sacl + 1);
429 for (i = 0; i < old_sacl->AceCount; i++, ace = ace_next( ace ))
431 if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
432 memcpy( replaced_ace, ace, ace->AceSize );
433 replaced_ace = (ACE_HEADER *)ace_next( replaced_ace );
437 if (new_sacl)
439 ace = (const ACE_HEADER *)(new_sacl + 1);
440 for (i = 0; i < new_sacl->AceCount; i++, ace = ace_next( ace ))
442 if (ace->AceType != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
443 memcpy( replaced_ace, ace, ace->AceSize );
444 replaced_ace = (ACE_HEADER *)ace_next( replaced_ace );
448 return replaced_acl;
451 /* maps from generic rights to specific rights as given by a mapping */
452 static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping)
454 if (*mask & GENERIC_READ) *mask |= mapping->GenericRead;
455 if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite;
456 if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute;
457 if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll;
458 *mask &= 0x0FFFFFFF;
461 static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 )
463 return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart);
466 static inline void allocate_luid( luid_t *luid )
468 prev_luid_value.low_part++;
469 *luid = prev_luid_value;
472 DECL_HANDLER( allocate_locally_unique_id )
474 allocate_luid( &reply->luid );
477 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in)
479 out->Luid = in->luid;
480 out->Attributes =
481 (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
482 (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
485 static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled )
487 struct privilege *privilege = mem_alloc( sizeof(*privilege) );
488 if (privilege)
490 privilege->luid = *luid;
491 privilege->def = privilege->enabled = (enabled != 0);
492 list_add_tail( &token->privileges, &privilege->entry );
494 return privilege;
497 static inline void privilege_remove( struct privilege *privilege )
499 list_remove( &privilege->entry );
500 free( privilege );
503 static void token_destroy( struct object *obj )
505 struct token* token;
506 struct list *cursor, *cursor_next;
508 assert( obj->ops == &token_ops );
509 token = (struct token *)obj;
511 free( token->user );
513 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
515 struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
516 privilege_remove( privilege );
519 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
521 struct group *group = LIST_ENTRY( cursor, struct group, entry );
522 list_remove( &group->entry );
523 free( group );
526 free( token->default_dacl );
529 /* creates a new token.
530 * groups may be NULL if group_count is 0.
531 * privs may be NULL if priv_count is 0.
532 * default_dacl may be NULL, indicating that all objects created by the user
533 * are unsecured.
534 * modified_id may be NULL, indicating that a new modified_id luid should be
535 * allocated.
537 static struct token *create_token( unsigned primary, const SID *user,
538 const SID_AND_ATTRIBUTES *groups, unsigned int group_count,
539 const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count,
540 const ACL *default_dacl, TOKEN_SOURCE source,
541 const luid_t *modified_id,
542 int impersonation_level )
544 struct token *token = alloc_object( &token_ops );
545 if (token)
547 unsigned int i;
549 allocate_luid( &token->token_id );
550 if (modified_id)
551 token->modified_id = *modified_id;
552 else
553 allocate_luid( &token->modified_id );
554 list_init( &token->privileges );
555 list_init( &token->groups );
556 token->primary = primary;
557 /* primary tokens don't have impersonation levels */
558 if (primary)
559 token->impersonation_level = -1;
560 else
561 token->impersonation_level = impersonation_level;
562 token->default_dacl = NULL;
563 token->primary_group = NULL;
565 /* copy user */
566 token->user = memdup( user, security_sid_len( user ));
567 if (!token->user)
569 release_object( token );
570 return NULL;
573 /* copy groups */
574 for (i = 0; i < group_count; i++)
576 size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] );
577 struct group *group = mem_alloc( size );
579 if (!group)
581 release_object( token );
582 return NULL;
584 memcpy( &group->sid, groups[i].Sid, security_sid_len( groups[i].Sid ));
585 group->enabled = TRUE;
586 group->def = TRUE;
587 group->logon = (groups[i].Attributes & SE_GROUP_LOGON_ID) != 0;
588 group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) != 0;
589 group->owner = (groups[i].Attributes & SE_GROUP_OWNER) != 0;
590 group->resource = FALSE;
591 group->deny_only = FALSE;
592 list_add_tail( &token->groups, &group->entry );
593 /* Use first owner capable group as an owner */
594 if (!token->primary_group && group->owner)
595 token->primary_group = &group->sid;
598 /* copy privileges */
599 for (i = 0; i < priv_count; i++)
601 /* note: we don't check uniqueness: the caller must make sure
602 * privs doesn't contain any duplicate luids */
603 if (!privilege_add( token, &privs[i].Luid,
604 privs[i].Attributes & SE_PRIVILEGE_ENABLED ))
606 release_object( token );
607 return NULL;
611 if (default_dacl)
613 token->default_dacl = memdup( default_dacl, default_dacl->AclSize );
614 if (!token->default_dacl)
616 release_object( token );
617 return NULL;
621 token->source = source;
623 return token;
626 struct token *token_duplicate( struct token *src_token, unsigned primary,
627 int impersonation_level, const struct security_descriptor *sd )
629 const luid_t *modified_id =
630 primary || (impersonation_level == src_token->impersonation_level) ?
631 &src_token->modified_id : NULL;
632 struct token *token = NULL;
633 struct privilege *privilege;
634 struct group *group;
636 if (!primary &&
637 (impersonation_level < SecurityAnonymous ||
638 impersonation_level > SecurityDelegation ||
639 (!src_token->primary && (impersonation_level > src_token->impersonation_level))))
641 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
642 return NULL;
645 token = create_token( primary, src_token->user, NULL, 0,
646 NULL, 0, src_token->default_dacl,
647 src_token->source, modified_id,
648 impersonation_level );
649 if (!token) return token;
651 /* copy groups */
652 token->primary_group = NULL;
653 LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct group, entry )
655 size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[group->sid.SubAuthorityCount] );
656 struct group *newgroup = mem_alloc( size );
657 if (!newgroup)
659 release_object( token );
660 return NULL;
662 memcpy( newgroup, group, size );
663 list_add_tail( &token->groups, &newgroup->entry );
664 if (src_token->primary_group == &group->sid)
665 token->primary_group = &newgroup->sid;
667 assert( token->primary_group );
669 /* copy privileges */
670 LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
671 if (!privilege_add( token, &privilege->luid, privilege->enabled ))
673 release_object( token );
674 return NULL;
677 if (sd) default_set_sd( &token->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
678 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
680 return token;
683 static ACL *create_default_dacl( const SID *user )
685 ACCESS_ALLOWED_ACE *aaa;
686 ACL *default_dacl;
687 SID *sid;
688 size_t default_dacl_size = sizeof(ACL) +
689 2*(sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
690 sizeof(local_system_sid) +
691 security_sid_len( user );
693 default_dacl = mem_alloc( default_dacl_size );
694 if (!default_dacl) return NULL;
696 default_dacl->AclRevision = ACL_REVISION;
697 default_dacl->Sbz1 = 0;
698 default_dacl->AclSize = default_dacl_size;
699 default_dacl->AceCount = 2;
700 default_dacl->Sbz2 = 0;
702 /* GENERIC_ALL for Local System */
703 aaa = (ACCESS_ALLOWED_ACE *)(default_dacl + 1);
704 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
705 aaa->Header.AceFlags = 0;
706 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
707 sizeof(local_system_sid);
708 aaa->Mask = GENERIC_ALL;
709 sid = (SID *)&aaa->SidStart;
710 memcpy( sid, &local_system_sid, sizeof(local_system_sid) );
712 /* GENERIC_ALL for specified user */
713 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
714 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
715 aaa->Header.AceFlags = 0;
716 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) + security_sid_len( user );
717 aaa->Mask = GENERIC_ALL;
718 sid = (SID *)&aaa->SidStart;
719 memcpy( sid, user, security_sid_len( user ));
721 return default_dacl;
724 struct sid_data
726 SID_IDENTIFIER_AUTHORITY idauth;
727 int count;
728 unsigned int subauth[MAX_SUBAUTH_COUNT];
731 static struct security_descriptor *create_security_label_sd( struct token *token, PSID label_sid )
733 size_t sid_len = security_sid_len( label_sid ), sacl_size, sd_size;
734 SYSTEM_MANDATORY_LABEL_ACE *smla;
735 struct security_descriptor *sd;
736 ACL *sacl;
738 sacl_size = sizeof(ACL) + FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart) + sid_len;
739 sd_size = sizeof(struct security_descriptor) + sacl_size;
740 if (!(sd = mem_alloc( sd_size )))
741 return NULL;
743 sd->control = SE_SACL_PRESENT;
744 sd->owner_len = 0;
745 sd->group_len = 0;
746 sd->sacl_len = sacl_size;
747 sd->dacl_len = 0;
749 sacl = (ACL *)(sd + 1);
750 sacl->AclRevision = ACL_REVISION;
751 sacl->Sbz1 = 0;
752 sacl->AclSize = sacl_size;
753 sacl->AceCount = 1;
754 sacl->Sbz2 = 0;
756 smla = (SYSTEM_MANDATORY_LABEL_ACE *)(sacl + 1);
757 smla->Header.AceType = SYSTEM_MANDATORY_LABEL_ACE_TYPE;
758 smla->Header.AceFlags = 0;
759 smla->Header.AceSize = FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart) + sid_len;
760 smla->Mask = SYSTEM_MANDATORY_LABEL_NO_WRITE_UP;
761 memcpy( &smla->SidStart, label_sid, sid_len );
763 assert( sd_is_valid( sd, sd_size ) );
764 return sd;
767 int token_assign_label( struct token *token, PSID label )
769 struct security_descriptor *sd;
770 int ret = 0;
772 if ((sd = create_security_label_sd( token, label )))
774 ret = set_sd_defaults_from_token( &token->obj, sd, LABEL_SECURITY_INFORMATION, token );
775 free( sd );
778 return ret;
781 struct token *token_create_admin( void )
783 struct token *token = NULL;
784 static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
785 static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
786 static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
787 /* on Windows, this value changes every time the user logs on */
788 static const unsigned int logon_subauth[] = { SECURITY_LOGON_IDS_RID, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
789 PSID alias_admins_sid;
790 PSID alias_users_sid;
791 PSID logon_sid;
792 const SID *user_sid = security_unix_uid_to_sid( getuid() );
793 ACL *default_dacl = create_default_dacl( user_sid );
795 alias_admins_sid = security_sid_alloc( &nt_authority, sizeof(alias_admins_subauth)/sizeof(alias_admins_subauth[0]),
796 alias_admins_subauth );
797 alias_users_sid = security_sid_alloc( &nt_authority, sizeof(alias_users_subauth)/sizeof(alias_users_subauth[0]),
798 alias_users_subauth );
799 logon_sid = security_sid_alloc( &nt_authority, sizeof(logon_subauth)/sizeof(logon_subauth[0]),
800 logon_subauth );
802 if (alias_admins_sid && alias_users_sid && logon_sid && default_dacl)
804 const LUID_AND_ATTRIBUTES admin_privs[] =
806 { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
807 { SeSecurityPrivilege , 0 },
808 { SeBackupPrivilege , 0 },
809 { SeRestorePrivilege , 0 },
810 { SeSystemtimePrivilege , 0 },
811 { SeShutdownPrivilege , 0 },
812 { SeRemoteShutdownPrivilege , 0 },
813 { SeTakeOwnershipPrivilege , 0 },
814 { SeDebugPrivilege , 0 },
815 { SeSystemEnvironmentPrivilege , 0 },
816 { SeSystemProfilePrivilege , 0 },
817 { SeProfileSingleProcessPrivilege, 0 },
818 { SeIncreaseBasePriorityPrivilege, 0 },
819 { SeLoadDriverPrivilege , SE_PRIVILEGE_ENABLED },
820 { SeCreatePagefilePrivilege , 0 },
821 { SeIncreaseQuotaPrivilege , 0 },
822 { SeUndockPrivilege , 0 },
823 { SeManageVolumePrivilege , 0 },
824 { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED },
825 { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED },
827 /* note: we don't include non-builtin groups here for the user -
828 * telling us these is the job of a client-side program */
829 const SID_AND_ATTRIBUTES admin_groups[] =
831 { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
832 { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
833 { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
834 { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
835 { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER },
836 { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
837 { logon_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_LOGON_ID },
839 static const TOKEN_SOURCE admin_source = {"SeMgr", {0, 0}};
840 token = create_token( TRUE, user_sid, admin_groups, sizeof(admin_groups)/sizeof(admin_groups[0]),
841 admin_privs, sizeof(admin_privs)/sizeof(admin_privs[0]), default_dacl,
842 admin_source, NULL, -1 );
843 /* we really need a primary group */
844 assert( token->primary_group );
847 free( logon_sid );
848 free( alias_admins_sid );
849 free( alias_users_sid );
850 free( default_dacl );
852 return token;
855 static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only )
857 struct privilege *privilege;
858 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
860 if (is_equal_luid( luid, &privilege->luid ))
862 if (enabled_only && !privilege->enabled)
863 return NULL;
864 return privilege;
867 return NULL;
870 static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs,
871 unsigned int count, LUID_AND_ATTRIBUTES *mod_privs,
872 unsigned int mod_privs_count )
874 unsigned int i, modified_count = 0;
876 /* mark as modified */
877 allocate_luid( &token->modified_id );
879 for (i = 0; i < count; i++)
881 struct privilege *privilege =
882 token_find_privilege( token, &privs[i].Luid, FALSE );
883 if (!privilege)
885 set_error( STATUS_NOT_ALL_ASSIGNED );
886 continue;
889 if (privs[i].Attributes & SE_PRIVILEGE_REMOVED)
890 privilege_remove( privilege );
891 else
893 /* save previous state for caller */
894 if (mod_privs_count)
896 luid_and_attr_from_privilege(mod_privs, privilege);
897 mod_privs++;
898 mod_privs_count--;
899 modified_count++;
902 if (privs[i].Attributes & SE_PRIVILEGE_ENABLED)
903 privilege->enabled = TRUE;
904 else
905 privilege->enabled = FALSE;
908 return modified_count;
911 static void token_disable_privileges( struct token *token )
913 struct privilege *privilege;
915 /* mark as modified */
916 allocate_luid( &token->modified_id );
918 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
919 privilege->enabled = FALSE;
922 int token_check_privileges( struct token *token, int all_required,
923 const LUID_AND_ATTRIBUTES *reqprivs,
924 unsigned int count, LUID_AND_ATTRIBUTES *usedprivs)
926 unsigned int i, enabled_count = 0;
928 for (i = 0; i < count; i++)
930 struct privilege *privilege =
931 token_find_privilege( token, &reqprivs[i].Luid, TRUE );
933 if (usedprivs)
934 usedprivs[i] = reqprivs[i];
936 if (privilege && privilege->enabled)
938 enabled_count++;
939 if (usedprivs)
940 usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS;
944 if (all_required)
945 return (enabled_count == count);
946 else
947 return (enabled_count > 0);
950 int token_sid_present( struct token *token, const SID *sid, int deny )
952 struct group *group;
954 if (security_equal_sid( token->user, sid )) return TRUE;
956 LIST_FOR_EACH_ENTRY( group, &token->groups, struct group, entry )
958 if (!group->enabled) continue;
959 if (group->deny_only && !deny) continue;
961 if (security_equal_sid( &group->sid, sid )) return TRUE;
964 return FALSE;
967 /* Checks access to a security descriptor. 'sd' must have been validated by
968 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
969 * the reason. 'status' parameter will indicate if access is granted or denied.
971 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
973 static unsigned int token_access_check( struct token *token,
974 const struct security_descriptor *sd,
975 unsigned int desired_access,
976 LUID_AND_ATTRIBUTES *privs,
977 unsigned int *priv_count,
978 const GENERIC_MAPPING *mapping,
979 unsigned int *granted_access,
980 unsigned int *status )
982 unsigned int current_access = 0;
983 unsigned int denied_access = 0;
984 ULONG i;
985 const ACL *dacl;
986 int dacl_present;
987 const ACE_HEADER *ace;
988 const SID *owner;
990 /* assume no access rights */
991 *granted_access = 0;
993 /* fail if desired_access contains generic rights */
994 if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
996 if (priv_count) *priv_count = 0;
997 return STATUS_GENERIC_NOT_MAPPED;
1000 dacl = sd_get_dacl( sd, &dacl_present );
1001 owner = sd_get_owner( sd );
1002 if (!owner || !sd_get_group( sd ))
1004 if (priv_count) *priv_count = 0;
1005 return STATUS_INVALID_SECURITY_DESCR;
1008 /* 1: Grant desired access if the object is unprotected */
1009 if (!dacl_present || !dacl)
1011 if (priv_count) *priv_count = 0;
1012 if (desired_access & MAXIMUM_ALLOWED)
1013 *granted_access = mapping->GenericAll;
1014 else
1015 *granted_access = desired_access;
1016 return *status = STATUS_SUCCESS;
1019 /* 2: Check if caller wants access to system security part. Note: access
1020 * is only granted if specifically asked for */
1021 if (desired_access & ACCESS_SYSTEM_SECURITY)
1023 const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 };
1024 LUID_AND_ATTRIBUTES retpriv = security_priv;
1025 if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
1027 if (priv_count)
1029 /* assumes that there will only be one privilege to return */
1030 if (*priv_count >= 1)
1032 *priv_count = 1;
1033 *privs = retpriv;
1035 else
1037 *priv_count = 1;
1038 return STATUS_BUFFER_TOO_SMALL;
1041 current_access |= ACCESS_SYSTEM_SECURITY;
1042 if (desired_access == current_access)
1044 *granted_access = current_access;
1045 return *status = STATUS_SUCCESS;
1048 else
1050 if (priv_count) *priv_count = 0;
1051 *status = STATUS_PRIVILEGE_NOT_HELD;
1052 return STATUS_SUCCESS;
1055 else if (priv_count) *priv_count = 0;
1057 /* 3: Check whether the token is the owner */
1058 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
1059 * checked when a "set owner" call is made, overriding the access rights
1060 * determined here. */
1061 if (token_sid_present( token, owner, FALSE ))
1063 current_access |= (READ_CONTROL | WRITE_DAC);
1064 if (desired_access == current_access)
1066 *granted_access = current_access;
1067 return *status = STATUS_SUCCESS;
1071 /* 4: Grant rights according to the DACL */
1072 ace = (const ACE_HEADER *)(dacl + 1);
1073 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
1075 const ACCESS_ALLOWED_ACE *aa_ace;
1076 const ACCESS_DENIED_ACE *ad_ace;
1077 const SID *sid;
1079 if (ace->AceFlags & INHERIT_ONLY_ACE)
1080 continue;
1082 switch (ace->AceType)
1084 case ACCESS_DENIED_ACE_TYPE:
1085 ad_ace = (const ACCESS_DENIED_ACE *)ace;
1086 sid = (const SID *)&ad_ace->SidStart;
1087 if (token_sid_present( token, sid, TRUE ))
1089 unsigned int access = ad_ace->Mask;
1090 map_generic_mask(&access, mapping);
1091 if (desired_access & MAXIMUM_ALLOWED)
1092 denied_access |= access;
1093 else
1095 denied_access |= (access & ~current_access);
1096 if (desired_access & access) goto done;
1099 break;
1100 case ACCESS_ALLOWED_ACE_TYPE:
1101 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
1102 sid = (const SID *)&aa_ace->SidStart;
1103 if (token_sid_present( token, sid, FALSE ))
1105 unsigned int access = aa_ace->Mask;
1106 map_generic_mask(&access, mapping);
1107 if (desired_access & MAXIMUM_ALLOWED)
1108 current_access |= access;
1109 else
1110 current_access |= (access & ~denied_access);
1112 break;
1115 /* don't bother carrying on checking if we've already got all of
1116 * rights we need */
1117 if (desired_access == *granted_access)
1118 break;
1121 done:
1122 if (desired_access & MAXIMUM_ALLOWED)
1123 *granted_access = current_access & ~denied_access;
1124 else
1125 if ((current_access & desired_access) == desired_access)
1126 *granted_access = current_access & desired_access;
1127 else
1128 *granted_access = 0;
1130 *status = *granted_access ? STATUS_SUCCESS : STATUS_ACCESS_DENIED;
1131 return STATUS_SUCCESS;
1134 const ACL *token_get_default_dacl( struct token *token )
1136 return token->default_dacl;
1139 const SID *token_get_user( struct token *token )
1141 return token->user;
1144 const SID *token_get_primary_group( struct token *token )
1146 return token->primary_group;
1149 int check_object_access(struct object *obj, unsigned int *access)
1151 GENERIC_MAPPING mapping;
1152 struct token *token = current->token ? current->token : current->process->token;
1153 unsigned int status;
1154 int res;
1156 mapping.GenericAll = obj->ops->map_access( obj, GENERIC_ALL );
1158 if (!obj->sd)
1160 if (*access & MAXIMUM_ALLOWED)
1161 *access = mapping.GenericAll;
1162 return TRUE;
1165 mapping.GenericRead = obj->ops->map_access( obj, GENERIC_READ );
1166 mapping.GenericWrite = obj->ops->map_access( obj, GENERIC_WRITE );
1167 mapping.GenericExecute = obj->ops->map_access( obj, GENERIC_EXECUTE );
1169 res = token_access_check( token, obj->sd, *access, NULL, NULL,
1170 &mapping, access, &status ) == STATUS_SUCCESS &&
1171 status == STATUS_SUCCESS;
1173 if (!res) set_error( STATUS_ACCESS_DENIED );
1174 return res;
1178 /* open a security token */
1179 DECL_HANDLER(open_token)
1181 if (req->flags & OPEN_TOKEN_THREAD)
1183 struct thread *thread = get_thread_from_handle( req->handle, 0 );
1184 if (thread)
1186 if (thread->token)
1188 if (!thread->token->primary && thread->token->impersonation_level <= SecurityAnonymous)
1189 set_error( STATUS_CANT_OPEN_ANONYMOUS );
1190 else
1191 reply->token = alloc_handle( current->process, thread->token,
1192 req->access, req->attributes );
1194 else
1195 set_error( STATUS_NO_TOKEN );
1196 release_object( thread );
1199 else
1201 struct process *process = get_process_from_handle( req->handle, 0 );
1202 if (process)
1204 if (process->token)
1205 reply->token = alloc_handle( current->process, process->token, req->access,
1206 req->attributes );
1207 else
1208 set_error( STATUS_NO_TOKEN );
1209 release_object( process );
1214 /* adjust the privileges held by a token */
1215 DECL_HANDLER(adjust_token_privileges)
1217 struct token *token;
1218 unsigned int access = TOKEN_ADJUST_PRIVILEGES;
1220 if (req->get_modified_state) access |= TOKEN_QUERY;
1222 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1223 access, &token_ops )))
1225 const LUID_AND_ATTRIBUTES *privs = get_req_data();
1226 LUID_AND_ATTRIBUTES *modified_privs = NULL;
1227 unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
1228 unsigned int modified_priv_count = 0;
1230 if (req->get_modified_state && !req->disable_all)
1232 unsigned int i;
1233 /* count modified privs */
1234 for (i = 0; i < priv_count; i++)
1236 struct privilege *privilege =
1237 token_find_privilege( token, &privs[i].Luid, FALSE );
1238 if (privilege && req->get_modified_state)
1239 modified_priv_count++;
1241 reply->len = modified_priv_count;
1242 modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
1243 if (modified_priv_count)
1244 modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
1246 reply->len = modified_priv_count * sizeof(*modified_privs);
1248 if (req->disable_all)
1249 token_disable_privileges( token );
1250 else
1251 token_adjust_privileges( token, privs, priv_count, modified_privs, modified_priv_count );
1253 release_object( token );
1257 /* retrieves the list of privileges that may be held be the token */
1258 DECL_HANDLER(get_token_privileges)
1260 struct token *token;
1262 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1263 TOKEN_QUERY,
1264 &token_ops )))
1266 int priv_count = 0;
1267 LUID_AND_ATTRIBUTES *privs;
1268 struct privilege *privilege;
1270 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
1271 priv_count++;
1273 reply->len = priv_count * sizeof(*privs);
1274 if (reply->len <= get_reply_max_size())
1276 privs = set_reply_data_size( priv_count * sizeof(*privs) );
1277 if (privs)
1279 int i = 0;
1280 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
1282 luid_and_attr_from_privilege( &privs[i], privilege );
1283 i++;
1287 else
1288 set_error(STATUS_BUFFER_TOO_SMALL);
1290 release_object( token );
1294 /* creates a duplicate of the token */
1295 DECL_HANDLER(duplicate_token)
1297 struct token *src_token;
1298 struct unicode_str name;
1299 const struct security_descriptor *sd;
1300 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1302 if (!objattr) return;
1304 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
1305 TOKEN_DUPLICATE,
1306 &token_ops )))
1308 struct token *token = token_duplicate( src_token, req->primary, req->impersonation_level, sd );
1309 if (token)
1311 reply->new_handle = alloc_handle_no_access_check( current->process, token, req->access, objattr->attributes );
1312 release_object( token );
1314 release_object( src_token );
1318 /* checks the specified privileges are held by the token */
1319 DECL_HANDLER(check_token_privileges)
1321 struct token *token;
1323 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1324 TOKEN_QUERY,
1325 &token_ops )))
1327 unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
1329 if (!token->primary && token->impersonation_level <= SecurityAnonymous)
1330 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
1331 else if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES))
1333 LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
1334 reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
1336 else
1337 set_error( STATUS_BUFFER_OVERFLOW );
1338 release_object( token );
1342 /* checks that a user represented by a token is allowed to access an object
1343 * represented by a security descriptor */
1344 DECL_HANDLER(access_check)
1346 data_size_t sd_size = get_req_data_size();
1347 const struct security_descriptor *sd = get_req_data();
1348 struct token *token;
1350 if (!sd_is_valid( sd, sd_size ))
1352 set_error( STATUS_ACCESS_VIOLATION );
1353 return;
1356 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1357 TOKEN_QUERY,
1358 &token_ops )))
1360 GENERIC_MAPPING mapping;
1361 unsigned int status;
1362 LUID_AND_ATTRIBUTES priv;
1363 unsigned int priv_count = 1;
1365 memset(&priv, 0, sizeof(priv));
1367 /* only impersonation tokens may be used with this function */
1368 if (token->primary)
1370 set_error( STATUS_NO_IMPERSONATION_TOKEN );
1371 release_object( token );
1372 return;
1374 /* anonymous impersonation tokens can't be used */
1375 if (token->impersonation_level <= SecurityAnonymous)
1377 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
1378 release_object( token );
1379 return;
1382 mapping.GenericRead = req->mapping_read;
1383 mapping.GenericWrite = req->mapping_write;
1384 mapping.GenericExecute = req->mapping_execute;
1385 mapping.GenericAll = req->mapping_all;
1387 status = token_access_check(
1388 token, sd, req->desired_access, &priv, &priv_count, &mapping,
1389 &reply->access_granted, &reply->access_status );
1391 reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES);
1393 if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
1395 LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) );
1396 memcpy( privs, &priv, sizeof(priv) );
1399 set_error( status );
1400 release_object( token );
1404 /* retrieves the SID of the user that the token represents */
1405 DECL_HANDLER(get_token_sid)
1407 struct token *token;
1409 reply->sid_len = 0;
1411 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1412 TOKEN_QUERY,
1413 &token_ops )))
1415 const SID *sid = NULL;
1417 switch (req->which_sid)
1419 case TokenUser:
1420 assert(token->user);
1421 sid = token->user;
1422 break;
1423 case TokenPrimaryGroup:
1424 sid = token->primary_group;
1425 break;
1426 case TokenOwner:
1428 struct group *group;
1429 LIST_FOR_EACH_ENTRY( group, &token->groups, struct group, entry )
1431 if (group->owner)
1433 sid = &group->sid;
1434 break;
1437 break;
1439 default:
1440 set_error( STATUS_INVALID_PARAMETER );
1441 break;
1444 if (sid)
1446 reply->sid_len = security_sid_len( sid );
1447 if (reply->sid_len <= get_reply_max_size()) set_reply_data( sid, reply->sid_len );
1448 else set_error( STATUS_BUFFER_TOO_SMALL );
1450 release_object( token );
1454 /* retrieves the groups that the user represented by the token belongs to */
1455 DECL_HANDLER(get_token_groups)
1457 struct token *token;
1459 reply->user_len = 0;
1461 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1462 TOKEN_QUERY,
1463 &token_ops )))
1465 size_t size_needed = sizeof(struct token_groups);
1466 size_t sid_size = 0;
1467 unsigned int group_count = 0;
1468 const struct group *group;
1470 LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
1472 group_count++;
1473 sid_size += security_sid_len( &group->sid );
1475 size_needed += sid_size;
1476 /* attributes size */
1477 size_needed += sizeof(unsigned int) * group_count;
1479 /* reply buffer contains size_needed bytes formatted as:
1481 unsigned int count;
1482 unsigned int attrib[count];
1483 char sid_data[];
1485 user_len includes extra data needed for TOKEN_GROUPS representation,
1486 required caller buffer size calculated here to avoid extra server call */
1487 reply->user_len = FIELD_OFFSET( TOKEN_GROUPS, Groups[group_count] ) + sid_size;
1489 if (reply->user_len <= get_reply_max_size())
1491 struct token_groups *tg = set_reply_data_size( size_needed );
1492 if (tg)
1494 unsigned int *attr_ptr = (unsigned int *)(tg + 1);
1495 SID *sid_ptr = (SID *)(attr_ptr + group_count);
1497 tg->count = group_count;
1499 LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
1502 *attr_ptr = 0;
1503 if (group->mandatory) *attr_ptr |= SE_GROUP_MANDATORY;
1504 if (group->def) *attr_ptr |= SE_GROUP_ENABLED_BY_DEFAULT;
1505 if (group->enabled) *attr_ptr |= SE_GROUP_ENABLED;
1506 if (group->owner) *attr_ptr |= SE_GROUP_OWNER;
1507 if (group->deny_only) *attr_ptr |= SE_GROUP_USE_FOR_DENY_ONLY;
1508 if (group->resource) *attr_ptr |= SE_GROUP_RESOURCE;
1509 if (group->logon) *attr_ptr |= SE_GROUP_LOGON_ID;
1511 memcpy(sid_ptr, &group->sid, security_sid_len( &group->sid ));
1513 sid_ptr = (SID *)((char *)sid_ptr + security_sid_len( &group->sid ));
1514 attr_ptr++;
1518 else set_error( STATUS_BUFFER_TOO_SMALL );
1520 release_object( token );
1524 DECL_HANDLER(get_token_impersonation_level)
1526 struct token *token;
1528 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1529 TOKEN_QUERY,
1530 &token_ops )))
1532 if (token->primary)
1533 set_error( STATUS_INVALID_PARAMETER );
1534 else
1535 reply->impersonation_level = token->impersonation_level;
1537 release_object( token );
1541 DECL_HANDLER(get_token_statistics)
1543 struct token *token;
1545 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1546 TOKEN_QUERY,
1547 &token_ops )))
1549 reply->token_id = token->token_id;
1550 reply->modified_id = token->modified_id;
1551 reply->primary = token->primary;
1552 reply->impersonation_level = token->impersonation_level;
1553 reply->group_count = list_count( &token->groups );
1554 reply->privilege_count = list_count( &token->privileges );
1556 release_object( token );
1560 DECL_HANDLER(get_token_default_dacl)
1562 struct token *token;
1564 reply->acl_len = 0;
1566 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1567 TOKEN_QUERY,
1568 &token_ops )))
1570 if (token->default_dacl)
1571 reply->acl_len = token->default_dacl->AclSize;
1573 if (reply->acl_len <= get_reply_max_size())
1575 ACL *acl_reply = set_reply_data_size( reply->acl_len );
1576 if (acl_reply)
1577 memcpy( acl_reply, token->default_dacl, reply->acl_len );
1579 else set_error( STATUS_BUFFER_TOO_SMALL );
1581 release_object( token );
1585 DECL_HANDLER(set_token_default_dacl)
1587 struct token *token;
1589 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1590 TOKEN_ADJUST_DEFAULT,
1591 &token_ops )))
1593 const ACL *acl = get_req_data();
1594 unsigned int acl_size = get_req_data_size();
1596 free( token->default_dacl );
1597 token->default_dacl = NULL;
1599 if (acl_size)
1600 token->default_dacl = memdup( acl, acl_size );
1602 release_object( token );