summaryrefslogtreecommitdiff
path: root/src/parabola_hackers/password.go
blob: 4ea345fc76d513ce5558a79f67080198b9d44b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2015-2016 Luke Shumaker <lukeshu@sbcglobal.net>.
//
// This is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this manual; if not, see
// <http://www.gnu.org/licenses/>.

package parabola_hackers

import (
	"io/ioutil"
	"lukeshu.com/git/go/libgnulinux.git/crypt"
	"fmt"
	"os"
	"strings"
)

/* Note that the password hash value should be one of:
   <empty> - no password set, allow login without password
   !       - used to prevent logins
   x       - "valid" encrypted password that does not match any valid password
             often used to indicate that the password is defined elsewhere
   other   - encrypted password, in crypt(3) format */

func LoadUserPassword(filename string) (hash string, err error) {
	file, err := os.Open(filename)
	if err != nil {
		return
	}
	contents, err := ioutil.ReadAll(file)
	if err != nil {
		return
	}
	lines := strings.Split(string(contents), "\n")
	switch len(lines) {
	case 1:
		hash = lines[0]
	case 2:
		if lines[1] == "" {
			hash = lines[0]
		} else {
			err = fmt.Errorf("Invalid password format in file: %q", filename)
		}
	default:
		err = fmt.Errorf("Invalid password format in file (number of lines): %q", filename)
		return
	}
	if hash != "!" && !crypt.SaltOk(hash) {
		hash = "!"
		err = fmt.Errorf("Invalid password format in file (invalid salt): %q", filename)
		return
	}
	return
}