By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,780 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,780 IT Pros & Developers. It's quick & easy.

RegLoadKey while impersonated.

P: n/a
Hi,

I'm having to hack (because I don’t know c# very well) together a migration
application for my company for use with migrating to active directory. (using
VS2005/.NET 2.0)

The goal is to write an application that will run under administrative
credentials to copy their old profile “My documents, favorites, desktop, and
default printer” to their new AD profile.

The application is complete except for the impersonation. Independently,
the two methods work great.. However, in order to load the registry hive (to
get their old default printer), I have to get the token for the current
process and adjust the privileges. I’ve been banging my head now for two
days and just can’t seem to figure out where I’m overlapped. The two just
will not work together. If I impersonate, I don’t get my hive information,
if I get my hive information, I don’t get my impersonation. I’ve verified
that the sudo credentials are the same as my personal administrative
credentials right down to system policies (Act as part of the OS, etc…). I’m
certain it’s related to the tokens

The return from the RegLoadKey is: 1314

Can anyone provide me any insight please?

Thank you very much…

Code ---
#### Impersonation (called after InitializeComponent)#######

//Get token and impersonate user!
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);

const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
const int SecurityImpersonation = 2;

tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;

bool returnValue = LogonUser(DomainuserName, domainName, userPass,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref
dupeTokenHandle);
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
//Impersonation complete, don't forget to undo personating!

#### RegLoadKey (called with “migrate” button) #######
int token = 0;
int retval = 0;
TOKEN_PRIVILEGES TP = new TOKEN_PRIVILEGES();
TOKEN_PRIVILEGES TP2 = new TOKEN_PRIVILEGES();
LUID RestoreLuid = new LUID();
LUID BackupLuid = new LUID();
retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY, ref token);
retval = LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestoreLuid);
retval = LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackupLuid);
TP.PrivilegeCount = 1;
TP.Attributes = SE_PRIVILEGE_ENABLED;
TP.Luid = RestoreLuid;
TP2.PrivilegeCount = 1;
TP2.Attributes = SE_PRIVILEGE_ENABLED;
TP2.Luid = BackupLuid;
retval = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
retval = AdjustTokenPrivileges(token, 0, ref TP2, 1024, 0, 0);
// Load the offline profile hive
int result = RegLoadKey(HKEY_USERS, "MIGRATION", strPath + @"\NTUSER.DAT");
RegistryKey dPrintKey = Registry.Users;
dPrintKey = dPrintKey.OpenSubKey(@"MIGRATION\Software\Microsof t\Windows
NT\CurrentVersion\Windows");
Object dPrinter = dPrintKey.GetValue("Device");
string retvalue = dPrinter.ToString();
dPrintKey.Close();
RegUnLoadKey(HKEY_USERS, "MIGRATION");

May 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Bump please

"Ed McAndrew" wrote:
Hi,

I'm having to hack (because I don’t know c# very well) together a migration
application for my company for use with migrating to active directory. (using
VS2005/.NET 2.0)

The goal is to write an application that will run under administrative
credentials to copy their old profile “My documents, favorites, desktop, and
default printer” to their new AD profile.

The application is complete except for the impersonation. Independently,
the two methods work great.. However, in order to load the registry hive (to
get their old default printer), I have to get the token for the current
process and adjust the privileges. I’ve been banging my head now for two
days and just can’t seem to figure out where I’m overlapped. The two just
will not work together. If I impersonate, I don’t get my hive information,
if I get my hive information, I don’t get my impersonation. I’ve verified
that the sudo credentials are the same as my personal administrative
credentials right down to system policies (Act as part of the OS, etc…). I’m
certain it’s related to the tokens

The return from the RegLoadKey is: 1314

Can anyone provide me any insight please?

Thank you very much…

Code ---
#### Impersonation (called after InitializeComponent)#######

//Get token and impersonate user!
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);

const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
const int SecurityImpersonation = 2;

tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;

