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

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