CreateHelper.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. internal class CreateHelper
  15. {
  16. internal static SMB2Command GetCreateResponse(CreateRequest request, ISMBShare share, SMB2ConnectionState state)
  17. {
  18. SMB2Session session = state.GetSession(request.Header.SessionID);
  19. string path = request.Name;
  20. if (!path.StartsWith(@"\"))
  21. {
  22. path = @"\" + path;
  23. }
  24. FileAccess createAccess = NTFileStoreHelper.ToCreateFileAccess(request.DesiredAccess, request.CreateDisposition);
  25. if (share is FileSystemShare)
  26. {
  27. if (!((FileSystemShare)share).HasAccess(session.SecurityContext, path, createAccess))
  28. {
  29. state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. User '{2}' was denied access.", share.Name, path, session.UserName);
  30. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  31. }
  32. }
  33. object handle;
  34. FileStatus fileStatus;
  35. NTStatus createStatus = share.FileStore.CreateFile(out handle, out fileStatus, path, request.DesiredAccess, request.ShareAccess, request.CreateDisposition, request.CreateOptions, session.SecurityContext);
  36. if (createStatus != NTStatus.STATUS_SUCCESS)
  37. {
  38. return new ErrorResponse(request.CommandName, createStatus);
  39. }
  40. FileID? fileID = session.AddOpenFile(request.Header.TreeID, path, handle);
  41. if (fileID == null)
  42. {
  43. share.FileStore.CloseFile(handle);
  44. return new ErrorResponse(request.CommandName, NTStatus.STATUS_TOO_MANY_OPENED_FILES);
  45. }
  46. if (share is NamedPipeShare)
  47. {
  48. return CreateResponseForNamedPipe(fileID.Value, FileStatus.FILE_OPENED);
  49. }
  50. else
  51. {
  52. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, handle);
  53. CreateResponse response = CreateResponseFromFileSystemEntry(fileInfo, fileID.Value, fileStatus);
  54. if (request.RequestedOplockLevel == OplockLevel.Batch)
  55. {
  56. response.OplockLevel = OplockLevel.Batch;
  57. }
  58. return response;
  59. }
  60. }
  61. private static CreateResponse CreateResponseForNamedPipe(FileID fileID, FileStatus fileStatus)
  62. {
  63. CreateResponse response = new CreateResponse();
  64. response.CreateAction = (CreateAction)fileStatus;
  65. response.FileAttributes = FileAttributes.Normal;
  66. response.FileId = fileID;
  67. return response;
  68. }
  69. private static CreateResponse CreateResponseFromFileSystemEntry(FileNetworkOpenInformation fileInfo, FileID fileID, FileStatus fileStatus)
  70. {
  71. CreateResponse response = new CreateResponse();
  72. response.CreateAction = (CreateAction)fileStatus;
  73. response.CreationTime = fileInfo.CreationTime;
  74. response.LastWriteTime = fileInfo.LastWriteTime;
  75. response.ChangeTime = fileInfo.LastWriteTime;
  76. response.LastAccessTime = fileInfo.LastAccessTime;
  77. response.AllocationSize = fileInfo.AllocationSize;
  78. response.EndofFile = fileInfo.EndOfFile;
  79. response.FileAttributes = fileInfo.FileAttributes;
  80. response.FileId = fileID;
  81. return response;
  82. }
  83. }
  84. }