NTFileSystemHelper.Set.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Utilities;
  11. namespace SMBLibrary.Server
  12. {
  13. public partial class NTFileSystemHelper
  14. {
  15. public static NTStatus SetFileInformation(IFileSystem fileSystem, OpenFileObject openFile, FileInformation information, ConnectionState state)
  16. {
  17. if (information is FileBasicInformation)
  18. {
  19. FileBasicInformation basicInformation = (FileBasicInformation)information;
  20. bool isHidden = ((basicInformation.FileAttributes & FileAttributes.Hidden) > 0);
  21. bool isReadonly = (basicInformation.FileAttributes & FileAttributes.ReadOnly) > 0;
  22. bool isArchived = (basicInformation.FileAttributes & FileAttributes.Archive) > 0;
  23. try
  24. {
  25. fileSystem.SetAttributes(openFile.Path, isHidden, isReadonly, isArchived);
  26. }
  27. catch (UnauthorizedAccessException)
  28. {
  29. state.LogToServer(Severity.Debug, "SetFileInformation: Failed to set file attributes on '{0}'. Access Denied.", openFile.Path);
  30. return NTStatus.STATUS_ACCESS_DENIED;
  31. }
  32. try
  33. {
  34. fileSystem.SetDates(openFile.Path, basicInformation.CreationTime, basicInformation.LastWriteTime, basicInformation.LastAccessTime);
  35. }
  36. catch (IOException ex)
  37. {
  38. ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
  39. if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
  40. {
  41. state.LogToServer(Severity.Debug, "SetFileInformation: Failed to set file dates on '{0}'. Sharing Violation.", openFile.Path);
  42. return NTStatus.STATUS_SHARING_VIOLATION;
  43. }
  44. else
  45. {
  46. state.LogToServer(Severity.Debug, "SetFileInformation: Failed to set file dates on '{0}'. Data Error.", openFile.Path);
  47. return NTStatus.STATUS_DATA_ERROR;
  48. }
  49. }
  50. catch (UnauthorizedAccessException)
  51. {
  52. state.LogToServer(Severity.Debug, "SetFileInformation: Failed to set file dates on '{0}'. Access Denied.", openFile.Path);
  53. return NTStatus.STATUS_ACCESS_DENIED;
  54. }
  55. return NTStatus.STATUS_SUCCESS;
  56. }
  57. else if (information is FileRenameInformationType2)
  58. {
  59. FileRenameInformationType2 renameInformation = (FileRenameInformationType2)information;
  60. string destination = renameInformation.FileName;
  61. if (!destination.StartsWith(@"\"))
  62. {
  63. destination = @"\" + destination;
  64. }
  65. if (openFile.Stream != null)
  66. {
  67. openFile.Stream.Close();
  68. }
  69. try
  70. {
  71. if (renameInformation.ReplaceIfExists && (fileSystem.GetEntry(destination) != null ))
  72. {
  73. fileSystem.Delete(destination);
  74. }
  75. fileSystem.Move(openFile.Path, destination);
  76. }
  77. catch (IOException ex)
  78. {
  79. ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
  80. if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
  81. {
  82. state.LogToServer(Severity.Debug, "SetFileInformation: Cannot rename '{0}'. Sharing Violation.", openFile.Path);
  83. return NTStatus.STATUS_SHARING_VIOLATION;
  84. }
  85. if (errorCode == (ushort)Win32Error.ERROR_ALREADY_EXISTS)
  86. {
  87. state.LogToServer(Severity.Debug, "SetFileInformation: Cannot rename '{0}'. Already Exists.", openFile.Path);
  88. return NTStatus.STATUS_OBJECT_NAME_EXISTS;
  89. }
  90. else
  91. {
  92. state.LogToServer(Severity.Debug, "SetFileInformation: Cannot rename '{0}'. Data Error.", openFile.Path);
  93. return NTStatus.STATUS_DATA_ERROR;
  94. }
  95. }
  96. catch (UnauthorizedAccessException)
  97. {
  98. state.LogToServer(Severity.Debug, "SetFileInformation: Cannot rename '{0}'. Access Denied.", openFile.Path);
  99. return NTStatus.STATUS_ACCESS_DENIED;
  100. }
  101. openFile.Path = destination;
  102. return NTStatus.STATUS_SUCCESS;
  103. }
  104. else if (information is FileDispositionInformation)
  105. {
  106. if (((FileDispositionInformation)information).DeletePending)
  107. {
  108. // We're supposed to delete the file on close, but it's too late to report errors at this late stage
  109. if (openFile.Stream != null)
  110. {
  111. openFile.Stream.Close();
  112. }
  113. try
  114. {
  115. state.LogToServer(Severity.Information, "SetFileInformation: Deleting file '{0}'", openFile.Path);
  116. fileSystem.Delete(openFile.Path);
  117. }
  118. catch (IOException ex)
  119. {
  120. ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
  121. if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
  122. {
  123. state.LogToServer(Severity.Information, "SetFileInformation: Error deleting '{0}'. Sharing Violation.", openFile.Path);
  124. return NTStatus.STATUS_SHARING_VIOLATION;
  125. }
  126. else
  127. {
  128. state.LogToServer(Severity.Information, "SetFileInformation: Error deleting '{0}'. Data Error.", openFile.Path);
  129. return NTStatus.STATUS_DATA_ERROR;
  130. }
  131. }
  132. catch (UnauthorizedAccessException)
  133. {
  134. state.LogToServer(Severity.Information, "SetFileInformation: Error deleting '{0}', Access Denied.", openFile.Path);
  135. return NTStatus.STATUS_ACCESS_DENIED;
  136. }
  137. }
  138. return NTStatus.STATUS_SUCCESS;
  139. }
  140. else if (information is FileAllocationInformation)
  141. {
  142. ulong allocationSize = ((FileAllocationInformation)information).AllocationSize;
  143. try
  144. {
  145. openFile.Stream.SetLength((long)allocationSize);
  146. }
  147. catch (IOException ex)
  148. {
  149. ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
  150. if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
  151. {
  152. state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set allocation for '{0}'. Sharing Violation.", openFile.Path);
  153. return NTStatus.STATUS_SHARING_VIOLATION;
  154. }
  155. else
  156. {
  157. state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set allocation for '{0}'. Data Error.", openFile.Path);
  158. return NTStatus.STATUS_DATA_ERROR;
  159. }
  160. }
  161. catch (UnauthorizedAccessException)
  162. {
  163. state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set allocation for '{0}'. Access Denied.", openFile.Path);
  164. return NTStatus.STATUS_ACCESS_DENIED;
  165. }
  166. return NTStatus.STATUS_SUCCESS;
  167. }
  168. else if (information is FileEndOfFileInformation)
  169. {
  170. ulong endOfFile = ((FileEndOfFileInformation)information).EndOfFile;
  171. try
  172. {
  173. openFile.Stream.SetLength((long)endOfFile);
  174. }
  175. catch (IOException ex)
  176. {
  177. ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
  178. if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
  179. {
  180. state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set end of file for '{0}'. Sharing Violation.", openFile.Path);
  181. return NTStatus.STATUS_SHARING_VIOLATION;
  182. }
  183. else
  184. {
  185. state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set end of file for '{0}'. Data Error.", openFile.Path);
  186. return NTStatus.STATUS_DATA_ERROR;
  187. }
  188. }
  189. catch (UnauthorizedAccessException)
  190. {
  191. state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set end of file for '{0}'. Access Denied.", openFile.Path);
  192. return NTStatus.STATUS_ACCESS_DENIED;
  193. }
  194. return NTStatus.STATUS_SUCCESS;
  195. }
  196. else
  197. {
  198. return NTStatus.STATUS_NOT_IMPLEMENTED;
  199. }
  200. }
  201. }
  202. }