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.

hostserv.go 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "errors"
  6. "fmt"
  7. "regexp"
  8. "github.com/goshuirc/irc-go/ircfmt"
  9. "github.com/ergochat/ergo/irc/utils"
  10. )
  11. const (
  12. hostservHelp = `HostServ lets you manage your vhost (i.e., the string displayed
  13. in place of your client's hostname/IP).`
  14. )
  15. var (
  16. errVHostBadCharacters = errors.New("Vhost contains prohibited characters")
  17. errVHostTooLong = errors.New("Vhost is too long")
  18. // ascii only for now
  19. defaultValidVhostRegex = regexp.MustCompile(`^[0-9A-Za-z.\-_/]+$`)
  20. )
  21. func hostservEnabled(config *Config) bool {
  22. return config.Accounts.VHosts.Enabled
  23. }
  24. var (
  25. hostservCommands = map[string]*serviceCommand{
  26. "on": {
  27. handler: hsOnOffHandler,
  28. help: `Syntax: $bON$b
  29. ON enables your vhost, if you have one approved.`,
  30. helpShort: `$bON$b enables your vhost, if you have one approved.`,
  31. authRequired: true,
  32. enabled: hostservEnabled,
  33. },
  34. "off": {
  35. handler: hsOnOffHandler,
  36. help: `Syntax: $bOFF$b
  37. OFF disables your vhost, if you have one approved.`,
  38. helpShort: `$bOFF$b disables your vhost, if you have one approved.`,
  39. authRequired: true,
  40. enabled: hostservEnabled,
  41. },
  42. "status": {
  43. handler: hsStatusHandler,
  44. help: `Syntax: $bSTATUS [user]$b
  45. STATUS displays your current vhost, if any, and the status of your most recent
  46. request for a new one. A server operator can view someone else's status.`,
  47. helpShort: `$bSTATUS$b shows your vhost and request status.`,
  48. enabled: hostservEnabled,
  49. },
  50. "set": {
  51. handler: hsSetHandler,
  52. help: `Syntax: $bSET <user> <vhost>$b
  53. SET sets a user's vhost, bypassing the request system.`,
  54. helpShort: `$bSET$b sets a user's vhost.`,
  55. capabs: []string{"vhosts"},
  56. enabled: hostservEnabled,
  57. minParams: 2,
  58. },
  59. "del": {
  60. handler: hsSetHandler,
  61. help: `Syntax: $bDEL <user>$b
  62. DEL deletes a user's vhost.`,
  63. helpShort: `$bDEL$b deletes a user's vhost.`,
  64. capabs: []string{"vhosts"},
  65. enabled: hostservEnabled,
  66. minParams: 1,
  67. },
  68. "setcloaksecret": {
  69. handler: hsSetCloakSecretHandler,
  70. help: `Syntax: $bSETCLOAKSECRET$b <secret> [code]
  71. SETCLOAKSECRET can be used to set or rotate the cloak secret. You should use
  72. a cryptographically strong secret. To prevent accidental modification, a
  73. verification code is required; invoking the command without a code will
  74. display the necessary code.`,
  75. helpShort: `$bSETCLOAKSECRET$b modifies the IP cloaking secret.`,
  76. capabs: []string{"vhosts", "rehash"},
  77. minParams: 1,
  78. maxParams: 2,
  79. },
  80. }
  81. )
  82. func hsOnOffHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  83. enable := false
  84. if command == "on" {
  85. enable = true
  86. }
  87. _, err := server.accounts.VHostSetEnabled(client, enable)
  88. if err == errNoVhost {
  89. service.Notice(rb, client.t(err.Error()))
  90. } else if err != nil {
  91. service.Notice(rb, client.t("An error occurred"))
  92. } else if enable {
  93. service.Notice(rb, client.t("Successfully enabled your vhost"))
  94. } else {
  95. service.Notice(rb, client.t("Successfully disabled your vhost"))
  96. }
  97. }
  98. func hsStatusHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  99. var accountName string
  100. if len(params) > 0 {
  101. if !client.HasRoleCapabs("vhosts") {
  102. service.Notice(rb, client.t("Command restricted"))
  103. return
  104. }
  105. accountName = params[0]
  106. } else {
  107. accountName = client.Account()
  108. if accountName == "" {
  109. service.Notice(rb, client.t("You're not logged into an account"))
  110. return
  111. }
  112. }
  113. account, err := server.accounts.LoadAccount(accountName)
  114. if err != nil {
  115. if err != errAccountDoesNotExist {
  116. server.logger.Warning("internal", "error loading account info", accountName, err.Error())
  117. }
  118. service.Notice(rb, client.t("No such account"))
  119. return
  120. }
  121. if account.VHost.ApprovedVHost != "" {
  122. service.Notice(rb, fmt.Sprintf(client.t("Account %[1]s has vhost: %[2]s"), accountName, account.VHost.ApprovedVHost))
  123. if !account.VHost.Enabled {
  124. service.Notice(rb, client.t("This vhost is currently disabled, but can be enabled with /HS ON"))
  125. }
  126. } else {
  127. service.Notice(rb, fmt.Sprintf(client.t("Account %s has no vhost"), accountName))
  128. }
  129. }
  130. func validateVhost(server *Server, vhost string, oper bool) error {
  131. config := server.Config()
  132. if len(vhost) > config.Accounts.VHosts.MaxLength {
  133. return errVHostTooLong
  134. }
  135. if !config.Accounts.VHosts.validRegexp.MatchString(vhost) {
  136. return errVHostBadCharacters
  137. }
  138. return nil
  139. }
  140. func hsSetHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  141. user := params[0]
  142. var vhost string
  143. if command == "set" {
  144. vhost = params[1]
  145. if validateVhost(server, vhost, true) != nil {
  146. service.Notice(rb, client.t("Invalid vhost"))
  147. return
  148. }
  149. }
  150. // else: command == "del", vhost == ""
  151. _, err := server.accounts.VHostSet(user, vhost)
  152. if err != nil {
  153. service.Notice(rb, client.t("An error occurred"))
  154. } else if vhost != "" {
  155. service.Notice(rb, client.t("Successfully set vhost"))
  156. } else {
  157. service.Notice(rb, client.t("Successfully cleared vhost"))
  158. }
  159. }
  160. func hsSetCloakSecretHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  161. secret := params[0]
  162. expectedCode := utils.ConfirmationCode(secret, server.ctime)
  163. if len(params) == 1 || params[1] != expectedCode {
  164. service.Notice(rb, ircfmt.Unescape(client.t("$bWarning: changing the cloak secret will invalidate stored ban/invite/exception lists.$b")))
  165. service.Notice(rb, fmt.Sprintf(client.t("To confirm, run this command: %s"), fmt.Sprintf("/HS SETCLOAKSECRET %s %s", secret, expectedCode)))
  166. return
  167. }
  168. StoreCloakSecret(server.store, secret)
  169. service.Notice(rb, client.t("Rotated the cloak secret; you must rehash or restart the server for it to take effect"))
  170. }