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-09-14 18:58:10 +00:00
|
|
|
"strings"
|
2018-01-23 21:53:05 +00:00
|
|
|
|
|
|
|
"github.com/jameskeane/bcrypt"
|
2018-01-22 06:13:35 +00:00
|
|
|
)
|
|
|
|
|
2018-10-04 05:31:35 +00:00
|
|
|
func userExistsOnSystem(who string) bool {
|
|
|
|
_, userErr := user.Lookup(who)
|
2018-11-25 18:24:10 +00:00
|
|
|
return userErr == nil
|
2018-10-04 05:31:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-25 18:24:10 +00:00
|
|
|
// AuthUserByPasswd checks user login information using a password.
|
|
|
|
// This checks /etc/hkexsh.passwd for auth info, and system /etc/passwd
|
|
|
|
// to cross-check the user actually exists.
|
|
|
|
// nolint: gocyclo
|
2018-09-14 06:51:49 +00:00
|
|
|
func AuthUserByPasswd(username string, auth string, fname string) (valid bool, allowedCmds string) {
|
2018-11-25 18:24:10 +00:00
|
|
|
b, e := ioutil.ReadFile(fname) // nolint: gosec
|
2018-01-25 02:14:21 +00:00
|
|
|
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] {
|
2018-11-25 18:24:10 +00:00
|
|
|
tmp, err := bcrypt.Hash(auth, record[1])
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
r = nil
|
|
|
|
runtime.GC()
|
|
|
|
|
2018-10-04 05:31:35 +00:00
|
|
|
if !userExistsOnSystem(username) {
|
|
|
|
valid = false
|
|
|
|
}
|
2018-01-22 06:13:35 +00:00
|
|
|
return
|
|
|
|
}
|
2018-09-14 06:51:49 +00:00
|
|
|
|
2018-11-25 18:24:10 +00:00
|
|
|
// AuthUserByToken checks user login information against an auth token.
|
|
|
|
// Auth tokens are stored in each user's $HOME/.hkexsh_id and are requested
|
|
|
|
// via the -g option.
|
|
|
|
// The function also check system /etc/passwd to cross-check the user
|
|
|
|
// actually exists.
|
2018-09-14 08:13:14 +00:00
|
|
|
func AuthUserByToken(username string, connhostname string, auth string) (valid bool) {
|
2018-09-14 18:58:10 +00:00
|
|
|
auth = strings.TrimSpace(auth)
|
2018-09-14 06:51:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-14 18:58:10 +00:00
|
|
|
r := csv.NewReader(bytes.NewReader(b))
|
|
|
|
|
|
|
|
r.Comma = ':'
|
|
|
|
r.Comment = '#'
|
|
|
|
r.FieldsPerRecord = 2 // connhost:authtoken
|
|
|
|
for {
|
|
|
|
record, err := r.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
record[0] = strings.TrimSpace(record[0])
|
|
|
|
record[1] = strings.TrimSpace(record[1])
|
2018-09-17 00:30:02 +00:00
|
|
|
//fmt.Println("auth:", auth, "record:",
|
|
|
|
// strings.Join([]string{record[0], record[1]}, ":"))
|
2018-09-14 18:58:10 +00:00
|
|
|
|
|
|
|
if (connhostname == record[0]) &&
|
|
|
|
(auth == strings.Join([]string{record[0], record[1]}, ":")) {
|
2018-11-25 18:24:10 +00:00
|
|
|
valid = true
|
|
|
|
break
|
2018-09-14 18:58:10 +00:00
|
|
|
}
|
2018-09-14 06:51:49 +00:00
|
|
|
}
|
2018-10-04 05:31:35 +00:00
|
|
|
if !userExistsOnSystem(username) {
|
|
|
|
valid = false
|
|
|
|
}
|
2018-09-14 06:51:49 +00:00
|
|
|
return
|
|
|
|
}
|