/
etc.go
195 lines (148 loc) · 3.8 KB
/
etc.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
/**
* SPDX-License-Identifier: Apache-2.0
* Copyright 2020 vorteil.io Pty Ltd
*/
package vorteil
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/google/uuid"
"github.com/rakyll/statik/fs"
)
const (
pathMachineID = "/etc/machine-id"
)
var (
etcFiles = []string{"group", "localtime", "nsswitch.conf", "passwd", "resolv.conf"}
)
func writeEtcFile(baseName, fullName string) error {
fs, err := fs.New()
if err != nil {
return err
}
r, err := fs.Open(fmt.Sprintf("/%s.dat", baseName))
if err != nil {
return err
}
defer r.Close()
contents, err := ioutil.ReadAll(r)
if err != nil {
return err
}
err = ioutil.WriteFile(fullName, contents, 0644)
if err != nil {
return err
}
return nil
}
func generateEtcHosts(hostname string) {
if _, err := os.Stat("/etc/hosts"); os.IsNotExist(err) {
logDebug("file /etc/hosts does not exist, creating")
var str strings.Builder
str.WriteString("127.0.0.1\tlocalhost\n")
str.WriteString(fmt.Sprintf("127.0.0.1\t%s\n", hostname))
str.WriteString("::1\tip6-localhost ip6-loopback\n")
str.WriteString("fe00::0\tip6-localnet\n")
str.WriteString("ff00::0\tip6-mcastprefix\n")
str.WriteString("ff02::1\tip6-allnodes\n")
str.WriteString("ff02::2\tip6-allrouters\n")
logDebug("/etc/hosts:\n%s", str.String())
err = ioutil.WriteFile("/etc/hosts", []byte(str.String()), 0644)
if err != nil {
logError("can not create %s file: %v", "/etc/hosts", err)
}
}
}
func appendUserFile(k, v, user string) {
// check if records exist for uid 1000
e, err := ioutil.ReadFile(k)
if err != nil {
logError("can not read %s", k)
return
}
if !strings.Contains(string(e), fmt.Sprintf("%s:x:1000", user)) {
logDebug("append values to %s", k)
e = append(e, fmt.Sprintf("%s\n", v)...)
ioutil.WriteFile(k, e, 0644)
}
}
func createUserFile(k, v, user string) {
err := ioutil.WriteFile(k, []byte(fmt.Sprintf("%s\n", v)), 0644)
if err != nil {
logError(err.Error())
}
}
func generateEtcMachineID() {
if _, err := os.Stat(pathMachineID); os.IsNotExist(err) {
id, err := uuid.NewRandom()
if err != nil {
logError(err.Error())
return
}
err = ioutil.WriteFile(pathMachineID, []byte(id.String()), 0644)
if err != nil {
logError(err.Error())
return
}
}
}
func addVorteilUserGroup(user string) {
if user == "" {
user = "vorteil"
}
etc := map[string]string{
"/etc/passwd": fmt.Sprintf("root:x:0:0:root:/:/bin/false\n%s:x:1000:1000:%s:/:/bin/false", user, user),
"/etc/group": fmt.Sprintf("root:x:0:root\n%s:x:1000:%s", user, user),
}
for k, v := range etc {
logDebug("checking %s", k)
if _, err := os.Stat(k); err == nil {
appendUserFile(k, v, user)
} else if os.IsNotExist(err) {
createUserFile(k, v, user)
}
}
}
/* etcGenerateFiles creates required files in /etc. The variable
'base' is basically only for testing and should be '/etc' during runtime */
func etcGenerateFiles(hostname, user string) error {
os.MkdirAll("/etc", 0755)
addVorteilUserGroup(user)
for _, f := range etcFiles {
fullName := filepath.Join("/etc", f)
if _, err := os.Stat(fullName); os.IsNotExist(err) {
logDebug("creating file %s", fullName)
writeEtcFile(f, fullName)
}
}
err := genHostnameFile(hostname)
if err != nil {
return err
}
// needs random and takes some time
go generateEtcMachineID()
return nil
}
func genHostnameFile(hostname string) error {
// fqdn to hostname
if strings.IndexByte(hostname, '.') > 0 {
hostname = hostname[0:strings.IndexByte(hostname, '.')]
}
// set hostname (/etc/hostname)
f, err := os.OpenFile("/etc/hostname", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, strings.NewReader(hostname))
if err != nil {
return err
}
// generate /etc/hosts
generateEtcHosts(hostname)
return nil
}