OpenAndXHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.Services;
  12. using SMBLibrary.SMB1;
  13. using Utilities;
  14. namespace SMBLibrary.Server.SMB1
  15. {
  16. internal class OpenAndXHelper
  17. {
  18. internal static SMB1Command GetOpenAndXResponse(SMB1Header header, OpenAndXRequest request, ISMBShare share, SMB1ConnectionState state)
  19. {
  20. SMB1Session session = state.GetSession(header.UID);
  21. bool isExtended = (request.Flags & OpenFlags.SMB_OPEN_EXTENDED_RESPONSE) > 0;
  22. string path = request.FileName;
  23. AccessMask desiredAccess;
  24. ShareAccess shareAccess;
  25. CreateDisposition createDisposition;
  26. try
  27. {
  28. desiredAccess = ToAccessMask(request.AccessMode.AccessMode);
  29. shareAccess = ToShareAccess(request.AccessMode.SharingMode);
  30. createDisposition = ToCreateDisposition(request.OpenMode);
  31. }
  32. catch (ArgumentException)
  33. {
  34. // Invalid input according to MS-CIFS
  35. header.Status = NTStatus.STATUS_OS2_INVALID_ACCESS;
  36. return new ErrorResponse(request.CommandName);
  37. }
  38. CreateOptions createOptions = ToCreateOptions(request.AccessMode);
  39. FileAccess fileAccess = ToFileAccess(request.AccessMode.AccessMode);
  40. if (share is FileSystemShare)
  41. {
  42. if (!((FileSystemShare)share).HasAccess(session.SecurityContext, path, fileAccess))
  43. {
  44. state.LogToServer(Severity.Verbose, "OpenAndX: Opening '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.FileName, session.UserName);
  45. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  46. return new ErrorResponse(request.CommandName);
  47. }
  48. }
  49. object handle;
  50. FileStatus fileStatus;
  51. header.Status = share.FileStore.CreateFile(out handle, out fileStatus, path, desiredAccess, shareAccess, createDisposition, createOptions, session.SecurityContext);
  52. if (header.Status != NTStatus.STATUS_SUCCESS)
  53. {
  54. state.LogToServer(Severity.Verbose, "OpenAndX: Opening '{0}{1}' failed. NTStatus: '{2}'.", share.Name, path, header.Status);
  55. return new ErrorResponse(request.CommandName);
  56. }
  57. state.LogToServer(Severity.Verbose, "OpenAndX: Opened '{0}{1}'.", share.Name, path);
  58. ushort? fileID = session.AddOpenFile(header.TID, path, handle);
  59. if (!fileID.HasValue)
  60. {
  61. share.FileStore.CloseFile(handle);
  62. header.Status = NTStatus.STATUS_TOO_MANY_OPENED_FILES;
  63. return new ErrorResponse(request.CommandName);
  64. }
  65. OpenResult openResult = ToOpenResult(fileStatus);
  66. if (share is NamedPipeShare)
  67. {
  68. if (isExtended)
  69. {
  70. return CreateResponseExtendedForNamedPipe(fileID.Value, openResult);
  71. }
  72. else
  73. {
  74. return CreateResponseForNamedPipe(fileID.Value, openResult);
  75. }
  76. }
  77. else // FileSystemShare
  78. {
  79. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, handle);
  80. if (isExtended)
  81. {
  82. return CreateResponseExtendedFromFileInfo(fileInfo, fileID.Value, openResult);
  83. }
  84. else
  85. {
  86. return CreateResponseFromFileInfo(fileInfo, fileID.Value, openResult);
  87. }
  88. }
  89. }
  90. private static AccessMask ToAccessMask(AccessMode accessMode)
  91. {
  92. if (accessMode == AccessMode.Read)
  93. {
  94. return FileAccessMask.GENERIC_READ;
  95. }
  96. if (accessMode == AccessMode.Write)
  97. {
  98. return FileAccessMask.GENERIC_WRITE | FileAccessMask.FILE_READ_ATTRIBUTES;
  99. }
  100. else if (accessMode == AccessMode.ReadWrite)
  101. {
  102. return FileAccessMask.GENERIC_READ | FileAccessMask.GENERIC_WRITE;
  103. }
  104. else if (accessMode == AccessMode.Execute)
  105. {
  106. return FileAccessMask.GENERIC_READ | FileAccessMask.GENERIC_EXECUTE;
  107. }
  108. else
  109. {
  110. throw new ArgumentException("Invalid AccessMode value");
  111. }
  112. }
  113. private static FileAccess ToFileAccess(AccessMode accessMode)
  114. {
  115. if (accessMode == AccessMode.Write)
  116. {
  117. return FileAccess.Write;
  118. }
  119. else if (accessMode == AccessMode.ReadWrite)
  120. {
  121. return FileAccess.ReadWrite;
  122. }
  123. else
  124. {
  125. return FileAccess.Read;
  126. }
  127. }
  128. private static ShareAccess ToShareAccess(SharingMode sharingMode)
  129. {
  130. if (sharingMode == SharingMode.Compatibility)
  131. {
  132. return ShareAccess.FILE_SHARE_READ;
  133. }
  134. else if (sharingMode == SharingMode.DenyReadWriteExecute)
  135. {
  136. return 0;
  137. }
  138. else if (sharingMode == SharingMode.DenyWrite)
  139. {
  140. return ShareAccess.FILE_SHARE_READ;
  141. }
  142. else if (sharingMode == SharingMode.DenyReadExecute)
  143. {
  144. return ShareAccess.FILE_SHARE_WRITE;
  145. }
  146. else if (sharingMode == SharingMode.DenyNothing)
  147. {
  148. return ShareAccess.FILE_SHARE_READ | ShareAccess.FILE_SHARE_WRITE;
  149. }
  150. else if (sharingMode == (SharingMode)0xFF)
  151. {
  152. return 0;
  153. }
  154. else
  155. {
  156. throw new ArgumentException("Invalid SharingMode value");
  157. }
  158. }
  159. private static CreateDisposition ToCreateDisposition(OpenMode openMode)
  160. {
  161. if (openMode.CreateFile == CreateFile.ReturnErrorIfNotExist)
  162. {
  163. if (openMode.FileExistsOpts == FileExistsOpts.ReturnError)
  164. {
  165. throw new ArgumentException("Invalid OpenMode combination");
  166. }
  167. else if (openMode.FileExistsOpts == FileExistsOpts.Append)
  168. {
  169. return CreateDisposition.FILE_OPEN;
  170. }
  171. else if (openMode.FileExistsOpts == FileExistsOpts.TruncateToZero)
  172. {
  173. return CreateDisposition.FILE_OVERWRITE;
  174. }
  175. }
  176. else if (openMode.CreateFile == CreateFile.CreateIfNotExist)
  177. {
  178. if (openMode.FileExistsOpts == FileExistsOpts.ReturnError)
  179. {
  180. return CreateDisposition.FILE_CREATE;
  181. }
  182. else if (openMode.FileExistsOpts == FileExistsOpts.Append)
  183. {
  184. return CreateDisposition.FILE_OPEN_IF;
  185. }
  186. else if (openMode.FileExistsOpts == FileExistsOpts.TruncateToZero)
  187. {
  188. return CreateDisposition.FILE_OVERWRITE_IF;
  189. }
  190. }
  191. throw new ArgumentException("Invalid OpenMode combination");
  192. }
  193. private static CreateOptions ToCreateOptions(AccessModeOptions accessModeOptions)
  194. {
  195. CreateOptions result = CreateOptions.FILE_NON_DIRECTORY_FILE | CreateOptions.FILE_COMPLETE_IF_OPLOCKED;
  196. if (accessModeOptions.ReferenceLocality == ReferenceLocality.Sequential)
  197. {
  198. result |= CreateOptions.FILE_SEQUENTIAL_ONLY;
  199. }
  200. else if (accessModeOptions.ReferenceLocality == ReferenceLocality.Random)
  201. {
  202. result |= CreateOptions.FILE_RANDOM_ACCESS;
  203. }
  204. else if (accessModeOptions.ReferenceLocality == ReferenceLocality.RandomWithLocality)
  205. {
  206. result |= CreateOptions.FILE_RANDOM_ACCESS;
  207. }
  208. if (accessModeOptions.CachedMode == CachedMode.DoNotCacheFile)
  209. {
  210. result |= CreateOptions.FILE_NO_INTERMEDIATE_BUFFERING;
  211. }
  212. if (accessModeOptions.WriteThroughMode == WriteThroughMode.WriteThrough)
  213. {
  214. result |= CreateOptions.FILE_WRITE_THROUGH;
  215. }
  216. return result;
  217. }
  218. private static OpenResult ToOpenResult(FileStatus fileStatus)
  219. {
  220. if (fileStatus == FileStatus.FILE_OVERWRITTEN ||
  221. fileStatus == FileStatus.FILE_SUPERSEDED)
  222. {
  223. return OpenResult.FileExistedAndWasTruncated;
  224. }
  225. else if (fileStatus == FileStatus.FILE_CREATED)
  226. {
  227. return OpenResult.NotExistedAndWasCreated;
  228. }
  229. else
  230. {
  231. return OpenResult.FileExistedAndWasOpened;
  232. }
  233. }
  234. private static OpenAndXResponse CreateResponseForNamedPipe(ushort fileID, OpenResult openResult)
  235. {
  236. OpenAndXResponse response = new OpenAndXResponse();
  237. response.FID = fileID;
  238. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ_WRITE;
  239. response.ResourceType = ResourceType.FileTypeMessageModePipe;
  240. response.NMPipeStatus.ICount = 255;
  241. response.NMPipeStatus.ReadMode = ReadMode.MessageMode;
  242. response.NMPipeStatus.NamedPipeType = NamedPipeType.MessageNodePipe;
  243. response.OpenResults.OpenResult = openResult;
  244. return response;
  245. }
  246. private static OpenAndXResponseExtended CreateResponseExtendedForNamedPipe(ushort fileID, OpenResult openResult)
  247. {
  248. OpenAndXResponseExtended response = new OpenAndXResponseExtended();
  249. response.FID = fileID;
  250. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ_WRITE;
  251. response.ResourceType = ResourceType.FileTypeMessageModePipe;
  252. response.NMPipeStatus.ICount = 255;
  253. response.NMPipeStatus.ReadMode = ReadMode.MessageMode;
  254. response.NMPipeStatus.NamedPipeType = NamedPipeType.MessageNodePipe;
  255. response.OpenResults.OpenResult = openResult;
  256. return response;
  257. }
  258. private static OpenAndXResponse CreateResponseFromFileInfo(FileNetworkOpenInformation fileInfo, ushort fileID, OpenResult openResult)
  259. {
  260. OpenAndXResponse response = new OpenAndXResponse();
  261. response.FID = fileID;
  262. response.FileAttrs = SMB1FileStoreHelper.GetFileAttributes(fileInfo.FileAttributes);
  263. response.LastWriteTime = fileInfo.LastWriteTime;
  264. response.FileDataSize = (uint)Math.Min(UInt32.MaxValue, fileInfo.EndOfFile);
  265. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ;
  266. response.ResourceType = ResourceType.FileTypeDisk;
  267. response.OpenResults.OpenResult = openResult;
  268. return response;
  269. }
  270. private static OpenAndXResponseExtended CreateResponseExtendedFromFileInfo(FileNetworkOpenInformation fileInfo, ushort fileID, OpenResult openResult)
  271. {
  272. OpenAndXResponseExtended response = new OpenAndXResponseExtended();
  273. response.FID = fileID;
  274. response.FileAttrs = SMB1FileStoreHelper.GetFileAttributes(fileInfo.FileAttributes);
  275. response.LastWriteTime = fileInfo.LastWriteTime;
  276. response.FileDataSize = (uint)Math.Min(UInt32.MaxValue, fileInfo.EndOfFile);
  277. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ;
  278. response.ResourceType = ResourceType.FileTypeDisk;
  279. response.OpenResults.OpenResult = openResult;
  280. response.MaximalAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  281. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  282. FileAccessMask.FILE_EXECUTE |
  283. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  284. FileAccessMask.DELETE | FileAccessMask.READ_CONTROL | FileAccessMask.WRITE_DAC | FileAccessMask.WRITE_OWNER | FileAccessMask.SYNCHRONIZE;
  285. response.GuestMaximalAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA |
  286. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  287. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  288. FileAccessMask.READ_CONTROL | FileAccessMask.SYNCHRONIZE;
  289. return response;
  290. }
  291. }
  292. }