' ########################################################################################
' Microsoft Windows
' File: SubAuth.inc
' Contents: This module defines types and macros for Subauthentication Packages
' Copyright (c) 2011 Jos? Roca
' Portions Copyright (c) Microsoft Corporation.
' All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################
#IF NOT %DEF(PBX_SubAuth_PBI)
#DEFINE PBX_SubAuth_PBI
%EQUATE `SUBAUTH_INC = 1
'#if (!defined(_NTDEF_)) && (!defined(_NTSECAPI_))
'typedef LONG NTSTATUS, *PNTSTATUS;
'typedef struct _UNICODE_STRING {
' USHORT Length;
' USHORT MaximumLength;
' PWSTR Buffer;
'} UNICODE_STRING, *PUNICODE_STRING;
'typedef struct _STRING {
' USHORT Length;
' USHORT MaximumLength;
' PCHAR Buffer;
'} STRING, *PSTRING;
'#endif
'#ifndef _NTDEF_
#IF NOT %DEF(%NTDEF)
 #IF NOT %DEF(%NTSECAPI_INC)
 ' // Size = 8 bytes
 TYPE UNICODE_STRING WORD
 Length AS WORD
 MaximumLength AS WORD
 Buffer AS WSTRINGZ PTR
 END TYPE
 ' // Size = 8 bytes
 TYPE ANSI_STRING WORD
 Length AS WORD
 MaximumLength AS WORD
 Buffer AS ASCIIZ PTR
 END TYPE
 #ENDIF
#ENDIF
' // Size = 8 bytes
TYPE OLD_LARGE_INTEGER DWORD
 LowPart AS DWORD ' ULONG
 HighPart AS LONG
END TYPE
'#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
#IF NOT %DEF(%NTDEF)
FUNCTION NT_SUCCESS(BYVAL Status AS LONG) AS LONG
 FUNCTION = (BITS(LONG, Status) >= 0)
END FUNCTION
#ENDIF
'#endif
'#ifndef _NTSAM_SAM_HANDLE_
'typedef PVOID SAM_HANDLE, *PSAM_HANDLE;
'#define _NTSAM_SAM_HANDLE_
'#endif
'#ifndef _NTSAM_USER_ACCOUNT_FLAGS_
#IF NOT %DEF(%NTSAM_USER_ACCOUNT_FLAGS)
'//
'// User account control flags...
'//
%EQUATE `USER_ACCOUNT_DISABLED = &H00000001
%EQUATE `USER_HOME_DIRECTORY_REQUIRED = &H00000002
%EQUATE `USER_PASSWORD_NOT_REQUIRED = &H00000004
%EQUATE `USER_TEMP_DUPLICATE_ACCOUNT = &H00000008
%EQUATE `USER_NORMAL_ACCOUNT = &H00000010
%EQUATE `USER_MNS_LOGON_ACCOUNT = &H00000020
%EQUATE `USER_INTERDOMAIN_TRUST_ACCOUNT = &H00000040
%EQUATE `USER_WORKSTATION_TRUST_ACCOUNT = &H00000080
%EQUATE `USER_SERVER_TRUST_ACCOUNT = &H00000100
%EQUATE `USER_DONT_EXPIRE_PASSWORD = &H00000200
%EQUATE `USER_ACCOUNT_AUTO_LOCKED = &H00000400
%EQUATE `USER_ENCRYPTED_TEXT_PASSWORD_ALLOWED = &H00000800
%EQUATE `USER_SMARTCARD_REQUIRED = &H00001000
%EQUATE `USER_TRUSTED_FOR_DELEGATION = &H00002000
%EQUATE `USER_NOT_DELEGATED = &H00004000
%EQUATE `USER_USE_DES_KEY_ONLY = &H00008000
%EQUATE `USER_DONT_REQUIRE_PREAUTH = &H00010000
%EQUATE `USER_PASSWORD_EXPIRED = &H00020000
%EQUATE `USER_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = &H00040000
%EQUATE `USER_NO_AUTH_DATA_REQUIRED = &H00080000
%EQUATE `USER_PARTIAL_SECRETS_ACCOUNT = &H00100000
%EQUATE `USER_USE_AES_KEYS = &H00200000
'#define NEXT_FREE_ACCOUNT_CONTROL_BIT (USER_USE_AES_KEYS << 1)
%EQUATE `NEXT_FREE_ACCOUNT_CONTROL_BIT = %USER_USE_AES_KEYS * 2
%EQUATE `USER_MACHINE_ACCOUNT_MASK = (%USER_INTERDOMAIN_TRUST_ACCOUNT OR %USER_WORKSTATION_TRUST_ACCOUNT OR %USER_SERVER_TRUST_ACCOUNT)
%EQUATE `USER_ACCOUNT_TYPE_MASK = (%USER_TEMP_DUPLICATE_ACCOUNT OR %USER_NORMAL_ACCOUNT OR %USER_MACHINE_ACCOUNT_MASK)
%EQUATE `USER_COMPUTED_ACCOUNT_CONTROL_BITS = (%USER_ACCOUNT_AUTO_LOCKED OR %USER_PASSWORD_EXPIRED)
'//
'// Logon times may be expressed in day, hour, or minute granularity.
'//
'// Days per week = 7
'// Hours per week = 168
'// Minutes per week = 10080
'//
%EQUATE `SAM_DAYS_PER_WEEK = 7
%EQUATE `SAM_HOURS_PER_WEEK = 24 * %SAM_DAYS_PER_WEEK
%EQUATE `SAM_MINUTES_PER_WEEK = 60 * %SAM_HOURS_PER_WEEK
'typedef struct _LOGON_HOURS {
' USHORT UnitsPerWeek;
' //
' // UnitsPerWeek is the number of equal length time units the week is
' // divided into. This value is used to compute the length of the bit
' // string in logon_hours. Must be less than or equal to
' // SAM_UNITS_PER_WEEK (10080) for this release.
' //
' // LogonHours is a bit map of valid logon times. Each bit represents
' // a unique division in a week. The largest bit map supported is 1260
' // bytes (10080 bits), which represents minutes per week. In this case
' // the first bit (bit 0, byte 0) is Sunday, 00:00:00 - 00-00:59; bit 1,
' // byte 0 is Sunday, 00:01:00 - 00:01:59, etc. A NULL pointer means
' // DONT_CHANGE for SamSetInformationUser() calls.
' //
' PUCHAR LogonHours;
'} LOGON_HOURS, *PLOGON_HOURS;
' // Size ) 8 bytes
TYPE LOGON_HOURS DWORD
 UnitsPerWeek AS WORD
 LogonHours AS BYTE PTR
