/// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbNtCreateAndxRequestPacket(SmbNtCreateAndxRequestPacket packet)
            : base(packet)
        {
            this.InitDefaultValue();

            this.smbParameters.WordCount = packet.SmbParameters.WordCount;
            this.smbParameters.AndXCommand = packet.SmbParameters.AndXCommand;
            this.smbParameters.AndXReserved = packet.SmbParameters.AndXReserved;
            this.smbParameters.AndXOffset = packet.SmbParameters.AndXOffset;
            this.smbParameters.Reserved = packet.SmbParameters.Reserved;
            this.smbParameters.NameLength = packet.SmbParameters.NameLength;
            this.smbParameters.Flags = packet.SmbParameters.Flags;
            this.smbParameters.RootDirectoryFID = packet.SmbParameters.RootDirectoryFID;
            this.smbParameters.DesiredAccess = packet.SmbParameters.DesiredAccess;
            this.smbParameters.AllocationSize = packet.SmbParameters.AllocationSize;
            this.smbParameters.ExtFileAttributes = packet.SmbParameters.ExtFileAttributes;
            this.smbParameters.ShareAccess = packet.SmbParameters.ShareAccess;
            this.smbParameters.CreateDisposition = packet.SmbParameters.CreateDisposition;
            this.smbParameters.CreateOptions = packet.SmbParameters.CreateOptions;
            this.smbParameters.ImpersonationLevel = packet.SmbParameters.ImpersonationLevel;
            this.smbParameters.SecurityFlags = packet.smbParameters.SecurityFlags;
            this.smbData.ByteCount = packet.SmbData.ByteCount;
            this.smbData.Pad = packet.smbData.Pad;

            if (packet.smbData.FileName != null)
            {
                this.smbData.FileName = new byte[packet.smbData.FileName.Length];
                Array.Copy(packet.smbData.FileName, this.smbData.FileName, packet.smbData.FileName.Length);
            }
            else
            {
                this.smbData.FileName = new byte[0];
            }
        }
