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
37 #define MAX_SUBAUTH_COUNT 1
39 const LUID SeIncreaseQuotaPrivilege = { 5, 0 };
40 const LUID SeSecurityPrivilege = { 8, 0 };
41 const LUID SeTakeOwnershipPrivilege = { 9, 0 };
42 const LUID SeLoadDriverPrivilege = { 10, 0 };
43 const LUID SeSystemProfilePrivilege = { 11, 0 };
44 const LUID SeSystemtimePrivilege = { 12, 0 };
45 const LUID SeProfileSingleProcessPrivilege = { 13, 0 };
46 const LUID SeIncreaseBasePriorityPrivilege = { 14, 0 };
47 const LUID SeCreatePagefilePrivilege = { 15, 0 };
48 const LUID SeBackupPrivilege = { 17, 0 };
49 const LUID SeRestorePrivilege = { 18, 0 };
50 const LUID SeShutdownPrivilege = { 19, 0 };
51 const LUID SeDebugPrivilege = { 20, 0 };
52 const LUID SeSystemEnvironmentPrivilege = { 22, 0 };
53 const LUID SeChangeNotifyPrivilege = { 23, 0 };
54 const LUID SeRemoteShutdownPrivilege = { 24, 0 };
55 const LUID SeUndockPrivilege = { 25, 0 };
56 const LUID SeManageVolumePrivilege = { 28, 0 };
57 const LUID SeImpersonatePrivilege = { 29, 0 };
58 const LUID SeCreateGlobalPrivilege = { 30, 0 };
60 static const SID world_sid = { SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY }, { SECURITY_WORLD_RID } };
61 static const SID local_sid = { SID_REVISION, 1, { SECURITY_LOCAL_SID_AUTHORITY }, { SECURITY_LOCAL_RID } };
62 static const SID interactive_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_INTERACTIVE_RID } };
63 static const SID authenticated_user_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_AUTHENTICATED_USER_RID } };
64 static const SID local_system_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SYSTEM_RID } };
65 static PSID security_world_sid = (PSID)&world_sid;
66 static PSID security_local_sid = (PSID)&local_sid;
67 static PSID security_interactive_sid = (PSID)&interactive_sid;
68 static PSID security_authenticated_user_sid = (PSID)&authenticated_user_sid;
72 struct object obj; /* object header */
73 struct list privileges; /* privileges available to the token */
74 struct list groups; /* groups that the user of this token belongs to (sid_and_attributes) */
75 SID *user; /* SID of user this token represents */
82 unsigned enabled : 1; /* is the privilege currently enabled? */
83 unsigned def : 1; /* is the privilege enabled by default? */
86 struct sid_and_attributes
89 unsigned enabled : 1; /* is the sid currently enabled? */
90 unsigned def : 1; /* is the sid enabled by default? */
91 unsigned logon : 1; /* is this a logon sid? */
92 unsigned mandatory: 1; /* is this sid always enabled? */
93 unsigned owner : 1; /* can this sid be an owner of an object? */
94 unsigned resource : 1; /* is this a domain-local group? */
95 unsigned deny_only: 1; /* is this a sid that should be use for denying only? */
99 static void token_dump( struct object *obj, int verbose );
100 static void token_destroy( struct object *obj );
102 static const struct object_ops token_ops =
104 sizeof(struct token), /* size */
105 token_dump, /* dump */
106 no_add_queue, /* add_queue */
107 NULL, /* remove_queue */
109 NULL, /* satisfied */
110 no_signal, /* signal */
111 no_get_fd, /* get_fd */
112 no_close_handle, /* close_handle */
113 token_destroy /* destroy */
117 static void token_dump( struct object *obj, int verbose )
119 fprintf( stderr, "Security token\n" );
120 /* FIXME: dump token members */
123 static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] )
126 SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) );
127 if (!sid) return NULL;
128 sid->Revision = SID_REVISION;
129 sid->SubAuthorityCount = subauthcount;
130 sid->IdentifierAuthority = *idauthority;
131 for (i = 0; i < subauthcount; i++)
132 sid->SubAuthority[i] = subauth[i];
136 static inline int security_equal_sid( const SID *sid1, const SID *sid2 )
138 return ((sid1->SubAuthorityCount == sid2->SubAuthorityCount) &&
139 !memcmp( sid1, sid2, FIELD_OFFSET(SID, SubAuthority[sid1->SubAuthorityCount]) ));
142 static const ACE_HEADER *ace_next( const ACE_HEADER *ace )
144 return (const ACE_HEADER *)((const char *)ace + ace->AceSize);
147 static int acl_is_valid( const ACL *acl, size_t size )
150 const ACE_HEADER *ace;
152 if (size < sizeof(ACL))
155 size = min(size, MAX_ACL_LEN);
159 ace = (const ACE_HEADER *)(acl + 1);
160 for (i = 0; i < acl->AceCount; i++)
164 if (size < sizeof(ACE_HEADER))
166 if (size < ace->AceSize)
168 size -= ace->AceSize;
169 switch (ace->AceType)
171 case ACCESS_DENIED_ACE_TYPE:
172 sid = (const SID *)&((const ACCESS_DENIED_ACE *)ace)->SidStart;
174 case ACCESS_ALLOWED_ACE_TYPE:
175 sid = (const SID *)&((const ACCESS_ALLOWED_ACE *)ace)->SidStart;
177 case SYSTEM_AUDIT_ACE_TYPE:
178 sid = (const SID *)&((const SYSTEM_AUDIT_ACE *)ace)->SidStart;
180 case SYSTEM_ALARM_ACE_TYPE:
181 sid = (const SID *)&((const SYSTEM_ALARM_ACE *)ace)->SidStart;
186 if (size < sizeof(SID) ||
187 size < FIELD_OFFSET(SID, SubAuthority[sid->SubAuthorityCount]))
189 ace = ace_next( ace );
194 /* gets the discretionary access control list from a security descriptor */
195 static inline const ACL *sd_get_dacl( const struct security_descriptor *sd, int *present )
197 *present = (sd->control & SE_DACL_PRESENT ? TRUE : FALSE);
200 return (const ACL *)((const char *)(sd + 1) +
201 sd->owner_len + sd->group_len + sd->sacl_len);
206 /* gets the system access control list from a security descriptor */
207 static inline const ACL *sd_get_sacl( const struct security_descriptor *sd, int *present )
209 *present = (sd->control & SE_SACL_PRESENT ? TRUE : FALSE);
212 return (const ACL *)((const char *)(sd + 1) +
213 sd->owner_len + sd->group_len);
218 /* gets the owner from a security descriptor */
219 static inline const SID *sd_get_owner( const struct security_descriptor *sd )
222 return (const SID *)(sd + 1);
227 /* gets the primary group from a security descriptor */
228 static inline const SID *sd_get_group( const struct security_descriptor *sd )
231 return (const SID *)((const char *)(sd + 1) + sd->owner_len);
236 /* checks whether all members of a security descriptor fit inside the size
237 * of memory specified */
238 static int sd_is_valid( const struct security_descriptor *sd, size_t size )
240 size_t offset = sizeof(struct security_descriptor);
250 if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
251 (offset + sd->owner_len > size))
253 owner = sd_get_owner( sd );
256 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]);
257 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
260 offset += sd->owner_len;
262 if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
263 (offset + sd->group_len > size))
265 group = sd_get_group( sd );
268 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
269 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
272 offset += sd->group_len;
274 if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
276 sacl = sd_get_sacl( sd, &dummy );
277 if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
279 offset += sd->sacl_len;
281 if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
283 dacl = sd_get_dacl( sd, &dummy );
284 if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
286 offset += sd->dacl_len;
291 /* maps from generic rights to specific rights as given by a mapping */
292 static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping)
294 if (*mask & GENERIC_READ) *mask |= mapping->GenericRead;
295 if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite;
296 if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute;
297 if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll;
301 static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 )
303 return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart);
306 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in)
308 out->Luid = in->luid;
310 (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
311 (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
314 static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled )
316 struct privilege *privilege = mem_alloc( sizeof(*privilege) );
319 privilege->luid = *luid;
320 privilege->def = privilege->enabled = (enabled != 0);
321 list_add_tail( &token->privileges, &privilege->entry );
326 static inline void privilege_remove( struct privilege *privilege )
328 list_remove( &privilege->entry );
332 static void token_destroy( struct object *obj )
335 struct list *cursor, *cursor_next;
337 assert( obj->ops == &token_ops );
338 token = (struct token *)obj;
342 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
344 struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
345 privilege_remove( privilege );
348 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
350 struct sid_and_attributes *group = LIST_ENTRY( cursor, struct sid_and_attributes, entry );
351 list_remove( &group->entry );
356 static struct token *create_token( const SID *user,
357 const SID_AND_ATTRIBUTES *groups, unsigned int group_count,
358 const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count )
360 struct token *token = alloc_object( &token_ops );
364 list_init( &token->privileges );
365 list_init( &token->groups );
367 token->user = memdup( user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
370 release_object( token );
375 for (i = 0; i < group_count; i++)
377 size_t size = FIELD_OFFSET( struct sid_and_attributes, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] );
378 struct sid_and_attributes *group = mem_alloc( size );
379 memcpy( &group->sid, groups[i].Sid, FIELD_OFFSET( SID, SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] ) );
380 group->enabled = TRUE;
382 group->logon = FALSE;
383 group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) ? TRUE : FALSE;
384 group->owner = FALSE;
385 group->resource = FALSE;
386 group->deny_only = FALSE;
387 list_add_tail( &token->groups, &group->entry );
390 /* copy privileges */
391 for (i = 0; i < priv_count; i++)
393 /* note: we don't check uniqueness: the caller must make sure
394 * privs doesn't contain any duplicate luids */
395 if (!privilege_add( token, &privs[i].Luid,
396 privs[i].Attributes & SE_PRIVILEGE_ENABLED ))
398 release_object( token );
408 SID_IDENTIFIER_AUTHORITY idauth;
410 unsigned int subauth[MAX_SUBAUTH_COUNT];
413 struct token *token_create_admin( void )
415 struct token *token = NULL;
416 static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
417 static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
418 static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
419 PSID alias_admins_sid;
420 PSID alias_users_sid;
422 alias_admins_sid = security_sid_alloc( &nt_authority, sizeof(alias_admins_subauth)/sizeof(alias_admins_subauth[0]),
423 alias_admins_subauth );
424 alias_users_sid = security_sid_alloc( &nt_authority, sizeof(alias_users_subauth)/sizeof(alias_users_subauth[0]),
425 alias_users_subauth );
427 if (alias_admins_sid && alias_users_sid)
429 const LUID_AND_ATTRIBUTES admin_privs[] =
431 { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
432 { SeSecurityPrivilege , 0 },
433 { SeBackupPrivilege , 0 },
434 { SeRestorePrivilege , 0 },
435 { SeSystemtimePrivilege , 0 },
436 { SeShutdownPrivilege , 0 },
437 { SeRemoteShutdownPrivilege , 0 },
438 { SeTakeOwnershipPrivilege , 0 },
439 { SeDebugPrivilege , 0 },
440 { SeSystemEnvironmentPrivilege , 0 },
441 { SeSystemProfilePrivilege , 0 },
442 { SeProfileSingleProcessPrivilege, 0 },
443 { SeIncreaseBasePriorityPrivilege, 0 },
444 { SeLoadDriverPrivilege , 0 },
445 { SeCreatePagefilePrivilege , 0 },
446 { SeIncreaseQuotaPrivilege , 0 },
447 { SeUndockPrivilege , 0 },
448 { SeManageVolumePrivilege , 0 },
449 { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED },
450 { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED },
452 /* note: we don't include non-builtin groups here for the user -
453 * telling us these is the job of a client-side program */
454 const SID_AND_ATTRIBUTES admin_groups[] =
456 { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
457 { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
458 { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
459 { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
460 { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
461 { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
463 /* note: we just set the user sid to be the local system builtin sid -
464 * telling us what this should be is the job of a client-side program */
465 token = create_token( &local_system_sid,
466 admin_groups, sizeof(admin_groups)/sizeof(admin_groups[0]),
467 admin_privs, sizeof(admin_privs)/sizeof(admin_privs[0]) );
470 if (alias_admins_sid)
471 free( alias_admins_sid );
473 free( alias_users_sid );
478 static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only)
480 struct privilege *privilege;
481 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
483 if (is_equal_luid( luid, &privilege->luid ))
485 if (enabled_only && !privilege->enabled)
493 static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs,
494 unsigned int count, LUID_AND_ATTRIBUTES *mod_privs,
495 unsigned int mod_privs_count)
498 unsigned int modified_count = 0;
500 for (i = 0; i < count; i++)
502 struct privilege *privilege =
503 token_find_privilege( token, &privs[i].Luid, FALSE );
506 set_error( STATUS_NOT_ALL_ASSIGNED );
510 if (privs[i].Attributes & SE_PRIVILEGE_REMOVE)
511 privilege_remove( privilege );
514 /* save previous state for caller */
517 luid_and_attr_from_privilege(mod_privs, privilege);
523 if (privs[i].Attributes & SE_PRIVILEGE_ENABLED)
524 privilege->enabled = TRUE;
526 privilege->enabled = FALSE;
529 return modified_count;
532 static void token_disable_privileges( struct token *token )
534 struct privilege *privilege;
535 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
536 privilege->enabled = FALSE;
539 int token_check_privileges( struct token *token, int all_required,
540 const LUID_AND_ATTRIBUTES *reqprivs,
541 unsigned int count, LUID_AND_ATTRIBUTES *usedprivs)
544 unsigned int enabled_count = 0;
546 for (i = 0; i < count; i++)
548 struct privilege *privilege =
549 token_find_privilege( token, &reqprivs[i].Luid, TRUE );
552 usedprivs[i] = reqprivs[i];
554 if (privilege && privilege->enabled)
558 usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS;
563 return (enabled_count == count);
565 return (enabled_count > 0);
568 static int token_sid_present( struct token *token, const SID *sid, int deny )
570 struct sid_and_attributes *group;
572 if (security_equal_sid( token->user, sid )) return TRUE;
574 LIST_FOR_EACH_ENTRY( group, &token->groups, struct sid_and_attributes, entry )
576 if (!group->enabled) continue;
577 if (group->deny_only && !deny) continue;
579 if (security_equal_sid( &group->sid, sid )) return TRUE;
585 /* checks access to a security descriptor. sd must have been validated by caller.
586 * it returns STATUS_SUCCESS if access was granted to the object, or an error
587 * status code if not, giving the reason. errors not relating to giving access
588 * to the object are returned in the status parameter. granted_access and
589 * status always have a valid value stored in them on return. */
590 static unsigned int token_access_check( struct token *token,
591 const struct security_descriptor *sd,
592 unsigned int desired_access,
593 LUID_AND_ATTRIBUTES *privs,
594 unsigned int *priv_count,
595 const GENERIC_MAPPING *mapping,
596 unsigned int *granted_access,
597 unsigned int *status )
599 unsigned int current_access = 0;
600 unsigned int denied_access = 0;
604 const ACE_HEADER *ace;
607 /* assume success, but no access rights */
608 *status = STATUS_SUCCESS;
611 /* fail if desired_access contains generic rights */
612 if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
615 *status = STATUS_GENERIC_NOT_MAPPED;
616 return STATUS_ACCESS_DENIED;
619 dacl = sd_get_dacl( sd, &dacl_present );
620 owner = sd_get_owner( sd );
621 if (!owner || !sd_get_group( sd ))
624 *status = STATUS_INVALID_SECURITY_DESCR;
625 return STATUS_ACCESS_DENIED;
628 /* 1: Grant desired access if the object is unprotected */
632 *granted_access = desired_access;
633 return STATUS_SUCCESS;
638 return STATUS_ACCESS_DENIED;
641 /* 2: Check if caller wants access to system security part. Note: access
642 * is only granted if specifically asked for */
643 if (desired_access & ACCESS_SYSTEM_SECURITY)
645 const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 };
646 LUID_AND_ATTRIBUTES retpriv = security_priv;
647 if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
651 /* assumes that there will only be one privilege to return */
652 if (*priv_count >= 1)
660 return STATUS_BUFFER_TOO_SMALL;
663 current_access |= ACCESS_SYSTEM_SECURITY;
664 if (desired_access == current_access)
666 *granted_access = current_access;
667 return STATUS_SUCCESS;
673 return STATUS_PRIVILEGE_NOT_HELD;
676 else if (priv_count) *priv_count = 0;
678 /* 3: Check whether the token is the owner */
679 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
680 * checked when a "set owner" call is made, overriding the access rights
681 * determined here. */
682 if (token_sid_present( token, owner, FALSE ))
684 current_access |= (READ_CONTROL | WRITE_DAC);
685 if (desired_access == current_access)
687 *granted_access = current_access;
688 return STATUS_SUCCESS;
692 /* 4: Grant rights according to the DACL */
693 ace = (const ACE_HEADER *)(dacl + 1);
694 for (i = 0; i < dacl->AceCount; i++)
696 const ACCESS_ALLOWED_ACE *aa_ace;
697 const ACCESS_DENIED_ACE *ad_ace;
699 switch (ace->AceType)
701 case ACCESS_DENIED_ACE_TYPE:
702 ad_ace = (const ACCESS_DENIED_ACE *)ace;
703 sid = (const SID *)&ad_ace->SidStart;
704 if (token_sid_present( token, sid, TRUE ))
706 unsigned int access = ad_ace->Mask;
707 map_generic_mask(&access, mapping);
708 if (desired_access & MAXIMUM_ALLOWED)
709 denied_access |= access;
712 denied_access |= (access & ~current_access);
713 if (desired_access & access)
716 return STATUS_SUCCESS;
721 case ACCESS_ALLOWED_ACE_TYPE:
722 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
723 sid = (const SID *)&aa_ace->SidStart;
724 if (token_sid_present( token, sid, FALSE ))
726 unsigned int access = aa_ace->Mask;
727 map_generic_mask(&access, mapping);
728 if (desired_access & MAXIMUM_ALLOWED)
729 current_access |= access;
731 current_access |= (access & ~denied_access);
736 /* don't bother carrying on checking if we've already got all of
738 if (desired_access == *granted_access)
741 ace = ace_next( ace );
744 if (desired_access & MAXIMUM_ALLOWED)
746 *granted_access = current_access & ~denied_access;
748 return STATUS_SUCCESS;
750 return STATUS_ACCESS_DENIED;
754 if ((current_access & desired_access) == desired_access)
756 *granted_access = current_access & desired_access;
757 return STATUS_SUCCESS;
760 return STATUS_ACCESS_DENIED;
764 /* open a security token */
765 DECL_HANDLER(open_token)
767 if (req->flags & OPEN_TOKEN_THREAD)
769 struct thread *thread = get_thread_from_handle( req->handle, 0 );
773 reply->token = alloc_handle( current->process, thread->token, TOKEN_ALL_ACCESS, 0);
775 set_error(STATUS_NO_TOKEN);
776 release_object( thread );
781 struct process *process = get_process_from_handle( req->handle, 0 );
785 reply->token = alloc_handle( current->process, process->token, TOKEN_ALL_ACCESS, 0);
787 set_error(STATUS_NO_TOKEN);
788 release_object( process );
793 /* adjust the privileges held by a token */
794 DECL_HANDLER(adjust_token_privileges)
797 unsigned int access = TOKEN_ADJUST_PRIVILEGES;
799 if (req->get_modified_state) access |= TOKEN_QUERY;
801 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
802 access, &token_ops )))
804 const LUID_AND_ATTRIBUTES *privs = get_req_data();
805 LUID_AND_ATTRIBUTES *modified_privs = NULL;
806 unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
807 unsigned int modified_priv_count = 0;
809 if (req->get_modified_state && !req->disable_all)
812 /* count modified privs */
813 for (i = 0; i < priv_count; i++)
815 struct privilege *privilege =
816 token_find_privilege( token, &privs[i].Luid, FALSE );
817 if (privilege && req->get_modified_state)
818 modified_priv_count++;
820 reply->len = modified_priv_count;
821 modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
822 if (modified_priv_count)
823 modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
825 reply->len = modified_priv_count * sizeof(*modified_privs);
827 if (req->disable_all)
828 token_disable_privileges( token );
830 modified_priv_count = token_adjust_privileges( token, privs,
831 priv_count, modified_privs, modified_priv_count );
833 release_object( token );
837 /* retrieves the list of privileges that may be held be the token */
838 DECL_HANDLER(get_token_privileges)
842 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
847 LUID_AND_ATTRIBUTES *privs;
848 struct privilege *privilege;
850 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
853 reply->len = priv_count * sizeof(*privs);
854 if (reply->len <= get_reply_max_size())
856 privs = set_reply_data_size( priv_count * sizeof(*privs) );
860 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
862 luid_and_attr_from_privilege( &privs[i], privilege );
868 set_error(STATUS_BUFFER_TOO_SMALL);
870 release_object( token );
874 /* creates a duplicate of the token */
875 DECL_HANDLER(duplicate_token)
877 struct token *src_token;
878 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
882 /* FIXME: use req->primary and req->impersonation_level */
883 struct token *token = create_token( src_token->user, NULL, 0, NULL, 0 );
886 struct privilege *privilege;
889 /* FIXME: copy groups */
891 LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
892 privilege_add( token, &privilege->luid, privilege->enabled );
894 access = req->access;
895 if (access & MAXIMUM_ALLOWED) access = TOKEN_ALL_ACCESS; /* FIXME: needs general solution */
896 reply->new_handle = alloc_handle( current->process, token, access, req->inherit);
897 release_object( token );
899 release_object( src_token );
903 /* checks the specified privileges are held by the token */
904 DECL_HANDLER(check_token_privileges)
908 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
912 unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
913 if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES))
915 LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
916 reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
919 set_error( STATUS_BUFFER_OVERFLOW );
920 release_object( token );
924 /* checks that a user represented by a token is allowed to access an object
925 * represented by a security descriptor */
926 DECL_HANDLER(access_check)
928 size_t sd_size = get_req_data_size();
929 const struct security_descriptor *sd = get_req_data();
932 if (!sd_is_valid( sd, sd_size ))
934 set_error( STATUS_ACCESS_VIOLATION );
938 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
942 GENERIC_MAPPING mapping;
944 LUID_AND_ATTRIBUTES priv;
945 unsigned int priv_count = 1;
947 memset(&priv, 0, sizeof(priv));
949 /* FIXME: check token is an impersonation token, if not return
950 * STATUS_NO_IMPERSONATION_TOKEN */
952 mapping.GenericRead = req->mapping_read;
953 mapping.GenericWrite = req->mapping_write;
954 mapping.GenericExecute = req->mapping_execute;
955 mapping.GenericAll = req->mapping_all;
957 reply->access_status = token_access_check(
958 token, sd, req->desired_access, &priv, &priv_count, &mapping,
959 &reply->access_granted, &status );
961 reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES);
963 if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
965 LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) );
966 memcpy( privs, &priv, sizeof(priv) );
969 if (status != STATUS_SUCCESS)
972 release_object( token );