1 // Config: parse permissions.yaml
3 // Copyright (C) 2021 Simon Ruderich
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.
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.
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/>.
30 "ruderich.org/simon/safcm"
33 func LoadPermissions(group string, files map[string]*safcm.File) error {
34 path := filepath.Join(group, "permissions.yaml")
36 var cfg map[string]string
37 x, err := os.ReadFile(path)
39 if os.IsNotExist(err) {
44 err = yaml.UnmarshalStrict(x, &cfg)
46 return fmt.Errorf("%s: failed to load: %v", path, err)
49 for p, x := range cfg {
52 return fmt.Errorf("%s: %q does not exist in files/",
56 xs := strings.Fields(x)
57 if len(xs) != 1 && len(xs) != 3 {
58 return fmt.Errorf("%s: invalid line %q "+
59 "(expected <perm> [<user> <group>])",
62 perm, err := strconv.ParseInt(xs[0], 8, 32)
64 return fmt.Errorf("%s: invalid permission %q "+
65 "(expected e.g. %q or %q)",
66 path, xs[0], "0644", "01777")
68 if perm < 0 || perm > 07777 {
69 return fmt.Errorf("%s: invalid permission %#o "+
70 "(expected e.g. %#o or %#o)",
71 path, perm, 0644, 01777)
76 if file.Mode.Perm()&0111 != 0 && perm&0111 == 0 {
78 "%s: %q: trying to remove +x from file, "+
79 "manually chmod -x in files/",
82 file.Mode = file.Mode.Type() | FullPermToFileMode(int(perm))
92 func FileModeToFullPerm(mode fs.FileMode) int {
94 if mode&fs.ModeSticky != 0 {
97 if mode&fs.ModeSetgid != 0 {
100 if mode&fs.ModeSetuid != 0 {
106 func FullPermToFileMode(perm int) fs.FileMode {
107 mode := fs.FileMode(perm & 0777)
109 mode |= fs.ModeSticky
112 mode |= fs.ModeSetgid
115 mode |= fs.ModeSetuid