Skip to content

Commit 8e0e529

Browse files
committed
feature: read force-command from client certificate
1 parent 255075d commit 8e0e529

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

cmd/sshproxy/sshproxy.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/moby/term"
3535
"github.com/op/go-logging"
3636
"go.etcd.io/etcd/client/v3"
37+
"golang.org/x/crypto/ssh"
3738
)
3839

3940
var (
@@ -206,6 +207,31 @@ type ConnInfo struct {
206207
SSH *SSHInfo // SSH source and destination (from SSH_CONNECTION)
207208
}
208209

210+
// GetOriginalCommand returns the force-command included in the client ssh
211+
// certificate, if any. Otherwise, it returns the content of the environment
212+
// variable SSH_ORIGINAL_COMMAND. No error is returned. In case of any error,
213+
// the content of SSH_ORIGINAL_COMMAND will be returned.
214+
func getOriginalCommand() string {
215+
userAuthFile := os.Getenv("SSH_USER_AUTH")
216+
if userAuthFile != "" {
217+
content, err := os.ReadFile(userAuthFile)
218+
if err == nil {
219+
prefix := []byte("publickey ")
220+
key, found := bytes.CutPrefix(content, prefix)
221+
if found {
222+
out, comment, options, rest, err := ssh.ParseAuthorizedKey(key)
223+
if err == nil {
224+
fmt.Println("out: %v", out)
225+
fmt.Println("comment: %v", comment)
226+
fmt.Println("options: %v", options)
227+
fmt.Println("rest: %v", rest)
228+
}
229+
}
230+
}
231+
}
232+
return os.Getenv("SSH_ORIGINAL_COMMAND")
233+
}
234+
209235
func main() {
210236
os.Exit(mainExitCode())
211237
}
@@ -428,7 +454,7 @@ func mainExitCode() int {
428454
}
429455
}()
430456

431-
originalCmd := os.Getenv("SSH_ORIGINAL_COMMAND")
457+
originalCmd := getOriginalCommand()
432458
log.Debugf("original command = %s", originalCmd)
433459

434460
interactiveCommand := term.IsTerminal(os.Stdout.Fd())

0 commit comments

Comments
 (0)