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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "fmt"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/goshuirc/irc-go/ircfmt"
  11. "github.com/oragono/oragono/irc/modes"
  12. "github.com/oragono/oragono/irc/passwd"
  13. "github.com/oragono/oragono/irc/sno"
  14. "github.com/oragono/oragono/irc/utils"
  15. )
  16. // "enabled" callbacks for specific nickserv commands
  17. func servCmdRequiresAccreg(config *Config) bool {
  18. return config.Accounts.Registration.Enabled
  19. }
  20. func servCmdRequiresAuthEnabled(config *Config) bool {
  21. return config.Accounts.AuthenticationEnabled
  22. }
  23. func servCmdRequiresNickRes(config *Config) bool {
  24. return config.Accounts.AuthenticationEnabled && config.Accounts.NickReservation.Enabled
  25. }
  26. func servCmdRequiresBouncerEnabled(config *Config) bool {
  27. return config.Accounts.Multiclient.Enabled
  28. }
  29. const (
  30. nsPrefix = "NickServ!NickServ@localhost"
  31. )
  32. const nickservHelp = `NickServ lets you register, log in to, and manage an account.`
  33. var (
  34. nickservCommands = map[string]*serviceCommand{
  35. "drop": {
  36. handler: nsDropHandler,
  37. help: `Syntax: $bDROP [nickname]$b
  38. DROP de-links the given (or your current) nickname from your user account.`,
  39. helpShort: `$bDROP$b de-links your current (or the given) nickname from your user account.`,
  40. enabled: servCmdRequiresNickRes,
  41. authRequired: true,
  42. },
  43. "enforce": {
  44. hidden: true,
  45. handler: nsEnforceHandler,
  46. help: `Syntax: $bENFORCE [method]$b
  47. ENFORCE is an alias for $bGET enforce$b and $bSET enforce$b. See the help
  48. entry for $bSET$b for more information.`,
  49. authRequired: true,
  50. enabled: servCmdRequiresNickRes,
  51. },
  52. "ghost": {
  53. handler: nsGhostHandler,
  54. help: `Syntax: $bGHOST <nickname>$b
  55. GHOST disconnects the given user from the network if they're logged in with the
  56. same user account, letting you reclaim your nickname.`,
  57. helpShort: `$bGHOST$b reclaims your nickname.`,
  58. enabled: servCmdRequiresNickRes,
  59. authRequired: true,
  60. minParams: 1,
  61. },
  62. "group": {
  63. handler: nsGroupHandler,
  64. help: `Syntax: $bGROUP$b
  65. GROUP links your current nickname with your logged-in account, so other people
  66. will not be able to use it.`,
  67. helpShort: `$bGROUP$b links your current nickname to your user account.`,
  68. enabled: servCmdRequiresNickRes,
  69. authRequired: true,
  70. },
  71. "identify": {
  72. handler: nsIdentifyHandler,
  73. help: `Syntax: $bIDENTIFY <username> [password]$b
  74. IDENTIFY lets you login to the given username using either password auth, or
  75. certfp (your client certificate) if a password is not given.`,
  76. helpShort: `$bIDENTIFY$b lets you login to your account.`,
  77. enabled: servCmdRequiresAuthEnabled,
  78. minParams: 1,
  79. },
  80. "list": {
  81. handler: nsListHandler,
  82. help: `Syntax: $bLIST [regex]$b
  83. LIST returns the list of registered nicknames, which match the given regex.
  84. If no regex is provided, all registered nicknames are returned.`,
  85. helpShort: `$bLIST$b searches the list of registered nicknames.`,
  86. enabled: servCmdRequiresAuthEnabled,
  87. capabs: []string{"accreg"},
  88. minParams: 0,
  89. },
  90. "info": {
  91. handler: nsInfoHandler,
  92. help: `Syntax: $bINFO [username]$b
  93. INFO gives you information about the given (or your own) user account.`,
  94. helpShort: `$bINFO$b gives you information on a user account.`,
  95. },
  96. "register": {
  97. handler: nsRegisterHandler,
  98. // TODO: "email" is an oversimplification here; it's actually any callback, e.g.,
  99. // person@example.com, mailto:person@example.com, tel:16505551234.
  100. help: `Syntax: $bREGISTER <password> [email]$b
  101. REGISTER lets you register your current nickname as a user account. If the
  102. server allows anonymous registration, you can omit the e-mail address.
  103. If you are currently logged in with a TLS client certificate and wish to use
  104. it instead of a password to log in, send * as the password.`,
  105. helpShort: `$bREGISTER$b lets you register a user account.`,
  106. enabled: servCmdRequiresAccreg,
  107. minParams: 1,
  108. maxParams: 2,
  109. },
  110. "sadrop": {
  111. handler: nsDropHandler,
  112. help: `Syntax: $bSADROP <nickname>$b
  113. SADROP forcibly de-links the given nickname from the attached user account.`,
  114. helpShort: `$bSADROP$b forcibly de-links the given nickname from its user account.`,
  115. capabs: []string{"accreg"},
  116. enabled: servCmdRequiresNickRes,
  117. minParams: 1,
  118. },
  119. "saregister": {
  120. handler: nsSaregisterHandler,
  121. help: `Syntax: $bSAREGISTER <username> [password]$b
  122. SAREGISTER registers an account on someone else's behalf.
  123. This is for use in configurations that require SASL for all connections;
  124. an administrator can set use this command to set up user accounts.`,
  125. helpShort: `$bSAREGISTER$b registers an account on someone else's behalf.`,
  126. enabled: servCmdRequiresAuthEnabled,
  127. capabs: []string{"accreg"},
  128. minParams: 1,
  129. },
  130. "sessions": {
  131. handler: nsSessionsHandler,
  132. help: `Syntax: $bSESSIONS [nickname]$b
  133. SESSIONS lists information about the sessions currently attached, via
  134. the server's multiclient functionality, to your nickname. An administrator
  135. can use this command to list another user's sessions.`,
  136. helpShort: `$bSESSIONS$b lists the sessions attached to a nickname.`,
  137. enabled: servCmdRequiresBouncerEnabled,
  138. },
  139. "unregister": {
  140. handler: nsUnregisterHandler,
  141. help: `Syntax: $bUNREGISTER <username> [code]$b
  142. UNREGISTER lets you delete your user account (or someone else's, if you're an
  143. IRC operator with the correct permissions). To prevent accidental
  144. unregistrations, a verification code is required; invoking the command without
  145. a code will display the necessary code.`,
  146. helpShort: `$bUNREGISTER$b lets you delete your user account.`,
  147. enabled: servCmdRequiresAuthEnabled,
  148. minParams: 1,
  149. },
  150. "erase": {
  151. handler: nsUnregisterHandler,
  152. help: `Syntax: $bERASE <username> [code]$b
  153. ERASE deletes all records of an account, allowing it to be re-registered.
  154. This should be used with caution, because it violates an expectation that
  155. account names are permanent identifiers. Typically, UNREGISTER should be
  156. used instead. A confirmation code is required; invoking the command
  157. without a code will display the necessary code.`,
  158. helpShort: `$bERASE$b erases all records of an account, allowing reuse.`,
  159. enabled: servCmdRequiresAuthEnabled,
  160. capabs: []string{"accreg"},
  161. minParams: 1,
  162. },
  163. "verify": {
  164. handler: nsVerifyHandler,
  165. help: `Syntax: $bVERIFY <username> <code>$b
  166. VERIFY lets you complete an account registration, if the server requires email
  167. or other verification.`,
  168. helpShort: `$bVERIFY$b lets you complete account registration.`,
  169. enabled: servCmdRequiresAccreg,
  170. minParams: 2,
  171. },
  172. "passwd": {
  173. handler: nsPasswdHandler,
  174. help: `Syntax: $bPASSWD <current> <new> <new_again>$b
  175. Or: $bPASSWD <username> <new>$b
  176. PASSWD lets you change your account password. You must supply your current
  177. password and confirm the new one by typing it twice. If you're an IRC operator
  178. with the correct permissions, you can use PASSWD to reset someone else's
  179. password by supplying their username and then the desired password. To
  180. indicate an empty password, use * instead.`,
  181. helpShort: `$bPASSWD$b lets you change your password.`,
  182. enabled: servCmdRequiresAuthEnabled,
  183. minParams: 2,
  184. },
  185. "get": {
  186. handler: nsGetHandler,
  187. help: `Syntax: $bGET <setting>$b
  188. GET queries the current values of your account settings. For more information
  189. on the settings and their possible values, see HELP SET.`,
  190. helpShort: `$bGET$b queries the current values of your account settings`,
  191. authRequired: true,
  192. enabled: servCmdRequiresAuthEnabled,
  193. minParams: 1,
  194. },
  195. "saget": {
  196. handler: nsGetHandler,
  197. help: `Syntax: $bSAGET <account> <setting>$b
  198. SAGET queries the values of someone else's account settings. For more
  199. information on the settings and their possible values, see HELP SET.`,
  200. helpShort: `$bSAGET$b queries the current values of another user's account settings`,
  201. enabled: servCmdRequiresAuthEnabled,
  202. minParams: 2,
  203. capabs: []string{"accreg"},
  204. },
  205. "set": {
  206. handler: nsSetHandler,
  207. helpShort: `$bSET$b modifies your account settings`,
  208. // these are broken out as separate strings so they can be translated separately
  209. helpStrings: []string{
  210. `Syntax $bSET <setting> <value>$b
  211. SET modifies your account settings. The following settings are available:`,
  212. `$bENFORCE$b
  213. 'enforce' lets you specify a custom enforcement mechanism for your registered
  214. nicknames. Your options are:
  215. 1. 'none' [no enforcement, overriding the server default]
  216. 2. 'strict' [you must already be authenticated to use the nick]
  217. 3. 'default' [use the server default]`,
  218. `$bMULTICLIENT$b
  219. If 'multiclient' is enabled and you are already logged in and using a nick, a
  220. second client of yours that authenticates with SASL and requests the same nick
  221. is allowed to attach to the nick as well (this is comparable to the behavior
  222. of IRC "bouncers" like ZNC). Your options are 'on' (allow this behavior),
  223. 'off' (disallow it), and 'default' (use the server default value).`,
  224. `$bAUTOREPLAY-LINES$b
  225. 'autoreplay-lines' controls the number of lines of channel history that will
  226. be replayed to you automatically when joining a channel. Your options are any
  227. positive number, 0 to disable the feature, and 'default' to use the server
  228. default.`,
  229. `$bREPLAY-JOINS$b
  230. 'replay-joins' controls whether replayed channel history will include
  231. lines for join and part. This provides more information about the context of
  232. messages, but may be spammy. Your options are 'always', 'never', and the default
  233. of 'commands-only' (the messages will be replayed in /HISTORY output, but not
  234. during autoreplay).`,
  235. `$bALWAYS-ON$b
  236. 'always-on' controls whether your nickname/identity will remain active
  237. even while you are disconnected from the server. Your options are 'true',
  238. 'false', and 'default' (use the server default value).`,
  239. `$bAUTOREPLAY-MISSED$b
  240. 'autoreplay-missed' is only effective for always-on clients. If enabled,
  241. if you have at most one active session, the server will remember the time
  242. you disconnect and then replay missed messages to you when you reconnect.
  243. Your options are 'on' and 'off'.`,
  244. `$bDM-HISTORY$b
  245. 'dm-history' is only effective for always-on clients. It lets you control
  246. how the history of your direct messages is stored. Your options are:
  247. 1. 'off' [no history]
  248. 2. 'ephemeral' [a limited amount of temporary history, not stored on disk]
  249. 3. 'on' [history stored in a permanent database, if available]
  250. 4. 'default' [use the server default]`,
  251. `$bAUTO-AWAY$b
  252. 'auto-away' is only effective for always-on clients. If enabled, you will
  253. automatically be marked away when all your sessions are disconnected, and
  254. automatically return from away when you connect again.`,
  255. },
  256. authRequired: true,
  257. enabled: servCmdRequiresAuthEnabled,
  258. minParams: 2,
  259. },
  260. "saset": {
  261. handler: nsSetHandler,
  262. help: `Syntax: $bSASET <account> <setting> <value>$b
  263. SASET modifies the values of someone else's account settings. For more
  264. information on the settings and their possible values, see HELP SET.`,
  265. helpShort: `$bSASET$b modifies another user's account settings`,
  266. enabled: servCmdRequiresAuthEnabled,
  267. minParams: 3,
  268. capabs: []string{"accreg"},
  269. },
  270. "cert": {
  271. handler: nsCertHandler,
  272. help: `Syntax: $bCERT <LIST | ADD | DEL> [account] [certfp]$b
  273. CERT examines or modifies the TLS certificate fingerprints that can be used to
  274. log into an account. Specifically, $bCERT LIST$b lists the authorized
  275. fingerprints, $bCERT ADD <fingerprint>$b adds a new fingerprint, and
  276. $bCERT DEL <fingerprint>$b removes a fingerprint. If you're an IRC operator
  277. with the correct permissions, you can act on another user's account, for
  278. example with $bCERT ADD <account> <fingerprint>$b.`,
  279. helpShort: `$bCERT$b controls a user account's certificate fingerprints`,
  280. enabled: servCmdRequiresAuthEnabled,
  281. minParams: 1,
  282. },
  283. "suspend": {
  284. handler: nsSuspendHandler,
  285. help: `Syntax: $bSUSPEND <nickname>$b
  286. SUSPEND disables an account and disconnects the associated clients.`,
  287. helpShort: `$bSUSPEND$b disables an account and disconnects the clients`,
  288. minParams: 1,
  289. capabs: []string{"accreg"},
  290. },
  291. "unsuspend": {
  292. handler: nsUnsuspendHandler,
  293. help: `Syntax: $bUNSUSPEND <nickname>$b
  294. UNSUSPEND reverses a previous SUSPEND, restoring access to the account.`,
  295. helpShort: `$bUNSUSPEND$b restores access to a suspended account`,
  296. minParams: 1,
  297. capabs: []string{"accreg"},
  298. },
  299. }
  300. )
  301. // nsNotice sends the client a notice from NickServ
  302. func nsNotice(rb *ResponseBuffer, text string) {
  303. // XXX i can't figure out how to use OragonoServices[servicename].prefix here
  304. // without creating a compile-time initialization loop
  305. rb.Add(nil, nsPrefix, "NOTICE", rb.target.Nick(), text)
  306. }
  307. func nsGetHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  308. var account string
  309. if command == "saget" {
  310. account = params[0]
  311. params = params[1:]
  312. } else {
  313. account = client.Account()
  314. }
  315. accountData, err := server.accounts.LoadAccount(account)
  316. if err == errAccountDoesNotExist {
  317. nsNotice(rb, client.t("No such account"))
  318. return
  319. } else if err != nil {
  320. nsNotice(rb, client.t("Error loading account data"))
  321. return
  322. }
  323. displaySetting(params[0], accountData.Settings, client, rb)
  324. }
  325. func displaySetting(settingName string, settings AccountSettings, client *Client, rb *ResponseBuffer) {
  326. config := client.server.Config()
  327. switch strings.ToLower(settingName) {
  328. case "enforce":
  329. storedValue := settings.NickEnforcement
  330. serializedStoredValue := nickReservationToString(storedValue)
  331. nsNotice(rb, fmt.Sprintf(client.t("Your stored nickname enforcement setting is: %s"), serializedStoredValue))
  332. serializedActualValue := nickReservationToString(configuredEnforcementMethod(config, storedValue))
  333. nsNotice(rb, fmt.Sprintf(client.t("Given current server settings, your nickname is enforced with: %s"), serializedActualValue))
  334. case "autoreplay-lines":
  335. if settings.AutoreplayLines == nil {
  336. nsNotice(rb, fmt.Sprintf(client.t("You will receive the server default of %d lines of autoreplayed history"), config.History.AutoreplayOnJoin))
  337. } else {
  338. nsNotice(rb, fmt.Sprintf(client.t("You will receive %d lines of autoreplayed history"), *settings.AutoreplayLines))
  339. }
  340. case "replay-joins":
  341. switch settings.ReplayJoins {
  342. case ReplayJoinsCommandsOnly:
  343. nsNotice(rb, client.t("You will see JOINs and PARTs in /HISTORY output, but not in autoreplay"))
  344. case ReplayJoinsAlways:
  345. nsNotice(rb, client.t("You will see JOINs and PARTs in /HISTORY output and in autoreplay"))
  346. case ReplayJoinsNever:
  347. nsNotice(rb, client.t("You will not see JOINs and PARTs in /HISTORY output or in autoreplay"))
  348. }
  349. case "multiclient":
  350. if !config.Accounts.Multiclient.Enabled {
  351. nsNotice(rb, client.t("This feature has been disabled by the server administrators"))
  352. } else {
  353. switch settings.AllowBouncer {
  354. case MulticlientAllowedServerDefault:
  355. if config.Accounts.Multiclient.AllowedByDefault {
  356. nsNotice(rb, client.t("Multiclient functionality is currently enabled for your account, but you can opt out"))
  357. } else {
  358. nsNotice(rb, client.t("Multiclient functionality is currently disabled for your account, but you can opt in"))
  359. }
  360. case MulticlientDisallowedByUser:
  361. nsNotice(rb, client.t("Multiclient functionality is currently disabled for your account"))
  362. case MulticlientAllowedByUser:
  363. nsNotice(rb, client.t("Multiclient functionality is currently enabled for your account"))
  364. }
  365. }
  366. case "always-on":
  367. stored := settings.AlwaysOn
  368. actual := persistenceEnabled(config.Accounts.Multiclient.AlwaysOn, stored)
  369. nsNotice(rb, fmt.Sprintf(client.t("Your stored always-on setting is: %s"), persistentStatusToString(stored)))
  370. if actual {
  371. nsNotice(rb, client.t("Given current server settings, your client is always-on"))
  372. } else {
  373. nsNotice(rb, client.t("Given current server settings, your client is not always-on"))
  374. }
  375. case "autoreplay-missed":
  376. stored := settings.AutoreplayMissed
  377. if stored {
  378. alwaysOn := persistenceEnabled(config.Accounts.Multiclient.AlwaysOn, settings.AlwaysOn)
  379. if alwaysOn {
  380. nsNotice(rb, client.t("Autoreplay of missed messages is enabled"))
  381. } else {
  382. nsNotice(rb, client.t("You have enabled autoreplay of missed messages, but you can't receive them because your client isn't set to always-on"))
  383. }
  384. } else {
  385. nsNotice(rb, client.t("Your account is not configured to receive autoreplayed missed messages"))
  386. }
  387. case "auto-away":
  388. stored := settings.AutoAway
  389. alwaysOn := persistenceEnabled(config.Accounts.Multiclient.AlwaysOn, settings.AlwaysOn)
  390. actual := persistenceEnabled(config.Accounts.Multiclient.AutoAway, settings.AutoAway)
  391. nsNotice(rb, fmt.Sprintf(client.t("Your stored auto-away setting is: %s"), persistentStatusToString(stored)))
  392. if actual && alwaysOn {
  393. nsNotice(rb, client.t("Given current server settings, auto-away is enabled for your client"))
  394. } else if actual && !alwaysOn {
  395. nsNotice(rb, client.t("Because your client is not always-on, auto-away is disabled"))
  396. } else if !actual {
  397. nsNotice(rb, client.t("Given current server settings, auto-away is disabled for your client"))
  398. }
  399. case "dm-history":
  400. effectiveValue := historyEnabled(config.History.Persistent.DirectMessages, settings.DMHistory)
  401. nsNotice(rb, fmt.Sprintf(client.t("Your stored direct message history setting is: %s"), historyStatusToString(settings.DMHistory)))
  402. nsNotice(rb, fmt.Sprintf(client.t("Given current server settings, your direct message history setting is: %s"), historyStatusToString(effectiveValue)))
  403. default:
  404. nsNotice(rb, client.t("No such setting"))
  405. }
  406. }
  407. func nsSetHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  408. var account string
  409. if command == "saset" {
  410. account = params[0]
  411. params = params[1:]
  412. } else {
  413. account = client.Account()
  414. }
  415. var munger settingsMunger
  416. var finalSettings AccountSettings
  417. var err error
  418. switch strings.ToLower(params[0]) {
  419. case "pass", "password":
  420. nsNotice(rb, client.t("To change a password, use the PASSWD command. For details, /msg NickServ HELP PASSWD"))
  421. return
  422. case "enforce":
  423. var method NickEnforcementMethod
  424. method, err = nickReservationFromString(params[1])
  425. if err != nil {
  426. err = errInvalidParams
  427. break
  428. }
  429. // updating enforcement settings is special-cased, because it requires
  430. // an update to server.accounts.accountToMethod
  431. finalSettings, err = server.accounts.SetEnforcementStatus(account, method)
  432. if err == nil {
  433. finalSettings.NickEnforcement = method // success
  434. }
  435. case "autoreplay-lines":
  436. var newValue *int
  437. if strings.ToLower(params[1]) != "default" {
  438. val, err_ := strconv.Atoi(params[1])
  439. if err_ != nil || val < 0 {
  440. err = errInvalidParams
  441. break
  442. }
  443. newValue = new(int)
  444. *newValue = val
  445. }
  446. munger = func(in AccountSettings) (out AccountSettings, err error) {
  447. out = in
  448. out.AutoreplayLines = newValue
  449. return
  450. }
  451. case "multiclient":
  452. var newValue MulticlientAllowedSetting
  453. if strings.ToLower(params[1]) == "default" {
  454. newValue = MulticlientAllowedServerDefault
  455. } else {
  456. var enabled bool
  457. enabled, err = utils.StringToBool(params[1])
  458. if enabled {
  459. newValue = MulticlientAllowedByUser
  460. } else {
  461. newValue = MulticlientDisallowedByUser
  462. }
  463. }
  464. if err == nil {
  465. munger = func(in AccountSettings) (out AccountSettings, err error) {
  466. out = in
  467. out.AllowBouncer = newValue
  468. return
  469. }
  470. }
  471. case "replay-joins":
  472. var newValue ReplayJoinsSetting
  473. newValue, err = replayJoinsSettingFromString(params[1])
  474. if err == nil {
  475. munger = func(in AccountSettings) (out AccountSettings, err error) {
  476. out = in
  477. out.ReplayJoins = newValue
  478. return
  479. }
  480. }
  481. case "always-on":
  482. // #821: it's problematic to alter the value of always-on if you're not
  483. // the (actual or potential) always-on client yourself. make an exception
  484. // for `saset` to give operators an escape hatch (any consistency problems
  485. // can probably be fixed by restarting the server):
  486. if command != "saset" {
  487. details := client.Details()
  488. if details.nick != details.accountName {
  489. err = errNickAccountMismatch
  490. }
  491. }
  492. if err == nil {
  493. var newValue PersistentStatus
  494. newValue, err = persistentStatusFromString(params[1])
  495. // "opt-in" and "opt-out" don't make sense as user preferences
  496. if err == nil && newValue != PersistentOptIn && newValue != PersistentOptOut {
  497. munger = func(in AccountSettings) (out AccountSettings, err error) {
  498. out = in
  499. out.AlwaysOn = newValue
  500. return
  501. }
  502. }
  503. }
  504. case "autoreplay-missed":
  505. var newValue bool
  506. newValue, err = utils.StringToBool(params[1])
  507. if err == nil {
  508. munger = func(in AccountSettings) (out AccountSettings, err error) {
  509. out = in
  510. out.AutoreplayMissed = newValue
  511. return
  512. }
  513. }
  514. case "auto-away":
  515. var newValue PersistentStatus
  516. newValue, err = persistentStatusFromString(params[1])
  517. // "opt-in" and "opt-out" don't make sense as user preferences
  518. if err == nil && newValue != PersistentOptIn && newValue != PersistentOptOut {
  519. munger = func(in AccountSettings) (out AccountSettings, err error) {
  520. out = in
  521. out.AutoAway = newValue
  522. return
  523. }
  524. }
  525. case "dm-history":
  526. var newValue HistoryStatus
  527. newValue, err = historyStatusFromString(params[1])
  528. if err == nil {
  529. munger = func(in AccountSettings) (out AccountSettings, err error) {
  530. out = in
  531. out.DMHistory = newValue
  532. return
  533. }
  534. }
  535. default:
  536. err = errInvalidParams
  537. }
  538. if munger != nil {
  539. finalSettings, err = server.accounts.ModifyAccountSettings(account, munger)
  540. }
  541. switch err {
  542. case nil:
  543. nsNotice(rb, client.t("Successfully changed your account settings"))
  544. displaySetting(params[0], finalSettings, client, rb)
  545. case errInvalidParams, errAccountDoesNotExist, errFeatureDisabled, errAccountUnverified, errAccountUpdateFailed:
  546. nsNotice(rb, client.t(err.Error()))
  547. case errNickAccountMismatch:
  548. nsNotice(rb, fmt.Sprintf(client.t("Your nickname must match your account name %s exactly to modify this setting. Try changing it with /NICK, or logging out and back in with the correct nickname."), client.AccountName()))
  549. default:
  550. // unknown error
  551. nsNotice(rb, client.t("An error occurred"))
  552. }
  553. }
  554. func nsDropHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  555. sadrop := command == "sadrop"
  556. var nick string
  557. if len(params) > 0 {
  558. nick = params[0]
  559. } else {
  560. nick = client.NickCasefolded()
  561. }
  562. err := server.accounts.SetNickReserved(client, nick, sadrop, false)
  563. if err == nil {
  564. nsNotice(rb, fmt.Sprintf(client.t("Successfully ungrouped nick %s with your account"), nick))
  565. } else if err == errAccountNotLoggedIn {
  566. nsNotice(rb, client.t("You're not logged into an account"))
  567. } else if err == errAccountCantDropPrimaryNick {
  568. nsNotice(rb, client.t("You can't ungroup your primary nickname (try unregistering your account instead)"))
  569. } else {
  570. nsNotice(rb, client.t("Could not ungroup nick"))
  571. }
  572. }
  573. func nsGhostHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  574. nick := params[0]
  575. ghost := server.clients.Get(nick)
  576. if ghost == nil {
  577. nsNotice(rb, client.t("No such nick"))
  578. return
  579. } else if ghost == client {
  580. nsNotice(rb, client.t("You can't GHOST yourself (try /QUIT instead)"))
  581. return
  582. } else if ghost.AlwaysOn() {
  583. nsNotice(rb, client.t("You can't GHOST an always-on client"))
  584. return
  585. }
  586. authorized := false
  587. account := client.Account()
  588. if account != "" {
  589. // the user must either own the nick, or the target client
  590. authorized = (server.accounts.NickToAccount(nick) == account) || (ghost.Account() == account)
  591. }
  592. if !authorized {
  593. nsNotice(rb, client.t("You don't own that nick"))
  594. return
  595. }
  596. ghost.Quit(fmt.Sprintf(ghost.t("GHOSTed by %s"), client.Nick()), nil)
  597. ghost.destroy(nil)
  598. }
  599. func nsGroupHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  600. nick := client.Nick()
  601. err := server.accounts.SetNickReserved(client, nick, false, true)
  602. if err == nil {
  603. nsNotice(rb, fmt.Sprintf(client.t("Successfully grouped nick %s with your account"), nick))
  604. } else if err == errAccountTooManyNicks {
  605. nsNotice(rb, client.t("You have too many nicks reserved already (you can remove some with /NS DROP)"))
  606. } else if err == errNicknameReserved {
  607. nsNotice(rb, client.t("That nickname is already reserved by someone else"))
  608. } else {
  609. nsNotice(rb, client.t("Error reserving nickname"))
  610. }
  611. }
  612. func nsLoginThrottleCheck(client *Client, rb *ResponseBuffer) (success bool) {
  613. throttled, remainingTime := client.checkLoginThrottle()
  614. if throttled {
  615. nsNotice(rb, fmt.Sprintf(client.t("Please wait at least %v and try again"), remainingTime))
  616. }
  617. return !throttled
  618. }
  619. func nsIdentifyHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  620. if client.LoggedIntoAccount() {
  621. nsNotice(rb, client.t("You're already logged into an account"))
  622. return
  623. }
  624. var err error
  625. loginSuccessful := false
  626. var username, passphrase string
  627. if len(params) == 1 {
  628. if rb.session.certfp != "" {
  629. username = params[0]
  630. } else {
  631. // XXX undocumented compatibility mode with other nickservs, allowing
  632. // /msg NickServ identify passphrase
  633. username = client.NickCasefolded()
  634. passphrase = params[0]
  635. }
  636. } else {
  637. username = params[0]
  638. passphrase = params[1]
  639. }
  640. // try passphrase
  641. if passphrase != "" {
  642. err = server.accounts.AuthenticateByPassphrase(client, username, passphrase)
  643. loginSuccessful = (err == nil)
  644. }
  645. // try certfp
  646. if !loginSuccessful && rb.session.certfp != "" {
  647. err = server.accounts.AuthenticateByCertFP(client, rb.session.certfp, "")
  648. loginSuccessful = (err == nil)
  649. }
  650. nickFixupFailed := false
  651. if loginSuccessful {
  652. if !fixupNickEqualsAccount(client, rb, server.Config()) {
  653. loginSuccessful = false
  654. // fixupNickEqualsAccount sends its own error message, don't send another
  655. nickFixupFailed = true
  656. }
  657. }
  658. if loginSuccessful {
  659. sendSuccessfulAccountAuth(client, rb, true, true)
  660. } else if !nickFixupFailed {
  661. nsNotice(rb, fmt.Sprintf(client.t("Authentication failed: %s"), authErrorToMessage(server, err)))
  662. }
  663. }
  664. func nsListHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  665. if !client.HasRoleCapabs("accreg") {
  666. nsNotice(rb, client.t("Insufficient privileges"))
  667. return
  668. }
  669. var searchRegex *regexp.Regexp
  670. if len(params) > 0 {
  671. var err error
  672. searchRegex, err = regexp.Compile(params[0])
  673. if err != nil {
  674. nsNotice(rb, client.t("Invalid regex"))
  675. return
  676. }
  677. }
  678. nsNotice(rb, ircfmt.Unescape(client.t("*** $bNickServ LIST$b ***")))
  679. nicks := server.accounts.AllNicks()
  680. for _, nick := range nicks {
  681. if searchRegex == nil || searchRegex.MatchString(nick) {
  682. nsNotice(rb, fmt.Sprintf(" %s", nick))
  683. }
  684. }
  685. nsNotice(rb, ircfmt.Unescape(client.t("*** $bEnd of NickServ LIST$b ***")))
  686. }
  687. func nsInfoHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  688. if !server.Config().Accounts.AuthenticationEnabled && !client.HasRoleCapabs("accreg") {
  689. nsNotice(rb, client.t("This command has been disabled by the server administrators"))
  690. return
  691. }
  692. var accountName string
  693. if len(params) > 0 {
  694. nick := params[0]
  695. if server.Config().Accounts.NickReservation.Enabled {
  696. accountName = server.accounts.NickToAccount(nick)
  697. if accountName == "" {
  698. nsNotice(rb, client.t("That nickname is not registered"))
  699. return
  700. }
  701. } else {
  702. accountName = nick
  703. }
  704. } else {
  705. accountName = client.Account()
  706. if accountName == "" {
  707. nsNotice(rb, client.t("You're not logged into an account"))
  708. return
  709. }
  710. }
  711. account, err := server.accounts.LoadAccount(accountName)
  712. if err != nil || !account.Verified {
  713. nsNotice(rb, client.t("Account does not exist"))
  714. return
  715. }
  716. nsNotice(rb, fmt.Sprintf(client.t("Account: %s"), account.Name))
  717. registeredAt := account.RegisteredAt.Format(time.RFC1123)
  718. nsNotice(rb, fmt.Sprintf(client.t("Registered at: %s"), registeredAt))
  719. // TODO nicer formatting for this
  720. for _, nick := range account.AdditionalNicks {
  721. nsNotice(rb, fmt.Sprintf(client.t("Additional grouped nick: %s"), nick))
  722. }
  723. for _, channel := range server.accounts.ChannelsForAccount(accountName) {
  724. nsNotice(rb, fmt.Sprintf(client.t("Registered channel: %s"), channel))
  725. }
  726. }
  727. func nsRegisterHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  728. details := client.Details()
  729. passphrase := params[0]
  730. var email string
  731. if 1 < len(params) {
  732. email = params[1]
  733. }
  734. certfp := rb.session.certfp
  735. if passphrase == "*" {
  736. if certfp == "" {
  737. nsNotice(rb, client.t("You must be connected with TLS and a client certificate to do this"))
  738. return
  739. } else {
  740. passphrase = ""
  741. }
  742. }
  743. if passphrase != "" {
  744. cfPassphrase, err := Casefold(passphrase)
  745. if err == nil && cfPassphrase == details.nickCasefolded {
  746. nsNotice(rb, client.t("Usage: REGISTER <passphrase> [email]")) // #1179
  747. return
  748. }
  749. }
  750. if !nsLoginThrottleCheck(client, rb) {
  751. return
  752. }
  753. config := server.Config()
  754. account := details.nick
  755. if config.Accounts.NickReservation.ForceGuestFormat {
  756. matches := config.Accounts.NickReservation.guestRegexp.FindStringSubmatch(account)
  757. if matches == nil || len(matches) < 2 {
  758. nsNotice(rb, client.t("Erroneous nickname"))
  759. return
  760. }
  761. account = matches[1]
  762. }
  763. var callbackNamespace, callbackValue string
  764. noneCallbackAllowed := false
  765. for _, callback := range config.Accounts.Registration.EnabledCallbacks {
  766. if callback == "*" {
  767. noneCallbackAllowed = true
  768. }
  769. }
  770. // XXX if ACC REGISTER allows registration with the `none` callback, then ignore
  771. // any callback that was passed here (to avoid confusion in the case where the ircd
  772. // has no mail server configured). otherwise, register using the provided callback:
  773. if noneCallbackAllowed {
  774. callbackNamespace = "*"
  775. } else {
  776. callbackNamespace, callbackValue = parseCallback(email, config.Accounts)
  777. if callbackNamespace == "" || callbackValue == "" {
  778. nsNotice(rb, client.t("Registration requires a valid e-mail address"))
  779. return
  780. }
  781. }
  782. err := server.accounts.Register(client, account, callbackNamespace, callbackValue, passphrase, rb.session.certfp)
  783. if err == nil {
  784. if callbackNamespace == "*" {
  785. err = server.accounts.Verify(client, account, "")
  786. if err == nil && fixupNickEqualsAccount(client, rb, config) {
  787. sendSuccessfulRegResponse(client, rb, true)
  788. }
  789. } else {
  790. messageTemplate := client.t("Account created, pending verification; verification code has been sent to %s")
  791. message := fmt.Sprintf(messageTemplate, callbackValue)
  792. nsNotice(rb, message)
  793. }
  794. } else {
  795. // details could not be stored and relevant numerics have been dispatched, abort
  796. message, _ := registrationErrorToMessageAndCode(err)
  797. nsNotice(rb, client.t(message))
  798. }
  799. }
  800. func nsSaregisterHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  801. var account, passphrase string
  802. account = params[0]
  803. if 1 < len(params) && params[1] != "*" {
  804. passphrase = params[1]
  805. }
  806. err := server.accounts.SARegister(account, passphrase)
  807. if err != nil {
  808. var errMsg string
  809. if err == errAccountAlreadyRegistered || err == errAccountAlreadyVerified {
  810. errMsg = client.t("Account already exists")
  811. } else if err == errAccountBadPassphrase {
  812. errMsg = client.t("Passphrase contains forbidden characters or is otherwise invalid")
  813. } else {
  814. server.logger.Error("services", "unknown error from saregister", err.Error())
  815. errMsg = client.t("Could not register")
  816. }
  817. nsNotice(rb, errMsg)
  818. } else {
  819. nsNotice(rb, fmt.Sprintf(client.t("Successfully registered account %s"), account))
  820. server.snomasks.Send(sno.LocalAccounts, fmt.Sprintf(ircfmt.Unescape("Operator $c[grey][$r%s$c[grey]] registered account $c[grey][$r%s$c[grey]] with SAREGISTER"), client.Oper().Name, account))
  821. }
  822. }
  823. func nsUnregisterHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  824. erase := command == "erase"
  825. username := params[0]
  826. var verificationCode string
  827. if len(params) > 1 {
  828. verificationCode = params[1]
  829. }
  830. if username == "" {
  831. nsNotice(rb, client.t("You must specify an account"))
  832. return
  833. }
  834. var accountName string
  835. var registeredAt time.Time
  836. if erase {
  837. // account may not be in a loadable state, e.g., if it was unregistered
  838. accountName = username
  839. // make the confirmation code nondeterministic for ERASE
  840. registeredAt = server.ctime
  841. } else {
  842. account, err := server.accounts.LoadAccount(username)
  843. if err == errAccountDoesNotExist {
  844. nsNotice(rb, client.t("Invalid account name"))
  845. return
  846. } else if err != nil {
  847. nsNotice(rb, client.t("Internal error"))
  848. return
  849. }
  850. accountName = account.Name
  851. registeredAt = account.RegisteredAt
  852. }
  853. if !(accountName == client.AccountName() || client.HasRoleCapabs("accreg")) {
  854. nsNotice(rb, client.t("Insufficient oper privs"))
  855. return
  856. }
  857. expectedCode := utils.ConfirmationCode(accountName, registeredAt)
  858. if expectedCode != verificationCode {
  859. if erase {
  860. nsNotice(rb, ircfmt.Unescape(client.t("$bWarning: erasing this account will allow it to be re-registered; consider UNREGISTER instead.$b")))
  861. } else {
  862. nsNotice(rb, ircfmt.Unescape(client.t("$bWarning: unregistering this account will remove its stored privileges.$b")))
  863. }
  864. nsNotice(rb, fmt.Sprintf(client.t("To confirm, run this command: %s"), fmt.Sprintf("/NS %s %s %s", strings.ToUpper(command), accountName, expectedCode)))
  865. return
  866. }
  867. err := server.accounts.Unregister(accountName, erase)
  868. if err == errAccountDoesNotExist {
  869. nsNotice(rb, client.t(err.Error()))
  870. } else if err != nil {
  871. nsNotice(rb, client.t("Error while unregistering account"))
  872. } else {
  873. nsNotice(rb, fmt.Sprintf(client.t("Successfully unregistered account %s"), accountName))
  874. server.logger.Info("accounts", "client", client.Nick(), "unregistered account", accountName)
  875. client.server.snomasks.Send(sno.LocalAccounts, fmt.Sprintf(ircfmt.Unescape("Client $c[grey][$r%s$c[grey]] unregistered account $c[grey][$r%s$c[grey]]"), client.NickMaskString(), accountName))
  876. }
  877. }
  878. func nsVerifyHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  879. username, code := params[0], params[1]
  880. err := server.accounts.Verify(client, username, code)
  881. var errorMessage string
  882. if err != nil {
  883. switch err {
  884. case errAccountAlreadyLoggedIn, errAccountVerificationInvalidCode, errAccountAlreadyVerified:
  885. errorMessage = err.Error()
  886. default:
  887. errorMessage = errAccountVerificationFailed.Error()
  888. }
  889. }
  890. if errorMessage != "" {
  891. nsNotice(rb, client.t(errorMessage))
  892. return
  893. }
  894. if fixupNickEqualsAccount(client, rb, server.Config()) {
  895. sendSuccessfulRegResponse(client, rb, true)
  896. }
  897. }
  898. func nsPasswdHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  899. var target string
  900. var newPassword string
  901. var errorMessage string
  902. hasPrivs := client.HasRoleCapabs("accreg")
  903. switch len(params) {
  904. case 2:
  905. if !hasPrivs {
  906. errorMessage = `Insufficient privileges`
  907. } else {
  908. target, newPassword = params[0], params[1]
  909. if newPassword == "*" {
  910. newPassword = ""
  911. }
  912. }
  913. case 3:
  914. target = client.Account()
  915. if target == "" {
  916. errorMessage = `You're not logged into an account`
  917. } else if params[1] != params[2] {
  918. errorMessage = `Passwords do not match`
  919. } else {
  920. if !nsLoginThrottleCheck(client, rb) {
  921. return
  922. }
  923. accountData, err := server.accounts.LoadAccount(target)
  924. if err != nil {
  925. errorMessage = `You're not logged into an account`
  926. } else {
  927. hash := accountData.Credentials.PassphraseHash
  928. if hash != nil && passwd.CompareHashAndPassword(hash, []byte(params[0])) != nil {
  929. errorMessage = `Password incorrect`
  930. } else {
  931. newPassword = params[1]
  932. if newPassword == "*" {
  933. newPassword = ""
  934. }
  935. }
  936. }
  937. }
  938. default:
  939. errorMessage = `Invalid parameters`
  940. }
  941. if errorMessage != "" {
  942. nsNotice(rb, client.t(errorMessage))
  943. return
  944. }
  945. err := server.accounts.setPassword(target, newPassword, hasPrivs)
  946. switch err {
  947. case nil:
  948. nsNotice(rb, client.t("Password changed"))
  949. case errEmptyCredentials:
  950. nsNotice(rb, client.t("You can't delete your password unless you add a certificate fingerprint"))
  951. case errCredsExternallyManaged:
  952. nsNotice(rb, client.t("Your account credentials are managed externally and cannot be changed here"))
  953. case errCASFailed:
  954. nsNotice(rb, client.t("Try again later"))
  955. default:
  956. server.logger.Error("internal", "could not upgrade user password:", err.Error())
  957. nsNotice(rb, client.t("Password could not be changed due to server error"))
  958. }
  959. }
  960. func nsEnforceHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  961. newParams := []string{"enforce"}
  962. if len(params) == 0 {
  963. nsGetHandler(server, client, "get", newParams, rb)
  964. } else {
  965. newParams = append(newParams, params[0])
  966. nsSetHandler(server, client, "set", newParams, rb)
  967. }
  968. }
  969. func nsSessionsHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  970. target := client
  971. if 0 < len(params) {
  972. target = server.clients.Get(params[0])
  973. if target == nil {
  974. nsNotice(rb, client.t("No such nick"))
  975. return
  976. }
  977. // same permissions check as RPL_WHOISACTUALLY for now:
  978. if target != client && !client.HasMode(modes.Operator) {
  979. nsNotice(rb, client.t("Command restricted"))
  980. return
  981. }
  982. }
  983. sessionData, currentIndex := target.AllSessionData(rb.session)
  984. nsNotice(rb, fmt.Sprintf(client.t("Nickname %[1]s has %[2]d attached session(s)"), target.Nick(), len(sessionData)))
  985. for i, session := range sessionData {
  986. if currentIndex == i {
  987. nsNotice(rb, fmt.Sprintf(client.t("Session %d (currently attached session):"), i+1))
  988. } else {
  989. nsNotice(rb, fmt.Sprintf(client.t("Session %d:"), i+1))
  990. }
  991. if session.deviceID != "" {
  992. nsNotice(rb, fmt.Sprintf(client.t("Device ID: %s"), session.deviceID))
  993. }
  994. nsNotice(rb, fmt.Sprintf(client.t("IP address: %s"), session.ip.String()))
  995. nsNotice(rb, fmt.Sprintf(client.t("Hostname: %s"), session.hostname))
  996. nsNotice(rb, fmt.Sprintf(client.t("Created at: %s"), session.ctime.Format(time.RFC1123)))
  997. nsNotice(rb, fmt.Sprintf(client.t("Last active: %s"), session.atime.Format(time.RFC1123)))
  998. if session.certfp != "" {
  999. nsNotice(rb, fmt.Sprintf(client.t("Certfp: %s"), session.certfp))
  1000. }
  1001. }
  1002. }
  1003. func nsCertHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  1004. verb := strings.ToLower(params[0])
  1005. params = params[1:]
  1006. var target, certfp string
  1007. switch verb {
  1008. case "list":
  1009. if 1 <= len(params) {
  1010. target = params[0]
  1011. }
  1012. case "add", "del":
  1013. if 2 <= len(params) {
  1014. target, certfp = params[0], params[1]
  1015. } else if len(params) == 1 {
  1016. certfp = params[0]
  1017. } else if len(params) == 0 && verb == "add" && rb.session.certfp != "" {
  1018. certfp = rb.session.certfp // #1059
  1019. } else {
  1020. nsNotice(rb, client.t("Invalid parameters"))
  1021. return
  1022. }
  1023. default:
  1024. nsNotice(rb, client.t("Invalid parameters"))
  1025. return
  1026. }
  1027. hasPrivs := client.HasRoleCapabs("accreg")
  1028. if target != "" && !hasPrivs {
  1029. nsNotice(rb, client.t("Insufficient privileges"))
  1030. return
  1031. } else if target == "" {
  1032. target = client.Account()
  1033. if target == "" {
  1034. nsNotice(rb, client.t("You're not logged into an account"))
  1035. return
  1036. }
  1037. }
  1038. var err error
  1039. switch verb {
  1040. case "list":
  1041. accountData, err := server.accounts.LoadAccount(target)
  1042. if err == errAccountDoesNotExist {
  1043. nsNotice(rb, client.t("Account does not exist"))
  1044. return
  1045. } else if err != nil {
  1046. nsNotice(rb, client.t("An error occurred"))
  1047. return
  1048. }
  1049. certfps := accountData.Credentials.Certfps
  1050. nsNotice(rb, fmt.Sprintf(client.t("There are %[1]d certificate fingerprint(s) authorized for account %[2]s."), len(certfps), accountData.Name))
  1051. for i, certfp := range certfps {
  1052. nsNotice(rb, fmt.Sprintf("%d: %s", i+1, certfp))
  1053. }
  1054. return
  1055. case "add":
  1056. err = server.accounts.addRemoveCertfp(target, certfp, true, hasPrivs)
  1057. case "del":
  1058. err = server.accounts.addRemoveCertfp(target, certfp, false, hasPrivs)
  1059. }
  1060. switch err {
  1061. case nil:
  1062. if verb == "add" {
  1063. nsNotice(rb, client.t("Certificate fingerprint successfully added"))
  1064. } else {
  1065. nsNotice(rb, client.t("Certificate fingerprint successfully removed"))
  1066. }
  1067. case errNoop:
  1068. if verb == "add" {
  1069. nsNotice(rb, client.t("That certificate fingerprint was already authorized"))
  1070. } else {
  1071. nsNotice(rb, client.t("Certificate fingerprint not found"))
  1072. }
  1073. case errAccountDoesNotExist:
  1074. nsNotice(rb, client.t("Account does not exist"))
  1075. case errLimitExceeded:
  1076. nsNotice(rb, client.t("You already have too many certificate fingerprints"))
  1077. case utils.ErrInvalidCertfp:
  1078. nsNotice(rb, client.t("Invalid certificate fingerprint"))
  1079. case errCertfpAlreadyExists:
  1080. nsNotice(rb, client.t("That certificate fingerprint is already associated with another account"))
  1081. case errEmptyCredentials:
  1082. nsNotice(rb, client.t("You can't remove all your certificate fingerprints unless you add a password"))
  1083. case errCredsExternallyManaged:
  1084. nsNotice(rb, client.t("Your account credentials are managed externally and cannot be changed here"))
  1085. case errCASFailed:
  1086. nsNotice(rb, client.t("Try again later"))
  1087. default:
  1088. server.logger.Error("internal", "could not modify certificates:", err.Error())
  1089. nsNotice(rb, client.t("An error occurred"))
  1090. }
  1091. }
  1092. func nsSuspendHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  1093. err := server.accounts.Suspend(params[0])
  1094. switch err {
  1095. case nil:
  1096. nsNotice(rb, fmt.Sprintf(client.t("Successfully suspended account %s"), params[0]))
  1097. case errAccountDoesNotExist:
  1098. nsNotice(rb, client.t("No such account"))
  1099. default:
  1100. nsNotice(rb, client.t("An error occurred"))
  1101. }
  1102. }
  1103. func nsUnsuspendHandler(server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  1104. err := server.accounts.Unsuspend(params[0])
  1105. switch err {
  1106. case nil:
  1107. nsNotice(rb, fmt.Sprintf(client.t("Successfully un-suspended account %s"), params[0]))
  1108. case errAccountDoesNotExist:
  1109. nsNotice(rb, client.t("No such account"))
  1110. default:
  1111. nsNotice(rb, client.t("An error occurred"))
  1112. }
  1113. }