-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjust_apache.pl
executable file
·539 lines (500 loc) · 19.3 KB
/
adjust_apache.pl
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Carp;
use Data::Dumper;
use File::Temp qw(tempfile tempdir);
use Getopt::Long;
my $Q='"';
my $q="'";
my @idhunks = split(' ', `id`);
my @hunks = grep { /uid=/ } @idhunks;
die ("Must be root to use this")
unless( $hunks[0] eq "uid=0(root)" );
@idhunks = undef;
@hunks = undef;
my $start_time = `date +%Y%m%d-%H%M%S`;
chomp($start_time);
my $do_help = 0;
my $do_automatic = ( -t STDIN ) ? 0 : 1; # test for terminal stdin
my $debug = $ENV{'DEBUG'};
my $usage = "$0 :
Use this to update /etc/hosts and /etc/httpd/http.conf for
LANforge server operations. By default this script will backup
your /etc/hosts file to /etc/.hosts.\$date and write a new copy
to /tmp/t_hosts_\$random. It will show you the difference between
the files and prompt you to continue. When you approve it will
copy /tmp/t_hosts_\$random to /etc/hosts.
-d --debug enable debug (or use \$ set DEBUG=1)
-a --auto automatic operation mode, no prompts
-h --help this message
";
GetOptions(
"h|help" => \$do_help,
"d|debug" => \$debug,
"a|auto|automatic" => \$do_automatic,
) || die($usage);
if ($do_help) {
print $usage;
exit(0);
}
sub syslogg {
my $msg = join('\n', @_);
$msg =~ s/\r*\n/ /;
`logger -t adjust_apache "$msg"`
}
sub err {
my $msg = "[error] ".join("\n", @_);
print STDERR $msg, "\n";
syslogg($msg) if ($do_automatic);
}
sub die_err {
my $msg = "[fatal] ".join("\n", @_);
syslogg($msg) if ($do_automatic);
die($msg);
}
sub warning {
my $msg = "[warning] ".join("\n", @_);
print STDOUT $msg, "\n";
syslogg($msg) if ($do_automatic);
}
sub info {
my $msg = "[inf] ".join("\n", @_);
print STDOUT $msg, "\n";
syslogg($msg) if ($do_automatic);
}
my $MgrHostname = `cat /etc/hostname`;
chomp($MgrHostname);
if ($MgrHostname =~ /^\s*$/) {
die("System does not appear to have a hostname or /etc/hostname is misformatted.");
}
info("Will be setting hostname to $MgrHostname");
sleep 3 if ($debug);
my $config_v = "/home/lanforge/config.values";
# grab the config.values file
die_err("Unable to find $config_v" )
unless ( -f $config_v);
my @configv_lines = `cat $config_v`;
die_err("Probably too little data in config.values")
unless (@configv_lines > 5);
my %configv = ();
foreach my $line (@configv_lines) {
my ($key, $val) = $line =~ /^(\S+)\s+(.*)$/;
$configv{$key} = $val;
}
die_err("Unable to parse config.values")
unless ((keys %configv) > 5);
die_err("no mgt_dev in config.values")
unless defined $configv{'mgt_dev'};
info("LANforge config states mgt_dev $configv{'mgt_dev'}");
if ( ! -d "/sys/class/net/$configv{'mgt_dev'}") {
warning("config.values contains:\n",
join("\n", @configv_lines),
"\n");
die_err( "Unable to find [/sys/class/net/$configv{'mgt_dev'}] Please run lfconfig to set mgt_port value.");
}
my $ipline = `ip -o a show $configv{"mgt_dev"}`;
my ($ip) = $ipline =~ / inet ([0-9.]+)(\/\d+)? /g;
die_err("No ip found for mgt_dev; your config.values file is out of date: $!")
unless ((defined $ip) && ($ip ne ""));
print "ip: $ip\n" if ($debug);
# This must be kept in sync with similar code in lf_kinstall.
my $found_localhost = 0;
my $fname = "/etc/hosts";
my $backup = "/etc/.hosts.$start_time";
`cp $fname $backup`;
die_err("Unable to create backup of /etc/hosts at $backup") if ( ! -f $backup );
my ($fh, $editfile) = tempfile( "t_hosts_XXXX", DIR=>'/tmp', SUFFIX=>'.txt');
if (-f "$fname") {
my @lines = `cat $fname`;
#open(FILE, ">$fname") or die "Couldn't open file: $fname for writing: $!\n\n";
my $foundit = 0;
my $i;
# chomp is way to simplistic if we need to weed out \r\n characters as well
#chomp(@lines);
for (my $i = 0; $i < @lines; $i++) {
($lines[$i]) = $lines[$i] =~ /^([^\r\n]+)\r?\n$/;
}
# we want to consolidate the $ip $hostname entry for MgrHostname
my @newlines = ();
my %more_hostnames = ();
my $new_entry = "$ip ";
#my $blank = 0;
#my $was_blank = 0;
my $counter = 0;
if ((exists $ENV{"DEBUG"}) && ($ENV{"DEBUG"} eq "1")) {
$debug = 1;
}
my %host_map = (
"localhost.localdomain" => "127.0.0.1",
"localhost" => "127.0.0.1",
"localhost4.localdomain4" => "127.0.0.1",
"localhost4" => "127.0.0.1",
"localhost.localdomain" => "::1",
"localhost" => "::1",
"localhost6.loaldomain6" => "::1",
"localhost6" => "::1",
$MgrHostname => $ip,
"lanforge.localnet" => "192.168.1.101",
"lanforge.localdomain" => "192.168.1.101",
);
my %comment_map = ();
my %address_marker_map = ();
my %address_map = (
"127.0.0.1" => "localhost.localdomain localhost localhost4.localdomain4 localhost4",
"::1" => "localhost.localdomain localhost localhost6.loaldomain6 localhost6",
$ip => $MgrHostname,
"192.168.1.101" => "lanforge.localnet lanforge.localdomain",
);
if ($debug) {
print Dumper(\%address_map);
print Dumper(\%host_map);
}
my $prevname = "";
my $previp = "";
for my $ln (@lines) {
next if (!(defined $ln));
# print "\nLN[$ln]\n" if ($debug);
next if ($ln =~ /^\s*$/);
next if ($ln =~ /^\s*#/);
next if ($ln =~ /LF-HOSTAME-NEXT/); # old typo
next if ($ln =~ /LF-HOSTNAME-NEXT/);
my $comment = undef;
# print "PARSING IPv4 ln[$ln]\n" if ($debug);
if ($ln =~ /#/) {
($comment) = $ln =~ /^[^#]+(#.*)$/;
($ln) = $ln =~ /^([^#]+)\s*#/;
print "line with comment becomes [$ln]\n" if ($debug);
}
@hunks = split(/\s+/, $ln);
my $middleip = 0;
my $counter2 = -1;
my $linehasip = 0;
my $lfhostname = 0;
if ((defined $comment) && ($comment ne "")) {
$comment_map{$hunks[0]} = $comment;
}
for my $hunk (@hunks) {
# print "\n HUNK",$counter2,"-:$hunk:- " if ($debug);
$counter2++;
next if ($hunk =~ /^localhost/);
next if ($hunk =~ /^lanforge-srv$/);
next if ($hunk =~ /^lanforge\.local(domain|net)$/);
next if ($hunk =~ /^extra6?-\d+/);
if ($hunk =~ /^\s*$/) {
next;
}
if ($hunk =~ /^$ip$/) {
$linehasip++;
$lfhostname++;
}
elsif ($hunk =~ /^$MgrHostname$/) {
$lfhostname++;
$prevname = $hunk;
}
$previp = "" if (!defined($previp));
if (($hunk =~ /^127\.0\.0\.1/)
|| ($hunk =~ /^192\.168\.1\.101/)
|| ($hunk =~ /^::1$/)) {
$previp = $hunk;
$linehasip++;
}
elsif ($hunk =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
$linehasip++;
# print " IP4($hunk)" if ($debug);
if ($counter2 > 0) { # we're not first item on line
$middleip++ if ($counter2 > 0);
# print "middle" if ($debug);
}
if (!(defined $address_map{$hunk})) {
$address_map{$hunk} = "";
}
# print "+IP4" if ($debug);
if (("" ne $prevname) && ($counter2 > 0)) {
# print " hunk($hunk)prev($prevname)" if ($debug);
$address_map{$hunk} .= " $prevname"
if ($address_map{$hunk} !~ /\s*$prevname\s*/);
# $host_map{$prevname} .= " $hunk";
if ($host_map{$prevname} !~ /\b$hunk\b/) {
$host_map{$prevname} .= " $hunk";
}
}
$previp = $hunk;
}
elsif (($hunk =~ /[G-Zg-z]+\.?/) || ($hunk =~ /^[^:A-Fa-f0-9]+/)) {
# print " notIP($hunk)" if ($debug);
$prevname = $hunk;
if ($middleip) {
# print " middle($previp)" if ($debug);
$address_map{$previp} .= " $hunk"
if ($address_map{$previp} !~ /\b$hunk\b/);
$prevname = $hunk;
if ($host_map{$prevname} !~ /\b$hunk\b/) {
$host_map{$prevname} .= " $previp";
}
}
elsif ($linehasip) {
# print " prev($previp) hunk($hunk)" if ($debug);
$address_map{$previp} .= " $hunk"
if ($address_map{$previp} !~ /\s*$hunk\s*/);
if ((defined $prevname) && (exists $host_map{$prevname}) && ($host_map{$prevname} !~ /\b$hunk\b/)) {
$host_map{$hunk} .= " $previp";
}
}
elsif ($lfhostname) {
$more_hostnames{$hunk} = 1;
if ($host_map{$prevname} !~ /\b$hunk\b/) {
$host_map{$hunk} .= " $previp";
}
}
else { # strange word
if ("" eq $previp) {
print " hunk($hunk) has no IP***" if ($debug);
$more_hostnames{$hunk} = 1;
}
elsif ($address_map{$previp} !~ /\s*$hunk\s*/) {
$address_map{$previp} .= " $hunk";
if ($host_map{$prevname} !~ /\b$hunk\b/) {
$host_map{$hunk} .= " $previp";
}
}
}
}
elsif (($hunk =~ /::/)
|| ($hunk =~ /[0-9A-Fa-f]+:/)) {
# print " hunk6($hunk)" if ($debug);
$linehasip++;
if (!(defined $address_map{$hunk})) {
$address_map{$hunk} = "";
}
$previp = $hunk;
}
elsif ($address_map{$previp} !~ /\s*$hunk\s*/) {
# is hostname and not an ip
$address_map{$previp} .= " $hunk";
if ($host_map{$prevname} !~ /\b$hunk\b/) {
$host_map{$hunk} .= " $previp";
}
}
} # ~foreach hunk
} # ~foreach line
if (($host_map{$MgrHostname} !~ /^\s*$/) && ($host_map{$MgrHostname} =~ /\S+\s+\S+/)) {
print("Multiple IPs for this hostname: " . $host_map{$MgrHostname} . "\n") if ($debug);
my @iphunks = split(/\s+/, $host_map{$MgrHostname});
print "Changing $MgrHostname to $ip; hostmap: <<$host_map{$MgrHostname}>> addrmap: <<$address_map{$ip}>>\n"
if ($debug);
$host_map{$MgrHostname} = $ip;
}
for my $name (sort keys %more_hostnames) {
$address_map{$ip} .= " $name";
print "updated address_map entry: $ip -> $address_map{$ip}\n" if ($debug);
}
# this might be premature
unshift(@newlines, "192.168.1.101 " . $address_map{"192.168.1.101"});
unshift(@newlines, "127.0.0.1 " . $address_map{"127.0.0.1"});
unshift(@newlines, "::1 " . $address_map{"::1"});
my %used_addresses = ();
delete($address_map{"192.168.1.101"});
$used_addresses{"192.168.1.101"} = 1;
delete($address_map{"127.0.0.1"});
$used_addresses{"127.0.0.1"} = 1;
delete($address_map{"::1"});
$used_addresses{"::1"} = 1;
if ($debug) {
print "# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n";
print "\nAddress map\n";
print Dumper(\%address_map);
print "\nHost map\n";
print Dumper(\%host_map);
print "# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n";
sleep 2;
}
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
# we want to maintain the original line ordering as faithfully as possible
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
for my $ln (@lines) {
$ln = "" if (!(defined $ln));
print "old[$ln]\n" if ($debug);
# if we are comments or blank lines, preserve them
next if ($ln =~ /LF-HOSTNAME-NEXT/);
next if ($ln =~ /\b$MgrHostname\b/); # skip our mgt hostname
next if ($ln =~ /^$host_map{$MgrHostname}\s+/); # line starts with present IP addr
if (($ln =~ /^\s*$/) || ($ln =~ /^\s*#/)) {
push(@newlines, $ln);
next;
}
@hunks = split(/\s+/, $ln);
if (exists $address_map{$hunks[0]}) {
if ((exists $address_marker_map{$hunks[0]})
|| (exists $used_addresses{$hunks[0]})) {
print "already printed $hunks[0]\n" if ($debug);
next;
}
my $comment = "";
if (exists $comment_map{$hunks[0]}) {
$comment = " $comment_map{$hunks[0]}";
}
push(@newlines, "$hunks[0] $address_map{$hunks[0]}$comment");
$address_marker_map{$hunks[0]} = 1;
next;
}
if (!(exists $used_addresses{$hunks[0]})) {
warning("untracked IP <<$hunks[0]>> Used addresses:");
print Dumper(\%address_marker_map) if ($debug);
print Dumper(\%used_addresses) if ($debug);
}
}
push(@newlines, "###-LF-HOSTNAME-NEXT-###");
push(@newlines, $ip . " " . $address_map{$ip});
if ($debug) {
print "# ----- new /etc/hosts ----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n";
for my $ln (@newlines) {
print "$ln\n";
}
print "# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n";
sleep 5;
}
# write to /tmp/t_hosts_$random
for my $ln (@newlines) {
print $fh "$ln\n";
}
close $fh;
my $wc_edit_file = `wc -l < $editfile`;
chomp($wc_edit_file);
my $wc_orig_file = `wc -l < $backup`;
if ($wc_edit_file == 0) {
die_err("Abandoning $editfile, it was blank.");
exit(1);
}
my $there_are_diffs = `/bin/diff /etc/hosts $editfile > /dev/null && echo 0 || echo 1`;
chomp($there_are_diffs);
$there_are_diffs = int($there_are_diffs);
if (! $there_are_diffs) {
info("No difference in hosts file.");
sleep(1) if (!$do_automatic);
}
elsif (!$do_automatic) {
my $msg = "Original /etc/hosts file backed up to $backup\n"
. "The hosts file differs by " . ($wc_orig_file - $wc_edit_file) . "lines, at: $editfile\n"
. "Displaying difference...\n";
info($msg);
sleep(2);
my $diffcmd = "diff -y /etc/hosts $editfile";
if ( -x "/usr/bin/colordiff" ) {
$diffcmd = "colordiff -y /etc/hosts $editfile";
}
open(my $diff_in, "-|", $diffcmd);
my ($diff_out, $diff_file) = tempfile( "diff_hosts_XXXX", DIR=>"/tmp" );
my @diff_lines = <$diff_in>;
close($diff_in);
print $diff_out join("", @diff_lines);
close($diff_out);
system("/bin/less -Nr $diff_file");
print "/bin/less -dNr $diff_file\n" if ($debug);
# prompt to exit
print "Press Enter to continue, [ctrl-c] to quit >";
my $i = <STDIN>;
}
if ($there_are_diffs) {
warning("Line comparison: $backup\: $wc_orig_file, $editfile\: $wc_edit_file");
warning("Installing new hosts file from $editfile, backup at $backup");
system("cp $editfile /etc/hosts");
}
} # ~if found hosts file
my $local_crt ="";
my $local_key ="";
my $hostname_crt ="";
my $hostname_key ="";
# check for hostname shaped cert files
if (-f "/etc/pki/tls/certs/localhost.crt") {
$local_crt = "/etc/pki/tls/certs/localhost.crt";
}
if (-f "/etc/pki/tls/private/localhost.key") {
$local_key = "/etc/pki/tls/private/localhost.key";
}
if (-f "/etc/pki/tls/certs/$MgrHostname.crt") {
$hostname_crt = "/etc/pki/tls/certs/$MgrHostname.crt";
}
if (-f "/etc/pki/tls/private/$MgrHostname.key") {
$hostname_key = "/etc/pki/tls/private/$MgrHostname.key";
}
# grab the 0000-default.conf file
my @places_to_check = (
"/etc/apache2/apache2.conf",
"/etc/apache2/ports.conf",
"/etc/apache2/sites-available/000-default.conf",
"/etc/apache2/sites-available/0000-default.conf",
"/etc/httpd/conf/http.conf",
"/etc/httpd/conf/httpd.conf",
"/etc/httpd/conf.d/ssl.conf",
"/etc/httpd/conf.d/00-ServerName.conf",
);
foreach my $file (@places_to_check) {
if (-f $file) {
print "Checking $file...\n";
my @lines = `cat $file`;
chomp @lines;
# we want to match Listen 80$ or Listen 443 https$
# we want to replace with Listen lanforge-mgr:80$ or Listen lanforge-mgr:443 https$
@hunks = grep {/^\s*(Listen|SSLCertificate)/} @lines;
#print Dumper(["hunks", \@hunks]);
if (@hunks) {
my $edited = 0;
my @newlines = ();
print "[$file] has something to change...\n";
for my $ln (@hunks) {
print " > $ln\n";
}
@hunks = (@hunks, "\n");
foreach my $confline (@lines) {
my $has_listen = 0;
my $old_confline = $confline;
if ($confline =~ /^\s*Listen/) {
$has_listen++;
}
if ($confline =~ /^\s*Listen\s+(?:80|443)\b/) {
$confline =~ s/Listen\s+/Listen ${MgrHostname}:/;
print " [$file] new line: $confline\n";
}
elsif ($confline =~ /^\s*Listen\s+:\d+\b/) {
$confline =~ s/Listen\s+:/Listen ${MgrHostname}:/;
print " [$file] new line: $confline\n";
}
elsif ($confline =~ /^\s*Listen\s+(?:[^:]+:(80|443))\b/) {
$confline =~ s/Listen\s+[^:]+:/Listen ${MgrHostname}:/;
print " [$file] new line: $confline\n";
}
if ($confline =~ /^\s*SSLCertificateFile\b/) {
$confline = "SSLCertificateFile $hostname_crt" if ("" ne $hostname_crt);
print " [$file] new line: $confline\n";
}
if ($confline =~ /^\s*SSLCertificateKeyFile\b/) {
$confline = "SSLCertificateKeyFile $hostname_key" if ("" ne $hostname_key);
print " [$file] new line: $confline\n";
}
if (($has_listen > 0) && ($old_confline eq $confline)) {
print " [$file] Listen line unchanged: [$confline]\n";
}
push @newlines, $confline;
$edited++ if ($confline =~ /# modified by lanforge/);
}
push(@newlines, "# modified by lanforge\n") if ($edited == 0);
my $fh;
die($!) unless open($fh, ">", $file);
print $fh join("\n", @newlines);
close $fh;
}
else {
print "Nothing looking like [Listen 80|443] in $file\n";
}
}
} # ~for places_to_check
if (-d "/etc/httpd/conf.d") {
die($!) unless open(FILE, ">", "/etc/httpd/conf.d/00-ServerName.conf");
print FILE "ServerName $MgrHostname\n";
#print FILE "Listen $MgrHostname:80\n";
#print FILE "Listen $MgrHostname:443\n";
close FILE;
}
#