Skip to content

Instantly share code, notes, and snippets.

@jossef
Created March 3, 2015 12:57
Show Gist options
  • Select an option

  • Save jossef/b5aae953cc356638e654 to your computer and use it in GitHub Desktop.

Select an option

Save jossef/b5aae953cc356638e654 to your computer and use it in GitHub Desktop.
Linux Share Permissions Fixer
#!/usr/bin/python
import pyinotify
import subprocess
import os
wm = pyinotify.WatchManager()
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
path = event.pathname
if os.path.exists(path):
print 'chmod {0}'.format(path)
command = "chmod 777 '{0}'".format(path)
print subprocess.call(command, shell=True)
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch('/share', pyinotify.IN_CREATE, rec=True)
notifier.loop()
#!/bin/sh -e
DAEMON="/usr/local/bin/share-fixer"
DAEMONUSER="root"
DEAMON_NAME="share-fixer"
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
d_start () {
log_daemon_msg "Starting $DEAMON_NAME"
start-stop-daemon --background --name $DEAMON_NAME --start --user $DAEMONUSER --exec $DAEMON
log_end_msg $?
}
d_stop () {
log_daemon_msg "Stopping $DEAMON_NAME"
start-stop-daemon --name $DEAMON_NAME --stop --retry 5 --name $DEAMON_NAME
log_end_msg $?
}
case "$1" in
start|stop)
d_${1}
;;
restart|reload|force-reload)
d_stop
d_start
;;
force-stop)
d_stop
killall -q $DEAMON_NAME || true
sleep 2
killall -q -9 $DEAMON_NAME || true
;;
status)
status_of_proc "$DEAMON_NAME" "$DAEMON" "system-wide $DEAMON_NAME" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$DEAMON_NAME {start|stop|force-stop|restart|reload|force-reload|statu$
exit 1
;;
esac
exit 0
@jossef

jossef commented Mar 3, 2015

Copy link
Copy Markdown
Author

TL;DR

this is a workaround for Linux machines acts as File Servers over LAN (mounted via smb or nfs) to remove new files' permissions (like Everyone with full control in windows)

Why?

when you setup a linux machine as both nfs and samba to serve files over the network for windows and linux clients, you will probably deal with what's called permission hell. there are some good and secured solution involving your clients to configure their users to be in certain groups.

But, let's simplify the client experience with 0 configuration requirements
the idea is to set in the server a Watch - when a client creates a new file a trigger will call chmod <mask> <watch-path>

i find it convenient workaround for a small network share


save share-fixer-daemon in

/etc/init.d/share-fixer 

save share-fixer in

/usr/local/bin/share-fixer

run

sudo pip install pyinotify
sudo chmod 777 /usr/local/bin/share-fixer
sudo chmod 777 /etc/init.d/share-fixer
sudo update-rc.d share-fixer defaults
sudo service share-fixer start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment