github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/pkg/group/group.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package group 16 17 import ( 18 "bufio" 19 "errors" 20 "fmt" 21 "io" 22 "os" 23 "strconv" 24 "strings" 25 26 "github.com/hashicorp/errwrap" 27 ) 28 29 const ( 30 groupFilePath = "/etc/group" 31 ) 32 33 // Group represents an entry in the group file. 34 type Group struct { 35 Name string 36 Pass string 37 Gid int 38 Users []string 39 } 40 41 // LookupGid reads the group file specified by groupFile, and returns the gid of the group 42 // specified by groupName. 43 func LookupGidFromFile(groupName, groupFile string) (gid int, err error) { 44 groups, err := parseGroupFile(groupFile) 45 if err != nil { 46 return -1, errwrap.Wrap(fmt.Errorf("error parsing %q file", groupFile), err) 47 } 48 49 group, ok := groups[groupName] 50 if !ok { 51 return -1, fmt.Errorf("%q group not found", groupName) 52 } 53 54 return group.Gid, nil 55 } 56 57 // LookupGid reads the group file and returns the gid of the group 58 // specified by groupName. 59 func LookupGid(groupName string) (gid int, err error) { 60 return LookupGidFromFile(groupName, groupFilePath) 61 } 62 63 func parseGroupFile(path string) (group map[string]*Group, err error) { 64 groupFile, err := os.Open(path) 65 if err != nil { 66 return nil, err 67 } 68 defer groupFile.Close() 69 70 return parseGroups(groupFile) 71 } 72 73 func parseGroups(r io.Reader) (group map[string]*Group, err error) { 74 s := bufio.NewScanner(r) 75 out := make(map[string]*Group) 76 77 for s.Scan() { 78 if err := s.Err(); err != nil { 79 return nil, err 80 } 81 82 text := s.Text() 83 if text == "" { 84 continue 85 } 86 87 p, err := parseGroupLine(text) 88 if err != nil { 89 return nil, errwrap.Wrap(errors.New("error parsing line"), err) 90 } 91 92 out[p.Name] = p 93 } 94 95 return out, nil 96 } 97 98 func parseGroupLine(line string) (*Group, error) { 99 const ( 100 NameIdx = iota 101 PassIdx 102 GidIdx 103 UsersIdx 104 ) 105 var err error 106 107 if line == "" { 108 return nil, errors.New("cannot parse empty line") 109 } 110 111 splits := strings.Split(line, ":") 112 if len(splits) < 4 { 113 return nil, fmt.Errorf("expected at least 4 fields, got %d", len(splits)) 114 } 115 116 group := &Group{ 117 Name: splits[NameIdx], 118 Pass: splits[PassIdx], 119 } 120 121 group.Gid, err = strconv.Atoi(splits[GidIdx]) 122 if err != nil { 123 return nil, errwrap.Wrap(errors.New("unable to parse gid"), err) 124 } 125 126 u := splits[UsersIdx] 127 if u != "" { 128 group.Users = strings.Split(u, ",") 129 } else { 130 group.Users = []string{} 131 } 132 133 return group, nil 134 }