Debian Codegraph @

user/idtools_unix.go

raw symbols SCIP
1 //go:build !windows
2
3 package user
4
5 import (
6 "fmt"
7 "os"
8 "path/filepath"
9 "strconv"
10 "syscall"
11 )
12
13 func mkdirAs(path string, mode os.FileMode, uid, gid int, mkAll, onlyNew bool) error {
14 path, err := filepath.Abs(path)
15 if err != nil {
16 return err
17 }
18
19 stat, err := os.Stat(path)
20 if err == nil {
21 if !stat.IsDir() {
22 return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
23 }
24 if onlyNew {
25 return nil
26 }
27
28 // short-circuit -- we were called with an existing directory and chown was requested
29 return setPermissions(path, mode, uid, gid, stat)
30 }
31
32 // make an array containing the original path asked for, plus (for mkAll == true)
33 // all path components leading up to the complete path that don't exist before we MkdirAll
34 // so that we can chown all of them properly at the end. If onlyNew is true, we won't
35 // chown the full directory path if it exists
36 var paths []string
37 if os.IsNotExist(err) {
38 paths = append(paths, path)
39 }
40
41 if mkAll {
42 // walk back to "/" looking for directories which do not exist
43 // and add them to the paths array for chown after creation
44 dirPath := path
45 for {
46 dirPath = filepath.Dir(dirPath)
47 if dirPath == "/" {
48 break
49 }
50 if _, err = os.Stat(dirPath); os.IsNotExist(err) {
51 paths = append(paths, dirPath)
52 }
53 }
54 if err = os.MkdirAll(path, mode); err != nil {
55 return err
56 }
57 } else if err = os.Mkdir(path, mode); err != nil {
58 return err
59 }
60 // even if it existed, we will chown the requested path + any subpaths that
61 // didn't exist when we called MkdirAll
62 for _, pathComponent := range paths {
63 if err = setPermissions(pathComponent, mode, uid, gid, nil); err != nil {
64 return err
65 }
66 }
67 return nil
68 }
69
70 // setPermissions performs a chown/chmod only if the uid/gid don't match what's requested
71 // Normally a Chown is a no-op if uid/gid match, but in some cases this can still cause an error, e.g. if the
72 // dir is on an NFS share, so don't call chown unless we absolutely must.
73 // Likewise for setting permissions.
74 func setPermissions(p string, mode os.FileMode, uid, gid int, stat os.FileInfo) error {
75 if stat == nil {
76 var err error
77 stat, err = os.Stat(p)
78 if err != nil {
79 return err
80 }
81 }
82 if stat.Mode().Perm() != mode.Perm() {
83 if err := os.Chmod(p, mode.Perm()); err != nil {
84 return err
85 }
86 }
87 ssi := stat.Sys().(*syscall.Stat_t)
88 if ssi.Uid == uint32(uid) && ssi.Gid == uint32(gid) {
89 return nil
90 }
91 return os.Chown(p, uid, gid)
92 }
93
94 // LoadIdentityMapping takes a requested username and
95 // using the data from /etc/sub{uid,gid} ranges, creates the
96 // proper uid and gid remapping ranges for that user/group pair
97 func LoadIdentityMapping(name string) (IdentityMapping, error) {
98 // TODO: Consider adding support for calling out to "getent"
99 usr, err := LookupUser(name)
100 if err != nil {
101 return IdentityMapping{}, fmt.Errorf("could not get user for username %s: %w", name, err)
102 }
103
104 subuidRanges, err := lookupSubRangesFile("/etc/subuid", usr)
105 if err != nil {
106 return IdentityMapping{}, err
107 }
108 subgidRanges, err := lookupSubRangesFile("/etc/subgid", usr)
109 if err != nil {
110 return IdentityMapping{}, err
111 }
112
113 return IdentityMapping{
114 UIDMaps: subuidRanges,
115 GIDMaps: subgidRanges,
116 }, nil
117 }
118
119 func lookupSubRangesFile(path string, usr User) ([]IDMap, error) {
120 uidstr := strconv.Itoa(usr.Uid)
121 rangeList, err := ParseSubIDFileFilter(path, func(sid SubID) bool {
122 return sid.Name == usr.Name || sid.Name == uidstr
123 })
124 if err != nil {
125 return nil, err
126 }
127 if len(rangeList) == 0 {
128 return nil, fmt.Errorf("no subuid ranges found for user %q", usr.Name)
129 }
130
131 idMap := []IDMap{}
132
133 var containerID int64
134 for _, idrange := range rangeList {
135 idMap = append(idMap, IDMap{
136 ID: containerID,
137 ParentID: idrange.SubID,
138 Count: idrange.Count,
139 })
140 containerID = containerID + idrange.Count
141 }
142 return idMap, nil
143 }