main.c 27 KB

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