/
kopano-fetchmail.sh
282 lines (282 loc) · 7.38 KB
/
kopano-fetchmail.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/sh
# (c) 2015/16 vbettag - script for fetchmail integration with kopano via postfix
#
FRC="/etc/kopano/fetchmailrc"
case "$1" in
help)
echo "kopano-fetchmail (c) TosoBoso: script for fetchmail integration with kopano via postfix"
echo "Usage: kopano-fetchmail list, add, remove, (no)keep, (re)start, stop, status, init, test, help"
echo "list entries, add new fetchmail user or remove it. Init as a service or run in test mode"
exit 0
;;
status)
if service fetchmail status | grep -q "fetchmail is running"
then
echo "fetchmail is running."
else
echo "fetchmail is NOT running."
fi
exit 0
;;
stop)
if [ $# -gt 1 ] && [ $2 = "force" ]
then
echo "stoping fetchmail with kill-all..."
CMD="killall -q -9 fetchmail"
if [ -e /var/run/fetchmail/fetchmail.pid ] ; then rm /var/run/fetchmail/fetchmail.pid ; fi
else
echo "stoping gracefully. check the status and eventually run stop force"
CMD="service fetchmail stop"
fi
;;
start)
if grep -q "place your configuration here" $FRC
then
echo "at least 1 entry must be present and init has to be done before starting it.."
exit 1
fi
CMD="service fetchmail start"
;;
restart)
if grep -q "place your configuration here" $FRC
then
echo "at least 1 entry must be present and init has to be done before starting it.."
exit 1
fi
echo "restarting by stop gracefully then 4s later kill-all then start.."
if [ -e /var/run/fetchmail/fetchmail.pid ] && service fetchmail status | grep -q "fetchmail is running"
then
service fetchmail stop
sleep 4
fi
killall -q -9 fetchmail
if [ -e /var/run/fetchmail/fetchmail.pid ] ; then rm /var/run/fetchmail/fetchmail.pid ; fi
CMD="service fetchmail start"
;;
test)
if [ $# -gt 1 ]
then
SEC=$2
else
SEC=40
fi
if [ -e /var/run/fetchmail/fetchmail.pid ] && service fetchmail status | grep -q "fetchmail is running"
then
service fetchmail stop
sleep 4
fi
killall -q -9 fetchmail
# fake pid to avoid ugly red failed message for service stop...
touch /var/run/fetchmail/fetchmail.pid
echo "fetchmail in debug mode hit crtl.c and then restart service.."
CMD="/etc/init.d/fetchmail debug-run"
;;
init)
if grep -q "#place your configuration here" $FRC
then
echo "at least 1 entry must be present before adding fetchmail to kopano-init.."
exit 1
fi
echo "init: enable fetchmail in kopano-default, add it to dagent local_admin_users and start it.."
sed -i -e 's~FETCHMAIL_ENABLED.*~FETCHMAIL_ENABLED=yes~' /etc/kopano/default
# ensure fetchmail part of local_admin_users to dagent via server.cfg
if ! grep -q fetchmail /etc/kopano/server.cfg
then
sed -i -e "s~root kopano~root kopano fetchmail~" /etc/kopano/server.cfg
fi
touch /etc/kopano/default-fetchmail
if ! grep -q START_DAEMON /etc/kopano/default-fetchmail
then
echo -e "# Declare demon mode here\nSTART_DAEMON=yes" > /etc/kopano/default-fetchmail
fi
sed -i -e "s~START_DAEMON=no~START_DAEMON=yes~" /etc/kopano/default-fetchmail
# make softlinks
ln -sf $FRC /etc/fetchmailrc
ln -sf /etc/kopano/default-fetchmail /etc/default/fetchmail
adduser fetchmail kopano
chown fetchmail.kopano $FRC
chmod 600 $FRC
touch /var/log/kopano/fetchmail.log
chown root.kopano /var/log/kopano/fetchmail.log
chmod 660 /var/log/kopano/fetchmail.log
touch /var/log/kopano/dagent.log
chown root.kopano /var/log/kopano/dagent.log
chmod 660 /var/log/kopano/dagent.log
# ensure rotate log of fetchmail
echo -e "/var/log/kopano/fetchmail.log {" > /etc/logrotate.d/fetchmail
echo -e "su root kopano\nweekly\nmissingok\nrotate 4\ncompress\ndelaycompress\n}" >> /etc/logrotate.d/fetchmail
if service fetchmail status | grep -q "fetchmail is running"
then
service fetchmail stop
rm -f /var/run/fetchmail/fetchmail.pid
fi
CMD="service fetchmail start"
;;
list)
if grep -q "place your configuration here" $FRC
then
echo "no poll entries so far"
exit 1
fi
USRLIST='z-user; r-user; r-pwd; server; protocol; port; ssl; folder(s)'
while read LINE
do
IFS=" "
set -- $LINE
# we only take poll ntries with 13 or 1 entries
#poll pop3.svr protocol pop3 port 995 user xy pass 'z' ssl mda "pgm z-usr"
if [ $# -gt 12 ] && [ $1 = "poll" ]
then
SVR=$2
PROT=$4
PORT=$6
USR=$8
PWD=$10
# folder imap only before mda/is in pos 11 or 12 dependent on ssl
if ([ $PROT = "imap" ] || [ $PROT = "IMAP" ]) && [ $11 != "mda" ] && [ $12 != "mda" ] && [ $11 != "is" ] && [ $12 != "is" ]
then
# 2 more compared to default mda on pos 13 or 14
if [ $11 != "ssl" ]
then
FLD=$12
if [ $13 = "mda" ]
then
# two pos after mda, cut last char
ZUSR=${15%?}
else
ZUSR=$14
fi
SSL="no-ssl"
else
FLD=$13
if [ $14 = "mda" ]
then
ZUSR=${16%?}
else
ZUSR=$15
fi
SSL="ssl"
fi
else
#default pop3 no folder mda on pos 11 or 12
FLD="INBOX"
if [ $11 != "ssl" ]
then
if [ $11 = "mda" ]
then
# two pos after mda, cut last char
ZUSR=${13%?}
else
ZUSR=$12
fi
SSL="no-ssl"
else
if [ $12 = "mda" ]
then
ZUSR=${14%?}
else
ZUSR=$13
fi
SSL="ssl"
fi
fi
USRLIST="$USRLIST\n$ZUSR;$USR;$10;$SVR;$PROT;$PORT;$SSL;$FLD"
fi
done < $FRC
echo "$USRLIST"
exit 0
;;
add)
if [ $# -lt 9 ]
then
echo "please provide all fetch-mail parameters in order: z-user r-user r-pwd server protocol port ssl folder (INBOX or n/a for pop3)"
exit 1
fi
ZUSR=$2
USR=$3
PWD=$4
SVR=$5
PROT=$6
PORT=$7
SSL=$8
if ([ $PROT = "imap" ] || [ $PROT = "IMAP" ]) && [ $9 != "n/a" ]
then
FLD=" folder $9"
else
FLD=""
fi
if [ $SSL != "ssl" ]
then
SSL=""
else
SSL=" ssl"
fi
if grep $ZUSR $FRC | grep -q $USR
then
echo "no duplicate entries with same kopano and remote user allowed."
exit 1
fi
NOINIT=0
if grep -q "#place your configuration here" $FRC
then
sed -i -e "s~#place your configuration here~# poll entries~" $FRC
NOINIT=1
fi
chmod 660 $FRC
if grep -q "#preconnect_tstamp=on" $FRC
then
PRECON=" preconnect \"date >> /var/log/kopano/fetchmail.log\""
else
PRECON=""
fi
if grep -q "#mda=on" $FRC
then
# the mda mail delivery way with z-account
DELIVER="mda \"/usr/sbin/kopano-dagent $ZUSR\""
else
# the postfix way with z-email
#DOMAIN=`grep ^mydomain /etc/kopano/postfix/main.cf | cut -f2 -d'=' | grep -o '[^\t ].*'`
# set envelope X-Delivered-to localdomains $DOMAIN after fetch from
DELIVER="is $ZUSR here"
fi
echo "poll $SVR protocol $PROT port $PORT user $USR pass '${PWD}'${SSL}${FLD} ${DELIVER}${PRECON}" >> $FRC
chmod 600 $FRC
echo "OK adding fetchmail entry for z-user $ZUSR as $USR at $SVR."
if [ $NOINIT -gt 0 ]
then
echo "post adding first entry you have to run kopano-fetchmail init."
exit 0
fi
killall -q -9 fetchmail
sleep 2
if [ -e /var/run/fetchmail/fetchmail.pid ] ; then rm /var/run/fetchmail/fetchmail.pid ; fi
CMD="service fetchmail start"
;;
remove)
if [ $# -lt 3 ]
then
echo "please provide kopano user and external user of entry to remove"
exit 1
fi
LINE=`grep $2 $FRC | grep $3`
if [ -n "$LINE" ]
then
sed -i -e "s~$LINE~~" $FRC
else
echo "no match found for $2:$3"
exit 1
fi
echo "removed fetchmail entry for z-user $2 as $3."
killall -q -9 fetchmail
sleep 2
if [ -e /var/run/fetchmail/fetchmail.pid ] ; then rm /var/run/fetchmail/fetchmail.pid ; fi
CMD="service fetchmail start"
;;
*)
echo "Usage: kopano-fetchmail list, add, remove, (re)start, stop, status, init, test, help"
exit 1
;;
esac
# run command
$CMD
exit 0