summaryrefslogtreecommitdiff
path: root/cmd/podman-mac-helper/main.go
blob: 8d995519faf04a380089916f535b1e18ca23d6ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//go:build darwin
// +build darwin

package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"regexp"
	"strconv"
	"strings"

	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

const (
	defaultPrefix = "/usr/local"
	dockerSock    = "/var/run/docker.sock"
)

var installPrefix string

var rootCmd = &cobra.Command{
	Use:               "podman-mac-helper",
	Short:             "A system helper to manage docker.sock",
	Long:              `podman-mac-helper is a system helper service and tool for managing docker.sock `,
	CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
	SilenceErrors:     true,
}

// Note, this code is security sensitive since it runs under privilege.
// Limit actions to what is strictly necessary, and take appropriate
// safeguards
//
// After installation the service call is ran under launchd in a nowait
// inetd style fashion, so stdin, stdout, and stderr are all pointing to
// an accepted connection
//
// This service is installed once per user and will redirect
// /var/run/docker to the fixed user-assigned unix socket location.
//
// Control communication is restricted to each user specific service via
// unix file permissions

func main() {
	if os.Geteuid() != 0 {
		fmt.Printf("This command must be ran as root via sudo or osascript\n")
		os.Exit(1)
	}

	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
	}
}

func getUserInfo(name string) (string, string, string, error) {
	// We exec id instead of using user.Lookup to remain compat
	// with CGO disabled.
	cmd := exec.Command("/usr/bin/id", "-P", name)
	output, err := cmd.StdoutPipe()
	if err != nil {
		return "", "", "", err
	}

	if err := cmd.Start(); err != nil {
		return "", "", "", err
	}

	entry := readCapped(output)
	elements := strings.Split(entry, ":")
	if len(elements) < 9 || elements[0] != name {
		return "", "", "", errors.New("Could not lookup user")
	}

	return elements[0], elements[2], elements[8], nil
}

func getUser() (string, string, string, error) {
	name, found := os.LookupEnv("SUDO_USER")
	if !found {
		name, found = os.LookupEnv("USER")
		if !found {
			return "", "", "", errors.New("could not determine user")
		}
	}

	_, uid, home, err := getUserInfo(name)
	if err != nil {
		return "", "", "", fmt.Errorf("could not lookup user: %s", name)
	}
	id, err := strconv.Atoi(uid)
	if err != nil {
		return "", "", "", fmt.Errorf("invalid uid for user: %s", name)
	}
	if id == 0 {
		return "", "", "", fmt.Errorf("unexpected root user")
	}

	return name, uid, home, nil
}

// Used for commands that don't return a proper exit code
func runDetectErr(name string, args ...string) error {
	cmd := exec.Command(name, args...)
	errReader, err := cmd.StderrPipe()
	if err != nil {
		return err
	}

	err = cmd.Start()
	if err == nil {
		errString := readCapped(errReader)
		if len(errString) > 0 {
			re := regexp.MustCompile(`\r?\n`)
			err = errors.New(re.ReplaceAllString(errString, ": "))
		}
	}

	if werr := cmd.Wait(); werr != nil {
		err = werr
	}

	return err
}

func readCapped(reader io.Reader) string {
	// Cap output
	buffer := make([]byte, 2048)
	n, _ := io.ReadFull(reader, buffer)
	_, _ = io.Copy(ioutil.Discard, reader)
	if n > 0 {
		return string(buffer[:n])
	}

	return ""
}

func addPrefixFlag(cmd *cobra.Command) {
	cmd.Flags().StringVar(&installPrefix, "prefix", defaultPrefix, "Sets the install location prefix")
}

func silentUsage(cmd *cobra.Command, args []string) {
	cmd.SilenceUsage = true
	cmd.SilenceErrors = true
}