ssh.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. package nebula
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "github.com/sirupsen/logrus"
  8. "github.com/slackhq/nebula/sshd"
  9. "io/ioutil"
  10. "net"
  11. "os"
  12. "reflect"
  13. "runtime/pprof"
  14. "strings"
  15. "syscall"
  16. )
  17. type sshListHostMapFlags struct {
  18. Json bool
  19. Pretty bool
  20. }
  21. type sshPrintCertFlags struct {
  22. Json bool
  23. Pretty bool
  24. }
  25. type sshPrintTunnelFlags struct {
  26. Pretty bool
  27. }
  28. type sshChangeRemoteFlags struct {
  29. Address string
  30. }
  31. type sshCloseTunnelFlags struct {
  32. LocalOnly bool
  33. }
  34. type sshCreateTunnelFlags struct {
  35. Address string
  36. }
  37. func wireSSHReload(ssh *sshd.SSHServer, c *Config) {
  38. c.RegisterReloadCallback(func(c *Config) {
  39. if c.GetBool("sshd.enabled", false) {
  40. err := configSSH(ssh, c)
  41. if err != nil {
  42. l.WithError(err).Error("Failed to reconfigure the sshd")
  43. ssh.Stop()
  44. }
  45. } else {
  46. ssh.Stop()
  47. }
  48. })
  49. }
  50. func configSSH(ssh *sshd.SSHServer, c *Config) error {
  51. //TODO conntrack list
  52. //TODO print firewall rules or hash?
  53. listen := c.GetString("sshd.listen", "")
  54. if listen == "" {
  55. return fmt.Errorf("sshd.listen must be provided")
  56. }
  57. port := strings.Split(listen, ":")
  58. if len(port) < 2 {
  59. return fmt.Errorf("sshd.listen does not have a port")
  60. } else if port[1] == "22" {
  61. return fmt.Errorf("sshd.listen can not use port 22")
  62. }
  63. //TODO: no good way to reload this right now
  64. hostKeyFile := c.GetString("sshd.host_key", "")
  65. if hostKeyFile == "" {
  66. return fmt.Errorf("sshd.host_key must be provided")
  67. }
  68. hostKeyBytes, err := ioutil.ReadFile(hostKeyFile)
  69. if err != nil {
  70. return fmt.Errorf("error while loading sshd.host_key file: %s", err)
  71. }
  72. err = ssh.SetHostKey(hostKeyBytes)
  73. if err != nil {
  74. return fmt.Errorf("error while adding sshd.host_key: %s", err)
  75. }
  76. rawKeys := c.Get("sshd.authorized_users")
  77. keys, ok := rawKeys.([]interface{})
  78. if ok {
  79. for _, rk := range keys {
  80. kDef, ok := rk.(map[interface{}]interface{})
  81. if !ok {
  82. l.WithField("sshKeyConfig", rk).Warn("Authorized user had an error, ignoring")
  83. continue
  84. }
  85. user, ok := kDef["user"].(string)
  86. if !ok {
  87. l.WithField("sshKeyConfig", rk).Warn("Authorized user is missing the user field")
  88. continue
  89. }
  90. k := kDef["keys"]
  91. switch v := k.(type) {
  92. case string:
  93. err := ssh.AddAuthorizedKey(user, v)
  94. if err != nil {
  95. l.WithError(err).WithField("sshKeyConfig", rk).WithField("sshKey", v).Warn("Failed to authorize key")
  96. continue
  97. }
  98. case []interface{}:
  99. for _, subK := range v {
  100. sk, ok := subK.(string)
  101. if !ok {
  102. l.WithField("sshKeyConfig", rk).WithField("sshKey", subK).Warn("Did not understand ssh key")
  103. continue
  104. }
  105. err := ssh.AddAuthorizedKey(user, sk)
  106. if err != nil {
  107. l.WithError(err).WithField("sshKeyConfig", sk).Warn("Failed to authorize key")
  108. continue
  109. }
  110. }
  111. default:
  112. l.WithField("sshKeyConfig", rk).Warn("Authorized user is missing the keys field or was not understood")
  113. }
  114. }
  115. } else {
  116. l.Info("no ssh users to authorize")
  117. }
  118. if c.GetBool("sshd.enabled", false) {
  119. ssh.Stop()
  120. go ssh.Run(listen)
  121. } else {
  122. ssh.Stop()
  123. }
  124. return nil
  125. }
  126. func attachCommands(ssh *sshd.SSHServer, hostMap *HostMap, pendingHostMap *HostMap, lightHouse *LightHouse, ifce *Interface) {
  127. ssh.RegisterCommand(&sshd.Command{
  128. Name: "list-hostmap",
  129. ShortDescription: "List all known previously connected hosts",
  130. Flags: func() (*flag.FlagSet, interface{}) {
  131. fl := flag.NewFlagSet("", flag.ContinueOnError)
  132. s := sshListHostMapFlags{}
  133. fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
  134. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
  135. return fl, &s
  136. },
  137. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  138. return sshListHostMap(hostMap, fs, w)
  139. },
  140. })
  141. ssh.RegisterCommand(&sshd.Command{
  142. Name: "list-pending-hostmap",
  143. ShortDescription: "List all handshaking hosts",
  144. Flags: func() (*flag.FlagSet, interface{}) {
  145. fl := flag.NewFlagSet("", flag.ContinueOnError)
  146. s := sshListHostMapFlags{}
  147. fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
  148. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
  149. return fl, &s
  150. },
  151. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  152. return sshListHostMap(pendingHostMap, fs, w)
  153. },
  154. })
  155. ssh.RegisterCommand(&sshd.Command{
  156. Name: "list-lighthouse-addrmap",
  157. ShortDescription: "List all lighthouse map entries",
  158. Flags: func() (*flag.FlagSet, interface{}) {
  159. fl := flag.NewFlagSet("", flag.ContinueOnError)
  160. s := sshListHostMapFlags{}
  161. fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
  162. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
  163. return fl, &s
  164. },
  165. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  166. return sshListLighthouseMap(lightHouse, fs, w)
  167. },
  168. })
  169. ssh.RegisterCommand(&sshd.Command{
  170. Name: "reload",
  171. ShortDescription: "Reloads configuration from disk, same as sending HUP to the process",
  172. Callback: sshReload,
  173. })
  174. ssh.RegisterCommand(&sshd.Command{
  175. Name: "start-cpu-profile",
  176. ShortDescription: "Starts a cpu profile and write output to the provided file",
  177. Callback: sshStartCpuProfile,
  178. })
  179. ssh.RegisterCommand(&sshd.Command{
  180. Name: "stop-cpu-profile",
  181. ShortDescription: "Stops a cpu profile and writes output to the previously provided file",
  182. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  183. pprof.StopCPUProfile()
  184. return w.WriteLine("If a CPU profile was running it is now stopped")
  185. },
  186. })
  187. ssh.RegisterCommand(&sshd.Command{
  188. Name: "save-heap-profile",
  189. ShortDescription: "Saves a heap profile to the provided path",
  190. Callback: sshGetHeapProfile,
  191. })
  192. ssh.RegisterCommand(&sshd.Command{
  193. Name: "log-level",
  194. ShortDescription: "Gets or sets the current log level",
  195. Callback: sshLogLevel,
  196. })
  197. ssh.RegisterCommand(&sshd.Command{
  198. Name: "log-format",
  199. ShortDescription: "Gets or sets the current log format",
  200. Callback: sshLogFormat,
  201. })
  202. ssh.RegisterCommand(&sshd.Command{
  203. Name: "version",
  204. ShortDescription: "Prints the currently running version of nebula",
  205. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  206. return sshVersion(ifce, fs, a, w)
  207. },
  208. })
  209. ssh.RegisterCommand(&sshd.Command{
  210. Name: "print-cert",
  211. ShortDescription: "Prints the current certificate being used or the certificate for the provided vpn ip",
  212. Flags: func() (*flag.FlagSet, interface{}) {
  213. fl := flag.NewFlagSet("", flag.ContinueOnError)
  214. s := sshPrintCertFlags{}
  215. fl.BoolVar(&s.Json, "json", false, "outputs as json")
  216. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
  217. return fl, &s
  218. },
  219. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  220. return sshPrintCert(ifce, fs, a, w)
  221. },
  222. })
  223. ssh.RegisterCommand(&sshd.Command{
  224. Name: "print-tunnel",
  225. ShortDescription: "Prints json details about a tunnel for the provided vpn ip",
  226. Flags: func() (*flag.FlagSet, interface{}) {
  227. fl := flag.NewFlagSet("", flag.ContinueOnError)
  228. s := sshPrintTunnelFlags{}
  229. fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
  230. return fl, &s
  231. },
  232. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  233. return sshPrintTunnel(ifce, fs, a, w)
  234. },
  235. })
  236. ssh.RegisterCommand(&sshd.Command{
  237. Name: "change-remote",
  238. ShortDescription: "Changes the remote address used in the tunnel for the provided vpn ip",
  239. Flags: func() (*flag.FlagSet, interface{}) {
  240. fl := flag.NewFlagSet("", flag.ContinueOnError)
  241. s := sshChangeRemoteFlags{}
  242. fl.StringVar(&s.Address, "address", "", "The new remote address, ip:port")
  243. return fl, &s
  244. },
  245. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  246. return sshChangeRemote(ifce, fs, a, w)
  247. },
  248. })
  249. ssh.RegisterCommand(&sshd.Command{
  250. Name: "close-tunnel",
  251. ShortDescription: "Closes a tunnel for the provided vpn ip",
  252. Flags: func() (*flag.FlagSet, interface{}) {
  253. fl := flag.NewFlagSet("", flag.ContinueOnError)
  254. s := sshCloseTunnelFlags{}
  255. fl.BoolVar(&s.LocalOnly, "local-only", false, "Disables notifying the remote that the tunnel is shutting down")
  256. return fl, &s
  257. },
  258. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  259. return sshCloseTunnel(ifce, fs, a, w)
  260. },
  261. })
  262. ssh.RegisterCommand(&sshd.Command{
  263. Name: "create-tunnel",
  264. ShortDescription: "Creates a tunnel for the provided vpn ip and address",
  265. Help: "The lighthouses will be queried for real addresses but you can provide one as well.",
  266. Flags: func() (*flag.FlagSet, interface{}) {
  267. fl := flag.NewFlagSet("", flag.ContinueOnError)
  268. s := sshCreateTunnelFlags{}
  269. fl.StringVar(&s.Address, "address", "", "Optionally provide a real remote address, ip:port ")
  270. return fl, &s
  271. },
  272. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  273. return sshCreateTunnel(ifce, fs, a, w)
  274. },
  275. })
  276. ssh.RegisterCommand(&sshd.Command{
  277. Name: "query-lighthouse",
  278. ShortDescription: "Query the lighthouses for the provided vpn ip",
  279. Help: "This command is asynchronous. Only currently known udp ips will be printed.",
  280. Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
  281. return sshQueryLighthouse(ifce, fs, a, w)
  282. },
  283. })
  284. }
  285. func sshListHostMap(hostMap *HostMap, a interface{}, w sshd.StringWriter) error {
  286. fs, ok := a.(*sshListHostMapFlags)
  287. if !ok {
  288. //TODO: error
  289. return nil
  290. }
  291. hostMap.RLock()
  292. defer hostMap.RUnlock()
  293. if fs.Json || fs.Pretty {
  294. js := json.NewEncoder(w.GetWriter())
  295. if fs.Pretty {
  296. js.SetIndent("", " ")
  297. }
  298. d := make([]m, len(hostMap.Hosts))
  299. x := 0
  300. var h m
  301. for _, v := range hostMap.Hosts {
  302. h = m{
  303. "vpnIp": int2ip(v.hostId),
  304. "localIndex": v.localIndexId,
  305. "remoteIndex": v.remoteIndexId,
  306. "remoteAddrs": v.RemoteUDPAddrs(),
  307. "cachedPackets": len(v.packetStore),
  308. "cert": v.GetCert(),
  309. }
  310. if v.ConnectionState != nil {
  311. h["messageCounter"] = v.ConnectionState.messageCounter
  312. }
  313. d[x] = h
  314. x++
  315. }
  316. err := js.Encode(d)
  317. if err != nil {
  318. //TODO
  319. return nil
  320. }
  321. } else {
  322. for i, v := range hostMap.Hosts {
  323. err := w.WriteLine(fmt.Sprintf("%s: %s", int2ip(i), v.RemoteUDPAddrs()))
  324. if err != nil {
  325. return err
  326. }
  327. }
  328. }
  329. return nil
  330. }
  331. func sshListLighthouseMap(lightHouse *LightHouse, a interface{}, w sshd.StringWriter) error {
  332. fs, ok := a.(*sshListHostMapFlags)
  333. if !ok {
  334. //TODO: error
  335. return nil
  336. }
  337. lightHouse.RLock()
  338. defer lightHouse.RUnlock()
  339. if fs.Json || fs.Pretty {
  340. js := json.NewEncoder(w.GetWriter())
  341. if fs.Pretty {
  342. js.SetIndent("", " ")
  343. }
  344. d := make([]m, len(lightHouse.addrMap))
  345. x := 0
  346. var h m
  347. for vpnIp, v := range lightHouse.addrMap {
  348. ips := make([]string, len(v))
  349. for i, ip := range v {
  350. ips[i] = ip.String()
  351. }
  352. h = m{
  353. "vpnIp": int2ip(vpnIp),
  354. "addrs": ips,
  355. }
  356. d[x] = h
  357. x++
  358. }
  359. err := js.Encode(d)
  360. if err != nil {
  361. //TODO
  362. return nil
  363. }
  364. } else {
  365. for vpnIp, v := range lightHouse.addrMap {
  366. ips := make([]string, len(v))
  367. for i, ip := range v {
  368. ips[i] = ip.String()
  369. }
  370. err := w.WriteLine(fmt.Sprintf("%s: %s", int2ip(vpnIp), ips))
  371. if err != nil {
  372. return err
  373. }
  374. }
  375. }
  376. return nil
  377. }
  378. func sshStartCpuProfile(fs interface{}, a []string, w sshd.StringWriter) error {
  379. if len(a) == 0 {
  380. err := w.WriteLine("No path to write profile provided")
  381. return err
  382. }
  383. file, err := os.Create(a[0])
  384. if err != nil {
  385. err = w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
  386. return err
  387. }
  388. err = pprof.StartCPUProfile(file)
  389. if err != nil {
  390. err = w.WriteLine(fmt.Sprintf("Unable to start cpu profile: %s", err))
  391. return err
  392. }
  393. err = w.WriteLine(fmt.Sprintf("Started cpu profile, issue stop-cpu-profile to write the output to %s", a))
  394. return err
  395. }
  396. func sshVersion(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  397. return w.WriteLine(fmt.Sprintf("%s", ifce.version))
  398. }
  399. func sshQueryLighthouse(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  400. if len(a) == 0 {
  401. return w.WriteLine("No vpn ip was provided")
  402. }
  403. vpnIp := ip2int(net.ParseIP(a[0]))
  404. if vpnIp == 0 {
  405. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  406. }
  407. ips, _ := ifce.lightHouse.Query(vpnIp, ifce)
  408. return json.NewEncoder(w.GetWriter()).Encode(ips)
  409. }
  410. func sshCloseTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  411. flags, ok := fs.(*sshCloseTunnelFlags)
  412. if !ok {
  413. //TODO: error
  414. return nil
  415. }
  416. if len(a) == 0 {
  417. return w.WriteLine("No vpn ip was provided")
  418. }
  419. vpnIp := ip2int(net.ParseIP(a[0]))
  420. if vpnIp == 0 {
  421. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  422. }
  423. hostInfo, err := ifce.hostMap.QueryVpnIP(uint32(vpnIp))
  424. if err != nil {
  425. return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
  426. }
  427. if !flags.LocalOnly {
  428. ifce.send(
  429. closeTunnel,
  430. 0,
  431. hostInfo.ConnectionState,
  432. hostInfo,
  433. hostInfo.remote,
  434. []byte{},
  435. make([]byte, 12, 12),
  436. make([]byte, mtu),
  437. )
  438. }
  439. ifce.closeTunnel(hostInfo)
  440. return w.WriteLine("Closed")
  441. }
  442. func sshCreateTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  443. flags, ok := fs.(*sshCreateTunnelFlags)
  444. if !ok {
  445. //TODO: error
  446. return nil
  447. }
  448. if len(a) == 0 {
  449. return w.WriteLine("No vpn ip was provided")
  450. }
  451. vpnIp := ip2int(net.ParseIP(a[0]))
  452. if vpnIp == 0 {
  453. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  454. }
  455. hostInfo, _ := ifce.hostMap.QueryVpnIP(uint32(vpnIp))
  456. if hostInfo != nil {
  457. return w.WriteLine(fmt.Sprintf("Tunnel already exists"))
  458. }
  459. hostInfo, _ = ifce.handshakeManager.pendingHostMap.QueryVpnIP(uint32(vpnIp))
  460. if hostInfo != nil {
  461. return w.WriteLine(fmt.Sprintf("Tunnel already handshaking"))
  462. }
  463. var addr *udpAddr
  464. if flags.Address != "" {
  465. addr = NewUDPAddrFromString(flags.Address)
  466. if addr == nil {
  467. return w.WriteLine("Address could not be parsed")
  468. }
  469. }
  470. hostInfo = ifce.handshakeManager.AddVpnIP(vpnIp)
  471. if addr != nil {
  472. hostInfo.SetRemote(*addr)
  473. }
  474. ifce.getOrHandshake(vpnIp)
  475. return w.WriteLine("Created")
  476. }
  477. func sshChangeRemote(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  478. flags, ok := fs.(*sshChangeRemoteFlags)
  479. if !ok {
  480. //TODO: error
  481. return nil
  482. }
  483. if len(a) == 0 {
  484. return w.WriteLine("No vpn ip was provided")
  485. }
  486. if flags.Address == "" {
  487. return w.WriteLine("No address was provided")
  488. }
  489. addr := NewUDPAddrFromString(flags.Address)
  490. if addr == nil {
  491. return w.WriteLine("Address could not be parsed")
  492. }
  493. vpnIp := ip2int(net.ParseIP(a[0]))
  494. if vpnIp == 0 {
  495. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  496. }
  497. hostInfo, err := ifce.hostMap.QueryVpnIP(uint32(vpnIp))
  498. if err != nil {
  499. return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
  500. }
  501. hostInfo.SetRemote(*addr)
  502. return w.WriteLine("Changed")
  503. }
  504. func sshGetHeapProfile(fs interface{}, a []string, w sshd.StringWriter) error {
  505. if len(a) == 0 {
  506. return w.WriteLine("No path to write profile provided")
  507. }
  508. file, err := os.Create(a[0])
  509. if err != nil {
  510. err = w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
  511. return err
  512. }
  513. err = pprof.WriteHeapProfile(file)
  514. if err != nil {
  515. err = w.WriteLine(fmt.Sprintf("Unable to write profile: %s", err))
  516. return err
  517. }
  518. err = w.WriteLine(fmt.Sprintf("Mem profile created at %s", a))
  519. return err
  520. }
  521. func sshLogLevel(fs interface{}, a []string, w sshd.StringWriter) error {
  522. if len(a) == 0 {
  523. return w.WriteLine(fmt.Sprintf("Log level is: %s", l.Level))
  524. }
  525. level, err := logrus.ParseLevel(a[0])
  526. if err != nil {
  527. return w.WriteLine(fmt.Sprintf("Unknown log level %s. Possible log levels: %s", a, logrus.AllLevels))
  528. }
  529. l.SetLevel(level)
  530. return w.WriteLine(fmt.Sprintf("Log level is: %s", l.Level))
  531. }
  532. func sshLogFormat(fs interface{}, a []string, w sshd.StringWriter) error {
  533. if len(a) == 0 {
  534. return w.WriteLine(fmt.Sprintf("Log format is: %s", reflect.TypeOf(l.Formatter)))
  535. }
  536. logFormat := strings.ToLower(a[0])
  537. switch logFormat {
  538. case "text":
  539. l.Formatter = &logrus.TextFormatter{}
  540. case "json":
  541. l.Formatter = &logrus.JSONFormatter{}
  542. default:
  543. return fmt.Errorf("unknown log format `%s`. possible formats: %s", logFormat, []string{"text", "json"})
  544. }
  545. return w.WriteLine(fmt.Sprintf("Log format is: %s", reflect.TypeOf(l.Formatter)))
  546. }
  547. func sshPrintCert(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  548. args, ok := fs.(*sshPrintCertFlags)
  549. if !ok {
  550. //TODO: error
  551. return nil
  552. }
  553. cert := ifce.certState.certificate
  554. if len(a) > 0 {
  555. vpnIp := ip2int(net.ParseIP(a[0]))
  556. if vpnIp == 0 {
  557. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  558. }
  559. hostInfo, err := ifce.hostMap.QueryVpnIP(uint32(vpnIp))
  560. if err != nil {
  561. return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
  562. }
  563. cert = hostInfo.GetCert()
  564. }
  565. if args.Json || args.Pretty {
  566. b, err := cert.MarshalJSON()
  567. if err != nil {
  568. //TODO: handle it
  569. return nil
  570. }
  571. if args.Pretty {
  572. buf := new(bytes.Buffer)
  573. err := json.Indent(buf, b, "", " ")
  574. b = buf.Bytes()
  575. if err != nil {
  576. //TODO: handle it
  577. return nil
  578. }
  579. }
  580. return w.WriteBytes(b)
  581. }
  582. return w.WriteLine(cert.String())
  583. }
  584. func sshPrintTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
  585. args, ok := fs.(*sshPrintTunnelFlags)
  586. if !ok {
  587. //TODO: error
  588. return nil
  589. }
  590. if len(a) == 0 {
  591. return w.WriteLine("No vpn ip was provided")
  592. }
  593. vpnIp := ip2int(net.ParseIP(a[0]))
  594. if vpnIp == 0 {
  595. return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
  596. }
  597. hostInfo, err := ifce.hostMap.QueryVpnIP(uint32(vpnIp))
  598. if err != nil {
  599. return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
  600. }
  601. enc := json.NewEncoder(w.GetWriter())
  602. if args.Pretty {
  603. enc.SetIndent("", " ")
  604. }
  605. return enc.Encode(hostInfo)
  606. }
  607. func sshReload(fs interface{}, a []string, w sshd.StringWriter) error {
  608. p, err := os.FindProcess(os.Getpid())
  609. if err != nil {
  610. return w.WriteLine(err.Error())
  611. //TODO
  612. }
  613. err = p.Signal(syscall.SIGHUP)
  614. if err != nil {
  615. return w.WriteLine(err.Error())
  616. //TODO
  617. }
  618. return w.WriteLine("HUP sent")
  619. }