main.c 28 KB

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