main.c 26 KB

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