forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_user.go
313 lines (270 loc) · 8.41 KB
/
system_user.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package asserts
import (
"fmt"
"net/mail"
"regexp"
"strconv"
"strings"
"time"
)
var validSystemUserUsernames = regexp.MustCompile(`^[a-z0-9][-a-z0-9+.-_]*$`)
// SystemUser holds a system-user assertion which allows creating local
// system users.
type SystemUser struct {
assertionBase
series []string
models []string
serials []string
sshKeys []string
since time.Time
until time.Time
forcePasswordChange bool
}
// BrandID returns the brand identifier that signed this assertion.
func (su *SystemUser) BrandID() string {
return su.HeaderString("brand-id")
}
// Email returns the email address that this assertion is valid for.
func (su *SystemUser) Email() string {
return su.HeaderString("email")
}
// Series returns the series that this assertion is valid for.
func (su *SystemUser) Series() []string {
return su.series
}
// Models returns the models that this assertion is valid for.
func (su *SystemUser) Models() []string {
return su.models
}
// Serials returns the serials that this assertion is valid for.
func (su *SystemUser) Serials() []string {
return su.serials
}
// Name returns the full name of the user (e.g. Random Guy).
func (su *SystemUser) Name() string {
return su.HeaderString("name")
}
// Username returns the system user name that should be created (e.g. "foo").
func (su *SystemUser) Username() string {
return su.HeaderString("username")
}
// Password returns the crypt(3) compatible password for the user.
// Note that only ID: $6$ or stronger is supported (sha512crypt).
func (su *SystemUser) Password() string {
return su.HeaderString("password")
}
// ForcePasswordChange returns true if the user needs to change the password
// after the first login.
func (su *SystemUser) ForcePasswordChange() bool {
return su.forcePasswordChange
}
// SSHKeys returns the ssh keys for the user.
func (su *SystemUser) SSHKeys() []string {
return su.sshKeys
}
// Since returns the time since the assertion is valid.
func (su *SystemUser) Since() time.Time {
return su.since
}
// Until returns the time until the assertion is valid.
func (su *SystemUser) Until() time.Time {
return su.until
}
// ValidAt returns whether the system-user is valid at 'when' time.
func (su *SystemUser) ValidAt(when time.Time) bool {
valid := when.After(su.since) || when.Equal(su.since)
if valid {
valid = when.Before(su.until)
}
return valid
}
// Implement further consistency checks.
func (su *SystemUser) checkConsistency(db RODatabase, acck *AccountKey) error {
// Do the cross-checks when this assertion is actually used,
// i.e. in the create-user code. See also Model.checkConsitency
return nil
}
// sanity
var _ consistencyChecker = (*SystemUser)(nil)
type shadow struct {
ID string
Rounds string
Salt string
Hash string
}
// crypt(3) compatible hashes have the forms:
// - $id$salt$hash
// - $id$rounds=N$salt$hash
func parseShadowLine(line string) (*shadow, error) {
l := strings.SplitN(line, "$", 5)
if len(l) != 4 && len(l) != 5 {
return nil, fmt.Errorf(`hashed password must be of the form "$integer-id$salt$hash", see crypt(3)`)
}
// if rounds is the second field, the line must consist of 4
if strings.HasPrefix(l[2], "rounds=") && len(l) == 4 {
return nil, fmt.Errorf(`missing hash field`)
}
// shadow line without $rounds=N$
if len(l) == 4 {
return &shadow{
ID: l[1],
Salt: l[2],
Hash: l[3],
}, nil
}
// shadow line with rounds
return &shadow{
ID: l[1],
Rounds: l[2],
Salt: l[3],
Hash: l[4],
}, nil
}
// see crypt(3) for the legal chars
var isValidSaltAndHash = regexp.MustCompile(`^[a-zA-Z0-9./]+$`).MatchString
func checkHashedPassword(headers map[string]interface{}, name string) (string, error) {
pw, err := checkOptionalString(headers, name)
if err != nil {
return "", err
}
// the pw string is optional, so just return if its empty
if pw == "" {
return "", nil
}
// parse the shadow line
shd, err := parseShadowLine(pw)
if err != nil {
return "", fmt.Errorf(`%q header invalid: %s`, name, err)
}
// and verify it
// see crypt(3), ID 6 means SHA-512 (since glibc 2.7)
ID, err := strconv.Atoi(shd.ID)
if err != nil {
return "", fmt.Errorf(`%q header must start with "$integer-id$", got %q`, name, shd.ID)
}
// double check that we only allow modern hashes
if ID < 6 {
return "", fmt.Errorf("%q header only supports $id$ values of 6 (sha512crypt) or higher", name)
}
// the $rounds=N$ part is optional
if strings.HasPrefix(shd.Rounds, "rounds=") {
rounds, err := strconv.Atoi(strings.SplitN(shd.Rounds, "=", 2)[1])
if err != nil {
return "", fmt.Errorf("%q header has invalid number of rounds: %s", name, err)
}
if rounds < 5000 || rounds > 999999999 {
return "", fmt.Errorf("%q header rounds parameter out of bounds: %d", name, rounds)
}
}
if !isValidSaltAndHash(shd.Salt) {
return "", fmt.Errorf("%q header has invalid chars in salt %q", name, shd.Salt)
}
if !isValidSaltAndHash(shd.Hash) {
return "", fmt.Errorf("%q header has invalid chars in hash %q", name, shd.Hash)
}
return pw, nil
}
func assembleSystemUser(assert assertionBase) (Assertion, error) {
// brand-id here can be different from authority-id,
// the code using the assertion must use the policy set
// by the model assertion system-user-authority header
email, err := checkNotEmptyString(assert.headers, "email")
if err != nil {
return nil, err
}
if _, err := mail.ParseAddress(email); err != nil {
return nil, fmt.Errorf(`"email" header must be a RFC 5322 compliant email address: %s`, err)
}
series, err := checkStringList(assert.headers, "series")
if err != nil {
return nil, err
}
models, err := checkStringList(assert.headers, "models")
if err != nil {
return nil, err
}
serials, err := checkStringList(assert.headers, "serials")
if err != nil {
return nil, err
}
if len(serials) > 0 && assert.Format() < 1 {
return nil, fmt.Errorf(`the "serials" header is only supported for format 1 or greater`)
}
if len(serials) > 0 && len(models) != 1 {
return nil, fmt.Errorf(`in the presence of the "serials" header "models" must specify exactly one model`)
}
if _, err := checkOptionalString(assert.headers, "name"); err != nil {
return nil, err
}
if _, err := checkStringMatches(assert.headers, "username", validSystemUserUsernames); err != nil {
return nil, err
}
password, err := checkHashedPassword(assert.headers, "password")
if err != nil {
return nil, err
}
forcePasswordChange, err := checkOptionalBool(assert.headers, "force-password-change")
if err != nil {
return nil, err
}
if forcePasswordChange && password == "" {
return nil, fmt.Errorf(`cannot use "force-password-change" with an empty "password"`)
}
sshKeys, err := checkStringList(assert.headers, "ssh-keys")
if err != nil {
return nil, err
}
since, err := checkRFC3339Date(assert.headers, "since")
if err != nil {
return nil, err
}
until, err := checkRFC3339Date(assert.headers, "until")
if err != nil {
return nil, err
}
if until.Before(since) {
return nil, fmt.Errorf("'until' time cannot be before 'since' time")
}
// "global" system-user assertion can only be valid for 1y
if len(models) == 0 && until.After(since.AddDate(1, 0, 0)) {
return nil, fmt.Errorf("'until' time cannot be more than 365 days in the future when no models are specified")
}
return &SystemUser{
assertionBase: assert,
series: series,
models: models,
serials: serials,
sshKeys: sshKeys,
since: since,
until: until,
forcePasswordChange: forcePasswordChange,
}, nil
}
func systemUserFormatAnalyze(headers map[string]interface{}, body []byte) (formatnum int, err error) {
formatnum = 0
serials, err := checkStringList(headers, "serials")
if err != nil {
return 0, err
}
if len(serials) > 0 {
formatnum = 1
}
return formatnum, nil
}