Skip to content

Commit

Permalink
Support login in freebsd by system user/password
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
Jianhui Zhao committed Apr 29, 2019
1 parent 4532711 commit 6c9c7fe
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ generate windows 386

generate darwin amd64
generate darwin 386

generate freebsd amd64
generate freebsd 386
57 changes: 57 additions & 0 deletions login_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"errors"
"io/ioutil"
"os"
"strings"

"github.com/GehirnInc/crypt"
_ "github.com/GehirnInc/crypt/apr1_crypt"
_ "github.com/GehirnInc/crypt/md5_crypt"
_ "github.com/GehirnInc/crypt/sha256_crypt"
_ "github.com/GehirnInc/crypt/sha512_crypt"
)

type spwd struct {
sp_namp string
sp_pwdp string
}

func getspnam(name string) (*spwd, error) {
/* Disallow potentially-malicious user names */
if name == "" || name[0] == '.' || strings.Contains(name, "/") {
return nil, errors.New("Invalid")
}

data, err := ioutil.ReadFile("/etc/master.passwd")
if err != nil {
return nil, err
}

for _, l := range strings.Split(string(data), "\n") {
if !strings.HasPrefix(l, name+":") {
continue
}

s := strings.Split(l, ":")

return &spwd{s[0], s[1]}, nil
}

return nil, errors.New("Not found")
}

func checkUser() bool {
return os.Getuid() == 0
}

func login(username, password string) bool {
sp, _ := getspnam(username)
if sp == nil {
return false
}

c := crypt.NewFromHash(sp.sp_pwdp)
return c.Verify(sp.sp_pwdp, []byte(password)) == nil
}

0 comments on commit 6c9c7fe

Please sign in to comment.