-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 and commands only in /sbin #592
Comments
with "sudo ifup" where "ifup" is only in "/sbin", "ifup" is colorized in red
Known issue. z-sy-h looks for commands in $PATH, but sudoers(5) can
define an alternative path for root. Both the path value and the directories
specified therein may be unreadable by non-root users, so there's no way to fix
this in the general case. Ideas are welcome though.
|
Can z-sy-h define a specific variable used only with "sudo" code of z-sy-h which contains default path of root's commands (as /sbin). |
Can z-sy-h define a specific variable used only with "sudo" code of
z-sy-h which contains default path of root's commands (as /sbin).
If an user defines an alternative path for root in sudoers, then he can
modify this variable ?
Such users would also need to send the command-path style in the
:completion:*:sudo:* zstyle context. It would be nice to have
completion and z-sy-h both obtain the value from the same place.
|
On my zsh, the completion of "ifup" fails, and the completion of "sudo ifup" successes. I want to install z-sy-h on OS for students in System Administration (Live Raizo), but it's very annoying if it shows a bad information : ifup is correct only behind sudo (if i am not root). And i can't add "/sbin" in the path of user (and it is not realistic) |
/sbin is in users' PATH on FreeBSD. Good luck with your course! |
Sorry, i believe that i was not been clear : I didn't ask how to do the completion on zsh for /sbin.
It is not in Debian, and i think that is a good thing ;-) |
Because that hasn't been implemented. Patches welcome. |
I can try.... |
Great! Let's open a new issue to discuss possible approaches. |
I think to add in "_zsh_highlight_main__type" something as |
-1. It is unacceptable to run sudo from z-sy-h. |
Knowing whether the command word is |
Ok, it's more complicated, but you're right. And with this code ?
|
This is plausible. We can't solve the general case, but a solution that's sound though not complete is worth consideration. |
@Raizo62 Thanks for putting up with my briefer replies that I sent from the mobile. |
Isn't this costly performance wise ? Also, not a big fan of hard coding the path like this, and subtle issues can occur because of the order of PATH entries, so it is seems tricky to get this right. I think the |
Isn't this costly performance wise ? Also, not a big fan of hard coding
the path like this, and subtle issues can occur because of the order of
PATH entries, so it is seems tricky to get this right.
We could make it a bit fancier with a function that locally does path=( "${(@)path/\/bin/\/sbin}" ), runs 'rehash', and copies $commands to another name... but that's still guesswork and heuristics. How about a much KISSier approach: in 'sudo foo' always make 'foo' not highlighted at all. The rationale is that we can't know what sudoers(5) does: it could both remove and add $PATH entries.
I think the `zstyle` approach is the best one (`zstyle ":zsh-syntax-highlighting:*:sudo:*" command-path /bin /sbin ...`). If you are in this situation you already have to hardcode root's PATH in your zshrc for completion anyway.
(To clarify, z-sy-h does not currently honour any zstyles whatsoever, so no one should set that zstyle in their zshrc.)
Sure, that makes sense as a larger / longer-term change.
|
Not often costly, if we call this code only when we use "sudo"
If user adds PATH entries in sudoes, he can also add them in config of z-sy-h It is not a perfect solution, but often a good solution |
Not often costly, if we call this code only when we use "sudo"
You do realize that z-sy-h's main loop gets called for _every single character_ typed at the prompt? That's why we should avoid stat()s in the main loop.
|
Yes. I have tested the code that i have proposed ;-) |
I'm not terribly fond of special casing sudo. Users could also be restricted to particular binaries even with particular arguments. Then there's doas and pkexec that could also be special cased. Saying sudo will be treated just like every other precommand is a clear line, but adding this special case leads to more complexity to become more correct. If we do special case sudo, we should probably use the completion zstyle, so users only have to configure it twice (once for sudo itself too). |
I'm not terribly fond of special casing sudo. Users could also be
restricted to particular binaries even with particular arguments. Then
there's doas and pkexec that could also be special cased.
Saying sudo will be treated just like every other precommand is a clear
line, but adding this special case leads to more complexity to become
more correct.
Then what do you think about the other option I suggested --- making the 'foo' in 'sudo foo' not highlighted at all? This needn't be a special case for sudo; that rule could be applied to other precommands, such as doas(1) and jexec(1).
On the other hand, the behaviour of master is correct for precommands such as nice(1) and moreutils' chronic(1), so perhaps we should have two classes of precommands: those that resolve their argv[0]-to-be argument in the same way this zsh process would, and those that don't --- but only two classes, not N.
?
|
"sudo" is a command very used and z-sy-h will be a great help for users who write commands. Other idea to code :
User must define : export ROOT_PATH="/sbin:/usr/sbin:/usr/local/sbin" |
Could you create a new branch where i can beginning to write a proposition of patch ?
Could you show me how (and where) to do that ? (i can, but i think that will not be very tidy) |
For a branch please use one in your own repository and PR here for reviews. However, please remember we haven't yet agreed what we want the functionality to end up being — whether we'd not highlight the I don't want to discourage you, but I don't want you to burn out or be frustrated either; by all means do feel free to experiment and to sketch out proposals, but remember consensus hasn't yet emerged from this discussion. To figure out whether |
... and I'm glad to see new people being interested in writing code for z-sy-h. :) |
I propose a code here : https://github.com/Raizo62/zsh-syntax-highlighting/tree/sudo If you use z-sy-h as before (without define new varable), you have your old result : if you define the variable ZSH_HIGHLIGHT_SPECIAL_PATH
and i believe that the new code is very efficient :
|
Thanks for writing that! As I said before, we still have to achieve consensus on what to do before we decide how to do it, but having a proof of concept will be helpful. |
Yes, i understand. In your thoughts, don't forget that :
This is a real need. Also, perhaps that z-sy-h can initialize ZSH_HIGHLIGHT_SPECIAL_PATH if it doesn't detect sbin in the PATH of user ? |
I don't understand : If a user doesn't define the variable "ZSH_HIGHLIGHT_SPECIAL_PATH" (it is the default case), then zsh doesn't highlight commands behind "sudo". It is that you want, doesn't you ? |
That is what I proposed, but it's not what your code does. With your code, if the variable is unset, commands after As I said, the next step is for this thread to consense on what the new behaviour should be. You propose that there should be a new parameter that simply modifies PATH. There are a number of questions about that (summarizing from this thread):
Also, your patch would cause /usr/sbin/grep to be called in preference to /usr/bin/grep, if |
This is also what your current code does. With this patch, it can do a little more.
Yes, but, after 3 months, there is no idea, proposition, arguments :-( You can also have a variable with a list of the "special" commands. Zsh will show in green the commands in this list, if user uses "sudo". |
I don't mean to be rude, but I suppose I'd better say it plainly. Your code is not ready to be merged. It doesn't have docs, it doesn't have tests, it doesn't address the concern from the last paragraph of my previous comment, and it doesn't test the bitfield's value correctly. It could form the core of a solution, but it's not there yet.
Would you care to explain why you think that would be better?
Yes, that could work. There's a possible problem here if root's path contains directories that are |
Also, there's the complexity question. Enumerating the root's path is O(N) work where N is the number of binaries in root's path; testing existence of a single binary is O(M log N) where M is the number of directories in root's path… and that's without considering performance of network filesystems. That is: obtaining the list of permitted commands ahead of time might not work performantly in some setups. |
I know. But i don't use my time to write docs or optimize code, if, already, you don't want this idea. For your last paragraph, i see solution : When you will have the problem, you can save the original value of PATH before to modify it. After you can restore it before your "grep".
Because that will help very often (we do very often "sudo cmd"), and mislead very rarely ("sudo -u alice"). In the second case, we can remove the variable ZSH_HIGHLIGHT_SPECIAL_PATH, or change his value (local folder with files with good name)...
The second method doesn't enumerate path. It is only a list of "word". If the first parameter of sudo is in this list then zsh shows it with green. |
Sure.
Sure.
Sorry, but no. It's not good design to require hoop jumping for a common operation that usually requires none.
Are we talking about the same thing? I wasn't asking you to justify the user-settable knob. I was asking you to justify why
Yes, I understood that. It's just that I anticipate people would set that list to |
You can also move the function which detects if a command is known in a new function. This function will do the local PATH.
(Sorry, i don't understand the word "DTRT" or found the correct spell) |
Yes, that has been suggested upthread. It could work, although thought will need to be put into the cache invalidation question (see the fifth bullet in my comment from yesterday).
It's an acronym of "do the right thing".
That's neither here nor there; we aren't talking about how to highlight |
I wish to highlight
Why do you say that |
> This behaviour is exactly why sudo ifup doesn't DTRT by default on Debian derivatives.
Why do you say that `sudo ifup` doesn't do the right thing ?
Because it highlights `ifup` in red...
I wish to highlight `ifconfig` in *green* (behind `sudo`) because the
command `sudo ifconfig` is correct (very often, without "rarely" case
as `sudo -u alice`) : the shell command shows the good result.
But if I do a spell error (`sudo ficonfig`), the command is uncorrect
and zsh can alerte me (with red word) before the shell.
If i have no color behind `sudo`, i must wait the error of the shell
to know that i have do an spelling error.
You don't have to convince me that the green/red distinction is useful. I agree that it would be nice for 'sudo ifup' to highlight 'ifup' in green. However, I'm asking a different question: what algorithm should 'sudo foo' follow to decide what color to highlight 'foo' in when root's path is NOT configured.
|
'sudo foo' must follow the same algorithm as sudo to know what color to use with 'foo'. I am not the good person to answer to this question. I choose the solution which colors correctly more often |
I can add also that unset ZSH_HIGHLIGHT_SPECIAL_PATH can is the case where the user is not in sudoers : then all commands behind sudo (even |
First, Second, the common case, provided that the user has typed |
You may have meant "set to empty" rather than "unset", but your code doesn't distinguish these two cases, and even if it did, I doubt zsh-syntax-highlighting is popular enough that admins will code their sudoers(5) configs into /usr/local/etc/zshrc's setting of your variable. |
Yes, i know. I did think this idea when i was answer you. It is easy to modify the patch to do this.
Perhaps, but if zsh-syntax-highlighting is enough popular to be installed by the "admin", the "admin" can configured it ;-) I propose a boolean variable to color the command after |
No, it won't be red. It will be green, like I already said.
That's what I proposed back in December. I'm glad we agree on something :-) |
I speaked with a system with ZSH_HIGHLIGHT_SPECIAL_PATH (and with unset)
I am not against if i have always a functionality to color the commands behind sudo ;-) |
Yes, and I already said —
That means I regret to say that I find this thread/discussion/collaboration challenging to participate in; moreover, I suspect the feeling's mutual. There are still unanswered design questions¹, but I fear that trying to discuss them would frustrate both of us. I'd like to avoid that outcome, if possible. ¹ For one, shouldn't the actual argument to the |
I believe that you want a miracle solution. Miracle solution because zsh-syntax-highlighting doesn't have access to files like I tried to convince you to accept a partial solution where it is the user who explains to zsh-syntax-highlighting what is the good color. I'm afraid that this functionality stays in standby for a longtime. Already 3 months without progress. I think that a miracle solution will need more more time. At the end, i hoped that you accept to have a "beta-functionality" (beta because no perfect) disabled by default. Ok. It is your code after all. I will try to write a patch which applies our discussion. I will copy-paste it here. |
Then you misunderstood me. As far as I'm concerned, "z-sy-h doesn't have access to sudoers(5)" is a design constraint. What I'd like to do is to find a good solution, within the bounds of the effective constraints.
That part I agree with.
For avoidance of doubt, I never consider whether a fix is complete as a criterion to merging it; I only consider whether it is sound. In other words, I would happily merge a patch that implements only part of the desired behaviour. Your patch, however, fixes one use-case but regresses others.
I look forward to the revised patch. |
|
- Add funtion `zsh_highlight_main__test_type_builtin` also the change of PATH can't modify the behavior of the commands in `zsh_highlight_main__type`
Thanks for the patch!
Please send the same code as a pull request so it's easier to review.
- I wanted that foo in `sudo foo` has normal color if `ZSH_HIGHLIGHT_SPECIAL_PATH` is not exist, but i don't know how to do that.....
Good question.
Right now, it gets highlighted as unknown-token because of line 840 in master, right?
How about adding a new style, "indeterminate"? _zsh_highlight_main__type would return some new value, the loop would map that value to the style [indeterminate] (rather than [unknown-token], and we can make that style have no special highlighting by default. (@phy1729 what do you think?)
```
_zsh_highlight_main__test_type_builtin() {
}
_zsh_highlight_main__type() {
}
So, to summarize, your overall approach is:
- When falling back to the 'type' builtin, run it with a custom PATH;
- Add two new knobs, ZSH_HIGHLIGHT_SPECIAL_PATH and ZSH_HIGHLIGHT_SPECIAL_COMMAND.
Is that an accurate summary?
Thanks again for the patch; I'll have to sleep on it.
|
It is done.
Yes it is. In the description of the pull request, i have put examples to use them. |
I'm all for distinguishing the different meanings of unknown-token. Couple of questions,
I don't see it brought up before (apologies if I missed it), but the current approach would mishighlight other precomamnds (e.g. |
That's a textbook case for zstyle's, isn't it? (in order to simulate an associative array having plain arrays for values) I wonder what to do with multiple precommands (e.g., See also #608. Note to self: upstream, should |
See also workers/45424, "completion: Add **/sbin to PATH when completing commands like sudo". |
This is a special case of #695: once we have the additional styles proposed there, we could easily highlight the command word after |
… and a duplicate of #107. |
Hi
Sorry, i come back for an other problem with sudo
With "sudo ls" , "ls" is colorized correctly with green
with "sudo ifup" where "ifup" is only in "/sbin", "ifup" is colorized in red
The text was updated successfully, but these errors were encountered: