Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Tokens |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
| 5 | * Copyright (C) 2003 Mike McCormack |
Robert Shearman | d2ea92d | 2005-04-22 21:17:15 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005 Robert Shearman |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 28 | #include <stdarg.h> |
Rob Shearman | 8184bcc | 2007-10-03 13:09:33 +0100 | [diff] [blame] | 29 | #include <unistd.h> |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 30 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 31 | #include "ntstatus.h" |
| 32 | #define WIN32_NO_STATUS |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 33 | #include "windef.h" |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 34 | #include "winternl.h" |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 35 | |
| 36 | #include "handle.h" |
| 37 | #include "thread.h" |
| 38 | #include "process.h" |
| 39 | #include "request.h" |
Robert Shearman | d2ea92d | 2005-04-22 21:17:15 +0000 | [diff] [blame] | 40 | #include "security.h" |
| 41 | |
Robert Shearman | 91eaea5 | 2005-07-18 13:22:55 +0000 | [diff] [blame] | 42 | #include "wine/unicode.h" |
| 43 | |
Robert Shearman | 2a782c6 | 2005-05-16 17:52:46 +0000 | [diff] [blame] | 44 | #define MAX_SUBAUTH_COUNT 1 |
| 45 | |
Robert Shearman | d2ea92d | 2005-04-22 21:17:15 +0000 | [diff] [blame] | 46 | const LUID SeIncreaseQuotaPrivilege = { 5, 0 }; |
| 47 | const LUID SeSecurityPrivilege = { 8, 0 }; |
| 48 | const LUID SeTakeOwnershipPrivilege = { 9, 0 }; |
| 49 | const LUID SeLoadDriverPrivilege = { 10, 0 }; |
| 50 | const LUID SeSystemProfilePrivilege = { 11, 0 }; |
| 51 | const LUID SeSystemtimePrivilege = { 12, 0 }; |
| 52 | const LUID SeProfileSingleProcessPrivilege = { 13, 0 }; |
| 53 | const LUID SeIncreaseBasePriorityPrivilege = { 14, 0 }; |
| 54 | const LUID SeCreatePagefilePrivilege = { 15, 0 }; |
| 55 | const LUID SeBackupPrivilege = { 17, 0 }; |
| 56 | const LUID SeRestorePrivilege = { 18, 0 }; |
| 57 | const LUID SeShutdownPrivilege = { 19, 0 }; |
| 58 | const LUID SeDebugPrivilege = { 20, 0 }; |
| 59 | const LUID SeSystemEnvironmentPrivilege = { 22, 0 }; |
| 60 | const LUID SeChangeNotifyPrivilege = { 23, 0 }; |
| 61 | const LUID SeRemoteShutdownPrivilege = { 24, 0 }; |
| 62 | const LUID SeUndockPrivilege = { 25, 0 }; |
| 63 | const LUID SeManageVolumePrivilege = { 28, 0 }; |
| 64 | const LUID SeImpersonatePrivilege = { 29, 0 }; |
| 65 | const LUID SeCreateGlobalPrivilege = { 30, 0 }; |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 66 | |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 67 | static const SID world_sid = { SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY }, { SECURITY_WORLD_RID } }; |
| 68 | static const SID local_sid = { SID_REVISION, 1, { SECURITY_LOCAL_SID_AUTHORITY }, { SECURITY_LOCAL_RID } }; |
| 69 | static const SID interactive_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_INTERACTIVE_RID } }; |
Rob Shearman | 8184bcc | 2007-10-03 13:09:33 +0100 | [diff] [blame] | 70 | static const SID anonymous_logon_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_ANONYMOUS_LOGON_RID } }; |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 71 | static const SID authenticated_user_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_AUTHENTICATED_USER_RID } }; |
| 72 | static const SID local_system_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SYSTEM_RID } }; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame^] | 73 | const PSID security_world_sid = (PSID)&world_sid; |
Robert Shearman | 91eaea5 | 2005-07-18 13:22:55 +0000 | [diff] [blame] | 74 | static const PSID security_local_sid = (PSID)&local_sid; |
| 75 | const PSID security_interactive_sid = (PSID)&interactive_sid; |
| 76 | static const PSID security_authenticated_user_sid = (PSID)&authenticated_user_sid; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame^] | 77 | const PSID security_local_system_sid = (PSID)&local_system_sid; |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 78 | |
Alexandre Julliard | bf17ce8 | 2007-03-07 12:43:12 +0100 | [diff] [blame] | 79 | static luid_t prev_luid_value = { 1000, 0 }; |
Rob Shearman | df0d625 | 2007-02-15 23:20:48 +0000 | [diff] [blame] | 80 | |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 81 | struct token |
| 82 | { |
| 83 | struct object obj; /* object header */ |
Alexandre Julliard | bf17ce8 | 2007-03-07 12:43:12 +0100 | [diff] [blame] | 84 | luid_t token_id; /* system-unique id of token */ |
| 85 | luid_t modified_id; /* new id allocated every time token is modified */ |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 86 | struct list privileges; /* privileges available to the token */ |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 87 | struct list groups; /* groups that the user of this token belongs to (sid_and_attributes) */ |
Robert Shearman | 2a782c6 | 2005-05-16 17:52:46 +0000 | [diff] [blame] | 88 | SID *user; /* SID of user this token represents */ |
Vitaliy Margolen | 6413a9c | 2007-02-09 09:24:53 -0700 | [diff] [blame] | 89 | SID *primary_group; /* SID of user's primary group */ |
Robert Shearman | f95ef09 | 2005-06-14 18:10:04 +0000 | [diff] [blame] | 90 | unsigned primary; /* is this a primary or impersonation token? */ |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 91 | ACL *default_dacl; /* the default DACL to assign to objects created by this user */ |
Robert Shearman | d18711e | 2006-03-29 18:37:39 +0100 | [diff] [blame] | 92 | TOKEN_SOURCE source; /* source of the token */ |
Rob Shearman | c9b9847 | 2007-02-15 23:21:40 +0000 | [diff] [blame] | 93 | SECURITY_IMPERSONATION_LEVEL impersonation_level; /* impersonation level this token is capable of if non-primary token */ |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | struct privilege |
| 97 | { |
| 98 | struct list entry; |
| 99 | LUID luid; |
Michael Stefaniuc | d40517c | 2005-05-09 09:26:28 +0000 | [diff] [blame] | 100 | unsigned enabled : 1; /* is the privilege currently enabled? */ |
| 101 | unsigned def : 1; /* is the privilege enabled by default? */ |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Robert Shearman | 3396a66 | 2006-05-13 16:58:19 +0100 | [diff] [blame] | 104 | struct group |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 105 | { |
| 106 | struct list entry; |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 107 | unsigned enabled : 1; /* is the sid currently enabled? */ |
| 108 | unsigned def : 1; /* is the sid enabled by default? */ |
| 109 | unsigned logon : 1; /* is this a logon sid? */ |
| 110 | unsigned mandatory: 1; /* is this sid always enabled? */ |
| 111 | unsigned owner : 1; /* can this sid be an owner of an object? */ |
| 112 | unsigned resource : 1; /* is this a domain-local group? */ |
| 113 | unsigned deny_only: 1; /* is this a sid that should be use for denying only? */ |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 114 | SID sid; |
| 115 | }; |
| 116 | |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 117 | static void token_dump( struct object *obj, int verbose ); |
Alexandre Julliard | 5fb2e68 | 2005-12-12 15:01:08 +0100 | [diff] [blame] | 118 | static unsigned int token_map_access( struct object *obj, unsigned int access ); |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 119 | static void token_destroy( struct object *obj ); |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 120 | |
| 121 | static const struct object_ops token_ops = |
| 122 | { |
| 123 | sizeof(struct token), /* size */ |
| 124 | token_dump, /* dump */ |
| 125 | no_add_queue, /* add_queue */ |
| 126 | NULL, /* remove_queue */ |
| 127 | NULL, /* signaled */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 128 | NULL, /* satisfied */ |
| 129 | no_signal, /* signal */ |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 130 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 5fb2e68 | 2005-12-12 15:01:08 +0100 | [diff] [blame] | 131 | token_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 132 | default_get_sd, /* get_sd */ |
| 133 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 134 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 135 | no_open_file, /* open_file */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 136 | no_close_handle, /* close_handle */ |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 137 | token_destroy /* destroy */ |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | |
| 141 | static void token_dump( struct object *obj, int verbose ) |
| 142 | { |
| 143 | fprintf( stderr, "Security token\n" ); |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 144 | /* FIXME: dump token members */ |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Alexandre Julliard | 5fb2e68 | 2005-12-12 15:01:08 +0100 | [diff] [blame] | 147 | static unsigned int token_map_access( struct object *obj, unsigned int access ) |
| 148 | { |
| 149 | if (access & GENERIC_READ) access |= TOKEN_READ; |
| 150 | if (access & GENERIC_WRITE) access |= TOKEN_WRITE; |
| 151 | if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE; |
| 152 | if (access & GENERIC_ALL) access |= TOKEN_ALL_ACCESS; |
| 153 | return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL); |
| 154 | } |
| 155 | |
Robert Shearman | 2a782c6 | 2005-05-16 17:52:46 +0000 | [diff] [blame] | 156 | static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] ) |
| 157 | { |
| 158 | int i; |
| 159 | SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) ); |
| 160 | if (!sid) return NULL; |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 161 | sid->Revision = SID_REVISION; |
Robert Shearman | 2a782c6 | 2005-05-16 17:52:46 +0000 | [diff] [blame] | 162 | sid->SubAuthorityCount = subauthcount; |
| 163 | sid->IdentifierAuthority = *idauthority; |
| 164 | for (i = 0; i < subauthcount; i++) |
| 165 | sid->SubAuthority[i] = subauth[i]; |
| 166 | return sid; |
| 167 | } |
| 168 | |
Robert Shearman | 4bba216 | 2005-06-20 13:18:38 +0000 | [diff] [blame] | 169 | void security_set_thread_token( struct thread *thread, obj_handle_t handle ) |
| 170 | { |
| 171 | if (!handle) |
| 172 | { |
| 173 | if (thread->token) |
| 174 | release_object( thread->token ); |
| 175 | thread->token = NULL; |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | struct token *token = (struct token *)get_handle_obj( current->process, |
| 180 | handle, |
| 181 | TOKEN_IMPERSONATE, |
| 182 | &token_ops ); |
| 183 | if (token) |
| 184 | { |
| 185 | if (thread->token) |
| 186 | release_object( thread->token ); |
| 187 | thread->token = token; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Rob Shearman | 8184bcc | 2007-10-03 13:09:33 +0100 | [diff] [blame] | 192 | const SID *security_unix_uid_to_sid( uid_t uid ) |
| 193 | { |
| 194 | /* very simple mapping: either the current user or not the current user */ |
| 195 | if (uid == getuid()) |
| 196 | return &interactive_sid; |
| 197 | else |
| 198 | return &anonymous_logon_sid; |
| 199 | } |
| 200 | |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 201 | static int acl_is_valid( const ACL *acl, data_size_t size ) |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 202 | { |
| 203 | ULONG i; |
| 204 | const ACE_HEADER *ace; |
| 205 | |
| 206 | if (size < sizeof(ACL)) |
| 207 | return FALSE; |
| 208 | |
| 209 | size = min(size, MAX_ACL_LEN); |
| 210 | |
| 211 | size -= sizeof(ACL); |
| 212 | |
| 213 | ace = (const ACE_HEADER *)(acl + 1); |
| 214 | for (i = 0; i < acl->AceCount; i++) |
| 215 | { |
| 216 | const SID *sid; |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 217 | data_size_t sid_size; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 218 | |
| 219 | if (size < sizeof(ACE_HEADER)) |
| 220 | return FALSE; |
| 221 | if (size < ace->AceSize) |
| 222 | return FALSE; |
| 223 | size -= ace->AceSize; |
| 224 | switch (ace->AceType) |
| 225 | { |
| 226 | case ACCESS_DENIED_ACE_TYPE: |
| 227 | sid = (const SID *)&((const ACCESS_DENIED_ACE *)ace)->SidStart; |
Robert Shearman | dacc3db | 2005-06-14 19:15:58 +0000 | [diff] [blame] | 228 | sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart); |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 229 | break; |
| 230 | case ACCESS_ALLOWED_ACE_TYPE: |
| 231 | sid = (const SID *)&((const ACCESS_ALLOWED_ACE *)ace)->SidStart; |
Robert Shearman | dacc3db | 2005-06-14 19:15:58 +0000 | [diff] [blame] | 232 | sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart); |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 233 | break; |
| 234 | case SYSTEM_AUDIT_ACE_TYPE: |
| 235 | sid = (const SID *)&((const SYSTEM_AUDIT_ACE *)ace)->SidStart; |
Robert Shearman | dacc3db | 2005-06-14 19:15:58 +0000 | [diff] [blame] | 236 | sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart); |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 237 | break; |
| 238 | case SYSTEM_ALARM_ACE_TYPE: |
| 239 | sid = (const SID *)&((const SYSTEM_ALARM_ACE *)ace)->SidStart; |
Robert Shearman | dacc3db | 2005-06-14 19:15:58 +0000 | [diff] [blame] | 240 | sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_ALARM_ACE, SidStart); |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 241 | break; |
| 242 | default: |
| 243 | return FALSE; |
| 244 | } |
Robert Shearman | dacc3db | 2005-06-14 19:15:58 +0000 | [diff] [blame] | 245 | if (sid_size < FIELD_OFFSET(SID, SubAuthority[0]) || |
| 246 | sid_size < FIELD_OFFSET(SID, SubAuthority[sid->SubAuthorityCount])) |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 247 | return FALSE; |
| 248 | ace = ace_next( ace ); |
| 249 | } |
| 250 | return TRUE; |
| 251 | } |
| 252 | |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 253 | /* checks whether all members of a security descriptor fit inside the size |
| 254 | * of memory specified */ |
Rob Shearman | 5af809a | 2007-10-02 15:54:51 +0100 | [diff] [blame] | 255 | int sd_is_valid( const struct security_descriptor *sd, data_size_t size ) |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 256 | { |
| 257 | size_t offset = sizeof(struct security_descriptor); |
| 258 | const SID *group; |
| 259 | const SID *owner; |
| 260 | const ACL *sacl; |
| 261 | const ACL *dacl; |
| 262 | int dummy; |
| 263 | |
| 264 | if (size < offset) |
| 265 | return FALSE; |
| 266 | |
| 267 | if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) || |
| 268 | (offset + sd->owner_len > size)) |
| 269 | return FALSE; |
| 270 | owner = sd_get_owner( sd ); |
| 271 | if (owner) |
| 272 | { |
| 273 | size_t needed_size = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]); |
| 274 | if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len)) |
| 275 | return FALSE; |
| 276 | } |
| 277 | offset += sd->owner_len; |
| 278 | |
| 279 | if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) || |
| 280 | (offset + sd->group_len > size)) |
| 281 | return FALSE; |
| 282 | group = sd_get_group( sd ); |
| 283 | if (group) |
| 284 | { |
| 285 | size_t needed_size = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]); |
Vitaliy Margolen | aa28ed0 | 2007-02-07 21:11:18 -0700 | [diff] [blame] | 286 | if ((sd->group_len < sizeof(SID)) || (needed_size > sd->group_len)) |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 287 | return FALSE; |
| 288 | } |
| 289 | offset += sd->group_len; |
| 290 | |
| 291 | if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size)) |
| 292 | return FALSE; |
| 293 | sacl = sd_get_sacl( sd, &dummy ); |
| 294 | if (sacl && !acl_is_valid( sacl, sd->sacl_len )) |
| 295 | return FALSE; |
| 296 | offset += sd->sacl_len; |
| 297 | |
| 298 | if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size)) |
| 299 | return FALSE; |
| 300 | dacl = sd_get_dacl( sd, &dummy ); |
| 301 | if (dacl && !acl_is_valid( dacl, sd->dacl_len )) |
| 302 | return FALSE; |
| 303 | offset += sd->dacl_len; |
| 304 | |
| 305 | return TRUE; |
| 306 | } |
| 307 | |
| 308 | /* maps from generic rights to specific rights as given by a mapping */ |
| 309 | static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping) |
| 310 | { |
| 311 | if (*mask & GENERIC_READ) *mask |= mapping->GenericRead; |
| 312 | if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite; |
| 313 | if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute; |
| 314 | if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll; |
| 315 | *mask &= 0x0FFFFFFF; |
| 316 | } |
| 317 | |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 318 | static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 ) |
| 319 | { |
| 320 | return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart); |
| 321 | } |
| 322 | |
Alexandre Julliard | bf17ce8 | 2007-03-07 12:43:12 +0100 | [diff] [blame] | 323 | static inline void allocate_luid( luid_t *luid ) |
Rob Shearman | df0d625 | 2007-02-15 23:20:48 +0000 | [diff] [blame] | 324 | { |
Alexandre Julliard | bf17ce8 | 2007-03-07 12:43:12 +0100 | [diff] [blame] | 325 | prev_luid_value.low_part++; |
Rob Shearman | df0d625 | 2007-02-15 23:20:48 +0000 | [diff] [blame] | 326 | *luid = prev_luid_value; |
| 327 | } |
| 328 | |
Juan Lang | c2cb296 | 2007-03-06 16:33:26 -0800 | [diff] [blame] | 329 | DECL_HANDLER( allocate_locally_unique_id ) |
| 330 | { |
Alexandre Julliard | bf17ce8 | 2007-03-07 12:43:12 +0100 | [diff] [blame] | 331 | allocate_luid( &reply->luid ); |
Juan Lang | c2cb296 | 2007-03-06 16:33:26 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 334 | static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in) |
| 335 | { |
| 336 | out->Luid = in->luid; |
| 337 | out->Attributes = |
| 338 | (in->enabled ? SE_PRIVILEGE_ENABLED : 0) | |
| 339 | (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0); |
| 340 | } |
| 341 | |
| 342 | static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled ) |
| 343 | { |
| 344 | struct privilege *privilege = mem_alloc( sizeof(*privilege) ); |
| 345 | if (privilege) |
| 346 | { |
| 347 | privilege->luid = *luid; |
| 348 | privilege->def = privilege->enabled = (enabled != 0); |
| 349 | list_add_tail( &token->privileges, &privilege->entry ); |
| 350 | } |
| 351 | return privilege; |
| 352 | } |
| 353 | |
Robert Shearman | 2a782c6 | 2005-05-16 17:52:46 +0000 | [diff] [blame] | 354 | static inline void privilege_remove( struct privilege *privilege ) |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 355 | { |
| 356 | list_remove( &privilege->entry ); |
| 357 | free( privilege ); |
| 358 | } |
| 359 | |
| 360 | static void token_destroy( struct object *obj ) |
| 361 | { |
| 362 | struct token* token; |
| 363 | struct list *cursor, *cursor_next; |
| 364 | |
| 365 | assert( obj->ops == &token_ops ); |
| 366 | token = (struct token *)obj; |
| 367 | |
Robert Shearman | 2a782c6 | 2005-05-16 17:52:46 +0000 | [diff] [blame] | 368 | free( token->user ); |
| 369 | |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 370 | LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges ) |
| 371 | { |
| 372 | struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry ); |
| 373 | privilege_remove( privilege ); |
| 374 | } |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 375 | |
| 376 | LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups ) |
| 377 | { |
Robert Shearman | 3396a66 | 2006-05-13 16:58:19 +0100 | [diff] [blame] | 378 | struct group *group = LIST_ENTRY( cursor, struct group, entry ); |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 379 | list_remove( &group->entry ); |
| 380 | free( group ); |
| 381 | } |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 382 | |
| 383 | free( token->default_dacl ); |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 386 | /* creates a new token. |
| 387 | * groups may be NULL if group_count is 0. |
| 388 | * privs may be NULL if priv_count is 0. |
| 389 | * default_dacl may be NULL, indicating that all objects created by the user |
| 390 | * are unsecured. |
Rob Shearman | df0d625 | 2007-02-15 23:20:48 +0000 | [diff] [blame] | 391 | * modified_id may be NULL, indicating that a new modified_id luid should be |
| 392 | * allocated. |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 393 | */ |
Robert Shearman | f95ef09 | 2005-06-14 18:10:04 +0000 | [diff] [blame] | 394 | static struct token *create_token( unsigned primary, const SID *user, |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 395 | const SID_AND_ATTRIBUTES *groups, unsigned int group_count, |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 396 | const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count, |
Rob Shearman | df0d625 | 2007-02-15 23:20:48 +0000 | [diff] [blame] | 397 | const ACL *default_dacl, TOKEN_SOURCE source, |
Alexandre Julliard | bf17ce8 | 2007-03-07 12:43:12 +0100 | [diff] [blame] | 398 | const luid_t *modified_id, |
Rob Shearman | c9b9847 | 2007-02-15 23:21:40 +0000 | [diff] [blame] | 399 | SECURITY_IMPERSONATION_LEVEL impersonation_level ) |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 400 | { |
| 401 | struct token *token = alloc_object( &token_ops ); |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 402 | if (token) |
| 403 | { |
Michael Stefaniuc | a624977 | 2006-07-25 10:30:04 +0200 | [diff] [blame] | 404 | unsigned int i; |
Robert Shearman | f95ef09 | 2005-06-14 18:10:04 +0000 | [diff] [blame] | 405 | |
Rob Shearman | df0d625 | 2007-02-15 23:20:48 +0000 | [diff] [blame] | 406 | allocate_luid( &token->token_id ); |
| 407 | if (modified_id) |
| 408 | token->modified_id = *modified_id; |
| 409 | else |
| 410 | allocate_luid( &token->modified_id ); |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 411 | list_init( &token->privileges ); |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 412 | list_init( &token->groups ); |
Robert Shearman | f95ef09 | 2005-06-14 18:10:04 +0000 | [diff] [blame] | 413 | token->primary = primary; |
Rob Shearman | c9b9847 | 2007-02-15 23:21:40 +0000 | [diff] [blame] | 414 | /* primary tokens don't have impersonation levels */ |
| 415 | if (primary) |
| 416 | token->impersonation_level = -1; |
| 417 | else |
| 418 | token->impersonation_level = impersonation_level; |
Vitaliy Margolen | 55fdda4 | 2007-01-24 23:43:30 -0700 | [diff] [blame] | 419 | token->default_dacl = NULL; |
Vitaliy Margolen | 6413a9c | 2007-02-09 09:24:53 -0700 | [diff] [blame] | 420 | token->primary_group = NULL; |
Robert Shearman | f95ef09 | 2005-06-14 18:10:04 +0000 | [diff] [blame] | 421 | |
Robert Shearman | 2a782c6 | 2005-05-16 17:52:46 +0000 | [diff] [blame] | 422 | /* copy user */ |
| 423 | token->user = memdup( user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) ); |
| 424 | if (!token->user) |
| 425 | { |
| 426 | release_object( token ); |
| 427 | return NULL; |
| 428 | } |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 429 | |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 430 | /* copy groups */ |
| 431 | for (i = 0; i < group_count; i++) |
| 432 | { |
Robert Shearman | 3396a66 | 2006-05-13 16:58:19 +0100 | [diff] [blame] | 433 | size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] ); |
| 434 | struct group *group = mem_alloc( size ); |
Robert Shearman | 9e0d056 | 2006-04-07 11:16:28 +0100 | [diff] [blame] | 435 | |
| 436 | if (!group) |
| 437 | { |
| 438 | release_object( token ); |
| 439 | return NULL; |
| 440 | } |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 441 | memcpy( &group->sid, groups[i].Sid, FIELD_OFFSET( SID, SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] ) ); |
| 442 | group->enabled = TRUE; |
| 443 | group->def = TRUE; |
| 444 | group->logon = FALSE; |
| 445 | group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) ? TRUE : FALSE; |
Vitaliy Margolen | 6413a9c | 2007-02-09 09:24:53 -0700 | [diff] [blame] | 446 | group->owner = groups[i].Attributes & SE_GROUP_OWNER ? TRUE : FALSE; |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 447 | group->resource = FALSE; |
| 448 | group->deny_only = FALSE; |
| 449 | list_add_tail( &token->groups, &group->entry ); |
Vitaliy Margolen | 6413a9c | 2007-02-09 09:24:53 -0700 | [diff] [blame] | 450 | /* Use first owner capable group as an owner */ |
| 451 | if (!token->primary_group && group->owner) |
| 452 | token->primary_group = &group->sid; |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 453 | } |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 454 | |
Robert Shearman | 2a782c6 | 2005-05-16 17:52:46 +0000 | [diff] [blame] | 455 | /* copy privileges */ |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 456 | for (i = 0; i < priv_count; i++) |
| 457 | { |
| 458 | /* note: we don't check uniqueness: the caller must make sure |
| 459 | * privs doesn't contain any duplicate luids */ |
| 460 | if (!privilege_add( token, &privs[i].Luid, |
| 461 | privs[i].Attributes & SE_PRIVILEGE_ENABLED )) |
| 462 | { |
| 463 | release_object( token ); |
| 464 | return NULL; |
| 465 | } |
| 466 | } |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 467 | |
| 468 | if (default_dacl) |
| 469 | { |
| 470 | token->default_dacl = memdup( default_dacl, default_dacl->AclSize ); |
| 471 | if (!token->default_dacl) |
| 472 | { |
| 473 | release_object( token ); |
| 474 | return NULL; |
| 475 | } |
| 476 | } |
Robert Shearman | d18711e | 2006-03-29 18:37:39 +0100 | [diff] [blame] | 477 | |
| 478 | token->source = source; |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 479 | } |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 480 | return token; |
| 481 | } |
| 482 | |
Rob Shearman | bdf964d | 2007-05-28 18:39:33 +0100 | [diff] [blame] | 483 | struct token *token_duplicate( struct token *src_token, unsigned primary, |
| 484 | SECURITY_IMPERSONATION_LEVEL impersonation_level ) |
| 485 | { |
| 486 | const luid_t *modified_id = |
| 487 | primary || (impersonation_level == src_token->impersonation_level) ? |
| 488 | &src_token->modified_id : NULL; |
| 489 | struct token *token = NULL; |
| 490 | struct privilege *privilege; |
| 491 | struct group *group; |
| 492 | |
| 493 | if ((impersonation_level < SecurityAnonymous) || |
| 494 | (impersonation_level > SecurityDelegation)) |
| 495 | { |
| 496 | set_error( STATUS_BAD_IMPERSONATION_LEVEL ); |
| 497 | return NULL; |
| 498 | } |
| 499 | |
| 500 | if (primary || (impersonation_level <= src_token->impersonation_level)) |
| 501 | token = create_token( primary, src_token->user, NULL, 0, |
| 502 | NULL, 0, src_token->default_dacl, |
| 503 | src_token->source, modified_id, |
| 504 | impersonation_level ); |
| 505 | else set_error( STATUS_BAD_IMPERSONATION_LEVEL ); |
| 506 | |
| 507 | if (!token) return token; |
| 508 | |
| 509 | /* copy groups */ |
| 510 | LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct group, entry ) |
| 511 | { |
| 512 | size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[group->sid.SubAuthorityCount] ); |
| 513 | struct group *newgroup = mem_alloc( size ); |
| 514 | if (!newgroup) |
| 515 | { |
| 516 | release_object( token ); |
| 517 | return NULL; |
| 518 | } |
| 519 | memcpy( newgroup, group, size ); |
| 520 | list_add_tail( &token->groups, &newgroup->entry ); |
| 521 | } |
| 522 | token->primary_group = src_token->primary_group; |
| 523 | assert( token->primary_group ); |
| 524 | |
| 525 | /* copy privileges */ |
| 526 | LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry ) |
| 527 | if (!privilege_add( token, &privilege->luid, privilege->enabled )) |
| 528 | { |
| 529 | release_object( token ); |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | return token; |
| 534 | } |
| 535 | |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 536 | static ACL *create_default_dacl( const SID *user ) |
| 537 | { |
| 538 | ACCESS_ALLOWED_ACE *aaa; |
| 539 | ACL *default_dacl; |
| 540 | SID *sid; |
| 541 | size_t default_dacl_size = sizeof(ACL) + |
| 542 | 2*(sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) + |
| 543 | sizeof(local_system_sid) + |
| 544 | FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]); |
| 545 | |
| 546 | default_dacl = mem_alloc( default_dacl_size ); |
| 547 | if (!default_dacl) return NULL; |
| 548 | |
Rob Shearman | 45b6706 | 2007-10-02 15:57:02 +0100 | [diff] [blame] | 549 | default_dacl->AclRevision = ACL_REVISION; |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 550 | default_dacl->Sbz1 = 0; |
| 551 | default_dacl->AclSize = default_dacl_size; |
| 552 | default_dacl->AceCount = 2; |
| 553 | default_dacl->Sbz2 = 0; |
| 554 | |
| 555 | /* GENERIC_ALL for Local System */ |
| 556 | aaa = (ACCESS_ALLOWED_ACE *)(default_dacl + 1); |
| 557 | aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE; |
| 558 | aaa->Header.AceFlags = 0; |
| 559 | aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) + |
| 560 | sizeof(local_system_sid); |
| 561 | aaa->Mask = GENERIC_ALL; |
| 562 | sid = (SID *)&aaa->SidStart; |
| 563 | memcpy( sid, &local_system_sid, sizeof(local_system_sid) ); |
| 564 | |
| 565 | /* GENERIC_ALL for specified user */ |
Andrew Talbot | 53dae9b | 2006-12-16 17:43:17 +0000 | [diff] [blame] | 566 | aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize); |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 567 | aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE; |
| 568 | aaa->Header.AceFlags = 0; |
| 569 | aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) + |
| 570 | FIELD_OFFSET( SID, SubAuthority[user->SubAuthorityCount] ); |
| 571 | aaa->Mask = GENERIC_ALL; |
| 572 | sid = (SID *)&aaa->SidStart; |
| 573 | memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) ); |
| 574 | |
| 575 | return default_dacl; |
| 576 | } |
| 577 | |
Robert Shearman | 2a782c6 | 2005-05-16 17:52:46 +0000 | [diff] [blame] | 578 | struct sid_data |
| 579 | { |
| 580 | SID_IDENTIFIER_AUTHORITY idauth; |
| 581 | int count; |
| 582 | unsigned int subauth[MAX_SUBAUTH_COUNT]; |
| 583 | }; |
| 584 | |
Robert Shearman | d2ea92d | 2005-04-22 21:17:15 +0000 | [diff] [blame] | 585 | struct token *token_create_admin( void ) |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 586 | { |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 587 | struct token *token = NULL; |
| 588 | static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY }; |
| 589 | static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS }; |
| 590 | static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS }; |
Rob Shearman | fa074bc | 2007-05-18 10:51:50 +0100 | [diff] [blame] | 591 | /* on Windows, this value changes every time the user logs on */ |
| 592 | static const unsigned int logon_subauth[] = { SECURITY_LOGON_IDS_RID, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ }; |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 593 | PSID alias_admins_sid; |
| 594 | PSID alias_users_sid; |
Rob Shearman | fa074bc | 2007-05-18 10:51:50 +0100 | [diff] [blame] | 595 | PSID logon_sid; |
Vitaliy Margolen | 97b122c | 2007-01-19 12:28:59 -0700 | [diff] [blame] | 596 | /* note: should be the owner specified in the token */ |
| 597 | ACL *default_dacl = create_default_dacl( &interactive_sid ); |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 598 | |
| 599 | alias_admins_sid = security_sid_alloc( &nt_authority, sizeof(alias_admins_subauth)/sizeof(alias_admins_subauth[0]), |
| 600 | alias_admins_subauth ); |
| 601 | alias_users_sid = security_sid_alloc( &nt_authority, sizeof(alias_users_subauth)/sizeof(alias_users_subauth[0]), |
| 602 | alias_users_subauth ); |
Rob Shearman | fa074bc | 2007-05-18 10:51:50 +0100 | [diff] [blame] | 603 | logon_sid = security_sid_alloc( &nt_authority, sizeof(logon_subauth)/sizeof(logon_subauth[0]), |
| 604 | logon_subauth ); |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 605 | |
Rob Shearman | fa074bc | 2007-05-18 10:51:50 +0100 | [diff] [blame] | 606 | if (alias_admins_sid && alias_users_sid && logon_sid && default_dacl) |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 607 | { |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 608 | const LUID_AND_ATTRIBUTES admin_privs[] = |
| 609 | { |
| 610 | { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED }, |
| 611 | { SeSecurityPrivilege , 0 }, |
| 612 | { SeBackupPrivilege , 0 }, |
| 613 | { SeRestorePrivilege , 0 }, |
| 614 | { SeSystemtimePrivilege , 0 }, |
| 615 | { SeShutdownPrivilege , 0 }, |
| 616 | { SeRemoteShutdownPrivilege , 0 }, |
| 617 | { SeTakeOwnershipPrivilege , 0 }, |
| 618 | { SeDebugPrivilege , 0 }, |
| 619 | { SeSystemEnvironmentPrivilege , 0 }, |
| 620 | { SeSystemProfilePrivilege , 0 }, |
| 621 | { SeProfileSingleProcessPrivilege, 0 }, |
| 622 | { SeIncreaseBasePriorityPrivilege, 0 }, |
Peter Dons Tychsen | ec4d890 | 2007-07-27 02:05:04 +0200 | [diff] [blame] | 623 | { SeLoadDriverPrivilege , SE_PRIVILEGE_ENABLED }, |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 624 | { SeCreatePagefilePrivilege , 0 }, |
| 625 | { SeIncreaseQuotaPrivilege , 0 }, |
| 626 | { SeUndockPrivilege , 0 }, |
| 627 | { SeManageVolumePrivilege , 0 }, |
| 628 | { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED }, |
| 629 | { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED }, |
| 630 | }; |
| 631 | /* note: we don't include non-builtin groups here for the user - |
| 632 | * telling us these is the job of a client-side program */ |
| 633 | const SID_AND_ATTRIBUTES admin_groups[] = |
| 634 | { |
| 635 | { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY }, |
| 636 | { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY }, |
| 637 | { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY }, |
| 638 | { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY }, |
Vitaliy Margolen | 6413a9c | 2007-02-09 09:24:53 -0700 | [diff] [blame] | 639 | { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER }, |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 640 | { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY }, |
Rob Shearman | fa074bc | 2007-05-18 10:51:50 +0100 | [diff] [blame] | 641 | { logon_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_LOGON_ID }, |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 642 | }; |
Robert Shearman | d18711e | 2006-03-29 18:37:39 +0100 | [diff] [blame] | 643 | static const TOKEN_SOURCE admin_source = {"SeMgr", {0, 0}}; |
Rob Shearman | 8184bcc | 2007-10-03 13:09:33 +0100 | [diff] [blame] | 644 | token = create_token( TRUE, security_unix_uid_to_sid( getuid() ), |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 645 | admin_groups, sizeof(admin_groups)/sizeof(admin_groups[0]), |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 646 | admin_privs, sizeof(admin_privs)/sizeof(admin_privs[0]), |
Rob Shearman | c9b9847 | 2007-02-15 23:21:40 +0000 | [diff] [blame] | 647 | default_dacl, admin_source, NULL, -1 ); |
Vitaliy Margolen | 6413a9c | 2007-02-09 09:24:53 -0700 | [diff] [blame] | 648 | /* we really need a primary group */ |
| 649 | assert( token->primary_group ); |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Rob Shearman | fa074bc | 2007-05-18 10:51:50 +0100 | [diff] [blame] | 652 | free( logon_sid ); |
Michael Stefaniuc | 5ceccec | 2006-10-09 23:34:36 +0200 | [diff] [blame] | 653 | free( alias_admins_sid ); |
| 654 | free( alias_users_sid ); |
| 655 | free( default_dacl ); |
Robert Shearman | 9b82644 | 2005-06-09 09:47:28 +0000 | [diff] [blame] | 656 | |
Robert Shearman | 2a782c6 | 2005-05-16 17:52:46 +0000 | [diff] [blame] | 657 | return token; |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 660 | static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only ) |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 661 | { |
| 662 | struct privilege *privilege; |
| 663 | LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry ) |
| 664 | { |
| 665 | if (is_equal_luid( luid, &privilege->luid )) |
| 666 | { |
| 667 | if (enabled_only && !privilege->enabled) |
| 668 | return NULL; |
| 669 | return privilege; |
| 670 | } |
| 671 | } |
| 672 | return NULL; |
| 673 | } |
| 674 | |
| 675 | static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs, |
| 676 | unsigned int count, LUID_AND_ATTRIBUTES *mod_privs, |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 677 | unsigned int mod_privs_count ) |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 678 | { |
Michael Stefaniuc | a624977 | 2006-07-25 10:30:04 +0200 | [diff] [blame] | 679 | unsigned int i, modified_count = 0; |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 680 | |
Rob Shearman | df0d625 | 2007-02-15 23:20:48 +0000 | [diff] [blame] | 681 | /* mark as modified */ |
| 682 | allocate_luid( &token->modified_id ); |
| 683 | |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 684 | for (i = 0; i < count; i++) |
| 685 | { |
| 686 | struct privilege *privilege = |
| 687 | token_find_privilege( token, &privs[i].Luid, FALSE ); |
| 688 | if (!privilege) |
| 689 | { |
| 690 | set_error( STATUS_NOT_ALL_ASSIGNED ); |
| 691 | continue; |
| 692 | } |
| 693 | |
| 694 | if (privs[i].Attributes & SE_PRIVILEGE_REMOVE) |
| 695 | privilege_remove( privilege ); |
| 696 | else |
| 697 | { |
| 698 | /* save previous state for caller */ |
| 699 | if (mod_privs_count) |
| 700 | { |
| 701 | luid_and_attr_from_privilege(mod_privs, privilege); |
| 702 | mod_privs++; |
| 703 | mod_privs_count--; |
| 704 | modified_count++; |
| 705 | } |
| 706 | |
| 707 | if (privs[i].Attributes & SE_PRIVILEGE_ENABLED) |
| 708 | privilege->enabled = TRUE; |
| 709 | else |
| 710 | privilege->enabled = FALSE; |
| 711 | } |
| 712 | } |
| 713 | return modified_count; |
| 714 | } |
| 715 | |
| 716 | static void token_disable_privileges( struct token *token ) |
| 717 | { |
| 718 | struct privilege *privilege; |
Rob Shearman | df0d625 | 2007-02-15 23:20:48 +0000 | [diff] [blame] | 719 | |
| 720 | /* mark as modified */ |
| 721 | allocate_luid( &token->modified_id ); |
| 722 | |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 723 | LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry ) |
| 724 | privilege->enabled = FALSE; |
| 725 | } |
| 726 | |
Robert Shearman | d2ea92d | 2005-04-22 21:17:15 +0000 | [diff] [blame] | 727 | int token_check_privileges( struct token *token, int all_required, |
| 728 | const LUID_AND_ATTRIBUTES *reqprivs, |
| 729 | unsigned int count, LUID_AND_ATTRIBUTES *usedprivs) |
| 730 | { |
Michael Stefaniuc | a624977 | 2006-07-25 10:30:04 +0200 | [diff] [blame] | 731 | unsigned int i, enabled_count = 0; |
Robert Shearman | d2ea92d | 2005-04-22 21:17:15 +0000 | [diff] [blame] | 732 | |
| 733 | for (i = 0; i < count; i++) |
| 734 | { |
| 735 | struct privilege *privilege = |
| 736 | token_find_privilege( token, &reqprivs[i].Luid, TRUE ); |
| 737 | |
| 738 | if (usedprivs) |
| 739 | usedprivs[i] = reqprivs[i]; |
| 740 | |
| 741 | if (privilege && privilege->enabled) |
| 742 | { |
| 743 | enabled_count++; |
| 744 | if (usedprivs) |
| 745 | usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | if (all_required) |
| 750 | return (enabled_count == count); |
| 751 | else |
| 752 | return (enabled_count > 0); |
| 753 | } |
| 754 | |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 755 | static int token_sid_present( struct token *token, const SID *sid, int deny ) |
| 756 | { |
Robert Shearman | 3396a66 | 2006-05-13 16:58:19 +0100 | [diff] [blame] | 757 | struct group *group; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 758 | |
| 759 | if (security_equal_sid( token->user, sid )) return TRUE; |
| 760 | |
Robert Shearman | 3396a66 | 2006-05-13 16:58:19 +0100 | [diff] [blame] | 761 | LIST_FOR_EACH_ENTRY( group, &token->groups, struct group, entry ) |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 762 | { |
| 763 | if (!group->enabled) continue; |
| 764 | if (group->deny_only && !deny) continue; |
| 765 | |
| 766 | if (security_equal_sid( &group->sid, sid )) return TRUE; |
| 767 | } |
| 768 | |
| 769 | return FALSE; |
| 770 | } |
| 771 | |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 772 | /* Checks access to a security descriptor. 'sd' must have been validated by |
| 773 | * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating |
| 774 | * the reason. 'status' parameter will indicate if access is granted or denied. |
| 775 | * |
| 776 | * If both returned value and 'status' are STATUS_SUCCESS then access is granted. |
| 777 | */ |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 778 | static unsigned int token_access_check( struct token *token, |
| 779 | const struct security_descriptor *sd, |
| 780 | unsigned int desired_access, |
| 781 | LUID_AND_ATTRIBUTES *privs, |
| 782 | unsigned int *priv_count, |
| 783 | const GENERIC_MAPPING *mapping, |
| 784 | unsigned int *granted_access, |
| 785 | unsigned int *status ) |
| 786 | { |
| 787 | unsigned int current_access = 0; |
| 788 | unsigned int denied_access = 0; |
| 789 | ULONG i; |
| 790 | const ACL *dacl; |
| 791 | int dacl_present; |
| 792 | const ACE_HEADER *ace; |
| 793 | const SID *owner; |
| 794 | |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 795 | /* assume no access rights */ |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 796 | *granted_access = 0; |
| 797 | |
| 798 | /* fail if desired_access contains generic rights */ |
| 799 | if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL)) |
| 800 | { |
| 801 | *priv_count = 0; |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 802 | return STATUS_GENERIC_NOT_MAPPED; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | dacl = sd_get_dacl( sd, &dacl_present ); |
| 806 | owner = sd_get_owner( sd ); |
| 807 | if (!owner || !sd_get_group( sd )) |
| 808 | { |
| 809 | *priv_count = 0; |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 810 | return STATUS_INVALID_SECURITY_DESCR; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /* 1: Grant desired access if the object is unprotected */ |
| 814 | if (!dacl_present) |
| 815 | { |
| 816 | *priv_count = 0; |
| 817 | *granted_access = desired_access; |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 818 | return *status = STATUS_SUCCESS; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 819 | } |
| 820 | if (!dacl) |
| 821 | { |
| 822 | *priv_count = 0; |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 823 | *status = STATUS_ACCESS_DENIED; |
| 824 | return STATUS_SUCCESS; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /* 2: Check if caller wants access to system security part. Note: access |
| 828 | * is only granted if specifically asked for */ |
| 829 | if (desired_access & ACCESS_SYSTEM_SECURITY) |
| 830 | { |
| 831 | const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 }; |
| 832 | LUID_AND_ATTRIBUTES retpriv = security_priv; |
| 833 | if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv )) |
| 834 | { |
| 835 | if (priv_count) |
| 836 | { |
| 837 | /* assumes that there will only be one privilege to return */ |
| 838 | if (*priv_count >= 1) |
| 839 | { |
| 840 | *priv_count = 1; |
| 841 | *privs = retpriv; |
| 842 | } |
| 843 | else |
| 844 | { |
| 845 | *priv_count = 1; |
| 846 | return STATUS_BUFFER_TOO_SMALL; |
| 847 | } |
| 848 | } |
| 849 | current_access |= ACCESS_SYSTEM_SECURITY; |
| 850 | if (desired_access == current_access) |
| 851 | { |
| 852 | *granted_access = current_access; |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 853 | return *status = STATUS_SUCCESS; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | else |
| 857 | { |
| 858 | *priv_count = 0; |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 859 | *status = STATUS_PRIVILEGE_NOT_HELD; |
| 860 | return STATUS_SUCCESS; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | else if (priv_count) *priv_count = 0; |
| 864 | |
| 865 | /* 3: Check whether the token is the owner */ |
| 866 | /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead |
| 867 | * checked when a "set owner" call is made, overriding the access rights |
| 868 | * determined here. */ |
| 869 | if (token_sid_present( token, owner, FALSE )) |
| 870 | { |
| 871 | current_access |= (READ_CONTROL | WRITE_DAC); |
| 872 | if (desired_access == current_access) |
| 873 | { |
| 874 | *granted_access = current_access; |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 875 | return *status = STATUS_SUCCESS; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
| 879 | /* 4: Grant rights according to the DACL */ |
| 880 | ace = (const ACE_HEADER *)(dacl + 1); |
| 881 | for (i = 0; i < dacl->AceCount; i++) |
| 882 | { |
| 883 | const ACCESS_ALLOWED_ACE *aa_ace; |
| 884 | const ACCESS_DENIED_ACE *ad_ace; |
| 885 | const SID *sid; |
| 886 | switch (ace->AceType) |
| 887 | { |
| 888 | case ACCESS_DENIED_ACE_TYPE: |
| 889 | ad_ace = (const ACCESS_DENIED_ACE *)ace; |
| 890 | sid = (const SID *)&ad_ace->SidStart; |
| 891 | if (token_sid_present( token, sid, TRUE )) |
| 892 | { |
| 893 | unsigned int access = ad_ace->Mask; |
| 894 | map_generic_mask(&access, mapping); |
| 895 | if (desired_access & MAXIMUM_ALLOWED) |
| 896 | denied_access |= access; |
| 897 | else |
| 898 | { |
| 899 | denied_access |= (access & ~current_access); |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 900 | if (desired_access & access) goto done; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | break; |
| 904 | case ACCESS_ALLOWED_ACE_TYPE: |
| 905 | aa_ace = (const ACCESS_ALLOWED_ACE *)ace; |
| 906 | sid = (const SID *)&aa_ace->SidStart; |
| 907 | if (token_sid_present( token, sid, FALSE )) |
| 908 | { |
| 909 | unsigned int access = aa_ace->Mask; |
| 910 | map_generic_mask(&access, mapping); |
| 911 | if (desired_access & MAXIMUM_ALLOWED) |
| 912 | current_access |= access; |
| 913 | else |
| 914 | current_access |= (access & ~denied_access); |
| 915 | } |
| 916 | break; |
| 917 | } |
| 918 | |
| 919 | /* don't bother carrying on checking if we've already got all of |
| 920 | * rights we need */ |
| 921 | if (desired_access == *granted_access) |
| 922 | break; |
| 923 | |
| 924 | ace = ace_next( ace ); |
| 925 | } |
| 926 | |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 927 | done: |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 928 | if (desired_access & MAXIMUM_ALLOWED) |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 929 | *granted_access = current_access & ~denied_access; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 930 | else |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 931 | if ((current_access & desired_access) == desired_access) |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 932 | *granted_access = current_access & desired_access; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 933 | else |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 934 | *granted_access = 0; |
| 935 | |
| 936 | *status = *granted_access ? STATUS_SUCCESS : STATUS_ACCESS_DENIED; |
| 937 | return STATUS_SUCCESS; |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Robert Shearman | fbf0ea9 | 2005-07-13 19:31:27 +0000 | [diff] [blame] | 940 | const ACL *token_get_default_dacl( struct token *token ) |
| 941 | { |
| 942 | return token->default_dacl; |
| 943 | } |
| 944 | |
Rob Shearman | 5af809a | 2007-10-02 15:54:51 +0100 | [diff] [blame] | 945 | const SID *token_get_user( struct token *token ) |
Vitaliy Margolen | b0e9d7e | 2007-02-07 21:43:11 -0700 | [diff] [blame] | 946 | { |
Rob Shearman | 5af809a | 2007-10-02 15:54:51 +0100 | [diff] [blame] | 947 | return token->user; |
| 948 | } |
Vitaliy Margolen | b0e9d7e | 2007-02-07 21:43:11 -0700 | [diff] [blame] | 949 | |
Rob Shearman | 5af809a | 2007-10-02 15:54:51 +0100 | [diff] [blame] | 950 | const SID *token_get_primary_group( struct token *token ) |
| 951 | { |
| 952 | return token->primary_group; |
Vitaliy Margolen | b0e9d7e | 2007-02-07 21:43:11 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Rob Shearman | 6a76a0a | 2007-02-21 13:59:59 +0000 | [diff] [blame] | 955 | int check_object_access(struct object *obj, unsigned int *access) |
| 956 | { |
| 957 | GENERIC_MAPPING mapping; |
| 958 | struct token *token = current->token ? current->token : current->process->token; |
| 959 | LUID_AND_ATTRIBUTES priv; |
| 960 | unsigned int status, priv_count = 1; |
| 961 | int res; |
| 962 | |
| 963 | mapping.GenericAll = obj->ops->map_access( obj, GENERIC_ALL ); |
| 964 | |
| 965 | if (!obj->sd) |
| 966 | { |
| 967 | if (*access & MAXIMUM_ALLOWED) |
| 968 | *access = mapping.GenericAll; |
| 969 | return TRUE; |
| 970 | } |
| 971 | |
| 972 | mapping.GenericRead = obj->ops->map_access( obj, GENERIC_READ ); |
| 973 | mapping.GenericWrite = obj->ops->map_access( obj, GENERIC_WRITE ); |
| 974 | mapping.GenericExecute = obj->ops->map_access( obj, GENERIC_EXECUTE ); |
| 975 | |
| 976 | res = token_access_check( token, obj->sd, *access, &priv, &priv_count, |
| 977 | &mapping, access, &status ) == STATUS_SUCCESS && |
| 978 | status == STATUS_SUCCESS; |
| 979 | |
| 980 | if (!res) set_error( STATUS_ACCESS_DENIED ); |
| 981 | return res; |
| 982 | } |
| 983 | |
| 984 | |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 985 | /* open a security token */ |
| 986 | DECL_HANDLER(open_token) |
| 987 | { |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 988 | if (req->flags & OPEN_TOKEN_THREAD) |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 989 | { |
| 990 | struct thread *thread = get_thread_from_handle( req->handle, 0 ); |
| 991 | if (thread) |
| 992 | { |
| 993 | if (thread->token) |
Rob Shearman | c9b9847 | 2007-02-15 23:21:40 +0000 | [diff] [blame] | 994 | { |
| 995 | if (thread->token->impersonation_level <= SecurityAnonymous) |
| 996 | set_error( STATUS_CANT_OPEN_ANONYMOUS ); |
| 997 | else |
| 998 | reply->token = alloc_handle( current->process, thread->token, |
| 999 | req->access, req->attributes ); |
| 1000 | } |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 1001 | else |
Rob Shearman | c9b9847 | 2007-02-15 23:21:40 +0000 | [diff] [blame] | 1002 | set_error( STATUS_NO_TOKEN ); |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 1003 | release_object( thread ); |
| 1004 | } |
| 1005 | } |
| 1006 | else |
| 1007 | { |
| 1008 | struct process *process = get_process_from_handle( req->handle, 0 ); |
| 1009 | if (process) |
| 1010 | { |
| 1011 | if (process->token) |
Alexandre Julliard | 836d07c | 2005-12-09 12:17:19 +0100 | [diff] [blame] | 1012 | reply->token = alloc_handle( current->process, process->token, req->access, |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 1013 | req->attributes ); |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 1014 | else |
Rob Shearman | c9b9847 | 2007-02-15 23:21:40 +0000 | [diff] [blame] | 1015 | set_error( STATUS_NO_TOKEN ); |
Mike McCormack | 36cd6f5 | 2003-07-24 00:07:00 +0000 | [diff] [blame] | 1016 | release_object( process ); |
| 1017 | } |
| 1018 | } |
| 1019 | } |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 1020 | |
| 1021 | /* adjust the privileges held by a token */ |
| 1022 | DECL_HANDLER(adjust_token_privileges) |
| 1023 | { |
| 1024 | struct token *token; |
| 1025 | unsigned int access = TOKEN_ADJUST_PRIVILEGES; |
| 1026 | |
| 1027 | if (req->get_modified_state) access |= TOKEN_QUERY; |
| 1028 | |
| 1029 | if ((token = (struct token *)get_handle_obj( current->process, req->handle, |
| 1030 | access, &token_ops ))) |
| 1031 | { |
| 1032 | const LUID_AND_ATTRIBUTES *privs = get_req_data(); |
| 1033 | LUID_AND_ATTRIBUTES *modified_privs = NULL; |
| 1034 | unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES); |
| 1035 | unsigned int modified_priv_count = 0; |
| 1036 | |
| 1037 | if (req->get_modified_state && !req->disable_all) |
| 1038 | { |
Michael Stefaniuc | a624977 | 2006-07-25 10:30:04 +0200 | [diff] [blame] | 1039 | unsigned int i; |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 1040 | /* count modified privs */ |
| 1041 | for (i = 0; i < priv_count; i++) |
| 1042 | { |
| 1043 | struct privilege *privilege = |
| 1044 | token_find_privilege( token, &privs[i].Luid, FALSE ); |
| 1045 | if (privilege && req->get_modified_state) |
| 1046 | modified_priv_count++; |
| 1047 | } |
| 1048 | reply->len = modified_priv_count; |
| 1049 | modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) ); |
| 1050 | if (modified_priv_count) |
| 1051 | modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) ); |
| 1052 | } |
| 1053 | reply->len = modified_priv_count * sizeof(*modified_privs); |
| 1054 | |
| 1055 | if (req->disable_all) |
| 1056 | token_disable_privileges( token ); |
| 1057 | else |
| 1058 | modified_priv_count = token_adjust_privileges( token, privs, |
| 1059 | priv_count, modified_privs, modified_priv_count ); |
| 1060 | |
| 1061 | release_object( token ); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | /* retrieves the list of privileges that may be held be the token */ |
| 1066 | DECL_HANDLER(get_token_privileges) |
| 1067 | { |
| 1068 | struct token *token; |
| 1069 | |
| 1070 | if ((token = (struct token *)get_handle_obj( current->process, req->handle, |
| 1071 | TOKEN_QUERY, |
| 1072 | &token_ops ))) |
| 1073 | { |
| 1074 | int priv_count = 0; |
| 1075 | LUID_AND_ATTRIBUTES *privs; |
| 1076 | struct privilege *privilege; |
| 1077 | |
| 1078 | LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry ) |
| 1079 | priv_count++; |
| 1080 | |
| 1081 | reply->len = priv_count * sizeof(*privs); |
| 1082 | if (reply->len <= get_reply_max_size()) |
| 1083 | { |
| 1084 | privs = set_reply_data_size( priv_count * sizeof(*privs) ); |
| 1085 | if (privs) |
| 1086 | { |
| 1087 | int i = 0; |
| 1088 | LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry ) |
| 1089 | { |
| 1090 | luid_and_attr_from_privilege( &privs[i], privilege ); |
| 1091 | i++; |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | else |
| 1096 | set_error(STATUS_BUFFER_TOO_SMALL); |
| 1097 | |
| 1098 | release_object( token ); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | /* creates a duplicate of the token */ |
| 1103 | DECL_HANDLER(duplicate_token) |
| 1104 | { |
| 1105 | struct token *src_token; |
Rob Shearman | c9b9847 | 2007-02-15 23:21:40 +0000 | [diff] [blame] | 1106 | |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 1107 | if ((src_token = (struct token *)get_handle_obj( current->process, req->handle, |
| 1108 | TOKEN_DUPLICATE, |
| 1109 | &token_ops ))) |
| 1110 | { |
Rob Shearman | bdf964d | 2007-05-28 18:39:33 +0100 | [diff] [blame] | 1111 | struct token *token = token_duplicate( src_token, req->primary, req->impersonation_level ); |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 1112 | if (token) |
| 1113 | { |
Rob Shearman | bdf964d | 2007-05-28 18:39:33 +0100 | [diff] [blame] | 1114 | reply->new_handle = alloc_handle( current->process, token, req->access, req->attributes); |
Robert Shearman | b0f02b2 | 2005-02-11 11:52:06 +0000 | [diff] [blame] | 1115 | release_object( token ); |
| 1116 | } |
| 1117 | release_object( src_token ); |
| 1118 | } |
| 1119 | } |
Robert Shearman | d2ea92d | 2005-04-22 21:17:15 +0000 | [diff] [blame] | 1120 | |
| 1121 | /* checks the specified privileges are held by the token */ |
| 1122 | DECL_HANDLER(check_token_privileges) |
| 1123 | { |
| 1124 | struct token *token; |
| 1125 | |
| 1126 | if ((token = (struct token *)get_handle_obj( current->process, req->handle, |
| 1127 | TOKEN_QUERY, |
| 1128 | &token_ops ))) |
| 1129 | { |
| 1130 | unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES); |
Rob Shearman | c9b9847 | 2007-02-15 23:21:40 +0000 | [diff] [blame] | 1131 | |
| 1132 | if (!token->primary && token->impersonation_level <= SecurityAnonymous) |
| 1133 | set_error( STATUS_BAD_IMPERSONATION_LEVEL ); |
| 1134 | else if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES)) |
Robert Shearman | d2ea92d | 2005-04-22 21:17:15 +0000 | [diff] [blame] | 1135 | { |
| 1136 | LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) ); |
| 1137 | reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs ); |
| 1138 | } |
| 1139 | else |
| 1140 | set_error( STATUS_BUFFER_OVERFLOW ); |
| 1141 | release_object( token ); |
| 1142 | } |
| 1143 | } |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 1144 | |
| 1145 | /* checks that a user represented by a token is allowed to access an object |
| 1146 | * represented by a security descriptor */ |
| 1147 | DECL_HANDLER(access_check) |
| 1148 | { |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 1149 | data_size_t sd_size = get_req_data_size(); |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 1150 | const struct security_descriptor *sd = get_req_data(); |
| 1151 | struct token *token; |
| 1152 | |
| 1153 | if (!sd_is_valid( sd, sd_size )) |
| 1154 | { |
| 1155 | set_error( STATUS_ACCESS_VIOLATION ); |
| 1156 | return; |
| 1157 | } |
| 1158 | |
| 1159 | if ((token = (struct token *)get_handle_obj( current->process, req->handle, |
| 1160 | TOKEN_QUERY, |
| 1161 | &token_ops ))) |
| 1162 | { |
| 1163 | GENERIC_MAPPING mapping; |
| 1164 | unsigned int status; |
| 1165 | LUID_AND_ATTRIBUTES priv; |
| 1166 | unsigned int priv_count = 1; |
| 1167 | |
| 1168 | memset(&priv, 0, sizeof(priv)); |
| 1169 | |
Robert Shearman | 4bba216 | 2005-06-20 13:18:38 +0000 | [diff] [blame] | 1170 | /* only impersonation tokens may be used with this function */ |
| 1171 | if (token->primary) |
| 1172 | { |
| 1173 | set_error( STATUS_NO_IMPERSONATION_TOKEN ); |
| 1174 | release_object( token ); |
| 1175 | return; |
| 1176 | } |
Rob Shearman | c9b9847 | 2007-02-15 23:21:40 +0000 | [diff] [blame] | 1177 | /* anonymous impersonation tokens can't be used */ |
| 1178 | if (token->impersonation_level <= SecurityAnonymous) |
| 1179 | { |
| 1180 | set_error( STATUS_BAD_IMPERSONATION_LEVEL ); |
| 1181 | release_object( token ); |
| 1182 | return; |
| 1183 | } |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 1184 | |
| 1185 | mapping.GenericRead = req->mapping_read; |
| 1186 | mapping.GenericWrite = req->mapping_write; |
| 1187 | mapping.GenericExecute = req->mapping_execute; |
| 1188 | mapping.GenericAll = req->mapping_all; |
| 1189 | |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 1190 | status = token_access_check( |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 1191 | token, sd, req->desired_access, &priv, &priv_count, &mapping, |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 1192 | &reply->access_granted, &reply->access_status ); |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 1193 | |
| 1194 | reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES); |
| 1195 | |
| 1196 | if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size())) |
| 1197 | { |
| 1198 | LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) ); |
| 1199 | memcpy( privs, &priv, sizeof(priv) ); |
| 1200 | } |
| 1201 | |
Vitaliy Margolen | fad936c | 2007-01-24 23:42:15 -0700 | [diff] [blame] | 1202 | set_error( status ); |
Robert Shearman | 4ad9341 | 2005-05-24 12:32:18 +0000 | [diff] [blame] | 1203 | release_object( token ); |
| 1204 | } |
| 1205 | } |
Robert Shearman | 91eaea5 | 2005-07-18 13:22:55 +0000 | [diff] [blame] | 1206 | |
Francois Gouget | 11ae0f6 | 2006-06-15 17:38:04 +0200 | [diff] [blame] | 1207 | /* retrieves the SID of the user that the token represents */ |
Robert Shearman | 91eaea5 | 2005-07-18 13:22:55 +0000 | [diff] [blame] | 1208 | DECL_HANDLER(get_token_user) |
| 1209 | { |
| 1210 | struct token *token; |
| 1211 | |
| 1212 | reply->user_len = 0; |
| 1213 | |
| 1214 | if ((token = (struct token *)get_handle_obj( current->process, req->handle, |
| 1215 | TOKEN_QUERY, |
| 1216 | &token_ops ))) |
| 1217 | { |
| 1218 | const SID *user = token->user; |
| 1219 | |
| 1220 | reply->user_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]); |
| 1221 | if (reply->user_len <= get_reply_max_size()) |
| 1222 | { |
| 1223 | SID *user_reply = set_reply_data_size( reply->user_len ); |
| 1224 | if (user_reply) |
| 1225 | memcpy( user_reply, user, reply->user_len ); |
| 1226 | } |
| 1227 | else set_error( STATUS_BUFFER_TOO_SMALL ); |
| 1228 | |
| 1229 | release_object( token ); |
| 1230 | } |
| 1231 | } |
Robert Shearman | 3396a66 | 2006-05-13 16:58:19 +0100 | [diff] [blame] | 1232 | |
| 1233 | /* retrieves the groups that the user represented by the token belongs to */ |
| 1234 | DECL_HANDLER(get_token_groups) |
| 1235 | { |
| 1236 | struct token *token; |
| 1237 | |
| 1238 | reply->user_len = 0; |
| 1239 | |
| 1240 | if ((token = (struct token *)get_handle_obj( current->process, req->handle, |
| 1241 | TOKEN_QUERY, |
| 1242 | &token_ops ))) |
| 1243 | { |
| 1244 | size_t size_needed = sizeof(struct token_groups); |
| 1245 | unsigned int group_count = 0; |
| 1246 | const struct group *group; |
| 1247 | |
| 1248 | LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry ) |
| 1249 | { |
| 1250 | group_count++; |
| 1251 | size_needed += FIELD_OFFSET(SID, SubAuthority[group->sid.SubAuthorityCount]); |
| 1252 | } |
| 1253 | size_needed += sizeof(unsigned int) * group_count; |
| 1254 | |
| 1255 | reply->user_len = size_needed; |
| 1256 | |
| 1257 | if (size_needed <= get_reply_max_size()) |
| 1258 | { |
| 1259 | struct token_groups *tg = set_reply_data_size( size_needed ); |
| 1260 | if (tg) |
| 1261 | { |
| 1262 | unsigned int *attr_ptr = (unsigned int *)(tg + 1); |
| 1263 | SID *sid_ptr = (SID *)(attr_ptr + group_count); |
| 1264 | |
| 1265 | tg->count = group_count; |
| 1266 | |
| 1267 | LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry ) |
| 1268 | { |
| 1269 | |
| 1270 | *attr_ptr = 0; |
| 1271 | if (group->mandatory) *attr_ptr |= SE_GROUP_MANDATORY; |
| 1272 | if (group->def) *attr_ptr |= SE_GROUP_ENABLED_BY_DEFAULT; |
| 1273 | if (group->enabled) *attr_ptr |= SE_GROUP_ENABLED; |
| 1274 | if (group->owner) *attr_ptr |= SE_GROUP_OWNER; |
| 1275 | if (group->deny_only) *attr_ptr |= SE_GROUP_USE_FOR_DENY_ONLY; |
| 1276 | if (group->resource) *attr_ptr |= SE_GROUP_RESOURCE; |
| 1277 | |
| 1278 | memcpy(sid_ptr, &group->sid, FIELD_OFFSET(SID, SubAuthority[group->sid.SubAuthorityCount])); |
| 1279 | |
| 1280 | sid_ptr = (SID *)((char *)sid_ptr + FIELD_OFFSET(SID, SubAuthority[group->sid.SubAuthorityCount])); |
| 1281 | attr_ptr++; |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | else set_error( STATUS_BUFFER_TOO_SMALL ); |
| 1286 | |
| 1287 | release_object( token ); |
| 1288 | } |
| 1289 | } |
Vitaliy Margolen | b0e9d7e | 2007-02-07 21:43:11 -0700 | [diff] [blame] | 1290 | |
Rob Shearman | d342d14 | 2007-02-21 17:10:45 +0000 | [diff] [blame] | 1291 | DECL_HANDLER(get_token_impersonation_level) |
| 1292 | { |
| 1293 | struct token *token; |
| 1294 | |
| 1295 | if ((token = (struct token *)get_handle_obj( current->process, req->handle, |
| 1296 | TOKEN_QUERY, |
| 1297 | &token_ops ))) |
| 1298 | { |
| 1299 | if (token->primary) |
| 1300 | set_error( STATUS_INVALID_PARAMETER ); |
| 1301 | else |
| 1302 | reply->impersonation_level = token->impersonation_level; |
| 1303 | |
| 1304 | release_object( token ); |
| 1305 | } |
| 1306 | } |
| 1307 | |
Rob Shearman | 3f431a0 | 2007-09-13 16:47:56 +0100 | [diff] [blame] | 1308 | DECL_HANDLER(get_token_statistics) |
| 1309 | { |
| 1310 | struct token *token; |
| 1311 | |
| 1312 | if ((token = (struct token *)get_handle_obj( current->process, req->handle, |
| 1313 | TOKEN_QUERY, |
| 1314 | &token_ops ))) |
| 1315 | { |
| 1316 | reply->token_id = token->token_id; |
| 1317 | reply->modified_id = token->modified_id; |
| 1318 | reply->primary = token->primary; |
| 1319 | reply->impersonation_level = token->impersonation_level; |
| 1320 | reply->group_count = list_count( &token->groups ); |
| 1321 | reply->privilege_count = list_count( &token->privileges ); |
| 1322 | |
| 1323 | release_object( token ); |
| 1324 | } |
| 1325 | } |