This repository has been archived by the owner on Jul 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.go
157 lines (130 loc) · 3.07 KB
/
operations.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
package system
import (
"bufio"
"bytes"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
copier "github.com/otiai10/copy"
)
type Interface interface {
// Create home directory for new user
CreateHomeDir(dir string, uid, gid int) error
CreateAuthorizedKeysFile(homeDir string, principal string) error
FixChown(homeDir string, uid, gid int) error
// Recursively delete home directory if exists.
DeleteHomeDir(dir string) error
// Kill all user processes.
PurgeUserLegacy(username string) error
}
type SystemOperator struct {
dryRun bool
}
func NewSystemOperator() *SystemOperator {
dryRun := os.Getenv("USER_DRY_RUN")
return &SystemOperator{
dryRun: dryRun == "yes",
}
}
func (s *SystemOperator) CreateHomeDir(dir string, uid, gid int) error {
if s.dryRun {
fmt.Printf("Create dir '%s' for %d:%d\n", dir, uid, gid)
return nil
}
_, err := os.Stat(dir)
if os.IsNotExist(err) {
err := copier.Copy("/etc/skel", dir)
if err != nil {
return err
}
err = os.Chown(dir, uid, gid)
if err != nil {
return err
}
} else if err != nil {
return err
}
err = os.Chown(dir, uid, gid)
if err != nil {
return err
}
return nil
}
func (s *SystemOperator) DeleteHomeDir(dir string) error {
if s.dryRun {
fmt.Printf("Delete dir '%s'\n", dir)
return nil
}
return os.RemoveAll(dir)
}
func (s *SystemOperator) PurgeUserLegacy(username string) error {
if s.dryRun {
fmt.Printf("Purge user legacy for '%s'\n", username)
return nil
}
// we can't parse /var/run/utmp directly, since its serialization format is platform-dependent
cmd := exec.Command("who", "-u")
out, err := cmd.Output()
if err != nil {
return err
}
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) != 7 {
return fmt.Errorf(`"who" command output doesn't have 7 fields: %q`, line)
}
pidToKill, err := strconv.Atoi(fields[6])
if err != nil {
return err
}
if fields[0] == username {
process, err := os.FindProcess(pidToKill)
if err != nil {
return nil
}
err = process.Kill()
if err != nil {
return nil
}
}
}
return nil
}
func (s *SystemOperator) CreateAuthorizedKeysFile(homeDir string, principal string) error {
content := GenerateAuthorizedKeysFile(principal, "")
sshDir := filepath.Join(homeDir, ".ssh")
filePath := filepath.Join(sshDir, "authorized_keys")
err := CreateAuthorizedKeysFile(filePath, content)
if err != nil {
return err
}
err = os.MkdirAll(sshDir, 0700)
if err != nil {
return fmt.Errorf("create .ssh dir: %v")
}
err = os.Chmod(filePath, 0600)
if err != nil {
return fmt.Errorf("change .ssh/authorized_keys mode: %v")
}
return nil
}
func (s *SystemOperator) FixChown(homeDir string, uid, gid int) error {
err := os.Chown(homeDir, uid, gid)
if err != nil {
return err
}
err = filepath.Walk(homeDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
// prevent panic by handling failure accessing a path
return err
}
return os.Chown(path, uid, gid)
})
return err
}