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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #define WIN32_NO_STATUS
41 #include "wine/unicode.h"
43 #define MAX_SUBAUTH_COUNT 1
45 const LUID SeIncreaseQuotaPrivilege = { 5, 0 };
46 const LUID SeSecurityPrivilege = { 8, 0 };
47 const LUID SeTakeOwnershipPrivilege = { 9, 0 };
48 const LUID SeLoadDriverPrivilege = { 10, 0 };
49 const LUID SeSystemProfilePrivilege = { 11, 0 };
50 const LUID SeSystemtimePrivilege = { 12, 0 };
51 const LUID SeProfileSingleProcessPrivilege = { 13, 0 };
52 const LUID SeIncreaseBasePriorityPrivilege = { 14, 0 };
53 const LUID SeCreatePagefilePrivilege = { 15, 0 };
54 const LUID SeBackupPrivilege = { 17, 0 };
55 const LUID SeRestorePrivilege = { 18, 0 };
56 const LUID SeShutdownPrivilege = { 19, 0 };
57 const LUID SeDebugPrivilege = { 20, 0 };
58 const LUID SeSystemEnvironmentPrivilege = { 22, 0 };
59 const LUID SeChangeNotifyPrivilege = { 23, 0 };
60 const LUID SeRemoteShutdownPrivilege = { 24, 0 };
61 const LUID SeUndockPrivilege = { 25, 0 };
62 const LUID SeManageVolumePrivilege = { 28, 0 };
63 const LUID SeImpersonatePrivilege = { 29, 0 };
64 const LUID SeCreateGlobalPrivilege = { 30, 0 };
66 static const SID world_sid = { SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY }, { SECURITY_WORLD_RID } };
67 static const SID local_sid = { SID_REVISION, 1, { SECURITY_LOCAL_SID_AUTHORITY }, { SECURITY_LOCAL_RID } };
68 static const SID interactive_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_INTERACTIVE_RID } };
69 static const SID authenticated_user_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_AUTHENTICATED_USER_RID } };
70 static const SID local_system_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SYSTEM_RID } };
71 static const PSID security_world_sid = (PSID)&world_sid;
72 static const PSID security_local_sid = (PSID)&local_sid;
73 const PSID security_interactive_sid = (PSID)&interactive_sid;
74 static const PSID security_authenticated_user_sid = (PSID)&authenticated_user_sid;
75 static const PSID security_local_system_sid = (PSID)&local_system_sid;
79 struct object obj; /* object header */
80 struct list privileges; /* privileges available to the token */
81 struct list groups; /* groups that the user of this token belongs to (sid_and_attributes) */
82 SID *user; /* SID of user this token represents */
83 unsigned primary; /* is this a primary or impersonation token? */
84 ACL *default_dacl; /* the default DACL to assign to objects created by this user */
91 unsigned enabled : 1; /* is the privilege currently enabled? */
92 unsigned def : 1; /* is the privilege enabled by default? */
95 struct sid_and_attributes
98 unsigned enabled : 1; /* is the sid currently enabled? */
99 unsigned def : 1; /* is the sid enabled by default? */
100 unsigned logon : 1; /* is this a logon sid? */
101 unsigned mandatory: 1; /* is this sid always enabled? */
102 unsigned owner : 1; /* can this sid be an owner of an object? */
103 unsigned resource : 1; /* is this a domain-local group? */
104 unsigned deny_only: 1; /* is this a sid that should be use for denying only? */
108 static void token_dump( struct object *obj, int verbose );
109 static void token_destroy( struct object *obj );
111 static const struct object_ops token_ops =
113 sizeof(struct token), /* size */
114 token_dump, /* dump */
115 no_add_queue, /* add_queue */
116 NULL, /* remove_queue */
118 NULL, /* satisfied */
119 no_signal, /* signal */
120 no_get_fd, /* get_fd */
121 no_lookup_name, /* lookup_name */
122 no_close_handle, /* close_handle */
123 token_destroy /* destroy */
127 static void token_dump( struct object *obj, int verbose )
129 fprintf( stderr, "Security token\n" );
130 /* FIXME: dump token members */
133 static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] )
136 SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) );
137 if (!sid) return NULL;
138 sid->Revision = SID_REVISION;
139 sid->SubAuthorityCount = subauthcount;
140 sid->IdentifierAuthority = *idauthority;
141 for (i = 0; i < subauthcount; i++)
142 sid->SubAuthority[i] = subauth[i];
146 static inline int security_equal_sid( const SID *sid1, const SID *sid2 )
148 return ((sid1->SubAuthorityCount == sid2->SubAuthorityCount) &&
149 !memcmp( sid1, sid2, FIELD_OFFSET(SID, SubAuthority[sid1->SubAuthorityCount]) ));
152 void security_set_thread_token( struct thread *thread, obj_handle_t handle )
157 release_object( thread->token );
158 thread->token = NULL;
162 struct token *token = (struct token *)get_handle_obj( current->process,
169 release_object( thread->token );
170 thread->token = token;
175 static const ACE_HEADER *ace_next( const ACE_HEADER *ace )
177 return (const ACE_HEADER *)((const char *)ace + ace->AceSize);
180 static int acl_is_valid( const ACL *acl, size_t size )
183 const ACE_HEADER *ace;
185 if (size < sizeof(ACL))
188 size = min(size, MAX_ACL_LEN);
192 ace = (const ACE_HEADER *)(acl + 1);
193 for (i = 0; i < acl->AceCount; i++)
198 if (size < sizeof(ACE_HEADER))
200 if (size < ace->AceSize)
202 size -= ace->AceSize;
203 switch (ace->AceType)
205 case ACCESS_DENIED_ACE_TYPE:
206 sid = (const SID *)&((const ACCESS_DENIED_ACE *)ace)->SidStart;
207 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart);
209 case ACCESS_ALLOWED_ACE_TYPE:
210 sid = (const SID *)&((const ACCESS_ALLOWED_ACE *)ace)->SidStart;
211 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart);
213 case SYSTEM_AUDIT_ACE_TYPE:
214 sid = (const SID *)&((const SYSTEM_AUDIT_ACE *)ace)->SidStart;
215 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart);
217 case SYSTEM_ALARM_ACE_TYPE:
218 sid = (const SID *)&((const SYSTEM_ALARM_ACE *)ace)->SidStart;
219 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_ALARM_ACE, SidStart);
224 if (sid_size < FIELD_OFFSET(SID, SubAuthority[0]) ||
225 sid_size < FIELD_OFFSET(SID, SubAuthority[sid->SubAuthorityCount]))
227 ace = ace_next( ace );
232 /* gets the discretionary access control list from a security descriptor */
233 static inline const ACL *sd_get_dacl( const struct security_descriptor *sd, int *present )
235 *present = (sd->control & SE_DACL_PRESENT ? TRUE : FALSE);
238 return (const ACL *)((const char *)(sd + 1) +
239 sd->owner_len + sd->group_len + sd->sacl_len);
244 /* gets the system access control list from a security descriptor */
245 static inline const ACL *sd_get_sacl( const struct security_descriptor *sd, int *present )
247 *present = (sd->control & SE_SACL_PRESENT ? TRUE : FALSE);
250 return (const ACL *)((const char *)(sd + 1) +
251 sd->owner_len + sd->group_len);
256 /* gets the owner from a security descriptor */
257 static inline const SID *sd_get_owner( const struct security_descriptor *sd )
260 return (const SID *)(sd + 1);
265 /* gets the primary group from a security descriptor */
266 static inline const SID *sd_get_group( const struct security_descriptor *sd )
269 return (const SID *)((const char *)(sd + 1) + sd->owner_len);
274 /* checks whether all members of a security descriptor fit inside the size
275 * of memory specified */
276 static int sd_is_valid( const struct security_descriptor *sd, size_t size )
278 size_t offset = sizeof(struct security_descriptor);
288 if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
289 (offset + sd->owner_len > size))
291 owner = sd_get_owner( sd );
294 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]);
295 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
298 offset += sd->owner_len;
300 if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
301 (offset + sd->group_len > size))
303 group = sd_get_group( sd );
306 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
307 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
310 offset += sd->group_len;
312 if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
314 sacl = sd_get_sacl( sd, &dummy );
315 if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
317 offset += sd->sacl_len;
319 if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
321 dacl = sd_get_dacl( sd, &dummy );
322 if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
324 offset += sd->dacl_len;
329 /* maps from generic rights to specific rights as given by a mapping */
330 static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping)
332 if (*mask & GENERIC_READ) *mask |= mapping->GenericRead;
333 if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite;
334 if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute;
335 if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll;
339 static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 )
341 return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart);
344 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in)
346 out->Luid = in->luid;
348 (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
349 (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
352 static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled )
354 struct privilege *privilege = mem_alloc( sizeof(*privilege) );
357 privilege->luid = *luid;
358 privilege->def = privilege->enabled = (enabled != 0);
359 list_add_tail( &token->privileges, &privilege->entry );
364 static inline void privilege_remove( struct privilege *privilege )
366 list_remove( &privilege->entry );
370 static void token_destroy( struct object *obj )
373 struct list *cursor, *cursor_next;
375 assert( obj->ops == &token_ops );
376 token = (struct token *)obj;
380 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
382 struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
383 privilege_remove( privilege );
386 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
388 struct sid_and_attributes *group = LIST_ENTRY( cursor, struct sid_and_attributes, entry );
389 list_remove( &group->entry );
393 free( token->default_dacl );
396 /* creates a new token.
397 * groups may be NULL if group_count is 0.
398 * privs may be NULL if priv_count is 0.
399 * default_dacl may be NULL, indicating that all objects created by the user
402 static struct token *create_token( unsigned primary, const SID *user,
403 const SID_AND_ATTRIBUTES *groups, unsigned int group_count,
404 const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count,
405 const ACL *default_dacl )
407 struct token *token = alloc_object( &token_ops );
412 list_init( &token->privileges );
413 list_init( &token->groups );
414 token->primary = primary;
417 token->user = memdup( user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
420 release_object( token );
425 for (i = 0; i < group_count; i++)
427 size_t size = FIELD_OFFSET( struct sid_and_attributes, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] );
428 struct sid_and_attributes *group = mem_alloc( size );
429 memcpy( &group->sid, groups[i].Sid, FIELD_OFFSET( SID, SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] ) );
430 group->enabled = TRUE;
432 group->logon = FALSE;
433 group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) ? TRUE : FALSE;
434 group->owner = FALSE;
435 group->resource = FALSE;
436 group->deny_only = FALSE;
437 list_add_tail( &token->groups, &group->entry );
440 /* copy privileges */
441 for (i = 0; i < priv_count; i++)
443 /* note: we don't check uniqueness: the caller must make sure
444 * privs doesn't contain any duplicate luids */
445 if (!privilege_add( token, &privs[i].Luid,
446 privs[i].Attributes & SE_PRIVILEGE_ENABLED ))
448 release_object( token );
455 token->default_dacl = memdup( default_dacl, default_dacl->AclSize );
456 if (!token->default_dacl)
458 release_object( token );
463 token->default_dacl = NULL;
468 static ACL *create_default_dacl( const SID *user )
470 ACCESS_ALLOWED_ACE *aaa;
473 size_t default_dacl_size = sizeof(ACL) +
474 2*(sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
475 sizeof(local_system_sid) +
476 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
478 default_dacl = mem_alloc( default_dacl_size );
479 if (!default_dacl) return NULL;
481 default_dacl->AclRevision = MAX_ACL_REVISION;
482 default_dacl->Sbz1 = 0;
483 default_dacl->AclSize = default_dacl_size;
484 default_dacl->AceCount = 2;
485 default_dacl->Sbz2 = 0;
487 /* GENERIC_ALL for Local System */
488 aaa = (ACCESS_ALLOWED_ACE *)(default_dacl + 1);
489 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
490 aaa->Header.AceFlags = 0;
491 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
492 sizeof(local_system_sid);
493 aaa->Mask = GENERIC_ALL;
494 sid = (SID *)&aaa->SidStart;
495 memcpy( sid, &local_system_sid, sizeof(local_system_sid) );
497 /* GENERIC_ALL for specified user */
498 aaa = (ACCESS_ALLOWED_ACE *)((const char *)aaa + aaa->Header.AceSize);
499 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
500 aaa->Header.AceFlags = 0;
501 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
502 FIELD_OFFSET( SID, SubAuthority[user->SubAuthorityCount] );
503 aaa->Mask = GENERIC_ALL;
504 sid = (SID *)&aaa->SidStart;
505 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
512 SID_IDENTIFIER_AUTHORITY idauth;
514 unsigned int subauth[MAX_SUBAUTH_COUNT];
517 struct token *token_create_admin( void )
519 struct token *token = NULL;
520 static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
521 static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
522 static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
523 PSID alias_admins_sid;
524 PSID alias_users_sid;
525 ACL *default_dacl = create_default_dacl( &local_system_sid );
527 alias_admins_sid = security_sid_alloc( &nt_authority, sizeof(alias_admins_subauth)/sizeof(alias_admins_subauth[0]),
528 alias_admins_subauth );
529 alias_users_sid = security_sid_alloc( &nt_authority, sizeof(alias_users_subauth)/sizeof(alias_users_subauth[0]),
530 alias_users_subauth );
532 if (alias_admins_sid && alias_users_sid && default_dacl)
534 const LUID_AND_ATTRIBUTES admin_privs[] =
536 { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
537 { SeSecurityPrivilege , 0 },
538 { SeBackupPrivilege , 0 },
539 { SeRestorePrivilege , 0 },
540 { SeSystemtimePrivilege , 0 },
541 { SeShutdownPrivilege , 0 },
542 { SeRemoteShutdownPrivilege , 0 },
543 { SeTakeOwnershipPrivilege , 0 },
544 { SeDebugPrivilege , 0 },
545 { SeSystemEnvironmentPrivilege , 0 },
546 { SeSystemProfilePrivilege , 0 },
547 { SeProfileSingleProcessPrivilege, 0 },
548 { SeIncreaseBasePriorityPrivilege, 0 },
549 { SeLoadDriverPrivilege , 0 },
550 { SeCreatePagefilePrivilege , 0 },
551 { SeIncreaseQuotaPrivilege , 0 },
552 { SeUndockPrivilege , 0 },
553 { SeManageVolumePrivilege , 0 },
554 { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED },
555 { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED },
557 /* note: we don't include non-builtin groups here for the user -
558 * telling us these is the job of a client-side program */
559 const SID_AND_ATTRIBUTES admin_groups[] =
561 { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
562 { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
563 { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
564 { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
565 { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
566 { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
568 /* note: we just set the user sid to be the interactive builtin sid -
569 * we should really translate the UNIX user id to a sid */
570 token = create_token( TRUE, &interactive_sid,
571 admin_groups, sizeof(admin_groups)/sizeof(admin_groups[0]),
572 admin_privs, sizeof(admin_privs)/sizeof(admin_privs[0]),
576 if (alias_admins_sid)
577 free( alias_admins_sid );
579 free( alias_users_sid );
581 free( default_dacl );
586 static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only )
588 struct privilege *privilege;
589 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
591 if (is_equal_luid( luid, &privilege->luid ))
593 if (enabled_only && !privilege->enabled)
601 static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs,
602 unsigned int count, LUID_AND_ATTRIBUTES *mod_privs,
603 unsigned int mod_privs_count )
606 unsigned int modified_count = 0;
608 for (i = 0; i < count; i++)
610 struct privilege *privilege =
611 token_find_privilege( token, &privs[i].Luid, FALSE );
614 set_error( STATUS_NOT_ALL_ASSIGNED );
618 if (privs[i].Attributes & SE_PRIVILEGE_REMOVE)
619 privilege_remove( privilege );
622 /* save previous state for caller */
625 luid_and_attr_from_privilege(mod_privs, privilege);
631 if (privs[i].Attributes & SE_PRIVILEGE_ENABLED)
632 privilege->enabled = TRUE;
634 privilege->enabled = FALSE;
637 return modified_count;
640 static void token_disable_privileges( struct token *token )
642 struct privilege *privilege;
643 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
644 privilege->enabled = FALSE;
647 int token_check_privileges( struct token *token, int all_required,
648 const LUID_AND_ATTRIBUTES *reqprivs,
649 unsigned int count, LUID_AND_ATTRIBUTES *usedprivs)
652 unsigned int enabled_count = 0;
654 for (i = 0; i < count; i++)
656 struct privilege *privilege =
657 token_find_privilege( token, &reqprivs[i].Luid, TRUE );
660 usedprivs[i] = reqprivs[i];
662 if (privilege && privilege->enabled)
666 usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS;
671 return (enabled_count == count);
673 return (enabled_count > 0);
676 static int token_sid_present( struct token *token, const SID *sid, int deny )
678 struct sid_and_attributes *group;
680 if (security_equal_sid( token->user, sid )) return TRUE;
682 LIST_FOR_EACH_ENTRY( group, &token->groups, struct sid_and_attributes, entry )
684 if (!group->enabled) continue;
685 if (group->deny_only && !deny) continue;
687 if (security_equal_sid( &group->sid, sid )) return TRUE;
693 /* checks access to a security descriptor. sd must have been validated by caller.
694 * it returns STATUS_SUCCESS if access was granted to the object, or an error
695 * status code if not, giving the reason. errors not relating to giving access
696 * to the object are returned in the status parameter. granted_access and
697 * status always have a valid value stored in them on return. */
698 static unsigned int token_access_check( struct token *token,
699 const struct security_descriptor *sd,
700 unsigned int desired_access,
701 LUID_AND_ATTRIBUTES *privs,
702 unsigned int *priv_count,
703 const GENERIC_MAPPING *mapping,
704 unsigned int *granted_access,
705 unsigned int *status )
707 unsigned int current_access = 0;
708 unsigned int denied_access = 0;
712 const ACE_HEADER *ace;
715 /* assume success, but no access rights */
716 *status = STATUS_SUCCESS;
719 /* fail if desired_access contains generic rights */
720 if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
723 *status = STATUS_GENERIC_NOT_MAPPED;
724 return STATUS_ACCESS_DENIED;
727 dacl = sd_get_dacl( sd, &dacl_present );
728 owner = sd_get_owner( sd );
729 if (!owner || !sd_get_group( sd ))
732 *status = STATUS_INVALID_SECURITY_DESCR;
733 return STATUS_ACCESS_DENIED;
736 /* 1: Grant desired access if the object is unprotected */
740 *granted_access = desired_access;
741 return STATUS_SUCCESS;
746 return STATUS_ACCESS_DENIED;
749 /* 2: Check if caller wants access to system security part. Note: access
750 * is only granted if specifically asked for */
751 if (desired_access & ACCESS_SYSTEM_SECURITY)
753 const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 };
754 LUID_AND_ATTRIBUTES retpriv = security_priv;
755 if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
759 /* assumes that there will only be one privilege to return */
760 if (*priv_count >= 1)
768 return STATUS_BUFFER_TOO_SMALL;
771 current_access |= ACCESS_SYSTEM_SECURITY;
772 if (desired_access == current_access)
774 *granted_access = current_access;
775 return STATUS_SUCCESS;
781 return STATUS_PRIVILEGE_NOT_HELD;
784 else if (priv_count) *priv_count = 0;
786 /* 3: Check whether the token is the owner */
787 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
788 * checked when a "set owner" call is made, overriding the access rights
789 * determined here. */
790 if (token_sid_present( token, owner, FALSE ))
792 current_access |= (READ_CONTROL | WRITE_DAC);
793 if (desired_access == current_access)
795 *granted_access = current_access;
796 return STATUS_SUCCESS;
800 /* 4: Grant rights according to the DACL */
801 ace = (const ACE_HEADER *)(dacl + 1);
802 for (i = 0; i < dacl->AceCount; i++)
804 const ACCESS_ALLOWED_ACE *aa_ace;
805 const ACCESS_DENIED_ACE *ad_ace;
807 switch (ace->AceType)
809 case ACCESS_DENIED_ACE_TYPE:
810 ad_ace = (const ACCESS_DENIED_ACE *)ace;
811 sid = (const SID *)&ad_ace->SidStart;
812 if (token_sid_present( token, sid, TRUE ))
814 unsigned int access = ad_ace->Mask;
815 map_generic_mask(&access, mapping);
816 if (desired_access & MAXIMUM_ALLOWED)
817 denied_access |= access;
820 denied_access |= (access & ~current_access);
821 if (desired_access & access)
824 return STATUS_SUCCESS;
829 case ACCESS_ALLOWED_ACE_TYPE:
830 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
831 sid = (const SID *)&aa_ace->SidStart;
832 if (token_sid_present( token, sid, FALSE ))
834 unsigned int access = aa_ace->Mask;
835 map_generic_mask(&access, mapping);
836 if (desired_access & MAXIMUM_ALLOWED)
837 current_access |= access;
839 current_access |= (access & ~denied_access);
844 /* don't bother carrying on checking if we've already got all of
846 if (desired_access == *granted_access)
849 ace = ace_next( ace );
852 if (desired_access & MAXIMUM_ALLOWED)
854 *granted_access = current_access & ~denied_access;
856 return STATUS_SUCCESS;
858 return STATUS_ACCESS_DENIED;
862 if ((current_access & desired_access) == desired_access)
864 *granted_access = current_access & desired_access;
865 return STATUS_SUCCESS;
868 return STATUS_ACCESS_DENIED;
872 const ACL *token_get_default_dacl( struct token *token )
874 return token->default_dacl;
877 /* open a security token */
878 DECL_HANDLER(open_token)
880 if (req->flags & OPEN_TOKEN_THREAD)
882 struct thread *thread = get_thread_from_handle( req->handle, 0 );
886 reply->token = alloc_handle( current->process, thread->token, TOKEN_ALL_ACCESS, 0);
888 set_error(STATUS_NO_TOKEN);
889 release_object( thread );
894 struct process *process = get_process_from_handle( req->handle, 0 );
898 reply->token = alloc_handle( current->process, process->token, TOKEN_ALL_ACCESS, 0);
900 set_error(STATUS_NO_TOKEN);
901 release_object( process );
906 /* adjust the privileges held by a token */
907 DECL_HANDLER(adjust_token_privileges)
910 unsigned int access = TOKEN_ADJUST_PRIVILEGES;
912 if (req->get_modified_state) access |= TOKEN_QUERY;
914 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
915 access, &token_ops )))
917 const LUID_AND_ATTRIBUTES *privs = get_req_data();
918 LUID_AND_ATTRIBUTES *modified_privs = NULL;
919 unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
920 unsigned int modified_priv_count = 0;
922 if (req->get_modified_state && !req->disable_all)
925 /* count modified privs */
926 for (i = 0; i < priv_count; i++)
928 struct privilege *privilege =
929 token_find_privilege( token, &privs[i].Luid, FALSE );
930 if (privilege && req->get_modified_state)
931 modified_priv_count++;
933 reply->len = modified_priv_count;
934 modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
935 if (modified_priv_count)
936 modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
938 reply->len = modified_priv_count * sizeof(*modified_privs);
940 if (req->disable_all)
941 token_disable_privileges( token );
943 modified_priv_count = token_adjust_privileges( token, privs,
944 priv_count, modified_privs, modified_priv_count );
946 release_object( token );
950 /* retrieves the list of privileges that may be held be the token */
951 DECL_HANDLER(get_token_privileges)
955 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
960 LUID_AND_ATTRIBUTES *privs;
961 struct privilege *privilege;
963 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
966 reply->len = priv_count * sizeof(*privs);
967 if (reply->len <= get_reply_max_size())
969 privs = set_reply_data_size( priv_count * sizeof(*privs) );
973 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
975 luid_and_attr_from_privilege( &privs[i], privilege );
981 set_error(STATUS_BUFFER_TOO_SMALL);
983 release_object( token );
987 /* creates a duplicate of the token */
988 DECL_HANDLER(duplicate_token)
990 struct token *src_token;
991 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
995 /* FIXME: use req->impersonation_level */
996 struct token *token = create_token( req->primary, src_token->user, NULL, 0, NULL, 0, src_token->default_dacl );
999 struct privilege *privilege;
1000 struct sid_and_attributes *group;
1001 unsigned int access;
1004 LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct sid_and_attributes, entry )
1006 size_t size = FIELD_OFFSET( struct sid_and_attributes, sid.SubAuthority[group->sid.SubAuthorityCount] );
1007 struct sid_and_attributes *newgroup = mem_alloc( size );
1008 memcpy( newgroup, group, size );
1009 list_add_tail( &token->groups, &newgroup->entry );
1012 /* copy privileges */
1013 LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
1014 privilege_add( token, &privilege->luid, privilege->enabled );
1016 access = req->access;
1017 if (access & MAXIMUM_ALLOWED) access = TOKEN_ALL_ACCESS; /* FIXME: needs general solution */
1018 reply->new_handle = alloc_handle( current->process, token, access, req->inherit);
1019 release_object( token );
1021 release_object( src_token );
1025 /* checks the specified privileges are held by the token */
1026 DECL_HANDLER(check_token_privileges)
1028 struct token *token;
1030 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1034 unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
1035 if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES))
1037 LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
1038 reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
1041 set_error( STATUS_BUFFER_OVERFLOW );
1042 release_object( token );
1046 /* checks that a user represented by a token is allowed to access an object
1047 * represented by a security descriptor */
1048 DECL_HANDLER(access_check)
1050 size_t sd_size = get_req_data_size();
1051 const struct security_descriptor *sd = get_req_data();
1052 struct token *token;
1054 if (!sd_is_valid( sd, sd_size ))
1056 set_error( STATUS_ACCESS_VIOLATION );
1060 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1064 GENERIC_MAPPING mapping;
1065 unsigned int status;
1066 LUID_AND_ATTRIBUTES priv;
1067 unsigned int priv_count = 1;
1069 memset(&priv, 0, sizeof(priv));
1071 /* only impersonation tokens may be used with this function */
1074 set_error( STATUS_NO_IMPERSONATION_TOKEN );
1075 release_object( token );
1079 mapping.GenericRead = req->mapping_read;
1080 mapping.GenericWrite = req->mapping_write;
1081 mapping.GenericExecute = req->mapping_execute;
1082 mapping.GenericAll = req->mapping_all;
1084 reply->access_status = token_access_check(
1085 token, sd, req->desired_access, &priv, &priv_count, &mapping,
1086 &reply->access_granted, &status );
1088 reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES);
1090 if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
1092 LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) );
1093 memcpy( privs, &priv, sizeof(priv) );
1096 if (status != STATUS_SUCCESS)
1097 set_error( status );
1099 release_object( token );
1104 DECL_HANDLER(get_token_user)
1106 struct token *token;
1108 reply->user_len = 0;
1110 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1114 const SID *user = token->user;
1116 reply->user_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
1117 if (reply->user_len <= get_reply_max_size())
1119 SID *user_reply = set_reply_data_size( reply->user_len );
1121 memcpy( user_reply, user, reply->user_len );
1123 else set_error( STATUS_BUFFER_TOO_SMALL );
1125 release_object( token );