File: INetSim.pm

package info (click to toggle)
inetsim 1.3.1+dfsg.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,672 kB
  • sloc: perl: 13,059; sh: 267; makefile: 18; ansic: 13
file content (426 lines) | stat: -rw-r--r-- 12,232 bytes parent folder | download
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
# -*- perl -*-
#
# INetSim - An internet simulation framework
#
# (c)2007-2019 Matthias Eckert, Thomas Hungenberg
#
#############################################################

my $VERSION = "INetSim 1.3.1 (2019-08-16)";

package INetSim;

use strict;
use warnings;
use POSIX;
# modules to use
use INetSim::CommandLine;
use INetSim::Config;
use INetSim::Log;
use INetSim::FakeTime;
use INetSim::Chargen::TCP;
use INetSim::Chargen::UDP;
use INetSim::Daytime::TCP;
use INetSim::Daytime::UDP;
use INetSim::Discard::TCP;
use INetSim::Discard::UDP;
use INetSim::Echo::TCP;
use INetSim::Echo::UDP;
use INetSim::Quotd::TCP;
use INetSim::Quotd::UDP;
use INetSim::Time::TCP;
use INetSim::Time::UDP;
use INetSim::HTTP;
use INetSim::Ident;
use INetSim::NTP;
use INetSim::SMTP;
use INetSim::POP3;
use INetSim::DNS;
use INetSim::TFTP;
use INetSim::Report;
use INetSim::Finger;
use INetSim::Dummy::TCP;
use INetSim::Dummy::UDP;
use INetSim::FTP;
use INetSim::Syslog;
use INetSim::IRC;


#############################################################
# Local variables

my $PPID = $$;   # Parent PID
my @childs = (); # Child PIDs


#############################################################
# Child process handling
#
sub fork_services {
    my @services_to_start = INetSim::Config::getServicesToStart();
    foreach (@services_to_start) {
        my $pid = fork();
        if ($pid) {
            # we are the parent process
            push(@childs, $pid);
        }
        elsif ($pid == 0){
            # we are the child process
            if(/\Adns\z/) {
                INetSim::DNS::dns();
            }
            elsif(/\Asmtp\z/) {
                INetSim::SMTP->run;
            }
            elsif(/\Asmtps\z/) {
                INetSim::SMTP->new({ SSL => 1 })->run;
            }
            elsif(/\Apop3\z/) {
                INetSim::POP3->run;
            }
            elsif(/\Apop3s\z/) {
                INetSim::POP3->new({ SSL => 1 })->run;
            }
            elsif(/\Ahttp\z/) {
                INetSim::HTTP->run;
            }
            elsif(/\Ahttps\z/) {
                INetSim::HTTP->new({ SSL => 1 })->run;
            }
            elsif(/\Antp\z/) {
                INetSim::NTP->run;
            }
            elsif(/\Atime_tcp\z/) {
                INetSim::Time::TCP->run;
            }
            elsif(/\Atime_udp\z/) {
                INetSim::Time::UDP->run;
            }
            elsif(/\Adaytime_tcp\z/) {
                INetSim::Daytime::TCP->run;
            }
            elsif(/\Adaytime_udp\z/) {
                INetSim::Daytime::UDP->run;
            }
            elsif(/\Aident\z/) {
                INetSim::Ident->run;
            }
            elsif(/\Aecho_tcp\z/) {
                INetSim::Echo::TCP->run;
            }
            elsif(/\Aecho_udp\z/) {
                INetSim::Echo::UDP->run;
            }
            elsif(/\Adiscard_tcp\z/) {
                INetSim::Discard::TCP->run;
            }
            elsif(/\Adiscard_udp\z/) {
                INetSim::Discard::UDP->run;
            }
            elsif(/\Achargen_tcp\z/) {
                INetSim::Chargen::TCP->run;
            }
            elsif(/\Achargen_udp\z/) {
                INetSim::Chargen::UDP->run;
            }
            elsif(/\Aquotd_tcp\z/) {
                INetSim::Quotd::TCP->run;
            }
            elsif(/\Aquotd_udp\z/) {
                INetSim::Quotd::UDP->run;
            }
            elsif(/\Atftp\z/) {
                INetSim::TFTP->run;
            }
            elsif(/\Afinger\z/) {
                INetSim::Finger->run;
            }
            elsif(/\Adummy_tcp\z/) {
                INetSim::Dummy::TCP->run;
            }
            elsif(/\Adummy_udp\z/) {
                INetSim::Dummy::UDP->run;
            }
            elsif(/\Aftp\z/) {
                INetSim::FTP->run;
            }
            elsif(/\Aftps\z/) {
                INetSim::FTP->new({ SSL => 1 })->run;
            }
            elsif(/\Asyslog\z/) {
                INetSim::Syslog->run;
            }
            elsif(/\Airc\z/) {
                INetSim::IRC->run;
            }
            elsif(/\Aircs\z/) {
                INetSim::IRC->run( SSL => 1 );
            }
        }
        else {
            error_exit("Could not fork: $!", 1);
        }
    }
    sleep 1;
}


