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

Using su can cause $XDG_* vars to get set to subfolders of /root, which messes up Znap initialization #183

Open
pedromarquetti opened this issue Jul 1, 2022 · 18 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@pedromarquetti
Copy link

pedromarquetti commented Jul 1, 2022

Describe the bug

I just started using znap, followed the basic dotfiles usage for my (phlm) and root users, but when I $ sudo su then # su phlm, some "zf_mkdir" errors appear, followed by an infinite autocomplete loop

Steps To Reproduce

sudo su
su <your user>

Expected behavior

Normal switch between root user and my user with sudo su and su <my user>

Observed behavior

Currently, when I su <my user> after doing sudo su, apparently my shell is trying to mkdir /root/.local (even though I just switched to my non-root user with su), see log below

Click to expand

phlm% whoami
phlm
phlm% sudo su
[sudo] password for phlm: 
phlm# whoami
root
phlm# su phlm
init.zsh:zf_mkdir:44: cannot make directory '/root/.local': permission denied
init.zsh:zf_mkdir:44: cannot make directory '/root/.cache': permission denied
init.zsh:zf_mkdir:44: cannot make directory '/root/.cache': permission denied
init.zsh:zf_mkdir:44: cannot make directory '/root/.config': permission denied
init.zsh:zf_mkdir:44: cannot make directory '/root/.local': permission denied
init.zsh:zf_ln:61: /home/phlm/Git/zsh-snap/functions/_znap: permission denied
.autocomplete.__init__:zf_mkdir:81: cannot make directory '/root/.local': permission denied
.autocomplete.__init__:zf_mkdir:85: cannot make directory '/root/.local': permission denied
.autocomplete.__init__:zf_mkdir:103: cannot make directory '/root/.cache': permission denied
.autocomplete.async.start:6: permission denied: /root/.local/share/zsh-autocomplete/log/2022-07-01.async.log
.autocomplete.async.complete.fd-widget:3: permission denied: /root/.local/share/zsh-autocomplete/log/2022-07-01.log

... infinite loop ...

Environment:

  • OS: Pop!_OS 22.04 LTS x86_64
  • Terminal: Tilix
  • Zsh version: zsh 5.8.1 (x86_64-ubuntu-linux-gnu)
  • Repo version: main de2e7c6

Additional context

I used Oh My Zsh previously and it was working fine with only zsh-autocomplete, zsh-autosuggestions and zsh-syntax-highlighting, I wanted to change to zsnap for being light-weight.
I'll include my zshrc, but I changed it multiple times, still the same result...

My .zshrc

# Download Znap, if it's not there yet.
[[ -f ~/Git/zsh-snap/znap.zsh ]] ||
    git clone --depth 1 -- \
        https://github.com/marlonrichert/zsh-snap.git ~/Git/zsh-snap

source ~/Git/zsh-snap/znap.zsh # Start Znap

znap source automatically downloads and starts your plugins.

znap source marlonrichert/zsh-autocomplete
znap source zsh-users/zsh-autosuggestions
znap source zsh-users/zsh-syntax-highlighting

Am I doing something wrong?
@pedromarquetti pedromarquetti added the bug Something isn't working label Jul 1, 2022
@marlonrichert
Copy link
Owner

Looks like, when you do su <user> either

  • $HOME is set to /root or
  • $XDG_CACHE_HOME, $XDG_CONFIG_HOME and $XDG_DATA_HOME are set to /root/<subdir>

and when using su <user>, /root is not writable and /home/<user>/Git/zsh-snap is not readable.

@marlonrichert
Copy link
Owner

Can you try doing su -m user? Does that solve the problem?

@pedromarquetti
Copy link
Author

Can you try doing su -m user? Does that solve the problem?

It does solve the problem, it doesn't try to write to /root/, but then znap + auto-complete and auto-suggestions is not loaded.

@marlonrichert
Copy link
Owner

Does it give any error messages?

@marlonrichert marlonrichert added the question Further information is requested label Jul 3, 2022
@pedromarquetti
Copy link
Author

pedromarquetti commented Jul 3, 2022

No. It only shows the default phlm% prompt.
Do you think it's some misconfiguration? I could also send some log files (the ones that were able to be written)

Also, just to add, if I comment out znap source marlonrichert/zsh-autocomplete from .zshrc, the infinite loop doesn't happen, but it still gives the permission denied error that I mentioned

@marlonrichert
Copy link
Owner

