-1

Is it possible to start the mc-wrapper with sudo and still use the last selected directory on the console when quitting sudo mc (requirement number 4)? My default alias looks like this.

alias mc='EDITOR="${EDITOR-mcedit}" . /usr/lib/mc/mc-wrapper.sh'

Some errors (for the Googlers)

sudo: mc: command not found
sudo: .: command not found  # "." == "source"

My requirements

  • Ubuntu 18.04.1.
  • The alias should work with and without sudo call.
  • If possible, a single alias for mc in /etc/bash.bashrc for all users.
  • The directory you changed to with sudo mc should be "preserved" after closing the Midnight Commander. This means that you will not be in the same directory as you started sudo mc (provided it is not the same directory).

Optional requirements

  • See if the alias was started with super powers.
  • See if the alias was started with sudo.
  • If the alias mc was started without super powers or sudo, ask if the program mc should still be started with sudo rights.
  • If the question is answered No, use my default alias.
  • In all other cases, the first four requirements should be met.
  • The editor (e.g. mcedit or vi) within mc should be selectable via another alias like mcvi (for vi) without code duplication.
  • Arguments should be passed on to the program, like sudo mc /opt/ /mnt/
qräbnö
  • 2,722
  • 27
  • 40
  • 1
    A vote down without leaving a comment is like no vote down. I think this question helps me (if I have the same problem in the future ;)) and therefore most likely others. – qräbnö Nov 29 '18 at 15:13
  • `sudo` requires an real executable, not an alias or shell function. – chepner Nov 29 '18 at 15:44
  • 1
    No, see my answer. – qräbnö Nov 29 '18 at 15:45
  • You cannot preserve directory changes made by `mc`, short of saving the path to the current directory in a file before exiting `mc`, then having your script re-read that file to change the directory after exiting `mc`. – chepner Nov 29 '18 at 15:46
  • 1
    No, see my answer. ;) – qräbnö Nov 29 '18 at 15:46
  • Your answer builds an alias that runs `sudo` on a temporary script. – chepner Nov 29 '18 at 15:48
  • Is that illegal? ;) – qräbnö Nov 29 '18 at 15:49
  • It's usually better to work *with* the system than against it. – that other guy Nov 29 '18 at 17:17
  • I didn't invent sudo, nor aliases, functions, mc and Ubuntu. One or the other assumes that Windows is rubbish, but in this case this also applies to Linux. Probably this will be closed now, because too chatty. Well, then I benefit from my own work and will not share it in the future. – qräbnö Nov 29 '18 at 17:43

1 Answers1

0

Here's one hacky solution (tested, but the last two optional requirements are still missing).

/etc/bash.bashrc

alias sudo='sudo '  # fixes "sudo: mc: command not found" (breaks with an argument: sudo -H ll)

# sudo apt update && sudo apt install dialog mc pwgen
#
# Start the alias with a "real" command (instead of a shell keyword like "if") so that sudo is not confused.
# This first command (env) will eat sudo and all arguments! Even the following file redirection including the angle bracket is already executed without sudo.
alias mc='env > "/tmp/mc.env.$(whoami).$$"
MC_USER="$(whoami)"
MC_ENV_FILE="/tmp/mc.env.${MC_USER}.$$"
# cat "${MC_ENV_FILE}"
if [ "$(cat "${MC_ENV_FILE}" | grep "$(id -nu 0)" | wc -l)" -gt "3" ]; then
    # echo "This alias was called with super powers."
    MC_ROOT="root"
fi
if [ "$(cat "${MC_ENV_FILE}" | grep "^SUDO_" | wc -l)" -gt "3" ]; then
    # echo "This alias was called with sudo (possibly sudo -i or sudo -s was entered before)."
    MC_SUDO="sudo"
fi
if [ "${MC_ROOT}" == "root" ] || [ "${MC_SUDO}" == "sudo" ]; then
    MC_DIALOG="0"
else
    # echo "This alias was called as normal user."
    dialog --cr-wrap --defaultno --title "sudo mc" --yesno "Do you want super powers (sudo/root)?\n\n(Alternatively you can use \"sudo mc\" directly next time.)\n\nAgain: Do you want super powers (sudo/root)?" 9 64
    MC_DIALOG="$?"
    tput reset
fi
if [ "${MC_DIALOG}" != "0" ]; then
    # echo "No, do not use sudo and stay normal user."
    # echo "Standard wrapper (without arguments)..."
    EDITOR="${EDITOR-mcedit}" . /usr/lib/mc/mc-wrapper.sh  # does not work with sudo because "." is not a program like "ls" or "grep"!
else
    # echo "Yes, exec those decisive commands with super powers."
    # echo "Custom wrapper (also without arguments)..."
    MC_PWD_FILE_DIRNAME="${TMPDIR-/tmp}/mc-${MC_USER}/"
    MC_PWD_FILE="${MC_PWD_FILE_DIRNAME}mc.pwd.$$.$(pwgen 13 1)"
    sudo mkdir -p "$MC_PWD_FILE_DIRNAME"
    sudo chown "$(sudo whoami)":"$(sudo whoami)" "$MC_PWD_FILE_DIRNAME"
    sudo chmod 0700 "$MC_PWD_FILE_DIRNAME"
    sudo EDITOR="${EDITOR-mcedit}" /usr/bin/mc -P "$MC_PWD_FILE"
    sudo chown -R "$MC_USER":"$MC_USER" "$MC_PWD_FILE_DIRNAME"
    if test -r "$MC_PWD_FILE"; then
        MC_PWD=$(cat "$MC_PWD_FILE")
        if test -n "$MC_PWD" && test -d "$MC_PWD"; then
            cd "$MC_PWD"
        fi
        unset MC_PWD
    fi
    rm -f "$MC_PWD_FILE"
    unset MC_PWD_FILE
    unset MC_PWD_FILE_DIRNAME
fi
unset MC_DIALOG
unset MC_SUDO
unset MC_ROOT
rm -f "${MC_ENV_FILE}"
unset MC_ENV_FILE
unset MC_USER
# This terminating line break is required:
'

I didn't manage to use a function mcwrapper (and $(declare -f mcwrapper)) and I don't think it's that easy either!?

qräbnö
  • 2,722
  • 27
  • 40