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.

oragono.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2014-2015 Edmund Huber
  3. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  4. // released under the MIT license
  5. package main
  6. import (
  7. "bufio"
  8. "fmt"
  9. "log"
  10. "os"
  11. "strings"
  12. "syscall"
  13. "github.com/docopt/docopt-go"
  14. "github.com/oragono/oragono/irc"
  15. "github.com/oragono/oragono/irc/logger"
  16. "github.com/oragono/oragono/irc/mkcerts"
  17. "github.com/oragono/oragono/irc/utils"
  18. "golang.org/x/crypto/bcrypt"
  19. "golang.org/x/crypto/ssh/terminal"
  20. )
  21. var commit = ""
  22. // get a password from stdin from the user
  23. func getPassword() string {
  24. fd := int(os.Stdin.Fd())
  25. if terminal.IsTerminal(fd) {
  26. bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
  27. if err != nil {
  28. log.Fatal("Error reading password:", err.Error())
  29. }
  30. return string(bytePassword)
  31. }
  32. reader := bufio.NewReader(os.Stdin)
  33. text, _ := reader.ReadString('\n')
  34. return strings.TrimSpace(text)
  35. }
  36. // implements the `oragono mkcerts` command
  37. func doMkcerts(configFile string, quiet bool) {
  38. config, err := irc.LoadRawConfig(configFile)
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. if !quiet {
  43. log.Println("making self-signed certificates")
  44. }
  45. certToKey := make(map[string]string)
  46. for name, conf := range config.Server.Listeners {
  47. if conf.TLS.Cert == "" {
  48. continue
  49. }
  50. existingKey, ok := certToKey[conf.TLS.Cert]
  51. if ok {
  52. if existingKey == conf.TLS.Key {
  53. continue
  54. } else {
  55. log.Fatal("Conflicting TLS key files for", conf.TLS.Cert)
  56. }
  57. }
  58. if !quiet {
  59. log.Printf(" making cert for %s listener\n", name)
  60. }
  61. host := config.Server.Name
  62. cert, key := conf.TLS.Cert, conf.TLS.Key
  63. err := mkcerts.CreateCert("Oragono", host, cert, key)
  64. if err == nil {
  65. if !quiet {
  66. log.Printf(" Certificate created at %s : %s\n", cert, key)
  67. }
  68. certToKey[cert] = key
  69. } else {
  70. log.Fatal(" Could not create certificate:", err.Error())
  71. }
  72. }
  73. }
  74. func main() {
  75. version := irc.SemVer
  76. usage := `oragono.
  77. Usage:
  78. oragono initdb [--conf <filename>] [--quiet]
  79. oragono upgradedb [--conf <filename>] [--quiet]
  80. oragono genpasswd [--conf <filename>] [--quiet]
  81. oragono mkcerts [--conf <filename>] [--quiet]
  82. oragono mksecret [--conf <filename>] [--quiet]
  83. oragono run [--conf <filename>] [--quiet]
  84. oragono -h | --help
  85. oragono --version
  86. Options:
  87. --conf <filename> Configuration file to use [default: ircd.yaml].
  88. --quiet Don't show startup/shutdown lines.
  89. -h --help Show this screen.
  90. --version Show version.`
  91. arguments, _ := docopt.ParseArgs(usage, nil, version)
  92. // don't require a config file for genpasswd or mksecret
  93. if arguments["genpasswd"].(bool) {
  94. var password string
  95. fd := int(os.Stdin.Fd())
  96. if terminal.IsTerminal(fd) {
  97. fmt.Print("Enter Password: ")
  98. password = getPassword()
  99. fmt.Print("\n")
  100. fmt.Print("Reenter Password: ")
  101. confirm := getPassword()
  102. fmt.Print("\n")
  103. if confirm != password {
  104. log.Fatal("passwords do not match")
  105. }
  106. } else {
  107. password = getPassword()
  108. }
  109. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
  110. if err != nil {
  111. log.Fatal("encoding error:", err.Error())
  112. }
  113. fmt.Print(string(hash))
  114. if terminal.IsTerminal(fd) {
  115. fmt.Println()
  116. }
  117. return
  118. } else if arguments["mksecret"].(bool) {
  119. fmt.Println(utils.GenerateSecretKey())
  120. return
  121. } else if arguments["mkcerts"].(bool) {
  122. doMkcerts(arguments["--conf"].(string), arguments["--quiet"].(bool))
  123. return
  124. }
  125. configfile := arguments["--conf"].(string)
  126. config, err := irc.LoadConfig(configfile)
  127. if err != nil && !(err == irc.ErrInvalidCertKeyPair && arguments["mkcerts"].(bool)) {
  128. log.Fatal("Config file did not load successfully: ", err.Error())
  129. }
  130. logman, err := logger.NewManager(config.Logging)
  131. if err != nil {
  132. log.Fatal("Logger did not load successfully:", err.Error())
  133. }
  134. if arguments["initdb"].(bool) {
  135. irc.InitDB(config.Datastore.Path)
  136. if !arguments["--quiet"].(bool) {
  137. log.Println("database initialized: ", config.Datastore.Path)
  138. }
  139. } else if arguments["upgradedb"].(bool) {
  140. err = irc.UpgradeDB(config)
  141. if err != nil {
  142. log.Fatal("Error while upgrading db:", err.Error())
  143. }
  144. if !arguments["--quiet"].(bool) {
  145. log.Println("database upgraded: ", config.Datastore.Path)
  146. }
  147. } else if arguments["run"].(bool) {
  148. if !arguments["--quiet"].(bool) {
  149. logman.Info("server", fmt.Sprintf("Oragono v%s starting", irc.SemVer))
  150. if commit == "" {
  151. logman.Debug("server", fmt.Sprintf("Could not get current commit"))
  152. } else {
  153. logman.Info("server", fmt.Sprintf("Running commit %s", commit))
  154. }
  155. }
  156. // set current git commit
  157. irc.Commit = commit
  158. if commit != "" {
  159. irc.Ver = fmt.Sprintf("%s-%s", irc.Ver, commit)
  160. }
  161. // warning if running a non-final version
  162. if strings.Contains(irc.SemVer, "unreleased") {
  163. logman.Warning("server", "You are currently running an unreleased beta version of Oragono that may be unstable and could corrupt your database.\nIf you are running a production network, please download the latest build from https://oragono.io/downloads.html and run that instead.")
  164. }
  165. server, err := irc.NewServer(config, logman)
  166. if err != nil {
  167. logman.Error("server", fmt.Sprintf("Could not load server: %s", err.Error()))
  168. os.Exit(1)
  169. }
  170. if !arguments["--quiet"].(bool) {
  171. logman.Info("server", "Server running")
  172. defer logman.Info("server", fmt.Sprintf("Oragono v%s exiting", irc.SemVer))
  173. }
  174. server.Run()
  175. }
  176. }