-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodhosts
executable file
·273 lines (225 loc) · 7.48 KB
/
modhosts
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/perl
#
# modhosts - utility to modify (move,delete) host records
#
# Copyright (c) Michal Kostenec <[email protected]> 2013-2014.
# Copyright (c) Timo Kokkonen <[email protected]> 2001-2003.
#
require 5;
use Getopt::Long;
use Net::Netmask;
use Sauron::DB;
use Sauron::Util;
use Sauron::BackEnd;
use Sauron::Sauron;
use Net::IP qw(:PROC);
load_config();
##############################################
$user = (getpwuid($<))[0];
GetOptions("help|h","cidr=s","name=s","move=s","delete",
"info=s","rename=s","commit","excludeip=s",
"excludename=s","type=s","ether=s","etherempty",
"setedate=s","verbose","setgroup=s");
if ($opt_help || @ARGV < 2) {
print "syntax: $0 <server> <zone> [options]\n\n",
"options:\n",
"\t--cidr=<cidr>\t\tselect hosts within CIDR block\n",
"\t--name=<regexp>\t\tselect hostnames matching the regexp\n",
"\t--info=<regexp>\t\tselect host with matching info\n\n",
"\t--type=<type>\t\tselect only hosts of this type\n",
"\t--ether=<regexp>\t\tselect hosts with matching MAC\n",
"\t--etherempty\t\tselect hosts without a MAC\n",
"\t--delete\t\tdelete matching hosts\n",
"\t--move=<CIDR>,<IP>\tmove hosts to given net starting from IP\n",
"\t--rename=<regexp>\trename hosts with given substitution 'regexp'\n",
"\t--setedate=<days>\tset host expiration date to today+days\n",
"\t--setgroup=<name>\tassign host to a group\n",
"\n",
"\t--commit\t\tcommit changes (w/o this NO changes are made)\n",
"\n";
print "" if ($opt_help);
exit(0);
}
db_connect();
set_muser($user);
$server=$ARGV[0];
$zone=$ARGV[1];
$opt_verbose=($opt_verbose ? 1 : 0);
$opt_etherempty=($opt_etherempty ? 1 : 0);
$serverid=get_server_id($server);
fatal("cannot find server '$server'") unless ($serverid > 0);
$zoneid=get_zone_id($zone,$serverid);
fatal("cannot find zone '$zone'") unless ($zoneid > 0);
fatal("no host selection criteria(s) specified")
unless ($opt_cidr || $opt_name || $opt_info || $opt_ether);
if ($opt_cidr || $opt_move) {
$cidr_t=", a_entries a ";
$cidr_f=", a.ip,a.id ";
$cidr_r=" AND a.host=h.id ";
}
if ($opt_cidr) {
fatal("invalid CIDR: $cidr") unless (is_cidr($opt_cidr));
if (new Net::IP($opt_cidr)->size() == 1) {
$cidr .= " AND a.ip = '$opt_cidr' ";
} else {
$cidr.= " AND a.ip << '$opt_cidr' ";
}
print "CIDR criteria: $opt_cidr\n";
}
if ($opt_ether) {
print "ETHER: criteria: $opt_ether\n";
$ether= " AND h.ether ~ '$opt_ether' ";
}
if ($opt_etherempty) {
$ether= " AND (h.ether = '' OR h.ether IS NULL) ";
}
if ($opt_setedate) {
fatal("invalid arguments for setedate option: $opt_setedate")
unless ($opt_setedate =~ /^\d+$/);
$new_edate = time() + int($opt_setedate * 86400);
print "SET expiration date to: ".localtime($new_edate)." ($new_edate)\n";
}
if ($opt_setgroup) {
if($opt_setgroup eq "NONE") {
$gid = -1;
}
else {
fatal("cannot find group '$opt_setgroup'") if (($gid=get_group_by_name($serverid,$opt_setgroup)) < 0);
fatal("cannot get group type for '$opt_setgroup'") if (($gtype = get_group_type_by_name($serverid,$opt_setgroup)) < 0);
fatal("cannot assign group with type 'DHCP Class' ($opt_setgroup)") if $gtype == 3;
}
}
if ($opt_type) {
$opt_type=lc($opt_type);
if ($opt_type eq 'host') { $type=1; }
elsif ($opt_type eq 'cname') { $type=4; }
elsif ($opt_type eq 'arec') { $type=7; }
elsif ($opt_type eq 'srv') { $type=8; }
elsif ($opt_type eq 'sshfp') { $type=11; }
else {
fatal("unknown type for option --type");
}
$type = " AND h.type = $type ";
}
if ($opt_name) {
$name=" AND h.domain ~* '$opt_name' ";
print "Hostname regexp: $opt_name\n";
}
if ($opt_info) {
$info=" AND (h.info ~* " . db_encode_str($opt_info) . " OR h.huser ~* " . db_encode_str($opt_info) .
" OR h.dept ~* " . db_encode_str($opt_info) . " OR h.location ~* " . db_encode_str($opt_info) . ") ";
print "Host info regexp: $opt_info\n";
}
if ($opt_move) {
fatal("invalid parameters to option --move")
unless ($opt_move =~ /^(\S+),(\S+)$/);
$move_net=$1;
$move_ip=$2;
$m_net = new Net::IP($move_net) or print "nelze net\n";
$m_ip = new Net::IP($move_ip) or print "nelze ip\n";
fatal("invalid CIDR parameter to option move: $move_net")
unless $m_net;
fatal("invalid IP parameter to option move: $move_ip")
unless $m_ip;
print "Move matching hosts to net $move_net starting from $move_ip\n";
fatal("invalid parameters to option move") if ($m_net->overlaps($m_ip) == $IP_NO_OVERLAP or $m_net->overlaps($m_ip) == undef);
}
if ($opt_rename) {
fatal("invalid substituion regexp for parameter rename: $opt_rename")
unless ($opt_rename =~ /^s\/(.*)\/(.*)\/$/);
$rename1=$1; $rename2=$2;
print "Rename rule: s/$rename1/$rename2/\n";
}
if ($opt_excludeip) {
@exclude_ips = split(/,/,$opt_excludeip);
# $ecount=@exclude_ips;
foreach $ip (@exclude_ips) {
print "Excluded IP: $ip\n";
fatal("Invalid exclude IP sepcified: $ip") unless (is_cidr($ip));
}
}
$sql="SELECT h.id,h.domain,h.type,h.ether $cidr_f FROM hosts h $cidr_t " .
"WHERE h.zone=$zoneid $cidr_r $cidr $name $ether $info $type " .
"ORDER BY h.domain;";
print "$sql\n" if ($opt_verbose);
db_query($sql,\@q);
print db_errormsg() . "\n" if (db_errormsg());
$count=@q;
print "Found $count hosts(s) matching the criteria.\n";
exit unless ($count > 0);
if ($opt_delete || $opt_rename || $opt_move || $opt_setgroup ||
$opt_setedate) {
if ($opt_commit) {
print "Are you sure you want to apply changes to database? [y/N]? ";
chomp ($answer = <STDIN>);
exit unless ($answer eq 'y' || $answer eq 'Y');
}
}
db_begin();
db_ignore_begin_and_commit(1);
for $i (0..$#q) {
printf "%-6d %-30s %2d %12s %s\n", $q[$i][0],$q[$i][1],$q[$i][2],
$q[$i][3],$q[$i][4];
if ($opt_excludeip) {
$ip_skip=0;
for $tmpip (@exclude_ips) {
if ($tmpip eq $q[$i][4]) {
print "Skipping excluded IP: $tmpip\n";
$ip_skip=1;
}
}
next if ($ip_skip);
}
$id=$q[$i][0];
$domain=$q[$i][1];
if ($opt_rename) {
$domain =~ s/$rename1/$rename2/;
print "\trename: $q[$i][1] --> $domain\n";
}
if ($opt_move) {
$m_net += ($m_ip->intip() - $m_net->intip());
$new_ip = ip_compress_address($m_net->ip(), $m_net->version());
while (ip_in_use($serverid,$new_ip)) {
#print "\tSKIP ip: $new_ip\n";
$m_net++;
$new_ip = ip_compress_address($m_net->ip(), $m_net->version());
}
fatal("cannot find new ip!") if (ip_in_use($serverid,$new_ip));
print "\tnew ip: $new_ip\n";
}
undef %host;
fatal("cannot get host record (id=$id)") if (get_host($id,\%host));
if ($opt_delete) {
print "\tDelete: $q[$i][1]\n";
fatal("cannot delete host: $q[$i][1]") if (delete_host($id));
}
elsif ($opt_rename || $opt_move || $opt_setedate || $opt_setgroup) {
$host{domain}=$domain;
if ($opt_move) {
for $j (1..$#{$host{ip}}) {
#print "IP: $host{ip}[$j][0] $host{ip}[$j][1]\n";
if ($host{ip}[$j][0] eq $q[$i][5]) {
print "\tIP MATCH: $host{ip}[$j][0] $host{ip}[$j][1]\n";
$host{ip}[$j][1]=$new_ip;
$host{ip}[$j][4]=1;
}
}
}
$host{expiration}=$new_edate if ($opt_setedate);
if ($opt_setgroup) {
print "Changing group $host{grp} --> $gid\n";
$host{grp}=$gid;
}
print "Updating $q[$i][1]...\n";
fatal("cannot update host: $q[$i][1]\n".db_errormsg()."\n")
if (update_host(\%host));
}
}
db_ignore_begin_and_commit(0);
if ($opt_commit) {
fatal("cannot commit changes to database") if (db_commit() < 0);
} else {
print "NO CHANGES MADE!\n";
}
exit;
# eof :-)