]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - .inspircd.inc
Migrate SSL metadata and visible information (/whois line) to single module
[user/henk/code/inspircd.git] / .inspircd.inc
1 #!/usr/bin/perl
2 #       +------------------------------------+
3 #       | Inspire Internet Relay Chat Daemon |
4 #       +------------------------------------+
5 #
6 #  InspIRCd: (C) 2002-2009 InspIRCd Development Team
7 # See: http://wiki.inspircd.org/Credits
8 #
9 # This program is free but copyrighted software; see
10 #          the file COPYING for details.
11 #
12 # ---------------------------------------------------
13 #
14 use strict;
15 use POSIX;
16 use Fcntl;
17
18 my $basepath    =       "@BASE_DIR@";
19 my $confpath    =       "@CONFIG_DIR@/";
20 my $binpath     =       "@BINARY_DIR@";
21 my $libpath     =       "@LIBRARY_DIR@";
22 my $valgrindlogpath     =       "$basepath/valgrindlogs";
23 my $executable  =       "@EXECUTABLE@";
24 my $version     =       "@VERSION@";
25
26 our($pid,$pidfile);
27 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
28 my $arg = shift(@ARGV);
29 my $conf = $confpath . "inspircd.conf";
30 for my $a (@ARGV)
31 {
32         if ($a =~ m/^--config=(.*)$/)
33         {
34                 $conf = $1;
35                 last;
36         }
37 }
38 getpidfile($conf);
39
40 # System for naming script command subs:
41 # cmd_<name> - Normal command for use by users.
42 # dev_<name> - Developer commands.
43 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
44 # Ideally command subs shouldn't return.
45
46 my $subname = $arg;
47 $subname =~ s/-/_/g;
48 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
49 if (!defined($sub))
50 {
51         print STDERR "Invalid command or none given.\n";
52         cmd_help();
53         exit 1;
54 }
55 else
56 {
57         $sub->(@ARGV);
58         exit 0;
59 }
60
61 sub cmd_help()
62 {
63         my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
64         my @cmds = grep /^cmd_/, @subs;
65         my @devs = grep /^dev_/, @subs;
66         local $_;
67         $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
68         $_ =~ s/_/-/g foreach (@cmds, @devs);
69         print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
70         print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
71         exit 0;
72 }
73
74 sub cmd_status()
75 {
76         if (getstatus() == 1) {
77                 my $pid = getprocessid();
78                 print "InspIRCd is running (PID: $pid)\n";
79                 exit();
80         } else {
81                 print "InspIRCd is not running. (Or PID File not found)\n";
82                 exit();
83         }
84 }
85
86 sub cmd_rehash()
87 {
88         if (getstatus() == 1) {
89                 my $pid = getprocessid();
90                 system("kill -HUP $pid >/dev/null 2>&1");
91                 print "InspIRCd rehashed (pid: $pid).\n";
92                 exit();
93         } else {
94                 print "InspIRCd is not running. (Or PID File not found)\n";
95                 exit();
96         }
97 }
98
99 sub cmd_cron()
100 {
101         if (getstatus() == 0) { goto &cmd_start(); }
102         exit();
103 }
104
105 sub cmd_version()
106 {
107         print "InspIRCd version: $version\n";
108         exit();
109 }
110
111 sub cmd_restart(@)
112 {
113         cmd_stop();
114         unlink($pidfile) if (-e $pidfile);
115         goto &cmd_start;
116 }
117
118 sub hid_cheese_sandwich()
119 {
120         print "Creating Cheese Sandwich..\n";
121         print "Done.\n";
122         exit();
123 }
124
125 sub cmd_start(@)
126 {
127         # Check to see its not 'running' already.
128         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
129         # If we are still alive here.. Try starting the IRCd..
130         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
131         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
132
133         exec { "$binpath/$executable" } "$binpath/$executable", @_;
134         die "Failed to start IRCd: $!\n";
135 }
136
137 sub dev_debug(@)
138 {
139         # Check to see its not 'running' already.
140         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
141
142         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
143         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
144
145         # Check we have gdb
146         checkgdb();
147
148         # If we are still alive here.. Try starting the IRCd..
149         exec 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(-nofork -debug), @_;
150         die "Failed to start GDB: $!\n";
151 }
152
153 sub dev_screendebug(@)
154 {
155         # Check to see its not 'running' already.
156         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
157
158         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
159
160         #Check we have gdb
161         checkgdb();
162         checkscreen();
163
164         # If we are still alive here.. Try starting the IRCd..
165         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
166         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
167         exec qw(screen -m -d gdb), "--comand=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(-nofork -debug -nolog), @_;
168         die "Failed to start screen: $!\n";
169 }
170
171 sub dev_valdebug(@)
172 {
173         # Check to see its not 'running' already.
174         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
175
176         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
177         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
178
179         # Check we have valgrind and gdb
180         checkvalgrind();
181         checkgdb();
182
183         # If we are still alive here.. Try starting the IRCd..
184         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
185         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
186         exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
187         die "Failed to start valgrind: $!\n";
188 }
189
190 sub dev_valdebug_unattended(@)
191 {
192         # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
193         # Check to see its not 'running' already.
194         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
195
196         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
197         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
198
199         # Check we have valgrind and gdb
200         checkvalgrind();
201         checkgdb();
202
203         # If we are still alive here.. Try starting the IRCd..
204         #
205         # NOTE: Saving the debug log (redirected stdout), while useful, is a potential security risk AND one hell of a spacehog. DO NOT SAVE THIS WHERE EVERYONE HAS ACCESS!
206         # Redirect stdout to /dev/null if you're worried about the security.
207         #
208         my $pid = fork;
209         if ($pid == 0) {
210                 POSIX::setsid();
211                 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
212                 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
213                 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
214                 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
215                 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
216                 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
217         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
218         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
219                 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=15 --track-fds=yes),
220                         "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
221                         qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
222                         "$binpath/$executable", qw(-nofork -debug -nolog), @_;
223                 die "Can't execute valgrind: $!\n";
224         }
225 }
226
227 sub dev_screenvaldebug(@)
228 {
229         # Check to see its not 'running' already.
230         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
231
232         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
233         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
234
235         #Check we have gdb
236         checkvalgrind();
237         checkgdb();
238         checkscreen();
239
240         # If we are still alive here.. Try starting the IRCd..
241         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
242         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
243         exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
244         die "Failed to start screen: $!\n";
245 }
246
247 sub cmd_stop()
248 {
249         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
250         # Get to here, we have something to kill.
251         my $pid = getprocessid();
252         print "Stopping InspIRCd (pid: $pid)...\n";
253         my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 5;
254         kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
255         for (1..$maxwait) {
256                 sleep 1;
257                 if (getstatus() == 0) {
258                         print "InspIRCd Stopped.\n";
259                         return;
260                 }
261         }
262         print "InspIRCd not dying quietly -- forcing kill\n";
263         kill KILL => $pid;
264         return 0;
265 }
266
267 ###
268 # Generic Helper Functions.
269 ###
270
271 # GetPidfile Version 2 - Now With Include Support..
272 # I beg for months for include support in insp, then..
273 # when it is added, it comes around and BITES ME IN THE ASS,
274 # because i then have to code support into this script.. Evil.
275
276 # Craig got bitten in the ass again --
277 # in 1.1 beta the include file is manditory, therefore
278 # if we cant find it, default to %conf%/inspircd.pid.
279 # Note, this also contains a fix for when the pid file is
280 # defined, but defined in a comment (line starts with #)
281 # -- Brain
282
283 my %filesparsed;
284
285 sub getpidfile
286 {
287         my ($file) = @_;
288         # Before we start, do we have a PID already? (Should never occur)
289         if ($pid ne "") {
290                 return;
291         }
292         # Are We using a relative path?
293         if ($file !~ /^\//) {
294                 # Convert it to a full path.
295                 $file = $confpath . $file;
296         }
297
298         # Have we checked this file before?
299         return if $filesparsed{$file};
300         $filesparsed{$file} = 1;
301
302         # Open the File..
303         open INFILE, "< $file" or die "Unable to open file $file included in configuration\n";
304         # Grab entire file contents..
305         my(@lines) = <INFILE>;
306         # Close the file
307         close INFILE;
308
309         # remove trailing spaces
310         chomp(@lines);
311         for my $i (@lines) {
312                 # clean it up
313                 $i =~ s/[^=]+=\s(.*)/\1/;
314                 # Does this file have a pid?
315                 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
316                 {
317                         # Set the PID file and return.
318                         $pidfile = $1;
319                         return;
320                 }
321         }
322
323         # If we get here, NO PID FILE! -- Check for includes
324         for my $i (@lines) {
325                 $i =~ s/[^=]+=\s(.*)/\1/;
326                 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
327                 {
328                         # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
329                         getpidfile($1);
330                         # Was a PID found?
331                         if ($pidfile ne "") {
332                                 # Yes, Return.
333                                 return;
334                         }
335                 }
336         }
337
338         # End of includes / No includes found. Using default.
339         $pidfile = $confpath . "inspircd.pid";
340 }
341
342 sub getstatus {
343         my $pid = getprocessid();
344         return 0 if $pid == 0;
345         return kill 0, $pid;
346 }
347
348
349 sub getprocessid {
350         my $pid;
351         open PIDFILE, "< $pidfile" or return 0;
352         while(<PIDFILE>)
353         {
354                 $pid = $_;
355         }
356         close PIDFILE;
357         return $pid;
358 }
359
360 sub checkvalgrind
361 {
362         unless(`valgrind --version`)
363         {
364                 print "Couldn't start valgrind: $!\n";
365                 exit;
366         }
367 }
368
369 sub checkgdb
370 {
371         unless(`gdb --version`)
372         {
373                 print "Couldn't start gdb: $!\n";
374                 exit;
375         }
376 }
377
378 sub checkscreen
379 {
380         unless(`screen --version`)
381         {
382                 print "Couldn't start screen: $!\n";
383                 exit;
384         }
385 }
386
387 sub checkxmllint
388 {
389         open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
390 }
391
392 sub cmd_checkconf()
393 {
394         checkxmllint();
395         validateconf($conf);
396         print "Config check complete\n";
397         exit 0;
398 }
399
400 my %filechecked;
401
402 sub validateconf
403 {
404         my ($file) = @_;
405
406         # Are We using a relative path?
407         if ($file !~ /^\//) {
408                 # Convert it to a full path..
409                 $file = $confpath . $file;
410         }
411
412         # Have we checked this file before?
413         return if $filechecked{$file};
414         $filechecked{$file} = 1;
415
416         # Open the File..
417         open INFILE, "< $file" or die "Unable to open file $file\n";
418         # Grab entire file contents..
419         my(@lines) = <INFILE>;
420         # Close the file
421         close INFILE;
422
423         # remove trailing spaces
424         chomp(@lines);
425
426         my @newlines = ();
427         my @blanks = ();
428         my $conline;
429
430         push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
431 #       push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
432         push @newlines, "<config>";
433
434         for my $i (@lines)
435         {
436                 # remove trailing newlines
437                 chomp($i);
438
439                 # convert tabs to spaces
440                 $i =~ s/\t/ /g;
441
442                 # remove leading spaces
443                 $i =~ s/^ *//;
444
445                 # remove comments
446                 $i =~ s/^#.*//;
447
448                 # remove trailing #s
449                 $i =~ s/(.*)#$/\1/;
450
451                 # remove trailing comments
452                 my $line = "";
453                 my $quote = 0;
454                 for (my $j = 0; $j < length($i); $j++)
455                 {
456                         if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
457                         $line .= substr($i,$j, 1);
458                 }
459                 $i = $line;
460
461                 # remove trailing spaces
462                 $i =~ s/ *$//;
463
464                 # setup incf for include check and clean it up, since this breaks parsing use local var
465                 my $incf = $i;
466                 $incf =~ s/[^=]+=\s(.*)/\1/;
467
468                 # include file?
469                 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
470                 {
471                         # yes, process it
472                         validateconf($1);
473                 }
474
475                 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
476                 {
477                         push @newlines, $conline;
478                         push @newlines, @blanks;
479                         $conline = $i;
480                 }
481
482                 if ($i =~ /^<.*>$/)
483                 {
484                         $i =~ s/(.*)>$/\1 \/>/;
485                         push @newlines, $i;
486                 }
487                 elsif ($i =~ /.*>$/)
488                 {
489                         $conline .= " $i";
490                         $conline =~ s/(.*)>$/\1 \/>/;
491                         push @blanks, "";
492                         push @newlines, $conline;
493                         push @newlines, @blanks;
494                         $conline = "";
495                         undef @blanks;
496                 }
497                 elsif ($i =~ /^<.*/)
498                 {
499                         $conline = $i;
500                 }
501                 elsif ($conline =~ /^<.*/ && $i)
502                 {
503                         $conline .= " $i";
504                         push @blanks, "";
505                 }
506                 else
507                 {
508                         if ($conline)
509                         {
510                                 push @blanks, $i;
511                         }
512                         else
513                         {
514                                 push @newlines, $i;
515                         }
516                 }
517         }
518         if ($conline)
519         {
520                 push @newlines, $conline;
521                 push @newlines, @blanks;
522         }
523
524         push @newlines, "</config>";
525
526         my $tmpfile;
527         do
528         {
529                 $tmpfile = tmpnam();
530         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
531
532         for my $n (@newlines)
533         {
534                 print TF "$n\n";
535         }
536         close TF;
537
538         my @result = `xmllint -noout $tmpfile 2>&1`;
539         chomp(@result);
540
541         my $skip = 0;
542         for my $n (@result)
543         {
544                 if ($skip)
545                 {
546                         $skip = 0;
547                         next;
548                 }
549                 $n =~ s/$tmpfile\:\d*\: *//g;
550                 if ($n =~ /.*config>.*/)
551                 {
552                         $n = "";
553                         $skip = 1;
554                 }
555
556                 if ($n && !$skip)
557                 {
558                         if ($n =~ /line \d*/)
559                         {
560                                 my $lineno = $n;
561                                 $lineno =~ s/.*line (\d*).*/\1/;
562                                 $lineno = $lineno-2;
563                                 $n =~ s/line (\d*)/line $lineno/;
564                         }
565                         print "$file : $n\n";
566                 }
567         }
568         unlink($tmpfile);
569 }