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 typedef struct WELLKNOWNRID
120 WELL_KNOWN_SID_TYPE Type
;
124 static const WELLKNOWNRID WellKnownRids
[] = {
125 { WinAccountAdministratorSid
, DOMAIN_USER_RID_ADMIN
},
126 { WinAccountGuestSid
, DOMAIN_USER_RID_GUEST
},
127 { WinAccountKrbtgtSid
, DOMAIN_USER_RID_KRBTGT
},
128 { WinAccountDomainAdminsSid
, DOMAIN_GROUP_RID_ADMINS
},
129 { WinAccountDomainUsersSid
, DOMAIN_GROUP_RID_USERS
},
130 { WinAccountDomainGuestsSid
, DOMAIN_GROUP_RID_GUESTS
},
131 { WinAccountComputersSid
, DOMAIN_GROUP_RID_COMPUTERS
},
132 { WinAccountControllersSid
, DOMAIN_GROUP_RID_CONTROLLERS
},
133 { WinAccountCertAdminsSid
, DOMAIN_GROUP_RID_CERT_ADMINS
},
134 { WinAccountSchemaAdminsSid
, DOMAIN_GROUP_RID_SCHEMA_ADMINS
},
135 { WinAccountEnterpriseAdminsSid
, DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
},
136 { WinAccountPolicyAdminsSid
, DOMAIN_GROUP_RID_POLICY_ADMINS
},
137 { WinAccountRasAndIasServersSid
, DOMAIN_ALIAS_RID_RAS_SERVERS
},
140 static const SID sidWorld
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
} , { SECURITY_WORLD_RID
} };
145 static const WCHAR SDDL_ACCESS_ALLOWED
[] = {'A',0};
146 static const WCHAR SDDL_ACCESS_DENIED
[] = {'D',0};
147 static const WCHAR SDDL_OBJECT_ACCESS_ALLOWED
[] = {'O','A',0};
148 static const WCHAR SDDL_OBJECT_ACCESS_DENIED
[] = {'O','D',0};
149 static const WCHAR SDDL_AUDIT
[] = {'A','U',0};
150 static const WCHAR SDDL_ALARM
[] = {'A','L',0};
151 static const WCHAR SDDL_OBJECT_AUDIT
[] = {'O','U',0};
152 static const WCHAR SDDL_OBJECT_ALARM
[] = {'O','L',0};
157 #define ADS_RIGHT_DS_CREATE_CHILD 0x0001
158 #define ADS_RIGHT_DS_DELETE_CHILD 0x0002
159 #define ADS_RIGHT_ACTRL_DS_LIST 0x0004
160 #define ADS_RIGHT_DS_SELF 0x0008
161 #define ADS_RIGHT_DS_READ_PROP 0x0010
162 #define ADS_RIGHT_DS_WRITE_PROP 0x0020
163 #define ADS_RIGHT_DS_DELETE_TREE 0x0040
164 #define ADS_RIGHT_DS_LIST_OBJECT 0x0080
165 #define ADS_RIGHT_DS_CONTROL_ACCESS 0x0100
170 static const WCHAR SDDL_CONTAINER_INHERIT
[] = {'C','I',0};
171 static const WCHAR SDDL_OBJECT_INHERIT
[] = {'O','I',0};
172 static const WCHAR SDDL_NO_PROPAGATE
[] = {'N','P',0};
173 static const WCHAR SDDL_INHERIT_ONLY
[] = {'I','O',0};
174 static const WCHAR SDDL_INHERITED
[] = {'I','D',0};
175 static const WCHAR SDDL_AUDIT_SUCCESS
[] = {'S','A',0};
176 static const WCHAR SDDL_AUDIT_FAILURE
[] = {'F','A',0};
178 static const char * debugstr_sid(PSID sid
)
181 SID
* psid
= (SID
*)sid
;
186 auth
= psid
->IdentifierAuthority
.Value
[5] +
187 (psid
->IdentifierAuthority
.Value
[4] << 8) +
188 (psid
->IdentifierAuthority
.Value
[3] << 16) +
189 (psid
->IdentifierAuthority
.Value
[2] << 24);
191 switch (psid
->SubAuthorityCount
) {
193 return wine_dbg_sprintf("S-%d-%d", psid
->Revision
, auth
);
195 return wine_dbg_sprintf("S-%d-%d-%lu", psid
->Revision
, auth
,
196 psid
->SubAuthority
[0]);
198 return wine_dbg_sprintf("S-%d-%d-%lu-%lu", psid
->Revision
, auth
,
199 psid
->SubAuthority
[0], psid
->SubAuthority
[1]);
201 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu", psid
->Revision
, auth
,
202 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2]);
204 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
205 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
206 psid
->SubAuthority
[3]);
208 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
209 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
210 psid
->SubAuthority
[3], psid
->SubAuthority
[4]);
212 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
213 psid
->SubAuthority
[3], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
214 psid
->SubAuthority
[0], psid
->SubAuthority
[4], psid
->SubAuthority
[5]);
216 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
217 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
218 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
219 psid
->SubAuthority
[6]);
221 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
222 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
223 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
224 psid
->SubAuthority
[6], psid
->SubAuthority
[7]);
229 static const ACEFLAG AceRights
[] =
231 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
232 { SDDL_GENERIC_READ
, GENERIC_READ
},
233 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
234 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
236 { SDDL_READ_CONTROL
, READ_CONTROL
},
237 { SDDL_STANDARD_DELETE
, DELETE
},
238 { SDDL_WRITE_DAC
, WRITE_DAC
},
239 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
241 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
242 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
243 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
244 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
245 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
246 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
247 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
248 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
249 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
251 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
252 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
253 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
254 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
256 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
257 { SDDL_KEY_READ
, KEY_READ
},
258 { SDDL_KEY_WRITE
, KEY_WRITE
},
259 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
263 /* set last error code from NT status and get the proper boolean return value */
264 /* used for functions that are a simple wrapper around the corresponding ntdll API */
265 static __inline BOOL
set_ntstatus( NTSTATUS status
)
267 if (status
) SetLastError( RtlNtStatusToDosError( status
));
271 static const RECORD SidTable
[] =
273 { SDDL_ACCOUNT_OPERATORS
, WinBuiltinAccountOperatorsSid
},
274 { SDDL_ALIAS_PREW2KCOMPACC
, WinBuiltinPreWindows2000CompatibleAccessSid
},
275 { SDDL_ANONYMOUS
, WinAnonymousSid
},
276 { SDDL_AUTHENTICATED_USERS
, WinAuthenticatedUserSid
},
277 { SDDL_BUILTIN_ADMINISTRATORS
, WinBuiltinAdministratorsSid
},
278 { SDDL_BUILTIN_GUESTS
, WinBuiltinGuestsSid
},
279 { SDDL_BACKUP_OPERATORS
, WinBuiltinBackupOperatorsSid
},
280 { SDDL_BUILTIN_USERS
, WinBuiltinUsersSid
},
281 { SDDL_CERT_SERV_ADMINISTRATORS
, WinAccountCertAdminsSid
/* FIXME: DOMAIN_GROUP_RID_CERT_ADMINS */ },
282 { SDDL_CREATOR_GROUP
, WinCreatorGroupSid
},
283 { SDDL_CREATOR_OWNER
, WinCreatorOwnerSid
},
284 { SDDL_DOMAIN_ADMINISTRATORS
, WinAccountDomainAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ADMINS */ },
285 { SDDL_DOMAIN_COMPUTERS
, WinAccountComputersSid
/* FIXME: DOMAIN_GROUP_RID_COMPUTERS */ },
286 { SDDL_DOMAIN_DOMAIN_CONTROLLERS
, WinAccountControllersSid
/* FIXME: DOMAIN_GROUP_RID_CONTROLLERS */ },
287 { SDDL_DOMAIN_GUESTS
, WinAccountDomainGuestsSid
/* FIXME: DOMAIN_GROUP_RID_GUESTS */ },
288 { SDDL_DOMAIN_USERS
, WinAccountDomainUsersSid
/* FIXME: DOMAIN_GROUP_RID_USERS */ },
289 { SDDL_ENTERPRISE_ADMINS
, WinAccountEnterpriseAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ENTERPRISE_ADMINS */ },
290 { SDDL_ENTERPRISE_DOMAIN_CONTROLLERS
, WinEnterpriseControllersSid
},
291 { SDDL_EVERYONE
, WinWorldSid
},
292 { SDDL_GROUP_POLICY_ADMINS
, WinAccountPolicyAdminsSid
/* FIXME: DOMAIN_GROUP_RID_POLICY_ADMINS */ },
293 { SDDL_INTERACTIVE
, WinInteractiveSid
},
294 { SDDL_LOCAL_ADMIN
, WinAccountAdministratorSid
/* FIXME: DOMAIN_USER_RID_ADMIN */ },
295 { SDDL_LOCAL_GUEST
, WinAccountGuestSid
/* FIXME: DOMAIN_USER_RID_GUEST */ },
296 { SDDL_LOCAL_SERVICE
, WinLocalServiceSid
},
297 { SDDL_LOCAL_SYSTEM
, WinLocalSystemSid
},
298 { SDDL_NETWORK
, WinNetworkSid
},
299 { SDDL_NETWORK_CONFIGURATION_OPS
, WinBuiltinNetworkConfigurationOperatorsSid
},
300 { SDDL_NETWORK_SERVICE
, WinNetworkServiceSid
},
301 { SDDL_PRINTER_OPERATORS
, WinBuiltinPrintOperatorsSid
},
302 { SDDL_PERSONAL_SELF
, WinSelfSid
},
303 { SDDL_POWER_USERS
, WinBuiltinPowerUsersSid
},
304 { SDDL_RAS_SERVERS
, WinAccountRasAndIasServersSid
/* FIXME: DOMAIN_ALIAS_RID_RAS_SERVERS */ },
305 { SDDL_REMOTE_DESKTOP
, WinBuiltinRemoteDesktopUsersSid
},
306 { SDDL_REPLICATOR
, WinBuiltinReplicatorSid
},
307 { SDDL_RESTRICTED_CODE
, WinRestrictedCodeSid
},
308 { SDDL_SCHEMA_ADMINISTRATORS
, WinAccountSchemaAdminsSid
/* FIXME: DOMAIN_GROUP_RID_SCHEMA_ADMINS */ },
309 { SDDL_SERVER_OPERATORS
, WinBuiltinSystemOperatorsSid
},
310 { SDDL_SERVICE
, WinServiceSid
},
314 /* Exported functions */
320 AllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
321 BYTE nSubAuthorityCount
,
322 DWORD dwSubAuthority0
,
323 DWORD dwSubAuthority1
,
324 DWORD dwSubAuthority2
,
325 DWORD dwSubAuthority3
,
326 DWORD dwSubAuthority4
,
327 DWORD dwSubAuthority5
,
328 DWORD dwSubAuthority6
,
329 DWORD dwSubAuthority7
,
334 Status
= RtlAllocateAndInitializeSid(pIdentifierAuthority
,
345 if (!NT_SUCCESS(Status
))
347 SetLastError(RtlNtStatusToDosError(Status
));
358 * Docs says this function does NOT return a value
359 * even thou it's defined to return a PVOID...
365 return RtlFreeSid(pSid
);
372 CopySid(DWORD nDestinationSidLength
,
373 PSID pDestinationSid
,
378 Status
= RtlCopySid(nDestinationSidLength
,
381 if (!NT_SUCCESS (Status
))
383 SetLastError(RtlNtStatusToDosError(Status
));
395 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
396 IN PSID DomainSid OPTIONAL
,
401 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
403 if (cbSid
== NULL
|| (DomainSid
&& !IsValidSid(DomainSid
)))
405 SetLastError(ERROR_INVALID_PARAMETER
);
409 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++) {
410 if (WellKnownSids
[i
].Type
== WellKnownSidType
) {
411 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
416 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
421 SetLastError(ERROR_INVALID_PARAMETER
);
424 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
430 if (DomainSid
== NULL
|| *GetSidSubAuthorityCount(DomainSid
) == SID_MAX_SUB_AUTHORITIES
)
432 SetLastError(ERROR_INVALID_PARAMETER
);
436 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
437 if (WellKnownRids
[i
].Type
== WellKnownSidType
) {
438 UCHAR domain_subauth
= *GetSidSubAuthorityCount(DomainSid
);
439 DWORD domain_sid_length
= GetSidLengthRequired(domain_subauth
);
440 DWORD output_sid_length
= GetSidLengthRequired(domain_subauth
+ 1);
442 if (*cbSid
< output_sid_length
)
444 *cbSid
= output_sid_length
;
445 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
450 SetLastError(ERROR_INVALID_PARAMETER
);
453 CopyMemory(pSid
, DomainSid
, domain_sid_length
);
454 (*GetSidSubAuthorityCount(pSid
))++;
455 (*GetSidSubAuthority(pSid
, domain_subauth
)) = WellKnownRids
[i
].Rid
;
456 *cbSid
= output_sid_length
;
460 SetLastError(ERROR_INVALID_PARAMETER
);
469 IsWellKnownSid(IN PSID pSid
,
470 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
473 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
475 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
477 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
479 if (EqualSid(pSid
, (PSID
)(&WellKnownSids
[i
].Sid
.Revision
)))
492 IsValidSid(PSID pSid
)
494 return (BOOL
)RtlValidSid(pSid
);
505 SetLastError(ERROR_SUCCESS
);
506 return RtlEqualSid (pSid1
, pSid2
);
514 EqualPrefixSid(PSID pSid1
,
517 return RtlEqualPrefixSid (pSid1
, pSid2
);
525 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
527 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
535 InitializeSid(PSID Sid
,
536 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
537 BYTE nSubAuthorityCount
)
541 Status
= RtlInitializeSid(Sid
,
542 pIdentifierAuthority
,
544 if (!NT_SUCCESS(Status
))
546 SetLastError(RtlNtStatusToDosError(Status
));
556 PSID_IDENTIFIER_AUTHORITY
558 GetSidIdentifierAuthority(PSID pSid
)
560 return RtlIdentifierAuthoritySid(pSid
);
568 GetSidSubAuthority(PSID pSid
,
571 SetLastError(ERROR_SUCCESS
);
572 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
580 GetSidSubAuthorityCount(PSID pSid
)
582 SetLastError(ERROR_SUCCESS
);
583 return RtlSubAuthorityCountSid(pSid
);
591 GetLengthSid(PSID pSid
)
593 return (DWORD
)RtlLengthSid(pSid
);
596 /**********************************************************************
597 * PrivilegeCheck EXPORTED
602 PrivilegeCheck(HANDLE ClientToken
,
603 PPRIVILEGE_SET RequiredPrivileges
,
609 Status
= NtPrivilegeCheck(ClientToken
,
612 if (!NT_SUCCESS(Status
))
614 SetLastError(RtlNtStatusToDosError(Status
));
618 *pfResult
= (BOOL
)Result
;
623 /******************************************************************************
624 * ParseAclStringFlags
626 static DWORD
ParseAclStringFlags(LPCWSTR
* StringAcl
)
629 LPCWSTR szAcl
= *StringAcl
;
631 while (*szAcl
!= '(')
635 flags
|= SE_DACL_PROTECTED
;
637 else if (*szAcl
== 'A')
641 flags
|= SE_DACL_AUTO_INHERIT_REQ
;
642 else if (*szAcl
== 'I')
643 flags
|= SE_DACL_AUTO_INHERITED
;
652 /******************************************************************************
655 static const ACEFLAG AceType
[] =
657 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
658 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
659 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
660 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
662 { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
663 { SDDL_OBJECT_ACCESS_DENIED, ACCESS_DENIED_OBJECT_ACE_TYPE },
664 { SDDL_OBJECT_ALARM, SYSTEM_ALARM_OBJECT_ACE_TYPE },
665 { SDDL_OBJECT_AUDIT, SYSTEM_AUDIT_OBJECT_ACE_TYPE },
670 static BYTE
ParseAceStringType(LPCWSTR
* StringAcl
)
673 LPCWSTR szAcl
= *StringAcl
;
674 const ACEFLAG
*lpaf
= AceType
;
677 (len
= strlenW(lpaf
->wstr
)) &&
678 strncmpW(lpaf
->wstr
, szAcl
, len
))
689 /******************************************************************************
690 * ParseAceStringFlags
692 static const ACEFLAG AceFlags
[] =
694 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
695 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
696 { SDDL_INHERITED
, INHERITED_ACE
},
697 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
698 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
699 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
700 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
704 static BYTE
ParseAceStringFlags(LPCWSTR
* StringAcl
)
708 LPCWSTR szAcl
= *StringAcl
;
710 while (*szAcl
!= ';')
712 const ACEFLAG
*lpaf
= AceFlags
;
715 (len
= strlenW(lpaf
->wstr
)) &&
716 strncmpW(lpaf
->wstr
, szAcl
, len
))
722 flags
|= lpaf
->value
;
731 /******************************************************************************
732 * ParseAceStringRights
734 static DWORD
ParseAceStringRights(LPCWSTR
* StringAcl
)
738 LPCWSTR szAcl
= *StringAcl
;
740 if ((*szAcl
== '0') && (*(szAcl
+ 1) == 'x'))
744 while (*p
&& *p
!= ';')
747 if (p
- szAcl
<= 10 /* 8 hex digits + "0x" */ )
749 rights
= strtoulW(szAcl
, NULL
, 16);
753 WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl
, p
- szAcl
));
757 while (*szAcl
!= ';')
759 const ACEFLAG
*lpaf
= AceRights
;
762 (len
= strlenW(lpaf
->wstr
)) &&
763 strncmpW(lpaf
->wstr
, szAcl
, len
))
771 rights
|= lpaf
->value
;
781 /******************************************************************************
782 * ParseStringAclToAcl
784 * dacl_flags(string_ace1)(string_ace2)... (string_acen)
787 ParseStringAclToAcl(LPCWSTR StringAcl
,
794 DWORD length
= sizeof(ACL
);
797 PACCESS_ALLOWED_ACE pAce
= NULL
; /* pointer to current ACE */
799 TRACE("%s\n", debugstr_w(StringAcl
));
804 if (pAcl
) /* pAce is only useful if we're setting values */
805 pAce
= (PACCESS_ALLOWED_ACE
) (pAcl
+ 1);
807 /* Parse ACL flags */
808 *lpdwFlags
= ParseAclStringFlags(&StringAcl
);
811 while (*StringAcl
== '(')
816 val
= ParseAceStringType(&StringAcl
);
818 pAce
->Header
.AceType
= (BYTE
) val
;
819 if (*StringAcl
!= ';')
823 /* Parse ACE flags */
824 val
= ParseAceStringFlags(&StringAcl
);
826 pAce
->Header
.AceFlags
= (BYTE
) val
;
827 if (*StringAcl
!= ';')
831 /* Parse ACE rights */
832 val
= ParseAceStringRights(&StringAcl
);
835 if (*StringAcl
!= ';')
839 /* Parse ACE object guid */
840 if (*StringAcl
!= ';')
842 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
847 /* Parse ACE inherit object guid */
848 if (*StringAcl
!= ';')
850 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
855 /* Parse ACE account sid */
856 if (ParseStringSidToSid(StringAcl
, pAce
? &pAce
->SidStart
: NULL
, &sidlen
))
858 while (*StringAcl
&& *StringAcl
!= ')')
862 if (*StringAcl
!= ')')
866 acesize
= sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
) + sidlen
;
870 pAce
->Header
.AceSize
= acesize
;
871 pAce
= (PACCESS_ALLOWED_ACE
)((LPBYTE
)pAce
+ acesize
);
880 ERR("ACL too large\n");
886 pAcl
->AclRevision
= ACL_REVISION
;
888 pAcl
->AclSize
= length
;
889 pAcl
->AceCount
= acecount
++;
895 SetLastError(ERROR_INVALID_ACL
);
896 WARN("Invalid ACE string format\n");
901 /******************************************************************************
902 * ParseStringSecurityDescriptorToSecurityDescriptor
905 ParseStringSecurityDescriptorToSecurityDescriptor(LPCWSTR StringSecurityDescriptor
,
906 SECURITY_DESCRIPTOR_RELATIVE
* SecurityDescriptor
,
913 LPBYTE lpNext
= NULL
;
916 *cBytes
= sizeof(SECURITY_DESCRIPTOR
);
918 if (SecurityDescriptor
)
919 lpNext
= (LPBYTE
)(SecurityDescriptor
+ 1);
921 while (*StringSecurityDescriptor
)
923 toktype
= *StringSecurityDescriptor
;
925 /* Expect char identifier followed by ':' */
926 StringSecurityDescriptor
++;
927 if (*StringSecurityDescriptor
!= ':')
929 SetLastError(ERROR_INVALID_PARAMETER
);
932 StringSecurityDescriptor
++;
935 lptoken
= StringSecurityDescriptor
;
936 while (*lptoken
&& *lptoken
!= ':')
942 len
= lptoken
- StringSecurityDescriptor
;
943 memcpy( tok
, StringSecurityDescriptor
, len
* sizeof(WCHAR
) );
952 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
955 if (SecurityDescriptor
)
957 SecurityDescriptor
->Owner
= lpNext
- (LPBYTE
)SecurityDescriptor
;
958 lpNext
+= bytes
; /* Advance to next token */
970 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
973 if (SecurityDescriptor
)
975 SecurityDescriptor
->Group
= lpNext
- (LPBYTE
)SecurityDescriptor
;
976 lpNext
+= bytes
; /* Advance to next token */
989 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
992 if (SecurityDescriptor
)
994 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
| flags
;
995 SecurityDescriptor
->Dacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
996 lpNext
+= bytes
; /* Advance to next token */
1009 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1012 if (SecurityDescriptor
)
1014 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
| flags
;
1015 SecurityDescriptor
->Sacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1016 lpNext
+= bytes
; /* Advance to next token */
1025 FIXME("Unknown token\n");
1026 SetLastError(ERROR_INVALID_PARAMETER
);
1030 StringSecurityDescriptor
= lptoken
;
1039 /* Winehq cvs 20050916 */
1040 /******************************************************************************
1041 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
1046 ConvertStringSecurityDescriptorToSecurityDescriptorA(LPCSTR StringSecurityDescriptor
,
1047 DWORD StringSDRevision
,
1048 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1049 PULONG SecurityDescriptorSize
)
1053 LPWSTR StringSecurityDescriptorW
;
1055 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
1056 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1058 if (StringSecurityDescriptorW
)
1060 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
1062 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
1063 StringSDRevision
, SecurityDescriptor
,
1064 SecurityDescriptorSize
);
1065 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
1071 /******************************************************************************
1072 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
1076 ConvertStringSecurityDescriptorToSecurityDescriptorW(LPCWSTR StringSecurityDescriptor
,
1077 DWORD StringSDRevision
,
1078 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1079 PULONG SecurityDescriptorSize
)
1082 SECURITY_DESCRIPTOR
* psd
;
1085 TRACE("%s\n", debugstr_w(StringSecurityDescriptor
));
1087 if (GetVersion() & 0x80000000)
1089 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1092 else if (!StringSecurityDescriptor
|| !SecurityDescriptor
)
1094 SetLastError(ERROR_INVALID_PARAMETER
);
1097 else if (StringSDRevision
!= SID_REVISION
)
1099 SetLastError(ERROR_UNKNOWN_REVISION
);
1103 /* Compute security descriptor length */
1104 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1108 psd
= *SecurityDescriptor
= LocalAlloc(GMEM_ZEROINIT
, cBytes
);
1109 if (!psd
) goto lend
;
1111 psd
->Revision
= SID_REVISION
;
1112 psd
->Control
|= SE_SELF_RELATIVE
;
1114 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1115 (SECURITY_DESCRIPTOR_RELATIVE
*)psd
, &cBytes
))
1121 if (SecurityDescriptorSize
)
1122 *SecurityDescriptorSize
= cBytes
;
1127 TRACE(" ret=%d\n", bret
);
1131 static void DumpString(LPCWSTR string
, int cch
, WCHAR
**pwptr
, ULONG
*plen
)
1134 cch
= strlenW(string
);
1141 memcpy(*pwptr
, string
, sizeof(WCHAR
)*cch
);
1146 static BOOL
DumpSidNumeric(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
1149 WCHAR fmt
[] = { 'S','-','%','u','-','%','d',0 };
1150 WCHAR subauthfmt
[] = { '-','%','u',0 };
1154 if( !IsValidSid( psid
) || pisid
->Revision
!= SDDL_REVISION
)
1156 SetLastError(ERROR_INVALID_SID
);
1160 if (pisid
->IdentifierAuthority
.Value
[0] ||
1161 pisid
->IdentifierAuthority
.Value
[1])
1163 FIXME("not matching MS' bugs\n");
1164 SetLastError(ERROR_INVALID_SID
);
1168 sprintfW( buf
, fmt
, pisid
->Revision
,
1170 MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
1171 pisid
->IdentifierAuthority
.Value
[4] ),
1172 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
1173 pisid
->IdentifierAuthority
.Value
[2] )
1175 DumpString(buf
, -1, pwptr
, plen
);
1177 for( i
=0; i
<pisid
->SubAuthorityCount
; i
++ )
1179 sprintfW( buf
, subauthfmt
, pisid
->SubAuthority
[i
] );
1180 DumpString(buf
, -1, pwptr
, plen
);
1185 static BOOL
DumpSid(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
1188 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
1190 if (WellKnownSids
[i
].wstr
[0] && EqualSid(psid
, (PSID
)&(WellKnownSids
[i
].Sid
.Revision
)))
1192 DumpString(WellKnownSids
[i
].wstr
, 2, pwptr
, plen
);
1197 return DumpSidNumeric(psid
, pwptr
, plen
);
1200 static const LPCWSTR AceRightBitNames
[32] = {
1201 SDDL_CREATE_CHILD
, /* 0 */
1205 SDDL_READ_PROPERTY
, /* 4 */
1206 SDDL_WRITE_PROPERTY
,
1209 SDDL_CONTROL_ACCESS
, /* 8 */
1217 SDDL_STANDARD_DELETE
, /* 16 */
1229 SDDL_GENERIC_ALL
, /* 28 */
1230 SDDL_GENERIC_EXECUTE
,
1235 static void DumpRights(DWORD mask
, WCHAR
**pwptr
, ULONG
*plen
)
1237 static const WCHAR fmtW
[] = {'0','x','%','x',0};
1244 /* first check if the right have name */
1245 for (i
= 0; i
< sizeof(AceRights
)/sizeof(AceRights
[0]); i
++)
1247 if (AceRights
[i
].wstr
== NULL
)
1249 if (mask
== AceRights
[i
].value
)
1251 DumpString(AceRights
[i
].wstr
, -1, pwptr
, plen
);
1256 /* then check if it can be built from bit names */
1257 for (i
= 0; i
< 32; i
++)
1259 if ((mask
& (1 << i
)) && (AceRightBitNames
[i
] == NULL
))
1261 /* can't be built from bit names */
1262 sprintfW(buf
, fmtW
, mask
);
1263 DumpString(buf
, -1, pwptr
, plen
);
1268 /* build from bit names */
1269 for (i
= 0; i
< 32; i
++)
1270 if (mask
& (1 << i
))
1271 DumpString(AceRightBitNames
[i
], -1, pwptr
, plen
);
1274 static BOOL
DumpAce(LPVOID pace
, WCHAR
**pwptr
, ULONG
*plen
)
1276 ACCESS_ALLOWED_ACE
*piace
; /* all the supported ACEs have the same memory layout */
1277 static const WCHAR openbr
= '(';
1278 static const WCHAR closebr
= ')';
1279 static const WCHAR semicolon
= ';';
1281 if (((PACE_HEADER
)pace
)->AceType
> SYSTEM_ALARM_ACE_TYPE
|| ((PACE_HEADER
)pace
)->AceSize
< sizeof(ACCESS_ALLOWED_ACE
))
1283 SetLastError(ERROR_INVALID_ACL
);
1288 DumpString(&openbr
, 1, pwptr
, plen
);
1289 switch (piace
->Header
.AceType
)
1291 case ACCESS_ALLOWED_ACE_TYPE
:
1292 DumpString(SDDL_ACCESS_ALLOWED
, -1, pwptr
, plen
);
1294 case ACCESS_DENIED_ACE_TYPE
:
1295 DumpString(SDDL_ACCESS_DENIED
, -1, pwptr
, plen
);
1297 case SYSTEM_AUDIT_ACE_TYPE
:
1298 DumpString(SDDL_AUDIT
, -1, pwptr
, plen
);
1300 case SYSTEM_ALARM_ACE_TYPE
:
1301 DumpString(SDDL_ALARM
, -1, pwptr
, plen
);
1304 DumpString(&semicolon
, 1, pwptr
, plen
);
1306 if (piace
->Header
.AceFlags
& OBJECT_INHERIT_ACE
)
1307 DumpString(SDDL_OBJECT_INHERIT
, -1, pwptr
, plen
);
1308 if (piace
->Header
.AceFlags
& CONTAINER_INHERIT_ACE
)
1309 DumpString(SDDL_CONTAINER_INHERIT
, -1, pwptr
, plen
);
1310 if (piace
->Header
.AceFlags
& NO_PROPAGATE_INHERIT_ACE
)
1311 DumpString(SDDL_NO_PROPAGATE
, -1, pwptr
, plen
);
1312 if (piace
->Header
.AceFlags
& INHERIT_ONLY_ACE
)
1313 DumpString(SDDL_INHERIT_ONLY
, -1, pwptr
, plen
);
1314 if (piace
->Header
.AceFlags
& INHERITED_ACE
)
1315 DumpString(SDDL_INHERITED
, -1, pwptr
, plen
);
1316 if (piace
->Header
.AceFlags
& SUCCESSFUL_ACCESS_ACE_FLAG
)
1317 DumpString(SDDL_AUDIT_SUCCESS
, -1, pwptr
, plen
);
1318 if (piace
->Header
.AceFlags
& FAILED_ACCESS_ACE_FLAG
)
1319 DumpString(SDDL_AUDIT_FAILURE
, -1, pwptr
, plen
);
1320 DumpString(&semicolon
, 1, pwptr
, plen
);
1321 DumpRights(piace
->Mask
, pwptr
, plen
);
1322 DumpString(&semicolon
, 1, pwptr
, plen
);
1323 /* objects not supported */
1324 DumpString(&semicolon
, 1, pwptr
, plen
);
1325 /* objects not supported */
1326 DumpString(&semicolon
, 1, pwptr
, plen
);
1327 if (!DumpSid((PSID
)&piace
->SidStart
, pwptr
, plen
))
1329 DumpString(&closebr
, 1, pwptr
, plen
);
1333 static BOOL
DumpAcl(PACL pacl
, WCHAR
**pwptr
, ULONG
*plen
, BOOL
protected, BOOL autoInheritReq
, BOOL autoInherited
)
1339 DumpString(SDDL_PROTECTED
, -1, pwptr
, plen
);
1341 DumpString(SDDL_AUTO_INHERIT_REQ
, -1, pwptr
, plen
);
1343 DumpString(SDDL_AUTO_INHERITED
, -1, pwptr
, plen
);
1348 if (!IsValidAcl(pacl
))
1351 count
= pacl
->AceCount
;
1352 for (i
= 0; i
< count
; i
++)
1355 if (!GetAce(pacl
, i
, &ace
))
1357 if (!DumpAce(ace
, pwptr
, plen
))
1364 static BOOL
DumpOwner(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
1366 static const WCHAR prefix
[] = {'O',':',0};
1370 if (!GetSecurityDescriptorOwner(SecurityDescriptor
, &psid
, &bDefaulted
))
1376 DumpString(prefix
, -1, pwptr
, plen
);
1377 if (!DumpSid(psid
, pwptr
, plen
))
1382 static BOOL
DumpGroup(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
1384 static const WCHAR prefix
[] = {'G',':',0};
1388 if (!GetSecurityDescriptorGroup(SecurityDescriptor
, &psid
, &bDefaulted
))
1394 DumpString(prefix
, -1, pwptr
, plen
);
1395 if (!DumpSid(psid
, pwptr
, plen
))
1400 static BOOL
DumpDacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
1402 static const WCHAR dacl
[] = {'D',':',0};
1403 SECURITY_DESCRIPTOR_CONTROL control
;
1404 BOOL present
, defaulted
;
1408 if (!GetSecurityDescriptorDacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
1411 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
1417 DumpString(dacl
, 2, pwptr
, plen
);
1418 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_DACL_PROTECTED
, control
& SE_DACL_AUTO_INHERIT_REQ
, control
& SE_DACL_AUTO_INHERITED
))
1423 static BOOL
DumpSacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
1425 static const WCHAR sacl
[] = {'S',':',0};
1426 SECURITY_DESCRIPTOR_CONTROL control
;
1427 BOOL present
, defaulted
;
1431 if (!GetSecurityDescriptorSacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
1434 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
1440 DumpString(sacl
, 2, pwptr
, plen
);
1441 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_SACL_PROTECTED
, control
& SE_SACL_AUTO_INHERIT_REQ
, control
& SE_SACL_AUTO_INHERITED
))
1446 /******************************************************************************
1447 * ConvertSecurityDescriptorToStringSecurityDescriptorW [ADVAPI32.@]
1451 ConvertSecurityDescriptorToStringSecurityDescriptorW(PSECURITY_DESCRIPTOR SecurityDescriptor
,
1453 SECURITY_INFORMATION SecurityInformation
,
1454 LPWSTR
*OutputString
,
1460 if (SDRevision
!= SDDL_REVISION_1
)
1462 ERR("Program requested unknown SDDL revision %d\n", SDRevision
);
1463 SetLastError(ERROR_UNKNOWN_REVISION
);
1468 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
1469 if (!DumpOwner(SecurityDescriptor
, NULL
, &len
))
1471 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
1472 if (!DumpGroup(SecurityDescriptor
, NULL
, &len
))
1474 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
1475 if (!DumpDacl(SecurityDescriptor
, NULL
, &len
))
1477 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
1478 if (!DumpSacl(SecurityDescriptor
, NULL
, &len
))
1481 wstr
= wptr
= LocalAlloc(0, (len
+ 1)*sizeof(WCHAR
));
1485 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
1486 if (!DumpOwner(SecurityDescriptor
, &wptr
, NULL
))
1488 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
1489 if (!DumpGroup(SecurityDescriptor
, &wptr
, NULL
))
1491 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
1492 if (!DumpDacl(SecurityDescriptor
, &wptr
, NULL
))
1494 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
1495 if (!DumpSacl(SecurityDescriptor
, &wptr
, NULL
))
1499 TRACE("ret: %s, %d\n", wine_dbgstr_w(wstr
), len
);
1500 *OutputString
= wstr
;
1502 *OutputLen
= strlenW(*OutputString
)+1;
1506 /******************************************************************************
1507 * ConvertSecurityDescriptorToStringSecurityDescriptorA [ADVAPI32.@]
1511 ConvertSecurityDescriptorToStringSecurityDescriptorA(PSECURITY_DESCRIPTOR SecurityDescriptor
,
1513 SECURITY_INFORMATION Information
,
1514 LPSTR
*OutputString
,
1520 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
1524 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
1525 *OutputString
= HeapAlloc(GetProcessHeap(), 0, lenA
);
1526 if (*OutputString
== NULL
)
1532 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
1535 if (OutputLen
!= NULL
)
1541 *OutputString
= NULL
;
1553 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
1557 DWORD i
, cBytes
, identAuth
, csubauth
;
1561 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
1565 SetLastError(ERROR_INVALID_SID
);
1569 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
1571 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
1573 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
1574 size
= SECURITY_MAX_SID_SIZE
;
1575 *sid
= LocalAlloc(0, size
);
1578 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1581 ret
= CreateWellKnownSid(knownSid
,
1587 SetLastError(ERROR_INVALID_SID
);
1594 /* That's probably a string S-R-I-S-S... */
1595 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
1597 SetLastError(ERROR_INVALID_SID
);
1601 cBytes
= ComputeStringSidSize(StringSid
);
1602 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
1605 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1610 csubauth
= ((cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
1612 StringSid
+= 2; /* Advance to Revision */
1613 pisid
->Revision
= atoiW(StringSid
);
1615 if (pisid
->Revision
!= SDDL_REVISION
)
1617 TRACE("Revision %d is unknown\n", pisid
->Revision
);
1618 goto lend
; /* ERROR_INVALID_SID */
1622 TRACE("SubAuthorityCount is 0\n");
1623 goto lend
; /* ERROR_INVALID_SID */
1626 pisid
->SubAuthorityCount
= csubauth
;
1628 /* Advance to identifier authority */
1629 while (*StringSid
&& *StringSid
!= '-')
1631 if (*StringSid
== '-')
1634 /* MS' implementation can't handle values greater than 2^32 - 1, so
1635 * we don't either; assume most significant bytes are always 0
1637 pisid
->IdentifierAuthority
.Value
[0] = 0;
1638 pisid
->IdentifierAuthority
.Value
[1] = 0;
1639 identAuth
= atoiW(StringSid
);
1640 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
1641 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
1642 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
1643 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
1645 /* Advance to first sub authority */
1646 while (*StringSid
&& *StringSid
!= '-')
1648 if (*StringSid
== '-')
1653 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
1655 while (*StringSid
&& *StringSid
!= '-')
1657 if (*StringSid
== '-')
1661 if (i
!= pisid
->SubAuthorityCount
)
1662 goto lend
; /* ERROR_INVALID_SID */
1671 SetLastError(ERROR_INVALID_SID
);
1674 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");
1683 ConvertStringSidToSidA(IN LPCSTR StringSid
,
1686 BOOL bRetVal
= FALSE
;
1688 TRACE("%s, %p\n", debugstr_a(StringSid
), sid
);
1689 if (GetVersion() & 0x80000000)
1690 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1691 else if (!StringSid
|| !sid
)
1692 SetLastError(ERROR_INVALID_PARAMETER
);
1695 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
1696 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1697 if (wStringSid
== NULL
)
1699 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
1700 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
1701 HeapFree(GetProcessHeap(), 0, wStringSid
);
1711 ConvertSidToStringSidW(PSID Sid
,
1715 UNICODE_STRING UnicodeString
;
1716 WCHAR FixedBuffer
[64];
1718 if (!RtlValidSid(Sid
))
1720 SetLastError(ERROR_INVALID_SID
);
1724 UnicodeString
.Length
= 0;
1725 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
1726 UnicodeString
.Buffer
= FixedBuffer
;
1727 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
1728 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1730 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
1733 if (!NT_SUCCESS(Status
))
1735 SetLastError(RtlNtStatusToDosError(Status
));
1739 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
1740 if (NULL
== *StringSid
)
1742 if (UnicodeString
.Buffer
!= FixedBuffer
)
1744 RtlFreeUnicodeString(&UnicodeString
);
1746 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1750 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
1751 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
1752 if (UnicodeString
.Buffer
!= FixedBuffer
)
1754 RtlFreeUnicodeString(&UnicodeString
);
1765 ConvertSidToStringSidA(PSID Sid
,
1771 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
1776 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
1779 LocalFree(StringSidW
);
1780 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1784 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
1785 if (NULL
== *StringSid
)
1787 LocalFree(StringSidW
);
1788 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1792 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
1794 LocalFree(StringSid
);
1795 LocalFree(StringSidW
);
1799 LocalFree(StringSidW
);
1808 AllocateLocallyUniqueId(PLUID Luid
)
1812 Status
= NtAllocateLocallyUniqueId (Luid
);
1813 if (!NT_SUCCESS (Status
))
1815 SetLastError(RtlNtStatusToDosError(Status
));
1822 /******************************************************************************
1823 * ComputeStringSidSize
1825 static DWORD
ComputeStringSidSize(LPCWSTR StringSid
)
1827 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I(-S)+ */
1832 if (*StringSid
== '-')
1838 return GetSidLengthRequired(ctok
- 2);
1840 else /* String constant format - Only available in winxp and above */
1844 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
1845 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
1846 return GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
1849 return GetSidLengthRequired(0);
1852 /******************************************************************************
1853 * ParseStringSidToSid
1855 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
)
1860 TRACE("%s, %p, %p\n", debugstr_w(StringSid
), pSid
, cBytes
);
1863 SetLastError(ERROR_INVALID_PARAMETER
);
1864 TRACE("StringSid is NULL, returning FALSE\n");
1868 while (*StringSid
== ' ')
1871 *cBytes
= ComputeStringSidSize(StringSid
);
1872 if (!pisid
) /* Simply compute the size */
1874 TRACE("only size requested, returning TRUE\n");
1878 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
1880 DWORD i
= 0, identAuth
;
1881 DWORD csubauth
= ((*cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
1883 StringSid
+= 2; /* Advance to Revision */
1884 pisid
->Revision
= atoiW(StringSid
);
1886 if (pisid
->Revision
!= SDDL_REVISION
)
1888 TRACE("Revision %d is unknown\n", pisid
->Revision
);
1889 goto lend
; /* ERROR_INVALID_SID */
1893 TRACE("SubAuthorityCount is 0\n");
1894 goto lend
; /* ERROR_INVALID_SID */
1897 pisid
->SubAuthorityCount
= csubauth
;
1899 /* Advance to identifier authority */
1900 while (*StringSid
&& *StringSid
!= '-')
1902 if (*StringSid
== '-')
1905 /* MS' implementation can't handle values greater than 2^32 - 1, so
1906 * we don't either; assume most significant bytes are always 0
1908 pisid
->IdentifierAuthority
.Value
[0] = 0;
1909 pisid
->IdentifierAuthority
.Value
[1] = 0;
1910 identAuth
= atoiW(StringSid
);
1911 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
1912 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
1913 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
1914 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
1916 /* Advance to first sub authority */
1917 while (*StringSid
&& *StringSid
!= '-')
1919 if (*StringSid
== '-')
1924 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
1926 while (*StringSid
&& *StringSid
!= '-')
1928 if (*StringSid
== '-')
1932 if (i
!= pisid
->SubAuthorityCount
)
1933 goto lend
; /* ERROR_INVALID_SID */
1937 else /* String constant format - Only available in winxp and above */
1940 pisid
->Revision
= SDDL_REVISION
;
1942 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
1943 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
1946 pisid
->SubAuthorityCount
= WellKnownSids
[i
].Sid
.SubAuthorityCount
;
1947 pisid
->IdentifierAuthority
= WellKnownSids
[i
].Sid
.IdentifierAuthority
;
1948 for (j
= 0; j
< WellKnownSids
[i
].Sid
.SubAuthorityCount
; j
++)
1949 pisid
->SubAuthority
[j
] = WellKnownSids
[i
].Sid
.SubAuthority
[j
];
1954 FIXME("String constant not supported: %s\n", debugstr_wn(StringSid
, 2));
1959 SetLastError(ERROR_INVALID_SID
);
1961 TRACE("returning %s\n", bret
? "TRUE" : "FALSE");
1970 GetWindowsAccountDomainSid(IN PSID pSid
,
1971 OUT PSID ppDomainSid
,
1972 IN OUT DWORD
* cbSid
)
1983 EqualDomainSid(IN PSID pSid1
,