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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. func fileDoesNotExist(file string) bool {
  37. if _, err := os.Stat(file); os.IsNotExist(err) {
  38. return true
  39. }
  40. return false
  41. }
  42. // implements the `oragono mkcerts` command
  43. func doMkcerts(configFile string, quiet bool) {
  44. config, err := irc.LoadRawConfig(configFile)
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. if !quiet {
  49. log.Println("making self-signed certificates")
  50. }
  51. certToKey := make(map[string]string)
  52. for name, conf := range config.Server.Listeners {
  53. if conf.TLS.Cert == "" {
  54. continue
  55. }
  56. existingKey, ok := certToKey[conf.TLS.Cert]
  57. if ok {
  58. if existingKey == conf.TLS.Key {
  59. continue
  60. } else {
  61. log.Fatal("Conflicting TLS key files for ", conf.TLS.Cert)
  62. }
  63. }
  64. if !quiet {
  65. log.Printf(" making cert for %s listener\n", name)
  66. }
  67. host := config.Server.Name
  68. cert, key := conf.TLS.Cert, conf.TLS.Key
  69. if !(fileDoesNotExist(cert) && fileDoesNotExist(key)) {
  70. log.Fatalf("Preexisting TLS cert and/or key files: %s %s", cert, key)
  71. }
  72. err := mkcerts.CreateCert("Oragono", host, cert, key)
  73. if err == nil {
  74. if !quiet {
  75. log.Printf(" Certificate created at %s : %s\n", cert, key)
  76. }
  77. certToKey[cert] = key
  78. } else {
  79. log.Fatal(" Could not create certificate:", err.Error())
  80. }
  81. }
  82. }
  83. func main() {
  84. version := irc.SemVer
  85. usage := `oragono.
  86. Usage:
  87. oragono initdb [--conf <filename>] [--quiet]
  88. oragono upgradedb [--conf <filename>] [--quiet]
  89. oragono genpasswd [--conf <filename>] [--quiet]
  90. oragono mkcerts [--conf <filename>] [--quiet]
  91. oragono mksecret [--conf <filename>] [--quiet]
  92. oragono run [--conf <filename>] [--quiet]
  93. oragono -h | --help
  94. oragono --version
  95. Options:
  96. --conf <filename> Configuration file to use [default: ircd.yaml].
  97. --quiet Don't show startup/shutdown lines.
  98. -h --help Show this screen.
  99. --version Show version.`
  100. arguments, _ := docopt.ParseArgs(usage, nil, version)
  101. // don't require a config file for genpasswd or mksecret
  102. if arguments["genpasswd"].(bool) {
  103. var password string
  104. fd := int(os.Stdin.Fd())
  105. if terminal.IsTerminal(fd) {
  106. fmt.Print("Enter Password: ")
  107. password = getPassword()
  108. fmt.Print("\n")
  109. fmt.Print("Reenter Password: ")
  110. confirm := getPassword()
  111. fmt.Print("\n")
  112. if confirm != password {
  113. log.Fatal("passwords do not match")
  114. }
  115. } else {
  116. password = getPassword()
  117. }
  118. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
  119. if err != nil {
  120. log.Fatal("encoding error:", err.Error())
  121. }
  122. fmt.Print(string(hash))
  123. if terminal.IsTerminal(fd) {
  124. fmt.Println()
  125. }
  126. return
  127. } else if arguments["mksecret"].(bool) {
  128. fmt.Println(utils.GenerateSecretKey())
  129. return
  130. } else if arguments["mkcerts"].(bool) {
  131. doMkcerts(arguments["--conf"].(string), arguments["--quiet"].(bool))
  132. return
  133. }
  134. configfile := arguments["--conf"].(string)
  135. config, err := irc.LoadConfig(configfile)
  136. if err != nil && !(err == irc.ErrInvalidCertKeyPair && arguments["mkcerts"].(bool)) {
  137. log.Fatal("Config file did not load successfully: ", err.Error())
  138. }
  139. logman, err := logger.NewManager(config.Logging)
  140. if err != nil {
  141. log.Fatal("Logger did not load successfully:", err.Error())
  142. }
  143. if arguments["initdb"].(bool) {
  144. irc.InitDB(config.Datastore.Path)
  145. if !arguments["--quiet"].(bool) {
  146. log.Println("database initialized: ", config.Datastore.Path)
  147. }
  148. } else if arguments["upgradedb"].(bool) {
  149. err = irc.UpgradeDB(config)
  150. if err != nil {
  151. log.Fatal("Error while upgrading db:", err.Error())
  152. }
  153. if !arguments["--quiet"].(bool) {
  154. log.Println("database upgraded: ", config.Datastore.Path)
  155. }
  156. } else if arguments["run"].(bool) {
  157. if !arguments["--quiet"].(bool) {
  158. logman.Info("server", fmt.Sprintf("Oragono v%s starting", irc.SemVer))
  159. if commit == "" {
  160. logman.Debug("server", fmt.Sprintf("Could not get current commit"))
  161. } else {
  162. logman.Info("server", fmt.Sprintf("Running commit %s", commit))
  163. }
  164. }
  165. // set current git commit
  166. irc.Commit = commit
  167. if commit != "" {
  168. irc.Ver = fmt.Sprintf("%s-%s", irc.Ver, commit)
  169. }
  170. // warning if running a non-final version
  171. if strings.Contains(irc.SemVer, "unreleased") {
  172. 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.")
  173. }
  174. server, err := irc.NewServer(config, logman)
  175. if err != nil {
  176. logman.Error("server", fmt.Sprintf("Could not load server: %s", err.Error()))
  177. os.Exit(1)
  178. }
  179. if !arguments["--quiet"].(bool) {
  180. logman.Info("server", "Server running")
  181. defer logman.Info("server", fmt.Sprintf("Oragono v%s exiting", irc.SemVer))
  182. }
  183. server.Run()
  184. }
  185. }