main.c 24 KB

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