/
qmail.txt
1371 lines (979 loc) · 35.4 KB
/
qmail.txt
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
: modern life with qmail
Cheatsheet for setting up qmail with modern requirements in less than 60min.
With special instructions for CentOS 6 (on DigitalOcean).
The name of the article and many contents refer to http://www.lifewithqmail.org/. [0]
For those knowing Uberspace [19]: In it's core you'll get the same setup, but without the space limit / disk quota. (Uberspace additionally has SpamAssassin, DSPAM and automx).
While searching for sources and patches, I stumbled upon a lot of dead links. Therefore I set up a mirror for everything I'm using in this tutorial [22].
<!--TOC-->
:: Some words first
We will use three variables very often: $domain, $mail_domain and $address. Depending on what you want to achieve, you should make a choice now.
Three scenarios:
* The FQDN is EXAMPLE.COM, webmail is served on EXAMPLE.COM, mails go to @EXAMPLE.COM.
* The FQDN is EXAMPLE.COM, webmail is served on e.g. mail.EXAMPLE.COM, mails go to @EXAMPLE.COM.
* The FQDN is e.g. mail.EXAMPLE.COM, webmail is served on the _same_ URL, mails go to @EXAMPLE.COM.
Set the variables accordingly. If you need something else, you can easily customize this:
====
fqdn='FOO'
cd $HOME
# 1
echo "
domain='${fqdn}'
address='${domain}'
mail_domain='${domain}'" > .domain-vars.txt
# 2
echo "
domain='${fqdn}'
address='${domain}'
mail_domain='mail.${domain}'" > .domain-vars.txt
# 3
echo "
domain='${fqdn}'
address='$(echo ${fqdn} | cut -d '.' -f 2-)'
mail_domain='${fqdn}'" > .domain-vars.txt
====
Remember to source those variables every time you start a new shell:
====
. $HOME/.domain-vars.txt
====
:: (reverse) DNS
For Reverse DNS to work, you have to name the Droplet like your host.
Don't forget to set the DNS records soon enough. E.g.:
* mail.DOMAIN
* imap.DOMAIN
* smtp.DOMAIN
* send.DOMAIN
:: general preparation
Adapt to your time zone:
====
timezone='Europe/Berlin'
\cp /etc/localtime /root/old.timezone
\rm /etc/localtime
ln -s /usr/share/zoneinfo/${timezone} /etc/localtime
====
You might want to restrict SSH login to public key authentication. Only do this, if you can sign in via public key:
====
echo '
PasswordAuthentication no
' >> /etc/ssh/sshd_config
sed -i -e '/^PasswordAuthentication [yY][eE][sS]/s/^/#/g' /etc/ssh/sshd_config
/etc/init.d/sshd restart
====
Update and install essentials:
====
yum -y update
yum -y install mksh bind-utils wget rsync telnet man
====
====
reboot
====
====
. $HOME/.domain-vars.txt
====
Set your hostname:
====
file='/etc/sysconfig/network'
sed -i -e 's/^HOSTNAME/#HOSTNAME/' $file
echo "HOSTNAME=${domain}" >> $file
#ip="$(ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}')"
#sed -i -e "s/^${ip}/#${ip}/" /etc/hosts
#printf "${ip}\t%s\n" "${domain}" >> /etc/hosts
hostname ${domain}
hostname --fqdn
====
The last line should now give you your hostname.
:: get a cert from Let's Encrypt
/Adapted from: /
[16], [26]
First we need python 2.7 in a way that dosn't break CentOS 6:
====
yum install centos-release-SCL
yum install python27
====
Now certbot:
====
yum -y install epel-release
cd $HOME
dir='src/letsencrypt'; mkdir -p $dir ; cd $dir
wget https://dl.eff.org/certbot-auto
wget -N https://dl.eff.org/certbot-auto.asc
gpg2 -k
gpg2 --recv-key A2CFB51FA275A7286234E7B24D17C995CD9775F2
gpg2 --trusted-key 4D17C995CD9775F2 --verify certbot-auto.asc certbot-auto
chmod a+x certbot-auto
====
/Use the second line to agree to their TOS without user input./
You might need to adapt the domains to your needs.
``scl`` provides python 2.7 to certbot.
====
scl enable python27 "./certbot-auto -n --email admin@${address} certonly --standalone \
-d ${domain} -d imap.${address} -d smtp.${address} -d send.${address}"
# scl enable python27 "./certbot-auto --agree-tos --email admin@${address} -n certonly --standalone \
-d ${domain} -d imap.${address} -d smtp.${address} -d send.${address}"
====
This may fail occasionally (e.g. with '[Errno 111] Connection refused'. It's fine to just try again.
Add cronjob for renewal:
====
cat > $HOME/src/letsencrypt/renew.sh <<__EOF__
#!/bin/sh
. $HOME/.domain-vars.txt
sha256sum /etc/letsencrypt/live/\${domain}/fullchain.pem \
> $HOME/src/letsencrypt/fullchain_shasum.txt
/usr/local/bin/svc -d /service/lighttpd
sleep 2
scl enable python27 "$HOME/src/letsencrypt/certbot-auto renew -q"
/usr/local/bin/svc -u /service/lighttpd
sha256sum -c $HOME/src/letsencrypt/fullchain_shasum.txt > /dev/null 2>&1
if [[ \$? -eq 0 ]]; then
exit 1
fi
__EOF__
(crontab -l 2>/dev/null; echo "$(expr $RANDOM % 60) */12 * * * mksh $HOME/src/letsencrypt/renew.sh") | crontab -
====
Some directories we may need:
====
mkdir -p /etc/ssl/certs/ /etc/ssl/private/
====
In case you used certbot with python 2.6 before use this to get rid of the old dependencies:
====
rm -Rf /root/.local/share/letsencrypt
====
:: netqmail
"qmail is a secure, reliable, efficient, simple message transfer agent. It is designed for typical Internet-connected UNIX hosts." [1]
qmail has not been changed much since 1998 lacking reasons to do so. It has a most convincible record of 0 known security holes.
netqmail is a distribution of qmail from 2007, bringing small and sensible changes for contemporary systems. New features like TLS and SMTP AUTH to adapt to the modern web are provided through patches.
Prepare:
====
yum -y install gcc gcc-c++ make patch openssl-devel mailx
====
/Remember to take snapshots throughout the process./
Get the sources:
====
cd $HOME
dir='src/netqmail'; mkdir -p $dir ; cd $dir
echo '
74300364b6be26be08e34f768532e911789827179e680bc89db3525fe415762f checkpassword-0.90.tar.gz
a55535012b2be7a52dcd9eccabb9a198b13be50d0384143bd3b32b8710df4c1f daemontools-0.76.tar.gz
4ddea56c7cbf6c64a052d98c79392d0fd72767b3440b70ad18c5def63205eda3 fake-smtp-1.0-1.noarch.rpm
08f1677785022174603c5b82a3b736615c07215d0d3f42b6e397766be8a32099 large-dns-response.patch
8e7d98d15211fc9f9c28109e942e2268f42a6672d68df92a42f2afa90ff00532 netqmail-1.06.tar.gz
2d6f3494e516d0dfbe6c1a5aecb4362771fcbd238b426ce597318835d1623dd2 netqmail-1.06-tls+auth-20151215_20160609.patch
b3b82f60ed388fad213d52e68049ed8983c029394d6bbaceaeee656c16c42867 qmailctl-script
cae4f0c4b360cc342d611e860879204a11060ce42cc7b2e2e25106dcb6d4abbf qmail-smtpd-auth-0510_tgz.bin
9478c55496df2154bb0bf6ec947ca3e8567f928ce495d09b885ac0450666f04c qmail-smtpd-run
eb1b9dc83e1fa515e77b8f6ff5b089400a4fe89b205f7078bef0dd1a56fea219 qmail-smtpd-run-checkpassword.patch
dd232e227ae9544aea37ef1ec9db7db805e8d9f41baa2924d23b53d2a9745770 qmail-smtpd-run-checkvpw.patch
4a0615cab74886f5b4f7e8fd32933a07b955536a3476d74ea087a3ea66a23e9c ucspi-tcp-0.88.tar.gz
' > SHA256SUM.txt
wget http://qmail.org/netqmail-1.06.tar.gz
wget https://cr.yp.to/ucspi-tcp/ucspi-tcp-0.88.tar.gz
wget https://cr.yp.to/daemontools/daemontools-0.76.tar.gz
wget https://cr.yp.to/checkpwd/checkpassword-0.90.tar.gz
wget https://gist.githubusercontent.com/alexh-name/0dc5dfe51d33b0214d6d601955f7b77b/raw/f420c1d3ea799ed2362a30335ed76095333cfc7c/qmailctl-script
wget https://gist.githubusercontent.com/alexh-name/80197620dfe2068a3c4ce5f892bad7ba/raw/1b453933d4067c09e718cbdb1e2b0f50b0a8c63c/qmail-smtpd-run
wget https://gist.githubusercontent.com/alexh-name/35a72e00d19e069daadf624f75fe6e12/raw/0f905e3b720c6cd9decb277d653be93f1ae20610/qmail-smtpd-run-checkpassword.patch
wget https://gist.githubusercontent.com/alexh-name/5a1dc1831999142f2c31f22acec65960/raw/391093a74d768de713a7cbb0dcc948c17b5d805f/qmail-smtpd-run-checkvpw.patch
wget https://gist.githubusercontent.com/alexh-name/0c1452ad7a5f521d2ba6134b8618a560/raw/bebfdfa2b23da67d10422acba556e1248f98dc3c/netqmail-1.06-tls+auth-20151215_20160609.patch
wget https://gist.githubusercontent.com/alexh-name/008eba81b0133a7d13228423170baf25/raw/4896bccd64ac74c2113d328144fdfdd1e15a72a5/large-dns-response.patch
wget https://qmail.jms1.net/fake-smtp-1.0-1.noarch.rpm
wget http://www.fehcom.de/qmail/auth/qmail-smtpd-auth-0510_tgz.bin
sha256sum -c SHA256SUM.txt
====
The last line must give you 'OK' for every file,
otherwise, the downloads are corrupted.
/The following heavily borrows from http://www.lifewithqmail.org/lwq.html#installation/
[0]
Bring the sources to the right places.
====
umask 022
mkdir -p /usr/local/src
cp netqmail-1.06.tar.gz ucspi-tcp-0.88.tar.gz \
checkpassword-0.90.tar.gz \
netqmail-1.06-tls+auth-20151215_20160609.patch \
large-dns-response.patch \
qmail-smtpd-auth-0510_tgz.bin /usr/local/src
mkdir -p /package
cp daemontools-0.76.tar.gz /package
chmod 1755 /package
====
Unpack:
====
cd /package
gunzip daemontools-0.76.tar.gz
tar xpf daemontools-0.76.tar
rm -f *.tar # optional, again
cd /usr/local/src
gunzip netqmail-1.06.tar.gz
tar xpf netqmail-1.06.tar
gunzip ucspi-tcp-0.88.tar.gz
tar xpf ucspi-tcp-0.88.tar
gunzip checkpassword-0.90.tar
tar -xf checkpassword-0.90.tar
#rm -f *.tar # optional, unless space is very tight
====
::: ucspi-tcp
ucspi-tcp makes the TCP server part of qmail.
====
cd /usr/local/src/ucspi-tcp-0.88
patch < /usr/local/src/netqmail-1.06/other-patches/ucspi-tcp-0.88.errno.patch
make
make setup check
====
Now qmail. Patch. Auth first:
====
cd /usr/local/src/
tar xf qmail-smtpd-auth-0510_tgz.bin -C netqmail-1.06
cd netqmail-1.06 && ./install_smtpd-auth.sh && cd ../
====
then TLS:
====
patch -d netqmail-1.06/ < netqmail-1.06-tls+auth-20151215_20160609.patch
patch -d netqmail-1.06/ < large-dns-response.patch
====
Here we will disable CRAM_MD5:
====
sed -i -e 's/^#define\ CRAM_MD5/\/\*#define\ CRAM_MD5\*\//' netqmail-1.06/qmail-smtpd.c
====
This is where qmail will go:
====
mkdir /var/qmail
====
"The easiest way to create the necessary users and groups is to create a little script file to do it for you. In the source directory you'll find a file called INSTALL.ids. It contains the command lines for many platforms, so copying the file to another name and editing that is quick and easy." [0]
Except we know we are on Linux and let sed do the work:
====
cd /usr/local/src/netqmail-1.06
cp INSTALL.ids IDS
echo '#!/bin/sh' > users.sh
grep -A8 '# groupadd nofiles' IDS | sed -e 's/^ # //g' >> users.sh
sh users.sh
====
Compile, install and set the domain.
====
make setup check && make tmprsadh && ./config-fast ${address}
====
DH parameters will be generated now. Grab some drink and do a short workout.
We choose a mode of delivery and will follow Dan J. Bernsteins advice to use Maildir over mbox or other solutions:
====
./maildirmake /etc/skel/Maildir
cp /var/qmail/boot/home /var/qmail/rc
sed -i -e 's/\/Mailbox/\/Maildir\//g' /var/qmail/rc
chmod 755 /var/qmail/rc
mkdir /var/log/qmail
echo './Maildir/' > /var/qmail/control/defaultdelivery
====
Make qmail start up and shut down cleanly with the system.
Copy a startup/shutdown script to the right place and make it executable:
====
cp $HOME/src/netqmail/qmailctl-script /var/qmail/bin/qmailctl
chmod 755 /var/qmail/bin/qmailctl
ln -s /var/qmail/bin/qmailctl /usr/local/bin
====
More stuff to do:
====
mkdir -p /var/qmail/supervise/qmail-send/log
mkdir -p /var/qmail/supervise/qmail-smtpd/log
mkdir -p /var/qmail/supervise/qmail-submission/log
echo '#!/bin/sh
exec /var/qmail/rc' > /var/qmail/supervise/qmail-send/run
echo '#!/bin/sh
exec /usr/local/bin/setuidgid qmaill /usr/local/bin/multilog t /var/log/qmail' > /var/qmail/supervise/qmail-send/log/run
cp $HOME/src/netqmail/qmail-smtpd-run /var/qmail/supervise/qmail-smtpd/run
cp $HOME/src/netqmail/qmail-smtpd-run /var/qmail/supervise/qmail-submission/run
sed -i 's/\ smtp\ /\ submission\ /' /var/qmail/supervise/qmail-submission/run
echo 20 > /var/qmail/control/concurrencyincoming
chmod 644 /var/qmail/control/concurrencyincoming
echo "${domain}" > /var/qmail/control/smtpgreeting
chmod 644 /var/qmail/control/smtpgreeting
echo '#!/bin/sh
exec /usr/local/bin/setuidgid qmaill /usr/local/bin/multilog t /var/log/qmail/smtpd' > /var/qmail/supervise/qmail-smtpd/log/run
echo '#!/bin/sh
exec /usr/local/bin/setuidgid qmaill /usr/local/bin/multilog t /var/log/qmail/submission' > /var/qmail/supervise/qmail-submission/log/run
chmod 755 /var/qmail/supervise/qmail-send/run
chmod 755 /var/qmail/supervise/qmail-send/log/run
chmod 755 /var/qmail/supervise/qmail-smtpd/run
chmod 755 /var/qmail/supervise/qmail-smtpd/log/run
chmod 755 /var/qmail/supervise/qmail-submission/run
chmod 755 /var/qmail/supervise/qmail-submission/log/run
mkdir -p /var/log/qmail/smtpd /var/log/qmail/submission
chown qmaill /var/log/qmail /var/log/qmail/smtpd /var/log/qmail/submission
echo '127.:allow,RELAYCLIENT=""' >> /etc/tcp.smtp
qmailctl cdb
====
Get rid of Sendmail:
====
/etc/init.d/sendmail stop
rpm -ivh $HOME/src/netqmail/fake-smtp-1.0-1.noarch.rpm
yum -y erase postfix
mv /usr/lib/sendmail /usr/lib/sendmail.old # ignore errors
mv /usr/sbin/sendmail /usr/sbin/sendmail.old # ignore errors
chmod 0 /usr/lib/sendmail.old /usr/sbin/sendmail.old # ignore errors
ln -s /var/qmail/bin/sendmail /usr/lib
ln -s /var/qmail/bin/sendmail /usr/sbin
====
Add aliases. Either put an actual account instead of 'postmaster', or create the user postmaster for real:
====
echo postmaster > /var/qmail/alias/.qmail-root
echo postmaster > /var/qmail/alias/.qmail-postmaster
ln -s .qmail-postmaster /var/qmail/alias/.qmail-mailer-daemon
ln -s .qmail-postmaster /var/qmail/alias/.qmail-abuse
chmod 644 /var/qmail/alias/.qmail-root /var/qmail/alias/.qmail-postmaster
useradd postmaster
====
Set a maximum size for mails in bytes. This would be 100MB:
====
echo 104857600 > /var/qmail/control/databytes
====
::: crypto
We will use haveged to increase the available entropy:
====
yum install haveged
====
Add service:
====
service='haveged'
cmd='/usr/sbin/haveged -F -v -1'
usr='root'
mkdir -m 0755 -p /var/service/"${service}"/log
mkdir -m 0750 -p /var/log/"${service}"
cat >> /var/service/"${service}"/run <<__EOF__
#!/bin/sh
exec ${cmd} 2>&1
__EOF__
cat >> /var/service/"${service}"/log/run <<__EOF__
#!/bin/sh
exec /usr/local/bin/setuidgid ${usr} /usr/local/bin/multilog t /var/log/${service}
__EOF__
chmod +x /var/service/"${service}"/run /var/service/"${service}"/log/run
chown "${usr}":"${usr}" /var/log/"${service}"
ln -s /var/service/"${service}" /service/
====
Put our certs to the right places and renew automatically:
====
dest='/var/qmail/control/servercert.pem'
cat /etc/letsencrypt/live/${domain}/privkey.pem > ${dest}
cat /etc/letsencrypt/live/${domain}/fullchain.pem >> ${dest}
chown qmaild:root ${dest}
chmod 400 ${dest}
ln -s ${dest} /var/qmail/control/clientcert.pem
cat >> $HOME/src/letsencrypt/renew.sh <<__EOF__
### renew qmail
mv ${dest} ${dest}.old
cat /etc/letsencrypt/live/\${domain}/privkey.pem > ${dest}
cat /etc/letsencrypt/live/\${domain}/fullchain.pem >> ${dest}
chown qmaild:root ${dest}
chmod 400 ${dest}
/usr/local/bin/qmailctl restart
__EOF__
====
Renew RSA and DH daily per cronjob (and be nice while doing so):
====
(crontab -l 2>/dev/null; echo "01 01 * * * nice -n 19 /var/qmail/bin/update_tmprsadh > /dev/null 2>&1") | crontab -
====
::: checkpassword
"checkpassword provides a simple, uniform password-checking interface to all root applications."
You can use this and have a fully working MTA. Later on though this guide will use VMailMgr for virtual users and to integrate qmail to dovecot. It is then possible to replace checkpassword with checkvpw for smtp auth as well, if you like.
====
cd /usr/local/src/checkpassword-0.90
patch -d . < ../netqmail-1.06/other-patches/checkpassword-0.90.errno.patch
make setup check
====
Now we need not only root but also the user qmaild to be able to check for correct passwords via checkpassword. Normally only root can do that.
For this, we have to allow qmaild's group to run checkpassword successfully:
====
chown root:$(id -g qmaild) /bin/checkpassword
chmod 4750 /bin/checkpassword
====
Now we modify /var/qmail/supervise/qmail-submission/run so that qmail-submission uses
checkpassword for authentication:
====
patch /var/qmail/supervise/qmail-submission/run < $HOME/src/netqmail/qmail-smtpd-run-checkpassword.patch
====
::: daemontools
daemontools will start and stop qmail.
====
cd /package/admin/daemontools-0.76
cd src
patch < /usr/local/src/netqmail-1.06/other-patches/daemontools-0.76.errno.patch
cd ..
package/install
====
Some trickery for CentOS to start daemontools itself on boot:
====
echo 'start on runlevel [12345]
respawn
exec /command/svscanboot' > /etc/init/svscan.conf
initctl reload-configuration
initctl start svscan
====
Now we can link qmail to /service, which will make it start:
====
ln -s /var/qmail/supervise/qmail-send /var/qmail/supervise/qmail-smtpd /var/qmail/supervise/qmail-submission /service
====
:: adding users
====
user=FOO
passwd=BAR
useradd ${user}
echo "${user}:${passwd}" | chpasswd
====
Now check, whether qmaild can authenticate the user correctly:
====
su - qmaild -c 'printf "%s\0%s\0%s\0" "'${user}'" "'${passwd}'" Y123456 | /bin/checkpassword id 3<&0'
====
:: VMailMgr
"VMailMgr (short for Virtual MAIL ManaGeR) is a package of programs designed to manage multiple domains of mail addresses and mailboxes on a single host. It co-operates with qmail for mail delivery and program control." [7]
Get sources:
====
cd $HOME
dir='src/vmailmgr'; mkdir -p $dir ; cd $dir
wget http://untroubled.org/vmailmgr/current/vmailmgr-0.97.tar.gz
wget https://gist.githubusercontent.com/alexh-name/d6e5da485a7fd9d3e3c47852fbd7bb83/raw/4ea276358e5319e084427161f0ddf8122a832643/vmailmgr-glibc-2.10+.patch
echo '
6266873db29991bf74c912e8423cee5ebf11b3b944ea413682d6b75634c399c2 vmailmgr-0.97.tar.gz
0289f47727575b3a069ee3f85b2c8530109a17f522d1383fc43330e5872f1ed2 vmailmgr-glibc-2.10+.patch
' > SHA256SUM.txt
sha256sum -c SHA256SUM.txt
====
When fetching was alright, put it to the right places to compile:
====
cp vmailmgr-0.97.tar.gz vmailmgr-glibc-2.10+.patch /usr/local/src/
cd /usr/local/src/
tar -xf vmailmgr-0.97.tar.gz
patch -d vmailmgr-0.97/authenticate/ < vmailmgr-glibc-2.10+.patch
cd vmailmgr-0.97
./configure
make
make install
====
Now, vdeliver shall manage new deliveries:
====
echo '|/usr/local/bin/vdeliver' > /var/qmail/control/defaultdelivery
====
To give virtual users the ability to authenticate for smtp too, we need to replace checkpassword by checkvpw:
====
\cp $HOME/src/netqmail/qmail-smtpd-run /var/qmail/supervise/qmail-submission/run
sed -i 's/\ smtp\ /\ submission\ /' /var/qmail/supervise/qmail-submission/run
patch /var/qmail/supervise/qmail-submission/run < $HOME/src/netqmail/qmail-smtpd-run-checkvpw.patch
chmod u+s /usr/local/bin/checkvpw
svc -du /service/qmail-submission
====
More information for configuration can be found here: [8]
But for now we can move on.
:: dovecot
Get the source [4]:
====
cd $HOME
dir='src/dovecot'; mkdir -p $dir ; cd $dir
wget https://dovecot.org/releases/2.2/dovecot-2.2.35.tar.gz
wget http://mij.oltrelinux.com/net/dovecot-qmail-vmailmgr/checkvpw-dovecot-wrapper.c
echo '
93b2acfc75da633e03d52e868755e996a377e468f0ff315060c4b8bfa21df168 checkvpw-dovecot-wrapper.c
cce15db2fc5233386b63a3cf21c465c09e6e55014ed66c4f184b7d221a47180c dovecot-2.2.35.tar.gz
' > SHA256SUM.txt
sha256sum -c SHA256SUM.txt
====
When fetching was alright, put it to the right places to compile:
====
cp dovecot-2.2.35.tar.gz checkvpw-dovecot-wrapper.c /usr/local/src/
cd /usr/local/src/
tar -xf dovecot-2.2.35.tar.gz
cd dovecot-2.2.35
./configure
make
make install
====
Dovecot needs some while. Grab a drink and do a short workout.
Create unix users [5]:
====
groupadd dovenull
useradd -r -g dovenull dovenull
groupadd dovecot
useradd -r -g dovecot dovecot
====
Later on, for integrating with VMailMgr, we need a wrapper [9]:
====
cd /usr/local/src/
cc -Wall -O2 -o checkvpw-dovecot-wrapper checkvpw-dovecot-wrapper.c
install -s -o root checkvpw-dovecot-wrapper /usr/local/bin
====
Configure dovecot [6].
First, get the example configuration files to the right place:
====
mkdir /usr/local/etc/dovecot/
cp -r /usr/local/share/doc/dovecot/example-config/* /usr/local/etc/dovecot/
cd /usr/local/etc/dovecot/conf.d/
====
Then, use checkvpw for authentication [10] [11]:
====
cp auth-checkpassword.conf.ext auth-checkvpw.conf.ext
sed -i -e 's/\/usr\/bin\/checkpassword/\/usr\/local\/bin\/checkvpw-dovecot-wrapper/g' auth-checkvpw.conf.ext
echo 'auth_mechanisms = plain
service auth {
user = root
}' >> auth-checkvpw.conf.ext
echo '!include auth-checkvpw.conf.ext' >> 10-auth.conf
sed -i -e 's/^!include\ auth-system.conf.ext/#!include\ auth-system.conf.ext/' 10-auth.conf
chgrp dovecot /usr/local/libexec/dovecot/checkpassword-reply; chmod g+s /usr/local/libexec/dovecot/checkpassword-reply
====
And set mail location:
====
sed -i -e 's/#mail_location\ =/mail_location\ =\ maildir:\%h/' 10-mail.conf
====
Touch local.conf for some more stuff:
====
cd ../
touch local.conf
====
To only serve IMAP:
====
echo 'protocols = imap' >> local.conf
====
For POP also do instead:
====
echo 'protocols = imap pop3
# to disable non-ssl pop3
service pop3-login {
inet_listener pop3 {
port = 0
}
}' >> local.conf
====
Harden the encryption:
====
echo 'ssl = required
ssl_protocols = !SSLv2 !SSLv3
ssl_dh_parameters_length = 2048' >> local.conf
====
A good idea might be to go through some checks [12].
We want dovecot to also use our Let's Encrypt certificate and it should also be renewed automatically:
====
ssl_priv='/etc/ssl/private/dovecot.pem'
ssl_cert='/etc/ssl/certs/dovecot.pem'
cp /etc/letsencrypt/live/${domain}/privkey.pem ${ssl_priv}
cp /etc/letsencrypt/live/${domain}/fullchain.pem ${ssl_cert}
chown root:root ${ssl_priv}
chown root:root ${ssl_cert}
chmod 0400 ${ssl_priv}
chmod 0444 ${ssl_cert}
cat >> $HOME/src/letsencrypt/renew.sh <<__EOF__
### renew dovecot
mv ${ssl_priv} ${ssl_priv}.old
mv ${ssl_cert} ${ssl_cert}.old
cp /etc/letsencrypt/live/\${domain}/privkey.pem ${ssl_priv}
cp /etc/letsencrypt/live/\${domain}/fullchain.pem ${ssl_cert}
chown root:root ${ssl_priv}
chown root:root ${ssl_cert}
chmod 0400 ${ssl_priv}
chmod 0444 ${ssl_cert}
/usr/local/bin/svc -du /service/dovecot
__EOF__
====
Now, make it start on boot and log:
====
mkdir -m 0755 -p /var/service/dovecot/
echo '#!/bin/sh
cmd="/usr/local/sbin/dovecot -F"
exec ${cmd} 2>&1
' > /var/service/dovecot/run
chmod +x /var/service/dovecot/run
chmod -R 0755 /var/service/dovecot
ln -s /var/service/dovecot /service/
====
:: Roundcube
HMPF. This is were huge dependencies start. For webmail we of course need a webserver, for this particular one PHP and MySQL as well.
But we are lucky: Roundcube can run on lighttpd!
First, get all the dependencies and roundcube itself (which will also install apache as an dependency...):
====
yum -y install roundcubemail
====
::: lighttpd
Install:
====
yum -y install lighttpd lighttpd-fastcgi
====
Pretty much all we have to configure is TLS.
I will go with TLS for the whole server, again using our Let's Encrypt certificate:
====
cd /etc/lighttpd/
sed -i \
-e 's/^#server\.max-fds/server.max-fds/' \
-e 's/^server\.use-ipv6/#server.use-ipv6/' lighttpd.conf
echo 'ssl.engine = "enable"
ssl.pemfile = "/etc/ssl/private/lighttpd.pem"
ssl.ca-file = "/etc/ssl/certs/CA.pem"
ssl.dh-file = "/etc/lighttpd/ssl/dh2048.pem"
ssl.cipher-list = "HIGH !aNULL !3DES +kEDH +kRSA !kSRP !kPSK"
ssl.honor-cipher-order = "enable"
ssl.use-sslv2 = "disable"
ssl.use-sslv3 = "disable"
server.port = 443
' > conf.d/TLS-global.conf
sed -i -e 's/^.*server.port.*=\ 80/#\ server.port\ =\ 80/' lighttpd.conf
echo 'include "conf.d/TLS-global.conf"' >> lighttpd.conf
sed -i -e 's/#\ *"mod_redirect",/\ \ "mod_redirect",/' modules.conf
echo '$SERVER["socket"] == ":80" {
$HTTP["host"] =~ "(.*)" {
url.redirect = ( "^/(.*)" => "https://%1/$1" )
}
}' > conf.d/HTTP-to-HTTPS.conf
echo 'include "conf.d/HTTP-to-HTTPS.conf"' >> lighttpd.conf
====
This is for simple virtual hosts:
====
sed -i -e 's/#include\ "conf\.d\/simple_vhost\.conf"/include\ "conf\.d\/simple_vhost\.conf"/' modules.conf
====
This way, we can just do:
====
mkdir -p /var/www/vhosts/${mail_domain}/htdocs/
====
which will be the root of https://${mail_domain}/.
For fastcgi:
====
sed -i \
-e 's/#include\ "conf\.d\/fastcgi\.conf"/include\ "conf\.d\/fastcgi\.conf"/' \
modules.conf
timezone="Europe\/Berlin"
sed -i \
-e 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=1/' \
-e "s/;date\.timezone\ =/date\.timezone\ =\ "${timezone}"/" \
/etc/php.ini
echo 'fastcgi.server = ( ".php" => ((
"bin-path" => "/usr/bin/php-cgi",
"socket" => "/tmp/php.socket",
"max-procs" => 1,
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "16",
"PHP_FCGI_MAX_REQUESTS" => "10000"
),
"broken-scriptfilename" => "enable"
))
)
' > conf.d/fastcgi-server.conf
echo 'include "conf.d/fastcgi-server.conf"' >> lighttpd.conf
chown :lighttpd /var/lib/php/session/
====
For more options, see lighttpd.conf.
Put the certificate to the right place and renew automatically:
====
ssl_priv='/etc/ssl/private/lighttpd.pem'
ssl_CA='/etc/ssl/certs/CA.pem'
cat /etc/letsencrypt/live/${domain}/privkey.pem > ${ssl_priv}
cat /etc/letsencrypt/live/${domain}/cert.pem >> ${ssl_priv}
cp /etc/letsencrypt/live/${domain}/chain.pem ${ssl_CA}
chown lighttpd:root ${ssl_priv}
chmod 400 ${ssl_priv}
chmod 444 ${ssl_CA}
cat >> $HOME/src/letsencrypt/renew.sh <<__EOF__
### renew lighttpd
mv ${ssl_priv} ${ssl_priv}.old
mv ${ssl_CA} ${ssl_CA}.old
cat /etc/letsencrypt/live/\${domain}/privkey.pem > ${ssl_priv}
cat /etc/letsencrypt/live/\${domain}/cert.pem >> ${ssl_priv}
cp /etc/letsencrypt/live/\${domain}/chain.pem ${ssl_CA}
chown lighttpd:root ${ssl_priv}
chmod 400 ${ssl_priv}
chmod 444 ${ssl_CA}
/usr/local/bin/svc -du /service/lighttpd
__EOF__
====
Also generate own and strong DH-Parameters and renew automatically:
====
mkdir /etc/lighttpd/ssl/ $HOME/src/lighttpd/
openssl dhparam -2 -out /etc/lighttpd/ssl/dh2048.pem.new 2048 &&
chmod 600 /etc/lighttpd/ssl/dh2048.pem.new &&
chown lighttpd:lighttpd /etc/lighttpd/ssl/dh2048.pem.new &&
mv -f /etc/lighttpd/ssl/dh2048.pem.new /etc/lighttpd/ssl/dh2048.pem
echo '#!/bin/sh
openssl dhparam -2 -out /etc/lighttpd/ssl/dh2048.pem.new 2048 &&
chmod 600 /etc/lighttpd/ssl/dh2048.pem.new &&
chown lighttpd:lighttpd /etc/lighttpd/ssl/dh2048.pem.new &&
mv -f /etc/lighttpd/ssl/dh2048.pem.new /etc/lighttpd/ssl/dh2048.pem
' > $HOME/src/lighttpd/dh.sh
chmod +x $HOME/src/lighttpd/dh.sh
(crontab -l 2>/dev/null; echo "02 02 * * * nice -n 19 $HOME/src/lighttpd/dh.sh > /dev/null 2>&1") | crontab -
====
Again, we are generating DH parameters. Grab a drink and do a short workout.
Run on startup:
====
mkdir -m 0755 -p /var/service/lighttpd
echo '#!/bin/sh
cmd="/usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf"
exec ${cmd} 2>&1
' > /var/service/lighttpd/run
chmod -R 0755 /var/service/lighttpd
ln -s /var/service/lighttpd /service/
====
And don't forget to check security. Maybe on https://www.ssllabs.com/ssltest/ [13].
You can test setup in general and virtual hosts by putting some files to ${mail_domain}:
====
cp -r /var/www/lighttpd/* /var/www/vhosts/${mail_domain}/htdocs/
====