2014-03-18 20:49:16 +00:00
|
|
|
// +build selinux,linux
|
|
|
|
|
|
|
|
package label
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/pkg/selinux"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GenLabels(options string) (string, string, error) {
|
|
|
|
processLabel, mountLabel := selinux.GetLxcContexts()
|
|
|
|
if processLabel == "" { // SELinux is disabled
|
2014-04-02 16:52:49 +00:00
|
|
|
return "", "", nil
|
2014-03-18 20:49:16 +00:00
|
|
|
}
|
2014-04-02 16:52:49 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
s = strings.Fields(options)
|
|
|
|
l = len(s)
|
|
|
|
)
|
2014-03-18 20:49:16 +00:00
|
|
|
if l > 0 {
|
|
|
|
pcon := selinux.NewContext(processLabel)
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
o := strings.Split(s[i], "=")
|
|
|
|
pcon[o[0]] = o[1]
|
|
|
|
}
|
|
|
|
processLabel = pcon.Get()
|
|
|
|
mountLabel, err = selinux.CopyLevel(processLabel, mountLabel)
|
|
|
|
}
|
|
|
|
return processLabel, mountLabel, err
|
|
|
|
}
|
|
|
|
|
2014-04-02 16:52:49 +00:00
|
|
|
func FormatMountLabel(src string, mountLabel string) string {
|
|
|
|
if mountLabel != "" {
|
|
|
|
switch src {
|
|
|
|
case "":
|
|
|
|
src = fmt.Sprintf("%s,context=%s", src, mountLabel)
|
|
|
|
default:
|
|
|
|
src = fmt.Sprintf("context=%s", mountLabel)
|
2014-03-18 20:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-02 16:52:49 +00:00
|
|
|
return src
|
2014-03-18 20:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetProcessLabel(processLabel string) error {
|
|
|
|
if selinux.SelinuxEnabled() {
|
|
|
|
return selinux.Setexeccon(processLabel)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetProcessLabel() (string, error) {
|
|
|
|
if selinux.SelinuxEnabled() {
|
|
|
|
return selinux.Getexeccon()
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetFileLabel(path string, fileLabel string) error {
|
|
|
|
if selinux.SelinuxEnabled() && fileLabel != "" {
|
|
|
|
return selinux.Setfilecon(path, fileLabel)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPidCon(pid int) (string, error) {
|
|
|
|
return selinux.Getpidcon(pid)
|
|
|
|
}
|
2014-04-01 14:03:29 +00:00
|
|
|
|
|
|
|
func Init() {
|
|
|
|
selinux.SelinuxEnabled()
|
|
|
|
}
|