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 Hervé Poussineau
8 * PROJECT: ReactOS system libraries
9 * FILE: lib/advapi32/sec/sid.c
10 * PURPOSE: Security ID functions
15 #include <wine/debug.h>
16 #include <wine/unicode.h>
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
} } },
101 static const SID sidWorld
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
} , { SECURITY_WORLD_RID
} };
106 static const WCHAR SDDL_ACCESS_ALLOWED
[] = {'A',0};
107 static const WCHAR SDDL_ACCESS_DENIED
[] = {'D',0};
108 static const WCHAR SDDL_OBJECT_ACCESS_ALLOWED
[] = {'O','A',0};
109 static const WCHAR SDDL_OBJECT_ACCESS_DENIED
[] = {'O','D',0};
110 static const WCHAR SDDL_AUDIT
[] = {'A','U',0};
111 static const WCHAR SDDL_ALARM
[] = {'A','L',0};
112 static const WCHAR SDDL_OBJECT_AUDIT
[] = {'O','U',0};
113 static const WCHAR SDDL_OBJECT_ALARM
[] = {'O','L',0};
118 #define ADS_RIGHT_DS_CREATE_CHILD 0x0001
119 #define ADS_RIGHT_DS_DELETE_CHILD 0x0002
120 #define ADS_RIGHT_ACTRL_DS_LIST 0x0004
121 #define ADS_RIGHT_DS_SELF 0x0008
122 #define ADS_RIGHT_DS_READ_PROP 0x0010
123 #define ADS_RIGHT_DS_WRITE_PROP 0x0020
124 #define ADS_RIGHT_DS_DELETE_TREE 0x0040
125 #define ADS_RIGHT_DS_LIST_OBJECT 0x0080
126 #define ADS_RIGHT_DS_CONTROL_ACCESS 0x0100
131 static const WCHAR SDDL_CONTAINER_INHERIT
[] = {'C','I',0};
132 static const WCHAR SDDL_OBJECT_INHERIT
[] = {'O','I',0};
133 static const WCHAR SDDL_NO_PROPAGATE
[] = {'N','P',0};
134 static const WCHAR SDDL_INHERIT_ONLY
[] = {'I','O',0};
135 static const WCHAR SDDL_INHERITED
[] = {'I','D',0};
136 static const WCHAR SDDL_AUDIT_SUCCESS
[] = {'S','A',0};
137 static const WCHAR SDDL_AUDIT_FAILURE
[] = {'F','A',0};
139 static const char * debugstr_sid(PSID sid
)
142 SID
* psid
= (SID
*)sid
;
147 auth
= psid
->IdentifierAuthority
.Value
[5] +
148 (psid
->IdentifierAuthority
.Value
[4] << 8) +
149 (psid
->IdentifierAuthority
.Value
[3] << 16) +
150 (psid
->IdentifierAuthority
.Value
[2] << 24);
152 switch (psid
->SubAuthorityCount
) {
154 return wine_dbg_sprintf("S-%d-%d", psid
->Revision
, auth
);
156 return wine_dbg_sprintf("S-%d-%d-%lu", psid
->Revision
, auth
,
157 psid
->SubAuthority
[0]);
159 return wine_dbg_sprintf("S-%d-%d-%lu-%lu", psid
->Revision
, auth
,
160 psid
->SubAuthority
[0], psid
->SubAuthority
[1]);
162 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu", psid
->Revision
, auth
,
163 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2]);
165 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
166 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
167 psid
->SubAuthority
[3]);
169 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
170 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
171 psid
->SubAuthority
[3], psid
->SubAuthority
[4]);
173 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
174 psid
->SubAuthority
[3], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
175 psid
->SubAuthority
[0], psid
->SubAuthority
[4], psid
->SubAuthority
[5]);
177 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
178 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
179 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
180 psid
->SubAuthority
[6]);
182 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
183 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
184 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
185 psid
->SubAuthority
[6], psid
->SubAuthority
[7]);
190 static const ACEFLAG AceRights
[] =
192 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
193 { SDDL_GENERIC_READ
, GENERIC_READ
},
194 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
195 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
197 { SDDL_READ_CONTROL
, READ_CONTROL
},
198 { SDDL_STANDARD_DELETE
, DELETE
},
199 { SDDL_WRITE_DAC
, WRITE_DAC
},
200 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
202 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
203 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
204 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
205 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
206 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
207 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
208 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
209 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
210 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
212 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
213 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
214 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
215 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
217 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
218 { SDDL_KEY_READ
, KEY_READ
},
219 { SDDL_KEY_WRITE
, KEY_WRITE
},
220 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
224 static const LPCWSTR AceRightBitNames
[32] = {
225 SDDL_CREATE_CHILD
, /* 0 */
229 SDDL_READ_PROPERTY
, /* 4 */
233 SDDL_CONTROL_ACCESS
, /* 8 */
241 SDDL_STANDARD_DELETE
, /* 16 */
253 SDDL_GENERIC_ALL
, /* 28 */
254 SDDL_GENERIC_EXECUTE
,
260 /* set last error code from NT status and get the proper boolean return value */
261 /* used for functions that are a simple wrapper around the corresponding ntdll API */
262 static __inline BOOL
set_ntstatus( NTSTATUS status
)
264 if (status
) SetLastError( RtlNtStatusToDosError( status
));
270 IN
const RECORD
* Table
,
272 OUT SIZE_T
* pKeyLength
,
275 const RECORD
* pRecord
= Table
;
276 while (pRecord
->key
!= NULL
)
278 if (wcsncmp(pRecord
->key
, Key
, wcslen(pRecord
->key
)) == 0)
280 *pKeyLength
= wcslen(pRecord
->key
);
281 *pItem
= pRecord
->value
;
286 SetLastError(ERROR_INVALID_PARAMETER
);
296 WCHAR str
[SDDL_ALIAS_SIZE
+ 1];
302 wcsncpy(str
, Buffer
, SDDL_ALIAS_SIZE
);
303 for (i
= SDDL_ALIAS_SIZE
; i
> 0; i
--)
305 str
[i
] = UNICODE_NULL
;
306 if (ConvertStringSidToSidW(str
, pSid
))
313 end
= wcschr(Buffer
, SDDL_ACE_ENDC
);
316 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
319 strSid
= (LPWSTR
)LocalAlloc(0, (end
- Buffer
) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
));
322 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
325 wcsncpy(strSid
, Buffer
, end
- Buffer
+ 1);
326 strSid
[end
- Buffer
] = UNICODE_NULL
;
327 *pLength
= end
- Buffer
;
328 ret
= ConvertStringSidToSidW(strSid
, pSid
);
333 static const RECORD DaclFlagTable
[] =
335 { SDDL_PROTECTED
, SE_DACL_PROTECTED
},
336 { SDDL_AUTO_INHERIT_REQ
, SE_DACL_AUTO_INHERIT_REQ
},
337 { SDDL_AUTO_INHERITED
, SE_DACL_AUTO_INHERITED
},
341 static const RECORD SaclFlagTable
[] =
343 { SDDL_PROTECTED
, SE_SACL_PROTECTED
},
344 { SDDL_AUTO_INHERIT_REQ
, SE_SACL_AUTO_INHERIT_REQ
},
345 { SDDL_AUTO_INHERITED
, SE_SACL_AUTO_INHERITED
},
349 static const RECORD AceFlagTable
[] =
351 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
352 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
353 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
354 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
355 { SDDL_INHERITED
, INHERITED_ACE
},
356 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
357 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
364 IN
const RECORD
* FlagTable
,
369 LPCWSTR ptr
= Buffer
;
370 SIZE_T PartialLength
;
374 while (*ptr
!= LimitChar
)
376 if (!FindKeyInTable(FlagTable
, ptr
, &PartialLength
, &Flag
))
379 ptr
+= PartialLength
;
381 *pLength
= ptr
- Buffer
;
385 static const RECORD AccessMaskTable
[] =
387 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
388 { SDDL_GENERIC_READ
, GENERIC_READ
},
389 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
390 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
391 { SDDL_READ_CONTROL
, READ_CONTROL
},
392 { SDDL_STANDARD_DELETE
, DELETE
},
393 { SDDL_WRITE_DAC
, WRITE_DAC
},
394 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
395 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
396 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
397 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
398 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
399 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
400 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
401 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
402 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
403 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
404 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
405 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
406 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
407 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
408 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
409 { SDDL_KEY_READ
, KEY_READ
},
410 { SDDL_KEY_WRITE
, KEY_WRITE
},
411 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
416 ParseAccessMaskString(
418 OUT DWORD
* pAccessMask
,
421 /* FIXME: Allow hexadecimal string for access rights! */
423 return ParseFlagsString(Buffer
, AccessMaskTable
, SDDL_SEPERATORC
, pAccessMask
, pLength
);
430 OUT BOOL
* pIsGuidValid
,
433 WCHAR GuidStr
[MAX_GUID_STRING_LEN
+ 1];
436 end
= wcschr(Buffer
, SDDL_SEPERATORC
);
439 SetLastError(ERROR_INVALID_PARAMETER
);
443 *pLength
= end
- Buffer
;
444 *pIsGuidValid
= (end
!= Buffer
);
448 if (end
- Buffer
> MAX_GUID_STRING_LEN
- 1)
450 SetLastError(ERROR_INVALID_PARAMETER
);
453 GuidStr
[end
- Buffer
] = UNICODE_NULL
;
454 wcsncpy(GuidStr
, Buffer
, end
- Buffer
);
455 if (RPC_S_OK
!= UuidFromStringW((unsigned short*)&GuidStr
, pGuid
))
457 SetLastError(ERROR_INVALID_PARAMETER
);
463 static const RECORD AceTypeTable
[] =
465 { SDDL_OBJECT_ACCESS_ALLOWED
, ACCESS_ALLOWED_OBJECT_ACE_TYPE
},
466 { SDDL_OBJECT_ACCESS_DENIED
, ACCESS_DENIED_OBJECT_ACE_TYPE
},
467 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
468 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
469 { SDDL_OBJECT_AUDIT
, SYSTEM_AUDIT_OBJECT_ACE_TYPE
},
470 { SDDL_OBJECT_ALARM
, SYSTEM_ALARM_OBJECT_ACE_TYPE
},
471 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
472 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
482 LPCWSTR ptr
= Buffer
;
483 SIZE_T PartialLength
;
484 DWORD aceType
, aceFlags
, accessMask
;
485 GUID object
, inheritObject
;
486 BOOL objectValid
, inheritObjectValid
;
490 if (*ptr
!= SDDL_ACE_BEGINC
)
492 SetLastError(ERROR_INVALID_PARAMETER
);
495 ptr
++; /* Skip SDDL_ACE_BEGINC */
497 if (!FindKeyInTable(AceTypeTable
, ptr
, &PartialLength
, &aceType
))
499 ptr
+= PartialLength
;
501 if (*ptr
!= SDDL_SEPERATORC
)
503 SetLastError(ERROR_INVALID_PARAMETER
);
506 ptr
++; /* Skip SDDL_SEPERATORC */
508 if (!ParseFlagsString(ptr
, AceFlagTable
, SDDL_SEPERATORC
, &aceFlags
, &PartialLength
))
510 ptr
+= PartialLength
+ 1;
512 if (!ParseAccessMaskString(ptr
, &accessMask
, &PartialLength
))
514 ptr
+= PartialLength
+ 1;
516 if (!ParseGuidString(ptr
, &object
, &objectValid
, &PartialLength
))
518 ptr
+= PartialLength
+ 1;
520 if (!ParseGuidString(ptr
, &inheritObject
, &inheritObjectValid
, &PartialLength
))
522 ptr
+= PartialLength
+ 1;
524 if (!ParseSidString(ptr
, &sid
, &PartialLength
))
526 ptr
+= PartialLength
;
527 if (*ptr
!= SDDL_ACE_ENDC
)
529 SetLastError(ERROR_INVALID_PARAMETER
);
532 ptr
++; /* Skip SDDL_ACE_ENDC */
533 *pLength
= ptr
- Buffer
;
537 case ACCESS_ALLOWED_ACE_TYPE
:
538 ret
= AddAccessAllowedAceEx(
545 case ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
546 ret
= AddAccessAllowedObjectAce(
551 objectValid
? &object
: NULL
,
552 inheritObjectValid
? &inheritObject
: NULL
,
555 case ACCESS_DENIED_ACE_TYPE
:
556 ret
= AddAccessDeniedAceEx(
563 case ACCESS_DENIED_OBJECT_ACE_TYPE
:
564 ret
= AddAccessDeniedObjectAce(
569 objectValid
? &object
: NULL
,
570 inheritObjectValid
? &inheritObject
: NULL
,
573 case SYSTEM_AUDIT_ACE_TYPE
:
574 ret
= AddAuditAccessAceEx(
583 case SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
584 ret
= AddAuditAccessObjectAce(
589 objectValid
? &object
: NULL
,
590 inheritObjectValid
? &inheritObject
: NULL
,
595 case SYSTEM_ALARM_ACE_TYPE
:
596 case SYSTEM_ALARM_OBJECT_ACE_TYPE
:
599 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
607 /* Exported functions */
613 AllocateLocallyUniqueId(PLUID Luid
)
617 Status
= NtAllocateLocallyUniqueId (Luid
);
618 if (!NT_SUCCESS (Status
))
620 SetLastError(RtlNtStatusToDosError(Status
));
632 AllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
633 BYTE nSubAuthorityCount
,
634 DWORD dwSubAuthority0
,
635 DWORD dwSubAuthority1
,
636 DWORD dwSubAuthority2
,
637 DWORD dwSubAuthority3
,
638 DWORD dwSubAuthority4
,
639 DWORD dwSubAuthority5
,
640 DWORD dwSubAuthority6
,
641 DWORD dwSubAuthority7
,
646 Status
= RtlAllocateAndInitializeSid(pIdentifierAuthority
,
657 if (!NT_SUCCESS(Status
))
659 SetLastError(RtlNtStatusToDosError(Status
));
671 CopySid(DWORD nDestinationSidLength
,
672 PSID pDestinationSid
,
677 Status
= RtlCopySid(nDestinationSidLength
,
680 if (!NT_SUCCESS (Status
))
682 SetLastError(RtlNtStatusToDosError(Status
));
689 static void DumpString(LPCWSTR string
, int cch
, WCHAR
**pwptr
, ULONG
*plen
)
692 cch
= strlenW(string
);
699 memcpy(*pwptr
, string
, sizeof(WCHAR
)*cch
);
704 static BOOL
DumpSidNumeric(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
707 WCHAR fmt
[] = { 'S','-','%','u','-','%','d',0 };
708 WCHAR subauthfmt
[] = { '-','%','u',0 };
712 if( !IsValidSid( psid
) || pisid
->Revision
!= SDDL_REVISION
)
714 SetLastError(ERROR_INVALID_SID
);
718 if (pisid
->IdentifierAuthority
.Value
[0] ||
719 pisid
->IdentifierAuthority
.Value
[1])
721 FIXME("not matching MS' bugs\n");
722 SetLastError(ERROR_INVALID_SID
);
726 sprintfW( buf
, fmt
, pisid
->Revision
,
728 MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
729 pisid
->IdentifierAuthority
.Value
[4] ),
730 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
731 pisid
->IdentifierAuthority
.Value
[2] )
733 DumpString(buf
, -1, pwptr
, plen
);
735 for( i
=0; i
<pisid
->SubAuthorityCount
; i
++ )
737 sprintfW( buf
, subauthfmt
, pisid
->SubAuthority
[i
] );
738 DumpString(buf
, -1, pwptr
, plen
);
743 static BOOL
DumpSid(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
746 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
748 if (WellKnownSids
[i
].wstr
[0] && EqualSid(psid
, (PSID
)&(WellKnownSids
[i
].Sid
.Revision
)))
750 DumpString(WellKnownSids
[i
].wstr
, 2, pwptr
, plen
);
755 return DumpSidNumeric(psid
, pwptr
, plen
);
758 static void DumpRights(DWORD mask
, WCHAR
**pwptr
, ULONG
*plen
)
760 static const WCHAR fmtW
[] = {'0','x','%','x',0};
767 /* first check if the right have name */
768 for (i
= 0; i
< sizeof(AceRights
)/sizeof(AceRights
[0]); i
++)
770 if (AceRights
[i
].wstr
== NULL
)
772 if (mask
== AceRights
[i
].value
)
774 DumpString(AceRights
[i
].wstr
, -1, pwptr
, plen
);
779 /* then check if it can be built from bit names */
780 for (i
= 0; i
< 32; i
++)
782 if ((mask
& (1 << i
)) && (AceRightBitNames
[i
] == NULL
))
784 /* can't be built from bit names */
785 sprintfW(buf
, fmtW
, mask
);
786 DumpString(buf
, -1, pwptr
, plen
);
791 /* build from bit names */
792 for (i
= 0; i
< 32; i
++)
794 DumpString(AceRightBitNames
[i
], -1, pwptr
, plen
);
797 static BOOL
DumpAce(LPVOID pace
, WCHAR
**pwptr
, ULONG
*plen
)
799 ACCESS_ALLOWED_ACE
*piace
; /* all the supported ACEs have the same memory layout */
800 static const WCHAR openbr
= '(';
801 static const WCHAR closebr
= ')';
802 static const WCHAR semicolon
= ';';
804 if (((PACE_HEADER
)pace
)->AceType
> SYSTEM_ALARM_ACE_TYPE
|| ((PACE_HEADER
)pace
)->AceSize
< sizeof(ACCESS_ALLOWED_ACE
))
806 SetLastError(ERROR_INVALID_ACL
);
810 piace
= (ACCESS_ALLOWED_ACE
*)pace
;
811 DumpString(&openbr
, 1, pwptr
, plen
);
812 switch (piace
->Header
.AceType
)
814 case ACCESS_ALLOWED_ACE_TYPE
:
815 DumpString(SDDL_ACCESS_ALLOWED
, -1, pwptr
, plen
);
817 case ACCESS_DENIED_ACE_TYPE
:
818 DumpString(SDDL_ACCESS_DENIED
, -1, pwptr
, plen
);
820 case SYSTEM_AUDIT_ACE_TYPE
:
821 DumpString(SDDL_AUDIT
, -1, pwptr
, plen
);
823 case SYSTEM_ALARM_ACE_TYPE
:
824 DumpString(SDDL_ALARM
, -1, pwptr
, plen
);
827 DumpString(&semicolon
, 1, pwptr
, plen
);
829 if (piace
->Header
.AceFlags
& OBJECT_INHERIT_ACE
)
830 DumpString(SDDL_OBJECT_INHERIT
, -1, pwptr
, plen
);
831 if (piace
->Header
.AceFlags
& CONTAINER_INHERIT_ACE
)
832 DumpString(SDDL_CONTAINER_INHERIT
, -1, pwptr
, plen
);
833 if (piace
->Header
.AceFlags
& NO_PROPAGATE_INHERIT_ACE
)
834 DumpString(SDDL_NO_PROPAGATE
, -1, pwptr
, plen
);
835 if (piace
->Header
.AceFlags
& INHERIT_ONLY_ACE
)
836 DumpString(SDDL_INHERIT_ONLY
, -1, pwptr
, plen
);
837 if (piace
->Header
.AceFlags
& INHERITED_ACE
)
838 DumpString(SDDL_INHERITED
, -1, pwptr
, plen
);
839 if (piace
->Header
.AceFlags
& SUCCESSFUL_ACCESS_ACE_FLAG
)
840 DumpString(SDDL_AUDIT_SUCCESS
, -1, pwptr
, plen
);
841 if (piace
->Header
.AceFlags
& FAILED_ACCESS_ACE_FLAG
)
842 DumpString(SDDL_AUDIT_FAILURE
, -1, pwptr
, plen
);
843 DumpString(&semicolon
, 1, pwptr
, plen
);
844 DumpRights(piace
->Mask
, pwptr
, plen
);
845 DumpString(&semicolon
, 1, pwptr
, plen
);
846 /* objects not supported */
847 DumpString(&semicolon
, 1, pwptr
, plen
);
848 /* objects not supported */
849 DumpString(&semicolon
, 1, pwptr
, plen
);
850 if (!DumpSid((PSID
)&piace
->SidStart
, pwptr
, plen
))
852 DumpString(&closebr
, 1, pwptr
, plen
);
856 static BOOL
DumpAcl(PACL pacl
, WCHAR
**pwptr
, ULONG
*plen
, BOOL
protected, BOOL autoInheritReq
, BOOL autoInherited
)
862 DumpString(SDDL_PROTECTED
, -1, pwptr
, plen
);
864 DumpString(SDDL_AUTO_INHERIT_REQ
, -1, pwptr
, plen
);
866 DumpString(SDDL_AUTO_INHERITED
, -1, pwptr
, plen
);
871 if (!IsValidAcl(pacl
))
874 count
= pacl
->AceCount
;
875 for (i
= 0; i
< count
; i
++)
878 if (!GetAce(pacl
, i
, &ace
))
880 if (!DumpAce(ace
, pwptr
, plen
))
887 static BOOL
DumpOwner(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
889 static const WCHAR prefix
[] = {'O',':',0};
893 if (!GetSecurityDescriptorOwner(SecurityDescriptor
, &psid
, &bDefaulted
))
899 DumpString(prefix
, -1, pwptr
, plen
);
900 if (!DumpSid(psid
, pwptr
, plen
))
905 static BOOL
DumpGroup(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
907 static const WCHAR prefix
[] = {'G',':',0};
911 if (!GetSecurityDescriptorGroup(SecurityDescriptor
, &psid
, &bDefaulted
))
917 DumpString(prefix
, -1, pwptr
, plen
);
918 if (!DumpSid(psid
, pwptr
, plen
))
923 static BOOL
DumpDacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
925 static const WCHAR dacl
[] = {'D',':',0};
926 SECURITY_DESCRIPTOR_CONTROL control
;
927 BOOL present
, defaulted
;
931 if (!GetSecurityDescriptorDacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
934 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
940 DumpString(dacl
, 2, pwptr
, plen
);
941 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_DACL_PROTECTED
, control
& SE_DACL_AUTO_INHERIT_REQ
, control
& SE_DACL_AUTO_INHERITED
))
946 static BOOL
DumpSacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
948 static const WCHAR sacl
[] = {'S',':',0};
949 SECURITY_DESCRIPTOR_CONTROL control
;
950 BOOL present
, defaulted
;
954 if (!GetSecurityDescriptorSacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
957 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
963 DumpString(sacl
, 2, pwptr
, plen
);
964 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_SACL_PROTECTED
, control
& SE_SACL_AUTO_INHERIT_REQ
, control
& SE_SACL_AUTO_INHERITED
))
969 /******************************************************************************
970 * ConvertSecurityDescriptorToStringSecurityDescriptorW [ADVAPI32.@]
974 ConvertSecurityDescriptorToStringSecurityDescriptorW(PSECURITY_DESCRIPTOR SecurityDescriptor
,
976 SECURITY_INFORMATION SecurityInformation
,
977 LPWSTR
*OutputString
,
983 if (SDRevision
!= SDDL_REVISION_1
)
985 ERR("Pogram requested unknown SDDL revision %d\n", SDRevision
);
986 SetLastError(ERROR_UNKNOWN_REVISION
);
991 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
992 if (!DumpOwner(SecurityDescriptor
, NULL
, &len
))
994 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
995 if (!DumpGroup(SecurityDescriptor
, NULL
, &len
))
997 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
998 if (!DumpDacl(SecurityDescriptor
, NULL
, &len
))
1000 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
1001 if (!DumpSacl(SecurityDescriptor
, NULL
, &len
))
1004 wstr
= wptr
= LocalAlloc(0, (len
+ 1)*sizeof(WCHAR
));
1005 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
1006 if (!DumpOwner(SecurityDescriptor
, &wptr
, NULL
))
1008 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
1009 if (!DumpGroup(SecurityDescriptor
, &wptr
, NULL
))
1011 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
1012 if (!DumpDacl(SecurityDescriptor
, &wptr
, NULL
))
1014 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
1015 if (!DumpSacl(SecurityDescriptor
, &wptr
, NULL
))
1019 TRACE("ret: %s, %d\n", wine_dbgstr_w(wstr
), len
);
1020 *OutputString
= wstr
;
1022 *OutputLen
= strlenW(*OutputString
)+1;
1027 /******************************************************************************
1028 * ConvertSecurityDescriptorToStringSecurityDescriptorA [ADVAPI32.@]
1032 ConvertSecurityDescriptorToStringSecurityDescriptorA(PSECURITY_DESCRIPTOR SecurityDescriptor
,
1034 SECURITY_INFORMATION Information
,
1035 LPSTR
*OutputString
,
1040 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
1044 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
1045 *OutputString
= HeapAlloc(GetProcessHeap(), 0, lenA
);
1046 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
1049 if (OutputLen
!= NULL
)
1055 *OutputString
= NULL
;
1062 /******************************************************************************
1063 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
1067 ConvertStringSecurityDescriptorToSecurityDescriptorW(
1068 IN LPCWSTR StringSecurityDescriptor
,
1069 IN DWORD StringSDRevision
,
1070 OUT PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1071 OUT PULONG SecurityDescriptorSize
)
1073 PSECURITY_DESCRIPTOR sd
= NULL
;
1076 if (!StringSecurityDescriptor
)
1077 SetLastError(ERROR_INVALID_PARAMETER
);
1078 else if (StringSDRevision
!= SDDL_REVISION_1
)
1079 SetLastError(ERROR_INVALID_PARAMETER
);
1082 LPCWSTR ptr
= StringSecurityDescriptor
;
1083 DWORD numberOfAces
= 0;
1084 DWORD relativeSdSize
;
1086 PSECURITY_DESCRIPTOR relativeSd
= NULL
;
1089 BOOL present
, dummy
;
1090 /* An easy way to know how much space we need for an ACL is to count
1091 * the number of ACEs and say that we have 1 SID by ACE
1093 ptr
= wcschr(StringSecurityDescriptor
, SDDL_ACE_BEGINC
);
1097 ptr
= wcschr(ptr
+ 1, SDDL_ACE_BEGINC
);
1099 MaxAclSize
= sizeof(ACL
) + numberOfAces
*
1100 (sizeof(ACCESS_ALLOWED_OBJECT_ACE
) + SECURITY_MAX_SID_SIZE
);
1102 sd
= (SECURITY_DESCRIPTOR
*)LocalAlloc(0, sizeof(SECURITY_DESCRIPTOR
));
1105 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1108 ret
= InitializeSecurityDescriptor(sd
, SECURITY_DESCRIPTOR_REVISION
);
1112 /* Now, really parse the string */
1113 ptr
= StringSecurityDescriptor
;
1116 if (ptr
[1] != SDDL_DELIMINATORC
)
1118 SetLastError(ERROR_INVALID_PARAMETER
);
1131 ret
= ParseSidString(ptr
, &pSid
, &Length
);
1135 ret
= SetSecurityDescriptorOwner(sd
, pSid
, FALSE
);
1137 ret
= SetSecurityDescriptorGroup(sd
, pSid
, FALSE
);
1151 BOOL isDacl
= (ptr
[-2] == 'D');
1154 ret
= ParseFlagsString(ptr
, DaclFlagTable
, SDDL_ACE_BEGINC
, &aclFlags
, &Length
);
1156 ret
= ParseFlagsString(ptr
, SaclFlagTable
, SDDL_ACE_BEGINC
, &aclFlags
, &Length
);
1159 pAcl
= (PACL
)LocalAlloc(0, MaxAclSize
);
1162 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1166 if (!InitializeAcl(pAcl
, (DWORD
)MaxAclSize
, ACL_REVISION_DS
))
1173 ret
= SetSecurityDescriptorControl(
1175 (SECURITY_DESCRIPTOR_CONTROL
)aclFlags
,
1176 (SECURITY_DESCRIPTOR_CONTROL
)aclFlags
);
1184 while (*ptr
== SDDL_ACE_BEGINC
)
1186 ret
= ParseAceString(ptr
, pAcl
, &Length
);
1195 ret
= SetSecurityDescriptorDacl(sd
, TRUE
, pAcl
, FALSE
);
1197 ret
= SetSecurityDescriptorSacl(sd
, TRUE
, pAcl
, FALSE
);
1207 SetLastError(ERROR_INVALID_PARAMETER
);
1218 LocalFree(relativeSd
);
1219 relativeSd
= LocalAlloc(0, relativeSdSize
);
1222 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1225 ret
= MakeSelfRelativeSD(sd
, relativeSd
, &relativeSdSize
);
1226 if (ret
|| GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1229 if (SecurityDescriptorSize
)
1230 *SecurityDescriptorSize
= relativeSdSize
;
1231 *SecurityDescriptor
= relativeSd
;
1234 if (GetSecurityDescriptorOwner(sd
, &pSid
, &dummy
))
1236 if (GetSecurityDescriptorGroup(sd
, &pSid
, &dummy
))
1238 if (GetSecurityDescriptorDacl(sd
, &present
, &pAcl
, &dummy
) && present
)
1240 if (GetSecurityDescriptorSacl(sd
, &present
, &pAcl
, &dummy
) && present
)
1248 /* Winehq cvs 20050916 */
1249 /******************************************************************************
1250 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
1253 BOOL WINAPI
ConvertStringSecurityDescriptorToSecurityDescriptorA(
1254 LPCSTR StringSecurityDescriptor
,
1255 DWORD StringSDRevision
,
1256 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1257 PULONG SecurityDescriptorSize
)
1261 LPWSTR StringSecurityDescriptorW
;
1263 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
1264 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1266 if (StringSecurityDescriptorW
)
1268 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
1270 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
1271 StringSDRevision
, SecurityDescriptor
,
1272 SecurityDescriptorSize
);
1273 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
1283 EqualPrefixSid(PSID pSid1
,
1286 return RtlEqualPrefixSid (pSid1
, pSid2
);
1294 EqualSid(PSID pSid1
,
1297 return RtlEqualSid (pSid1
, pSid2
);
1305 * Docs says this function does NOT return a value
1306 * even thou it's defined to return a PVOID...
1311 return RtlFreeSid(pSid
);
1319 GetLengthSid(PSID pSid
)
1321 return (DWORD
)RtlLengthSid(pSid
);
1328 PSID_IDENTIFIER_AUTHORITY STDCALL
1329 GetSidIdentifierAuthority(PSID pSid
)
1331 return RtlIdentifierAuthoritySid(pSid
);
1339 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
1341 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
1349 GetSidSubAuthority(PSID pSid
,
1350 DWORD nSubAuthority
)
1352 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
1360 GetSidSubAuthorityCount(PSID pSid
)
1362 return RtlSubAuthorityCountSid(pSid
);
1370 InitializeSid(PSID Sid
,
1371 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
1372 BYTE nSubAuthorityCount
)
1376 Status
= RtlInitializeSid(Sid
,
1377 pIdentifierAuthority
,
1378 nSubAuthorityCount
);
1379 if (!NT_SUCCESS(Status
))
1381 SetLastError(RtlNtStatusToDosError(Status
));
1393 IsValidSid(PSID pSid
)
1395 return (BOOL
)RtlValidSid(pSid
);
1403 ConvertSidToStringSidW(PSID Sid
,
1407 UNICODE_STRING UnicodeString
;
1408 WCHAR FixedBuffer
[64];
1410 if (!RtlValidSid(Sid
))
1412 SetLastError(ERROR_INVALID_SID
);
1416 UnicodeString
.Length
= 0;
1417 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
1418 UnicodeString
.Buffer
= FixedBuffer
;
1419 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
1420 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1422 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
1425 if (!NT_SUCCESS(Status
))
1427 SetLastError(RtlNtStatusToDosError(Status
));
1431 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
1432 if (NULL
== *StringSid
)
1434 if (UnicodeString
.Buffer
!= FixedBuffer
)
1436 RtlFreeUnicodeString(&UnicodeString
);
1438 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1442 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
1443 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
1444 if (UnicodeString
.Buffer
!= FixedBuffer
)
1446 RtlFreeUnicodeString(&UnicodeString
);
1457 ConvertSidToStringSidA(PSID Sid
,
1463 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
1468 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
1471 LocalFree(StringSidW
);
1472 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1476 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
1477 if (NULL
== *StringSid
)
1479 LocalFree(StringSidW
);
1480 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1484 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
1486 LocalFree(StringSid
);
1487 LocalFree(StringSidW
);
1491 LocalFree(StringSidW
);
1501 EqualDomainSid(IN PSID pSid1
,
1514 GetWindowsAccountDomainSid(IN PSID pSid
,
1515 OUT PSID ppDomainSid
,
1516 IN OUT DWORD
* cbSid
)
1527 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
1528 IN PSID DomainSid OPTIONAL
,
1530 IN OUT DWORD
* cbSid
)
1533 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
1535 if (DomainSid
!= NULL
)
1537 FIXME("Only local computer supported!\n");
1538 SetLastError(ERROR_INVALID_PARAMETER
); /* FIXME */
1542 if (cbSid
== NULL
|| pSid
== NULL
)
1544 SetLastError(ERROR_INVALID_PARAMETER
);
1548 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
1550 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
1552 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
1554 if (*cbSid
< length
)
1556 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1560 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
1566 SetLastError(ERROR_INVALID_PARAMETER
);
1575 IsWellKnownSid(IN PSID pSid
,
1576 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
1579 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
1581 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
1583 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
1585 if (EqualSid(pSid
, (PSID
)((ULONG_PTR
)&WellKnownSids
[i
].Sid
.Revision
)))
1598 ConvertStringSidToSidA(IN LPCSTR StringSid
,
1601 BOOL bRetVal
= FALSE
;
1603 if (!StringSid
|| !sid
)
1604 SetLastError(ERROR_INVALID_PARAMETER
);
1607 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
1608 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1609 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
1610 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
1611 HeapFree(GetProcessHeap(), 0, wStringSid
);
1617 /******************************************************************************
1618 * ComputeStringSidSize
1621 ComputeStringSidSize(LPCWSTR StringSid
)
1623 DWORD size
= sizeof(SID
);
1625 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
1630 if (*StringSid
== '-')
1636 size
+= (ctok
- 3) * sizeof(DWORD
);
1638 else /* String constant format - Only available in winxp and above */
1642 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
1643 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
1644 size
+= (WellKnownSids
[i
].Sid
.SubAuthorityCount
- 1) * sizeof(DWORD
);
1650 static const RECORD SidTable
[] =
1652 { SDDL_ACCOUNT_OPERATORS
, WinBuiltinAccountOperatorsSid
},
1653 { SDDL_ALIAS_PREW2KCOMPACC
, WinBuiltinPreWindows2000CompatibleAccessSid
},
1654 { SDDL_ANONYMOUS
, WinAnonymousSid
},
1655 { SDDL_AUTHENTICATED_USERS
, WinAuthenticatedUserSid
},
1656 { SDDL_BUILTIN_ADMINISTRATORS
, WinBuiltinAdministratorsSid
},
1657 { SDDL_BUILTIN_GUESTS
, WinBuiltinGuestsSid
},
1658 { SDDL_BACKUP_OPERATORS
, WinBuiltinBackupOperatorsSid
},
1659 { SDDL_BUILTIN_USERS
, WinBuiltinUsersSid
},
1660 { SDDL_CERT_SERV_ADMINISTRATORS
, WinAccountCertAdminsSid
/* FIXME: DOMAIN_GROUP_RID_CERT_ADMINS */ },
1661 { SDDL_CREATOR_GROUP
, WinCreatorGroupSid
},
1662 { SDDL_CREATOR_OWNER
, WinCreatorOwnerSid
},
1663 { SDDL_DOMAIN_ADMINISTRATORS
, WinAccountDomainAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ADMINS */ },
1664 { SDDL_DOMAIN_COMPUTERS
, WinAccountComputersSid
/* FIXME: DOMAIN_GROUP_RID_COMPUTERS */ },
1665 { SDDL_DOMAIN_DOMAIN_CONTROLLERS
, WinAccountControllersSid
/* FIXME: DOMAIN_GROUP_RID_CONTROLLERS */ },
1666 { SDDL_DOMAIN_GUESTS
, WinAccountDomainGuestsSid
/* FIXME: DOMAIN_GROUP_RID_GUESTS */ },
1667 { SDDL_DOMAIN_USERS
, WinAccountDomainUsersSid
/* FIXME: DOMAIN_GROUP_RID_USERS */ },
1668 { SDDL_ENTERPRISE_ADMINS
, WinAccountEnterpriseAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ENTERPRISE_ADMINS */ },
1669 { SDDL_ENTERPRISE_DOMAIN_CONTROLLERS
, WinLogonIdsSid
/* FIXME: SECURITY_SERVER_LOGON_RID */ },
1670 { SDDL_EVERYONE
, WinWorldSid
},
1671 { SDDL_GROUP_POLICY_ADMINS
, WinAccountPolicyAdminsSid
/* FIXME: DOMAIN_GROUP_RID_POLICY_ADMINS */ },
1672 { SDDL_INTERACTIVE
, WinInteractiveSid
},
1673 { SDDL_LOCAL_ADMIN
, WinAccountAdministratorSid
/* FIXME: DOMAIN_USER_RID_ADMIN */ },
1674 { SDDL_LOCAL_GUEST
, WinAccountGuestSid
/* FIXME: DOMAIN_USER_RID_GUEST */ },
1675 { SDDL_LOCAL_SERVICE
, WinLocalServiceSid
},
1676 { SDDL_LOCAL_SYSTEM
, WinLocalSystemSid
},
1677 { SDDL_NETWORK
, WinNetworkSid
},
1678 { SDDL_NETWORK_CONFIGURATION_OPS
, WinBuiltinNetworkConfigurationOperatorsSid
},
1679 { SDDL_NETWORK_SERVICE
, WinNetworkServiceSid
},
1680 { SDDL_PRINTER_OPERATORS
, WinBuiltinPrintOperatorsSid
},
1681 { SDDL_PERSONAL_SELF
, WinSelfSid
},
1682 { SDDL_POWER_USERS
, WinBuiltinPowerUsersSid
},
1683 { SDDL_RAS_SERVERS
, WinAccountRasAndIasServersSid
/* FIXME: DOMAIN_ALIAS_RID_RAS_SERVERS */ },
1684 { SDDL_REMOTE_DESKTOP
, WinBuiltinRemoteDesktopUsersSid
},
1685 { SDDL_REPLICATOR
, WinBuiltinReplicatorSid
},
1686 { SDDL_RESTRICTED_CODE
, WinRestrictedCodeSid
},
1687 { SDDL_SCHEMA_ADMINISTRATORS
, WinAccountSchemaAdminsSid
/* FIXME: DOMAIN_GROUP_RID_SCHEMA_ADMINS */ },
1688 { SDDL_SERVER_OPERATORS
, WinBuiltinSystemOperatorsSid
},
1689 { SDDL_SERVICE
, WinServiceSid
},
1697 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
1701 DWORD i
, cBytes
, identAuth
, csubauth
;
1705 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
1709 SetLastError(ERROR_INVALID_SID
);
1712 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
1714 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
1716 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
1717 size
= SECURITY_MAX_SID_SIZE
;
1718 *sid
= LocalAlloc(0, size
);
1721 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1724 ret
= CreateWellKnownSid(
1731 SetLastError(ERROR_INVALID_SID
);
1738 /* That's probably a string S-R-I-S-S... */
1739 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
1741 SetLastError(ERROR_INVALID_SID
);
1745 cBytes
= ComputeStringSidSize(StringSid
);
1746 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
1749 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1754 csubauth
= ((cBytes
- sizeof(SID
)) / sizeof(DWORD
)) + 1;
1756 StringSid
+= 2; /* Advance to Revision */
1757 pisid
->Revision
= atoiW(StringSid
);
1759 if (pisid
->Revision
!= SDDL_REVISION
)
1761 TRACE("Revision %d is unknown\n", pisid
->Revision
);
1762 goto lend
; /* ERROR_INVALID_SID */
1766 TRACE("SubAuthorityCount is 0\n");
1767 goto lend
; /* ERROR_INVALID_SID */
1770 pisid
->SubAuthorityCount
= csubauth
;
1772 /* Advance to identifier authority */
1773 while (*StringSid
&& *StringSid
!= '-')
1775 if (*StringSid
== '-')
1778 /* MS' implementation can't handle values greater than 2^32 - 1, so
1779 * we don't either; assume most significant bytes are always 0
1781 pisid
->IdentifierAuthority
.Value
[0] = 0;
1782 pisid
->IdentifierAuthority
.Value
[1] = 0;
1783 identAuth
= atoiW(StringSid
);
1784 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
1785 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
1786 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
1787 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
1789 /* Advance to first sub authority */
1790 while (*StringSid
&& *StringSid
!= '-')
1792 if (*StringSid
== '-')
1797 while (*StringSid
&& *StringSid
!= '-')
1799 if (*StringSid
== '-')
1802 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
1805 if (i
!= pisid
->SubAuthorityCount
)
1806 goto lend
; /* ERROR_INVALID_SID */
1813 SetLastError(ERROR_INVALID_SID
);
1815 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");