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 unsigned int token_map_access( struct object *obj, unsigned int access );
110 static void token_destroy( struct object *obj );
112 static const struct object_ops token_ops =
114 sizeof(struct token), /* size */
115 token_dump, /* dump */
116 no_add_queue, /* add_queue */
117 NULL, /* remove_queue */
119 NULL, /* satisfied */
120 no_signal, /* signal */
121 no_get_fd, /* get_fd */
122 token_map_access, /* map_access */
123 no_lookup_name, /* lookup_name */
124 no_close_handle, /* close_handle */
125 token_destroy /* destroy */
129 static void token_dump( struct object *obj, int verbose )
131 fprintf( stderr, "Security token\n" );
132 /* FIXME: dump token members */
135 static unsigned int token_map_access( struct object *obj, unsigned int access )
137 if (access & GENERIC_READ) access |= TOKEN_READ;
138 if (access & GENERIC_WRITE) access |= TOKEN_WRITE;
139 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
140 if (access & GENERIC_ALL) access |= TOKEN_ALL_ACCESS;
141 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
144 static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] )
147 SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) );
148 if (!sid) return NULL;
149 sid->Revision = SID_REVISION;
150 sid->SubAuthorityCount = subauthcount;
151 sid->IdentifierAuthority = *idauthority;
152 for (i = 0; i < subauthcount; i++)
153 sid->SubAuthority[i] = subauth[i];
157 static inline int security_equal_sid( const SID *sid1, const SID *sid2 )
159 return ((sid1->SubAuthorityCount == sid2->SubAuthorityCount) &&
160 !memcmp( sid1, sid2, FIELD_OFFSET(SID, SubAuthority[sid1->SubAuthorityCount]) ));
163 void security_set_thread_token( struct thread *thread, obj_handle_t handle )
168 release_object( thread->token );
169 thread->token = NULL;
173 struct token *token = (struct token *)get_handle_obj( current->process,
180 release_object( thread->token );
181 thread->token = token;
186 static const ACE_HEADER *ace_next( const ACE_HEADER *ace )
188 return (const ACE_HEADER *)((const char *)ace + ace->AceSize);
191 static int acl_is_valid( const ACL *acl, size_t size )
194 const ACE_HEADER *ace;
196 if (size < sizeof(ACL))
199 size = min(size, MAX_ACL_LEN);
203 ace = (const ACE_HEADER *)(acl + 1);
204 for (i = 0; i < acl->AceCount; i++)
209 if (size < sizeof(ACE_HEADER))
211 if (size < ace->AceSize)
213 size -= ace->AceSize;
214 switch (ace->AceType)
216 case ACCESS_DENIED_ACE_TYPE:
217 sid = (const SID *)&((const ACCESS_DENIED_ACE *)ace)->SidStart;
218 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart);
220 case ACCESS_ALLOWED_ACE_TYPE:
221 sid = (const SID *)&((const ACCESS_ALLOWED_ACE *)ace)->SidStart;
222 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart);
224 case SYSTEM_AUDIT_ACE_TYPE:
225 sid = (const SID *)&((const SYSTEM_AUDIT_ACE *)ace)->SidStart;
226 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart);
228 case SYSTEM_ALARM_ACE_TYPE:
229 sid = (const SID *)&((const SYSTEM_ALARM_ACE *)ace)->SidStart;
230 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_ALARM_ACE, SidStart);
235 if (sid_size < FIELD_OFFSET(SID, SubAuthority[0]) ||
236 sid_size < FIELD_OFFSET(SID, SubAuthority[sid->SubAuthorityCount]))
238 ace = ace_next( ace );
243 /* gets the discretionary access control list from a security descriptor */
244 static inline const ACL *sd_get_dacl( const struct security_descriptor *sd, int *present )
246 *present = (sd->control & SE_DACL_PRESENT ? TRUE : FALSE);
249 return (const ACL *)((const char *)(sd + 1) +
250 sd->owner_len + sd->group_len + sd->sacl_len);
255 /* gets the system access control list from a security descriptor */
256 static inline const ACL *sd_get_sacl( const struct security_descriptor *sd, int *present )
258 *present = (sd->control & SE_SACL_PRESENT ? TRUE : FALSE);
261 return (const ACL *)((const char *)(sd + 1) +
262 sd->owner_len + sd->group_len);
267 /* gets the owner from a security descriptor */
268 static inline const SID *sd_get_owner( const struct security_descriptor *sd )
271 return (const SID *)(sd + 1);
276 /* gets the primary group from a security descriptor */
277 static inline const SID *sd_get_group( const struct security_descriptor *sd )
280 return (const SID *)((const char *)(sd + 1) + sd->owner_len);
285 /* checks whether all members of a security descriptor fit inside the size
286 * of memory specified */
287 static int sd_is_valid( const struct security_descriptor *sd, size_t size )
289 size_t offset = sizeof(struct security_descriptor);
299 if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
300 (offset + sd->owner_len > size))
302 owner = sd_get_owner( sd );
305 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]);
306 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
309 offset += sd->owner_len;
311 if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
312 (offset + sd->group_len > size))
314 group = sd_get_group( sd );
317 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
318 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
321 offset += sd->group_len;
323 if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
325 sacl = sd_get_sacl( sd, &dummy );
326 if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
328 offset += sd->sacl_len;
330 if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
332 dacl = sd_get_dacl( sd, &dummy );
333 if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
335 offset += sd->dacl_len;
340 /* maps from generic rights to specific rights as given by a mapping */
341 static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping)
343 if (*mask & GENERIC_READ) *mask |= mapping->GenericRead;
344 if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite;
345 if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute;
346 if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll;
350 static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 )
352 return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart);
355 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in)
357 out->Luid = in->luid;
359 (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
360 (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
363 static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled )
365 struct privilege *privilege = mem_alloc( sizeof(*privilege) );
368 privilege->luid = *luid;
369 privilege->def = privilege->enabled = (enabled != 0);
370 list_add_tail( &token->privileges, &privilege->entry );
375 static inline void privilege_remove( struct privilege *privilege )
377 list_remove( &privilege->entry );
381 static void token_destroy( struct object *obj )
384 struct list *cursor, *cursor_next;
386 assert( obj->ops == &token_ops );
387 token = (struct token *)obj;
391 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
393 struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
394 privilege_remove( privilege );
397 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
399 struct sid_and_attributes *group = LIST_ENTRY( cursor, struct sid_and_attributes, entry );
400 list_remove( &group->entry );
404 free( token->default_dacl );
407 /* creates a new token.
408 * groups may be NULL if group_count is 0.
409 * privs may be NULL if priv_count is 0.
410 * default_dacl may be NULL, indicating that all objects created by the user
413 static struct token *create_token( unsigned primary, const SID *user,
414 const SID_AND_ATTRIBUTES *groups, unsigned int group_count,
415 const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count,
416 const ACL *default_dacl )
418 struct token *token = alloc_object( &token_ops );
423 list_init( &token->privileges );
424 list_init( &token->groups );
425 token->primary = primary;
428 token->user = memdup( user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
431 release_object( token );
436 for (i = 0; i < group_count; i++)
438 size_t size = FIELD_OFFSET( struct sid_and_attributes, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] );
439 struct sid_and_attributes *group = mem_alloc( size );
440 memcpy( &group->sid, groups[i].Sid, FIELD_OFFSET( SID, SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] ) );
441 group->enabled = TRUE;
443 group->logon = FALSE;
444 group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) ? TRUE : FALSE;
445 group->owner = FALSE;
446 group->resource = FALSE;
447 group->deny_only = FALSE;
448 list_add_tail( &token->groups, &group->entry );
451 /* copy privileges */
452 for (i = 0; i < priv_count; i++)
454 /* note: we don't check uniqueness: the caller must make sure
455 * privs doesn't contain any duplicate luids */
456 if (!privilege_add( token, &privs[i].Luid,
457 privs[i].Attributes & SE_PRIVILEGE_ENABLED ))
459 release_object( token );
466 token->default_dacl = memdup( default_dacl, default_dacl->AclSize );
467 if (!token->default_dacl)
469 release_object( token );
474 token->default_dacl = NULL;
479 static ACL *create_default_dacl( const SID *user )
481 ACCESS_ALLOWED_ACE *aaa;
484 size_t default_dacl_size = sizeof(ACL) +
485 2*(sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
486 sizeof(local_system_sid) +
487 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
489 default_dacl = mem_alloc( default_dacl_size );
490 if (!default_dacl) return NULL;
492 default_dacl->AclRevision = MAX_ACL_REVISION;
493 default_dacl->Sbz1 = 0;
494 default_dacl->AclSize = default_dacl_size;
495 default_dacl->AceCount = 2;
496 default_dacl->Sbz2 = 0;
498 /* GENERIC_ALL for Local System */
499 aaa = (ACCESS_ALLOWED_ACE *)(default_dacl + 1);
500 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
501 aaa->Header.AceFlags = 0;
502 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
503 sizeof(local_system_sid);
504 aaa->Mask = GENERIC_ALL;
505 sid = (SID *)&aaa->SidStart;
506 memcpy( sid, &local_system_sid, sizeof(local_system_sid) );
508 /* GENERIC_ALL for specified user */
509 aaa = (ACCESS_ALLOWED_ACE *)((const char *)aaa + aaa->Header.AceSize);
510 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
511 aaa->Header.AceFlags = 0;
512 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
513 FIELD_OFFSET( SID, SubAuthority[user->SubAuthorityCount] );
514 aaa->Mask = GENERIC_ALL;
515 sid = (SID *)&aaa->SidStart;
516 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
523 SID_IDENTIFIER_AUTHORITY idauth;
525 unsigned int subauth[MAX_SUBAUTH_COUNT];
528 struct token *token_create_admin( void )
530 struct token *token = NULL;
531 static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
532 static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
533 static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
534 PSID alias_admins_sid;
535 PSID alias_users_sid;
536 ACL *default_dacl = create_default_dacl( &local_system_sid );
538 alias_admins_sid = security_sid_alloc( &nt_authority, sizeof(alias_admins_subauth)/sizeof(alias_admins_subauth[0]),
539 alias_admins_subauth );
540 alias_users_sid = security_sid_alloc( &nt_authority, sizeof(alias_users_subauth)/sizeof(alias_users_subauth[0]),
541 alias_users_subauth );
543 if (alias_admins_sid && alias_users_sid && default_dacl)
545 const LUID_AND_ATTRIBUTES admin_privs[] =
547 { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
548 { SeSecurityPrivilege , 0 },
549 { SeBackupPrivilege , 0 },
550 { SeRestorePrivilege , 0 },
551 { SeSystemtimePrivilege , 0 },
552 { SeShutdownPrivilege , 0 },
553 { SeRemoteShutdownPrivilege , 0 },
554 { SeTakeOwnershipPrivilege , 0 },
555 { SeDebugPrivilege , 0 },
556 { SeSystemEnvironmentPrivilege , 0 },
557 { SeSystemProfilePrivilege , 0 },
558 { SeProfileSingleProcessPrivilege, 0 },
559 { SeIncreaseBasePriorityPrivilege, 0 },
560 { SeLoadDriverPrivilege , 0 },
561 { SeCreatePagefilePrivilege , 0 },
562 { SeIncreaseQuotaPrivilege , 0 },
563 { SeUndockPrivilege , 0 },
564 { SeManageVolumePrivilege , 0 },
565 { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED },
566 { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED },
568 /* note: we don't include non-builtin groups here for the user -
569 * telling us these is the job of a client-side program */
570 const SID_AND_ATTRIBUTES admin_groups[] =
572 { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
573 { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
574 { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
575 { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
576 { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
577 { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
579 /* note: we just set the user sid to be the interactive builtin sid -
580 * we should really translate the UNIX user id to a sid */
581 token = create_token( TRUE, &interactive_sid,
582 admin_groups, sizeof(admin_groups)/sizeof(admin_groups[0]),
583 admin_privs, sizeof(admin_privs)/sizeof(admin_privs[0]),
587 if (alias_admins_sid)
588 free( alias_admins_sid );
590 free( alias_users_sid );
592 free( default_dacl );
597 static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only )
599 struct privilege *privilege;
600 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
602 if (is_equal_luid( luid, &privilege->luid ))
604 if (enabled_only && !privilege->enabled)
612 static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs,
613 unsigned int count, LUID_AND_ATTRIBUTES *mod_privs,
614 unsigned int mod_privs_count )
617 unsigned int modified_count = 0;
619 for (i = 0; i < count; i++)
621 struct privilege *privilege =
622 token_find_privilege( token, &privs[i].Luid, FALSE );
625 set_error( STATUS_NOT_ALL_ASSIGNED );
629 if (privs[i].Attributes & SE_PRIVILEGE_REMOVE)
630 privilege_remove( privilege );
633 /* save previous state for caller */
636 luid_and_attr_from_privilege(mod_privs, privilege);
642 if (privs[i].Attributes & SE_PRIVILEGE_ENABLED)
643 privilege->enabled = TRUE;
645 privilege->enabled = FALSE;
648 return modified_count;
651 static void token_disable_privileges( struct token *token )
653 struct privilege *privilege;
654 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
655 privilege->enabled = FALSE;
658 int token_check_privileges( struct token *token, int all_required,
659 const LUID_AND_ATTRIBUTES *reqprivs,
660 unsigned int count, LUID_AND_ATTRIBUTES *usedprivs)
663 unsigned int enabled_count = 0;
665 for (i = 0; i < count; i++)
667 struct privilege *privilege =
668 token_find_privilege( token, &reqprivs[i].Luid, TRUE );
671 usedprivs[i] = reqprivs[i];
673 if (privilege && privilege->enabled)
677 usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS;
682 return (enabled_count == count);
684 return (enabled_count > 0);
687 static int token_sid_present( struct token *token, const SID *sid, int deny )
689 struct sid_and_attributes *group;
691 if (security_equal_sid( token->user, sid )) return TRUE;
693 LIST_FOR_EACH_ENTRY( group, &token->groups, struct sid_and_attributes, entry )
695 if (!group->enabled) continue;
696 if (group->deny_only && !deny) continue;
698 if (security_equal_sid( &group->sid, sid )) return TRUE;
704 /* checks access to a security descriptor. sd must have been validated by caller.
705 * it returns STATUS_SUCCESS if access was granted to the object, or an error
706 * status code if not, giving the reason. errors not relating to giving access
707 * to the object are returned in the status parameter. granted_access and
708 * status always have a valid value stored in them on return. */
709 static unsigned int token_access_check( struct token *token,
710 const struct security_descriptor *sd,
711 unsigned int desired_access,
712 LUID_AND_ATTRIBUTES *privs,
713 unsigned int *priv_count,
714 const GENERIC_MAPPING *mapping,
715 unsigned int *granted_access,
716 unsigned int *status )
718 unsigned int current_access = 0;
719 unsigned int denied_access = 0;
723 const ACE_HEADER *ace;
726 /* assume success, but no access rights */
727 *status = STATUS_SUCCESS;
730 /* fail if desired_access contains generic rights */
731 if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
734 *status = STATUS_GENERIC_NOT_MAPPED;
735 return STATUS_ACCESS_DENIED;
738 dacl = sd_get_dacl( sd, &dacl_present );
739 owner = sd_get_owner( sd );
740 if (!owner || !sd_get_group( sd ))
743 *status = STATUS_INVALID_SECURITY_DESCR;
744 return STATUS_ACCESS_DENIED;
747 /* 1: Grant desired access if the object is unprotected */
751 *granted_access = desired_access;
752 return STATUS_SUCCESS;
757 return STATUS_ACCESS_DENIED;
760 /* 2: Check if caller wants access to system security part. Note: access
761 * is only granted if specifically asked for */
762 if (desired_access & ACCESS_SYSTEM_SECURITY)
764 const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 };
765 LUID_AND_ATTRIBUTES retpriv = security_priv;
766 if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
770 /* assumes that there will only be one privilege to return */
771 if (*priv_count >= 1)
779 return STATUS_BUFFER_TOO_SMALL;
782 current_access |= ACCESS_SYSTEM_SECURITY;
783 if (desired_access == current_access)
785 *granted_access = current_access;
786 return STATUS_SUCCESS;
792 return STATUS_PRIVILEGE_NOT_HELD;
795 else if (priv_count) *priv_count = 0;
797 /* 3: Check whether the token is the owner */
798 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
799 * checked when a "set owner" call is made, overriding the access rights
800 * determined here. */
801 if (token_sid_present( token, owner, FALSE ))
803 current_access |= (READ_CONTROL | WRITE_DAC);
804 if (desired_access == current_access)
806 *granted_access = current_access;
807 return STATUS_SUCCESS;
811 /* 4: Grant rights according to the DACL */
812 ace = (const ACE_HEADER *)(dacl + 1);
813 for (i = 0; i < dacl->AceCount; i++)
815 const ACCESS_ALLOWED_ACE *aa_ace;
816 const ACCESS_DENIED_ACE *ad_ace;
818 switch (ace->AceType)
820 case ACCESS_DENIED_ACE_TYPE:
821 ad_ace = (const ACCESS_DENIED_ACE *)ace;
822 sid = (const SID *)&ad_ace->SidStart;
823 if (token_sid_present( token, sid, TRUE ))
825 unsigned int access = ad_ace->Mask;
826 map_generic_mask(&access, mapping);
827 if (desired_access & MAXIMUM_ALLOWED)
828 denied_access |= access;
831 denied_access |= (access & ~current_access);
832 if (desired_access & access)
835 return STATUS_SUCCESS;
840 case ACCESS_ALLOWED_ACE_TYPE:
841 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
842 sid = (const SID *)&aa_ace->SidStart;
843 if (token_sid_present( token, sid, FALSE ))
845 unsigned int access = aa_ace->Mask;
846 map_generic_mask(&access, mapping);
847 if (desired_access & MAXIMUM_ALLOWED)
848 current_access |= access;
850 current_access |= (access & ~denied_access);
855 /* don't bother carrying on checking if we've already got all of
857 if (desired_access == *granted_access)
860 ace = ace_next( ace );
863 if (desired_access & MAXIMUM_ALLOWED)
865 *granted_access = current_access & ~denied_access;
867 return STATUS_SUCCESS;
869 return STATUS_ACCESS_DENIED;
873 if ((current_access & desired_access) == desired_access)
875 *granted_access = current_access & desired_access;
876 return STATUS_SUCCESS;
879 return STATUS_ACCESS_DENIED;
883 const ACL *token_get_default_dacl( struct token *token )
885 return token->default_dacl;
888 /* open a security token */
889 DECL_HANDLER(open_token)
891 if (req->flags & OPEN_TOKEN_THREAD)
893 struct thread *thread = get_thread_from_handle( req->handle, 0 );
897 reply->token = alloc_handle( current->process, thread->token, req->access,
900 set_error(STATUS_NO_TOKEN);
901 release_object( thread );
906 struct process *process = get_process_from_handle( req->handle, 0 );
910 reply->token = alloc_handle( current->process, process->token, req->access,
913 set_error(STATUS_NO_TOKEN);
914 release_object( process );
919 /* adjust the privileges held by a token */
920 DECL_HANDLER(adjust_token_privileges)
923 unsigned int access = TOKEN_ADJUST_PRIVILEGES;
925 if (req->get_modified_state) access |= TOKEN_QUERY;
927 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
928 access, &token_ops )))
930 const LUID_AND_ATTRIBUTES *privs = get_req_data();
931 LUID_AND_ATTRIBUTES *modified_privs = NULL;
932 unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
933 unsigned int modified_priv_count = 0;
935 if (req->get_modified_state && !req->disable_all)
938 /* count modified privs */
939 for (i = 0; i < priv_count; i++)
941 struct privilege *privilege =
942 token_find_privilege( token, &privs[i].Luid, FALSE );
943 if (privilege && req->get_modified_state)
944 modified_priv_count++;
946 reply->len = modified_priv_count;
947 modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
948 if (modified_priv_count)
949 modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
951 reply->len = modified_priv_count * sizeof(*modified_privs);
953 if (req->disable_all)
954 token_disable_privileges( token );
956 modified_priv_count = token_adjust_privileges( token, privs,
957 priv_count, modified_privs, modified_priv_count );
959 release_object( token );
963 /* retrieves the list of privileges that may be held be the token */
964 DECL_HANDLER(get_token_privileges)
968 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
973 LUID_AND_ATTRIBUTES *privs;
974 struct privilege *privilege;
976 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
979 reply->len = priv_count * sizeof(*privs);
980 if (reply->len <= get_reply_max_size())
982 privs = set_reply_data_size( priv_count * sizeof(*privs) );
986 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
988 luid_and_attr_from_privilege( &privs[i], privilege );
994 set_error(STATUS_BUFFER_TOO_SMALL);
996 release_object( token );
1000 /* creates a duplicate of the token */
1001 DECL_HANDLER(duplicate_token)
1003 struct token *src_token;
1004 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
1008 /* FIXME: use req->impersonation_level */
1009 struct token *token = create_token( req->primary, src_token->user, NULL, 0, NULL, 0, src_token->default_dacl );
1012 struct privilege *privilege;
1013 struct sid_and_attributes *group;
1014 unsigned int access;
1017 LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct sid_and_attributes, entry )
1019 size_t size = FIELD_OFFSET( struct sid_and_attributes, sid.SubAuthority[group->sid.SubAuthorityCount] );
1020 struct sid_and_attributes *newgroup = mem_alloc( size );
1021 memcpy( newgroup, group, size );
1022 list_add_tail( &token->groups, &newgroup->entry );
1025 /* copy privileges */
1026 LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
1027 privilege_add( token, &privilege->luid, privilege->enabled );
1029 access = req->access;
1030 if (access & MAXIMUM_ALLOWED) access = TOKEN_ALL_ACCESS; /* FIXME: needs general solution */
1031 reply->new_handle = alloc_handle( current->process, token, access, req->attributes);
1032 release_object( token );
1034 release_object( src_token );
1038 /* checks the specified privileges are held by the token */
1039 DECL_HANDLER(check_token_privileges)
1041 struct token *token;
1043 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1047 unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
1048 if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES))
1050 LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
1051 reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
1054 set_error( STATUS_BUFFER_OVERFLOW );
1055 release_object( token );
1059 /* checks that a user represented by a token is allowed to access an object
1060 * represented by a security descriptor */
1061 DECL_HANDLER(access_check)
1063 size_t sd_size = get_req_data_size();
1064 const struct security_descriptor *sd = get_req_data();
1065 struct token *token;
1067 if (!sd_is_valid( sd, sd_size ))
1069 set_error( STATUS_ACCESS_VIOLATION );
1073 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1077 GENERIC_MAPPING mapping;
1078 unsigned int status;
1079 LUID_AND_ATTRIBUTES priv;
1080 unsigned int priv_count = 1;
1082 memset(&priv, 0, sizeof(priv));
1084 /* only impersonation tokens may be used with this function */
1087 set_error( STATUS_NO_IMPERSONATION_TOKEN );
1088 release_object( token );
1092 mapping.GenericRead = req->mapping_read;
1093 mapping.GenericWrite = req->mapping_write;
1094 mapping.GenericExecute = req->mapping_execute;
1095 mapping.GenericAll = req->mapping_all;
1097 reply->access_status = token_access_check(
1098 token, sd, req->desired_access, &priv, &priv_count, &mapping,
1099 &reply->access_granted, &status );
1101 reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES);
1103 if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
1105 LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) );
1106 memcpy( privs, &priv, sizeof(priv) );
1109 if (status != STATUS_SUCCESS)
1110 set_error( status );
1112 release_object( token );
1117 DECL_HANDLER(get_token_user)
1119 struct token *token;
1121 reply->user_len = 0;
1123 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1127 const SID *user = token->user;
1129 reply->user_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
1130 if (reply->user_len <= get_reply_max_size())
1132 SID *user_reply = set_reply_data_size( reply->user_len );
1134 memcpy( user_reply, user, reply->user_len );
1136 else set_error( STATUS_BUFFER_TOO_SMALL );
1138 release_object( token );