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 token_destroy /* destroy */
116 static void token_dump( struct object *obj, int verbose )
118 fprintf( stderr, "Security token\n" );
119 /* FIXME: dump token members */
122 static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] )
125 SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) );
126 if (!sid) return NULL;
127 sid->Revision = SID_REVISION;
128 sid->SubAuthorityCount = subauthcount;
129 sid->IdentifierAuthority = *idauthority;
130 for (i = 0; i < subauthcount; i++)
131 sid->SubAuthority[i] = subauth[i];
135 static inline int security_equal_sid( const SID *sid1, const SID *sid2 )
137 return ((sid1->SubAuthorityCount == sid2->SubAuthorityCount) &&
138 !memcmp( sid1, sid2, FIELD_OFFSET(SID, SubAuthority[sid1->SubAuthorityCount]) ));
141 static const ACE_HEADER *ace_next( const ACE_HEADER *ace )
143 return (const ACE_HEADER *)((const char *)ace + ace->AceSize);
146 static int acl_is_valid( const ACL *acl, size_t size )
149 const ACE_HEADER *ace;
151 if (size < sizeof(ACL))
154 size = min(size, MAX_ACL_LEN);
158 ace = (const ACE_HEADER *)(acl + 1);
159 for (i = 0; i < acl->AceCount; i++)
163 if (size < sizeof(ACE_HEADER))
165 if (size < ace->AceSize)
167 size -= ace->AceSize;
168 switch (ace->AceType)
170 case ACCESS_DENIED_ACE_TYPE:
171 sid = (const SID *)&((const ACCESS_DENIED_ACE *)ace)->SidStart;
173 case ACCESS_ALLOWED_ACE_TYPE:
174 sid = (const SID *)&((const ACCESS_ALLOWED_ACE *)ace)->SidStart;
176 case SYSTEM_AUDIT_ACE_TYPE:
177 sid = (const SID *)&((const SYSTEM_AUDIT_ACE *)ace)->SidStart;
179 case SYSTEM_ALARM_ACE_TYPE:
180 sid = (const SID *)&((const SYSTEM_ALARM_ACE *)ace)->SidStart;
185 if (size < sizeof(SID) ||
186 size < FIELD_OFFSET(SID, SubAuthority[sid->SubAuthorityCount]))
188 ace = ace_next( ace );
193 /* gets the discretionary access control list from a security descriptor */
194 static inline const ACL *sd_get_dacl( const struct security_descriptor *sd, int *present )
196 *present = (sd->control & SE_DACL_PRESENT ? TRUE : FALSE);
199 return (const ACL *)((const char *)(sd + 1) +
200 sd->owner_len + sd->group_len + sd->sacl_len);
205 /* gets the system access control list from a security descriptor */
206 static inline const ACL *sd_get_sacl( const struct security_descriptor *sd, int *present )
208 *present = (sd->control & SE_SACL_PRESENT ? TRUE : FALSE);
211 return (const ACL *)((const char *)(sd + 1) +
212 sd->owner_len + sd->group_len);
217 /* gets the owner from a security descriptor */
218 static inline const SID *sd_get_owner( const struct security_descriptor *sd )
221 return (const SID *)(sd + 1);
226 /* gets the primary group from a security descriptor */
227 static inline const SID *sd_get_group( const struct security_descriptor *sd )
230 return (const SID *)((const char *)(sd + 1) + sd->owner_len);
235 /* checks whether all members of a security descriptor fit inside the size
236 * of memory specified */
237 static int sd_is_valid( const struct security_descriptor *sd, size_t size )
239 size_t offset = sizeof(struct security_descriptor);
249 if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
250 (offset + sd->owner_len > size))
252 owner = sd_get_owner( sd );
255 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]);
256 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
259 offset += sd->owner_len;
261 if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
262 (offset + sd->group_len > size))
264 group = sd_get_group( sd );
267 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
268 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
271 offset += sd->group_len;
273 if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
275 sacl = sd_get_sacl( sd, &dummy );
276 if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
278 offset += sd->sacl_len;
280 if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
282 dacl = sd_get_dacl( sd, &dummy );
283 if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
285 offset += sd->dacl_len;
290 /* maps from generic rights to specific rights as given by a mapping */
291 static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping)
293 if (*mask & GENERIC_READ) *mask |= mapping->GenericRead;
294 if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite;
295 if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute;
296 if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll;
300 static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 )
302 return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart);
305 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in)
307 out->Luid = in->luid;
309 (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
310 (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
313 static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled )
315 struct privilege *privilege = mem_alloc( sizeof(*privilege) );
318 privilege->luid = *luid;
319 privilege->def = privilege->enabled = (enabled != 0);
320 list_add_tail( &token->privileges, &privilege->entry );
325 static inline void privilege_remove( struct privilege *privilege )
327 list_remove( &privilege->entry );
331 static void token_destroy( struct object *obj )
334 struct list *cursor, *cursor_next;
336 assert( obj->ops == &token_ops );
337 token = (struct token *)obj;
341 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
343 struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
344 privilege_remove( privilege );
347 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
349 struct sid_and_attributes *group = LIST_ENTRY( cursor, struct sid_and_attributes, entry );
350 list_remove( &group->entry );
355 static struct token *create_token( const SID *user,
356 const SID_AND_ATTRIBUTES *groups, unsigned int group_count,
357 const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count )
359 struct token *token = alloc_object( &token_ops );
363 list_init( &token->privileges );
364 list_init( &token->groups );
366 token->user = memdup( user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
369 release_object( token );
374 for (i = 0; i < group_count; i++)
376 size_t size = FIELD_OFFSET( struct sid_and_attributes, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] );
377 struct sid_and_attributes *group = mem_alloc( size );
378 memcpy( &group->sid, groups[i].Sid, FIELD_OFFSET( SID, SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] ) );
379 group->enabled = TRUE;
381 group->logon = FALSE;
382 group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) ? TRUE : FALSE;
383 group->owner = FALSE;
384 group->resource = FALSE;
385 group->deny_only = FALSE;
386 list_add_tail( &token->groups, &group->entry );
389 /* copy privileges */
390 for (i = 0; i < priv_count; i++)
392 /* note: we don't check uniqueness: the caller must make sure
393 * privs doesn't contain any duplicate luids */
394 if (!privilege_add( token, &privs[i].Luid,
395 privs[i].Attributes & SE_PRIVILEGE_ENABLED ))
397 release_object( token );
407 SID_IDENTIFIER_AUTHORITY idauth;
409 unsigned int subauth[MAX_SUBAUTH_COUNT];
412 struct token *token_create_admin( void )
414 struct token *token = NULL;
415 static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
416 static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
417 static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
418 PSID alias_admins_sid;
419 PSID alias_users_sid;
421 alias_admins_sid = security_sid_alloc( &nt_authority, sizeof(alias_admins_subauth)/sizeof(alias_admins_subauth[0]),
422 alias_admins_subauth );
423 alias_users_sid = security_sid_alloc( &nt_authority, sizeof(alias_users_subauth)/sizeof(alias_users_subauth[0]),
424 alias_users_subauth );
426 if (alias_admins_sid && alias_users_sid)
428 const LUID_AND_ATTRIBUTES admin_privs[] =
430 { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
431 { SeSecurityPrivilege , 0 },
432 { SeBackupPrivilege , 0 },
433 { SeRestorePrivilege , 0 },
434 { SeSystemtimePrivilege , 0 },
435 { SeShutdownPrivilege , 0 },
436 { SeRemoteShutdownPrivilege , 0 },
437 { SeTakeOwnershipPrivilege , 0 },
438 { SeDebugPrivilege , 0 },
439 { SeSystemEnvironmentPrivilege , 0 },
440 { SeSystemProfilePrivilege , 0 },
441 { SeProfileSingleProcessPrivilege, 0 },
442 { SeIncreaseBasePriorityPrivilege, 0 },
443 { SeLoadDriverPrivilege , 0 },
444 { SeCreatePagefilePrivilege , 0 },
445 { SeIncreaseQuotaPrivilege , 0 },
446 { SeUndockPrivilege , 0 },
447 { SeManageVolumePrivilege , 0 },
448 { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED },
449 { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED },
451 /* note: we don't include non-builtin groups here for the user -
452 * telling us these is the job of a client-side program */
453 const SID_AND_ATTRIBUTES admin_groups[] =
455 { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
456 { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
457 { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
458 { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
459 { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
460 { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
462 /* note: we just set the user sid to be the local system builtin sid -
463 * telling us what this should be is the job of a client-side program */
464 token = create_token( &local_system_sid,
465 admin_groups, sizeof(admin_groups)/sizeof(admin_groups[0]),
466 admin_privs, sizeof(admin_privs)/sizeof(admin_privs[0]) );
469 if (alias_admins_sid)
470 free( alias_admins_sid );
472 free( alias_users_sid );
477 static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only)
479 struct privilege *privilege;
480 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
482 if (is_equal_luid( luid, &privilege->luid ))
484 if (enabled_only && !privilege->enabled)
492 static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs,
493 unsigned int count, LUID_AND_ATTRIBUTES *mod_privs,
494 unsigned int mod_privs_count)
497 unsigned int modified_count = 0;
499 for (i = 0; i < count; i++)
501 struct privilege *privilege =
502 token_find_privilege( token, &privs[i].Luid, FALSE );
505 set_error( STATUS_NOT_ALL_ASSIGNED );
509 if (privs[i].Attributes & SE_PRIVILEGE_REMOVE)
510 privilege_remove( privilege );
513 /* save previous state for caller */
516 luid_and_attr_from_privilege(mod_privs, privilege);
522 if (privs[i].Attributes & SE_PRIVILEGE_ENABLED)
523 privilege->enabled = TRUE;
525 privilege->enabled = FALSE;
528 return modified_count;
531 static void token_disable_privileges( struct token *token )
533 struct privilege *privilege;
534 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
535 privilege->enabled = FALSE;
538 int token_check_privileges( struct token *token, int all_required,
539 const LUID_AND_ATTRIBUTES *reqprivs,
540 unsigned int count, LUID_AND_ATTRIBUTES *usedprivs)
543 unsigned int enabled_count = 0;
545 for (i = 0; i < count; i++)
547 struct privilege *privilege =
548 token_find_privilege( token, &reqprivs[i].Luid, TRUE );
551 usedprivs[i] = reqprivs[i];
553 if (privilege && privilege->enabled)
557 usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS;
562 return (enabled_count == count);
564 return (enabled_count > 0);
567 static int token_sid_present( struct token *token, const SID *sid, int deny )
569 struct sid_and_attributes *group;
571 if (security_equal_sid( token->user, sid )) return TRUE;
573 LIST_FOR_EACH_ENTRY( group, &token->groups, struct sid_and_attributes, entry )
575 if (!group->enabled) continue;
576 if (group->deny_only && !deny) continue;
578 if (security_equal_sid( &group->sid, sid )) return TRUE;
584 /* checks access to a security descriptor. sd must have been validated by caller.
585 * it returns STATUS_SUCCESS if access was granted to the object, or an error
586 * status code if not, giving the reason. errors not relating to giving access
587 * to the object are returned in the status parameter. granted_access and
588 * status always have a valid value stored in them on return. */
589 static unsigned int token_access_check( struct token *token,
590 const struct security_descriptor *sd,
591 unsigned int desired_access,
592 LUID_AND_ATTRIBUTES *privs,
593 unsigned int *priv_count,
594 const GENERIC_MAPPING *mapping,
595 unsigned int *granted_access,
596 unsigned int *status )
598 unsigned int current_access = 0;
599 unsigned int denied_access = 0;
603 const ACE_HEADER *ace;
606 /* assume success, but no access rights */
607 *status = STATUS_SUCCESS;
610 /* fail if desired_access contains generic rights */
611 if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
614 *status = STATUS_GENERIC_NOT_MAPPED;
615 return STATUS_ACCESS_DENIED;
618 dacl = sd_get_dacl( sd, &dacl_present );
619 owner = sd_get_owner( sd );
620 if (!owner || !sd_get_group( sd ))
623 *status = STATUS_INVALID_SECURITY_DESCR;
624 return STATUS_ACCESS_DENIED;
627 /* 1: Grant desired access if the object is unprotected */
631 *granted_access = desired_access;
632 return STATUS_SUCCESS;
637 return STATUS_ACCESS_DENIED;
640 /* 2: Check if caller wants access to system security part. Note: access
641 * is only granted if specifically asked for */
642 if (desired_access & ACCESS_SYSTEM_SECURITY)
644 const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 };
645 LUID_AND_ATTRIBUTES retpriv = security_priv;
646 if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
650 /* assumes that there will only be one privilege to return */
651 if (*priv_count >= 1)
659 return STATUS_BUFFER_TOO_SMALL;
662 current_access |= ACCESS_SYSTEM_SECURITY;
663 if (desired_access == current_access)
665 *granted_access = current_access;
666 return STATUS_SUCCESS;
672 return STATUS_PRIVILEGE_NOT_HELD;
675 else if (priv_count) *priv_count = 0;
677 /* 3: Check whether the token is the owner */
678 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
679 * checked when a "set owner" call is made, overriding the access rights
680 * determined here. */
681 if (token_sid_present( token, owner, FALSE ))
683 current_access |= (READ_CONTROL | WRITE_DAC);
684 if (desired_access == current_access)
686 *granted_access = current_access;
687 return STATUS_SUCCESS;
691 /* 4: Grant rights according to the DACL */
692 ace = (const ACE_HEADER *)(dacl + 1);
693 for (i = 0; i < dacl->AceCount; i++)
695 const ACCESS_ALLOWED_ACE *aa_ace;
696 const ACCESS_DENIED_ACE *ad_ace;
698 switch (ace->AceType)
700 case ACCESS_DENIED_ACE_TYPE:
701 ad_ace = (const ACCESS_DENIED_ACE *)ace;
702 sid = (const SID *)&ad_ace->SidStart;
703 if (token_sid_present( token, sid, TRUE ))
705 unsigned int access = ad_ace->Mask;
706 map_generic_mask(&access, mapping);
707 if (desired_access & MAXIMUM_ALLOWED)
708 denied_access |= access;
711 denied_access |= (access & ~current_access);
712 if (desired_access & access)
715 return STATUS_SUCCESS;
720 case ACCESS_ALLOWED_ACE_TYPE:
721 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
722 sid = (const SID *)&aa_ace->SidStart;
723 if (token_sid_present( token, sid, FALSE ))
725 unsigned int access = aa_ace->Mask;
726 map_generic_mask(&access, mapping);
727 if (desired_access & MAXIMUM_ALLOWED)
728 current_access |= access;
730 current_access |= (access & ~denied_access);
735 /* don't bother carrying on checking if we've already got all of
737 if (desired_access == *granted_access)
740 ace = ace_next( ace );
743 if (desired_access & MAXIMUM_ALLOWED)
745 *granted_access = current_access & ~denied_access;
747 return STATUS_SUCCESS;
749 return STATUS_ACCESS_DENIED;
753 if ((current_access & desired_access) == desired_access)
755 *granted_access = current_access & desired_access;
756 return STATUS_SUCCESS;
759 return STATUS_ACCESS_DENIED;
763 /* open a security token */
764 DECL_HANDLER(open_token)
766 if( req->flags & OPEN_TOKEN_THREAD )
768 struct thread *thread = get_thread_from_handle( req->handle, 0 );
772 reply->token = alloc_handle( current->process, thread->token, TOKEN_ALL_ACCESS, 0);
774 set_error(STATUS_NO_TOKEN);
775 release_object( thread );
780 struct process *process = get_process_from_handle( req->handle, 0 );
784 reply->token = alloc_handle( current->process, process->token, TOKEN_ALL_ACCESS, 0);
786 set_error(STATUS_NO_TOKEN);
787 release_object( process );
792 /* adjust the privileges held by a token */
793 DECL_HANDLER(adjust_token_privileges)
796 unsigned int access = TOKEN_ADJUST_PRIVILEGES;
798 if (req->get_modified_state) access |= TOKEN_QUERY;
800 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
801 access, &token_ops )))
803 const LUID_AND_ATTRIBUTES *privs = get_req_data();
804 LUID_AND_ATTRIBUTES *modified_privs = NULL;
805 unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
806 unsigned int modified_priv_count = 0;
808 if (req->get_modified_state && !req->disable_all)
811 /* count modified privs */
812 for (i = 0; i < priv_count; i++)
814 struct privilege *privilege =
815 token_find_privilege( token, &privs[i].Luid, FALSE );
816 if (privilege && req->get_modified_state)
817 modified_priv_count++;
819 reply->len = modified_priv_count;
820 modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
821 if (modified_priv_count)
822 modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
824 reply->len = modified_priv_count * sizeof(*modified_privs);
826 if (req->disable_all)
827 token_disable_privileges( token );
829 modified_priv_count = token_adjust_privileges( token, privs,
830 priv_count, modified_privs, modified_priv_count );
832 release_object( token );
836 /* retrieves the list of privileges that may be held be the token */
837 DECL_HANDLER(get_token_privileges)
841 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
846 LUID_AND_ATTRIBUTES *privs;
847 struct privilege *privilege;
849 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
852 reply->len = priv_count * sizeof(*privs);
853 if (reply->len <= get_reply_max_size())
855 privs = set_reply_data_size( priv_count * sizeof(*privs) );
859 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
861 luid_and_attr_from_privilege( &privs[i], privilege );
867 set_error(STATUS_BUFFER_TOO_SMALL);
869 release_object( token );
873 /* creates a duplicate of the token */
874 DECL_HANDLER(duplicate_token)
876 struct token *src_token;
877 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
881 /* FIXME: use req->primary and req->impersonation_level */
882 struct token *token = create_token( src_token->user, NULL, 0, NULL, 0 );
885 struct privilege *privilege;
888 /* FIXME: copy groups */
890 LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
891 privilege_add( token, &privilege->luid, privilege->enabled );
893 access = req->access;
894 if (access & MAXIMUM_ALLOWED) access = TOKEN_ALL_ACCESS; /* FIXME: needs general solution */
895 reply->new_handle = alloc_handle( current->process, token, access, req->inherit);
896 release_object( token );
898 release_object( src_token );
902 /* checks the specified privileges are held by the token */
903 DECL_HANDLER(check_token_privileges)
907 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
911 unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
912 if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES))
914 LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
915 reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
918 set_error( STATUS_BUFFER_OVERFLOW );
919 release_object( token );
923 /* checks that a user represented by a token is allowed to access an object
924 * represented by a security descriptor */
925 DECL_HANDLER(access_check)
927 size_t sd_size = get_req_data_size();
928 const struct security_descriptor *sd = get_req_data();
931 if (!sd_is_valid( sd, sd_size ))
933 set_error( STATUS_ACCESS_VIOLATION );
937 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
941 GENERIC_MAPPING mapping;
943 LUID_AND_ATTRIBUTES priv;
944 unsigned int priv_count = 1;
946 memset(&priv, 0, sizeof(priv));
948 /* FIXME: check token is an impersonation token, if not return
949 * STATUS_NO_IMPERSONATION_TOKEN */
951 mapping.GenericRead = req->mapping_read;
952 mapping.GenericWrite = req->mapping_write;
953 mapping.GenericExecute = req->mapping_execute;
954 mapping.GenericAll = req->mapping_all;
956 reply->access_status = token_access_check(
957 token, sd, req->desired_access, &priv, &priv_count, &mapping,
958 &reply->access_granted, &status );
960 reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES);
962 if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
964 LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) );
965 memcpy( privs, &priv, sizeof(priv) );
968 if (status != STATUS_SUCCESS)
971 release_object( token );