Mageia Bugzilla – Attachment 9341 Details for
Bug 19026
sshd no longer sets DISPLAY variable (with X11UseLocalhost workaround)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
New Account
|
Forgot Password
color prompt script
99prompt.sh (text/plain), 9.01 KB, created by
Pierre Fortin
on 2017-05-26 23:06:53 CEST
(
hide
)
Description:
color prompt script
Filename:
MIME Type:
Creator:
Pierre Fortin
Created:
2017-05-26 23:06:53 CEST
Size:
9.01 KB
patch
obsolete
>#!/bin/bash -x ># -*- mode: sh; -*- > ># Copyright: Pierre Fortin, 2007-present <pf@pfortin.com> ># License: GPLv3 > >##################################################################### > ># This script sets the user prompt according to various conditions. ># You can change settings and colors for an individual user by copying ># /etc/profile.d/99prompt.rc to ~/.USERprompt.rc and changing to suit. ># ># NOTES: ># 1. when invoked via "su" (not "su {-,--login}": USER=caller & UID=root ># 2. If you experience problems, set prompt_DEBUG=1 (in ># /etc/profile.d/99prompt.rc or ~/.99prompt.rc ># and email the output (or a patch :) to the author, along with ># a desciption of what you were trying to do, plus what happened ># vs. what you expected. > >###################################################################### ># Based on article at: ># http://www-106.ibm.com/developerworks/linux/library/l-tip-prompt/ ># color prompt (root=red; users=cyan) > ># 11/21/2007 Complete re-write to better determine how we got here. ># 01/24/2010 Minor tweaks and better comments. ># 09/03/2010 Fixed some comments and minor bugs ># 09/03/2010 Added "wrapped" prompt capability -- this allows for ># a 2-line prompt with the full path and just the time ># at which the PREVIOUS command finished in front of the ># {$|#} prompt -- this allows for more space for longer ># commands without wrapping the command line. ># 01/03/2011 More tuneups. Better documented colors. ># Discovered a DoS from using 'exit' instead of 'return' ># which is allowed outside functions if the script is executed ># during a . (source) command. ># 06/06/2014 fix for empty RMTNAME ># 11/09/2015 more documentation ># 07/04/2016 fix bug when using "su" v. "su -" ># add option to display date (only if time also enabled) ># 07/22/2016 for the past couple of days, environment variable DISPLAY ># is no longer set. Causes firefox, USERprompt.sh, etc to fail. ># Added warning message if DISPLAY is not set. ># 07/23/2016 sigh... now $USER is not set to root if invoked with "su" ># "su -" does set USER=root ># 07/23/2016 variables are poluting the user space -- prefix most variables ># with "prompt_", then unset them at end ># 07/23/2016 use new colors for "su" v. "su -" ># 07/23/2016 simplified logging since logfile name contains date ># moved LOGGING & DUMP to .rc ># 07/30/2016 major redesign of which source information is used ># to build prompt. ># 08/01/2016 added error & debug code > >####################################################################### ># Workaround for $0 not set to script name ($0 contains "-bash") >prompt_OURPATH="/etc/profile.d" ># Using "USER" as prefix to non-system profile stuff... >prompt_SCRIPTname="99prompt.sh" >#prompt_COLORMAP="${prompt_OURPATH}/USERprompt2.colormap" >prompt_RC="99prompt.rc" # also used for ~/.99prompt.rc > >###################################################################### ># Get colormap -- now incorporated in .rc file > >###################################################################### ># Get user's color choices > >[ -r "${prompt_OURPATH}/${prompt_RC}" ] && \ > . "${prompt_OURPATH}/${prompt_RC}" # system defaults >[ -r "~/.${prompt_RC}" ] && \ > . "~/.${prompt_RC}" # user overrides >[ -z "PROMPT" ] && { > echo "FATAL: [.]${prompt_RC} did not set prompt string!" > return 1 >} > >###################################################################### ># Collect information about our connection... > >prompt_getcolors () { > # [using a function to allow localizing variables] > # build color variables with: > # INSTANCE LOCATION HOW USR MODE > # PROMPT_{REAL,SSH}_{LOCAL,REMOTE}_{INIT,SU}_{ROOT,USER}_{NOLOGIN,LOGIN} > # start with defaults > > # When we become ROOT, we lose the information which tells us how we got here; > # so we have to collect it from our parent PID ($PPID) > local CMD=$(ps -p $PPID -o cmd --no-headers) > > local INSTANCE="REAL" # REAL{KDEINIT,XTERM,?} | SSH > local LOCATION="LOCAL" # LOCAL | REMOTE > local HOW="INIT" # INIT | SU > local USR="USER" # USER | ROOT > local MODE="NOLOGIN" # NOLOGIN | LOGIN > > [ $prompt_DEBUG -eq 0 ] || set -x # start debugging output > > # determine initial login > case $CMD in > kdeinit*) > MODE="LOGIN" > ;; > #xterm*) ;; # handle these in catchall (*) > ssh*) > INSTANCE="SSH" > ;; > "su"*) # su [-|--login] > HOW="SU" > ;; > *) ;; # use initial settings for kdeinit*, xterm, ... > esac > # anything but ":D.S" is likely ssh > [ ".${DISPLAY:0:1}" == ".:" ] || INSTANCE="SSH" > if [ -n "${SSH_CONNECTION}" ]; then > INSTANCE="SSH" > if [ ".${SSH_CONNECTION[0]:0:3}" == ".127" ]; then > LOCATION="LOCAL" > else > LOCATION="REMOTE" > fi > else > #LOCATION="LOCAL" > [ ".$USER" == ".root" -o $SHLVL -eq 1 ] && { > MODE="LOGIN" >#BUG LOCATION="REMOTE" # this may be reset to local if IP=127.0.0.1 > # Substitute User (aka su) > # Using "su" (without -|--login) results in: > # $USER=empty $USERNAME=caller $UID=0 > # FIXME: add "[caller]" to prompt string...? > } > fi > # root? USER may not be "root" > [ $UID -eq 0 ] && { > USR="ROOT" > # but; if "root", "su -|--login" was used > [ ".$USER" == "root" ] && MODE="LOGIN" > } > # put all the pieces together > PROMPT_COLOR="PROMPT_${INSTANCE}_${LOCATION}_${HOW}_${USR}_${MODE}[@]" > > [ $prompt_DEBUG -eq 0 ] || set +x # stop debuggin output >} > >prompt_getcolors # call the main function > >###################################################################### ># Picking a color that is readable with both black and white backgrounds ># is not easy. Results vary with hardware and user's eyesight. > ># While it is tempting to exit if PROMPT is empty; keep going and unset ># all our variables to avoid namespace polution >#[ ${#PROMPT[@]} ] || exit # nothing to do if no prompt string(s) > ># Check for, and warn about a problem that is rarely seen >if [ -z "${DISPLAY}" ]; then > cat <<EOF >WARNING: Environment variable DISPLAY is empty; many applications/services > may fail in weird ways. e.g., X11 over ssh will have no display. >EOF >fi > >## Determine our mode based on how we got here... > ># "su --login" is recommended over "su -" > ># DISPLAY is representative -- numbers may differ ># IP = 127.0.0.1 for localhost; any other IP indicates remote host ># UDT = user@domain.tld ># SSH* refers to $SSH_CONNECTION ># USRN refers to $USERNAME > ># CMD DISPLAY SSH* SHLVL USER USRN UID ># --------------- -------- -- ----- ---- ---- ---- ># [konsole] :0.0 2 user 1000 ># su :0.0 3 user root 0 ># su --login :0.0 1 root 0 ># su user :0.0 3 user 1000 ># su --login user :0.0 1 user 1000 ># ># The following starts with an ssh connection; then using su... ># ># ssh [user@]host UDT:10.0 IP 1 user 1000 ># su UDT:10.0 IP 2 user root 0 ># su --login UDT:10.0 ** 1 root 0 ># su user UDT:10.0 IP 2 user 1000 ># su --login user UDT:10.0 ** 1 user 1000 ># ># ** No idea why an IP address is not available in "su --login" mode > ># Messy enough...? > ># In case more debugging information is needed, here is some used ># during the development of this script... >#echo "DISPLAY = $DISPLAY" >#echo "SSH_CONNECTION = ${SSH_CONNECTION}" >##echo "XTERM = $XTERM" >#echo "SHLVL = $SHLVL" >#echo "USER=$USER - USERNAME=$USERNAME - UID=$UID EUID=${EUID}" >#echo "$(ps $PPID)" > ># PROMPT (from .rc file) ># colors make [..] brackets redundant >prompt_COLOR=(${!PROMPT_COLOR}) ># extract the pieces >prompt_MODE=${prompt_COLOR[0]} >prompt_FG=${prompt_COLOR[1]} >prompt_BG=${prompt_COLOR[2]} > ># Set a default prompt to indicate problems... >[ -z "${prompt_BG}" ] && { > prompt_MODE=7 # reverse > prompt_FG=93 # light yellow > prompt_BG=100 # dark gray > PROMPT=( \ > "Design flaw in ${prompt_OURPATH}/${prompt_SCRIPTname} script; or\n" \ > "bad color variable(s) in ${prompt_OURPATH}/${prompt_RC} or\n" \ > "${HOME}/.${prompt_RC} resource file.\n" \ > "** See debug comments in script.${prompt_OFF}\n\n" \ > "${PROMPT[@]}") >} > ># setup color {pre,post}ambles >prompt_ESC='\033[' >prompt_RST="${prompt_ESC}0m" > >prompt_ON="\[${prompt_ESC}${prompt_MODE};${prompt_FG};${prompt_BG}m\]" >prompt_OFF="\[${prompt_RST}\]" > ># All this work just to export PS1: > >[ $prompt_DEBUG -eq 0 ] || set -x # start debugging output > >PS1="${prompt_RST}" >if [ ${#PROMPT[@]} -eq 1 ]; then # one string > PS1="${prompt_ON}${PROMPT[0]}${prompt_OFF}" >else # multiple strings > # process each prompt string > # quotes required to prevent tokenizing strings: > for prompt_string in "${PROMPT[@]}"; do > PS1="${PS1}${prompt_ON}${prompt_string}${prompt_OFF}" > done >fi > >[ $prompt_DEBUG -eq 0 ] || set +x # stop debuggin output > ># unset our variables & functions >unset ${!prompt_*} ># unset variables from .rc >unset PROMPT >unset ${!PROMPT_REAL*} >unset ${!PROMPT_SSH*} >unset ${!mode*} >unset ${!fg*} >unset ${!bg*}
#!/bin/bash -x # -*- mode: sh; -*- # Copyright: Pierre Fortin, 2007-present <pf@pfortin.com> # License: GPLv3 ##################################################################### # This script sets the user prompt according to various conditions. # You can change settings and colors for an individual user by copying # /etc/profile.d/99prompt.rc to ~/.USERprompt.rc and changing to suit. # # NOTES: # 1. when invoked via "su" (not "su {-,--login}": USER=caller & UID=root # 2. If you experience problems, set prompt_DEBUG=1 (in # /etc/profile.d/99prompt.rc or ~/.99prompt.rc # and email the output (or a patch :) to the author, along with # a desciption of what you were trying to do, plus what happened # vs. what you expected. ###################################################################### # Based on article at: # http://www-106.ibm.com/developerworks/linux/library/l-tip-prompt/ # color prompt (root=red; users=cyan) # 11/21/2007 Complete re-write to better determine how we got here. # 01/24/2010 Minor tweaks and better comments. # 09/03/2010 Fixed some comments and minor bugs # 09/03/2010 Added "wrapped" prompt capability -- this allows for # a 2-line prompt with the full path and just the time # at which the PREVIOUS command finished in front of the # {$|#} prompt -- this allows for more space for longer # commands without wrapping the command line. # 01/03/2011 More tuneups. Better documented colors. # Discovered a DoS from using 'exit' instead of 'return' # which is allowed outside functions if the script is executed # during a . (source) command. # 06/06/2014 fix for empty RMTNAME # 11/09/2015 more documentation # 07/04/2016 fix bug when using "su" v. "su -" # add option to display date (only if time also enabled) # 07/22/2016 for the past couple of days, environment variable DISPLAY # is no longer set. Causes firefox, USERprompt.sh, etc to fail. # Added warning message if DISPLAY is not set. # 07/23/2016 sigh... now $USER is not set to root if invoked with "su" # "su -" does set USER=root # 07/23/2016 variables are poluting the user space -- prefix most variables # with "prompt_", then unset them at end # 07/23/2016 use new colors for "su" v. "su -" # 07/23/2016 simplified logging since logfile name contains date # moved LOGGING & DUMP to .rc # 07/30/2016 major redesign of which source information is used # to build prompt. # 08/01/2016 added error & debug code ####################################################################### # Workaround for $0 not set to script name ($0 contains "-bash") prompt_OURPATH="/etc/profile.d" # Using "USER" as prefix to non-system profile stuff... prompt_SCRIPTname="99prompt.sh" #prompt_COLORMAP="${prompt_OURPATH}/USERprompt2.colormap" prompt_RC="99prompt.rc" # also used for ~/.99prompt.rc ###################################################################### # Get colormap -- now incorporated in .rc file ###################################################################### # Get user's color choices [ -r "${prompt_OURPATH}/${prompt_RC}" ] && \ . "${prompt_OURPATH}/${prompt_RC}" # system defaults [ -r "~/.${prompt_RC}" ] && \ . "~/.${prompt_RC}" # user overrides [ -z "PROMPT" ] && { echo "FATAL: [.]${prompt_RC} did not set prompt string!" return 1 } ###################################################################### # Collect information about our connection... prompt_getcolors () { # [using a function to allow localizing variables] # build color variables with: # INSTANCE LOCATION HOW USR MODE # PROMPT_{REAL,SSH}_{LOCAL,REMOTE}_{INIT,SU}_{ROOT,USER}_{NOLOGIN,LOGIN} # start with defaults # When we become ROOT, we lose the information which tells us how we got here; # so we have to collect it from our parent PID ($PPID) local CMD=$(ps -p $PPID -o cmd --no-headers) local INSTANCE="REAL" # REAL{KDEINIT,XTERM,?} | SSH local LOCATION="LOCAL" # LOCAL | REMOTE local HOW="INIT" # INIT | SU local USR="USER" # USER | ROOT local MODE="NOLOGIN" # NOLOGIN | LOGIN [ $prompt_DEBUG -eq 0 ] || set -x # start debugging output # determine initial login case $CMD in kdeinit*) MODE="LOGIN" ;; #xterm*) ;; # handle these in catchall (*) ssh*) INSTANCE="SSH" ;; "su"*) # su [-|--login] HOW="SU" ;; *) ;; # use initial settings for kdeinit*, xterm, ... esac # anything but ":D.S" is likely ssh [ ".${DISPLAY:0:1}" == ".:" ] || INSTANCE="SSH" if [ -n "${SSH_CONNECTION}" ]; then INSTANCE="SSH" if [ ".${SSH_CONNECTION[0]:0:3}" == ".127" ]; then LOCATION="LOCAL" else LOCATION="REMOTE" fi else #LOCATION="LOCAL" [ ".$USER" == ".root" -o $SHLVL -eq 1 ] && { MODE="LOGIN" #BUG LOCATION="REMOTE" # this may be reset to local if IP=127.0.0.1 # Substitute User (aka su) # Using "su" (without -|--login) results in: # $USER=empty $USERNAME=caller $UID=0 # FIXME: add "[caller]" to prompt string...? } fi # root? USER may not be "root" [ $UID -eq 0 ] && { USR="ROOT" # but; if "root", "su -|--login" was used [ ".$USER" == "root" ] && MODE="LOGIN" } # put all the pieces together PROMPT_COLOR="PROMPT_${INSTANCE}_${LOCATION}_${HOW}_${USR}_${MODE}[@]" [ $prompt_DEBUG -eq 0 ] || set +x # stop debuggin output } prompt_getcolors # call the main function ###################################################################### # Picking a color that is readable with both black and white backgrounds # is not easy. Results vary with hardware and user's eyesight. # While it is tempting to exit if PROMPT is empty; keep going and unset # all our variables to avoid namespace polution #[ ${#PROMPT[@]} ] || exit # nothing to do if no prompt string(s) # Check for, and warn about a problem that is rarely seen if [ -z "${DISPLAY}" ]; then cat <<EOF WARNING: Environment variable DISPLAY is empty; many applications/services may fail in weird ways. e.g., X11 over ssh will have no display. EOF fi ## Determine our mode based on how we got here... # "su --login" is recommended over "su -" # DISPLAY is representative -- numbers may differ # IP = 127.0.0.1 for localhost; any other IP indicates remote host # UDT = user@domain.tld # SSH* refers to $SSH_CONNECTION # USRN refers to $USERNAME # CMD DISPLAY SSH* SHLVL USER USRN UID # --------------- -------- -- ----- ---- ---- ---- # [konsole] :0.0 2 user 1000 # su :0.0 3 user root 0 # su --login :0.0 1 root 0 # su user :0.0 3 user 1000 # su --login user :0.0 1 user 1000 # # The following starts with an ssh connection; then using su... # # ssh [user@]host UDT:10.0 IP 1 user 1000 # su UDT:10.0 IP 2 user root 0 # su --login UDT:10.0 ** 1 root 0 # su user UDT:10.0 IP 2 user 1000 # su --login user UDT:10.0 ** 1 user 1000 # # ** No idea why an IP address is not available in "su --login" mode # Messy enough...? # In case more debugging information is needed, here is some used # during the development of this script... #echo "DISPLAY = $DISPLAY" #echo "SSH_CONNECTION = ${SSH_CONNECTION}" ##echo "XTERM = $XTERM" #echo "SHLVL = $SHLVL" #echo "USER=$USER - USERNAME=$USERNAME - UID=$UID EUID=${EUID}" #echo "$(ps $PPID)" # PROMPT (from .rc file) # colors make [..] brackets redundant prompt_COLOR=(${!PROMPT_COLOR}) # extract the pieces prompt_MODE=${prompt_COLOR[0]} prompt_FG=${prompt_COLOR[1]} prompt_BG=${prompt_COLOR[2]} # Set a default prompt to indicate problems... [ -z "${prompt_BG}" ] && { prompt_MODE=7 # reverse prompt_FG=93 # light yellow prompt_BG=100 # dark gray PROMPT=( \ "Design flaw in ${prompt_OURPATH}/${prompt_SCRIPTname} script; or\n" \ "bad color variable(s) in ${prompt_OURPATH}/${prompt_RC} or\n" \ "${HOME}/.${prompt_RC} resource file.\n" \ "** See debug comments in script.${prompt_OFF}\n\n" \ "${PROMPT[@]}") } # setup color {pre,post}ambles prompt_ESC='\033[' prompt_RST="${prompt_ESC}0m" prompt_ON="\[${prompt_ESC}${prompt_MODE};${prompt_FG};${prompt_BG}m\]" prompt_OFF="\[${prompt_RST}\]" # All this work just to export PS1: [ $prompt_DEBUG -eq 0 ] || set -x # start debugging output PS1="${prompt_RST}" if [ ${#PROMPT[@]} -eq 1 ]; then # one string PS1="${prompt_ON}${PROMPT[0]}${prompt_OFF}" else # multiple strings # process each prompt string # quotes required to prevent tokenizing strings: for prompt_string in "${PROMPT[@]}"; do PS1="${PS1}${prompt_ON}${prompt_string}${prompt_OFF}" done fi [ $prompt_DEBUG -eq 0 ] || set +x # stop debuggin output # unset our variables & functions unset ${!prompt_*} # unset variables from .rc unset PROMPT unset ${!PROMPT_REAL*} unset ${!PROMPT_SSH*} unset ${!mode*} unset ${!fg*} unset ${!bg*}
View Attachment As Raw
Actions:
View
Attachments on
bug 19026
: 9341 |
9342
|
9395