winutil.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // RegBase is the registry path inside HKEY_LOCAL_MACHINE where registry settings
  9. // are stored. This constant is a non-empty string only when GOOS=windows.
  10. const RegBase = regBase
  11. // GetPolicyString looks up a registry value in the local machine's path for
  12. // system policies, or returns the given default if it can't.
  13. // Use this function to read values that may be set by sysadmins via the MSI
  14. // installer or via GPO. For registry settings that you do *not* want to be
  15. // visible to sysadmin tools, use GetRegString instead.
  16. //
  17. // This function will only work on GOOS=windows. Trying to run it on any other
  18. // OS will always return the default value.
  19. func GetPolicyString(name, defval string) string {
  20. return getPolicyString(name, defval)
  21. }
  22. // GetPolicyInteger looks up a registry value in the local machine's path for
  23. // system policies, or returns the given default if it can't.
  24. // Use this function to read values that may be set by sysadmins via the MSI
  25. // installer or via GPO. For registry settings that you do *not* want to be
  26. // visible to sysadmin tools, use GetRegInteger instead.
  27. //
  28. // This function will only work on GOOS=windows. Trying to run it on any other
  29. // OS will always return the default value.
  30. func GetPolicyInteger(name string, defval uint64) uint64 {
  31. return getPolicyInteger(name, defval)
  32. }
  33. // GetRegString looks up a registry path in the local machine path, or returns
  34. // the given default if it can't.
  35. //
  36. // This function will only work on GOOS=windows. Trying to run it on any other
  37. // OS will always return the default value.
  38. func GetRegString(name, defval string) string {
  39. return getRegString(name, defval)
  40. }
  41. // GetRegInteger looks up a registry path in the local machine path, or returns
  42. // the given default if it can't.
  43. //
  44. // This function will only work on GOOS=windows. Trying to run it on any other
  45. // OS will always return the default value.
  46. func GetRegInteger(name string, defval uint64) uint64 {
  47. return getRegInteger(name, defval)
  48. }
  49. // IsSIDValidPrincipal determines whether the SID contained in uid represents a
  50. // type that is a valid security principal under Windows. This check helps us
  51. // work around a bug in the standard library's Windows implementation of
  52. // LookupId in os/user.
  53. // See https://github.com/tailscale/tailscale/issues/869
  54. //
  55. // This function will only work on GOOS=windows. Trying to run it on any other
  56. // OS will always return false.
  57. func IsSIDValidPrincipal(uid string) bool {
  58. return isSIDValidPrincipal(uid)
  59. }
  60. // LookupPseudoUser attempts to resolve the user specified by uid by checking
  61. // against well-known pseudo-users on Windows. This is a temporary workaround
  62. // until https://github.com/golang/go/issues/49509 is resolved and shipped.
  63. //
  64. // This function will only work on GOOS=windows. Trying to run it on any other
  65. // OS will always return an error.
  66. func LookupPseudoUser(uid string) (*user.User, error) {
  67. return lookupPseudoUser(uid)
  68. }