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.NewReader(r)
67 t, err := s.ReadString('\n')
75 x := strings.Split(t, ":")
77 return nil, fmt.Errorf("invalid line %q", t)
80 gid, err := strconv.ParseUint(x[2], 10, 64)
82 return nil, errors.Wrapf(err, "invalid gid in line %q", t)
85 // ReadString() contains the delimiter
86 x[3] = strings.TrimSuffix(x[3], "\n")
89 // No members must result in empty slice, not slice with the
92 members = strings.Split(x[3], ",")
94 res = append(res, Group{
105 func SerializeGroup(g Group) []byte {
106 le := binary.LittleEndian
108 // Concatenate all (NUL-terminated) strings and store the offsets
109 var mems bytes.Buffer
110 var mems_off []uint16
111 for _, m := range g.Members {
112 mems_off = append(mems_off, uint16(mems.Len()))
113 mems.Write([]byte(m))
116 var data bytes.Buffer
117 data.Write([]byte(g.Name))
119 offPasswd := uint16(data.Len())
120 data.Write([]byte(g.Passwd))
122 alignBufferTo(&data, 2) // align the following uint16
123 offMemOff := uint16(data.Len())
124 // Offsets for group members
125 offMem := offMemOff + 2*uint16(len(mems_off))
126 for _, o := range mems_off {
127 tmp := make([]byte, 2)
128 le.PutUint16(tmp, offMem+o)
131 // And the group members concatenated as above
132 data.Write(mems.Bytes())
133 size := uint16(data.Len())
135 var res bytes.Buffer // serialized result
137 id := make([]byte, 8)
139 le.PutUint64(id, g.Gid)
142 off := make([]byte, 2)
144 le.PutUint16(off, offPasswd)
147 le.PutUint16(off, offMemOff)
150 le.PutUint16(off, uint16(len(g.Members)))
153 le.PutUint16(off, size)
156 res.Write(data.Bytes())
157 // We must pad each entry so that all uint64 at the beginning of the
158 // struct are 8 byte aligned
159 alignBufferTo(&res, 8)
164 func SerializeGroups(w io.Writer, grs []Group) error {
165 // Serialize groups and store offsets
166 var data bytes.Buffer
167 offsets := make(map[GroupKey]uint64)
168 for _, g := range grs {
169 // TODO: warn about duplicate entries
170 offsets[toKey(g)] = uint64(data.Len())
171 data.Write(SerializeGroup(g))
174 // Copy to prevent sorting from modifying the argument
175 sorted := make([]Group, len(grs))
178 le := binary.LittleEndian
179 tmp := make([]byte, 8)
181 // Create index "sorted" in input order, used when iterating over all
182 // passwd entries (getgrent_r); keeping the original order makes
184 var indexOrig bytes.Buffer
185 for _, g := range grs {
186 le.PutUint64(tmp, offsets[toKey(g)])
190 // Create index sorted after id
191 var indexId bytes.Buffer
192 sort.Slice(sorted, func(i, j int) bool {
193 return sorted[i].Gid < sorted[j].Gid
195 for _, g := range sorted {
196 le.PutUint64(tmp, offsets[toKey(g)])
200 // Create index sorted after name
201 var indexName bytes.Buffer
202 sort.Slice(sorted, func(i, j int) bool {
203 return sorted[i].Name < sorted[j].Name
205 for _, g := range sorted {
206 le.PutUint64(tmp, offsets[toKey(g)])
211 if len(grs)*8 != indexOrig.Len() ||
212 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)