Daemon for rox-filler

Hi to all friends .On my suse 13.1 i use rox-filler as default filemager .For mount unmount removable device, i found for any filesystem is need a different options and a right line on
/etc/fstab . On repository i don’t found any right application to do this . Then I have decided to write the following script
------------start script ---------------------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash
#################################################

licenza LGPLv3 8/2014 gumo73@virgilio.it version 0.1 anfstab means a new fstab

daemon to change just in time fstab for removable media /dev/sdp9

#################################################

#remove from anfstab.conf wrong lines
new_anfstab_conf()
{
nice grep -v “$1” /etc/anfstab.conf > /etc/anfstab.old
nice cp /etc/anfstab.old /etc/anfstab.conf
}
#delete line on fstab
new_fstab()
{
nice echo “new fstab remove /dev/sd$1” > /etc/fstab.old
nice grep -v ^/dev/sd$1 /etc/fstab > /etc/fstab.old
nice cp /etc/fstab.old /etc/fstab
}
#test filesystem of a new device …
testdevice ()
{
if ! -n “$(cat /etc/anfstab.conf | grep ^“device” | cut -f 3 -d : | grep “sd$1” )” ]
then
nice mount -o ro,nosuid /dev/sd$1 /media/test/
fi
if -n “$(nice mount | grep /dev/sd$1 )” ]
then
for i in vfat ext2 ext3 ext4 xfs
do
if -n “$(nice df -t $i /media/test/ | grep /media/test)” ]
then
rsp=$i
break
fi
done
else
nice echo “error on test mount point for /dev/sd$1” >> /var/log/anfstab.log
unset rsp
fi
if -n “$( nice mount | grep “/media/test” )” ]
then
nice umount /media/test
fi
}
#if need add a new mount point and add right line to fstab
add_mountp()
{
testdevice “$1”
if -n “$rsp” ]
then
usr=$(nice who -u | grep " :0 " | cut -f 1 -d " “)
grp=$(nice cat /etc/anfstab.conf | grep “$rsp” | cut -f 1 -d “:” )
if -n “$( nice cat /etc/group | grep ^”$grp” | grep “$usr”)" ]
then
opt=$(nice cat /etc/anfstab.conf | grep “$rsp” | cut -f 4 -d “:” )
if “yes_user” = “$(nice cat /etc/anfstab.conf | grep “$rsp” | cut -f 2 -d “:”)” ]
then
opt="$opt,uid=$usr"
fi
if -n “$grp” ] && -n “$usr” ] && -n “$opt” ]
then
mkdir /mnt/usb$1
nice echo “/dev/sd$1 /mnt/usb$1 “$rsp” “$opt” 0 0” >> /etc/fstab
fi
fi
unset usr
unset grp
fi
}
#check if device is mounted or not …
amusb()
{
for i in a b c d e f g h k i l m n o p
do
for j in 1 2 3 4 5 6 7 8 9
do
if -e /dev/“sd$i$j” ]
then
if ! -n “$( nice mount | grep “sd$i$j”)” ]
then
if ! -e /mnt/“usb$i$j” ]
then
add_mountp “$i$j”
fi
fi
fi
done
done
}
#delete a unused mount point if device is not present…
del_mountp()
{
for i in a b c d e f g h k i l m n o p
do
for j in 1 2 3 4 5 6 7 8 9
do
if -d /mnt/usb$i$j ]
then
if ! -e /dev/“sd$i$j” ]
then
rm -r /mnt/usb$i$j
fi
fi
if ! -e “/dev/sd$i$j” ] && -n “$(cat /etc/fstab | grep ^/dev/sd$i$j )” ]
then
new_fstab “$i$j”
fi
done
done
}
#verifing configuration file
control_conf ()
{
for i in vfat ext2 ext3 ext4 xfs
do
if -n “$(nice cat /etc/anfstab.conf | grep $i | cat -n | grep 2)” ]
then
nice echo “error double filesystem option on $i” >> /var/log/anfstab.log
new_anfstab_conf “$i”
fi
if -n “$(nice cat /etc/anfstab.conf | grep $i )” ]
then
if “$(nice cat /etc/anfstab.conf | grep $i | cut -f 5 -d “:”)” != “x” ]
then
nice echo “error number fields do not correct on $i line” >> /var/log/anfstab.log
nfstab_conf “$i”
fi
fi
rsp=$(nice cat /etc/anfstab.conf | grep $i | cut -f 1 -d “:” )
if ! -n “$(nice cat /etc/group- | grep ^$rsp )” ]
then
nice echo “error no valid group $grp for $i line”>> /var/log/anfstab.log
new_anfstab_conf “$i”
fi
if -n “$(nice cat /etc/anfstab.conf | grep $i )” ] && ! -n “$(nice cat /etc/anfstab.conf | grep $i | cut -f 4 -d : )” ]
then
nice echo “error no optinos found for $i” >> /var/log/anfstab.log
fi
done
if -n “$(nice cat /etc/anfstab.conf | grep ^device )” ]
then
if ! -n “$(nice cat /etc/anfstab.conf | grep ^device | cat -n | grep 2)” ]
then
if “$(nice cat /etc/anfstab.conf | grep ^“device” | cut -f 2 -d :)” = “banned” ]
then
if ! -n “$(nice cat /etc/anfstab.conf | grep ^“device” | cut -f 4 -d :)” ]
then
if “$(nice cat /etc/anfstab.conf | grep ^“device” | cut -f 5 -d :)” = “x” ]
then
if ! -n “$(nice cat /etc/anfstab.conf | grep ^“device” | cut -f 3 -d : )” ]
then
new_anfstab_conf “device”
nice echo “error of syntax line: wrong sdx” >> /var/log/anfstab.log
fi
else
new_anfstab_conf “device”
nice echo “error of syntax line: wrong end letter " >> /var/log/anfstab.log
fi
else
nice echo “errror blank unrechable in line:device” >> /var/log/anfstab.log
new_anfstab_conf “device”
fi
else
nice echo “error of syntax device:banned:” >> /var/log/anfstab.log
new_anfstab_conf “device”
fi
else
nice echo “error of syntax device:line multiplies " >> /var/log/anfstab.log
new_anfstab_conf “device”
fi
fi
}
####################################################
#Main
####################################################
pd=”$$”
echo -n “$pd” > /var/run/anfstab.pid
unset pd
if -f /var/log/anfstab.log ]
then
cp /var/log/anfstab.log /var/log/anfstab.log.old
echo “new log” > /var/log/anfstab.log
else
echo “new log” > /var/log/anfstab.log
fi
if ! -e /etc/anfstab.conf ]
then
echo “#Group:mount as user yes_user/no_user:type of filesystem:options to write on fstab:x:” > /etc/anfstab.conf
echo “#Device:banned:sdx,sdy::x:” >> /etc/anfstab.conf
echo “device:banned:sdz::x:” >> /etc/anfstab.conf
echo “cdrom:yes_user:vfat:users,uid=100,nosuid,rw,sync:x:” >> /etc/anfstab.conf
fi
if ! -f /etc/fstab.save ]
then
cp /etc/fstab /etc/fstab.save
fi
while -e /var/run/anfstab.pid ]
do
if ! -d /media/test/ ]
then
mkdir /media/test
fi
sleep 1
control_conf
sleep 1
amusb
sleep 1
del_mountp
sleep 2
done
exit 0;
-----------------------------------------------------------end script --------------------------------------------------------------------------------------------
I put it on /bin/anfstab . But i need help for best support on suse .
I have also written the start / stop utility
--------------------------start script ------------------------------------------------------------------------------------------------------------------------------------
#!/bin/sh
#################################

