00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
#include "cmp.h"
00032
#include <stdio.h>
00033
#include <stdlib.h>
00034
#include <string.h>
00035
00036 #define WORK_SIZE 1024
00037
00038
void __cdecl
main(
int argc,
char *);
00039
void processargs();
00040
00041
void
00042
Copy(
00043 HANDLE Source,
00044 HANDLE Dest
00045 );
00046
00047 UNICODE_STRING
SourceName;
00048 UNICODE_STRING
DestName;
00049 BOOLEAN
CopySecurity;
00050
00051
void
00052 __cdecl
main(
00053
int argc,
00054
char *argv[]
00055 )
00056 {
00057
NTSTATUS Status;
00058 OBJECT_ATTRIBUTES
ObjectAttributes;
00059 HANDLE SourceHandle;
00060 HANDLE DestHandle;
00061
00062
00063
00064
00065
processargs(argc, argv);
00066
00067
00068
00069
00070
00071
00072 printf(
"rttrecpy: starting\n");
00073
00074 InitializeObjectAttributes(
00075 &
ObjectAttributes,
00076 &
SourceName,
00077 OBJ_CASE_INSENSITIVE,
00078 (HANDLE)
NULL,
00079
NULL
00080 );
00081
00082
Status =
NtOpenKey(
00083 &SourceHandle,
00084 KEY_READ,
00085 &
ObjectAttributes
00086 );
00087
if (!
NT_SUCCESS(
Status)) {
00088 printf(
"rttrecpy: NtOpenKey %wS failed %08lx\n", &
SourceName,
Status);
00089
exit(1);
00090 }
00091
00092 InitializeObjectAttributes(&
ObjectAttributes,
00093 &
DestName,
00094 OBJ_CASE_INSENSITIVE,
00095
NULL,
00096
NULL);
00097
Status =
NtCreateKey(&DestHandle,
00098 KEY_WRITE,
00099 &
ObjectAttributes,
00100 0,
00101
NULL,
00102 0,
00103
NULL);
00104
if (!
NT_SUCCESS(
Status)) {
00105 printf(
"rttrecpy: NtCreateKey %wS failed %08lx\n",
DestName,
Status);
00106
exit(1);
00107 }
00108
00109
Copy(SourceHandle, DestHandle);
00110 }
00111
00112
00113
void
00114 Copy(
00115 HANDLE Source,
00116 HANDLE Dest
00117 )
00118 {
00119
NTSTATUS Status;
00120 PKEY_BASIC_INFORMATION KeyInformation;
00121 PKEY_VALUE_FULL_INFORMATION KeyValue;
00122 OBJECT_ATTRIBUTES
ObjectAttributes;
00123 ULONG NamePos;
00124 ULONG index;
00125 STRING enumname;
00126 HANDLE SourceChild;
00127 HANDLE DestChild;
00128 ULONG ResultLength;
00129
static char buffer[
WORK_SIZE];
00130
static char SecurityBuffer[
WORK_SIZE];
00131 PSECURITY_DESCRIPTOR SecurityDescriptor;
00132 UNICODE_STRING
ValueName;
00133 UNICODE_STRING
KeyName;
00134
00135
00136
00137
00138
00139 KeyValue = (PKEY_VALUE_FULL_INFORMATION)buffer;
00140
for (index = 0;
TRUE; index++) {
00141
Status =
NtEnumerateValueKey(Source,
00142 index,
00143 KeyValueFullInformation,
00144 buffer,
00145
WORK_SIZE,
00146 &ResultLength);
00147
00148
if (!
NT_SUCCESS(
Status)) {
00149
if (
Status == STATUS_NO_MORE_ENTRIES) {
00150
00151
00152
00153
00154
break;
00155 }
else {
00156 printf(
"rttrecpy: NtEnumerateValueKey failed %08lx\n",
Status);
00157
break;
00158 }
00159 }
00160
00161
ValueName.Buffer = KeyValue->Name;
00162
ValueName.Length = KeyValue->NameLength;
00163
00164
Status =
NtSetValueKey(Dest,
00165 &
ValueName,
00166 KeyValue->TitleIndex,
00167 KeyValue->Type,
00168 buffer+KeyValue->DataOffset,
00169 KeyValue->DataLength);
00170
if (!
NT_SUCCESS(
Status)) {
00171 printf(
"rttrecpy: NtSetValueKey failed to set value %wS\n",&
ValueName);
00172 }
00173
00174 }
00175
00176
00177
00178
00179
00180 KeyInformation = (PKEY_BASIC_INFORMATION)buffer;
00181
if (
CopySecurity) {
00182 SecurityDescriptor = SecurityBuffer;
00183 }
else {
00184 SecurityDescriptor =
NULL;
00185 }
00186
for (index = 0;
TRUE; index++) {
00187
00188
Status =
NtEnumerateKey(Source,
00189 index,
00190 KeyBasicInformation,
00191 KeyInformation,
00192
WORK_SIZE,
00193 &ResultLength);
00194
00195
if (
Status == STATUS_NO_MORE_ENTRIES) {
00196
00197
break;
00198
00199 }
else if (!
NT_SUCCESS(
Status)) {
00200 printf(
"rttrecpy: NtEnumerateKey failed Status = %08lx\n",
Status);
00201
exit(1);
00202 }
00203
00204
KeyName.Buffer = KeyInformation->Name;
00205
KeyName.Length = KeyInformation->NameLength;
00206
00207 InitializeObjectAttributes(
00208 &
ObjectAttributes,
00209 &
KeyName,
00210 OBJ_CASE_INSENSITIVE,
00211 Source,
00212
NULL
00213 );
00214
00215
Status =
NtOpenKey(
00216 &SourceChild,
00217 KEY_READ,
00218 &
ObjectAttributes
00219 );
00220
if (!
NT_SUCCESS(
Status)) {
00221 printf(
"rttrecpy: NtOpenKey %wS failed: %08lx\n",
Status);
00222
exit(1);
00223 }
00224
00225
if (
CopySecurity) {
00226
Status =
NtQuerySecurityObject(SourceChild,
00227 DACL_SECURITY_INFORMATION,
00228 SecurityDescriptor,
00229
WORK_SIZE,
00230 &ResultLength);
00231
if (!
NT_SUCCESS(
Status)) {
00232 printf(
"rttrecpy: NtQuerySecurityObject failed %08lx\n",
Status);
00233 }
00234 }
00235
00236 InitializeObjectAttributes(&
ObjectAttributes,
00237 &
KeyName,
00238 OBJ_CASE_INSENSITIVE,
00239 Dest,
00240 SecurityDescriptor);
00241
Status =
NtCreateKey(&DestChild,
00242 KEY_READ | KEY_WRITE,
00243 &
ObjectAttributes,
00244 0,
00245
NULL,
00246 0,
00247
NULL);
00248
if (!
NT_SUCCESS(
Status)) {
00249 printf(
"rttrecpy: NtCreateKey %wS failed %08lx\n",
Status);
00250
exit(1);
00251 }
00252
00253
Copy(SourceChild, DestChild);
00254
NtClose(SourceChild);
00255
NtClose(DestChild);
00256
00257 }
00258
00259
return;
00260 }
00261
00262
00263
void
00264 processargs(
00265
int argc,
00266
char *argv[]
00267 )
00268 {
00269 ANSI_STRING temp;
00270
char **p;
00271
00272
if ( (argc > 4) || (argc < 3) )
00273 {
00274 printf(
"Usage: %s [-s] <SourceKey> <DestKey>\n",
00275 argv[0]);
00276
exit(1);
00277 }
00278
00279 p=argv+1;
00280
if (_stricmp(*p,
"-s")==0) {
00281
CopySecurity =
TRUE;
00282 ++p;
00283 }
else {
00284
CopySecurity =
FALSE;
00285 }
00286
00287
RtlInitAnsiString(
00288 &temp,
00289 *p
00290 );
00291
00292 ++p;
00293
00294
RtlAnsiStringToUnicodeString(
00295 &
SourceName,
00296 &temp,
00297
TRUE
00298 );
00299
00300
RtlInitAnsiString(&temp,
00301 *p);
00302
00303
RtlAnsiStringToUnicodeString(&
DestName,
00304 &temp,
00305
TRUE);
00306
00307
return;
00308 }