winutil.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package winutil contains misc Windows/Win32 helper functions.
  4. package winutil
  5. import (
  6. "os/user"
  7. )
  8. const (
  9. // RegBase is the registry path inside HKEY_LOCAL_MACHINE where registry settings
  10. // are stored. This constant is a non-empty string only when GOOS=windows.
  11. RegBase = regBase
  12. // RegPolicyBase is the registry path inside HKEY_LOCAL_MACHINE where registry
  13. // policies are stored. This constant is a non-empty string only when
  14. // GOOS=windows.
  15. RegPolicyBase = regPolicyBase
  16. )
  17. // GetPolicyString looks up a registry value in the local machine's path for
  18. // system policies, or returns empty string and the error.
  19. // Use this function to read values that may be set by sysadmins via the MSI
  20. // installer or via GPO. For registry settings that you do *not* want to be
  21. // visible to sysadmin tools, use GetRegString instead.
  22. //
  23. // This function will only work on GOOS=windows. Trying to run it on any other
  24. // OS will always return an empty string and ErrNoValue.
  25. // If value does not exist or another error happens, returns empty string and error.
  26. func GetPolicyString(name string) (string, error) {
  27. return getPolicyString(name)
  28. }
  29. // GetPolicyInteger looks up a registry value in the local machine's path for
  30. // system policies, or returns 0 and the associated error.
  31. // Use this function to read values that may be set by sysadmins via the MSI
  32. // installer or via GPO. For registry settings that you do *not* want to be
  33. // visible to sysadmin tools, use GetRegInteger instead.
  34. //
  35. // This function will only work on GOOS=windows. Trying to run it on any other
  36. // OS will always return 0 and ErrNoValue.
  37. // If value does not exist or another error happens, returns 0 and error.
  38. func GetPolicyInteger(name string) (uint64, error) {
  39. return getPolicyInteger(name)
  40. }
  41. func GetPolicyStringArray(name string) ([]string, error) {
  42. return getPolicyStringArray(name)
  43. }
  44. // GetRegString looks up a registry path in the local machine path, or returns
  45. // an empty string and error.
  46. //
  47. // This function will only work on GOOS=windows. Trying to run it on any other
  48. // OS will always return an empty string and ErrNoValue.
  49. // If value does not exist or another error happens, returns empty string and error.
  50. func GetRegString(name string) (string, error) {
  51. return getRegString(name)
  52. }
  53. // GetRegInteger looks up a registry path in the local machine path, or returns
  54. // 0 and the error.
  55. //
  56. // This function will only work on GOOS=windows. Trying to run it on any other
  57. // OS will always return 0 and ErrNoValue.
  58. // If value does not exist or another error happens, returns 0 and error.
  59. func GetRegInteger(name string) (uint64, error) {
  60. return getRegInteger(name)
  61. }
  62. // IsSIDValidPrincipal determines whether the SID contained in uid represents a
  63. // type that is a valid security principal under Windows. This check helps us
  64. // work around a bug in the standard library's Windows implementation of
  65. // LookupId in os/user.
  66. // See https://github.com/tailscale/tailscale/issues/869
  67. //
  68. // This function will only work on GOOS=windows. Trying to run it on any other
  69. // OS will always return false.
  70. func IsSIDValidPrincipal(uid string) bool {
  71. return isSIDValidPrincipal(uid)
  72. }
  73. // LookupPseudoUser attempts to resolve the user specified by uid by checking
  74. // against well-known pseudo-users on Windows. This is a temporary workaround
  75. // until https://github.com/golang/go/issues/49509 is resolved and shipped.
  76. //
  77. // This function will only work on GOOS=windows. Trying to run it on any other
  78. // OS will always return an error.
  79. func LookupPseudoUser(uid string) (*user.User, error) {
  80. return lookupPseudoUser(uid)
  81. }
  82. // RegisterForRestartOpts supplies options to RegisterForRestart.
  83. type RegisterForRestartOpts struct {
  84. RestartOnCrash bool // When true, this program will be restarted after a crash.
  85. RestartOnHang bool // When true, this program will be restarted after a hang.
  86. RestartOnUpgrade bool // When true, this program will be restarted after an upgrade.
  87. RestartOnReboot bool // When true, this program will be restarted after a reboot.
  88. UseCmdLineArgs bool // When true, CmdLineArgs will be used as the program's arguments upon restart. Otherwise no arguments will be provided.
  89. CmdLineArgs []string // When UseCmdLineArgs == true, contains the command line arguments, excluding the executable name itself. If nil or empty, the arguments from the current process will be re-used.
  90. }
  91. // RegisterForRestart registers the current process' restart preferences with
  92. // the Windows Restart Manager. This enables the OS to intelligently restart
  93. // the calling executable as requested via opts. This should be called by any
  94. // programs which need to be restarted by the installer post-update.
  95. //
  96. // This function may be called multiple times; the opts from the most recent
  97. // call will override those from any previous invocations.
  98. //
  99. // This function will only work on GOOS=windows. Trying to run it on any other
  100. // OS will always return nil.
  101. func RegisterForRestart(opts RegisterForRestartOpts) error {
  102. return registerForRestart(opts)
  103. }