END TYPE
' // Size ) 8 bytes
TYPE SR_SECURITY_DESCRIPTOR DWORD
 Length AS DWORD ' ULONG
 SecurityDescriptor AS BYTE PTR
END TYPE
'#define _NTSAM_USER_ACCOUNT_FLAG_
'#endif
' Microsoft drops the final "S" on this definition, but the equate is clearly
' intended to match the #IF block, so...
%EQUATE `NTSAM_USER_ACCOUNT_FLAGS = 1
#ENDIF ' #IF NOT %DEF(%NTSAM_USER_ACCOUNT_FLAGS)
'#ifndef _NTSAM_USER_ALL_INFO_
'#include "pshpack4.h"
#IF NOT %DEF(%NTSAM_USER_ALL_INFO)
' // Size = 196 bytes
TYPE USER_ALL_INFORMATION ' Must be 8 byte aligned
 LastLogon AS QUAD ' LARGE_INTEGER
 LastLogoff AS QUAD ' LARGE_INTEGER
 PasswordLastSet AS QUAD ' LARGE_INTEGER
 AccountExpires AS QUAD ' LARGE_INTEGER
 PasswordCanChange AS QUAD ' LARGE_INTEGER
 PasswordMustChange AS QUAD ' LARGE_INTEGER
 UserName AS UNICODE_STRING ' UNICODE_STRING
 FullName AS UNICODE_STRING ' UNICODE_STRING
 HomeDirectory AS UNICODE_STRING ' UNICODE_STRING
 HomeDirectoryDrive AS UNICODE_STRING ' UNICODE_STRING
 ScriptPath AS UNICODE_STRING ' UNICODE_STRING
 ProfilePath AS UNICODE_STRING ' UNICODE_STRING
 AdminComment AS UNICODE_STRING ' UNICODE_STRING
 WorkStations AS UNICODE_STRING ' UNICODE_STRING
 UserComment AS UNICODE_STRING ' UNICODE_STRING
 Parameters AS UNICODE_STRING ' UNICODE_STRING
 LmPassword AS UNICODE_STRING ' UNICODE_STRING
 NtPassword AS UNICODE_STRING ' UNICODE_STRING
 PrivateData AS UNICODE_STRING ' UNICODE_STRING
 SecurityDescriptor AS SR_SECURITY_DESCRIPTOR ' SR_SECURITY_DESCRIPTOR
 UserId AS DWORD ' ULONG
 PrimaryGroupId AS DWORD ' ULONG
 UserAccountControl AS DWORD ' ULONG
 WhichFields AS DWORD ' ULONG
 LogonHours AS LOGON_HOURS ' LOGON_HOURS
 BadPasswordCount AS WORD ' USHORT
 LogonCount AS WORD ' USHORT
 CountryCode AS WORD ' USHORT
 CodePage AS WORD ' USHORT
 LmPasswordPresent AS BYTE ' BOOLEAN
 NtPasswordPresent AS BYTE ' BOOLEAN
 PasswordExpired AS BYTE ' BOOLEAN
 PrivateDataSensitive AS BYTE ' BOOLEAN
END TYPE
%EQUATE `NTSAM_USER_ALL_INFO = 1
#ENDIF ' #IF NOT %DEF(%NTSAM_USER_ALL_INFO)
'#include "poppack.h"
'#define _NTSAM_USER_ALL_INFO_
'#endif
'#ifndef _NTSAM_SAM_USER_PARMS_
'#define USER_ALL_PARAMETERS 0x00200000
'#define _NTSAM_SAM_USER_PARMS_
'#endif
#IF NOT %DEF(%NTSAM_SAM_USER_PARMS)
 %USER_ALL_PARAMETERS = &H00200000
 %NTSAM_SAM_USER_PARMS = 1
