-
Notifications
You must be signed in to change notification settings - Fork 0
/
jailer.go
59 lines (50 loc) · 1.19 KB
/
jailer.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
58
59
package jailer
import (
"fmt"
"os"
"os/exec"
"os/user"
"path"
"strconv"
"syscall"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const BaseJailPath = "/opt/jail"
// CreateJail sets up the named directory for use with chroot
func CreateJail(name string) error {
logrus.Debugf("Creating jail for %v", name)
_, err := os.Stat(path.Join(BaseJailPath, name))
if err == nil {
return nil
}
cmd := exec.Command("/usr/bin/jailer.sh", name)
out, err := cmd.CombinedOutput()
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("error running the jail command: %v", string(out)))
}
logrus.Debugf("Output from create jail command %v", string(out))
return nil
}
// GetUserCred looks up the user and provides it in syscall.Credential
func GetUserCred() (*syscall.Credential, error) {
u, err := user.Current()
if err != nil {
uID := os.Getuid()
u, err = user.LookupId(strconv.Itoa(uID))
if err != nil {
return nil, err
}
}
i, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
return nil, err
}
uid := uint32(i)
i, err = strconv.ParseUint(u.Gid, 10, 32)
if err != nil {
return nil, err
}
gid := uint32(i)
return &syscall.Credential{Uid: uid, Gid: gid}, nil
}