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/sec/sid.c
11 * PURPOSE: Security ID functions
15 WINE_DEFAULT_DEBUG_CHANNEL(advapi
);
17 #define MAX_GUID_STRING_LEN 39
20 AddAuditAccessAceEx(PACL pAcl
,
35 typedef struct _MAX_SID
37 /* same fields as struct _SID */
39 BYTE SubAuthorityCount
;
40 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
41 DWORD SubAuthority
[SID_MAX_SUB_AUTHORITIES
];
44 typedef struct WELLKNOWNSID
47 WELL_KNOWN_SID_TYPE Type
;
51 typedef struct _ACEFLAG
55 } ACEFLAG
, *LPACEFLAG
;
57 static const WELLKNOWNSID WellKnownSids
[] =
59 { {0,0}, WinNullSid
, { SID_REVISION
, 1, { SECURITY_NULL_SID_AUTHORITY
}, { SECURITY_NULL_RID
} } },
60 { {'W','D'}, WinWorldSid
, { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} } },
61 { {0,0}, WinLocalSid
, { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} } },
62 { {'C','O'}, WinCreatorOwnerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_RID
} } },
63 { {'C','G'}, WinCreatorGroupSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_RID
} } },
64 { {0,0}, WinCreatorOwnerServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_SERVER_RID
} } },
65 { {0,0}, WinCreatorGroupServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_SERVER_RID
} } },
66 { {0,0}, WinNtAuthoritySid
, { SID_REVISION
, 0, { SECURITY_NT_AUTHORITY
}, { SECURITY_NULL_RID
} } },
67 { {0,0}, WinDialupSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_DIALUP_RID
} } },
68 { {'N','U'}, WinNetworkSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_RID
} } },
69 { {0,0}, WinBatchSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BATCH_RID
} } },
70 { {'I','U'}, WinInteractiveSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} } },
71 { {'S','U'}, WinServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_SERVICE_RID
} } },
72 { {'A','N'}, WinAnonymousSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} } },
73 { {0,0}, WinProxySid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PROXY_RID
} } },
74 { {'E','D'}, WinEnterpriseControllersSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ENTERPRISE_CONTROLLERS_RID
} } },
75 { {'P','S'}, WinSelfSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PRINCIPAL_SELF_RID
} } },
76 { {'A','U'}, WinAuthenticatedUserSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} } },
77 { {'R','C'}, WinRestrictedCodeSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_RESTRICTED_CODE_RID
} } },
78 { {0,0}, WinTerminalServerSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_TERMINAL_SERVER_RID
} } },
79 { {0,0}, WinRemoteLogonIdSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_REMOTE_LOGON_RID
} } },
80 { {'S','Y'}, WinLocalSystemSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} } },
81 { {'L','S'}, WinLocalServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SERVICE_RID
} } },
82 { {'N','S'}, WinNetworkServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_SERVICE_RID
} } },
83 { {0,0}, WinBuiltinDomainSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
} } },
84 { {'B','A'}, WinBuiltinAdministratorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} } },
85 { {'B','U'}, WinBuiltinUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} } },
86 { {'B','G'}, WinBuiltinGuestsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_GUESTS
} } },
87 { {'P','U'}, WinBuiltinPowerUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_POWER_USERS
} } },
88 { {'A','O'}, WinBuiltinAccountOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ACCOUNT_OPS
} } },
89 { {'S','O'}, WinBuiltinSystemOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_SYSTEM_OPS
} } },
90 { {'P','O'}, WinBuiltinPrintOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PRINT_OPS
} } },
91 { {'B','O'}, WinBuiltinBackupOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_BACKUP_OPS
} } },
92 { {'R','E'}, WinBuiltinReplicatorSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REPLICATOR
} } },
93 { {'R','U'}, WinBuiltinPreWindows2000CompatibleAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PREW2KCOMPACCESS
} } },
94 { {'R','D'}, WinBuiltinRemoteDesktopUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS
} } },
95 { {'N','O'}, WinBuiltinNetworkConfigurationOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS
} } },
96 { {0,0}, WinNTLMAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_NTLM_RID
} } },
97 { {0,0}, WinDigestAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_DIGEST_RID
} } },
98 { {0,0}, WinSChannelAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_SCHANNEL_RID
} } },
99 { {0,0}, WinThisOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_THIS_ORGANIZATION_RID
} } },
100 { {0,0}, WinOtherOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_OTHER_ORGANIZATION_RID
} } },
101 { {0,0}, WinBuiltinIncomingForestTrustBuildersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS
} } },
102 { {0,0}, WinBuiltinPerfMonitoringUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_MONITORING_USERS
} } },
103 { {0,0}, WinBuiltinPerfLoggingUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_LOGGING_USERS
} } },
104 { {0,0}, WinBuiltinAuthorizationAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS
} } },
105 { {0,0}, WinBuiltinTerminalServerLicenseServersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS
} } },
106 { {0,0}, WinBuiltinDCOMUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_DCOM_USERS
} } },
107 { {'L','W'}, WinLowLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_LOW_RID
} } },
108 { {'M','E'}, WinMediumLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_MEDIUM_RID
} } },
109 { {'H','I'}, WinHighLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_HIGH_RID
} } },
110 { {'S','I'}, WinSystemLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_SYSTEM_RID
} } },
113 typedef struct WELLKNOWNRID
115 WELL_KNOWN_SID_TYPE Type
;
119 static const WELLKNOWNRID WellKnownRids
[] = {
120 { WinAccountAdministratorSid
, DOMAIN_USER_RID_ADMIN
},
121 { WinAccountGuestSid
, DOMAIN_USER_RID_GUEST
},
122 { WinAccountKrbtgtSid
, DOMAIN_USER_RID_KRBTGT
},
123 { WinAccountDomainAdminsSid
, DOMAIN_GROUP_RID_ADMINS
},
124 { WinAccountDomainUsersSid
, DOMAIN_GROUP_RID_USERS
},
125 { WinAccountDomainGuestsSid
, DOMAIN_GROUP_RID_GUESTS
},
126 { WinAccountComputersSid
, DOMAIN_GROUP_RID_COMPUTERS
},
127 { WinAccountControllersSid
, DOMAIN_GROUP_RID_CONTROLLERS
},
128 { WinAccountCertAdminsSid
, DOMAIN_GROUP_RID_CERT_ADMINS
},
129 { WinAccountSchemaAdminsSid
, DOMAIN_GROUP_RID_SCHEMA_ADMINS
},
130 { WinAccountEnterpriseAdminsSid
, DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
},
131 { WinAccountPolicyAdminsSid
, DOMAIN_GROUP_RID_POLICY_ADMINS
},
132 { WinAccountRasAndIasServersSid
, DOMAIN_ALIAS_RID_RAS_SERVERS
},
135 static const SID sidWorld
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
} , { SECURITY_WORLD_RID
} };
140 static const WCHAR SDDL_ACCESS_ALLOWED
[] = {'A',0};
141 static const WCHAR SDDL_ACCESS_DENIED
[] = {'D',0};
142 static const WCHAR SDDL_OBJECT_ACCESS_ALLOWED
[] = {'O','A',0};
143 static const WCHAR SDDL_OBJECT_ACCESS_DENIED
[] = {'O','D',0};
144 static const WCHAR SDDL_AUDIT
[] = {'A','U',0};
145 static const WCHAR SDDL_ALARM
[] = {'A','L',0};
146 static const WCHAR SDDL_OBJECT_AUDIT
[] = {'O','U',0};
147 static const WCHAR SDDL_OBJECT_ALARM
[] = {'O','L',0};
152 #define ADS_RIGHT_DS_CREATE_CHILD 0x0001
153 #define ADS_RIGHT_DS_DELETE_CHILD 0x0002
154 #define ADS_RIGHT_ACTRL_DS_LIST 0x0004
155 #define ADS_RIGHT_DS_SELF 0x0008
156 #define ADS_RIGHT_DS_READ_PROP 0x0010
157 #define ADS_RIGHT_DS_WRITE_PROP 0x0020
158 #define ADS_RIGHT_DS_DELETE_TREE 0x0040
159 #define ADS_RIGHT_DS_LIST_OBJECT 0x0080
160 #define ADS_RIGHT_DS_CONTROL_ACCESS 0x0100
165 static const WCHAR SDDL_CONTAINER_INHERIT
[] = {'C','I',0};
166 static const WCHAR SDDL_OBJECT_INHERIT
[] = {'O','I',0};
167 static const WCHAR SDDL_NO_PROPAGATE
[] = {'N','P',0};
168 static const WCHAR SDDL_INHERIT_ONLY
[] = {'I','O',0};
169 static const WCHAR SDDL_INHERITED
[] = {'I','D',0};
170 static const WCHAR SDDL_AUDIT_SUCCESS
[] = {'S','A',0};
171 static const WCHAR SDDL_AUDIT_FAILURE
[] = {'F','A',0};
173 static const char * debugstr_sid(PSID sid
)
176 SID
* psid
= (SID
*)sid
;
181 auth
= psid
->IdentifierAuthority
.Value
[5] +
182 (psid
->IdentifierAuthority
.Value
[4] << 8) +
183 (psid
->IdentifierAuthority
.Value
[3] << 16) +
184 (psid
->IdentifierAuthority
.Value
[2] << 24);
186 switch (psid
->SubAuthorityCount
) {
188 return wine_dbg_sprintf("S-%d-%d", psid
->Revision
, auth
);
190 return wine_dbg_sprintf("S-%d-%d-%lu", psid
->Revision
, auth
,
191 psid
->SubAuthority
[0]);
193 return wine_dbg_sprintf("S-%d-%d-%lu-%lu", psid
->Revision
, auth
,
194 psid
->SubAuthority
[0], psid
->SubAuthority
[1]);
196 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu", psid
->Revision
, auth
,
197 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2]);
199 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
200 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
201 psid
->SubAuthority
[3]);
203 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
204 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
205 psid
->SubAuthority
[3], psid
->SubAuthority
[4]);
207 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
208 psid
->SubAuthority
[3], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
209 psid
->SubAuthority
[0], psid
->SubAuthority
[4], psid
->SubAuthority
[5]);
211 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
212 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
213 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
214 psid
->SubAuthority
[6]);
216 return wine_dbg_sprintf("S-%d-%d-%lu-%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], psid
->SubAuthority
[7]);
224 static const ACEFLAG AceRights
[] =
226 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
227 { SDDL_GENERIC_READ
, GENERIC_READ
},
228 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
229 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
231 { SDDL_READ_CONTROL
, READ_CONTROL
},
232 { SDDL_STANDARD_DELETE
, DELETE
},
233 { SDDL_WRITE_DAC
, WRITE_DAC
},
234 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
236 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
237 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
238 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
239 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
240 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
241 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
242 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
243 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
244 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
246 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
247 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
248 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
249 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
251 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
252 { SDDL_KEY_READ
, KEY_READ
},
253 { SDDL_KEY_WRITE
, KEY_WRITE
},
254 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
258 static const LPCWSTR AceRightBitNames
[32] = {
259 SDDL_CREATE_CHILD
, /* 0 */
263 SDDL_READ_PROPERTY
, /* 4 */
267 SDDL_CONTROL_ACCESS
, /* 8 */
275 SDDL_STANDARD_DELETE
, /* 16 */
287 SDDL_GENERIC_ALL
, /* 28 */
288 SDDL_GENERIC_EXECUTE
,
294 /* set last error code from NT status and get the proper boolean return value */
295 /* used for functions that are a simple wrapper around the corresponding ntdll API */
296 static __inline BOOL
set_ntstatus( NTSTATUS status
)
298 if (status
) SetLastError( RtlNtStatusToDosError( status
));
302 /************************************************************
303 * ADVAPI_GetComputerSid
305 * Reads the computer SID from the registry.
307 BOOL
ADVAPI_GetComputerSid(PSID sid
)
312 static const WCHAR Account
[] = { 'S','E','C','U','R','I','T','Y','\\','S','A','M','\\','D','o','m','a','i','n','s','\\','A','c','c','o','u','n','t',0 };
313 static const WCHAR V
[] = { 'V',0 };
315 if ((ret
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, Account
, 0,
316 KEY_READ
, &key
)) == ERROR_SUCCESS
)
319 ret
= RegQueryValueExW(key
, V
, NULL
, NULL
, NULL
, &size
);
320 if (ret
== ERROR_MORE_DATA
|| ret
== ERROR_SUCCESS
)
322 BYTE
* data
= HeapAlloc(GetProcessHeap(), 0, size
);
325 if ((ret
= RegQueryValueExW(key
, V
, NULL
, NULL
,
326 data
, &size
)) == ERROR_SUCCESS
)
328 /* the SID is in the last 24 bytes of the binary data */
329 CopyMemory(sid
, &data
[size
-24], 24);
332 HeapFree(GetProcessHeap(), 0, data
);
338 if(retval
== TRUE
) return retval
;
340 /* create a new random SID */
341 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE
, Account
,
342 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &key
, NULL
) == ERROR_SUCCESS
)
345 SID_IDENTIFIER_AUTHORITY identifierAuthority
= {SECURITY_NT_AUTHORITY
};
348 if (RtlGenRandom(id
, sizeof(id
)))
350 if (AllocateAndInitializeSid(&identifierAuthority
, 4, SECURITY_NT_NON_UNIQUE
, id
[0], id
[1], id
[2], 0, 0, 0, 0, &new_sid
))
352 if (RegSetValueExW(key
, V
, 0, REG_BINARY
, new_sid
, GetLengthSid(new_sid
)) == ERROR_SUCCESS
)
353 retval
= CopySid(GetLengthSid(new_sid
), sid
, new_sid
);
364 /* Exported functions */
370 AllocateLocallyUniqueId(PLUID Luid
)
374 Status
= NtAllocateLocallyUniqueId (Luid
);
375 if (!NT_SUCCESS (Status
))
377 SetLastError(RtlNtStatusToDosError(Status
));
389 AllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
390 BYTE nSubAuthorityCount
,
391 DWORD dwSubAuthority0
,
392 DWORD dwSubAuthority1
,
393 DWORD dwSubAuthority2
,
394 DWORD dwSubAuthority3
,
395 DWORD dwSubAuthority4
,
396 DWORD dwSubAuthority5
,
397 DWORD dwSubAuthority6
,
398 DWORD dwSubAuthority7
,
403 Status
= RtlAllocateAndInitializeSid(pIdentifierAuthority
,
414 if (!NT_SUCCESS(Status
))
416 SetLastError(RtlNtStatusToDosError(Status
));
428 CopySid(DWORD nDestinationSidLength
,
429 PSID pDestinationSid
,
434 Status
= RtlCopySid(nDestinationSidLength
,
437 if (!NT_SUCCESS (Status
))
439 SetLastError(RtlNtStatusToDosError(Status
));
446 static void DumpString(LPCWSTR string
, int cch
, WCHAR
**pwptr
, ULONG
*plen
)
449 cch
= strlenW(string
);
456 memcpy(*pwptr
, string
, sizeof(WCHAR
)*cch
);
461 static BOOL
DumpSidNumeric(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
464 WCHAR fmt
[] = { 'S','-','%','u','-','%','d',0 };
465 WCHAR subauthfmt
[] = { '-','%','u',0 };
469 if( !IsValidSid( psid
) || pisid
->Revision
!= SDDL_REVISION
)
471 SetLastError(ERROR_INVALID_SID
);
475 if (pisid
->IdentifierAuthority
.Value
[0] ||
476 pisid
->IdentifierAuthority
.Value
[1])
478 FIXME("not matching MS' bugs\n");
479 SetLastError(ERROR_INVALID_SID
);
483 sprintfW( buf
, fmt
, pisid
->Revision
,
485 MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
486 pisid
->IdentifierAuthority
.Value
[4] ),
487 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
488 pisid
->IdentifierAuthority
.Value
[2] )
490 DumpString(buf
, -1, pwptr
, plen
);
492 for( i
=0; i
<pisid
->SubAuthorityCount
; i
++ )
494 sprintfW( buf
, subauthfmt
, pisid
->SubAuthority
[i
] );
495 DumpString(buf
, -1, pwptr
, plen
);
500 static BOOL
DumpSid(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
503 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
505 if (WellKnownSids
[i
].wstr
[0] && EqualSid(psid
, (PSID
)&(WellKnownSids
[i
].Sid
.Revision
)))
507 DumpString(WellKnownSids
[i
].wstr
, 2, pwptr
, plen
);
512 return DumpSidNumeric(psid
, pwptr
, plen
);
515 static void DumpRights(DWORD mask
, WCHAR
**pwptr
, ULONG
*plen
)
517 static const WCHAR fmtW
[] = {'0','x','%','x',0};
524 /* first check if the right have name */
525 for (i
= 0; i
< sizeof(AceRights
)/sizeof(AceRights
[0]); i
++)
527 if (AceRights
[i
].wstr
== NULL
)
529 if (mask
== AceRights
[i
].value
)
531 DumpString(AceRights
[i
].wstr
, -1, pwptr
, plen
);
536 /* then check if it can be built from bit names */
537 for (i
= 0; i
< 32; i
++)
539 if ((mask
& (1 << i
)) && (AceRightBitNames
[i
] == NULL
))
541 /* can't be built from bit names */
542 sprintfW(buf
, fmtW
, mask
);
543 DumpString(buf
, -1, pwptr
, plen
);
548 /* build from bit names */
549 for (i
= 0; i
< 32; i
++)
551 DumpString(AceRightBitNames
[i
], -1, pwptr
, plen
);
554 static BOOL
DumpAce(LPVOID pace
, WCHAR
**pwptr
, ULONG
*plen
)
556 ACCESS_ALLOWED_ACE
*piace
; /* all the supported ACEs have the same memory layout */
557 static const WCHAR openbr
= '(';
558 static const WCHAR closebr
= ')';
559 static const WCHAR semicolon
= ';';
561 if (((PACE_HEADER
)pace
)->AceType
> SYSTEM_ALARM_ACE_TYPE
|| ((PACE_HEADER
)pace
)->AceSize
< sizeof(ACCESS_ALLOWED_ACE
))
563 SetLastError(ERROR_INVALID_ACL
);
568 DumpString(&openbr
, 1, pwptr
, plen
);
569 switch (piace
->Header
.AceType
)
571 case ACCESS_ALLOWED_ACE_TYPE
:
572 DumpString(SDDL_ACCESS_ALLOWED
, -1, pwptr
, plen
);
574 case ACCESS_DENIED_ACE_TYPE
:
575 DumpString(SDDL_ACCESS_DENIED
, -1, pwptr
, plen
);
577 case SYSTEM_AUDIT_ACE_TYPE
:
578 DumpString(SDDL_AUDIT
, -1, pwptr
, plen
);
580 case SYSTEM_ALARM_ACE_TYPE
:
581 DumpString(SDDL_ALARM
, -1, pwptr
, plen
);
584 DumpString(&semicolon
, 1, pwptr
, plen
);
586 if (piace
->Header
.AceFlags
& OBJECT_INHERIT_ACE
)
587 DumpString(SDDL_OBJECT_INHERIT
, -1, pwptr
, plen
);
588 if (piace
->Header
.AceFlags
& CONTAINER_INHERIT_ACE
)
589 DumpString(SDDL_CONTAINER_INHERIT
, -1, pwptr
, plen
);
590 if (piace
->Header
.AceFlags
& NO_PROPAGATE_INHERIT_ACE
)
591 DumpString(SDDL_NO_PROPAGATE
, -1, pwptr
, plen
);
592 if (piace
->Header
.AceFlags
& INHERIT_ONLY_ACE
)
593 DumpString(SDDL_INHERIT_ONLY
, -1, pwptr
, plen
);
594 if (piace
->Header
.AceFlags
& INHERITED_ACE
)
595 DumpString(SDDL_INHERITED
, -1, pwptr
, plen
);
596 if (piace
->Header
.AceFlags
& SUCCESSFUL_ACCESS_ACE_FLAG
)
597 DumpString(SDDL_AUDIT_SUCCESS
, -1, pwptr
, plen
);
598 if (piace
->Header
.AceFlags
& FAILED_ACCESS_ACE_FLAG
)
599 DumpString(SDDL_AUDIT_FAILURE
, -1, pwptr
, plen
);
600 DumpString(&semicolon
, 1, pwptr
, plen
);
601 DumpRights(piace
->Mask
, pwptr
, plen
);
602 DumpString(&semicolon
, 1, pwptr
, plen
);
603 /* objects not supported */
604 DumpString(&semicolon
, 1, pwptr
, plen
);
605 /* objects not supported */
606 DumpString(&semicolon
, 1, pwptr
, plen
);
607 if (!DumpSid((PSID
)&piace
->SidStart
, pwptr
, plen
))
609 DumpString(&closebr
, 1, pwptr
, plen
);
613 static BOOL
DumpAcl(PACL pacl
, WCHAR
**pwptr
, ULONG
*plen
, BOOL
protected, BOOL autoInheritReq
, BOOL autoInherited
)
619 DumpString(SDDL_PROTECTED
, -1, pwptr
, plen
);
621 DumpString(SDDL_AUTO_INHERIT_REQ
, -1, pwptr
, plen
);
623 DumpString(SDDL_AUTO_INHERITED
, -1, pwptr
, plen
);
628 if (!IsValidAcl(pacl
))
631 count
= pacl
->AceCount
;
632 for (i
= 0; i
< count
; i
++)
635 if (!GetAce(pacl
, i
, &ace
))
637 if (!DumpAce(ace
, pwptr
, plen
))
644 static BOOL
DumpOwner(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
646 static const WCHAR prefix
[] = {'O',':',0};
650 if (!GetSecurityDescriptorOwner(SecurityDescriptor
, &psid
, &bDefaulted
))
656 DumpString(prefix
, -1, pwptr
, plen
);
657 if (!DumpSid(psid
, pwptr
, plen
))
662 static BOOL
DumpGroup(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
664 static const WCHAR prefix
[] = {'G',':',0};
668 if (!GetSecurityDescriptorGroup(SecurityDescriptor
, &psid
, &bDefaulted
))
674 DumpString(prefix
, -1, pwptr
, plen
);
675 if (!DumpSid(psid
, pwptr
, plen
))
680 static BOOL
DumpDacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
682 static const WCHAR dacl
[] = {'D',':',0};
683 SECURITY_DESCRIPTOR_CONTROL control
;
684 BOOL present
, defaulted
;
688 if (!GetSecurityDescriptorDacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
691 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
697 DumpString(dacl
, 2, pwptr
, plen
);
698 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_DACL_PROTECTED
, control
& SE_DACL_AUTO_INHERIT_REQ
, control
& SE_DACL_AUTO_INHERITED
))
703 static BOOL
DumpSacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
705 static const WCHAR sacl
[] = {'S',':',0};
706 SECURITY_DESCRIPTOR_CONTROL control
;
707 BOOL present
, defaulted
;
711 if (!GetSecurityDescriptorSacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
714 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
720 DumpString(sacl
, 2, pwptr
, plen
);
721 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_SACL_PROTECTED
, control
& SE_SACL_AUTO_INHERIT_REQ
, control
& SE_SACL_AUTO_INHERITED
))
726 /******************************************************************************
727 * ConvertSecurityDescriptorToStringSecurityDescriptorW [ADVAPI32.@]
731 ConvertSecurityDescriptorToStringSecurityDescriptorW(PSECURITY_DESCRIPTOR SecurityDescriptor
,
733 SECURITY_INFORMATION SecurityInformation
,
734 LPWSTR
*OutputString
,
740 if (SDRevision
!= SDDL_REVISION_1
)
742 ERR("Pogram requested unknown SDDL revision %d\n", SDRevision
);
743 SetLastError(ERROR_UNKNOWN_REVISION
);
748 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
749 if (!DumpOwner(SecurityDescriptor
, NULL
, &len
))
751 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
752 if (!DumpGroup(SecurityDescriptor
, NULL
, &len
))
754 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
755 if (!DumpDacl(SecurityDescriptor
, NULL
, &len
))
757 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
758 if (!DumpSacl(SecurityDescriptor
, NULL
, &len
))
761 wstr
= wptr
= LocalAlloc(0, (len
+ 1)*sizeof(WCHAR
));
762 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
763 if (!DumpOwner(SecurityDescriptor
, &wptr
, NULL
))
765 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
766 if (!DumpGroup(SecurityDescriptor
, &wptr
, NULL
))
768 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
769 if (!DumpDacl(SecurityDescriptor
, &wptr
, NULL
))
771 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
772 if (!DumpSacl(SecurityDescriptor
, &wptr
, NULL
))
776 TRACE("ret: %s, %d\n", wine_dbgstr_w(wstr
), len
);
777 *OutputString
= wstr
;
779 *OutputLen
= strlenW(*OutputString
)+1;
784 /******************************************************************************
785 * ConvertSecurityDescriptorToStringSecurityDescriptorA [ADVAPI32.@]
789 ConvertSecurityDescriptorToStringSecurityDescriptorA(PSECURITY_DESCRIPTOR SecurityDescriptor
,
791 SECURITY_INFORMATION Information
,
797 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
801 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
802 *OutputString
= HeapAlloc(GetProcessHeap(), 0, lenA
);
803 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
806 if (OutputLen
!= NULL
)
812 *OutputString
= NULL
;
820 /******************************************************************************
821 * ComputeStringSidSize
823 static DWORD
ComputeStringSidSize(LPCWSTR StringSid
)
825 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I(-S)+ */
830 if (*StringSid
== '-')
836 return GetSidLengthRequired(ctok
- 2);
838 else /* String constant format - Only available in winxp and above */
842 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
843 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
844 return GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
847 return GetSidLengthRequired(0);
850 /******************************************************************************
851 * ParseStringSidToSid
853 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
)
858 TRACE("%s, %p, %p\n", debugstr_w(StringSid
), pSid
, cBytes
);
861 SetLastError(ERROR_INVALID_PARAMETER
);
862 TRACE("StringSid is NULL, returning FALSE\n");
866 while (*StringSid
== ' ')
869 *cBytes
= ComputeStringSidSize(StringSid
);
870 if (!pisid
) /* Simply compute the size */
872 TRACE("only size requested, returning TRUE\n");
876 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
878 DWORD i
= 0, identAuth
;
879 DWORD csubauth
= ((*cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
881 StringSid
+= 2; /* Advance to Revision */
882 pisid
->Revision
= atoiW(StringSid
);
884 if (pisid
->Revision
!= SDDL_REVISION
)
886 TRACE("Revision %d is unknown\n", pisid
->Revision
);
887 goto lend
; /* ERROR_INVALID_SID */
891 TRACE("SubAuthorityCount is 0\n");
892 goto lend
; /* ERROR_INVALID_SID */
895 pisid
->SubAuthorityCount
= csubauth
;
897 /* Advance to identifier authority */
898 while (*StringSid
&& *StringSid
!= '-')
900 if (*StringSid
== '-')
903 /* MS' implementation can't handle values greater than 2^32 - 1, so
904 * we don't either; assume most significant bytes are always 0
906 pisid
->IdentifierAuthority
.Value
[0] = 0;
907 pisid
->IdentifierAuthority
.Value
[1] = 0;
908 identAuth
= atoiW(StringSid
);
909 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
910 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
911 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
912 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
914 /* Advance to first sub authority */
915 while (*StringSid
&& *StringSid
!= '-')
917 if (*StringSid
== '-')
922 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
924 while (*StringSid
&& *StringSid
!= '-')
926 if (*StringSid
== '-')
930 if (i
!= pisid
->SubAuthorityCount
)
931 goto lend
; /* ERROR_INVALID_SID */
935 else /* String constant format - Only available in winxp and above */
938 pisid
->Revision
= SDDL_REVISION
;
940 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
941 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
944 pisid
->SubAuthorityCount
= WellKnownSids
[i
].Sid
.SubAuthorityCount
;
945 pisid
->IdentifierAuthority
= WellKnownSids
[i
].Sid
.IdentifierAuthority
;
946 for (j
= 0; j
< WellKnownSids
[i
].Sid
.SubAuthorityCount
; j
++)
947 pisid
->SubAuthority
[j
] = WellKnownSids
[i
].Sid
.SubAuthority
[j
];
952 FIXME("String constant not supported: %s\n", debugstr_wn(StringSid
, 2));
957 SetLastError(ERROR_INVALID_SID
);
959 TRACE("returning %s\n", bret
? "TRUE" : "FALSE");
963 /******************************************************************************
964 * ParseAclStringFlags
966 static DWORD
ParseAclStringFlags(LPCWSTR
* StringAcl
)
969 LPCWSTR szAcl
= *StringAcl
;
971 while (*szAcl
!= '(')
975 flags
|= SE_DACL_PROTECTED
;
977 else if (*szAcl
== 'A')
981 flags
|= SE_DACL_AUTO_INHERIT_REQ
;
982 else if (*szAcl
== 'I')
983 flags
|= SE_DACL_AUTO_INHERITED
;
992 /******************************************************************************
995 static const ACEFLAG AceType
[] =
997 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
998 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
999 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
1000 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
1002 { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
1003 { SDDL_OBJECT_ACCESS_DENIED, ACCESS_DENIED_OBJECT_ACE_TYPE },
1004 { SDDL_OBJECT_ALARM, SYSTEM_ALARM_OBJECT_ACE_TYPE },
1005 { SDDL_OBJECT_AUDIT, SYSTEM_AUDIT_OBJECT_ACE_TYPE },
1010 static BYTE
ParseAceStringType(LPCWSTR
* StringAcl
)
1013 LPCWSTR szAcl
= *StringAcl
;
1014 const ACEFLAG
*lpaf
= AceType
;
1016 while (lpaf
->wstr
&&
1017 (len
= strlenW(lpaf
->wstr
)) &&
1018 strncmpW(lpaf
->wstr
, szAcl
, len
))
1029 /******************************************************************************
1030 * ParseAceStringFlags
1032 static const ACEFLAG AceFlags
[] =
1034 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
1035 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
1036 { SDDL_INHERITED
, INHERITED_ACE
},
1037 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
1038 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
1039 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
1040 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
1044 static BYTE
ParseAceStringFlags(LPCWSTR
* StringAcl
)
1048 LPCWSTR szAcl
= *StringAcl
;
1050 while (*szAcl
!= ';')
1052 const ACEFLAG
*lpaf
= AceFlags
;
1054 while (lpaf
->wstr
&&
1055 (len
= strlenW(lpaf
->wstr
)) &&
1056 strncmpW(lpaf
->wstr
, szAcl
, len
))
1062 flags
|= lpaf
->value
;
1071 /******************************************************************************
1072 * ParseAceStringRights
1074 static DWORD
ParseAceStringRights(LPCWSTR
* StringAcl
)
1078 LPCWSTR szAcl
= *StringAcl
;
1080 if ((*szAcl
== '0') && (*(szAcl
+ 1) == 'x'))
1084 while (*p
&& *p
!= ';')
1087 if (p
- szAcl
<= 10 /* 8 hex digits + "0x" */ )
1089 rights
= strtoulW(szAcl
, NULL
, 16);
1093 WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl
, p
- szAcl
));
1097 while (*szAcl
!= ';')
1099 const ACEFLAG
*lpaf
= AceRights
;
1101 while (lpaf
->wstr
&&
1102 (len
= strlenW(lpaf
->wstr
)) &&
1103 strncmpW(lpaf
->wstr
, szAcl
, len
))
1111 rights
|= lpaf
->value
;
1121 /******************************************************************************
1122 * ParseStringAclToAcl
1124 * dacl_flags(string_ace1)(string_ace2)... (string_acen)
1126 static BOOL
ParseStringAclToAcl(LPCWSTR StringAcl
, LPDWORD lpdwFlags
,
1127 PACL pAcl
, LPDWORD cBytes
)
1131 DWORD length
= sizeof(ACL
);
1134 PACCESS_ALLOWED_ACE pAce
= NULL
; /* pointer to current ACE */
1136 TRACE("%s\n", debugstr_w(StringAcl
));
1141 if (pAcl
) /* pAce is only useful if we're setting values */
1142 pAce
= (PACCESS_ALLOWED_ACE
) (pAcl
+ 1);
1144 /* Parse ACL flags */
1145 *lpdwFlags
= ParseAclStringFlags(&StringAcl
);
1148 while (*StringAcl
== '(')
1152 /* Parse ACE type */
1153 val
= ParseAceStringType(&StringAcl
);
1155 pAce
->Header
.AceType
= (BYTE
) val
;
1156 if (*StringAcl
!= ';')
1160 /* Parse ACE flags */
1161 val
= ParseAceStringFlags(&StringAcl
);
1163 pAce
->Header
.AceFlags
= (BYTE
) val
;
1164 if (*StringAcl
!= ';')
1168 /* Parse ACE rights */
1169 val
= ParseAceStringRights(&StringAcl
);
1172 if (*StringAcl
!= ';')
1176 /* Parse ACE object guid */
1177 if (*StringAcl
!= ';')
1179 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
1184 /* Parse ACE inherit object guid */
1185 if (*StringAcl
!= ';')
1187 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
1192 /* Parse ACE account sid */
1193 if (ParseStringSidToSid(StringAcl
, pAce
? &pAce
->SidStart
: NULL
, &sidlen
))
1195 while (*StringAcl
&& *StringAcl
!= ')')
1199 if (*StringAcl
!= ')')
1203 acesize
= sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
) + sidlen
;
1207 pAce
->Header
.AceSize
= acesize
;
1208 pAce
= (PACCESS_ALLOWED_ACE
)((LPBYTE
)pAce
+ acesize
);
1215 if (length
> 0xffff)
1217 ERR("ACL too large\n");
1223 pAcl
->AclRevision
= ACL_REVISION
;
1225 pAcl
->AclSize
= length
;
1226 pAcl
->AceCount
= acecount
++;
1232 SetLastError(ERROR_INVALID_ACL
);
1233 WARN("Invalid ACE string format\n");
1238 /******************************************************************************
1239 * ParseStringSecurityDescriptorToSecurityDescriptor
1241 static BOOL
ParseStringSecurityDescriptorToSecurityDescriptor(
1242 LPCWSTR StringSecurityDescriptor
,
1243 SECURITY_DESCRIPTOR_RELATIVE
* SecurityDescriptor
,
1248 WCHAR tok
[MAX_PATH
];
1250 LPBYTE lpNext
= NULL
;
1253 *cBytes
= sizeof(SECURITY_DESCRIPTOR
);
1255 if (SecurityDescriptor
)
1256 lpNext
= (LPBYTE
)(SecurityDescriptor
+ 1);
1258 while (*StringSecurityDescriptor
)
1260 toktype
= *StringSecurityDescriptor
;
1262 /* Expect char identifier followed by ':' */
1263 StringSecurityDescriptor
++;
1264 if (*StringSecurityDescriptor
!= ':')
1266 SetLastError(ERROR_INVALID_PARAMETER
);
1269 StringSecurityDescriptor
++;
1272 lptoken
= StringSecurityDescriptor
;
1273 while (*lptoken
&& *lptoken
!= ':')
1279 len
= lptoken
- StringSecurityDescriptor
;
1280 memcpy( tok
, StringSecurityDescriptor
, len
* sizeof(WCHAR
) );
1289 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
1292 if (SecurityDescriptor
)
1294 SecurityDescriptor
->Owner
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1295 lpNext
+= bytes
; /* Advance to next token */
1307 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
1310 if (SecurityDescriptor
)
1312 SecurityDescriptor
->Group
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1313 lpNext
+= bytes
; /* Advance to next token */
1326 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1329 if (SecurityDescriptor
)
1331 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
| flags
;
1332 SecurityDescriptor
->Dacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1333 lpNext
+= bytes
; /* Advance to next token */
1346 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1349 if (SecurityDescriptor
)
1351 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
| flags
;
1352 SecurityDescriptor
->Sacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1353 lpNext
+= bytes
; /* Advance to next token */
1362 FIXME("Unknown token\n");
1363 SetLastError(ERROR_INVALID_PARAMETER
);
1367 StringSecurityDescriptor
= lptoken
;
1377 /******************************************************************************
1378 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
1381 BOOL WINAPI
ConvertStringSecurityDescriptorToSecurityDescriptorW(
1382 LPCWSTR StringSecurityDescriptor
,
1383 DWORD StringSDRevision
,
1384 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1385 PULONG SecurityDescriptorSize
)
1388 SECURITY_DESCRIPTOR
* psd
;
1391 TRACE("%s\n", debugstr_w(StringSecurityDescriptor
));
1393 if (GetVersion() & 0x80000000)
1395 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1398 else if (!StringSecurityDescriptor
|| !SecurityDescriptor
)
1400 SetLastError(ERROR_INVALID_PARAMETER
);
1403 else if (StringSDRevision
!= SID_REVISION
)
1405 SetLastError(ERROR_UNKNOWN_REVISION
);
1409 /* Compute security descriptor length */
1410 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1414 psd
= *SecurityDescriptor
= LocalAlloc(GMEM_ZEROINIT
, cBytes
);
1415 if (!psd
) goto lend
;
1417 psd
->Revision
= SID_REVISION
;
1418 psd
->Control
|= SE_SELF_RELATIVE
;
1420 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1421 (SECURITY_DESCRIPTOR_RELATIVE
*)psd
, &cBytes
))
1427 if (SecurityDescriptorSize
)
1428 *SecurityDescriptorSize
= cBytes
;
1433 TRACE(" ret=%d\n", bret
);
1438 /* Winehq cvs 20050916 */
1439 /******************************************************************************
1440 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
1443 BOOL WINAPI
ConvertStringSecurityDescriptorToSecurityDescriptorA(
1444 LPCSTR StringSecurityDescriptor
,
1445 DWORD StringSDRevision
,
1446 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1447 PULONG SecurityDescriptorSize
)
1451 LPWSTR StringSecurityDescriptorW
;
1453 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
1454 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1456 if (StringSecurityDescriptorW
)
1458 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
1460 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
1461 StringSDRevision
, SecurityDescriptor
,
1462 SecurityDescriptorSize
);
1463 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
1473 EqualPrefixSid(PSID pSid1
,
1476 return RtlEqualPrefixSid (pSid1
, pSid2
);
1484 EqualSid(PSID pSid1
,
1487 return RtlEqualSid (pSid1
, pSid2
);
1495 * Docs says this function does NOT return a value
1496 * even thou it's defined to return a PVOID...
1501 return RtlFreeSid(pSid
);
1509 GetLengthSid(PSID pSid
)
1511 return (DWORD
)RtlLengthSid(pSid
);
1518 PSID_IDENTIFIER_AUTHORITY WINAPI
1519 GetSidIdentifierAuthority(PSID pSid
)
1521 return RtlIdentifierAuthoritySid(pSid
);
1529 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
1531 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
1539 GetSidSubAuthority(PSID pSid
,
1540 DWORD nSubAuthority
)
1542 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
1550 GetSidSubAuthorityCount(PSID pSid
)
1552 return RtlSubAuthorityCountSid(pSid
);
1560 InitializeSid(PSID Sid
,
1561 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
1562 BYTE nSubAuthorityCount
)
1566 Status
= RtlInitializeSid(Sid
,
1567 pIdentifierAuthority
,
1568 nSubAuthorityCount
);
1569 if (!NT_SUCCESS(Status
))
1571 SetLastError(RtlNtStatusToDosError(Status
));
1583 IsValidSid(PSID pSid
)
1585 return (BOOL
)RtlValidSid(pSid
);
1593 ConvertSidToStringSidW(PSID Sid
,
1597 UNICODE_STRING UnicodeString
;
1598 WCHAR FixedBuffer
[64];
1600 if (!RtlValidSid(Sid
))
1602 SetLastError(ERROR_INVALID_SID
);
1606 UnicodeString
.Length
= 0;
1607 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
1608 UnicodeString
.Buffer
= FixedBuffer
;
1609 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
1610 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1612 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
1615 if (!NT_SUCCESS(Status
))
1617 SetLastError(RtlNtStatusToDosError(Status
));
1621 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
1622 if (NULL
== *StringSid
)
1624 if (UnicodeString
.Buffer
!= FixedBuffer
)
1626 RtlFreeUnicodeString(&UnicodeString
);
1628 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1632 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
1633 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
1634 if (UnicodeString
.Buffer
!= FixedBuffer
)
1636 RtlFreeUnicodeString(&UnicodeString
);
1647 ConvertSidToStringSidA(PSID Sid
,
1653 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
1658 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
1661 LocalFree(StringSidW
);
1662 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1666 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
1667 if (NULL
== *StringSid
)
1669 LocalFree(StringSidW
);
1670 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1674 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
1676 LocalFree(StringSid
);
1677 LocalFree(StringSidW
);
1681 LocalFree(StringSidW
);
1691 EqualDomainSid(IN PSID pSid1
,
1704 GetWindowsAccountDomainSid(IN PSID pSid
,
1705 OUT PSID ppDomainSid
,
1706 IN OUT DWORD
* cbSid
)
1717 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
1718 IN PSID DomainSid OPTIONAL
,
1720 IN OUT DWORD
* cbSid
)
1723 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
1725 if (cbSid
== NULL
|| (DomainSid
&& !IsValidSid(DomainSid
)))
1727 SetLastError(ERROR_INVALID_PARAMETER
);
1731 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++) {
1732 if (WellKnownSids
[i
].Type
== WellKnownSidType
) {
1733 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
1735 if (*cbSid
< length
)
1738 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1743 SetLastError(ERROR_INVALID_PARAMETER
);
1746 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
1752 if (DomainSid
== NULL
|| *GetSidSubAuthorityCount(DomainSid
) == SID_MAX_SUB_AUTHORITIES
)
1754 SetLastError(ERROR_INVALID_PARAMETER
);
1758 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
1759 if (WellKnownRids
[i
].Type
== WellKnownSidType
) {
1760 UCHAR domain_subauth
= *GetSidSubAuthorityCount(DomainSid
);
1761 DWORD domain_sid_length
= GetSidLengthRequired(domain_subauth
);
1762 DWORD output_sid_length
= GetSidLengthRequired(domain_subauth
+ 1);
1764 if (*cbSid
< output_sid_length
)
1766 *cbSid
= output_sid_length
;
1767 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1772 SetLastError(ERROR_INVALID_PARAMETER
);
1775 CopyMemory(pSid
, DomainSid
, domain_sid_length
);
1776 (*GetSidSubAuthorityCount(pSid
))++;
1777 (*GetSidSubAuthority(pSid
, domain_subauth
)) = WellKnownRids
[i
].Rid
;
1778 *cbSid
= output_sid_length
;
1782 SetLastError(ERROR_INVALID_PARAMETER
);
1791 IsWellKnownSid(IN PSID pSid
,
1792 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
1795 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
1797 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
1799 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
1801 if (EqualSid(pSid
, (PSID
)(&WellKnownSids
[i
].Sid
.Revision
)))
1814 ConvertStringSidToSidA(IN LPCSTR StringSid
,
1817 BOOL bRetVal
= FALSE
;
1819 TRACE("%s, %p\n", debugstr_a(StringSid
), sid
);
1820 if (GetVersion() & 0x80000000)
1821 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1822 else if (!StringSid
|| !sid
)
1823 SetLastError(ERROR_INVALID_PARAMETER
);
1826 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
1827 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1828 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
1829 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
1830 HeapFree(GetProcessHeap(), 0, wStringSid
);
1836 static const RECORD SidTable
[] =
1838 { SDDL_ACCOUNT_OPERATORS
, WinBuiltinAccountOperatorsSid
},
1839 { SDDL_ALIAS_PREW2KCOMPACC
, WinBuiltinPreWindows2000CompatibleAccessSid
},
1840 { SDDL_ANONYMOUS
, WinAnonymousSid
},
1841 { SDDL_AUTHENTICATED_USERS
, WinAuthenticatedUserSid
},
1842 { SDDL_BUILTIN_ADMINISTRATORS
, WinBuiltinAdministratorsSid
},
1843 { SDDL_BUILTIN_GUESTS
, WinBuiltinGuestsSid
},
1844 { SDDL_BACKUP_OPERATORS
, WinBuiltinBackupOperatorsSid
},
1845 { SDDL_BUILTIN_USERS
, WinBuiltinUsersSid
},
1846 { SDDL_CERT_SERV_ADMINISTRATORS
, WinAccountCertAdminsSid
/* FIXME: DOMAIN_GROUP_RID_CERT_ADMINS */ },
1847 { SDDL_CREATOR_GROUP
, WinCreatorGroupSid
},
1848 { SDDL_CREATOR_OWNER
, WinCreatorOwnerSid
},
1849 { SDDL_DOMAIN_ADMINISTRATORS
, WinAccountDomainAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ADMINS */ },
1850 { SDDL_DOMAIN_COMPUTERS
, WinAccountComputersSid
/* FIXME: DOMAIN_GROUP_RID_COMPUTERS */ },
1851 { SDDL_DOMAIN_DOMAIN_CONTROLLERS
, WinAccountControllersSid
/* FIXME: DOMAIN_GROUP_RID_CONTROLLERS */ },
1852 { SDDL_DOMAIN_GUESTS
, WinAccountDomainGuestsSid
/* FIXME: DOMAIN_GROUP_RID_GUESTS */ },
1853 { SDDL_DOMAIN_USERS
, WinAccountDomainUsersSid
/* FIXME: DOMAIN_GROUP_RID_USERS */ },
1854 { SDDL_ENTERPRISE_ADMINS
, WinAccountEnterpriseAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ENTERPRISE_ADMINS */ },
1855 { SDDL_ENTERPRISE_DOMAIN_CONTROLLERS
, WinLogonIdsSid
/* FIXME: SECURITY_SERVER_LOGON_RID */ },
1856 { SDDL_EVERYONE
, WinWorldSid
},
1857 { SDDL_GROUP_POLICY_ADMINS
, WinAccountPolicyAdminsSid
/* FIXME: DOMAIN_GROUP_RID_POLICY_ADMINS */ },
1858 { SDDL_INTERACTIVE
, WinInteractiveSid
},
1859 { SDDL_LOCAL_ADMIN
, WinAccountAdministratorSid
/* FIXME: DOMAIN_USER_RID_ADMIN */ },
1860 { SDDL_LOCAL_GUEST
, WinAccountGuestSid
/* FIXME: DOMAIN_USER_RID_GUEST */ },
1861 { SDDL_LOCAL_SERVICE
, WinLocalServiceSid
},
1862 { SDDL_LOCAL_SYSTEM
, WinLocalSystemSid
},
1863 { SDDL_NETWORK
, WinNetworkSid
},
1864 { SDDL_NETWORK_CONFIGURATION_OPS
, WinBuiltinNetworkConfigurationOperatorsSid
},
1865 { SDDL_NETWORK_SERVICE
, WinNetworkServiceSid
},
1866 { SDDL_PRINTER_OPERATORS
, WinBuiltinPrintOperatorsSid
},
1867 { SDDL_PERSONAL_SELF
, WinSelfSid
},
1868 { SDDL_POWER_USERS
, WinBuiltinPowerUsersSid
},
1869 { SDDL_RAS_SERVERS
, WinAccountRasAndIasServersSid
/* FIXME: DOMAIN_ALIAS_RID_RAS_SERVERS */ },
1870 { SDDL_REMOTE_DESKTOP
, WinBuiltinRemoteDesktopUsersSid
},
1871 { SDDL_REPLICATOR
, WinBuiltinReplicatorSid
},
1872 { SDDL_RESTRICTED_CODE
, WinRestrictedCodeSid
},
1873 { SDDL_SCHEMA_ADMINISTRATORS
, WinAccountSchemaAdminsSid
/* FIXME: DOMAIN_GROUP_RID_SCHEMA_ADMINS */ },
1874 { SDDL_SERVER_OPERATORS
, WinBuiltinSystemOperatorsSid
},
1875 { SDDL_SERVICE
, WinServiceSid
},
1883 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
1887 DWORD i
, cBytes
, identAuth
, csubauth
;
1891 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
1895 SetLastError(ERROR_INVALID_SID
);
1898 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
1900 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
1902 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
1903 size
= SECURITY_MAX_SID_SIZE
;
1904 *sid
= LocalAlloc(0, size
);
1907 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1910 ret
= CreateWellKnownSid(
1917 SetLastError(ERROR_INVALID_SID
);
1924 /* That's probably a string S-R-I-S-S... */
1925 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
1927 SetLastError(ERROR_INVALID_SID
);
1931 cBytes
= ComputeStringSidSize(StringSid
);
1932 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
1935 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1940 csubauth
= ((cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
1942 StringSid
+= 2; /* Advance to Revision */
1943 pisid
->Revision
= atoiW(StringSid
);
1945 if (pisid
->Revision
!= SDDL_REVISION
)
1947 TRACE("Revision %d is unknown\n", pisid
->Revision
);
1948 goto lend
; /* ERROR_INVALID_SID */
1952 TRACE("SubAuthorityCount is 0\n");
1953 goto lend
; /* ERROR_INVALID_SID */
1956 pisid
->SubAuthorityCount
= csubauth
;
1958 /* Advance to identifier authority */
1959 while (*StringSid
&& *StringSid
!= '-')
1961 if (*StringSid
== '-')
1964 /* MS' implementation can't handle values greater than 2^32 - 1, so
1965 * we don't either; assume most significant bytes are always 0
1967 pisid
->IdentifierAuthority
.Value
[0] = 0;
1968 pisid
->IdentifierAuthority
.Value
[1] = 0;
1969 identAuth
= atoiW(StringSid
);
1970 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
1971 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
1972 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
1973 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
1975 /* Advance to first sub authority */
1976 while (*StringSid
&& *StringSid
!= '-')
1978 if (*StringSid
== '-')
1983 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
1985 while (*StringSid
&& *StringSid
!= '-')
1987 if (*StringSid
== '-')
1991 if (i
!= pisid
->SubAuthorityCount
)
1992 goto lend
; /* ERROR_INVALID_SID */
2001 SetLastError(ERROR_INVALID_SID
);
2004 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");