ssh.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. package nebula
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "flag"
  7. "fmt"
  8. "net"
  9. "os"
  10. "reflect"
  11. "runtime"
  12. "runtime/pprof"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "github.com/sirupsen/logrus"
  17. "github.com/slackhq/nebula/config"
  18. "github.com/slackhq/nebula/header"
  19. "github.com/slackhq/nebula/iputil"
  20. "github.com/slackhq/nebula/sshd"
  21. "github.com/slackhq/nebula/udp"
  22. )
  23. type sshListHostMapFlags struct {
  24. Json bool
  25. Pretty bool
  26. ByIndex bool
  27. }
  28. type sshPrintCertFlags struct {
  29. Json bool
  30. Pretty bool
  31. Raw bool
  32. }
  33. type sshPrintTunnelFlags struct {
  34. Pretty bool
  35. }
  36. type sshChangeRemoteFlags struct {
  37. Address string
  38. }
  39. type sshCloseTunnelFlags struct {
  40. LocalOnly bool
  41. }
  42. type sshCreateTunnelFlags struct {
  43. Address string
  44. }
  45. type sshDeviceInfoFlags struct {
  46. Json bool
  47. Pretty bool
  48. }
  49. func wireSSHReload(l *logrus.Logger, ssh *sshd.SSHServer, c *config.C) {
  50. c.RegisterReloadCallback(func(c *config.C) {
  51. if c.GetBool("sshd.enabled", false) {
  52. sshRun, err := configSSH(l, ssh, c)
  53. if err != nil {
  54. l.WithError(err).Error("Failed to reconfigure the sshd")
  55. ssh.Stop()
  56. }
  57. if sshRun != nil {
  58. go sshRun()
  59. }
  60. } else {
  61. ssh.Stop()
  62. }
  63. })
  64. }
  65. // configSSH reads the ssh info out of the passed-in Config and
  66. // updates the passed-in SSHServer. On success, it returns a function
  67. // that callers may invoke to run the configured ssh server. On
  68. // failure, it returns nil, error.
  69. func configSSH(l *logrus.Logger, ssh *sshd.SSHServer, c *config.C) (func(), error) {
  70. //TODO conntrack list
  71. //TODO print firewall rules or hash?
  72. listen := c.GetString("sshd.listen", "")
  73. if listen == "" {
  74. return nil, fmt.Errorf("sshd.listen must be provided")
  75. }
  76. _, port, err := net.SplitHostPort(listen)
  77. if err != nil {
  78. return nil, fmt.Errorf("invalid sshd.listen address: %s", err)
  79. }
  80. if port == "22" {
  81. return nil, fmt.Errorf("sshd.listen can not use port 22")
  82. }
  83. //TODO: no good way to reload this right now
  84. hostKeyPathOrKey := c.GetString("sshd.host_key", "")
  85. if hostKeyPathOrKey == "" {
  86. return nil, fmt.Errorf("sshd.host_key must be provided")
  87. }
  88. var hostKeyBytes []byte
  89. if strings.Contains(hostKeyPathOrKey, "-----BEGIN") {
  90. hostKeyBytes = []byte(hostKeyPathOrKey)
  91. } else {
  92. hostKeyBytes, err = os.ReadFile(hostKeyPathOrKey)
  93. if err != nil {
  94. return nil, fmt.Errorf("error while loading sshd.host_key file: %s", err)
  95. }
  96. }
  97. err = ssh.SetHostKey(hostKeyBytes)
  98. if err != nil {
  99. return nil, fmt.Errorf("error while adding sshd.host_key: %s", err)
  100. }
  101. rawKeys := c.Get("sshd.authorized_users")
  102. keys, ok := rawKeys.([]interface{})
  103. if ok {
  104. for _, rk := range keys {
  105. kDef, ok := rk.(map[interface{}]interface{})
  106. if !ok {
  107. l.WithField("sshKeyConfig", rk).Warn("Authorized user had an error, ignoring")
  108. continue
  109. }
  110. user, ok := kDef["user"].(string)
  111. if !ok {
  112. l.WithField("sshKeyConfig", rk).Warn("Authorized user is missing the user field")
  113. continue
  114. }
  115. k := kDef["keys"]
  116. switch v := k.(type) {
  117. case string:
  118. err := ssh.AddAuthorizedKey(user, v)
  119. if err != nil {
  120. l.WithError(err).WithField("sshKeyConfig", rk).WithField("sshKey", v).Warn("Failed to authorize key")
  121. continue
  122. }
  123. case []interface{}:
  124. for _, subK := range v {
  125. sk, ok := subK.(string)
  126. if !ok {
  127. l.WithField("sshKeyConfig", rk).WithField("sshKey", subK).Warn("Did not understand ssh key")
  128. continue
  129. }
  130. err := ssh.AddAuthorizedKey(user, sk)
  131. if err != nil {
  132. l.WithError(err).WithField("sshKeyConfig", sk).Warn("Failed to authorize key")
  133. continue
  134. }
  135. }
  136. default:
  137. l.WithField("sshKeyConfig", rk).Warn("Authorized user is missing the keys field or was not understood")
  138. }
  139. }
  140. } else {
  141. l.Info("no ssh users to authorize")
  142. }
  143. var runner func()
  144. if c.GetBool("sshd.enabled", false) {
  145. ssh.Stop()
  146. runner = func() {
  147. if err := ssh.Run(listen); err != nil {
  148. l.WithField("err", err).Warn("Failed to run the SSH server")
  149. }
  150. }
  151. } else {
  152. ssh.Stop()
  153. }
  154. return runner, nil
  155. }
  156. func attachCommands(l *logrus.Logger, c *config.C, ssh *sshd.SSHServer, f *Interface) {
  157. ssh.RegisterCommand(&sshd.Command{
  158. Name: "list-hostmap",
  159. ShortDescription: "List all known previously connected hosts",
  160. Flags: func() (*flag.FlagSet, interface{}) {
  161. fl := flag.NewFlagSet("", flag.ContinueOnError)
  162. s := sshListHostMapFlags{}
  163. fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
  164. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
  165. fl.BoolVar(&s.ByIndex, "by-index", false, "gets all hosts in the hostmap from the index table")
  166. return fl, &s
  167. },
  168. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  169. return sshListHostMap(f.hostMap, fs, w)
  170. },
  171. })
  172. ssh.RegisterCommand(&sshd.Command{
  173. Name: "list-pending-hostmap",
  174. ShortDescription: "List all handshaking hosts",
  175. Flags: func() (*flag.FlagSet, interface{}) {
  176. fl := flag.NewFlagSet("", flag.ContinueOnError)
  177. s := sshListHostMapFlags{}
  178. fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
  179. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
  180. fl.BoolVar(&s.ByIndex, "by-index", false, "gets all hosts in the hostmap from the index table")
  181. return fl, &s
  182. },
  183. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  184. return sshListHostMap(f.handshakeManager, fs, w)
  185. },
  186. })
  187. ssh.RegisterCommand(&sshd.Command{
  188. Name: "list-lighthouse-addrmap",
  189. ShortDescription: "List all lighthouse map entries",
  190. Flags: func() (*flag.FlagSet, interface{}) {
  191. fl := flag.NewFlagSet("", flag.ContinueOnError)
  192. s := sshListHostMapFlags{}
  193. fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
  194. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
  195. return fl, &s
  196. },
  197. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  198. return sshListLighthouseMap(f.lightHouse, fs, w)
  199. },
  200. })
  201. ssh.RegisterCommand(&sshd.Command{
  202. Name: "reload",
  203. ShortDescription: "Reloads configuration from disk, same as sending HUP to the process",
  204. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  205. return sshReload(c, w)
  206. },
  207. })
  208. ssh.RegisterCommand(&sshd.Command{
  209. Name: "start-cpu-profile",
  210. ShortDescription: "Starts a cpu profile and write output to the provided file, ex: `cpu-profile.pb.gz`",
  211. Callback: sshStartCpuProfile,
  212. })
  213. ssh.RegisterCommand(&sshd.Command{
  214. Name: "stop-cpu-profile",
  215. ShortDescription: "Stops a cpu profile and writes output to the previously provided file",
  216. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  217. pprof.StopCPUProfile()
  218. return w.WriteLine("If a CPU profile was running it is now stopped")
  219. },
  220. })
  221. ssh.RegisterCommand(&sshd.Command{
  222. Name: "save-heap-profile",
  223. ShortDescription: "Saves a heap profile to the provided path, ex: `heap-profile.pb.gz`",
  224. Callback: sshGetHeapProfile,
  225. })
  226. ssh.RegisterCommand(&sshd.Command{
  227. Name: "mutex-profile-fraction",
  228. ShortDescription: "Gets or sets runtime.SetMutexProfileFraction",
  229. Callback: sshMutexProfileFraction,
  230. })
  231. ssh.RegisterCommand(&sshd.Command{
  232. Name: "save-mutex-profile",
  233. ShortDescription: "Saves a mutex profile to the provided path, ex: `mutex-profile.pb.gz`",
  234. Callback: sshGetMutexProfile,
  235. })
  236. ssh.RegisterCommand(&sshd.Command{
  237. Name: "log-level",
  238. ShortDescription: "Gets or sets the current log level",
  239. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  240. return sshLogLevel(l, fs, a, w)
  241. },
  242. })
  243. ssh.RegisterCommand(&sshd.Command{
  244. Name: "log-format",
  245. ShortDescription: "Gets or sets the current log format",
  246. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  247. return sshLogFormat(l, fs, a, w)
  248. },
  249. })
  250. ssh.RegisterCommand(&sshd.Command{
  251. Name: "version",
  252. ShortDescription: "Prints the currently running version of nebula",
  253. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  254. return sshVersion(f, fs, a, w)
  255. },
  256. })
  257. ssh.RegisterCommand(&sshd.Command{
  258. Name: "device-info",
  259. ShortDescription: "Prints information about the network device.",
  260. Flags: func() (*flag.FlagSet, interface{}) {
  261. fl := flag.NewFlagSet("", flag.ContinueOnError)
  262. s := sshDeviceInfoFlags{}
  263. fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
  264. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
  265. return fl, &s
  266. },
  267. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  268. return sshDeviceInfo(f, fs, w)
  269. },
  270. })
  271. ssh.RegisterCommand(&sshd.Command{
  272. Name: "print-cert",
  273. ShortDescription: "Prints the current certificate being used or the certificate for the provided vpn ip",
  274. Flags: func() (*flag.FlagSet, interface{}) {
  275. fl := flag.NewFlagSet("", flag.ContinueOnError)
  276. s := sshPrintCertFlags{}
  277. fl.BoolVar(&s.Json, "json", false, "outputs as json")
  278. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
  279. fl.BoolVar(&s.Raw, "raw", false, "raw prints the PEM encoded certificate, not compatible with -json or -pretty")
  280. return fl, &s
  281. },
  282. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  283. return sshPrintCert(f, fs, a, w)
  284. },
  285. })
  286. ssh.RegisterCommand(&sshd.Command{
  287. Name: "print-tunnel",
  288. ShortDescription: "Prints json details about a tunnel for the provided vpn ip",
  289. Flags: func() (*flag.FlagSet, interface{}) {
  290. fl := flag.NewFlagSet("", flag.ContinueOnError)
  291. s := sshPrintTunnelFlags{}
  292. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
  293. return fl, &s
  294. },
  295. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  296. return sshPrintTunnel(f, fs, a, w)
  297. },
  298. })
  299. ssh.RegisterCommand(&sshd.Command{
  300. Name: "print-relays",
  301. ShortDescription: "Prints json details about all relay info",
  302. Flags: func() (*flag.FlagSet, interface{}) {
  303. fl := flag.NewFlagSet("", flag.ContinueOnError)
  304. s := sshPrintTunnelFlags{}
  305. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
  306. return fl, &s
  307. },
  308. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  309. return sshPrintRelays(f, fs, a, w)
  310. },
  311. })
  312. ssh.RegisterCommand(&sshd.Command{
  313. Name: "change-remote",
  314. ShortDescription: "Changes the remote address used in the tunnel for the provided vpn ip",
  315. Flags: func() (*flag.FlagSet, interface{}) {
  316. fl := flag.NewFlagSet("", flag.ContinueOnError)
  317. s := sshChangeRemoteFlags{}
  318. fl.StringVar(&s.Address, "address", "", "The new remote address, ip:port")
  319. return fl, &s
  320. },
  321. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  322. return sshChangeRemote(f, fs, a, w)
  323. },
  324. })
  325. ssh.RegisterCommand(&sshd.Command{
  326. Name: "close-tunnel",
  327. ShortDescription: "Closes a tunnel for the provided vpn ip",
  328. Flags: func() (*flag.FlagSet, interface{}) {
  329. fl := flag.NewFlagSet("", flag.ContinueOnError)
  330. s := sshCloseTunnelFlags{}
  331. fl.BoolVar(&s.LocalOnly, "local-only", false, "Disables notifying the remote that the tunnel is shutting down")
  332. return fl, &s
  333. },
  334. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  335. return sshCloseTunnel(f, fs, a, w)
  336. },
  337. })
  338. ssh.RegisterCommand(&sshd.Command{
  339. Name: "create-tunnel",
  340. ShortDescription: "Creates a tunnel for the provided vpn ip and address",
  341. Help: "The lighthouses will be queried for real addresses but you can provide one as well.",
  342. Flags: func() (*flag.FlagSet, interface{}) {
  343. fl := flag.NewFlagSet("", flag.ContinueOnError)
  344. s := sshCreateTunnelFlags{}
  345. fl.StringVar(&s.Address, "address", "", "Optionally provide a real remote address, ip:port ")
  346. return fl, &s
  347. },
  348. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  349. return sshCreateTunnel(f, fs, a, w)
  350. },
  351. })
  352. ssh.RegisterCommand(&sshd.Command{
  353. Name: "query-lighthouse",
  354. ShortDescription: "Query the lighthouses for the provided vpn ip",
  355. Help: "This command is asynchronous. Only currently known udp ips will be printed.",
  356. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  357. return sshQueryLighthouse(f, fs, a, w)
  358. },
  359. })
  360. }
  361. func sshListHostMap(hl controlHostLister, a interface{}, w sshd.StringWriter) error {
  362. fs, ok := a.(*sshListHostMapFlags)
  363. if !ok {
  364. //TODO: error
  365. return nil
  366. }
  367. var hm []ControlHostInfo
  368. if fs.ByIndex {
  369. hm = listHostMapIndexes(hl)
  370. } else {
  371. hm = listHostMapHosts(hl)
  372. }
  373. sort.Slice(hm, func(i, j int) bool {
  374. return bytes.Compare(hm[i].VpnIp, hm[j].VpnIp) < 0
  375. })
  376. if fs.Json || fs.Pretty {
  377. js := json.NewEncoder(w.GetWriter())
  378. if fs.Pretty {
  379. js.SetIndent("", " ")
  380. }
  381. err := js.Encode(hm)
  382. if err != nil {
  383. //TODO
  384. return nil
  385. }
  386. } else {
  387. for _, v := range hm {
  388. err := w.WriteLine(fmt.Sprintf("%s: %s", v.VpnIp, v.RemoteAddrs))
  389. if err != nil {
  390. return err
  391. }
  392. }
  393. }
  394. return nil
  395. }
  396. func sshListLighthouseMap(lightHouse *LightHouse, a interface{}, w sshd.StringWriter) error {
  397. fs, ok := a.(*sshListHostMapFlags)
  398. if !ok {
  399. //TODO: error
  400. return nil
  401. }
  402. type lighthouseInfo struct {
  403. VpnIp string `json:"vpnIp"`
  404. Addrs *CacheMap `json:"addrs"`
  405. }
  406. lightHouse.RLock()
  407. addrMap := make([]lighthouseInfo, len(lightHouse.addrMap))
  408. x := 0
  409. for k, v := range lightHouse.addrMap {
  410. addrMap[x] = lighthouseInfo{
  411. VpnIp: k.String(),
  412. Addrs: v.CopyCache(),
  413. }
  414. x++
  415. }
  416. lightHouse.RUnlock()
  417. sort.Slice(addrMap, func(i, j int) bool {
  418. return strings.Compare(addrMap[i].VpnIp, addrMap[j].VpnIp) < 0
  419. })
  420. if fs.Json || fs.Pretty {
  421. js := json.NewEncoder(w.GetWriter())
  422. if fs.Pretty {
  423. js.SetIndent("", " ")
  424. }
  425. err := js.Encode(addrMap)
  426. if err != nil {
  427. //TODO
  428. return nil
  429. }
  430. } else {
  431. for _, v := range addrMap {
  432. b, err := json.Marshal(v.Addrs)
  433. if err != nil {
  434. return err
  435. }
  436. err = w.WriteLine(fmt.Sprintf("%s: %s", v.VpnIp, string(b)))
  437. if err != nil {
  438. return err
  439. }
  440. }
  441. }
  442. return nil
  443. }
  444. func sshStartCpuProfile(fs interface{}, a []string, w sshd.StringWriter) error {
  445. if len(a) == 0 {
  446. err := w.WriteLine("No path to write profile provided")
  447. return err
  448. }
  449. file, err := os.Create(a[0])
  450. if err != nil {
  451. err = w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
  452. return err
  453. }
  454. err = pprof.StartCPUProfile(file)
  455. if err != nil {
  456. err = w.WriteLine(fmt.Sprintf("Unable to start cpu profile: %s", err))
  457. return err
  458. }
  459. err = w.WriteLine(fmt.Sprintf("Started cpu profile, issue stop-cpu-profile to write the output to %s", a))
  460. return err
  461. }
  462. func sshVersion(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  463. return w.WriteLine(fmt.Sprintf("%s", ifce.version))
  464. }
  465. func sshQueryLighthouse(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  466. if len(a) == 0 {
  467. return w.WriteLine("No vpn ip was provided")
  468. }
  469. parsedIp := net.ParseIP(a[0])
  470. if parsedIp == nil {
  471. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  472. }
  473. vpnIp := iputil.Ip2VpnIp(parsedIp)
  474. if vpnIp == 0 {
  475. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  476. }
  477. var cm *CacheMap
  478. rl := ifce.lightHouse.Query(vpnIp)
  479. if rl != nil {
  480. cm = rl.CopyCache()
  481. }
  482. return json.NewEncoder(w.GetWriter()).Encode(cm)
  483. }
  484. func sshCloseTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  485. flags, ok := fs.(*sshCloseTunnelFlags)
  486. if !ok {
  487. //TODO: error
  488. return nil
  489. }
  490. if len(a) == 0 {
  491. return w.WriteLine("No vpn ip was provided")
  492. }
  493. parsedIp := net.ParseIP(a[0])
  494. if parsedIp == nil {
  495. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  496. }
  497. vpnIp := iputil.Ip2VpnIp(parsedIp)
  498. if vpnIp == 0 {
  499. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  500. }
  501. hostInfo := ifce.hostMap.QueryVpnIp(vpnIp)
  502. if hostInfo == nil {
  503. return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
  504. }
  505. if !flags.LocalOnly {
  506. ifce.send(
  507. header.CloseTunnel,
  508. 0,
  509. hostInfo.ConnectionState,
  510. hostInfo,
  511. []byte{},
  512. make([]byte, 12, 12),
  513. make([]byte, mtu),
  514. )
  515. }
  516. ifce.closeTunnel(hostInfo)
  517. return w.WriteLine("Closed")
  518. }
  519. func sshCreateTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  520. flags, ok := fs.(*sshCreateTunnelFlags)
  521. if !ok {
  522. //TODO: error
  523. return nil
  524. }
  525. if len(a) == 0 {
  526. return w.WriteLine("No vpn ip was provided")
  527. }
  528. parsedIp := net.ParseIP(a[0])
  529. if parsedIp == nil {
  530. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  531. }
  532. vpnIp := iputil.Ip2VpnIp(parsedIp)
  533. if vpnIp == 0 {
  534. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  535. }
  536. hostInfo := ifce.hostMap.QueryVpnIp(vpnIp)
  537. if hostInfo != nil {
  538. return w.WriteLine(fmt.Sprintf("Tunnel already exists"))
  539. }
  540. hostInfo = ifce.handshakeManager.QueryVpnIp(vpnIp)
  541. if hostInfo != nil {
  542. return w.WriteLine(fmt.Sprintf("Tunnel already handshaking"))
  543. }
  544. var addr *udp.Addr
  545. if flags.Address != "" {
  546. addr = udp.NewAddrFromString(flags.Address)
  547. if addr == nil {
  548. return w.WriteLine("Address could not be parsed")
  549. }
  550. }
  551. hostInfo = ifce.handshakeManager.StartHandshake(vpnIp, nil)
  552. if addr != nil {
  553. hostInfo.SetRemote(addr)
  554. }
  555. return w.WriteLine("Created")
  556. }
  557. func sshChangeRemote(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  558. flags, ok := fs.(*sshChangeRemoteFlags)
  559. if !ok {
  560. //TODO: error
  561. return nil
  562. }
  563. if len(a) == 0 {
  564. return w.WriteLine("No vpn ip was provided")
  565. }
  566. if flags.Address == "" {
  567. return w.WriteLine("No address was provided")
  568. }
  569. addr := udp.NewAddrFromString(flags.Address)
  570. if addr == nil {
  571. return w.WriteLine("Address could not be parsed")
  572. }
  573. parsedIp := net.ParseIP(a[0])
  574. if parsedIp == nil {
  575. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  576. }
  577. vpnIp := iputil.Ip2VpnIp(parsedIp)
  578. if vpnIp == 0 {
  579. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  580. }
  581. hostInfo := ifce.hostMap.QueryVpnIp(vpnIp)
  582. if hostInfo == nil {
  583. return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
  584. }
  585. hostInfo.SetRemote(addr)
  586. return w.WriteLine("Changed")
  587. }
  588. func sshGetHeapProfile(fs interface{}, a []string, w sshd.StringWriter) error {
  589. if len(a) == 0 {
  590. return w.WriteLine("No path to write profile provided")
  591. }
  592. file, err := os.Create(a[0])
  593. if err != nil {
  594. err = w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
  595. return err
  596. }
  597. err = pprof.WriteHeapProfile(file)
  598. if err != nil {
  599. err = w.WriteLine(fmt.Sprintf("Unable to write profile: %s", err))
  600. return err
  601. }
  602. err = w.WriteLine(fmt.Sprintf("Mem profile created at %s", a))
  603. return err
  604. }
  605. func sshMutexProfileFraction(fs interface{}, a []string, w sshd.StringWriter) error {
  606. if len(a) == 0 {
  607. rate := runtime.SetMutexProfileFraction(-1)
  608. return w.WriteLine(fmt.Sprintf("Current value: %d", rate))
  609. }
  610. newRate, err := strconv.Atoi(a[0])
  611. if err != nil {
  612. return w.WriteLine(fmt.Sprintf("Invalid argument: %s", a[0]))
  613. }
  614. oldRate := runtime.SetMutexProfileFraction(newRate)
  615. return w.WriteLine(fmt.Sprintf("New value: %d. Old value: %d", newRate, oldRate))
  616. }
  617. func sshGetMutexProfile(fs interface{}, a []string, w sshd.StringWriter) error {
  618. if len(a) == 0 {
  619. return w.WriteLine("No path to write profile provided")
  620. }
  621. file, err := os.Create(a[0])
  622. if err != nil {
  623. return w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
  624. }
  625. defer file.Close()
  626. mutexProfile := pprof.Lookup("mutex")
  627. if mutexProfile == nil {
  628. return w.WriteLine("Unable to get pprof.Lookup(\"mutex\")")
  629. }
  630. err = mutexProfile.WriteTo(file, 0)
  631. if err != nil {
  632. return w.WriteLine(fmt.Sprintf("Unable to write profile: %s", err))
  633. }
  634. return w.WriteLine(fmt.Sprintf("Mutex profile created at %s", a))
  635. }
  636. func sshLogLevel(l *logrus.Logger, fs interface{}, a []string, w sshd.StringWriter) error {
  637. if len(a) == 0 {
  638. return w.WriteLine(fmt.Sprintf("Log level is: %s", l.Level))
  639. }
  640. level, err := logrus.ParseLevel(a[0])
  641. if err != nil {
  642. return w.WriteLine(fmt.Sprintf("Unknown log level %s. Possible log levels: %s", a, logrus.AllLevels))
  643. }
  644. l.SetLevel(level)
  645. return w.WriteLine(fmt.Sprintf("Log level is: %s", l.Level))
  646. }
  647. func sshLogFormat(l *logrus.Logger, fs interface{}, a []string, w sshd.StringWriter) error {
  648. if len(a) == 0 {
  649. return w.WriteLine(fmt.Sprintf("Log format is: %s", reflect.TypeOf(l.Formatter)))
  650. }
  651. logFormat := strings.ToLower(a[0])
  652. switch logFormat {
  653. case "text":
  654. l.Formatter = &logrus.TextFormatter{}
  655. case "json":
  656. l.Formatter = &logrus.JSONFormatter{}
  657. default:
  658. return fmt.Errorf("unknown log format `%s`. possible formats: %s", logFormat, []string{"text", "json"})
  659. }
  660. return w.WriteLine(fmt.Sprintf("Log format is: %s", reflect.TypeOf(l.Formatter)))
  661. }
  662. func sshPrintCert(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  663. args, ok := fs.(*sshPrintCertFlags)
  664. if !ok {
  665. //TODO: error
  666. return nil
  667. }
  668. cert := ifce.pki.GetCertState().Certificate
  669. if len(a) > 0 {
  670. parsedIp := net.ParseIP(a[0])
  671. if parsedIp == nil {
  672. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  673. }
  674. vpnIp := iputil.Ip2VpnIp(parsedIp)
  675. if vpnIp == 0 {
  676. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  677. }
  678. hostInfo := ifce.hostMap.QueryVpnIp(vpnIp)
  679. if hostInfo == nil {
  680. return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
  681. }
  682. cert = hostInfo.GetCert()
  683. }
  684. if args.Json || args.Pretty {
  685. b, err := cert.MarshalJSON()
  686. if err != nil {
  687. //TODO: handle it
  688. return nil
  689. }
  690. if args.Pretty {
  691. buf := new(bytes.Buffer)
  692. err := json.Indent(buf, b, "", " ")
  693. b = buf.Bytes()
  694. if err != nil {
  695. //TODO: handle it
  696. return nil
  697. }
  698. }
  699. return w.WriteBytes(b)
  700. }
  701. if args.Raw {
  702. b, err := cert.MarshalToPEM()
  703. if err != nil {
  704. //TODO: handle it
  705. return nil
  706. }
  707. return w.WriteBytes(b)
  708. }
  709. return w.WriteLine(cert.String())
  710. }
  711. func sshPrintRelays(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  712. args, ok := fs.(*sshPrintTunnelFlags)
  713. if !ok {
  714. //TODO: error
  715. w.WriteLine(fmt.Sprintf("sshPrintRelays failed to convert args type"))
  716. return nil
  717. }
  718. relays := map[uint32]*HostInfo{}
  719. ifce.hostMap.Lock()
  720. for k, v := range ifce.hostMap.Relays {
  721. relays[k] = v
  722. }
  723. ifce.hostMap.Unlock()
  724. type RelayFor struct {
  725. Error error
  726. Type string
  727. State string
  728. PeerIp iputil.VpnIp
  729. LocalIndex uint32
  730. RemoteIndex uint32
  731. RelayedThrough []iputil.VpnIp
  732. }
  733. type RelayOutput struct {
  734. NebulaIp iputil.VpnIp
  735. RelayForIps []RelayFor
  736. }
  737. type CmdOutput struct {
  738. Relays []*RelayOutput
  739. }
  740. co := CmdOutput{}
  741. enc := json.NewEncoder(w.GetWriter())
  742. if args.Pretty {
  743. enc.SetIndent("", " ")
  744. }
  745. for k, v := range relays {
  746. ro := RelayOutput{NebulaIp: v.vpnIp}
  747. co.Relays = append(co.Relays, &ro)
  748. relayHI := ifce.hostMap.QueryVpnIp(v.vpnIp)
  749. if relayHI == nil {
  750. ro.RelayForIps = append(ro.RelayForIps, RelayFor{Error: errors.New("could not find hostinfo")})
  751. continue
  752. }
  753. for _, vpnIp := range relayHI.relayState.CopyRelayForIps() {
  754. rf := RelayFor{Error: nil}
  755. r, ok := relayHI.relayState.GetRelayForByIp(vpnIp)
  756. if ok {
  757. t := ""
  758. switch r.Type {
  759. case ForwardingType:
  760. t = "forwarding"
  761. case TerminalType:
  762. t = "terminal"
  763. default:
  764. t = "unknown"
  765. }
  766. s := ""
  767. switch r.State {
  768. case Requested:
  769. s = "requested"
  770. case Established:
  771. s = "established"
  772. default:
  773. s = "unknown"
  774. }
  775. rf.LocalIndex = r.LocalIndex
  776. rf.RemoteIndex = r.RemoteIndex
  777. rf.PeerIp = r.PeerIp
  778. rf.Type = t
  779. rf.State = s
  780. if rf.LocalIndex != k {
  781. rf.Error = fmt.Errorf("hostmap LocalIndex '%v' does not match RelayState LocalIndex", k)
  782. }
  783. }
  784. relayedHI := ifce.hostMap.QueryVpnIp(vpnIp)
  785. if relayedHI != nil {
  786. rf.RelayedThrough = append(rf.RelayedThrough, relayedHI.relayState.CopyRelayIps()...)
  787. }
  788. ro.RelayForIps = append(ro.RelayForIps, rf)
  789. }
  790. }
  791. err := enc.Encode(co)
  792. if err != nil {
  793. return err
  794. }
  795. return nil
  796. }
  797. func sshPrintTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  798. args, ok := fs.(*sshPrintTunnelFlags)
  799. if !ok {
  800. //TODO: error
  801. return nil
  802. }
  803. if len(a) == 0 {
  804. return w.WriteLine("No vpn ip was provided")
  805. }
  806. parsedIp := net.ParseIP(a[0])
  807. if parsedIp == nil {
  808. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  809. }
  810. vpnIp := iputil.Ip2VpnIp(parsedIp)
  811. if vpnIp == 0 {
  812. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  813. }
  814. hostInfo := ifce.hostMap.QueryVpnIp(vpnIp)
  815. if hostInfo == nil {
  816. return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
  817. }
  818. enc := json.NewEncoder(w.GetWriter())
  819. if args.Pretty {
  820. enc.SetIndent("", " ")
  821. }
  822. return enc.Encode(copyHostInfo(hostInfo, ifce.hostMap.GetPreferredRanges()))
  823. }
  824. func sshDeviceInfo(ifce *Interface, fs interface{}, w sshd.StringWriter) error {
  825. data := struct {
  826. Name string `json:"name"`
  827. Cidr string `json:"cidr"`
  828. }{
  829. Name: ifce.inside.Name(),
  830. Cidr: ifce.inside.Cidr().String(),
  831. }
  832. flags, ok := fs.(*sshDeviceInfoFlags)
  833. if !ok {
  834. return fmt.Errorf("internal error: expected flags to be sshDeviceInfoFlags but was %+v", fs)
  835. }
  836. if flags.Json || flags.Pretty {
  837. js := json.NewEncoder(w.GetWriter())
  838. if flags.Pretty {
  839. js.SetIndent("", " ")
  840. }
  841. return js.Encode(data)
  842. } else {
  843. return w.WriteLine(fmt.Sprintf("name=%v cidr=%v", data.Name, data.Cidr))
  844. }
  845. }
  846. func sshReload(c *config.C, w sshd.StringWriter) error {
  847. err := w.WriteLine("Reloading config")
  848. c.ReloadConfig()
  849. return err
  850. }