GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — VortexPanel/panel/mailx.py at main · BrowserlessAPI/VortexPanel

Expand file tree

/

mailx.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
1269 lines (1125 loc) · 50.5 KB
/

mailx.py

Copy path

File metadata and controls

1269 lines (1125 loc) · 50.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""Mail server extras: spam filter (Rspamd), DKIM signing, autoresponders (Sieve),
daily sending limits (Postfix policy service), and the SPF / DKIM / DMARC / MX
records of each mail domain.
Everything here is optional on top of the base setup in routes/mail.py and is
applied with backups: a configuration that does not validate (postfix check,
doveconf -n, rspamadm configtest) is put back and the services keep running
with the previous one.
Files
data/mail/settings.json spam filter and default limits (panel)
data/mail/limits.json per-mailbox / per-domain limits, groups (hosting accounts)
data/mail/autoresponders.json autoresponder settings per mailbox
/etc/postfix/vp_limits.json limits as read by the policy service (generated)
/var/lib/vortexpanel/mail/ counters (sqlite) and limit events (policy service, user vmail)
/var/lib/vortexpanel/dkim/ DKIM private keys (read by rspamd)
"""
import base64
import datetime
import json
import os
import random
import re
import shutil
import socket
import sqlite3
import struct
import subprocess
import threading
import time
INSTALL_DIR = '/opt/vortexpanel'
DATA = os.path.join(INSTALL_DIR, 'data', 'mail')
SETTINGS = os.path.join(DATA, 'settings.json')
LIMITS = os.path.join(DATA, 'limits.json')
AUTORESP = os.path.join(DATA, 'autoresponders.json')
INSTALL_STATE = os.path.join(DATA, 'install.json')
POLICY_LIMITS = '/etc/postfix/vp_limits.json'
STATE_DIR = '/var/lib/vortexpanel/mail'
COUNTS_DB = os.path.join(STATE_DIR, 'counts.db')
EVENTS = os.path.join(STATE_DIR, 'events.jsonl')
DKIM_DIR = '/var/lib/vortexpanel/dkim'
VMAIL_BASE = '/var/mail/vhosts'
# mailbox Sieve scripts live outside the maildir (a .dovecot.sieve in the maildir root
# looks like a mail folder to Dovecot when the home and the maildir are the same)
SIEVE_USERS = '/var/mail/vp-sieve'
SIEVE_DIR = '/etc/dovecot/sieve'
DOVECOT_EXTRA = '/etc/dovecot/conf.d/98-vortexpanel-sieve.conf'
RSPAMD_LOCAL = '/etc/rspamd/local.d'
RSPAMD_FILES = ('redis.conf', 'worker-proxy.inc', 'actions.conf', 'milter_headers.conf', 'dkim_signing.conf',
'multimap.conf', 'greylist.conf', 'vp_allow_addr.map', 'vp_allow_dom.map',
'vp_block_addr.map', 'vp_block_dom.map')
POLICY_SRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'mail_policy.py')
POLICY_SCRIPT = '/usr/local/lib/vortexpanel/mail_policy.py' # readable by vmail (the panel folder is not)
SENDMAIL_SRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'vp_sendmail.py')
SENDMAIL_WRAPPER = '/usr/local/lib/vortexpanel/vp-sendmail' # PHP sendmail_path of hosting accounts
COUNT_SOCKET = '/run/vortexpanel/mailcount.sock'
COUNT_UNIT = 'vortexpanel-mailcount'
MILTER = 'inet:localhost:11332'
SELECTOR = 'default'
DEFAULTS = {
'spam': {'enabled': True, 'junk_score': 6.0, 'reject_score': 15.0, 'greylist': False, 'learn': True,
'allow': [], 'block': []},
'limits': {'mailbox': 500, 'domain': 2000, 'alert': True},
'dmarc_policy': 'quarantine',
}
_lock = threading.Lock()
# ------------------------------------------------------------------ small helpers
def run(args, timeout=120, input_text=None):
try:
r = subprocess.run(args, capture_output=True, text=True, timeout=timeout, input=input_text)
return r.returncode, (r.stdout or '').strip(), (r.stderr or r.stdout or '').strip()
except FileNotFoundError:
return 127, '', f'{args[0]} not found'
except subprocess.TimeoutExpired:
return 124, '', f'{args[0]} timed out'
def _read_json(path, default):
try:
with open(path) as f:
data = json.load(f)
return data if isinstance(data, type(default)) else default
except (OSError, ValueError):
return default
def _write_json(path, data, mode=0o600):
os.makedirs(os.path.dirname(path), exist_ok=True)
tmp = path + '.tmp'
fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, mode)
with os.fdopen(fd, 'w') as f:
json.dump(data, f, indent=2, sort_keys=True)
os.chmod(tmp, mode)
os.replace(tmp, path)
def _write_text(path, text, mode=0o644):
os.makedirs(os.path.dirname(path), exist_ok=True)
tmp = path + '.vp-tmp'
with open(tmp, 'w') as f:
f.write(text)
os.chmod(tmp, mode)
os.replace(tmp, path)
def _backup(paths):
"""{path: content or None} for later restore."""
out = {}
for p in paths:
try:
with open(p) as f:
out[p] = f.read()
except OSError:
out[p] = None
return out
def _restore(saved):
for p, content in saved.items():
try:
if content is None:
if os.path.exists(p):
os.unlink(p)
else:
with open(p, 'w') as f:
f.write(content)
except OSError:
pass
def _postconf(key):
rc, out, _ = run(['postconf', '-h', key], timeout=30)
return out if rc == 0 else ''
def _deep_merge(base, over):
out = dict(base)
for k, v in (over or {}).items():
out[k] = _deep_merge(base[k], v) if isinstance(base.get(k), dict) and isinstance(v, dict) else v
return out
def os_family():
try:
txt = open('/etc/os-release').read().lower()
except OSError:
return 'debian'
return 'rhel' if re.search(r'^id(_like)?=.*(rhel|fedora|centos)', txt, re.M) else 'debian'
EMAIL_RE = re.compile(r'^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$')
DOMAIN_RE = re.compile(r'^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$')
# ------------------------------------------------------------------ settings
def settings():
return _deep_merge(DEFAULTS, _read_json(SETTINGS, {}))
def save_settings(new):
cur = settings()
sp = new.get('spam') or {}
for k in ('enabled', 'greylist', 'learn'):
if k in sp:
cur['spam'][k] = bool(sp[k])
for k, lo, hi in (('junk_score', 1, 50), ('reject_score', 2, 100)):
if k in sp:
try:
cur['spam'][k] = max(lo, min(hi, round(float(sp[k]), 1)))
except (TypeError, ValueError):
raise ValueError(f'{k.replace("_", " ")} must be a number')
if cur['spam']['reject_score'] <= cur['spam']['junk_score']:
raise ValueError('The refuse score must be higher than the Junk score')
for k in ('allow', 'block'):
if k in sp:
items = sp[k] if isinstance(sp[k], list) else str(sp[k]).splitlines()
clean = []
for it in items:
it = str(it).strip().lower()
if not it:
continue
if it.startswith('*@'):
it = it[2:]
if not (EMAIL_RE.match(it) or DOMAIN_RE.match(it)):
raise ValueError(f'"{it}" is not an email address or a domain')
clean.append(it)
cur['spam'][k] = sorted(set(clean))[:2000]
lim = new.get('limits') or {}
for k in ('mailbox', 'domain'):
if k in lim:
try:
cur['limits'][k] = max(0, min(1000000, int(lim[k])))
except (TypeError, ValueError):
raise ValueError('Limits must be whole numbers (0 = unlimited)')
if 'alert' in lim:
cur['limits']['alert'] = bool(lim['alert'])
if new.get('dmarc_policy') in ('none', 'quarantine', 'reject'):
cur['dmarc_policy'] = new['dmarc_policy']
_write_json(SETTINGS, cur)
return cur
def limits():
data = _read_json(LIMITS, {})
data.setdefault('mailbox', {})
data.setdefault('domain', {})
data.setdefault('groups', {})
return data
def set_mailbox_limit(email, value):
"""value None = server default, 0 = unlimited."""
email = email.lower()
with _lock:
data = limits()
if value is None or value == '':
data['mailbox'].pop(email, None)
else:
data['mailbox'][email] = max(0, min(1000000, int(value)))
_write_json(LIMITS, data)
export_limits()
def set_group(name, domains, account_limit=None, mailbox_limit=None, uid=None):
"""Limits for a group of domains (a hosting account): account_limit counts all
mailboxes of all its domains together; mailbox_limit is the default per mailbox.
None removes the value (server defaults apply). uid: the account's Linux user,
so mail its websites send (vp-sendmail) counts toward the group."""
if not re.fullmatch(r'[a-z0-9_.:-]{1,64}', name or ''):
raise ValueError('invalid group name')
with _lock:
data = limits()
g = {'domains': sorted({d.lower() for d in domains if DOMAIN_RE.match((d or '').lower())})}
if account_limit is not None:
g['limit'] = max(0, int(account_limit))
if mailbox_limit is not None:
g['mailbox_limit'] = max(0, int(mailbox_limit))
if uid is not None:
g['uid'] = int(uid)
data['groups'][name] = g
_write_json(LIMITS, data)
export_limits()
if uid is not None:
ensure_web_mail_counter()
def remove_group(name):
with _lock:
data = limits()
if data['groups'].pop(name, None) is not None:
_write_json(LIMITS, data)
export_limits()
def export_limits():
"""The file the policy service reads (world-readable, no secrets)."""
s = settings()
data = limits()
out = {'default_mailbox': int(s['limits']['mailbox']), 'default_domain': int(s['limits']['domain']),
'mailbox': data['mailbox'], 'domain': data['domain'], 'groups': data['groups'],
'uids': {str(g['uid']): n for n, g in data['groups'].items() if 'uid' in g},
'events': bool(s['limits'].get('alert', True))}
if os.path.isdir('/etc/postfix'):
_write_text(POLICY_LIMITS, json.dumps(out, indent=1, sort_keys=True), 0o644)
# ------------------------------------------------------------------ counters (read side)
def _db():
if not os.path.exists(COUNTS_DB):
return None
con = sqlite3.connect(COUNTS_DB, timeout=5)
return con
def sent_today():
"""{key: n} for today: 'mbx:<email>', 'dom:<domain>', 'grp:<group>'."""
con = _db()
if con is None:
return {}
try:
day = datetime.date.today().isoformat()
return {k: n for k, n in con.execute('SELECT key, n FROM counts WHERE day=?', (day,))}
except sqlite3.Error:
return {}
finally:
con.close()
def effective_mailbox_limit(email, lim=None, st=None):
lim = lim or limits()
st = st or settings()
email = email.lower()
if email in lim['mailbox']:
return int(lim['mailbox'][email])
dom = email.split('@', 1)[-1]
for g in lim['groups'].values():
if dom in g.get('domains', []) and 'mailbox_limit' in g:
return int(g['mailbox_limit'])
return int(st['limits']['mailbox'])
def ensure_web_mail_counter():
"""The socket vp-sendmail asks before website mail goes out (systemd, one short
process per message, running as vmail like the Postfix policy service)."""
if not os.path.isdir('/run/systemd/system') or not os.path.isdir('/etc/postfix'):
return False
install_policy_script()
try:
src = open(SENDMAIL_SRC).read()
if not os.path.exists(SENDMAIL_WRAPPER) or open(SENDMAIL_WRAPPER).read() != src:
os.makedirs(os.path.dirname(SENDMAIL_WRAPPER), exist_ok=True)
_write_text(SENDMAIL_WRAPPER, src, 0o755)
except OSError:
return False
os.makedirs(STATE_DIR, exist_ok=True)
sock = (f'[Unit]\nDescription=VortexPanel website mail counter\n\n[Socket]\nListenStream={COUNT_SOCKET}\n'
'SocketMode=0666\nAccept=yes\nMaxConnections=64\n\n[Install]\nWantedBy=sockets.target\n')
svc = ('[Unit]\nDescription=VortexPanel website mail counter (one check)\n\n[Service]\n'
f'ExecStart=/usr/bin/python3 -I {POLICY_SCRIPT} --peer\nStandardInput=socket\nStandardOutput=journal\n'
'User=vmail\nGroup=vmail\nTimeoutSec=20\n')
changed = False
for name, text in ((f'{COUNT_UNIT}.socket', sock), (f'{COUNT_UNIT}@.service', svc)):
p = f'/etc/systemd/system/{name}'
if not os.path.exists(p) or open(p).read() != text:
_write_text(p, text, 0o644)
changed = True
if changed:
run(['systemctl', 'daemon-reload'])
rc, _, _ = run(['systemctl', 'is-active', '--quiet', f'{COUNT_UNIT}.socket'])
if changed or rc != 0:
run(['systemctl', 'enable', f'{COUNT_UNIT}.socket'])
run(['systemctl', 'restart', f'{COUNT_UNIT}.socket'])
return True
def refused_today():
con = _db()
if con is None:
return 0
try:
day = datetime.date.today().isoformat()
row = con.execute("SELECT SUM(n) FROM counts WHERE day=? AND key LIKE 'refused:%'", (day,)).fetchone()
return int(row[0] or 0)
except sqlite3.Error:
return 0
finally:
con.close()
def top_refused_today():
con = _db()
if con is None:
return ''
try:
day = datetime.date.today().isoformat()
row = con.execute("SELECT key, n FROM counts WHERE day=? AND key LIKE 'refused:%' ORDER BY n DESC LIMIT 1",
(day,)).fetchone()
return row[0].split(':', 1)[1] if row else ''
except sqlite3.Error:
return ''
finally:
con.close()
def new_events(offset):
"""(events, new_offset) from the policy service's event file."""
try:
size = os.path.getsize(EVENTS)
except OSError:
return [], 0
if offset > size:
offset = 0
out = []
with open(EVENTS) as f:
f.seek(offset)
for line in f:
try:
out.append(json.loads(line))
except ValueError:
pass
offset = f.tell()
return out, offset
# ------------------------------------------------------------------ components
def components():
return {
'rspamd': bool(shutil.which('rspamd')) or os.path.exists('/usr/bin/rspamd'),
'redis': bool(shutil.which('redis-server')),
'sieve': _sieve_installed(),
'rspamd_active': run(['systemctl', 'is-active', 'rspamd'], timeout=15)[1] == 'active',
'dovecot_version': '.'.join(map(str, dovecot_version())),
}
def _sieve_installed():
for p in ('/usr/lib/dovecot/modules/lib90_sieve_plugin.so', '/usr/lib64/dovecot/lib90_sieve_plugin.so',
'/usr/lib64/dovecot/modules/lib90_sieve_plugin.so', '/usr/lib/dovecot/lib90_sieve_plugin.so'):
if os.path.exists(p):
return True
return bool(shutil.which('sievec'))
def dovecot_version():
rc, out, _ = run(['dovecot', '--version'], timeout=15)
m = re.match(r'\s*(\d+)\.(\d+)', out or '')
return (int(m.group(1)), int(m.group(2))) if (rc == 0 and m) else (0, 0)
def install_state():
return _read_json(INSTALL_STATE, {})
def start_install():
"""Install Rspamd and the Sieve plugin in the background, then apply."""
st = install_state()
if st.get('running') and time.time() - st.get('started', 0) < 1800:
raise RuntimeError('An install is already running')
state = {'running': True, 'started': int(time.time()), 'lines': ['Installing the spam filter and Sieve...'], 'ok': None}
_write_json(INSTALL_STATE, state)
def say(msg):
state['lines'].append(msg)
_write_json(INSTALL_STATE, state)
def job():
try:
fam = os_family()
env = dict(os.environ, DEBIAN_FRONTEND='noninteractive')
if fam == 'debian':
cmds = [['apt-get', 'update'], ['apt-get', 'install', '-y', 'rspamd', 'dovecot-sieve']]
if not shutil.which('redis-server'):
cmds.append(['apt-get', 'install', '-y', 'redis-server'])
else:
rel = ''
try:
rel = re.search(r'^VERSION_ID="?(\d+)', open('/etc/os-release').read(), re.M).group(1)
except (OSError, AttributeError):
pass
cmds = []
if not shutil.which('rspamd'):
say('Adding the Rspamd package repository (rspamd.com)')
try:
import urllib.request
repo = urllib.request.urlopen(f'https://rspamd.com/rpm-stable/centos-{rel or "9"}/rspamd.repo',
timeout=30).read().decode()
_write_text('/etc/yum.repos.d/rspamd.repo', repo)
cmds.append(['rpm', '--import', 'https://rspamd.com/rpm-stable/gpg.key'])
except Exception as e: # noqa: BLE001
raise RuntimeError(f'could not add the Rspamd repository: {e}')
pm = 'dnf' if shutil.which('dnf') else 'yum'
cmds.append([pm, 'install', '-y', 'rspamd', 'dovecot-pigeonhole'] + ([] if shutil.which('redis-server') else ['redis']))
for c in cmds:
say('$ ' + ' '.join(c))
r = subprocess.run(c, capture_output=True, text=True, timeout=1800, env=env)
tail = (r.stdout + r.stderr).strip().splitlines()[-4:]
for t in tail:
say(' ' + t[:200])
if r.returncode != 0:
raise RuntimeError(f'{c[0]} failed (exit {r.returncode})')
for unit in ('redis-server', 'redis'):
if run(['systemctl', 'cat', unit], timeout=15)[0] == 0:
run(['systemctl', 'enable', '--now', unit], timeout=120)
break
run(['systemctl', 'enable', '--now', 'rspamd'], timeout=120)
say('Installed. Connecting it to Postfix and Dovecot...')
log = []
err = apply_all(log)
for l in log:
say(l)
if err:
raise RuntimeError(err)
state['ok'] = True
say('Spam filter and autoresponders are ready.')
except Exception as e: # noqa: BLE001
state['ok'] = False
say(f'FAILED: {e}')
finally:
state['running'] = False
_write_json(INSTALL_STATE, state)
threading.Thread(target=job, daemon=True).start()
return state
# ------------------------------------------------------------------ apply: postfix
def install_policy_script():
"""Copy the policy service where the vmail user can run it (also after panel updates)."""
try:
with open(POLICY_SRC) as f:
src = f.read()
cur = open(POLICY_SCRIPT).read() if os.path.exists(POLICY_SCRIPT) else None
if cur != src:
os.makedirs(os.path.dirname(POLICY_SCRIPT), exist_ok=True)
os.chmod(os.path.dirname(POLICY_SCRIPT), 0o755)
_write_text(POLICY_SCRIPT, src, 0o755)
except OSError:
pass
def _postfix_policy(log):
"""Daily sending limits: a Postfix policy service checked at the end of each
message sent by a signed-in mailbox."""
os.makedirs(STATE_DIR, exist_ok=True)
run(['chown', 'vmail:vmail', STATE_DIR])
install_policy_script()
os.chmod(STATE_DIR, 0o750)
export_limits()
rc, _, err = run(['postconf', '-M',
f'vppolicy/unix=vppolicy unix - n n - 0 spawn user=vmail argv=/usr/bin/python3 {POLICY_SCRIPT}'])
if rc != 0:
return f'postconf -M vppolicy: {err}'
for st in ('vppolicy_time_limit = 3600',
'smtpd_end_of_data_restrictions = check_policy_service unix:private/vppolicy'):
rc, _, err = run(['postconf', '-e', st])
if rc != 0:
return f'postconf -e "{st}": {err}'
log.append('sending limits checked by the panel policy service')
return ''
def _postfix_milter(log, on):
cur = [m.strip() for m in re.split(r'[,\s]+', _postconf('smtpd_milters')) if m.strip()]
cur_ns = [m.strip() for m in re.split(r'[,\s]+', _postconf('non_smtpd_milters')) if m.strip()]
want = [m for m in cur if m != MILTER] + ([MILTER] if on else [])
want_ns = [m for m in cur_ns if m != MILTER] + ([MILTER] if on else [])
sets = [f'smtpd_milters = {", ".join(want)}', f'non_smtpd_milters = {", ".join(want_ns)}']
if on:
sets += ['milter_protocol = 6', 'milter_default_action = accept',
'milter_mail_macros = i {mail_addr} {client_addr} {client_name} {auth_authen}']
for st in sets:
rc, _, err = run(['postconf', '-e', st])
if rc != 0:
return f'postconf -e "{st}": {err}'
log.append('Postfix passes mail through Rspamd (spam check and DKIM signing)' if on else
'Rspamd is not connected to Postfix')
return ''
# ------------------------------------------------------------------ apply: rspamd
def _rspamd_user():
for u in ('_rspamd', 'rspamd'):
if run(['id', u], timeout=10)[0] == 0:
return u
return 'root'
def _redis_conf():
"""Rspamd keeps what it learns (Bayes), greylisting and fuzzy data in Redis."""
pw = ''
for p in ('/etc/redis/redis.conf', '/etc/redis.conf', '/etc/redis/default.conf'):
try:
m = re.search(r'^\s*requirepass\s+(\S+)', open(p).read(), re.M)
except OSError:
continue
if m:
pw = m.group(1).strip('"')
break
return ('# Managed by VortexPanel\nservers = "127.0.0.1:6379";\n'
+ (f'password = "{pw}";\n' if pw and re.fullmatch(r'[^"\\\s]{1,128}', pw) else ''))
def _rspamd_files(s):
sp = s['spam']
on = bool(sp['enabled'])
junk, rej = float(sp['junk_score']), float(sp['reject_score'])
acts = (f'reject = {rej};\nadd_header = {junk};\n'
+ (f'greylist = {max(1.0, junk - 2)};\n' if sp['greylist'] else 'greylist = null;\n')
if on else 'reject = null;\nadd_header = null;\ngreylist = null;\nrewrite_subject = null;\n')
files = {
'redis.conf': _redis_conf(),
'worker-proxy.inc': ('# Managed by VortexPanel\nmilter = yes;\ntimeout = 120s;\n'
'bind_socket = "localhost:11332";\nupstream "local" {\n default = yes;\n self_scan = yes;\n}\n'),
'actions.conf': '# Managed by VortexPanel (Mail Server > Spam & limits)\n' + acts,
'milter_headers.conf': ('# Managed by VortexPanel\nuse = ["x-spam-status", "authentication-results"];\n'
'skip_local = true;\nskip_authenticated = true;\n'),
'dkim_signing.conf': (f'# Managed by VortexPanel\npath = "{DKIM_DIR}/$domain.$selector.key";\n'
f'selector = "{SELECTOR}";\nallow_username_mismatch = true;\nuse_esld = false;\n'
'sign_local = true;\nsign_authenticated = true;\ncheck_pubkey = false;\n'
'allow_hdrfrom_mismatch = false;\n'),
'greylist.conf': '# Managed by VortexPanel\nenabled = ' + ('true' if (on and sp['greylist']) else 'false') + ';\n',
'multimap.conf': (
'# Managed by VortexPanel (allow and block lists)\n'
f'VP_ALLOW_ADDR {{ type = "from"; filter = "email:addr"; map = "{RSPAMD_LOCAL}/vp_allow_addr.map"; score = -20.0; description = "Allowed by the server administrator"; }}\n'
f'VP_ALLOW_DOM {{ type = "from"; filter = "email:domain"; map = "{RSPAMD_LOCAL}/vp_allow_dom.map"; score = -20.0; description = "Allowed by the server administrator"; }}\n'
f'VP_BLOCK_ADDR {{ type = "from"; filter = "email:addr"; map = "{RSPAMD_LOCAL}/vp_block_addr.map"; action = "reject"; message = "Blocked by the server administrator"; description = "Blocked sender"; }}\n'
f'VP_BLOCK_DOM {{ type = "from"; filter = "email:domain"; map = "{RSPAMD_LOCAL}/vp_block_dom.map"; action = "reject"; message = "Blocked by the server administrator"; description = "Blocked sender domain"; }}\n'),
'vp_allow_addr.map': '\n'.join(x for x in sp['allow'] if '@' in x) + '\n',
'vp_allow_dom.map': '\n'.join(x for x in sp['allow'] if '@' not in x) + '\n',
'vp_block_addr.map': '\n'.join(x for x in sp['block'] if '@' in x) + '\n',
'vp_block_dom.map': '\n'.join(x for x in sp['block'] if '@' not in x) + '\n',
}
return files
def _apply_rspamd(log, s):
if not os.path.isdir('/etc/rspamd'):
return 'Rspamd is not installed'
paths = [os.path.join(RSPAMD_LOCAL, n) for n in RSPAMD_FILES]
saved = _backup(paths)
user = _rspamd_user()
for name, text in _rspamd_files(s).items():
path = os.path.join(RSPAMD_LOCAL, name)
if name == 'redis.conf':
# holds the Redis password: readable by root and Rspamd only
_write_text(path, text, 0o640)
run(['chown', f'root:{user}' if user != 'root' else 'root:root', path])
else:
_write_text(path, text, 0o644)
os.makedirs(DKIM_DIR, exist_ok=True)
run(['chown', '-R', f'root:{user}' if user != 'root' else 'root:root', DKIM_DIR])
os.chmod(DKIM_DIR, 0o750)
rc, out, err = run(['rspamadm', 'configtest'], timeout=120)
if rc != 0:
_restore(saved)
return f'Rspamd rejected the configuration, the previous one was kept: {(err or out)[-400:]}'
rc, _, err = run(['systemctl', 'restart', 'rspamd'], timeout=120)
if rc != 0:
_restore(saved)
run(['systemctl', 'restart', 'rspamd'], timeout=120)
return f'Rspamd did not restart, the previous configuration was restored: {err[-300:]}'
log.append('Rspamd configured' + (' (spam check on)' if s['spam']['enabled'] else ' (spam check off, DKIM signing on)'))
return ''
# ------------------------------------------------------------------ apply: dovecot sieve
SIEVE_BEFORE = """# Managed by VortexPanel: spam goes to Junk
require ["fileinto", "mailbox"];
if anyof (header :is "X-Spam" "Yes", header :contains "X-Spam-Status" "Yes,") {
fileinto :create "Junk";
stop;
}
"""
LEARN_SPAM = """require ["vnd.dovecot.pipe", "copy", "imapsieve"];
pipe :copy "vp-learn" ["spam"];
"""
LEARN_HAM = """require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];
if environment :matches "imap.mailbox" "*" { set "mailbox" "${1}"; }
if string "${mailbox}" "Trash" { stop; }
pipe :copy "vp-learn" ["ham"];
"""
LEARN_BIN = """#!/bin/sh
# Managed by VortexPanel: teach Rspamd from messages moved into or out of Junk
case "$1" in spam|ham) ;; *) exit 0 ;; esac
exec /usr/bin/rspamc -h localhost:11334 "learn_$1" >/dev/null 2>&1
"""
def _dovecot_conf(ver, learn):
if ver >= (2, 4):
conf = ("# Managed by VortexPanel: Sieve (autoresponders, spam to Junk). Dovecot 2.4 syntax.\n"
"protocol lmtp {\n mail_plugins {\n sieve = yes\n }\n}\n"
f"sieve_script personal {{\n driver = file\n path = {SIEVE_USERS}/%{{user | domain}}/%{{user | username}}/scripts\n"
f" active_path = {SIEVE_USERS}/%{{user | domain}}/%{{user | username}}/active.sieve\n}}\n"
f"sieve_script vp_before {{\n type = before\n driver = file\n path = {SIEVE_DIR}/vp-before.sieve\n}}\n"
"namespace inbox {\n mailbox Junk {\n auto = subscribe\n special_use = \\Junk\n }\n}\n")
return conf
conf = ("# Managed by VortexPanel: Sieve (autoresponders, spam to Junk, learning). Do not edit by hand.\n"
"protocol lmtp {\n mail_plugins = $mail_plugins sieve\n}\n"
"namespace inbox {\n mailbox Junk {\n auto = subscribe\n special_use = \\Junk\n }\n}\n"
"plugin {\n"
f" sieve = file:{SIEVE_USERS}/%d/%n/scripts;active={SIEVE_USERS}/%d/%n/active.sieve\n"
f" sieve_before = {SIEVE_DIR}/vp-before.sieve\n")
if learn:
conf += (" sieve_plugins = sieve_imapsieve sieve_extprograms\n"
" sieve_global_extensions = +vnd.dovecot.pipe +vnd.dovecot.environment\n"
f" sieve_pipe_bin_dir = {SIEVE_DIR}/bin\n"
" imapsieve_mailbox1_name = Junk\n"
" imapsieve_mailbox1_causes = COPY APPEND\n"
f" imapsieve_mailbox1_before = file:{SIEVE_DIR}/vp-learn-spam.sieve\n"
" imapsieve_mailbox2_name = *\n"
" imapsieve_mailbox2_from = Junk\n"
" imapsieve_mailbox2_causes = COPY\n"
f" imapsieve_mailbox2_before = file:{SIEVE_DIR}/vp-learn-ham.sieve\n")
conf += "}\n"
if learn:
conf += "protocol imap {\n mail_plugins = $mail_plugins imap_sieve\n}\n"
return conf
BASE_CONF = '/etc/dovecot/conf.d/99-vortexpanel.conf'
VMAIL_HOME = '/var/mail/vp-home'
def _dovecot_base_home():
"""Older setups used the maildir as the Dovecot home, so vacation's memory file
(.dovecot.lda-dupes) appeared as a mail folder. Move the home out of the maildir."""
try:
from panel.routes.mail import _dovecot_conf as base_conf
except Exception: # noqa: BLE001
return ''
if not os.path.exists(BASE_CONF):
return ''
want = base_conf(dovecot_version())
cur = open(BASE_CONF).read()
os.makedirs(VMAIL_HOME, exist_ok=True)
run(['chown', 'vmail:vmail', VMAIL_HOME])
os.chmod(VMAIL_HOME, 0o770)
if cur == want or VMAIL_HOME in cur:
return ''
_write_text(BASE_CONF, want, 0o644)
if run(['doveconf', '-n'], timeout=60)[0] != 0:
_write_text(BASE_CONF, cur, 0o644)
return 'Dovecot rejected the updated mailbox home, nothing was changed'
# move what Dovecot kept in the old home (the maildir root) to the new one
for dom in os.listdir(VMAIL_BASE) if os.path.isdir(VMAIL_BASE) else []:
ddir = os.path.join(VMAIL_BASE, dom)
if not os.path.isdir(ddir):
continue
for user in os.listdir(ddir):
mdir = os.path.join(ddir, user)
for name in ('.dovecot.lda-dupes', '.dovecot.lda-dupes.locks', '.dovecot.sieve', '.dovecot.sieve.log', '.dovecot.svbin'):
p = os.path.join(mdir, name)
if os.path.lexists(p):
dst = os.path.join(VMAIL_HOME, dom, user)
os.makedirs(dst, exist_ok=True)
try:
if os.path.islink(p) or name.endswith(('.sieve', '.svbin', '.log')):
os.unlink(p) if not os.path.isdir(p) else shutil.rmtree(p, ignore_errors=True)
else:
shutil.move(p, os.path.join(dst, name))
except OSError:
pass
if os.path.isdir(os.path.join(mdir, 'sieve')) and not os.path.isdir(os.path.join(mdir, 'sieve', 'cur')):
shutil.rmtree(os.path.join(mdir, 'sieve'), ignore_errors=True)
run(['chown', '-R', 'vmail:vmail', VMAIL_HOME])
return ''
def learn_supported():
return dovecot_version() < (2, 4)
def _apply_dovecot(log, s):
if not os.path.isdir('/etc/dovecot/conf.d'):
return 'Dovecot is not installed'
if not _sieve_installed():
return 'The Dovecot Sieve plugin is not installed'
ver = dovecot_version()
learn = bool(s['spam'].get('learn')) and ver < (2, 4) and components()['rspamd']
base_err = _dovecot_base_home()
if base_err:
return base_err
files = {os.path.join(SIEVE_DIR, 'vp-before.sieve'): (SIEVE_BEFORE, 0o644),
os.path.join(SIEVE_DIR, 'vp-learn-spam.sieve'): (LEARN_SPAM, 0o644),
os.path.join(SIEVE_DIR, 'vp-learn-ham.sieve'): (LEARN_HAM, 0o644),
os.path.join(SIEVE_DIR, 'bin', 'vp-learn'): (LEARN_BIN, 0o755),
DOVECOT_EXTRA: (_dovecot_conf(ver, learn), 0o644)}
saved = _backup(list(files))
for p, (text, mode) in files.items():
_write_text(p, text, mode)
if shutil.which('sievec'):
for n in ('vp-before.sieve', 'vp-learn-spam.sieve', 'vp-learn-ham.sieve'):
if n != 'vp-before.sieve' and not learn:
continue
rc, _, err = run(['sievec', os.path.join(SIEVE_DIR, n)], timeout=60)
if rc != 0 and n == 'vp-before.sieve':
_restore(saved)
return f'sievec {n}: {err[-300:]}'
rc, _, err = run(['doveconf', '-n'], timeout=60)
if rc != 0:
_restore(saved)
return f'Dovecot rejected the Sieve configuration, the previous one was kept: {err[-400:]}'
rc, _, err = run(['systemctl', 'restart', 'dovecot'], timeout=120)
if rc != 0:
_restore(saved)
run(['systemctl', 'restart', 'dovecot'], timeout=120)
return f'Dovecot did not restart, the previous configuration was restored: {err[-300:]}'
log.append('Dovecot Sieve on (autoresponders, spam to Junk' + (', learning from Junk)' if learn else ')'))
return ''
# ------------------------------------------------------------------ apply everything
def apply_all(log, only=None):
"""Apply the extras that are installed. Returns '' or an error (configs that
failed to validate are put back)."""
if not os.path.isdir('/etc/postfix'):
return 'Postfix is not installed'
s = settings()
comp = components()
pf_saved = _backup(['/etc/postfix/main.cf', '/etc/postfix/master.cf'])
errors = []
if only in (None, 'postfix'):
err = _postfix_policy(log)
if not err and comp['rspamd']:
err = _postfix_milter(log, True)
if not err:
rc, _, perr = run(['postfix', 'check'], timeout=60)
if rc != 0:
err = f'postfix check: {perr[-300:]}'
if err:
_restore(pf_saved)
errors.append(err + ' (Postfix configuration put back)')
else:
rc, _, rerr = run(['systemctl', 'reload', 'postfix'], timeout=120)
if rc != 0:
_restore(pf_saved)
run(['systemctl', 'restart', 'postfix'], timeout=120)
errors.append(f'Postfix did not reload, configuration put back: {rerr[-200:]}')
if comp['rspamd'] and only in (None, 'rspamd'):
for d in mail_domains():
ensure_dkim_key(d)
err = _apply_rspamd(log, s)
if err:
errors.append(err)
if comp['sieve'] and only in (None, 'dovecot'):
err = _apply_dovecot(log, s)
if err:
errors.append(err)
else:
for email in list(_read_json(AUTORESP, {}).keys()):
_write_autoresponder(email)
return '; '.join(errors)
def maintenance():
"""Run every few minutes (panel.tasks backups): keep the policy service current after
panel updates and connect it on mail servers set up before it existed."""
try:
from panel.routes.mail import _mail_configured
if not os.path.isdir('/etc/postfix') or not _mail_configured():
return ''
except Exception: # noqa: BLE001
return ''
install_policy_script()
if not os.path.exists(SETTINGS):
# a mail server from before sending limits existed: keep sending unlimited
# until the administrator sets limits (Mail Server > Spam & limits)
cur = settings()
cur['limits']['mailbox'] = 0
cur['limits']['domain'] = 0
_write_json(SETTINGS, cur)
export_limits()
if any('uid' in g for g in limits()['groups'].values()):
ensure_web_mail_counter()
if 'vppolicy' not in _postconf('smtpd_end_of_data_restrictions'):
log = []
err = apply_all(log, only='postfix')
return err or 'mail sending limits connected'
return ''
# ------------------------------------------------------------------ autoresponders
def _sieve_str(s):
return '"' + str(s).replace('\\', '\\\\').replace('"', '\\"') + '"'
def _sieve_text(body):
lines = str(body).replace('\r\n', '\n').replace('\r', '\n').split('\n')
out = ['text:']
for l in lines:
out.append('.' + l if l.startswith('.') else l)
out.append('.')
return '\n'.join(out) + '\n'
def _sieve_home(email):
user, dom = email.split('@', 1)
return os.path.join(SIEVE_USERS, dom, user)
def get_autoresponder(email):
return _read_json(AUTORESP, {}).get(email.lower()) or {
'enabled': False, 'subject': '', 'body': '', 'start': '', 'end': '', 'days': 3}
def autoresponder_status(email, ar=None):
ar = ar or get_autoresponder(email)
if not ar.get('enabled'):
return 'off'
today = datetime.date.today().isoformat()
if ar.get('end') and ar['end'] < today:
return 'ended'
if ar.get('start') and ar['start'] > today:
return 'scheduled'
return 'on'
def save_autoresponder(email, d):
email = email.lower()
if not EMAIL_RE.match(email):
raise ValueError('Invalid mailbox')
ar = {'enabled': bool(d.get('enabled')),
'subject': str(d.get('subject') or '').strip()[:200],
'body': str(d.get('body') or '').strip()[:8000],
'start': str(d.get('start') or '').strip(), 'end': str(d.get('end') or '').strip(),
'days': d.get('days', 3)}
for k in ('start', 'end'):
if ar[k] and not re.fullmatch(r'\d{4}-\d{2}-\d{2}', ar[k]):
raise ValueError('Dates must be YYYY-MM-DD')
if ar['start'] and ar['end'] and ar['end'] < ar['start']:
raise ValueError('The end date is before the start date')
try:
ar['days'] = max(1, min(30, int(ar['days'])))
except (TypeError, ValueError):
raise ValueError('Days must be a number from 1 to 30')
if ar['enabled'] and not ar['body']:
raise ValueError('Write the reply message')
if any(c in ar['subject'] for c in '\r\n'):
raise ValueError('The subject must be one line')
if ar['enabled'] and not _sieve_installed():
raise ValueError('Autoresponders need the Sieve plugin: Mail Server > Spam & limits > Install')
with _lock:
data = _read_json(AUTORESP, {})
data[email] = ar
_write_json(AUTORESP, data)
_write_autoresponder(email)
return ar
def delete_autoresponder(email):
email = email.lower()
with _lock:
data = _read_json(AUTORESP, {})
if data.pop(email, None) is not None:
_write_json(AUTORESP, data)
home = _sieve_home(email)
if os.path.isdir(home):
shutil.rmtree(home, ignore_errors=True)
def _write_autoresponder(email):
ar = get_autoresponder(email)
user, dom = email.split('@', 1)
if not os.path.isdir(os.path.join(VMAIL_BASE, dom, user)):
return
home = _sieve_home(email)
sdir = os.path.join(home, 'scripts')
os.makedirs(sdir, exist_ok=True)
for d in (SIEVE_USERS, os.path.join(SIEVE_USERS, dom), home):
run(['chown', 'vmail:vmail', d])
os.chmod(d, 0o750)
script = os.path.join(sdir, 'vortex.sieve')
req = ['vacation']
cond = []
if ar.get('start') or ar.get('end'):
req += ['date', 'relational']
if ar.get('start'):
cond.append(f'currentdate :value "ge" "date" {_sieve_str(ar["start"])}')
if ar.get('end'):
cond.append(f'currentdate :value "le" "date" {_sieve_str(ar["end"])}')
noreply = ('not address :localpart :matches "from" ["*noreply*", "*no-reply*", "*donotreply*", '
'"*do-not-reply*", "mailer-daemon", "postmaster"]')
cond.append(noreply)
action = (f'vacation :days {int(ar.get("days") or 3)} :from {_sieve_str(email)} '
+ (f':subject {_sieve_str(ar["subject"])} ' if ar.get('subject') else '')
+ f':addresses [{_sieve_str(email)}]\n{_sieve_text(ar.get("body") or "")};') # text: ends with '.' on its own line
text = f'# Managed by VortexPanel (autoresponder for {email})\nrequire {json.dumps(sorted(set(req)))};\n'
if ar.get('enabled'):
text += f'if allof ({", ".join(cond)}) {{\n{action}\n}}\n'
_write_text(script, text, 0o640)
link = os.path.join(home, 'active.sieve')
try:
if os.path.islink(link) or os.path.exists(link):
os.unlink(link)
os.symlink('scripts/vortex.sieve', link)
except OSError:
pass
run(['chown', '-R', 'vmail:vmail', sdir])
run(['chown', '-h', 'vmail:vmail', link])
# an older layout kept the script in the maildir; remove it
old = os.path.join(VMAIL_BASE, dom, user, '.dovecot.sieve')
if os.path.islink(old):
try:
os.unlink(old)
shutil.rmtree(os.path.join(VMAIL_BASE, dom, user, 'sieve'), ignore_errors=True)
except OSError:
pass
# ------------------------------------------------------------------ DKIM keys and DNS records
def mail_domains():
out = []
try:
for line in open('/etc/postfix/virtual_mailbox_domains'):
t = line.split()
if t and not t[0].startswith('#'):
out.append(t[0].lower())
except OSError:
pass
return out
def dkim_key_path(domain):
return os.path.join(DKIM_DIR, f'{domain}.{SELECTOR}.key')
def ensure_dkim_key(domain, renew=False):
"""Create (or reuse an older opendkim key) the private key; returns the TXT value."""
if not DOMAIN_RE.match(domain):
raise ValueError('Invalid domain')
path = dkim_key_path(domain)
os.makedirs(DKIM_DIR, exist_ok=True)
if renew or not os.path.exists(path):
legacy = f'/etc/opendkim/keys/{domain}/default.private'
View remainder of file in raw view
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.