#ENDIF
%EQUATE `CLEAR_BLOCK_LENGTH = 8
UNION CLEAR_BLOCK_UNION
 data AS STRING * %CLEAR_BLOCK_LENGTH ' CHAR data[CLEAR_BLOCK_LENGTH]
 ' // For compatibility with the PB declares
 cdata AS STRING * %CLEAR_BLOCK_LENGTH ' CHAR data[CLEAR_BLOCK_LENGTH]
END UNION
' // Size = 8 bytes
TYPE CLEAR_BLOCK BYTE
 CLEAR_BLOCK_UNION
END TYPE
%EQUATE `CYPHER_BLOCK_LENGTH = 8
UNION CYPHER_BLOCK_UNION
 data AS STRING * %CYPHER_BLOCK_LENGTH ' CHAR data[CYPHER_BLOCK_LENGTH]
 ' // For compatibility with the PB declares
 cdata AS STRING * %CYPHER_BLOCK_LENGTH ' CHAR data[CYPHER_BLOCK_LENGTH]
END UNION
' // Size = 8 bytes
TYPE CYPHER_BLOCK BYTE
 CYPHER_BLOCK_UNION
END TYPE
UNION LM_OWF_PASSWORD_UNION
 data(1) AS CYPHER_BLOCK ' CYPHER_BLOCK data[2]
 ' // For compatibility with the PB declares
 cdata(1) AS CYPHER_BLOCK ' CYPHER_BLOCK data[2]
END UNION
' // Size = 16 bytes
TYPE LM_OWF_PASSWORD BYTE
 LM_OWF_PASSWORD_UNION
