Fixed windows admin

This commit is contained in:
Stefan Goppelt 2022-04-06 09:43:17 +02:00
parent 01b1bb4153
commit 85f19a6074
4 changed files with 63 additions and 16 deletions

10
go.mod
View File

@ -2,9 +2,13 @@ module scm.yoorie.de/go-lib/util
go 1.18 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 ( require (
github.com/google/go-cmp v0.5.7 // indirect github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
) )

21
go.sum
View File

@ -1,8 +1,13 @@
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= 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=

View File

@ -4,18 +4,47 @@
package util package util
import ( import (
"log"
"os" "os"
"path/filepath" "path/filepath"
"golang.org/x/sys/windows"
) )
// IsSuperUser returns true, if the current user is a super user // IsSuperUser returns true, if the current user is a super user
// A.K.A root, Administrator etc // A.K.A root, Administrator etc
func IsSuperUser() bool { 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 { if err != nil {
log.Fatalf("SID Error: %s", err)
return false 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 // GetGlobalConfigurationDirectory returns OS specific location for putting

View File

@ -3,12 +3,12 @@ package util
import ( import (
"testing" "testing"
"gotest.tools/assert" "github.com/stretchr/testify/assert"
) )
func TestFileExist(t *testing.T) { func TestFileExist(t *testing.T) {
assert.Assert(t, FileExists("utils.go")) assert.True(t, FileExists("utils.go"))
assert.Assert(t, !FileExists("Utils2.go")) assert.True(t, !FileExists("Utils2.go"))
} }
func TestJoiningSlash(t *testing.T) { 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"))
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())
}
*/