26763: fix problem on failed cd -s to relative path
[zsh.git] / Completion / compaudit
blob7107c2fff210f733c0f14b7db4d737964f11833c
1 # So that this file can also be read with `.' or `source' ...
2 compaudit() {                           # Define and then call
4 # Audit the fpath to assure that it contains all the directories needed by
5 # the completion system, and that those directories are at least unlikely
6 # to contain dangerous files.  This is far from perfect, as the modes or
7 # ownership of files or directories might change between the time of the
8 # audit and the time the function is executed.
10 # This function is designed to be called from compinit, which assumes that
11 # it is in the same directory, i.e., it can be autoloaded from the initial
12 # fpath as compinit was.  Most local parameter names in this function must
13 # therefore be the same as those used in compinit.
15 emulate -L zsh
16 setopt extendedglob
18 [[ -x /usr/bin/getent ]] || getent() {
19   if [[ $2 = <-> ]]; then
20     grep ":$2:[^:]*$" /etc/$1
21   else
22     grep "^$2:" /etc/$1
23   fi
26 # The positional parameters are the directories to check, else fpath.
27 if (( $# )); then
28   local _compdir=''
29 elif (( $#fpath == 0 )); then
30   print 'compaudit: No directories in $fpath, cannot continue' 1>&2
31   return 1
32 else
33   set -- $fpath
36 # _i_check is defined by compinit; used here as a test for whether this
37 # function is running standalone or was called by compinit.  If called
38 # by compinit, we use parameters that are defined in compinit's scope,
39 # otherwise we make them local here.
40 (( $+_i_check )) || {
41   local _i_q _i_line _i_file _i_fail=verbose
42   local -a _i_files _i_addfiles _i_wdirs _i_wfiles
43   local -a -U +h fpath
46 fpath=( $* )
48 # _compdir may be defined by the user; see the compinit documentation.
49 # If it isn't defined, we want it to point somewhere sensible, but the
50 # user is allowed to set it to empty to bypass the check below.
51 (( $+_compdir )) || {
52   local _compdir=${fpath[(r)*/$ZSH_VERSION/*]}
53   [[ -z $_compdir ]] && _compdir=$fpath[1]
54   ### [[ -d $_compdir/../Base ]] && _compdir=${_compdir:h}
57 _i_wdirs=()
58 _i_wfiles=()
60 _i_files=( ${^~fpath:/.}/^([^_]*|*~|*.zwc)(N) )
61 if [[ -n $_compdir ]]; then
62   if [[ $#_i_files -lt 20 || $_compdir = */Base || -d $_compdir/Base ]]; then
63     # Too few files: we need some more directories, or we need to check
64     # that all directories (not just Base) are present.
65     _i_addfiles=()
66     if [[ -d $_compdir/Base/Core ]]; then
67       # Add all the Completion subdirectories (CVS-layout)
68       _i_addfiles=(${_compdir}/*/*(/^M))
69     elif [[ -d $_compdir/Base ]]; then
70       # Likewise (installation-layout)
71       _i_addfiles=(${_compdir}/*(/^M))
72     fi
73     for _i_line in {1..$#_i_addfiles}; do
74       _i_file=${_i_addfiles[$_i_line]}
75       [[ -d $_i_file && -z ${fpath[(r)$_i_file]} ]] ||
76         _i_addfiles[$_i_line]=
77     done
78     fpath=($fpath $_i_addfiles)
79     _i_files=( ${^~fpath:/.}/^([^_]*|*~|*.zwc)(N) )
80   fi
83 [[ $_i_fail == use ]] && return 0
85 # RedHat Linux "per-user groups" check.  This is tricky, because it's very
86 # difficult to tell whether the sysadmin has put someone else into your
87 # "private" group (e.g., via the default group field in /etc/passwd, or
88 # by NFS group sharing with an untrustworthy machine).  So we must assume
89 # that this has not happened, and pick the best group.
91 local GROUP GROUPMEM _i_pw _i_gid _i_ulwdirs
92 if ((UID == EUID )); then
93   getent group $LOGNAME | IFS=: read GROUP _i_pw _i_gid GROUPMEM
94 else
95   getent group $EGID | IFS=: read GROUP _i_pw _i_gid GROUPMEM
98 # We search for:
99 # - world/group-writable directories in fpath not owned by root and the user
100 # - parent-directories of directories in fpath that are world/group-writable
101 #   and not owned by root and the user (that would allow someone to put a
102 #   digest file for one of the directories into the parent directory)
103 # - digest files for one of the directories in fpath not owned by root and
104 #   the user
105 # - and for files in directories from fpath not owned by root and the user
106 #   (including zwc files)
108 if [[ $GROUP == $LOGNAME && ( -z $GROUPMEM || $GROUPMEM == $LOGNAME ) ]]; then
109   _i_wdirs=( ${^fpath}(N-f:g+w:^g:${GROUP}:,-f:o+w:,-^u0u${EUID})
110              ${^fpath:h}(N-f:g+w:^g:${GROUP}:,-f:o+w:,-^u0u${EUID}) )
111 else
112   _i_wdirs=( ${^fpath}(N-f:g+w:,-f:o+w:,-^u0u${EUID})
113              ${^fpath:h}(N-f:g+w:,-f:o+w:,-^u0u${EUID}) )
116 if [[ -f /etc/debian_version ]]
117 then
118 _i_ulwdirs=( ${(M)_i_wdirs:#/usr/local/*} )
119 _i_wdirs=( ${_i_wdirs:#/usr/local/*} ${^_i_ulwdir}(Nf:g+ws:^g:staff:,f:o+w:,^u0) )
122 _i_wdirs=( $_i_wdirs ${^fpath}.zwc^([^_]*|*~)(N-^u0u${EUID}) )
123 _i_wfiles=( ${^fpath}/^([^_]*|*~)(N-^u0u${EUID}) )
125 case "${#_i_wdirs}:${#_i_wfiles}" in
126 (0:0) _i_q= ;;
127 (0:*) _i_q=files ;;
128 (*:0) _i_q=directories ;;
129 (*:*) _i_q='directories and files' ;;
130 esac
132 if [[ -n "$_i_q" ]]; then
133   [[ $_i_fail == verbose ]] && {
134     print There are insecure ${_i_q}: 1>&2
135     print -l - $_i_wdirs $_i_wfiles
136   }
137   return 1
139 return 0
141 }                                       # Define and then call
143 compaudit "$@"