# Sharing Folders

---

# File Sharing

_Peios / Using Peios / Sharing Folders_

> How Peios shares folders with other computers over SMB, how clients sign in, and how network access and file permissions decide what each person can do.

Peios can share folders with Windows, Mac and Linux computers on your
network. People reach a shared folder from their own computer's file manager
and work with its files as if they were local, signed in with an account on
the Peios machine. If you have used file sharing on Windows, this will be
familiar: Peios speaks the same protocol, SMB, and uses the same model of
shares, share permissions, and file permissions.

The file server is **fservd**, a service that peinit runs. You manage it with
the **File Sharing** window or the `fserv` command.

## Shares

A **share** offers one folder on this machine under a name, such as
`Projects` for `/srv/projects`. A Windows user reaches it as
`\\peios-box\Projects`, and a Mac or Linux user as
`smb://peios-box/Projects`, where `peios-box` is this machine's name on the
network.

A share is defined once and switched on for each protocol it is served
over. SMB is the only protocol so far, and a new share is served over it
unless you say otherwise. Removing a share stops offering the folder; the
folder and its files stay exactly where they are.

Shares live in the registry under `Machine\Software\Fservd\Shares`. fservd
watches the key, so a change applies to new connections within a few
seconds, without restarting anything. People already connected keep the
share they connected to until they disconnect.

Browsing to the server, such as opening `\\peios-box` in File Explorer,
lists the shares the person browsing can use: fservd tries each one as
them, so nobody sees the name of a share they can't open. An administrator
can list every share to everyone instead, with **Show every share** in the
File Sharing window or `fserv listing all`. A share whose name ends in `$`
is never listed, but people who know its address can still open it.

## Signing in

People who connect sign in with an account this machine knows, such as a
local account. The client proves it knows the account's password with
NTLMv2, and fservd passes that proof to authd, which checks it against the
account. fservd never sees the password. A successful sign-in creates a
logon session for the person, as signing in at the console does, except
that its logon type is Network.

fservd doesn't accept guest sign-ins, or the older LM and NTLMv1 proofs.
Every signed-in session is signed, so a message changed on its way across
the network is refused. Traffic is not encrypted, so share sensitive files
only over networks you trust.

A client can sign its session in again without disconnecting, as Windows
does on long-lived connections, and may switch to another account as it
does. Files already open stay open with the access they were granted;
everything after that is done as the new account. When a client reconnects
after its network drops, it names its earlier session, and fservd closes
that session's files first if it belongs to the same account, so nobody is
locked out of their own files by a connection that is already gone.

## Anonymous connections

Some devices, such as scanners and media players, connect without signing
in. Every share refuses them unless you turn on **Allow anonymous** for it,
in the share's **Network Access** in the File Sharing window, or with
`fserv share set <name> --anonymous on`. Browsing the server anonymously
shows only the shares that allow it.

An anonymous connection acts as the Anonymous identity. It belongs to
Everyone but not to Authenticated Users, and has Untrusted integrity, so
it can use only files and shares that grant Everyone access, and it can't
change anything labelled above Untrusted, which by default is everything.
The **Read-only for everyone signed in** and **Administrators can change;
others read** restrictions refuse it, since they grant signed-in people
only. An anonymous session's messages are signed only if the device asks,
and then with keys anyone can work out, so a message changed in transit
isn't reliably detected.

## Who can do what

fservd acts as each signed-in person for every file operation: when someone
opens a file through a share, the kernel checks the file's own permissions
against that person's token, exactly as it would for a program they ran on
this machine. A file someone couldn't read at the console, they can't read
through a share either.

A share can also have its own **network access**, a security descriptor
that limits what people can do through that share. When someone connects,
fservd checks the share's network access; every file they then open is
limited to what it granted. Network access only ever narrows the files' own
permissions. It can make a share read-only for everyone, but it can't let
anyone change a file whose permissions refuse them.

A share with no network access of its own leaves the decision to the files'
permissions alone. That is the default, and it is usually what you want:
the folder's permissions already say who can use the files. Add network
access when the network should be more restricted than this machine, as
described in [Restricting Network Access](/peios/using-peios/sharing-folders/restricting-network-access.md).

## Files in use

When a Windows program opens a file it can say what others can do with the
file while it has it open: read it, change it, or delete it. On Peios the
kernel keeps those rules for everyone, so they apply to programs on this
machine as well as to other people on the network. A program here that tries
to change or delete a file someone has open without allowing it gets a
"device or resource busy" error until they close it.

