You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

security.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build windows
  5. package svc
  6. import (
  7. "golang.org/x/sys/windows"
  8. )
  9. func allocSid(subAuth0 uint32) (*windows.SID, error) {
  10. var sid *windows.SID
  11. err := windows.AllocateAndInitializeSid(&windows.SECURITY_NT_AUTHORITY,
  12. 1, subAuth0, 0, 0, 0, 0, 0, 0, 0, &sid)
  13. if err != nil {
  14. return nil, err
  15. }
  16. return sid, nil
  17. }
  18. // IsAnInteractiveSession determines if calling process is running interactively.
  19. // It queries the process token for membership in the Interactive group.
  20. // http://stackoverflow.com/questions/2668851/how-do-i-detect-that-my-application-is-running-as-service-or-in-an-interactive-s
  21. func IsAnInteractiveSession() (bool, error) {
  22. interSid, err := allocSid(windows.SECURITY_INTERACTIVE_RID)
  23. if err != nil {
  24. return false, err
  25. }
  26. defer windows.FreeSid(interSid)
  27. serviceSid, err := allocSid(windows.SECURITY_SERVICE_RID)
  28. if err != nil {
  29. return false, err
  30. }
  31. defer windows.FreeSid(serviceSid)
  32. t, err := windows.OpenCurrentProcessToken()
  33. if err != nil {
  34. return false, err
  35. }
  36. defer t.Close()
  37. gs, err := t.GetTokenGroups()
  38. if err != nil {
  39. return false, err
  40. }
  41. for _, g := range gs.AllGroups() {
  42. if windows.EqualSid(g.Sid, interSid) {
  43. return true, nil
  44. }
  45. if windows.EqualSid(g.Sid, serviceSid) {
  46. return false, nil
  47. }
  48. }
  49. return false, nil
  50. }