views:

224

answers:

4

I need to punctually invoque commands (net share, netsh...) requiring admin rights from a software launched by a user account.

Under a Unix variant, I would install alongside my user mode software a small, cautiously written script bearing a suid bit and which would invoke the needed command(s).

What is the equivalent best practice under Microsoft Windows? I would especially be interested in a Vista friendly, but XP compatible solution.

+1  A: 

One method could be to have a service that runs with administratively privileges. Then from your application, tell the service to invoke those commands. I'm assuming that you do not want the user to be required to know administrative credentials.

BobbyShaftoe
I indeed would like to shed the user from ussues such as knowing and typing in credentials. Your method is ingenious, but isn't a continously running service overkill for this task? Is there no Windows suid equivalent?
Tanelorn
There isn't really an equivalent. You could get the user credentials from the installing user and store that in some encrypted form somewhere else. However, the user may not like that and you would have to handle the case when the credentials change. But it would work too, just use the RunAs feature
BobbyShaftoe
However, I would go with the first method.
BobbyShaftoe
+1  A: 

As BobbyShaftoe says, the canonical way to solve this problem in Windows is via a service, since services by default execute under the LocalSystem account. Any other method is going to need to logon as an admin user, which would require credentials.

The additional bit of information that you'd need for Vista is that the client application would need to talk to the service via some form of IPC that can cross session boundaries, since on Vista the console and services are in different sessions. The normal IPC method used in this case is a named pipe.

The same solution will work fine in XP.

Bob Moore
This is exactly the kind of answer I needed; I wanted to use a canonical way which does not require specific credentials. Thanks for the extra tip wrt Vista.
Tanelorn
A: 

You can use the WinAPI functions LogonUser and CreateProcessAsUser to programatically start a new process with different access rights.

For your scenario I would write a batch script with the required net commands, and if needed create a new cmd.exe process that gets the script name as a parameter.

This solution requires you to store the credentials used in LogonUser in your application somehow, so there might be security risks.

Treb
Batch script called with administrative privileges? This is a train-wreck disaster waiting to happen. Storing admin credentials isn't a good idea either.
snemarch
A: 

If this is just for a particular user or two on a small handful of machines, you could use the MakeMeAdmin scripts and type in the admin password once yourself (stored with /savecred) on the the machine.

Dan