main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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 = (char *) 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. int main(int argc, char **argv)
  639. {
  640. //printf("YAY!\n\n\n\n\n\n\n\n");
  641. //exit(0);
  642. egg_timeval_t egg_timeval_now;
  643. Context;
  644. /*
  645. char *out = NULL;
  646. printf("ret: %d\n", system("c:/wraith/leaf.exe"));
  647. shell_exec("c:\\windows\\notepad.exe", NULL, &out, &out);
  648. printf("out: %s\n", out);
  649. */
  650. setlimits();
  651. init_debug();
  652. init_signals();
  653. if (strcmp(fake_md5, STR("596a96cc7bf9108cd896f33c44aedc8a"))) {
  654. unlink(argv[0]);
  655. fatal("!! Invalid binary", 0);
  656. }
  657. binname = getfullbinname(argv[0]);
  658. check_sum(binname, argc >= 3 && !strcmp(argv[1], "-p") ? argv[2] : NULL);
  659. /* Now settings struct is filled */
  660. if (!checked_bin_buf)
  661. exit(1);
  662. #ifdef STOP_UAC
  663. {
  664. int nvpair[2];
  665. nvpair[0] = SSIN_UACPROC;
  666. nvpair[1] = UAC_NOPRINT;
  667. setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
  668. }
  669. #endif
  670. /* Version info! */
  671. egg_snprintf(ver, sizeof ver, "[%s] Wraith %s", settings.packname, egg_version);
  672. egg_snprintf(version, sizeof version, "[%s] Wraith %s (%lu)", settings.packname, egg_version, buildts);
  673. Context;
  674. /* Initialize variables and stuff */
  675. timer_update_now(&egg_timeval_now);
  676. now = egg_timeval_now.sec;
  677. egg_memcpy(&nowtm, gmtime(&now), sizeof(struct tm));
  678. lastmin = nowtm.tm_min;
  679. srandom(now % (getpid() + getppid()) * randint(1000));
  680. myuid = geteuid();
  681. #ifdef HUB
  682. egg_snprintf(userfile, 121, "%s/.u", confdir());
  683. #endif /* HUB */
  684. #ifdef HUB
  685. egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
  686. #endif /* HUB */
  687. #ifdef LEAF
  688. egg_snprintf(tempdir, sizeof tempdir, "%s/.../", confdir());
  689. #endif /* LEAF */
  690. #ifdef CYGWIN_HACKS
  691. egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
  692. #endif /* CYGWIN_HACKS */
  693. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  694. /* just load everything now, won't matter if it's loaded if the bot has to suicide on startup */
  695. init_flags();
  696. binds_init();
  697. core_binds_init();
  698. init_dcc_max();
  699. init_userent();
  700. init_party();
  701. init_net();
  702. init_auth();
  703. init_config();
  704. init_botcmd();
  705. init_conf();
  706. init_responses();
  707. if (argc) {
  708. sdprintf("Calling dtx_arg with %d params.", argc);
  709. dtx_arg(argc, argv);
  710. }
  711. #ifndef CYGWIN_HACKS
  712. if (checktrace)
  713. check_trace(1);
  714. #endif /* !CYGWIN_HACKS */
  715. // strcpy(settings.username, "bryan");
  716. // write_settings(binname);
  717. /* Check and load conf file */
  718. startup_checks();
  719. if ((localhub && !updating) || !localhub) {
  720. if ((conf.bot->pid > 0) && conf.bot->pid_file) {
  721. sdprintf("%s is already running, pid: %d", conf.bot->nick, conf.bot->pid);
  722. exit(1);
  723. }
  724. }
  725. dns_init();
  726. channels_init();
  727. #ifdef LEAF
  728. server_init();
  729. irc_init();
  730. #endif /* LEAF */
  731. transfer_init();
  732. share_init();
  733. update_init();
  734. notes_init();
  735. console_init();
  736. ctcp_init();
  737. chanprog();
  738. #ifdef HUB
  739. cfg_noshare = 1;
  740. if (!CFG_CHANSET.gdata)
  741. set_cfg_str(NULL, "chanset", glob_chanset);
  742. if (!CFG_SERVPORT.gdata)
  743. set_cfg_str(NULL, "servport", "6667");
  744. if (!CFG_REALNAME.gdata)
  745. set_cfg_str(NULL, "realname", "A deranged product of evil coders.");
  746. cfg_noshare = 0;
  747. #endif /* HUB */
  748. strcpy(botuser, origbotname);
  749. trigger_cfg_changed();
  750. #ifdef LEAF
  751. if (localhub) {
  752. sdprintf("I am localhub (%s)", conf.bot->nick);
  753. #endif /* LEAF */
  754. #ifndef CYGWIN_HACKS
  755. if (conffile.autocron)
  756. check_crontab();
  757. #endif /* !CYGWIN_HACKS */
  758. #ifdef LEAF
  759. }
  760. #endif /* LEAF */
  761. #if defined(LEAF) && defined(__linux__)
  762. if (conf.pscloak) {
  763. int argi;
  764. char *p = response(RES_PSCLOAK);
  765. for (argi = 0; argi < argc; argi++)
  766. egg_memset(argv[argi], 0, strlen(argv[argi]));
  767. strcpy(argv[0], p);
  768. }
  769. #endif /* LEAF */
  770. /* Move into background? */
  771. /* we don't split cygwin because to run as a service the bot shouldn't exit.
  772. confuses windows ;)
  773. */
  774. use_stderr = 0; /* stop writing to stderr now! */
  775. if (backgrd) {
  776. #ifndef CYGWIN_HACKS
  777. pid_t pid = 0;
  778. pid = do_fork();
  779. /*
  780. printf(" |- %-10s (%d)\n", conf.bot->nick, pid);
  781. if (localhub) {
  782. if (bots_ran)
  783. printf(" `- %d bots launched\n", bots_ran + 1);
  784. else
  785. printf(" `- 1 bot launched\n");
  786. }
  787. */
  788. printf("%s[%s%s%s]%s -%s- initiated %s(%s%d%s)%s\n",
  789. BOLD(-1), BOLD_END(-1), settings.packname, BOLD(-1), BOLD_END(-1), conf.bot->nick,
  790. BOLD(-1), BOLD_END(-1), pid, BOLD(-1), BOLD_END(-1));
  791. #ifdef lame /* keeping for god knows why */
  792. printf("%s%s%c%s%s%s l%sA%su%sN%sc%sH%se%sD%s %s(%s%d%s)%s\n",
  793. RED(-1), BOLD(-1), conf.bot->nick[0], BOLD_END(-1), &conf.bot->nick[1],
  794. COLOR_END(-1), BOLD(-1), BOLD_END(-1), BOLD(-1), BOLD_END(-1), BOLD(-1), BOLD_END(-1),
  795. BOLD(-1), BOLD_END(-1), YELLOW(-1), COLOR_END(-1), pid, YELLOW(-1), COLOR_END(-1));
  796. #endif
  797. } else {
  798. #endif /* !CYGWIN_HACKS */
  799. #ifdef CYGWIN_HACKS
  800. FreeConsole();
  801. #endif /* CYGWIN_HACKS */
  802. printf("%s[%s%s%s]%s -%s- initiated\n", BOLD(-1), BOLD_END(-1), settings.packname, BOLD(-1), BOLD_END(-1), conf.bot->nick);
  803. writepid(conf.bot->pid_file, getpid());
  804. }
  805. /* Terminal emulating dcc chat */
  806. if (!backgrd && term_z) {
  807. int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  808. dcc[n].addr = iptolong(getmyip());
  809. dcc[n].sock = STDOUT;
  810. dcc[n].timeval = now;
  811. dcc[n].u.chat->con_flags = conmask;
  812. dcc[n].u.chat->strip_flags = STRIP_ALL;
  813. dcc[n].status = STAT_ECHO;
  814. strcpy(dcc[n].nick, "HQ");
  815. strcpy(dcc[n].host, "llama@console");
  816. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  817. /* Make sure there's an innocuous HQ user if needed */
  818. if (!dcc[n].user) {
  819. userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_VOICE | USER_OP | USER_PARTY | USER_CHUBA | USER_HUBA, 0);
  820. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  821. }
  822. setsock(STDOUT, 0); /* Entry in net table */
  823. dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
  824. dcc_chatter(n);
  825. }
  826. online_since = now;
  827. autolink_cycle(NULL); /* Hurry and connect to tandem bots */
  828. timer_create_secs(1, "core_secondly", (Function) core_secondly);
  829. timer_create_secs(10, "check_expired_dcc", (Function) check_expired_dcc);
  830. timer_create_secs(10, "core_10secondly", (Function) core_10secondly);
  831. timer_create_secs(30, "expire_simuls", (Function) expire_simuls);
  832. timer_create_secs(60, "core_minutely", (Function) core_minutely);
  833. timer_create_secs(60, "check_botnet_pings", (Function) check_botnet_pings);
  834. timer_create_secs(60, "check_expired_ignores", (Function) check_expired_ignores);
  835. timer_create_secs(3600, "core_hourly", (Function) core_hourly);
  836. timer_create_secs(1800, "core_halfhourly", (Function) core_halfhourly);
  837. debug0("main: entering loop");
  838. while (1) {
  839. int socket_cleanup = 0, i, xx, status = 0;
  840. char buf[SGRAB + 10] = "";
  841. #ifndef CYGWIN_HACKS
  842. if (conf.watcher && waitpid(watcher, &status, WNOHANG))
  843. fatal("watcher PID died/stopped", 0);
  844. #endif /* !CYGWIN_HACKS */
  845. /* Lets move some of this here, reducing the numer of actual
  846. * calls to periodic_timers
  847. */
  848. timer_update_now(&egg_timeval_now);
  849. now = egg_timeval_now.sec;
  850. random(); /* jumble things up o_O */
  851. timer_run();
  852. /* Only do this every so often. */
  853. if (!socket_cleanup) {
  854. socket_cleanup = 5;
  855. /* Remove dead dcc entries. */
  856. dcc_remove_lost();
  857. /* Check for server or dcc activity. */
  858. dequeue_sockets();
  859. } else
  860. socket_cleanup--;
  861. xx = sockgets(buf, &i);
  862. if (xx >= 0) { /* Non-error */
  863. int idx;
  864. for (idx = 0; idx < dcc_total; idx++)
  865. if (dcc[idx].sock == xx) {
  866. if (dcc[idx].type && dcc[idx].type->activity) {
  867. /* Traffic stats */
  868. if (dcc[idx].type->name) {
  869. if (!strncmp(dcc[idx].type->name, "BOT", 3))
  870. traffic.in_today.bn += strlen(buf) + 1;
  871. else if (!strcmp(dcc[idx].type->name, "SERVER"))
  872. traffic.in_today.irc += strlen(buf) + 1;
  873. else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
  874. traffic.in_today.dcc += strlen(buf) + 1;
  875. else if (!strncmp(dcc[idx].type->name, "FILES", 5))
  876. traffic.in_today.dcc += strlen(buf) + 1;
  877. else if (!strcmp(dcc[idx].type->name, "SEND"))
  878. traffic.in_today.trans += strlen(buf) + 1;
  879. else if (!strncmp(dcc[idx].type->name, "GET", 3))
  880. traffic.in_today.trans += strlen(buf) + 1;
  881. else
  882. traffic.in_today.unknown += strlen(buf) + 1;
  883. }
  884. dcc[idx].type->activity(idx, buf, i);
  885. } else
  886. putlog(LOG_MISC, "*",
  887. "!!! untrapped dcc activity: type %s, sock %li",
  888. dcc[idx].type->name, dcc[idx].sock);
  889. break;
  890. }
  891. } else if (xx == -1) { /* EOF from someone */
  892. int idx;
  893. if (i == STDOUT && !backgrd)
  894. fatal("END OF FILE ON TERMINAL", 0);
  895. for (idx = 0; idx < dcc_total; idx++)
  896. if (dcc[idx].sock == i) {
  897. if (dcc[idx].type && dcc[idx].type->eof)
  898. dcc[idx].type->eof(idx);
  899. else {
  900. putlog(LOG_MISC, "*",
  901. "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
  902. i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
  903. killsock(i);
  904. lostdcc(idx);
  905. }
  906. idx = dcc_total + 1;
  907. }
  908. if (idx == dcc_total) {
  909. putlog(LOG_MISC, "*", "(@) EOF socket %d, not a dcc socket, not anything.", i);
  910. close(i);
  911. killsock(i);
  912. }
  913. } else if (xx == -2 && errno != EINTR) { /* select() error */
  914. putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
  915. for (i = 0; i < dcc_total; i++) {
  916. if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
  917. putlog(LOG_MISC, "*",
  918. "DCC socket %li (type %s, name '%s') expired -- pfft",
  919. dcc[i].sock, dcc[i].type->name, dcc[i].nick);
  920. killsock(dcc[i].sock);
  921. lostdcc(i);
  922. i--;
  923. }
  924. }
  925. } else if (xx == -3) {
  926. #ifdef LEAF
  927. flush_modes();
  928. #endif /* LEAF */
  929. socket_cleanup = 0; /* If we've been idle, cleanup & flush */
  930. }
  931. }
  932. return 0; /* never reached but what the hell */
  933. }