FileStoreResponseHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Copyright (C) 2014-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 System.Text;
  11. using SMBLibrary.SMB1;
  12. using Utilities;
  13. namespace SMBLibrary.Server.SMB1
  14. {
  15. internal class FileStoreResponseHelper
  16. {
  17. internal static SMB1Command GetCreateDirectoryResponse(SMB1Header header, CreateDirectoryRequest request, ISMBShare share, SMB1ConnectionState state)
  18. {
  19. SMB1Session session = state.GetSession(header.UID);
  20. if (share is FileSystemShare)
  21. {
  22. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.DirectoryName))
  23. {
  24. state.LogToServer(Severity.Verbose, "Create Directory '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.DirectoryName, session.UserName);
  25. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  26. return new ErrorResponse(request.CommandName);
  27. }
  28. }
  29. header.Status = SMB1FileStoreHelper.CreateDirectory(share.FileStore, request.DirectoryName, session.SecurityContext);
  30. if (header.Status != NTStatus.STATUS_SUCCESS)
  31. {
  32. return new ErrorResponse(request.CommandName);
  33. }
  34. return new CreateDirectoryResponse();
  35. }
  36. internal static SMB1Command GetDeleteDirectoryResponse(SMB1Header header, DeleteDirectoryRequest request, ISMBShare share, SMB1ConnectionState state)
  37. {
  38. SMB1Session session = state.GetSession(header.UID);
  39. if (share is FileSystemShare)
  40. {
  41. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.DirectoryName))
  42. {
  43. state.LogToServer(Severity.Verbose, "Delete Directory '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.DirectoryName, session.UserName);
  44. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  45. return new ErrorResponse(request.CommandName);
  46. }
  47. }
  48. header.Status = SMB1FileStoreHelper.DeleteDirectory(share.FileStore, request.DirectoryName, session.SecurityContext);
  49. if (header.Status != NTStatus.STATUS_SUCCESS)
  50. {
  51. return new ErrorResponse(request.CommandName);
  52. }
  53. return new DeleteDirectoryResponse();
  54. }
  55. internal static SMB1Command GetDeleteResponse(SMB1Header header, DeleteRequest request, ISMBShare share, SMB1ConnectionState state)
  56. {
  57. SMB1Session session = state.GetSession(header.UID);
  58. if (share is FileSystemShare)
  59. {
  60. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.FileName))
  61. {
  62. state.LogToServer(Severity.Verbose, "Delete '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.FileName, session.UserName);
  63. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  64. return new ErrorResponse(request.CommandName);
  65. }
  66. }
  67. // [MS-CIFS] This command cannot delete directories or volumes.
  68. header.Status = SMB1FileStoreHelper.DeleteFile(share.FileStore, request.FileName, session.SecurityContext);
  69. if (header.Status != NTStatus.STATUS_SUCCESS)
  70. {
  71. return new ErrorResponse(request.CommandName);
  72. }
  73. return new DeleteResponse();
  74. }
  75. internal static SMB1Command GetRenameResponse(SMB1Header header, RenameRequest request, ISMBShare share, SMB1ConnectionState state)
  76. {
  77. SMB1Session session = state.GetSession(header.UID);
  78. if (share is FileSystemShare)
  79. {
  80. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.OldFileName))
  81. {
  82. state.LogToServer(Severity.Verbose, "Rename '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.OldFileName, session.UserName);
  83. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  84. return new ErrorResponse(request.CommandName);
  85. }
  86. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.NewFileName))
  87. {
  88. state.LogToServer(Severity.Verbose, "Rename '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.OldFileName, session.UserName);
  89. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  90. return new ErrorResponse(request.CommandName);
  91. }
  92. }
  93. header.Status = SMB1FileStoreHelper.Rename(share.FileStore, request.OldFileName, request.NewFileName, request.SearchAttributes, session.SecurityContext);
  94. if (header.Status != NTStatus.STATUS_SUCCESS)
  95. {
  96. return new ErrorResponse(request.CommandName);
  97. }
  98. return new RenameResponse();
  99. }
  100. internal static SMB1Command GetCheckDirectoryResponse(SMB1Header header, CheckDirectoryRequest request, ISMBShare share, SMB1ConnectionState state)
  101. {
  102. SMB1Session session = state.GetSession(header.UID);
  103. if (share is FileSystemShare)
  104. {
  105. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, request.DirectoryName))
  106. {
  107. state.LogToServer(Severity.Verbose, "Check Directory '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.DirectoryName, session.UserName);
  108. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  109. return new ErrorResponse(request.CommandName);
  110. }
  111. }
  112. header.Status = SMB1FileStoreHelper.CheckDirectory(share.FileStore, request.DirectoryName, session.SecurityContext);
  113. if (header.Status != NTStatus.STATUS_SUCCESS)
  114. {
  115. return new ErrorResponse(request.CommandName);
  116. }
  117. return new CheckDirectoryResponse();
  118. }
  119. internal static SMB1Command GetQueryInformationResponse(SMB1Header header, QueryInformationRequest request, ISMBShare share, SMB1ConnectionState state)
  120. {
  121. SMB1Session session = state.GetSession(header.UID);
  122. if (share is FileSystemShare)
  123. {
  124. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, request.FileName))
  125. {
  126. state.LogToServer(Severity.Verbose, "Query Information on '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.FileName, session.UserName);
  127. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  128. return new ErrorResponse(request.CommandName);
  129. }
  130. }
  131. FileNetworkOpenInformation fileInfo;
  132. header.Status = SMB1FileStoreHelper.QueryInformation(out fileInfo, share.FileStore, request.FileName, session.SecurityContext);
  133. if (header.Status != NTStatus.STATUS_SUCCESS)
  134. {
  135. return new ErrorResponse(request.CommandName);
  136. }
  137. QueryInformationResponse response = new QueryInformationResponse();
  138. response.FileAttributes = SMB1FileStoreHelper.GetFileAttributes(fileInfo.FileAttributes);
  139. response.LastWriteTime = fileInfo.LastWriteTime;
  140. response.FileSize = (uint)Math.Min(UInt32.MaxValue, fileInfo.EndOfFile);
  141. return response;
  142. }
  143. internal static SMB1Command GetSetInformationResponse(SMB1Header header, SetInformationRequest request, ISMBShare share, SMB1ConnectionState state)
  144. {
  145. SMB1Session session = state.GetSession(header.UID);
  146. if (share is FileSystemShare)
  147. {
  148. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.FileName))
  149. {
  150. state.LogToServer(Severity.Verbose, "Set Information on '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.FileName, session.UserName);
  151. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  152. return new ErrorResponse(request.CommandName);
  153. }
  154. }
  155. header.Status = SMB1FileStoreHelper.SetInformation(share.FileStore, request.FileName, request.FileAttributes, request.LastWriteTime, session.SecurityContext);
  156. if (header.Status != NTStatus.STATUS_SUCCESS)
  157. {
  158. return new ErrorResponse(request.CommandName);
  159. }
  160. return new SetInformationResponse();
  161. }
  162. internal static SMB1Command GetSetInformation2Response(SMB1Header header, SetInformation2Request request, ISMBShare share, SMB1ConnectionState state)
  163. {
  164. SMB1Session session = state.GetSession(header.UID);
  165. OpenFileObject openFile = session.GetOpenFileObject(request.FID);
  166. if (openFile == null)
  167. {
  168. header.Status = NTStatus.STATUS_SMB_BAD_FID;
  169. return new ErrorResponse(request.CommandName);
  170. }
  171. if (share is FileSystemShare)
  172. {
  173. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, openFile.Path))
  174. {
  175. state.LogToServer(Severity.Verbose, "Set Information 2 on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  176. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  177. return new ErrorResponse(request.CommandName);
  178. }
  179. }
  180. header.Status = SMB1FileStoreHelper.SetInformation2(share.FileStore, openFile.Handle, request.CreationDateTime, request.LastAccessDateTime, request.LastWriteDateTime);
  181. if (header.Status != NTStatus.STATUS_SUCCESS)
  182. {
  183. return new ErrorResponse(request.CommandName);
  184. }
  185. return new SetInformation2Response();
  186. }
  187. }
  188. }