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 <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
diff --git a/windows/svc/mgr/config.go b/windows/svc/mgr/config.go
index d804e31..61447a5 100644
--- a/windows/svc/mgr/config.go
+++ b/windows/svc/mgr/config.go
@@ -42,6 +42,7 @@
 	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 @@
 		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 @@
 	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 76965b5..24638d7 100644
--- a/windows/svc/mgr/mgr.go
+++ b/windows/svc/mgr/mgr.go
@@ -102,9 +102,19 @@
 	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
 		}
 	}