2017-06-20 11:48:14 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-07-16 01:50:01 +00:00
|
|
|
"path/filepath"
|
2017-06-20 11:48:14 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/rancher/os/config"
|
|
|
|
"github.com/rancher/os/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AutologinMain() {
|
|
|
|
log.InitLogger()
|
|
|
|
app := cli.NewApp()
|
|
|
|
|
|
|
|
app.Name = os.Args[0]
|
|
|
|
app.Usage = "autologin console"
|
|
|
|
app.Version = config.Version
|
|
|
|
app.Author = "Rancher Labs, Inc."
|
|
|
|
app.Email = "sven@rancher.com"
|
|
|
|
app.EnableBashCompletion = true
|
|
|
|
app.Action = autologinAction
|
|
|
|
app.HideHelp = true
|
|
|
|
app.Run(os.Args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func autologinAction(c *cli.Context) error {
|
|
|
|
cmd := exec.Command("/bin/stty", "sane")
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
usertty := ""
|
|
|
|
user := "root"
|
|
|
|
tty := ""
|
|
|
|
if c.NArg() > 0 {
|
|
|
|
usertty = c.Args().Get(0)
|
|
|
|
s := strings.SplitN(usertty, ":", 2)
|
|
|
|
user = s[0]
|
|
|
|
if len(s) > 1 {
|
|
|
|
tty = s[1]
|
|
|
|
}
|
|
|
|
}
|
2017-07-16 01:50:01 +00:00
|
|
|
|
|
|
|
mode := filepath.Base(os.Args[0])
|
|
|
|
console := CurrentConsole()
|
|
|
|
|
2017-06-20 11:48:14 +00:00
|
|
|
cfg := config.LoadConfig()
|
|
|
|
// replace \n and \l
|
|
|
|
banner := config.Banner
|
|
|
|
banner = strings.Replace(banner, "\\v", config.Version, -1)
|
2017-06-20 11:54:46 +00:00
|
|
|
banner = strings.Replace(banner, "\\s", "RancherOS "+runtime.GOARCH, -1)
|
2017-07-13 13:06:57 +00:00
|
|
|
banner = strings.Replace(banner, "\\r", config.GetKernelVersion(), -1)
|
2017-06-20 11:48:14 +00:00
|
|
|
banner = strings.Replace(banner, "\\n", cfg.Hostname, -1)
|
|
|
|
banner = strings.Replace(banner, "\\l", tty, -1)
|
|
|
|
banner = strings.Replace(banner, "\\\\", "\\", -1)
|
|
|
|
banner = banner + "\n"
|
2017-07-16 01:50:01 +00:00
|
|
|
banner = banner + "Autologin " + console + "\n"
|
2017-06-20 11:48:14 +00:00
|
|
|
fmt.Printf(banner)
|
|
|
|
|
2017-07-14 03:15:43 +00:00
|
|
|
loginBin := ""
|
|
|
|
args := []string{}
|
2017-07-16 01:50:01 +00:00
|
|
|
if console == "centos" || console == "fedora" ||
|
|
|
|
mode == "recovery" {
|
2017-07-14 03:15:43 +00:00
|
|
|
// For some reason, centos and fedora ttyS0 and tty1 don't work with `login -f rancher`
|
|
|
|
// until I make time to read their source, lets just give us a way to get work done
|
|
|
|
loginBin = "bash"
|
|
|
|
args = append(args, "--login")
|
2017-09-15 13:38:15 +00:00
|
|
|
if mode == "recovery" {
|
|
|
|
os.Setenv("PROMPT_COMMAND", `echo "[`+fmt.Sprintf("Recovery console %s@%s:${PWD}", user, cfg.Hostname)+`]"`)
|
|
|
|
}
|
2017-07-14 03:15:43 +00:00
|
|
|
} else {
|
|
|
|
loginBin = "login"
|
|
|
|
args = append(args, "-f", user)
|
2017-07-16 01:50:01 +00:00
|
|
|
// TODO: add a PROMPT_COMMAND if we haven't switch-rooted
|
2017-06-20 11:48:14 +00:00
|
|
|
}
|
2017-07-14 03:15:43 +00:00
|
|
|
|
|
|
|
loginBinPath, err := exec.LookPath(loginBin)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error finding %s in path: %s", cmd.Args[0], err)
|
|
|
|
return err
|
|
|
|
}
|
2017-07-16 01:50:01 +00:00
|
|
|
os.Setenv("TERM", "linux")
|
2017-07-14 03:15:43 +00:00
|
|
|
|
2017-07-14 08:05:57 +00:00
|
|
|
// Causes all sorts of issues
|
|
|
|
//return syscall.Exec(loginBinPath, args, os.Environ())
|
|
|
|
cmd = exec.Command(loginBinPath, args...)
|
|
|
|
cmd.Env = os.Environ()
|
2017-07-14 03:15:43 +00:00
|
|
|
|
2017-07-14 08:05:57 +00:00
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
log.Errorf("\nError starting %s: %s", cmd.Args[0], err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-06-20 11:48:14 +00:00
|
|
|
}
|