From 7de126d52c3b150aa8288b672dde546bc99b2f5f Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Wed, 1 May 2019 17:35:29 +0800 Subject: [PATCH] pwauth: Optimize code Signed-off-by: Jianhui Zhao --- http.go | 3 +-- pwauth/auth_darwin.go | 8 ++++++-- pwauth/auth_freebsd.go | 13 +++++++++---- pwauth/auth_linux.go | 13 +++++++++---- pwauth/auth_windows.go | 13 +++++++++++-- pwauth/pwauth.go | 7 +++++++ 6 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 pwauth/pwauth.go diff --git a/http.go b/http.go index 030cb6d..337eeb3 100644 --- a/http.go +++ b/http.go @@ -47,8 +47,7 @@ func httpAuth(w http.ResponseWriter, r *http.Request) bool { } func httpLogin(cfg *RttysConfig, creds *Credentials) bool { - ok := pwauth.Auth(creds.Username, creds.Password) - if ok { + if err := pwauth.Auth(creds.Username, creds.Password); err == nil { return true } diff --git a/pwauth/auth_darwin.go b/pwauth/auth_darwin.go index d55d25e..87d5a84 100644 --- a/pwauth/auth_darwin.go +++ b/pwauth/auth_darwin.go @@ -1,6 +1,10 @@ package pwauth +import ( + "errors" +) + // Need to be implemented -func Auth(username, password string) bool { - return false +func auth(username, password string) error { + return errors.New("not implemented") } diff --git a/pwauth/auth_freebsd.go b/pwauth/auth_freebsd.go index 967ac25..bfdbb51 100644 --- a/pwauth/auth_freebsd.go +++ b/pwauth/auth_freebsd.go @@ -4,6 +4,7 @@ import ( "errors" "io/ioutil" "os" + "os/user" "strings" "github.com/GehirnInc/crypt" @@ -37,16 +38,20 @@ func getPassword(name string) (string, error) { return "", errors.New("Not found") } -func Auth(username, password string) bool { +func auth(username, password string) error { if os.Getuid() != 0 { - return false + return errors.New("Cannot possibly work without effective root") + } + + if _, err := user.Lookup(username); err != nil { + return err } pw, err := getPassword(username) if err != nil { - return false + return err } c := crypt.NewFromHash(pw) - return c.Verify(pw, []byte(password)) == nil + return c.Verify(pw, []byte(password)) } diff --git a/pwauth/auth_linux.go b/pwauth/auth_linux.go index fb6c6ad..9fbf631 100644 --- a/pwauth/auth_linux.go +++ b/pwauth/auth_linux.go @@ -4,6 +4,7 @@ import ( "errors" "io/ioutil" "os" + "os/user" "strings" "github.com/GehirnInc/crypt" @@ -37,16 +38,20 @@ func getPassword(name string) (string, error) { return "", errors.New("Not found") } -func Auth(username, password string) bool { +func auth(username, password string) error { if os.Getuid() != 0 { - return false + return errors.New("Cannot possibly work without effective root") + } + + if _, err := user.Lookup(username); err != nil { + return err } pw, err := getPassword(username) if err != nil { - return false + return err } c := crypt.NewFromHash(pw) - return c.Verify(pw, []byte(password)) == nil + return c.Verify(pw, []byte(password)) } diff --git a/pwauth/auth_windows.go b/pwauth/auth_windows.go index bf549b9..f65c8c9 100644 --- a/pwauth/auth_windows.go +++ b/pwauth/auth_windows.go @@ -1,6 +1,7 @@ package pwauth import ( + "os/user" "syscall" "unsafe" ) @@ -31,12 +32,20 @@ func LogonUserW(username, domain, password *uint16, logonType, logonProvider uin return token, nil } -func Auth(username, password string) bool { +func auth(username, password string) error { + if _, err := user.Lookup(username); err != nil { + return err + } + pUsername, _ := syscall.UTF16PtrFromString(username) pDomain, _ := syscall.UTF16PtrFromString(".") pPassword, _ := syscall.UTF16PtrFromString(password) _, err := LogonUserW(pUsername, pDomain, pPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT) - return err == nil || err == errERROR_ACCOUNT_RESTRICTION + if err == errERROR_ACCOUNT_RESTRICTION { + return nil + } + + return err } diff --git a/pwauth/pwauth.go b/pwauth/pwauth.go new file mode 100644 index 0000000..e2f264d --- /dev/null +++ b/pwauth/pwauth.go @@ -0,0 +1,7 @@ +package pwauth + +// Auth check the validity of the username/password pair.If the +// credentials are not valid, this function will return an error. +func Auth(username, password string) error { + return auth(username, password) +}