Add /etc/mail/spamassassin to prune prefixes
[pkgusr] / usr / bin / forall_direntries_from
1 #!/bin/bash
2 ## Originally...
3 # Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
4 # You may do everything with this code except misrepresent its origin.
5 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
6
7 # Copyright (C) 2014 Steve Youngs <steve@steveyoungs.com>
8 ## Loads of minor tweaks --SY.
9
10 # The following list should contain the mount points of all
11 # filesystems that are to be scanned as a space-separated list within
12 # parentheses. / will usually be in this list and if you have /usr on
13 # a separate partition, it will also be in this list. Other
14 # non-special filesystems where package users could own files should
15 # also be put in this list. Mount points whose filesystems are
16 # special, such as procfs or sysfs must not be in this list. While a
17 # simple find on those special filesystems should be harmless,
18 # operations such as "-exec grep something" are NOT SAFE and may have
19 # HARMFUL SIDE-EFFECTS, especially when performed as root.
20
21 ## Bastard settings
22 # fs_to_scan=(/ /opt /usr /var)
23
24 fs_to_scan=(/)
25
26 # Files with a path prefix found in the following list are ignored. This
27 # list will usually contain the parent directory of your package users'
28 # home directories, because normally you don't want to scan those. You
29 # can also add other directories that will never contain package user
30 # files, such as /home. This reduces scan time.
31
32 # Directories that are on separate filesystems (separate from the ones
33 # listed in fs_to_scan above) don't need to be listed here because of
34 # the -xdev option used in the find command.
35
36 ## Bastard settings
37 # prune_prefixes=(\
38 #       /{,*/{,*/}}lost+found \
39 #       /root \
40 #       /opt/pgsql/data \
41 #       /opt/sql-ledger/{spool,templates,users,css} \
42 #       /etc/apache/ssl.key \
43 #       /etc/audisp/plugins.d \
44 #       /etc/cups/ssl \
45 #       /etc/firewall \
46 #       /etc/mail/spamassassin \
47 #       /etc/pam.d \
48 #       /etc/polkit-1/rules.d \
49 #       /etc/skel \
50 #       /etc/ssl/private \
51 #       /etc/sudoers.d \
52 #       /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
53 #       /var/log \
54 #       /usr/lib/pkgusr \
55 #       /usr/share/polkit-1/rules.d \
56 #       /var/tmp \
57 #       /var/{cache,chroot,db,run,snmp,spool} \
58 #       /var/lib/{sshd,nfs,spamassassin,pulse,machines}) #NO TRAILING SLASHES!!!!
59
60 prune_prefixes=(\
61         /home \
62         /root \
63         /usr/lib/pkgusr \
64         /usr/src) #NO TRAILING SLASHES!!!!
65
66 ## NOTE: 
67 # If you are scanning MS-DOS, CD-ROM, or AFS volumes you need
68 # to set $NOLEAF here to '-noleaf'.  Setting this does significantly
69 # slow down the search so only do so if you really need to.
70
71 # NOLEAF='-noleaf'
72 NOLEAF=
73
74 if [ $# -lt 1 -o "$1" = "--help" ]; then
75     echo 1>&2
76     echo 1>&2 'USAGE: '"${0##*/}"' <user_or_group_name> [<find-commands>]'
77     echo 1>&2
78     echo 1>&2 '  If <find-commands> contains no action other than -prune, -print will be'
79     echo 1>&2 '    executed for all matching files.'
80     echo 1>&2 '  Entries will be matched if group and/or user equals <user_or_group_name>'
81     echo 1>&2 '    (numeric UID/GID allowed).'
82     echo 1>&2 '  All matching entries will be acted on, including device special files, so'
83     echo 1>&2 '    you should be extra careful with the <find-commands> you provide!'
84     echo 1>&2
85     exit 1
86 fi
87
88 # suppress ugly debug output from shell
89 trap ':' SIGPIPE
90
91 ugname="$1"
92 shift 1  # remove user_or_group_name from argument list
93
94 ugmatcher=(-false)
95 # test if find accepts ugname as a user, and append to ugmatcher if it does
96 if find / -maxdepth 0 -user "$ugname" >/dev/null 2>&1 ; then
97     ugmatcher[${#ugmatcher[@]}]="-or"
98     ugmatcher[${#ugmatcher[@]}]="-user"
99     ugmatcher[${#ugmatcher[@]}]="$ugname"
100 fi
101 # test if find accepts ugname as a group, and append to ugmatcher if it does
102 if find / -maxdepth 0 -group "$ugname" >/dev/null 2>&1 ; then
103     ugmatcher[${#ugmatcher[@]}]="-or"
104     ugmatcher[${#ugmatcher[@]}]="-group"
105     ugmatcher[${#ugmatcher[@]}]="$ugname"
106 fi
107
108 # if find accepted ugname as neither user nor group, then exit
109 if [ "${#ugmatcher[@]}" = 1 ]; then
110     echo 1>&2 'find does not accept `'"$ugname'"' as group or user name'
111     exit 1
112 fi
113
114 # construct find commands that match the prune_prefixes. Each prefix will be
115 # matched as -path <prefix> -or -path <prefix>/*
116 # so that the directory itself and all subdirectories are matched.
117 y=(\( -false)
118 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1))
119 do
120     y[${#y[@]}]='-or'
121     y[${#y[@]}]=-path
122     y[${#y[@]}]="${prune_prefixes[$i]}"
123     y[${#y[@]}]='-or'
124     y[${#y[@]}]=-path
125     y[${#y[@]}]="${prune_prefixes[$i]}/*"
126 done
127 y[${#y[@]}]=')'
128
129 # The uninstall_package script sets this to `-ignore_readdir_race' so
130 # that find doesn't print errors when things it is searching for
131 # disappear.
132 # IGNORE_READDIR_RACE='-ignore_readdir_race'
133 IGNORE_READDIR_RACE=
134
135 # In the following find command, the part
136 # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
137 # is responsible for preventing the files that match prune_prefixes from
138 # being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
139 # -prune has no effect and is always false when -depth is used.
140 # The -true before "$@" ensures that -depth can be passed as only parameter.
141
142 find "${fs_to_scan[@]}" -xdev $NOLEAF $IGNORE_READDIR_RACE \
143     -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
144     -and \( "${ugmatcher[@]}" \) -and \( -true "$@" \)