Samba – How to force Samba to create directory

active-directorysambashare

I need help with Samba configuration.

What I want to achieve is configuration, where windows user on share see only his files.
This is simply achived With configuration like this:

Users are authorized by Windows AD Server

[BACKUP]
  comment = BACKUP STORAGE LOCATION
  path = /storage/BACKUP
  read only = no
  browseable = yes
  writable = yes
  create mode = 0600
  directory mode = 0700
  force directory mode = 0700
  force create mode = 0600
  access based share enum = yes
  hide unreadable = yes
  valid users = "@DOMAINNAME+SOMEUSERGROUP"

It works ok but…
On the server side, everything in directory /storage/BACKUP keeps files of every user.

So I would like to create directory here for every user (I Can't use [home], because it's already used)

So i Would like to keep it like this:

/storage/BACKUP/username/

So when i change path, and add %U at end, everything is almost ok.

The problem is that i need to manually create directory /storage/BACKUP/username.

So what i need is somehow force Samba to create this directory before user to access this share.

I've tried adding add user script = /path/to/mkdir /storage/BACKUP/%U
But this is not working because:

  1. I don't know why 😉
  2. I've already have users logged in before
  3. It should start for new created users, this will be ok for me, but it not works.

I'm not creating users in linux, after they login, so i'm not using add user/machine script anywhere else.

So i want to force Samba to create directory for user, when this user tries to connect.
I'm searching google from couple of hours, and didn't find a way to do it that will work for me.

I need to keep /server/BACKUP location for everyone, but on the server side, Need to keep files in separated directories per user, so creating a new share is also not a solution.

Best Answer

You can use the preexec or root preexec options for this. They specify a script that is run upon connection to a share. In case of preexec the share is run as the connecting user, and as root with root preexec.

In your share:

[BACKUP]
root preexec = /etc/samba/gendir.sh %u

where /etc/samba/gendir.sh looks somewhat like this:

#!/bin/bash 
DIRECTORY=/storage/BACKUP/$1
if [ ! -d "$DIRECTORY" ]; then
   mkdir $DIRECTORY
fi

Depending on your requirements, add chown and/or chmod statements to the script.