SMB1FileStoreHelper.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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, DirectoryAccessMask.FILE_ADD_SUBDIRECTORY, 0, ShareAccess.FILE_SHARE_READ | ShareAccess.FILE_SHARE_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. NTStatus status = fileStore.CreateFile(out handle, out fileStatus, path, DirectoryAccessMask.DELETE, 0, 0, CreateDisposition.FILE_OPEN, createOptions, securityContext);
  41. if (status != NTStatus.STATUS_SUCCESS)
  42. {
  43. return status;
  44. }
  45. FileDispositionInformation fileDispositionInfo = new FileDispositionInformation();
  46. fileDispositionInfo.DeletePending = true;
  47. status = fileStore.SetFileInformation(handle, fileDispositionInfo);
  48. if (status != NTStatus.STATUS_SUCCESS)
  49. {
  50. return status;
  51. }
  52. fileStore.CloseFile(handle);
  53. return status;
  54. }
  55. public static NTStatus Rename(INTFileStore fileStore, string oldName, string newName, SMBFileAttributes searchAttributes, SecurityContext securityContext)
  56. {
  57. object handle;
  58. FileStatus fileStatus;
  59. CreateOptions createOptions = 0;
  60. // Windows 2000 SP4 clients will use this command to rename directories.
  61. // Hidden, System and Directory attributes are inclusive.
  62. if ((searchAttributes & SMBFileAttributes.Directory) == 0)
  63. {
  64. createOptions = CreateOptions.FILE_NON_DIRECTORY_FILE;
  65. }
  66. NTStatus status = fileStore.CreateFile(out handle, out fileStatus, oldName, DirectoryAccessMask.DELETE, 0, 0, CreateDisposition.FILE_OPEN, createOptions, securityContext);
  67. if (status != NTStatus.STATUS_SUCCESS)
  68. {
  69. return status;
  70. }
  71. FileRenameInformationType2 renameInfo = new FileRenameInformationType2();
  72. renameInfo.ReplaceIfExists = false;
  73. renameInfo.FileName = newName;
  74. status = fileStore.SetFileInformation(handle, renameInfo);
  75. if (status != NTStatus.STATUS_SUCCESS)
  76. {
  77. return status;
  78. }
  79. fileStore.CloseFile(handle);
  80. return status;
  81. }
  82. public static NTStatus CheckDirectory(INTFileStore fileStore, string path, SecurityContext securityContext)
  83. {
  84. object handle;
  85. FileStatus fileStatus;
  86. NTStatus openStatus = fileStore.CreateFile(out handle, out fileStatus, path, (AccessMask)0, 0, ShareAccess.FILE_SHARE_READ | ShareAccess.FILE_SHARE_WRITE, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, securityContext);
  87. if (openStatus != NTStatus.STATUS_SUCCESS)
  88. {
  89. return openStatus;
  90. }
  91. fileStore.CloseFile(handle);
  92. return NTStatus.STATUS_SUCCESS;
  93. }
  94. public static NTStatus QueryInformation(out FileNetworkOpenInformation fileInfo, INTFileStore fileStore, string path, SecurityContext securityContext)
  95. {
  96. object handle;
  97. FileStatus fileStatus;
  98. NTStatus openStatus = fileStore.CreateFile(out handle, out fileStatus, path, FileAccessMask.FILE_READ_ATTRIBUTES, 0, ShareAccess.FILE_SHARE_READ | ShareAccess.FILE_SHARE_WRITE, CreateDisposition.FILE_OPEN, 0, securityContext);
  99. if (openStatus != NTStatus.STATUS_SUCCESS)
  100. {
  101. fileInfo = null;
  102. return openStatus;
  103. }
  104. fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(fileStore, handle);
  105. fileStore.CloseFile(handle);
  106. return NTStatus.STATUS_SUCCESS;
  107. }
  108. public static NTStatus SetInformation(INTFileStore fileStore, string path, SMBFileAttributes fileAttributes, DateTime? lastWriteTime, SecurityContext securityContext)
  109. {
  110. object handle;
  111. FileStatus fileStatus;
  112. NTStatus status = fileStore.CreateFile(out handle, out fileStatus, path, FileAccessMask.FILE_WRITE_ATTRIBUTES, (FileAttributes)0, ShareAccess.FILE_SHARE_READ | ShareAccess.FILE_SHARE_WRITE, CreateDisposition.FILE_OPEN, 0, securityContext);
  113. if (status != NTStatus.STATUS_SUCCESS)
  114. {
  115. return status;
  116. }
  117. FileBasicInformation basicInfo = new FileBasicInformation();
  118. basicInfo.LastWriteTime = lastWriteTime;
  119. if ((fileAttributes & SMBFileAttributes.Hidden) > 0)
  120. {
  121. basicInfo.FileAttributes |= FileAttributes.Hidden;
  122. }
  123. if ((fileAttributes & SMBFileAttributes.ReadOnly) > 0)
  124. {
  125. basicInfo.FileAttributes |= FileAttributes.ReadOnly;
  126. }
  127. if ((fileAttributes & SMBFileAttributes.Archive) > 0)
  128. {
  129. basicInfo.FileAttributes |= FileAttributes.Archive;
  130. }
  131. status = fileStore.SetFileInformation(handle, basicInfo);
  132. fileStore.CloseFile(handle);
  133. return status;
  134. }
  135. public static NTStatus SetInformation2(INTFileStore fileStore, object handle, DateTime? creationTime, DateTime? lastAccessTime, DateTime? lastWriteTime)
  136. {
  137. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(fileStore, handle);
  138. FileBasicInformation basicInfo = new FileBasicInformation();
  139. basicInfo.FileAttributes = fileInfo.FileAttributes;
  140. basicInfo.CreationTime = creationTime;
  141. basicInfo.LastAccessTime = lastAccessTime;
  142. basicInfo.LastWriteTime = lastWriteTime;
  143. return fileStore.SetFileInformation(handle, basicInfo);
  144. }
  145. public static SMBFileAttributes GetFileAttributes(FileAttributes attributes)
  146. {
  147. SMBFileAttributes result = SMBFileAttributes.Normal;
  148. if ((attributes & FileAttributes.Hidden) > 0)
  149. {
  150. result |= SMBFileAttributes.Hidden;
  151. }
  152. if ((attributes & FileAttributes.ReadOnly) > 0)
  153. {
  154. result |= SMBFileAttributes.ReadOnly;
  155. }
  156. if ((attributes & FileAttributes.Archive) > 0)
  157. {
  158. result |= SMBFileAttributes.Archive;
  159. }
  160. if ((attributes & FileAttributes.Directory) > 0)
  161. {
  162. result |= SMBFileAttributes.Directory;
  163. }
  164. return result;
  165. }
  166. }
  167. }