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
29 #include <sys/types.h>
33 #define WIN32_NO_STATUS
43 #define MAX_SUBAUTH_COUNT 1
45 const LUID SeIncreaseQuotaPrivilege
= { 5, 0 };
46 const LUID SeTcbPrivilege
= { 7, 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 } };
86 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
} };
88 const PSID security_world_sid
= (PSID
)&world_sid
;
89 static const PSID security_local_sid
= (PSID
)&local_sid
;
90 static const PSID security_interactive_sid
= (PSID
)&interactive_sid
;
91 static const PSID security_authenticated_user_sid
= (PSID
)&authenticated_user_sid
;
92 const PSID security_local_system_sid
= (PSID
)&local_system_sid
;
93 const PSID security_local_user_sid
= (PSID
)&local_user_sid
;
94 const PSID security_builtin_admins_sid
= (PSID
)&builtin_admins_sid
;
95 const PSID security_builtin_users_sid
= (PSID
)&builtin_users_sid
;
96 const PSID security_domain_users_sid
= (PSID
)&domain_users_sid
;
97 const PSID security_high_label_sid
= (PSID
)&high_label_sid
;
99 static luid_t prev_luid_value
= { 1000, 0 };
101 static const WCHAR token_name
[] = {'T','o','k','e','n'};
103 struct type_descr token_type
=
105 { token_name
, sizeof(token_name
) }, /* name */
106 TOKEN_ALL_ACCESS
| SYNCHRONIZE
, /* valid_access */
108 STANDARD_RIGHTS_READ
| TOKEN_QUERY_SOURCE
| TOKEN_QUERY
| TOKEN_DUPLICATE
,
109 STANDARD_RIGHTS_WRITE
| TOKEN_ADJUST_SESSIONID
| TOKEN_ADJUST_DEFAULT
| TOKEN_ADJUST_GROUPS
110 | TOKEN_ADJUST_PRIVILEGES
,
111 STANDARD_RIGHTS_EXECUTE
| TOKEN_IMPERSONATE
| TOKEN_ASSIGN_PRIMARY
,
118 struct object obj
; /* object header */
119 luid_t token_id
; /* system-unique id of token */
120 luid_t modified_id
; /* new id allocated every time token is modified */
121 struct list privileges
; /* privileges available to the token */
122 struct list groups
; /* groups that the user of this token belongs to (sid_and_attributes) */
123 SID
*user
; /* SID of user this token represents */
124 SID
*owner
; /* SID of owner (points to user or one of groups) */
125 SID
*primary_group
; /* SID of user's primary group (points to one of groups) */
126 unsigned int primary
; /* is this a primary or impersonation token? */
127 unsigned int session_id
; /* token session id */
128 ACL
*default_dacl
; /* the default DACL to assign to objects created by this user */
129 TOKEN_SOURCE source
; /* source of the token */
130 int impersonation_level
; /* impersonation level this token is capable of if non-primary token */
131 int elevation
; /* elevation type */
138 unsigned enabled
: 1; /* is the privilege currently enabled? */
139 unsigned def
: 1; /* is the privilege enabled by default? */
145 unsigned enabled
: 1; /* is the sid currently enabled? */
146 unsigned def
: 1; /* is the sid enabled by default? */
147 unsigned logon
: 1; /* is this a logon sid? */
148 unsigned mandatory
: 1; /* is this sid always enabled? */
149 unsigned owner
: 1; /* can this sid be an owner of an object? */
150 unsigned resource
: 1; /* is this a domain-local group? */
151 unsigned deny_only
: 1; /* is this a sid that should be use for denying only? */
155 static void token_dump( struct object
*obj
, int verbose
);
156 static void token_destroy( struct object
*obj
);
158 static const struct object_ops token_ops
=
160 sizeof(struct token
), /* size */
161 &token_type
, /* type */
162 token_dump
, /* dump */
163 no_add_queue
, /* add_queue */
164 NULL
, /* remove_queue */
166 NULL
, /* satisfied */
167 no_signal
, /* signal */
168 no_get_fd
, /* get_fd */
169 default_map_access
, /* map_access */
170 default_get_sd
, /* get_sd */
171 default_set_sd
, /* set_sd */
172 no_get_full_name
, /* get_full_name */
173 no_lookup_name
, /* lookup_name */
174 no_link_name
, /* link_name */
175 NULL
, /* unlink_name */
176 no_open_file
, /* open_file */
177 no_kernel_obj_list
, /* get_kernel_obj_list */
178 no_close_handle
, /* close_handle */
179 token_destroy
/* destroy */
182 static void token_dump( struct object
*obj
, int verbose
)
184 struct token
*token
= (struct token
*)obj
;
185 assert( obj
->ops
== &token_ops
);
186 fprintf( stderr
, "Token id=%d.%u primary=%u impersonation level=%d\n", token
->token_id
.high_part
,
187 token
->token_id
.low_part
, token
->primary
, token
->impersonation_level
);
190 static SID
*security_sid_alloc( const SID_IDENTIFIER_AUTHORITY
*idauthority
, int subauthcount
, const unsigned int subauth
[] )
193 SID
*sid
= mem_alloc( FIELD_OFFSET(SID
, SubAuthority
[subauthcount
]) );
194 if (!sid
) return NULL
;
195 sid
->Revision
= SID_REVISION
;
196 sid
->SubAuthorityCount
= subauthcount
;
197 sid
->IdentifierAuthority
= *idauthority
;
198 for (i
= 0; i
< subauthcount
; i
++)
199 sid
->SubAuthority
[i
] = subauth
[i
];
203 void security_set_thread_token( struct thread
*thread
, obj_handle_t handle
)
208 release_object( thread
->token
);
209 thread
->token
= NULL
;
213 struct token
*token
= (struct token
*)get_handle_obj( current
->process
,
220 release_object( thread
->token
);
221 thread
->token
= token
;
226 const SID
*security_unix_uid_to_sid( uid_t uid
)
228 /* very simple mapping: either the current user or not the current user */
230 return (const SID
*)&local_user_sid
;
232 return &anonymous_logon_sid
;
235 static int acl_is_valid( const ACL
*acl
, data_size_t size
)
238 const ACE_HEADER
*ace
;
240 if (size
< sizeof(ACL
))
243 size
= min(size
, MAX_ACL_LEN
);
247 ace
= (const ACE_HEADER
*)(acl
+ 1);
248 for (i
= 0; i
< acl
->AceCount
; i
++)
251 data_size_t sid_size
;
253 if (size
< sizeof(ACE_HEADER
))
255 if (size
< ace
->AceSize
)
257 size
-= ace
->AceSize
;
258 switch (ace
->AceType
)
260 case ACCESS_DENIED_ACE_TYPE
:
261 sid
= (const SID
*)&((const ACCESS_DENIED_ACE
*)ace
)->SidStart
;
262 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_DENIED_ACE
, SidStart
);
264 case ACCESS_ALLOWED_ACE_TYPE
:
265 sid
= (const SID
*)&((const ACCESS_ALLOWED_ACE
*)ace
)->SidStart
;
266 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
);
268 case SYSTEM_AUDIT_ACE_TYPE
:
269 sid
= (const SID
*)&((const SYSTEM_AUDIT_ACE
*)ace
)->SidStart
;
270 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_AUDIT_ACE
, SidStart
);
272 case SYSTEM_ALARM_ACE_TYPE
:
273 sid
= (const SID
*)&((const SYSTEM_ALARM_ACE
*)ace
)->SidStart
;
274 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_ALARM_ACE
, SidStart
);
276 case SYSTEM_MANDATORY_LABEL_ACE_TYPE
:
277 sid
= (const SID
*)&((const SYSTEM_MANDATORY_LABEL_ACE
*)ace
)->SidStart
;
278 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
);
283 if (sid_size
< FIELD_OFFSET(SID
, SubAuthority
[0]) || sid_size
< security_sid_len( sid
))
285 ace
= ace_next( ace
);
290 static unsigned int get_sid_count( const SID
*sid
, data_size_t size
)
294 for (count
= 0; size
>= sizeof(SID
) && security_sid_len( sid
) <= size
; count
++)
296 size
-= security_sid_len( sid
);
297 sid
= (const SID
*)((char *)sid
+ security_sid_len( sid
));
303 /* checks whether all members of a security descriptor fit inside the size
304 * of memory specified */
305 int sd_is_valid( const struct security_descriptor
*sd
, data_size_t size
)
307 size_t offset
= sizeof(struct security_descriptor
);
317 if ((sd
->owner_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
318 (offset
+ sd
->owner_len
> size
))
320 owner
= sd_get_owner( sd
);
323 if ((sd
->owner_len
< sizeof(SID
)) || (security_sid_len( owner
) > sd
->owner_len
))
326 offset
+= sd
->owner_len
;
328 if ((sd
->group_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
329 (offset
+ sd
->group_len
> size
))
331 group
= sd_get_group( sd
);
334 if ((sd
->group_len
< sizeof(SID
)) || (security_sid_len( group
) > sd
->group_len
))
337 offset
+= sd
->group_len
;
339 if ((sd
->sacl_len
>= MAX_ACL_LEN
) || (offset
+ sd
->sacl_len
> size
))
341 sacl
= sd_get_sacl( sd
, &dummy
);
342 if (sacl
&& !acl_is_valid( sacl
, sd
->sacl_len
))
344 offset
+= sd
->sacl_len
;
346 if ((sd
->dacl_len
>= MAX_ACL_LEN
) || (offset
+ sd
->dacl_len
> size
))
348 dacl
= sd_get_dacl( sd
, &dummy
);
349 if (dacl
&& !acl_is_valid( dacl
, sd
->dacl_len
))
351 offset
+= sd
->dacl_len
;
356 /* extract security labels from SACL */
357 ACL
*extract_security_labels( const ACL
*sacl
)
359 size_t size
= sizeof(ACL
);
360 const ACE_HEADER
*ace
;
361 ACE_HEADER
*label_ace
;
362 unsigned int i
, count
= 0;
365 ace
= (const ACE_HEADER
*)(sacl
+ 1);
366 for (i
= 0; i
< sacl
->AceCount
; i
++, ace
= ace_next( ace
))
368 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
)
370 size
+= ace
->AceSize
;
375 label_acl
= mem_alloc( size
);
376 if (!label_acl
) return NULL
;
378 label_acl
->AclRevision
= sacl
->AclRevision
;
380 label_acl
->AclSize
= size
;
381 label_acl
->AceCount
= count
;
383 label_ace
= (ACE_HEADER
*)(label_acl
+ 1);
385 ace
= (const ACE_HEADER
*)(sacl
+ 1);
386 for (i
= 0; i
< sacl
->AceCount
; i
++, ace
= ace_next( ace
))
388 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
)
390 memcpy( label_ace
, ace
, ace
->AceSize
);
391 label_ace
= (ACE_HEADER
*)ace_next( label_ace
);
397 /* replace security labels in an existing SACL */
398 ACL
*replace_security_labels( const ACL
*old_sacl
, const ACL
*new_sacl
)
400 const ACE_HEADER
*ace
;
401 ACE_HEADER
*replaced_ace
;
402 size_t size
= sizeof(ACL
);
403 unsigned int i
, count
= 0;
404 BYTE revision
= ACL_REVISION
;
409 revision
= max( revision
, old_sacl
->AclRevision
);
410 ace
= (const ACE_HEADER
*)(old_sacl
+ 1);
411 for (i
= 0; i
< old_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
413 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
414 size
+= ace
->AceSize
;
421 revision
= max( revision
, new_sacl
->AclRevision
);
422 ace
= (const ACE_HEADER
*)(new_sacl
+ 1);
423 for (i
= 0; i
< new_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
425 if (ace
->AceType
!= SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
426 size
+= ace
->AceSize
;
431 replaced_acl
= mem_alloc( size
);
432 if (!replaced_acl
) return NULL
;
434 replaced_acl
->AclRevision
= revision
;
435 replaced_acl
->Sbz1
= 0;
436 replaced_acl
->AclSize
= size
;
437 replaced_acl
->AceCount
= count
;
438 replaced_acl
->Sbz2
= 0;
439 replaced_ace
= (ACE_HEADER
*)(replaced_acl
+ 1);
443 ace
= (const ACE_HEADER
*)(old_sacl
+ 1);
444 for (i
= 0; i
< old_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
446 if (ace
->AceType
== SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
447 memcpy( replaced_ace
, ace
, ace
->AceSize
);
448 replaced_ace
= (ACE_HEADER
*)ace_next( replaced_ace
);
454 ace
= (const ACE_HEADER
*)(new_sacl
+ 1);
455 for (i
= 0; i
< new_sacl
->AceCount
; i
++, ace
= ace_next( ace
))
457 if (ace
->AceType
!= SYSTEM_MANDATORY_LABEL_ACE_TYPE
) continue;
458 memcpy( replaced_ace
, ace
, ace
->AceSize
);
459 replaced_ace
= (ACE_HEADER
*)ace_next( replaced_ace
);
466 static inline int is_equal_luid( const LUID
*luid1
, const LUID
*luid2
)
468 return (luid1
->LowPart
== luid2
->LowPart
&& luid1
->HighPart
== luid2
->HighPart
);
471 static inline void allocate_luid( luid_t
*luid
)
473 prev_luid_value
.low_part
++;
474 *luid
= prev_luid_value
;
477 DECL_HANDLER( allocate_locally_unique_id
)
479 allocate_luid( &reply
->luid
);
482 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES
*out
, const struct privilege
*in
)
484 out
->Luid
= in
->luid
;
486 (in
->enabled
? SE_PRIVILEGE_ENABLED
: 0) |
487 (in
->def
? SE_PRIVILEGE_ENABLED_BY_DEFAULT
: 0);
490 static struct privilege
*privilege_add( struct token
*token
, const LUID
*luid
, int enabled
)
492 struct privilege
*privilege
= mem_alloc( sizeof(*privilege
) );
495 privilege
->luid
= *luid
;
496 privilege
->def
= privilege
->enabled
= (enabled
!= 0);
497 list_add_tail( &token
->privileges
, &privilege
->entry
);
502 static inline void privilege_remove( struct privilege
*privilege
)
504 list_remove( &privilege
->entry
);
508 static void token_destroy( struct object
*obj
)
511 struct list
*cursor
, *cursor_next
;
513 assert( obj
->ops
== &token_ops
);
514 token
= (struct token
*)obj
;
518 LIST_FOR_EACH_SAFE( cursor
, cursor_next
, &token
->privileges
)
520 struct privilege
*privilege
= LIST_ENTRY( cursor
, struct privilege
, entry
);
521 privilege_remove( privilege
);
524 LIST_FOR_EACH_SAFE( cursor
, cursor_next
, &token
->groups
)
526 struct group
*group
= LIST_ENTRY( cursor
, struct group
, entry
);
527 list_remove( &group
->entry
);
531 free( token
->default_dacl
);
534 /* creates a new token.
535 * groups may be NULL if group_count is 0.
536 * privs may be NULL if priv_count is 0.
537 * default_dacl may be NULL, indicating that all objects created by the user
539 * modified_id may be NULL, indicating that a new modified_id luid should be
542 static struct token
*create_token( unsigned int primary
, unsigned int session_id
, const SID
*user
,
543 const SID_AND_ATTRIBUTES
*groups
, unsigned int group_count
,
544 const LUID_AND_ATTRIBUTES
*privs
, unsigned int priv_count
,
545 const ACL
*default_dacl
, TOKEN_SOURCE source
,
546 const luid_t
*modified_id
,
547 int impersonation_level
, int elevation
)
549 struct token
*token
= alloc_object( &token_ops
);
554 allocate_luid( &token
->token_id
);
556 token
->modified_id
= *modified_id
;
558 allocate_luid( &token
->modified_id
);
559 list_init( &token
->privileges
);
560 list_init( &token
->groups
);
561 token
->primary
= primary
;
562 token
->session_id
= session_id
;
563 /* primary tokens don't have impersonation levels */
565 token
->impersonation_level
= -1;
567 token
->impersonation_level
= impersonation_level
;
568 token
->default_dacl
= NULL
;
569 token
->primary_group
= NULL
;
570 token
->elevation
= elevation
;
573 token
->user
= memdup( user
, security_sid_len( user
));
576 release_object( token
);
581 for (i
= 0; i
< group_count
; i
++)
583 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[((const SID
*)groups
[i
].Sid
)->SubAuthorityCount
] );
584 struct group
*group
= mem_alloc( size
);
588 release_object( token
);
591 memcpy( &group
->sid
, groups
[i
].Sid
, security_sid_len( groups
[i
].Sid
));
592 group
->enabled
= TRUE
;
594 group
->logon
= (groups
[i
].Attributes
& SE_GROUP_LOGON_ID
) != 0;
595 group
->mandatory
= (groups
[i
].Attributes
& SE_GROUP_MANDATORY
) != 0;
596 group
->owner
= (groups
[i
].Attributes
& SE_GROUP_OWNER
) != 0;
597 group
->resource
= FALSE
;
598 group
->deny_only
= FALSE
;
599 list_add_tail( &token
->groups
, &group
->entry
);
600 /* Use first owner capable group as owner and primary group */
601 if (!token
->primary_group
&& group
->owner
)
603 token
->owner
= &group
->sid
;
604 token
->primary_group
= &group
->sid
;
608 /* copy privileges */
609 for (i
= 0; i
< priv_count
; i
++)
611 /* note: we don't check uniqueness: the caller must make sure
612 * privs doesn't contain any duplicate luids */
613 if (!privilege_add( token
, &privs
[i
].Luid
,
614 privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
))
616 release_object( token
);
623 token
->default_dacl
= memdup( default_dacl
, default_dacl
->AclSize
);
624 if (!token
->default_dacl
)
626 release_object( token
);
631 token
->source
= source
;
636 static int filter_group( struct group
*group
, const SID
*filter
, unsigned int count
)
640 for (i
= 0; i
< count
; i
++)
642 if (security_equal_sid( &group
->sid
, filter
)) return 1;
643 filter
= (const SID
*)((char *)filter
+ security_sid_len( filter
));
649 static int filter_privilege( struct privilege
*privilege
, const LUID_AND_ATTRIBUTES
*filter
, unsigned int count
)
653 for (i
= 0; i
< count
; i
++)
655 if (!memcmp( &privilege
->luid
, &filter
[i
].Luid
, sizeof(LUID
) ))
662 struct token
*token_duplicate( struct token
*src_token
, unsigned primary
,
663 int impersonation_level
, const struct security_descriptor
*sd
,
664 const LUID_AND_ATTRIBUTES
*remove_privs
, unsigned int remove_priv_count
,
665 const SID
*remove_groups
, unsigned int remove_group_count
)
667 const luid_t
*modified_id
=
668 primary
|| (impersonation_level
== src_token
->impersonation_level
) ?
669 &src_token
->modified_id
: NULL
;
670 struct token
*token
= NULL
;
671 struct privilege
*privilege
;
675 (impersonation_level
< SecurityAnonymous
||
676 impersonation_level
> SecurityDelegation
||
677 (!src_token
->primary
&& (impersonation_level
> src_token
->impersonation_level
))))
679 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
683 token
= create_token( primary
, src_token
->session_id
, src_token
->user
, NULL
, 0,
684 NULL
, 0, src_token
->default_dacl
,
685 src_token
->source
, modified_id
,
686 impersonation_level
, src_token
->elevation
);
687 if (!token
) return token
;
690 token
->primary_group
= NULL
;
691 LIST_FOR_EACH_ENTRY( group
, &src_token
->groups
, struct group
, entry
)
693 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[group
->sid
.SubAuthorityCount
] );
694 struct group
*newgroup
= mem_alloc( size
);
697 release_object( token
);
700 memcpy( newgroup
, group
, size
);
701 if (filter_group( group
, remove_groups
, remove_group_count
))
703 newgroup
->enabled
= 0;
705 newgroup
->deny_only
= 1;
707 list_add_tail( &token
->groups
, &newgroup
->entry
);
708 if (src_token
->primary_group
== &group
->sid
)
710 token
->owner
= &newgroup
->sid
;
711 token
->primary_group
= &newgroup
->sid
;
714 assert( token
->primary_group
);
716 /* copy privileges */
717 LIST_FOR_EACH_ENTRY( privilege
, &src_token
->privileges
, struct privilege
, entry
)
719 if (filter_privilege( privilege
, remove_privs
, remove_priv_count
)) continue;
720 if (!privilege_add( token
, &privilege
->luid
, privilege
->enabled
))
722 release_object( token
);
727 if (sd
) default_set_sd( &token
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
728 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
733 static ACL
*create_default_dacl( const SID
*user
)
735 ACCESS_ALLOWED_ACE
*aaa
;
738 size_t default_dacl_size
= sizeof(ACL
) +
739 2*(sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
740 sizeof(local_system_sid
) +
741 security_sid_len( user
);
743 default_dacl
= mem_alloc( default_dacl_size
);
744 if (!default_dacl
) return NULL
;
746 default_dacl
->AclRevision
= ACL_REVISION
;
747 default_dacl
->Sbz1
= 0;
748 default_dacl
->AclSize
= default_dacl_size
;
749 default_dacl
->AceCount
= 2;
750 default_dacl
->Sbz2
= 0;
752 /* GENERIC_ALL for Local System */
753 aaa
= (ACCESS_ALLOWED_ACE
*)(default_dacl
+ 1);
754 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
755 aaa
->Header
.AceFlags
= 0;
756 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
757 sizeof(local_system_sid
);
758 aaa
->Mask
= GENERIC_ALL
;
759 sid
= (SID
*)&aaa
->SidStart
;
760 memcpy( sid
, &local_system_sid
, sizeof(local_system_sid
) );
762 /* GENERIC_ALL for specified user */
763 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
764 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
765 aaa
->Header
.AceFlags
= 0;
766 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) + security_sid_len( user
);
767 aaa
->Mask
= GENERIC_ALL
;
768 sid
= (SID
*)&aaa
->SidStart
;
769 memcpy( sid
, user
, security_sid_len( user
));
776 SID_IDENTIFIER_AUTHORITY idauth
;
778 unsigned int subauth
[MAX_SUBAUTH_COUNT
];
781 static struct security_descriptor
*create_security_label_sd( struct token
*token
, PSID label_sid
)
783 size_t sid_len
= security_sid_len( label_sid
), sacl_size
, sd_size
;
784 SYSTEM_MANDATORY_LABEL_ACE
*smla
;
785 struct security_descriptor
*sd
;
788 sacl_size
= sizeof(ACL
) + FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
) + sid_len
;
789 sd_size
= sizeof(struct security_descriptor
) + sacl_size
;
790 if (!(sd
= mem_alloc( sd_size
)))
793 sd
->control
= SE_SACL_PRESENT
;
796 sd
->sacl_len
= sacl_size
;
799 sacl
= (ACL
*)(sd
+ 1);
800 sacl
->AclRevision
= ACL_REVISION
;
802 sacl
->AclSize
= sacl_size
;
806 smla
= (SYSTEM_MANDATORY_LABEL_ACE
*)(sacl
+ 1);
807 smla
->Header
.AceType
= SYSTEM_MANDATORY_LABEL_ACE_TYPE
;
808 smla
->Header
.AceFlags
= 0;
809 smla
->Header
.AceSize
= FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE
, SidStart
) + sid_len
;
810 smla
->Mask
= SYSTEM_MANDATORY_LABEL_NO_WRITE_UP
;
811 memcpy( &smla
->SidStart
, label_sid
, sid_len
);
813 assert( sd_is_valid( sd
, sd_size
) );
817 int token_assign_label( struct token
*token
, PSID label
)
819 struct security_descriptor
*sd
;
822 if ((sd
= create_security_label_sd( token
, label
)))
824 ret
= set_sd_defaults_from_token( &token
->obj
, sd
, LABEL_SECURITY_INFORMATION
, token
);
831 struct token
*get_token_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
833 return (struct token
*)get_handle_obj( process
, handle
, access
, &token_ops
);
836 struct token
*token_create_admin( unsigned primary
, int impersonation_level
, int elevation
, unsigned int session_id
)
838 struct token
*token
= NULL
;
839 static const SID_IDENTIFIER_AUTHORITY nt_authority
= { SECURITY_NT_AUTHORITY
};
840 static const unsigned int alias_admins_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
};
841 static const unsigned int alias_users_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
};
842 /* on Windows, this value changes every time the user logs on */
843 static const unsigned int logon_subauth
[] = { SECURITY_LOGON_IDS_RID
, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
844 PSID alias_admins_sid
;
845 PSID alias_users_sid
;
847 const SID
*user_sid
= security_unix_uid_to_sid( getuid() );
848 ACL
*default_dacl
= create_default_dacl( user_sid
);
850 alias_admins_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( alias_admins_subauth
),
851 alias_admins_subauth
);
852 alias_users_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( alias_users_subauth
),
853 alias_users_subauth
);
854 logon_sid
= security_sid_alloc( &nt_authority
, ARRAY_SIZE( logon_subauth
), logon_subauth
);
856 if (alias_admins_sid
&& alias_users_sid
&& logon_sid
&& default_dacl
)
858 const LUID_AND_ATTRIBUTES admin_privs
[] =
860 { SeChangeNotifyPrivilege
, SE_PRIVILEGE_ENABLED
},
861 { SeTcbPrivilege
, 0 },
862 { SeSecurityPrivilege
, 0 },
863 { SeBackupPrivilege
, 0 },
864 { SeRestorePrivilege
, 0 },
865 { SeSystemtimePrivilege
, 0 },
866 { SeShutdownPrivilege
, 0 },
867 { SeRemoteShutdownPrivilege
, 0 },
868 { SeTakeOwnershipPrivilege
, 0 },
869 { SeDebugPrivilege
, 0 },
870 { SeSystemEnvironmentPrivilege
, 0 },
871 { SeSystemProfilePrivilege
, 0 },
872 { SeProfileSingleProcessPrivilege
, 0 },
873 { SeIncreaseBasePriorityPrivilege
, 0 },
874 { SeLoadDriverPrivilege
, SE_PRIVILEGE_ENABLED
},
875 { SeCreatePagefilePrivilege
, 0 },
876 { SeIncreaseQuotaPrivilege
, 0 },
877 { SeUndockPrivilege
, 0 },
878 { SeManageVolumePrivilege
, 0 },
879 { SeImpersonatePrivilege
, SE_PRIVILEGE_ENABLED
},
880 { SeCreateGlobalPrivilege
, SE_PRIVILEGE_ENABLED
},
882 /* note: we don't include non-builtin groups here for the user -
883 * telling us these is the job of a client-side program */
884 const SID_AND_ATTRIBUTES admin_groups
[] =
886 { security_world_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
887 { security_local_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
888 { security_interactive_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
889 { security_authenticated_user_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
890 { security_domain_users_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_OWNER
},
891 { alias_admins_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_OWNER
},
892 { alias_users_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
893 { logon_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_LOGON_ID
},
895 static const TOKEN_SOURCE admin_source
= {"SeMgr", {0, 0}};
896 token
= create_token( primary
, session_id
, user_sid
, admin_groups
, ARRAY_SIZE( admin_groups
),
897 admin_privs
, ARRAY_SIZE( admin_privs
), default_dacl
,
898 admin_source
, NULL
, impersonation_level
, elevation
);
899 /* we really need a primary group */
900 assert( token
->primary_group
);
904 free( alias_admins_sid
);
905 free( alias_users_sid
);
906 free( default_dacl
);
911 static struct privilege
*token_find_privilege( struct token
*token
, const LUID
*luid
, int enabled_only
)
913 struct privilege
*privilege
;
914 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
916 if (is_equal_luid( luid
, &privilege
->luid
))
918 if (enabled_only
&& !privilege
->enabled
)
926 static unsigned int token_adjust_privileges( struct token
*token
, const LUID_AND_ATTRIBUTES
*privs
,
927 unsigned int count
, LUID_AND_ATTRIBUTES
*mod_privs
,
928 unsigned int mod_privs_count
)
930 unsigned int i
, modified_count
= 0;
932 /* mark as modified */
933 allocate_luid( &token
->modified_id
);
935 for (i
= 0; i
< count
; i
++)
937 struct privilege
*privilege
=
938 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
941 set_error( STATUS_NOT_ALL_ASSIGNED
);
945 if (privs
[i
].Attributes
& SE_PRIVILEGE_REMOVED
)
946 privilege_remove( privilege
);
949 /* save previous state for caller */
952 luid_and_attr_from_privilege(mod_privs
, privilege
);
958 if (privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
)
959 privilege
->enabled
= TRUE
;
961 privilege
->enabled
= FALSE
;
964 return modified_count
;
967 static void token_disable_privileges( struct token
*token
)
969 struct privilege
*privilege
;
971 /* mark as modified */
972 allocate_luid( &token
->modified_id
);
974 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
975 privilege
->enabled
= FALSE
;
978 int token_check_privileges( struct token
*token
, int all_required
,
979 const LUID_AND_ATTRIBUTES
*reqprivs
,
980 unsigned int count
, LUID_AND_ATTRIBUTES
*usedprivs
)
982 unsigned int i
, enabled_count
= 0;
984 for (i
= 0; i
< count
; i
++)
986 struct privilege
*privilege
=
987 token_find_privilege( token
, &reqprivs
[i
].Luid
, TRUE
);
990 usedprivs
[i
] = reqprivs
[i
];
992 if (privilege
&& privilege
->enabled
)
996 usedprivs
[i
].Attributes
|= SE_PRIVILEGE_USED_FOR_ACCESS
;
1001 return (enabled_count
== count
);
1003 return (enabled_count
> 0);
1006 int token_sid_present( struct token
*token
, const SID
*sid
, int deny
)
1008 struct group
*group
;
1010 if (security_equal_sid( token
->user
, sid
)) return TRUE
;
1012 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, struct group
, entry
)
1014 if (!group
->enabled
) continue;
1015 if (group
->deny_only
&& !deny
) continue;
1017 if (security_equal_sid( &group
->sid
, sid
)) return TRUE
;
1023 /* Checks access to a security descriptor. 'sd' must have been validated by
1024 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
1025 * the reason. 'status' parameter will indicate if access is granted or denied.
1027 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
1029 static unsigned int token_access_check( struct token
*token
,
1030 const struct security_descriptor
*sd
,
1031 unsigned int desired_access
,
1032 LUID_AND_ATTRIBUTES
*privs
,
1033 unsigned int *priv_count
,
1034 const generic_map_t
*mapping
,
1035 unsigned int *granted_access
,
1036 unsigned int *status
)
1038 unsigned int current_access
= 0;
1039 unsigned int denied_access
= 0;
1043 const ACE_HEADER
*ace
;
1046 /* assume no access rights */
1047 *granted_access
= 0;
1049 /* fail if desired_access contains generic rights */
1050 if (desired_access
& (GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
|GENERIC_ALL
))
1052 if (priv_count
) *priv_count
= 0;
1053 return STATUS_GENERIC_NOT_MAPPED
;
1056 dacl
= sd_get_dacl( sd
, &dacl_present
);
1057 owner
= sd_get_owner( sd
);
1058 if (!owner
|| !sd_get_group( sd
))
1060 if (priv_count
) *priv_count
= 0;
1061 return STATUS_INVALID_SECURITY_DESCR
;
1064 /* 1: Grant desired access if the object is unprotected */
1065 if (!dacl_present
|| !dacl
)
1067 if (priv_count
) *priv_count
= 0;
1068 if (desired_access
& MAXIMUM_ALLOWED
)
1069 *granted_access
= mapping
->all
;
1071 *granted_access
= desired_access
;
1072 return *status
= STATUS_SUCCESS
;
1075 /* 2: Check if caller wants access to system security part. Note: access
1076 * is only granted if specifically asked for */
1077 if (desired_access
& ACCESS_SYSTEM_SECURITY
)
1079 const LUID_AND_ATTRIBUTES security_priv
= { SeSecurityPrivilege
, 0 };
1080 LUID_AND_ATTRIBUTES retpriv
= security_priv
;
1081 if (token_check_privileges( token
, TRUE
, &security_priv
, 1, &retpriv
))
1085 /* assumes that there will only be one privilege to return */
1086 if (*priv_count
>= 1)
1094 return STATUS_BUFFER_TOO_SMALL
;
1097 current_access
|= ACCESS_SYSTEM_SECURITY
;
1098 if (desired_access
== current_access
)
1100 *granted_access
= current_access
;
1101 return *status
= STATUS_SUCCESS
;
1106 if (priv_count
) *priv_count
= 0;
1107 *status
= STATUS_PRIVILEGE_NOT_HELD
;
1108 return STATUS_SUCCESS
;
1111 else if (priv_count
) *priv_count
= 0;
1113 /* 3: Check whether the token is the owner */
1114 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
1115 * checked when a "set owner" call is made, overriding the access rights
1116 * determined here. */
1117 if (token_sid_present( token
, owner
, FALSE
))
1119 current_access
|= (STANDARD_RIGHTS_REQUIRED
| SYNCHRONIZE
);
1120 if (desired_access
== current_access
)
1122 *granted_access
= current_access
;
1123 return *status
= STATUS_SUCCESS
;
1127 /* 4: Grant rights according to the DACL */
1128 ace
= (const ACE_HEADER
*)(dacl
+ 1);
1129 for (i
= 0; i
< dacl
->AceCount
; i
++, ace
= ace_next( ace
))
1131 const ACCESS_ALLOWED_ACE
*aa_ace
;
1132 const ACCESS_DENIED_ACE
*ad_ace
;
1135 if (ace
->AceFlags
& INHERIT_ONLY_ACE
)
1138 switch (ace
->AceType
)
1140 case ACCESS_DENIED_ACE_TYPE
:
1141 ad_ace
= (const ACCESS_DENIED_ACE
*)ace
;
1142 sid
= (const SID
*)&ad_ace
->SidStart
;
1143 if (token_sid_present( token
, sid
, TRUE
))
1145 unsigned int access
= map_access( ad_ace
->Mask
, mapping
);
1146 if (desired_access
& MAXIMUM_ALLOWED
)
1147 denied_access
|= access
;
1150 denied_access
|= (access
& ~current_access
);
1151 if (desired_access
& access
) goto done
;
1155 case ACCESS_ALLOWED_ACE_TYPE
:
1156 aa_ace
= (const ACCESS_ALLOWED_ACE
*)ace
;
1157 sid
= (const SID
*)&aa_ace
->SidStart
;
1158 if (token_sid_present( token
, sid
, FALSE
))
1160 unsigned int access
= map_access( aa_ace
->Mask
, mapping
);
1161 if (desired_access
& MAXIMUM_ALLOWED
)
1162 current_access
|= access
;
1164 current_access
|= (access
& ~denied_access
);
1169 /* don't bother carrying on checking if we've already got all of
1171 if (desired_access
== *granted_access
)
1176 if (desired_access
& MAXIMUM_ALLOWED
)
1177 *granted_access
= current_access
& ~denied_access
;
1179 if ((current_access
& desired_access
) == desired_access
)
1180 *granted_access
= current_access
& desired_access
;
1182 *granted_access
= 0;
1184 *status
= *granted_access
? STATUS_SUCCESS
: STATUS_ACCESS_DENIED
;
1185 return STATUS_SUCCESS
;
1188 const ACL
*token_get_default_dacl( struct token
*token
)
1190 return token
->default_dacl
;
1193 const SID
*token_get_user( struct token
*token
)
1198 const SID
*token_get_primary_group( struct token
*token
)
1200 return token
->primary_group
;
1203 unsigned int token_get_session_id( struct token
*token
)
1205 return token
->session_id
;
1208 int check_object_access(struct token
*token
, struct object
*obj
, unsigned int *access
)
1210 generic_map_t mapping
;
1211 unsigned int status
;
1215 token
= current
->token
? current
->token
: current
->process
->token
;
1217 mapping
.all
= obj
->ops
->map_access( obj
, GENERIC_ALL
);
1221 if (*access
& MAXIMUM_ALLOWED
) *access
= mapping
.all
;
1225 mapping
.read
= obj
->ops
->map_access( obj
, GENERIC_READ
);
1226 mapping
.write
= obj
->ops
->map_access( obj
, GENERIC_WRITE
);
1227 mapping
.exec
= obj
->ops
->map_access( obj
, GENERIC_EXECUTE
);
1229 res
= token_access_check( token
, obj
->sd
, *access
, NULL
, NULL
,
1230 &mapping
, access
, &status
) == STATUS_SUCCESS
&&
1231 status
== STATUS_SUCCESS
;
1233 if (!res
) set_error( STATUS_ACCESS_DENIED
);
1238 /* open a security token */
1239 DECL_HANDLER(open_token
)
1241 if (req
->flags
& OPEN_TOKEN_THREAD
)
1243 struct thread
*thread
= get_thread_from_handle( req
->handle
, 0 );
1248 if (!thread
->token
->primary
&& thread
->token
->impersonation_level
<= SecurityAnonymous
)
1249 set_error( STATUS_CANT_OPEN_ANONYMOUS
);
1251 reply
->token
= alloc_handle( current
->process
, thread
->token
,
1252 req
->access
, req
->attributes
);
1255 set_error( STATUS_NO_TOKEN
);
1256 release_object( thread
);
1261 struct process
*process
= get_process_from_handle( req
->handle
, 0 );
1265 reply
->token
= alloc_handle( current
->process
, process
->token
, req
->access
,
1268 set_error( STATUS_NO_TOKEN
);
1269 release_object( process
);
1274 /* adjust the privileges held by a token */
1275 DECL_HANDLER(adjust_token_privileges
)
1277 struct token
*token
;
1278 unsigned int access
= TOKEN_ADJUST_PRIVILEGES
;
1280 if (req
->get_modified_state
) access
|= TOKEN_QUERY
;
1282 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1283 access
, &token_ops
)))
1285 const LUID_AND_ATTRIBUTES
*privs
= get_req_data();
1286 LUID_AND_ATTRIBUTES
*modified_privs
= NULL
;
1287 unsigned int priv_count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1288 unsigned int modified_priv_count
= 0;
1290 if (req
->get_modified_state
&& !req
->disable_all
)
1293 /* count modified privs */
1294 for (i
= 0; i
< priv_count
; i
++)
1296 struct privilege
*privilege
=
1297 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
1298 if (privilege
&& req
->get_modified_state
)
1299 modified_priv_count
++;
1301 reply
->len
= modified_priv_count
;
1302 modified_priv_count
= min( modified_priv_count
, get_reply_max_size() / sizeof(*modified_privs
) );
1303 if (modified_priv_count
)
1304 modified_privs
= set_reply_data_size( modified_priv_count
* sizeof(*modified_privs
) );
1306 reply
->len
= modified_priv_count
* sizeof(*modified_privs
);
1308 if (req
->disable_all
)
1309 token_disable_privileges( token
);
1311 token_adjust_privileges( token
, privs
, priv_count
, modified_privs
, modified_priv_count
);
1313 release_object( token
);
1317 /* retrieves the list of privileges that may be held be the token */
1318 DECL_HANDLER(get_token_privileges
)
1320 struct token
*token
;
1322 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1327 LUID_AND_ATTRIBUTES
*privs
;
1328 struct privilege
*privilege
;
1330 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1333 reply
->len
= priv_count
* sizeof(*privs
);
1334 if (reply
->len
<= get_reply_max_size())
1336 privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1340 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1342 luid_and_attr_from_privilege( &privs
[i
], privilege
);
1348 set_error(STATUS_BUFFER_TOO_SMALL
);
1350 release_object( token
);
1354 /* creates a duplicate of the token */
1355 DECL_HANDLER(duplicate_token
)
1357 struct token
*src_token
;
1358 struct unicode_str name
;
1359 const struct security_descriptor
*sd
;
1360 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1362 if (!objattr
) return;
1364 if ((src_token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1368 struct token
*token
= token_duplicate( src_token
, req
->primary
, req
->impersonation_level
, sd
, NULL
, 0, NULL
, 0 );
1371 unsigned int access
= req
->access
? req
->access
: get_handle_access( current
->process
, req
->handle
);
1372 reply
->new_handle
= alloc_handle_no_access_check( current
->process
, token
, access
, objattr
->attributes
);
1373 release_object( token
);
1375 release_object( src_token
);
1379 /* creates a restricted version of a token */
1380 DECL_HANDLER(filter_token
)
1382 struct token
*src_token
;
1384 if ((src_token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
, TOKEN_DUPLICATE
, &token_ops
)))
1386 const LUID_AND_ATTRIBUTES
*filter_privileges
= get_req_data();
1387 unsigned int priv_count
, group_count
;
1388 const SID
*filter_groups
;
1389 struct token
*token
;
1391 priv_count
= min( req
->privileges_size
, get_req_data_size() ) / sizeof(LUID_AND_ATTRIBUTES
);
1392 filter_groups
= (const SID
*)((char *)filter_privileges
+ priv_count
* sizeof(LUID_AND_ATTRIBUTES
));
1393 group_count
= get_sid_count( filter_groups
, get_req_data_size() - priv_count
* sizeof(LUID_AND_ATTRIBUTES
) );
1395 token
= token_duplicate( src_token
, src_token
->primary
, src_token
->impersonation_level
, NULL
,
1396 filter_privileges
, priv_count
, filter_groups
, group_count
);
1399 unsigned int access
= get_handle_access( current
->process
, req
->handle
);
1400 reply
->new_handle
= alloc_handle_no_access_check( current
->process
, token
, access
, 0 );
1401 release_object( token
);
1403 release_object( src_token
);
1407 /* checks the specified privileges are held by the token */
1408 DECL_HANDLER(check_token_privileges
)
1410 struct token
*token
;
1412 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1416 unsigned int count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1418 if (!token
->primary
&& token
->impersonation_level
<= SecurityAnonymous
)
1419 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1420 else if (get_reply_max_size() >= count
* sizeof(LUID_AND_ATTRIBUTES
))
1422 LUID_AND_ATTRIBUTES
*usedprivs
= set_reply_data_size( count
* sizeof(*usedprivs
) );
1423 reply
->has_privileges
= token_check_privileges( token
, req
->all_required
, get_req_data(), count
, usedprivs
);
1426 set_error( STATUS_BUFFER_OVERFLOW
);
1427 release_object( token
);
1431 /* checks that a user represented by a token is allowed to access an object
1432 * represented by a security descriptor */
1433 DECL_HANDLER(access_check
)
1435 data_size_t sd_size
= get_req_data_size();
1436 const struct security_descriptor
*sd
= get_req_data();
1437 struct token
*token
;
1439 if (!sd_is_valid( sd
, sd_size
))
1441 set_error( STATUS_ACCESS_VIOLATION
);
1445 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1449 unsigned int status
;
1450 LUID_AND_ATTRIBUTES priv
;
1451 unsigned int priv_count
= 1;
1453 memset(&priv
, 0, sizeof(priv
));
1455 /* only impersonation tokens may be used with this function */
1458 set_error( STATUS_NO_IMPERSONATION_TOKEN
);
1459 release_object( token
);
1462 /* anonymous impersonation tokens can't be used */
1463 if (token
->impersonation_level
<= SecurityAnonymous
)
1465 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1466 release_object( token
);
1470 status
= token_access_check( token
, sd
, req
->desired_access
, &priv
, &priv_count
, &req
->mapping
,
1471 &reply
->access_granted
, &reply
->access_status
);
1473 reply
->privileges_len
= priv_count
*sizeof(LUID_AND_ATTRIBUTES
);
1475 if ((priv_count
> 0) && (reply
->privileges_len
<= get_reply_max_size()))
1477 LUID_AND_ATTRIBUTES
*privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1478 memcpy( privs
, &priv
, sizeof(priv
) );
1481 set_error( status
);
1482 release_object( token
);
1486 /* retrieves an SID from the token */
1487 DECL_HANDLER(get_token_sid
)
1489 struct token
*token
;
1493 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
, TOKEN_QUERY
, &token_ops
)))
1495 const SID
*sid
= NULL
;
1497 switch (req
->which_sid
)
1500 assert(token
->user
);
1503 case TokenPrimaryGroup
:
1504 sid
= token
->primary_group
;
1510 sid
= (const SID
*)&builtin_logon_sid
;
1513 set_error( STATUS_INVALID_PARAMETER
);
1519 reply
->sid_len
= security_sid_len( sid
);
1520 if (reply
->sid_len
<= get_reply_max_size()) set_reply_data( sid
, reply
->sid_len
);
1521 else set_error( STATUS_BUFFER_TOO_SMALL
);
1523 release_object( token
);
1527 /* retrieves the groups that the user represented by the token belongs to */
1528 DECL_HANDLER(get_token_groups
)
1530 struct token
*token
;
1532 reply
->user_len
= 0;
1534 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1538 size_t size_needed
= sizeof(struct token_groups
);
1539 size_t sid_size
= 0;
1540 unsigned int group_count
= 0;
1541 const struct group
*group
;
1543 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1546 sid_size
+= security_sid_len( &group
->sid
);
1548 size_needed
+= sid_size
;
1549 /* attributes size */
1550 size_needed
+= sizeof(unsigned int) * group_count
;
1552 /* reply buffer contains size_needed bytes formatted as:
1555 unsigned int attrib[count];
1558 user_len includes extra data needed for TOKEN_GROUPS representation,
1559 required caller buffer size calculated here to avoid extra server call */
1560 reply
->user_len
= FIELD_OFFSET( TOKEN_GROUPS
, Groups
[group_count
] ) + sid_size
;
1562 if (reply
->user_len
<= get_reply_max_size())
1564 struct token_groups
*tg
= set_reply_data_size( size_needed
);
1567 unsigned int *attr_ptr
= (unsigned int *)(tg
+ 1);
1568 SID
*sid_ptr
= (SID
*)(attr_ptr
+ group_count
);
1570 tg
->count
= group_count
;
1572 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1576 if (group
->mandatory
) *attr_ptr
|= SE_GROUP_MANDATORY
;
1577 if (group
->def
) *attr_ptr
|= SE_GROUP_ENABLED_BY_DEFAULT
;
1578 if (group
->enabled
) *attr_ptr
|= SE_GROUP_ENABLED
;
1579 if (group
->owner
) *attr_ptr
|= SE_GROUP_OWNER
;
1580 if (group
->deny_only
) *attr_ptr
|= SE_GROUP_USE_FOR_DENY_ONLY
;
1581 if (group
->resource
) *attr_ptr
|= SE_GROUP_RESOURCE
;
1582 if (group
->logon
) *attr_ptr
|= SE_GROUP_LOGON_ID
;
1584 memcpy(sid_ptr
, &group
->sid
, security_sid_len( &group
->sid
));
1586 sid_ptr
= (SID
*)((char *)sid_ptr
+ security_sid_len( &group
->sid
));
1591 else set_error( STATUS_BUFFER_TOO_SMALL
);
1593 release_object( token
);
1597 DECL_HANDLER(get_token_info
)
1599 struct token
*token
;
1601 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
, TOKEN_QUERY
, &token_ops
)))
1603 reply
->token_id
= token
->token_id
;
1604 reply
->modified_id
= token
->modified_id
;
1605 reply
->session_id
= token
->session_id
;
1606 reply
->primary
= token
->primary
;
1607 reply
->impersonation_level
= token
->impersonation_level
;
1608 reply
->elevation
= token
->elevation
;
1609 reply
->group_count
= list_count( &token
->groups
);
1610 reply
->privilege_count
= list_count( &token
->privileges
);
1611 release_object( token
);
1615 DECL_HANDLER(get_token_default_dacl
)
1617 struct token
*token
;
1621 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1625 if (token
->default_dacl
)
1626 reply
->acl_len
= token
->default_dacl
->AclSize
;
1628 if (reply
->acl_len
<= get_reply_max_size())
1630 ACL
*acl_reply
= set_reply_data_size( reply
->acl_len
);
1632 memcpy( acl_reply
, token
->default_dacl
, reply
->acl_len
);
1634 else set_error( STATUS_BUFFER_TOO_SMALL
);
1636 release_object( token
);
1640 DECL_HANDLER(set_token_default_dacl
)
1642 struct token
*token
;
1644 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1645 TOKEN_ADJUST_DEFAULT
,
1648 const ACL
*acl
= get_req_data();
1649 unsigned int acl_size
= get_req_data_size();
1651 free( token
->default_dacl
);
1652 token
->default_dacl
= NULL
;
1655 token
->default_dacl
= memdup( acl
, acl_size
);
1657 release_object( token
);
1661 DECL_HANDLER(create_linked_token
)
1663 struct token
*token
, *linked
;
1666 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1667 TOKEN_QUERY
, &token_ops
)))
1669 switch (token
->elevation
)
1671 case TokenElevationTypeFull
:
1672 elevation
= TokenElevationTypeLimited
;
1674 case TokenElevationTypeLimited
:
1675 elevation
= TokenElevationTypeFull
;
1678 release_object( token
);
1681 if ((linked
= token_create_admin( FALSE
, SecurityIdentification
, elevation
, token
->session_id
)))
1683 reply
->linked
= alloc_handle( current
->process
, linked
, TOKEN_ALL_ACCESS
, 0 );
1684 release_object( linked
);
1686 release_object( token
);