-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd-zone
executable file
·119 lines (98 loc) · 4.25 KB
/
add-zone
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
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to add zones to the database
#
use strict;
use HOSTDB;
use Getopt::Long;
use vars qw ($opt_delegated $opt_default_ttl $opt_ttl $opt_serial $opt_mname $opt_rname $opt_refresh $opt_retry $opt_expiry $opt_minimum $opt_owner $opt_force $opt_debug $opt_help);
my %o = ();
my $res = GetOptions (
"delegated=s",
"default_ttl=s",
"ttl=s",
"serial=s",
"mname=s",
"rname=s",
"refresh=s",
"retry=s",
"expiry=s",
"minimum=s",
"force",
"help"
);
#die ("$0: Parsing options failed\n") if ($res);
my $debug = defined ($opt_debug);
my $zonename = shift;
my $owner = shift;
sub usage
{
my $msg = shift;
# interpolation
die(<<EOT);
${msg}Syntax: $0 [options] zonename owner
options:
--debug debug
--force don\'t perform certain sanity checks (DUP checking)
object modifiers :
--delegated Y or N
--default_ttl what to use for \$TTL - BIND parseable please
--ttl SOA TTL - BIND parseable please
--serial SOA serial - BIND parseable please
--mname SOA mname - BIND parseable please
--rname SOA rname - BIND parseable please
--refresh SOA refresh - BIND parseable please
--retry SOA retry - BIND parseable please
--expiry SOA expiry - BIND parseable please
--minimum SOA minimum - BIND parseable please
EOT
}
usage('') if ($opt_help);
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
usage ("") if (! $zonename or ! $owner);
usage ("Invalid zonename '$zonename'\n\n") unless ($hostdb->is_valid_domainname ($zonename));
if (! $opt_force) {
# do some extra sanity checks if not forced
my $z = $hostdb->findzonebyname ($zonename);
die ("$0: A zone with name '$zonename' already exists, use --force to add anyways.\n") if (defined ($z));
}
my $zone = $hostdb->create_zone ();
$zone->zonename ($zonename) or die ("$0: Invalid value - $zone->{error}\n");
$zone->owner ($owner) or die ("$0: Invalid value - $zone->{error}\n");
# defaults, set before checking if they are supplied
$zone->delegated ('N') or die ("$0: Could not set default 'delegated': $zone->{error}\n");
# if a serial is supplied, use that one later - otherwise we must set something
my ($sec, $min, $hour, $mday, $mon, $year, $yday, $isdst) = localtime ();
$year += 1900; # yes, this is Y2K safe (why do you even bother? this was written
# in the year of 2002)
$mon++;
my $today = sprintf ("%.4d%.2d%.2d", $year, $mon, $mday);
$zone->serial ("${today}01") or die ("$0: Could not set default 'serial': $zone->{error}\n") if (! defined ($opt_serial));
# optional ones
$zone->delegated ($opt_delegated) or die ("$0: Invalid value - $zone->{error}\n") if (defined ($opt_delegated));
$zone->default_ttl ($opt_default_ttl) or die ("$0: Invalid value - $zone->{error}\n") if (defined ($opt_default_ttl));
$zone->ttl ($opt_ttl) or die ("$0: Invalid value - $zone->{error}\n") if (defined ($opt_ttl));
$zone->serial ($opt_serial) or die ("$0: Invalid value - $zone->{error}\n") if (defined ($opt_serial));
$zone->mname ($opt_mname) or die ("$0: Invalid value - $zone->{error}\n") if (defined ($opt_mname));
$zone->rname ($opt_rname) or die ("$0: Invalid value - $zone->{error}\n") if (defined ($opt_rname));
$zone->refresh ($opt_refresh) or die ("$0: Invalid value - $zone->{error}\n") if (defined ($opt_refresh));
$zone->retry ($opt_retry) or die ("$0: Invalid value - $zone->{error}\n") if (defined ($opt_retry));
$zone->expiry ($opt_expiry) or die ("$0: Invalid value - $zone->{error}\n") if (defined ($opt_expiry));
$zone->minimum ($opt_minimum) or die ("$0: Invalid value - $zone->{error}\n") if (defined ($opt_minimum));
$zone->commit () or die ("$0: Could not commit zone object - $zone->{error}\n");
my @hosts_in_new_zone = $hostdb->findhostbywildcardname ("%.$zonename");
if (@hosts_in_new_zone) {
print ("You should probably move the following hosts to the new zone '$zonename' :\n\n");
printf "%-7s %-7s %-16s %s\n", "id", "partof", "ip", "hostname";
foreach my $host (@hosts_in_new_zone) {
printf "%-7s %-7s %-16s %s\n", $host->id (),
defined ($host->partof ())?$host->partof ():"-",
$host->ip (), $host->hostname ();
}
print ("\n\nThe easiest way to do that is to simply run 'housekeep-dnszone -n' (dry run) and then 'housekeep-dnszone' once.\n\n");
}
exit (0);