Source code of Windows XP (NT5)
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.

198 lines
4.2 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. rtsave.c
  5. Abstract:
  6. NT level registry api test program, basic non-error paths.
  7. Perform an NtSaveKey call to dump part of the registry to a file.
  8. rtsave <KeyPath> <FileName>
  9. Example:
  10. rtsave \registry\machine\user userfile.rd
  11. Author:
  12. Bryan Willman (bryanwi) 22-Jan-92
  13. Revision History:
  14. --*/
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include "cmp.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #define WORK_SIZE 1024
  23. void __cdecl main(int, char *[]);
  24. void processargs();
  25. UNICODE_STRING KeyPath;
  26. WCHAR KeyPathBuffer[WORK_SIZE];
  27. UNICODE_STRING FileName;
  28. WCHAR FileNameBuffer[WORK_SIZE];
  29. void
  30. __cdecl main(
  31. int argc,
  32. char *argv[]
  33. )
  34. {
  35. NTSTATUS status;
  36. OBJECT_ATTRIBUTES ObjectAttributes;
  37. IO_STATUS_BLOCK IoStatus;
  38. HANDLE FileHandle;
  39. HANDLE KeyHandle;
  40. BOOLEAN WasEnabled;
  41. //
  42. // Process args
  43. //
  44. KeyPath.MaximumLength = WORK_SIZE;
  45. KeyPath.Length = 0L;
  46. KeyPath.Buffer = &(KeyPathBuffer[0]);
  47. FileName.MaximumLength = WORK_SIZE;
  48. FileName.Length = 0L;
  49. FileName.Buffer = &(FileNameBuffer[0]);
  50. processargs(argc, argv);
  51. //
  52. // Set up and open FileName
  53. //
  54. printf("rtsave: starting\n");
  55. printf("rtsave: saving hive rooted at\n\t'%ws'\nto file\n\t'%ws'\n",
  56. KeyPath.Buffer, FileName.Buffer);
  57. InitializeObjectAttributes(
  58. &ObjectAttributes,
  59. &FileName,
  60. 0,
  61. (HANDLE)NULL,
  62. NULL
  63. );
  64. ObjectAttributes.Attributes |= OBJ_CASE_INSENSITIVE;
  65. status = NtCreateFile(
  66. &FileHandle,
  67. GENERIC_WRITE | SYNCHRONIZE,
  68. &ObjectAttributes,
  69. &IoStatus,
  70. NULL, // AllocationSize
  71. FILE_ATTRIBUTE_NORMAL,
  72. FILE_SHARE_READ, // ShareAccess
  73. FILE_CREATE,
  74. FILE_SYNCHRONOUS_IO_NONALERT,
  75. NULL, // EaBuffer
  76. 0 // EaLength
  77. );
  78. if (!NT_SUCCESS(status)) {
  79. if (status == STATUS_OBJECT_NAME_COLLISION) {
  80. printf("rtsave: file '%ws' already exists!\n",
  81. FileName.Buffer);
  82. exit(1);
  83. }
  84. printf("rtsave: file open failed status = %08lx\n", status);
  85. exit(1);
  86. }
  87. InitializeObjectAttributes(
  88. &ObjectAttributes,
  89. &KeyPath,
  90. 0,
  91. (HANDLE)NULL,
  92. NULL
  93. );
  94. ObjectAttributes.Attributes |= OBJ_CASE_INSENSITIVE;
  95. status = NtOpenKey(
  96. &KeyHandle,
  97. MAXIMUM_ALLOWED,
  98. &ObjectAttributes
  99. );
  100. if (!NT_SUCCESS(status)) {
  101. printf("rtsave: key open failed status = %08lx\n", status);
  102. exit(1);
  103. }
  104. RtlAdjustPrivilege(SE_BACKUP_PRIVILEGE, TRUE, FALSE, &WasEnabled);
  105. status = NtSaveKey(KeyHandle, FileHandle);
  106. RtlAdjustPrivilege(SE_BACKUP_PRIVILEGE, WasEnabled, FALSE, &WasEnabled);
  107. if (!NT_SUCCESS(status)) {
  108. printf("rtsave: NtSaveKey failed status = %08lx\n", status);
  109. exit(1);
  110. }
  111. printf("rtsave: success\n");
  112. exit(0);
  113. }
  114. void
  115. processargs(
  116. int argc,
  117. char *argv[]
  118. )
  119. {
  120. ANSI_STRING temp;
  121. UNICODE_STRING DosFileName;
  122. if ( (argc != 3) )
  123. {
  124. printf("Usage: %s <KeyName> <FileName>\nWhere <FileName> does NOT already exist\n",
  125. argv[0]);
  126. printf("Example: %s \\registry\\machine\\security d:\\backups\\security\n",
  127. argv[0]);
  128. exit(1);
  129. }
  130. RtlInitAnsiString(
  131. &temp,
  132. argv[1]
  133. );
  134. RtlAnsiStringToUnicodeString(
  135. &KeyPath,
  136. &temp,
  137. TRUE
  138. );
  139. RtlInitAnsiString(
  140. &temp,
  141. argv[2]
  142. );
  143. RtlAnsiStringToUnicodeString(
  144. &DosFileName,
  145. &temp,
  146. TRUE
  147. );
  148. RtlDosPathNameToNtPathName_U( DosFileName.Buffer,
  149. &FileName,
  150. NULL,
  151. NULL );
  152. return;
  153. }