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
16 #include <wine/debug.h>
17 #include <wine/unicode.h>
19 WINE_DEFAULT_DEBUG_CHANNEL(advapi
);
21 #define MAX_GUID_STRING_LEN 39
24 AddAuditAccessAceEx(PACL pAcl
,
39 typedef struct _MAX_SID
41 /* same fields as struct _SID */
43 BYTE SubAuthorityCount
;
44 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
45 DWORD SubAuthority
[SID_MAX_SUB_AUTHORITIES
];
48 typedef struct WELLKNOWNSID
51 WELL_KNOWN_SID_TYPE Type
;
55 typedef struct _ACEFLAG
59 } ACEFLAG
, *LPACEFLAG
;
61 static const WELLKNOWNSID WellKnownSids
[] =
63 { {0,0}, WinNullSid
, { SID_REVISION
, 1, { SECURITY_NULL_SID_AUTHORITY
}, { SECURITY_NULL_RID
} } },
64 { {'W','D'}, WinWorldSid
, { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} } },
65 { {0,0}, WinLocalSid
, { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} } },
66 { {'C','O'}, WinCreatorOwnerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_RID
} } },
67 { {'C','G'}, WinCreatorGroupSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_RID
} } },
68 { {0,0}, WinCreatorOwnerServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_SERVER_RID
} } },
69 { {0,0}, WinCreatorGroupServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_SERVER_RID
} } },
70 { {0,0}, WinNtAuthoritySid
, { SID_REVISION
, 0, { SECURITY_NT_AUTHORITY
}, { SECURITY_NULL_RID
} } },
71 { {0,0}, WinDialupSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_DIALUP_RID
} } },
72 { {'N','U'}, WinNetworkSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_RID
} } },
73 { {0,0}, WinBatchSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BATCH_RID
} } },
74 { {'I','U'}, WinInteractiveSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} } },
75 { {'S','U'}, WinServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_SERVICE_RID
} } },
76 { {'A','N'}, WinAnonymousSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} } },
77 { {0,0}, WinProxySid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PROXY_RID
} } },
78 { {'E','D'}, WinEnterpriseControllersSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ENTERPRISE_CONTROLLERS_RID
} } },
79 { {'P','S'}, WinSelfSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PRINCIPAL_SELF_RID
} } },
80 { {'A','U'}, WinAuthenticatedUserSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} } },
81 { {'R','C'}, WinRestrictedCodeSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_RESTRICTED_CODE_RID
} } },
82 { {0,0}, WinTerminalServerSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_TERMINAL_SERVER_RID
} } },
83 { {0,0}, WinRemoteLogonIdSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_REMOTE_LOGON_RID
} } },
84 { {'S','Y'}, WinLocalSystemSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} } },
85 { {'L','S'}, WinLocalServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SERVICE_RID
} } },
86 { {'N','S'}, WinNetworkServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_SERVICE_RID
} } },
87 { {0,0}, WinBuiltinDomainSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
} } },
88 { {'B','A'}, WinBuiltinAdministratorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} } },
89 { {'B','U'}, WinBuiltinUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} } },
90 { {'B','G'}, WinBuiltinGuestsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_GUESTS
} } },
91 { {'P','U'}, WinBuiltinPowerUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_POWER_USERS
} } },
92 { {'A','O'}, WinBuiltinAccountOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ACCOUNT_OPS
} } },
93 { {'S','O'}, WinBuiltinSystemOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_SYSTEM_OPS
} } },
94 { {'P','O'}, WinBuiltinPrintOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PRINT_OPS
} } },
95 { {'B','O'}, WinBuiltinBackupOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_BACKUP_OPS
} } },
96 { {'R','E'}, WinBuiltinReplicatorSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REPLICATOR
} } },
97 { {'R','U'}, WinBuiltinPreWindows2000CompatibleAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PREW2KCOMPACCESS
} } },
98 { {'R','D'}, WinBuiltinRemoteDesktopUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS
} } },
99 { {'N','O'}, WinBuiltinNetworkConfigurationOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS
} } },
100 { {0,0}, WinNTLMAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_NTLM_RID
} } },
101 { {0,0}, WinDigestAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_DIGEST_RID
} } },
102 { {0,0}, WinSChannelAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_SCHANNEL_RID
} } },
103 { {0,0}, WinThisOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_THIS_ORGANIZATION_RID
} } },
104 { {0,0}, WinOtherOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_OTHER_ORGANIZATION_RID
} } },
105 { {0,0}, WinBuiltinIncomingForestTrustBuildersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS
} } },
106 { {0,0}, WinBuiltinPerfMonitoringUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_MONITORING_USERS
} } },
107 { {0,0}, WinBuiltinPerfLoggingUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_LOGGING_USERS
} } },
108 { {0,0}, WinBuiltinAuthorizationAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS
} } },
109 { {0,0}, WinBuiltinTerminalServerLicenseServersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS
} } },
110 { {0,0}, WinBuiltinDCOMUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_DCOM_USERS
} } },
111 { {'L','W'}, WinLowLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_LOW_RID
} } },
112 { {'M','E'}, WinMediumLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_MEDIUM_RID
} } },
113 { {'H','I'}, WinHighLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_HIGH_RID
} } },
114 { {'S','I'}, WinSystemLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_SYSTEM_RID
} } },
117 typedef struct WELLKNOWNRID
119 WELL_KNOWN_SID_TYPE Type
;
123 static const WELLKNOWNRID WellKnownRids
[] = {
124 { WinAccountAdministratorSid
, DOMAIN_USER_RID_ADMIN
},
125 { WinAccountGuestSid
, DOMAIN_USER_RID_GUEST
},
126 { WinAccountKrbtgtSid
, DOMAIN_USER_RID_KRBTGT
},
127 { WinAccountDomainAdminsSid
, DOMAIN_GROUP_RID_ADMINS
},
128 { WinAccountDomainUsersSid
, DOMAIN_GROUP_RID_USERS
},
129 { WinAccountDomainGuestsSid
, DOMAIN_GROUP_RID_GUESTS
},
130 { WinAccountComputersSid
, DOMAIN_GROUP_RID_COMPUTERS
},
131 { WinAccountControllersSid
, DOMAIN_GROUP_RID_CONTROLLERS
},
132 { WinAccountCertAdminsSid
, DOMAIN_GROUP_RID_CERT_ADMINS
},
133 { WinAccountSchemaAdminsSid
, DOMAIN_GROUP_RID_SCHEMA_ADMINS
},
134 { WinAccountEnterpriseAdminsSid
, DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
},
135 { WinAccountPolicyAdminsSid
, DOMAIN_GROUP_RID_POLICY_ADMINS
},
136 { WinAccountRasAndIasServersSid
, DOMAIN_ALIAS_RID_RAS_SERVERS
},
139 static const SID sidWorld
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
} , { SECURITY_WORLD_RID
} };
144 static const WCHAR SDDL_ACCESS_ALLOWED
[] = {'A',0};
145 static const WCHAR SDDL_ACCESS_DENIED
[] = {'D',0};
146 static const WCHAR SDDL_OBJECT_ACCESS_ALLOWED
[] = {'O','A',0};
147 static const WCHAR SDDL_OBJECT_ACCESS_DENIED
[] = {'O','D',0};
148 static const WCHAR SDDL_AUDIT
[] = {'A','U',0};
149 static const WCHAR SDDL_ALARM
[] = {'A','L',0};
150 static const WCHAR SDDL_OBJECT_AUDIT
[] = {'O','U',0};
151 static const WCHAR SDDL_OBJECT_ALARM
[] = {'O','L',0};
156 #define ADS_RIGHT_DS_CREATE_CHILD 0x0001
157 #define ADS_RIGHT_DS_DELETE_CHILD 0x0002
158 #define ADS_RIGHT_ACTRL_DS_LIST 0x0004
159 #define ADS_RIGHT_DS_SELF 0x0008
160 #define ADS_RIGHT_DS_READ_PROP 0x0010
161 #define ADS_RIGHT_DS_WRITE_PROP 0x0020
162 #define ADS_RIGHT_DS_DELETE_TREE 0x0040
163 #define ADS_RIGHT_DS_LIST_OBJECT 0x0080
164 #define ADS_RIGHT_DS_CONTROL_ACCESS 0x0100
169 static const WCHAR SDDL_CONTAINER_INHERIT
[] = {'C','I',0};
170 static const WCHAR SDDL_OBJECT_INHERIT
[] = {'O','I',0};
171 static const WCHAR SDDL_NO_PROPAGATE
[] = {'N','P',0};
172 static const WCHAR SDDL_INHERIT_ONLY
[] = {'I','O',0};
173 static const WCHAR SDDL_INHERITED
[] = {'I','D',0};
174 static const WCHAR SDDL_AUDIT_SUCCESS
[] = {'S','A',0};
175 static const WCHAR SDDL_AUDIT_FAILURE
[] = {'F','A',0};
177 static const char * debugstr_sid(PSID sid
)
180 SID
* psid
= (SID
*)sid
;
185 auth
= psid
->IdentifierAuthority
.Value
[5] +
186 (psid
->IdentifierAuthority
.Value
[4] << 8) +
187 (psid
->IdentifierAuthority
.Value
[3] << 16) +
188 (psid
->IdentifierAuthority
.Value
[2] << 24);
190 switch (psid
->SubAuthorityCount
) {
192 return wine_dbg_sprintf("S-%d-%d", psid
->Revision
, auth
);
194 return wine_dbg_sprintf("S-%d-%d-%lu", psid
->Revision
, auth
,
195 psid
->SubAuthority
[0]);
197 return wine_dbg_sprintf("S-%d-%d-%lu-%lu", psid
->Revision
, auth
,
198 psid
->SubAuthority
[0], psid
->SubAuthority
[1]);
200 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu", psid
->Revision
, auth
,
201 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2]);
203 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
204 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
205 psid
->SubAuthority
[3]);
207 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
208 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
209 psid
->SubAuthority
[3], psid
->SubAuthority
[4]);
211 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
212 psid
->SubAuthority
[3], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
213 psid
->SubAuthority
[0], psid
->SubAuthority
[4], psid
->SubAuthority
[5]);
215 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
216 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
217 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
218 psid
->SubAuthority
[6]);
220 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
221 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
222 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
223 psid
->SubAuthority
[6], psid
->SubAuthority
[7]);
228 static const ACEFLAG AceRights
[] =
230 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
231 { SDDL_GENERIC_READ
, GENERIC_READ
},
232 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
233 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
235 { SDDL_READ_CONTROL
, READ_CONTROL
},
236 { SDDL_STANDARD_DELETE
, DELETE
},
237 { SDDL_WRITE_DAC
, WRITE_DAC
},
238 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
240 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
241 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
242 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
243 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
244 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
245 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
246 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
247 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
248 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
250 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
251 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
252 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
253 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
255 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
256 { SDDL_KEY_READ
, KEY_READ
},
257 { SDDL_KEY_WRITE
, KEY_WRITE
},
258 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
262 static const LPCWSTR AceRightBitNames
[32] = {
263 SDDL_CREATE_CHILD
, /* 0 */
267 SDDL_READ_PROPERTY
, /* 4 */
271 SDDL_CONTROL_ACCESS
, /* 8 */
279 SDDL_STANDARD_DELETE
, /* 16 */
291 SDDL_GENERIC_ALL
, /* 28 */
292 SDDL_GENERIC_EXECUTE
,
298 /* set last error code from NT status and get the proper boolean return value */
299 /* used for functions that are a simple wrapper around the corresponding ntdll API */
300 static __inline BOOL
set_ntstatus( NTSTATUS status
)
302 if (status
) SetLastError( RtlNtStatusToDosError( status
));
306 /* Exported functions */
312 AllocateLocallyUniqueId(PLUID Luid
)
316 Status
= NtAllocateLocallyUniqueId (Luid
);
317 if (!NT_SUCCESS (Status
))
319 SetLastError(RtlNtStatusToDosError(Status
));
331 AllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
332 BYTE nSubAuthorityCount
,
333 DWORD dwSubAuthority0
,
334 DWORD dwSubAuthority1
,
335 DWORD dwSubAuthority2
,
336 DWORD dwSubAuthority3
,
337 DWORD dwSubAuthority4
,
338 DWORD dwSubAuthority5
,
339 DWORD dwSubAuthority6
,
340 DWORD dwSubAuthority7
,
345 Status
= RtlAllocateAndInitializeSid(pIdentifierAuthority
,
356 if (!NT_SUCCESS(Status
))
358 SetLastError(RtlNtStatusToDosError(Status
));
370 CopySid(DWORD nDestinationSidLength
,
371 PSID pDestinationSid
,
376 Status
= RtlCopySid(nDestinationSidLength
,
379 if (!NT_SUCCESS (Status
))
381 SetLastError(RtlNtStatusToDosError(Status
));
388 static void DumpString(LPCWSTR string
, int cch
, WCHAR
**pwptr
, ULONG
*plen
)
391 cch
= strlenW(string
);
398 memcpy(*pwptr
, string
, sizeof(WCHAR
)*cch
);
403 static BOOL
DumpSidNumeric(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
406 WCHAR fmt
[] = { 'S','-','%','u','-','%','d',0 };
407 WCHAR subauthfmt
[] = { '-','%','u',0 };
411 if( !IsValidSid( psid
) || pisid
->Revision
!= SDDL_REVISION
)
413 SetLastError(ERROR_INVALID_SID
);
417 if (pisid
->IdentifierAuthority
.Value
[0] ||
418 pisid
->IdentifierAuthority
.Value
[1])
420 FIXME("not matching MS' bugs\n");
421 SetLastError(ERROR_INVALID_SID
);
425 sprintfW( buf
, fmt
, pisid
->Revision
,
427 MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
428 pisid
->IdentifierAuthority
.Value
[4] ),
429 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
430 pisid
->IdentifierAuthority
.Value
[2] )
432 DumpString(buf
, -1, pwptr
, plen
);
434 for( i
=0; i
<pisid
->SubAuthorityCount
; i
++ )
436 sprintfW( buf
, subauthfmt
, pisid
->SubAuthority
[i
] );
437 DumpString(buf
, -1, pwptr
, plen
);
442 static BOOL
DumpSid(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
445 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
447 if (WellKnownSids
[i
].wstr
[0] && EqualSid(psid
, (PSID
)&(WellKnownSids
[i
].Sid
.Revision
)))
449 DumpString(WellKnownSids
[i
].wstr
, 2, pwptr
, plen
);
454 return DumpSidNumeric(psid
, pwptr
, plen
);
457 static void DumpRights(DWORD mask
, WCHAR
**pwptr
, ULONG
*plen
)
459 static const WCHAR fmtW
[] = {'0','x','%','x',0};
466 /* first check if the right have name */
467 for (i
= 0; i
< sizeof(AceRights
)/sizeof(AceRights
[0]); i
++)
469 if (AceRights
[i
].wstr
== NULL
)
471 if (mask
== AceRights
[i
].value
)
473 DumpString(AceRights
[i
].wstr
, -1, pwptr
, plen
);
478 /* then check if it can be built from bit names */
479 for (i
= 0; i
< 32; i
++)
481 if ((mask
& (1 << i
)) && (AceRightBitNames
[i
] == NULL
))
483 /* can't be built from bit names */
484 sprintfW(buf
, fmtW
, mask
);
485 DumpString(buf
, -1, pwptr
, plen
);
490 /* build from bit names */
491 for (i
= 0; i
< 32; i
++)
493 DumpString(AceRightBitNames
[i
], -1, pwptr
, plen
);
496 static BOOL
DumpAce(LPVOID pace
, WCHAR
**pwptr
, ULONG
*plen
)
498 ACCESS_ALLOWED_ACE
*piace
; /* all the supported ACEs have the same memory layout */
499 static const WCHAR openbr
= '(';
500 static const WCHAR closebr
= ')';
501 static const WCHAR semicolon
= ';';
503 if (((PACE_HEADER
)pace
)->AceType
> SYSTEM_ALARM_ACE_TYPE
|| ((PACE_HEADER
)pace
)->AceSize
< sizeof(ACCESS_ALLOWED_ACE
))
505 SetLastError(ERROR_INVALID_ACL
);
510 DumpString(&openbr
, 1, pwptr
, plen
);
511 switch (piace
->Header
.AceType
)
513 case ACCESS_ALLOWED_ACE_TYPE
:
514 DumpString(SDDL_ACCESS_ALLOWED
, -1, pwptr
, plen
);
516 case ACCESS_DENIED_ACE_TYPE
:
517 DumpString(SDDL_ACCESS_DENIED
, -1, pwptr
, plen
);
519 case SYSTEM_AUDIT_ACE_TYPE
:
520 DumpString(SDDL_AUDIT
, -1, pwptr
, plen
);
522 case SYSTEM_ALARM_ACE_TYPE
:
523 DumpString(SDDL_ALARM
, -1, pwptr
, plen
);
526 DumpString(&semicolon
, 1, pwptr
, plen
);
528 if (piace
->Header
.AceFlags
& OBJECT_INHERIT_ACE
)
529 DumpString(SDDL_OBJECT_INHERIT
, -1, pwptr
, plen
);
530 if (piace
->Header
.AceFlags
& CONTAINER_INHERIT_ACE
)
531 DumpString(SDDL_CONTAINER_INHERIT
, -1, pwptr
, plen
);
532 if (piace
->Header
.AceFlags
& NO_PROPAGATE_INHERIT_ACE
)
533 DumpString(SDDL_NO_PROPAGATE
, -1, pwptr
, plen
);
534 if (piace
->Header
.AceFlags
& INHERIT_ONLY_ACE
)
535 DumpString(SDDL_INHERIT_ONLY
, -1, pwptr
, plen
);
536 if (piace
->Header
.AceFlags
& INHERITED_ACE
)
537 DumpString(SDDL_INHERITED
, -1, pwptr
, plen
);
538 if (piace
->Header
.AceFlags
& SUCCESSFUL_ACCESS_ACE_FLAG
)
539 DumpString(SDDL_AUDIT_SUCCESS
, -1, pwptr
, plen
);
540 if (piace
->Header
.AceFlags
& FAILED_ACCESS_ACE_FLAG
)
541 DumpString(SDDL_AUDIT_FAILURE
, -1, pwptr
, plen
);
542 DumpString(&semicolon
, 1, pwptr
, plen
);
543 DumpRights(piace
->Mask
, pwptr
, plen
);
544 DumpString(&semicolon
, 1, pwptr
, plen
);
545 /* objects not supported */
546 DumpString(&semicolon
, 1, pwptr
, plen
);
547 /* objects not supported */
548 DumpString(&semicolon
, 1, pwptr
, plen
);
549 if (!DumpSid((PSID
)&piace
->SidStart
, pwptr
, plen
))
551 DumpString(&closebr
, 1, pwptr
, plen
);
555 static BOOL
DumpAcl(PACL pacl
, WCHAR
**pwptr
, ULONG
*plen
, BOOL
protected, BOOL autoInheritReq
, BOOL autoInherited
)
561 DumpString(SDDL_PROTECTED
, -1, pwptr
, plen
);
563 DumpString(SDDL_AUTO_INHERIT_REQ
, -1, pwptr
, plen
);
565 DumpString(SDDL_AUTO_INHERITED
, -1, pwptr
, plen
);
570 if (!IsValidAcl(pacl
))
573 count
= pacl
->AceCount
;
574 for (i
= 0; i
< count
; i
++)
577 if (!GetAce(pacl
, i
, &ace
))
579 if (!DumpAce(ace
, pwptr
, plen
))
586 static BOOL
DumpOwner(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
588 static const WCHAR prefix
[] = {'O',':',0};
592 if (!GetSecurityDescriptorOwner(SecurityDescriptor
, &psid
, &bDefaulted
))
598 DumpString(prefix
, -1, pwptr
, plen
);
599 if (!DumpSid(psid
, pwptr
, plen
))
604 static BOOL
DumpGroup(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
606 static const WCHAR prefix
[] = {'G',':',0};
610 if (!GetSecurityDescriptorGroup(SecurityDescriptor
, &psid
, &bDefaulted
))
616 DumpString(prefix
, -1, pwptr
, plen
);
617 if (!DumpSid(psid
, pwptr
, plen
))
622 static BOOL
DumpDacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
624 static const WCHAR dacl
[] = {'D',':',0};
625 SECURITY_DESCRIPTOR_CONTROL control
;
626 BOOL present
, defaulted
;
630 if (!GetSecurityDescriptorDacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
633 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
639 DumpString(dacl
, 2, pwptr
, plen
);
640 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_DACL_PROTECTED
, control
& SE_DACL_AUTO_INHERIT_REQ
, control
& SE_DACL_AUTO_INHERITED
))
645 static BOOL
DumpSacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
647 static const WCHAR sacl
[] = {'S',':',0};
648 SECURITY_DESCRIPTOR_CONTROL control
;
649 BOOL present
, defaulted
;
653 if (!GetSecurityDescriptorSacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
656 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
662 DumpString(sacl
, 2, pwptr
, plen
);
663 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_SACL_PROTECTED
, control
& SE_SACL_AUTO_INHERIT_REQ
, control
& SE_SACL_AUTO_INHERITED
))
668 /******************************************************************************
669 * ConvertSecurityDescriptorToStringSecurityDescriptorW [ADVAPI32.@]
673 ConvertSecurityDescriptorToStringSecurityDescriptorW(PSECURITY_DESCRIPTOR SecurityDescriptor
,
675 SECURITY_INFORMATION SecurityInformation
,
676 LPWSTR
*OutputString
,
682 if (SDRevision
!= SDDL_REVISION_1
)
684 ERR("Pogram requested unknown SDDL revision %d\n", SDRevision
);
685 SetLastError(ERROR_UNKNOWN_REVISION
);
690 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
691 if (!DumpOwner(SecurityDescriptor
, NULL
, &len
))
693 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
694 if (!DumpGroup(SecurityDescriptor
, NULL
, &len
))
696 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
697 if (!DumpDacl(SecurityDescriptor
, NULL
, &len
))
699 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
700 if (!DumpSacl(SecurityDescriptor
, NULL
, &len
))
703 wstr
= wptr
= LocalAlloc(0, (len
+ 1)*sizeof(WCHAR
));
704 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
705 if (!DumpOwner(SecurityDescriptor
, &wptr
, NULL
))
707 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
708 if (!DumpGroup(SecurityDescriptor
, &wptr
, NULL
))
710 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
711 if (!DumpDacl(SecurityDescriptor
, &wptr
, NULL
))
713 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
714 if (!DumpSacl(SecurityDescriptor
, &wptr
, NULL
))
718 TRACE("ret: %s, %d\n", wine_dbgstr_w(wstr
), len
);
719 *OutputString
= wstr
;
721 *OutputLen
= strlenW(*OutputString
)+1;
726 /******************************************************************************
727 * ConvertSecurityDescriptorToStringSecurityDescriptorA [ADVAPI32.@]
731 ConvertSecurityDescriptorToStringSecurityDescriptorA(PSECURITY_DESCRIPTOR SecurityDescriptor
,
733 SECURITY_INFORMATION Information
,
739 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
743 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
744 *OutputString
= HeapAlloc(GetProcessHeap(), 0, lenA
);
745 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
748 if (OutputLen
!= NULL
)
754 *OutputString
= NULL
;
762 /******************************************************************************
763 * ComputeStringSidSize
765 static DWORD
ComputeStringSidSize(LPCWSTR StringSid
)
767 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I(-S)+ */
772 if (*StringSid
== '-')
778 return GetSidLengthRequired(ctok
- 2);
780 else /* String constant format - Only available in winxp and above */
784 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
785 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
786 return GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
789 return GetSidLengthRequired(0);
792 /******************************************************************************
793 * ParseStringSidToSid
795 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
)
800 TRACE("%s, %p, %p\n", debugstr_w(StringSid
), pSid
, cBytes
);
803 SetLastError(ERROR_INVALID_PARAMETER
);
804 TRACE("StringSid is NULL, returning FALSE\n");
808 *cBytes
= ComputeStringSidSize(StringSid
);
809 if (!pisid
) /* Simply compute the size */
811 TRACE("only size requested, returning TRUE\n");
815 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
817 DWORD i
= 0, identAuth
;
818 DWORD csubauth
= ((*cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
820 StringSid
+= 2; /* Advance to Revision */
821 pisid
->Revision
= atoiW(StringSid
);
823 if (pisid
->Revision
!= SDDL_REVISION
)
825 TRACE("Revision %d is unknown\n", pisid
->Revision
);
826 goto lend
; /* ERROR_INVALID_SID */
830 TRACE("SubAuthorityCount is 0\n");
831 goto lend
; /* ERROR_INVALID_SID */
834 pisid
->SubAuthorityCount
= csubauth
;
836 /* Advance to identifier authority */
837 while (*StringSid
&& *StringSid
!= '-')
839 if (*StringSid
== '-')
842 /* MS' implementation can't handle values greater than 2^32 - 1, so
843 * we don't either; assume most significant bytes are always 0
845 pisid
->IdentifierAuthority
.Value
[0] = 0;
846 pisid
->IdentifierAuthority
.Value
[1] = 0;
847 identAuth
= atoiW(StringSid
);
848 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
849 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
850 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
851 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
853 /* Advance to first sub authority */
854 while (*StringSid
&& *StringSid
!= '-')
856 if (*StringSid
== '-')
861 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
863 while (*StringSid
&& *StringSid
!= '-')
865 if (*StringSid
== '-')
869 if (i
!= pisid
->SubAuthorityCount
)
870 goto lend
; /* ERROR_INVALID_SID */
874 else /* String constant format - Only available in winxp and above */
877 pisid
->Revision
= SDDL_REVISION
;
879 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
880 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
883 pisid
->SubAuthorityCount
= WellKnownSids
[i
].Sid
.SubAuthorityCount
;
884 pisid
->IdentifierAuthority
= WellKnownSids
[i
].Sid
.IdentifierAuthority
;
885 for (j
= 0; j
< WellKnownSids
[i
].Sid
.SubAuthorityCount
; j
++)
886 pisid
->SubAuthority
[j
] = WellKnownSids
[i
].Sid
.SubAuthority
[j
];
891 FIXME("String constant not supported: %s\n", debugstr_wn(StringSid
, 2));
896 SetLastError(ERROR_INVALID_SID
);
898 TRACE("returning %s\n", bret
? "TRUE" : "FALSE");
902 /******************************************************************************
903 * ParseAclStringFlags
905 static DWORD
ParseAclStringFlags(LPCWSTR
* StringAcl
)
908 LPCWSTR szAcl
= *StringAcl
;
910 while (*szAcl
!= '(')
914 flags
|= SE_DACL_PROTECTED
;
916 else if (*szAcl
== 'A')
920 flags
|= SE_DACL_AUTO_INHERIT_REQ
;
921 else if (*szAcl
== 'I')
922 flags
|= SE_DACL_AUTO_INHERITED
;
931 /******************************************************************************
934 static const ACEFLAG AceType
[] =
936 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
937 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
938 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
939 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
941 { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
942 { SDDL_OBJECT_ACCESS_DENIED, ACCESS_DENIED_OBJECT_ACE_TYPE },
943 { SDDL_OBJECT_ALARM, SYSTEM_ALARM_OBJECT_ACE_TYPE },
944 { SDDL_OBJECT_AUDIT, SYSTEM_AUDIT_OBJECT_ACE_TYPE },
949 static BYTE
ParseAceStringType(LPCWSTR
* StringAcl
)
952 LPCWSTR szAcl
= *StringAcl
;
953 const ACEFLAG
*lpaf
= AceType
;
956 (len
= strlenW(lpaf
->wstr
)) &&
957 strncmpW(lpaf
->wstr
, szAcl
, len
))
968 /******************************************************************************
969 * ParseAceStringFlags
971 static const ACEFLAG AceFlags
[] =
973 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
974 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
975 { SDDL_INHERITED
, INHERITED_ACE
},
976 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
977 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
978 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
979 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
983 static BYTE
ParseAceStringFlags(LPCWSTR
* StringAcl
)
987 LPCWSTR szAcl
= *StringAcl
;
989 while (*szAcl
!= ';')
991 const ACEFLAG
*lpaf
= AceFlags
;
994 (len
= strlenW(lpaf
->wstr
)) &&
995 strncmpW(lpaf
->wstr
, szAcl
, len
))
1001 flags
|= lpaf
->value
;
1010 /******************************************************************************
1011 * ParseAceStringRights
1013 static DWORD
ParseAceStringRights(LPCWSTR
* StringAcl
)
1017 LPCWSTR szAcl
= *StringAcl
;
1019 if ((*szAcl
== '0') && (*(szAcl
+ 1) == 'x'))
1023 while (*p
&& *p
!= ';')
1026 if (p
- szAcl
<= 10 /* 8 hex digits + "0x" */ )
1028 rights
= strtoulW(szAcl
, NULL
, 16);
1032 WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl
, p
- szAcl
));
1036 while (*szAcl
!= ';')
1038 const ACEFLAG
*lpaf
= AceRights
;
1040 while (lpaf
->wstr
&&
1041 (len
= strlenW(lpaf
->wstr
)) &&
1042 strncmpW(lpaf
->wstr
, szAcl
, len
))
1050 rights
|= lpaf
->value
;
1060 /******************************************************************************
1061 * ParseStringAclToAcl
1063 * dacl_flags(string_ace1)(string_ace2)... (string_acen)
1065 static BOOL
ParseStringAclToAcl(LPCWSTR StringAcl
, LPDWORD lpdwFlags
,
1066 PACL pAcl
, LPDWORD cBytes
)
1070 DWORD length
= sizeof(ACL
);
1073 PACCESS_ALLOWED_ACE pAce
= NULL
; /* pointer to current ACE */
1075 TRACE("%s\n", debugstr_w(StringAcl
));
1080 if (pAcl
) /* pAce is only useful if we're setting values */
1081 pAce
= (PACCESS_ALLOWED_ACE
) (pAcl
+ 1);
1083 /* Parse ACL flags */
1084 *lpdwFlags
= ParseAclStringFlags(&StringAcl
);
1087 while (*StringAcl
== '(')
1091 /* Parse ACE type */
1092 val
= ParseAceStringType(&StringAcl
);
1094 pAce
->Header
.AceType
= (BYTE
) val
;
1095 if (*StringAcl
!= ';')
1099 /* Parse ACE flags */
1100 val
= ParseAceStringFlags(&StringAcl
);
1102 pAce
->Header
.AceFlags
= (BYTE
) val
;
1103 if (*StringAcl
!= ';')
1107 /* Parse ACE rights */
1108 val
= ParseAceStringRights(&StringAcl
);
1111 if (*StringAcl
!= ';')
1115 /* Parse ACE object guid */
1116 if (*StringAcl
!= ';')
1118 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
1123 /* Parse ACE inherit object guid */
1124 if (*StringAcl
!= ';')
1126 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
1131 /* Parse ACE account sid */
1132 if (ParseStringSidToSid(StringAcl
, pAce
? &pAce
->SidStart
: NULL
, &sidlen
))
1134 while (*StringAcl
&& *StringAcl
!= ')')
1138 if (*StringAcl
!= ')')
1142 acesize
= sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
) + sidlen
;
1146 pAce
->Header
.AceSize
= acesize
;
1147 pAce
= (PACCESS_ALLOWED_ACE
)((LPBYTE
)pAce
+ acesize
);
1154 if (length
> 0xffff)
1156 ERR("ACL too large\n");
1162 pAcl
->AclRevision
= ACL_REVISION
;
1164 pAcl
->AclSize
= length
;
1165 pAcl
->AceCount
= acecount
++;
1171 SetLastError(ERROR_INVALID_ACL
);
1172 WARN("Invalid ACE string format\n");
1177 /******************************************************************************
1178 * ParseStringSecurityDescriptorToSecurityDescriptor
1180 static BOOL
ParseStringSecurityDescriptorToSecurityDescriptor(
1181 LPCWSTR StringSecurityDescriptor
,
1182 SECURITY_DESCRIPTOR_RELATIVE
* SecurityDescriptor
,
1187 WCHAR tok
[MAX_PATH
];
1189 LPBYTE lpNext
= NULL
;
1192 *cBytes
= sizeof(SECURITY_DESCRIPTOR
);
1194 if (SecurityDescriptor
)
1195 lpNext
= (LPBYTE
)(SecurityDescriptor
+ 1);
1197 while (*StringSecurityDescriptor
)
1199 toktype
= *StringSecurityDescriptor
;
1201 /* Expect char identifier followed by ':' */
1202 StringSecurityDescriptor
++;
1203 if (*StringSecurityDescriptor
!= ':')
1205 SetLastError(ERROR_INVALID_PARAMETER
);
1208 StringSecurityDescriptor
++;
1211 lptoken
= StringSecurityDescriptor
;
1212 while (*lptoken
&& *lptoken
!= ':')
1218 len
= lptoken
- StringSecurityDescriptor
;
1219 memcpy( tok
, StringSecurityDescriptor
, len
* sizeof(WCHAR
) );
1228 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
1231 if (SecurityDescriptor
)
1233 SecurityDescriptor
->Owner
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1234 lpNext
+= bytes
; /* Advance to next token */
1246 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
1249 if (SecurityDescriptor
)
1251 SecurityDescriptor
->Group
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1252 lpNext
+= bytes
; /* Advance to next token */
1265 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1268 if (SecurityDescriptor
)
1270 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
| flags
;
1271 SecurityDescriptor
->Dacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1272 lpNext
+= bytes
; /* Advance to next token */
1285 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1288 if (SecurityDescriptor
)
1290 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
| flags
;
1291 SecurityDescriptor
->Sacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1292 lpNext
+= bytes
; /* Advance to next token */
1301 FIXME("Unknown token\n");
1302 SetLastError(ERROR_INVALID_PARAMETER
);
1306 StringSecurityDescriptor
= lptoken
;
1316 /******************************************************************************
1317 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
1320 BOOL WINAPI
ConvertStringSecurityDescriptorToSecurityDescriptorW(
1321 LPCWSTR StringSecurityDescriptor
,
1322 DWORD StringSDRevision
,
1323 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1324 PULONG SecurityDescriptorSize
)
1327 SECURITY_DESCRIPTOR
* psd
;
1330 TRACE("%s\n", debugstr_w(StringSecurityDescriptor
));
1332 if (GetVersion() & 0x80000000)
1334 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1337 else if (!StringSecurityDescriptor
|| !SecurityDescriptor
)
1339 SetLastError(ERROR_INVALID_PARAMETER
);
1342 else if (StringSDRevision
!= SID_REVISION
)
1344 SetLastError(ERROR_UNKNOWN_REVISION
);
1348 /* Compute security descriptor length */
1349 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1353 psd
= *SecurityDescriptor
= LocalAlloc(GMEM_ZEROINIT
, cBytes
);
1354 if (!psd
) goto lend
;
1356 psd
->Revision
= SID_REVISION
;
1357 psd
->Control
|= SE_SELF_RELATIVE
;
1359 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1360 (SECURITY_DESCRIPTOR_RELATIVE
*)psd
, &cBytes
))
1366 if (SecurityDescriptorSize
)
1367 *SecurityDescriptorSize
= cBytes
;
1372 TRACE(" ret=%d\n", bret
);
1377 /* Winehq cvs 20050916 */
1378 /******************************************************************************
1379 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
1382 BOOL WINAPI
ConvertStringSecurityDescriptorToSecurityDescriptorA(
1383 LPCSTR StringSecurityDescriptor
,
1384 DWORD StringSDRevision
,
1385 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1386 PULONG SecurityDescriptorSize
)
1390 LPWSTR StringSecurityDescriptorW
;
1392 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
1393 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1395 if (StringSecurityDescriptorW
)
1397 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
1399 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
1400 StringSDRevision
, SecurityDescriptor
,
1401 SecurityDescriptorSize
);
1402 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
1412 EqualPrefixSid(PSID pSid1
,
1415 return RtlEqualPrefixSid (pSid1
, pSid2
);
1423 EqualSid(PSID pSid1
,
1426 return RtlEqualSid (pSid1
, pSid2
);
1434 * Docs says this function does NOT return a value
1435 * even thou it's defined to return a PVOID...
1440 return RtlFreeSid(pSid
);
1448 GetLengthSid(PSID pSid
)
1450 return (DWORD
)RtlLengthSid(pSid
);
1457 PSID_IDENTIFIER_AUTHORITY WINAPI
1458 GetSidIdentifierAuthority(PSID pSid
)
1460 return RtlIdentifierAuthoritySid(pSid
);
1468 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
1470 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
1478 GetSidSubAuthority(PSID pSid
,
1479 DWORD nSubAuthority
)
1481 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
1489 GetSidSubAuthorityCount(PSID pSid
)
1491 return RtlSubAuthorityCountSid(pSid
);
1499 InitializeSid(PSID Sid
,
1500 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
1501 BYTE nSubAuthorityCount
)
1505 Status
= RtlInitializeSid(Sid
,
1506 pIdentifierAuthority
,
1507 nSubAuthorityCount
);
1508 if (!NT_SUCCESS(Status
))
1510 SetLastError(RtlNtStatusToDosError(Status
));
1522 IsValidSid(PSID pSid
)
1524 return (BOOL
)RtlValidSid(pSid
);
1532 ConvertSidToStringSidW(PSID Sid
,
1536 UNICODE_STRING UnicodeString
;
1537 WCHAR FixedBuffer
[64];
1539 if (!RtlValidSid(Sid
))
1541 SetLastError(ERROR_INVALID_SID
);
1545 UnicodeString
.Length
= 0;
1546 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
1547 UnicodeString
.Buffer
= FixedBuffer
;
1548 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
1549 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1551 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
1554 if (!NT_SUCCESS(Status
))
1556 SetLastError(RtlNtStatusToDosError(Status
));
1560 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
1561 if (NULL
== *StringSid
)
1563 if (UnicodeString
.Buffer
!= FixedBuffer
)
1565 RtlFreeUnicodeString(&UnicodeString
);
1567 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1571 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
1572 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
1573 if (UnicodeString
.Buffer
!= FixedBuffer
)
1575 RtlFreeUnicodeString(&UnicodeString
);
1586 ConvertSidToStringSidA(PSID Sid
,
1592 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
1597 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
1600 LocalFree(StringSidW
);
1601 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1605 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
1606 if (NULL
== *StringSid
)
1608 LocalFree(StringSidW
);
1609 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1613 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
1615 LocalFree(StringSid
);
1616 LocalFree(StringSidW
);
1620 LocalFree(StringSidW
);
1630 EqualDomainSid(IN PSID pSid1
,
1643 GetWindowsAccountDomainSid(IN PSID pSid
,
1644 OUT PSID ppDomainSid
,
1645 IN OUT DWORD
* cbSid
)
1656 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
1657 IN PSID DomainSid OPTIONAL
,
1659 IN OUT DWORD
* cbSid
)
1662 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
1664 if (cbSid
== NULL
|| (DomainSid
&& !IsValidSid(DomainSid
)))
1666 SetLastError(ERROR_INVALID_PARAMETER
);
1670 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++) {
1671 if (WellKnownSids
[i
].Type
== WellKnownSidType
) {
1672 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
1674 if (*cbSid
< length
)
1677 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1682 SetLastError(ERROR_INVALID_PARAMETER
);
1685 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
1691 if (DomainSid
== NULL
|| *GetSidSubAuthorityCount(DomainSid
) == SID_MAX_SUB_AUTHORITIES
)
1693 SetLastError(ERROR_INVALID_PARAMETER
);
1697 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
1698 if (WellKnownRids
[i
].Type
== WellKnownSidType
) {
1699 UCHAR domain_subauth
= *GetSidSubAuthorityCount(DomainSid
);
1700 DWORD domain_sid_length
= GetSidLengthRequired(domain_subauth
);
1701 DWORD output_sid_length
= GetSidLengthRequired(domain_subauth
+ 1);
1703 if (*cbSid
< output_sid_length
)
1705 *cbSid
= output_sid_length
;
1706 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1711 SetLastError(ERROR_INVALID_PARAMETER
);
1714 CopyMemory(pSid
, DomainSid
, domain_sid_length
);
1715 (*GetSidSubAuthorityCount(pSid
))++;
1716 (*GetSidSubAuthority(pSid
, domain_subauth
)) = WellKnownRids
[i
].Rid
;
1717 *cbSid
= output_sid_length
;
1721 SetLastError(ERROR_INVALID_PARAMETER
);
1730 IsWellKnownSid(IN PSID pSid
,
1731 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
1734 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
1736 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
1738 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
1740 if (EqualSid(pSid
, (PSID
)((ULONG_PTR
)&WellKnownSids
[i
].Sid
.Revision
)))
1753 ConvertStringSidToSidA(IN LPCSTR StringSid
,
1756 BOOL bRetVal
= FALSE
;
1758 if (!StringSid
|| !sid
)
1759 SetLastError(ERROR_INVALID_PARAMETER
);
1762 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
1763 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1764 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
1765 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
1766 HeapFree(GetProcessHeap(), 0, wStringSid
);
1772 static const RECORD SidTable
[] =
1774 { SDDL_ACCOUNT_OPERATORS
, WinBuiltinAccountOperatorsSid
},
1775 { SDDL_ALIAS_PREW2KCOMPACC
, WinBuiltinPreWindows2000CompatibleAccessSid
},
1776 { SDDL_ANONYMOUS
, WinAnonymousSid
},
1777 { SDDL_AUTHENTICATED_USERS
, WinAuthenticatedUserSid
},
1778 { SDDL_BUILTIN_ADMINISTRATORS
, WinBuiltinAdministratorsSid
},
1779 { SDDL_BUILTIN_GUESTS
, WinBuiltinGuestsSid
},
1780 { SDDL_BACKUP_OPERATORS
, WinBuiltinBackupOperatorsSid
},
1781 { SDDL_BUILTIN_USERS
, WinBuiltinUsersSid
},
1782 { SDDL_CERT_SERV_ADMINISTRATORS
, WinAccountCertAdminsSid
/* FIXME: DOMAIN_GROUP_RID_CERT_ADMINS */ },
1783 { SDDL_CREATOR_GROUP
, WinCreatorGroupSid
},
1784 { SDDL_CREATOR_OWNER
, WinCreatorOwnerSid
},
1785 { SDDL_DOMAIN_ADMINISTRATORS
, WinAccountDomainAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ADMINS */ },
1786 { SDDL_DOMAIN_COMPUTERS
, WinAccountComputersSid
/* FIXME: DOMAIN_GROUP_RID_COMPUTERS */ },
1787 { SDDL_DOMAIN_DOMAIN_CONTROLLERS
, WinAccountControllersSid
/* FIXME: DOMAIN_GROUP_RID_CONTROLLERS */ },
1788 { SDDL_DOMAIN_GUESTS
, WinAccountDomainGuestsSid
/* FIXME: DOMAIN_GROUP_RID_GUESTS */ },
1789 { SDDL_DOMAIN_USERS
, WinAccountDomainUsersSid
/* FIXME: DOMAIN_GROUP_RID_USERS */ },
1790 { SDDL_ENTERPRISE_ADMINS
, WinAccountEnterpriseAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ENTERPRISE_ADMINS */ },
1791 { SDDL_ENTERPRISE_DOMAIN_CONTROLLERS
, WinLogonIdsSid
/* FIXME: SECURITY_SERVER_LOGON_RID */ },
1792 { SDDL_EVERYONE
, WinWorldSid
},
1793 { SDDL_GROUP_POLICY_ADMINS
, WinAccountPolicyAdminsSid
/* FIXME: DOMAIN_GROUP_RID_POLICY_ADMINS */ },
1794 { SDDL_INTERACTIVE
, WinInteractiveSid
},
1795 { SDDL_LOCAL_ADMIN
, WinAccountAdministratorSid
/* FIXME: DOMAIN_USER_RID_ADMIN */ },
1796 { SDDL_LOCAL_GUEST
, WinAccountGuestSid
/* FIXME: DOMAIN_USER_RID_GUEST */ },
1797 { SDDL_LOCAL_SERVICE
, WinLocalServiceSid
},
1798 { SDDL_LOCAL_SYSTEM
, WinLocalSystemSid
},
1799 { SDDL_NETWORK
, WinNetworkSid
},
1800 { SDDL_NETWORK_CONFIGURATION_OPS
, WinBuiltinNetworkConfigurationOperatorsSid
},
1801 { SDDL_NETWORK_SERVICE
, WinNetworkServiceSid
},
1802 { SDDL_PRINTER_OPERATORS
, WinBuiltinPrintOperatorsSid
},
1803 { SDDL_PERSONAL_SELF
, WinSelfSid
},
1804 { SDDL_POWER_USERS
, WinBuiltinPowerUsersSid
},
1805 { SDDL_RAS_SERVERS
, WinAccountRasAndIasServersSid
/* FIXME: DOMAIN_ALIAS_RID_RAS_SERVERS */ },
1806 { SDDL_REMOTE_DESKTOP
, WinBuiltinRemoteDesktopUsersSid
},
1807 { SDDL_REPLICATOR
, WinBuiltinReplicatorSid
},
1808 { SDDL_RESTRICTED_CODE
, WinRestrictedCodeSid
},
1809 { SDDL_SCHEMA_ADMINISTRATORS
, WinAccountSchemaAdminsSid
/* FIXME: DOMAIN_GROUP_RID_SCHEMA_ADMINS */ },
1810 { SDDL_SERVER_OPERATORS
, WinBuiltinSystemOperatorsSid
},
1811 { SDDL_SERVICE
, WinServiceSid
},
1819 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
1823 DWORD i
, cBytes
, identAuth
, csubauth
;
1827 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
1831 SetLastError(ERROR_INVALID_SID
);
1834 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
1836 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
1838 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
1839 size
= SECURITY_MAX_SID_SIZE
;
1840 *sid
= LocalAlloc(0, size
);
1843 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1846 ret
= CreateWellKnownSid(
1853 SetLastError(ERROR_INVALID_SID
);
1860 /* That's probably a string S-R-I-S-S... */
1861 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
1863 SetLastError(ERROR_INVALID_SID
);
1867 cBytes
= ComputeStringSidSize(StringSid
);
1868 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
1871 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1876 csubauth
= ((cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
1878 StringSid
+= 2; /* Advance to Revision */
1879 pisid
->Revision
= atoiW(StringSid
);
1881 if (pisid
->Revision
!= SDDL_REVISION
)
1883 TRACE("Revision %d is unknown\n", pisid
->Revision
);
1884 goto lend
; /* ERROR_INVALID_SID */
1888 TRACE("SubAuthorityCount is 0\n");
1889 goto lend
; /* ERROR_INVALID_SID */
1892 pisid
->SubAuthorityCount
= csubauth
;
1894 /* Advance to identifier authority */
1895 while (*StringSid
&& *StringSid
!= '-')
1897 if (*StringSid
== '-')
1900 /* MS' implementation can't handle values greater than 2^32 - 1, so
1901 * we don't either; assume most significant bytes are always 0
1903 pisid
->IdentifierAuthority
.Value
[0] = 0;
1904 pisid
->IdentifierAuthority
.Value
[1] = 0;
1905 identAuth
= atoiW(StringSid
);
1906 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
1907 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
1908 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
1909 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
1911 /* Advance to first sub authority */
1912 while (*StringSid
&& *StringSid
!= '-')
1914 if (*StringSid
== '-')
1917 pisid
->SubAuthority
[i
] = atoiW(StringSid
);
1921 while (*StringSid
&& *StringSid
!= '-')
1923 if (*StringSid
== '-')
1926 pisid
->SubAuthority
[++i
] = atoiW(StringSid
);
1929 if (i
!= pisid
->SubAuthorityCount
)
1930 goto lend
; /* ERROR_INVALID_SID */
1937 SetLastError(ERROR_INVALID_SID
);
1939 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");