]>
Commit | Line | Data |
---|---|---|
1 | # NOTE: | |
2 | # our .zprofile can be expensive, so we keep track of what has been run | |
3 | # already, and only set up what is necessary. this also allows us to re-source | |
4 | # the file to reinitialize specific parts when desired. | |
5 | # | |
6 | # in order to ensure future interactive environments are set up as early as | |
7 | # possible, we source this file from .zshenv for non-login shells, under some | |
8 | # specific conditions outlined there. | |
9 | # | |
10 | # this file respects non-interactive sessions and will not intentionally emit | |
11 | # output. | |
12 | ||
13 | ### cleanup | |
14 | # XXX: only call after relevant vars have been set up, defined early so that | |
15 | # below code can utilize it after they do so | |
16 | function _sev_zcleanup { | |
17 | ## gpg forwarding | |
18 | if [[ -d $_sev_gpg_forward_dir && ( -z $1 || $1 == 'gpg-forward' ) ]] { | |
19 | # clean up forward dirs if its session is dead or we ask for it | |
20 | find $_sev_gpg_forward_dir -mindepth 1 -maxdepth 1 -type d | | |
21 | while {read -r x} { | |
22 | # NOTE: the only way we can get here is if we have not been | |
23 | # forwarded before, if the user asks for it, or during | |
24 | # logout. if our own pid already has a dir, it is most likely | |
25 | # stale, the user wants it removed, or something is very | |
26 | # broken—in all 3 of these cases the best choice is remove it. | |
27 | p=$(basename $x) | |
28 | if {[[ -v _sev_gpg_forward_clean || $$ == $p ]] || | |
29 | ! kill -0 $p 2>/dev/null} { | |
30 | find $x -mindepth 1 -maxdepth 1 | while {read -r y} { | |
31 | # XXX: real dirs will stop unlink, consider it a feature | |
32 | unlink $y | |
33 | } | |
34 | # don't force in case something important is still there | |
35 | rmdir $x | |
36 | } | |
37 | } | |
38 | # reset GNUPGHOME if we removed our own dir | |
39 | if [[ $GNUPGHOME =~ '/.ssh_forward/\d+/*$' && ! -e $GNUPGHOME ]] | |
40 | GNUPGHOME=${GNUPGHOME%$MATCH} | |
41 | } | |
42 | ||
43 | ## tmp | |
44 | # NOTE: _sev_tmp is not unset so session dirs will not be recreated | |
45 | # NOTE: XDG dirs that use our tmp are not unset here, they are in zlogout | |
46 | if [[ -d $_sev_tmp && ( -z $1 || $1 == 'tmp' ) ]] { | |
47 | # clean up tmp dirs if its session is dead or we ask for it | |
48 | find $_sev_tmp -mindepth 1 -maxdepth 1 -name '.session.*' -type d | | |
49 | while {read -r x} { | |
50 | # NOTE: same rationale as above | |
51 | p=${$(basename $x)#.session.} | |
52 | if {[[ -v _sev_tmp_clean || $$ == $p ]] || | |
53 | ! kill -0 $p 2>/dev/null} { | |
54 | rm -rf $x | |
55 | } | |
56 | } | |
57 | } | |
58 | ||
59 | unset x p y | |
60 | } | |
61 | ||
62 | ### xdg local dir | |
63 | # NOTE: need this for tmp, so confirm it exists. | |
64 | # XXX: perms are not specified for XDG dirs except runtime, but I think 760 | |
65 | # makes the most sense. shouldn't break anything since no one else should | |
66 | # be poking around in our dir. | |
67 | [[ -e ~/.local ]] || mkdir -m760 ~/.local | |
68 | ||
69 | ### tmp | |
70 | # NOTE: specs say that POSIX tmp and XDG runtime directories should exist | |
71 | # until the last session is logged out (POSIX can exist for longer). | |
72 | # since we can't reliably keep track of sessions in a cross-platform | |
73 | # manner, the current implementation should use a separate directory per | |
74 | # toplevel session (i.e. SHLVL=1). this should placate most applications, | |
75 | # though it is not expressly spec compliant. this may also cause problems | |
76 | # with disowned applications that still try to use the directories after | |
77 | # the toplevel shell has already logged out and the dirs removed, but the | |
78 | # chances of that are slim. | |
79 | if [[ ! -v _sev_tmp ]] { | |
80 | # create personal TMPDIR under system tmp | |
81 | t=${TMPDIR:-${TEMPDIR:-${TEMP:-${TMP:-/tmp}}}}/.home-$LOGNAME | |
82 | [[ -e $t ]] || mkdir -m700 $t 2>/dev/null | |
83 | _sev_tmp=~/.local/tmp | |
84 | if [[ ! -d $t ]] { | |
85 | # fallback TMPDIR to bare local directory or existing softlink | |
86 | [[ -o interactive ]] && | |
87 | print -P "%F{orange}*** Can't create tmp dir $t, using $_sev_tmp%f" | |
88 | [[ -h $_sev_tmp && ! -d _sev_tmp ]] && unlink $_sev_tmp 2>/dev/null | |
89 | [[ ! -e $_sev_tmp ]] && mkdir -m700 $_sev_tmp 2>/dev/null | |
90 | if [[ ! -d $_sev_tmp ]] { | |
91 | _sev_tmp=${$(mktemp 2>/dev/null):/tmp} | |
92 | [[ -o interactive ]] && | |
93 | print -P "%F{red}!!! Can't create tmp dir, using $_sev_tmp%f" | |
94 | } | |
95 | } elif [[ -f $_sev_tmp || ( -d $_sev_tmp && ! -h $_sev_tmp ) ]] { | |
96 | # file or non-softlink directory is on our local dir | |
97 | [[ -o interactive ]] && | |
98 | print -P "%F{orange}*** $_sev_tmp exists, can't link to tmp dir $t, ignoring it%f" | |
99 | _sev_tmp=$t | |
100 | } else { | |
101 | # link local dir to tmp dir | |
102 | if [[ -h $_sev_tmp && $_sev_tmp:P != $t:P ]] { | |
103 | [[ -o interactive ]] && | |
104 | print -P "%F{orange}*** $_sev_tmp links to ${_sev_tmp:P} and not ${t:P}, unlinking it%f" | |
105 | # XXX: race condition for existing sessions still using this dir | |
106 | unlink $_sev_tmp 2>/dev/null | |
107 | } | |
108 | ln -s $t $_sev_tmp 2>/dev/null | |
109 | } | |
110 | if [[ -v _sev_tmp ]] { | |
111 | # ensure dir is clean | |
112 | _sev_zcleanup tmp | |
113 | # finally create our subdir for this session | |
114 | t=$_sev_tmp/.session.$$ | |
115 | if ! mkdir -m700 $t 2>/dev/null; then | |
116 | [[ -o interactive ]] && | |
117 | print -P "%F{red}!!! Can't create session tmp subdir $t, using $_sev_tmp%f" | |
118 | t=$_sev_tmp | |
119 | fi | |
120 | export _sev_tmp TMPDIR=$t TEMPDIR=$t TEMP=$t TMP=$t | |
121 | } | |
122 | unset t | |
123 | } | |
124 | ||
125 | ### xdg | |
126 | if [[ ! -v _sev_setup_xdg ]] { | |
127 | ## merge with any existing dirs and remove duplicates using unique arrays | |
128 | # NOTE: we are accepting whatever value might be set for CONFIG and DATA; | |
129 | # if it wasn't set, we just use default and leave it unset | |
130 | # NOTE: include and then remove CONFIG_HOME and DATA_HOME to ensure they | |
131 | # are not present in the array if it was added before we got to it | |
132 | ||
133 | # source user dirs before other vars; technically it is against spec to | |
134 | # include any of the below dirs there, but you never know what crazy shit | |
135 | # people will do. I rather handle them sanely with our own code than let | |
136 | # them override after the fact. | |
137 | [[ -e $XDG_CONFIG_HOME/user-dirs.dirs ]] && | |
138 | emulate sh -c "source $XDG_CONFIG_HOME/user-dirs.dirs" | |
139 | ||
140 | typeset -UT XDG_DATA_DIRS xdg_data_dirs | |
141 | if [[ -v XDG_DATA_HOME ]] { | |
142 | export XDG_DATA_HOME | |
143 | } elif [[ ! -e ~/.local/share ]] { | |
144 | mkdir -m760 ~/.local/share | |
145 | } | |
146 | xdg_data_dirs=($XDG_DATA_HOME /{opt,usr/local,usr/pkg,usr}/share | |
147 | "${XDG_DATA_DIRS:+${xdg_data_dirs[@]}}") | |
148 | # XXX: if colons are not escaped, could remove unintended part of string | |
149 | # TODO: remove empty element via array not scalar | |
150 | export XDG_DATA_DIRS=${XDG_DATA_DIRS#$XDG_DATA_HOME:} | |
151 | ||
152 | typeset -UT XDG_CONFIG_DIRS xdg_config_dirs | |
153 | if [[ -v XDG_CONFIG_HOME ]] { | |
154 | export XDG_CONFIG_HOME | |
155 | } elif [[ ! -e ~/.config ]] { | |
156 | mkdir -m760 ~/.config | |
157 | } | |
158 | # I am of the belief .local should follow FHS /usr/local... | |
159 | [[ -e ~/.local/etc ]] || ln -s ~/.config ~/.local/etc | |
160 | xdg_config_dirs=($XDG_CONFIG_HOME ${XDG_CONFIG_DIRS:+"$xdg_config_dirs[@]"} | |
161 | {/opt,/usr/local,/usr/pkg,}/etc/xdg) | |
162 | # XXX: if colons are not escaped, could remove unintended part of string | |
163 | # TODO: remove empty element via array not scalar | |
164 | export XDG_CONFIG_DIRS=${XDG_CONFIG_DIRS#$XDG_CONFIG_HOME:} | |
165 | ||
166 | if [[ -v XDG_STATE_HOME ]] { | |
167 | export XDG_STATE_HOME | |
168 | } elif [[ ! -e ~/.local/state ]] { | |
169 | mkdir -m760 ~/.local/state | |
170 | } | |
171 | ||
172 | if [[ -v XDG_CACHE_HOME ]] { | |
173 | export XDG_CACHE_HOME | |
174 | } else { | |
175 | if [[ -v _sev_tmp ]] { | |
176 | export XDG_CACHE_HOME=$_sev_tmp/.xdg.cache | |
177 | [[ -e $XDG_CACHE_HOME ]] || mkdir -m700 $XDG_CACHE_HOME | |
178 | } elif [[ ! -e ~/.cache ]] { | |
179 | mkdir -m700 ~/.cache | |
180 | } | |
181 | } | |
182 | ||
183 | if [[ -v XDG_RUNTIME_DIR ]] { | |
184 | export XDG_RUNTIME_DIR | |
185 | } elif [[ -v TMPDIR ]] { | |
186 | # make runtime dir in our session-specific tmpdir | |
187 | export XDG_RUNTIME_DIR=$TMPDIR/.xdg.runtime | |
188 | # same as in tmpdir creation, ensure dir doesn't exist | |
189 | if [[ -h $XDG_RUNTIME_DIR ]] { | |
190 | unlink $XDG_RUNTIME_DIR 2>/dev/null | |
191 | } elif [[ -e $XDG_RUNTIME_DIR ]] { | |
192 | rm -rf $XDG_RUNTIME_DIR 2>/dev/null | |
193 | } | |
194 | mkdir -m700 $XDG_RUNTIME_DIR 2>/dev/null | |
195 | } | |
196 | ||
197 | export _sev_setup_xdg= | |
198 | } | |
199 | ||
200 | ### dbus | |
201 | if [[ ! -v DBUS_SESSION_BUS_ADDRESS && -v commands[dbus-launch] ]] { | |
202 | eval $(dbus-launch) | |
203 | export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID | |
204 | } | |
205 | ||
206 | ### gpg home | |
207 | if [[ ! -v GNUPGHOME ]] { | |
208 | export GNUPGHOME=${XDG_CONFIG_HOME:-~/.config}/gnupg | |
209 | # move existing gnupg dir to our new home | |
210 | if [[ -d ~/.gnupg && ! -d $GNUPGHOME ]] { | |
211 | mv ~/.gnupg $GNUPGHOME | |
212 | } | |
213 | } | |
214 | ||
215 | ### gpg agent + forwarding | |
216 | # NOTE: while ssh manages its auth sock in-protocol when ForwardSsh is enabled, | |
217 | # GPG must be forwarded manually over Unix socket. to support this, we | |
218 | # forward the restricted gpg-agent extra socket to the remote host with a | |
219 | # RemoteForward rule in ~/.ssh/config that uses the _GNUPG_SOCK_* vars. | |
220 | # | |
221 | # to avoid conflicts with other ssh sessions where the same user is | |
222 | # connecting to the same host from different machines, gpg should utilize | |
223 | # its own forwarded socket for each session. previously, you could | |
224 | # provide a path to the agent socket in GPG_AGENT_INFO, but that was | |
225 | # deprecated in GPG v2.1; instead, we must replace the socket in gpg's | |
226 | # socket dir. | |
227 | # | |
228 | # gnupg commits fb88f37–aab8a0b moved sockets from GNUPGHOME by default | |
229 | # to directories under /run. aab8a0b in particular solidifies the | |
230 | # functionality shift to utilize systemd-logind's user runtime | |
231 | # directories. if the dir isn't found, gpg will fall back to the homedir. | |
232 | # since previous functionality allowed multiple homedirs with multiple | |
233 | # sockets, a provision was added where changing GNUPGHOME will also | |
234 | # change the socket dir to a hashed dir under the usual runtime dir. | |
235 | # | |
236 | # utilizing this information, we can conclude: | |
237 | # - on systems with user runtime dirs, changing GNUPGHOME will also | |
238 | # give us a unique socket dir under the user runtime dir; | |
239 | # - on other systems, the socket dir follows GNUPGHOME. | |
240 | # therefore, the safest way to ensure unique sockets while not having to | |
241 | # write specific logic for both scenarios is to simply change GNUPGHOME. | |
242 | # the easiest way to do this is to create a new dir and link the contents | |
243 | # of GNUPGHOME to the new home. we can then replace the agent sockets | |
244 | # there with the forwarded one. | |
245 | # | |
246 | # NOTE: since Unix sockets are not supported under Windows, this will not work | |
247 | # under msys, cygwin, mingw, etc., but may work under wsl2. | |
248 | # | |
249 | # HACK: without SendEnv, which is disabled by default in most sshd configs, | |
250 | # there is no foolproof way to prevent race conditions via socket | |
251 | # filename collisions or to pass the desired forward path to the remote | |
252 | # host environment. we just have to guess the path we choose is good on | |
253 | # the desination, and assume the newest matching socket is the correct | |
254 | # one after connecting. in theory, we could occlude the ssh binary on | |
255 | # PATH with an alias or script that would allow us to communicate with | |
256 | # the remote host before opening a shell, so that we can have the host | |
257 | # communicate back to the client where it wants a socket created or ask | |
258 | # the host if the path the client wants to use is writable. however, this | |
259 | # would open up too many edge cases where it wouldn't work or be too | |
260 | # clunky (e.g. asking for password twice) to make it worth it. | |
261 | function _gpg_socketpath { | |
262 | # dirs are percent-encoded: https://stackoverflow.com/a/64312099 | |
263 | echo ${1//(#b)%([[:xdigit:]](#c2))/${(#):-0x$match[1]}} | |
264 | } | |
265 | if [[ ! -v _sev_setup_gpg_forward && -v commands[gpg] ]] { | |
266 | # XXX: assuming /tmp exists and is writable on destination | |
267 | export _GNUPG_SOCK_DEST_BASE=/tmp/.gpg-agent-forward | |
268 | export _GNUPG_SOCK_DEST_EXT=$(date +%s).$RANDOM | |
269 | export _GNUPG_SOCK_DEST=$_GNUPG_SOCK_DEST_BASE.$_GNUPG_SOCK_DEST_EXT | |
270 | export _sev_gpg_forward_dir=$XDG_RUNTIME_DIR/gnupg/.ssh_forward | |
271 | _sev_zcleanup gpg-forward | |
272 | ||
273 | # find our forwarded socket | |
274 | s=($_GNUPG_SOCK_DEST_BASE*(N=oc[1])) | |
275 | if [[ -n $s && -v SSH_CLIENT ]] { | |
276 | # create new forward dir | |
277 | export _sev_setup_gpg_forward= | |
278 | h=$_sev_gpg_forward_dir/$$ | |
279 | mkdir -pm700 $h | |
280 | for x (gpg{,-agent}.conf sshcontrol random_seed | |
281 | pubring.kbx{,~} trustdb.gpg private-keys-v1.d crls.d) { | |
282 | ln -s ${GNUPGHOME:-~/.gnupg}/$x $h | |
283 | } | |
284 | export GNUPGHOME=$h | |
285 | unset h | |
286 | for x in $(gpgconf --list-dirs | grep 'agent-.*-\?socket:'); do | |
287 | x=$(_gpg_socketpath ${x/#agent-*socket:}) | |
288 | if [[ ! -v primary ]] { | |
289 | # move forwarded socket to first valid agent socket path | |
290 | # XXX: if tmp is on different filesystem this may not work | |
291 | mv $s $x | |
292 | primary=$x | |
293 | } else { | |
294 | # make links to forwarded socket for any others | |
295 | ln -s $primary $x | |
296 | } | |
297 | done | |
298 | unset x primary | |
299 | } | |
300 | unset s | |
301 | ||
302 | # what we will forward if we start a new ssh connection | |
303 | # NOTE: do this after setting up GNUPGHOME to pick up new socket path; | |
304 | # if already connected over SSH, extra should be the remote one | |
305 | export _GNUPG_SOCK_SRC=$(_gpg_socketpath \ | |
306 | $(gpgconf --list-dirs agent-extra-socket)) | |
307 | } elif [[ ! -v _sev_setup_gpg_forward ]] { | |
308 | # required for RemoteForward to not error out if the vars are unset | |
309 | [[ ! -v _GNUPG_SOCK_SRC ]] && export _GNUPG_SOCK_SRC=/nonexistent | |
310 | [[ ! -v _GNUPG_SOCK_DEST ]] && export _GNUPG_SOCK_DEST=/nonexistent | |
311 | } | |
312 | ||
313 | ### gpg agent | |
314 | if [[ -v commands[gpg-connect-agent] && | |
315 | ( ! -v _sev_setup_gpgagent || -v _sev_refresh_gpgagent ) ]] { | |
316 | # avoid printing if we have already set up tty before | |
317 | [[ ! -v _sev_setup_gpgagent && -o interactive ]] && p=true || p=false | |
318 | if {$p} { | |
319 | print -nP '%F{blue}>>>%f GPG: ' | |
320 | if [[ -v _sev_setup_gpg_forward ]] { | |
321 | print -nP '%F{yellow}Forwarded agent ' | |
322 | } else { | |
323 | print -nP '%F{green}Agent ' | |
324 | } | |
325 | } | |
326 | gpg-connect-agent /bye >/dev/null 2>&1 | |
327 | if [[ $? -ne 0 ]] { | |
328 | $p && print -P '%F{red}communication error' | |
329 | } else { | |
330 | if [[ ! -v _sev_setup_gpg_forward ]] { | |
331 | if [[ ${+GPG_TTY} -eq 0 && -o interactive ]] | |
332 | export GPG_TTY=$(tty) | |
333 | if [[ ( -v DISPLAY || -v WAYLAND_DISPLAY ) && | |
334 | ${PINENTRY_USER_DATA/USE_TTY=0} == $PINENTRY_USER_DATA ]] | |
335 | export PINENTRY_USER_DATA=USE_TTY=$(( | |
336 | ${+DISPLAY} + ${+WAYLAND_DISPLAY} == 0)) | |
337 | # XXX: don't know if gpg-agent supports comments after directives | |
338 | # XXX: path could have # | |
339 | # XXX: we are assuming this is our pinentry from .local/bin | |
340 | sed -Ei 's#^([[:space:]]*pinentry-program[[:space:]]).*$#\1'$HOME'/.local/bin/pinentry#' \ | |
341 | ${GNUPGHOME:-~/.gnupg}/gpg-agent.conf 2>/dev/null | |
342 | # XXX: could check for changes before doing this to save perf | |
343 | gpg-connect-agent RELOADAGENT UPDATESTARTUPTTY /bye >/dev/null 2>&1 | |
344 | if {$p} { | |
345 | gpg-connect-agent /subst /serverpid \ | |
346 | "/echo pid \${get serverpid} on $GPG_TTY" /bye 2>/dev/null | |
347 | print -nP '%f' | |
348 | } | |
349 | } elif {$p} { | |
350 | print -P '%f' | |
351 | } | |
352 | export _sev_setup_gpgagent= | |
353 | } | |
354 | unset p _sev_refresh_gpgagent | |
355 | } | |
356 | ||
357 | ### ssh agent | |
358 | if [[ ! -v _sev_setup_ssh ]] { | |
359 | # NOTE: preferred order of agents to check: okcagent, gnupg, openssh | |
360 | # first block takes care of okcagent and openssh, second gnupg | |
361 | # XXX: doesn't actually check if ssh is enabled in gpg | |
362 | [[ -o interactive ]] && print -nP '%F{blue}>>>%f SSH: %F{green}' | |
363 | if [[ ! -v SSH_AUTH_SOCK && ( -v commands[okc-ssh-agent] || | |
364 | ( -v commands[ssh-agent] && ! -v commands[gpg] ) ) ]] { | |
365 | okc=${commands[okc-ssh-agent]:+okc-} | |
366 | e=$_sev_tmp/${okc}ssh-agent-exports | |
367 | typeset sock= | |
368 | typeset -i pid= | |
369 | if [[ -f $e ]] { | |
370 | IFS=$'\0' read -r sock pid <$e | |
371 | } | |
372 | if [[ -S $sock && $pid > 0 ]] && kill -0 $pid; then | |
373 | [[ -o interactive ]] && print -P "Reusing agent PID $pid%f" | |
374 | export SSH_AUTH_SOCK=$sock | |
375 | export SSH_AGENT_PID=$pid | |
376 | else | |
377 | # TODO: ensure ssh-agent path looks legit to avoid unsafe eval? | |
378 | # XXX: doesn't appear to be any other way to handle redirection. | |
379 | # because eval needs to write to current scope environment | |
380 | # subshells can't be used to capture output and print. | |
381 | c='TMPDIR=$_sev_tmp ${okc}ssh-agent' | |
382 | if [[ -o interactive ]] { | |
383 | eval $(eval $=c) | |
384 | print -nP '%f' | |
385 | } else { | |
386 | eval $(eval $=c) >/dev/null 2>&1 | |
387 | } | |
388 | echo -n $SSH_AUTH_SOCK$'\0'$SSH_AGENT_PID >!$e | |
389 | unset c | |
390 | fi | |
391 | unset okc e sock pid | |
392 | } elif [[ ! -v SSH_AUTH_SOCK && -v commands[gpg] ]] { | |
393 | # since gpg should have been started above, just export and notify | |
394 | if [[ -o interactive ]] { | |
395 | if [[ -v _sev_setup_gpg_forward ]] { | |
396 | echo 'Forwarded GPG agent' | |
397 | } else { | |
398 | gpg-connect-agent /subst /serverpid \ | |
399 | '/echo GPG agent pid ${get serverpid}' /bye | |
400 | } | |
401 | print -nP '%f' | |
402 | } | |
403 | export SSH_AUTH_SOCK=$(_gpg_socketpath \ | |
404 | $(gpgconf --list-dirs agent-ssh-socket)) | |
405 | } elif [[ -v SSH_AUTH_SOCK ]] { | |
406 | [[ -o interactive ]] && print -P 'Preconfigured agent%f' | |
407 | } else { | |
408 | [[ -o interactive ]] && print -P '%F{red}No agent available%f' | |
409 | } | |
410 | ||
411 | export _sev_setup_ssh= | |
412 | } | |
413 | unfunction _gpg_socketpath | |
414 | ||
415 | ### perl local lib | |
416 | [[ -v commands[perl] && -d $XDG_DATA_HOME/perl5/lib/perl5 && | |
417 | ! -v PERL_LOCAL_LIB_ROOT ]] && | |
418 | eval $(perl -I$XDG_DATA_HOME/perl5/lib/perl5 \ | |
419 | -Mlocal::lib=$XDG_DATA_HOME/perl5 2>/dev/null) | |
420 | ||
421 | ### load site-specific | |
422 | load-site-dotfile zprofile |