main.c 25 KB

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