CreateHelper.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if (share is NamedPipeShare)
  25. {
  26. Stream pipeStream = ((NamedPipeShare)share).OpenPipe(path);
  27. if (pipeStream != null)
  28. {
  29. ulong? persistentFileID = session.AddOpenFile(path, pipeStream);
  30. if (!persistentFileID.HasValue)
  31. {
  32. return new ErrorResponse(request.CommandName, NTStatus.STATUS_TOO_MANY_OPENED_FILES);
  33. }
  34. return CreateResponseForNamedPipe(persistentFileID.Value);
  35. }
  36. else
  37. {
  38. return new ErrorResponse(request.CommandName, NTStatus.STATUS_OBJECT_PATH_NOT_FOUND);
  39. }
  40. }
  41. else
  42. {
  43. FileSystemShare fileSystemShare = (FileSystemShare)share;
  44. FileSystemEntry entry;
  45. NTStatus createStatus = NTFileSystemHelper.CreateFile(out entry, (FileSystemShare)share, session.UserName, path, request.CreateDisposition, request.CreateOptions, request.DesiredAccess, state);
  46. if (createStatus != NTStatus.STATUS_SUCCESS)
  47. {
  48. return new ErrorResponse(request.CommandName, createStatus);
  49. }
  50. IFileSystem fileSystem = fileSystemShare.FileSystem;
  51. FileAccess fileAccess = NTFileSystemHelper.ToFileAccess(request.DesiredAccess.File);
  52. FileShare fileShare = NTFileSystemHelper.ToFileShare(request.ShareAccess);
  53. Stream stream;
  54. bool deleteOnClose = false;
  55. if (fileAccess == (FileAccess)0 || entry.IsDirectory)
  56. {
  57. stream = null;
  58. }
  59. else
  60. {
  61. // When FILE_OPEN_REPARSE_POINT is specified, the operation should continue normally if the file is not a reparse point.
  62. // FILE_OPEN_REPARSE_POINT is a hint that the caller does not intend to actually read the file, with the exception
  63. // of a file copy operation (where the caller will attempt to simply copy the reparse point).
  64. deleteOnClose = (request.CreateOptions & CreateOptions.FILE_DELETE_ON_CLOSE) > 0;
  65. bool openReparsePoint = (request.CreateOptions & CreateOptions.FILE_OPEN_REPARSE_POINT) > 0;
  66. bool disableBuffering = (request.CreateOptions & CreateOptions.FILE_NO_INTERMEDIATE_BUFFERING) > 0;
  67. bool buffered = (request.CreateOptions & CreateOptions.FILE_SEQUENTIAL_ONLY) > 0 && !disableBuffering && !openReparsePoint;
  68. state.LogToServer(Severity.Verbose, "Create: Opening '{0}', Access={1}, Share={2}, Buffered={3}", path, fileAccess, fileShare, buffered);
  69. try
  70. {
  71. stream = fileSystem.OpenFile(path, FileMode.Open, fileAccess, fileShare);
  72. }
  73. catch (IOException ex)
  74. {
  75. ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
  76. if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
  77. {
  78. state.LogToServer(Severity.Debug, "NTCreate: Sharing violation opening '{0}'", path);
  79. return new ErrorResponse(request.CommandName, NTStatus.STATUS_SHARING_VIOLATION);
  80. }
  81. else
  82. {
  83. state.LogToServer(Severity.Debug, "NTCreate: Sharing violation opening '{0}', Data Error", path);
  84. return new ErrorResponse(request.CommandName, NTStatus.STATUS_DATA_ERROR);
  85. }
  86. }
  87. catch (UnauthorizedAccessException)
  88. {
  89. state.LogToServer(Severity.Debug, "NTCreate: Sharing violation opening '{0}', Access Denied", path);
  90. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  91. }
  92. if (buffered)
  93. {
  94. stream = new PrefetchedStream(stream);
  95. }
  96. }
  97. ulong? persistentFileID = session.AddOpenFile(path, stream, deleteOnClose);
  98. if (!persistentFileID.HasValue)
  99. {
  100. return new ErrorResponse(request.CommandName, NTStatus.STATUS_TOO_MANY_OPENED_FILES);
  101. }
  102. CreateResponse response = CreateResponseFromFileSystemEntry(entry, persistentFileID.Value);
  103. if (request.RequestedOplockLevel == OplockLevel.Batch)
  104. {
  105. response.OplockLevel = OplockLevel.Batch;
  106. }
  107. return response;
  108. }
  109. }
  110. private static CreateResponse CreateResponseForNamedPipe(ulong persistentFileID)
  111. {
  112. CreateResponse response = new CreateResponse();
  113. response.FileId.Persistent = persistentFileID;
  114. response.CreateAction = CreateAction.FILE_OPENED;
  115. response.FileAttributes = FileAttributes.Normal;
  116. return response;
  117. }
  118. private static CreateResponse CreateResponseFromFileSystemEntry(FileSystemEntry entry, ulong persistentFileID)
  119. {
  120. CreateResponse response = new CreateResponse();
  121. if (entry.IsDirectory)
  122. {
  123. response.FileAttributes = FileAttributes.Directory;
  124. }
  125. else
  126. {
  127. response.FileAttributes = FileAttributes.Normal;
  128. }
  129. response.FileId.Persistent = persistentFileID;
  130. response.CreateAction = CreateAction.FILE_OPENED;
  131. response.CreationTime = entry.CreationTime;
  132. response.LastWriteTime = entry.LastWriteTime;
  133. response.ChangeTime = entry.LastWriteTime;
  134. response.LastAccessTime = entry.LastAccessTime;
  135. response.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  136. response.EndofFile = entry.Size;
  137. return response;
  138. }
  139. }
  140. }