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

import (
	"fmt"
	"syscall"

	"hakurei.app/container"
	"hakurei.app/hst"
)

// spAccountOp sets up user account emulation inside the container.
type spAccountOp struct {
	// Inner directory to use as the home directory of the emulated user.
	Home *container.Absolute
	// String matching the default NAME_REGEX value from adduser to use as the username of the emulated user.
	Username string
	// Pathname of shell to use for the emulated user.
	Shell *container.Absolute
}

func (s *spAccountOp) toSystem(*outcomeStateSys, *hst.Config) error {
	const fallbackUsername = "chronos"

	// do checks here to fail before fork/exec
	if s.Home == nil || s.Shell == nil {
		// unreachable
		return syscall.ENOTRECOVERABLE
	}
	if s.Username == "" {
		s.Username = fallbackUsername
	} else if !isValidUsername(s.Username) {
		return newWithMessage(fmt.Sprintf("invalid user name %q", s.Username))
	}
	return nil
}

func (s *spAccountOp) toContainer(state *outcomeStateParams) error {
	state.params.Dir = s.Home
	state.env["HOME"] = s.Home.String()
	state.env["USER"] = s.Username
	state.env["SHELL"] = s.Shell.String()

	state.params.
		Place(container.AbsFHSEtc.Append("passwd"),
			[]byte(s.Username+":x:"+
				state.mapuid.String()+":"+
				state.mapgid.String()+
				":Hakurei:"+
				s.Home.String()+":"+
				s.Shell.String()+"\n")).
		Place(container.AbsFHSEtc.Append("group"),
			[]byte("hakurei:x:"+state.mapgid.String()+":\n"))

	return nil
}