Example #2
0
        /// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbNtCreateAndxRequestPacket(SmbNtCreateAndxRequestPacket packet)
            : base(packet)
        {
            this.InitDefaultValue();

            this.smbParameters.WordCount          = packet.SmbParameters.WordCount;
            this.smbParameters.AndXCommand        = packet.SmbParameters.AndXCommand;
            this.smbParameters.AndXReserved       = packet.SmbParameters.AndXReserved;
            this.smbParameters.AndXOffset         = packet.SmbParameters.AndXOffset;
            this.smbParameters.Reserved           = packet.SmbParameters.Reserved;
            this.smbParameters.NameLength         = packet.SmbParameters.NameLength;
            this.smbParameters.Flags              = packet.SmbParameters.Flags;
            this.smbParameters.RootDirectoryFID   = packet.SmbParameters.RootDirectoryFID;
            this.smbParameters.DesiredAccess      = packet.SmbParameters.DesiredAccess;
            this.smbParameters.AllocationSize     = packet.SmbParameters.AllocationSize;
            this.smbParameters.ExtFileAttributes  = packet.SmbParameters.ExtFileAttributes;
            this.smbParameters.ShareAccess        = packet.SmbParameters.ShareAccess;
            this.smbParameters.CreateDisposition  = packet.SmbParameters.CreateDisposition;
            this.smbParameters.CreateOptions      = packet.SmbParameters.CreateOptions;
            this.smbParameters.ImpersonationLevel = packet.SmbParameters.ImpersonationLevel;
            this.smbParameters.SecurityFlags      = packet.smbParameters.SecurityFlags;
            this.smbData.ByteCount = packet.SmbData.ByteCount;
            this.smbData.Pad       = packet.smbData.Pad;

            if (packet.smbData.FileName != null)
            {
                this.smbData.FileName = new byte[packet.smbData.FileName.Length];
                Array.Copy(packet.smbData.FileName, this.smbData.FileName, packet.smbData.FileName.Length);
            }
            else
            {
                this.smbData.FileName = new byte[0];
            }
        }
        /// <summary>
        /// to create a NtCreateAndx 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="smbParametersFlags">A 32-bit field containing a set of flags that modify the client
        /// request</param>
        /// <param name="rootDirectoryFID">If nonzero, this value is the File ID of an opened root directory, and the
        /// FileName field MUST be handled as relative to the directory specified by this RootDirectoryFID.</param>
        /// <param name="desiredAccess">A 32-bit field of flags that indicate standard, specific, and generic access
        /// rights. </param>
        /// <param name="allocationSize">The client MUST set this value to the initial allocation size of the file in
        /// bytes.</param>
        /// <param name="extFileAttributes">A 32-bit field containing encoded file attribute values and file access
        /// behavior flag values</param>
        /// <param name="shareAccess">A 32-bit field that specifies how the file SHOULD be shared with other
        /// processes.</param>
        /// <param name="createDisposition">A 32-bit value that represents the action to take if the file already
        /// exists or if the file is a new file and does not already exist</param>
        /// <param name="createOptions">A 32-bit field containing flag options to use if creating the file or
        /// directory</param>
        /// <param name="impersonationLevel">A value that indicates what security context the server SHOULD use when
        /// executing the command on behalf of the client. Value names are provided for convenience only</param>
        /// <param name="securityFlags">A 32-bit field containing a set of options that specify the security tracking
        /// mode.</param>
        /// <param name="fileName">A string that represents the fully qualified name of the file relative to the
        /// supplied TID to create or truncate on the server.</param>
        /// <param name="andxPacket">the andx packet.</param>
        /// <returns>a NtCreateAndx request packet</returns>
        public SmbNtCreateAndxRequestPacket CreateNtCreateAndxRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            NtTransactFlags smbParametersFlags,
            uint rootDirectoryFID,
            NtTransactDesiredAccess desiredAccess,
            ulong allocationSize,
            SMB_EXT_FILE_ATTR extFileAttributes,
            NtTransactShareAccess shareAccess,
            NtTransactCreateDisposition createDisposition,
            NtTransactCreateOptions createOptions,
            NtTransactImpersonationLevel impersonationLevel,
            NtTransactSecurityFlags securityFlags,
            string fileName,
            SmbPacket andxPacket)
        {
            if (fileName == null)
            {
                fileName = string.Empty;
            }

            SmbNtCreateAndxRequestPacket packet = new SmbNtCreateAndxRequestPacket();

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

            SMB_COM_NT_CREATE_ANDX_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_CREATE_ANDX_Request_SMB_Parameters();
            if (andxPacket == null)
            {
                smbParameters.AndXCommand = SmbCommand.SMB_COM_NO_ANDX_COMMAND;
            }
            else
            {
                smbParameters.AndXCommand = andxPacket.SmbHeader.Command;
            }
            smbParameters.AndXReserved = 0;
            smbParameters.Reserved = 0;
            smbParameters.Flags = (uint)smbParametersFlags;
            smbParameters.RootDirectoryFID = rootDirectoryFID;
            smbParameters.DesiredAccess = (uint)desiredAccess;
            smbParameters.AllocationSize = allocationSize;
            smbParameters.ExtFileAttributes = (uint)extFileAttributes;
            smbParameters.ShareAccess = (uint)shareAccess;
            smbParameters.CreateDisposition = (uint)createDisposition;
            smbParameters.CreateOptions = (uint)createOptions;
            smbParameters.ImpersonationLevel = (uint)impersonationLevel;
            smbParameters.SecurityFlags = (byte)securityFlags;
            smbParameters.WordCount = (byte)(Marshal.SizeOf(smbParameters) / NumBytesOfWord);

            SMB_COM_NT_CREATE_ANDX_Request_SMB_Data smbData = new SMB_COM_NT_CREATE_ANDX_Request_SMB_Data();

            // 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)
            {
                // if Unicode, add 1 byte pad for align on 16-bits.
                smbData.Pad = new byte[1];
                smbData.FileName = CifsMessageUtils.ToSmbStringBytes(fileName, true);
                smbParameters.NameLength = (ushort)(smbData.FileName.Length);
            }
            else
            {
                smbData.Pad = new byte[0];
                smbData.FileName = CifsMessageUtils.ToSmbStringBytes(fileName, false);
                smbParameters.NameLength = (ushort)smbData.FileName.Length;
            }

            smbData.ByteCount = (ushort)(smbData.Pad.Length + smbData.FileName.Length);

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.AndxPacket = andxPacket;

            return packet;
        }
        public SmbNtCreateAndxResponsePacket CreateNtCreateAndxResponse(
            CifsServerPerConnection connection,
            SmbNtCreateAndxRequestPacket request,
            OplockLevelValue opLockLevel,
            ulong allocationSize,
            ulong endOfFile,
            FileTypeValue resourceType,
            SMB_NMPIPE_STATUS nmPipeStatus,
            byte directory,
            SmbPacket andxPacket)
        {
            SmbNtCreateAndxResponsePacket response = new SmbNtCreateAndxResponsePacket();
            response.SmbHeader = CifsMessageUtils.CreateSmbHeader(connection, request);

            SMB_COM_NT_CREATE_ANDX_Response_SMB_Parameters smbParameters = response.SmbParameters;
            smbParameters.AndXCommand =
                andxPacket != null ? andxPacket.SmbHeader.Command : SmbCommand.SMB_COM_NO_ANDX_COMMAND;
            smbParameters.AndXReserved = 0x00;
            smbParameters.AndXOffset = (ushort)(response.HeaderSize + Marshal.SizeOf(response.SmbParameters)
                + Marshal.SizeOf(response.SmbData));
            smbParameters.OplockLevel = opLockLevel;
            smbParameters.FID = (ushort)connection.GenerateFID();
            smbParameters.CreateDisposition = NtTransactCreateDisposition.FILE_CREATE;
            FileTime fileTime = new FileTime();
            fileTime.Time = (ulong)DateTime.Now.ToFileTime();
            smbParameters.CreateTime = fileTime;
            smbParameters.LastAccessTime = fileTime;
            smbParameters.LastChangeTime = fileTime;
            smbParameters.ExtFileAttributes = (SMB_EXT_FILE_ATTR)request.SmbParameters.ExtFileAttributes;
            smbParameters.AllocationSize = allocationSize;
            smbParameters.EndOfFile = endOfFile;
            smbParameters.ResourceType = resourceType;
            smbParameters.NMPipeStatus = nmPipeStatus;
            smbParameters.Directory = directory;
            smbParameters.WordCount = (byte)(TypeMarshal.GetBlockMemorySize(smbParameters) / 2);
            response.SmbParameters = smbParameters;

            response.AndxPacket = andxPacket;
            response.UpdateHeader();

            return response;
        }