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
42 type GroupKey struct {
49 func toKey(g Group) GroupKey {
54 Members: strings.Join(g.Members, ","),
58 // ParseGroups parses a file in the format of /etc/group and returns all
59 // entries as Group structs.
60 func ParseGroups(r io.Reader) ([]Group, error) {
63 s := bufio.NewScanner(r)
67 x := strings.Split(t, ":")
69 return nil, fmt.Errorf("invalid line %q", t)
72 gid, err := strconv.ParseUint(x[2], 10, 64)
74 return nil, errors.Wrapf(err, "invalid gid in line %q", t)
78 // No members must result in empty slice, not slice with the
81 members = strings.Split(x[3], ",")
83 res = append(res, Group{
98 func SerializeGroup(g Group) []byte {
99 le := binary.LittleEndian
101 // Concatenate all (NUL-terminated) strings and store the offsets
102 var mems bytes.Buffer
103 var mems_off []uint16
104 for _, m := range g.Members {
105 mems_off = append(mems_off, uint16(mems.Len()))
106 mems.Write([]byte(m))
109 var data bytes.Buffer
110 data.Write([]byte(g.Name))
112 offPasswd := uint16(data.Len())
113 data.Write([]byte(g.Passwd))
115 // Padding to align the following uint16
116 if data.Len()%2 != 0 {
119 offMemOff := uint16(data.Len())
120 // Offsets for group members
121 offMem := offMemOff + 2*uint16(len(mems_off))
122 for _, o := range mems_off {
123 tmp := make([]byte, 2)
124 le.PutUint16(tmp, offMem+o)
127 // And the group members concatenated as above
128 data.Write(mems.Bytes())
129 size := uint16(data.Len())
131 var res bytes.Buffer // serialized result
133 id := make([]byte, 8)
135 le.PutUint64(id, g.Gid)
138 off := make([]byte, 2)
140 le.PutUint16(off, offPasswd)
143 le.PutUint16(off, offMemOff)
146 le.PutUint16(off, uint16(len(g.Members)))
149 le.PutUint16(off, size)
152 res.Write(data.Bytes())
153 // We must pad each entry so that all uint64 at the beginning of the
154 // struct are 8 byte aligned
157 for i := 0; i < 8-l%8; i++ {
165 func SerializeGroups(w io.Writer, grs []Group) error {
166 // Serialize groups and store offsets
167 var data bytes.Buffer
168 offsets := make(map[GroupKey]uint64)
169 for _, g := range grs {
170 // TODO: warn about duplicate entries
171 offsets[toKey(g)] = uint64(data.Len())
172 data.Write(SerializeGroup(g))
175 // Copy to prevent sorting from modifying the argument
176 sorted := make([]Group, len(grs))
179 le := binary.LittleEndian
180 tmp := make([]byte, 8)
182 // Create index "sorted" in input order, used when iterating over all
183 // passwd entries (getgrent_r); keeping the original order makes
185 var indexOrig bytes.Buffer
186 for _, g := range grs {
187 le.PutUint64(tmp, offsets[toKey(g)])
191 // Create index sorted after id
192 var indexId bytes.Buffer
193 sort.Slice(sorted, func(i, j int) bool {
194 return sorted[i].Gid < sorted[j].Gid
196 for _, g := range sorted {
197 le.PutUint64(tmp, offsets[toKey(g)])
201 // Create index sorted after name
202 var indexName bytes.Buffer
203 sort.Slice(sorted, func(i, j int) bool {
204 return sorted[i].Name < sorted[j].Name
206 for _, g := range sorted {
207 le.PutUint64(tmp, offsets[toKey(g)])
212 if indexOrig.Len() != indexId.Len() ||
213 indexId.Len() != indexName.Len() {
214 return fmt.Errorf("indexes have inconsistent length")
220 w.Write([]byte("NSS-CASH"))
222 le.PutUint64(tmp, GroupVersion)
225 le.PutUint64(tmp, uint64(len(grs)))
229 le.PutUint64(tmp, offset)
232 offset += uint64(indexOrig.Len())
233 le.PutUint64(tmp, offset)
236 offset += uint64(indexId.Len())
237 le.PutUint64(tmp, offset)
240 offset += uint64(indexName.Len())
241 le.PutUint64(tmp, offset)
244 _, err := indexOrig.WriteTo(w)
248 _, err = indexId.WriteTo(w)
252 _, err = indexName.WriteTo(w)
256 _, err = data.WriteTo(w)