FreeRDP's shadow server for Linux
=================================

Share this desktop's own screen with RDP clients: the kit's FreeRDP clients, FreeRDP Web,
mstsc. What the user sees is what the client sees. It shares the session that is already
there, like VNC or GNOME's own Desktop Sharing; it does not start sessions of its own.

  bin/freerdp-shadow-cli     the shadow server
  freerdp-shadow.service     a systemd user unit: starts it with your desktop session
  bin/winpr-hash             a line of a SAM file (not needed for the system's accounts)

Optional, off unless you install it (see "The session manager" below; it runs as root):

  bin/freerdp-shadow-sesman      one port for every user of this computer: each
                                 connection goes to the GNOME session of the user it names
  freerdp-shadow-sesman.service  its systemd system unit, on 127.0.0.1:3388

One server for two kinds of desktop; it takes, at start, the first that finds a monitor:

  Mutter   GNOME, on Wayland or X11, through Mutter's own screen-cast and remote-desktop
           interfaces on the session bus (no portal, no permission dialog): the picture
           through PipeWire, the keyboard and mouse through libei (Mutter 45 or newer,
           GNOME 45: older GNOME shows the picture but takes no input)
  X11      any other X11 desktop (Xfce, MATE, KDE on X11...), the display in $DISPLAY

/subsystem lists them, the one it would take marked with *; /subsystem:x11 forces X11.

Sound and microphone work on both, through PipeWire and WirePlumber in the session: what the
default sink plays goes to the client, a client's microphone appears as a source of the
session. No sound card is needed. The clipboard is shared on Mutter only: text, HTML and
images, no files. A selection made before the server starts shows up with the next copy.

It needs from the system: glibc 2.34 or newer, PAM, libsystemd, PipeWire's
libpipewire-0.3 and the X11 libraries (libX11, Xext, Xdamage, Xfixes, Xinerama, Xtst), which
a desktop has. Everything else (FreeRDP, OpenSSL, the AV1, AV2 and Opus codecs, libei, libpng)
is inside. MANIFEST lists the sources and what the server takes from the system.

Install
-------

  tar -xJf freerdp-host-*-linux-$(uname -m).tar.xz
  mkdir -p ~/.local/opt ~/.config/systemd/user
  rm -rf ~/.local/opt/freerdp-host   # an earlier version, if any (your settings stay)
  mv freerdp-host-*-linux-$(uname -m) ~/.local/opt/freerdp-host
  cp ~/.local/opt/freerdp-host/freerdp-shadow.service ~/.config/systemd/user/
  systemctl --user daemon-reload
  systemctl --user enable --now freerdp-shadow.service

Its log is the unit's journal: journalctl --user -u freerdp-shadow (-f to follow it) shows
what it shares and who logs in. Where the journal is kept in memory only (no
/var/log/journal, as on some distributions), journald keeps no file of your own and
journalctl --user may show nothing: read it as root (sudo journalctl -t freerdp-shadow-cli),
or keep the journal on disk (Storage=persistent in /etc/systemd/journald.conf, then
sudo systemctl restart systemd-journald). It starts with the desktop session (GNOME
starts graphical-session.target) and stops with it. A desktop that does not start that target
(Xfce, MATE...) gets it from its autostart instead, with the display:

  systemctl --user import-environment DISPLAY XAUTHORITY && systemctl --user start freerdp-shadow

GNOME shows its screen-sharing indicator in the top bar while the screen is shared. Stopping
the sharing there ends the server, with exit status 0, so Restart=on-failure does not start
it again: systemctl --user start freerdp-shadow when you want it back. A crash does restart it.

AV2 is on: the clients that offer it (the kit's FreeRDP clients and FreeRDP Web) get AV2,
the others AV1; AV2 costs this computer more processor time (libavm, AV2's reference
encoder). AV1 only: in the unit's ExecStart, change /gfx-av2 to /gfx-av2:off (AV2 is
on by default since FreeRDP 20261110.3), then
systemctl --user daemon-reload && systemctl --user restart freerdp-shadow. There is no H.264
encoder in it: a client without AV1 or AV2 (Windows' own Remote Desktop client, most
others) gets RemoteFX or the older bitmap codecs.

It listens on 3388, the kit's shadow port on every OS: 3389 stays free for the system's own
RDP servers (GNOME's Remote Desktop or Desktop Sharing, gnome-remote-desktop; xrdp). The
session manager below takes 3388 too: install one or the other, or give the second a
/port:<another> (never 3389).

Remove it with

  systemctl --user disable --now freerdp-shadow.service
  rm ~/.config/systemd/user/freerdp-shadow.service
  rm -r ~/.local/opt/freerdp-host

The size of the picture
-----------------------

With X11 it shares the display as it is, at its own size: a client's window shows it,
scaled if the window is smaller, and a client's /dynamic-resolution, /multimon layout and
HiDPI scale factors change nothing (no Display Control; the client logs no "HiDPI:" line).
With Mutter (GNOME) the server takes the client's window size through Display Control.
For a session that follows the client's windows and monitors, HiDPI included, use a server
that starts sessions of its own: xrdp, the session manager below (a headless GNOME per
user), or Windows' own Remote Desktop.

Connect
-------

The login is the account whose desktop it is (your own), checked by the system's PAM over TLS
security (/sec:tls in the unit; keep it there). Another account is refused: it would see
your screen. GNOME (Mutter) asks PAM's service freerdp-shadow when /etc/pam.d/freerdp-shadow
exists, else login. Keep the server on 127.0.0.1 as the unit has it, and reach it through SSH:

  ssh -L 13388:127.0.0.1:3388 <user>@<this computer>

then connect a client to 127.0.0.1:13388. FreeRDP Web: start a WebSocket gateway on this
computer (websockify 127.0.0.1:3390 127.0.0.1:3388) and forward 3390 instead.

The server makes its own TLS certificate on its first start, in ~/.config/freerdp/shadow/.

The session manager (optional, runs as root)
--------------------------------------------

freerdp-shadow-sesman lets every user of this computer reach their own GNOME session on one
port, without installing the user unit above: it runs as root, as a system service, on
127.0.0.1:3388, the same port as the user unit (install one or the other, or give the second
a port of its own, never 3389). For each
connection it reads the client's first packet and nothing else, takes the user it names
(the client's user name, "Cookie: mstshash=<user>"), finds that user's GNOME Shell, starts
the shadow server in the user's own systemd manager if it is not running yet (the unit
freerdp-sesman-shadow, on a socket in /run/user/<uid>), drops to the user and hands the
connection over. It never sees a password: TLS and the login (PAM, the session's owner only)
happen in that shadow server, as with the user unit. A user with no GNOME session gets a
headless one if they are in the group freerdp-headless (below), and is refused otherwise.

Install it as root, in /opt/freerdp-host (a root service must not run programs a user can
change, so not from ~/.local):

  sudo rm -rf /opt/freerdp-host   # an earlier version, if any
  sudo tar -xJf freerdp-host-*-linux-$(uname -m).tar.xz -C /opt
  sudo mv /opt/freerdp-host-*-linux-$(uname -m) /opt/freerdp-host
  sudo chown -R root:root /opt/freerdp-host
  sudo cp /opt/freerdp-host/freerdp-shadow-sesman.service /etc/systemd/system/
  sudo systemctl daemon-reload
  sudo systemctl enable --now freerdp-shadow-sesman.service

A session the manager starts has no seat, so polkit takes it for a remote one, and GNOME Software
would bring administrator dialogs into it. So the manager closes both ways it starts, for its own
sessions only: it is out of the autostart, and gnome-software.service is masked until the user's
manager ends. So it does not open from the menu or show up in an Activities search.
`gnome-software` in a terminal still starts it, and it then asks like any other program. A session
already running keeps what it started with until it ends.

A login with the right password unlocks a locked session the session manager started; it has no
real screen. A GNOME on a real screen stays locked.

If an earlier version's install step added a polkit rule, remove it:

  sudo rm -f /etc/polkit-1/rules.d/49-freerdp-shadow-sesman.rules

Connect as with the user unit, through SSH, to port 3388:

  ssh -L 13388:127.0.0.1:3388 <user>@<this computer>

then a client to 127.0.0.1:13388 with your own user name and password. Its log:
sudo journalctl -u freerdp-shadow-sesman (who connects, to whose session); the shadow
server's own lines are in your journal, journalctl --user -u freerdp-sesman-shadow.

Headless sessions: a user of the group freerdp-headless who has no GNOME session gets a
headless GNOME Shell, with a monitor of its own (1920x1080; --headless-size in the unit's
ExecStart changes it), started by the first connection that names them. The group does not
exist until you make it, so this is off until then:

  sudo groupadd -f freerdp-headless
  sudo usermod -aG freerdp-headless <user>

The first connection waits for GNOME and the shadow server to start (about half a minute in
our tests, two at most; a client that gives up
earlier connects on its next try). The session keeps running when the client leaves, so the
next connection finds the same desktop, until the user logs out in it or
sudo systemctl stop freerdp-sesman-gnome-<user>. It is started before the login (the login
happens in the shadow server, once GNOME is up): anyone who reaches the port can make it
start for a member of the group, so keep the port on 127.0.0.1 (reached over SSH, as the unit
has it) or a private network. Log out of a headless session before logging in on this
computer's own screen as the same user: two GNOME sessions of one user at once are not
supported.

Remove it with

  sudo systemctl disable --now freerdp-shadow-sesman.service
  sudo rm /etc/systemd/system/freerdp-shadow-sesman.service
  sudo rm -r /opt/freerdp-host

The camera
----------

A client's camera becomes a camera of this computer, named "FreeRDP Camera", that any
app here can use (a browser, a video call). The camera device is v4l2loopback, a kernel module
you install from your distribution; this package does not ship it. Without it the server says so
in one log line and works as before.

Install the module, then load it with the camera's name:

  Debian, Ubuntu:  sudo apt install v4l2loopback-dkms
  Fedora:          sudo dnf install akmod-v4l2loopback
                   (RPM Fusion "free", a third-party repository: enable it first, https://rpmfusion.org/Configuration)
  Arch:            sudo pacman -S v4l2loopback-dkms linux-headers

  sudo modprobe v4l2loopback exclusive_caps=1 card_label="FreeRDP Camera"

To load it at every boot:

  echo v4l2loopback | sudo tee /etc/modules-load.d/freerdp-camera.conf
  echo 'options v4l2loopback exclusive_caps=1 card_label="FreeRDP Camera"' |
      sudo tee /etc/modprobe.d/freerdp-camera.conf

The server uses only a v4l2loopback device named "FreeRDP Camera" (the card_label above), never
another one, so a camera of OBS or another tool is never touched. To use a different device, set
FREERDP_SHADOW_CAMERA_SINK=v4l2:/dev/videoN.

One client's camera at a time: the first connected client that shares one. A view-only client
can't share its camera.

exclusive_caps=1 is what browsers need (Chrome lists the camera only then). The server
writes the camera from its start; it shows a still (a camera with a slash) until a client's
camera feeds it. With v4l2loopback 0.13 or newer the client's camera runs only while an app here
uses the camera; an older one (Ubuntu 24.04 has 0.12.7) cannot tell, and the client's camera then
runs while the client is connected (the server says so in one log line).

Secure Boot: the DKMS (Debian, Ubuntu, Arch) and akmods (Fedora) builds sign the module with
a key of this machine (a MOK), which the firmware must trust before the module loads. Enrol it
once, then reboot and confirm it in the blue MokManager screen:

  Debian:  sudo mokutil --import /var/lib/dkms/mok.pub
  Ubuntu:  sudo mokutil --import /var/lib/shim-signed/mok/MOK.der
  Fedora:  sudo kmodgenca -a && sudo mokutil --import /etc/pki/akmods/certs/public_key.der

The server needs to write the device: your user in the group "video"
(sudo usermod -aG video "$USER", then log in again) where the distribution does not grant it.

What it cannot do
-----------------

It runs in your session, after you have logged in: it cannot show the login screen. Before
anyone logs in, only the session manager's headless sessions reach this computer. A Wayland
desktop other than GNOME (KDE Plasma, sway) has no subsystem here yet; on X11 they work
through the X11 one (not through the session manager, which finds GNOME sessions only).

What is in it: MANIFEST (the sources and their versions), licenses/.
More: https://github.com/congzhangzh/rdp-release-kit
