mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.0 KiB
188 lines
5.0 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package ssh
|
||
|
|
||
|
import (
|
||
9 years ago
|
"fmt"
|
||
9 years ago
|
"io"
|
||
11 years ago
|
"io/ioutil"
|
||
|
"net"
|
||
|
"os"
|
||
|
"os/exec"
|
||
9 years ago
|
"path/filepath"
|
||
11 years ago
|
"strings"
|
||
|
|
||
|
"github.com/Unknwon/com"
|
||
9 years ago
|
"golang.org/x/crypto/ssh"
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
11 years ago
|
|
||
9 years ago
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/setting"
|
||
11 years ago
|
)
|
||
|
|
||
9 years ago
|
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) {
|
||
11 years ago
|
for newChan := range chans {
|
||
|
if newChan.ChannelType() != "session" {
|
||
|
newChan.Reject(ssh.UnknownChannelType, "unknown channel type")
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
|
||
|
ch, reqs, err := newChan.Accept()
|
||
11 years ago
|
if err != nil {
|
||
9 years ago
|
log.Error(3, "Error accepting channel: %v", err)
|
||
11 years ago
|
continue
|
||
|
}
|
||
|
|
||
|
go func(in <-chan *ssh.Request) {
|
||
9 years ago
|
defer ch.Close()
|
||
11 years ago
|
for req := range in {
|
||
9 years ago
|
payload := cleanCommand(string(req.Payload))
|
||
11 years ago
|
switch req.Type {
|
||
|
case "env":
|
||
|
args := strings.Split(strings.Replace(payload, "\x00", "", -1), "\v")
|
||
|
if len(args) != 2 {
|
||
9 years ago
|
log.Warn("SSH: Invalid env arguments: '%#v'", args)
|
||
9 years ago
|
continue
|
||
11 years ago
|
}
|
||
|
args[0] = strings.TrimLeft(args[0], "\x04")
|
||
|
_, _, err := com.ExecCmdBytes("env", args[0]+"="+args[1])
|
||
|
if err != nil {
|
||
|
log.Error(3, "env: %v", err)
|
||
9 years ago
|
return
|
||
11 years ago
|
}
|
||
|
case "exec":
|
||
9 years ago
|
cmdName := strings.TrimLeft(payload, "'()")
|
||
9 years ago
|
log.Trace("SSH: Payload: %v", cmdName)
|
||
9 years ago
|
|
||
|
args := []string{"serv", "key-" + keyID, "--config=" + setting.CustomConf}
|
||
9 years ago
|
log.Trace("SSH: Arguments: %v", args)
|
||
9 years ago
|
cmd := exec.Command(setting.AppPath, args...)
|
||
9 years ago
|
cmd.Env = append(os.Environ(), "SSH_ORIGINAL_COMMAND="+cmdName)
|
||
9 years ago
|
|
||
|
stdout, err := cmd.StdoutPipe()
|
||
|
if err != nil {
|
||
9 years ago
|
log.Error(3, "SSH: StdoutPipe: %v", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
stderr, err := cmd.StderrPipe()
|
||
|
if err != nil {
|
||
9 years ago
|
log.Error(3, "SSH: StderrPipe: %v", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
input, err := cmd.StdinPipe()
|
||
|
if err != nil {
|
||
9 years ago
|
log.Error(3, "SSH: StdinPipe: %v", err)
|
||
9 years ago
|
return
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// FIXME: check timeout
|
||
9 years ago
|
if err = cmd.Start(); err != nil {
|
||
9 years ago
|
log.Error(3, "SSH: Start: %v", err)
|
||
9 years ago
|
return
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
req.Reply(true, nil)
|
||
9 years ago
|
go io.Copy(input, ch)
|
||
|
io.Copy(ch, stdout)
|
||
|
io.Copy(ch.Stderr(), stderr)
|
||
|
|
||
|
if err = cmd.Wait(); err != nil {
|
||
9 years ago
|
log.Error(3, "SSH: Wait: %v", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
ch.SendRequest("exit-status", false, []byte{0, 0, 0, 0})
|
||
|
return
|
||
|
default:
|
||
11 years ago
|
}
|
||
|
}
|
||
9 years ago
|
}(reqs)
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func listen(config *ssh.ServerConfig, host string, port int) {
|
||
|
listener, err := net.Listen("tcp", host+":"+com.ToStr(port))
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
log.Fatal(4, "Fail to start SSH server: %v", err)
|
||
11 years ago
|
}
|
||
|
for {
|
||
|
// Once a ServerConfig has been configured, connections can be accepted.
|
||
|
conn, err := listener.Accept()
|
||
|
if err != nil {
|
||
9 years ago
|
log.Error(3, "SSH: Error accepting incoming connection: %v", err)
|
||
11 years ago
|
continue
|
||
|
}
|
||
9 years ago
|
|
||
11 years ago
|
// Before use, a handshake must be performed on the incoming net.Conn.
|
||
9 years ago
|
// 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 {
|
||
|
log.Warn("SSH: Handshaking was terminated: %v", err)
|
||
|
} else {
|
||
|
log.Error(3, "SSH: Error on handshaking: %v", err)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
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)
|
||
|
}()
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Listen starts a SSH server listens on given port.
|
||
8 years ago
|
func Listen(host string, port int, ciphers []string) {
|
||
11 years ago
|
config := &ssh.ServerConfig{
|
||
8 years ago
|
Config: ssh.Config{
|
||
|
Ciphers: ciphers,
|
||
|
},
|
||
11 years ago
|
PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
|
||
9 years ago
|
pkey, err := models.SearchPublicKeyByContent(strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key))))
|
||
|
if err != nil {
|
||
|
log.Error(3, "SearchPublicKeyByContent: %v", err)
|
||
|
return nil, err
|
||
|
}
|
||
|
return &ssh.Permissions{Extensions: map[string]string{"key-id": com.ToStr(pkey.ID)}}, nil
|
||
11 years ago
|
},
|
||
|
}
|
||
|
|
||
9 years ago
|
keyPath := filepath.Join(setting.AppDataPath, "ssh/gogs.rsa")
|
||
|
if !com.IsExist(keyPath) {
|
||
|
os.MkdirAll(filepath.Dir(keyPath), os.ModePerm)
|
||
8 years ago
|
_, stderr, err := com.ExecCmd(setting.SSH.KeygenPath, "-f", keyPath, "-t", "rsa", "-N", "")
|
||
9 years ago
|
if err != nil {
|
||
|
panic(fmt.Sprintf("Fail to generate private key: %v - %s", err, stderr))
|
||
|
}
|
||
9 years ago
|
log.Trace("SSH: New private key is generateed: %s", keyPath)
|
||
9 years ago
|
}
|
||
|
|
||
|
privateBytes, err := ioutil.ReadFile(keyPath)
|
||
11 years ago
|
if err != nil {
|
||
9 years ago
|
panic("SSH: Fail to load private key")
|
||
11 years ago
|
}
|
||
|
private, err := ssh.ParsePrivateKey(privateBytes)
|
||
|
if err != nil {
|
||
9 years ago
|
panic("SSH: Fail to parse private key")
|
||
11 years ago
|
}
|
||
|
config.AddHostKey(private)
|
||
|
|
||
8 years ago
|
go listen(config, host, port)
|
||
11 years ago
|
}
|