groups_unix.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright The containerd Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package continuity
  14. import (
  15. "bufio"
  16. "fmt"
  17. "io"
  18. "os"
  19. "strconv"
  20. "strings"
  21. )
  22. // TODO(stevvooe): This needs a lot of work before we can call it useful.
  23. type groupIndex struct {
  24. byName map[string]*group
  25. byGID map[int]*group
  26. }
  27. func getGroupIndex() (*groupIndex, error) {
  28. f, err := os.Open("/etc/group")
  29. if err != nil {
  30. return nil, err
  31. }
  32. defer f.Close()
  33. groups, err := parseGroups(f)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return newGroupIndex(groups), nil
  38. }
  39. func newGroupIndex(groups []group) *groupIndex {
  40. gi := &groupIndex{
  41. byName: make(map[string]*group),
  42. byGID: make(map[int]*group),
  43. }
  44. for i, group := range groups {
  45. gi.byGID[group.gid] = &groups[i]
  46. gi.byName[group.name] = &groups[i]
  47. }
  48. return gi
  49. }
  50. type group struct {
  51. name string
  52. gid int
  53. members []string
  54. }
  55. func getGroupName(gid int) (string, error) {
  56. f, err := os.Open("/etc/group")
  57. if err != nil {
  58. return "", err
  59. }
  60. defer f.Close()
  61. groups, err := parseGroups(f)
  62. if err != nil {
  63. return "", err
  64. }
  65. for _, group := range groups {
  66. if group.gid == gid {
  67. return group.name, nil
  68. }
  69. }
  70. return "", fmt.Errorf("no group for gid")
  71. }
  72. // parseGroups parses an /etc/group file for group names, ids and membership.
  73. // This is unix specific.
  74. func parseGroups(rd io.Reader) ([]group, error) {
  75. var groups []group
  76. scanner := bufio.NewScanner(rd)
  77. for scanner.Scan() {
  78. if strings.HasPrefix(scanner.Text(), "#") {
  79. continue // skip comment
  80. }
  81. parts := strings.SplitN(scanner.Text(), ":", 4)
  82. if len(parts) != 4 {
  83. return nil, fmt.Errorf("bad entry: %q", scanner.Text())
  84. }
  85. name, _, sgid, smembers := parts[0], parts[1], parts[2], parts[3]
  86. gid, err := strconv.Atoi(sgid)
  87. if err != nil {
  88. return nil, fmt.Errorf("bad gid: %q", gid)
  89. }
  90. members := strings.Split(smembers, ",")
  91. groups = append(groups, group{
  92. name: name,
  93. gid: gid,
  94. members: members,
  95. })
  96. }
  97. if scanner.Err() != nil {
  98. return nil, scanner.Err()
  99. }
  100. return groups, nil
  101. }