]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - .inspircd.inc
Include explicit parameter list in ProtocolInterface::SendMode
[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://www.inspircd.org/wiki/index.php/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         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
205         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
206         #
207         # 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!
208         # Redirect stdout to /dev/null if you're worried about the security.
209         #
210         my $pid = fork;
211         if ($pid == 0) {
212                 POSIX::setsid();
213                 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
214                 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
215                 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
216                 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
217                 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
218                 exec qw(valgrind -v --tool=memcheck --leak-check=yes --num-callers=10 --time-stamp=yes --log-fd=2), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
219                 die "Can't execute valgrind: $!\n";
220         }
221 }
222
223 sub dev_screenvaldebug(@)
224 {
225         # Check to see its not 'running' already.
226         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
227
228         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
229         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
230
231         #Check we have gdb
232         checkvalgrind();
233         checkgdb();
234         checkscreen();
235
236         # If we are still alive here.. Try starting the IRCd..
237         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
238         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
239         exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
240         die "Failed to start screen: $!\n";
241 }
242
243 sub cmd_stop()
244 {
245         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
246         # Get to here, we have something to kill.
247         my $pid = getprocessid();
248         print "Stopping InspIRCd (pid: $pid)...\n";
249         my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 30 : 5;
250         kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
251         for (1..$maxwait) {
252                 sleep 1;
253                 if (getstatus() == 0) {
254                         print "InspIRCd Stopped.\n";
255                         return;
256                 }
257         }
258         print "InspIRCd not dying quietly -- forcing kill\n";
259         kill KILL => $pid;
260         return 0;
261 }
262
263 ###
264 # Generic Helper Functions.
265 ###
266
267 # GetPidfile Version 2 - Now With Include Support..
268 # I beg for months for include support in insp, then..
269 # when it is added, it comes around and BITES ME IN THE ASS,
270 # because i then have to code support into this script.. Evil.
271
272 # Craig got bitten in the ass again --
273 # in 1.1 beta the include file is manditory, therefore
274 # if we cant find it, default to %conf%/inspircd.pid.
275 # Note, this also contains a fix for when the pid file is
276 # defined, but defined in a comment (line starts with #)
277 # -- Brain
278
279 my %filesparsed;
280
281 sub getpidfile
282 {
283         my ($file) = @_;
284         # Before we start, do we have a PID already? (Should never occur)
285         if ($pid ne "") {
286                 return;
287         }
288         # Are We using a relative path?
289         if ($file !~ /^\//) {
290                 # Convert it to a full path.
291                 $file = $confpath . $file;
292         }
293
294         # Have we checked this file before?
295         return if $filesparsed{$file};
296         $filesparsed{$file} = 1;
297
298         # Open the File..
299         open INFILE, "< $file" or die "Unable to open file $file included in configuration\n";
300         # Grab entire file contents..
301         my(@lines) = <INFILE>;
302         # Close the file
303         close INFILE;
304
305         # remove trailing spaces
306         chomp(@lines);
307         for my $i (@lines) {
308                 # clean it up
309                 $i =~ s/[^=]+=\s(.*)/\1/;
310                 # Does this file have a pid?
311                 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
312                 {
313                         # Set the PID file and return.
314                         $pidfile = $1;
315                         return;
316                 }
317         }
318
319         # If we get here, NO PID FILE! -- Check for includes
320         for my $i (@lines) {
321                 $i =~ s/[^=]+=\s(.*)/\1/;
322                 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
323                 {
324                         # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
325                         getpidfile($1);
326                         # Was a PID found?
327                         if ($pidfile ne "") {
328                                 # Yes, Return.
329                                 return;
330                         }
331                 }
332         }
333
334         # End of includes / No includes found. Using default.
335         $pidfile = $confpath . "inspircd.pid";
336 }
337
338 sub getstatus {
339         my $pid = getprocessid();
340         return 0 if $pid == 0;
341         return kill 0, $pid;
342 }
343
344
345 sub getprocessid {
346         my $pid;
347         open PIDFILE, "< $pidfile" or return 0;
348         while(<PIDFILE>)
349         {
350                 $pid = $_;
351         }
352         close PIDFILE;
353         return $pid;
354 }
355
356 sub checkvalgrind
357 {
358         unless(`valgrind --version`)
359         {
360                 print "Couldn't start valgrind: $!\n";
361                 exit;
362         }
363 }
364
365 sub checkgdb
366 {
367         unless(`gdb --version`)
368         {
369                 print "Couldn't start gdb: $!\n";
370                 exit;
371         }
372 }
373
374 sub checkscreen
375 {
376         unless(`screen --version`)
377         {
378                 print "Couldn't start screen: $!\n";
379                 exit;
380         }
381 }
382
383 sub checkxmllint
384 {
385         open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
386 }
387
388 sub cmd_checkconf()
389 {
390         checkxmllint();
391         validateconf($conf);
392         print "Config check complete\n";
393         exit 0;
394 }
395
396 my %filechecked;
397
398 sub validateconf
399 {
400         my ($file) = @_;
401
402         # Are We using a relative path?
403         if ($file !~ /^\//) {
404                 # Convert it to a full path..
405                 $file = $confpath . $file;
406         }
407
408         # Have we checked this file before?
409         return if $filechecked{$file};
410         $filechecked{$file} = 1;
411
412         # Open the File..
413         open INFILE, "< $file" or die "Unable to open file $file\n";
414         # Grab entire file contents..
415         my(@lines) = <INFILE>;
416         # Close the file
417         close INFILE;
418
419         # remove trailing spaces
420         chomp(@lines);
421
422         my @newlines = ();
423         my @blanks = ();
424         my $conline;
425
426         push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
427 #       push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
428         push @newlines, "<config>";
429
430         for my $i (@lines)
431         {
432                 # remove trailing newlines
433                 chomp($i);
434
435                 # convert tabs to spaces
436                 $i =~ s/\t/ /g;
437
438                 # remove leading spaces
439                 $i =~ s/^ *//;
440
441                 # remove comments
442                 $i =~ s/^#.*//;
443
444                 # remove trailing #s
445                 $i =~ s/(.*)#$/\1/;
446
447                 # remove trailing comments
448                 my $line = "";
449                 my $quote = 0;
450                 for (my $j = 0; $j < length($i); $j++)
451                 {
452                         if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
453                         $line .= substr($i,$j, 1);
454                 }
455                 $i = $line;
456
457                 # remove trailing spaces
458                 $i =~ s/ *$//;
459
460                 # setup incf for include check and clean it up, since this breaks parsing use local var
461                 my $incf = $i;
462                 $incf =~ s/[^=]+=\s(.*)/\1/;
463
464                 # include file?
465                 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
466                 {
467                         # yes, process it
468                         validateconf($1);
469                 }
470
471                 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
472                 {
473                         push @newlines, $conline;
474                         push @newlines, @blanks;
475                         $conline = $i;
476                 }
477
478                 if ($i =~ /^<.*>$/)
479                 {
480                         $i =~ s/(.*)>$/\1 \/>/;
481                         push @newlines, $i;
482                 }
483                 elsif ($i =~ /.*>$/)
484                 {
485                         $conline .= " $i";
486                         $conline =~ s/(.*)>$/\1 \/>/;
487                         push @blanks, "";
488                         push @newlines, $conline;
489                         push @newlines, @blanks;
490                         $conline = "";
491                         undef @blanks;
492                 }
493                 elsif ($i =~ /^<.*/)
494                 {
495                         $conline = $i;
496                 }
497                 elsif ($conline =~ /^<.*/ && $i)
498                 {
499                         $conline .= " $i";
500                         push @blanks, "";
501                 }
502                 else
503                 {
504                         if ($conline)
505                         {
506                                 push @blanks, $i;
507                         }
508                         else
509                         {
510                                 push @newlines, $i;
511                         }
512                 }
513         }
514         if ($conline)
515         {
516                 push @newlines, $conline;
517                 push @newlines, @blanks;
518         }
519
520         push @newlines, "</config>";
521
522         my $tmpfile;
523         do
524         {
525                 $tmpfile = tmpnam();
526         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
527
528         for my $n (@newlines)
529         {
530                 print TF "$n\n";
531         }
532         close TF;
533
534         my @result = `xmllint -noout $tmpfile 2>&1`;
535         chomp(@result);
536
537         my $skip = 0;
538         for my $n (@result)
539         {
540                 if ($skip)
541                 {
542                         $skip = 0;
543                         next;
544                 }
545                 $n =~ s/$tmpfile\:\d*\: *//g;
546                 if ($n =~ /.*config>.*/)
547                 {
548                         $n = "";
549                         $skip = 1;
550                 }
551
552                 if ($n && !$skip)
553                 {
554                         if ($n =~ /line \d*/)
555                         {
556                                 my $lineno = $n;
557                                 $lineno =~ s/.*line (\d*).*/\1/;
558                                 $lineno = $lineno-2;
559                                 $n =~ s/line (\d*)/line $lineno/;
560                         }
561                         print "$file : $n\n";
562                 }
563         }
564         unlink($tmpfile);
565 }