main.c 24 KB

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