|
| 1 | +package user |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | +) |
| 7 | + |
| 8 | +// MkdirOpt is a type for options to pass to Mkdir calls |
| 9 | +type MkdirOpt func(*mkdirOptions) |
| 10 | + |
| 11 | +type mkdirOptions struct { |
| 12 | + onlyNew bool |
| 13 | +} |
| 14 | + |
| 15 | +// WithOnlyNew is an option for MkdirAllAndChown that will only change ownership and permissions |
| 16 | +// on newly created directories. If the directory already exists, it will not be modified |
| 17 | +func WithOnlyNew(o *mkdirOptions) { |
| 18 | + o.onlyNew = true |
| 19 | +} |
| 20 | + |
| 21 | +// MkdirAllAndChown creates a directory (include any along the path) and then modifies |
| 22 | +// ownership to the requested uid/gid. By default, if the directory already exists, this |
| 23 | +// function will still change ownership and permissions. If WithOnlyNew is passed as an |
| 24 | +// option, then only the newly created directories will have ownership and permissions changed. |
| 25 | +func MkdirAllAndChown(path string, mode os.FileMode, uid, gid int, opts ...MkdirOpt) error { |
| 26 | + var options mkdirOptions |
| 27 | + for _, opt := range opts { |
| 28 | + opt(&options) |
| 29 | + } |
| 30 | + |
| 31 | + return mkdirAs(path, mode, uid, gid, true, options.onlyNew) |
| 32 | +} |
| 33 | + |
| 34 | +// MkdirAndChown creates a directory and then modifies ownership to the requested uid/gid. |
| 35 | +// By default, if the directory already exists, this function still changes ownership and permissions. |
| 36 | +// If WithOnlyNew is passed as an option, then only the newly created directory will have ownership |
| 37 | +// and permissions changed. |
| 38 | +// Note that unlike os.Mkdir(), this function does not return IsExist error |
| 39 | +// in case path already exists. |
| 40 | +func MkdirAndChown(path string, mode os.FileMode, uid, gid int, opts ...MkdirOpt) error { |
| 41 | + var options mkdirOptions |
| 42 | + for _, opt := range opts { |
| 43 | + opt(&options) |
| 44 | + } |
| 45 | + return mkdirAs(path, mode, uid, gid, false, options.onlyNew) |
| 46 | +} |
| 47 | + |
| 48 | +// getRootUIDGID retrieves the remapped root uid/gid pair from the set of maps. |
| 49 | +// If the maps are empty, then the root uid/gid will default to "real" 0/0 |
| 50 | +func getRootUIDGID(uidMap, gidMap []IDMap) (int, int, error) { |
| 51 | + uid, err := toHost(0, uidMap) |
| 52 | + if err != nil { |
| 53 | + return -1, -1, err |
| 54 | + } |
| 55 | + gid, err := toHost(0, gidMap) |
| 56 | + if err != nil { |
| 57 | + return -1, -1, err |
| 58 | + } |
| 59 | + return uid, gid, nil |
| 60 | +} |
| 61 | + |
| 62 | +// toContainer takes an id mapping, and uses it to translate a |
| 63 | +// host ID to the remapped ID. If no map is provided, then the translation |
| 64 | +// assumes a 1-to-1 mapping and returns the passed in id |
| 65 | +func toContainer(hostID int, idMap []IDMap) (int, error) { |
| 66 | + if idMap == nil { |
| 67 | + return hostID, nil |
| 68 | + } |
| 69 | + for _, m := range idMap { |
| 70 | + if (int64(hostID) >= m.ParentID) && (int64(hostID) <= (m.ParentID + m.Count - 1)) { |
| 71 | + contID := int(m.ID + (int64(hostID) - m.ParentID)) |
| 72 | + return contID, nil |
| 73 | + } |
| 74 | + } |
| 75 | + return -1, fmt.Errorf("host ID %d cannot be mapped to a container ID", hostID) |
| 76 | +} |
| 77 | + |
| 78 | +// toHost takes an id mapping and a remapped ID, and translates the |
| 79 | +// ID to the mapped host ID. If no map is provided, then the translation |
| 80 | +// assumes a 1-to-1 mapping and returns the passed in id # |
| 81 | +func toHost(contID int, idMap []IDMap) (int, error) { |
| 82 | + if idMap == nil { |
| 83 | + return contID, nil |
| 84 | + } |
| 85 | + for _, m := range idMap { |
| 86 | + if (int64(contID) >= m.ID) && (int64(contID) <= (m.ID + m.Count - 1)) { |
| 87 | + hostID := int(m.ParentID + (int64(contID) - m.ID)) |
| 88 | + return hostID, nil |
| 89 | + } |
| 90 | + } |
| 91 | + return -1, fmt.Errorf("container ID %d cannot be mapped to a host ID", contID) |
| 92 | +} |
| 93 | + |
| 94 | +// IdentityMapping contains a mappings of UIDs and GIDs. |
| 95 | +// The zero value represents an empty mapping. |
| 96 | +type IdentityMapping struct { |
| 97 | + UIDMaps []IDMap `json:"UIDMaps"` |
| 98 | + GIDMaps []IDMap `json:"GIDMaps"` |
| 99 | +} |
| 100 | + |
| 101 | +// RootPair returns a uid and gid pair for the root user. The error is ignored |
| 102 | +// because a root user always exists, and the defaults are correct when the uid |
| 103 | +// and gid maps are empty. |
| 104 | +func (i IdentityMapping) RootPair() (int, int) { |
| 105 | + uid, gid, _ := getRootUIDGID(i.UIDMaps, i.GIDMaps) |
| 106 | + return uid, gid |
| 107 | +} |
| 108 | + |
| 109 | +// ToHost returns the host UID and GID for the container uid, gid. |
| 110 | +// Remapping is only performed if the ids aren't already the remapped root ids |
| 111 | +func (i IdentityMapping) ToHost(uid, gid int) (int, int, error) { |
| 112 | + var err error |
| 113 | + ruid, rgid := i.RootPair() |
| 114 | + |
| 115 | + if uid != ruid { |
| 116 | + ruid, err = toHost(uid, i.UIDMaps) |
| 117 | + if err != nil { |
| 118 | + return ruid, rgid, err |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if gid != rgid { |
| 123 | + rgid, err = toHost(gid, i.GIDMaps) |
| 124 | + } |
| 125 | + return ruid, rgid, err |
| 126 | +} |
| 127 | + |
| 128 | +// ToContainer returns the container UID and GID for the host uid and gid |
| 129 | +func (i IdentityMapping) ToContainer(uid, gid int) (int, int, error) { |
| 130 | + ruid, err := toContainer(uid, i.UIDMaps) |
| 131 | + if err != nil { |
| 132 | + return -1, -1, err |
| 133 | + } |
| 134 | + rgid, err := toContainer(gid, i.GIDMaps) |
| 135 | + return ruid, rgid, err |
| 136 | +} |
| 137 | + |
| 138 | +// Empty returns true if there are no id mappings |
| 139 | +func (i IdentityMapping) Empty() bool { |
| 140 | + return len(i.UIDMaps) == 0 && len(i.GIDMaps) == 0 |
| 141 | +} |
0 commit comments