main.c 25 KB

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