2 * COPYRIGHT: See COPYING in the top level directory
4 * Copyright 1999, 2000 Juergen Schmied <juergen.schmied@debitel.net>
5 * Copyright 2003 CodeWeavers Inc. (Ulrich Czekalla)
6 * Copyright 2006 Robert Reif
7 * Copyright 2006 Hervé Poussineau
9 * PROJECT: ReactOS system libraries
10 * FILE: dll/win32/advapi32/sec/sid.c
11 * PURPOSE: Security ID functions
16 #include <wine/debug.h>
17 #include <wine/unicode.h>
19 WINE_DEFAULT_DEBUG_CHANNEL(advapi
);
21 #define MAX_GUID_STRING_LEN 39
24 AddAuditAccessAceEx(PACL pAcl
,
39 typedef struct _MAX_SID
41 /* same fields as struct _SID */
43 BYTE SubAuthorityCount
;
44 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
45 DWORD SubAuthority
[SID_MAX_SUB_AUTHORITIES
];
48 typedef struct WELLKNOWNSID
51 WELL_KNOWN_SID_TYPE Type
;
55 typedef struct _ACEFLAG
59 } ACEFLAG
, *LPACEFLAG
;
61 static const WELLKNOWNSID WellKnownSids
[] =
63 { {0,0}, WinNullSid
, { SID_REVISION
, 1, { SECURITY_NULL_SID_AUTHORITY
}, { SECURITY_NULL_RID
} } },
64 { {'W','D'}, WinWorldSid
, { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} } },
65 { {0,0}, WinLocalSid
, { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} } },
66 { {'C','O'}, WinCreatorOwnerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_RID
} } },
67 { {'C','G'}, WinCreatorGroupSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_RID
} } },
68 { {0,0}, WinCreatorOwnerServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_OWNER_SERVER_RID
} } },
69 { {0,0}, WinCreatorGroupServerSid
, { SID_REVISION
, 1, { SECURITY_CREATOR_SID_AUTHORITY
}, { SECURITY_CREATOR_GROUP_SERVER_RID
} } },
70 { {0,0}, WinNtAuthoritySid
, { SID_REVISION
, 0, { SECURITY_NT_AUTHORITY
}, { SECURITY_NULL_RID
} } },
71 { {0,0}, WinDialupSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_DIALUP_RID
} } },
72 { {'N','U'}, WinNetworkSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_RID
} } },
73 { {0,0}, WinBatchSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BATCH_RID
} } },
74 { {'I','U'}, WinInteractiveSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} } },
75 { {'S','U'}, WinServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_SERVICE_RID
} } },
76 { {'A','N'}, WinAnonymousSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} } },
77 { {0,0}, WinProxySid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PROXY_RID
} } },
78 { {'E','D'}, WinEnterpriseControllersSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ENTERPRISE_CONTROLLERS_RID
} } },
79 { {'P','S'}, WinSelfSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_PRINCIPAL_SELF_RID
} } },
80 { {'A','U'}, WinAuthenticatedUserSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} } },
81 { {'R','C'}, WinRestrictedCodeSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_RESTRICTED_CODE_RID
} } },
82 { {0,0}, WinTerminalServerSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_TERMINAL_SERVER_RID
} } },
83 { {0,0}, WinRemoteLogonIdSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_REMOTE_LOGON_RID
} } },
84 { {'S','Y'}, WinLocalSystemSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} } },
85 { {'L','S'}, WinLocalServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SERVICE_RID
} } },
86 { {'N','S'}, WinNetworkServiceSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_NETWORK_SERVICE_RID
} } },
87 { {0,0}, WinBuiltinDomainSid
, { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
} } },
88 { {'B','A'}, WinBuiltinAdministratorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
} } },
89 { {'B','U'}, WinBuiltinUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
} } },
90 { {'B','G'}, WinBuiltinGuestsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_GUESTS
} } },
91 { {'P','U'}, WinBuiltinPowerUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_POWER_USERS
} } },
92 { {'A','O'}, WinBuiltinAccountOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ACCOUNT_OPS
} } },
93 { {'S','O'}, WinBuiltinSystemOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_SYSTEM_OPS
} } },
94 { {'P','O'}, WinBuiltinPrintOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PRINT_OPS
} } },
95 { {'B','O'}, WinBuiltinBackupOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_BACKUP_OPS
} } },
96 { {'R','E'}, WinBuiltinReplicatorSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REPLICATOR
} } },
97 { {'R','U'}, WinBuiltinPreWindows2000CompatibleAccessSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_PREW2KCOMPACCESS
} } },
98 { {'R','D'}, WinBuiltinRemoteDesktopUsersSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS
} } },
99 { {'N','O'}, WinBuiltinNetworkConfigurationOperatorsSid
, { SID_REVISION
, 2, { SECURITY_NT_AUTHORITY
}, { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS
} } },
102 static const SID sidWorld
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
} , { SECURITY_WORLD_RID
} };
107 static const WCHAR SDDL_ACCESS_ALLOWED
[] = {'A',0};
108 static const WCHAR SDDL_ACCESS_DENIED
[] = {'D',0};
109 static const WCHAR SDDL_OBJECT_ACCESS_ALLOWED
[] = {'O','A',0};
110 static const WCHAR SDDL_OBJECT_ACCESS_DENIED
[] = {'O','D',0};
111 static const WCHAR SDDL_AUDIT
[] = {'A','U',0};
112 static const WCHAR SDDL_ALARM
[] = {'A','L',0};
113 static const WCHAR SDDL_OBJECT_AUDIT
[] = {'O','U',0};
114 static const WCHAR SDDL_OBJECT_ALARM
[] = {'O','L',0};
119 #define ADS_RIGHT_DS_CREATE_CHILD 0x0001
120 #define ADS_RIGHT_DS_DELETE_CHILD 0x0002
121 #define ADS_RIGHT_ACTRL_DS_LIST 0x0004
122 #define ADS_RIGHT_DS_SELF 0x0008
123 #define ADS_RIGHT_DS_READ_PROP 0x0010
124 #define ADS_RIGHT_DS_WRITE_PROP 0x0020
125 #define ADS_RIGHT_DS_DELETE_TREE 0x0040
126 #define ADS_RIGHT_DS_LIST_OBJECT 0x0080
127 #define ADS_RIGHT_DS_CONTROL_ACCESS 0x0100
132 static const WCHAR SDDL_CONTAINER_INHERIT
[] = {'C','I',0};
133 static const WCHAR SDDL_OBJECT_INHERIT
[] = {'O','I',0};
134 static const WCHAR SDDL_NO_PROPAGATE
[] = {'N','P',0};
135 static const WCHAR SDDL_INHERIT_ONLY
[] = {'I','O',0};
136 static const WCHAR SDDL_INHERITED
[] = {'I','D',0};
137 static const WCHAR SDDL_AUDIT_SUCCESS
[] = {'S','A',0};
138 static const WCHAR SDDL_AUDIT_FAILURE
[] = {'F','A',0};
140 static const char * debugstr_sid(PSID sid
)
143 SID
* psid
= (SID
*)sid
;
148 auth
= psid
->IdentifierAuthority
.Value
[5] +
149 (psid
->IdentifierAuthority
.Value
[4] << 8) +
150 (psid
->IdentifierAuthority
.Value
[3] << 16) +
151 (psid
->IdentifierAuthority
.Value
[2] << 24);
153 switch (psid
->SubAuthorityCount
) {
155 return wine_dbg_sprintf("S-%d-%d", psid
->Revision
, auth
);
157 return wine_dbg_sprintf("S-%d-%d-%lu", psid
->Revision
, auth
,
158 psid
->SubAuthority
[0]);
160 return wine_dbg_sprintf("S-%d-%d-%lu-%lu", psid
->Revision
, auth
,
161 psid
->SubAuthority
[0], psid
->SubAuthority
[1]);
163 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu", psid
->Revision
, auth
,
164 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2]);
166 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
167 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
168 psid
->SubAuthority
[3]);
170 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
171 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
172 psid
->SubAuthority
[3], psid
->SubAuthority
[4]);
174 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
175 psid
->SubAuthority
[3], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
176 psid
->SubAuthority
[0], psid
->SubAuthority
[4], psid
->SubAuthority
[5]);
178 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
179 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
180 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
181 psid
->SubAuthority
[6]);
183 return wine_dbg_sprintf("S-%d-%d-%lu-%lu-%lu-%lu-%lu-%lu-%lu-%lu", psid
->Revision
, auth
,
184 psid
->SubAuthority
[0], psid
->SubAuthority
[1], psid
->SubAuthority
[2],
185 psid
->SubAuthority
[3], psid
->SubAuthority
[4], psid
->SubAuthority
[5],
186 psid
->SubAuthority
[6], psid
->SubAuthority
[7]);
191 static const ACEFLAG AceRights
[] =
193 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
194 { SDDL_GENERIC_READ
, GENERIC_READ
},
195 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
196 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
198 { SDDL_READ_CONTROL
, READ_CONTROL
},
199 { SDDL_STANDARD_DELETE
, DELETE
},
200 { SDDL_WRITE_DAC
, WRITE_DAC
},
201 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
203 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
204 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
205 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
206 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
207 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
208 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
209 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
210 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
211 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
213 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
214 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
215 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
216 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
218 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
219 { SDDL_KEY_READ
, KEY_READ
},
220 { SDDL_KEY_WRITE
, KEY_WRITE
},
221 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
225 static const LPCWSTR AceRightBitNames
[32] = {
226 SDDL_CREATE_CHILD
, /* 0 */
230 SDDL_READ_PROPERTY
, /* 4 */
234 SDDL_CONTROL_ACCESS
, /* 8 */
242 SDDL_STANDARD_DELETE
, /* 16 */
254 SDDL_GENERIC_ALL
, /* 28 */
255 SDDL_GENERIC_EXECUTE
,
261 /* set last error code from NT status and get the proper boolean return value */
262 /* used for functions that are a simple wrapper around the corresponding ntdll API */
263 static __inline BOOL
set_ntstatus( NTSTATUS status
)
265 if (status
) SetLastError( RtlNtStatusToDosError( status
));
271 IN
const RECORD
* Table
,
273 OUT SIZE_T
* pKeyLength
,
276 const RECORD
* pRecord
= Table
;
277 while (pRecord
->key
!= NULL
)
279 if (wcsncmp(pRecord
->key
, Key
, wcslen(pRecord
->key
)) == 0)
281 *pKeyLength
= wcslen(pRecord
->key
);
282 *pItem
= pRecord
->value
;
287 SetLastError(ERROR_INVALID_PARAMETER
);
297 WCHAR str
[SDDL_ALIAS_SIZE
+ 1];
303 wcsncpy(str
, Buffer
, SDDL_ALIAS_SIZE
);
304 for (i
= SDDL_ALIAS_SIZE
; i
> 0; i
--)
306 str
[i
] = UNICODE_NULL
;
307 if (ConvertStringSidToSidW(str
, pSid
))
314 end
= wcschr(Buffer
, SDDL_ACE_ENDC
);
317 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
320 strSid
= (LPWSTR
)LocalAlloc(0, (end
- Buffer
) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
));
323 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
326 wcsncpy(strSid
, Buffer
, end
- Buffer
+ 1);
327 strSid
[end
- Buffer
] = UNICODE_NULL
;
328 *pLength
= end
- Buffer
;
329 ret
= ConvertStringSidToSidW(strSid
, pSid
);
334 static const RECORD DaclFlagTable
[] =
336 { SDDL_PROTECTED
, SE_DACL_PROTECTED
},
337 { SDDL_AUTO_INHERIT_REQ
, SE_DACL_AUTO_INHERIT_REQ
},
338 { SDDL_AUTO_INHERITED
, SE_DACL_AUTO_INHERITED
},
342 static const RECORD SaclFlagTable
[] =
344 { SDDL_PROTECTED
, SE_SACL_PROTECTED
},
345 { SDDL_AUTO_INHERIT_REQ
, SE_SACL_AUTO_INHERIT_REQ
},
346 { SDDL_AUTO_INHERITED
, SE_SACL_AUTO_INHERITED
},
350 static const RECORD AceFlagTable
[] =
352 { SDDL_CONTAINER_INHERIT
, CONTAINER_INHERIT_ACE
},
353 { SDDL_OBJECT_INHERIT
, OBJECT_INHERIT_ACE
},
354 { SDDL_NO_PROPAGATE
, NO_PROPAGATE_INHERIT_ACE
},
355 { SDDL_INHERIT_ONLY
, INHERIT_ONLY_ACE
},
356 { SDDL_INHERITED
, INHERITED_ACE
},
357 { SDDL_AUDIT_SUCCESS
, SUCCESSFUL_ACCESS_ACE_FLAG
},
358 { SDDL_AUDIT_FAILURE
, FAILED_ACCESS_ACE_FLAG
},
365 IN
const RECORD
* FlagTable
,
370 LPCWSTR ptr
= Buffer
;
371 SIZE_T PartialLength
;
375 while (*ptr
!= LimitChar
)
377 if (!FindKeyInTable(FlagTable
, ptr
, &PartialLength
, &Flag
))
380 ptr
+= PartialLength
;
382 *pLength
= ptr
- Buffer
;
386 static const RECORD AccessMaskTable
[] =
388 { SDDL_GENERIC_ALL
, GENERIC_ALL
},
389 { SDDL_GENERIC_READ
, GENERIC_READ
},
390 { SDDL_GENERIC_WRITE
, GENERIC_WRITE
},
391 { SDDL_GENERIC_EXECUTE
, GENERIC_EXECUTE
},
392 { SDDL_READ_CONTROL
, READ_CONTROL
},
393 { SDDL_STANDARD_DELETE
, DELETE
},
394 { SDDL_WRITE_DAC
, WRITE_DAC
},
395 { SDDL_WRITE_OWNER
, WRITE_OWNER
},
396 { SDDL_READ_PROPERTY
, ADS_RIGHT_DS_READ_PROP
},
397 { SDDL_WRITE_PROPERTY
, ADS_RIGHT_DS_WRITE_PROP
},
398 { SDDL_CREATE_CHILD
, ADS_RIGHT_DS_CREATE_CHILD
},
399 { SDDL_DELETE_CHILD
, ADS_RIGHT_DS_DELETE_CHILD
},
400 { SDDL_LIST_CHILDREN
, ADS_RIGHT_ACTRL_DS_LIST
},
401 { SDDL_SELF_WRITE
, ADS_RIGHT_DS_SELF
},
402 { SDDL_LIST_OBJECT
, ADS_RIGHT_DS_LIST_OBJECT
},
403 { SDDL_DELETE_TREE
, ADS_RIGHT_DS_DELETE_TREE
},
404 { SDDL_CONTROL_ACCESS
, ADS_RIGHT_DS_CONTROL_ACCESS
},
405 { SDDL_FILE_ALL
, FILE_ALL_ACCESS
},
406 { SDDL_FILE_READ
, FILE_GENERIC_READ
},
407 { SDDL_FILE_WRITE
, FILE_GENERIC_WRITE
},
408 { SDDL_FILE_EXECUTE
, FILE_GENERIC_EXECUTE
},
409 { SDDL_KEY_ALL
, KEY_ALL_ACCESS
},
410 { SDDL_KEY_READ
, KEY_READ
},
411 { SDDL_KEY_WRITE
, KEY_WRITE
},
412 { SDDL_KEY_EXECUTE
, KEY_EXECUTE
},
417 ParseAccessMaskString(
419 OUT DWORD
* pAccessMask
,
422 /* FIXME: Allow hexadecimal string for access rights! */
424 return ParseFlagsString(Buffer
, AccessMaskTable
, SDDL_SEPERATORC
, pAccessMask
, pLength
);
431 OUT BOOL
* pIsGuidValid
,
434 WCHAR GuidStr
[MAX_GUID_STRING_LEN
+ 1];
437 end
= wcschr(Buffer
, SDDL_SEPERATORC
);
440 SetLastError(ERROR_INVALID_PARAMETER
);
444 *pLength
= end
- Buffer
;
445 *pIsGuidValid
= (end
!= Buffer
);
449 if (end
- Buffer
> MAX_GUID_STRING_LEN
- 1)
451 SetLastError(ERROR_INVALID_PARAMETER
);
454 GuidStr
[end
- Buffer
] = UNICODE_NULL
;
455 wcsncpy(GuidStr
, Buffer
, end
- Buffer
);
456 if (RPC_S_OK
!= UuidFromStringW((unsigned short*)&GuidStr
, pGuid
))
458 SetLastError(ERROR_INVALID_PARAMETER
);
464 static const RECORD AceTypeTable
[] =
466 { SDDL_OBJECT_ACCESS_ALLOWED
, ACCESS_ALLOWED_OBJECT_ACE_TYPE
},
467 { SDDL_OBJECT_ACCESS_DENIED
, ACCESS_DENIED_OBJECT_ACE_TYPE
},
468 { SDDL_AUDIT
, SYSTEM_AUDIT_ACE_TYPE
},
469 { SDDL_ALARM
, SYSTEM_ALARM_ACE_TYPE
},
470 { SDDL_OBJECT_AUDIT
, SYSTEM_AUDIT_OBJECT_ACE_TYPE
},
471 { SDDL_OBJECT_ALARM
, SYSTEM_ALARM_OBJECT_ACE_TYPE
},
472 { SDDL_ACCESS_ALLOWED
, ACCESS_ALLOWED_ACE_TYPE
},
473 { SDDL_ACCESS_DENIED
, ACCESS_DENIED_ACE_TYPE
},
483 LPCWSTR ptr
= Buffer
;
484 SIZE_T PartialLength
;
485 DWORD aceType
, aceFlags
, accessMask
;
486 GUID object
, inheritObject
;
487 BOOL objectValid
, inheritObjectValid
;
491 if (*ptr
!= SDDL_ACE_BEGINC
)
493 SetLastError(ERROR_INVALID_PARAMETER
);
496 ptr
++; /* Skip SDDL_ACE_BEGINC */
498 if (!FindKeyInTable(AceTypeTable
, ptr
, &PartialLength
, &aceType
))
500 ptr
+= PartialLength
;
502 if (*ptr
!= SDDL_SEPERATORC
)
504 SetLastError(ERROR_INVALID_PARAMETER
);
507 ptr
++; /* Skip SDDL_SEPERATORC */
509 if (!ParseFlagsString(ptr
, AceFlagTable
, SDDL_SEPERATORC
, &aceFlags
, &PartialLength
))
511 ptr
+= PartialLength
+ 1;
513 if (!ParseAccessMaskString(ptr
, &accessMask
, &PartialLength
))
515 ptr
+= PartialLength
+ 1;
517 if (!ParseGuidString(ptr
, &object
, &objectValid
, &PartialLength
))
519 ptr
+= PartialLength
+ 1;
521 if (!ParseGuidString(ptr
, &inheritObject
, &inheritObjectValid
, &PartialLength
))
523 ptr
+= PartialLength
+ 1;
525 if (!ParseSidString(ptr
, &sid
, &PartialLength
))
527 ptr
+= PartialLength
;
528 if (*ptr
!= SDDL_ACE_ENDC
)
530 SetLastError(ERROR_INVALID_PARAMETER
);
533 ptr
++; /* Skip SDDL_ACE_ENDC */
534 *pLength
= ptr
- Buffer
;
538 case ACCESS_ALLOWED_ACE_TYPE
:
539 ret
= AddAccessAllowedAceEx(
546 case ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
547 ret
= AddAccessAllowedObjectAce(
552 objectValid
? &object
: NULL
,
553 inheritObjectValid
? &inheritObject
: NULL
,
556 case ACCESS_DENIED_ACE_TYPE
:
557 ret
= AddAccessDeniedAceEx(
564 case ACCESS_DENIED_OBJECT_ACE_TYPE
:
565 ret
= AddAccessDeniedObjectAce(
570 objectValid
? &object
: NULL
,
571 inheritObjectValid
? &inheritObject
: NULL
,
574 case SYSTEM_AUDIT_ACE_TYPE
:
575 ret
= AddAuditAccessAceEx(
584 case SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
585 ret
= AddAuditAccessObjectAce(
590 objectValid
? &object
: NULL
,
591 inheritObjectValid
? &inheritObject
: NULL
,
596 case SYSTEM_ALARM_ACE_TYPE
:
597 case SYSTEM_ALARM_OBJECT_ACE_TYPE
:
600 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
608 /* Exported functions */
614 AllocateLocallyUniqueId(PLUID Luid
)
618 Status
= NtAllocateLocallyUniqueId (Luid
);
619 if (!NT_SUCCESS (Status
))
621 SetLastError(RtlNtStatusToDosError(Status
));
633 AllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
634 BYTE nSubAuthorityCount
,
635 DWORD dwSubAuthority0
,
636 DWORD dwSubAuthority1
,
637 DWORD dwSubAuthority2
,
638 DWORD dwSubAuthority3
,
639 DWORD dwSubAuthority4
,
640 DWORD dwSubAuthority5
,
641 DWORD dwSubAuthority6
,
642 DWORD dwSubAuthority7
,
647 Status
= RtlAllocateAndInitializeSid(pIdentifierAuthority
,
658 if (!NT_SUCCESS(Status
))
660 SetLastError(RtlNtStatusToDosError(Status
));
672 CopySid(DWORD nDestinationSidLength
,
673 PSID pDestinationSid
,
678 Status
= RtlCopySid(nDestinationSidLength
,
681 if (!NT_SUCCESS (Status
))
683 SetLastError(RtlNtStatusToDosError(Status
));
690 static void DumpString(LPCWSTR string
, int cch
, WCHAR
**pwptr
, ULONG
*plen
)
693 cch
= strlenW(string
);
700 memcpy(*pwptr
, string
, sizeof(WCHAR
)*cch
);
705 static BOOL
DumpSidNumeric(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
708 WCHAR fmt
[] = { 'S','-','%','u','-','%','d',0 };
709 WCHAR subauthfmt
[] = { '-','%','u',0 };
713 if( !IsValidSid( psid
) || pisid
->Revision
!= SDDL_REVISION
)
715 SetLastError(ERROR_INVALID_SID
);
719 if (pisid
->IdentifierAuthority
.Value
[0] ||
720 pisid
->IdentifierAuthority
.Value
[1])
722 FIXME("not matching MS' bugs\n");
723 SetLastError(ERROR_INVALID_SID
);
727 sprintfW( buf
, fmt
, pisid
->Revision
,
729 MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
730 pisid
->IdentifierAuthority
.Value
[4] ),
731 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
732 pisid
->IdentifierAuthority
.Value
[2] )
734 DumpString(buf
, -1, pwptr
, plen
);
736 for( i
=0; i
<pisid
->SubAuthorityCount
; i
++ )
738 sprintfW( buf
, subauthfmt
, pisid
->SubAuthority
[i
] );
739 DumpString(buf
, -1, pwptr
, plen
);
744 static BOOL
DumpSid(PSID psid
, WCHAR
**pwptr
, ULONG
*plen
)
747 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
749 if (WellKnownSids
[i
].wstr
[0] && EqualSid(psid
, (PSID
)&(WellKnownSids
[i
].Sid
.Revision
)))
751 DumpString(WellKnownSids
[i
].wstr
, 2, pwptr
, plen
);
756 return DumpSidNumeric(psid
, pwptr
, plen
);
759 static void DumpRights(DWORD mask
, WCHAR
**pwptr
, ULONG
*plen
)
761 static const WCHAR fmtW
[] = {'0','x','%','x',0};
768 /* first check if the right have name */
769 for (i
= 0; i
< sizeof(AceRights
)/sizeof(AceRights
[0]); i
++)
771 if (AceRights
[i
].wstr
== NULL
)
773 if (mask
== AceRights
[i
].value
)
775 DumpString(AceRights
[i
].wstr
, -1, pwptr
, plen
);
780 /* then check if it can be built from bit names */
781 for (i
= 0; i
< 32; i
++)
783 if ((mask
& (1 << i
)) && (AceRightBitNames
[i
] == NULL
))
785 /* can't be built from bit names */
786 sprintfW(buf
, fmtW
, mask
);
787 DumpString(buf
, -1, pwptr
, plen
);
792 /* build from bit names */
793 for (i
= 0; i
< 32; i
++)
795 DumpString(AceRightBitNames
[i
], -1, pwptr
, plen
);
798 static BOOL
DumpAce(LPVOID pace
, WCHAR
**pwptr
, ULONG
*plen
)
800 ACCESS_ALLOWED_ACE
*piace
; /* all the supported ACEs have the same memory layout */
801 static const WCHAR openbr
= '(';
802 static const WCHAR closebr
= ')';
803 static const WCHAR semicolon
= ';';
805 if (((PACE_HEADER
)pace
)->AceType
> SYSTEM_ALARM_ACE_TYPE
|| ((PACE_HEADER
)pace
)->AceSize
< sizeof(ACCESS_ALLOWED_ACE
))
807 SetLastError(ERROR_INVALID_ACL
);
811 piace
= (ACCESS_ALLOWED_ACE
*)pace
;
812 DumpString(&openbr
, 1, pwptr
, plen
);
813 switch (piace
->Header
.AceType
)
815 case ACCESS_ALLOWED_ACE_TYPE
:
816 DumpString(SDDL_ACCESS_ALLOWED
, -1, pwptr
, plen
);
818 case ACCESS_DENIED_ACE_TYPE
:
819 DumpString(SDDL_ACCESS_DENIED
, -1, pwptr
, plen
);
821 case SYSTEM_AUDIT_ACE_TYPE
:
822 DumpString(SDDL_AUDIT
, -1, pwptr
, plen
);
824 case SYSTEM_ALARM_ACE_TYPE
:
825 DumpString(SDDL_ALARM
, -1, pwptr
, plen
);
828 DumpString(&semicolon
, 1, pwptr
, plen
);
830 if (piace
->Header
.AceFlags
& OBJECT_INHERIT_ACE
)
831 DumpString(SDDL_OBJECT_INHERIT
, -1, pwptr
, plen
);
832 if (piace
->Header
.AceFlags
& CONTAINER_INHERIT_ACE
)
833 DumpString(SDDL_CONTAINER_INHERIT
, -1, pwptr
, plen
);
834 if (piace
->Header
.AceFlags
& NO_PROPAGATE_INHERIT_ACE
)
835 DumpString(SDDL_NO_PROPAGATE
, -1, pwptr
, plen
);
836 if (piace
->Header
.AceFlags
& INHERIT_ONLY_ACE
)
837 DumpString(SDDL_INHERIT_ONLY
, -1, pwptr
, plen
);
838 if (piace
->Header
.AceFlags
& INHERITED_ACE
)
839 DumpString(SDDL_INHERITED
, -1, pwptr
, plen
);
840 if (piace
->Header
.AceFlags
& SUCCESSFUL_ACCESS_ACE_FLAG
)
841 DumpString(SDDL_AUDIT_SUCCESS
, -1, pwptr
, plen
);
842 if (piace
->Header
.AceFlags
& FAILED_ACCESS_ACE_FLAG
)
843 DumpString(SDDL_AUDIT_FAILURE
, -1, pwptr
, plen
);
844 DumpString(&semicolon
, 1, pwptr
, plen
);
845 DumpRights(piace
->Mask
, pwptr
, plen
);
846 DumpString(&semicolon
, 1, pwptr
, plen
);
847 /* objects not supported */
848 DumpString(&semicolon
, 1, pwptr
, plen
);
849 /* objects not supported */
850 DumpString(&semicolon
, 1, pwptr
, plen
);
851 if (!DumpSid((PSID
)&piace
->SidStart
, pwptr
, plen
))
853 DumpString(&closebr
, 1, pwptr
, plen
);
857 static BOOL
DumpAcl(PACL pacl
, WCHAR
**pwptr
, ULONG
*plen
, BOOL
protected, BOOL autoInheritReq
, BOOL autoInherited
)
863 DumpString(SDDL_PROTECTED
, -1, pwptr
, plen
);
865 DumpString(SDDL_AUTO_INHERIT_REQ
, -1, pwptr
, plen
);
867 DumpString(SDDL_AUTO_INHERITED
, -1, pwptr
, plen
);
872 if (!IsValidAcl(pacl
))
875 count
= pacl
->AceCount
;
876 for (i
= 0; i
< count
; i
++)
879 if (!GetAce(pacl
, i
, &ace
))
881 if (!DumpAce(ace
, pwptr
, plen
))
888 static BOOL
DumpOwner(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
890 static const WCHAR prefix
[] = {'O',':',0};
894 if (!GetSecurityDescriptorOwner(SecurityDescriptor
, &psid
, &bDefaulted
))
900 DumpString(prefix
, -1, pwptr
, plen
);
901 if (!DumpSid(psid
, pwptr
, plen
))
906 static BOOL
DumpGroup(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
908 static const WCHAR prefix
[] = {'G',':',0};
912 if (!GetSecurityDescriptorGroup(SecurityDescriptor
, &psid
, &bDefaulted
))
918 DumpString(prefix
, -1, pwptr
, plen
);
919 if (!DumpSid(psid
, pwptr
, plen
))
924 static BOOL
DumpDacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
926 static const WCHAR dacl
[] = {'D',':',0};
927 SECURITY_DESCRIPTOR_CONTROL control
;
928 BOOL present
, defaulted
;
932 if (!GetSecurityDescriptorDacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
935 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
941 DumpString(dacl
, 2, pwptr
, plen
);
942 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_DACL_PROTECTED
, control
& SE_DACL_AUTO_INHERIT_REQ
, control
& SE_DACL_AUTO_INHERITED
))
947 static BOOL
DumpSacl(PSECURITY_DESCRIPTOR SecurityDescriptor
, WCHAR
**pwptr
, ULONG
*plen
)
949 static const WCHAR sacl
[] = {'S',':',0};
950 SECURITY_DESCRIPTOR_CONTROL control
;
951 BOOL present
, defaulted
;
955 if (!GetSecurityDescriptorSacl(SecurityDescriptor
, &present
, &pacl
, &defaulted
))
958 if (!GetSecurityDescriptorControl(SecurityDescriptor
, &control
, &revision
))
964 DumpString(sacl
, 2, pwptr
, plen
);
965 if (!DumpAcl(pacl
, pwptr
, plen
, control
& SE_SACL_PROTECTED
, control
& SE_SACL_AUTO_INHERIT_REQ
, control
& SE_SACL_AUTO_INHERITED
))
970 /******************************************************************************
971 * ConvertSecurityDescriptorToStringSecurityDescriptorW [ADVAPI32.@]
975 ConvertSecurityDescriptorToStringSecurityDescriptorW(PSECURITY_DESCRIPTOR SecurityDescriptor
,
977 SECURITY_INFORMATION SecurityInformation
,
978 LPWSTR
*OutputString
,
984 if (SDRevision
!= SDDL_REVISION_1
)
986 ERR("Pogram requested unknown SDDL revision %d\n", SDRevision
);
987 SetLastError(ERROR_UNKNOWN_REVISION
);
992 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
993 if (!DumpOwner(SecurityDescriptor
, NULL
, &len
))
995 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
996 if (!DumpGroup(SecurityDescriptor
, NULL
, &len
))
998 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
999 if (!DumpDacl(SecurityDescriptor
, NULL
, &len
))
1001 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
1002 if (!DumpSacl(SecurityDescriptor
, NULL
, &len
))
1005 wstr
= wptr
= LocalAlloc(0, (len
+ 1)*sizeof(WCHAR
));
1006 if (SecurityInformation
& OWNER_SECURITY_INFORMATION
)
1007 if (!DumpOwner(SecurityDescriptor
, &wptr
, NULL
))
1009 if (SecurityInformation
& GROUP_SECURITY_INFORMATION
)
1010 if (!DumpGroup(SecurityDescriptor
, &wptr
, NULL
))
1012 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
1013 if (!DumpDacl(SecurityDescriptor
, &wptr
, NULL
))
1015 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
1016 if (!DumpSacl(SecurityDescriptor
, &wptr
, NULL
))
1020 TRACE("ret: %s, %d\n", wine_dbgstr_w(wstr
), len
);
1021 *OutputString
= wstr
;
1023 *OutputLen
= strlenW(*OutputString
)+1;
1028 /******************************************************************************
1029 * ConvertSecurityDescriptorToStringSecurityDescriptorA [ADVAPI32.@]
1033 ConvertSecurityDescriptorToStringSecurityDescriptorA(PSECURITY_DESCRIPTOR SecurityDescriptor
,
1035 SECURITY_INFORMATION Information
,
1036 LPSTR
*OutputString
,
1041 if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor
, SDRevision
, Information
, &wstr
, &len
))
1045 lenA
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, NULL
, 0, NULL
, NULL
);
1046 *OutputString
= HeapAlloc(GetProcessHeap(), 0, lenA
);
1047 WideCharToMultiByte(CP_ACP
, 0, wstr
, len
, *OutputString
, lenA
, NULL
, NULL
);
1050 if (OutputLen
!= NULL
)
1056 *OutputString
= NULL
;
1063 /******************************************************************************
1064 * ConvertStringSecurityDescriptorToSecurityDescriptorW [ADVAPI32.@]
1068 ConvertStringSecurityDescriptorToSecurityDescriptorW(
1069 IN LPCWSTR StringSecurityDescriptor
,
1070 IN DWORD StringSDRevision
,
1071 OUT PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1072 OUT PULONG SecurityDescriptorSize
)
1074 PSECURITY_DESCRIPTOR sd
= NULL
;
1077 if (!StringSecurityDescriptor
)
1078 SetLastError(ERROR_INVALID_PARAMETER
);
1079 else if (StringSDRevision
!= SDDL_REVISION_1
)
1080 SetLastError(ERROR_INVALID_PARAMETER
);
1083 LPCWSTR ptr
= StringSecurityDescriptor
;
1084 DWORD numberOfAces
= 0;
1085 DWORD relativeSdSize
;
1087 PSECURITY_DESCRIPTOR relativeSd
= NULL
;
1090 BOOL present
, dummy
;
1091 /* An easy way to know how much space we need for an ACL is to count
1092 * the number of ACEs and say that we have 1 SID by ACE
1094 ptr
= wcschr(StringSecurityDescriptor
, SDDL_ACE_BEGINC
);
1098 ptr
= wcschr(ptr
+ 1, SDDL_ACE_BEGINC
);
1100 MaxAclSize
= sizeof(ACL
) + numberOfAces
*
1101 (sizeof(ACCESS_ALLOWED_OBJECT_ACE
) + SECURITY_MAX_SID_SIZE
);
1103 sd
= (SECURITY_DESCRIPTOR
*)LocalAlloc(0, sizeof(SECURITY_DESCRIPTOR
));
1106 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1109 ret
= InitializeSecurityDescriptor(sd
, SECURITY_DESCRIPTOR_REVISION
);
1113 /* Now, really parse the string */
1114 ptr
= StringSecurityDescriptor
;
1117 if (ptr
[1] != SDDL_DELIMINATORC
)
1119 SetLastError(ERROR_INVALID_PARAMETER
);
1132 ret
= ParseSidString(ptr
, &pSid
, &Length
);
1136 ret
= SetSecurityDescriptorOwner(sd
, pSid
, FALSE
);
1138 ret
= SetSecurityDescriptorGroup(sd
, pSid
, FALSE
);
1152 BOOL isDacl
= (ptr
[-2] == 'D');
1155 ret
= ParseFlagsString(ptr
, DaclFlagTable
, SDDL_ACE_BEGINC
, &aclFlags
, &Length
);
1157 ret
= ParseFlagsString(ptr
, SaclFlagTable
, SDDL_ACE_BEGINC
, &aclFlags
, &Length
);
1160 pAcl
= (PACL
)LocalAlloc(0, MaxAclSize
);
1163 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1167 if (!InitializeAcl(pAcl
, (DWORD
)MaxAclSize
, ACL_REVISION_DS
))
1174 ret
= SetSecurityDescriptorControl(
1176 (SECURITY_DESCRIPTOR_CONTROL
)aclFlags
,
1177 (SECURITY_DESCRIPTOR_CONTROL
)aclFlags
);
1185 while (*ptr
== SDDL_ACE_BEGINC
)
1187 ret
= ParseAceString(ptr
, pAcl
, &Length
);
1196 ret
= SetSecurityDescriptorDacl(sd
, TRUE
, pAcl
, FALSE
);
1198 ret
= SetSecurityDescriptorSacl(sd
, TRUE
, pAcl
, FALSE
);
1208 SetLastError(ERROR_INVALID_PARAMETER
);
1219 LocalFree(relativeSd
);
1220 relativeSd
= LocalAlloc(0, relativeSdSize
);
1223 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1226 ret
= MakeSelfRelativeSD(sd
, relativeSd
, &relativeSdSize
);
1227 if (ret
|| GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1230 if (SecurityDescriptorSize
)
1231 *SecurityDescriptorSize
= relativeSdSize
;
1232 *SecurityDescriptor
= relativeSd
;
1235 if (GetSecurityDescriptorOwner(sd
, &pSid
, &dummy
))
1237 if (GetSecurityDescriptorGroup(sd
, &pSid
, &dummy
))
1239 if (GetSecurityDescriptorDacl(sd
, &present
, &pAcl
, &dummy
) && present
)
1241 if (GetSecurityDescriptorSacl(sd
, &present
, &pAcl
, &dummy
) && present
)
1249 /* Winehq cvs 20050916 */
1250 /******************************************************************************
1251 * ConvertStringSecurityDescriptorToSecurityDescriptorA [ADVAPI32.@]
1254 BOOL WINAPI
ConvertStringSecurityDescriptorToSecurityDescriptorA(
1255 LPCSTR StringSecurityDescriptor
,
1256 DWORD StringSDRevision
,
1257 PSECURITY_DESCRIPTOR
* SecurityDescriptor
,
1258 PULONG SecurityDescriptorSize
)
1262 LPWSTR StringSecurityDescriptorW
;
1264 len
= MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, NULL
, 0);
1265 StringSecurityDescriptorW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1267 if (StringSecurityDescriptorW
)
1269 MultiByteToWideChar(CP_ACP
, 0, StringSecurityDescriptor
, -1, StringSecurityDescriptorW
, len
);
1271 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(StringSecurityDescriptorW
,
1272 StringSDRevision
, SecurityDescriptor
,
1273 SecurityDescriptorSize
);
1274 HeapFree(GetProcessHeap(), 0, StringSecurityDescriptorW
);
1284 EqualPrefixSid(PSID pSid1
,
1287 return RtlEqualPrefixSid (pSid1
, pSid2
);
1295 EqualSid(PSID pSid1
,
1298 return RtlEqualSid (pSid1
, pSid2
);
1306 * Docs says this function does NOT return a value
1307 * even thou it's defined to return a PVOID...
1312 return RtlFreeSid(pSid
);
1320 GetLengthSid(PSID pSid
)
1322 return (DWORD
)RtlLengthSid(pSid
);
1329 PSID_IDENTIFIER_AUTHORITY STDCALL
1330 GetSidIdentifierAuthority(PSID pSid
)
1332 return RtlIdentifierAuthoritySid(pSid
);
1340 GetSidLengthRequired(UCHAR nSubAuthorityCount
)
1342 return (DWORD
)RtlLengthRequiredSid(nSubAuthorityCount
);
1350 GetSidSubAuthority(PSID pSid
,
1351 DWORD nSubAuthority
)
1353 return (PDWORD
)RtlSubAuthoritySid(pSid
, nSubAuthority
);
1361 GetSidSubAuthorityCount(PSID pSid
)
1363 return RtlSubAuthorityCountSid(pSid
);
1371 InitializeSid(PSID Sid
,
1372 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority
,
1373 BYTE nSubAuthorityCount
)
1377 Status
= RtlInitializeSid(Sid
,
1378 pIdentifierAuthority
,
1379 nSubAuthorityCount
);
1380 if (!NT_SUCCESS(Status
))
1382 SetLastError(RtlNtStatusToDosError(Status
));
1394 IsValidSid(PSID pSid
)
1396 return (BOOL
)RtlValidSid(pSid
);
1404 ConvertSidToStringSidW(PSID Sid
,
1408 UNICODE_STRING UnicodeString
;
1409 WCHAR FixedBuffer
[64];
1411 if (!RtlValidSid(Sid
))
1413 SetLastError(ERROR_INVALID_SID
);
1417 UnicodeString
.Length
= 0;
1418 UnicodeString
.MaximumLength
= sizeof(FixedBuffer
);
1419 UnicodeString
.Buffer
= FixedBuffer
;
1420 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, FALSE
);
1421 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1423 Status
= RtlConvertSidToUnicodeString(&UnicodeString
, Sid
, TRUE
);
1426 if (!NT_SUCCESS(Status
))
1428 SetLastError(RtlNtStatusToDosError(Status
));
1432 *StringSid
= LocalAlloc(LMEM_FIXED
, UnicodeString
.Length
+ sizeof(WCHAR
));
1433 if (NULL
== *StringSid
)
1435 if (UnicodeString
.Buffer
!= FixedBuffer
)
1437 RtlFreeUnicodeString(&UnicodeString
);
1439 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1443 MoveMemory(*StringSid
, UnicodeString
.Buffer
, UnicodeString
.Length
);
1444 ZeroMemory((PCHAR
) *StringSid
+ UnicodeString
.Length
, sizeof(WCHAR
));
1445 if (UnicodeString
.Buffer
!= FixedBuffer
)
1447 RtlFreeUnicodeString(&UnicodeString
);
1458 ConvertSidToStringSidA(PSID Sid
,
1464 if (!ConvertSidToStringSidW(Sid
, &StringSidW
))
1469 Len
= WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, NULL
, 0, NULL
, NULL
);
1472 LocalFree(StringSidW
);
1473 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1477 *StringSid
= LocalAlloc(LMEM_FIXED
, Len
);
1478 if (NULL
== *StringSid
)
1480 LocalFree(StringSidW
);
1481 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1485 if (!WideCharToMultiByte(CP_ACP
, 0, StringSidW
, -1, *StringSid
, Len
, NULL
, NULL
))
1487 LocalFree(StringSid
);
1488 LocalFree(StringSidW
);
1492 LocalFree(StringSidW
);
1502 EqualDomainSid(IN PSID pSid1
,
1515 GetWindowsAccountDomainSid(IN PSID pSid
,
1516 OUT PSID ppDomainSid
,
1517 IN OUT DWORD
* cbSid
)
1528 CreateWellKnownSid(IN WELL_KNOWN_SID_TYPE WellKnownSidType
,
1529 IN PSID DomainSid OPTIONAL
,
1531 IN OUT DWORD
* cbSid
)
1534 TRACE("(%d, %s, %p, %p)\n", WellKnownSidType
, debugstr_sid(DomainSid
), pSid
, cbSid
);
1536 if (DomainSid
!= NULL
)
1538 FIXME("Only local computer supported!\n");
1539 SetLastError(ERROR_INVALID_PARAMETER
); /* FIXME */
1543 if (cbSid
== NULL
|| pSid
== NULL
)
1545 SetLastError(ERROR_INVALID_PARAMETER
);
1549 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
1551 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
1553 DWORD length
= GetSidLengthRequired(WellKnownSids
[i
].Sid
.SubAuthorityCount
);
1555 if (*cbSid
< length
)
1557 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1561 CopyMemory(pSid
, &WellKnownSids
[i
].Sid
.Revision
, length
);
1567 SetLastError(ERROR_INVALID_PARAMETER
);
1576 IsWellKnownSid(IN PSID pSid
,
1577 IN WELL_KNOWN_SID_TYPE WellKnownSidType
)
1580 TRACE("(%s, %d)\n", debugstr_sid(pSid
), WellKnownSidType
);
1582 for (i
= 0; i
< sizeof(WellKnownSids
) / sizeof(WellKnownSids
[0]); i
++)
1584 if (WellKnownSids
[i
].Type
== WellKnownSidType
)
1586 if (EqualSid(pSid
, (PSID
)((ULONG_PTR
)&WellKnownSids
[i
].Sid
.Revision
)))
1599 ConvertStringSidToSidA(IN LPCSTR StringSid
,
1602 BOOL bRetVal
= FALSE
;
1604 if (!StringSid
|| !sid
)
1605 SetLastError(ERROR_INVALID_PARAMETER
);
1608 UINT len
= MultiByteToWideChar(CP_ACP
, 0, StringSid
, -1, NULL
, 0);
1609 LPWSTR wStringSid
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1610 MultiByteToWideChar(CP_ACP
, 0, StringSid
, - 1, wStringSid
, len
);
1611 bRetVal
= ConvertStringSidToSidW(wStringSid
, sid
);
1612 HeapFree(GetProcessHeap(), 0, wStringSid
);
1618 /******************************************************************************
1619 * ComputeStringSidSize
1622 ComputeStringSidSize(LPCWSTR StringSid
)
1624 DWORD size
= sizeof(SID
);
1626 if (StringSid
[0] == 'S' && StringSid
[1] == '-') /* S-R-I-S-S */
1631 if (*StringSid
== '-')
1637 size
+= (ctok
- 3) * sizeof(DWORD
);
1639 else /* String constant format - Only available in winxp and above */
1643 for (i
= 0; i
< sizeof(WellKnownSids
)/sizeof(WellKnownSids
[0]); i
++)
1644 if (!strncmpW(WellKnownSids
[i
].wstr
, StringSid
, 2))
1645 size
+= (WellKnownSids
[i
].Sid
.SubAuthorityCount
- 1) * sizeof(DWORD
);
1651 static const RECORD SidTable
[] =
1653 { SDDL_ACCOUNT_OPERATORS
, WinBuiltinAccountOperatorsSid
},
1654 { SDDL_ALIAS_PREW2KCOMPACC
, WinBuiltinPreWindows2000CompatibleAccessSid
},
1655 { SDDL_ANONYMOUS
, WinAnonymousSid
},
1656 { SDDL_AUTHENTICATED_USERS
, WinAuthenticatedUserSid
},
1657 { SDDL_BUILTIN_ADMINISTRATORS
, WinBuiltinAdministratorsSid
},
1658 { SDDL_BUILTIN_GUESTS
, WinBuiltinGuestsSid
},
1659 { SDDL_BACKUP_OPERATORS
, WinBuiltinBackupOperatorsSid
},
1660 { SDDL_BUILTIN_USERS
, WinBuiltinUsersSid
},
1661 { SDDL_CERT_SERV_ADMINISTRATORS
, WinAccountCertAdminsSid
/* FIXME: DOMAIN_GROUP_RID_CERT_ADMINS */ },
1662 { SDDL_CREATOR_GROUP
, WinCreatorGroupSid
},
1663 { SDDL_CREATOR_OWNER
, WinCreatorOwnerSid
},
1664 { SDDL_DOMAIN_ADMINISTRATORS
, WinAccountDomainAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ADMINS */ },
1665 { SDDL_DOMAIN_COMPUTERS
, WinAccountComputersSid
/* FIXME: DOMAIN_GROUP_RID_COMPUTERS */ },
1666 { SDDL_DOMAIN_DOMAIN_CONTROLLERS
, WinAccountControllersSid
/* FIXME: DOMAIN_GROUP_RID_CONTROLLERS */ },
1667 { SDDL_DOMAIN_GUESTS
, WinAccountDomainGuestsSid
/* FIXME: DOMAIN_GROUP_RID_GUESTS */ },
1668 { SDDL_DOMAIN_USERS
, WinAccountDomainUsersSid
/* FIXME: DOMAIN_GROUP_RID_USERS */ },
1669 { SDDL_ENTERPRISE_ADMINS
, WinAccountEnterpriseAdminsSid
/* FIXME: DOMAIN_GROUP_RID_ENTERPRISE_ADMINS */ },
1670 { SDDL_ENTERPRISE_DOMAIN_CONTROLLERS
, WinLogonIdsSid
/* FIXME: SECURITY_SERVER_LOGON_RID */ },
1671 { SDDL_EVERYONE
, WinWorldSid
},
1672 { SDDL_GROUP_POLICY_ADMINS
, WinAccountPolicyAdminsSid
/* FIXME: DOMAIN_GROUP_RID_POLICY_ADMINS */ },
1673 { SDDL_INTERACTIVE
, WinInteractiveSid
},
1674 { SDDL_LOCAL_ADMIN
, WinAccountAdministratorSid
/* FIXME: DOMAIN_USER_RID_ADMIN */ },
1675 { SDDL_LOCAL_GUEST
, WinAccountGuestSid
/* FIXME: DOMAIN_USER_RID_GUEST */ },
1676 { SDDL_LOCAL_SERVICE
, WinLocalServiceSid
},
1677 { SDDL_LOCAL_SYSTEM
, WinLocalSystemSid
},
1678 { SDDL_NETWORK
, WinNetworkSid
},
1679 { SDDL_NETWORK_CONFIGURATION_OPS
, WinBuiltinNetworkConfigurationOperatorsSid
},
1680 { SDDL_NETWORK_SERVICE
, WinNetworkServiceSid
},
1681 { SDDL_PRINTER_OPERATORS
, WinBuiltinPrintOperatorsSid
},
1682 { SDDL_PERSONAL_SELF
, WinSelfSid
},
1683 { SDDL_POWER_USERS
, WinBuiltinPowerUsersSid
},
1684 { SDDL_RAS_SERVERS
, WinAccountRasAndIasServersSid
/* FIXME: DOMAIN_ALIAS_RID_RAS_SERVERS */ },
1685 { SDDL_REMOTE_DESKTOP
, WinBuiltinRemoteDesktopUsersSid
},
1686 { SDDL_REPLICATOR
, WinBuiltinReplicatorSid
},
1687 { SDDL_RESTRICTED_CODE
, WinRestrictedCodeSid
},
1688 { SDDL_SCHEMA_ADMINISTRATORS
, WinAccountSchemaAdminsSid
/* FIXME: DOMAIN_GROUP_RID_SCHEMA_ADMINS */ },
1689 { SDDL_SERVER_OPERATORS
, WinBuiltinSystemOperatorsSid
},
1690 { SDDL_SERVICE
, WinServiceSid
},
1698 ConvertStringSidToSidW(IN LPCWSTR StringSid
,
1702 DWORD i
, cBytes
, identAuth
, csubauth
;
1706 TRACE("%s %p\n", debugstr_w(StringSid
), sid
);
1710 SetLastError(ERROR_INVALID_SID
);
1713 for (i
= 0; i
< sizeof(SidTable
) / sizeof(SidTable
[0]) - 1; i
++)
1715 if (wcscmp(StringSid
, SidTable
[i
].key
) == 0)
1717 WELL_KNOWN_SID_TYPE knownSid
= (WELL_KNOWN_SID_TYPE
)SidTable
[i
].value
;
1718 size
= SECURITY_MAX_SID_SIZE
;
1719 *sid
= LocalAlloc(0, size
);
1722 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1725 ret
= CreateWellKnownSid(
1732 SetLastError(ERROR_INVALID_SID
);
1739 /* That's probably a string S-R-I-S-S... */
1740 if (StringSid
[0] != 'S' || StringSid
[1] != '-')
1742 SetLastError(ERROR_INVALID_SID
);
1746 cBytes
= ComputeStringSidSize(StringSid
);
1747 pisid
= (SID
*)LocalAlloc( 0, cBytes
);
1750 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1755 csubauth
= ((cBytes
- sizeof(SID
)) / sizeof(DWORD
)) + 1;
1757 StringSid
+= 2; /* Advance to Revision */
1758 pisid
->Revision
= atoiW(StringSid
);
1760 if (pisid
->Revision
!= SDDL_REVISION
)
1762 TRACE("Revision %d is unknown\n", pisid
->Revision
);
1763 goto lend
; /* ERROR_INVALID_SID */
1767 TRACE("SubAuthorityCount is 0\n");
1768 goto lend
; /* ERROR_INVALID_SID */
1771 pisid
->SubAuthorityCount
= csubauth
;
1773 /* Advance to identifier authority */
1774 while (*StringSid
&& *StringSid
!= '-')
1776 if (*StringSid
== '-')
1779 /* MS' implementation can't handle values greater than 2^32 - 1, so
1780 * we don't either; assume most significant bytes are always 0
1782 pisid
->IdentifierAuthority
.Value
[0] = 0;
1783 pisid
->IdentifierAuthority
.Value
[1] = 0;
1784 identAuth
= atoiW(StringSid
);
1785 pisid
->IdentifierAuthority
.Value
[5] = identAuth
& 0xff;
1786 pisid
->IdentifierAuthority
.Value
[4] = (identAuth
& 0xff00) >> 8;
1787 pisid
->IdentifierAuthority
.Value
[3] = (identAuth
& 0xff0000) >> 16;
1788 pisid
->IdentifierAuthority
.Value
[2] = (identAuth
& 0xff000000) >> 24;
1790 /* Advance to first sub authority */
1791 while (*StringSid
&& *StringSid
!= '-')
1793 if (*StringSid
== '-')
1798 while (*StringSid
&& *StringSid
!= '-')
1800 if (*StringSid
== '-')
1803 pisid
->SubAuthority
[i
++] = atoiW(StringSid
);
1806 if (i
!= pisid
->SubAuthorityCount
)
1807 goto lend
; /* ERROR_INVALID_SID */
1814 SetLastError(ERROR_INVALID_SID
);
1816 TRACE("returning %s\n", ret
? "TRUE" : "FALSE");