Jump to content

Fixing share contents ownership and permissions -> a simple solution to share!


Recommended Posts

So I'm sure I'm not the only one that has had some pains with some Docker containers not assigning the right ownership or permissions on files that they create in the shared folders. Or, if a an SMB user puts files onto a networks share.

 

In my case, I couldn't change that in the Docker container I was running.

 

So while I am not a scripter, I did stay at a Holiday-Inn Express last night! So I fired up ChatGPT and got to work on writing a script that would fix this problem, in real-time.

 

I think it's pretty self-explanatory if you go through the code. But this will monitor files/folders for whenever a file or folder is touched on some way. It will then check the permissions and ownership and fix them accordingly.

 

I added array variables so that you can define which targets you want to monitor. I also added an array variable to exclude certain file extensions from being changed. Add the share paths you want to monitor each in quotes. Then add the file extensions you want to exclude, each in quotes. The script is set to monitor recursivly so you only have to enter the top level. Everything contained will be monitored.

 

Pop it into the userscripts add-on and set it to start at array start.

 

So far in my testing it's been working a treat! But do your own testing to make sure that it is working how you want it to before you set it to start automatically.

 

#!/bin/bash

# Monitored directories
monitored_dirs=("/mnt/user/path1/" "/mnt/user/path2/")

# Excluded extensions
excluded_exts=("ext1" "ext2")

# Function to check ownership and permissions of files and directories
check_obj() {
    if [[ -e "$1" ]]; then
        # File or directory exists
        # Exclude files with excluded extensions
        for ext in "${excluded_exts[@]}"; do
            if [[ "$1" == *".$ext" ]]; then
                return
            fi
        done

        # Check ownership
        owner=$(stat -c '%U' "$1")
        if [[ "$owner" != "nobody" ]]; then
            # Set ownership to nobody
            chown nobody "$1"
        fi

        # Check permissions
        if [[ -d "$1" ]]; then
            # Directory
            perms=$(stat -c '%a' "$1")
            if [[ "$perms" != "777" ]]; then
                # Set permissions to 'drwxrwxrwx'
                chmod 777 "$1"
            fi
        else
            # File
            perms=$(stat -c '%a' "$1")
            if [[ "$perms" != "666" ]]; then
                # Set permissions to '-rw-rw-rw-'
                chmod 666 "$1"
            fi
        fi
    fi
}

# Watch for changes to files and directories
inotifywait -r -m -e create,modify,move,attrib "${monitored_dirs[@]}" |
while read path action file; do
    # Check ownership and permissions of changed object
    check_obj "$path/$file"
done

 

Edited by aglyons
  • Thanks 1
Link to comment
  • 8 months later...

Thanks for this!
Did you encounter any problems with this script? Like disks or server not going to sleep, etc. ?

 

Edit: I'll be using it for now because I don't have time to find what is misconfigured. It's always better to have a correct configuration than messing with permissions.

Edited by Steace
Link to comment

I haven't run into any issues myself. I'm not a scripter really. This was built with the help of ChatGPT and just kept adding revisions until it did everything I was looking for.

 

I've run into some docker containers that don't provide UUID/GUID features so it's impossible to set those. That's why I created it. It has saved me headaches of not being able to access files created by those apps across the LAN/SMB.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...