main.c 27 KB

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