main.c 26 KB

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