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 #define MAX_SUBAUTH_COUNT 1
44 const LUID SeIncreaseQuotaPrivilege
= { 5, 0 };
45 const LUID SeSecurityPrivilege
= { 8, 0 };
46 const LUID SeTakeOwnershipPrivilege
= { 9, 0 };
47 const LUID SeLoadDriverPrivilege
= { 10, 0 };
48 const LUID SeSystemProfilePrivilege
= { 11, 0 };
49 const LUID SeSystemtimePrivilege
= { 12, 0 };
50 const LUID SeProfileSingleProcessPrivilege
= { 13, 0 };
51 const LUID SeIncreaseBasePriorityPrivilege
= { 14, 0 };
52 const LUID SeCreatePagefilePrivilege
= { 15, 0 };
53 const LUID SeBackupPrivilege
= { 17, 0 };
54 const LUID SeRestorePrivilege
= { 18, 0 };
55 const LUID SeShutdownPrivilege
= { 19, 0 };
56 const LUID SeDebugPrivilege
= { 20, 0 };
57 const LUID SeSystemEnvironmentPrivilege
= { 22, 0 };
58 const LUID SeChangeNotifyPrivilege
= { 23, 0 };
59 const LUID SeRemoteShutdownPrivilege
= { 24, 0 };
60 const LUID SeUndockPrivilege
= { 25, 0 };
61 const LUID SeManageVolumePrivilege
= { 28, 0 };
62 const LUID SeImpersonatePrivilege
= { 29, 0 };
63 const LUID SeCreateGlobalPrivilege
= { 30, 0 };
65 #define SID_N(n) struct /* same fields as struct SID */ \
68 BYTE SubAuthorityCount; \
69 SID_IDENTIFIER_AUTHORITY IdentifierAuthority; \
70 DWORD SubAuthority[n]; \
73 static const SID world_sid
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} };
74 static const SID local_sid
= { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} };
75 static const SID interactive_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} };
76 static const SID anonymous_logon_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} };
77 static const SID authenticated_user_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} };
78 static const SID local_system_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} };
79 static const SID high_label_sid
= { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_HIGH_RID
} };
80 static const SID_N(5) local_user_sid
= { SID_REVISION
, 5, { SECURITY_NT_AUTHORITY
}, { SECURITY_NT_NON_UNIQUE
, 0, 0, 0, 1000 } };
81 static const SID_N(2) builtin_admins_sid
= { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} };
82 static const SID_N(2) builtin_users_sid
= { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} };
83 static const SID_N(3) builtin_logon_sid
= { SID_REVISION
, 3, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOGON_IDS_RID
, 0, 0 } };
84 static const SID_N(5) domain_users_sid
= { SID_REVISION
, 5, { SECURITY_NT_AUTHORITY
}, { SECURITY_NT_NON_UNIQUE
, 0, 0, 0, DOMAIN_GROUP_RID_USERS
} };
86 const PSID security_world_sid
= (PSID
)&world_sid
;
87 static const PSID security_local_sid
= (PSID
)&local_sid
;
88 static const PSID security_interactive_sid
= (PSID
)&interactive_sid
;
89 static const PSID security_authenticated_user_sid
= (PSID
)&authenticated_user_sid
;
90 const PSID security_local_system_sid
= (PSID
)&local_system_sid
;
91 const PSID security_local_user_sid
= (PSID
)&local_user_sid
;
92 const PSID security_builtin_admins_sid
= (PSID
)&builtin_admins_sid
;
93 const PSID security_builtin_users_sid
= (PSID
)&builtin_users_sid
;
94 const PSID security_domain_users_sid
= (PSID
)&domain_users_sid
;
95 const PSID security_high_label_sid
= (PSID
)&high_label_sid
;
97 static luid_t prev_luid_value
= { 1000, 0 };
101 struct object obj
; /* object header */
102 luid_t token_id
; /* system-unique id of token */
103 luid_t modified_id
; /* new id allocated every time token is modified */
104 struct list privileges
; /* privileges available to the token */
105 struct list groups
; /* groups that the user of this token belongs to (sid_and_attributes) */
106 SID
*user
; /* SID of user this token represents */
107 SID
*owner
; /* SID of owner (points to user or one of groups) */
108 SID
*primary_group
; /* SID of user's primary group (points to one of groups) */
109 unsigned primary
; /* is this a primary or impersonation token? */
110 ACL
*default_dacl
; /* the default DACL to assign to objects created by this user */
111 TOKEN_SOURCE source
; /* source of the token */
112 int impersonation_level
; /* impersonation level this token is capable of if non-primary token */
119 unsigned enabled
: 1; /* is the privilege currently enabled? */
120 unsigned def
: 1; /* is the privilege enabled by default? */
126 unsigned enabled
: 1; /* is the sid currently enabled? */
127 unsigned def
: 1; /* is the sid enabled by default? */
128 unsigned logon
: 1; /* is this a logon sid? */
129 unsigned mandatory
: 1; /* is this sid always enabled? */
130 unsigned owner
: 1; /* can this sid be an owner of an object? */
131 unsigned resource
: 1; /* is this a domain-local group? */
132 unsigned deny_only
: 1; /* is this a sid that should be use for denying only? */
136 static void token_dump( struct object
*obj
, int verbose
);
137 static struct object_type
*token_get_type( struct object
*obj
);
138 static unsigned int token_map_access( struct object
*obj
, unsigned int access
);
139 static void token_destroy( struct object
*obj
);
141 static const struct object_ops token_ops
=
143 sizeof(struct token
), /* size */
144 token_dump
, /* dump */
145 token_get_type
, /* get_type */
146 no_add_queue
, /* add_queue */
147 NULL
, /* remove_queue */
149 NULL
, /* satisfied */
150 no_signal
, /* signal */
151 no_get_fd
, /* get_fd */
152 token_map_access
, /* map_access */
153 default_get_sd
, /* get_sd */
154 default_set_sd
, /* set_sd */
155 no_get_full_name
, /* get_full_name */
156 no_lookup_name
, /* lookup_name */
157 no_link_name
, /* link_name */
158 NULL
, /* unlink_name */
159 no_open_file
, /* open_file */
160 no_kernel_obj_list
, /* get_kernel_obj_list */
161 no_close_handle
, /* close_handle */
162 token_destroy
/* destroy */
165 static void token_dump( struct object
*obj
, int verbose
)
167 struct token
*token
= (struct token
*)obj
;
168 assert( obj
->ops
== &token_ops
);
169 fprintf( stderr
, "Token id=%d.%u primary=%u impersonation level=%d\n", token
->token_id
.high_part
,
170 token
->token_id
.low_part
, token
->primary
, token
->impersonation_level
);
173 static struct object_type
*token_get_type( struct object
*obj
)
175 static const WCHAR name
[] = {'T','o','k','e','n'};
176 static const struct unicode_str str
= { name
, sizeof(name
) };
177 return get_object_type( &str
);
180 static unsigned int token_map_access( struct object
*obj
, unsigned int access
)
182 if (access
& GENERIC_READ
) access
|= TOKEN_READ
;
183 if (access
& GENERIC_WRITE
) access
|= TOKEN_WRITE
;
184 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
185 if (access
& GENERIC_ALL
) access
|= TOKEN_ALL_ACCESS
;
186 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
189 static SID
*security_sid_alloc( const SID_IDENTIFIER_AUTHORITY
*idauthority
, int subauthcount
, const unsigned int subauth
[] )
192 SID
*sid
= mem_alloc( FIELD_OFFSET(SID
, SubAuthority
[subauthcount
]) );
193 if (!sid
) return NULL
;
194 sid
->Revision
= SID_REVISION
;
195 sid
->SubAuthorityCount
= subauthcount
;
196 sid
->IdentifierAuthority
= *idauthority
;
197 for (i
= 0; i
< subauthcount
; i
++)
198 sid
->SubAuthority
[i
] = subauth
[i
];
202 void security_set_thread_token( struct thread
*thread
, obj_handle_t handle
)
207 release_object( thread
->token
);
208 thread
->token
= NULL
;
212 struct token
*token
= (struct token
*)get_handle_obj( current
->process
,
219 release_object( thread
->token
);
220 thread
->token
= token
;
225 const SID
*security_unix_uid_to_sid( uid_t uid
)
227 /* very simple mapping: either the current user or not the current user */
229 return (const SID
*)&local_user_sid
;
231 return &anonymous_logon_sid
;
234 static int acl_is_valid( const ACL
*acl
, data_size_t size
)
237 const ACE_HEADER
*ace
;
239 if (size
< sizeof(ACL
))
242 size
= min(size
, MAX_ACL_LEN
);
246 ace
= (const ACE_HEADER
*)(acl
+ 1);
247 for (i
= 0; i
< acl
->AceCount
; i
++)
250 data_size_t sid_size
;
252 if (size
< sizeof(ACE_HEADER
))
254 if (size
< ace
->AceSize
)
256 size
-= ace
->AceSize
;
257 switch (ace
->AceType
)
259 case ACCESS_DENIED_ACE_TYPE
:
260 sid
= (const SID
*)&((const ACCESS_DENIED_ACE
*)ace
)->SidStart
;
261 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_DENIED_ACE
, SidStart
);
263 case ACCESS_ALLOWED_ACE_TYPE
:
264 sid
= (const SID
*)&((const ACCESS_ALLOWED_ACE
*)ace
)->SidStart
;
265 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
);
267 case SYSTEM_AUDIT_ACE_TYPE
:
268 sid
= (const SID
*)&((const SYSTEM_AUDIT_ACE
*)ace
)->SidStart
;
269 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_AUDIT_ACE
, SidStart
);
271 case SYSTEM_ALARM_ACE_TYPE
:
272 sid
= (const SID
*)&((const SYSTEM_ALARM_ACE
*)ace
)->SidStart
;
273 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_ALARM_ACE
, SidStart
);
275 case SYSTEM_MANDATORY_LABEL_ACE_TYPE
:
276 sid
= (const SID
*)&((const SYSTEM_MANDATORY_LABEL_ACE
*)ace
)->SidStart
;
277 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
);
282 if (sid_size
< FIELD_OFFSET(SID
, SubAuthority
[0]) || sid_size
< security_sid_len( sid
))
284 ace
= ace_next( ace
);
289 /* checks whether all members of a security descriptor fit inside the size
290 * of memory specified */
291 int sd_is_valid( const struct security_descriptor
*sd
, data_size_t size
)
293 size_t offset
= sizeof(struct security_descriptor
);
303 if ((sd
->owner_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
304 (offset
+ sd
->owner_len
> size
))
306 owner
= sd_get_owner( sd
);
309 if ((sd
->owner_len
< sizeof(SID
)) || (security_sid_len( owner
) > sd
->owner_len
))
312 offset
+= sd
->owner_len
;
314 if ((sd
->group_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
315 (offset
+ sd
->group_len
> size
))
317 group
= sd_get_group( sd
);
320 if ((sd
->group_len
< sizeof(SID
)) || (security_sid_len( group
) > sd
->group_len
))
323 offset
+= sd
->group_len
;
325 if ((sd
->sacl_len
>= MAX_ACL_LEN
) || (offset
+ sd
->sacl_len
> size
))
327 sacl
= sd_get_sacl( sd
, &dummy
);
328 if (sacl
&& !acl_is_valid( sacl
, sd
->sacl_len
))
330 offset
+= sd
->sacl_len
;
332 if ((sd
->dacl_len
>= MAX_ACL_LEN
) || (offset
+ sd
->dacl_len
> size
))
334 dacl
= sd_get_dacl( sd
, &dummy
);
335 if (dacl
&& !acl_is_valid( dacl
, sd
->dacl_len
))
337 offset
+= sd
->dacl_len
;
342 /* extract security labels from SACL */
343 ACL
*extract_security_labels( const ACL
*sacl
)
345 size_t size
= sizeof(ACL
);
346 const ACE_HEADER
*ace
;
347 ACE_HEADER
*label_ace
;
348 unsigned int i
, count
= 0;
351 ace
= (const ACE_HEADER
*)(sacl
+ 1);
352 for (i
= 0; i
< sacl
->AceCount
; i
++, ace
= ace_next( ace
))
354 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
)
356 size
+= ace
->AceSize
;
361 label_acl
= mem_alloc( size
);
362 if (!label_acl
) return NULL
;
364 label_acl
->AclRevision
= sacl
->AclRevision
;
366 label_acl
->AclSize
= size
;
367 label_acl
->AceCount
= count
;
369 label_ace
= (ACE_HEADER
*)(label_acl
+ 1);
371 ace
= (const ACE_HEADER
*)(sacl
+ 1);
372 for (i
= 0; i
< sacl
->AceCount
; i
++, ace
= ace_next( ace
))
374 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
)
376 memcpy( label_ace
, ace
, ace
->AceSize
);
377 label_ace
= (ACE_HEADER
*)ace_next( label_ace
);
383 /* replace security labels in an existing SACL */
384 ACL
*replace_security_labels( const ACL
*old_sacl
, const ACL
*new_sacl
)
386 const ACE_HEADER
*ace
;
387 ACE_HEADER
*replaced_ace
;
388 size_t size
= sizeof(ACL
);
389 unsigned int i
, count
= 0;
390 BYTE revision
= ACL_REVISION
;
395 revision
= max( revision
, old_sacl
->AclRevision
);
396 ace
= (const ACE_HEADER
*)(old_sacl
+ 1);
397 for (i
= 0; i
< old_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
399 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
400 size
+= ace
->AceSize
;
407 revision
= max( revision
, new_sacl
->AclRevision
);
408 ace
= (const ACE_HEADER
*)(new_sacl
+ 1);
409 for (i
= 0; i
< new_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
411 if (ace
->AceType
!= SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
412 size
+= ace
->AceSize
;
417 replaced_acl
= mem_alloc( size
);
418 if (!replaced_acl
) return NULL
;
420 replaced_acl
->AclRevision
= revision
;
421 replaced_acl
->Sbz1
= 0;
422 replaced_acl
->AclSize
= size
;
423 replaced_acl
->AceCount
= count
;
424 replaced_acl
->Sbz2
= 0;
425 replaced_ace
= (ACE_HEADER
*)(replaced_acl
+ 1);
429 ace
= (const ACE_HEADER
*)(old_sacl
+ 1);
430 for (i
= 0; i
< old_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
432 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
433 memcpy( replaced_ace
, ace
, ace
->AceSize
);
434 replaced_ace
= (ACE_HEADER
*)ace_next( replaced_ace
);
440 ace
= (const ACE_HEADER
*)(new_sacl
+ 1);
441 for (i
= 0; i
< new_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
443 if (ace
->AceType
!= SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
444 memcpy( replaced_ace
, ace
, ace
->AceSize
);
445 replaced_ace
= (ACE_HEADER
*)ace_next( replaced_ace
);
452 /* maps from generic rights to specific rights as given by a mapping */
453 static inline void map_generic_mask(unsigned int *mask
, const GENERIC_MAPPING
*mapping
)
455 if (*mask
& GENERIC_READ
) *mask
|= mapping
->GenericRead
;
456 if (*mask
& GENERIC_WRITE
) *mask
|= mapping
->GenericWrite
;
457 if (*mask
& GENERIC_EXECUTE
) *mask
|= mapping
->GenericExecute
;
458 if (*mask
& GENERIC_ALL
) *mask
|= mapping
->GenericAll
;
462 static inline int is_equal_luid( const LUID
*luid1
, const LUID
*luid2
)
464 return (luid1
->LowPart
== luid2
->LowPart
&& luid1
->HighPart
== luid2
->HighPart
);
467 static inline void allocate_luid( luid_t
*luid
)
469 prev_luid_value
.low_part
++;
470 *luid
= prev_luid_value
;
473 DECL_HANDLER( allocate_locally_unique_id
)
475 allocate_luid( &reply
->luid
);
478 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES
*out
, const struct privilege
*in
)
480 out
->Luid
= in
->luid
;
482 (in
->enabled
? SE_PRIVILEGE_ENABLED
: 0) |
483 (in
->def
? SE_PRIVILEGE_ENABLED_BY_DEFAULT
: 0);
486 static struct privilege
*privilege_add( struct token
*token
, const LUID
*luid
, int enabled
)
488 struct privilege
*privilege
= mem_alloc( sizeof(*privilege
) );
491 privilege
->luid
= *luid
;
492 privilege
->def
= privilege
->enabled
= (enabled
!= 0);
493 list_add_tail( &token
->privileges
, &privilege
->entry
);
498 static inline void privilege_remove( struct privilege
*privilege
)
500 list_remove( &privilege
->entry
);
504 static void token_destroy( struct object
*obj
)
507 struct list
*cursor
, *cursor_next
;
509 assert( obj
->ops
== &token_ops
);
510 token
= (struct token
*)obj
;
514 LIST_FOR_EACH_SAFE( cursor
, cursor_next
, &token
->privileges
)
516 struct privilege
*privilege
= LIST_ENTRY( cursor
, struct privilege
, entry
);
517 privilege_remove( privilege
);
520 LIST_FOR_EACH_SAFE( cursor
, cursor_next
, &token
->groups
)
522 struct group
*group
= LIST_ENTRY( cursor
, struct group
, entry
);
523 list_remove( &group
->entry
);
527 free( token
->default_dacl
);
530 /* creates a new token.
531 * groups may be NULL if group_count is 0.
532 * privs may be NULL if priv_count is 0.
533 * default_dacl may be NULL, indicating that all objects created by the user
535 * modified_id may be NULL, indicating that a new modified_id luid should be
538 static struct token
*create_token( unsigned primary
, const SID
*user
,
539 const SID_AND_ATTRIBUTES
*groups
, unsigned int group_count
,
540 const LUID_AND_ATTRIBUTES
*privs
, unsigned int priv_count
,
541 const ACL
*default_dacl
, TOKEN_SOURCE source
,
542 const luid_t
*modified_id
,
543 int impersonation_level
)
545 struct token
*token
= alloc_object( &token_ops
);
550 allocate_luid( &token
->token_id
);
552 token
->modified_id
= *modified_id
;
554 allocate_luid( &token
->modified_id
);
555 list_init( &token
->privileges
);
556 list_init( &token
->groups
);
557 token
->primary
= primary
;
558 /* primary tokens don't have impersonation levels */
560 token
->impersonation_level
= -1;
562 token
->impersonation_level
= impersonation_level
;
563 token
->default_dacl
= NULL
;
564 token
->primary_group
= NULL
;
567 token
->user
= memdup( user
, security_sid_len( user
));
570 release_object( token
);
575 for (i
= 0; i
< group_count
; i
++)
577 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[((const SID
*)groups
[i
].Sid
)->SubAuthorityCount
] );
578 struct group
*group
= mem_alloc( size
);
582 release_object( token
);
585 memcpy( &group
->sid
, groups
[i
].Sid
, security_sid_len( groups
[i
].Sid
));
586 group
->enabled
= TRUE
;
588 group
->logon
= (groups
[i
].Attributes
& SE_GROUP_LOGON_ID
) != 0;
589 group
->mandatory
= (groups
[i
].Attributes
& SE_GROUP_MANDATORY
) != 0;
590 group
->owner
= (groups
[i
].Attributes
& SE_GROUP_OWNER
) != 0;
591 group
->resource
= FALSE
;
592 group
->deny_only
= FALSE
;
593 list_add_tail( &token
->groups
, &group
->entry
);
594 /* Use first owner capable group as owner and primary group */
595 if (!token
->primary_group
&& group
->owner
)
597 token
->owner
= &group
->sid
;
598 token
->primary_group
= &group
->sid
;
602 /* copy privileges */
603 for (i
= 0; i
< priv_count
; i
++)
605 /* note: we don't check uniqueness: the caller must make sure
606 * privs doesn't contain any duplicate luids */
607 if (!privilege_add( token
, &privs
[i
].Luid
,
608 privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
))
610 release_object( token
);
617 token
->default_dacl
= memdup( default_dacl
, default_dacl
->AclSize
);
618 if (!token
->default_dacl
)
620 release_object( token
);
625 token
->source
= source
;
630 struct token
*token_duplicate( struct token
*src_token
, unsigned primary
,
631 int impersonation_level
, const struct security_descriptor
*sd
)
633 const luid_t
*modified_id
=
634 primary
|| (impersonation_level
== src_token
->impersonation_level
) ?
635 &src_token
->modified_id
: NULL
;
636 struct token
*token
= NULL
;
637 struct privilege
*privilege
;
641 (impersonation_level
< SecurityAnonymous
||
642 impersonation_level
> SecurityDelegation
||
643 (!src_token
->primary
&& (impersonation_level
> src_token
->impersonation_level
))))
645 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
649 token
= create_token( primary
, src_token
->user
, NULL
, 0,
650 NULL
, 0, src_token
->default_dacl
,
651 src_token
->source
, modified_id
,
652 impersonation_level
);
653 if (!token
) return token
;
656 token
->primary_group
= NULL
;
657 LIST_FOR_EACH_ENTRY( group
, &src_token
->groups
, struct group
, entry
)
659 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[group
->sid
.SubAuthorityCount
] );
660 struct group
*newgroup
= mem_alloc( size
);
663 release_object( token
);
666 memcpy( newgroup
, group
, size
);
667 list_add_tail( &token
->groups
, &newgroup
->entry
);
668 if (src_token
->primary_group
== &group
->sid
)
670 token
->owner
= &newgroup
->sid
;
671 token
->primary_group
= &newgroup
->sid
;
674 assert( token
->primary_group
);
676 /* copy privileges */
677 LIST_FOR_EACH_ENTRY( privilege
, &src_token
->privileges
, struct privilege
, entry
)
678 if (!privilege_add( token
, &privilege
->luid
, privilege
->enabled
))
680 release_object( token
);
684 if (sd
) default_set_sd( &token
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
685 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
690 static ACL
*create_default_dacl( const SID
*user
)
692 ACCESS_ALLOWED_ACE
*aaa
;
695 size_t default_dacl_size
= sizeof(ACL
) +
696 2*(sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
697 sizeof(local_system_sid
) +
698 security_sid_len( user
);
700 default_dacl
= mem_alloc( default_dacl_size
);
701 if (!default_dacl
) return NULL
;
703 default_dacl
->AclRevision
= ACL_REVISION
;
704 default_dacl
->Sbz1
= 0;
705 default_dacl
->AclSize
= default_dacl_size
;
706 default_dacl
->AceCount
= 2;
707 default_dacl
->Sbz2
= 0;
709 /* GENERIC_ALL for Local System */
710 aaa
= (ACCESS_ALLOWED_ACE
*)(default_dacl
+ 1);
711 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
712 aaa
->Header
.AceFlags
= 0;
713 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
714 sizeof(local_system_sid
);
715 aaa
->Mask
= GENERIC_ALL
;
716 sid
= (SID
*)&aaa
->SidStart
;
717 memcpy( sid
, &local_system_sid
, sizeof(local_system_sid
) );
719 /* GENERIC_ALL for specified user */
720 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
721 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
722 aaa
->Header
.AceFlags
= 0;
723 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) + security_sid_len( user
);
724 aaa
->Mask
= GENERIC_ALL
;
725 sid
= (SID
*)&aaa
->SidStart
;
726 memcpy( sid
, user
, security_sid_len( user
));
733 SID_IDENTIFIER_AUTHORITY idauth
;
735 unsigned int subauth
[MAX_SUBAUTH_COUNT
];
738 static struct security_descriptor
*create_security_label_sd( struct token
*token
, PSID label_sid
)
740 size_t sid_len
= security_sid_len( label_sid
), sacl_size
, sd_size
;
741 SYSTEM_MANDATORY_LABEL_ACE
*smla
;
742 struct security_descriptor
*sd
;
745 sacl_size
= sizeof(ACL
) + FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
) + sid_len
;
746 sd_size
= sizeof(struct security_descriptor
) + sacl_size
;
747 if (!(sd
= mem_alloc( sd_size
)))
750 sd
->control
= SE_SACL_PRESENT
;
753 sd
->sacl_len
= sacl_size
;
756 sacl
= (ACL
*)(sd
+ 1);
757 sacl
->AclRevision
= ACL_REVISION
;
759 sacl
->AclSize
= sacl_size
;
763 smla
= (SYSTEM_MANDATORY_LABEL_ACE
*)(sacl
+ 1);
764 smla
->Header
.AceType
= SYSTEM_MANDATORY_LABEL_ACE_TYPE
;
765 smla
->Header
.AceFlags
= 0;
766 smla
->Header
.AceSize
= FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
) + sid_len
;
767 smla
->Mask
= SYSTEM_MANDATORY_LABEL_NO_WRITE_UP
;
768 memcpy( &smla
->SidStart
, label_sid
, sid_len
);
770 assert( sd_is_valid( sd
, sd_size
) );
774 int token_assign_label( struct token
*token
, PSID label
)
776 struct security_descriptor
*sd
;
779 if ((sd
= create_security_label_sd( token
, label
)))
781 ret
= set_sd_defaults_from_token( &token
->obj
, sd
, LABEL_SECURITY_INFORMATION
, token
);
788 struct token
*token_create_admin( void )
790 struct token
*token
= NULL
;
791 static const SID_IDENTIFIER_AUTHORITY nt_authority
= { SECURITY_NT_AUTHORITY
};
792 static const unsigned int alias_admins_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
};
793 static const unsigned int alias_users_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
};
794 /* on Windows, this value changes every time the user logs on */
795 static const unsigned int logon_subauth
[] = { SECURITY_LOGON_IDS_RID
, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
796 PSID alias_admins_sid
;
797 PSID alias_users_sid
;
799 const SID
*user_sid
= security_unix_uid_to_sid( getuid() );
800 ACL
*default_dacl
= create_default_dacl( user_sid
);
802 alias_admins_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( alias_admins_subauth
),
803 alias_admins_subauth
);
804 alias_users_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( alias_users_subauth
),
805 alias_users_subauth
);
806 logon_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( logon_subauth
), logon_subauth
);
808 if (alias_admins_sid
&& alias_users_sid
&& logon_sid
&& default_dacl
)
810 const LUID_AND_ATTRIBUTES admin_privs
[] =
812 { SeChangeNotifyPrivilege
, SE_PRIVILEGE_ENABLED
},
813 { SeSecurityPrivilege
, 0 },
814 { SeBackupPrivilege
, 0 },
815 { SeRestorePrivilege
, 0 },
816 { SeSystemtimePrivilege
, 0 },
817 { SeShutdownPrivilege
, 0 },
818 { SeRemoteShutdownPrivilege
, 0 },
819 { SeTakeOwnershipPrivilege
, 0 },
820 { SeDebugPrivilege
, 0 },
821 { SeSystemEnvironmentPrivilege
, 0 },
822 { SeSystemProfilePrivilege
, 0 },
823 { SeProfileSingleProcessPrivilege
, 0 },
824 { SeIncreaseBasePriorityPrivilege
, 0 },
825 { SeLoadDriverPrivilege
, SE_PRIVILEGE_ENABLED
},
826 { SeCreatePagefilePrivilege
, 0 },
827 { SeIncreaseQuotaPrivilege
, 0 },
828 { SeUndockPrivilege
, 0 },
829 { SeManageVolumePrivilege
, 0 },
830 { SeImpersonatePrivilege
, SE_PRIVILEGE_ENABLED
},
831 { SeCreateGlobalPrivilege
, SE_PRIVILEGE_ENABLED
},
833 /* note: we don't include non-builtin groups here for the user -
834 * telling us these is the job of a client-side program */
835 const SID_AND_ATTRIBUTES admin_groups
[] =
837 { security_world_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
838 { security_local_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
839 { security_interactive_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
840 { security_authenticated_user_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
841 { security_domain_users_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_OWNER
},
842 { alias_admins_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_OWNER
},
843 { alias_users_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
844 { logon_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_LOGON_ID
},
846 static const TOKEN_SOURCE admin_source
= {"SeMgr", {0, 0}};
847 token
= create_token( TRUE
, user_sid
, admin_groups
, ARRAY_SIZE( admin_groups
),
848 admin_privs
, ARRAY_SIZE( admin_privs
), default_dacl
,
849 admin_source
, NULL
, -1 );
850 /* we really need a primary group */
851 assert( token
->primary_group
);
855 free( alias_admins_sid
);
856 free( alias_users_sid
);
857 free( default_dacl
);
862 static struct privilege
*token_find_privilege( struct token
*token
, const LUID
*luid
, int enabled_only
)
864 struct privilege
*privilege
;
865 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
867 if (is_equal_luid( luid
, &privilege
->luid
))
869 if (enabled_only
&& !privilege
->enabled
)
877 static unsigned int token_adjust_privileges( struct token
*token
, const LUID_AND_ATTRIBUTES
*privs
,
878 unsigned int count
, LUID_AND_ATTRIBUTES
*mod_privs
,
879 unsigned int mod_privs_count
)
881 unsigned int i
, modified_count
= 0;
883 /* mark as modified */
884 allocate_luid( &token
->modified_id
);
886 for (i
= 0; i
< count
; i
++)
888 struct privilege
*privilege
=
889 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
892 set_error( STATUS_NOT_ALL_ASSIGNED
);
896 if (privs
[i
].Attributes
& SE_PRIVILEGE_REMOVED
)
897 privilege_remove( privilege
);
900 /* save previous state for caller */
903 luid_and_attr_from_privilege(mod_privs
, privilege
);
909 if (privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
)
910 privilege
->enabled
= TRUE
;
912 privilege
->enabled
= FALSE
;
915 return modified_count
;
918 static void token_disable_privileges( struct token
*token
)
920 struct privilege
*privilege
;
922 /* mark as modified */
923 allocate_luid( &token
->modified_id
);
925 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
926 privilege
->enabled
= FALSE
;
929 int token_check_privileges( struct token
*token
, int all_required
,
930 const LUID_AND_ATTRIBUTES
*reqprivs
,
931 unsigned int count
, LUID_AND_ATTRIBUTES
*usedprivs
)
933 unsigned int i
, enabled_count
= 0;
935 for (i
= 0; i
< count
; i
++)
937 struct privilege
*privilege
=
938 token_find_privilege( token
, &reqprivs
[i
].Luid
, TRUE
);
941 usedprivs
[i
] = reqprivs
[i
];
943 if (privilege
&& privilege
->enabled
)
947 usedprivs
[i
].Attributes
|= SE_PRIVILEGE_USED_FOR_ACCESS
;
952 return (enabled_count
== count
);
954 return (enabled_count
> 0);
957 int token_sid_present( struct token
*token
, const SID
*sid
, int deny
)
961 if (security_equal_sid( token
->user
, sid
)) return TRUE
;
963 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, struct group
, entry
)
965 if (!group
->enabled
) continue;
966 if (group
->deny_only
&& !deny
) continue;
968 if (security_equal_sid( &group
->sid
, sid
)) return TRUE
;
974 /* Checks access to a security descriptor. 'sd' must have been validated by
975 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
976 * the reason. 'status' parameter will indicate if access is granted or denied.
978 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
980 static unsigned int token_access_check( struct token
*token
,
981 const struct security_descriptor
*sd
,
982 unsigned int desired_access
,
983 LUID_AND_ATTRIBUTES
*privs
,
984 unsigned int *priv_count
,
985 const GENERIC_MAPPING
*mapping
,
986 unsigned int *granted_access
,
987 unsigned int *status
)
989 unsigned int current_access
= 0;
990 unsigned int denied_access
= 0;
994 const ACE_HEADER
*ace
;
997 /* assume no access rights */
1000 /* fail if desired_access contains generic rights */
1001 if (desired_access
& (GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
|GENERIC_ALL
))
1003 if (priv_count
) *priv_count
= 0;
1004 return STATUS_GENERIC_NOT_MAPPED
;
1007 dacl
= sd_get_dacl( sd
, &dacl_present
);
1008 owner
= sd_get_owner( sd
);
1009 if (!owner
|| !sd_get_group( sd
))
1011 if (priv_count
) *priv_count
= 0;
1012 return STATUS_INVALID_SECURITY_DESCR
;
1015 /* 1: Grant desired access if the object is unprotected */
1016 if (!dacl_present
|| !dacl
)
1018 if (priv_count
) *priv_count
= 0;
1019 if (desired_access
& MAXIMUM_ALLOWED
)
1020 *granted_access
= mapping
->GenericAll
;
1022 *granted_access
= desired_access
;
1023 return *status
= STATUS_SUCCESS
;
1026 /* 2: Check if caller wants access to system security part. Note: access
1027 * is only granted if specifically asked for */
1028 if (desired_access
& ACCESS_SYSTEM_SECURITY
)
1030 const LUID_AND_ATTRIBUTES security_priv
= { SeSecurityPrivilege
, 0 };
1031 LUID_AND_ATTRIBUTES retpriv
= security_priv
;
1032 if (token_check_privileges( token
, TRUE
, &security_priv
, 1, &retpriv
))
1036 /* assumes that there will only be one privilege to return */
1037 if (*priv_count
>= 1)
1045 return STATUS_BUFFER_TOO_SMALL
;
1048 current_access
|= ACCESS_SYSTEM_SECURITY
;
1049 if (desired_access
== current_access
)
1051 *granted_access
= current_access
;
1052 return *status
= STATUS_SUCCESS
;
1057 if (priv_count
) *priv_count
= 0;
1058 *status
= STATUS_PRIVILEGE_NOT_HELD
;
1059 return STATUS_SUCCESS
;
1062 else if (priv_count
) *priv_count
= 0;
1064 /* 3: Check whether the token is the owner */
1065 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
1066 * checked when a "set owner" call is made, overriding the access rights
1067 * determined here. */
1068 if (token_sid_present( token
, owner
, FALSE
))
1070 current_access
|= (READ_CONTROL
| WRITE_DAC
);
1071 if (desired_access
== current_access
)
1073 *granted_access
= current_access
;
1074 return *status
= STATUS_SUCCESS
;
1078 /* 4: Grant rights according to the DACL */
1079 ace
= (const ACE_HEADER
*)(dacl
+ 1);
1080 for (i
= 0; i
< dacl
->AceCount
; i
++, ace
= ace_next( ace
))
1082 const ACCESS_ALLOWED_ACE
*aa_ace
;
1083 const ACCESS_DENIED_ACE
*ad_ace
;
1086 if (ace
->AceFlags
& INHERIT_ONLY_ACE
)
1089 switch (ace
->AceType
)
1091 case ACCESS_DENIED_ACE_TYPE
:
1092 ad_ace
= (const ACCESS_DENIED_ACE
*)ace
;
1093 sid
= (const SID
*)&ad_ace
->SidStart
;
1094 if (token_sid_present( token
, sid
, TRUE
))
1096 unsigned int access
= ad_ace
->Mask
;
1097 map_generic_mask(&access
, mapping
);
1098 if (desired_access
& MAXIMUM_ALLOWED
)
1099 denied_access
|= access
;
1102 denied_access
|= (access
& ~current_access
);
1103 if (desired_access
& access
) goto done
;
1107 case ACCESS_ALLOWED_ACE_TYPE
:
1108 aa_ace
= (const ACCESS_ALLOWED_ACE
*)ace
;
1109 sid
= (const SID
*)&aa_ace
->SidStart
;
1110 if (token_sid_present( token
, sid
, FALSE
))
1112 unsigned int access
= aa_ace
->Mask
;
1113 map_generic_mask(&access
, mapping
);
1114 if (desired_access
& MAXIMUM_ALLOWED
)
1115 current_access
|= access
;
1117 current_access
|= (access
& ~denied_access
);
1122 /* don't bother carrying on checking if we've already got all of
1124 if (desired_access
== *granted_access
)
1129 if (desired_access
& MAXIMUM_ALLOWED
)
1130 *granted_access
= current_access
& ~denied_access
;
1132 if ((current_access
& desired_access
) == desired_access
)
1133 *granted_access
= current_access
& desired_access
;
1135 *granted_access
= 0;
1137 *status
= *granted_access
? STATUS_SUCCESS
: STATUS_ACCESS_DENIED
;
1138 return STATUS_SUCCESS
;
1141 const ACL
*token_get_default_dacl( struct token
*token
)
1143 return token
->default_dacl
;
1146 const SID
*token_get_user( struct token
*token
)
1151 const SID
*token_get_primary_group( struct token
*token
)
1153 return token
->primary_group
;
1156 int check_object_access(struct object
*obj
, unsigned int *access
)
1158 GENERIC_MAPPING mapping
;
1159 struct token
*token
= current
->token
? current
->token
: current
->process
->token
;
1160 unsigned int status
;
1163 mapping
.GenericAll
= obj
->ops
->map_access( obj
, GENERIC_ALL
);
1167 if (*access
& MAXIMUM_ALLOWED
)
1168 *access
= mapping
.GenericAll
;
1172 mapping
.GenericRead
= obj
->ops
->map_access( obj
, GENERIC_READ
);
1173 mapping
.GenericWrite
= obj
->ops
->map_access( obj
, GENERIC_WRITE
);
1174 mapping
.GenericExecute
= obj
->ops
->map_access( obj
, GENERIC_EXECUTE
);
1176 res
= token_access_check( token
, obj
->sd
, *access
, NULL
, NULL
,
1177 &mapping
, access
, &status
) == STATUS_SUCCESS
&&
1178 status
== STATUS_SUCCESS
;
1180 if (!res
) set_error( STATUS_ACCESS_DENIED
);
1185 /* open a security token */
1186 DECL_HANDLER(open_token
)
1188 if (req
->flags
& OPEN_TOKEN_THREAD
)
1190 struct thread
*thread
= get_thread_from_handle( req
->handle
, 0 );
1195 if (!thread
->token
->primary
&& thread
->token
->impersonation_level
<= SecurityAnonymous
)
1196 set_error( STATUS_CANT_OPEN_ANONYMOUS
);
1198 reply
->token
= alloc_handle( current
->process
, thread
->token
,
1199 req
->access
, req
->attributes
);
1202 set_error( STATUS_NO_TOKEN
);
1203 release_object( thread
);
1208 struct process
*process
= get_process_from_handle( req
->handle
, 0 );
1212 reply
->token
= alloc_handle( current
->process
, process
->token
, req
->access
,
1215 set_error( STATUS_NO_TOKEN
);
1216 release_object( process
);
1221 /* adjust the privileges held by a token */
1222 DECL_HANDLER(adjust_token_privileges
)
1224 struct token
*token
;
1225 unsigned int access
= TOKEN_ADJUST_PRIVILEGES
;
1227 if (req
->get_modified_state
) access
|= TOKEN_QUERY
;
1229 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1230 access
, &token_ops
)))
1232 const LUID_AND_ATTRIBUTES
*privs
= get_req_data();
1233 LUID_AND_ATTRIBUTES
*modified_privs
= NULL
;
1234 unsigned int priv_count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1235 unsigned int modified_priv_count
= 0;
1237 if (req
->get_modified_state
&& !req
->disable_all
)
1240 /* count modified privs */
1241 for (i
= 0; i
< priv_count
; i
++)
1243 struct privilege
*privilege
=
1244 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
1245 if (privilege
&& req
->get_modified_state
)
1246 modified_priv_count
++;
1248 reply
->len
= modified_priv_count
;
1249 modified_priv_count
= min( modified_priv_count
, get_reply_max_size() / sizeof(*modified_privs
) );
1250 if (modified_priv_count
)
1251 modified_privs
= set_reply_data_size( modified_priv_count
* sizeof(*modified_privs
) );
1253 reply
->len
= modified_priv_count
* sizeof(*modified_privs
);
1255 if (req
->disable_all
)
1256 token_disable_privileges( token
);
1258 token_adjust_privileges( token
, privs
, priv_count
, modified_privs
, modified_priv_count
);
1260 release_object( token
);
1264 /* retrieves the list of privileges that may be held be the token */
1265 DECL_HANDLER(get_token_privileges
)
1267 struct token
*token
;
1269 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1274 LUID_AND_ATTRIBUTES
*privs
;
1275 struct privilege
*privilege
;
1277 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1280 reply
->len
= priv_count
* sizeof(*privs
);
1281 if (reply
->len
<= get_reply_max_size())
1283 privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1287 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1289 luid_and_attr_from_privilege( &privs
[i
], privilege
);
1295 set_error(STATUS_BUFFER_TOO_SMALL
);
1297 release_object( token
);
1301 /* creates a duplicate of the token */
1302 DECL_HANDLER(duplicate_token
)
1304 struct token
*src_token
;
1305 struct unicode_str name
;
1306 const struct security_descriptor
*sd
;
1307 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1309 if (!objattr
) return;
1311 if ((src_token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1315 struct token
*token
= token_duplicate( src_token
, req
->primary
, req
->impersonation_level
, sd
);
1318 reply
->new_handle
= alloc_handle_no_access_check( current
->process
, token
, req
->access
, objattr
->attributes
);
1319 release_object( token
);
1321 release_object( src_token
);
1325 /* checks the specified privileges are held by the token */
1326 DECL_HANDLER(check_token_privileges
)
1328 struct token
*token
;
1330 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1334 unsigned int count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1336 if (!token
->primary
&& token
->impersonation_level
<= SecurityAnonymous
)
1337 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1338 else if (get_reply_max_size() >= count
* sizeof(LUID_AND_ATTRIBUTES
))
1340 LUID_AND_ATTRIBUTES
*usedprivs
= set_reply_data_size( count
* sizeof(*usedprivs
) );
1341 reply
->has_privileges
= token_check_privileges( token
, req
->all_required
, get_req_data(), count
, usedprivs
);
1344 set_error( STATUS_BUFFER_OVERFLOW
);
1345 release_object( token
);
1349 /* checks that a user represented by a token is allowed to access an object
1350 * represented by a security descriptor */
1351 DECL_HANDLER(access_check
)
1353 data_size_t sd_size
= get_req_data_size();
1354 const struct security_descriptor
*sd
= get_req_data();
1355 struct token
*token
;
1357 if (!sd_is_valid( sd
, sd_size
))
1359 set_error( STATUS_ACCESS_VIOLATION
);
1363 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1367 GENERIC_MAPPING mapping
;
1368 unsigned int status
;
1369 LUID_AND_ATTRIBUTES priv
;
1370 unsigned int priv_count
= 1;
1372 memset(&priv
, 0, sizeof(priv
));
1374 /* only impersonation tokens may be used with this function */
1377 set_error( STATUS_NO_IMPERSONATION_TOKEN
);
1378 release_object( token
);
1381 /* anonymous impersonation tokens can't be used */
1382 if (token
->impersonation_level
<= SecurityAnonymous
)
1384 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1385 release_object( token
);
1389 mapping
.GenericRead
= req
->mapping_read
;
1390 mapping
.GenericWrite
= req
->mapping_write
;
1391 mapping
.GenericExecute
= req
->mapping_execute
;
1392 mapping
.GenericAll
= req
->mapping_all
;
1394 status
= token_access_check(
1395 token
, sd
, req
->desired_access
, &priv
, &priv_count
, &mapping
,
1396 &reply
->access_granted
, &reply
->access_status
);
1398 reply
->privileges_len
= priv_count
*sizeof(LUID_AND_ATTRIBUTES
);
1400 if ((priv_count
> 0) && (reply
->privileges_len
<= get_reply_max_size()))
1402 LUID_AND_ATTRIBUTES
*privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1403 memcpy( privs
, &priv
, sizeof(priv
) );
1406 set_error( status
);
1407 release_object( token
);
1411 /* retrieves an SID from the token */
1412 DECL_HANDLER(get_token_sid
)
1414 struct token
*token
;
1418 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
, TOKEN_QUERY
, &token_ops
)))
1420 const SID
*sid
= NULL
;
1422 switch (req
->which_sid
)
1425 assert(token
->user
);
1428 case TokenPrimaryGroup
:
1429 sid
= token
->primary_group
;
1435 sid
= (const SID
*)&builtin_logon_sid
;
1438 set_error( STATUS_INVALID_PARAMETER
);
1444 reply
->sid_len
= security_sid_len( sid
);
1445 if (reply
->sid_len
<= get_reply_max_size()) set_reply_data( sid
, reply
->sid_len
);
1446 else set_error( STATUS_BUFFER_TOO_SMALL
);
1448 release_object( token
);
1452 /* retrieves the groups that the user represented by the token belongs to */
1453 DECL_HANDLER(get_token_groups
)
1455 struct token
*token
;
1457 reply
->user_len
= 0;
1459 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1463 size_t size_needed
= sizeof(struct token_groups
);
1464 size_t sid_size
= 0;
1465 unsigned int group_count
= 0;
1466 const struct group
*group
;
1468 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1471 sid_size
+= security_sid_len( &group
->sid
);
1473 size_needed
+= sid_size
;
1474 /* attributes size */
1475 size_needed
+= sizeof(unsigned int) * group_count
;
1477 /* reply buffer contains size_needed bytes formatted as:
1480 unsigned int attrib[count];
1483 user_len includes extra data needed for TOKEN_GROUPS representation,
1484 required caller buffer size calculated here to avoid extra server call */
1485 reply
->user_len
= FIELD_OFFSET( TOKEN_GROUPS
, Groups
[group_count
] ) + sid_size
;
1487 if (reply
->user_len
<= get_reply_max_size())
1489 struct token_groups
*tg
= set_reply_data_size( size_needed
);
1492 unsigned int *attr_ptr
= (unsigned int *)(tg
+ 1);
1493 SID
*sid_ptr
= (SID
*)(attr_ptr
+ group_count
);
1495 tg
->count
= group_count
;
1497 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1501 if (group
->mandatory
) *attr_ptr
|= SE_GROUP_MANDATORY
;
1502 if (group
->def
) *attr_ptr
|= SE_GROUP_ENABLED_BY_DEFAULT
;
1503 if (group
->enabled
) *attr_ptr
|= SE_GROUP_ENABLED
;
1504 if (group
->owner
) *attr_ptr
|= SE_GROUP_OWNER
;
1505 if (group
->deny_only
) *attr_ptr
|= SE_GROUP_USE_FOR_DENY_ONLY
;
1506 if (group
->resource
) *attr_ptr
|= SE_GROUP_RESOURCE
;
1507 if (group
->logon
) *attr_ptr
|= SE_GROUP_LOGON_ID
;
1509 memcpy(sid_ptr
, &group
->sid
, security_sid_len( &group
->sid
));
1511 sid_ptr
= (SID
*)((char *)sid_ptr
+ security_sid_len( &group
->sid
));
1516 else set_error( STATUS_BUFFER_TOO_SMALL
);
1518 release_object( token
);
1522 DECL_HANDLER(get_token_impersonation_level
)
1524 struct token
*token
;
1526 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1531 set_error( STATUS_INVALID_PARAMETER
);
1533 reply
->impersonation_level
= token
->impersonation_level
;
1535 release_object( token
);
1539 DECL_HANDLER(get_token_statistics
)
1541 struct token
*token
;
1543 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1547 reply
->token_id
= token
->token_id
;
1548 reply
->modified_id
= token
->modified_id
;
1549 reply
->primary
= token
->primary
;
1550 reply
->impersonation_level
= token
->impersonation_level
;
1551 reply
->group_count
= list_count( &token
->groups
);
1552 reply
->privilege_count
= list_count( &token
->privileges
);
1554 release_object( token
);
1558 DECL_HANDLER(get_token_default_dacl
)
1560 struct token
*token
;
1564 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1568 if (token
->default_dacl
)
1569 reply
->acl_len
= token
->default_dacl
->AclSize
;
1571 if (reply
->acl_len
<= get_reply_max_size())
1573 ACL
*acl_reply
= set_reply_data_size( reply
->acl_len
);
1575 memcpy( acl_reply
, token
->default_dacl
, reply
->acl_len
);
1577 else set_error( STATUS_BUFFER_TOO_SMALL
);
1579 release_object( token
);
1583 DECL_HANDLER(set_token_default_dacl
)
1585 struct token
*token
;
1587 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1588 TOKEN_ADJUST_DEFAULT
,
1591 const ACL
*acl
= get_req_data();
1592 unsigned int acl_size
= get_req_data_size();
1594 free( token
->default_dacl
);
1595 token
->default_dacl
= NULL
;
1598 token
->default_dacl
= memdup( acl
, acl_size
);
1600 release_object( token
);