2 * COPYRIGHT: See COPYING in the top level directory
4 * Copyright 1999, 2000 Juergen Schmied <juergen.schmied@debitel.net>
5 * Copyright 2003 CodeWeavers Inc. (Ulrich Czekalla)
6 * Copyright 2006 Robert Reif
7 * Copyright 2006 Hervé Poussineau
9 * PROJECT: ReactOS system libraries
10 * FILE: dll/win32/advapi32/wine/security.c
17 WINE_DEFAULT_DEBUG_CHANNEL(advapi
);
19 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
);
21 typedef struct _ACEFLAG
25 } ACEFLAG
, *LPACEFLAG
;
27 typedef struct _MAX_SID
29 /* same fields as struct _SID */
31 BYTE SubAuthorityCount
;
32 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
33 DWORD SubAuthority
[SID_MAX_SUB_AUTHORITIES
];
36 typedef struct WELLKNOWNSID
39 WELL_KNOWN_SID_TYPE Type
;
43 static const WELLKNOWNSID WellKnownSids
[] =
45 { {0,0}, WinNullSid
, { SID_REVISION
, 1, { SECURITY_NULL_SID_AUTHORITY
}, { SECURITY_NULL_RID
} } },
46 { {'W','D'}, WinWorldSid
, { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} } },
47 { {0,0}, WinLocalSid
, { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} } },
48 { {'C','O'}, WinCreatorOwnerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_RID
} } },
49 { {'C','G'}, WinCreatorGroupSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_RID
} } },
50 { {0,0}, WinCreatorOwnerServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_SERVER_RID
} } },
51 { {0,0}, WinCreatorGroupServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_SERVER_RID
} } },
52 { {0,0}, WinNtAuthoritySid
, { SID_REVISION
, 0, { SECURITY_NT_AUTHORITY
}, { SECURITY_NULL_RID
} } },
53 { {0,0}, WinDialupSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_DIALUP_RID
} } },
54 { {'N','U'}, WinNetworkSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_RID
} } },
55 { {0,0}, WinBatchSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BATCH_RID
} } },
56 { {'I','U'}, WinInteractiveSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} } },
57 { {'S','U'}, WinServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_SERVICE_RID
} } },
58 { {'A','N'}, WinAnonymousSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} } },
59 { {0,0}, WinProxySid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PROXY_RID
} } },
60 { {'E','D'}, WinEnterpriseControllersSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ENTERPRISE_CONTROLLERS_RID
} } },
61 { {'P','S'}, WinSelfSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PRINCIPAL_SELF_RID
} } },
62 { {'A','U'}, WinAuthenticatedUserSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} } },
63 { {'R','C'}, WinRestrictedCodeSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_RESTRICTED_CODE_RID
} } },
64 { {0,0}, WinTerminalServerSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_TERMINAL_SERVER_RID
} } },
65 { {0,0}, WinRemoteLogonIdSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_REMOTE_LOGON_RID
} } },
66 { {'S','Y'}, WinLocalSystemSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} } },
67 { {'L','S'}, WinLocalServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SERVICE_RID
} } },
68 { {'N','S'}, WinNetworkServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_SERVICE_RID
} } },
69 { {0,0}, WinBuiltinDomainSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
} } },
70 { {'B','A'}, WinBuiltinAdministratorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} } },
71 { {'B','U'}, WinBuiltinUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} } },
72 { {'B','G'}, WinBuiltinGuestsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_GUESTS
} } },
73 { {'P','U'}, WinBuiltinPowerUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_POWER_USERS
} } },
74 { {'A','O'}, WinBuiltinAccountOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ACCOUNT_OPS
} } },
75 { {'S','O'}, WinBuiltinSystemOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_SYSTEM_OPS
} } },
76 { {'P','O'}, WinBuiltinPrintOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PRINT_OPS
} } },
77 { {'B','O'}, WinBuiltinBackupOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_BACKUP_OPS
} } },
78 { {'R','E'}, WinBuiltinReplicatorSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REPLICATOR
} } },
79 { {'R','U'}, WinBuiltinPreWindows2000CompatibleAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PREW2KCOMPACCESS
} } },
80 { {'R','D'}, WinBuiltinRemoteDesktopUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS
} } },
81 { {'N','O'}, WinBuiltinNetworkConfigurationOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS
} } },
82 { {0,0}, WinNTLMAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_NTLM_RID
} } },
83 { {0,0}, WinDigestAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_DIGEST_RID
} } },
84 { {0,0}, WinSChannelAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_SCHANNEL_RID
} } },
85 { {0,0}, WinThisOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_THIS_ORGANIZATION_RID
} } },
86 { {0,0}, WinOtherOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_OTHER_ORGANIZATION_RID
} } },
87 { {0,0}, WinBuiltinIncomingForestTrustBuildersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS
} } },
88 { {0,0}, WinBuiltinPerfMonitoringUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_MONITORING_USERS
} } },
89 { {0,0}, WinBuiltinPerfLoggingUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_LOGGING_USERS
} } },
90 { {0,0}, WinBuiltinAuthorizationAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS
} } },
91 { {0,0}, WinBuiltinTerminalServerLicenseServersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS
} } },
92 { {0,0}, WinBuiltinDCOMUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_DCOM_USERS
} } },
93 { {'L','W'}, WinLowLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_LOW_RID
} } },
94 { {'M','E'}, WinMediumLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_MEDIUM_RID
} } },
95 { {'H','I'}, WinHighLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_HIGH_RID
} } },
96 { {'S','I'}, WinSystemLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_SYSTEM_RID
} } },
99 /* these SIDs must be constructed as relative to some domain - only the RID is well-known */
100 typedef struct WELLKNOWNRID
103 WELL_KNOWN_SID_TYPE Type
;
107 static const WELLKNOWNRID WellKnownRids
[] = {
108 { {'L','A'}, WinAccountAdministratorSid
, DOMAIN_USER_RID_ADMIN
},
109 { {'L','G'}, WinAccountGuestSid
, DOMAIN_USER_RID_GUEST
},
110 { {0,0}, WinAccountKrbtgtSid
, DOMAIN_USER_RID_KRBTGT
},
111 { {'D','A'}, WinAccountDomainAdminsSid
, DOMAIN_GROUP_RID_ADMINS
},
112 { {'D','U'}, WinAccountDomainUsersSid
, DOMAIN_GROUP_RID_USERS
},
113 { {'D','G'}, WinAccountDomainGuestsSid
, DOMAIN_GROUP_RID_GUESTS
},
114 { {'D','C'}, WinAccountComputersSid
, DOMAIN_GROUP_RID_COMPUTERS
},
115 { {'D','D'}, WinAccountControllersSid
, DOMAIN_GROUP_RID_CONTROLLERS
},
116 { {'C','A'}, WinAccountCertAdminsSid
, DOMAIN_GROUP_RID_CERT_ADMINS
},
117 { {'S','A'}, WinAccountSchemaAdminsSid
, DOMAIN_GROUP_RID_SCHEMA_ADMINS
},
118 { {'E','A'}, WinAccountEnterpriseAdminsSid
, DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
},
119 { {'P','A'}, WinAccountPolicyAdminsSid
, DOMAIN_GROUP_RID_POLICY_ADMINS
},
120 { {'R','S'}, WinAccountRasAndIasServersSid
, DOMAIN_ALIAS_RID_RAS_SERVERS
},
123 static const SID sidWorld
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
} , { SECURITY_WORLD_RID
} };
128 static const WCHAR SDDL_ACCESS_ALLOWED
[] = {'A',0};
129 static const WCHAR SDDL_ACCESS_DENIED
[] = {'D',0};
130 static const WCHAR SDDL_OBJECT_ACCESS_ALLOWED
[] = {'O','A',0};
131 static const WCHAR SDDL_OBJECT_ACCESS_DENIED
[] = {'O','D',0};
132 static const WCHAR SDDL_AUDIT
[] = {'A','U',0};
133 static const WCHAR SDDL_ALARM
[] = {'A','L',0};
134 static const WCHAR SDDL_OBJECT_AUDIT
[] = {'O','U',0};
135 static const WCHAR SDDL_OBJECT_ALARM
[] = {'O','L',0};
140 #define ADS_RIGHT_DS_CREATE_CHILD 0x0001
141 #define ADS_RIGHT_DS_DELETE_CHILD 0x0002
142 #define ADS_RIGHT_ACTRL_DS_LIST 0x0004
143 #define ADS_RIGHT_DS_SELF 0x0008
144 #define ADS_RIGHT_DS_READ_PROP 0x0010
145 #define ADS_RIGHT_DS_WRITE_PROP 0x0020
146 #define ADS_RIGHT_DS_DELETE_TREE 0x0040
147 #define ADS_RIGHT_DS_LIST_OBJECT 0x0080
148 #define ADS_RIGHT_DS_CONTROL_ACCESS 0x0100
153 static const WCHAR SDDL_CONTAINER_INHERIT
[] = {'C','I',0};
154 static const WCHAR SDDL_OBJECT_INHERIT
[] = {'O','I',0};
155 static const WCHAR SDDL_NO_PROPAGATE
[] = {'N','P',0};
156 static const WCHAR SDDL_INHERIT_ONLY
[] = {'I','O',0};
157 static const WCHAR SDDL_INHERITED
[] = {'I','D',0};
158 static const WCHAR SDDL_AUDIT_SUCCESS
[] = {'S','A',0};
159 static const WCHAR SDDL_AUDIT_FAILURE
[] = {'F','A',0};
161 static const char * debugstr_sid(PSID sid
)
164 SID
* psid
= (SID
*)sid
;
169 auth
= psid
->IdentifierAuthority
.Value
[5] +
170 (psid
->IdentifierAuthority
.Value
[4] << 8) +
171 (psid
->IdentifierAuthority
.Value
[3] << 16) +
172 (psid
->IdentifierAuthority
.Value
[2] << 24);
174 switch (psid
->SubAuthorityCount
) {
176 return wine_dbg_sprintf("S-%d-%d", psid
->Revision
, auth
);
178 return wine_dbg_sprintf("S-%d-%d-%lu", psid
->Revision
, auth
,
179 psid
->SubAuthority
[0]);
181 return wine_dbg_sprintf("S-%d-%d-%lu-%lu", psid
->Revision
, auth
,
182 psid
->SubAuthority
[0], psid
->SubAuthority
[1]);
184 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu", psid
->Revision
, auth
,
185 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2]);
187 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
188 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
189 psid
->SubAuthority
[3]);
191 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
192 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
193 psid
->SubAuthority
[3], psid
->SubAuthority
[4]);
195 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
196 psid
->SubAuthority
[3], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
197 psid
->SubAuthority
[0], psid
->SubAuthority
[4], psid
->SubAuthority
[5]);
199 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
200 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
201 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
202 psid
->SubAuthority
[6]);
204 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
205 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
206 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
207 psid
->SubAuthority
[6], psid
->SubAuthority
[7]);
212 /* set last error code from NT status and get the proper boolean return value */
213 /* used for functions that are a simple wrapper around the corresponding ntdll API */
214 static __inline BOOL
set_ntstatus( NTSTATUS status
)
216 if (!NT_SUCCESS(status
)) SetLastError( RtlNtStatusToDosError( status
));
217 return NT_SUCCESS(status
);
220 static LPWSTR
SERV_dup( LPCSTR str
)
227 len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
228 wstr
= heap_alloc( len
*sizeof (WCHAR
) );
229 MultiByteToWideChar( CP_ACP
, 0, str
, -1, wstr
, len
);
233 /************************************************************
234 * ADVAPI_IsLocalComputer
236 * Checks whether the server name indicates local machine.
238 BOOL
ADVAPI_IsLocalComputer(LPCWSTR ServerName
)
240 DWORD dwSize
= MAX_COMPUTERNAME_LENGTH
+ 1;
244 if (!ServerName
|| !ServerName
[0])
247 buf
= heap_alloc(dwSize
* sizeof(WCHAR
));
248 Result
= GetComputerNameW(buf
, &dwSize
);
249 if (Result
&& (ServerName
[0] == '\\') && (ServerName
[1] == '\\'))
251 Result
= Result
&& !lstrcmpW(ServerName
, buf
);
257 /************************************************************
258 * ADVAPI_GetComputerSid
260 BOOL
ADVAPI_GetComputerSid(PSID sid
)
262 static const struct /* same fields as struct SID */
265 BYTE SubAuthorityCount
;
266 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
267 DWORD SubAuthority
[4];
269 { SID_REVISION
, 4, { SECURITY_NT_AUTHORITY
}, { SECURITY_NT_NON_UNIQUE
, 0, 0, 0 } };
271 memcpy( sid
, &computer_sid
, sizeof(computer_sid
) );
275 /* Exported functions */
281 OpenProcessToken(HANDLE ProcessHandle
,
287 TRACE("%p, %x, %p.\n", ProcessHandle
, DesiredAccess
, TokenHandle
);
289 Status
= NtOpenProcessToken(ProcessHandle
,
292 if (!NT_SUCCESS(Status
))
294 ERR("NtOpenProcessToken failed! Status %08x.\n", Status
);
295 SetLastError(RtlNtStatusToDosError(Status
));
299 TRACE("Returning token %p.\n", *TokenHandle
);
304 /******************************************************************************
305 * OpenThreadToken [ADVAPI32.@]
307 * Opens the access token associated with a thread handle.
310 * ThreadHandle [I] Handle to process
311 * DesiredAccess [I] Desired access to the thread
313 * TokenHandle [O] Destination for the token handle
316 * Success: TRUE. TokenHandle contains the access token.
320 * See NtOpenThreadToken.
323 OpenThreadToken( HANDLE ThreadHandle
, DWORD DesiredAccess
,
324 BOOL OpenAsSelf
, HANDLE
*TokenHandle
)
326 return set_ntstatus( NtOpenThreadToken(ThreadHandle
, DesiredAccess
, OpenAsSelf
, TokenHandle
));
333 AdjustTokenGroups(HANDLE TokenHandle
,
335 PTOKEN_GROUPS NewState
,
337 PTOKEN_GROUPS PreviousState
,
342 Status
= NtAdjustGroupsToken(TokenHandle
,
347 (PULONG
)ReturnLength
);
348 if (!NT_SUCCESS(Status
))
350 SetLastError(RtlNtStatusToDosError(Status
));
361 AdjustTokenPrivileges(HANDLE TokenHandle
,
362 BOOL DisableAllPrivileges
,
363 PTOKEN_PRIVILEGES NewState
,
365 PTOKEN_PRIVILEGES PreviousState
,
370 Status
= NtAdjustPrivilegesToken(TokenHandle
,
371 DisableAllPrivileges
,
375 (PULONG
)ReturnLength
);
376 if (STATUS_NOT_ALL_ASSIGNED
== Status
)
378 SetLastError(ERROR_NOT_ALL_ASSIGNED
);
382 if (!NT_SUCCESS(Status
))
384 SetLastError(RtlNtStatusToDosError(Status
));
388 /* AdjustTokenPrivileges is documented to do this */
389 SetLastError(ERROR_SUCCESS
);
398 GetTokenInformation(HANDLE TokenHandle
,
399 TOKEN_INFORMATION_CLASS TokenInformationClass
,
400 LPVOID TokenInformation
,
401 DWORD TokenInformationLength
,
406 Status
= NtQueryInformationToken(TokenHandle
,
407 TokenInformationClass
,
409 TokenInformationLength
,
410 (PULONG
)ReturnLength
);
411 if (!NT_SUCCESS(Status
))
413 SetLastError(RtlNtStatusToDosError(Status
));
424 SetTokenInformation(HANDLE TokenHandle
,
425 TOKEN_INFORMATION_CLASS TokenInformationClass
,
426 LPVOID TokenInformation
,
427 DWORD TokenInformationLength
)
431 Status
= NtSetInformationToken(TokenHandle
,
432 TokenInformationClass
,
434 TokenInformationLength
);
435 if (!NT_SUCCESS(Status
))
437 SetLastError(RtlNtStatusToDosError(Status
));
448 SetThreadToken(IN PHANDLE ThreadHandle OPTIONAL
,
449 IN HANDLE TokenHandle
)
454 hThread
= (ThreadHandle
!= NULL
) ? *ThreadHandle
: NtCurrentThread();
456 Status
= NtSetInformationThread(hThread
,
457 ThreadImpersonationToken
,
460 if (!NT_SUCCESS(Status
))
462 SetLastError(RtlNtStatusToDosError(Status
));
469 /*************************************************************************
470 * CreateRestrictedToken [ADVAPI32.@]
472 * Create a new more restricted token from an existing token.
475 * baseToken [I] Token to base the new restricted token on
477 * nDisableSids [I] Length of disableSids array
478 * disableSids [I] Array of SIDs to disable in the new token
479 * nDeletePrivs [I] Length of deletePrivs array
480 * deletePrivs [I] Array of privileges to delete in the new token
481 * nRestrictSids [I] Length of restrictSids array
482 * restrictSids [I] Array of SIDs to restrict in the new token
483 * newToken [O] Address where the new token is stored
489 BOOL WINAPI
CreateRestrictedToken(
493 PSID_AND_ATTRIBUTES disableSids
,
495 PLUID_AND_ATTRIBUTES deletePrivs
,
497 PSID_AND_ATTRIBUTES restrictSids
,
501 SECURITY_IMPERSONATION_LEVEL level
= TokenImpersonationLevel
;
504 FIXME("(%p, 0x%x, %u, %p, %u, %p, %u, %p, %p): stub\n",
505 baseToken
, flags
, nDisableSids
, disableSids
,
506 nDeletePrivs
, deletePrivs
,
507 nRestrictSids
, restrictSids
,
511 if (!GetTokenInformation( baseToken
, TokenType
, &type
, size
, &size
)) return FALSE
;
512 if (type
== TokenImpersonation
)
514 size
= sizeof(level
);
515 if (!GetTokenInformation( baseToken
, TokenImpersonationLevel
, &level
, size
, &size
))
518 return DuplicateTokenEx( baseToken
, MAXIMUM_ALLOWED
, NULL
, level
, type
, newToken
);
521 /******************************************************************************
522 * AllocateAndInitializeSid [ADVAPI32.@]
525 * pIdentifierAuthority []
526 * nSubAuthorityCount []
538 AllocateAndInitializeSid( PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
539 BYTE nSubAuthorityCount
,
540 DWORD nSubAuthority0
, DWORD nSubAuthority1
,
541 DWORD nSubAuthority2
, DWORD nSubAuthority3
,
542 DWORD nSubAuthority4
, DWORD nSubAuthority5
,
543 DWORD nSubAuthority6
, DWORD nSubAuthority7
,
546 return set_ntstatus( RtlAllocateAndInitializeSid(
547 pIdentifierAuthority
, nSubAuthorityCount
,
548 nSubAuthority0
, nSubAuthority1
, nSubAuthority2
, nSubAuthority3
,
549 nSubAuthority4
, nSubAuthority5
, nSubAuthority6
, nSubAuthority7
,
557 * Docs says this function does NOT return a value
558 * even thou it's defined to return a PVOID...
564 return RtlFreeSid(pSid
);
567 /******************************************************************************
568 * CopySid [ADVAPI32.@]
571 * nDestinationSidLength []
576 CopySid( DWORD nDestinationSidLength
, PSID pDestinationSid
, PSID pSourceSid
)
578 return set_ntstatus(RtlCopySid(nDestinationSidLength
, pDestinationSid
, pSourceSid
));
586 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
587 IN PSID DomainSid OPTIONAL
,
592 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
594 if (cbSid
== NULL
|| (DomainSid
&& !IsValidSid(DomainSid
)))
596 SetLastError(ERROR_INVALID_PARAMETER
);
600 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++) {
601 if (WellKnownSids
[i
].Type
== WellKnownSidType
) {
602 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
607 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
612 SetLastError(ERROR_INVALID_PARAMETER
);
615 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
621 if (DomainSid
== NULL
|| *GetSidSubAuthorityCount(DomainSid
) == SID_MAX_SUB_AUTHORITIES
)
623 SetLastError(ERROR_INVALID_PARAMETER
);
627 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
628 if (WellKnownRids
[i
].Type
== WellKnownSidType
) {
629 UCHAR domain_subauth
= *GetSidSubAuthorityCount(DomainSid
);
630 DWORD domain_sid_length
= GetSidLengthRequired(domain_subauth
);
631 DWORD output_sid_length
= GetSidLengthRequired(domain_subauth
+ 1);
633 if (*cbSid
< output_sid_length
)
635 *cbSid
= output_sid_length
;
636 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
641 SetLastError(ERROR_INVALID_PARAMETER
);
644 CopyMemory(pSid
, DomainSid
, domain_sid_length
);
645 (*GetSidSubAuthorityCount(pSid
))++;
646 (*GetSidSubAuthority(pSid
, domain_subauth
)) = WellKnownRids
[i
].Rid
;
647 *cbSid
= output_sid_length
;
651 SetLastError(ERROR_INVALID_PARAMETER
);
660 IsWellKnownSid(IN PSID pSid
,
661 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
664 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
666 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
668 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
670 if (EqualSid(pSid
, (PSID
)(&WellKnownSids
[i
].Sid
.Revision
)))
683 IsValidSid(PSID pSid
)
685 return (BOOL
)RtlValidSid(pSid
);
696 SetLastError(ERROR_SUCCESS
);
697 return RtlEqualSid (pSid1
, pSid2
);
705 EqualPrefixSid(PSID pSid1
,
708 return RtlEqualPrefixSid (pSid1
, pSid2
);
716 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
718 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
726 InitializeSid(PSID Sid
,
727 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
728 BYTE nSubAuthorityCount
)
732 Status
= RtlInitializeSid(Sid
,
733 pIdentifierAuthority
,
735 if (!NT_SUCCESS(Status
))
737 SetLastError(RtlNtStatusToDosError(Status
));
747 PSID_IDENTIFIER_AUTHORITY
749 GetSidIdentifierAuthority(PSID pSid
)
751 return RtlIdentifierAuthoritySid(pSid
);
759 GetSidSubAuthority(PSID pSid
,
762 SetLastError(ERROR_SUCCESS
);
763 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
771 GetSidSubAuthorityCount(PSID pSid
)
773 SetLastError(ERROR_SUCCESS
);
774 return RtlSubAuthorityCountSid(pSid
);
782 GetLengthSid(PSID pSid
)
784 return (DWORD
)RtlLengthSid(pSid
);
792 InitializeSecurityDescriptor(PSECURITY_DESCRIPTOR pSecurityDescriptor
,
797 Status
= RtlCreateSecurityDescriptor(pSecurityDescriptor
,
799 if (!NT_SUCCESS(Status
))
801 SetLastError(RtlNtStatusToDosError(Status
));
813 MakeAbsoluteSD(PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor
,
814 PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor
,
815 LPDWORD lpdwAbsoluteSecurityDescriptorSize
,
817 LPDWORD lpdwDaclSize
,
819 LPDWORD lpdwSaclSize
,
821 LPDWORD lpdwOwnerSize
,
823 LPDWORD lpdwPrimaryGroupSize
)
827 Status
= RtlSelfRelativeToAbsoluteSD(pSelfRelativeSecurityDescriptor
,
828 pAbsoluteSecurityDescriptor
,
829 lpdwAbsoluteSecurityDescriptorSize
,
837 lpdwPrimaryGroupSize
);
838 if (!NT_SUCCESS(Status
))
840 SetLastError(RtlNtStatusToDosError(Status
));
847 /******************************************************************************
848 * GetKernelObjectSecurity [ADVAPI32.@]
850 BOOL WINAPI
GetKernelObjectSecurity(
852 SECURITY_INFORMATION RequestedInformation
,
853 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
855 LPDWORD lpnLengthNeeded
)
857 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n", Handle
, RequestedInformation
,
858 pSecurityDescriptor
, nLength
, lpnLengthNeeded
);
860 return set_ntstatus( NtQuerySecurityObject(Handle
, RequestedInformation
, pSecurityDescriptor
,
861 nLength
, lpnLengthNeeded
));
869 InitializeAcl(PACL pAcl
,
875 Status
= RtlCreateAcl(pAcl
,
878 if (!NT_SUCCESS(Status
))
880 SetLastError(RtlNtStatusToDosError(Status
));
887 BOOL WINAPI
ImpersonateNamedPipeClient( HANDLE hNamedPipe
)
889 IO_STATUS_BLOCK io_block
;
891 TRACE("(%p)\n", hNamedPipe
);
893 return set_ntstatus( NtFsControlFile(hNamedPipe
, NULL
, NULL
, NULL
,
894 &io_block
, FSCTL_PIPE_IMPERSONATE
, NULL
, 0, NULL
, 0) );
902 AddAccessAllowedAce(PACL pAcl
,
909 Status
= RtlAddAccessAllowedAce(pAcl
,
913 if (!NT_SUCCESS(Status
))
915 SetLastError(RtlNtStatusToDosError(Status
));
926 AddAccessAllowedAceEx(PACL pAcl
,
934 Status
= RtlAddAccessAllowedAceEx(pAcl
,
939 if (!NT_SUCCESS(Status
))
941 SetLastError(RtlNtStatusToDosError(Status
));
953 AddAccessDeniedAce(PACL pAcl
,
960 Status
= RtlAddAccessDeniedAce(pAcl
,
964 if (!NT_SUCCESS(Status
))
966 SetLastError(RtlNtStatusToDosError(Status
));
977 AddAccessDeniedAceEx(PACL pAcl
,
985 Status
= RtlAddAccessDeniedAceEx(pAcl
,
990 if (!NT_SUCCESS(Status
))
992 SetLastError(RtlNtStatusToDosError(Status
));
1005 DWORD dwAceRevision
,
1006 DWORD dwStartingAceIndex
,
1008 DWORD nAceListLength
)
1012 Status
= RtlAddAce(pAcl
,
1017 if (!NT_SUCCESS(Status
))
1019 SetLastError(RtlNtStatusToDosError(Status
));
1026 /******************************************************************************
1027 * DeleteAce [ADVAPI32.@]
1029 BOOL WINAPI
DeleteAce(PACL pAcl
, DWORD dwAceIndex
)
1031 return set_ntstatus(RtlDeleteAce(pAcl
, dwAceIndex
));
1039 FindFirstFreeAce(PACL pAcl
,
1042 return RtlFirstFreeAce(pAcl
,
1046 /******************************************************************************
1047 * GetAce [ADVAPI32.@]
1049 BOOL WINAPI
GetAce(PACL pAcl
,DWORD dwAceIndex
,LPVOID
*pAce
)
1051 return set_ntstatus(RtlGetAce(pAcl
, dwAceIndex
, pAce
));
1054 /******************************************************************************
1055 * GetAclInformation [ADVAPI32.@]
1057 BOOL WINAPI
GetAclInformation(
1059 LPVOID pAclInformation
,
1060 DWORD nAclInformationLength
,
1061 ACL_INFORMATION_CLASS dwAclInformationClass
)
1063 return set_ntstatus(RtlQueryInformationAcl(pAcl
, pAclInformation
,
1064 nAclInformationLength
, dwAclInformationClass
));
1072 IsValidAcl(PACL pAcl
)
1074 return RtlValidAcl (pAcl
);
1081 AllocateLocallyUniqueId(PLUID Luid
)
1085 Status
= NtAllocateLocallyUniqueId (Luid
);
1086 if (!NT_SUCCESS (Status
))
1088 SetLastError(RtlNtStatusToDosError(Status
));
1095 /**********************************************************************
1096 * LookupPrivilegeDisplayNameA EXPORTED
1102 LookupPrivilegeDisplayNameA(LPCSTR lpSystemName
,
1104 LPSTR lpDisplayName
,
1105 LPDWORD cchDisplayName
,
1106 LPDWORD lpLanguageId
)
1108 UNICODE_STRING lpSystemNameW
;
1109 UNICODE_STRING lpNameW
;
1113 TRACE("%s %s %p %p %p\n", debugstr_a(lpSystemName
), debugstr_a(lpName
), lpName
, cchDisplayName
, lpLanguageId
);
1115 RtlCreateUnicodeStringFromAsciiz(&lpSystemNameW
, lpSystemName
);
1116 RtlCreateUnicodeStringFromAsciiz(&lpNameW
, lpName
);
1117 ret
= LookupPrivilegeDisplayNameW(lpSystemNameW
.Buffer
, lpNameW
.Buffer
, NULL
, &wLen
, lpLanguageId
);
1118 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1120 LPWSTR lpDisplayNameW
= HeapAlloc(GetProcessHeap(), 0, wLen
* sizeof(WCHAR
));
1122 ret
= LookupPrivilegeDisplayNameW(lpSystemNameW
.Buffer
, lpNameW
.Buffer
, lpDisplayNameW
,
1123 &wLen
, lpLanguageId
);
1126 unsigned int len
= WideCharToMultiByte(CP_ACP
, 0, lpDisplayNameW
, -1, lpDisplayName
,
1127 *cchDisplayName
, NULL
, NULL
);
1131 /* WideCharToMultiByte failed */
1134 else if (len
> *cchDisplayName
)
1136 *cchDisplayName
= len
;
1137 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1142 /* WideCharToMultiByte succeeded, output length needs to be
1143 * length not including NULL terminator
1145 *cchDisplayName
= len
- 1;
1148 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
1150 RtlFreeUnicodeString(&lpSystemNameW
);
1151 RtlFreeUnicodeString(&lpNameW
);
1155 /**********************************************************************
1156 * LookupPrivilegeNameA EXPORTED
1162 LookupPrivilegeNameA(LPCSTR lpSystemName
,
1167 UNICODE_STRING lpSystemNameW
;
1171 TRACE("%s %p %p %p\n", debugstr_a(lpSystemName
), lpLuid
, lpName
, cchName
);
1173 RtlCreateUnicodeStringFromAsciiz(&lpSystemNameW
, lpSystemName
);
1174 ret
= LookupPrivilegeNameW(lpSystemNameW
.Buffer
, lpLuid
, NULL
, &wLen
);
1175 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1177 LPWSTR lpNameW
= HeapAlloc(GetProcessHeap(), 0, wLen
* sizeof(WCHAR
));
1179 ret
= LookupPrivilegeNameW(lpSystemNameW
.Buffer
, lpLuid
, lpNameW
,
1183 /* Windows crashes if cchName is NULL, so will I */
1184 unsigned int len
= WideCharToMultiByte(CP_ACP
, 0, lpNameW
, -1, lpName
,
1185 *cchName
, NULL
, NULL
);
1189 /* WideCharToMultiByte failed */
1192 else if (len
> *cchName
)
1195 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1200 /* WideCharToMultiByte succeeded, output length needs to be
1201 * length not including NULL terminator
1206 HeapFree(GetProcessHeap(), 0, lpNameW
);
1208 RtlFreeUnicodeString(&lpSystemNameW
);
1212 /******************************************************************************
1213 * GetFileSecurityA [ADVAPI32.@]
1215 * Obtains Specified information about the security of a file or directory.
1218 * lpFileName [I] Name of the file to get info for
1219 * RequestedInformation [I] SE_ flags from "winnt.h"
1220 * pSecurityDescriptor [O] Destination for security information
1221 * nLength [I] Length of pSecurityDescriptor
1222 * lpnLengthNeeded [O] Destination for length of returned security information
1225 * Success: TRUE. pSecurityDescriptor contains the requested information.
1226 * Failure: FALSE. lpnLengthNeeded contains the required space to return the info.
1229 * The information returned is constrained by the callers access rights and
1236 GetFileSecurityA(LPCSTR lpFileName
,
1237 SECURITY_INFORMATION RequestedInformation
,
1238 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1240 LPDWORD lpnLengthNeeded
)
1242 UNICODE_STRING FileName
;
1245 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
1247 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1251 bResult
= GetFileSecurityW(FileName
.Buffer
,
1252 RequestedInformation
,
1253 pSecurityDescriptor
,
1257 RtlFreeUnicodeString(&FileName
);
1267 GetFileSecurityW(LPCWSTR lpFileName
,
1268 SECURITY_INFORMATION RequestedInformation
,
1269 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1271 LPDWORD lpnLengthNeeded
)
1273 OBJECT_ATTRIBUTES ObjectAttributes
;
1274 IO_STATUS_BLOCK StatusBlock
;
1275 UNICODE_STRING FileName
;
1276 ULONG AccessMask
= 0;
1280 TRACE("GetFileSecurityW() called\n");
1282 QuerySecurityAccessMask(RequestedInformation
, &AccessMask
);
1284 if (!RtlDosPathNameToNtPathName_U(lpFileName
,
1289 ERR("Invalid path\n");
1290 SetLastError(ERROR_INVALID_NAME
);
1294 InitializeObjectAttributes(&ObjectAttributes
,
1296 OBJ_CASE_INSENSITIVE
,
1300 Status
= NtOpenFile(&FileHandle
,
1304 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1307 RtlFreeHeap(RtlGetProcessHeap(),
1311 if (!NT_SUCCESS(Status
))
1313 ERR("NtOpenFile() failed (Status %lx)\n", Status
);
1314 SetLastError(RtlNtStatusToDosError(Status
));
1318 Status
= NtQuerySecurityObject(FileHandle
,
1319 RequestedInformation
,
1320 pSecurityDescriptor
,
1323 NtClose(FileHandle
);
1324 if (!NT_SUCCESS(Status
))
1326 ERR("NtQuerySecurityObject() failed (Status %lx)\n", Status
);
1327 SetLastError(RtlNtStatusToDosError(Status
));
1334 /******************************************************************************
1335 * SetFileSecurityA [ADVAPI32.@]
1336 * Sets the security of a file or directory
1342 SetFileSecurityA(LPCSTR lpFileName
,
1343 SECURITY_INFORMATION SecurityInformation
,
1344 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
1346 UNICODE_STRING FileName
;
1349 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
1351 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1355 bResult
= SetFileSecurityW(FileName
.Buffer
,
1356 SecurityInformation
,
1357 pSecurityDescriptor
);
1359 RtlFreeUnicodeString(&FileName
);
1364 /******************************************************************************
1365 * SetFileSecurityW [ADVAPI32.@]
1366 * Sets the security of a file or directory
1372 SetFileSecurityW(LPCWSTR lpFileName
,
1373 SECURITY_INFORMATION SecurityInformation
,
1374 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
1376 OBJECT_ATTRIBUTES ObjectAttributes
;
1377 IO_STATUS_BLOCK StatusBlock
;
1378 UNICODE_STRING FileName
;
1379 ULONG AccessMask
= 0;
1383 TRACE("SetFileSecurityW() called\n");
1385 SetSecurityAccessMask(SecurityInformation
, &AccessMask
);
1387 if (!RtlDosPathNameToNtPathName_U(lpFileName
,
1392 ERR("Invalid path\n");
1393 SetLastError(ERROR_INVALID_NAME
);
1397 InitializeObjectAttributes(&ObjectAttributes
,
1399 OBJ_CASE_INSENSITIVE
,
1403 Status
= NtOpenFile(&FileHandle
,
1407 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1410 RtlFreeHeap(RtlGetProcessHeap(),
1414 if (!NT_SUCCESS(Status
))
1416 ERR("NtOpenFile() failed (Status %lx)\n", Status
);
1417 SetLastError(RtlNtStatusToDosError(Status
));
1421 Status
= NtSetSecurityObject(FileHandle
,
1422 SecurityInformation
,
1423 pSecurityDescriptor
);
1424 NtClose(FileHandle
);
1426 if (!NT_SUCCESS(Status
))
1428 ERR("NtSetSecurityObject() failed (Status %lx)\n", Status
);
1429 SetLastError(RtlNtStatusToDosError(Status
));
1436 /******************************************************************************
1437 * QueryWindows31FilesMigration [ADVAPI32.@]
1443 QueryWindows31FilesMigration( DWORD x1
)
1445 FIXME("(%d):stub\n",x1
);
1449 /******************************************************************************
1450 * SynchronizeWindows31FilesAndWindowsNTRegistry [ADVAPI32.@]
1459 SynchronizeWindows31FilesAndWindowsNTRegistry( DWORD x1
, DWORD x2
, DWORD x3
,
1462 FIXME("(0x%08x,0x%08x,0x%08x,0x%08x):stub\n",x1
,x2
,x3
,x4
);
1474 HANDLE Token
= NULL
;
1476 Status
= NtSetInformationThread(NtCurrentThread(),
1477 ThreadImpersonationToken
,
1480 if (!NT_SUCCESS(Status
))
1482 SetLastError(RtlNtStatusToDosError(Status
));
1494 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
1498 Status
= RtlImpersonateSelf(ImpersonationLevel
);
1499 if (!NT_SUCCESS(Status
))
1501 SetLastError(RtlNtStatusToDosError(Status
));
1513 AccessCheck(IN PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1514 IN HANDLE ClientToken
,
1515 IN DWORD DesiredAccess
,
1516 IN PGENERIC_MAPPING GenericMapping
,
1517 OUT PPRIVILEGE_SET PrivilegeSet OPTIONAL
,
1518 IN OUT LPDWORD PrivilegeSetLength
,
1519 OUT LPDWORD GrantedAccess
,
1520 OUT LPBOOL AccessStatus
)
1523 NTSTATUS NtAccessStatus
;
1525 /* Do the access check */
1526 Status
= NtAccessCheck(pSecurityDescriptor
,
1531 (PULONG
)PrivilegeSetLength
,
1532 (PACCESS_MASK
)GrantedAccess
,
1535 /* See if the access check operation succeeded */
1536 if (!NT_SUCCESS(Status
))
1539 SetLastError(RtlNtStatusToDosError(Status
));
1543 /* Now check the access status */
1544 if (!NT_SUCCESS(NtAccessStatus
))
1547 SetLastError(RtlNtStatusToDosError(NtAccessStatus
));
1548 *AccessStatus
= FALSE
;
1552 /* Access granted */
1553 *AccessStatus
= TRUE
;
1556 /* Check succeeded */
1563 BOOL WINAPI
AccessCheckByType(
1564 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1565 PSID PrincipalSelfSid
,
1567 DWORD DesiredAccess
,
1568 POBJECT_TYPE_LIST ObjectTypeList
,
1569 DWORD ObjectTypeListLength
,
1570 PGENERIC_MAPPING GenericMapping
,
1571 PPRIVILEGE_SET PrivilegeSet
,
1572 LPDWORD PrivilegeSetLength
,
1573 LPDWORD GrantedAccess
,
1574 LPBOOL AccessStatus
)
1578 *AccessStatus
= TRUE
;
1580 return !*AccessStatus
;
1588 SetKernelObjectSecurity(HANDLE Handle
,
1589 SECURITY_INFORMATION SecurityInformation
,
1590 PSECURITY_DESCRIPTOR SecurityDescriptor
)
1594 Status
= NtSetSecurityObject(Handle
,
1595 SecurityInformation
,
1596 SecurityDescriptor
);
1597 if (!NT_SUCCESS(Status
))
1599 SetLastError(RtlNtStatusToDosError(Status
));
1611 AddAuditAccessAce(PACL pAcl
,
1612 DWORD dwAceRevision
,
1620 Status
= RtlAddAuditAccessAce(pAcl
,
1626 if (!NT_SUCCESS(Status
))
1628 SetLastError(RtlNtStatusToDosError(Status
));
1639 AddAuditAccessAceEx(PACL pAcl
,
1640 DWORD dwAceRevision
,
1649 Status
= RtlAddAuditAccessAceEx(pAcl
,
1656 if (!NT_SUCCESS(Status
))
1658 SetLastError(RtlNtStatusToDosError(Status
));
1665 /******************************************************************************
1666 * LookupAccountNameA [ADVAPI32.@]
1672 LookupAccountNameA(LPCSTR SystemName
,
1676 LPSTR ReferencedDomainName
,
1677 LPDWORD hReferencedDomainNameLength
,
1678 PSID_NAME_USE SidNameUse
)
1681 UNICODE_STRING lpSystemW
;
1682 UNICODE_STRING lpAccountW
;
1683 LPWSTR lpReferencedDomainNameW
= NULL
;
1685 RtlCreateUnicodeStringFromAsciiz(&lpSystemW
, SystemName
);
1686 RtlCreateUnicodeStringFromAsciiz(&lpAccountW
, AccountName
);
1688 if (ReferencedDomainName
)
1689 lpReferencedDomainNameW
= HeapAlloc(GetProcessHeap(),
1691 *hReferencedDomainNameLength
* sizeof(WCHAR
));
1693 ret
= LookupAccountNameW(lpSystemW
.Buffer
,
1697 lpReferencedDomainNameW
,
1698 hReferencedDomainNameLength
,
1701 if (ret
&& lpReferencedDomainNameW
)
1703 WideCharToMultiByte(CP_ACP
,
1705 lpReferencedDomainNameW
,
1706 *hReferencedDomainNameLength
+ 1,
1707 ReferencedDomainName
,
1708 *hReferencedDomainNameLength
+ 1,
1713 RtlFreeUnicodeString(&lpSystemW
);
1714 RtlFreeUnicodeString(&lpAccountW
);
1715 HeapFree(GetProcessHeap(), 0, lpReferencedDomainNameW
);
1720 /**********************************************************************
1721 * PrivilegeCheck EXPORTED
1726 PrivilegeCheck(HANDLE ClientToken
,
1727 PPRIVILEGE_SET RequiredPrivileges
,
1733 Status
= NtPrivilegeCheck(ClientToken
,
1736 if (!NT_SUCCESS(Status
))
1738 SetLastError(RtlNtStatusToDosError(Status
));
1742 *pfResult
= (BOOL
)Result
;
1747 /******************************************************************************
1748 * GetSecurityInfoExW EXPORTED
1752 GetSecurityInfoExA(HANDLE hObject
,
1753 SE_OBJECT_TYPE ObjectType
,
1754 SECURITY_INFORMATION SecurityInfo
,
1757 PACTRL_ACCESSA
*ppAccessList
,
1758 PACTRL_AUDITA
*ppAuditList
,
1762 FIXME("%s() not implemented!\n", __FUNCTION__
);
1763 return ERROR_BAD_PROVIDER
;
1767 /******************************************************************************
1768 * GetSecurityInfoExW EXPORTED
1772 GetSecurityInfoExW(HANDLE hObject
,
1773 SE_OBJECT_TYPE ObjectType
,
1774 SECURITY_INFORMATION SecurityInfo
,
1777 PACTRL_ACCESSW
*ppAccessList
,
1778 PACTRL_AUDITW
*ppAuditList
,
1782 FIXME("%s() not implemented!\n", __FUNCTION__
);
1783 return ERROR_BAD_PROVIDER
;
1786 /******************************************************************************
1787 * BuildExplicitAccessWithNameA [ADVAPI32.@]
1790 BuildExplicitAccessWithNameA(PEXPLICIT_ACCESSA pExplicitAccess
,
1792 DWORD AccessPermissions
,
1793 ACCESS_MODE AccessMode
,
1796 pExplicitAccess
->grfAccessPermissions
= AccessPermissions
;
1797 pExplicitAccess
->grfAccessMode
= AccessMode
;
1798 pExplicitAccess
->grfInheritance
= Inheritance
;
1800 pExplicitAccess
->Trustee
.pMultipleTrustee
= NULL
;
1801 pExplicitAccess
->Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1802 pExplicitAccess
->Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
1803 pExplicitAccess
->Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1804 pExplicitAccess
->Trustee
.ptstrName
= pTrusteeName
;
1808 /******************************************************************************
1809 * BuildExplicitAccessWithNameW [ADVAPI32.@]
1812 BuildExplicitAccessWithNameW(PEXPLICIT_ACCESSW pExplicitAccess
,
1813 LPWSTR pTrusteeName
,
1814 DWORD AccessPermissions
,
1815 ACCESS_MODE AccessMode
,
1818 pExplicitAccess
->grfAccessPermissions
= AccessPermissions
;
1819 pExplicitAccess
->grfAccessMode
= AccessMode
;
1820 pExplicitAccess
->grfInheritance
= Inheritance
;
1822 pExplicitAccess
->Trustee
.pMultipleTrustee
= NULL
;
1823 pExplicitAccess
->Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1824 pExplicitAccess
->Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
1825 pExplicitAccess
->Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1826 pExplicitAccess
->Trustee
.ptstrName
= pTrusteeName
;
1829 /******************************************************************************
1830 * BuildTrusteeWithObjectsAndNameA [ADVAPI32.@]
1832 VOID WINAPI
BuildTrusteeWithObjectsAndNameA( PTRUSTEEA pTrustee
, POBJECTS_AND_NAME_A pObjName
,
1833 SE_OBJECT_TYPE ObjectType
, LPSTR ObjectTypeName
,
1834 LPSTR InheritedObjectTypeName
, LPSTR Name
)
1836 DWORD ObjectsPresent
= 0;
1838 TRACE("%p %p 0x%08x %p %p %s\n", pTrustee
, pObjName
,
1839 ObjectType
, ObjectTypeName
, InheritedObjectTypeName
, debugstr_a(Name
));
1841 /* Fill the OBJECTS_AND_NAME structure */
1842 pObjName
->ObjectType
= ObjectType
;
1843 if (ObjectTypeName
!= NULL
)
1845 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1848 pObjName
->InheritedObjectTypeName
= InheritedObjectTypeName
;
1849 if (InheritedObjectTypeName
!= NULL
)
1851 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1854 pObjName
->ObjectsPresent
= ObjectsPresent
;
1855 pObjName
->ptstrName
= Name
;
1857 /* Fill the TRUSTEE structure */
1858 pTrustee
->pMultipleTrustee
= NULL
;
1859 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1860 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_NAME
;
1861 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1862 pTrustee
->ptstrName
= (LPSTR
)pObjName
;
1865 /******************************************************************************
1866 * BuildTrusteeWithObjectsAndNameW [ADVAPI32.@]
1868 VOID WINAPI
BuildTrusteeWithObjectsAndNameW( PTRUSTEEW pTrustee
, POBJECTS_AND_NAME_W pObjName
,
1869 SE_OBJECT_TYPE ObjectType
, LPWSTR ObjectTypeName
,
1870 LPWSTR InheritedObjectTypeName
, LPWSTR Name
)
1872 DWORD ObjectsPresent
= 0;
1874 TRACE("%p %p 0x%08x %p %p %s\n", pTrustee
, pObjName
,
1875 ObjectType
, ObjectTypeName
, InheritedObjectTypeName
, debugstr_w(Name
));
1877 /* Fill the OBJECTS_AND_NAME structure */
1878 pObjName
->ObjectType
= ObjectType
;
1879 if (ObjectTypeName
!= NULL
)
1881 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1884 pObjName
->InheritedObjectTypeName
= InheritedObjectTypeName
;
1885 if (InheritedObjectTypeName
!= NULL
)
1887 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1890 pObjName
->ObjectsPresent
= ObjectsPresent
;
1891 pObjName
->ptstrName
= Name
;
1893 /* Fill the TRUSTEE structure */
1894 pTrustee
->pMultipleTrustee
= NULL
;
1895 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1896 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_NAME
;
1897 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1898 pTrustee
->ptstrName
= (LPWSTR
)pObjName
;
1901 /******************************************************************************
1902 * BuildTrusteeWithObjectsAndSidA [ADVAPI32.@]
1905 BuildTrusteeWithObjectsAndSidA(PTRUSTEEA pTrustee
,
1906 POBJECTS_AND_SID pObjSid
,
1908 GUID
*pInheritedObjectGuid
,
1911 DWORD ObjectsPresent
= 0;
1913 TRACE("%p %p %p %p %p\n", pTrustee
, pObjSid
, pObjectGuid
, pInheritedObjectGuid
, pSid
);
1915 /* Fill the OBJECTS_AND_SID structure */
1916 if (pObjectGuid
!= NULL
)
1918 pObjSid
->ObjectTypeGuid
= *pObjectGuid
;
1919 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1923 ZeroMemory(&pObjSid
->ObjectTypeGuid
,
1927 if (pInheritedObjectGuid
!= NULL
)
1929 pObjSid
->InheritedObjectTypeGuid
= *pInheritedObjectGuid
;
1930 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1934 ZeroMemory(&pObjSid
->InheritedObjectTypeGuid
,
1938 pObjSid
->ObjectsPresent
= ObjectsPresent
;
1939 pObjSid
->pSid
= pSid
;
1941 /* Fill the TRUSTEE structure */
1942 pTrustee
->pMultipleTrustee
= NULL
;
1943 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1944 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_SID
;
1945 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1946 pTrustee
->ptstrName
= (LPSTR
) pObjSid
;
1950 /******************************************************************************
1951 * BuildTrusteeWithObjectsAndSidW [ADVAPI32.@]
1954 BuildTrusteeWithObjectsAndSidW(PTRUSTEEW pTrustee
,
1955 POBJECTS_AND_SID pObjSid
,
1957 GUID
*pInheritedObjectGuid
,
1960 DWORD ObjectsPresent
= 0;
1962 TRACE("%p %p %p %p %p\n", pTrustee
, pObjSid
, pObjectGuid
, pInheritedObjectGuid
, pSid
);
1964 /* Fill the OBJECTS_AND_SID structure */
1965 if (pObjectGuid
!= NULL
)
1967 pObjSid
->ObjectTypeGuid
= *pObjectGuid
;
1968 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1972 ZeroMemory(&pObjSid
->ObjectTypeGuid
,
1976 if (pInheritedObjectGuid
!= NULL
)
1978 pObjSid
->InheritedObjectTypeGuid
= *pInheritedObjectGuid
;
1979 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1983 ZeroMemory(&pObjSid
->InheritedObjectTypeGuid
,
1987 pObjSid
->ObjectsPresent
= ObjectsPresent
;
1988 pObjSid
->pSid
= pSid
;
1990 /* Fill the TRUSTEE structure */
1991 pTrustee
->pMultipleTrustee
= NULL
;
1992 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1993 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_SID
;
1994 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1995 pTrustee
->ptstrName
= (LPWSTR
) pObjSid
;
1998 /******************************************************************************
1999 * BuildTrusteeWithSidA [ADVAPI32.@]
2002 BuildTrusteeWithSidA(PTRUSTEE_A pTrustee
,
2005 TRACE("%p %p\n", pTrustee
, pSid
);
2007 pTrustee
->pMultipleTrustee
= NULL
;
2008 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2009 pTrustee
->TrusteeForm
= TRUSTEE_IS_SID
;
2010 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2011 pTrustee
->ptstrName
= (LPSTR
) pSid
;
2015 /******************************************************************************
2016 * BuildTrusteeWithSidW [ADVAPI32.@]
2019 BuildTrusteeWithSidW(PTRUSTEE_W pTrustee
,
2022 TRACE("%p %p\n", pTrustee
, pSid
);
2024 pTrustee
->pMultipleTrustee
= NULL
;
2025 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2026 pTrustee
->TrusteeForm
= TRUSTEE_IS_SID
;
2027 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2028 pTrustee
->ptstrName
= (LPWSTR
) pSid
;
2031 /******************************************************************************
2032 * BuildTrusteeWithNameA [ADVAPI32.@]
2035 BuildTrusteeWithNameA(PTRUSTEE_A pTrustee
,
2038 TRACE("%p %s\n", pTrustee
, name
);
2040 pTrustee
->pMultipleTrustee
= NULL
;
2041 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2042 pTrustee
->TrusteeForm
= TRUSTEE_IS_NAME
;
2043 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2044 pTrustee
->ptstrName
= name
;
2047 /******************************************************************************
2048 * BuildTrusteeWithNameW [ADVAPI32.@]
2051 BuildTrusteeWithNameW(PTRUSTEE_W pTrustee
,
2054 TRACE("%p %s\n", pTrustee
, name
);
2056 pTrustee
->pMultipleTrustee
= NULL
;
2057 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2058 pTrustee
->TrusteeForm
= TRUSTEE_IS_NAME
;
2059 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2060 pTrustee
->ptstrName
= name
;
2063 /******************************************************************************
2064 * GetTrusteeFormA [ADVAPI32.@]
2066 TRUSTEE_FORM WINAPI
GetTrusteeFormA(PTRUSTEEA pTrustee
)
2068 TRACE("(%p)\n", pTrustee
);
2071 return TRUSTEE_BAD_FORM
;
2073 return pTrustee
->TrusteeForm
;
2076 /******************************************************************************
2077 * GetTrusteeFormW [ADVAPI32.@]
2079 TRUSTEE_FORM WINAPI
GetTrusteeFormW(PTRUSTEEW pTrustee
)
2081 TRACE("(%p)\n", pTrustee
);
2084 return TRUSTEE_BAD_FORM
;
2086 return pTrustee
->TrusteeForm
;
2089 /******************************************************************************
2090 * GetTrusteeNameA [ADVAPI32.@]
2093 GetTrusteeNameA(PTRUSTEE_A pTrustee
)
2095 return pTrustee
->ptstrName
;
2099 /******************************************************************************
2100 * GetTrusteeNameW [ADVAPI32.@]
2103 GetTrusteeNameW(PTRUSTEE_W pTrustee
)
2105 return pTrustee
->ptstrName
;
2108 /******************************************************************************
2109 * GetTrusteeTypeA [ADVAPI32.@]
2112 GetTrusteeTypeA(PTRUSTEE_A pTrustee
)
2114 return pTrustee
->TrusteeType
;
2117 /******************************************************************************
2118 * GetTrusteeTypeW [ADVAPI32.@]
2121 GetTrusteeTypeW(PTRUSTEE_W pTrustee
)
2123 return pTrustee
->TrusteeType
;
2131 SetAclInformation(PACL pAcl
,
2132 LPVOID pAclInformation
,
2133 DWORD nAclInformationLength
,
2134 ACL_INFORMATION_CLASS dwAclInformationClass
)
2138 Status
= RtlSetInformationAcl(pAcl
,
2140 nAclInformationLength
,
2141 dwAclInformationClass
);
2142 if (!NT_SUCCESS(Status
))
2144 SetLastError(RtlNtStatusToDosError(Status
));
2151 /**********************************************************************
2152 * SetNamedSecurityInfoA EXPORTED
2158 SetNamedSecurityInfoA(LPSTR pObjectName
,
2159 SE_OBJECT_TYPE ObjectType
,
2160 SECURITY_INFORMATION SecurityInfo
,
2166 UNICODE_STRING ObjectName
;
2169 if (!RtlCreateUnicodeStringFromAsciiz(&ObjectName
, pObjectName
))
2171 return ERROR_NOT_ENOUGH_MEMORY
;
2174 Ret
= SetNamedSecurityInfoW(ObjectName
.Buffer
,
2182 RtlFreeUnicodeString(&ObjectName
);
2192 AreAllAccessesGranted(DWORD GrantedAccess
,
2193 DWORD DesiredAccess
)
2195 return (BOOL
)RtlAreAllAccessesGranted(GrantedAccess
,
2204 AreAnyAccessesGranted(DWORD GrantedAccess
,
2205 DWORD DesiredAccess
)
2207 return (BOOL
)RtlAreAnyAccessesGranted(GrantedAccess
,
2211 /******************************************************************************
2212 * ParseAclStringFlags
2214 static DWORD
ParseAclStringFlags(LPCWSTR
* StringAcl
)
2217 LPCWSTR szAcl
= *StringAcl
;
2219 while (*szAcl
!= '(')
2223 flags
|= SE_DACL_PROTECTED
;
2225 else if (*szAcl
== 'A')
2229 flags
|= SE_DACL_AUTO_INHERIT_REQ
;
2230 else if (*szAcl
== 'I')
2231 flags
|= SE_DACL_AUTO_INHERITED
;
2240 /******************************************************************************
2241 * ParseAceStringType
2243 static const ACEFLAG AceType
[] =
2245 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
2246 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
2247 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
2248 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
2250 { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
2251 { SDDL_OBJECT_ACCESS_DENIED, ACCESS_DENIED_OBJECT_ACE_TYPE },
2252 { SDDL_OBJECT_ALARM, SYSTEM_ALARM_OBJECT_ACE_TYPE },
2253 { SDDL_OBJECT_AUDIT, SYSTEM_AUDIT_OBJECT_ACE_TYPE },
2258 static BYTE
ParseAceStringType(LPCWSTR
* StringAcl
)
2261 LPCWSTR szAcl
= *StringAcl
;
2262 const ACEFLAG
*lpaf
= AceType
;
2264 while (*szAcl
== ' ')
2267 while (lpaf
->wstr
&&
2268 (len
= strlenW(lpaf
->wstr
)) &&
2269 strncmpW(lpaf
->wstr
, szAcl
, len
))
2275 *StringAcl
= szAcl
+ len
;
2280 /******************************************************************************
2281 * ParseAceStringFlags
2283 static const ACEFLAG AceFlags
[] =
2285 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
2286 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
2287 { SDDL_INHERITED
, INHERITED_ACE
},
2288 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
2289 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
2290 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
2291 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
2295 static BYTE
ParseAceStringFlags(LPCWSTR
* StringAcl
)
2299 LPCWSTR szAcl
= *StringAcl
;
2301 while (*szAcl
== ' ')
2304 while (*szAcl
!= ';')
2306 const ACEFLAG
*lpaf
= AceFlags
;
2308 while (lpaf
->wstr
&&
2309 (len
= strlenW(lpaf
->wstr
)) &&
2310 strncmpW(lpaf
->wstr
, szAcl
, len
))
2316 flags
|= lpaf
->value
;
2325 /******************************************************************************
2326 * ParseAceStringRights
2328 static const ACEFLAG AceRights
[] =
2330 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
2331 { SDDL_GENERIC_READ
, GENERIC_READ
},
2332 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
2333 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
2335 { SDDL_READ_CONTROL
, READ_CONTROL
},
2336 { SDDL_STANDARD_DELETE
, DELETE
},
2337 { SDDL_WRITE_DAC
, WRITE_DAC
},
2338 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
2340 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
2341 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
2342 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
2343 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
2344 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
2345 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
2346 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
2347 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
2348 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
2350 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
2351 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
2352 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
2353 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
2355 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
2356 { SDDL_KEY_READ
, KEY_READ
},
2357 { SDDL_KEY_WRITE
, KEY_WRITE
},
2358 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
2362 static DWORD
ParseAceStringRights(LPCWSTR
* StringAcl
)
2366 LPCWSTR szAcl
= *StringAcl
;
2368 while (*szAcl
== ' ')
2371 if ((*szAcl
== '0') && (*(szAcl
+ 1) == 'x'))
2375 while (*p
&& *p
!= ';')
2378 if (p
- szAcl
<= 10 /* 8 hex digits + "0x" */ )
2380 rights
= strtoulW(szAcl
, NULL
, 16);
2384 WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl
, p
- szAcl
));
2388 while (*szAcl
!= ';')
2390 const ACEFLAG
*lpaf
= AceRights
;
2392 while (lpaf
->wstr
&&
2393 (len
= strlenW(lpaf
->wstr
)) &&
2394 strncmpW(lpaf
->wstr
, szAcl
, len
))
2402 rights
|= lpaf
->value
;
2412 /******************************************************************************
2413 * ParseStringAclToAcl
2415 * dacl_flags(string_ace1)(string_ace2)... (string_acen)
2417 static BOOL
ParseStringAclToAcl(LPCWSTR StringAcl
, LPDWORD lpdwFlags
,
2418 PACL pAcl
, LPDWORD cBytes
)
2422 DWORD length
= sizeof(ACL
);
2425 PACCESS_ALLOWED_ACE pAce
= NULL
; /* pointer to current ACE */
2426 DWORD error
= ERROR_INVALID_ACL
;
2428 TRACE("%s\n", debugstr_w(StringAcl
));
2433 if (pAcl
) /* pAce is only useful if we're setting values */
2434 pAce
= (PACCESS_ALLOWED_ACE
) (pAcl
+ 1);
2436 /* Parse ACL flags */
2437 *lpdwFlags
= ParseAclStringFlags(&StringAcl
);
2440 while (*StringAcl
== '(')
2444 /* Parse ACE type */
2445 val
= ParseAceStringType(&StringAcl
);
2447 pAce
->Header
.AceType
= (BYTE
) val
;
2448 if (*StringAcl
!= ';')
2450 error
= RPC_S_INVALID_STRING_UUID
;
2455 /* Parse ACE flags */
2456 val
= ParseAceStringFlags(&StringAcl
);
2458 pAce
->Header
.AceFlags
= (BYTE
) val
;
2459 if (*StringAcl
!= ';')
2463 /* Parse ACE rights */
2464 val
= ParseAceStringRights(&StringAcl
);
2467 if (*StringAcl
!= ';')
2471 /* Parse ACE object guid */
2472 while (*StringAcl
== ' ')
2474 if (*StringAcl
!= ';')
2476 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
2481 /* Parse ACE inherit object guid */
2482 while (*StringAcl
== ' ')
2484 if (*StringAcl
!= ';')
2486 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
2491 /* Parse ACE account sid */
2492 if (ParseStringSidToSid(StringAcl
, pAce
? &pAce
->SidStart
: NULL
, &sidlen
))
2494 while (*StringAcl
&& *StringAcl
!= ')')
2498 if (*StringAcl
!= ')')
2502 acesize
= sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
) + sidlen
;
2506 pAce
->Header
.AceSize
= acesize
;
2507 pAce
= (PACCESS_ALLOWED_ACE
)((LPBYTE
)pAce
+ acesize
);
2514 if (length
> 0xffff)
2516 ERR("ACL too large\n");
2522 pAcl
->AclRevision
= ACL_REVISION
;
2524 pAcl
->AclSize
= length
;
2525 pAcl
->AceCount
= acecount
++;
2531 SetLastError(error
);
2532 WARN("Invalid ACE string format\n");
2537 /******************************************************************************
2538 * ParseStringSecurityDescriptorToSecurityDescriptor
2540 static BOOL
ParseStringSecurityDescriptorToSecurityDescriptor(
2541 LPCWSTR StringSecurityDescriptor
,
2542 SECURITY_DESCRIPTOR_RELATIVE
* SecurityDescriptor
,
2549 LPBYTE lpNext
= NULL
;
2552 *cBytes
= sizeof(SECURITY_DESCRIPTOR
);
2554 tok
= heap_alloc( (lstrlenW(StringSecurityDescriptor
) + 1) * sizeof(WCHAR
));
2556 if (SecurityDescriptor
)
2557 lpNext
= (LPBYTE
)(SecurityDescriptor
+ 1);
2559 while (*StringSecurityDescriptor
== ' ')
2560 StringSecurityDescriptor
++;
2562 while (*StringSecurityDescriptor
)
2564 toktype
= *StringSecurityDescriptor
;
2566 /* Expect char identifier followed by ':' */
2567 StringSecurityDescriptor
++;
2568 if (*StringSecurityDescriptor
!= ':')
2570 SetLastError(ERROR_INVALID_PARAMETER
);
2573 StringSecurityDescriptor
++;
2576 lptoken
= StringSecurityDescriptor
;
2577 while (*lptoken
&& *lptoken
!= ':')
2583 len
= lptoken
- StringSecurityDescriptor
;
2584 memcpy( tok
, StringSecurityDescriptor
, len
* sizeof(WCHAR
) );
2593 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
2596 if (SecurityDescriptor
)
2598 SecurityDescriptor
->Owner
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2599 lpNext
+= bytes
; /* Advance to next token */
2611 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
2614 if (SecurityDescriptor
)
2616 SecurityDescriptor
->Group
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2617 lpNext
+= bytes
; /* Advance to next token */
2630 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
2633 if (SecurityDescriptor
)
2635 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
| flags
;
2636 SecurityDescriptor
->Dacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2637 lpNext
+= bytes
; /* Advance to next token */
2650 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
2653 if (SecurityDescriptor
)
2655 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
| flags
;
2656 SecurityDescriptor
->Sacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2657 lpNext
+= bytes
; /* Advance to next token */
2666 FIXME("Unknown token\n");
2667 SetLastError(ERROR_INVALID_PARAMETER
);
2671 StringSecurityDescriptor
= lptoken
;
2681 /* Winehq cvs 20050916 */
2682 /******************************************************************************
2683 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
2688 ConvertStringSecurityDescriptorToSecurityDescriptorA(LPCSTR StringSecurityDescriptor
,
2689 DWORD StringSDRevision
,
2690 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
2691 PULONG SecurityDescriptorSize
)
2695 LPWSTR StringSecurityDescriptorW
;
2697 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
2698 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2700 if (StringSecurityDescriptorW
)
2702 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
2704 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
2705 StringSDRevision
, SecurityDescriptor
,
2706 SecurityDescriptorSize
);
2707 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
2713 /******************************************************************************
2714 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
2718 ConvertStringSecurityDescriptorToSecurityDescriptorW(LPCWSTR StringSecurityDescriptor
,
2719 DWORD StringSDRevision
,
2720 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
2721 PULONG SecurityDescriptorSize
)
2724 SECURITY_DESCRIPTOR
* psd
;
2727 TRACE("%s\n", debugstr_w(StringSecurityDescriptor
));
2729 if (GetVersion() & 0x80000000)
2731 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2734 else if (!StringSecurityDescriptor
|| !SecurityDescriptor
)
2736 SetLastError(ERROR_INVALID_PARAMETER
);
2739 else if (StringSDRevision
!= SID_REVISION
)
2741 SetLastError(ERROR_UNKNOWN_REVISION
);
2745 /* Compute security descriptor length */
2746 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
2750 psd
= *SecurityDescriptor
= LocalAlloc(GMEM_ZEROINIT
, cBytes
);
2751 if (!psd
) goto lend
;
2753 psd
->Revision
= SID_REVISION
;
2754 psd
->Control
|= SE_SELF_RELATIVE
;
2756 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
2757 (SECURITY_DESCRIPTOR_RELATIVE
*)psd
, &cBytes
))
2763 if (SecurityDescriptorSize
)
2764 *SecurityDescriptorSize
= cBytes
;
2769 TRACE(" ret=%d\n", bret
);
2773 static void DumpString(LPCWSTR string
, int cch
, WCHAR
**pwptr
, ULONG
*plen
)
2776 cch
= strlenW(string
);
2783 memcpy(*pwptr
, string
, sizeof(WCHAR
)*cch
);
2788 static BOOL
DumpSidNumeric(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
2791 WCHAR fmt
[] = { 'S','-','%','u','-','%','d',0 };
2792 WCHAR subauthfmt
[] = { '-','%','u',0 };
2796 if( !IsValidSid( psid
) || pisid
->Revision
!= SDDL_REVISION
)
2798 SetLastError(ERROR_INVALID_SID
);
2802 if (pisid
->IdentifierAuthority
.Value
[0] ||
2803 pisid
->IdentifierAuthority
.Value
[1])
2805 FIXME("not matching MS' bugs\n");
2806 SetLastError(ERROR_INVALID_SID
);
2810 sprintfW( buf
, fmt
, pisid
->Revision
,
2812 MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
2813 pisid
->IdentifierAuthority
.Value
[4] ),
2814 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
2815 pisid
->IdentifierAuthority
.Value
[2] )
2817 DumpString(buf
, -1, pwptr
, plen
);
2819 for( i
=0; i
<pisid
->SubAuthorityCount
; i
++ )
2821 sprintfW( buf
, subauthfmt
, pisid
->SubAuthority
[i
] );
2822 DumpString(buf
, -1, pwptr
, plen
);
2827 static BOOL
DumpSid(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
2830 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
2832 if (WellKnownSids
[i
].wstr
[0] && EqualSid(psid
, (PSID
)&(WellKnownSids
[i
].Sid
.Revision
)))
2834 DumpString(WellKnownSids
[i
].wstr
, 2, pwptr
, plen
);
2839 return DumpSidNumeric(psid
, pwptr
, plen
);
2842 static const LPCWSTR AceRightBitNames
[32] = {
2843 SDDL_CREATE_CHILD
, /* 0 */
2847 SDDL_READ_PROPERTY
, /* 4 */
2848 SDDL_WRITE_PROPERTY
,
2851 SDDL_CONTROL_ACCESS
, /* 8 */
2859 SDDL_STANDARD_DELETE
, /* 16 */
2871 SDDL_GENERIC_ALL
, /* 28 */
2872 SDDL_GENERIC_EXECUTE
,
2877 static void DumpRights(DWORD mask
, WCHAR
**pwptr
, ULONG
*plen
)
2879 static const WCHAR fmtW
[] = {'0','x','%','x',0};
2886 /* first check if the right have name */
2887 for (i
= 0; i
< sizeof(AceRights
)/sizeof(AceRights
[0]); i
++)
2889 if (AceRights
[i
].wstr
== NULL
)
2891 if (mask
== AceRights
[i
].value
)
2893 DumpString(AceRights
[i
].wstr
, -1, pwptr
, plen
);
2898 /* then check if it can be built from bit names */
2899 for (i
= 0; i
< 32; i
++)
2901 if ((mask
& (1 << i
)) && (AceRightBitNames
[i
] == NULL
))
2903 /* can't be built from bit names */
2904 sprintfW(buf
, fmtW
, mask
);
2905 DumpString(buf
, -1, pwptr
, plen
);
2910 /* build from bit names */
2911 for (i
= 0; i
< 32; i
++)
2912 if (mask
& (1 << i
))
2913 DumpString(AceRightBitNames
[i
], -1, pwptr
, plen
);
2916 static BOOL
DumpAce(LPVOID pace
, WCHAR
**pwptr
, ULONG
*plen
)
2918 ACCESS_ALLOWED_ACE
*piace
; /* all the supported ACEs have the same memory layout */
2919 static const WCHAR openbr
= '(';
2920 static const WCHAR closebr
= ')';
2921 static const WCHAR semicolon
= ';';
2923 if (((PACE_HEADER
)pace
)->AceType
> SYSTEM_ALARM_ACE_TYPE
|| ((PACE_HEADER
)pace
)->AceSize
< sizeof(ACCESS_ALLOWED_ACE
))
2925 SetLastError(ERROR_INVALID_ACL
);
2930 DumpString(&openbr
, 1, pwptr
, plen
);
2931 switch (piace
->Header
.AceType
)
2933 case ACCESS_ALLOWED_ACE_TYPE
:
2934 DumpString(SDDL_ACCESS_ALLOWED
, -1, pwptr
, plen
);
2936 case ACCESS_DENIED_ACE_TYPE
:
2937 DumpString(SDDL_ACCESS_DENIED
, -1, pwptr
, plen
);
2939 case SYSTEM_AUDIT_ACE_TYPE
:
2940 DumpString(SDDL_AUDIT
, -1, pwptr
, plen
);
2942 case SYSTEM_ALARM_ACE_TYPE
:
2943 DumpString(SDDL_ALARM
, -1, pwptr
, plen
);
2946 DumpString(&semicolon
, 1, pwptr
, plen
);
2948 if (piace
->Header
.AceFlags
& OBJECT_INHERIT_ACE
)
2949 DumpString(SDDL_OBJECT_INHERIT
, -1, pwptr
, plen
);
2950 if (piace
->Header
.AceFlags
& CONTAINER_INHERIT_ACE
)
2951 DumpString(SDDL_CONTAINER_INHERIT
, -1, pwptr
, plen
);
2952 if (piace
->Header
.AceFlags
& NO_PROPAGATE_INHERIT_ACE
)
2953 DumpString(SDDL_NO_PROPAGATE
, -1, pwptr
, plen
);
2954 if (piace
->Header
.AceFlags
& INHERIT_ONLY_ACE
)
2955 DumpString(SDDL_INHERIT_ONLY
, -1, pwptr
, plen
);
2956 if (piace
->Header
.AceFlags
& INHERITED_ACE
)
2957 DumpString(SDDL_INHERITED
, -1, pwptr
, plen
);
2958 if (piace
->Header
.AceFlags
& SUCCESSFUL_ACCESS_ACE_FLAG
)
2959 DumpString(SDDL_AUDIT_SUCCESS
, -1, pwptr
, plen
);
2960 if (piace
->Header
.AceFlags
& FAILED_ACCESS_ACE_FLAG
)
2961 DumpString(SDDL_AUDIT_FAILURE
, -1, pwptr
, plen
);
2962 DumpString(&semicolon
, 1, pwptr
, plen
);
2963 DumpRights(piace
->Mask
, pwptr
, plen
);
2964 DumpString(&semicolon
, 1, pwptr
, plen
);
2965 /* objects not supported */
2966 DumpString(&semicolon
, 1, pwptr
, plen
);
2967 /* objects not supported */
2968 DumpString(&semicolon
, 1, pwptr
, plen
);
2969 if (!DumpSid((PSID
)&piace
->SidStart
, pwptr
, plen
))
2971 DumpString(&closebr
, 1, pwptr
, plen
);
2975 static BOOL
DumpAcl(PACL pacl
, WCHAR
**pwptr
, ULONG
*plen
, BOOL
protected, BOOL autoInheritReq
, BOOL autoInherited
)
2981 DumpString(SDDL_PROTECTED
, -1, pwptr
, plen
);
2983 DumpString(SDDL_AUTO_INHERIT_REQ
, -1, pwptr
, plen
);
2985 DumpString(SDDL_AUTO_INHERITED
, -1, pwptr
, plen
);
2990 if (!IsValidAcl(pacl
))
2993 count
= pacl
->AceCount
;
2994 for (i
= 0; i
< count
; i
++)
2997 if (!GetAce(pacl
, i
, &ace
))
2999 if (!DumpAce(ace
, pwptr
, plen
))
3006 static BOOL
DumpOwner(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3008 static const WCHAR prefix
[] = {'O',':',0};
3012 if (!GetSecurityDescriptorOwner(SecurityDescriptor
, &psid
, &bDefaulted
))
3018 DumpString(prefix
, -1, pwptr
, plen
);
3019 if (!DumpSid(psid
, pwptr
, plen
))
3024 static BOOL
DumpGroup(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3026 static const WCHAR prefix
[] = {'G',':',0};
3030 if (!GetSecurityDescriptorGroup(SecurityDescriptor
, &psid
, &bDefaulted
))
3036 DumpString(prefix
, -1, pwptr
, plen
);
3037 if (!DumpSid(psid
, pwptr
, plen
))
3042 static BOOL
DumpDacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3044 static const WCHAR dacl
[] = {'D',':',0};
3045 SECURITY_DESCRIPTOR_CONTROL control
;
3046 BOOL present
, defaulted
;
3050 if (!GetSecurityDescriptorDacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
3053 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
3059 DumpString(dacl
, 2, pwptr
, plen
);
3060 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_DACL_PROTECTED
, control
& SE_DACL_AUTO_INHERIT_REQ
, control
& SE_DACL_AUTO_INHERITED
))
3065 static BOOL
DumpSacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3067 static const WCHAR sacl
[] = {'S',':',0};
3068 SECURITY_DESCRIPTOR_CONTROL control
;
3069 BOOL present
, defaulted
;
3073 if (!GetSecurityDescriptorSacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
3076 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
3082 DumpString(sacl
, 2, pwptr
, plen
);
3083 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_SACL_PROTECTED
, control
& SE_SACL_AUTO_INHERIT_REQ
, control
& SE_SACL_AUTO_INHERITED
))
3088 /******************************************************************************
3089 * ConvertSecurityDescriptorToStringSecurityDescriptorW [ADVAPI32.@]
3091 BOOL WINAPI
ConvertSecurityDescriptorToStringSecurityDescriptorW(PSECURITY_DESCRIPTOR SecurityDescriptor
, DWORD SDRevision
, SECURITY_INFORMATION RequestedInformation
, LPWSTR
*OutputString
, PULONG OutputLen
)
3096 if (SDRevision
!= SDDL_REVISION_1
)
3098 ERR("Program requested unknown SDDL revision %d\n", SDRevision
);
3099 SetLastError(ERROR_UNKNOWN_REVISION
);
3104 if (RequestedInformation
& OWNER_SECURITY_INFORMATION
)
3105 if (!DumpOwner(SecurityDescriptor
, NULL
, &len
))
3107 if (RequestedInformation
& GROUP_SECURITY_INFORMATION
)
3108 if (!DumpGroup(SecurityDescriptor
, NULL
, &len
))
3110 if (RequestedInformation
& DACL_SECURITY_INFORMATION
)
3111 if (!DumpDacl(SecurityDescriptor
, NULL
, &len
))
3113 if (RequestedInformation
& SACL_SECURITY_INFORMATION
)
3114 if (!DumpSacl(SecurityDescriptor
, NULL
, &len
))
3117 wstr
= wptr
= LocalAlloc(0, (len
+ 1)*sizeof(WCHAR
));
3123 if (RequestedInformation
& OWNER_SECURITY_INFORMATION
)
3124 if (!DumpOwner(SecurityDescriptor
, &wptr
, NULL
)) {
3128 if (RequestedInformation
& GROUP_SECURITY_INFORMATION
)
3129 if (!DumpGroup(SecurityDescriptor
, &wptr
, NULL
)) {
3133 if (RequestedInformation
& DACL_SECURITY_INFORMATION
)
3134 if (!DumpDacl(SecurityDescriptor
, &wptr
, NULL
)) {
3138 if (RequestedInformation
& SACL_SECURITY_INFORMATION
)
3139 if (!DumpSacl(SecurityDescriptor
, &wptr
, NULL
)) {
3145 TRACE("ret: %s, %d\n", wine_dbgstr_w(wstr
), len
);
3146 *OutputString
= wstr
;
3148 *OutputLen
= strlenW(*OutputString
)+1;
3152 /******************************************************************************
3153 * ConvertSecurityDescriptorToStringSecurityDescriptorA [ADVAPI32.@]
3155 BOOL WINAPI
ConvertSecurityDescriptorToStringSecurityDescriptorA(PSECURITY_DESCRIPTOR SecurityDescriptor
, DWORD SDRevision
, SECURITY_INFORMATION Information
, LPSTR
*OutputString
, PULONG OutputLen
)
3159 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
3163 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
3164 *OutputString
= heap_alloc(lenA
);
3166 if (*OutputString
== NULL
)
3173 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
3176 if (OutputLen
!= NULL
)
3182 *OutputString
= NULL
;
3189 /******************************************************************************
3190 * ConvertStringSidToSidW [ADVAPI32.@]
3192 BOOL WINAPI
ConvertStringSidToSidW(LPCWSTR StringSid
, PSID
* Sid
)
3197 TRACE("%s, %p\n", debugstr_w(StringSid
), Sid
);
3198 if (GetVersion() & 0x80000000)
3199 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3200 else if (!StringSid
|| !Sid
)
3201 SetLastError(ERROR_INVALID_PARAMETER
);
3202 else if (ParseStringSidToSid(StringSid
, NULL
, &cBytes
))
3204 PSID pSid
= *Sid
= LocalAlloc(0, cBytes
);
3206 bret
= ParseStringSidToSid(StringSid
, pSid
, &cBytes
);
3213 /******************************************************************************
3214 * ConvertStringSidToSidA [ADVAPI32.@]
3216 BOOL WINAPI
ConvertStringSidToSidA(LPCSTR StringSid
, PSID
* Sid
)
3220 TRACE("%s, %p\n", debugstr_a(StringSid
), Sid
);
3221 if (GetVersion() & 0x80000000)
3222 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3223 else if (!StringSid
|| !Sid
)
3224 SetLastError(ERROR_INVALID_PARAMETER
);
3227 WCHAR
*wStringSid
= SERV_dup(StringSid
);
3228 bret
= ConvertStringSidToSidW(wStringSid
, Sid
);
3229 heap_free(wStringSid
);
3239 ConvertSidToStringSidW(PSID Sid
,
3243 UNICODE_STRING UnicodeString
;
3244 WCHAR FixedBuffer
[64];
3246 if (!RtlValidSid(Sid
))
3248 SetLastError(ERROR_INVALID_SID
);
3252 UnicodeString
.Length
= 0;
3253 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
3254 UnicodeString
.Buffer
= FixedBuffer
;
3255 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
3256 if (STATUS_BUFFER_TOO_SMALL
== Status
)
3258 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
3261 if (!NT_SUCCESS(Status
))
3263 SetLastError(RtlNtStatusToDosError(Status
));
3267 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
3268 if (NULL
== *StringSid
)
3270 if (UnicodeString
.Buffer
!= FixedBuffer
)
3272 RtlFreeUnicodeString(&UnicodeString
);
3274 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3278 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
3279 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
3280 if (UnicodeString
.Buffer
!= FixedBuffer
)
3282 RtlFreeUnicodeString(&UnicodeString
);
3293 ConvertSidToStringSidA(PSID Sid
,
3299 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
3304 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
3307 LocalFree(StringSidW
);
3308 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3312 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
3313 if (NULL
== *StringSid
)
3315 LocalFree(StringSidW
);
3316 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3320 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
3322 LocalFree(StringSid
);
3323 LocalFree(StringSidW
);
3327 LocalFree(StringSidW
);
3336 CreateProcessWithLogonW(LPCWSTR lpUsername
,
3340 LPCWSTR lpApplicationName
,
3341 LPWSTR lpCommandLine
,
3342 DWORD dwCreationFlags
,
3343 LPVOID lpEnvironment
,
3344 LPCWSTR lpCurrentDirectory
,
3345 LPSTARTUPINFOW lpStartupInfo
,
3346 LPPROCESS_INFORMATION lpProcessInformation
)
3348 FIXME("%s %s %s 0x%08x %s %s 0x%08x %p %s %p %p stub\n", debugstr_w(lpUsername
), debugstr_w(lpDomain
),
3349 debugstr_w(lpPassword
), dwLogonFlags
, debugstr_w(lpApplicationName
),
3350 debugstr_w(lpCommandLine
), dwCreationFlags
, lpEnvironment
, debugstr_w(lpCurrentDirectory
),
3351 lpStartupInfo
, lpProcessInformation
);
3356 BOOL WINAPI
CreateProcessWithTokenW(HANDLE token
, DWORD logon_flags
, LPCWSTR application_name
, LPWSTR command_line
,
3357 DWORD creation_flags
, void *environment
, LPCWSTR current_directory
, STARTUPINFOW
*startup_info
,
3358 PROCESS_INFORMATION
*process_information
)
3360 FIXME("%p 0x%08x %s %s 0x%08x %p %s %p %p - semi-stub\n", token
,
3361 logon_flags
, debugstr_w(application_name
), debugstr_w(command_line
),
3362 creation_flags
, environment
, debugstr_w(current_directory
),
3363 startup_info
, process_information
);
3365 /* FIXME: check if handles should be inherited */
3366 return CreateProcessW( application_name
, command_line
, NULL
, NULL
, FALSE
, creation_flags
, environment
,
3367 current_directory
, startup_info
, process_information
);
3374 DuplicateTokenEx(IN HANDLE ExistingTokenHandle
,
3375 IN DWORD dwDesiredAccess
,
3376 IN LPSECURITY_ATTRIBUTES lpTokenAttributes OPTIONAL
,
3377 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
,
3378 IN TOKEN_TYPE TokenType
,
3379 OUT PHANDLE DuplicateTokenHandle
)
3381 OBJECT_ATTRIBUTES ObjectAttributes
;
3383 SECURITY_QUALITY_OF_SERVICE Sqos
;
3385 TRACE("%p 0x%08x 0x%08x 0x%08x %p\n", ExistingTokenHandle
, dwDesiredAccess
,
3386 ImpersonationLevel
, TokenType
, DuplicateTokenHandle
);
3388 Sqos
.Length
= sizeof(SECURITY_QUALITY_OF_SERVICE
);
3389 Sqos
.ImpersonationLevel
= ImpersonationLevel
;
3390 Sqos
.ContextTrackingMode
= 0;
3391 Sqos
.EffectiveOnly
= FALSE
;
3393 if (lpTokenAttributes
!= NULL
)
3395 InitializeObjectAttributes(&ObjectAttributes
,
3397 lpTokenAttributes
->bInheritHandle
? OBJ_INHERIT
: 0,
3399 lpTokenAttributes
->lpSecurityDescriptor
);
3403 InitializeObjectAttributes(&ObjectAttributes
,
3410 ObjectAttributes
.SecurityQualityOfService
= &Sqos
;
3412 Status
= NtDuplicateToken(ExistingTokenHandle
,
3417 DuplicateTokenHandle
);
3418 if (!NT_SUCCESS(Status
))
3420 ERR("NtDuplicateToken failed: Status %08x\n", Status
);
3421 SetLastError(RtlNtStatusToDosError(Status
));
3425 TRACE("Returning token %p.\n", *DuplicateTokenHandle
);
3434 DuplicateToken(IN HANDLE ExistingTokenHandle
,
3435 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
,
3436 OUT PHANDLE DuplicateTokenHandle
)
3438 return DuplicateTokenEx(ExistingTokenHandle
,
3439 TOKEN_IMPERSONATE
| TOKEN_QUERY
,
3443 DuplicateTokenHandle
);
3446 /******************************************************************************
3447 * ComputeStringSidSize
3449 static DWORD
ComputeStringSidSize(LPCWSTR StringSid
)
3451 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I(-S)+ */
3456 if (*StringSid
== '-')
3462 return GetSidLengthRequired(ctok
- 2);
3464 else /* String constant format - Only available in winxp and above */
3468 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
3469 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
3470 return GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
3472 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
3473 if (!strncmpW(WellKnownRids
[i
].wstr
, StringSid
, 2))
3476 ADVAPI_GetComputerSid(&local
);
3477 return GetSidLengthRequired(*GetSidSubAuthorityCount(&local
) + 1);
3482 return GetSidLengthRequired(0);
3485 /******************************************************************************
3486 * ParseStringSidToSid
3488 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
)
3493 TRACE("%s, %p, %p\n", debugstr_w(StringSid
), pSid
, cBytes
);
3496 SetLastError(ERROR_INVALID_PARAMETER
);
3497 TRACE("StringSid is NULL, returning FALSE\n");
3501 while (*StringSid
== ' ')
3505 goto lend
; /* ERROR_INVALID_SID */
3507 *cBytes
= ComputeStringSidSize(StringSid
);
3508 if (!pisid
) /* Simply compute the size */
3510 TRACE("only size requested, returning TRUE with %d\n", *cBytes
);
3514 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
3516 DWORD i
= 0, identAuth
;
3517 DWORD csubauth
= ((*cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
3519 StringSid
+= 2; /* Advance to Revision */
3520 pisid
->Revision
= atoiW(StringSid
);
3522 if (pisid
->Revision
!= SDDL_REVISION
)
3524 TRACE("Revision %d is unknown\n", pisid
->Revision
);
3525 goto lend
; /* ERROR_INVALID_SID */
3529 TRACE("SubAuthorityCount is 0\n");
3530 goto lend
; /* ERROR_INVALID_SID */
3533 pisid
->SubAuthorityCount
= csubauth
;
3535 /* Advance to identifier authority */
3536 while (*StringSid
&& *StringSid
!= '-')
3538 if (*StringSid
== '-')
3541 /* MS' implementation can't handle values greater than 2^32 - 1, so
3542 * we don't either; assume most significant bytes are always 0
3544 pisid
->IdentifierAuthority
.Value
[0] = 0;
3545 pisid
->IdentifierAuthority
.Value
[1] = 0;
3546 identAuth
= atoiW(StringSid
);
3547 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
3548 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
3549 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
3550 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
3552 /* Advance to first sub authority */
3553 while (*StringSid
&& *StringSid
!= '-')
3555 if (*StringSid
== '-')
3560 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
3562 while (*StringSid
&& *StringSid
!= '-')
3564 if (*StringSid
== '-')
3568 if (i
!= pisid
->SubAuthorityCount
)
3569 goto lend
; /* ERROR_INVALID_SID */
3573 else /* String constant format - Only available in winxp and above */
3576 pisid
->Revision
= SDDL_REVISION
;
3578 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
3579 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
3582 pisid
->SubAuthorityCount
= WellKnownSids
[i
].Sid
.SubAuthorityCount
;
3583 pisid
->IdentifierAuthority
= WellKnownSids
[i
].Sid
.IdentifierAuthority
;
3584 for (j
= 0; j
< WellKnownSids
[i
].Sid
.SubAuthorityCount
; j
++)
3585 pisid
->SubAuthority
[j
] = WellKnownSids
[i
].Sid
.SubAuthority
[j
];
3589 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
3590 if (!strncmpW(WellKnownRids
[i
].wstr
, StringSid
, 2))
3592 ADVAPI_GetComputerSid(pisid
);
3593 pisid
->SubAuthority
[pisid
->SubAuthorityCount
] = WellKnownRids
[i
].Rid
;
3594 pisid
->SubAuthorityCount
++;
3599 FIXME("String constant not supported: %s\n", debugstr_wn(StringSid
, 2));
3604 SetLastError(ERROR_INVALID_SID
);
3606 TRACE("returning %s\n", bret
? "TRUE" : "FALSE");
3610 /**********************************************************************
3611 * GetNamedSecurityInfoA EXPORTED
3617 GetNamedSecurityInfoA(LPSTR pObjectName
,
3618 SE_OBJECT_TYPE ObjectType
,
3619 SECURITY_INFORMATION SecurityInfo
,
3624 PSECURITY_DESCRIPTOR
*ppSecurityDescriptor
)
3630 TRACE("%s %d %d %p %p %p %p %p\n", pObjectName
, ObjectType
, SecurityInfo
,
3631 ppsidOwner
, ppsidGroup
, ppDacl
, ppSacl
, ppSecurityDescriptor
);
3635 len
= MultiByteToWideChar( CP_ACP
, 0, pObjectName
, -1, NULL
, 0 );
3636 wstr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
));
3637 MultiByteToWideChar( CP_ACP
, 0, pObjectName
, -1, wstr
, len
);
3640 r
= GetNamedSecurityInfoW( wstr
, ObjectType
, SecurityInfo
, ppsidOwner
,
3641 ppsidGroup
, ppDacl
, ppSacl
, ppSecurityDescriptor
);
3643 HeapFree( GetProcessHeap(), 0, wstr
);
3653 GetWindowsAccountDomainSid(IN PSID pSid
,
3654 OUT PSID ppDomainSid
,
3655 IN OUT DWORD
* cbSid
)
3666 EqualDomainSid(IN PSID pSid1
,