common.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * swsyd - swsy daemon, make nsswitch easy
  3. * Copyright (C) 2024 Marcus Pedersén marcus@marcux.org
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package common
  19. import (
  20. "os"
  21. "fmt"
  22. "log/slog"
  23. "strings"
  24. "path/filepath"
  25. "github.com/spf13/viper"
  26. )
  27. // Default config
  28. const CONFIGFILE string = "swsyd.conf"
  29. const CONFIGPATH string = "/etc/swsy/"
  30. const PORT uint16 = 9779
  31. const DBFILE string = "/var/lib/swsy/swsyd.db"
  32. const LOGFILE string = "/var/log/swsy/swsyd.log"
  33. const MinUID = 10000
  34. const MinGID = 10000
  35. const DefaultHome = "/home"
  36. const DefaultShell = "/usr/bin/bash"
  37. type Config struct {
  38. Port uint16
  39. Dbfile string
  40. Logfile string
  41. Loglevel slog.Level
  42. MinUid uint64
  43. MinGid uint64
  44. DefaultHome string
  45. DefaultShell string
  46. }
  47. // Parse config file
  48. // if values are missing
  49. // default is used
  50. func GetConfig() Config {
  51. conf := Config{}
  52. viper.SetConfigName(CONFIGFILE)
  53. viper.SetConfigType("toml")
  54. viper.AddConfigPath(CONFIGPATH)
  55. if err := viper.ReadInConfig(); err != nil {
  56. fmt.Fprintf(os.Stderr, "Failed to read config file: %s\n%s\n",
  57. filepath.Join(CONFIGPATH, CONFIGFILE), err)
  58. fmt.Println("Using default config values")
  59. conf.Port = PORT
  60. conf.Dbfile = DBFILE
  61. conf.Logfile = LOGFILE
  62. conf.Loglevel = slog.LevelInfo
  63. conf.MinUid = MinUID
  64. conf.MinGid = MinGID
  65. conf.DefaultHome = DefaultHome
  66. conf.DefaultShell = DefaultShell
  67. } else {
  68. conf.Port = viper.GetUint16("swsyd.port")
  69. if conf.Port == 0 {
  70. conf.Port = PORT
  71. }
  72. conf.MinUid = viper.GetUint64("swsyd.minuid")
  73. if conf.MinUid == 0 {
  74. conf.MinUid = MinUID
  75. }
  76. conf.MinGid = viper.GetUint64("swsyd.mingid")
  77. if conf.MinGid == 0 {
  78. conf.MinGid = MinGID
  79. }
  80. conf.DefaultHome = viper.GetString("swsyd.defaulthome")
  81. if len(conf.DefaultHome) == 0 {
  82. conf.DefaultHome = DefaultHome
  83. }
  84. conf.DefaultShell = viper.GetString("swsyd.defaultshell")
  85. if len(conf.DefaultShell) == 0 {
  86. conf.DefaultShell = DefaultShell
  87. }
  88. conf.Dbfile = viper.GetString("swsyd.dbfile")
  89. if len(conf.Dbfile) == 0 {
  90. conf.Dbfile = DBFILE
  91. }
  92. conf.Logfile = viper.GetString("swsyd.logfile")
  93. if len(conf.Logfile) == 0 {
  94. conf.Logfile = LOGFILE
  95. }
  96. level := strings.ToLower(viper.GetString("swsyd.loglevel"))
  97. if level == "debug" {
  98. conf.Loglevel = slog.LevelDebug
  99. } else if level == "warn" {
  100. conf.Loglevel = slog.LevelWarn
  101. } else if level == "error" {
  102. conf.Loglevel = slog.LevelError
  103. } else {
  104. conf.Loglevel = slog.LevelInfo
  105. }
  106. }
  107. return conf
  108. }