[PP-SCRIPT] Change downloaded folder rights

Share your scripts or request scripts with specific features.
Forum rules
Please keep the forum clean - one topic per script. Questions not related to a specific script should be posted in Support forum.
Post Reply
sbeek
Posts: 9
Joined: 30 Jun 2013, 17:11

[PP-SCRIPT] Change downloaded folder rights

Post by sbeek » 03 Jul 2013, 10:05

Hi,

I'm running NZBget, CouchPotato and SickBeard on a Synology NAS. All of these packages run on their own user. Whenever NZBget downloaded a file, the renaming & deletion of post processing by CouchPotato and Sickbeard do not have access to the downloaded item, because it is created under the NZBget user.

I created a simple script, which works 75% of the time but fails randomly. As my programming experience isn't really high, could any one assist to get it working better? Sometimes it fails, when running PP again it goes Okay.

Here is the script:

Code: Select all

#!/usr/bin/env python

### NZBGET POST-PROCESSING SCRIPT

import os
import grp
import pwd
import sys


print('[DETAIL] Changing owner settings...')

for root, dirs, files in os.walk(os.environ['NZBPP_DIRECTORY']):
	for momo in dirs:
		os.chown(momo, pwd.getpwnam("sickbeard").pw_uid, grp.getgrnam("users").gr_gid)
	for file in files:
		fname = os.path.join(root, file)
		os.chown(fname, pwd.getpwnam("sickbeard").pw_uid, grp.getgrnam("users").gr_gid)


os.chown(os.environ['NZBPP_DIRECTORY'], pwd.getpwnam("sickbeard").pw_uid, grp.getgrnam("users").gr_gid)


		
sys.exit(93)
Looking forward for any help on this! :)

wusa
Posts: 22
Joined: 05 Sep 2013, 18:39

Re: [PP-SCRIPT] Change downloaded folder rights

Post by wusa » 25 Sep 2013, 15:44

I have written some piece in bash, maybe helpful.

Code: Select all

#!/usr/bin/sh

# cfp post-processing script for NZBGet wusamba wusamba {at] gmail [D o T} com

################################################################################
### NZBGET POST-PROCESSING SCRIPT                                            ###

# Change file permission.

################################################################################
### OPTIONS                                                                  ###

# Put user here.
user=user

# Put group here.
group=group

# Set owner (yes, no).
setowner=no

# Put mode here.
mode=666

# Set mode (yes, no).
setmode=no

### NZBGET POST-PROCESSING SCRIPT                                            ###
################################################################################


# Exit codes
#POSTPROCESS_PARCHECK=92
#POSTPROCESS_SUCCESS=93
POSTPROCESS_ERROR=94
#POSTPROCESS_NONE=95

POSTPROCESS=95

# Check if the script is called from nzbget 11.0 or later
if [ "$NZBOP_SCRIPTDIR" = "" ]; then
        echo "*** NZBGet post-processing script ***"
        echo "This script is supposed to be called from nzbget (11.0 or later)."
        exit $POSTPROCESS_ERROR
fi

# Check if directory exists
if [ -d "$NZBPP_DIRECTORY" ]; then
	# chown
	if [ "$NZBPO_SETOWNER" = "yes" ]; then
		if (chown $NZBPO_USER:$NZBPO_GROUP -R "$NZBPP_DIRECTORY"); then
			echo "[INFO] Permission set"
			POSTPROCESS=93
		else
			echo "[WARN] Permission not set"
			exit $POSTPROCESS_ERROR
		fi
	fi
	# chmod
	if [ "$NZBPO_SETMODE" = "yes" ]; then
		if (chmod $NZBPO_MODE -R "$NZBPP_DIRECTORY"); then
			echo "[INFO] Mode set"
			POSTPROCESS=93
		else
			echo "[WARN] Mode not set"
			exit $POSTPROCESS_ERROR
		fi
	fi
else
	echo "[WARN] Directory not found"
	echo "[DETAIL] $NZBPP_DIRECTORY"
	exit $POSTPROCESS
