misc.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199
  1. /*
  2. * misc.c -- handles:
  3. * split() maskhost() dumplots() daysago() days() daysdur()
  4. * queueing output for the bot (msg and help)
  5. * resync buffers for sharebots
  6. * motd display and %var substitution
  7. *
  8. */
  9. #include "common.h"
  10. #include "misc.h"
  11. #include "rfc1459.h"
  12. #include "misc_file.h"
  13. #include "dcc.h"
  14. #include "users.h"
  15. #include "main.h"
  16. #include "debug.h"
  17. #include "dccutil.h"
  18. #include "chanprog.h"
  19. #include "color.h"
  20. #include "botmsg.h"
  21. #include "bg.h"
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <sys/wait.h>
  25. #include <ctype.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include "chan.h"
  29. #include "tandem.h"
  30. #include "modules.h"
  31. #include <pwd.h>
  32. #include <errno.h>
  33. #include <libgen.h>
  34. #ifdef HUB
  35. #include "userrec.h"
  36. #endif /* HUB */
  37. #ifdef S_ANTITRACE
  38. #include <sys/ptrace.h>
  39. #include <sys/wait.h>
  40. #endif /* S_ANTITRACE */
  41. #include <net/if.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/socket.h>
  44. #include <signal.h>
  45. #include <sys/utsname.h>
  46. #include "stat.h"
  47. #include <stdarg.h>
  48. extern struct userrec *userlist;
  49. extern struct dcc_t *dcc;
  50. extern struct chanset_t *chanset;
  51. extern char version[], origbotname[], botname[],
  52. admin[], network[], motdfile[], ver[], botnetnick[],
  53. userfile[], dcc_prefix[],
  54. *binname, pid_file[], tempdir[], *owneremail;
  55. extern int backgrd, term_z, use_stderr, dcc_total, timesync,
  56. #ifdef HUB
  57. my_port,
  58. #endif
  59. strict_host, loading,
  60. localhub;
  61. extern time_t now;
  62. extern struct cfg_entry CFG_MOTD, CFG_LOGIN, CFG_BADPROCESS, CFG_PROCESSLIST, CFG_PROMISC,
  63. CFG_TRACE, CFG_HIJACK;
  64. void detected(int, char *);
  65. int server_lag = 0; /* GUESS! */
  66. /*
  67. * Misc functions
  68. */
  69. /* low-level stuff for other modules
  70. */
  71. /* This implementation wont overrun dst - 'max' is the max bytes that dst
  72. * can be, including the null terminator. So if 'dst' is a 128 byte buffer,
  73. * pass 128 as 'max'. The function will _always_ null-terminate 'dst'.
  74. *
  75. * Returns: The number of characters appended to 'dst'.
  76. *
  77. * Usage eg.
  78. *
  79. * char buf[128];
  80. * size_t bufsize = sizeof(buf);
  81. *
  82. * buf[0] = 0, bufsize--;
  83. *
  84. * while (blah && bufsize) {
  85. * bufsize -= egg_strcatn(buf, <some-long-string>, sizeof(buf));
  86. * }
  87. *
  88. * <Cybah>
  89. */
  90. int egg_strcatn(char *dst, const char *src, size_t max)
  91. {
  92. size_t tmpmax = 0;
  93. /* find end of 'dst' */
  94. while (*dst && max > 0) {
  95. dst++;
  96. max--;
  97. }
  98. /* Store 'max', so we can use it to workout how many characters were
  99. * written later on.
  100. */
  101. tmpmax = max;
  102. /* copy upto, but not including the null terminator */
  103. while (*src && max > 1) {
  104. *dst++ = *src++;
  105. max--;
  106. }
  107. /* null-terminate the buffer */
  108. *dst = 0;
  109. /* Don't include the terminating null in our count, as it will cumulate
  110. * in loops - causing a headache for the caller.
  111. */
  112. return tmpmax - max;
  113. }
  114. int my_strcpy(register char *a, register char *b)
  115. {
  116. register char *c = b;
  117. while (*b)
  118. *a++ = *b++;
  119. *a = *b;
  120. return b - c;
  121. }
  122. /* Split first word off of rest and put it in first
  123. */
  124. void splitc(char *first, char *rest, char divider)
  125. {
  126. char *p = strchr(rest, divider);
  127. if (p == NULL) {
  128. if (first != rest && first)
  129. first[0] = 0;
  130. return;
  131. }
  132. *p = 0;
  133. if (first != NULL)
  134. strcpy(first, rest);
  135. if (first != rest)
  136. /* In most circumstances, strcpy with src and dst being the same buffer
  137. * can produce undefined results. We're safe here, as the src is
  138. * guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
  139. */
  140. strcpy(rest, p + 1);
  141. }
  142. /* As above, but lets you specify the 'max' number of bytes (EXCLUDING the
  143. * terminating null).
  144. *
  145. * Example of use:
  146. *
  147. * char buf[HANDLEN + 1];
  148. *
  149. * splitcn(buf, input, "@", HANDLEN);
  150. *
  151. * <Cybah>
  152. */
  153. void splitcn(char *first, char *rest, char divider, size_t max)
  154. {
  155. char *p = strchr(rest, divider);
  156. if (p == NULL) {
  157. if (first != rest && first)
  158. first[0] = 0;
  159. return;
  160. }
  161. *p = 0;
  162. if (first != NULL)
  163. strncpyz(first, rest, max);
  164. if (first != rest)
  165. /* In most circumstances, strcpy with src and dst being the same buffer
  166. * can produce undefined results. We're safe here, as the src is
  167. * guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
  168. */
  169. strcpy(rest, p + 1);
  170. }
  171. char *splitnick(char **blah)
  172. {
  173. char *p = strchr(*blah, '!'), *q = *blah;
  174. if (p) {
  175. *p = 0;
  176. *blah = p + 1;
  177. return q;
  178. }
  179. return "";
  180. }
  181. void remove_crlf(char **line)
  182. {
  183. char *p;
  184. p = strchr(*line, '\n');
  185. if (p != NULL)
  186. *p = 0;
  187. p = strchr(*line, '\r');
  188. if (p != NULL)
  189. *p = 0;
  190. }
  191. char *newsplit(char **rest)
  192. {
  193. register char *o, *r;
  194. if (!rest)
  195. return *rest = "";
  196. o = *rest;
  197. while (*o == ' ')
  198. o++;
  199. r = o;
  200. while (*o && (*o != ' '))
  201. o++;
  202. if (*o)
  203. *o++ = 0;
  204. *rest = o;
  205. return r;
  206. }
  207. /* Convert "abc!user@a.b.host" into "*!user@*.b.host"
  208. * or "abc!user@1.2.3.4" into "*!user@1.2.3.*"
  209. * or "abc!user@0:0:0:0:0:ffff:1.2.3.4" into "*!user@0:0:0:0:0:ffff:1.2.3.*"
  210. * or "abc!user@3ffe:604:2:b02e:6174:7265:6964:6573" into
  211. * "*!user@3ffe:604:2:b02e:6174:7265:6964:*"
  212. */
  213. void maskhost(const char *s, char *nw)
  214. {
  215. register const char *p, *q, *e, *f;
  216. int i;
  217. *nw++ = '*';
  218. *nw++ = '!';
  219. p = (q = strchr(s, '!')) ? q + 1 : s;
  220. /* Strip of any nick, if a username is found, use last 8 chars */
  221. if ((q = strchr(p, '@'))) {
  222. int fl = 0;
  223. if ((q - p) > 9) {
  224. nw[0] = '*';
  225. p = q - 7;
  226. i = 1;
  227. } else
  228. i = 0;
  229. while (*p != '@') {
  230. if (!fl && strchr("~+-^=", *p)) {
  231. if (strict_host)
  232. nw[i] = '?';
  233. else
  234. i--;
  235. } else
  236. nw[i] = *p;
  237. fl++;
  238. p++;
  239. i++;
  240. }
  241. nw[i++] = '@';
  242. q++;
  243. } else {
  244. nw[0] = '*';
  245. nw[1] = '@';
  246. i = 2;
  247. q = s;
  248. }
  249. nw += i;
  250. e = NULL;
  251. /* Now q points to the hostname, i point to where to put the mask */
  252. if ((!(p = strchr(q, '.')) || !(e = strchr(p + 1, '.'))) && !strchr(q, ':'))
  253. /* TLD or 2 part host */
  254. strcpy(nw, q);
  255. else {
  256. if (e == NULL) { /* IPv6 address? */
  257. const char *mask_str;
  258. f = strrchr(q, ':');
  259. if (strchr(f, '.')) { /* IPv4 wrapped in an IPv6? */
  260. f = strrchr(f, '.');
  261. mask_str = ".*";
  262. } else /* ... no, true IPv6. */
  263. mask_str = ":*";
  264. strncpy(nw, q, f - q);
  265. /* No need to nw[f-q] = 0 here, as the strcpy below will
  266. * terminate the string for us.
  267. */
  268. nw += (f - q);
  269. strcpy(nw, mask_str);
  270. } else {
  271. for (f = e; *f; f++);
  272. f--;
  273. if (*f >= '0' && *f <= '9') { /* Numeric IP address */
  274. while (*f != '.')
  275. f--;
  276. strncpy(nw, q, f - q);
  277. /* No need to nw[f-q] = 0 here, as the strcpy below will
  278. * terminate the string for us.
  279. */
  280. nw += (f - q);
  281. strcpy(nw, ".*");
  282. } else { /* Normal host >= 3 parts */
  283. /* a.b.c -> *.b.c
  284. * a.b.c.d -> *.b.c.d if tld is a country (2 chars)
  285. * OR *.c.d if tld is com/edu/etc (3 chars)
  286. * a.b.c.d.e -> *.c.d.e etc
  287. */
  288. const char *x = strchr(e + 1, '.');
  289. if (!x)
  290. x = p;
  291. else if (strchr(x + 1, '.'))
  292. x = e;
  293. else if (strlen(x) == 3)
  294. x = p;
  295. else
  296. x = e;
  297. sprintf(nw, "*%s", x);
  298. }
  299. }
  300. }
  301. }
  302. /* Dump a potentially super-long string of text.
  303. */
  304. void dumplots(int idx, const char *prefix, char *data)
  305. {
  306. char *p = data, *q, *n, c;
  307. const int max_data_len = 500 - strlen(prefix);
  308. if (!*data) {
  309. dprintf(idx, "%s\n", prefix);
  310. return;
  311. }
  312. while (strlen(p) > max_data_len) {
  313. q = p + max_data_len;
  314. /* Search for embedded linefeed first */
  315. n = strchr(p, '\n');
  316. if (n && n < q) {
  317. /* Great! dump that first line then start over */
  318. *n = 0;
  319. dprintf(idx, "%s%s\n", prefix, p);
  320. *n = '\n';
  321. p = n + 1;
  322. } else {
  323. /* Search backwards for the last space */
  324. while (*q != ' ' && q != p)
  325. q--;
  326. if (q == p)
  327. q = p + max_data_len;
  328. c = *q;
  329. *q = 0;
  330. dprintf(idx, "%s%s\n", prefix, p);
  331. *q = c;
  332. p = q;
  333. if (c == ' ')
  334. p++;
  335. }
  336. }
  337. /* Last trailing bit: split by linefeeds if possible */
  338. n = strchr(p, '\n');
  339. while (n) {
  340. *n = 0;
  341. dprintf(idx, "%s%s\n", prefix, p);
  342. *n = '\n';
  343. p = n + 1;
  344. n = strchr(p, '\n');
  345. }
  346. if (*p)
  347. dprintf(idx, "%s%s\n", prefix, p); /* Last trailing bit */
  348. }
  349. /* Convert an interval (in seconds) to one of:
  350. * "19 days ago", "1 day ago", "18:12"
  351. */
  352. void daysago(time_t now, time_t then, char *out)
  353. {
  354. if (now - then > 86400) {
  355. int days = (now - then) / 86400;
  356. sprintf(out, "%d day%s ago", days, (days == 1) ? "" : "s");
  357. return;
  358. }
  359. #ifdef S_UTCTIME
  360. egg_strftime(out, 6, "%H:%M", gmtime(&then));
  361. #else /* !S_UTCTIME */
  362. egg_strftime(out, 6, "%H:%M", localtime(&then));
  363. #endif /* S_UTCTIME */
  364. }
  365. /* Convert an interval (in seconds) to one of:
  366. * "in 19 days", "in 1 day", "at 18:12"
  367. */
  368. void days(time_t now, time_t then, char *out)
  369. {
  370. if (now - then > 86400) {
  371. int days = (now - then) / 86400;
  372. sprintf(out, "in %d day%s", days, (days == 1) ? "" : "s");
  373. return;
  374. }
  375. #ifdef S_UTCTIME
  376. egg_strftime(out, 9, "at %H:%M", gmtime(&now));
  377. #else /* !S_UTCTIME */
  378. egg_strftime(out, 9, "at %H:%M", localtime(&now));
  379. #endif /* S_UTCTIME */
  380. }
  381. /* Convert an interval (in seconds) to one of:
  382. * "for 19 days", "for 1 day", "for 09:10"
  383. */
  384. void daysdur(time_t now, time_t then, char *out)
  385. {
  386. char s[81];
  387. int hrs, mins;
  388. if (now - then > 86400) {
  389. int days = (now - then) / 86400;
  390. sprintf(out, "for %d day%s", days, (days == 1) ? "" : "s");
  391. return;
  392. }
  393. strcpy(out, "for ");
  394. now -= then;
  395. hrs = (int) (now / 3600);
  396. mins = (int) ((now - (hrs * 3600)) / 60);
  397. sprintf(s, "%02d:%02d", hrs, mins);
  398. strcat(out, s);
  399. }
  400. /* show l33t banner */
  401. char *wbanner() {
  402. int r;
  403. r = random();
  404. switch (r % 9) {
  405. case 0: return STR(" .__ __ .__\n__ _ ______________ |__|/ |_| |__\n\\ \\/ \\/ /\\_ __ \\__ \\ | \\ __\\ | \\\n \\ / | | \\// __ \\| || | | Y \\\n \\/\\_/ |__| (____ /__||__| |___| /\n \\/ \\/\n");
  406. case 1: return STR(" _ _ _ \n__ ___ __ __ _(_) |_| |__ \n\\ \\ /\\ / / '__/ _` | | __| '_ \\ \n \\ V V /| | | (_| | | |_| | | |\n \\_/\\_/ |_| \\__,_|_|\\__|_| |_|\n");
  407. case 2: return STR("@@@ @@@ @@@ @@@@@@@ @@@@@@ @@@ @@@@@@@ @@@ @@@\n@@@ @@@ @@@ @@@@@@@@ @@@@@@@@ @@@ @@@@@@@ @@@ @@@\n@@! @@! @@! @@! @@@ @@! @@@ @@! @@! @@! @@@\n!@! !@! !@! !@! @!@ !@! @!@ !@! !@! !@! @!@\n@!! !!@ @!@ @!@!!@! @!@!@!@! !!@ @!! @!@!@!@!\n!@! !!! !@! !!@!@! !!!@!!!! !!! !!! !!!@!!!!\n!!: !!: !!: !!: :!! !!: !!! !!: !!: !!: !!!\n:!: :!: :!: :!: !:! :!: !:! :!: :!: :!: !:!\n :::: :: ::: :: ::: :: ::: :: :: :: :::\n :: : : : : : : : : : : : : : :\n");
  408. case 3: return STR(" o8o . oooo\n `'' .o8 `888\noooo oooo ooo oooo d8b .oooo. oooo .o888oo 888 .oo.\n `88. `88. .8' `888''8P `P )88b `888 888 888P'Y88b\n `88..]88..8' 888 .oP'888 888 888 888 888\n `888'`888' 888 d8( 888 888 888 . 888 888\n `8' `8' d888b `Y888''8o o888o '888' o888o o888o\n");
  409. case 4: return STR(" *\n * * **\n** *** ** **\n** * ** **\n ** *** **** *** **** ******** **\n ** *** *** * **** **** * **** *** ******** ** ***\n ** *** **** ** **** * *** * *** ** ** * ***\n ** ** ** ** * **** ** ** *** ***\n ** ** ** ** ** ** ** ** ** **\n ** ** ** ** ** ** ** ** ** **\n ** ** ** ** ** ** ** ** ** **\n ** ** * ** ** ** ** ** ** **\n ******* ******* *** ** ** ** ** ** **\n ***** ***** *** ***** ** *** * ** ** **\n *** ** *** ** **\n *\n *\n *\n *\n");
  410. case 5: return STR(" ::: === === :::==== :::==== ::: :::==== ::: ===\n ::: === === ::: === ::: === ::: :::==== ::: ===\n === === === ======= ======== === === ========\n =========== === === === === === === === ===\n ==== ==== === === === === === === === ===\n");
  411. case 6: return STR(" _ _ _ ______ _______ _____ _______ _ _\n | | | |_____/ |_____| | | |_____|\n |__|__| | \\_ | | __|__ | | |\n");
  412. case 7: return STR(" dBPdBPdBP dBBBBBb dBBBBBb dBP dBBBBBBP dBP dBP\n dBP BB\n dBPdBPdBP dBBBBK dBP BB dBP dBP dBBBBBP\n dBPdBPdBP dBP BB dBP BB dBP dBP dBP dBP\n dBBBBBBBP dBP dB' dBBBBBBB dBP dBP dBP dBP\n");
  413. case 8: return STR(" /\n # #/\n ### # ##\n## # ## ##\n## ## ##\n ## ### #### ### /### /### ### ########## /##\n ## ### ###/ ###/ #### / ###/ ########### ## / ###\n ## ### ### ## ###/ ### ## ## ##/ ###\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## /# / ## ## /# ## ## ## ##\n ######/ ######/ ### ####/ ## ### /## ## ##\n ##### ##### ### ### ##/##/ ## ## ##\n /\n /\n /\n /");
  414. }
  415. return "";
  416. }
  417. void show_banner(int idx)
  418. {
  419. dumplots(-dcc[idx].sock, "", wbanner()); /* we use sock so that colors aren't applied to banner */
  420. dprintf(idx, "\n \n");
  421. dprintf(idx, STR("info, bugs, suggestions, comments:\n- http://wraith.shatow.net/ -\n \n"));
  422. }
  423. /* show motd to dcc chatter */
  424. void show_motd(int idx)
  425. {
  426. if (CFG_MOTD.gdata && *(char *) CFG_MOTD.gdata) {
  427. char *who, *buf, date[50];
  428. time_t time;
  429. void *buf_ptr;
  430. buf = buf_ptr = strdup((char *) CFG_MOTD.gdata);
  431. who = newsplit(&buf);
  432. time = atoi(newsplit(&buf));
  433. #ifdef S_UTCTIME
  434. egg_strftime(date, sizeof date, "%c %Z", gmtime(&time));
  435. #else /* !S_UTCTIME */
  436. egg_strftime(date, sizeof date, "%c %Z", localtime(&time));
  437. #endif /* S_UTCTIME */
  438. dprintf(idx, "Motd set by \002%s\002 (%s)\n", who, date);
  439. dumplots(idx, "* ", replace(buf, "\\n", "\n"));
  440. dprintf(idx, " \n");
  441. free(buf_ptr);
  442. } else
  443. dprintf(idx, STR("Motd: none\n"));
  444. }
  445. void show_channels(int idx, char *handle)
  446. {
  447. struct chanset_t *chan;
  448. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0, 0 };
  449. struct userrec *u;
  450. int first = 0, l = 0, total = 0;
  451. char format[120];
  452. #ifdef LEAF
  453. module_entry *me = module_find("irc", 0, 0);
  454. Function *func = me->funcs;
  455. #endif /* LEAF */
  456. if (handle)
  457. u = get_user_by_handle(userlist, handle);
  458. else
  459. u = dcc[idx].user;
  460. for (chan = chanset;chan;chan = chan->next) {
  461. get_user_flagrec(u, &fr, chan->dname);
  462. if (l < strlen(chan->dname)) {
  463. l = strlen(chan->dname);
  464. }
  465. if (chk_op(fr, chan))
  466. total++;
  467. }
  468. egg_snprintf(format, sizeof format, " %%c%%-%us %%-s%%-s%%-s%%-s%%-s\n", (l+2));
  469. for (chan = chanset;chan;chan = chan->next) {
  470. #ifdef LEAF
  471. int opped = (func[16] (chan));
  472. #else /* !LEAF */
  473. int opped = 0;
  474. #endif /* LEAF */
  475. get_user_flagrec(u, &fr, chan->dname);
  476. if (chk_op(fr, chan)) {
  477. if (!first) {
  478. dprintf(idx, STR("%s %s access to %d channel%s:\n"), handle ? u->handle : "You", handle ? "has" : "have", total, (total > 1) ? "s" : "");
  479. first = 1;
  480. }
  481. dprintf(idx, format, opped ? '@' : ' ', chan->dname, !shouldjoin(chan) ? "(inactive) " : "",
  482. channel_private(chan) ? "(private) " : "", !channel_manop(chan) ? "(no manop) " : "",
  483. channel_bitch(chan) ? "(bitch) " : "", channel_closed(chan) ? "(closed)" : "");
  484. }
  485. }
  486. if (!first)
  487. dprintf(idx, STR("%s %s not have access to any channels.\n"), handle ? u->handle : "You", handle ? "does" : "do");
  488. }
  489. int getting_users()
  490. {
  491. int i;
  492. for (i = 0; i < dcc_total; i++)
  493. if ((dcc[i].type == &DCC_BOT) && (dcc[i].status & STAT_GETTING))
  494. return 1;
  495. return 0;
  496. }
  497. char *extracthostname(char *hostmask)
  498. {
  499. char *p = strrchr(hostmask, '@');
  500. return p ? p + 1 : "";
  501. }
  502. /* Create a string with random letters and digits
  503. */
  504. void make_rand_str(char *s, int len)
  505. {
  506. int j, r = 0;
  507. for (j = 0; j < len; j++) {
  508. r = random();
  509. if (r % 4 == 0)
  510. s[j] = '0' + (random() % 10);
  511. else if (r % 4 == 1)
  512. s[j] = 'a' + (random() % 26);
  513. else if (r % 4 == 2)
  514. s[j] = 'A' + (random() % 26);
  515. else
  516. s[j] = '!' + (random() % 15);
  517. if (s[j] == 33 || s[j] == 37 || s[j] == 34 || s[j] == 40 || s[j] == 41 || s[j] == 38 || s[j] == 36) //no % ( ) &
  518. s[j] = 35;
  519. }
  520. s[len] = '\0';
  521. }
  522. /* Convert an octal string into a decimal integer value. If the string
  523. * is empty or contains non-octal characters, -1 is returned.
  524. */
  525. int oatoi(const char *octal)
  526. {
  527. register int i;
  528. if (!*octal)
  529. return -1;
  530. for (i = 0; ((*octal >= '0') && (*octal <= '7')); octal++)
  531. i = (i * 8) + (*octal - '0');
  532. if (*octal)
  533. return -1;
  534. return i;
  535. }
  536. /* Return an allocated buffer which contains a copy of the string
  537. * 'str', with all 'div' characters escaped by 'mask'. 'mask'
  538. * characters are escaped too.
  539. *
  540. * Remember to free the returned memory block.
  541. */
  542. char *str_escape(const char *str, const char div, const char mask)
  543. {
  544. const int len = strlen(str);
  545. int buflen = (2 * len), blen = 0;
  546. char *buf = malloc(buflen + 1), *b = buf;
  547. const char *s;
  548. if (!buf)
  549. return NULL;
  550. for (s = str; *s; s++) {
  551. /* Resize buffer. */
  552. if ((buflen - blen) <= 3) {
  553. buflen = (buflen * 2);
  554. buf = realloc(buf, buflen + 1);
  555. if (!buf)
  556. return NULL;
  557. b = buf + blen;
  558. }
  559. if (*s == div || *s == mask) {
  560. sprintf(b, "%c%02x", mask, *s);
  561. b += 3;
  562. blen += 3;
  563. } else {
  564. *(b++) = *s;
  565. blen++;
  566. }
  567. }
  568. *b = 0;
  569. return buf;
  570. }
  571. /* Is every character in a string a digit? */
  572. int str_isdigit(const char *str)
  573. {
  574. if (!*str)
  575. return 0;
  576. for(; *str; ++str) {
  577. if (!egg_isdigit(*str))
  578. return 0;
  579. }
  580. return 1;
  581. }
  582. /* Search for a certain character 'div' in the string 'str', while
  583. * ignoring escaped characters prefixed with 'mask'.
  584. *
  585. * The string
  586. *
  587. * "\\3a\\5c i am funny \\3a):further text\\5c):oink"
  588. *
  589. * as str, '\\' as mask and ':' as div would change the str buffer
  590. * to
  591. *
  592. * ":\\ i am funny :)"
  593. *
  594. * and return a pointer to "further text\\5c):oink".
  595. *
  596. * NOTE: If you look carefully, you'll notice that strchr_unescape()
  597. * behaves differently than strchr().
  598. */
  599. char *strchr_unescape(char *str, const char div, register const char esc_char)
  600. {
  601. char buf[3];
  602. register char *s, *p;
  603. buf[3] = 0;
  604. for (s = p = str; *s; s++, p++) {
  605. if (*s == esc_char) { /* Found escape character. */
  606. /* Convert code to character. */
  607. buf[0] = s[1], buf[1] = s[2];
  608. *p = (unsigned char) strtol(buf, NULL, 16);
  609. s += 2;
  610. } else if (*s == div) {
  611. *p = *s = 0;
  612. return (s + 1); /* Found searched for character. */
  613. } else
  614. *p = *s;
  615. }
  616. *p = 0;
  617. return NULL;
  618. }
  619. /* As strchr_unescape(), but converts the complete string, without
  620. * searching for a specific delimiter character.
  621. */
  622. void str_unescape(char *str, register const char esc_char)
  623. {
  624. (void) strchr_unescape(str, 0, esc_char);
  625. }
  626. /* Kills the bot. s1 is the reason shown to other bots,
  627. * s2 the reason shown on the partyline. (Sup 25Jul2001)
  628. */
  629. void kill_bot(char *s1, char *s2)
  630. {
  631. #ifdef HUB
  632. write_userfile(-1);
  633. #endif /* HUB */
  634. call_hook(HOOK_DIE);
  635. chatout("*** %s\n", s1);
  636. botnet_send_chat(-1, botnetnick, s1);
  637. botnet_send_bye();
  638. fatal(s2, 0);
  639. }
  640. int isupdatehub()
  641. {
  642. #ifdef HUB
  643. struct userrec *buser;
  644. buser = get_user_by_handle(userlist, botnetnick);
  645. if ((buser) && (buser->flags & USER_UPDATEHUB))
  646. return 1;
  647. else
  648. #endif /* HUB */
  649. return 0;
  650. }
  651. int ischanhub()
  652. {
  653. struct userrec *buser;
  654. buser = get_user_by_handle(userlist, botnetnick);
  655. if ((buser) && (buser->flags & USER_CHANHUB))
  656. return 1;
  657. else
  658. return 0;
  659. }
  660. int dovoice(struct chanset_t *chan)
  661. {
  662. struct userrec *user = NULL;
  663. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0 };
  664. if (!chan) return 0;
  665. user = get_user_by_handle(userlist, botnetnick);
  666. get_user_flagrec(user, &fr, chan->dname);
  667. if (glob_dovoice(fr) || chan_dovoice(fr))
  668. return 1;
  669. return 0;
  670. }
  671. int dolimit(struct chanset_t *chan)
  672. {
  673. struct userrec *user = NULL;
  674. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0 };
  675. if (!chan) return 0;
  676. user = get_user_by_handle(userlist, botnetnick);
  677. get_user_flagrec(user, &fr, chan->dname);
  678. if (glob_dolimit(fr) || chan_dolimit(fr))
  679. return 1;
  680. return 0;
  681. }
  682. #ifdef S_LASTCHECK
  683. char last_buf[128]="";
  684. #endif /* S_LASTCHECK */
  685. void check_last() {
  686. #ifdef S_LASTCHECK
  687. char user[20];
  688. struct passwd *pw;
  689. if (!strcmp((char *) CFG_LOGIN.ldata ? CFG_LOGIN.ldata : CFG_LOGIN.gdata ? CFG_LOGIN.gdata : "ignore", "ignore"))
  690. return;
  691. pw = getpwuid(geteuid());
  692. if (!pw) return;
  693. strncpyz(user, pw->pw_name ? pw->pw_name : "" , sizeof(user));
  694. if (user[0]) {
  695. char *out;
  696. char buf[50];
  697. sprintf(buf, STR("last %s"), user);
  698. if (shell_exec(buf, NULL, &out, NULL)) {
  699. if (out) {
  700. char *p;
  701. p = strchr(out, '\n');
  702. if (p)
  703. *p = 0;
  704. if (strlen(out) > 10) {
  705. if (last_buf[0]) {
  706. if (strncmp(last_buf, out, sizeof(last_buf))) {
  707. char wrk[16384];
  708. sprintf(wrk, STR("Login: %s"), out);
  709. detected(DETECT_LOGIN, wrk);
  710. }
  711. }
  712. strncpyz(last_buf, out, sizeof(last_buf));
  713. }
  714. free(out);
  715. }
  716. }
  717. }
  718. #endif /* S_LASTCHECK */
  719. }
  720. void check_processes()
  721. {
  722. #ifdef S_PROCESSCHECK
  723. char *proclist,
  724. *out,
  725. *p,
  726. *np,
  727. *curp,
  728. buf[1024],
  729. bin[128];
  730. if (!strcmp((char *) CFG_BADPROCESS.ldata ? CFG_BADPROCESS.ldata : CFG_BADPROCESS.gdata ? CFG_BADPROCESS.gdata : "ignore", "ignore"))
  731. return;
  732. proclist = (char *) (CFG_PROCESSLIST.ldata && ((char *) CFG_PROCESSLIST.ldata)[0] ?
  733. CFG_PROCESSLIST.ldata : CFG_PROCESSLIST.gdata && ((char *) CFG_PROCESSLIST.gdata)[0] ? CFG_PROCESSLIST.gdata : NULL);
  734. if (!proclist)
  735. return;
  736. if (!shell_exec(STR("ps x"), NULL, &out, NULL))
  737. return;
  738. /* Get this binary's filename */
  739. strncpyz(buf, binname, sizeof(buf));
  740. p = strrchr(buf, '/');
  741. if (p) {
  742. p++;
  743. strncpyz(bin, p, sizeof(bin));
  744. } else {
  745. bin[0] = 0;
  746. }
  747. /* Fix up the "permitted processes" list */
  748. p = malloc(strlen(proclist) + strlen(bin) + 6);
  749. strcpy(p, proclist);
  750. strcat(p, " ");
  751. strcat(p, bin);
  752. strcat(p, " ");
  753. proclist = p;
  754. curp = out;
  755. while (curp) {
  756. np = strchr(curp, '\n');
  757. if (np)
  758. *np++ = 0;
  759. if (atoi(curp) > 0) {
  760. char *pid,
  761. *tty,
  762. *stat,
  763. *time,
  764. cmd[512],
  765. line[2048];
  766. strncpyz(line, curp, sizeof(line));
  767. /* it's a process line */
  768. /* Assuming format: pid tty stat time cmd */
  769. pid = newsplit(&curp);
  770. tty = newsplit(&curp);
  771. stat = newsplit(&curp);
  772. time = newsplit(&curp);
  773. strncpyz(cmd, curp, sizeof(cmd));
  774. /* skip any <defunct> procs "/bin/sh -c" crontab stuff and binname crontab stuff */
  775. if (!strstr(cmd, STR("<defunct>")) && !strncmp(cmd, STR("/bin/sh -c"), 10)
  776. && !strncmp(cmd, binname, strlen(binname))) {
  777. /* get rid of any args */
  778. if ((p = strchr(cmd, ' ')))
  779. *p = 0;
  780. /* remove [] or () */
  781. if (strlen(cmd)) {
  782. p = cmd + strlen(cmd) - 1;
  783. if (((cmd[0] == '(') && (*p == ')')) || ((cmd[0] == '[') && (*p == ']'))) {
  784. *p = 0;
  785. strcpy(buf, cmd + 1);
  786. strcpy(cmd, buf);
  787. }
  788. }
  789. /* remove path */
  790. if ((p = strrchr(cmd, '/'))) {
  791. p++;
  792. strcpy(buf, p);
  793. strcpy(cmd, buf);
  794. }
  795. /* skip "ps" */
  796. if (strcmp(cmd, "ps")) {
  797. /* see if proc's in permitted list */
  798. strcat(cmd, " ");
  799. if ((p = strstr(proclist, cmd))) {
  800. /* Remove from permitted list */
  801. while (*p != ' ')
  802. *p++ = 1;
  803. } else {
  804. char wrk[16384];
  805. sprintf(wrk, STR("Unexpected process: %s"), line);
  806. detected(DETECT_PROCESS, wrk);
  807. }
  808. }
  809. }
  810. }
  811. curp = np;
  812. }
  813. free(proclist);
  814. if (out)
  815. free(out);
  816. #endif /* S_PROCESSCHECK */
  817. }
  818. void check_promisc()
  819. {
  820. #ifdef S_PROMISC
  821. #ifdef SIOCGIFCONF
  822. char buf[8192];
  823. struct ifreq ifreq, *ifr;
  824. struct ifconf ifcnf;
  825. char *cp, *cplim;
  826. int sock;
  827. if (!strcmp((char *) CFG_PROMISC.ldata ? CFG_PROMISC.ldata : CFG_PROMISC.gdata ? CFG_PROMISC.gdata : "ignore", "ignore"))
  828. return;
  829. sock = socket(AF_INET, SOCK_STREAM, 0);
  830. ifcnf.ifc_len = 8191;
  831. ifcnf.ifc_buf = buf;
  832. if (ioctl(sock, SIOCGIFCONF, (char *) &ifcnf) < 0) {
  833. close(sock);
  834. return;
  835. }
  836. ifr = ifcnf.ifc_req;
  837. cplim = buf + ifcnf.ifc_len;
  838. for (cp = buf; cp < cplim; cp += sizeof(ifr->ifr_name) + sizeof(ifr->ifr_addr)) {
  839. ifr = (struct ifreq *) cp;
  840. ifreq = *ifr;
  841. if (!ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq)) {
  842. if (ifreq.ifr_flags & IFF_PROMISC) {
  843. close(sock);
  844. detected(DETECT_PROMISC, STR("Detected promiscuous mode"));
  845. return;
  846. }
  847. }
  848. }
  849. close(sock);
  850. #endif /* SIOCGIFCONF */
  851. #endif /* S_PROMISC */
  852. }
  853. #ifdef S_ANTITRACE
  854. int traced = 0;
  855. void got_trace(int z)
  856. {
  857. traced = 0;
  858. }
  859. #endif /* S_ANTITRACE */
  860. void check_trace()
  861. {
  862. #ifdef S_ANTITRACE
  863. int x, parent, i;
  864. struct sigaction sv, *oldsv = NULL;
  865. if (!strcmp((char *) CFG_TRACE.ldata ? CFG_TRACE.ldata : CFG_TRACE.gdata ? CFG_TRACE.gdata : "ignore", "ignore"))
  866. return;
  867. parent = getpid();
  868. #ifdef __linux__
  869. egg_bzero(&sv, sizeof(sv));
  870. sv.sa_handler = got_trace;
  871. sigemptyset(&sv.sa_mask);
  872. oldsv = NULL;
  873. sigaction(SIGTRAP, &sv, oldsv);
  874. traced = 1;
  875. asm("INT3");
  876. sigaction(SIGTRAP, oldsv, NULL);
  877. if (traced)
  878. detected(DETECT_TRACE, STR("I'm being traced!"));
  879. else {
  880. x = fork();
  881. if (x == -1)
  882. return;
  883. else if (x == 0) {
  884. i = ptrace(PTRACE_ATTACH, parent, 0, 0);
  885. if (i == (-1) && errno == EPERM)
  886. detected(DETECT_TRACE, STR("I'm being traced!"));
  887. else {
  888. waitpid(parent, &i, 0);
  889. kill(parent, SIGCHLD);
  890. ptrace(PTRACE_DETACH, parent, 0, 0);
  891. kill(parent, SIGCHLD);
  892. }
  893. exit(0);
  894. } else
  895. wait(&i);
  896. }
  897. #endif /* __linux__ */
  898. #ifdef __FreeBSD__
  899. x = fork();
  900. if (x == -1)
  901. return;
  902. else if (x == 0) {
  903. i = ptrace(PT_ATTACH, parent, 0, 0);
  904. if (i == (-1) && errno == EBUSY)
  905. detected(DETECT_TRACE, STR("I'm being traced"));
  906. else {
  907. wait(&i);
  908. i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
  909. kill(parent, SIGCHLD);
  910. wait(&i);
  911. i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
  912. wait(&i);
  913. }
  914. exit(0);
  915. } else
  916. waitpid(x, NULL, 0);
  917. #endif /* __FreeBSD__ */
  918. #ifdef __OpenBSD__
  919. x = fork();
  920. if (x == -1)
  921. return;
  922. else if (x == 0) {
  923. i = ptrace(PT_ATTACH, parent, 0, 0);
  924. if (i == (-1) && errno == EBUSY)
  925. detected(DETECT_TRACE, STR("I'm being traced"));
  926. else {
  927. wait(&i);
  928. i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
  929. kill(parent, SIGCHLD);
  930. wait(&i);
  931. i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
  932. wait(&i);
  933. }
  934. exit(0);
  935. } else
  936. waitpid(x, NULL, 0);
  937. #endif /* __OpenBSD__ */
  938. #endif /* S_ANTITRACE */
  939. }
  940. int shell_exec(char *cmdline, char *input, char **output, char **erroutput)
  941. {
  942. FILE *inpFile,
  943. *outFile,
  944. *errFile;
  945. char tmpfile[161];
  946. int x, fd;
  947. int parent = getpid();
  948. if (!cmdline)
  949. return 0;
  950. /* Set up temp files */
  951. /* always use mkstemp() when handling temp filess! -dizz */
  952. sprintf(tmpfile, STR("%s.in-XXXXXX"), tempdir);
  953. if ((fd = mkstemp(tmpfile)) == -1 || (inpFile = fdopen(fd, "w+")) == NULL) {
  954. if (fd != -1) {
  955. unlink(tmpfile);
  956. close(fd);
  957. }
  958. putlog(LOG_ERRORS, "*" , STR("exec: Couldn't open '%s': %s"), tmpfile, strerror(errno));
  959. return 0;
  960. }
  961. unlink(tmpfile);
  962. if (input) {
  963. if (fwrite(input, 1, strlen(input), inpFile) != strlen(input)) {
  964. fclose(inpFile);
  965. putlog(LOG_ERRORS, "*", STR("exec: Couldn't write to '%s': %s"), tmpfile, strerror(errno));
  966. return 0;
  967. }
  968. fseek(inpFile, 0, SEEK_SET);
  969. }
  970. unlink(tmpfile);
  971. sprintf(tmpfile, STR("%s.err-XXXXXX"), tempdir);
  972. if ((fd = mkstemp(tmpfile)) == -1 || (errFile = fdopen(fd, "w+")) == NULL) {
  973. if (fd != -1) {
  974. unlink(tmpfile);
  975. close(fd);
  976. }
  977. putlog(LOG_ERRORS, "*", STR("exec: Couldn't open '%s': %s"), tmpfile, strerror(errno));
  978. return 0;
  979. }
  980. unlink(tmpfile);
  981. sprintf(tmpfile, STR("%s.out-XXXXXX"), tempdir);
  982. if ((fd = mkstemp(tmpfile)) == -1 || (outFile = fdopen(fd, "w+")) == NULL) {
  983. if (fd != -1) {
  984. unlink(tmpfile);
  985. close(fd);
  986. }
  987. putlog(LOG_ERRORS, "*", STR("exec: Couldn't open '%s': %s"), tmpfile, strerror(errno));
  988. return 0;
  989. }
  990. unlink(tmpfile);
  991. x = fork();
  992. if (x == -1) {
  993. putlog(LOG_ERRORS, "*", STR("exec: fork() failed: %s"), strerror(errno));
  994. fclose(inpFile);
  995. fclose(errFile);
  996. fclose(outFile);
  997. return 0;
  998. }
  999. if (x) {
  1000. /* Parent: wait for the child to complete */
  1001. int st = 0;
  1002. waitpid(x, &st, 0);
  1003. /* Now read the files into the buffers */
  1004. fclose(inpFile);
  1005. fflush(outFile);
  1006. fflush(errFile);
  1007. if (erroutput) {
  1008. char *buf;
  1009. int fs;
  1010. fseek(errFile, 0, SEEK_END);
  1011. fs = ftell(errFile);
  1012. if (fs == 0) {
  1013. (*erroutput) = NULL;
  1014. } else {
  1015. buf = malloc(fs + 1);
  1016. fseek(errFile, 0, SEEK_SET);
  1017. fread(buf, 1, fs, errFile);
  1018. buf[fs] = 0;
  1019. (*erroutput) = buf;
  1020. }
  1021. }
  1022. fclose(errFile);
  1023. if (output) {
  1024. char *buf;
  1025. int fs;
  1026. fseek(outFile, 0, SEEK_END);
  1027. fs = ftell(outFile);
  1028. if (fs == 0) {
  1029. (*output) = NULL;
  1030. } else {
  1031. buf = malloc(fs + 1);
  1032. fseek(outFile, 0, SEEK_SET);
  1033. fread(buf, 1, fs, outFile);
  1034. buf[fs] = 0;
  1035. (*output) = buf;
  1036. }
  1037. }
  1038. fclose(outFile);
  1039. return 1;
  1040. } else {
  1041. /* Child: make fd's and set them up as std* */
  1042. int ind,
  1043. outd,
  1044. errd;
  1045. char *argv[4];
  1046. ind = fileno(inpFile);
  1047. outd = fileno(outFile);
  1048. errd = fileno(errFile);
  1049. if (dup2(ind, STDIN_FILENO) == (-1)) {
  1050. kill(parent, SIGCHLD);
  1051. exit(1);
  1052. }
  1053. if (dup2(outd, STDOUT_FILENO) == (-1)) {
  1054. kill(parent, SIGCHLD);
  1055. exit(1);
  1056. }
  1057. if (dup2(errd, STDERR_FILENO) == (-1)) {
  1058. kill(parent, SIGCHLD);
  1059. exit(1);
  1060. }
  1061. argv[0] = STR("sh");
  1062. argv[1] = STR("-c");
  1063. argv[2] = cmdline;
  1064. argv[3] = NULL;
  1065. execvp(argv[0], &argv[0]);
  1066. kill(parent, SIGCHLD);
  1067. exit(1);
  1068. }
  1069. }
  1070. /* Update system code
  1071. */
  1072. int ucnt = 0;
  1073. void updatelocal(void)
  1074. {
  1075. #ifdef LEAF
  1076. module_entry *me;
  1077. #endif /* LEAF */
  1078. Context;
  1079. if (ucnt < 300) {
  1080. ucnt++;
  1081. return;
  1082. }
  1083. del_hook(HOOK_SECONDLY, (Function) updatelocal);
  1084. ucnt = 0;
  1085. /* let's drop the server connection ASAP */
  1086. #ifdef LEAF
  1087. if ((me = module_find("server", 0, 0))) {
  1088. Function *func = me->funcs;
  1089. (func[SERVER_NUKESERVER]) ("Updating...");
  1090. }
  1091. #endif /* LEAF */
  1092. botnet_send_chat(-1, botnetnick, "Updating...");
  1093. botnet_send_bye();
  1094. fatal("Updating...", 1);
  1095. usleep(2000 * 500);
  1096. unlink(pid_file); //if this fails it is ok, cron will restart the bot, *hopefully*
  1097. system(binname); //start new bot.
  1098. exit(0);
  1099. }
  1100. int updatebin(int idx, char *par, int autoi)
  1101. {
  1102. char *path = NULL,
  1103. *newbin;
  1104. char buf[DIRMAX], old[DIRMAX], testbuf[DIRMAX];
  1105. struct stat sb;
  1106. int i;
  1107. #ifdef LEAF
  1108. module_entry *me;
  1109. #endif /* LEAF */
  1110. buf[0] = testbuf[0] = old[0] = 0;
  1111. path = newsplit(&par);
  1112. par = path;
  1113. if (!par[0]) {
  1114. if (idx)
  1115. dprintf(idx, STR("Not enough parameters.\n"));
  1116. return 1;
  1117. }
  1118. path = malloc(strlen(binname) + strlen(par) + 2);
  1119. strcpy(path, binname);
  1120. newbin = strrchr(path, '/');
  1121. if (!newbin) {
  1122. free(path);
  1123. if (idx)
  1124. dprintf(idx, STR("Don't know current binary name\n"));
  1125. return 1;
  1126. }
  1127. newbin++;
  1128. if (strchr(par, '/')) {
  1129. *newbin = 0;
  1130. if (idx)
  1131. dprintf(idx, STR("New binary must be in %s and name must be specified without path information\n"), path);
  1132. free(path);
  1133. return 1;
  1134. }
  1135. strcpy(newbin, par);
  1136. if (!strcmp(path, binname)) {
  1137. free(path);
  1138. if (idx)
  1139. dprintf(idx, STR("Can't update with the current binary\n"));
  1140. return 1;
  1141. }
  1142. if (stat(path, &sb)) {
  1143. if (idx)
  1144. dprintf(idx, STR("%s can't be accessed\n"), path);
  1145. free(path);
  1146. return 1;
  1147. }
  1148. if (chmod(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
  1149. if (idx)
  1150. dprintf(idx, STR("Can't set mode 0600 on %s\n"), path);
  1151. free(path);
  1152. return 1;
  1153. }
  1154. //make a backup just in case.
  1155. egg_snprintf(old, sizeof old, "%s.bin.old", tempdir);
  1156. copyfile(binname, old);
  1157. /* The binary should return '2' when ran with -2, if not it's probably corrupt. */
  1158. egg_snprintf(testbuf, sizeof testbuf, "%s -2", path);
  1159. i = system(testbuf);
  1160. if (i == -1 || WEXITSTATUS(i) != 2) {
  1161. if (idx)
  1162. dprintf(idx, STR("Couldn't restart new binary (error %d)\n"), i);
  1163. putlog(LOG_MISC, "*", STR("Couldn't restart new binary (error %d)\n"), i);
  1164. return i;
  1165. }
  1166. if (movefile(path, binname)) {
  1167. if (idx)
  1168. dprintf(idx, STR("Can't rename %s to %s\n"), path, binname);
  1169. free(path);
  1170. return 1;
  1171. }
  1172. sprintf(buf, "%s", binname);
  1173. #ifdef LEAF
  1174. if (localhub) {
  1175. /* if localhub = 1, this is the spawn bot and controls
  1176. * the spawning of new bots. */
  1177. sprintf(buf, "%s -L %s -P %d", buf, botnetnick, getpid());
  1178. }
  1179. #endif /* LEAF */
  1180. //safe to run new binary..
  1181. #ifdef LEAF
  1182. if (!autoi && !localhub) //dont delete pid for auto update!!!
  1183. #endif /* LEAF */
  1184. unlink(pid_file); //delete pid so new binary doesnt exit.
  1185. #ifdef HUB
  1186. listen_all(my_port, 1); //close the listening port...
  1187. usleep(5000);
  1188. #endif /* HUB */
  1189. putlog(LOG_DEBUG, "*", "Running for update: %s", buf);
  1190. #ifdef LEAF
  1191. if (!autoi && localhub) {
  1192. /* let's drop the server connection ASAP */
  1193. if ((me = module_find("server", 0, 0))) {
  1194. Function *func = me->funcs;
  1195. (func[SERVER_NUKESERVER]) ("Updating...");
  1196. }
  1197. #endif /* LEAF */
  1198. if (idx)
  1199. dprintf(idx, STR("Updating...bye\n"));
  1200. putlog(LOG_MISC, "*", STR("Updating...\n"));
  1201. botnet_send_chat(-1, botnetnick, "Updating...");
  1202. botnet_send_bye();
  1203. fatal("Updating...", 1);
  1204. usleep(2000 * 500);
  1205. system(buf); /* run the binary, it SHOULD work from earlier tests.. */
  1206. exit(0);
  1207. #ifdef LEAF
  1208. } else if (localhub && autoi) {
  1209. system(buf);
  1210. add_hook(HOOK_SECONDLY, (Function) updatelocal);
  1211. return 0;
  1212. }
  1213. #endif /* LEAF */
  1214. /* this should never be reached */
  1215. return 2;
  1216. }
  1217. int bot_aggressive_to(struct userrec *u)
  1218. {
  1219. char mypval[20],
  1220. botpval[20];
  1221. link_pref_val(u, botpval);
  1222. link_pref_val(get_user_by_handle(userlist, botnetnick), mypval);
  1223. if (strcmp(mypval, botpval) < 0)
  1224. return 1;
  1225. else
  1226. return 0;
  1227. }
  1228. void detected(int code, char *msg)
  1229. {
  1230. #ifdef LEAF
  1231. module_entry *me;
  1232. #endif /* LEAF */
  1233. char *p = NULL;
  1234. char tmp[512];
  1235. struct userrec *u;
  1236. struct flag_record fr = { FR_GLOBAL, 0, 0 };
  1237. int act;
  1238. u = get_user_by_handle(userlist, botnetnick);
  1239. #ifdef S_LASTCHECK
  1240. if (code == DETECT_LOGIN)
  1241. p = (char *) (CFG_LOGIN.ldata ? CFG_LOGIN.ldata : (CFG_LOGIN.gdata ? CFG_LOGIN.gdata : NULL));
  1242. #endif /* S_LASTCHECK */
  1243. #ifdef S_ANTITRACE
  1244. if (code == DETECT_TRACE)
  1245. p = (char *) (CFG_TRACE.ldata ? CFG_TRACE.ldata : (CFG_TRACE.gdata ? CFG_TRACE.gdata : NULL));
  1246. #endif /* S_ANTITRACE */
  1247. #ifdef S_PROMISC
  1248. if (code == DETECT_PROMISC)
  1249. p = (char *) (CFG_PROMISC.ldata ? CFG_PROMISC.ldata : (CFG_PROMISC.gdata ? CFG_PROMISC.gdata : NULL));
  1250. #endif /* S_PROMISC */
  1251. #ifdef S_PROCESSCHECK
  1252. if (code == DETECT_PROCESS)
  1253. p = (char *) (CFG_BADPROCESS.ldata ? CFG_BADPROCESS.ldata : (CFG_BADPROCESS.gdata ? CFG_BADPROCESS.gdata : NULL));
  1254. #endif /* S_PROMISC */
  1255. #ifdef S_HIJACKCHECK
  1256. if (code == DETECT_SIGCONT)
  1257. p = (char *) (CFG_HIJACK.ldata ? CFG_HIJACK.ldata : (CFG_HIJACK.gdata ? CFG_HIJACK.gdata : NULL));
  1258. #endif /* S_PROMISC */
  1259. if (!p)
  1260. act = DET_WARN;
  1261. else if (!strcmp(p, STR("die")))
  1262. act = DET_DIE;
  1263. else if (!strcmp(p, STR("reject")))
  1264. act = DET_REJECT;
  1265. else if (!strcmp(p, STR("suicide")))
  1266. act = DET_SUICIDE;
  1267. else if (!strcmp(p, STR("ignore")))
  1268. act = DET_IGNORE;
  1269. else
  1270. act = DET_WARN;
  1271. switch (act) {
  1272. case DET_IGNORE:
  1273. break;
  1274. case DET_WARN:
  1275. putlog(LOG_WARN, "*", msg);
  1276. break;
  1277. case DET_REJECT:
  1278. do_fork();
  1279. putlog(LOG_WARN, "*", STR("Setting myself +d: %s"), msg);
  1280. sprintf(tmp, STR("+d: %s"), msg);
  1281. set_user(&USERENTRY_COMMENT, u, tmp);
  1282. get_user_flagrec(u, &fr, 0);
  1283. fr.global = USER_DEOP | USER_BOT;
  1284. set_user_flagrec(u, &fr, 0);
  1285. sleep(1);
  1286. break;
  1287. case DET_DIE:
  1288. putlog(LOG_WARN, "*", STR("Dying: %s"), msg);
  1289. sprintf(tmp, STR("Dying: %s"), msg);
  1290. set_user(&USERENTRY_COMMENT, u, tmp);
  1291. #ifdef LEAF
  1292. if ((me = module_find("server", 0, 0))) {
  1293. Function *func = me->funcs;
  1294. (func[SERVER_NUKESERVER]) ("BBL");
  1295. }
  1296. #endif /* LEAF */
  1297. sleep(1);
  1298. fatal(msg, 0);
  1299. break;
  1300. case DET_SUICIDE:
  1301. putlog(LOG_WARN, "*", STR("Comitting suicide: %s"), msg);
  1302. sprintf(tmp, STR("Suicide: %s"), msg);
  1303. set_user(&USERENTRY_COMMENT, u, tmp);
  1304. #ifdef LEAF
  1305. if ((me = module_find("server", 0, 0))) {
  1306. Function *func = me->funcs;
  1307. (func[SERVER_NUKESERVER]) ("HARAKIRI!!");
  1308. }
  1309. #endif /* LEAF */
  1310. sleep(1);
  1311. unlink(binname);
  1312. #ifdef HUB
  1313. unlink(userfile);
  1314. sprintf(tmp, STR("%s~"), userfile);
  1315. unlink(tmp);
  1316. #endif /* HUB */
  1317. fatal(msg, 0);
  1318. break;
  1319. }
  1320. }
  1321. char kickprefix[25] = "";
  1322. char bankickprefix[25] = "";
  1323. char * kickreason(int kind) {
  1324. int r;
  1325. r=random();
  1326. switch (kind) {
  1327. case KICK_BANNED:
  1328. switch (r % 6) {
  1329. case 0: return STR("bye");
  1330. case 1: return STR("banned");
  1331. case 2: return STR("see you in hell");
  1332. case 3: return STR("go away");
  1333. case 4: return STR("cya around lewser");
  1334. case 5: return STR("unwanted!");
  1335. }
  1336. case KICK_KUSER:
  1337. switch (r % 4) {
  1338. case 0: return STR("not wanted");
  1339. case 1: return STR("something tells me you're annoying");
  1340. case 2: return STR("don't bug me lewser");
  1341. case 3: return STR("creep");
  1342. }
  1343. case KICK_KICKBAN:
  1344. switch (r % 4) {
  1345. case 0: return STR("gone");
  1346. case 1: return STR("stupid");
  1347. case 2: return STR("lewser");
  1348. case 3: return STR("...");
  1349. }
  1350. case KICK_MASSDEOP:
  1351. switch (r % 8) {
  1352. case 0: return STR("spammer!");
  1353. case 1: return STR("easy on the modes now");
  1354. case 2: return STR("mode this");
  1355. case 3: return STR("nice try");
  1356. case 4: return STR("really?");
  1357. case 5: return STR("you lose");
  1358. case 6: return STR("scary... really scary...");
  1359. case 7: return STR("i win kthx");
  1360. }
  1361. case KICK_BADOP:
  1362. switch (r % 5) {
  1363. case 0: return STR("neat...");
  1364. case 1: return STR("oh, no you don't. go away.");
  1365. case 2: return STR("didn't you forget something now?");
  1366. case 3: return STR("no");
  1367. case 4: return STR("hijack this");
  1368. }
  1369. case KICK_BADOPPED:
  1370. switch (r % 5) {
  1371. case 0: return STR("fuck off kid");
  1372. case 1: return STR("asl?");
  1373. case 2: return STR("whoa... what a hacker... skills!");
  1374. case 3: return STR("yes! yes! yes! hit me baby one more time!");
  1375. case 4: return STR("with your skills, you're better off jacking off than hijacking");
  1376. }
  1377. case KICK_MANUALOP:
  1378. switch (r % 6) {
  1379. case 0: return STR("naughty kid");
  1380. case 1: return STR("didn't someone tell you that is bad?");
  1381. case 2: return STR("want perm?");
  1382. case 3: return STR("see how much good that did you?");
  1383. case 4: return STR("not a smart move...");
  1384. case 5: return STR("jackass!");
  1385. }
  1386. case KICK_MANUALOPPED:
  1387. switch (r % 8) {
  1388. case 0: return STR("your pal got mean friends. like me.");
  1389. case 1: return STR("uhh now.. don't wake me up...");
  1390. case 2: return STR("hi hun. missed me?");
  1391. case 3: return STR("spammer! die!");
  1392. case 4: return STR("boo!");
  1393. case 5: return STR("that @ was useful, don't ya think?");
  1394. case 6: return STR("not in my book");
  1395. case 7: return STR("lol, really?");
  1396. }
  1397. case KICK_CLOSED:
  1398. switch (r % 17) {
  1399. case 0: return STR("locked");
  1400. case 1: return STR("later");
  1401. case 2: return STR("closed for now");
  1402. case 3: return STR("come back later");
  1403. case 4: return STR("better safe than sorry");
  1404. case 5: return STR("cleanup, come back later");
  1405. case 6: return STR("this channel is closed");
  1406. case 7: return STR("shutting down for now");
  1407. case 8: return STR("lockdown");
  1408. case 9: return STR("reopening later");
  1409. case 10: return STR("not for the public atm");
  1410. case 11: return STR("private channel for now");
  1411. case 12: return STR("might reopen soon, might reopen later");
  1412. case 13: return STR("you're not supposed to be here right now");
  1413. case 14: return STR("sorry, closed");
  1414. case 15: return STR("try us later, atm we're locked down");
  1415. case 16: return STR("closed. try tomorrow");
  1416. }
  1417. case KICK_FLOOD:
  1418. switch (r % 7) {
  1419. case 0: return STR("so much bullshit in such a short time. amazing.");
  1420. case 1: return STR("slow down. i'm trying to read here.");
  1421. case 2: return STR("uhm... you actually think irc is for talking?");
  1422. case 3: return STR("talk talk talk");
  1423. case 4: return STR("blabbering are we?");
  1424. case 5: return STR("... and i don't even like you!");
  1425. case 6: return STR("and you're outa here...");
  1426. }
  1427. case KICK_NICKFLOOD:
  1428. switch (r % 7) {
  1429. case 0: return STR("make up your mind?");
  1430. case 1: return STR("be schizofrenic elsewhere");
  1431. case 2: return STR("I'm loosing track of you... not!");
  1432. case 3: return STR("that is REALLY annoying");
  1433. case 4: return STR("try this: /NICK n00b");
  1434. case 5: return STR("playing hide 'n' seek?");
  1435. case 6: return STR("gotcha!");
  1436. }
  1437. case KICK_KICKFLOOD:
  1438. switch (r % 6) {
  1439. case 0: return STR("easier to just leave if you wan't to be alone");
  1440. case 1: return STR("cool down");
  1441. case 2: return STR("don't be so damned aggressive. that's my job.");
  1442. case 3: return STR("kicking's fun, isn't it?");
  1443. case 4: return STR("what's the rush?");
  1444. case 5: return STR("next time you do that, i'll kick you again");
  1445. }
  1446. case KICK_BOGUSUSERNAME:
  1447. return STR("bogus username");
  1448. case KICK_MEAN:
  1449. switch (r % 11) {
  1450. case 0: return STR("hey! that wasn't very nice!");
  1451. case 1: return STR("don't fuck with my pals");
  1452. case 2: return STR("meanie!");
  1453. case 3: return STR("I can be a bitch too...");
  1454. case 4: return STR("leave the bots alone, will ya?");
  1455. case 5: return STR("not very clever");
  1456. case 6: return STR("watch it");
  1457. case 7: return STR("fuck off");
  1458. case 8: return STR("easy now. that's a friend.");
  1459. case 9: return STR("abuse of power. leave that to me, will ya?");
  1460. case 10: return STR("there as some things you cannot do, and that was one of them...");
  1461. }
  1462. case KICK_BOGUSKEY:
  1463. return STR("I have a really hard time reading that key");
  1464. default:
  1465. return "OMFG@YUO";
  1466. }
  1467. }
  1468. void makeplaincookie(char *chname, char *nick, char *buf)
  1469. {
  1470. /*
  1471. plain cookie:
  1472. Last 6 digits of time
  1473. Last 5 chars of nick
  1474. Last 4 regular chars of chan
  1475. */
  1476. char work[256],
  1477. work2[256];
  1478. int i,
  1479. n;
  1480. sprintf(work, STR("%010li"), (now + timesync));
  1481. strcpy(buf, (char *) &work[4]);
  1482. work[0] = 0;
  1483. if (strlen(nick) < 5)
  1484. while (strlen(work) + strlen(nick) < 5)
  1485. strcat(work, " ");
  1486. else
  1487. strcpy(work, (char *) &nick[strlen(nick) - 5]);
  1488. strcat(buf, work);
  1489. n = 3;
  1490. for (i = strlen(chname) - 1; (i >= 0) && (n >= 0); i--)
  1491. if (((unsigned char) chname[i] < 128) && ((unsigned char) chname[i] > 32)) {
  1492. work2[n] = tolower(chname[i]);
  1493. n--;
  1494. }
  1495. while (n >= 0)
  1496. work2[n--] = ' ';
  1497. work2[4] = 0;
  1498. strcat(buf, work2);
  1499. }
  1500. int goodpass(char *pass, int idx, char *nick)
  1501. {
  1502. char *tell;
  1503. #ifdef S_NAZIPASS
  1504. int i, nalpha = 0, lcase = 0, ucase = 0, ocase = 0, tc;
  1505. #endif /* S_NAZIPASS */
  1506. if (!pass[0])
  1507. return 0;
  1508. tell = malloc(300);
  1509. #ifdef S_NAZIPASS
  1510. for (i = 0; i < strlen(pass); i++) {
  1511. tc = (int) pass[i];
  1512. if (tc < 58 && tc > 47)
  1513. ocase++; //number
  1514. else if (tc < 91 && tc > 64)
  1515. ucase++; //upper case
  1516. else if (tc < 123 && tc > 96)
  1517. lcase++; //lower case
  1518. else
  1519. nalpha++; //non-alphabet/number
  1520. }
  1521. /* if (ocase < 1 || lcase < 2 || ucase < 2 || nalpha < 1 || strlen(pass) < 8) { */
  1522. if (ocase < 1 || lcase < 2 || ucase < 2 || strlen(pass) < 8) {
  1523. #else /* !S_NAZIPASS */
  1524. if (strlen(pass) < 8) {
  1525. #endif /* S_NAZIPASS */
  1526. sprintf(tell, "Insecure pass, must be: ");
  1527. #ifdef S_NAZIPASS
  1528. if (ocase < 1)
  1529. strcat(tell, "\002>= 1 number\002, ");
  1530. else
  1531. strcat(tell, ">= 1 number, ");
  1532. if (lcase < 2)
  1533. strcat(tell, "\002>= 2 lcase\002, ");
  1534. else
  1535. strcat(tell, ">= 2 lowercase, ");
  1536. if (ucase < 2)
  1537. strcat(tell, "\002>= 2 ucase\002, ");
  1538. else
  1539. strcat(tell, ">= 2 uppercase, ");
  1540. /* This is annoying as hell
  1541. if (nalpha < 1)
  1542. strcat(tell, "\002>= 1 non-alpha/num\002, ");
  1543. else
  1544. strcat(tell, ">= 1 non-alpha/num, ");
  1545. */
  1546. #endif /* S_NAZIPASS */
  1547. if (strlen(pass) < 8)
  1548. strcat(tell, "\002>= 8 chars.\002");
  1549. else
  1550. strcat(tell, ">= 8 chars.");
  1551. if (idx)
  1552. dprintf(idx, "%s\n", tell);
  1553. else if (nick[0])
  1554. dprintf(DP_HELP, STR("NOTICE %s :%s\n"), nick, tell);
  1555. free(tell);
  1556. return 0;
  1557. }
  1558. free(tell);
  1559. return 1;
  1560. }
  1561. char *replace (char *string, char *oldie, char *newbie)
  1562. {
  1563. static char newstring[1024] = "";
  1564. int str_index, newstr_index, oldie_index, end, new_len, old_len, cpy_len;
  1565. char *c;
  1566. if (string == NULL) return "";
  1567. if ((c = (char *) strstr(string, oldie)) == NULL) return string;
  1568. new_len = strlen(newbie);
  1569. old_len = strlen(oldie);
  1570. end = strlen(string) - old_len;
  1571. oldie_index = c - string;
  1572. newstr_index = 0;
  1573. str_index = 0;
  1574. while(str_index <= end && c != NULL) {
  1575. cpy_len = oldie_index-str_index;
  1576. strncpy(newstring + newstr_index, string + str_index, cpy_len);
  1577. newstr_index += cpy_len;
  1578. str_index += cpy_len;
  1579. strcpy(newstring + newstr_index, newbie);
  1580. newstr_index += new_len;
  1581. str_index += old_len;
  1582. if((c = (char *) strstr(string + str_index, oldie)) != NULL)
  1583. oldie_index = c - string;
  1584. }
  1585. strcpy(newstring + newstr_index, string + str_index);
  1586. return (newstring);
  1587. }
  1588. char *getfullbinname(char *argv0)
  1589. {
  1590. char *cwd, *bin, *p, *p2;
  1591. bin = strdup(argv0);
  1592. if (bin[0] == '/') {
  1593. return bin;
  1594. }
  1595. cwd = malloc(8192);
  1596. getcwd(cwd, 8191);
  1597. cwd[8191] = 0;
  1598. if (cwd[strlen(cwd) - 1] == '/')
  1599. cwd[strlen(cwd) - 1] = 0;
  1600. p = bin;
  1601. p2 = strchr(p, '/');
  1602. while (p) {
  1603. if (p2)
  1604. *p2++ = 0;
  1605. if (!strcmp(p, "..")) {
  1606. p = strrchr(cwd, '/');
  1607. if (p)
  1608. *p = 0;
  1609. } else if (strcmp(p, ".")) {
  1610. strcat(cwd, "/");
  1611. strcat(cwd, p);
  1612. }
  1613. p = p2;
  1614. if (p)
  1615. p2 = strchr(p, '/');
  1616. }
  1617. free(bin);
  1618. bin = strdup(cwd);
  1619. free(cwd);
  1620. return bin;
  1621. }
  1622. char *werr_tostr(int errnum)
  1623. {
  1624. switch (errnum) {
  1625. case ERR_BINSTAT:
  1626. return STR("Cannot access binary");
  1627. case ERR_BINMOD:
  1628. return STR("Cannot chmod() binary");
  1629. case ERR_PASSWD:
  1630. return STR("Cannot access the global passwd file");
  1631. case ERR_WRONGBINDIR:
  1632. return STR("Wrong directory/binary name");
  1633. case ERR_CONFSTAT:
  1634. #ifdef LEAF
  1635. return STR("Cannot access config directory (~/.ssh/)");
  1636. #else
  1637. return STR("Cannot access config directory (./)");
  1638. #endif /* LEAF */
  1639. case ERR_TMPSTAT:
  1640. #ifdef LEAF
  1641. return STR("Cannot access tmp directory (~/.ssh/.../)");
  1642. #else
  1643. return STR("Cannot access config directory (./tmp/)");
  1644. #endif /* LEAF */
  1645. case ERR_CONFDIRMOD:
  1646. #ifdef LEAF
  1647. return STR("Cannot chmod() config directory (~/.ssh/)");
  1648. #else
  1649. return STR("Cannot chmod() config directory (./)");
  1650. #endif /* LEAF */
  1651. case ERR_CONFMOD:
  1652. #ifdef LEAF
  1653. return STR("Cannot chmod() config (~/.ssh/.known_hosts/)");
  1654. #else
  1655. return STR("Cannot chmod() config (./conf)");
  1656. #endif /* LEAF */
  1657. case ERR_TMPMOD:
  1658. #ifdef LEAF
  1659. return STR("Cannot chmod() tmp directory (~/.ssh/.../)");
  1660. #else
  1661. return STR("Cannot chmod() tmp directory (./tmp)");
  1662. #endif /* LEAF */
  1663. case ERR_NOCONF:
  1664. #ifdef LEAF
  1665. return STR("The local config is missing (~/.ssh/.known_hosts)");
  1666. #else
  1667. return STR("The local config is missing (./conf)");
  1668. #endif /* LEAF */
  1669. case ERR_CONFBADENC:
  1670. return STR("Encryption in config is wrong/corrupt");
  1671. case ERR_WRONGUID:
  1672. return STR("UID in conf does not match getuid()");
  1673. case ERR_WRONGUNAME:
  1674. return STR("Uname in conf does not match uname()");
  1675. case ERR_BADCONF:
  1676. return STR("Config file is incomplete");
  1677. default:
  1678. return STR("Unforseen error");
  1679. }
  1680. }
  1681. void werr(int errnum)
  1682. {
  1683. putlog(LOG_MISC, "*", STR("error #%d"), errnum);
  1684. sdprintf(STR("error translates to: %s"), werr_tostr(errnum));
  1685. printf(STR("(segmentation fault)\n"));
  1686. fatal("", 0);
  1687. }
  1688. void local_check_should_lock()
  1689. {
  1690. module_entry *me;
  1691. if ((me = module_find("channels", 0, 0))) {
  1692. Function *func = me->funcs;
  1693. /* check_should_lock() */
  1694. (func[51]) ();
  1695. }
  1696. }
  1697. /* convert binary hashes to hex */
  1698. char *btoh(const unsigned char *md, int len)
  1699. {
  1700. int i;
  1701. char buf[100], *ret;
  1702. for (i = 0; i < len; i++)
  1703. sprintf(&(buf[i*2]), "%02x", md[i]);
  1704. ret = buf;
  1705. return ret;
  1706. }
  1707. #define HELP_BOLD 1
  1708. #define HELP_REV 2
  1709. #define HELP_UNDER 4
  1710. #define HELP_FLASH 8
  1711. /* so many string++ is making the problem */
  1712. void showhelp (int idx, struct flag_record *flags, char *string)
  1713. {
  1714. static int help_flags;
  1715. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
  1716. char helpstr[12288], tmp[2] = "", flagstr[10] = "";
  1717. int ok = 1;
  1718. helpstr[0] = 0;
  1719. while (string && string[0]) {
  1720. if (*string == '%') {
  1721. if (!strncmp(string + 1, "{+", 2)) {
  1722. while (*string && *string != '+') {
  1723. string++;
  1724. }
  1725. flagstr[0] = 0;
  1726. while (*string && *string != '}') {
  1727. sprintf(tmp, "%c", *string);
  1728. strcat(flagstr, tmp);
  1729. string++;
  1730. }
  1731. string++;
  1732. break_down_flags(flagstr, &fr, NULL);
  1733. if (flagrec_ok(&fr, flags)) {
  1734. ok = 1;
  1735. while (*string && *string != '%') {
  1736. sprintf(tmp, "%c", *string);
  1737. strcat(helpstr, tmp);
  1738. string++;
  1739. }
  1740. if (!strncmp(string + 1, "{-", 2)) {
  1741. ok = 1;
  1742. while (*string && *string != '}') {
  1743. string++;
  1744. }
  1745. string++;
  1746. }
  1747. } else {
  1748. ok = 0;
  1749. }
  1750. } else if (!strncmp(string + 1, "{-", 2)) {
  1751. ok = 1;
  1752. while (*string && *string != '}') {
  1753. string++;
  1754. }
  1755. string++;
  1756. } else if (*string == '{') {
  1757. while (*string && *string != '}') {
  1758. string++;
  1759. }
  1760. } else if (*(string + 1) == 'b') {
  1761. string += 2;
  1762. if (help_flags & HELP_BOLD) {
  1763. help_flags &= ~HELP_BOLD;
  1764. strcat(helpstr, color(idx, BOLD_CLOSE, 0));
  1765. } else {
  1766. help_flags |= HELP_BOLD;
  1767. strcat(helpstr, color(idx, BOLD_OPEN, 0));
  1768. }
  1769. } else if (*(string + 1) == 'f') {
  1770. string += 2;
  1771. if (help_flags & HELP_FLASH) {
  1772. strcat(helpstr, color(idx, FLASH_CLOSE, 0));
  1773. help_flags &= ~HELP_FLASH;
  1774. } else {
  1775. help_flags |= HELP_FLASH;
  1776. strcat(helpstr, color(idx, FLASH_OPEN, 0));
  1777. }
  1778. } else if (*(string + 1) == 'd') {
  1779. string += 2;
  1780. strcat(helpstr, dcc_prefix);
  1781. } else if (*(string + 1) == '%') {
  1782. string += 2;
  1783. strcat(helpstr, "%");
  1784. } else {
  1785. if (ok) {
  1786. sprintf(tmp, "%c", *string);
  1787. strcat(helpstr, tmp);
  1788. }
  1789. string++;
  1790. }
  1791. } else {
  1792. if (ok) {
  1793. sprintf(tmp, "%c", *string);
  1794. strcat(helpstr, tmp);
  1795. }
  1796. string++;
  1797. }
  1798. }
  1799. helpstr[strlen(helpstr)] = 0;
  1800. if (helpstr[0]) dumplots(idx, "", helpstr);
  1801. }
  1802. /* Arrange the N elements of ARRAY in random order. */
  1803. static void shuffleArray(char *array[], int n)
  1804. {
  1805. int i;
  1806. for (i = 0; i < n; i++) {
  1807. int j = i + random() / (RAND_MAX / (n - i) + 1);
  1808. char *t = array[j];
  1809. array[j] = array[i];
  1810. array[i] = t;
  1811. }
  1812. }
  1813. void shuffle(char *string, char *delim)
  1814. {
  1815. char *array[501], *str, *work;
  1816. int len = 0, i = 0;
  1817. work = strdup(string);
  1818. str = strtok(work, delim);
  1819. while(str && *str)
  1820. {
  1821. array[len] = str;
  1822. len++;
  1823. str = strtok((char*) NULL, delim);
  1824. }
  1825. shuffleArray(array, len);
  1826. string[0] = 0;
  1827. for (i = 0; i < len; i++) {
  1828. strcat(string, array[i]);
  1829. if (i != len - 1)
  1830. strcat(string, delim);
  1831. }
  1832. free(work);
  1833. string[strlen(string)] = 0;
  1834. }
  1835. char *color(int idx, int type, int color)
  1836. {
  1837. int ansi = 0;
  1838. /* if user is connected over TELNET or !backgrd, show ANSI
  1839. * if they are relaying, they are most likely on an IRC client and should have mIRC codes
  1840. */
  1841. if ((idx && ((dcc[idx].type != &DCC_RELAYING) && (dcc[idx].status & STAT_TELNET))) || !backgrd) ansi++;
  1842. if (type == BOLD_OPEN) {
  1843. return ansi ? "\033[1m" : "\002";
  1844. } else if (type == BOLD_CLOSE) {
  1845. return ansi ? "\033[22m" : "\002";
  1846. } else if (type == UNDERLINE_OPEN) {
  1847. return ansi ? "\033[4m" : "\037";
  1848. } else if (type == UNDERLINE_CLOSE) {
  1849. return ansi ? "\033[24m" : "\037";
  1850. } else if (type == FLASH_OPEN) {
  1851. return ansi ? "\033[5m" : "\002\037";
  1852. } else if (type == FLASH_CLOSE) {
  1853. return ansi ? "\033[0m" : "\037\002";
  1854. } else if (type == COLOR_OPEN) {
  1855. switch (color) {
  1856. case C_BLACK: return ansi ? "\033[30m" : "\00301";
  1857. case C_RED: return ansi ? "\033[31m" : "\00305";
  1858. case C_GREEN: return ansi ? "\033[32m" : "\00303";
  1859. case C_BROWN: return ansi ? "\033[33m" : "\00307";
  1860. case C_BLUE: return ansi ? "\033[34m" : "\00302";
  1861. case C_PURPLE: return ansi ? "\033[35m" : "\00306";
  1862. case C_CYAN: return ansi ? "\033[36m" : "\00310";
  1863. case C_WHITE: return ansi ? "\033[1;37m" : "\00300";
  1864. case C_DARKGREY: return ansi ? "\033[1;30m" : "\00314";
  1865. case C_LIGHTRED: return ansi ? "\033[1;31m" : "\00304";
  1866. case C_LIGHTGREEN: return ansi ? "\033[1;32m" : "\00309";
  1867. case C_LIGHTBLUE: return ansi ? "\033[1;34m" : "\00312";
  1868. case C_LIGHTPURPLE: return ansi ? "\033[1;35m" : "\00313";
  1869. case C_LIGHTCYAN: return ansi ? "\033[1;36m" : "\00311";
  1870. case C_LIGHTGREY: return ansi ? "\033[37m" : "\00315";
  1871. case C_YELLOW: return ansi ? "\033[1;33m" : "\00308";
  1872. default: break;
  1873. }
  1874. } else if (type == COLOR_CLOSE) {
  1875. return ansi ? "\033[0m" : "\00300";
  1876. }
  1877. /* This should never be reached.. */
  1878. return "";
  1879. }
  1880. int email(char *subject, char *msg, int who)
  1881. {
  1882. struct utsname un;
  1883. char open[2048], addrs[1024];
  1884. int mail = 0, sendmail = 0;
  1885. FILE *f;
  1886. uname(&un);
  1887. if (is_file("/usr/sbin/sendmail"))
  1888. sendmail++;
  1889. else if (is_file("/usr/bin/mail"))
  1890. mail++;
  1891. else {
  1892. putlog(LOG_WARN, "*", "I Have no usable mail client.");
  1893. return 1;
  1894. }
  1895. open[0] = addrs[0] = 0;
  1896. if (who & EMAIL_OWNERS) {
  1897. sprintf(addrs, "%s", replace(owneremail, ",", " "));
  1898. }
  1899. if (who & EMAIL_TEAM) {
  1900. if (addrs[0])
  1901. sprintf(addrs, "%s wraith@shatow.net", addrs);
  1902. else
  1903. sprintf(addrs, "wraith@shatow.net");
  1904. }
  1905. if (sendmail)
  1906. sprintf(open, "/usr/sbin/sendmail -t");
  1907. else if (mail)
  1908. sprintf(open, "/usr/bin/mail %s -a \"From: %s@%s\" -s \"%s\" -a \"Content-Type: text/plain\"", addrs, (origbotname && origbotname[0]) ? origbotname : "none", un.nodename, subject);
  1909. if ((f = popen(open, "w"))) {
  1910. if (sendmail) {
  1911. struct passwd *pw;
  1912. pw = getpwuid(geteuid());
  1913. fprintf(f, "To: %s\n", addrs);
  1914. fprintf(f, "From: %s@%s\n", (origbotname && origbotname[0]) ? origbotname : pw->pw_name, un.nodename);
  1915. fprintf(f, "Subject: %s\n", subject);
  1916. fprintf(f, "Content-Type: text/plain\n");
  1917. }
  1918. fprintf(f, "%s\n", msg);
  1919. if (fflush(f))
  1920. return 1;
  1921. if (pclose(f))
  1922. return 1;
  1923. } else
  1924. return 1;
  1925. return 0;
  1926. }
  1927. void baduname(char *conf, char *my_uname) {
  1928. char *tmpfile = malloc(strlen(tempdir) + 3 + 1);
  1929. int send = 0;
  1930. tmpfile[0] = 0;
  1931. sprintf(tmpfile, "%s.un", tempdir);
  1932. sdprintf("CHECKING %s", tmpfile);
  1933. if (is_file(tmpfile)) {
  1934. struct stat ss;
  1935. time_t diff;
  1936. stat(tmpfile, &ss);
  1937. diff = now - ss.st_mtime;
  1938. if (diff >= 86400) send++; /* only send once a day */
  1939. } else {
  1940. FILE *fp;
  1941. if ((fp = fopen(tmpfile, "w"))) {
  1942. fprintf(fp, "\n");
  1943. fflush(fp);
  1944. fclose(fp);
  1945. send++; /* only send if we could write the file. */
  1946. }
  1947. }
  1948. if (send) {
  1949. struct passwd *pw;
  1950. struct utsname un;
  1951. char msg[501], subject[31];
  1952. pw = getpwuid(geteuid());
  1953. if (!pw) return;
  1954. uname(&un);
  1955. egg_snprintf(subject, sizeof subject, "CONF/UNAME() mismatch notice");
  1956. egg_snprintf(msg, sizeof msg, "This is an auto email from a wraith bot which has you in it's OWNER_EMAIL list..\n \nThe uname() output on this box has changed, probably due to a kernel upgrade...\nMy login is: %s\nConf : %s\nUname(): %s\n \nThis email will only be sent once a day while this error is present.\nYou need to login to my shell (%s) and fix my local config.\n", pw->pw_name, conf, my_uname, un.nodename);
  1957. email(subject, msg, EMAIL_OWNERS);
  1958. }
  1959. free(tmpfile);
  1960. }
  1961. char *homedir()
  1962. {
  1963. static char homedir[DIRMAX] = "";
  1964. if (!homedir || (homedir && !homedir[0])) {
  1965. char tmp[DIRMAX];
  1966. struct passwd *pw;
  1967. sdprintf(STR("If the bot dies after this, try compiling on Debian."));
  1968. Context;
  1969. pw = getpwuid(geteuid());
  1970. sdprintf(STR("End Debian suggestion."));
  1971. if (!pw)
  1972. werr(ERR_PASSWD);
  1973. Context;
  1974. egg_snprintf(tmp, sizeof tmp, "%s", pw->pw_dir);
  1975. Context;
  1976. realpath(tmp, homedir); /* this will convert lame home dirs of /home/blah->/usr/home/blah */
  1977. }
  1978. return homedir;
  1979. }
  1980. char *confdir()
  1981. {
  1982. static char confdir[DIRMAX] = "";
  1983. if (!confdir || (confdir && !confdir[0])) {
  1984. #ifdef LEAF
  1985. {
  1986. egg_snprintf(confdir, sizeof confdir, "%s/.ssh", homedir());
  1987. }
  1988. #endif /* LEAF */
  1989. #ifdef HUB
  1990. {
  1991. char *buf = strdup(binname);
  1992. egg_snprintf(confdir, sizeof confdir, "%s", dirname(buf));
  1993. free(buf);
  1994. }
  1995. #endif /* HUB */
  1996. }
  1997. return confdir;
  1998. }
  1999. char *my_uname()
  2000. {
  2001. static char os_uname[250] = "";
  2002. if (!os_uname || (os_uname && !os_uname[0])) {
  2003. char *unix_n, *vers_n;
  2004. struct utsname un;
  2005. if (uname(&un) < 0) {
  2006. unix_n = "*unkown*";
  2007. vers_n = "";
  2008. } else {
  2009. unix_n = un.nodename;
  2010. #ifdef __FreeBSD__
  2011. vers_n = un.release;
  2012. #else /* __linux__ */
  2013. vers_n = un.version;
  2014. #endif /* __FreeBSD__ */
  2015. }
  2016. egg_snprintf(os_uname, sizeof os_uname, "%s %s", unix_n, vers_n);
  2017. }
  2018. return os_uname;
  2019. }