wined3d: Don't validate the frontbuffer's DRAWABLE location in wined3d_swapchain_resi...
[wine.git] / server / token.c
blob4df8d2e0c6eba8a3717c06ae5ba8182923d20d37
1 /*
2 * Tokens
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
23 #include "config.h"
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <unistd.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winternl.h"
37 #include "handle.h"
38 #include "thread.h"
39 #include "process.h"
40 #include "request.h"
41 #include "security.h"
43 #define MAX_SUBAUTH_COUNT 1
45 const struct luid SeIncreaseQuotaPrivilege = { 5, 0 };
46 const struct luid SeTcbPrivilege = { 7, 0 };
47 const struct luid SeSecurityPrivilege = { 8, 0 };
48 const struct luid SeTakeOwnershipPrivilege = { 9, 0 };
49 const struct luid SeLoadDriverPrivilege = { 10, 0 };
50 const struct luid SeSystemProfilePrivilege = { 11, 0 };
51 const struct luid SeSystemtimePrivilege = { 12, 0 };
52 const struct luid SeProfileSingleProcessPrivilege = { 13, 0 };
53 const struct luid SeIncreaseBasePriorityPrivilege = { 14, 0 };
54 const struct luid SeCreatePagefilePrivilege = { 15, 0 };
55 const struct luid SeBackupPrivilege = { 17, 0 };
56 const struct luid SeRestorePrivilege = { 18, 0 };
57 const struct luid SeShutdownPrivilege = { 19, 0 };
58 const struct luid SeDebugPrivilege = { 20, 0 };
59 const struct luid SeSystemEnvironmentPrivilege = { 22, 0 };
60 const struct luid SeChangeNotifyPrivilege = { 23, 0 };
61 const struct luid SeRemoteShutdownPrivilege = { 24, 0 };
62 const struct luid SeUndockPrivilege = { 25, 0 };
63 const struct luid SeManageVolumePrivilege = { 28, 0 };
64 const struct luid SeImpersonatePrivilege = { 29, 0 };
65 const struct luid SeCreateGlobalPrivilege = { 30, 0 };
67 struct sid_attrs
69 const struct sid *sid;
70 unsigned int attrs;
73 const struct sid world_sid = { SID_REVISION, 1, SECURITY_WORLD_SID_AUTHORITY, { SECURITY_WORLD_RID } };
74 const struct sid local_system_sid = { SID_REVISION, 1, SECURITY_NT_AUTHORITY, { SECURITY_LOCAL_SYSTEM_RID } };
75 const struct sid high_label_sid = { SID_REVISION, 1, SECURITY_MANDATORY_LABEL_AUTHORITY, { SECURITY_MANDATORY_HIGH_RID } };
76 const struct sid local_user_sid = { SID_REVISION, 5, SECURITY_NT_AUTHORITY, { SECURITY_NT_NON_UNIQUE, 0, 0, 0, 1000 } };
77 const struct sid builtin_admins_sid = { SID_REVISION, 2, SECURITY_NT_AUTHORITY, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS } };
78 const struct sid builtin_users_sid = { SID_REVISION, 2, SECURITY_NT_AUTHORITY, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS } };
79 const struct sid domain_users_sid = { SID_REVISION, 5, SECURITY_NT_AUTHORITY, { SECURITY_NT_NON_UNIQUE, 0, 0, 0, DOMAIN_GROUP_RID_USERS } };
81 static const struct sid local_sid = { SID_REVISION, 1, SECURITY_LOCAL_SID_AUTHORITY, { SECURITY_LOCAL_RID } };
82 static const struct sid interactive_sid = { SID_REVISION, 1, SECURITY_NT_AUTHORITY, { SECURITY_INTERACTIVE_RID } };
83 static const struct sid anonymous_logon_sid = { SID_REVISION, 1, SECURITY_NT_AUTHORITY, { SECURITY_ANONYMOUS_LOGON_RID } };
84 static const struct sid authenticated_user_sid = { SID_REVISION, 1, SECURITY_NT_AUTHORITY, { SECURITY_AUTHENTICATED_USER_RID } };
86 static struct luid prev_luid_value = { 1000, 0 };
88 static const WCHAR token_name[] = {'T','o','k','e','n'};
90 struct type_descr token_type =
92 { token_name, sizeof(token_name) }, /* name */
93 TOKEN_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
94 { /* mapping */
95 STANDARD_RIGHTS_READ | TOKEN_QUERY_SOURCE | TOKEN_QUERY | TOKEN_DUPLICATE,
96 STANDARD_RIGHTS_WRITE | TOKEN_ADJUST_SESSIONID | TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_GROUPS
97 | TOKEN_ADJUST_PRIVILEGES,
98 STANDARD_RIGHTS_EXECUTE | TOKEN_IMPERSONATE | TOKEN_ASSIGN_PRIMARY,
99 TOKEN_ALL_ACCESS
103 struct token
105 struct object obj; /* object header */
106 struct luid token_id; /* system-unique id of token */
107 struct luid modified_id; /* new id allocated every time token is modified */
108 struct list privileges; /* privileges available to the token */
109 struct list groups; /* groups that the user of this token belongs to (sid_and_attributes) */
110 struct sid *user; /* SID of user this token represents */
111 struct sid *owner; /* SID of owner (points to user or one of groups) */
112 struct sid *primary_group; /* SID of user's primary group (points to one of groups) */
113 unsigned int primary; /* is this a primary or impersonation token? */
114 unsigned int session_id; /* token session id */
115 struct acl *default_dacl; /* the default DACL to assign to objects created by this user */
116 int impersonation_level; /* impersonation level this token is capable of if non-primary token */
117 int elevation; /* elevation type */
120 struct privilege
122 struct list entry;
123 struct luid luid;
124 unsigned enabled : 1; /* is the privilege currently enabled? */
125 unsigned def : 1; /* is the privilege enabled by default? */
128 struct group
130 struct list entry;
131 unsigned int attrs;
132 struct sid sid;
135 static void token_dump( struct object *obj, int verbose );
136 static void token_destroy( struct object *obj );
138 static const struct object_ops token_ops =
140 sizeof(struct token), /* size */
141 &token_type, /* type */
142 token_dump, /* dump */
143 no_add_queue, /* add_queue */
144 NULL, /* remove_queue */
145 NULL, /* signaled */
146 NULL, /* satisfied */
147 no_signal, /* signal */
148 no_get_fd, /* get_fd */
149 default_map_access, /* map_access */
150 default_get_sd, /* get_sd */
151 default_set_sd, /* set_sd */
152 no_get_full_name, /* get_full_name */
153 no_lookup_name, /* lookup_name */
154 no_link_name, /* link_name */
155 NULL, /* unlink_name */
156 no_open_file, /* open_file */
157 no_kernel_obj_list, /* get_kernel_obj_list */
158 no_close_handle, /* close_handle */
159 token_destroy /* destroy */
162 static void token_dump( struct object *obj, int verbose )
164 struct token *token = (struct token *)obj;
165 assert( obj->ops == &token_ops );
166 fprintf( stderr, "Token id=%d.%u primary=%u impersonation level=%d\n", token->token_id.high_part,
167 token->token_id.low_part, token->primary, token->impersonation_level );
170 void security_set_thread_token( struct thread *thread, obj_handle_t handle )
172 if (!handle)
174 if (thread->token)
175 release_object( thread->token );
176 thread->token = NULL;
178 else
180 struct token *token = (struct token *)get_handle_obj( current->process,
181 handle,
182 TOKEN_IMPERSONATE,
183 &token_ops );
184 if (token)
186 if (thread->token)
187 release_object( thread->token );
188 thread->token = token;
193 const struct sid *security_unix_uid_to_sid( uid_t uid )
195 /* very simple mapping: either the current user or not the current user */
196 if (uid == getuid())
197 return &local_user_sid;
198 else
199 return &anonymous_logon_sid;
202 static int acl_is_valid( const struct acl *acl, data_size_t size )
204 ULONG i;
205 const struct ace *ace;
207 if (size < sizeof(*acl)) return FALSE;
209 size = min(size, MAX_ACL_LEN);
210 size -= sizeof(*acl);
212 for (i = 0, ace = ace_first( acl ); i < acl->count; i++, ace = ace_next( ace ))
214 if (size < sizeof(*ace) || size < ace->size) return FALSE;
215 size -= ace->size;
216 switch (ace->type)
218 case ACCESS_DENIED_ACE_TYPE:
219 case ACCESS_ALLOWED_ACE_TYPE:
220 case SYSTEM_AUDIT_ACE_TYPE:
221 case SYSTEM_ALARM_ACE_TYPE:
222 case SYSTEM_MANDATORY_LABEL_ACE_TYPE:
223 break;
224 default:
225 return FALSE;
227 if (!sid_valid_size( (const struct sid *)(ace + 1), ace->size - sizeof(*ace) )) return FALSE;
229 return TRUE;
232 static unsigned int get_sid_count( const struct sid *sid, data_size_t size )
234 unsigned int count;
236 for (count = 0; sid_valid_size( sid, size ); count++)
238 size -= sid_len( sid );
239 sid = (const struct sid *)((char *)sid + sid_len( sid ));
241 return count;
244 /* checks whether all members of a security descriptor fit inside the size
245 * of memory specified */
246 int sd_is_valid( const struct security_descriptor *sd, data_size_t size )
248 size_t offset = sizeof(struct security_descriptor);
249 const struct sid *group;
250 const struct sid *owner;
251 const struct acl *sacl;
252 const struct acl *dacl;
253 int dummy;
255 if (size < offset)
256 return FALSE;
258 if (sd->owner_len >= offsetof(struct sid, sub_auth[255]) || offset + sd->owner_len > size) return FALSE;
259 owner = sd_get_owner( sd );
260 offset += sd->owner_len;
261 if (owner && !sid_valid_size( owner, sd->owner_len )) return FALSE;
263 if (sd->group_len >= offsetof(struct sid, sub_auth[255]) || offset + sd->group_len > size) return FALSE;
264 group = sd_get_group( sd );
265 offset += sd->group_len;
266 if (group && !sid_valid_size( group, sd->group_len )) return FALSE;
268 if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
269 return FALSE;
270 sacl = sd_get_sacl( sd, &dummy );
271 if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
272 return FALSE;
273 offset += sd->sacl_len;
275 if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
276 return FALSE;
277 dacl = sd_get_dacl( sd, &dummy );
278 if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
279 return FALSE;
280 offset += sd->dacl_len;
282 return TRUE;
285 /* extract security labels from SACL */
286 struct acl *extract_security_labels( const struct acl *sacl )
288 size_t size = sizeof(*sacl);
289 const struct ace *ace;
290 struct ace *label_ace;
291 unsigned int i, count = 0;
292 struct acl *label_acl;
294 for (i = 0, ace = ace_first( sacl ); i < sacl->count; i++, ace = ace_next( ace ))
296 if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
298 size += ace->size;
299 count++;
303 label_acl = mem_alloc( size );
304 if (!label_acl) return NULL;
306 label_acl->revision = sacl->revision;
307 label_acl->pad1 = 0;
308 label_acl->size = size;
309 label_acl->count = count;
310 label_acl->pad2 = 0;
312 label_ace = ace_first( label_acl );
313 for (i = 0, ace = ace_first( sacl ); i < sacl->count; i++, ace = ace_next( ace ))
315 if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
317 memcpy( label_ace, ace, ace->size );
318 label_ace = ace_next( label_ace );
321 return label_acl;
324 /* replace security labels in an existing SACL */
325 struct acl *replace_security_labels( const struct acl *old_sacl, const struct acl *new_sacl )
327 const struct ace *ace;
328 struct ace *replaced_ace;
329 unsigned int i, count = 0;
330 unsigned char revision = ACL_REVISION;
331 struct acl *replaced_acl;
332 data_size_t size = sizeof(*replaced_acl);
334 if (old_sacl)
336 revision = max( revision, old_sacl->revision );
337 for (i = 0, ace = ace_first( old_sacl ); i < old_sacl->count; i++, ace = ace_next( ace ))
339 if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
340 size += ace->size;
341 count++;
345 if (new_sacl)
347 revision = max( revision, new_sacl->revision );
348 for (i = 0, ace = ace_first( new_sacl ); i < new_sacl->count; i++, ace = ace_next( ace ))
350 if (ace->type != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
351 size += ace->size;
352 count++;
355 if (size > MAX_ACL_LEN)
357 set_error( STATUS_INVALID_ACL );
358 return NULL;
361 replaced_acl = mem_alloc( size );
362 if (!replaced_acl) return NULL;
364 replaced_acl->revision = revision;
365 replaced_acl->pad1 = 0;
366 replaced_acl->size = size;
367 replaced_acl->count = count;
368 replaced_acl->pad2 = 0;
370 replaced_ace = (struct ace *)(replaced_acl + 1);
371 if (old_sacl)
373 for (i = 0, ace = ace_first( old_sacl ); i < old_sacl->count; i++, ace = ace_next( ace ))
375 if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
376 memcpy( replaced_ace, ace, ace->size );
377 replaced_ace = ace_next( replaced_ace );
381 if (new_sacl)
383 for (i = 0, ace = ace_first( new_sacl ); i < new_sacl->count; i++, ace = ace_next( ace ))
385 if (ace->type != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
386 memcpy( replaced_ace, ace, ace->size );
387 replaced_ace = ace_next( replaced_ace );
391 return replaced_acl;
394 static inline int is_equal_luid( struct luid luid1, struct luid luid2 )
396 return (luid1.low_part == luid2.low_part && luid1.high_part == luid2.high_part);
399 static inline void allocate_luid( struct luid *luid )
401 prev_luid_value.low_part++;
402 *luid = prev_luid_value;
405 DECL_HANDLER( allocate_locally_unique_id )
407 allocate_luid( &reply->luid );
410 static inline struct luid_attr luid_and_attr_from_privilege( const struct privilege *in )
412 struct luid_attr ret = { in->luid };
414 ret.attrs = (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
415 (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
416 return ret;
419 static struct privilege *privilege_add( struct token *token, struct luid luid, int enabled )
421 struct privilege *privilege = mem_alloc( sizeof(*privilege) );
422 if (privilege)
424 privilege->luid = luid;
425 privilege->def = privilege->enabled = (enabled != 0);
426 list_add_tail( &token->privileges, &privilege->entry );
428 return privilege;
431 static inline void privilege_remove( struct privilege *privilege )
433 list_remove( &privilege->entry );
434 free( privilege );
437 static void token_destroy( struct object *obj )
439 struct token* token;
440 struct list *cursor, *cursor_next;
442 assert( obj->ops == &token_ops );
443 token = (struct token *)obj;
445 free( token->user );
447 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
449 struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
450 privilege_remove( privilege );
453 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
455 struct group *group = LIST_ENTRY( cursor, struct group, entry );
456 list_remove( &group->entry );
457 free( group );
460 free( token->default_dacl );
463 /* creates a new token.
464 * groups may be NULL if group_count is 0.
465 * privs may be NULL if priv_count is 0.
466 * default_dacl may be NULL, indicating that all objects created by the user
467 * are unsecured.
468 * modified_id may be NULL, indicating that a new modified_id luid should be
469 * allocated.
471 static struct token *create_token( unsigned int primary, unsigned int session_id, const struct sid *user,
472 const struct sid_attrs *groups, unsigned int group_count,
473 const struct luid_attr *privs, unsigned int priv_count,
474 const struct acl *default_dacl, const struct luid *modified_id,
475 unsigned int primary_group, int impersonation_level, int elevation )
477 struct token *token = alloc_object( &token_ops );
478 if (token)
480 unsigned int i;
482 allocate_luid( &token->token_id );
483 if (modified_id)
484 token->modified_id = *modified_id;
485 else
486 allocate_luid( &token->modified_id );
487 list_init( &token->privileges );
488 list_init( &token->groups );
489 token->primary = primary;
490 token->session_id = session_id;
491 /* primary tokens don't have impersonation levels */
492 if (primary)
493 token->impersonation_level = -1;
494 else
495 token->impersonation_level = impersonation_level;
496 token->default_dacl = NULL;
497 token->primary_group = NULL;
498 token->elevation = elevation;
500 /* copy user */
501 token->user = memdup( user, sid_len( user ));
502 if (!token->user)
504 release_object( token );
505 return NULL;
508 /* copy groups */
509 for (i = 0; i < group_count; i++)
511 size_t size = offsetof( struct group, sid.sub_auth[groups[i].sid->sub_count] );
512 struct group *group = mem_alloc( size );
514 if (!group)
516 release_object( token );
517 return NULL;
519 group->attrs = groups[i].attrs;
520 copy_sid( &group->sid, groups[i].sid );
521 list_add_tail( &token->groups, &group->entry );
523 if (primary_group == i)
525 token->owner = &group->sid;
526 token->primary_group = &group->sid;
530 /* copy privileges */
531 for (i = 0; i < priv_count; i++)
533 /* note: we don't check uniqueness: the caller must make sure
534 * privs doesn't contain any duplicate luids */
535 if (!privilege_add( token, privs[i].luid, privs[i].attrs & SE_PRIVILEGE_ENABLED ))
537 release_object( token );
538 return NULL;
542 if (default_dacl)
544 token->default_dacl = memdup( default_dacl, default_dacl->size );
545 if (!token->default_dacl)
547 release_object( token );
548 return NULL;
552 return token;
555 static int filter_group( struct group *group, const struct sid *filter, unsigned int count )
557 unsigned int i;
559 for (i = 0; i < count; i++)
561 if (equal_sid( &group->sid, filter )) return 1;
562 filter = (const struct sid *)((char *)filter + sid_len( filter ));
565 return 0;
568 static int filter_privilege( struct privilege *privilege, const struct luid_attr *filter, unsigned int count )
570 unsigned int i;
572 for (i = 0; i < count; i++)
573 if (is_equal_luid( privilege->luid, filter[i].luid )) return 1;
575 return 0;
578 struct token *token_duplicate( struct token *src_token, unsigned primary,
579 int impersonation_level, const struct security_descriptor *sd,
580 const struct luid_attr *remove_privs, unsigned int remove_priv_count,
581 const struct sid *remove_groups, unsigned int remove_group_count)
583 const struct luid *modified_id =
584 primary || (impersonation_level == src_token->impersonation_level) ?
585 &src_token->modified_id : NULL;
586 struct token *token = NULL;
587 struct privilege *privilege;
588 struct group *group;
590 if (!primary &&
591 (impersonation_level < SecurityAnonymous ||
592 impersonation_level > SecurityDelegation ||
593 (!src_token->primary && (impersonation_level > src_token->impersonation_level))))
595 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
596 return NULL;
599 token = create_token( primary, src_token->session_id, src_token->user, NULL, 0,
600 NULL, 0, src_token->default_dacl, modified_id,
601 0, impersonation_level, src_token->elevation );
602 if (!token) return token;
604 /* copy groups */
605 token->primary_group = NULL;
606 LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct group, entry )
608 size_t size = offsetof( struct group, sid.sub_auth[group->sid.sub_count] );
609 struct group *newgroup = mem_alloc( size );
610 if (!newgroup)
612 release_object( token );
613 return NULL;
615 memcpy( newgroup, group, size );
616 if (filter_group( group, remove_groups, remove_group_count ))
618 newgroup->attrs &= ~(SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT);
619 newgroup->attrs |= SE_GROUP_USE_FOR_DENY_ONLY;
621 list_add_tail( &token->groups, &newgroup->entry );
622 if (src_token->primary_group == &group->sid)
624 token->owner = &newgroup->sid;
625 token->primary_group = &newgroup->sid;
628 assert( token->primary_group );
630 /* copy privileges */
631 LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
633 if (filter_privilege( privilege, remove_privs, remove_priv_count )) continue;
634 if (!privilege_add( token, privilege->luid, privilege->enabled ))
636 release_object( token );
637 return NULL;
641 if (sd) default_set_sd( &token->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
642 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
644 return token;
647 static struct acl *create_default_dacl( const struct sid *user )
649 struct ace *ace;
650 struct acl *default_dacl;
651 size_t default_dacl_size = sizeof(*default_dacl) + 2 * sizeof(*ace) +
652 sid_len( &local_system_sid ) + sid_len( user );
654 default_dacl = mem_alloc( default_dacl_size );
655 if (!default_dacl) return NULL;
657 default_dacl->revision = ACL_REVISION;
658 default_dacl->pad1 = 0;
659 default_dacl->size = default_dacl_size;
660 default_dacl->count = 2;
661 default_dacl->pad2 = 0;
663 /* GENERIC_ALL for Local System */
664 ace = set_ace( ace_first( default_dacl ), &local_system_sid, ACCESS_ALLOWED_ACE_TYPE, 0, GENERIC_ALL );
665 /* GENERIC_ALL for specified user */
666 set_ace( ace_next( ace ), user, ACCESS_ALLOWED_ACE_TYPE, 0, GENERIC_ALL );
668 return default_dacl;
671 struct sid_data
673 SID_IDENTIFIER_AUTHORITY idauth;
674 int count;
675 unsigned int subauth[MAX_SUBAUTH_COUNT];
678 static struct security_descriptor *create_security_label_sd( struct token *token, const struct sid *label_sid )
680 size_t sid_size = sid_len( label_sid ), sacl_size, sd_size;
681 struct security_descriptor *sd;
682 struct acl *sacl;
684 sacl_size = sizeof(*sacl) + sizeof(struct ace) + sid_size;
685 sd_size = sizeof(struct security_descriptor) + sacl_size;
686 if (!(sd = mem_alloc( sd_size )))
687 return NULL;
689 sd->control = SE_SACL_PRESENT;
690 sd->owner_len = 0;
691 sd->group_len = 0;
692 sd->sacl_len = sacl_size;
693 sd->dacl_len = 0;
695 sacl = (struct acl *)(sd + 1);
696 sacl->revision = ACL_REVISION;
697 sacl->pad1 = 0;
698 sacl->size = sacl_size;
699 sacl->count = 1;
700 sacl->pad2 = 0;
702 set_ace( ace_first( sacl ), label_sid, SYSTEM_MANDATORY_LABEL_ACE_TYPE, 0,
703 SYSTEM_MANDATORY_LABEL_NO_WRITE_UP );
704 assert( sd_is_valid( sd, sd_size ) );
705 return sd;
708 int token_assign_label( struct token *token, const struct sid *label )
710 struct security_descriptor *sd;
711 int ret = 0;
713 if ((sd = create_security_label_sd( token, label )))
715 ret = set_sd_defaults_from_token( &token->obj, sd, LABEL_SECURITY_INFORMATION, token );
716 free( sd );
719 return ret;
722 struct token *get_token_obj( struct process *process, obj_handle_t handle, unsigned int access )
724 return (struct token *)get_handle_obj( process, handle, access, &token_ops );
727 struct token *token_create_admin( unsigned primary, int impersonation_level, int elevation, unsigned int session_id )
729 struct token *token = NULL;
730 struct sid alias_admins_sid = { SID_REVISION, 2, SECURITY_NT_AUTHORITY, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS }};
731 struct sid alias_users_sid = { SID_REVISION, 2, SECURITY_NT_AUTHORITY, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS }};
732 /* on Windows, this value changes every time the user logs on */
733 struct sid logon_sid = { SID_REVISION, 3, SECURITY_NT_AUTHORITY, { SECURITY_LOGON_IDS_RID, 0, 0 /* FIXME: should be randomly generated when tokens are inherited by new processes */ }};
734 const struct sid *user_sid = security_unix_uid_to_sid( getuid() );
735 struct acl *default_dacl = create_default_dacl( &domain_users_sid );
736 const struct luid_attr admin_privs[] =
738 { SeChangeNotifyPrivilege, SE_PRIVILEGE_ENABLED },
739 { SeTcbPrivilege, 0 },
740 { SeSecurityPrivilege, 0 },
741 { SeBackupPrivilege, 0 },
742 { SeRestorePrivilege, 0 },
743 { SeSystemtimePrivilege, 0 },
744 { SeShutdownPrivilege, 0 },
745 { SeRemoteShutdownPrivilege, 0 },
746 { SeTakeOwnershipPrivilege, 0 },
747 { SeDebugPrivilege, 0 },
748 { SeSystemEnvironmentPrivilege, 0 },
749 { SeSystemProfilePrivilege, 0 },
750 { SeProfileSingleProcessPrivilege, 0 },
751 { SeIncreaseBasePriorityPrivilege, 0 },
752 { SeLoadDriverPrivilege, SE_PRIVILEGE_ENABLED },
753 { SeCreatePagefilePrivilege, 0 },
754 { SeIncreaseQuotaPrivilege, 0 },
755 { SeUndockPrivilege, 0 },
756 { SeManageVolumePrivilege, 0 },
757 { SeImpersonatePrivilege, SE_PRIVILEGE_ENABLED },
758 { SeCreateGlobalPrivilege, SE_PRIVILEGE_ENABLED },
760 /* note: we don't include non-builtin groups here for the user -
761 * telling us these is the job of a client-side program */
762 const struct sid_attrs admin_groups[] =
764 { &world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
765 { &local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
766 { &interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
767 { &authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
768 { &domain_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER },
769 { &alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER },
770 { &alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
771 { &logon_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_LOGON_ID },
774 token = create_token( primary, session_id, user_sid, admin_groups, ARRAY_SIZE( admin_groups ),
775 admin_privs, ARRAY_SIZE( admin_privs ), default_dacl,
776 NULL, 4 /* domain_users */, impersonation_level, elevation );
777 /* we really need a primary group */
778 assert( token->primary_group );
780 free( default_dacl );
781 return token;
784 static struct privilege *token_find_privilege( struct token *token, struct luid luid, int enabled_only )
786 struct privilege *privilege;
787 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
789 if (is_equal_luid( luid, privilege->luid ))
791 if (enabled_only && !privilege->enabled)
792 return NULL;
793 return privilege;
796 return NULL;
799 static unsigned int token_adjust_privileges( struct token *token, const struct luid_attr *privs,
800 unsigned int count, struct luid_attr *mod_privs,
801 unsigned int mod_privs_count )
803 unsigned int i, modified_count = 0;
805 /* mark as modified */
806 allocate_luid( &token->modified_id );
808 for (i = 0; i < count; i++)
810 struct privilege *privilege = token_find_privilege( token, privs[i].luid, FALSE );
811 if (!privilege)
813 set_error( STATUS_NOT_ALL_ASSIGNED );
814 continue;
817 if (privs[i].attrs & SE_PRIVILEGE_REMOVED) privilege_remove( privilege );
818 else
820 /* save previous state for caller */
821 if (mod_privs_count)
823 *mod_privs++ = luid_and_attr_from_privilege( privilege );
824 mod_privs_count--;
825 modified_count++;
827 privilege->enabled = !!(privs[i].attrs & SE_PRIVILEGE_ENABLED);
830 return modified_count;
833 static void token_disable_privileges( struct token *token )
835 struct privilege *privilege;
837 /* mark as modified */
838 allocate_luid( &token->modified_id );
840 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
841 privilege->enabled = FALSE;
844 int token_check_privileges( struct token *token, int all_required, const struct luid_attr *reqprivs,
845 unsigned int count, struct luid_attr *usedprivs)
847 unsigned int i, enabled_count = 0;
849 for (i = 0; i < count; i++)
851 struct privilege *privilege = token_find_privilege( token, reqprivs[i].luid, TRUE );
853 if (usedprivs)
854 usedprivs[i] = reqprivs[i];
856 if (privilege && privilege->enabled)
858 enabled_count++;
859 if (usedprivs) usedprivs[i].attrs |= SE_PRIVILEGE_USED_FOR_ACCESS;
863 if (all_required)
864 return (enabled_count == count);
865 else
866 return (enabled_count > 0);
869 int token_sid_present( struct token *token, const struct sid *sid, int deny )
871 struct group *group;
873 if (equal_sid( token->user, sid )) return TRUE;
875 LIST_FOR_EACH_ENTRY( group, &token->groups, struct group, entry )
877 if (!(group->attrs & SE_GROUP_ENABLED)) continue;
878 if (!deny && (group->attrs & SE_GROUP_USE_FOR_DENY_ONLY)) continue;
879 if (equal_sid( &group->sid, sid )) return TRUE;
882 return FALSE;
885 /* Checks access to a security descriptor. 'sd' must have been validated by
886 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
887 * the reason. 'status' parameter will indicate if access is granted or denied.
889 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
891 static unsigned int token_access_check( struct token *token,
892 const struct security_descriptor *sd,
893 unsigned int desired_access,
894 struct luid_attr *privs,
895 unsigned int *priv_count,
896 const generic_map_t *mapping,
897 unsigned int *granted_access,
898 unsigned int *status )
900 unsigned int current_access = 0;
901 unsigned int denied_access = 0;
902 ULONG i;
903 const struct acl *dacl;
904 int dacl_present;
905 const struct ace *ace;
906 const struct sid *owner;
908 /* assume no access rights */
909 *granted_access = 0;
911 /* fail if desired_access contains generic rights */
912 if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
914 if (priv_count) *priv_count = 0;
915 return STATUS_GENERIC_NOT_MAPPED;
918 dacl = sd_get_dacl( sd, &dacl_present );
919 owner = sd_get_owner( sd );
920 if (!owner || !sd_get_group( sd ))
922 if (priv_count) *priv_count = 0;
923 return STATUS_INVALID_SECURITY_DESCR;
926 /* 1: Grant desired access if the object is unprotected */
927 if (!dacl_present || !dacl)
929 if (priv_count) *priv_count = 0;
930 if (desired_access & MAXIMUM_ALLOWED)
931 *granted_access = mapping->all;
932 else
933 *granted_access = desired_access;
934 return *status = STATUS_SUCCESS;
937 /* 2: Check if caller wants access to system security part. Note: access
938 * is only granted if specifically asked for */
939 if (desired_access & ACCESS_SYSTEM_SECURITY)
941 const struct luid_attr security_priv = { SeSecurityPrivilege, 0 };
942 struct luid_attr retpriv = security_priv;
943 if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
945 if (priv_count)
947 /* assumes that there will only be one privilege to return */
948 if (*priv_count >= 1)
950 *priv_count = 1;
951 *privs = retpriv;
953 else
955 *priv_count = 1;
956 return STATUS_BUFFER_TOO_SMALL;
959 current_access |= ACCESS_SYSTEM_SECURITY;
960 if (desired_access == current_access)
962 *granted_access = current_access;
963 return *status = STATUS_SUCCESS;
966 else
968 if (priv_count) *priv_count = 0;
969 *status = STATUS_PRIVILEGE_NOT_HELD;
970 return STATUS_SUCCESS;
973 else if (priv_count) *priv_count = 0;
975 /* 3: Check whether the token is the owner */
976 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
977 * checked when a "set owner" call is made, overriding the access rights
978 * determined here. */
979 if (token_sid_present( token, owner, FALSE ))
981 current_access |= (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE);
982 if (desired_access == current_access)
984 *granted_access = current_access;
985 return *status = STATUS_SUCCESS;
989 /* 4: Grant rights according to the DACL */
990 for (i = 0, ace = ace_first( dacl ); i < dacl->count; i++, ace = ace_next( ace ))
992 const struct sid *sid = (const struct sid *)(ace + 1);
994 if (ace->flags & INHERIT_ONLY_ACE) continue;
996 switch (ace->type)
998 case ACCESS_DENIED_ACE_TYPE:
999 if (token_sid_present( token, sid, TRUE ))
1001 unsigned int access = map_access( ace->mask, mapping );
1002 if (desired_access & MAXIMUM_ALLOWED)
1003 denied_access |= access;
1004 else
1006 denied_access |= (access & ~current_access);
1007 if (desired_access & access) goto done;
1010 break;
1011 case ACCESS_ALLOWED_ACE_TYPE:
1012 if (token_sid_present( token, sid, FALSE ))
1014 unsigned int access = map_access( ace->mask, mapping );
1015 if (desired_access & MAXIMUM_ALLOWED)
1016 current_access |= access;
1017 else
1018 current_access |= (access & ~denied_access);
1020 break;
1023 /* don't bother carrying on checking if we've already got all of
1024 * rights we need */
1025 if (desired_access == *granted_access)
1026 break;
1029 done:
1030 if (desired_access & MAXIMUM_ALLOWED)
1031 *granted_access = current_access & ~denied_access;
1032 else
1033 if ((current_access & desired_access) == desired_access)
1034 *granted_access = current_access & desired_access;
1035 else
1036 *granted_access = 0;
1038 *status = *granted_access ? STATUS_SUCCESS : STATUS_ACCESS_DENIED;
1039 return STATUS_SUCCESS;
1042 const struct acl *token_get_default_dacl( struct token *token )
1044 return token->default_dacl;
1047 const struct sid *token_get_owner( struct token *token )
1049 return token->owner;
1052 const struct sid *token_get_primary_group( struct token *token )
1054 return token->primary_group;
1057 unsigned int token_get_session_id( struct token *token )
1059 return token->session_id;
1062 int check_object_access(struct token *token, struct object *obj, unsigned int *access)
1064 generic_map_t mapping;
1065 unsigned int status;
1066 int res;
1068 if (!token)
1069 token = current->token ? current->token : current->process->token;
1071 mapping.all = obj->ops->map_access( obj, GENERIC_ALL );
1073 if (!obj->sd)
1075 if (*access & MAXIMUM_ALLOWED) *access = mapping.all;
1076 return TRUE;
1079 mapping.read = obj->ops->map_access( obj, GENERIC_READ );
1080 mapping.write = obj->ops->map_access( obj, GENERIC_WRITE );
1081 mapping.exec = obj->ops->map_access( obj, GENERIC_EXECUTE );
1083 res = token_access_check( token, obj->sd, *access, NULL, NULL,
1084 &mapping, access, &status ) == STATUS_SUCCESS &&
1085 status == STATUS_SUCCESS;
1087 if (!res) set_error( STATUS_ACCESS_DENIED );
1088 return res;
1092 /* create a security token */
1093 DECL_HANDLER(create_token)
1095 struct token *token;
1096 struct object_attributes *objattr;
1097 struct sid *user;
1098 struct sid_attrs *groups;
1099 struct luid_attr *privs;
1100 struct acl *dacl = NULL;
1101 unsigned int i;
1102 data_size_t data_size, groups_size;
1103 struct acl *default_dacl = NULL;
1104 unsigned int *attrs;
1105 struct sid *sid;
1107 objattr = (struct object_attributes *)get_req_data();
1108 user = (struct sid *)get_req_data_after_objattr( objattr, &data_size );
1110 if (!user || !sid_valid_size( user, data_size ))
1112 set_error( STATUS_INVALID_PARAMETER );
1113 return;
1116 data_size -= sid_len( user );
1117 groups_size = req->group_count * sizeof( attrs[0] );
1119 if (data_size < groups_size)
1121 set_error( STATUS_INVALID_PARAMETER );
1122 return;
1125 if (req->primary_group < 0 || req->primary_group >= req->group_count)
1127 set_error( STATUS_INVALID_PARAMETER );
1128 return;
1131 groups = mem_alloc( req->group_count * sizeof( groups[0] ) );
1132 if (!groups) return;
1134 attrs = (unsigned int *)((char *)user + sid_len( user ));
1135 sid = (struct sid *)&attrs[req->group_count];
1137 for (i = 0; i < req->group_count; i++)
1139 groups[i].attrs = attrs[i];
1140 groups[i].sid = sid;
1142 if (!sid_valid_size( sid, data_size - groups_size ))
1144 free( groups );
1145 set_error( STATUS_INVALID_PARAMETER );
1146 return;
1149 groups_size += sid_len( sid );
1150 sid = (struct sid *)((char *)sid + sid_len( sid ));
1153 data_size -= groups_size;
1155 if (data_size < req->priv_count * sizeof( privs[0] ))
1157 free( groups );
1158 set_error( STATUS_INVALID_PARAMETER );
1159 return;
1162 privs = (struct luid_attr *)((char *)attrs + groups_size);
1163 data_size -= req->priv_count * sizeof( privs[0] );
1165 if (data_size)
1167 dacl = (struct acl *)((char *)privs + req->priv_count * sizeof(privs[0]));
1168 if (!acl_is_valid( dacl, data_size ))
1170 free( groups );
1171 set_error( STATUS_INVALID_PARAMETER );
1172 return;
1175 else
1176 dacl = default_dacl = create_default_dacl( &domain_users_sid );
1178 token = create_token( req->primary, default_session_id, user, groups, req->group_count,
1179 privs, req->priv_count, dacl, NULL, req->primary_group, req->impersonation_level, 0 );
1180 if (token)
1181 reply->token = alloc_handle( current->process, token, req->access, objattr->attributes );
1183 free( default_dacl );
1184 free( groups );
1188 /* open a security token */
1189 DECL_HANDLER(open_token)
1191 if (req->flags & OPEN_TOKEN_THREAD)
1193 struct thread *thread = get_thread_from_handle( req->handle, 0 );
1194 if (thread)
1196 if (thread->token)
1198 if (!thread->token->primary && thread->token->impersonation_level <= SecurityAnonymous)
1199 set_error( STATUS_CANT_OPEN_ANONYMOUS );
1200 else
1201 reply->token = alloc_handle( current->process, thread->token,
1202 req->access, req->attributes );
1204 else
1205 set_error( STATUS_NO_TOKEN );
1206 release_object( thread );
1209 else
1211 struct process *process = get_process_from_handle( req->handle, 0 );
1212 if (process)
1214 if (process->token)
1215 reply->token = alloc_handle( current->process, process->token, req->access,
1216 req->attributes );
1217 else
1218 set_error( STATUS_NO_TOKEN );
1219 release_object( process );
1224 /* adjust the privileges held by a token */
1225 DECL_HANDLER(adjust_token_privileges)
1227 struct token *token;
1228 unsigned int access = TOKEN_ADJUST_PRIVILEGES;
1230 if (req->get_modified_state) access |= TOKEN_QUERY;
1232 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1233 access, &token_ops )))
1235 const struct luid_attr *privs = get_req_data();
1236 struct luid_attr *modified_privs = NULL;
1237 unsigned int priv_count = get_req_data_size() / sizeof(*privs);
1238 unsigned int modified_priv_count = 0;
1240 if (req->get_modified_state && !req->disable_all)
1242 unsigned int i;
1243 /* count modified privs */
1244 for (i = 0; i < priv_count; i++)
1246 struct privilege *privilege = token_find_privilege( token, privs[i].luid, FALSE );
1247 if (privilege && req->get_modified_state)
1248 modified_priv_count++;
1250 reply->len = modified_priv_count;
1251 modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
1252 if (modified_priv_count)
1253 modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
1255 reply->len = modified_priv_count * sizeof(*modified_privs);
1257 if (req->disable_all)
1258 token_disable_privileges( token );
1259 else
1260 token_adjust_privileges( token, privs, priv_count, modified_privs, modified_priv_count );
1262 release_object( token );
1266 /* retrieves the list of privileges that may be held be the token */
1267 DECL_HANDLER(get_token_privileges)
1269 struct token *token;
1271 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1272 TOKEN_QUERY,
1273 &token_ops )))
1275 int priv_count = 0;
1276 struct luid_attr *privs;
1277 struct privilege *privilege;
1279 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
1280 priv_count++;
1282 reply->len = priv_count * sizeof(*privs);
1283 if (reply->len <= get_reply_max_size())
1285 privs = set_reply_data_size( priv_count * sizeof(*privs) );
1286 if (privs)
1287 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
1288 *privs++ = luid_and_attr_from_privilege( privilege );
1290 else
1291 set_error(STATUS_BUFFER_TOO_SMALL);
1293 release_object( token );
1297 /* creates a duplicate of the token */
1298 DECL_HANDLER(duplicate_token)
1300 struct token *src_token;
1301 struct unicode_str name;
1302 const struct security_descriptor *sd;
1303 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1305 if (!objattr) return;
1307 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
1308 TOKEN_DUPLICATE,
1309 &token_ops )))
1311 struct token *token = token_duplicate( src_token, req->primary, req->impersonation_level, sd, NULL, 0, NULL, 0 );
1312 if (token)
1314 unsigned int access = req->access ? req->access : get_handle_access( current->process, req->handle );
1315 reply->new_handle = alloc_handle_no_access_check( current->process, token, access, objattr->attributes );
1316 release_object( token );
1318 release_object( src_token );
1322 /* creates a restricted version of a token */
1323 DECL_HANDLER(filter_token)
1325 struct token *src_token;
1327 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_DUPLICATE, &token_ops )))
1329 const struct luid_attr *filter_privileges = get_req_data();
1330 unsigned int priv_count, group_count;
1331 const struct sid *filter_groups;
1332 struct token *token;
1334 priv_count = min( req->privileges_size, get_req_data_size() ) / sizeof(struct luid_attr);
1335 filter_groups = (const struct sid *)((char *)filter_privileges + priv_count * sizeof(struct luid_attr));
1336 group_count = get_sid_count( filter_groups, get_req_data_size() - priv_count * sizeof(struct luid_attr) );
1338 token = token_duplicate( src_token, src_token->primary, src_token->impersonation_level, NULL,
1339 filter_privileges, priv_count, filter_groups, group_count );
1340 if (token)
1342 unsigned int access = get_handle_access( current->process, req->handle );
1343 reply->new_handle = alloc_handle_no_access_check( current->process, token, access, 0 );
1344 release_object( token );
1346 release_object( src_token );
1350 /* checks the specified privileges are held by the token */
1351 DECL_HANDLER(check_token_privileges)
1353 struct token *token;
1355 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1356 TOKEN_QUERY,
1357 &token_ops )))
1359 unsigned int count = get_req_data_size() / sizeof(struct luid_attr);
1361 if (!token->primary && token->impersonation_level <= SecurityAnonymous)
1362 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
1363 else if (get_reply_max_size() >= count * sizeof(struct luid_attr))
1365 struct luid_attr *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
1366 reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
1368 else
1369 set_error( STATUS_BUFFER_OVERFLOW );
1370 release_object( token );
1374 /* checks that a user represented by a token is allowed to access an object
1375 * represented by a security descriptor */
1376 DECL_HANDLER(access_check)
1378 data_size_t sd_size = get_req_data_size();
1379 const struct security_descriptor *sd = get_req_data();
1380 struct token *token;
1382 if (!sd_is_valid( sd, sd_size ))
1384 set_error( STATUS_ACCESS_VIOLATION );
1385 return;
1388 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1389 TOKEN_QUERY,
1390 &token_ops )))
1392 unsigned int status;
1393 struct luid_attr priv;
1394 unsigned int priv_count = 1;
1396 memset(&priv, 0, sizeof(priv));
1398 /* only impersonation tokens may be used with this function */
1399 if (token->primary)
1401 set_error( STATUS_NO_IMPERSONATION_TOKEN );
1402 release_object( token );
1403 return;
1405 /* anonymous impersonation tokens can't be used */
1406 if (token->impersonation_level <= SecurityAnonymous)
1408 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
1409 release_object( token );
1410 return;
1413 status = token_access_check( token, sd, req->desired_access, &priv, &priv_count, &req->mapping,
1414 &reply->access_granted, &reply->access_status );
1416 reply->privileges_len = priv_count*sizeof(struct luid_attr);
1418 if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
1420 struct luid_attr *privs = set_reply_data_size( priv_count * sizeof(*privs) );
1421 memcpy( privs, &priv, sizeof(priv) );
1424 set_error( status );
1425 release_object( token );
1429 /* retrieves an SID from the token */
1430 DECL_HANDLER(get_token_sid)
1432 struct token *token;
1434 reply->sid_len = 0;
1436 if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
1438 const struct sid *sid = NULL;
1440 switch (req->which_sid)
1442 case TokenUser:
1443 assert(token->user);
1444 sid = token->user;
1445 break;
1446 case TokenPrimaryGroup:
1447 sid = token->primary_group;
1448 break;
1449 case TokenOwner:
1450 sid = token->owner;
1451 break;
1452 default:
1453 set_error( STATUS_INVALID_PARAMETER );
1454 break;
1457 if (sid)
1459 reply->sid_len = sid_len( sid );
1460 if (reply->sid_len <= get_reply_max_size()) set_reply_data( sid, reply->sid_len );
1461 else set_error( STATUS_BUFFER_TOO_SMALL );
1463 release_object( token );
1467 /* retrieves the groups that the user represented by the token belongs to */
1468 DECL_HANDLER(get_token_groups)
1470 struct token *token;
1472 if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
1474 unsigned int group_count = 0;
1475 const struct group *group;
1477 LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
1479 if (req->attr_mask && !(group->attrs & req->attr_mask)) continue;
1480 group_count++;
1481 reply->sid_len += sid_len( &group->sid );
1483 reply->attr_len = sizeof(unsigned int) * group_count;
1485 if (reply->attr_len + reply->sid_len <= get_reply_max_size())
1487 unsigned int *attr_ptr = set_reply_data_size( reply->attr_len + reply->sid_len );
1488 struct sid *sid = (struct sid *)(attr_ptr + group_count);
1490 if (attr_ptr)
1492 LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
1494 if (req->attr_mask && !(group->attrs & req->attr_mask)) continue;
1495 sid = copy_sid( sid, &group->sid );
1496 *attr_ptr++ = group->attrs;
1500 else set_error( STATUS_BUFFER_TOO_SMALL );
1502 release_object( token );
1506 DECL_HANDLER(get_token_info)
1508 struct token *token;
1510 if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
1512 reply->token_id = token->token_id;
1513 reply->modified_id = token->modified_id;
1514 reply->session_id = token->session_id;
1515 reply->primary = token->primary;
1516 reply->impersonation_level = token->impersonation_level;
1517 reply->elevation = token->elevation;
1518 reply->group_count = list_count( &token->groups );
1519 reply->privilege_count = list_count( &token->privileges );
1520 release_object( token );
1524 DECL_HANDLER(get_token_default_dacl)
1526 struct token *token;
1528 reply->acl_len = 0;
1530 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1531 TOKEN_QUERY,
1532 &token_ops )))
1534 if (token->default_dacl)
1535 reply->acl_len = token->default_dacl->size;
1537 if (reply->acl_len <= get_reply_max_size())
1539 struct acl *acl_reply = set_reply_data_size( reply->acl_len );
1540 if (acl_reply)
1541 memcpy( acl_reply, token->default_dacl, reply->acl_len );
1543 else set_error( STATUS_BUFFER_TOO_SMALL );
1545 release_object( token );
1549 DECL_HANDLER(set_token_default_dacl)
1551 struct token *token;
1552 const struct acl *acl = get_req_data();
1553 unsigned int acl_size = get_req_data_size();
1555 if (acl_size && !acl_is_valid( acl, acl_size ))
1557 set_error( STATUS_INVALID_ACL );
1558 return;
1561 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1562 TOKEN_ADJUST_DEFAULT,
1563 &token_ops )))
1565 free( token->default_dacl );
1566 token->default_dacl = NULL;
1568 if (acl_size)
1569 token->default_dacl = memdup( acl, acl_size );
1571 release_object( token );
1575 DECL_HANDLER(create_linked_token)
1577 struct token *token, *linked;
1578 int elevation;
1580 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1581 TOKEN_QUERY, &token_ops )))
1583 switch (token->elevation)
1585 case TokenElevationTypeFull:
1586 elevation = TokenElevationTypeLimited;
1587 break;
1588 case TokenElevationTypeLimited:
1589 elevation = TokenElevationTypeFull;
1590 break;
1591 default:
1592 release_object( token );
1593 return;
1595 if ((linked = token_create_admin( FALSE, SecurityIdentification, elevation, token->session_id )))
1597 reply->linked = alloc_handle( current->process, linked, TOKEN_ALL_ACCESS, 0 );
1598 release_object( linked );
1600 release_object( token );