bool returnValue = LogonUser(DomainuserName, domainName, userPass,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref
dupeTokenHandle);
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
//Impersonation complete, don't forget to undo personating!

#### RegLoadKey (called with “migrate” button) #######
int token = 0;
int retval = 0;
TOKEN_PRIVILEGES TP = new TOKEN_PRIVILEGES();
TOKEN_PRIVILEGES TP2 = new TOKEN_PRIVILEGES();
LUID RestoreLuid = new LUID();
LUID BackupLuid = new LUID();
retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY, ref token);
retval = LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestoreLuid);
retval = LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackupLuid);
TP.PrivilegeCount = 1;
TP.Attributes = SE_PRIVILEGE_ENABLED;
TP.Luid = RestoreLuid;
TP2.PrivilegeCount = 1;
TP2.Attributes = SE_PRIVILEGE_ENABLED;
TP2.Luid = BackupLuid;
retval = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
retval = AdjustTokenPrivileges(token, 0, ref TP2, 1024, 0, 0);
// Load the offline profile hive
int result = RegLoadKey(HKEY_USERS, "MIGRATION", strPath + @"\NTUSER.DAT");
RegistryKey dPrintKey = Registry.Users;
dPrintKey = dPrintKey.OpenSubKey(@"MIGRATION\Software\Microsof t\Windows
NT\CurrentVersion\Windows");
Object dPrinter = dPrintKey.GetValue("Device");
string retvalue = dPrinter.ToString();
dPrintKey.Close();
RegUnLoadKey(HKEY_USERS, "MIGRATION");

May 16 '06 #2

P: n/a
Ed McAndrew <Ed********@discussions.microsoft.com> wrote:
The return from the RegLoadKey is: 1314


net helpmsg 1314 returns:

"A required privilege is not held by the client."

-- Barry
May 16 '06 #3

P: n/a
Thanks Barry,

I'm aware that it's a privlige issue. I'm certain that I need to adjust the
tokens privlige for the impersonation using "AdjustTokenPrivileges" (as in
the second sniplet of code), but am unsure of what "handle" I should adjust.

"Barry Kelly" wrote:
Ed McAndrew <Ed********@discussions.microsoft.com> wrote:
The return from the RegLoadKey is: 1314


net helpmsg 1314 returns:

"A required privilege is not held by the client."

-- Barry

May 16 '06 #4

P: n/a
Bump please

"Ed McAndrew" wrote:
Hi,

I'm having to hack (because I don’t know c# very well) together a migration
application for my company for use with migrating to active directory. (using
VS2005/.NET 2.0)

The goal is to write an application that will run under administrative
credentials to copy their old profile “My documents, favorites, desktop, and
default printer” to their new AD profile.

The application is complete except for the impersonation. Independently,
the two methods work great.. However, in order to load the registry hive (to
get their old default printer), I have to get the token for the current
process and adjust the privileges. I’ve been banging my head now for two
days and just can’t seem to figure out where I’m overlapped. The two just
will not work together. If I impersonate, I don’t get my hive information,
if I get my hive information, I don’t get my impersonation. I’ve verified
that the sudo credentials are the same as my personal administrative
credentials right down to system policies (Act as part of the OS, etc…). I’m
certain it’s related to the tokens

The return from the RegLoadKey is: 1314

Can anyone provide me any insight please?

Thank you very much…

Code ---
#### Impersonation (called after InitializeComponent)#######

//Get token and impersonate user!
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);

const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
const int SecurityImpersonation = 2;

tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;

bool returnValue = LogonUser(DomainuserName, domainName, userPass,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref
dupeTokenHandle);
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
//Impersonation complete, don't forget to undo personating!

