multinsupd.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/perl
  2. #####################################################
  3. # multinsupd.pl
  4. #
  5. # This script can be used as the value for the
  6. # "nsupdate" parameter in gnudip.conf or minidip.conf
  7. # to expand single nsupdate delete and add commands
  8. # for particular domains into multiple commands,
  9. # including the original.
  10. #
  11. # The actual nsupdate command should be passed as the
  12. # arguments to this script. So the name of this script
  13. # just gets inserted in front of the nsupdate command.
  14. #
  15. # It reads the names of domains to be expanded, and
  16. # the list of additional domains to insert, from the
  17. # etc/multinsupd.conf, or from a file name passed as
  18. # an argument.
  19. #
  20. # Note that the aliases must be in zones with the same
  21. # TSIG key as the zone of the domain being expanded,
  22. # since the updates are done in the same execution of
  23. # nsupdate
  24. #
  25. #####################################################
  26. # Perl modules
  27. use strict;
  28. use Getopt::Std;
  29. # global variables
  30. use vars qw($logger);
  31. # locate ourselves
  32. my $gnudipdir;
  33. use FindBin;
  34. BEGIN {
  35. $gnudipdir = '';
  36. if ($FindBin::Bin =~ /(.*)\/.+?/) {
  37. $gnudipdir = $1;
  38. }
  39. }
  40. use lib "$gnudipdir/lib";
  41. # GnuDIP common subroutines
  42. use gdipdaemon;
  43. use gdiplib;
  44. # process command line
  45. sub usage {
  46. print STDERR <<"EOQ";
  47. usage: multinsupd.pl { -h | [ -c conffile ] nsupdate_command }
  48. usage: Expand nsupdate delete and add commands for special domain
  49. usage: names. Read configuration from "conffile" if given, otherwise
  50. usage: from multinsupd.conf in GnuDIP "etc".
  51. usage: -h: Print this usage message.
  52. EOQ
  53. }
  54. use vars qw/ $opt_h $opt_c /;
  55. if (!getopts('hlc:')) {
  56. usage();
  57. exit 1;
  58. }
  59. if ($opt_h) {
  60. usage();
  61. exit;
  62. }
  63. if (@ARGV eq 0) {
  64. usage();
  65. exit 1;
  66. }
  67. # the real nsupdate command
  68. my $nsupdate = join(' ', @ARGV);
  69. # get preferences from config file
  70. $opt_c = "$gnudipdir/etc/multinsupd.conf" if !$opt_c;
  71. my $conf = getconf($opt_c);
  72. if (!$conf) {
  73. print STDERR "multinsupd.pl has exited - getconf returned nothing\n";
  74. exit 1;
  75. }
  76. # logger command
  77. $logger = $$conf{'logger'};
  78. if (!$logger) {
  79. print STDERR "multinsupd.pl has exited - configuration parameter \"logger\" not defined";
  80. exit 1;
  81. }
  82. # read and process each line
  83. my %expands;
  84. my @lines;
  85. while (<STDIN>) {
  86. chomp(my $line = $_);
  87. push @lines, ($line);
  88. my @token = split(' ', $line);
  89. next if !defined $token[0] or $token[0] ne 'update';
  90. my $domain = validdomain($token[2]);
  91. $domain = substr($domain, 0, -1);
  92. my $aliases;
  93. $aliases = $$conf{"alias.$domain."};
  94. $aliases = $$conf{"alias.$domain"} if !defined($aliases);
  95. $aliases = $$conf{"$domain."} if !defined($aliases);
  96. $aliases = $$conf{"$domain"} if !defined($aliases);
  97. next if !defined($aliases);
  98. my @domains = split(' ', $aliases);
  99. if ($token[1] eq 'delete' and $token[3] eq 'A') {
  100. foreach my $alias (@domains) {
  101. push @lines, ("update delete $alias A");
  102. }
  103. $expands{$domain} = $aliases;
  104. } elsif ($token[1] eq 'add' and $token[4] eq 'A') {
  105. my $TTL = $token[3];
  106. my $IP = $token[5];
  107. foreach my $alias (@domains) {
  108. push @lines, ("update add $alias $TTL A $IP");
  109. }
  110. $expands{$domain} = $aliases;
  111. }
  112. }
  113. # show some indication that we did something
  114. foreach my $expand (keys %expands) {
  115. writelog("Inserted aliases for $expand: $expands{$expand}");
  116. }
  117. # for debugging
  118. #debug_logger("nsupdate: $nsupdate");
  119. #foreach my $line (@lines) {
  120. # debug_logger("$line");
  121. #}
  122. # call nsupdate
  123. my $retc = callcommand('', \&writelog, $nsupdate, @lines);
  124. exit if $retc;
  125. print STDERR "multinsupd.pl has exited - callcommand failed: $nsupdate\n";
  126. exit 1;
  127. # for debugging
  128. #sub debug_logger {
  129. # my $line = shift;
  130. # system('/usr/bin/logger', '-t', 'multinsupd.pl', $line);
  131. #}