summaryrefslogtreecommitdiff
path: root/src/nshd/hackers_git/hackers_parse.go
blob: d5370ebb4fa04e22fd6bd0bfab75d7889b0962cb (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2015 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.
//
// The GNU General Public License's references to "object code" and
// "executables" are to be interpreted to also include the output of
// any document formatting or typesetting system, including
// intermediate and printed output.
//
// 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 hackers_git

import (
	"fmt"
	yaml "gopkg.in/yaml.v2"
	"io/ioutil"
	"lukeshu.com/git/go/libsystemd.git/sd_daemon/logger"
	"os"
	"path"
	"strconv"
	"strings"
)

func filename2uid(filename string) int32 {
	basename := path.Base(filename)
	parts := strings.SplitN(basename, ".", 2)
	if len(parts) != 2 || parts[1] != "yml" {
		return -1
	}
	uid, err := strconv.ParseInt(parts[0], 10, 32)
	if err != nil {
		return -1
	}
	return int32(uid)
}

var usersGid = name2gid("users")

func parse_user_yaml(filename string) (ret user, err error) {
	ret.passwd.UID = filename2uid(filename)

	if ret.passwd.UID < 0 {
		err = fmt.Errorf("Invalid user filename: %q", filename)
		return
	}
	file, err := os.Open(filename)
	if err != nil {
		return
	}
	contents, err := ioutil.ReadAll(file)
	if err != nil {
		return
	}
	var _data interface{}
	err = yaml.Unmarshal(contents, &_data)
	if err != nil {
		return
	}
	data, isMap := _data.(map[interface{}]interface{})
	errs := []string{}
	if !isMap {
		errs = append(errs, "root node is not a map")
	} else {
		if iface, isSet := data["username"]; !isSet {
			errs = append(errs, "\"username\" is not set")
		} else if str, isTyp := iface.(string); !isTyp {
			errs = append(errs, "\"username\" is not a string")
		} else {
			ret.passwd.Name = str
			ret.passwd.HomeDir = "/home/" + str
		}

		if iface, isSet := data["fullname"]; !isSet {
			errs = append(errs, "\"fullname\" is not set")
		} else if str, isTyp := iface.(string); !isTyp {
			errs = append(errs, "\"fullname\" is not a string")
		} else {
			ret.passwd.GECOS = str
		}

		if iface, isSet := data["shell"]; !isSet {
			errs = append(errs, "\"shell\" is not set")
		} else if str, isTyp := iface.(string); !isTyp {
			errs = append(errs, "\"shell\" is not a string")
		} else {
			ret.passwd.Shell = str
		}

		if iface, isSet := data["groups"]; !isSet {
			ret.groups = make([]string, 0)
		} else if ary, isTyp := iface.([]interface{}); !isTyp {
			errs = append(errs, "\"groups\" is not an array")
		} else {
			groups := make(map[string]bool, len(ary))
			e := false
			for _, iface := range ary {
				if str, isTyp := iface.(string); !isTyp {
					errs = append(errs, "\"group\" item is not an array")
					e = true
					break
				} else {
					groups[str] = true
				}
			}
			if !e {
				ret.groups = set2list(groups)
			}
		}
	}
	if len(errs) > 0 {
		err = &yaml.TypeError{Errors: errs}
	}

	ret.passwd.PwHash = "!"
	ret.passwd.GID = usersGid

	return
}

func parse_user_password(filename string) (hash string) {
	hash = "!"
	file, err := os.Open(filename)
	if err != nil {
		logger.Debug("hackers.git: %v", err)
		return
	}
	contents, err := ioutil.ReadAll(file)
	if err != nil {
		logger.Debug("hackers.git: Error while reading: %q: %v", filename, err)
		return
	}
	lines := strings.Split(string(contents), "\n")
	switch len(lines) {
	case 1:
		hash = lines[0]
	case 2:
		if lines[1] == "" {
			hash = lines[0]
		} else {
			logger.Debug("hackers.git: Invalid password format in file: %q", filename)
		}
	default:
		logger.Debug("hackers.git: Invalid password format in file: %q", filename)
	}
	return
}