main.c 26 KB

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