views:

147

answers:

4

I've a website that puts info into a mySQL database and a windows service (written in VB.Net) that polls the db and actions what's in there. However, occasionally it stalls and rather than having to RDP into the server, I want superusers to click on a button to restart the service. I can get the button to say, do a directory listing of c:\ output to a test file so the whole 'getting a command to execute on a remote server' issue works. However, I can't restart services. I assume this is a security thing (although it has full rights to c:\ I thought I'd sussed that already). The website runs under the normal IUSR_user so am I doing something really stupid or can someone explain how to get the service to be restarted ?

Many thanks Adrian

A: 

I'd write a service that restarts selected services and call that from my website, just for fun!

Mr. Smith
A: 

It seems that you need to give the user permissions on the specific service. The simplest way to do this (in my view) is to use subinacl.exe from the Windows Resource Kit.

subinacl.exe /service MySQL /Grant=IUSR_user

I'm would be very careful when doing this on a public-facing web server. Have both authentication and rate limiting on the restarting code.

itsadok
It's not public facing so no issues there. Installed it, ran the command as you showed but alas, no joy. Shows no error message, just doesn't do anything :-(
Adrian Aitken
A: 

Use the ServiceController class:

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx

This should allow you to connect to the named service and stop/start it.

EDIT: In addition, you probably need to have your web app impersonate a user with rights to restart the service. This can be accomplished via an identity impersonate in the web.config, or running the web app in a configured application pool in IIS. Keep in mind that the web app will have to run on the same box as the service is running on.

XDecker
do you have any web.config samples showing how to impersonate someone ?
Adrian Aitken
It's pretty straightforward. Create an IIS application pool with the identity of the domain user you want your app to run as. Add your application to this poolThen in the web.config add: <system.web><identity impersonate="true" /></system.web>Your web app will then run as the domain user that you specified in its application pool
XDecker
+1  A: 

I think a significantly bigger problem is that you have a service that's stalling. Do you have any logs or errors that the service outputs to that you could use to diagnose why it's stalling?

Leafy
see comment above !!!
Adrian Aitken