2014-07-26 00:24:27 -04:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-02-07 02:13:12 -05:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2014-07-26 00:24:27 -04:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2019-02-07 02:13:12 -05:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2015-11-08 16:59:56 -05:00
|
|
|
"io"
|
2014-07-26 00:24:27 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-11-08 16:59:56 -05:00
|
|
|
"path/filepath"
|
2014-07-26 00:24:27 -04:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Unknwon/com"
|
2015-11-08 16:59:56 -05:00
|
|
|
"golang.org/x/crypto/ssh"
|
2014-07-26 00:24:27 -04:00
|
|
|
|
2016-11-10 11:24:48 -05:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2014-07-26 00:24:27 -04:00
|
|
|
)
|
|
|
|
|
2015-11-08 16:59:56 -05:00
|
|
|
func cleanCommand(cmd string) string {
|
|
|
|
i := strings.Index(cmd, "git")
|
|
|
|
if i == -1 {
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
return cmd[i:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleServerConn(keyID string, chans <-chan ssh.NewChannel) {
|
2014-07-26 00:24:27 -04:00
|
|
|
for newChan := range chans {
|
|
|
|
if newChan.ChannelType() != "session" {
|
2019-06-12 15:41:28 -04:00
|
|
|
err := newChan.Reject(ssh.UnknownChannelType, "unknown channel type")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error rejecting channel: %v", err)
|
|
|
|
}
|
2014-07-26 00:24:27 -04:00
|
|
|
continue
|
|
|
|
}
|
2015-11-08 16:59:56 -05:00
|
|
|
|
|
|
|
ch, reqs, err := newChan.Accept()
|
2014-07-26 00:24:27 -04:00
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("Error accepting channel: %v", err)
|
2014-07-26 00:24:27 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(in <-chan *ssh.Request) {
|
2019-06-12 15:41:28 -04:00
|
|
|
defer func() {
|
|
|
|
if err = ch.Close(); err != nil {
|
|
|
|
log.Error("Close: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2014-07-26 00:24:27 -04:00
|
|
|
for req := range in {
|
2015-11-08 16:59:56 -05:00
|
|
|
payload := cleanCommand(string(req.Payload))
|
2014-07-26 00:24:27 -04:00
|
|
|
switch req.Type {
|
|
|
|
case "exec":
|
2015-11-08 16:59:56 -05:00
|
|
|
cmdName := strings.TrimLeft(payload, "'()")
|
2016-02-01 12:10:49 -05:00
|
|
|
log.Trace("SSH: Payload: %v", cmdName)
|
2015-11-30 15:40:05 -05:00
|
|
|
|
|
|
|
args := []string{"serv", "key-" + keyID, "--config=" + setting.CustomConf}
|
2016-02-01 12:10:49 -05:00
|
|
|
log.Trace("SSH: Arguments: %v", args)
|
2015-11-30 15:40:05 -05:00
|
|
|
cmd := exec.Command(setting.AppPath, args...)
|
2017-01-23 17:44:23 -05:00
|
|
|
cmd.Env = append(
|
|
|
|
os.Environ(),
|
|
|
|
"SSH_ORIGINAL_COMMAND="+cmdName,
|
|
|
|
"SKIP_MINWINSVC=1",
|
|
|
|
)
|
2015-11-08 16:59:56 -05:00
|
|
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("SSH: StdoutPipe: %v", err)
|
2015-11-08 16:59:56 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("SSH: StderrPipe: %v", err)
|
2015-11-08 16:59:56 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
input, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("SSH: StdinPipe: %v", err)
|
2015-11-08 16:59:56 -05:00
|
|
|
return
|
2014-07-26 00:24:27 -04:00
|
|
|
}
|
2015-11-08 16:59:56 -05:00
|
|
|
|
2015-11-15 17:07:44 -05:00
|
|
|
// FIXME: check timeout
|
2015-11-08 16:59:56 -05:00
|
|
|
if err = cmd.Start(); err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("SSH: Start: %v", err)
|
2015-11-08 16:59:56 -05:00
|
|
|
return
|
2015-11-14 13:21:31 -05:00
|
|
|
}
|
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
err = req.Reply(true, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("SSH: Reply: %v", err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
_, err = io.Copy(input, ch)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("SSH: Copy: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
_, err = io.Copy(ch, stdout)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("SSH: Copy: %v", err)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(ch.Stderr(), stderr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("SSH: Copy: %v", err)
|
|
|
|
}
|
2015-11-14 13:21:31 -05:00
|
|
|
|
|
|
|
if err = cmd.Wait(); err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("SSH: Wait: %v", err)
|
2015-11-08 16:59:56 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
_, err = ch.SendRequest("exit-status", false, []byte{0, 0, 0, 0})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("SSH: SendRequest: %v", err)
|
|
|
|
}
|
2015-11-08 16:59:56 -05:00
|
|
|
return
|
|
|
|
default:
|
2014-07-26 00:24:27 -04:00
|
|
|
}
|
|
|
|
}
|
2015-11-08 16:59:56 -05:00
|
|
|
}(reqs)
|
2014-07-26 00:24:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 07:26:43 -05:00
|
|
|
func listen(config *ssh.ServerConfig, host string, port int) {
|
|
|
|
listener, err := net.Listen("tcp", host+":"+com.ToStr(port))
|
2014-07-26 00:24:27 -04:00
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Fatal("Failed to start SSH server: %v", err)
|
2014-07-26 00:24:27 -04:00
|
|
|
}
|
|
|
|
for {
|
|
|
|
// Once a ServerConfig has been configured, connections can be accepted.
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("SSH: Error accepting incoming connection: %v", err)
|
2014-07-26 00:24:27 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-02-01 12:10:49 -05:00
|
|
|
|
2014-07-26 00:24:27 -04:00
|
|
|
// Before use, a handshake must be performed on the incoming net.Conn.
|
2016-02-01 12:10:49 -05:00
|
|
|
// It must be handled in a separate goroutine,
|
|
|
|
// otherwise one user could easily block entire loop.
|
|
|
|
// For example, user could be asked to trust server key fingerprint and hangs.
|
|
|
|
go func() {
|
|
|
|
log.Trace("SSH: Handshaking for %s", conn.RemoteAddr())
|
|
|
|
sConn, chans, reqs, err := ssh.NewServerConn(conn, config)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
2019-01-19 11:29:17 -05:00
|
|
|
log.Warn("SSH: Handshaking with %s was terminated: %v", conn.RemoteAddr(), err)
|
2016-02-01 12:10:49 -05:00
|
|
|
} else {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("SSH: Error on handshaking with %s: %v", conn.RemoteAddr(), err)
|
2016-02-01 12:10:49 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2015-11-08 16:59:56 -05:00
|
|
|
|
2016-02-01 12:10:49 -05:00
|
|
|
log.Trace("SSH: Connection from %s (%s)", sConn.RemoteAddr(), sConn.ClientVersion())
|
|
|
|
// The incoming Request channel must be serviced.
|
|
|
|
go ssh.DiscardRequests(reqs)
|
|
|
|
go handleServerConn(sConn.Permissions.Extensions["key-id"], chans)
|
|
|
|
}()
|
2014-07-26 00:24:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen starts a SSH server listens on given port.
|
2017-11-02 11:26:41 -04:00
|
|
|
func Listen(host string, port int, ciphers []string, keyExchanges []string, macs []string) {
|
2014-07-26 00:24:27 -04:00
|
|
|
config := &ssh.ServerConfig{
|
2017-10-21 09:13:41 -04:00
|
|
|
Config: ssh.Config{
|
2017-11-02 11:26:41 -04:00
|
|
|
Ciphers: ciphers,
|
|
|
|
KeyExchanges: keyExchanges,
|
|
|
|
MACs: macs,
|
2017-10-21 09:13:41 -04:00
|
|
|
},
|
2014-07-26 00:24:27 -04:00
|
|
|
PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
|
2015-11-08 16:59:56 -05:00
|
|
|
pkey, err := models.SearchPublicKeyByContent(strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key))))
|
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("SearchPublicKeyByContent: %v", err)
|
2015-11-08 16:59:56 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ssh.Permissions{Extensions: map[string]string{"key-id": com.ToStr(pkey.ID)}}, nil
|
2014-07-26 00:24:27 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-11-14 13:21:31 -05:00
|
|
|
keyPath := filepath.Join(setting.AppDataPath, "ssh/gogs.rsa")
|
|
|
|
if !com.IsExist(keyPath) {
|
2016-11-30 18:56:15 -05:00
|
|
|
filePath := filepath.Dir(keyPath)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Error("Failed to create dir %s: %v", filePath, err)
|
2016-11-30 18:56:15 -05:00
|
|
|
}
|
|
|
|
|
2019-02-07 02:13:12 -05:00
|
|
|
err := GenKeyPair(keyPath)
|
2015-11-14 13:21:31 -05:00
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Fatal("Failed to generate private key: %v", err)
|
2015-11-14 13:21:31 -05:00
|
|
|
}
|
2016-02-01 12:10:49 -05:00
|
|
|
log.Trace("SSH: New private key is generateed: %s", keyPath)
|
2015-11-14 13:21:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
privateBytes, err := ioutil.ReadFile(keyPath)
|
2014-07-26 00:24:27 -04:00
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Fatal("SSH: Failed to load private key")
|
2014-07-26 00:24:27 -04:00
|
|
|
}
|
|
|
|
private, err := ssh.ParsePrivateKey(privateBytes)
|
|
|
|
if err != nil {
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Fatal("SSH: Failed to parse private key")
|
2014-07-26 00:24:27 -04:00
|
|
|
}
|
|
|
|
config.AddHostKey(private)
|
|
|
|
|
2016-12-22 07:26:43 -05:00
|
|
|
go listen(config, host, port)
|
2014-07-26 00:24:27 -04:00
|
|
|
}
|
2019-02-07 02:13:12 -05:00
|
|
|
|
|
|
|
// GenKeyPair make a pair of public and private keys for SSH access.
|
|
|
|
// Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
|
|
|
|
// Private Key generated is PEM encoded
|
|
|
|
func GenKeyPair(keyPath string) error {
|
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
|
|
|
|
f, err := os.OpenFile(keyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
defer func() {
|
|
|
|
if err = f.Close(); err != nil {
|
|
|
|
log.Error("Close: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-02-07 02:13:12 -05:00
|
|
|
|
|
|
|
if err := pem.Encode(f, privateKeyPEM); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate public key
|
|
|
|
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
public := ssh.MarshalAuthorizedKey(pub)
|
|
|
|
p, err := os.OpenFile(keyPath+".pub", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
defer func() {
|
|
|
|
if err = p.Close(); err != nil {
|
|
|
|
log.Error("Close: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-02-07 02:13:12 -05:00
|
|
|
_, err = p.Write(public)
|
|
|
|
return err
|
|
|
|
}
|