main.c 28 KB

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