Deleting works as it does on Windows: a file deleted while others have it
open keeps its name until the last of them closes it, and can't be opened
again meanwhile.

## File attributes

Windows gives files attributes such as read-only and hidden. Through a
share they work like this:

- **Read-only** is a Peios attribute too, so it sticks. A read-only file
  can't be changed or deleted, by anyone, on the network or on this machine,
  until the attribute is cleared; `attrib -r` clears it here.
- **Hidden** follows the Linux convention: a file or folder whose name
  starts with a dot is shown as hidden. Marking another file hidden from
  Windows has no lasting effect.
- **Created** is the file's creation date. Copying a file onto a share
  keeps its original date, and Windows can change it. Peios also keeps the
  date the file system created it, which nothing can change.
- **Archive** always shows as set on files, and changes to it aren't kept.
  A backup that copies only files with Archive set, such as `robocopy /M`
  or `xcopy /M`, copies every file each time.
- **Not indexed** is kept as the file's PGSS Indexing preference, the
  `user.pgss.indexing` attribute that `attrib +i` sets here: setting it
  from Windows keeps the file's contents out of search, and clearing it
  removes the preference. A folder's preference covers everything inside
  it, but Windows shows it only on the folder itself. On a file system
  that can't keep it, it's accepted and not kept.
- **Temporary** is accepted from Windows and not kept.
- **Extended attributes**, the name and value pairs some Windows programs
  attach to files, are kept as Linux extended attributes named `user.`
  followed by the name in capitals, so `getfattr` shows them here. Linux
  programs' own `user.` attributes appear to Windows the same way, except
  `user.peios.` ones, which Peios reserves, and the indexing preference,
  which appears as **not indexed**. Peios standards' `user.pgss.`
  attributes, such as a file's icon, keep their lower-case names when
  Windows writes them back.

## What is recorded

fservd records an event when someone sets up a session, connects to a
share, signs in again, or their session ends, and when its configuration is
loaded. Each
records the client's address and the person's security identifier.
`fservd.session.started` carries the logon session that authd created, so
you can join it to authd's `authd.logon.attempted` record and to the
kernel's audit records of the files that person opened. Run
`evman fservd.share.connected` to see what each event holds, and see
[Diagnosing File Sharing](/peios/using-peios/sharing-folders/diagnosing-file-sharing.md)
for queries.

For the command and its options, read `fserv(8)`; for the service, read
`fservd(8)`. Every registry value is documented by regman: run
`regman 'Machine\Software\Fservd'`.

---

# Sharing a Folder

_Peios / Using Peios / Sharing Folders_

> Share a folder with other computers on the network, with the File Sharing window or the fserv command, and check that it is reachable.

Share a folder when people on other computers need to open and save its
files. When you finish, the folder is reachable from Windows, Mac and Linux
computers by an address such as `\\peios-box\Projects`.

## Before you start

- Sign in as a member of Administrators. Only Administrators can change
  shares.
- Make sure the folder exists, and that its permissions let the people who
  will use it open its files. Sharing a folder doesn't change its
  permissions: [File Sharing](/peios/using-peios/sharing-folders/file-sharing.md) explains
  why.
- Each person who connects needs an account on this machine.

## Share a folder with the File Sharing window

1. Open **File Sharing** from the launcher. From a terminal, `fserv --window`
   opens the same window.
2. Choose **Share a folder** in the sidebar.
3. In **Folder**, type the folder's full path, such as `/srv/projects`, or
   choose **Browse…** to pick it.
4. In **Name**, keep the suggested name or type another. The name is part
   of the address people type, so a short name without spaces is easiest.
5. Leave **Network access** set to **No network restriction**, unless the
   share should be more restricted than the folder's own permissions.
6. Choose **Share Folder**.

The new share's page shows its addresses under **Connect**, and the file
server's state is on the **Service** page. If the file server is stopped,
choose **Start**, and turn on **Start when Peios starts** so the share is
available after every restart.

## Share a folder with the fserv command

1. Add the share:

   ```sh
   fserv share add Projects /srv/projects
   ```

2. If the file server isn't running, start it, and make it start at boot:

   ```sh
   fserv service start
   fserv service boot on
   ```

`fserv share add` refuses a folder that doesn't exist, a name another share
already has, or a name with characters that SMB doesn't allow, such as `/`
or `:`. Read `fserv(8)` for every option.

