main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*
  2. * main.c -- handles:
  3. * core event handling
  4. * command line arguments
  5. * context and assert debugging
  6. *
  7. */
  8. #include "common.h"
  9. #include "main.h"
  10. #include "hooks.h"
  11. #include "dcc.h"
  12. #include "misc.h"
  13. #include "settings.h"
  14. #include "salt.h"
  15. #include "misc_file.h"
  16. #include "net.h"
  17. #include "users.h"
  18. #include "shell.h"
  19. #include "userrec.h"
  20. #include "tclhash.h"
  21. #include "cfg.h"
  22. #include "dccutil.h"
  23. #include "crypt.h"
  24. #include "debug.h"
  25. #include "chanprog.h"
  26. #include "traffic.h"
  27. #include "bg.h"
  28. #include "botnet.h"
  29. #include "build.h"
  30. #ifdef LEAF
  31. #include "src/mod/server.mod/server.h"
  32. #endif /* LEAF */
  33. #include <time.h>
  34. #include <errno.h>
  35. #include <unistd.h>
  36. #ifdef STOP_UAC /* osf/1 complains a lot */
  37. # include <sys/sysinfo.h>
  38. # define UAC_NOPRINT /* Don't report unaligned fixups */
  39. #endif /* STOP_UAC */
  40. #include <sys/file.h>
  41. #include <sys/stat.h>
  42. #include "chan.h"
  43. #include "tandem.h"
  44. #include "egg_timer.h"
  45. #include "core_binds.h"
  46. #ifdef CYGWIN_HACKS
  47. #include <windows.h>
  48. #endif
  49. #ifndef _POSIX_SOURCE
  50. /* Solaris needs this */
  51. #define _POSIX_SOURCE
  52. #endif
  53. extern int optind;
  54. const time_t buildts = CVSBUILD; /* build timestamp (UTC) */
  55. const char egg_version[1024] = "1.1.4";
  56. int localhub = 1; /* we set this to 0 if we get a -B */
  57. int role;
  58. int loading = 0;
  59. int default_flags = 0; /* Default user flags and */
  60. int default_uflags = 0; /* Default userdefinied flags for people
  61. who say 'hello' or for .adduser */
  62. int backgrd = 1; /* Run in the background? */
  63. uid_t myuid;
  64. int term_z = 0; /* Foreground: use the terminal as a party line? */
  65. int updating = 0; /* this is set when the binary is called from itself. */
  66. char tempdir[DIRMAX] = "";
  67. char *binname = NULL;
  68. time_t online_since; /* Unix-time that the bot loaded up */
  69. char owner[121] = ""; /* Permanent owner(s) of the bot */
  70. char version[81] = ""; /* Version info (long form) */
  71. char ver[41] = ""; /* Version info (short form) */
  72. int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
  73. int do_restart = 0; /* .restart has been called, restart asap */
  74. char quit_msg[1024]; /* quit message */
  75. time_t now; /* duh, now :) */
  76. #define fork_interval atoi( CFG_FORKINTERVAL.ldata ? CFG_FORKINTERVAL.ldata : CFG_FORKINTERVAL.gdata ? CFG_FORKINTERVAL.gdata : "0")
  77. #ifdef S_CONFEDIT
  78. static int do_confedit = 0; /* show conf menu if -C */
  79. #endif /* S_CONFEDIT */
  80. #ifdef LEAF
  81. static char do_killbot[21] = "";
  82. #endif /* LEAF */
  83. static int checktrace = 1; /* Check for trace when starting up? */
  84. static char *getfullbinname(const char *argv_zero)
  85. {
  86. char cwd[DIRMAX] = "", *bin = NULL, *p = NULL, *p2 = NULL;
  87. bin = strdup(argv_zero);
  88. if (bin[0] == '/')
  89. return bin;
  90. if (!getcwd(cwd, DIRMAX))
  91. fatal("getcwd() failed", 0);
  92. if (cwd[strlen(cwd) - 1] == '/')
  93. cwd[strlen(cwd) - 1] = 0;
  94. p = bin;
  95. p2 = strchr(p, '/');
  96. while (p) {
  97. if (p2)
  98. *p2++ = 0;
  99. if (!strcmp(p, "..")) {
  100. p = strrchr(cwd, '/');
  101. if (p)
  102. *p = 0;
  103. } else if (strcmp(p, ".")) {
  104. strcat(cwd, "/");
  105. strcat(cwd, p);
  106. }
  107. p = p2;
  108. if (p)
  109. p2 = strchr(p, '/');
  110. }
  111. str_redup(&bin, cwd);
  112. return bin;
  113. }
  114. /* Traffic stats
  115. */
  116. egg_traffic_t traffic;
  117. void fatal(const char *s, int recoverable)
  118. {
  119. int i = 0;
  120. #ifdef LEAF
  121. nuke_server((char *) s);
  122. #endif /* LEAF */
  123. if (s[0])
  124. putlog(LOG_MISC, "*", "* %s", s);
  125. /* flushlogs(); */
  126. #ifdef HAVE_SSL
  127. ssl_cleanup();
  128. #endif /* HAVE_SSL */
  129. for (i = 0; i < dcc_total; i++)
  130. if (dcc[i].sock >= 0)
  131. killsock(dcc[i].sock);
  132. if (!recoverable) {
  133. if (conf.bot && conf.bot->pid_file)
  134. unlink(conf.bot->pid_file);
  135. exit(1);
  136. }
  137. }
  138. static void check_expired_dcc()
  139. {
  140. int i;
  141. for (i = 0; i < dcc_total; i++)
  142. if (dcc[i].type && dcc[i].type->timeout_val &&
  143. ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
  144. if (dcc[i].type->timeout)
  145. dcc[i].type->timeout(i);
  146. else if (dcc[i].type->eof)
  147. dcc[i].type->eof(i);
  148. else
  149. continue;
  150. /* Only timeout 1 socket per cycle, too risky for more */
  151. return;
  152. }
  153. }
  154. static void expire_simuls() {
  155. int idx = 0;
  156. for (idx = 0; idx < dcc_total; idx++) {
  157. if (dcc[idx].simul > 0) {
  158. if ((now - dcc[idx].simultime) >= 20) { /* expire simuls after 20 seconds (re-uses idx, so it wont fill up) */
  159. dcc[idx].simul = -1;
  160. lostdcc(idx);
  161. }
  162. }
  163. }
  164. }
  165. static void checkpass()
  166. {
  167. static int checkedpass = 0;
  168. if (!checkedpass) {
  169. char *gpasswd = NULL;
  170. gpasswd = (char *) getpass("bash$ ");
  171. checkedpass = 1;
  172. if (!gpasswd || (gpasswd && md5cmp(shellhash, gpasswd))) {
  173. werr(ERR_BADPASS);
  174. }
  175. }
  176. }
  177. static void got_ed(char *which, char *in, char *out)
  178. {
  179. sdprintf(STR("got_Ed called: -%s i: %s o: %s"), which, in, out);
  180. if (!in || !out)
  181. fatal(STR("Wrong number of arguments: -e/-d <infile> <outfile/STDOUT>"),0);
  182. if (!strcmp(in, out))
  183. fatal(STR("<infile> should NOT be the same name as <outfile>"), 0);
  184. if (!strcmp(which, "e")) {
  185. EncryptFile(in, out);
  186. fatal(STR("File Encryption complete"),3);
  187. } else if (!strcmp(which, "d")) {
  188. DecryptFile(in, out);
  189. fatal(STR("File Decryption complete"),3);
  190. }
  191. exit(0);
  192. }
  193. static void show_help()
  194. {
  195. char format[81] = "";
  196. egg_snprintf(format, sizeof format, "%%-30s %%-30s\n");
  197. printf(STR("Wraith %s\n\n"), egg_version);
  198. printf(format, STR("Option"), STR("Description"));
  199. printf(format, STR("------"), STR("-----------"));
  200. printf(format, STR("-B <botnick>"), STR("Starts the specified bot"));
  201. printf(format, STR("-C"), STR("Config file menu system"));
  202. printf(format, STR("-e <infile> <outfile>"), STR("Encrypt infile to outfile"));
  203. printf(format, STR("-d <infile> <outfile>"), STR("Decrypt infile to outfile"));
  204. printf(format, STR("-D"), STR("Enables debug mode (see -n)"));
  205. printf(format, STR("-E [#/all]"), STR("Display Error codes english translation (use 'all' to display all)"));
  206. /* printf(format, STR("-g <file>"), STR("Generates a template config file"));
  207. printf(format, STR("-G <file>"), STR("Generates a custom config for the box"));
  208. */
  209. printf(format, STR("-h"), STR("Display this help listing"));
  210. printf(format, STR("-k <botname>"), STR("Terminates (botname) with kill -9"));
  211. printf(format, STR("-n"), STR("Disables backgrounding first bot in conf"));
  212. printf(format, STR("-s"), STR("Disables checking for ptrace/strace during startup (no pass needed)"));
  213. printf(format, STR("-t"), STR("Enables \"Partyline\" emulation (requires -n)"));
  214. printf(format, STR("-v"), STR("Displays bot version"));
  215. exit(0);
  216. }
  217. #ifdef LEAF
  218. # define PARSE_FLAGS "2B:Cd:De:Eg:G:k:L:P:hnstv"
  219. #else /* !LEAF */
  220. # define PARSE_FLAGS "2Cd:De:Eg:G:hnstv"
  221. #endif /* HUB */
  222. #define FLAGS_CHECKPASS "CdDeEgGhkntv"
  223. static void dtx_arg(int argc, char *argv[])
  224. {
  225. int i = 0;
  226. #ifdef LEAF
  227. int localhub_pid = 0;
  228. #endif /* LEAF */
  229. char *p = NULL;
  230. opterr = 0;
  231. while ((i = getopt(argc, argv, PARSE_FLAGS)) != EOF) {
  232. if (strchr(FLAGS_CHECKPASS, i))
  233. checkpass();
  234. switch (i) {
  235. case '2': /* used for testing new binary through update */
  236. exit(2);
  237. #ifdef LEAF
  238. case 'B':
  239. localhub = 0;
  240. strncpyz(origbotname, optarg, NICKLEN + 1);
  241. break;
  242. #endif /* LEAF */
  243. #ifdef S_CONFEDIT
  244. case 'C':
  245. do_confedit = 1;
  246. break;
  247. #endif /* S_CONFEDIT */
  248. case 'h':
  249. show_help();
  250. #ifdef LEAF
  251. case 'k': /* kill bot */
  252. strncpyz(do_killbot, optarg, sizeof do_killbot);
  253. #endif /* LEAF */
  254. case 'n':
  255. backgrd = 0;
  256. break;
  257. case 's':
  258. checktrace = 0;
  259. break;
  260. case 't':
  261. term_z = 1;
  262. break;
  263. case 'D':
  264. sdebug = 1;
  265. sdprintf(STR("debug enabled"));
  266. break;
  267. case 'E':
  268. p = argv[optind];
  269. if (p && p[0]) {
  270. if (!strcmp(p, "all")) {
  271. int n;
  272. putlog(LOG_MISC, "*", STR("Listing all errors"));
  273. for (n = 1; n < ERR_MAX; n++)
  274. putlog(LOG_MISC, "*", STR("Error #%d: %s"), n, werr_tostr(n));
  275. } else if (egg_isdigit(p[0])) {
  276. putlog(LOG_MISC, "*", STR("Error #%d: %s"), atoi(p), werr_tostr(atoi(p)));
  277. }
  278. exit(0);
  279. } else {
  280. fatal(STR("You must specify error number after -E (or 'all')"), 0);
  281. }
  282. break;
  283. case 'e':
  284. if (argv[optind])
  285. p = argv[optind];
  286. got_ed("e", optarg, p);
  287. case 'd':
  288. if (argv[optind])
  289. p = argv[optind];
  290. got_ed("d", optarg, p);
  291. case 'v':
  292. {
  293. char date[50] = "";
  294. egg_strftime(date, sizeof date, "%c %Z", gmtime(&buildts));
  295. printf("Wraith %s\nBuild Date: %s (%lu)\n", egg_version, date, buildts);
  296. printf("SALTS\nfiles: %s\nbotlink: %s\n", SALT1, SALT2);
  297. exit(0);
  298. }
  299. #ifdef LEAF
  300. case 'L':
  301. {
  302. localhub_pid = checkpid(optarg, NULL);
  303. break;
  304. }
  305. case 'P':
  306. if (atoi(optarg) && (atoi(optarg) != localhub_pid))
  307. exit(2);
  308. else
  309. sdprintf(STR("Updating..."));
  310. localhub = 1;
  311. updating = 1;
  312. break;
  313. #endif
  314. case '?':
  315. default:
  316. break;
  317. }
  318. }
  319. }
  320. /* Timer info */
  321. static int lastmin = 99;
  322. static struct tm nowtm;
  323. int curcheck = 0;
  324. void core_10secondly()
  325. {
  326. curcheck++;
  327. check_promisc();
  328. if (curcheck == 1)
  329. check_trace();
  330. #ifdef LEAF
  331. if (localhub) {
  332. #endif /* LEAF */
  333. if (curcheck == 2)
  334. check_last();
  335. if (curcheck == 3) {
  336. check_processes();
  337. curcheck = 0;
  338. }
  339. #ifdef LEAF
  340. }
  341. #endif /* LEAF */
  342. }
  343. static void core_secondly()
  344. {
  345. static int cnt = 0;
  346. int miltime;
  347. #ifdef CRAZY_TRACE
  348. if (!attached) crazy_trace();
  349. #endif /* CRAZY_TRACE */
  350. if (fork_interval && backgrd && ((now - lastfork) > fork_interval))
  351. do_fork();
  352. cnt++;
  353. if ((cnt % 30) == 0) {
  354. autolink_cycle(NULL); /* attempt autolinks */
  355. cnt = 0;
  356. }
  357. #ifdef S_UTCTIME
  358. egg_memcpy(&nowtm, gmtime(&now), sizeof(struct tm));
  359. #else /* !S_UTCTIME */
  360. egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
  361. #endif /* S_UTCTIME */
  362. if (nowtm.tm_min != lastmin) {
  363. int i = 0;
  364. /* Once a minute */
  365. lastmin = (lastmin + 1) % 60;
  366. /* In case for some reason more than 1 min has passed: */
  367. while (nowtm.tm_min != lastmin) {
  368. /* Timer drift, dammit */
  369. debug2(STR("timer: drift (lastmin=%d, now=%d)"), lastmin, nowtm.tm_min);
  370. i++;
  371. lastmin = (lastmin + 1) % 60;
  372. }
  373. if (i > 1)
  374. putlog(LOG_MISC, "*", STR("(!) timer drift -- spun %d minutes"), i);
  375. miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
  376. if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
  377. /* flushlogs(); */
  378. if (!miltime) { /* At midnight */
  379. char s[25] = "";
  380. strncpyz(s, ctime(&now), sizeof s);
  381. #ifdef HUB
  382. putlog(LOG_ALL, "*", STR("--- %.11s%s"), s, s + 20);
  383. backup_userfile();
  384. #endif /* HUB */
  385. }
  386. }
  387. /* These no longer need checking since they are all check vs minutely
  388. * settings and we only get this far on the minute.
  389. */
  390. #ifdef HUB
  391. if (miltime == 300)
  392. call_hook(HOOK_DAILY);
  393. #endif /* HUB */
  394. }
  395. }
  396. static void core_minutely()
  397. {
  398. #ifdef LEAF
  399. check_mypid();
  400. #endif
  401. check_bind_time(&nowtm);
  402. /* flushlogs(); */
  403. }
  404. static void core_hourly()
  405. {
  406. }
  407. static void core_halfhourly()
  408. {
  409. #ifdef HUB
  410. write_userfile(-1);
  411. #endif /* HUB */
  412. }
  413. static void event_resettraffic()
  414. {
  415. traffic.out_total.irc += traffic.out_today.irc;
  416. traffic.out_total.bn += traffic.out_today.bn;
  417. traffic.out_total.dcc += traffic.out_today.dcc;
  418. traffic.out_total.filesys += traffic.out_today.filesys;
  419. traffic.out_total.trans += traffic.out_today.trans;
  420. traffic.out_total.unknown += traffic.out_today.unknown;
  421. traffic.in_total.irc += traffic.in_today.irc;
  422. traffic.in_total.bn += traffic.in_today.bn;
  423. traffic.in_total.dcc += traffic.in_today.dcc;
  424. traffic.in_total.filesys += traffic.in_today.filesys;
  425. traffic.in_total.trans += traffic.in_today.trans;
  426. traffic.in_total.unknown += traffic.in_today.unknown;
  427. egg_memset(&traffic.out_today, 0, sizeof(traffic.out_today));
  428. egg_memset(&traffic.in_today, 0, sizeof(traffic.in_today));
  429. }
  430. static void startup_checks() {
  431. char cfile[DIRMAX] = "";
  432. #ifdef HUB
  433. egg_snprintf(cfile, sizeof cfile, STR("%s/conf"), confdir());
  434. #else /* LEAF */
  435. egg_snprintf(cfile, sizeof cfile, STR("%s/.known_hosts"), confdir());
  436. #endif /* HUB */
  437. if (!can_stat(confdir())) {
  438. #ifdef LEAF
  439. if (mkdir(confdir(), S_IRUSR | S_IWUSR | S_IXUSR)) {
  440. unlink(confdir());
  441. if (!can_stat(confdir()))
  442. if (mkdir(confdir(), S_IRUSR | S_IWUSR | S_IXUSR))
  443. #endif /* LEAF */
  444. werr(ERR_CONFSTAT);
  445. #ifdef LEAF
  446. }
  447. #endif /* LEAF */
  448. }
  449. if (!fixmod(confdir()))
  450. werr(ERR_CONFDIRMOD);
  451. /*technically no longer needed?
  452. else if (!can_stat(cfile))
  453. werr(ERR_NOCONF);
  454. */
  455. else if (can_stat(cfile) && !fixmod(cfile))
  456. werr(ERR_CONFMOD);
  457. if (!can_stat(tempdir)) {
  458. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR)) {
  459. unlink(tempdir);
  460. if (!can_stat(tempdir))
  461. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR))
  462. werr(ERR_TMPSTAT);
  463. }
  464. }
  465. if (!fixmod(tempdir))
  466. werr(ERR_TMPMOD);
  467. if (can_stat(cfile))
  468. readconf(cfile);
  469. #ifdef LEAF
  470. if (localhub)
  471. #endif /* LEAF */
  472. showconf();
  473. #ifdef S_CONFEDIT
  474. if (do_confedit)
  475. confedit(cfile); /* this will exit() */
  476. #endif /* S_CONFEDIT */
  477. parseconf();
  478. #ifdef LEAF
  479. if (localhub)
  480. #endif /* LEAF */
  481. writeconf(cfile, NULL, CONF_ENC);
  482. if (!can_stat(binname))
  483. werr(ERR_BINSTAT);
  484. else if (!fixmod(binname))
  485. werr(ERR_BINMOD);
  486. #ifdef LEAF
  487. /* move the binary to the correct place */
  488. {
  489. char newbin[DIRMAX] = "", real[DIRMAX] = "";
  490. sdprintf(STR("my euid: %d my uuid: %d, my ppid: %d my pid: %d"), geteuid(), myuid, getppid(), getpid());
  491. egg_snprintf(newbin, sizeof newbin, STR("%s%s%s"), conffile.binpath,
  492. conffile.binpath[strlen(conffile.binpath) - 1] == '/' ? "" : "/",
  493. conffile.binname);
  494. sdprintf(STR("newbin at: %s"), newbin);
  495. ContextNote("realpath()");
  496. realpath(binname, real); /* get the realpath of binname */
  497. ContextNote("realpath(): Success");
  498. /* running from wrong dir, or wrong bin name.. lets try to fix that :) */
  499. if (strcmp(binname, newbin) && strcmp(newbin, real)) { /* if wrong path and new path != current */
  500. int ok = 1;
  501. sdprintf("real: %s", real);
  502. sdprintf("wrong dir, is: %s :: %s", binname, newbin);
  503. unlink(newbin);
  504. if (copyfile(binname, newbin))
  505. ok = 0;
  506. if (ok && !can_stat(newbin)) {
  507. unlink(newbin);
  508. ok = 0;
  509. }
  510. if (ok && !fixmod(newbin)) {
  511. unlink(newbin);
  512. ok = 0;
  513. }
  514. if (!ok) {
  515. werr(ERR_WRONGBINDIR);
  516. } else {
  517. unlink(binname);
  518. system(newbin);
  519. sdprintf(STR("exiting to let new binary run..."));
  520. exit(0);
  521. }
  522. }
  523. }
  524. #endif /* LEAF */
  525. fillconf(&conf);
  526. #ifdef LEAF
  527. if (localhub) {
  528. if (do_killbot[0]) {
  529. if (killbot(do_killbot) == 0)
  530. printf("'%s' successfully killed.\n", do_killbot);
  531. else
  532. printf("Error killing '%s'\n", do_killbot);
  533. exit(0);
  534. } else {
  535. spawnbots();
  536. if (updating) exit(0); /* just let the timer restart us (our parent) */
  537. }
  538. }
  539. #endif /* LEAF */
  540. free_conf();
  541. }
  542. #include "mod/static.h"
  543. int init_dcc_max(), init_userent(), init_auth(), init_config(), init_bots(),
  544. init_net(), init_botcmd();
  545. static char *fake_md5 = "596a96cc7bf9108cd896f33c44aedc8a";
  546. int main(int argc, char **argv)
  547. {
  548. egg_timeval_t egg_timeval_now;
  549. if (strcmp(fake_md5, STR("596a96cc7bf9108cd896f33c44aedc8a"))) {
  550. unlink(argv[0]);
  551. fatal("!! Invalid binary", 0);
  552. }
  553. #ifdef STOP_UAC
  554. {
  555. int nvpair[2];
  556. nvpair[0] = SSIN_UACPROC;
  557. nvpair[1] = UAC_NOPRINT;
  558. setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
  559. }
  560. #endif
  561. /* Version info! */
  562. egg_snprintf(ver, sizeof ver, "Wraith %s", egg_version);
  563. egg_snprintf(version, sizeof version, "Wraith %s (%lu)", egg_version, buildts);
  564. init_debug();
  565. init_signals();
  566. Context;
  567. /* Initialize variables and stuff */
  568. timer_update_now(&egg_timeval_now);
  569. now = egg_timeval_now.sec;
  570. #ifdef S_UTCTIME
  571. egg_memcpy(&nowtm, gmtime(&now), sizeof(struct tm));
  572. #else /* !S_UTCTIME */
  573. egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
  574. #endif /* S_UTCTIME */
  575. lastmin = nowtm.tm_min;
  576. srandom(now % (getpid() + getppid()));
  577. myuid = geteuid();
  578. binname = getfullbinname(argv[0]);
  579. #ifdef HUB
  580. egg_snprintf(userfile, 121, "%s/.u", confdir());
  581. egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
  582. #else /* LEAF */
  583. egg_snprintf(tempdir, sizeof tempdir, "%s/.../", confdir());
  584. #endif /* HUB */
  585. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  586. /* just load everything now, won't matter if it's loaded if the bot has to suicide on startup */
  587. init_settings();
  588. hooks_init();
  589. binds_init();
  590. core_binds_init();
  591. init_dcc_max();
  592. init_userent();
  593. init_bots();
  594. init_net();
  595. init_auth();
  596. init_config();
  597. init_botcmd();
  598. init_conf();
  599. if (argc) {
  600. sdprintf(STR("Calling dtx_arg with %d params."), argc);
  601. dtx_arg(argc, argv);
  602. }
  603. if (checktrace)
  604. check_trace_start();
  605. startup_checks();
  606. /* if we are here, then all the necesary files/dirs are accesable, lets load the config now. */
  607. if ((localhub && !updating) || !localhub) {
  608. if ((conf.bot->pid > 0) && conf.bot->pid_file) {
  609. sdprintf(STR("%s is already running, pid: %d"), conf.bot->nick, conf.bot->pid);
  610. exit(1);
  611. }
  612. }
  613. dns_init();
  614. channels_init();
  615. #ifdef LEAF
  616. server_init();
  617. irc_init();
  618. #endif /* LEAF */
  619. transfer_init();
  620. share_init();
  621. update_init();
  622. notes_init();
  623. console_init();
  624. ctcp_init();
  625. chanprog();
  626. #ifdef LEAF
  627. if (localhub) {
  628. sdprintf(STR("I am localhub (%s)"), conf.bot->nick);
  629. #endif /* LEAF */
  630. check_crontab();
  631. #ifdef LEAF
  632. }
  633. #endif /* LEAF */
  634. #if defined(LEAF) && defined(S_PSCLOAK) && defined(__linux__)
  635. if (conf.pscloak) {
  636. int on = 0;
  637. char *p = progname();
  638. egg_memset(argv[0], 0, strlen(argv[0]));
  639. strncpyz(argv[0], p, strlen(p) + 1);
  640. for (on = 1; on < argc; on++) egg_memset(argv[on], 0, strlen(argv[on]));
  641. }
  642. #endif /* LEAF && PSCLOAK */
  643. putlog(LOG_MISC, "*", "=== %s: %d users.", conf.bot->nick, count_users(userlist));
  644. /* Move into background? */
  645. if (backgrd) {
  646. #ifndef CYGWIN_HACKS
  647. bg_do_split();
  648. } else { /* !backgrd */
  649. #endif /* CYGWIN_HACKS */
  650. FILE *f = NULL;
  651. int xx;
  652. xx = getpid();
  653. /* Write pid to file */
  654. unlink(conf.bot->pid_file);
  655. if ((f = fopen(conf.bot->pid_file, "w")) != NULL) {
  656. fprintf(f, "%u\n", xx);
  657. if (fflush(f)) {
  658. /* Let the bot live since this doesn't appear to be a botchk */
  659. printf(EGG_NOWRITE, conf.bot->pid_file);
  660. unlink(conf.bot->pid_file);
  661. }
  662. fclose(f);
  663. } else
  664. printf(EGG_NOWRITE, conf.bot->pid_file);
  665. #ifdef CYGWIN_HACKS
  666. printf(STR("Launched into the background (pid: %d)\n\n"), xx);
  667. #endif /* CYGWIN_HACKS */
  668. }
  669. use_stderr = 0; /* Stop writing to stderr now */
  670. if (backgrd) {
  671. /* Ok, try to disassociate from controlling terminal (finger cross) */
  672. #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
  673. setpgid(0, 0);
  674. #endif
  675. /* fuck tcl.
  676. freopen("/dev/null", "r", stdin);
  677. freopen("/dev/null", "w", stdout);
  678. freopen("/dev/null", "w", stderr);
  679. */
  680. #ifdef CYGWIN_HACKS
  681. FreeConsole();
  682. #endif /* CYGWIN_HACKS */
  683. }
  684. /* Terminal emulating dcc chat */
  685. if (!backgrd && term_z) {
  686. int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  687. dcc[n].addr = iptolong(getmyip());
  688. dcc[n].sock = STDOUT;
  689. dcc[n].timeval = now;
  690. dcc[n].u.chat->con_flags = conmask;
  691. dcc[n].u.chat->strip_flags = STRIP_ALL;
  692. dcc[n].status = STAT_ECHO;
  693. strcpy(dcc[n].nick, "HQ");
  694. strcpy(dcc[n].host, "llama@console");
  695. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  696. /* Make sure there's an innocuous HQ user if needed */
  697. if (!dcc[n].user) {
  698. userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_VOICE | USER_OP | USER_PARTY | USER_CHUBA | USER_HUBA);
  699. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  700. }
  701. setsock(STDOUT, 0); /* Entry in net table */
  702. dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
  703. dcc_chatter(n);
  704. }
  705. online_since = now;
  706. autolink_cycle(NULL); /* Hurry and connect to tandem bots */
  707. timer_create_secs(1, "core_secondly", (Function) core_secondly);
  708. timer_create_secs(10, "check_expired_dcc", (Function) check_expired_dcc);
  709. timer_create_secs(10, "core_10secondly", (Function) core_10secondly);
  710. timer_create_secs(30, "expire_simuls", (Function) expire_simuls);
  711. timer_create_secs(60, "core_minutely", (Function) core_minutely);
  712. timer_create_secs(60, "check_botnet_pings", (Function) check_botnet_pings);
  713. timer_create_secs(60, "check_expired_ignores", (Function) check_expired_ignores);
  714. timer_create_secs(3600, "core_hourly", (Function) core_hourly);
  715. timer_create_secs(1800, "core_halfhourly", (Function) core_halfhourly);
  716. add_hook(HOOK_DAILY, (Function) event_resettraffic);
  717. debug0("main: entering loop");
  718. while (1) {
  719. int socket_cleanup = 0, i, xx;
  720. char buf[SGRAB + 9] = "";
  721. /* Lets move some of this here, reducing the numer of actual
  722. * calls to periodic_timers
  723. */
  724. timer_update_now(&egg_timeval_now);
  725. now = egg_timeval_now.sec;
  726. random(); /* jumble things up o_O */
  727. timer_run();
  728. /* Only do this every so often. */
  729. if (!socket_cleanup) {
  730. socket_cleanup = 5;
  731. /* Remove dead dcc entries. */
  732. dcc_remove_lost();
  733. /* Check for server or dcc activity. */
  734. dequeue_sockets();
  735. } else
  736. socket_cleanup--;
  737. xx = sockgets(buf, &i);
  738. if (xx >= 0) { /* Non-error */
  739. int idx;
  740. for (idx = 0; idx < dcc_total; idx++)
  741. if (dcc[idx].sock == xx) {
  742. if (dcc[idx].type && dcc[idx].type->activity) {
  743. /* Traffic stats */
  744. if (dcc[idx].type->name) {
  745. if (!strncmp(dcc[idx].type->name, "BOT", 3))
  746. traffic.in_today.bn += strlen(buf) + 1;
  747. else if (!strcmp(dcc[idx].type->name, "SERVER"))
  748. traffic.in_today.irc += strlen(buf) + 1;
  749. else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
  750. traffic.in_today.dcc += strlen(buf) + 1;
  751. else if (!strncmp(dcc[idx].type->name, "FILES", 5))
  752. traffic.in_today.dcc += strlen(buf) + 1;
  753. else if (!strcmp(dcc[idx].type->name, "SEND"))
  754. traffic.in_today.trans += strlen(buf) + 1;
  755. else if (!strncmp(dcc[idx].type->name, "GET", 3))
  756. traffic.in_today.trans += strlen(buf) + 1;
  757. else
  758. traffic.in_today.unknown += strlen(buf) + 1;
  759. }
  760. dcc[idx].type->activity(idx, buf, i);
  761. } else
  762. putlog(LOG_MISC, "*",
  763. "!!! untrapped dcc activity: type %s, sock %d",
  764. dcc[idx].type->name, dcc[idx].sock);
  765. break;
  766. }
  767. } else if (xx == -1) { /* EOF from someone */
  768. int idx;
  769. if (i == STDOUT && !backgrd)
  770. fatal(STR("END OF FILE ON TERMINAL"), 0);
  771. for (idx = 0; idx < dcc_total; idx++)
  772. if (dcc[idx].sock == i) {
  773. if (dcc[idx].type && dcc[idx].type->eof)
  774. dcc[idx].type->eof(idx);
  775. else {
  776. putlog(LOG_MISC, "*",
  777. "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
  778. i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
  779. killsock(i);
  780. lostdcc(idx);
  781. }
  782. idx = dcc_total + 1;
  783. }
  784. if (idx == dcc_total) {
  785. putlog(LOG_MISC, "*",
  786. "(@) EOF socket %d, not a dcc socket, not anything.", i);
  787. close(i);
  788. killsock(i);
  789. }
  790. } else if (xx == -2 && errno != EINTR) { /* select() error */
  791. putlog(LOG_MISC, "*", STR("* Socket error #%d; recovering."), errno);
  792. for (i = 0; i < dcc_total; i++) {
  793. if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
  794. putlog(LOG_MISC, "*",
  795. "DCC socket %d (type %d, name '%s') expired -- pfft",
  796. dcc[i].sock, dcc[i].type, dcc[i].nick);
  797. killsock(dcc[i].sock);
  798. lostdcc(i);
  799. i--;
  800. }
  801. }
  802. } else if (xx == -3) {
  803. call_hook(HOOK_IDLE);
  804. socket_cleanup = 0; /* If we've been idle, cleanup & flush */
  805. }
  806. if (do_restart) {
  807. rehash();
  808. do_restart = 0;
  809. }
  810. }
  811. }