This file is indexed.

/usr/sbin/fai-monitor is in fai-server 5.0.3ubuntu1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl -w
# vim:et:ts=2:sw=2:

#*********************************************************************
#
# fai-monitor -- monitor daemon which collects client status info
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2003-2015 by Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
#
#*********************************************************************

use strict;
use Socket;
use Getopt::Std;

$| = 1;
my ($port, $timeout, $daemon, $timestamp);
my $pidfile = '/var/run/fai-monitor.pid';
my $logfile = '-';
my $daemonlogfile = '/var/log/fai-monitor.log';
my $useip;

our %variables;

our ($opt_b,$opt_h,$opt_p,$opt_l,$opt_t,$opt_d,$opt_P,$opt_T,$opt_i);
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub logline(@) {
  open(LOGFILE, $logfile) or return 0;
  print LOGFILE (scalar localtime(), ' - ') if ($timestamp);
  print LOGFILE @_ or return 0;
  close(LOGFILE);
  return 1;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub signal_die(@) {
  logline(@_);
  unlink($pidfile);
  exit(1);
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub signal_warn(@) {
  logline(@_) or die "log: $!";
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub signal_deadly(@) {
  # Use the die-handler
  signal_die('Caught deadly signal ' . shift() . "\n");
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub server_init() {
  logline("FAI monitoring daemon starting..\n") or die "log: $!";

  # Init signals
  $SIG{INT} = \&signal_deadly;
  $SIG{QUIT} = \&signal_deadly;
  $SIG{TERM} = \&signal_deadly;
  $SIG{__DIE__} = \&signal_die;
  $SIG{__WARN__} = \&signal_warn;
  # HUP is usually used to reopen log files. This is not a problem
  # in this design.
  $SIG{HUP} = 'IGNORE';

  if ($daemon) {
    if (-e $pidfile) {
    # Pid file already exists. Check if it's a valid pid.
      open(PIDFILE, '<', "$pidfile") or die "open $pidfile: $!";
      my $pid = <PIDFILE>;
      chomp($pid);
      if ($pid ne '') {
      # Kill -0 exits with value 0 if pid is alive
        system("kill -0 $pid 2> /dev/null");
        if ($? == 0) {
          logline("Pidfile $pidfile exists and contains an existing pid. Exiting.\n");
          exit(1);
        }
      }
      close(PIDFILE);
    }
    eval "Proc::Daemon::Init";
    umask 022;

    open(PIDFILE, '>', "$pidfile") or die "open $pidfile: $!";
    print PIDFILE $$ or die "print $pidfile: $!";
    close(PIDFILE);
  }

  # Listen
  my $proto = getprotobyname('tcp');
  socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
  setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsockopt: $!";

  my $paddr = sockaddr_in($port, INADDR_ANY);

  bind(SERVER, $paddr) or die "bind: $!";
  listen(SERVER, SOMAXCONN) or die "listen: $!";
  logline("FAI monitoring daemon started on port $port with pid $$\n") or die "log: $!";
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub big_loop() {

  # accept a connection, print message received and close
  my ($client_addr);
  while ($client_addr = accept(CLIENT, SERVER)) {
    my ($port, $iaddr) = sockaddr_in($client_addr);
    my $ip = inet_ntoa($iaddr);

    my $inp = '';

    eval {
      local $SIG{__DIE__};
      local $SIG{__WARN__};
      local $SIG{'ALRM'} = sub { die("Timeout"); };

      alarm($timeout);
      $inp = <CLIENT>;
      alarm(0);
      defined($inp) && $inp =~ /^VARIABLE / && reply_to_client($inp)
    };

    close CLIENT;

    if (!defined($inp) || $inp eq '') {
      # Client did not send anything, or alarm went off
      logline("$ip:$port: No data or timeout.\n") or die "log: $!";
      next;
    }

    if ($inp =~ /^([^\s;]+)\s+TASKEND install 0/ && $opt_b) {
      my $cname = $1;
      if ($useip) {
        $cname = $ip;
      }
      system('fai-chboot', '-d', $cname);
      logline("$ip:$port: Disabling pxelinux configuration for $cname\n") or die "log: $!";
    }
    logline("$inp") or die "log: $!";
  }
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub read_list_of_variables {

  # read a file which contains a list of variables
  # format:  key=value
  # value may contain blanks
  # generate a hash

  my $varfile = "/var/log/fai/variables";
  return unless -f $varfile;

  open(VARFILE, '<', "$varfile") or die "open $varfile: $!";
  while (<VARFILE>) {
    next if /^#/;
    if (/^(\w+)=(.*)/) {
      $variables{$1} = $2;
    }
  }
  close(VARFILE);
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub reply_to_client {

  my $name = shift;

  chomp $name;
  $name =~ s/^VARIABLE //;

  unless (exists $variables{$name}) {
    print CLIENT "UNKNOWN $name\n";
    return;
  }
  print CLIENT "OK $variables{$name}\n";
  return;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub usage {

  print << "EOF";
fai-monitor, FAI monitor daemon.

    Copyright (C) 2003-2015 by Thomas Lange

Usage: fai-monitor [OPTIONS]

    -b                  Call fai-chboot to change boot parameter.
    -p PORT             Set port to listen to. Default is 4711.
    -l FILE             Logfile. Default is standard out and
                        '$daemonlogfile' in daemon mode.
    -t TIMEOUT          Timeout for bad clients. 0 to disable.
    -d                  Daemon mode.
    -P FILE             PID-file. Default is '$pidfile'.
                        Used only if starting in daemon mode.
    -T                  Print timestamps in the log.
    -i                  When using -b: send IP of client to fai-choot
                        instead of the hostname the host reports.

EOF
  exit 0;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

getopts('bhTp:l:t:dP:i') || usage;
$opt_h && usage;
$port = $opt_p || 4711;
$timeout = $opt_t || 5;
$daemon = $opt_d || 0;
$timestamp = $opt_T || 0;
$useip = $opt_i || 0;

if (defined($opt_P)) {
  $pidfile = $opt_P;
}

if (defined($opt_d)) {
  (eval "require Proc::Daemon") or
    die "Daemon mode not available, Proc::Daemon not found. Please install libproc-daemon-perl\n";
  # If in daemon mode, use standard daemon log file
  $logfile = $daemonlogfile;
}

if (defined($opt_l)) {
  $logfile = $opt_l;
}

# Constuct a $logfile that open can take as an argument
if ($logfile eq '-') {
  $logfile = ">&STDOUT";
}
else {
  $logfile = ">>$logfile";
}


read_list_of_variables;
server_init;
big_loop;