END TYPE
'typedef LM_OWF_PASSWORD * PLM_OWF_PASSWORD;
'typedef CLEAR_BLOCK LM_CHALLENGE;
'typedef LM_CHALLENGE * PLM_CHALLENGE;
'typedef LM_OWF_PASSWORD NT_OWF_PASSWORD;
'typedef NT_OWF_PASSWORD * PNT_OWF_PASSWORD;
'typedef LM_CHALLENGE NT_CHALLENGE;
'typedef NT_CHALLENGE * PNT_CHALLENGE;
MACRO LM_CHALLENGE = CLEAR_BLOCK
MACRO NT_OWF_PASSWORD = LM_OWF_PASSWORD
MACRO NT_CHALLENGE = LM_CHALLENGE
%EQUATE `USER_SESSION_KEY_LENGTH = %CYPHER_BLOCK_LENGTH * 2
UNION USER_SESSION_KEY_UNION
 data(1) AS CYPHER_BLOCK ' CYPHER_BLOCK data[2]
 ' // For compatibility with the PB declares
 cdata(1) AS CYPHER_BLOCK ' CYPHER_BLOCK data[2]
END UNION
' // Size = 16 bytes
TYPE USER_SESSION_KEY BYTE
 USER_SESSION_KEY_UNION
END TYPE
'typedef enum _NETLOGON_LOGON_INFO_CLASS {
%EQUATE `NetlogonInteractiveInformation = 1
%EQUATE `NetlogonNetworkInformation = 2
%EQUATE `NetlogonServiceInformation = 3
%EQUATE `NetlogonGenericInformation = 4
%EQUATE `NetlogonInteractiveTransitiveInformation = 5
%EQUATE `NetlogonNetworkTransitiveInformation = 6
%EQUATE `NetlogonServiceTransitiveInformation = 7
' // Size = 36 bytes
TYPE NETLOGON_LOGON_IDENTITY_INFO ' Must be 8 byte aligned
 LogonDomainName AS UNICODE_STRING ' UNICODE_STRING
 ParameterControl AS DWORD ' ULONG
 LogonId AS QUAD ' OLD_LARGE_INTEGER
 UserName AS UNICODE_STRING ' UNICODE_STRING
 Workstation AS UNICODE_STRING ' UNICODE_STRING
END TYPE
' // Size = 68 bytes
TYPE NETLOGON_INTERACTIVE_INFO DWORD
 Identity AS NETLOGON_LOGON_IDENTITY_INFO
 LmOwfPassword AS LM_OWF_PASSWORD
 NtOwfPassword AS NT_OWF_PASSWORD
END TYPE
' // Size = 68 bytes
TYPE NETLOGON_SERVICE_INFO DWORD
 Identity AS NETLOGON_LOGON_IDENTITY_INFO
 LmOwfPassword AS LM_OWF_PASSWORD
 NtOwfPassword AS NT_OWF_PASSWORD
END TYPE
' // Size = 60 bytes
TYPE NETLOGON_NETWORK_INFO DWORD
 Identity AS NETLOGON_LOGON_IDENTITY_INFO
 LmChallenge AS LM_CHALLENGE
 NtChallengeResponse AS ANSI_STRING
 LmChallengeResponse AS ANSI_STRING
END TYPE
' // Size = 52 bytes
TYPE NETLOGON_GENERIC_INFO DWORD
 Identity AS NETLOGON_LOGON_IDENTITY_INFO
 PackageName AS UNICODE_STRING
 DataLength AS DWORD ' ULONG
 LogonData AS BYTE PTR
END TYPE
'// Values for Flags
%EQUATE `MSV1_0_PASSTHRU = &H01
%EQUATE `MSV1_0_GUEST_LOGON = &H02
DECLARE FUNCTION Msv1_0SubAuthenticationRoutine ALIAS "Msv1_0SubAuthenticationRoutine" ( BYVAL LogonLevel AS LONG , BYREF LogonInformation AS ANY , BYVAL Flags AS QUAD , BYREF UserAll AS USER_ALL_INFORMATION , BYREF WhichFields AS QUAD , BYREF UserFlags AS QUAD , BYREF Authoritative AS BYTE , BYREF LogoffTime AS ANY , BYREF KickoffTime AS ANY ) AS LONG ' NTSTATUS
' // Size = 64 bytes
TYPE MSV1_0_VALIDATION_INFO QWORD FILL ' Must be 8 byte aligned
 LogoffTime AS QUAD ' LARGE_INTEGER
 KickoffTime AS QUAD ' LARGE_INTEGER
 LogonServer AS UNICODE_STRING ' UNICODE_STRING
 LogonDomainName AS UNICODE_STRING ' UNICODE_STRING
 SessionKey AS USER_SESSION_KEY ' USER_SESSION_KEY
 Authoritative AS BYTE ' BOOLEAN
 UserFlags AS DWORD ' ULONG
 WhichFields AS DWORD ' ULONG
 UserId AS DWORD ' ULONG
