2014-02-18 19:56:11 -05:00
|
|
|
package libcontainer
|
|
|
|
|
2014-02-25 00:52:29 -05:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/syndtr/gocapability/capability"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-02-25 18:19:13 -05:00
|
|
|
ErrUnkownNamespace = errors.New("Unknown namespace")
|
|
|
|
ErrUnkownCapability = errors.New("Unknown capability")
|
|
|
|
ErrUnsupported = errors.New("Unsupported method")
|
2014-02-25 00:52:29 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// namespaceList is used to convert the libcontainer types
|
|
|
|
// into the names of the files located in /proc/<pid>/ns/* for
|
|
|
|
// each namespace
|
|
|
|
var (
|
2014-02-25 18:19:13 -05:00
|
|
|
namespaceList = Namespaces{}
|
|
|
|
|
2014-02-25 00:52:29 -05:00
|
|
|
capabilityList = Capabilities{
|
2014-03-20 20:10:24 -04:00
|
|
|
{Key: "SETPCAP", Value: capability.CAP_SETPCAP, Enabled: false},
|
|
|
|
{Key: "SYS_MODULE", Value: capability.CAP_SYS_MODULE, Enabled: false},
|
|
|
|
{Key: "SYS_RAWIO", Value: capability.CAP_SYS_RAWIO, Enabled: false},
|
|
|
|
{Key: "SYS_PACCT", Value: capability.CAP_SYS_PACCT, Enabled: false},
|
|
|
|
{Key: "SYS_ADMIN", Value: capability.CAP_SYS_ADMIN, Enabled: false},
|
|
|
|
{Key: "SYS_NICE", Value: capability.CAP_SYS_NICE, Enabled: false},
|
|
|
|
{Key: "SYS_RESOURCE", Value: capability.CAP_SYS_RESOURCE, Enabled: false},
|
|
|
|
{Key: "SYS_TIME", Value: capability.CAP_SYS_TIME, Enabled: false},
|
|
|
|
{Key: "SYS_TTY_CONFIG", Value: capability.CAP_SYS_TTY_CONFIG, Enabled: false},
|
|
|
|
{Key: "MKNOD", Value: capability.CAP_MKNOD, Enabled: false},
|
|
|
|
{Key: "AUDIT_WRITE", Value: capability.CAP_AUDIT_WRITE, Enabled: false},
|
|
|
|
{Key: "AUDIT_CONTROL", Value: capability.CAP_AUDIT_CONTROL, Enabled: false},
|
|
|
|
{Key: "MAC_OVERRIDE", Value: capability.CAP_MAC_OVERRIDE, Enabled: false},
|
|
|
|
{Key: "MAC_ADMIN", Value: capability.CAP_MAC_ADMIN, Enabled: false},
|
|
|
|
{Key: "NET_ADMIN", Value: capability.CAP_NET_ADMIN, Enabled: false},
|
2014-02-25 00:52:29 -05:00
|
|
|
}
|
2014-02-18 19:56:11 -05:00
|
|
|
)
|
2014-02-19 22:14:31 -05:00
|
|
|
|
2014-02-20 01:43:40 -05:00
|
|
|
type (
|
2014-02-25 00:52:29 -05:00
|
|
|
Namespace struct {
|
2014-03-20 19:09:01 -04:00
|
|
|
Key string `json:"key,omitempty"`
|
|
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
|
|
Value int `json:"value,omitempty"`
|
|
|
|
File string `json:"file,omitempty"`
|
2014-02-25 00:52:29 -05:00
|
|
|
}
|
|
|
|
Namespaces []*Namespace
|
2014-02-20 01:43:40 -05:00
|
|
|
)
|
2014-02-19 22:14:31 -05:00
|
|
|
|
2014-02-25 18:19:13 -05:00
|
|
|
func (ns *Namespace) String() string {
|
|
|
|
return ns.Key
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:52:29 -05:00
|
|
|
func GetNamespace(key string) *Namespace {
|
|
|
|
for _, ns := range namespaceList {
|
|
|
|
if ns.Key == key {
|
2014-03-20 20:23:34 -04:00
|
|
|
cpy := *ns
|
|
|
|
return &cpy
|
2014-02-25 00:52:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-20 01:43:40 -05:00
|
|
|
// Contains returns true if the specified Namespace is
|
|
|
|
// in the slice
|
2014-02-25 00:52:29 -05:00
|
|
|
func (n Namespaces) Contains(ns string) bool {
|
2014-03-20 20:23:34 -04:00
|
|
|
return n.Get(ns) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n Namespaces) Get(ns string) *Namespace {
|
2014-03-04 11:55:12 -05:00
|
|
|
for _, nsp := range n {
|
|
|
|
if nsp.Key == ns {
|
2014-03-20 20:23:34 -04:00
|
|
|
return nsp
|
2014-03-04 11:55:12 -05:00
|
|
|
}
|
|
|
|
}
|
2014-03-20 20:23:34 -04:00
|
|
|
return nil
|
2014-02-25 00:52:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
Capability struct {
|
2014-03-20 19:09:01 -04:00
|
|
|
Key string `json:"key,omitempty"`
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
Value capability.Cap `json:"value,omitempty"`
|
2014-02-25 00:52:29 -05:00
|
|
|
}
|
|
|
|
Capabilities []*Capability
|
|
|
|
)
|
|
|
|
|
2014-02-25 18:19:13 -05:00
|
|
|
func (c *Capability) String() string {
|
|
|
|
return c.Key
|
2014-02-25 00:52:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetCapability(key string) *Capability {
|
|
|
|
for _, capp := range capabilityList {
|
|
|
|
if capp.Key == key {
|
2014-03-20 20:10:24 -04:00
|
|
|
cpy := *capp
|
|
|
|
return &cpy
|
2014-02-19 22:14:31 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 00:52:29 -05:00
|
|
|
return nil
|
2014-02-19 22:14:31 -05:00
|
|
|
}
|
|
|
|
|
2014-02-20 01:43:40 -05:00
|
|
|
// Contains returns true if the specified Capability is
|
|
|
|
// in the slice
|
2014-02-25 00:52:29 -05:00
|
|
|
func (c Capabilities) Contains(capp string) bool {
|
2014-03-20 20:10:24 -04:00
|
|
|
return c.Get(capp) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Capabilities) Get(capp string) *Capability {
|
2014-03-04 11:55:12 -05:00
|
|
|
for _, cap := range c {
|
|
|
|
if cap.Key == capp {
|
2014-03-20 20:10:24 -04:00
|
|
|
return cap
|
2014-03-04 11:55:12 -05:00
|
|
|
}
|
|
|
|
}
|
2014-03-20 20:10:24 -04:00
|
|
|
return nil
|
2014-02-19 22:14:31 -05:00
|
|
|
}
|