@@ -3,7 +3,9 @@ package ssh2docker
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
+ "os"
6
7
"os/exec"
8
+ "strings"
7
9
8
10
"github.com/moul/ssh2docker/vendor/github.com/Sirupsen/logrus"
9
11
"github.com/moul/ssh2docker/vendor/golang.org/x/crypto/ssh"
@@ -37,7 +39,7 @@ func (s *Server) CheckConfig(config *ClientConfig) error {
37
39
func (s * Server ) PublicKeyCallback (conn ssh.ConnMetadata , key ssh.PublicKey ) (* ssh.Permissions , error ) {
38
40
username := conn .User ()
39
41
clientID := conn .RemoteAddr ().String ()
40
- keyText := string (ssh .MarshalAuthorizedKey (key ))
42
+ keyText := strings . TrimSpace ( string (ssh .MarshalAuthorizedKey (key ) ))
41
43
logrus .Debugf ("PublicKeyCallback: %q %q" , username , keyText )
42
44
// sessionID := conn.SessionID()
43
45
@@ -63,17 +65,44 @@ func (s *Server) KeyboardInteractiveCallback(conn ssh.ConnMetadata, challenge ss
63
65
64
66
config := s .ClientConfigs [clientID ]
65
67
if config == nil {
66
- config : = & ClientConfig {
68
+ s . ClientConfigs [ clientID ] = & ClientConfig {
67
69
RemoteUser : username ,
68
70
ImageName : username ,
71
+ Keys : []string {},
69
72
Env : make (Environment , 0 ),
70
73
}
71
- s .ClientConfigs [clientID ] = config
72
74
}
75
+ config = s .ClientConfigs [clientID ]
73
76
74
- if len (config .Keys ) > 0 {
75
- logrus .Debugf ("%d keys received, trying to authenticate" )
76
- // FIXME: authenticate here
77
+ if len (config .Keys ) == 0 {
78
+ logrus .Warnf ("No user keys, continuing with password authentication" )
79
+ return nil , s .CheckConfig (config )
80
+ }
81
+
82
+ if s .PublicKeyAuthScript != "" {
83
+ logrus .Debugf ("%d keys received, trying to authenticate using hook script" , len (config .Keys ))
84
+ script , err := expandUser (s .PublicKeyAuthScript )
85
+ if err != nil {
86
+ logrus .Warnf ("Failed to expandUser: %v" , err )
87
+ return nil , err
88
+ }
89
+ args := append ([]string {username }, config .Keys ... )
90
+ cmd := exec .Command (script , args ... )
91
+ // FIXME: redirect stderr to logrus
92
+ cmd .Stderr = os .Stderr
93
+ output , err := cmd .Output ()
94
+ if err != nil {
95
+ logrus .Warnf ("Failed to execute publickey-auth-script: %v" , err )
96
+ return nil , err
97
+ }
98
+
99
+ err = json .Unmarshal (output , & config )
100
+ if err != nil {
101
+ logrus .Warnf ("Failed to unmarshal json %q: %v" , string (output ), err )
102
+ return nil , err
103
+ }
104
+ } else {
105
+ logrus .Debugf ("%d keys received, but no hook script, continuing" , len (config .Keys ))
77
106
}
78
107
79
108
return nil , s .CheckConfig (config )
@@ -88,24 +117,26 @@ func (s *Server) PasswordCallback(conn ssh.ConnMetadata, password []byte) (*ssh.
88
117
89
118
config := s .ClientConfigs [clientID ]
90
119
if config == nil {
91
- config : = & ClientConfig {
120
+ s . ClientConfigs [ clientID ] = & ClientConfig {
92
121
//Allowed: true,
93
122
RemoteUser : username ,
94
123
ImageName : username ,
124
+ Keys : []string {},
95
125
Env : make (Environment , 0 ),
96
126
}
97
- s .ClientConfigs [clientID ] = config
98
127
}
128
+ config = s .ClientConfigs [clientID ]
99
129
100
130
if s .PasswordAuthScript != "" {
101
- // Using a hook script
102
131
script , err := expandUser (s .PasswordAuthScript )
103
132
if err != nil {
104
133
logrus .Warnf ("Failed to expandUser: %v" , err )
105
134
return nil , err
106
135
}
107
136
cmd := exec .Command (script , username , string (password ))
108
- output , err := cmd .CombinedOutput ()
137
+ // FIXME: redirect stderr to logrus
138
+ cmd .Stderr = os .Stderr
139
+ output , err := cmd .Output ()
109
140
if err != nil {
110
141
logrus .Warnf ("Failed to execute password-auth-script: %v" , err )
111
142
return nil , err
0 commit comments