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 (!NT_SUCCESS(status
)) SetLastError( RtlNtStatusToDosError( status
));
268 return NT_SUCCESS(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 /************************************************************
315 * ADVAPI_IsLocalComputer
317 * Checks whether the server name indicates local machine.
319 BOOL
ADVAPI_IsLocalComputer(LPCWSTR ServerName
)
321 DWORD dwSize
= MAX_COMPUTERNAME_LENGTH
+ 1;
325 if (!ServerName
|| !ServerName
[0])
328 buf
= heap_alloc(dwSize
* sizeof(WCHAR
));
329 Result
= GetComputerNameW(buf
, &dwSize
);
330 if (Result
&& (ServerName
[0] == '\\') && (ServerName
[1] == '\\'))
332 Result
= Result
&& !lstrcmpW(ServerName
, buf
);
338 /* Exported functions */
344 OpenProcessToken(HANDLE ProcessHandle
,
350 TRACE("%p, %x, %p.\n", ProcessHandle
, DesiredAccess
, TokenHandle
);
352 Status
= NtOpenProcessToken(ProcessHandle
,
355 if (!NT_SUCCESS(Status
))
357 ERR("NtOpenProcessToken failed! Status %08x.\n", Status
);
358 SetLastError(RtlNtStatusToDosError(Status
));
362 TRACE("Returning token %p.\n", *TokenHandle
);
371 OpenThreadToken(HANDLE ThreadHandle
,
378 Status
= NtOpenThreadToken(ThreadHandle
,
382 if (!NT_SUCCESS(Status
))
384 SetLastError(RtlNtStatusToDosError(Status
));
395 AdjustTokenGroups(HANDLE TokenHandle
,
397 PTOKEN_GROUPS NewState
,
399 PTOKEN_GROUPS PreviousState
,
404 Status
= NtAdjustGroupsToken(TokenHandle
,
409 (PULONG
)ReturnLength
);
410 if (!NT_SUCCESS(Status
))
412 SetLastError(RtlNtStatusToDosError(Status
));
423 AdjustTokenPrivileges(HANDLE TokenHandle
,
424 BOOL DisableAllPrivileges
,
425 PTOKEN_PRIVILEGES NewState
,
427 PTOKEN_PRIVILEGES PreviousState
,
432 Status
= NtAdjustPrivilegesToken(TokenHandle
,
433 DisableAllPrivileges
,
437 (PULONG
)ReturnLength
);
438 if (STATUS_NOT_ALL_ASSIGNED
== Status
)
440 SetLastError(ERROR_NOT_ALL_ASSIGNED
);
444 if (!NT_SUCCESS(Status
))
446 SetLastError(RtlNtStatusToDosError(Status
));
450 /* AdjustTokenPrivileges is documented to do this */
451 SetLastError(ERROR_SUCCESS
);
460 GetTokenInformation(HANDLE TokenHandle
,
461 TOKEN_INFORMATION_CLASS TokenInformationClass
,
462 LPVOID TokenInformation
,
463 DWORD TokenInformationLength
,
468 Status
= NtQueryInformationToken(TokenHandle
,
469 TokenInformationClass
,
471 TokenInformationLength
,
472 (PULONG
)ReturnLength
);
473 if (!NT_SUCCESS(Status
))
475 SetLastError(RtlNtStatusToDosError(Status
));
486 SetTokenInformation(HANDLE TokenHandle
,
487 TOKEN_INFORMATION_CLASS TokenInformationClass
,
488 LPVOID TokenInformation
,
489 DWORD TokenInformationLength
)
493 Status
= NtSetInformationToken(TokenHandle
,
494 TokenInformationClass
,
496 TokenInformationLength
);
497 if (!NT_SUCCESS(Status
))
499 SetLastError(RtlNtStatusToDosError(Status
));
510 SetThreadToken(IN PHANDLE ThreadHandle OPTIONAL
,
511 IN HANDLE TokenHandle
)
516 hThread
= (ThreadHandle
!= NULL
) ? *ThreadHandle
: NtCurrentThread();
518 Status
= NtSetInformationThread(hThread
,
519 ThreadImpersonationToken
,
522 if (!NT_SUCCESS(Status
))
524 SetLastError(RtlNtStatusToDosError(Status
));
531 /*************************************************************************
532 * CreateRestrictedToken [ADVAPI32.@]
534 * Create a new more restricted token from an existing token.
537 * baseToken [I] Token to base the new restricted token on
539 * nDisableSids [I] Length of disableSids array
540 * disableSids [I] Array of SIDs to disable in the new token
541 * nDeletePrivs [I] Length of deletePrivs array
542 * deletePrivs [I] Array of privileges to delete in the new token
543 * nRestrictSids [I] Length of restrictSids array
544 * restrictSids [I] Array of SIDs to restrict in the new token
545 * newToken [O] Address where the new token is stored
551 BOOL WINAPI
CreateRestrictedToken(
555 PSID_AND_ATTRIBUTES disableSids
,
557 PLUID_AND_ATTRIBUTES deletePrivs
,
559 PSID_AND_ATTRIBUTES restrictSids
,
563 SECURITY_IMPERSONATION_LEVEL level
= TokenImpersonationLevel
;
566 FIXME("(%p, 0x%x, %u, %p, %u, %p, %u, %p, %p): stub\n",
567 baseToken
, flags
, nDisableSids
, disableSids
,
568 nDeletePrivs
, deletePrivs
,
569 nRestrictSids
, restrictSids
,
573 if (!GetTokenInformation( baseToken
, TokenType
, &type
, size
, &size
)) return FALSE
;
574 if (type
== TokenImpersonation
)
576 size
= sizeof(level
);
577 if (!GetTokenInformation( baseToken
, TokenImpersonationLevel
, &level
, size
, &size
))
580 return DuplicateTokenEx( baseToken
, MAXIMUM_ALLOWED
, NULL
, level
, type
, newToken
);
583 /******************************************************************************
584 * AllocateAndInitializeSid [ADVAPI32.@]
587 * pIdentifierAuthority []
588 * nSubAuthorityCount []
600 AllocateAndInitializeSid( PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
601 BYTE nSubAuthorityCount
,
602 DWORD nSubAuthority0
, DWORD nSubAuthority1
,
603 DWORD nSubAuthority2
, DWORD nSubAuthority3
,
604 DWORD nSubAuthority4
, DWORD nSubAuthority5
,
605 DWORD nSubAuthority6
, DWORD nSubAuthority7
,
608 return set_ntstatus( RtlAllocateAndInitializeSid(
609 pIdentifierAuthority
, nSubAuthorityCount
,
610 nSubAuthority0
, nSubAuthority1
, nSubAuthority2
, nSubAuthority3
,
611 nSubAuthority4
, nSubAuthority5
, nSubAuthority6
, nSubAuthority7
,
619 * Docs says this function does NOT return a value
620 * even thou it's defined to return a PVOID...
626 return RtlFreeSid(pSid
);
633 CopySid(DWORD nDestinationSidLength
,
634 PSID pDestinationSid
,
639 Status
= RtlCopySid(nDestinationSidLength
,
642 if (!NT_SUCCESS (Status
))
644 SetLastError(RtlNtStatusToDosError(Status
));
656 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
657 IN PSID DomainSid OPTIONAL
,
662 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
664 if (cbSid
== NULL
|| (DomainSid
&& !IsValidSid(DomainSid
)))
666 SetLastError(ERROR_INVALID_PARAMETER
);
670 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++) {
671 if (WellKnownSids
[i
].Type
== WellKnownSidType
) {
672 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
677 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
682 SetLastError(ERROR_INVALID_PARAMETER
);
685 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
691 if (DomainSid
== NULL
|| *GetSidSubAuthorityCount(DomainSid
) == SID_MAX_SUB_AUTHORITIES
)
693 SetLastError(ERROR_INVALID_PARAMETER
);
697 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
698 if (WellKnownRids
[i
].Type
== WellKnownSidType
) {
699 UCHAR domain_subauth
= *GetSidSubAuthorityCount(DomainSid
);
700 DWORD domain_sid_length
= GetSidLengthRequired(domain_subauth
);
701 DWORD output_sid_length
= GetSidLengthRequired(domain_subauth
+ 1);
703 if (*cbSid
< output_sid_length
)
705 *cbSid
= output_sid_length
;
706 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
711 SetLastError(ERROR_INVALID_PARAMETER
);
714 CopyMemory(pSid
, DomainSid
, domain_sid_length
);
715 (*GetSidSubAuthorityCount(pSid
))++;
716 (*GetSidSubAuthority(pSid
, domain_subauth
)) = WellKnownRids
[i
].Rid
;
717 *cbSid
= output_sid_length
;
721 SetLastError(ERROR_INVALID_PARAMETER
);
730 IsWellKnownSid(IN PSID pSid
,
731 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
734 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
736 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
738 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
740 if (EqualSid(pSid
, (PSID
)(&WellKnownSids
[i
].Sid
.Revision
)))
753 IsValidSid(PSID pSid
)
755 return (BOOL
)RtlValidSid(pSid
);
766 SetLastError(ERROR_SUCCESS
);
767 return RtlEqualSid (pSid1
, pSid2
);
775 EqualPrefixSid(PSID pSid1
,
778 return RtlEqualPrefixSid (pSid1
, pSid2
);
786 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
788 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
796 InitializeSid(PSID Sid
,
797 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
798 BYTE nSubAuthorityCount
)
802 Status
= RtlInitializeSid(Sid
,
803 pIdentifierAuthority
,
805 if (!NT_SUCCESS(Status
))
807 SetLastError(RtlNtStatusToDosError(Status
));
817 PSID_IDENTIFIER_AUTHORITY
819 GetSidIdentifierAuthority(PSID pSid
)
821 return RtlIdentifierAuthoritySid(pSid
);
829 GetSidSubAuthority(PSID pSid
,
832 SetLastError(ERROR_SUCCESS
);
833 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
841 GetSidSubAuthorityCount(PSID pSid
)
843 SetLastError(ERROR_SUCCESS
);
844 return RtlSubAuthorityCountSid(pSid
);
852 GetLengthSid(PSID pSid
)
854 return (DWORD
)RtlLengthSid(pSid
);
857 /******************************************************************************
858 * GetKernelObjectSecurity [ADVAPI32.@]
860 BOOL WINAPI
GetKernelObjectSecurity(
862 SECURITY_INFORMATION RequestedInformation
,
863 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
865 LPDWORD lpnLengthNeeded
)
867 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n", Handle
, RequestedInformation
,
868 pSecurityDescriptor
, nLength
, lpnLengthNeeded
);
870 return set_ntstatus( NtQuerySecurityObject(Handle
, RequestedInformation
, pSecurityDescriptor
,
871 nLength
, lpnLengthNeeded
));
879 InitializeAcl(PACL pAcl
,
885 Status
= RtlCreateAcl(pAcl
,
888 if (!NT_SUCCESS(Status
))
890 SetLastError(RtlNtStatusToDosError(Status
));
897 /**********************************************************************
898 * ImpersonateNamedPipeClient EXPORTED
904 ImpersonateNamedPipeClient(HANDLE hNamedPipe
)
906 IO_STATUS_BLOCK StatusBlock
;
909 TRACE("ImpersonateNamedPipeClient() called\n");
911 Status
= NtFsControlFile(hNamedPipe
,
916 FSCTL_PIPE_IMPERSONATE
,
921 if (!NT_SUCCESS(Status
))
923 SetLastError(RtlNtStatusToDosError(Status
));
935 AddAccessAllowedAce(PACL pAcl
,
942 Status
= RtlAddAccessAllowedAce(pAcl
,
946 if (!NT_SUCCESS(Status
))
948 SetLastError(RtlNtStatusToDosError(Status
));
959 AddAccessAllowedAceEx(PACL pAcl
,
967 Status
= RtlAddAccessAllowedAceEx(pAcl
,
972 if (!NT_SUCCESS(Status
))
974 SetLastError(RtlNtStatusToDosError(Status
));
986 AddAccessDeniedAce(PACL pAcl
,
993 Status
= RtlAddAccessDeniedAce(pAcl
,
997 if (!NT_SUCCESS(Status
))
999 SetLastError(RtlNtStatusToDosError(Status
));
1010 AddAccessDeniedAceEx(PACL pAcl
,
1011 DWORD dwAceRevision
,
1018 Status
= RtlAddAccessDeniedAceEx(pAcl
,
1023 if (!NT_SUCCESS(Status
))
1025 SetLastError(RtlNtStatusToDosError(Status
));
1038 DWORD dwAceRevision
,
1039 DWORD dwStartingAceIndex
,
1041 DWORD nAceListLength
)
1045 Status
= RtlAddAce(pAcl
,
1050 if (!NT_SUCCESS(Status
))
1052 SetLastError(RtlNtStatusToDosError(Status
));
1064 DeleteAce(PACL pAcl
,
1069 Status
= RtlDeleteAce(pAcl
,
1071 if (!NT_SUCCESS(Status
))
1073 SetLastError(RtlNtStatusToDosError(Status
));
1085 FindFirstFreeAce(PACL pAcl
,
1088 return RtlFirstFreeAce(pAcl
,
1104 Status
= RtlGetAce(pAcl
,
1107 if (!NT_SUCCESS(Status
))
1109 SetLastError(RtlNtStatusToDosError(Status
));
1121 GetAclInformation(PACL pAcl
,
1122 LPVOID pAclInformation
,
1123 DWORD nAclInformationLength
,
1124 ACL_INFORMATION_CLASS dwAclInformationClass
)
1128 Status
= RtlQueryInformationAcl(pAcl
,
1130 nAclInformationLength
,
1131 dwAclInformationClass
);
1132 if (!NT_SUCCESS(Status
))
1134 SetLastError(RtlNtStatusToDosError(Status
));
1146 IsValidAcl(PACL pAcl
)
1148 return RtlValidAcl (pAcl
);
1155 AllocateLocallyUniqueId(PLUID Luid
)
1159 Status
= NtAllocateLocallyUniqueId (Luid
);
1160 if (!NT_SUCCESS (Status
))
1162 SetLastError(RtlNtStatusToDosError(Status
));
1169 /**********************************************************************
1170 * LookupPrivilegeDisplayNameA EXPORTED
1176 LookupPrivilegeDisplayNameA(LPCSTR lpSystemName
,
1178 LPSTR lpDisplayName
,
1179 LPDWORD cbDisplayName
,
1180 LPDWORD lpLanguageId
)
1182 FIXME("%s() not implemented!\n", __FUNCTION__
);
1183 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
1188 /**********************************************************************
1189 * LookupPrivilegeDisplayNameW EXPORTED
1195 LookupPrivilegeDisplayNameW(LPCWSTR lpSystemName
,
1197 LPWSTR lpDisplayName
,
1198 LPDWORD cbDisplayName
,
1199 LPDWORD lpLanguageId
)
1201 FIXME("%s() not implemented!\n", __FUNCTION__
);
1202 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
1206 /**********************************************************************
1207 * LookupPrivilegeNameA EXPORTED
1213 LookupPrivilegeNameA(LPCSTR lpSystemName
,
1218 UNICODE_STRING lpSystemNameW
;
1222 TRACE("%s %p %p %p\n", debugstr_a(lpSystemName
), lpLuid
, lpName
, cchName
);
1224 RtlCreateUnicodeStringFromAsciiz(&lpSystemNameW
, lpSystemName
);
1225 ret
= LookupPrivilegeNameW(lpSystemNameW
.Buffer
, lpLuid
, NULL
, &wLen
);
1226 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1228 LPWSTR lpNameW
= HeapAlloc(GetProcessHeap(), 0, wLen
* sizeof(WCHAR
));
1230 ret
= LookupPrivilegeNameW(lpSystemNameW
.Buffer
, lpLuid
, lpNameW
,
1234 /* Windows crashes if cchName is NULL, so will I */
1235 unsigned int len
= WideCharToMultiByte(CP_ACP
, 0, lpNameW
, -1, lpName
,
1236 *cchName
, NULL
, NULL
);
1240 /* WideCharToMultiByte failed */
1243 else if (len
> *cchName
)
1246 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1251 /* WideCharToMultiByte succeeded, output length needs to be
1252 * length not including NULL terminator
1257 HeapFree(GetProcessHeap(), 0, lpNameW
);
1259 RtlFreeUnicodeString(&lpSystemNameW
);
1263 /******************************************************************************
1264 * GetFileSecurityA [ADVAPI32.@]
1266 * Obtains Specified information about the security of a file or directory.
1269 * lpFileName [I] Name of the file to get info for
1270 * RequestedInformation [I] SE_ flags from "winnt.h"
1271 * pSecurityDescriptor [O] Destination for security information
1272 * nLength [I] Length of pSecurityDescriptor
1273 * lpnLengthNeeded [O] Destination for length of returned security information
1276 * Success: TRUE. pSecurityDescriptor contains the requested information.
1277 * Failure: FALSE. lpnLengthNeeded contains the required space to return the info.
1280 * The information returned is constrained by the callers access rights and
1287 GetFileSecurityA(LPCSTR lpFileName
,
1288 SECURITY_INFORMATION RequestedInformation
,
1289 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1291 LPDWORD lpnLengthNeeded
)
1293 UNICODE_STRING FileName
;
1296 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
1298 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1302 bResult
= GetFileSecurityW(FileName
.Buffer
,
1303 RequestedInformation
,
1304 pSecurityDescriptor
,
1308 RtlFreeUnicodeString(&FileName
);
1318 GetFileSecurityW(LPCWSTR lpFileName
,
1319 SECURITY_INFORMATION RequestedInformation
,
1320 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1322 LPDWORD lpnLengthNeeded
)
1324 OBJECT_ATTRIBUTES ObjectAttributes
;
1325 IO_STATUS_BLOCK StatusBlock
;
1326 UNICODE_STRING FileName
;
1327 ULONG AccessMask
= 0;
1331 TRACE("GetFileSecurityW() called\n");
1333 QuerySecurityAccessMask(RequestedInformation
, &AccessMask
);
1335 if (!RtlDosPathNameToNtPathName_U(lpFileName
,
1340 ERR("Invalid path\n");
1341 SetLastError(ERROR_INVALID_NAME
);
1345 InitializeObjectAttributes(&ObjectAttributes
,
1347 OBJ_CASE_INSENSITIVE
,
1351 Status
= NtOpenFile(&FileHandle
,
1355 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1358 RtlFreeHeap(RtlGetProcessHeap(),
1362 if (!NT_SUCCESS(Status
))
1364 ERR("NtOpenFile() failed (Status %lx)\n", Status
);
1365 SetLastError(RtlNtStatusToDosError(Status
));
1369 Status
= NtQuerySecurityObject(FileHandle
,
1370 RequestedInformation
,
1371 pSecurityDescriptor
,
1374 NtClose(FileHandle
);
1375 if (!NT_SUCCESS(Status
))
1377 ERR("NtQuerySecurityObject() failed (Status %lx)\n", Status
);
1378 SetLastError(RtlNtStatusToDosError(Status
));
1385 /******************************************************************************
1386 * SetFileSecurityA [ADVAPI32.@]
1387 * Sets the security of a file or directory
1393 SetFileSecurityA(LPCSTR lpFileName
,
1394 SECURITY_INFORMATION SecurityInformation
,
1395 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
1397 UNICODE_STRING FileName
;
1400 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
1402 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1406 bResult
= SetFileSecurityW(FileName
.Buffer
,
1407 SecurityInformation
,
1408 pSecurityDescriptor
);
1410 RtlFreeUnicodeString(&FileName
);
1415 /******************************************************************************
1416 * SetFileSecurityW [ADVAPI32.@]
1417 * Sets the security of a file or directory
1423 SetFileSecurityW(LPCWSTR lpFileName
,
1424 SECURITY_INFORMATION SecurityInformation
,
1425 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
1427 OBJECT_ATTRIBUTES ObjectAttributes
;
1428 IO_STATUS_BLOCK StatusBlock
;
1429 UNICODE_STRING FileName
;
1430 ULONG AccessMask
= 0;
1434 TRACE("SetFileSecurityW() called\n");
1436 SetSecurityAccessMask(SecurityInformation
, &AccessMask
);
1438 if (!RtlDosPathNameToNtPathName_U(lpFileName
,
1443 ERR("Invalid path\n");
1444 SetLastError(ERROR_INVALID_NAME
);
1448 InitializeObjectAttributes(&ObjectAttributes
,
1450 OBJ_CASE_INSENSITIVE
,
1454 Status
= NtOpenFile(&FileHandle
,
1458 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1461 RtlFreeHeap(RtlGetProcessHeap(),
1465 if (!NT_SUCCESS(Status
))
1467 ERR("NtOpenFile() failed (Status %lx)\n", Status
);
1468 SetLastError(RtlNtStatusToDosError(Status
));
1472 Status
= NtSetSecurityObject(FileHandle
,
1473 SecurityInformation
,
1474 pSecurityDescriptor
);
1475 NtClose(FileHandle
);
1477 if (!NT_SUCCESS(Status
))
1479 ERR("NtSetSecurityObject() failed (Status %lx)\n", Status
);
1480 SetLastError(RtlNtStatusToDosError(Status
));
1487 /******************************************************************************
1488 * QueryWindows31FilesMigration [ADVAPI32.@]
1494 QueryWindows31FilesMigration( DWORD x1
)
1496 FIXME("(%d):stub\n",x1
);
1500 /******************************************************************************
1501 * SynchronizeWindows31FilesAndWindowsNTRegistry [ADVAPI32.@]
1510 SynchronizeWindows31FilesAndWindowsNTRegistry( DWORD x1
, DWORD x2
, DWORD x3
,
1513 FIXME("(0x%08x,0x%08x,0x%08x,0x%08x):stub\n",x1
,x2
,x3
,x4
);
1525 HANDLE Token
= NULL
;
1527 Status
= NtSetInformationThread(NtCurrentThread(),
1528 ThreadImpersonationToken
,
1531 if (!NT_SUCCESS(Status
))
1533 SetLastError(RtlNtStatusToDosError(Status
));
1545 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
1549 Status
= RtlImpersonateSelf(ImpersonationLevel
);
1550 if (!NT_SUCCESS(Status
))
1552 SetLastError(RtlNtStatusToDosError(Status
));
1564 AccessCheck(IN PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1565 IN HANDLE ClientToken
,
1566 IN DWORD DesiredAccess
,
1567 IN PGENERIC_MAPPING GenericMapping
,
1568 OUT PPRIVILEGE_SET PrivilegeSet OPTIONAL
,
1569 IN OUT LPDWORD PrivilegeSetLength
,
1570 OUT LPDWORD GrantedAccess
,
1571 OUT LPBOOL AccessStatus
)
1574 NTSTATUS NtAccessStatus
;
1576 /* Do the access check */
1577 Status
= NtAccessCheck(pSecurityDescriptor
,
1582 (PULONG
)PrivilegeSetLength
,
1583 (PACCESS_MASK
)GrantedAccess
,
1586 /* See if the access check operation succeeded */
1587 if (!NT_SUCCESS(Status
))
1590 SetLastError(RtlNtStatusToDosError(Status
));
1594 /* Now check the access status */
1595 if (!NT_SUCCESS(NtAccessStatus
))
1598 SetLastError(RtlNtStatusToDosError(NtAccessStatus
));
1599 *AccessStatus
= FALSE
;
1603 /* Access granted */
1604 *AccessStatus
= TRUE
;
1607 /* Check succeeded */
1614 BOOL WINAPI
AccessCheckByType(
1615 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1616 PSID PrincipalSelfSid
,
1618 DWORD DesiredAccess
,
1619 POBJECT_TYPE_LIST ObjectTypeList
,
1620 DWORD ObjectTypeListLength
,
1621 PGENERIC_MAPPING GenericMapping
,
1622 PPRIVILEGE_SET PrivilegeSet
,
1623 LPDWORD PrivilegeSetLength
,
1624 LPDWORD GrantedAccess
,
1625 LPBOOL AccessStatus
)
1629 *AccessStatus
= TRUE
;
1631 return !*AccessStatus
;
1639 SetKernelObjectSecurity(HANDLE Handle
,
1640 SECURITY_INFORMATION SecurityInformation
,
1641 PSECURITY_DESCRIPTOR SecurityDescriptor
)
1645 Status
= NtSetSecurityObject(Handle
,
1646 SecurityInformation
,
1647 SecurityDescriptor
);
1648 if (!NT_SUCCESS(Status
))
1650 SetLastError(RtlNtStatusToDosError(Status
));
1662 AddAuditAccessAce(PACL pAcl
,
1663 DWORD dwAceRevision
,
1671 Status
= RtlAddAuditAccessAce(pAcl
,
1677 if (!NT_SUCCESS(Status
))
1679 SetLastError(RtlNtStatusToDosError(Status
));
1690 AddAuditAccessAceEx(PACL pAcl
,
1691 DWORD dwAceRevision
,
1700 Status
= RtlAddAuditAccessAceEx(pAcl
,
1707 if (!NT_SUCCESS(Status
))
1709 SetLastError(RtlNtStatusToDosError(Status
));
1716 /******************************************************************************
1717 * LookupAccountNameA [ADVAPI32.@]
1723 LookupAccountNameA(LPCSTR SystemName
,
1727 LPSTR ReferencedDomainName
,
1728 LPDWORD hReferencedDomainNameLength
,
1729 PSID_NAME_USE SidNameUse
)
1732 UNICODE_STRING lpSystemW
;
1733 UNICODE_STRING lpAccountW
;
1734 LPWSTR lpReferencedDomainNameW
= NULL
;
1736 RtlCreateUnicodeStringFromAsciiz(&lpSystemW
, SystemName
);
1737 RtlCreateUnicodeStringFromAsciiz(&lpAccountW
, AccountName
);
1739 if (ReferencedDomainName
)
1740 lpReferencedDomainNameW
= HeapAlloc(GetProcessHeap(),
1742 *hReferencedDomainNameLength
* sizeof(WCHAR
));
1744 ret
= LookupAccountNameW(lpSystemW
.Buffer
,
1748 lpReferencedDomainNameW
,
1749 hReferencedDomainNameLength
,
1752 if (ret
&& lpReferencedDomainNameW
)
1754 WideCharToMultiByte(CP_ACP
,
1756 lpReferencedDomainNameW
,
1757 *hReferencedDomainNameLength
+ 1,
1758 ReferencedDomainName
,
1759 *hReferencedDomainNameLength
+ 1,
1764 RtlFreeUnicodeString(&lpSystemW
);
1765 RtlFreeUnicodeString(&lpAccountW
);
1766 HeapFree(GetProcessHeap(), 0, lpReferencedDomainNameW
);
1771 /**********************************************************************
1772 * PrivilegeCheck EXPORTED
1777 PrivilegeCheck(HANDLE ClientToken
,
1778 PPRIVILEGE_SET RequiredPrivileges
,
1784 Status
= NtPrivilegeCheck(ClientToken
,
1787 if (!NT_SUCCESS(Status
))
1789 SetLastError(RtlNtStatusToDosError(Status
));
1793 *pfResult
= (BOOL
)Result
;
1798 /******************************************************************************
1799 * GetSecurityInfoExW EXPORTED
1803 GetSecurityInfoExA(HANDLE hObject
,
1804 SE_OBJECT_TYPE ObjectType
,
1805 SECURITY_INFORMATION SecurityInfo
,
1808 PACTRL_ACCESSA
*ppAccessList
,
1809 PACTRL_AUDITA
*ppAuditList
,
1813 FIXME("%s() not implemented!\n", __FUNCTION__
);
1814 return ERROR_BAD_PROVIDER
;
1818 /******************************************************************************
1819 * GetSecurityInfoExW EXPORTED
1823 GetSecurityInfoExW(HANDLE hObject
,
1824 SE_OBJECT_TYPE ObjectType
,
1825 SECURITY_INFORMATION SecurityInfo
,
1828 PACTRL_ACCESSW
*ppAccessList
,
1829 PACTRL_AUDITW
*ppAuditList
,
1833 FIXME("%s() not implemented!\n", __FUNCTION__
);
1834 return ERROR_BAD_PROVIDER
;
1837 /******************************************************************************
1838 * BuildExplicitAccessWithNameA [ADVAPI32.@]
1841 BuildExplicitAccessWithNameA(PEXPLICIT_ACCESSA pExplicitAccess
,
1843 DWORD AccessPermissions
,
1844 ACCESS_MODE AccessMode
,
1847 pExplicitAccess
->grfAccessPermissions
= AccessPermissions
;
1848 pExplicitAccess
->grfAccessMode
= AccessMode
;
1849 pExplicitAccess
->grfInheritance
= Inheritance
;
1851 pExplicitAccess
->Trustee
.pMultipleTrustee
= NULL
;
1852 pExplicitAccess
->Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1853 pExplicitAccess
->Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
1854 pExplicitAccess
->Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1855 pExplicitAccess
->Trustee
.ptstrName
= pTrusteeName
;
1859 /******************************************************************************
1860 * BuildExplicitAccessWithNameW [ADVAPI32.@]
1863 BuildExplicitAccessWithNameW(PEXPLICIT_ACCESSW pExplicitAccess
,
1864 LPWSTR pTrusteeName
,
1865 DWORD AccessPermissions
,
1866 ACCESS_MODE AccessMode
,
1869 pExplicitAccess
->grfAccessPermissions
= AccessPermissions
;
1870 pExplicitAccess
->grfAccessMode
= AccessMode
;
1871 pExplicitAccess
->grfInheritance
= Inheritance
;
1873 pExplicitAccess
->Trustee
.pMultipleTrustee
= NULL
;
1874 pExplicitAccess
->Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1875 pExplicitAccess
->Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
1876 pExplicitAccess
->Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1877 pExplicitAccess
->Trustee
.ptstrName
= pTrusteeName
;
1880 /******************************************************************************
1881 * BuildTrusteeWithObjectsAndNameA [ADVAPI32.@]
1883 VOID WINAPI
BuildTrusteeWithObjectsAndNameA( PTRUSTEEA pTrustee
, POBJECTS_AND_NAME_A pObjName
,
1884 SE_OBJECT_TYPE ObjectType
, LPSTR ObjectTypeName
,
1885 LPSTR InheritedObjectTypeName
, LPSTR Name
)
1887 DWORD ObjectsPresent
= 0;
1889 TRACE("%p %p 0x%08x %p %p %s\n", pTrustee
, pObjName
,
1890 ObjectType
, ObjectTypeName
, InheritedObjectTypeName
, debugstr_a(Name
));
1892 /* Fill the OBJECTS_AND_NAME structure */
1893 pObjName
->ObjectType
= ObjectType
;
1894 if (ObjectTypeName
!= NULL
)
1896 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1899 pObjName
->InheritedObjectTypeName
= InheritedObjectTypeName
;
1900 if (InheritedObjectTypeName
!= NULL
)
1902 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1905 pObjName
->ObjectsPresent
= ObjectsPresent
;
1906 pObjName
->ptstrName
= Name
;
1908 /* Fill the TRUSTEE structure */
1909 pTrustee
->pMultipleTrustee
= NULL
;
1910 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1911 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_NAME
;
1912 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1913 pTrustee
->ptstrName
= (LPSTR
)pObjName
;
1916 /******************************************************************************
1917 * BuildTrusteeWithObjectsAndNameW [ADVAPI32.@]
1919 VOID WINAPI
BuildTrusteeWithObjectsAndNameW( PTRUSTEEW pTrustee
, POBJECTS_AND_NAME_W pObjName
,
1920 SE_OBJECT_TYPE ObjectType
, LPWSTR ObjectTypeName
,
1921 LPWSTR InheritedObjectTypeName
, LPWSTR Name
)
1923 DWORD ObjectsPresent
= 0;
1925 TRACE("%p %p 0x%08x %p %p %s\n", pTrustee
, pObjName
,
1926 ObjectType
, ObjectTypeName
, InheritedObjectTypeName
, debugstr_w(Name
));
1928 /* Fill the OBJECTS_AND_NAME structure */
1929 pObjName
->ObjectType
= ObjectType
;
1930 if (ObjectTypeName
!= NULL
)
1932 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1935 pObjName
->InheritedObjectTypeName
= InheritedObjectTypeName
;
1936 if (InheritedObjectTypeName
!= NULL
)
1938 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1941 pObjName
->ObjectsPresent
= ObjectsPresent
;
1942 pObjName
->ptstrName
= Name
;
1944 /* Fill the TRUSTEE structure */
1945 pTrustee
->pMultipleTrustee
= NULL
;
1946 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1947 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_NAME
;
1948 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1949 pTrustee
->ptstrName
= (LPWSTR
)pObjName
;
1952 /******************************************************************************
1953 * BuildTrusteeWithObjectsAndSidA [ADVAPI32.@]
1956 BuildTrusteeWithObjectsAndSidA(PTRUSTEEA pTrustee
,
1957 POBJECTS_AND_SID pObjSid
,
1959 GUID
*pInheritedObjectGuid
,
1962 DWORD ObjectsPresent
= 0;
1964 TRACE("%p %p %p %p %p\n", pTrustee
, pObjSid
, pObjectGuid
, pInheritedObjectGuid
, pSid
);
1966 /* Fill the OBJECTS_AND_SID structure */
1967 if (pObjectGuid
!= NULL
)
1969 pObjSid
->ObjectTypeGuid
= *pObjectGuid
;
1970 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1974 ZeroMemory(&pObjSid
->ObjectTypeGuid
,
1978 if (pInheritedObjectGuid
!= NULL
)
1980 pObjSid
->InheritedObjectTypeGuid
= *pInheritedObjectGuid
;
1981 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1985 ZeroMemory(&pObjSid
->InheritedObjectTypeGuid
,
1989 pObjSid
->ObjectsPresent
= ObjectsPresent
;
1990 pObjSid
->pSid
= pSid
;
1992 /* Fill the TRUSTEE structure */
1993 pTrustee
->pMultipleTrustee
= NULL
;
1994 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1995 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_SID
;
1996 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1997 pTrustee
->ptstrName
= (LPSTR
) pObjSid
;
2001 /******************************************************************************
2002 * BuildTrusteeWithObjectsAndSidW [ADVAPI32.@]
2005 BuildTrusteeWithObjectsAndSidW(PTRUSTEEW pTrustee
,
2006 POBJECTS_AND_SID pObjSid
,
2008 GUID
*pInheritedObjectGuid
,
2011 DWORD ObjectsPresent
= 0;
2013 TRACE("%p %p %p %p %p\n", pTrustee
, pObjSid
, pObjectGuid
, pInheritedObjectGuid
, pSid
);
2015 /* Fill the OBJECTS_AND_SID structure */
2016 if (pObjectGuid
!= NULL
)
2018 pObjSid
->ObjectTypeGuid
= *pObjectGuid
;
2019 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
2023 ZeroMemory(&pObjSid
->ObjectTypeGuid
,
2027 if (pInheritedObjectGuid
!= NULL
)
2029 pObjSid
->InheritedObjectTypeGuid
= *pInheritedObjectGuid
;
2030 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
2034 ZeroMemory(&pObjSid
->InheritedObjectTypeGuid
,
2038 pObjSid
->ObjectsPresent
= ObjectsPresent
;
2039 pObjSid
->pSid
= pSid
;
2041 /* Fill the TRUSTEE structure */
2042 pTrustee
->pMultipleTrustee
= NULL
;
2043 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2044 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_SID
;
2045 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2046 pTrustee
->ptstrName
= (LPWSTR
) pObjSid
;
2049 /******************************************************************************
2050 * BuildTrusteeWithSidA [ADVAPI32.@]
2053 BuildTrusteeWithSidA(PTRUSTEE_A pTrustee
,
2056 TRACE("%p %p\n", pTrustee
, pSid
);
2058 pTrustee
->pMultipleTrustee
= NULL
;
2059 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2060 pTrustee
->TrusteeForm
= TRUSTEE_IS_SID
;
2061 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2062 pTrustee
->ptstrName
= (LPSTR
) pSid
;
2066 /******************************************************************************
2067 * BuildTrusteeWithSidW [ADVAPI32.@]
2070 BuildTrusteeWithSidW(PTRUSTEE_W pTrustee
,
2073 TRACE("%p %p\n", pTrustee
, pSid
);
2075 pTrustee
->pMultipleTrustee
= NULL
;
2076 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2077 pTrustee
->TrusteeForm
= TRUSTEE_IS_SID
;
2078 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2079 pTrustee
->ptstrName
= (LPWSTR
) pSid
;
2082 /******************************************************************************
2083 * BuildTrusteeWithNameA [ADVAPI32.@]
2086 BuildTrusteeWithNameA(PTRUSTEE_A pTrustee
,
2089 TRACE("%p %s\n", pTrustee
, name
);
2091 pTrustee
->pMultipleTrustee
= NULL
;
2092 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2093 pTrustee
->TrusteeForm
= TRUSTEE_IS_NAME
;
2094 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2095 pTrustee
->ptstrName
= name
;
2098 /******************************************************************************
2099 * BuildTrusteeWithNameW [ADVAPI32.@]
2102 BuildTrusteeWithNameW(PTRUSTEE_W pTrustee
,
2105 TRACE("%p %s\n", pTrustee
, name
);
2107 pTrustee
->pMultipleTrustee
= NULL
;
2108 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2109 pTrustee
->TrusteeForm
= TRUSTEE_IS_NAME
;
2110 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2111 pTrustee
->ptstrName
= name
;
2114 /******************************************************************************
2115 * GetTrusteeFormW [ADVAPI32.@]
2118 GetTrusteeFormA(PTRUSTEE_A pTrustee
)
2120 return pTrustee
->TrusteeForm
;
2124 /******************************************************************************
2125 * GetTrusteeFormW [ADVAPI32.@]
2128 GetTrusteeFormW(PTRUSTEE_W pTrustee
)
2130 return pTrustee
->TrusteeForm
;
2133 /******************************************************************************
2134 * GetTrusteeNameA [ADVAPI32.@]
2137 GetTrusteeNameA(PTRUSTEE_A pTrustee
)
2139 return pTrustee
->ptstrName
;
2143 /******************************************************************************
2144 * GetTrusteeNameW [ADVAPI32.@]
2147 GetTrusteeNameW(PTRUSTEE_W pTrustee
)
2149 return pTrustee
->ptstrName
;
2152 /******************************************************************************
2153 * GetTrusteeTypeA [ADVAPI32.@]
2156 GetTrusteeTypeA(PTRUSTEE_A pTrustee
)
2158 return pTrustee
->TrusteeType
;
2161 /******************************************************************************
2162 * GetTrusteeTypeW [ADVAPI32.@]
2165 GetTrusteeTypeW(PTRUSTEE_W pTrustee
)
2167 return pTrustee
->TrusteeType
;
2175 SetAclInformation(PACL pAcl
,
2176 LPVOID pAclInformation
,
2177 DWORD nAclInformationLength
,
2178 ACL_INFORMATION_CLASS dwAclInformationClass
)
2182 Status
= RtlSetInformationAcl(pAcl
,
2184 nAclInformationLength
,
2185 dwAclInformationClass
);
2186 if (!NT_SUCCESS(Status
))
2188 SetLastError(RtlNtStatusToDosError(Status
));
2195 /**********************************************************************
2196 * SetNamedSecurityInfoA EXPORTED
2202 SetNamedSecurityInfoA(LPSTR pObjectName
,
2203 SE_OBJECT_TYPE ObjectType
,
2204 SECURITY_INFORMATION SecurityInfo
,
2210 UNICODE_STRING ObjectName
;
2213 if (!RtlCreateUnicodeStringFromAsciiz(&ObjectName
, pObjectName
))
2215 return ERROR_NOT_ENOUGH_MEMORY
;
2218 Ret
= SetNamedSecurityInfoW(ObjectName
.Buffer
,
2226 RtlFreeUnicodeString(&ObjectName
);
2236 AreAllAccessesGranted(DWORD GrantedAccess
,
2237 DWORD DesiredAccess
)
2239 return (BOOL
)RtlAreAllAccessesGranted(GrantedAccess
,
2248 AreAnyAccessesGranted(DWORD GrantedAccess
,
2249 DWORD DesiredAccess
)
2251 return (BOOL
)RtlAreAnyAccessesGranted(GrantedAccess
,
2255 /******************************************************************************
2256 * ParseAclStringFlags
2258 static DWORD
ParseAclStringFlags(LPCWSTR
* StringAcl
)
2261 LPCWSTR szAcl
= *StringAcl
;
2263 while (*szAcl
!= '(')
2267 flags
|= SE_DACL_PROTECTED
;
2269 else if (*szAcl
== 'A')
2273 flags
|= SE_DACL_AUTO_INHERIT_REQ
;
2274 else if (*szAcl
== 'I')
2275 flags
|= SE_DACL_AUTO_INHERITED
;
2284 /******************************************************************************
2285 * ParseAceStringType
2287 static const ACEFLAG AceType
[] =
2289 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
2290 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
2291 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
2292 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
2294 { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
2295 { SDDL_OBJECT_ACCESS_DENIED, ACCESS_DENIED_OBJECT_ACE_TYPE },
2296 { SDDL_OBJECT_ALARM, SYSTEM_ALARM_OBJECT_ACE_TYPE },
2297 { SDDL_OBJECT_AUDIT, SYSTEM_AUDIT_OBJECT_ACE_TYPE },
2302 static BYTE
ParseAceStringType(LPCWSTR
* StringAcl
)
2305 LPCWSTR szAcl
= *StringAcl
;
2306 const ACEFLAG
*lpaf
= AceType
;
2308 while (lpaf
->wstr
&&
2309 (len
= strlenW(lpaf
->wstr
)) &&
2310 strncmpW(lpaf
->wstr
, szAcl
, len
))
2321 /******************************************************************************
2322 * ParseAceStringFlags
2324 static const ACEFLAG AceFlags
[] =
2326 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
2327 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
2328 { SDDL_INHERITED
, INHERITED_ACE
},
2329 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
2330 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
2331 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
2332 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
2336 static BYTE
ParseAceStringFlags(LPCWSTR
* StringAcl
)
2340 LPCWSTR szAcl
= *StringAcl
;
2342 while (*szAcl
!= ';')
2344 const ACEFLAG
*lpaf
= AceFlags
;
2346 while (lpaf
->wstr
&&
2347 (len
= strlenW(lpaf
->wstr
)) &&
2348 strncmpW(lpaf
->wstr
, szAcl
, len
))
2354 flags
|= lpaf
->value
;
2363 /******************************************************************************
2364 * ParseAceStringRights
2366 static DWORD
ParseAceStringRights(LPCWSTR
* StringAcl
)
2370 LPCWSTR szAcl
= *StringAcl
;
2372 if ((*szAcl
== '0') && (*(szAcl
+ 1) == 'x'))
2376 while (*p
&& *p
!= ';')
2379 if (p
- szAcl
<= 10 /* 8 hex digits + "0x" */ )
2381 rights
= strtoulW(szAcl
, NULL
, 16);
2385 WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl
, p
- szAcl
));
2389 while (*szAcl
!= ';')
2391 const ACEFLAG
*lpaf
= AceRights
;
2393 while (lpaf
->wstr
&&
2394 (len
= strlenW(lpaf
->wstr
)) &&
2395 strncmpW(lpaf
->wstr
, szAcl
, len
))
2403 rights
|= lpaf
->value
;
2413 /******************************************************************************
2414 * ParseStringAclToAcl
2416 * dacl_flags(string_ace1)(string_ace2)... (string_acen)
2419 ParseStringAclToAcl(LPCWSTR StringAcl
,
2426 DWORD length
= sizeof(ACL
);
2429 PACCESS_ALLOWED_ACE pAce
= NULL
; /* pointer to current ACE */
2431 TRACE("%s\n", debugstr_w(StringAcl
));
2436 if (pAcl
) /* pAce is only useful if we're setting values */
2437 pAce
= (PACCESS_ALLOWED_ACE
) (pAcl
+ 1);
2439 /* Parse ACL flags */
2440 *lpdwFlags
= ParseAclStringFlags(&StringAcl
);
2443 while (*StringAcl
== '(')
2447 /* Parse ACE type */
2448 val
= ParseAceStringType(&StringAcl
);
2450 pAce
->Header
.AceType
= (BYTE
) val
;
2451 if (*StringAcl
!= ';')
2455 /* Parse ACE flags */
2456 val
= ParseAceStringFlags(&StringAcl
);
2458 pAce
->Header
.AceFlags
= (BYTE
) val
;
2459 if (*StringAcl
!= ';')
2463 /* Parse ACE rights */
2464 val
= ParseAceStringRights(&StringAcl
);
2467 if (*StringAcl
!= ';')
2471 /* Parse ACE object guid */
2472 if (*StringAcl
!= ';')
2474 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
2479 /* Parse ACE inherit object guid */
2480 if (*StringAcl
!= ';')
2482 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
2487 /* Parse ACE account sid */
2488 if (ParseStringSidToSid(StringAcl
, pAce
? &pAce
->SidStart
: NULL
, &sidlen
))
2490 while (*StringAcl
&& *StringAcl
!= ')')
2494 if (*StringAcl
!= ')')
2498 acesize
= sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
) + sidlen
;
2502 pAce
->Header
.AceSize
= acesize
;
2503 pAce
= (PACCESS_ALLOWED_ACE
)((LPBYTE
)pAce
+ acesize
);
2510 if (length
> 0xffff)
2512 ERR("ACL too large\n");
2518 pAcl
->AclRevision
= ACL_REVISION
;
2520 pAcl
->AclSize
= length
;
2521 pAcl
->AceCount
= acecount
++;
2527 SetLastError(ERROR_INVALID_ACL
);
2528 WARN("Invalid ACE string format\n");
2533 /******************************************************************************
2534 * ParseStringSecurityDescriptorToSecurityDescriptor
2537 ParseStringSecurityDescriptorToSecurityDescriptor(LPCWSTR StringSecurityDescriptor
,
2538 SECURITY_DESCRIPTOR_RELATIVE
* SecurityDescriptor
,
2543 WCHAR tok
[MAX_PATH
];
2545 LPBYTE lpNext
= NULL
;
2548 *cBytes
= sizeof(SECURITY_DESCRIPTOR
);
2550 if (SecurityDescriptor
)
2551 lpNext
= (LPBYTE
)(SecurityDescriptor
+ 1);
2553 while (*StringSecurityDescriptor
)
2555 toktype
= *StringSecurityDescriptor
;
2557 /* Expect char identifier followed by ':' */
2558 StringSecurityDescriptor
++;
2559 if (*StringSecurityDescriptor
!= ':')
2561 SetLastError(ERROR_INVALID_PARAMETER
);
2564 StringSecurityDescriptor
++;
2567 lptoken
= StringSecurityDescriptor
;
2568 while (*lptoken
&& *lptoken
!= ':')
2574 len
= lptoken
- StringSecurityDescriptor
;
2575 memcpy( tok
, StringSecurityDescriptor
, len
* sizeof(WCHAR
) );
2584 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
2587 if (SecurityDescriptor
)
2589 SecurityDescriptor
->Owner
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2590 lpNext
+= bytes
; /* Advance to next token */
2602 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
2605 if (SecurityDescriptor
)
2607 SecurityDescriptor
->Group
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2608 lpNext
+= bytes
; /* Advance to next token */
2621 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
2624 if (SecurityDescriptor
)
2626 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
| flags
;
2627 SecurityDescriptor
->Dacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2628 lpNext
+= bytes
; /* Advance to next token */
2641 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
2644 if (SecurityDescriptor
)
2646 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
| flags
;
2647 SecurityDescriptor
->Sacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2648 lpNext
+= bytes
; /* Advance to next token */
2657 FIXME("Unknown token\n");
2658 SetLastError(ERROR_INVALID_PARAMETER
);
2662 StringSecurityDescriptor
= lptoken
;
2671 /* Winehq cvs 20050916 */
2672 /******************************************************************************
2673 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
2678 ConvertStringSecurityDescriptorToSecurityDescriptorA(LPCSTR StringSecurityDescriptor
,
2679 DWORD StringSDRevision
,
2680 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
2681 PULONG SecurityDescriptorSize
)
2685 LPWSTR StringSecurityDescriptorW
;
2687 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
2688 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2690 if (StringSecurityDescriptorW
)
2692 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
2694 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
2695 StringSDRevision
, SecurityDescriptor
,
2696 SecurityDescriptorSize
);
2697 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
2703 /******************************************************************************
2704 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
2708 ConvertStringSecurityDescriptorToSecurityDescriptorW(LPCWSTR StringSecurityDescriptor
,
2709 DWORD StringSDRevision
,
2710 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
2711 PULONG SecurityDescriptorSize
)
2714 SECURITY_DESCRIPTOR
* psd
;
2717 TRACE("%s\n", debugstr_w(StringSecurityDescriptor
));
2719 if (GetVersion() & 0x80000000)
2721 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2724 else if (!StringSecurityDescriptor
|| !SecurityDescriptor
)
2726 SetLastError(ERROR_INVALID_PARAMETER
);
2729 else if (StringSDRevision
!= SID_REVISION
)
2731 SetLastError(ERROR_UNKNOWN_REVISION
);
2735 /* Compute security descriptor length */
2736 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
2740 psd
= *SecurityDescriptor
= LocalAlloc(GMEM_ZEROINIT
, cBytes
);
2741 if (!psd
) goto lend
;
2743 psd
->Revision
= SID_REVISION
;
2744 psd
->Control
|= SE_SELF_RELATIVE
;
2746 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
2747 (SECURITY_DESCRIPTOR_RELATIVE
*)psd
, &cBytes
))
2753 if (SecurityDescriptorSize
)
2754 *SecurityDescriptorSize
= cBytes
;
2759 TRACE(" ret=%d\n", bret
);
2763 static void DumpString(LPCWSTR string
, int cch
, WCHAR
**pwptr
, ULONG
*plen
)
2766 cch
= strlenW(string
);
2773 memcpy(*pwptr
, string
, sizeof(WCHAR
)*cch
);
2778 static BOOL
DumpSidNumeric(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
2781 WCHAR fmt
[] = { 'S','-','%','u','-','%','d',0 };
2782 WCHAR subauthfmt
[] = { '-','%','u',0 };
2786 if( !IsValidSid( psid
) || pisid
->Revision
!= SDDL_REVISION
)
2788 SetLastError(ERROR_INVALID_SID
);
2792 if (pisid
->IdentifierAuthority
.Value
[0] ||
2793 pisid
->IdentifierAuthority
.Value
[1])
2795 FIXME("not matching MS' bugs\n");
2796 SetLastError(ERROR_INVALID_SID
);
2800 sprintfW( buf
, fmt
, pisid
->Revision
,
2802 MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
2803 pisid
->IdentifierAuthority
.Value
[4] ),
2804 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
2805 pisid
->IdentifierAuthority
.Value
[2] )
2807 DumpString(buf
, -1, pwptr
, plen
);
2809 for( i
=0; i
<pisid
->SubAuthorityCount
; i
++ )
2811 sprintfW( buf
, subauthfmt
, pisid
->SubAuthority
[i
] );
2812 DumpString(buf
, -1, pwptr
, plen
);
2817 static BOOL
DumpSid(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
2820 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
2822 if (WellKnownSids
[i
].wstr
[0] && EqualSid(psid
, (PSID
)&(WellKnownSids
[i
].Sid
.Revision
)))
2824 DumpString(WellKnownSids
[i
].wstr
, 2, pwptr
, plen
);
2829 return DumpSidNumeric(psid
, pwptr
, plen
);
2832 static const LPCWSTR AceRightBitNames
[32] = {
2833 SDDL_CREATE_CHILD
, /* 0 */
2837 SDDL_READ_PROPERTY
, /* 4 */
2838 SDDL_WRITE_PROPERTY
,
2841 SDDL_CONTROL_ACCESS
, /* 8 */
2849 SDDL_STANDARD_DELETE
, /* 16 */
2861 SDDL_GENERIC_ALL
, /* 28 */
2862 SDDL_GENERIC_EXECUTE
,
2867 static void DumpRights(DWORD mask
, WCHAR
**pwptr
, ULONG
*plen
)
2869 static const WCHAR fmtW
[] = {'0','x','%','x',0};
2876 /* first check if the right have name */
2877 for (i
= 0; i
< sizeof(AceRights
)/sizeof(AceRights
[0]); i
++)
2879 if (AceRights
[i
].wstr
== NULL
)
2881 if (mask
== AceRights
[i
].value
)
2883 DumpString(AceRights
[i
].wstr
, -1, pwptr
, plen
);
2888 /* then check if it can be built from bit names */
2889 for (i
= 0; i
< 32; i
++)
2891 if ((mask
& (1 << i
)) && (AceRightBitNames
[i
] == NULL
))
2893 /* can't be built from bit names */
2894 sprintfW(buf
, fmtW
, mask
);
2895 DumpString(buf
, -1, pwptr
, plen
);
2900 /* build from bit names */
2901 for (i
= 0; i
< 32; i
++)
2902 if (mask
& (1 << i
))
2903 DumpString(AceRightBitNames
[i
], -1, pwptr
, plen
);
2906 static BOOL
DumpAce(LPVOID pace
, WCHAR
**pwptr
, ULONG
*plen
)
2908 ACCESS_ALLOWED_ACE
*piace
; /* all the supported ACEs have the same memory layout */
2909 static const WCHAR openbr
= '(';
2910 static const WCHAR closebr
= ')';
2911 static const WCHAR semicolon
= ';';
2913 if (((PACE_HEADER
)pace
)->AceType
> SYSTEM_ALARM_ACE_TYPE
|| ((PACE_HEADER
)pace
)->AceSize
< sizeof(ACCESS_ALLOWED_ACE
))
2915 SetLastError(ERROR_INVALID_ACL
);
2920 DumpString(&openbr
, 1, pwptr
, plen
);
2921 switch (piace
->Header
.AceType
)
2923 case ACCESS_ALLOWED_ACE_TYPE
:
2924 DumpString(SDDL_ACCESS_ALLOWED
, -1, pwptr
, plen
);
2926 case ACCESS_DENIED_ACE_TYPE
:
2927 DumpString(SDDL_ACCESS_DENIED
, -1, pwptr
, plen
);
2929 case SYSTEM_AUDIT_ACE_TYPE
:
2930 DumpString(SDDL_AUDIT
, -1, pwptr
, plen
);
2932 case SYSTEM_ALARM_ACE_TYPE
:
2933 DumpString(SDDL_ALARM
, -1, pwptr
, plen
);
2936 DumpString(&semicolon
, 1, pwptr
, plen
);
2938 if (piace
->Header
.AceFlags
& OBJECT_INHERIT_ACE
)
2939 DumpString(SDDL_OBJECT_INHERIT
, -1, pwptr
, plen
);
2940 if (piace
->Header
.AceFlags
& CONTAINER_INHERIT_ACE
)
2941 DumpString(SDDL_CONTAINER_INHERIT
, -1, pwptr
, plen
);
2942 if (piace
->Header
.AceFlags
& NO_PROPAGATE_INHERIT_ACE
)
2943 DumpString(SDDL_NO_PROPAGATE
, -1, pwptr
, plen
);
2944 if (piace
->Header
.AceFlags
& INHERIT_ONLY_ACE
)
2945 DumpString(SDDL_INHERIT_ONLY
, -1, pwptr
, plen
);
2946 if (piace
->Header
.AceFlags
& INHERITED_ACE
)
2947 DumpString(SDDL_INHERITED
, -1, pwptr
, plen
);
2948 if (piace
->Header
.AceFlags
& SUCCESSFUL_ACCESS_ACE_FLAG
)
2949 DumpString(SDDL_AUDIT_SUCCESS
, -1, pwptr
, plen
);
2950 if (piace
->Header
.AceFlags
& FAILED_ACCESS_ACE_FLAG
)
2951 DumpString(SDDL_AUDIT_FAILURE
, -1, pwptr
, plen
);
2952 DumpString(&semicolon
, 1, pwptr
, plen
);
2953 DumpRights(piace
->Mask
, pwptr
, plen
);
2954 DumpString(&semicolon
, 1, pwptr
, plen
);
2955 /* objects not supported */
2956 DumpString(&semicolon
, 1, pwptr
, plen
);
2957 /* objects not supported */
2958 DumpString(&semicolon
, 1, pwptr
, plen
);
2959 if (!DumpSid((PSID
)&piace
->SidStart
, pwptr
, plen
))
2961 DumpString(&closebr
, 1, pwptr
, plen
);
2965 static BOOL
DumpAcl(PACL pacl
, WCHAR
**pwptr
, ULONG
*plen
, BOOL
protected, BOOL autoInheritReq
, BOOL autoInherited
)
2971 DumpString(SDDL_PROTECTED
, -1, pwptr
, plen
);
2973 DumpString(SDDL_AUTO_INHERIT_REQ
, -1, pwptr
, plen
);
2975 DumpString(SDDL_AUTO_INHERITED
, -1, pwptr
, plen
);
2980 if (!IsValidAcl(pacl
))
2983 count
= pacl
->AceCount
;
2984 for (i
= 0; i
< count
; i
++)
2987 if (!GetAce(pacl
, i
, &ace
))
2989 if (!DumpAce(ace
, pwptr
, plen
))
2996 static BOOL
DumpOwner(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
2998 static const WCHAR prefix
[] = {'O',':',0};
3002 if (!GetSecurityDescriptorOwner(SecurityDescriptor
, &psid
, &bDefaulted
))
3008 DumpString(prefix
, -1, pwptr
, plen
);
3009 if (!DumpSid(psid
, pwptr
, plen
))
3014 static BOOL
DumpGroup(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3016 static const WCHAR prefix
[] = {'G',':',0};
3020 if (!GetSecurityDescriptorGroup(SecurityDescriptor
, &psid
, &bDefaulted
))
3026 DumpString(prefix
, -1, pwptr
, plen
);
3027 if (!DumpSid(psid
, pwptr
, plen
))
3032 static BOOL
DumpDacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3034 static const WCHAR dacl
[] = {'D',':',0};
3035 SECURITY_DESCRIPTOR_CONTROL control
;
3036 BOOL present
, defaulted
;
3040 if (!GetSecurityDescriptorDacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
3043 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
3049 DumpString(dacl
, 2, pwptr
, plen
);
3050 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_DACL_PROTECTED
, control
& SE_DACL_AUTO_INHERIT_REQ
, control
& SE_DACL_AUTO_INHERITED
))
3055 static BOOL
DumpSacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3057 static const WCHAR sacl
[] = {'S',':',0};
3058 SECURITY_DESCRIPTOR_CONTROL control
;
3059 BOOL present
, defaulted
;
3063 if (!GetSecurityDescriptorSacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
3066 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
3072 DumpString(sacl
, 2, pwptr
, plen
);
3073 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_SACL_PROTECTED
, control
& SE_SACL_AUTO_INHERIT_REQ
, control
& SE_SACL_AUTO_INHERITED
))
3078 /******************************************************************************
3079 * ConvertSecurityDescriptorToStringSecurityDescriptorW [ADVAPI32.@]
3083 ConvertSecurityDescriptorToStringSecurityDescriptorW(PSECURITY_DESCRIPTOR SecurityDescriptor
,
3085 SECURITY_INFORMATION SecurityInformation
,
3086 LPWSTR
*OutputString
,
3092 if (SDRevision
!= SDDL_REVISION_1
)
3094 ERR("Program requested unknown SDDL revision %d\n", SDRevision
);
3095 SetLastError(ERROR_UNKNOWN_REVISION
);
3100 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
3101 if (!DumpOwner(SecurityDescriptor
, NULL
, &len
))
3103 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
3104 if (!DumpGroup(SecurityDescriptor
, NULL
, &len
))
3106 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
3107 if (!DumpDacl(SecurityDescriptor
, NULL
, &len
))
3109 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
3110 if (!DumpSacl(SecurityDescriptor
, NULL
, &len
))
3113 wstr
= wptr
= LocalAlloc(0, (len
+ 1)*sizeof(WCHAR
));
3117 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
3118 if (!DumpOwner(SecurityDescriptor
, &wptr
, NULL
))
3120 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
3121 if (!DumpGroup(SecurityDescriptor
, &wptr
, NULL
))
3123 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
3124 if (!DumpDacl(SecurityDescriptor
, &wptr
, NULL
))
3126 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
3127 if (!DumpSacl(SecurityDescriptor
, &wptr
, NULL
))
3131 TRACE("ret: %s, %d\n", wine_dbgstr_w(wstr
), len
);
3132 *OutputString
= wstr
;
3134 *OutputLen
= strlenW(*OutputString
)+1;
3138 /******************************************************************************
3139 * ConvertSecurityDescriptorToStringSecurityDescriptorA [ADVAPI32.@]
3143 ConvertSecurityDescriptorToStringSecurityDescriptorA(PSECURITY_DESCRIPTOR SecurityDescriptor
,
3145 SECURITY_INFORMATION Information
,
3146 LPSTR
*OutputString
,
3152 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
3156 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
3157 *OutputString
= HeapAlloc(GetProcessHeap(), 0, lenA
);
3158 if (*OutputString
== NULL
)
3164 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
3167 if (OutputLen
!= NULL
)
3173 *OutputString
= NULL
;
3185 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
3189 DWORD i
, cBytes
, identAuth
, csubauth
;
3193 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
3197 SetLastError(ERROR_INVALID_SID
);
3201 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
3203 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
3205 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
3206 size
= SECURITY_MAX_SID_SIZE
;
3207 *sid
= LocalAlloc(0, size
);
3210 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3213 ret
= CreateWellKnownSid(knownSid
,
3219 SetLastError(ERROR_INVALID_SID
);
3226 /* That's probably a string S-R-I-S-S... */
3227 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
3229 SetLastError(ERROR_INVALID_SID
);
3233 cBytes
= ComputeStringSidSize(StringSid
);
3234 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
3237 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3242 csubauth
= ((cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
3244 StringSid
+= 2; /* Advance to Revision */
3245 pisid
->Revision
= atoiW(StringSid
);
3247 if (pisid
->Revision
!= SDDL_REVISION
)
3249 TRACE("Revision %d is unknown\n", pisid
->Revision
);
3250 goto lend
; /* ERROR_INVALID_SID */
3254 TRACE("SubAuthorityCount is 0\n");
3255 goto lend
; /* ERROR_INVALID_SID */
3258 pisid
->SubAuthorityCount
= csubauth
;
3260 /* Advance to identifier authority */
3261 while (*StringSid
&& *StringSid
!= '-')
3263 if (*StringSid
== '-')
3266 /* MS' implementation can't handle values greater than 2^32 - 1, so
3267 * we don't either; assume most significant bytes are always 0
3269 pisid
->IdentifierAuthority
.Value
[0] = 0;
3270 pisid
->IdentifierAuthority
.Value
[1] = 0;
3271 identAuth
= atoiW(StringSid
);
3272 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
3273 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
3274 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
3275 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
3277 /* Advance to first sub authority */
3278 while (*StringSid
&& *StringSid
!= '-')
3280 if (*StringSid
== '-')
3285 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
3287 while (*StringSid
&& *StringSid
!= '-')
3289 if (*StringSid
== '-')
3293 if (i
!= pisid
->SubAuthorityCount
)
3294 goto lend
; /* ERROR_INVALID_SID */
3303 SetLastError(ERROR_INVALID_SID
);
3306 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");
3315 ConvertStringSidToSidA(IN LPCSTR StringSid
,
3318 BOOL bRetVal
= FALSE
;
3320 TRACE("%s, %p\n", debugstr_a(StringSid
), sid
);
3321 if (GetVersion() & 0x80000000)
3322 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3323 else if (!StringSid
|| !sid
)
3324 SetLastError(ERROR_INVALID_PARAMETER
);
3327 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
3328 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3329 if (wStringSid
== NULL
)
3331 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
3332 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
3333 HeapFree(GetProcessHeap(), 0, wStringSid
);
3343 ConvertSidToStringSidW(PSID Sid
,
3347 UNICODE_STRING UnicodeString
;
3348 WCHAR FixedBuffer
[64];
3350 if (!RtlValidSid(Sid
))
3352 SetLastError(ERROR_INVALID_SID
);
3356 UnicodeString
.Length
= 0;
3357 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
3358 UnicodeString
.Buffer
= FixedBuffer
;
3359 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
3360 if (STATUS_BUFFER_TOO_SMALL
== Status
)
3362 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
3365 if (!NT_SUCCESS(Status
))
3367 SetLastError(RtlNtStatusToDosError(Status
));
3371 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
3372 if (NULL
== *StringSid
)
3374 if (UnicodeString
.Buffer
!= FixedBuffer
)
3376 RtlFreeUnicodeString(&UnicodeString
);
3378 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3382 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
3383 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
3384 if (UnicodeString
.Buffer
!= FixedBuffer
)
3386 RtlFreeUnicodeString(&UnicodeString
);
3397 ConvertSidToStringSidA(PSID Sid
,
3403 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
3408 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
3411 LocalFree(StringSidW
);
3412 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3416 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
3417 if (NULL
== *StringSid
)
3419 LocalFree(StringSidW
);
3420 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3424 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
3426 LocalFree(StringSid
);
3427 LocalFree(StringSidW
);
3431 LocalFree(StringSidW
);
3440 CreateProcessWithLogonW(LPCWSTR lpUsername
,
3444 LPCWSTR lpApplicationName
,
3445 LPWSTR lpCommandLine
,
3446 DWORD dwCreationFlags
,
3447 LPVOID lpEnvironment
,
3448 LPCWSTR lpCurrentDirectory
,
3449 LPSTARTUPINFOW lpStartupInfo
,
3450 LPPROCESS_INFORMATION lpProcessInformation
)
3452 FIXME("%s %s %s 0x%08x %s %s 0x%08x %p %s %p %p stub\n", debugstr_w(lpUsername
), debugstr_w(lpDomain
),
3453 debugstr_w(lpPassword
), dwLogonFlags
, debugstr_w(lpApplicationName
),
3454 debugstr_w(lpCommandLine
), dwCreationFlags
, lpEnvironment
, debugstr_w(lpCurrentDirectory
),
3455 lpStartupInfo
, lpProcessInformation
);
3462 CreateProcessWithTokenW(IN HANDLE hToken
,
3463 IN DWORD dwLogonFlags
,
3464 IN LPCWSTR lpApplicationName OPTIONAL
,
3465 IN OUT LPWSTR lpCommandLine OPTIONAL
,
3466 IN DWORD dwCreationFlags
,
3467 IN LPVOID lpEnvironment OPTIONAL
,
3468 IN LPCWSTR lpCurrentDirectory OPTIONAL
,
3469 IN LPSTARTUPINFOW lpStartupInfo
,
3470 OUT LPPROCESS_INFORMATION lpProcessInfo
)
3480 DuplicateTokenEx(IN HANDLE ExistingTokenHandle
,
3481 IN DWORD dwDesiredAccess
,
3482 IN LPSECURITY_ATTRIBUTES lpTokenAttributes OPTIONAL
,
3483 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
,
3484 IN TOKEN_TYPE TokenType
,
3485 OUT PHANDLE DuplicateTokenHandle
)
3487 OBJECT_ATTRIBUTES ObjectAttributes
;
3489 SECURITY_QUALITY_OF_SERVICE Sqos
;
3491 TRACE("%p 0x%08x 0x%08x 0x%08x %p\n", ExistingTokenHandle
, dwDesiredAccess
,
3492 ImpersonationLevel
, TokenType
, DuplicateTokenHandle
);
3494 Sqos
.Length
= sizeof(SECURITY_QUALITY_OF_SERVICE
);
3495 Sqos
.ImpersonationLevel
= ImpersonationLevel
;
3496 Sqos
.ContextTrackingMode
= 0;
3497 Sqos
.EffectiveOnly
= FALSE
;
3499 if (lpTokenAttributes
!= NULL
)
3501 InitializeObjectAttributes(&ObjectAttributes
,
3503 lpTokenAttributes
->bInheritHandle
? OBJ_INHERIT
: 0,
3505 lpTokenAttributes
->lpSecurityDescriptor
);
3509 InitializeObjectAttributes(&ObjectAttributes
,
3516 ObjectAttributes
.SecurityQualityOfService
= &Sqos
;
3518 Status
= NtDuplicateToken(ExistingTokenHandle
,
3523 DuplicateTokenHandle
);
3524 if (!NT_SUCCESS(Status
))
3526 ERR("NtDuplicateToken failed: Status %08x\n", Status
);
3527 SetLastError(RtlNtStatusToDosError(Status
));
3531 TRACE("Returning token %p.\n", *DuplicateTokenHandle
);
3540 DuplicateToken(IN HANDLE ExistingTokenHandle
,
3541 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
,
3542 OUT PHANDLE DuplicateTokenHandle
)
3544 return DuplicateTokenEx(ExistingTokenHandle
,
3545 TOKEN_IMPERSONATE
| TOKEN_QUERY
,
3549 DuplicateTokenHandle
);
3552 /******************************************************************************
3553 * ComputeStringSidSize
3555 static DWORD
ComputeStringSidSize(LPCWSTR StringSid
)
3557 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I(-S)+ */
3562 if (*StringSid
== '-')
3568 return GetSidLengthRequired(ctok
- 2);
3570 else /* String constant format - Only available in winxp and above */
3574 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
3575 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
3576 return GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
3579 return GetSidLengthRequired(0);
3582 /******************************************************************************
3583 * ParseStringSidToSid
3585 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
)
3590 TRACE("%s, %p, %p\n", debugstr_w(StringSid
), pSid
, cBytes
);
3593 SetLastError(ERROR_INVALID_PARAMETER
);
3594 TRACE("StringSid is NULL, returning FALSE\n");
3598 while (*StringSid
== ' ')
3601 *cBytes
= ComputeStringSidSize(StringSid
);
3602 if (!pisid
) /* Simply compute the size */
3604 TRACE("only size requested, returning TRUE\n");
3608 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
3610 DWORD i
= 0, identAuth
;
3611 DWORD csubauth
= ((*cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
3613 StringSid
+= 2; /* Advance to Revision */
3614 pisid
->Revision
= atoiW(StringSid
);
3616 if (pisid
->Revision
!= SDDL_REVISION
)
3618 TRACE("Revision %d is unknown\n", pisid
->Revision
);
3619 goto lend
; /* ERROR_INVALID_SID */
3623 TRACE("SubAuthorityCount is 0\n");
3624 goto lend
; /* ERROR_INVALID_SID */
3627 pisid
->SubAuthorityCount
= csubauth
;
3629 /* Advance to identifier authority */
3630 while (*StringSid
&& *StringSid
!= '-')
3632 if (*StringSid
== '-')
3635 /* MS' implementation can't handle values greater than 2^32 - 1, so
3636 * we don't either; assume most significant bytes are always 0
3638 pisid
->IdentifierAuthority
.Value
[0] = 0;
3639 pisid
->IdentifierAuthority
.Value
[1] = 0;
3640 identAuth
= atoiW(StringSid
);
3641 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
3642 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
3643 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
3644 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
3646 /* Advance to first sub authority */
3647 while (*StringSid
&& *StringSid
!= '-')
3649 if (*StringSid
== '-')
3654 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
3656 while (*StringSid
&& *StringSid
!= '-')
3658 if (*StringSid
== '-')
3662 if (i
!= pisid
->SubAuthorityCount
)
3663 goto lend
; /* ERROR_INVALID_SID */
3667 else /* String constant format - Only available in winxp and above */
3670 pisid
->Revision
= SDDL_REVISION
;
3672 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
3673 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
3676 pisid
->SubAuthorityCount
= WellKnownSids
[i
].Sid
.SubAuthorityCount
;
3677 pisid
->IdentifierAuthority
= WellKnownSids
[i
].Sid
.IdentifierAuthority
;
3678 for (j
= 0; j
< WellKnownSids
[i
].Sid
.SubAuthorityCount
; j
++)
3679 pisid
->SubAuthority
[j
] = WellKnownSids
[i
].Sid
.SubAuthority
[j
];
3684 FIXME("String constant not supported: %s\n", debugstr_wn(StringSid
, 2));
3689 SetLastError(ERROR_INVALID_SID
);
3691 TRACE("returning %s\n", bret
? "TRUE" : "FALSE");
3695 /**********************************************************************
3696 * GetNamedSecurityInfoA EXPORTED
3702 GetNamedSecurityInfoA(LPSTR pObjectName
,
3703 SE_OBJECT_TYPE ObjectType
,
3704 SECURITY_INFORMATION SecurityInfo
,
3709 PSECURITY_DESCRIPTOR
*ppSecurityDescriptor
)
3715 TRACE("%s %d %d %p %p %p %p %p\n", pObjectName
, ObjectType
, SecurityInfo
,
3716 ppsidOwner
, ppsidGroup
, ppDacl
, ppSacl
, ppSecurityDescriptor
);
3720 len
= MultiByteToWideChar( CP_ACP
, 0, pObjectName
, -1, NULL
, 0 );
3721 wstr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
));
3722 MultiByteToWideChar( CP_ACP
, 0, pObjectName
, -1, wstr
, len
);
3725 r
= GetNamedSecurityInfoW( wstr
, ObjectType
, SecurityInfo
, ppsidOwner
,
3726 ppsidGroup
, ppDacl
, ppSacl
, ppSecurityDescriptor
);
3728 HeapFree( GetProcessHeap(), 0, wstr
);
3738 GetWindowsAccountDomainSid(IN PSID pSid
,
3739 OUT PSID ppDomainSid
,
3740 IN OUT DWORD
* cbSid
)
3751 EqualDomainSid(IN PSID pSid1
,