anfstab start/stop lgplv3 gumo73@virgilio.it

#################################
case $1 in
start )
nice /bin/anfstab &
sleep 2
if -f /var/run/anfstab.pid ]
then
echo “ANFSTAB START!”
else
echo "ANFSTAB DON’T START "
fi
;;
stop )
if -f /var/run/anfstab.pid ]
then
echo “ANFSTB STOP!!”
rm /var/run/anfstab.pid
fi
;;
restart )
if -f /var/run/anfstab.pid ]
then
echo “ANFSTB STOP!!”
rm /var/run/anfstab.pid
fi
nice /bin/anfstab &
sleep 2
if -f /var/run/anfstab.pid ]
then
echo “ANFSTAB START”
else
echo "ANFSTAB DON’T START "
fi
;;
status )
if -f /var/run/anfstab.pid ]
then
echo "ANFSTAB RUNNING! "
else
echo “ANFSTAB DON’T RUNNING”
fi
;;
esac
exit 0
----------------------------------------------end Script ----------------------------------------------------------------------------------------------------------------------------------------------

put it on /etc/init.d/local and link it ln -s /etc/init.d/local /etc/init.d/rc5.d/S99local
The permissions of execution are obligatory on /etc/init.d/local and /bin/anfstab
Demon need run as root . Daemon create a conf file on /etc/anfstab.conf and you can edit it for any filesystem vfat ext2 -3-4 xfs

The bug are how to restore saved fstab ,In case of anomalous turning off

thanks to all for the help O_o

Some adjust informations .
You need to be a memeber of a linux system group and using X on :0 display .
To see script corectly select printable version .

Just a comment on the script, you can remove the cat. Also what do you mean by anomalous?

cat | grep is wrong because grep can parse file on its own.

this code

usr=$(nice who -u | grep " :0 " | cut -f  1  -d " ")

is thesame as

usr=$USER

I use the command cat to increase the difficulty of a hacker in to stop the script because any command is a long pipe and is so difficult to kill a command in a pipe .
The pipe (who -u | grep " :0 " ) i use it to known who is using X11 :0 root window

But when it running as root $USER is my user ? The script running as Root user . You can make a will It with your changes.

you ask me ""Just a comment on the script, you can remove the cat. Also what do you mean by anomalous? “”
When the pc extinguishes him for lack of energy and dont have the needed time to go off normally

I Lost to tell the banned device normally are the swap and the script running without any cpu consumption problems.

Thanks a lot for help O_o

jetchisel asked to me “Also what do you mean by anomalous”

When the pc turn off for lack of energy and not have a right time to made a normal shutdown .

another note: In a /etc/anfstab.conf you need add to banned device ( for example sda2 ) the swap device normally for don’t have errors in the logs /var/log/anfstab.log
: Daemon neeed running as root user O_o

who --help

to see some more options, or

who -u | awk '$2 ~ /:0/{print $1}'

is another option if you really need to parse the who -u so you can avoid the grep … | cut …
If the system did not shutdown properly then not even a trap at the beginning of your script could help imo
The cat should go. If “hackers” has access to your system then changing your script is the last thing you should worry about. :wink:
The rest i don’t understand, it might just be a language barrier.

with out any external commands you can save the output in usr using read

read -r usr < <(who -u)

Using PE to extract the first field.

read -r usr < <(who -u); echo "${usr%% *}"

This is a good idea " usr=$(nice who -u | awk ‘$2 ~ /:0/{print $1}’) " .

But is a short pipe,a good hacker atempt to kill a command by disturbing the acess to the ram , then user going to be come root
and hacker atempt to crack xterm .

the command nice at start of evry line chenge the time of execution and hacker cannot syncronize using sleep as start

Last linux kernel working by TMPFS filesystem ,I think the hugepage they could be used .

You need 3 command pipe to create a command so difficult to kill or break .
Think a pipes about three commands and propose it to me .

Explain the solution do you think about the bug

If the system did not shutdown properly then not even a trap at the beginning of your script could help imo

thanks a lot for help

But awk is in all shell ? dash sh …

awk is not a shell but it is its own (scripting) language, like python,perl, ruby etc.

awk is not a shell but i…
Right awk is a stand alone command with is owen package rpm .
My script atempt to use only unix shell commands, if you think a pipe only by shell commands is a best help for me.

Btw, there is

http://www.suseitalia.org/

if there is no italian forum in here, you might have better communication in there.

Sorry for may bad english i use it no offen, be patience , but i think this is right place for my script .
I dont’ use awk becouse on some systemis is not installed bat i think is an good idea…

Hey, I’m not a native English speaker as well and im sure folks in the forum noticed that already because of my wrong grammar and spelling :-), but i don’t know Italian so we cannot have a good communication.

Thank you for bearing me .
Italian is not simple but not even the German :slight_smile:
The main problem of this script is where put a copy and paste to restore fstab in case it is with errors at restart of computer .

Any way this is my fstab

LABEL=Msi_Root           /                    ext4       acl,user_xattr        1 1
LABEL=Msi_Home           /home                ext4       acl,user_xattr        1 2
LABEL=Msi_swap       swap                 swap       defaults              0 0

This is what i am trying to say about the trap

#!/bin/bash
trap ' -f fstab1 ]] && mv fstab1 fstab' 2

cp /etc/fstab .
cp /etc/fstab fstab1

printf '%s
' 'g/Msi/s//Hp/g' w | ed -s fstab

sleep 10

If you run this script it and let it finish the 10 seconds wait, it will replace all occurrence of Msi with Hp in my copy of fstab but if you do a CTR+C before the script gave you your shell back the trap is triggered. So the edited file is replaced by the fstab1 which is the default value of my /etc/fstab.

The number 2 is the signal being captured by the script which is the signal for CTR+C (control key and the c key). To find out about the signals run

man 7 signal

Imo if the system will not shutdown properly (like what i have said) the trap will not be any help in this case.
You could probably create a startup daemon and do the moving of the default fstab in there. (If it is possible)

You can try it out on your own.

first you can probably go in /tmp

cd /tmp

Then create the file below and name it fstab


LABEL=Msi_Root           /                    ext4       acl,user_xattr        1 1
LABEL=Msi_Home           /home                ext4       acl,user_xattr        1 2
LABEL=Msi_swap       swap                 swap       defaults              0 0

Now create the script and this time name it foo

#!/bin/bash
trap ' -f fstab1 ]] && mv fstab1 fstab' 2

cp fstab fstab1

printf '%s
' 'g/Msi/s//Hp/g' w | ed -s fstab

sleep 10

make it executable

chmod +x foo

Now first run it and let it finish the sleep 10 which is 10 seconds pause/delay,

./foo

then check the fstab file or run diff between the two.

diff fstab fstab1
diff fstab fstab1
1,3c1,3
< LABEL=Hp_Root           /                    ext4       acl,user_xattr        1 1
< LABEL=Hp_Home           /home                ext4       acl,user_xattr        1 2
< LABEL=Hp_swap       swap                 swap       defaults              0 0
---
> LABEL=Msi_Root           /                    ext4       acl,user_xattr        1 1
> LABEL=Msi_Home           /home                ext4       acl,user_xattr        1 2
> LABEL=Msi_swap       swap       

Now copy fstab1 to fstab

cp fstab1 fstab

Next run the script and do the CTR+C before it is finished,

jetchisel@localhost:/tmp> ./foo
^Cjetchisel@localhost:/tmp> 

the fstab is reverted back to Msi. I hope that helps :slight_smile:

By the way you can put more than one signal as long as it is a valid signal from man 7 signal

trap ' -f fstab1 ]] && mv fstab1 fstab' 2 3 15