idtools_unix.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // +build !windows
  2. package idtools
  3. import (
  4. "bytes"
  5. "fmt"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "sync"
  11. "github.com/docker/docker/pkg/system"
  12. "github.com/opencontainers/runc/libcontainer/user"
  13. )
  14. var (
  15. entOnce sync.Once
  16. getentCmd string
  17. )
  18. func mkdirAs(path string, mode os.FileMode, ownerUID, ownerGID int, mkAll, chownExisting bool) error {
  19. // make an array containing the original path asked for, plus (for mkAll == true)
  20. // all path components leading up to the complete path that don't exist before we MkdirAll
  21. // so that we can chown all of them properly at the end. If chownExisting is false, we won't
  22. // chown the full directory path if it exists
  23. var paths []string
  24. if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
  25. paths = []string{path}
  26. } else if err == nil && chownExisting {
  27. // short-circuit--we were called with an existing directory and chown was requested
  28. return os.Chown(path, ownerUID, ownerGID)
  29. } else if err == nil {
  30. // nothing to do; directory path fully exists already and chown was NOT requested
  31. return nil
  32. }
  33. if mkAll {
  34. // walk back to "/" looking for directories which do not exist
  35. // and add them to the paths array for chown after creation
  36. dirPath := path
  37. for {
  38. dirPath = filepath.Dir(dirPath)
  39. if dirPath == "/" {
  40. break
  41. }
  42. if _, err := os.Stat(dirPath); err != nil && os.IsNotExist(err) {
  43. paths = append(paths, dirPath)
  44. }
  45. }
  46. if err := system.MkdirAll(path, mode, ""); err != nil && !os.IsExist(err) {
  47. return err
  48. }
  49. } else {
  50. if err := os.Mkdir(path, mode); err != nil && !os.IsExist(err) {
  51. return err
  52. }
  53. }
  54. // even if it existed, we will chown the requested path + any subpaths that
  55. // didn't exist when we called MkdirAll
  56. for _, pathComponent := range paths {
  57. if err := os.Chown(pathComponent, ownerUID, ownerGID); err != nil {
  58. return err
  59. }
  60. }
  61. return nil
  62. }
  63. // CanAccess takes a valid (existing) directory and a uid, gid pair and determines
  64. // if that uid, gid pair has access (execute bit) to the directory
  65. func CanAccess(path string, pair IDPair) bool {
  66. statInfo, err := system.Stat(path)
  67. if err != nil {
  68. return false
  69. }
  70. fileMode := os.FileMode(statInfo.Mode())
  71. permBits := fileMode.Perm()
  72. return accessible(statInfo.UID() == uint32(pair.UID),
  73. statInfo.GID() == uint32(pair.GID), permBits)
  74. }
  75. func accessible(isOwner, isGroup bool, perms os.FileMode) bool {
  76. if isOwner && (perms&0100 == 0100) {
  77. return true
  78. }
  79. if isGroup && (perms&0010 == 0010) {
  80. return true
  81. }
  82. if perms&0001 == 0001 {
  83. return true
  84. }
  85. return false
  86. }
  87. // LookupUser uses traditional local system files lookup (from libcontainer/user) on a username,
  88. // followed by a call to `getent` for supporting host configured non-files passwd and group dbs
  89. func LookupUser(username string) (user.User, error) {
  90. // first try a local system files lookup using existing capabilities
  91. usr, err := user.LookupUser(username)
  92. if err == nil {
  93. return usr, nil
  94. }
  95. // local files lookup failed; attempt to call `getent` to query configured passwd dbs
  96. usr, err = getentUser(fmt.Sprintf("%s %s", "passwd", username))
  97. if err != nil {
  98. return user.User{}, err
  99. }
  100. return usr, nil
  101. }
  102. // LookupUID uses traditional local system files lookup (from libcontainer/user) on a uid,
  103. // followed by a call to `getent` for supporting host configured non-files passwd and group dbs
  104. func LookupUID(uid int) (user.User, error) {
  105. // first try a local system files lookup using existing capabilities
  106. usr, err := user.LookupUid(uid)
  107. if err == nil {
  108. return usr, nil
  109. }
  110. // local files lookup failed; attempt to call `getent` to query configured passwd dbs
  111. return getentUser(fmt.Sprintf("%s %d", "passwd", uid))
  112. }
  113. func getentUser(args string) (user.User, error) {
  114. reader, err := callGetent(args)
  115. if err != nil {
  116. return user.User{}, err
  117. }
  118. users, err := user.ParsePasswd(reader)
  119. if err != nil {
  120. return user.User{}, err
  121. }
  122. if len(users) == 0 {
  123. return user.User{}, fmt.Errorf("getent failed to find passwd entry for %q", strings.Split(args, " ")[1])
  124. }
  125. return users[0], nil
  126. }
  127. // LookupGroup uses traditional local system files lookup (from libcontainer/user) on a group name,
  128. // followed by a call to `getent` for supporting host configured non-files passwd and group dbs
  129. func LookupGroup(groupname string) (user.Group, error) {
  130. // first try a local system files lookup using existing capabilities
  131. group, err := user.LookupGroup(groupname)
  132. if err == nil {
  133. return group, nil
  134. }
  135. // local files lookup failed; attempt to call `getent` to query configured group dbs
  136. return getentGroup(fmt.Sprintf("%s %s", "group", groupname))
  137. }
  138. // LookupGID uses traditional local system files lookup (from libcontainer/user) on a group ID,
  139. // followed by a call to `getent` for supporting host configured non-files passwd and group dbs
  140. func LookupGID(gid int) (user.Group, error) {
  141. // first try a local system files lookup using existing capabilities
  142. group, err := user.LookupGid(gid)
  143. if err == nil {
  144. return group, nil
  145. }
  146. // local files lookup failed; attempt to call `getent` to query configured group dbs
  147. return getentGroup(fmt.Sprintf("%s %d", "group", gid))
  148. }
  149. func getentGroup(args string) (user.Group, error) {
  150. reader, err := callGetent(args)
  151. if err != nil {
  152. return user.Group{}, err
  153. }
  154. groups, err := user.ParseGroup(reader)
  155. if err != nil {
  156. return user.Group{}, err
  157. }
  158. if len(groups) == 0 {
  159. return user.Group{}, fmt.Errorf("getent failed to find groups entry for %q", strings.Split(args, " ")[1])
  160. }
  161. return groups[0], nil
  162. }
  163. func callGetent(args string) (io.Reader, error) {
  164. entOnce.Do(func() { getentCmd, _ = resolveBinary("getent") })
  165. // if no `getent` command on host, can't do anything else
  166. if getentCmd == "" {
  167. return nil, fmt.Errorf("")
  168. }
  169. out, err := execCmd(getentCmd, args)
  170. if err != nil {
  171. exitCode, errC := system.GetExitCode(err)
  172. if errC != nil {
  173. return nil, err
  174. }
  175. switch exitCode {
  176. case 1:
  177. return nil, fmt.Errorf("getent reported invalid parameters/database unknown")
  178. case 2:
  179. terms := strings.Split(args, " ")
  180. return nil, fmt.Errorf("getent unable to find entry %q in %s database", terms[1], terms[0])
  181. case 3:
  182. return nil, fmt.Errorf("getent database doesn't support enumeration")
  183. default:
  184. return nil, err
  185. }
  186. }
  187. return bytes.NewReader(out), nil
  188. }