aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/outcome/spaccount.go
blob: 0e9c602a3bc17f16fe5217c78d32e3bc591c9ae6 (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
package outcome

import (
	"encoding/gob"
	"fmt"
	"syscall"

	"hakurei.app/container/fhs"
	"hakurei.app/internal/validate"
)

func init() { gob.Register(spAccountOp{}) }

// spAccountOp sets up user account emulation inside the container.
type spAccountOp struct{}

func (s spAccountOp) toSystem(state *outcomeStateSys) error {
	// do checks here to fail before fork/exec
	if state.Container == nil || state.Container.Home == nil || state.Container.Shell == nil {
		// unreachable
		return syscall.ENOTRECOVERABLE
	}

	// default is applied in toContainer
	if state.Container.Username != "" && !validate.IsValidUsername(state.Container.Username) {
		return newWithMessage(fmt.Sprintf("invalid user name %q", state.Container.Username))
	}
	return nil
}

func (s spAccountOp) toContainer(state *outcomeStateParams) error {
	const fallbackUsername = "chronos"

	username := state.Container.Username
	if username == "" {
		username = fallbackUsername
	}

	state.params.Dir = state.Container.Home
	state.env["HOME"] = state.Container.Home.String()
	state.env["USER"] = username
	state.env["SHELL"] = state.Container.Shell.String()

	state.params.
		Place(fhs.AbsEtc.Append("passwd"),
			[]byte(username+":x:"+
				state.mapuid.String()+":"+
				state.mapgid.String()+
				":Hakurei:"+
				state.Container.Home.String()+":"+
				state.Container.Shell.String()+"\n")).
		Place(fhs.AbsEtc.Append("group"),
			[]byte("hakurei:x:"+state.mapgid.String()+":\n"))

	return nil
}