SMB1FileStoreHelper.cs 7.9 KB

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