| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987 | 
							- package nebula
 
- import (
 
- 	"bytes"
 
- 	"encoding/json"
 
- 	"errors"
 
- 	"flag"
 
- 	"fmt"
 
- 	"net"
 
- 	"net/netip"
 
- 	"os"
 
- 	"reflect"
 
- 	"runtime"
 
- 	"runtime/pprof"
 
- 	"sort"
 
- 	"strconv"
 
- 	"strings"
 
- 	"github.com/sirupsen/logrus"
 
- 	"github.com/slackhq/nebula/config"
 
- 	"github.com/slackhq/nebula/header"
 
- 	"github.com/slackhq/nebula/sshd"
 
- )
 
- type sshListHostMapFlags struct {
 
- 	Json    bool
 
- 	Pretty  bool
 
- 	ByIndex bool
 
- }
 
- type sshPrintCertFlags struct {
 
- 	Json   bool
 
- 	Pretty bool
 
- 	Raw    bool
 
- }
 
- type sshPrintTunnelFlags struct {
 
- 	Pretty bool
 
- }
 
- type sshChangeRemoteFlags struct {
 
- 	Address string
 
- }
 
- type sshCloseTunnelFlags struct {
 
- 	LocalOnly bool
 
- }
 
- type sshCreateTunnelFlags struct {
 
- 	Address string
 
- }
 
- type sshDeviceInfoFlags struct {
 
- 	Json   bool
 
- 	Pretty bool
 
- }
 
- func wireSSHReload(l *logrus.Logger, ssh *sshd.SSHServer, c *config.C) {
 
- 	c.RegisterReloadCallback(func(c *config.C) {
 
- 		if c.GetBool("sshd.enabled", false) {
 
- 			sshRun, err := configSSH(l, ssh, c)
 
- 			if err != nil {
 
- 				l.WithError(err).Error("Failed to reconfigure the sshd")
 
- 				ssh.Stop()
 
- 			}
 
- 			if sshRun != nil {
 
- 				go sshRun()
 
- 			}
 
- 		} else {
 
- 			ssh.Stop()
 
- 		}
 
- 	})
 
- }
 
- // configSSH reads the ssh info out of the passed-in Config and
 
- // updates the passed-in SSHServer. On success, it returns a function
 
- // that callers may invoke to run the configured ssh server. On
 
- // failure, it returns nil, error.
 
