From 53583c7e676f3ff0c1a42f624ad2d68233099172 Mon Sep 17 00:00:00 2001 From: RickyRajinder Date: Wed, 1 Jan 2020 23:19:59 -0800 Subject: [PATCH] Add check if mysqlctl EUID is 0 Signed-off-by: RickyRajinder --- go/cmd/mysqlctl/mysqlctl.go | 5 +++++ go/cmd/tools.go | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/go/cmd/mysqlctl/mysqlctl.go b/go/cmd/mysqlctl/mysqlctl.go index 4e8b0634d29..8793d8420a6 100644 --- a/go/cmd/mysqlctl/mysqlctl.go +++ b/go/cmd/mysqlctl/mysqlctl.go @@ -25,6 +25,7 @@ import ( "golang.org/x/net/context" + "vitess.io/vitess/go/cmd" "vitess.io/vitess/go/exit" "vitess.io/vitess/go/flagutil" "vitess.io/vitess/go/mysql" @@ -235,6 +236,10 @@ func main() { fmt.Fprintf(os.Stderr, "\n") } + if cmd.IsRunningAsRoot() { + fmt.Fprintln(os.Stderr, "mysqlctl cannot be ran as root. Please run as a different user") + exit.Return(1) + } dbconfigs.RegisterFlags(dbconfigs.Dba) flag.Parse() diff --git a/go/cmd/tools.go b/go/cmd/tools.go index 9722dbc267c..75234b4890d 100644 --- a/go/cmd/tools.go +++ b/go/cmd/tools.go @@ -20,6 +20,7 @@ import ( "os" "os/exec" "strings" + "syscall" ) // DetachFromTerminalAndExit allows a command line program to detach from the terminal and continue running @@ -41,3 +42,8 @@ func DetachFromTerminalAndExit() { fmt.Println("[PID]", cmd.Process.Pid) os.Exit(0) } + +// IsRunningAsRoot checks if a component is being ran as root +func IsRunningAsRoot() bool { + return syscall.Geteuid() == 0 +}