Before doing su, can you put set -vx in your .zshrc file (before sourcing Zsh Autocomplete) and share the output here in a file?

@pedromarquetti
Copy link
Author

Ok, so I don't understand why set -xv >/tmp/zsh.log is not working when doing sudo su > su phlm , I had to manually copy my terminal.

The log i got after doing su with zsh-autocomplete, there's an long loop there, i had to su phlm to stop it.
zsh-autocomplete.log

The log I got after I ran su without zsh-autocomplete sourced:
zshrc.log

Hope this helps

@marlonrichert
Copy link
Owner

Your zsh-autocomplete.log is 0 KB. Did your upload fail somehow?

@marlonrichert
Copy link
Owner

What happens if, instead of using znap source to start Zsh Autocomplete, you use just source?

@pedromarquetti
Copy link
Author

Your zsh-autocomplete.log is 0 KB. Did your upload fail somehow?

Now it uploaded correctly zsh-autocomplete.log, I did su phlm at the end to stop the loop.

What happens if, instead of using znap source to start Zsh Autocomplete, you use just source?

If I do source ~/Git/zsh-autocomplete/zsh-autocomplete.plugin.zsh, same loop occurs

@marlonrichert
Copy link
Owner

Can you comment out most of the things in your .zshrc and leave only the minimum that’s necessary to reproduce the bug? Then share your debug output here again.

@pedromarquetti
Copy link
Author

pedromarquetti commented Jul 4, 2022

What is "XDG_CACHE_HOME", "XDG_CONFIG_HOME" and "XDG_DATA_HOME" used for? I added

export XDG_CACHE_HOME=$HOME/.cache
export XDG_CONFIG_HOME=$HOME/.config
export XDG_DATA_HOME=~/.local/share

At the beginning of my .zshrc and no errors or infinite loops occur

@marlonrichert
Copy link
Owner

marlonrichert commented Jul 5, 2022

OK, thanks. That’s a good clue.

In your .zshrc, before setting or exporting those, can you add

print -lr "${(q+)HOME}" "${(q+)XDG_CACHE_HOME}" "${(q+)XDG_CONFIG_HOME}" "${(q+)XDG_DATA_HOME}"

and share the output here?

@pedromarquetti
Copy link
Author

/home/phlm
''
''
''
~ $ sudo su                                                                         
[sudo] password for phlm: 
~ # su phlm                                                                
/home/phlm
/root/.cache
/root/.config
/root/.local/share                                                                            

@marlonrichert marlonrichert self-assigned this Jul 11, 2022
@marlonrichert
Copy link
Owner

OK, so, for some reason, when you expand your user's privileges with su, then $XDG_CACHE_HOME, $XDG_CONFIG_HOME and $XDG_DATA_HOME get set. You need to disable this somehow.

Can you check the following files and see if they get set somewhere in there?

/etc/zshenv
/etc/zprofile
/etc/zshrc
/etc/zlogin
/etc/zlogout

@marlonrichert marlonrichert changed the title Problem setting up zsh-snap, am I being stupid? $XDG_* vars get set to subfolders of /root when using su Nov 14, 2022
@marlonrichert marlonrichert changed the title $XDG_* vars get set to subfolders of /root when using su Using su can cause $XDG_* vars to get set to subfolders of /root, which messes up Znap initialization Nov 14, 2022
@marlonrichert marlonrichert added documentation Improvements or additions to documentation and removed bug Something isn't working labels Nov 14, 2022
@pedromarquetti
Copy link
Author

Zsh has its own zsh folder inside /etc/, with

-rw-r--r-- 1 root root  344 Feb 12  2022 zlogin
-rw-r--r-- 1 root root   58 Feb 12  2022 zlogout
-rw-r--r-- 1 root root  264 Feb 12  2022 zprofile
-rw-r--r-- 1 root root  623 Feb 12  2022 zshenv
-rw-r--r-- 1 root root 3900 Feb 12  2022 zshrc

all of the files insite /etc/zsh are unchanged, and XDG variables do not get set in any of them

@marlonrichert
Copy link
Owner

marlonrichert commented Jan 7, 2023

What needs to be done is

  1. For each XDG dir, check if it is writable for us.
  2. If not, set it to the default value instead.
  3. If the default value is not writable to us either, then bail out and show an informative error.

Pull requests welcome. 🙂

@marlonrichert marlonrichert added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers and removed documentation Improvements or additions to documentation question Further information is requested labels Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants