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

Annotation of /eggdrop1.8/src/main.c

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


Revision 1.4 - (hide annotations) (download) (as text)
Tue Oct 19 12:13:33 2010 UTC (8 years, 8 months ago) by pseudo
Branch: MAIN
Changes since 1.3: +25 -2 lines
File MIME type: text/x-chdr
Added full SSL support including Tcl commands.
Added support for certificate authentication.
Added support for botnet and partyline encryption using ssl.
Documented the new features and commands.
Fixed add_server() problems with IPv6 addresses in the server list.

1 simple 1.1 /*
2     * main.c -- handles:
3     * core event handling
4     * signal handling
5     * command line arguments
6     * context and assert debugging
7     *
8 pseudo 1.4 * $Id: main.c,v 1.3 2010/10/10 21:24:43 pseudo Exp $
9 simple 1.1 */
10     /*
11     * Copyright (C) 1997 Robey Pointer
12     * Copyright (C) 1999 - 2010 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    
36     #include <fcntl.h>
37     #include <errno.h>
38     #include <signal.h>
39     #include <netdb.h>
40     #include <setjmp.h>
41    
42     #ifdef TIME_WITH_SYS_TIME
43     # include <sys/time.h>
44     # include <time.h>
45     #else
46     # ifdef HAVE_SYS_TIME_H
47     # include <sys/time.h>
48     # else
49     # include <time.h>
50     # endif
51     #endif
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    
58     #include "chan.h"
59     #include "modules.h"
60     #include "tandem.h"
61     #include "bg.h"
62    
63     #ifdef DEBUG /* For debug compile */
64     # include <sys/resource.h> /* setrlimit() */
65     #endif
66    
67     #ifdef CYGWIN_HACKS
68     # include <windows.h>
69     #endif
70    
71     #ifndef _POSIX_SOURCE
72     # define _POSIX_SOURCE 1 /* Solaris needs this */
73     #endif
74    
75     extern char origbotname[], userfile[], botnetnick[];
76     extern int dcc_total, conmask, cache_hit, cache_miss, max_logs, quick_logs,
77     quiet_save;
78     extern struct dcc_t *dcc;
79     extern struct userrec *userlist;
80     extern struct chanset_t *chanset;
81     extern log_t *logs;
82     extern Tcl_Interp *interp;
83     extern tcl_timer_t *timer, *utimer;
84     extern sigjmp_buf alarmret;
85     time_t now;
86     static int argc;
87     static char **argv;
88    
89     /*
90     * Please use the PATCH macro instead of directly altering the version
91     * string from now on (it makes it much easier to maintain patches).
92     * Also please read the README file regarding your rights to distribute
93     * modified versions of this bot.
94     */
95    
96     char egg_version[1024] = "1.8.0";
97     int egg_numver = 1080000;
98    
99     char notify_new[121] = ""; /* Person to send a note to for new users */
100     int default_flags = 0; /* Default user flags */
101     int default_uflags = 0; /* Default user-definied flags */
102    
103     int backgrd = 1; /* Run in the background? */
104     int con_chan = 0; /* Foreground: constantly display channel stats? */
105     int term_z = 0; /* Foreground: use the terminal as a partyline? */
106     int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
107    
108     char configfile[121] = "eggdrop.conf"; /* Default config file name */
109     char pid_file[120]; /* Name of the pid file */
110     char helpdir[121] = "help/"; /* Directory of help files */
111     char textdir[121] = "text/"; /* Directory for text files */
112    
113     int keep_all_logs = 0; /* Never erase logfiles? */
114     char logfile_suffix[21] = ".%d%b%Y"; /* Format of logfile suffix */
115     int switch_logfiles_at = 300; /* When to switch logfiles */
116    
117     time_t online_since; /* time that the bot was started */
118    
119     int make_userfile = 0; /* Using bot in userfile-creation mode? */
120     char owner[121] = ""; /* Permanent owner(s) of the bot */
121    
122     int save_users_at = 0; /* Minutes past the hour to save the userfile? */
123     int notify_users_at = 0; /* Minutes past the hour to notify users of notes? */
124    
125     char version[81]; /* Version info (long form) */
126     char ver[41]; /* Version info (short form) */
127     char egg_xtra[2048]; /* Patch info */
128    
129     int do_restart = 0; /* .restart has been called, restart ASAP */
130     int die_on_sighup = 0; /* Die if bot receives SIGHUP */
131     int die_on_sigterm = 1; /* Die if bot receives SIGTERM */
132     int resolve_timeout = 15; /* Hostname/address lookup timeout */
133     char quit_msg[1024]; /* Quit message */
134    
135     /* Traffic stats */
136     unsigned long otraffic_irc = 0;
137     unsigned long otraffic_irc_today = 0;
138     unsigned long otraffic_bn = 0;
139     unsigned long otraffic_bn_today = 0;
140     unsigned long otraffic_dcc = 0;
141     unsigned long otraffic_dcc_today = 0;
142     unsigned long otraffic_filesys = 0;
143     unsigned long otraffic_filesys_today = 0;
144     unsigned long otraffic_trans = 0;
145     unsigned long otraffic_trans_today = 0;
146     unsigned long otraffic_unknown = 0;
147     unsigned long otraffic_unknown_today = 0;
148     unsigned long itraffic_irc = 0;
149     unsigned long itraffic_irc_today = 0;
150     unsigned long itraffic_bn = 0;
151     unsigned long itraffic_bn_today = 0;
152     unsigned long itraffic_dcc = 0;
153     unsigned long itraffic_dcc_today = 0;
154     unsigned long itraffic_trans = 0;
155     unsigned long itraffic_trans_today = 0;
156     unsigned long itraffic_unknown = 0;
157     unsigned long itraffic_unknown_today = 0;
158    
159     #ifdef DEBUG_CONTEXT
160     /* Context storage for fatal crashes */
161     char cx_file[16][30];
162     char cx_note[16][256];
163     int cx_line[16];
164     int cx_ptr = 0;
165     #endif
166    
167 pseudo 1.4 #ifdef TLS
168     int ssl_cleanup();
169     #endif
170 simple 1.1
171     void fatal(const char *s, int recoverable)
172     {
173     int i;
174    
175     putlog(LOG_MISC, "*", "* %s", s);
176     flushlogs();
177     for (i = 0; i < dcc_total; i++)
178     if (dcc[i].sock >= 0)
179     killsock(dcc[i].sock);
180 pseudo 1.4 #ifdef TLS
181     ssl_cleanup();
182     #endif
183 simple 1.1 unlink(pid_file);
184     if (!recoverable) {
185     bg_send_quit(BG_ABORT);
186     exit(1);
187     }
188     }
189    
190     int expmem_chanprog();
191     int expmem_users();
192     int expmem_misc();
193     int expmem_dccutil();
194     int expmem_botnet();
195     int expmem_tcl();
196     int expmem_tclhash();
197     int expmem_net();
198     int expmem_modules(int);
199     int expmem_language();
200     int expmem_tcldcc();
201     int expmem_tclmisc();
202 pseudo 1.4 int expmem_dns();
203     #ifdef TLS
204     int expmem_tls();
205     #endif
206 simple 1.1
207     /* For mem.c : calculate memory we SHOULD be using
208     */
209     int expected_memory(void)
210     {
211     int tot;
212    
213     tot = expmem_chanprog() + expmem_users() + expmem_misc() + expmem_dccutil() +
214     expmem_botnet() + expmem_tcl() + expmem_tclhash() + expmem_net() +
215     expmem_modules(0) + expmem_language() + expmem_tcldcc() +
216 pseudo 1.4 expmem_tclmisc() + expmem_dns();
217     #ifdef TLS
218     tot += expmem_tls();
219     #endif
220 simple 1.1 return tot;
221     }
222    
223     static void check_expired_dcc()
224     {
225     int i;
226    
227     for (i = 0; i < dcc_total; i++)
228     if (dcc[i].type && dcc[i].type->timeout_val &&
229     ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
230     if (dcc[i].type->timeout)
231     dcc[i].type->timeout(i);
232     else if (dcc[i].type->eof)
233     dcc[i].type->eof(i);
234     else
235     continue;
236     /* Only timeout 1 socket per cycle, too risky for more */
237     return;
238     }
239     }
240    
241     #ifndef DEBUG_CONTEXT
242     #define write_debug() do {} while (0)
243     #else
244     static int nested_debug = 0;
245    
246     static void write_debug()
247     {
248     int x;
249     char s[25];
250     int y;
251    
252     if (nested_debug) {
253     /* Yoicks, if we have this there's serious trouble!
254     * All of these are pretty reliable, so we'll try these.
255     *
256     * NOTE: dont try and display context-notes in here, it's
257     * _not_ safe <cybah>
258     */
259     x = creat("DEBUG.DEBUG", 0644);
260     setsock(x, SOCK_NONSOCK);
261     if (x >= 0) {
262     strncpyz(s, ctime(&now), sizeof s);
263     dprintf(-x, "Debug (%s) written %s\n", ver, s);
264     dprintf(-x, "Please report problem to bugs@eggheads.org\n");
265     dprintf(-x, "after a visit to http://www.eggheads.org/bugzilla/\n");
266     dprintf(-x, "Full Patch List: %s\n", egg_xtra);
267     dprintf(-x, "Context: ");
268     cx_ptr = cx_ptr & 15;
269     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
270     dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
271     dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
272     killsock(x);
273     close(x);
274     }
275     bg_send_quit(BG_ABORT);
276     exit(1); /* Dont even try & tell people about, that may
277     * have caused the fault last time. */
278     } else
279     nested_debug = 1;
280     putlog(LOG_MISC, "*", "* Last context: %s/%d [%s]", cx_file[cx_ptr],
281     cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
282     putlog(LOG_MISC, "*", "* Please REPORT this BUG!");
283     putlog(LOG_MISC, "*", "* Check doc/BUG-REPORT on how to do so.");
284     x = creat("DEBUG", 0644);
285     setsock(x, SOCK_NONSOCK);
286     if (x < 0) {
287     putlog(LOG_MISC, "*", "* Failed to write DEBUG");
288     } else {
289     strncpyz(s, ctime(&now), sizeof s);
290     dprintf(-x, "Debug (%s) written %s\n", ver, s);
291     dprintf(-x, "Full Patch List: %s\n", egg_xtra);
292     #ifdef STATIC
293     dprintf(-x, "STATICALLY LINKED\n");
294     #endif
295    
296     /* info library */
297     dprintf(-x, "Tcl library: %s\n",
298     ((interp) && (Tcl_Eval(interp, "info library") == TCL_OK)) ?
299     tcl_resultstring() : "*unknown*");
300    
301     /* info tclversion/patchlevel */
302     dprintf(-x, "Tcl version: %s (header version %s)\n",
303     ((interp) && (Tcl_Eval(interp, "info patchlevel") == TCL_OK)) ?
304     tcl_resultstring() : (Tcl_Eval(interp, "info tclversion") == TCL_OK) ?
305     tcl_resultstring() : "*unknown*", TCL_PATCH_LEVEL ? TCL_PATCH_LEVEL :
306     "*unknown*");
307    
308     if (tcl_threaded())
309     dprintf(-x, "Tcl is threaded\n");
310 pseudo 1.3 #ifdef IPV6
311     dprintf(-x, "Compiled with IPv6 support\n");
312     #else
313     dprintf(-x, "Compiled without IPv6 support\n");
314     #endif
315 simple 1.1
316 pseudo 1.3 dprintf(-x, "Configure flags: %s\n", EGG_AC_ARGS);
317 simple 1.1 #ifdef CCFLAGS
318     dprintf(-x, "Compile flags: %s\n", CCFLAGS);
319     #endif
320    
321     #ifdef LDFLAGS
322     dprintf(-x, "Link flags: %s\n", LDFLAGS);
323     #endif
324    
325     #ifdef STRIPFLAGS
326     dprintf(-x, "Strip flags: %s\n", STRIPFLAGS);
327     #endif
328    
329     dprintf(-x, "Context: ");
330     cx_ptr = cx_ptr & 15;
331     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
332     dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
333     (cx_note[y][0]) ? cx_note[y] : "");
334     dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
335     (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
336     tell_dcc(-x);
337     dprintf(-x, "\n");
338     debug_mem_to_dcc(-x);
339     killsock(x);
340     close(x);
341     putlog(LOG_MISC, "*", "* Wrote DEBUG");
342     }
343     }
344     #endif /* DEBUG_CONTEXT */
345    
346     static void got_bus(int z)
347     {
348     write_debug();
349     fatal("BUS ERROR -- CRASHING!", 1);
350     #ifdef SA_RESETHAND
351     kill(getpid(), SIGBUS);
352     #else
353     bg_send_quit(BG_ABORT);
354     exit(1);
355     #endif
356     }
357    
358     static void got_segv(int z)
359     {
360     write_debug();
361     fatal("SEGMENT VIOLATION -- CRASHING!", 1);
362     #ifdef SA_RESETHAND
363     kill(getpid(), SIGSEGV);
364     #else
365     bg_send_quit(BG_ABORT);
366     exit(1);
367     #endif
368     }
369    
370     static void got_fpe(int z)
371     {
372     write_debug();
373     fatal("FLOATING POINT ERROR -- CRASHING!", 0);
374     }
375    
376     static void got_term(int z)
377     {
378     write_userfile(-1);
379     check_tcl_event("sigterm");
380     if (die_on_sigterm) {
381     botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
382     fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
383     } else
384     putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
385     }
386    
387     static void got_quit(int z)
388     {
389     check_tcl_event("sigquit");
390     putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
391     return;
392     }
393    
394     static void got_hup(int z)
395     {
396     write_userfile(-1);
397     check_tcl_event("sighup");
398     if (die_on_sighup) {
399     fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
400     } else
401     putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
402     do_restart = -2;
403     return;
404     }
405    
406     /* A call to resolver (gethostbyname, etc) timed out
407     */
408     static void got_alarm(int z)
409     {
410     siglongjmp(alarmret, 1);
411    
412     /* -Never reached- */
413     }
414    
415     /* Got ILL signal -- log context and continue
416     */
417     static void got_ill(int z)
418     {
419     check_tcl_event("sigill");
420     #ifdef DEBUG_CONTEXT
421     putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
422     cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
423     #endif
424     }
425    
426     #ifdef DEBUG_CONTEXT
427     /* Called from the Context macro.
428     */
429     void eggContext(const char *file, int line, const char *module)
430     {
431     char x[31], *p;
432    
433     p = strrchr(file, '/');
434     if (!module) {
435     strncpyz(x, p ? p + 1 : file, sizeof x);
436     } else
437     egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
438     cx_ptr = ((cx_ptr + 1) & 15);
439     strcpy(cx_file[cx_ptr], x);
440     cx_line[cx_ptr] = line;
441     cx_note[cx_ptr][0] = 0;
442     }
443    
444     /* Called from the ContextNote macro.
445     */
446     void eggContextNote(const char *file, int line, const char *module,
447     const char *note)
448     {
449     char x[31], *p;
450    
451     p = strrchr(file, '/');
452     if (!module)
453     strncpyz(x, p ? p + 1 : file, sizeof x);
454     else
455     egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
456     cx_ptr = ((cx_ptr + 1) & 15);
457     strcpy(cx_file[cx_ptr], x);
458     cx_line[cx_ptr] = line;
459     strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
460     }
461     #endif /* DEBUG_CONTEXT */
462    
463     #ifdef DEBUG_ASSERT
464     /* Called from the Assert macro.
465     */
466     void eggAssert(const char *file, int line, const char *module)
467     {
468     write_debug();
469     if (!module)
470     putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
471     else
472     putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
473     fatal("ASSERT FAILED -- CRASHING!", 1);
474     }
475     #endif
476    
477     static void do_arg(char *s)
478     {
479     char x[512], *z = x;
480     int i;
481    
482     if (s[0] == '-')
483     for (i = 1; i < strlen(s); i++) {
484     switch (s[i]) {
485     case 'n':
486     backgrd = 0;
487     break;
488     case 'c':
489     con_chan = 1;
490     term_z = 0;
491     break;
492     case 't':
493     con_chan = 0;
494     term_z = 1;
495     break;
496     case 'm':
497     make_userfile = 1;
498     break;
499     case 'v':
500     strncpyz(x, egg_version, sizeof x);
501     newsplit(&z);
502     newsplit(&z);
503     printf("%s\n", version);
504     if (z[0])
505     printf(" (patches: %s)\n", z);
506 pseudo 1.3 printf("Configured with: " EGG_AC_ARGS "\n");
507     printf("Compiled with: ");
508     #ifdef IPV6
509     printf("IPv6, ");
510     #endif
511     printf("handlen=%d\n", HANDLEN);
512 simple 1.1 bg_send_quit(BG_ABORT);
513     exit(0);
514     break; /* this should never be reached */
515     case 'h':
516     printf("\n%s\n\n", version);
517     printf(EGG_USAGE);
518     printf("\n");
519     bg_send_quit(BG_ABORT);
520     exit(0);
521     break; /* this should never be reached */
522     }
523     } else
524     strncpyz(configfile, s, sizeof configfile);
525     }
526    
527     void backup_userfile(void)
528     {
529     char s[125];
530    
531     if (quiet_save < 2)
532     putlog(LOG_MISC, "*", USERF_BACKUP);
533     egg_snprintf(s, sizeof s, "%s~bak", userfile);
534     copyfile(userfile, s);
535     }
536    
537     /* Timer info */
538     static int lastmin = 99;
539     static time_t then;
540     static struct tm nowtm;
541    
542     /* Called once a second.
543     *
544     * Note: Try to not put any Context lines in here (guppy 21Mar2000).
545     */
546     static void core_secondly()
547     {
548     static int cnt = 0;
549     int miltime;
550    
551     do_check_timers(&utimer); /* Secondly timers */
552     cnt++;
553     if (cnt >= 10) { /* Every 10 seconds */
554     cnt = 0;
555     check_expired_dcc();
556     if (con_chan && !backgrd) {
557     dprintf(DP_STDOUT, "\033[2J\033[1;1H");
558     tell_verbose_status(DP_STDOUT);
559     do_module_report(DP_STDOUT, 0, "server");
560     do_module_report(DP_STDOUT, 0, "channels");
561     tell_mem_status_dcc(DP_STDOUT);
562     }
563     }
564     egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
565     if (nowtm.tm_min != lastmin) {
566     int i = 0;
567    
568     /* Once a minute */
569     lastmin = (lastmin + 1) % 60;
570     call_hook(HOOK_MINUTELY);
571     check_expired_ignores();
572     autolink_cycle(NULL); /* Attempt autolinks */
573     /* In case for some reason more than 1 min has passed: */
574     while (nowtm.tm_min != lastmin) {
575     /* Timer drift, dammit */
576     debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
577     i++;
578     lastmin = (lastmin + 1) % 60;
579     call_hook(HOOK_MINUTELY);
580     }
581     if (i > 1)
582     putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
583     miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
584     if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
585     call_hook(HOOK_5MINUTELY);
586     check_botnet_pings();
587     if (!quick_logs) {
588     flushlogs();
589     check_logsize();
590     }
591     if (!miltime) { /* At midnight */
592     char s[25];
593     int j;
594    
595     strncpyz(s, ctime(&now), sizeof s);
596     if (quiet_save < 3)
597     putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
598     call_hook(HOOK_BACKUP);
599     for (j = 0; j < max_logs; j++) {
600     if (logs[j].filename != NULL && logs[j].f != NULL) {
601     fclose(logs[j].f);
602     logs[j].f = NULL;
603     }
604     }
605     }
606     }
607     if (nowtm.tm_min == notify_users_at)
608     call_hook(HOOK_HOURLY);
609     /* These no longer need checking since they are all check vs minutely
610     * settings and we only get this far on the minute.
611     */
612     if (miltime == switch_logfiles_at) {
613     call_hook(HOOK_DAILY);
614     if (!keep_all_logs) {
615     if (quiet_save < 3)
616     putlog(LOG_MISC, "*", MISC_LOGSWITCH);
617     for (i = 0; i < max_logs; i++)
618     if (logs[i].filename) {
619     char s[1024];
620    
621     if (logs[i].f) {
622     fclose(logs[i].f);
623     logs[i].f = NULL;
624     }
625     egg_snprintf(s, sizeof s, "%s.yesterday", logs[i].filename);
626     unlink(s);
627     movefile(logs[i].filename, s);
628     }
629     }
630     }
631     }
632     }
633    
634     static void core_minutely()
635     {
636     check_tcl_time(&nowtm);
637     check_tcl_cron(&nowtm);
638     do_check_timers(&timer);
639     if (quick_logs != 0) {
640     flushlogs();
641     check_logsize();
642     }
643     }
644    
645     static void core_hourly()
646     {
647     write_userfile(-1);
648     }
649    
650     static void event_rehash()
651     {
652     check_tcl_event("rehash");
653     }
654    
655     static void event_prerehash()
656     {
657     check_tcl_event("prerehash");
658     }
659    
660     static void event_save()
661     {
662     check_tcl_event("save");
663     }
664    
665     static void event_logfile()
666     {
667     check_tcl_event("logfile");
668     }
669    
670     static void event_resettraffic()
671     {
672     otraffic_irc += otraffic_irc_today;
673     itraffic_irc += itraffic_irc_today;
674     otraffic_bn += otraffic_bn_today;
675     itraffic_bn += itraffic_bn_today;
676     otraffic_dcc += otraffic_dcc_today;
677     itraffic_dcc += itraffic_dcc_today;
678     otraffic_unknown += otraffic_unknown_today;
679     itraffic_unknown += itraffic_unknown_today;
680     otraffic_trans += otraffic_trans_today;
681     itraffic_trans += itraffic_trans_today;
682     otraffic_irc_today = otraffic_bn_today = 0;
683     otraffic_dcc_today = otraffic_unknown_today = 0;
684     itraffic_irc_today = itraffic_bn_today = 0;
685     itraffic_dcc_today = itraffic_unknown_today = 0;
686     itraffic_trans_today = otraffic_trans_today = 0;
687     }
688    
689     static void event_loaded()
690     {
691     check_tcl_event("loaded");
692     }
693    
694     void kill_tcl();
695     extern module_entry *module_list;
696     void restart_chons();
697    
698     #ifdef STATIC
699     void check_static(char *, char *(*)());
700    
701     #include "mod/static.h"
702     #endif
703     int init_threaddata(int);
704     int init_mem();
705     int init_userent();
706     int init_misc();
707     int init_bots();
708     int init_modules();
709     int init_tcl(int, char **);
710     int init_language(int);
711 pseudo 1.4 #ifdef TLS
712     int ssl_init();
713     #endif
714 simple 1.1
715     static void patch(const char *str)
716     {
717     char *p = strchr(egg_version, '+');
718    
719     if (!p)
720     p = &egg_version[strlen(egg_version)];
721     sprintf(p, "+%s", str);
722     egg_numver++;
723     sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
724     }
725    
726     static inline void garbage_collect(void)
727     {
728     static u_8bit_t run_cnt = 0;
729    
730     if (run_cnt == 3)
731     garbage_collect_tclhash();
732     else
733     run_cnt++;
734     }
735    
736     int mainloop(int toplevel)
737     {
738     static int socket_cleanup = 0;
739     int xx, i, eggbusy = 1, tclbusy = 0;
740     char buf[520];
741    
742     /* Lets move some of this here, reducing the numer of actual
743     * calls to periodic_timers
744     */
745     now = time(NULL);
746     /*
747     * FIXME: Get rid of this, it's ugly and wastes lots of cpu.
748     *
749     * pre-1.3.0 Eggdrop had random() in the once a second block below.
750     *
751     * This attempts to keep random() more random by constantly
752     * calling random() and updating the state information.
753     */
754     random(); /* Woop, lets really jumble things */
755    
756     /* If we want to restart, we have to unwind to the toplevel.
757     * Tcl will Panic if we kill the interp with Tcl_Eval in progress.
758     * This is done by returning -1 in tickle_WaitForEvent.
759     */
760     if (do_restart && do_restart != -2 && !toplevel)
761     return -1;
762    
763     /* Once a second */
764     if (now != then) {
765     call_hook(HOOK_SECONDLY);
766     then = now;
767     }
768    
769     /* Only do this every so often. */
770     if (!socket_cleanup) {
771     socket_cleanup = 5;
772    
773     /* Remove dead dcc entries. */
774     dcc_remove_lost();
775    
776     /* Check for server or dcc activity. */
777     dequeue_sockets();
778     } else
779     socket_cleanup--;
780    
781     /* Free unused structures. */
782     garbage_collect();
783    
784     xx = sockgets(buf, &i);
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     itraffic_bn_today += strlen(buf) + 1;
795     else if (!strcmp(dcc[idx].type->name, "SERVER"))
796     itraffic_irc_today += strlen(buf) + 1;
797     else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
798     itraffic_dcc_today += strlen(buf) + 1;
799     else if (!strncmp(dcc[idx].type->name, "FILES", 5))
800     itraffic_dcc_today += strlen(buf) + 1;
801     else if (!strcmp(dcc[idx].type->name, "SEND"))
802     itraffic_trans_today += strlen(buf) + 1;
803     else if (!strncmp(dcc[idx].type->name, "GET", 3))
804     itraffic_trans_today += strlen(buf) + 1;
805     else
806     itraffic_unknown_today += 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     eggbusy = 0;
855     } else if (xx == -5) {
856     eggbusy = 0;
857     tclbusy = 1;
858     }
859    
860     if (do_restart) {
861     if (do_restart == -2)
862     rehash();
863     else if (!toplevel)
864     return -1; /* Unwind to toplevel before restarting */
865     else {
866     /* Unload as many modules as possible */
867     int f = 1;
868     module_entry *p;
869     Function startfunc;
870     char name[256];
871    
872     /* oops, I guess we should call this event before tcl is restarted */
873     check_tcl_event("prerestart");
874    
875     while (f) {
876     f = 0;
877     for (p = module_list; p != NULL; p = p->next) {
878     dependancy *d = dependancy_list;
879     int ok = 1;
880    
881     while (ok && d) {
882     if (d->needed == p)
883     ok = 0;
884     d = d->next;
885     }
886     if (ok) {
887     strcpy(name, p->name);
888     if (module_unload(name, botnetnick) == NULL) {
889     f = 1;
890     break;
891     }
892     }
893     }
894     }
895    
896     /* Make sure we don't have any modules left hanging around other than
897     * "eggdrop" and the two that are supposed to be.
898     */
899     for (f = 0, p = module_list; p; p = p->next) {
900     if (strcmp(p->name, "eggdrop") && strcmp(p->name, "encryption") &&
901     strcmp(p->name, "uptime")) {
902     f++;
903     }
904     }
905     if (f != 0) {
906     putlog(LOG_MISC, "*", MOD_STAGNANT);
907     }
908    
909     flushlogs();
910     kill_tcl();
911     init_tcl(argc, argv);
912     init_language(0);
913    
914     /* this resets our modules which we didn't unload (encryption and uptime) */
915     for (p = module_list; p; p = p->next) {
916     if (p->funcs) {
917     startfunc = p->funcs[MODCALL_START];
918     startfunc(NULL);
919     }
920     }
921    
922     rehash();
923 pseudo 1.4 #ifdef TLS
924     ssl_cleanup();
925     ssl_init();
926     #endif
927 simple 1.1 restart_chons();
928     call_hook(HOOK_LOADED);
929     }
930     eggbusy = 1;
931     do_restart = 0;
932     }
933    
934     #ifdef USE_TCL_EVENTS
935     if (!eggbusy) {
936     /* Process all pending tcl events */
937     # ifdef REPLACE_NOTIFIER
938     if (Tcl_ServiceAll())
939     tclbusy = 1;
940     # else
941     while (Tcl_DoOneEvent(TCL_DONT_WAIT | TCL_ALL_EVENTS))
942     tclbusy = 1;
943     # endif /* REPLACE_NOTIFIER */
944     #endif /* USE_TCL_EVENTS */
945     }
946    
947     return (eggbusy || tclbusy);
948     }
949    
950     int main(int arg_c, char **arg_v)
951     {
952     int i, xx;
953     char s[25];
954     FILE *f;
955     struct sigaction sv;
956     struct chanset_t *chan;
957     #ifdef DEBUG
958     struct rlimit cdlim;
959     #endif
960     #ifdef STOP_UAC
961     int nvpair[2];
962     #endif
963    
964     /* Make sure it can write core, if you make debug. Else it's pretty
965     * useless (dw)
966     *
967     * Only allow unlimited size core files when compiled with DEBUG defined.
968     * This is not a good idea for normal builds -- in these cases, use the
969     * default system resource limits instead.
970     */
971     #ifdef DEBUG
972     cdlim.rlim_cur = RLIM_INFINITY;
973     cdlim.rlim_max = RLIM_INFINITY;
974     setrlimit(RLIMIT_CORE, &cdlim);
975     #endif
976    
977     #ifdef DEBUG_CONTEXT
978     /* Initialise context list */
979     for (i = 0; i < 16; i++)
980     Context;
981     #endif
982    
983     /* Include patch.h header for patch("...") */
984     #include "patch.h"
985    
986     argc = arg_c;
987     argv = arg_v;
988    
989     /* Version info! */
990     egg_snprintf(ver, sizeof ver, "eggdrop v%s", egg_version);
991     egg_snprintf(version, sizeof version,
992     "Eggdrop v%s (C) 1997 Robey Pointer (C) 2010 Eggheads",
993     egg_version);
994     /* Now add on the patchlevel (for Tcl) */
995     sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
996     strcat(egg_version, egg_xtra);
997    
998     /* For OSF/1 */
999     #ifdef STOP_UAC
1000     /* Don't print "unaligned access fixup" warning to the user */
1001     nvpair[0] = SSIN_UACPROC;
1002     nvpair[1] = UAC_NOPRINT;
1003     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
1004     #endif
1005    
1006     /* Set up error traps: */
1007     sv.sa_handler = got_bus;
1008     sigemptyset(&sv.sa_mask);
1009     #ifdef SA_RESETHAND
1010     sv.sa_flags = SA_RESETHAND;
1011     #else
1012     sv.sa_flags = 0;
1013     #endif
1014     sigaction(SIGBUS, &sv, NULL);
1015     sv.sa_handler = got_segv;
1016     sigaction(SIGSEGV, &sv, NULL);
1017     #ifdef SA_RESETHAND
1018     sv.sa_flags = 0;
1019     #endif
1020     sv.sa_handler = got_fpe;
1021     sigaction(SIGFPE, &sv, NULL);
1022     sv.sa_handler = got_term;
1023     sigaction(SIGTERM, &sv, NULL);
1024     sv.sa_handler = got_hup;
1025     sigaction(SIGHUP, &sv, NULL);
1026     sv.sa_handler = got_quit;
1027     sigaction(SIGQUIT, &sv, NULL);
1028     sv.sa_handler = SIG_IGN;
1029     sigaction(SIGPIPE, &sv, NULL);
1030     sv.sa_handler = got_ill;
1031     sigaction(SIGILL, &sv, NULL);
1032     sv.sa_handler = got_alarm;
1033     sigaction(SIGALRM, &sv, NULL);
1034    
1035     /* Initialize variables and stuff */
1036     now = time(NULL);
1037     chanset = NULL;
1038     egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
1039     lastmin = nowtm.tm_min;
1040     srandom((unsigned int) (now % (getpid() + getppid())));
1041     init_mem();
1042     init_language(1);
1043     if (argc > 1)
1044     for (i = 1; i < argc; i++)
1045     do_arg(argv[i]);
1046    
1047     printf("\n%s\n", version);
1048    
1049     #ifndef CYGWIN_HACKS
1050     /* Don't allow eggdrop to run as root
1051     * This check isn't useful under cygwin and has been
1052     * reported to cause trouble in some situations.
1053     */
1054     if (((int) getuid() == 0) || ((int) geteuid() == 0))
1055     fatal("ERROR: Eggdrop will not run as root!", 0);
1056     #endif
1057    
1058     #ifndef REPLACE_NOTIFIER
1059     init_threaddata(1);
1060     #endif
1061     init_userent();
1062     init_misc();
1063     init_bots();
1064     init_modules();
1065     if (backgrd)
1066     bg_prepare_split();
1067     init_tcl(argc, argv);
1068     init_language(0);
1069     #ifdef STATIC
1070     link_statics();
1071     #endif
1072     strncpyz(s, ctime(&now), sizeof s);
1073     strcpy(&s[11], &s[20]);
1074     putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
1075     chanprog();
1076     if (!encrypt_pass) {
1077     printf(MOD_NOCRYPT);
1078     bg_send_quit(BG_ABORT);
1079     exit(1);
1080     }
1081     i = 0;
1082     for (chan = chanset; chan; chan = chan->next)
1083     i++;
1084     putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
1085     botnetnick, i, count_users(userlist));
1086 pseudo 1.4 #ifdef TLS
1087     ssl_init();
1088     #endif
1089 simple 1.1 cache_miss = 0;
1090     cache_hit = 0;
1091     if (!pid_file[0])
1092     egg_snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
1093    
1094     /* Check for pre-existing eggdrop! */
1095     f = fopen(pid_file, "r");
1096     if (f != NULL) {
1097     fgets(s, 10, f);
1098     xx = atoi(s);
1099     i = kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
1100     * is used */
1101     if (i == 0 || errno != ESRCH) {
1102     printf(EGG_RUNNING1, botnetnick);
1103     printf(EGG_RUNNING2, pid_file);
1104     bg_send_quit(BG_ABORT);
1105     exit(1);
1106     }
1107     }
1108    
1109     /* Move into background? */
1110     if (backgrd) {
1111     bg_do_split();
1112     } else { /* !backgrd */
1113     xx = getpid();
1114     if (xx != 0) {
1115     FILE *fp;
1116    
1117     /* Write pid to file */
1118     unlink(pid_file);
1119     fp = fopen(pid_file, "w");
1120     if (fp != NULL) {
1121     fprintf(fp, "%u\n", xx);
1122     if (fflush(fp)) {
1123     /* Let the bot live since this doesn't appear to be a botchk */
1124     printf(EGG_NOWRITE, pid_file);
1125     fclose(fp);
1126     unlink(pid_file);
1127     } else
1128     fclose(fp);
1129     } else
1130     printf(EGG_NOWRITE, pid_file);
1131     }
1132     }
1133    
1134     use_stderr = 0; /* Stop writing to stderr now */
1135     if (backgrd) {
1136     /* Ok, try to disassociate from controlling terminal (finger cross) */
1137     #ifdef HAVE_SETPGID
1138     setpgid(0, 0);
1139     #endif
1140     /* Tcl wants the stdin, stdout and stderr file handles kept open. */
1141     freopen("/dev/null", "r", stdin);
1142     freopen("/dev/null", "w", stdout);
1143     freopen("/dev/null", "w", stderr);
1144     #ifdef CYGWIN_HACKS
1145     FreeConsole();
1146     #endif
1147     }
1148    
1149     /* Terminal emulating dcc chat */
1150     if (!backgrd && term_z) {
1151     int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
1152    
1153 pseudo 1.2 getvhost(&dcc[n].sockname, AF_INET);
1154 simple 1.1 dcc[n].sock = STDOUT;
1155     dcc[n].timeval = now;
1156     dcc[n].u.chat->con_flags = conmask;
1157     dcc[n].u.chat->strip_flags = STRIP_ALL;
1158     dcc[n].status = STAT_ECHO;
1159     strcpy(dcc[n].nick, "HQ");
1160     strcpy(dcc[n].host, "llama@console");
1161     /* HACK: Workaround not to pass literal "HQ" as a non-const arg */
1162     dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
1163     /* Make sure there's an innocuous HQ user if needed */
1164     if (!dcc[n].user) {
1165     userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY);
1166     dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
1167     }
1168     setsock(STDOUT, 0); /* Entry in net table */
1169     dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
1170     dcc_chatter(n);
1171     }
1172    
1173     then = now;
1174     online_since = now;
1175     autolink_cycle(NULL); /* Hurry and connect to tandem bots */
1176     add_help_reference("cmds1.help");
1177     add_help_reference("cmds2.help");
1178     add_help_reference("core.help");
1179     add_hook(HOOK_SECONDLY, (Function) core_secondly);
1180     add_hook(HOOK_MINUTELY, (Function) core_minutely);
1181     add_hook(HOOK_HOURLY, (Function) core_hourly);
1182     add_hook(HOOK_REHASH, (Function) event_rehash);
1183     add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
1184     add_hook(HOOK_USERFILE, (Function) event_save);
1185     add_hook(HOOK_BACKUP, (Function) backup_userfile);
1186     add_hook(HOOK_DAILY, (Function) event_logfile);
1187     add_hook(HOOK_DAILY, (Function) event_resettraffic);
1188     add_hook(HOOK_LOADED, (Function) event_loaded);
1189    
1190     call_hook(HOOK_LOADED);
1191    
1192     debug0("main: entering loop");
1193     while (1) {
1194     mainloop(1);
1195     }
1196     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23