main.c 27 KB

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