]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - config.go
README.adoc: fix syntax for nested list
[nsscash/nsscash.git] / config.go
1 // Configuration file parsing and validation
2
3 // Copyright (C) 2019  Simon Ruderich
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Affero 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 Affero General Public License for more details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18 package main
19
20 import (
21         "fmt"
22         "os"
23
24         "github.com/BurntSushi/toml"
25 )
26
27 type Config struct {
28         StatePath string
29         Files     []File `toml:"file"`
30 }
31
32 type File struct {
33         Type     FileType
34         Url      string
35         Path     string
36         CA       string
37         Username string
38         Password string
39
40         body []byte // internally used by handleFiles()
41 }
42
43 //go:generate stringer -type=FileType
44 type FileType int
45
46 const (
47         FileTypePlain FileType = iota
48         FileTypePasswd
49         FileTypeGroup
50 )
51
52 func (t *FileType) UnmarshalText(text []byte) error {
53         switch string(text) {
54         case "plain":
55                 *t = FileTypePlain
56         case "passwd":
57                 *t = FileTypePasswd
58         case "group":
59                 *t = FileTypeGroup
60         default:
61                 return fmt.Errorf("invalid file type %q", text)
62         }
63         return nil
64 }
65
66 func LoadConfig(path string) (*Config, error) {
67         var cfg Config
68
69         md, err := toml.DecodeFile(path, &cfg)
70         if err != nil {
71                 return nil, err
72         }
73         undecoded := md.Undecoded()
74         if len(undecoded) != 0 {
75                 return nil, fmt.Errorf("invalid fields used: %q", undecoded)
76         }
77
78         f, err := os.Stat(path)
79         if err != nil {
80                 return nil, err
81         }
82         perms := f.Mode().Perm()
83         unsafe := (perms & 0077) != 0 // readable by others
84
85         if cfg.StatePath == "" {
86                 return nil, fmt.Errorf("statepath must not be empty")
87         }
88
89         for i, f := range cfg.Files {
90                 if f.Url == "" {
91                         return nil, fmt.Errorf(
92                                 "file[%d].url must not be empty", i)
93                 }
94                 if f.Path == "" {
95                         return nil, fmt.Errorf(
96                                 "file[%d].path must not be empty", i)
97                 }
98                 if (f.Username != "" || f.Password != "") && unsafe {
99                         return nil, fmt.Errorf(
100                                 "file[%d].username/passsword in use and "+
101                                         "unsafe permissions %v on %q",
102                                 i, perms, path)
103                 }
104         }
105
106         return &cfg, nil
107 }