From 83eebf71338e4ab31b265a5813543f156af28886 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 6 Jun 2019 18:40:12 +0200 Subject: [PATCH] windows: properly plumb sidtype into service creation Many service attributes are settable using the ordinary CreateService function, but ones added later in Windows need to be set using the ChangeServiceConfig2 function. One of these is the Description field, which is nicely plumbed behind the scenes, so that users of the API can set it, and the mgr package will just figure out what to do with it. Another one that works exactly the same way is SidType. Support for its constants was added in 30999d6 ("windows: add missing service constants"), but it wasn't actually built into the mgr package's configuration struct, creating inconstancies in interface. This commit rectifies that by adding proper support to mgr's config struct. Change-Id: I4f148f2d2477a03a65e8a571a8401392f6fe9f28 Reviewed-on: https://go-review.googlesource.com/c/sys/+/180978 Run-TryBot: Jason Donenfeld TryBot-Result: Gobot Gobot Reviewed-by: Matt Layher Reviewed-by: Alex Brainman --- windows/svc/mgr/config.go | 9 +++++++++ windows/svc/mgr/mgr.go | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/windows/svc/mgr/config.go b/windows/svc/mgr/config.go index d804e31f..61447a58 100644 --- a/windows/svc/mgr/config.go +++ b/windows/svc/mgr/config.go @@ -42,6 +42,7 @@ type Config struct { DisplayName string Password string Description string + SidType uint32 // one of SERVICE_SID_TYPE, the type of sid to use for the service } func toString(p *uint16) string { @@ -114,6 +115,10 @@ func updateDescription(handle windows.Handle, desc string) error { windows.SERVICE_CONFIG_DESCRIPTION, (*byte)(unsafe.Pointer(&d))) } +func updateSidType(handle windows.Handle, sidType uint32) error { + return windows.ChangeServiceConfig2(handle, windows.SERVICE_CONFIG_SERVICE_SID_INFO, (*byte)(unsafe.Pointer(&sidType))) +} + // UpdateConfig updates service s configuration parameters. func (s *Service) UpdateConfig(c Config) error { err := windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType, @@ -123,6 +128,10 @@ func (s *Service) UpdateConfig(c Config) error { if err != nil { return err } + err = updateSidType(s.Handle, c.SidType) + if err != nil { + return err + } return updateDescription(s.Handle, c.Description) } diff --git a/windows/svc/mgr/mgr.go b/windows/svc/mgr/mgr.go index 76965b56..24638d72 100644 --- a/windows/svc/mgr/mgr.go +++ b/windows/svc/mgr/mgr.go @@ -102,9 +102,19 @@ func (m *Mgr) CreateService(name, exepath string, c Config, args ...string) (*Se if err != nil { return nil, err } + if c.SidType != windows.SERVICE_SID_TYPE_NONE { + err = updateSidType(h, c.SidType) + if err != nil { + windows.DeleteService(h) + windows.CloseHandle(h) + return nil, err + } + } if c.Description != "" { err = updateDescription(h, c.Description) if err != nil { + windows.DeleteService(h) + windows.CloseHandle(h) return nil, err } }