main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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 "binary.h"
  11. #include "dcc.h"
  12. #include "misc.h"
  13. #include "settings.h"
  14. #include "salt.h"
  15. #include "misc_file.h"
  16. #include "net.h"
  17. #include "users.h"
  18. #include "shell.h"
  19. #include "userrec.h"
  20. #include "tclhash.h"
  21. #include "cfg.h"
  22. #include "dccutil.h"
  23. #include "crypt.h"
  24. #include "debug.h"
  25. #include "chanprog.h"
  26. #include "traffic.h"
  27. #include "bg.h"
  28. #include "botnet.h"
  29. #include "build.h"
  30. #ifdef LEAF
  31. #include "src/mod/irc.mod/irc.h"
  32. #include "src/mod/server.mod/server.h"
  33. #endif /* LEAF */
  34. /* FIXME: REMOVE AFTER 1.1.4 */
  35. #include "src/mod/channels.mod/channels.h"
  36. #include <time.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #ifdef STOP_UAC /* osf/1 complains a lot */
  40. # include <sys/sysinfo.h>
  41. # define UAC_NOPRINT /* Don't report unaligned fixups */
  42. #endif /* STOP_UAC */
  43. #include <sys/file.h>
  44. #include <sys/stat.h>
  45. #include "chan.h"
  46. #include "tandem.h"
  47. #include "egg_timer.h"
  48. #include "core_binds.h"
  49. #ifdef CYGWIN_HACKS
  50. #include <windows.h>
  51. #include <getopt.h>
  52. #endif /* CYGWIN_HACKS */
  53. #ifndef _POSIX_SOURCE
  54. /* Solaris needs this */
  55. #define _POSIX_SOURCE
  56. #endif
  57. extern int optind;
  58. const time_t buildts = CVSBUILD; /* build timestamp (UTC) */
  59. const char egg_version[1024] = "1.1.6";
  60. int localhub = 1; /* we set this to 0 if we get a -B */
  61. int role;
  62. int loading = 0;
  63. int default_flags = 0; /* Default user flags and */
  64. int default_uflags = 0; /* Default userdefinied flags for people
  65. who say 'hello' or for .adduser */
  66. int backgrd = 1; /* Run in the background? */
  67. uid_t myuid;
  68. int term_z = 0; /* Foreground: use the terminal as a party line? */
  69. int updating = 0; /* this is set when the binary is called from itself. */
  70. char tempdir[DIRMAX] = "";
  71. char *binname = NULL;
  72. time_t online_since; /* Unix-time that the bot loaded up */
  73. char owner[121] = ""; /* Permanent owner(s) of the bot */
  74. char version[81] = ""; /* Version info (long form) */
  75. char ver[41] = ""; /* Version info (short form) */
  76. int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
  77. char quit_msg[1024]; /* quit message */
  78. time_t now; /* duh, now :) */
  79. #define fork_interval atoi( CFG_FORKINTERVAL.ldata ? CFG_FORKINTERVAL.ldata : CFG_FORKINTERVAL.gdata ? CFG_FORKINTERVAL.gdata : "0")
  80. #ifdef S_CONFEDIT
  81. static int do_confedit = 0; /* show conf menu if -C */
  82. #endif /* S_CONFEDIT */
  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. 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. }
  162. }
  163. }
  164. }
  165. static void checkpass()
  166. {
  167. static int checkedpass = 0;
  168. if (!checkedpass) {
  169. char *gpasswd = NULL;
  170. gpasswd = (char *) getpass("bash$ ");
  171. checkedpass = 1;
  172. if (!gpasswd || (gpasswd && md5cmp(shellhash, gpasswd))) {
  173. werr(ERR_BADPASS);
  174. }
  175. }
  176. }
  177. static void got_ed(char *which, char *in, char *out)
  178. {
  179. sdprintf("got_Ed called: -%s i: %s o: %s", which, in, out);
  180. if (!in || !out)
  181. fatal(STR("Wrong number of arguments: -e/-d <infile> <outfile/STDOUT>"),0);
  182. if (!strcmp(in, out))
  183. fatal("<infile> should NOT be the same name as <outfile>", 0);
  184. if (!strcmp(which, "e")) {
  185. Encrypt_File(in, out);
  186. fatal("File Encryption complete",3);
  187. } else if (!strcmp(which, "d")) {
  188. Decrypt_File(in, out);
  189. fatal("File Decryption complete",3);
  190. }
  191. exit(0);
  192. }
  193. static void show_help()
  194. {
  195. char format[81] = "";
  196. egg_snprintf(format, sizeof format, "%%-30s %%-30s\n");
  197. printf(STR("Wraith %s\n\n"), egg_version);
  198. printf(format, "Option", "Description");
  199. printf(format, "------", "-----------");
  200. printf(format, STR("-B <botnick>"), STR("Starts the specified bot"));
  201. printf(format, STR("-C"), STR("Config file menu system"));
  202. printf(format, STR("-e <infile> <outfile>"), STR("Encrypt infile to outfile"));
  203. printf(format, STR("-d <infile> <outfile>"), STR("Decrypt infile to outfile"));
  204. printf(format, STR("-D"), STR("Enables debug mode (see -n)"));
  205. printf(format, STR("-E [#/all]"), STR("Display Error codes english translation (use 'all' to display all)"));
  206. /* printf(format, STR("-g <file>"), STR("Generates a template config file"));
  207. printf(format, STR("-G <file>"), STR("Generates a custom config for the box"));
  208. */
  209. printf(format, "-h", "Display this help listing");
  210. printf(format, STR("-k <botname>"), STR("Terminates (botname) with kill -9"));
  211. printf(format, STR("-n"), STR("Disables backgrounding first bot in conf"));
  212. printf(format, STR("-s"), STR("Disables checking for ptrace/strace during startup (no pass needed)"));
  213. printf(format, STR("-t"), STR("Enables \"Partyline\" emulation (requires -n)"));
  214. printf(format, "-v", "Displays bot version");
  215. exit(0);
  216. }
  217. #ifdef LEAF
  218. # define PARSE_FLAGS "02B:Cd:De:Eg:G:k:L:P:hnstv"
  219. #else /* !LEAF */
  220. # define PARSE_FLAGS "02Cd:De:Eg:G:hnstv"
  221. #endif /* HUB */
  222. #define FLAGS_CHECKPASS "CdDeEgGhkntv"
  223. static void dtx_arg(int argc, char *argv[])
  224. {
  225. int i = 0;
  226. #ifdef LEAF
  227. int localhub_pid = 0;
  228. #endif /* LEAF */
  229. char *p = NULL;
  230. opterr = 0;
  231. while ((i = getopt(argc, argv, PARSE_FLAGS)) != EOF) {
  232. if (strchr(FLAGS_CHECKPASS, i))
  233. checkpass();
  234. switch (i) {
  235. case '0':
  236. exit(0);
  237. case '2': /* used for testing new binary through update */
  238. exit(2);
  239. #ifdef LEAF
  240. case 'B':
  241. localhub = 0;
  242. strncpyz(origbotname, optarg, NICKLEN + 1);
  243. break;
  244. #endif /* LEAF */
  245. #ifdef S_CONFEDIT
  246. case 'C':
  247. do_confedit = 1;
  248. break;
  249. #endif /* S_CONFEDIT */
  250. case 'h':
  251. show_help();
  252. #ifdef LEAF
  253. case 'k': /* kill bot */
  254. strncpyz(do_killbot, optarg, sizeof do_killbot);
  255. #endif /* LEAF */
  256. case 'n':
  257. backgrd = 0;
  258. break;
  259. case 's':
  260. checktrace = 0;
  261. break;
  262. case 't':
  263. term_z = 1;
  264. break;
  265. case 'D':
  266. sdebug = 1;
  267. sdprintf("debug enabled");
  268. break;
  269. case 'E':
  270. p = argv[optind];
  271. if (p && p[0]) {
  272. if (!strcmp(p, "all")) {
  273. int n;
  274. putlog(LOG_MISC, "*", "Listing all errors");
  275. for (n = 1; n < ERR_MAX; n++)
  276. putlog(LOG_MISC, "*", "Error #%d: %s", n, werr_tostr(n));
  277. } else if (egg_isdigit(p[0])) {
  278. putlog(LOG_MISC, "*", "Error #%d: %s", atoi(p), werr_tostr(atoi(p)));
  279. }
  280. exit(0);
  281. } else {
  282. fatal(STR("You must specify error number after -E (or 'all')"), 0);
  283. }
  284. break;
  285. case 'e':
  286. if (argv[optind])
  287. p = argv[optind];
  288. got_ed("e", optarg, p);
  289. case 'd':
  290. if (argv[optind])
  291. p = argv[optind];
  292. got_ed("d", optarg, p);
  293. case 'v':
  294. {
  295. char date[50] = "";
  296. egg_strftime(date, sizeof date, "%c %Z", gmtime(&buildts));
  297. printf("Wraith %s\nBuild Date: %s (%lu)\n", egg_version, date, buildts);
  298. printf("SALTS\nfiles: %s\nbotlink: %s\n", SALT1, SALT2);
  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. int curcheck = 0;
  326. void core_10secondly()
  327. {
  328. curcheck++;
  329. check_promisc();
  330. if (curcheck == 1)
  331. check_trace();
  332. #ifdef LEAF
  333. if (localhub) {
  334. #endif /* LEAF */
  335. #ifndef CYGWIN_HACKS
  336. if (curcheck == 2)
  337. check_last();
  338. if (curcheck == 3) {
  339. check_processes();
  340. curcheck = 0;
  341. }
  342. #endif /* !CYGWIN_HACKS */
  343. #ifdef LEAF
  344. }
  345. #endif /* LEAF */
  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. #ifdef S_UTCTIME
  382. egg_memcpy(&nowtm, gmtime(&now), sizeof(struct tm));
  383. #else /* !S_UTCTIME */
  384. egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
  385. #endif /* S_UTCTIME */
  386. if (nowtm.tm_min != lastmin) {
  387. int i = 0;
  388. /* Once a minute */
  389. lastmin = (lastmin + 1) % 60;
  390. /* In case for some reason more than 1 min has passed: */
  391. while (nowtm.tm_min != lastmin) {
  392. /* Timer drift, dammit */
  393. debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
  394. i++;
  395. lastmin = (lastmin + 1) % 60;
  396. }
  397. if (i > 1)
  398. putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
  399. miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
  400. if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
  401. /* flushlogs(); */
  402. if (!miltime) { /* At midnight */
  403. char s[25] = "";
  404. strncpyz(s, ctime(&now), sizeof s);
  405. #ifdef HUB
  406. putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
  407. backup_userfile();
  408. #endif /* HUB */
  409. }
  410. }
  411. /* These no longer need checking since they are all check vs minutely
  412. * settings and we only get this far on the minute.
  413. */
  414. if (miltime == 300)
  415. event_resettraffic();
  416. }
  417. }
  418. static void core_minutely()
  419. {
  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. char cfile[DIRMAX] = "";
  437. int enc = CONF_ENC;
  438. #ifdef LEAF
  439. egg_snprintf(cfile, sizeof cfile, STR("%s/.known_hosts"), confdir());
  440. #endif /* LEAF */
  441. #ifdef HUB
  442. egg_snprintf(cfile, sizeof cfile, STR("%s/conf"), confdir());
  443. #endif /* HUB */
  444. #ifdef CYGWIN_HACKS
  445. egg_snprintf(cfile, sizeof cfile, STR("%s/conf.txt"), confdir());
  446. enc =~ CONF_ENC;
  447. #endif /* CYGWIN_HACKS */
  448. if (!can_stat(confdir())) {
  449. #ifdef LEAF
  450. if (mkdir(confdir(), S_IRUSR | S_IWUSR | S_IXUSR)) {
  451. unlink(confdir());
  452. if (!can_stat(confdir()))
  453. if (mkdir(confdir(), S_IRUSR | S_IWUSR | S_IXUSR))
  454. #endif /* LEAF */
  455. werr(ERR_CONFSTAT);
  456. #ifdef LEAF
  457. }
  458. #endif /* LEAF */
  459. }
  460. if (!fixmod(confdir()))
  461. werr(ERR_CONFDIRMOD);
  462. /*technically no longer needed?
  463. else if (!can_stat(cfile))
  464. werr(ERR_NOCONF);
  465. */
  466. else if (can_stat(cfile) && !fixmod(cfile))
  467. werr(ERR_CONFMOD);
  468. if (!can_stat(tempdir)) {
  469. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR)) {
  470. unlink(tempdir);
  471. if (!can_stat(tempdir))
  472. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR))
  473. werr(ERR_TMPSTAT);
  474. }
  475. }
  476. if (!fixmod(tempdir))
  477. werr(ERR_TMPMOD);
  478. /* test tempdir: it's vital */
  479. {
  480. FILE *f = NULL;
  481. char s[DIRMAX] = "";
  482. int fd;
  483. egg_snprintf(s, sizeof s, "%s.test-XXXXXX", tempdir);
  484. if ((fd = mkstemp(s)) == -1 || (f = fdopen(fd, "w")) == NULL) {
  485. if (fd != -1) {
  486. unlink(s);
  487. close(fd);
  488. }
  489. fatal("Can't write to tempdir!", 0);
  490. }
  491. fprintf(f, "\n");
  492. if (fflush(f))
  493. fatal(strerror(errno), 0);
  494. unlink(s);
  495. fclose(f);
  496. }
  497. if (can_stat(cfile))
  498. readconf(cfile, enc);
  499. #ifdef LEAF
  500. if (localhub)
  501. #endif /* LEAF */
  502. showconf();
  503. #ifdef S_CONFEDIT
  504. if (do_confedit)
  505. confedit(cfile); /* this will exit() */
  506. #endif /* S_CONFEDIT */
  507. parseconf();
  508. #ifdef LEAF
  509. if (localhub)
  510. #endif /* LEAF */
  511. writeconf(cfile, NULL, enc);
  512. if (!can_stat(binname))
  513. werr(ERR_BINSTAT);
  514. else if (!fixmod(binname))
  515. werr(ERR_BINMOD);
  516. #ifdef LEAF
  517. #ifndef CYGWIN_HACKS
  518. /* move the binary to the correct place */
  519. {
  520. char newbin[DIRMAX] = "", real[DIRMAX] = "";
  521. sdprintf("my euid: %d my uuid: %d, my ppid: %d my pid: %d", geteuid(), myuid, getppid(), getpid());
  522. egg_snprintf(newbin, sizeof newbin, "%s%s%s", conffile.binpath,
  523. conffile.binpath[strlen(conffile.binpath) - 1] == '/' ? "" : "/",
  524. conffile.binname);
  525. sdprintf("newbin at: %s", newbin);
  526. ContextNote("realpath()");
  527. realpath(binname, real); /* get the realpath of binname */
  528. ContextNote("realpath(): Success");
  529. /* running from wrong dir, or wrong bin name.. lets try to fix that :) */
  530. if (strcmp(binname, newbin) && strcmp(newbin, real)) { /* if wrong path and new path != current */
  531. int ok = 1;
  532. sdprintf("real: %s", real);
  533. sdprintf("wrong dir, is: %s :: %s", binname, newbin);
  534. unlink(newbin);
  535. if (copyfile(binname, newbin))
  536. ok = 0;
  537. if (ok && !can_stat(newbin)) {
  538. unlink(newbin);
  539. ok = 0;
  540. }
  541. if (ok && !fixmod(newbin)) {
  542. unlink(newbin);
  543. ok = 0;
  544. }
  545. if (!ok) {
  546. werr(ERR_WRONGBINDIR);
  547. } else {
  548. unlink(binname);
  549. system(newbin);
  550. sdprintf("exiting to let new binary run...");
  551. exit(0);
  552. }
  553. }
  554. }
  555. #endif /* !CYGWIN_HACKS */
  556. #endif /* LEAF */
  557. fillconf(&conf);
  558. #ifdef LEAF
  559. if (localhub) {
  560. if (do_killbot[0]) {
  561. if (killbot(do_killbot) == 0)
  562. printf("'%s' successfully killed.\n", do_killbot);
  563. else
  564. printf("Error killing '%s'\n", do_killbot);
  565. exit(0);
  566. } else {
  567. spawnbots();
  568. if (updating) exit(0); /* just let the timer restart us (our parent) */
  569. }
  570. }
  571. #endif /* LEAF */
  572. free_conf();
  573. }
  574. #include "mod/static.h"
  575. int init_dcc_max(), init_userent(), init_auth(), init_config(), init_bots(),
  576. init_net(), init_botcmd();
  577. static char *fake_md5 = "596a96cc7bf9108cd896f33c44aedc8a";
  578. int main(int argc, char **argv)
  579. {
  580. egg_timeval_t egg_timeval_now;
  581. if (strcmp(fake_md5, STR("596a96cc7bf9108cd896f33c44aedc8a"))) {
  582. unlink(argv[0]);
  583. fatal("!! Invalid binary", 0);
  584. }
  585. #ifdef STOP_UAC
  586. {
  587. int nvpair[2];
  588. nvpair[0] = SSIN_UACPROC;
  589. nvpair[1] = UAC_NOPRINT;
  590. setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
  591. }
  592. #endif
  593. /* Version info! */
  594. egg_snprintf(ver, sizeof ver, "Wraith %s", egg_version);
  595. egg_snprintf(version, sizeof version, "Wraith %s (%lu)", egg_version, buildts);
  596. init_debug();
  597. init_signals();
  598. Context;
  599. /* Initialize variables and stuff */
  600. timer_update_now(&egg_timeval_now);
  601. now = egg_timeval_now.sec;
  602. #ifdef S_UTCTIME
  603. egg_memcpy(&nowtm, gmtime(&now), sizeof(struct tm));
  604. #else /* !S_UTCTIME */
  605. egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
  606. #endif /* S_UTCTIME */
  607. lastmin = nowtm.tm_min;
  608. srandom(now % (getpid() + getppid()));
  609. myuid = geteuid();
  610. binname = getfullbinname(argv[0]);
  611. #ifndef CYGWIN_HACKS
  612. if (!encdata.data[1]) {
  613. printf("* Wrote checksum to binary. (%s)\n", bin_md5(binname, WRITE_MD5));
  614. } else {
  615. char *hash = NULL;
  616. hash = decrypt_string(SALT1, encdata.data);
  617. if (strcmp(bin_md5(binname, GET_MD5), hash)) {
  618. free(hash);
  619. unlink(argv[0]);
  620. fatal("!! Invalid binary", 0);
  621. }
  622. free(hash);
  623. }
  624. if (!checked_bin_buf)
  625. exit(1);
  626. #endif /* !CYGWIN_HACKS */
  627. #ifdef HUB
  628. egg_snprintf(userfile, 121, "%s/.u", confdir());
  629. #endif /* HUB */
  630. #ifdef HUB
  631. egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
  632. #endif /* HUB */
  633. #ifdef LEAF
  634. egg_snprintf(tempdir, sizeof tempdir, "%s/.../", confdir());
  635. #endif /* LEAF */
  636. #ifdef CYGWIN_HACKS
  637. egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
  638. #endif /* CYGWIN_HACKS */
  639. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  640. /* just load everything now, won't matter if it's loaded if the bot has to suicide on startup */
  641. init_settings();
  642. binds_init();
  643. core_binds_init();
  644. init_dcc_max();
  645. init_userent();
  646. init_bots();
  647. init_net();
  648. init_auth();
  649. init_config();
  650. init_botcmd();
  651. init_conf();
  652. if (argc) {
  653. sdprintf("Calling dtx_arg with %d params.", argc);
  654. dtx_arg(argc, argv);
  655. }
  656. if (checktrace)
  657. check_trace_start();
  658. startup_checks();
  659. /* if we are here, then all the necesary files/dirs are accesable, lets load the config now. */
  660. if ((localhub && !updating) || !localhub) {
  661. if ((conf.bot->pid > 0) && conf.bot->pid_file) {
  662. sdprintf("%s is already running, pid: %d", conf.bot->nick, conf.bot->pid);
  663. exit(1);
  664. }
  665. }
  666. dns_init();
  667. channels_init();
  668. #ifdef LEAF
  669. server_init();
  670. irc_init();
  671. #endif /* LEAF */
  672. transfer_init();
  673. share_init();
  674. update_init();
  675. notes_init();
  676. console_init();
  677. ctcp_init();
  678. chanprog();
  679. #ifdef HUB
  680. /* FIXME: Remove after 1.1.4 */
  681. /* this should only happen while upgrading to 1.1.4 */
  682. if (!CFG_CHANSET.gdata) {
  683. cfg_noshare++;
  684. set_cfg_str(NULL, "chanset", glob_chanset);
  685. cfg_noshare--;
  686. }
  687. #endif /* HUB */
  688. strcpy(botuser, origbotname);
  689. trigger_cfg_changed();
  690. #ifdef LEAF
  691. if (localhub) {
  692. sdprintf("I am localhub (%s)", conf.bot->nick);
  693. #endif /* LEAF */
  694. if (conffile.autocron)
  695. check_crontab();
  696. #ifdef LEAF
  697. }
  698. #endif /* LEAF */
  699. #if defined(LEAF) && defined(S_PSCLOAK) && defined(__linux__)
  700. if (conf.pscloak) {
  701. int on = 0;
  702. char *p = progname();
  703. egg_memset(argv[0], 0, strlen(argv[0]));
  704. strncpyz(argv[0], p, strlen(p) + 1);
  705. for (on = 1; on < argc; on++) egg_memset(argv[on], 0, strlen(argv[on]));
  706. }
  707. #endif /* LEAF && PSCLOAK */
  708. putlog(LOG_MISC, "*", "=== %s: %d users.", conf.bot->nick, count_users(userlist));
  709. /* Move into background? */
  710. /* we don't split cygwin because to run as a service the bot shouldn't exit.
  711. confuses windows ;)
  712. */
  713. if (backgrd) {
  714. #ifndef CYGWIN_HACKS
  715. bg_do_split();
  716. } else {
  717. #endif /* !CYGWIN_HACKS */
  718. FILE *f = NULL;
  719. int xx;
  720. xx = getpid();
  721. /* Write pid to file */
  722. unlink(conf.bot->pid_file);
  723. if ((f = fopen(conf.bot->pid_file, "w")) != NULL) {
  724. fprintf(f, "%u\n", xx);
  725. if (fflush(f)) {
  726. printf(EGG_NOWRITE, conf.bot->pid_file);
  727. unlink(conf.bot->pid_file);
  728. }
  729. fclose(f);
  730. } else
  731. printf(EGG_NOWRITE, conf.bot->pid_file);
  732. #ifdef CYGWIN_HACKS
  733. printf("Launched into the background (pid: %d)\n\n", xx);
  734. #endif
  735. }
  736. use_stderr = 0; /* Stop writing to stderr now */
  737. if (backgrd) {
  738. /* Ok, try to disassociate from controlling terminal (finger cross) */
  739. #ifndef CYGWIN_HACKS
  740. setpgid(0, 0);
  741. #endif /* !CYGWIN_HACKS */
  742. /* fuck tcl.
  743. freopen("/dev/null", "r", stdin);
  744. freopen("/dev/null", "w", stdout);
  745. freopen("/dev/null", "w", stderr);
  746. */
  747. #ifdef CYGWIN_HACKS
  748. FreeConsole();
  749. #endif /* CYGWIN_HACKS */
  750. }
  751. /* Terminal emulating dcc chat */
  752. if (!backgrd && term_z) {
  753. int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  754. dcc[n].addr = iptolong(getmyip());
  755. dcc[n].sock = STDOUT;
  756. dcc[n].timeval = now;
  757. dcc[n].u.chat->con_flags = conmask;
  758. dcc[n].u.chat->strip_flags = STRIP_ALL;
  759. dcc[n].status = STAT_ECHO;
  760. strcpy(dcc[n].nick, "HQ");
  761. strcpy(dcc[n].host, "llama@console");
  762. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  763. /* Make sure there's an innocuous HQ user if needed */
  764. if (!dcc[n].user) {
  765. userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_VOICE | USER_OP | USER_PARTY | USER_CHUBA | USER_HUBA);
  766. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  767. }
  768. setsock(STDOUT, 0); /* Entry in net table */
  769. dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
  770. dcc_chatter(n);
  771. }
  772. online_since = now;
  773. autolink_cycle(NULL); /* Hurry and connect to tandem bots */
  774. timer_create_secs(1, "core_secondly", (Function) core_secondly);
  775. timer_create_secs(10, "check_expired_dcc", (Function) check_expired_dcc);
  776. timer_create_secs(10, "core_10secondly", (Function) core_10secondly);
  777. timer_create_secs(30, "expire_simuls", (Function) expire_simuls);
  778. timer_create_secs(60, "core_minutely", (Function) core_minutely);
  779. timer_create_secs(60, "check_botnet_pings", (Function) check_botnet_pings);
  780. timer_create_secs(60, "check_expired_ignores", (Function) check_expired_ignores);
  781. timer_create_secs(3600, "core_hourly", (Function) core_hourly);
  782. timer_create_secs(1800, "core_halfhourly", (Function) core_halfhourly);
  783. debug0("main: entering loop");
  784. while (1) {
  785. int socket_cleanup = 0, i, xx;
  786. char buf[SGRAB + 10] = "";
  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 %d",
  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 %d (type %d, name '%s') expired -- pfft",
  861. dcc[i].sock, dcc[i].type, 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. }