2018-01-22 06:13:35 +00:00
|
|
|
// Authentication routines for the HKExSh
|
2018-04-07 20:04:10 +00:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017-2018 Russell Magee
|
|
|
|
// Licensed under the terms of the MIT license (see LICENSE.mit in this
|
|
|
|
// distribution)
|
|
|
|
//
|
|
|
|
// golang implementation by Russ Magee (rmagee_at_gmail.com)
|
2018-01-22 06:13:35 +00:00
|
|
|
|
2018-04-04 22:43:27 +00:00
|
|
|
package hkexsh
|
2018-01-22 06:13:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/csv"
|
2018-09-14 06:51:49 +00:00
|
|
|
"fmt"
|
2018-01-22 06:13:35 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2018-09-14 06:51:49 +00:00
|
|
|
"os/user"
|
2018-01-22 06:13:35 +00:00
|
|
|
"runtime"
|
2018-01-23 21:53:05 +00:00
|
|
|
|
|
|
|
"github.com/jameskeane/bcrypt"
|
2018-01-22 06:13:35 +00:00
|
|
|
)
|
|
|
|
|
2018-09-14 06:51:49 +00:00
|
|
|
func AuthUserByPasswd(username string, auth string, fname string) (valid bool, allowedCmds string) {
|
2018-01-25 02:14:21 +00:00
|
|
|
b, e := ioutil.ReadFile(fname)
|
|
|
|
if e != nil {
|
|
|
|
valid = false
|
2018-02-17 02:46:29 +00:00
|
|
|
log.Println("ERROR: Cannot read hkexsh.passwd file!")
|
2018-01-25 02:14:21 +00:00
|
|
|
log.Fatal(e)
|
|
|
|
}
|
2018-01-22 06:13:35 +00:00
|
|
|
r := csv.NewReader(bytes.NewReader(b))
|
|
|
|
|
|
|
|
r.Comma = ':'
|
|
|
|
r.Comment = '#'
|
2018-09-09 05:01:33 +00:00
|
|
|
r.FieldsPerRecord = 3 // username:salt:authCookie [TODO:disallowedCmdList (a,b,...)]
|
2018-01-22 06:13:35 +00:00
|
|
|
for {
|
|
|
|
record, err := r.Read()
|
|
|
|
if err == io.EOF {
|
2018-09-12 05:36:20 +00:00
|
|
|
// Use dummy entry if user not found
|
|
|
|
// (prevent user enumeration attack via obvious timing diff;
|
|
|
|
// ie., not attempting any auth at all)
|
|
|
|
record = []string{"$nosuchuser$",
|
|
|
|
"$2a$12$l0coBlRDNEJeQVl6GdEPbU",
|
|
|
|
"$2a$12$l0coBlRDNEJeQVl6GdEPbUC/xmuOANvqgmrMVum6S4i.EXPgnTXy6"}
|
|
|
|
username = "$nosuchuser$"
|
|
|
|
err = nil
|
2018-01-22 06:13:35 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:53:05 +00:00
|
|
|
if username == record[0] {
|
|
|
|
tmp, _ := bcrypt.Hash(auth, record[1])
|
2018-09-12 05:36:20 +00:00
|
|
|
if tmp == record[2] && username != "$nosuchuser$" {
|
2018-01-23 21:53:05 +00:00
|
|
|
valid = true
|
|
|
|
}
|
2018-01-22 06:13:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-05-05 06:25:26 +00:00
|
|
|
// Security scrub
|
|
|
|
for i := range b {
|
|
|
|
b[i] = 0
|
|
|
|
}
|
|
|
|
b = nil
|
|
|
|
r = nil
|
|
|
|
runtime.GC()
|
|
|
|
|
2018-01-22 06:13:35 +00:00
|
|
|
return
|
|
|
|
}
|
2018-09-14 06:51:49 +00:00
|
|
|
|
|
|
|
func AuthUserByToken(username string, auth string) (valid bool) {
|
|
|
|
u, ue := user.Lookup(username)
|
|
|
|
if ue != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
b, e := ioutil.ReadFile(fmt.Sprintf("%s/.hkexsh_id", u.HomeDir))
|
|
|
|
if e != nil {
|
|
|
|
log.Printf("INFO: Cannot read %s/.hkexsh_id\n", u.HomeDir)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(b) == auth {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|