main.c 27 KB

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