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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/oragono/oragono/irc/sno"
  9. )
  10. const hostservHelp = `HostServ lets you manage your vhost (i.e., the string displayed
  11. in place of your client's hostname/IP).`
  12. var (
  13. errVHostBadCharacters = errors.New("Vhost contains prohibited characters")
  14. errVHostTooLong = errors.New("Vhost is too long")
  15. // ascii only for now
  16. defaultValidVhostRegex = regexp.MustCompile(`^[0-9A-Za-z.\-_/]+$`)
  17. )
  18. func hostservEnabled(config *Config) bool {
  19. return config.Accounts.VHosts.Enabled
  20. }
  21. func hostservRequestsEnabled(config *Config) bool {
  22. return config.Accounts.VHosts.Enabled && config.Accounts.VHosts.UserRequests.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. "request": {
  43. handler: hsRequestHandler,
  44. help: `Syntax: $bREQUEST <vhost>$b
  45. REQUEST requests that a new vhost by assigned to your account. The request must
  46. then be approved by a server operator.`,
  47. helpShort: `$bREQUEST$b requests a new vhost, pending operator approval.`,
  48. authRequired: true,
  49. enabled: hostservRequestsEnabled,
  50. minParams: 1,
  51. },
  52. "status": {
  53. handler: hsStatusHandler,
  54. help: `Syntax: $bSTATUS [user]$b
  55. STATUS displays your current vhost, if any, and the status of your most recent
  56. request for a new one. A server operator can view someone else's status.`,
  57. helpShort: `$bSTATUS$b shows your vhost and request status.`,
  58. enabled: hostservEnabled,
  59. },
  60. "set": {
  61. handler: hsSetHandler,
  62. help: `Syntax: $bSET <user> <vhost>$b
  63. SET sets a user's vhost, bypassing the request system.`,
  64. helpShort: `$bSET$b sets a user's vhost.`,
  65. capabs: []string{"vhosts"},
  66. enabled: hostservEnabled,
  67. minParams: 2,
  68. },
  69. "del": {
  70. handler: hsSetHandler,
  71. help: `Syntax: $bDEL <user>$b
  72. DEL deletes a user's vhost.`,
  73. helpShort: `$bDEL$b deletes a user's vhost.`,
  74. capabs: []string{"vhosts"},
  75. enabled: hostservEnabled,
  76. minParams: 1,
  77. },
  78. "waiting": {
  79. handler: hsWaitingHandler,
  80. help: `Syntax: $bWAITING$b
  81. WAITING shows a list of pending vhost requests, which can then be approved
  82. or rejected.`,
  83. helpShort: `$bWAITING$b shows a list of pending vhost requests.`,
  84. capabs: []string{"vhosts"},
  85. enabled: hostservEnabled,
  86. },
  87. "approve": {
  88. handler: hsApproveHandler,
  89. help: `Syntax: $bAPPROVE <user>$b
  90. APPROVE approves a user's vhost request.`,
  91. helpShort: `$bAPPROVE$b approves a user's vhost request.`,
  92. capabs: []string{"vhosts"},
  93. enabled: hostservEnabled,
  94. minParams: 1,
  95. },
  96. "reject": {
  97. handler: hsRejectHandler,
  98. help: `Syntax: $bREJECT <user> [<reason>]$b
  99. REJECT rejects a user's vhost request, optionally giving them a reason
  100. for the rejection.`,
  101. helpShort: `$bREJECT$b rejects a user's vhost request.`,
  102. capabs: []string{"vhosts"},
  103. enabled: hostservEnabled,
  104. minParams: 1,
  105. maxParams: 2,
  106. unsplitFinalParam: true,
  107. },
  108. "forbid": {
  109. handler: hsForbidHandler,
  110. help: `Syntax: $bFORBID <user>$b
  111. FORBID prevents a user from using any vhost, including ones on the offer list.`,
  112. helpShort: `$bFORBID$b prevents a user from using vhosts.`,
  113. capabs: []string{"vhosts"},
  114. enabled: hostservEnabled,
  115. minParams: 1,
  116. maxParams: 1,
  117. },
  118. "permit": {
  119. handler: hsForbidHandler,
  120. help: `Syntax: $bPERMIT <user>$b
  121. PERMIT undoes FORBID, allowing the user to TAKE vhosts again.`,
  122. helpShort: `$bPERMIT$b allows a user to use vhosts again.`,
  123. capabs: []string{"vhosts"},
  124. enabled: hostservEnabled,
  125. minParams: 1,
  126. maxParams: 1,
  127. },
  128. "offerlist": {
  129. handler: hsOfferListHandler,
  130. help: `Syntax: $bOFFERLIST$b
  131. OFFERLIST lists vhosts that can be chosen without requiring operator approval;
  132. to use one of the listed vhosts, take it with /HOSTSERV TAKE.`,
  133. helpShort: `$bOFFERLIST$b lists vhosts that can be taken without operator approval.`,
  134. enabled: hostservEnabled,
  135. minParams: 0,
  136. maxParams: 0,
  137. },
  138. "take": {
  139. handler: hsTakeHandler,
  140. help: `Syntax: $bTAKE$b <vhost>
  141. TAKE sets your vhost to one of the vhosts in the server's offer list; to see
  142. the offered vhosts, use /HOSTSERV OFFERLIST.`,
  143. helpShort: `$bTAKE$b sets your vhost to one of the options from the offer list.`,
  144. enabled: hostservEnabled,
  145. authRequired: true,
  146. minParams: 1,
  147. maxParams: 1,
  148. },
  149. }
  150. )
  151. // hsNotice sends the client a notice from HostServ
  152. func hsNotice(rb *ResponseBuffer, text string) {
  153. rb.Add(nil, "HostServ!HostServ@localhost", "NOTICE", rb.target.Nick(), text)
  154. }
  155. // hsNotifyChannel notifies the designated channel of new vhost activity
  156. func hsNotifyChannel(server *Server, message string) {
  157. chname := server.AccountConfig().VHosts.UserRequests.Channel
  158. channel := server.channels.Get(chname)
  159. if channel == nil {
  160. return
  161. }
  162. chname = channel.Name()
  163. for _, client := range channel.Members() {
  164. client.Send(nil, "HostServ", "PRIVMSG", chname, message)
  165. }
  166. }
  167. func hsOnOffHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  168. enable := false
  169. if command == "on" {
  170. enable = true
  171. }
  172. _, err := server.accounts.VHostSetEnabled(client, enable)
  173. if err == errNoVhost {
  174. hsNotice(rb, client.t(err.Error()))
  175. } else if err != nil {
  176. hsNotice(rb, client.t("An error occurred"))
  177. } else if enable {
  178. hsNotice(rb, client.t("Successfully enabled your vhost"))
  179. } else {
  180. hsNotice(rb, client.t("Successfully disabled your vhost"))
  181. }
  182. }
  183. func hsRequestHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  184. vhost := params[0]
  185. if validateVhost(server, vhost, false) != nil {
  186. hsNotice(rb, client.t("Invalid vhost"))
  187. return
  188. }
  189. accountName := client.Account()
  190. _, err := server.accounts.VHostRequest(accountName, vhost, server.Config().Accounts.VHosts.UserRequests.Cooldown)
  191. if err != nil {
  192. if throttled, ok := err.(*vhostThrottleExceeded); ok {
  193. hsNotice(rb, fmt.Sprintf(client.t("You must wait an additional %v before making another request"), throttled.timeRemaining))
  194. } else if err == errVhostsForbidden {
  195. hsNotice(rb, client.t("An administrator has denied you the ability to use vhosts"))
  196. } else {
  197. hsNotice(rb, client.t("An error occurred"))
  198. }
  199. } else {
  200. hsNotice(rb, client.t("Your vhost request will be reviewed by an administrator"))
  201. chanMsg := fmt.Sprintf("Account %s requests vhost %s", accountName, vhost)
  202. hsNotifyChannel(server, chanMsg)
  203. server.snomasks.Send(sno.LocalVhosts, chanMsg)
  204. }
  205. }
  206. func hsStatusHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  207. var accountName string
  208. if len(params) > 0 {
  209. if !client.HasRoleCapabs("vhosts") {
  210. hsNotice(rb, client.t("Command restricted"))
  211. return
  212. }
  213. accountName = params[0]
  214. } else {
  215. accountName = client.Account()
  216. if accountName == "" {
  217. hsNotice(rb, client.t("You're not logged into an account"))
  218. return
  219. }
  220. }
  221. account, err := server.accounts.LoadAccount(accountName)
  222. if err != nil {
  223. if err != errAccountDoesNotExist {
  224. server.logger.Warning("internal", "error loading account info", accountName, err.Error())
  225. }
  226. hsNotice(rb, client.t("No such account"))
  227. return
  228. }
  229. if account.VHost.Forbidden {
  230. hsNotice(rb, client.t("An administrator has denied you the ability to use vhosts"))
  231. return
  232. }
  233. if account.VHost.ApprovedVHost != "" {
  234. hsNotice(rb, fmt.Sprintf(client.t("Account %[1]s has vhost: %[2]s"), accountName, account.VHost.ApprovedVHost))
  235. if !account.VHost.Enabled {
  236. hsNotice(rb, client.t("This vhost is currently disabled, but can be enabled with /HS ON"))
  237. }
  238. } else {
  239. hsNotice(rb, fmt.Sprintf(client.t("Account %s has no vhost"), accountName))
  240. }
  241. if account.VHost.RequestedVHost != "" {
  242. hsNotice(rb, fmt.Sprintf(client.t("A request is pending for vhost: %s"), account.VHost.RequestedVHost))
  243. }
  244. if account.VHost.RejectedVHost != "" {
  245. hsNotice(rb, fmt.Sprintf(client.t("A request was previously made for vhost: %s"), account.VHost.RejectedVHost))
  246. hsNotice(rb, fmt.Sprintf(client.t("It was rejected for reason: %s"), account.VHost.RejectionReason))
  247. }
  248. }
  249. func validateVhost(server *Server, vhost string, oper bool) error {
  250. ac := server.AccountConfig()
  251. if len(vhost) > ac.VHosts.MaxLength {
  252. return errVHostTooLong
  253. }
  254. if !ac.VHosts.ValidRegexp.MatchString(vhost) {
  255. return errVHostBadCharacters
  256. }
  257. return nil
  258. }
  259. func hsSetHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  260. user := params[0]
  261. var vhost string
  262. if command == "set" {
  263. vhost = params[1]
  264. if validateVhost(server, vhost, true) != nil {
  265. hsNotice(rb, client.t("Invalid vhost"))
  266. return
  267. }
  268. }
  269. // else: command == "del", vhost == ""
  270. _, err := server.accounts.VHostSet(user, vhost)
  271. if err != nil {
  272. hsNotice(rb, client.t("An error occurred"))
  273. } else if vhost != "" {
  274. hsNotice(rb, client.t("Successfully set vhost"))
  275. } else {
  276. hsNotice(rb, client.t("Successfully cleared vhost"))
  277. }
  278. }
  279. func hsWaitingHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  280. requests, total := server.accounts.VHostListRequests(10)
  281. hsNotice(rb, fmt.Sprintf(client.t("There are %[1]d pending requests for vhosts (%[2]d displayed)"), total, len(requests)))
  282. for i, request := range requests {
  283. hsNotice(rb, fmt.Sprintf(client.t("%[1]d. User %[2]s requests vhost: %[3]s"), i+1, request.Account, request.RequestedVHost))
  284. }
  285. }
  286. func hsApproveHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  287. user := params[0]
  288. vhostInfo, err := server.accounts.VHostApprove(user)
  289. if err != nil {
  290. hsNotice(rb, client.t("An error occurred"))
  291. } else {
  292. hsNotice(rb, fmt.Sprintf(client.t("Successfully approved vhost request for %s"), user))
  293. chanMsg := fmt.Sprintf("Oper %[1]s approved vhost %[2]s for account %[3]s", client.Nick(), vhostInfo.ApprovedVHost, user)
  294. hsNotifyChannel(server, chanMsg)
  295. server.snomasks.Send(sno.LocalVhosts, chanMsg)
  296. for _, client := range server.accounts.AccountToClients(user) {
  297. client.Notice(client.t("Your vhost request was approved by an administrator"))
  298. }
  299. }
  300. }
  301. func hsRejectHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  302. var reason string
  303. user := params[0]
  304. if len(params) > 1 {
  305. reason = params[1]
  306. }
  307. vhostInfo, err := server.accounts.VHostReject(user, reason)
  308. if err != nil {
  309. hsNotice(rb, client.t("An error occurred"))
  310. } else {
  311. hsNotice(rb, fmt.Sprintf(client.t("Successfully rejected vhost request for %s"), user))
  312. chanMsg := fmt.Sprintf("Oper %s rejected vhost %s for account %s, with the reason: %v", client.Nick(), vhostInfo.RejectedVHost, user, reason)
  313. hsNotifyChannel(server, chanMsg)
  314. server.snomasks.Send(sno.LocalVhosts, chanMsg)
  315. for _, client := range server.accounts.AccountToClients(user) {
  316. if reason == "" {
  317. client.Notice("Your vhost request was rejected by an administrator")
  318. } else {
  319. client.Notice(fmt.Sprintf(client.t("Your vhost request was rejected by an administrator. The reason given was: %s"), reason))
  320. }
  321. }
  322. }
  323. }
  324. func hsForbidHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  325. user := params[0]
  326. forbidden := command == "forbid"
  327. _, err := server.accounts.VHostForbid(user, forbidden)
  328. if err == errAccountDoesNotExist {
  329. hsNotice(rb, client.t("No such account"))
  330. } else if err != nil {
  331. hsNotice(rb, client.t("An error occurred"))
  332. } else {
  333. if forbidden {
  334. hsNotice(rb, fmt.Sprintf(client.t("User %s is no longer allowed to use vhosts"), user))
  335. } else {
  336. hsNotice(rb, fmt.Sprintf(client.t("User %s is now allowed to use vhosts"), user))
  337. }
  338. }
  339. }
  340. func hsOfferListHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  341. vhostConfig := server.Config().Accounts.VHosts
  342. if len(vhostConfig.OfferList) == 0 {
  343. if vhostConfig.UserRequests.Enabled {
  344. hsNotice(rb, client.t("The server does not offer any vhosts, but you can request one with /HOSTSERV REQUEST"))
  345. } else {
  346. hsNotice(rb, client.t("The server does not offer any vhosts"))
  347. }
  348. } else {
  349. hsNotice(rb, client.t("The following vhosts are available and can be chosen with /HOSTSERV TAKE:"))
  350. for _, vhost := range vhostConfig.OfferList {
  351. hsNotice(rb, vhost)
  352. }
  353. }
  354. }
  355. func hsTakeHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  356. config := server.Config()
  357. vhost := params[0]
  358. found := false
  359. for _, offered := range config.Accounts.VHosts.OfferList {
  360. if offered == vhost {
  361. found = true
  362. }
  363. }
  364. if !found {
  365. hsNotice(rb, client.t("That vhost isn't being offered by the server"))
  366. return
  367. }
  368. account := client.Account()
  369. _, err := server.accounts.VHostTake(account, vhost, config.Accounts.VHosts.UserRequests.Cooldown)
  370. if err != nil {
  371. if throttled, ok := err.(*vhostThrottleExceeded); ok {
  372. hsNotice(rb, fmt.Sprintf(client.t("You must wait an additional %v before taking a vhost"), throttled.timeRemaining))
  373. } else if err == errVhostsForbidden {
  374. hsNotice(rb, client.t("An administrator has denied you the ability to use vhosts"))
  375. } else {
  376. hsNotice(rb, client.t("An error occurred"))
  377. }
  378. } else {
  379. hsNotice(rb, client.t("Successfully set vhost"))
  380. server.snomasks.Send(sno.LocalVhosts, fmt.Sprintf("Client %s (account %s) took vhost %s", client.Nick(), account, vhost))
  381. }
  382. }