Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sudo state detection is working incorrectly #604

Closed
Roang-zero1 opened this issue Apr 3, 2020 · 9 comments
Closed

sudo state detection is working incorrectly #604

Roang-zero1 opened this issue Apr 3, 2020 · 9 comments

Comments

@Roang-zero1
Copy link

The detection of the sudo state of the current terminal is not working as I would think it works from the description.
The current implementation at

local state
if (( P9K_SSH )); then
if [[ -n "$SUDO_COMMAND" ]]; then
state="REMOTE_SUDO"
else
state="REMOTE"
fi
elif [[ -n "$SUDO_COMMAND" ]]; then
state="SUDO"
else
state="DEFAULT"
fi
only shows the sudo state if the shell has been started within a sudo environment that is not a root user.
It doesn't show when a sudo command without password promt is possible.

More details in the related bug in Powerline9k at:

@Roang-zero1 Roang-zero1 changed the title sudo stat detection is working incorrectly sudo state detection is working incorrectly Apr 3, 2020
@romkatv
Copy link
Owner

romkatv commented Apr 3, 2020

SUDO state in p9k is meaningless. It's also meaningless in p10k.

The detection of the sudo state of the current terminal is not working as I would think it works from the description.

Which description?

@Roang-zero1
Copy link
Author

Sorry I meant the description from the powerlevel9k segment.
Which states:

State Meaning
SUDO You are using elevated rights
REMOTE_SUDO You are SSH'ed into the machine and have elevated rights

@romkatv
Copy link
Owner

romkatv commented Apr 3, 2020

That description makes no sense though. "You are using elevated rights" is ROOT state.

Could you tell me what you want to achieve?

@romkatv
Copy link
Owner

romkatv commented Apr 3, 2020

Powerlevel9k docs also have this sentence:

SUDO and REMOTE_SUDO states are also available to show whether the current user or remote user has superuser privileges.

This also makes no sense. Here's how it actually works:

  1. (Both in 9k and 10k) If you have privileges, context segment is in state ROOT.
  2. (Only 10k) If you are connected over ssh, P9K_SSH is 1.

SUDO and REMOTE_SUDO have the same meaning as DEFAULT. I keep these states for backward compatibility but they really have no meaning distinct from DEFAULT.

@Roang-zero1
Copy link
Author

Roang-zero1 commented Apr 3, 2020

I will focus on LOCAL as the REMOTE context is the same just for SSH connection:
My understanding of the states would be the following:

State Meaning Function
DEFAULT You are a normal user You are a normal user; If you try to use a command such as sudo vi <file> you would be promted for a password
ROOT You are the root user You are in a terminal for the user root
SUDO You are using elevated rights You are a normal user; If you try to use a command such as sudo vi <file> the command would be executed as you used sudo before and the terminal is "elevated"

This was the functionality before the changes done in Powerlevel9k/powerlevel9k#937 (but the old functionality had problems with spamming journalctl)

@romkatv
Copy link
Owner

romkatv commented Apr 3, 2020

This was the functionality before

The old implementation was showing sudo if sudo true succeeds without password. It's not the same thing as running from a TTY blessed by sudo. For example, try adding $USER ALL=(ALL) NOPASSWD: /bin/true to sudoers. It won't grant you any real privileges and yet sudo true will succeed without password.

The old implementation had serious issues. If you actually typed sudo cmd and entered your password, powerlevel9k was extending the ticket on every prompt. It was also spamming journal with failed sudo attempts.

AFAIK, there is no portable (or at least not horrible) way to check whether the current TTY is blessed by sudo. If you know how to do this, please share.

@Roang-zero1
Copy link
Author

Roang-zero1 commented Apr 3, 2020

Just some brainstorming I have not tested this method or thought about implementation with zsh/powerlevel10k:

So in effect we could parse the timestamp for our user in our tty (ttydev/ppid) and see if he is authenticated as uid 0 (auth_uid). The only thing I am not sure is how to get the timeout for the timestamp.

@romkatv
Copy link
Owner

romkatv commented Apr 3, 2020

Yep, I know how it works.

The location of the database is different in different distros. On my machine it's in /var/run/utmp. On BSDs it also has a different format.

I'm closing this issue if that's OK with you.

@Roang-zero1
Copy link
Author

Sure, thanks for going into the details

@romkatv romkatv closed this as completed Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants