Skip to content

Commit a68ab30

Browse files
Serhii PonomarovSerhii Ponomarov
authored andcommitted
Improve documentation
1 parent f9204fa commit a68ab30

File tree

4 files changed

+49
-36
lines changed

4 files changed

+49
-36
lines changed

README.md

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,72 @@ A lightweight SSH server frontend where authentication and connections
77
are controlled with command handlers / shell scripts.
88

99
## Using sshfront
10-
```
11-
Usage: ./sshfront [options] <handler>
1210

13-
-a="": authentication hook. empty=allow all
14-
-d=false: debug mode
15-
-e=false: pass environment to handler
16-
-h="0.0.0.0": ip to listen on
17-
-k="~/.ssh/id_rsa": private host key path
18-
-p="22": port to listen on
19-
```
11+
12+
Usage: ./sshfront [options] <handler>
13+
14+
-a="": authentication hook. empty=allow all
15+
-d=false: debug mode
16+
-e=false: pass environment to handler
17+
-h="0.0.0.0": ip to listen on
18+
-k="~/.ssh/id_rsa": private host key path
19+
-p="22": port to listen on
2020

2121

2222
#### handler $command...
2323

24-
* `$command...` command line arguments specified to run by the SSH client
24+
`$command...` command line arguments specified to run by the SSH client
2525

26-
The handler is a command that's used to handle all SSH connections. Output, stderr, and the exit code is returned to the client. If the client provides stdin, that's passed to the handler.
26+
The handler is a command that's used to handle all SSH connections.
27+
Output, stderr, and the exit code is returned to the client.
28+
If the client provides stdin, that's passed to the handler.
2729

28-
If the authentication hook was specified, any output is parsed as environment variables and added to the handler environment. `$USER` is always the SSH user used to connect and `$SSH_ORIGINAL_COMMAND` is the command specified from the client if not interactive.
30+
If the authentication hook was specified, any output is parsed as environment variables and added to the handler environment.
31+
`$USER` is always the SSH user used to connect and `$SSH_ORIGINAL_COMMAND` is the command specified from the client if not interactive.
2932

3033
#### auth-hook $user $key
3134

3235
* `$user` argument is the name of the user being used to attempt the connection
3336
* `$key` argument is the public key data being provided for authentication
3437

35-
The auth hook is a command used for authenticating incoming SSH connections. If it returns with exit status 0, the connection will be allowed, otherwise it will be denied. The output of auth hook must be empty, or key-value pairs in the form `KEY=value` separated by newlines, which will be added to the environment of connection handler.
38+
The auth hook is a command used for authenticating incoming SSH connections.
39+
If it returns with exit status 0, the connection will be allowed, otherwise it will be denied.
40+
The output of auth hook must be empty, or key-value pairs in the form `KEY=value` separated by newlines, which will be added to the environment of connection handler.
3641

3742
The auth hook is optional, but if not specified then all connections are allowed.
3843
It is a good idea to always specify an auth hook.
3944

45+
46+
See example/authcheck auth hook that checks that the pub key is authorized. Usage:
47+
48+
sshfront -a example/authcheck
49+
50+
4051
## Examples
4152

4253
**Many of these bypass authentication and may allow remote execution, *do not* run this in production.**
4354

4455
Echo server:
4556

46-
```
47-
server$ sshfront $(which echo)
48-
client$ ssh $SERVER "hello world"
49-
hello world
50-
```
57+
server$ sshfront $(which echo)
58+
client$ ssh $SERVER "hello world"
59+
hello world
5160

5261
Echo host's environment to clients:
5362

54-
```
55-
server$ sshfront -e $(env)
56-
client$ ssh $SERVER
57-
USER=root
58-
HOME=/root
59-
LANG=en_US.UTF-8
60-
...
61-
```
63+
server$ sshfront -e $(env)
64+
client$ ssh $SERVER
65+
USER=root
66+
HOME=/root
67+
LANG=en_US.UTF-8
68+
...
6269

6370
Bash server:
6471

65-
```
66-
server$ sshfront $(which bash)
67-
client$ ssh $SERVER
68-
bash-4.3$ echo "this is a bash instance running on the server"
69-
this is a bash instance running on the server
70-
```
72+
server$ sshfront $(which bash)
73+
client$ ssh $SERVER
74+
bash-4.3$ echo "this is a bash instance running on the server"
75+
this is a bash instance running on the server
7176

7277

7378
## Sponsors

example/authcheck

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#!/bin/bash
2+
# check that the user's pubkey is authorized
23
grep "$2" "/home/$1/.ssh/authorized_keys" > /dev/null 2>&1

example/helloworld

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/bash
2-
echo "Hello world: $@"
2+
echo "Execute command: $@"
3+
echo "Env vars:"
34
env
5+
# if FD = 1 then this is an interactive shell
46
if [[ -t 1 ]]; then
57
exec bash
68
fi

internal/handlers.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func HandleAuth(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, err
6767
}, nil
6868
}
6969

70-
keydata := string(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key)))
71-
cmd, err := handlerCmd(*AuthHook, conn.User(), keydata)
70+
pubKey := string(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key)))
71+
cmd, err := handlerCmd(*AuthHook, conn.User(), pubKey)
7272
if err != nil {
7373
return nil, err
7474
}
@@ -192,6 +192,8 @@ func (h *sshHandler) handleEnv(req *ssh.Request) {
192192
req.Reply(true, nil)
193193
}
194194

195+
// handleExec when executed a command e.g.
196+
// ssh user@localhost -p 2222 'echo Hello'
195197
func (h *sshHandler) handleExec(req *ssh.Request) {
196198
h.Lock()
197199
defer h.Unlock()
@@ -234,6 +236,8 @@ func (h *sshHandler) handleExec(req *ssh.Request) {
234236
h.Exit(cmd.Run())
235237
}
236238

239+
// handlePty when executed a command e.g.
240+
// ssh user@localhost -p 2222
237241
func (h *sshHandler) handlePty(req *ssh.Request) {
238242
h.Lock()
239243
defer h.Unlock()
@@ -246,7 +250,8 @@ func (h *sshHandler) handlePty(req *ssh.Request) {
246250

247251
width, height, okSize := parsePtyRequest(req.Payload)
248252

249-
cmd, err := handlerCmd(flag.Arg(0))
253+
scriptName := flag.Arg(0)
254+
cmd, err := handlerCmd(scriptName)
250255
if err != nil {
251256
Debug("failed handler init:", err)
252257
h.channel.Close()

0 commit comments

Comments
 (0)