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 while (*StringSid
== ' ')
811 *cBytes
= ComputeStringSidSize(StringSid
);
812 if (!pisid
) /* Simply compute the size */
814 TRACE("only size requested, returning TRUE\n");
818 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
820 DWORD i
= 0, identAuth
;
821 DWORD csubauth
= ((*cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
823 StringSid
+= 2; /* Advance to Revision */
824 pisid
->Revision
= atoiW(StringSid
);
826 if (pisid
->Revision
!= SDDL_REVISION
)
828 TRACE("Revision %d is unknown\n", pisid
->Revision
);
829 goto lend
; /* ERROR_INVALID_SID */
833 TRACE("SubAuthorityCount is 0\n");
834 goto lend
; /* ERROR_INVALID_SID */
837 pisid
->SubAuthorityCount
= csubauth
;
839 /* Advance to identifier authority */
840 while (*StringSid
&& *StringSid
!= '-')
842 if (*StringSid
== '-')
845 /* MS' implementation can't handle values greater than 2^32 - 1, so
846 * we don't either; assume most significant bytes are always 0
848 pisid
->IdentifierAuthority
.Value
[0] = 0;
849 pisid
->IdentifierAuthority
.Value
[1] = 0;
850 identAuth
= atoiW(StringSid
);
851 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
852 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
853 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
854 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
856 /* Advance to first sub authority */
857 while (*StringSid
&& *StringSid
!= '-')
859 if (*StringSid
== '-')
864 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
866 while (*StringSid
&& *StringSid
!= '-')
868 if (*StringSid
== '-')
872 if (i
!= pisid
->SubAuthorityCount
)
873 goto lend
; /* ERROR_INVALID_SID */
877 else /* String constant format - Only available in winxp and above */
880 pisid
->Revision
= SDDL_REVISION
;
882 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
883 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
886 pisid
->SubAuthorityCount
= WellKnownSids
[i
].Sid
.SubAuthorityCount
;
887 pisid
->IdentifierAuthority
= WellKnownSids
[i
].Sid
.IdentifierAuthority
;
888 for (j
= 0; j
< WellKnownSids
[i
].Sid
.SubAuthorityCount
; j
++)
889 pisid
->SubAuthority
[j
] = WellKnownSids
[i
].Sid
.SubAuthority
[j
];
894 FIXME("String constant not supported: %s\n", debugstr_wn(StringSid
, 2));
899 SetLastError(ERROR_INVALID_SID
);
901 TRACE("returning %s\n", bret
? "TRUE" : "FALSE");
905 /******************************************************************************
906 * ParseAclStringFlags
908 static DWORD
ParseAclStringFlags(LPCWSTR
* StringAcl
)
911 LPCWSTR szAcl
= *StringAcl
;
913 while (*szAcl
!= '(')
917 flags
|= SE_DACL_PROTECTED
;
919 else if (*szAcl
== 'A')
923 flags
|= SE_DACL_AUTO_INHERIT_REQ
;
924 else if (*szAcl
== 'I')
925 flags
|= SE_DACL_AUTO_INHERITED
;
934 /******************************************************************************
937 static const ACEFLAG AceType
[] =
939 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
940 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
941 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
942 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
944 { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
945 { SDDL_OBJECT_ACCESS_DENIED, ACCESS_DENIED_OBJECT_ACE_TYPE },
946 { SDDL_OBJECT_ALARM, SYSTEM_ALARM_OBJECT_ACE_TYPE },
947 { SDDL_OBJECT_AUDIT, SYSTEM_AUDIT_OBJECT_ACE_TYPE },
952 static BYTE
ParseAceStringType(LPCWSTR
* StringAcl
)
955 LPCWSTR szAcl
= *StringAcl
;
956 const ACEFLAG
*lpaf
= AceType
;
959 (len
= strlenW(lpaf
->wstr
)) &&
960 strncmpW(lpaf
->wstr
, szAcl
, len
))
971 /******************************************************************************
972 * ParseAceStringFlags
974 static const ACEFLAG AceFlags
[] =
976 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
977 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
978 { SDDL_INHERITED
, INHERITED_ACE
},
979 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
980 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
981 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
982 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
986 static BYTE
ParseAceStringFlags(LPCWSTR
* StringAcl
)
990 LPCWSTR szAcl
= *StringAcl
;
992 while (*szAcl
!= ';')
994 const ACEFLAG
*lpaf
= AceFlags
;
997 (len
= strlenW(lpaf
->wstr
)) &&
998 strncmpW(lpaf
->wstr
, szAcl
, len
))
1004 flags
|= lpaf
->value
;
1013 /******************************************************************************
1014 * ParseAceStringRights
1016 static DWORD
ParseAceStringRights(LPCWSTR
* StringAcl
)
1020 LPCWSTR szAcl
= *StringAcl
;
1022 if ((*szAcl
== '0') && (*(szAcl
+ 1) == 'x'))
1026 while (*p
&& *p
!= ';')
1029 if (p
- szAcl
<= 10 /* 8 hex digits + "0x" */ )
1031 rights
= strtoulW(szAcl
, NULL
, 16);
1035 WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl
, p
- szAcl
));
1039 while (*szAcl
!= ';')
1041 const ACEFLAG
*lpaf
= AceRights
;
1043 while (lpaf
->wstr
&&
1044 (len
= strlenW(lpaf
->wstr
)) &&
1045 strncmpW(lpaf
->wstr
, szAcl
, len
))
1053 rights
|= lpaf
->value
;
1063 /******************************************************************************
1064 * ParseStringAclToAcl
1066 * dacl_flags(string_ace1)(string_ace2)... (string_acen)
1068 static BOOL
ParseStringAclToAcl(LPCWSTR StringAcl
, LPDWORD lpdwFlags
,
1069 PACL pAcl
, LPDWORD cBytes
)
1073 DWORD length
= sizeof(ACL
);
1076 PACCESS_ALLOWED_ACE pAce
= NULL
; /* pointer to current ACE */
1078 TRACE("%s\n", debugstr_w(StringAcl
));
1083 if (pAcl
) /* pAce is only useful if we're setting values */
1084 pAce
= (PACCESS_ALLOWED_ACE
) (pAcl
+ 1);
1086 /* Parse ACL flags */
1087 *lpdwFlags
= ParseAclStringFlags(&StringAcl
);
1090 while (*StringAcl
== '(')
1094 /* Parse ACE type */
1095 val
= ParseAceStringType(&StringAcl
);
1097 pAce
->Header
.AceType
= (BYTE
) val
;
1098 if (*StringAcl
!= ';')
1102 /* Parse ACE flags */
1103 val
= ParseAceStringFlags(&StringAcl
);
1105 pAce
->Header
.AceFlags
= (BYTE
) val
;
1106 if (*StringAcl
!= ';')
1110 /* Parse ACE rights */
1111 val
= ParseAceStringRights(&StringAcl
);
1114 if (*StringAcl
!= ';')
1118 /* Parse ACE object guid */
1119 if (*StringAcl
!= ';')
1121 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
1126 /* Parse ACE inherit object guid */
1127 if (*StringAcl
!= ';')
1129 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
1134 /* Parse ACE account sid */
1135 if (ParseStringSidToSid(StringAcl
, pAce
? &pAce
->SidStart
: NULL
, &sidlen
))
1137 while (*StringAcl
&& *StringAcl
!= ')')
1141 if (*StringAcl
!= ')')
1145 acesize
= sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
) + sidlen
;
1149 pAce
->Header
.AceSize
= acesize
;
1150 pAce
= (PACCESS_ALLOWED_ACE
)((LPBYTE
)pAce
+ acesize
);
1157 if (length
> 0xffff)
1159 ERR("ACL too large\n");
1165 pAcl
->AclRevision
= ACL_REVISION
;
1167 pAcl
->AclSize
= length
;
1168 pAcl
->AceCount
= acecount
++;
1174 SetLastError(ERROR_INVALID_ACL
);
1175 WARN("Invalid ACE string format\n");
1180 /******************************************************************************
1181 * ParseStringSecurityDescriptorToSecurityDescriptor
1183 static BOOL
ParseStringSecurityDescriptorToSecurityDescriptor(
1184 LPCWSTR StringSecurityDescriptor
,
1185 SECURITY_DESCRIPTOR_RELATIVE
* SecurityDescriptor
,
1190 WCHAR tok
[MAX_PATH
];
1192 LPBYTE lpNext
= NULL
;
1195 *cBytes
= sizeof(SECURITY_DESCRIPTOR
);
1197 if (SecurityDescriptor
)
1198 lpNext
= (LPBYTE
)(SecurityDescriptor
+ 1);
1200 while (*StringSecurityDescriptor
)
1202 toktype
= *StringSecurityDescriptor
;
1204 /* Expect char identifier followed by ':' */
1205 StringSecurityDescriptor
++;
1206 if (*StringSecurityDescriptor
!= ':')
1208 SetLastError(ERROR_INVALID_PARAMETER
);
1211 StringSecurityDescriptor
++;
1214 lptoken
= StringSecurityDescriptor
;
1215 while (*lptoken
&& *lptoken
!= ':')
1221 len
= lptoken
- StringSecurityDescriptor
;
1222 memcpy( tok
, StringSecurityDescriptor
, len
* sizeof(WCHAR
) );
1231 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
1234 if (SecurityDescriptor
)
1236 SecurityDescriptor
->Owner
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1237 lpNext
+= bytes
; /* Advance to next token */
1249 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
1252 if (SecurityDescriptor
)
1254 SecurityDescriptor
->Group
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1255 lpNext
+= bytes
; /* Advance to next token */
1268 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1271 if (SecurityDescriptor
)
1273 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
| flags
;
1274 SecurityDescriptor
->Dacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1275 lpNext
+= bytes
; /* Advance to next token */
1288 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1291 if (SecurityDescriptor
)
1293 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
| flags
;
1294 SecurityDescriptor
->Sacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1295 lpNext
+= bytes
; /* Advance to next token */
1304 FIXME("Unknown token\n");
1305 SetLastError(ERROR_INVALID_PARAMETER
);
1309 StringSecurityDescriptor
= lptoken
;
1319 /******************************************************************************
1320 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
1323 BOOL WINAPI
ConvertStringSecurityDescriptorToSecurityDescriptorW(
1324 LPCWSTR StringSecurityDescriptor
,
1325 DWORD StringSDRevision
,
1326 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1327 PULONG SecurityDescriptorSize
)
1330 SECURITY_DESCRIPTOR
* psd
;
1333 TRACE("%s\n", debugstr_w(StringSecurityDescriptor
));
1335 if (GetVersion() & 0x80000000)
1337 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1340 else if (!StringSecurityDescriptor
|| !SecurityDescriptor
)
1342 SetLastError(ERROR_INVALID_PARAMETER
);
1345 else if (StringSDRevision
!= SID_REVISION
)
1347 SetLastError(ERROR_UNKNOWN_REVISION
);
1351 /* Compute security descriptor length */
1352 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1356 psd
= *SecurityDescriptor
= LocalAlloc(GMEM_ZEROINIT
, cBytes
);
1357 if (!psd
) goto lend
;
1359 psd
->Revision
= SID_REVISION
;
1360 psd
->Control
|= SE_SELF_RELATIVE
;
1362 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1363 (SECURITY_DESCRIPTOR_RELATIVE
*)psd
, &cBytes
))
1369 if (SecurityDescriptorSize
)
1370 *SecurityDescriptorSize
= cBytes
;
1375 TRACE(" ret=%d\n", bret
);
1380 /* Winehq cvs 20050916 */
1381 /******************************************************************************
1382 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
1385 BOOL WINAPI
ConvertStringSecurityDescriptorToSecurityDescriptorA(
1386 LPCSTR StringSecurityDescriptor
,
1387 DWORD StringSDRevision
,
1388 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1389 PULONG SecurityDescriptorSize
)
1393 LPWSTR StringSecurityDescriptorW
;
1395 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
1396 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1398 if (StringSecurityDescriptorW
)
1400 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
1402 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
1403 StringSDRevision
, SecurityDescriptor
,
1404 SecurityDescriptorSize
);
1405 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
1415 EqualPrefixSid(PSID pSid1
,
1418 return RtlEqualPrefixSid (pSid1
, pSid2
);
1426 EqualSid(PSID pSid1
,
1429 return RtlEqualSid (pSid1
, pSid2
);
1437 * Docs says this function does NOT return a value
1438 * even thou it's defined to return a PVOID...
1443 return RtlFreeSid(pSid
);
1451 GetLengthSid(PSID pSid
)
1453 return (DWORD
)RtlLengthSid(pSid
);
1460 PSID_IDENTIFIER_AUTHORITY WINAPI
1461 GetSidIdentifierAuthority(PSID pSid
)
1463 return RtlIdentifierAuthoritySid(pSid
);
1471 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
1473 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
1481 GetSidSubAuthority(PSID pSid
,
1482 DWORD nSubAuthority
)
1484 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
1492 GetSidSubAuthorityCount(PSID pSid
)
1494 return RtlSubAuthorityCountSid(pSid
);
1502 InitializeSid(PSID Sid
,
1503 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
1504 BYTE nSubAuthorityCount
)
1508 Status
= RtlInitializeSid(Sid
,
1509 pIdentifierAuthority
,
1510 nSubAuthorityCount
);
1511 if (!NT_SUCCESS(Status
))
1513 SetLastError(RtlNtStatusToDosError(Status
));
1525 IsValidSid(PSID pSid
)
1527 return (BOOL
)RtlValidSid(pSid
);
1535 ConvertSidToStringSidW(PSID Sid
,
1539 UNICODE_STRING UnicodeString
;
1540 WCHAR FixedBuffer
[64];
1542 if (!RtlValidSid(Sid
))
1544 SetLastError(ERROR_INVALID_SID
);
1548 UnicodeString
.Length
= 0;
1549 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
1550 UnicodeString
.Buffer
= FixedBuffer
;
1551 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
1552 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1554 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
1557 if (!NT_SUCCESS(Status
))
1559 SetLastError(RtlNtStatusToDosError(Status
));
1563 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
1564 if (NULL
== *StringSid
)
1566 if (UnicodeString
.Buffer
!= FixedBuffer
)
1568 RtlFreeUnicodeString(&UnicodeString
);
1570 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1574 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
1575 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
1576 if (UnicodeString
.Buffer
!= FixedBuffer
)
1578 RtlFreeUnicodeString(&UnicodeString
);
1589 ConvertSidToStringSidA(PSID Sid
,
1595 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
1600 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
1603 LocalFree(StringSidW
);
1604 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1608 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
1609 if (NULL
== *StringSid
)
1611 LocalFree(StringSidW
);
1612 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1616 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
1618 LocalFree(StringSid
);
1619 LocalFree(StringSidW
);
1623 LocalFree(StringSidW
);
1633 EqualDomainSid(IN PSID pSid1
,
1646 GetWindowsAccountDomainSid(IN PSID pSid
,
1647 OUT PSID ppDomainSid
,
1648 IN OUT DWORD
* cbSid
)
1659 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
1660 IN PSID DomainSid OPTIONAL
,
1662 IN OUT DWORD
* cbSid
)
1665 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
1667 if (cbSid
== NULL
|| (DomainSid
&& !IsValidSid(DomainSid
)))
1669 SetLastError(ERROR_INVALID_PARAMETER
);
1673 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++) {
1674 if (WellKnownSids
[i
].Type
== WellKnownSidType
) {
1675 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
1677 if (*cbSid
< length
)
1680 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1685 SetLastError(ERROR_INVALID_PARAMETER
);
1688 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
1694 if (DomainSid
== NULL
|| *GetSidSubAuthorityCount(DomainSid
) == SID_MAX_SUB_AUTHORITIES
)
1696 SetLastError(ERROR_INVALID_PARAMETER
);
1700 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
1701 if (WellKnownRids
[i
].Type
== WellKnownSidType
) {
1702 UCHAR domain_subauth
= *GetSidSubAuthorityCount(DomainSid
);
1703 DWORD domain_sid_length
= GetSidLengthRequired(domain_subauth
);
1704 DWORD output_sid_length
= GetSidLengthRequired(domain_subauth
+ 1);
1706 if (*cbSid
< output_sid_length
)
1708 *cbSid
= output_sid_length
;
1709 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1714 SetLastError(ERROR_INVALID_PARAMETER
);
1717 CopyMemory(pSid
, DomainSid
, domain_sid_length
);
1718 (*GetSidSubAuthorityCount(pSid
))++;
1719 (*GetSidSubAuthority(pSid
, domain_subauth
)) = WellKnownRids
[i
].Rid
;
1720 *cbSid
= output_sid_length
;
1724 SetLastError(ERROR_INVALID_PARAMETER
);
1733 IsWellKnownSid(IN PSID pSid
,
1734 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
1737 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
1739 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
1741 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
1743 if (EqualSid(pSid
, (PSID
)((ULONG_PTR
)&WellKnownSids
[i
].Sid
.Revision
)))
1756 ConvertStringSidToSidA(IN LPCSTR StringSid
,
1759 BOOL bRetVal
= FALSE
;
1761 if (!StringSid
|| !sid
)
1762 SetLastError(ERROR_INVALID_PARAMETER
);
1765 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
1766 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1767 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
1768 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
1769 HeapFree(GetProcessHeap(), 0, wStringSid
);
1775 static const RECORD SidTable
[] =
1777 { SDDL_ACCOUNT_OPERATORS
, WinBuiltinAccountOperatorsSid
},
1778 { SDDL_ALIAS_PREW2KCOMPACC
, WinBuiltinPreWindows2000CompatibleAccessSid
},
1779 { SDDL_ANONYMOUS
, WinAnonymousSid
},
1780 { SDDL_AUTHENTICATED_USERS
, WinAuthenticatedUserSid
},
1781 { SDDL_BUILTIN_ADMINISTRATORS
, WinBuiltinAdministratorsSid
},
1782 { SDDL_BUILTIN_GUESTS
, WinBuiltinGuestsSid
},
1783 { SDDL_BACKUP_OPERATORS
, WinBuiltinBackupOperatorsSid
},
1784 { SDDL_BUILTIN_USERS
, WinBuiltinUsersSid
},
1785 { SDDL_CERT_SERV_ADMINISTRATORS
, WinAccountCertAdminsSid
/* FIXME: DOMAIN_GROUP_RID_CERT_ADMINS */ },
1786 { SDDL_CREATOR_GROUP
, WinCreatorGroupSid
},
1787 { SDDL_CREATOR_OWNER
, WinCreatorOwnerSid
},
1788 { SDDL_DOMAIN_ADMINISTRATORS
, WinAccountDomainAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ADMINS */ },
1789 { SDDL_DOMAIN_COMPUTERS
, WinAccountComputersSid
/* FIXME: DOMAIN_GROUP_RID_COMPUTERS */ },
1790 { SDDL_DOMAIN_DOMAIN_CONTROLLERS
, WinAccountControllersSid
/* FIXME: DOMAIN_GROUP_RID_CONTROLLERS */ },
1791 { SDDL_DOMAIN_GUESTS
, WinAccountDomainGuestsSid
/* FIXME: DOMAIN_GROUP_RID_GUESTS */ },
1792 { SDDL_DOMAIN_USERS
, WinAccountDomainUsersSid
/* FIXME: DOMAIN_GROUP_RID_USERS */ },
1793 { SDDL_ENTERPRISE_ADMINS
, WinAccountEnterpriseAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ENTERPRISE_ADMINS */ },
1794 { SDDL_ENTERPRISE_DOMAIN_CONTROLLERS
, WinLogonIdsSid
/* FIXME: SECURITY_SERVER_LOGON_RID */ },
1795 { SDDL_EVERYONE
, WinWorldSid
},
1796 { SDDL_GROUP_POLICY_ADMINS
, WinAccountPolicyAdminsSid
/* FIXME: DOMAIN_GROUP_RID_POLICY_ADMINS */ },
1797 { SDDL_INTERACTIVE
, WinInteractiveSid
},
1798 { SDDL_LOCAL_ADMIN
, WinAccountAdministratorSid
/* FIXME: DOMAIN_USER_RID_ADMIN */ },
1799 { SDDL_LOCAL_GUEST
, WinAccountGuestSid
/* FIXME: DOMAIN_USER_RID_GUEST */ },
1800 { SDDL_LOCAL_SERVICE
, WinLocalServiceSid
},
1801 { SDDL_LOCAL_SYSTEM
, WinLocalSystemSid
},
1802 { SDDL_NETWORK
, WinNetworkSid
},
1803 { SDDL_NETWORK_CONFIGURATION_OPS
, WinBuiltinNetworkConfigurationOperatorsSid
},
1804 { SDDL_NETWORK_SERVICE
, WinNetworkServiceSid
},
1805 { SDDL_PRINTER_OPERATORS
, WinBuiltinPrintOperatorsSid
},
1806 { SDDL_PERSONAL_SELF
, WinSelfSid
},
1807 { SDDL_POWER_USERS
, WinBuiltinPowerUsersSid
},
1808 { SDDL_RAS_SERVERS
, WinAccountRasAndIasServersSid
/* FIXME: DOMAIN_ALIAS_RID_RAS_SERVERS */ },
1809 { SDDL_REMOTE_DESKTOP
, WinBuiltinRemoteDesktopUsersSid
},
1810 { SDDL_REPLICATOR
, WinBuiltinReplicatorSid
},
1811 { SDDL_RESTRICTED_CODE
, WinRestrictedCodeSid
},
1812 { SDDL_SCHEMA_ADMINISTRATORS
, WinAccountSchemaAdminsSid
/* FIXME: DOMAIN_GROUP_RID_SCHEMA_ADMINS */ },
1813 { SDDL_SERVER_OPERATORS
, WinBuiltinSystemOperatorsSid
},
1814 { SDDL_SERVICE
, WinServiceSid
},
1822 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
1826 DWORD i
, cBytes
, identAuth
, csubauth
;
1830 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
1834 SetLastError(ERROR_INVALID_SID
);
1837 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
1839 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
1841 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
1842 size
= SECURITY_MAX_SID_SIZE
;
1843 *sid
= LocalAlloc(0, size
);
1846 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1849 ret
= CreateWellKnownSid(
1856 SetLastError(ERROR_INVALID_SID
);
1863 /* That's probably a string S-R-I-S-S... */
1864 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
1866 SetLastError(ERROR_INVALID_SID
);
1870 cBytes
= ComputeStringSidSize(StringSid
);
1871 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
1874 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1879 csubauth
= ((cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
1881 StringSid
+= 2; /* Advance to Revision */
1882 pisid
->Revision
= atoiW(StringSid
);
1884 if (pisid
->Revision
!= SDDL_REVISION
)
1886 TRACE("Revision %d is unknown\n", pisid
->Revision
);
1887 goto lend
; /* ERROR_INVALID_SID */
1891 TRACE("SubAuthorityCount is 0\n");
1892 goto lend
; /* ERROR_INVALID_SID */
1895 pisid
->SubAuthorityCount
= csubauth
;
1897 /* Advance to identifier authority */
1898 while (*StringSid
&& *StringSid
!= '-')
1900 if (*StringSid
== '-')
1903 /* MS' implementation can't handle values greater than 2^32 - 1, so
1904 * we don't either; assume most significant bytes are always 0
1906 pisid
->IdentifierAuthority
.Value
[0] = 0;
1907 pisid
->IdentifierAuthority
.Value
[1] = 0;
1908 identAuth
= atoiW(StringSid
);
1909 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
1910 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
1911 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
1912 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
1914 /* Advance to first sub authority */
1915 while (*StringSid
&& *StringSid
!= '-')
1917 if (*StringSid
== '-')
1920 pisid
->SubAuthority
[i
] = atoiW(StringSid
);
1924 while (*StringSid
&& *StringSid
!= '-')
1926 if (*StringSid
== '-')
1929 pisid
->SubAuthority
[++i
] = atoiW(StringSid
);
1932 if (i
!= pisid
->SubAuthorityCount
)
1933 goto lend
; /* ERROR_INVALID_SID */
1940 SetLastError(ERROR_INVALID_SID
);
1942 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");