sub handle_pid {
    my $cmd = shift;
    my $pidfile = INetSim::CommandLine::getCommandLineOption("pidfile");

    $pidfile =~ /(.*)/; # evil untaint
    $pidfile = $1;
    if ($cmd eq "create") {
        if (-f $pidfile) {
            print STDOUT "PIDfile '$pidfile' exists - INetSim already running?\n";
            exit 1;
        }
        else {
            if (! open (PIDFILE, "> $pidfile")) {
                print STDOUT "Unable to open PIDfile for writing: $!\n";
                exit 1;
            }
            print PIDFILE $PPID;
            close PIDFILE;
        }
    }
    elsif ($cmd eq "remove") {
        if (-f $pidfile) {
            unlink $pidfile;
        }
        else {
            print STDOUT "Hmm, PIDfile '$pidfile' not found (but, who cares?)\n";
        }
    }
}


sub auto_faketime {
    if (INetSim::Config::getConfigParameter("Faketime_AutoDelay") > 0) {
        my $pid = fork();
        if ($pid) {
            # we are the parent process
            push(@childs, $pid);
        }
        elsif ($pid == 0){
            # we are the child process
            INetSim::FakeTime::auto_faketime();
        }
    }
}


sub redirect_packets {
    if (INetSim::Config::getConfigParameter("Redirect_Enabled")) {
        # check for linux
        if ($^O !~ /linux/i) {
            INetSim::Log::MainLog("failed! Error: Sorry, the Redirect module does not support this operating system!", "redirect");
            return 0;
        }
        # check for nfqueue library
        eval {
            eval "use nfqueue; 1" or die;
        };
        if ($@) {
            INetSim::Log::MainLog("failed! Error: Sorry, this module requires the perl nfqueue-bindings!", "redirect");
            return 0;
        }
        # check for redirect module
        eval {
               eval "use INetSim::Redirect; 1" or die;
        };
        if ($@) {
            INetSim::Log::MainLog("failed! Error: $@", "redirect");
            return 0;
        }
        my $pid = fork();
        if ($pid) {
            # we are the parent process
            push(@childs, $pid);
        }
        elsif ($pid == 0){
            # we are the child process
            INetSim::Redirect::run();
        }
    }
}


sub rest_in_peace {
    my $count = @childs;
    my $i;

    for ($i = 0; $i < $count; $i++) {
        waitpid(-1,&WNOHANG);
        if (! (kill (0, $childs[$i]))) {
            splice (@childs, $i, 1);
            $count = @childs;
            $i--;
        }
    }
}


sub wait_pids {
    wait();
    foreach (@childs){
        waitpid($_, 0);
    }
}


sub kill_pids {
    foreach (@childs){
        kill("TERM", $_);
        waitpid($_, 0);
    }
}


sub error_exit {
    my $msg = shift;
    if (! defined $msg) {
        $msg = "Unknown error";
    }
    my $exitcode = shift;
    if (! defined $exitcode) {
        $exitcode = 1;
    }
    elsif (($exitcode !~ /\A[\d]{1,3}\z/) || (int($exitcode) < 0) || (int($exitcode > 255))) {
        print STDOUT "Illegal exit code!\n";
        $exitcode = 1;
    }

    print STDOUT "Error: $msg.\n";

    kill_pids();
    wait_pids();
    handle_pid("remove");

    exit 1;
}


#############################################################
# Main
#

