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
);
862 GetKernelObjectSecurity(HANDLE Handle
,
863 SECURITY_INFORMATION RequestedInformation
,
864 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
866 LPDWORD lpnLengthNeeded
)
870 Status
= NtQuerySecurityObject(Handle
,
871 RequestedInformation
,
875 if (!NT_SUCCESS(Status
))
877 SetLastError(RtlNtStatusToDosError(Status
));
889 InitializeAcl(PACL pAcl
,
895 Status
= RtlCreateAcl(pAcl
,
898 if (!NT_SUCCESS(Status
))
900 SetLastError(RtlNtStatusToDosError(Status
));
907 /**********************************************************************
908 * ImpersonateNamedPipeClient EXPORTED
914 ImpersonateNamedPipeClient(HANDLE hNamedPipe
)
916 IO_STATUS_BLOCK StatusBlock
;
919 TRACE("ImpersonateNamedPipeClient() called\n");
921 Status
= NtFsControlFile(hNamedPipe
,
926 FSCTL_PIPE_IMPERSONATE
,
931 if (!NT_SUCCESS(Status
))
933 SetLastError(RtlNtStatusToDosError(Status
));
945 AddAccessAllowedAce(PACL pAcl
,
952 Status
= RtlAddAccessAllowedAce(pAcl
,
956 if (!NT_SUCCESS(Status
))
958 SetLastError(RtlNtStatusToDosError(Status
));
969 AddAccessAllowedAceEx(PACL pAcl
,
977 Status
= RtlAddAccessAllowedAceEx(pAcl
,
982 if (!NT_SUCCESS(Status
))
984 SetLastError(RtlNtStatusToDosError(Status
));
996 AddAccessDeniedAce(PACL pAcl
,
1003 Status
= RtlAddAccessDeniedAce(pAcl
,
1007 if (!NT_SUCCESS(Status
))
1009 SetLastError(RtlNtStatusToDosError(Status
));
1020 AddAccessDeniedAceEx(PACL pAcl
,
1021 DWORD dwAceRevision
,
1028 Status
= RtlAddAccessDeniedAceEx(pAcl
,
1033 if (!NT_SUCCESS(Status
))
1035 SetLastError(RtlNtStatusToDosError(Status
));
1048 DWORD dwAceRevision
,
1049 DWORD dwStartingAceIndex
,
1051 DWORD nAceListLength
)
1055 Status
= RtlAddAce(pAcl
,
1060 if (!NT_SUCCESS(Status
))
1062 SetLastError(RtlNtStatusToDosError(Status
));
1074 DeleteAce(PACL pAcl
,
1079 Status
= RtlDeleteAce(pAcl
,
1081 if (!NT_SUCCESS(Status
))
1083 SetLastError(RtlNtStatusToDosError(Status
));
1095 FindFirstFreeAce(PACL pAcl
,
1098 return RtlFirstFreeAce(pAcl
,
1114 Status
= RtlGetAce(pAcl
,
1117 if (!NT_SUCCESS(Status
))
1119 SetLastError(RtlNtStatusToDosError(Status
));
1131 GetAclInformation(PACL pAcl
,
1132 LPVOID pAclInformation
,
1133 DWORD nAclInformationLength
,
1134 ACL_INFORMATION_CLASS dwAclInformationClass
)
1138 Status
= RtlQueryInformationAcl(pAcl
,
1140 nAclInformationLength
,
1141 dwAclInformationClass
);
1142 if (!NT_SUCCESS(Status
))
1144 SetLastError(RtlNtStatusToDosError(Status
));
1156 IsValidAcl(PACL pAcl
)
1158 return RtlValidAcl (pAcl
);
1165 AllocateLocallyUniqueId(PLUID Luid
)
1169 Status
= NtAllocateLocallyUniqueId (Luid
);
1170 if (!NT_SUCCESS (Status
))
1172 SetLastError(RtlNtStatusToDosError(Status
));
1179 /**********************************************************************
1180 * LookupPrivilegeDisplayNameA EXPORTED
1186 LookupPrivilegeDisplayNameA(LPCSTR lpSystemName
,
1188 LPSTR lpDisplayName
,
1189 LPDWORD cbDisplayName
,
1190 LPDWORD lpLanguageId
)
1192 FIXME("%s() not implemented!\n", __FUNCTION__
);
1193 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
1198 /**********************************************************************
1199 * LookupPrivilegeDisplayNameW EXPORTED
1205 LookupPrivilegeDisplayNameW(LPCWSTR lpSystemName
,
1207 LPWSTR lpDisplayName
,
1208 LPDWORD cbDisplayName
,
1209 LPDWORD lpLanguageId
)
1211 FIXME("%s() not implemented!\n", __FUNCTION__
);
1212 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
1216 /**********************************************************************
1217 * LookupPrivilegeNameA EXPORTED
1223 LookupPrivilegeNameA(LPCSTR lpSystemName
,
1228 UNICODE_STRING lpSystemNameW
;
1232 TRACE("%s %p %p %p\n", debugstr_a(lpSystemName
), lpLuid
, lpName
, cchName
);
1234 RtlCreateUnicodeStringFromAsciiz(&lpSystemNameW
, lpSystemName
);
1235 ret
= LookupPrivilegeNameW(lpSystemNameW
.Buffer
, lpLuid
, NULL
, &wLen
);
1236 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1238 LPWSTR lpNameW
= HeapAlloc(GetProcessHeap(), 0, wLen
* sizeof(WCHAR
));
1240 ret
= LookupPrivilegeNameW(lpSystemNameW
.Buffer
, lpLuid
, lpNameW
,
1244 /* Windows crashes if cchName is NULL, so will I */
1245 unsigned int len
= WideCharToMultiByte(CP_ACP
, 0, lpNameW
, -1, lpName
,
1246 *cchName
, NULL
, NULL
);
1250 /* WideCharToMultiByte failed */
1253 else if (len
> *cchName
)
1256 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1261 /* WideCharToMultiByte succeeded, output length needs to be
1262 * length not including NULL terminator
1267 HeapFree(GetProcessHeap(), 0, lpNameW
);
1269 RtlFreeUnicodeString(&lpSystemNameW
);
1273 /******************************************************************************
1274 * GetFileSecurityA [ADVAPI32.@]
1276 * Obtains Specified information about the security of a file or directory.
1279 * lpFileName [I] Name of the file to get info for
1280 * RequestedInformation [I] SE_ flags from "winnt.h"
1281 * pSecurityDescriptor [O] Destination for security information
1282 * nLength [I] Length of pSecurityDescriptor
1283 * lpnLengthNeeded [O] Destination for length of returned security information
1286 * Success: TRUE. pSecurityDescriptor contains the requested information.
1287 * Failure: FALSE. lpnLengthNeeded contains the required space to return the info.
1290 * The information returned is constrained by the callers access rights and
1297 GetFileSecurityA(LPCSTR lpFileName
,
1298 SECURITY_INFORMATION RequestedInformation
,
1299 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1301 LPDWORD lpnLengthNeeded
)
1303 UNICODE_STRING FileName
;
1306 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
1308 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1312 bResult
= GetFileSecurityW(FileName
.Buffer
,
1313 RequestedInformation
,
1314 pSecurityDescriptor
,
1318 RtlFreeUnicodeString(&FileName
);
1328 GetFileSecurityW(LPCWSTR lpFileName
,
1329 SECURITY_INFORMATION RequestedInformation
,
1330 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1332 LPDWORD lpnLengthNeeded
)
1334 OBJECT_ATTRIBUTES ObjectAttributes
;
1335 IO_STATUS_BLOCK StatusBlock
;
1336 UNICODE_STRING FileName
;
1337 ULONG AccessMask
= 0;
1341 TRACE("GetFileSecurityW() called\n");
1343 QuerySecurityAccessMask(RequestedInformation
, &AccessMask
);
1345 if (!RtlDosPathNameToNtPathName_U(lpFileName
,
1350 ERR("Invalid path\n");
1351 SetLastError(ERROR_INVALID_NAME
);
1355 InitializeObjectAttributes(&ObjectAttributes
,
1357 OBJ_CASE_INSENSITIVE
,
1361 Status
= NtOpenFile(&FileHandle
,
1365 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1368 RtlFreeHeap(RtlGetProcessHeap(),
1372 if (!NT_SUCCESS(Status
))
1374 ERR("NtOpenFile() failed (Status %lx)\n", Status
);
1375 SetLastError(RtlNtStatusToDosError(Status
));
1379 Status
= NtQuerySecurityObject(FileHandle
,
1380 RequestedInformation
,
1381 pSecurityDescriptor
,
1384 NtClose(FileHandle
);
1385 if (!NT_SUCCESS(Status
))
1387 ERR("NtQuerySecurityObject() failed (Status %lx)\n", Status
);
1388 SetLastError(RtlNtStatusToDosError(Status
));
1395 /******************************************************************************
1396 * SetFileSecurityA [ADVAPI32.@]
1397 * Sets the security of a file or directory
1403 SetFileSecurityA(LPCSTR lpFileName
,
1404 SECURITY_INFORMATION SecurityInformation
,
1405 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
1407 UNICODE_STRING FileName
;
1410 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
1412 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1416 bResult
= SetFileSecurityW(FileName
.Buffer
,
1417 SecurityInformation
,
1418 pSecurityDescriptor
);
1420 RtlFreeUnicodeString(&FileName
);
1425 /******************************************************************************
1426 * SetFileSecurityW [ADVAPI32.@]
1427 * Sets the security of a file or directory
1433 SetFileSecurityW(LPCWSTR lpFileName
,
1434 SECURITY_INFORMATION SecurityInformation
,
1435 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
1437 OBJECT_ATTRIBUTES ObjectAttributes
;
1438 IO_STATUS_BLOCK StatusBlock
;
1439 UNICODE_STRING FileName
;
1440 ULONG AccessMask
= 0;
1444 TRACE("SetFileSecurityW() called\n");
1446 SetSecurityAccessMask(SecurityInformation
, &AccessMask
);
1448 if (!RtlDosPathNameToNtPathName_U(lpFileName
,
1453 ERR("Invalid path\n");
1454 SetLastError(ERROR_INVALID_NAME
);
1458 InitializeObjectAttributes(&ObjectAttributes
,
1460 OBJ_CASE_INSENSITIVE
,
1464 Status
= NtOpenFile(&FileHandle
,
1468 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1471 RtlFreeHeap(RtlGetProcessHeap(),
1475 if (!NT_SUCCESS(Status
))
1477 ERR("NtOpenFile() failed (Status %lx)\n", Status
);
1478 SetLastError(RtlNtStatusToDosError(Status
));
1482 Status
= NtSetSecurityObject(FileHandle
,
1483 SecurityInformation
,
1484 pSecurityDescriptor
);
1485 NtClose(FileHandle
);
1487 if (!NT_SUCCESS(Status
))
1489 ERR("NtSetSecurityObject() failed (Status %lx)\n", Status
);
1490 SetLastError(RtlNtStatusToDosError(Status
));
1497 /******************************************************************************
1498 * QueryWindows31FilesMigration [ADVAPI32.@]
1504 QueryWindows31FilesMigration( DWORD x1
)
1506 FIXME("(%d):stub\n",x1
);
1510 /******************************************************************************
1511 * SynchronizeWindows31FilesAndWindowsNTRegistry [ADVAPI32.@]
1520 SynchronizeWindows31FilesAndWindowsNTRegistry( DWORD x1
, DWORD x2
, DWORD x3
,
1523 FIXME("(0x%08x,0x%08x,0x%08x,0x%08x):stub\n",x1
,x2
,x3
,x4
);
1535 HANDLE Token
= NULL
;
1537 Status
= NtSetInformationThread(NtCurrentThread(),
1538 ThreadImpersonationToken
,
1541 if (!NT_SUCCESS(Status
))
1543 SetLastError(RtlNtStatusToDosError(Status
));
1555 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
1559 Status
= RtlImpersonateSelf(ImpersonationLevel
);
1560 if (!NT_SUCCESS(Status
))
1562 SetLastError(RtlNtStatusToDosError(Status
));
1574 AccessCheck(IN PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1575 IN HANDLE ClientToken
,
1576 IN DWORD DesiredAccess
,
1577 IN PGENERIC_MAPPING GenericMapping
,
1578 OUT PPRIVILEGE_SET PrivilegeSet OPTIONAL
,
1579 IN OUT LPDWORD PrivilegeSetLength
,
1580 OUT LPDWORD GrantedAccess
,
1581 OUT LPBOOL AccessStatus
)
1584 NTSTATUS NtAccessStatus
;
1586 /* Do the access check */
1587 Status
= NtAccessCheck(pSecurityDescriptor
,
1592 (PULONG
)PrivilegeSetLength
,
1593 (PACCESS_MASK
)GrantedAccess
,
1596 /* See if the access check operation succeeded */
1597 if (!NT_SUCCESS(Status
))
1600 SetLastError(RtlNtStatusToDosError(Status
));
1604 /* Now check the access status */
1605 if (!NT_SUCCESS(NtAccessStatus
))
1608 SetLastError(RtlNtStatusToDosError(NtAccessStatus
));
1609 *AccessStatus
= FALSE
;
1613 /* Access granted */
1614 *AccessStatus
= TRUE
;
1617 /* Check succeeded */
1624 BOOL WINAPI
AccessCheckByType(
1625 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
1626 PSID PrincipalSelfSid
,
1628 DWORD DesiredAccess
,
1629 POBJECT_TYPE_LIST ObjectTypeList
,
1630 DWORD ObjectTypeListLength
,
1631 PGENERIC_MAPPING GenericMapping
,
1632 PPRIVILEGE_SET PrivilegeSet
,
1633 LPDWORD PrivilegeSetLength
,
1634 LPDWORD GrantedAccess
,
1635 LPBOOL AccessStatus
)
1639 *AccessStatus
= TRUE
;
1641 return !*AccessStatus
;
1649 SetKernelObjectSecurity(HANDLE Handle
,
1650 SECURITY_INFORMATION SecurityInformation
,
1651 PSECURITY_DESCRIPTOR SecurityDescriptor
)
1655 Status
= NtSetSecurityObject(Handle
,
1656 SecurityInformation
,
1657 SecurityDescriptor
);
1658 if (!NT_SUCCESS(Status
))
1660 SetLastError(RtlNtStatusToDosError(Status
));
1672 AddAuditAccessAce(PACL pAcl
,
1673 DWORD dwAceRevision
,
1681 Status
= RtlAddAuditAccessAce(pAcl
,
1687 if (!NT_SUCCESS(Status
))
1689 SetLastError(RtlNtStatusToDosError(Status
));
1700 AddAuditAccessAceEx(PACL pAcl
,
1701 DWORD dwAceRevision
,
1710 Status
= RtlAddAuditAccessAceEx(pAcl
,
1717 if (!NT_SUCCESS(Status
))
1719 SetLastError(RtlNtStatusToDosError(Status
));
1726 /******************************************************************************
1727 * LookupAccountNameA [ADVAPI32.@]
1733 LookupAccountNameA(LPCSTR SystemName
,
1737 LPSTR ReferencedDomainName
,
1738 LPDWORD hReferencedDomainNameLength
,
1739 PSID_NAME_USE SidNameUse
)
1742 UNICODE_STRING lpSystemW
;
1743 UNICODE_STRING lpAccountW
;
1744 LPWSTR lpReferencedDomainNameW
= NULL
;
1746 RtlCreateUnicodeStringFromAsciiz(&lpSystemW
, SystemName
);
1747 RtlCreateUnicodeStringFromAsciiz(&lpAccountW
, AccountName
);
1749 if (ReferencedDomainName
)
1750 lpReferencedDomainNameW
= HeapAlloc(GetProcessHeap(),
1752 *hReferencedDomainNameLength
* sizeof(WCHAR
));
1754 ret
= LookupAccountNameW(lpSystemW
.Buffer
,
1758 lpReferencedDomainNameW
,
1759 hReferencedDomainNameLength
,
1762 if (ret
&& lpReferencedDomainNameW
)
1764 WideCharToMultiByte(CP_ACP
,
1766 lpReferencedDomainNameW
,
1767 *hReferencedDomainNameLength
+ 1,
1768 ReferencedDomainName
,
1769 *hReferencedDomainNameLength
+ 1,
1774 RtlFreeUnicodeString(&lpSystemW
);
1775 RtlFreeUnicodeString(&lpAccountW
);
1776 HeapFree(GetProcessHeap(), 0, lpReferencedDomainNameW
);
1781 /**********************************************************************
1782 * PrivilegeCheck EXPORTED
1787 PrivilegeCheck(HANDLE ClientToken
,
1788 PPRIVILEGE_SET RequiredPrivileges
,
1794 Status
= NtPrivilegeCheck(ClientToken
,
1797 if (!NT_SUCCESS(Status
))
1799 SetLastError(RtlNtStatusToDosError(Status
));
1803 *pfResult
= (BOOL
)Result
;
1808 /******************************************************************************
1809 * GetSecurityInfoExW EXPORTED
1813 GetSecurityInfoExA(HANDLE hObject
,
1814 SE_OBJECT_TYPE ObjectType
,
1815 SECURITY_INFORMATION SecurityInfo
,
1818 PACTRL_ACCESSA
*ppAccessList
,
1819 PACTRL_AUDITA
*ppAuditList
,
1823 FIXME("%s() not implemented!\n", __FUNCTION__
);
1824 return ERROR_BAD_PROVIDER
;
1828 /******************************************************************************
1829 * GetSecurityInfoExW EXPORTED
1833 GetSecurityInfoExW(HANDLE hObject
,
1834 SE_OBJECT_TYPE ObjectType
,
1835 SECURITY_INFORMATION SecurityInfo
,
1838 PACTRL_ACCESSW
*ppAccessList
,
1839 PACTRL_AUDITW
*ppAuditList
,
1843 FIXME("%s() not implemented!\n", __FUNCTION__
);
1844 return ERROR_BAD_PROVIDER
;
1847 /******************************************************************************
1848 * BuildExplicitAccessWithNameA [ADVAPI32.@]
1851 BuildExplicitAccessWithNameA(PEXPLICIT_ACCESSA pExplicitAccess
,
1853 DWORD AccessPermissions
,
1854 ACCESS_MODE AccessMode
,
1857 pExplicitAccess
->grfAccessPermissions
= AccessPermissions
;
1858 pExplicitAccess
->grfAccessMode
= AccessMode
;
1859 pExplicitAccess
->grfInheritance
= Inheritance
;
1861 pExplicitAccess
->Trustee
.pMultipleTrustee
= NULL
;
1862 pExplicitAccess
->Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1863 pExplicitAccess
->Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
1864 pExplicitAccess
->Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1865 pExplicitAccess
->Trustee
.ptstrName
= pTrusteeName
;
1869 /******************************************************************************
1870 * BuildExplicitAccessWithNameW [ADVAPI32.@]
1873 BuildExplicitAccessWithNameW(PEXPLICIT_ACCESSW pExplicitAccess
,
1874 LPWSTR pTrusteeName
,
1875 DWORD AccessPermissions
,
1876 ACCESS_MODE AccessMode
,
1879 pExplicitAccess
->grfAccessPermissions
= AccessPermissions
;
1880 pExplicitAccess
->grfAccessMode
= AccessMode
;
1881 pExplicitAccess
->grfInheritance
= Inheritance
;
1883 pExplicitAccess
->Trustee
.pMultipleTrustee
= NULL
;
1884 pExplicitAccess
->Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1885 pExplicitAccess
->Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
1886 pExplicitAccess
->Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1887 pExplicitAccess
->Trustee
.ptstrName
= pTrusteeName
;
1890 /******************************************************************************
1891 * BuildTrusteeWithObjectsAndNameA [ADVAPI32.@]
1893 VOID WINAPI
BuildTrusteeWithObjectsAndNameA( PTRUSTEEA pTrustee
, POBJECTS_AND_NAME_A pObjName
,
1894 SE_OBJECT_TYPE ObjectType
, LPSTR ObjectTypeName
,
1895 LPSTR InheritedObjectTypeName
, LPSTR Name
)
1897 DWORD ObjectsPresent
= 0;
1899 TRACE("%p %p 0x%08x %p %p %s\n", pTrustee
, pObjName
,
1900 ObjectType
, ObjectTypeName
, InheritedObjectTypeName
, debugstr_a(Name
));
1902 /* Fill the OBJECTS_AND_NAME structure */
1903 pObjName
->ObjectType
= ObjectType
;
1904 if (ObjectTypeName
!= NULL
)
1906 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1909 pObjName
->InheritedObjectTypeName
= InheritedObjectTypeName
;
1910 if (InheritedObjectTypeName
!= NULL
)
1912 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1915 pObjName
->ObjectsPresent
= ObjectsPresent
;
1916 pObjName
->ptstrName
= Name
;
1918 /* Fill the TRUSTEE structure */
1919 pTrustee
->pMultipleTrustee
= NULL
;
1920 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1921 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_NAME
;
1922 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1923 pTrustee
->ptstrName
= (LPSTR
)pObjName
;
1926 /******************************************************************************
1927 * BuildTrusteeWithObjectsAndNameW [ADVAPI32.@]
1929 VOID WINAPI
BuildTrusteeWithObjectsAndNameW( PTRUSTEEW pTrustee
, POBJECTS_AND_NAME_W pObjName
,
1930 SE_OBJECT_TYPE ObjectType
, LPWSTR ObjectTypeName
,
1931 LPWSTR InheritedObjectTypeName
, LPWSTR Name
)
1933 DWORD ObjectsPresent
= 0;
1935 TRACE("%p %p 0x%08x %p %p %s\n", pTrustee
, pObjName
,
1936 ObjectType
, ObjectTypeName
, InheritedObjectTypeName
, debugstr_w(Name
));
1938 /* Fill the OBJECTS_AND_NAME structure */
1939 pObjName
->ObjectType
= ObjectType
;
1940 if (ObjectTypeName
!= NULL
)
1942 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1945 pObjName
->InheritedObjectTypeName
= InheritedObjectTypeName
;
1946 if (InheritedObjectTypeName
!= NULL
)
1948 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1951 pObjName
->ObjectsPresent
= ObjectsPresent
;
1952 pObjName
->ptstrName
= Name
;
1954 /* Fill the TRUSTEE structure */
1955 pTrustee
->pMultipleTrustee
= NULL
;
1956 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
1957 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_NAME
;
1958 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
1959 pTrustee
->ptstrName
= (LPWSTR
)pObjName
;
1962 /******************************************************************************
1963 * BuildTrusteeWithObjectsAndSidA [ADVAPI32.@]
1966 BuildTrusteeWithObjectsAndSidA(PTRUSTEEA pTrustee
,
1967 POBJECTS_AND_SID pObjSid
,
1969 GUID
*pInheritedObjectGuid
,
1972 DWORD ObjectsPresent
= 0;
1974 TRACE("%p %p %p %p %p\n", pTrustee
, pObjSid
, pObjectGuid
, pInheritedObjectGuid
, pSid
);
1976 /* Fill the OBJECTS_AND_SID structure */
1977 if (pObjectGuid
!= NULL
)
1979 pObjSid
->ObjectTypeGuid
= *pObjectGuid
;
1980 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
1984 ZeroMemory(&pObjSid
->ObjectTypeGuid
,
1988 if (pInheritedObjectGuid
!= NULL
)
1990 pObjSid
->InheritedObjectTypeGuid
= *pInheritedObjectGuid
;
1991 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
1995 ZeroMemory(&pObjSid
->InheritedObjectTypeGuid
,
1999 pObjSid
->ObjectsPresent
= ObjectsPresent
;
2000 pObjSid
->pSid
= pSid
;
2002 /* Fill the TRUSTEE structure */
2003 pTrustee
->pMultipleTrustee
= NULL
;
2004 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2005 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_SID
;
2006 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2007 pTrustee
->ptstrName
= (LPSTR
) pObjSid
;
2011 /******************************************************************************
2012 * BuildTrusteeWithObjectsAndSidW [ADVAPI32.@]
2015 BuildTrusteeWithObjectsAndSidW(PTRUSTEEW pTrustee
,
2016 POBJECTS_AND_SID pObjSid
,
2018 GUID
*pInheritedObjectGuid
,
2021 DWORD ObjectsPresent
= 0;
2023 TRACE("%p %p %p %p %p\n", pTrustee
, pObjSid
, pObjectGuid
, pInheritedObjectGuid
, pSid
);
2025 /* Fill the OBJECTS_AND_SID structure */
2026 if (pObjectGuid
!= NULL
)
2028 pObjSid
->ObjectTypeGuid
= *pObjectGuid
;
2029 ObjectsPresent
|= ACE_OBJECT_TYPE_PRESENT
;
2033 ZeroMemory(&pObjSid
->ObjectTypeGuid
,
2037 if (pInheritedObjectGuid
!= NULL
)
2039 pObjSid
->InheritedObjectTypeGuid
= *pInheritedObjectGuid
;
2040 ObjectsPresent
|= ACE_INHERITED_OBJECT_TYPE_PRESENT
;
2044 ZeroMemory(&pObjSid
->InheritedObjectTypeGuid
,
2048 pObjSid
->ObjectsPresent
= ObjectsPresent
;
2049 pObjSid
->pSid
= pSid
;
2051 /* Fill the TRUSTEE structure */
2052 pTrustee
->pMultipleTrustee
= NULL
;
2053 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2054 pTrustee
->TrusteeForm
= TRUSTEE_IS_OBJECTS_AND_SID
;
2055 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2056 pTrustee
->ptstrName
= (LPWSTR
) pObjSid
;
2059 /******************************************************************************
2060 * BuildTrusteeWithSidA [ADVAPI32.@]
2063 BuildTrusteeWithSidA(PTRUSTEE_A pTrustee
,
2066 TRACE("%p %p\n", pTrustee
, pSid
);
2068 pTrustee
->pMultipleTrustee
= NULL
;
2069 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2070 pTrustee
->TrusteeForm
= TRUSTEE_IS_SID
;
2071 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2072 pTrustee
->ptstrName
= (LPSTR
) pSid
;
2076 /******************************************************************************
2077 * BuildTrusteeWithSidW [ADVAPI32.@]
2080 BuildTrusteeWithSidW(PTRUSTEE_W pTrustee
,
2083 TRACE("%p %p\n", pTrustee
, pSid
);
2085 pTrustee
->pMultipleTrustee
= NULL
;
2086 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2087 pTrustee
->TrusteeForm
= TRUSTEE_IS_SID
;
2088 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2089 pTrustee
->ptstrName
= (LPWSTR
) pSid
;
2092 /******************************************************************************
2093 * BuildTrusteeWithNameA [ADVAPI32.@]
2096 BuildTrusteeWithNameA(PTRUSTEE_A pTrustee
,
2099 TRACE("%p %s\n", pTrustee
, name
);
2101 pTrustee
->pMultipleTrustee
= NULL
;
2102 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2103 pTrustee
->TrusteeForm
= TRUSTEE_IS_NAME
;
2104 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2105 pTrustee
->ptstrName
= name
;
2108 /******************************************************************************
2109 * BuildTrusteeWithNameW [ADVAPI32.@]
2112 BuildTrusteeWithNameW(PTRUSTEE_W pTrustee
,
2115 TRACE("%p %s\n", pTrustee
, name
);
2117 pTrustee
->pMultipleTrustee
= NULL
;
2118 pTrustee
->MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2119 pTrustee
->TrusteeForm
= TRUSTEE_IS_NAME
;
2120 pTrustee
->TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2121 pTrustee
->ptstrName
= name
;
2124 /******************************************************************************
2125 * GetTrusteeFormW [ADVAPI32.@]
2128 GetTrusteeFormA(PTRUSTEE_A pTrustee
)
2130 return pTrustee
->TrusteeForm
;
2134 /******************************************************************************
2135 * GetTrusteeFormW [ADVAPI32.@]
2138 GetTrusteeFormW(PTRUSTEE_W pTrustee
)
2140 return pTrustee
->TrusteeForm
;
2143 /******************************************************************************
2144 * GetTrusteeNameA [ADVAPI32.@]
2147 GetTrusteeNameA(PTRUSTEE_A pTrustee
)
2149 return pTrustee
->ptstrName
;
2153 /******************************************************************************
2154 * GetTrusteeNameW [ADVAPI32.@]
2157 GetTrusteeNameW(PTRUSTEE_W pTrustee
)
2159 return pTrustee
->ptstrName
;
2162 /******************************************************************************
2163 * GetTrusteeTypeA [ADVAPI32.@]
2166 GetTrusteeTypeA(PTRUSTEE_A pTrustee
)
2168 return pTrustee
->TrusteeType
;
2171 /******************************************************************************
2172 * GetTrusteeTypeW [ADVAPI32.@]
2175 GetTrusteeTypeW(PTRUSTEE_W pTrustee
)
2177 return pTrustee
->TrusteeType
;
2185 SetAclInformation(PACL pAcl
,
2186 LPVOID pAclInformation
,
2187 DWORD nAclInformationLength
,
2188 ACL_INFORMATION_CLASS dwAclInformationClass
)
2192 Status
= RtlSetInformationAcl(pAcl
,
2194 nAclInformationLength
,
2195 dwAclInformationClass
);
2196 if (!NT_SUCCESS(Status
))
2198 SetLastError(RtlNtStatusToDosError(Status
));
2205 /**********************************************************************
2206 * SetNamedSecurityInfoA EXPORTED
2212 SetNamedSecurityInfoA(LPSTR pObjectName
,
2213 SE_OBJECT_TYPE ObjectType
,
2214 SECURITY_INFORMATION SecurityInfo
,
2220 UNICODE_STRING ObjectName
;
2223 if (!RtlCreateUnicodeStringFromAsciiz(&ObjectName
, pObjectName
))
2225 return ERROR_NOT_ENOUGH_MEMORY
;
2228 Ret
= SetNamedSecurityInfoW(ObjectName
.Buffer
,
2236 RtlFreeUnicodeString(&ObjectName
);
2246 AreAllAccessesGranted(DWORD GrantedAccess
,
2247 DWORD DesiredAccess
)
2249 return (BOOL
)RtlAreAllAccessesGranted(GrantedAccess
,
2258 AreAnyAccessesGranted(DWORD GrantedAccess
,
2259 DWORD DesiredAccess
)
2261 return (BOOL
)RtlAreAnyAccessesGranted(GrantedAccess
,
2265 /******************************************************************************
2266 * ParseAclStringFlags
2268 static DWORD
ParseAclStringFlags(LPCWSTR
* StringAcl
)
2271 LPCWSTR szAcl
= *StringAcl
;
2273 while (*szAcl
!= '(')
2277 flags
|= SE_DACL_PROTECTED
;
2279 else if (*szAcl
== 'A')
2283 flags
|= SE_DACL_AUTO_INHERIT_REQ
;
2284 else if (*szAcl
== 'I')
2285 flags
|= SE_DACL_AUTO_INHERITED
;
2294 /******************************************************************************
2295 * ParseAceStringType
2297 static const ACEFLAG AceType
[] =
2299 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
2300 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
2301 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
2302 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
2304 { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
2305 { SDDL_OBJECT_ACCESS_DENIED, ACCESS_DENIED_OBJECT_ACE_TYPE },
2306 { SDDL_OBJECT_ALARM, SYSTEM_ALARM_OBJECT_ACE_TYPE },
2307 { SDDL_OBJECT_AUDIT, SYSTEM_AUDIT_OBJECT_ACE_TYPE },
2312 static BYTE
ParseAceStringType(LPCWSTR
* StringAcl
)
2315 LPCWSTR szAcl
= *StringAcl
;
2316 const ACEFLAG
*lpaf
= AceType
;
2318 while (lpaf
->wstr
&&
2319 (len
= strlenW(lpaf
->wstr
)) &&
2320 strncmpW(lpaf
->wstr
, szAcl
, len
))
2331 /******************************************************************************
2332 * ParseAceStringFlags
2334 static const ACEFLAG AceFlags
[] =
2336 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
2337 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
2338 { SDDL_INHERITED
, INHERITED_ACE
},
2339 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
2340 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
2341 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
2342 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
2346 static BYTE
ParseAceStringFlags(LPCWSTR
* StringAcl
)
2350 LPCWSTR szAcl
= *StringAcl
;
2352 while (*szAcl
!= ';')
2354 const ACEFLAG
*lpaf
= AceFlags
;
2356 while (lpaf
->wstr
&&
2357 (len
= strlenW(lpaf
->wstr
)) &&
2358 strncmpW(lpaf
->wstr
, szAcl
, len
))
2364 flags
|= lpaf
->value
;
2373 /******************************************************************************
2374 * ParseAceStringRights
2376 static DWORD
ParseAceStringRights(LPCWSTR
* StringAcl
)
2380 LPCWSTR szAcl
= *StringAcl
;
2382 if ((*szAcl
== '0') && (*(szAcl
+ 1) == 'x'))
2386 while (*p
&& *p
!= ';')
2389 if (p
- szAcl
<= 10 /* 8 hex digits + "0x" */ )
2391 rights
= strtoulW(szAcl
, NULL
, 16);
2395 WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl
, p
- szAcl
));
2399 while (*szAcl
!= ';')
2401 const ACEFLAG
*lpaf
= AceRights
;
2403 while (lpaf
->wstr
&&
2404 (len
= strlenW(lpaf
->wstr
)) &&
2405 strncmpW(lpaf
->wstr
, szAcl
, len
))
2413 rights
|= lpaf
->value
;
2423 /******************************************************************************
2424 * ParseStringAclToAcl
2426 * dacl_flags(string_ace1)(string_ace2)... (string_acen)
2429 ParseStringAclToAcl(LPCWSTR StringAcl
,
2436 DWORD length
= sizeof(ACL
);
2439 PACCESS_ALLOWED_ACE pAce
= NULL
; /* pointer to current ACE */
2441 TRACE("%s\n", debugstr_w(StringAcl
));
2446 if (pAcl
) /* pAce is only useful if we're setting values */
2447 pAce
= (PACCESS_ALLOWED_ACE
) (pAcl
+ 1);
2449 /* Parse ACL flags */
2450 *lpdwFlags
= ParseAclStringFlags(&StringAcl
);
2453 while (*StringAcl
== '(')
2457 /* Parse ACE type */
2458 val
= ParseAceStringType(&StringAcl
);
2460 pAce
->Header
.AceType
= (BYTE
) val
;
2461 if (*StringAcl
!= ';')
2465 /* Parse ACE flags */
2466 val
= ParseAceStringFlags(&StringAcl
);
2468 pAce
->Header
.AceFlags
= (BYTE
) val
;
2469 if (*StringAcl
!= ';')
2473 /* Parse ACE rights */
2474 val
= ParseAceStringRights(&StringAcl
);
2477 if (*StringAcl
!= ';')
2481 /* Parse ACE object guid */
2482 if (*StringAcl
!= ';')
2484 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
2489 /* Parse ACE inherit object guid */
2490 if (*StringAcl
!= ';')
2492 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
2497 /* Parse ACE account sid */
2498 if (ParseStringSidToSid(StringAcl
, pAce
? &pAce
->SidStart
: NULL
, &sidlen
))
2500 while (*StringAcl
&& *StringAcl
!= ')')
2504 if (*StringAcl
!= ')')
2508 acesize
= sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
) + sidlen
;
2512 pAce
->Header
.AceSize
= acesize
;
2513 pAce
= (PACCESS_ALLOWED_ACE
)((LPBYTE
)pAce
+ acesize
);
2520 if (length
> 0xffff)
2522 ERR("ACL too large\n");
2528 pAcl
->AclRevision
= ACL_REVISION
;
2530 pAcl
->AclSize
= length
;
2531 pAcl
->AceCount
= acecount
++;
2537 SetLastError(ERROR_INVALID_ACL
);
2538 WARN("Invalid ACE string format\n");
2543 /******************************************************************************
2544 * ParseStringSecurityDescriptorToSecurityDescriptor
2547 ParseStringSecurityDescriptorToSecurityDescriptor(LPCWSTR StringSecurityDescriptor
,
2548 SECURITY_DESCRIPTOR_RELATIVE
* SecurityDescriptor
,
2553 WCHAR tok
[MAX_PATH
];
2555 LPBYTE lpNext
= NULL
;
2558 *cBytes
= sizeof(SECURITY_DESCRIPTOR
);
2560 if (SecurityDescriptor
)
2561 lpNext
= (LPBYTE
)(SecurityDescriptor
+ 1);
2563 while (*StringSecurityDescriptor
)
2565 toktype
= *StringSecurityDescriptor
;
2567 /* Expect char identifier followed by ':' */
2568 StringSecurityDescriptor
++;
2569 if (*StringSecurityDescriptor
!= ':')
2571 SetLastError(ERROR_INVALID_PARAMETER
);
2574 StringSecurityDescriptor
++;
2577 lptoken
= StringSecurityDescriptor
;
2578 while (*lptoken
&& *lptoken
!= ':')
2584 len
= lptoken
- StringSecurityDescriptor
;
2585 memcpy( tok
, StringSecurityDescriptor
, len
* sizeof(WCHAR
) );
2594 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
2597 if (SecurityDescriptor
)
2599 SecurityDescriptor
->Owner
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2600 lpNext
+= bytes
; /* Advance to next token */
2612 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
2615 if (SecurityDescriptor
)
2617 SecurityDescriptor
->Group
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2618 lpNext
+= bytes
; /* Advance to next token */
2631 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
2634 if (SecurityDescriptor
)
2636 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
| flags
;
2637 SecurityDescriptor
->Dacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2638 lpNext
+= bytes
; /* Advance to next token */
2651 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
2654 if (SecurityDescriptor
)
2656 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
| flags
;
2657 SecurityDescriptor
->Sacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
2658 lpNext
+= bytes
; /* Advance to next token */
2667 FIXME("Unknown token\n");
2668 SetLastError(ERROR_INVALID_PARAMETER
);
2672 StringSecurityDescriptor
= lptoken
;
2681 /* Winehq cvs 20050916 */
2682 /******************************************************************************
2683 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
2688 ConvertStringSecurityDescriptorToSecurityDescriptorA(LPCSTR StringSecurityDescriptor
,
2689 DWORD StringSDRevision
,
2690 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
2691 PULONG SecurityDescriptorSize
)
2695 LPWSTR StringSecurityDescriptorW
;
2697 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
2698 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2700 if (StringSecurityDescriptorW
)
2702 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
2704 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
2705 StringSDRevision
, SecurityDescriptor
,
2706 SecurityDescriptorSize
);
2707 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
2713 /******************************************************************************
2714 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
2718 ConvertStringSecurityDescriptorToSecurityDescriptorW(LPCWSTR StringSecurityDescriptor
,
2719 DWORD StringSDRevision
,
2720 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
2721 PULONG SecurityDescriptorSize
)
2724 SECURITY_DESCRIPTOR
* psd
;
2727 TRACE("%s\n", debugstr_w(StringSecurityDescriptor
));
2729 if (GetVersion() & 0x80000000)
2731 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2734 else if (!StringSecurityDescriptor
|| !SecurityDescriptor
)
2736 SetLastError(ERROR_INVALID_PARAMETER
);
2739 else if (StringSDRevision
!= SID_REVISION
)
2741 SetLastError(ERROR_UNKNOWN_REVISION
);
2745 /* Compute security descriptor length */
2746 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
2750 psd
= *SecurityDescriptor
= LocalAlloc(GMEM_ZEROINIT
, cBytes
);
2751 if (!psd
) goto lend
;
2753 psd
->Revision
= SID_REVISION
;
2754 psd
->Control
|= SE_SELF_RELATIVE
;
2756 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
2757 (SECURITY_DESCRIPTOR_RELATIVE
*)psd
, &cBytes
))
2763 if (SecurityDescriptorSize
)
2764 *SecurityDescriptorSize
= cBytes
;
2769 TRACE(" ret=%d\n", bret
);
2773 static void DumpString(LPCWSTR string
, int cch
, WCHAR
**pwptr
, ULONG
*plen
)
2776 cch
= strlenW(string
);
2783 memcpy(*pwptr
, string
, sizeof(WCHAR
)*cch
);
2788 static BOOL
DumpSidNumeric(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
2791 WCHAR fmt
[] = { 'S','-','%','u','-','%','d',0 };
2792 WCHAR subauthfmt
[] = { '-','%','u',0 };
2796 if( !IsValidSid( psid
) || pisid
->Revision
!= SDDL_REVISION
)
2798 SetLastError(ERROR_INVALID_SID
);
2802 if (pisid
->IdentifierAuthority
.Value
[0] ||
2803 pisid
->IdentifierAuthority
.Value
[1])
2805 FIXME("not matching MS' bugs\n");
2806 SetLastError(ERROR_INVALID_SID
);
2810 sprintfW( buf
, fmt
, pisid
->Revision
,
2812 MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
2813 pisid
->IdentifierAuthority
.Value
[4] ),
2814 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
2815 pisid
->IdentifierAuthority
.Value
[2] )
2817 DumpString(buf
, -1, pwptr
, plen
);
2819 for( i
=0; i
<pisid
->SubAuthorityCount
; i
++ )
2821 sprintfW( buf
, subauthfmt
, pisid
->SubAuthority
[i
] );
2822 DumpString(buf
, -1, pwptr
, plen
);
2827 static BOOL
DumpSid(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
2830 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
2832 if (WellKnownSids
[i
].wstr
[0] && EqualSid(psid
, (PSID
)&(WellKnownSids
[i
].Sid
.Revision
)))
2834 DumpString(WellKnownSids
[i
].wstr
, 2, pwptr
, plen
);
2839 return DumpSidNumeric(psid
, pwptr
, plen
);
2842 static const LPCWSTR AceRightBitNames
[32] = {
2843 SDDL_CREATE_CHILD
, /* 0 */
2847 SDDL_READ_PROPERTY
, /* 4 */
2848 SDDL_WRITE_PROPERTY
,
2851 SDDL_CONTROL_ACCESS
, /* 8 */
2859 SDDL_STANDARD_DELETE
, /* 16 */
2871 SDDL_GENERIC_ALL
, /* 28 */
2872 SDDL_GENERIC_EXECUTE
,
2877 static void DumpRights(DWORD mask
, WCHAR
**pwptr
, ULONG
*plen
)
2879 static const WCHAR fmtW
[] = {'0','x','%','x',0};
2886 /* first check if the right have name */
2887 for (i
= 0; i
< sizeof(AceRights
)/sizeof(AceRights
[0]); i
++)
2889 if (AceRights
[i
].wstr
== NULL
)
2891 if (mask
== AceRights
[i
].value
)
2893 DumpString(AceRights
[i
].wstr
, -1, pwptr
, plen
);
2898 /* then check if it can be built from bit names */
2899 for (i
= 0; i
< 32; i
++)
2901 if ((mask
& (1 << i
)) && (AceRightBitNames
[i
] == NULL
))
2903 /* can't be built from bit names */
2904 sprintfW(buf
, fmtW
, mask
);
2905 DumpString(buf
, -1, pwptr
, plen
);
2910 /* build from bit names */
2911 for (i
= 0; i
< 32; i
++)
2912 if (mask
& (1 << i
))
2913 DumpString(AceRightBitNames
[i
], -1, pwptr
, plen
);
2916 static BOOL
DumpAce(LPVOID pace
, WCHAR
**pwptr
, ULONG
*plen
)
2918 ACCESS_ALLOWED_ACE
*piace
; /* all the supported ACEs have the same memory layout */
2919 static const WCHAR openbr
= '(';
2920 static const WCHAR closebr
= ')';
2921 static const WCHAR semicolon
= ';';
2923 if (((PACE_HEADER
)pace
)->AceType
> SYSTEM_ALARM_ACE_TYPE
|| ((PACE_HEADER
)pace
)->AceSize
< sizeof(ACCESS_ALLOWED_ACE
))
2925 SetLastError(ERROR_INVALID_ACL
);
2930 DumpString(&openbr
, 1, pwptr
, plen
);
2931 switch (piace
->Header
.AceType
)
2933 case ACCESS_ALLOWED_ACE_TYPE
:
2934 DumpString(SDDL_ACCESS_ALLOWED
, -1, pwptr
, plen
);
2936 case ACCESS_DENIED_ACE_TYPE
:
2937 DumpString(SDDL_ACCESS_DENIED
, -1, pwptr
, plen
);
2939 case SYSTEM_AUDIT_ACE_TYPE
:
2940 DumpString(SDDL_AUDIT
, -1, pwptr
, plen
);
2942 case SYSTEM_ALARM_ACE_TYPE
:
2943 DumpString(SDDL_ALARM
, -1, pwptr
, plen
);
2946 DumpString(&semicolon
, 1, pwptr
, plen
);
2948 if (piace
->Header
.AceFlags
& OBJECT_INHERIT_ACE
)
2949 DumpString(SDDL_OBJECT_INHERIT
, -1, pwptr
, plen
);
2950 if (piace
->Header
.AceFlags
& CONTAINER_INHERIT_ACE
)
2951 DumpString(SDDL_CONTAINER_INHERIT
, -1, pwptr
, plen
);
2952 if (piace
->Header
.AceFlags
& NO_PROPAGATE_INHERIT_ACE
)
2953 DumpString(SDDL_NO_PROPAGATE
, -1, pwptr
, plen
);
2954 if (piace
->Header
.AceFlags
& INHERIT_ONLY_ACE
)
2955 DumpString(SDDL_INHERIT_ONLY
, -1, pwptr
, plen
);
2956 if (piace
->Header
.AceFlags
& INHERITED_ACE
)
2957 DumpString(SDDL_INHERITED
, -1, pwptr
, plen
);
2958 if (piace
->Header
.AceFlags
& SUCCESSFUL_ACCESS_ACE_FLAG
)
2959 DumpString(SDDL_AUDIT_SUCCESS
, -1, pwptr
, plen
);
2960 if (piace
->Header
.AceFlags
& FAILED_ACCESS_ACE_FLAG
)
2961 DumpString(SDDL_AUDIT_FAILURE
, -1, pwptr
, plen
);
2962 DumpString(&semicolon
, 1, pwptr
, plen
);
2963 DumpRights(piace
->Mask
, pwptr
, plen
);
2964 DumpString(&semicolon
, 1, pwptr
, plen
);
2965 /* objects not supported */
2966 DumpString(&semicolon
, 1, pwptr
, plen
);
2967 /* objects not supported */
2968 DumpString(&semicolon
, 1, pwptr
, plen
);
2969 if (!DumpSid((PSID
)&piace
->SidStart
, pwptr
, plen
))
2971 DumpString(&closebr
, 1, pwptr
, plen
);
2975 static BOOL
DumpAcl(PACL pacl
, WCHAR
**pwptr
, ULONG
*plen
, BOOL
protected, BOOL autoInheritReq
, BOOL autoInherited
)
2981 DumpString(SDDL_PROTECTED
, -1, pwptr
, plen
);
2983 DumpString(SDDL_AUTO_INHERIT_REQ
, -1, pwptr
, plen
);
2985 DumpString(SDDL_AUTO_INHERITED
, -1, pwptr
, plen
);
2990 if (!IsValidAcl(pacl
))
2993 count
= pacl
->AceCount
;
2994 for (i
= 0; i
< count
; i
++)
2997 if (!GetAce(pacl
, i
, &ace
))
2999 if (!DumpAce(ace
, pwptr
, plen
))
3006 static BOOL
DumpOwner(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3008 static const WCHAR prefix
[] = {'O',':',0};
3012 if (!GetSecurityDescriptorOwner(SecurityDescriptor
, &psid
, &bDefaulted
))
3018 DumpString(prefix
, -1, pwptr
, plen
);
3019 if (!DumpSid(psid
, pwptr
, plen
))
3024 static BOOL
DumpGroup(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3026 static const WCHAR prefix
[] = {'G',':',0};
3030 if (!GetSecurityDescriptorGroup(SecurityDescriptor
, &psid
, &bDefaulted
))
3036 DumpString(prefix
, -1, pwptr
, plen
);
3037 if (!DumpSid(psid
, pwptr
, plen
))
3042 static BOOL
DumpDacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3044 static const WCHAR dacl
[] = {'D',':',0};
3045 SECURITY_DESCRIPTOR_CONTROL control
;
3046 BOOL present
, defaulted
;
3050 if (!GetSecurityDescriptorDacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
3053 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
3059 DumpString(dacl
, 2, pwptr
, plen
);
3060 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_DACL_PROTECTED
, control
& SE_DACL_AUTO_INHERIT_REQ
, control
& SE_DACL_AUTO_INHERITED
))
3065 static BOOL
DumpSacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
3067 static const WCHAR sacl
[] = {'S',':',0};
3068 SECURITY_DESCRIPTOR_CONTROL control
;
3069 BOOL present
, defaulted
;
3073 if (!GetSecurityDescriptorSacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
3076 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
3082 DumpString(sacl
, 2, pwptr
, plen
);
3083 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_SACL_PROTECTED
, control
& SE_SACL_AUTO_INHERIT_REQ
, control
& SE_SACL_AUTO_INHERITED
))
3088 /******************************************************************************
3089 * ConvertSecurityDescriptorToStringSecurityDescriptorW [ADVAPI32.@]
3093 ConvertSecurityDescriptorToStringSecurityDescriptorW(PSECURITY_DESCRIPTOR SecurityDescriptor
,
3095 SECURITY_INFORMATION SecurityInformation
,
3096 LPWSTR
*OutputString
,
3102 if (SDRevision
!= SDDL_REVISION_1
)
3104 ERR("Program requested unknown SDDL revision %d\n", SDRevision
);
3105 SetLastError(ERROR_UNKNOWN_REVISION
);
3110 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
3111 if (!DumpOwner(SecurityDescriptor
, NULL
, &len
))
3113 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
3114 if (!DumpGroup(SecurityDescriptor
, NULL
, &len
))
3116 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
3117 if (!DumpDacl(SecurityDescriptor
, NULL
, &len
))
3119 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
3120 if (!DumpSacl(SecurityDescriptor
, NULL
, &len
))
3123 wstr
= wptr
= LocalAlloc(0, (len
+ 1)*sizeof(WCHAR
));
3127 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
3128 if (!DumpOwner(SecurityDescriptor
, &wptr
, NULL
))
3130 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
3131 if (!DumpGroup(SecurityDescriptor
, &wptr
, NULL
))
3133 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
3134 if (!DumpDacl(SecurityDescriptor
, &wptr
, NULL
))
3136 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
3137 if (!DumpSacl(SecurityDescriptor
, &wptr
, NULL
))
3141 TRACE("ret: %s, %d\n", wine_dbgstr_w(wstr
), len
);
3142 *OutputString
= wstr
;
3144 *OutputLen
= strlenW(*OutputString
)+1;
3148 /******************************************************************************
3149 * ConvertSecurityDescriptorToStringSecurityDescriptorA [ADVAPI32.@]
3153 ConvertSecurityDescriptorToStringSecurityDescriptorA(PSECURITY_DESCRIPTOR SecurityDescriptor
,
3155 SECURITY_INFORMATION Information
,
3156 LPSTR
*OutputString
,
3162 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
3166 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
3167 *OutputString
= HeapAlloc(GetProcessHeap(), 0, lenA
);
3168 if (*OutputString
== NULL
)
3174 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
3177 if (OutputLen
!= NULL
)
3183 *OutputString
= NULL
;
3195 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
3199 DWORD i
, cBytes
, identAuth
, csubauth
;
3203 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
3207 SetLastError(ERROR_INVALID_SID
);
3211 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
3213 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
3215 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
3216 size
= SECURITY_MAX_SID_SIZE
;
3217 *sid
= LocalAlloc(0, size
);
3220 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3223 ret
= CreateWellKnownSid(knownSid
,
3229 SetLastError(ERROR_INVALID_SID
);
3236 /* That's probably a string S-R-I-S-S... */
3237 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
3239 SetLastError(ERROR_INVALID_SID
);
3243 cBytes
= ComputeStringSidSize(StringSid
);
3244 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
3247 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3252 csubauth
= ((cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
3254 StringSid
+= 2; /* Advance to Revision */
3255 pisid
->Revision
= atoiW(StringSid
);
3257 if (pisid
->Revision
!= SDDL_REVISION
)
3259 TRACE("Revision %d is unknown\n", pisid
->Revision
);
3260 goto lend
; /* ERROR_INVALID_SID */
3264 TRACE("SubAuthorityCount is 0\n");
3265 goto lend
; /* ERROR_INVALID_SID */
3268 pisid
->SubAuthorityCount
= csubauth
;
3270 /* Advance to identifier authority */
3271 while (*StringSid
&& *StringSid
!= '-')
3273 if (*StringSid
== '-')
3276 /* MS' implementation can't handle values greater than 2^32 - 1, so
3277 * we don't either; assume most significant bytes are always 0
3279 pisid
->IdentifierAuthority
.Value
[0] = 0;
3280 pisid
->IdentifierAuthority
.Value
[1] = 0;
3281 identAuth
= atoiW(StringSid
);
3282 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
3283 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
3284 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
3285 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
3287 /* Advance to first sub authority */
3288 while (*StringSid
&& *StringSid
!= '-')
3290 if (*StringSid
== '-')
3295 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
3297 while (*StringSid
&& *StringSid
!= '-')
3299 if (*StringSid
== '-')
3303 if (i
!= pisid
->SubAuthorityCount
)
3304 goto lend
; /* ERROR_INVALID_SID */
3313 SetLastError(ERROR_INVALID_SID
);
3316 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");
3325 ConvertStringSidToSidA(IN LPCSTR StringSid
,
3328 BOOL bRetVal
= FALSE
;
3330 TRACE("%s, %p\n", debugstr_a(StringSid
), sid
);
3331 if (GetVersion() & 0x80000000)
3332 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3333 else if (!StringSid
|| !sid
)
3334 SetLastError(ERROR_INVALID_PARAMETER
);
3337 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
3338 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3339 if (wStringSid
== NULL
)
3341 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
3342 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
3343 HeapFree(GetProcessHeap(), 0, wStringSid
);
3353 ConvertSidToStringSidW(PSID Sid
,
3357 UNICODE_STRING UnicodeString
;
3358 WCHAR FixedBuffer
[64];
3360 if (!RtlValidSid(Sid
))
3362 SetLastError(ERROR_INVALID_SID
);
3366 UnicodeString
.Length
= 0;
3367 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
3368 UnicodeString
.Buffer
= FixedBuffer
;
3369 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
3370 if (STATUS_BUFFER_TOO_SMALL
== Status
)
3372 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
3375 if (!NT_SUCCESS(Status
))
3377 SetLastError(RtlNtStatusToDosError(Status
));
3381 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
3382 if (NULL
== *StringSid
)
3384 if (UnicodeString
.Buffer
!= FixedBuffer
)
3386 RtlFreeUnicodeString(&UnicodeString
);
3388 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3392 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
3393 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
3394 if (UnicodeString
.Buffer
!= FixedBuffer
)
3396 RtlFreeUnicodeString(&UnicodeString
);
3407 ConvertSidToStringSidA(PSID Sid
,
3413 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
3418 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
3421 LocalFree(StringSidW
);
3422 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3426 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
3427 if (NULL
== *StringSid
)
3429 LocalFree(StringSidW
);
3430 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3434 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
3436 LocalFree(StringSid
);
3437 LocalFree(StringSidW
);
3441 LocalFree(StringSidW
);
3450 CreateProcessWithLogonW(LPCWSTR lpUsername
,
3454 LPCWSTR lpApplicationName
,
3455 LPWSTR lpCommandLine
,
3456 DWORD dwCreationFlags
,
3457 LPVOID lpEnvironment
,
3458 LPCWSTR lpCurrentDirectory
,
3459 LPSTARTUPINFOW lpStartupInfo
,
3460 LPPROCESS_INFORMATION lpProcessInformation
)
3462 FIXME("%s %s %s 0x%08x %s %s 0x%08x %p %s %p %p stub\n", debugstr_w(lpUsername
), debugstr_w(lpDomain
),
3463 debugstr_w(lpPassword
), dwLogonFlags
, debugstr_w(lpApplicationName
),
3464 debugstr_w(lpCommandLine
), dwCreationFlags
, lpEnvironment
, debugstr_w(lpCurrentDirectory
),
3465 lpStartupInfo
, lpProcessInformation
);
3472 CreateProcessWithTokenW(IN HANDLE hToken
,
3473 IN DWORD dwLogonFlags
,
3474 IN LPCWSTR lpApplicationName OPTIONAL
,
3475 IN OUT LPWSTR lpCommandLine OPTIONAL
,
3476 IN DWORD dwCreationFlags
,
3477 IN LPVOID lpEnvironment OPTIONAL
,
3478 IN LPCWSTR lpCurrentDirectory OPTIONAL
,
3479 IN LPSTARTUPINFOW lpStartupInfo
,
3480 OUT LPPROCESS_INFORMATION lpProcessInfo
)
3490 DuplicateTokenEx(IN HANDLE ExistingTokenHandle
,
3491 IN DWORD dwDesiredAccess
,
3492 IN LPSECURITY_ATTRIBUTES lpTokenAttributes OPTIONAL
,
3493 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
,
3494 IN TOKEN_TYPE TokenType
,
3495 OUT PHANDLE DuplicateTokenHandle
)
3497 OBJECT_ATTRIBUTES ObjectAttributes
;
3499 SECURITY_QUALITY_OF_SERVICE Sqos
;
3501 TRACE("%p 0x%08x 0x%08x 0x%08x %p\n", ExistingTokenHandle
, dwDesiredAccess
,
3502 ImpersonationLevel
, TokenType
, DuplicateTokenHandle
);
3504 Sqos
.Length
= sizeof(SECURITY_QUALITY_OF_SERVICE
);
3505 Sqos
.ImpersonationLevel
= ImpersonationLevel
;
3506 Sqos
.ContextTrackingMode
= 0;
3507 Sqos
.EffectiveOnly
= FALSE
;
3509 if (lpTokenAttributes
!= NULL
)
3511 InitializeObjectAttributes(&ObjectAttributes
,
3513 lpTokenAttributes
->bInheritHandle
? OBJ_INHERIT
: 0,
3515 lpTokenAttributes
->lpSecurityDescriptor
);
3519 InitializeObjectAttributes(&ObjectAttributes
,
3526 ObjectAttributes
.SecurityQualityOfService
= &Sqos
;
3528 Status
= NtDuplicateToken(ExistingTokenHandle
,
3533 DuplicateTokenHandle
);
3534 if (!NT_SUCCESS(Status
))
3536 ERR("NtDuplicateToken failed: Status %08x\n", Status
);
3537 SetLastError(RtlNtStatusToDosError(Status
));
3541 TRACE("Returning token %p.\n", *DuplicateTokenHandle
);
3550 DuplicateToken(IN HANDLE ExistingTokenHandle
,
3551 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
,
3552 OUT PHANDLE DuplicateTokenHandle
)
3554 return DuplicateTokenEx(ExistingTokenHandle
,
3555 TOKEN_IMPERSONATE
| TOKEN_QUERY
,
3559 DuplicateTokenHandle
);
3562 /******************************************************************************
3563 * ComputeStringSidSize
3565 static DWORD
ComputeStringSidSize(LPCWSTR StringSid
)
3567 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I(-S)+ */
3572 if (*StringSid
== '-')
3578 return GetSidLengthRequired(ctok
- 2);
3580 else /* String constant format - Only available in winxp and above */
3584 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
3585 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
3586 return GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
3589 return GetSidLengthRequired(0);
3592 /******************************************************************************
3593 * ParseStringSidToSid
3595 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
)
3600 TRACE("%s, %p, %p\n", debugstr_w(StringSid
), pSid
, cBytes
);
3603 SetLastError(ERROR_INVALID_PARAMETER
);
3604 TRACE("StringSid is NULL, returning FALSE\n");
3608 while (*StringSid
== ' ')
3611 *cBytes
= ComputeStringSidSize(StringSid
);
3612 if (!pisid
) /* Simply compute the size */
3614 TRACE("only size requested, returning TRUE\n");
3618 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
3620 DWORD i
= 0, identAuth
;
3621 DWORD csubauth
= ((*cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
3623 StringSid
+= 2; /* Advance to Revision */
3624 pisid
->Revision
= atoiW(StringSid
);
3626 if (pisid
->Revision
!= SDDL_REVISION
)
3628 TRACE("Revision %d is unknown\n", pisid
->Revision
);
3629 goto lend
; /* ERROR_INVALID_SID */
3633 TRACE("SubAuthorityCount is 0\n");
3634 goto lend
; /* ERROR_INVALID_SID */
3637 pisid
->SubAuthorityCount
= csubauth
;
3639 /* Advance to identifier authority */
3640 while (*StringSid
&& *StringSid
!= '-')
3642 if (*StringSid
== '-')
3645 /* MS' implementation can't handle values greater than 2^32 - 1, so
3646 * we don't either; assume most significant bytes are always 0
3648 pisid
->IdentifierAuthority
.Value
[0] = 0;
3649 pisid
->IdentifierAuthority
.Value
[1] = 0;
3650 identAuth
= atoiW(StringSid
);
3651 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
3652 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
3653 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
3654 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
3656 /* Advance to first sub authority */
3657 while (*StringSid
&& *StringSid
!= '-')
3659 if (*StringSid
== '-')
3664 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
3666 while (*StringSid
&& *StringSid
!= '-')
3668 if (*StringSid
== '-')
3672 if (i
!= pisid
->SubAuthorityCount
)
3673 goto lend
; /* ERROR_INVALID_SID */
3677 else /* String constant format - Only available in winxp and above */
3680 pisid
->Revision
= SDDL_REVISION
;
3682 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
3683 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
3686 pisid
->SubAuthorityCount
= WellKnownSids
[i
].Sid
.SubAuthorityCount
;
3687 pisid
->IdentifierAuthority
= WellKnownSids
[i
].Sid
.IdentifierAuthority
;
3688 for (j
= 0; j
< WellKnownSids
[i
].Sid
.SubAuthorityCount
; j
++)
3689 pisid
->SubAuthority
[j
] = WellKnownSids
[i
].Sid
.SubAuthority
[j
];
3694 FIXME("String constant not supported: %s\n", debugstr_wn(StringSid
, 2));
3699 SetLastError(ERROR_INVALID_SID
);
3701 TRACE("returning %s\n", bret
? "TRUE" : "FALSE");
3705 /**********************************************************************
3706 * GetNamedSecurityInfoA EXPORTED
3712 GetNamedSecurityInfoA(LPSTR pObjectName
,
3713 SE_OBJECT_TYPE ObjectType
,
3714 SECURITY_INFORMATION SecurityInfo
,
3719 PSECURITY_DESCRIPTOR
*ppSecurityDescriptor
)
3725 TRACE("%s %d %d %p %p %p %p %p\n", pObjectName
, ObjectType
, SecurityInfo
,
3726 ppsidOwner
, ppsidGroup
, ppDacl
, ppSacl
, ppSecurityDescriptor
);
3730 len
= MultiByteToWideChar( CP_ACP
, 0, pObjectName
, -1, NULL
, 0 );
3731 wstr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
));
3732 MultiByteToWideChar( CP_ACP
, 0, pObjectName
, -1, wstr
, len
);
3735 r
= GetNamedSecurityInfoW( wstr
, ObjectType
, SecurityInfo
, ppsidOwner
,
3736 ppsidGroup
, ppDacl
, ppSacl
, ppSecurityDescriptor
);
3738 HeapFree( GetProcessHeap(), 0, wstr
);
3748 GetWindowsAccountDomainSid(IN PSID pSid
,
3749 OUT PSID ppDomainSid
,
3750 IN OUT DWORD
* cbSid
)
3761 EqualDomainSid(IN PSID pSid1
,