forked from u-root/u-root
/
main.go
368 lines (341 loc) · 10.6 KB
/
main.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright 2017-2019 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"crypto/tls"
"crypto/x509"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv6"
"github.com/insomniacslk/dhcp/iana"
"github.com/insomniacslk/dhcp/interfaces"
"github.com/insomniacslk/dhcp/netboot"
"github.com/u-root/u-root/pkg/crypto"
"github.com/u-root/u-root/pkg/kexec"
)
var (
useV4 = flag.Bool("4", false, "Get a DHCPv4 lease")
useV6 = flag.Bool("6", true, "Get a DHCPv6 lease")
ifname = flag.String("i", "", "Interface to send packets through")
dryRun = flag.Bool("dryrun", false, "Do everything except assigning IP addresses, changing DNS, and kexec")
doDebug = flag.Bool("d", false, "Print debug output")
skipDHCP = flag.Bool("skip-dhcp", false, "Skip DHCP and rely on SLAAC for network configuration. This requires -netboot-url")
overrideNetbootURL = flag.String("netboot-url", "", "Override the netboot URL normally obtained via DHCP")
readTimeout = flag.Int("timeout", 3, "Read timeout in seconds")
dhcpRetries = flag.Int("retries", 3, "Number of times a DHCP request is retried")
userClass = flag.String("userclass", "", "Override DHCP User Class option")
caCertFile = flag.String("cacerts", "/etc/cacerts.pem", "CA cert file")
skipCertVerify = flag.Bool("skip-cert-verify", false, "Don't authenticate https certs")
doFix = flag.Bool("fix", false, "Try to run fixmynetboot if netboot fails")
)
const (
interfaceUpTimeout = 10 * time.Second
maxHTTPAttempts = 3
retryInterval = time.Second
)
var banner = `
_________________________________
< Net booting is so hot right now >
---------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
`
var debug = func(string, ...interface{}) {}
func main() {
flag.Parse()
if *skipDHCP && *overrideNetbootURL == "" {
log.Fatal("-skip-dhcp requires -netboot-url")
}
if *doDebug {
debug = log.Printf
}
log.Print(banner)
if !*useV6 && !*useV4 {
log.Fatal("At least one of DHCPv6 and DHCPv4 is required")
}
iflist := []net.Interface{}
if *ifname != "" {
var iface *net.Interface
var err error
if iface, err = net.InterfaceByName(*ifname); err != nil {
log.Fatalf("Could not find interface %s: %v", *ifname, err)
}
iflist = append(iflist, *iface)
} else {
var err error
if iflist, err = interfaces.GetNonLoopbackInterfaces(); err != nil {
log.Fatalf("Could not obtain the list of network interfaces: %v", err)
}
}
for _, iface := range iflist {
log.Printf("Waiting for network interface %s to come up", iface.Name)
start := time.Now()
_, err := netboot.IfUp(iface.Name, interfaceUpTimeout)
if err != nil {
log.Printf("IfUp failed: %v", err)
continue
}
debug("Interface %s is up after %v", iface.Name, time.Since(start))
var dhcp []dhcpFunc
if *useV6 {
dhcp = append(dhcp, dhcp6)
}
if *useV4 {
dhcp = append(dhcp, dhcp4)
}
for _, d := range dhcp {
if err := boot(iface.Name, d); err != nil {
if *doFix {
cmd := exec.Command("fixmynetboot", iface.Name)
log.Printf("Running %s", strings.Join(cmd.Args, " "))
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
log.Printf("Error calling fixmynetboot: %v", err)
log.Print("fixmynetboot failed. Check the above output to manually debug the issue.")
os.Exit(1)
}
}
log.Printf("Could not boot from %s: %v", iface.Name, err)
}
}
}
log.Fatalln("Could not boot from any interfaces")
}
func retryableNetError(err error) bool {
if err == nil {
return false
}
switch err := err.(type) {
case net.Error:
if err.Timeout() {
return true
}
}
return false
}
func retryableHTTPError(resp *http.Response) bool {
if resp == nil {
return false
}
if resp.StatusCode == 500 || resp.StatusCode == 502 {
return true
}
return false
}
func boot(ifname string, dhcp dhcpFunc) error {
var (
netconf *netboot.NetConf
bootfile string
err error
)
if *skipDHCP {
log.Print("Skipping DHCP")
} else {
// send a netboot request via DHCP
netconf, bootfile, err = dhcp(ifname)
if err != nil {
return fmt.Errorf("DHCPv6: netboot request for interface %s failed: %v", ifname, err)
}
debug("DHCP: network configuration: %+v", netconf)
if !*dryRun {
log.Printf("DHCP: configuring network interface %s with %v", ifname, netconf)
if err = netboot.ConfigureInterface(ifname, netconf); err != nil {
return fmt.Errorf("DHCP: cannot configure interface %s: %v", ifname, err)
}
}
if *overrideNetbootURL != "" {
bootfile = *overrideNetbootURL
}
log.Printf("DHCP: boot file for interface %s is %s", ifname, bootfile)
}
if *overrideNetbootURL != "" {
bootfile = *overrideNetbootURL
}
debug("DHCP: boot file URL is %s", bootfile)
// check for supported schemes
scheme, err := getScheme(bootfile)
if err != nil {
return fmt.Errorf("DHCP: cannot get scheme from URL: %v", err)
}
if scheme == "" {
return errors.New("DHCP: no valid scheme found in URL")
}
client, err := getClientForBootfile(bootfile)
if err != nil {
return fmt.Errorf("DHCP: cannot get client for %s: %v", bootfile, err)
}
log.Printf("DHCP: fetching boot file URL: %s", bootfile)
var resp *http.Response
for attempt := 0; attempt < maxHTTPAttempts; attempt++ {
log.Printf("netboot: attempt %d for http.Get", attempt+1)
req, err := http.NewRequest(http.MethodGet, bootfile, nil)
if err != nil {
return fmt.Errorf("could not build request for %s: %v", bootfile, err)
}
resp, err = client.Do(req)
if err != nil && retryableNetError(err) || retryableHTTPError(resp) {
time.Sleep(retryInterval)
continue
}
if err == nil {
break
}
return fmt.Errorf("DHCP: http.Get of %s failed: %v", bootfile, err)
}
// FIXME this will not be called if something fails after this point
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("status code is not 200 OK: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("DHCP: cannot read boot file from the network: %v", err)
}
crypto.TryMeasureData(crypto.BootConfigPCR, body, bootfile)
u, err := url.Parse(bootfile)
if err != nil {
return fmt.Errorf("DHCP: cannot parse URL %s: %v", bootfile, err)
}
// extract file name component
if strings.HasSuffix(u.Path, "/") {
return fmt.Errorf("invalid file path, cannot end with '/': %s", u.Path)
}
filename := filepath.Base(u.Path)
if filename == "." || filename == "" {
return fmt.Errorf("invalid empty file name extracted from file path %s", u.Path)
}
if err = ioutil.WriteFile(filename, body, 0400); err != nil {
return fmt.Errorf("DHCP: cannot write to file %s: %v", filename, err)
}
debug("DHCP: saved boot file to %s", filename)
if !*dryRun {
log.Printf("DHCP: kexec'ing into %s", filename)
kernel, err := os.OpenFile(filename, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("DHCP: cannot open file %s: %v", filename, err)
}
if err = kexec.FileLoad(kernel, nil /* ramfs */, "" /* cmdline */); err != nil {
return fmt.Errorf("DHCP: kexec.FileLoad failed: %v", err)
}
if err = kexec.Reboot(); err != nil {
return fmt.Errorf("DHCP: kexec.Reboot failed: %v", err)
}
}
return nil
}
func getScheme(urlstring string) (string, error) {
u, err := url.Parse(urlstring)
if err != nil {
return "", err
}
scheme := strings.ToLower(u.Scheme)
if scheme != "http" && scheme != "https" {
return "", fmt.Errorf("URL scheme '%s' must be http or https", scheme)
}
return scheme, nil
}
func loadCaCerts() (*x509.CertPool, error) {
rootCAs, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
if rootCAs == nil {
debug("certs: rootCAs == nil")
rootCAs = x509.NewCertPool()
}
caCerts, err := ioutil.ReadFile(*caCertFile)
if err != nil {
return nil, fmt.Errorf("could not find cert file '%v' - %v", *caCertFile, err)
}
// TODO: Decide if this should also support compressed certs
// Might be better to have a generic compressed config API
if ok := rootCAs.AppendCertsFromPEM(caCerts); !ok {
debug("Failed to append CA Certs from %s, using system certs only", *caCertFile)
} else {
debug("CA certs appended from PEM")
}
return rootCAs, nil
}
func getClientForBootfile(bootfile string) (*http.Client, error) {
var client *http.Client
scheme, err := getScheme(bootfile)
if err != nil {
return nil, err
}
switch scheme {
case "https":
var config *tls.Config
if *skipCertVerify {
config = &tls.Config{
InsecureSkipVerify: true,
}
} else if *caCertFile != "" {
rootCAs, err := loadCaCerts()
if err != nil {
return nil, err
}
config = &tls.Config{
RootCAs: rootCAs,
}
}
tr := &http.Transport{TLSClientConfig: config}
client = &http.Client{Transport: tr}
debug("https client setup (use certs from VPD: %t, skipCertVerify %t)",
*skipCertVerify, *caCertFile != "")
case "http":
client = &http.Client{}
debug("http client setup")
default:
return nil, fmt.Errorf("Scheme %s is unsupported", scheme)
}
return client, nil
}
type dhcpFunc func(string) (*netboot.NetConf, string, error)
func dhcp6(ifname string) (*netboot.NetConf, string, error) {
log.Printf("Trying to obtain a DHCPv6 lease on %s", ifname)
modifiers := []dhcpv6.Modifier{
dhcpv6.WithArchType(iana.EFI_X86_64),
}
if *userClass != "" {
modifiers = append(modifiers, dhcpv6.WithUserClass([]byte(*userClass)))
}
conversation, err := netboot.RequestNetbootv6(ifname, time.Duration(*readTimeout)*time.Second, *dhcpRetries, modifiers...)
for _, m := range conversation {
debug(m.Summary())
}
if err != nil {
return nil, "", fmt.Errorf("DHCPv6: netboot request for interface %s failed: %v", ifname, err)
}
return netboot.ConversationToNetconf(conversation)
}
func dhcp4(ifname string) (*netboot.NetConf, string, error) {
log.Printf("Trying to obtain a DHCPv4 lease on %s", ifname)
var modifiers []dhcpv4.Modifier
if *userClass != "" {
modifiers = append(modifiers, dhcpv4.WithUserClass(*userClass, false))
}
conversation, err := netboot.RequestNetbootv4(ifname, time.Duration(*readTimeout)*time.Second, *dhcpRetries, modifiers...)
for _, m := range conversation {
debug(m.Summary())
}
if err != nil {
return nil, "", fmt.Errorf("DHCPv4: netboot request for interface %s failed: %v", ifname, err)
}
return netboot.ConversationToNetconfv4(conversation)
}