CreateHelper.cs 6.1 KB

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