OpenAndXHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. ushort? fileID = session.AddOpenFile(header.TID, path, handle);
  58. if (!fileID.HasValue)
  59. {
  60. share.FileStore.CloseFile(handle);
  61. header.Status = NTStatus.STATUS_TOO_MANY_OPENED_FILES;
  62. return new ErrorResponse(request.CommandName);
  63. }
  64. OpenResult openResult = ToOpenResult(fileStatus);
  65. if (share is NamedPipeShare)
  66. {
  67. if (isExtended)
  68. {
  69. return CreateResponseExtendedForNamedPipe(fileID.Value, openResult);
  70. }
  71. else
  72. {
  73. return CreateResponseForNamedPipe(fileID.Value, openResult);
  74. }
  75. }
  76. else // FileSystemShare
  77. {
  78. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, handle);
  79. if (isExtended)
  80. {
  81. return CreateResponseExtendedFromFileInfo(fileInfo, fileID.Value, openResult);
  82. }
  83. else
  84. {
  85. return CreateResponseFromFileInfo(fileInfo, fileID.Value, openResult);
  86. }
  87. }
  88. }
  89. private static AccessMask ToAccessMask(AccessMode accessMode)
  90. {
  91. if (accessMode == AccessMode.Read)
  92. {
  93. return FileAccessMask.GENERIC_READ;
  94. }
  95. if (accessMode == AccessMode.Write)
  96. {
  97. return FileAccessMask.GENERIC_WRITE | FileAccessMask.FILE_READ_ATTRIBUTES;
  98. }
  99. else if (accessMode == AccessMode.ReadWrite)
  100. {
  101. return FileAccessMask.GENERIC_READ | FileAccessMask.GENERIC_WRITE;
  102. }
  103. else if (accessMode == AccessMode.Execute)
  104. {
  105. return FileAccessMask.GENERIC_READ | FileAccessMask.GENERIC_EXECUTE;
  106. }
  107. else
  108. {
  109. throw new ArgumentException("Invalid AccessMode value");
  110. }
  111. }
  112. private static FileAccess ToFileAccess(AccessMode accessMode)
  113. {
  114. if (accessMode == AccessMode.Write)
  115. {
  116. return FileAccess.Write;
  117. }
  118. else if (accessMode == AccessMode.ReadWrite)
  119. {
  120. return FileAccess.ReadWrite;
  121. }
  122. else
  123. {
  124. return FileAccess.Read;
  125. }
  126. }
  127. private static ShareAccess ToShareAccess(SharingMode sharingMode)
  128. {
  129. if (sharingMode == SharingMode.Compatibility)
  130. {
  131. return ShareAccess.FILE_SHARE_READ;
  132. }
  133. else if (sharingMode == SharingMode.DenyReadWriteExecute)
  134. {
  135. return 0;
  136. }
  137. else if (sharingMode == SharingMode.DenyWrite)
  138. {
  139. return ShareAccess.FILE_SHARE_READ;
  140. }
  141. else if (sharingMode == SharingMode.DenyReadExecute)
  142. {
  143. return ShareAccess.FILE_SHARE_WRITE;
  144. }
  145. else if (sharingMode == SharingMode.DenyNothing)
  146. {
  147. return ShareAccess.FILE_SHARE_READ | ShareAccess.FILE_SHARE_WRITE;
  148. }
  149. else if (sharingMode == (SharingMode)0xFF)
  150. {
  151. return 0;
  152. }
  153. else
  154. {
  155. throw new ArgumentException("Invalid SharingMode value");
  156. }
  157. }
  158. private static CreateDisposition ToCreateDisposition(OpenMode openMode)
  159. {
  160. if (openMode.CreateFile == CreateFile.ReturnErrorIfNotExist)
  161. {
  162. if (openMode.FileExistsOpts == FileExistsOpts.ReturnError)
  163. {
  164. throw new ArgumentException("Invalid OpenMode combination");
  165. }
  166. else if (openMode.FileExistsOpts == FileExistsOpts.Append)
  167. {
  168. return CreateDisposition.FILE_OPEN;
  169. }
  170. else if (openMode.FileExistsOpts == FileExistsOpts.TruncateToZero)
  171. {
  172. return CreateDisposition.FILE_OVERWRITE;
  173. }
  174. }
  175. else if (openMode.CreateFile == CreateFile.CreateIfNotExist)
  176. {
  177. if (openMode.FileExistsOpts == FileExistsOpts.ReturnError)
  178. {
  179. return CreateDisposition.FILE_CREATE;
  180. }
  181. else if (openMode.FileExistsOpts == FileExistsOpts.Append)
  182. {
  183. return CreateDisposition.FILE_OPEN_IF;
  184. }
  185. else if (openMode.FileExistsOpts == FileExistsOpts.TruncateToZero)
  186. {
  187. return CreateDisposition.FILE_OVERWRITE_IF;
  188. }
  189. }
  190. throw new ArgumentException("Invalid OpenMode combination");
  191. }
  192. private static CreateOptions ToCreateOptions(AccessModeOptions accessModeOptions)
  193. {
  194. CreateOptions result = CreateOptions.FILE_NON_DIRECTORY_FILE | CreateOptions.FILE_COMPLETE_IF_OPLOCKED;
  195. if (accessModeOptions.ReferenceLocality == ReferenceLocality.Sequential)
  196. {
  197. result |= CreateOptions.FILE_SEQUENTIAL_ONLY;
  198. }
  199. else if (accessModeOptions.ReferenceLocality == ReferenceLocality.Random)
  200. {
  201. result |= CreateOptions.FILE_RANDOM_ACCESS;
  202. }
  203. else if (accessModeOptions.ReferenceLocality == ReferenceLocality.RandomWithLocality)
  204. {
  205. result |= CreateOptions.FILE_RANDOM_ACCESS;
  206. }
  207. if (accessModeOptions.CachedMode == CachedMode.DoNotCacheFile)
  208. {
  209. result |= CreateOptions.FILE_NO_INTERMEDIATE_BUFFERING;
  210. }
  211. if (accessModeOptions.WriteThroughMode == WriteThroughMode.WriteThrough)
  212. {
  213. result |= CreateOptions.FILE_WRITE_THROUGH;
  214. }
  215. return result;
  216. }
  217. private static OpenResult ToOpenResult(FileStatus fileStatus)
  218. {
  219. if (fileStatus == FileStatus.FILE_OVERWRITTEN ||
  220. fileStatus == FileStatus.FILE_SUPERSEDED)
  221. {
  222. return OpenResult.FileExistedAndWasTruncated;
  223. }
  224. else if (fileStatus == FileStatus.FILE_CREATED)
  225. {
  226. return OpenResult.NotExistedAndWasCreated;
  227. }
  228. else
  229. {
  230. return OpenResult.FileExistedAndWasOpened;
  231. }
  232. }
  233. private static OpenAndXResponse CreateResponseForNamedPipe(ushort fileID, OpenResult openResult)
  234. {
  235. OpenAndXResponse response = new OpenAndXResponse();
  236. response.FID = fileID;
  237. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ_WRITE;
  238. response.ResourceType = ResourceType.FileTypeMessageModePipe;
  239. response.NMPipeStatus.ICount = 255;
  240. response.NMPipeStatus.ReadMode = ReadMode.MessageMode;
  241. response.NMPipeStatus.NamedPipeType = NamedPipeType.MessageNodePipe;
  242. response.OpenResults.OpenResult = openResult;
  243. return response;
  244. }
  245. private static OpenAndXResponseExtended CreateResponseExtendedForNamedPipe(ushort fileID, OpenResult openResult)
  246. {
  247. OpenAndXResponseExtended response = new OpenAndXResponseExtended();
  248. response.FID = fileID;
  249. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ_WRITE;
  250. response.ResourceType = ResourceType.FileTypeMessageModePipe;
  251. response.NMPipeStatus.ICount = 255;
  252. response.NMPipeStatus.ReadMode = ReadMode.MessageMode;
  253. response.NMPipeStatus.NamedPipeType = NamedPipeType.MessageNodePipe;
  254. response.OpenResults.OpenResult = openResult;
  255. return response;
  256. }
  257. private static OpenAndXResponse CreateResponseFromFileInfo(FileNetworkOpenInformation fileInfo, ushort fileID, OpenResult openResult)
  258. {
  259. OpenAndXResponse response = new OpenAndXResponse();
  260. response.FID = fileID;
  261. response.FileAttrs = SMB1FileStoreHelper.GetFileAttributes(fileInfo.FileAttributes);
  262. response.LastWriteTime = fileInfo.LastWriteTime;
  263. response.FileDataSize = (uint)Math.Min(UInt32.MaxValue, fileInfo.EndOfFile);
  264. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ;
  265. response.ResourceType = ResourceType.FileTypeDisk;
  266. response.OpenResults.OpenResult = openResult;
  267. return response;
  268. }
  269. private static OpenAndXResponseExtended CreateResponseExtendedFromFileInfo(FileNetworkOpenInformation fileInfo, ushort fileID, OpenResult openResult)
  270. {
  271. OpenAndXResponseExtended response = new OpenAndXResponseExtended();
  272. response.FID = fileID;
  273. response.FileAttrs = SMB1FileStoreHelper.GetFileAttributes(fileInfo.FileAttributes);
  274. response.LastWriteTime = fileInfo.LastWriteTime;
  275. response.FileDataSize = (uint)Math.Min(UInt32.MaxValue, fileInfo.EndOfFile);
  276. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ;
  277. response.ResourceType = ResourceType.FileTypeDisk;
  278. response.OpenResults.OpenResult = openResult;
  279. response.MaximalAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  280. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  281. FileAccessMask.FILE_EXECUTE |
  282. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  283. FileAccessMask.DELETE | FileAccessMask.READ_CONTROL | FileAccessMask.WRITE_DAC | FileAccessMask.WRITE_OWNER | FileAccessMask.SYNCHRONIZE;
  284. response.GuestMaximalAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA |
  285. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  286. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  287. FileAccessMask.READ_CONTROL | FileAccessMask.SYNCHRONIZE;
  288. return response;
  289. }
  290. }
  291. }