Handle hostnames with upper-case letters
[webmin.git] / servers / auto.pl
1 #!/usr/local/bin/perl
2 # Automatically discover new servers
3
4 $no_acl_check++;
5 use strict;
6 use warnings;
7 require './servers-lib.pl';
8 our (%config, %text, @cluster_modules);
9 &foreign_require("mailboxes", "mailboxes-lib.pl");
10 &foreign_require("net", "net-lib.pl");
11
12 my $debug;
13 if ($ARGV[0] eq "--debug" || $ARGV[0] eq "-debug") {
14         $debug = 1;
15         }
16
17 my $nets;
18 if (!$config{'auto_net'}) {
19         $nets = &get_my_address();
20         }
21 elsif (&check_ipaddress($config{'auto_net'})) {
22         $nets = $config{'auto_net'};
23         }
24 else {
25         my ($iface) = grep { $_->{'fullname'} eq $config{'auto_net'} }
26                            &net::active_interfaces();
27         $iface && $iface->{'address'} || die $text{'find_eiface'};
28         $nets = $iface->{'address'};
29         }
30 my @broad;
31 foreach my $net (split(/\s+/, $nets)) {
32         $net =~ s/\.\d+$/\.0/;
33         $net =~ /^(\d+\.\d+\.\d+)\.0$/ || die $text{'find_escan'};
34         for(my $i=0; $i<256; $i++) {
35                 push(@broad, "$1.$i");
36                 }
37         }
38 my $limit = $config{'scan_time'};
39 my @cluster = grep { $config{'auto_'.$_} } @cluster_modules;
40 if ($debug) {
41         print "Checking on ",join(" ", @broad),"\n";
42         print "User = $config{'auto_user'}\n";
43         print "Pass = $config{'auto_pass'}\n";
44         }
45 my ($found, $already, $foundme, $addmods) = 
46         &find_servers(\@broad, $config{'scan_time'}, !$debug,
47               $config{'auto_user'},
48               $config{'auto_pass'}, $config{'auto_type'}, \@cluster,
49               $config{'auto_self'});
50 if ($debug) {
51         foreach my $f (@$found) {
52                 my $added = $addmods->{$f->{'id'}};
53                 print "On $f->{'host'} added $added->[0]->{'host'} ",
54                       ($added->[1] ? "OK" : "FAILED")," ",
55                       $added->[2],"\n";
56                 }
57         }
58
59 # Send an email for each new system found
60 my @servers = &list_servers();
61 if ($config{'auto_email'}) {
62         foreach my $f (@$found) {
63                 &send_auto_email(&text('email_regsubject', $f->{'host'}),
64                                  &text('email_reg', $f->{'host'}));
65                 }
66         }
67
68 # See if there were any systems that are registered and on the same net, but
69 # were not found 3 times in a row.
70 if ($config{'auto_remove'}) {
71         my @net = split(/\./, $nets);
72         foreach my $s (@servers) {
73                 my $ip = &to_ipaddress($s->{'host'});
74                 my @ip = split(/\./, $ip);
75                 if ($ip[0] == $net[0] && $ip[1] == $net[1] &&
76                     $ip[2] == $net[2]) {
77                         # On scanned net, so should have been found
78                         my ($f) = grep { &to_ipaddress($_->{'host'}) eq $ip }
79                                 (@$found, @$already);
80                         if (!$f && $s->{'notfound'}++ >= 3) {
81                                 # Not found too many times Delete it ..
82                                 &delete_server($s->{'id'});
83                                 if ($config{'auto_email'}) {
84                                         &send_auto_email(
85                                                 &text('email_unregsubject',
86                                                       $f->{'host'}),
87                                                 &text('email_unreg',
88                                                       $f->{'host'}));
89                                         }
90                                 }
91                         else {
92                                 # Found, or only not found once
93                                 if ($f) {
94                                         $s->{'notfound'} = 0;
95                                         }
96                                 &save_server($s);
97                                 }
98                         }
99                 }
100         }
101
102 sub send_auto_email
103 {
104 my ($subject, $body) = @_;
105 &mailboxes::send_text_mail(&mailboxes::get_from_address(),
106                            $config{'auto_email'},
107                            undef,
108                            $subject,
109                            $body,
110                            $config{'auto_smtp'});
111 }
112