main.c 26 KB

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