-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqmail-fixq.sh
166 lines (147 loc) · 6.7 KB
/
qmail-fixq.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/sh
CFGFILE="conf.tmp"
OUTFILE="qmail-fixq"
[ -f "$CFGFILE" ] || exit 1
# the 'stat' command is different on Linux/BSD
STATOPTS="-f %Lp"
cat systype | grep -i linux >/dev/null
if [ $? -eq 0 ] ; then STATOPTS="-c %a" ; fi
cat warn-auto.sh > "$OUTFILE"
echo "" >> "$OUTFILE"
echo $(cat "$CFGFILE" | grep ^HOMEDIR | sed s}HOMEDIR}QMAILDIR}g) >> "$OUTFILE"
echo $(cat "$CFGFILE" | grep ^UIDQ) >> "$OUTFILE"
echo $(cat "$CFGFILE" | grep ^UIDR) >> "$OUTFILE"
echo $(cat "$CFGFILE" | grep ^UIDS) >> "$OUTFILE"
echo $(cat "$CFGFILE" | grep ^GIDQ) >> "$OUTFILE"
echo $(cat "$CFGFILE" | grep ^SPLIT) >> "$OUTFILE"
echo "" >> "$OUTFILE"
echo "VARDIR=\"\$QMAILDIR/var\"" >> "$OUTFILE"
echo "
showHelp() {
echo \"Usage: $OUTFILE [option [-m]\"
echo
echo -e \" -c\tcheck structure of the queue for issues (default)\"
echo -e \" -f\tfix issues of the queue\"
echo -e \" -h\tShow this help\"
# echo -e \" -m\tincludes check of messages (this could take a long time)\" # useless in practice, messages
# will be fixed always
echo
exit 0
}" >> "$OUTFILE"
echo "fixqueue() {" >> "$OUTFILE"
echo " echo -n \"Fixing queue ...\"" >> "$OUTFILE"
# the code comes from the 'install' script
fln=`cat install | grep -n '^# queue' | cut -d: -f1` # get the first line
lln=`cat install | grep -n '^# bin' | cut -d: -f1` # lastline
fln=`expr $fln + 1` # skip one line on begin
lines=`expr $lln - $fln - 1` # ... and on the end
#cat install | grep -A $l '^# queue' >> $OUTFILE
cat install | tail -n +$fln | head -n $lines | sed 's/^/ /' >> "$OUTFILE"
echo " echo \"done!\"" >> "$OUTFILE"
echo "}" >> "$OUTFILE"
echo "chkqueue() {
echo \"Checking queue for issues ...\"
SPLITMESS=\`find \$VARDIR/queue/mess/* -type d -print | wc -l\`
SPLITINFO=\`find \$VARDIR/queue/info/* -type d -print | wc -l\`
SPLITINTD=\`find \$VARDIR/queue/intd/* -type d -print | wc -l\`
SPLITLOCL=\`find \$VARDIR/queue/local/* -type d -print | wc -l\`
SPLITREMT=\`find \$VARDIR/queue/remote/* -type d -print | wc -l\`
SPLITTODO=\`find \$VARDIR/queue/todo/* -type d -print | wc -l\`
if [ \"\$SPLIT\" -ne \"\$SPLITMESS\" ]; then SPLITERR=1 ; fi
if [ \"\$SPLIT\" -ne \"\$SPLITINFO\" ]; then SPLITERR=1 ; fi
if [ \"\$SPLIT\" -ne \"\$SPLITINTD\" ]; then SPLITERR=1 ; fi
if [ \"\$SPLIT\" -ne \"\$SPLITLOCL\" ]; then SPLITERR=1 ; fi
if [ \"\$SPLIT\" -ne \"\$SPLITREMT\" ]; then SPLITERR=1 ; fi
if [ \"\$SPLIT\" -ne \"\$SPLITTODO\" ]; then SPLITERR=1 ; fi
if [ \"\$SPLITERR\" ] ; then
echo \"invalid queue split\" ; ERR=1
else
echo \"splitting ok!\"
fi
# check the directory structure
# group have to be \'qmail\' always (recursive)
GRPNAME=\`getent group \$GIDQ | awk -F: '{print \$1}'\`
find \$VARDIR/queue/* -type d -print | xargs ls -ld | awk '{print \$4}' | grep -v \$GRPNAME >/dev/null
if [ \$? -eq 0 ] ; then GRPERR=1 ; fi
# check for uid qmailq/qmails on splitted directories
USRNAME=\`id -un \$UIDQ\`
for f in intd mess todo local remote info
do
if [ \"\$f\" = \"local\" ] ; then USRNAME=\`id -un \$UIDS\` ; fi
#echo \$f
ls -ld \$VARDIR/queue/\$f | awk '{print \$3}' | grep -v \$USRNAME >/dev/null
if [ \$? -eq 0 ] ; then USRERR=1 ; fi
#find \$VARDIR/queue/\$f/*
find \$VARDIR/queue/\$f/* | xargs ls -ld | awk '{print \$3}' | grep -v \$USRNAME >/dev/null
if [ \$? -eq 0 ] ; then USRERR=1 ; fi
done
# check pid, bounce
ls -ld \$VARDIR/queue/bounce | awk '{print \$3}' | grep -v \$USRNAME >/dev/null
if [ \$? -eq 0 ] ; then USRERR=1 ; fi
USRNAME=\`id -un \$UIDQ\` # until now USRNAME was 'qmails'
ls -ld \$VARDIR/queue/pid | awk '{print \$3}' | grep -v \$USRNAME >/dev/null
if [ \$? -eq 0 ] ; then USRERR=1 ; fi
# special: lock
#echo U: \$USRNAME
ls -ld \$VARDIR/queue/lock | awk '{print \$3}' | grep -v \$USRNAME >/dev/null
if [ \$? -eq 0 ] ; then USRERR=1 ; fi
USRNAME=\`id -un \$UIDS\` # ... back to USRNAME was 'qmails'
ls -l \$VARDIR/queue/lock/trigger | awk '{print \$3}' | grep -v \$USRNAME >/dev/null
if [ \$? -eq 0 ] ; then USRERR=1 ; fi
#echo U: \$USRNAME
ls -l \$VARDIR/queue/lock/sendmutex | awk '{print \$3}' | grep -v \$USRNAME >/dev/null
if [ \$? -eq 0 ] ; then USRERR=1 ; fi
USRNAME=\`id -un \$UIDR\` # ... and now we need USRNAME 'qmailr'
#echo U: \$USRNAME
ls -l \$VARDIR/queue/lock/tcpto | awk '{print \$3}' | grep -v \$USRNAME >/dev/null
if [ \$? -eq 0 ] ; then USRERR=1 ; fi
if [ \"\$USRERR\" ] || [ \"\$GRPERR\" ] ; then
echo \"invalid owner(s) or group!\"
else
echo \"user/group ok!\"
fi
# check permissions w/ and w/o subdirectories
stat $STATOPTS \`ls -d \$VARDIR/queue/mess\` | grep -v 750 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/mess/*\` | grep -v 750 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/todo\` | grep -v 750 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/todo/*\` | grep -v 750 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/info\` | grep -v 700 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/info/*\` | grep -v 700 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/intd\` | grep -v 700 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/intd/*\` | grep -v 700 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/local\` | grep -v 700 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/local/*\` | grep -v 700 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/remote\` | grep -v 700 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
stat $STATOPTS \`ls -d \$VARDIR/queue/remote/*\` | grep -v 700 >/dev/null
if [ \$? -eq 0 ] ; then PRMERR=1 ; fi
if [ \"\$PRMERR\" ] ; then
echo \"invalid permissions(s)!\"
else
echo \"permissions ok!\"
fi
# echo
}" >> "$OUTFILE"
echo "
if [ ! \"\$1\" ] ; then chkqueue ; exit 0 ; fi
while getopts ":hcf" o; do
case \"\${o}\" in
h) showHelp;;
f) fixqueue;;
c) chkqueue;;
m) FIXMSG=\"Y\" ; fixqueue;;
*) echo "Invalid option!" ; showHelp;;
esac
done ; shift $((OPTIND-1))
exit 0" >> "$OUTFILE"
chmod 755 $OUTFILE