Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

nickserv.go 14KB

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