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_lookup_name
, /* lookup_name */
156 no_link_name
, /* link_name */
157 NULL
, /* unlink_name */
158 no_open_file
, /* open_file */
159 no_kernel_obj_list
, /* get_kernel_obj_list */
160 no_close_handle
, /* close_handle */
161 token_destroy
/* destroy */
164 static void token_dump( struct object
*obj
, int verbose
)
166 struct token
*token
= (struct token
*)obj
;
167 assert( obj
->ops
== &token_ops
);
168 fprintf( stderr
, "Token id=%d.%u primary=%u impersonation level=%d\n", token
->token_id
.high_part
,
169 token
->token_id
.low_part
, token
->primary
, token
->impersonation_level
);
172 static struct object_type
*token_get_type( struct object
*obj
)
174 static const WCHAR name
[] = {'T','o','k','e','n'};
175 static const struct unicode_str str
= { name
, sizeof(name
) };
176 return get_object_type( &str
);
179 static unsigned int token_map_access( struct object
*obj
, unsigned int access
)
181 if (access
& GENERIC_READ
) access
|= TOKEN_READ
;
182 if (access
& GENERIC_WRITE
) access
|= TOKEN_WRITE
;
183 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
184 if (access
& GENERIC_ALL
) access
|= TOKEN_ALL_ACCESS
;
185 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
188 static SID
*security_sid_alloc( const SID_IDENTIFIER_AUTHORITY
*idauthority
, int subauthcount
, const unsigned int subauth
[] )
191 SID
*sid
= mem_alloc( FIELD_OFFSET(SID
, SubAuthority
[subauthcount
]) );
192 if (!sid
) return NULL
;
193 sid
->Revision
= SID_REVISION
;
194 sid
->SubAuthorityCount
= subauthcount
;
195 sid
->IdentifierAuthority
= *idauthority
;
196 for (i
= 0; i
< subauthcount
; i
++)
197 sid
->SubAuthority
[i
] = subauth
[i
];
201 void security_set_thread_token( struct thread
*thread
, obj_handle_t handle
)
206 release_object( thread
->token
);
207 thread
->token
= NULL
;
211 struct token
*token
= (struct token
*)get_handle_obj( current
->process
,
218 release_object( thread
->token
);
219 thread
->token
= token
;
224 const SID
*security_unix_uid_to_sid( uid_t uid
)
226 /* very simple mapping: either the current user or not the current user */
228 return (const SID
*)&local_user_sid
;
230 return &anonymous_logon_sid
;
233 static int acl_is_valid( const ACL
*acl
, data_size_t size
)
236 const ACE_HEADER
*ace
;
238 if (size
< sizeof(ACL
))
241 size
= min(size
, MAX_ACL_LEN
);
245 ace
= (const ACE_HEADER
*)(acl
+ 1);
246 for (i
= 0; i
< acl
->AceCount
; i
++)
249 data_size_t sid_size
;
251 if (size
< sizeof(ACE_HEADER
))
253 if (size
< ace
->AceSize
)
255 size
-= ace
->AceSize
;
256 switch (ace
->AceType
)
258 case ACCESS_DENIED_ACE_TYPE
:
259 sid
= (const SID
*)&((const ACCESS_DENIED_ACE
*)ace
)->SidStart
;
260 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_DENIED_ACE
, SidStart
);
262 case ACCESS_ALLOWED_ACE_TYPE
:
263 sid
= (const SID
*)&((const ACCESS_ALLOWED_ACE
*)ace
)->SidStart
;
264 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
);
266 case SYSTEM_AUDIT_ACE_TYPE
:
267 sid
= (const SID
*)&((const SYSTEM_AUDIT_ACE
*)ace
)->SidStart
;
268 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_AUDIT_ACE
, SidStart
);
270 case SYSTEM_ALARM_ACE_TYPE
:
271 sid
= (const SID
*)&((const SYSTEM_ALARM_ACE
*)ace
)->SidStart
;
272 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_ALARM_ACE
, SidStart
);
274 case SYSTEM_MANDATORY_LABEL_ACE_TYPE
:
275 sid
= (const SID
*)&((const SYSTEM_MANDATORY_LABEL_ACE
*)ace
)->SidStart
;
276 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
);
281 if (sid_size
< FIELD_OFFSET(SID
, SubAuthority
[0]) || sid_size
< security_sid_len( sid
))
283 ace
= ace_next( ace
);
288 /* checks whether all members of a security descriptor fit inside the size
289 * of memory specified */
290 int sd_is_valid( const struct security_descriptor
*sd
, data_size_t size
)
292 size_t offset
= sizeof(struct security_descriptor
);
302 if ((sd
->owner_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
303 (offset
+ sd
->owner_len
> size
))
305 owner
= sd_get_owner( sd
);
308 if ((sd
->owner_len
< sizeof(SID
)) || (security_sid_len( owner
) > sd
->owner_len
))
311 offset
+= sd
->owner_len
;
313 if ((sd
->group_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
314 (offset
+ sd
->group_len
> size
))
316 group
= sd_get_group( sd
);
319 if ((sd
->group_len
< sizeof(SID
)) || (security_sid_len( group
) > 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 owner and primary group */
594 if (!token
->primary_group
&& group
->owner
)
596 token
->owner
= &group
->sid
;
597 token
->primary_group
= &group
->sid
;
601 /* copy privileges */
602 for (i
= 0; i
< priv_count
; i
++)
604 /* note: we don't check uniqueness: the caller must make sure
605 * privs doesn't contain any duplicate luids */
606 if (!privilege_add( token
, &privs
[i
].Luid
,
607 privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
))
609 release_object( token
);
616 token
->default_dacl
= memdup( default_dacl
, default_dacl
->AclSize
);
617 if (!token
->default_dacl
)
619 release_object( token
);
624 token
->source
= source
;
629 struct token
*token_duplicate( struct token
*src_token
, unsigned primary
,
630 int impersonation_level
, const struct security_descriptor
*sd
)
632 const luid_t
*modified_id
=
633 primary
|| (impersonation_level
== src_token
->impersonation_level
) ?
634 &src_token
->modified_id
: NULL
;
635 struct token
*token
= NULL
;
636 struct privilege
*privilege
;
640 (impersonation_level
< SecurityAnonymous
||
641 impersonation_level
> SecurityDelegation
||
642 (!src_token
->primary
&& (impersonation_level
> src_token
->impersonation_level
))))
644 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
648 token
= create_token( primary
, src_token
->user
, NULL
, 0,
649 NULL
, 0, src_token
->default_dacl
,
650 src_token
->source
, modified_id
,
651 impersonation_level
);
652 if (!token
) return token
;
655 token
->primary_group
= NULL
;
656 LIST_FOR_EACH_ENTRY( group
, &src_token
->groups
, struct group
, entry
)
658 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[group
->sid
.SubAuthorityCount
] );
659 struct group
*newgroup
= mem_alloc( size
);
662 release_object( token
);
665 memcpy( newgroup
, group
, size
);
666 list_add_tail( &token
->groups
, &newgroup
->entry
);
667 if (src_token
->primary_group
== &group
->sid
)
669 token
->owner
= &newgroup
->sid
;
670 token
->primary_group
= &newgroup
->sid
;
673 assert( token
->primary_group
);
675 /* copy privileges */
676 LIST_FOR_EACH_ENTRY( privilege
, &src_token
->privileges
, struct privilege
, entry
)
677 if (!privilege_add( token
, &privilege
->luid
, privilege
->enabled
))
679 release_object( token
);
683 if (sd
) default_set_sd( &token
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
684 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
689 static ACL
*create_default_dacl( const SID
*user
)
691 ACCESS_ALLOWED_ACE
*aaa
;
694 size_t default_dacl_size
= sizeof(ACL
) +
695 2*(sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
696 sizeof(local_system_sid
) +
697 security_sid_len( user
);
699 default_dacl
= mem_alloc( default_dacl_size
);
700 if (!default_dacl
) return NULL
;
702 default_dacl
->AclRevision
= ACL_REVISION
;
703 default_dacl
->Sbz1
= 0;
704 default_dacl
->AclSize
= default_dacl_size
;
705 default_dacl
->AceCount
= 2;
706 default_dacl
->Sbz2
= 0;
708 /* GENERIC_ALL for Local System */
709 aaa
= (ACCESS_ALLOWED_ACE
*)(default_dacl
+ 1);
710 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
711 aaa
->Header
.AceFlags
= 0;
712 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
713 sizeof(local_system_sid
);
714 aaa
->Mask
= GENERIC_ALL
;
715 sid
= (SID
*)&aaa
->SidStart
;
716 memcpy( sid
, &local_system_sid
, sizeof(local_system_sid
) );
718 /* GENERIC_ALL for specified user */
719 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
720 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
721 aaa
->Header
.AceFlags
= 0;
722 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) + security_sid_len( user
);
723 aaa
->Mask
= GENERIC_ALL
;
724 sid
= (SID
*)&aaa
->SidStart
;
725 memcpy( sid
, user
, security_sid_len( user
));
732 SID_IDENTIFIER_AUTHORITY idauth
;
734 unsigned int subauth
[MAX_SUBAUTH_COUNT
];
737 static struct security_descriptor
*create_security_label_sd( struct token
*token
, PSID label_sid
)
739 size_t sid_len
= security_sid_len( label_sid
), sacl_size
, sd_size
;
740 SYSTEM_MANDATORY_LABEL_ACE
*smla
;
741 struct security_descriptor
*sd
;
744 sacl_size
= sizeof(ACL
) + FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
) + sid_len
;
745 sd_size
= sizeof(struct security_descriptor
) + sacl_size
;
746 if (!(sd
= mem_alloc( sd_size
)))
749 sd
->control
= SE_SACL_PRESENT
;
752 sd
->sacl_len
= sacl_size
;
755 sacl
= (ACL
*)(sd
+ 1);
756 sacl
->AclRevision
= ACL_REVISION
;
758 sacl
->AclSize
= sacl_size
;
762 smla
= (SYSTEM_MANDATORY_LABEL_ACE
*)(sacl
+ 1);
763 smla
->Header
.AceType
= SYSTEM_MANDATORY_LABEL_ACE_TYPE
;
764 smla
->Header
.AceFlags
= 0;
765 smla
->Header
.AceSize
= FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
) + sid_len
;
766 smla
->Mask
= SYSTEM_MANDATORY_LABEL_NO_WRITE_UP
;
767 memcpy( &smla
->SidStart
, label_sid
, sid_len
);
769 assert( sd_is_valid( sd
, sd_size
) );
773 int token_assign_label( struct token
*token
, PSID label
)
775 struct security_descriptor
*sd
;
778 if ((sd
= create_security_label_sd( token
, label
)))
780 ret
= set_sd_defaults_from_token( &token
->obj
, sd
, LABEL_SECURITY_INFORMATION
, token
);
787 struct token
*token_create_admin( void )
789 struct token
*token
= NULL
;
790 static const SID_IDENTIFIER_AUTHORITY nt_authority
= { SECURITY_NT_AUTHORITY
};
791 static const unsigned int alias_admins_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
};
792 static const unsigned int alias_users_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
};
793 /* on Windows, this value changes every time the user logs on */
794 static const unsigned int logon_subauth
[] = { SECURITY_LOGON_IDS_RID
, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
795 PSID alias_admins_sid
;
796 PSID alias_users_sid
;
798 const SID
*user_sid
= security_unix_uid_to_sid( getuid() );
799 ACL
*default_dacl
= create_default_dacl( user_sid
);
801 alias_admins_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( alias_admins_subauth
),
802 alias_admins_subauth
);
803 alias_users_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( alias_users_subauth
),
804 alias_users_subauth
);
805 logon_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( logon_subauth
), logon_subauth
);
807 if (alias_admins_sid
&& alias_users_sid
&& logon_sid
&& default_dacl
)
809 const LUID_AND_ATTRIBUTES admin_privs
[] =
811 { SeChangeNotifyPrivilege
, SE_PRIVILEGE_ENABLED
},
812 { SeSecurityPrivilege
, 0 },
813 { SeBackupPrivilege
, 0 },
814 { SeRestorePrivilege
, 0 },
815 { SeSystemtimePrivilege
, 0 },
816 { SeShutdownPrivilege
, 0 },
817 { SeRemoteShutdownPrivilege
, 0 },
818 { SeTakeOwnershipPrivilege
, 0 },
819 { SeDebugPrivilege
, 0 },
820 { SeSystemEnvironmentPrivilege
, 0 },
821 { SeSystemProfilePrivilege
, 0 },
822 { SeProfileSingleProcessPrivilege
, 0 },
823 { SeIncreaseBasePriorityPrivilege
, 0 },
824 { SeLoadDriverPrivilege
, SE_PRIVILEGE_ENABLED
},
825 { SeCreatePagefilePrivilege
, 0 },
826 { SeIncreaseQuotaPrivilege
, 0 },
827 { SeUndockPrivilege
, 0 },
828 { SeManageVolumePrivilege
, 0 },
829 { SeImpersonatePrivilege
, SE_PRIVILEGE_ENABLED
},
830 { SeCreateGlobalPrivilege
, SE_PRIVILEGE_ENABLED
},
832 /* note: we don't include non-builtin groups here for the user -
833 * telling us these is the job of a client-side program */
834 const SID_AND_ATTRIBUTES admin_groups
[] =
836 { security_world_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
837 { security_local_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
838 { security_interactive_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
839 { security_authenticated_user_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
840 { security_domain_users_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_OWNER
},
841 { alias_admins_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_OWNER
},
842 { alias_users_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
843 { logon_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_LOGON_ID
},
845 static const TOKEN_SOURCE admin_source
= {"SeMgr", {0, 0}};
846 token
= create_token( TRUE
, user_sid
, admin_groups
, ARRAY_SIZE( admin_groups
),
847 admin_privs
, ARRAY_SIZE( admin_privs
), default_dacl
,
848 admin_source
, NULL
, -1 );
849 /* we really need a primary group */
850 assert( token
->primary_group
);
854 free( alias_admins_sid
);
855 free( alias_users_sid
);
856 free( default_dacl
);
861 static struct privilege
*token_find_privilege( struct token
*token
, const LUID
*luid
, int enabled_only
)
863 struct privilege
*privilege
;
864 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
866 if (is_equal_luid( luid
, &privilege
->luid
))
868 if (enabled_only
&& !privilege
->enabled
)
876 static unsigned int token_adjust_privileges( struct token
*token
, const LUID_AND_ATTRIBUTES
*privs
,
877 unsigned int count
, LUID_AND_ATTRIBUTES
*mod_privs
,
878 unsigned int mod_privs_count
)
880 unsigned int i
, modified_count
= 0;
882 /* mark as modified */
883 allocate_luid( &token
->modified_id
);
885 for (i
= 0; i
< count
; i
++)
887 struct privilege
*privilege
=
888 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
891 set_error( STATUS_NOT_ALL_ASSIGNED
);
895 if (privs
[i
].Attributes
& SE_PRIVILEGE_REMOVED
)
896 privilege_remove( privilege
);
899 /* save previous state for caller */
902 luid_and_attr_from_privilege(mod_privs
, privilege
);
908 if (privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
)
909 privilege
->enabled
= TRUE
;
911 privilege
->enabled
= FALSE
;
914 return modified_count
;
917 static void token_disable_privileges( struct token
*token
)
919 struct privilege
*privilege
;
921 /* mark as modified */
922 allocate_luid( &token
->modified_id
);
924 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
925 privilege
->enabled
= FALSE
;
928 int token_check_privileges( struct token
*token
, int all_required
,
929 const LUID_AND_ATTRIBUTES
*reqprivs
,
930 unsigned int count
, LUID_AND_ATTRIBUTES
*usedprivs
)
932 unsigned int i
, enabled_count
= 0;
934 for (i
= 0; i
< count
; i
++)
936 struct privilege
*privilege
=
937 token_find_privilege( token
, &reqprivs
[i
].Luid
, TRUE
);
940 usedprivs
[i
] = reqprivs
[i
];
942 if (privilege
&& privilege
->enabled
)
946 usedprivs
[i
].Attributes
|= SE_PRIVILEGE_USED_FOR_ACCESS
;
951 return (enabled_count
== count
);
953 return (enabled_count
> 0);
956 int token_sid_present( struct token
*token
, const SID
*sid
, int deny
)
960 if (security_equal_sid( token
->user
, sid
)) return TRUE
;
962 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, struct group
, entry
)
964 if (!group
->enabled
) continue;
965 if (group
->deny_only
&& !deny
) continue;
967 if (security_equal_sid( &group
->sid
, sid
)) return TRUE
;
973 /* Checks access to a security descriptor. 'sd' must have been validated by
974 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
975 * the reason. 'status' parameter will indicate if access is granted or denied.
977 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
979 static unsigned int token_access_check( struct token
*token
,
980 const struct security_descriptor
*sd
,
981 unsigned int desired_access
,
982 LUID_AND_ATTRIBUTES
*privs
,
983 unsigned int *priv_count
,
984 const GENERIC_MAPPING
*mapping
,
985 unsigned int *granted_access
,
986 unsigned int *status
)
988 unsigned int current_access
= 0;
989 unsigned int denied_access
= 0;
993 const ACE_HEADER
*ace
;
996 /* assume no access rights */
999 /* fail if desired_access contains generic rights */
1000 if (desired_access
& (GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
|GENERIC_ALL
))
1002 if (priv_count
) *priv_count
= 0;
1003 return STATUS_GENERIC_NOT_MAPPED
;
1006 dacl
= sd_get_dacl( sd
, &dacl_present
);
1007 owner
= sd_get_owner( sd
);
1008 if (!owner
|| !sd_get_group( sd
))
1010 if (priv_count
) *priv_count
= 0;
1011 return STATUS_INVALID_SECURITY_DESCR
;
1014 /* 1: Grant desired access if the object is unprotected */
1015 if (!dacl_present
|| !dacl
)
1017 if (priv_count
) *priv_count
= 0;
1018 if (desired_access
& MAXIMUM_ALLOWED
)
1019 *granted_access
= mapping
->GenericAll
;
1021 *granted_access
= desired_access
;
1022 return *status
= STATUS_SUCCESS
;
1025 /* 2: Check if caller wants access to system security part. Note: access
1026 * is only granted if specifically asked for */
1027 if (desired_access
& ACCESS_SYSTEM_SECURITY
)
1029 const LUID_AND_ATTRIBUTES security_priv
= { SeSecurityPrivilege
, 0 };
1030 LUID_AND_ATTRIBUTES retpriv
= security_priv
;
1031 if (token_check_privileges( token
, TRUE
, &security_priv
, 1, &retpriv
))
1035 /* assumes that there will only be one privilege to return */
1036 if (*priv_count
>= 1)
1044 return STATUS_BUFFER_TOO_SMALL
;
1047 current_access
|= ACCESS_SYSTEM_SECURITY
;
1048 if (desired_access
== current_access
)
1050 *granted_access
= current_access
;
1051 return *status
= STATUS_SUCCESS
;
1056 if (priv_count
) *priv_count
= 0;
1057 *status
= STATUS_PRIVILEGE_NOT_HELD
;
1058 return STATUS_SUCCESS
;
1061 else if (priv_count
) *priv_count
= 0;
1063 /* 3: Check whether the token is the owner */
1064 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
1065 * checked when a "set owner" call is made, overriding the access rights
1066 * determined here. */
1067 if (token_sid_present( token
, owner
, FALSE
))
1069 current_access
|= (READ_CONTROL
| WRITE_DAC
);
1070 if (desired_access
== current_access
)
1072 *granted_access
= current_access
;
1073 return *status
= STATUS_SUCCESS
;
1077 /* 4: Grant rights according to the DACL */
1078 ace
= (const ACE_HEADER
*)(dacl
+ 1);
1079 for (i
= 0; i
< dacl
->AceCount
; i
++, ace
= ace_next( ace
))
1081 const ACCESS_ALLOWED_ACE
*aa_ace
;
1082 const ACCESS_DENIED_ACE
*ad_ace
;
1085 if (ace
->AceFlags
& INHERIT_ONLY_ACE
)
1088 switch (ace
->AceType
)
1090 case ACCESS_DENIED_ACE_TYPE
:
1091 ad_ace
= (const ACCESS_DENIED_ACE
*)ace
;
1092 sid
= (const SID
*)&ad_ace
->SidStart
;
1093 if (token_sid_present( token
, sid
, TRUE
))
1095 unsigned int access
= ad_ace
->Mask
;
1096 map_generic_mask(&access
, mapping
);
1097 if (desired_access
& MAXIMUM_ALLOWED
)
1098 denied_access
|= access
;
1101 denied_access
|= (access
& ~current_access
);
1102 if (desired_access
& access
) goto done
;
1106 case ACCESS_ALLOWED_ACE_TYPE
:
1107 aa_ace
= (const ACCESS_ALLOWED_ACE
*)ace
;
1108 sid
= (const SID
*)&aa_ace
->SidStart
;
1109 if (token_sid_present( token
, sid
, FALSE
))
1111 unsigned int access
= aa_ace
->Mask
;
1112 map_generic_mask(&access
, mapping
);
1113 if (desired_access
& MAXIMUM_ALLOWED
)
1114 current_access
|= access
;
1116 current_access
|= (access
& ~denied_access
);
1121 /* don't bother carrying on checking if we've already got all of
1123 if (desired_access
== *granted_access
)
1128 if (desired_access
& MAXIMUM_ALLOWED
)
1129 *granted_access
= current_access
& ~denied_access
;
1131 if ((current_access
& desired_access
) == desired_access
)
1132 *granted_access
= current_access
& desired_access
;
1134 *granted_access
= 0;
1136 *status
= *granted_access
? STATUS_SUCCESS
: STATUS_ACCESS_DENIED
;
1137 return STATUS_SUCCESS
;
1140 const ACL
*token_get_default_dacl( struct token
*token
)
1142 return token
->default_dacl
;
1145 const SID
*token_get_user( struct token
*token
)
1150 const SID
*token_get_primary_group( struct token
*token
)
1152 return token
->primary_group
;
1155 int check_object_access(struct object
*obj
, unsigned int *access
)
1157 GENERIC_MAPPING mapping
;
1158 struct token
*token
= current
->token
? current
->token
: current
->process
->token
;
1159 unsigned int status
;
1162 mapping
.GenericAll
= obj
->ops
->map_access( obj
, GENERIC_ALL
);
1166 if (*access
& MAXIMUM_ALLOWED
)
1167 *access
= mapping
.GenericAll
;
1171 mapping
.GenericRead
= obj
->ops
->map_access( obj
, GENERIC_READ
);
1172 mapping
.GenericWrite
= obj
->ops
->map_access( obj
, GENERIC_WRITE
);
1173 mapping
.GenericExecute
= obj
->ops
->map_access( obj
, GENERIC_EXECUTE
);
1175 res
= token_access_check( token
, obj
->sd
, *access
, NULL
, NULL
,
1176 &mapping
, access
, &status
) == STATUS_SUCCESS
&&
1177 status
== STATUS_SUCCESS
;
1179 if (!res
) set_error( STATUS_ACCESS_DENIED
);
1184 /* open a security token */
1185 DECL_HANDLER(open_token
)
1187 if (req
->flags
& OPEN_TOKEN_THREAD
)
1189 struct thread
*thread
= get_thread_from_handle( req
->handle
, 0 );
1194 if (!thread
->token
->primary
&& thread
->token
->impersonation_level
<= SecurityAnonymous
)
1195 set_error( STATUS_CANT_OPEN_ANONYMOUS
);
1197 reply
->token
= alloc_handle( current
->process
, thread
->token
,
1198 req
->access
, req
->attributes
);
1201 set_error( STATUS_NO_TOKEN
);
1202 release_object( thread
);
1207 struct process
*process
= get_process_from_handle( req
->handle
, 0 );
1211 reply
->token
= alloc_handle( current
->process
, process
->token
, req
->access
,
1214 set_error( STATUS_NO_TOKEN
);
1215 release_object( process
);
1220 /* adjust the privileges held by a token */
1221 DECL_HANDLER(adjust_token_privileges
)
1223 struct token
*token
;
1224 unsigned int access
= TOKEN_ADJUST_PRIVILEGES
;
1226 if (req
->get_modified_state
) access
|= TOKEN_QUERY
;
1228 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1229 access
, &token_ops
)))
1231 const LUID_AND_ATTRIBUTES
*privs
= get_req_data();
1232 LUID_AND_ATTRIBUTES
*modified_privs
= NULL
;
1233 unsigned int priv_count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1234 unsigned int modified_priv_count
= 0;
1236 if (req
->get_modified_state
&& !req
->disable_all
)
1239 /* count modified privs */
1240 for (i
= 0; i
< priv_count
; i
++)
1242 struct privilege
*privilege
=
1243 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
1244 if (privilege
&& req
->get_modified_state
)
1245 modified_priv_count
++;
1247 reply
->len
= modified_priv_count
;
1248 modified_priv_count
= min( modified_priv_count
, get_reply_max_size() / sizeof(*modified_privs
) );
1249 if (modified_priv_count
)
1250 modified_privs
= set_reply_data_size( modified_priv_count
* sizeof(*modified_privs
) );
1252 reply
->len
= modified_priv_count
* sizeof(*modified_privs
);
1254 if (req
->disable_all
)
1255 token_disable_privileges( token
);
1257 token_adjust_privileges( token
, privs
, priv_count
, modified_privs
, modified_priv_count
);
1259 release_object( token
);
1263 /* retrieves the list of privileges that may be held be the token */
1264 DECL_HANDLER(get_token_privileges
)
1266 struct token
*token
;
1268 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1273 LUID_AND_ATTRIBUTES
*privs
;
1274 struct privilege
*privilege
;
1276 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1279 reply
->len
= priv_count
* sizeof(*privs
);
1280 if (reply
->len
<= get_reply_max_size())
1282 privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1286 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1288 luid_and_attr_from_privilege( &privs
[i
], privilege
);
1294 set_error(STATUS_BUFFER_TOO_SMALL
);
1296 release_object( token
);
1300 /* creates a duplicate of the token */
1301 DECL_HANDLER(duplicate_token
)
1303 struct token
*src_token
;
1304 struct unicode_str name
;
1305 const struct security_descriptor
*sd
;
1306 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1308 if (!objattr
) return;
1310 if ((src_token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1314 struct token
*token
= token_duplicate( src_token
, req
->primary
, req
->impersonation_level
, sd
);
1317 reply
->new_handle
= alloc_handle_no_access_check( current
->process
, token
, req
->access
, objattr
->attributes
);
1318 release_object( token
);
1320 release_object( src_token
);
1324 /* checks the specified privileges are held by the token */
1325 DECL_HANDLER(check_token_privileges
)
1327 struct token
*token
;
1329 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1333 unsigned int count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1335 if (!token
->primary
&& token
->impersonation_level
<= SecurityAnonymous
)
1336 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1337 else if (get_reply_max_size() >= count
* sizeof(LUID_AND_ATTRIBUTES
))
1339 LUID_AND_ATTRIBUTES
*usedprivs
= set_reply_data_size( count
* sizeof(*usedprivs
) );
1340 reply
->has_privileges
= token_check_privileges( token
, req
->all_required
, get_req_data(), count
, usedprivs
);
1343 set_error( STATUS_BUFFER_OVERFLOW
);
1344 release_object( token
);
1348 /* checks that a user represented by a token is allowed to access an object
1349 * represented by a security descriptor */
1350 DECL_HANDLER(access_check
)
1352 data_size_t sd_size
= get_req_data_size();
1353 const struct security_descriptor
*sd
= get_req_data();
1354 struct token
*token
;
1356 if (!sd_is_valid( sd
, sd_size
))
1358 set_error( STATUS_ACCESS_VIOLATION
);
1362 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1366 GENERIC_MAPPING mapping
;
1367 unsigned int status
;
1368 LUID_AND_ATTRIBUTES priv
;
1369 unsigned int priv_count
= 1;
1371 memset(&priv
, 0, sizeof(priv
));
1373 /* only impersonation tokens may be used with this function */
1376 set_error( STATUS_NO_IMPERSONATION_TOKEN
);
1377 release_object( token
);
1380 /* anonymous impersonation tokens can't be used */
1381 if (token
->impersonation_level
<= SecurityAnonymous
)
1383 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1384 release_object( token
);
1388 mapping
.GenericRead
= req
->mapping_read
;
1389 mapping
.GenericWrite
= req
->mapping_write
;
1390 mapping
.GenericExecute
= req
->mapping_execute
;
1391 mapping
.GenericAll
= req
->mapping_all
;
1393 status
= token_access_check(
1394 token
, sd
, req
->desired_access
, &priv
, &priv_count
, &mapping
,
1395 &reply
->access_granted
, &reply
->access_status
);
1397 reply
->privileges_len
= priv_count
*sizeof(LUID_AND_ATTRIBUTES
);
1399 if ((priv_count
> 0) && (reply
->privileges_len
<= get_reply_max_size()))
1401 LUID_AND_ATTRIBUTES
*privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1402 memcpy( privs
, &priv
, sizeof(priv
) );
1405 set_error( status
);
1406 release_object( token
);
1410 /* retrieves an SID from the token */
1411 DECL_HANDLER(get_token_sid
)
1413 struct token
*token
;
1417 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
, TOKEN_QUERY
, &token_ops
)))
1419 const SID
*sid
= NULL
;
1421 switch (req
->which_sid
)
1424 assert(token
->user
);
1427 case TokenPrimaryGroup
:
1428 sid
= token
->primary_group
;
1434 sid
= (const SID
*)&builtin_logon_sid
;
1437 set_error( STATUS_INVALID_PARAMETER
);
1443 reply
->sid_len
= security_sid_len( sid
);
1444 if (reply
->sid_len
<= get_reply_max_size()) set_reply_data( sid
, reply
->sid_len
);
1445 else set_error( STATUS_BUFFER_TOO_SMALL
);
1447 release_object( token
);
1451 /* retrieves the groups that the user represented by the token belongs to */
1452 DECL_HANDLER(get_token_groups
)
1454 struct token
*token
;
1456 reply
->user_len
= 0;
1458 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1462 size_t size_needed
= sizeof(struct token_groups
);
1463 size_t sid_size
= 0;
1464 unsigned int group_count
= 0;
1465 const struct group
*group
;
1467 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1470 sid_size
+= security_sid_len( &group
->sid
);
1472 size_needed
+= sid_size
;
1473 /* attributes size */
1474 size_needed
+= sizeof(unsigned int) * group_count
;
1476 /* reply buffer contains size_needed bytes formatted as:
1479 unsigned int attrib[count];
1482 user_len includes extra data needed for TOKEN_GROUPS representation,
1483 required caller buffer size calculated here to avoid extra server call */
1484 reply
->user_len
= FIELD_OFFSET( TOKEN_GROUPS
, Groups
[group_count
] ) + sid_size
;
1486 if (reply
->user_len
<= get_reply_max_size())
1488 struct token_groups
*tg
= set_reply_data_size( size_needed
);
1491 unsigned int *attr_ptr
= (unsigned int *)(tg
+ 1);
1492 SID
*sid_ptr
= (SID
*)(attr_ptr
+ group_count
);
1494 tg
->count
= group_count
;
1496 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1500 if (group
->mandatory
) *attr_ptr
|= SE_GROUP_MANDATORY
;
1501 if (group
->def
) *attr_ptr
|= SE_GROUP_ENABLED_BY_DEFAULT
;
1502 if (group
->enabled
) *attr_ptr
|= SE_GROUP_ENABLED
;
1503 if (group
->owner
) *attr_ptr
|= SE_GROUP_OWNER
;
1504 if (group
->deny_only
) *attr_ptr
|= SE_GROUP_USE_FOR_DENY_ONLY
;
1505 if (group
->resource
) *attr_ptr
|= SE_GROUP_RESOURCE
;
1506 if (group
->logon
) *attr_ptr
|= SE_GROUP_LOGON_ID
;
1508 memcpy(sid_ptr
, &group
->sid
, security_sid_len( &group
->sid
));
1510 sid_ptr
= (SID
*)((char *)sid_ptr
+ security_sid_len( &group
->sid
));
1515 else set_error( STATUS_BUFFER_TOO_SMALL
);
1517 release_object( token
);
1521 DECL_HANDLER(get_token_impersonation_level
)
1523 struct token
*token
;
1525 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1530 set_error( STATUS_INVALID_PARAMETER
);
1532 reply
->impersonation_level
= token
->impersonation_level
;
1534 release_object( token
);
1538 DECL_HANDLER(get_token_statistics
)
1540 struct token
*token
;
1542 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1546 reply
->token_id
= token
->token_id
;
1547 reply
->modified_id
= token
->modified_id
;
1548 reply
->primary
= token
->primary
;
1549 reply
->impersonation_level
= token
->impersonation_level
;
1550 reply
->group_count
= list_count( &token
->groups
);
1551 reply
->privilege_count
= list_count( &token
->privileges
);
1553 release_object( token
);
1557 DECL_HANDLER(get_token_default_dacl
)
1559 struct token
*token
;
1563 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1567 if (token
->default_dacl
)
1568 reply
->acl_len
= token
->default_dacl
->AclSize
;
1570 if (reply
->acl_len
<= get_reply_max_size())
1572 ACL
*acl_reply
= set_reply_data_size( reply
->acl_len
);
1574 memcpy( acl_reply
, token
->default_dacl
, reply
->acl_len
);
1576 else set_error( STATUS_BUFFER_TOO_SMALL
);
1578 release_object( token
);
1582 DECL_HANDLER(set_token_default_dacl
)
1584 struct token
*token
;
1586 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1587 TOKEN_ADJUST_DEFAULT
,
1590 const ACL
*acl
= get_req_data();
1591 unsigned int acl_size
= get_req_data_size();
1593 free( token
->default_dacl
);
1594 token
->default_dacl
= NULL
;
1597 token
->default_dacl
= memdup( acl
, acl_size
);
1599 release_object( token
);