ssh.go 19 KB

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