forked from sylabs/singularity
/
passwd.go
57 lines (46 loc) · 1.71 KB
/
passwd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package files
import (
"fmt"
"io/ioutil"
"os"
"github.com/sylabs/singularity/internal/pkg/sylog"
"github.com/sylabs/singularity/internal/pkg/util/fs"
"github.com/sylabs/singularity/internal/pkg/util/user"
)
// Passwd creates a passwd template based on content of file provided in path,
// updates content with current user information and returns content
func Passwd(path string, home string, uid int) (content []byte, err error) {
sylog.Verbosef("Checking for template passwd file: %s\n", path)
if fs.IsFile(path) == false {
return content, fmt.Errorf("passwd file doesn't exist in container, not updating")
}
sylog.Verbosef("Creating passwd content\n")
passwdFile, err := os.Open(path)
if err != nil {
return content, fmt.Errorf("failed to open passwd file in container: %s", err)
}
defer passwdFile.Close()
content, err = ioutil.ReadAll(passwdFile)
if err != nil {
return content, fmt.Errorf("failed to read passwd file content in container: %s", err)
}
pwInfo, err := user.GetPwUID(uint32(uid))
if err != nil {
return content, err
}
homeDir := pwInfo.Dir
if home != "" {
homeDir = home
}
userInfo := fmt.Sprintf("%s:x:%d:%d:%s:%s:%s\n", pwInfo.Name, pwInfo.UID, pwInfo.GID, pwInfo.Gecos, homeDir, pwInfo.Shell)
if content[len(content)-1] != '\n' {
content = append(content, byte('\n'))
}
sylog.Verbosef("Creating template passwd file and appending user data: %s\n", path)
content = append(content, []byte(userInfo)...)
return content, nil
}