Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/perl
  2. #
  3. # Generate an 'everybody' alias for a domain.
  4. #
  5. # Create the file /etc/mkeveryone.conf
  6. # chmod 640 /etc/mkeveryone.conf
  7. # Example of mkeveryone.conf
  8. #
  9. # userid=postfix
  10. # passwd=postfix
  11. # db=postfix
  12. # host=localhost
  13. # port=3306
  14. # domain=domain.tld
  15. # target=everybody@domain.tld
  16. # ignore=vacation@domain.tld
  17. # ignore=spam@domain.tld
  18. # ignore=newsletter@domain.tld
  19. # ignore=root@domain.tld
  20. #
  21. # Save this file in, for example, /usr/local/sbin/mkeveryone.pl
  22. # chmod 750 /usr/local/sbin/mkeveryone.pl
  23. #
  24. # Run the script!
  25. #
  26. use DBI;
  27. use Time::Local;
  28. use POSIX qw(EAGAIN);
  29. use Fcntl;
  30. use IO;
  31. use IO::File;
  32. my $timeNow=time();
  33. my $DATFILE = "/etc/mkeveryone.conf";
  34. my $FILEHANDLE = "";
  35. # database information
  36. my $db="postfix";
  37. my $host="localhost";
  38. my $port="3306";
  39. my $userid="postfix";
  40. my $passwd="postfix";
  41. my $domain="domain.tld";
  42. my $target="everyone@$domain";
  43. my @ignore;
  44. my @dest;
  45. open (FILEHANDLE, $DATFILE);
  46. while ( $LINE = <FILEHANDLE> ) {
  47. if ( length $LINE > 0 ) {
  48. chomp $LINE;
  49. $RETURNCODE = 0;
  50. SWITCH: {
  51. $LINE =~ /^ignore/i and do {
  52. $LINE =~ s/^ignore// && $LINE =~ s/=// && $LINE =~ s/^ //g;
  53. @ignore = (@ignore,$LINE);
  54. };
  55. $LINE =~ /^userid/i and do {
  56. # Userid found.";
  57. $LINE =~ s/^userid// && $LINE =~ s/=// && $LINE =~ s/^ //g;
  58. $userid = $LINE;
  59. };
  60. $LINE =~ /^passwd/i and do {
  61. # Passwd found.";
  62. $LINE =~ s/^passwd// && $LINE =~ s/=// && $LINE =~ s/^ //g;
  63. $passwd = $LINE;
  64. };
  65. $LINE =~ /^db/i and do {
  66. # Database found.";
  67. $LINE =~ s/^db// && $LINE =~ s/=// && $LINE =~ s/^ //g;
  68. $db = $LINE;
  69. };
  70. $LINE =~ /^host/i and do {
  71. # Database host found.";
  72. $LINE =~ s/^host// && $LINE =~ s/=// && $LINE =~ s/^ //g;
  73. $host = $LINE;
  74. };
  75. $LINE =~ /^port/i and do {
  76. # Database host found.";
  77. $LINE =~ s/^port// && $LINE =~ s/=// && $LINE =~ s/^ //g;
  78. $port = $LINE;
  79. };
  80. $LINE =~ /^target/i and do {
  81. # Database host found.";
  82. $LINE =~ s/^target// && $LINE =~ s/=// && $LINE =~ s/^ //g;
  83. $target = $LINE;
  84. };
  85. $LINE =~ /^domain/i and do {
  86. # Database host found.";
  87. $LINE =~ s/^domain// && $LINE =~ s/=// && $LINE =~ s/^ //g;
  88. $domain = $LINE;
  89. };
  90. }
  91. }
  92. }
  93. print "Connecting to database $db on $host:$port...\n\r";
  94. print "Target email address is $target...\n\r";
  95. my $connectionInfo="DBI:mysql:database=$db;$host:$port";
  96. # make connection to database
  97. $dbh = DBI->connect($connectionInfo,$userid,$passwd);
  98. # Delete the old message...prepare and execute query
  99. $query = "SELECT username FROM mailbox WHERE domain='$domain';";
  100. $sth = $dbh->prepare($query);
  101. $sth->execute();
  102. # assign fields to variables
  103. $sth->bind_columns(\$username);
  104. my $ign="false";
  105. while($sth->fetch()) {
  106. $ign = "false";
  107. foreach $ignored ( @ignore ) {
  108. if ( $username eq $ignored ){
  109. $ign = "true";
  110. }
  111. }
  112. if ( $ign eq "false" ) {
  113. @dest = (@dest,$username);
  114. }
  115. }
  116. # Delete the old aliases...prepare and execute query
  117. $query = "DELETE FROM alias WHERE address='$target';";
  118. $sth = $dbh->prepare($query);
  119. $sth->execute();
  120. print "Record deleted from the database.\r\n";
  121. $sth->finish();
  122. $goto = join(",",@dest);
  123. print "$goto\n\r\n\r";
  124. # Insert the new message...prepare and execute query
  125. $query = "INSERT INTO alias (address,goto,domain,created,modified) VALUES ('$target','$goto','$domain',now(),now());";
  126. $sth = $dbh->prepare($query);
  127. $sth->execute();
  128. print "Record added to the database.\r\n";
  129. $sth->finish();
  130. # disconnect from databse
  131. $dbh->disconnect;