ssh.go 23 KB

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