FileStoreResponseHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. state.LogToServer(Severity.Verbose, "Create Directory '{0}{1}' failed. NTStatus: {2}.", share.Name, request.DirectoryName, header.Status);
  33. return new ErrorResponse(request.CommandName);
  34. }
  35. state.LogToServer(Severity.Verbose, "Create Directory: User '{0}' created '{1}{2}'.", session.UserName, share.Name, request.DirectoryName);
  36. return new CreateDirectoryResponse();
  37. }
  38. internal static SMB1Command GetDeleteDirectoryResponse(SMB1Header header, DeleteDirectoryRequest request, ISMBShare share, SMB1ConnectionState state)
  39. {
  40. SMB1Session session = state.GetSession(header.UID);
  41. if (share is FileSystemShare)
  42. {
  43. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.DirectoryName))
  44. {
  45. state.LogToServer(Severity.Verbose, "Delete Directory '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.DirectoryName, session.UserName);
  46. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  47. return new ErrorResponse(request.CommandName);
  48. }
  49. }
  50. header.Status = SMB1FileStoreHelper.DeleteDirectory(share.FileStore, request.DirectoryName, session.SecurityContext);
  51. if (header.Status != NTStatus.STATUS_SUCCESS)
  52. {
  53. state.LogToServer(Severity.Verbose, "Delete Directory '{0}{1}' failed. NTStatus: {2}.", share.Name, request.DirectoryName, header.Status);
  54. return new ErrorResponse(request.CommandName);
  55. }
  56. state.LogToServer(Severity.Verbose, "Delete Directory: User '{0}' deleted '{1}{2}'.", session.UserName, share.Name, request.DirectoryName);
  57. return new DeleteDirectoryResponse();
  58. }
  59. internal static SMB1Command GetDeleteResponse(SMB1Header header, DeleteRequest request, ISMBShare share, SMB1ConnectionState state)
  60. {
  61. SMB1Session session = state.GetSession(header.UID);
  62. if (share is FileSystemShare)
  63. {
  64. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.FileName))
  65. {
  66. state.LogToServer(Severity.Verbose, "Delete '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.FileName, session.UserName);
  67. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  68. return new ErrorResponse(request.CommandName);
  69. }
  70. }
  71. // [MS-CIFS] This command cannot delete directories or volumes.
  72. header.Status = SMB1FileStoreHelper.DeleteFile(share.FileStore, request.FileName, session.SecurityContext);
  73. if (header.Status != NTStatus.STATUS_SUCCESS)
  74. {
  75. state.LogToServer(Severity.Verbose, "Delete '{0}{1}' failed. NTStatus: {2}.", share.Name, request.FileName, header.Status);
  76. return new ErrorResponse(request.CommandName);
  77. }
  78. state.LogToServer(Severity.Verbose, "Delete: User '{0}' deleted '{1}{2}'.", session.UserName, share.Name, request.FileName);
  79. return new DeleteResponse();
  80. }
  81. internal static SMB1Command GetRenameResponse(SMB1Header header, RenameRequest request, ISMBShare share, SMB1ConnectionState state)
  82. {
  83. SMB1Session session = state.GetSession(header.UID);
  84. if (share is FileSystemShare)
  85. {
  86. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.OldFileName))
  87. {
  88. state.LogToServer(Severity.Verbose, "Rename '{0}{1}' to '{0}{2}' failed. User '{3}' was denied access.", share.Name, request.OldFileName, request.NewFileName, session.UserName);
  89. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  90. return new ErrorResponse(request.CommandName);
  91. }
  92. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.NewFileName))
  93. {
  94. state.LogToServer(Severity.Verbose, "Rename '{0}{1}' to '{0}{2}' failed. User '{3}' was denied access.", share.Name, request.OldFileName, request.NewFileName, session.UserName);
  95. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  96. return new ErrorResponse(request.CommandName);
  97. }
  98. }
  99. header.Status = SMB1FileStoreHelper.Rename(share.FileStore, request.OldFileName, request.NewFileName, request.SearchAttributes, session.SecurityContext);
  100. if (header.Status != NTStatus.STATUS_SUCCESS)
  101. {
  102. state.LogToServer(Severity.Verbose, "Rename '{0}{1}' to '{0}{2}' failed. NTStatus: {3}.", share.Name, request.OldFileName, request.NewFileName, header.Status);
  103. return new ErrorResponse(request.CommandName);
  104. }
  105. state.LogToServer(Severity.Verbose, "Rename: User '{0}' renamed '{1}{2}' to '{1}{3}'.", session.UserName, share.Name, request.OldFileName, request.NewFileName);
  106. return new RenameResponse();
  107. }
  108. internal static SMB1Command GetCheckDirectoryResponse(SMB1Header header, CheckDirectoryRequest request, ISMBShare share, SMB1ConnectionState state)
  109. {
  110. SMB1Session session = state.GetSession(header.UID);
  111. string path = request.DirectoryName;
  112. if (!path.StartsWith(@"\"))
  113. {
  114. path = @"\" + path;
  115. }
  116. if (share is FileSystemShare)
  117. {
  118. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, path))
  119. {
  120. state.LogToServer(Severity.Verbose, "Check Directory '{0}{1}' failed. User '{2}' was denied access.", share.Name, path, session.UserName);
  121. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  122. return new ErrorResponse(request.CommandName);
  123. }
  124. }
  125. header.Status = SMB1FileStoreHelper.CheckDirectory(share.FileStore, path, session.SecurityContext);
  126. if (header.Status != NTStatus.STATUS_SUCCESS)
  127. {
  128. return new ErrorResponse(request.CommandName);
  129. }
  130. return new CheckDirectoryResponse();
  131. }
  132. internal static SMB1Command GetQueryInformationResponse(SMB1Header header, QueryInformationRequest request, ISMBShare share, SMB1ConnectionState state)
  133. {
  134. SMB1Session session = state.GetSession(header.UID);
  135. string path = request.FileName;
  136. if (!path.StartsWith(@"\"))
  137. {
  138. path = @"\" + path;
  139. }
  140. if (share is FileSystemShare)
  141. {
  142. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, path))
  143. {
  144. state.LogToServer(Severity.Verbose, "Query Information on '{0}{1}' failed. User '{2}' was denied access.", share.Name, path, session.UserName);
  145. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  146. return new ErrorResponse(request.CommandName);
  147. }
  148. }
  149. FileNetworkOpenInformation fileInfo;
  150. header.Status = SMB1FileStoreHelper.QueryInformation(out fileInfo, share.FileStore, path, session.SecurityContext);
  151. if (header.Status != NTStatus.STATUS_SUCCESS)
  152. {
  153. return new ErrorResponse(request.CommandName);
  154. }
  155. QueryInformationResponse response = new QueryInformationResponse();
  156. response.FileAttributes = SMB1FileStoreHelper.GetFileAttributes(fileInfo.FileAttributes);
  157. response.LastWriteTime = fileInfo.LastWriteTime;
  158. response.FileSize = (uint)Math.Min(UInt32.MaxValue, fileInfo.EndOfFile);
  159. return response;
  160. }
  161. internal static SMB1Command GetSetInformationResponse(SMB1Header header, SetInformationRequest request, ISMBShare share, SMB1ConnectionState state)
  162. {
  163. SMB1Session session = state.GetSession(header.UID);
  164. if (share is FileSystemShare)
  165. {
  166. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, request.FileName))
  167. {
  168. state.LogToServer(Severity.Verbose, "Set Information on '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.FileName, session.UserName);
  169. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  170. return new ErrorResponse(request.CommandName);
  171. }
  172. }
  173. header.Status = SMB1FileStoreHelper.SetInformation(share.FileStore, request.FileName, request.FileAttributes, request.LastWriteTime, session.SecurityContext);
  174. if (header.Status != NTStatus.STATUS_SUCCESS)
  175. {
  176. state.LogToServer(Severity.Verbose, "Set Information on '{0}{1}' failed. NTStatus: {2}", share.Name, request.FileName, header.Status);
  177. return new ErrorResponse(request.CommandName);
  178. }
  179. state.LogToServer(Severity.Verbose, "Set Information on '{0}{1}' succeeded.", share.Name, request.FileName);
  180. return new SetInformationResponse();
  181. }
  182. internal static SMB1Command GetSetInformation2Response(SMB1Header header, SetInformation2Request request, ISMBShare share, SMB1ConnectionState state)
  183. {
  184. SMB1Session session = state.GetSession(header.UID);
  185. OpenFileObject openFile = session.GetOpenFileObject(request.FID);
  186. if (openFile == null)
  187. {
  188. state.LogToServer(Severity.Verbose, "Set Information 2 failed. Invalid FID. (UID: {0}, TID: {1}, FID: {2})", header.UID, header.TID, request.FID);
  189. header.Status = NTStatus.STATUS_SMB_BAD_FID;
  190. return new ErrorResponse(request.CommandName);
  191. }
  192. if (share is FileSystemShare)
  193. {
  194. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, openFile.Path))
  195. {
  196. state.LogToServer(Severity.Verbose, "Set Information 2 on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  197. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  198. return new ErrorResponse(request.CommandName);
  199. }
  200. }
  201. header.Status = SMB1FileStoreHelper.SetInformation2(share.FileStore, openFile.Handle, request.CreationDateTime, request.LastAccessDateTime, request.LastWriteDateTime);
  202. if (header.Status != NTStatus.STATUS_SUCCESS)
  203. {
  204. state.LogToServer(Severity.Verbose, "Set Information 2 on '{0}{1}' failed. NTStatus: {2}", share.Name, openFile.Path, header.Status);
  205. return new ErrorResponse(request.CommandName);
  206. }
  207. state.LogToServer(Severity.Verbose, "Set Information 2 on '{0}{1}' succeeded.", share.Name, openFile.Path);
  208. return new SetInformation2Response();
  209. }
  210. }
  211. }