| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 | // This code is derived from jcifs smb client library <jcifs at samba dot org>// Ported by J. Arturo <webmaster at komodosoft dot net>//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//  // This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU// Lesser General Public License for more details.//  // You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USAusing System;using SharpCifs.Util;using SharpCifs.Util.Sharpen;using SharpCifs.Util.Transport;namespace SharpCifs.Smb{	public abstract class ServerMessageBlock: Response	{		internal static LogStream Log = LogStream.GetInstance();        internal static long Ticks1601 = new DateTime(1601, 1, 1).Ticks;		internal static readonly byte[] Header = { 0xFF, (byte)('S'), (byte)('M'), 			(byte)('B'), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };		internal static void WriteInt2(long val, byte[] dst, int dstIndex)		{			dst[dstIndex] = unchecked((byte)(val));			dst[++dstIndex] = unchecked((byte)(val >> 8));		}		internal static void WriteInt4(long val, byte[] dst, int dstIndex)		{			dst[dstIndex] = unchecked((byte)(val));			dst[++dstIndex] = unchecked((byte)(val >>= 8));			dst[++dstIndex] = unchecked((byte)(val >>= 8));			dst[++dstIndex] = unchecked((byte)(val >> 8));		}		internal static int ReadInt2(byte[] src, int srcIndex)		{			return unchecked(src[srcIndex] & 0xFF) + ((src[srcIndex + 1] & 0xFF) << 8);		}		internal static int ReadInt4(byte[] src, int srcIndex)		{			return unchecked(src[srcIndex] & 0xFF) + ((src[srcIndex + 1] & 0xFF) << 8) + ((src[srcIndex + 2]                 & 0xFF) << 16) + ((src[srcIndex + 3] & 0xFF) << 24);		}		internal static long ReadInt8(byte[] src, int srcIndex)		{			return unchecked(ReadInt4(src, srcIndex) & unchecked(0xFFFFFFFFL)) + unchecked((long)(ReadInt4				(src, srcIndex + 4)) << 32);		}		internal static void WriteInt8(long val, byte[] dst, int dstIndex)		{			dst[dstIndex] = unchecked((byte)(val));			dst[++dstIndex] = unchecked((byte)(val >>= 8));			dst[++dstIndex] = unchecked((byte)(val >>= 8));			dst[++dstIndex] = unchecked((byte)(val >>= 8));			dst[++dstIndex] = unchecked((byte)(val >>= 8));			dst[++dstIndex] = unchecked((byte)(val >>= 8));			dst[++dstIndex] = unchecked((byte)(val >>= 8));			dst[++dstIndex] = unchecked((byte)(val >> 8));		}		internal static long ReadTime(byte[] src, int srcIndex)		{			int low = ReadInt4(src, srcIndex);			int hi = ReadInt4(src, srcIndex + 4);			long t = ((long)hi << (int)32L) | (low & unchecked((long)(0xFFFFFFFFL)));			t = (t / 10000L - SmbConstants.MillisecondsBetween1970And1601);			return t;		}		internal static void WriteTime(long t, byte[] dst, int dstIndex)		{			if (t != 0L)			{                t = (t + SmbConstants.MillisecondsBetween1970And1601) * 10000L;			}			WriteInt8(t, dst, dstIndex);		}		internal static long ReadUTime(byte[] buffer, int bufferIndex)		{			return ReadInt4(buffer, bufferIndex) * 1000L;		}		internal static void WriteUTime(long t, byte[] dst, int dstIndex)		{			if (t == 0L || t == unchecked((long)(0xFFFFFFFFFFFFFFFFL)))			{				WriteInt4(unchecked((int)(0xFFFFFFFF)), dst, dstIndex);				return;			}			// t isn't in DST either			WriteInt4((int)(t / 1000L), dst, dstIndex);		}		internal const byte SmbComCreateDirectory = 0x00;		internal const byte SmbComDeleteDirectory = 0x01;		internal const byte SmbComClose = 0x04;		internal const byte SmbComDelete = 0x06;		internal const byte SmbComRename = 0x07;		internal const byte SmbComQueryInformation = 0x08;		internal const byte SmbComWrite = 0x0B;		internal const byte SmbComCheckDirectory = 0x10;		internal const byte SmbComTransaction = 0x25;		internal const byte SmbComTransactionSecondary = 0x26;		internal const byte SmbComMove = 0x2A;		internal const byte SmbComEcho = 0x2B;		internal const byte SmbComOpenAndx = 0x2D;		internal const byte SmbComReadAndx = 0x2E;		internal const byte SmbComWriteAndx = 0x2F;	    internal const byte SmbComTransaction2 = 0x32;					internal const byte SmbComFindClose2 = 0x34;		internal const byte SmbComTreeDisconnect = 0x71;		internal const byte SmbComNegotiate = 0x72;		internal const byte SmbComSessionSetupAndx = 0x73;		internal const byte SmbComLogoffAndx = 0x74;		internal const byte SmbComTreeConnectAndx = 0x75;		internal const byte SmbComNtTransact = 0xA0;		internal const byte SmbComNtTransactSecondary = 0xA1;		internal const byte SmbComNtCreateAndx = 0xA2;		internal byte Command;		internal byte Flags;		internal int HeaderStart;		internal int Length;		internal int BatchLevel;		internal int ErrorCode;		internal int Flags2;		internal int Tid;		internal int Pid;		internal int Uid;		internal int Mid;		internal int WordCount;		internal int ByteCount;		internal bool UseUnicode;		internal bool Received;		internal bool ExtendedSecurity;		internal long ResponseTimeout = 1;		internal int SignSeq;		internal bool VerifyFailed;		internal NtlmPasswordAuthentication Auth = null;		internal string Path;		internal SigningDigest Digest;		internal ServerMessageBlock Response;		public ServerMessageBlock()		{			Flags = unchecked((byte)(SmbConstants.FlagsPathNamesCaseless | SmbConstants.FlagsPathNamesCanonicalized				));			Pid = SmbConstants.Pid;			BatchLevel = 0;		}		internal virtual void Reset()		{            Flags = unchecked((byte)(SmbConstants.FlagsPathNamesCaseless | SmbConstants.FlagsPathNamesCanonicalized				));			Flags2 = 0;			ErrorCode = 0;			Received = false;			Digest = null;		}		internal virtual int WriteString(string str, byte[] dst, int dstIndex)		{			return WriteString(str, dst, dstIndex, UseUnicode);		}		internal virtual int WriteString(string str, byte[] dst, int dstIndex, bool useUnicode			)		{			int start = dstIndex;			try			{				if (useUnicode)				{					// Unicode requires word alignment					if (((dstIndex - HeaderStart) % 2) != 0)					{						dst[dstIndex++] = (byte)('\0');					}                    Array.Copy(Runtime.GetBytesForString(str, SmbConstants.UniEncoding), 0, dst, dstIndex						, str.Length * 2);					dstIndex += str.Length * 2;					dst[dstIndex++] = (byte)('\0');					dst[dstIndex++] = (byte)('\0');				}				else				{                    byte[] b = Runtime.GetBytesForString(str, SmbConstants.OemEncoding);					Array.Copy(b, 0, dst, dstIndex, b.Length);					dstIndex += b.Length;					dst[dstIndex++] = (byte)('\0');				}			}			catch (UnsupportedEncodingException uee)			{				if (Log.Level > 1)				{					Runtime.PrintStackTrace(uee, Log);				}			}			return dstIndex - start;		}		internal virtual string ReadString(byte[] src, int srcIndex)		{			return ReadString(src, srcIndex, 256, UseUnicode);		}		internal virtual string ReadString(byte[] src, int srcIndex, int maxLen, bool useUnicode			)		{			int len = 0;			string str = null;			try			{				if (useUnicode)				{					// Unicode requires word alignment					if (((srcIndex - HeaderStart) % 2) != 0)					{						srcIndex++;					}					while (src[srcIndex + len] != 0x00 || src[srcIndex						 + len + 1] != 0x00)					{						len += 2;						if (len > maxLen)						{							if (Log.Level > 0)							{								Hexdump.ToHexdump(Console.Error, src, srcIndex, maxLen < 128 ? maxLen + 8 : 									128);							}							throw new RuntimeException("zero termination not found");						}					}                    str = Runtime.GetStringForBytes(src, srcIndex, len, SmbConstants.UniEncoding);				}				else				{					while (src[srcIndex + len] != 0x00)					{						len++;						if (len > maxLen)						{							if (Log.Level > 0)							{								Hexdump.ToHexdump(Console.Error, src, srcIndex, maxLen < 128 ? maxLen + 8 : 									128);							}							throw new RuntimeException("zero termination not found");						}					}                    str = Runtime.GetStringForBytes(src, srcIndex, len, SmbConstants.OemEncoding);				}			}			catch (UnsupportedEncodingException uee)			{				if (Log.Level > 1)				{					Runtime.PrintStackTrace(uee, Log);				}			}			return str;		}		internal virtual string ReadString(byte[] src, int srcIndex, int srcEnd, int maxLen			, bool useUnicode)		{			int len = 0;			string str = null;			try			{				if (useUnicode)				{					// Unicode requires word alignment					if (((srcIndex - HeaderStart) % 2) != 0)					{						srcIndex++;					}					for (len = 0; (srcIndex + len + 1) < srcEnd; len += 2)					{						if (src[srcIndex + len] == 0x00 && src[srcIndex							 + len + 1] == 0x00)						{							break;						}						if (len > maxLen)						{							if (Log.Level > 0)							{								Hexdump.ToHexdump(Console.Error, src, srcIndex, maxLen < 128 ? maxLen + 8 : 									128);							}							throw new RuntimeException("zero termination not found");						}					}                    str = Runtime.GetStringForBytes(src, srcIndex, len, SmbConstants.UniEncoding);				}				else				{					for (len = 0; srcIndex < srcEnd; len++)					{						if (src[srcIndex + len] == 0x00)						{							break;						}						if (len > maxLen)						{							if (Log.Level > 0)							{								Hexdump.ToHexdump(Console.Error, src, srcIndex, maxLen < 128 ? maxLen + 8 : 									128);							}							throw new RuntimeException("zero termination not found");						}					}                    str = Runtime.GetStringForBytes(src, srcIndex, len, SmbConstants.OemEncoding);				}			}			catch (UnsupportedEncodingException uee)			{				if (Log.Level > 1)				{					Runtime.PrintStackTrace(uee, Log);				}			}			return str;		}		internal virtual int StringWireLength(string str, int offset)		{			int len = str.Length + 1;			if (UseUnicode)			{				len = str.Length * 2 + 2;				len = (offset % 2) != 0 ? len + 1 : len;			}			return len;		}		internal virtual int ReadStringLength(byte[] src, int srcIndex, int max)		{			int len = 0;			while (src[srcIndex + len] != 0x00)			{				if (len++ > max)				{					throw new RuntimeException("zero termination not found: " + this);				}			}			return len;		}		internal virtual int Encode(byte[] dst, int dstIndex)		{			int start = HeaderStart = dstIndex;			dstIndex += WriteHeaderWireFormat(dst, dstIndex);			WordCount = WriteParameterWordsWireFormat(dst, dstIndex + 1);			dst[dstIndex++] = unchecked((byte)((WordCount / 2) & 0xFF));			dstIndex += WordCount;			WordCount /= 2;			ByteCount = WriteBytesWireFormat(dst, dstIndex + 2);			dst[dstIndex++] = unchecked((byte)(ByteCount & 0xFF));			dst[dstIndex++] = unchecked((byte)((ByteCount >> 8) & 0xFF));			dstIndex += ByteCount;			Length = dstIndex - start;			if (Digest != null)			{				Digest.Sign(dst, HeaderStart, Length, this, Response);			}			return Length;		}		internal virtual int Decode(byte[] buffer, int bufferIndex)		{			int start = HeaderStart = bufferIndex;			bufferIndex += ReadHeaderWireFormat(buffer, bufferIndex);			WordCount = buffer[bufferIndex++];			if (WordCount != 0)			{				int n;				if ((n = ReadParameterWordsWireFormat(buffer, bufferIndex)) != WordCount * 2)				{					if (Log.Level >= 5)					{						Log.WriteLine("wordCount * 2=" + (WordCount * 2) + " but readParameterWordsWireFormat returned "							 + n);					}				}				bufferIndex += WordCount * 2;			}			ByteCount = ReadInt2(buffer, bufferIndex);			bufferIndex += 2;			if (ByteCount != 0)			{				int n;				if ((n = ReadBytesWireFormat(buffer, bufferIndex)) != ByteCount)				{					if (Log.Level >= 5)					{						Log.WriteLine("byteCount=" + ByteCount + " but readBytesWireFormat returned " + n							);					}				}				// Don't think we can rely on n being correct here. Must use byteCount.				// Last paragraph of section 3.13.3 eludes to this.				bufferIndex += ByteCount;			}			Length = bufferIndex - start;			return Length;		}		internal virtual int WriteHeaderWireFormat(byte[] dst, int dstIndex)		{			Array.Copy(Header, 0, dst, dstIndex, Header.Length);            dst[dstIndex + SmbConstants.CmdOffset] = Command;            dst[dstIndex + SmbConstants.FlagsOffset] = Flags;            WriteInt2(Flags2, dst, dstIndex + SmbConstants.FlagsOffset + 1);            dstIndex += SmbConstants.TidOffset;			WriteInt2(Tid, dst, dstIndex);			WriteInt2(Pid, dst, dstIndex + 2);			WriteInt2(Uid, dst, dstIndex + 4);			WriteInt2(Mid, dst, dstIndex + 6);            return SmbConstants.HeaderLength;		}		internal virtual int ReadHeaderWireFormat(byte[] buffer, int bufferIndex)		{            Command = buffer[bufferIndex + SmbConstants.CmdOffset];            ErrorCode = ReadInt4(buffer, bufferIndex + SmbConstants.ErrorCodeOffset);            Flags = buffer[bufferIndex + SmbConstants.FlagsOffset];            Flags2 = ReadInt2(buffer, bufferIndex + SmbConstants.FlagsOffset + 1);            Tid = ReadInt2(buffer, bufferIndex + SmbConstants.TidOffset);            Pid = ReadInt2(buffer, bufferIndex + SmbConstants.TidOffset + 2);            Uid = ReadInt2(buffer, bufferIndex + SmbConstants.TidOffset + 4);            Mid = ReadInt2(buffer, bufferIndex + SmbConstants.TidOffset + 6);            return SmbConstants.HeaderLength;		}		internal virtual bool IsResponse()		{            return (Flags & SmbConstants.FlagsResponse) == SmbConstants.FlagsResponse;		}		internal abstract int WriteParameterWordsWireFormat(byte[] dst, int dstIndex);		internal abstract int WriteBytesWireFormat(byte[] dst, int dstIndex);		internal abstract int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex			);		internal abstract int ReadBytesWireFormat(byte[] buffer, int bufferIndex);		public override int GetHashCode()		{			return Mid;		}		public override bool Equals(object obj)		{			return obj is ServerMessageBlock && ((ServerMessageBlock)obj)				.Mid == Mid;		}		public override string ToString()		{			string c;			switch (Command)			{				case SmbComNegotiate:				{					c = "SMB_COM_NEGOTIATE";					break;				}				case SmbComSessionSetupAndx:				{					c = "SMB_COM_SESSION_SETUP_ANDX";					break;				}				case SmbComTreeConnectAndx:				{					c = "SMB_COM_TREE_CONNECT_ANDX";					break;				}				case SmbComQueryInformation:				{					c = "SMB_COM_QUERY_INFORMATION";					break;				}				case SmbComCheckDirectory:				{					c = "SMB_COM_CHECK_DIRECTORY";					break;				}				case SmbComTransaction:				{					c = "SMB_COM_TRANSACTION";					break;				}				case SmbComTransaction2:				{					c = "SMB_COM_TRANSACTION2";					break;				}				case SmbComTransactionSecondary:				{					c = "SMB_COM_TRANSACTION_SECONDARY";					break;				}				case SmbComFindClose2:				{					c = "SMB_COM_FIND_CLOSE2";					break;				}				case SmbComTreeDisconnect:				{					c = "SMB_COM_TREE_DISCONNECT";					break;				}				case SmbComLogoffAndx:				{					c = "SMB_COM_LOGOFF_ANDX";					break;				}				case SmbComEcho:				{					c = "SMB_COM_ECHO";					break;				}				case SmbComMove:				{					c = "SMB_COM_MOVE";					break;				}				case SmbComRename:				{					c = "SMB_COM_RENAME";					break;				}				case SmbComDelete:				{					c = "SMB_COM_DELETE";					break;				}				case SmbComDeleteDirectory:				{					c = "SMB_COM_DELETE_DIRECTORY";					break;				}				case SmbComNtCreateAndx:				{					c = "SMB_COM_NT_CREATE_ANDX";					break;				}				case SmbComOpenAndx:				{					c = "SMB_COM_OPEN_ANDX";					break;				}				case SmbComReadAndx:				{					c = "SMB_COM_READ_ANDX";					break;				}				case SmbComClose:				{					c = "SMB_COM_CLOSE";					break;				}				case SmbComWriteAndx:				{					c = "SMB_COM_WRITE_ANDX";					break;				}				case SmbComCreateDirectory:				{					c = "SMB_COM_CREATE_DIRECTORY";					break;				}				case SmbComNtTransact:				{					c = "SMB_COM_NT_TRANSACT";					break;				}				case SmbComNtTransactSecondary:				{					c = "SMB_COM_NT_TRANSACT_SECONDARY";					break;				}				default:				{					c = "UNKNOWN";					break;				}			}			string str = ErrorCode == 0 ? "0" : SmbException.GetMessageByCode(ErrorCode);			return "command=" + c + ",received=" + Received + ",errorCode=" + str 				+ ",flags=0x" + Hexdump.ToHexString(Flags & 0xFF, 4) + ",flags2=0x"				 + Hexdump.ToHexString(Flags2, 4) + ",signSeq=" + SignSeq + ",tid=" + Tid + ",pid="				 + Pid + ",uid=" + Uid + ",mid=" + Mid + ",wordCount=" + WordCount + ",byteCount="				 + ByteCount;		}	}}
 |