idtools_unix.go 6.2 KB

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