[Bug 3609] New: Support the special ${RANDOM} environment variable

bugzilla-daemon at mindrot.org
Wed, 30 Aug 2023 21:52:14 -0000

Bug ID: 3609
           Summary: Support the special ${RANDOM} environment variable
           Product: Portable OpenSSH
           Version: 9.4p1
          Hardware: Other
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P5
         Component: ssh
          Assignee: unassigned-bugs at mindrot.org
          Reporter: erjoalgo at gmail.com

Created attachment 3727
  --> https://bugzilla.mindrot.org/attachment.cgi?id=3727&action=edit
Patch to implement ${RANDOM} support

I use the RemoteForward keyword in my sshd_config file to create a unix
socket
on the remote host, I personally use this socket to map to a local
program
that can receive GUI-related commands, e.g. set clipboard, open a URL
in a browser,
etc, from any commands executed on the remote server. It looks
something like this:


Match host *
    SendEnv DESKTOP_GROUP_NUMBER
    RemoteForward /tmp/.x-service-debian-x1-7th.sock localhost:1959


Sometimes I have multiple ssh connections to the same machine, e.g.
using
multiple desktops for different areas of work, and the RemoteForward
request will fail because the remote socket has a fixed path and will
already be taken. So I need to make the RemoteForward unix socket path
to
be unique for each ssh connection.

I looked through man ssh_config and noticed the TOKENS section, but
they all
appear to be deterministic. I also noticed that RemoteForward
supports environment variables and tried to use the ${RANDOM}
environment variable, but this failed because ${RANDOM} is not a real
environment variable but rather a bash feature.

So I implemented a feature to support a bash-like ${RANDOM} environment
variable
when expanding tokens and environment variables. This can be very
useful for
generating connection-unique strings, and usage should be
straightforward. My RemoteForward directive now looks like:


Match host *
    SendEnv DESKTOP_GROUP_NUMBER
    RemoteForward /tmp/.x-service-debian-x1-7th-${RANDOM}.sock
localhost:1959


I've tested and this works as expected. I have attached the patch, I
hope it can be merged, please let me know if you have any suggestions.

Thanks,

Ernesto