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
18 WINE_DEFAULT_DEBUG_CHANNEL(advapi
);
20 #define MAX_GUID_STRING_LEN 39
23 AddAuditAccessAceEx(PACL pAcl
,
38 typedef struct _MAX_SID
40 /* same fields as struct _SID */
42 BYTE SubAuthorityCount
;
43 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
44 DWORD SubAuthority
[SID_MAX_SUB_AUTHORITIES
];
47 typedef struct WELLKNOWNSID
50 WELL_KNOWN_SID_TYPE Type
;
54 typedef struct _ACEFLAG
58 } ACEFLAG
, *LPACEFLAG
;
60 static const WELLKNOWNSID WellKnownSids
[] =
62 { {0,0}, WinNullSid
, { SID_REVISION
, 1, { SECURITY_NULL_SID_AUTHORITY
}, { SECURITY_NULL_RID
} } },
63 { {'W','D'}, WinWorldSid
, { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} } },
64 { {0,0}, WinLocalSid
, { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} } },
65 { {'C','O'}, WinCreatorOwnerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_RID
} } },
66 { {'C','G'}, WinCreatorGroupSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_RID
} } },
67 { {0,0}, WinCreatorOwnerServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_SERVER_RID
} } },
68 { {0,0}, WinCreatorGroupServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_SERVER_RID
} } },
69 { {0,0}, WinNtAuthoritySid
, { SID_REVISION
, 0, { SECURITY_NT_AUTHORITY
}, { SECURITY_NULL_RID
} } },
70 { {0,0}, WinDialupSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_DIALUP_RID
} } },
71 { {'N','U'}, WinNetworkSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_RID
} } },
72 { {0,0}, WinBatchSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BATCH_RID
} } },
73 { {'I','U'}, WinInteractiveSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} } },
74 { {'S','U'}, WinServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_SERVICE_RID
} } },
75 { {'A','N'}, WinAnonymousSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} } },
76 { {0,0}, WinProxySid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PROXY_RID
} } },
77 { {'E','D'}, WinEnterpriseControllersSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ENTERPRISE_CONTROLLERS_RID
} } },
78 { {'P','S'}, WinSelfSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PRINCIPAL_SELF_RID
} } },
79 { {'A','U'}, WinAuthenticatedUserSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} } },
80 { {'R','C'}, WinRestrictedCodeSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_RESTRICTED_CODE_RID
} } },
81 { {0,0}, WinTerminalServerSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_TERMINAL_SERVER_RID
} } },
82 { {0,0}, WinRemoteLogonIdSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_REMOTE_LOGON_RID
} } },
83 { {'S','Y'}, WinLocalSystemSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} } },
84 { {'L','S'}, WinLocalServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SERVICE_RID
} } },
85 { {'N','S'}, WinNetworkServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_SERVICE_RID
} } },
86 { {0,0}, WinBuiltinDomainSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
} } },
87 { {'B','A'}, WinBuiltinAdministratorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} } },
88 { {'B','U'}, WinBuiltinUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} } },
89 { {'B','G'}, WinBuiltinGuestsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_GUESTS
} } },
90 { {'P','U'}, WinBuiltinPowerUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_POWER_USERS
} } },
91 { {'A','O'}, WinBuiltinAccountOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ACCOUNT_OPS
} } },
92 { {'S','O'}, WinBuiltinSystemOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_SYSTEM_OPS
} } },
93 { {'P','O'}, WinBuiltinPrintOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PRINT_OPS
} } },
94 { {'B','O'}, WinBuiltinBackupOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_BACKUP_OPS
} } },
95 { {'R','E'}, WinBuiltinReplicatorSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REPLICATOR
} } },
96 { {'R','U'}, WinBuiltinPreWindows2000CompatibleAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PREW2KCOMPACCESS
} } },
97 { {'R','D'}, WinBuiltinRemoteDesktopUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS
} } },
98 { {'N','O'}, WinBuiltinNetworkConfigurationOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS
} } },
99 { {0,0}, WinNTLMAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_NTLM_RID
} } },
100 { {0,0}, WinDigestAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_DIGEST_RID
} } },
101 { {0,0}, WinSChannelAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_SCHANNEL_RID
} } },
102 { {0,0}, WinThisOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_THIS_ORGANIZATION_RID
} } },
103 { {0,0}, WinOtherOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_OTHER_ORGANIZATION_RID
} } },
104 { {0,0}, WinBuiltinIncomingForestTrustBuildersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS
} } },
105 { {0,0}, WinBuiltinPerfMonitoringUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_MONITORING_USERS
} } },
106 { {0,0}, WinBuiltinPerfLoggingUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_LOGGING_USERS
} } },
107 { {0,0}, WinBuiltinAuthorizationAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS
} } },
108 { {0,0}, WinBuiltinTerminalServerLicenseServersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS
} } },
109 { {0,0}, WinBuiltinDCOMUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_DCOM_USERS
} } },
110 { {'L','W'}, WinLowLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_LOW_RID
} } },
111 { {'M','E'}, WinMediumLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_MEDIUM_RID
} } },
112 { {'H','I'}, WinHighLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_HIGH_RID
} } },
113 { {'S','I'}, WinSystemLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_SYSTEM_RID
} } },
116 typedef struct WELLKNOWNRID
118 WELL_KNOWN_SID_TYPE Type
;
122 static const WELLKNOWNRID WellKnownRids
[] = {
123 { WinAccountAdministratorSid
, DOMAIN_USER_RID_ADMIN
},
124 { WinAccountGuestSid
, DOMAIN_USER_RID_GUEST
},
125 { WinAccountKrbtgtSid
, DOMAIN_USER_RID_KRBTGT
},
126 { WinAccountDomainAdminsSid
, DOMAIN_GROUP_RID_ADMINS
},
127 { WinAccountDomainUsersSid
, DOMAIN_GROUP_RID_USERS
},
128 { WinAccountDomainGuestsSid
, DOMAIN_GROUP_RID_GUESTS
},
129 { WinAccountComputersSid
, DOMAIN_GROUP_RID_COMPUTERS
},
130 { WinAccountControllersSid
, DOMAIN_GROUP_RID_CONTROLLERS
},
131 { WinAccountCertAdminsSid
, DOMAIN_GROUP_RID_CERT_ADMINS
},
132 { WinAccountSchemaAdminsSid
, DOMAIN_GROUP_RID_SCHEMA_ADMINS
},
133 { WinAccountEnterpriseAdminsSid
, DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
},
134 { WinAccountPolicyAdminsSid
, DOMAIN_GROUP_RID_POLICY_ADMINS
},
135 { WinAccountRasAndIasServersSid
, DOMAIN_ALIAS_RID_RAS_SERVERS
},
138 static const SID sidWorld
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
} , { SECURITY_WORLD_RID
} };
143 static const WCHAR SDDL_ACCESS_ALLOWED
[] = {'A',0};
144 static const WCHAR SDDL_ACCESS_DENIED
[] = {'D',0};
145 static const WCHAR SDDL_OBJECT_ACCESS_ALLOWED
[] = {'O','A',0};
146 static const WCHAR SDDL_OBJECT_ACCESS_DENIED
[] = {'O','D',0};
147 static const WCHAR SDDL_AUDIT
[] = {'A','U',0};
148 static const WCHAR SDDL_ALARM
[] = {'A','L',0};
149 static const WCHAR SDDL_OBJECT_AUDIT
[] = {'O','U',0};
150 static const WCHAR SDDL_OBJECT_ALARM
[] = {'O','L',0};
155 #define ADS_RIGHT_DS_CREATE_CHILD 0x0001
156 #define ADS_RIGHT_DS_DELETE_CHILD 0x0002
157 #define ADS_RIGHT_ACTRL_DS_LIST 0x0004
158 #define ADS_RIGHT_DS_SELF 0x0008
159 #define ADS_RIGHT_DS_READ_PROP 0x0010
160 #define ADS_RIGHT_DS_WRITE_PROP 0x0020
161 #define ADS_RIGHT_DS_DELETE_TREE 0x0040
162 #define ADS_RIGHT_DS_LIST_OBJECT 0x0080
163 #define ADS_RIGHT_DS_CONTROL_ACCESS 0x0100
168 static const WCHAR SDDL_CONTAINER_INHERIT
[] = {'C','I',0};
169 static const WCHAR SDDL_OBJECT_INHERIT
[] = {'O','I',0};
170 static const WCHAR SDDL_NO_PROPAGATE
[] = {'N','P',0};
171 static const WCHAR SDDL_INHERIT_ONLY
[] = {'I','O',0};
172 static const WCHAR SDDL_INHERITED
[] = {'I','D',0};
173 static const WCHAR SDDL_AUDIT_SUCCESS
[] = {'S','A',0};
174 static const WCHAR SDDL_AUDIT_FAILURE
[] = {'F','A',0};
176 static const char * debugstr_sid(PSID sid
)
179 SID
* psid
= (SID
*)sid
;
184 auth
= psid
->IdentifierAuthority
.Value
[5] +
185 (psid
->IdentifierAuthority
.Value
[4] << 8) +
186 (psid
->IdentifierAuthority
.Value
[3] << 16) +
187 (psid
->IdentifierAuthority
.Value
[2] << 24);
189 switch (psid
->SubAuthorityCount
) {
191 return wine_dbg_sprintf("S-%d-%d", psid
->Revision
, auth
);
193 return wine_dbg_sprintf("S-%d-%d-%lu", psid
->Revision
, auth
,
194 psid
->SubAuthority
[0]);
196 return wine_dbg_sprintf("S-%d-%d-%lu-%lu", psid
->Revision
, auth
,
197 psid
->SubAuthority
[0], psid
->SubAuthority
[1]);
199 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu", psid
->Revision
, auth
,
200 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2]);
202 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
203 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
204 psid
->SubAuthority
[3]);
206 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
207 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
208 psid
->SubAuthority
[3], psid
->SubAuthority
[4]);
210 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
211 psid
->SubAuthority
[3], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
212 psid
->SubAuthority
[0], psid
->SubAuthority
[4], psid
->SubAuthority
[5]);
214 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
215 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
216 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
217 psid
->SubAuthority
[6]);
219 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
220 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
221 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
222 psid
->SubAuthority
[6], psid
->SubAuthority
[7]);
227 static const ACEFLAG AceRights
[] =
229 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
230 { SDDL_GENERIC_READ
, GENERIC_READ
},
231 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
232 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
234 { SDDL_READ_CONTROL
, READ_CONTROL
},
235 { SDDL_STANDARD_DELETE
, DELETE
},
236 { SDDL_WRITE_DAC
, WRITE_DAC
},
237 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
239 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
240 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
241 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
242 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
243 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
244 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
245 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
246 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
247 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
249 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
250 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
251 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
252 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
254 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
255 { SDDL_KEY_READ
, KEY_READ
},
256 { SDDL_KEY_WRITE
, KEY_WRITE
},
257 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
261 static const LPCWSTR AceRightBitNames
[32] = {
262 SDDL_CREATE_CHILD
, /* 0 */
266 SDDL_READ_PROPERTY
, /* 4 */
270 SDDL_CONTROL_ACCESS
, /* 8 */
278 SDDL_STANDARD_DELETE
, /* 16 */
290 SDDL_GENERIC_ALL
, /* 28 */
291 SDDL_GENERIC_EXECUTE
,
297 /* set last error code from NT status and get the proper boolean return value */
298 /* used for functions that are a simple wrapper around the corresponding ntdll API */
299 static __inline BOOL
set_ntstatus( NTSTATUS status
)
301 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("Program 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
));
707 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
708 if (!DumpOwner(SecurityDescriptor
, &wptr
, NULL
))
710 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
711 if (!DumpGroup(SecurityDescriptor
, &wptr
, NULL
))
713 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
714 if (!DumpDacl(SecurityDescriptor
, &wptr
, NULL
))
716 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
717 if (!DumpSacl(SecurityDescriptor
, &wptr
, NULL
))
721 TRACE("ret: %s, %d\n", wine_dbgstr_w(wstr
), len
);
722 *OutputString
= wstr
;
724 *OutputLen
= strlenW(*OutputString
)+1;
729 /******************************************************************************
730 * ConvertSecurityDescriptorToStringSecurityDescriptorA [ADVAPI32.@]
734 ConvertSecurityDescriptorToStringSecurityDescriptorA(PSECURITY_DESCRIPTOR SecurityDescriptor
,
736 SECURITY_INFORMATION Information
,
743 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
747 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
748 *OutputString
= HeapAlloc(GetProcessHeap(), 0, lenA
);
749 if (*OutputString
== NULL
)
755 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
758 if (OutputLen
!= NULL
)
764 *OutputString
= NULL
;
772 /******************************************************************************
773 * ComputeStringSidSize
775 static DWORD
ComputeStringSidSize(LPCWSTR StringSid
)
777 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I(-S)+ */
782 if (*StringSid
== '-')
788 return GetSidLengthRequired(ctok
- 2);
790 else /* String constant format - Only available in winxp and above */
794 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
795 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
796 return GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
799 return GetSidLengthRequired(0);
802 /******************************************************************************
803 * ParseStringSidToSid
805 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
)
810 TRACE("%s, %p, %p\n", debugstr_w(StringSid
), pSid
, cBytes
);
813 SetLastError(ERROR_INVALID_PARAMETER
);
814 TRACE("StringSid is NULL, returning FALSE\n");
818 while (*StringSid
== ' ')
821 *cBytes
= ComputeStringSidSize(StringSid
);
822 if (!pisid
) /* Simply compute the size */
824 TRACE("only size requested, returning TRUE\n");
828 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
830 DWORD i
= 0, identAuth
;
831 DWORD csubauth
= ((*cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
833 StringSid
+= 2; /* Advance to Revision */
834 pisid
->Revision
= atoiW(StringSid
);
836 if (pisid
->Revision
!= SDDL_REVISION
)
838 TRACE("Revision %d is unknown\n", pisid
->Revision
);
839 goto lend
; /* ERROR_INVALID_SID */
843 TRACE("SubAuthorityCount is 0\n");
844 goto lend
; /* ERROR_INVALID_SID */
847 pisid
->SubAuthorityCount
= csubauth
;
849 /* Advance to identifier authority */
850 while (*StringSid
&& *StringSid
!= '-')
852 if (*StringSid
== '-')
855 /* MS' implementation can't handle values greater than 2^32 - 1, so
856 * we don't either; assume most significant bytes are always 0
858 pisid
->IdentifierAuthority
.Value
[0] = 0;
859 pisid
->IdentifierAuthority
.Value
[1] = 0;
860 identAuth
= atoiW(StringSid
);
861 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
862 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
863 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
864 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
866 /* Advance to first sub authority */
867 while (*StringSid
&& *StringSid
!= '-')
869 if (*StringSid
== '-')
874 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
876 while (*StringSid
&& *StringSid
!= '-')
878 if (*StringSid
== '-')
882 if (i
!= pisid
->SubAuthorityCount
)
883 goto lend
; /* ERROR_INVALID_SID */
887 else /* String constant format - Only available in winxp and above */
890 pisid
->Revision
= SDDL_REVISION
;
892 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
893 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
896 pisid
->SubAuthorityCount
= WellKnownSids
[i
].Sid
.SubAuthorityCount
;
897 pisid
->IdentifierAuthority
= WellKnownSids
[i
].Sid
.IdentifierAuthority
;
898 for (j
= 0; j
< WellKnownSids
[i
].Sid
.SubAuthorityCount
; j
++)
899 pisid
->SubAuthority
[j
] = WellKnownSids
[i
].Sid
.SubAuthority
[j
];
904 FIXME("String constant not supported: %s\n", debugstr_wn(StringSid
, 2));
909 SetLastError(ERROR_INVALID_SID
);
911 TRACE("returning %s\n", bret
? "TRUE" : "FALSE");
915 /******************************************************************************
916 * ParseAclStringFlags
918 static DWORD
ParseAclStringFlags(LPCWSTR
* StringAcl
)
921 LPCWSTR szAcl
= *StringAcl
;
923 while (*szAcl
!= '(')
927 flags
|= SE_DACL_PROTECTED
;
929 else if (*szAcl
== 'A')
933 flags
|= SE_DACL_AUTO_INHERIT_REQ
;
934 else if (*szAcl
== 'I')
935 flags
|= SE_DACL_AUTO_INHERITED
;
944 /******************************************************************************
947 static const ACEFLAG AceType
[] =
949 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
950 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
951 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
952 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
954 { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
955 { SDDL_OBJECT_ACCESS_DENIED, ACCESS_DENIED_OBJECT_ACE_TYPE },
956 { SDDL_OBJECT_ALARM, SYSTEM_ALARM_OBJECT_ACE_TYPE },
957 { SDDL_OBJECT_AUDIT, SYSTEM_AUDIT_OBJECT_ACE_TYPE },
962 static BYTE
ParseAceStringType(LPCWSTR
* StringAcl
)
965 LPCWSTR szAcl
= *StringAcl
;
966 const ACEFLAG
*lpaf
= AceType
;
969 (len
= strlenW(lpaf
->wstr
)) &&
970 strncmpW(lpaf
->wstr
, szAcl
, len
))
981 /******************************************************************************
982 * ParseAceStringFlags
984 static const ACEFLAG AceFlags
[] =
986 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
987 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
988 { SDDL_INHERITED
, INHERITED_ACE
},
989 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
990 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
991 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
992 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
996 static BYTE
ParseAceStringFlags(LPCWSTR
* StringAcl
)
1000 LPCWSTR szAcl
= *StringAcl
;
1002 while (*szAcl
!= ';')
1004 const ACEFLAG
*lpaf
= AceFlags
;
1006 while (lpaf
->wstr
&&
1007 (len
= strlenW(lpaf
->wstr
)) &&
1008 strncmpW(lpaf
->wstr
, szAcl
, len
))
1014 flags
|= lpaf
->value
;
1023 /******************************************************************************
1024 * ParseAceStringRights
1026 static DWORD
ParseAceStringRights(LPCWSTR
* StringAcl
)
1030 LPCWSTR szAcl
= *StringAcl
;
1032 if ((*szAcl
== '0') && (*(szAcl
+ 1) == 'x'))
1036 while (*p
&& *p
!= ';')
1039 if (p
- szAcl
<= 10 /* 8 hex digits + "0x" */ )
1041 rights
= strtoulW(szAcl
, NULL
, 16);
1045 WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl
, p
- szAcl
));
1049 while (*szAcl
!= ';')
1051 const ACEFLAG
*lpaf
= AceRights
;
1053 while (lpaf
->wstr
&&
1054 (len
= strlenW(lpaf
->wstr
)) &&
1055 strncmpW(lpaf
->wstr
, szAcl
, len
))
1063 rights
|= lpaf
->value
;
1073 /******************************************************************************
1074 * ParseStringAclToAcl
1076 * dacl_flags(string_ace1)(string_ace2)... (string_acen)
1079 ParseStringAclToAcl(LPCWSTR StringAcl
,
1086 DWORD length
= sizeof(ACL
);
1089 PACCESS_ALLOWED_ACE pAce
= NULL
; /* pointer to current ACE */
1091 TRACE("%s\n", debugstr_w(StringAcl
));
1096 if (pAcl
) /* pAce is only useful if we're setting values */
1097 pAce
= (PACCESS_ALLOWED_ACE
) (pAcl
+ 1);
1099 /* Parse ACL flags */
1100 *lpdwFlags
= ParseAclStringFlags(&StringAcl
);
1103 while (*StringAcl
== '(')
1107 /* Parse ACE type */
1108 val
= ParseAceStringType(&StringAcl
);
1110 pAce
->Header
.AceType
= (BYTE
) val
;
1111 if (*StringAcl
!= ';')
1115 /* Parse ACE flags */
1116 val
= ParseAceStringFlags(&StringAcl
);
1118 pAce
->Header
.AceFlags
= (BYTE
) val
;
1119 if (*StringAcl
!= ';')
1123 /* Parse ACE rights */
1124 val
= ParseAceStringRights(&StringAcl
);
1127 if (*StringAcl
!= ';')
1131 /* Parse ACE object guid */
1132 if (*StringAcl
!= ';')
1134 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
1139 /* Parse ACE inherit object guid */
1140 if (*StringAcl
!= ';')
1142 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
1147 /* Parse ACE account sid */
1148 if (ParseStringSidToSid(StringAcl
, pAce
? &pAce
->SidStart
: NULL
, &sidlen
))
1150 while (*StringAcl
&& *StringAcl
!= ')')
1154 if (*StringAcl
!= ')')
1158 acesize
= sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
) + sidlen
;
1162 pAce
->Header
.AceSize
= acesize
;
1163 pAce
= (PACCESS_ALLOWED_ACE
)((LPBYTE
)pAce
+ acesize
);
1170 if (length
> 0xffff)
1172 ERR("ACL too large\n");
1178 pAcl
->AclRevision
= ACL_REVISION
;
1180 pAcl
->AclSize
= length
;
1181 pAcl
->AceCount
= acecount
++;
1187 SetLastError(ERROR_INVALID_ACL
);
1188 WARN("Invalid ACE string format\n");
1193 /******************************************************************************
1194 * ParseStringSecurityDescriptorToSecurityDescriptor
1197 ParseStringSecurityDescriptorToSecurityDescriptor(LPCWSTR StringSecurityDescriptor
,
1198 SECURITY_DESCRIPTOR_RELATIVE
* SecurityDescriptor
,
1203 WCHAR tok
[MAX_PATH
];
1205 LPBYTE lpNext
= NULL
;
1208 *cBytes
= sizeof(SECURITY_DESCRIPTOR
);
1210 if (SecurityDescriptor
)
1211 lpNext
= (LPBYTE
)(SecurityDescriptor
+ 1);
1213 while (*StringSecurityDescriptor
)
1215 toktype
= *StringSecurityDescriptor
;
1217 /* Expect char identifier followed by ':' */
1218 StringSecurityDescriptor
++;
1219 if (*StringSecurityDescriptor
!= ':')
1221 SetLastError(ERROR_INVALID_PARAMETER
);
1224 StringSecurityDescriptor
++;
1227 lptoken
= StringSecurityDescriptor
;
1228 while (*lptoken
&& *lptoken
!= ':')
1234 len
= lptoken
- StringSecurityDescriptor
;
1235 memcpy( tok
, StringSecurityDescriptor
, len
* sizeof(WCHAR
) );
1244 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
1247 if (SecurityDescriptor
)
1249 SecurityDescriptor
->Owner
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1250 lpNext
+= bytes
; /* Advance to next token */
1262 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
1265 if (SecurityDescriptor
)
1267 SecurityDescriptor
->Group
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1268 lpNext
+= bytes
; /* Advance to next token */
1281 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1284 if (SecurityDescriptor
)
1286 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
| flags
;
1287 SecurityDescriptor
->Dacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1288 lpNext
+= bytes
; /* Advance to next token */
1301 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1304 if (SecurityDescriptor
)
1306 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
| flags
;
1307 SecurityDescriptor
->Sacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1308 lpNext
+= bytes
; /* Advance to next token */
1317 FIXME("Unknown token\n");
1318 SetLastError(ERROR_INVALID_PARAMETER
);
1322 StringSecurityDescriptor
= lptoken
;
1332 /******************************************************************************
1333 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
1337 ConvertStringSecurityDescriptorToSecurityDescriptorW(LPCWSTR StringSecurityDescriptor
,
1338 DWORD StringSDRevision
,
1339 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1340 PULONG SecurityDescriptorSize
)
1343 SECURITY_DESCRIPTOR
* psd
;
1346 TRACE("%s\n", debugstr_w(StringSecurityDescriptor
));
1348 if (GetVersion() & 0x80000000)
1350 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1353 else if (!StringSecurityDescriptor
|| !SecurityDescriptor
)
1355 SetLastError(ERROR_INVALID_PARAMETER
);
1358 else if (StringSDRevision
!= SID_REVISION
)
1360 SetLastError(ERROR_UNKNOWN_REVISION
);
1364 /* Compute security descriptor length */
1365 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1369 psd
= *SecurityDescriptor
= LocalAlloc(GMEM_ZEROINIT
, cBytes
);
1370 if (!psd
) goto lend
;
1372 psd
->Revision
= SID_REVISION
;
1373 psd
->Control
|= SE_SELF_RELATIVE
;
1375 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1376 (SECURITY_DESCRIPTOR_RELATIVE
*)psd
, &cBytes
))
1382 if (SecurityDescriptorSize
)
1383 *SecurityDescriptorSize
= cBytes
;
1388 TRACE(" ret=%d\n", bret
);
1393 /* Winehq cvs 20050916 */
1394 /******************************************************************************
1395 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
1400 ConvertStringSecurityDescriptorToSecurityDescriptorA(LPCSTR StringSecurityDescriptor
,
1401 DWORD StringSDRevision
,
1402 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1403 PULONG SecurityDescriptorSize
)
1407 LPWSTR StringSecurityDescriptorW
;
1409 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
1410 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1412 if (StringSecurityDescriptorW
)
1414 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
1416 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
1417 StringSDRevision
, SecurityDescriptor
,
1418 SecurityDescriptorSize
);
1419 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
1430 EqualPrefixSid(PSID pSid1
,
1433 return RtlEqualPrefixSid (pSid1
, pSid2
);
1442 EqualSid(PSID pSid1
,
1445 SetLastError(ERROR_SUCCESS
);
1446 return RtlEqualSid (pSid1
, pSid2
);
1454 * Docs says this function does NOT return a value
1455 * even thou it's defined to return a PVOID...
1461 return RtlFreeSid(pSid
);
1470 GetLengthSid(PSID pSid
)
1472 return (DWORD
)RtlLengthSid(pSid
);
1479 PSID_IDENTIFIER_AUTHORITY
1481 GetSidIdentifierAuthority(PSID pSid
)
1483 return RtlIdentifierAuthoritySid(pSid
);
1492 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
1494 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
1503 GetSidSubAuthority(PSID pSid
,
1504 DWORD nSubAuthority
)
1506 SetLastError(ERROR_SUCCESS
);
1507 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
1516 GetSidSubAuthorityCount(PSID pSid
)
1518 SetLastError(ERROR_SUCCESS
);
1519 return RtlSubAuthorityCountSid(pSid
);
1528 InitializeSid(PSID Sid
,
1529 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
1530 BYTE nSubAuthorityCount
)
1534 Status
= RtlInitializeSid(Sid
,
1535 pIdentifierAuthority
,
1536 nSubAuthorityCount
);
1537 if (!NT_SUCCESS(Status
))
1539 SetLastError(RtlNtStatusToDosError(Status
));
1552 IsValidSid(PSID pSid
)
1554 return (BOOL
)RtlValidSid(pSid
);
1563 ConvertSidToStringSidW(PSID Sid
,
1567 UNICODE_STRING UnicodeString
;
1568 WCHAR FixedBuffer
[64];
1570 if (!RtlValidSid(Sid
))
1572 SetLastError(ERROR_INVALID_SID
);
1576 UnicodeString
.Length
= 0;
1577 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
1578 UnicodeString
.Buffer
= FixedBuffer
;
1579 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
1580 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1582 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
1585 if (!NT_SUCCESS(Status
))
1587 SetLastError(RtlNtStatusToDosError(Status
));
1591 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
1592 if (NULL
== *StringSid
)
1594 if (UnicodeString
.Buffer
!= FixedBuffer
)
1596 RtlFreeUnicodeString(&UnicodeString
);
1598 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1602 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
1603 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
1604 if (UnicodeString
.Buffer
!= FixedBuffer
)
1606 RtlFreeUnicodeString(&UnicodeString
);
1618 ConvertSidToStringSidA(PSID Sid
,
1624 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
1629 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
1632 LocalFree(StringSidW
);
1633 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1637 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
1638 if (NULL
== *StringSid
)
1640 LocalFree(StringSidW
);
1641 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1645 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
1647 LocalFree(StringSid
);
1648 LocalFree(StringSidW
);
1652 LocalFree(StringSidW
);
1663 EqualDomainSid(IN PSID pSid1
,
1677 GetWindowsAccountDomainSid(IN PSID pSid
,
1678 OUT PSID ppDomainSid
,
1679 IN OUT DWORD
* cbSid
)
1691 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
1692 IN PSID DomainSid OPTIONAL
,
1694 IN OUT DWORD
* cbSid
)
1697 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
1699 if (cbSid
== NULL
|| (DomainSid
&& !IsValidSid(DomainSid
)))
1701 SetLastError(ERROR_INVALID_PARAMETER
);
1705 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++) {
1706 if (WellKnownSids
[i
].Type
== WellKnownSidType
) {
1707 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
1709 if (*cbSid
< length
)
1712 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1717 SetLastError(ERROR_INVALID_PARAMETER
);
1720 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
1726 if (DomainSid
== NULL
|| *GetSidSubAuthorityCount(DomainSid
) == SID_MAX_SUB_AUTHORITIES
)
1728 SetLastError(ERROR_INVALID_PARAMETER
);
1732 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
1733 if (WellKnownRids
[i
].Type
== WellKnownSidType
) {
1734 UCHAR domain_subauth
= *GetSidSubAuthorityCount(DomainSid
);
1735 DWORD domain_sid_length
= GetSidLengthRequired(domain_subauth
);
1736 DWORD output_sid_length
= GetSidLengthRequired(domain_subauth
+ 1);
1738 if (*cbSid
< output_sid_length
)
1740 *cbSid
= output_sid_length
;
1741 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1746 SetLastError(ERROR_INVALID_PARAMETER
);
1749 CopyMemory(pSid
, DomainSid
, domain_sid_length
);
1750 (*GetSidSubAuthorityCount(pSid
))++;
1751 (*GetSidSubAuthority(pSid
, domain_subauth
)) = WellKnownRids
[i
].Rid
;
1752 *cbSid
= output_sid_length
;
1756 SetLastError(ERROR_INVALID_PARAMETER
);
1766 IsWellKnownSid(IN PSID pSid
,
1767 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
1770 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
1772 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
1774 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
1776 if (EqualSid(pSid
, (PSID
)(&WellKnownSids
[i
].Sid
.Revision
)))
1790 ConvertStringSidToSidA(IN LPCSTR StringSid
,
1793 BOOL bRetVal
= FALSE
;
1795 TRACE("%s, %p\n", debugstr_a(StringSid
), sid
);
1796 if (GetVersion() & 0x80000000)
1797 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1798 else if (!StringSid
|| !sid
)
1799 SetLastError(ERROR_INVALID_PARAMETER
);
1802 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
1803 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1804 if (wStringSid
== NULL
)
1806 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
1807 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
1808 HeapFree(GetProcessHeap(), 0, wStringSid
);
1814 static const RECORD SidTable
[] =
1816 { SDDL_ACCOUNT_OPERATORS
, WinBuiltinAccountOperatorsSid
},
1817 { SDDL_ALIAS_PREW2KCOMPACC
, WinBuiltinPreWindows2000CompatibleAccessSid
},
1818 { SDDL_ANONYMOUS
, WinAnonymousSid
},
1819 { SDDL_AUTHENTICATED_USERS
, WinAuthenticatedUserSid
},
1820 { SDDL_BUILTIN_ADMINISTRATORS
, WinBuiltinAdministratorsSid
},
1821 { SDDL_BUILTIN_GUESTS
, WinBuiltinGuestsSid
},
1822 { SDDL_BACKUP_OPERATORS
, WinBuiltinBackupOperatorsSid
},
1823 { SDDL_BUILTIN_USERS
, WinBuiltinUsersSid
},
1824 { SDDL_CERT_SERV_ADMINISTRATORS
, WinAccountCertAdminsSid
/* FIXME: DOMAIN_GROUP_RID_CERT_ADMINS */ },
1825 { SDDL_CREATOR_GROUP
, WinCreatorGroupSid
},
1826 { SDDL_CREATOR_OWNER
, WinCreatorOwnerSid
},
1827 { SDDL_DOMAIN_ADMINISTRATORS
, WinAccountDomainAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ADMINS */ },
1828 { SDDL_DOMAIN_COMPUTERS
, WinAccountComputersSid
/* FIXME: DOMAIN_GROUP_RID_COMPUTERS */ },
1829 { SDDL_DOMAIN_DOMAIN_CONTROLLERS
, WinAccountControllersSid
/* FIXME: DOMAIN_GROUP_RID_CONTROLLERS */ },
1830 { SDDL_DOMAIN_GUESTS
, WinAccountDomainGuestsSid
/* FIXME: DOMAIN_GROUP_RID_GUESTS */ },
1831 { SDDL_DOMAIN_USERS
, WinAccountDomainUsersSid
/* FIXME: DOMAIN_GROUP_RID_USERS */ },
1832 { SDDL_ENTERPRISE_ADMINS
, WinAccountEnterpriseAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ENTERPRISE_ADMINS */ },
1833 { SDDL_ENTERPRISE_DOMAIN_CONTROLLERS
, WinEnterpriseControllersSid
},
1834 { SDDL_EVERYONE
, WinWorldSid
},
1835 { SDDL_GROUP_POLICY_ADMINS
, WinAccountPolicyAdminsSid
/* FIXME: DOMAIN_GROUP_RID_POLICY_ADMINS */ },
1836 { SDDL_INTERACTIVE
, WinInteractiveSid
},
1837 { SDDL_LOCAL_ADMIN
, WinAccountAdministratorSid
/* FIXME: DOMAIN_USER_RID_ADMIN */ },
1838 { SDDL_LOCAL_GUEST
, WinAccountGuestSid
/* FIXME: DOMAIN_USER_RID_GUEST */ },
1839 { SDDL_LOCAL_SERVICE
, WinLocalServiceSid
},
1840 { SDDL_LOCAL_SYSTEM
, WinLocalSystemSid
},
1841 { SDDL_NETWORK
, WinNetworkSid
},
1842 { SDDL_NETWORK_CONFIGURATION_OPS
, WinBuiltinNetworkConfigurationOperatorsSid
},
1843 { SDDL_NETWORK_SERVICE
, WinNetworkServiceSid
},
1844 { SDDL_PRINTER_OPERATORS
, WinBuiltinPrintOperatorsSid
},
1845 { SDDL_PERSONAL_SELF
, WinSelfSid
},
1846 { SDDL_POWER_USERS
, WinBuiltinPowerUsersSid
},
1847 { SDDL_RAS_SERVERS
, WinAccountRasAndIasServersSid
/* FIXME: DOMAIN_ALIAS_RID_RAS_SERVERS */ },
1848 { SDDL_REMOTE_DESKTOP
, WinBuiltinRemoteDesktopUsersSid
},
1849 { SDDL_REPLICATOR
, WinBuiltinReplicatorSid
},
1850 { SDDL_RESTRICTED_CODE
, WinRestrictedCodeSid
},
1851 { SDDL_SCHEMA_ADMINISTRATORS
, WinAccountSchemaAdminsSid
/* FIXME: DOMAIN_GROUP_RID_SCHEMA_ADMINS */ },
1852 { SDDL_SERVER_OPERATORS
, WinBuiltinSystemOperatorsSid
},
1853 { SDDL_SERVICE
, WinServiceSid
},
1862 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
1866 DWORD i
, cBytes
, identAuth
, csubauth
;
1870 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
1874 SetLastError(ERROR_INVALID_SID
);
1878 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
1880 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
1882 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
1883 size
= SECURITY_MAX_SID_SIZE
;
1884 *sid
= LocalAlloc(0, size
);
1887 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1890 ret
= CreateWellKnownSid(knownSid
,
1896 SetLastError(ERROR_INVALID_SID
);
1903 /* That's probably a string S-R-I-S-S... */
1904 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
1906 SetLastError(ERROR_INVALID_SID
);
1910 cBytes
= ComputeStringSidSize(StringSid
);
1911 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
1914 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1919 csubauth
= ((cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
1921 StringSid
+= 2; /* Advance to Revision */
1922 pisid
->Revision
= atoiW(StringSid
);
1924 if (pisid
->Revision
!= SDDL_REVISION
)
1926 TRACE("Revision %d is unknown\n", pisid
->Revision
);
1927 goto lend
; /* ERROR_INVALID_SID */
1931 TRACE("SubAuthorityCount is 0\n");
1932 goto lend
; /* ERROR_INVALID_SID */
1935 pisid
->SubAuthorityCount
= csubauth
;
1937 /* Advance to identifier authority */
1938 while (*StringSid
&& *StringSid
!= '-')
1940 if (*StringSid
== '-')
1943 /* MS' implementation can't handle values greater than 2^32 - 1, so
1944 * we don't either; assume most significant bytes are always 0
1946 pisid
->IdentifierAuthority
.Value
[0] = 0;
1947 pisid
->IdentifierAuthority
.Value
[1] = 0;
1948 identAuth
= atoiW(StringSid
);
1949 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
1950 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
1951 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
1952 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
1954 /* Advance to first sub authority */
1955 while (*StringSid
&& *StringSid
!= '-')
1957 if (*StringSid
== '-')
1962 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
1964 while (*StringSid
&& *StringSid
!= '-')
1966 if (*StringSid
== '-')
1970 if (i
!= pisid
->SubAuthorityCount
)
1971 goto lend
; /* ERROR_INVALID_SID */
1980 SetLastError(ERROR_INVALID_SID
);
1983 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");