Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. "time"
  9. "github.com/goshuirc/irc-go/ircfmt"
  10. "github.com/oragono/oragono/irc/sno"
  11. "github.com/oragono/oragono/irc/utils"
  12. )
  13. const (
  14. hostservHelp = `HostServ lets you manage your vhost (i.e., the string displayed
  15. in place of your client's hostname/IP).`
  16. hsNickMask = "HostServ!HostServ@localhost"
  17. )
  18. var (
  19. errVHostBadCharacters = errors.New("Vhost contains prohibited characters")
  20. errVHostTooLong = errors.New("Vhost is too long")
  21. // ascii only for now
  22. defaultValidVhostRegex = regexp.MustCompile(`^[0-9A-Za-z.\-_/]+$`)
  23. )
  24. func hostservEnabled(config *Config) bool {
  25. return config.Accounts.VHosts.Enabled
  26. }
  27. func hostservRequestsEnabled(config *Config) bool {
  28. return config.Accounts.VHosts.Enabled && config.Accounts.VHosts.UserRequests.Enabled
  29. }
  30. var (
  31. hostservCommands = map[string]*serviceCommand{
  32. "on": {
  33. handler: hsOnOffHandler,
  34. help: `Syntax: $bON$b
  35. ON enables your vhost, if you have one approved.`,
  36. helpShort: `$bON$b enables your vhost, if you have one approved.`,
  37. authRequired: true,
  38. enabled: hostservEnabled,
  39. },
  40. "off": {
  41. handler: hsOnOffHandler,
  42. help: `Syntax: $bOFF$b
  43. OFF disables your vhost, if you have one approved.`,
  44. helpShort: `$bOFF$b disables your vhost, if you have one approved.`,
  45. authRequired: true,
  46. enabled: hostservEnabled,
  47. },
  48. "request": {
  49. handler: hsRequestHandler,
  50. help: `Syntax: $bREQUEST <vhost>$b
  51. REQUEST requests that a new vhost by assigned to your account. The request must
  52. then be approved by a server operator.`,
  53. helpShort: `$bREQUEST$b requests a new vhost, pending operator approval.`,
  54. authRequired: true,
  55. enabled: hostservRequestsEnabled,
  56. minParams: 1,
  57. },
  58. "status": {
  59. handler: hsStatusHandler,
  60. help: `Syntax: $bSTATUS [user]$b
  61. STATUS displays your current vhost, if any, and the status of your most recent
  62. request for a new one. A server operator can view someone else's status.`,
  63. helpShort: `$bSTATUS$b shows your vhost and request status.`,
  64. enabled: hostservEnabled,
  65. },
  66. "set": {
  67. handler: hsSetHandler,
  68. help: `Syntax: $bSET <user> <vhost>$b
  69. SET sets a user's vhost, bypassing the request system.`,
  70. helpShort: `$bSET$b sets a user's vhost.`,
  71. capabs: []string{"vhosts"},
  72. enabled: hostservEnabled,
  73. minParams: 2,
  74. },
  75. "del": {
  76. handler: hsSetHandler,
  77. help: `Syntax: $bDEL <user>$b
  78. DEL deletes a user's vhost.`,
  79. helpShort: `$bDEL$b deletes a user's vhost.`,
  80. capabs: []string{"vhosts"},
  81. enabled: hostservEnabled,
  82. minParams: 1,
  83. },
  84. "waiting": {
  85. handler: hsWaitingHandler,
  86. help: `Syntax: $bWAITING$b
  87. WAITING shows a list of pending vhost requests, which can then be approved
  88. or rejected.`,
  89. helpShort: `$bWAITING$b shows a list of pending vhost requests.`,
  90. capabs: []string{"vhosts"},
  91. enabled: hostservEnabled,
  92. },
  93. "approve": {
  94. handler: hsApproveHandler,
  95. help: `Syntax: $bAPPROVE <user>$b
  96. APPROVE approves a user's vhost request.`,
  97. helpShort: `$bAPPROVE$b approves a user's vhost request.`,
  98. capabs: []string{"vhosts"},
  99. enabled: hostservEnabled,
  100. minParams: 1,
  101. },
  102. "reject": {
  103. handler: hsRejectHandler,
  104. help: `Syntax: $bREJECT <user> [<reason>]$b
  105. REJECT rejects a user's vhost request, optionally giving them a reason
  106. for the rejection.`,
  107. helpShort: `$bREJECT$b rejects a user's vhost request.`,
  108. capabs: []string{"vhosts"},
  109. enabled: hostservEnabled,
  110. minParams: 1,
  111. maxParams: 2,
  112. unsplitFinalParam: true,
  113. },
  114. "setcloaksecret": {
  115. handler: hsSetCloakSecretHandler,
  116. help: `Syntax: $bSETCLOAKSECRET$b <secret> [code]
  117. SETCLOAKSECRET can be used to set or rotate the cloak secret. You should use
  118. a cryptographically strong secret. To prevent accidental modification, a
  119. verification code is required; invoking the command without a code will
  120. display the necessary code.`,
  121. helpShort: `$bSETCLOAKSECRET$b modifies the IP cloaking secret.`,
  122. capabs: []string{"vhosts", "rehash"},
  123. minParams: 1,
  124. maxParams: 2,
  125. },
  126. }
  127. )
  128. // hsNotice sends the client a notice from HostServ
  129. func hsNotice(rb *ResponseBuffer, text string) {
  130. rb.Add(nil, hsNickMask, "NOTICE", rb.target.Nick(), text)
  131. }
  132. // hsNotifyChannel notifies the designated channel of new vhost activity
  133. func hsNotifyChannel(server *Server, message string) {
  134. chname := server.Config().Accounts.VHosts.UserRequests.Channel
  135. channel := server.channels.Get(chname)
  136. if channel == nil {
  137. return
  138. }
  139. chname = channel.Name()
  140. for _, client := range channel.Members() {
  141. client.Send(nil, hsNickMask, "PRIVMSG", chname, message)
  142. }
  143. }
  144. func hsOnOffHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  145. enable := false
  146. if command == "on" {
  147. enable = true
  148. }
  149. _, err := server.accounts.VHostSetEnabled(client, enable)
  150. if err == errNoVhost {
  151. hsNotice(rb, client.t(err.Error()))
  152. } else if err != nil {
  153. hsNotice(rb, client.t("An error occurred"))
  154. } else if enable {
  155. hsNotice(rb, client.t("Successfully enabled your vhost"))
  156. } else {
  157. hsNotice(rb, client.t("Successfully disabled your vhost"))
  158. }
  159. }
  160. func hsRequestHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  161. vhost := params[0]
  162. if validateVhost(server, vhost, false) != nil {
  163. hsNotice(rb, client.t("Invalid vhost"))
  164. return
  165. }
  166. accountName := client.Account()
  167. _, err := server.accounts.VHostRequest(accountName, vhost, time.Duration(server.Config().Accounts.VHosts.UserRequests.Cooldown))
  168. if err != nil {
  169. if throttled, ok := err.(*vhostThrottleExceeded); ok {
  170. hsNotice(rb, fmt.Sprintf(client.t("You must wait an additional %v before making another request"), throttled.timeRemaining))
  171. } else {
  172. hsNotice(rb, client.t("An error occurred"))
  173. }
  174. } else {
  175. hsNotice(rb, client.t("Your vhost request will be reviewed by an administrator"))
  176. chanMsg := fmt.Sprintf("Account %s requests vhost %s", accountName, vhost)
  177. hsNotifyChannel(server, chanMsg)
  178. server.snomasks.Send(sno.LocalVhosts, chanMsg)
  179. }
  180. }
  181. func hsStatusHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  182. var accountName string
  183. if len(params) > 0 {
  184. if !client.HasRoleCapabs("vhosts") {
  185. hsNotice(rb, client.t("Command restricted"))
  186. return
  187. }
  188. accountName = params[0]
  189. } else {
  190. accountName = client.Account()
  191. if accountName == "" {
  192. hsNotice(rb, client.t("You're not logged into an account"))
  193. return
  194. }
  195. }
  196. account, err := server.accounts.LoadAccount(accountName)
  197. if err != nil {
  198. if err != errAccountDoesNotExist {
  199. server.logger.Warning("internal", "error loading account info", accountName, err.Error())
  200. }
  201. hsNotice(rb, client.t("No such account"))
  202. return
  203. }
  204. if account.VHost.ApprovedVHost != "" {
  205. hsNotice(rb, fmt.Sprintf(client.t("Account %[1]s has vhost: %[2]s"), accountName, account.VHost.ApprovedVHost))
  206. if !account.VHost.Enabled {
  207. hsNotice(rb, client.t("This vhost is currently disabled, but can be enabled with /HS ON"))
  208. }
  209. } else {
  210. hsNotice(rb, fmt.Sprintf(client.t("Account %s has no vhost"), accountName))
  211. }
  212. if account.VHost.RequestedVHost != "" {
  213. hsNotice(rb, fmt.Sprintf(client.t("A request is pending for vhost: %s"), account.VHost.RequestedVHost))
  214. }
  215. if account.VHost.RejectedVHost != "" {
  216. hsNotice(rb, fmt.Sprintf(client.t("A request was previously made for vhost: %s"), account.VHost.RejectedVHost))
  217. hsNotice(rb, fmt.Sprintf(client.t("It was rejected for reason: %s"), account.VHost.RejectionReason))
  218. }
  219. }
  220. func validateVhost(server *Server, vhost string, oper bool) error {
  221. config := server.Config()
  222. if len(vhost) > config.Accounts.VHosts.MaxLength {
  223. return errVHostTooLong
  224. }
  225. if !config.Accounts.VHosts.ValidRegexp.MatchString(vhost) {
  226. return errVHostBadCharacters
  227. }
  228. return nil
  229. }
  230. func hsSetHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  231. user := params[0]
  232. var vhost string
  233. if command == "set" {
  234. vhost = params[1]
  235. if validateVhost(server, vhost, true) != nil {
  236. hsNotice(rb, client.t("Invalid vhost"))
  237. return
  238. }
  239. }
  240. // else: command == "del", vhost == ""
  241. _, err := server.accounts.VHostSet(user, vhost)
  242. if err != nil {
  243. hsNotice(rb, client.t("An error occurred"))
  244. } else if vhost != "" {
  245. hsNotice(rb, client.t("Successfully set vhost"))
  246. } else {
  247. hsNotice(rb, client.t("Successfully cleared vhost"))
  248. }
  249. }
  250. func hsWaitingHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  251. requests, total := server.accounts.VHostListRequests(10)
  252. hsNotice(rb, fmt.Sprintf(client.t("There are %[1]d pending requests for vhosts (%[2]d displayed)"), total, len(requests)))
  253. for i, request := range requests {
  254. hsNotice(rb, fmt.Sprintf(client.t("%[1]d. User %[2]s requests vhost: %[3]s"), i+1, request.Account, request.RequestedVHost))
  255. }
  256. }
  257. func hsApproveHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  258. user := params[0]
  259. vhostInfo, err := server.accounts.VHostApprove(user)
  260. if err != nil {
  261. hsNotice(rb, client.t("An error occurred"))
  262. } else {
  263. hsNotice(rb, fmt.Sprintf(client.t("Successfully approved vhost request for %s"), user))
  264. chanMsg := fmt.Sprintf("Oper %[1]s approved vhost %[2]s for account %[3]s", client.Nick(), vhostInfo.ApprovedVHost, user)
  265. hsNotifyChannel(server, chanMsg)
  266. server.snomasks.Send(sno.LocalVhosts, chanMsg)
  267. for _, client := range server.accounts.AccountToClients(user) {
  268. client.Send(nil, hsNickMask, "NOTICE", client.Nick(), client.t("Your vhost request was approved by an administrator"))
  269. }
  270. }
  271. }
  272. func hsRejectHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  273. var reason string
  274. user := params[0]
  275. if len(params) > 1 {
  276. reason = params[1]
  277. }
  278. vhostInfo, err := server.accounts.VHostReject(user, reason)
  279. if err != nil {
  280. hsNotice(rb, client.t("An error occurred"))
  281. } else {
  282. hsNotice(rb, fmt.Sprintf(client.t("Successfully rejected vhost request for %s"), user))
  283. chanMsg := fmt.Sprintf("Oper %s rejected vhost %s for account %s, with the reason: %v", client.Nick(), vhostInfo.RejectedVHost, user, reason)
  284. hsNotifyChannel(server, chanMsg)
  285. server.snomasks.Send(sno.LocalVhosts, chanMsg)
  286. for _, client := range server.accounts.AccountToClients(user) {
  287. if reason == "" {
  288. client.Send(nil, hsNickMask, "NOTICE", client.Nick(), client.t("Your vhost request was rejected by an administrator"))
  289. } else {
  290. client.Send(nil, hsNickMask, "NOTICE", client.Nick(), fmt.Sprintf(client.t("Your vhost request was rejected by an administrator. The reason given was: %s"), reason))
  291. }
  292. }
  293. }
  294. }
  295. func hsSetCloakSecretHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  296. secret := params[0]
  297. expectedCode := utils.ConfirmationCode(secret, server.ctime)
  298. if len(params) == 1 || params[1] != expectedCode {
  299. hsNotice(rb, ircfmt.Unescape(client.t("$bWarning: changing the cloak secret will invalidate stored ban/invite/exception lists.$b")))
  300. hsNotice(rb, fmt.Sprintf(client.t("To confirm, run this command: %s"), fmt.Sprintf("/HS SETCLOAKSECRET %s %s", secret, expectedCode)))
  301. return
  302. }
  303. StoreCloakSecret(server.store, secret)
  304. hsNotice(rb, client.t("Rotated the cloak secret; you must rehash or restart the server for it to take effect"))
  305. }