1 // Parse /etc/group files and serialize them
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/>.
30 "github.com/pkg/errors"
33 // Version written in SerializeGroups()
34 const GroupVersion = 1
43 // Go does not support slices in map keys; therefore, use this separate struct
44 type GroupKey struct {
48 Members string // "," separated
51 func toKey(g Group) GroupKey {
56 Members: strings.Join(g.Members, ","),
60 // ParseGroups parses a file in the format of /etc/group and returns all
61 // entries as slice of Group structs.
62 func ParseGroups(r io.Reader) ([]Group, error) {
65 s := bufio.NewScanner(r)
69 x := strings.Split(t, ":")
71 return nil, fmt.Errorf("invalid line %q", t)
74 gid, err := strconv.ParseUint(x[2], 10, 64)
76 return nil, errors.Wrapf(err, "invalid gid in line %q", t)
80 // No members must result in empty slice, not slice with the
83 members = strings.Split(x[3], ",")
85 res = append(res, Group{
100 func SerializeGroup(g Group) []byte {
101 le := binary.LittleEndian
103 // Concatenate all (NUL-terminated) strings and store the offsets
104 var mems bytes.Buffer
105 var mems_off []uint16
106 for _, m := range g.Members {
107 mems_off = append(mems_off, uint16(mems.Len()))
108 mems.Write([]byte(m))
111 var data bytes.Buffer
112 data.Write([]byte(g.Name))
114 offPasswd := uint16(data.Len())
115 data.Write([]byte(g.Passwd))
117 alignBufferTo(&data, 2) // align the following uint16
118 offMemOff := uint16(data.Len())
119 // Offsets for group members
120 offMem := offMemOff + 2*uint16(len(mems_off))
121 for _, o := range mems_off {
122 tmp := make([]byte, 2)
123 le.PutUint16(tmp, offMem+o)
126 // And the group members concatenated as above
127 data.Write(mems.Bytes())
128 size := uint16(data.Len())
130 var res bytes.Buffer // serialized result
132 id := make([]byte, 8)
134 le.PutUint64(id, g.Gid)
137 off := make([]byte, 2)
139 le.PutUint16(off, offPasswd)
142 le.PutUint16(off, offMemOff)
145 le.PutUint16(off, uint16(len(g.Members)))
148 le.PutUint16(off, size)
151 res.Write(data.Bytes())
152 // We must pad each entry so that all uint64 at the beginning of the
153 // struct are 8 byte aligned
154 alignBufferTo(&res, 8)
159 func SerializeGroups(w io.Writer, grs []Group) error {
160 // Serialize groups and store offsets
161 var data bytes.Buffer
162 offsets := make(map[GroupKey]uint64)
163 for _, g := range grs {
164 // TODO: warn about duplicate entries
165 offsets[toKey(g)] = uint64(data.Len())
166 data.Write(SerializeGroup(g))
169 // Copy to prevent sorting from modifying the argument
170 sorted := make([]Group, len(grs))
173 le := binary.LittleEndian
174 tmp := make([]byte, 8)
176 // Create index "sorted" in input order, used when iterating over all
177 // passwd entries (getgrent_r); keeping the original order makes
179 var indexOrig bytes.Buffer
180 for _, g := range grs {
181 le.PutUint64(tmp, offsets[toKey(g)])
185 // Create index sorted after id
186 var indexId bytes.Buffer
187 sort.Slice(sorted, func(i, j int) bool {
188 return sorted[i].Gid < sorted[j].Gid
190 for _, g := range sorted {
191 le.PutUint64(tmp, offsets[toKey(g)])
195 // Create index sorted after name
196 var indexName bytes.Buffer
197 sort.Slice(sorted, func(i, j int) bool {
198 return sorted[i].Name < sorted[j].Name
200 for _, g := range sorted {
201 le.PutUint64(tmp, offsets[toKey(g)])
206 if len(grs)*8 != indexOrig.Len() ||
207 indexOrig.Len() != indexId.Len() ||
208 indexId.Len() != indexName.Len() {
209 return fmt.Errorf("indexes have inconsistent length")
215 w.Write([]byte("NSS-CASH"))
217 le.PutUint64(tmp, GroupVersion)
220 le.PutUint64(tmp, uint64(len(grs)))
224 le.PutUint64(tmp, offset)
227 offset += uint64(indexOrig.Len())
228 le.PutUint64(tmp, offset)
231 offset += uint64(indexId.Len())
232 le.PutUint64(tmp, offset)
235 offset += uint64(indexName.Len())
236 le.PutUint64(tmp, offset)
239 _, err := indexOrig.WriteTo(w)
243 _, err = indexId.WriteTo(w)
247 _, err = indexName.WriteTo(w)
251 _, err = data.WriteTo(w)