forked from zhaojh329/rttys
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support login in freebsd by system user/password
Signed-off-by: Jianhui Zhao <[email protected]>
- Loading branch information
Jianhui Zhao
committed
Apr 29, 2019
1 parent
4532711
commit 6c9c7fe
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,6 @@ generate windows 386 | |
|
||
generate darwin amd64 | ||
generate darwin 386 | ||
|
||
generate freebsd amd64 | ||
generate freebsd 386 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |