idtools_unix.go 6.2 KB

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