...
1 package configs
2
3 import (
4 "errors"
5 "fmt"
6 "math"
7 )
8
9 var (
10 errNoUIDMap = errors.New("User namespaces enabled, but no uid mappings found.")
11 errNoUserMap = errors.New("User namespaces enabled, but no user mapping found.")
12 errNoGIDMap = errors.New("User namespaces enabled, but no gid mappings found.")
13 errNoGroupMap = errors.New("User namespaces enabled, but no group mapping found.")
14 )
15
16
17
18 func (c Config) HostUID(containerId int) (int, error) {
19 if c.Namespaces.Contains(NEWUSER) {
20 if c.UidMappings == nil {
21 return -1, errNoUIDMap
22 }
23 id, found := c.hostIDFromMapping(int64(containerId), c.UidMappings)
24 if !found {
25 return -1, errNoUserMap
26 }
27
28
29
30
31 if id > math.MaxInt {
32 return -1, fmt.Errorf("mapping for uid %d (host id %d) is larger than native integer size (%d)", containerId, id, math.MaxInt)
33 }
34 return int(id), nil
35 }
36
37 return containerId, nil
38 }
39
40
41
42 func (c Config) HostRootUID() (int, error) {
43 return c.HostUID(0)
44 }
45
46
47
48 func (c Config) HostGID(containerId int) (int, error) {
49 if c.Namespaces.Contains(NEWUSER) {
50 if c.GidMappings == nil {
51 return -1, errNoGIDMap
52 }
53 id, found := c.hostIDFromMapping(int64(containerId), c.GidMappings)
54 if !found {
55 return -1, errNoGroupMap
56 }
57
58
59
60
61 if id > math.MaxInt {
62 return -1, fmt.Errorf("mapping for gid %d (host id %d) is larger than native integer size (%d)", containerId, id, math.MaxInt)
63 }
64 return int(id), nil
65 }
66
67 return containerId, nil
68 }
69
70
71
72 func (c Config) HostRootGID() (int, error) {
73 return c.HostGID(0)
74 }
75
76
77
78 func (c Config) hostIDFromMapping(containerID int64, uMap []IDMap) (int64, bool) {
79 for _, m := range uMap {
80 if (containerID >= m.ContainerID) && (containerID <= (m.ContainerID + m.Size - 1)) {
81 hostID := m.HostID + (containerID - m.ContainerID)
82 return hostID, true
83 }
84 }
85 return -1, false
86 }
87
View as plain text