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
32 #define WIN32_NO_STATUS
42 #include "wine/unicode.h"
44 #define MAX_SUBAUTH_COUNT 1
46 const LUID SeIncreaseQuotaPrivilege
= { 5, 0 };
47 const LUID SeSecurityPrivilege
= { 8, 0 };
48 const LUID SeTakeOwnershipPrivilege
= { 9, 0 };
49 const LUID SeLoadDriverPrivilege
= { 10, 0 };
50 const LUID SeSystemProfilePrivilege
= { 11, 0 };
51 const LUID SeSystemtimePrivilege
= { 12, 0 };
52 const LUID SeProfileSingleProcessPrivilege
= { 13, 0 };
53 const LUID SeIncreaseBasePriorityPrivilege
= { 14, 0 };
54 const LUID SeCreatePagefilePrivilege
= { 15, 0 };
55 const LUID SeBackupPrivilege
= { 17, 0 };
56 const LUID SeRestorePrivilege
= { 18, 0 };
57 const LUID SeShutdownPrivilege
= { 19, 0 };
58 const LUID SeDebugPrivilege
= { 20, 0 };
59 const LUID SeSystemEnvironmentPrivilege
= { 22, 0 };
60 const LUID SeChangeNotifyPrivilege
= { 23, 0 };
61 const LUID SeRemoteShutdownPrivilege
= { 24, 0 };
62 const LUID SeUndockPrivilege
= { 25, 0 };
63 const LUID SeManageVolumePrivilege
= { 28, 0 };
64 const LUID SeImpersonatePrivilege
= { 29, 0 };
65 const LUID SeCreateGlobalPrivilege
= { 30, 0 };
67 static const SID world_sid
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} };
68 static const SID local_sid
= { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} };
69 static const SID interactive_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} };
70 static const SID anonymous_logon_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} };
71 static const SID authenticated_user_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} };
72 static const SID local_system_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} };
73 static const SID high_label_sid
= { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_HIGH_RID
} };
74 static const struct /* same fields as struct SID */
77 BYTE SubAuthorityCount
;
78 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
79 DWORD SubAuthority
[5];
80 } local_user_sid
= { SID_REVISION
, 5, { SECURITY_NT_AUTHORITY
}, { SECURITY_NT_NON_UNIQUE
, 0, 0, 0, 1000 } };
81 static const struct /* same fields as struct SID */
84 BYTE SubAuthorityCount
;
85 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
86 DWORD SubAuthority
[2];
87 } builtin_admins_sid
= { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} };
88 static const struct /* same fields as struct SID */
91 BYTE SubAuthorityCount
;
92 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
93 DWORD SubAuthority
[2];
94 } builtin_users_sid
= { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} };
96 const PSID security_world_sid
= (PSID
)&world_sid
;
97 static const PSID security_local_sid
= (PSID
)&local_sid
;
98 static const PSID security_interactive_sid
= (PSID
)&interactive_sid
;
99 static const PSID security_authenticated_user_sid
= (PSID
)&authenticated_user_sid
;
100 const PSID security_local_system_sid
= (PSID
)&local_system_sid
;
101 const PSID security_local_user_sid
= (PSID
)&local_user_sid
;
102 const PSID security_builtin_admins_sid
= (PSID
)&builtin_admins_sid
;
103 const PSID security_builtin_users_sid
= (PSID
)&builtin_users_sid
;
104 const PSID security_high_label_sid
= (PSID
)&high_label_sid
;
106 static luid_t prev_luid_value
= { 1000, 0 };
110 struct object obj
; /* object header */
111 luid_t token_id
; /* system-unique id of token */
112 luid_t modified_id
; /* new id allocated every time token is modified */
113 struct list privileges
; /* privileges available to the token */
114 struct list groups
; /* groups that the user of this token belongs to (sid_and_attributes) */
115 SID
*user
; /* SID of user this token represents */
116 SID
*primary_group
; /* SID of user's primary group */
117 unsigned primary
; /* is this a primary or impersonation token? */
118 ACL
*default_dacl
; /* the default DACL to assign to objects created by this user */
119 TOKEN_SOURCE source
; /* source of the token */
120 int impersonation_level
; /* impersonation level this token is capable of if non-primary token */
127 unsigned enabled
: 1; /* is the privilege currently enabled? */
128 unsigned def
: 1; /* is the privilege enabled by default? */
134 unsigned enabled
: 1; /* is the sid currently enabled? */
135 unsigned def
: 1; /* is the sid enabled by default? */
136 unsigned logon
: 1; /* is this a logon sid? */
137 unsigned mandatory
: 1; /* is this sid always enabled? */
138 unsigned owner
: 1; /* can this sid be an owner of an object? */
139 unsigned resource
: 1; /* is this a domain-local group? */
140 unsigned deny_only
: 1; /* is this a sid that should be use for denying only? */
144 static void token_dump( struct object
*obj
, int verbose
);
145 static unsigned int token_map_access( struct object
*obj
, unsigned int access
);
146 static void token_destroy( struct object
*obj
);
148 static const struct object_ops token_ops
=
150 sizeof(struct token
), /* size */
151 token_dump
, /* dump */
152 no_get_type
, /* get_type */
153 no_add_queue
, /* add_queue */
154 NULL
, /* remove_queue */
156 NULL
, /* satisfied */
157 no_signal
, /* signal */
158 no_get_fd
, /* get_fd */
159 token_map_access
, /* map_access */
160 default_get_sd
, /* get_sd */
161 default_set_sd
, /* set_sd */
162 no_lookup_name
, /* lookup_name */
163 no_link_name
, /* link_name */
164 NULL
, /* unlink_name */
165 no_open_file
, /* open_file */
166 no_close_handle
, /* close_handle */
167 token_destroy
/* destroy */
171 static void token_dump( struct object
*obj
, int verbose
)
173 fprintf( stderr
, "Security token\n" );
174 /* FIXME: dump token members */
177 static unsigned int token_map_access( struct object
*obj
, unsigned int access
)
179 if (access
& GENERIC_READ
) access
|= TOKEN_READ
;
180 if (access
& GENERIC_WRITE
) access
|= TOKEN_WRITE
;
181 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
182 if (access
& GENERIC_ALL
) access
|= TOKEN_ALL_ACCESS
;
183 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
186 static SID
*security_sid_alloc( const SID_IDENTIFIER_AUTHORITY
*idauthority
, int subauthcount
, const unsigned int subauth
[] )
189 SID
*sid
= mem_alloc( FIELD_OFFSET(SID
, SubAuthority
[subauthcount
]) );
190 if (!sid
) return NULL
;
191 sid
->Revision
= SID_REVISION
;
192 sid
->SubAuthorityCount
= subauthcount
;
193 sid
->IdentifierAuthority
= *idauthority
;
194 for (i
= 0; i
< subauthcount
; i
++)
195 sid
->SubAuthority
[i
] = subauth
[i
];
199 void security_set_thread_token( struct thread
*thread
, obj_handle_t handle
)
204 release_object( thread
->token
);
205 thread
->token
= NULL
;
209 struct token
*token
= (struct token
*)get_handle_obj( current
->process
,
216 release_object( thread
->token
);
217 thread
->token
= token
;
222 const SID
*security_unix_uid_to_sid( uid_t uid
)
224 /* very simple mapping: either the current user or not the current user */
226 return (const SID
*)&local_user_sid
;
228 return &anonymous_logon_sid
;
231 static int acl_is_valid( const ACL
*acl
, data_size_t size
)
234 const ACE_HEADER
*ace
;
236 if (size
< sizeof(ACL
))
239 size
= min(size
, MAX_ACL_LEN
);
243 ace
= (const ACE_HEADER
*)(acl
+ 1);
244 for (i
= 0; i
< acl
->AceCount
; i
++)
247 data_size_t sid_size
;
249 if (size
< sizeof(ACE_HEADER
))
251 if (size
< ace
->AceSize
)
253 size
-= ace
->AceSize
;
254 switch (ace
->AceType
)
256 case ACCESS_DENIED_ACE_TYPE
:
257 sid
= (const SID
*)&((const ACCESS_DENIED_ACE
*)ace
)->SidStart
;
258 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_DENIED_ACE
, SidStart
);
260 case ACCESS_ALLOWED_ACE_TYPE
:
261 sid
= (const SID
*)&((const ACCESS_ALLOWED_ACE
*)ace
)->SidStart
;
262 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
);
264 case SYSTEM_AUDIT_ACE_TYPE
:
265 sid
= (const SID
*)&((const SYSTEM_AUDIT_ACE
*)ace
)->SidStart
;
266 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_AUDIT_ACE
, SidStart
);
268 case SYSTEM_ALARM_ACE_TYPE
:
269 sid
= (const SID
*)&((const SYSTEM_ALARM_ACE
*)ace
)->SidStart
;
270 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_ALARM_ACE
, SidStart
);
272 case SYSTEM_MANDATORY_LABEL_ACE_TYPE
:
273 sid
= (const SID
*)&((const SYSTEM_MANDATORY_LABEL_ACE
*)ace
)->SidStart
;
274 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
);
279 if (sid_size
< FIELD_OFFSET(SID
, SubAuthority
[0]) || sid_size
< security_sid_len( sid
))
281 ace
= ace_next( ace
);
286 /* checks whether all members of a security descriptor fit inside the size
287 * of memory specified */
288 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
= security_sid_len( owner
);
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
= security_sid_len( group
);
319 if ((sd
->group_len
< sizeof(SID
)) || (needed_size
> sd
->group_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 /* extract security labels from SACL */
342 ACL
*extract_security_labels( const ACL
*sacl
)
344 size_t size
= sizeof(ACL
);
345 const ACE_HEADER
*ace
;
346 ACE_HEADER
*label_ace
;
347 unsigned int i
, count
= 0;
350 ace
= (const ACE_HEADER
*)(sacl
+ 1);
351 for (i
= 0; i
< sacl
->AceCount
; i
++, ace
= ace_next( ace
))
353 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
)
355 size
+= ace
->AceSize
;
360 label_acl
= mem_alloc( size
);
361 if (!label_acl
) return NULL
;
363 label_acl
->AclRevision
= sacl
->AclRevision
;
365 label_acl
->AclSize
= size
;
366 label_acl
->AceCount
= count
;
368 label_ace
= (ACE_HEADER
*)(label_acl
+ 1);
370 ace
= (const ACE_HEADER
*)(sacl
+ 1);
371 for (i
= 0; i
< sacl
->AceCount
; i
++, ace
= ace_next( ace
))
373 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
)
375 memcpy( label_ace
, ace
, ace
->AceSize
);
376 label_ace
= (ACE_HEADER
*)ace_next( label_ace
);
382 /* replace security labels in an existing SACL */
383 ACL
*replace_security_labels( const ACL
*old_sacl
, const ACL
*new_sacl
)
385 const ACE_HEADER
*ace
;
386 ACE_HEADER
*replaced_ace
;
387 size_t size
= sizeof(ACL
);
388 unsigned int i
, count
= 0;
389 BYTE revision
= ACL_REVISION
;
394 revision
= max( revision
, old_sacl
->AclRevision
);
395 ace
= (const ACE_HEADER
*)(old_sacl
+ 1);
396 for (i
= 0; i
< old_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
398 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
399 size
+= ace
->AceSize
;
406 revision
= max( revision
, new_sacl
->AclRevision
);
407 ace
= (const ACE_HEADER
*)(new_sacl
+ 1);
408 for (i
= 0; i
< new_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
410 if (ace
->AceType
!= SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
411 size
+= ace
->AceSize
;
416 replaced_acl
= mem_alloc( size
);
417 if (!replaced_acl
) return NULL
;
419 replaced_acl
->AclRevision
= revision
;
420 replaced_acl
->Sbz1
= 0;
421 replaced_acl
->AclSize
= size
;
422 replaced_acl
->AceCount
= count
;
423 replaced_acl
->Sbz2
= 0;
424 replaced_ace
= (ACE_HEADER
*)(replaced_acl
+ 1);
428 ace
= (const ACE_HEADER
*)(old_sacl
+ 1);
429 for (i
= 0; i
< old_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
431 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
432 memcpy( replaced_ace
, ace
, ace
->AceSize
);
433 replaced_ace
= (ACE_HEADER
*)ace_next( replaced_ace
);
439 ace
= (const ACE_HEADER
*)(new_sacl
+ 1);
440 for (i
= 0; i
< new_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
442 if (ace
->AceType
!= SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
443 memcpy( replaced_ace
, ace
, ace
->AceSize
);
444 replaced_ace
= (ACE_HEADER
*)ace_next( replaced_ace
);
451 /* maps from generic rights to specific rights as given by a mapping */
452 static inline void map_generic_mask(unsigned int *mask
, const GENERIC_MAPPING
*mapping
)
454 if (*mask
& GENERIC_READ
) *mask
|= mapping
->GenericRead
;
455 if (*mask
& GENERIC_WRITE
) *mask
|= mapping
->GenericWrite
;
456 if (*mask
& GENERIC_EXECUTE
) *mask
|= mapping
->GenericExecute
;
457 if (*mask
& GENERIC_ALL
) *mask
|= mapping
->GenericAll
;
461 static inline int is_equal_luid( const LUID
*luid1
, const LUID
*luid2
)
463 return (luid1
->LowPart
== luid2
->LowPart
&& luid1
->HighPart
== luid2
->HighPart
);
466 static inline void allocate_luid( luid_t
*luid
)
468 prev_luid_value
.low_part
++;
469 *luid
= prev_luid_value
;
472 DECL_HANDLER( allocate_locally_unique_id
)
474 allocate_luid( &reply
->luid
);
477 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES
*out
, const struct privilege
*in
)
479 out
->Luid
= in
->luid
;
481 (in
->enabled
? SE_PRIVILEGE_ENABLED
: 0) |
482 (in
->def
? SE_PRIVILEGE_ENABLED_BY_DEFAULT
: 0);
485 static struct privilege
*privilege_add( struct token
*token
, const LUID
*luid
, int enabled
)
487 struct privilege
*privilege
= mem_alloc( sizeof(*privilege
) );
490 privilege
->luid
= *luid
;
491 privilege
->def
= privilege
->enabled
= (enabled
!= 0);
492 list_add_tail( &token
->privileges
, &privilege
->entry
);
497 static inline void privilege_remove( struct privilege
*privilege
)
499 list_remove( &privilege
->entry
);
503 static void token_destroy( struct object
*obj
)
506 struct list
*cursor
, *cursor_next
;
508 assert( obj
->ops
== &token_ops
);
509 token
= (struct token
*)obj
;
513 LIST_FOR_EACH_SAFE( cursor
, cursor_next
, &token
->privileges
)
515 struct privilege
*privilege
= LIST_ENTRY( cursor
, struct privilege
, entry
);
516 privilege_remove( privilege
);
519 LIST_FOR_EACH_SAFE( cursor
, cursor_next
, &token
->groups
)
521 struct group
*group
= LIST_ENTRY( cursor
, struct group
, entry
);
522 list_remove( &group
->entry
);
526 free( token
->default_dacl
);
529 /* creates a new token.
530 * groups may be NULL if group_count is 0.
531 * privs may be NULL if priv_count is 0.
532 * default_dacl may be NULL, indicating that all objects created by the user
534 * modified_id may be NULL, indicating that a new modified_id luid should be
537 static struct token
*create_token( unsigned primary
, const SID
*user
,
538 const SID_AND_ATTRIBUTES
*groups
, unsigned int group_count
,
539 const LUID_AND_ATTRIBUTES
*privs
, unsigned int priv_count
,
540 const ACL
*default_dacl
, TOKEN_SOURCE source
,
541 const luid_t
*modified_id
,
542 int impersonation_level
)
544 struct token
*token
= alloc_object( &token_ops
);
549 allocate_luid( &token
->token_id
);
551 token
->modified_id
= *modified_id
;
553 allocate_luid( &token
->modified_id
);
554 list_init( &token
->privileges
);
555 list_init( &token
->groups
);
556 token
->primary
= primary
;
557 /* primary tokens don't have impersonation levels */
559 token
->impersonation_level
= -1;
561 token
->impersonation_level
= impersonation_level
;
562 token
->default_dacl
= NULL
;
563 token
->primary_group
= NULL
;
566 token
->user
= memdup( user
, security_sid_len( user
));
569 release_object( token
);
574 for (i
= 0; i
< group_count
; i
++)
576 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[((const SID
*)groups
[i
].Sid
)->SubAuthorityCount
] );
577 struct group
*group
= mem_alloc( size
);
581 release_object( token
);
584 memcpy( &group
->sid
, groups
[i
].Sid
, security_sid_len( groups
[i
].Sid
));
585 group
->enabled
= TRUE
;
587 group
->logon
= (groups
[i
].Attributes
& SE_GROUP_LOGON_ID
) != 0;
588 group
->mandatory
= (groups
[i
].Attributes
& SE_GROUP_MANDATORY
) != 0;
589 group
->owner
= (groups
[i
].Attributes
& SE_GROUP_OWNER
) != 0;
590 group
->resource
= FALSE
;
591 group
->deny_only
= FALSE
;
592 list_add_tail( &token
->groups
, &group
->entry
);
593 /* Use first owner capable group as an owner */
594 if (!token
->primary_group
&& group
->owner
)
595 token
->primary_group
= &group
->sid
;
598 /* copy privileges */
599 for (i
= 0; i
< priv_count
; i
++)
601 /* note: we don't check uniqueness: the caller must make sure
602 * privs doesn't contain any duplicate luids */
603 if (!privilege_add( token
, &privs
[i
].Luid
,
604 privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
))
606 release_object( token
);
613 token
->default_dacl
= memdup( default_dacl
, default_dacl
->AclSize
);
614 if (!token
->default_dacl
)
616 release_object( token
);
621 token
->source
= source
;
626 struct token
*token_duplicate( struct token
*src_token
, unsigned primary
,
627 int impersonation_level
, const struct security_descriptor
*sd
)
629 const luid_t
*modified_id
=
630 primary
|| (impersonation_level
== src_token
->impersonation_level
) ?
631 &src_token
->modified_id
: NULL
;
632 struct token
*token
= NULL
;
633 struct privilege
*privilege
;
637 (impersonation_level
< SecurityAnonymous
||
638 impersonation_level
> SecurityDelegation
||
639 (!src_token
->primary
&& (impersonation_level
> src_token
->impersonation_level
))))
641 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
645 token
= create_token( primary
, src_token
->user
, NULL
, 0,
646 NULL
, 0, src_token
->default_dacl
,
647 src_token
->source
, modified_id
,
648 impersonation_level
);
649 if (!token
) return token
;
652 token
->primary_group
= NULL
;
653 LIST_FOR_EACH_ENTRY( group
, &src_token
->groups
, struct group
, entry
)
655 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[group
->sid
.SubAuthorityCount
] );
656 struct group
*newgroup
= mem_alloc( size
);
659 release_object( token
);
662 memcpy( newgroup
, group
, size
);
663 list_add_tail( &token
->groups
, &newgroup
->entry
);
664 if (src_token
->primary_group
== &group
->sid
)
665 token
->primary_group
= &newgroup
->sid
;
667 assert( token
->primary_group
);
669 /* copy privileges */
670 LIST_FOR_EACH_ENTRY( privilege
, &src_token
->privileges
, struct privilege
, entry
)
671 if (!privilege_add( token
, &privilege
->luid
, privilege
->enabled
))
673 release_object( token
);
677 if (sd
) default_set_sd( &token
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
678 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
683 static ACL
*create_default_dacl( const SID
*user
)
685 ACCESS_ALLOWED_ACE
*aaa
;
688 size_t default_dacl_size
= sizeof(ACL
) +
689 2*(sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
690 sizeof(local_system_sid
) +
691 security_sid_len( user
);
693 default_dacl
= mem_alloc( default_dacl_size
);
694 if (!default_dacl
) return NULL
;
696 default_dacl
->AclRevision
= ACL_REVISION
;
697 default_dacl
->Sbz1
= 0;
698 default_dacl
->AclSize
= default_dacl_size
;
699 default_dacl
->AceCount
= 2;
700 default_dacl
->Sbz2
= 0;
702 /* GENERIC_ALL for Local System */
703 aaa
= (ACCESS_ALLOWED_ACE
*)(default_dacl
+ 1);
704 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
705 aaa
->Header
.AceFlags
= 0;
706 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
707 sizeof(local_system_sid
);
708 aaa
->Mask
= GENERIC_ALL
;
709 sid
= (SID
*)&aaa
->SidStart
;
710 memcpy( sid
, &local_system_sid
, sizeof(local_system_sid
) );
712 /* GENERIC_ALL for specified user */
713 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
714 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
715 aaa
->Header
.AceFlags
= 0;
716 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) + security_sid_len( user
);
717 aaa
->Mask
= GENERIC_ALL
;
718 sid
= (SID
*)&aaa
->SidStart
;
719 memcpy( sid
, user
, security_sid_len( user
));
726 SID_IDENTIFIER_AUTHORITY idauth
;
728 unsigned int subauth
[MAX_SUBAUTH_COUNT
];
731 static struct security_descriptor
*create_security_label_sd( struct token
*token
, PSID label_sid
)
733 size_t sid_len
= security_sid_len( label_sid
), sacl_size
, sd_size
;
734 SYSTEM_MANDATORY_LABEL_ACE
*smla
;
735 struct security_descriptor
*sd
;
738 sacl_size
= sizeof(ACL
) + FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
) + sid_len
;
739 sd_size
= sizeof(struct security_descriptor
) + sacl_size
;
740 if (!(sd
= mem_alloc( sd_size
)))
743 sd
->control
= SE_SACL_PRESENT
;
746 sd
->sacl_len
= sacl_size
;
749 sacl
= (ACL
*)(sd
+ 1);
750 sacl
->AclRevision
= ACL_REVISION
;
752 sacl
->AclSize
= sacl_size
;
756 smla
= (SYSTEM_MANDATORY_LABEL_ACE
*)(sacl
+ 1);
757 smla
->Header
.AceType
= SYSTEM_MANDATORY_LABEL_ACE_TYPE
;
758 smla
->Header
.AceFlags
= 0;
759 smla
->Header
.AceSize
= FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
) + sid_len
;
760 smla
->Mask
= SYSTEM_MANDATORY_LABEL_NO_WRITE_UP
;
761 memcpy( &smla
->SidStart
, label_sid
, sid_len
);
763 assert( sd_is_valid( sd
, sd_size
) );
767 int token_assign_label( struct token
*token
, PSID label
)
769 struct security_descriptor
*sd
;
772 if ((sd
= create_security_label_sd( token
, label
)))
774 ret
= set_sd_defaults_from_token( &token
->obj
, sd
, LABEL_SECURITY_INFORMATION
, token
);
781 struct token
*token_create_admin( void )
783 struct token
*token
= NULL
;
784 static const SID_IDENTIFIER_AUTHORITY nt_authority
= { SECURITY_NT_AUTHORITY
};
785 static const unsigned int alias_admins_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
};
786 static const unsigned int alias_users_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
};
787 /* on Windows, this value changes every time the user logs on */
788 static const unsigned int logon_subauth
[] = { SECURITY_LOGON_IDS_RID
, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
789 PSID alias_admins_sid
;
790 PSID alias_users_sid
;
792 const SID
*user_sid
= security_unix_uid_to_sid( getuid() );
793 ACL
*default_dacl
= create_default_dacl( user_sid
);
795 alias_admins_sid
= security_sid_alloc( &nt_authority
, sizeof(alias_admins_subauth
)/sizeof(alias_admins_subauth
[0]),
796 alias_admins_subauth
);
797 alias_users_sid
= security_sid_alloc( &nt_authority
, sizeof(alias_users_subauth
)/sizeof(alias_users_subauth
[0]),
798 alias_users_subauth
);
799 logon_sid
= security_sid_alloc( &nt_authority
, sizeof(logon_subauth
)/sizeof(logon_subauth
[0]),
802 if (alias_admins_sid
&& alias_users_sid
&& logon_sid
&& default_dacl
)
804 const LUID_AND_ATTRIBUTES admin_privs
[] =
806 { SeChangeNotifyPrivilege
, SE_PRIVILEGE_ENABLED
},
807 { SeSecurityPrivilege
, 0 },
808 { SeBackupPrivilege
, 0 },
809 { SeRestorePrivilege
, 0 },
810 { SeSystemtimePrivilege
, 0 },
811 { SeShutdownPrivilege
, 0 },
812 { SeRemoteShutdownPrivilege
, 0 },
813 { SeTakeOwnershipPrivilege
, 0 },
814 { SeDebugPrivilege
, 0 },
815 { SeSystemEnvironmentPrivilege
, 0 },
816 { SeSystemProfilePrivilege
, 0 },
817 { SeProfileSingleProcessPrivilege
, 0 },
818 { SeIncreaseBasePriorityPrivilege
, 0 },
819 { SeLoadDriverPrivilege
, SE_PRIVILEGE_ENABLED
},
820 { SeCreatePagefilePrivilege
, 0 },
821 { SeIncreaseQuotaPrivilege
, 0 },
822 { SeUndockPrivilege
, 0 },
823 { SeManageVolumePrivilege
, 0 },
824 { SeImpersonatePrivilege
, SE_PRIVILEGE_ENABLED
},
825 { SeCreateGlobalPrivilege
, SE_PRIVILEGE_ENABLED
},
827 /* note: we don't include non-builtin groups here for the user -
828 * telling us these is the job of a client-side program */
829 const SID_AND_ATTRIBUTES admin_groups
[] =
831 { security_world_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
832 { security_local_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
833 { security_interactive_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
834 { security_authenticated_user_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
835 { alias_admins_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_OWNER
},
836 { alias_users_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
837 { logon_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_LOGON_ID
},
839 static const TOKEN_SOURCE admin_source
= {"SeMgr", {0, 0}};
840 token
= create_token( TRUE
, user_sid
, admin_groups
, sizeof(admin_groups
)/sizeof(admin_groups
[0]),
841 admin_privs
, sizeof(admin_privs
)/sizeof(admin_privs
[0]), default_dacl
,
842 admin_source
, NULL
, -1 );
843 /* we really need a primary group */
844 assert( token
->primary_group
);
848 free( alias_admins_sid
);
849 free( alias_users_sid
);
850 free( default_dacl
);
855 static struct privilege
*token_find_privilege( struct token
*token
, const LUID
*luid
, int enabled_only
)
857 struct privilege
*privilege
;
858 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
860 if (is_equal_luid( luid
, &privilege
->luid
))
862 if (enabled_only
&& !privilege
->enabled
)
870 static unsigned int token_adjust_privileges( struct token
*token
, const LUID_AND_ATTRIBUTES
*privs
,
871 unsigned int count
, LUID_AND_ATTRIBUTES
*mod_privs
,
872 unsigned int mod_privs_count
)
874 unsigned int i
, modified_count
= 0;
876 /* mark as modified */
877 allocate_luid( &token
->modified_id
);
879 for (i
= 0; i
< count
; i
++)
881 struct privilege
*privilege
=
882 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
885 set_error( STATUS_NOT_ALL_ASSIGNED
);
889 if (privs
[i
].Attributes
& SE_PRIVILEGE_REMOVED
)
890 privilege_remove( privilege
);
893 /* save previous state for caller */
896 luid_and_attr_from_privilege(mod_privs
, privilege
);
902 if (privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
)
903 privilege
->enabled
= TRUE
;
905 privilege
->enabled
= FALSE
;
908 return modified_count
;
911 static void token_disable_privileges( struct token
*token
)
913 struct privilege
*privilege
;
915 /* mark as modified */
916 allocate_luid( &token
->modified_id
);
918 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
919 privilege
->enabled
= FALSE
;
922 int token_check_privileges( struct token
*token
, int all_required
,
923 const LUID_AND_ATTRIBUTES
*reqprivs
,
924 unsigned int count
, LUID_AND_ATTRIBUTES
*usedprivs
)
926 unsigned int i
, enabled_count
= 0;
928 for (i
= 0; i
< count
; i
++)
930 struct privilege
*privilege
=
931 token_find_privilege( token
, &reqprivs
[i
].Luid
, TRUE
);
934 usedprivs
[i
] = reqprivs
[i
];
936 if (privilege
&& privilege
->enabled
)
940 usedprivs
[i
].Attributes
|= SE_PRIVILEGE_USED_FOR_ACCESS
;
945 return (enabled_count
== count
);
947 return (enabled_count
> 0);
950 int token_sid_present( struct token
*token
, const SID
*sid
, int deny
)
954 if (security_equal_sid( token
->user
, sid
)) return TRUE
;
956 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, struct group
, entry
)
958 if (!group
->enabled
) continue;
959 if (group
->deny_only
&& !deny
) continue;
961 if (security_equal_sid( &group
->sid
, sid
)) return TRUE
;
967 /* Checks access to a security descriptor. 'sd' must have been validated by
968 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
969 * the reason. 'status' parameter will indicate if access is granted or denied.
971 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
973 static unsigned int token_access_check( struct token
*token
,
974 const struct security_descriptor
*sd
,
975 unsigned int desired_access
,
976 LUID_AND_ATTRIBUTES
*privs
,
977 unsigned int *priv_count
,
978 const GENERIC_MAPPING
*mapping
,
979 unsigned int *granted_access
,
980 unsigned int *status
)
982 unsigned int current_access
= 0;
983 unsigned int denied_access
= 0;
987 const ACE_HEADER
*ace
;
990 /* assume no access rights */
993 /* fail if desired_access contains generic rights */
994 if (desired_access
& (GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
|GENERIC_ALL
))
996 if (priv_count
) *priv_count
= 0;
997 return STATUS_GENERIC_NOT_MAPPED
;
1000 dacl
= sd_get_dacl( sd
, &dacl_present
);
1001 owner
= sd_get_owner( sd
);
1002 if (!owner
|| !sd_get_group( sd
))
1004 if (priv_count
) *priv_count
= 0;
1005 return STATUS_INVALID_SECURITY_DESCR
;
1008 /* 1: Grant desired access if the object is unprotected */
1009 if (!dacl_present
|| !dacl
)
1011 if (priv_count
) *priv_count
= 0;
1012 if (desired_access
& MAXIMUM_ALLOWED
)
1013 *granted_access
= mapping
->GenericAll
;
1015 *granted_access
= desired_access
;
1016 return *status
= STATUS_SUCCESS
;
1019 /* 2: Check if caller wants access to system security part. Note: access
1020 * is only granted if specifically asked for */
1021 if (desired_access
& ACCESS_SYSTEM_SECURITY
)
1023 const LUID_AND_ATTRIBUTES security_priv
= { SeSecurityPrivilege
, 0 };
1024 LUID_AND_ATTRIBUTES retpriv
= security_priv
;
1025 if (token_check_privileges( token
, TRUE
, &security_priv
, 1, &retpriv
))
1029 /* assumes that there will only be one privilege to return */
1030 if (*priv_count
>= 1)
1038 return STATUS_BUFFER_TOO_SMALL
;
1041 current_access
|= ACCESS_SYSTEM_SECURITY
;
1042 if (desired_access
== current_access
)
1044 *granted_access
= current_access
;
1045 return *status
= STATUS_SUCCESS
;
1050 if (priv_count
) *priv_count
= 0;
1051 *status
= STATUS_PRIVILEGE_NOT_HELD
;
1052 return STATUS_SUCCESS
;
1055 else if (priv_count
) *priv_count
= 0;
1057 /* 3: Check whether the token is the owner */
1058 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
1059 * checked when a "set owner" call is made, overriding the access rights
1060 * determined here. */
1061 if (token_sid_present( token
, owner
, FALSE
))
1063 current_access
|= (READ_CONTROL
| WRITE_DAC
);
1064 if (desired_access
== current_access
)
1066 *granted_access
= current_access
;
1067 return *status
= STATUS_SUCCESS
;
1071 /* 4: Grant rights according to the DACL */
1072 ace
= (const ACE_HEADER
*)(dacl
+ 1);
1073 for (i
= 0; i
< dacl
->AceCount
; i
++, ace
= ace_next( ace
))
1075 const ACCESS_ALLOWED_ACE
*aa_ace
;
1076 const ACCESS_DENIED_ACE
*ad_ace
;
1079 if (ace
->AceFlags
& INHERIT_ONLY_ACE
)
1082 switch (ace
->AceType
)
1084 case ACCESS_DENIED_ACE_TYPE
:
1085 ad_ace
= (const ACCESS_DENIED_ACE
*)ace
;
1086 sid
= (const SID
*)&ad_ace
->SidStart
;
1087 if (token_sid_present( token
, sid
, TRUE
))
1089 unsigned int access
= ad_ace
->Mask
;
1090 map_generic_mask(&access
, mapping
);
1091 if (desired_access
& MAXIMUM_ALLOWED
)
1092 denied_access
|= access
;
1095 denied_access
|= (access
& ~current_access
);
1096 if (desired_access
& access
) goto done
;
1100 case ACCESS_ALLOWED_ACE_TYPE
:
1101 aa_ace
= (const ACCESS_ALLOWED_ACE
*)ace
;
1102 sid
= (const SID
*)&aa_ace
->SidStart
;
1103 if (token_sid_present( token
, sid
, FALSE
))
1105 unsigned int access
= aa_ace
->Mask
;
1106 map_generic_mask(&access
, mapping
);
1107 if (desired_access
& MAXIMUM_ALLOWED
)
1108 current_access
|= access
;
1110 current_access
|= (access
& ~denied_access
);
1115 /* don't bother carrying on checking if we've already got all of
1117 if (desired_access
== *granted_access
)
1122 if (desired_access
& MAXIMUM_ALLOWED
)
1123 *granted_access
= current_access
& ~denied_access
;
1125 if ((current_access
& desired_access
) == desired_access
)
1126 *granted_access
= current_access
& desired_access
;
1128 *granted_access
= 0;
1130 *status
= *granted_access
? STATUS_SUCCESS
: STATUS_ACCESS_DENIED
;
1131 return STATUS_SUCCESS
;
1134 const ACL
*token_get_default_dacl( struct token
*token
)
1136 return token
->default_dacl
;
1139 const SID
*token_get_user( struct token
*token
)
1144 const SID
*token_get_primary_group( struct token
*token
)
1146 return token
->primary_group
;
1149 int check_object_access(struct object
*obj
, unsigned int *access
)
1151 GENERIC_MAPPING mapping
;
1152 struct token
*token
= current
->token
? current
->token
: current
->process
->token
;
1153 unsigned int status
;
1156 mapping
.GenericAll
= obj
->ops
->map_access( obj
, GENERIC_ALL
);
1160 if (*access
& MAXIMUM_ALLOWED
)
1161 *access
= mapping
.GenericAll
;
1165 mapping
.GenericRead
= obj
->ops
->map_access( obj
, GENERIC_READ
);
1166 mapping
.GenericWrite
= obj
->ops
->map_access( obj
, GENERIC_WRITE
);
1167 mapping
.GenericExecute
= obj
->ops
->map_access( obj
, GENERIC_EXECUTE
);
1169 res
= token_access_check( token
, obj
->sd
, *access
, NULL
, NULL
,
1170 &mapping
, access
, &status
) == STATUS_SUCCESS
&&
1171 status
== STATUS_SUCCESS
;
1173 if (!res
) set_error( STATUS_ACCESS_DENIED
);
1178 /* open a security token */
1179 DECL_HANDLER(open_token
)
1181 if (req
->flags
& OPEN_TOKEN_THREAD
)
1183 struct thread
*thread
= get_thread_from_handle( req
->handle
, 0 );
1188 if (!thread
->token
->primary
&& thread
->token
->impersonation_level
<= SecurityAnonymous
)
1189 set_error( STATUS_CANT_OPEN_ANONYMOUS
);
1191 reply
->token
= alloc_handle( current
->process
, thread
->token
,
1192 req
->access
, req
->attributes
);
1195 set_error( STATUS_NO_TOKEN
);
1196 release_object( thread
);
1201 struct process
*process
= get_process_from_handle( req
->handle
, 0 );
1205 reply
->token
= alloc_handle( current
->process
, process
->token
, req
->access
,
1208 set_error( STATUS_NO_TOKEN
);
1209 release_object( process
);
1214 /* adjust the privileges held by a token */
1215 DECL_HANDLER(adjust_token_privileges
)
1217 struct token
*token
;
1218 unsigned int access
= TOKEN_ADJUST_PRIVILEGES
;
1220 if (req
->get_modified_state
) access
|= TOKEN_QUERY
;
1222 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1223 access
, &token_ops
)))
1225 const LUID_AND_ATTRIBUTES
*privs
= get_req_data();
1226 LUID_AND_ATTRIBUTES
*modified_privs
= NULL
;
1227 unsigned int priv_count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1228 unsigned int modified_priv_count
= 0;
1230 if (req
->get_modified_state
&& !req
->disable_all
)
1233 /* count modified privs */
1234 for (i
= 0; i
< priv_count
; i
++)
1236 struct privilege
*privilege
=
1237 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
1238 if (privilege
&& req
->get_modified_state
)
1239 modified_priv_count
++;
1241 reply
->len
= modified_priv_count
;
1242 modified_priv_count
= min( modified_priv_count
, get_reply_max_size() / sizeof(*modified_privs
) );
1243 if (modified_priv_count
)
1244 modified_privs
= set_reply_data_size( modified_priv_count
* sizeof(*modified_privs
) );
1246 reply
->len
= modified_priv_count
* sizeof(*modified_privs
);
1248 if (req
->disable_all
)
1249 token_disable_privileges( token
);
1251 token_adjust_privileges( token
, privs
, priv_count
, modified_privs
, modified_priv_count
);
1253 release_object( token
);
1257 /* retrieves the list of privileges that may be held be the token */
1258 DECL_HANDLER(get_token_privileges
)
1260 struct token
*token
;
1262 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1267 LUID_AND_ATTRIBUTES
*privs
;
1268 struct privilege
*privilege
;
1270 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1273 reply
->len
= priv_count
* sizeof(*privs
);
1274 if (reply
->len
<= get_reply_max_size())
1276 privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1280 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1282 luid_and_attr_from_privilege( &privs
[i
], privilege
);
1288 set_error(STATUS_BUFFER_TOO_SMALL
);
1290 release_object( token
);
1294 /* creates a duplicate of the token */
1295 DECL_HANDLER(duplicate_token
)
1297 struct token
*src_token
;
1298 struct unicode_str name
;
1299 const struct security_descriptor
*sd
;
1300 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1302 if (!objattr
) return;
1304 if ((src_token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1308 struct token
*token
= token_duplicate( src_token
, req
->primary
, req
->impersonation_level
, sd
);
1311 reply
->new_handle
= alloc_handle_no_access_check( current
->process
, token
, req
->access
, objattr
->attributes
);
1312 release_object( token
);
1314 release_object( src_token
);
1318 /* checks the specified privileges are held by the token */
1319 DECL_HANDLER(check_token_privileges
)
1321 struct token
*token
;
1323 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1327 unsigned int count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1329 if (!token
->primary
&& token
->impersonation_level
<= SecurityAnonymous
)
1330 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1331 else if (get_reply_max_size() >= count
* sizeof(LUID_AND_ATTRIBUTES
))
1333 LUID_AND_ATTRIBUTES
*usedprivs
= set_reply_data_size( count
* sizeof(*usedprivs
) );
1334 reply
->has_privileges
= token_check_privileges( token
, req
->all_required
, get_req_data(), count
, usedprivs
);
1337 set_error( STATUS_BUFFER_OVERFLOW
);
1338 release_object( token
);
1342 /* checks that a user represented by a token is allowed to access an object
1343 * represented by a security descriptor */
1344 DECL_HANDLER(access_check
)
1346 data_size_t sd_size
= get_req_data_size();
1347 const struct security_descriptor
*sd
= get_req_data();
1348 struct token
*token
;
1350 if (!sd_is_valid( sd
, sd_size
))
1352 set_error( STATUS_ACCESS_VIOLATION
);
1356 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1360 GENERIC_MAPPING mapping
;
1361 unsigned int status
;
1362 LUID_AND_ATTRIBUTES priv
;
1363 unsigned int priv_count
= 1;
1365 memset(&priv
, 0, sizeof(priv
));
1367 /* only impersonation tokens may be used with this function */
1370 set_error( STATUS_NO_IMPERSONATION_TOKEN
);
1371 release_object( token
);
1374 /* anonymous impersonation tokens can't be used */
1375 if (token
->impersonation_level
<= SecurityAnonymous
)
1377 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1378 release_object( token
);
1382 mapping
.GenericRead
= req
->mapping_read
;
1383 mapping
.GenericWrite
= req
->mapping_write
;
1384 mapping
.GenericExecute
= req
->mapping_execute
;
1385 mapping
.GenericAll
= req
->mapping_all
;
1387 status
= token_access_check(
1388 token
, sd
, req
->desired_access
, &priv
, &priv_count
, &mapping
,
1389 &reply
->access_granted
, &reply
->access_status
);
1391 reply
->privileges_len
= priv_count
*sizeof(LUID_AND_ATTRIBUTES
);
1393 if ((priv_count
> 0) && (reply
->privileges_len
<= get_reply_max_size()))
1395 LUID_AND_ATTRIBUTES
*privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1396 memcpy( privs
, &priv
, sizeof(priv
) );
1399 set_error( status
);
1400 release_object( token
);
1404 /* retrieves the SID of the user that the token represents */
1405 DECL_HANDLER(get_token_sid
)
1407 struct token
*token
;
1411 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1415 const SID
*sid
= NULL
;
1417 switch (req
->which_sid
)
1420 assert(token
->user
);
1423 case TokenPrimaryGroup
:
1424 sid
= token
->primary_group
;
1428 struct group
*group
;
1429 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, struct group
, entry
)
1440 set_error( STATUS_INVALID_PARAMETER
);
1446 reply
->sid_len
= security_sid_len( sid
);
1447 if (reply
->sid_len
<= get_reply_max_size()) set_reply_data( sid
, reply
->sid_len
);
1448 else set_error( STATUS_BUFFER_TOO_SMALL
);
1450 release_object( token
);
1454 /* retrieves the groups that the user represented by the token belongs to */
1455 DECL_HANDLER(get_token_groups
)
1457 struct token
*token
;
1459 reply
->user_len
= 0;
1461 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1465 size_t size_needed
= sizeof(struct token_groups
);
1466 size_t sid_size
= 0;
1467 unsigned int group_count
= 0;
1468 const struct group
*group
;
1470 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1473 sid_size
+= security_sid_len( &group
->sid
);
1475 size_needed
+= sid_size
;
1476 /* attributes size */
1477 size_needed
+= sizeof(unsigned int) * group_count
;
1479 /* reply buffer contains size_needed bytes formatted as:
1482 unsigned int attrib[count];
1485 user_len includes extra data needed for TOKEN_GROUPS representation,
1486 required caller buffer size calculated here to avoid extra server call */
1487 reply
->user_len
= FIELD_OFFSET( TOKEN_GROUPS
, Groups
[group_count
] ) + sid_size
;
1489 if (reply
->user_len
<= get_reply_max_size())
1491 struct token_groups
*tg
= set_reply_data_size( size_needed
);
1494 unsigned int *attr_ptr
= (unsigned int *)(tg
+ 1);
1495 SID
*sid_ptr
= (SID
*)(attr_ptr
+ group_count
);
1497 tg
->count
= group_count
;
1499 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1503 if (group
->mandatory
) *attr_ptr
|= SE_GROUP_MANDATORY
;
1504 if (group
->def
) *attr_ptr
|= SE_GROUP_ENABLED_BY_DEFAULT
;
1505 if (group
->enabled
) *attr_ptr
|= SE_GROUP_ENABLED
;
1506 if (group
->owner
) *attr_ptr
|= SE_GROUP_OWNER
;
1507 if (group
->deny_only
) *attr_ptr
|= SE_GROUP_USE_FOR_DENY_ONLY
;
1508 if (group
->resource
) *attr_ptr
|= SE_GROUP_RESOURCE
;
1509 if (group
->logon
) *attr_ptr
|= SE_GROUP_LOGON_ID
;
1511 memcpy(sid_ptr
, &group
->sid
, security_sid_len( &group
->sid
));
1513 sid_ptr
= (SID
*)((char *)sid_ptr
+ security_sid_len( &group
->sid
));
1518 else set_error( STATUS_BUFFER_TOO_SMALL
);
1520 release_object( token
);
1524 DECL_HANDLER(get_token_impersonation_level
)
1526 struct token
*token
;
1528 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1533 set_error( STATUS_INVALID_PARAMETER
);
1535 reply
->impersonation_level
= token
->impersonation_level
;
1537 release_object( token
);
1541 DECL_HANDLER(get_token_statistics
)
1543 struct token
*token
;
1545 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1549 reply
->token_id
= token
->token_id
;
1550 reply
->modified_id
= token
->modified_id
;
1551 reply
->primary
= token
->primary
;
1552 reply
->impersonation_level
= token
->impersonation_level
;
1553 reply
->group_count
= list_count( &token
->groups
);
1554 reply
->privilege_count
= list_count( &token
->privileges
);
1556 release_object( token
);
1560 DECL_HANDLER(get_token_default_dacl
)
1562 struct token
*token
;
1566 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1570 if (token
->default_dacl
)
1571 reply
->acl_len
= token
->default_dacl
->AclSize
;
1573 if (reply
->acl_len
<= get_reply_max_size())
1575 ACL
*acl_reply
= set_reply_data_size( reply
->acl_len
);
1577 memcpy( acl_reply
, token
->default_dacl
, reply
->acl_len
);
1579 else set_error( STATUS_BUFFER_TOO_SMALL
);
1581 release_object( token
);
1585 DECL_HANDLER(set_token_default_dacl
)
1587 struct token
*token
;
1589 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1590 TOKEN_ADJUST_DEFAULT
,
1593 const ACL
*acl
= get_req_data();
1594 unsigned int acl_size
= get_req_data_size();
1596 free( token
->default_dacl
);
1597 token
->default_dacl
= NULL
;
1600 token
->default_dacl
= memdup( acl
, acl_size
);
1602 release_object( token
);