diff --git a/go.mod b/go.mod index fdac399..694ab5d 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,13 @@ module scm.yoorie.de/go-lib/util go 1.18 -require gotest.tools v2.2.0+incompatible +require ( + github.com/stretchr/testify v1.7.1 + golang.org/x/sys v0.0.0-20220405052023-b1e9470b6e64 +) require ( - github.com/google/go-cmp v0.5.7 // indirect - github.com/pkg/errors v0.9.1 // indirect + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index bf21e2d..eab2ab1 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,13 @@ -github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= -github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= -gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220405052023-b1e9470b6e64 h1:D1v9ucDTYBtbz5vNuBbAhIMAGhQhJ6Ym5ah3maMVNX4= +golang.org/x/sys v0.0.0-20220405052023-b1e9470b6e64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/os_windows.go b/os_windows.go index 102eee1..ea7e3ce 100644 --- a/os_windows.go +++ b/os_windows.go @@ -4,18 +4,47 @@ package util import ( + "log" "os" "path/filepath" + + "golang.org/x/sys/windows" ) // IsSuperUser returns true, if the current user is a super user // A.K.A root, Administrator etc func IsSuperUser() bool { - _, err := os.Open("\\\\.\\PHYSICALDRIVE0") + var sid *windows.SID + + // Although this looks scary, it is directly copied from the + // official windows documentation. The Go API for this is a + // direct wrap around the official C++ API. + // See https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-checktokenmembership + err := windows.AllocateAndInitializeSid( + &windows.SECURITY_NT_AUTHORITY, + 2, + windows.SECURITY_BUILTIN_DOMAIN_RID, + windows.DOMAIN_ALIAS_RID_ADMINS, + 0, 0, 0, 0, 0, 0, + &sid) if err != nil { + log.Fatalf("SID Error: %s", err) return false } - return true + defer windows.FreeSid(sid) + + // This appears to cast a null pointer so I'm not sure why this + // works, but this guy says it does and it Works for Me™: + // https://github.com/golang/go/issues/28804#issuecomment-438838144 + token := windows.Token(0) + + member, err := token.IsMember(sid) + if err != nil { + log.Fatalf("Token Membership Error: %s", err) + return false + } + + return member } // GetGlobalConfigurationDirectory returns OS specific location for putting diff --git a/utils_test.go b/utils_test.go index 04b8308..53de342 100644 --- a/utils_test.go +++ b/utils_test.go @@ -3,12 +3,12 @@ package util import ( "testing" - "gotest.tools/assert" + "github.com/stretchr/testify/assert" ) func TestFileExist(t *testing.T) { - assert.Assert(t, FileExists("utils.go")) - assert.Assert(t, !FileExists("Utils2.go")) + assert.True(t, FileExists("utils.go")) + assert.True(t, !FileExists("Utils2.go")) } func TestJoiningSlash(t *testing.T) { @@ -18,3 +18,12 @@ func TestJoiningSlash(t *testing.T) { assert.Equal(t, "http://my.tld/docs/bla/blub", JoiningSlash("http://my.tld/docs", "bla/", "blub")) assert.Equal(t, "http://my.tld/docs/bla/blub", JoiningSlash("http://my.tld/docs/", "bla/", "blub")) } + +/* Can run only as admin within windows or linux +func TestIsSuperUser(t *testing.T) { + cuser, err := user.Current() + assert.Nil(t, err) + assert.NotNil(t, cuser) + assert.True(t, IsSuperUser()) +} +*/