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 #define SID_N(n) struct /* same fields as struct SID */ \
70 BYTE SubAuthorityCount; \
71 SID_IDENTIFIER_AUTHORITY IdentifierAuthority; \
72 DWORD SubAuthority[n]; \
75 static const SID world_sid
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} };
76 static const SID local_sid
= { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} };
77 static const SID interactive_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} };
78 static const SID anonymous_logon_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} };
79 static const SID authenticated_user_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} };
80 static const SID local_system_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} };
81 static const SID high_label_sid
= { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_HIGH_RID
} };
82 static const SID_N(5) local_user_sid
= { SID_REVISION
, 5, { SECURITY_NT_AUTHORITY
}, { SECURITY_NT_NON_UNIQUE
, 0, 0, 0, 1000 } };
83 static const SID_N(2) builtin_admins_sid
= { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} };
84 static const SID_N(2) builtin_users_sid
= { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} };
85 static const SID_N(3) builtin_logon_sid
= { SID_REVISION
, 3, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOGON_IDS_RID
, 0, 0 } };
87 const PSID security_world_sid
= (PSID
)&world_sid
;
88 static const PSID security_local_sid
= (PSID
)&local_sid
;
89 static const PSID security_interactive_sid
= (PSID
)&interactive_sid
;
90 static const PSID security_authenticated_user_sid
= (PSID
)&authenticated_user_sid
;
91 const PSID security_local_system_sid
= (PSID
)&local_system_sid
;
92 const PSID security_local_user_sid
= (PSID
)&local_user_sid
;
93 const PSID security_builtin_admins_sid
= (PSID
)&builtin_admins_sid
;
94 const PSID security_builtin_users_sid
= (PSID
)&builtin_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_close_handle
, /* close_handle */
160 token_destroy
/* destroy */
163 static void token_dump( struct object
*obj
, int verbose
)
165 struct token
*token
= (struct token
*)obj
;
166 assert( obj
->ops
== &token_ops
);
167 fprintf( stderr
, "Token id=%d.%u primary=%u impersonation level=%d\n", token
->token_id
.high_part
,
168 token
->token_id
.low_part
, token
->primary
, token
->impersonation_level
);
171 static struct object_type
*token_get_type( struct object
*obj
)
173 static const WCHAR name
[] = {'T','o','k','e','n'};
174 static const struct unicode_str str
= { name
, sizeof(name
) };
175 return get_object_type( &str
);
178 static unsigned int token_map_access( struct object
*obj
, unsigned int access
)
180 if (access
& GENERIC_READ
) access
|= TOKEN_READ
;
181 if (access
& GENERIC_WRITE
) access
|= TOKEN_WRITE
;
182 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
183 if (access
& GENERIC_ALL
) access
|= TOKEN_ALL_ACCESS
;
184 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
187 static SID
*security_sid_alloc( const SID_IDENTIFIER_AUTHORITY
*idauthority
, int subauthcount
, const unsigned int subauth
[] )
190 SID
*sid
= mem_alloc( FIELD_OFFSET(SID
, SubAuthority
[subauthcount
]) );
191 if (!sid
) return NULL
;
192 sid
->Revision
= SID_REVISION
;
193 sid
->SubAuthorityCount
= subauthcount
;
194 sid
->IdentifierAuthority
= *idauthority
;
195 for (i
= 0; i
< subauthcount
; i
++)
196 sid
->SubAuthority
[i
] = subauth
[i
];
200 void security_set_thread_token( struct thread
*thread
, obj_handle_t handle
)
205 release_object( thread
->token
);
206 thread
->token
= NULL
;
210 struct token
*token
= (struct token
*)get_handle_obj( current
->process
,
217 release_object( thread
->token
);
218 thread
->token
= token
;
223 const SID
*security_unix_uid_to_sid( uid_t uid
)
225 /* very simple mapping: either the current user or not the current user */
227 return (const SID
*)&local_user_sid
;
229 return &anonymous_logon_sid
;
232 static int acl_is_valid( const ACL
*acl
, data_size_t size
)
235 const ACE_HEADER
*ace
;
237 if (size
< sizeof(ACL
))
240 size
= min(size
, MAX_ACL_LEN
);
244 ace
= (const ACE_HEADER
*)(acl
+ 1);
245 for (i
= 0; i
< acl
->AceCount
; i
++)
248 data_size_t sid_size
;
250 if (size
< sizeof(ACE_HEADER
))
252 if (size
< ace
->AceSize
)
254 size
-= ace
->AceSize
;
255 switch (ace
->AceType
)
257 case ACCESS_DENIED_ACE_TYPE
:
258 sid
= (const SID
*)&((const ACCESS_DENIED_ACE
*)ace
)->SidStart
;
259 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_DENIED_ACE
, SidStart
);
261 case ACCESS_ALLOWED_ACE_TYPE
:
262 sid
= (const SID
*)&((const ACCESS_ALLOWED_ACE
*)ace
)->SidStart
;
263 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
);
265 case SYSTEM_AUDIT_ACE_TYPE
:
266 sid
= (const SID
*)&((const SYSTEM_AUDIT_ACE
*)ace
)->SidStart
;
267 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_AUDIT_ACE
, SidStart
);
269 case SYSTEM_ALARM_ACE_TYPE
:
270 sid
= (const SID
*)&((const SYSTEM_ALARM_ACE
*)ace
)->SidStart
;
271 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_ALARM_ACE
, SidStart
);
273 case SYSTEM_MANDATORY_LABEL_ACE_TYPE
:
274 sid
= (const SID
*)&((const SYSTEM_MANDATORY_LABEL_ACE
*)ace
)->SidStart
;
275 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
);
280 if (sid_size
< FIELD_OFFSET(SID
, SubAuthority
[0]) || sid_size
< security_sid_len( sid
))
282 ace
= ace_next( ace
);
287 /* checks whether all members of a security descriptor fit inside the size
288 * of memory specified */
289 int sd_is_valid( const struct security_descriptor
*sd
, data_size_t size
)
291 size_t offset
= sizeof(struct security_descriptor
);
301 if ((sd
->owner_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
302 (offset
+ sd
->owner_len
> size
))
304 owner
= sd_get_owner( sd
);
307 size_t needed_size
= security_sid_len( owner
);
308 if ((sd
->owner_len
< sizeof(SID
)) || (needed_size
> 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 size_t needed_size
= security_sid_len( group
);
320 if ((sd
->group_len
< sizeof(SID
)) || (needed_size
> 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 { 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
);