]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - group.go
nsscash: add "ca" option for files
[nsscash/nsscash.git] / group.go
index 0ad86711431d068ff0922ceace8c39cdba01a1a0..66da8f00551e32de8272a5b02b2fdb7c4006039f 100644 (file)
--- a/group.go
+++ b/group.go
@@ -23,6 +23,7 @@ import (
        "encoding/binary"
        "fmt"
        "io"
+       "math"
        "sort"
        "strconv"
        "strings"
@@ -62,9 +63,20 @@ func toKey(g Group) GroupKey {
 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 {
@@ -76,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
@@ -89,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
@@ -125,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
@@ -153,7 +169,7 @@ func SerializeGroup(g Group) []byte {
        // struct are 8 byte aligned
        alignBufferTo(&res, 8)
 
-       return res.Bytes()
+       return res.Bytes(), nil
 }
 
 func SerializeGroups(w io.Writer, grs []Group) error {
@@ -163,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