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.

nickserv.go 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "fmt"
  6. "sort"
  7. "strings"
  8. "github.com/goshuirc/irc-go/ircfmt"
  9. "github.com/oragono/oragono/irc/modes"
  10. "github.com/oragono/oragono/irc/utils"
  11. )
  12. const nickservHelp = `NickServ lets you register and login to an account.
  13. To see in-depth help for a specific NickServ command, try:
  14. $b/NS HELP <command>$b
  15. Here are the commands you can use:
  16. %s`
  17. type nsCommand struct {
  18. capabs []string // oper capabs the given user has to have to access this command
  19. handler func(server *Server, client *Client, command, params string, rb *ResponseBuffer)
  20. help string
  21. helpShort string
  22. nickReservation bool // nick reservation must be enabled to use this command
  23. oper bool // true if the user has to be an oper to use this command
  24. }
  25. var (
  26. nickservCommands = map[string]*nsCommand{
  27. "drop": {
  28. handler: nsDropHandler,
  29. help: `Syntax: $bDROP [nickname]$b
  30. DROP de-links the given (or your current) nickname from your user account.`,
  31. helpShort: `$bDROP$b de-links your current (or the given) nickname from your user account.`,
  32. nickReservation: true,
  33. },
  34. "ghost": {
  35. handler: nsGhostHandler,
  36. help: `Syntax: $bGHOST <nickname>$b
  37. GHOST disconnects the given user from the network if they're logged in with the
  38. same user account, letting you reclaim your nickname.`,
  39. helpShort: `$bGHOST$b reclaims your nickname.`,
  40. },
  41. "group": {
  42. handler: nsGroupHandler,
  43. help: `Syntax: $bGROUP$b
  44. GROUP links your current nickname with your logged-in account, preventing other
  45. users from changing to it (or forcing them to rename).`,
  46. helpShort: `$bGROUP$b links your current nickname to your user account.`,
  47. nickReservation: true,
  48. },
  49. "help": {
  50. help: `Syntax: $bHELP [command]$b
  51. HELP returns information on the given command.`,
  52. helpShort: `$bHELP$b shows in-depth information about commands.`,
  53. },
  54. "identify": {
  55. handler: nsIdentifyHandler,
  56. help: `Syntax: $bIDENTIFY <username> [password]$b
  57. IDENTIFY lets you login to the given username using either password auth, or
  58. certfp (your client certificate) if a password is not given.`,
  59. helpShort: `$bIDENTIFY$b lets you login to your account.`,
  60. },
  61. "info": {
  62. handler: nsInfoHandler,
  63. help: `Syntax: $bINFO [username]$b
  64. INFO gives you information about the given (or your own) user account.`,
  65. helpShort: `$bINFO$b gives you information on a user account.`,
  66. },
  67. "register": {
  68. handler: nsRegisterHandler,
  69. // TODO: "email" is an oversimplification here; it's actually any callback, e.g.,
  70. // person@example.com, mailto:person@example.com, tel:16505551234.
  71. help: `Syntax: $bREGISTER <username> <email> [password]$b
  72. REGISTER lets you register a user account. If the server allows anonymous
  73. registration, you can send an asterisk (*) as the email address.
  74. If the password is left out, your account will be registered to your TLS client
  75. certificate (and you will need to use that certificate to login in future).`,
  76. helpShort: `$bREGISTER$b lets you register a user account.`,
  77. },
  78. "sadrop": {
  79. handler: nsDropHandler,
  80. help: `Syntax: $bSADROP <nickname>$b
  81. SADROP foribly de-links the given nickname from the attached user account.`,
  82. helpShort: `$bSADROP$b forcibly de-links the given nickname from its user account.`,
  83. nickReservation: true,
  84. capabs: []string{"unregister"},
  85. },
  86. "unregister": {
  87. handler: nsUnregisterHandler,
  88. help: `Syntax: $bUNREGISTER [username]$b
  89. UNREGISTER lets you delete your user account (or the given one, if you're an
  90. IRC operator with the correct permissions).`,
  91. helpShort: `$bUNREGISTER$b lets you delete your user account.`,
  92. },
  93. "verify": {
  94. handler: nsVerifyHandler,
  95. help: `Syntax: $bVERIFY <username> <code>$b
  96. VERIFY lets you complete an account registration, if the server requires email
  97. or other verification.`,
  98. helpShort: `$bVERIFY$b lets you complete account registration.`,
  99. },
  100. }
  101. )
  102. // nsNotice sends the client a notice from NickServ
  103. func nsNotice(rb *ResponseBuffer, text string) {
  104. rb.Add(nil, "NickServ", "NOTICE", rb.target.Nick(), text)
  105. }
  106. // nickservNoticeHandler handles NOTICEs that NickServ receives.
  107. func (server *Server) nickservNoticeHandler(client *Client, message string, rb *ResponseBuffer) {
  108. // do nothing
  109. }
  110. // nickservPrivmsgHandler handles PRIVMSGs that NickServ receives.
  111. func (server *Server) nickservPrivmsgHandler(client *Client, message string, rb *ResponseBuffer) {
  112. commandName, params := utils.ExtractParam(message)
  113. commandName = strings.ToLower(commandName)
  114. commandInfo := nickservCommands[commandName]
  115. if commandInfo == nil {
  116. nsNotice(rb, client.t("Unknown command. To see available commands, run /NS HELP"))
  117. return
  118. }
  119. if commandInfo.oper && !client.HasMode(modes.Operator) {
  120. nsNotice(rb, client.t("Command restricted"))
  121. return
  122. }
  123. if 0 < len(commandInfo.capabs) && !client.HasRoleCapabs(commandInfo.capabs...) {
  124. nsNotice(rb, client.t("Command restricted"))
  125. return
  126. }
  127. if commandInfo.nickReservation && !server.AccountConfig().Registration.Enabled {
  128. nsNotice(rb, client.t("Account registration has been disabled"))
  129. return
  130. }
  131. // custom help handling here to prevent recursive init loop
  132. if commandName == "help" {
  133. nsHelpHandler(server, client, commandName, params, rb)
  134. return
  135. }
  136. if commandInfo.handler == nil {
  137. nsNotice(rb, client.t("Command error. Please report this to the developers"))
  138. return
  139. }
  140. server.logger.Debug("nickserv", fmt.Sprintf("Client %s ran command %s", client.Nick(), commandName))
  141. commandInfo.handler(server, client, commandName, params, rb)
  142. }
  143. func nsDropHandler(server *Server, client *Client, command, params string, rb *ResponseBuffer) {
  144. sadrop := command == "sadrop"
  145. nick, _ := utils.ExtractParam(params)
  146. err := server.accounts.SetNickReserved(client, nick, sadrop, false)
  147. if err == nil {
  148. nsNotice(rb, fmt.Sprintf(client.t("Successfully ungrouped nick %s with your account"), nick))
  149. } else if err == errAccountNotLoggedIn {
  150. nsNotice(rb, client.t("You're not logged into an account"))
  151. } else if err == errAccountCantDropPrimaryNick {
  152. nsNotice(rb, client.t("You can't ungroup your primary nickname (try unregistering your account instead)"))
  153. } else if err == errNicknameReserved {
  154. nsNotice(rb, client.t("That nickname is already reserved by someone else"))
  155. } else {
  156. nsNotice(rb, client.t("Could not ungroup nick"))
  157. }
  158. }
  159. func nsGhostHandler(server *Server, client *Client, command, params string, rb *ResponseBuffer) {
  160. nick, _ := utils.ExtractParam(params)
  161. ghost := server.clients.Get(nick)
  162. if ghost == nil {
  163. nsNotice(rb, client.t("No such nick"))
  164. return
  165. } else if ghost == client {
  166. nsNotice(rb, client.t("You can't GHOST yourself (try /QUIT instead)"))
  167. return
  168. }
  169. authorized := false
  170. account := client.Account()
  171. if account != "" {
  172. // the user must either own the nick, or the target client
  173. authorized = (server.accounts.NickToAccount(nick) == account) || (ghost.Account() == account)
  174. }
  175. if !authorized {
  176. nsNotice(rb, client.t("You don't own that nick"))
  177. return
  178. }
  179. ghost.Quit(fmt.Sprintf(ghost.t("GHOSTed by %s"), client.Nick()))
  180. ghost.destroy(false)
  181. }
  182. func nsGroupHandler(server *Server, client *Client, command, params string, rb *ResponseBuffer) {
  183. account := client.Account()
  184. if account == "" {
  185. nsNotice(rb, client.t("You're not logged into an account"))
  186. return
  187. }
  188. nick := client.NickCasefolded()
  189. err := server.accounts.SetNickReserved(client, nick, false, true)
  190. if err == nil {
  191. nsNotice(rb, fmt.Sprintf(client.t("Successfully grouped nick %s with your account"), nick))
  192. } else if err == errAccountTooManyNicks {
  193. nsNotice(rb, client.t("You have too many nicks reserved already (you can remove some with /NS DROP)"))
  194. } else if err == errNicknameReserved {
  195. nsNotice(rb, client.t("That nickname is already reserved by someone else"))
  196. } else {
  197. nsNotice(rb, client.t("Error reserving nickname"))
  198. }
  199. }
  200. func nsHelpHandler(server *Server, client *Client, command, params string, rb *ResponseBuffer) {
  201. nsNotice(rb, ircfmt.Unescape(client.t("*** $bNickServ HELP$b ***")))
  202. if params == "" {
  203. // show general help
  204. var shownHelpLines sort.StringSlice
  205. for _, commandInfo := range nickservCommands {
  206. // skip commands user can't access
  207. if commandInfo.oper && !client.HasMode(modes.Operator) {
  208. continue
  209. }
  210. if 0 < len(commandInfo.capabs) && !client.HasRoleCapabs(commandInfo.capabs...) {
  211. continue
  212. }
  213. if commandInfo.nickReservation && !server.AccountConfig().Registration.Enabled {
  214. continue
  215. }
  216. shownHelpLines = append(shownHelpLines, " "+client.t(commandInfo.helpShort))
  217. }
  218. // sort help lines
  219. sort.Sort(shownHelpLines)
  220. // assemble help text
  221. assembledHelpLines := strings.Join(shownHelpLines, "\n")
  222. fullHelp := ircfmt.Unescape(fmt.Sprintf(client.t(nickservHelp), assembledHelpLines))
  223. // push out help text
  224. for _, line := range strings.Split(fullHelp, "\n") {
  225. nsNotice(rb, line)
  226. }
  227. } else {
  228. commandInfo := nickservCommands[strings.ToLower(strings.TrimSpace(params))]
  229. if commandInfo == nil {
  230. nsNotice(rb, client.t("Unknown command. To see available commands, run /NS HELP"))
  231. } else {
  232. for _, line := range strings.Split(ircfmt.Unescape(client.t(commandInfo.help)), "\n") {
  233. nsNotice(rb, line)
  234. }
  235. }
  236. }
  237. nsNotice(rb, ircfmt.Unescape(client.t("*** $bEnd of NickServ HELP$b ***")))
  238. }
  239. func nsIdentifyHandler(server *Server, client *Client, command, params string, rb *ResponseBuffer) {
  240. // fail out if we need to
  241. if !server.AccountConfig().AuthenticationEnabled {
  242. nsNotice(rb, client.t("Login has been disabled"))
  243. return
  244. }
  245. loginSuccessful := false
  246. username, passphrase := utils.ExtractParam(params)
  247. // try passphrase
  248. if username != "" && passphrase != "" {
  249. err := server.accounts.AuthenticateByPassphrase(client, username, passphrase)
  250. loginSuccessful = (err == nil)
  251. }
  252. // try certfp
  253. if !loginSuccessful && client.certfp != "" {
  254. err := server.accounts.AuthenticateByCertFP(client)
  255. loginSuccessful = (err == nil)
  256. }
  257. if loginSuccessful {
  258. sendSuccessfulSaslAuth(client, rb, true)
  259. } else {
  260. nsNotice(rb, client.t("Could not login with your TLS certificate or supplied username/password"))
  261. }
  262. }
  263. func nsInfoHandler(server *Server, client *Client, command, params string, rb *ResponseBuffer) {
  264. nick, _ := utils.ExtractParam(params)
  265. if nick == "" {
  266. nick = client.Nick()
  267. }
  268. accountName := nick
  269. if server.AccountConfig().NickReservation.Enabled {
  270. accountName = server.accounts.NickToAccount(nick)
  271. if accountName == "" {
  272. nsNotice(rb, client.t("That nickname is not registered"))
  273. return
  274. }
  275. }
  276. account, err := server.accounts.LoadAccount(accountName)
  277. if err != nil || !account.Verified {
  278. nsNotice(rb, client.t("Account does not exist"))
  279. }
  280. nsNotice(rb, fmt.Sprintf(client.t("Account: %s"), account.Name))
  281. registeredAt := account.RegisteredAt.Format("Jan 02, 2006 15:04:05Z")
  282. nsNotice(rb, fmt.Sprintf(client.t("Registered at: %s"), registeredAt))
  283. // TODO nicer formatting for this
  284. for _, nick := range account.AdditionalNicks {
  285. nsNotice(rb, fmt.Sprintf(client.t("Additional grouped nick: %s"), nick))
  286. }
  287. }
  288. func nsRegisterHandler(server *Server, client *Client, command, params string, rb *ResponseBuffer) {
  289. // get params
  290. username, afterUsername := utils.ExtractParam(params)
  291. email, passphrase := utils.ExtractParam(afterUsername)
  292. if !server.AccountConfig().Registration.Enabled {
  293. nsNotice(rb, client.t("Account registration has been disabled"))
  294. return
  295. }
  296. if username == "" {
  297. nsNotice(rb, client.t("No username supplied"))
  298. return
  299. }
  300. certfp := client.certfp
  301. if passphrase == "" && certfp == "" {
  302. nsNotice(rb, client.t("You need to either supply a passphrase or be connected via TLS with a client cert"))
  303. return
  304. }
  305. if client.LoggedIntoAccount() {
  306. if server.AccountConfig().Registration.AllowMultiplePerConnection {
  307. server.accounts.Logout(client)
  308. } else {
  309. nsNotice(rb, client.t("You're already logged into an account"))
  310. return
  311. }
  312. }
  313. config := server.AccountConfig()
  314. var callbackNamespace, callbackValue string
  315. noneCallbackAllowed := false
  316. for _, callback := range config.Registration.EnabledCallbacks {
  317. if callback == "*" {
  318. noneCallbackAllowed = true
  319. }
  320. }
  321. // XXX if ACC REGISTER allows registration with the `none` callback, then ignore
  322. // any callback that was passed here (to avoid confusion in the case where the ircd
  323. // has no mail server configured). otherwise, register using the provided callback:
  324. if noneCallbackAllowed {
  325. callbackNamespace = "*"
  326. } else {
  327. callbackNamespace, callbackValue = parseCallback(email, config)
  328. if callbackNamespace == "" {
  329. nsNotice(rb, client.t("Registration requires a valid e-mail address"))
  330. return
  331. }
  332. }
  333. // get and sanitise account name
  334. account := strings.TrimSpace(username)
  335. err := server.accounts.Register(client, account, callbackNamespace, callbackValue, passphrase, client.certfp)
  336. if err == nil {
  337. if callbackNamespace == "*" {
  338. err = server.accounts.Verify(client, account, "")
  339. if err == nil {
  340. sendSuccessfulRegResponse(client, rb, true)
  341. }
  342. } else {
  343. messageTemplate := client.t("Account created, pending verification; verification code has been sent to %s:%s")
  344. message := fmt.Sprintf(messageTemplate, callbackNamespace, callbackValue)
  345. nsNotice(rb, message)
  346. }
  347. }
  348. // details could not be stored and relevant numerics have been dispatched, abort
  349. if err != nil {
  350. errMsg := client.t("Could not register")
  351. if err == errCertfpAlreadyExists {
  352. errMsg = client.t("An account already exists for your certificate fingerprint")
  353. } else if err == errAccountAlreadyRegistered {
  354. errMsg = client.t("Account already exists")
  355. }
  356. nsNotice(rb, errMsg)
  357. return
  358. }
  359. }
  360. func nsUnregisterHandler(server *Server, client *Client, command, params string, rb *ResponseBuffer) {
  361. username, _ := utils.ExtractParam(params)
  362. if !server.AccountConfig().Registration.Enabled {
  363. nsNotice(rb, client.t("Account registration has been disabled"))
  364. return
  365. }
  366. if username == "" {
  367. username = client.Account()
  368. }
  369. if username == "" {
  370. nsNotice(rb, client.t("You're not logged into an account"))
  371. return
  372. }
  373. cfname, err := CasefoldName(username)
  374. if err != nil {
  375. nsNotice(rb, client.t("Invalid username"))
  376. return
  377. }
  378. if !(cfname == client.Account() || client.HasRoleCapabs("unregister")) {
  379. nsNotice(rb, client.t("Insufficient oper privs"))
  380. return
  381. }
  382. if cfname == client.Account() {
  383. client.server.accounts.Logout(client)
  384. }
  385. err = server.accounts.Unregister(cfname)
  386. if err == errAccountDoesNotExist {
  387. nsNotice(rb, client.t(err.Error()))
  388. } else if err != nil {
  389. nsNotice(rb, client.t("Error while unregistering account"))
  390. } else {
  391. nsNotice(rb, fmt.Sprintf(client.t("Successfully unregistered account %s"), cfname))
  392. }
  393. }
  394. func nsVerifyHandler(server *Server, client *Client, command, params string, rb *ResponseBuffer) {
  395. username, code := utils.ExtractParam(params)
  396. err := server.accounts.Verify(client, username, code)
  397. var errorMessage string
  398. if err == errAccountVerificationInvalidCode || err == errAccountAlreadyVerified {
  399. errorMessage = err.Error()
  400. } else if err != nil {
  401. errorMessage = errAccountVerificationFailed.Error()
  402. }
  403. if errorMessage != "" {
  404. nsNotice(rb, client.t(errorMessage))
  405. return
  406. }
  407. sendSuccessfulRegResponse(client, rb, true)
  408. }