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

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