Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
6.8 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h> // exit
  3. #include <io.h> // _get_osfhandle
  4. #include <nt.h>
  5. #include <ntrtl.h>
  6. #include <nturtl.h>
  7. #include <ntioapi.h>
  8. #include <windef.h>
  9. #include <winbase.h>
  10. VOID
  11. PrintError(
  12. DWORD Error
  13. )
  14. {
  15. LPVOID lpMsgBuf = NULL;
  16. FormatMessage(
  17. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  18. FORMAT_MESSAGE_FROM_SYSTEM |
  19. FORMAT_MESSAGE_IGNORE_INSERTS,
  20. NULL,
  21. Error,
  22. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  23. (LPTSTR) &lpMsgBuf,
  24. 0,
  25. NULL
  26. );
  27. printf("%ws\n",lpMsgBuf);
  28. LocalFree( lpMsgBuf );
  29. }
  30. VOID
  31. Usage(
  32. PCHAR AppName,
  33. DWORD ExitStatus
  34. )
  35. /*++
  36. Routine Description:
  37. Print usage and exit
  38. Arguments:
  39. ExitStatus - exits with this status
  40. Return Value:
  41. Exit(ExitStatus)
  42. --*/
  43. {
  44. printf("%s create <filename> <reparse tag value in hex> <reparse point value in quotes>\n",
  45. AppName);
  46. printf("\n");
  47. printf("%s read <filename>\n", AppName);
  48. printf("\n");
  49. printf("%s delete <filename>\n", AppName);
  50. printf("\n");
  51. exit(ExitStatus);
  52. }
  53. VOID
  54. DeleteReparsePoint(
  55. IN DWORD argc,
  56. IN PCHAR *argv
  57. )
  58. {
  59. HANDLE File = INVALID_HANDLE_VALUE;
  60. PREPARSE_GUID_DATA_BUFFER ReparsePointData = NULL;
  61. DWORD BytesReturned = 0;
  62. CHAR *Data = NULL;
  63. REPARSE_GUID_DATA_BUFFER Header;
  64. DWORD WStatus = ERROR_SUCCESS;
  65. ReparsePointData = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
  66. if(!ReparsePointData) {
  67. printf("Out of memory.\n");
  68. exit(1);
  69. }
  70. File = CreateFileA(argv[2],
  71. MAXIMUM_ALLOWED,
  72. 0,
  73. NULL,
  74. OPEN_EXISTING,
  75. FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS ,
  76. NULL
  77. );
  78. if(File == INVALID_HANDLE_VALUE) {
  79. WStatus = GetLastError();
  80. printf("Error opening file %s: %d\n", argv[2], WStatus);
  81. PrintError(WStatus);
  82. free(ReparsePointData);
  83. exit(1);
  84. }
  85. if(!DeviceIoControl(File,
  86. FSCTL_GET_REPARSE_POINT,
  87. NULL,
  88. 0,
  89. ReparsePointData,
  90. MAXIMUM_REPARSE_DATA_BUFFER_SIZE,
  91. &BytesReturned,
  92. NULL
  93. )) {
  94. WStatus = GetLastError();
  95. printf("Error reading reparse point data: %d\n", WStatus);
  96. PrintError(WStatus);
  97. CloseHandle(File);
  98. free(ReparsePointData);
  99. exit(1);
  100. }
  101. Header.ReparseGuid = ReparsePointData->ReparseGuid;
  102. Header.ReparseTag = ReparsePointData->ReparseTag;
  103. Header.ReparseDataLength = 0;
  104. if(!DeviceIoControl(File,
  105. FSCTL_DELETE_REPARSE_POINT,
  106. &Header,
  107. REPARSE_GUID_DATA_BUFFER_HEADER_SIZE,
  108. NULL,
  109. 0,
  110. &BytesReturned,
  111. NULL
  112. )) {
  113. WStatus = GetLastError();
  114. printf("Error deleting reparse point data: %d\n", WStatus);
  115. PrintError(WStatus);
  116. CloseHandle(File);
  117. free(ReparsePointData);
  118. exit(1);
  119. }
  120. CloseHandle(File);
  121. /*
  122. if(!DeleteFileA(argv[2])){
  123. WStatus = GetLastError();
  124. printf("Error deleting file %s: %d\n", argv[2], WStatus);
  125. PrintError(WStatus);
  126. CloseHandle(File);
  127. free(ReparsePointData);
  128. exit(1);
  129. }
  130. */
  131. free(ReparsePointData);
  132. }
  133. VOID
  134. ReadReparsePoint(
  135. IN DWORD argc,
  136. IN PCHAR *argv
  137. )
  138. {
  139. HANDLE File = INVALID_HANDLE_VALUE;
  140. PREPARSE_GUID_DATA_BUFFER ReparsePointData = NULL;
  141. DWORD BytesReturned = 0;
  142. CHAR *Data = NULL;
  143. DWORD WStatus = ERROR_SUCCESS;
  144. ReparsePointData = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
  145. if(!ReparsePointData) {
  146. printf("Out of memory.\n");
  147. exit(1);
  148. }
  149. File = CreateFileA(argv[2],
  150. MAXIMUM_ALLOWED,
  151. 0,
  152. NULL,
  153. OPEN_EXISTING,
  154. FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS ,
  155. NULL
  156. );
  157. if(File == INVALID_HANDLE_VALUE) {
  158. WStatus = GetLastError();
  159. printf("Error opening file %s: %d\n", argv[2], WStatus);
  160. PrintError(WStatus);
  161. exit(1);
  162. }
  163. if(!DeviceIoControl(File,
  164. FSCTL_GET_REPARSE_POINT,
  165. NULL,
  166. 0,
  167. ReparsePointData,
  168. MAXIMUM_REPARSE_DATA_BUFFER_SIZE,
  169. &BytesReturned,
  170. NULL
  171. )) {
  172. WStatus = GetLastError();
  173. printf("Error reading reparse point data: %d\n", WStatus);
  174. PrintError(WStatus);
  175. CloseHandle(File);
  176. exit(1);
  177. }
  178. CloseHandle(File);
  179. printf("Reparse tag = 0x%08x\n", ReparsePointData->ReparseTag);
  180. Data = (CHAR *)&(ReparsePointData->GenericReparseBuffer.DataBuffer);
  181. Data[ReparsePointData->ReparseDataLength] = '\0';
  182. printf("Reparse data = \"%s\"\n", Data);
  183. printf("\n");
  184. free(ReparsePointData);
  185. }
  186. VOID
  187. CreateReparsePoint(
  188. IN DWORD argc,
  189. IN PCHAR *argv
  190. )
  191. {
  192. ULONG TagValue = 0;
  193. HANDLE File = INVALID_HANDLE_VALUE;
  194. PREPARSE_GUID_DATA_BUFFER ReparsePointData = NULL;
  195. DWORD BytesReturned = 0;
  196. USHORT DataSize = strlen(argv[4]) + sizeof(char);
  197. DWORD WStatus = ERROR_SUCCESS;
  198. sscanf(argv[3],"%x", &TagValue);
  199. ReparsePointData = malloc(REPARSE_GUID_DATA_BUFFER_HEADER_SIZE + DataSize);
  200. if(!ReparsePointData) {
  201. printf("Out of memory.\n");
  202. exit(1);
  203. }
  204. ReparsePointData->ReparseTag = TagValue;
  205. ReparsePointData->ReparseDataLength = DataSize;
  206. memcpy(&(ReparsePointData->GenericReparseBuffer.DataBuffer), argv[4], DataSize );
  207. File = CreateFileA(argv[2],
  208. MAXIMUM_ALLOWED,
  209. 0,
  210. NULL,
  211. OPEN_ALWAYS,
  212. FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS ,
  213. NULL
  214. );
  215. if(File == INVALID_HANDLE_VALUE) {
  216. WStatus = GetLastError();
  217. printf("Error opening file %s: %d\n", argv[2], WStatus);
  218. PrintError(WStatus);
  219. free(ReparsePointData);
  220. exit(1);
  221. }
  222. if(!DeviceIoControl(File,
  223. FSCTL_SET_REPARSE_POINT,
  224. ReparsePointData,
  225. REPARSE_GUID_DATA_BUFFER_HEADER_SIZE + DataSize,
  226. NULL,
  227. 0,
  228. &BytesReturned,
  229. NULL
  230. )) {
  231. WStatus = GetLastError();
  232. printf("Error setting reparse point data: %d\n", WStatus);
  233. PrintError(WStatus);
  234. CloseHandle(File);
  235. free(ReparsePointData);
  236. exit(1);
  237. }
  238. CloseHandle(File);
  239. free(ReparsePointData);
  240. }
  241. VOID _cdecl
  242. main(
  243. IN DWORD argc,
  244. IN PCHAR *argv
  245. )
  246. /*++
  247. Routine Description:
  248. Process the command line.
  249. Arguments:
  250. argc
  251. argv
  252. Return Value:
  253. Exits with 0 if everything went okay. Otherwise, 1.
  254. --*/
  255. {
  256. //
  257. // Print usage and exit
  258. //
  259. if (argc <= 2 ) {
  260. Usage(argv[0], 0);
  261. }
  262. //
  263. // Find the subcommand
  264. //
  265. if (!strcmp(argv[1], "create") && (argc == 5)) {
  266. CreateReparsePoint(argc, argv);
  267. } else if (!strcmp(argv[1], "read") && (argc == 3)) {
  268. ReadReparsePoint(argc, argv);
  269. } else if (!strcmp(argv[1], "delete") && (argc == 3)) {
  270. DeleteReparsePoint(argc, argv);
  271. } else if (!strcmp(argv[1], "/?")) {
  272. Usage(argv[0], 0);
  273. } else {
  274. fprintf(stderr, "Invalid usage.\n");
  275. Usage(argv[0], 0);
  276. }
  277. exit(0);
  278. }