1 // Download and write files atomically to the file system
3 // Copyright (C) 2019 Simon Ruderich
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.
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.
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/>.
33 "github.com/pkg/errors"
36 func handleFiles(cfg *Config, state *State) error {
37 // Split into fetch and deploy phase to prevent updates of only some
38 // files which might lead to inconsistent state; obviously this won't
39 // work during the deploy phase, but it helps if the web server fails
40 // to deliver some files
42 for i, f := range cfg.Files {
43 err := fetchFile(&cfg.Files[i], state)
45 return errors.Wrapf(err, "%q (%v)", f.Url, f.Type)
49 for i, f := range cfg.Files {
55 err := deployFile(&cfg.Files[i])
57 return errors.Wrapf(err, "%q (%v)", f.Url, f.Type)
64 func checksumFile(file *File) (string, error) {
65 x, err := ioutil.ReadFile(file.Path)
69 return checksumBytes(x), nil
72 func checksumBytes(x []byte) string {
75 return hex.EncodeToString(h.Sum(nil))
78 func fetchFile(file *File, state *State) error {
79 t := state.LastModified[file.Url]
81 hash, err := checksumFile(file)
83 // See below in deployFile() for the reason
84 return errors.Wrapf(err, "file.path %q must exist", file.Path)
86 if hash != state.Checksum[file.Url] {
87 log.Printf("%q -> %q: hash has changed", file.Url, file.Path)
89 t = zero // force download
93 status, body, err := fetchIfModified(file.Url,
94 file.Username, file.Password, file.CA, &t)
98 if status == http.StatusNotModified {
100 return fmt.Errorf("status code 304 " +
101 "but did not send If-Modified-Since")
103 log.Printf("%q -> %q: not modified", file.Url, file.Path)
106 if status != http.StatusOK {
107 return fmt.Errorf("status code %v", status)
109 state.LastModified[file.Url] = t
111 if file.Type == FileTypePlain {
113 return fmt.Errorf("refusing to use empty response")
117 } else if file.Type == FileTypePasswd {
118 pws, err := ParsePasswds(bytes.NewReader(body))
122 // Safety check: having no users can be very dangerous, don't
125 return fmt.Errorf("refusing to use empty passwd file")
129 err = SerializePasswds(&x, pws)
133 file.body = x.Bytes()
135 } else if file.Type == FileTypeGroup {
136 grs, err := ParseGroups(bytes.NewReader(body))
141 return fmt.Errorf("refusing to use empty group file")
145 err = SerializeGroups(&x, grs)
149 file.body = x.Bytes()
152 return fmt.Errorf("unsupported file type %v", file.Type)
155 state.Checksum[file.Url] = checksumBytes(file.body)
159 func deployFile(file *File) error {
160 log.Printf("%q -> %q: updating file", file.Url, file.Path)
163 if len(file.body) == 0 {
164 return fmt.Errorf("refusing to write empty file")
167 // Write the file in an atomic fashion by creating a temporary file
168 // and renaming it over the target file
170 dir := filepath.Dir(file.Path)
171 name := filepath.Base(file.Path)
173 f, err := ioutil.TempFile(dir, "tmp-"+name+"-")
177 defer os.Remove(f.Name())
180 // Apply permissions/user/group from the target file but remove the
181 // write permissions to discourage manual modifications, use Stat
182 // instead of Lstat as only the target's permissions are relevant
183 stat, err := os.Stat(file.Path)
185 // We do not create the path if it doesn't exist, because we
186 // do not know the proper permissions
187 return errors.Wrapf(err, "file.path %q must exist", file.Path)
189 err = f.Chmod(stat.Mode() & ^os.FileMode(0222)) // remove write perms
193 // TODO: support more systems
194 sys, ok := stat.Sys().(*syscall.Stat_t)
196 return fmt.Errorf("unsupported FileInfo.Sys()")
198 err = f.Chown(int(sys.Uid), int(sys.Gid))
203 _, err = f.Write(file.body)
211 return os.Rename(f.Name(), file.Path)