Remove CGO dependency for user lookup in allocdir
os/user's user.Lookup requires that the artifact be compiled with CGO support enabled. This change instead reads /etc/passwd directly. The code was acquired from docker/docker#1096
This commit is contained in:
parent
677b18ca52
commit
ffe67d8910
|
@ -5,9 +5,11 @@ package allocdir
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,7 +28,7 @@ func (d *AllocDir) dropDirPermissions(path string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
u, err := user.Lookup("nobody")
|
u, err := userLookup("nobody")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -69,3 +71,28 @@ func getGid(u *user.User) (int, error) {
|
||||||
|
|
||||||
return gid, nil
|
return gid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// userLookup checks if the given username or uid is present in /etc/passwd
|
||||||
|
// and returns the user struct.
|
||||||
|
// If the username is not found, an error is returned.
|
||||||
|
// Credit to @creak, https://github.com/docker/docker/pull/1096
|
||||||
|
func userLookup(uid string) (*user.User, error) {
|
||||||
|
file, err := ioutil.ReadFile("/etc/passwd")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, line := range strings.Split(string(file), "\n") {
|
||||||
|
data := strings.Split(line, ":")
|
||||||
|
if len(data) > 5 && (data[0] == uid || data[2] == uid) {
|
||||||
|
return &user.User{
|
||||||
|
Uid: data[2],
|
||||||
|
Gid: data[3],
|
||||||
|
Username: data[0],
|
||||||
|
Name: data[4],
|
||||||
|
HomeDir: data[5],
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("User not found in /etc/passwd")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue