Hi,
could you please provide a hook for non-root users to execute commands
at ip-up/ip-down, like @reboot of cron provides for machine startup?
The following script is more intended to demonstrate what I mean than
a real example. Its too klugy, and this kind of thing doesn't belong
into crontabs.
#! /bin/bash
SU="su -c"
cd /var/spool/cron/crontabs
# set ulimit here, if you like
# This script runs users' commands at ip-up or ip-down. I wrote this
# because I feel there is no need to let root control who can fetch
# mail / update a homesite / monitor websites / ... Commands in a
# user's crontab are executed by /bin/sh, strippind leading '#@@ip-up ',
# output is mailed if there is any. While crontabs may not be the best
# place to put this, at least this script doesn't create still another
# place with auto-executed commands.
# make this suitable for ip-up and ip-down
case "$0" in
*user-ip-up) EVENT=ip-up ;;
*user-ip-down) EVENT=ip-down ;;
*) echo "usage: .*user-ip-(up|down) read the script" &&
exit 4 ;;
esac
for i in [a-zA-Z0-9]* ; { #primitive "untaint"
# is there such a user?
cut -d ':' -f 1 /etc/passwd |egrep "$i" >/dev/null || continue
# is he allowed to use cron?
if [ -f /etc/cron.allow ] ; then
egrep "$i" /etc/cron.allow >/dev/null ||continue
fi
if [ -f /etc/cron.deny -a ! -f /etc/cron.allow ] ; then
egrep "$i" /etc/cron.deny >/dev/null && continue
fi
# is he making use of this feature?
egrep '^#@@'$EVENT'[ ]+' "$i" >/dev/null || continue
# security for temp files
TMP_EXEC=`tempfile` || exit 5
TMP_MAIL=`tempfile` || exit 5
chmod 0700 $TMP_EXEC
chmod 0600 $TMP_MAIL
chown "$i" $TMP_EXEC $TMP_MAIL
# ignore things like "$MAILTO", "$SHELL", environment for now
# /var is mounted "-o noexec" on my system
# process commands in background: don't let users wait on another
( echo '#! /bin/sh' ;
cat "$i" |
sed -n 's/^#@@'$EVENT'[ ][ ]*\(.*\)/\1/p' ) > $TMP_EXEC
( $SU "/bin/sh $TMP_EXEC" "$i" 2>&1 > $TMP_MAIL
[ -s $TMP_MAIL ] && mail -s "`hostname` $EVENT: `date`" "$i" <$TMP_MAIL
rm -f $TMP_MAIL $TMP_EXEC )&
# finished with user
} # finished with all users
###5user-ip-up
--- Begin /etc/ppp/ip-up.d/0dns-up (modified conffile)
Config file not present or no permissions for access
--- End /etc/ppp/ip-up.d/0dns-up
--- Begin /etc/ppp/ip-down.d/0dns-down (modified conffile)
Config file not present or no permissions for access
--- End /etc/ppp/ip-down.d/0dns-down