-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcommon.conf
More file actions
593 lines (506 loc) · 18.4 KB
/
common.conf
File metadata and controls
593 lines (506 loc) · 18.4 KB
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
# Common configurations for several scripts
# This config is not minded to be edited by users
#
# Do not change this unless you know what are you doing!!!
# Load OS metadata
source /etc/os-release
# Author Email
AUTHOR=pavelmc@gmail.com
# APT options:
# '-y -qq' for automatic and super quiet installs
# '-y' for noisy but detailed installs
APT_OPTS="-y -qq"
export DEBIAN_FRONTEND=noninteractive
# OS support matrix
# Ubuntu
OS_DISCONTINUED_U=("bionic" "focal")
OS_LEGACY_U=("jammy")
OS_SUPPORTED_U=("noble")
# Debian
OS_DISCONTINUED_D=("buster")
OS_LEGACY_D=("bullseye")
OS_SUPPORTED_D=("bookworm" "trixie")
# mix
OS_DISCONTINUED=("${OS_DISCONTINUED_U[@]}" "${OS_DISCONTINUED_D[@]}")
OS_LEGACY=("${OS_LEGACY_U[@]}" "${OS_LEGACY_D[@]}")
OS_SUPPORTED=("${OS_SUPPORTED_U[@]}" "${OS_SUPPORTED_D[@]}")
# all supported distros
OS_WORKING_U=("${OS_DISCONTINUED_U[@]}" "${OS_LEGACY_U[@]}" "${OS_SUPPORTED_U[@]}")
OS_WORKING_D=("${OS_DISCONTINUED_D[@]}" "${OS_LEGACY_D[@]}" "${OS_SUPPORTED_D[@]}")
OS_WORKING=("${OS_WORKING_U[@]}" "${OS_WORKING_D[@]}")
# Common deps packages
COMMON_DEPS_PKGS="rsyslog ldap-utils libldap-common dnsutils netcat-traditional openssl ca-certificates wget git gettext-base cron sudo"
#### Pkgs to install for Debian Buster & Bullseye (10/11)
DEBIAN_BASE_PKGS="postfix postfix-pcre postfix-ldap dovecot-core dovecot-pop3d dovecot-imapd dovecot-ldap dovecot-sieve dovecot-managesieved libnet-ldap-perl rsync dnsutils pflogsumm mailutils p7zip-full unrar-free cabextract amavisd-new"
# Pkgs to install for Ubuntu Bionic & Focal (18.04/20.04)
UBUNTU_BASE_PKGS="postfix postfix-pcre postfix-ldap dovecot-core dovecot-pop3d dovecot-imapd dovecot-ldap dovecot-sieve dovecot-managesieved libnet-ldap-perl rsync dnsutils pflogsumm mailutils p7zip-full unrar-free cabextract amavisd-new-postfix p7zip-rar"
### Spamassassin pkgs [olds by default]
DEBIAN_SPAMD_PKGS="spamassassin"
SPAMD_SERVICENAMES="spamassassin"
case "${VERSION_CODENAME}" in
noble|trixie)
DEBIAN_SPAMD_PKGS="spamd"
SPAMD_SERVICENAMES="spamd"
;;
esac
### Clamav pkgs
DEBIAN_AV_PKGS="clamav-freshclam clamav-daemon"
### Disclaimer pkgs
DEBIAN_DISCLAIMER_PKGS="altermime"
### HTTP server pkgs
WEBSERVER_PKGS="nginx php-fpm php-dom php-mbstring php-bz2 php-zip php-json php-xml php-net-ldap3 php-ldap php-gd php-exif php-sqlite3 php-tidy fail2ban nftables sqlite3 dbconfig-sqlite3 php-mysql"
### WEBMAILS pkgs
ROUNDCUBE_PKGS="roundcube roundcube-sqlite3 roundcube-plugins-extra roundcube-mysql"
SNAPPY_PKGS=""
SNAPPY_DIR="/var/www/snappymail"
#### Common service names to start/stop (TODO if the same as PKGCOMMON reduce it at the end)
SERVICENAMES="dovecot postfix amavisd-new"
# but for debian 11 and ubuntu 22.04 and onwards amavisd-new is just "amavis"
case "${VERSION_CODENAME}" in
bullseye|jammy|noble|bookworm|trixie)
SERVICENAMES="dovecot postfix amavis"
;;
esac
### AV service names
AV_SERVICENAMES="clamav-daemon clamav-freshclam"
# Get the var names from the config file
VARS=$(cat mailad.conf | grep -v "#" | sort -r | uniq | cut -d "=" -f 1)
### Special vars that need a special care
# local domain: will be filled on provision.sh script
ESCDOMAIN=""
# National/regional domain to restric some users
ESCNATIONAL=".cu"
# install data for the feedback
INSTFILE=/etc/mailad/installs.data
###########################################
# Common functions
###########################################
# Vars needed for the functions below
PKGS=""
SRVS=""
# Debian-like pkgs already installed check
function already_installed_debs {
# List of pkgs is dynamically assigned by the code below
# Check if there is already one of them installed and warn the user about it
# offering a way to uninstall it
for p in $(echo $PKGCOMMON | xargs) ; do
# Test if the pkg is installed
LIST=$(dpkg -l | grep $p)
if [ "$LIST" != "" ] ; then
# Fail, some of the packages are installed
echo "===> ERROR!"
echo " Some of the pkgs we are about to install are already installed"
echo " so, this system is dirty and it's not recommended to install it"
echo " here; or you can force a purge runnig: 'make install-purge'"
echo " and run 'make install' again"
echo " "
exit 1
fi
done
}
# Debian packages install
function install_debs {
# Fix ubuntu bug: see https://bugs.launchpad.net/ubuntu/+source/amavisd-new/+bug/251377
echo "===> Apply Ubuntu fix for some envs [Github runners?]"
dirs=(/ /usr /bin /sbin /usr/bin /usr/sbin /usr/local /usr/local/bin /usr/local/sbin)
changed_dirs=()
for dir in "${dirs[@]}"; do
if [ -d "$dir" ]; then
perm=$(stat -c "%a" "$dir" 2>/dev/null)
if [ "$perm" -eq 777 ]; then
echo "Changing permissions of $dir from 777 to 755"
chmod 755 "$dir" && changed_dirs+=("$dir")
fi
else
echo "Notice: $dir does not exist, skipping."
fi
done
# Do it
export DEBIAN_FRONTEND=noninteractive
apt-get install ${APT_OPTS} $PKGS
# Checking for success
R=$?
if [ $R -ne 0 ] ; then
# Debug
dervices
# apt install failed on any way
echo "==========================================================================="
echo "ERROR: The update and install of the main software failed, this is mostly"
echo " a problem related to a bad configured repository or a not reacheable"
echo " one, please fix that and try again."
echo "==========================================================================="
echo " The deps install process will stop now"
echo "==========================================================================="
# Exit 1
exit 1
fi
}
# Return the pkgs for the AV install if it's enabled
function debian_av_pkgs {
# Load the config file on demand
source /etc/mailad/mailad.conf
# Check for AV
if [ "$ENABLE_AV" == "yes" -o "$ENABLE_AV" == "Yes" ] ; then
echo "${DEBIAN_AV_PKGS}"
fi
}
# Return the pkgs for the spamd install if it's enabled
function debian_spamd_pkgs {
# Load the config file on demand
source /etc/mailad/mailad.conf
# Check for spamd
if [ "$ENABLE_SPAMD" == "yes" -o "$ENABLE_SPAMD" == "Yes" ] ; then
echo "${DEBIAN_SPAMD_PKGS}"
fi
}
# Return the pkgs for disclaimer settings
function debian_disclaimer_pkgs {
# Load the config file on demand
source /etc/mailad/mailad.conf
# Check for spamd
if [ "$ENABLE_DISCLAIMER" == "yes" -o "$ENABLE_DISCLAIMER" == "Yes" ] ; then
echo "${DEBIAN_DISCLAIMER_PKGS}"
fi
}
# Craft the correct set if pkgs
function craft_pkg_list {
# Just one parameter, the distro name in lowercase
# Load the config file on demand
source /etc/mailad/mailad.conf
# Optional Debian pkgs from the configs
DAV=$(debian_av_pkgs)
DSPAMD=$(debian_spamd_pkgs)
DDISC=$(debian_disclaimer_pkgs)
case "$1" in
"debian")
PKGS="${DEBIAN_BASE_PKGS} $DAV $DSPAMD $DDISC"
;;
"ubuntu")
PKGS="${UBUNTU_BASE_PKGS} $DAV $DSPAMD $DDISC"
;;
*)
echo "=== ERROR === Not suported distribution"
exit 1
;;
esac
}
# Remove the pkgs in debian-like distros
function debian_remove_pkgs {
# Add an asterisk at the end of the PKGS to wipe al related packages
P=$(echo "$PKGS" | tr ' ' '\n' | sort | uniq | xargs | sed s/" "/"* "/g)
# Remove all pkgs letting apt build the tree
# two times to fix problems with postfix-amavis-new
apt-get purge $P* ${APT_OPTS}
apt-get purge $P* ${APT_OPTS}
# Autoremove some of the pkgs left over
apt-get autoremove ${APT_OPTS}
}
# Craft the list of services to stop/start/restart, etc
function craft_services {
# Load local config
source /etc/mailad/mailad.conf
# SRV = basic services
SRVS=${SERVICENAMES}
# Check for SPAMD
if [ "$ENABLE_SPAMD" == "yes" -o "$ENABLE_SPAMD" == "Yes" ] ; then
SRVS="${SRVS} ${SPAMD_SERVICENAMES}"
fi
# Check for AV
if [ "$ENABLE_AV" == "yes" -o "$ENABLE_AV" == "Yes" ] ; then
SRVS="${SRVS} ${AV_SERVICENAMES}"
fi
}
# Control the services
function services() {
# Just one argument: the action (start/stop/reload/etc)
# Build the dynamic list of services
craft_services
# Magic in 1... 2... 3:
for s in $(echo ${SRVS} | xargs) ; do
# Do it
echo "===> Doing $1 with $s..."
systemctl --no-pager $1 $s
sleep 2
systemctl --no-pager status $s
done
}
# Debug status of the services
function dervices() {
# no parameters will output to /tests/[service].log
for s in $(echo ${SRVS} | xargs) ; do
echo "===> Dumping logs of $s to $(pwd)/tests/$s.log"
journalctl -xeu $s 2>&1 | tee -a ./tests/$s.log
done
# also syslog
tail -n 500 /var/log/syslog > ./tests/syslog.log
}
# Restore an individual files
function extract() {
# 3 Arguments
# 1 - Backup file full path
# 2 - File to extract
# 3 - [optional] Alternative path to extract
BKPFILE="$1"
FILE="$2"
ALT="$3"
# Change dir to root
cd /
ISTHERE=$(tar -ztf ${BKPFILE} | grep "$FILE" | grep -v .db)
if [ "$ISTHERE" == "$FILE" ] ; then
# It's there
if [ "$ALT" != "" ] ; then
# Place it under $ALT
tar -zxvf ${BKPFILE} ${FILE}
mv ${FILE} ${ALT}
echo "Moved to ${ALT}"
else
# Place it on the default file path
tar -zxvf ${BKPFILE} ${FILE}
fi
fi
}
# Warn about a not supported dovecot version
function dovecot_version {
# Just one argument, the dovecot version
echo "==========================================================================="
echo "ERROR: Can't locate the dovecot version or it's a not supported one"
echo " detected version is: '$1'"
echo " and it must be v2.2, v2.3 or v2.4 "
echo "==========================================================================="
echo " The install process will stop now, please fix that"
echo "==========================================================================="
# exit
exit 1
}
# Disable AV services
function disable_av() {
# No AV, stop services to save resources if not stoped/disabled/masked
ACTIVE=$(systemctl is-active clamav-freshclam)
if [ "$ACTIVE" == "active" ] ; then
# disabling
systemctl stop clamav-freshclam || true
systemctl disable clamav-freshclam || true
systemctl mask clamav-freshclam || true
echo "===> Disabing Clamav-freshclam as requested by the config"
fi
ACTIVE=$(systemctl is-active clamav-daemon)
if [ "$ACTIVE" == "active" ] ; then
# disabling
systemctl stop clamav-daemon || true
systemctl disable clamav-daemon || true
systemctl mask clamav-daemon || true
echo "===> Disabing clamav-daemon as requested by the config"
fi
}
# Enable AV services
function enable_av() {
ACTIVE=$(systemctl is-active clamav-freshclam)
if [ "$ACTIVE" == "inactive" ] ; then
systemctl unmask clamav-freshclam
systemctl enable clamav-freshclam
systemctl restart clamav-freshclam
echo "===> Enabling Clamav-freshclam as requested by the config"
fi
ACTIVE=$(systemctl is-active clamav-daemon)
if [ "$ACTIVE" == "inactive" ] ; then
systemctl unmask clamav-daemon
systemctl enable clamav-daemon
systemctl restart clamav-daemon
echo "===> Enabling Clamav-daemon as requested by the config"
fi
}
# Enable SpamAssasin services
function enable_sa() {
ACTIVE=$(systemctl is-active ${SPAMD_SERVICENAMES})
if [ "$ACTIVE" == "inactive" ] ; then
systemctl unmask ${SPAMD_SERVICENAMES}
systemctl enable ${SPAMD_SERVICENAMES}
systemctl restart ${SPAMD_SERVICENAMES}
echo "===> Enabling SpamAssassin as requested by the config"
fi
}
# Disable SpamAssasin services
function disable_sa() {
ACTIVE=$(systemctl is-active ${SPAMD_SERVICENAMES})
if [ "$ACTIVE" == "active" ] ; then
systemctl stop ${SPAMD_SERVICENAMES}
systemctl mask ${SPAMD_SERVICENAMES}
systemctl disable ${SPAMD_SERVICENAMES}
echo "===> Disabling SpamAssassin as requested by the config"
fi
}
# Get the ldap uri based on the file options
# same function on scripts/groups.sh file
function get_ldap_uri {
# Import local settings
source /etc/mailad/mailad.conf
PROTO="ldaps"
PORT=636
# detect if NOT secure ldap and change the proto and port of the uri
if [ "$SECURELDAP" == "" -o "$SECURELDAP" == "no" -o "$SECURELDAP" == "No" ] ; then
# Use a not secure ldap
PROTO="ldap"
PORT=389
fi
SOUT=""
# Fun start here
for DC in $(echo "${HOSTAD}") ; do
if [ ! "${SOUT}" ] ; then
SOUT="${PROTO}://${DC}:${PORT}"
else
SOUT="${SOUT} ${PROTO}://${DC}:${PORT}"
fi
done
echo "${SOUT}"
}
# get the SOA server from the list of the HOSTAD or fail if not
function get_soa {
# Import local settings
source /etc/mailad/mailad.conf
# development trick
if [ "$DOMAIN" = "mailad.cu" ]; then
echo "$HOSTAD"
exit 0
fi
SOAREC=$(dig SOA $DOMAIN +short)
if [ -z "$SOAREC" ] ; then
# fails silently
echo ""
else
# get hostname and IP of the SOA
HOST=$(echo $SOAREC | awk '{print $1}' | rev | cut -d "." -f 2- | rev)
# fail safes
if [ -z "${HOST}" ] ; then
# fails silently
echo ""
fi
IPSOA=$(dig A $HOST +short)
if [ -z "${IPSOA}" ] ; then
# fails silently
echo ""
fi
# testing if the SOA is listed in the HOSTAD var
for R in $(echo "${HOSTAD}") ; do
if [ "${R}" == "${HOST}" ] ; then
# it's a hostname
echo "${R}"
fi
if [ "${R}" == "${IPSOA}" ] ; then
# it's a IP, fails silently
echo ""
fi
done
# fails silently
echo ""
fi
}
# get the SOA server from the list of the HOSTAD or fail if not
function get_soa_verbose {
# Import local settings
source /etc/mailad/mailad.conf
# development trick
if [ "$DOMAIN" = "mailad.cu" ]; then
echo "$HOSTAD"
exit 0
fi
SOAREC=$(dig SOA $DOMAIN +short)
if [ -z "$SOAREC" ] ; then
# fail
echo "================================================================================="
echo "ERROR!"
echo " The DOMAIN you declared in mailad.conf has no SOA record in the actual DNS"
echo " That, or your DNS is not configured correctly in this host"
echo "================================================================================="
echo " "
exit 1
else
# get hostname and IP of the SOA
HOST=$(echo $SOAREC | awk '{print $1}' | rev | cut -d "." -f 2- | rev)
# fail safes
if [ -z "${HOST}" ] ; then
echo "================================================================================="
echo "ERROR!"
echo " Strange!? got a SOA record for domain but after parsing to get the host it's"
echo " empty!: ${SOAREC} > ${HOST}"
echo "================================================================================="
echo " "
exit 1
fi
IPSOA=$(dig A $HOST +short)
if [ -z "${IPSOA}" ] ; then
# fail
echo "================================================================================="
echo "ERROR!"
echo " Strange!? got a SOA record & hostname, but after parsing to get the IP it's"
echo " empty!: ${SOAREC} > ${HOST} > ${IPSOA}"
echo "================================================================================="
echo " "
exit 1
fi
# testing if the SOA is listed in the HOSTAD var
RESULT=""
for R in $(echo "${HOSTAD}") ; do
if [ "${R}" == "${HOST}" ] ; then
# it's a hostname
RESULT="h"
echo "===> The SOA record is listed as a hostname in HOSTAD!"
break
fi
if [ "${R}" == "${IPSOA}" ] ; then
# it's a hostname
RESULT="i"
echo "===> The SOA record is listed as an IP in HOSTAD!"
break
fi
done
# test for empty results
if [ -z "${RESULT}" ] ; then
# fail
echo "================================================================================="
echo "ERROR!"
echo " None of the HOSTs in the HOSTAD list is the SOA of the domain, please"
echo " check that, you must list the SOA in there!"
echo ""
echo " '${HOSTAD}' vs. ${HOST}"
echo "================================================================================="
echo " "
exit 1
fi
#test for IPs
if [ "${RESULT}" == "i" ] ; then
# fail
echo "================================================================================="
echo "ERROR!"
echo " You specified the SOA server in IP format, that's ok, but will broke the"
echo " LDAP's SSL certification checking procedure."
echo ""
echo " You must use the a FQDN of that IP in the HOSTAD variable in the"
echo " /etc/mailad/mailad.conf file."
echo ""
echo " Use '${HOST}' instead of '${IPSOA}'"
echo "================================================================================="
echo " "
exit 1
fi
fi
}
# Send and email using swaks, sender is always $ADMINMAIL
function send_email {
# Parameters
# 1- subject
# 2- recipients
# 3- file path to body
FROM=${ADMINMAIL}
SUBJECT="${1}"
RECIPIENTS="${2}"
BODYFILE="${3}"
# doit:
cat "${BODYFILE}" | swaks \
-s 127.0.0.1 \
--protocol SMTP \
-t "${RECIPIENTS}" \
-f "${FROM}" \
--h-Subject "${SUBJECT}" \
-ha \
--body -
}