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
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 */
85 TOKEN_SOURCE source; /* source of the token */
92 unsigned enabled : 1; /* is the privilege currently enabled? */
93 unsigned def : 1; /* is the privilege enabled by default? */
99 unsigned enabled : 1; /* is the sid currently enabled? */
100 unsigned def : 1; /* is the sid enabled by default? */
101 unsigned logon : 1; /* is this a logon sid? */
102 unsigned mandatory: 1; /* is this sid always enabled? */
103 unsigned owner : 1; /* can this sid be an owner of an object? */
104 unsigned resource : 1; /* is this a domain-local group? */
105 unsigned deny_only: 1; /* is this a sid that should be use for denying only? */
109 static void token_dump( struct object *obj, int verbose );
110 static unsigned int token_map_access( struct object *obj, unsigned int access );
111 static void token_destroy( struct object *obj );
113 static const struct object_ops token_ops =
115 sizeof(struct token), /* size */
116 token_dump, /* dump */
117 no_add_queue, /* add_queue */
118 NULL, /* remove_queue */
120 NULL, /* satisfied */
121 no_signal, /* signal */
122 no_get_fd, /* get_fd */
123 token_map_access, /* map_access */
124 no_lookup_name, /* lookup_name */
125 no_close_handle, /* close_handle */
126 token_destroy /* destroy */
130 static void token_dump( struct object *obj, int verbose )
132 fprintf( stderr, "Security token\n" );
133 /* FIXME: dump token members */
136 static unsigned int token_map_access( struct object *obj, unsigned int access )
138 if (access & GENERIC_READ) access |= TOKEN_READ;
139 if (access & GENERIC_WRITE) access |= TOKEN_WRITE;
140 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
141 if (access & GENERIC_ALL) access |= TOKEN_ALL_ACCESS;
142 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
145 static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] )
148 SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) );
149 if (!sid) return NULL;
150 sid->Revision = SID_REVISION;
151 sid->SubAuthorityCount = subauthcount;
152 sid->IdentifierAuthority = *idauthority;
153 for (i = 0; i < subauthcount; i++)
154 sid->SubAuthority[i] = subauth[i];
158 static inline int security_equal_sid( const SID *sid1, const SID *sid2 )
160 return ((sid1->SubAuthorityCount == sid2->SubAuthorityCount) &&
161 !memcmp( sid1, sid2, FIELD_OFFSET(SID, SubAuthority[sid1->SubAuthorityCount]) ));
164 void security_set_thread_token( struct thread *thread, obj_handle_t handle )
169 release_object( thread->token );
170 thread->token = NULL;
174 struct token *token = (struct token *)get_handle_obj( current->process,
181 release_object( thread->token );
182 thread->token = token;
187 static const ACE_HEADER *ace_next( const ACE_HEADER *ace )
189 return (const ACE_HEADER *)((const char *)ace + ace->AceSize);
192 static int acl_is_valid( const ACL *acl, data_size_t size )
195 const ACE_HEADER *ace;
197 if (size < sizeof(ACL))
200 size = min(size, MAX_ACL_LEN);
204 ace = (const ACE_HEADER *)(acl + 1);
205 for (i = 0; i < acl->AceCount; i++)
208 data_size_t sid_size;
210 if (size < sizeof(ACE_HEADER))
212 if (size < ace->AceSize)
214 size -= ace->AceSize;
215 switch (ace->AceType)
217 case ACCESS_DENIED_ACE_TYPE:
218 sid = (const SID *)&((const ACCESS_DENIED_ACE *)ace)->SidStart;
219 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart);
221 case ACCESS_ALLOWED_ACE_TYPE:
222 sid = (const SID *)&((const ACCESS_ALLOWED_ACE *)ace)->SidStart;
223 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart);
225 case SYSTEM_AUDIT_ACE_TYPE:
226 sid = (const SID *)&((const SYSTEM_AUDIT_ACE *)ace)->SidStart;
227 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart);
229 case SYSTEM_ALARM_ACE_TYPE:
230 sid = (const SID *)&((const SYSTEM_ALARM_ACE *)ace)->SidStart;
231 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_ALARM_ACE, SidStart);
236 if (sid_size < FIELD_OFFSET(SID, SubAuthority[0]) ||
237 sid_size < FIELD_OFFSET(SID, SubAuthority[sid->SubAuthorityCount]))
239 ace = ace_next( ace );
244 /* gets the discretionary access control list from a security descriptor */
245 static inline const ACL *sd_get_dacl( const struct security_descriptor *sd, int *present )
247 *present = (sd->control & SE_DACL_PRESENT ? TRUE : FALSE);
250 return (const ACL *)((const char *)(sd + 1) +
251 sd->owner_len + sd->group_len + sd->sacl_len);
256 /* gets the system access control list from a security descriptor */
257 static inline const ACL *sd_get_sacl( const struct security_descriptor *sd, int *present )
259 *present = (sd->control & SE_SACL_PRESENT ? TRUE : FALSE);
262 return (const ACL *)((const char *)(sd + 1) +
263 sd->owner_len + sd->group_len);
268 /* gets the owner from a security descriptor */
269 static inline const SID *sd_get_owner( const struct security_descriptor *sd )
272 return (const SID *)(sd + 1);
277 /* gets the primary group from a security descriptor */
278 static inline const SID *sd_get_group( const struct security_descriptor *sd )
281 return (const SID *)((const char *)(sd + 1) + sd->owner_len);
286 /* checks whether all members of a security descriptor fit inside the size
287 * of memory specified */
288 static int sd_is_valid( const struct security_descriptor *sd, data_size_t size )
290 size_t offset = sizeof(struct security_descriptor);
300 if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
301 (offset + sd->owner_len > size))
303 owner = sd_get_owner( sd );
306 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]);
307 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
310 offset += sd->owner_len;
312 if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
313 (offset + sd->group_len > size))
315 group = sd_get_group( sd );
318 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
319 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
322 offset += sd->group_len;
324 if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
326 sacl = sd_get_sacl( sd, &dummy );
327 if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
329 offset += sd->sacl_len;
331 if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
333 dacl = sd_get_dacl( sd, &dummy );
334 if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
336 offset += sd->dacl_len;
341 /* maps from generic rights to specific rights as given by a mapping */
342 static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping)
344 if (*mask & GENERIC_READ) *mask |= mapping->GenericRead;
345 if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite;
346 if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute;
347 if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll;
351 static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 )
353 return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart);
356 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in)
358 out->Luid = in->luid;
360 (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
361 (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
364 static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled )
366 struct privilege *privilege = mem_alloc( sizeof(*privilege) );
369 privilege->luid = *luid;
370 privilege->def = privilege->enabled = (enabled != 0);
371 list_add_tail( &token->privileges, &privilege->entry );
376 static inline void privilege_remove( struct privilege *privilege )
378 list_remove( &privilege->entry );
382 static void token_destroy( struct object *obj )
385 struct list *cursor, *cursor_next;
387 assert( obj->ops == &token_ops );
388 token = (struct token *)obj;
392 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
394 struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
395 privilege_remove( privilege );
398 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
400 struct group *group = LIST_ENTRY( cursor, struct group, entry );
401 list_remove( &group->entry );
405 free( token->default_dacl );
408 /* creates a new token.
409 * groups may be NULL if group_count is 0.
410 * privs may be NULL if priv_count is 0.
411 * default_dacl may be NULL, indicating that all objects created by the user
414 static struct token *create_token( unsigned primary, const SID *user,
415 const SID_AND_ATTRIBUTES *groups, unsigned int group_count,
416 const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count,
417 const ACL *default_dacl, TOKEN_SOURCE source )
419 struct token *token = alloc_object( &token_ops );
424 list_init( &token->privileges );
425 list_init( &token->groups );
426 token->primary = primary;
427 token->default_dacl = NULL;
430 token->user = memdup( user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
433 release_object( token );
438 for (i = 0; i < group_count; i++)
440 size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] );
441 struct group *group = mem_alloc( size );
445 release_object( token );
448 memcpy( &group->sid, groups[i].Sid, FIELD_OFFSET( SID, SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] ) );
449 group->enabled = TRUE;
451 group->logon = FALSE;
452 group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) ? TRUE : FALSE;
453 group->owner = FALSE;
454 group->resource = FALSE;
455 group->deny_only = FALSE;
456 list_add_tail( &token->groups, &group->entry );
459 /* copy privileges */
460 for (i = 0; i < priv_count; i++)
462 /* note: we don't check uniqueness: the caller must make sure
463 * privs doesn't contain any duplicate luids */
464 if (!privilege_add( token, &privs[i].Luid,
465 privs[i].Attributes & SE_PRIVILEGE_ENABLED ))
467 release_object( token );
474 token->default_dacl = memdup( default_dacl, default_dacl->AclSize );
475 if (!token->default_dacl)
477 release_object( token );
482 token->source = source;
487 static ACL *create_default_dacl( const SID *user )
489 ACCESS_ALLOWED_ACE *aaa;
492 size_t default_dacl_size = sizeof(ACL) +
493 2*(sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
494 sizeof(local_system_sid) +
495 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
497 default_dacl = mem_alloc( default_dacl_size );
498 if (!default_dacl) return NULL;
500 default_dacl->AclRevision = MAX_ACL_REVISION;
501 default_dacl->Sbz1 = 0;
502 default_dacl->AclSize = default_dacl_size;
503 default_dacl->AceCount = 2;
504 default_dacl->Sbz2 = 0;
506 /* GENERIC_ALL for Local System */
507 aaa = (ACCESS_ALLOWED_ACE *)(default_dacl + 1);
508 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
509 aaa->Header.AceFlags = 0;
510 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
511 sizeof(local_system_sid);
512 aaa->Mask = GENERIC_ALL;
513 sid = (SID *)&aaa->SidStart;
514 memcpy( sid, &local_system_sid, sizeof(local_system_sid) );
516 /* GENERIC_ALL for specified user */
517 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
518 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
519 aaa->Header.AceFlags = 0;
520 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
521 FIELD_OFFSET( SID, SubAuthority[user->SubAuthorityCount] );
522 aaa->Mask = GENERIC_ALL;
523 sid = (SID *)&aaa->SidStart;
524 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
531 SID_IDENTIFIER_AUTHORITY idauth;
533 unsigned int subauth[MAX_SUBAUTH_COUNT];
536 struct token *token_create_admin( void )
538 struct token *token = NULL;
539 static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
540 static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
541 static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
542 PSID alias_admins_sid;
543 PSID alias_users_sid;
544 /* note: should be the owner specified in the token */
545 ACL *default_dacl = create_default_dacl( &interactive_sid );
547 alias_admins_sid = security_sid_alloc( &nt_authority, sizeof(alias_admins_subauth)/sizeof(alias_admins_subauth[0]),
548 alias_admins_subauth );
549 alias_users_sid = security_sid_alloc( &nt_authority, sizeof(alias_users_subauth)/sizeof(alias_users_subauth[0]),
550 alias_users_subauth );
552 if (alias_admins_sid && alias_users_sid && default_dacl)
554 const LUID_AND_ATTRIBUTES admin_privs[] =
556 { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
557 { SeSecurityPrivilege , 0 },
558 { SeBackupPrivilege , 0 },
559 { SeRestorePrivilege , 0 },
560 { SeSystemtimePrivilege , 0 },
561 { SeShutdownPrivilege , 0 },
562 { SeRemoteShutdownPrivilege , 0 },
563 { SeTakeOwnershipPrivilege , 0 },
564 { SeDebugPrivilege , 0 },
565 { SeSystemEnvironmentPrivilege , 0 },
566 { SeSystemProfilePrivilege , 0 },
567 { SeProfileSingleProcessPrivilege, 0 },
568 { SeIncreaseBasePriorityPrivilege, 0 },
569 { SeLoadDriverPrivilege , 0 },
570 { SeCreatePagefilePrivilege , 0 },
571 { SeIncreaseQuotaPrivilege , 0 },
572 { SeUndockPrivilege , 0 },
573 { SeManageVolumePrivilege , 0 },
574 { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED },
575 { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED },
577 /* note: we don't include non-builtin groups here for the user -
578 * telling us these is the job of a client-side program */
579 const SID_AND_ATTRIBUTES admin_groups[] =
581 { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
582 { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
583 { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
584 { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
585 { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
586 { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
588 static const TOKEN_SOURCE admin_source = {"SeMgr", {0, 0}};
589 /* note: we just set the user sid to be the interactive builtin sid -
590 * we should really translate the UNIX user id to a sid */
591 token = create_token( TRUE, &interactive_sid,
592 admin_groups, sizeof(admin_groups)/sizeof(admin_groups[0]),
593 admin_privs, sizeof(admin_privs)/sizeof(admin_privs[0]),
594 default_dacl, admin_source );
597 free( alias_admins_sid );
598 free( alias_users_sid );
599 free( default_dacl );
604 static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only )
606 struct privilege *privilege;
607 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
609 if (is_equal_luid( luid, &privilege->luid ))
611 if (enabled_only && !privilege->enabled)
619 static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs,
620 unsigned int count, LUID_AND_ATTRIBUTES *mod_privs,
621 unsigned int mod_privs_count )
623 unsigned int i, modified_count = 0;
625 for (i = 0; i < count; i++)
627 struct privilege *privilege =
628 token_find_privilege( token, &privs[i].Luid, FALSE );
631 set_error( STATUS_NOT_ALL_ASSIGNED );
635 if (privs[i].Attributes & SE_PRIVILEGE_REMOVE)
636 privilege_remove( privilege );
639 /* save previous state for caller */
642 luid_and_attr_from_privilege(mod_privs, privilege);
648 if (privs[i].Attributes & SE_PRIVILEGE_ENABLED)
649 privilege->enabled = TRUE;
651 privilege->enabled = FALSE;
654 return modified_count;
657 static void token_disable_privileges( struct token *token )
659 struct privilege *privilege;
660 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
661 privilege->enabled = FALSE;
664 int token_check_privileges( struct token *token, int all_required,
665 const LUID_AND_ATTRIBUTES *reqprivs,
666 unsigned int count, LUID_AND_ATTRIBUTES *usedprivs)
668 unsigned int i, enabled_count = 0;
670 for (i = 0; i < count; i++)
672 struct privilege *privilege =
673 token_find_privilege( token, &reqprivs[i].Luid, TRUE );
676 usedprivs[i] = reqprivs[i];
678 if (privilege && privilege->enabled)
682 usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS;
687 return (enabled_count == count);
689 return (enabled_count > 0);
692 static int token_sid_present( struct token *token, const SID *sid, int deny )
696 if (security_equal_sid( token->user, sid )) return TRUE;
698 LIST_FOR_EACH_ENTRY( group, &token->groups, struct group, entry )
700 if (!group->enabled) continue;
701 if (group->deny_only && !deny) continue;
703 if (security_equal_sid( &group->sid, sid )) return TRUE;
709 /* Checks access to a security descriptor. 'sd' must have been validated by
710 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
711 * the reason. 'status' parameter will indicate if access is granted or denied.
713 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
715 static unsigned int token_access_check( struct token *token,
716 const struct security_descriptor *sd,
717 unsigned int desired_access,
718 LUID_AND_ATTRIBUTES *privs,
719 unsigned int *priv_count,
720 const GENERIC_MAPPING *mapping,
721 unsigned int *granted_access,
722 unsigned int *status )
724 unsigned int current_access = 0;
725 unsigned int denied_access = 0;
729 const ACE_HEADER *ace;
732 /* assume no access rights */
735 /* fail if desired_access contains generic rights */
736 if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
739 return STATUS_GENERIC_NOT_MAPPED;
742 dacl = sd_get_dacl( sd, &dacl_present );
743 owner = sd_get_owner( sd );
744 if (!owner || !sd_get_group( sd ))
747 return STATUS_INVALID_SECURITY_DESCR;
750 /* 1: Grant desired access if the object is unprotected */
754 *granted_access = desired_access;
755 return *status = STATUS_SUCCESS;
760 *status = STATUS_ACCESS_DENIED;
761 return STATUS_SUCCESS;
764 /* 2: Check if caller wants access to system security part. Note: access
765 * is only granted if specifically asked for */
766 if (desired_access & ACCESS_SYSTEM_SECURITY)
768 const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 };
769 LUID_AND_ATTRIBUTES retpriv = security_priv;
770 if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
774 /* assumes that there will only be one privilege to return */
775 if (*priv_count >= 1)
783 return STATUS_BUFFER_TOO_SMALL;
786 current_access |= ACCESS_SYSTEM_SECURITY;
787 if (desired_access == current_access)
789 *granted_access = current_access;
790 return *status = STATUS_SUCCESS;
796 *status = STATUS_PRIVILEGE_NOT_HELD;
797 return STATUS_SUCCESS;
800 else if (priv_count) *priv_count = 0;
802 /* 3: Check whether the token is the owner */
803 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
804 * checked when a "set owner" call is made, overriding the access rights
805 * determined here. */
806 if (token_sid_present( token, owner, FALSE ))
808 current_access |= (READ_CONTROL | WRITE_DAC);
809 if (desired_access == current_access)
811 *granted_access = current_access;
812 return *status = STATUS_SUCCESS;
816 /* 4: Grant rights according to the DACL */
817 ace = (const ACE_HEADER *)(dacl + 1);
818 for (i = 0; i < dacl->AceCount; i++)
820 const ACCESS_ALLOWED_ACE *aa_ace;
821 const ACCESS_DENIED_ACE *ad_ace;
823 switch (ace->AceType)
825 case ACCESS_DENIED_ACE_TYPE:
826 ad_ace = (const ACCESS_DENIED_ACE *)ace;
827 sid = (const SID *)&ad_ace->SidStart;
828 if (token_sid_present( token, sid, TRUE ))
830 unsigned int access = ad_ace->Mask;
831 map_generic_mask(&access, mapping);
832 if (desired_access & MAXIMUM_ALLOWED)
833 denied_access |= access;
836 denied_access |= (access & ~current_access);
837 if (desired_access & access) goto done;
841 case ACCESS_ALLOWED_ACE_TYPE:
842 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
843 sid = (const SID *)&aa_ace->SidStart;
844 if (token_sid_present( token, sid, FALSE ))
846 unsigned int access = aa_ace->Mask;
847 map_generic_mask(&access, mapping);
848 if (desired_access & MAXIMUM_ALLOWED)
849 current_access |= access;
851 current_access |= (access & ~denied_access);
856 /* don't bother carrying on checking if we've already got all of
858 if (desired_access == *granted_access)
861 ace = ace_next( ace );
865 if (desired_access & MAXIMUM_ALLOWED)
866 *granted_access = current_access & ~denied_access;
868 if ((current_access & desired_access) == desired_access)
869 *granted_access = current_access & desired_access;
873 *status = *granted_access ? STATUS_SUCCESS : STATUS_ACCESS_DENIED;
874 return STATUS_SUCCESS;
877 const ACL *token_get_default_dacl( struct token *token )
879 return token->default_dacl;
882 /* open a security token */
883 DECL_HANDLER(open_token)
885 if (req->flags & OPEN_TOKEN_THREAD)
887 struct thread *thread = get_thread_from_handle( req->handle, 0 );
891 reply->token = alloc_handle( current->process, thread->token, req->access,
894 set_error(STATUS_NO_TOKEN);
895 release_object( thread );
900 struct process *process = get_process_from_handle( req->handle, 0 );
904 reply->token = alloc_handle( current->process, process->token, req->access,
907 set_error(STATUS_NO_TOKEN);
908 release_object( process );
913 /* adjust the privileges held by a token */
914 DECL_HANDLER(adjust_token_privileges)
917 unsigned int access = TOKEN_ADJUST_PRIVILEGES;
919 if (req->get_modified_state) access |= TOKEN_QUERY;
921 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
922 access, &token_ops )))
924 const LUID_AND_ATTRIBUTES *privs = get_req_data();
925 LUID_AND_ATTRIBUTES *modified_privs = NULL;
926 unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
927 unsigned int modified_priv_count = 0;
929 if (req->get_modified_state && !req->disable_all)
932 /* count modified privs */
933 for (i = 0; i < priv_count; i++)
935 struct privilege *privilege =
936 token_find_privilege( token, &privs[i].Luid, FALSE );
937 if (privilege && req->get_modified_state)
938 modified_priv_count++;
940 reply->len = modified_priv_count;
941 modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
942 if (modified_priv_count)
943 modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
945 reply->len = modified_priv_count * sizeof(*modified_privs);
947 if (req->disable_all)
948 token_disable_privileges( token );
950 modified_priv_count = token_adjust_privileges( token, privs,
951 priv_count, modified_privs, modified_priv_count );
953 release_object( token );
957 /* retrieves the list of privileges that may be held be the token */
958 DECL_HANDLER(get_token_privileges)
962 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
967 LUID_AND_ATTRIBUTES *privs;
968 struct privilege *privilege;
970 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
973 reply->len = priv_count * sizeof(*privs);
974 if (reply->len <= get_reply_max_size())
976 privs = set_reply_data_size( priv_count * sizeof(*privs) );
980 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
982 luid_and_attr_from_privilege( &privs[i], privilege );
988 set_error(STATUS_BUFFER_TOO_SMALL);
990 release_object( token );
994 /* creates a duplicate of the token */
995 DECL_HANDLER(duplicate_token)
997 struct token *src_token;
998 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
1002 /* FIXME: use req->impersonation_level */
1003 struct token *token = create_token( req->primary, src_token->user,
1005 src_token->default_dacl,
1006 src_token->source );
1009 struct privilege *privilege;
1010 struct group *group;
1011 unsigned int access;
1014 LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct group, entry )
1016 size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[group->sid.SubAuthorityCount] );
1017 struct group *newgroup = mem_alloc( size );
1020 release_object( token );
1021 release_object( src_token );
1024 memcpy( newgroup, group, size );
1025 list_add_tail( &token->groups, &newgroup->entry );
1028 /* copy privileges */
1029 LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
1030 privilege_add( token, &privilege->luid, privilege->enabled );
1032 access = req->access;
1033 if (access & MAXIMUM_ALLOWED) access = TOKEN_ALL_ACCESS; /* FIXME: needs general solution */
1034 reply->new_handle = alloc_handle( current->process, token, access, req->attributes);
1035 release_object( token );
1037 release_object( src_token );
1041 /* checks the specified privileges are held by the token */
1042 DECL_HANDLER(check_token_privileges)
1044 struct token *token;
1046 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1050 unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
1051 if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES))
1053 LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
1054 reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
1057 set_error( STATUS_BUFFER_OVERFLOW );
1058 release_object( token );
1062 /* checks that a user represented by a token is allowed to access an object
1063 * represented by a security descriptor */
1064 DECL_HANDLER(access_check)
1066 data_size_t sd_size = get_req_data_size();
1067 const struct security_descriptor *sd = get_req_data();
1068 struct token *token;
1070 if (!sd_is_valid( sd, sd_size ))
1072 set_error( STATUS_ACCESS_VIOLATION );
1076 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1080 GENERIC_MAPPING mapping;
1081 unsigned int status;
1082 LUID_AND_ATTRIBUTES priv;
1083 unsigned int priv_count = 1;
1085 memset(&priv, 0, sizeof(priv));
1087 /* only impersonation tokens may be used with this function */
1090 set_error( STATUS_NO_IMPERSONATION_TOKEN );
1091 release_object( token );
1095 mapping.GenericRead = req->mapping_read;
1096 mapping.GenericWrite = req->mapping_write;
1097 mapping.GenericExecute = req->mapping_execute;
1098 mapping.GenericAll = req->mapping_all;
1100 status = token_access_check(
1101 token, sd, req->desired_access, &priv, &priv_count, &mapping,
1102 &reply->access_granted, &reply->access_status );
1104 reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES);
1106 if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
1108 LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) );
1109 memcpy( privs, &priv, sizeof(priv) );
1112 set_error( status );
1113 release_object( token );
1117 /* retrieves the SID of the user that the token represents */
1118 DECL_HANDLER(get_token_user)
1120 struct token *token;
1122 reply->user_len = 0;
1124 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1128 const SID *user = token->user;
1130 reply->user_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
1131 if (reply->user_len <= get_reply_max_size())
1133 SID *user_reply = set_reply_data_size( reply->user_len );
1135 memcpy( user_reply, user, reply->user_len );
1137 else set_error( STATUS_BUFFER_TOO_SMALL );
1139 release_object( token );
1143 /* retrieves the groups that the user represented by the token belongs to */
1144 DECL_HANDLER(get_token_groups)
1146 struct token *token;
1148 reply->user_len = 0;
1150 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1154 size_t size_needed = sizeof(struct token_groups);
1155 unsigned int group_count = 0;
1156 const struct group *group;
1158 LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
1161 size_needed += FIELD_OFFSET(SID, SubAuthority[group->sid.SubAuthorityCount]);
1163 size_needed += sizeof(unsigned int) * group_count;
1165 reply->user_len = size_needed;
1167 if (size_needed <= get_reply_max_size())
1169 struct token_groups *tg = set_reply_data_size( size_needed );
1172 unsigned int *attr_ptr = (unsigned int *)(tg + 1);
1173 SID *sid_ptr = (SID *)(attr_ptr + group_count);
1175 tg->count = group_count;
1177 LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
1181 if (group->mandatory) *attr_ptr |= SE_GROUP_MANDATORY;
1182 if (group->def) *attr_ptr |= SE_GROUP_ENABLED_BY_DEFAULT;
1183 if (group->enabled) *attr_ptr |= SE_GROUP_ENABLED;
1184 if (group->owner) *attr_ptr |= SE_GROUP_OWNER;
1185 if (group->deny_only) *attr_ptr |= SE_GROUP_USE_FOR_DENY_ONLY;
1186 if (group->resource) *attr_ptr |= SE_GROUP_RESOURCE;
1188 memcpy(sid_ptr, &group->sid, FIELD_OFFSET(SID, SubAuthority[group->sid.SubAuthorityCount]));
1190 sid_ptr = (SID *)((char *)sid_ptr + FIELD_OFFSET(SID, SubAuthority[group->sid.SubAuthorityCount]));
1195 else set_error( STATUS_BUFFER_TOO_SMALL );
1197 release_object( token );