mirror of https://framagit.org/kyodev/kyopages.git
102 lines
3.6 KiB
Bash
Executable File
102 lines
3.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
version=24/09/2017
|
|
|
|
# user ayant initié la session graphique, assigne $user_
|
|
# return 1 sur échec identification user, return 2 sur absence home/
|
|
# gestion variable environnement user avec: USER_INSTALL=user script
|
|
f__user(){ # 24/09/2017
|
|
if [ "$USER_INSTALL" ]; then # user_ via variable environnement, moyen d'injecter root
|
|
user_="$USER_INSTALL";
|
|
sortie="variable environnement USER_INSTALL"
|
|
return 0
|
|
fi
|
|
local user_id test root_login
|
|
root_login="$(grep ':0:' /etc/passwd | cut -d':' -f1)" || root_login="root"
|
|
if [ "$SUDO_UID" ]; then
|
|
user_id="$SUDO_UID";
|
|
elif grep -qEo '[0-9]+' <<< "$XDG_RUNTIME_DIR" ; then
|
|
user_id="$(grep -Eo '[0-9]+' <<< $XDG_RUNTIME_DIR | cut -d'/' -f4)"
|
|
elif grep -qEo '[0-9]+' <<< "$XAUTHORITY" ; then
|
|
user_id="$(grep -Eo '[0-9]+' <<< $XAUTHORITY | cut -d'/' -f4)"
|
|
fi
|
|
[ "$user_id" ] && user_="$(grep $user_id /etc/passwd | cut -d ":" -f 1 )"
|
|
if [ "$user_" ] && [ "$user_" != "$root_login" ]; then
|
|
sortie="sortie user_id"
|
|
return 0
|
|
else
|
|
if [ "$SUDO_USER" ] && [ "$SUDO_USER" != "$root_login" ]; then
|
|
user_="$SUDO_USER";
|
|
elif grep -qv 'root' <<< "$(who)"; then
|
|
user_="$(grep -v 'root' <<< $(who) | head -n1 | cut -d ' ' -f1)";
|
|
elif grep -q 'hourly.*get[A-Z].*\.anacrontab.*\.config/anacron/spool' /etc/crontab; then
|
|
user_="$(grep 'hourly.*get[A-Z].*\.anacrontab.*\.config/anacron/spool' /etc/crontab | head -n1 | cut -d' ' -f2)";
|
|
fi
|
|
fi
|
|
sortie="sortie user final"
|
|
if [ -z "$user_" ]; then return 1; fi
|
|
if [ ! -d "/home/$user_" ]; then return 2; fi
|
|
return 0
|
|
}
|
|
|
|
f__type(){
|
|
if [ "$SSH_TTY" ]; then
|
|
type="ssh"
|
|
elif [ "$SUDO_UID" ]; then
|
|
type="sudo"
|
|
elif [ "$XDG_CURRENT_DESKTOP" ]; then
|
|
type="${XDG_CURRENT_DESKTOP,,}"
|
|
fi
|
|
# type+="$(tty -s && echo 'terminal' || echo $(LC_ALL=C tty))"
|
|
if [[ "$TERM" =~ linux ]]; then #debian 9 recovery ou nomodeset TERM=linux
|
|
echo "$TERM"
|
|
fi
|
|
}
|
|
|
|
echo -e "\ndétection user ayant acquit les droits root\n"
|
|
[ $EUID -eq 0 ] || echo -e "merci tester aussi le script en ROOT\n"
|
|
|
|
f__user && retourFUser=0 || retourFUser="$?"
|
|
[ "$retourFUser" -gt 0 ] && echo "houla :("
|
|
[ "$retourFUser" -eq 1 ] && echo -e "user indéterminé, pour contourner, lancer le script avec:\n USER_INSTALL=user $0 \n"
|
|
[ "$retourFUser" -eq 2 ] && echo -e "user détecté, mais pas de home: /home/$user_"
|
|
|
|
echo -e "\tuser détecté: $user_"
|
|
echo -e "\tsortie: $sortie"
|
|
echo -e "\tEUID: $EUID"
|
|
f__type
|
|
echo -e "\ttype: $type\n"
|
|
|
|
f__user_debug(){ # v20/08/2017-4
|
|
|
|
local user_id
|
|
|
|
debug+="user_id01:$SUDO_UID (SUDO_UID) \n";
|
|
debug+="user_id02:$(grep -Eo '[0-9]+' <<< $XDG_RUNTIME_DIR | cut -d'/' -f4) (XDG_RUNTIME_DIR) \n"
|
|
debug+="user_id03:$(grep -Eo '[0-9]+' <<< $XAUTHORITY | cut -d'/' -f4) (XAUTHORITY) \n\n"
|
|
if [ "$SUDO_UID" ]; then
|
|
user_id="$SUDO_UID"
|
|
elif [ "$(grep -Eo '[0-9]+' <<< $XDG_RUNTIME_DIR | cut -d'/' -f4)" ];then
|
|
user_id="$(grep -Eo '[0-9]+' <<< $XDG_RUNTIME_DIR | cut -d'/' -f4)"
|
|
elif [ "$(grep -Eo '[0-9]+' <<< $XAUTHORITY | cut -d'/' -f4)" ];then
|
|
user_id="$(grep -Eo '[0-9]+' <<< $XAUTHORITY | cut -d'/' -f4) (XAUTHORITY)"
|
|
fi
|
|
debug+="user00:$USER_INSTALL (USER_INSTALL) \n";
|
|
debug+="user01:$(grep $user_id /etc/passwd | cut -d ":" -f 1 ) (user_id) \n"
|
|
debug+="user02:$SUDO_USER (SUDO_USER) \n"
|
|
debug+="user03:$(grep -v 'root' <<< $(who) | head -n1 | cut -d ' ' -f1) (who) \n"
|
|
debug+="user04:$(grep 'hourly.*get[A-Z].*\.anacrontab.*\.config/anacron/spool' /etc/crontab | head -n1 | cut -d' ' -f2) (anacrontab) \n"
|
|
|
|
}
|
|
|
|
f__user_debug
|
|
|
|
echo -e "\tdebug:"
|
|
echo -e "$debug"
|
|
exit 0
|
|
|
|
# test crontab
|
|
#*/5 * * * * kyodev /home/<user>/test_user > /home/<user>/cronTestuserUser
|
|
#*/5 * * * * root /home/<user>/test_user > /home/<user>/cronTestuserUser
|
|
|