I am making two syscalls to the IIS Hosted Web Core (HWC) WebCoreActivate
and WebCoreShutdown
APIs. I would like to add a user context, so when the syscalls are made in Windows, they are run underneath a specific user's context - i.e., that user's permissions.
type WebCore struct {
activated bool
Handle syscall.Handle
Credentials syscall.Credential
}
func New() (error, *WebCore) {
hwebcore, err := syscall.LoadLibrary(os.ExpandEnv(`${windir}\system32\inetsrv\hwebcore.dll`))
if err != nil {
return err, nil
}
return nil, &WebCore{
activated: false,
Handle: hwebcore,
}
}
func (w *WebCore) Activate(appHostConfigPath, rootWebConfigPath, instanceName string) error {
if !w.activated {
webCoreActivate, err := syscall.GetProcAddress(w.Handle, "WebCoreActivate")
if err != nil {
return err
}
var nargs uintptr = 3
_, _, exitCode := syscall.Syscall(uintptr(webCoreActivate),
nargs,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(appHostConfigPath))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(rootWebConfigPath))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(instanceName))))
if exitCode != 0 {
return fmt.Errorf("WebCoreActivate returned exit code: %d", exitCode)
}
fmt.Printf("Server Started for %+v
", instanceName)
w.activated = true
}
return nil
}
func (w *WebCore) Shutdown(immediate int, instanceName string) error {
if w.activated {
webCoreShutdown, err := syscall.GetProcAddress(w.Handle, "WebCoreShutdown")
if err != nil {
return err
}
var nargs uintptr = 1
_, _, exitCode := syscall.Syscall(uintptr(webCoreShutdown),
nargs, uintptr(unsafe.Pointer(&immediate)), 0, 0)
if exitCode != 0 {
return fmt.Errorf("WebCoreShutdown returned exit code: %d", exitCode)
}
fmt.Printf("Server Shutdown for %+v
", instanceName)
}
return nil
}
I've been reading about the exec.Command().SysProcAttr
structure, and how you can attach specific user credentials via the syscall.Credentials{}
structure. However, that seems limited to executing commands via os.exec
. If I wanted to tie a specific user's context to a native syscall in Windows, how would I do that?