/[cvs]/eggdrop1.9/src/main.c
ViewVC logotype

Contents of /eggdrop1.9/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.79 - (show annotations) (download) (as text)
Wed Oct 10 10:44:04 2001 UTC (17 years, 10 months ago) by tothwolf
Branch: MAIN
Changes since 1.78: +7 -26 lines
File MIME type: text/x-chdr
all expmem functions and support removed
all memory status/debugging code removed
all #ifdef DEBUG_MEM code removed

nmalloc macro/n_malloc function removed, use malloc() now
mod_malloc function removed
my_strdup function removed, use malloc_strcpy macro now
channel_malloc removed, use malloc_memset macro now
n_malloc_null function removed, use malloc_memset macro now
user_malloc macro/_user_malloc function removed, use malloc() now

nrealloc macro/n_realloc function removed, use realloc() now
mod_realloc function removed

nfree macro/n_free function removed, use free() now
mod_free function removed
my_free macro removed, use free_null macro now
Tcl_Free compat macro now uses free() instead of removed n_free function

malloc->strcpy now uses malloc_strcpy macro
free(ptr);ptr=NULL; now uses free_null macro
malloc->bzero now uses malloc_memset macro
get_data_ptr macro/_get_data_ptr function removed, use malloc_memset macro now

compat memcpy removed
compat memset removed
egg_memcpy/my_memcpy misuse/mess changed to memcpy
egg_bzero/bzero macro removed, use memset() now

many uninitialized pointers now default to NULL

match.c is now compiled on it's own instead of being included into tclhash.c
match.c cleanup, removed broken/incomplete ircII compat code
wild_match macro removed, _wild_match function renamed to wild_match
wild_match_per function no longer declared static
added prototypes for wild_match and wild_match_per functions

Major function/macro changes:
  nmalloc -> malloc()
  nrealloc -> realloc()
  nfree -> free()

Bugs found and fixed during audit:
  buffer overflow in adduser()

Bugs fixed when code was removed:
  typo in filedb3.c: EBUG_MEM -> DEBUG_MEM
  extra egg_bzero call from filesys.c

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23