mirror of https://framagit.org/kyodev/kyopages.git
v21/21/2017
This commit is contained in:
parent
cfe210db21
commit
fbfcff310c
|
@ -2,55 +2,120 @@
|
|||
|
||||
contact="IRC freenode ##sdeb ou https://framagit.org/kyodev/kyopages/issues"
|
||||
|
||||
#gnome: XAUTHORITY=/run/user/1000/gdm/Xauthority
|
||||
#gnome sudo: idem & SUDO_UID=1000
|
||||
#mate&xfce: XAUTHORITY=/home/user/.Xauthority
|
||||
|
||||
# user ayant initié la session graphique, assigne $user_, testé avec mate, gnome, xfce. manque KDE
|
||||
# return 1 sur echec ident user, return 2 sur absence home
|
||||
# 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(){ # v17/08/2017-4
|
||||
local user_id
|
||||
if [ "$USER_INSTALL" ]; then user_="$USER_INSTALL"; debug+="user00 "; fi # user_ via variable environnement
|
||||
if [ -z "$user_" ]; then # priorité à $USER_INSTALL
|
||||
if [ "$SUDO_UID" ]; then user_id="$SUDO_UID"; debug+="user01 "; fi # sudo
|
||||
user_id="$(grep -qEo '[0-9]+' <<< "$XAUTHORITY")" || user_id="$(grep -qEo '[0-9]+' <<< $XDG_RUNTIME_DIR)"
|
||||
if [ "$user_id" ]; then user_="$(grep $user_id /etc/passwd | cut -d ":" -f 1 )"; debug+="user02 "
|
||||
else user_="$(echo $XAUTHORITY | cut -d "/" -f 3)"; debug+="user03 "; fi
|
||||
if [ -z "$user_" ] && [ "$LOGNAME" ] ; then user_="$(grep -v 'root' <<< $LOGNAME)"; debug+="user04 "
|
||||
elif [ -z "$user_" ] && [ "$(grep -v 'root' <<< $HOME)" ]; then user_="$(cut -sd / -f 3 <<< $HOME)"; debug+="user05 "
|
||||
elif [ -z "$user_" ]; then user_="$(grep -v 'root' <<< $(who | head -n1 | cut -d ' ' -f1))"; debug+="user06 "
|
||||
f__user(){ # v21/08/2017-4
|
||||
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
|
||||
local root_login="$(grep ':0:' /etc/passwd | cut -d':' -f1)" || local 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
|
||||
if [ -z "$user_" ]; then
|
||||
echo -e "user indéterminé, pour contourner, lancer le script avec:\n USER_INSTALL=user $0 n" \
|
||||
"merci contacter $projet $contact pour corriger ça"
|
||||
return 1
|
||||
fi
|
||||
if [ ! -d "/home/$user_" ]; then
|
||||
echo -e "répertoire '/home/$user_' INEXISTANT"
|
||||
return 2
|
||||
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
|
||||
if [ -n "$DISPLAY" ] && [ -z "$type" ]; then type="$(xprop -root | awk '/KDE_SESSION_VERSION|^_MUFFIN|xfce4|xfce5/')"; fi
|
||||
type+="$(tty -s && echo ' terminal' || echo $(LC_ALL=C tty))"
|
||||
}
|
||||
|
||||
echo -e "\ndétection user ayant acquit les droits root\n"
|
||||
[ $EUID -eq 0 ] || echo -e "merci tester aussi le script en ROOT\n"
|
||||
|
||||
[ $EUID -eq 0 ] || echo -e "vous devez être ROOT pour tester ce script, c'est trop facile sinon ;)\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_"
|
||||
|
||||
f__user || echo -e "échec détection user (debug: $debug)\n"
|
||||
[ "$?" -eq 2 ] && noHome="true"
|
||||
echo -e "\tuser détecté: $user_"
|
||||
echo -e "\tsortie: $sortie"
|
||||
echo -e "\tEUID: $EUID"
|
||||
f__type
|
||||
echo -e "\ttype: $type\n"
|
||||
|
||||
[ "$user_" ] && printf "\tuser détecté: $user_\n\tdebug: $debug\n\tEUID:$EUID\n"
|
||||
f__user_debug(){ # v20/08/2017-4
|
||||
|
||||
if [ "$SSH_TTY" ]; then type="SSH"
|
||||
elif [ "$SUDO_UID" ] ; then type="SUDO"
|
||||
elif [ "$XDG_CURRENT_DESKTOP" ]; then type="$XDG_CURRENT_DESKTOP"; fi
|
||||
if [ -n "$DISPLAY" ] && [ -z "$type" ]; then type="$(xprop -root | awk '/KDE_SESSION_VERSION|^_MUFFIN|xfce4|xfce5/')"; fi
|
||||
printf "\t$type\n"
|
||||
local user_id
|
||||
|
||||
[ "$noHome" == "true" ] && echo "user détecté, mais pas de home: /home/$user_" || echo
|
||||
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
|
||||
|
||||
wget -O test_user https://framagit.org/kyodev/kyopages/raw/master/scripts/test_user
|
||||
chmod +x test_user && ./test_user
|
||||
cron
|
||||
root:
|
||||
sortie: sortie user final
|
||||
type: not a tty
|
||||
user03:kyodev (who)
|
||||
user04:kyodev (anacrontab)
|
||||
|
||||
user:
|
||||
sortie: sortie user final
|
||||
type: not a tty
|
||||
user03:kyodev (who)
|
||||
user04:kyodev (anacrontab)
|
||||
|
||||
terminal
|
||||
root:
|
||||
sortie: sortie user_id
|
||||
type: MATE terminal
|
||||
user_id02:1000 (XDG_RUNTIME_DIR)
|
||||
user01:kyodev (user_id)
|
||||
user03:kyodev (who)
|
||||
user04:kyodev (anacrontab)
|
||||
|
||||
user:
|
||||
sortie: sortie user_id
|
||||
type: MATE terminal
|
||||
user_id02:1000 (XDG_RUNTIME_DIR)
|
||||
user01:kyodev (user_id)
|
||||
user03:kyodev (who)
|
||||
user04:kyodev (anacrontab)
|
||||
|
||||
|
|
Loading…
Reference in New Issue