## Check the result

Run `fserv share show` with the share's name:

```sh
fserv share show Projects
```

The output includes `SMB on`, the access, and the addresses to connect to:

```text
Share           Projects
Folder          /srv/projects
SMB             on
Access          No network restriction
Windows path    \\peios-box\Projects
Mac/Linux       smb://peios-box/Projects
```

`fserv status` shows whether the file server is running. Then connect from
another computer, as described in
[Connecting to a Share](/peios/using-peios/sharing-folders/connecting-to-a-share.md).

## Stop sharing a folder

On the share's page in File Sharing, choose **Stop Sharing…**, or run:

```sh
fserv share remove Projects
```

The folder and its files stay where they are. People already connected
keep the share until they disconnect.

---

# Connecting to a Share

_Peios / Using Peios / Sharing Folders_

> Open a Peios share from Windows, a Mac, or Linux, and sign in with an account on the Peios machine.

Connect to a share to open and save its files from another computer. You
sign in with your account on the Peios machine, not the account on the
computer you are using.

## Before you start

- Get the share's address from its administrator. It looks like
  `\\peios-box\Projects` for Windows, or `smb://peios-box/Projects` for a
  Mac or Linux. Or browse to the server, `\\peios-box`, to see the shares
  you can use.
- Know your account name and password on the Peios machine.
- Your computer must support SMB 3.1.1. Windows 10 and later, current
  versions of macOS, and Linux with a recent kernel or Samba do.
- If your computer can't find the Peios machine by name, use its IP
  address in place of the name, as in `\\192.0.2.10\Projects`.

## Connect from Windows

1. Open File Explorer.
2. Type the share's address, such as `\\peios-box\Projects`, in the address
   bar, and press Enter. To choose from the shares you can use, type the
   server's address, `\\peios-box`, instead.
3. When Windows asks for network credentials, type your account name and
   password on the Peios machine.

To give the share a drive letter, choose **Map network drive** from the
**This PC** menu, or run this in a terminal, replacing `alice` with your
account name:

```text
net use Z: \\peios-box\Projects /user:alice
```

Windows asks for the password, then shows the share as drive `Z:`.

## Connect from a Mac

1. In Finder, choose **Go › Connect to Server**.
2. Type the share's address, such as `smb://peios-box/Projects`, and choose
   **Connect**.
3. Choose **Registered User**, and type your account name and password on
   the Peios machine.

## Connect from Linux

Most Linux file managers connect to an `smb://` address. In GNOME Files,
choose **Other Locations**, type `smb://peios-box/Projects` in **Connect to
Server**, and sign in with your account name and password on the Peios
machine. Leave the domain as it is: the Peios machine ignores it.

## Check the result

The share's folder opens, and you can open its files. What you can do with
each file depends on its permissions on the Peios machine, and on any
network restriction the share has.

If the connection is refused, see
[Diagnosing File Sharing](/peios/using-peios/sharing-folders/diagnosing-file-sharing.md).

---

# Restricting Network Access

_Peios / Using Peios / Sharing Folders_

> Limit what people can do through a share, whatever the files' own permissions allow, with a preset or a list of people and groups.

Restrict a share's network access when people should be able to do less
over the network than on this machine, such as only reading files they could
change at the console. A share's network access only ever narrows the files'
own permissions; to let someone use files they can't use now, change the
files' permissions instead.

## Before you start

- Sign in as a member of Administrators.
- Decide whether a preset fits. Most needs are one of these:
  - **No network restriction**: each file's own permissions decide. This
    is the default.
  - **Read-only for everyone signed in**: nobody can change files through
    the share.
  - **Administrators can change; others read**: only Administrators can
    change files through the share.

  Both restrictions grant signed-in people only, so they refuse anonymous
  connections even on a share that allows them.

## Choose a preset

In File Sharing, open the share's page, and choose **Use** beside the
preset under **Network Access**. Or run `fserv share set` with the preset's
name, `open`, `read-only` or `admins-change`:

```sh
fserv share set Projects --access read-only
```

## Choose specific people and groups

1. In File Sharing, open the share's page.
2. Under **Network Access**, choose **Choose…** beside **Specific people
   and groups**.
3. Choose **Add…** to add the people and groups who can use the share, and
   give each **Read**, **Change**, or **Full control**. Remove the entries
   for anyone who shouldn't use it, including **Authenticated Users**,
   which a share without a restriction starts with. To admit anonymous
   connections on a share that allows them, grant **Everyone** instead.