END TYPE
'// values for WhichFields
%EQUATE `MSV1_0_VALIDATION_LOGOFF_TIME = &H00000001
%EQUATE `MSV1_0_VALIDATION_KICKOFF_TIME = &H00000002
%EQUATE `MSV1_0_VALIDATION_LOGON_SERVER = &H00000004
%EQUATE `MSV1_0_VALIDATION_LOGON_DOMAIN = &H00000008
%EQUATE `MSV1_0_VALIDATION_SESSION_KEY = &H00000010
%EQUATE `MSV1_0_VALIDATION_USER_FLAGS = &H00000020
%EQUATE `MSV1_0_VALIDATION_USER_ID = &H00000040
'// legal values for ActionsPerformed
%EQUATE `MSV1_0_SUBAUTH_ACCOUNT_DISABLED = &H00000001
%EQUATE `MSV1_0_SUBAUTH_PASSWORD = &H00000002
%EQUATE `MSV1_0_SUBAUTH_WORKSTATIONS = &H00000004
%EQUATE `MSV1_0_SUBAUTH_LOGON_HOURS = &H00000008
%EQUATE `MSV1_0_SUBAUTH_ACCOUNT_EXPIRY = &H00000010
%EQUATE `MSV1_0_SUBAUTH_PASSWORD_EXPIRY = &H00000020
%EQUATE `MSV1_0_SUBAUTH_ACCOUNT_TYPE = &H00000040
%EQUATE `MSV1_0_SUBAUTH_LOCKOUT = &H00000080
DECLARE FUNCTION Msv1_0SubAuthenticationRoutineEx ALIAS "Msv1_0SubAuthenticationRoutineEx" ( BYVAL LogonLevel AS LONG , BYREF LogonInformation AS ANY , BYVAL Flags AS QUAD , BYREF UserAll AS USER_ALL_INFORMATION , BYREF UserHandle AS ANY , BYREF ValidationInfo AS MSV1_0_VALIDATION_INFO , BYREF ActionsPerformed AS QUAD ) AS LONG ' NTSTATUS
DECLARE FUNCTION Msv1_0SubAuthenticationRoutineGeneric ALIAS "Msv1_0SubAuthenticationRoutineGeneric" ( BYVAL SubmitBuffer AS QUAD , BYVAL SubmitBufferLength AS QUAD , BYREF ReturnBufferLength AS QUAD , BYREF ReturnBuffer AS ANY ) AS LONG ' NTSTATUS
DECLARE FUNCTION Msv1_0SubAuthenticationFilter ALIAS "Msv1_0SubAuthenticationFilter" ( BYVAL LogonLevel AS LONG , BYVAL LogonInformation AS QUAD , BYVAL Flags AS QUAD , BYREF UserAll AS USER_ALL_INFORMATION , BYREF WhichFields AS QUAD , BYREF UserFlags AS QUAD , BYREF Authoritative AS BYTE , BYREF LogoffTime AS ANY , BYREF KickoffTime AS ANY ) AS LONG ' NTSTATUS
%EQUATE `STATUS_SUCCESS = &H00000000&
%EQUATE `STATUS_INVALID_INFO_CLASS = &HC0000003&
%EQUATE `STATUS_NO_SUCH_USER = &HC0000064&
%EQUATE `STATUS_WRONG_PASSWORD = &HC000006A&
%EQUATE `STATUS_PASSWORD_RESTRICTION = &HC000006C&
%EQUATE `STATUS_LOGON_FAILURE = &HC000006D&
%EQUATE `STATUS_ACCOUNT_RESTRICTION = &HC000006E&
%EQUATE `STATUS_INVALID_LOGON_HOURS = &HC000006F&
%EQUATE `STATUS_INVALID_WORKSTATION = &HC0000070&
%EQUATE `STATUS_PASSWORD_EXPIRED = &HC0000071&
%EQUATE `STATUS_ACCOUNT_DISABLED = &HC0000072&
%EQUATE `STATUS_INSUFFICIENT_RESOURCES = &HC000009A&
%EQUATE `STATUS_ACCOUNT_EXPIRED = &HC0000193&
%EQUATE `STATUS_PASSWORD_MUST_CHANGE = &HC0000224&
%EQUATE `STATUS_ACCOUNT_LOCKED_OUT = &HC0000234&
#ENDIF
