2 * COPYRIGHT: See COPYING in the top level directory
4 * Copyright 1999, 2000 Juergen Schmied <juergen.schmied@debitel.net>
5 * Copyright 2003 CodeWeavers Inc. (Ulrich Czekalla)
6 * Copyright 2006 Robert Reif
7 * Copyright 2006 Hervé Poussineau
9 * PROJECT: ReactOS system libraries
10 * FILE: dll/win32/advapi32/sec/sid.c
11 * PURPOSE: Security ID functions
15 WINE_DEFAULT_DEBUG_CHANNEL(advapi
);
17 #define MAX_GUID_STRING_LEN 39
20 AddAuditAccessAceEx(PACL pAcl
,
35 typedef struct _MAX_SID
37 /* same fields as struct _SID */
39 BYTE SubAuthorityCount
;
40 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
41 DWORD SubAuthority
[SID_MAX_SUB_AUTHORITIES
];
44 typedef struct WELLKNOWNSID
47 WELL_KNOWN_SID_TYPE Type
;
51 typedef struct _ACEFLAG
55 } ACEFLAG
, *LPACEFLAG
;
57 static const WELLKNOWNSID WellKnownSids
[] =
59 { {0,0}, WinNullSid
, { SID_REVISION
, 1, { SECURITY_NULL_SID_AUTHORITY
}, { SECURITY_NULL_RID
} } },
60 { {'W','D'}, WinWorldSid
, { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} } },
61 { {0,0}, WinLocalSid
, { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} } },
62 { {'C','O'}, WinCreatorOwnerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_RID
} } },
63 { {'C','G'}, WinCreatorGroupSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_RID
} } },
64 { {0,0}, WinCreatorOwnerServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_SERVER_RID
} } },
65 { {0,0}, WinCreatorGroupServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_SERVER_RID
} } },
66 { {0,0}, WinNtAuthoritySid
, { SID_REVISION
, 0, { SECURITY_NT_AUTHORITY
}, { SECURITY_NULL_RID
} } },
67 { {0,0}, WinDialupSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_DIALUP_RID
} } },
68 { {'N','U'}, WinNetworkSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_RID
} } },
69 { {0,0}, WinBatchSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BATCH_RID
} } },
70 { {'I','U'}, WinInteractiveSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} } },
71 { {'S','U'}, WinServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_SERVICE_RID
} } },
72 { {'A','N'}, WinAnonymousSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} } },
73 { {0,0}, WinProxySid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PROXY_RID
} } },
74 { {'E','D'}, WinEnterpriseControllersSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ENTERPRISE_CONTROLLERS_RID
} } },
75 { {'P','S'}, WinSelfSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PRINCIPAL_SELF_RID
} } },
76 { {'A','U'}, WinAuthenticatedUserSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} } },
77 { {'R','C'}, WinRestrictedCodeSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_RESTRICTED_CODE_RID
} } },
78 { {0,0}, WinTerminalServerSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_TERMINAL_SERVER_RID
} } },
79 { {0,0}, WinRemoteLogonIdSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_REMOTE_LOGON_RID
} } },
80 { {'S','Y'}, WinLocalSystemSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} } },
81 { {'L','S'}, WinLocalServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SERVICE_RID
} } },
82 { {'N','S'}, WinNetworkServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_SERVICE_RID
} } },
83 { {0,0}, WinBuiltinDomainSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
} } },
84 { {'B','A'}, WinBuiltinAdministratorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} } },
85 { {'B','U'}, WinBuiltinUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} } },
86 { {'B','G'}, WinBuiltinGuestsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_GUESTS
} } },
87 { {'P','U'}, WinBuiltinPowerUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_POWER_USERS
} } },
88 { {'A','O'}, WinBuiltinAccountOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ACCOUNT_OPS
} } },
89 { {'S','O'}, WinBuiltinSystemOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_SYSTEM_OPS
} } },
90 { {'P','O'}, WinBuiltinPrintOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PRINT_OPS
} } },
91 { {'B','O'}, WinBuiltinBackupOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_BACKUP_OPS
} } },
92 { {'R','E'}, WinBuiltinReplicatorSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REPLICATOR
} } },
93 { {'R','U'}, WinBuiltinPreWindows2000CompatibleAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PREW2KCOMPACCESS
} } },
94 { {'R','D'}, WinBuiltinRemoteDesktopUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS
} } },
95 { {'N','O'}, WinBuiltinNetworkConfigurationOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS
} } },
96 { {0,0}, WinNTLMAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_NTLM_RID
} } },
97 { {0,0}, WinDigestAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_DIGEST_RID
} } },
98 { {0,0}, WinSChannelAuthenticationSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_PACKAGE_BASE_RID
, SECURITY_PACKAGE_SCHANNEL_RID
} } },
99 { {0,0}, WinThisOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_THIS_ORGANIZATION_RID
} } },
100 { {0,0}, WinOtherOrganizationSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_OTHER_ORGANIZATION_RID
} } },
101 { {0,0}, WinBuiltinIncomingForestTrustBuildersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS
} } },
102 { {0,0}, WinBuiltinPerfMonitoringUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_MONITORING_USERS
} } },
103 { {0,0}, WinBuiltinPerfLoggingUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_LOGGING_USERS
} } },
104 { {0,0}, WinBuiltinAuthorizationAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS
} } },
105 { {0,0}, WinBuiltinTerminalServerLicenseServersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS
} } },
106 { {0,0}, WinBuiltinDCOMUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_DCOM_USERS
} } },
107 { {'L','W'}, WinLowLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_LOW_RID
} } },
108 { {'M','E'}, WinMediumLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_MEDIUM_RID
} } },
109 { {'H','I'}, WinHighLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_HIGH_RID
} } },
110 { {'S','I'}, WinSystemLabelSid
, { SID_REVISION
, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY
}, { SECURITY_MANDATORY_SYSTEM_RID
} } },
113 typedef struct WELLKNOWNRID
115 WELL_KNOWN_SID_TYPE Type
;
119 static const WELLKNOWNRID WellKnownRids
[] = {
120 { WinAccountAdministratorSid
, DOMAIN_USER_RID_ADMIN
},
121 { WinAccountGuestSid
, DOMAIN_USER_RID_GUEST
},
122 { WinAccountKrbtgtSid
, DOMAIN_USER_RID_KRBTGT
},
123 { WinAccountDomainAdminsSid
, DOMAIN_GROUP_RID_ADMINS
},
124 { WinAccountDomainUsersSid
, DOMAIN_GROUP_RID_USERS
},
125 { WinAccountDomainGuestsSid
, DOMAIN_GROUP_RID_GUESTS
},
126 { WinAccountComputersSid
, DOMAIN_GROUP_RID_COMPUTERS
},
127 { WinAccountControllersSid
, DOMAIN_GROUP_RID_CONTROLLERS
},
128 { WinAccountCertAdminsSid
, DOMAIN_GROUP_RID_CERT_ADMINS
},
129 { WinAccountSchemaAdminsSid
, DOMAIN_GROUP_RID_SCHEMA_ADMINS
},
130 { WinAccountEnterpriseAdminsSid
, DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
},
131 { WinAccountPolicyAdminsSid
, DOMAIN_GROUP_RID_POLICY_ADMINS
},
132 { WinAccountRasAndIasServersSid
, DOMAIN_ALIAS_RID_RAS_SERVERS
},
135 static const SID sidWorld
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
} , { SECURITY_WORLD_RID
} };
140 static const WCHAR SDDL_ACCESS_ALLOWED
[] = {'A',0};
141 static const WCHAR SDDL_ACCESS_DENIED
[] = {'D',0};
142 static const WCHAR SDDL_OBJECT_ACCESS_ALLOWED
[] = {'O','A',0};
143 static const WCHAR SDDL_OBJECT_ACCESS_DENIED
[] = {'O','D',0};
144 static const WCHAR SDDL_AUDIT
[] = {'A','U',0};
145 static const WCHAR SDDL_ALARM
[] = {'A','L',0};
146 static const WCHAR SDDL_OBJECT_AUDIT
[] = {'O','U',0};
147 static const WCHAR SDDL_OBJECT_ALARM
[] = {'O','L',0};
152 #define ADS_RIGHT_DS_CREATE_CHILD 0x0001
153 #define ADS_RIGHT_DS_DELETE_CHILD 0x0002
154 #define ADS_RIGHT_ACTRL_DS_LIST 0x0004
155 #define ADS_RIGHT_DS_SELF 0x0008
156 #define ADS_RIGHT_DS_READ_PROP 0x0010
157 #define ADS_RIGHT_DS_WRITE_PROP 0x0020
158 #define ADS_RIGHT_DS_DELETE_TREE 0x0040
159 #define ADS_RIGHT_DS_LIST_OBJECT 0x0080
160 #define ADS_RIGHT_DS_CONTROL_ACCESS 0x0100
165 static const WCHAR SDDL_CONTAINER_INHERIT
[] = {'C','I',0};
166 static const WCHAR SDDL_OBJECT_INHERIT
[] = {'O','I',0};
167 static const WCHAR SDDL_NO_PROPAGATE
[] = {'N','P',0};
168 static const WCHAR SDDL_INHERIT_ONLY
[] = {'I','O',0};
169 static const WCHAR SDDL_INHERITED
[] = {'I','D',0};
170 static const WCHAR SDDL_AUDIT_SUCCESS
[] = {'S','A',0};
171 static const WCHAR SDDL_AUDIT_FAILURE
[] = {'F','A',0};
173 static const char * debugstr_sid(PSID sid
)
176 SID
* psid
= (SID
*)sid
;
181 auth
= psid
->IdentifierAuthority
.Value
[5] +
182 (psid
->IdentifierAuthority
.Value
[4] << 8) +
183 (psid
->IdentifierAuthority
.Value
[3] << 16) +
184 (psid
->IdentifierAuthority
.Value
[2] << 24);
186 switch (psid
->SubAuthorityCount
) {
188 return wine_dbg_sprintf("S-%d-%d", psid
->Revision
, auth
);
190 return wine_dbg_sprintf("S-%d-%d-%lu", psid
->Revision
, auth
,
191 psid
->SubAuthority
[0]);
193 return wine_dbg_sprintf("S-%d-%d-%lu-%lu", psid
->Revision
, auth
,
194 psid
->SubAuthority
[0], psid
->SubAuthority
[1]);
196 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu", psid
->Revision
, auth
,
197 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2]);
199 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
200 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
201 psid
->SubAuthority
[3]);
203 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
204 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
205 psid
->SubAuthority
[3], psid
->SubAuthority
[4]);
207 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
208 psid
->SubAuthority
[3], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
209 psid
->SubAuthority
[0], psid
->SubAuthority
[4], psid
->SubAuthority
[5]);
211 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
212 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
213 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
214 psid
->SubAuthority
[6]);
216 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
217 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
218 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
219 psid
->SubAuthority
[6], psid
->SubAuthority
[7]);
224 static const ACEFLAG AceRights
[] =
226 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
227 { SDDL_GENERIC_READ
, GENERIC_READ
},
228 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
229 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
231 { SDDL_READ_CONTROL
, READ_CONTROL
},
232 { SDDL_STANDARD_DELETE
, DELETE
},
233 { SDDL_WRITE_DAC
, WRITE_DAC
},
234 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
236 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
237 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
238 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
239 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
240 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
241 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
242 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
243 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
244 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
246 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
247 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
248 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
249 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
251 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
252 { SDDL_KEY_READ
, KEY_READ
},
253 { SDDL_KEY_WRITE
, KEY_WRITE
},
254 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
258 static const LPCWSTR AceRightBitNames
[32] = {
259 SDDL_CREATE_CHILD
, /* 0 */
263 SDDL_READ_PROPERTY
, /* 4 */
267 SDDL_CONTROL_ACCESS
, /* 8 */
275 SDDL_STANDARD_DELETE
, /* 16 */
287 SDDL_GENERIC_ALL
, /* 28 */
288 SDDL_GENERIC_EXECUTE
,
294 /* set last error code from NT status and get the proper boolean return value */
295 /* used for functions that are a simple wrapper around the corresponding ntdll API */
296 static __inline BOOL
set_ntstatus( NTSTATUS status
)
298 if (status
) SetLastError( RtlNtStatusToDosError( status
));
303 /* Exported functions */
309 AllocateLocallyUniqueId(PLUID Luid
)
313 Status
= NtAllocateLocallyUniqueId (Luid
);
314 if (!NT_SUCCESS (Status
))
316 SetLastError(RtlNtStatusToDosError(Status
));
328 AllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
329 BYTE nSubAuthorityCount
,
330 DWORD dwSubAuthority0
,
331 DWORD dwSubAuthority1
,
332 DWORD dwSubAuthority2
,
333 DWORD dwSubAuthority3
,
334 DWORD dwSubAuthority4
,
335 DWORD dwSubAuthority5
,
336 DWORD dwSubAuthority6
,
337 DWORD dwSubAuthority7
,
342 Status
= RtlAllocateAndInitializeSid(pIdentifierAuthority
,
353 if (!NT_SUCCESS(Status
))
355 SetLastError(RtlNtStatusToDosError(Status
));
367 CopySid(DWORD nDestinationSidLength
,
368 PSID pDestinationSid
,
373 Status
= RtlCopySid(nDestinationSidLength
,
376 if (!NT_SUCCESS (Status
))
378 SetLastError(RtlNtStatusToDosError(Status
));
385 static void DumpString(LPCWSTR string
, int cch
, WCHAR
**pwptr
, ULONG
*plen
)
388 cch
= strlenW(string
);
395 memcpy(*pwptr
, string
, sizeof(WCHAR
)*cch
);
400 static BOOL
DumpSidNumeric(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
403 WCHAR fmt
[] = { 'S','-','%','u','-','%','d',0 };
404 WCHAR subauthfmt
[] = { '-','%','u',0 };
408 if( !IsValidSid( psid
) || pisid
->Revision
!= SDDL_REVISION
)
410 SetLastError(ERROR_INVALID_SID
);
414 if (pisid
->IdentifierAuthority
.Value
[0] ||
415 pisid
->IdentifierAuthority
.Value
[1])
417 FIXME("not matching MS' bugs\n");
418 SetLastError(ERROR_INVALID_SID
);
422 sprintfW( buf
, fmt
, pisid
->Revision
,
424 MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
425 pisid
->IdentifierAuthority
.Value
[4] ),
426 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
427 pisid
->IdentifierAuthority
.Value
[2] )
429 DumpString(buf
, -1, pwptr
, plen
);
431 for( i
=0; i
<pisid
->SubAuthorityCount
; i
++ )
433 sprintfW( buf
, subauthfmt
, pisid
->SubAuthority
[i
] );
434 DumpString(buf
, -1, pwptr
, plen
);
439 static BOOL
DumpSid(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
442 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
444 if (WellKnownSids
[i
].wstr
[0] && EqualSid(psid
, (PSID
)&(WellKnownSids
[i
].Sid
.Revision
)))
446 DumpString(WellKnownSids
[i
].wstr
, 2, pwptr
, plen
);
451 return DumpSidNumeric(psid
, pwptr
, plen
);
454 static void DumpRights(DWORD mask
, WCHAR
**pwptr
, ULONG
*plen
)
456 static const WCHAR fmtW
[] = {'0','x','%','x',0};
463 /* first check if the right have name */
464 for (i
= 0; i
< sizeof(AceRights
)/sizeof(AceRights
[0]); i
++)
466 if (AceRights
[i
].wstr
== NULL
)
468 if (mask
== AceRights
[i
].value
)
470 DumpString(AceRights
[i
].wstr
, -1, pwptr
, plen
);
475 /* then check if it can be built from bit names */
476 for (i
= 0; i
< 32; i
++)
478 if ((mask
& (1 << i
)) && (AceRightBitNames
[i
] == NULL
))
480 /* can't be built from bit names */
481 sprintfW(buf
, fmtW
, mask
);
482 DumpString(buf
, -1, pwptr
, plen
);
487 /* build from bit names */
488 for (i
= 0; i
< 32; i
++)
490 DumpString(AceRightBitNames
[i
], -1, pwptr
, plen
);
493 static BOOL
DumpAce(LPVOID pace
, WCHAR
**pwptr
, ULONG
*plen
)
495 ACCESS_ALLOWED_ACE
*piace
; /* all the supported ACEs have the same memory layout */
496 static const WCHAR openbr
= '(';
497 static const WCHAR closebr
= ')';
498 static const WCHAR semicolon
= ';';
500 if (((PACE_HEADER
)pace
)->AceType
> SYSTEM_ALARM_ACE_TYPE
|| ((PACE_HEADER
)pace
)->AceSize
< sizeof(ACCESS_ALLOWED_ACE
))
502 SetLastError(ERROR_INVALID_ACL
);
507 DumpString(&openbr
, 1, pwptr
, plen
);
508 switch (piace
->Header
.AceType
)
510 case ACCESS_ALLOWED_ACE_TYPE
:
511 DumpString(SDDL_ACCESS_ALLOWED
, -1, pwptr
, plen
);
513 case ACCESS_DENIED_ACE_TYPE
:
514 DumpString(SDDL_ACCESS_DENIED
, -1, pwptr
, plen
);
516 case SYSTEM_AUDIT_ACE_TYPE
:
517 DumpString(SDDL_AUDIT
, -1, pwptr
, plen
);
519 case SYSTEM_ALARM_ACE_TYPE
:
520 DumpString(SDDL_ALARM
, -1, pwptr
, plen
);
523 DumpString(&semicolon
, 1, pwptr
, plen
);
525 if (piace
->Header
.AceFlags
& OBJECT_INHERIT_ACE
)
526 DumpString(SDDL_OBJECT_INHERIT
, -1, pwptr
, plen
);
527 if (piace
->Header
.AceFlags
& CONTAINER_INHERIT_ACE
)
528 DumpString(SDDL_CONTAINER_INHERIT
, -1, pwptr
, plen
);
529 if (piace
->Header
.AceFlags
& NO_PROPAGATE_INHERIT_ACE
)
530 DumpString(SDDL_NO_PROPAGATE
, -1, pwptr
, plen
);
531 if (piace
->Header
.AceFlags
& INHERIT_ONLY_ACE
)
532 DumpString(SDDL_INHERIT_ONLY
, -1, pwptr
, plen
);
533 if (piace
->Header
.AceFlags
& INHERITED_ACE
)
534 DumpString(SDDL_INHERITED
, -1, pwptr
, plen
);
535 if (piace
->Header
.AceFlags
& SUCCESSFUL_ACCESS_ACE_FLAG
)
536 DumpString(SDDL_AUDIT_SUCCESS
, -1, pwptr
, plen
);
537 if (piace
->Header
.AceFlags
& FAILED_ACCESS_ACE_FLAG
)
538 DumpString(SDDL_AUDIT_FAILURE
, -1, pwptr
, plen
);
539 DumpString(&semicolon
, 1, pwptr
, plen
);
540 DumpRights(piace
->Mask
, pwptr
, plen
);
541 DumpString(&semicolon
, 1, pwptr
, plen
);
542 /* objects not supported */
543 DumpString(&semicolon
, 1, pwptr
, plen
);
544 /* objects not supported */
545 DumpString(&semicolon
, 1, pwptr
, plen
);
546 if (!DumpSid((PSID
)&piace
->SidStart
, pwptr
, plen
))
548 DumpString(&closebr
, 1, pwptr
, plen
);
552 static BOOL
DumpAcl(PACL pacl
, WCHAR
**pwptr
, ULONG
*plen
, BOOL
protected, BOOL autoInheritReq
, BOOL autoInherited
)
558 DumpString(SDDL_PROTECTED
, -1, pwptr
, plen
);
560 DumpString(SDDL_AUTO_INHERIT_REQ
, -1, pwptr
, plen
);
562 DumpString(SDDL_AUTO_INHERITED
, -1, pwptr
, plen
);
567 if (!IsValidAcl(pacl
))
570 count
= pacl
->AceCount
;
571 for (i
= 0; i
< count
; i
++)
574 if (!GetAce(pacl
, i
, &ace
))
576 if (!DumpAce(ace
, pwptr
, plen
))
583 static BOOL
DumpOwner(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
585 static const WCHAR prefix
[] = {'O',':',0};
589 if (!GetSecurityDescriptorOwner(SecurityDescriptor
, &psid
, &bDefaulted
))
595 DumpString(prefix
, -1, pwptr
, plen
);
596 if (!DumpSid(psid
, pwptr
, plen
))
601 static BOOL
DumpGroup(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
603 static const WCHAR prefix
[] = {'G',':',0};
607 if (!GetSecurityDescriptorGroup(SecurityDescriptor
, &psid
, &bDefaulted
))
613 DumpString(prefix
, -1, pwptr
, plen
);
614 if (!DumpSid(psid
, pwptr
, plen
))
619 static BOOL
DumpDacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
621 static const WCHAR dacl
[] = {'D',':',0};
622 SECURITY_DESCRIPTOR_CONTROL control
;
623 BOOL present
, defaulted
;
627 if (!GetSecurityDescriptorDacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
630 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
636 DumpString(dacl
, 2, pwptr
, plen
);
637 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_DACL_PROTECTED
, control
& SE_DACL_AUTO_INHERIT_REQ
, control
& SE_DACL_AUTO_INHERITED
))
642 static BOOL
DumpSacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
644 static const WCHAR sacl
[] = {'S',':',0};
645 SECURITY_DESCRIPTOR_CONTROL control
;
646 BOOL present
, defaulted
;
650 if (!GetSecurityDescriptorSacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
653 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
659 DumpString(sacl
, 2, pwptr
, plen
);
660 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_SACL_PROTECTED
, control
& SE_SACL_AUTO_INHERIT_REQ
, control
& SE_SACL_AUTO_INHERITED
))
665 /******************************************************************************
666 * ConvertSecurityDescriptorToStringSecurityDescriptorW [ADVAPI32.@]
670 ConvertSecurityDescriptorToStringSecurityDescriptorW(PSECURITY_DESCRIPTOR SecurityDescriptor
,
672 SECURITY_INFORMATION SecurityInformation
,
673 LPWSTR
*OutputString
,
679 if (SDRevision
!= SDDL_REVISION_1
)
681 ERR("Program requested unknown SDDL revision %d\n", SDRevision
);
682 SetLastError(ERROR_UNKNOWN_REVISION
);
687 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
688 if (!DumpOwner(SecurityDescriptor
, NULL
, &len
))
690 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
691 if (!DumpGroup(SecurityDescriptor
, NULL
, &len
))
693 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
694 if (!DumpDacl(SecurityDescriptor
, NULL
, &len
))
696 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
697 if (!DumpSacl(SecurityDescriptor
, NULL
, &len
))
700 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
,
740 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
744 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
745 *OutputString
= HeapAlloc(GetProcessHeap(), 0, lenA
);
746 if (*OutputString
== NULL
)
752 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
755 if (OutputLen
!= NULL
)
761 *OutputString
= NULL
;
769 /******************************************************************************
770 * ComputeStringSidSize
772 static DWORD
ComputeStringSidSize(LPCWSTR StringSid
)
774 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I(-S)+ */
779 if (*StringSid
== '-')
785 return GetSidLengthRequired(ctok
- 2);
787 else /* String constant format - Only available in winxp and above */
791 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
792 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
793 return GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
796 return GetSidLengthRequired(0);
799 /******************************************************************************
800 * ParseStringSidToSid
802 static BOOL
ParseStringSidToSid(LPCWSTR StringSid
, PSID pSid
, LPDWORD cBytes
)
807 TRACE("%s, %p, %p\n", debugstr_w(StringSid
), pSid
, cBytes
);
810 SetLastError(ERROR_INVALID_PARAMETER
);
811 TRACE("StringSid is NULL, returning FALSE\n");
815 while (*StringSid
== ' ')
818 *cBytes
= ComputeStringSidSize(StringSid
);
819 if (!pisid
) /* Simply compute the size */
821 TRACE("only size requested, returning TRUE\n");
825 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
827 DWORD i
= 0, identAuth
;
828 DWORD csubauth
= ((*cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
830 StringSid
+= 2; /* Advance to Revision */
831 pisid
->Revision
= atoiW(StringSid
);
833 if (pisid
->Revision
!= SDDL_REVISION
)
835 TRACE("Revision %d is unknown\n", pisid
->Revision
);
836 goto lend
; /* ERROR_INVALID_SID */
840 TRACE("SubAuthorityCount is 0\n");
841 goto lend
; /* ERROR_INVALID_SID */
844 pisid
->SubAuthorityCount
= csubauth
;
846 /* Advance to identifier authority */
847 while (*StringSid
&& *StringSid
!= '-')
849 if (*StringSid
== '-')
852 /* MS' implementation can't handle values greater than 2^32 - 1, so
853 * we don't either; assume most significant bytes are always 0
855 pisid
->IdentifierAuthority
.Value
[0] = 0;
856 pisid
->IdentifierAuthority
.Value
[1] = 0;
857 identAuth
= atoiW(StringSid
);
858 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
859 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
860 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
861 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
863 /* Advance to first sub authority */
864 while (*StringSid
&& *StringSid
!= '-')
866 if (*StringSid
== '-')
871 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
873 while (*StringSid
&& *StringSid
!= '-')
875 if (*StringSid
== '-')
879 if (i
!= pisid
->SubAuthorityCount
)
880 goto lend
; /* ERROR_INVALID_SID */
884 else /* String constant format - Only available in winxp and above */
887 pisid
->Revision
= SDDL_REVISION
;
889 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
890 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
893 pisid
->SubAuthorityCount
= WellKnownSids
[i
].Sid
.SubAuthorityCount
;
894 pisid
->IdentifierAuthority
= WellKnownSids
[i
].Sid
.IdentifierAuthority
;
895 for (j
= 0; j
< WellKnownSids
[i
].Sid
.SubAuthorityCount
; j
++)
896 pisid
->SubAuthority
[j
] = WellKnownSids
[i
].Sid
.SubAuthority
[j
];
901 FIXME("String constant not supported: %s\n", debugstr_wn(StringSid
, 2));
906 SetLastError(ERROR_INVALID_SID
);
908 TRACE("returning %s\n", bret
? "TRUE" : "FALSE");
912 /******************************************************************************
913 * ParseAclStringFlags
915 static DWORD
ParseAclStringFlags(LPCWSTR
* StringAcl
)
918 LPCWSTR szAcl
= *StringAcl
;
920 while (*szAcl
!= '(')
924 flags
|= SE_DACL_PROTECTED
;
926 else if (*szAcl
== 'A')
930 flags
|= SE_DACL_AUTO_INHERIT_REQ
;
931 else if (*szAcl
== 'I')
932 flags
|= SE_DACL_AUTO_INHERITED
;
941 /******************************************************************************
944 static const ACEFLAG AceType
[] =
946 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
947 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
948 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
949 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
951 { SDDL_OBJECT_ACCESS_ALLOWED, ACCESS_ALLOWED_OBJECT_ACE_TYPE },
952 { SDDL_OBJECT_ACCESS_DENIED, ACCESS_DENIED_OBJECT_ACE_TYPE },
953 { SDDL_OBJECT_ALARM, SYSTEM_ALARM_OBJECT_ACE_TYPE },
954 { SDDL_OBJECT_AUDIT, SYSTEM_AUDIT_OBJECT_ACE_TYPE },
959 static BYTE
ParseAceStringType(LPCWSTR
* StringAcl
)
962 LPCWSTR szAcl
= *StringAcl
;
963 const ACEFLAG
*lpaf
= AceType
;
966 (len
= strlenW(lpaf
->wstr
)) &&
967 strncmpW(lpaf
->wstr
, szAcl
, len
))
978 /******************************************************************************
979 * ParseAceStringFlags
981 static const ACEFLAG AceFlags
[] =
983 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
984 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
985 { SDDL_INHERITED
, INHERITED_ACE
},
986 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
987 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
988 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
989 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
993 static BYTE
ParseAceStringFlags(LPCWSTR
* StringAcl
)
997 LPCWSTR szAcl
= *StringAcl
;
999 while (*szAcl
!= ';')
1001 const ACEFLAG
*lpaf
= AceFlags
;
1003 while (lpaf
->wstr
&&
1004 (len
= strlenW(lpaf
->wstr
)) &&
1005 strncmpW(lpaf
->wstr
, szAcl
, len
))
1011 flags
|= lpaf
->value
;
1020 /******************************************************************************
1021 * ParseAceStringRights
1023 static DWORD
ParseAceStringRights(LPCWSTR
* StringAcl
)
1027 LPCWSTR szAcl
= *StringAcl
;
1029 if ((*szAcl
== '0') && (*(szAcl
+ 1) == 'x'))
1033 while (*p
&& *p
!= ';')
1036 if (p
- szAcl
<= 10 /* 8 hex digits + "0x" */ )
1038 rights
= strtoulW(szAcl
, NULL
, 16);
1042 WARN("Invalid rights string format: %s\n", debugstr_wn(szAcl
, p
- szAcl
));
1046 while (*szAcl
!= ';')
1048 const ACEFLAG
*lpaf
= AceRights
;
1050 while (lpaf
->wstr
&&
1051 (len
= strlenW(lpaf
->wstr
)) &&
1052 strncmpW(lpaf
->wstr
, szAcl
, len
))
1060 rights
|= lpaf
->value
;
1070 /******************************************************************************
1071 * ParseStringAclToAcl
1073 * dacl_flags(string_ace1)(string_ace2)... (string_acen)
1076 ParseStringAclToAcl(LPCWSTR StringAcl
,
1083 DWORD length
= sizeof(ACL
);
1086 PACCESS_ALLOWED_ACE pAce
= NULL
; /* pointer to current ACE */
1088 TRACE("%s\n", debugstr_w(StringAcl
));
1093 if (pAcl
) /* pAce is only useful if we're setting values */
1094 pAce
= (PACCESS_ALLOWED_ACE
) (pAcl
+ 1);
1096 /* Parse ACL flags */
1097 *lpdwFlags
= ParseAclStringFlags(&StringAcl
);
1100 while (*StringAcl
== '(')
1104 /* Parse ACE type */
1105 val
= ParseAceStringType(&StringAcl
);
1107 pAce
->Header
.AceType
= (BYTE
) val
;
1108 if (*StringAcl
!= ';')
1112 /* Parse ACE flags */
1113 val
= ParseAceStringFlags(&StringAcl
);
1115 pAce
->Header
.AceFlags
= (BYTE
) val
;
1116 if (*StringAcl
!= ';')
1120 /* Parse ACE rights */
1121 val
= ParseAceStringRights(&StringAcl
);
1124 if (*StringAcl
!= ';')
1128 /* Parse ACE object guid */
1129 if (*StringAcl
!= ';')
1131 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
1136 /* Parse ACE inherit object guid */
1137 if (*StringAcl
!= ';')
1139 FIXME("Support for *_OBJECT_ACE_TYPE not implemented\n");
1144 /* Parse ACE account sid */
1145 if (ParseStringSidToSid(StringAcl
, pAce
? &pAce
->SidStart
: NULL
, &sidlen
))
1147 while (*StringAcl
&& *StringAcl
!= ')')
1151 if (*StringAcl
!= ')')
1155 acesize
= sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
) + sidlen
;
1159 pAce
->Header
.AceSize
= acesize
;
1160 pAce
= (PACCESS_ALLOWED_ACE
)((LPBYTE
)pAce
+ acesize
);
1167 if (length
> 0xffff)
1169 ERR("ACL too large\n");
1175 pAcl
->AclRevision
= ACL_REVISION
;
1177 pAcl
->AclSize
= length
;
1178 pAcl
->AceCount
= acecount
++;
1184 SetLastError(ERROR_INVALID_ACL
);
1185 WARN("Invalid ACE string format\n");
1190 /******************************************************************************
1191 * ParseStringSecurityDescriptorToSecurityDescriptor
1194 ParseStringSecurityDescriptorToSecurityDescriptor(LPCWSTR StringSecurityDescriptor
,
1195 SECURITY_DESCRIPTOR_RELATIVE
* SecurityDescriptor
,
1200 WCHAR tok
[MAX_PATH
];
1202 LPBYTE lpNext
= NULL
;
1205 *cBytes
= sizeof(SECURITY_DESCRIPTOR
);
1207 if (SecurityDescriptor
)
1208 lpNext
= (LPBYTE
)(SecurityDescriptor
+ 1);
1210 while (*StringSecurityDescriptor
)
1212 toktype
= *StringSecurityDescriptor
;
1214 /* Expect char identifier followed by ':' */
1215 StringSecurityDescriptor
++;
1216 if (*StringSecurityDescriptor
!= ':')
1218 SetLastError(ERROR_INVALID_PARAMETER
);
1221 StringSecurityDescriptor
++;
1224 lptoken
= StringSecurityDescriptor
;
1225 while (*lptoken
&& *lptoken
!= ':')
1231 len
= lptoken
- StringSecurityDescriptor
;
1232 memcpy( tok
, StringSecurityDescriptor
, len
* sizeof(WCHAR
) );
1241 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
1244 if (SecurityDescriptor
)
1246 SecurityDescriptor
->Owner
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1247 lpNext
+= bytes
; /* Advance to next token */
1259 if (!ParseStringSidToSid(tok
, lpNext
, &bytes
))
1262 if (SecurityDescriptor
)
1264 SecurityDescriptor
->Group
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1265 lpNext
+= bytes
; /* Advance to next token */
1278 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1281 if (SecurityDescriptor
)
1283 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
| flags
;
1284 SecurityDescriptor
->Dacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1285 lpNext
+= bytes
; /* Advance to next token */
1298 if (!ParseStringAclToAcl(tok
, &flags
, (PACL
)lpNext
, &bytes
))
1301 if (SecurityDescriptor
)
1303 SecurityDescriptor
->Control
|= SE_SACL_PRESENT
| flags
;
1304 SecurityDescriptor
->Sacl
= lpNext
- (LPBYTE
)SecurityDescriptor
;
1305 lpNext
+= bytes
; /* Advance to next token */
1314 FIXME("Unknown token\n");
1315 SetLastError(ERROR_INVALID_PARAMETER
);
1319 StringSecurityDescriptor
= lptoken
;
1329 /******************************************************************************
1330 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
1334 ConvertStringSecurityDescriptorToSecurityDescriptorW(LPCWSTR StringSecurityDescriptor
,
1335 DWORD StringSDRevision
,
1336 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1337 PULONG SecurityDescriptorSize
)
1340 SECURITY_DESCRIPTOR
* psd
;
1343 TRACE("%s\n", debugstr_w(StringSecurityDescriptor
));
1345 if (GetVersion() & 0x80000000)
1347 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1350 else if (!StringSecurityDescriptor
|| !SecurityDescriptor
)
1352 SetLastError(ERROR_INVALID_PARAMETER
);
1355 else if (StringSDRevision
!= SID_REVISION
)
1357 SetLastError(ERROR_UNKNOWN_REVISION
);
1361 /* Compute security descriptor length */
1362 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1366 psd
= *SecurityDescriptor
= LocalAlloc(GMEM_ZEROINIT
, cBytes
);
1367 if (!psd
) goto lend
;
1369 psd
->Revision
= SID_REVISION
;
1370 psd
->Control
|= SE_SELF_RELATIVE
;
1372 if (!ParseStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor
,
1373 (SECURITY_DESCRIPTOR_RELATIVE
*)psd
, &cBytes
))
1379 if (SecurityDescriptorSize
)
1380 *SecurityDescriptorSize
= cBytes
;
1385 TRACE(" ret=%d\n", bret
);
1390 /* Winehq cvs 20050916 */
1391 /******************************************************************************
1392 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
1397 ConvertStringSecurityDescriptorToSecurityDescriptorA(LPCSTR StringSecurityDescriptor
,
1398 DWORD StringSDRevision
,
1399 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1400 PULONG SecurityDescriptorSize
)
1404 LPWSTR StringSecurityDescriptorW
;
1406 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
1407 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1409 if (StringSecurityDescriptorW
)
1411 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
1413 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
1414 StringSDRevision
, SecurityDescriptor
,
1415 SecurityDescriptorSize
);
1416 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
1427 EqualPrefixSid(PSID pSid1
,
1430 return RtlEqualPrefixSid (pSid1
, pSid2
);
1439 EqualSid(PSID pSid1
,
1442 return RtlEqualSid (pSid1
, pSid2
);
1450 * Docs says this function does NOT return a value
1451 * even thou it's defined to return a PVOID...
1457 return RtlFreeSid(pSid
);
1466 GetLengthSid(PSID pSid
)
1468 return (DWORD
)RtlLengthSid(pSid
);
1475 PSID_IDENTIFIER_AUTHORITY
1477 GetSidIdentifierAuthority(PSID pSid
)
1479 return RtlIdentifierAuthoritySid(pSid
);
1488 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
1490 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
1499 GetSidSubAuthority(PSID pSid
,
1500 DWORD nSubAuthority
)
1502 SetLastError(ERROR_SUCCESS
);
1503 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
1512 GetSidSubAuthorityCount(PSID pSid
)
1514 SetLastError(ERROR_SUCCESS
);
1515 return RtlSubAuthorityCountSid(pSid
);
1524 InitializeSid(PSID Sid
,
1525 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
1526 BYTE nSubAuthorityCount
)
1530 Status
= RtlInitializeSid(Sid
,
1531 pIdentifierAuthority
,
1532 nSubAuthorityCount
);
1533 if (!NT_SUCCESS(Status
))
1535 SetLastError(RtlNtStatusToDosError(Status
));
1548 IsValidSid(PSID pSid
)
1550 return (BOOL
)RtlValidSid(pSid
);
1559 ConvertSidToStringSidW(PSID Sid
,
1563 UNICODE_STRING UnicodeString
;
1564 WCHAR FixedBuffer
[64];
1566 if (!RtlValidSid(Sid
))
1568 SetLastError(ERROR_INVALID_SID
);
1572 UnicodeString
.Length
= 0;
1573 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
1574 UnicodeString
.Buffer
= FixedBuffer
;
1575 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
1576 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1578 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
1581 if (!NT_SUCCESS(Status
))
1583 SetLastError(RtlNtStatusToDosError(Status
));
1587 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
1588 if (NULL
== *StringSid
)
1590 if (UnicodeString
.Buffer
!= FixedBuffer
)
1592 RtlFreeUnicodeString(&UnicodeString
);
1594 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1598 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
1599 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
1600 if (UnicodeString
.Buffer
!= FixedBuffer
)
1602 RtlFreeUnicodeString(&UnicodeString
);
1614 ConvertSidToStringSidA(PSID Sid
,
1620 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
1625 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
1628 LocalFree(StringSidW
);
1629 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1633 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
1634 if (NULL
== *StringSid
)
1636 LocalFree(StringSidW
);
1637 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1641 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
1643 LocalFree(StringSid
);
1644 LocalFree(StringSidW
);
1648 LocalFree(StringSidW
);
1659 EqualDomainSid(IN PSID pSid1
,
1673 GetWindowsAccountDomainSid(IN PSID pSid
,
1674 OUT PSID ppDomainSid
,
1675 IN OUT DWORD
* cbSid
)
1687 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
1688 IN PSID DomainSid OPTIONAL
,
1690 IN OUT DWORD
* cbSid
)
1693 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
1695 if (cbSid
== NULL
|| (DomainSid
&& !IsValidSid(DomainSid
)))
1697 SetLastError(ERROR_INVALID_PARAMETER
);
1701 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++) {
1702 if (WellKnownSids
[i
].Type
== WellKnownSidType
) {
1703 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
1705 if (*cbSid
< length
)
1708 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1713 SetLastError(ERROR_INVALID_PARAMETER
);
1716 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
1722 if (DomainSid
== NULL
|| *GetSidSubAuthorityCount(DomainSid
) == SID_MAX_SUB_AUTHORITIES
)
1724 SetLastError(ERROR_INVALID_PARAMETER
);
1728 for (i
= 0; i
< sizeof(WellKnownRids
)/sizeof(WellKnownRids
[0]); i
++)
1729 if (WellKnownRids
[i
].Type
== WellKnownSidType
) {
1730 UCHAR domain_subauth
= *GetSidSubAuthorityCount(DomainSid
);
1731 DWORD domain_sid_length
= GetSidLengthRequired(domain_subauth
);
1732 DWORD output_sid_length
= GetSidLengthRequired(domain_subauth
+ 1);
1734 if (*cbSid
< output_sid_length
)
1736 *cbSid
= output_sid_length
;
1737 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1742 SetLastError(ERROR_INVALID_PARAMETER
);
1745 CopyMemory(pSid
, DomainSid
, domain_sid_length
);
1746 (*GetSidSubAuthorityCount(pSid
))++;
1747 (*GetSidSubAuthority(pSid
, domain_subauth
)) = WellKnownRids
[i
].Rid
;
1748 *cbSid
= output_sid_length
;
1752 SetLastError(ERROR_INVALID_PARAMETER
);
1762 IsWellKnownSid(IN PSID pSid
,
1763 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
1766 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
1768 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
1770 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
1772 if (EqualSid(pSid
, (PSID
)(&WellKnownSids
[i
].Sid
.Revision
)))
1786 ConvertStringSidToSidA(IN LPCSTR StringSid
,
1789 BOOL bRetVal
= FALSE
;
1791 TRACE("%s, %p\n", debugstr_a(StringSid
), sid
);
1792 if (GetVersion() & 0x80000000)
1793 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1794 else if (!StringSid
|| !sid
)
1795 SetLastError(ERROR_INVALID_PARAMETER
);
1798 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
1799 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1800 if (wStringSid
== NULL
)
1802 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
1803 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
1804 HeapFree(GetProcessHeap(), 0, wStringSid
);
1810 static const RECORD SidTable
[] =
1812 { SDDL_ACCOUNT_OPERATORS
, WinBuiltinAccountOperatorsSid
},
1813 { SDDL_ALIAS_PREW2KCOMPACC
, WinBuiltinPreWindows2000CompatibleAccessSid
},
1814 { SDDL_ANONYMOUS
, WinAnonymousSid
},
1815 { SDDL_AUTHENTICATED_USERS
, WinAuthenticatedUserSid
},
1816 { SDDL_BUILTIN_ADMINISTRATORS
, WinBuiltinAdministratorsSid
},
1817 { SDDL_BUILTIN_GUESTS
, WinBuiltinGuestsSid
},
1818 { SDDL_BACKUP_OPERATORS
, WinBuiltinBackupOperatorsSid
},
1819 { SDDL_BUILTIN_USERS
, WinBuiltinUsersSid
},
1820 { SDDL_CERT_SERV_ADMINISTRATORS
, WinAccountCertAdminsSid
/* FIXME: DOMAIN_GROUP_RID_CERT_ADMINS */ },
1821 { SDDL_CREATOR_GROUP
, WinCreatorGroupSid
},
1822 { SDDL_CREATOR_OWNER
, WinCreatorOwnerSid
},
1823 { SDDL_DOMAIN_ADMINISTRATORS
, WinAccountDomainAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ADMINS */ },
1824 { SDDL_DOMAIN_COMPUTERS
, WinAccountComputersSid
/* FIXME: DOMAIN_GROUP_RID_COMPUTERS */ },
1825 { SDDL_DOMAIN_DOMAIN_CONTROLLERS
, WinAccountControllersSid
/* FIXME: DOMAIN_GROUP_RID_CONTROLLERS */ },
1826 { SDDL_DOMAIN_GUESTS
, WinAccountDomainGuestsSid
/* FIXME: DOMAIN_GROUP_RID_GUESTS */ },
1827 { SDDL_DOMAIN_USERS
, WinAccountDomainUsersSid
/* FIXME: DOMAIN_GROUP_RID_USERS */ },
1828 { SDDL_ENTERPRISE_ADMINS
, WinAccountEnterpriseAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ENTERPRISE_ADMINS */ },
1829 { SDDL_ENTERPRISE_DOMAIN_CONTROLLERS
, WinLogonIdsSid
/* FIXME: SECURITY_SERVER_LOGON_RID */ },
1830 { SDDL_EVERYONE
, WinWorldSid
},
1831 { SDDL_GROUP_POLICY_ADMINS
, WinAccountPolicyAdminsSid
/* FIXME: DOMAIN_GROUP_RID_POLICY_ADMINS */ },
1832 { SDDL_INTERACTIVE
, WinInteractiveSid
},
1833 { SDDL_LOCAL_ADMIN
, WinAccountAdministratorSid
/* FIXME: DOMAIN_USER_RID_ADMIN */ },
1834 { SDDL_LOCAL_GUEST
, WinAccountGuestSid
/* FIXME: DOMAIN_USER_RID_GUEST */ },
1835 { SDDL_LOCAL_SERVICE
, WinLocalServiceSid
},
1836 { SDDL_LOCAL_SYSTEM
, WinLocalSystemSid
},
1837 { SDDL_NETWORK
, WinNetworkSid
},
1838 { SDDL_NETWORK_CONFIGURATION_OPS
, WinBuiltinNetworkConfigurationOperatorsSid
},
1839 { SDDL_NETWORK_SERVICE
, WinNetworkServiceSid
},
1840 { SDDL_PRINTER_OPERATORS
, WinBuiltinPrintOperatorsSid
},
1841 { SDDL_PERSONAL_SELF
, WinSelfSid
},
1842 { SDDL_POWER_USERS
, WinBuiltinPowerUsersSid
},
1843 { SDDL_RAS_SERVERS
, WinAccountRasAndIasServersSid
/* FIXME: DOMAIN_ALIAS_RID_RAS_SERVERS */ },
1844 { SDDL_REMOTE_DESKTOP
, WinBuiltinRemoteDesktopUsersSid
},
1845 { SDDL_REPLICATOR
, WinBuiltinReplicatorSid
},
1846 { SDDL_RESTRICTED_CODE
, WinRestrictedCodeSid
},
1847 { SDDL_SCHEMA_ADMINISTRATORS
, WinAccountSchemaAdminsSid
/* FIXME: DOMAIN_GROUP_RID_SCHEMA_ADMINS */ },
1848 { SDDL_SERVER_OPERATORS
, WinBuiltinSystemOperatorsSid
},
1849 { SDDL_SERVICE
, WinServiceSid
},
1858 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
1862 DWORD i
, cBytes
, identAuth
, csubauth
;
1866 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
1870 SetLastError(ERROR_INVALID_SID
);
1874 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
1876 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
1878 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
1879 size
= SECURITY_MAX_SID_SIZE
;
1880 *sid
= LocalAlloc(0, size
);
1883 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1886 ret
= CreateWellKnownSid(knownSid
,
1892 SetLastError(ERROR_INVALID_SID
);
1899 /* That's probably a string S-R-I-S-S... */
1900 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
1902 SetLastError(ERROR_INVALID_SID
);
1906 cBytes
= ComputeStringSidSize(StringSid
);
1907 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
1910 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1915 csubauth
= ((cBytes
- GetSidLengthRequired(0)) / sizeof(DWORD
));
1917 StringSid
+= 2; /* Advance to Revision */
1918 pisid
->Revision
= atoiW(StringSid
);
1920 if (pisid
->Revision
!= SDDL_REVISION
)
1922 TRACE("Revision %d is unknown\n", pisid
->Revision
);
1923 goto lend
; /* ERROR_INVALID_SID */
1927 TRACE("SubAuthorityCount is 0\n");
1928 goto lend
; /* ERROR_INVALID_SID */
1931 pisid
->SubAuthorityCount
= csubauth
;
1933 /* Advance to identifier authority */
1934 while (*StringSid
&& *StringSid
!= '-')
1936 if (*StringSid
== '-')
1939 /* MS' implementation can't handle values greater than 2^32 - 1, so
1940 * we don't either; assume most significant bytes are always 0
1942 pisid
->IdentifierAuthority
.Value
[0] = 0;
1943 pisid
->IdentifierAuthority
.Value
[1] = 0;
1944 identAuth
= atoiW(StringSid
);
1945 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
1946 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
1947 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
1948 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
1950 /* Advance to first sub authority */
1951 while (*StringSid
&& *StringSid
!= '-')
1953 if (*StringSid
== '-')
1958 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
1960 while (*StringSid
&& *StringSid
!= '-')
1962 if (*StringSid
== '-')
1966 if (i
!= pisid
->SubAuthorityCount
)
1967 goto lend
; /* ERROR_INVALID_SID */
1976 SetLastError(ERROR_INVALID_SID
);
1979 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");