main.c 26 KB

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