1 |
/* |
2 |
* main.c -- |
3 |
* |
4 |
* core event handling |
5 |
* signal handling |
6 |
* command line arguments |
7 |
*/ |
8 |
/* |
9 |
* Copyright (C) 1997 Robey Pointer |
10 |
* Copyright (C) 1999, 2000, 2001, 2002 Eggheads Development Team |
11 |
* |
12 |
* This program is free software; you can redistribute it and/or |
13 |
* modify it under the terms of the GNU General Public License |
14 |
* as published by the Free Software Foundation; either version 2 |
15 |
* of the License, or (at your option) any later version. |
16 |
* |
17 |
* This program is distributed in the hope that it will be useful, |
18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 |
* GNU General Public License for more details. |
21 |
* |
22 |
* You should have received a copy of the GNU General Public License |
23 |
* along with this program; if not, write to the Free Software |
24 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 |
*/ |
26 |
/* |
27 |
* The author (Robey Pointer) can be reached at: robey@netcom.com |
28 |
* NOTE: Robey is no longer working on this code, there is a discussion |
29 |
* list available at eggheads@eggheads.org. |
30 |
*/ |
31 |
|
32 |
#ifndef lint |
33 |
static const char rcsid[] = "$Id$"; |
34 |
#endif |
35 |
|
36 |
#include "main.h" |
37 |
#include <fcntl.h> |
38 |
#include <time.h> |
39 |
#include <errno.h> |
40 |
#include <signal.h> |
41 |
#include <netdb.h> |
42 |
#include <setjmp.h> |
43 |
#include "users.h" /* check_expired_ignores, |
44 |
autolink_cycle, |
45 |
get_user_by_handle */ |
46 |
#include "chanprog.h" /* tell_verbose_status, |
47 |
chanprog, rehash */ |
48 |
#include "net.h" /* killsock, setsock, |
49 |
dequeue_sockets, sockgets */ |
50 |
#include "userrec.h" /* write_userfile, count_users */ |
51 |
#include <locale.h> |
52 |
|
53 |
#ifdef STOP_UAC /* osf/1 complains a lot */ |
54 |
#include <sys/sysinfo.h> |
55 |
#define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */ |
56 |
#endif |
57 |
/* Some systems have a working sys/wait.h even though configure will |
58 |
* decide it's not bsd compatable. Oh well. |
59 |
*/ |
60 |
|
61 |
#include "chan.h" |
62 |
#include "modules.h" |
63 |
#include "tandem.h" |
64 |
#include "bg.h" |
65 |
#include "egg_timer.h" |
66 |
#include "core_binds.h" |
67 |
#include "logfile.h" |
68 |
#include "misc.h" |
69 |
#include "dccutil.h" /* dprintf_eggdrop, new_dcc, |
70 |
dcc_chatter, dcc_remove_lost, |
71 |
lostdcc */ |
72 |
|
73 |
#include "lib/adns/adns.h" |
74 |
|
75 |
#ifdef CYGWIN_HACKS |
76 |
#include <windows.h> |
77 |
#endif |
78 |
|
79 |
#ifndef _POSIX_SOURCE |
80 |
/* Solaris needs this */ |
81 |
#define _POSIX_SOURCE 1 |
82 |
#endif |
83 |
|
84 |
extern char userfile[], botnetnick[]; |
85 |
extern int dcc_total, conmask, cache_hit, cache_miss; |
86 |
extern struct dcc_t *dcc; |
87 |
extern struct userrec *userlist; |
88 |
extern struct chanset_t *chanset; |
89 |
extern jmp_buf alarmret; |
90 |
|
91 |
#ifndef MAKING_MODS |
92 |
extern struct dcc_table DCC_CHAT; |
93 |
#endif /* MAKING_MODS */ |
94 |
|
95 |
/* |
96 |
* Please use the PATCH macro instead of directly altering the version |
97 |
* string from now on (it makes it much easier to maintain patches). |
98 |
* Also please read the README file regarding your rights to distribute |
99 |
* modified versions of this bot. |
100 |
*/ |
101 |
|
102 |
char egg_version[1024] = VERSION; |
103 |
int egg_numver = VERSION_NUM; |
104 |
|
105 |
eggdrop_t *egg = NULL; /* Eggdrop's context */ |
106 |
|
107 |
char notify_new[121] = ""; /* Person to send a note to for new users */ |
108 |
int default_flags = 0; /* Default user flags and */ |
109 |
int default_uflags = 0; /* Default userdefinied flags for people |
110 |
who say 'hello' or for .adduser */ |
111 |
|
112 |
int backgrd = 1; /* Run in the background? */ |
113 |
int con_chan = 0; /* Foreground: constantly display channel |
114 |
stats? */ |
115 |
int term_z = 0; /* Foreground: use the terminal as a party |
116 |
line? */ |
117 |
char configfile[121] = "eggdrop.conf"; /* Name of the config file */ |
118 |
static char preload_module[121] = ""; /* Name of the module to preload */ |
119 |
char helpdir[121]; /* Directory of help files (if used) */ |
120 |
char textdir[121] = ""; /* Directory for text files that get dumped */ |
121 |
time_t online_since; /* Unix-time that the bot loaded up */ |
122 |
int make_userfile = 0; /* Using bot in make-userfile mode? (first |
123 |
user to 'hello' becomes master) */ |
124 |
char owner[121] = ""; /* Permanent owner(s) of the bot */ |
125 |
char pid_file[120]; /* Name of the file for the pid to be |
126 |
stored in */ |
127 |
int save_users_at = 0; /* How many minutes past the hour to |
128 |
save the userfile? */ |
129 |
int notify_users_at = 0; /* How many minutes past the hour to |
130 |
notify users of notes? */ |
131 |
char version[81]; /* Version info (long form) */ |
132 |
char ver[41]; /* Version info (short form) */ |
133 |
char egg_xtra[2048]; /* Patch info */ |
134 |
int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */ |
135 |
int do_restart = 0; /* .restart has been called, restart asap */ |
136 |
int die_on_sighup = 0; /* die if bot receives SIGHUP */ |
137 |
int die_on_sigterm = 1; /* die if bot receives SIGTERM */ |
138 |
int resolve_timeout = 15; /* hostname/address lookup timeout */ |
139 |
char quit_msg[1024]; /* quit message */ |
140 |
time_t now; /* duh, now :) */ |
141 |
egg_timeval_t egg_timeval_now; /* Same thing, but seconds and microseconds. */ |
142 |
|
143 |
/* Traffic stats |
144 |
*/ |
145 |
#include "traffic.h" /* egg_traffic_t */ |
146 |
egg_traffic_t traffic; |
147 |
|
148 |
void fatal(const char *s, int recoverable) |
149 |
{ |
150 |
int i; |
151 |
|
152 |
putlog(LOG_MISC, "*", "* %s", s); |
153 |
flushlogs(); |
154 |
for (i = 0; i < dcc_total; i++) |
155 |
if (dcc[i].sock >= 0) |
156 |
killsock(dcc[i].sock); |
157 |
unlink(pid_file); |
158 |
if (!recoverable) { |
159 |
bg_send_quit(BG_ABORT); |
160 |
exit(1); |
161 |
} |
162 |
} |
163 |
|
164 |
static void check_expired_dcc() |
165 |
{ |
166 |
int i; |
167 |
|
168 |
for (i = 0; i < dcc_total; i++) |
169 |
if (dcc[i].type && dcc[i].type->timeout_val && |
170 |
((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) { |
171 |
if (dcc[i].type->timeout) |
172 |
dcc[i].type->timeout(i); |
173 |
else if (dcc[i].type->eof) |
174 |
dcc[i].type->eof(i); |
175 |
else |
176 |
continue; |
177 |
/* Only timeout 1 socket per cycle, too risky for more */ |
178 |
return; |
179 |
} |
180 |
} |
181 |
|
182 |
static void got_bus(int z) |
183 |
{ |
184 |
fatal("BUS ERROR -- CRASHING!", 1); |
185 |
#ifdef SA_RESETHAND |
186 |
kill(getpid(), SIGBUS); |
187 |
#else |
188 |
bg_send_quit(BG_ABORT); |
189 |
exit(1); |
190 |
#endif |
191 |
} |
192 |
|
193 |
static void got_segv(int z) |
194 |
{ |
195 |
fatal("SEGMENT VIOLATION -- CRASHING!", 1); |
196 |
#ifdef SA_RESETHAND |
197 |
kill(getpid(), SIGSEGV); |
198 |
#else |
199 |
bg_send_quit(BG_ABORT); |
200 |
exit(1); |
201 |
#endif |
202 |
} |
203 |
|
204 |
static void got_fpe(int z) |
205 |
{ |
206 |
fatal("FLOATING POINT ERROR -- CRASHING!", 0); |
207 |
} |
208 |
|
209 |
static void got_term(int z) |
210 |
{ |
211 |
write_userfile(-1); |
212 |
check_bind_event("sigterm"); |
213 |
if (die_on_sigterm) { |
214 |
fatal("TERMINATE SIGNAL -- SIGNING OFF", 0); |
215 |
} else { |
216 |
putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)"); |
217 |
} |
218 |
} |
219 |
|
220 |
static void got_quit(int z) |
221 |
{ |
222 |
check_bind_event("sigquit"); |
223 |
putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)"); |
224 |
return; |
225 |
} |
226 |
|
227 |
static void got_hup(int z) |
228 |
{ |
229 |
write_userfile(-1); |
230 |
check_bind_event("sighup"); |
231 |
if (die_on_sighup) { |
232 |
fatal("HANGUP SIGNAL -- SIGNING OFF", 0); |
233 |
} else |
234 |
putlog(LOG_MISC, "*", "Received HUP signal: rehashing..."); |
235 |
do_restart = -2; |
236 |
return; |
237 |
} |
238 |
|
239 |
/* A call to resolver (gethostbyname, etc) timed out |
240 |
*/ |
241 |
static void got_alarm(int z) |
242 |
{ |
243 |
longjmp(alarmret, 1); |
244 |
|
245 |
/* -Never reached- */ |
246 |
} |
247 |
|
248 |
/* Got ILL signal |
249 |
*/ |
250 |
static void got_ill(int z) |
251 |
{ |
252 |
check_bind_event("sigill"); |
253 |
} |
254 |
|
255 |
|
256 |
static void print_version (void) |
257 |
{ |
258 |
char x[1024], *z = x; |
259 |
|
260 |
strlcpy(x, egg_version, sizeof x); |
261 |
newsplit(&z); |
262 |
newsplit(&z); |
263 |
printf("%s\n", version); |
264 |
if (z[0]) |
265 |
printf(" (patches: %s)\n", z); |
266 |
} |
267 |
|
268 |
static void print_help (void) |
269 |
{ |
270 |
print_version (); |
271 |
printf("\n\ |
272 |
Usage: %s [OPTIONS]... [FILE]\n", PACKAGE); |
273 |
printf(_("\n\ |
274 |
-h, --help Print help and exit\n\ |
275 |
-v, --version Print version and exit\n\ |
276 |
-n, --foreground Don't go into the background (default=off)\n\ |
277 |
-c --channel-stats (with -n) Display channel stats every 10 seconds\n\ |
278 |
-t --terminal (with -n) Use terminal to simulate dcc-chat\n\ |
279 |
-m --make-userfile Userfile creation mode\n\ |
280 |
-p --load-module=STRING Modules to load before parsing any configuration\n\ |
281 |
file\n\ |
282 |
FILE optional config filename (default 'eggdrop.conf')\n")); |
283 |
printf("\n"); |
284 |
} |
285 |
|
286 |
static void do_args(int argc, char * const *argv) |
287 |
{ |
288 |
int c; |
289 |
|
290 |
optarg = 0; |
291 |
optind = 1; |
292 |
opterr = 1; |
293 |
optopt = '?'; |
294 |
|
295 |
while (1) { |
296 |
int option_index = 0; |
297 |
static struct option long_options[] = { |
298 |
{ "help", 0, NULL, 'h' }, |
299 |
{ "version", 0, NULL, 'v' }, |
300 |
{ "load-module", 1, NULL, 'p' }, |
301 |
{ "foreground", 0, NULL, 'n' }, |
302 |
{ "channel-stats", 0, NULL, 'c' }, |
303 |
{ "terminal", 0, NULL, 't' }, |
304 |
{ "make-userfile", 0, NULL, 'm' }, |
305 |
{ NULL, 0, NULL, 0 } |
306 |
}; |
307 |
|
308 |
c = getopt_long (argc, argv, "hvp:nctm", long_options, &option_index); |
309 |
|
310 |
if (c == -1) break; |
311 |
|
312 |
switch (c) { |
313 |
case 'n': |
314 |
backgrd = 0; |
315 |
break; |
316 |
|
317 |
case 'c': |
318 |
con_chan = 1; |
319 |
term_z = 0; |
320 |
break; |
321 |
|
322 |
case 't': |
323 |
con_chan = 0; |
324 |
term_z = 1; |
325 |
break; |
326 |
|
327 |
case 'm': |
328 |
make_userfile = 1; |
329 |
break; |
330 |
|
331 |
case 'p': |
332 |
strlcpy(preload_module, optarg, sizeof preload_module); |
333 |
break; |
334 |
|
335 |
case 'v': |
336 |
print_version(); |
337 |
bg_send_quit(BG_ABORT); |
338 |
exit(0); |
339 |
break; /* this should never be reached */ |
340 |
|
341 |
case 'h': |
342 |
print_help(); |
343 |
bg_send_quit(BG_ABORT); |
344 |
exit(0); |
345 |
break; /* this should never be reached */ |
346 |
|
347 |
case 0: /* Long option with no short option */ |
348 |
case '?': /* Invalid option. */ |
349 |
/* getopt_long() already printed an error message. */ |
350 |
bg_send_quit(BG_ABORT); |
351 |
exit(1); |
352 |
|
353 |
default: /* bug: option not considered. */ |
354 |
fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c); |
355 |
bg_send_quit(BG_ABORT); |
356 |
exit(1); |
357 |
break; /* this should never be reached */ |
358 |
} |
359 |
} |
360 |
if (optind < argc) |
361 |
strlcpy(configfile, argv[optind], sizeof configfile); |
362 |
} |
363 |
|
364 |
/* Timer info */ |
365 |
static int lastmin = 99; |
366 |
static struct tm nowtm; |
367 |
|
368 |
/* Called once a second. |
369 |
*/ |
370 |
static void core_secondly() |
371 |
{ |
372 |
static int cnt = 0; |
373 |
int miltime; |
374 |
|
375 |
call_hook(HOOK_SECONDLY); /* Will be removed later */ |
376 |
cnt++; |
377 |
if (cnt >= 10) { /* Every 10 seconds */ |
378 |
cnt = 0; |
379 |
check_expired_dcc(); |
380 |
if (con_chan && !backgrd) { |
381 |
dprintf(DP_STDOUT, "\033[2J\033[1;1H"); |
382 |
tell_verbose_status(DP_STDOUT); |
383 |
do_module_report(DP_STDOUT, 0, "server"); |
384 |
do_module_report(DP_STDOUT, 0, "channels"); |
385 |
} |
386 |
} |
387 |
memcpy(&nowtm, localtime(&now), sizeof(struct tm)); |
388 |
if (nowtm.tm_min != lastmin) { |
389 |
int i = 0; |
390 |
|
391 |
/* Once a minute */ |
392 |
lastmin = (lastmin + 1) % 60; |
393 |
call_hook(HOOK_MINUTELY); |
394 |
check_expired_ignores(); |
395 |
autolink_cycle(NULL); /* Attempt autolinks */ |
396 |
/* In case for some reason more than 1 min has passed: */ |
397 |
while (nowtm.tm_min != lastmin) { |
398 |
/* Timer drift, dammit */ |
399 |
debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min); |
400 |
i++; |
401 |
lastmin = (lastmin + 1) % 60; |
402 |
call_hook(HOOK_MINUTELY); |
403 |
} |
404 |
if (i > 1) |
405 |
putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i); |
406 |
miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min); |
407 |
if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */ |
408 |
call_hook(HOOK_5MINUTELY); |
409 |
if (!miltime) { /* At midnight */ |
410 |
char s[25]; |
411 |
|
412 |
strlcpy(s, ctime(&now), sizeof s); |
413 |
putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20); |
414 |
call_hook(HOOK_BACKUP); |
415 |
call_hook(HOOK_DAILY); |
416 |
} |
417 |
} |
418 |
if (nowtm.tm_min == notify_users_at) |
419 |
call_hook(HOOK_HOURLY); |
420 |
} |
421 |
} |
422 |
|
423 |
static void core_minutely() |
424 |
{ |
425 |
check_bind_time(&nowtm); |
426 |
} |
427 |
|
428 |
static void core_hourly() |
429 |
{ |
430 |
write_userfile(-1); |
431 |
} |
432 |
|
433 |
static void event_rehash() |
434 |
{ |
435 |
check_bind_event("rehash"); |
436 |
} |
437 |
|
438 |
static void event_prerehash() |
439 |
{ |
440 |
check_bind_event("prerehash"); |
441 |
} |
442 |
|
443 |
static void event_save() |
444 |
{ |
445 |
check_bind_event("save"); |
446 |
} |
447 |
|
448 |
static void event_logfile() |
449 |
{ |
450 |
check_bind_event("logfile"); |
451 |
} |
452 |
|
453 |
static void event_resettraffic() |
454 |
{ |
455 |
traffic.out_total.irc += traffic.out_today.irc; |
456 |
traffic.out_total.bn += traffic.out_today.bn; |
457 |
traffic.out_total.dcc += traffic.out_today.dcc; |
458 |
traffic.out_total.filesys += traffic.out_today.filesys; |
459 |
traffic.out_total.trans += traffic.out_today.trans; |
460 |
traffic.out_total.unknown += traffic.out_today.unknown; |
461 |
|
462 |
traffic.in_total.irc += traffic.in_today.irc; |
463 |
traffic.in_total.bn += traffic.in_today.bn; |
464 |
traffic.in_total.dcc += traffic.in_today.dcc; |
465 |
traffic.in_total.filesys += traffic.in_today.filesys; |
466 |
traffic.in_total.trans += traffic.in_today.trans; |
467 |
traffic.in_total.unknown += traffic.in_today.unknown; |
468 |
|
469 |
memset(&traffic.out_today, 0, sizeof(traffic.out_today)); |
470 |
memset(&traffic.in_today, 0, sizeof(traffic.in_today)); |
471 |
} |
472 |
|
473 |
static void event_loaded() |
474 |
{ |
475 |
check_bind_event("loaded"); |
476 |
} |
477 |
|
478 |
void kill_tcl(); |
479 |
extern module_entry *module_list; |
480 |
void restart_chons(); |
481 |
|
482 |
int init_userent(), init_net(), |
483 |
init_tcl(int, char **); |
484 |
int user_init(); |
485 |
void botnet_init(); |
486 |
void dns_init(); |
487 |
void binds_init(); |
488 |
void dcc_init(); |
489 |
|
490 |
void patch(const char *str) |
491 |
{ |
492 |
char *p = strchr(egg_version, '+'); |
493 |
|
494 |
if (!p) |
495 |
p = &egg_version[strlen(egg_version)]; |
496 |
sprintf(p, "+%s", str); |
497 |
egg_numver++; |
498 |
sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str); |
499 |
} |
500 |
|
501 |
/* |
502 |
static inline void garbage_collect(void) |
503 |
{ |
504 |
static u_8bit_t run_cnt = 0; |
505 |
|
506 |
if (run_cnt == 3) |
507 |
garbage_collect_tclhash(); |
508 |
else |
509 |
run_cnt++; |
510 |
} |
511 |
*/ |
512 |
|
513 |
int main(int argc, char **argv) |
514 |
{ |
515 |
int xx, i; |
516 |
char buf[520], s[25]; |
517 |
FILE *f; |
518 |
struct sigaction sv; |
519 |
struct chanset_t *chan; |
520 |
egg_timeval_t howlong; |
521 |
|
522 |
#ifdef DEBUG |
523 |
/* Make sure it can write core, if you make debug. Else it's pretty |
524 |
* useless (dw) |
525 |
*/ |
526 |
{ |
527 |
#include <sys/resource.h> |
528 |
struct rlimit cdlim; |
529 |
|
530 |
cdlim.rlim_cur = RLIM_INFINITY; |
531 |
cdlim.rlim_max = RLIM_INFINITY; |
532 |
setrlimit(RLIMIT_CORE, &cdlim); |
533 |
} |
534 |
#endif |
535 |
|
536 |
#ifdef ENABLE_NLS |
537 |
setlocale(LC_MESSAGES, ""); |
538 |
bindtextdomain(PACKAGE, LOCALEDIR); |
539 |
textdomain(PACKAGE); |
540 |
#endif |
541 |
|
542 |
#include "patch.h" |
543 |
/* Version info! */ |
544 |
snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version); |
545 |
snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2002 Eggheads", |
546 |
egg_version); |
547 |
/* Now add on the patchlevel (for Tcl) */ |
548 |
sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver); |
549 |
strcat(egg_version, egg_xtra); |
550 |
#ifdef STOP_UAC |
551 |
{ |
552 |
int nvpair[2]; |
553 |
|
554 |
nvpair[0] = SSIN_UACPROC; |
555 |
nvpair[1] = UAC_NOPRINT; |
556 |
setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0); |
557 |
} |
558 |
#endif |
559 |
|
560 |
/* Set up error traps: */ |
561 |
sv.sa_handler = got_bus; |
562 |
sigemptyset(&sv.sa_mask); |
563 |
#ifdef SA_RESETHAND |
564 |
sv.sa_flags = SA_RESETHAND; |
565 |
#else |
566 |
sv.sa_flags = 0; |
567 |
#endif |
568 |
sigaction(SIGBUS, &sv, NULL); |
569 |
sv.sa_handler = got_segv; |
570 |
sigaction(SIGSEGV, &sv, NULL); |
571 |
#ifdef SA_RESETHAND |
572 |
sv.sa_flags = 0; |
573 |
#endif |
574 |
sv.sa_handler = got_fpe; |
575 |
sigaction(SIGFPE, &sv, NULL); |
576 |
sv.sa_handler = got_term; |
577 |
sigaction(SIGTERM, &sv, NULL); |
578 |
sv.sa_handler = got_hup; |
579 |
sigaction(SIGHUP, &sv, NULL); |
580 |
sv.sa_handler = got_quit; |
581 |
sigaction(SIGQUIT, &sv, NULL); |
582 |
sv.sa_handler = SIG_IGN; |
583 |
sigaction(SIGPIPE, &sv, NULL); |
584 |
sv.sa_handler = got_ill; |
585 |
sigaction(SIGILL, &sv, NULL); |
586 |
sv.sa_handler = got_alarm; |
587 |
sigaction(SIGALRM, &sv, NULL); |
588 |
|
589 |
/* Initialize variables and stuff */ |
590 |
timer_get_time(&egg_timeval_now); |
591 |
now = egg_timeval_now.sec; |
592 |
chanset = NULL; |
593 |
memcpy(&nowtm, localtime(&now), sizeof(struct tm)); |
594 |
lastmin = nowtm.tm_min; |
595 |
srandom(now % (getpid() + getppid())); |
596 |
|
597 |
do_args(argc, argv); |
598 |
|
599 |
printf("\n%s\n", version); |
600 |
printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n"); |
601 |
|
602 |
/* Don't allow eggdrop to run as root */ |
603 |
if (((int) getuid() == 0) || ((int) geteuid() == 0)) |
604 |
fatal(_("ERROR: Eggdrop will not run as root!"), 0); |
605 |
|
606 |
egg = eggdrop_new(); |
607 |
script_init(); |
608 |
binds_init(); |
609 |
modules_init(); |
610 |
logfile_init(); |
611 |
timer_init(); |
612 |
dns_init(); |
613 |
core_binds_init(); |
614 |
dcc_init(); |
615 |
user_init(); |
616 |
init_userent(); |
617 |
botnet_init(); |
618 |
init_net(); |
619 |
|
620 |
if (backgrd) |
621 |
bg_prepare_split(); |
622 |
|
623 |
init_tcl(argc, argv); |
624 |
|
625 |
strlcpy(s, ctime(&now), sizeof s); |
626 |
strcpy(&s[11], &s[20]); |
627 |
putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s); |
628 |
if (preload_module[0]) |
629 |
module_load(preload_module); |
630 |
chanprog(); |
631 |
if (!encrypt_pass) { |
632 |
printf(_("You have installed modules but have not selected an encryption\n\ |
633 |
module, please consult the default config file for info.\n")); |
634 |
bg_send_quit(BG_ABORT); |
635 |
exit(1); |
636 |
} |
637 |
i = 0; |
638 |
for (chan = chanset; chan; chan = chan->next) |
639 |
i++; |
640 |
putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.", |
641 |
botnetnick, i, count_users(userlist)); |
642 |
cache_miss = 0; |
643 |
cache_hit = 0; |
644 |
|
645 |
if (!pid_file[0]) |
646 |
snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick); |
647 |
|
648 |
/* Check for pre-existing eggdrop! */ |
649 |
f = fopen(pid_file, "r"); |
650 |
if (f != NULL) { |
651 |
fgets(s, 10, f); |
652 |
xx = atoi(s); |
653 |
kill(xx, SIGCHLD); /* Meaningless kill to determine if pid |
654 |
is used */ |
655 |
if (errno != ESRCH) { |
656 |
printf(_("I detect %s already running from this directory.\n"), botnetnick); |
657 |
printf(_("If this is incorrect, erase the %s\n"), pid_file); |
658 |
bg_send_quit(BG_ABORT); |
659 |
exit(1); |
660 |
} |
661 |
} |
662 |
|
663 |
/* Move into background? */ |
664 |
if (backgrd) { |
665 |
#ifndef CYGWIN_HACKS |
666 |
bg_do_split(); |
667 |
} else { /* !backgrd */ |
668 |
#endif |
669 |
xx = getpid(); |
670 |
if (xx != 0) { |
671 |
FILE *fp; |
672 |
|
673 |
/* Write pid to file */ |
674 |
unlink(pid_file); |
675 |
fp = fopen(pid_file, "w"); |
676 |
if (fp != NULL) { |
677 |
fprintf(fp, "%u\n", xx); |
678 |
if (fflush(fp)) { |
679 |
/* Let the bot live since this doesn't appear to be a botchk */ |
680 |
printf(_("* Warning! Could not write %s file!\n"), pid_file); |
681 |
fclose(fp); |
682 |
unlink(pid_file); |
683 |
} else |
684 |
fclose(fp); |
685 |
} else |
686 |
printf(_("* Warning! Could not write %s file!\n"), pid_file); |
687 |
#ifdef CYGWIN_HACKS |
688 |
printf("Launched into the background (pid: %d)\n\n", xx); |
689 |
#endif |
690 |
} |
691 |
} |
692 |
|
693 |
use_stderr = 0; /* Stop writing to stderr now */ |
694 |
if (backgrd) { |
695 |
/* Ok, try to disassociate from controlling terminal (finger cross) */ |
696 |
#if HAVE_SETPGID && !defined(CYGWIN_HACKS) |
697 |
setpgid(0, 0); |
698 |
#endif |
699 |
/* Tcl wants the stdin, stdout and stderr file handles kept open. */ |
700 |
freopen("/dev/null", "r", stdin); |
701 |
freopen("/dev/null", "w", stdout); |
702 |
freopen("/dev/null", "w", stderr); |
703 |
#ifdef CYGWIN_HACKS |
704 |
FreeConsole(); |
705 |
#endif |
706 |
} |
707 |
|
708 |
/* Terminal emulating dcc chat */ |
709 |
if (!backgrd && term_z) { |
710 |
int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info)); |
711 |
|
712 |
dcc[n].addr[0] = '\0'; |
713 |
dcc[n].sock = STDOUT; |
714 |
dcc[n].timeval = now; |
715 |
dcc[n].u.chat->con_flags = conmask; |
716 |
dcc[n].u.chat->strip_flags = STRIP_ALL; |
717 |
dcc[n].status = STAT_ECHO; |
718 |
strcpy(dcc[n].nick, "HQ"); |
719 |
strcpy(dcc[n].host, "llama@console"); |
720 |
dcc[n].user = get_user_by_handle(userlist, dcc[n].nick); |
721 |
/* Make sure there's an innocuous HQ user if needed */ |
722 |
if (!dcc[n].user) { |
723 |
userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY); |
724 |
dcc[n].user = get_user_by_handle(userlist, dcc[n].nick); |
725 |
} |
726 |
setsock(STDOUT, 0); /* Entry in net table */ |
727 |
dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n"); |
728 |
dcc_chatter(n); |
729 |
} |
730 |
|
731 |
online_since = now; |
732 |
autolink_cycle(NULL); /* Hurry and connect to tandem bots */ |
733 |
add_help_reference("cmds1.help"); |
734 |
add_help_reference("cmds2.help"); |
735 |
add_help_reference("core.help"); |
736 |
howlong.sec = 1; |
737 |
howlong.usec = 0; |
738 |
timer_create_repeater(&howlong, (Function) core_secondly); |
739 |
add_hook(HOOK_MINUTELY, (Function) core_minutely); |
740 |
add_hook(HOOK_HOURLY, (Function) core_hourly); |
741 |
add_hook(HOOK_REHASH, (Function) event_rehash); |
742 |
add_hook(HOOK_PRE_REHASH, (Function) event_prerehash); |
743 |
add_hook(HOOK_USERFILE, (Function) event_save); |
744 |
add_hook(HOOK_BACKUP, (Function) backup_userfile); |
745 |
add_hook(HOOK_DAILY, (Function) event_logfile); |
746 |
add_hook(HOOK_DAILY, (Function) event_resettraffic); |
747 |
add_hook(HOOK_LOADED, (Function) event_loaded); |
748 |
|
749 |
call_hook(HOOK_LOADED); |
750 |
|
751 |
debug0("main: entering loop"); |
752 |
while (1) { |
753 |
int socket_cleanup = 0; |
754 |
|
755 |
#if !defined(HAVE_PRE7_5_TCL) |
756 |
/* Process a single tcl event */ |
757 |
Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT); |
758 |
#endif |
759 |
|
760 |
/* Lets move some of this here, reducing the numer of actual |
761 |
* calls to periodic_timers |
762 |
*/ |
763 |
timer_get_time(&egg_timeval_now); |
764 |
now = egg_timeval_now.sec; |
765 |
random(); /* Woop, lets really jumble things */ |
766 |
timer_run(); |
767 |
|
768 |
/* Only do this every so often. */ |
769 |
if (!socket_cleanup) { |
770 |
socket_cleanup = 5; |
771 |
|
772 |
/* Remove dead dcc entries. */ |
773 |
dcc_remove_lost(); |
774 |
|
775 |
/* Check for server or dcc activity. */ |
776 |
dequeue_sockets(); |
777 |
} else |
778 |
socket_cleanup--; |
779 |
|
780 |
/* Free unused structures. */ |
781 |
/* garbage_collect(); */ |
782 |
|
783 |
xx = sockgets(buf, &i); |
784 |
timer_get_time(&egg_timeval_now); |
785 |
if (xx >= 0) { /* Non-error */ |
786 |
int idx; |
787 |
|
788 |
for (idx = 0; idx < dcc_total; idx++) |
789 |
if (dcc[idx].sock == xx) { |
790 |
if (dcc[idx].type && dcc[idx].type->activity) { |
791 |
/* Traffic stats */ |
792 |
if (dcc[idx].type->name) { |
793 |
if (!strncmp(dcc[idx].type->name, "BOT", 3)) |
794 |
traffic.in_today.bn += strlen(buf) + 1; |
795 |
else if (!strcmp(dcc[idx].type->name, "SERVER")) |
796 |
traffic.in_today.irc += strlen(buf) + 1; |
797 |
else if (!strncmp(dcc[idx].type->name, "CHAT", 4)) |
798 |
traffic.in_today.dcc += strlen(buf) + 1; |
799 |
else if (!strncmp(dcc[idx].type->name, "FILES", 5)) |
800 |
traffic.in_today.dcc += strlen(buf) + 1; |
801 |
else if (!strcmp(dcc[idx].type->name, "SEND")) |
802 |
traffic.in_today.trans += strlen(buf) + 1; |
803 |
else if (!strncmp(dcc[idx].type->name, "GET", 3)) |
804 |
traffic.in_today.trans += strlen(buf) + 1; |
805 |
else |
806 |
traffic.in_today.unknown += strlen(buf) + 1; |
807 |
} |
808 |
dcc[idx].type->activity(idx, buf, i); |
809 |
} else |
810 |
putlog(LOG_MISC, "*", |
811 |
"!!! untrapped dcc activity: type %s, sock %d", |
812 |
dcc[idx].type->name, dcc[idx].sock); |
813 |
break; |
814 |
} |
815 |
} else if (xx == -1) { /* EOF from someone */ |
816 |
int idx; |
817 |
|
818 |
if (i == STDOUT && !backgrd) |
819 |
fatal("END OF FILE ON TERMINAL", 0); |
820 |
for (idx = 0; idx < dcc_total; idx++) |
821 |
if (dcc[idx].sock == i) { |
822 |
if (dcc[idx].type && dcc[idx].type->eof) |
823 |
dcc[idx].type->eof(idx); |
824 |
else { |
825 |
putlog(LOG_MISC, "*", |
826 |
"*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED", |
827 |
i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*"); |
828 |
killsock(i); |
829 |
lostdcc(idx); |
830 |
} |
831 |
idx = dcc_total + 1; |
832 |
} |
833 |
if (idx == dcc_total) { |
834 |
putlog(LOG_MISC, "*", |
835 |
"(@) EOF socket %d, not a dcc socket, not anything.", i); |
836 |
close(i); |
837 |
killsock(i); |
838 |
} |
839 |
} else if (xx == -2 && errno != EINTR) { /* select() error */ |
840 |
putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno); |
841 |
for (i = 0; i < dcc_total; i++) { |
842 |
if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) { |
843 |
putlog(LOG_MISC, "*", |
844 |
"DCC socket %d (type %d, name '%s') expired -- pfft", |
845 |
dcc[i].sock, dcc[i].type, dcc[i].nick); |
846 |
killsock(dcc[i].sock); |
847 |
lostdcc(i); |
848 |
i--; |
849 |
} |
850 |
} |
851 |
} else if (xx == -3) { |
852 |
call_hook(HOOK_IDLE); |
853 |
socket_cleanup = 0; /* If we've been idle, cleanup & flush */ |
854 |
} |
855 |
|
856 |
if (do_restart) { |
857 |
if (do_restart == -2) |
858 |
rehash(); |
859 |
else { |
860 |
/* Unload as many modules as possible */ |
861 |
int f = 1; |
862 |
module_entry *p; |
863 |
Function x; |
864 |
char xx[256]; |
865 |
|
866 |
/* oops, I guess we should call this event before tcl is restarted */ |
867 |
check_bind_event("prerestart"); |
868 |
|
869 |
while (f) { |
870 |
f = 0; |
871 |
for (p = module_list; p != NULL; p = p->next) { |
872 |
dependancy *d = dependancy_list; |
873 |
int ok = 1; |
874 |
|
875 |
while (ok && d) { |
876 |
if (d->needed == p) |
877 |
ok = 0; |
878 |
d = d->next; |
879 |
} |
880 |
if (ok) { |
881 |
strcpy(xx, p->name); |
882 |
if (module_unload(xx, botnetnick) == NULL) { |
883 |
f = 1; |
884 |
break; |
885 |
} |
886 |
} |
887 |
} |
888 |
} |
889 |
p = module_list; |
890 |
if (p && p->next && p->next->next) |
891 |
/* Should be only 2 modules now - blowfish (or some other |
892 |
encryption module) and eggdrop. */ |
893 |
putlog(LOG_MISC, "*", _("Stagnant module; there WILL be memory leaks!")); |
894 |
flushlogs(); |
895 |
kill_tcl(); |
896 |
timer_destroy_all(); /* Destroy all timers. */ |
897 |
init_tcl(argc, argv); |
898 |
/* We expect the encryption module as the current module pointed |
899 |
* to by `module_list'. |
900 |
*/ |
901 |
x = p->funcs[MODCALL_START]; |
902 |
/* `NULL' indicates that we just recently restarted. The module |
903 |
* is expected to re-initialise as needed. |
904 |
*/ |
905 |
x(NULL); |
906 |
rehash(); |
907 |
restart_chons(); |
908 |
call_hook(HOOK_LOADED); |
909 |
} |
910 |
do_restart = 0; |
911 |
} |
912 |
} |
913 |
} |