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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/goshuirc/irc-go/ircfmt"
  11. "github.com/oragono/oragono/irc/passwd"
  12. "github.com/oragono/oragono/irc/sno"
  13. "github.com/tidwall/buntdb"
  14. )
  15. const nickservHelp = `NickServ lets you register and log into a user account.
  16. To register an account:
  17. /NS REGISTER username [password]
  18. Leave out [password] if you're registering using your client certificate fingerprint.
  19. To login to an account:
  20. /NS IDENTIFY [username password]
  21. Leave out [username password] to use your client certificate fingerprint. Otherwise,
  22. the given username and password will be used.`
  23. func (server *Server) nickservReceiveNotice(client *Client, message string) {
  24. // do nothing
  25. }
  26. // extractParam extracts a parameter from the given string, returning the param and the rest of the string.
  27. func extractParam(line string) (string, string) {
  28. rawParams := strings.SplitN(strings.TrimSpace(line), " ", 2)
  29. param0 := rawParams[0]
  30. var param1 string
  31. if 1 < len(rawParams) {
  32. param1 = strings.TrimSpace(rawParams[1])
  33. }
  34. return param0, param1
  35. }
  36. func (server *Server) nickservReceivePrivmsg(client *Client, message string) {
  37. command, params := extractParam(message)
  38. command = strings.ToLower(command)
  39. if command == "help" {
  40. for _, line := range strings.Split(nickservHelp, "\n") {
  41. client.Notice(line)
  42. }
  43. } else if command == "register" {
  44. // get params
  45. username, passphrase := extractParam(params)
  46. // fail out if we need to
  47. if username == "" {
  48. client.Notice(client.t("No username supplied"))
  49. return
  50. }
  51. certfp := client.certfp
  52. if passphrase == "" && certfp == "" {
  53. client.Notice(client.t("You need to either supply a passphrase or be connected via TLS with a client cert"))
  54. return
  55. }
  56. if !server.accountRegistration.Enabled {
  57. client.Notice(client.t("Account registration has been disabled"))
  58. return
  59. }
  60. if client.LoggedIntoAccount() {
  61. if server.accountRegistration.AllowMultiplePerConnection {
  62. client.LogoutOfAccount()
  63. } else {
  64. client.Notice(client.t("You're already logged into an account"))
  65. return
  66. }
  67. }
  68. // get and sanitise account name
  69. account := strings.TrimSpace(username)
  70. casefoldedAccount, err := CasefoldName(account)
  71. // probably don't need explicit check for "*" here... but let's do it anyway just to make sure
  72. if err != nil || username == "*" {
  73. client.Notice(client.t("Account name is not valid"))
  74. return
  75. }
  76. // check whether account exists
  77. // do it all in one write tx to prevent races
  78. err = server.store.Update(func(tx *buntdb.Tx) error {
  79. accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
  80. _, err := tx.Get(accountKey)
  81. if err != buntdb.ErrNotFound {
  82. //TODO(dan): if account verified key doesn't exist account is not verified, calc the maximum time without verification and expire and continue if need be
  83. client.Notice(client.t("Account already exists"))
  84. return errAccountCreation
  85. }
  86. registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
  87. tx.Set(accountKey, "1", nil)
  88. tx.Set(fmt.Sprintf(keyAccountName, casefoldedAccount), account, nil)
  89. tx.Set(registeredTimeKey, strconv.FormatInt(time.Now().Unix(), 10), nil)
  90. return nil
  91. })
  92. // account could not be created and relevant numerics have been dispatched, abort
  93. if err != nil {
  94. if err != errAccountCreation {
  95. client.Notice(client.t("Account registration failed"))
  96. }
  97. return
  98. }
  99. // store details
  100. err = server.store.Update(func(tx *buntdb.Tx) error {
  101. // certfp special lookup key
  102. if passphrase == "" {
  103. assembledKeyCertToAccount := fmt.Sprintf(keyCertToAccount, client.certfp)
  104. // make sure certfp doesn't already exist because that'd be silly
  105. _, err := tx.Get(assembledKeyCertToAccount)
  106. if err != buntdb.ErrNotFound {
  107. return errCertfpAlreadyExists
  108. }
  109. tx.Set(assembledKeyCertToAccount, casefoldedAccount, nil)
  110. }
  111. // make creds
  112. var creds AccountCredentials
  113. // always set passphrase salt
  114. creds.PassphraseSalt, err = passwd.NewSalt()
  115. if err != nil {
  116. return fmt.Errorf("Could not create passphrase salt: %s", err.Error())
  117. }
  118. if passphrase == "" {
  119. creds.Certificate = client.certfp
  120. } else {
  121. creds.PassphraseHash, err = server.passwords.GenerateFromPassword(creds.PassphraseSalt, passphrase)
  122. if err != nil {
  123. return fmt.Errorf("Could not hash password: %s", err)
  124. }
  125. }
  126. credText, err := json.Marshal(creds)
  127. if err != nil {
  128. return fmt.Errorf("Could not marshal creds: %s", err)
  129. }
  130. tx.Set(fmt.Sprintf(keyAccountCredentials, account), string(credText), nil)
  131. return nil
  132. })
  133. // details could not be stored and relevant numerics have been dispatched, abort
  134. if err != nil {
  135. errMsg := "Could not register"
  136. if err == errCertfpAlreadyExists {
  137. errMsg = "An account already exists for your certificate fingerprint"
  138. }
  139. client.Notice(errMsg)
  140. removeFailedAccRegisterData(server.store, casefoldedAccount)
  141. return
  142. }
  143. err = server.store.Update(func(tx *buntdb.Tx) error {
  144. tx.Set(fmt.Sprintf(keyAccountVerified, casefoldedAccount), "1", nil)
  145. // load acct info inside store tx
  146. account := ClientAccount{
  147. Name: username,
  148. RegisteredAt: time.Now(),
  149. Clients: []*Client{client},
  150. }
  151. //TODO(dan): Consider creating ircd-wide account adding/removing/affecting lock for protecting access to these sorts of variables
  152. server.accounts[casefoldedAccount] = &account
  153. client.account = &account
  154. client.Notice(client.t("Account created"))
  155. client.Send(nil, server.name, RPL_LOGGEDIN, client.nick, client.nickMaskString, account.Name, fmt.Sprintf(client.t("You are now logged in as %s"), account.Name))
  156. client.Send(nil, server.name, RPL_SASLSUCCESS, client.nick, client.t("Authentication successful"))
  157. server.snomasks.Send(sno.LocalAccounts, fmt.Sprintf(ircfmt.Unescape("Account registered $c[grey][$r%s$c[grey]] by $c[grey][$r%s$c[grey]]"), account.Name, client.nickMaskString))
  158. return nil
  159. })
  160. if err != nil {
  161. client.Notice(client.t("Account registration failed"))
  162. removeFailedAccRegisterData(server.store, casefoldedAccount)
  163. return
  164. }
  165. } else if command == "identify" {
  166. // fail out if we need to
  167. if !server.accountAuthenticationEnabled {
  168. client.Notice(client.t("Login has been disabled"))
  169. return
  170. }
  171. // try passphrase
  172. username, passphrase := extractParam(params)
  173. if username != "" && passphrase != "" {
  174. // keep it the same as in the ACC CREATE stage
  175. accountKey, err := CasefoldName(username)
  176. if err != nil {
  177. client.Notice(client.t("Could not login with your username/password"))
  178. return
  179. }
  180. // load and check acct data all in one update to prevent races.
  181. // as noted elsewhere, change to proper locking for Account type later probably
  182. var accountName string
  183. err = server.store.Update(func(tx *buntdb.Tx) error {
  184. // confirm account is verified
  185. _, err = tx.Get(fmt.Sprintf(keyAccountVerified, accountKey))
  186. if err != nil {
  187. return errSaslFail
  188. }
  189. creds, err := loadAccountCredentials(tx, accountKey)
  190. if err != nil {
  191. return err
  192. }
  193. // ensure creds are valid
  194. if len(creds.PassphraseHash) < 1 || len(creds.PassphraseSalt) < 1 || len(passphrase) < 1 {
  195. return errSaslFail
  196. }
  197. err = server.passwords.CompareHashAndPassword(creds.PassphraseHash, creds.PassphraseSalt, passphrase)
  198. // succeeded, load account info if necessary
  199. account, exists := server.accounts[accountKey]
  200. if !exists {
  201. account = loadAccount(server, tx, accountKey)
  202. }
  203. client.LoginToAccount(account)
  204. accountName = account.Name
  205. return err
  206. })
  207. if err == nil {
  208. client.Notice(fmt.Sprintf(client.t("You're now logged in as %s"), accountName))
  209. return
  210. }
  211. }
  212. // try certfp
  213. certfp := client.certfp
  214. if certfp != "" {
  215. var accountName string
  216. err := server.store.Update(func(tx *buntdb.Tx) error {
  217. // certfp lookup key
  218. accountKey, err := tx.Get(fmt.Sprintf(keyCertToAccount, certfp))
  219. if err != nil {
  220. return errSaslFail
  221. }
  222. // confirm account exists
  223. _, err = tx.Get(fmt.Sprintf(keyAccountExists, accountKey))
  224. if err != nil {
  225. return errSaslFail
  226. }
  227. // confirm account is verified
  228. _, err = tx.Get(fmt.Sprintf(keyAccountVerified, accountKey))
  229. if err != nil {
  230. return errSaslFail
  231. }
  232. // confirm the certfp in that account's credentials
  233. creds, err := loadAccountCredentials(tx, accountKey)
  234. if err != nil || creds.Certificate != client.certfp {
  235. return errSaslFail
  236. }
  237. // succeeded, load account info if necessary
  238. account, exists := server.accounts[accountKey]
  239. if !exists {
  240. account = loadAccount(server, tx, accountKey)
  241. }
  242. client.LoginToAccount(account)
  243. accountName = account.Name
  244. return nil
  245. })
  246. if err == nil {
  247. client.Notice(fmt.Sprintf(client.t("You're now logged in as %s"), accountName))
  248. return
  249. }
  250. }
  251. client.Notice(client.t("Could not login with your TLS certificate or supplied username/password"))
  252. } else {
  253. client.Notice(client.t("Command not recognised. To see the available commands, run /NS HELP"))
  254. }
  255. }