Ejemplo n.º 1
0
// NewClient initializes a new client
func NewClient(conn *ssh.ServerConn, chans <-chan ssh.NewChannel, reqs <-chan *ssh.Request, server *Server) *Client {
	client := Client{
		Idx:        clientCounter,
		ClientID:   conn.RemoteAddr().String(),
		ChannelIdx: 0,
		Conn:       conn,
		Chans:      chans,
		Reqs:       reqs,
		Server:     server,

		// Default ClientConfig, will be overwritten if a hook is used
		Config: &ClientConfig{
			ImageName:  strings.Replace(conn.User(), "_", "/", -1),
			RemoteUser: "******",
			Env:        Environment{},
			Command:    make([]string, 0),
		},
	}

	if server.LocalUser != "" {
		client.Config.IsLocal = client.Config.ImageName == server.LocalUser
	}

	if _, found := server.ClientConfigs[client.ClientID]; !found {
		server.ClientConfigs[client.ClientID] = client.Config
	}

	clientCounter++

	logrus.Infof("NewClient (%d): User=%q, ClientVersion=%q", client.Idx, conn.User(), fmt.Sprintf("%x", conn.ClientVersion()))
	return &client
}
Ejemplo n.º 2
0
// Action is the default cli action to execute
func Action(c *cli.Context) {
	// Initialize the SSH server
	server, err := ssh2docker.NewServer()
	if err != nil {
		logrus.Fatalf("Cannot create server: %v", err)
	}

	// Restrict list of allowed images
	if c.String("allowed-images") != "" {
		server.AllowedImages = strings.Split(c.String("allowed-images"), ",")
	}

	// Configure server
	server.DefaultShell = c.String("shell")
	server.DockerRunArgs = strings.Split(c.String("docker-run-args"), " ")
	server.NoJoin = c.Bool("no-join")
	server.CleanOnStartup = c.Bool("clean-on-startup")
	server.PasswordAuthScript = c.String("password-auth-script")
	server.PublicKeyAuthScript = c.String("publickey-auth-script")
	server.LocalUser = c.String("local-user")
	server.Banner = c.String("banner")

	// Register the SSH host key
	hostKey := c.String("host-key")
	switch hostKey {
	case "built-in":
		hostKey = DefaultHostKey
	case "system":
		hostKey = "/etc/ssh/ssh_host_rsa_key"
	}
	err = server.AddHostKey(hostKey)
	if err != nil {
		logrus.Fatalf("Cannot add host key: %v", err)
	}

	// Bind TCP socket
	bindAddress := c.String("bind")
	listener, err := net.Listen("tcp", bindAddress)
	if err != nil {
		logrus.Fatalf("Failed to start listener on %q: %v", bindAddress, err)
	}
	logrus.Infof("Listening on %q", bindAddress)

	// Initialize server
	if err = server.Init(); err != nil {
		logrus.Fatalf("Failed to initialize the server: %v", err)
	}

	// Accept new clients
	for {
		conn, err := listener.Accept()
		if err != nil {
			logrus.Error("Accept failed: %v", err)
			continue
		}
		go server.Handle(conn)
	}
}