sasl.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package sasl
  26. import (
  27. "bufio"
  28. "bytes"
  29. "encoding/base64"
  30. "encoding/hex"
  31. "fmt"
  32. "io"
  33. "log"
  34. "net"
  35. "os"
  36. "strconv"
  37. "strings"
  38. "github.com/google/uuid"
  39. )
  40. type SaslServer struct {
  41. pid int
  42. cuid int
  43. }
  44. const (
  45. Version = "VERSION"
  46. CPid = "CPID"
  47. SPid = "SPID"
  48. Cuid = "CUID"
  49. Cookie = "COOKIE"
  50. Mech = "MECH"
  51. Done = "DONE"
  52. Auth = "AUTH"
  53. Fail = "FAIL"
  54. Cont = "CONT"
  55. Ok = "OK"
  56. )
  57. const (
  58. ContinueStateNone = iota
  59. ContinueStateCredentials
  60. )
  61. func NewSaslServer() *SaslServer {
  62. return &SaslServer{
  63. pid: os.Getpid(),
  64. cuid: 0,
  65. }
  66. }
  67. func (s *SaslServer) Run() {
  68. go func() {
  69. l, err := net.Listen("tcp", "127.0.0.1:65201")
  70. if err != nil {
  71. log.Fatalf("Coulf not start SASL server: %s\n", err)
  72. return
  73. }
  74. defer l.Close()
  75. log.Printf("Listen sasl on: %s\n", l.Addr().String())
  76. for {
  77. conn, err := l.Accept()
  78. s.cuid++
  79. if err != nil {
  80. log.Println("Error accepting: ", err.Error())
  81. continue
  82. }
  83. go s.handleRequest(conn)
  84. }
  85. }()
  86. }
  87. func (s *SaslServer) handleRequest(conn net.Conn) {
  88. connectionReader := bufio.NewReader(conn)
  89. continueState := ContinueStateNone
  90. for {
  91. fullbuf, err := connectionReader.ReadString('\n')
  92. if err == io.EOF {
  93. continue
  94. }
  95. if err != nil {
  96. fmt.Printf("Read error %s\n", err)
  97. }
  98. currentMessage := fullbuf
  99. if strings.Index(currentMessage, Version) == 0 {
  100. versionIds := strings.Split(currentMessage, "\t")
  101. if len(versionIds) < 3 {
  102. break
  103. }
  104. if major, err := strconv.Atoi(versionIds[1]); err != nil || major != 1 {
  105. break
  106. }
  107. cookieUuid := uuid.New()
  108. fmt.Fprintf(conn, "%s\t%d\t%d\n", Version, 1, 2)
  109. fmt.Fprintf(conn, "%s\t%s\t%s\n", Mech, "PLAIN", "plaintext")
  110. fmt.Fprintf(conn, "%s\t%s\t%s\n", Mech, "LOGIN", "plaintext")
  111. fmt.Fprintf(conn, "%s\t%d\n", SPid, s.pid)
  112. fmt.Fprintf(conn, "%s\t%d\n", Cuid, s.cuid)
  113. fmt.Fprintf(conn, "%s\t%s\n", Cookie, hex.EncodeToString(cookieUuid[:]))
  114. fmt.Fprintf(conn, "%s\n", Done)
  115. } else if strings.Index(currentMessage, Auth) == 0 {
  116. authIds := strings.Split(currentMessage, "\t")
  117. if len(authIds) < 2 {
  118. break
  119. }
  120. fmt.Fprintf(conn, "%s\t%s\t%s\n", Cont, authIds[1], base64.StdEncoding.EncodeToString([]byte("Username:")))
  121. continueState = ContinueStateCredentials
  122. } else if strings.Index(currentMessage, Cont) == 0 {
  123. contIds := strings.Split(currentMessage, "\t")
  124. if len(contIds) < 2 {
  125. break
  126. }
  127. if continueState == ContinueStateCredentials {
  128. if len(contIds) < 3 {
  129. fmt.Fprintf(conn, "%s\t%s\treason=%s\n", Fail, contIds[1], "invalid base64 data")
  130. return
  131. }
  132. credentials, err := base64.StdEncoding.DecodeString(contIds[2])
  133. if err != nil {
  134. fmt.Fprintf(conn, "%s\t%s\treason=%s\n", Fail, contIds[1], "invalid base64 data")
  135. return
  136. }
  137. credentialList := bytes.Split(credentials, []byte{0})
  138. if len(credentialList) < 3 {
  139. fmt.Fprintf(conn, "%s\t%s\treason=%s\n", Fail, contIds[1], "invalid user or password")
  140. return
  141. }
  142. // identity := credentialList[0]
  143. login := credentialList[1]
  144. // password := credentialList[2]
  145. //TODO: Use auth here
  146. // if login != "semlanik@semlanik.org" || password != "test" {
  147. if true {
  148. fmt.Fprintf(conn, "%s\t%s\treason=%s\n", Fail, contIds[1], "invalid user or password")
  149. return
  150. }
  151. fmt.Fprintf(conn, "%s\t%s\tuser=%s\n", Ok, contIds[1], login)
  152. continueState = ContinueStateNone
  153. } else {
  154. fmt.Fprintf(conn, "%s\t%s\treason=%s\n", Fail, contIds[1], "invalid user or password")
  155. }
  156. }
  157. }
  158. conn.Close()
  159. }