/* ntlm.h  -- interface declarations for SMB authentication code */

#include "smbtypes.h"

/* 
 * These structures are byte-order dependant, and should not
 * be manipulated except by the use of the routines provided
 *
 * Specification at
 * https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/
 */

typedef struct
{
uint16  len;     // 0
uint16  maxlen;  // 2
uint32  offset;  // 4
}tSmbStrHeader;  // 8

typedef struct //NTLM NEGOTIATE_MESSAGE
{
char          ident[8];			// Signature, MUST be "NTLMSSP\0"
uint32        msgType; 			// MessageType, "MUST" be 0x00000001
uint32        flags;   			// NegotiateFlags
tSmbStrHeader    user; 			// DomeNameFields - lcient authentication domain
tSmbStrHeader    domain;		// WorkstationFields
					// Version (2 x 4 Bytes), optional
uint8         buffer[1024];		// Payload (variable)
uint32        bufIndex;
}tSmbNtlmAuthRequest;

typedef struct //NTLM CHALLENGE_MESSAGE
{
char          ident[8];			//    0 Signature
uint32        msgType;			//    8 MessageType
tSmbStrHeader    uDomain;		//   12 TargetNameFields
uint32        flags;			//   20 NegotiateFlags
uint8         challengeData[8];		//   24 ServerChallenge
uint32        context[2];		//   32 Reserved
tSmbStrHeader    targetInfo;		//   40 TargetInfoFields
					//   48 Version (2 x 4 Bytes), optional
uint8         buffer[1024];		//   48 Payload (variable)
uint32        bufIndex;			// 1072
}tSmbNtlmAuthChallenge;			// 1076


typedef struct //NTLM AUTHENTICATE_MESSAGE
{
char          ident[8];			//    0 Signature
uint32        msgType;			//    8 MessageType
tSmbStrHeader    lmResponse;		//   12 LmChallengeResponseFields
tSmbStrHeader    ntResponse;		//   20 NtChallengeResponseFields
tSmbStrHeader    uDomain;		//   28 DomainNameFields
tSmbStrHeader    uUser;			//   36 UserNameFields
tSmbStrHeader    uWks;			//   44 WorkstationFields
tSmbStrHeader    sessionKey;		//   52 EncryptedRandomSessionKeyFields
uint32        flags;			//   60 NegotiateFlags
					//   64 Version (2 x 4 Bytes), optional, MIC (16 Bytes)
uint8         buffer[1024];		//   64 Payload (variable)
uint32        bufIndex;                 // 1088
}tSmbNtlmAuthResponse;                  // 1092

/* public: */

#define SmbLength(ptr) (((ptr)->buffer - (uint8*)(ptr)) + (ptr)->bufIndex)

extern void dumpSmbNtlmAuthRequest(FILE *fp, tSmbNtlmAuthRequest *request);
extern void dumpSmbNtlmAuthChallenge(FILE *fp, tSmbNtlmAuthChallenge *challenge);
extern void dumpSmbNtlmAuthResponse(FILE *fp, tSmbNtlmAuthResponse *response);

extern int buildSmbNtlmAuthRequest(tSmbNtlmAuthRequest *request, char *user, char *domain); /**< returns 0 for success */
extern int buildSmbNtlmAuthResponse(tSmbNtlmAuthChallenge *challenge, tSmbNtlmAuthResponse *response, char *user, char *password); /**< returns 0 for success */

#define NTLM_BUFFER_TOO_SMALL ((int)-2)  // must not overlap PS_SUCCESS, PS_AUTHFAIL or any other PS_*

/* ntlm.h ends here */
