main.c 27 KB

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