fi

exit $POSTPROCESS
Adjust first line for your OS! (ex. #!/ffp/bin/sh)
Attachments
cfp.sh
(1.75 KiB) Downloaded 904 times

dime
Posts: 18
Joined: 14 Aug 2013, 21:09

Re: [PP-SCRIPT] Change downloaded folder rights

Post by dime » 24 Jul 2014, 21:55

wusa wrote:I have written some piece in bash, maybe helpful.
Unfortunately this doesn't seem to work correctly. You need to set +x bit on directories or else you cannot read their contents. If you use setmode=yes with mode=664 it blanket sets this for both files and directories, when actually directories need to be set 775.

edit: here is my working, crude version without any logging/error checking. Need to test downloads with N-level depth dirs/files...
edit2: seems to work fine

Code: Select all

#!/bin/sh

# post-processing script for fixing permissions

################################################################################
### NZBGET POST-PROCESSING SCRIPT                                            ###

# Change file permission.

################################################################################
### OPTIONS                                                                  ###

# Put mode for files here. Should be -x (ex: 664 or 664 or 666)
mode=664

# Put mode for directories here. Should be +x (ex: 755 or 775 or 777)
dirmode=775

### NZBGET POST-PROCESSING SCRIPT                                            ###
################################################################################

# recursively set perms files
find "$NZBPP_DIRECTORY" -type f -exec chmod $NZBPO_MODE {} ';'

# recursively set perms dirs
find "$NZBPP_DIRECTORY" -type d -exec chmod $NZBPO_DIRMODE {} ';'

# 93 is code for success
exit 93
Attachments
permissions.sh
recursive set perms and +X mode for dirs
(983 Bytes) Downloaded 2162 times

wusa
Posts: 22
Joined: 05 Sep 2013, 18:39

Re: [PP-SCRIPT] Change downloaded folder rights

Post by wusa » 04 Aug 2014, 14:48

Nice one. Mine works (for me) fine, too. But now we have a more secure method. 8-)

igiddyuup
Posts: 3
Joined: 12 Mar 2018, 13:51

Re: [PP-SCRIPT] Change downloaded folder rights

Post by igiddyuup » 10 Apr 2018, 12:33

Hey I'm running this on my ReadyNAS 204, it doesn't seem to be changing the permissions properly for me. I want the files to be 777 and 666.

I have the script set up as:
# Put mode for files here. Should be -x (ex: 664 or 664 or 666)
mode=666

# Put mode for directories here. Should be +x (ex: 755 or 775 or 777)
dirmode=777


The files are coming in as: 0664 rw-rw-r--
Folders: 0755 rwxr-xr-x

What am I doing wrong?

fryfrog
Posts: 10
Joined: 03 Sep 2014, 23:16

Re: [PP-SCRIPT] Change downloaded folder rights

Post by fryfrog » 27 Jun 2018, 15:55

I liked how well @wusa's handled settings and errors, but @dime's is really the right way to fix it because files and folders need different permissions to work correctly. So I combined them and I think it is a slight improvement on both. :)

Code: Select all

#!/bin/bash

################################################################################
### NZBGET POST-PROCESSING SCRIPT                                            ###

# Change user:group ownership and folder/file permission.

################################################################################
### OPTIONS                                                                  ###

# Set owner and group (yes, no).
setowner=no

# Put user here.
user=user

# Put group here.
group=group

# Set folder and file permissions (yes, no).
setmode=no

# Put file mode here.
mode=664

# Put folder mode here.
dirmode=775

### NZBGET POST-PROCESSING SCRIPT                                            ###
################################################################################


# Exit codes
#POSTPROCESS_PARCHECK=92
#POSTPROCESS_SUCCESS=93
POSTPROCESS_ERROR=94
#POSTPROCESS_NONE=95

POSTPROCESS=95