- func configSSH(l *logrus.Logger, ssh *sshd.SSHServer, c *config.C) (func(), error) {
 
- 	listen := c.GetString("sshd.listen", "")
 
- 	if listen == "" {
 
- 		return nil, fmt.Errorf("sshd.listen must be provided")
 
- 	}
 
- 	_, port, err := net.SplitHostPort(listen)
 
- 	if err != nil {
 
- 		return nil, fmt.Errorf("invalid sshd.listen address: %s", err)
 
- 	}
 
- 	if port == "22" {
 
- 		return nil, fmt.Errorf("sshd.listen can not use port 22")
 
- 	}
 
- 	hostKeyPathOrKey := c.GetString("sshd.host_key", "")
 
- 	if hostKeyPathOrKey == "" {
 
- 		return nil, fmt.Errorf("sshd.host_key must be provided")
 
- 	}
 
- 	var hostKeyBytes []byte
 
- 	if strings.Contains(hostKeyPathOrKey, "-----BEGIN") {
 
- 		hostKeyBytes = []byte(hostKeyPathOrKey)
 
- 	} else {
 
- 		hostKeyBytes, err = os.ReadFile(hostKeyPathOrKey)
 
- 		if err != nil {
 
- 			return nil, fmt.Errorf("error while loading sshd.host_key file: %s", err)
 
- 		}
 
- 	}
 
- 	err = ssh.SetHostKey(hostKeyBytes)
 
- 	if err != nil {
 
- 		return nil, fmt.Errorf("error while adding sshd.host_key: %s", err)
 
- 	}
 
- 	// Clear existing trusted CAs and authorized keys
 
- 	ssh.ClearTrustedCAs()
 
- 	ssh.ClearAuthorizedKeys()
 
- 	rawCAs := c.GetStringSlice("sshd.trusted_cas", []string{})
 
- 	for _, caAuthorizedKey := range rawCAs {
 
- 		err := ssh.AddTrustedCA(caAuthorizedKey)
 
- 		if err != nil {
 
- 			l.WithError(err).WithField("sshCA", caAuthorizedKey).Warn("SSH CA had an error, ignoring")
 
- 			continue
 
- 		}
 
- 	}
 
- 	rawKeys := c.Get("sshd.authorized_users")
 
- 	keys, ok := rawKeys.([]any)
 
- 	if ok {
 
- 		for _, rk := range keys {
 
- 			kDef, ok := rk.(map[string]any)
 
- 			if !ok {
 
- 				l.WithField("sshKeyConfig", rk).Warn("Authorized user had an error, ignoring")
 
- 				continue
 
- 			}
 
- 			user, ok := kDef["user"].(string)
 
- 			if !ok {
 
- 				l.WithField("sshKeyConfig", rk).Warn("Authorized user is missing the user field")
 
- 				continue
 
- 			}
 
- 			k := kDef["keys"]
 
- 			switch v := k.(type) {
 
- 			case string:
 
- 				err := ssh.AddAuthorizedKey(user, v)
 
- 				if err != nil {
 
- 					l.WithError(err).WithField("sshKeyConfig", rk).WithField("sshKey", v).Warn("Failed to authorize key")
 
- 					continue
 
- 				}
 
- 			case []any:
 
- 				for _, subK := range v {
 
- 					sk, ok := subK.(string)
 
- 					if !ok {
 
- 						l.WithField("sshKeyConfig", rk).WithField("sshKey", subK).Warn("Did not understand ssh key")
 
- 						continue
 
- 					}
 
- 					err := ssh.AddAuthorizedKey(user, sk)
 
- 					if err != nil {
 
- 						l.WithError(err).WithField("sshKeyConfig", sk).Warn("Failed to authorize key")
 
- 						continue
 
- 					}
 
- 				}
 
- 			default:
 
- 				l.WithField("sshKeyConfig", rk).Warn("Authorized user is missing the keys field or was not understood")
 
- 			}
 
- 		}
 
- 	} else {
 
- 		l.Info("no ssh users to authorize")
 
- 	}
 
- 	var runner func()
 
- 	if c.GetBool("sshd.enabled", false) {
 
- 		ssh.Stop()
 
- 		runner = func() {
 
- 			if err := ssh.Run(listen); err != nil {
 
- 				l.WithField("err", err).Warn("Failed to run the SSH server")
 
- 			}
 
- 		}
 
- 	} else {
 
- 		ssh.Stop()
 
- 	}
 
- 	return runner, nil
 
- }
 
- func attachCommands(l *logrus.Logger, c *config.C, ssh *sshd.SSHServer, f *Interface) {
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "list-hostmap",
 
- 		ShortDescription: "List all known previously connected hosts",
 
- 		Flags: func() (*flag.FlagSet, any) {
 
- 			fl := flag.NewFlagSet("", flag.ContinueOnError)
 
- 			s := sshListHostMapFlags{}
 
- 			fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
 
- 			fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
 
- 			fl.BoolVar(&s.ByIndex, "by-index", false, "gets all hosts in the hostmap from the index table")
 
- 			return fl, &s
 
- 		},
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshListHostMap(f.hostMap, fs, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "list-pending-hostmap",
 
- 		ShortDescription: "List all handshaking hosts",
 
- 		Flags: func() (*flag.FlagSet, any) {
 
- 			fl := flag.NewFlagSet("", flag.ContinueOnError)
 
- 			s := sshListHostMapFlags{}
 
- 			fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
 
- 			fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
 
- 			fl.BoolVar(&s.ByIndex, "by-index", false, "gets all hosts in the hostmap from the index table")
 
- 			return fl, &s
 
- 		},
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshListHostMap(f.handshakeManager, fs, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "list-lighthouse-addrmap",
 
- 		ShortDescription: "List all lighthouse map entries",
 
- 		Flags: func() (*flag.FlagSet, any) {
 
- 			fl := flag.NewFlagSet("", flag.ContinueOnError)
 
- 			s := sshListHostMapFlags{}
 
- 			fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
 
- 			fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
 
- 			return fl, &s
 
- 		},
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshListLighthouseMap(f.lightHouse, fs, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "reload",
 
- 		ShortDescription: "Reloads configuration from disk, same as sending HUP to the process",
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshReload(c, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "start-cpu-profile",
 
- 		ShortDescription: "Starts a cpu profile and write output to the provided file, ex: `cpu-profile.pb.gz`",
 
- 		Callback:         sshStartCpuProfile,
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "stop-cpu-profile",
 
- 		ShortDescription: "Stops a cpu profile and writes output to the previously provided file",
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			pprof.StopCPUProfile()
 
- 			return w.WriteLine("If a CPU profile was running it is now stopped")
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "save-heap-profile",
 
- 		ShortDescription: "Saves a heap profile to the provided path, ex: `heap-profile.pb.gz`",
 
- 		Callback:         sshGetHeapProfile,
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "mutex-profile-fraction",
 
- 		ShortDescription: "Gets or sets runtime.SetMutexProfileFraction",
 
- 		Callback:         sshMutexProfileFraction,
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "save-mutex-profile",
 
- 		ShortDescription: "Saves a mutex profile to the provided path, ex: `mutex-profile.pb.gz`",
 
- 		Callback:         sshGetMutexProfile,
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "log-level",
 
- 		ShortDescription: "Gets or sets the current log level",
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshLogLevel(l, fs, a, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "log-format",
 
- 		ShortDescription: "Gets or sets the current log format",
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshLogFormat(l, fs, a, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "version",
 
- 		ShortDescription: "Prints the currently running version of nebula",
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshVersion(f, fs, a, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "device-info",
 
- 		ShortDescription: "Prints information about the network device.",
 
- 		Flags: func() (*flag.FlagSet, any) {
 
- 			fl := flag.NewFlagSet("", flag.ContinueOnError)
 
- 			s := sshDeviceInfoFlags{}
 
- 			fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
 
- 			fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
 
- 			return fl, &s
 
- 		},
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshDeviceInfo(f, fs, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "print-cert",
 
- 		ShortDescription: "Prints the current certificate being used or the certificate for the provided vpn addr",
 
- 		Flags: func() (*flag.FlagSet, any) {
 
- 			fl := flag.NewFlagSet("", flag.ContinueOnError)
 
- 			s := sshPrintCertFlags{}
 
- 			fl.BoolVar(&s.Json, "json", false, "outputs as json")
 
- 			fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
 
- 			fl.BoolVar(&s.Raw, "raw", false, "raw prints the PEM encoded certificate, not compatible with -json or -pretty")
 
- 			return fl, &s
 
- 		},
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshPrintCert(f, fs, a, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "print-tunnel",
 
- 		ShortDescription: "Prints json details about a tunnel for the provided vpn addr",
 
- 		Flags: func() (*flag.FlagSet, any) {
 
- 			fl := flag.NewFlagSet("", flag.ContinueOnError)
 
- 			s := sshPrintTunnelFlags{}
 
- 			fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
 
- 			return fl, &s
 
- 		},
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshPrintTunnel(f, fs, a, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "print-relays",
 
- 		ShortDescription: "Prints json details about all relay info",
 
- 		Flags: func() (*flag.FlagSet, any) {
 
- 			fl := flag.NewFlagSet("", flag.ContinueOnError)
 
- 			s := sshPrintTunnelFlags{}
 
- 			fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
 
- 			return fl, &s
 
- 		},
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshPrintRelays(f, fs, a, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "change-remote",
 
- 		ShortDescription: "Changes the remote address used in the tunnel for the provided vpn addr",
 
- 		Flags: func() (*flag.FlagSet, any) {
 
- 			fl := flag.NewFlagSet("", flag.ContinueOnError)
 
- 			s := sshChangeRemoteFlags{}
 
- 			fl.StringVar(&s.Address, "address", "", "The new remote address, ip:port")
 
- 			return fl, &s
 
- 		},
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshChangeRemote(f, fs, a, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "close-tunnel",
 
- 		ShortDescription: "Closes a tunnel for the provided vpn addr",
 
- 		Flags: func() (*flag.FlagSet, any) {
 
- 			fl := flag.NewFlagSet("", flag.ContinueOnError)
 
- 			s := sshCloseTunnelFlags{}
 
- 			fl.BoolVar(&s.LocalOnly, "local-only", false, "Disables notifying the remote that the tunnel is shutting down")
 
- 			return fl, &s
 
- 		},
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshCloseTunnel(f, fs, a, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "create-tunnel",
 
- 		ShortDescription: "Creates a tunnel for the provided vpn address",
 
- 		Help:             "The lighthouses will be queried for real addresses but you can provide one as well.",
 
- 		Flags: func() (*flag.FlagSet, any) {
 
- 			fl := flag.NewFlagSet("", flag.ContinueOnError)
 
- 			s := sshCreateTunnelFlags{}
 
- 			fl.StringVar(&s.Address, "address", "", "Optionally provide a real remote address, ip:port ")
 
- 			return fl, &s
 
- 		},
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshCreateTunnel(f, fs, a, w)
 
- 		},
 
- 	})
 
- 	ssh.RegisterCommand(&sshd.Command{
 
- 		Name:             "query-lighthouse",
 
- 		ShortDescription: "Query the lighthouses for the provided vpn address",
 
- 		Help:             "This command is asynchronous. Only currently known udp addresses will be printed.",
 
- 		Callback: func(fs any, a []string, w sshd.StringWriter) error {
 
- 			return sshQueryLighthouse(f, fs, a, w)
 
- 		},
 
- 	})
 
- }
 
- func sshListHostMap(hl controlHostLister, a any, w sshd.StringWriter) error {
 
- 	fs, ok := a.(*sshListHostMapFlags)
 
- 	if !ok {
 
- 		return nil
 
- 	}
 
- 	var hm []ControlHostInfo
 
- 	if fs.ByIndex {
 
- 		hm = listHostMapIndexes(hl)
 
- 	} else {
 
- 		hm = listHostMapHosts(hl)
 
- 	}
 
- 	sort.Slice(hm, func(i, j int) bool {
 
- 		return hm[i].VpnAddrs[0].Compare(hm[j].VpnAddrs[0]) < 0
 
- 	})
 
- 	if fs.Json || fs.Pretty {
 
- 		js := json.NewEncoder(w.GetWriter())
 
- 		if fs.Pretty {
 
- 			js.SetIndent("", "    ")
 
- 		}
 
- 		err := js.Encode(hm)
 
- 		if err != nil {
 
- 			return nil
 
- 		}
 
- 	} else {
 
- 		for _, v := range hm {
 
- 			err := w.WriteLine(fmt.Sprintf("%s: %s", v.VpnAddrs, v.RemoteAddrs))
 
- 			if err != nil {
 
- 				return err
 
- 			}
 
- 		}
 
- 	}
 
- 	return nil
 
- }
 
- func sshListLighthouseMap(lightHouse *LightHouse, a any, w sshd.StringWriter) error {
 
- 	fs, ok := a.(*sshListHostMapFlags)
 
- 	if !ok {
 
- 		return nil
 
- 	}
 
- 	type lighthouseInfo struct {
 
- 		VpnAddr string    `json:"vpnAddr"`
 
- 		Addrs   *CacheMap `json:"addrs"`
 
- 	}
 
- 	lightHouse.RLock()
 
- 	addrMap := make([]lighthouseInfo, len(lightHouse.addrMap))
 
- 	x := 0
 
- 	for k, v := range lightHouse.addrMap {
 
- 		addrMap[x] = lighthouseInfo{
 
- 			VpnAddr: k.String(),
 
- 			Addrs:   v.CopyCache(),
 
- 		}
 
- 		x++
 
- 	}
 
- 	lightHouse.RUnlock()
 
- 	sort.Slice(addrMap, func(i, j int) bool {
 
- 		return strings.Compare(addrMap[i].VpnAddr, addrMap[j].VpnAddr) < 0
 
- 	})
 
- 	if fs.Json || fs.Pretty {
 
- 		js := json.NewEncoder(w.GetWriter())
 
- 		if fs.Pretty {
 
- 			js.SetIndent("", "    ")
 
- 		}
 
- 		err := js.Encode(addrMap)
 
- 		if err != nil {
 
- 			return nil
 
- 		}
 
- 	} else {
 
- 		for _, v := range addrMap {
 
- 			b, err := json.Marshal(v.Addrs)
 
- 			if err != nil {
 
- 				return err
 
- 			}
 
- 			err = w.WriteLine(fmt.Sprintf("%s: %s", v.VpnAddr, string(b)))
 
- 			if err != nil {
 
- 				return err
 
- 			}
 
- 		}
 
- 	}
 
- 	return nil
 
- }
 
- func sshStartCpuProfile(fs any, a []string, w sshd.StringWriter) error {
 
- 	if len(a) == 0 {
 
- 		err := w.WriteLine("No path to write profile provided")
 
- 		return err
 
- 	}
 
- 	file, err := os.Create(a[0])
 
- 	if err != nil {
 
- 		err = w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
 
- 		return err
 
- 	}
 
- 	err = pprof.StartCPUProfile(file)
 
- 	if err != nil {
 
- 		err = w.WriteLine(fmt.Sprintf("Unable to start cpu profile: %s", err))
 
- 		return err
 
- 	}
 
- 	err = w.WriteLine(fmt.Sprintf("Started cpu profile, issue stop-cpu-profile to write the output to %s", a))
 
- 	return err
 
- }
 
- func sshVersion(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
 
- 	return w.WriteLine(fmt.Sprintf("%s", ifce.version))
 
- }
 
- func sshQueryLighthouse(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
 
- 	if len(a) == 0 {
 
- 		return w.WriteLine("No vpn address was provided")
 
- 	}
 
- 	vpnAddr, err := netip.ParseAddr(a[0])
 
- 	if err != nil {
 
- 		return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
 
- 	}
 
- 	if !vpnAddr.IsValid() {
 
- 		return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
 
- 	}
 
- 	var cm *CacheMap
 
- 	rl := ifce.lightHouse.Query(vpnAddr)
 
- 	if rl != nil {
 
- 		cm = rl.CopyCache()
 
- 	}
 
- 	return json.NewEncoder(w.GetWriter()).Encode(cm)
 
- }
 
- func sshCloseTunnel(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
 
- 	flags, ok := fs.(*sshCloseTunnelFlags)
 
- 	if !ok {
 
- 		return nil
 
- 	}
 
- 	if len(a) == 0 {
 
- 		return w.WriteLine("No vpn address was provided")
 
- 	}
 
- 	vpnAddr, err := netip.ParseAddr(a[0])
 
- 	if err != nil {
 
- 		return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
 
- 	}
 
- 	if !vpnAddr.IsValid() {
 
- 		return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
 
- 	}
 
- 	hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
 
- 	if hostInfo == nil {
 
- 		return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn address: %v", a[0]))
 
- 	}
 
- 	if !flags.LocalOnly {
 
- 		ifce.send(
 
- 			header.CloseTunnel,
 
- 			0,
 
- 			hostInfo.ConnectionState,
 
- 			hostInfo,
 
- 			[]byte{},
 
- 			make([]byte, 12, 12),
 
- 			make([]byte, mtu),
 
- 		)
 
- 	}
 
- 	ifce.closeTunnel(hostInfo)
 
- 	return w.WriteLine("Closed")
 
- }
 
- func sshCreateTunnel(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
 
- 	flags, ok := fs.(*sshCreateTunnelFlags)
 
- 	if !ok {
 
- 		return nil
 
- 	}
 
- 	if len(a) == 0 {
 
- 		return w.WriteLine("No vpn address was provided")
 
- 	}
 
- 	vpnAddr, err := netip.ParseAddr(a[0])
 
- 	if err != nil {
 
- 		return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
 
- 	}
 
- 	if !vpnAddr.IsValid() {
 
- 		return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
 
- 	}
 
- 	hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
 
- 	if hostInfo != nil {
 
- 		return w.WriteLine(fmt.Sprintf("Tunnel already exists"))
 
- 	}
 
- 	hostInfo = ifce.handshakeManager.QueryVpnAddr(vpnAddr)
 
- 	if hostInfo != nil {
 
- 		return w.WriteLine(fmt.Sprintf("Tunnel already handshaking"))
 
- 	}
 
- 	var addr netip.AddrPort
 
- 	if flags.Address != "" {
 
- 		addr, err = netip.ParseAddrPort(flags.Address)
 
- 		if err != nil {
 
- 			return w.WriteLine("Address could not be parsed")
 
- 		}
 
- 	}
 
- 	hostInfo = ifce.handshakeManager.StartHandshake(vpnAddr, nil)
 
- 	if addr.IsValid() {
 
- 		hostInfo.SetRemote(addr)
 
- 	}
 
- 	return w.WriteLine("Created")
 
- }
 
- func sshChangeRemote(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
 
- 	flags, ok := fs.(*sshChangeRemoteFlags)
 
- 	if !ok {
 
- 		return nil
 
- 	}
 
- 	if len(a) == 0 {
 
- 		return w.WriteLine("No vpn address was provided")
 
- 	}
 
- 	if flags.Address == "" {
 
- 		return w.WriteLine("No address was provided")
 
- 	}
 
- 	addr, err := netip.ParseAddrPort(flags.Address)
 
- 	if err != nil {
 
- 		return w.WriteLine("Address could not be parsed")
 
- 	}
 
- 	vpnAddr, err := netip.ParseAddr(a[0])
 
- 	if err != nil {
 
- 		return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
 
- 	}
 
- 	if !vpnAddr.IsValid() {
 
- 		return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
 
- 	}
 
- 	hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
 
- 	if hostInfo == nil {
 
- 		return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn address: %v", a[0]))
 
- 	}
 
- 	hostInfo.SetRemote(addr)
 
- 	return w.WriteLine("Changed")
 
- }
 
- func sshGetHeapProfile(fs any, a []string, w sshd.StringWriter) error {
 
- 	if len(a) == 0 {
 
- 		return w.WriteLine("No path to write profile provided")
 
- 	}
 
- 	file, err := os.Create(a[0])
 
- 	if err != nil {
 
- 		err = w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
 
- 		return err
 
- 	}
 
- 	err = pprof.WriteHeapProfile(file)
 
- 	if err != nil {
 
- 		err = w.WriteLine(fmt.Sprintf("Unable to write profile: %s", err))
 
- 		return err
 
- 	}
 
- 	err = w.WriteLine(fmt.Sprintf("Mem profile created at %s", a))
 
- 	return err
 
- }
 
- func sshMutexProfileFraction(fs any, a []string, w sshd.StringWriter) error {
 
- 	if len(a) == 0 {
 
- 		rate := runtime.SetMutexProfileFraction(-1)
 
- 		return w.WriteLine(fmt.Sprintf("Current value: %d", rate))
 
- 	}
 
- 	newRate, err := strconv.Atoi(a[0])
 
- 	if err != nil {
 
- 		return w.WriteLine(fmt.Sprintf("Invalid argument: %s", a[0]))
 
- 	}
 
- 	oldRate := runtime.SetMutexProfileFraction(newRate)
 
- 	return w.WriteLine(fmt.Sprintf("New value: %d. Old value: %d", newRate, oldRate))
 
- }
 
- func sshGetMutexProfile(fs any, a []string, w sshd.StringWriter) error {
 
- 	if len(a) == 0 {
 
- 		return w.WriteLine("No path to write profile provided")
 
- 	}
 
- 	file, err := os.Create(a[0])
 
- 	if err != nil {
 
- 		return w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
 
- 	}
 
- 	defer file.Close()
 
- 	mutexProfile := pprof.Lookup("mutex")
 
- 	if mutexProfile == nil {
 
- 		return w.WriteLine("Unable to get pprof.Lookup(\"mutex\")")
 
- 	}
 
- 	err = mutexProfile.WriteTo(file, 0)
 
- 	if err != nil {
 
- 		return w.WriteLine(fmt.Sprintf("Unable to write profile: %s", err))
 
- 	}
 
- 	return w.WriteLine(fmt.Sprintf("Mutex profile created at %s", a))
 
- }
 
- func sshLogLevel(l *logrus.Logger, fs any, a []string, w sshd.StringWriter) error {
 
- 	if len(a) == 0 {
 
- 		return w.WriteLine(fmt.Sprintf("Log level is: %s", l.Level))
 
- 	}
 
- 	level, err := logrus.ParseLevel(a[0])
 
- 	if err != nil {
 
- 		return w.WriteLine(fmt.Sprintf("Unknown log level %s. Possible log levels: %s", a, logrus.AllLevels))
 
- 	}
 
- 	l.SetLevel(level)
 
- 	return w.WriteLine(fmt.Sprintf("Log level is: %s", l.Level))
 
- }
 
- func sshLogFormat(l *logrus.Logger, fs any, a []string, w sshd.StringWriter) error {
 
- 	if len(a) == 0 {
 
- 		return w.WriteLine(fmt.Sprintf("Log format is: %s", reflect.TypeOf(l.Formatter)))
 
- 	}
 
- 	logFormat := strings.ToLower(a[0])
 
- 	switch logFormat {
 
- 	case "text":
 
- 		l.Formatter = &logrus.TextFormatter{}
 
- 	case "json":
 
- 		l.Formatter = &logrus.JSONFormatter{}
 
- 	default:
 
- 		return fmt.Errorf("unknown log format `%s`. possible formats: %s", logFormat, []string{"text", "json"})
 
- 	}
 
- 	return w.WriteLine(fmt.Sprintf("Log format is: %s", reflect.TypeOf(l.Formatter)))
 
- }
 
- func sshPrintCert(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
 
- 	args, ok := fs.(*sshPrintCertFlags)
 
- 	if !ok {
 
- 		return nil
 
- 	}
 
- 	cert := ifce.pki.getCertState().GetDefaultCertificate()
 
- 	if len(a) > 0 {
 
- 		vpnAddr, err := netip.ParseAddr(a[0])
 
- 		if err != nil {
 
- 			return w.WriteLine(fmt.Sprintf("The provided vpn addr could not be parsed: %s", a[0]))
 
- 		}
 
- 		if !vpnAddr.IsValid() {
 
- 			return w.WriteLine(fmt.Sprintf("The provided vpn addr could not be parsed: %s", a[0]))
 
- 		}
 
- 		hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
 
- 		if hostInfo == nil {
 
- 			return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn addr: %v", a[0]))
 
- 		}
 
- 		cert = hostInfo.GetCert().Certificate
 
- 	}
 
- 	if args.Json || args.Pretty {
 
- 		b, err := cert.MarshalJSON()
 
- 		if err != nil {
 
- 			return nil
 
- 		}
 
- 		if args.Pretty {
 
- 			buf := new(bytes.Buffer)
 
- 			err := json.Indent(buf, b, "", "    ")
 
- 			b = buf.Bytes()
 
- 			if err != nil {
 
- 				return nil
 
- 			}
 
- 		}
 
- 		return w.WriteBytes(b)
 
- 	}
 
- 	if args.Raw {
 
- 		b, err := cert.MarshalPEM()
 
- 		if err != nil {
 
- 			return nil
 
- 		}
 
- 		return w.WriteBytes(b)
 
- 	}
 
- 	return w.WriteLine(cert.String())
 
- }
 
- func sshPrintRelays(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
 
- 	args, ok := fs.(*sshPrintTunnelFlags)
 
- 	if !ok {
 
- 		w.WriteLine(fmt.Sprintf("sshPrintRelays failed to convert args type"))
 
- 		return nil
 
- 	}
 
- 	relays := map[uint32]*HostInfo{}
 
- 	ifce.hostMap.Lock()
 
- 	for k, v := range ifce.hostMap.Relays {
 
- 		relays[k] = v
 
- 	}
 
- 	ifce.hostMap.Unlock()
 
- 	type RelayFor struct {
 
- 		Error          error
 
- 		Type           string
 
- 		State          string
 
- 		PeerAddr       netip.Addr
 
- 		LocalIndex     uint32
 
- 		RemoteIndex    uint32
 
- 		RelayedThrough []netip.Addr
 
- 	}
 
- 	type RelayOutput struct {
 
- 		NebulaAddr    netip.Addr
 
- 		RelayForAddrs []RelayFor
 
- 	}
 
- 	type CmdOutput struct {
 
- 		Relays []*RelayOutput
 
- 	}
 
- 	co := CmdOutput{}
 
- 	enc := json.NewEncoder(w.GetWriter())
 
- 	if args.Pretty {
 
- 		enc.SetIndent("", "    ")
 
- 	}
 
- 	for k, v := range relays {
 
- 		ro := RelayOutput{NebulaAddr: v.vpnAddrs[0]}
 
- 		co.Relays = append(co.Relays, &ro)
 
- 		relayHI := ifce.hostMap.QueryVpnAddr(v.vpnAddrs[0])
 
- 		if relayHI == nil {
 
- 			ro.RelayForAddrs = append(ro.RelayForAddrs, RelayFor{Error: errors.New("could not find hostinfo")})
 
- 			continue
 
- 		}
 
- 		for _, vpnAddr := range relayHI.relayState.CopyRelayForIps() {
 
- 			rf := RelayFor{Error: nil}
 
- 			r, ok := relayHI.relayState.GetRelayForByAddr(vpnAddr)
 
- 			if ok {
 
- 				t := ""
 
- 				switch r.Type {
 
- 				case ForwardingType:
 
- 					t = "forwarding"
 
- 				case TerminalType:
 
- 					t = "terminal"
 
- 				default:
 
- 					t = "unknown"
 
- 				}
 
- 				s := ""
 
- 				switch r.State {
 
- 				case Requested:
 
- 					s = "requested"
 
- 				case Established:
 
- 					s = "established"
 
- 				default:
 
- 					s = "unknown"
 
- 				}
 
- 				rf.LocalIndex = r.LocalIndex
 
- 				rf.RemoteIndex = r.RemoteIndex
 
- 				rf.PeerAddr = r.PeerAddr
 
- 				rf.Type = t
 
- 				rf.State = s
 
- 				if rf.LocalIndex != k {
 
- 					rf.Error = fmt.Errorf("hostmap LocalIndex '%v' does not match RelayState LocalIndex", k)
 
- 				}
 
- 			}
 
- 			relayedHI := ifce.hostMap.QueryVpnAddr(vpnAddr)
 
- 			if relayedHI != nil {
 
- 				rf.RelayedThrough = append(rf.RelayedThrough, relayedHI.relayState.CopyRelayIps()...)
 
- 			}
 
- 			ro.RelayForAddrs = append(ro.RelayForAddrs, rf)
 
- 		}
 
- 	}
 
- 	err := enc.Encode(co)
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	return nil
 
- }
 
- func sshPrintTunnel(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
 
- 	args, ok := fs.(*sshPrintTunnelFlags)
 
- 	if !ok {
 
- 		return nil
 
- 	}
 
- 	if len(a) == 0 {
 
- 		return w.WriteLine("No vpn address was provided")
 
- 	}
 
- 	vpnAddr, err := netip.ParseAddr(a[0])
 
- 	if err != nil {
 
- 		return w.WriteLine(fmt.Sprintf("The provided vpn addr could not be parsed: %s", a[0]))
 
- 	}
 
- 	if !vpnAddr.IsValid() {
 
- 		return w.WriteLine(fmt.Sprintf("The provided vpn addr could not be parsed: %s", a[0]))
 
- 	}
 
- 	hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
 
- 	if hostInfo == nil {
 
- 		return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn addr: %v", a[0]))
 
- 	}
 
- 	enc := json.NewEncoder(w.GetWriter())
 
- 	if args.Pretty {
 
- 		enc.SetIndent("", "    ")
 
- 	}
 
- 	return enc.Encode(copyHostInfo(hostInfo, ifce.hostMap.GetPreferredRanges()))
 
- }
 
- func sshDeviceInfo(ifce *Interface, fs any, w sshd.StringWriter) error {
 
- 	data := struct {
 
- 		Name string         `json:"name"`
 
- 		Cidr []netip.Prefix `json:"cidr"`
 
- 	}{
 
- 		Name: ifce.inside.Name(),
 
- 		Cidr: make([]netip.Prefix, len(ifce.inside.Networks())),
 
- 	}
 
- 	copy(data.Cidr, ifce.inside.Networks())
 
- 	flags, ok := fs.(*sshDeviceInfoFlags)
 
- 	if !ok {
 
- 		return fmt.Errorf("internal error: expected flags to be sshDeviceInfoFlags but was %+v", fs)
 
- 	}
 
- 	if flags.Json || flags.Pretty {
 
- 		js := json.NewEncoder(w.GetWriter())
 
- 		if flags.Pretty {
 
- 			js.SetIndent("", "    ")
 
- 		}
 
- 		return js.Encode(data)
 
- 	} else {
 
- 		return w.WriteLine(fmt.Sprintf("name=%v cidr=%v", data.Name, data.Cidr))
 
- 	}
 
- }
 
- func sshReload(c *config.C, w sshd.StringWriter) error {
 
- 	err := w.WriteLine("Reloading config")
 
- 	c.ReloadConfig()
 
- 	return err
 
- }
 
 
  |