Skip to content

Share permissions and groups

Exercise share-permissions-and-groups-annex.pdf · PDF

Yesterday everybody at Tramuntana opened the same drawer. Now Aina keeps the salaries there. Pau, the intern, is curious. What stops him from opening the file?

Nothing, yet. A file server that gives everything to everybody is only half a file server. This page turns a simple sentence from the company, "each department writes in its own folder, nobody else gets in, and administration can read development", into real shares and real permissions. And you will discover a rule that decides every access to every network folder you will ever configure.

What you will learn

  • Tell share permissions (in smb.conf) from file-system permissions (on the folder), and say which one wins
  • Turn a "who may do what" table into shares with valid users, read only and write list
  • Prepare a department folder: group owner, setgid, and the right create mask
  • Give a second group read-only access with an ACL
  • Test every user against every share and explain each refusal by its message
  • Why a hidden share is not a protected share

Two doors, both must say yes

Remember the four doors from the Samba file server page: the building, reception, the drawer, the paper. Today we work on the last two.

Door 3 · the share Door 4 · the file system
Where it is written /etc/samba/smb.conf On the folder and the files: owner, group, rwx, ACL
Who checks it Samba (smbd) The Linux kernel
The words valid users, read only, write list chgrp, chmod, setfacl
It protects Only access through the network share Every access: Samba, SSH, the console

Share permissions say who may use a share and how; file-system permissions say what each user may do with each file. You pass both, one after the other.

flowchart LR
    U["<b>marta</b><br>wants to write<br>in dev"] --> S{"<b>Door 3 · the drawer</b><br>smb.conf says?"}
    S -->|"no"| X1["refused:<br>tree connect failed"]
    S -->|"yes"| F{"<b>Door 4 · the paper</b><br>rwx and ACL say?"}
    F -->|"no"| X2["refused:<br>opening remote file"]
    F -->|"yes"| OK["<b>she writes</b>"]

If one door says read and the other says write, you can read. The stricter door always wins. Keep that in mind; you will prove it with two experiments at the end.

The share words

Parameter Meaning
valid users = @dev @admin Only these may enter. @name = a Linux group
read only = yes Nobody writes... except:
write list = @dev ...these, who write even when the share is read-only
browseable = no The share is not shown in the list. It hides; it does not protect

Careful with the @: valid users = dev means a user called dev, not the group.

The table comes first

What the company asked for, written as a table. RW = read and write, R = read, – = no access.

Share marta (dev) pau (dev) joan (design) aina (admin)
common RW RW RW RW
dev RW RW – R
design – – RW –
admin – – – RW

Why can Aina read dev? She prepares the invoices and must see which projects exist, but she must not change code. That is least privilege again: this table is the security policy of the server, written before anybody touches a file.

Notice that every permission goes to a group. When Tramuntana hires Laia, a second designer, you add her to design (plus passwd and smbpasswd -a) and change nothing in smb.conf.

Three department shares

On the server, one folder per department, owned by its group:

for d in dev design admin; do
  sudo mkdir -p /srv/samba/$d
  sudo chgrp $d /srv/samba/$d
  sudo chmod 2770 /srv/samba/$d
done
sudo smbpasswd -a pau
sudo smbpasswd -a aina

2770 means: owner and group get rwx, others get nothing, and the 2 is setgid on a directory (Unit 5): every new file inside belongs to the folder's group, not to the creator's main group.

Then the sections in smb.conf (the first, simplest version):

[dev]
   path = /srv/samba/dev
   valid users = @dev
   read only = no

[design]
   path = /srv/samba/design
   valid users = @design
   read only = no
   create mask = 0660
   directory mask = 2770

[admin]
   path = /srv/samba/admin
   valid users = @admin
   read only = no
   browseable = no
   create mask = 0660
   directory mask = 2770

testparm -s, sudo systemctl restart smbd, and test every user against every share from deb-lab:

smbclient //ubu-srv/design -U joan -c 'put hello.txt joan.txt; ls'
smbclient -L //ubu-srv -U marta
Test Result
joan → design, put Works
joan → dev tree connect failed: NT_STATUS_ACCESS_DENIED (door 3)
marta → admin tree connect failed: NT_STATUS_ACCESS_DENIED (door 3)
aina → admin, put Works
smbclient -L as anybody Shows common, dev, design, but not admin

Careful: hidden is not protected

admin does not appear in the list, yet Aina opens it by typing its name, and Marta is refused by valid users, not by the hiding. browseable = no is tidiness, not security.

The trap: Pau cannot edit Marta's file

As Marta, put plan.txt in dev. As Pau, try to replace it: refused. Both are in dev, both passed both doors. Why? Look at the file on the server:

ls -l /srv/samba/dev

The file belongs to marta, group dev, with permissions rwxr--r--: the group can only read. When you say nothing, Samba creates files with create mask = 0744: write for the owner only.

A create mask is the set of permissions Samba allows on new files; a directory mask is the same for new folders.

What a shared folder needs:

Need How Where
New files belong to the department's group setgid on the folder: the 2 in chmod 2770 File system
The group can write them create mask = 0660 smb.conf
New folders stay shared too directory mask = 2770 smb.conf

With the masks added to [dev] and smbd restarted, new files arrive like this:

-rw-rw---- marta:dev /srv/samba/dev/plan.txt
drwxrws--- marta:dev /srv/samba/dev/sprint1

Masks only act on new files

The old plan.txt still has rwxr--r--. Masks are applied when a file is created; old files keep what they had. Delete it and put it again, or chmod it.

A second group, read-only: the ACL

Aina must read dev. But a folder has one group owner, and it is dev. To give a second group different rights you need an ACL (access control list, Unit 5): an extra list of permissions on the file, beyond owner, group and others.

sudo setfacl -m g:admin:rx /srv/samba/dev
sudo setfacl -d -m g:admin:rx /srv/samba/dev
getfacl -p /srv/samba/dev

The first line gives admin read access to the folder; the second (-d, default) makes every new file inside inherit it. Forget -d and Aina can list the folder but not read the new files.

And at door 3, the final [dev] section: admin may enter, the share is read-only, and only dev is in the write list.

[dev]
   comment = Development department
   path = /srv/samba/dev
   valid users = @dev @admin
   read only = yes
   write list = @dev
   create mask = 0660
   directory mask = 2770
$ smbclient //ubu-srv/dev -U aina -c 'get plan.txt; put hello.txt aina.txt'
getting file \plan.txt of size 19 as plan.txt (18.6 KiloBytes/sec) (average 18.6 KiloBytes/sec)
NT_STATUS_ACCESS_DENIED opening remote file \aina.txt

Aina reads but cannot write: exactly the R in the table. Marta and Pau can still write.

Which door wins? Two experiments

Write your prediction before you try each one.

Door 3 · share Door 4 · file system Can she write?
A · Marta write list = @dev: yes sudo chmod g-w /srv/samba/dev: no ?
B · Aina read only, not in the write list: no sudo setfacl -m g:admin:rwx /srv/samba/dev: yes ?
The results (open after your prediction)
  • A: Marta cannot write: NT_STATUS_ACCESS_DENIED opening remote file. The share said yes, the file system said no.
  • B: Aina cannot write: the same message. The file system said yes, the share said no.

The effective permission is the stricter of the two doors. To give access you must open both; to take it away, closing one is enough. Restore both changes afterwards.

If you may only close one door, close the file system: it also protects the folder from somebody who enters by SSH. Good practice is both: the share says who comes in, the file system says what they do.

From Windows the rule is the same: as Aina, \\192.168.122.10\dev opens and shows the files, and creating one gives an access-denied window. As Marta (after net use * /delete) it works.

Key takeaways

  • Every network access passes two doors: share permissions in smb.conf and file-system permissions on the folder. The stricter one wins.
  • Write the "who may do what" table first; give permissions to groups, never to people.
  • valid users decides who enters, read only + write list who writes; browseable = no only hides.
  • A department folder needs a group owner, setgid (chmod 2770) and create mask = 0660 / directory mask = 2770. Masks act on new files only.
  • A second group with different rights needs an ACL: setfacl -m plus -d for new files.

Quick check

1. The share says read and write. The folder gives Aina's group only r-x. What can Aina do?
  • Only read
  • Read and write
  • Nothing at all
  • Write but not read

Both doors must say yes, so the stricter one wins. The file system only allows reading, so she can read and nothing more.

2. Files created through Samba are read-only for the group. Which line in the share fixes it?
  • create mask = 0660
  • browseable = yes
  • valid users = @dev
  • read only = yes

Without a mask, Samba creates files as rwxr--r--. create mask = 0660 makes new files readable and writable by the owner and the group.

3. What does browseable = no do to a share?
  • It hides the share from the list
  • It blocks every user from opening it
  • It makes the share read-only
  • It asks every user for a second password

It only removes the name from the list. Anyone who types the name can still try, and valid users is what actually protects it.

4. The folder dev is owned by the group dev. The group admin must read it too. What do you use?
  • An ACL with setfacl -m g:admin:rx
  • chmod 777 on the folder
  • A second chgrp to the group admin
  • The sticky bit on the folder

A folder has only one group owner. An ACL adds a second group with its own rights; chmod 777 would open it to everybody, and a second chgrp would remove dev.

5. Pau and Marta are both in dev, but Pau cannot replace a file Marta created. Why?
  • The file was created without write permission for the group
  • Pau is not in the valid users of the share
  • Pau has no Samba password yet
  • The share is hidden from the list

Being in the same group only gives what the file's permissions give the group. Without a create mask, the file arrives as rwxr--r--: the group can only read.

6. After adding create mask = 0660, Marta's old plan.txt is still read-only for the group. Why?
  • Masks only act on files when they are created
  • The ACL overrides the mask on old files
  • smbd must be reinstalled to apply masks
  • Masks only act on folders, not on files

A create mask is applied at the moment a file is created. Old files keep their permissions: delete and re-create them, or chmod them.

7. Tramuntana hires Laia, a second designer. Select the two that are true.
  • Adding her to the group design is enough for the department share
  • She also needs a Samba password with smbpasswd -a
  • Her name must be added to the design section of smb.conf
  • The design folder must be given to her with chown

Permissions go to groups, so one useradd -G design (plus passwd and smbpasswd -a) gives her everything, with zero changes to smb.conf or the folder.

Samba asked each user for a password. The next protocol asks nothing: it trusts the client machine. Why would anybody want that? Find out in NFS.