mongle_dkim_config 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. no warnings qw{experimental};
  5. use feature qw{signatures};
  6. use List::Util qw{uniq};
  7. use Config::Simple;
  8. use File::Copy;
  9. use File::Touch;
  10. use DNS::Unbound;
  11. use Net::DNS::Packet;
  12. my @domains2add = @ARGV;
  13. my $dkim_config_file = "/etc/opendkim.conf";
  14. my $trusted_hosts_file = '/etc/opendkim/TrustedHosts';
  15. my $keytable_file = '/etc/opendkim/KeyTable';
  16. my $signing_table_file = '/etc/opendkim/SigningTable';
  17. DKIM_CONFIG: {
  18. my $cfg = Config::Simple->new($dkim_config_file);
  19. die "Can't open opendkim config file" unless $cfg;
  20. $cfg->param('KeyTable', $keytable_file );
  21. $cfg->param('SigningTable', $signing_table_file);
  22. $cfg->param('ExternalIgnoreList', $trusted_hosts_file);
  23. $cfg->param('InternalHosts', $trusted_hosts_file);
  24. # This way we support signing more than one domain
  25. $cfg->delete('Domain');
  26. $cfg->delete('KeyFile');
  27. $cfg->delete('Selector');
  28. File::Copy::copy($dkim_config_file, "$dkim_config_file.bak") or die "Could not back up old dkim config";
  29. $cfg->save();
  30. print "OpenDKIM config file ($dkim_config_file) changed.\n";
  31. }
  32. TRUSTED_HOSTS: {
  33. my @hosts = read_lines( $trusted_hosts_file );
  34. my @ips2add = grep { defined $_ } map {
  35. ( domain2ips( $_, "A" ),
  36. domain2ips( $_, "AAAA" ) )
  37. } @domains2add;
  38. push(@hosts, "127.0.0.1", "localhost", "::1", @domains2add, @ips2add);
  39. @hosts = uniq @hosts;
  40. backup_and_emit( $trusted_hosts_file, @hosts);
  41. }
  42. KEY_TABLE: {
  43. my @lines = read_lines( $keytable_file );
  44. push(@lines, (map { "mail._domainkey.$_ $_:mail:/etc/opendkim/keys/$_/mail.private" } @domains2add ) );
  45. @lines = uniq @lines;
  46. backup_and_emit($keytable_file, @lines);
  47. }
  48. SIGNING_TABLE: {
  49. my @lines = read_lines( $signing_table_file );
  50. push(@lines, (map { "$_ mail._domainkey.$_" } @domains2add ) );
  51. @lines = uniq @lines;
  52. backup_and_emit($signing_table_file, @lines);
  53. }
  54. sub read_lines( $file ) {
  55. File::Touch::touch($file);
  56. open(my $fh, '<', $file);
  57. my @lines = map { chomp $_; $_ } readline $fh;
  58. close $fh;
  59. return @lines;
  60. }
  61. sub backup_and_emit($file, @lines) {
  62. File::Copy::copy($file, "$file.bak") or die "Could not back up $file";
  63. open(my $wh, '>', $file);
  64. foreach my $line (@lines) {
  65. print $wh "$line\n";
  66. }
  67. close $wh;
  68. print "$file changed.\n";
  69. }
  70. sub domain2ips( $domain, $type ) {
  71. my $resolver = DNS::Unbound->new();
  72. my $p = $resolver->resolve( $domain, $type )->answer_packet();
  73. my @rrs = Net::DNS::Packet->new( \$p )->answer;
  74. return map { $_->address } @rrs;
  75. }