#### RegLoadKey (called with “migrate” button) #######
int token = 0;
int retval = 0;
TOKEN_PRIVILEGES TP = new TOKEN_PRIVILEGES();
TOKEN_PRIVILEGES TP2 = new TOKEN_PRIVILEGES();
LUID RestoreLuid = new LUID();
LUID BackupLuid = new LUID();
retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY, ref token);
retval = LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestoreLuid);
retval = LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackupLuid);
TP.PrivilegeCount = 1;
TP.Attributes = SE_PRIVILEGE_ENABLED;
TP.Luid = RestoreLuid;
TP2.PrivilegeCount = 1;
TP2.Attributes = SE_PRIVILEGE_ENABLED;
TP2.Luid = BackupLuid;
retval = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
retval = AdjustTokenPrivileges(token, 0, ref TP2, 1024, 0, 0);
// Load the offline profile hive
int result = RegLoadKey(HKEY_USERS, "MIGRATION", strPath + @"\NTUSER.DAT");
RegistryKey dPrintKey = Registry.Users;
dPrintKey = dPrintKey.OpenSubKey(@"MIGRATION\Software\Microsof t\Windows
NT\CurrentVersion\Windows");
Object dPrinter = dPrintKey.GetValue("Device");
string retvalue = dPrinter.ToString();
dPrintKey.Close();
RegUnLoadKey(HKEY_USERS, "MIGRATION");

May 17 '06 #5

P: n/a
Does anybody have any idea(s)?

Sorry to nag... but I'm down to the wire and still have not figured it out
on my own.

Ed

"Ed McAndrew" wrote:
Hi,

I'm having to hack (because I don’t know c# very well) together a migration
application for my company for use with migrating to active directory. (using
VS2005/.NET 2.0)

The goal is to write an application that will run under administrative
credentials to copy their old profile “My documents, favorites, desktop, and
default printer” to their new AD profile.

The application is complete except for the impersonation. Independently,
the two methods work great.. However, in order to load the registry hive (to
get their old default printer), I have to get the token for the current
process and adjust the privileges. I’ve been banging my head now for two
days and just can’t seem to figure out where I’m overlapped. The two just
will not work together. If I impersonate, I don’t get my hive information,
if I get my hive information, I don’t get my impersonation. I’ve verified
that the sudo credentials are the same as my personal administrative
credentials right down to system policies (Act as part of the OS, etc…). I’m
certain it’s related to the tokens

The return from the RegLoadKey is: 1314

Can anyone provide me any insight please?

Thank you very much…

Code ---
#### Impersonation (called after InitializeComponent)#######

//Get token and impersonate user!
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);

const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
const int SecurityImpersonation = 2;

tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;

bool returnValue = LogonUser(DomainuserName, domainName, userPass,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref
dupeTokenHandle);
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
//Impersonation complete, don't forget to undo personating!

#### RegLoadKey (called with “migrate” button) #######
int token = 0;
int retval = 0;
TOKEN_PRIVILEGES TP = new TOKEN_PRIVILEGES();
TOKEN_PRIVILEGES TP2 = new TOKEN_PRIVILEGES();
LUID RestoreLuid = new LUID();
LUID BackupLuid = new LUID();
retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY, ref token);
retval = LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestoreLuid);
retval = LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackupLuid);
TP.PrivilegeCount = 1;
TP.Attributes = SE_PRIVILEGE_ENABLED;
TP.Luid = RestoreLuid;
TP2.PrivilegeCount = 1;
TP2.Attributes = SE_PRIVILEGE_ENABLED;
TP2.Luid = BackupLuid;
retval = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
retval = AdjustTokenPrivileges(token, 0, ref TP2, 1024, 0, 0);
// Load the offline profile hive
int result = RegLoadKey(HKEY_USERS, "MIGRATION", strPath + @"\NTUSER.DAT");
RegistryKey dPrintKey = Registry.Users;
dPrintKey = dPrintKey.OpenSubKey(@"MIGRATION\Software\Microsof t\Windows
NT\CurrentVersion\Windows");
Object dPrinter = dPrintKey.GetValue("Device");
string retvalue = dPrinter.ToString();
dPrintKey.Close();
RegUnLoadKey(HKEY_USERS, "MIGRATION");

May 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.