views:

598

answers:

5

I need to remotely install windows service on number of computers, so I use CreateService() and other service functions from winapi. I know admin password and user name for machines that I need access to. In order to gain access to remote machine I impersonate calling process with help of LogonUser like this:

//all variables are initialized correctly  
int status = 0;        
status = LogonUser(lpwUsername,        
               lpwDomain,         
                   lpwPassword,          
                   LOGON32_LOGON_NEW_CREDENTIALS,       
                   LOGON32_PROVIDER_DEFAULT,   
                   &hToken);         


if (status == 0)    
{   
         //here comes a error  
}  

status = ImpersonateLoggedOnUser(hToken);     
if (status == 0)       
{     
    //once again a error     
}      

//ok, now we are impersonated, do all service work there

So, I gain access to machine in a domain, but some of computers are out of domain. On machines that are out of domain this code doesn't work. Is there any way to access service manager on machine out of domain?

+2  A: 

You can do it , the account needs to exist on the remote machine and you need to use the machine name for the domain name in the LogonUser call.

Tony Edgecombe
A: 

this doesn't work
I will be really very glad if you show me where i'm wrong. I call to OpenSCManager in place that i marked with the commentary in question.
SC_HANDLE schManager = OpenSCManager(lpwMachineName, NULL, SC_MANAGER_ALL_ACCESS); and get ERROR_ ACCESS _DENIED

Neverrav
A: 

Rather than rolling your own, why not just use the SC built-in command?

Jon Grant
A: 

OK, problem resolved (not really very good, but rather OK). I used WNetAddConnection() to ipc$ on remote machine.

Neverrav
A: 

I am also want to do the same thing. I am trying to install service on remote compute. Can you please provide your code, or hint to proceed. I am not able to understand what u have discussed 100%.

Thanks