main.c 27 KB

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