FileStoreResponseHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. string path = request.DirectoryName;
  104. if (!path.StartsWith(@"\"))
  105. {
  106. path = @"\" + path;
  107. }
  108. if (share is FileSystemShare)
  109. {
  110. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, path))
  111. {
  112. state.LogToServer(Severity.Verbose, "Check Directory '{0}{1}' failed. User '{2}' was denied access.", share.Name, path, session.UserName);
  113. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  114. return new ErrorResponse(request.CommandName);
  115. }
  116. }
  117. header.Status = SMB1FileStoreHelper.CheckDirectory(share.FileStore, path, session.SecurityContext);
  118. if (header.Status != NTStatus.STATUS_SUCCESS)
  119. {
  120. return new ErrorResponse(request.CommandName);
  121. }
  122. return new CheckDirectoryResponse();
  123. }
  124. internal static SMB1Command GetQueryInformationResponse(SMB1Header header, QueryInformationRequest request, ISMBShare share, SMB1ConnectionState state)
  125. {
  126. SMB1Session session = state.GetSession(header.UID);
  127. string path = request.FileName;
  128. if (!path.StartsWith(@"\"))
  129. {
  130. path = @"\" + path;
  131. }
  132. if (share is FileSystemShare)
  133. {
  134. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, path))
  135. {
  136. state.LogToServer(Severity.Verbose, "Query Information on '{0}{1}' failed. User '{2}' was denied access.", share.Name, path, session.UserName);
  137. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  138. return new ErrorResponse(request.CommandName);
  139. }
  140. }
  141. FileNetworkOpenInformation fileInfo;
  142. header.Status = SMB1FileStoreHelper.QueryInformation(out fileInfo, share.FileStore, path, session.SecurityContext);
  143. if (header.Status != NTStatus.STATUS_SUCCESS)
  144. {
  145. return new ErrorResponse(request.CommandName);
  146. }
  147. QueryInformationResponse response = new QueryInformationResponse();
  148. response.FileAttributes = SMB1FileStoreHelper.GetFileAttributes(fileInfo.FileAttributes);
  149. response.LastWriteTime = fileInfo.LastWriteTime;
  150. response.FileSize = (uint)Math.Min(UInt32.MaxValue, fileInfo.EndOfFile);
  151. return response;
  152. }
  153. internal static SMB1Command GetSetInformationResponse(SMB1Header header, SetInformationRequest request, ISMBShare share, SMB1ConnectionState state)
  154. {
  155. SMB1Session session = state.GetSession(header.UID);
  156. if (share is FileSystemShare)
  157. {
  158. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.FileName))
  159. {
  160. state.LogToServer(Severity.Verbose, "Set Information on '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.FileName, session.UserName);
  161. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  162. return new ErrorResponse(request.CommandName);
  163. }
  164. }
  165. header.Status = SMB1FileStoreHelper.SetInformation(share.FileStore, request.FileName, request.FileAttributes, request.LastWriteTime, session.SecurityContext);
  166. if (header.Status != NTStatus.STATUS_SUCCESS)
  167. {
  168. return new ErrorResponse(request.CommandName);
  169. }
  170. return new SetInformationResponse();
  171. }
  172. internal static SMB1Command GetSetInformation2Response(SMB1Header header, SetInformation2Request request, ISMBShare share, SMB1ConnectionState state)
  173. {
  174. SMB1Session session = state.GetSession(header.UID);
  175. OpenFileObject openFile = session.GetOpenFileObject(request.FID);
  176. if (openFile == null)
  177. {
  178. header.Status = NTStatus.STATUS_SMB_BAD_FID;
  179. return new ErrorResponse(request.CommandName);
  180. }
  181. if (share is FileSystemShare)
  182. {
  183. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, openFile.Path))
  184. {
  185. state.LogToServer(Severity.Verbose, "Set Information 2 on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  186. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  187. return new ErrorResponse(request.CommandName);
  188. }
  189. }
  190. header.Status = SMB1FileStoreHelper.SetInformation2(share.FileStore, openFile.Handle, request.CreationDateTime, request.LastAccessDateTime, request.LastWriteDateTime);
  191. if (header.Status != NTStatus.STATUS_SUCCESS)
  192. {
  193. return new ErrorResponse(request.CommandName);
  194. }
  195. return new SetInformation2Response();
  196. }
  197. }
  198. }