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