# Check if the script is called from nzbget 11.0 or later
if [[ "${NZBOP_SCRIPTDIR}" = "" ]]; then
  echo "*** NZBGet post-processing script ***"
  echo "This script is supposed to be called from nzbget (11.0 or later)."
  exit ${POSTPROCESS_ERROR}
fi

if [[ "${NZBPO_USER}" = "user" ]] || [[ "${NZBPO_GROUP}" = "group" ]]; then
  echo "*** NZBGet post-processing script ***"
  echo "[WARN] The user and group are set to defaults, check script settings."
  exit ${POSTPROCESS_ERROR}
fi

# Check if directory exists
if [[ -d "${NZBPP_DIRECTORY}" ]]; then
  # chown
  if [[ "${NZBPO_SETOWNER}" = "yes" ]]; then
    if chown "${NZBPO_USER}:${NZBPO_GROUP}" -R "${NZBPP_DIRECTORY}"; then
      echo "[INFO] User and group set"
      POSTPROCESS=93
    else
      echo "[WARN] User and group NOT set"
      exit ${POSTPROCESS_ERROR}
    fi
  fi

  # chmod
  if [[ "${NZBPO_SETMODE}" = "yes" ]]; then
    # recursively set perms on files
    if find "${NZBPP_DIRECTORY}" -type f -exec chmod "${NZBPO_MODE}" '{}' ';'; then
      echo "[INFO] File permissions set"
      POSTPROCESS=93
    else
      echo "[WARN] File permissions NOT set"
      exit ${POSTPROCESS_ERROR}
    fi

    # recursively set perms on folders
    if find "${NZBPP_DIRECTORY}" -type d -exec chmod "${NZBPO_DIRMODE}" '{}' ';'; then
      echo "[INFO] Folder permissions set"
      POSTPROCESS=93
    else
      echo "[WARN] Folder permissions NOT set"
      exit ${POSTPROCESS_ERROR}
    fi
  fi
else
  echo "[WARN] Directory not found"
  echo "[DETAIL] $NZBPP_DIRECTORY"
  exit ${POSTPROCESS}
fi

exit ${POSTPROCESS}

lordfrikk
Posts: 1
Joined: 30 Aug 2018, 20:34

Re: [PP-SCRIPT] Change downloaded folder rights

Post by lordfrikk » 30 Aug 2018, 20:37

I just wanted to thank everyone for putting this script together. I've been banging my head against the wall when nzbget wouldn't cooperate with other apps like radarr and finally found out it's because I cannot manually set permissions. Seems like a weird oversight to allow us to change umask but not default permissions... Oh, well, it's solved now!

monsen
Posts: 1
Joined: 03 Jan 2019, 19:20

Re: [PP-SCRIPT] Change downloaded folder rights

Post by monsen » 04 Jan 2019, 14:39

This is actually exactly what I need. After I could not get my own script to change the ownerships on downloads I searched and found this thread. Unfortunately I am having similar issues with this solution. The permissions part (chmod) works well but the change of ownership does not. The last script from fryfrog is really well done but still does not work for that part.
Trying different things brings different error messages through the log that are really confusing and don't really help to solve this.
My assumption upto here is that CHOWN cannot be used from within the script for me because of user rights.

Here is my setup:
NZBGet runs in a docker container under the user "dockeruser"
dockeruser has been added to "sudoers" for using "chown"
Nevertheless dockeruser cannot use "sudo" from within an nzbget script (log says: "sudo: command not found")
Running "chown" without "sudo" from within the script also brings up an error (log says: "chown: invalid user: ‘X:Y’ -- Weird?!)

So here my question:
I cannot run nzbget as root but need to use the restricted dockeruser.
Does anyone have an idea how I could get the "chown" part to run correctly?
Is anyone using this with a restricted user maybe even docker?

Nsar
Posts: 1
Joined: 06 Jan 2019, 12:09

Re: [PP-SCRIPT] Change downloaded folder rights

Post by Nsar » 09 May 2020, 05:05

any update after sooo lng time ? i think this is very important for synology users im sure theres lots of poeple is looking for this

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests