2014-02-19 00:56:11 +00:00
|
|
|
package libcontainer
|
|
|
|
|
2014-02-25 05:52:29 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"github.com/syndtr/gocapability/capability"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-02-25 23:19:13 +00:00
|
|
|
ErrUnkownNamespace = errors.New("Unknown namespace")
|
|
|
|
ErrUnkownCapability = errors.New("Unknown capability")
|
|
|
|
ErrUnsupported = errors.New("Unsupported method")
|
2014-02-25 05:52:29 +00: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 23:19:13 +00:00
|
|
|
namespaceList = Namespaces{}
|
|
|
|
|
2014-02-25 05:52:29 +00:00
|
|
|
capabilityList = Capabilities{
|
|
|
|
{Key: "SETPCAP", Value: capability.CAP_SETPCAP},
|
|
|
|
{Key: "SYS_MODULE", Value: capability.CAP_SYS_MODULE},
|
|
|
|
{Key: "SYS_RAWIO", Value: capability.CAP_SYS_RAWIO},
|
|
|
|
{Key: "SYS_PACCT", Value: capability.CAP_SYS_PACCT},
|
|
|
|
{Key: "SYS_ADMIN", Value: capability.CAP_SYS_ADMIN},
|
|
|
|
{Key: "SYS_NICE", Value: capability.CAP_SYS_NICE},
|
|
|
|
{Key: "SYS_RESOURCE", Value: capability.CAP_SYS_RESOURCE},
|
|
|
|
{Key: "SYS_TIME", Value: capability.CAP_SYS_TIME},
|
|
|
|
{Key: "SYS_TTY_CONFIG", Value: capability.CAP_SYS_TTY_CONFIG},
|
|
|
|
{Key: "MKNOD", Value: capability.CAP_MKNOD},
|
|
|
|
{Key: "AUDIT_WRITE", Value: capability.CAP_AUDIT_WRITE},
|
|
|
|
{Key: "AUDIT_CONTROL", Value: capability.CAP_AUDIT_CONTROL},
|
|
|
|
{Key: "MAC_OVERRIDE", Value: capability.CAP_MAC_OVERRIDE},
|
|
|
|
{Key: "MAC_ADMIN", Value: capability.CAP_MAC_ADMIN},
|
|
|
|
{Key: "NET_ADMIN", Value: capability.CAP_NET_ADMIN},
|
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
)
|
2014-02-20 03:14:31 +00:00
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
type (
|
2014-02-25 05:52:29 +00:00
|
|
|
Namespace struct {
|
|
|
|
Key string
|
|
|
|
Value int
|
|
|
|
File string
|
|
|
|
}
|
|
|
|
Namespaces []*Namespace
|
2014-02-20 06:43:40 +00:00
|
|
|
)
|
2014-02-20 03:14:31 +00:00
|
|
|
|
2014-02-25 23:19:13 +00:00
|
|
|
func (ns *Namespace) String() string {
|
|
|
|
return ns.Key
|
|
|
|
}
|
|
|
|
|
2014-02-25 05:52:29 +00:00
|
|
|
func (ns *Namespace) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(ns.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *Namespace) UnmarshalJSON(src []byte) error {
|
|
|
|
var nsName string
|
|
|
|
if err := json.Unmarshal(src, &nsName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ret := GetNamespace(nsName)
|
|
|
|
if ret == nil {
|
|
|
|
return ErrUnkownNamespace
|
|
|
|
}
|
|
|
|
*ns = *ret
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetNamespace(key string) *Namespace {
|
|
|
|
for _, ns := range namespaceList {
|
|
|
|
if ns.Key == key {
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if os.Getenv("DEBUG") != "" {
|
|
|
|
panic("Unreachable: Namespace not found")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// Contains returns true if the specified Namespace is
|
|
|
|
// in the slice
|
2014-02-25 05:52:29 +00:00
|
|
|
func (n Namespaces) Contains(ns string) bool {
|
|
|
|
return GetNamespace(ns) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
Capability struct {
|
|
|
|
Key string
|
|
|
|
Value capability.Cap
|
|
|
|
}
|
|
|
|
Capabilities []*Capability
|
|
|
|
)
|
|
|
|
|
2014-02-25 23:19:13 +00:00
|
|
|
func (c *Capability) String() string {
|
|
|
|
return c.Key
|
2014-02-25 05:52:29 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 23:19:13 +00:00
|
|
|
func (c *Capability) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(c.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Capability) UnmarshalJSON(src []byte) error {
|
2014-02-25 05:52:29 +00:00
|
|
|
var capName string
|
|
|
|
if err := json.Unmarshal(src, &capName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ret := GetCapability(capName)
|
|
|
|
if ret == nil {
|
2014-02-25 23:19:13 +00:00
|
|
|
return ErrUnkownCapability
|
2014-02-25 05:52:29 +00:00
|
|
|
}
|
2014-02-25 23:19:13 +00:00
|
|
|
*c = *ret
|
2014-02-25 05:52:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCapability(key string) *Capability {
|
|
|
|
for _, capp := range capabilityList {
|
|
|
|
if capp.Key == key {
|
|
|
|
return capp
|
2014-02-20 03:14:31 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 05:52:29 +00:00
|
|
|
if os.Getenv("DEBUG") != "" {
|
2014-02-25 23:19:13 +00:00
|
|
|
panic("Unreachable: Capability not found")
|
2014-02-25 05:52:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
2014-02-20 03:14:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// Contains returns true if the specified Capability is
|
|
|
|
// in the slice
|
2014-02-25 05:52:29 +00:00
|
|
|
func (c Capabilities) Contains(capp string) bool {
|
|
|
|
return GetCapability(capp) != nil
|
2014-02-20 03:14:31 +00:00
|
|
|
}
|