main.c 25 KB

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