4. Choose **OK**.

From the command line, give the share a security descriptor in SDDL. This
one lets Administrators change files and every signed-in person read them:

```sh
fserv share set Projects --security 'O:BAG:BAD:(A;;0x1301bf;;;BA)(A;;0x1200a9;;;AU)'
```

The rights are read (`0x1200A9`), change (`0x1301BF`), and full control
(`0x1F01FF`). To name one group, put its security identifier in place of
`AU`.

## Check the result

Run `fserv share show Projects`. **Access** names the preset in use, or says
`Custom network permissions`. The change applies to new connections within
a few seconds; anyone already connected keeps the access they connected
with until they disconnect.

Connect as someone the share should restrict, and try the change it
should refuse. A share that grants someone nothing refuses their connection
outright, and fservd records `fservd.share.connected` with
`access-denied`.

---

# Diagnosing File Sharing

_Peios / Using Peios / Sharing Folders_

> Find out why someone can't connect to a share or use its files, from fservd's events, authd's sign-in records, and the service log.

Diagnose a file-sharing problem when someone can't reach a share, can't sign
in, or can't open or change its files. fservd records each step of a
connection as an event, so the records usually say which step failed and
why.

## Before you start

- Sign in as a member of Administrators, so you can read the events and the
  file server's log.
- Note the time of the failure, and the address of the computer that
  failed, if you know it.

## Check the file server

Run `fserv status`. **Service** should be `active`, and **Shares** should
count the share as on SMB. If the service is stopped, start it with
`fserv service start`. A warning such as
`fserv: warning: share Old: Path is missing` names a share that can't be
used; fix it with `fserv share set`, or remove it.

If a client can't connect at all and fservd records no
`fservd.session.started`, check that the firewall admits SMB: `net policy`
should show the `smb` flow rule, which passes inbound TCP port 445. fservd's
package adds it; a rule an administrator wrote that refuses the port, or a
missing rule, stops every connection before it reaches fservd.

## Read the connection's events

List the last hour of fservd's events:

```sh
evctl 'EVENTS fservd.* SINCE 1h ago TAKE 50'
```

A connection produces `fservd.session.started` when the person signs in,
then `fservd.share.connected` for each share they open. Each record has
the client's address in `source.address`. Show only failures:

```sh
evctl 'EVENTS fservd.* SINCE 1h ago WHERE outcome.success == false'
```

Then read `outcome.reason` on the failed record:

| Event | Reason | What to do |
|---|---|---|
| `fservd.session.started` | `authentication-failed` | Read authd's record, below |
| `fservd.session.started` | `not-ntlmv2` | Sign in with an account, not as a guest, using NTLMv2 |
| `fservd.share.connected` | `no-such-share` | Check the name, and that SMB is on for the share |
| `fservd.share.connected` | `access-denied` | The share's network access grants this person nothing, or they connected anonymously and the share doesn't allow it |

Run `evman fservd.session.started` for every reason and field.

## Find out why a sign-in failed

authd records every sign-in it handles as `authd.logon.attempted`:

```sh
evctl 'EVENTS authd.logon.attempted SINCE 1h ago WHERE outcome.success == false'
```

Its `outcome.reason` says whether the account name or password was wrong
(`authentication-failed`), the account is restricted, or the account isn't
allowed to sign in over the network (`logon-type-not-permitted`). authd never
records the account name a failed sign-in gave.

## When someone can connect but can't use a file

fservd acts as the person for every file operation, so a file they can't
open over the network is usually one they couldn't open at the console
either. Check the file's permissions on this machine first. If those allow
the operation, check the share's network access: `fserv share show` with
the share's name shows it, and a restriction such as **Read-only for
everyone signed in** refuses every change.

To record exactly which file access the kernel refused, add an audit entry
to the folder's descriptor. Every record of the person's file access then
carries the same `subject.token.auth-id` as the `object.session.id` on
their `fservd.session.started` record.

## Read the file server's log

fservd writes a line to its log for each share it serves when it starts,
and for each share it can't use:

```sh
evctl 'LOGS FROM fservd SINCE 1h ago TAKE 40'
```

## Check the result

Ask the person to connect again. A successful connection records
`fservd.session.started` and `fservd.share.connected` with
`outcome.success` true.
