/// <exception cref="SmbLibraryStd.UnsupportedInformationLevelException"></exception>
        public static FileInformationClass ToFileInformationClass(FindInformationLevel informationLevel)
        {
            switch (informationLevel)
            {
            case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
                return(FileInformationClass.FileDirectoryInformation);

            case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
                return(FileInformationClass.FileFullDirectoryInformation);

            case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
                return(FileInformationClass.FileNamesInformation);

            case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
                return(FileInformationClass.FileBothDirectoryInformation);

            case FindInformationLevel.SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
                return(FileInformationClass.FileIdFullDirectoryInformation);

            case FindInformationLevel.SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
                return(FileInformationClass.FileIdBothDirectoryInformation);

            default:
                throw new UnsupportedInformationLevelException();
            }
        }
 /// <summary>
 /// Constructor: Create a request directly from a buffer.
 /// </summary>
 public SmbTrans2FindFirst2FinalResponsePacket(FindInformationLevel informationLevel,
                                               bool isResumeKeyExisted)
 {
     this.InitDefaultValue();
     this.informationLevel   = informationLevel;
     this.isResumeKeyExisted = isResumeKeyExisted;
 }
 /// <summary>
 /// Constructor: Create a request directly from a buffer.
 /// </summary>
 public SmbTrans2FindFirst2FinalResponsePacket(FindInformationLevel informationLevel,
     bool isResumeKeyExisted)
 {
     this.InitDefaultValue();
     this.informationLevel = informationLevel;
     this.isResumeKeyExisted = isResumeKeyExisted;
 }
Beispiel #4
0
        public static FindInformation ReadEntry(byte[] buffer, int offset, FindInformationLevel informationLevel, bool isUnicode)
        {
            switch (informationLevel)
            {
            case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
                return(new FindFileDirectoryInfo(buffer, offset, isUnicode));

            case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
                return(new FindFileFullDirectoryInfo(buffer, offset, isUnicode));

            case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
                return(new FindFileNamesInfo(buffer, offset, isUnicode));

            case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
                return(new FindFileBothDirectoryInfo(buffer, offset, isUnicode));

            case FindInformationLevel.SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
                return(new FindFileIDFullDirectoryInfo(buffer, offset, isUnicode));

            case FindInformationLevel.SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
                return(new FindFileIDBothDirectoryInfo(buffer, offset, isUnicode));

            default:
                throw new UnsupportedInformationLevelException();
            }
        }
Beispiel #5
0
        private static FileInformationClass GetFileInformationClass(FindInformationLevel informationLevel)
        {
            switch (informationLevel)
            {
            case FindInformationLevel.SMB_INFO_STANDARD:
                return(FileInformationClass.FileDirectoryInformation);

            case FindInformationLevel.SMB_INFO_QUERY_EA_SIZE:
                return(FileInformationClass.FileFullDirectoryInformation);

            case FindInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST:
                return(FileInformationClass.FileDirectoryInformation);

            case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
                return(FileInformationClass.FileDirectoryInformation);

            case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
                return(FileInformationClass.FileFullDirectoryInformation);

            case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
                return(FileInformationClass.FileNamesInformation);

            case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
                return(FileInformationClass.FileBothDirectoryInformation);

            default:
                throw new UnsupportedInformationLevelException();
            }
        }
        public static FindInformationEntry ReadEntry(byte[] buffer, ref int offset, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys)
        {
            switch (informationLevel)
            {
            case FindInformationLevel.SMB_INFO_STANDARD:
                return(new FindInfoStandard(buffer, ref offset, isUnicode, returnResumeKeys));

            case FindInformationLevel.SMB_INFO_QUERY_EA_SIZE:
                return(new FindInfoQueryEASize(buffer, ref offset, isUnicode, returnResumeKeys));

            case FindInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST:
                return(new FindInfoQueryExtendedAttributesFromList(buffer, ref offset, isUnicode, returnResumeKeys));

            case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
                return(new FindFileDirectoryInfo(buffer, ref offset, isUnicode));

            case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
                return(new FindFileFullDirectoryInfo(buffer, ref offset, isUnicode));

            case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
                return(new FindFileNamesInfo(buffer, ref offset, isUnicode));

            case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
                return(new FindFileBothDirectoryInfo(buffer, ref offset, isUnicode));

            default:
                throw new InvalidRequestException();;
            }
        }
Beispiel #7
0
 public Transaction2FindNext2Request(byte[] parameters, byte[] data, bool isUnicode) : base()
 {
     SID              = LittleEndianConverter.ToUInt16(parameters, 0);
     SearchCount      = LittleEndianConverter.ToUInt16(parameters, 2);
     InformationLevel = (FindInformationLevel)LittleEndianConverter.ToUInt16(parameters, 4);
     ResumeKey        = LittleEndianConverter.ToUInt32(parameters, 6);
     Flags            = (FindFlags)LittleEndianConverter.ToUInt16(parameters, 10);
     FileName         = SMBHelper.ReadSMBString(parameters, 12, isUnicode);
 }
Beispiel #8
0
        public Transaction2FindNext2Response(byte[] parameters, byte[] data, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys) : base()
        {
            SearchCount    = LittleEndianConverter.ToUInt16(parameters, 0);
            EndOfSearch    = LittleEndianConverter.ToUInt16(parameters, 2) != 0;
            EaErrorOffset  = LittleEndianConverter.ToUInt16(parameters, 4);
            LastNameOffset = LittleEndianConverter.ToUInt16(parameters, 6);

            FindInfoList = new FindInformation(data, informationLevel, isUnicode, returnResumeKeys);
        }
