session.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package xs
  2. // Package xs - a secure terminal client/server written from scratch in Go
  3. //
  4. // Copyright (c) 2017-2020 Russell Magee
  5. // Licensed under the terms of the MIT license (see LICENSE.mit in this
  6. // distribution)
  7. //
  8. // golang implementation by Russ Magee (rmagee_at_gmail.com)
  9. // Session info/routines for the HKExSh
  10. import (
  11. "fmt"
  12. "runtime"
  13. )
  14. // Session holds essential bookkeeping info about an active session.
  15. type Session struct {
  16. op []byte
  17. who []byte
  18. connhost []byte
  19. termtype []byte // client initial $TERM
  20. cmd []byte
  21. authCookie []byte
  22. status uint32 // exit status (0-255 is std UNIX status)
  23. }
  24. // Output Session record as a string. Implements Stringer interface.
  25. func (h *Session) String() string {
  26. return fmt.Sprintf("xs.Session:\nOp:%v\nWho:%v\nCmd:%v\nAuthCookie:%v\nStatus:%v",
  27. h.op, h.who, h.cmd, h.AuthCookie(false), h.status)
  28. }
  29. // Op returns the op code of the Session (interactive shell, cmd, ...)
  30. func (h Session) Op() []byte {
  31. return h.op
  32. }
  33. // SetOp stores the op code desired for a Session.
  34. func (h *Session) SetOp(o []byte) {
  35. h.op = o
  36. }
  37. // Who returns the user associated with a Session.
  38. func (h Session) Who() []byte {
  39. return h.who
  40. }
  41. // SetWho sets the username associated with a Session.
  42. func (h *Session) SetWho(w []byte) {
  43. h.who = w
  44. }
  45. // ConnHost returns the connecting hostname/IP string for a Session.
  46. func (h Session) ConnHost() []byte {
  47. return h.connhost
  48. }
  49. // SetConnHost stores the connecting hostname/IP string for a Session.
  50. func (h *Session) SetConnHost(n []byte) {
  51. h.connhost = n
  52. }
  53. // TermType returns the TERM env variable reported by the client initiating
  54. // a Session.
  55. func (h Session) TermType() []byte {
  56. return h.termtype
  57. }
  58. // SetTermType stores the TERM env variable supplied by the client initiating
  59. // a Session.
  60. func (h *Session) SetTermType(t []byte) {
  61. h.termtype = t
  62. }
  63. // Cmd returns the command requested for execution by a client initiating
  64. // the Session.
  65. func (h Session) Cmd() []byte {
  66. return h.cmd
  67. }
  68. // SetCmd stores the command request by the client for execution when initiating
  69. // the Session.
  70. func (h *Session) SetCmd(c []byte) {
  71. h.cmd = c
  72. }
  73. // AuthCookie returns the authcookie (essentially the password) used for
  74. // authorization of the Session. This return value is censored unless
  75. // reallyShow is true (so dumps of Session Info do not accidentally leak it).
  76. func (h Session) AuthCookie(reallyShow bool) []byte {
  77. if reallyShow {
  78. return h.authCookie
  79. }
  80. return []byte("**REDACTED**")
  81. }
  82. // SetAuthCookie stores the authcookie (essentially the password) used to
  83. // authenticate the Session.
  84. func (h *Session) SetAuthCookie(a []byte) {
  85. h.authCookie = a
  86. }
  87. // ClearAuthCookie attempts to scrub the Session's stored authcookie.
  88. //
  89. // This should of course be called as soon as possible after authentication
  90. // and it is no longer required.
  91. func (h *Session) ClearAuthCookie() {
  92. for i := range h.authCookie {
  93. h.authCookie[i] = 0
  94. }
  95. runtime.GC()
  96. }
  97. // Status returns the (current) Session status code.
  98. //
  99. // This usually corresponds to a UNIX shell exit code, but
  100. // extended codes are returns at times to indicate internal errors.
  101. func (h Session) Status() uint32 {
  102. return h.status
  103. }
  104. // SetStatus stores the current Session status code.
  105. func (h *Session) SetStatus(s uint32) {
  106. h.status = s
  107. }
  108. // NewSession returns a new Session record.
  109. func NewSession(op, who, connhost, ttype, cmd, authcookie []byte, status uint32) *Session {
  110. return &Session{
  111. op: op,
  112. who: who,
  113. connhost: connhost,
  114. termtype: ttype,
  115. cmd: cmd,
  116. authCookie: authcookie,
  117. status: status}
  118. }