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.

590 lines
13 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. objectid.c
  5. Abstract:
  6. This file contains code for commands that affect object ids.
  7. Author:
  8. Wesley Witt [wesw] 1-March-2000
  9. Revision History:
  10. --*/
  11. #include <precomp.h>
  12. INT
  13. ObjectIdHelp(
  14. IN INT argc,
  15. IN PWSTR argv[]
  16. )
  17. {
  18. DisplayMsg( MSG_USAGE_OBJECTID );
  19. return EXIT_CODE_SUCCESS;
  20. }
  21. VOID
  22. PrintObjectId(
  23. PFILE_OBJECTID_BUFFER lpOutBuffer
  24. )
  25. {
  26. INT Index;
  27. #define CHARLENGTH 16
  28. // Remember two characters in each byte dump
  29. WCHAR Buffer[2 * CHARLENGTH + 1];
  30. int BufferOffset;
  31. DisplayMsg( MSG_OBJECTID_TEXT );
  32. BufferOffset = 0;
  33. for ( Index = 0 ; Index < CHARLENGTH ; Index++ ) {
  34. BufferOffset += swprintf( &Buffer[BufferOffset],
  35. L"%02x",
  36. lpOutBuffer->ObjectId[Index] );
  37. }
  38. OutputMessage( Buffer );
  39. OutputMessage( L"\r\n" );
  40. DisplayMsg( MSG_BIRTHVOLID_TEXT );
  41. BufferOffset = 0;
  42. for ( Index = 0 ; Index < CHARLENGTH ; Index++ ) {
  43. BufferOffset += swprintf( &Buffer[BufferOffset],
  44. L"%02x",
  45. lpOutBuffer->BirthVolumeId[Index] );
  46. }
  47. OutputMessage( Buffer );
  48. OutputMessage( L"\r\n" );
  49. DisplayMsg( MSG_BIRTHOBJECTID_TEXT );
  50. BufferOffset = 0;
  51. for ( Index = 0 ; Index < CHARLENGTH ; Index++ ) {
  52. BufferOffset += swprintf( &Buffer[BufferOffset],
  53. L"%02x",
  54. lpOutBuffer->BirthObjectId[Index] );
  55. }
  56. OutputMessage( Buffer );
  57. OutputMessage( L"\r\n" );
  58. DisplayMsg( MSG_DOMAINID_TEXT );
  59. BufferOffset = 0;
  60. for ( Index = 0 ; Index < CHARLENGTH ; Index++ ) {
  61. BufferOffset += swprintf( &Buffer[BufferOffset],
  62. L"%02x",
  63. lpOutBuffer->DomainId[Index] );
  64. }
  65. OutputMessage( Buffer );
  66. OutputMessage( L"\r\n" );
  67. }
  68. UCHAR
  69. GetNibbleValue(
  70. IN OUT PCWSTR *String
  71. )
  72. {
  73. UCHAR v;
  74. WCHAR c;
  75. c = *(*String)++;
  76. c = (UCHAR) toupper( c );
  77. if (isdigit( c )) {
  78. v = c - L'0';
  79. } else if (isalpha( c ) && c <= L'F') {
  80. v = c - L'A' + 10;
  81. } else {
  82. (*String)--;
  83. return 0;
  84. }
  85. return v;
  86. }
  87. UCHAR
  88. GetByteValue(
  89. IN OUT PCWSTR *String
  90. )
  91. {
  92. UCHAR v = GetNibbleValue( String );
  93. v *= 16;
  94. v += GetNibbleValue( String );
  95. return v;
  96. }
  97. BOOL
  98. ConvertStringToHexData(
  99. IN PCWSTR Arg,
  100. OUT PUCHAR Buffer
  101. )
  102. {
  103. int i;
  104. for (i = 0; i < 16; i++) {
  105. Buffer[i] = GetByteValue( &Arg );
  106. }
  107. return *Arg == L'\0';
  108. }
  109. INT
  110. SetObjectId(
  111. IN INT argc,
  112. IN PWSTR argv[]
  113. )
  114. /*++
  115. Routine Description:
  116. This routine sets the objectID for the file specified.
  117. Arguments:
  118. argc - The argument count.
  119. argv - Array of Strings of the form :
  120. ' fscutl setoid <oid=val> <bvid=val> <boid=val> <did=val> <pathname>'.
  121. Return Value:
  122. None
  123. --*/
  124. {
  125. PWSTR Filename = NULL;
  126. HANDLE FileHandle = INVALID_HANDLE_VALUE;
  127. PFILE_OBJECTID_BUFFER lpInBuffer = NULL;
  128. BOOL Status;
  129. DWORD nInBufferSize;
  130. DWORD BytesReturned;
  131. INT ExitCode = EXIT_CODE_SUCCESS;
  132. try {
  133. if (argc != 5) {
  134. DisplayMsg( MSG_USAGE_SETOBJECTID );
  135. if (argc != 0) {
  136. ExitCode = EXIT_CODE_FAILURE;
  137. }
  138. leave;
  139. }
  140. Filename = GetFullPath( argv[4] );
  141. if (!Filename) {
  142. DisplayError();
  143. ExitCode = EXIT_CODE_FAILURE;
  144. leave;
  145. }
  146. if (!IsVolumeLocalNTFS( Filename[0] )) {
  147. DisplayMsg( MSG_NTFS_REQUIRED );
  148. ExitCode = EXIT_CODE_FAILURE;
  149. leave;
  150. }
  151. nInBufferSize = sizeof(FILE_OBJECTID_BUFFER);
  152. lpInBuffer = (PFILE_OBJECTID_BUFFER) malloc ( nInBufferSize );
  153. if (lpInBuffer == NULL) {
  154. DisplayErrorMsg( ERROR_NOT_ENOUGH_MEMORY );
  155. ExitCode = EXIT_CODE_FAILURE;
  156. leave;
  157. }
  158. //
  159. // Convert the input strings into the correct data
  160. //
  161. if (!ConvertStringToHexData( argv[0], lpInBuffer->ObjectId)
  162. || !ConvertStringToHexData( argv[1], lpInBuffer->BirthVolumeId)
  163. || !ConvertStringToHexData( argv[2], lpInBuffer->BirthObjectId)
  164. || !ConvertStringToHexData( argv[3], lpInBuffer->DomainId)) {
  165. DisplayMsg( MSG_USAGE_SETOBJECTID );
  166. ExitCode = EXIT_CODE_FAILURE;
  167. leave;
  168. }
  169. if (!EnablePrivilege( SE_RESTORE_NAME )) {
  170. DisplayError( );
  171. ExitCode = EXIT_CODE_FAILURE;
  172. leave;
  173. }
  174. FileHandle = CreateFile(
  175. Filename,
  176. GENERIC_WRITE,
  177. FILE_SHARE_READ | FILE_SHARE_WRITE,
  178. NULL,
  179. OPEN_EXISTING,
  180. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
  181. NULL
  182. );
  183. if (FileHandle == INVALID_HANDLE_VALUE) {
  184. DisplayError();
  185. ExitCode = EXIT_CODE_FAILURE;
  186. leave;
  187. }
  188. Status = DeviceIoControl(
  189. FileHandle,
  190. FSCTL_SET_OBJECT_ID,
  191. (LPVOID) lpInBuffer,
  192. nInBufferSize,
  193. NULL,
  194. 0,
  195. &BytesReturned,
  196. (LPOVERLAPPED)NULL
  197. );
  198. if (!Status) {
  199. DisplayError();
  200. ExitCode = EXIT_CODE_FAILURE;
  201. leave;
  202. }
  203. } finally {
  204. if (FileHandle != INVALID_HANDLE_VALUE) {
  205. CloseHandle( FileHandle );
  206. }
  207. free(lpInBuffer);
  208. free( Filename );
  209. }
  210. return ExitCode;
  211. }
  212. INT
  213. GetObjectId(
  214. IN INT argc,
  215. IN PWSTR argv[]
  216. )
  217. /*++
  218. Routine Description:
  219. This routine gets the objectID associated with the file specified.
  220. Arguments:
  221. argc - The argument count.
  222. argv - Array of Strings of the form :
  223. ' fscutl getoid <pathname> '.
  224. Return Value:
  225. None
  226. --*/
  227. {
  228. HANDLE FileHandle = INVALID_HANDLE_VALUE;
  229. PWSTR Filename = NULL;
  230. PFILE_OBJECTID_BUFFER lpOutBuffer = NULL;
  231. BOOL Status;
  232. DWORD nOutBufferSize;
  233. DWORD BytesReturned;
  234. INT ExitCode = EXIT_CODE_SUCCESS;
  235. try {
  236. if (argc != 1) {
  237. DisplayMsg( MSG_USAGE_GETOBJECTID );
  238. if (argc != 0) {
  239. ExitCode = EXIT_CODE_FAILURE;
  240. }
  241. leave;
  242. }
  243. Filename = GetFullPath( argv[0] );
  244. if (!Filename) {
  245. DisplayError();
  246. ExitCode = EXIT_CODE_FAILURE;
  247. leave;
  248. }
  249. if (!IsVolumeNTFS( Filename )) {
  250. DisplayMsg( MSG_NTFS_REQUIRED );
  251. ExitCode = EXIT_CODE_FAILURE;
  252. leave;
  253. }
  254. nOutBufferSize = sizeof(FILE_OBJECTID_BUFFER);
  255. lpOutBuffer = (PFILE_OBJECTID_BUFFER) malloc ( nOutBufferSize );
  256. if (lpOutBuffer == NULL) {
  257. DisplayErrorMsg( ERROR_NOT_ENOUGH_MEMORY );
  258. ExitCode = EXIT_CODE_FAILURE;
  259. leave;
  260. }
  261. FileHandle = CreateFile(
  262. Filename,
  263. GENERIC_READ,
  264. FILE_SHARE_READ | FILE_SHARE_WRITE,
  265. NULL,
  266. OPEN_EXISTING,
  267. FILE_ATTRIBUTE_NORMAL,
  268. NULL
  269. );
  270. if (FileHandle == INVALID_HANDLE_VALUE) {
  271. DisplayError();
  272. ExitCode = EXIT_CODE_FAILURE;
  273. leave;
  274. }
  275. Status = DeviceIoControl(
  276. FileHandle,
  277. FSCTL_GET_OBJECT_ID,
  278. NULL,
  279. 0,
  280. (LPVOID) lpOutBuffer,
  281. nOutBufferSize,
  282. &BytesReturned,
  283. (LPOVERLAPPED)NULL
  284. );
  285. if (!Status) {
  286. if (GetLastError( ) == ERROR_FILE_NOT_FOUND) {
  287. DisplayMsg( MSG_NO_OBJECT_ID );
  288. } else {
  289. DisplayError();
  290. ExitCode = EXIT_CODE_FAILURE;
  291. }
  292. leave;
  293. }
  294. PrintObjectId( lpOutBuffer );
  295. } finally {
  296. if (FileHandle != INVALID_HANDLE_VALUE) {
  297. CloseHandle( FileHandle );
  298. }
  299. free(lpOutBuffer);
  300. free( Filename );
  301. }
  302. return ExitCode;
  303. }
  304. INT
  305. CreateOrGetObjectId(
  306. IN INT argc,
  307. IN PWSTR argv[]
  308. )
  309. /*++
  310. Routine Description:
  311. This routine gets the objectID for the file, if it exists, else
  312. creates an objectID and returns it.
  313. Arguments:
  314. argc - The argument count.
  315. argv - Array of Strings of the form :
  316. ' fscutl crgetoid <pathname>'.
  317. Return Value:
  318. None
  319. --*/
  320. {
  321. PWSTR Filename = NULL;
  322. HANDLE FileHandle = INVALID_HANDLE_VALUE;
  323. PFILE_OBJECTID_BUFFER lpOutBuffer = NULL;
  324. BOOL Status;
  325. DWORD nOutBufferSize;
  326. DWORD BytesReturned;
  327. INT ExitCode = EXIT_CODE_SUCCESS;
  328. try {
  329. if (argc != 1) {
  330. DisplayMsg( MSG_USAGE_CREATEOBJECTID );
  331. if (argc != 0) {
  332. ExitCode = EXIT_CODE_FAILURE;
  333. }
  334. leave;
  335. }
  336. Filename = GetFullPath( argv[0] );
  337. if (!Filename) {
  338. DisplayError();
  339. ExitCode = EXIT_CODE_FAILURE;
  340. leave;
  341. }
  342. if (!IsVolumeLocalNTFS( Filename[0] )) {
  343. DisplayMsg( MSG_NTFS_REQUIRED );
  344. ExitCode = EXIT_CODE_FAILURE;
  345. leave;
  346. }
  347. nOutBufferSize = sizeof(FILE_OBJECTID_BUFFER);
  348. lpOutBuffer = (PFILE_OBJECTID_BUFFER) malloc ( nOutBufferSize );
  349. if (lpOutBuffer == NULL) {
  350. DisplayErrorMsg( ERROR_NOT_ENOUGH_MEMORY );
  351. ExitCode = EXIT_CODE_FAILURE;
  352. leave;
  353. }
  354. FileHandle = CreateFile(
  355. Filename,
  356. GENERIC_READ | GENERIC_WRITE,
  357. FILE_SHARE_READ | FILE_SHARE_WRITE,
  358. NULL,
  359. OPEN_EXISTING,
  360. FILE_ATTRIBUTE_NORMAL,
  361. NULL
  362. );
  363. if (FileHandle == INVALID_HANDLE_VALUE) {
  364. DisplayError();
  365. ExitCode = EXIT_CODE_FAILURE;
  366. leave;
  367. }
  368. Status = DeviceIoControl(
  369. FileHandle,
  370. FSCTL_CREATE_OR_GET_OBJECT_ID,
  371. NULL,
  372. 0,
  373. (LPVOID) lpOutBuffer,
  374. nOutBufferSize,
  375. &BytesReturned,
  376. (LPOVERLAPPED)NULL
  377. );
  378. if (!Status) {
  379. DisplayError();
  380. ExitCode = EXIT_CODE_FAILURE;
  381. leave;
  382. }
  383. PrintObjectId( lpOutBuffer );
  384. } finally {
  385. if (FileHandle != INVALID_HANDLE_VALUE) {
  386. CloseHandle( FileHandle );
  387. }
  388. if (lpOutBuffer) {
  389. free(lpOutBuffer);
  390. }
  391. if (Filename) {
  392. free( Filename );
  393. }
  394. }
  395. return ExitCode;
  396. }
  397. INT
  398. DeleteObjectId(
  399. IN INT argc,
  400. IN PWSTR argv[]
  401. )
  402. /*++
  403. Routine Description:
  404. This routine deletes the objectID associated with the file
  405. specified.
  406. Arguments:
  407. argc - The argument count.
  408. argv - Array of Strings of the form :
  409. ' fscutl deloid <pathname>'.
  410. Return Value:
  411. None
  412. --*/
  413. {
  414. HANDLE FileHandle = INVALID_HANDLE_VALUE;
  415. PWSTR Filename = NULL;
  416. BOOL Status;
  417. DWORD BytesReturned;
  418. INT ExitCode = EXIT_CODE_SUCCESS;
  419. try {
  420. if (argc != 1) {
  421. DisplayMsg( MSG_USAGE_DELETEOBJECTID );
  422. if (argc != 0) {
  423. ExitCode = EXIT_CODE_FAILURE;}
  424. leave;
  425. }
  426. Filename = GetFullPath( argv[0] );
  427. if (!Filename) {
  428. DisplayError();
  429. if (argc != 0) {
  430. ExitCode = EXIT_CODE_FAILURE;
  431. }
  432. leave;
  433. }
  434. if (!IsVolumeLocalNTFS( Filename[0] )) {
  435. DisplayMsg( MSG_NTFS_REQUIRED );
  436. ExitCode = EXIT_CODE_FAILURE;
  437. leave;
  438. }
  439. FileHandle = CreateFile(
  440. Filename,
  441. GENERIC_WRITE,
  442. FILE_SHARE_READ | FILE_SHARE_WRITE,
  443. NULL,
  444. OPEN_EXISTING,
  445. FILE_ATTRIBUTE_NORMAL,
  446. NULL
  447. );
  448. if (FileHandle == INVALID_HANDLE_VALUE) {
  449. DisplayError();
  450. ExitCode = EXIT_CODE_FAILURE;
  451. leave;
  452. }
  453. Status = DeviceIoControl(
  454. FileHandle,
  455. FSCTL_DELETE_OBJECT_ID,
  456. NULL,
  457. 0,
  458. NULL,
  459. 0,
  460. &BytesReturned,
  461. (LPOVERLAPPED)NULL
  462. );
  463. if (!Status) {
  464. DisplayError();
  465. ExitCode = EXIT_CODE_FAILURE;
  466. leave;
  467. }
  468. } finally {
  469. if (FileHandle != INVALID_HANDLE_VALUE) {
  470. CloseHandle( FileHandle );
  471. }
  472. free( Filename );
  473. }
  474. return ExitCode;
  475. }