Added Norwegian translation.
[wine/multimedia.git] / server / token.c
blobe42e891d8447c23f16138dabce115bb8aee6251f
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "windef.h"
31 #include "handle.h"
32 #include "thread.h"
33 #include "process.h"
34 #include "request.h"
35 #include "security.h"
37 #include "wine/unicode.h"
39 #define MAX_SUBAUTH_COUNT 1
41 const LUID SeIncreaseQuotaPrivilege = { 5, 0 };
42 const LUID SeSecurityPrivilege = { 8, 0 };
43 const LUID SeTakeOwnershipPrivilege = { 9, 0 };
44 const LUID SeLoadDriverPrivilege = { 10, 0 };
45 const LUID SeSystemProfilePrivilege = { 11, 0 };
46 const LUID SeSystemtimePrivilege = { 12, 0 };
47 const LUID SeProfileSingleProcessPrivilege = { 13, 0 };
48 const LUID SeIncreaseBasePriorityPrivilege = { 14, 0 };
49 const LUID SeCreatePagefilePrivilege = { 15, 0 };
50 const LUID SeBackupPrivilege = { 17, 0 };
51 const LUID SeRestorePrivilege = { 18, 0 };
52 const LUID SeShutdownPrivilege = { 19, 0 };
53 const LUID SeDebugPrivilege = { 20, 0 };
54 const LUID SeSystemEnvironmentPrivilege = { 22, 0 };
55 const LUID SeChangeNotifyPrivilege = { 23, 0 };
56 const LUID SeRemoteShutdownPrivilege = { 24, 0 };
57 const LUID SeUndockPrivilege = { 25, 0 };
58 const LUID SeManageVolumePrivilege = { 28, 0 };
59 const LUID SeImpersonatePrivilege = { 29, 0 };
60 const LUID SeCreateGlobalPrivilege = { 30, 0 };
62 static const SID world_sid = { SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY }, { SECURITY_WORLD_RID } };
63 static const SID local_sid = { SID_REVISION, 1, { SECURITY_LOCAL_SID_AUTHORITY }, { SECURITY_LOCAL_RID } };
64 static const SID interactive_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_INTERACTIVE_RID } };
65 static const SID authenticated_user_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_AUTHENTICATED_USER_RID } };
66 static const SID local_system_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SYSTEM_RID } };
67 static const PSID security_world_sid = (PSID)&world_sid;
68 static const PSID security_local_sid = (PSID)&local_sid;
69 const PSID security_interactive_sid = (PSID)&interactive_sid;
70 static const PSID security_authenticated_user_sid = (PSID)&authenticated_user_sid;
71 static const PSID security_local_system_sid = (PSID)&local_system_sid;
73 struct token
75 struct object obj; /* object header */
76 struct list privileges; /* privileges available to the token */
77 struct list groups; /* groups that the user of this token belongs to (sid_and_attributes) */
78 SID *user; /* SID of user this token represents */
79 unsigned primary; /* is this a primary or impersonation token? */
80 ACL *default_dacl; /* the default DACL to assign to objects created by this user */
83 struct privilege
85 struct list entry;
86 LUID luid;
87 unsigned enabled : 1; /* is the privilege currently enabled? */
88 unsigned def : 1; /* is the privilege enabled by default? */
91 struct sid_and_attributes
93 struct list entry;
94 unsigned enabled : 1; /* is the sid currently enabled? */
95 unsigned def : 1; /* is the sid enabled by default? */
96 unsigned logon : 1; /* is this a logon sid? */
97 unsigned mandatory: 1; /* is this sid always enabled? */
98 unsigned owner : 1; /* can this sid be an owner of an object? */
99 unsigned resource : 1; /* is this a domain-local group? */
100 unsigned deny_only: 1; /* is this a sid that should be use for denying only? */
101 SID sid;
104 static void token_dump( struct object *obj, int verbose );
105 static void token_destroy( struct object *obj );
107 static const struct object_ops token_ops =
109 sizeof(struct token), /* size */
110 token_dump, /* dump */
111 no_add_queue, /* add_queue */
112 NULL, /* remove_queue */
113 NULL, /* signaled */
114 NULL, /* satisfied */
115 no_signal, /* signal */
116 no_get_fd, /* get_fd */
117 no_close_handle, /* close_handle */
118 token_destroy /* destroy */
122 static void token_dump( struct object *obj, int verbose )
124 fprintf( stderr, "Security token\n" );
125 /* FIXME: dump token members */
128 static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] )
130 int i;
131 SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) );
132 if (!sid) return NULL;
133 sid->Revision = SID_REVISION;
134 sid->SubAuthorityCount = subauthcount;
135 sid->IdentifierAuthority = *idauthority;
136 for (i = 0; i < subauthcount; i++)
137 sid->SubAuthority[i] = subauth[i];
138 return sid;
141 static inline int security_equal_sid( const SID *sid1, const SID *sid2 )
143 return ((sid1->SubAuthorityCount == sid2->SubAuthorityCount) &&
144 !memcmp( sid1, sid2, FIELD_OFFSET(SID, SubAuthority[sid1->SubAuthorityCount]) ));
147 void security_set_thread_token( struct thread *thread, obj_handle_t handle )
149 if (!handle)
151 if (thread->token)
152 release_object( thread->token );
153 thread->token = NULL;
155 else
157 struct token *token = (struct token *)get_handle_obj( current->process,
158 handle,
159 TOKEN_IMPERSONATE,
160 &token_ops );
161 if (token)
163 if (thread->token)
164 release_object( thread->token );
165 thread->token = token;
170 static const ACE_HEADER *ace_next( const ACE_HEADER *ace )
172 return (const ACE_HEADER *)((const char *)ace + ace->AceSize);
175 static int acl_is_valid( const ACL *acl, size_t size )
177 ULONG i;
178 const ACE_HEADER *ace;
180 if (size < sizeof(ACL))
181 return FALSE;
183 size = min(size, MAX_ACL_LEN);
185 size -= sizeof(ACL);
187 ace = (const ACE_HEADER *)(acl + 1);
188 for (i = 0; i < acl->AceCount; i++)
190 const SID *sid;
191 size_t sid_size;
193 if (size < sizeof(ACE_HEADER))
194 return FALSE;
195 if (size < ace->AceSize)
196 return FALSE;
197 size -= ace->AceSize;
198 switch (ace->AceType)
200 case ACCESS_DENIED_ACE_TYPE:
201 sid = (const SID *)&((const ACCESS_DENIED_ACE *)ace)->SidStart;
202 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart);
203 break;
204 case ACCESS_ALLOWED_ACE_TYPE:
205 sid = (const SID *)&((const ACCESS_ALLOWED_ACE *)ace)->SidStart;
206 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart);
207 break;
208 case SYSTEM_AUDIT_ACE_TYPE:
209 sid = (const SID *)&((const SYSTEM_AUDIT_ACE *)ace)->SidStart;
210 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart);
211 break;
212 case SYSTEM_ALARM_ACE_TYPE:
213 sid = (const SID *)&((const SYSTEM_ALARM_ACE *)ace)->SidStart;
214 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_ALARM_ACE, SidStart);
215 break;
216 default:
217 return FALSE;
219 if (sid_size < FIELD_OFFSET(SID, SubAuthority[0]) ||
220 sid_size < FIELD_OFFSET(SID, SubAuthority[sid->SubAuthorityCount]))
221 return FALSE;
222 ace = ace_next( ace );
224 return TRUE;
227 /* gets the discretionary access control list from a security descriptor */
228 static inline const ACL *sd_get_dacl( const struct security_descriptor *sd, int *present )
230 *present = (sd->control & SE_DACL_PRESENT ? TRUE : FALSE);
232 if (sd->dacl_len)
233 return (const ACL *)((const char *)(sd + 1) +
234 sd->owner_len + sd->group_len + sd->sacl_len);
235 else
236 return NULL;
239 /* gets the system access control list from a security descriptor */
240 static inline const ACL *sd_get_sacl( const struct security_descriptor *sd, int *present )
242 *present = (sd->control & SE_SACL_PRESENT ? TRUE : FALSE);
244 if (sd->sacl_len)
245 return (const ACL *)((const char *)(sd + 1) +
246 sd->owner_len + sd->group_len);
247 else
248 return NULL;
251 /* gets the owner from a security descriptor */
252 static inline const SID *sd_get_owner( const struct security_descriptor *sd )
254 if (sd->owner_len)
255 return (const SID *)(sd + 1);
256 else
257 return NULL;
260 /* gets the primary group from a security descriptor */
261 static inline const SID *sd_get_group( const struct security_descriptor *sd )
263 if (sd->group_len)
264 return (const SID *)((const char *)(sd + 1) + sd->owner_len);
265 else
266 return NULL;
269 /* checks whether all members of a security descriptor fit inside the size
270 * of memory specified */
271 static int sd_is_valid( const struct security_descriptor *sd, size_t size )
273 size_t offset = sizeof(struct security_descriptor);
274 const SID *group;
275 const SID *owner;
276 const ACL *sacl;
277 const ACL *dacl;
278 int dummy;
280 if (size < offset)
281 return FALSE;
283 if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
284 (offset + sd->owner_len > size))
285 return FALSE;
286 owner = sd_get_owner( sd );
287 if (owner)
289 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]);
290 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
291 return FALSE;
293 offset += sd->owner_len;
295 if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
296 (offset + sd->group_len > size))
297 return FALSE;
298 group = sd_get_group( sd );
299 if (group)
301 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
302 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
303 return FALSE;
305 offset += sd->group_len;
307 if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
308 return FALSE;
309 sacl = sd_get_sacl( sd, &dummy );
310 if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
311 return FALSE;
312 offset += sd->sacl_len;
314 if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
315 return FALSE;
316 dacl = sd_get_dacl( sd, &dummy );
317 if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
318 return FALSE;
319 offset += sd->dacl_len;
321 return TRUE;
324 /* maps from generic rights to specific rights as given by a mapping */
325 static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping)
327 if (*mask & GENERIC_READ) *mask |= mapping->GenericRead;
328 if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite;
329 if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute;
330 if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll;
331 *mask &= 0x0FFFFFFF;
334 static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 )
336 return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart);
339 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in)
341 out->Luid = in->luid;
342 out->Attributes =
343 (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
344 (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
347 static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled )
349 struct privilege *privilege = mem_alloc( sizeof(*privilege) );
350 if (privilege)
352 privilege->luid = *luid;
353 privilege->def = privilege->enabled = (enabled != 0);
354 list_add_tail( &token->privileges, &privilege->entry );
356 return privilege;
359 static inline void privilege_remove( struct privilege *privilege )
361 list_remove( &privilege->entry );
362 free( privilege );
365 static void token_destroy( struct object *obj )
367 struct token* token;
368 struct list *cursor, *cursor_next;
370 assert( obj->ops == &token_ops );
371 token = (struct token *)obj;
373 free( token->user );
375 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
377 struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
378 privilege_remove( privilege );
381 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
383 struct sid_and_attributes *group = LIST_ENTRY( cursor, struct sid_and_attributes, entry );
384 list_remove( &group->entry );
385 free( group );
388 free( token->default_dacl );
391 /* creates a new token.
392 * groups may be NULL if group_count is 0.
393 * privs may be NULL if priv_count is 0.
394 * default_dacl may be NULL, indicating that all objects created by the user
395 * are unsecured.
397 static struct token *create_token( unsigned primary, const SID *user,
398 const SID_AND_ATTRIBUTES *groups, unsigned int group_count,
399 const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count,
400 const ACL *default_dacl )
402 struct token *token = alloc_object( &token_ops );
403 if (token)
405 int i;
407 list_init( &token->privileges );
408 list_init( &token->groups );
409 token->primary = primary;
411 /* copy user */
412 token->user = memdup( user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
413 if (!token->user)
415 release_object( token );
416 return NULL;
419 /* copy groups */
420 for (i = 0; i < group_count; i++)
422 size_t size = FIELD_OFFSET( struct sid_and_attributes, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] );
423 struct sid_and_attributes *group = mem_alloc( size );
424 memcpy( &group->sid, groups[i].Sid, FIELD_OFFSET( SID, SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] ) );
425 group->enabled = TRUE;
426 group->def = TRUE;
427 group->logon = FALSE;
428 group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) ? TRUE : FALSE;
429 group->owner = FALSE;
430 group->resource = FALSE;
431 group->deny_only = FALSE;
432 list_add_tail( &token->groups, &group->entry );
435 /* copy privileges */
436 for (i = 0; i < priv_count; i++)
438 /* note: we don't check uniqueness: the caller must make sure
439 * privs doesn't contain any duplicate luids */
440 if (!privilege_add( token, &privs[i].Luid,
441 privs[i].Attributes & SE_PRIVILEGE_ENABLED ))
443 release_object( token );
444 return NULL;
448 if (default_dacl)
450 token->default_dacl = memdup( default_dacl, default_dacl->AclSize );
451 if (!token->default_dacl)
453 release_object( token );
454 return NULL;
457 else
458 token->default_dacl = NULL;
460 return token;
463 static ACL *create_default_dacl( const SID *user )
465 ACCESS_ALLOWED_ACE *aaa;
466 ACL *default_dacl;
467 SID *sid;
468 size_t default_dacl_size = sizeof(ACL) +
469 2*(sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
470 sizeof(local_system_sid) +
471 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
473 default_dacl = mem_alloc( default_dacl_size );
474 if (!default_dacl) return NULL;
476 default_dacl->AclRevision = MAX_ACL_REVISION;
477 default_dacl->Sbz1 = 0;
478 default_dacl->AclSize = default_dacl_size;
479 default_dacl->AceCount = 2;
480 default_dacl->Sbz2 = 0;
482 /* GENERIC_ALL for Local System */
483 aaa = (ACCESS_ALLOWED_ACE *)(default_dacl + 1);
484 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
485 aaa->Header.AceFlags = 0;
486 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
487 sizeof(local_system_sid);
488 aaa->Mask = GENERIC_ALL;
489 sid = (SID *)&aaa->SidStart;
490 memcpy( sid, &local_system_sid, sizeof(local_system_sid) );
492 /* GENERIC_ALL for specified user */
493 aaa = (ACCESS_ALLOWED_ACE *)((const char *)aaa + aaa->Header.AceSize);
494 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
495 aaa->Header.AceFlags = 0;
496 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
497 FIELD_OFFSET( SID, SubAuthority[user->SubAuthorityCount] );
498 aaa->Mask = GENERIC_ALL;
499 sid = (SID *)&aaa->SidStart;
500 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
502 return default_dacl;
505 struct sid_data
507 SID_IDENTIFIER_AUTHORITY idauth;
508 int count;
509 unsigned int subauth[MAX_SUBAUTH_COUNT];
512 struct token *token_create_admin( void )
514 struct token *token = NULL;
515 static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
516 static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
517 static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
518 PSID alias_admins_sid;
519 PSID alias_users_sid;
520 ACL *default_dacl = create_default_dacl( &local_system_sid );
522 alias_admins_sid = security_sid_alloc( &nt_authority, sizeof(alias_admins_subauth)/sizeof(alias_admins_subauth[0]),
523 alias_admins_subauth );
524 alias_users_sid = security_sid_alloc( &nt_authority, sizeof(alias_users_subauth)/sizeof(alias_users_subauth[0]),
525 alias_users_subauth );
527 if (alias_admins_sid && alias_users_sid && default_dacl)
529 const LUID_AND_ATTRIBUTES admin_privs[] =
531 { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
532 { SeSecurityPrivilege , 0 },
533 { SeBackupPrivilege , 0 },
534 { SeRestorePrivilege , 0 },
535 { SeSystemtimePrivilege , 0 },
536 { SeShutdownPrivilege , 0 },
537 { SeRemoteShutdownPrivilege , 0 },
538 { SeTakeOwnershipPrivilege , 0 },
539 { SeDebugPrivilege , 0 },
540 { SeSystemEnvironmentPrivilege , 0 },
541 { SeSystemProfilePrivilege , 0 },
542 { SeProfileSingleProcessPrivilege, 0 },
543 { SeIncreaseBasePriorityPrivilege, 0 },
544 { SeLoadDriverPrivilege , 0 },
545 { SeCreatePagefilePrivilege , 0 },
546 { SeIncreaseQuotaPrivilege , 0 },
547 { SeUndockPrivilege , 0 },
548 { SeManageVolumePrivilege , 0 },
549 { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED },
550 { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED },
552 /* note: we don't include non-builtin groups here for the user -
553 * telling us these is the job of a client-side program */
554 const SID_AND_ATTRIBUTES admin_groups[] =
556 { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
557 { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
558 { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
559 { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
560 { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
561 { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
563 /* note: we just set the user sid to be the interactive builtin sid -
564 * we should really translate the UNIX user id to a sid */
565 token = create_token( TRUE, &interactive_sid,
566 admin_groups, sizeof(admin_groups)/sizeof(admin_groups[0]),
567 admin_privs, sizeof(admin_privs)/sizeof(admin_privs[0]),
568 default_dacl );
571 if (alias_admins_sid)
572 free( alias_admins_sid );
573 if (alias_users_sid)
574 free( alias_users_sid );
575 if (default_dacl)
576 free( default_dacl );
578 return token;
581 static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only )
583 struct privilege *privilege;
584 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
586 if (is_equal_luid( luid, &privilege->luid ))
588 if (enabled_only && !privilege->enabled)
589 return NULL;
590 return privilege;
593 return NULL;
596 static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs,
597 unsigned int count, LUID_AND_ATTRIBUTES *mod_privs,
598 unsigned int mod_privs_count )
600 int i;
601 unsigned int modified_count = 0;
603 for (i = 0; i < count; i++)
605 struct privilege *privilege =
606 token_find_privilege( token, &privs[i].Luid, FALSE );
607 if (!privilege)
609 set_error( STATUS_NOT_ALL_ASSIGNED );
610 continue;
613 if (privs[i].Attributes & SE_PRIVILEGE_REMOVE)
614 privilege_remove( privilege );
615 else
617 /* save previous state for caller */
618 if (mod_privs_count)
620 luid_and_attr_from_privilege(mod_privs, privilege);
621 mod_privs++;
622 mod_privs_count--;
623 modified_count++;
626 if (privs[i].Attributes & SE_PRIVILEGE_ENABLED)
627 privilege->enabled = TRUE;
628 else
629 privilege->enabled = FALSE;
632 return modified_count;
635 static void token_disable_privileges( struct token *token )
637 struct privilege *privilege;
638 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
639 privilege->enabled = FALSE;
642 int token_check_privileges( struct token *token, int all_required,
643 const LUID_AND_ATTRIBUTES *reqprivs,
644 unsigned int count, LUID_AND_ATTRIBUTES *usedprivs)
646 int i;
647 unsigned int enabled_count = 0;
649 for (i = 0; i < count; i++)
651 struct privilege *privilege =
652 token_find_privilege( token, &reqprivs[i].Luid, TRUE );
654 if (usedprivs)
655 usedprivs[i] = reqprivs[i];
657 if (privilege && privilege->enabled)
659 enabled_count++;
660 if (usedprivs)
661 usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS;
665 if (all_required)
666 return (enabled_count == count);
667 else
668 return (enabled_count > 0);
671 static int token_sid_present( struct token *token, const SID *sid, int deny )
673 struct sid_and_attributes *group;
675 if (security_equal_sid( token->user, sid )) return TRUE;
677 LIST_FOR_EACH_ENTRY( group, &token->groups, struct sid_and_attributes, entry )
679 if (!group->enabled) continue;
680 if (group->deny_only && !deny) continue;
682 if (security_equal_sid( &group->sid, sid )) return TRUE;
685 return FALSE;
688 /* checks access to a security descriptor. sd must have been validated by caller.
689 * it returns STATUS_SUCCESS if access was granted to the object, or an error
690 * status code if not, giving the reason. errors not relating to giving access
691 * to the object are returned in the status parameter. granted_access and
692 * status always have a valid value stored in them on return. */
693 static unsigned int token_access_check( struct token *token,
694 const struct security_descriptor *sd,
695 unsigned int desired_access,
696 LUID_AND_ATTRIBUTES *privs,
697 unsigned int *priv_count,
698 const GENERIC_MAPPING *mapping,
699 unsigned int *granted_access,
700 unsigned int *status )
702 unsigned int current_access = 0;
703 unsigned int denied_access = 0;
704 ULONG i;
705 const ACL *dacl;
706 int dacl_present;
707 const ACE_HEADER *ace;
708 const SID *owner;
710 /* assume success, but no access rights */
711 *status = STATUS_SUCCESS;
712 *granted_access = 0;
714 /* fail if desired_access contains generic rights */
715 if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
717 *priv_count = 0;
718 *status = STATUS_GENERIC_NOT_MAPPED;
719 return STATUS_ACCESS_DENIED;
722 dacl = sd_get_dacl( sd, &dacl_present );
723 owner = sd_get_owner( sd );
724 if (!owner || !sd_get_group( sd ))
726 *priv_count = 0;
727 *status = STATUS_INVALID_SECURITY_DESCR;
728 return STATUS_ACCESS_DENIED;
731 /* 1: Grant desired access if the object is unprotected */
732 if (!dacl_present)
734 *priv_count = 0;
735 *granted_access = desired_access;
736 return STATUS_SUCCESS;
738 if (!dacl)
740 *priv_count = 0;
741 return STATUS_ACCESS_DENIED;
744 /* 2: Check if caller wants access to system security part. Note: access
745 * is only granted if specifically asked for */
746 if (desired_access & ACCESS_SYSTEM_SECURITY)
748 const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 };
749 LUID_AND_ATTRIBUTES retpriv = security_priv;
750 if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
752 if (priv_count)
754 /* assumes that there will only be one privilege to return */
755 if (*priv_count >= 1)
757 *priv_count = 1;
758 *privs = retpriv;
760 else
762 *priv_count = 1;
763 return STATUS_BUFFER_TOO_SMALL;
766 current_access |= ACCESS_SYSTEM_SECURITY;
767 if (desired_access == current_access)
769 *granted_access = current_access;
770 return STATUS_SUCCESS;
773 else
775 *priv_count = 0;
776 return STATUS_PRIVILEGE_NOT_HELD;
779 else if (priv_count) *priv_count = 0;
781 /* 3: Check whether the token is the owner */
782 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
783 * checked when a "set owner" call is made, overriding the access rights
784 * determined here. */
785 if (token_sid_present( token, owner, FALSE ))
787 current_access |= (READ_CONTROL | WRITE_DAC);
788 if (desired_access == current_access)
790 *granted_access = current_access;
791 return STATUS_SUCCESS;
795 /* 4: Grant rights according to the DACL */
796 ace = (const ACE_HEADER *)(dacl + 1);
797 for (i = 0; i < dacl->AceCount; i++)
799 const ACCESS_ALLOWED_ACE *aa_ace;
800 const ACCESS_DENIED_ACE *ad_ace;
801 const SID *sid;
802 switch (ace->AceType)
804 case ACCESS_DENIED_ACE_TYPE:
805 ad_ace = (const ACCESS_DENIED_ACE *)ace;
806 sid = (const SID *)&ad_ace->SidStart;
807 if (token_sid_present( token, sid, TRUE ))
809 unsigned int access = ad_ace->Mask;
810 map_generic_mask(&access, mapping);
811 if (desired_access & MAXIMUM_ALLOWED)
812 denied_access |= access;
813 else
815 denied_access |= (access & ~current_access);
816 if (desired_access & access)
818 *granted_access = 0;
819 return STATUS_SUCCESS;
823 break;
824 case ACCESS_ALLOWED_ACE_TYPE:
825 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
826 sid = (const SID *)&aa_ace->SidStart;
827 if (token_sid_present( token, sid, FALSE ))
829 unsigned int access = aa_ace->Mask;
830 map_generic_mask(&access, mapping);
831 if (desired_access & MAXIMUM_ALLOWED)
832 current_access |= access;
833 else
834 current_access |= (access & ~denied_access);
836 break;
839 /* don't bother carrying on checking if we've already got all of
840 * rights we need */
841 if (desired_access == *granted_access)
842 break;
844 ace = ace_next( ace );
847 if (desired_access & MAXIMUM_ALLOWED)
849 *granted_access = current_access & ~denied_access;
850 if (*granted_access)
851 return STATUS_SUCCESS;
852 else
853 return STATUS_ACCESS_DENIED;
855 else
857 if ((current_access & desired_access) == desired_access)
859 *granted_access = current_access & desired_access;
860 return STATUS_SUCCESS;
862 else
863 return STATUS_ACCESS_DENIED;
867 const ACL *token_get_default_dacl( struct token *token )
869 return token->default_dacl;
872 /* open a security token */
873 DECL_HANDLER(open_token)
875 if (req->flags & OPEN_TOKEN_THREAD)
877 struct thread *thread = get_thread_from_handle( req->handle, 0 );
878 if (thread)
880 if (thread->token)
881 reply->token = alloc_handle( current->process, thread->token, TOKEN_ALL_ACCESS, 0);
882 else
883 set_error(STATUS_NO_TOKEN);
884 release_object( thread );
887 else
889 struct process *process = get_process_from_handle( req->handle, 0 );
890 if (process)
892 if (process->token)
893 reply->token = alloc_handle( current->process, process->token, TOKEN_ALL_ACCESS, 0);
894 else
895 set_error(STATUS_NO_TOKEN);
896 release_object( process );
901 /* adjust the privileges held by a token */
902 DECL_HANDLER(adjust_token_privileges)
904 struct token *token;
905 unsigned int access = TOKEN_ADJUST_PRIVILEGES;
907 if (req->get_modified_state) access |= TOKEN_QUERY;
909 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
910 access, &token_ops )))
912 const LUID_AND_ATTRIBUTES *privs = get_req_data();
913 LUID_AND_ATTRIBUTES *modified_privs = NULL;
914 unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
915 unsigned int modified_priv_count = 0;
917 if (req->get_modified_state && !req->disable_all)
919 int i;
920 /* count modified privs */
921 for (i = 0; i < priv_count; i++)
923 struct privilege *privilege =
924 token_find_privilege( token, &privs[i].Luid, FALSE );
925 if (privilege && req->get_modified_state)
926 modified_priv_count++;
928 reply->len = modified_priv_count;
929 modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
930 if (modified_priv_count)
931 modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
933 reply->len = modified_priv_count * sizeof(*modified_privs);
935 if (req->disable_all)
936 token_disable_privileges( token );
937 else
938 modified_priv_count = token_adjust_privileges( token, privs,
939 priv_count, modified_privs, modified_priv_count );
941 release_object( token );
945 /* retrieves the list of privileges that may be held be the token */
946 DECL_HANDLER(get_token_privileges)
948 struct token *token;
950 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
951 TOKEN_QUERY,
952 &token_ops )))
954 int priv_count = 0;
955 LUID_AND_ATTRIBUTES *privs;
956 struct privilege *privilege;
958 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
959 priv_count++;
961 reply->len = priv_count * sizeof(*privs);
962 if (reply->len <= get_reply_max_size())
964 privs = set_reply_data_size( priv_count * sizeof(*privs) );
965 if (privs)
967 int i = 0;
968 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
970 luid_and_attr_from_privilege( &privs[i], privilege );
971 i++;
975 else
976 set_error(STATUS_BUFFER_TOO_SMALL);
978 release_object( token );
982 /* creates a duplicate of the token */
983 DECL_HANDLER(duplicate_token)
985 struct token *src_token;
986 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
987 TOKEN_DUPLICATE,
988 &token_ops )))
990 /* FIXME: use req->impersonation_level */
991 struct token *token = create_token( req->primary, src_token->user, NULL, 0, NULL, 0, src_token->default_dacl );
992 if (token)
994 struct privilege *privilege;
995 struct sid_and_attributes *group;
996 unsigned int access;
998 /* copy groups */
999 LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct sid_and_attributes, entry )
1001 size_t size = FIELD_OFFSET( struct sid_and_attributes, sid.SubAuthority[group->sid.SubAuthorityCount] );
1002 struct sid_and_attributes *newgroup = mem_alloc( size );
1003 memcpy( newgroup, group, size );
1004 list_add_tail( &token->groups, &newgroup->entry );
1007 /* copy privileges */
1008 LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
1009 privilege_add( token, &privilege->luid, privilege->enabled );
1011 access = req->access;
1012 if (access & MAXIMUM_ALLOWED) access = TOKEN_ALL_ACCESS; /* FIXME: needs general solution */
1013 reply->new_handle = alloc_handle( current->process, token, access, req->inherit);
1014 release_object( token );
1016 release_object( src_token );
1020 /* checks the specified privileges are held by the token */
1021 DECL_HANDLER(check_token_privileges)
1023 struct token *token;
1025 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1026 TOKEN_QUERY,
1027 &token_ops )))
1029 unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
1030 if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES))
1032 LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
1033 reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
1035 else
1036 set_error( STATUS_BUFFER_OVERFLOW );
1037 release_object( token );
1041 /* checks that a user represented by a token is allowed to access an object
1042 * represented by a security descriptor */
1043 DECL_HANDLER(access_check)
1045 size_t sd_size = get_req_data_size();
1046 const struct security_descriptor *sd = get_req_data();
1047 struct token *token;
1049 if (!sd_is_valid( sd, sd_size ))
1051 set_error( STATUS_ACCESS_VIOLATION );
1052 return;
1055 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1056 TOKEN_QUERY,
1057 &token_ops )))
1059 GENERIC_MAPPING mapping;
1060 unsigned int status;
1061 LUID_AND_ATTRIBUTES priv;
1062 unsigned int priv_count = 1;
1064 memset(&priv, 0, sizeof(priv));
1066 /* only impersonation tokens may be used with this function */
1067 if (token->primary)
1069 set_error( STATUS_NO_IMPERSONATION_TOKEN );
1070 release_object( token );
1071 return;
1074 mapping.GenericRead = req->mapping_read;
1075 mapping.GenericWrite = req->mapping_write;
1076 mapping.GenericExecute = req->mapping_execute;
1077 mapping.GenericAll = req->mapping_all;
1079 reply->access_status = token_access_check(
1080 token, sd, req->desired_access, &priv, &priv_count, &mapping,
1081 &reply->access_granted, &status );
1083 reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES);
1085 if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
1087 LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) );
1088 memcpy( privs, &priv, sizeof(priv) );
1091 if (status != STATUS_SUCCESS)
1092 set_error( status );
1094 release_object( token );
1098 /* */
1099 DECL_HANDLER(get_token_user)
1101 struct token *token;
1103 reply->user_len = 0;
1105 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1106 TOKEN_QUERY,
1107 &token_ops )))
1109 const SID *user = token->user;
1111 reply->user_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
1112 if (reply->user_len <= get_reply_max_size())
1114 SID *user_reply = set_reply_data_size( reply->user_len );
1115 if (user_reply)
1116 memcpy( user_reply, user, reply->user_len );
1118 else set_error( STATUS_BUFFER_TOO_SMALL );
1120 release_object( token );