sysadmin/scripts/aclset.sh

117 lines
2.9 KiB
Bash
Raw Normal View History

2017-09-15 14:11:31 -04:00
#!/bin/bash
2020-04-07 16:14:32 -04:00
# acl setup
2017-09-15 14:11:31 -04:00
### vars
2020-04-07 16:14:32 -04:00
watchdir="/srv/share"
2020-04-07 16:16:06 -04:00
domainadmin="user"
password="pass"
2017-09-15 14:11:31 -04:00
###
#init
controlfile="control.txt"
2020-04-07 16:14:32 -04:00
passfile="delete.txt"
2017-09-15 14:11:31 -04:00
aclset="";
acldel="";
old_IFS=$IFS # save the field separator
IFS=$'\n' # new field separator, the end of line
2020-04-07 16:14:32 -04:00
exec >> /var/log/afxacl.log 2>&1
2017-09-15 14:11:31 -04:00
2020-04-07 16:14:32 -04:00
mlocate --database=/var/tmp/afxacl.db $controlfile > /var/tmp/afxacl.set.1.tmp
mlocate --database=/var/tmp/afxacl.db $passfile > /var/tmp/afxacl.del.1.tmp
updatedb --database-root=$watchdir --output /var/tmp/afxacl.db -l 0
mlocate --database=/var/tmp/afxacl.db $controlfile > /var/tmp/afxacl.set.2.tmp
mlocate --database=/var/tmp/afxacl.db $passfile > /var/tmp/afxacl.del.2.tmp
2017-09-15 14:11:31 -04:00
2020-04-07 16:14:32 -04:00
setlist=`diff /var/tmp/afxacl.set.1.tmp /var/tmp/afxacl.set.2.tmp`
2017-09-15 14:11:31 -04:00
aclset=`echo "$setlist" | grep '>'`
2020-04-07 16:14:32 -04:00
dellist=`diff /var/tmp/afxacl.del.1.tmp /var/tmp/afxacl.del.2.tmp`
2017-09-15 14:11:31 -04:00
acldel=`echo "$dellist" | grep '>'`
#del
if [ -n "$acldel" ]
then
while read dline;
do
curcontroldel=`echo "$dline" | cut -c 3-`;
echo "unlocking $curcontroldel"
2020-04-07 16:14:32 -04:00
ccut=`expr ${#passfile} + 1`
cdir=`echo "$curcontroldel" | rev | cut -c $ccut- | rev`
echo ""
2017-09-15 14:11:31 -04:00
if [ -d "$cdir" ];
then
if grep -q $password "$curcontroldel";
2020-04-07 16:14:32 -04:00
then
echo "password accepted"
chattr -i "$cdir/$controlfile"
rm "$cdir/$controlfile"
setfacl -R --remove-all "$cdir"
chmod 770 "$cdir"
echo ""
echo "current permissions:"
getfacl "$cdir"
rm "$curcontroldel"
else
echo "invalid password!"
rm "$curcontroldel"
fi
2017-09-15 14:11:31 -04:00
else
echo "warning: whole dir was deleted"
fi
2020-04-07 16:14:32 -04:00
echo ""
updatedb --database-root=$watchdir --output /var/tmp/afxacl.db -l 0
echo ""
2017-09-15 14:11:31 -04:00
done < <(echo "$acldel")
fi
# set
if [ -n "$aclset" ]
then
while read cline;
do
2020-04-07 16:14:32 -04:00
curcontrolset=`echo "$cline" | cut -c 3-`;
echo "setting up acl from $curcontrolset"
ccuser=`stat -c "%U" "$curcontrolset"`
if [ "$ccuser" != "$domainadmin" ];
then
echo "$ccuser is not a valid admin!"
rm $curcontrolset
continue;
fi
echo ""
2017-09-15 14:11:31 -04:00
ccut=`expr ${#controlfile} + 1`
2020-04-07 16:14:32 -04:00
cdir=`echo "$curcontrolset" | rev | cut -c $ccut- | rev`
2017-09-15 14:11:31 -04:00
chmod 700 "$cdir"
for uline in $(cat "$curcontrolset")
do
echo "add user $uline ..."
2020-04-07 16:14:32 -04:00
setfacl -R -n -m u:$uline:rwx "$cdir"
2017-09-15 14:11:31 -04:00
done
2020-04-07 16:14:32 -04:00
echo "add admin $domainadmin ..."
setfacl -R -n -m u:$domainadmin:rwx "$cdir"
setfacl -R -n -m m::rwx "$cdir"
chattr +i "$curcontrolset"
echo ""
echo "current permissions:"
getfacl "$cdir"
echo ""
echo ""
2017-09-15 14:11:31 -04:00
done < <(echo "$aclset")
fi
IFS=$old_IFS # restore default field separator
2020-04-07 16:14:32 -04:00
#if [ -s /var/log/afxacl.log ];
#then
# mutt -s "ACL" user@mail.com < /var/tmp/afxacl.log
#fi
2017-09-15 14:11:31 -04:00
#cleantmp
2020-04-07 16:14:32 -04:00
rm /var/tmp/afxacl.set*
rm /var/tmp/afxacl.del*