/[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.190 - (show annotations) (download) (as text)
Wed May 9 01:32:32 2007 UTC (12 years, 3 months ago) by sven
Branch: MAIN
Changes since 1.189: +10 -2 lines
File MIME type: text/x-chdr

 * Created a double linked list to hold all user records. Hash tables are
   great, but sometimes you just have to go through all records.
 * Refuse to actively link to bots with a negative priority. Incoming
   connections from these bots will still be acceppted.
 * Refuse partyline logins from bots. Bots usually don't have a password
   set, so that might be exploitable.
 * Ping connected bots if they haven't sent anything for 5 minutes.
 * Started to structure the user settings and documented it with a Doxygen
   page.
 * Added autolinking for bots based on a priority. See botnet_autolink()
   documentation for details.

1 /* main.c
2 *
3 * Copyright (C) 1997 Robey Pointer
4 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Eggheads Development Team
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #ifndef lint
22 static const char rcsid[] = "$Id: main.c,v 1.189 2006-11-14 14:51:24 sven Exp $";
23 #endif
24
25 #include <eggdrop/eggdrop.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <locale.h>
32 #include <ltdl.h>
33
34 #include "debug.h"
35 #include "core_config.h"
36 #include "core_party.h"
37 #include "core_binds.h"
38 #include "logfile.h"
39 #include "terminal.h"
40 #include "bg.h"
41 #include "main.h"
42
43 #ifdef STOP_UAC /* OSF/1 complains a lot */
44 # include <sys/sysinfo.h>
45 # define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
46 #endif
47
48 #ifdef CYGWIN_HACKS
49 # include <windows.h>
50 #endif
51
52 #ifndef _POSIX_SOURCE
53 # define _POSIX_SOURCE 1 /* Solaris needs this */
54 #endif
55
56 #define RUNMODE_NORMAL 1
57 #define RUNMODE_SHUTDOWN 2
58 #define RUNMODE_RESTART 3
59
60 int runmode = 1; /* The most important variable ever! */
61 int backgrd = 1; /* Run in the background? */
62 int make_userfile = 0; /* Start bot in make-userfile mode? */
63 int terminal_mode = 0; /* Terminal mode */
64
65 /* XXX: remove this feature (but not yet) */
66 int debug_run = 0; /* Just init and shutdown */
67
68 const char *configfile = NULL; /* Name of the config file */
69 char pid_file[512]; /* Name of Eggdrop's pid file */
70
71 time_t online_since; /* Time the bot was started */
72 time_t now; /* Current time */
73 egg_timeval_t egg_timeval_now; /* Current time in seconds and microseconds. */
74
75 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
76
77 char version[81];
78
79 static int lastmin = 99;
80 static struct tm nowtm;
81
82 void fatal(const char *errmsg)
83 {
84 putlog(LOG_MISC, "*", "Fatal: %s", errmsg);
85 flushlogs();
86 exit(1);
87 }
88
89 static void got_bus(int z)
90 {
91 fatal("BUS ERROR.");
92 }
93
94 static void got_segv(int z)
95 {
96 fatal("SEGMENT VIOLATION.");
97 }
98
99 static void got_fpe(int z)
100 {
101 fatal("FLOATING POINT ERROR.");
102 }
103
104 static void got_term(int z)
105 {
106 eggdrop_event("sigterm");
107 if (core_config.die_on_sigterm) core_shutdown(SHUTDOWN_SIGTERM, NULL, NULL);
108 else putlog(LOG_MISC, "*", _("Received TERM signal (ignoring)."));
109 }
110
111 static void got_quit(int z)
112 {
113 eggdrop_event("sigquit");
114 putlog(LOG_MISC, "*", _("Received QUIT signal (ignoring)."));
115 return;
116 }
117
118 static void got_hup(int z)
119 {
120 eggdrop_event("sighup");
121 putlog(LOG_MISC, "*", _("Received HUP signal (ignoring)."));
122 return;
123 }
124
125 static void print_version(void)
126 {
127 printf("%s\n", version);
128 }
129
130 static void print_help(char *const *argv)
131 {
132 print_version();
133 printf(_("\nUsage: %s [OPTIONS]... [FILE]\n"), argv[0]);
134 printf(_("\n\
135 -h, --help Print help and exit\n\
136 -v, --version Print version and exit\n\
137 -n, --foreground Don't go into the background (default=off)\n\
138 -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
139 -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
140 -m --make-userfile Userfile creation mode\n\
141 -d --debug Do a debugging dry-run\n\
142 FILE optional config filename (default 'config.xml')\n"));
143 printf("\n");
144 }
145
146 static void do_args(int argc, char **argv)
147 {
148 int c;
149
150 optarg = 0;
151 optind = 1;
152 opterr = 1;
153 optopt = '?';
154
155 while (1) {
156 int option_index = 0;
157
158 static struct option long_options[] = {
159 { "help", 0, NULL, 'h' },
160 { "version", 0, NULL, 'v' },
161 { "load-module", 1, NULL, 'p' },
162 { "foreground", 0, NULL, 'n' },
163 { "terminal", 0, NULL, 't' },
164 { "make-userfile", 0, NULL, 'm' },
165 { "debug", 0, NULL, 'd' },
166 {NULL, 0, NULL, 0}
167 };
168
169 c = getopt_long(argc, argv, "hvp:nctmd", long_options, &option_index);
170 if (c == -1) break;
171 switch (c) {
172 case 'n':
173 backgrd = 0;
174 break;
175 case 't':
176 terminal_mode = 1;
177 break;
178 case 'm':
179 make_userfile = 1;
180 break;
181 case 'd':
182 backgrd = 0;
183 debug_run = 1;
184 break;
185 case 'v':
186 print_version();
187 exit(0);
188 break; /* this should never be reached */
189 case 'h':
190 print_help(argv);
191 exit(0);
192 break; /* this should never be reached */
193 case 0: /* Long option with no short option */
194 case '?': /* Invalid option. */
195 /* getopt_long() already printed an error message. */
196 exit(1);
197 default: /* bug: option not considered. */
198 fprintf(stderr, _("%s: option unknown: %c\n"), argv[0], c);
199 exit(1);
200 break; /* this should never be reached */
201 }
202 }
203 if (optind < argc) {
204 configfile = argv[optind++];
205 eggdrop_set_params((const char **)&argv[optind], argc-optind);
206 }
207 else configfile = "config.xml";
208 }
209
210 /* Called once a second.
211 */
212 static int core_secondly()
213 {
214 static int cnt = 0;
215 int miltime;
216
217 check_bind_secondly();
218 cnt++;
219 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
220 if (nowtm.tm_min != lastmin) {
221 int i = 0;
222
223 /* Once a minute */
224 lastmin = (lastmin + 1) % 60;
225 eggdrop_event("minutely");
226 check_bind_time(&nowtm);
227 /* In case for some reason more than 1 min has passed: */
228 while (nowtm.tm_min != lastmin) {
229 /* Timer drift, dammit */
230 putlog(LOG_DEBUG, "*", "timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
231 i++;
232 lastmin = (lastmin + 1) % 60;
233 eggdrop_event("minutely");
234 }
235 if (i > 1) putlog(LOG_MISC, "*", _("Warning: timer drift (%d minutes)."), i);
236
237 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
238 if (nowtm.tm_min % 5 == 0) {
239 botnet_entity_t me = bot_entity((botnet_bot_t *) 0);
240 eggdrop_event("5minutely");
241 botnet_extension(EXTENSION_NEIGHBOURS, &me, NULL, NULL, "ping", "", 0);
242 botnet_autolink();
243 }
244 if (nowtm.tm_min == 0) eggdrop_event("hourly");
245 if (!miltime) { /* At midnight */
246 char s[25];
247
248 strlcpy(s, ctime(&now), sizeof s);
249 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
250 eggdrop_event("backup");
251 eggdrop_event("daily");
252 }
253 }
254 return(0);
255 }
256
257
258 int file_check(const char *filename)
259 {
260 struct stat sbuf = {0};
261 int r;
262
263 r = stat(filename, &sbuf);
264 if (r) return(-1);
265 errno = 0;
266 if (sbuf.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) return(-1);
267 return(0);
268 }
269
270 static int create_userfile(void)
271 {
272 user_t *owner;
273 char handle[512], password[12];
274 int len;
275
276 printf("\n");
277 if (user_count() != 0) {
278 printf("You are trying to create a new userfile, but the old one still exists (%s)!\n", core_config.userfile);
279 printf("Please remove the userfile, or do not pass the -m option.\n\n");
280 return(-1);
281 }
282
283 printf("Hello! I see you are creating a new userfile.\n\n");
284 printf("Let's create the owner account.\n\n");
285 do {
286 printf("Enter the owner handle: ");
287 fflush(stdout);
288 fgets(handle, sizeof(handle), stdin);
289 for (len = 0; handle[len]; len++) {
290 if (!ispunct(handle[len]) && !isalnum(handle[len])) break;
291 }
292 if (len == 0) printf("Come on, enter a real handle.\n\n");
293 } while (len <= 0);
294 handle[len] = 0;
295
296 owner = user_new(handle);
297 if (!owner) {
298 printf("Failed to create user record! Out of memory?\n\n");
299 return(-1);
300 }
301
302 user_rand_pass(password, sizeof(password));
303 user_set_pass(owner, password);
304 user_set_flags_str(owner, NULL, "+n");
305 printf("Your owner handle is '%s' and your password is '%s' (without the quotes).\n\n", handle, password);
306 memset(password, 0, sizeof(password));
307 str_redup(&core_config.owner, handle);
308 core_config_save();
309 user_save(core_config.userfile);
310
311 return(0);
312 }
313
314 static void init_signals(void)
315 {
316 struct sigaction sv;
317
318
319 sv.sa_handler = got_bus;
320 sigemptyset(&sv.sa_mask);
321 #ifdef SA_RESETHAND
322 sv.sa_flags = SA_RESETHAND;
323 #else
324 sv.sa_flags = 0;
325 #endif
326 sigaction(SIGBUS, &sv, NULL);
327
328 sv.sa_handler = got_segv;
329 //sigaction(SIGSEGV, &sv, NULL);
330 #ifdef SA_RESETHAND
331 sv.sa_flags = 0;
332 #endif
333
334 sv.sa_handler = got_fpe;
335 sigaction(SIGFPE, &sv, NULL);
336
337 sv.sa_handler = got_term;
338 sigaction(SIGTERM, &sv, NULL);
339
340 sv.sa_handler = got_hup;
341 sigaction(SIGHUP, &sv, NULL);
342
343 sv.sa_handler = got_quit;
344 sigaction(SIGQUIT, &sv, NULL);
345
346 sv.sa_handler = SIG_IGN;
347 sigaction(SIGPIPE, &sv, NULL);
348
349 sv.sa_handler = SIG_IGN;
350 sigaction(SIGUSR1, &sv, NULL);
351 }
352
353 static void core_shutdown_or_restart()
354 {
355 /* notifiy shutdown listeners */
356 check_bind_shutdown();
357
358 putlog(LOG_MISC, "*", _("Saving user file..."));
359 user_save(core_config.userfile);
360
361 /* destroy terminal */
362 if (terminal_mode)
363 terminal_shutdown(runmode);
364
365 /* unload core help */
366 //help_unload_by_module("core");
367
368 /* shutdown core binds */
369 core_binds_shutdown();
370
371 /* shutdown logging */
372 logfile_shutdown();
373
374 putlog(LOG_MISC, "*", _("Saving config file..."));
375 core_config_save();
376
377 /* shutdown libeggdrop */
378 eggdrop_shutdown();
379
380 /* force a garbage run */
381 garbage_run();
382
383 /* just remove pid file if didn't restart */
384 if (runmode != RUNMODE_RESTART)
385 unlink(pid_file);
386 }
387
388 int main(int argc, char **argv)
389 {
390 int timeout;
391
392 #ifdef DEBUG
393 {
394 # include <sys/resource.h>
395 struct rlimit cdlim;
396
397 cdlim.rlim_cur = RLIM_INFINITY;
398 cdlim.rlim_max = RLIM_INFINITY;
399 setrlimit(RLIMIT_CORE, &cdlim);
400 }
401 #endif
402
403 init_signals();
404
405 #ifdef ENABLE_NLS
406 setlocale(LC_MESSAGES, "");
407 bindtextdomain(PACKAGE, LOCALEDIR);
408 textdomain(PACKAGE);
409 #endif
410
411 /* Initialize ltdl. */
412 LTDL_SET_PRELOADED_SYMBOLS();
413 if (lt_dlinit()) {
414 printf("Fatal error initializing ltdl: %s\n", lt_dlerror());
415 return(-1);
416 }
417
418 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2004 Eggheads Development Team", VERSION);
419
420 #ifdef STOP_UAC
421 {
422 int nvpair[2];
423
424 nvpair[0] = SSIN_UACPROC;
425 nvpair[1] = UAC_NOPRINT;
426 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
427 }
428 #endif
429
430 timer_update_now(&egg_timeval_now);
431 now = egg_timeval_now.sec;
432 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
433 lastmin = nowtm.tm_min;
434
435 srandom(now % (getpid() + getppid()));
436
437 do_args(argc, argv);
438 printf("\n%s\n", version);
439 printf("WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
440
441 /* we may not run as root */
442 if (((int) getuid() == 0) || ((int) geteuid() == 0)) {
443 fprintf(stderr, "ERROR\n");
444 fprintf(stderr, "\tEggdrop will not run as root!\n\n");
445 return -1;
446 }
447
448 init_flag_map();
449
450 /* config file may not be world read/writeable */
451 if (file_check(configfile)) {
452 fprintf(stderr, "ERROR\n");
453 if (errno) {
454 fprintf(stderr, "\tFailed to load config file '%s': %s\n\n", configfile, strerror(errno));
455 } else {
456 fprintf(stderr, "\tCheck file permissions on your config file '%s'!\n", configfile);
457 fprintf(stderr, "\tMake sure other groups and users cannot read/write it.\n\n");
458 }
459 return -1;
460 }
461
462 /* set uptime */
463 online_since = now;
464
465 do {
466 int tid;
467 egg_timeval_t howlong;
468
469 /* default select timeout is 1 sec */
470 howlong.sec = 1;
471 howlong.usec = 0;
472
473 tid = timer_create_repeater(&howlong, "main loop", core_secondly);
474
475 /* init core */
476 if (core_init() != 0)
477 break;
478
479 /* set normal running mode */
480 runmode = RUNMODE_NORMAL;
481
482 /* try to link other bots */
483 botnet_autolink();
484
485 putlog(LOG_DEBUG, "*", "Entering main loop.");
486
487 /* main loop */
488 while (!debug_run && runmode == RUNMODE_NORMAL) {
489 /* Update the time. */
490 timer_update_now(&egg_timeval_now);
491 now = egg_timeval_now.sec;
492
493 /* Run any expired timers. */
494 timer_run();
495
496 /* Figure out how long to wait for activity. */
497 if (timer_get_shortest(&howlong)) timeout = 1000;
498 else timeout = howlong.sec * 1000 + howlong.usec / 1000;
499
500 /* Wait and monitor sockets. */
501 sockbuf_update_all(timeout);
502
503 /* Run any scheduled cleanups. */
504 garbage_run();
505 }
506
507 /* shutdown secondly timer */
508 timer_destroy(tid);
509
510 /* Save user file, config file, ... */
511 core_shutdown_or_restart();
512
513 /* the only chance to loop again is that running = 2, meaning
514 we have a restart */
515 } while (runmode == RUNMODE_RESTART);
516
517 return 0;
518 }
519
520 int core_init()
521 {
522 int i;
523 char *name;
524 void *config_root, *entry;
525 char datetime[25];
526 static int lockfd = -1;
527 struct flock lock;
528
529 /* init libeggdrop */
530 eggdrop_init();
531
532 /* load config */
533 if (core_config_init(configfile) != 0)
534 return (-1);
535
536 /* init logging */
537 logfile_init();
538
539 /* did the user specify -m? */
540 if (make_userfile) {
541 if (create_userfile()) return(-1);
542 make_userfile = 0;
543 }
544
545 /* init background mode and pid file */
546 if (runmode != RUNMODE_RESTART) {
547 if (backgrd) bg_begin_split();
548 }
549
550 /* Check lock file. */
551 if (core_config.lockfile && lockfd == -1) {
552 lockfd = open(core_config.lockfile, O_WRONLY | O_CREAT, S_IWUSR);
553 if (lockfd < 0) {
554 fatal("Could not open lock file!");
555 }
556 memset(&lock, 0, sizeof(lock));
557 lock.l_type = F_WRLCK;
558 if (fcntl(lockfd, F_SETLK, &lock) != 0) {
559 fatal("Lock file is already locked! Is eggdrop already running?");
560 }
561 }
562
563 /* just issue this "Loading Eggdrop" message if we are not
564 * restarting */
565 if (runmode != RUNMODE_RESTART) {
566 strlcpy(datetime, ctime(&now), sizeof(datetime));
567 strcpy(&datetime[11], &datetime[20]);
568 putlog(LOG_ALL, "*", "Loading Eggdrop %s (%s)", VERSION, datetime);
569 }
570
571 /* load userlist */
572 if (core_config.userfile)
573 user_load(core_config.userfile);
574
575 /* init core bindings */
576 core_binds_init();
577
578 /* init core partyline */
579 core_party_init();
580
581 /* Load core help */
582 help_load_by_module ("core");
583
584 /* Put the module directory in the ltdl search path. */
585 if (core_config.module_path) module_add_dir(core_config.module_path);
586
587 /* Set a botname. */
588 if (core_config.botname) botnet_set_name(core_config.botname);
589
590 /* Initialize help system. */
591 if (core_config.help_path) {
592 help_set_default_path(core_config.help_path);
593 /* Load all help files here... */
594 }
595
596 /* Scan the autoload section of config. */
597 config_root = config_get_root("eggdrop");
598 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL)); i++) {
599 name = NULL;
600 config_get_str(&name, entry, NULL);
601 module_load(name);
602 /* For now we'll just load these help files... */
603 help_load_by_module(name);
604 }
605 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL)); i++) {
606 name = NULL;
607 config_get_str(&name, entry, NULL);
608 script_load(name);
609 }
610
611 /* notify init listeners */
612 check_bind_init();
613
614 /* start terminal */
615 if (runmode != RUNMODE_RESTART) {
616 if (backgrd) bg_finish_split();
617 }
618 if (terminal_mode) terminal_init();
619
620 return (0);
621 }
622
623 int core_restart(const char *nick)
624 {
625 putlog(LOG_MISC, "*", "Restarting...");
626 runmode = RUNMODE_RESTART;
627 return (0);
628 }
629
630 int core_shutdown(int how, const char *nick, const char *reason)
631 {
632 if (how == SHUTDOWN_SIGTERM)
633 putlog(LOG_MISC, "*", ("Received TERM signal, shutting down."));
634 else if (how != SHUTDOWN_RESTART)
635 putlog(LOG_MISC, "*", ("Shutdown requested by %s: %s"), nick,
636 (reason) ? reason : _("No reason"));
637
638 /* check how to shut down */
639 switch (how) {
640
641 case (SHUTDOWN_RESTART):
642 runmode = RUNMODE_RESTART;
643 break;
644
645 case (SHUTDOWN_GRACEFULL):
646 runmode = RUNMODE_SHUTDOWN;
647 break;
648
649 /* These two cases are treaded special because something
650 * unusual happened and we're better off if we exit() NOW
651 * than doing a gracefull shutdown with freeing all
652 * memory, closing all sockets, ... */
653 case (SHUTDOWN_HARD):
654 case (SHUTDOWN_SIGTERM):
655 runmode = RUNMODE_SHUTDOWN;
656 core_shutdown_or_restart();
657 exit(0);
658 break;
659
660 }
661
662 return BIND_RET_LOG;
663 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23