I’ve got a website that has windows authentication enable on it. From a page in the website, the users have the ability to start a service that does some stuff with the database.
It works fine for me to start the service because I’m a local admin on the server. But I just had a user test it and they can’t get the service started.
My question is:
Does anyone know of a way to get a list of services on a specified computer by name using a different windows account than the one they are currently logged in with?
I really don’t want to add all the users that need to start the service into a windows group and set them all to a local admin on my IIS server…..
Here’s some of the code I’ve got:
public static ServiceControllerStatus FindService() { ServiceControllerStatus status = ServiceControllerStatus.Stopped; try { string machineName = ConfigurationManager.AppSettings['ServiceMachineName']; ServiceController[] services = ServiceController.GetServices(machineName); string serviceName = ConfigurationManager.AppSettings['ServiceName'].ToLower(); foreach (ServiceController service in services) { if (service.ServiceName.ToLower() == serviceName) { status = service.Status; break; } } } catch(Exception ex) { status = ServiceControllerStatus.Stopped; SaveError(ex, 'Utilities - FindService()'); } return status; }
My exception comes from the second line in the try block. Here’s the error:
System.InvalidOperationException: Cannot open Service Control Manager on computer ‘server.domain.com’. This operation might require other privileges. —> System.ComponentModel.Win32Exception: Access is denied — End of inner exception stack trace — at System.ServiceProcess.ServiceController.GetDataBaseHandleWithAccess(String machineName, Int32 serviceControlManaqerAccess) at System.ServiceProcess.ServiceController.GetServicesOfType(String machineName, Int32 serviceType) at TelemarketingWebSite.Utilities.StartService()
Thanks for the help/info
Note: This doesn’t address enumerating services as a different user, but given the broader description of what you’re doing, I think it’s a good answer.
I think you can simplify this a lot, and possibly avoid part of the security problem, if you go directly to the service of interest. Instead of calling GetServices, try this:
This connects directly to the service of interest and bypasses the enumeration/search step. Therefore, it doesn’t require the caller to have the
SC_MANAGER_ENUMERATE_SERVICEright on the Service Control Manager (SCM), which remote users do not have by default. It does still requireSC_MANAGER_CONNECT, but according to MSDN that should be granted to remote authenticated users.Once you have found the service of interest, you’ll still need to be able to stop and start it, which your remote users probably don’t have rights to do. However, it’s possible to modify the security descriptor (DACL) on individual services, which would let you grant your remote users access to stop and start the service without requiring them to be local admins. This is done via the SetNamedSecurityInfo API function. The access rights you need to grant are
SERVICE_STARTandSERVICE_STOP. Depending on exactly which groups these users belong to, you might also need to grant themGENERIC_READ. All of these rights are described in MSDN.Here is some C++ code that would perform this setup, assuming the users of interest are in the ‘Remote Service Controllers’ group (which you would create) and the service name is ‘my-service-name’. Note that if you wanted to grant access to a well-known group such as Users (not necessarily a good idea) rather than a group you created, you need to change
TRUSTEE_IS_GROUPtoTRUSTEE_IS_WELL_KNOWN_GROUP.The code has no error checking, which you would want to add. All three functions that can fail (Get/SetNamedSecurityInfo and SetEntriesInAcl) return 0 to indicate success.
Another Note: You can also set a service’s security descriptor using the SC tool, which can be found under %WINDIR%\System32, but that doesn’t involve any programming.
This could also be done from C# using P/Invoke, but that’s a bit more work.
If you still specifically want to be able to enumerate services as these users, you need to grant them the
SC_MANAGER_ENUMERATE_SERVICEright on the SCM. Unfortunately, according to MSDN, the SCM’s security can only be modified on Windows Server 2003 sp1 or later.