forked from alexlance/userd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistro.go
More file actions
142 lines (137 loc) · 5.88 KB
/
Copy pathdistro.go
File metadata and controls
142 lines (137 loc) · 5.88 KB
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os/exec"
"strings"
)
// distroCommands for different flavours of Linux
type distroCommands struct {
addUser func(string, string) ([]byte, error)
delUser func(string) ([]byte, error)
changeShell func(string, string) ([]byte, error)
changePassword func(string, string) ([]byte, error)
changeHomeDir func(string, string) ([]byte, error)
changeGroups func(string, string) ([]byte, error)
changeComment func(string, string) ([]byte, error)
}
// get the short string version of the operating system eg debian:9
func getOS() string {
b, err := ioutil.ReadFile("/etc/os-release")
if err != nil {
log.Fatal(err)
}
s := strings.Split(string(b), "\n")
version := ""
version_id := ""
for _, line := range s {
if bits := strings.Split(line, `=`); len(bits) > 0 {
if bits[0] == "ID" {
version = strings.Replace(bits[1], `"`, ``, -1)
}
if bits[0] == "VERSION_ID" {
version_id = strings.Replace(bits[1], `"`, ``, -1)
}
}
}
if version != "" && version_id != "" {
return fmt.Sprintf("%s:%s", version, version_id)
} else if version != "" {
return version
} else {
return ""
}
}
// return an operating-system specific user management command to run
func getOSCommands(flavour string) distroCommands {
switch strings.ToLower(flavour) {
case "centos:7", "centos:7.4", "centos:7.5", "centos:7.6":
return distroCommands{
addUser: func(username string, home string) ([]byte, error) {
args := []string{"adduser", "-m", "--home-dir", home, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
delUser: func(username string) ([]byte, error) {
// kill any processes the user account may be running, otherwise
// the user account cannot be removed
pgrep := []string{"pgrep", "-l", "-u", username}
if processlist, _ := exec.Command(pgrep[0], pgrep[1:]...).CombinedOutput(); strings.TrimSpace(string(processlist)) != "" {
log.Printf("Found %s processes: %s", username, strings.TrimSpace(strings.Replace(string(processlist), "\n", " ", -1)))
pkill := []string{"pkill", "--signal", "9", "-e", "-u", username}
out, _ := exec.Command(pkill[0], pkill[1:]...).CombinedOutput()
if strings.TrimSpace(string(out)) != "" {
log.Printf("Killed %s processes: %s", username, strings.TrimSpace(strings.Replace(string(out), "\n", " ", -1)))
}
}
args := []string{"userdel", "--remove", "-f", username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
changeShell: func(username string, shell string) ([]byte, error) {
args := []string{"usermod", "--shell", shell, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
changePassword: func(username string, password string) ([]byte, error) {
args := []string{"usermod", "--password", password, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
changeHomeDir: func(username string, home string) ([]byte, error) {
args := []string{"usermod", "--move-home", "--home", home, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
changeGroups: func(username string, groups string) ([]byte, error) {
args := []string{"usermod", "--groups", groups, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
changeComment: func(username string, comment string) ([]byte, error) {
args := []string{"usermod", "--comment", comment, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
}
case "debian", "debian:8", "debian:9", "debian:10", "debian:11", "debian:12", "ubuntu:16.04", "ubuntu:18.04", "ubuntu:18.10", "ubuntu:19.04":
return distroCommands{
addUser: func(username string, home string) ([]byte, error) {
args := []string{"adduser", "--home", home, "--disabled-password", username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
delUser: func(username string) ([]byte, error) {
// kill any processes the user account may be running, otherwise
// the user account cannot be removed
pgrep := []string{"pgrep", "-l", "-u", username}
if processlist, _ := exec.Command(pgrep[0], pgrep[1:]...).CombinedOutput(); strings.TrimSpace(string(processlist)) != "" {
log.Printf("Found %s processes: %s", username, strings.TrimSpace(strings.Replace(string(processlist), "\n", " ", -1)))
pkill := []string{"pkill", "--signal", "9", "-e", "-u", username}
out, _ := exec.Command(pkill[0], pkill[1:]...).CombinedOutput()
if strings.TrimSpace(string(out)) != "" {
log.Printf("Killed %s processes: %s", username, strings.TrimSpace(strings.Replace(string(out), "\n", " ", -1)))
}
}
args := []string{"deluser", "--remove-home", username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
changeShell: func(username string, shell string) ([]byte, error) {
args := []string{"usermod", "--shell", shell, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
changePassword: func(username string, password string) ([]byte, error) {
args := []string{"usermod", "--password", password, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
changeHomeDir: func(username string, home string) ([]byte, error) {
args := []string{"usermod", "--move-home", "--home", home, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
changeGroups: func(username string, groups string) ([]byte, error) {
args := []string{"usermod", "--groups", groups, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
changeComment: func(username string, comment string) ([]byte, error) {
args := []string{"usermod", "--comment", comment, username}
return exec.Command(args[0], args[1:]...).CombinedOutput()
},
}
default:
log.Fatalf("No config for operating system: %s", flavour)
}
return distroCommands{}
}