-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtftpd.go
292 lines (252 loc) · 8.36 KB
/
tftpd.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
"github.com/maxiepax/go-via/config"
"github.com/maxiepax/go-via/db"
"github.com/maxiepax/go-via/models"
"github.com/sirupsen/logrus"
"gorm.io/gorm/clause"
"github.com/pin/tftp"
)
func readHandler(conf *config.Config) func(string, io.ReaderFrom) error {
return func(filename string, rf io.ReaderFrom) error {
// get the requesting ip-address and our source address
raddr := rf.(tftp.OutgoingTransfer).RemoteAddr()
laddr := rf.(tftp.RequestPacketInfo).LocalIP()
//strip the port
ip, _, _ := net.SplitHostPort(raddr.String())
//get the object that correlates with the ip
var address models.Address
db.DB.Preload(clause.Associations).First(&address, "ip = ?", ip)
//get the image info that correlates with the pool the ip is in
var image models.Image
db.DB.First(&image, "id = ?", address.Group.ImageID)
logrus.WithFields(logrus.Fields{
"raddr": raddr,
"laddr": laddr,
"filename": filename,
"imageid": image.ID,
"addressid": address.ID,
}).Debug("tftpd")
//if the filename is mboot.efi, we hijack it and serve the mboot.efi file that is part of that specific image, this guarantees that you always get an mboot file that works for the build
switch filename {
case "mboot.efi":
logrus.WithFields(logrus.Fields{
ip: "requesting mboot.efi",
}).Info("tftpd")
logrus.WithFields(logrus.Fields{
"id": address.ID,
"percentage": 10,
"progresstext": "mboot.efi",
}).Info("progress")
filename, _ = mbootPath(image.Path)
address.Progress = 10
address.Progresstext = "mboot.efi"
db.DB.Save(&address)
case "crypto64.efi":
logrus.WithFields(logrus.Fields{
ip: "requesting crypto64.efi",
}).Info("tftpd")
logrus.WithFields(logrus.Fields{
"id": address.ID,
"percentage": 12,
"progresstext": "crypto64.efi",
}).Info("progress")
filename, _ = crypto64Path(image.Path)
address.Progress = 12
address.Progresstext = "crypto64.efi"
db.DB.Save(&address)
case "boot.cfg":
serveBootCfg(filename, address, image, rf, conf)
case "/boot.cfg":
serveBootCfg(filename, address, image, rf, conf)
default:
//if no case matches, chroot to /tftp
if _, err := os.Stat("tftp/" + filename); err == nil {
filename = "tftp/" + filename
logrus.WithFields(logrus.Fields{
"lowercase file": filename,
}).Debug("tftpd")
} else {
dir, file := path.Split(filename)
upperfile := strings.ToUpper(string(file))
filename = "tftp/" + dir + upperfile
logrus.WithFields(logrus.Fields{
"uppercase file": filename,
}).Debug("tftpd")
}
}
// get the filesize to send filelength
fi, err := os.Stat(filename)
if err != nil {
return err
}
//set the filesize so that its advertized.
rf.(tftp.OutgoingTransfer).SetSize(fi.Size())
file, err := os.Open(filename)
if err != nil {
logrus.WithFields(logrus.Fields{
"could not open file": err,
}).Debug("tftpd")
return err
}
n, err := rf.ReadFrom(file)
if err != nil {
logrus.WithFields(logrus.Fields{
"could not read from file": err,
}).Debug("tftpd")
return err
}
logrus.WithFields(logrus.Fields{
"id": address.ID,
"ip": address.IP,
"host": address.Hostname,
"file": filename,
"bytes": n,
}).Info("tftpd")
return nil
}
}
func TFTPd(conf *config.Config) {
s := tftp.NewServer(readHandler(conf), nil)
s.SetTimeout(5 * time.Second) // optional
err := s.ListenAndServe(":69") // blocks until s.Shutdown() is called
if err != nil {
logrus.WithFields(logrus.Fields{
"could not start tftp server:": err,
}).Info("tftpd")
os.Exit(1)
}
}
func mbootPath(imagePath string) (string, error) {
//check these paths if the file exists.
paths := []string{"/EFI/BOOT/BOOTX64.EFI", "/EFI/BOOT/BOOTAA64.EFI", "/MBOOT.EFI", "/mboot.efi", "/efi/boot/bootx64.efi", "/efi/boot/bootaa64.efi"}
for _, v := range paths {
if _, err := os.Stat(imagePath + v); err == nil {
return imagePath + v, nil
}
}
//couldn't find the file
return "", fmt.Errorf("could not locate a mboot.efi")
}
func crypto64Path(imagePath string) (string, error) {
//check these paths if the file exists.
paths := []string{"/EFI/BOOT/CRYPTO64.EFI", "/efi/boot/crypto64.efi"}
for _, v := range paths {
if _, err := os.Stat(imagePath + v); err == nil {
return imagePath + v, nil
}
}
//couldn't find the file
return "", fmt.Errorf("could not locate a crypto64.efi")
}
func serveBootCfg(filename string, address models.Address, image models.Image, rf io.ReaderFrom, conf *config.Config) {
//if the filename is boot.cfg, or /boot.cfg, we serve the boot cfg that belongs to that build. unfortunately, it seems boot.cfg or /boot.cfg varies in builds.
// get the requesting ip-address and our source address
raddr := rf.(tftp.OutgoingTransfer).RemoteAddr()
laddr := rf.(tftp.RequestPacketInfo).LocalIP()
//strip the port
ip, _, _ := net.SplitHostPort(raddr.String())
logrus.WithFields(logrus.Fields{
ip: "requesting boot.cfg",
}).Info("tftpd")
logrus.WithFields(logrus.Fields{
"id": address.ID,
"percentage": 15,
"progresstext": "installation",
}).Info("progress")
address.Progress = 15
address.Progresstext = "installation"
db.DB.Save(&address)
bc, err := ioutil.ReadFile(image.Path + "/BOOT.CFG")
if err != nil {
logrus.Warn(err)
return
}
// strip slashes from paths in file
re := regexp.MustCompile("/")
bc = re.ReplaceAllLiteral(bc, []byte(""))
// add kickstart path to kernelopt
re = regexp.MustCompile("kernelopt=.*")
o := re.Find(bc)
bc = re.ReplaceAllLiteral(bc, append(o, []byte(" ks=https://"+laddr.String()+":"+strconv.Itoa(conf.Port)+"/ks.cfg")...))
// append the mac address of the hardware interface to ensure ks.cfg request comes from the right interface, along with ip, netmask and gateway.
nm := net.CIDRMask(address.Pool.Netmask, 32)
netmask := ipv4MaskString(nm)
re = regexp.MustCompile("kernelopt=.*")
o = re.Find(bc)
bc = re.ReplaceAllLiteral(bc, append(o, []byte(" netdevice="+address.Mac+" ip="+address.IP+" netmask="+netmask+" gateway="+address.Pool.Gateway)...))
// if vlan is configured for the group, append the vlan to kernelopts
if address.Group.Vlan != "" {
re = regexp.MustCompile("kernelopt=.*")
o = re.Find(bc)
bc = re.ReplaceAllLiteral(bc, append(o, []byte(" vlanid="+address.Group.Vlan)...))
}
// load options from the group
options := models.GroupOptions{}
json.Unmarshal(address.Group.Options, &options)
// if autopart is configured for the group, append autopart to kernelopt - https://kb.vmware.com/s/article/77009
/*
if options.AutoPart {
re = regexp.MustCompile("kernelopt=.*")
o = re.Find(bc)
bc = re.ReplaceAllLiteral(bc, append(o, []byte(" autoPartitionOnlyOnceAndSkipSsd=true")...))
}*/
// add allowLegacyCPU=true to kernelopt
if options.AllowLegacyCPU {
re = regexp.MustCompile("kernelopt=.*")
o = re.Find(bc)
bc = re.ReplaceAllLiteral(bc, append(o, []byte(" allowLegacyCPU=true")...))
}
// replace prefix with prefix=foldername
split := strings.Split(image.Path, "/")
re = regexp.MustCompile("prefix=")
o = re.Find(bc)
bc = re.ReplaceAllLiteral(bc, append(o, []byte(split[1])...))
// Make a buffer to read from
buff := bytes.NewBuffer(bc)
// Send the data from the buffer to the client
rf.(tftp.OutgoingTransfer).SetSize(int64(buff.Len()))
n, err := rf.ReadFrom(buff)
if err != nil {
//fmt.Fprintf(os.Stderr, "%v\n", err)
logrus.WithFields(logrus.Fields{
"os.Stderr": err,
}).Debug("tftpd")
return
}
logrus.WithFields(logrus.Fields{
"file": filename,
"bytes": n,
}).Info("tftpd")
//return nil
}
func ipv4MaskString(m []byte) string {
if len(m) != 4 {
panic("ipv4Mask: len must be 4 bytes")
}
return fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3])
}