2022-02-15 19:59:21 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-02-24 20:27:42 +00:00
|
|
|
//go:build linux || (darwin && !ios)
|
|
|
|
// +build linux darwin,!ios
|
2022-02-15 19:59:21 +00:00
|
|
|
|
|
|
|
// Package tailssh is an SSH server integrated into Tailscale.
|
|
|
|
package tailssh
|
|
|
|
|
|
|
|
import (
|
2022-02-23 23:47:57 +00:00
|
|
|
"context"
|
2022-02-15 19:59:21 +00:00
|
|
|
"encoding/json"
|
2022-02-18 22:10:26 +00:00
|
|
|
"errors"
|
2022-02-15 19:59:21 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-02-19 03:07:04 +00:00
|
|
|
"os/user"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2022-02-15 19:59:21 +00:00
|
|
|
"syscall"
|
2022-02-18 22:10:26 +00:00
|
|
|
"time"
|
2022-02-15 19:59:21 +00:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/creack/pty"
|
|
|
|
"github.com/gliderlabs/ssh"
|
|
|
|
"inet.af/netaddr"
|
2022-02-24 22:03:36 +00:00
|
|
|
"tailscale.com/cmd/tailscaled/childproc"
|
2022-02-15 19:59:21 +00:00
|
|
|
"tailscale.com/envknob"
|
|
|
|
"tailscale.com/ipn/ipnlocal"
|
|
|
|
"tailscale.com/net/tsaddr"
|
2022-02-18 22:10:26 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2022-02-15 19:59:21 +00:00
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
2022-02-24 22:03:36 +00:00
|
|
|
func init() {
|
|
|
|
childproc.Add("ssh", sshChild)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshChild([]string) error {
|
|
|
|
fmt.Println("TODO(maisem): ssh dbus stuff")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:59:21 +00:00
|
|
|
// TODO(bradfitz): this is all very temporary as code is temporarily
|
|
|
|
// being moved around; it will be restructured and documented in
|
|
|
|
// following commits.
|
|
|
|
|
|
|
|
// Handle handles an SSH connection from c.
|
|
|
|
func Handle(logf logger.Logf, lb *ipnlocal.LocalBackend, c net.Conn) error {
|
2022-02-24 21:31:54 +00:00
|
|
|
srv := &server{lb, logf}
|
|
|
|
ss, err := srv.newSSHServer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ss.HandleConn(c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *server) newSSHServer() (*ssh.Server, error) {
|
|
|
|
ss := &ssh.Server{
|
|
|
|
Handler: srv.handleSSH,
|
2022-02-15 19:59:21 +00:00
|
|
|
RequestHandlers: map[string]ssh.RequestHandler{},
|
|
|
|
SubsystemHandlers: map[string]ssh.SubsystemHandler{},
|
|
|
|
ChannelHandlers: map[string]ssh.ChannelHandler{},
|
|
|
|
}
|
|
|
|
for k, v := range ssh.DefaultRequestHandlers {
|
2022-02-24 21:31:54 +00:00
|
|
|
ss.RequestHandlers[k] = v
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
|
|
|
for k, v := range ssh.DefaultChannelHandlers {
|
2022-02-24 21:31:54 +00:00
|
|
|
ss.ChannelHandlers[k] = v
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
|
|
|
for k, v := range ssh.DefaultSubsystemHandlers {
|
2022-02-24 21:31:54 +00:00
|
|
|
ss.SubsystemHandlers[k] = v
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
2022-02-24 21:31:54 +00:00
|
|
|
keys, err := srv.lb.GetSSH_HostKeys()
|
2022-02-17 21:28:14 +00:00
|
|
|
if err != nil {
|
2022-02-24 21:31:54 +00:00
|
|
|
return nil, err
|
2022-02-17 21:28:14 +00:00
|
|
|
}
|
|
|
|
for _, signer := range keys {
|
2022-02-24 21:31:54 +00:00
|
|
|
ss.AddHostKey(signer)
|
2022-02-17 21:28:14 +00:00
|
|
|
}
|
2022-02-24 21:31:54 +00:00
|
|
|
return ss, nil
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
lb *ipnlocal.LocalBackend
|
|
|
|
logf logger.Logf
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:10:26 +00:00
|
|
|
var debugPolicyFile = envknob.String("TS_DEBUG_SSH_POLICY_FILE")
|
|
|
|
|
|
|
|
func (srv *server) sshPolicy() (_ *tailcfg.SSHPolicy, ok bool) {
|
|
|
|
lb := srv.lb
|
|
|
|
nm := lb.NetMap()
|
|
|
|
if nm == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if pol := nm.SSHPolicy; pol != nil {
|
|
|
|
return pol, true
|
|
|
|
}
|
|
|
|
if debugPolicyFile != "" {
|
|
|
|
f, err := os.ReadFile(debugPolicyFile)
|
|
|
|
if err != nil {
|
|
|
|
srv.logf("error reading debug SSH policy file: %v", err)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
p := new(tailcfg.SSHPolicy)
|
|
|
|
if err := json.Unmarshal(f, p); err != nil {
|
|
|
|
srv.logf("invalid JSON in %v: %v", debugPolicyFile, err)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return p, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:59:21 +00:00
|
|
|
func (srv *server) handleSSH(s ssh.Session) {
|
|
|
|
lb := srv.lb
|
|
|
|
logf := srv.logf
|
2022-02-19 03:07:04 +00:00
|
|
|
sshUser := s.User()
|
2022-02-15 19:59:21 +00:00
|
|
|
addr := s.RemoteAddr()
|
2022-02-19 03:07:04 +00:00
|
|
|
logf("Handling SSH from %v for user %v", addr, sshUser)
|
2022-02-15 19:59:21 +00:00
|
|
|
ta, ok := addr.(*net.TCPAddr)
|
|
|
|
if !ok {
|
|
|
|
logf("tsshd: rejecting non-TCP addr %T %v", addr, addr)
|
|
|
|
s.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tanetaddr, ok := netaddr.FromStdIP(ta.IP)
|
|
|
|
if !ok {
|
|
|
|
logf("tsshd: rejecting unparseable addr %v", ta.IP)
|
|
|
|
s.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !tsaddr.IsTailscaleIP(tanetaddr) {
|
|
|
|
logf("tsshd: rejecting non-Tailscale addr %v", ta.IP)
|
|
|
|
s.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:10:26 +00:00
|
|
|
pol, ok := srv.sshPolicy()
|
|
|
|
if !ok {
|
|
|
|
logf("tsshd: rejecting connection; no SSH policy")
|
2022-02-15 19:59:21 +00:00
|
|
|
s.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
2022-02-18 22:10:26 +00:00
|
|
|
|
2022-02-15 19:59:21 +00:00
|
|
|
srcIPP := netaddr.IPPortFrom(tanetaddr, uint16(ta.Port))
|
|
|
|
node, uprof, ok := lb.WhoIs(srcIPP)
|
|
|
|
if !ok {
|
|
|
|
fmt.Fprintf(s, "Hello, %v. I don't know who you are.\n", srcIPP)
|
2022-02-18 22:10:26 +00:00
|
|
|
s.Exit(1)
|
2022-02-15 19:59:21 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-18 22:10:26 +00:00
|
|
|
|
|
|
|
srcIP := srcIPP.IP()
|
2022-02-24 16:58:53 +00:00
|
|
|
ci := &sshConnInfo{
|
2022-02-18 22:10:26 +00:00
|
|
|
now: time.Now(),
|
2022-02-19 03:07:04 +00:00
|
|
|
sshUser: sshUser,
|
2022-02-18 22:10:26 +00:00
|
|
|
srcIP: srcIP,
|
|
|
|
node: node,
|
|
|
|
uprof: &uprof,
|
|
|
|
}
|
2022-02-24 16:58:53 +00:00
|
|
|
action, localUser, ok := evalSSHPolicy(pol, ci)
|
2022-02-18 22:10:26 +00:00
|
|
|
if ok && action.Message != "" {
|
2022-02-19 23:37:13 +00:00
|
|
|
io.WriteString(s.Stderr(), strings.Replace(action.Message, "\n", "\r\n", -1))
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
|
|
|
if !ok || action.Reject {
|
|
|
|
logf("ssh: access denied for %q from %v", uprof.LoginName, srcIP)
|
2022-02-15 19:59:21 +00:00
|
|
|
s.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
2022-02-18 22:10:26 +00:00
|
|
|
if !action.Accept || action.HoldAndDelegate != "" {
|
|
|
|
fmt.Fprintf(s, "TODO: other SSHAction outcomes")
|
|
|
|
s.Exit(1)
|
|
|
|
}
|
2022-02-19 23:37:13 +00:00
|
|
|
lu, err := user.Lookup(localUser)
|
|
|
|
if err != nil {
|
|
|
|
logf("ssh: user Lookup %q: %v", localUser, err)
|
2022-02-18 22:10:26 +00:00
|
|
|
s.Exit(1)
|
|
|
|
}
|
2022-02-19 23:37:13 +00:00
|
|
|
|
2022-02-23 23:47:57 +00:00
|
|
|
var ctx context.Context = context.Background()
|
|
|
|
if action.SesssionDuration != 0 {
|
|
|
|
sctx := newSSHContext()
|
|
|
|
ctx = sctx
|
|
|
|
t := time.AfterFunc(action.SesssionDuration, func() {
|
|
|
|
sctx.CloseWithError(userVisibleError{
|
|
|
|
fmt.Sprintf("Session timeout of %v elapsed.", action.SesssionDuration),
|
|
|
|
context.DeadlineExceeded,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer t.Stop()
|
|
|
|
}
|
|
|
|
srv.handleAcceptedSSH(ctx, s, ci, lu)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleAcceptedSSH handles s once it's been accepted and determined
|
|
|
|
// that it should run as local system user lu.
|
|
|
|
//
|
|
|
|
// When ctx is done, the session is forcefully terminated. If its Err
|
|
|
|
// is an SSHTerminationError, its SSHTerminationMessage is sent to the
|
|
|
|
// user.
|
|
|
|
func (srv *server) handleAcceptedSSH(ctx context.Context, s ssh.Session, ci *sshConnInfo, lu *user.User) {
|
|
|
|
logf := srv.logf
|
|
|
|
localUser := lu.Username
|
|
|
|
|
|
|
|
var err error
|
|
|
|
ptyReq, winCh, isPty := s.Pty()
|
|
|
|
logf("ssh: connection from %v %v to %v@ => %q. command = %q, env = %q", ci.srcIP, ci.uprof.LoginName, ci.sshUser, localUser, s.Command(), s.Environ())
|
2022-02-15 19:59:21 +00:00
|
|
|
var cmd *exec.Cmd
|
2022-02-19 23:37:13 +00:00
|
|
|
if euid := os.Geteuid(); euid != 0 {
|
|
|
|
if lu.Uid != fmt.Sprint(euid) {
|
|
|
|
logf("ssh: can't switch to user %q from process euid %v", localUser, euid)
|
|
|
|
fmt.Fprintf(s, "can't switch user\n")
|
2022-02-19 03:07:04 +00:00
|
|
|
s.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
2022-02-19 23:37:13 +00:00
|
|
|
cmd = exec.Command(loginShell(lu.Uid))
|
2022-02-24 19:08:06 +00:00
|
|
|
if rawCmd := s.RawCommand(); rawCmd != "" {
|
|
|
|
cmd.Args = append(cmd.Args, "-c", rawCmd)
|
|
|
|
}
|
2022-02-19 23:37:13 +00:00
|
|
|
} else {
|
|
|
|
if rawCmd := s.RawCommand(); rawCmd != "" {
|
|
|
|
cmd = exec.Command("/usr/bin/env", "su", "-c", rawCmd, localUser)
|
|
|
|
// TODO: and Env for PATH, SSH_CONNECTION, SSH_CLIENT, XDG_SESSION_TYPE, XDG_*, etc
|
|
|
|
} else {
|
|
|
|
cmd = exec.Command("/usr/bin/env", "su", "-", localUser)
|
|
|
|
}
|
|
|
|
}
|
2022-02-24 19:08:06 +00:00
|
|
|
cmd.Dir = lu.HomeDir
|
|
|
|
cmd.Env = append(cmd.Env, envForUser(lu)...)
|
2022-02-19 23:37:13 +00:00
|
|
|
if ptyReq.Term != "" {
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
|
|
|
|
}
|
|
|
|
// TODO(bradfitz,maisem): also blend in user's s.Environ()
|
|
|
|
logf("Running: %q", cmd.Args)
|
|
|
|
var toCmd io.WriteCloser
|
|
|
|
var fromCmd io.ReadCloser
|
|
|
|
if isPty {
|
|
|
|
f, err := pty.StartWithSize(cmd, &pty.Winsize{
|
|
|
|
Rows: uint16(ptyReq.Window.Width),
|
|
|
|
Cols: uint16(ptyReq.Window.Height),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logf("running shell: %v", err)
|
2022-02-19 03:07:04 +00:00
|
|
|
s.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
2022-02-19 23:37:13 +00:00
|
|
|
defer f.Close()
|
|
|
|
toCmd = f
|
|
|
|
fromCmd = f
|
|
|
|
go func() {
|
|
|
|
for win := range winCh {
|
|
|
|
setWinsize(f, win.Width, win.Height)
|
|
|
|
}
|
|
|
|
}()
|
2022-02-15 19:59:21 +00:00
|
|
|
} else {
|
2022-02-19 23:37:13 +00:00
|
|
|
stdin, stdout, stderr, err := startWithStdPipes(cmd)
|
|
|
|
if err != nil {
|
|
|
|
logf("ssh: start error: %f", err)
|
|
|
|
s.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fromCmd, toCmd = stdout, stdin
|
|
|
|
go func() { io.Copy(s.Stderr(), stderr) }()
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
2022-02-18 22:10:26 +00:00
|
|
|
|
2022-02-23 23:47:57 +00:00
|
|
|
if ctx.Done() != nil {
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-ctx.Done():
|
|
|
|
err := ctx.Err()
|
|
|
|
if serr, ok := err.(SSHTerminationError); ok {
|
|
|
|
msg := serr.SSHTerminationMessage()
|
|
|
|
if msg != "" {
|
|
|
|
io.WriteString(s.Stderr(), "\r\n\r\n"+msg+"\r\n\r\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logf("terminating SSH session from %v: %v", ci.srcIP, err)
|
|
|
|
cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
}()
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 19:59:21 +00:00
|
|
|
go func() {
|
2022-02-19 23:37:13 +00:00
|
|
|
_, err := io.Copy(toCmd, s) // stdin
|
|
|
|
logf("ssh: stdin copy: %v", err)
|
|
|
|
toCmd.Close()
|
2022-02-15 19:59:21 +00:00
|
|
|
}()
|
|
|
|
go func() {
|
2022-02-19 23:37:13 +00:00
|
|
|
_, err := io.Copy(s, fromCmd) // stdout
|
|
|
|
logf("ssh: stdout copy: %v", err)
|
2022-02-15 19:59:21 +00:00
|
|
|
}()
|
2022-02-19 23:37:13 +00:00
|
|
|
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err == nil {
|
|
|
|
logf("ssh: Wait: ok")
|
|
|
|
s.Exit(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ee, ok := err.(*exec.ExitError); ok {
|
|
|
|
code := ee.ProcessState.ExitCode()
|
|
|
|
logf("ssh: Wait: code=%v", code)
|
|
|
|
s.Exit(code)
|
|
|
|
return
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
2022-02-19 23:37:13 +00:00
|
|
|
|
|
|
|
logf("ssh: Wait: %v", err)
|
|
|
|
s.Exit(1)
|
2022-02-15 19:59:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func setWinsize(f *os.File, w, h int) {
|
|
|
|
syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ),
|
|
|
|
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
|
|
|
|
}
|
2022-02-18 22:10:26 +00:00
|
|
|
|
2022-02-24 16:58:53 +00:00
|
|
|
type sshConnInfo struct {
|
2022-02-18 22:10:26 +00:00
|
|
|
// now is the time to consider the present moment for the
|
|
|
|
// purposes of rule evaluation.
|
|
|
|
now time.Time
|
|
|
|
|
|
|
|
// sshUser is the requested local SSH username ("root", "alice", etc).
|
|
|
|
sshUser string
|
|
|
|
|
|
|
|
// srcIP is the Tailscale IP that the connection came from.
|
|
|
|
srcIP netaddr.IP
|
|
|
|
|
|
|
|
// node is srcIP's node.
|
|
|
|
node *tailcfg.Node
|
|
|
|
|
|
|
|
// uprof is node's UserProfile.
|
|
|
|
uprof *tailcfg.UserProfile
|
|
|
|
}
|
|
|
|
|
2022-02-24 16:58:53 +00:00
|
|
|
func evalSSHPolicy(pol *tailcfg.SSHPolicy, ci *sshConnInfo) (a *tailcfg.SSHAction, localUser string, ok bool) {
|
2022-02-18 22:10:26 +00:00
|
|
|
for _, r := range pol.Rules {
|
2022-02-24 16:58:53 +00:00
|
|
|
if a, localUser, err := matchRule(r, ci); err == nil {
|
2022-02-18 22:10:26 +00:00
|
|
|
return a, localUser, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal errors for testing; they don't escape to callers or logs.
|
|
|
|
var (
|
|
|
|
errNilRule = errors.New("nil rule")
|
|
|
|
errNilAction = errors.New("nil action")
|
|
|
|
errRuleExpired = errors.New("rule expired")
|
|
|
|
errPrincipalMatch = errors.New("principal didn't match")
|
|
|
|
errUserMatch = errors.New("user didn't match")
|
|
|
|
)
|
|
|
|
|
2022-02-24 16:58:53 +00:00
|
|
|
func matchRule(r *tailcfg.SSHRule, ci *sshConnInfo) (a *tailcfg.SSHAction, localUser string, err error) {
|
2022-02-18 22:10:26 +00:00
|
|
|
if r == nil {
|
|
|
|
return nil, "", errNilRule
|
|
|
|
}
|
|
|
|
if r.Action == nil {
|
|
|
|
return nil, "", errNilAction
|
|
|
|
}
|
2022-02-24 16:58:53 +00:00
|
|
|
if r.RuleExpires != nil && ci.now.After(*r.RuleExpires) {
|
2022-02-18 22:10:26 +00:00
|
|
|
return nil, "", errRuleExpired
|
|
|
|
}
|
2022-02-24 16:58:53 +00:00
|
|
|
if !matchesPrincipal(r.Principals, ci) {
|
2022-02-18 22:10:26 +00:00
|
|
|
return nil, "", errPrincipalMatch
|
|
|
|
}
|
|
|
|
if !r.Action.Reject || r.SSHUsers != nil {
|
2022-02-24 16:58:53 +00:00
|
|
|
localUser = mapLocalUser(r.SSHUsers, ci.sshUser)
|
2022-02-18 22:10:26 +00:00
|
|
|
if localUser == "" {
|
|
|
|
return nil, "", errUserMatch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.Action, localUser, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapLocalUser(ruleSSHUsers map[string]string, reqSSHUser string) (localUser string) {
|
|
|
|
if v, ok := ruleSSHUsers[reqSSHUser]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return ruleSSHUsers["*"]
|
|
|
|
}
|
|
|
|
|
2022-02-24 16:58:53 +00:00
|
|
|
func matchesPrincipal(ps []*tailcfg.SSHPrincipal, ci *sshConnInfo) bool {
|
2022-02-18 22:10:26 +00:00
|
|
|
for _, p := range ps {
|
|
|
|
if p == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p.Any {
|
|
|
|
return true
|
|
|
|
}
|
2022-02-24 16:58:53 +00:00
|
|
|
if !p.Node.IsZero() && ci.node != nil && p.Node == ci.node.StableID {
|
2022-02-18 22:10:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if p.NodeIP != "" {
|
2022-02-24 16:58:53 +00:00
|
|
|
if ip, _ := netaddr.ParseIP(p.NodeIP); ip == ci.srcIP {
|
2022-02-18 22:10:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2022-02-24 16:58:53 +00:00
|
|
|
if p.UserLogin != "" && ci.uprof != nil && ci.uprof.LoginName == p.UserLogin {
|
2022-02-18 22:10:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-02-19 03:07:04 +00:00
|
|
|
|
|
|
|
func loginShell(uid string) string {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
out, _ := exec.Command("getent", "passwd", uid).Output()
|
|
|
|
// out is "root:x:0:0:root:/root:/bin/bash"
|
|
|
|
f := strings.SplitN(string(out), ":", 10)
|
|
|
|
if len(f) > 6 {
|
2022-02-24 19:08:06 +00:00
|
|
|
return strings.TrimSpace(f[6]) // shell
|
2022-02-19 03:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if e := os.Getenv("SHELL"); e != "" {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return "/bin/bash"
|
|
|
|
}
|
2022-02-19 23:37:13 +00:00
|
|
|
|
|
|
|
func startWithStdPipes(cmd *exec.Cmd) (stdin io.WriteCloser, stdout, stderr io.ReadCloser, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
for _, c := range []io.Closer{stdin, stdout, stderr} {
|
|
|
|
if c != nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
stdin, err = cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stdout, err = cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stderr, err = cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = cmd.Start()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func envForUser(u *user.User) []string {
|
|
|
|
return []string{
|
|
|
|
fmt.Sprintf("SHELL=" + loginShell(u.Uid)),
|
|
|
|
fmt.Sprintf("USER=" + u.Username),
|
|
|
|
fmt.Sprintf("HOME=" + u.HomeDir),
|
|
|
|
}
|
|
|
|
}
|