2014-02-19 08:56:11 +08:00
|
|
|
package libcontainer
|
|
|
|
|
2014-02-25 13:52:29 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
2014-05-06 03:34:21 +08:00
|
|
|
|
2014-02-25 13:52:29 +08:00
|
|
|
"github.com/syndtr/gocapability/capability"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-02-26 07:19:13 +08:00
|
|
|
ErrUnkownNamespace = errors.New("Unknown namespace")
|
|
|
|
ErrUnkownCapability = errors.New("Unknown capability")
|
|
|
|
ErrUnsupported = errors.New("Unsupported method")
|
2014-02-25 13:52:29 +08:00
|
|
|
)
|
|
|
|
|
2014-04-11 23:26:23 +08:00
|
|
|
type Mounts []Mount
|
|
|
|
|
|
|
|
func (s Mounts) OfType(t string) Mounts {
|
|
|
|
out := Mounts{}
|
|
|
|
for _, m := range s {
|
|
|
|
if m.Type == t {
|
|
|
|
out = append(out, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
type Mount struct {
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Source string `json:"source,omitempty"` // Source path, in the host namespace
|
|
|
|
Destination string `json:"destination,omitempty"` // Destination path, in the container
|
|
|
|
Writable bool `json:"writable,omitempty"`
|
|
|
|
Private bool `json:"private,omitempty"`
|
|
|
|
}
|
|
|
|
|
2014-02-25 13:52:29 +08: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-26 07:19:13 +08:00
|
|
|
namespaceList = Namespaces{}
|
|
|
|
|
2014-02-25 13:52:29 +08:00
|
|
|
capabilityList = Capabilities{
|
2014-05-06 03:34:21 +08:00
|
|
|
{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},
|
|
|
|
{Key: "SYSLOG", Value: capability.CAP_SYSLOG},
|
2014-05-15 02:29:08 +08:00
|
|
|
{Key: "SETUID", Value: capability.CAP_SETUID},
|
|
|
|
{Key: "SETGID", Value: capability.CAP_SETGID},
|
|
|
|
{Key: "CHOWN", Value: capability.CAP_CHOWN},
|
|
|
|
{Key: "NET_RAW", Value: capability.CAP_NET_RAW},
|
|
|
|
{Key: "DAC_OVERRIDE", Value: capability.CAP_DAC_OVERRIDE},
|
2014-05-17 09:16:07 +08:00
|
|
|
{Key: "FOWNER", Value: capability.CAP_FOWNER},
|
2014-05-17 10:03:26 +08:00
|
|
|
{Key: "DAC_READ_SEARCH", Value: capability.CAP_DAC_READ_SEARCH},
|
|
|
|
{Key: "FSETID", Value: capability.CAP_FSETID},
|
|
|
|
{Key: "KILL", Value: capability.CAP_KILL},
|
|
|
|
{Key: "SETGID", Value: capability.CAP_SETGID},
|
|
|
|
{Key: "SETUID", Value: capability.CAP_SETUID},
|
|
|
|
{Key: "LINUX_IMMUTABLE", Value: capability.CAP_LINUX_IMMUTABLE},
|
|
|
|
{Key: "NET_BIND_SERVICE", Value: capability.CAP_NET_BIND_SERVICE},
|
|
|
|
{Key: "NET_BROADCAST", Value: capability.CAP_NET_BROADCAST},
|
|
|
|
{Key: "IPC_LOCK", Value: capability.CAP_IPC_LOCK},
|
|
|
|
{Key: "IPC_OWNER", Value: capability.CAP_IPC_OWNER},
|
|
|
|
{Key: "SYS_CHROOT", Value: capability.CAP_SYS_CHROOT},
|
|
|
|
{Key: "SYS_PTRACE", Value: capability.CAP_SYS_PTRACE},
|
|
|
|
{Key: "SYS_BOOT", Value: capability.CAP_SYS_BOOT},
|
|
|
|
{Key: "LEASE", Value: capability.CAP_LEASE},
|
|
|
|
{Key: "SETFCAP", Value: capability.CAP_SETFCAP},
|
|
|
|
{Key: "WAKE_ALARM", Value: capability.CAP_WAKE_ALARM},
|
|
|
|
{Key: "BLOCK_SUSPEND", Value: capability.CAP_BLOCK_SUSPEND},
|
2014-02-25 13:52:29 +08:00
|
|
|
}
|
2014-02-19 08:56:11 +08:00
|
|
|
)
|
2014-02-20 11:14:31 +08:00
|
|
|
|
2014-02-20 14:43:40 +08:00
|
|
|
type (
|
2014-02-25 13:52:29 +08:00
|
|
|
Namespace struct {
|
2014-05-06 03:34:21 +08:00
|
|
|
Key string `json:"key,omitempty"`
|
|
|
|
Value int `json:"value,omitempty"`
|
|
|
|
File string `json:"file,omitempty"`
|
2014-02-25 13:52:29 +08:00
|
|
|
}
|
|
|
|
Namespaces []*Namespace
|
2014-02-20 14:43:40 +08:00
|
|
|
)
|
2014-02-20 11:14:31 +08:00
|
|
|
|
2014-02-26 07:19:13 +08:00
|
|
|
func (ns *Namespace) String() string {
|
|
|
|
return ns.Key
|
|
|
|
}
|
|
|
|
|
2014-02-25 13:52:29 +08:00
|
|
|
func GetNamespace(key string) *Namespace {
|
|
|
|
for _, ns := range namespaceList {
|
|
|
|
if ns.Key == key {
|
2014-03-21 08:23:34 +08:00
|
|
|
cpy := *ns
|
|
|
|
return &cpy
|
2014-02-25 13:52:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:43:40 +08:00
|
|
|
// Contains returns true if the specified Namespace is
|
|
|
|
// in the slice
|
2014-02-25 13:52:29 +08:00
|
|
|
func (n Namespaces) Contains(ns string) bool {
|
2014-03-21 08:23:34 +08:00
|
|
|
return n.Get(ns) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n Namespaces) Get(ns string) *Namespace {
|
2014-03-05 00:55:12 +08:00
|
|
|
for _, nsp := range n {
|
2014-04-10 21:07:29 +08:00
|
|
|
if nsp != nil && nsp.Key == ns {
|
2014-03-21 08:23:34 +08:00
|
|
|
return nsp
|
2014-03-05 00:55:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-21 08:23:34 +08:00
|
|
|
return nil
|
2014-02-25 13:52:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
Capability struct {
|
2014-05-06 03:34:21 +08:00
|
|
|
Key string `json:"key,omitempty"`
|
|
|
|
Value capability.Cap `json:"value,omitempty"`
|
2014-02-25 13:52:29 +08:00
|
|
|
}
|
|
|
|
Capabilities []*Capability
|
|
|
|
)
|
|
|
|
|
2014-02-26 07:19:13 +08:00
|
|
|
func (c *Capability) String() string {
|
|
|
|
return c.Key
|
2014-02-25 13:52:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetCapability(key string) *Capability {
|
|
|
|
for _, capp := range capabilityList {
|
|
|
|
if capp.Key == key {
|
2014-03-21 08:10:24 +08:00
|
|
|
cpy := *capp
|
|
|
|
return &cpy
|
2014-02-20 11:14:31 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 13:52:29 +08:00
|
|
|
return nil
|
2014-02-20 11:14:31 +08:00
|
|
|
}
|
|
|
|
|
2014-05-17 08:44:10 +08:00
|
|
|
func GetAllCapabilities() []string {
|
|
|
|
output := make([]string, len(capabilityList))
|
|
|
|
for i, capability := range capabilityList {
|
|
|
|
output[i] = capability.String()
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:43:40 +08:00
|
|
|
// Contains returns true if the specified Capability is
|
|
|
|
// in the slice
|
2014-02-25 13:52:29 +08:00
|
|
|
func (c Capabilities) Contains(capp string) bool {
|
2014-03-21 08:10:24 +08:00
|
|
|
return c.Get(capp) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Capabilities) Get(capp string) *Capability {
|
2014-03-05 00:55:12 +08:00
|
|
|
for _, cap := range c {
|
|
|
|
if cap.Key == capp {
|
2014-03-21 08:10:24 +08:00
|
|
|
return cap
|
2014-03-05 00:55:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-21 08:10:24 +08:00
|
|
|
return nil
|
2014-02-20 11:14:31 +08:00
|
|
|
}
|