SMB1FileStoreHelper.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.Text;
  10. using SMBLibrary.SMB1;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB1
  13. {
  14. internal partial class SMB1FileStoreHelper
  15. {
  16. public static NTStatus CreateDirectory(INTFileStore fileStore, string path, SecurityContext securityContext)
  17. {
  18. object handle;
  19. FileStatus fileStatus;
  20. NTStatus createStatus = fileStore.CreateFile(out handle, out fileStatus, path, (AccessMask)DirectoryAccessMask.FILE_ADD_SUBDIRECTORY, 0, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_CREATE, CreateOptions.FILE_DIRECTORY_FILE, securityContext);
  21. if (createStatus != NTStatus.STATUS_SUCCESS)
  22. {
  23. return createStatus;
  24. }
  25. fileStore.CloseFile(handle);
  26. return createStatus;
  27. }
  28. public static NTStatus DeleteDirectory(INTFileStore fileStore, string path, SecurityContext securityContext)
  29. {
  30. return Delete(fileStore, path, CreateOptions.FILE_DIRECTORY_FILE, securityContext);
  31. }
  32. public static NTStatus DeleteFile(INTFileStore fileStore, string path, SecurityContext securityContext)
  33. {
  34. return Delete(fileStore, path, CreateOptions.FILE_NON_DIRECTORY_FILE, securityContext);
  35. }
  36. public static NTStatus Delete(INTFileStore fileStore, string path, CreateOptions createOptions, SecurityContext securityContext)
  37. {
  38. object handle;
  39. FileStatus fileStatus;
  40. ShareAccess shareAccess = ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete;
  41. NTStatus status = fileStore.CreateFile(out handle, out fileStatus, path, AccessMask.DELETE, 0, shareAccess, CreateDisposition.FILE_OPEN, createOptions, securityContext);
  42. if (status != NTStatus.STATUS_SUCCESS)
  43. {
  44. return status;
  45. }
  46. FileDispositionInformation fileDispositionInfo = new FileDispositionInformation();
  47. fileDispositionInfo.DeletePending = true;
  48. status = fileStore.SetFileInformation(handle, fileDispositionInfo);
  49. fileStore.CloseFile(handle);
  50. return status;
  51. }
  52. public static NTStatus Rename(INTFileStore fileStore, string oldName, string newName, SMBFileAttributes searchAttributes, SecurityContext securityContext)
  53. {
  54. object handle;
  55. FileStatus fileStatus;
  56. CreateOptions createOptions = 0;
  57. // Windows 2000 SP4 clients will use this command to rename directories.
  58. // Hidden, System and Directory attributes are inclusive.
  59. if ((searchAttributes & SMBFileAttributes.Directory) == 0)
  60. {
  61. createOptions = CreateOptions.FILE_NON_DIRECTORY_FILE;
  62. }
  63. ShareAccess shareAccess = ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete;
  64. NTStatus status = fileStore.CreateFile(out handle, out fileStatus, oldName, AccessMask.DELETE, 0, shareAccess, CreateDisposition.FILE_OPEN, createOptions, securityContext);
  65. if (status != NTStatus.STATUS_SUCCESS)
  66. {
  67. return status;
  68. }
  69. FileRenameInformationType2 renameInfo = new FileRenameInformationType2();
  70. renameInfo.ReplaceIfExists = false;
  71. renameInfo.FileName = newName;
  72. status = fileStore.SetFileInformation(handle, renameInfo);
  73. fileStore.CloseFile(handle);
  74. return status;
  75. }
  76. public static NTStatus CheckDirectory(INTFileStore fileStore, string path, SecurityContext securityContext)
  77. {
  78. object handle;
  79. FileStatus fileStatus;
  80. NTStatus openStatus = fileStore.CreateFile(out handle, out fileStatus, path, (AccessMask)0, 0, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, securityContext);
  81. if (openStatus != NTStatus.STATUS_SUCCESS)
  82. {
  83. return openStatus;
  84. }
  85. fileStore.CloseFile(handle);
  86. return NTStatus.STATUS_SUCCESS;
  87. }
  88. public static NTStatus QueryInformation(out FileNetworkOpenInformation fileInfo, INTFileStore fileStore, string path, SecurityContext securityContext)
  89. {
  90. object handle;
  91. FileStatus fileStatus;
  92. NTStatus openStatus = fileStore.CreateFile(out handle, out fileStatus, path, (AccessMask)FileAccessMask.FILE_READ_ATTRIBUTES, 0, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, 0, securityContext);
  93. if (openStatus != NTStatus.STATUS_SUCCESS)
  94. {
  95. fileInfo = null;
  96. return openStatus;
  97. }
  98. fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(fileStore, handle);
  99. fileStore.CloseFile(handle);
  100. return NTStatus.STATUS_SUCCESS;
  101. }
  102. public static NTStatus SetInformation(INTFileStore fileStore, string path, SMBFileAttributes fileAttributes, DateTime? lastWriteTime, SecurityContext securityContext)
  103. {
  104. object handle;
  105. FileStatus fileStatus;
  106. NTStatus status = fileStore.CreateFile(out handle, out fileStatus, path, (AccessMask)FileAccessMask.FILE_WRITE_ATTRIBUTES, (FileAttributes)0, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, 0, securityContext);
  107. if (status != NTStatus.STATUS_SUCCESS)
  108. {
  109. return status;
  110. }
  111. FileBasicInformation basicInfo = new FileBasicInformation();
  112. basicInfo.LastWriteTime = lastWriteTime;
  113. if ((fileAttributes & SMBFileAttributes.Hidden) > 0)
  114. {
  115. basicInfo.FileAttributes |= FileAttributes.Hidden;
  116. }
  117. if ((fileAttributes & SMBFileAttributes.ReadOnly) > 0)
  118. {
  119. basicInfo.FileAttributes |= FileAttributes.ReadOnly;
  120. }
  121. if ((fileAttributes & SMBFileAttributes.Archive) > 0)
  122. {
  123. basicInfo.FileAttributes |= FileAttributes.Archive;
  124. }
  125. status = fileStore.SetFileInformation(handle, basicInfo);
  126. fileStore.CloseFile(handle);
  127. return status;
  128. }
  129. public static NTStatus SetInformation2(INTFileStore fileStore, object handle, DateTime? creationTime, DateTime? lastAccessTime, DateTime? lastWriteTime)
  130. {
  131. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(fileStore, handle);
  132. FileBasicInformation basicInfo = new FileBasicInformation();
  133. basicInfo.FileAttributes = fileInfo.FileAttributes;
  134. basicInfo.CreationTime = creationTime;
  135. basicInfo.LastAccessTime = lastAccessTime;
  136. basicInfo.LastWriteTime = lastWriteTime;
  137. return fileStore.SetFileInformation(handle, basicInfo);
  138. }
  139. public static SMBFileAttributes GetFileAttributes(FileAttributes attributes)
  140. {
  141. SMBFileAttributes result = SMBFileAttributes.Normal;
  142. if ((attributes & FileAttributes.Hidden) > 0)
  143. {
  144. result |= SMBFileAttributes.Hidden;
  145. }
  146. if ((attributes & FileAttributes.ReadOnly) > 0)
  147. {
  148. result |= SMBFileAttributes.ReadOnly;
  149. }
  150. if ((attributes & FileAttributes.Archive) > 0)
  151. {
  152. result |= SMBFileAttributes.Archive;
  153. }
  154. if ((attributes & FileAttributes.Directory) > 0)
  155. {
  156. result |= SMBFileAttributes.Directory;
  157. }
  158. return result;
  159. }
  160. }
  161. }