sub main {
    # Parse commandline options
    INetSim::CommandLine::parse_options();

    # Check command line option 'help'
    if (INetSim::CommandLine::getCommandLineOption("help")) {
        print STDOUT << "EOF";
$VERSION by Matthias Eckert & Thomas Hungenberg

Usage: $0 [options]

Available options:
  --help                         Print this help message.
  --version                      Show version information.
  --config=<filename>            Configuration file to use.
  --log-dir=<directory>          Directory logfiles are written to.
  --data-dir=<directory>         Directory containing service data.
  --report-dir=<directory>       Directory reports are written to.
  --bind-address=<IP address>    Default IP address to bind services to.
                                 Overrides configuration option 'default_bind_address'.
  --max-childs=<num>             Default maximum number of child processes per service.
                                 Overrides configuration option 'default_max_childs'.
  --user=<username>              Default user to run services.
                                 Overrides configuration option 'default_run_as_user'.
  --faketime-init-delta=<secs>   Initial faketime delta (seconds).
                                 Overrides configuration option 'faketime_init_delta'.
  --faketime-auto-delay=<secs>   Delay for auto incrementing faketime (seconds).
                                 Overrides configuration option 'faketime_auto_delay'.
  --faketime-auto-incr=<secs>    Delta for auto incrementing faketime (seconds).
                                 Overrides configuration option 'faketime_auto_increment'.
  --session=<id>                 Session id to use. Defaults to main process id.
  --pidfile=<filename>           Pid file to use. Defaults to '/var/run/inetsim.pid'.

EOF
;
        exit 0;
    }
    elsif (INetSim::CommandLine::getCommandLineOption("version")) {
        print STDOUT "$VERSION by Matthias Eckert & Thomas Hungenberg\n";
        exit 0;
    }


    # Check if we are running with root privileges (EUID 0)
    if ( $> != 0 ) {
        print STDOUT "Sorry, this program must be started as root!\n";
        exit 1;
    }

    # Check if group "inetsim" exists on system
    my $gid = getgrnam("inetsim");
    if (! defined $gid) {
        print STDOUT "No such group 'inetsim' configured on this system!\n";
        print STDOUT "Please create group and start again. See documentation for more information.\n";
        exit 1;
    }

    print STDOUT "$VERSION by Matthias Eckert & Thomas Hungenberg\n";

    # create pidfile
    handle_pid("create");

    # Parse configuration file
    INetSim::Config::parse_config();

    # Check if there are services to start configured, else exit
    if (! scalar(INetSim::Config::getServicesToStart())) {
        INetSim::Log::MainLog("No services to start configured. Exiting.");
        handle_pid("remove");
        exit 0;
    }

    # ignore some signal handlers during startup
    local $SIG{'INT'} = 'IGNORE';
    local $SIG{'HUP'} = 'IGNORE';
    local $SIG{'TERM'} = 'IGNORE';

    INetSim::Log::MainLog("=== INetSim main process started (PID $PPID) ===");
    INetSim::Log::MainLog("Session ID:     " . INetSim::Config::getConfigParameter("SessionID"));
    INetSim::Log::MainLog("Listening on:   " . INetSim::Config::getConfigParameter("Default_BindAddress"));
    INetSim::Log::MainLog("Real Date/Time: " . strftime "%Y-%m-%d %H:%M:%S", localtime);
    INetSim::Log::MainLog("Fake Date/Time: " . (strftime "%Y-%m-%d %H:%M:%S", localtime(INetSim::FakeTime::get_faketime())). " (Delta: " . INetSim::Config::getConfigParameter("Faketime_Delta") . " seconds)");
    INetSim::Log::MainLog(" Forking services...");
    fork_services();
    auto_faketime();
    redirect_packets();

    if ($$ == $PPID) {
        $0 = 'inetsim_main';
        sleep 2;
        # reap zombies ;-)
        rest_in_peace();
        INetSim::Log::MainLog(" done.");
        INetSim::Log::MainLog("Simulation running.");
        # catch up some signalhandlers for the parent process
        local $SIG{'INT'} = sub {kill_pids();};
        local $SIG{'HUP'} = sub {kill_pids();};
        local $SIG{'TERM'} = sub {kill_pids();};
        wait_pids();
        INetSim::Log::MainLog("Simulation stopped.");

        # create report
        if (INetSim::Config::getConfigParameter("Create_Reports")) {
            INetSim::Report::GenReport();
        }

        INetSim::Log::MainLog("=== INetSim main process stopped (PID $PPID) ===");
        INetSim::Log::MainLog(".");
    }

    # delete pidfile
    handle_pid("remove");
    exit 0;
}


1;
#