Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

setting.go 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package setting
  5. import (
  6. "fmt"
  7. "os"
  8. "runtime"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/user"
  13. "code.gitea.io/gitea/modules/util"
  14. )
  15. // settings
  16. var (
  17. // AppVer is the version of the current build of Gitea. It is set in main.go from main.Version.
  18. AppVer string
  19. // AppBuiltWith represents a human-readable version go runtime build version and build tags. (See main.go formatBuiltWith().)
  20. AppBuiltWith string
  21. // AppStartTime store time gitea has started
  22. AppStartTime time.Time
  23. // Other global setting objects
  24. CfgProvider ConfigProvider
  25. RunMode string
  26. RunUser string
  27. IsProd bool
  28. IsWindows bool
  29. // IsInTesting indicates whether the testing is running. A lot of unreliable code causes a lot of nonsense error logs during testing
  30. // TODO: this is only a temporary solution, we should make the test code more reliable
  31. IsInTesting = false
  32. )
  33. func init() {
  34. IsWindows = runtime.GOOS == "windows"
  35. if AppVer == "" {
  36. AppVer = "dev"
  37. }
  38. // We can rely on log.CanColorStdout being set properly because modules/log/console_windows.go comes before modules/setting/setting.go lexicographically
  39. // By default set this logger at Info - we'll change it later, but we need to start with something.
  40. log.SetConsoleLogger(log.DEFAULT, "console", log.INFO)
  41. }
  42. // IsRunUserMatchCurrentUser returns false if configured run user does not match
  43. // actual user that runs the app. The first return value is the actual user name.
  44. // This check is ignored under Windows since SSH remote login is not the main
  45. // method to login on Windows.
  46. func IsRunUserMatchCurrentUser(runUser string) (string, bool) {
  47. if IsWindows || SSH.StartBuiltinServer {
  48. return "", true
  49. }
  50. currentUser := user.CurrentUsername()
  51. return currentUser, runUser == currentUser
  52. }
  53. // PrepareAppDataPath creates app data directory if necessary
  54. func PrepareAppDataPath() error {
  55. // FIXME: There are too many calls to MkdirAll in old code. It is incorrect.
  56. // For example, if someDir=/mnt/vol1/gitea-home/data, if the mount point /mnt/vol1 is not mounted when Gitea runs,
  57. // then gitea will make new empty directories in /mnt/vol1, all are stored in the root filesystem.
  58. // The correct behavior should be: creating parent directories is end users' duty. We only create sub-directories in existing parent directories.
  59. // For quickstart, the parent directories should be created automatically for first startup (eg: a flag or a check of INSTALL_LOCK).
  60. // Now we can take the first step to do correctly (using Mkdir) in other packages, and prepare the AppDataPath here, then make a refactor in future.
  61. st, err := os.Stat(AppDataPath)
  62. if os.IsNotExist(err) {
  63. err = os.MkdirAll(AppDataPath, os.ModePerm)
  64. if err != nil {
  65. return fmt.Errorf("unable to create the APP_DATA_PATH directory: %q, Error: %w", AppDataPath, err)
  66. }
  67. return nil
  68. }
  69. if err != nil {
  70. return fmt.Errorf("unable to use APP_DATA_PATH %q. Error: %w", AppDataPath, err)
  71. }
  72. if !st.IsDir() /* also works for symlink */ {
  73. return fmt.Errorf("the APP_DATA_PATH %q is not a directory (or symlink to a directory) and can't be used", AppDataPath)
  74. }
  75. return nil
  76. }
  77. func InitCfgProvider(file string) {
  78. var err error
  79. if CfgProvider, err = NewConfigProviderFromFile(file); err != nil {
  80. log.Fatal("Unable to init config provider from %q: %v", file, err)
  81. }
  82. CfgProvider.DisableSaving() // do not allow saving the CfgProvider into file, it will be polluted by the "MustXxx" calls
  83. }
  84. func MustInstalled() {
  85. if !InstallLock {
  86. log.Fatal(`Unable to load config file for a installed Gitea instance, you should either use "--config" to set your config file (app.ini), or run "gitea web" command to install Gitea.`)
  87. }
  88. }
  89. func LoadCommonSettings() {
  90. if err := loadCommonSettingsFrom(CfgProvider); err != nil {
  91. log.Fatal("Unable to load settings from config: %v", err)
  92. }
  93. }
  94. // loadCommonSettingsFrom loads common configurations from a configuration provider.
  95. func loadCommonSettingsFrom(cfg ConfigProvider) error {
  96. // WARNING: don't change the sequence except you know what you are doing.
  97. loadRunModeFrom(cfg)
  98. loadLogGlobalFrom(cfg)
  99. loadServerFrom(cfg)
  100. loadSSHFrom(cfg)
  101. mustCurrentRunUserMatch(cfg) // it depends on the SSH config, only non-builtin SSH server requires this check
  102. loadOAuth2From(cfg)
  103. loadSecurityFrom(cfg)
  104. if err := loadAttachmentFrom(cfg); err != nil {
  105. return err
  106. }
  107. if err := loadLFSFrom(cfg); err != nil {
  108. return err
  109. }
  110. loadTimeFrom(cfg)
  111. loadRepositoryFrom(cfg)
  112. if err := loadAvatarsFrom(cfg); err != nil {
  113. return err
  114. }
  115. if err := loadRepoAvatarFrom(cfg); err != nil {
  116. return err
  117. }
  118. if err := loadPackagesFrom(cfg); err != nil {
  119. return err
  120. }
  121. if err := loadActionsFrom(cfg); err != nil {
  122. return err
  123. }
  124. loadUIFrom(cfg)
  125. loadAdminFrom(cfg)
  126. loadAPIFrom(cfg)
  127. loadMetricsFrom(cfg)
  128. loadCamoFrom(cfg)
  129. loadI18nFrom(cfg)
  130. loadGitFrom(cfg)
  131. loadMirrorFrom(cfg)
  132. loadMarkupFrom(cfg)
  133. loadOtherFrom(cfg)
  134. return nil
  135. }
  136. func loadRunModeFrom(rootCfg ConfigProvider) {
  137. rootSec := rootCfg.Section("")
  138. RunUser = rootSec.Key("RUN_USER").MustString(user.CurrentUsername())
  139. // The following is a purposefully undocumented option. Please do not run Gitea as root. It will only cause future headaches.
  140. // Please don't use root as a bandaid to "fix" something that is broken, instead the broken thing should instead be fixed properly.
  141. unsafeAllowRunAsRoot := ConfigSectionKeyBool(rootSec, "I_AM_BEING_UNSAFE_RUNNING_AS_ROOT")
  142. unsafeAllowRunAsRoot = unsafeAllowRunAsRoot || util.OptionalBoolParse(os.Getenv("GITEA_I_AM_BEING_UNSAFE_RUNNING_AS_ROOT")).Value()
  143. RunMode = os.Getenv("GITEA_RUN_MODE")
  144. if RunMode == "" {
  145. RunMode = rootSec.Key("RUN_MODE").MustString("prod")
  146. }
  147. // non-dev mode is treated as prod mode, to protect users from accidentally running in dev mode if there is a typo in this value.
  148. RunMode = strings.ToLower(RunMode)
  149. if RunMode != "dev" {
  150. RunMode = "prod"
  151. }
  152. IsProd = RunMode != "dev"
  153. // check if we run as root
  154. if os.Getuid() == 0 {
  155. if !unsafeAllowRunAsRoot {
  156. // Special thanks to VLC which inspired the wording of this messaging.
  157. log.Fatal("Gitea is not supposed to be run as root. Sorry. If you need to use privileged TCP ports please instead use setcap and the `cap_net_bind_service` permission")
  158. }
  159. log.Critical("You are running Gitea using the root user, and have purposely chosen to skip built-in protections around this. You have been warned against this.")
  160. }
  161. }
  162. // HasInstallLock checks the install-lock in ConfigProvider directly, because sometimes the config file is not loaded into setting variables yet.
  163. func HasInstallLock(rootCfg ConfigProvider) bool {
  164. return rootCfg.Section("security").Key("INSTALL_LOCK").MustBool(false)
  165. }
  166. func mustCurrentRunUserMatch(rootCfg ConfigProvider) {
  167. // Does not check run user when the "InstallLock" is off.
  168. if HasInstallLock(rootCfg) {
  169. currentUser, match := IsRunUserMatchCurrentUser(RunUser)
  170. if !match {
  171. log.Fatal("Expect user '%s' but current user is: %s", RunUser, currentUser)
  172. }
  173. }
  174. }
  175. // LoadSettings initializes the settings for normal start up
  176. func LoadSettings() {
  177. initAllLoggers()
  178. loadDBSetting(CfgProvider)
  179. loadServiceFrom(CfgProvider)
  180. loadOAuth2ClientFrom(CfgProvider)
  181. loadCacheFrom(CfgProvider)
  182. loadSessionFrom(CfgProvider)
  183. loadCorsFrom(CfgProvider)
  184. loadMailsFrom(CfgProvider)
  185. loadProxyFrom(CfgProvider)
  186. loadWebhookFrom(CfgProvider)
  187. loadMigrationsFrom(CfgProvider)
  188. loadIndexerFrom(CfgProvider)
  189. loadTaskFrom(CfgProvider)
  190. LoadQueueSettings()
  191. loadProjectFrom(CfgProvider)
  192. loadMimeTypeMapFrom(CfgProvider)
  193. loadFederationFrom(CfgProvider)
  194. }
  195. // LoadSettingsForInstall initializes the settings for install
  196. func LoadSettingsForInstall() {
  197. loadDBSetting(CfgProvider)
  198. loadServiceFrom(CfgProvider)
  199. loadMailerFrom(CfgProvider)
  200. }
  201. var configuredPaths = make(map[string]string)
  202. func checkOverlappedPath(name, path string) {
  203. // TODO: some paths shouldn't overlap (storage.xxx.path), while some could (data path is the base path for storage path)
  204. if targetName, ok := configuredPaths[path]; ok && targetName != name {
  205. LogStartupProblem(1, log.ERROR, "Configured path %q is used by %q and %q at the same time. The paths must be unique to prevent data loss.", path, targetName, name)
  206. }
  207. configuredPaths[path] = name
  208. }