-
Notifications
You must be signed in to change notification settings - Fork 596
/
sudoers.go
98 lines (92 loc) · 2.49 KB
/
sudoers.go
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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/lima-vm/lima/pkg/networks"
"github.com/spf13/cobra"
)
func newSudoersCommand() *cobra.Command {
networksMD := "$PREFIX/share/doc/lima/docs/network.md"
if exe, err := os.Executable(); err == nil {
binDir := filepath.Dir(exe)
prefixDir := filepath.Dir(binDir)
networksMD = filepath.Join(prefixDir, "share/doc/lima/docs/network.md")
}
sudoersCommand := &cobra.Command{
Use: "sudoers [--check [SUDOERSFILE-TO-CHECK]]",
Example: `
To generate the /etc/sudoers.d/lima file:
$ limactl sudoers | sudo tee /etc/sudoers.d/lima
To validate the existing /etc/sudoers.d/lima file:
$ limactl sudoers --check /etc/sudoers.d/lima
`,
Short: "Generate the content of the /etc/sudoers.d/lima file",
Long: fmt.Sprintf(`Generate the content of the /etc/sudoers.d/lima file for enabling vmnet.framework support.
The content is written to stdout, NOT to the file.
This command must not run as the root.
See %s for the usage.`, networksMD),
Args: WrapArgsError(cobra.MaximumNArgs(1)),
RunE: sudoersAction,
GroupID: advancedCommand,
}
configFile, _ := networks.ConfigFile()
sudoersCommand.Flags().Bool("check", false,
fmt.Sprintf("check that the sudoers file is up-to-date with %q", configFile))
return sudoersCommand
}
func sudoersAction(cmd *cobra.Command, args []string) error {
if runtime.GOOS != "darwin" {
return errors.New("sudoers command is only supported on macOS right now")
}
check, err := cmd.Flags().GetBool("check")
if err != nil {
return err
}
if check {
return verifySudoAccess(args)
}
switch len(args) {
case 0:
// NOP
case 1:
return fmt.Errorf("the file argument can be specified only for --check mode")
default:
return fmt.Errorf("unexpected arguments %v", args)
}
sudoers, err := networks.Sudoers()
if err != nil {
return err
}
fmt.Print(sudoers)
return nil
}
func verifySudoAccess(args []string) error {
config, err := networks.Config()
if err != nil {
return err
}
if err := config.Validate(); err != nil {
return err
}
var file string
switch len(args) {
case 0:
file = config.Paths.Sudoers
if file == "" {
configFile, _ := networks.ConfigFile()
return fmt.Errorf("no sudoers file defined in %q", configFile)
}
case 1:
file = args[0]
default:
return errors.New("can check only a single sudoers file")
}
if err := config.VerifySudoAccess(file); err != nil {
return err
}
fmt.Printf("%q is up-to-date (or sudo doesn't require a password)\n", file)
return nil
}