]> git.proxmox.com Git - pmg-api.git/blob - PMG/Config.pm
improve description
[pmg-api.git] / PMG / Config.pm
1 package PMG::Config::Base;
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6
7 use PVE::Tools;
8 use PVE::JSONSchema qw(get_standard_option);
9 use PVE::SectionConfig;
10
11 use base qw(PVE::SectionConfig);
12
13 my $defaultData = {
14 propertyList => {
15 type => { description => "Section type." },
16 section => {
17 description => "Secion ID.",
18 type => 'string', format => 'pve-configid',
19 },
20 },
21 };
22
23 sub private {
24 return $defaultData;
25 }
26
27 sub format_section_header {
28 my ($class, $type, $sectionId) = @_;
29
30 die "internal error ($type ne $sectionId)" if $type ne $sectionId;
31
32 return "section: $type\n";
33 }
34
35
36 sub parse_section_header {
37 my ($class, $line) = @_;
38
39 if ($line =~ m/^section:\s*(\S+)\s*$/) {
40 my $section = $1;
41 my $errmsg = undef; # set if you want to skip whole section
42 eval { PVE::JSONSchema::pve_verify_configid($section); };
43 $errmsg = $@ if $@;
44 my $config = {}; # to return additional attributes
45 return ($section, $section, $errmsg, $config);
46 }
47 return undef;
48 }
49
50 package PMG::Config::Admin;
51
52 use strict;
53 use warnings;
54
55 use base qw(PMG::Config::Base);
56
57 sub type {
58 return 'admin';
59 }
60
61 sub properties {
62 return {
63 dailyreport => {
64 description => "Send daily reports.",
65 type => 'boolean',
66 default => 1,
67 },
68 demo => {
69 description => "Demo mode - do not start SMTP filter.",
70 type => 'boolean',
71 default => 0,
72 },
73 email => {
74 description => "Administrator E-Mail address.",
75 type => 'string', format => 'email',
76 default => 'admin@domain.tld',
77 },
78 proxyport => {
79 description => "HTTP proxy port.",
80 type => 'integer',
81 minimum => 1,
82 default => 8080,
83 },
84 proxyserver => {
85 description => "HTTP proxy server address.",
86 type => 'string',
87 },
88 proxyuser => {
89 description => "HTTP proxy user name.",
90 type => 'string',
91 },
92 proxypassword => {
93 description => "HTTP proxy password.",
94 type => 'string',
95 },
96 };
97 }
98
99 sub options {
100 return {
101 dailyreport => { optional => 1 },
102 demo => { optional => 1 },
103 email => { optional => 1 },
104 proxyport => { optional => 1 },
105 proxyserver => { optional => 1 },
106 proxyuser => { optional => 1 },
107 proxypassword => { optional => 1 },
108 };
109 }
110
111 package PMG::Config::Spam;
112
113 use strict;
114 use warnings;
115
116 use base qw(PMG::Config::Base);
117
118 sub type {
119 return 'spam';
120 }
121
122 sub properties {
123 return {
124 languages => {
125 description => "This option is used to specify which languages are considered OK for incoming mail.",
126 type => 'string',
127 pattern => '(all|([a-z][a-z])+( ([a-z][a-z])+)*)',
128 default => 'all',
129 },
130 use_bayes => {
131 description => "Whether to use the naive-Bayesian-style classifier.",
132 type => 'boolean',
133 default => 1,
134 },
135 use_awl => {
136 description => "Use the Auto-Whitelist plugin.",
137 type => 'boolean',
138 default => 1,
139 },
140 use_razor => {
141 description => "Whether to use Razor2, if it is available.",
142 type => 'boolean',
143 default => 1,
144 },
145 use_ocr => {
146 description => "Enable OCR to scan pictures.",
147 type => 'boolean',
148 default => 0,
149 },
150 wl_bounce_relays => {
151 description => "Whitelist legitimate bounce relays.",
152 type => 'string',
153 },
154 bounce_score => {
155 description => "Additional score for bounce mails.",
156 type => 'integer',
157 minimum => 0,
158 maximum => 1000,
159 default => 0,
160 },
161 rbl_checks => {
162 description => "Enable real time blacklists (RBL) checks.",
163 type => 'boolean',
164 default => 1,
165 },
166 maxspamsize => {
167 description => "Maximum size of spam messages in bytes.",
168 type => 'integer',
169 minimum => 64,
170 default => 200*1024,
171 },
172 };
173 }
174
175 sub options {
176 return {
177 use_awl => { optional => 1 },
178 use_razor => { optional => 1 },
179 use_ocr => { optional => 1 },
180 wl_bounce_relays => { optional => 1 },
181 languages => { optional => 1 },
182 use_bayes => { optional => 1 },
183 bounce_score => { optional => 1 },
184 rbl_checks => { optional => 1 },
185 maxspamsize => { optional => 1 },
186 };
187 }
188
189 package PMG::Config::ClamAV;
190
191 use strict;
192 use warnings;
193
194 use base qw(PMG::Config::Base);
195
196 sub type {
197 return 'clamav';
198 }
199
200 sub properties {
201 return {
202 dbmirror => {
203 description => "ClamAV database mirror server.",
204 type => 'string',
205 default => 'database.clamav.net',
206 },
207 archiveblockencrypted => {
208 description => "Wether to block encrypted archives. Mark encrypted archives as viruses.",
209 type => 'boolean',
210 default => 0,
211 },
212 archivemaxrec => {
213 description => "Nested archives are scanned recursively, e.g. if a ZIP archive contains a TAR file, all files within it will also be scanned. This options specifies how deeply the process should be continued. Warning: setting this limit too high may result in severe damage to the system.",
214 type => 'integer',
215 minimum => 1,
216 default => 5,
217 },
218 archivemaxfiles => {
219 description => "Number of files to be scanned within an archive, a document, or any other kind of container. Warning: disabling this limit or setting it too high may result in severe damage to the system.",
220 type => 'integer',
221 minimum => 0,
222 default => 1000,
223 },
224 archivemaxsize => {
225 description => "Files larger than this limit won't be scanned.",
226 type => 'integer',
227 minimum => 1000000,
228 default => 25000000,
229 },
230 maxscansize => {
231 description => "Sets the maximum amount of data to be scanned for each input file.",
232 type => 'integer',
233 minimum => 1000000,
234 default => 100000000,
235 },
236 maxcccount => {
237 description => "This option sets the lowest number of Credit Card or Social Security numbers found in a file to generate a detect.",
238 type => 'integer',
239 minimum => 0,
240 default => 0,
241 },
242 };
243 }
244
245 sub options {
246 return {
247 archiveblockencrypted => { optional => 1 },
248 archivemaxrec => { optional => 1 },
249 archivemaxfiles => { optional => 1 },
250 archivemaxsize => { optional => 1 },
251 maxscansize => { optional => 1 },
252 dbmirror => { optional => 1 },
253 maxcccount => { optional => 1 },
254 };
255 }
256
257 package PMG::Config::Mail;
258
259 use strict;
260 use warnings;
261
262 use PVE::ProcFSTools;
263
264 use base qw(PMG::Config::Base);
265
266 sub type {
267 return 'mail';
268 }
269
270 my $physicalmem = 0;
271 sub physical_memory {
272
273 return $physicalmem if $physicalmem;
274
275 my $info = PVE::ProcFSTools::read_meminfo();
276 my $total = int($info->{memtotal} / (1024*1024));
277
278 return $total;
279 }
280
281 sub get_max_filters {
282 # estimate optimal number of filter servers
283
284 my $max_servers = 5;
285 my $servermem = 120;
286 my $memory = physical_memory();
287 my $add_servers = int(($memory - 512)/$servermem);
288 $max_servers += $add_servers if $add_servers > 0;
289 $max_servers = 40 if $max_servers > 40;
290
291 return $max_servers - 2;
292 }
293
294 sub get_max_smtpd {
295 # estimate optimal number of smtpd daemons
296
297 my $max_servers = 25;
298 my $servermem = 20;
299 my $memory = physical_memory();
300 my $add_servers = int(($memory - 512)/$servermem);
301 $max_servers += $add_servers if $add_servers > 0;
302 $max_servers = 100 if $max_servers > 100;
303 return $max_servers;
304 }
305
306 sub get_max_policy {
307 # estimate optimal number of proxpolicy servers
308 my $max_servers = 2;
309 my $memory = physical_memory();
310 $max_servers = 5 if $memory >= 500;
311 return $max_servers;
312 }
313
314 sub properties {
315 return {
316 int_port => {
317 description => "SMTP port number for outgoing mail (trusted).",
318 type => 'integer',
319 minimum => 1,
320 maximum => 65535,
321 default => 25,
322 },
323 ext_port => {
324 description => "SMTP port number for incoming mail (untrusted). This must be a different number than 'int_port'.",
325 type => 'integer',
326 minimum => 1,
327 maximum => 65535,
328 default => 26,
329 },
330 relay => {
331 description => "The default mail delivery transport (incoming mails).",
332 type => 'string',
333 },
334 relayport => {
335 description => "SMTP port number for relay host.",
336 type => 'integer',
337 minimum => 1,
338 maximum => 65535,
339 default => 25,
340 },
341 relaynomx => {
342 description => "Disable MX lookups for default relay.",
343 type => 'boolean',
344 default => 0,
345 },
346 smarthost => {
347 description => "When set, all outgoing mails are deliverd to the specified smarthost.",
348 type => 'string',
349 },
350 banner => {
351 description => "ESMTP banner.",
352 type => 'string',
353 maxLength => 1024,
354 default => 'ESMTP Proxmox',
355 },
356 max_filters => {
357 description => "Maximum number of pmg-smtp-filter processes.",
358 type => 'integer',
359 minimum => 3,
360 maximum => 40,
361 default => get_max_filters(),
362 },
363 max_policy => {
364 description => "Maximum number of pmgpolicy processes.",
365 type => 'integer',
366 minimum => 2,
367 maximum => 10,
368 default => get_max_policy(),
369 },
370 max_smtpd_in => {
371 description => "Maximum number of SMTP daemon processes (in).",
372 type => 'integer',
373 minimum => 3,
374 maximum => 100,
375 default => get_max_smtpd(),
376 },
377 max_smtpd_out => {
378 description => "Maximum number of SMTP daemon processes (out).",
379 type => 'integer',
380 minimum => 3,
381 maximum => 100,
382 default => get_max_smtpd(),
383 },
384 conn_count_limit => {
385 description => "How many simultaneous connections any client is allowed to make to this service. To disable this feature, specify a limit of 0.",
386 type => 'integer',
387 minimum => 0,
388 default => 50,
389 },
390 conn_rate_limit => {
391 description => "The maximal number of connection attempts any client is allowed to make to this service per minute. To disable this feature, specify a limit of 0.",
392 type => 'integer',
393 minimum => 0,
394 default => 0,
395 },
396 message_rate_limit => {
397 description => "The maximal number of message delivery requests that any client is allowed to make to this service per minute.To disable this feature, specify a limit of 0.",
398 type => 'integer',
399 minimum => 0,
400 default => 0,
401 },
402 hide_received => {
403 description => "Hide received header in outgoing mails.",
404 type => 'boolean',
405 default => 0,
406 },
407 maxsize => {
408 description => "Maximum email size. Larger mails are rejected.",
409 type => 'integer',
410 minimum => 1024,
411 default => 1024*1024*10,
412 },
413 dwarning => {
414 description => "SMTP delay warning time (in hours).",
415 type => 'integer',
416 minimum => 0,
417 default => 4,
418 },
419 use_rbl => {
420 description => "Use Realtime Blacklists.",
421 type => 'boolean',
422 default => 1,
423 },
424 tls => {
425 description => "Use TLS.",
426 type => 'boolean',
427 default => 0,
428 },
429 spf => {
430 description => "Use Sender Policy Framework.",
431 type => 'boolean',
432 default => 1,
433 },
434 greylist => {
435 description => "Use Greylisting.",
436 type => 'boolean',
437 default => 1,
438 },
439 helotests => {
440 description => "Use SMTP HELO tests.",
441 type => 'boolean',
442 default => 0,
443 },
444 rejectunknown => {
445 description => "Reject unknown clients.",
446 type => 'boolean',
447 default => 0,
448 },
449 rejectunknownsender => {
450 description => "Reject unknown senders.",
451 type => 'boolean',
452 default => 0,
453 },
454 verifyreceivers => {
455 description => "Enable receiver verification. The value (if greater than 0) spefifies the numerical reply code when the Postfix SMTP server rejects a recipient address (450 or 550).",
456 type => 'integer',
457 minimum => 0,
458 maximum => 599,
459 default => 0,
460 },
461 dnsbl_sites => {
462 description => "Optional list of DNS white/blacklist domains (see postscreen_dnsbl_sites parameter).",
463 type => 'string',
464 },
465 };
466 }
467
468 sub options {
469 return {
470 int_port => { optional => 1 },
471 ext_port => { optional => 1 },
472 smarthost => { optional => 1 },
473 relay => { optional => 1 },
474 relayport => { optional => 1 },
475 relaynomx => { optional => 1 },
476 dwarning => { optional => 1 },
477 max_smtpd_in => { optional => 1 },
478 max_smtpd_out => { optional => 1 },
479 greylist => { optional => 1 },
480 helotests => { optional => 1 },
481 use_rbl => { optional => 1 },
482 tls => { optional => 1 },
483 spf => { optional => 1 },
484 maxsize => { optional => 1 },
485 banner => { optional => 1 },
486 max_filters => { optional => 1 },
487 max_policy => { optional => 1 },
488 hide_received => { optional => 1 },
489 rejectunknown => { optional => 1 },
490 rejectunknownsender => { optional => 1 },
491 conn_count_limit => { optional => 1 },
492 conn_rate_limit => { optional => 1 },
493 message_rate_limit => { optional => 1 },
494 verifyreceivers => { optional => 1 },
495 dnsbl_sites => { optional => 1 },
496 };
497 }
498 package PMG::Config;
499
500 use strict;
501 use warnings;
502 use IO::File;
503 use Data::Dumper;
504 use Template;
505
506 use PVE::SafeSyslog;
507 use PVE::Tools;
508 use PVE::INotify;
509
510 PMG::Config::Admin->register();
511 PMG::Config::Mail->register();
512 PMG::Config::Spam->register();
513 PMG::Config::ClamAV->register();
514
515 # initialize all plugins
516 PMG::Config::Base->init();
517
518
519 sub new {
520 my ($type) = @_;
521
522 my $class = ref($type) || $type;
523
524 my $cfg = PVE::INotify::read_file("pmg.conf");
525
526 return bless $cfg, $class;
527 }
528
529 sub write {
530 my ($self) = @_;
531
532 PVE::INotify::write_file("pmg.conf", $self);
533 }
534
535 my $lockfile = "/var/lock/pmgconfig.lck";
536
537 sub lock_config {
538 my ($code, $errmsg) = @_;
539
540 my $p = PVE::Tools::lock_file($lockfile, undef, $code);
541 if (my $err = $@) {
542 $errmsg ? die "$errmsg: $err" : die $err;
543 }
544 }
545
546 # set section values
547 sub set {
548 my ($self, $section, $key, $value) = @_;
549
550 my $pdata = PMG::Config::Base->private();
551
552 my $plugin = $pdata->{plugins}->{$section};
553 die "no such section '$section'" if !$plugin;
554
555 if (defined($value)) {
556 my $tmp = PMG::Config::Base->check_value($section, $key, $value, $section, 0);
557 $self->{ids}->{$section} = { type => $section } if !defined($self->{ids}->{$section});
558 $self->{ids}->{$section}->{$key} = PMG::Config::Base->decode_value($section, $key, $tmp);
559 } else {
560 if (defined($self->{ids}->{$section})) {
561 delete $self->{ids}->{$section}->{$key};
562 }
563 }
564
565 return undef;
566 }
567
568 # get section value or default
569 sub get {
570 my ($self, $section, $key) = @_;
571
572 my $pdata = PMG::Config::Base->private();
573 my $pdesc = $pdata->{propertyList}->{$key};
574 die "no such property '$section/$key'\n"
575 if !(defined($pdesc) && defined($pdata->{options}->{$section}) &&
576 defined($pdata->{options}->{$section}->{$key}));
577
578 if (defined($self->{ids}->{$section}) &&
579 defined(my $value = $self->{ids}->{$section}->{$key})) {
580 return $value;
581 }
582
583 return $pdesc->{default};
584 }
585
586 # get a whole section with default value
587 sub get_section {
588 my ($self, $section) = @_;
589
590 my $pdata = PMG::Config::Base->private();
591 return undef if !defined($pdata->{options}->{$section});
592
593 my $res = {};
594
595 foreach my $key (keys %{$pdata->{options}->{$section}}) {
596
597 my $pdesc = $pdata->{propertyList}->{$key};
598
599 if (defined($self->{ids}->{$section}) &&
600 defined(my $value = $self->{ids}->{$section}->{$key})) {
601 $res->{$key} = $value;
602 next;
603 }
604 $res->{$key} = $pdesc->{default};
605 }
606
607 return $res;
608 }
609
610 # get a whole config with default values
611 sub get_config {
612 my ($self) = @_;
613
614 my $pdata = PMG::Config::Base->private();
615
616 my $res = {};
617
618 foreach my $type (keys %{$pdata->{plugins}}) {
619 my $plugin = $pdata->{plugins}->{$type};
620 $res->{$type} = $self->get_section($type);
621 }
622
623 return $res;
624 }
625
626 sub read_pmg_conf {
627 my ($filename, $fh) = @_;
628
629 local $/ = undef; # slurp mode
630
631 my $raw = <$fh> if defined($fh);
632
633 return PMG::Config::Base->parse_config($filename, $raw);
634 }
635
636 sub write_pmg_conf {
637 my ($filename, $fh, $cfg) = @_;
638
639 my $raw = PMG::Config::Base->write_config($filename, $cfg);
640
641 PVE::Tools::safe_print($filename, $fh, $raw);
642 }
643
644 PVE::INotify::register_file('pmg.conf', "/etc/pmg/pmg.conf",
645 \&read_pmg_conf,
646 \&write_pmg_conf,
647 undef, always_call_parser => 1);
648
649 # parsers/writers for other files
650
651 my $domainsfilename = "/etc/pmg/domains";
652
653 sub read_pmg_domains {
654 my ($filename, $fh) = @_;
655
656 my $domains = [];
657
658 if (defined($fh)) {
659 while (defined(my $line = <$fh>)) {
660 if ($line =~ m/^\s*(\S+)\s*$/) {
661 my $domain = $1;
662 push @$domains, $domain;
663 }
664 }
665 }
666
667 return $domains;
668 }
669
670 sub write_pmg_domains {
671 my ($filename, $fh, $domain) = @_;
672
673 foreach my $domain (sort @$domain) {
674 PVE::Tools::safe_print($filename, $fh, "$domain\n");
675 }
676 }
677
678 PVE::INotify::register_file('domains', $domainsfilename,
679 \&read_pmg_domains,
680 \&write_pmg_domains,
681 undef, always_call_parser => 1);
682
683 my $transport_map_filename = "/etc/postfix/transport";
684
685 sub read_transport_map {
686 my ($filename, $fh) = @_;
687
688 return [] if !defined($fh);
689
690 my $res = {};
691
692 while (defined(my $line = <$fh>)) {
693 chomp $line;
694 next if $line =~ m/^\s*$/;
695 next if $line =~ m/^\s*\#/;
696
697 if ($line =~ m/^(\S+)\s+smtp:([^\s:]+):(\d+)\s*$/) {
698 my $domain = $1;
699 my $host = $2;
700 my $port =$3;
701 my $nomx;
702
703 if ($host =~ m/^\[(.*)\]$/) {
704 $host = $1;
705 $nomx = 1;
706 }
707
708 my $key = "$host:$port";
709
710 $res->{$key}->{nomx} = $nomx;
711 $res->{$key}->{host} = $host;
712 $res->{$key}->{port} = $port;
713 $res->{$key}->{transport} = $key;
714
715 push @{$res->{$key}->{domains}}, $domain;
716 }
717 }
718
719 my $ta = [];
720
721 foreach my $t (sort keys %$res) {
722 push @$ta, $res->{$t};
723 }
724
725 return $ta;
726 }
727
728 sub write_ransport_map {
729 my ($filename, $fh, $tmap) = @_;
730
731 return if !$tmap;
732
733 foreach my $t (sort { $a->{transport} cmp $b->{transport} } @$tmap) {
734 my $domains = $t->{domains};
735
736 foreach my $d (sort @$domains) {
737 if ($t->{nomx}) {
738 PVE::Tools::safe_print($filename, $fh, "$d smtp:[$t->{host}]:$t->{port}\n");
739 } else {
740 PVE::Tools::safe_print($filename, $fh, "$d smtp:$t->{host}:$t->{port}\n");
741 }
742 }
743 }
744 }
745
746 PVE::INotify::register_file('transport', $transport_map_filename,
747 \&read_transport_map,
748 \&write_ransport_map,
749 undef, always_call_parser => 1);
750
751 # config file generation using templates
752
753 sub get_template_vars {
754 my ($self) = @_;
755
756 my $vars = { pmg => $self->get_config() };
757
758 my $nodename = PVE::INotify::nodename();
759 my $int_ip = PMG::Cluster::remote_node_ip($nodename);
760 my $int_net_cidr = PMG::Utils::find_local_network_for_ip($int_ip);
761 $vars->{ipconfig}->{int_ip} = $int_ip;
762 # $vars->{ipconfig}->{int_net_cidr} = $int_net_cidr;
763
764 my $transportnets = []; # fixme
765 $vars->{postfix}->{transportnets} = join(' ', @$transportnets);
766
767 my $mynetworks = [ '127.0.0.0/8', '[::1]/128' ];
768 push @$mynetworks, @$transportnets;
769 push @$mynetworks, $int_net_cidr;
770
771 # add default relay to mynetworks
772 if (my $relay = $self->get('mail', 'relay')) {
773 if (Net::IP::ip_is_ipv4($relay)) {
774 push @$mynetworks, "$relay/32";
775 } elsif (Net::IP::ip_is_ipv6($relay)) {
776 push @$mynetworks, "[$relay]/128";
777 } else {
778 warn "unable to detect IP version of relay '$relay'";
779 }
780 }
781
782 $vars->{postfix}->{mynetworks} = join(' ', @$mynetworks);
783
784 my $usepolicy = 0;
785 $usepolicy = 1 if $self->get('mail', 'greylist') ||
786 $self->get('mail', 'spf') || $self->get('mail', 'use_rbl');
787 $vars->{postfix}->{usepolicy} = $usepolicy;
788
789 my $resolv = PVE::INotify::read_file('resolvconf');
790 $vars->{dns}->{hostname} = $nodename;
791 $vars->{dns}->{domain} = $resolv->{search};
792
793 return $vars;
794 }
795
796 # rewrite file from template
797 # return true if file has changed
798 sub rewrite_config_file {
799 my ($self, $tmplname, $dstfn) = @_;
800
801 my $demo = $self->get('admin', 'demo');
802
803 my $srcfn = ($tmplname =~ m|^.?/|) ?
804 $tmplname : "/var/lib/pmg/templates/$tmplname";
805
806 if ($demo) {
807 my $demosrc = "$srcfn.demo";
808 $srcfn = $demosrc if -f $demosrc;
809 }
810
811 my ($perm, $uid, $gid);
812
813 my $srcfd = IO::File->new ($srcfn, "r")
814 || die "cant read template '$srcfn' - $!: ERROR";
815
816 if ($dstfn eq '/etc/fetchmailrc') {
817 (undef, undef, $uid, $gid) = getpwnam('fetchmail');
818 $perm = 0600;
819 } elsif ($dstfn eq '/etc/clamav/freshclam.conf') {
820 # needed if file contains a HTTPProxyPasswort
821
822 $uid = getpwnam('clamav');
823 $gid = getgrnam('adm');
824 $perm = 0600;
825 }
826
827 my $template = Template->new({});
828
829 my $vars = $self->get_template_vars();
830
831 my $output = '';
832
833 $template->process($srcfd, $vars, \$output) ||
834 die $template->error();
835
836 $srcfd->close();
837
838 my $old = PVE::Tools::file_get_contents($dstfn, 128*1024) if -f $dstfn;
839
840 return 0 if defined($old) && ($old eq $output); # no change
841
842 PVE::Tools::file_set_contents($dstfn, $output, $perm);
843
844 if (defined($uid) && defined($gid)) {
845 chown($uid, $gid, $dstfn);
846 }
847
848 return 1;
849 }
850
851 # rewrite spam configuration
852 sub rewrite_config_spam {
853 my ($self) = @_;
854
855 my $use_awl = $self->get('spam', 'use_awl');
856 my $use_bayes = $self->get('spam', 'use_bayes');
857 my $use_razor = $self->get('spam', 'use_razor');
858
859 my $changes = 0;
860
861 # delete AW and bayes databases if those features are disabled
862 if (!$use_awl) {
863 $changes = 1 if unlink '/root/.spamassassin/auto-whitelist';
864 }
865
866 if (!$use_bayes) {
867 $changes = 1 if unlink '/root/.spamassassin/bayes_journal';
868 $changes = 1 if unlink '/root/.spamassassin/bayes_seen';
869 $changes = 1 if unlink '/root/.spamassassin/bayes_toks';
870 }
871
872 # make sure we have a custom.cf file (else cluster sync fails)
873 IO::File->new('/etc/mail/spamassassin/custom.cf', 'a', 0644);
874
875 $changes = 1 if $self->rewrite_config_file(
876 'local.cf.in', '/etc/mail/spamassassin/local.cf');
877
878 $changes = 1 if $self->rewrite_config_file(
879 'init.pre.in', '/etc/mail/spamassassin/init.pre');
880
881 $changes = 1 if $self->rewrite_config_file(
882 'v310.pre.in', '/etc/mail/spamassassin/v310.pre');
883
884 $changes = 1 if $self->rewrite_config_file(
885 'v320.pre.in', '/etc/mail/spamassassin/v320.pre');
886
887 if ($use_razor) {
888 mkdir "/root/.razor";
889
890 $changes = 1 if $self->rewrite_config_file(
891 'razor-agent.conf.in', '/root/.razor/razor-agent.conf');
892
893 if (! -e '/root/.razor/identity') {
894 eval {
895 my $timeout = 30;
896 PVE::Tools::run_command(['razor-admin', '-discover'], timeout => $timeout);
897 PVE::Tools::run_command(['razor-admin', '-register'], timeout => $timeout);
898 };
899 my $err = $@;
900 syslog('info', msgquote ("registering razor failed: $err")) if $err;
901 }
902 }
903
904 return $changes;
905 }
906
907 # rewrite ClamAV configuration
908 sub rewrite_config_clam {
909 my ($self) = @_;
910
911 return $self->rewrite_config_file(
912 'clamd.conf.in', '/etc/clamav/clamd.conf');
913 }
914
915 sub rewrite_config_freshclam {
916 my ($self) = @_;
917
918 return $self->rewrite_config_file(
919 'freshclam.conf.in', '/etc/clamav/freshclam.conf');
920 }
921
922 sub rewrite_config_postgres {
923 my ($self) = @_;
924
925 my $pgconfdir = "/etc/postgresql/9.6/main";
926
927 my $changes = 0;
928
929 $changes = 1 if $self->rewrite_config_file(
930 'pg_hba.conf.in', "$pgconfdir/pg_hba.conf");
931
932 $changes = 1 if $self->rewrite_config_file(
933 'postgresql.conf.in', "$pgconfdir/postgresql.conf");
934
935 return $changes;
936 }
937
938 # rewrite /root/.forward
939 sub rewrite_dot_forward {
940 my ($self) = @_;
941
942 my $dstfn = '/root/.forward';
943
944 my $email = $self->get('admin', 'email');
945
946 my $output = '';
947 if ($email && $email =~ m/\s*(\S+)\s*/) {
948 $output = "$1\n";
949 } else {
950 # empty .forward does not forward mails (see man local)
951 }
952
953 my $old = PVE::Tools::file_get_contents($dstfn, 128*1024) if -f $dstfn;
954
955 return 0 if defined($old) && ($old eq $output); # no change
956
957 PVE::Tools::file_set_contents($dstfn, $output);
958
959 return 1;
960 }
961
962 # rewrite /etc/postfix/*
963 sub rewrite_config_postfix {
964 my ($self) = @_;
965
966 # make sure we have required files (else postfix start fails)
967 IO::File->new($domainsfilename, 'a', 0644);
968 IO::File->new($transport_map_filename, 'a', 0644);
969
970 my $changes = 0;
971
972 if ($self->get('mail', 'tls')) {
973 eval {
974 PMG::Utils::gen_proxmox_tls_cert();
975 };
976 syslog ('info', msgquote ("generating certificate failed: $@")) if $@;
977 }
978
979 $changes = 1 if $self->rewrite_config_file(
980 'main.cf.in', '/etc/postfix/main.cf');
981
982 $changes = 1 if $self->rewrite_config_file(
983 'master.cf.in', '/etc/postfix/master.cf');
984
985 #rewrite_config_transports ($class);
986 #rewrite_config_whitelist ($class);
987 #rewrite_config_tls_policy ($class);
988
989 # make sure aliases.db is up to date
990 system('/usr/bin/newaliases');
991
992 return $changes;
993 }
994
995 sub rewrite_config {
996 my ($self, $restart_services) = @_;
997
998 if ($self->rewrite_config_postfix() && $restart_services) {
999 PMG::Utils::service_cmd('postfix', 'restart');
1000 }
1001
1002 if ($self->rewrite_dot_forward() && $restart_services) {
1003 # no need to restart anything
1004 }
1005
1006 if ($self->rewrite_config_postgres() && $restart_services) {
1007 # do nothing (too many side effects)?
1008 # does not happen anyways, because config does not change.
1009 }
1010
1011 if ($self->rewrite_config_spam() && $restart_services) {
1012 PMG::Utils::service_cmd('pmg-smtp-filter', 'restart');
1013 }
1014
1015 if ($self->rewrite_config_clam() && $restart_services) {
1016 PMG::Utils::service_cmd('clamav-daemon', 'restart');
1017 }
1018
1019 if ($self->rewrite_config_freshclam() && $restart_services) {
1020 PMG::Utils::service_cmd('clamav-freshclam', 'restart');
1021 }
1022
1023 }
1024
1025 1;