1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
- *
- * You can redistribute this program and/or modify it under the terms of
- * the GNU Lesser Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- */
- using System;
- using System.Collections.Generic;
- using System.IO;
- using SMBLibrary.SMB2;
- using Utilities;
- namespace SMBLibrary.Server.SMB2
- {
- internal class CreateHelper
- {
- internal static SMB2Command GetCreateResponse(CreateRequest request, ISMBShare share, SMB2ConnectionState state)
- {
- SMB2Session session = state.GetSession(request.Header.SessionID);
- string path = request.Name;
- if (!path.StartsWith(@"\"))
- {
- path = @"\" + path;
- }
- FileAccess createAccess = NTFileStoreHelper.ToCreateFileAccess(request.DesiredAccess, request.CreateDisposition);
- if (share is FileSystemShare)
- {
- if (!((FileSystemShare)share).HasAccess(session.SecurityContext, path, createAccess))
- {
- state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. User '{2}' was denied access.", share.Name, path, session.UserName);
- return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
- }
- }
- object handle;
- FileStatus fileStatus;
- // GetFileInformation/FileNetworkOpenInformation requires FILE_READ_ATTRIBUTES
- AccessMask desiredAccess = request.DesiredAccess.File | FileAccessMask.FILE_READ_ATTRIBUTES;
- NTStatus createStatus = share.FileStore.CreateFile(out handle, out fileStatus, path, desiredAccess, request.FileAttributes, request.ShareAccess, request.CreateDisposition, request.CreateOptions, session.SecurityContext);
- if (createStatus != NTStatus.STATUS_SUCCESS)
- {
- state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. NTStatus: {2}.", share.Name, path, createStatus);
- return new ErrorResponse(request.CommandName, createStatus);
- }
- FileID? fileID = session.AddOpenFile(request.Header.TreeID, share.Name, path, handle);
- if (fileID == null)
- {
- share.FileStore.CloseFile(handle);
- state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. Too many open files.", share.Name, path);
- return new ErrorResponse(request.CommandName, NTStatus.STATUS_TOO_MANY_OPENED_FILES);
- }
- state.LogToServer(Severity.Verbose, "Create: Opened '{0}{1}'. (SessionID: {2}, TreeID: {3}, FileId: {4})", share.Name, path, request.Header.SessionID, request.Header.TreeID, fileID.Value.Volatile);
- if (share is NamedPipeShare)
- {
- return CreateResponseForNamedPipe(fileID.Value, FileStatus.FILE_OPENED);
- }
- else
- {
- FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, handle);
- CreateResponse response = CreateResponseFromFileSystemEntry(fileInfo, fileID.Value, fileStatus);
- return response;
- }
- }
- private static CreateResponse CreateResponseForNamedPipe(FileID fileID, FileStatus fileStatus)
- {
- CreateResponse response = new CreateResponse();
- response.CreateAction = (CreateAction)fileStatus;
- response.FileAttributes = FileAttributes.Normal;
- response.FileId = fileID;
- return response;
- }
- private static CreateResponse CreateResponseFromFileSystemEntry(FileNetworkOpenInformation fileInfo, FileID fileID, FileStatus fileStatus)
- {
- CreateResponse response = new CreateResponse();
- response.CreateAction = (CreateAction)fileStatus;
- response.CreationTime = fileInfo.CreationTime;
- response.LastWriteTime = fileInfo.LastWriteTime;
- response.ChangeTime = fileInfo.LastWriteTime;
- response.LastAccessTime = fileInfo.LastAccessTime;
- response.AllocationSize = fileInfo.AllocationSize;
- response.EndofFile = fileInfo.EndOfFile;
- response.FileAttributes = fileInfo.FileAttributes;
- response.FileId = fileID;
- return response;
- }
- }
- }
|