]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - group.go
.github: update upstream actions to latest version
[nsscash/nsscash.git] / group.go
index 02502f62a147464eee533c8ce3a3ec1683fb0748..018a0c32716c665c690c78903b6eccdb5435ac54 100644 (file)
--- a/group.go
+++ b/group.go
@@ -1,6 +1,6 @@
 // Parse /etc/group files and serialize them
 
-// Copyright (C) 2019  Simon Ruderich
+// Copyright (C) 2019-2021  Simon Ruderich
 //
 // This program is free software: you can redistribute it and/or modify
 // it under the terms of the GNU Affero General Public License as published by
@@ -23,6 +23,7 @@ import (
        "encoding/binary"
        "fmt"
        "io"
+       "math"
        "sort"
        "strconv"
        "strings"
@@ -39,11 +40,13 @@ type Group struct {
        Gid     uint64
        Members []string
 }
+
+// Go does not support slices in map keys; therefore, use this separate struct
 type GroupKey struct {
        Name    string
        Passwd  string
        Gid     uint64
-       Members string
+       Members string // "," separated
 }
 
 func toKey(g Group) GroupKey {
@@ -56,13 +59,24 @@ func toKey(g Group) GroupKey {
 }
 
 // ParseGroups parses a file in the format of /etc/group and returns all
-// entries as Group structs.
+// entries as slice of Group structs.
 func ParseGroups(r io.Reader) ([]Group, error) {
        var res []Group
 
-       s := bufio.NewScanner(r)
-       for s.Scan() {
-               t := s.Text()
+       s := bufio.NewReader(r)
+       for {
+               t, err := s.ReadString('\n')
+               if err != nil {
+                       if err == io.EOF {
+                               if t != "" {
+                                       return nil, fmt.Errorf(
+                                               "no newline in last line: %q",
+                                               t)
+                               }
+                               break
+                       }
+                       return nil, err
+               }
 
                x := strings.Split(t, ":")
                if len(x) != 4 {
@@ -74,6 +88,9 @@ func ParseGroups(r io.Reader) ([]Group, error) {
                        return nil, errors.Wrapf(err, "invalid gid in line %q", t)
                }
 
+               // ReadString() contains the delimiter
+               x[3] = strings.TrimSuffix(x[3], "\n")
+
                var members []string
                // No members must result in empty slice, not slice with the
                // empty string
@@ -87,15 +104,11 @@ func ParseGroups(r io.Reader) ([]Group, error) {
                        Members: members,
                })
        }
-       err := s.Err()
-       if err != nil {
-               return nil, err
-       }
 
        return res, nil
 }
 
-func SerializeGroup(g Group) []byte {
+func SerializeGroup(g Group) ([]byte, error) {
        le := binary.LittleEndian
 
        // Concatenate all (NUL-terminated) strings and store the offsets
@@ -112,10 +125,7 @@ func SerializeGroup(g Group) []byte {
        offPasswd := uint16(data.Len())
        data.Write([]byte(g.Passwd))
        data.WriteByte(0)
-       // Padding to align the following uint16
-       if data.Len()%2 != 0 {
-               data.WriteByte(0)
-       }
+       alignBufferTo(&data, 2) // align the following uint16
        offMemOff := uint16(data.Len())
        // Offsets for group members
        offMem := offMemOff + 2*uint16(len(mems_off))
@@ -126,6 +136,11 @@ func SerializeGroup(g Group) []byte {
        }
        // And the group members concatenated as above
        data.Write(mems.Bytes())
+       // Ensure the offsets can fit the length of this entry
+       if data.Len() > math.MaxUint16 {
+               return nil, fmt.Errorf("group too large to serialize: %v, %v",
+                       data.Len(), g)
+       }
        size := uint16(data.Len())
 
        var res bytes.Buffer // serialized result
@@ -152,14 +167,9 @@ func SerializeGroup(g Group) []byte {
        res.Write(data.Bytes())
        // We must pad each entry so that all uint64 at the beginning of the
        // struct are 8 byte aligned
-       l := res.Len()
-       if l%8 != 0 {
-               for i := 0; i < 8-l%8; i++ {
-                       res.WriteByte(0)
-               }
-       }
+       alignBufferTo(&res, 8)
 
-       return res.Bytes()
+       return res.Bytes(), nil
 }
 
 func SerializeGroups(w io.Writer, grs []Group) error {
@@ -169,7 +179,11 @@ func SerializeGroups(w io.Writer, grs []Group) error {
        for _, g := range grs {
                // TODO: warn about duplicate entries
                offsets[toKey(g)] = uint64(data.Len())
-               data.Write(SerializeGroup(g))
+               x, err := SerializeGroup(g)
+               if err != nil {
+                       return err
+               }
+               data.Write(x)
        }
 
        // Copy to prevent sorting from modifying the argument
@@ -180,7 +194,7 @@ func SerializeGroups(w io.Writer, grs []Group) error {
        tmp := make([]byte, 8)
 
        // Create index "sorted" in input order, used when iterating over all
-       // passwd entries (getgrent_r); keeping the original order makes
+       // group entries (getgrent_r); keeping the original order makes
        // debugging easier
        var indexOrig bytes.Buffer
        for _, g := range grs {
@@ -209,7 +223,8 @@ func SerializeGroups(w io.Writer, grs []Group) error {
        }
 
        // Sanity check
-       if indexOrig.Len() != indexId.Len() ||
+       if len(grs)*8 != indexOrig.Len() ||
+               indexOrig.Len() != indexId.Len() ||
                indexId.Len() != indexName.Len() {
                return fmt.Errorf("indexes have inconsistent length")
        }