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 SeTcbPrivilege
= { 7, 0 };
46 const LUID SeSecurityPrivilege
= { 8, 0 };
47 const LUID SeTakeOwnershipPrivilege
= { 9, 0 };
48 const LUID SeLoadDriverPrivilege
= { 10, 0 };
49 const LUID SeSystemProfilePrivilege
= { 11, 0 };
50 const LUID SeSystemtimePrivilege
= { 12, 0 };
51 const LUID SeProfileSingleProcessPrivilege
= { 13, 0 };
52 const LUID SeIncreaseBasePriorityPrivilege
= { 14, 0 };
53 const LUID SeCreatePagefilePrivilege
= { 15, 0 };
54 const LUID SeBackupPrivilege
= { 17, 0 };
55 const LUID SeRestorePrivilege
= { 18, 0 };
56 const LUID SeShutdownPrivilege
= { 19, 0 };
57 const LUID SeDebugPrivilege
= { 20, 0 };
58 const LUID SeSystemEnvironmentPrivilege
= { 22, 0 };
59 const LUID SeChangeNotifyPrivilege
= { 23, 0 };
60 const LUID SeRemoteShutdownPrivilege
= { 24, 0 };
61 const LUID SeUndockPrivilege
= { 25, 0 };
62 const LUID SeManageVolumePrivilege
= { 28, 0 };
63 const LUID SeImpersonatePrivilege
= { 29, 0 };
64 const LUID SeCreateGlobalPrivilege
= { 30, 0 };
66 #define SID_N(n) struct /* same fields as struct SID */ \
69 BYTE SubAuthorityCount; \
70 SID_IDENTIFIER_AUTHORITY IdentifierAuthority; \
71 DWORD SubAuthority[n]; \
74 static const SID world_sid
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} };
75 static const SID local_sid
= { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} };
76 static const SID interactive_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} };
77 static const SID anonymous_logon_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} };
78 static const SID authenticated_user_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} };
79 static const SID local_system_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} };
80 static const SID high_label_sid
= { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_HIGH_RID
} };
81 static const SID_N(5) local_user_sid
= { SID_REVISION
, 5, { SECURITY_NT_AUTHORITY
}, { SECURITY_NT_NON_UNIQUE
, 0, 0, 0, 1000 } };
82 static const SID_N(2) builtin_admins_sid
= { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} };
83 static const SID_N(2) builtin_users_sid
= { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} };
84 static const SID_N(3) builtin_logon_sid
= { SID_REVISION
, 3, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOGON_IDS_RID
, 0, 0 } };
85 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
} };
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_domain_users_sid
= (PSID
)&domain_users_sid
;
96 const PSID security_high_label_sid
= (PSID
)&high_label_sid
;
98 static luid_t prev_luid_value
= { 1000, 0 };
100 static const WCHAR token_name
[] = {'T','o','k','e','n'};
102 struct type_descr token_type
=
104 { token_name
, sizeof(token_name
) }, /* name */
105 TOKEN_ALL_ACCESS
| SYNCHRONIZE
, /* valid_access */
107 STANDARD_RIGHTS_READ
| TOKEN_QUERY_SOURCE
| TOKEN_QUERY
| TOKEN_DUPLICATE
,
108 STANDARD_RIGHTS_WRITE
| TOKEN_ADJUST_SESSIONID
| TOKEN_ADJUST_DEFAULT
| TOKEN_ADJUST_GROUPS
109 | TOKEN_ADJUST_PRIVILEGES
,
110 STANDARD_RIGHTS_EXECUTE
| TOKEN_IMPERSONATE
| TOKEN_ASSIGN_PRIMARY
,
117 struct object obj
; /* object header */
118 luid_t token_id
; /* system-unique id of token */
119 luid_t modified_id
; /* new id allocated every time token is modified */
120 struct list privileges
; /* privileges available to the token */
121 struct list groups
; /* groups that the user of this token belongs to (sid_and_attributes) */
122 SID
*user
; /* SID of user this token represents */
123 SID
*owner
; /* SID of owner (points to user or one of groups) */
124 SID
*primary_group
; /* SID of user's primary group (points to one of groups) */
125 unsigned primary
; /* is this a primary or impersonation token? */
126 ACL
*default_dacl
; /* the default DACL to assign to objects created by this user */
127 TOKEN_SOURCE source
; /* source of the token */
128 int impersonation_level
; /* impersonation level this token is capable of if non-primary token */
135 unsigned enabled
: 1; /* is the privilege currently enabled? */
136 unsigned def
: 1; /* is the privilege enabled by default? */
142 unsigned enabled
: 1; /* is the sid currently enabled? */
143 unsigned def
: 1; /* is the sid enabled by default? */
144 unsigned logon
: 1; /* is this a logon sid? */
145 unsigned mandatory
: 1; /* is this sid always enabled? */
146 unsigned owner
: 1; /* can this sid be an owner of an object? */
147 unsigned resource
: 1; /* is this a domain-local group? */
148 unsigned deny_only
: 1; /* is this a sid that should be use for denying only? */
152 static void token_dump( struct object
*obj
, int verbose
);
153 static void token_destroy( struct object
*obj
);
155 static const struct object_ops token_ops
=
157 sizeof(struct token
), /* size */
158 &token_type
, /* type */
159 token_dump
, /* dump */
160 no_add_queue
, /* add_queue */
161 NULL
, /* remove_queue */
163 NULL
, /* satisfied */
164 no_signal
, /* signal */
165 no_get_fd
, /* get_fd */
166 default_map_access
, /* map_access */
167 default_get_sd
, /* get_sd */
168 default_set_sd
, /* set_sd */
169 no_get_full_name
, /* get_full_name */
170 no_lookup_name
, /* lookup_name */
171 no_link_name
, /* link_name */
172 NULL
, /* unlink_name */
173 no_open_file
, /* open_file */
174 no_kernel_obj_list
, /* get_kernel_obj_list */
175 no_close_handle
, /* close_handle */
176 token_destroy
/* destroy */
179 static void token_dump( struct object
*obj
, int verbose
)
181 struct token
*token
= (struct token
*)obj
;
182 assert( obj
->ops
== &token_ops
);
183 fprintf( stderr
, "Token id=%d.%u primary=%u impersonation level=%d\n", token
->token_id
.high_part
,
184 token
->token_id
.low_part
, token
->primary
, token
->impersonation_level
);
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 static unsigned int get_sid_count( const SID
*sid
, data_size_t size
)
291 for (count
= 0; size
>= sizeof(SID
) && security_sid_len( sid
) <= size
; count
++)
293 size
-= security_sid_len( sid
);
294 sid
= (const SID
*)((char *)sid
+ security_sid_len( sid
));
300 /* checks whether all members of a security descriptor fit inside the size
301 * of memory specified */
302 int sd_is_valid( const struct security_descriptor
*sd
, data_size_t size
)
304 size_t offset
= sizeof(struct security_descriptor
);
314 if ((sd
->owner_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
315 (offset
+ sd
->owner_len
> size
))
317 owner
= sd_get_owner( sd
);
320 if ((sd
->owner_len
< sizeof(SID
)) || (security_sid_len( owner
) > sd
->owner_len
))
323 offset
+= sd
->owner_len
;
325 if ((sd
->group_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
326 (offset
+ sd
->group_len
> size
))
328 group
= sd_get_group( sd
);
331 if ((sd
->group_len
< sizeof(SID
)) || (security_sid_len( group
) > sd
->group_len
))
334 offset
+= sd
->group_len
;
336 if ((sd
->sacl_len
>= MAX_ACL_LEN
) || (offset
+ sd
->sacl_len
> size
))
338 sacl
= sd_get_sacl( sd
, &dummy
);
339 if (sacl
&& !acl_is_valid( sacl
, sd
->sacl_len
))
341 offset
+= sd
->sacl_len
;
343 if ((sd
->dacl_len
>= MAX_ACL_LEN
) || (offset
+ sd
->dacl_len
> size
))
345 dacl
= sd_get_dacl( sd
, &dummy
);
346 if (dacl
&& !acl_is_valid( dacl
, sd
->dacl_len
))
348 offset
+= sd
->dacl_len
;
353 /* extract security labels from SACL */
354 ACL
*extract_security_labels( const ACL
*sacl
)
356 size_t size
= sizeof(ACL
);
357 const ACE_HEADER
*ace
;
358 ACE_HEADER
*label_ace
;
359 unsigned int i
, count
= 0;
362 ace
= (const ACE_HEADER
*)(sacl
+ 1);
363 for (i
= 0; i
< sacl
->AceCount
; i
++, ace
= ace_next( ace
))
365 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
)
367 size
+= ace
->AceSize
;
372 label_acl
= mem_alloc( size
);
373 if (!label_acl
) return NULL
;
375 label_acl
->AclRevision
= sacl
->AclRevision
;
377 label_acl
->AclSize
= size
;
378 label_acl
->AceCount
= count
;
380 label_ace
= (ACE_HEADER
*)(label_acl
+ 1);
382 ace
= (const ACE_HEADER
*)(sacl
+ 1);
383 for (i
= 0; i
< sacl
->AceCount
; i
++, ace
= ace_next( ace
))
385 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
)
387 memcpy( label_ace
, ace
, ace
->AceSize
);
388 label_ace
= (ACE_HEADER
*)ace_next( label_ace
);
394 /* replace security labels in an existing SACL */
395 ACL
*replace_security_labels( const ACL
*old_sacl
, const ACL
*new_sacl
)
397 const ACE_HEADER
*ace
;
398 ACE_HEADER
*replaced_ace
;
399 size_t size
= sizeof(ACL
);
400 unsigned int i
, count
= 0;
401 BYTE revision
= ACL_REVISION
;
406 revision
= max( revision
, old_sacl
->AclRevision
);
407 ace
= (const ACE_HEADER
*)(old_sacl
+ 1);
408 for (i
= 0; i
< old_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
410 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
411 size
+= ace
->AceSize
;
418 revision
= max( revision
, new_sacl
->AclRevision
);
419 ace
= (const ACE_HEADER
*)(new_sacl
+ 1);
420 for (i
= 0; i
< new_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
422 if (ace
->AceType
!= SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
423 size
+= ace
->AceSize
;
428 replaced_acl
= mem_alloc( size
);
429 if (!replaced_acl
) return NULL
;
431 replaced_acl
->AclRevision
= revision
;
432 replaced_acl
->Sbz1
= 0;
433 replaced_acl
->AclSize
= size
;
434 replaced_acl
->AceCount
= count
;
435 replaced_acl
->Sbz2
= 0;
436 replaced_ace
= (ACE_HEADER
*)(replaced_acl
+ 1);
440 ace
= (const ACE_HEADER
*)(old_sacl
+ 1);
441 for (i
= 0; i
< old_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
);
451 ace
= (const ACE_HEADER
*)(new_sacl
+ 1);
452 for (i
= 0; i
< new_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
454 if (ace
->AceType
!= SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
455 memcpy( replaced_ace
, ace
, ace
->AceSize
);
456 replaced_ace
= (ACE_HEADER
*)ace_next( replaced_ace
);
463 static inline int is_equal_luid( const LUID
*luid1
, const LUID
*luid2
)
465 return (luid1
->LowPart
== luid2
->LowPart
&& luid1
->HighPart
== luid2
->HighPart
);
468 static inline void allocate_luid( luid_t
*luid
)
470 prev_luid_value
.low_part
++;
471 *luid
= prev_luid_value
;
474 DECL_HANDLER( allocate_locally_unique_id
)
476 allocate_luid( &reply
->luid
);
479 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES
*out
, const struct privilege
*in
)
481 out
->Luid
= in
->luid
;
483 (in
->enabled
? SE_PRIVILEGE_ENABLED
: 0) |
484 (in
->def
? SE_PRIVILEGE_ENABLED_BY_DEFAULT
: 0);
487 static struct privilege
*privilege_add( struct token
*token
, const LUID
*luid
, int enabled
)
489 struct privilege
*privilege
= mem_alloc( sizeof(*privilege
) );
492 privilege
->luid
= *luid
;
493 privilege
->def
= privilege
->enabled
= (enabled
!= 0);
494 list_add_tail( &token
->privileges
, &privilege
->entry
);
499 static inline void privilege_remove( struct privilege
*privilege
)
501 list_remove( &privilege
->entry
);
505 static void token_destroy( struct object
*obj
)
508 struct list
*cursor
, *cursor_next
;
510 assert( obj
->ops
== &token_ops
);
511 token
= (struct token
*)obj
;
515 LIST_FOR_EACH_SAFE( cursor
, cursor_next
, &token
->privileges
)
517 struct privilege
*privilege
= LIST_ENTRY( cursor
, struct privilege
, entry
);
518 privilege_remove( privilege
);
521 LIST_FOR_EACH_SAFE( cursor
, cursor_next
, &token
->groups
)
523 struct group
*group
= LIST_ENTRY( cursor
, struct group
, entry
);
524 list_remove( &group
->entry
);
528 free( token
->default_dacl
);
531 /* creates a new token.
532 * groups may be NULL if group_count is 0.
533 * privs may be NULL if priv_count is 0.
534 * default_dacl may be NULL, indicating that all objects created by the user
536 * modified_id may be NULL, indicating that a new modified_id luid should be
539 static struct token
*create_token( unsigned primary
, const SID
*user
,
540 const SID_AND_ATTRIBUTES
*groups
, unsigned int group_count
,
541 const LUID_AND_ATTRIBUTES
*privs
, unsigned int priv_count
,
542 const ACL
*default_dacl
, TOKEN_SOURCE source
,
543 const luid_t
*modified_id
,
544 int impersonation_level
)
546 struct token
*token
= alloc_object( &token_ops
);
551 allocate_luid( &token
->token_id
);
553 token
->modified_id
= *modified_id
;
555 allocate_luid( &token
->modified_id
);
556 list_init( &token
->privileges
);
557 list_init( &token
->groups
);
558 token
->primary
= primary
;
559 /* primary tokens don't have impersonation levels */
561 token
->impersonation_level
= -1;
563 token
->impersonation_level
= impersonation_level
;
564 token
->default_dacl
= NULL
;
565 token
->primary_group
= NULL
;
568 token
->user
= memdup( user
, security_sid_len( user
));
571 release_object( token
);
576 for (i
= 0; i
< group_count
; i
++)
578 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[((const SID
*)groups
[i
].Sid
)->SubAuthorityCount
] );
579 struct group
*group
= mem_alloc( size
);
583 release_object( token
);
586 memcpy( &group
->sid
, groups
[i
].Sid
, security_sid_len( groups
[i
].Sid
));
587 group
->enabled
= TRUE
;
589 group
->logon
= (groups
[i
].Attributes
& SE_GROUP_LOGON_ID
) != 0;
590 group
->mandatory
= (groups
[i
].Attributes
& SE_GROUP_MANDATORY
) != 0;
591 group
->owner
= (groups
[i
].Attributes
& SE_GROUP_OWNER
) != 0;
592 group
->resource
= FALSE
;
593 group
->deny_only
= FALSE
;
594 list_add_tail( &token
->groups
, &group
->entry
);
595 /* Use first owner capable group as owner and primary group */
596 if (!token
->primary_group
&& group
->owner
)
598 token
->owner
= &group
->sid
;
599 token
->primary_group
= &group
->sid
;
603 /* copy privileges */
604 for (i
= 0; i
< priv_count
; i
++)
606 /* note: we don't check uniqueness: the caller must make sure
607 * privs doesn't contain any duplicate luids */
608 if (!privilege_add( token
, &privs
[i
].Luid
,
609 privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
))
611 release_object( token
);
618 token
->default_dacl
= memdup( default_dacl
, default_dacl
->AclSize
);
619 if (!token
->default_dacl
)
621 release_object( token
);
626 token
->source
= source
;
631 static int filter_group( struct group
*group
, const SID
*filter
, unsigned int count
)
635 for (i
= 0; i
< count
; i
++)
637 if (security_equal_sid( &group
->sid
, filter
)) return 1;
638 filter
= (const SID
*)((char *)filter
+ security_sid_len( filter
));
644 static int filter_privilege( struct privilege
*privilege
, const LUID_AND_ATTRIBUTES
*filter
, unsigned int count
)
648 for (i
= 0; i
< count
; i
++)
650 if (!memcmp( &privilege
->luid
, &filter
[i
].Luid
, sizeof(LUID
) ))
657 struct token
*token_duplicate( struct token
*src_token
, unsigned primary
,
658 int impersonation_level
, const struct security_descriptor
*sd
,
659 const LUID_AND_ATTRIBUTES
*remove_privs
, unsigned int remove_priv_count
,
660 const SID
*remove_groups
, unsigned int remove_group_count
)
662 const luid_t
*modified_id
=
663 primary
|| (impersonation_level
== src_token
->impersonation_level
) ?
664 &src_token
->modified_id
: NULL
;
665 struct token
*token
= NULL
;
666 struct privilege
*privilege
;
670 (impersonation_level
< SecurityAnonymous
||
671 impersonation_level
> SecurityDelegation
||
672 (!src_token
->primary
&& (impersonation_level
> src_token
->impersonation_level
))))
674 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
678 token
= create_token( primary
, src_token
->user
, NULL
, 0,
679 NULL
, 0, src_token
->default_dacl
,
680 src_token
->source
, modified_id
,
681 impersonation_level
);
682 if (!token
) return token
;
685 token
->primary_group
= NULL
;
686 LIST_FOR_EACH_ENTRY( group
, &src_token
->groups
, struct group
, entry
)
688 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[group
->sid
.SubAuthorityCount
] );
689 struct group
*newgroup
= mem_alloc( size
);
692 release_object( token
);
695 memcpy( newgroup
, group
, size
);
696 if (filter_group( group
, remove_groups
, remove_group_count
))
698 newgroup
->enabled
= 0;
700 newgroup
->deny_only
= 1;
702 list_add_tail( &token
->groups
, &newgroup
->entry
);
703 if (src_token
->primary_group
== &group
->sid
)
705 token
->owner
= &newgroup
->sid
;
706 token
->primary_group
= &newgroup
->sid
;
709 assert( token
->primary_group
);
711 /* copy privileges */
712 LIST_FOR_EACH_ENTRY( privilege
, &src_token
->privileges
, struct privilege
, entry
)
714 if (filter_privilege( privilege
, remove_privs
, remove_priv_count
)) continue;
715 if (!privilege_add( token
, &privilege
->luid
, privilege
->enabled
))
717 release_object( token
);
722 if (sd
) default_set_sd( &token
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
723 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
728 static ACL
*create_default_dacl( const SID
*user
)
730 ACCESS_ALLOWED_ACE
*aaa
;
733 size_t default_dacl_size
= sizeof(ACL
) +
734 2*(sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
735 sizeof(local_system_sid
) +
736 security_sid_len( user
);
738 default_dacl
= mem_alloc( default_dacl_size
);
739 if (!default_dacl
) return NULL
;
741 default_dacl
->AclRevision
= ACL_REVISION
;
742 default_dacl
->Sbz1
= 0;
743 default_dacl
->AclSize
= default_dacl_size
;
744 default_dacl
->AceCount
= 2;
745 default_dacl
->Sbz2
= 0;
747 /* GENERIC_ALL for Local System */
748 aaa
= (ACCESS_ALLOWED_ACE
*)(default_dacl
+ 1);
749 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
750 aaa
->Header
.AceFlags
= 0;
751 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
752 sizeof(local_system_sid
);
753 aaa
->Mask
= GENERIC_ALL
;
754 sid
= (SID
*)&aaa
->SidStart
;
755 memcpy( sid
, &local_system_sid
, sizeof(local_system_sid
) );
757 /* GENERIC_ALL for specified user */
758 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
759 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
760 aaa
->Header
.AceFlags
= 0;
761 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) + security_sid_len( user
);
762 aaa
->Mask
= GENERIC_ALL
;
763 sid
= (SID
*)&aaa
->SidStart
;
764 memcpy( sid
, user
, security_sid_len( user
));
771 SID_IDENTIFIER_AUTHORITY idauth
;
773 unsigned int subauth
[MAX_SUBAUTH_COUNT
];
776 static struct security_descriptor
*create_security_label_sd( struct token
*token
, PSID label_sid
)
778 size_t sid_len
= security_sid_len( label_sid
), sacl_size
, sd_size
;
779 SYSTEM_MANDATORY_LABEL_ACE
*smla
;
780 struct security_descriptor
*sd
;
783 sacl_size
= sizeof(ACL
) + FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
) + sid_len
;
784 sd_size
= sizeof(struct security_descriptor
) + sacl_size
;
785 if (!(sd
= mem_alloc( sd_size
)))
788 sd
->control
= SE_SACL_PRESENT
;
791 sd
->sacl_len
= sacl_size
;
794 sacl
= (ACL
*)(sd
+ 1);
795 sacl
->AclRevision
= ACL_REVISION
;
797 sacl
->AclSize
= sacl_size
;
801 smla
= (SYSTEM_MANDATORY_LABEL_ACE
*)(sacl
+ 1);
802 smla
->Header
.AceType
= SYSTEM_MANDATORY_LABEL_ACE_TYPE
;
803 smla
->Header
.AceFlags
= 0;
804 smla
->Header
.AceSize
= FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
) + sid_len
;
805 smla
->Mask
= SYSTEM_MANDATORY_LABEL_NO_WRITE_UP
;
806 memcpy( &smla
->SidStart
, label_sid
, sid_len
);
808 assert( sd_is_valid( sd
, sd_size
) );
812 int token_assign_label( struct token
*token
, PSID label
)
814 struct security_descriptor
*sd
;
817 if ((sd
= create_security_label_sd( token
, label
)))
819 ret
= set_sd_defaults_from_token( &token
->obj
, sd
, LABEL_SECURITY_INFORMATION
, token
);
826 struct token
*get_token_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
828 return (struct token
*)get_handle_obj( process
, handle
, access
, &token_ops
);
831 struct token
*token_create_admin( void )
833 struct token
*token
= NULL
;
834 static const SID_IDENTIFIER_AUTHORITY nt_authority
= { SECURITY_NT_AUTHORITY
};
835 static const unsigned int alias_admins_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
};
836 static const unsigned int alias_users_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
};
837 /* on Windows, this value changes every time the user logs on */
838 static const unsigned int logon_subauth
[] = { SECURITY_LOGON_IDS_RID
, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
839 PSID alias_admins_sid
;
840 PSID alias_users_sid
;
842 const SID
*user_sid
= security_unix_uid_to_sid( getuid() );
843 ACL
*default_dacl
= create_default_dacl( user_sid
);
845 alias_admins_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( alias_admins_subauth
),
846 alias_admins_subauth
);
847 alias_users_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( alias_users_subauth
),
848 alias_users_subauth
);
849 logon_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( logon_subauth
), logon_subauth
);
851 if (alias_admins_sid
&& alias_users_sid
&& logon_sid
&& default_dacl
)
853 const LUID_AND_ATTRIBUTES admin_privs
[] =
855 { SeChangeNotifyPrivilege
, SE_PRIVILEGE_ENABLED
},
856 { SeTcbPrivilege
, 0 },
857 { SeSecurityPrivilege
, 0 },
858 { SeBackupPrivilege
, 0 },
859 { SeRestorePrivilege
, 0 },
860 { SeSystemtimePrivilege
, 0 },
861 { SeShutdownPrivilege
, 0 },
862 { SeRemoteShutdownPrivilege
, 0 },
863 { SeTakeOwnershipPrivilege
, 0 },
864 { SeDebugPrivilege
, 0 },
865 { SeSystemEnvironmentPrivilege
, 0 },
866 { SeSystemProfilePrivilege
, 0 },
867 { SeProfileSingleProcessPrivilege
, 0 },
868 { SeIncreaseBasePriorityPrivilege
, 0 },
869 { SeLoadDriverPrivilege
, SE_PRIVILEGE_ENABLED
},
870 { SeCreatePagefilePrivilege
, 0 },
871 { SeIncreaseQuotaPrivilege
, 0 },
872 { SeUndockPrivilege
, 0 },
873 { SeManageVolumePrivilege
, 0 },
874 { SeImpersonatePrivilege
, SE_PRIVILEGE_ENABLED
},
875 { SeCreateGlobalPrivilege
, SE_PRIVILEGE_ENABLED
},
877 /* note: we don't include non-builtin groups here for the user -
878 * telling us these is the job of a client-side program */
879 const SID_AND_ATTRIBUTES admin_groups
[] =
881 { security_world_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
882 { security_local_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
883 { security_interactive_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
884 { security_authenticated_user_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
885 { security_domain_users_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_OWNER
},
886 { alias_admins_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_OWNER
},
887 { alias_users_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
888 { logon_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_LOGON_ID
},
890 static const TOKEN_SOURCE admin_source
= {"SeMgr", {0, 0}};
891 token
= create_token( TRUE
, user_sid
, admin_groups
, ARRAY_SIZE( admin_groups
),
892 admin_privs
, ARRAY_SIZE( admin_privs
), default_dacl
,
893 admin_source
, NULL
, -1 );
894 /* we really need a primary group */
895 assert( token
->primary_group
);
899 free( alias_admins_sid
);
900 free( alias_users_sid
);
901 free( default_dacl
);
906 static struct privilege
*token_find_privilege( struct token
*token
, const LUID
*luid
, int enabled_only
)
908 struct privilege
*privilege
;
909 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
911 if (is_equal_luid( luid
, &privilege
->luid
))
913 if (enabled_only
&& !privilege
->enabled
)
921 static unsigned int token_adjust_privileges( struct token
*token
, const LUID_AND_ATTRIBUTES
*privs
,
922 unsigned int count
, LUID_AND_ATTRIBUTES
*mod_privs
,
923 unsigned int mod_privs_count
)
925 unsigned int i
, modified_count
= 0;
927 /* mark as modified */
928 allocate_luid( &token
->modified_id
);
930 for (i
= 0; i
< count
; i
++)
932 struct privilege
*privilege
=
933 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
936 set_error( STATUS_NOT_ALL_ASSIGNED
);
940 if (privs
[i
].Attributes
& SE_PRIVILEGE_REMOVED
)
941 privilege_remove( privilege
);
944 /* save previous state for caller */
947 luid_and_attr_from_privilege(mod_privs
, privilege
);
953 if (privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
)
954 privilege
->enabled
= TRUE
;
956 privilege
->enabled
= FALSE
;
959 return modified_count
;
962 static void token_disable_privileges( struct token
*token
)
964 struct privilege
*privilege
;
966 /* mark as modified */
967 allocate_luid( &token
->modified_id
);
969 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
970 privilege
->enabled
= FALSE
;
973 int token_check_privileges( struct token
*token
, int all_required
,
974 const LUID_AND_ATTRIBUTES
*reqprivs
,
975 unsigned int count
, LUID_AND_ATTRIBUTES
*usedprivs
)
977 unsigned int i
, enabled_count
= 0;
979 for (i
= 0; i
< count
; i
++)
981 struct privilege
*privilege
=
982 token_find_privilege( token
, &reqprivs
[i
].Luid
, TRUE
);
985 usedprivs
[i
] = reqprivs
[i
];
987 if (privilege
&& privilege
->enabled
)
991 usedprivs
[i
].Attributes
|= SE_PRIVILEGE_USED_FOR_ACCESS
;
996 return (enabled_count
== count
);
998 return (enabled_count
> 0);
1001 int token_sid_present( struct token
*token
, const SID
*sid
, int deny
)
1003 struct group
*group
;
1005 if (security_equal_sid( token
->user
, sid
)) return TRUE
;
1007 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, struct group
, entry
)
1009 if (!group
->enabled
) continue;
1010 if (group
->deny_only
&& !deny
) continue;
1012 if (security_equal_sid( &group
->sid
, sid
)) return TRUE
;
1018 /* Checks access to a security descriptor. 'sd' must have been validated by
1019 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
1020 * the reason. 'status' parameter will indicate if access is granted or denied.
1022 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
1024 static unsigned int token_access_check( struct token
*token
,
1025 const struct security_descriptor
*sd
,
1026 unsigned int desired_access
,
1027 LUID_AND_ATTRIBUTES
*privs
,
1028 unsigned int *priv_count
,
1029 const generic_map_t
*mapping
,
1030 unsigned int *granted_access
,
1031 unsigned int *status
)
1033 unsigned int current_access
= 0;
1034 unsigned int denied_access
= 0;
1038 const ACE_HEADER
*ace
;
1041 /* assume no access rights */
1042 *granted_access
= 0;
1044 /* fail if desired_access contains generic rights */
1045 if (desired_access
& (GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
|GENERIC_ALL
))
1047 if (priv_count
) *priv_count
= 0;
1048 return STATUS_GENERIC_NOT_MAPPED
;
1051 dacl
= sd_get_dacl( sd
, &dacl_present
);
1052 owner
= sd_get_owner( sd
);
1053 if (!owner
|| !sd_get_group( sd
))
1055 if (priv_count
) *priv_count
= 0;
1056 return STATUS_INVALID_SECURITY_DESCR
;
1059 /* 1: Grant desired access if the object is unprotected */
1060 if (!dacl_present
|| !dacl
)
1062 if (priv_count
) *priv_count
= 0;
1063 if (desired_access
& MAXIMUM_ALLOWED
)
1064 *granted_access
= mapping
->all
;
1066 *granted_access
= desired_access
;
1067 return *status
= STATUS_SUCCESS
;
1070 /* 2: Check if caller wants access to system security part. Note: access
1071 * is only granted if specifically asked for */
1072 if (desired_access
& ACCESS_SYSTEM_SECURITY
)
1074 const LUID_AND_ATTRIBUTES security_priv
= { SeSecurityPrivilege
, 0 };
1075 LUID_AND_ATTRIBUTES retpriv
= security_priv
;
1076 if (token_check_privileges( token
, TRUE
, &security_priv
, 1, &retpriv
))
1080 /* assumes that there will only be one privilege to return */
1081 if (*priv_count
>= 1)
1089 return STATUS_BUFFER_TOO_SMALL
;
1092 current_access
|= ACCESS_SYSTEM_SECURITY
;
1093 if (desired_access
== current_access
)
1095 *granted_access
= current_access
;
1096 return *status
= STATUS_SUCCESS
;
1101 if (priv_count
) *priv_count
= 0;
1102 *status
= STATUS_PRIVILEGE_NOT_HELD
;
1103 return STATUS_SUCCESS
;
1106 else if (priv_count
) *priv_count
= 0;
1108 /* 3: Check whether the token is the owner */
1109 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
1110 * checked when a "set owner" call is made, overriding the access rights
1111 * determined here. */
1112 if (token_sid_present( token
, owner
, FALSE
))
1114 current_access
|= (READ_CONTROL
| WRITE_DAC
);
1115 if (desired_access
== current_access
)
1117 *granted_access
= current_access
;
1118 return *status
= STATUS_SUCCESS
;
1122 /* 4: Grant rights according to the DACL */
1123 ace
= (const ACE_HEADER
*)(dacl
+ 1);
1124 for (i
= 0; i
< dacl
->AceCount
; i
++, ace
= ace_next( ace
))
1126 const ACCESS_ALLOWED_ACE
*aa_ace
;
1127 const ACCESS_DENIED_ACE
*ad_ace
;
1130 if (ace
->AceFlags
& INHERIT_ONLY_ACE
)
1133 switch (ace
->AceType
)
1135 case ACCESS_DENIED_ACE_TYPE
:
1136 ad_ace
= (const ACCESS_DENIED_ACE
*)ace
;
1137 sid
= (const SID
*)&ad_ace
->SidStart
;
1138 if (token_sid_present( token
, sid
, TRUE
))
1140 unsigned int access
= map_access( ad_ace
->Mask
, mapping
);
1141 if (desired_access
& MAXIMUM_ALLOWED
)
1142 denied_access
|= access
;
1145 denied_access
|= (access
& ~current_access
);
1146 if (desired_access
& access
) goto done
;
1150 case ACCESS_ALLOWED_ACE_TYPE
:
1151 aa_ace
= (const ACCESS_ALLOWED_ACE
*)ace
;
1152 sid
= (const SID
*)&aa_ace
->SidStart
;
1153 if (token_sid_present( token
, sid
, FALSE
))
1155 unsigned int access
= map_access( aa_ace
->Mask
, mapping
);
1156 if (desired_access
& MAXIMUM_ALLOWED
)
1157 current_access
|= access
;
1159 current_access
|= (access
& ~denied_access
);
1164 /* don't bother carrying on checking if we've already got all of
1166 if (desired_access
== *granted_access
)
1171 if (desired_access
& MAXIMUM_ALLOWED
)
1172 *granted_access
= current_access
& ~denied_access
;
1174 if ((current_access
& desired_access
) == desired_access
)
1175 *granted_access
= current_access
& desired_access
;
1177 *granted_access
= 0;
1179 *status
= *granted_access
? STATUS_SUCCESS
: STATUS_ACCESS_DENIED
;
1180 return STATUS_SUCCESS
;
1183 const ACL
*token_get_default_dacl( struct token
*token
)
1185 return token
->default_dacl
;
1188 const SID
*token_get_user( struct token
*token
)
1193 const SID
*token_get_primary_group( struct token
*token
)
1195 return token
->primary_group
;
1198 int check_object_access(struct token
*token
, struct object
*obj
, unsigned int *access
)
1200 generic_map_t mapping
;
1201 unsigned int status
;
1205 token
= current
->token
? current
->token
: current
->process
->token
;
1207 mapping
.all
= obj
->ops
->map_access( obj
, GENERIC_ALL
);
1211 if (*access
& MAXIMUM_ALLOWED
) *access
= mapping
.all
;
1215 mapping
.read
= obj
->ops
->map_access( obj
, GENERIC_READ
);
1216 mapping
.write
= obj
->ops
->map_access( obj
, GENERIC_WRITE
);
1217 mapping
.exec
= obj
->ops
->map_access( obj
, GENERIC_EXECUTE
);
1219 res
= token_access_check( token
, obj
->sd
, *access
, NULL
, NULL
,
1220 &mapping
, access
, &status
) == STATUS_SUCCESS
&&
1221 status
== STATUS_SUCCESS
;
1223 if (!res
) set_error( STATUS_ACCESS_DENIED
);
1228 /* open a security token */
1229 DECL_HANDLER(open_token
)
1231 if (req
->flags
& OPEN_TOKEN_THREAD
)
1233 struct thread
*thread
= get_thread_from_handle( req
->handle
, 0 );
1238 if (!thread
->token
->primary
&& thread
->token
->impersonation_level
<= SecurityAnonymous
)
1239 set_error( STATUS_CANT_OPEN_ANONYMOUS
);
1241 reply
->token
= alloc_handle( current
->process
, thread
->token
,
1242 req
->access
, req
->attributes
);
1245 set_error( STATUS_NO_TOKEN
);
1246 release_object( thread
);
1251 struct process
*process
= get_process_from_handle( req
->handle
, 0 );
1255 reply
->token
= alloc_handle( current
->process
, process
->token
, req
->access
,
1258 set_error( STATUS_NO_TOKEN
);
1259 release_object( process
);
1264 /* adjust the privileges held by a token */
1265 DECL_HANDLER(adjust_token_privileges
)
1267 struct token
*token
;
1268 unsigned int access
= TOKEN_ADJUST_PRIVILEGES
;
1270 if (req
->get_modified_state
) access
|= TOKEN_QUERY
;
1272 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1273 access
, &token_ops
)))
1275 const LUID_AND_ATTRIBUTES
*privs
= get_req_data();
1276 LUID_AND_ATTRIBUTES
*modified_privs
= NULL
;
1277 unsigned int priv_count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1278 unsigned int modified_priv_count
= 0;
1280 if (req
->get_modified_state
&& !req
->disable_all
)
1283 /* count modified privs */
1284 for (i
= 0; i
< priv_count
; i
++)
1286 struct privilege
*privilege
=
1287 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
1288 if (privilege
&& req
->get_modified_state
)
1289 modified_priv_count
++;
1291 reply
->len
= modified_priv_count
;
1292 modified_priv_count
= min( modified_priv_count
, get_reply_max_size() / sizeof(*modified_privs
) );
1293 if (modified_priv_count
)
1294 modified_privs
= set_reply_data_size( modified_priv_count
* sizeof(*modified_privs
) );
1296 reply
->len
= modified_priv_count
* sizeof(*modified_privs
);
1298 if (req
->disable_all
)
1299 token_disable_privileges( token
);
1301 token_adjust_privileges( token
, privs
, priv_count
, modified_privs
, modified_priv_count
);
1303 release_object( token
);
1307 /* retrieves the list of privileges that may be held be the token */
1308 DECL_HANDLER(get_token_privileges
)
1310 struct token
*token
;
1312 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1317 LUID_AND_ATTRIBUTES
*privs
;
1318 struct privilege
*privilege
;
1320 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1323 reply
->len
= priv_count
* sizeof(*privs
);
1324 if (reply
->len
<= get_reply_max_size())
1326 privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1330 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1332 luid_and_attr_from_privilege( &privs
[i
], privilege
);
1338 set_error(STATUS_BUFFER_TOO_SMALL
);
1340 release_object( token
);
1344 /* creates a duplicate of the token */
1345 DECL_HANDLER(duplicate_token
)
1347 struct token
*src_token
;
1348 struct unicode_str name
;
1349 const struct security_descriptor
*sd
;
1350 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1352 if (!objattr
) return;
1354 if ((src_token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1358 struct token
*token
= token_duplicate( src_token
, req
->primary
, req
->impersonation_level
, sd
, NULL
, 0, NULL
, 0 );
1361 unsigned int access
= req
->access
? req
->access
: get_handle_access( current
->process
, req
->handle
);
1362 reply
->new_handle
= alloc_handle_no_access_check( current
->process
, token
, access
, objattr
->attributes
);
1363 release_object( token
);
1365 release_object( src_token
);
1369 /* creates a restricted version of a token */
1370 DECL_HANDLER(filter_token
)
1372 struct token
*src_token
;
1374 if ((src_token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
, TOKEN_DUPLICATE
, &token_ops
)))
1376 const LUID_AND_ATTRIBUTES
*filter_privileges
= get_req_data();
1377 unsigned int priv_count
, group_count
;
1378 const SID
*filter_groups
;
1379 struct token
*token
;
1381 priv_count
= min( req
->privileges_size
, get_req_data_size() ) / sizeof(LUID_AND_ATTRIBUTES
);
1382 filter_groups
= (const SID
*)((char *)filter_privileges
+ priv_count
* sizeof(LUID_AND_ATTRIBUTES
));
1383 group_count
= get_sid_count( filter_groups
, get_req_data_size() - priv_count
* sizeof(LUID_AND_ATTRIBUTES
) );
1385 token
= token_duplicate( src_token
, src_token
->primary
, src_token
->impersonation_level
, NULL
,
1386 filter_privileges
, priv_count
, filter_groups
, group_count
);
1389 unsigned int access
= get_handle_access( current
->process
, req
->handle
);
1390 reply
->new_handle
= alloc_handle_no_access_check( current
->process
, token
, access
, 0 );
1391 release_object( token
);
1393 release_object( src_token
);
1397 /* checks the specified privileges are held by the token */
1398 DECL_HANDLER(check_token_privileges
)
1400 struct token
*token
;
1402 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1406 unsigned int count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1408 if (!token
->primary
&& token
->impersonation_level
<= SecurityAnonymous
)
1409 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1410 else if (get_reply_max_size() >= count
* sizeof(LUID_AND_ATTRIBUTES
))
1412 LUID_AND_ATTRIBUTES
*usedprivs
= set_reply_data_size( count
* sizeof(*usedprivs
) );
1413 reply
->has_privileges
= token_check_privileges( token
, req
->all_required
, get_req_data(), count
, usedprivs
);
1416 set_error( STATUS_BUFFER_OVERFLOW
);
1417 release_object( token
);
1421 /* checks that a user represented by a token is allowed to access an object
1422 * represented by a security descriptor */
1423 DECL_HANDLER(access_check
)
1425 data_size_t sd_size
= get_req_data_size();
1426 const struct security_descriptor
*sd
= get_req_data();
1427 struct token
*token
;
1429 if (!sd_is_valid( sd
, sd_size
))
1431 set_error( STATUS_ACCESS_VIOLATION
);
1435 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1439 unsigned int status
;
1440 LUID_AND_ATTRIBUTES priv
;
1441 unsigned int priv_count
= 1;
1443 memset(&priv
, 0, sizeof(priv
));
1445 /* only impersonation tokens may be used with this function */
1448 set_error( STATUS_NO_IMPERSONATION_TOKEN
);
1449 release_object( token
);
1452 /* anonymous impersonation tokens can't be used */
1453 if (token
->impersonation_level
<= SecurityAnonymous
)
1455 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1456 release_object( token
);
1460 status
= token_access_check( token
, sd
, req
->desired_access
, &priv
, &priv_count
, &req
->mapping
,
1461 &reply
->access_granted
, &reply
->access_status
);
1463 reply
->privileges_len
= priv_count
*sizeof(LUID_AND_ATTRIBUTES
);
1465 if ((priv_count
> 0) && (reply
->privileges_len
<= get_reply_max_size()))
1467 LUID_AND_ATTRIBUTES
*privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1468 memcpy( privs
, &priv
, sizeof(priv
) );
1471 set_error( status
);
1472 release_object( token
);
1476 /* retrieves an SID from the token */
1477 DECL_HANDLER(get_token_sid
)
1479 struct token
*token
;
1483 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
, TOKEN_QUERY
, &token_ops
)))
1485 const SID
*sid
= NULL
;
1487 switch (req
->which_sid
)
1490 assert(token
->user
);
1493 case TokenPrimaryGroup
:
1494 sid
= token
->primary_group
;
1500 sid
= (const SID
*)&builtin_logon_sid
;
1503 set_error( STATUS_INVALID_PARAMETER
);
1509 reply
->sid_len
= security_sid_len( sid
);
1510 if (reply
->sid_len
<= get_reply_max_size()) set_reply_data( sid
, reply
->sid_len
);
1511 else set_error( STATUS_BUFFER_TOO_SMALL
);
1513 release_object( token
);
1517 /* retrieves the groups that the user represented by the token belongs to */
1518 DECL_HANDLER(get_token_groups
)
1520 struct token
*token
;
1522 reply
->user_len
= 0;
1524 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1528 size_t size_needed
= sizeof(struct token_groups
);
1529 size_t sid_size
= 0;
1530 unsigned int group_count
= 0;
1531 const struct group
*group
;
1533 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1536 sid_size
+= security_sid_len( &group
->sid
);
1538 size_needed
+= sid_size
;
1539 /* attributes size */
1540 size_needed
+= sizeof(unsigned int) * group_count
;
1542 /* reply buffer contains size_needed bytes formatted as:
1545 unsigned int attrib[count];
1548 user_len includes extra data needed for TOKEN_GROUPS representation,
1549 required caller buffer size calculated here to avoid extra server call */
1550 reply
->user_len
= FIELD_OFFSET( TOKEN_GROUPS
, Groups
[group_count
] ) + sid_size
;
1552 if (reply
->user_len
<= get_reply_max_size())
1554 struct token_groups
*tg
= set_reply_data_size( size_needed
);
1557 unsigned int *attr_ptr
= (unsigned int *)(tg
+ 1);
1558 SID
*sid_ptr
= (SID
*)(attr_ptr
+ group_count
);
1560 tg
->count
= group_count
;
1562 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1566 if (group
->mandatory
) *attr_ptr
|= SE_GROUP_MANDATORY
;
1567 if (group
->def
) *attr_ptr
|= SE_GROUP_ENABLED_BY_DEFAULT
;
1568 if (group
->enabled
) *attr_ptr
|= SE_GROUP_ENABLED
;
1569 if (group
->owner
) *attr_ptr
|= SE_GROUP_OWNER
;
1570 if (group
->deny_only
) *attr_ptr
|= SE_GROUP_USE_FOR_DENY_ONLY
;
1571 if (group
->resource
) *attr_ptr
|= SE_GROUP_RESOURCE
;
1572 if (group
->logon
) *attr_ptr
|= SE_GROUP_LOGON_ID
;
1574 memcpy(sid_ptr
, &group
->sid
, security_sid_len( &group
->sid
));
1576 sid_ptr
= (SID
*)((char *)sid_ptr
+ security_sid_len( &group
->sid
));
1581 else set_error( STATUS_BUFFER_TOO_SMALL
);
1583 release_object( token
);
1587 DECL_HANDLER(get_token_impersonation_level
)
1589 struct token
*token
;
1591 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1596 set_error( STATUS_INVALID_PARAMETER
);
1598 reply
->impersonation_level
= token
->impersonation_level
;
1600 release_object( token
);
1604 DECL_HANDLER(get_token_statistics
)
1606 struct token
*token
;
1608 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1612 reply
->token_id
= token
->token_id
;
1613 reply
->modified_id
= token
->modified_id
;
1614 reply
->primary
= token
->primary
;
1615 reply
->impersonation_level
= token
->impersonation_level
;
1616 reply
->group_count
= list_count( &token
->groups
);
1617 reply
->privilege_count
= list_count( &token
->privileges
);
1619 release_object( token
);
1623 DECL_HANDLER(get_token_default_dacl
)
1625 struct token
*token
;
1629 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1633 if (token
->default_dacl
)
1634 reply
->acl_len
= token
->default_dacl
->AclSize
;
1636 if (reply
->acl_len
<= get_reply_max_size())
1638 ACL
*acl_reply
= set_reply_data_size( reply
->acl_len
);
1640 memcpy( acl_reply
, token
->default_dacl
, reply
->acl_len
);
1642 else set_error( STATUS_BUFFER_TOO_SMALL
);
1644 release_object( token
);
1648 DECL_HANDLER(set_token_default_dacl
)
1650 struct token
*token
;
1652 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1653 TOKEN_ADJUST_DEFAULT
,
1656 const ACL
*acl
= get_req_data();
1657 unsigned int acl_size
= get_req_data_size();
1659 free( token
->default_dacl
);
1660 token
->default_dacl
= NULL
;
1663 token
->default_dacl
= memdup( acl
, acl_size
);
1665 release_object( token
);