-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
90 lines (75 loc) · 2.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/spf13/pflag"
"github.com/spf13/viper"
nsscache "github.com/MiLk/nsscache-go"
)
var (
systemShells []string
minUID = pflag.Int("min-uid", 2000, "Minimum UID number to accept")
minGID = pflag.Int("min-gid", 2000, "Minimum GID number to accept")
defHomeDir = pflag.String("homedir", "/tmp/{UID}", "Home directory to provide if none is available from NetAuth")
defShell = pflag.String("shell", "/bin/nologin", "Default shell to use if none is provided in the directory")
outDir = pflag.String("out", "/etc", "Output directory for cache files")
cfgfile = pflag.String("config", "", "Config file to use")
log hclog.Logger
)
func initialize() {
// Grab a listing of system shells and add them here
bytes, err := ioutil.ReadFile("/etc/shells")
if err != nil {
log.Error("Error reading /etc/shells %s", "error", err)
os.Exit(2)
}
shellString := string(bytes[:])
for _, s := range strings.Split(shellString, "\n") {
if s != "" {
systemShells = append(systemShells, s)
}
}
log.Info("The system will accept the following shells")
for _, s := range systemShells {
log.Info(fmt.Sprintf(" %s", s))
}
}
func main() {
log = hclog.L().Named("nsscache")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
if *cfgfile != "" {
viper.SetConfigFile(*cfgfile)
} else {
viper.SetConfigName("config")
viper.AddConfigPath("/etc/netauth/")
viper.AddConfigPath("$HOME/.netauth")
viper.AddConfigPath(".")
}
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Error reading config:", err)
os.Exit(1)
}
// Perform initialization
initialize()
filler, err := NewCacheFiller(int32(*minUID), int32(*minGID), *defShell, *defHomeDir, systemShells)
if err != nil {
log.Error("Error initializing Cache Filler: ", "error", err)
os.Exit(1)
}
cm := nsscache.NewCaches()
if err := cm.FillCaches(filler); err != nil {
log.Error("Unable to fill caches: ", "error", err)
os.Exit(1)
}
err = cm.WriteFiles(&nsscache.WriteOptions{
Directory: *outDir,
})
if err != nil {
log.Error("Error writing updated caches: ", "error", err)
}
log.Info("Caches Updated")
}