CreateHelper.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. public 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.UserName, path, createAccess, state.ClientEndPoint))
  28. {
  29. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  30. }
  31. }
  32. if (share is NamedPipeShare)
  33. {
  34. Stream pipeStream = ((NamedPipeShare)share).OpenPipe(path);
  35. if (pipeStream != null)
  36. {
  37. ulong? persistentFileID = session.AddOpenFile(path, pipeStream);
  38. if (!persistentFileID.HasValue)
  39. {
  40. return new ErrorResponse(request.CommandName, NTStatus.STATUS_TOO_MANY_OPENED_FILES);
  41. }
  42. return CreateResponseForNamedPipe(persistentFileID.Value);
  43. }
  44. else
  45. {
  46. return new ErrorResponse(request.CommandName, NTStatus.STATUS_OBJECT_PATH_NOT_FOUND);
  47. }
  48. }
  49. else
  50. {
  51. FileSystemShare fileSystemShare = (FileSystemShare)share;
  52. FileSystemEntry entry;
  53. NTStatus createStatus = NTFileSystemHelper.CreateFile(out entry, fileSystemShare.FileSystem, path, request.DesiredAccess, request.CreateDisposition, request.CreateOptions, state);
  54. if (createStatus != NTStatus.STATUS_SUCCESS)
  55. {
  56. return new ErrorResponse(request.CommandName, createStatus);
  57. }
  58. FileAccess fileAccess = NTFileStoreHelper.ToFileAccess(request.DesiredAccess.File);
  59. Stream stream;
  60. bool deleteOnClose = false;
  61. if (fileAccess == (FileAccess)0 || entry.IsDirectory)
  62. {
  63. stream = null;
  64. }
  65. else
  66. {
  67. IFileSystem fileSystem = fileSystemShare.FileSystem;
  68. // When FILE_OPEN_REPARSE_POINT is specified, the operation should continue normally if the file is not a reparse point.
  69. // FILE_OPEN_REPARSE_POINT is a hint that the caller does not intend to actually read the file, with the exception
  70. // of a file copy operation (where the caller will attempt to simply copy the reparse point).
  71. deleteOnClose = (request.CreateOptions & CreateOptions.FILE_DELETE_ON_CLOSE) > 0;
  72. bool openReparsePoint = (request.CreateOptions & CreateOptions.FILE_OPEN_REPARSE_POINT) > 0;
  73. bool disableBuffering = (request.CreateOptions & CreateOptions.FILE_NO_INTERMEDIATE_BUFFERING) > 0;
  74. bool buffered = (request.CreateOptions & CreateOptions.FILE_SEQUENTIAL_ONLY) > 0 && !disableBuffering && !openReparsePoint;
  75. NTStatus openStatus = NTFileSystemHelper.OpenFile(out stream, fileSystem, path, fileAccess, request.ShareAccess, buffered, state);
  76. if (openStatus != NTStatus.STATUS_SUCCESS)
  77. {
  78. return new ErrorResponse(request.CommandName, openStatus);
  79. }
  80. }
  81. ulong? persistentFileID = session.AddOpenFile(path, stream, deleteOnClose);
  82. if (!persistentFileID.HasValue)
  83. {
  84. return new ErrorResponse(request.CommandName, NTStatus.STATUS_TOO_MANY_OPENED_FILES);
  85. }
  86. CreateResponse response = CreateResponseFromFileSystemEntry(entry, persistentFileID.Value);
  87. if (request.RequestedOplockLevel == OplockLevel.Batch)
  88. {
  89. response.OplockLevel = OplockLevel.Batch;
  90. }
  91. return response;
  92. }
  93. }
  94. private static CreateResponse CreateResponseForNamedPipe(ulong persistentFileID)
  95. {
  96. CreateResponse response = new CreateResponse();
  97. response.FileId.Persistent = persistentFileID;
  98. response.CreateAction = CreateAction.FILE_OPENED;
  99. response.FileAttributes = FileAttributes.Normal;
  100. return response;
  101. }
  102. private static CreateResponse CreateResponseFromFileSystemEntry(FileSystemEntry entry, ulong persistentFileID)
  103. {
  104. CreateResponse response = new CreateResponse();
  105. if (entry.IsDirectory)
  106. {
  107. response.FileAttributes = FileAttributes.Directory;
  108. }
  109. else
  110. {
  111. response.FileAttributes = FileAttributes.Normal;
  112. }
  113. response.FileId.Persistent = persistentFileID;
  114. response.CreateAction = CreateAction.FILE_OPENED;
  115. response.CreationTime = entry.CreationTime;
  116. response.LastWriteTime = entry.LastWriteTime;
  117. response.ChangeTime = entry.LastWriteTime;
  118. response.LastAccessTime = entry.LastAccessTime;
  119. response.AllocationSize = (long)NTFileSystemHelper.GetAllocationSize(entry.Size);
  120. response.EndofFile = (long)entry.Size;
  121. return response;
  122. }
  123. }
  124. }