Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,

I am trying to access the registry of a remote computer (windows 2003).
Following the remarks in the article:
http://msdn.microsoft.com/en-us/library/ms724840(v=vs.85).aspx
I used both Impersonation and WNetAddConnection2.

Here is my code:
C++
void ConnectRemoteRegistry(LPCSTR machine, LPWSTR wRemoteName, LPWSTR wUser, LPWSTR wPass, 

LPCSTR cUser, LPCSTR cPass, std::string keyName)
{
DWORD dwRetVal;
NETRESOURCE nr;
DWORD dwFlags;

// Zero out the NETRESOURCE struct
memset(&nr, 0, sizeof (NETRESOURCE));

// Assign our values to the NETRESOURCE structure.
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = NULL;
nr.lpRemoteName = wRemoteName;
nr.lpProvider = NULL;

// Assign a value to the connection options
dwFlags = CONNECT_UPDATE_PROFILE;
dwRetVal = WNetAddConnection2(&nr, wPass, wUser, dwFlags); 

if (dwRetVal == NO_ERROR)
    wprintf(L"Connection added to %s\n", nr.lpRemoteName);
else
    wprintf(L"WNetAddConnection2 failed with error: %u\n", dwRetVal);

HANDLE _token;

dwRetVal = LogonUserA(cUser, NULL, cPass, LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_PROVIDER_DEFAULT, 

&_token);

if(dwRetVal != ERROR_SUCCESS)
{
    wprintf(L"LogonUserA failed with error: %u\n", dwRetVal);
}

dwRetVal = ImpersonateLoggedOnUser(_token);

if(dwRetVal != ERROR_SUCCESS)
{
    wprintf(L"ImpersonateLoggedOnUser failed with error: %u\n", dwRetVal);
}

HKEY root(HKEY_LOCAL_MACHINE);
HKEY r;
LONG rc = ::RegConnectRegistryA(machine, root, &r);

if (rc != ERROR_SUCCESS) 
{
    wprintf(L"RegConnectRegistryA failed with error: %u\n", dwRetVal);
}

HKEY key_handle;

rc = ::RegOpenKeyExA(r,keyName.c_str(), REG_OPTION_OPEN_LINK, 
KEY_QUERY_VALUE, & key_handle);

if (rc != ERROR_SUCCESS) 
{
    wprintf(L"RegOpenKeyExA failed with error: %u\n", dwRetVal);
}

RegCloseKey(r);
RevertToSelf();

dwRetVal = WNetCancelConnection2(nr.lpRemoteName, CONNECT_UPDATE_PROFILE, true);

if (dwRetVal != NO_ERROR)
    wprintf(L"WNetCancelConnection2 failed with error: %u\n", dwRetVal);
}

I am using a local administrator of the remote machine.
WNetAddConnection2 to c$ is completed successfully.
LogonUserA and ImpersonateLoggedOnUser are completed successfully.
RegConnectRegistryA is also completed successfully.
The problem is that RegOpenKeyExA returns 5 - access denied.

Can you please advise why that might be?

Thanks.
Posted
Updated 31-Jan-11 19:54pm
v4

Well the parameters you are passing don't seem right. Check MSDN [^]for details

from the documentation
MIDL
ulOptions
[in] Reserved; set to 0.
samDesired
[in] Not supported; set to 0.


your values
MIDL
RegOpenKeyExA(r,keyName.c_str(), REG_OPTION_OPEN_LINK,
KEY_QUERY_VALUE, & key_handle);
 
Share this answer
 
Comments
Emilio Garavaglia 31-Jan-11 15:29pm    
Comment from the OP:
You are right, the third parameter is wrong - thanks for that. Yet after fixing it, I still get the same result.
Dalek Dave 1-Feb-11 3:09am    
Good Answer.
So the problem comes from samDesired fourth parameter (try only STANDARD_RIGHTS_READ | KEY_QUERY_VALUE), and/or combination of third and fourth. I suggest that you read precisely Registry Key Security and Access Rights.[^] :((
 
Share this answer
 
v2
OK. I resolved the issue (not related to the way accessing the registry).

The LogonUser function should be done with these parameters:
dwRetVal = LogonUserA(cUser, NULL, cPass, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50 , &_token);
Most important - the user name, should be the user name alone and NOT machineName\UserName.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900