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