CreateHelper.cs 5.7 KB

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