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 DWORD
ComputeStringSidSize(LPCWSTR StringSid
);
20 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
);
22 #define MAX_GUID_STRING_LEN 39
25 AddAuditAccessAceEx(PACL pAcl
,
40 typedef struct _MAX_SID
42 /* same fields as struct _SID */
44 BYTE SubAuthorityCount
;
45 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
46 DWORD SubAuthority
[SID_MAX_SUB_AUTHORITIES
];
49 typedef struct WELLKNOWNSID
52 WELL_KNOWN_SID_TYPE Type
;
56 typedef struct _ACEFLAG
60 } ACEFLAG
, *LPACEFLAG
;
62 static const WELLKNOWNSID WellKnownSids
[] =
64 { {0,0}, WinNullSid
, { SID_REVISION
, 1, { SECURITY_NULL_SID_AUTHORITY
}, { SECURITY_NULL_RID
} } },
65 { {'W','D'}, WinWorldSid
, { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} } },
66 { {0,0}, WinLocalSid
, { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} } },
67 { {'C','O'}, WinCreatorOwnerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_RID
} } },
68 { {'C','G'}, WinCreatorGroupSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_RID
} } },
69 { {0,0}, WinCreatorOwnerServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_SERVER_RID
} } },
70 { {0,0}, WinCreatorGroupServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_SERVER_RID
} } },
71 { {0,0}, WinNtAuthoritySid
, { SID_REVISION
, 0, { SECURITY_NT_AUTHORITY
}, { SECURITY_NULL_RID
} } },
72 { {0,0}, WinDialupSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_DIALUP_RID
} } },
73 { {'N','U'}, WinNetworkSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_RID
} } },
74 { {0,0}, WinBatchSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BATCH_RID
} } },
75 { {'I','U'}, WinInteractiveSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} } },
76 { {'S','U'}, WinServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_SERVICE_RID
} } },
77 { {'A','N'}, WinAnonymousSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} } },
78 { {0,0}, WinProxySid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PROXY_RID
} } },
79 { {'E','D'}, WinEnterpriseControllersSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ENTERPRISE_CONTROLLERS_RID
} } },
80 { {'P','S'}, WinSelfSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PRINCIPAL_SELF_RID
} } },
81 { {'A','U'}, WinAuthenticatedUserSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} } },
82 { {'R','C'}, WinRestrictedCodeSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_RESTRICTED_CODE_RID
} } },
83 { {0,0}, WinTerminalServerSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_TERMINAL_SERVER_RID
} } },
84 { {0,0}, WinRemoteLogonIdSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_REMOTE_LOGON_RID
} } },
85 { {'S','Y'}, WinLocalSystemSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} } },
86 { {'L','S'}, WinLocalServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SERVICE_RID
} } },
87 { {'N','S'}, WinNetworkServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_SERVICE_RID
} } },
88 { {0,0}, WinBuiltinDomainSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
} } },
89 { {'B','A'}, WinBuiltinAdministratorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} } },
90 { {'B','U'}, WinBuiltinUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} } },
91 { {'B','G'}, WinBuiltinGuestsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_GUESTS
} } },
92 { {'P','U'}, WinBuiltinPowerUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_POWER_USERS
} } },
93 { {'A','O'}, WinBuiltinAccountOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ACCOUNT_OPS
} } },
94 { {'S','O'}, WinBuiltinSystemOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_SYSTEM_OPS
} } },
95 { {'P','O'}, WinBuiltinPrintOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PRINT_OPS
} } },
96 { {'B','O'}, WinBuiltinBackupOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_BACKUP_OPS
} } },
97 { {'R','E'}, WinBuiltinReplicatorSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REPLICATOR
} } },
98 { {'R','U'}, WinBuiltinPreWindows2000CompatibleAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PREW2KCOMPACCESS
} } },
99 { {'R','D'}, WinBuiltinRemoteDesktopUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS
} } },
100 { {'N','O'}, WinBuiltinNetworkConfigurationOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS
} } },
101 { {0,0}, WinNTLMAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_NTLM_RID
} } },
102 { {0,0}, WinDigestAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_DIGEST_RID
} } },
103 { {0,0}, WinSChannelAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_SCHANNEL_RID
} } },
104 { {0,0}, WinThisOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_THIS_ORGANIZATION_RID
} } },
105 { {0,0}, WinOtherOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_OTHER_ORGANIZATION_RID
} } },
106 { {0,0}, WinBuiltinIncomingForestTrustBuildersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS
} } },
107 { {0,0}, WinBuiltinPerfMonitoringUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_MONITORING_USERS
} } },
108 { {0,0}, WinBuiltinPerfLoggingUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_LOGGING_USERS
} } },
109 { {0,0}, WinBuiltinAuthorizationAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS
} } },
110 { {0,0}, WinBuiltinTerminalServerLicenseServersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS
} } },
111 { {0,0}, WinBuiltinDCOMUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_DCOM_USERS
} } },
112 { {'L','W'}, WinLowLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_LOW_RID
} } },
113 { {'M','E'}, WinMediumLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_MEDIUM_RID
} } },
114 { {'H','I'}, WinHighLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_HIGH_RID
} } },
115 { {'S','I'}, WinSystemLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_SYSTEM_RID
} } },
118 /* these SIDs must be constructed as relative to some domain - only the RID is well-known */
119 typedef struct WELLKNOWNRID
122 WELL_KNOWN_SID_TYPE Type
;
126 static const WELLKNOWNRID WellKnownRids
[] = {
127 { {'L','A'}, WinAccountAdministratorSid
, DOMAIN_USER_RID_ADMIN
},
128 { {'L','G'}, WinAccountGuestSid
, DOMAIN_USER_RID_GUEST
},
129 { {0,0}, WinAccountKrbtgtSid
, DOMAIN_USER_RID_KRBTGT
},
130 { {0,0}, WinAccountDomainAdminsSid
, DOMAIN_GROUP_RID_ADMINS
},
131 { {0,0}, WinAccountDomainUsersSid
, DOMAIN_GROUP_RID_USERS
},
132 { {0,0}, WinAccountDomainGuestsSid
, DOMAIN_GROUP_RID_GUESTS
},
133 { {0,0}, WinAccountComputersSid
, DOMAIN_GROUP_RID_COMPUTERS
},
134 { {0,0}, WinAccountControllersSid
, DOMAIN_GROUP_RID_CONTROLLERS
},
135 { {0,0}, WinAccountCertAdminsSid
, DOMAIN_GROUP_RID_CERT_ADMINS
},
136 { {0,0}, WinAccountSchemaAdminsSid
, DOMAIN_GROUP_RID_SCHEMA_ADMINS
},
137 { {0,0}, WinAccountEnterpriseAdminsSid
, DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
},
138 { {0,0}, WinAccountPolicyAdminsSid
, DOMAIN_GROUP_RID_POLICY_ADMINS
},
139 { {0,0}, WinAccountRasAndIasServersSid
, DOMAIN_ALIAS_RID_RAS_SERVERS
},
142 static const SID sidWorld
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
} , { SECURITY_WORLD_RID
} };
147 static const WCHAR SDDL_ACCESS_ALLOWED
[] = {'A',0};
148 static const WCHAR SDDL_ACCESS_DENIED
[] = {'D',0};
149 static const WCHAR SDDL_OBJECT_ACCESS_ALLOWED
[] = {'O','A',0};
150 static const WCHAR SDDL_OBJECT_ACCESS_DENIED
[] = {'O','D',0};
151 static const WCHAR SDDL_AUDIT
[] = {'A','U',0};
152 static const WCHAR SDDL_ALARM
[] = {'A','L',0};
153 static const WCHAR SDDL_OBJECT_AUDIT
[] = {'O','U',0};
154 static const WCHAR SDDL_OBJECT_ALARM
[] = {'O','L',0};
159 #define ADS_RIGHT_DS_CREATE_CHILD 0x0001
160 #define ADS_RIGHT_DS_DELETE_CHILD 0x0002
161 #define ADS_RIGHT_ACTRL_DS_LIST 0x0004
162 #define ADS_RIGHT_DS_SELF 0x0008
163 #define ADS_RIGHT_DS_READ_PROP 0x0010
164 #define ADS_RIGHT_DS_WRITE_PROP 0x0020
165 #define ADS_RIGHT_DS_DELETE_TREE 0x0040
166 #define ADS_RIGHT_DS_LIST_OBJECT 0x0080
167 #define ADS_RIGHT_DS_CONTROL_ACCESS 0x0100
172 static const WCHAR SDDL_CONTAINER_INHERIT
[] = {'C','I',0};
173 static const WCHAR SDDL_OBJECT_INHERIT
[] = {'O','I',0};
174 static const WCHAR SDDL_NO_PROPAGATE
[] = {'N','P',0};
175 static const WCHAR SDDL_INHERIT_ONLY
[] = {'I','O',0};
176 static const WCHAR SDDL_INHERITED
[] = {'I','D',0};
177 static const WCHAR SDDL_AUDIT_SUCCESS
[] = {'S','A',0};
178 static const WCHAR SDDL_AUDIT_FAILURE
[] = {'F','A',0};
180 static const char * debugstr_sid(PSID sid
)
183 SID
* psid
= (SID
*)sid
;
188 auth
= psid
->IdentifierAuthority
.Value
[5] +
189 (psid
->IdentifierAuthority
.Value
[4] << 8) +
190 (psid
->IdentifierAuthority
.Value
[3] << 16) +
191 (psid
->IdentifierAuthority
.Value
[2] << 24);
193 switch (psid
->SubAuthorityCount
) {
195 return wine_dbg_sprintf("S-%d-%d", psid
->Revision
, auth
);
197 return wine_dbg_sprintf("S-%d-%d-%lu", psid
->Revision
, auth
,
198 psid
->SubAuthority
[0]);
200 return wine_dbg_sprintf("S-%d-%d-%lu-%lu", psid
->Revision
, auth
,
201 psid
->SubAuthority
[0], psid
->SubAuthority
[1]);
203 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu", psid
->Revision
, auth
,
204 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2]);
206 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
207 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
208 psid
->SubAuthority
[3]);
210 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
211 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
212 psid
->SubAuthority
[3], psid
->SubAuthority
[4]);
214 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
215 psid
->SubAuthority
[3], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
216 psid
->SubAuthority
[0], psid
->SubAuthority
[4], psid
->SubAuthority
[5]);
218 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
219 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
220 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
221 psid
->SubAuthority
[6]);
223 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
224 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
225 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
226 psid
->SubAuthority
[6], psid
->SubAuthority
[7]);
231 /* set last error code from NT status and get the proper boolean return value */
232 /* used for functions that are a simple wrapper around the corresponding ntdll API */
233 static __inline BOOL
set_ntstatus( NTSTATUS status
)
235 if (!NT_SUCCESS(status
)) SetLastError( RtlNtStatusToDosError( status
));
236 return NT_SUCCESS(status
);
239 static const RECORD SidTable
[] =
241 { SDDL_ACCOUNT_OPERATORS
, WinBuiltinAccountOperatorsSid
},
242 { SDDL_ALIAS_PREW2KCOMPACC
, WinBuiltinPreWindows2000CompatibleAccessSid
},
243 { SDDL_ANONYMOUS
, WinAnonymousSid
},
244 { SDDL_AUTHENTICATED_USERS
, WinAuthenticatedUserSid
},
245 { SDDL_BUILTIN_ADMINISTRATORS
, WinBuiltinAdministratorsSid
},
246 { SDDL_BUILTIN_GUESTS
, WinBuiltinGuestsSid
},
247 { SDDL_BACKUP_OPERATORS
, WinBuiltinBackupOperatorsSid
},
248 { SDDL_BUILTIN_USERS
, WinBuiltinUsersSid
},
249 { SDDL_CERT_SERV_ADMINISTRATORS
, WinAccountCertAdminsSid
/* FIXME: DOMAIN_GROUP_RID_CERT_ADMINS */ },
250 { SDDL_CREATOR_GROUP
, WinCreatorGroupSid
},
251 { SDDL_CREATOR_OWNER
, WinCreatorOwnerSid
},
252 { SDDL_DOMAIN_ADMINISTRATORS
, WinAccountDomainAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ADMINS */ },
253 { SDDL_DOMAIN_COMPUTERS
, WinAccountComputersSid
/* FIXME: DOMAIN_GROUP_RID_COMPUTERS */ },
254 { SDDL_DOMAIN_DOMAIN_CONTROLLERS
, WinAccountControllersSid
/* FIXME: DOMAIN_GROUP_RID_CONTROLLERS */ },
255 { SDDL_DOMAIN_GUESTS
, WinAccountDomainGuestsSid
/* FIXME: DOMAIN_GROUP_RID_GUESTS */ },
256 { SDDL_DOMAIN_USERS
, WinAccountDomainUsersSid
/* FIXME: DOMAIN_GROUP_RID_USERS */ },
257 { SDDL_ENTERPRISE_ADMINS
, WinAccountEnterpriseAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ENTERPRISE_ADMINS */ },
258 { SDDL_ENTERPRISE_DOMAIN_CONTROLLERS
, WinEnterpriseControllersSid
},
259 { SDDL_EVERYONE
, WinWorldSid
},
260 { SDDL_GROUP_POLICY_ADMINS
, WinAccountPolicyAdminsSid
/* FIXME: DOMAIN_GROUP_RID_POLICY_ADMINS */ },
261 { SDDL_INTERACTIVE
, WinInteractiveSid
},
262 { SDDL_LOCAL_ADMIN
, WinAccountAdministratorSid
/* FIXME: DOMAIN_USER_RID_ADMIN */ },
263 { SDDL_LOCAL_GUEST
, WinAccountGuestSid
/* FIXME: DOMAIN_USER_RID_GUEST */ },
264 { SDDL_LOCAL_SERVICE
, WinLocalServiceSid
},
265 { SDDL_LOCAL_SYSTEM
, WinLocalSystemSid
},
266 { SDDL_NETWORK
, WinNetworkSid
},
267 { SDDL_NETWORK_CONFIGURATION_OPS
, WinBuiltinNetworkConfigurationOperatorsSid
},
268 { SDDL_NETWORK_SERVICE
, WinNetworkServiceSid
},
269 { SDDL_PRINTER_OPERATORS
, WinBuiltinPrintOperatorsSid
},
270 { SDDL_PERSONAL_SELF
, WinSelfSid
},
271 { SDDL_POWER_USERS
, WinBuiltinPowerUsersSid
},
272 { SDDL_RAS_SERVERS
, WinAccountRasAndIasServersSid
/* FIXME: DOMAIN_ALIAS_RID_RAS_SERVERS */ },
273 { SDDL_REMOTE_DESKTOP
, WinBuiltinRemoteDesktopUsersSid
},
274 { SDDL_REPLICATOR
, WinBuiltinReplicatorSid
},
275 { SDDL_RESTRICTED_CODE
, WinRestrictedCodeSid
},
276 { SDDL_SCHEMA_ADMINISTRATORS
, WinAccountSchemaAdminsSid
/* FIXME: DOMAIN_GROUP_RID_SCHEMA_ADMINS */ },
277 { SDDL_SERVER_OPERATORS
, WinBuiltinSystemOperatorsSid
},
278 { SDDL_SERVICE
, WinServiceSid
},
282 /************************************************************
283 * ADVAPI_IsLocalComputer
285 * Checks whether the server name indicates local machine.
287 BOOL
ADVAPI_IsLocalComputer(LPCWSTR ServerName
)
289 DWORD dwSize
= MAX_COMPUTERNAME_LENGTH
+ 1;
293 if (!ServerName
|| !ServerName
[0])
296 buf
= heap_alloc(dwSize
* sizeof(WCHAR
));
297 Result
= GetComputerNameW(buf
, &dwSize
);
298 if (Result
&& (ServerName
[0] == '\\') && (ServerName
[1] == '\\'))
300 Result
= Result
&& !lstrcmpW(ServerName
, buf
);
306 /************************************************************
307 * ADVAPI_GetComputerSid
309 BOOL
ADVAPI_GetComputerSid(PSID sid
)
311 static const struct /* same fields as struct SID */
314 BYTE SubAuthorityCount
;
315 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
316 DWORD SubAuthority
[4];
318 { SID_REVISION
, 4, { SECURITY_NT_AUTHORITY
}, { SECURITY_NT_NON_UNIQUE
, 0, 0, 0 } };
320 memcpy( sid
, &computer_sid
, sizeof(computer_sid
) );
324 /* Exported functions */
330 OpenProcessToken(HANDLE ProcessHandle
,
336 TRACE("%p, %x, %p.\n", ProcessHandle
, DesiredAccess
, TokenHandle
);
338 Status
= NtOpenProcessToken(ProcessHandle
,
341 if (!NT_SUCCESS(Status
))
343 ERR("NtOpenProcessToken failed! Status %08x.\n", Status
);
344 SetLastError(RtlNtStatusToDosError(Status
));
348 TRACE("Returning token %p.\n", *TokenHandle
);
353 /******************************************************************************
354 * OpenThreadToken [ADVAPI32.@]
356 * Opens the access token associated with a thread handle.
359 * ThreadHandle [I] Handle to process
360 * DesiredAccess [I] Desired access to the thread
362 * TokenHandle [O] Destination for the token handle
365 * Success: TRUE. TokenHandle contains the access token.
369 * See NtOpenThreadToken.
372 OpenThreadToken( HANDLE ThreadHandle
, DWORD DesiredAccess
,
373 BOOL OpenAsSelf
, HANDLE
*TokenHandle
)
375 return set_ntstatus( NtOpenThreadToken(ThreadHandle
, DesiredAccess
, OpenAsSelf
, TokenHandle
));
382 AdjustTokenGroups(HANDLE TokenHandle
,
384 PTOKEN_GROUPS NewState
,
386 PTOKEN_GROUPS PreviousState
,
391 Status
= NtAdjustGroupsToken(TokenHandle
,
396 (PULONG
)ReturnLength
);
397 if (!NT_SUCCESS(Status
))
399 SetLastError(RtlNtStatusToDosError(Status
));
410 AdjustTokenPrivileges(HANDLE TokenHandle
,
411 BOOL DisableAllPrivileges
,
412 PTOKEN_PRIVILEGES NewState
,
414 PTOKEN_PRIVILEGES PreviousState
,
419 Status
= NtAdjustPrivilegesToken(TokenHandle
,
420 DisableAllPrivileges
,
424 (PULONG
)ReturnLength
);
425 if (STATUS_NOT_ALL_ASSIGNED
== Status
)
427 SetLastError(ERROR_NOT_ALL_ASSIGNED
);
431 if (!NT_SUCCESS(Status
))
433 SetLastError(RtlNtStatusToDosError(Status
));
437 /* AdjustTokenPrivileges is documented to do this */
438 SetLastError(ERROR_SUCCESS
);
447 GetTokenInformation(HANDLE TokenHandle
,
448 TOKEN_INFORMATION_CLASS TokenInformationClass
,
449 LPVOID TokenInformation
,
450 DWORD TokenInformationLength
,
455 Status
= NtQueryInformationToken(TokenHandle
,
456 TokenInformationClass
,
458 TokenInformationLength
,
459 (PULONG
)ReturnLength
);
460 if (!NT_SUCCESS(Status
))
462 SetLastError(RtlNtStatusToDosError(Status
));
473 SetTokenInformation(HANDLE TokenHandle
,
474 TOKEN_INFORMATION_CLASS TokenInformationClass
,
475 LPVOID TokenInformation
,
476 DWORD TokenInformationLength
)
480 Status
= NtSetInformationToken(TokenHandle
,
481 TokenInformationClass
,
483 TokenInformationLength
);
484 if (!NT_SUCCESS(Status
))
486 SetLastError(RtlNtStatusToDosError(Status
));
497 SetThreadToken(IN PHANDLE ThreadHandle OPTIONAL
,
498 IN HANDLE TokenHandle
)
503 hThread
= (ThreadHandle
!= NULL
) ? *ThreadHandle
: NtCurrentThread();
505 Status
= NtSetInformationThread(hThread
,
506 ThreadImpersonationToken
,
509 if (!NT_SUCCESS(Status
))
511 SetLastError(RtlNtStatusToDosError(Status
));
518 /*************************************************************************
519 * CreateRestrictedToken [ADVAPI32.@]
521 * Create a new more restricted token from an existing token.
524 * baseToken [I] Token to base the new restricted token on
526 * nDisableSids [I] Length of disableSids array
527 * disableSids [I] Array of SIDs to disable in the new token
528 * nDeletePrivs [I] Length of deletePrivs array
529 * deletePrivs [I] Array of privileges to delete in the new token
530 * nRestrictSids [I] Length of restrictSids array
531 * restrictSids [I] Array of SIDs to restrict in the new token
532 * newToken [O] Address where the new token is stored
538 BOOL WINAPI
CreateRestrictedToken(
542 PSID_AND_ATTRIBUTES disableSids
,
544 PLUID_AND_ATTRIBUTES deletePrivs
,
546 PSID_AND_ATTRIBUTES restrictSids
,
550 SECURITY_IMPERSONATION_LEVEL level
= TokenImpersonationLevel
;
553 FIXME("(%p, 0x%x, %u, %p, %u, %p, %u, %p, %p): stub\n",
554 baseToken
, flags
, nDisableSids
, disableSids
,
555 nDeletePrivs
, deletePrivs
,
556 nRestrictSids
, restrictSids
,
560 if (!GetTokenInformation( baseToken
, TokenType
, &type
, size
, &size
)) return FALSE
;
561 if (type
== TokenImpersonation
)
563 size
= sizeof(level
);
564 if (!GetTokenInformation( baseToken
, TokenImpersonationLevel
, &level
, size
, &size
))
567 return DuplicateTokenEx( baseToken
, MAXIMUM_ALLOWED
, NULL
, level
, type
, newToken
);
570 /******************************************************************************
571 * AllocateAndInitializeSid [ADVAPI32.@]
574 * pIdentifierAuthority []
575 * nSubAuthorityCount []
587 AllocateAndInitializeSid( PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
588 BYTE nSubAuthorityCount
,
589 DWORD nSubAuthority0
, DWORD nSubAuthority1
,
590 DWORD nSubAuthority2
, DWORD nSubAuthority3
,
591 DWORD nSubAuthority4
, DWORD nSubAuthority5
,
592 DWORD nSubAuthority6
, DWORD nSubAuthority7
,
595 return set_ntstatus( RtlAllocateAndInitializeSid(
596 pIdentifierAuthority
, nSubAuthorityCount
,
597 nSubAuthority0
, nSubAuthority1
, nSubAuthority2
, nSubAuthority3
,
598 nSubAuthority4
, nSubAuthority5
, nSubAuthority6
, nSubAuthority7
,
606 * Docs says this function does NOT return a value
607 * even thou it's defined to return a PVOID...
613 return RtlFreeSid(pSid
);
616 /******************************************************************************
617 * CopySid [ADVAPI32.@]
620 * nDestinationSidLength []
625 CopySid( DWORD nDestinationSidLength
, PSID pDestinationSid
, PSID pSourceSid
)
627 return set_ntstatus(RtlCopySid(nDestinationSidLength
, pDestinationSid
, pSourceSid
));
635 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
636 IN PSID DomainSid OPTIONAL
,
641 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
643 if (cbSid
== NULL
|| (DomainSid
&& !IsValidSid(DomainSid
)))
645 SetLastError(ERROR_INVALID_PARAMETER
);
649 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++) {
650 if (WellKnownSids
[i
].Type
== WellKnownSidType
) {
651 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
656 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
661 SetLastError(ERROR_INVALID_PARAMETER
);
664 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
670 if (DomainSid
== NULL
|| *GetSidSubAuthorityCount(DomainSid
) == SID_MAX_SUB_AUTHORITIES
)
672 SetLastError(ERROR_INVALID_PARAMETER
);
676 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
677 if (WellKnownRids
[i
].Type
== WellKnownSidType
) {
678 UCHAR domain_subauth
= *GetSidSubAuthorityCount(DomainSid
);
679 DWORD domain_sid_length
= GetSidLengthRequired(domain_subauth
);
680 DWORD output_sid_length
= GetSidLengthRequired(domain_subauth
+ 1);
682 if (*cbSid
< output_sid_length
)
684 *cbSid
= output_sid_length
;
685 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
690 SetLastError(ERROR_INVALID_PARAMETER
);
693 CopyMemory(pSid
, DomainSid
, domain_sid_length
);
694 (*GetSidSubAuthorityCount(pSid
))++;
695 (*GetSidSubAuthority(pSid
, domain_subauth
)) = WellKnownRids
[i
].Rid
;
696 *cbSid
= output_sid_length
;
700 SetLastError(ERROR_INVALID_PARAMETER
);
709 IsWellKnownSid(IN PSID pSid
,
710 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
713 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
715 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
717 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
719 if (EqualSid(pSid
, (PSID
)(&WellKnownSids
[i
].Sid
.Revision
)))
732 IsValidSid(PSID pSid
)
734 return (BOOL
)RtlValidSid(pSid
);
745 SetLastError(ERROR_SUCCESS
);
746 return RtlEqualSid (pSid1
, pSid2
);
754 EqualPrefixSid(PSID pSid1
,
757 return RtlEqualPrefixSid (pSid1
, pSid2
);
765 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
767 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
775 InitializeSid(PSID Sid
,
776 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
777 BYTE nSubAuthorityCount
)
781 Status
= RtlInitializeSid(Sid
,
782 pIdentifierAuthority
,
784 if (!NT_SUCCESS(Status
))
786 SetLastError(RtlNtStatusToDosError(Status
));
796 PSID_IDENTIFIER_AUTHORITY
798 GetSidIdentifierAuthority(PSID pSid
)
800 return RtlIdentifierAuthoritySid(pSid
);
808 GetSidSubAuthority(PSID pSid
,
811 SetLastError(ERROR_SUCCESS
);
812 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
820 GetSidSubAuthorityCount(PSID pSid
)
822 SetLastError(ERROR_SUCCESS
);
823 return RtlSubAuthorityCountSid(pSid
);
831 GetLengthSid(PSID pSid
)
833 return (DWORD
)RtlLengthSid(pSid
);
841 InitializeSecurityDescriptor(PSECURITY_DESCRIPTOR pSecurityDescriptor
,
846 Status
= RtlCreateSecurityDescriptor(pSecurityDescriptor
,
848 if (!NT_SUCCESS(Status
))
850 SetLastError(RtlNtStatusToDosError(Status
));
862 MakeAbsoluteSD(PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor
,
863 PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor
,
864 LPDWORD lpdwAbsoluteSecurityDescriptorSize
,
866 LPDWORD lpdwDaclSize
,
868 LPDWORD lpdwSaclSize
,
870 LPDWORD lpdwOwnerSize
,
872 LPDWORD lpdwPrimaryGroupSize
)
876 Status
= RtlSelfRelativeToAbsoluteSD(pSelfRelativeSecurityDescriptor
,
877 pAbsoluteSecurityDescriptor
,
878 lpdwAbsoluteSecurityDescriptorSize
,
886 lpdwPrimaryGroupSize
);
887 if (!NT_SUCCESS(Status
))
889 SetLastError(RtlNtStatusToDosError(Status
));
896 /******************************************************************************
897 * GetKernelObjectSecurity [ADVAPI32.@]
899 BOOL WINAPI
GetKernelObjectSecurity(
901 SECURITY_INFORMATION RequestedInformation
,
902 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
904 LPDWORD lpnLengthNeeded
)
906 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n", Handle
, RequestedInformation
,
907 pSecurityDescriptor
, nLength
, lpnLengthNeeded
);
909 return set_ntstatus( NtQuerySecurityObject(Handle
, RequestedInformation
, pSecurityDescriptor
,
910 nLength
, lpnLengthNeeded
));
918 InitializeAcl(PACL pAcl
,
924 Status
= RtlCreateAcl(pAcl
,
927 if (!NT_SUCCESS(Status
))
929 SetLastError(RtlNtStatusToDosError(Status
));
936 BOOL WINAPI
ImpersonateNamedPipeClient( HANDLE hNamedPipe
)
938 IO_STATUS_BLOCK io_block
;
940 TRACE("(%p)\n", hNamedPipe
);
942 return set_ntstatus( NtFsControlFile(hNamedPipe
, NULL
, NULL
, NULL
,
943 &io_block
, FSCTL_PIPE_IMPERSONATE
, NULL
, 0, NULL
, 0) );
951 AddAccessAllowedAce(PACL pAcl
,
958 Status
= RtlAddAccessAllowedAce(pAcl
,
962 if (!NT_SUCCESS(Status
))
964 SetLastError(RtlNtStatusToDosError(Status
));
975 AddAccessAllowedAceEx(PACL pAcl
,
983 Status
= RtlAddAccessAllowedAceEx(pAcl
,
988 if (!NT_SUCCESS(Status
))
990 SetLastError(RtlNtStatusToDosError(Status
));
1002 AddAccessDeniedAce(PACL pAcl
,
1003 DWORD dwAceRevision
,
1009 Status
= RtlAddAccessDeniedAce(pAcl
,
1013 if (!NT_SUCCESS(Status
))
1015 SetLastError(RtlNtStatusToDosError(Status
));
1026 AddAccessDeniedAceEx(PACL pAcl
,
1027 DWORD dwAceRevision
,
1034 Status
= RtlAddAccessDeniedAceEx(pAcl
,
1039 if (!NT_SUCCESS(Status
))
1041 SetLastError(RtlNtStatusToDosError(Status
));
1054 DWORD dwAceRevision
,
1055 DWORD dwStartingAceIndex
,
1057 DWORD nAceListLength
)
1061 Status
= RtlAddAce(pAcl
,
1066 if (!NT_SUCCESS(Status
))
1068 SetLastError(RtlNtStatusToDosError(Status
));
1075 /******************************************************************************
1076 * DeleteAce [ADVAPI32.@]
1078 BOOL WINAPI
DeleteAce(PACL pAcl
, DWORD dwAceIndex
)
1080 return set_ntstatus(RtlDeleteAce(pAcl
, dwAceIndex
));
1088 FindFirstFreeAce(PACL pAcl
,
1091 return RtlFirstFreeAce(pAcl
,
1107 Status
= RtlGetAce(pAcl
,
1110 if (!NT_SUCCESS(Status
))
1112 SetLastError(RtlNtStatusToDosError(Status
));
1124 GetAclInformation(PACL pAcl
,
1125 LPVOID pAclInformation
,
1126 DWORD nAclInformationLength
,
1127 ACL_INFORMATION_CLASS dwAclInformationClass
)
1131 Status
= RtlQueryInformationAcl(pAcl
,
1133 nAclInformationLength
,
1134 dwAclInformationClass
);
1135 if (!NT_SUCCESS(Status
))
1137 SetLastError(RtlNtStatusToDosError(Status
));
1149 IsValidAcl(PACL pAcl
)
1151 return RtlValidAcl (pAcl
);
1158 AllocateLocallyUniqueId(PLUID Luid
)
1162 Status
= NtAllocateLocallyUniqueId (Luid
);
1163 if (!NT_SUCCESS (Status
))
1165 SetLastError(RtlNtStatusToDosError(Status
));
1172 /**********************************************************************
1173 * LookupPrivilegeDisplayNameA EXPORTED
1179 LookupPrivilegeDisplayNameA(LPCSTR lpSystemName
,
1181 LPSTR lpDisplayName
,
1182 LPDWORD cbDisplayName
,
1183 LPDWORD lpLanguageId
)
1185 FIXME("%s() not implemented!\n", __FUNCTION__
);
1186 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
1191 /**********************************************************************
1192 * LookupPrivilegeDisplayNameW EXPORTED
1198 LookupPrivilegeDisplayNameW(LPCWSTR lpSystemName
,
1200 LPWSTR lpDisplayName
,
1201 LPDWORD cbDisplayName
,
1202 LPDWORD lpLanguageId
)
1204 FIXME("%s() not implemented!\n", __FUNCTION__
);
1205 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
1209 /**********************************************************************
1210 * LookupPrivilegeNameA EXPORTED
1216 LookupPrivilegeNameA(LPCSTR lpSystemName
,
1221 UNICODE_STRING lpSystemNameW
;
1225 TRACE("%s %p %p %p\n", debugstr_a(lpSystemName
), lpLuid
, lpName
, cchName
);
1227 RtlCreateUnicodeStringFromAsciiz(&lpSystemNameW
, lpSystemName
);
1228 ret
= LookupPrivilegeNameW(lpSystemNameW
.Buffer
, lpLuid
, NULL
, &wLen
);
1229 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1231 LPWSTR lpNameW
= HeapAlloc(GetProcessHeap(), 0, wLen
* sizeof(WCHAR
));
1233 ret
= LookupPrivilegeNameW(lpSystemNameW
.Buffer
, lpLuid
, lpNameW
,
1237 /* Windows crashes if cchName is NULL, so will I */
1238 unsigned int len
= WideCharToMultiByte(CP_ACP
, 0, lpNameW
, -1, lpName
,
1239 *cchName
, NULL
, NULL
);
1243 /* WideCharToMultiByte failed */
1246 else if (len
> *cchName
)
1249 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1254 /* WideCharToMultiByte succeeded, output length needs to be
1255 * length not including NULL terminator
1260 HeapFree(GetProcessHeap(), 0, lpNameW
);
1262 RtlFreeUnicodeString(&lpSystemNameW
);
1266 /******************************************************************************
1267 * GetFileSecurityA [ADVAPI32.@]
1269 * Obtains Specified information about the security of a file or directory.
1272 * lpFileName [I] Name of the file to get info for
1273 * RequestedInformation [I] SE_ flags from "winnt.h"
1274 * pSecurityDescriptor [O] Destination for security information
1275 * nLength [I] Length of pSecurityDescriptor
1276 * lpnLengthNeeded [O] Destination for length of returned security information
1279 * Success: TRUE. pSecurityDescriptor contains the requested information.
1280 * Failure: FALSE. lpnLengthNeeded contains the required space to return the info.
1283 * The information returned is constrained by the callers access rights and
1290 GetFileSecurityA(LPCSTR lpFileName
,
1291 SECURITY_INFORMATION RequestedInformation
,
1292 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1294 LPDWORD lpnLengthNeeded
)
1296 UNICODE_STRING FileName
;
1299 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
1301 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1305 bResult
= GetFileSecurityW(FileName
.Buffer
,
1306 RequestedInformation
,
1307 pSecurityDescriptor
,
1311 RtlFreeUnicodeString(&FileName
);
1321 GetFileSecurityW(LPCWSTR lpFileName
,
1322 SECURITY_INFORMATION RequestedInformation
,
1323 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1325 LPDWORD lpnLengthNeeded
)
1327 OBJECT_ATTRIBUTES ObjectAttributes
;
1328 IO_STATUS_BLOCK StatusBlock
;
1329 UNICODE_STRING FileName
;
1330 ULONG AccessMask
= 0;
1334 TRACE("GetFileSecurityW() called\n");
1336 QuerySecurityAccessMask(RequestedInformation
, &AccessMask
);
1338 if (!RtlDosPathNameToNtPathName_U(lpFileName
,
1343 ERR("Invalid path\n");
1344 SetLastError(ERROR_INVALID_NAME
);
1348 InitializeObjectAttributes(&ObjectAttributes
,
1350 OBJ_CASE_INSENSITIVE
,
1354 Status
= NtOpenFile(&FileHandle
,
1358 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1361 RtlFreeHeap(RtlGetProcessHeap(),
1365 if (!NT_SUCCESS(Status
))
1367 ERR("NtOpenFile() failed (Status %lx)\n", Status
);
1368 SetLastError(RtlNtStatusToDosError(Status
));
1372 Status
= NtQuerySecurityObject(FileHandle
,
1373 RequestedInformation
,
1374 pSecurityDescriptor
,
1377 NtClose(FileHandle
);
1378 if (!NT_SUCCESS(Status
))
1380 ERR("NtQuerySecurityObject() failed (Status %lx)\n", Status
);
1381 SetLastError(RtlNtStatusToDosError(Status
));
1388 /******************************************************************************
1389 * SetFileSecurityA [ADVAPI32.@]
1390 * Sets the security of a file or directory
1396 SetFileSecurityA(LPCSTR lpFileName
,
1397 SECURITY_INFORMATION SecurityInformation
,
1398 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
1400 UNICODE_STRING FileName
;
1403 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
1405 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1409 bResult
= SetFileSecurityW(FileName
.Buffer
,
1410 SecurityInformation
,
1411 pSecurityDescriptor
);
1413 RtlFreeUnicodeString(&FileName
);
1418 /******************************************************************************
1419 * SetFileSecurityW [ADVAPI32.@]
1420 * Sets the security of a file or directory
1426 SetFileSecurityW(LPCWSTR lpFileName
,
1427 SECURITY_INFORMATION SecurityInformation
,
1428 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
1430 OBJECT_ATTRIBUTES ObjectAttributes
;
1431 IO_STATUS_BLOCK StatusBlock
;
1432 UNICODE_STRING FileName
;
1433 ULONG AccessMask
= 0;
1437 TRACE("SetFileSecurityW() called\n");
1439 SetSecurityAccessMask(SecurityInformation
, &AccessMask
);
1441 if (!RtlDosPathNameToNtPathName_U(lpFileName
,
1446 ERR("Invalid path\n");
1447 SetLastError(ERROR_INVALID_NAME
);
1451 InitializeObjectAttributes(&ObjectAttributes
,
1453 OBJ_CASE_INSENSITIVE
,
1457 Status
= NtOpenFile(&FileHandle
,
1461 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1464 RtlFreeHeap(RtlGetProcessHeap(),
1468 if (!NT_SUCCESS(Status
))
1470 ERR("NtOpenFile() failed (Status %lx)\n", Status
);
1471 SetLastError(RtlNtStatusToDosError(Status
));
1475 Status
= NtSetSecurityObject(FileHandle
,
1476 SecurityInformation
,
1477 pSecurityDescriptor
);
1478 NtClose(FileHandle
);
1480 if (!NT_SUCCESS(Status
))
1482 ERR("NtSetSecurityObject() failed (Status %lx)\n", Status
);
1483 SetLastError(RtlNtStatusToDosError(Status
));
1490 /******************************************************************************
1491 * QueryWindows31FilesMigration [ADVAPI32.@]
1497 QueryWindows31FilesMigration( DWORD x1
)
1499 FIXME("(%d):stub\n",x1
);
1503 /******************************************************************************
1504 * SynchronizeWindows31FilesAndWindowsNTRegistry [ADVAPI32.@]
1513 SynchronizeWindows31FilesAndWindowsNTRegistry( DWORD x1
, DWORD x2
, DWORD x3
,
1516 FIXME("(0x%08x,0x%08x,0x%08x,0x%08x):stub\n",x1
,x2
,x3
,x4
);
1528 HANDLE Token
= NULL
;
1530 Status
= NtSetInformationThread(NtCurrentThread(),
1531 ThreadImpersonationToken
,
1534 if (!NT_SUCCESS(Status
))
1536 SetLastError(RtlNtStatusToDosError(Status
));
1548 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
1552 Status
= RtlImpersonateSelf(ImpersonationLevel
);
1553 if (!NT_SUCCESS(Status
))
1555 SetLastError(RtlNtStatusToDosError(Status
));
1567 AccessCheck(IN PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1568 IN HANDLE ClientToken
,
1569 IN DWORD DesiredAccess
,
1570 IN PGENERIC_MAPPING GenericMapping
,
1571 OUT PPRIVILEGE_SET PrivilegeSet OPTIONAL
,
1572 IN OUT LPDWORD PrivilegeSetLength
,
1573 OUT LPDWORD GrantedAccess
,
1574 OUT LPBOOL AccessStatus
)
1577 NTSTATUS NtAccessStatus
;
1579 /* Do the access check */
1580 Status
= NtAccessCheck(pSecurityDescriptor
,
1585 (PULONG
)PrivilegeSetLength
,
1586 (PACCESS_MASK
)GrantedAccess
,
1589 /* See if the access check operation succeeded */
1590 if (!NT_SUCCESS(Status
))
1593 SetLastError(RtlNtStatusToDosError(Status
));
1597 /* Now check the access status */
1598 if (!NT_SUCCESS(NtAccessStatus
))
1601 SetLastError(RtlNtStatusToDosError(NtAccessStatus
));
1602 *AccessStatus
= FALSE
;
1606 /* Access granted */
1607 *AccessStatus
= TRUE
;
1610 /* Check succeeded */
1617 BOOL WINAPI
AccessCheckByType(
1618 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1619 PSID PrincipalSelfSid
,
1621 DWORD DesiredAccess
,
1622 POBJECT_TYPE_LIST ObjectTypeList
,
1623 DWORD ObjectTypeListLength
,
1624 PGENERIC_MAPPING GenericMapping
,
1625 PPRIVILEGE_SET PrivilegeSet
,
1626 LPDWORD PrivilegeSetLength
,
1627 LPDWORD GrantedAccess
,
1628 LPBOOL AccessStatus
)
1632 *AccessStatus
= TRUE
;
1634 return !*AccessStatus
;
1642 SetKernelObjectSecurity(HANDLE Handle
,
1643 SECURITY_INFORMATION SecurityInformation
,
1644 PSECURITY_DESCRIPTOR SecurityDescriptor
)
1648 Status
= NtSetSecurityObject(Handle
,
1649 SecurityInformation
,
1650 SecurityDescriptor
);
1651 if (!NT_SUCCESS(Status
))
1653 SetLastError(RtlNtStatusToDosError(Status
));
1665 AddAuditAccessAce(PACL pAcl
,
1666 DWORD dwAceRevision
,
1674 Status
= RtlAddAuditAccessAce(pAcl
,
1680 if (!NT_SUCCESS(Status
))
1682 SetLastError(RtlNtStatusToDosError(Status
));
1693 AddAuditAccessAceEx(PACL pAcl
,
1694 DWORD dwAceRevision
,
1703 Status
= RtlAddAuditAccessAceEx(pAcl
,
1710 if (!NT_SUCCESS(Status
))
1712 SetLastError(RtlNtStatusToDosError(Status
));
1719 /******************************************************************************
1720 * LookupAccountNameA [ADVAPI32.@]
1726 LookupAccountNameA(LPCSTR SystemName
,
1730 LPSTR ReferencedDomainName
,
1731 LPDWORD hReferencedDomainNameLength
,
1732 PSID_NAME_USE SidNameUse
)
1735 UNICODE_STRING lpSystemW
;
1736 UNICODE_STRING lpAccountW
;
1737 LPWSTR lpReferencedDomainNameW
= NULL
;
1739 RtlCreateUnicodeStringFromAsciiz(&lpSystemW
, SystemName
);
1740 RtlCreateUnicodeStringFromAsciiz(&lpAccountW
, AccountName
);
1742 if (ReferencedDomainName
)
1743 lpReferencedDomainNameW
= HeapAlloc(GetProcessHeap(),
1745 *hReferencedDomainNameLength
* sizeof(WCHAR
));
1747 ret
= LookupAccountNameW(lpSystemW
.Buffer
,
1751 lpReferencedDomainNameW
,
1752 hReferencedDomainNameLength
,
1755 if (ret
&& lpReferencedDomainNameW
)
1757 WideCharToMultiByte(CP_ACP
,
1759 lpReferencedDomainNameW
,
1760 *hReferencedDomainNameLength
+ 1,
1761 ReferencedDomainName
,
1762 *hReferencedDomainNameLength
+ 1,
1767 RtlFreeUnicodeString(&lpSystemW
);
1768 RtlFreeUnicodeString(&lpAccountW
);
1769 HeapFree(GetProcessHeap(), 0, lpReferencedDomainNameW
);
1774 /**********************************************************************
1775 * PrivilegeCheck EXPORTED
1780 PrivilegeCheck(HANDLE ClientToken
,
1781 PPRIVILEGE_SET RequiredPrivileges
,
1787 Status
= NtPrivilegeCheck(ClientToken
,
1790 if (!NT_SUCCESS(Status
))
1792 SetLastError(RtlNtStatusToDosError(Status
));
1796 *pfResult
= (BOOL
)Result
;
1801 /******************************************************************************
1802 * GetSecurityInfoExW EXPORTED
1806 GetSecurityInfoExA(HANDLE hObject
,
1807 SE_OBJECT_TYPE ObjectType
,
1808 SECURITY_INFORMATION SecurityInfo
,
1811 PACTRL_ACCESSA
*ppAccessList
,
1812 PACTRL_AUDITA
*ppAuditList
,
1816 FIXME("%s() not implemented!\n", __FUNCTION__
);
1817 return ERROR_BAD_PROVIDER
;
1821 /******************************************************************************
1822 * GetSecurityInfoExW EXPORTED
1826 GetSecurityInfoExW(HANDLE hObject
,
1827 SE_OBJECT_TYPE ObjectType
,
1828 SECURITY_INFORMATION SecurityInfo
,
1831 PACTRL_ACCESSW
*ppAccessList
,
1832 PACTRL_AUDITW
*ppAuditList
,
1836 FIXME("%s() not implemented!\n", __FUNCTION__
);
1837 return ERROR_BAD_PROVIDER
;
1840 /******************************************************************************
1841 * BuildExplicitAccessWithNameA [ADVAPI32.@]
1844 BuildExplicitAccessWithNameA(PEXPLICIT_ACCESSA pExplicitAccess
,
1846 DWORD AccessPermissions
,
1847 ACCESS_MODE AccessMode
,
1850 pExplicitAccess
->grfAccessPermissions
= AccessPermissions
;
1851 pExplicitAccess
->grfAccessMode
= AccessMode
;
1852 pExplicitAccess
->grfInheritance
= Inheritance
;
1854 pExplicitAccess
->Trustee
.pMultipleTrustee
= NULL
;
1855 pExplicitAccess
->Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1856 pExplicitAccess
->Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
1857 pExplicitAccess
->Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1858 pExplicitAccess
->Trustee
.ptstrName
= pTrusteeName
;
1862 /******************************************************************************
1863 * BuildExplicitAccessWithNameW [ADVAPI32.@]
1866 BuildExplicitAccessWithNameW(PEXPLICIT_ACCESSW pExplicitAccess
,
1867 LPWSTR pTrusteeName
,
1868 DWORD AccessPermissions
,
1869 ACCESS_MODE AccessMode
,
1872 pExplicitAccess
->grfAccessPermissions
= AccessPermissions
;
1873 pExplicitAccess
->grfAccessMode
= AccessMode
;
1874 pExplicitAccess
->grfInheritance
= Inheritance
;
1876 pExplicitAccess
->Trustee
.pMultipleTrustee
= NULL
;
1877 pExplicitAccess
->Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1878 pExplicitAccess
->Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
1879 pExplicitAccess
->Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1880 pExplicitAccess
->Trustee
.ptstrName
= pTrusteeName
;
1883 /******************************************************************************
1884 * BuildTrusteeWithObjectsAndNameA [ADVAPI32.@]
1886 VOID WINAPI
BuildTrusteeWithObjectsAndNameA( PTRUSTEEA pTrustee
, POBJECTS_AND_NAME_A pObjName
,
1887 SE_OBJECT_TYPE ObjectType
, LPSTR ObjectTypeName
,
1888 LPSTR InheritedObjectTypeName
, LPSTR Name
)
1890 DWORD ObjectsPresent
= 0;
1892 TRACE("%p %p 0x%08x %p %p %s\n", pTrustee
, pObjName
,
1893 ObjectType
, ObjectTypeName
, InheritedObjectTypeName
, debugstr_a(Name
));
1895 /* Fill the OBJECTS_AND_NAME structure */
1896 pObjName
->ObjectType
= ObjectType
;
1897 if (ObjectTypeName
!= NULL
)
1899 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1902 pObjName
->InheritedObjectTypeName
= InheritedObjectTypeName
;
1903 if (InheritedObjectTypeName
!= NULL
)
1905 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1908 pObjName
->ObjectsPresent
= ObjectsPresent
;
1909 pObjName
->ptstrName
= Name
;
1911 /* Fill the TRUSTEE structure */
1912 pTrustee
->pMultipleTrustee
= NULL
;
1913 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1914 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_NAME
;
1915 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1916 pTrustee
->ptstrName
= (LPSTR
)pObjName
;
1919 /******************************************************************************
1920 * BuildTrusteeWithObjectsAndNameW [ADVAPI32.@]
1922 VOID WINAPI
BuildTrusteeWithObjectsAndNameW( PTRUSTEEW pTrustee
, POBJECTS_AND_NAME_W pObjName
,
1923 SE_OBJECT_TYPE ObjectType
, LPWSTR ObjectTypeName
,
1924 LPWSTR InheritedObjectTypeName
, LPWSTR Name
)
1926 DWORD ObjectsPresent
= 0;
1928 TRACE("%p %p 0x%08x %p %p %s\n", pTrustee
, pObjName
,
1929 ObjectType
, ObjectTypeName
, InheritedObjectTypeName
, debugstr_w(Name
));
1931 /* Fill the OBJECTS_AND_NAME structure */
1932 pObjName
->ObjectType
= ObjectType
;
1933 if (ObjectTypeName
!= NULL
)
1935 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1938 pObjName
->InheritedObjectTypeName
= InheritedObjectTypeName
;
1939 if (InheritedObjectTypeName
!= NULL
)
1941 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1944 pObjName
->ObjectsPresent
= ObjectsPresent
;
1945 pObjName
->ptstrName
= Name
;
1947 /* Fill the TRUSTEE structure */
1948 pTrustee
->pMultipleTrustee
= NULL
;
1949 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1950 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_NAME
;
1951 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1952 pTrustee
->ptstrName
= (LPWSTR
)pObjName
;
1955 /******************************************************************************
1956 * BuildTrusteeWithObjectsAndSidA [ADVAPI32.@]
1959 BuildTrusteeWithObjectsAndSidA(PTRUSTEEA pTrustee
,
1960 POBJECTS_AND_SID pObjSid
,
1962 GUID
*pInheritedObjectGuid
,
1965 DWORD ObjectsPresent
= 0;
1967 TRACE("%p %p %p %p %p\n", pTrustee
, pObjSid
, pObjectGuid
, pInheritedObjectGuid
, pSid
);
1969 /* Fill the OBJECTS_AND_SID structure */
1970 if (pObjectGuid
!= NULL
)
1972 pObjSid
->ObjectTypeGuid
= *pObjectGuid
;
1973 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1977 ZeroMemory(&pObjSid
->ObjectTypeGuid
,
1981 if (pInheritedObjectGuid
!= NULL
)
1983 pObjSid
->InheritedObjectTypeGuid
= *pInheritedObjectGuid
;
1984 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1988 ZeroMemory(&pObjSid
->InheritedObjectTypeGuid
,
1992 pObjSid
->ObjectsPresent
= ObjectsPresent
;
1993 pObjSid
->pSid
= pSid
;
1995 /* Fill the TRUSTEE structure */
1996 pTrustee
->pMultipleTrustee
= NULL
;
1997 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1998 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_SID
;
1999 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2000 pTrustee
->ptstrName
= (LPSTR
) pObjSid
;
2004 /******************************************************************************
2005 * BuildTrusteeWithObjectsAndSidW [ADVAPI32.@]
2008 BuildTrusteeWithObjectsAndSidW(PTRUSTEEW pTrustee
,
2009 POBJECTS_AND_SID pObjSid
,
2011 GUID
*pInheritedObjectGuid
,
2014 DWORD ObjectsPresent
= 0;
2016 TRACE("%p %p %p %p %p\n", pTrustee
, pObjSid
, pObjectGuid
, pInheritedObjectGuid
, pSid
);
2018 /* Fill the OBJECTS_AND_SID structure */
2019 if (pObjectGuid
!= NULL
)
2021 pObjSid
->ObjectTypeGuid
= *pObjectGuid
;
2022 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
2026 ZeroMemory(&pObjSid
->ObjectTypeGuid
,
2030 if (pInheritedObjectGuid
!= NULL
)
2032 pObjSid
->InheritedObjectTypeGuid
= *pInheritedObjectGuid
;
2033 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
2037 ZeroMemory(&pObjSid
->InheritedObjectTypeGuid
,
2041 pObjSid
->ObjectsPresent
= ObjectsPresent
;
2042 pObjSid
->pSid
= pSid
;
2044 /* Fill the TRUSTEE structure */
2045 pTrustee
->pMultipleTrustee
= NULL
;
2046 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2047 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_SID
;
2048 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2049 pTrustee
->ptstrName
= (LPWSTR
) pObjSid
;
2052 /******************************************************************************
2053 * BuildTrusteeWithSidA [ADVAPI32.@]
2056 BuildTrusteeWithSidA(PTRUSTEE_A pTrustee
,
2059 TRACE("%p %p\n", pTrustee
, pSid
);
2061 pTrustee
->pMultipleTrustee
= NULL
;
2062 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2063 pTrustee
->TrusteeForm
= TRUSTEE_IS_SID
;
2064 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2065 pTrustee
->ptstrName
= (LPSTR
) pSid
;
2069 /******************************************************************************
2070 * BuildTrusteeWithSidW [ADVAPI32.@]
2073 BuildTrusteeWithSidW(PTRUSTEE_W pTrustee
,
2076 TRACE("%p %p\n", pTrustee
, pSid
);
2078 pTrustee
->pMultipleTrustee
= NULL
;
2079 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2080 pTrustee
->TrusteeForm
= TRUSTEE_IS_SID
;
2081 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2082 pTrustee
->ptstrName
= (LPWSTR
) pSid
;
2085 /******************************************************************************
2086 * BuildTrusteeWithNameA [ADVAPI32.@]
2089 BuildTrusteeWithNameA(PTRUSTEE_A pTrustee
,
2092 TRACE("%p %s\n", pTrustee
, name
);
2094 pTrustee
->pMultipleTrustee
= NULL
;
2095 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2096 pTrustee
->TrusteeForm
= TRUSTEE_IS_NAME
;
2097 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2098 pTrustee
->ptstrName
= name
;
2101 /******************************************************************************
2102 * BuildTrusteeWithNameW [ADVAPI32.@]
2105 BuildTrusteeWithNameW(PTRUSTEE_W pTrustee
,
2108 TRACE("%p %s\n", pTrustee
, name
);
2110 pTrustee
->pMultipleTrustee
= NULL
;
2111 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2112 pTrustee
->TrusteeForm
= TRUSTEE_IS_NAME
;
2113 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2114 pTrustee
->ptstrName
= name
;
2117 /******************************************************************************
2118 * GetTrusteeFormW [ADVAPI32.@]
2121 GetTrusteeFormA(PTRUSTEE_A pTrustee
)
2123 return pTrustee
->TrusteeForm
;
2127 /******************************************************************************
2128 * GetTrusteeFormW [ADVAPI32.@]
2131 GetTrusteeFormW(PTRUSTEE_W pTrustee
)
2133 return pTrustee
->TrusteeForm
;
2136 /******************************************************************************
2137 * GetTrusteeNameA [ADVAPI32.@]
2140 GetTrusteeNameA(PTRUSTEE_A pTrustee
)
2142 return pTrustee
->ptstrName
;
2146 /******************************************************************************
2147 * GetTrusteeNameW [ADVAPI32.@]
2150 GetTrusteeNameW(PTRUSTEE_W pTrustee
)
2152 return pTrustee
->ptstrName
;
2155 /******************************************************************************
2156 * GetTrusteeTypeA [ADVAPI32.@]
2159 GetTrusteeTypeA(PTRUSTEE_A pTrustee
)
2161 return pTrustee
->TrusteeType
;
2164 /******************************************************************************
2165 * GetTrusteeTypeW [ADVAPI32.@]
2168 GetTrusteeTypeW(PTRUSTEE_W pTrustee
)
2170 return pTrustee
->TrusteeType
;
2178 SetAclInformation(PACL pAcl
,
2179 LPVOID pAclInformation
,
2180 DWORD nAclInformationLength
,
2181 ACL_INFORMATION_CLASS dwAclInformationClass
)
2185 Status
= RtlSetInformationAcl(pAcl
,
2187 nAclInformationLength
,
2188 dwAclInformationClass
);
2189 if (!NT_SUCCESS(Status
))
2191 SetLastError(RtlNtStatusToDosError(Status
));
2198 /**********************************************************************
2199 * SetNamedSecurityInfoA EXPORTED
2205 SetNamedSecurityInfoA(LPSTR pObjectName
,
2206 SE_OBJECT_TYPE ObjectType
,
2207 SECURITY_INFORMATION SecurityInfo
,
2213 UNICODE_STRING ObjectName
;
2216 if (!RtlCreateUnicodeStringFromAsciiz(&ObjectName
, pObjectName
))
2218 return ERROR_NOT_ENOUGH_MEMORY
;
2221 Ret
= SetNamedSecurityInfoW(ObjectName
.Buffer
,
2229 RtlFreeUnicodeString(&ObjectName
);
2239 AreAllAccessesGranted(DWORD GrantedAccess
,
2240 DWORD DesiredAccess
)
2242 return (BOOL
)RtlAreAllAccessesGranted(GrantedAccess
,
2251 AreAnyAccessesGranted(DWORD GrantedAccess
,
2252 DWORD DesiredAccess
)
2254 return (BOOL
)RtlAreAnyAccessesGranted(GrantedAccess
,
2258 /******************************************************************************
2259 * ParseAclStringFlags
2261 static DWORD
ParseAclStringFlags(LPCWSTR
* StringAcl
)
2264 LPCWSTR szAcl
= *StringAcl
;
2266 while (*szAcl
!= '(')
2270 flags
|= SE_DACL_PROTECTED
;
2272 else if (*szAcl
== 'A')
2276 flags
|= SE_DACL_AUTO_INHERIT_REQ
;
2277 else if (*szAcl
== 'I')
2278 flags
|= SE_DACL_AUTO_INHERITED
;
2287 /******************************************************************************
2288 * ParseAceStringType
2290 static const ACEFLAG AceType
[] =
2292 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
2293 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
2294 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
2295 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
2297 { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
2298 { SDDL_OBJECT_ACCESS_DENIED, ACCESS_DENIED_OBJECT_ACE_TYPE },
2299 { SDDL_OBJECT_ALARM, SYSTEM_ALARM_OBJECT_ACE_TYPE },
2300 { SDDL_OBJECT_AUDIT, SYSTEM_AUDIT_OBJECT_ACE_TYPE },
2305 static BYTE
ParseAceStringType(LPCWSTR
* StringAcl
)
2308 LPCWSTR szAcl
= *StringAcl
;
2309 const ACEFLAG
*lpaf
= AceType
;
2311 while (*szAcl
== ' ')
2314 while (lpaf
->wstr
&&
2315 (len
= strlenW(lpaf
->wstr
)) &&
2316 strncmpW(lpaf
->wstr
, szAcl
, len
))
2322 *StringAcl
= szAcl
+ len
;
2327 /******************************************************************************
2328 * ParseAceStringFlags
2330 static const ACEFLAG AceFlags
[] =
2332 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
2333 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
2334 { SDDL_INHERITED
, INHERITED_ACE
},
2335 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
2336 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
2337 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
2338 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
2342 static BYTE
ParseAceStringFlags(LPCWSTR
* StringAcl
)
2346 LPCWSTR szAcl
= *StringAcl
;
2348 while (*szAcl
== ' ')
2351 while (*szAcl
!= ';')
2353 const ACEFLAG
*lpaf
= AceFlags
;
2355 while (lpaf
->wstr
&&
2356 (len
= strlenW(lpaf
->wstr
)) &&
2357 strncmpW(lpaf
->wstr
, szAcl
, len
))
2363 flags
|= lpaf
->value
;
2372 /******************************************************************************
2373 * ParseAceStringRights
2375 static const ACEFLAG AceRights
[] =
2377 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
2378 { SDDL_GENERIC_READ
, GENERIC_READ
},
2379 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
2380 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
2382 { SDDL_READ_CONTROL
, READ_CONTROL
},
2383 { SDDL_STANDARD_DELETE
, DELETE
},
2384 { SDDL_WRITE_DAC
, WRITE_DAC
},
2385 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
2387 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
2388 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
2389 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
2390 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
2391 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
2392 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
2393 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
2394 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
2395 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
2397 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
2398 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
2399 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
2400 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
2402 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
2403 { SDDL_KEY_READ
, KEY_READ
},
2404 { SDDL_KEY_WRITE
, KEY_WRITE
},
2405 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
2409 static DWORD
ParseAceStringRights(LPCWSTR
* StringAcl
)
2413 LPCWSTR szAcl
= *StringAcl
;
2415 while (*szAcl
== ' ')
2418 if ((*szAcl
== '0') && (*(szAcl
+ 1) == 'x'))
2422 while (*p
&& *p
!= ';')
2425 if (p
- szAcl
<= 10 /* 8 hex digits + "0x" */ )
2427 rights
= strtoulW(szAcl
, NULL
, 16);
2431 WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl
, p
- szAcl
));
2435 while (*szAcl
!= ';')
2437 const ACEFLAG
*lpaf
= AceRights
;
2439 while (lpaf
->wstr
&&
2440 (len
= strlenW(lpaf
->wstr
)) &&
2441 strncmpW(lpaf
->wstr
, szAcl
, len
))
2449 rights
|= lpaf
->value
;
2459 /******************************************************************************
2460 * ParseStringAclToAcl
2462 * dacl_flags(string_ace1)(string_ace2)... (string_acen)
2464 static BOOL
ParseStringAclToAcl(LPCWSTR StringAcl
, LPDWORD lpdwFlags
,
2465 PACL pAcl
, LPDWORD cBytes
)
2469 DWORD length
= sizeof(ACL
);
2472 PACCESS_ALLOWED_ACE pAce
= NULL
; /* pointer to current ACE */
2473 DWORD error
= ERROR_INVALID_ACL
;
2475 TRACE("%s\n", debugstr_w(StringAcl
));
2480 if (pAcl
) /* pAce is only useful if we're setting values */
2481 pAce
= (PACCESS_ALLOWED_ACE
) (pAcl
+ 1);
2483 /* Parse ACL flags */
2484 *lpdwFlags
= ParseAclStringFlags(&StringAcl
);
2487 while (*StringAcl
== '(')
2491 /* Parse ACE type */
2492 val
= ParseAceStringType(&StringAcl
);
2494 pAce
->Header
.AceType
= (BYTE
) val
;
2495 if (*StringAcl
!= ';')
2497 error
= RPC_S_INVALID_STRING_UUID
;
2502 /* Parse ACE flags */
2503 val
= ParseAceStringFlags(&StringAcl
);
2505 pAce
->Header
.AceFlags
= (BYTE
) val
;
2506 if (*StringAcl
!= ';')
2510 /* Parse ACE rights */
2511 val
= ParseAceStringRights(&StringAcl
);
2514 if (*StringAcl
!= ';')
2518 /* Parse ACE object guid */
2519 while (*StringAcl
== ' ')
2521 if (*StringAcl
!= ';')
2523 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
2528 /* Parse ACE inherit object guid */
2529 while (*StringAcl
== ' ')
2531 if (*StringAcl
!= ';')
2533 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
2538 /* Parse ACE account sid */
2539 if (ParseStringSidToSid(StringAcl
, pAce
? &pAce
->SidStart
: NULL
, &sidlen
))
2541 while (*StringAcl
&& *StringAcl
!= ')')
2545 if (*StringAcl
!= ')')
2549 acesize
= sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
) + sidlen
;
2553 pAce
->Header
.AceSize
= acesize
;
2554 pAce
= (PACCESS_ALLOWED_ACE
)((LPBYTE
)pAce
+ acesize
);
2561 if (length
> 0xffff)
2563 ERR("ACL too large\n");
2569 pAcl
->AclRevision
= ACL_REVISION
;
2571 pAcl
->AclSize
= length
;
2572 pAcl
->AceCount
= acecount
++;
2578 SetLastError(error
);
2579 WARN("Invalid ACE string format\n");
2584 /******************************************************************************
2585 * ParseStringSecurityDescriptorToSecurityDescriptor
2587 static BOOL
ParseStringSecurityDescriptorToSecurityDescriptor(
2588 LPCWSTR StringSecurityDescriptor
,
2589 SECURITY_DESCRIPTOR_RELATIVE
* SecurityDescriptor
,
2594 WCHAR tok
[MAX_PATH
];
2596 LPBYTE lpNext
= NULL
;
2599 *cBytes
= sizeof(SECURITY_DESCRIPTOR
);
2601 if (SecurityDescriptor
)
2602 lpNext
= (LPBYTE
)(SecurityDescriptor
+ 1);
2604 while (*StringSecurityDescriptor
== ' ')
2605 StringSecurityDescriptor
++;
2607 while (*StringSecurityDescriptor
)
2609 toktype
= *StringSecurityDescriptor
;
2611 /* Expect char identifier followed by ':' */
2612 StringSecurityDescriptor
++;
2613 if (*StringSecurityDescriptor
!= ':')
2615 SetLastError(ERROR_INVALID_PARAMETER
);
2618 StringSecurityDescriptor
++;
2621 lptoken
= StringSecurityDescriptor
;
2622 while (*lptoken
&& *lptoken
!= ':')
2628 len
= lptoken
- StringSecurityDescriptor
;
2629 memcpy( tok
, StringSecurityDescriptor
, len
* sizeof(WCHAR
) );
2638 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
2641 if (SecurityDescriptor
)
2643 SecurityDescriptor
->Owner
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2644 lpNext
+= bytes
; /* Advance to next token */
2656 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
2659 if (SecurityDescriptor
)
2661 SecurityDescriptor
->Group
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2662 lpNext
+= bytes
; /* Advance to next token */
2675 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
2678 if (SecurityDescriptor
)
2680 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
| flags
;
2681 SecurityDescriptor
->Dacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2682 lpNext
+= bytes
; /* Advance to next token */
2695 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
2698 if (SecurityDescriptor
)
2700 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
| flags
;
2701 SecurityDescriptor
->Sacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2702 lpNext
+= bytes
; /* Advance to next token */
2711 FIXME("Unknown token\n");
2712 SetLastError(ERROR_INVALID_PARAMETER
);
2716 StringSecurityDescriptor
= lptoken
;
2725 /* Winehq cvs 20050916 */
2726 /******************************************************************************
2727 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
2732 ConvertStringSecurityDescriptorToSecurityDescriptorA(LPCSTR StringSecurityDescriptor
,
2733 DWORD StringSDRevision
,
2734 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
2735 PULONG SecurityDescriptorSize
)
2739 LPWSTR StringSecurityDescriptorW
;
2741 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
2742 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2744 if (StringSecurityDescriptorW
)
2746 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
2748 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
2749 StringSDRevision
, SecurityDescriptor
,
2750 SecurityDescriptorSize
);
2751 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
2757 /******************************************************************************
2758 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
2762 ConvertStringSecurityDescriptorToSecurityDescriptorW(LPCWSTR StringSecurityDescriptor
,
2763 DWORD StringSDRevision
,
2764 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
2765 PULONG SecurityDescriptorSize
)
2768 SECURITY_DESCRIPTOR
* psd
;
2771 TRACE("%s\n", debugstr_w(StringSecurityDescriptor
));
2773 if (GetVersion() & 0x80000000)
2775 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2778 else if (!StringSecurityDescriptor
|| !SecurityDescriptor
)
2780 SetLastError(ERROR_INVALID_PARAMETER
);
2783 else if (StringSDRevision
!= SID_REVISION
)
2785 SetLastError(ERROR_UNKNOWN_REVISION
);
2789 /* Compute security descriptor length */
2790 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
2794 psd
= *SecurityDescriptor
= LocalAlloc(GMEM_ZEROINIT
, cBytes
);
2795 if (!psd
) goto lend
;
2797 psd
->Revision
= SID_REVISION
;
2798 psd
->Control
|= SE_SELF_RELATIVE
;
2800 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
2801 (SECURITY_DESCRIPTOR_RELATIVE
*)psd
, &cBytes
))
2807 if (SecurityDescriptorSize
)
2808 *SecurityDescriptorSize
= cBytes
;
2813 TRACE(" ret=%d\n", bret
);
2817 static void DumpString(LPCWSTR string
, int cch
, WCHAR
**pwptr
, ULONG
*plen
)
2820 cch
= strlenW(string
);
2827 memcpy(*pwptr
, string
, sizeof(WCHAR
)*cch
);
2832 static BOOL
DumpSidNumeric(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
2835 WCHAR fmt
[] = { 'S','-','%','u','-','%','d',0 };
2836 WCHAR subauthfmt
[] = { '-','%','u',0 };
2840 if( !IsValidSid( psid
) || pisid
->Revision
!= SDDL_REVISION
)
2842 SetLastError(ERROR_INVALID_SID
);
2846 if (pisid
->IdentifierAuthority
.Value
[0] ||
2847 pisid
->IdentifierAuthority
.Value
[1])
2849 FIXME("not matching MS' bugs\n");
2850 SetLastError(ERROR_INVALID_SID
);
2854 sprintfW( buf
, fmt
, pisid
->Revision
,
2856 MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
2857 pisid
->IdentifierAuthority
.Value
[4] ),
2858 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
2859 pisid
->IdentifierAuthority
.Value
[2] )
2861 DumpString(buf
, -1, pwptr
, plen
);
2863 for( i
=0; i
<pisid
->SubAuthorityCount
; i
++ )
2865 sprintfW( buf
, subauthfmt
, pisid
->SubAuthority
[i
] );
2866 DumpString(buf
, -1, pwptr
, plen
);
2871 static BOOL
DumpSid(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
2874 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
2876 if (WellKnownSids
[i
].wstr
[0] && EqualSid(psid
, (PSID
)&(WellKnownSids
[i
].Sid
.Revision
)))
2878 DumpString(WellKnownSids
[i
].wstr
, 2, pwptr
, plen
);
2883 return DumpSidNumeric(psid
, pwptr
, plen
);
2886 static const LPCWSTR AceRightBitNames
[32] = {
2887 SDDL_CREATE_CHILD
, /* 0 */
2891 SDDL_READ_PROPERTY
, /* 4 */
2892 SDDL_WRITE_PROPERTY
,
2895 SDDL_CONTROL_ACCESS
, /* 8 */
2903 SDDL_STANDARD_DELETE
, /* 16 */
2915 SDDL_GENERIC_ALL
, /* 28 */
2916 SDDL_GENERIC_EXECUTE
,
2921 static void DumpRights(DWORD mask
, WCHAR
**pwptr
, ULONG
*plen
)
2923 static const WCHAR fmtW
[] = {'0','x','%','x',0};
2930 /* first check if the right have name */
2931 for (i
= 0; i
< sizeof(AceRights
)/sizeof(AceRights
[0]); i
++)
2933 if (AceRights
[i
].wstr
== NULL
)
2935 if (mask
== AceRights
[i
].value
)
2937 DumpString(AceRights
[i
].wstr
, -1, pwptr
, plen
);
2942 /* then check if it can be built from bit names */
2943 for (i
= 0; i
< 32; i
++)
2945 if ((mask
& (1 << i
)) && (AceRightBitNames
[i
] == NULL
))
2947 /* can't be built from bit names */
2948 sprintfW(buf
, fmtW
, mask
);
2949 DumpString(buf
, -1, pwptr
, plen
);
2954 /* build from bit names */
2955 for (i
= 0; i
< 32; i
++)
2956 if (mask
& (1 << i
))
2957 DumpString(AceRightBitNames
[i
], -1, pwptr
, plen
);
2960 static BOOL
DumpAce(LPVOID pace
, WCHAR
**pwptr
, ULONG
*plen
)
2962 ACCESS_ALLOWED_ACE
*piace
; /* all the supported ACEs have the same memory layout */
2963 static const WCHAR openbr
= '(';
2964 static const WCHAR closebr
= ')';
2965 static const WCHAR semicolon
= ';';
2967 if (((PACE_HEADER
)pace
)->AceType
> SYSTEM_ALARM_ACE_TYPE
|| ((PACE_HEADER
)pace
)->AceSize
< sizeof(ACCESS_ALLOWED_ACE
))
2969 SetLastError(ERROR_INVALID_ACL
);
2974 DumpString(&openbr
, 1, pwptr
, plen
);
2975 switch (piace
->Header
.AceType
)
2977 case ACCESS_ALLOWED_ACE_TYPE
:
2978 DumpString(SDDL_ACCESS_ALLOWED
, -1, pwptr
, plen
);
2980 case ACCESS_DENIED_ACE_TYPE
:
2981 DumpString(SDDL_ACCESS_DENIED
, -1, pwptr
, plen
);
2983 case SYSTEM_AUDIT_ACE_TYPE
:
2984 DumpString(SDDL_AUDIT
, -1, pwptr
, plen
);
2986 case SYSTEM_ALARM_ACE_TYPE
:
2987 DumpString(SDDL_ALARM
, -1, pwptr
, plen
);
2990 DumpString(&semicolon
, 1, pwptr
, plen
);
2992 if (piace
->Header
.AceFlags
& OBJECT_INHERIT_ACE
)
2993 DumpString(SDDL_OBJECT_INHERIT
, -1, pwptr
, plen
);
2994 if (piace
->Header
.AceFlags
& CONTAINER_INHERIT_ACE
)
2995 DumpString(SDDL_CONTAINER_INHERIT
, -1, pwptr
, plen
);
2996 if (piace
->Header
.AceFlags
& NO_PROPAGATE_INHERIT_ACE
)
2997 DumpString(SDDL_NO_PROPAGATE
, -1, pwptr
, plen
);
2998 if (piace
->Header
.AceFlags
& INHERIT_ONLY_ACE
)
2999 DumpString(SDDL_INHERIT_ONLY
, -1, pwptr
, plen
);
3000 if (piace
->Header
.AceFlags
& INHERITED_ACE
)
3001 DumpString(SDDL_INHERITED
, -1, pwptr
, plen
);
3002 if (piace
->Header
.AceFlags
& SUCCESSFUL_ACCESS_ACE_FLAG
)
3003 DumpString(SDDL_AUDIT_SUCCESS
, -1, pwptr
, plen
);
3004 if (piace
->Header
.AceFlags
& FAILED_ACCESS_ACE_FLAG
)
3005 DumpString(SDDL_AUDIT_FAILURE
, -1, pwptr
, plen
);
3006 DumpString(&semicolon
, 1, pwptr
, plen
);
3007 DumpRights(piace
->Mask
, pwptr
, plen
);
3008 DumpString(&semicolon
, 1, pwptr
, plen
);
3009 /* objects not supported */
3010 DumpString(&semicolon
, 1, pwptr
, plen
);
3011 /* objects not supported */
3012 DumpString(&semicolon
, 1, pwptr
, plen
);
3013 if (!DumpSid((PSID
)&piace
->SidStart
, pwptr
, plen
))
3015 DumpString(&closebr
, 1, pwptr
, plen
);
3019 static BOOL
DumpAcl(PACL pacl
, WCHAR
**pwptr
, ULONG
*plen
, BOOL
protected, BOOL autoInheritReq
, BOOL autoInherited
)
3025 DumpString(SDDL_PROTECTED
, -1, pwptr
, plen
);
3027 DumpString(SDDL_AUTO_INHERIT_REQ
, -1, pwptr
, plen
);
3029 DumpString(SDDL_AUTO_INHERITED
, -1, pwptr
, plen
);
3034 if (!IsValidAcl(pacl
))
3037 count
= pacl
->AceCount
;
3038 for (i
= 0; i
< count
; i
++)
3041 if (!GetAce(pacl
, i
, &ace
))
3043 if (!DumpAce(ace
, pwptr
, plen
))
3050 static BOOL
DumpOwner(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3052 static const WCHAR prefix
[] = {'O',':',0};
3056 if (!GetSecurityDescriptorOwner(SecurityDescriptor
, &psid
, &bDefaulted
))
3062 DumpString(prefix
, -1, pwptr
, plen
);
3063 if (!DumpSid(psid
, pwptr
, plen
))
3068 static BOOL
DumpGroup(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3070 static const WCHAR prefix
[] = {'G',':',0};
3074 if (!GetSecurityDescriptorGroup(SecurityDescriptor
, &psid
, &bDefaulted
))
3080 DumpString(prefix
, -1, pwptr
, plen
);
3081 if (!DumpSid(psid
, pwptr
, plen
))
3086 static BOOL
DumpDacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3088 static const WCHAR dacl
[] = {'D',':',0};
3089 SECURITY_DESCRIPTOR_CONTROL control
;
3090 BOOL present
, defaulted
;
3094 if (!GetSecurityDescriptorDacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
3097 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
3103 DumpString(dacl
, 2, pwptr
, plen
);
3104 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_DACL_PROTECTED
, control
& SE_DACL_AUTO_INHERIT_REQ
, control
& SE_DACL_AUTO_INHERITED
))
3109 static BOOL
DumpSacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3111 static const WCHAR sacl
[] = {'S',':',0};
3112 SECURITY_DESCRIPTOR_CONTROL control
;
3113 BOOL present
, defaulted
;
3117 if (!GetSecurityDescriptorSacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
3120 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
3126 DumpString(sacl
, 2, pwptr
, plen
);
3127 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_SACL_PROTECTED
, control
& SE_SACL_AUTO_INHERIT_REQ
, control
& SE_SACL_AUTO_INHERITED
))
3132 /******************************************************************************
3133 * ConvertSecurityDescriptorToStringSecurityDescriptorW [ADVAPI32.@]
3137 ConvertSecurityDescriptorToStringSecurityDescriptorW(PSECURITY_DESCRIPTOR SecurityDescriptor
,
3139 SECURITY_INFORMATION SecurityInformation
,
3140 LPWSTR
*OutputString
,
3146 if (SDRevision
!= SDDL_REVISION_1
)
3148 ERR("Program requested unknown SDDL revision %d\n", SDRevision
);
3149 SetLastError(ERROR_UNKNOWN_REVISION
);
3154 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
3155 if (!DumpOwner(SecurityDescriptor
, NULL
, &len
))
3157 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
3158 if (!DumpGroup(SecurityDescriptor
, NULL
, &len
))
3160 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
3161 if (!DumpDacl(SecurityDescriptor
, NULL
, &len
))
3163 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
3164 if (!DumpSacl(SecurityDescriptor
, NULL
, &len
))
3167 wstr
= wptr
= LocalAlloc(0, (len
+ 1)*sizeof(WCHAR
));
3171 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
3172 if (!DumpOwner(SecurityDescriptor
, &wptr
, NULL
))
3174 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
3175 if (!DumpGroup(SecurityDescriptor
, &wptr
, NULL
))
3177 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
3178 if (!DumpDacl(SecurityDescriptor
, &wptr
, NULL
))
3180 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
3181 if (!DumpSacl(SecurityDescriptor
, &wptr
, NULL
))
3185 TRACE("ret: %s, %d\n", wine_dbgstr_w(wstr
), len
);
3186 *OutputString
= wstr
;
3188 *OutputLen
= strlenW(*OutputString
)+1;
3192 /******************************************************************************
3193 * ConvertSecurityDescriptorToStringSecurityDescriptorA [ADVAPI32.@]
3197 ConvertSecurityDescriptorToStringSecurityDescriptorA(PSECURITY_DESCRIPTOR SecurityDescriptor
,
3199 SECURITY_INFORMATION Information
,
3200 LPSTR
*OutputString
,
3206 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
3210 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
3211 *OutputString
= HeapAlloc(GetProcessHeap(), 0, lenA
);
3212 if (*OutputString
== NULL
)
3218 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
3221 if (OutputLen
!= NULL
)
3227 *OutputString
= NULL
;
3239 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
3243 DWORD i
, cBytes
, identAuth
, csubauth
;
3247 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
3251 SetLastError(ERROR_INVALID_SID
);
3255 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
3257 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
3259 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
3260 size
= SECURITY_MAX_SID_SIZE
;
3261 *sid
= LocalAlloc(0, size
);
3264 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3267 ret
= CreateWellKnownSid(knownSid
,
3273 SetLastError(ERROR_INVALID_SID
);
3280 /* That's probably a string S-R-I-S-S... */
3281 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
3283 SetLastError(ERROR_INVALID_SID
);
3287 cBytes
= ComputeStringSidSize(StringSid
);
3288 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
3291 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3296 csubauth
= ((cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
3298 StringSid
+= 2; /* Advance to Revision */
3299 pisid
->Revision
= atoiW(StringSid
);
3301 if (pisid
->Revision
!= SDDL_REVISION
)
3303 TRACE("Revision %d is unknown\n", pisid
->Revision
);
3304 goto lend
; /* ERROR_INVALID_SID */
3308 TRACE("SubAuthorityCount is 0\n");
3309 goto lend
; /* ERROR_INVALID_SID */
3312 pisid
->SubAuthorityCount
= csubauth
;
3314 /* Advance to identifier authority */
3315 while (*StringSid
&& *StringSid
!= '-')
3317 if (*StringSid
== '-')
3320 /* MS' implementation can't handle values greater than 2^32 - 1, so
3321 * we don't either; assume most significant bytes are always 0
3323 pisid
->IdentifierAuthority
.Value
[0] = 0;
3324 pisid
->IdentifierAuthority
.Value
[1] = 0;
3325 identAuth
= atoiW(StringSid
);
3326 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
3327 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
3328 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
3329 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
3331 /* Advance to first sub authority */
3332 while (*StringSid
&& *StringSid
!= '-')
3334 if (*StringSid
== '-')
3339 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
3341 while (*StringSid
&& *StringSid
!= '-')
3343 if (*StringSid
== '-')
3347 if (i
!= pisid
->SubAuthorityCount
)
3348 goto lend
; /* ERROR_INVALID_SID */
3357 SetLastError(ERROR_INVALID_SID
);
3360 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");
3369 ConvertStringSidToSidA(IN LPCSTR StringSid
,
3372 BOOL bRetVal
= FALSE
;
3374 TRACE("%s, %p\n", debugstr_a(StringSid
), sid
);
3375 if (GetVersion() & 0x80000000)
3376 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3377 else if (!StringSid
|| !sid
)
3378 SetLastError(ERROR_INVALID_PARAMETER
);
3381 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
3382 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3383 if (wStringSid
== NULL
)
3385 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
3386 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
3387 HeapFree(GetProcessHeap(), 0, wStringSid
);
3397 ConvertSidToStringSidW(PSID Sid
,
3401 UNICODE_STRING UnicodeString
;
3402 WCHAR FixedBuffer
[64];
3404 if (!RtlValidSid(Sid
))
3406 SetLastError(ERROR_INVALID_SID
);
3410 UnicodeString
.Length
= 0;
3411 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
3412 UnicodeString
.Buffer
= FixedBuffer
;
3413 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
3414 if (STATUS_BUFFER_TOO_SMALL
== Status
)
3416 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
3419 if (!NT_SUCCESS(Status
))
3421 SetLastError(RtlNtStatusToDosError(Status
));
3425 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
3426 if (NULL
== *StringSid
)
3428 if (UnicodeString
.Buffer
!= FixedBuffer
)
3430 RtlFreeUnicodeString(&UnicodeString
);
3432 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3436 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
3437 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
3438 if (UnicodeString
.Buffer
!= FixedBuffer
)
3440 RtlFreeUnicodeString(&UnicodeString
);
3451 ConvertSidToStringSidA(PSID Sid
,
3457 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
3462 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
3465 LocalFree(StringSidW
);
3466 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3470 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
3471 if (NULL
== *StringSid
)
3473 LocalFree(StringSidW
);
3474 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3478 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
3480 LocalFree(StringSid
);
3481 LocalFree(StringSidW
);
3485 LocalFree(StringSidW
);
3494 CreateProcessWithLogonW(LPCWSTR lpUsername
,
3498 LPCWSTR lpApplicationName
,
3499 LPWSTR lpCommandLine
,
3500 DWORD dwCreationFlags
,
3501 LPVOID lpEnvironment
,
3502 LPCWSTR lpCurrentDirectory
,
3503 LPSTARTUPINFOW lpStartupInfo
,
3504 LPPROCESS_INFORMATION lpProcessInformation
)
3506 FIXME("%s %s %s 0x%08x %s %s 0x%08x %p %s %p %p stub\n", debugstr_w(lpUsername
), debugstr_w(lpDomain
),
3507 debugstr_w(lpPassword
), dwLogonFlags
, debugstr_w(lpApplicationName
),
3508 debugstr_w(lpCommandLine
), dwCreationFlags
, lpEnvironment
, debugstr_w(lpCurrentDirectory
),
3509 lpStartupInfo
, lpProcessInformation
);
3516 CreateProcessWithTokenW(IN HANDLE hToken
,
3517 IN DWORD dwLogonFlags
,
3518 IN LPCWSTR lpApplicationName OPTIONAL
,
3519 IN OUT LPWSTR lpCommandLine OPTIONAL
,
3520 IN DWORD dwCreationFlags
,
3521 IN LPVOID lpEnvironment OPTIONAL
,
3522 IN LPCWSTR lpCurrentDirectory OPTIONAL
,
3523 IN LPSTARTUPINFOW lpStartupInfo
,
3524 OUT LPPROCESS_INFORMATION lpProcessInfo
)
3534 DuplicateTokenEx(IN HANDLE ExistingTokenHandle
,
3535 IN DWORD dwDesiredAccess
,
3536 IN LPSECURITY_ATTRIBUTES lpTokenAttributes OPTIONAL
,
3537 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
,
3538 IN TOKEN_TYPE TokenType
,
3539 OUT PHANDLE DuplicateTokenHandle
)
3541 OBJECT_ATTRIBUTES ObjectAttributes
;
3543 SECURITY_QUALITY_OF_SERVICE Sqos
;
3545 TRACE("%p 0x%08x 0x%08x 0x%08x %p\n", ExistingTokenHandle
, dwDesiredAccess
,
3546 ImpersonationLevel
, TokenType
, DuplicateTokenHandle
);
3548 Sqos
.Length
= sizeof(SECURITY_QUALITY_OF_SERVICE
);
3549 Sqos
.ImpersonationLevel
= ImpersonationLevel
;
3550 Sqos
.ContextTrackingMode
= 0;
3551 Sqos
.EffectiveOnly
= FALSE
;
3553 if (lpTokenAttributes
!= NULL
)
3555 InitializeObjectAttributes(&ObjectAttributes
,
3557 lpTokenAttributes
->bInheritHandle
? OBJ_INHERIT
: 0,
3559 lpTokenAttributes
->lpSecurityDescriptor
);
3563 InitializeObjectAttributes(&ObjectAttributes
,
3570 ObjectAttributes
.SecurityQualityOfService
= &Sqos
;
3572 Status
= NtDuplicateToken(ExistingTokenHandle
,
3577 DuplicateTokenHandle
);
3578 if (!NT_SUCCESS(Status
))
3580 ERR("NtDuplicateToken failed: Status %08x\n", Status
);
3581 SetLastError(RtlNtStatusToDosError(Status
));
3585 TRACE("Returning token %p.\n", *DuplicateTokenHandle
);
3594 DuplicateToken(IN HANDLE ExistingTokenHandle
,
3595 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
,
3596 OUT PHANDLE DuplicateTokenHandle
)
3598 return DuplicateTokenEx(ExistingTokenHandle
,
3599 TOKEN_IMPERSONATE
| TOKEN_QUERY
,
3603 DuplicateTokenHandle
);
3606 /******************************************************************************
3607 * ComputeStringSidSize
3609 static DWORD
ComputeStringSidSize(LPCWSTR StringSid
)
3611 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I(-S)+ */
3616 if (*StringSid
== '-')
3622 return GetSidLengthRequired(ctok
- 2);
3624 else /* String constant format - Only available in winxp and above */
3628 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
3629 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
3630 return GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
3632 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
3633 if (!strncmpW(WellKnownRids
[i
].wstr
, StringSid
, 2))
3636 ADVAPI_GetComputerSid(&local
);
3637 return GetSidLengthRequired(*GetSidSubAuthorityCount(&local
) + 1);
3642 return GetSidLengthRequired(0);
3645 /******************************************************************************
3646 * ParseStringSidToSid
3648 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
)
3653 TRACE("%s, %p, %p\n", debugstr_w(StringSid
), pSid
, cBytes
);
3656 SetLastError(ERROR_INVALID_PARAMETER
);
3657 TRACE("StringSid is NULL, returning FALSE\n");
3661 while (*StringSid
== ' ')
3664 *cBytes
= ComputeStringSidSize(StringSid
);
3665 if (!pisid
) /* Simply compute the size */
3667 TRACE("only size requested, returning TRUE with %d\n", *cBytes
);
3671 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
3673 DWORD i
= 0, identAuth
;
3674 DWORD csubauth
= ((*cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
3676 StringSid
+= 2; /* Advance to Revision */
3677 pisid
->Revision
= atoiW(StringSid
);
3679 if (pisid
->Revision
!= SDDL_REVISION
)
3681 TRACE("Revision %d is unknown\n", pisid
->Revision
);
3682 goto lend
; /* ERROR_INVALID_SID */
3686 TRACE("SubAuthorityCount is 0\n");
3687 goto lend
; /* ERROR_INVALID_SID */
3690 pisid
->SubAuthorityCount
= csubauth
;
3692 /* Advance to identifier authority */
3693 while (*StringSid
&& *StringSid
!= '-')
3695 if (*StringSid
== '-')
3698 /* MS' implementation can't handle values greater than 2^32 - 1, so
3699 * we don't either; assume most significant bytes are always 0
3701 pisid
->IdentifierAuthority
.Value
[0] = 0;
3702 pisid
->IdentifierAuthority
.Value
[1] = 0;
3703 identAuth
= atoiW(StringSid
);
3704 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
3705 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
3706 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
3707 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
3709 /* Advance to first sub authority */
3710 while (*StringSid
&& *StringSid
!= '-')
3712 if (*StringSid
== '-')
3717 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
3719 while (*StringSid
&& *StringSid
!= '-')
3721 if (*StringSid
== '-')
3725 if (i
!= pisid
->SubAuthorityCount
)
3726 goto lend
; /* ERROR_INVALID_SID */
3730 else /* String constant format - Only available in winxp and above */
3733 pisid
->Revision
= SDDL_REVISION
;
3735 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
3736 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))