You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

authscript.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2020 Shivaram Lingamneni
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "net"
  8. "github.com/oragono/oragono/irc/utils"
  9. )
  10. // JSON-serializable input and output types for the script
  11. type AuthScriptInput struct {
  12. AccountName string `json:"accountName,omitempty"`
  13. Passphrase string `json:"passphrase,omitempty"`
  14. Certfp string `json:"certfp,omitempty"`
  15. IP string `json:"ip,omitempty"`
  16. }
  17. type AuthScriptOutput struct {
  18. AccountName string `json:"accountName"`
  19. Success bool `json:"success"`
  20. Error string `json:"error"`
  21. }
  22. func CheckAuthScript(sem utils.Semaphore, config ScriptConfig, input AuthScriptInput) (output AuthScriptOutput, err error) {
  23. if sem != nil {
  24. sem.Acquire()
  25. defer sem.Release()
  26. }
  27. inputBytes, err := json.Marshal(input)
  28. if err != nil {
  29. return
  30. }
  31. outBytes, err := RunScript(config.Command, config.Args, inputBytes, config.Timeout, config.KillTimeout)
  32. if err != nil {
  33. return
  34. }
  35. err = json.Unmarshal(outBytes, &output)
  36. if err != nil {
  37. return
  38. }
  39. if output.Error != "" {
  40. err = fmt.Errorf("Authentication process reported error: %s", output.Error)
  41. }
  42. return
  43. }
  44. type IPScriptResult uint
  45. const (
  46. IPNotChecked IPScriptResult = 0
  47. IPAccepted IPScriptResult = 1
  48. IPBanned IPScriptResult = 2
  49. IPRequireSASL IPScriptResult = 3
  50. )
  51. type IPScriptInput struct {
  52. IP string `json:"ip"`
  53. }
  54. type IPScriptOutput struct {
  55. Result IPScriptResult `json:"result"`
  56. BanMessage string `json:"banMessage"`
  57. // for caching: the network to which this result is applicable, and a TTL in seconds:
  58. CacheNet string `json:"cacheNet"`
  59. CacheSeconds int `json:"cacheSeconds"`
  60. Error string `json:"error"`
  61. }
  62. func CheckIPBan(sem utils.Semaphore, config ScriptConfig, addr net.IP) (output IPScriptOutput, err error) {
  63. if sem != nil {
  64. sem.Acquire()
  65. defer sem.Release()
  66. }
  67. inputBytes, err := json.Marshal(IPScriptInput{IP: addr.String()})
  68. if err != nil {
  69. return
  70. }
  71. outBytes, err := RunScript(config.Command, config.Args, inputBytes, config.Timeout, config.KillTimeout)
  72. if err != nil {
  73. return
  74. }
  75. err = json.Unmarshal(outBytes, &output)
  76. if err != nil {
  77. return
  78. }
  79. if output.Error != "" {
  80. err = fmt.Errorf("IP ban process reported error: %s", output.Error)
  81. } else if !(IPAccepted <= output.Result && output.Result <= IPRequireSASL) {
  82. err = fmt.Errorf("Invalid result from IP checking script: %d", output.Result)
  83. }
  84. return
  85. }