main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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[1024] = "1.2.1";
  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. strncpyz(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. strncpyz(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. updating = (i == 'u' ? 1 : 2); /* use 2 if 'U' to not kill/spawn bots. */
  323. break;
  324. } else
  325. exit(0);
  326. case 'v':
  327. {
  328. char date[50] = "";
  329. egg_strftime(date, sizeof date, "%c %Z", gmtime(&buildts));
  330. printf("%s\nBuild Date: %s (%lu)\n", version, date, buildts);
  331. if (settings.uname[0]) {
  332. sdebug++;
  333. bin_to_conf();
  334. }
  335. exit(0);
  336. }
  337. #ifdef LEAF
  338. case 'L':
  339. {
  340. localhub_pid = checkpid(optarg, NULL);
  341. break;
  342. }
  343. case 'P':
  344. if (atoi(optarg) && localhub_pid && (atoi(optarg) != localhub_pid))
  345. exit(3);
  346. else
  347. sdprintf("Updating...");
  348. localhub = 1;
  349. updating = 1;
  350. break;
  351. #endif
  352. case '?':
  353. default:
  354. break;
  355. }
  356. }
  357. }
  358. /* Timer info */
  359. static time_t lastmin = 99;
  360. static struct tm nowtm;
  361. void core_10secondly()
  362. {
  363. #ifndef CYGWIN_HACKS
  364. static int curcheck = 0;
  365. curcheck++;
  366. if (curcheck == 1)
  367. check_trace(0);
  368. #ifdef LEAF
  369. if (localhub) {
  370. #endif /* LEAF */
  371. check_promisc();
  372. if (curcheck == 2)
  373. check_last();
  374. if (curcheck == 3) {
  375. check_processes();
  376. curcheck = 0;
  377. }
  378. #ifdef LEAF
  379. }
  380. #endif /* LEAF */
  381. #endif /* !CYGWIN_HACKS */
  382. }
  383. /* Traffic stats
  384. */
  385. egg_traffic_t traffic;
  386. static void event_resettraffic()
  387. {
  388. traffic.out_total.irc += traffic.out_today.irc;
  389. traffic.out_total.bn += traffic.out_today.bn;
  390. traffic.out_total.dcc += traffic.out_today.dcc;
  391. traffic.out_total.filesys += traffic.out_today.filesys;
  392. traffic.out_total.trans += traffic.out_today.trans;
  393. traffic.out_total.unknown += traffic.out_today.unknown;
  394. traffic.in_total.irc += traffic.in_today.irc;
  395. traffic.in_total.bn += traffic.in_today.bn;
  396. traffic.in_total.dcc += traffic.in_today.dcc;
  397. traffic.in_total.filesys += traffic.in_today.filesys;
  398. traffic.in_total.trans += traffic.in_today.trans;
  399. traffic.in_total.unknown += traffic.in_today.unknown;
  400. egg_memset(&traffic.out_today, 0, sizeof(traffic.out_today));
  401. egg_memset(&traffic.in_today, 0, sizeof(traffic.in_today));
  402. }
  403. static void core_secondly()
  404. {
  405. static int cnt = 0;
  406. time_t miltime;
  407. #ifdef CRAZY_TRACE
  408. if (!attached) crazy_trace();
  409. #endif /* CRAZY_TRACE */
  410. if (fork_interval && backgrd && ((now - lastfork) > fork_interval))
  411. do_fork();
  412. cnt++;
  413. if ((cnt % 30) == 0) {
  414. autolink_cycle(NULL); /* attempt autolinks */
  415. cnt = 0;
  416. }
  417. egg_memcpy(&nowtm, gmtime(&now), sizeof(struct tm));
  418. if (nowtm.tm_min != lastmin) {
  419. time_t i = 0;
  420. /* Once a minute */
  421. lastmin = (lastmin + 1) % 60;
  422. /* In case for some reason more than 1 min has passed: */
  423. while (nowtm.tm_min != lastmin) {
  424. /* Timer drift, dammit */
  425. debug2("timer: drift (lastmin=%lu, now=%d)", lastmin, nowtm.tm_min);
  426. i++;
  427. lastmin = (lastmin + 1) % 60;
  428. }
  429. if (i > 1)
  430. putlog(LOG_MISC, "*", "(!) timer drift -- spun %lu minutes", i);
  431. miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
  432. if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
  433. /* flushlogs(); */
  434. if (!miltime) { /* At midnight */
  435. char s[25] = "";
  436. strncpyz(s, ctime(&now), sizeof s);
  437. #ifdef HUB
  438. putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
  439. backup_userfile();
  440. #endif /* HUB */
  441. }
  442. }
  443. /* These no longer need checking since they are all check vs minutely
  444. * settings and we only get this far on the minute.
  445. */
  446. if (miltime == 300)
  447. event_resettraffic();
  448. }
  449. }
  450. /*
  451. static void check_autoaway()
  452. {
  453. for (int i = 0; i < dcc_total; i++)
  454. if (dcc[i].type && dcc[i].type == &DCC_CHAT && !(dcc[i].u.chat->away) && ((now - dcc[i].timeval) >= 600))
  455. set_away(i, "Auto away after 10 minutes.");
  456. }
  457. */
  458. static void core_minutely()
  459. {
  460. #ifdef HUB
  461. send_timesync(-1);
  462. #endif /* HUB */
  463. #ifdef LEAF
  464. check_maxfiles();
  465. check_mypid();
  466. #endif
  467. check_bind_time(&nowtm);
  468. // check_autoaway();
  469. /* flushlogs(); */
  470. }
  471. static void core_hourly()
  472. {
  473. }
  474. static void core_halfhourly()
  475. {
  476. #ifdef HUB
  477. write_userfile(-1);
  478. #endif /* HUB */
  479. }
  480. static void check_tempdir()
  481. {
  482. char *_confdir = confdir();
  483. if (_confdir && !can_stat(_confdir)) {
  484. if (mkdir(_confdir, S_IRUSR | S_IWUSR | S_IXUSR)) {
  485. unlink(_confdir);
  486. if (!can_stat(_confdir))
  487. mkdir(_confdir, S_IRUSR | S_IWUSR | S_IXUSR);
  488. }
  489. }
  490. fixmod(_confdir);
  491. if (!can_stat(tempdir)) {
  492. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR)) {
  493. unlink(tempdir);
  494. if (!can_stat(tempdir))
  495. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR))
  496. werr(ERR_TMPSTAT);
  497. }
  498. }
  499. if (fixmod(tempdir))
  500. werr(ERR_TMPMOD);
  501. /* test tempdir: it's vital */
  502. {
  503. Tempfile *testdir = new Tempfile("test");
  504. int result;
  505. fprintf(testdir->f, "\n");
  506. result = fflush(testdir->f);
  507. delete testdir;
  508. if (result)
  509. fatal(strerror(errno), 0);
  510. }
  511. }
  512. /* FIXME: Remove after 1.2 (the hacks) */
  513. static void startup_checks(int hack) {
  514. /* for compatability with old conf files
  515. * only check/use conf file if it exists and settings.uname is empty.
  516. * if settings.uname is NOT empty, just erase the conf file if it exists
  517. * otherwise, assume we're working only with the struct */
  518. check_tempdir();
  519. #ifdef CYGWIN_HACKS
  520. egg_snprintf(cfile, sizeof cfile, STR("%s/conf.txt"), confdir());
  521. if (can_stat(cfile))
  522. readconf(cfile, 0); /* will read into &conffile struct */
  523. #endif /* CYGWIN_HACKS */
  524. #ifndef CYGWIN_HACKS
  525. if (settings.uname[0])
  526. bin_to_conf(); /* read our memory from settings[] into conf[] */
  527. if (do_confedit)
  528. confedit(); /* this will exit() */
  529. #endif /* !CYGWIN_HACKS */
  530. parseconf(1);
  531. if (!can_stat(binname))
  532. werr(ERR_BINSTAT);
  533. else if (fixmod(binname))
  534. werr(ERR_BINMOD);
  535. #ifndef CYGWIN_HACKS
  536. move_bin(conffile.binpath, conffile.binname, 1);
  537. #endif /* !CYGWIN_HACKS */
  538. fillconf(&conf);
  539. #ifdef LEAF
  540. if (localhub && !used_B) {
  541. if (do_killbot[0]) {
  542. if (killbot(do_killbot) == 0)
  543. printf("'%s' successfully killed.\n", do_killbot);
  544. else
  545. printf("Error killing '%s'\n", do_killbot);
  546. exit(0);
  547. } else {
  548. #endif /* LEAF */
  549. /* this needs to be both hub/leaf */
  550. if (update_bin) { /* invoked with -u bin */
  551. if (updating != 2 && conf.bot->pid) {
  552. kill(conf.bot->pid, SIGKILL);
  553. unlink(conf.bot->pid_file);
  554. writepid(conf.bot->pid_file, getpid());
  555. }
  556. updatebin(DP_STDOUT, update_bin, 1); /* will call restart all bots */
  557. /* never reached */
  558. }
  559. #ifdef LEAF
  560. spawnbots();
  561. exit(0); /* our job is done! */
  562. }
  563. }
  564. if (!localhub) /* only clear conf on NON localhubs, we need it for cmd_conf */
  565. free_conf();
  566. #endif /* LEAF */
  567. }
  568. int init_dcc_max(), init_userent(), init_auth(), init_config(), init_party(),
  569. init_net(), init_botcmd();
  570. static char *fake_md5 = "596a96cc7bf9108cd896f33c44aedc8a";
  571. void console_init();
  572. void ctcp_init();
  573. void update_init();
  574. void notes_init();
  575. #ifdef LEAF
  576. void server_init();
  577. void irc_init();
  578. #endif /* LEAF */
  579. void channels_init();
  580. void compress_init();
  581. void share_init();
  582. void transfer_init();
  583. int main(int argc, char **argv)
  584. {
  585. egg_timeval_t egg_timeval_now;
  586. /* Initialize variables and stuff */
  587. timer_update_now(&egg_timeval_now);
  588. now = egg_timeval_now.sec;
  589. srandom(now % (getpid() + getppid()) * randint(1000));
  590. Context; /* FIXME: wtf is this here for?, probably some old hack to fix a corrupt heap */
  591. /*
  592. char *out = NULL;
  593. printf("ret: %d\n", system("c:/wraith/leaf.exe"));
  594. shell_exec("c:\\windows\\notepad.exe", NULL, &out, &out);
  595. printf("out: %s\n", out);
  596. */
  597. setlimits();
  598. init_debug();
  599. init_signals();
  600. if (strcmp(fake_md5, STR("596a96cc7bf9108cd896f33c44aedc8a"))) {
  601. unlink(argv[0]);
  602. fatal("!! Invalid binary", 0);
  603. }
  604. binname = getfullbinname(argv[0]);
  605. /* This allows -2/-0 to be used without an initialized binary */
  606. // if (!(argc == 2 && (!strcmp(argv[1], "-2") || !strcmp(argv[1], "0")))) {
  607. // doesn't work correctly yet, if we don't go in here, our settings stay encrypted
  608. check_sum(binname, argc >= 3 && !strcmp(argv[1], "-p") ? argv[2] : NULL);
  609. if (!checked_bin_buf)
  610. exit(1);
  611. // }
  612. /* Now settings struct is filled */
  613. #ifdef STOP_UAC
  614. {
  615. int nvpair[2] = { SSIN_UACPROC, UAC_NOPRINT };
  616. setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
  617. }
  618. #endif
  619. /* Version info! */
  620. egg_snprintf(ver, sizeof ver, "[%s] Wraith %s", settings.packname, egg_version);
  621. egg_snprintf(version, sizeof version, "[%s] Wraith %s (%lu)", settings.packname, egg_version, buildts);
  622. Context;
  623. egg_memcpy(&nowtm, gmtime(&now), sizeof(struct tm));
  624. lastmin = nowtm.tm_min;
  625. myuid = geteuid();
  626. #ifdef HUB
  627. egg_snprintf(userfile, 121, "%s/.u", confdir());
  628. #endif /* HUB */
  629. #ifdef HUB
  630. egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
  631. #endif /* HUB */
  632. #ifdef LEAF
  633. egg_snprintf(tempdir, sizeof tempdir, "%s/.../", confdir());
  634. #endif /* LEAF */
  635. #ifdef CYGWIN_HACKS
  636. egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
  637. #endif /* CYGWIN_HACKS */
  638. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  639. /* just load everything now, won't matter if it's loaded if the bot has to suicide on startup */
  640. init_flags();
  641. binds_init();
  642. core_binds_init();
  643. init_dcc_max();
  644. init_userent();
  645. init_party();
  646. init_net();
  647. init_auth();
  648. init_config();
  649. init_botcmd();
  650. init_conf();
  651. init_responses();
  652. if (argc) {
  653. sdprintf("Calling dtx_arg with %d params.", argc);
  654. dtx_arg(argc, argv);
  655. }
  656. sdprintf("my euid: %d my uuid: %d, my ppid: %d my pid: %d", geteuid(), myuid, getppid(), getpid());
  657. #ifndef CYGWIN_HACKS
  658. if (checktrace)
  659. check_trace(1);
  660. #endif /* !CYGWIN_HACKS */
  661. /* Check and load conf file */
  662. startup_checks(0);
  663. if ((localhub && !updating) || !localhub) {
  664. if ((conf.bot->pid > 0) && conf.bot->pid_file) {
  665. sdprintf("%s is already running, pid: %d", conf.bot->nick, conf.bot->pid);
  666. exit(1);
  667. }
  668. }
  669. egg_dns_init();
  670. channels_init();
  671. #ifdef LEAF
  672. server_init();
  673. irc_init();
  674. #endif /* LEAF */
  675. transfer_init();
  676. share_init();
  677. update_init();
  678. notes_init();
  679. console_init();
  680. ctcp_init();
  681. chanprog();
  682. #ifdef HUB
  683. cfg_noshare = 1;
  684. if (!CFG_CHANSET.gdata)
  685. set_cfg_str(NULL, "chanset", glob_chanset);
  686. if (!CFG_SERVPORT.gdata)
  687. set_cfg_str(NULL, "servport", "6667");
  688. if (!CFG_REALNAME.gdata)
  689. set_cfg_str(NULL, "realname", "A deranged product of evil coders.");
  690. cfg_noshare = 0;
  691. #endif /* HUB */
  692. strcpy(botuser, origbotname);
  693. trigger_cfg_changed();
  694. #ifdef LEAF
  695. if (localhub) {
  696. sdprintf("I am localhub (%s)", conf.bot->nick);
  697. #endif /* LEAF */
  698. #ifndef CYGWIN_HACKS
  699. if (conffile.autocron)
  700. check_crontab();
  701. #endif /* !CYGWIN_HACKS */
  702. #ifdef LEAF
  703. }
  704. #endif /* LEAF */
  705. #if defined(LEAF) && defined(__linux__)
  706. if (conf.pscloak) {
  707. const char *p = response(RES_PSCLOAK);
  708. for (int argi = 0; argi < argc; argi++)
  709. egg_memset(argv[argi], 0, strlen(argv[argi]));
  710. strcpy(argv[0], p);
  711. }
  712. #endif /* LEAF */
  713. /* Move into background? */
  714. /* we don't split cygwin because to run as a service the bot shouldn't exit.
  715. confuses windows ;)
  716. */
  717. use_stderr = 0; /* stop writing to stderr now! */
  718. if (backgrd) {
  719. #ifndef CYGWIN_HACKS
  720. pid_t pid = 0;
  721. pid = do_fork();
  722. /*
  723. printf(" |- %-10s (%d)\n", conf.bot->nick, pid);
  724. if (localhub) {
  725. if (bots_ran)
  726. printf(" `- %d bots launched\n", bots_ran + 1);
  727. else
  728. printf(" `- 1 bot launched\n");
  729. }
  730. */
  731. printf("%s[%s%s%s]%s -%s- initiated %s(%s%d%s)%s\n",
  732. BOLD(-1), BOLD_END(-1), settings.packname, BOLD(-1), BOLD_END(-1), conf.bot->nick,
  733. BOLD(-1), BOLD_END(-1), pid, BOLD(-1), BOLD_END(-1));
  734. #ifdef lame /* keeping for god knows why */
  735. printf("%s%s%c%s%s%s l%sA%su%sN%sc%sH%se%sD%s %s(%s%d%s)%s\n",
  736. RED(-1), BOLD(-1), conf.bot->nick[0], BOLD_END(-1), &conf.bot->nick[1],
  737. COLOR_END(-1), BOLD(-1), BOLD_END(-1), BOLD(-1), BOLD_END(-1), BOLD(-1), BOLD_END(-1),
  738. BOLD(-1), BOLD_END(-1), YELLOW(-1), COLOR_END(-1), pid, YELLOW(-1), COLOR_END(-1));
  739. #endif
  740. close_tty();
  741. } else {
  742. #endif /* !CYGWIN_HACKS */
  743. #ifdef CYGWIN_HACKS
  744. FreeConsole();
  745. #endif /* CYGWIN_HACKS */
  746. printf("%s[%s%s%s]%s -%s- initiated\n", BOLD(-1), BOLD_END(-1), settings.packname, BOLD(-1), BOLD_END(-1), conf.bot->nick);
  747. writepid(conf.bot->pid_file, getpid());
  748. }
  749. /* Terminal emulating dcc chat */
  750. if (!backgrd && term_z) {
  751. int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  752. dcc[n].addr = iptolong(getmyip());
  753. dcc[n].sock = STDOUT;
  754. dcc[n].timeval = now;
  755. dcc[n].u.chat->con_flags = conmask;
  756. dcc[n].u.chat->strip_flags = STRIP_ALL;
  757. dcc[n].status = STAT_ECHO;
  758. strcpy(dcc[n].nick, "HQ");
  759. strcpy(dcc[n].host, "llama@console");
  760. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  761. /* Make sure there's an innocuous HQ user if needed */
  762. if (!dcc[n].user) {
  763. userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_VOICE | USER_OP | USER_PARTY | USER_CHUBA | USER_HUBA, 0);
  764. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  765. }
  766. setsock(STDOUT, 0); /* Entry in net table */
  767. dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
  768. dcc_chatter(n);
  769. }
  770. online_since = now;
  771. autolink_cycle(NULL); /* Hurry and connect to tandem bots */
  772. timer_create_secs(1, "core_secondly", (Function) core_secondly);
  773. timer_create_secs(10, "check_expired_dcc", (Function) check_expired_dcc);
  774. timer_create_secs(10, "core_10secondly", (Function) core_10secondly);
  775. timer_create_secs(30, "expire_simuls", (Function) expire_simuls);
  776. timer_create_secs(60, "core_minutely", (Function) core_minutely);
  777. timer_create_secs(60, "check_botnet_pings", (Function) check_botnet_pings);
  778. timer_create_secs(60, "check_expired_ignores", (Function) check_expired_ignores);
  779. timer_create_secs(3600, "core_hourly", (Function) core_hourly);
  780. timer_create_secs(1800, "core_halfhourly", (Function) core_halfhourly);
  781. debug0("main: entering loop");
  782. int socket_cleanup = 0, status = 0, xx, i = 0, idx = 0;
  783. char buf[SGRAB + 10] = "";
  784. while (1) {
  785. #ifndef CYGWIN_HACKS
  786. if (conf.watcher && waitpid(watcher, &status, WNOHANG))
  787. fatal("watcher PID died/stopped", 0);
  788. #endif /* !CYGWIN_HACKS */
  789. /* Lets move some of this here, reducing the numer of actual
  790. * calls to periodic_timers
  791. */
  792. timer_update_now(&egg_timeval_now);
  793. now = egg_timeval_now.sec;
  794. random(); /* jumble things up o_O */
  795. timer_run();
  796. /* Only do this every so often. */
  797. if (!socket_cleanup) {
  798. socket_cleanup = 5;
  799. /* Check for server or dcc activity. */
  800. dequeue_sockets();
  801. } else
  802. socket_cleanup--;
  803. xx = sockgets(buf, &i);
  804. if (xx >= 0) { /* Non-error */
  805. for (idx = 0; idx < dcc_total; idx++) {
  806. if (dcc[idx].type && dcc[idx].sock == xx) {
  807. if (dcc[idx].type && dcc[idx].type->activity) {
  808. /* Traffic stats */
  809. if (dcc[idx].type->name) {
  810. if (!strncmp(dcc[idx].type->name, "BOT", 3))
  811. traffic.in_today.bn += strlen(buf) + 1;
  812. else if (!strcmp(dcc[idx].type->name, "SERVER"))
  813. traffic.in_today.irc += strlen(buf) + 1;
  814. else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
  815. traffic.in_today.dcc += strlen(buf) + 1;
  816. else if (!strncmp(dcc[idx].type->name, "FILES", 5))
  817. traffic.in_today.dcc += strlen(buf) + 1;
  818. else if (!strcmp(dcc[idx].type->name, "SEND"))
  819. traffic.in_today.trans += strlen(buf) + 1;
  820. else if (!strncmp(dcc[idx].type->name, "GET", 3))
  821. traffic.in_today.trans += strlen(buf) + 1;
  822. else
  823. traffic.in_today.unknown += strlen(buf) + 1;
  824. }
  825. dcc[idx].type->activity(idx, buf, i);
  826. } else
  827. putlog(LOG_MISC, "*",
  828. "!!! untrapped dcc activity: type %s, sock %d",
  829. dcc[idx].type->name, dcc[idx].sock);
  830. break;
  831. }
  832. }
  833. } else if (xx == -1) { /* EOF from someone */
  834. if (i == STDOUT && !backgrd)
  835. fatal("END OF FILE ON TERMINAL", 0);
  836. for (idx = 0; idx < dcc_total; idx++) {
  837. if (dcc[idx].type && dcc[idx].sock == i) {
  838. sdprintf("EOF on '%s' idx: %d", dcc[idx].type ? dcc[idx].type->name : "unknown", idx);
  839. if (dcc[idx].type->eof)
  840. dcc[idx].type->eof(idx);
  841. else {
  842. putlog(LOG_MISC, "*",
  843. "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
  844. i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
  845. killsock(i);
  846. lostdcc(idx);
  847. }
  848. idx = dcc_total + 1;
  849. }
  850. }
  851. if (idx == dcc_total) {
  852. putlog(LOG_MISC, "*", "(@) EOF socket %d, not a dcc socket, not anything.", i);
  853. close(i);
  854. killsock(i);
  855. }
  856. } else if (xx == -2 && errno != EINTR) { /* select() error */
  857. putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
  858. for (i = 0; i < dcc_total; i++) {
  859. if (dcc[i].type && dcc[i].sock != -1 && (fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
  860. putlog(LOG_MISC, "*",
  861. "DCC socket %d (type %s, name '%s') expired -- pfft",
  862. dcc[i].sock, dcc[i].type->name, dcc[i].nick);
  863. killsock(dcc[i].sock);
  864. lostdcc(i);
  865. i--;
  866. }
  867. }
  868. } else if (xx == -3) {
  869. #ifdef LEAF
  870. flush_modes();
  871. #endif /* LEAF */
  872. socket_cleanup = 0; /* If we've been idle, cleanup & flush */
  873. }
  874. }
  875. return 0; /* never reached but what the hell */
  876. }