1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/sysinit/sysinit.go

207 lines
4.3 KiB
Go
Raw Normal View History

package sysinit
import (
"encoding/json"
"flag"
"fmt"
"github.com/dotcloud/docker/netlink"
"github.com/dotcloud/docker/utils"
"github.com/syndtr/gocapability/capability"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
2013-12-02 13:58:45 -05:00
type DockerInitArgs struct {
user string
gateway string
workDir string
privileged bool
env []string
args []string
2013-12-02 13:58:45 -05:00
}
// Setup networking
2013-12-02 17:21:29 -05:00
func setupNetworking(args *DockerInitArgs) error {
2013-12-02 13:58:45 -05:00
if args.gateway == "" {
2013-12-02 17:21:29 -05:00
return nil
}
2013-12-02 13:58:45 -05:00
ip := net.ParseIP(args.gateway)
if ip == nil {
2013-12-02 17:21:29 -05:00
return fmt.Errorf("Unable to set up networking, %s is not a valid IP", args.gateway)
}
if err := netlink.AddDefaultGw(ip); err != nil {
2013-12-02 17:21:29 -05:00
return fmt.Errorf("Unable to set up networking: %v", err)
}
2013-12-02 17:21:29 -05:00
return nil
}
// Setup working directory
2013-12-02 17:21:29 -05:00
func setupWorkingDirectory(args *DockerInitArgs) error {
2013-12-02 13:58:45 -05:00
if args.workDir == "" {
2013-12-02 17:21:29 -05:00
return nil
}
2013-12-02 13:58:45 -05:00
if err := syscall.Chdir(args.workDir); err != nil {
2013-12-02 17:21:29 -05:00
return fmt.Errorf("Unable to change dir to %v: %v", args.workDir, err)
}
2013-12-02 17:21:29 -05:00
return nil
}
// Takes care of dropping privileges to the desired user
2013-12-02 17:21:29 -05:00
func changeUser(args *DockerInitArgs) error {
2013-12-02 13:58:45 -05:00
if args.user == "" {
2013-12-02 17:21:29 -05:00
return nil
}
2013-12-02 13:58:45 -05:00
userent, err := utils.UserLookup(args.user)
if err != nil {
2013-12-02 17:21:29 -05:00
return fmt.Errorf("Unable to find user %v: %v", args.user, err)
}
uid, err := strconv.Atoi(userent.Uid)
if err != nil {
2013-12-02 17:21:29 -05:00
return fmt.Errorf("Invalid uid: %v", userent.Uid)
}
gid, err := strconv.Atoi(userent.Gid)
if err != nil {
2013-12-02 17:21:29 -05:00
return fmt.Errorf("Invalid gid: %v", userent.Gid)
}
if err := syscall.Setgid(gid); err != nil {
2013-12-02 17:21:29 -05:00
return fmt.Errorf("setgid failed: %v", err)
}
if err := syscall.Setuid(uid); err != nil {
2013-12-02 17:21:29 -05:00
return fmt.Errorf("setuid failed: %v", err)
}
2013-12-02 17:21:29 -05:00
return nil
}
func setupCapabilities(args *DockerInitArgs) error {
if args.privileged {
return nil
}
drop := []capability.Cap{
capability.CAP_SETPCAP,
capability.CAP_SYS_MODULE,
capability.CAP_SYS_RAWIO,
capability.CAP_SYS_PACCT,
capability.CAP_SYS_ADMIN,
capability.CAP_SYS_NICE,
capability.CAP_SYS_RESOURCE,
capability.CAP_SYS_TIME,
capability.CAP_SYS_TTY_CONFIG,
capability.CAP_MKNOD,
capability.CAP_AUDIT_WRITE,
capability.CAP_AUDIT_CONTROL,
capability.CAP_MAC_OVERRIDE,
capability.CAP_MAC_ADMIN,
}
c, err := capability.NewPid(os.Getpid())
if err != nil {
return err
}
c.Unset(capability.CAPS|capability.BOUNDS, drop...)
if err := c.Apply(capability.CAPS | capability.BOUNDS); err != nil {
return err
}
return nil
}
// Clear environment pollution introduced by lxc-start
2013-12-02 13:58:45 -05:00
func setupEnv(args *DockerInitArgs) {
os.Clearenv()
2013-12-02 13:58:45 -05:00
for _, kv := range args.env {
parts := strings.SplitN(kv, "=", 2)
if len(parts) == 1 {
parts = append(parts, "")
}
os.Setenv(parts[0], parts[1])
}
}
2013-12-02 17:21:29 -05:00
func executeProgram(args *DockerInitArgs) error {
setupEnv(args)
if err := setupNetworking(args); err != nil {
return err
}
if err := setupCapabilities(args); err != nil {
return err
}
2013-12-02 17:21:29 -05:00
if err := setupWorkingDirectory(args); err != nil {
return err
}
if err := changeUser(args); err != nil {
return err
}
2013-12-02 13:58:45 -05:00
path, err := exec.LookPath(args.args[0])
if err != nil {
2013-12-02 13:58:45 -05:00
log.Printf("Unable to locate %v", args.args[0])
os.Exit(127)
}
2013-12-02 13:58:45 -05:00
if err := syscall.Exec(path, args.args, os.Environ()); err != nil {
panic(err)
}
2013-12-02 17:21:29 -05:00
// Will never reach here
return nil
}
// Sys Init code
// This code is run INSIDE the container and is responsible for setting
// up the environment before running the actual process
func SysInit() {
if len(os.Args) <= 1 {
fmt.Println("You should not invoke dockerinit manually")
os.Exit(1)
}
2013-12-02 13:58:45 -05:00
// Get cmdline arguments
user := flag.String("u", "", "username or uid")
gateway := flag.String("g", "", "gateway address")
workDir := flag.String("w", "", "workdir")
privileged := flag.Bool("privileged", false, "privileged mode")
flag.Parse()
2013-12-02 13:58:45 -05:00
// Get env
var env []string
content, err := ioutil.ReadFile("/.dockerenv")
if err != nil {
log.Fatalf("Unable to load environment variables: %v", err)
}
if err := json.Unmarshal(content, &env); err != nil {
log.Fatalf("Unable to unmarshal environment variables: %v", err)
}
args := &DockerInitArgs{
user: *user,
gateway: *gateway,
workDir: *workDir,
privileged: *privileged,
env: env,
args: flag.Args(),
2013-12-02 13:58:45 -05:00
}
2013-12-02 17:21:29 -05:00
if err := executeProgram(args); err != nil {
log.Fatal(err)
}
}