Beispiel #9
0
        public FindInformation(byte[] buffer, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys)
        {
            int offset = 0;

            while (offset < buffer.Length)
            {
                FindInformationEntry entry = FindInformationEntry.ReadEntry(buffer, ref offset, informationLevel, isUnicode, returnResumeKeys);
                this.Add(entry);
            }
        }
        public FindInformationList(byte[] buffer, FindInformationLevel informationLevel, bool isUnicode)
        {
            int offset = 0;

            while (offset < buffer.Length)
            {
                FindInformation entry = FindInformation.ReadEntry(buffer, offset, informationLevel, isUnicode);
                this.Add(entry);
                offset += (int)entry.NextEntryOffset;
            }
        }
 /// <exception cref="UnsupportedInformationLevelException"></exception>
 public static FileInformationClass ToFileInformationClass(FindInformationLevel informationLevel)
 {
     return(informationLevel switch
     {
         FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO => FileInformationClass.FileDirectoryInformation,
         FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO => FileInformationClass.FileFullDirectoryInformation,
         FindInformationLevel.SMB_FIND_FILE_NAMES_INFO => FileInformationClass.FileNamesInformation,
         FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO => FileInformationClass.FileBothDirectoryInformation,
         FindInformationLevel.SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO => FileInformationClass.FileIdFullDirectoryInformation,
         FindInformationLevel.SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO => FileInformationClass.FileIdBothDirectoryInformation,
         _ => throw new UnsupportedInformationLevelException()
     });
        /// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbTrans2FindNext2FinalResponsePacket(SmbTrans2FindNext2FinalResponsePacket packet)
            : base(packet)
        {
            this.InitDefaultValue();

            this.trans2Parameters.SearchCount = packet.trans2Parameters.SearchCount;
            this.trans2Parameters.EndOfSearch = packet.trans2Parameters.EndOfSearch;
            this.trans2Parameters.EaErrorOffset = packet.trans2Parameters.EaErrorOffset;
            this.trans2Parameters.LastNameOffset = packet.trans2Parameters.LastNameOffset;

            this.trans2Data.Data = packet.trans2Data.Data;
            this.informationLevel = packet.informationLevel;
            this.isResumeKeyExisted = packet.isResumeKeyExisted;
        }
        /// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbTrans2FindNext2FinalResponsePacket(SmbTrans2FindNext2FinalResponsePacket packet)
            : base(packet)
        {
            this.InitDefaultValue();

            this.trans2Parameters.SearchCount    = packet.trans2Parameters.SearchCount;
            this.trans2Parameters.EndOfSearch    = packet.trans2Parameters.EndOfSearch;
            this.trans2Parameters.EaErrorOffset  = packet.trans2Parameters.EaErrorOffset;
            this.trans2Parameters.LastNameOffset = packet.trans2Parameters.LastNameOffset;

            this.trans2Data.Data    = packet.trans2Data.Data;
            this.informationLevel   = packet.informationLevel;
            this.isResumeKeyExisted = packet.isResumeKeyExisted;
        }
Beispiel #14
0
        public Transaction2FindFirst2Request(byte[] parameters, byte[] data, bool isUnicode) : base()
        {
            SearchAttributes  = (FileAttributes)LittleEndianConverter.ToUInt16(parameters, 0);
            SearchCount       = LittleEndianConverter.ToUInt16(parameters, 2);
            Flags             = (FindFlags)LittleEndianConverter.ToUInt16(parameters, 4);
            InformationLevel  = (FindInformationLevel)LittleEndianConverter.ToUInt16(parameters, 6);
            SearchStorageType = (SearchStorageType)LittleEndianConverter.ToUInt32(parameters, 8);
            FileName          = SMBHelper.ReadSMBString(parameters, 12, isUnicode);

            if (InformationLevel == FindInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST)
            {
                GetExtendedAttributeList = new FullExtendedAttributeList(data, 0);
            }
        }
Beispiel #15
0
        public Transaction2FindNext2Request(byte[] parameters, byte[] data, bool isUnicode) : base()
        {
            SID              = LittleEndianConverter.ToUInt16(parameters, 0);
            SearchCount      = LittleEndianConverter.ToUInt16(parameters, 2);
            InformationLevel = (FindInformationLevel)LittleEndianConverter.ToUInt16(parameters, 4);
            ResumeKey        = LittleEndianConverter.ToUInt32(parameters, 6);
            Flags            = (FindFlags)LittleEndianConverter.ToUInt16(parameters, 10);
            FileName         = SMB1Helper.ReadSMBString(parameters, 12, isUnicode);

            if (InformationLevel == FindInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST)
            {
                GetExtendedAttributeList = new ExtendedAttributeNameList(data, 0);
            }
        }
Beispiel #16
0
 public static FindInformation ReadEntry(byte[] buffer, int offset, FindInformationLevel informationLevel, bool isUnicode)
 {
     return(informationLevel switch
     {
         FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO => new FindFileDirectoryInfo(buffer, offset,
                                                                                        isUnicode),
         FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO => new FindFileFullDirectoryInfo(buffer, offset,
                                                                                                 isUnicode),
         FindInformationLevel.SMB_FIND_FILE_NAMES_INFO => new FindFileNamesInfo(buffer, offset, isUnicode),
         FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO => new FindFileBothDirectoryInfo(buffer, offset,
                                                                                                 isUnicode),
         FindInformationLevel.SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO => new FindFileIDFullDirectoryInfo(buffer,
                                                                                                      offset, isUnicode),
         FindInformationLevel.SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO => new FindFileIDBothDirectoryInfo(buffer,
                                                                                                      offset, isUnicode),
         _ => throw new UnsupportedInformationLevelException()
     });
Beispiel #17
0
        public Transaction2FindNext2Request(byte[] parameters, byte[] data, bool isUnicode) : base()
        {
            SID              = LittleEndianConverter.ToUInt16(parameters, 0);
            SearchCount      = LittleEndianConverter.ToUInt16(parameters, 2);
            InformationLevel = (FindInformationLevel)LittleEndianConverter.ToUInt16(parameters, 4);
            ResumeKey        = LittleEndianConverter.ToUInt32(parameters, 6);
            Flags            = (FindFlags)LittleEndianConverter.ToUInt16(parameters, 10);

            if (isUnicode && parameters.Length == 13 && parameters[12] == 0x00)
            {
                // Workaround for PlayStation2 Slim
                FileName = String.Empty;
            }
            else
            {
                FileName = SMB1Helper.ReadSMBString(parameters, 12, isUnicode);
            }


            if (InformationLevel == FindInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST)
            {
                GetExtendedAttributeList = new ExtendedAttributeNameList(data, 0);
            }
        }
 /// <summary>
 /// Create Trans2FindNext2 request for client to find next file on server. 
 /// </summary>
 /// <param name = "messageId">the id of message, used to identity the request and the server response. </param>
 /// <param name = "sessionUid">the valid session id, must be response by server of the session setup request. </param>
 /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
 /// <param name = "flags">
 /// The Flags field contains individual flags, as specified in [CIFS] sections 2.4.2 and 3.1.1. 
 /// </param>
 /// <param name = "flags2">
 /// The Flags2 field contains individual bit flags that, depending on the negotiated SMB dialect, indicate   
 /// various client and server capabilities. 
 /// </param>
 /// <param name = "fileName">The file name to find. </param>
 /// <param name = "transactOptions">
 /// A set of bit flags that alter the behavior of the requested operation. Unused bit fields MUST be set to  
 /// zero by the client sending the request, and MUST be ignored by the server receiving the request. The 
 /// client MAY set either or both of the following bit flags 
 /// </param>
 /// <param name = "timeOut">
 /// The maximum amount of time in milliseconds to wait for the operation to complete. The client SHOULD set  
 /// this to 0 to indicate that no time-out is given. If the operation does not complete within the specified  
 /// time, the server MAY abort the request and send a failure response. 
 /// </param>
 /// <param name = "searchCount">
 /// The server MUST NOT return more entries than indicated by the value of this field. 
 /// </param>
 /// <param name = "findFlags">
 /// This 16-bit field of flags is used to request that the server take certain actions. 
 /// </param>
 /// <param name = "sid">used to set the field Trans2FindNext2RequestHeader.Sid (Search handle). </param>
 /// <param name = "resumeKey">
 /// This field MUST be the value of a ResumeKey field returned in the response from a TRANS2_FIND_FIRST2 or  
 /// TRANS2_FIND_NEXT2 that is part of the same search (same SID). 
 /// </param>
 /// <param name = "informationLevel">
 /// Indicates that client specifies the information it is requesting. Server return different data based on 
 /// the client's request. 
 /// </param>
 /// <returns>a find file the next request packet </returns>
 private SmbTrans2FindNext2RequestPacket CreateTrans2FindNext2Request(
     ushort messageId,
     ushort sessionUid,
     ushort treeId,
     SmbHeader_Flags_Values flags,
     SmbHeader_Flags2_Values flags2,
     string fileName,
     Trans2SmbParametersFlags transactOptions,
     uint timeOut,
     ushort searchCount,
     ushort sid,
     uint resumeKey,
     Trans2FindFlags findFlags,
     FindInformationLevel informationLevel)
 {
     return new SmbTrans2FindNext2RequestPacket(
         this.cifsClient.CreateTrans2FindNext2Request(
         messageId, sessionUid, treeId, (SmbFlags)flags, (SmbFlags2)flags2,
         this.capability.MaxParameterCount, this.capability.MaxDataCount, this.capability.MaxSetupCount,
         transactOptions, timeOut, sid, searchCount, (Cifs.FindInformationLevel)informationLevel, resumeKey,
         findFlags, fileName, null));
 }
 public FindInformationList GetFindInformationList(FindInformationLevel findInformationLevel, bool isUnicode, bool returnResumeKeys)
 {
     return(new FindInformationList(FindInformationListBytes, findInformationLevel, isUnicode, returnResumeKeys));
 }
 public FindInformationList GetFindInformationList(FindInformationLevel findInformationLevel, bool isUnicode)
 {
     return(new FindInformationList(FindInformationListBytes, findInformationLevel, isUnicode));
 }
 /// <summary>
 /// to create a Trans2FindFirst2 request packet.
 /// </summary>
 /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
 /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
 /// accessing.</param>
 /// <param name="extendedAttributeList">A list of extended file attribute name/value pairs. </param>
 /// <param name="searchCount">The server MUST NOT return more entries than indicated by the value of this
 /// field.</param>
 /// <param name="searchAttributes">File attributes to apply as a constraint to the file search.</param>
 /// <param name="findFlags">This 16-bit field of flags is used to request that the server take
 /// certain actions.</param>
 /// <param name="informationLevel">This field contains an information level code, which determines the
 /// information contained in the response.</param>
 /// <param name="searchStorageType">This field specifies if the find is searching for directories or for files.
 /// This field MUST be one of two values</param>
 /// <param name="name">A buffer containing the name of the file to be opened, created, or truncated. The
 /// string MUST be null terminated</param>
 /// <returns>a Trans2FindFirst2 request packet</returns>
 /// <exception cref="System.NullReferenceException">There is no connection in context. </exception>
 public SmbTrans2FindFirst2RequestPacket CreateTrans2FindFirst2Request(
     ushort uid,
     ushort treeId,
     SmbFileAttributes searchAttributes,
     Trans2FindFlags findFlags,
     ushort searchCount,
     FindInformationLevel informationLevel,
     Trans2FindFirst2SearchStorageType searchStorageType,
     string name,
     SMB_GEA[] extendedAttributeList)
 {
     return this.CreateTrans2FindFirst2Request(this.Context.GetMessageId(this.connectionId),
         uid, treeId, this.defaultParameters.Flag, this.defaultParameters.Flag2, this.defaultParameters.MaxParameterCount,
         this.defaultParameters.MaxDataCount, this.defaultParameters.MaxSetupCount, this.defaultParameters.Trans2SmbParametersFlags,
         this.defaultParameters.Timeout, searchAttributes, searchCount, findFlags, informationLevel,
         searchStorageType, name, extendedAttributeList);
 }
        /// <summary>
        /// convert data to SMB_FIND_FILE_BOTH_DIRECTORY_INFO array.
        /// </summary>
        /// <param name="informationLevel">the information level</param>
        /// <param name="arraySize">the array size</param>
        /// <param name="data">the data</param>
        /// <returns>the unmarshaled result</returns>
        public static object UnmarshalSmbFindInformationLevelPayloads(
            FindInformationLevel informationLevel,
            int arraySize, byte[] data)
        {
            switch ((FindInformationLevel)informationLevel)
            {
                case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
                    {
                        SMB_FIND_FILE_BOTH_DIRECTORY_INFO[] result =
                            new SMB_FIND_FILE_BOTH_DIRECTORY_INFO[arraySize];

                        using (MemoryStream memoryStream = new MemoryStream(data))
                        {
                            using (Channel channel = new Channel(null, memoryStream))
                            {
                                for (int i = 0; i < result.Length; i++)
                                {
                                    result[i] = channel.Read<SMB_FIND_FILE_BOTH_DIRECTORY_INFO>();
                                    int pad = Convert.ToInt32(result[i].NextEntryOffset -
                                        CifsMessageUtils.GetSize<SMB_FIND_FILE_BOTH_DIRECTORY_INFO>(result[i]));
                                    if (pad > 0)
                                    {
                                        channel.ReadBytes(pad);
                                    }
                                }
                            }
                        }

                        return result;
                    }

                case FindInformationLevel.SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
                    {
                        SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO[] result =
                            new SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO[arraySize];

                        using (MemoryStream memoryStream = new MemoryStream(data))
                        {
                            using (Channel channel = new Channel(null, memoryStream))
                            {
                                for (int i = 0; i < result.Length; i++)
                                {
                                    result[i] = channel.Read<SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO>();
                                    int pad = Convert.ToInt32(result[i].NextEntryOffset -
                                        CifsMessageUtils.GetSize<SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO>(result[i]));
                                    if (pad > 0)
                                    {
                                        channel.ReadBytes(pad);
                                    }
                                }
                            }
                        }

                        return result;
                    }

                case FindInformationLevel.SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
                    {

                        SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO[] result =
                            new SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO[arraySize];

                        using (MemoryStream memoryStream = new MemoryStream(data))
                        {
                            using (Channel channel = new Channel(null, memoryStream))
                            {
                                for (int i = 0; i < result.Length; i++)
                                {
                                    result[i] = channel.Read<SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO>();
                                    int pad = Convert.ToInt32(result[i].NextEntryOffset -
                                        CifsMessageUtils.GetSize<SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO>(result[i]));
                                    if (pad > 0)
                                    {
                                        channel.ReadBytes(pad);
                                    }
                                }
                            }
                        }
                        return result;
                    }

                default:

                    return null;
            }
        }
Beispiel #23
0
        /// <summary>
        /// convert data to SMB_FIND_FILE_BOTH_DIRECTORY_INFO array.
        /// </summary>
        /// <param name="informationLevel">the information level</param>
        /// <param name="arraySize">the array size</param>
        /// <param name="data">the data</param>
        /// <returns>the unmarshaled result</returns>
        public static object UnmarshalSmbFindInformationLevelPayloads(
            FindInformationLevel informationLevel,
            int arraySize, byte[] data)
        {
            switch ((FindInformationLevel)informationLevel)
            {
            case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
            {
                SMB_FIND_FILE_BOTH_DIRECTORY_INFO[] result =
                    new SMB_FIND_FILE_BOTH_DIRECTORY_INFO[arraySize];

                using (MemoryStream memoryStream = new MemoryStream(data))
                {
                    using (Channel channel = new Channel(null, memoryStream))
                    {
                        for (int i = 0; i < result.Length; i++)
                        {
                            result[i] = channel.Read <SMB_FIND_FILE_BOTH_DIRECTORY_INFO>();
                            int pad = Convert.ToInt32(result[i].NextEntryOffset -
                                                      CifsMessageUtils.GetSize <SMB_FIND_FILE_BOTH_DIRECTORY_INFO>(result[i]));
                            if (pad > 0)
                            {
                                channel.ReadBytes(pad);
                            }
                        }
                    }
                }

                return(result);
            }

            case FindInformationLevel.SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
            {
                SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO[] result =
                    new SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO[arraySize];

                using (MemoryStream memoryStream = new MemoryStream(data))
                {
                    using (Channel channel = new Channel(null, memoryStream))
                    {
                        for (int i = 0; i < result.Length; i++)
                        {
                            result[i] = channel.Read <SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO>();
                            int pad = Convert.ToInt32(result[i].NextEntryOffset -
                                                      CifsMessageUtils.GetSize <SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO>(result[i]));
                            if (pad > 0)
                            {
                                channel.ReadBytes(pad);
                            }
                        }
                    }
                }

                return(result);
            }

            case FindInformationLevel.SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
            {
                SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO[] result =
                    new SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO[arraySize];

                using (MemoryStream memoryStream = new MemoryStream(data))
                {
                    using (Channel channel = new Channel(null, memoryStream))
                    {
                        for (int i = 0; i < result.Length; i++)
                        {
                            result[i] = channel.Read <SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO>();
                            int pad = Convert.ToInt32(result[i].NextEntryOffset -
                                                      CifsMessageUtils.GetSize <SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO>(result[i]));
                            if (pad > 0)
                            {
                                channel.ReadBytes(pad);
                            }
                        }
                    }
                }
                return(result);
            }

            default:

                return(null);
            }
        }
Beispiel #24
0
        public void QueryDirectory(out List <FindInformation>?result, string fileName, FindInformationLevel informationLevel)
        {
            result = null;
            int maxOutputLength = 4096;
            Transaction2FindFirst2Request subCommand = new Transaction2FindFirst2Request
            {
                SearchAttributes = SMBFileAttributes.Hidden | SMBFileAttributes.System | SMBFileAttributes.Directory,
                SearchCount      = ushort.MaxValue,
                Flags            = FindFlags.SMB_FIND_CLOSE_AT_EOS,
                InformationLevel = informationLevel,
                FileName         = fileName
            };

            Transaction2Request request = new Transaction2Request
            {
                Setup           = subCommand.GetSetup(),
                TransParameters = subCommand.GetParameters(m_client.Unicode),
                TransData       = subCommand.GetData(m_client.Unicode)
            };

            request.TotalDataCount      = (ushort)request.TransData.Length;
            request.TotalParameterCount = (ushort)request.TransParameters.Length;
            request.MaxParameterCount   = Transaction2FindFirst2Response.ParametersLength;
            request.MaxDataCount        = (ushort)maxOutputLength;

            TrySendMessage(request);
            SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);

            reply.IsSuccessElseThrow();
            if (!(reply.Commands[0] is Transaction2Response))
            {
                throw new NtStatusException(reply.Header.Status);
            }

            result = new List <FindInformation>();
            Transaction2Response           response            = (Transaction2Response)reply.Commands[0];
            Transaction2FindFirst2Response subcommandResponse  = new Transaction2FindFirst2Response(response.TransParameters, response.TransData);
            FindInformationList            findInformationList = subcommandResponse.GetFindInformationList(subCommand.InformationLevel, reply.Header.UnicodeFlag);

            result.AddRange(findInformationList);
            bool endOfSearch = subcommandResponse.EndOfSearch;

            while (!endOfSearch)
            {
                Transaction2FindNext2Request nextSubCommand = new Transaction2FindNext2Request
                {
                    SID              = subcommandResponse.SID,
                    SearchCount      = ushort.MaxValue,
                    Flags            = FindFlags.SMB_FIND_CLOSE_AT_EOS | FindFlags.SMB_FIND_CONTINUE_FROM_LAST,
                    InformationLevel = informationLevel,
                    FileName         = fileName
                };

                request = new Transaction2Request
                {
                    Setup           = nextSubCommand.GetSetup(),
                    TransParameters = nextSubCommand.GetParameters(m_client.Unicode),
                    TransData       = nextSubCommand.GetData(m_client.Unicode)
                };
                request.TotalDataCount      = (ushort)request.TransData.Length;
                request.TotalParameterCount = (ushort)request.TransParameters.Length;
                request.MaxParameterCount   = Transaction2FindNext2Response.ParametersLength;
                request.MaxDataCount        = (ushort)maxOutputLength;

                TrySendMessage(request);
                reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
                if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response transaction2Response)
                {
                    Transaction2FindNext2Response nextSubCommandResponse = new Transaction2FindNext2Response(transaction2Response.TransParameters, transaction2Response.TransData);
                    findInformationList = nextSubCommandResponse.GetFindInformationList(subCommand.InformationLevel, reply.Header.UnicodeFlag);
                    result.AddRange(findInformationList);
                    endOfSearch = nextSubCommandResponse.EndOfSearch;
                }
                else
                {
                    endOfSearch = true;
                }
            }
            reply.IsSuccessElseThrow();
        }
 /// <summary>
 /// to create a Trans2FindNext2 request packet.
 /// </summary>
 /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
 /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
 /// accessing.</param>
 /// <param name="sid">This field MUST be the search identifier (SID) returned in TRANS2_FIND_FIRST2
 /// response.</param>
 /// <param name="searchCount">The server MUST NOT return more entries than indicated by the value of this
 /// field.</param>
 /// <param name="informationLevel">This field contains an information level code, which determines the
 /// information contained in the response.</param>
 /// <param name="resumeKey">This field MUST be the value of a ResumeKey field returned in the response from a
 /// TRANS2_FIND_FIRST2 or TRANS2_FIND_NEXT2 that is part of the same search (same SID).</param>
 /// <param name="findFlags">This 16-bit field of flags is used to request that the server take
 /// certain actions.</param>
 /// <param name="name">A buffer containing the name of the file to be opened, created, or truncated. The
 /// string MUST be null terminated</param>
 /// <param name="extendedAttributeList">Array of SMB_EA A list of extended file attribute name/value pairs.
 /// </param>
 /// <returns>a Trans2FindNext2 request packet</returns>
 /// <exception cref="System.NullReferenceException">There is no connection in context. </exception>
 public SmbTrans2FindNext2RequestPacket CreateTrans2FindNext2Request(
     ushort uid,
     ushort treeId,
     ushort sid,
     ushort searchCount,
     FindInformationLevel informationLevel,
     uint resumeKey,
     Trans2FindFlags findFlags,
     string name,
     SMB_GEA[] extendedAttributeList)
 {
     return this.CreateTrans2FindNext2Request(this.Context.GetMessageId(this.connectionId),
         uid, treeId, this.defaultParameters.Flag, this.defaultParameters.Flag2, this.defaultParameters.MaxParameterCount,
         this.defaultParameters.MaxDataCount, this.defaultParameters.MaxSetupCount, this.defaultParameters.Trans2SmbParametersFlags,
         this.defaultParameters.Timeout, sid, searchCount, informationLevel, resumeKey, findFlags, name,
         extendedAttributeList);
 }
        /// <summary>
        /// to create a Trans2FindNext2 request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <param name="smbParametersFlags">A set of bit flags that alter the behavior of the requested operation.
        /// Unused bit fields MUST be set to zero by the client sending the request, and MUST be ignored by the server
        /// receiving the request. The client MAY set either or both of the following bit flags</param>
        /// <param name="timeout">The number of milliseconds the server SHOULD wait for completion of the transaction
        /// before generating a timeout. A value of zero indicates that the operation MUST NOT block.</param>
        /// <param name="sid">This field MUST be the search identifier (SID) returned in TRANS2_FIND_FIRST2
        /// response.</param>
        /// <param name="searchCount">The server MUST NOT return more entries than indicated by the value of this
        /// field.</param>
        /// <param name="informationLevel">This field contains an information level code, which determines the
        /// information contained in the response.</param>
        /// <param name="resumeKey">This field MUST be the value of a ResumeKey field returned in the response from a
        /// TRANS2_FIND_FIRST2 or TRANS2_FIND_NEXT2 that is part of the same search (same SID).</param>
        /// <param name="findFlags">This 16-bit field of flags is used to request that the server take
        /// certain actions.</param>
        /// <param name="name">This field is present but not used in SMB_COM_TRANSACTION2 requests. If Unicode support
        /// has been negotiated, then this field MUST be aligned to a 16-bit boundary and MUST consist of two null bytes
        /// (a null-terminator). If Unicode support has not been negotiated this field will contain only one null
        /// byte.</param>
        /// <param name="extendedAttributeList">Array of SMB_EA A list of extended file attribute name/value pairs.
        /// </param>
        /// <returns>a Trans2FindNext2 request packet</returns>
        public SmbTrans2FindNext2RequestPacket CreateTrans2FindNext2Request(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            ushort maxParameterCount,
            ushort maxDataCount,
            byte maxSetupCount,
            Trans2SmbParametersFlags smbParametersFlags,
            uint timeout,
            ushort sid,
            ushort searchCount,
            FindInformationLevel informationLevel,
            uint resumeKey,
            Trans2FindFlags findFlags,
            string name,
            SMB_GEA[] extendedAttributeList)
        {
            if (name == null)
            {
                name = string.Empty;
            }
            if (extendedAttributeList == null)
            {
                extendedAttributeList = new SMB_GEA[0];
            }

            SmbTrans2FindNext2RequestPacket packet = new SmbTrans2FindNext2RequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_TRANSACTION2,
                messageId, uid, treeId, flags, flags2);

            // Set Smb_Parameters
            SMB_COM_TRANSACTION2_Request_SMB_Parameters smbParameters =
                new SMB_COM_TRANSACTION2_Request_SMB_Parameters();
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.Flags = (ushort)smbParametersFlags;
            smbParameters.Timeout = timeout;
            smbParameters.SetupCount = 1; // the correct count in word of the Setup is always 1.
            smbParameters.Setup = new ushort[] { (ushort)Trans2SubCommand.TRANS2_FIND_NEXT2 };
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_TRANSACTION2_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb_Data
            SMB_COM_TRANSACTION2_Request_SMB_Data smbData = new SMB_COM_TRANSACTION2_Request_SMB_Data();
            smbData.Name = CifsMessageUtils.ToSmbStringBytes(name,
                (flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE);

            // Set Trans2_Parameters
            TRANS2_FIND_NEXT2_Request_Trans2_Parameters trans2Parameters =
                new TRANS2_FIND_NEXT2_Request_Trans2_Parameters();
            trans2Parameters.SID = sid;
            trans2Parameters.SearchCount = searchCount;
            trans2Parameters.InformationLevel = informationLevel;
            trans2Parameters.ResumeKey = resumeKey;
            trans2Parameters.Flags = findFlags;
            trans2Parameters.FileName = CifsMessageUtils.ToSmbStringBytes(name,
                (flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE);

            // Set Trans2_Data
            TRANS2_FIND_NEXT2_Request_Trans2_Data trans2Data = new TRANS2_FIND_NEXT2_Request_Trans2_Data();
            trans2Data.GetExtendedAttributeList.GEAList = extendedAttributeList;
            trans2Data.GetExtendedAttributeList.SizeOfListInBytes =
                (uint)CifsMessageUtils.GetSize<uint>(trans2Data.GetExtendedAttributeList.SizeOfListInBytes);
            trans2Data.GetExtendedAttributeList.SizeOfListInBytes +=
                CifsMessageUtils.GetSmbQueryEAListSize(extendedAttributeList);

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.Trans2Parameters = trans2Parameters;
            packet.Trans2Data = trans2Data;
            packet.UpdateCountAndOffset();

            return packet;
        }
        /// <summary>
        /// Create Trans2FindFirst2 request for the client to find file on server. 
        /// </summary>
        /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
        /// <param name = "fileName">The file name to find. </param>
        /// <param name = "transactOptions">
        /// A set of bit flags that alter the behavior of the requested operation. Unused bit fields MUST be set to  
        /// zero by the client sending the request, and MUST be ignored by the server receiving the request. The 
        /// client MAY set either or both of the following bit flags 
        /// </param>
        /// <param name = "searchCount">
        /// The server MUST NOT return more entries than indicated by the value of this field. 
        /// </param>
        /// <param name = "findFlags">
        /// This 16-bit field of flags is used to request that the server take certain actions. 
        /// </param>
        /// <param name = "searchAttributes">File attributes to apply as a constraint to the file search. </param>
        /// <param name = "searchStorageType">
        /// This field specifies if the find is searching for directories or for files. This field MUST be one of two  
        /// values 
        /// </param>
        /// <param name = "isReparsePath">If true, the path in the request MUST contain an @GMT token. </param>
        /// <param name = "isKnowsLongName">
        /// is used to indicate to set the SMB_FLAGS2_KNOWS_LONG_NAMES flag in smb header or not. 
        /// </param>
        /// <param name = "informationLevel">
        /// Indicates that client specifies the information it is requesting. Server return different data based on 
        /// the client's request. 
        /// </param>
        /// <returns>a find file for the first request packet </returns>
        public virtual SmbTrans2FindFirst2RequestPacket CreateTrans2FindFirst2Request(
            ushort treeId,
            string fileName,
            Trans2SmbParametersFlags transactOptions,
            ushort searchCount,
            Trans2FindFlags findFlags,
            SmbFileAttributes searchAttributes,
            Trans2FindFirst2SearchStorageType searchStorageType,
            bool isReparsePath,
            bool isKnowsLongName,
            FindInformationLevel informationLevel)
        {
            SmbHeader_Flags2_Values flags2 = this.capability.Flags2;
            // update flags2 of header
            if (isReparsePath)
            {
                flags2 |= SmbHeader_Flags2_Values.SMB_FLAGS2_REPARSE_PATH;
            }

            if (isKnowsLongName)
            {
                flags2 |= SmbHeader_Flags2_Values.SMB_FLAGS2_KNOWS_LONG_NAMES;
            }
            return CreateTrans2FindFirst2Request(this.MessageId,
                this.GetSessionIdByTreeId(treeId), treeId,
               this.capability.Flag, flags2, fileName, transactOptions,
                this.capability.Timeout, searchCount, findFlags, searchAttributes, searchStorageType,
                informationLevel);
        }
Beispiel #28
0
        /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
        public static FindInformation GetFindInformation(QueryDirectoryFileInformation entry, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys)
        {
            switch (informationLevel)
            {
            case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
            {
                FileDirectoryInformation fileDirectoryInfo = (FileDirectoryInformation)entry;

                FindFileDirectoryInfo result = new FindFileDirectoryInfo();
                result.FileIndex          = fileDirectoryInfo.FileIndex;
                result.CreationTime       = fileDirectoryInfo.CreationTime;
                result.LastAccessTime     = fileDirectoryInfo.LastAccessTime;
                result.LastWriteTime      = fileDirectoryInfo.LastWriteTime;
                result.LastAttrChangeTime = fileDirectoryInfo.LastWriteTime;
                result.EndOfFile          = fileDirectoryInfo.EndOfFile;
                result.AllocationSize     = fileDirectoryInfo.AllocationSize;
                result.ExtFileAttributes  = (ExtendedFileAttributes)fileDirectoryInfo.FileAttributes;
                result.FileName           = fileDirectoryInfo.FileName;
                return(result);
            }

            case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
            {
                FileFullDirectoryInformation fileFullDirectoryInfo = (FileFullDirectoryInformation)entry;

                FindFileFullDirectoryInfo result = new FindFileFullDirectoryInfo();
                result.FileIndex          = fileFullDirectoryInfo.FileIndex;
                result.CreationTime       = fileFullDirectoryInfo.CreationTime;
                result.LastAccessTime     = fileFullDirectoryInfo.LastAccessTime;
                result.LastWriteTime      = fileFullDirectoryInfo.LastWriteTime;
                result.LastAttrChangeTime = fileFullDirectoryInfo.LastWriteTime;
                result.EndOfFile          = fileFullDirectoryInfo.EndOfFile;
                result.AllocationSize     = fileFullDirectoryInfo.AllocationSize;
                result.ExtFileAttributes  = (ExtendedFileAttributes)fileFullDirectoryInfo.FileAttributes;
                result.EASize             = fileFullDirectoryInfo.EaSize;
                result.FileName           = fileFullDirectoryInfo.FileName;
                return(result);
            }

            case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
            {
                FileNamesInformation fileNamesInfo = (FileNamesInformation)entry;

                FindFileNamesInfo result = new FindFileNamesInfo();
                result.FileIndex = fileNamesInfo.FileIndex;
                result.FileName  = fileNamesInfo.FileName;
                return(result);
            }

            case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
            {
                FileBothDirectoryInformation fileBothDirectoryInfo = (FileBothDirectoryInformation)entry;

                FindFileBothDirectoryInfo result = new FindFileBothDirectoryInfo();
                result.FileIndex         = fileBothDirectoryInfo.FileIndex;
                result.CreationTime      = fileBothDirectoryInfo.CreationTime;
                result.LastAccessTime    = fileBothDirectoryInfo.LastAccessTime;
                result.LastWriteTime     = fileBothDirectoryInfo.LastWriteTime;
                result.LastChangeTime    = fileBothDirectoryInfo.LastWriteTime;
                result.EndOfFile         = fileBothDirectoryInfo.EndOfFile;
                result.AllocationSize    = fileBothDirectoryInfo.AllocationSize;
                result.ExtFileAttributes = (ExtendedFileAttributes)fileBothDirectoryInfo.FileAttributes;
                result.EASize            = fileBothDirectoryInfo.EaSize;
                result.Reserved          = fileBothDirectoryInfo.Reserved;
                result.ShortName         = fileBothDirectoryInfo.ShortName;
                result.FileName          = fileBothDirectoryInfo.FileName;
                return(result);
            }

            default:
            {
                throw new UnsupportedInformationLevelException();
            }
            }
        }
 /// <summary>
 /// Check if the information level matches the information structure.
 /// </summary>
 /// <param name="level">information level</param>
 /// <param name="findInformationLevel">information level structure</param>
 /// <returns>true if the level matches the structure, false if not.</returns>
 private bool CheckInformationLevel(FindInformationLevel level, Array findInformationLevel)
 {
     bool ret = false;
     switch (level)
     {
         case FindInformationLevel.SMB_INFO_STANDARD:
             ret = findInformationLevel is SMB_INFO_STANDARD_OF_TRANS2_FIND_FIRST2[];
             break;
         case FindInformationLevel.SMB_INFO_QUERY_EA_SIZE:
             ret = findInformationLevel is SMB_INFO_QUERY_EA_SIZE_OF_TRANS2_FIND_FIRST2[];
             break;
         case FindInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST:
             ret = findInformationLevel is SMB_INFO_QUERY_EAS_FROM_LIST_OF_TRANS2_FIND_FIRST2[];
             break;
         case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
             ret = findInformationLevel is SMB_FIND_FILE_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[];
             break;
         case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
             ret = findInformationLevel is SMB_FIND_FILE_FULL_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[];
             break;
         case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
             ret = findInformationLevel is SMB_FIND_FILE_NAMES_INFO_OF_TRANS2_FIND_FIRST2[];
             break;
         case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
             ret = findInformationLevel is SMB_FIND_FILE_BOTH_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[];
             break;
         default:
             break;
     }
     return ret;
 }
Beispiel #30
0
        /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
        public static FindInformationList GetFindInformationList(List <QueryDirectoryFileInformation> entries, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys, int maxLength)
        {
            FindInformationList result = new FindInformationList();

            for (int index = 0; index < entries.Count; index++)
            {
                FindInformation infoEntry = GetFindInformation(entries[index], informationLevel, isUnicode, returnResumeKeys);
                result.Add(infoEntry);
                if (result.GetLength(isUnicode) > maxLength)
                {
                    result.RemoveAt(result.Count - 1);
                    break;
                }
            }
            return(result);
        }
        /// <summary>
        /// to create a Trans2FindFirst2 request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <param name="smbParametersFlags">A set of bit flags that alter the behavior of the requested operation.
        /// Unused bit fields MUST be set to zero by the client sending the request, and MUST be ignored by the server
        /// receiving the request. The client MAY set either or both of the following bit flags</param>
        /// <param name="timeout">The number of milliseconds the server SHOULD wait for completion of the transaction
        /// before generating a timeout. A value of zero indicates that the operation MUST NOT block.</param>
        /// <param name="extendedAttributeList">A list of extended file attribute name/value pairs. </param>
        /// <param name="searchCount">The server MUST NOT return more entries than indicated by the value of this
        /// field.</param>
        /// <param name="searchAttributes">File attributes to apply as a constraint to the file search.</param>
        /// <param name="informationLevel">This field contains an information level code, which determines the
        /// information contained in the response.</param>
        /// <param name="searchStorageType">This field specifies if the find is searching for directories or for files.
        /// This field MUST be one of two values</param>
        /// <param name="name">A buffer containing the name of the file to be opened, created, or truncated. The
        /// string MUST be null terminated</param>
        /// <param name="findFlags">This 16-bit field of flags is used to request that the server take
        /// certain actions.</param>
        /// <returns>a Trans2FindFirst2 request packet</returns>
        public SmbTrans2FindFirst2RequestPacket CreateTrans2FindFirst2Request(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            ushort maxParameterCount,
            ushort maxDataCount,
            byte maxSetupCount,
            Trans2SmbParametersFlags smbParametersFlags,
            uint timeout,
            SmbFileAttributes searchAttributes,
            ushort searchCount,
            Trans2FindFlags findFlags,
            FindInformationLevel informationLevel,
            Trans2FindFirst2SearchStorageType searchStorageType,
            string name,
            SMB_GEA[] extendedAttributeList)
        {
            if (name == null)
            {
                name = string.Empty;
            }
            if (extendedAttributeList == null)
            {
                extendedAttributeList = new SMB_GEA[0];
            }

            SmbTrans2FindFirst2RequestPacket packet = new SmbTrans2FindFirst2RequestPacket();

            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_TRANSACTION2,
                messageId, uid, treeId, flags, flags2);

            // Set Smb_Parameters
            SMB_COM_TRANSACTION2_Request_SMB_Parameters smbParameters =
                new SMB_COM_TRANSACTION2_Request_SMB_Parameters();
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.Flags = (ushort)smbParametersFlags;
            smbParameters.Timeout = timeout;
            smbParameters.SetupCount = 1; // the correct count in word of the Setup is always 1.
            smbParameters.Setup = new ushort[] { (ushort)Trans2SubCommand.TRANS2_FIND_FIRST2 };
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_TRANSACTION2_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb_Data
            SMB_COM_TRANSACTION2_Request_SMB_Data smbData = new SMB_COM_TRANSACTION2_Request_SMB_Data();
            smbData.Name = CifsMessageUtils.ToSmbStringBytes(name,
                (flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE);

            // The size of the preceding SmbParameters part plus Header part is an odd number for all cifs messages
            // Use Name field to judge whether needs to add one 16-bits align pad.
            if ((flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE && smbData.Name.Length
                % twoBytesAlign == 0)
            {
                // pad 1 byte for 16-bits align:
                smbData.Pad1 = new byte[1];
            }
            else
            {
                smbData.Pad1 = new byte[0];
            }

            // Set Trans2_Parameters
            TRANS2_FIND_FIRST2_Request_Trans2_Parameters trans2Parameters =
                new TRANS2_FIND_FIRST2_Request_Trans2_Parameters();
            trans2Parameters.SearchAttributes = searchAttributes;
            trans2Parameters.SearchCount = searchCount;
            trans2Parameters.Flags = findFlags;
            trans2Parameters.InformationLevel = informationLevel;
            trans2Parameters.SearchStorageType = searchStorageType;
            trans2Parameters.FileName = CifsMessageUtils.ToSmbStringBytes(name,
                (flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE);

            // Set Trans2_Data
            TRANS2_FIND_FIRST2_Request_Trans2_Data trans2Data = new TRANS2_FIND_FIRST2_Request_Trans2_Data();
            trans2Data.GetExtendedAttributeList.GEAList = extendedAttributeList;
            trans2Data.GetExtendedAttributeList.SizeOfListInBytes =
                (uint)CifsMessageUtils.GetSize<uint>(trans2Data.GetExtendedAttributeList.SizeOfListInBytes);
            trans2Data.GetExtendedAttributeList.SizeOfListInBytes +=
                CifsMessageUtils.GetSmbQueryEAListSize(extendedAttributeList);

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.Trans2Parameters = trans2Parameters;
            packet.Trans2Data = trans2Data;
            packet.UpdateCountAndOffset();

            return packet;
        }
Beispiel #32
0
        public NTStatus QueryDirectory(out List <FindInformation> result, string fileName, FindInformationLevel informationLevel)
        {
            result = null;
            int maxOutputLength = 4096;
            Transaction2FindFirst2Request subcommand = new Transaction2FindFirst2Request();

            subcommand.SearchAttributes = SMBFileAttributes.Hidden | SMBFileAttributes.System | SMBFileAttributes.Directory;
            subcommand.SearchCount      = UInt16.MaxValue;
            subcommand.Flags            = FindFlags.SMB_FIND_CLOSE_AT_EOS;
            subcommand.InformationLevel = informationLevel;
            subcommand.FileName         = fileName;

            Transaction2Request request = new Transaction2Request();

            request.Setup               = subcommand.GetSetup();
            request.TransParameters     = subcommand.GetParameters(m_client.Unicode);
            request.TransData           = subcommand.GetData(m_client.Unicode);
            request.TotalDataCount      = (ushort)request.TransData.Length;
            request.TotalParameterCount = (ushort)request.TransParameters.Length;
            request.MaxParameterCount   = Transaction2FindFirst2Response.ParametersLength;
            request.MaxDataCount        = (ushort)maxOutputLength;

            TrySendMessage(request);
            SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);

            if (reply != null)
            {
                if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
                {
                    result = new List <FindInformation>();
                    Transaction2Response           response            = (Transaction2Response)reply.Commands[0];
                    Transaction2FindFirst2Response subcommandResponse  = new Transaction2FindFirst2Response(response.TransParameters, response.TransData, reply.Header.UnicodeFlag);
                    FindInformationList            findInformationList = subcommandResponse.GetFindInformationList(subcommand.InformationLevel, reply.Header.UnicodeFlag);
                    result.AddRange(findInformationList);
                    bool endOfSearch = subcommandResponse.EndOfSearch;
                    while (!endOfSearch)
                    {
                        Transaction2FindNext2Request nextSubcommand = new Transaction2FindNext2Request();
                        nextSubcommand.SID              = subcommandResponse.SID;
                        nextSubcommand.SearchCount      = UInt16.MaxValue;
                        nextSubcommand.Flags            = FindFlags.SMB_FIND_CLOSE_AT_EOS | FindFlags.SMB_FIND_CONTINUE_FROM_LAST;
                        nextSubcommand.InformationLevel = informationLevel;
                        nextSubcommand.FileName         = fileName;

                        request                     = new Transaction2Request();
                        request.Setup               = nextSubcommand.GetSetup();
                        request.TransParameters     = nextSubcommand.GetParameters(m_client.Unicode);
                        request.TransData           = nextSubcommand.GetData(m_client.Unicode);
                        request.TotalDataCount      = (ushort)request.TransData.Length;
                        request.TotalParameterCount = (ushort)request.TransParameters.Length;
                        request.MaxParameterCount   = Transaction2FindNext2Response.ParametersLength;
                        request.MaxDataCount        = (ushort)maxOutputLength;

                        TrySendMessage(request);
                        reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
                        if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
                        {
                            response = (Transaction2Response)reply.Commands[0];
                            Transaction2FindNext2Response nextSubcommandResponse = new Transaction2FindNext2Response(response.TransParameters, response.TransData, reply.Header.UnicodeFlag);
                            findInformationList = nextSubcommandResponse.GetFindInformationList(subcommand.InformationLevel, reply.Header.UnicodeFlag);
                            result.AddRange(findInformationList);
                            endOfSearch = nextSubcommandResponse.EndOfSearch;
                        }
                        else
                        {
                            endOfSearch = true;
                        }
                    }
                }
                return(reply.Header.Status);
            }
            return(NTStatus.STATUS_INVALID_SMB);
        }
Beispiel #33
0
        internal static FindInformationEntry FromFileSystemEntry(FileSystemEntry entry, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys)
        {
            switch (informationLevel)
            {
            case FindInformationLevel.SMB_INFO_STANDARD:
            {
                FindInfoStandard result = new FindInfoStandard(returnResumeKeys);
                result.CreationDateTime   = entry.CreationTime;
                result.LastAccessDateTime = entry.LastAccessTime;
                result.LastWriteDateTime  = entry.LastWriteTime;
                result.FileDataSize       = (uint)Math.Min(entry.Size, UInt32.MaxValue);
                result.AllocationSize     = (uint)Math.Min(GetAllocationSize(entry.Size), UInt32.MaxValue);
                result.Attributes         = GetFileAttributes(entry);
                result.FileName           = entry.Name;
                return(result);
            }

            case FindInformationLevel.SMB_INFO_QUERY_EA_SIZE:
            {
                FindInfoQueryEASize result = new FindInfoQueryEASize(returnResumeKeys);
                result.CreationDateTime   = entry.CreationTime;
                result.LastAccessDateTime = entry.LastAccessTime;
                result.LastWriteDateTime  = entry.LastWriteTime;
                result.FileDataSize       = (uint)Math.Min(entry.Size, UInt32.MaxValue);
                result.AllocationSize     = (uint)Math.Min(GetAllocationSize(entry.Size), UInt32.MaxValue);
                result.Attributes         = GetFileAttributes(entry);
                result.EASize             = 0;
                result.FileName           = entry.Name;
                return(result);
            }

            case FindInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST:
            {
                FindInfoQueryExtendedAttributesFromList result = new FindInfoQueryExtendedAttributesFromList(returnResumeKeys);
                result.CreationDateTime      = entry.CreationTime;
                result.LastAccessDateTime    = entry.LastAccessTime;
                result.LastWriteDateTime     = entry.LastWriteTime;
                result.FileDataSize          = (uint)Math.Min(entry.Size, UInt32.MaxValue);
                result.AllocationSize        = (uint)Math.Min(GetAllocationSize(entry.Size), UInt32.MaxValue);
                result.Attributes            = GetFileAttributes(entry);
                result.ExtendedAttributeList = new FullExtendedAttributeList();
                return(result);
            }

            case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
            {
                FindFileDirectoryInfo result = new FindFileDirectoryInfo();
                result.CreationTime       = entry.CreationTime;
                result.LastAccessTime     = entry.LastAccessTime;
                result.LastWriteTime      = entry.LastWriteTime;
                result.LastAttrChangeTime = entry.LastWriteTime;
                result.EndOfFile          = entry.Size;
                result.AllocationSize     = GetAllocationSize(entry.Size);
                result.ExtFileAttributes  = GetExtendedFileAttributes(entry);
                result.FileName           = entry.Name;
                return(result);
            }

            case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
            {
                FindFileFullDirectoryInfo result = new FindFileFullDirectoryInfo();
                result.CreationTime       = entry.CreationTime;
                result.LastAccessTime     = entry.LastAccessTime;
                result.LastWriteTime      = entry.LastWriteTime;
                result.LastAttrChangeTime = entry.LastWriteTime;
                result.EndOfFile          = entry.Size;
                result.AllocationSize     = GetAllocationSize(entry.Size);
                result.ExtFileAttributes  = GetExtendedFileAttributes(entry);
                result.FileName           = entry.Name;
                return(result);
            }

            case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
            {
                FindFileNamesInfo result = new FindFileNamesInfo();
                result.FileName = entry.Name;
                return(result);
            }

            case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
            {
                FindFileBothDirectoryInfo result = new FindFileBothDirectoryInfo();
                result.CreationTime      = entry.CreationTime;
                result.LastAccessTime    = entry.LastAccessTime;
                result.LastWriteTime     = entry.LastWriteTime;
                result.LastChangeTime    = entry.LastWriteTime;
                result.EndOfFile         = entry.Size;
                result.AllocationSize    = GetAllocationSize(entry.Size);
                result.ExtFileAttributes = GetExtendedFileAttributes(entry);
                result.ShortName         = GetShortName(entry.Name);
                result.FileName          = entry.Name;
                return(result);
            }

            default:
            {
                throw new UnsupportedInformationLevelException();
            }
            }
        }